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;
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;
146 if (!FcInit())
return ret;
149 assert(fc_instance !=
nullptr);
154 std::string lang = fmt::format(
":lang={}", language_isocode.substr(0, language_isocode.find(
'_')));
163 if (fs ==
nullptr)
return ret;
165 int best_weight = -1;
166 const char *best_font =
nullptr;
169 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
170 FcChar8 *file =
nullptr;
171 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
172 if (res != FcResultMatch || file ==
nullptr)
continue;
176 FcPatternGetInteger(font, FC_SPACING, 0, &value);
177 if (callback->
Monospace() != (value == FC_MONO) && value != FC_DUAL)
continue;
180 FcPatternGetInteger(font, FC_SLANT, 0, &value);
181 if (value != 0)
continue;
184 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
186 if (best_weight != -1 && weight > best_weight)
continue;
190 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
191 if (res != FcResultMatch)
continue;
196 Debug(fontcache, 1,
"Font \"{}\" misses{} glyphs",
FromFcString(file), missing ?
"" :
" no");
199 best_weight = weight;
205 if (best_font ==
nullptr)
return false;
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
A searcher for missing glyphs.
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data=nullptr)=0
Set the right font names.
virtual bool Monospace()=0
Whether to search for a monospace font or not.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
fluid_settings_t * settings
FluidSynth settings handle.
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.
constexpr FontSizes FONTSIZES_REQUIRED
Mask of font sizes required to be present.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Settings for the four different fonts.