Mercurial > mplayer.hg
annotate libass/ass_cache.c @ 21442:f95a872702d5
fix two overlooked typos in previous sync cause build error
author | voroshil |
---|---|
date | Sun, 03 Dec 2006 06:38:45 +0000 |
parents | d7920b488fa2 |
children | 7af6c25a0cfc |
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 | |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20637
diff
changeset
|
29 #include "mputils.h" |
18937 | 30 #include "ass_fontconfig.h" |
21322 | 31 #include "ass_font.h" |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
32 #include "ass_bitmap.h" |
18937 | 33 #include "ass_cache.h" |
34 | |
21265 | 35 #define MAX_FONT_CACHE_SIZE 100 |
18937 | 36 |
21317 | 37 static ass_font_t** font_cache; |
21265 | 38 static int font_cache_size; |
18937 | 39 |
21265 | 40 static int font_compare(ass_font_desc_t* a, ass_font_desc_t* b) { |
18937 | 41 if (strcmp(a->family, b->family) != 0) |
42 return 0; | |
43 if (a->bold != b->bold) | |
44 return 0; | |
45 if (a->italic != b->italic) | |
46 return 0; | |
47 return 1; | |
48 } | |
49 | |
50 /** | |
21317 | 51 * \brief Get a face struct from cache. |
18937 | 52 * \param desc required face description |
21317 | 53 * \return font struct |
54 */ | |
55 ass_font_t* ass_font_cache_find(ass_font_desc_t* desc) | |
18937 | 56 { |
57 int i; | |
58 | |
21265 | 59 for (i=0; i<font_cache_size; ++i) |
21317 | 60 if (font_compare(desc, &(font_cache[i]->desc))) |
61 return font_cache[i]; | |
62 | |
63 return 0; | |
64 } | |
18937 | 65 |
21317 | 66 /** |
67 * \brief Add a face struct to cache. | |
68 * \param font font struct | |
69 */ | |
70 void ass_font_cache_add(ass_font_t* font) | |
71 { | |
21265 | 72 if (font_cache_size == MAX_FONT_CACHE_SIZE) { |
21066 | 73 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_TooManyFonts); |
21317 | 74 // FIXME: possible memory leak |
75 return; | |
18937 | 76 } |
77 | |
21317 | 78 font_cache[font_cache_size] = font; |
21277 | 79 font_cache_size++; |
18937 | 80 } |
81 | |
21265 | 82 void ass_font_cache_init(void) |
18937 | 83 { |
21317 | 84 font_cache = calloc(MAX_FONT_CACHE_SIZE, sizeof(ass_font_t*)); |
21265 | 85 font_cache_size = 0; |
18937 | 86 } |
87 | |
21265 | 88 void ass_font_cache_done(void) |
18937 | 89 { |
90 int i; | |
21265 | 91 for (i = 0; i < font_cache_size; ++i) { |
21317 | 92 ass_font_t* item = font_cache[i]; |
21277 | 93 ass_font_free(item); |
18937 | 94 } |
21265 | 95 free(font_cache); |
96 font_cache_size = 0; | |
18937 | 97 } |
98 | |
99 //--------------------------------- | |
100 // glyph cache | |
101 | |
102 #define GLYPH_HASH_SIZE (0xFFFF + 13) | |
103 | |
104 typedef struct glyph_hash_item_s { | |
105 glyph_hash_key_t key; | |
106 glyph_hash_val_t val; | |
107 struct glyph_hash_item_s* next; | |
108 } glyph_hash_item_t; | |
109 | |
110 typedef glyph_hash_item_t* glyph_hash_item_p; | |
111 | |
112 static glyph_hash_item_p* glyph_hash_root; | |
113 static int glyph_hash_size; | |
114 | |
115 static int glyph_compare(glyph_hash_key_t* a, glyph_hash_key_t* b) { | |
116 if (memcmp(a, b, sizeof(glyph_hash_key_t)) == 0) | |
117 return 1; | |
118 else | |
119 return 0; | |
120 } | |
121 | |
122 static unsigned glyph_hash(glyph_hash_key_t* key) { | |
123 unsigned val = 0; | |
124 unsigned i; | |
21348
d7920b488fa2
Use (ass_font_t, char code) instead of (FT_Face, glyph index) to identify
eugeni
parents:
21322
diff
changeset
|
125 for (i = 0; i < sizeof(key->font); ++i) |
d7920b488fa2
Use (ass_font_t, char code) instead of (FT_Face, glyph index) to identify
eugeni
parents:
21322
diff
changeset
|
126 val += *(unsigned char *)(&(key->font) + i); |
18937 | 127 val <<= 21; |
128 | |
129 if (key->bitmap) val &= 0x80000000; | |
19848 | 130 if (key->be) val &= 0x40000000; |
21348
d7920b488fa2
Use (ass_font_t, char code) instead of (FT_Face, glyph index) to identify
eugeni
parents:
21322
diff
changeset
|
131 val += key->ch; |
18937 | 132 val += key->size << 8; |
133 val += key->outline << 3; | |
134 val += key->advance.x << 10; | |
135 val += key->advance.y << 16; | |
136 val += key->bold << 1; | |
137 val += key->italic << 20; | |
138 return val; | |
139 } | |
140 | |
141 /** | |
142 * \brief Add a glyph to glyph cache. | |
143 * \param key hash key | |
144 * \param val hash val: 2 bitmap glyphs + some additional info | |
145 */ | |
146 void cache_add_glyph(glyph_hash_key_t* key, glyph_hash_val_t* val) | |
147 { | |
148 unsigned hash = glyph_hash(key); | |
149 glyph_hash_item_t** next = glyph_hash_root + (hash % GLYPH_HASH_SIZE); | |
150 while (*next) { | |
151 if (glyph_compare(key, &((*next)->key))) | |
152 return; | |
153 next = &((*next)->next); | |
154 assert(next); | |
155 } | |
156 (*next) = malloc(sizeof(glyph_hash_item_t)); | |
157 // (*next)->desc = glyph_key_copy(key, &((*next)->key)); | |
158 memcpy(&((*next)->key), key, sizeof(glyph_hash_key_t)); | |
159 memcpy(&((*next)->val), val, sizeof(glyph_hash_val_t)); | |
160 (*next)->next = 0; | |
161 | |
162 glyph_hash_size ++; | |
163 /* if (glyph_hash_size && (glyph_hash_size % 25 == 0)) { | |
164 printf("\nGlyph cache: %d entries, %d bytes\n", glyph_hash_size, glyph_hash_size * sizeof(glyph_hash_item_t)); | |
165 } */ | |
166 } | |
167 | |
168 /** | |
169 * \brief Get a glyph from glyph cache. | |
170 * \param key hash key | |
171 * \return requested hash val or 0 if not found | |
172 */ | |
173 glyph_hash_val_t* cache_find_glyph(glyph_hash_key_t* key) | |
174 { | |
175 unsigned hash = glyph_hash(key); | |
176 glyph_hash_item_t* item = glyph_hash_root[hash % GLYPH_HASH_SIZE]; | |
177 while (item) { | |
178 if (glyph_compare(key, &(item->key))) { | |
179 return &(item->val); | |
180 } | |
181 item = item->next; | |
182 } | |
183 return 0; | |
184 } | |
185 | |
186 void ass_glyph_cache_init(void) | |
187 { | |
188 glyph_hash_root = calloc(GLYPH_HASH_SIZE, sizeof(glyph_hash_item_p)); | |
189 glyph_hash_size = 0; | |
190 } | |
191 | |
19545 | 192 void ass_glyph_cache_done(void) |
18937 | 193 { |
194 int i; | |
195 for (i = 0; i < GLYPH_HASH_SIZE; ++i) { | |
196 glyph_hash_item_t* item = glyph_hash_root[i]; | |
197 while (item) { | |
198 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
|
199 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
|
200 if (item->val.bm_o) ass_free_bitmap(item->val.bm_o); |
19965 | 201 if (item->val.bm_s) ass_free_bitmap(item->val.bm_s); |
18937 | 202 free(item); |
203 item = next; | |
204 } | |
205 } | |
19545 | 206 free(glyph_hash_root); |
18937 | 207 glyph_hash_size = 0; |
208 } | |
209 | |
19545 | 210 void ass_glyph_cache_reset(void) |
19539 | 211 { |
19545 | 212 ass_glyph_cache_done(); |
213 ass_glyph_cache_init(); | |
19539 | 214 } |
215 |