Mercurial > mplayer.hg
annotate libass/ass_cache.c @ 20327:5e5e6c0a98c3
Add flac twocc
author | reimar |
---|---|
date | Sat, 21 Oct 2006 15:57:08 +0000 |
parents | fa122b7c71c6 |
children | e8885ec63928 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
3 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
23 #include <ft2build.h> | |
24 #include FT_FREETYPE_H | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
25 #include FT_GLYPH_H |
18937 | 26 |
27 #include <assert.h> | |
28 | |
29 #include "mp_msg.h" | |
30 #include "ass_fontconfig.h" | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
31 #include "ass_bitmap.h" |
18937 | 32 #include "ass_cache.h" |
33 | |
34 | |
35 typedef struct face_cache_item_s { | |
36 face_desc_t desc; | |
37 char* path; | |
38 int index; | |
39 FT_Face face; | |
40 } face_cache_item_t; | |
41 | |
42 #define MAX_FACE_CACHE_SIZE 100 | |
43 | |
44 static face_cache_item_t* face_cache; | |
45 static int face_cache_size; | |
46 | |
47 extern int no_more_font_messages; | |
48 | |
49 static int font_compare(face_desc_t* a, face_desc_t* b) { | |
50 if (strcmp(a->family, b->family) != 0) | |
51 return 0; | |
52 if (a->bold != b->bold) | |
53 return 0; | |
54 if (a->italic != b->italic) | |
55 return 0; | |
56 return 1; | |
57 } | |
58 | |
59 /** | |
60 * \brief Get a face object, either from cache or created through FreeType+FontConfig. | |
61 * \param library FreeType library object | |
62 * \param fontconfig_priv fontconfig private data | |
63 * \param desc required face description | |
64 * \param face out: the face object | |
65 */ | |
66 int ass_new_face(FT_Library library, void* fontconfig_priv, face_desc_t* desc, /*out*/ FT_Face* face) | |
67 { | |
68 FT_Error error; | |
69 int i; | |
70 char* path; | |
71 int index; | |
72 face_cache_item_t* item; | |
73 | |
74 for (i=0; i<face_cache_size; ++i) | |
75 if (font_compare(desc, &(face_cache[i].desc))) { | |
76 *face = face_cache[i].face; | |
77 return 0; | |
78 } | |
79 | |
80 if (face_cache_size == MAX_FACE_CACHE_SIZE) { | |
81 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "Too many fonts\n"); | |
82 return 1; | |
83 } | |
84 | |
85 path = fontconfig_select(fontconfig_priv, desc->family, desc->bold, desc->italic, &index); | |
86 | |
87 error = FT_New_Face(library, path, index, face); | |
88 if (error) { | |
89 if (!no_more_font_messages) | |
90 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error opening font: %s, %d\n", path, index); | |
91 no_more_font_messages = 1; | |
92 return 1; | |
93 } | |
94 | |
95 item = face_cache + face_cache_size; | |
96 item->path = strdup(path); | |
97 item->index = index; | |
98 item->face = *face; | |
99 memcpy(&(item->desc), desc, sizeof(face_desc_t)); | |
100 face_cache_size++; | |
101 return 0; | |
102 } | |
103 | |
104 void ass_face_cache_init(void) | |
105 { | |
106 face_cache = calloc(MAX_FACE_CACHE_SIZE, sizeof(face_cache_item_t)); | |
107 face_cache_size = 0; | |
108 } | |
109 | |
110 void ass_face_cache_done(void) | |
111 { | |
112 int i; | |
113 for (i = 0; i < face_cache_size; ++i) { | |
114 face_cache_item_t* item = face_cache + i; | |
115 if (item->face) FT_Done_Face(item->face); | |
116 if (item->path) free(item->path); | |
117 // FIXME: free desc ? | |
118 } | |
119 free(face_cache); | |
120 face_cache_size = 0; | |
121 } | |
122 | |
123 //--------------------------------- | |
124 // glyph cache | |
125 | |
126 #define GLYPH_HASH_SIZE (0xFFFF + 13) | |
127 | |
128 typedef struct glyph_hash_item_s { | |
129 glyph_hash_key_t key; | |
130 glyph_hash_val_t val; | |
131 struct glyph_hash_item_s* next; | |
132 } glyph_hash_item_t; | |
133 | |
134 typedef glyph_hash_item_t* glyph_hash_item_p; | |
135 | |
136 static glyph_hash_item_p* glyph_hash_root; | |
137 static int glyph_hash_size; | |
138 | |
139 static int glyph_compare(glyph_hash_key_t* a, glyph_hash_key_t* b) { | |
140 if (memcmp(a, b, sizeof(glyph_hash_key_t)) == 0) | |
141 return 1; | |
142 else | |
143 return 0; | |
144 } | |
145 | |
146 static unsigned glyph_hash(glyph_hash_key_t* key) { | |
147 unsigned val = 0; | |
148 unsigned i; | |
149 for (i = 0; i < sizeof(key->face); ++i) | |
150 val += *(unsigned char *)(&(key->face) + i); | |
151 val <<= 21; | |
152 | |
153 if (key->bitmap) val &= 0x80000000; | |
19848 | 154 if (key->be) val &= 0x40000000; |
18937 | 155 val += key->index; |
156 val += key->size << 8; | |
157 val += key->outline << 3; | |
158 val += key->advance.x << 10; | |
159 val += key->advance.y << 16; | |
160 val += key->bold << 1; | |
161 val += key->italic << 20; | |
162 return val; | |
163 } | |
164 | |
165 /** | |
166 * \brief Add a glyph to glyph cache. | |
167 * \param key hash key | |
168 * \param val hash val: 2 bitmap glyphs + some additional info | |
169 */ | |
170 void cache_add_glyph(glyph_hash_key_t* key, glyph_hash_val_t* val) | |
171 { | |
172 unsigned hash = glyph_hash(key); | |
173 glyph_hash_item_t** next = glyph_hash_root + (hash % GLYPH_HASH_SIZE); | |
174 while (*next) { | |
175 if (glyph_compare(key, &((*next)->key))) | |
176 return; | |
177 next = &((*next)->next); | |
178 assert(next); | |
179 } | |
180 (*next) = malloc(sizeof(glyph_hash_item_t)); | |
181 // (*next)->desc = glyph_key_copy(key, &((*next)->key)); | |
182 memcpy(&((*next)->key), key, sizeof(glyph_hash_key_t)); | |
183 memcpy(&((*next)->val), val, sizeof(glyph_hash_val_t)); | |
184 (*next)->next = 0; | |
185 | |
186 glyph_hash_size ++; | |
187 /* if (glyph_hash_size && (glyph_hash_size % 25 == 0)) { | |
188 printf("\nGlyph cache: %d entries, %d bytes\n", glyph_hash_size, glyph_hash_size * sizeof(glyph_hash_item_t)); | |
189 } */ | |
190 } | |
191 | |
192 /** | |
193 * \brief Get a glyph from glyph cache. | |
194 * \param key hash key | |
195 * \return requested hash val or 0 if not found | |
196 */ | |
197 glyph_hash_val_t* cache_find_glyph(glyph_hash_key_t* key) | |
198 { | |
199 unsigned hash = glyph_hash(key); | |
200 glyph_hash_item_t* item = glyph_hash_root[hash % GLYPH_HASH_SIZE]; | |
201 while (item) { | |
202 if (glyph_compare(key, &(item->key))) { | |
203 return &(item->val); | |
204 } | |
205 item = item->next; | |
206 } | |
207 return 0; | |
208 } | |
209 | |
210 void ass_glyph_cache_init(void) | |
211 { | |
212 glyph_hash_root = calloc(GLYPH_HASH_SIZE, sizeof(glyph_hash_item_p)); | |
213 glyph_hash_size = 0; | |
214 } | |
215 | |
19545 | 216 void ass_glyph_cache_done(void) |
18937 | 217 { |
218 int i; | |
219 for (i = 0; i < GLYPH_HASH_SIZE; ++i) { | |
220 glyph_hash_item_t* item = glyph_hash_root[i]; | |
221 while (item) { | |
222 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
|
223 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
|
224 if (item->val.bm_o) ass_free_bitmap(item->val.bm_o); |
19965 | 225 if (item->val.bm_s) ass_free_bitmap(item->val.bm_s); |
18937 | 226 free(item); |
227 item = next; | |
228 } | |
229 } | |
19545 | 230 free(glyph_hash_root); |
18937 | 231 glyph_hash_size = 0; |
232 } | |
233 | |
19545 | 234 void ass_glyph_cache_reset(void) |
19539 | 235 { |
19545 | 236 ass_glyph_cache_done(); |
237 ass_glyph_cache_init(); | |
19539 | 238 } |
239 |