Get rid of pointless preprocessor condition indirection and use ARCH_X86
[mplayer/glamo.git] / libass / ass_fontconfig.c
blobd0042dcdfdd50e10f23404815ee4aed18a16a01e
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 <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <inttypes.h>
32 #include <ft2build.h>
33 #include FT_FREETYPE_H
35 #include "mputils.h"
36 #include "ass.h"
37 #include "ass_library.h"
38 #include "ass_fontconfig.h"
40 #ifdef CONFIG_FONTCONFIG
41 #include <fontconfig/fontconfig.h>
42 #include <fontconfig/fcfreetype.h>
43 #endif
45 struct fc_instance_s {
46 #ifdef CONFIG_FONTCONFIG
47 FcConfig* config;
48 #endif
49 char* family_default;
50 char* path_default;
51 int index_default;
54 #ifdef CONFIG_FONTCONFIG
56 // 4yo fontconfig does not have these.
57 // They are only needed for debug output, anyway.
58 #ifndef FC_FULLNAME
59 #define FC_FULLNAME "fullname"
60 #endif
61 #ifndef FC_EMBOLDEN
62 #define FC_EMBOLDEN "embolden"
63 #endif
65 /**
66 * \brief Low-level font selection.
67 * \param priv private data
68 * \param family font family
69 * \param treat_family_as_pattern treat family as fontconfig pattern
70 * \param bold font weight value
71 * \param italic font slant value
72 * \param index out: font index inside a file
73 * \param code: the character that should be present in the font, can be 0
74 * \return font file path
75 */
76 static char* _select_font(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
77 unsigned bold, unsigned italic, int* index, uint32_t code)
79 FcBool rc;
80 FcResult result;
81 FcPattern *pat = NULL, *rpat = NULL;
82 int r_index, r_slant, r_weight;
83 FcChar8 *r_family, *r_style, *r_file, *r_fullname;
84 FcBool r_outline, r_embolden;
85 FcCharSet* r_charset;
86 FcFontSet* fset = NULL;
87 int curf;
88 char* retval = NULL;
89 int family_cnt;
91 *index = 0;
93 if (treat_family_as_pattern)
94 pat = FcNameParse((const FcChar8*)family);
95 else
96 pat = FcPatternCreate();
98 if (!pat)
99 goto error;
101 if (!treat_family_as_pattern) {
102 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family);
104 // In SSA/ASS fonts are sometimes referenced by their "full name",
105 // which is usually a concatenation of family name and font
106 // style (ex. Ottawa Bold). Full name is available from
107 // FontConfig pattern element FC_FULLNAME, but it is never
108 // used for font matching.
109 // Therefore, I'm removing words from the end of the name one
110 // by one, and adding shortened names to the pattern. It seems
111 // that the first value (full name in this case) has
112 // precedence in matching.
113 // An alternative approach could be to reimplement FcFontSort
114 // using FC_FULLNAME instead of FC_FAMILY.
115 family_cnt = 1;
117 char* s = strdup(family);
118 char* p = s + strlen(s);
119 while (--p > s)
120 if (*p == ' ' || *p == '-') {
121 *p = '\0';
122 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)s);
123 ++ family_cnt;
125 free(s);
128 FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
129 FcPatternAddInteger(pat, FC_SLANT, italic);
130 FcPatternAddInteger(pat, FC_WEIGHT, bold);
132 FcDefaultSubstitute(pat);
134 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
135 if (!rc)
136 goto error;
138 fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
139 if (!fset)
140 goto error;
142 for (curf = 0; curf < fset->nfont; ++curf) {
143 FcPattern* curp = fset->fonts[curf];
145 result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
146 if (result != FcResultMatch)
147 continue;
148 if (r_outline != FcTrue)
149 continue;
150 if (!code)
151 break;
152 result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
153 if (result != FcResultMatch)
154 continue;
155 if (FcCharSetHasChar(r_charset, code))
156 break;
159 if (curf >= fset->nfont)
160 goto error;
162 #if (FC_VERSION >= 20297)
163 // Remove all extra family names from original pattern.
164 // After this, FcFontRenderPrepare will select the most relevant family
165 // name in case there are more than one of them.
166 for (; family_cnt > 1; --family_cnt)
167 FcPatternRemove(pat, FC_FAMILY, family_cnt - 1);
168 #endif
170 rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
171 if (!rpat)
172 goto error;
174 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
175 if (result != FcResultMatch)
176 goto error;
177 *index = r_index;
179 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
180 if (result != FcResultMatch)
181 goto error;
182 retval = strdup((const char*)r_file);
184 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
185 if (result != FcResultMatch)
186 r_family = NULL;
188 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
189 if (result != FcResultMatch)
190 r_fullname = NULL;
192 if (!treat_family_as_pattern &&
193 !(r_family && strcasecmp((const char*)r_family, family) == 0) &&
194 !(r_fullname && strcasecmp((const char*)r_fullname, family) == 0))
195 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne,
196 (const char*)(r_fullname ? r_fullname : r_family), family);
198 result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
199 if (result != FcResultMatch)
200 r_style = NULL;
202 result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
203 if (result != FcResultMatch)
204 r_slant = 0;
206 result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
207 if (result != FcResultMatch)
208 r_weight = 0;
210 result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
211 if (result != FcResultMatch)
212 r_embolden = 0;
214 mp_msg(MSGT_ASS, MSGL_V, "[ass] Font info: family '%s', style '%s', fullname '%s',"
215 " slant %d, weight %d%s\n",
216 (const char*)r_family, (const char*)r_style, (const char*)r_fullname,
217 r_slant, r_weight, r_embolden ? ", embolden" : "");
219 error:
220 if (pat) FcPatternDestroy(pat);
221 if (rpat) FcPatternDestroy(rpat);
222 if (fset) FcFontSetDestroy(fset);
223 return retval;
227 * \brief Find a font. Use default family or path if necessary.
228 * \param priv_ private data
229 * \param family font family
230 * \param treat_family_as_pattern treat family as fontconfig pattern
231 * \param bold font weight value
232 * \param italic font slant value
233 * \param index out: font index inside a file
234 * \param code: the character that should be present in the font, can be 0
235 * \return font file path
237 char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
238 unsigned bold, unsigned italic, int* index, uint32_t code)
240 char* res = 0;
241 if (!priv->config) {
242 *index = priv->index_default;
243 return priv->path_default;
245 if (family && *family)
246 res = _select_font(priv, family, treat_family_as_pattern, bold, italic, index, code);
247 if (!res && priv->family_default) {
248 res = _select_font(priv, priv->family_default, 0, bold, italic, index, code);
249 if (res)
250 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily,
251 family, bold, italic, res, *index);
253 if (!res && priv->path_default) {
254 res = priv->path_default;
255 *index = priv->index_default;
256 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont,
257 family, bold, italic, res, *index);
259 if (!res) {
260 res = _select_font(priv, "Arial", 0, bold, italic, index, code);
261 if (res)
262 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily,
263 family, bold, italic, res, *index);
265 if (res)
266 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n",
267 family, bold, italic, res, *index);
268 return res;
271 #if (FC_VERSION < 20402)
272 static char* validate_fname(char* name)
274 char* fname;
275 char* p;
276 char* q;
277 unsigned code;
278 int sz = strlen(name);
280 q = fname = malloc(sz + 1);
281 p = name;
282 while (*p) {
283 code = utf8_get_char(&p);
284 if (code == 0)
285 break;
286 if ( (code > 0x7F) ||
287 (code == '\\') ||
288 (code == '/') ||
289 (code == ':') ||
290 (code == '*') ||
291 (code == '?') ||
292 (code == '<') ||
293 (code == '>') ||
294 (code == '|') ||
295 (code == 0))
297 *q++ = '_';
298 } else {
299 *q++ = code;
301 if (p - name > sz)
302 break;
304 *q = 0;
305 return fname;
307 #endif
310 * \brief Process memory font.
311 * \param priv private data
312 * \param library library object
313 * \param ftlibrary freetype library object
314 * \param idx index of the processed font in library->fontdata
315 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
316 * With older FontConfig versions, save the font to ~/.mplayer/fonts.
318 static void process_fontdata(fc_instance_t* priv, ass_library_t* library, FT_Library ftlibrary, int idx)
320 int rc;
321 const char* name = library->fontdata[idx].name;
322 const char* data = library->fontdata[idx].data;
323 int data_size = library->fontdata[idx].size;
325 #if (FC_VERSION < 20402)
326 struct stat st;
327 char* fname;
328 const char* fonts_dir = library->fonts_dir;
329 char buf[1000];
330 FILE* fp = NULL;
332 if (!fonts_dir)
333 return;
334 rc = stat(fonts_dir, &st);
335 if (rc) {
336 int res;
337 #ifndef __MINGW32__
338 res = mkdir(fonts_dir, 0700);
339 #else
340 res = mkdir(fonts_dir);
341 #endif
342 if (res) {
343 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir);
345 } else if (!S_ISDIR(st.st_mode)) {
346 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir);
349 fname = validate_fname((char*)name);
351 snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
352 free(fname);
354 fp = fopen(buf, "wb");
355 if (!fp) return;
357 fwrite(data, data_size, 1, fp);
358 fclose(fp);
360 #else // (FC_VERSION >= 20402)
361 FT_Face face;
362 FcPattern* pattern;
363 FcFontSet* fset;
364 FcBool res;
365 int face_index, num_faces = 1;
367 for (face_index = 0; face_index < num_faces; ++face_index) {
368 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, face_index, &face);
369 if (rc) {
370 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name);
371 return;
373 num_faces = face->num_faces;
375 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config));
376 if (!pattern) {
377 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace");
378 FT_Done_Face(face);
379 return;
382 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
383 if (!fset) {
384 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts");
385 FT_Done_Face(face);
386 return;
389 res = FcFontSetAdd(fset, pattern);
390 if (!res) {
391 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd");
392 FT_Done_Face(face);
393 return;
396 FT_Done_Face(face);
398 #endif
402 * \brief Init fontconfig.
403 * \param library libass library object
404 * \param ftlibrary freetype library object
405 * \param family default font family
406 * \param path default font path
407 * \return pointer to fontconfig private data
409 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc)
411 int rc;
412 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t));
413 const char* dir = library->fonts_dir;
414 int i;
416 if (!fc) {
417 mp_msg(MSGT_ASS, MSGL_WARN,
418 MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed);
419 goto exit;
422 rc = FcInit();
423 assert(rc);
425 priv->config = FcConfigGetCurrent();
426 if (!priv->config) {
427 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed);
428 goto exit;
431 for (i = 0; i < library->num_fontdata; ++i)
432 process_fontdata(priv, library, ftlibrary, i);
434 if (dir) {
435 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse)
437 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache);
438 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400)
439 mp_msg(MSGT_ASS, MSGL_WARN,
440 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported);
441 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir()
442 if (FcGetVersion() < 20390) {
443 FcFontSet* fcs;
444 FcStrSet* fss;
445 fcs = FcFontSetCreate();
446 fss = FcStrSetCreate();
447 rc = FcStrSetAdd(fss, (const FcChar8*)dir);
448 if (!rc) {
449 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed);
450 goto ErrorFontCache;
453 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config),
454 (const FcChar8 *)dir, FcFalse);
455 if (!rc) {
456 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed);
457 goto ErrorFontCache;
460 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir);
461 if (!rc) {
462 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave);
463 goto ErrorFontCache;
465 ErrorFontCache:
470 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir);
471 if (!rc) {
472 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed);
476 priv->family_default = family ? strdup(family) : NULL;
477 exit:
478 priv->path_default = path ? strdup(path) : NULL;
479 priv->index_default = 0;
481 return priv;
484 #else /* CONFIG_FONTCONFIG */
486 char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
487 unsigned bold, unsigned italic, int* index, uint32_t code)
489 *index = priv->index_default;
490 return priv->path_default;
493 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc)
495 fc_instance_t* priv;
497 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed);
499 priv = calloc(1, sizeof(fc_instance_t));
501 priv->path_default = strdup(path);
502 priv->index_default = 0;
503 return priv;
506 #endif
508 void fontconfig_done(fc_instance_t* priv)
510 // don't call FcFini() here, library can still be used by some code
511 if (priv && priv->path_default) free(priv->path_default);
512 if (priv && priv->family_default) free(priv->family_default);
513 if (priv) free(priv);