Relicense to ISC
[libass.git] / libass / ass_fontconfig.c
blob2571739ff219f67289763dbb6e22709e153a271d
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <inttypes.h>
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
32 #include "ass_utils.h"
33 #include "ass.h"
34 #include "ass_library.h"
35 #include "ass_fontconfig.h"
37 #ifdef CONFIG_FONTCONFIG
38 #include <fontconfig/fontconfig.h>
39 #include <fontconfig/fcfreetype.h>
40 #endif
42 struct fc_instance {
43 #ifdef CONFIG_FONTCONFIG
44 FcConfig *config;
45 #endif
46 char *family_default;
47 char *path_default;
48 int index_default;
51 #ifdef CONFIG_FONTCONFIG
53 /**
54 * \brief Case-insensitive match ASS/SSA font family against full name. (also
55 * known as "name for humans")
57 * \param lib library instance
58 * \param priv fontconfig instance
59 * \param family font fullname
60 * \param bold weight attribute
61 * \param italic italic attribute
62 * \return font set
64 static FcFontSet *
65 match_fullname(ASS_Library *lib, FCInstance *priv, const char *family,
66 unsigned bold, unsigned italic)
68 FcFontSet *sets[2];
69 FcFontSet *result = FcFontSetCreate();
70 int nsets = 0;
71 int i, fi;
73 if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetSystem)))
74 nsets++;
75 if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetApplication)))
76 nsets++;
78 // Run over font sets and patterns and try to match against full name
79 for (i = 0; i < nsets; i++) {
80 FcFontSet *set = sets[i];
81 for (fi = 0; fi < set->nfont; fi++) {
82 FcPattern *pat = set->fonts[fi];
83 char *fullname;
84 int pi = 0, at;
85 FcBool ol;
86 while (FcPatternGetString(pat, FC_FULLNAME, pi++,
87 (FcChar8 **) &fullname) == FcResultMatch) {
88 if (FcPatternGetBool(pat, FC_OUTLINE, 0, &ol) != FcResultMatch
89 || ol != FcTrue)
90 continue;
91 if (FcPatternGetInteger(pat, FC_SLANT, 0, &at) != FcResultMatch
92 || at < italic)
93 continue;
94 if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &at) != FcResultMatch
95 || at < bold)
96 continue;
97 if (strcasecmp(fullname, family) == 0) {
98 FcFontSetAdd(result, FcPatternDuplicate(pat));
99 break;
105 return result;
109 * \brief Low-level font selection.
110 * \param priv private data
111 * \param family font family
112 * \param treat_family_as_pattern treat family as fontconfig pattern
113 * \param bold font weight value
114 * \param italic font slant value
115 * \param index out: font index inside a file
116 * \param code: the character that should be present in the font, can be 0
117 * \return font file path
119 static char *select_font(ASS_Library *library, FCInstance *priv,
120 const char *family, int treat_family_as_pattern,
121 unsigned bold, unsigned italic, int *index,
122 uint32_t code)
124 FcBool rc;
125 FcResult result;
126 FcPattern *pat = NULL, *rpat = NULL;
127 int r_index, r_slant, r_weight;
128 FcChar8 *r_family, *r_style, *r_file, *r_fullname;
129 FcBool r_outline, r_embolden;
130 FcCharSet *r_charset;
131 FcFontSet *ffullname = NULL, *fsorted = NULL, *fset = NULL;
132 int curf;
133 char *retval = NULL;
134 int family_cnt = 0;
136 *index = 0;
138 if (treat_family_as_pattern)
139 pat = FcNameParse((const FcChar8 *) family);
140 else
141 pat = FcPatternCreate();
143 if (!pat)
144 goto error;
146 if (!treat_family_as_pattern) {
147 FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) family);
149 // In SSA/ASS fonts are sometimes referenced by their "full name",
150 // which is usually a concatenation of family name and font
151 // style (ex. Ottawa Bold). Full name is available from
152 // FontConfig pattern element FC_FULLNAME, but it is never
153 // used for font matching.
154 // Therefore, I'm removing words from the end of the name one
155 // by one, and adding shortened names to the pattern. It seems
156 // that the first value (full name in this case) has
157 // precedence in matching.
158 // An alternative approach could be to reimplement FcFontSort
159 // using FC_FULLNAME instead of FC_FAMILY.
160 family_cnt = 1;
162 char *s = strdup(family);
163 char *p = s + strlen(s);
164 while (--p > s)
165 if (*p == ' ' || *p == '-') {
166 *p = '\0';
167 FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) s);
168 ++family_cnt;
170 free(s);
173 FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
174 FcPatternAddInteger(pat, FC_SLANT, italic);
175 FcPatternAddInteger(pat, FC_WEIGHT, bold);
177 FcDefaultSubstitute(pat);
179 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
180 if (!rc)
181 goto error;
183 fsorted = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
184 ffullname = match_fullname(library, priv, family, bold, italic);
185 if (!fsorted || !ffullname)
186 goto error;
188 fset = FcFontSetCreate();
189 for (curf = 0; curf < ffullname->nfont; ++curf) {
190 FcPattern *curp = ffullname->fonts[curf];
191 FcPatternReference(curp);
192 FcFontSetAdd(fset, curp);
194 for (curf = 0; curf < fsorted->nfont; ++curf) {
195 FcPattern *curp = fsorted->fonts[curf];
196 FcPatternReference(curp);
197 FcFontSetAdd(fset, curp);
200 for (curf = 0; curf < fset->nfont; ++curf) {
201 FcPattern *curp = fset->fonts[curf];
203 result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
204 if (result != FcResultMatch)
205 continue;
206 if (r_outline != FcTrue)
207 continue;
208 if (!code)
209 break;
210 result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
211 if (result != FcResultMatch)
212 continue;
213 if (FcCharSetHasChar(r_charset, code))
214 break;
217 if (curf >= fset->nfont)
218 goto error;
220 if (!treat_family_as_pattern) {
221 // Remove all extra family names from original pattern.
222 // After this, FcFontRenderPrepare will select the most relevant family
223 // name in case there are more than one of them.
224 for (; family_cnt > 1; --family_cnt)
225 FcPatternRemove(pat, FC_FAMILY, family_cnt - 1);
228 rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
229 if (!rpat)
230 goto error;
232 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
233 if (result != FcResultMatch)
234 goto error;
235 *index = r_index;
237 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
238 if (result != FcResultMatch)
239 goto error;
240 retval = strdup((const char *) r_file);
242 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
243 if (result != FcResultMatch)
244 r_family = NULL;
246 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
247 if (result != FcResultMatch)
248 r_fullname = NULL;
250 if (!treat_family_as_pattern &&
251 !(r_family && strcasecmp((const char *) r_family, family) == 0) &&
252 !(r_fullname && strcasecmp((const char *) r_fullname, family) == 0))
253 ass_msg(library, MSGL_WARN,
254 "fontconfig: Selected font is not the requested one: "
255 "'%s' != '%s'",
256 (const char *) (r_fullname ? r_fullname : r_family), family);
258 result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
259 if (result != FcResultMatch)
260 r_style = NULL;
262 result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
263 if (result != FcResultMatch)
264 r_slant = 0;
266 result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
267 if (result != FcResultMatch)
268 r_weight = 0;
270 result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
271 if (result != FcResultMatch)
272 r_embolden = 0;
274 ass_msg(library, MSGL_V,
275 "Font info: family '%s', style '%s', fullname '%s',"
276 " slant %d, weight %d%s", (const char *) r_family,
277 (const char *) r_style, (const char *) r_fullname, r_slant,
278 r_weight, r_embolden ? ", embolden" : "");
280 error:
281 if (pat)
282 FcPatternDestroy(pat);
283 if (rpat)
284 FcPatternDestroy(rpat);
285 if (fsorted)
286 FcFontSetDestroy(fsorted);
287 if (ffullname)
288 FcFontSetDestroy(ffullname);
289 if (fset)
290 FcFontSetDestroy(fset);
291 return retval;
295 * \brief Find a font. Use default family or path if necessary.
296 * \param priv_ private data
297 * \param family font family
298 * \param treat_family_as_pattern treat family as fontconfig pattern
299 * \param bold font weight value
300 * \param italic font slant value
301 * \param index out: font index inside a file
302 * \param code: the character that should be present in the font, can be 0
303 * \return font file path
305 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
306 const char *family, int treat_family_as_pattern,
307 unsigned bold, unsigned italic, int *index,
308 uint32_t code)
310 char *res = 0;
311 if (!priv->config) {
312 *index = priv->index_default;
313 res = priv->path_default ? strdup(priv->path_default) : 0;
314 return res;
316 if (family && *family)
317 res =
318 select_font(library, priv, family, treat_family_as_pattern,
319 bold, italic, index, code);
320 if (!res && priv->family_default) {
321 res =
322 select_font(library, priv, priv->family_default, 0, bold,
323 italic, index, code);
324 if (res)
325 ass_msg(library, MSGL_WARN, "fontconfig_select: Using default "
326 "font family: (%s, %d, %d) -> %s, %d",
327 family, bold, italic, res, *index);
329 if (!res && priv->path_default) {
330 res = strdup(priv->path_default);
331 *index = priv->index_default;
332 ass_msg(library, MSGL_WARN, "fontconfig_select: Using default font: "
333 "(%s, %d, %d) -> %s, %d", family, bold, italic,
334 res, *index);
336 if (!res) {
337 res = select_font(library, priv, "Arial", 0, bold, italic,
338 index, code);
339 if (res)
340 ass_msg(library, MSGL_WARN, "fontconfig_select: Using 'Arial' "
341 "font family: (%s, %d, %d) -> %s, %d", family, bold,
342 italic, res, *index);
344 if (res)
345 ass_msg(library, MSGL_V,
346 "fontconfig_select: (%s, %d, %d) -> %s, %d", family, bold,
347 italic, res, *index);
348 return res;
352 * \brief Process memory font.
353 * \param priv private data
354 * \param library library object
355 * \param ftlibrary freetype library object
356 * \param idx index of the processed font in library->fontdata
358 * Builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
360 static void process_fontdata(FCInstance *priv, ASS_Library *library,
361 FT_Library ftlibrary, int idx)
363 int rc;
364 const char *name = library->fontdata[idx].name;
365 const char *data = library->fontdata[idx].data;
366 int data_size = library->fontdata[idx].size;
368 FT_Face face;
369 FcPattern *pattern;
370 FcFontSet *fset;
371 FcBool res;
372 int face_index, num_faces = 1;
374 for (face_index = 0; face_index < num_faces; ++face_index) {
375 rc = FT_New_Memory_Face(ftlibrary, (unsigned char *) data,
376 data_size, face_index, &face);
377 if (rc) {
378 ass_msg(library, MSGL_WARN, "Error opening memory font: %s",
379 name);
380 return;
382 num_faces = face->num_faces;
384 pattern =
385 FcFreeTypeQueryFace(face, (unsigned char *) name, face_index,
386 FcConfigGetBlanks(priv->config));
387 if (!pattern) {
388 ass_msg(library, MSGL_WARN, "%s failed", "FcFreeTypeQueryFace");
389 FT_Done_Face(face);
390 return;
393 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
394 if (!fset) {
395 ass_msg(library, MSGL_WARN, "%s failed", "FcConfigGetFonts");
396 FT_Done_Face(face);
397 return;
400 res = FcFontSetAdd(fset, pattern);
401 if (!res) {
402 ass_msg(library, MSGL_WARN, "%s failed", "FcFontSetAdd");
403 FT_Done_Face(face);
404 return;
407 FT_Done_Face(face);
412 * \brief Init fontconfig.
413 * \param library libass library object
414 * \param ftlibrary freetype library object
415 * \param family default font family
416 * \param path default font path
417 * \param fc whether fontconfig should be used
418 * \param config path to a fontconfig configuration file, or NULL
419 * \param update whether the fontconfig cache should be built/updated
420 * \return pointer to fontconfig private data
422 FCInstance *fontconfig_init(ASS_Library *library,
423 FT_Library ftlibrary, const char *family,
424 const char *path, int fc, const char *config,
425 int update)
427 int rc;
428 FCInstance *priv = calloc(1, sizeof(FCInstance));
429 const char *dir = library->fonts_dir;
430 int i;
432 if (!fc) {
433 ass_msg(library, MSGL_WARN,
434 "Fontconfig disabled, only default font will be used.");
435 goto exit;
438 priv->config = FcConfigCreate();
439 rc = FcConfigParseAndLoad(priv->config, (unsigned char *) config, FcTrue);
440 if (!rc) {
441 ass_msg(library, MSGL_WARN, "No usable fontconfig configuration "
442 "file found, using fallback.");
443 FcConfigDestroy(priv->config);
444 priv->config = FcInitLoadConfig();
445 rc++;
447 if (rc && update) {
448 FcConfigBuildFonts(priv->config);
451 if (!rc || !priv->config) {
452 ass_msg(library, MSGL_FATAL,
453 "No valid fontconfig configuration found!");
454 FcConfigDestroy(priv->config);
455 goto exit;
458 for (i = 0; i < library->num_fontdata; ++i)
459 process_fontdata(priv, library, ftlibrary, i);
461 if (dir) {
462 ass_msg(library, MSGL_V, "Updating font cache");
464 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8 *) dir);
465 if (!rc) {
466 ass_msg(library, MSGL_WARN, "%s failed", "FcConfigAppFontAddDir");
470 priv->family_default = family ? strdup(family) : NULL;
471 exit:
472 priv->path_default = path ? strdup(path) : NULL;
473 priv->index_default = 0;
475 return priv;
478 int fontconfig_update(FCInstance *priv)
480 return FcConfigBuildFonts(priv->config);
483 #else /* CONFIG_FONTCONFIG */
485 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
486 const char *family, int treat_family_as_pattern,
487 unsigned bold, unsigned italic, int *index,
488 uint32_t code)
490 *index = priv->index_default;
491 char* res = priv->path_default ? strdup(priv->path_default) : 0;
492 return res;
495 FCInstance *fontconfig_init(ASS_Library *library,
496 FT_Library ftlibrary, const char *family,
497 const char *path, int fc, const char *config,
498 int update)
500 FCInstance *priv;
502 ass_msg(library, MSGL_WARN,
503 "Fontconfig disabled, only default font will be used.");
505 priv = calloc(1, sizeof(FCInstance));
507 priv->path_default = path ? strdup(path) : 0;
508 priv->index_default = 0;
509 return priv;
512 int fontconfig_update(FCInstance *priv)
514 // Do nothing
515 return 1;
518 #endif
520 void fontconfig_done(FCInstance *priv)
523 if (priv) {
524 #ifdef CONFIG_FONTCONFIG
525 FcConfigDestroy(priv->config);
526 #endif
527 free(priv->path_default);
528 free(priv->family_default);
530 free(priv);