33 auto separator = font_name.find(
',');
34 if (separator == std::string_view::npos)
return { std::string(font_name), std::string() };
36 auto begin = font_name.find_first_not_of(
"\t ", separator + 1);
37 if (begin == std::string_view::npos)
return { std::string(font_name.substr(0, separator)), std::string() };
39 return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
44 FT_Error err = FT_Err_Cannot_Open_Resource;
47 ShowInfo(
"Unable to load font configuration");
51 auto fc_instance = FcConfigReference(
nullptr);
52 assert(fc_instance !=
nullptr);
58 FcPattern *pat = FcPatternCreate();
59 if (!font_family.empty()) FcPatternAddString(pat, FC_FAMILY,
reinterpret_cast<const FcChar8 *
>(font_family.c_str()));
60 if (!font_style.empty()) FcPatternAddString(pat, FC_STYLE,
reinterpret_cast<const FcChar8 *
>(font_style.c_str()));
61 FcConfigSubstitute(
nullptr, pat, FcMatchPattern);
62 FcDefaultSubstitute(pat);
63 FcFontSet *fs = FcFontSetCreate();
65 FcPattern *match = FcFontMatch(
nullptr, pat, &result);
67 if (fs !=
nullptr && match !=
nullptr) {
72 FcFontSetAdd(fs, match);
74 for (
int i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
76 if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
77 FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
78 FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch &&
79 FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) {
88 err = FT_New_Face(_library, (
char *)file, index, face);
94 FcPatternDestroy(pat);
96 FcConfigDestroy(fc_instance);
105 if (!FcInit())
return ret;
107 auto fc_instance = FcConfigReference(
nullptr);
108 assert(fc_instance !=
nullptr);
113 std::string lang = fmt::format(
":lang={}", language_isocode.substr(0, language_isocode.find(
'_')));
116 FcPattern *pat = FcNameParse((
const FcChar8 *)lang.c_str());
118 FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_INDEX, FC_SPACING, FC_SLANT, FC_WEIGHT,
nullptr);
120 FcFontSet *fs = FcFontList(
nullptr, pat, os);
123 FcObjectSetDestroy(os);
124 FcPatternDestroy(pat);
127 int best_weight = -1;
128 const char *best_font =
nullptr;
131 for (
int i = 0; i < fs->nfont; i++) {
132 FcPattern *font = fs->fonts[i];
134 FcChar8 *file =
nullptr;
135 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
136 if (res != FcResultMatch || file ==
nullptr) {
142 FcPatternGetInteger(font, FC_SPACING, 0, &value);
143 if (callback->
Monospace() != (value == FC_MONO) && value != FC_DUAL)
continue;
146 FcPatternGetInteger(font, FC_SLANT, 0, &value);
147 if (value != 0)
continue;
150 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
151 if (value <= best_weight)
continue;
155 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
156 if (res != FcResultMatch)
continue;
161 Debug(fontcache, 1,
"Font \"{}\" misses{} glyphs", (
char *)file, missing ?
"" :
" no");
165 best_font = (
const char *)file;
170 if (best_font !=
nullptr) {
177 FcFontSetDestroy(fs);
180 FcConfigDestroy(fc_instance);
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int, MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.