10#include "../../stdafx.h"
12#include "../../misc/autorelease.hpp"
13#include "../../debug.h"
14#include "../../fontcache.h"
15#include "../../string_func.h"
16#include "../../strings_func.h"
19#include <fontconfig/fontconfig.h>
23#include "../../safeguards.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;
123 if (err != FT_Err_Ok) {
124 ShowInfo(
"Unable to find '{}' font", font_name);
138 static constexpr int PREFERRED_WEIGHT_MIN = FC_WEIGHT_NORMAL;
139 static constexpr int PREFERRED_WEIGHT_MAX = FC_WEIGHT_MEDIUM;
141 if (weight < PREFERRED_WEIGHT_MIN)
return std::abs(weight - PREFERRED_WEIGHT_MIN);
142 if (weight > PREFERRED_WEIGHT_MAX)
return weight - PREFERRED_WEIGHT_MAX;
150 if (!FcInit())
return ret;
153 assert(fc_instance !=
nullptr);
161 std::string lang = language_isocode.empty() ?
"" : fmt::format(
":lang={}", language_isocode.substr(0, language_isocode.find(
'_')));
170 if (fs ==
nullptr)
return ret;
172 int best_weight = -1;
173 std::string best_font;
176 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
177 FcChar8 *file =
nullptr;
178 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
179 if (res != FcResultMatch || file ==
nullptr)
continue;
183 FcPatternGetInteger(font, FC_SPACING, 0, &value);
184 if (fontsizes.
Test(
FS_MONO) != (value == FC_MONO) && value != FC_DUAL)
continue;
187 FcPatternGetInteger(font, FC_SLANT, 0, &value);
188 if (value != 0)
continue;
191 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
193 if (best_weight != -1 && weight > best_weight)
continue;
195 size_t matching_chars = 0;
197 FcPatternGetCharSet(font, FC_CHARSET, 0, &charset);
198 for (
const char32_t &c : chars) {
199 if (FcCharSetHasChar(charset, c)) ++matching_chars;
202 if (matching_chars < chars.size()) {
203 Debug(fontcache, 1,
"Font \"{}\" misses {} glyphs",
reinterpret_cast<const char *
>(file), chars.size() - matching_chars);
209 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
210 if (res != FcResultMatch)
continue;
212 best_weight = weight;
217 if (best_font.empty())
return false;
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 AddFallbackWithHandle(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
Add a fallback font, with OS-specific handle.
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
A searcher for missing glyphs.
virtual std::set< char32_t > GetRequiredGlyphs(FontSizes fontsizes)=0
Get set of glyphs required for the current language.
#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.
Functions related to detecting/finding the right font.
@ FS_MONO
Index of the monospaced font in the font tables.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.