annotate libass/ass_cache.c @ 21001:0a9d4999368b

group together "quant_type" and quant_*_matrix since they are related
author gpoirier
date Sat, 18 Nov 2006 12:35:29 +0000
parents 092cb80a8ba4
children d138463e820b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
22
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include <ft2build.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
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
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
28
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
29 #include "mp_msg.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
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
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
32 #include "ass_cache.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
33
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
34
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
35 typedef struct face_cache_item_s {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36 face_desc_t desc;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
37 char* path;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38 int index;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 FT_Face face;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
40 } face_cache_item_t;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
42 #define MAX_FACE_CACHE_SIZE 100
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
44 static face_cache_item_t* face_cache;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 static int face_cache_size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
46
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
47 extern int no_more_font_messages;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
48
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
49 static int font_compare(face_desc_t* a, face_desc_t* b) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
50 if (strcmp(a->family, b->family) != 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
51 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
52 if (a->bold != b->bold)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
53 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
54 if (a->italic != b->italic)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
55 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
56 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
57 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
58
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
59 /**
20637
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
60 * Select Microfost Unicode CharMap, if the font has one.
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
61 * Otherwise, let FreeType decide.
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
62 */
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
63 static void charmap_magic(FT_Face face)
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
64 {
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
65 int i;
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
66 for (i = 0; i < face->num_charmaps; ++i) {
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
67 FT_CharMap cmap = face->charmaps[i];
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
68 unsigned pid = cmap->platform_id;
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
69 unsigned eid = cmap->encoding_id;
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
70 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
71 FT_Set_Charmap(face, cmap);
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
72 break;
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
73 }
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
74 }
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
75 }
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
76
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
77 /**
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
78 * \brief Get a face object, either from cache or created through FreeType+FontConfig.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
79 * \param library FreeType library object
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
80 * \param fontconfig_priv fontconfig private data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
81 * \param desc required face description
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
82 * \param face out: the face object
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
83 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
84 int ass_new_face(FT_Library library, void* fontconfig_priv, face_desc_t* desc, /*out*/ FT_Face* face)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
85 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86 FT_Error error;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
88 char* path;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
89 int index;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90 face_cache_item_t* item;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
91
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
92 for (i=0; i<face_cache_size; ++i)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
93 if (font_compare(desc, &(face_cache[i].desc))) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
94 *face = face_cache[i].face;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
95 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
96 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
97
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
98 if (face_cache_size == MAX_FACE_CACHE_SIZE) {
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20008
diff changeset
99 mp_msg(MSGT_ASS, MSGL_FATAL, "Too many fonts\n");
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
100 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
101 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
102
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
103 path = fontconfig_select(fontconfig_priv, desc->family, desc->bold, desc->italic, &index);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
104
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
105 error = FT_New_Face(library, path, index, face);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
106 if (error) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
107 if (!no_more_font_messages)
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20008
diff changeset
108 mp_msg(MSGT_ASS, MSGL_WARN, "Error opening font: %s, %d\n", path, index);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
109 no_more_font_messages = 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
110 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
111 }
20637
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
112
092cb80a8ba4 Prefer microsoft-specific charmaps to all other.
eugeni
parents: 20629
diff changeset
113 charmap_magic(*face);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
114
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
115 item = face_cache + face_cache_size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
116 item->path = strdup(path);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
117 item->index = index;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
118 item->face = *face;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
119 memcpy(&(item->desc), desc, sizeof(face_desc_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
120 face_cache_size++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
121 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
122 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
123
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
124 void ass_face_cache_init(void)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
125 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
126 face_cache = calloc(MAX_FACE_CACHE_SIZE, sizeof(face_cache_item_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
127 face_cache_size = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
128 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
129
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
130 void ass_face_cache_done(void)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
131 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
132 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
133 for (i = 0; i < face_cache_size; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
134 face_cache_item_t* item = face_cache + i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
135 if (item->face) FT_Done_Face(item->face);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
136 if (item->path) free(item->path);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
137 // FIXME: free desc ?
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
138 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
139 free(face_cache);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
140 face_cache_size = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
141 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
142
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
143 //---------------------------------
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
144 // glyph cache
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
145
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
146 #define GLYPH_HASH_SIZE (0xFFFF + 13)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
147
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
148 typedef struct glyph_hash_item_s {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
149 glyph_hash_key_t key;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
150 glyph_hash_val_t val;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
151 struct glyph_hash_item_s* next;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
152 } glyph_hash_item_t;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
153
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
154 typedef glyph_hash_item_t* glyph_hash_item_p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
155
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
156 static glyph_hash_item_p* glyph_hash_root;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
157 static int glyph_hash_size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
158
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
159 static int glyph_compare(glyph_hash_key_t* a, glyph_hash_key_t* b) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
160 if (memcmp(a, b, sizeof(glyph_hash_key_t)) == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
161 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
162 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
163 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
164 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
165
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
166 static unsigned glyph_hash(glyph_hash_key_t* key) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
167 unsigned val = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
168 unsigned i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
169 for (i = 0; i < sizeof(key->face); ++i)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
170 val += *(unsigned char *)(&(key->face) + i);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
171 val <<= 21;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
172
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
173 if (key->bitmap) val &= 0x80000000;
19848
07be98a5dd5f Add \be (blur edges) support to libass.
eugeni
parents: 19846
diff changeset
174 if (key->be) val &= 0x40000000;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
175 val += key->index;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
176 val += key->size << 8;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
177 val += key->outline << 3;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
178 val += key->advance.x << 10;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
179 val += key->advance.y << 16;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
180 val += key->bold << 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
181 val += key->italic << 20;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
182 return val;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
183 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
184
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
185 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
186 * \brief Add a glyph to glyph cache.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
187 * \param key hash key
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
188 * \param val hash val: 2 bitmap glyphs + some additional info
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
189 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
190 void cache_add_glyph(glyph_hash_key_t* key, glyph_hash_val_t* val)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
191 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
192 unsigned hash = glyph_hash(key);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
193 glyph_hash_item_t** next = glyph_hash_root + (hash % GLYPH_HASH_SIZE);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
194 while (*next) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
195 if (glyph_compare(key, &((*next)->key)))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
196 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
197 next = &((*next)->next);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
198 assert(next);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
199 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
200 (*next) = malloc(sizeof(glyph_hash_item_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
201 // (*next)->desc = glyph_key_copy(key, &((*next)->key));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
202 memcpy(&((*next)->key), key, sizeof(glyph_hash_key_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
203 memcpy(&((*next)->val), val, sizeof(glyph_hash_val_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
204 (*next)->next = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
205
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
206 glyph_hash_size ++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
207 /* if (glyph_hash_size && (glyph_hash_size % 25 == 0)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
208 printf("\nGlyph cache: %d entries, %d bytes\n", glyph_hash_size, glyph_hash_size * sizeof(glyph_hash_item_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
209 } */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
210 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
211
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
212 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
213 * \brief Get a glyph from glyph cache.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
214 * \param key hash key
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
215 * \return requested hash val or 0 if not found
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
216 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
217 glyph_hash_val_t* cache_find_glyph(glyph_hash_key_t* key)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
218 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
219 unsigned hash = glyph_hash(key);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
220 glyph_hash_item_t* item = glyph_hash_root[hash % GLYPH_HASH_SIZE];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
221 while (item) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
222 if (glyph_compare(key, &(item->key))) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
223 return &(item->val);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
224 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
225 item = item->next;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
226 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
227 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
228 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
229
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
230 void ass_glyph_cache_init(void)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
231 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
232 glyph_hash_root = calloc(GLYPH_HASH_SIZE, sizeof(glyph_hash_item_p));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
233 glyph_hash_size = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
234 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
235
19545
8f46b547db39 Simplify ass_glyph_cache_reset().
eugeni
parents: 19539
diff changeset
236 void ass_glyph_cache_done(void)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
237 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
238 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
239 for (i = 0; i < GLYPH_HASH_SIZE; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
240 glyph_hash_item_t* item = glyph_hash_root[i];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
241 while (item) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
242 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
243 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
244 if (item->val.bm_o) ass_free_bitmap(item->val.bm_o);
19965
70352570e9ae Shadow support in libass.
eugeni
parents: 19848
diff changeset
245 if (item->val.bm_s) ass_free_bitmap(item->val.bm_s);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
246 free(item);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
247 item = next;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
248 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
249 }
19545
8f46b547db39 Simplify ass_glyph_cache_reset().
eugeni
parents: 19539
diff changeset
250 free(glyph_hash_root);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
251 glyph_hash_size = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
252 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
253
19545
8f46b547db39 Simplify ass_glyph_cache_reset().
eugeni
parents: 19539
diff changeset
254 void ass_glyph_cache_reset(void)
19539
64009ae411fb Reset glyph cache on reconfigure.
eugeni
parents: 18937
diff changeset
255 {
19545
8f46b547db39 Simplify ass_glyph_cache_reset().
eugeni
parents: 19539
diff changeset
256 ass_glyph_cache_done();
8f46b547db39 Simplify ass_glyph_cache_reset().
eugeni
parents: 19539
diff changeset
257 ass_glyph_cache_init();
19539
64009ae411fb Reset glyph cache on reconfigure.
eugeni
parents: 18937
diff changeset
258 }
64009ae411fb Reset glyph cache on reconfigure.
eugeni
parents: 18937
diff changeset
259