10#include "../../stdafx.h"
12#include "../../misc/autorelease.hpp"
13#include "../../debug.h"
14#include "../../fontdetection.h"
15#include "../../string_func.h"
16#include "../../strings_func.h"
18#include <fontconfig/fontconfig.h>
20#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)) };
66 FT_Error err = FT_Err_Cannot_Open_Resource;
69 ShowInfo(
"Unable to load font configuration");
74 assert(fc_instance !=
nullptr);
81 if (!font_family.empty()) FcPatternAddString(pat.get(), FC_FAMILY,
ToFcString(font_family));
82 if (!font_style.empty()) FcPatternAddString(pat.get(), FC_STYLE,
ToFcString(font_style));
83 FcConfigSubstitute(
nullptr, pat.get(), FcMatchPattern);
84 FcDefaultSubstitute(pat.get());
87 FcPattern *match = FcFontMatch(
nullptr, pat.get(), &result);
89 if (fs ==
nullptr || match ==
nullptr)
return err;
91 FcFontSetAdd(fs.get(), match);
93 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
100 if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch)
continue;
101 if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch)
continue;
102 if (FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch)
continue;
103 if (FcPatternGetInteger(font, FC_INDEX, 0, &index) != FcResultMatch)
continue;
112 err = FT_New_Face(_ft_library,
FromFcString(file), index, face);
113 if (err == FT_Err_Ok)
return err;
124 if (!FcInit())
return ret;
127 assert(fc_instance !=
nullptr);
132 std::string lang = fmt::format(
":lang={}", language_isocode.substr(0, language_isocode.find(
'_')));
141 if (fs ==
nullptr)
return ret;
143 int best_weight = -1;
144 const char *best_font =
nullptr;
147 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
148 FcChar8 *file =
nullptr;
149 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
150 if (res != FcResultMatch || file ==
nullptr)
continue;
154 FcPatternGetInteger(font, FC_SPACING, 0, &value);
155 if (callback->
Monospace() != (value == FC_MONO) && value != FC_DUAL)
continue;
158 FcPatternGetInteger(font, FC_SLANT, 0, &value);
159 if (value != 0)
continue;
162 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
163 if (value <= best_weight)
continue;
167 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
168 if (res != FcResultMatch)
continue;
173 Debug(fontcache, 1,
"Font \"{}\" misses{} glyphs",
FromFcString(file), missing ?
"" :
" no");
182 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.
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 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.
bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
static std::tuple< std::string, std::string > SplitFontFamilyAndStyle(std::string_view font_name)
Split the font name into the font family and style.
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
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.