Mercurial > mplayer.hg
annotate libass/ass_font.c @ 23341:74f5109611e2
missed part of gui code change while introducing -subfont option (patch by Piotr Kaczuba)
author | ben |
---|---|
date | Sun, 20 May 2007 21:07:14 +0000 |
parents | 15b2b1e8a568 |
children | 27bac14b4ce4 |
rev | line source |
---|---|
21277 | 1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
2 // vim:ts=8:sw=8:noet:ai: | |
3 /* | |
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> | |
5 | |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program; if not, write to the Free Software | |
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
23 #include <inttypes.h> | |
24 #include <ft2build.h> | |
25 #include FT_FREETYPE_H | |
26 #include FT_SYNTHESIS_H | |
27 #include FT_GLYPH_H | |
23328 | 28 #include FT_TRUETYPE_TABLES_H |
21277 | 29 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
30 #include "ass.h" |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
31 #include "ass_library.h" |
21277 | 32 #include "ass_font.h" |
21321
7b7627ff1937
Move ass_font_desc_t and ass_font_t declarations to ass_font.h.
eugeni
parents:
21320
diff
changeset
|
33 #include "ass_bitmap.h" |
7b7627ff1937
Move ass_font_desc_t and ass_font_t declarations to ass_font.h.
eugeni
parents:
21320
diff
changeset
|
34 #include "ass_cache.h" |
21277 | 35 #include "ass_fontconfig.h" |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
36 #include "ass_utils.h" |
21277 | 37 #include "mputils.h" |
38 | |
39 /** | |
40 * Select Microfost Unicode CharMap, if the font has one. | |
41 * Otherwise, let FreeType decide. | |
42 */ | |
43 static void charmap_magic(FT_Face face) | |
44 { | |
45 int i; | |
46 for (i = 0; i < face->num_charmaps; ++i) { | |
47 FT_CharMap cmap = face->charmaps[i]; | |
48 unsigned pid = cmap->platform_id; | |
49 unsigned eid = cmap->encoding_id; | |
50 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) { | |
51 FT_Set_Charmap(face, cmap); | |
22210
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
52 return; |
21277 | 53 } |
54 } | |
22210
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
55 |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
56 if (!face->charmap) { |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
57 if (face->num_charmaps == 0) { |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
58 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps); |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
59 return; |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
60 } |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
61 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected); |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
62 FT_Set_Charmap(face, face->charmaps[0]); |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
63 return; |
4a958bd08920
Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents:
21630
diff
changeset
|
64 } |
21277 | 65 } |
66 | |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
67 static void update_transform(ass_font_t* font) |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
68 { |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
69 int i; |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
70 FT_Matrix m; |
23328 | 71 m.xx = double_to_d16(font->scale_x); |
72 m.yy = double_to_d16(font->scale_y); | |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
73 m.xy = m.yx = 0; |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
74 for (i = 0; i < font->n_faces; ++i) |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
75 FT_Set_Transform(font->faces[i], &m, &font->v); |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
76 } |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
77 |
21630 | 78 /** |
79 * \brief find a memory font by name | |
80 */ | |
21460 | 81 static int find_font(ass_library_t* library, char* name) |
82 { | |
83 int i; | |
84 for (i = 0; i < library->num_fontdata; ++i) | |
85 if (strcasecmp(name, library->fontdata[i].name) == 0) | |
86 return i; | |
87 return -1; | |
88 } | |
89 | |
21630 | 90 /** |
91 * \brief Create a new ass_font_t according to "desc" argument | |
92 */ | |
21460 | 93 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc) |
21277 | 94 { |
95 char* path; | |
96 int index; | |
97 FT_Face face; | |
98 int error; | |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
99 ass_font_t* fontp; |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
100 ass_font_t font; |
21460 | 101 int mem_idx; |
21317 | 102 |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
103 fontp = ass_font_cache_find(desc); |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
104 if (fontp) |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
105 return fontp; |
21277 | 106 |
107 path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index); | |
108 | |
21460 | 109 mem_idx = find_font(library, path); |
110 if (mem_idx >= 0) { | |
21615
62989854d340
Avoid "pointer targets differ in signedness" warnings.
eugeni
parents:
21614
diff
changeset
|
111 error = FT_New_Memory_Face(ftlibrary, (unsigned char*)library->fontdata[mem_idx].data, |
21460 | 112 library->fontdata[mem_idx].size, 0, &face); |
113 if (error) { | |
114 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path); | |
115 return 0; | |
116 } | |
117 } else { | |
21620 | 118 error = FT_New_Face(ftlibrary, path, index, &face); |
119 if (error) { | |
120 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index); | |
121 return 0; | |
122 } | |
21460 | 123 } |
21277 | 124 |
125 charmap_magic(face); | |
126 | |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
127 font.ftlibrary = ftlibrary; |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
128 font.faces[0] = face; |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
129 font.n_faces = 1; |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
130 font.desc.family = strdup(desc->family); |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
131 font.desc.bold = desc->bold; |
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
132 font.desc.italic = desc->italic; |
21277 | 133 |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
134 font.scale_x = font.scale_y = 1.; |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
135 font.v.x = font.v.y = 0; |
23300 | 136 font.size = 0.; |
21277 | 137 |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
138 #ifdef HAVE_FONTCONFIG |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
139 font.charset = FcCharSetCreate(); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
140 #endif |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
141 |
23212
c932f521344d
In ass_font_new, allocate temporary ass_font_t on stack and return the pointer
eugeni
parents:
23134
diff
changeset
|
142 return ass_font_cache_add(&font); |
21277 | 143 } |
144 | |
21630 | 145 /** |
146 * \brief Set font transformation matrix and shift vector | |
147 **/ | |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
148 void ass_font_set_transform(ass_font_t* font, double scale_x, double scale_y, FT_Vector* v) |
21277 | 149 { |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
150 font->scale_x = scale_x; |
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
151 font->scale_y = scale_y; |
21616
2704273f398d
FT_Set_Transform is fast enough to be called once for each glyph.
eugeni
parents:
21615
diff
changeset
|
152 font->v.x = v->x; |
2704273f398d
FT_Set_Transform is fast enough to be called once for each glyph.
eugeni
parents:
21615
diff
changeset
|
153 font->v.y = v->y; |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
154 update_transform(font); |
21277 | 155 } |
156 | |
23328 | 157 static void face_set_size(FT_Face face, double size) |
158 { | |
23340 | 159 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1)) |
23328 | 160 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea); |
161 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2); | |
162 double mscale = 1.; | |
163 FT_Size_RequestRec rq; | |
164 FT_Size_Metrics *m = &face->size->metrics; | |
165 // VSFilter uses metrics from TrueType OS/2 table | |
166 // The idea was borrowed from asa (http://asa.diac24.net) | |
167 if (hori && os2) | |
168 mscale = ((double)(hori->Ascender - hori->Descender)) / (os2->usWinAscent + os2->usWinDescent); | |
169 memset(&rq, 0, sizeof(rq)); | |
170 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM; | |
171 rq.width = 0; | |
172 rq.height = double_to_d6(size * mscale); | |
173 rq.horiResolution = rq.vertResolution = 0; | |
174 FT_Request_Size(face, &rq); | |
175 m->ascender /= mscale; | |
176 m->descender /= mscale; | |
177 m->height /= mscale; | |
23339
95bc9f4905f8
FT_Request_Size does not exist in FreeType 2.1.*. Fallback to FT_Set_Char_Size.
eugeni
parents:
23328
diff
changeset
|
178 #else |
95bc9f4905f8
FT_Request_Size does not exist in FreeType 2.1.*. Fallback to FT_Set_Char_Size.
eugeni
parents:
23328
diff
changeset
|
179 FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0); |
95bc9f4905f8
FT_Request_Size does not exist in FreeType 2.1.*. Fallback to FT_Set_Char_Size.
eugeni
parents:
23328
diff
changeset
|
180 #endif |
23328 | 181 } |
182 | |
21630 | 183 /** |
184 * \brief Set font size | |
185 **/ | |
23300 | 186 void ass_font_set_size(ass_font_t* font, double size) |
21277 | 187 { |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
188 int i; |
21319
415e2119e91c
Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents:
21317
diff
changeset
|
189 if (font->size != size) { |
21320 | 190 font->size = size; |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
191 for (i = 0; i < font->n_faces; ++i) |
23328 | 192 face_set_size(font->faces[i], size); |
21319
415e2119e91c
Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents:
21317
diff
changeset
|
193 } |
21277 | 194 } |
195 | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
196 #ifdef HAVE_FONTCONFIG |
21630 | 197 /** |
198 * \brief Select a new FT_Face with the given character | |
199 * The new face is added to the end of font->faces. | |
200 **/ | |
21618
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
201 static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font, uint32_t ch) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
202 { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
203 char* path; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
204 int index; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
205 FT_Face face; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
206 int error; |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
207 |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
208 if (font->n_faces == ASS_FONT_MAX_FACES) |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
209 return; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
210 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
211 path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
212 font->desc.italic, &index, font->charset); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
213 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
214 error = FT_New_Face(font->ftlibrary, path, index, &face); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
215 if (error) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
216 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
217 return; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
218 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
219 charmap_magic(face); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
220 |
21618
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
221 error = FT_Get_Char_Index(face, ch); |
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
222 if (error == 0) { // the new font face is not better then the old one |
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
223 FT_Done_Face(face); |
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
224 return; |
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
225 } |
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
226 |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
227 font->faces[font->n_faces++] = face; |
23299
0ee56ec36a40
Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents:
23212
diff
changeset
|
228 update_transform(font); |
23300 | 229 FT_Set_Pixel_Sizes(face, 0, (int)font->size); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
230 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
231 #endif |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
232 |
21630 | 233 /** |
234 * \brief Get maximal font ascender and descender. | |
235 * \param ch character code | |
236 * The values are extracted from the font face that provides glyphs for the given character | |
237 **/ | |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
238 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc) |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
239 { |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
240 int i; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
241 for (i = 0; i < font->n_faces; ++i) { |
21620 | 242 FT_Face face = font->faces[i]; |
243 if (FT_Get_Char_Index(face, ch)) { | |
244 int v, v2; | |
245 v = face->size->metrics.ascender; | |
246 v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale); | |
247 *asc = (v > v2 * 0.9) ? v : v2; | |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
248 |
21620 | 249 v = - face->size->metrics.descender; |
250 v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale); | |
251 *desc = (v > v2 * 0.9) ? v : v2; | |
252 return; | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
253 } |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
254 } |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
255 |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
256 *asc = *desc = 0; |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
257 } |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
258 |
21630 | 259 /** |
260 * \brief Get a glyph | |
261 * \param ch character code | |
262 **/ | |
23134
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
263 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting) |
21277 | 264 { |
265 int error; | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
266 int index = 0; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
267 int i; |
21277 | 268 FT_Glyph glyph; |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
269 FT_Face face = 0; |
23134
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
270 int flags = 0; |
21350 | 271 |
272 if (ch < 0x20) | |
273 return 0; | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
274 if (font->n_faces == 0) |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
275 return 0; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
276 |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
277 for (i = 0; i < font->n_faces; ++i) { |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
278 face = font->faces[i]; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
279 index = FT_Get_Char_Index(face, ch); |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
280 if (index) |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
281 break; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
282 } |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
283 |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
284 #ifdef HAVE_FONTCONFIG |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
285 FcCharSetAddChar(font->charset, ch); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
286 if (index == 0) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
287 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
288 ch, font->desc.family, font->desc.bold, font->desc.italic); |
21618
8111d4625194
Avoid storing font file path and index in ass_font_t.
eugeni
parents:
21616
diff
changeset
|
289 ass_font_reselect(fontconfig_priv, font, ch); |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
290 face = font->faces[font->n_faces - 1]; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
291 index = FT_Get_Char_Index(face, ch); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
292 if (index == 0) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
293 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
294 ch, font->desc.family, font->desc.bold, font->desc.italic); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
295 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
296 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
297 #endif |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
298 |
23134
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
299 switch (hinting) { |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
300 case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break; |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
301 case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break; |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
302 case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break; |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
303 case ASS_HINTING_NATIVE: flags = 0; break; |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
304 } |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
305 |
1de2a46a0987
Add -ass-hinting option for setting font hinting method.
eugeni
parents:
22210
diff
changeset
|
306 error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags); |
21277 | 307 if (error) { |
308 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph); | |
309 return 0; | |
310 } | |
311 | |
312 #if (FREETYPE_MAJOR > 2) || \ | |
313 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \ | |
314 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10)) | |
315 // FreeType >= 2.1.10 required | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
316 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) && |
21277 | 317 (font->desc.italic > 55)) { |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
318 FT_GlyphSlot_Oblique(face->glyph); |
21277 | 319 } |
320 #endif | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
321 error = FT_Get_Glyph(face->glyph, &glyph); |
21277 | 322 if (error) { |
323 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph); | |
324 return 0; | |
325 } | |
326 | |
327 return glyph; | |
328 } | |
329 | |
21630 | 330 /** |
331 * \brief Get kerning for the pair of glyphs. | |
332 **/ | |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
333 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2) |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
334 { |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
335 FT_Vector v = {0, 0}; |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
336 int i; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
337 |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
338 for (i = 0; i < font->n_faces; ++i) { |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
339 FT_Face face = font->faces[i]; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
340 int i1 = FT_Get_Char_Index(face, c1); |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
341 int i2 = FT_Get_Char_Index(face, c2); |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
342 if (i1 && i2) { |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
343 if (FT_HAS_KERNING(face)) |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
344 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v); |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
345 return v; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
346 } |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
347 if (i1 || i2) // these glyphs are from different font faces, no kerning information |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
348 return v; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
349 } |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
350 return v; |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
351 } |
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21460
diff
changeset
|
352 |
21630 | 353 /** |
354 * \brief Deallocate ass_font_t | |
355 **/ | |
21277 | 356 void ass_font_free(ass_font_t* font) |
357 { | |
21619
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
358 int i; |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
359 for (i = 0; i < font->n_faces; ++i) |
b4b51eb2904f
Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents:
21618
diff
changeset
|
360 if (font->faces[i]) FT_Done_Face(font->faces[i]); |
21277 | 361 if (font->desc.family) free(font->desc.family); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
362 #ifdef HAVE_FONTCONFIG |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
363 if (font->charset) FcCharSetDestroy(font->charset); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21350
diff
changeset
|
364 #endif |
21317 | 365 free(font); |
21277 | 366 } |