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
30 #include "ass_bitmap.h"
31 #include "ass_cache.h"
32 #include "ass_fontconfig.h"
36 * Select Microfost Unicode CharMap, if the font has one.
37 * Otherwise, let FreeType decide.
39 static void charmap_magic(FT_Face face)
42 for (i = 0; i < face->num_charmaps; ++i) {
43 FT_CharMap cmap = face->charmaps[i];
44 unsigned pid = cmap->platform_id;
45 unsigned eid = cmap->encoding_id;
46 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
47 FT_Set_Charmap(face, cmap);
53 ass_font_t* ass_font_new(FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
61 font = ass_font_cache_find(desc);
65 path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
67 error = FT_New_Face(ftlibrary, path, index, &face);
69 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
75 font = calloc(1, sizeof(ass_font_t));
76 font->ftlibrary = ftlibrary;
77 font->path = strdup(path);
80 font->desc.family = strdup(desc->family);
81 font->desc.bold = desc->bold;
82 font->desc.italic = desc->italic;
84 font->m.xx = font->m.yy = (FT_Fixed)0x10000L;
85 font->m.xy = font->m.yy = 0;
86 font->v.x = font->v.y = 0;
89 #ifdef HAVE_FONTCONFIG
90 font->charset = FcCharSetCreate();
93 ass_font_cache_add(font);
98 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v)
100 if (font->m.xx != m->xx ||
101 font->m.xy != m->xy ||
102 font->m.yx != m->yx ||
103 font->m.yy != m->yy ||
113 FT_Set_Transform(font->face, &font->m, &font->v);
117 void ass_font_set_size(ass_font_t* font, int size)
119 if (font->size != size) {
121 FT_Set_Pixel_Sizes(font->face, 0, size);
125 #ifdef HAVE_FONTCONFIG
126 static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font)
133 path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
134 font->desc.italic, &index, font->charset);
135 if (strcasecmp(path, font->path) == 0 && index == font->index) {
140 error = FT_New_Face(font->ftlibrary, path, index, &face);
142 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
147 if (font->face) FT_Done_Face(font->face);
151 font->path = strdup(path);
154 FT_Set_Transform(font->face, &font->m, &font->v);
155 FT_Set_Pixel_Sizes(font->face, 0, font->size);
159 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
168 index = FT_Get_Char_Index(font->face, ch);
169 #ifdef HAVE_FONTCONFIG
170 FcCharSetAddChar(font->charset, ch);
172 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
173 ch, font->desc.family, font->desc.bold, font->desc.italic);
174 ass_font_reselect(fontconfig_priv, font);
175 index = FT_Get_Char_Index(font->face, ch);
177 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
178 ch, font->desc.family, font->desc.bold, font->desc.italic);
183 error = FT_Load_Glyph(font->face, index, FT_LOAD_NO_BITMAP );
185 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
189 #if (FREETYPE_MAJOR > 2) || \
190 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
191 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
192 // FreeType >= 2.1.10 required
193 if (!(font->face->style_flags & FT_STYLE_FLAG_ITALIC) &&
194 (font->desc.italic > 55)) {
195 FT_GlyphSlot_Oblique(font->face->glyph);
198 error = FT_Get_Glyph(font->face->glyph, &glyph);
200 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
207 void ass_font_free(ass_font_t* font)
209 if (font->face) FT_Done_Face(font->face);
210 if (font->path) free(font->path);
211 if (font->desc.family) free(font->desc.family);
212 #ifdef HAVE_FONTCONFIG
213 if (font->charset) FcCharSetDestroy(font->charset);