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.
26 #include <sys/types.h>
30 #include FT_FREETYPE_H
32 #include "ass_utils.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>
43 #ifdef CONFIG_FONTCONFIG
51 #ifdef CONFIG_FONTCONFIG
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
65 match_fullname(ASS_Library
*lib
, FCInstance
*priv
, const char *family
,
66 unsigned bold
, unsigned italic
)
69 FcFontSet
*result
= FcFontSetCreate();
73 if ((sets
[nsets
] = FcConfigGetFonts(priv
->config
, FcSetSystem
)))
75 if ((sets
[nsets
] = FcConfigGetFonts(priv
->config
, FcSetApplication
)))
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
];
86 while (FcPatternGetString(pat
, FC_FULLNAME
, pi
++,
87 (FcChar8
**) &fullname
) == FcResultMatch
) {
88 if (FcPatternGetBool(pat
, FC_OUTLINE
, 0, &ol
) != FcResultMatch
91 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &at
) != FcResultMatch
94 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &at
) != FcResultMatch
97 if (strcasecmp(fullname
, family
) == 0) {
98 FcFontSetAdd(result
, FcPatternDuplicate(pat
));
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
,
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
;
138 if (treat_family_as_pattern
)
139 pat
= FcNameParse((const FcChar8
*) family
);
141 pat
= FcPatternCreate();
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.
162 char *s
= strdup(family
);
163 char *p
= s
+ strlen(s
);
165 if (*p
== ' ' || *p
== '-') {
167 FcPatternAddString(pat
, FC_FAMILY
, (const FcChar8
*) 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
);
183 fsorted
= FcFontSort(priv
->config
, pat
, FcTrue
, NULL
, &result
);
184 ffullname
= match_fullname(library
, priv
, family
, bold
, italic
);
185 if (!fsorted
|| !ffullname
)
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
)
206 if (r_outline
!= FcTrue
)
210 result
= FcPatternGetCharSet(curp
, FC_CHARSET
, 0, &r_charset
);
211 if (result
!= FcResultMatch
)
213 if (FcCharSetHasChar(r_charset
, code
))
217 if (curf
>= fset
->nfont
)
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
]);
232 result
= FcPatternGetInteger(rpat
, FC_INDEX
, 0, &r_index
);
233 if (result
!= FcResultMatch
)
237 result
= FcPatternGetString(rpat
, FC_FILE
, 0, &r_file
);
238 if (result
!= FcResultMatch
)
240 retval
= strdup((const char *) r_file
);
242 result
= FcPatternGetString(rpat
, FC_FAMILY
, 0, &r_family
);
243 if (result
!= FcResultMatch
)
246 result
= FcPatternGetString(rpat
, FC_FULLNAME
, 0, &r_fullname
);
247 if (result
!= FcResultMatch
)
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: "
256 (const char *) (r_fullname
? r_fullname
: r_family
), family
);
258 result
= FcPatternGetString(rpat
, FC_STYLE
, 0, &r_style
);
259 if (result
!= FcResultMatch
)
262 result
= FcPatternGetInteger(rpat
, FC_SLANT
, 0, &r_slant
);
263 if (result
!= FcResultMatch
)
266 result
= FcPatternGetInteger(rpat
, FC_WEIGHT
, 0, &r_weight
);
267 if (result
!= FcResultMatch
)
270 result
= FcPatternGetBool(rpat
, FC_EMBOLDEN
, 0, &r_embolden
);
271 if (result
!= FcResultMatch
)
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" : "");
282 FcPatternDestroy(pat
);
284 FcPatternDestroy(rpat
);
286 FcFontSetDestroy(fsorted
);
288 FcFontSetDestroy(ffullname
);
290 FcFontSetDestroy(fset
);
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
,
312 *index
= priv
->index_default
;
313 res
= priv
->path_default
? strdup(priv
->path_default
) : 0;
316 if (family
&& *family
)
318 select_font(library
, priv
, family
, treat_family_as_pattern
,
319 bold
, italic
, index
, code
);
320 if (!res
&& priv
->family_default
) {
322 select_font(library
, priv
, priv
->family_default
, 0, bold
,
323 italic
, index
, code
);
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
,
337 res
= select_font(library
, priv
, "Arial", 0, bold
, italic
,
340 ass_msg(library
, MSGL_WARN
, "fontconfig_select: Using 'Arial' "
341 "font family: (%s, %d, %d) -> %s, %d", family
, bold
,
342 italic
, res
, *index
);
345 ass_msg(library
, MSGL_V
,
346 "fontconfig_select: (%s, %d, %d) -> %s, %d", family
, bold
,
347 italic
, res
, *index
);
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
)
364 const char *name
= library
->fontdata
[idx
].name
;
365 const char *data
= library
->fontdata
[idx
].data
;
366 int data_size
= library
->fontdata
[idx
].size
;
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
);
378 ass_msg(library
, MSGL_WARN
, "Error opening memory font: %s",
382 num_faces
= face
->num_faces
;
385 FcFreeTypeQueryFace(face
, (unsigned char *) name
, face_index
,
386 FcConfigGetBlanks(priv
->config
));
388 ass_msg(library
, MSGL_WARN
, "%s failed", "FcFreeTypeQueryFace");
393 fset
= FcConfigGetFonts(priv
->config
, FcSetSystem
); // somehow it failes when asked for FcSetApplication
395 ass_msg(library
, MSGL_WARN
, "%s failed", "FcConfigGetFonts");
400 res
= FcFontSetAdd(fset
, pattern
);
402 ass_msg(library
, MSGL_WARN
, "%s failed", "FcFontSetAdd");
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
,
428 FCInstance
*priv
= calloc(1, sizeof(FCInstance
));
429 const char *dir
= library
->fonts_dir
;
433 ass_msg(library
, MSGL_WARN
,
434 "Fontconfig disabled, only default font will be used.");
438 priv
->config
= FcConfigCreate();
439 rc
= FcConfigParseAndLoad(priv
->config
, (unsigned char *) config
, FcTrue
);
441 ass_msg(library
, MSGL_WARN
, "No usable fontconfig configuration "
442 "file found, using fallback.");
443 FcConfigDestroy(priv
->config
);
444 priv
->config
= FcInitLoadConfig();
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
);
458 for (i
= 0; i
< library
->num_fontdata
; ++i
)
459 process_fontdata(priv
, library
, ftlibrary
, i
);
462 ass_msg(library
, MSGL_V
, "Updating font cache");
464 rc
= FcConfigAppFontAddDir(priv
->config
, (const FcChar8
*) dir
);
466 ass_msg(library
, MSGL_WARN
, "%s failed", "FcConfigAppFontAddDir");
470 priv
->family_default
= family
? strdup(family
) : NULL
;
472 priv
->path_default
= path
? strdup(path
) : NULL
;
473 priv
->index_default
= 0;
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
,
490 *index
= priv
->index_default
;
491 char* res
= priv
->path_default
? strdup(priv
->path_default
) : 0;
495 FCInstance
*fontconfig_init(ASS_Library
*library
,
496 FT_Library ftlibrary
, const char *family
,
497 const char *path
, int fc
, const char *config
,
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;
512 int fontconfig_update(FCInstance
*priv
)
520 void fontconfig_done(FCInstance
*priv
)
524 #ifdef CONFIG_FONTCONFIG
526 FcConfigDestroy(priv
->config
);
528 free(priv
->path_default
);
529 free(priv
->family_default
);