1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-23 14:54:30 +01:00

Upgrade platform_get_font_path()

This commit is contained in:
Gymnasiast
2022-01-10 13:41:12 +01:00
parent 310ad1e400
commit f917fffe34
11 changed files with 117 additions and 132 deletions

View File

@@ -24,6 +24,10 @@
// for PATH_MAX
# include <linux/limits.h>
# endif // __linux__
# ifndef NO_TTF
# include <fontconfig/fontconfig.h>
# endif // NO_TTF
# include "../OpenRCT2.h"
# include "../core/Path.hpp"
# include "../localisation/Language.h"
@@ -310,6 +314,63 @@ namespace Platform
return "";
}
std::string GetFontPath(const TTFFontDescriptor& font)
{
log_verbose("Looking for font %s with FontConfig.", font.font_name);
FcConfig* config = FcInitLoadConfigAndFonts();
if (!config)
{
log_error("Failed to initialize FontConfig library");
FcFini();
return "";
}
FcPattern* pat = FcNameParse(reinterpret_cast<const FcChar8*>(font.font_name));
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
std::string path = "";
FcResult result = FcResultNoMatch;
FcPattern* match = FcFontMatch(config, pat, &result);
if (match)
{
bool is_substitute = false;
// FontConfig implicitly falls back to any default font it is configured to handle.
// In our implementation, this cannot account for supported character sets, leading
// to unrendered characters (tofu) when trying to render e.g. CJK characters using a
// Western (sans-)serif font. We therefore ignore substitutions FontConfig provides,
// and instead rely on exact matches on the fonts predefined for each font family.
FcChar8* matched_font_face = nullptr;
if (FcPatternGetString(match, FC_FULLNAME, 0, &matched_font_face) == FcResultMatch
&& strcmp(font.font_name, reinterpret_cast<const char*>(matched_font_face)) != 0)
{
log_verbose("FontConfig provided substitute font %s -- disregarding.", matched_font_face);
is_substitute = true;
}
FcChar8* filename = nullptr;
if (!is_substitute && FcPatternGetString(match, FC_FILE, 0, &filename) == FcResultMatch)
{
path = reinterpret_cast<utf8*>(filename);
log_verbose("FontConfig provided font %s", filename);
}
FcPatternDestroy(match);
}
else
{
log_warning("Failed to find required font.");
}
FcPatternDestroy(pat);
FcConfigDestroy(config);
FcFini();
return path;
}
} // namespace Platform
#endif