/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "Host/SystemFontResolver.hpp" #include "Base/Logger.hpp" #include namespace OpenVulkano { std::string SystemFontResolver::GetSystemFontPath(const std::string& fontName) { // fontName -> fontPath static std::map fontFilesMapping = ReadSystemFonts(); auto it = fontFilesMapping.find(fontName); if (it != fontFilesMapping.end()) { return it->second; } return ""; } std::map SystemFontResolver::ReadSystemFonts() { 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 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(family)) + " " + std::string(reinterpret_cast(style))] = std::string(reinterpret_cast(file)); } } FcFontSetDestroy(fs); FcObjectSetDestroy(os); FcPatternDestroy(pat); FcConfigDestroy(config); return fontFilesMapping; } }