1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
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.
9  *
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.
17  */
18 
19 #include "config.h"
20 
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
31 
32 #include "ass_utils.h"
33 #include "ass.h"
34 #include "ass_library.h"
35 #include "ass_fontconfig.h"
36 
37 #ifdef CONFIG_FONTCONFIG
38 #include <fontconfig/fontconfig.h>
39 #include <fontconfig/fcfreetype.h>
40 #endif
41 
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;
49 };
50 
51 #ifdef CONFIG_FONTCONFIG
52 
53 /**
54  * \brief Case-insensitive match ASS/SSA font family against full name. (also
55  * known as "name for humans")
56  *
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
63  */
64 static FcFontSet *
match_fullname(ASS_Library * lib,FCInstance * priv,const char * family,unsigned bold,unsigned italic)65 match_fullname(ASS_Library *lib, FCInstance *priv, const char *family,
66                unsigned bold, unsigned italic)
67 {
68     FcFontSet *sets[2];
69     FcFontSet *result = FcFontSetCreate();
70     int nsets = 0;
71     int i, fi;
72 
73     if (!result)
74         return NULL;
75 
76     if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetSystem)))
77         nsets++;
78     if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetApplication)))
79         nsets++;
80 
81     // Run over font sets and patterns and try to match against full name
82     for (i = 0; i < nsets; i++) {
83         FcFontSet *set = sets[i];
84         for (fi = 0; fi < set->nfont; fi++) {
85             FcPattern *pat = set->fonts[fi];
86             char *fullname;
87             int pi = 0, at;
88             FcBool ol;
89             while (FcPatternGetString(pat, FC_FULLNAME, pi++,
90                    (FcChar8 **) &fullname) == FcResultMatch) {
91                 if (FcPatternGetBool(pat, FC_OUTLINE, 0, &ol) != FcResultMatch
92                     || ol != FcTrue)
93                     continue;
94                 if (FcPatternGetInteger(pat, FC_SLANT, 0, &at) != FcResultMatch
95                     || at < italic)
96                     continue;
97                 if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &at) != FcResultMatch
98                     || at < bold)
99                     continue;
100                 if (strcasecmp(fullname, family) == 0) {
101                     FcFontSetAdd(result, FcPatternDuplicate(pat));
102                     break;
103                 }
104             }
105         }
106     }
107 
108     return result;
109 }
110 
111 /**
112  * \brief Low-level font selection.
113  * \param priv private data
114  * \param family font family
115  * \param treat_family_as_pattern treat family as fontconfig pattern
116  * \param bold font weight value
117  * \param italic font slant value
118  * \param index out: font index inside a file
119  * \param code: the character that should be present in the font, can be 0
120  * \return font file path
121 */
select_font(ASS_Library * library,FCInstance * priv,const char * family,int treat_family_as_pattern,unsigned bold,unsigned italic,int * index,uint32_t code)122 static char *select_font(ASS_Library *library, FCInstance *priv,
123                           const char *family, int treat_family_as_pattern,
124                           unsigned bold, unsigned italic, int *index,
125                           uint32_t code)
126 {
127     FcBool rc;
128     FcResult result;
129     FcPattern *pat = NULL, *rpat = NULL;
130     int r_index, r_slant, r_weight;
131     FcChar8 *r_family, *r_style, *r_file, *r_fullname;
132     FcBool r_outline, r_embolden;
133     FcCharSet *r_charset;
134     FcFontSet *ffullname = NULL, *fsorted = NULL, *fset = NULL;
135     int curf;
136     char *retval = NULL;
137     int family_cnt = 0;
138 
139     *index = 0;
140 
141     if (treat_family_as_pattern)
142         pat = FcNameParse((const FcChar8 *) family);
143     else
144         pat = FcPatternCreate();
145 
146     if (!pat)
147         goto error;
148 
149     if (!treat_family_as_pattern) {
150         FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) family);
151 
152         // In SSA/ASS fonts are sometimes referenced by their "full name",
153         // which is usually a concatenation of family name and font
154         // style (ex. Ottawa Bold). Full name is available from
155         // FontConfig pattern element FC_FULLNAME, but it is never
156         // used for font matching.
157         // Therefore, I'm removing words from the end of the name one
158         // by one, and adding shortened names to the pattern. It seems
159         // that the first value (full name in this case) has
160         // precedence in matching.
161         // An alternative approach could be to reimplement FcFontSort
162         // using FC_FULLNAME instead of FC_FAMILY.
163         family_cnt = 1;
164         {
165             char *s = strdup(family);
166             if (!s)
167                 goto error;
168             char *p = s + strlen(s);
169             while (--p > s)
170                 if (*p == ' ' || *p == '-') {
171                     *p = '\0';
172                     FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) s);
173                     ++family_cnt;
174                 }
175             free(s);
176         }
177     }
178     FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
179     FcPatternAddInteger(pat, FC_SLANT, italic);
180     FcPatternAddInteger(pat, FC_WEIGHT, bold);
181 
182     FcDefaultSubstitute(pat);
183 
184     rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
185     if (!rc)
186         goto error;
187     /* Fontconfig defaults include a language setting, which it sets based on
188      * some environment variables or defaults to "en". Unset this as we don't
189      * know the real language, and because some some attached fonts lack
190      * non-ascii characters included in fontconfig's list of characters
191      * required for English support and therefore don't match the lang=en
192      * criterion.
193      */
194     FcPatternDel(pat, "lang");
195 
196     fsorted = FcFontSort(priv->config, pat, FcFalse, NULL, &result);
197     ffullname = match_fullname(library, priv, family, bold, italic);
198     if (!fsorted || !ffullname)
199         goto error;
200 
201     fset = FcFontSetCreate();
202     for (curf = 0; curf < ffullname->nfont; ++curf) {
203         FcPattern *curp = ffullname->fonts[curf];
204         FcPatternReference(curp);
205         FcFontSetAdd(fset, curp);
206     }
207     for (curf = 0; curf < fsorted->nfont; ++curf) {
208         FcPattern *curp = fsorted->fonts[curf];
209         FcPatternReference(curp);
210         FcFontSetAdd(fset, curp);
211     }
212 
213     for (curf = 0; curf < fset->nfont; ++curf) {
214         FcPattern *curp = fset->fonts[curf];
215 
216         result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
217         if (result != FcResultMatch)
218             continue;
219         if (r_outline != FcTrue)
220             continue;
221         if (!code)
222             break;
223         result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
224         if (result != FcResultMatch)
225             continue;
226         if (FcCharSetHasChar(r_charset, code))
227             break;
228     }
229 
230     if (curf >= fset->nfont)
231         goto error;
232 
233     if (!treat_family_as_pattern) {
234         // Remove all extra family names from original pattern.
235         // After this, FcFontRenderPrepare will select the most relevant family
236         // name in case there are more than one of them.
237         for (; family_cnt > 1; --family_cnt)
238             FcPatternRemove(pat, FC_FAMILY, family_cnt - 1);
239     }
240 
241     rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
242     if (!rpat)
243         goto error;
244 
245     result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
246     if (result != FcResultMatch)
247         goto error;
248     *index = r_index;
249 
250     result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
251     if (result != FcResultMatch)
252         goto error;
253     retval = strdup((const char *) r_file);
254     if (!retval)
255         goto error;
256 
257     result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
258     if (result != FcResultMatch)
259         r_family = NULL;
260 
261     result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
262     if (result != FcResultMatch)
263         r_fullname = NULL;
264 
265     if (!treat_family_as_pattern &&
266         !(r_family && strcasecmp((const char *) r_family, family) == 0) &&
267         !(r_fullname && strcasecmp((const char *) r_fullname, family) == 0)) {
268         char *fallback = (char *) (r_fullname ? r_fullname : r_family);
269         if (code) {
270             ass_msg(library, MSGL_WARN,
271                     "fontconfig: cannot find glyph U+%04X in font '%s', falling back to '%s'",
272                     (unsigned int)code, family, fallback);
273         } else {
274             ass_msg(library, MSGL_WARN,
275                     "fontconfig: cannot find font '%s', falling back to '%s'",
276                     family, fallback);
277         }
278     }
279 
280     result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
281     if (result != FcResultMatch)
282         r_style = NULL;
283 
284     result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
285     if (result != FcResultMatch)
286         r_slant = 0;
287 
288     result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
289     if (result != FcResultMatch)
290         r_weight = 0;
291 
292     result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
293     if (result != FcResultMatch)
294         r_embolden = 0;
295 
296     ass_msg(library, MSGL_V,
297            "Font info: family '%s', style '%s', fullname '%s',"
298            " slant %d, weight %d%s", (const char *) r_family,
299            (const char *) r_style, (const char *) r_fullname, r_slant,
300            r_weight, r_embolden ? ", embolden" : "");
301 
302   error:
303     if (pat)
304         FcPatternDestroy(pat);
305     if (rpat)
306         FcPatternDestroy(rpat);
307     if (fsorted)
308         FcFontSetDestroy(fsorted);
309     if (ffullname)
310         FcFontSetDestroy(ffullname);
311     if (fset)
312         FcFontSetDestroy(fset);
313     return retval;
314 }
315 
316 /**
317  * \brief Find a font. Use default family or path if necessary.
318  * \param priv_ private data
319  * \param family font family
320  * \param treat_family_as_pattern treat family as fontconfig pattern
321  * \param bold font weight value
322  * \param italic font slant value
323  * \param index out: font index inside a file
324  * \param code: the character that should be present in the font, can be 0
325  * \return font file path
326 */
fontconfig_select(ASS_Library * library,FCInstance * priv,const char * family,int treat_family_as_pattern,unsigned bold,unsigned italic,int * index,uint32_t code)327 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
328                         const char *family, int treat_family_as_pattern,
329                         unsigned bold, unsigned italic, int *index,
330                         uint32_t code)
331 {
332     char *res = 0;
333     if (!priv->config) {
334         *index = priv->index_default;
335         res = priv->path_default ? strdup(priv->path_default) : 0;
336         return res;
337     }
338     if (family && *family)
339         res =
340             select_font(library, priv, family, treat_family_as_pattern,
341                          bold, italic, index, code);
342     if (!res && priv->family_default) {
343         res =
344             select_font(library, priv, priv->family_default, 0, bold,
345                          italic, index, code);
346         if (res)
347             ass_msg(library, MSGL_WARN, "fontconfig_select: Using default "
348                     "font family: (%s, %d, %d) -> %s, %d",
349                     family, bold, italic, res, *index);
350     }
351     if (!res && priv->path_default) {
352         res = strdup(priv->path_default);
353         *index = priv->index_default;
354         if (res)
355             ass_msg(library, MSGL_WARN, "fontconfig_select: Using default font: "
356                     "(%s, %d, %d) -> %s, %d", family, bold, italic,
357                     res, *index);
358     }
359     if (!res) {
360         res = select_font(library, priv, "Arial", 0, bold, italic,
361                            index, code);
362         if (res)
363             ass_msg(library, MSGL_WARN, "fontconfig_select: Using 'Arial' "
364                     "font family: (%s, %d, %d) -> %s, %d", family, bold,
365                     italic, res, *index);
366     }
367     if (res)
368         ass_msg(library, MSGL_V,
369                 "fontconfig_select: (%s, %d, %d) -> %s, %d", family, bold,
370                 italic, res, *index);
371     return res;
372 }
373 
374 /**
375  * \brief Process memory font.
376  * \param priv private data
377  * \param library library object
378  * \param ftlibrary freetype library object
379  * \param idx index of the processed font in library->fontdata
380  *
381  * Builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
382 */
process_fontdata(FCInstance * priv,ASS_Library * library,FT_Library ftlibrary,int idx)383 static void process_fontdata(FCInstance *priv, ASS_Library *library,
384                              FT_Library ftlibrary, int idx)
385 {
386     int rc;
387     const char *name = library->fontdata[idx].name;
388     const char *data = library->fontdata[idx].data;
389     int data_size = library->fontdata[idx].size;
390 
391     FT_Face face;
392     FcPattern *pattern;
393     FcFontSet *fset;
394     FcBool res;
395     int face_index, num_faces = 1;
396 
397     for (face_index = 0; face_index < num_faces; ++face_index) {
398         ass_msg(library, MSGL_V, "Adding memory font '%s'", name);
399 
400         rc = FT_New_Memory_Face(ftlibrary, (unsigned char *) data,
401                                 data_size, face_index, &face);
402         if (rc) {
403             ass_msg(library, MSGL_WARN, "Error opening memory font: %s",
404                    name);
405             return;
406         }
407         num_faces = face->num_faces;
408 
409         pattern =
410             FcFreeTypeQueryFace(face, (unsigned char *) name, face_index,
411                                 FcConfigGetBlanks(priv->config));
412         if (!pattern) {
413             ass_msg(library, MSGL_WARN, "%s failed", "FcFreeTypeQueryFace");
414             FT_Done_Face(face);
415             return;
416         }
417 
418         fset = FcConfigGetFonts(priv->config, FcSetSystem);     // somehow it failes when asked for FcSetApplication
419         if (!fset) {
420             ass_msg(library, MSGL_WARN, "%s failed", "FcConfigGetFonts");
421             FT_Done_Face(face);
422             return;
423         }
424 
425         res = FcFontSetAdd(fset, pattern);
426         if (!res) {
427             ass_msg(library, MSGL_WARN, "%s failed", "FcFontSetAdd");
428             FT_Done_Face(face);
429             return;
430         }
431 
432         FT_Done_Face(face);
433     }
434 }
435 
436 /**
437  * \brief Init fontconfig.
438  * \param library libass library object
439  * \param ftlibrary freetype library object
440  * \param family default font family
441  * \param path default font path
442  * \param fc whether fontconfig should be used
443  * \param config path to a fontconfig configuration file, or NULL
444  * \param update whether the fontconfig cache should be built/updated
445  * \return pointer to fontconfig private data
446 */
fontconfig_init(ASS_Library * library,FT_Library ftlibrary,const char * family,const char * path,int fc,const char * config,int update)447 FCInstance *fontconfig_init(ASS_Library *library,
448                             FT_Library ftlibrary, const char *family,
449                             const char *path, int fc, const char *config,
450                             int update)
451 {
452     int rc;
453     FCInstance *priv = calloc(1, sizeof(FCInstance));
454     const char *dir = library->fonts_dir;
455     int i;
456 
457     if (!priv)
458         return NULL;
459 
460     if (!fc) {
461         ass_msg(library, MSGL_WARN,
462                "Fontconfig disabled, only default font will be used.");
463         goto exit;
464     }
465 
466     priv->config = FcConfigCreate();
467     rc = FcConfigParseAndLoad(priv->config, (unsigned char *) config, FcTrue);
468     if (!rc) {
469         ass_msg(library, MSGL_WARN, "No usable fontconfig configuration "
470                 "file found, using fallback.");
471         FcConfigDestroy(priv->config);
472         priv->config = FcInitLoadConfig();
473         rc++;
474     }
475     if (rc && update) {
476         FcConfigBuildFonts(priv->config);
477     }
478 
479     if (!rc || !priv->config) {
480         ass_msg(library, MSGL_FATAL,
481                 "No valid fontconfig configuration found!");
482         FcConfigDestroy(priv->config);
483         goto exit;
484     }
485 
486     for (i = 0; i < library->num_fontdata; ++i)
487         process_fontdata(priv, library, ftlibrary, i);
488 
489     if (dir) {
490         ass_msg(library, MSGL_V, "Updating font cache");
491 
492         rc = FcConfigAppFontAddDir(priv->config, (const FcChar8 *) dir);
493         if (!rc) {
494             ass_msg(library, MSGL_WARN, "%s failed", "FcConfigAppFontAddDir");
495         }
496     }
497 
498     priv->family_default = family ? strdup(family) : NULL;
499 exit:
500     priv->path_default = path ? strdup(path) : NULL;
501     priv->index_default = 0;
502 
503     return priv;
504 }
505 
fontconfig_update(FCInstance * priv)506 int fontconfig_update(FCInstance *priv)
507 {
508         return FcConfigBuildFonts(priv->config);
509 }
510 
511 #else                           /* CONFIG_FONTCONFIG */
512 
fontconfig_select(ASS_Library * library,FCInstance * priv,const char * family,int treat_family_as_pattern,unsigned bold,unsigned italic,int * index,uint32_t code)513 char *fontconfig_select(ASS_Library *library, FCInstance *priv,
514                         const char *family, int treat_family_as_pattern,
515                         unsigned bold, unsigned italic, int *index,
516                         uint32_t code)
517 {
518     *index = priv->index_default;
519     char* res = priv->path_default ? strdup(priv->path_default) : 0;
520     return res;
521 }
522 
fontconfig_init(ASS_Library * library,FT_Library ftlibrary,const char * family,const char * path,int fc,const char * config,int update)523 FCInstance *fontconfig_init(ASS_Library *library,
524                             FT_Library ftlibrary, const char *family,
525                             const char *path, int fc, const char *config,
526                             int update)
527 {
528     FCInstance *priv;
529 
530     ass_msg(library, MSGL_WARN,
531         "Fontconfig disabled, only default font will be used.");
532 
533     priv = calloc(1, sizeof(FCInstance));
534     if (!priv)
535         return NULL;
536 
537     priv->path_default = path ? strdup(path) : 0;
538     priv->index_default = 0;
539     return priv;
540 }
541 
fontconfig_update(FCInstance * priv)542 int fontconfig_update(FCInstance *priv)
543 {
544     // Do nothing
545     return 1;
546 }
547 
548 #endif
549 
fontconfig_done(FCInstance * priv)550 void fontconfig_done(FCInstance *priv)
551 {
552 
553     if (priv) {
554 #ifdef CONFIG_FONTCONFIG
555         if (priv->config)
556             FcConfigDestroy(priv->config);
557 #endif
558         free(priv->path_default);
559         free(priv->family_default);
560     }
561     free(priv);
562 }
563