Mercurial > mplayer.hg
annotate libass/ass_cache.c @ 19885:7f9b2b26e905
Replace silly compilation shell scripts by a simple Makefile.
author | diego |
---|---|
date | Mon, 18 Sep 2006 14:27:35 +0000 |
parents | 07be98a5dd5f |
children | 70352570e9ae |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <ft2build.h> | |
4 #include FT_FREETYPE_H | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
5 #include FT_GLYPH_H |
18937 | 6 |
7 #include <assert.h> | |
8 | |
9 #include "mp_msg.h" | |
10 #include "ass_fontconfig.h" | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
11 #include "ass_bitmap.h" |
18937 | 12 #include "ass_cache.h" |
13 | |
14 | |
15 typedef struct face_cache_item_s { | |
16 face_desc_t desc; | |
17 char* path; | |
18 int index; | |
19 FT_Face face; | |
20 } face_cache_item_t; | |
21 | |
22 #define MAX_FACE_CACHE_SIZE 100 | |
23 | |
24 static face_cache_item_t* face_cache; | |
25 static int face_cache_size; | |
26 | |
27 extern int no_more_font_messages; | |
28 | |
29 static int font_compare(face_desc_t* a, face_desc_t* b) { | |
30 if (strcmp(a->family, b->family) != 0) | |
31 return 0; | |
32 if (a->bold != b->bold) | |
33 return 0; | |
34 if (a->italic != b->italic) | |
35 return 0; | |
36 return 1; | |
37 } | |
38 | |
39 /** | |
40 * \brief Get a face object, either from cache or created through FreeType+FontConfig. | |
41 * \param library FreeType library object | |
42 * \param fontconfig_priv fontconfig private data | |
43 * \param desc required face description | |
44 * \param face out: the face object | |
45 */ | |
46 int ass_new_face(FT_Library library, void* fontconfig_priv, face_desc_t* desc, /*out*/ FT_Face* face) | |
47 { | |
48 FT_Error error; | |
49 int i; | |
50 char* path; | |
51 int index; | |
52 face_cache_item_t* item; | |
53 | |
54 for (i=0; i<face_cache_size; ++i) | |
55 if (font_compare(desc, &(face_cache[i].desc))) { | |
56 *face = face_cache[i].face; | |
57 return 0; | |
58 } | |
59 | |
60 if (face_cache_size == MAX_FACE_CACHE_SIZE) { | |
61 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "Too many fonts\n"); | |
62 return 1; | |
63 } | |
64 | |
65 path = fontconfig_select(fontconfig_priv, desc->family, desc->bold, desc->italic, &index); | |
66 | |
67 error = FT_New_Face(library, path, index, face); | |
68 if (error) { | |
69 if (!no_more_font_messages) | |
70 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error opening font: %s, %d\n", path, index); | |
71 no_more_font_messages = 1; | |
72 return 1; | |
73 } | |
74 | |
75 item = face_cache + face_cache_size; | |
76 item->path = strdup(path); | |
77 item->index = index; | |
78 item->face = *face; | |
79 memcpy(&(item->desc), desc, sizeof(face_desc_t)); | |
80 face_cache_size++; | |
81 return 0; | |
82 } | |
83 | |
84 void ass_face_cache_init(void) | |
85 { | |
86 face_cache = calloc(MAX_FACE_CACHE_SIZE, sizeof(face_cache_item_t)); | |
87 face_cache_size = 0; | |
88 } | |
89 | |
90 void ass_face_cache_done(void) | |
91 { | |
92 int i; | |
93 for (i = 0; i < face_cache_size; ++i) { | |
94 face_cache_item_t* item = face_cache + i; | |
95 if (item->face) FT_Done_Face(item->face); | |
96 if (item->path) free(item->path); | |
97 // FIXME: free desc ? | |
98 } | |
99 free(face_cache); | |
100 face_cache_size = 0; | |
101 } | |
102 | |
103 //--------------------------------- | |
104 // glyph cache | |
105 | |
106 #define GLYPH_HASH_SIZE (0xFFFF + 13) | |
107 | |
108 typedef struct glyph_hash_item_s { | |
109 glyph_hash_key_t key; | |
110 glyph_hash_val_t val; | |
111 struct glyph_hash_item_s* next; | |
112 } glyph_hash_item_t; | |
113 | |
114 typedef glyph_hash_item_t* glyph_hash_item_p; | |
115 | |
116 static glyph_hash_item_p* glyph_hash_root; | |
117 static int glyph_hash_size; | |
118 | |
119 static int glyph_compare(glyph_hash_key_t* a, glyph_hash_key_t* b) { | |
120 if (memcmp(a, b, sizeof(glyph_hash_key_t)) == 0) | |
121 return 1; | |
122 else | |
123 return 0; | |
124 } | |
125 | |
126 static unsigned glyph_hash(glyph_hash_key_t* key) { | |
127 unsigned val = 0; | |
128 unsigned i; | |
129 for (i = 0; i < sizeof(key->face); ++i) | |
130 val += *(unsigned char *)(&(key->face) + i); | |
131 val <<= 21; | |
132 | |
133 if (key->bitmap) val &= 0x80000000; | |
19848 | 134 if (key->be) val &= 0x40000000; |
18937 | 135 val += key->index; |
136 val += key->size << 8; | |
137 val += key->outline << 3; | |
138 val += key->advance.x << 10; | |
139 val += key->advance.y << 16; | |
140 val += key->bold << 1; | |
141 val += key->italic << 20; | |
142 return val; | |
143 } | |
144 | |
145 /** | |
146 * \brief Add a glyph to glyph cache. | |
147 * \param key hash key | |
148 * \param val hash val: 2 bitmap glyphs + some additional info | |
149 */ | |
150 void cache_add_glyph(glyph_hash_key_t* key, glyph_hash_val_t* val) | |
151 { | |
152 unsigned hash = glyph_hash(key); | |
153 glyph_hash_item_t** next = glyph_hash_root + (hash % GLYPH_HASH_SIZE); | |
154 while (*next) { | |
155 if (glyph_compare(key, &((*next)->key))) | |
156 return; | |
157 next = &((*next)->next); | |
158 assert(next); | |
159 } | |
160 (*next) = malloc(sizeof(glyph_hash_item_t)); | |
161 // (*next)->desc = glyph_key_copy(key, &((*next)->key)); | |
162 memcpy(&((*next)->key), key, sizeof(glyph_hash_key_t)); | |
163 memcpy(&((*next)->val), val, sizeof(glyph_hash_val_t)); | |
164 (*next)->next = 0; | |
165 | |
166 glyph_hash_size ++; | |
167 /* if (glyph_hash_size && (glyph_hash_size % 25 == 0)) { | |
168 printf("\nGlyph cache: %d entries, %d bytes\n", glyph_hash_size, glyph_hash_size * sizeof(glyph_hash_item_t)); | |
169 } */ | |
170 } | |
171 | |
172 /** | |
173 * \brief Get a glyph from glyph cache. | |
174 * \param key hash key | |
175 * \return requested hash val or 0 if not found | |
176 */ | |
177 glyph_hash_val_t* cache_find_glyph(glyph_hash_key_t* key) | |
178 { | |
179 unsigned hash = glyph_hash(key); | |
180 glyph_hash_item_t* item = glyph_hash_root[hash % GLYPH_HASH_SIZE]; | |
181 while (item) { | |
182 if (glyph_compare(key, &(item->key))) { | |
183 return &(item->val); | |
184 } | |
185 item = item->next; | |
186 } | |
187 return 0; | |
188 } | |
189 | |
190 void ass_glyph_cache_init(void) | |
191 { | |
192 glyph_hash_root = calloc(GLYPH_HASH_SIZE, sizeof(glyph_hash_item_p)); | |
193 glyph_hash_size = 0; | |
194 } | |
195 | |
19545 | 196 void ass_glyph_cache_done(void) |
18937 | 197 { |
198 int i; | |
199 for (i = 0; i < GLYPH_HASH_SIZE; ++i) { | |
200 glyph_hash_item_t* item = glyph_hash_root[i]; | |
201 while (item) { | |
202 glyph_hash_item_t* next = item->next; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
203 if (item->val.bm) ass_free_bitmap(item->val.bm); |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
204 if (item->val.bm_o) ass_free_bitmap(item->val.bm_o); |
18937 | 205 free(item); |
206 item = next; | |
207 } | |
208 } | |
19545 | 209 free(glyph_hash_root); |
18937 | 210 glyph_hash_size = 0; |
211 } | |
212 | |
19545 | 213 void ass_glyph_cache_reset(void) |
19539 | 214 { |
19545 | 215 ass_glyph_cache_done(); |
216 ass_glyph_cache_init(); | |
19539 | 217 } |
218 |