]> granicus.if.org Git - libass/blob - libass/ass_font.c
Reindent all source code.
[libass] / libass / ass_font.c
1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
6  * libass 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  * libass 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 along
17  * with libass; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, 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 #include FT_TRUETYPE_TABLES_H
29
30 #include "ass.h"
31 #include "ass_library.h"
32 #include "ass_font.h"
33 #include "ass_bitmap.h"
34 #include "ass_cache.h"
35 #include "ass_fontconfig.h"
36 #include "ass_utils.h"
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 */ 
51             && (eid == 1 /*unicode bmp */ 
52                 || eid == 10 /*full unicode */ )) {
53             FT_Set_Charmap(face, cmap);
54             return;
55         }
56     }
57
58     if (!face->charmap) {
59         if (face->num_charmaps == 0) {
60             mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
61             return;
62         }
63         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
64         FT_Set_Charmap(face, face->charmaps[0]);
65         return;
66     }
67 }
68
69 static void update_transform(ass_font_t *font)
70 {
71     int i;
72     FT_Matrix m;
73     m.xx = double_to_d16(font->scale_x);
74     m.yy = double_to_d16(font->scale_y);
75     m.xy = m.yx = 0;
76     for (i = 0; i < font->n_faces; ++i)
77         FT_Set_Transform(font->faces[i], &m, &font->v);
78 }
79
80 /**
81  * \brief find a memory font by name
82  */
83 static int find_font(ass_library_t *library, char *name)
84 {
85     int i;
86     for (i = 0; i < library->num_fontdata; ++i)
87         if (strcasecmp(name, library->fontdata[i].name) == 0)
88             return i;
89     return -1;
90 }
91
92 static void face_set_size(FT_Face face, double size);
93
94 static void buggy_font_workaround(FT_Face face)
95 {
96     // Some fonts have zero Ascender/Descender fields in 'hhea' table.
97     // In this case, get the information from 'os2' table or, as
98     // a last resort, from face.bbox.
99     if (face->ascender + face->descender == 0 || face->height == 0) {
100         TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
101         if (os2) {
102             face->ascender = os2->sTypoAscender;
103             face->descender = os2->sTypoDescender;
104             face->height = face->ascender - face->descender;
105         } else {
106             face->ascender = face->bbox.yMax;
107             face->descender = face->bbox.yMin;
108             face->height = face->ascender - face->descender;
109         }
110     }
111 }
112
113 /**
114  * \brief Select a face with the given charcode and add it to ass_font_t
115  * \return index of the new face in font->faces, -1 if failed
116  */
117 static int add_face(void *fc_priv, ass_font_t *font, uint32_t ch)
118 {
119     char *path;
120     int index;
121     FT_Face face;
122     int error;
123     int mem_idx;
124
125     if (font->n_faces == ASS_FONT_MAX_FACES)
126         return -1;
127
128     path =
129         fontconfig_select(fc_priv, font->desc.family,
130                           font->desc.treat_family_as_pattern,
131                           font->desc.bold, font->desc.italic, &index, ch);
132     if (!path)
133         return -1;
134
135     mem_idx = find_font(font->library, path);
136     if (mem_idx >= 0) {
137         error =
138             FT_New_Memory_Face(font->ftlibrary,
139                                (unsigned char *) font->library->
140                                fontdata[mem_idx].data,
141                                font->library->fontdata[mem_idx].size, 0,
142                                &face);
143         if (error) {
144             mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont,
145                    path);
146             return -1;
147         }
148     } else {
149         error = FT_New_Face(font->ftlibrary, path, index, &face);
150         if (error) {
151             mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path,
152                    index);
153             return -1;
154         }
155     }
156     charmap_magic(face);
157     buggy_font_workaround(face);
158
159     font->faces[font->n_faces++] = face;
160     update_transform(font);
161     face_set_size(face, font->size);
162     return font->n_faces - 1;
163 }
164
165 /**
166  * \brief Create a new ass_font_t according to "desc" argument
167  */
168 ass_font_t *ass_font_new(void *font_cache, ass_library_t *library,
169                          FT_Library ftlibrary, void *fc_priv,
170                          ass_font_desc_t *desc)
171 {
172     int error;
173     ass_font_t *fontp;
174     ass_font_t font;
175
176     fontp = ass_font_cache_find((hashmap_t *) font_cache, desc);
177     if (fontp)
178         return fontp;
179
180     font.library = library;
181     font.ftlibrary = ftlibrary;
182     font.n_faces = 0;
183     font.desc.family = strdup(desc->family);
184     font.desc.treat_family_as_pattern = desc->treat_family_as_pattern;
185     font.desc.bold = desc->bold;
186     font.desc.italic = desc->italic;
187
188     font.scale_x = font.scale_y = 1.;
189     font.v.x = font.v.y = 0;
190     font.size = 0.;
191
192     error = add_face(fc_priv, &font, 0);
193     if (error == -1) {
194         free(font.desc.family);
195         return 0;
196     } else
197         return ass_font_cache_add((hashmap_t *) font_cache, &font);
198 }
199
200 /**
201  * \brief Set font transformation matrix and shift vector
202  **/
203 void ass_font_set_transform(ass_font_t *font, double scale_x,
204                             double scale_y, FT_Vector * v)
205 {
206     font->scale_x = scale_x;
207     font->scale_y = scale_y;
208     font->v.x = v->x;
209     font->v.y = v->y;
210     update_transform(font);
211 }
212
213 static void face_set_size(FT_Face face, double size)
214 {
215 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1))
216     TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
217     TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
218     double mscale = 1.;
219     FT_Size_RequestRec rq;
220     FT_Size_Metrics *m = &face->size->metrics;
221     // VSFilter uses metrics from TrueType OS/2 table
222     // The idea was borrowed from asa (http://asa.diac24.net)
223     if (hori && os2) {
224         int hori_height = hori->Ascender - hori->Descender;
225         int os2_height = os2->usWinAscent + os2->usWinDescent;
226         if (hori_height && os2_height)
227             mscale = (double) hori_height / os2_height;
228     }
229     memset(&rq, 0, sizeof(rq));
230     rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
231     rq.width = 0;
232     rq.height = double_to_d6(size * mscale);
233     rq.horiResolution = rq.vertResolution = 0;
234     FT_Request_Size(face, &rq);
235     m->ascender /= mscale;
236     m->descender /= mscale;
237     m->height /= mscale;
238 #else
239     FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0);
240 #endif
241 }
242
243 /**
244  * \brief Set font size
245  **/
246 void ass_font_set_size(ass_font_t *font, double size)
247 {
248     int i;
249     if (font->size != size) {
250         font->size = size;
251         for (i = 0; i < font->n_faces; ++i)
252             face_set_size(font->faces[i], size);
253     }
254 }
255
256 /**
257  * \brief Get maximal font ascender and descender.
258  * \param ch character code
259  * The values are extracted from the font face that provides glyphs for the given character
260  **/
261 void ass_font_get_asc_desc(ass_font_t *font, uint32_t ch, int *asc,
262                            int *desc)
263 {
264     int i;
265     for (i = 0; i < font->n_faces; ++i) {
266         FT_Face face = font->faces[i];
267         if (FT_Get_Char_Index(face, ch)) {
268             *asc = face->size->metrics.ascender;
269             *desc = -face->size->metrics.descender;
270             return;
271         }
272     }
273
274     *asc = *desc = 0;
275 }
276
277 /**
278  * \brief Get a glyph
279  * \param ch character code
280  **/
281 FT_Glyph ass_font_get_glyph(void *fontconfig_priv, ass_font_t *font,
282                             uint32_t ch, ass_hinting_t hinting)
283 {
284     int error;
285     int index = 0;
286     int i;
287     FT_Glyph glyph;
288     FT_Face face = 0;
289     int flags = 0;
290
291     if (ch < 0x20)
292         return 0;
293     if (font->n_faces == 0)
294         return 0;
295
296     for (i = 0; i < font->n_faces; ++i) {
297         face = font->faces[i];
298         index = FT_Get_Char_Index(face, ch);
299         if (index)
300             break;
301     }
302
303 #ifdef CONFIG_FONTCONFIG
304     if (index == 0) {
305         int face_idx;
306         mp_msg(MSGT_ASS, MSGL_INFO,
307                MSGTR_LIBASS_GlyphNotFoundReselectingFont, ch,
308                font->desc.family, font->desc.bold, font->desc.italic);
309         face_idx = add_face(fontconfig_priv, font, ch);
310         if (face_idx >= 0) {
311             face = font->faces[face_idx];
312             index = FT_Get_Char_Index(face, ch);
313             if (index == 0) {
314                 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
315                        ch, font->desc.family, font->desc.bold,
316                        font->desc.italic);
317             }
318         }
319     }
320 #endif
321
322     switch (hinting) {
323     case ASS_HINTING_NONE:
324         flags = FT_LOAD_NO_HINTING;
325         break;
326     case ASS_HINTING_LIGHT:
327         flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT;
328         break;
329     case ASS_HINTING_NORMAL:
330         flags = FT_LOAD_FORCE_AUTOHINT;
331         break;
332     case ASS_HINTING_NATIVE:
333         flags = 0;
334         break;
335     }
336
337     error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
338     if (error) {
339         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
340         return 0;
341     }
342 #if (FREETYPE_MAJOR > 2) || \
343     ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
344     ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
345 // FreeType >= 2.1.10 required
346     if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
347         (font->desc.italic > 55)) {
348         FT_GlyphSlot_Oblique(face->glyph);
349     }
350 #endif
351     error = FT_Get_Glyph(face->glyph, &glyph);
352     if (error) {
353         mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
354         return 0;
355     }
356
357     return glyph;
358 }
359
360 /**
361  * \brief Get kerning for the pair of glyphs.
362  **/
363 FT_Vector ass_font_get_kerning(ass_font_t *font, uint32_t c1, uint32_t c2)
364 {
365     FT_Vector v = { 0, 0 };
366     int i;
367
368     for (i = 0; i < font->n_faces; ++i) {
369         FT_Face face = font->faces[i];
370         int i1 = FT_Get_Char_Index(face, c1);
371         int i2 = FT_Get_Char_Index(face, c2);
372         if (i1 && i2) {
373             if (FT_HAS_KERNING(face))
374                 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
375             return v;
376         }
377         if (i1 || i2)           // these glyphs are from different font faces, no kerning information
378             return v;
379     }
380     return v;
381 }
382
383 /**
384  * \brief Deallocate ass_font_t
385  **/
386 void ass_font_free(ass_font_t *font)
387 {
388     int i;
389     for (i = 0; i < font->n_faces; ++i)
390         if (font->faces[i])
391             FT_Done_Face(font->faces[i]);
392     if (font->desc.family)
393         free(font->desc.family);
394     free(font);
395 }