Handle illegal \a tags like VSFilter
[libass.git] / libass / ass_font.c
blob2e8548c5b8fe704b0c6ebb098678df78e47f03eb
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
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.
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.
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.
21 #include "config.h"
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 #include FT_OUTLINE_H
31 #include "ass.h"
32 #include "ass_library.h"
33 #include "ass_font.h"
34 #include "ass_bitmap.h"
35 #include "ass_cache.h"
36 #include "ass_fontconfig.h"
37 #include "ass_utils.h"
39 /**
40 * Select Microfost Unicode CharMap, if the font has one.
41 * Otherwise, let FreeType decide.
43 static void charmap_magic(ASS_Library *library, FT_Face face)
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;
58 if (!face->charmap) {
59 if (face->num_charmaps == 0) {
60 ass_msg(library, MSGL_WARN, "Font face with no charmaps");
61 return;
63 ass_msg(library, MSGL_WARN,
64 "No charmap autodetected, trying the first one");
65 FT_Set_Charmap(face, face->charmaps[0]);
66 return;
70 static void update_transform(ASS_Font *font)
72 int i;
73 FT_Matrix m;
74 m.xx = double_to_d16(font->scale_x);
75 m.yy = double_to_d16(font->scale_y);
76 m.xy = m.yx = 0;
77 for (i = 0; i < font->n_faces; ++i)
78 FT_Set_Transform(font->faces[i], &m, &font->v);
81 /**
82 * \brief find a memory font by name
84 static int find_font(ASS_Library *library, char *name)
86 int i;
87 for (i = 0; i < library->num_fontdata; ++i)
88 if (strcasecmp(name, library->fontdata[i].name) == 0)
89 return i;
90 return -1;
93 static void face_set_size(FT_Face face, double size);
95 static void buggy_font_workaround(FT_Face face)
97 // Some fonts have zero Ascender/Descender fields in 'hhea' table.
98 // In this case, get the information from 'os2' table or, as
99 // a last resort, from face.bbox.
100 if (face->ascender + face->descender == 0 || face->height == 0) {
101 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
102 if (os2) {
103 face->ascender = os2->sTypoAscender;
104 face->descender = os2->sTypoDescender;
105 face->height = face->ascender - face->descender;
106 } else {
107 face->ascender = face->bbox.yMax;
108 face->descender = face->bbox.yMin;
109 face->height = face->ascender - face->descender;
115 * \brief Select a face with the given charcode and add it to ASS_Font
116 * \return index of the new face in font->faces, -1 if failed
118 static int add_face(void *fc_priv, ASS_Font *font, uint32_t ch)
120 char *path;
121 int index;
122 FT_Face face;
123 int error;
124 int mem_idx;
126 if (font->n_faces == ASS_FONT_MAX_FACES)
127 return -1;
129 path =
130 fontconfig_select(font->library, fc_priv, font->desc.family,
131 font->desc.treat_family_as_pattern,
132 font->desc.bold, font->desc.italic, &index, ch);
133 if (!path)
134 return -1;
136 mem_idx = find_font(font->library, path);
137 if (mem_idx >= 0) {
138 error =
139 FT_New_Memory_Face(font->ftlibrary,
140 (unsigned char *) font->library->
141 fontdata[mem_idx].data,
142 font->library->fontdata[mem_idx].size, 0,
143 &face);
144 if (error) {
145 ass_msg(font->library, MSGL_WARN,
146 "Error opening memory font: '%s'", path);
147 free(path);
148 return -1;
150 } else {
151 error = FT_New_Face(font->ftlibrary, path, index, &face);
152 if (error) {
153 ass_msg(font->library, MSGL_WARN,
154 "Error opening font: '%s', %d", path, index);
155 free(path);
156 return -1;
159 charmap_magic(font->library, face);
160 buggy_font_workaround(face);
162 font->faces[font->n_faces++] = face;
163 update_transform(font);
164 face_set_size(face, font->size);
165 free(path);
166 return font->n_faces - 1;
170 * \brief Create a new ASS_Font according to "desc" argument
172 ASS_Font *ass_font_new(void *font_cache, ASS_Library *library,
173 FT_Library ftlibrary, void *fc_priv,
174 ASS_FontDesc *desc)
176 int error;
177 ASS_Font *fontp;
178 ASS_Font font;
180 fontp = ass_font_cache_find((Hashmap *) font_cache, desc);
181 if (fontp)
182 return fontp;
184 font.library = library;
185 font.ftlibrary = ftlibrary;
186 font.n_faces = 0;
187 font.desc.family = strdup(desc->family);
188 font.desc.treat_family_as_pattern = desc->treat_family_as_pattern;
189 font.desc.bold = desc->bold;
190 font.desc.italic = desc->italic;
192 font.scale_x = font.scale_y = 1.;
193 font.v.x = font.v.y = 0;
194 font.size = 0.;
196 error = add_face(fc_priv, &font, 0);
197 if (error == -1) {
198 free(font.desc.family);
199 return 0;
200 } else
201 return ass_font_cache_add((Hashmap *) font_cache, &font);
205 * \brief Set font transformation matrix and shift vector
207 void ass_font_set_transform(ASS_Font *font, double scale_x,
208 double scale_y, FT_Vector *v)
210 font->scale_x = scale_x;
211 font->scale_y = scale_y;
212 if (v) {
213 font->v.x = v->x;
214 font->v.y = v->y;
216 update_transform(font);
219 static void face_set_size(FT_Face face, double size)
221 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1))
222 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
223 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
224 double mscale = 1.;
225 FT_Size_RequestRec rq;
226 FT_Size_Metrics *m = &face->size->metrics;
227 // VSFilter uses metrics from TrueType OS/2 table
228 // The idea was borrowed from asa (http://asa.diac24.net)
229 if (hori && os2) {
230 int hori_height = hori->Ascender - hori->Descender;
231 int os2_height = os2->usWinAscent + os2->usWinDescent;
232 if (hori_height && os2_height)
233 mscale = (double) hori_height / os2_height;
235 memset(&rq, 0, sizeof(rq));
236 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
237 rq.width = 0;
238 rq.height = double_to_d6(size * mscale);
239 rq.horiResolution = rq.vertResolution = 0;
240 FT_Request_Size(face, &rq);
241 m->ascender /= mscale;
242 m->descender /= mscale;
243 m->height /= mscale;
244 #else
245 FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0);
246 #endif
250 * \brief Set font size
252 void ass_font_set_size(ASS_Font *font, double size)
254 int i;
255 if (font->size != size) {
256 font->size = size;
257 for (i = 0; i < font->n_faces; ++i)
258 face_set_size(font->faces[i], size);
263 * \brief Get maximal font ascender and descender.
264 * \param ch character code
265 * The values are extracted from the font face that provides glyphs for the given character
267 void ass_font_get_asc_desc(ASS_Font *font, uint32_t ch, int *asc,
268 int *desc)
270 int i;
271 for (i = 0; i < font->n_faces; ++i) {
272 FT_Face face = font->faces[i];
273 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
274 if (FT_Get_Char_Index(face, ch)) {
275 int y_scale = face->size->metrics.y_scale;
276 if (os2) {
277 *asc = FT_MulFix(os2->usWinAscent, y_scale);
278 *desc = FT_MulFix(os2->usWinDescent, y_scale);
279 } else {
280 *asc = FT_MulFix(face->ascender, y_scale);
281 *desc = FT_MulFix(-face->descender, y_scale);
283 return;
287 *asc = *desc = 0;
291 * Strike a glyph with a horizontal line; it's possible to underline it
292 * and/or strike through it. For the line's position and size, truetype
293 * tables are consulted. Obviously this relies on the data in the tables
294 * being accurate.
297 static int ass_strike_outline_glyph(FT_Face face, ASS_Font *font,
298 FT_Glyph glyph, int under, int through)
300 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
301 TT_Postscript *ps = FT_Get_Sfnt_Table(face, ft_sfnt_post);
302 FT_Outline *ol = &((FT_OutlineGlyph) glyph)->outline;
303 int bear, advance, y_scale, i, dir;
305 if (!under && !through)
306 return 0;
308 // Grow outline
309 i = (under ? 4 : 0) + (through ? 4 : 0);
310 ol->points = realloc(ol->points, sizeof(FT_Vector) *
311 (ol->n_points + i));
312 ol->tags = realloc(ol->tags, ol->n_points + i);
313 i = !!under + !!through;
314 ol->contours = realloc(ol->contours, sizeof(short) *
315 (ol->n_contours + i));
317 // If the bearing is negative, the glyph starts left of the current
318 // pen position
319 bear = FFMIN(face->glyph->metrics.horiBearingX, 0);
320 // We're adding half a pixel to avoid small gaps
321 advance = d16_to_d6(glyph->advance.x) + 32;
322 y_scale = face->size->metrics.y_scale;
324 // Reverse drawing direction for non-truetype fonts
325 dir = FT_Outline_Get_Orientation(ol);
327 // Add points to the outline
328 if (under && ps) {
329 int pos, size;
330 pos = FT_MulFix(ps->underlinePosition, y_scale * font->scale_y);
331 size = FT_MulFix(ps->underlineThickness,
332 y_scale * font->scale_y / 2);
334 if (pos > 0 || size <= 0)
335 return 1;
337 FT_Vector points[4] = {
338 {.x = bear, .y = pos + size},
339 {.x = advance, .y = pos + size},
340 {.x = advance, .y = pos - size},
341 {.x = bear, .y = pos - size},
344 if (dir == FT_ORIENTATION_TRUETYPE) {
345 for (i = 0; i < 4; i++) {
346 ol->points[ol->n_points] = points[i];
347 ol->tags[ol->n_points++] = 1;
349 } else {
350 for (i = 3; i >= 0; i--) {
351 ol->points[ol->n_points] = points[i];
352 ol->tags[ol->n_points++] = 1;
356 ol->contours[ol->n_contours++] = ol->n_points - 1;
359 if (through && os2) {
360 int pos, size;
361 pos = FT_MulFix(os2->yStrikeoutPosition, y_scale * font->scale_y);
362 size = FT_MulFix(os2->yStrikeoutSize, y_scale * font->scale_y / 2);
364 if (pos < 0 || size <= 0)
365 return 1;
367 FT_Vector points[4] = {
368 {.x = bear, .y = pos + size},
369 {.x = advance, .y = pos + size},
370 {.x = advance, .y = pos - size},
371 {.x = bear, .y = pos - size},
374 if (dir == FT_ORIENTATION_TRUETYPE) {
375 for (i = 0; i < 4; i++) {
376 ol->points[ol->n_points] = points[i];
377 ol->tags[ol->n_points++] = 1;
379 } else {
380 for (i = 3; i >= 0; i--) {
381 ol->points[ol->n_points] = points[i];
382 ol->tags[ol->n_points++] = 1;
386 ol->contours[ol->n_contours++] = ol->n_points - 1;
389 return 0;
393 * Slightly embold a glyph without touching its metrics
395 static void ass_glyph_embolden(FT_GlyphSlot slot)
397 int str;
399 if (slot->format != FT_GLYPH_FORMAT_OUTLINE)
400 return;
402 str = FT_MulFix(slot->face->units_per_EM,
403 slot->face->size->metrics.y_scale) / 64;
405 FT_Outline_Embolden(&slot->outline, str);
409 * \brief Get a glyph
410 * \param ch character code
412 FT_Glyph ass_font_get_glyph(void *fontconfig_priv, ASS_Font *font,
413 uint32_t ch, ASS_Hinting hinting, int deco)
415 int error;
416 int index = 0;
417 int i;
418 FT_Glyph glyph;
419 FT_Face face = 0;
420 int flags = 0;
422 if (ch < 0x20)
423 return 0;
424 if (font->n_faces == 0)
425 return 0;
427 for (i = 0; i < font->n_faces; ++i) {
428 face = font->faces[i];
429 index = FT_Get_Char_Index(face, ch);
430 if (index)
431 break;
434 #ifdef CONFIG_FONTCONFIG
435 if (index == 0) {
436 int face_idx;
437 ass_msg(font->library, MSGL_INFO,
438 "Glyph 0x%X not found, selecting one more "
439 "font for (%s, %d, %d)", ch, font->desc.family,
440 font->desc.bold, font->desc.italic);
441 face_idx = add_face(fontconfig_priv, font, ch);
442 if (face_idx >= 0) {
443 face = font->faces[face_idx];
444 index = FT_Get_Char_Index(face, ch);
445 if (index == 0) {
446 ass_msg(font->library, MSGL_ERR,
447 "Glyph 0x%X not found in font for (%s, %d, %d)",
448 ch, font->desc.family, font->desc.bold,
449 font->desc.italic);
453 #endif
455 switch (hinting) {
456 case ASS_HINTING_NONE:
457 flags = FT_LOAD_NO_HINTING;
458 break;
459 case ASS_HINTING_LIGHT:
460 flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT;
461 break;
462 case ASS_HINTING_NORMAL:
463 flags = FT_LOAD_FORCE_AUTOHINT;
464 break;
465 case ASS_HINTING_NATIVE:
466 flags = 0;
467 break;
470 error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
471 if (error) {
472 ass_msg(font->library, MSGL_WARN, "Error loading glyph, index %d",
473 index);
474 return 0;
476 #if (FREETYPE_MAJOR > 2) || \
477 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
478 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
479 // FreeType >= 2.1.10 required
480 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
481 (font->desc.italic > 55)) {
482 FT_GlyphSlot_Oblique(face->glyph);
485 if (!(face->style_flags & FT_STYLE_FLAG_BOLD) &&
486 (font->desc.bold > 80)) {
487 ass_glyph_embolden(face->glyph);
489 #endif
490 error = FT_Get_Glyph(face->glyph, &glyph);
491 if (error) {
492 ass_msg(font->library, MSGL_WARN, "Error loading glyph, index %d",
493 index);
494 return 0;
497 ass_strike_outline_glyph(face, font, glyph, deco & DECO_UNDERLINE,
498 deco & DECO_STRIKETHROUGH);
500 return glyph;
504 * \brief Get kerning for the pair of glyphs.
506 FT_Vector ass_font_get_kerning(ASS_Font *font, uint32_t c1, uint32_t c2)
508 FT_Vector v = { 0, 0 };
509 int i;
511 for (i = 0; i < font->n_faces; ++i) {
512 FT_Face face = font->faces[i];
513 int i1 = FT_Get_Char_Index(face, c1);
514 int i2 = FT_Get_Char_Index(face, c2);
515 if (i1 && i2) {
516 if (FT_HAS_KERNING(face))
517 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
518 return v;
520 if (i1 || i2) // these glyphs are from different font faces, no kerning information
521 return v;
523 return v;
527 * \brief Deallocate ASS_Font
529 void ass_font_free(ASS_Font *font)
531 int i;
532 for (i = 0; i < font->n_faces; ++i)
533 if (font->faces[i])
534 FT_Done_Face(font->faces[i]);
535 if (font->desc.family)
536 free(font->desc.family);
537 free(font);