19#include <fontconfig/fontconfig.h>
25extern FT_Library _ft_library;
34 return reinterpret_cast<const FcChar8 *
>(str.c_str());
44 return reinterpret_cast<const char *
>(str);
55 auto separator = font_name.find(
',');
56 if (separator == std::string_view::npos)
return { std::string(font_name), std::string() };
58 auto begin = font_name.find_first_not_of(
"\t ", separator + 1);
59 if (begin == std::string_view::npos)
return { std::string(font_name.substr(0, separator)), std::string() };
61 return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
72 FT_Error err = FT_Err_Cannot_Open_Resource;
75 ShowInfo(
"Unable to load font configuration");
80 assert(fc_instance !=
nullptr);
87 if (!font_family.empty()) FcPatternAddString(pat.get(), FC_FAMILY,
ToFcString(font_family));
88 if (!font_style.empty()) FcPatternAddString(pat.get(), FC_STYLE,
ToFcString(font_style));
89 FcConfigSubstitute(
nullptr, pat.get(), FcMatchPattern);
90 FcDefaultSubstitute(pat.get());
93 FcPattern *match = FcFontMatch(
nullptr, pat.get(), &result);
95 if (fs ==
nullptr || match ==
nullptr)
return err;
97 FcFontSetAdd(fs.get(), match);
99 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
106 if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch)
continue;
107 if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch)
continue;
108 if (FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch)
continue;
109 if (FcPatternGetInteger(font, FC_INDEX, 0, &index) != FcResultMatch)
continue;
118 err = FT_New_Face(_ft_library,
FromFcString(file), index, face);
119 if (err == FT_Err_Ok)
return err;
134 static constexpr int PREFERRED_WEIGHT_MIN = FC_WEIGHT_NORMAL;
135 static constexpr int PREFERRED_WEIGHT_MAX = FC_WEIGHT_MEDIUM;
137 if (weight < PREFERRED_WEIGHT_MIN)
return std::abs(weight - PREFERRED_WEIGHT_MIN);
138 if (weight > PREFERRED_WEIGHT_MAX)
return weight - PREFERRED_WEIGHT_MAX;
152 if (!FcInit())
return ret;
155 assert(fc_instance !=
nullptr);
160 std::string lang = fmt::format(
":lang={}", language_isocode.substr(0, language_isocode.find(
'_')));
169 if (fs ==
nullptr)
return ret;
171 int best_weight = -1;
172 const char *best_font =
nullptr;
175 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
176 FcChar8 *file =
nullptr;
177 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
178 if (res != FcResultMatch || file ==
nullptr)
continue;
182 FcPatternGetInteger(font, FC_SPACING, 0, &value);
186 FcPatternGetInteger(font, FC_SLANT, 0, &value);
187 if (value != 0)
continue;
190 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
192 if (best_weight != -1 && weight > best_weight)
continue;
196 size_t matching_chars = 0;
198 FcPatternGetCharSet(font, FC_CHARSET, 0, &charset);
200 if (FcCharSetHasChar(charset, c)) ++matching_chars;
203 if (matching_chars < callback->missing_glyphs.size()) {
210 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
211 if (res != FcResultMatch)
continue;
213 best_weight = weight;
218 if (best_font ==
nullptr)
return false;
Helper for std::unique_ptr to use an arbitrary function as the deleter.
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
static void AddFallback(FontSizes fontsizes, std::string_view name, const std::any &os_handle={})
Add a fallback font, with optional OS-specific handle.
A searcher for missing glyphs.
std::set< char32_t > missing_glyphs
Glyphs to search for.
FontSizes missing_fontsizes
Font sizes to actually search for.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
Load a freetype font face with the given font name.
static int GetPreferredWeightDistance(int weight)
Get distance between font weight and preferred font weights.
static const char * FromFcString(const FcChar8 *str)
Get a C-style string from a FontConfig-style string.
static const FcChar8 * ToFcString(const std::string &str)
Get a FontConfig-style string from a std::string.
static std::tuple< std::string, std::string > SplitFontFamilyAndStyle(std::string_view font_name)
Split the font name into the font family and style.
bool FontConfigFindFallbackFont(const std::string &language_isocode, MissingGlyphSearcher *callback)
Use FontConfig to find a fallback font.
Functions related to detecting/finding the right font.
Functions to read fonts from files and cache them.
@ Monospace
Index of the monospaced font in the font tables.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Functions related to low-level strings.
Functions related to OTTD's strings.