1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
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
25 #include FT_FREETYPE_H
26 #include FT_SYNTHESIS_H
28 #include FT_TRUETYPE_TABLES_H
31 #include "ass_library.h"
33 #include "ass_bitmap.h"
34 #include "ass_cache.h"
35 #include "ass_fontconfig.h"
36 #include "ass_utils.h"
40 * Select Microfost Unicode CharMap, if the font has one.
41 * Otherwise, let FreeType decide.
43 static void charmap_magic(FT_Face face)
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);
57 if (face->num_charmaps == 0) {
58 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
61 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
62 FT_Set_Charmap(face, face->charmaps[0]);
67 static void update_transform(ass_font_t* font)
71 m.xx = double_to_d16(font->scale_x);
72 m.yy = double_to_d16(font->scale_y);
74 for (i = 0; i < font->n_faces; ++i)
75 FT_Set_Transform(font->faces[i], &m, &font->v);
79 * \brief find a memory font by name
81 static int find_font(ass_library_t* library, char* name)
84 for (i = 0; i < library->num_fontdata; ++i)
85 if (strcasecmp(name, library->fontdata[i].name) == 0)
91 * \brief Create a new ass_font_t according to "desc" argument
93 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
103 fontp = ass_font_cache_find(desc);
107 path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
109 mem_idx = find_font(library, path);
111 error = FT_New_Memory_Face(ftlibrary, (unsigned char*)library->fontdata[mem_idx].data,
112 library->fontdata[mem_idx].size, 0, &face);
114 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
118 error = FT_New_Face(ftlibrary, path, index, &face);
120 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
127 font.ftlibrary = ftlibrary;
128 font.faces[0] = face;
130 font.desc.family = strdup(desc->family);
131 font.desc.bold = desc->bold;
132 font.desc.italic = desc->italic;
134 font.scale_x = font.scale_y = 1.;
135 font.v.x = font.v.y = 0;
138 #ifdef HAVE_FONTCONFIG
139 font.charset = FcCharSetCreate();
142 return ass_font_cache_add(&font);
146 * \brief Set font transformation matrix and shift vector
148 void ass_font_set_transform(ass_font_t* font, double scale_x, double scale_y, FT_Vector* v)
150 font->scale_x = scale_x;
151 font->scale_y = scale_y;
154 update_transform(font);
157 static void face_set_size(FT_Face face, double size)
159 #if (FREETYPE_MAJOR >= 2)
160 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
161 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
163 FT_Size_RequestRec rq;
164 FT_Size_Metrics *m = &face->size->metrics;
165 // VSFilter uses metrics from TrueType OS/2 table
166 // The idea was borrowed from asa (http://asa.diac24.net)
168 mscale = ((double)(hori->Ascender - hori->Descender)) / (os2->usWinAscent + os2->usWinDescent);
169 memset(&rq, 0, sizeof(rq));
170 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
172 rq.height = double_to_d6(size * mscale);
173 rq.horiResolution = rq.vertResolution = 0;
174 FT_Request_Size(face, &rq);
175 m->ascender /= mscale;
176 m->descender /= mscale;
179 FT_Set_Char_Size(face, 0, double_to_d6(size), 0, 0);
184 * \brief Set font size
186 void ass_font_set_size(ass_font_t* font, double size)
189 if (font->size != size) {
191 for (i = 0; i < font->n_faces; ++i)
192 face_set_size(font->faces[i], size);
196 #ifdef HAVE_FONTCONFIG
198 * \brief Select a new FT_Face with the given character
199 * The new face is added to the end of font->faces.
201 static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
208 if (font->n_faces == ASS_FONT_MAX_FACES)
211 path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
212 font->desc.italic, &index, font->charset);
214 error = FT_New_Face(font->ftlibrary, path, index, &face);
216 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
221 error = FT_Get_Char_Index(face, ch);
222 if (error == 0) { // the new font face is not better then the old one
227 font->faces[font->n_faces++] = face;
228 update_transform(font);
229 FT_Set_Pixel_Sizes(face, 0, (int)font->size);
234 * \brief Get maximal font ascender and descender.
235 * \param ch character code
236 * The values are extracted from the font face that provides glyphs for the given character
238 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
241 for (i = 0; i < font->n_faces; ++i) {
242 FT_Face face = font->faces[i];
243 if (FT_Get_Char_Index(face, ch)) {
245 v = face->size->metrics.ascender;
246 v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
247 *asc = (v > v2 * 0.9) ? v : v2;
249 v = - face->size->metrics.descender;
250 v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
251 *desc = (v > v2 * 0.9) ? v : v2;
261 * \param ch character code
263 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting)
274 if (font->n_faces == 0)
277 for (i = 0; i < font->n_faces; ++i) {
278 face = font->faces[i];
279 index = FT_Get_Char_Index(face, ch);
284 #ifdef HAVE_FONTCONFIG
285 FcCharSetAddChar(font->charset, ch);
287 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
288 ch, font->desc.family, font->desc.bold, font->desc.italic);
289 ass_font_reselect(fontconfig_priv, font, ch);
290 face = font->faces[font->n_faces - 1];
291 index = FT_Get_Char_Index(face, ch);
293 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
294 ch, font->desc.family, font->desc.bold, font->desc.italic);
300 case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break;
301 case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break;
302 case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break;
303 case ASS_HINTING_NATIVE: flags = 0; break;
306 error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
308 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
312 #if (FREETYPE_MAJOR > 2) || \
313 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
314 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
315 // FreeType >= 2.1.10 required
316 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
317 (font->desc.italic > 55)) {
318 FT_GlyphSlot_Oblique(face->glyph);
321 error = FT_Get_Glyph(face->glyph, &glyph);
323 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
331 * \brief Get kerning for the pair of glyphs.
333 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
335 FT_Vector v = {0, 0};
338 for (i = 0; i < font->n_faces; ++i) {
339 FT_Face face = font->faces[i];
340 int i1 = FT_Get_Char_Index(face, c1);
341 int i2 = FT_Get_Char_Index(face, c2);
343 if (FT_HAS_KERNING(face))
344 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
347 if (i1 || i2) // these glyphs are from different font faces, no kerning information
354 * \brief Deallocate ass_font_t
356 void ass_font_free(ass_font_t* font)
359 for (i = 0; i < font->n_faces; ++i)
360 if (font->faces[i]) FT_Done_Face(font->faces[i]);
361 if (font->desc.family) free(font->desc.family);
362 #ifdef HAVE_FONTCONFIG
363 if (font->charset) FcCharSetDestroy(font->charset);