10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "../../fontdetection.h"
13 #include "../../string_func.h"
14 #include "../../strings_func.h"
16 #include <fontconfig/fontconfig.h>
23 #include FT_FREETYPE_H
25 extern FT_Library _library;
30 FT_Error err = FT_Err_Cannot_Open_Resource;
33 ShowInfoF(
"Unable to load font configuration");
43 font_family =
stredup(font_name);
44 font_style = strchr(font_family,
',');
45 if (font_style !=
nullptr) {
48 while (*font_style ==
' ' || *font_style ==
'\t') font_style++;
52 pat = FcNameParse((FcChar8 *)font_family);
53 if (font_style !=
nullptr) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style);
54 FcConfigSubstitute(0, pat, FcMatchPattern);
55 FcDefaultSubstitute(pat);
56 fs = FcFontSetCreate();
57 match = FcFontMatch(0, pat, &result);
59 if (fs !=
nullptr && match !=
nullptr) {
64 FcFontSetAdd(fs, match);
66 for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
68 if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
69 FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
70 FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch) {
73 if (font_style !=
nullptr && strcasecmp(font_style, (
char *)style) != 0)
continue;
78 if (strcasecmp(font_family, (
char *)family) == 0) {
79 err = FT_New_Face(_library, (
char *)file, 0, face);
86 FcPatternDestroy(pat);
99 if (!FcInit())
return false;
108 char *split = strchr(lang,
'_');
109 if (split !=
nullptr) *split =
'\0';
112 FcPattern *pat = FcNameParse((FcChar8 *)lang);
114 FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT,
nullptr);
116 FcFontSet *fs = FcFontList(
nullptr, pat, os);
119 FcObjectSetDestroy(os);
120 FcPatternDestroy(pat);
123 int best_weight = -1;
124 const char *best_font =
nullptr;
126 for (
int i = 0; i < fs->nfont; i++) {
127 FcPattern *font = fs->fonts[i];
129 FcChar8 *file =
nullptr;
130 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
131 if (res != FcResultMatch || file ==
nullptr) {
137 FcPatternGetInteger(font, FC_SPACING, 0, &value);
138 if (callback->
Monospace() != (value == FC_MONO) && value != FC_DUAL)
continue;
141 FcPatternGetInteger(font, FC_SLANT, 0, &value);
142 if (value != 0)
continue;
145 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
146 if (value <= best_weight)
continue;
151 DEBUG(freetype, 1,
"Font \"%s\" misses%s glyphs", file, missing ?
"" :
" no");
155 best_font = (
const char *)file;
159 if (best_font !=
nullptr) {
166 FcFontSetDestroy(fs);