Replace libavutil internal header #includes with MPlayer copies
[mplayer.git] / libass / ass_font.c
blob73ee1c4a5bb38d240f632a4bdf828372b6e862ae
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>
6 * This file is part of libass.
8 * libass is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * libass is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with libass; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <inttypes.h>
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28 #include FT_SYNTHESIS_H
29 #include FT_GLYPH_H
30 #include FT_TRUETYPE_TABLES_H
32 #include "ass.h"
33 #include "ass_library.h"
34 #include "ass_font.h"
35 #include "ass_bitmap.h"
36 #include "ass_cache.h"
37 #include "ass_fontconfig.h"
38 #include "ass_utils.h"
39 #include "mputils.h"
41 /**
42 * Select Microfost Unicode CharMap, if the font has one.
43 * Otherwise, let FreeType decide.
45 static void charmap_magic(FT_Face face)
47 int i;
48 for (i = 0; i < face->num_charmaps; ++i) {
49 FT_CharMap cmap = face->charmaps[i];
50 unsigned pid = cmap->platform_id;
51 unsigned eid = cmap->encoding_id;
52 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
53 FT_Set_Charmap(face, cmap);
54 return;
58 if (!face->charmap) {
59 if (face->num_charmaps == 0) {
60 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
61 return;
63 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
64 FT_Set_Charmap(face, face->charmaps[0]);
65 return;
69 static void update_transform(ass_font_t* font)
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);
80 /**
81 * \brief find a memory font by name
83 static int find_font(ass_library_t* library, char* name)
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;
92 static void face_set_size(FT_Face face, double size);
94 static void buggy_font_workaround(FT_Face face)
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;
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
117 static int add_face(void* fc_priv, ass_font_t* font, uint32_t ch)
119 char* path;
120 int index;
121 FT_Face face;
122 int error;
123 int mem_idx;
125 if (font->n_faces == ASS_FONT_MAX_FACES)
126 return -1;
128 path = fontconfig_select(fc_priv, font->desc.family, font->desc.treat_family_as_pattern, font->desc.bold,
129 font->desc.italic, &index, ch);
130 if (!path)
131 return -1;
133 mem_idx = find_font(font->library, path);
134 if (mem_idx >= 0) {
135 error = FT_New_Memory_Face(font->ftlibrary, (unsigned char*)font->library->fontdata[mem_idx].data,
136 font->library->fontdata[mem_idx].size, 0, &face);
137 if (error) {
138 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
139 return -1;
141 } else {
142 error = FT_New_Face(font->ftlibrary, path, index, &face);
143 if (error) {
144 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
145 return -1;
148 charmap_magic(face);
149 buggy_font_workaround(face);
151 font->faces[font->n_faces++] = face;
152 update_transform(font);
153 face_set_size(face, font->size);
154 return font->n_faces - 1;
158 * \brief Create a new ass_font_t according to "desc" argument
160 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
162 int error;
163 ass_font_t* fontp;
164 ass_font_t font;
166 fontp = ass_font_cache_find(desc);
167 if (fontp)
168 return fontp;
170 font.library = library;
171 font.ftlibrary = ftlibrary;
172 font.n_faces = 0;
173 font.desc.family = strdup(desc->family);
174 font.desc.treat_family_as_pattern = desc->treat_family_as_pattern;
175 font.desc.bold = desc->bold;
176 font.desc.italic = desc->italic;
178 font.scale_x = font.scale_y = 1.;
179 font.v.x = font.v.y = 0;
180 font.size = 0.;
182 error = add_face(fc_priv, &font, 0);
183 if (error == -1) {
184 free(font.desc.family);
185 return 0;
186 } else
187 return ass_font_cache_add(&font);
191 * \brief Set font transformation matrix and shift vector
193 void ass_font_set_transform(ass_font_t* font, double scale_x, double scale_y, FT_Vector* v)
195 font->scale_x = scale_x;
196 font->scale_y = scale_y;
197 font->v.x = v->x;
198 font->v.y = v->y;
199 update_transform(font);
202 static void face_set_size(FT_Face face, double size)
204 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1))
205 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
206 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
207 double mscale = 1.;
208 FT_Size_RequestRec rq;
209 FT_Size_Metrics *m = &face->size->metrics;
210 // VSFilter uses metrics from TrueType OS/2 table
211 // The idea was borrowed from asa (http://asa.diac24.net)
212 if (hori && os2) {
213 int hori_height = hori->Ascender - hori->Descender;
214 int os2_height = os2->usWinAscent + os2->usWinDescent;
215 if (hori_height && os2_height)
216 mscale = (double)hori_height / os2_height;
218 memset(&rq, 0, sizeof(rq));
219 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
220 rq.width = 0;
221 rq.height = double_to_d6(size * mscale);
222 rq.horiResolution = rq.vertResolution = 0;
223 FT_Request_Size(face, &rq);
224 m->ascender /= mscale;
225 m->descender /= mscale;
226 m->height /= mscale;
227 #else
228 FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0);
229 #endif
233 * \brief Set font size
235 void ass_font_set_size(ass_font_t* font, double size)
237 int i;
238 if (font->size != size) {
239 font->size = size;
240 for (i = 0; i < font->n_faces; ++i)
241 face_set_size(font->faces[i], size);
246 * \brief Get maximal font ascender and descender.
247 * \param ch character code
248 * The values are extracted from the font face that provides glyphs for the given character
250 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
252 int i;
253 for (i = 0; i < font->n_faces; ++i) {
254 FT_Face face = font->faces[i];
255 if (FT_Get_Char_Index(face, ch)) {
256 *asc = face->size->metrics.ascender;
257 *desc = - face->size->metrics.descender;
258 return;
262 *asc = *desc = 0;
266 * \brief Get a glyph
267 * \param ch character code
269 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting)
271 int error;
272 int index = 0;
273 int i;
274 FT_Glyph glyph;
275 FT_Face face = 0;
276 int flags = 0;
278 if (ch < 0x20)
279 return 0;
280 if (font->n_faces == 0)
281 return 0;
283 for (i = 0; i < font->n_faces; ++i) {
284 face = font->faces[i];
285 index = FT_Get_Char_Index(face, ch);
286 if (index)
287 break;
290 #ifdef CONFIG_FONTCONFIG
291 if (index == 0) {
292 int face_idx;
293 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
294 ch, font->desc.family, font->desc.bold, font->desc.italic);
295 face_idx = add_face(fontconfig_priv, font, ch);
296 if (face_idx >= 0) {
297 face = font->faces[face_idx];
298 index = FT_Get_Char_Index(face, ch);
299 if (index == 0) {
300 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
301 ch, font->desc.family, font->desc.bold, font->desc.italic);
305 #endif
307 switch (hinting) {
308 case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break;
309 case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break;
310 case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break;
311 case ASS_HINTING_NATIVE: flags = 0; break;
314 error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
315 if (error) {
316 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
317 return 0;
320 #if (FREETYPE_MAJOR > 2) || \
321 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
322 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
323 // FreeType >= 2.1.10 required
324 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
325 (font->desc.italic > 55)) {
326 FT_GlyphSlot_Oblique(face->glyph);
328 #endif
329 error = FT_Get_Glyph(face->glyph, &glyph);
330 if (error) {
331 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
332 return 0;
335 return glyph;
339 * \brief Get kerning for the pair of glyphs.
341 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
343 FT_Vector v = {0, 0};
344 int i;
346 for (i = 0; i < font->n_faces; ++i) {
347 FT_Face face = font->faces[i];
348 int i1 = FT_Get_Char_Index(face, c1);
349 int i2 = FT_Get_Char_Index(face, c2);
350 if (i1 && i2) {
351 if (FT_HAS_KERNING(face))
352 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
353 return v;
355 if (i1 || i2) // these glyphs are from different font faces, no kerning information
356 return v;
358 return v;
362 * \brief Deallocate ass_font_t
364 void ass_font_free(ass_font_t* font)
366 int i;
367 for (i = 0; i < font->n_faces; ++i)
368 if (font->faces[i]) FT_Done_Face(font->faces[i]);
369 if (font->desc.family) free(font->desc.family);
370 free(font);