reimplement system fonts retrieval for Linux using fontconfig

This commit is contained in:
ohyzha
2025-01-18 16:56:00 +02:00
parent 5a5bac8479
commit 7670100d81
3 changed files with 142 additions and 8 deletions

View File

@@ -5,17 +5,56 @@
*/
#include "Host/SystemFontResolver.hpp"
#include <filesystem>
#include "Base/Logger.hpp"
#include <fontconfig/fontconfig.h>
namespace OpenVulkano
{
std::string SystemFontResolver::GetSystemFontPath(const std::string& fontName)
{
// fontName -> fontPath
static std::map<std::string, std::string> fontFilesMapping = ReadSystemFonts();
auto it = fontFilesMapping.find(fontName);
if (it != fontFilesMapping.end())
{
return it->second;
}
return "";
}
std::map<std::string, std::string> SystemFontResolver::ReadSystemFonts()
{
return {};
FcConfig* config = FcInitLoadConfigAndFonts();
FcPattern* pat = FcPatternCreate();
FcObjectSet* os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, NULL);
FcFontSet* fs = FcFontList(config, pat, os);
if (!fs)
{
Logger::DATA->warn("Could not get system fonts");
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
return {};
}
std::map<std::string, std::string> fontFilesMapping;
for (int i = 0; i < fs->nfont; ++i)
{
FcPattern* font = fs->fonts[i];
FcChar8* file, *style, *family;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch &&
FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
{
fontFilesMapping[std::string(reinterpret_cast<char*>(family)) + " " +
std::string(reinterpret_cast<char*>(style))]
= std::string(reinterpret_cast<char*>(file));
}
}
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
FcConfigDestroy(config);
return fontFilesMapping;
}
}