Skip to content

Commit

Permalink
Fontconfig windows workaround v2 (#1809)
Browse files Browse the repository at this point in the history
* Custom xml for windows since mingw builds won't work with the fontconfig
special strings.
---------

Co-authored-by: sprunk <[email protected]>
  • Loading branch information
saurtron and sprunk authored Dec 11, 2024
1 parent f83afe1 commit 05c5724
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions rts/Rendering/Fonts/CFontTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,28 @@ class FtLibraryHandler {
// and system fonts included. also linux actually has system config files that can be used by fontconfig.

#ifdef _WIN32
// fontconfig will resolve the special keys here.
static constexpr const char* configFmt = R"(<fontconfig><dir>WINDOWSFONTDIR</dir><cachedir>fontcache</cachedir><cachedir>LOCAL_APPDATA_FONTCONFIG_CACHE</cachedir></fontconfig>)";
static constexpr auto winFontPath = "%WINDIR%\\fonts";
const int neededSize = ExpandEnvironmentStrings(winFontPath, nullptr, 0);
std::vector <char> osFontsDir (neededSize);
ExpandEnvironmentStrings(winFontPath, osFontsDir.data(), osFontsDir.size());

static constexpr const char* configFmt = R"(<fontconfig><dir>%s</dir><cachedir>fontcache</cachedir></fontconfig>)";
const std::string configFmtVar = fmt::sprintf(configFmt, osFontsDir.data());
#else
static constexpr const char* configFmt = R"(<fontconfig><cachedir>fontcache</cachedir></fontconfig>)";
const std::string configFmtVar = R"(<fontconfig><cachedir>fontcache</cachedir></fontconfig>)";
#endif

#ifdef _WIN32
// Explicitly set the config with xml for windows.
res = FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmt), FcTrue);
res = FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmtVar.c_str()), FcTrue);
#else
// Load system configuration (passing 0 here so fc will use the default os config file if possible).
res = FcConfigParseAndLoad(config, 0, true);
#endif
if (res) {
#ifndef _WIN32
// add local cache after system config for linux
FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmt), FcTrue);
FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmtVar.c_str()), FcTrue);
#endif

LOG_MSG("[%s] Using Fontconfig light init", false, __func__);
Expand All @@ -191,7 +196,7 @@ class FtLibraryHandler {
config = fcConfig;

// add our cache at the back of the new config.
FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmt), FcTrue);
FcConfigParseAndLoadFromMemory(config, reinterpret_cast<const FcChar8*>(configFmtVar.c_str()), FcTrue);
} else {
LOG_MSG("%s config and fonts. No system fallbacks will be available", false, errprefix.c_str());
}
Expand Down

0 comments on commit 05c5724

Please sign in to comment.