consistency cosmetics
[mplayer/greg.git] / libass / ass_font.c
blob37bec8b50e2e91a68ded98f6d0ae057aface4529
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 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.
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.
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
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
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"
39 /**
40 * Select Microfost Unicode CharMap, if the font has one.
41 * Otherwise, let FreeType decide.
43 static void charmap_magic(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*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
51 FT_Set_Charmap(face, cmap);
52 return;
56 if (!face->charmap) {
57 if (face->num_charmaps == 0) {
58 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
59 return;
61 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
62 FT_Set_Charmap(face, face->charmaps[0]);
63 return;
67 static void update_transform(ass_font_t* font)
69 int i;
70 FT_Matrix m;
71 m.xx = double_to_d16(font->scale_x);
72 m.yy = double_to_d16(font->scale_y);
73 m.xy = m.yx = 0;
74 for (i = 0; i < font->n_faces; ++i)
75 FT_Set_Transform(font->faces[i], &m, &font->v);
78 /**
79 * \brief find a memory font by name
81 static int find_font(ass_library_t* library, char* name)
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;
90 static void face_set_size(FT_Face face, double size);
92 static void buggy_font_workaround(FT_Face face)
94 // Some fonts have zero Ascender/Descender fields in 'hhea' table.
95 // In this case, get the information from 'os2' table or, as
96 // a last resort, from face.bbox.
97 if (face->ascender + face->descender == 0 || face->height == 0) {
98 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
99 if (os2) {
100 face->ascender = os2->sTypoAscender;
101 face->descender = os2->sTypoDescender;
102 face->height = face->ascender - face->descender;
103 } else {
104 face->ascender = face->bbox.yMax;
105 face->descender = face->bbox.yMin;
106 face->height = face->ascender - face->descender;
112 * \brief Select a face with the given charcode and add it to ass_font_t
113 * \return index of the new face in font->faces, -1 if failed
115 static int add_face(void* fc_priv, ass_font_t* font, uint32_t ch)
117 char* path;
118 int index;
119 FT_Face face;
120 int error;
121 int mem_idx;
123 if (font->n_faces == ASS_FONT_MAX_FACES)
124 return -1;
126 path = fontconfig_select(fc_priv, font->desc.family, font->desc.bold,
127 font->desc.italic, &index, ch);
129 mem_idx = find_font(font->library, path);
130 if (mem_idx >= 0) {
131 error = FT_New_Memory_Face(font->ftlibrary, (unsigned char*)font->library->fontdata[mem_idx].data,
132 font->library->fontdata[mem_idx].size, 0, &face);
133 if (error) {
134 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
135 return -1;
137 } else {
138 error = FT_New_Face(font->ftlibrary, path, index, &face);
139 if (error) {
140 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
141 return -1;
144 charmap_magic(face);
145 buggy_font_workaround(face);
147 font->faces[font->n_faces++] = face;
148 update_transform(font);
149 face_set_size(face, font->size);
150 return font->n_faces - 1;
154 * \brief Create a new ass_font_t according to "desc" argument
156 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
158 int error;
159 ass_font_t* fontp;
160 ass_font_t font;
162 fontp = ass_font_cache_find(desc);
163 if (fontp)
164 return fontp;
166 font.library = library;
167 font.ftlibrary = ftlibrary;
168 font.n_faces = 0;
169 font.desc.family = strdup(desc->family);
170 font.desc.bold = desc->bold;
171 font.desc.italic = desc->italic;
173 font.scale_x = font.scale_y = 1.;
174 font.v.x = font.v.y = 0;
175 font.size = 0.;
177 error = add_face(fc_priv, &font, 0);
178 if (error == -1) {
179 free(font.desc.family);
180 return 0;
181 } else
182 return ass_font_cache_add(&font);
186 * \brief Set font transformation matrix and shift vector
188 void ass_font_set_transform(ass_font_t* font, double scale_x, double scale_y, FT_Vector* v)
190 font->scale_x = scale_x;
191 font->scale_y = scale_y;
192 font->v.x = v->x;
193 font->v.y = v->y;
194 update_transform(font);
197 static void face_set_size(FT_Face face, double size)
199 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1))
200 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
201 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
202 double mscale = 1.;
203 FT_Size_RequestRec rq;
204 FT_Size_Metrics *m = &face->size->metrics;
205 // VSFilter uses metrics from TrueType OS/2 table
206 // The idea was borrowed from asa (http://asa.diac24.net)
207 if (hori && os2) {
208 int hori_height = hori->Ascender - hori->Descender;
209 int os2_height = os2->usWinAscent + os2->usWinDescent;
210 if (hori_height && os2_height)
211 mscale = (double)hori_height / os2_height;
213 memset(&rq, 0, sizeof(rq));
214 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
215 rq.width = 0;
216 rq.height = double_to_d6(size * mscale);
217 rq.horiResolution = rq.vertResolution = 0;
218 FT_Request_Size(face, &rq);
219 m->ascender /= mscale;
220 m->descender /= mscale;
221 m->height /= mscale;
222 #else
223 FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0);
224 #endif
228 * \brief Set font size
230 void ass_font_set_size(ass_font_t* font, double size)
232 int i;
233 if (font->size != size) {
234 font->size = size;
235 for (i = 0; i < font->n_faces; ++i)
236 face_set_size(font->faces[i], size);
241 * \brief Get maximal font ascender and descender.
242 * \param ch character code
243 * The values are extracted from the font face that provides glyphs for the given character
245 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
247 int i;
248 for (i = 0; i < font->n_faces; ++i) {
249 FT_Face face = font->faces[i];
250 if (FT_Get_Char_Index(face, ch)) {
251 int v, v2;
252 v = face->size->metrics.ascender;
253 v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
254 *asc = (v > v2 * 0.9) ? v : v2;
256 v = - face->size->metrics.descender;
257 v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
258 *desc = (v > v2 * 0.9) ? v : v2;
259 return;
263 *asc = *desc = 0;
267 * \brief Get a glyph
268 * \param ch character code
270 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting)
272 int error;
273 int index = 0;
274 int i;
275 FT_Glyph glyph;
276 FT_Face face = 0;
277 int flags = 0;
279 if (ch < 0x20)
280 return 0;
281 if (font->n_faces == 0)
282 return 0;
284 for (i = 0; i < font->n_faces; ++i) {
285 face = font->faces[i];
286 index = FT_Get_Char_Index(face, ch);
287 if (index)
288 break;
291 #ifdef HAVE_FONTCONFIG
292 if (index == 0) {
293 int face_idx;
294 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
295 ch, font->desc.family, font->desc.bold, font->desc.italic);
296 face_idx = add_face(fontconfig_priv, font, ch);
297 if (face_idx >= 0) {
298 face = font->faces[face_idx];
299 index = FT_Get_Char_Index(face, ch);
300 if (index == 0) {
301 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
302 ch, font->desc.family, font->desc.bold, font->desc.italic);
306 #endif
308 switch (hinting) {
309 case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break;
310 case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break;
311 case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break;
312 case ASS_HINTING_NATIVE: flags = 0; break;
315 error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
316 if (error) {
317 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
318 return 0;
321 #if (FREETYPE_MAJOR > 2) || \
322 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
323 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
324 // FreeType >= 2.1.10 required
325 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
326 (font->desc.italic > 55)) {
327 FT_GlyphSlot_Oblique(face->glyph);
329 #endif
330 error = FT_Get_Glyph(face->glyph, &glyph);
331 if (error) {
332 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
333 return 0;
336 return glyph;
340 * \brief Get kerning for the pair of glyphs.
342 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
344 FT_Vector v = {0, 0};
345 int i;
347 for (i = 0; i < font->n_faces; ++i) {
348 FT_Face face = font->faces[i];
349 int i1 = FT_Get_Char_Index(face, c1);
350 int i2 = FT_Get_Char_Index(face, c2);
351 if (i1 && i2) {
352 if (FT_HAS_KERNING(face))
353 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
354 return v;
356 if (i1 || i2) // these glyphs are from different font faces, no kerning information
357 return v;
359 return v;
363 * \brief Deallocate ass_font_t
365 void ass_font_free(ass_font_t* font)
367 int i;
368 for (i = 0; i < font->n_faces; ++i)
369 if (font->faces[i]) FT_Done_Face(font->faces[i]);
370 if (font->desc.family) free(font->desc.family);
371 free(font);