]> granicus.if.org Git - libass/blob - libass/ass_font.c
Add -ass-hinting option for setting font hinting method.
[libass] / libass / ass_font.c
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
28
29 #include "ass.h"
30 #include "ass_library.h"
31 #include "ass_font.h"
32 #include "ass_bitmap.h"
33 #include "ass_cache.h"
34 #include "ass_fontconfig.h"
35 #include "mputils.h"
36
37 /**
38  * Select Microfost Unicode CharMap, if the font has one.
39  * Otherwise, let FreeType decide.
40  */
41 static void charmap_magic(FT_Face face)
42 {
43         int i;
44         for (i = 0; i < face->num_charmaps; ++i) {
45                 FT_CharMap cmap = face->charmaps[i];
46                 unsigned pid = cmap->platform_id;
47                 unsigned eid = cmap->encoding_id;
48                 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
49                         FT_Set_Charmap(face, cmap);
50                         return;
51                 }
52         }
53
54         if (!face->charmap) {
55                 if (face->num_charmaps == 0) {
56                         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
57                         return;
58                 }
59                 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
60                 FT_Set_Charmap(face, face->charmaps[0]);
61                 return;
62         }
63 }
64
65 /**
66  * \brief find a memory font by name
67  */
68 static int find_font(ass_library_t* library, char* name)
69 {
70         int i;
71         for (i = 0; i < library->num_fontdata; ++i)
72                 if (strcasecmp(name, library->fontdata[i].name) == 0)
73                         return i;
74         return -1;
75 }
76
77 /**
78  * \brief Create a new ass_font_t according to "desc" argument
79  */
80 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
81 {
82         char* path;
83         int index;
84         FT_Face face;
85         int error;
86         ass_font_t* font;
87         int mem_idx;
88
89         font = ass_font_cache_find(desc);
90         if (font)
91                 return font;
92         
93         path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
94         
95         mem_idx = find_font(library, path);
96         if (mem_idx >= 0) {
97                 error = FT_New_Memory_Face(ftlibrary, (unsigned char*)library->fontdata[mem_idx].data,
98                                            library->fontdata[mem_idx].size, 0, &face);
99                 if (error) {
100                         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
101                         return 0;
102                 }
103         } else {
104                 error = FT_New_Face(ftlibrary, path, index, &face);
105                 if (error) {
106                         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
107                         return 0;
108                 }
109         }
110
111         charmap_magic(face);
112         
113         font = calloc(1, sizeof(ass_font_t));
114         font->ftlibrary = ftlibrary;
115         font->faces[0] = face;
116         font->n_faces = 1;
117         font->desc.family = strdup(desc->family);
118         font->desc.bold = desc->bold;
119         font->desc.italic = desc->italic;
120
121         font->m.xx = font->m.yy = (FT_Fixed)0x10000L;
122         font->m.xy = font->m.yy = 0;
123         font->v.x = font->v.y = 0;
124         font->size = 0;
125
126 #ifdef HAVE_FONTCONFIG
127         font->charset = FcCharSetCreate();
128 #endif
129
130         ass_font_cache_add(font);
131         
132         return font;
133 }
134
135 /**
136  * \brief Set font transformation matrix and shift vector
137  **/
138 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v)
139 {
140         int i;
141         font->m.xx = m->xx;
142         font->m.xy = m->xy;
143         font->m.yx = m->yx;
144         font->m.yy = m->yy;
145         font->v.x = v->x;
146         font->v.y = v->y;
147         for (i = 0; i < font->n_faces; ++i)
148                 FT_Set_Transform(font->faces[i], &font->m, &font->v);
149 }
150
151 /**
152  * \brief Set font size
153  **/
154 void ass_font_set_size(ass_font_t* font, int size)
155 {
156         int i;
157         if (font->size != size) {
158                 font->size = size;
159                 for (i = 0; i < font->n_faces; ++i)
160                         FT_Set_Pixel_Sizes(font->faces[i], 0, size);
161         }
162 }
163
164 #ifdef HAVE_FONTCONFIG
165 /**
166  * \brief Select a new FT_Face with the given character
167  * The new face is added to the end of font->faces.
168  **/
169 static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
170 {
171         char* path;
172         int index;
173         FT_Face face;
174         int error;
175
176         if (font->n_faces == ASS_FONT_MAX_FACES)
177                 return;
178         
179         path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
180                                               font->desc.italic, &index, font->charset);
181
182         error = FT_New_Face(font->ftlibrary, path, index, &face);
183         if (error) {
184                 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
185                 return;
186         }
187         charmap_magic(face);
188
189         error = FT_Get_Char_Index(face, ch);
190         if (error == 0) { // the new font face is not better then the old one
191                 FT_Done_Face(face);
192                 return;
193         }
194
195         font->faces[font->n_faces++] = face;
196         
197         FT_Set_Transform(face, &font->m, &font->v);
198         FT_Set_Pixel_Sizes(face, 0, font->size);
199 }
200 #endif
201
202 /**
203  * \brief Get maximal font ascender and descender.
204  * \param ch character code
205  * The values are extracted from the font face that provides glyphs for the given character
206  **/
207 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
208 {
209         int i;
210         for (i = 0; i < font->n_faces; ++i) {
211                 FT_Face face = font->faces[i];
212                 if (FT_Get_Char_Index(face, ch)) {
213                         int v, v2;
214                         v = face->size->metrics.ascender;
215                         v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
216                         *asc = (v > v2 * 0.9) ? v : v2;
217                                 
218                         v = - face->size->metrics.descender;
219                         v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
220                         *desc = (v > v2 * 0.9) ? v : v2;
221                         return;
222                 }
223         }
224         
225         *asc = *desc = 0;
226 }
227
228 /**
229  * \brief Get a glyph
230  * \param ch character code
231  **/
232 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting)
233 {
234         int error;
235         int index = 0;
236         int i;
237         FT_Glyph glyph;
238         FT_Face face = 0;
239         int flags = 0;
240
241         if (ch < 0x20)
242                 return 0;
243         if (font->n_faces == 0)
244                 return 0;
245
246         for (i = 0; i < font->n_faces; ++i) {
247                 face = font->faces[i];
248                 index = FT_Get_Char_Index(face, ch);
249                 if (index)
250                         break;
251         }
252
253 #ifdef HAVE_FONTCONFIG
254         FcCharSetAddChar(font->charset, ch);
255         if (index == 0) {
256                 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
257                        ch, font->desc.family, font->desc.bold, font->desc.italic);
258                 ass_font_reselect(fontconfig_priv, font, ch);
259                 face = font->faces[font->n_faces - 1];
260                 index = FT_Get_Char_Index(face, ch);
261                 if (index == 0) {
262                         mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
263                                ch, font->desc.family, font->desc.bold, font->desc.italic);
264                 }
265         }
266 #endif
267
268         switch (hinting) {
269         case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break;
270         case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break;
271         case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break;
272         case ASS_HINTING_NATIVE: flags = 0; break;
273         }
274         
275         error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
276         if (error) {
277                 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
278                 return 0;
279         }
280         
281 #if (FREETYPE_MAJOR > 2) || \
282     ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
283     ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
284 // FreeType >= 2.1.10 required
285         if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) && 
286                         (font->desc.italic > 55)) {
287                 FT_GlyphSlot_Oblique(face->glyph);
288         }
289 #endif
290         error = FT_Get_Glyph(face->glyph, &glyph);
291         if (error) {
292                 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
293                 return 0;
294         }
295         
296         return glyph;
297 }
298
299 /**
300  * \brief Get kerning for the pair of glyphs.
301  **/
302 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
303 {
304         FT_Vector v = {0, 0};
305         int i;
306
307         for (i = 0; i < font->n_faces; ++i) {
308                 FT_Face face = font->faces[i];
309                 int i1 = FT_Get_Char_Index(face, c1);
310                 int i2 = FT_Get_Char_Index(face, c2);
311                 if (i1 && i2) {
312                         if (FT_HAS_KERNING(face))
313                                 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
314                         return v;
315                 }
316                 if (i1 || i2) // these glyphs are from different font faces, no kerning information
317                         return v;
318         }
319         return v;
320 }
321
322 /**
323  * \brief Deallocate ass_font_t
324  **/
325 void ass_font_free(ass_font_t* font)
326 {
327         int i;
328         for (i = 0; i < font->n_faces; ++i)
329                 if (font->faces[i]) FT_Done_Face(font->faces[i]);
330         if (font->desc.family) free(font->desc.family);
331 #ifdef HAVE_FONTCONFIG
332         if (font->charset) FcCharSetDestroy(font->charset);
333 #endif
334         free(font);
335 }