65 lines
2.3 KiB
C++
65 lines
2.3 KiB
C++
/*
|
|
* 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 "Base/Utils.hpp"
|
|
#include <fontconfig/fontconfig.h>
|
|
#include <memory>
|
|
|
|
namespace OpenVulkano
|
|
{
|
|
namespace
|
|
{
|
|
const std::filesystem::path FALLBACK_PATH;
|
|
}
|
|
|
|
const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
|
|
{
|
|
// fontName -> fontPath
|
|
static std::map<std::string, std::filesystem::path> fontFilesMapping = ReadSystemFonts();
|
|
std::string fontNameLower = Utils::ToLower(fontName);
|
|
auto it = fontFilesMapping.find(fontNameLower);
|
|
if (it != fontFilesMapping.end()) return it->second;
|
|
it = fontFilesMapping.find(fontNameLower + " regular");
|
|
if (it != fontFilesMapping.end()) return it->second;
|
|
return FALLBACK_PATH;
|
|
}
|
|
|
|
std::map<std::string, std::filesystem::path> SystemFontResolver::ReadSystemFonts()
|
|
{
|
|
std::unique_ptr<FcConfig, decltype(&FcConfigDestroy)> config(FcInitLoadConfigAndFonts(), &FcConfigDestroy);
|
|
std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pat(FcPatternCreate(), &FcPatternDestroy);
|
|
std::unique_ptr<FcObjectSet, decltype(&FcObjectSetDestroy)> os(FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, NULL), &FcObjectSetDestroy);
|
|
std::unique_ptr<FcFontSet, decltype(&FcFontSetDestroy)> fs(FcFontList(config.get(), pat.get(), os.get()), &FcFontSetDestroy);
|
|
|
|
if (!fs)
|
|
{
|
|
Logger::DATA->warn("Could not get system fonts");
|
|
return {};
|
|
}
|
|
|
|
std::map<std::string, std::filesystem::path> fontFilesMapping;
|
|
for (int i = 0; i < fs->nfont; ++i)
|
|
{
|
|
FcPattern* font = fs->fonts[i];
|
|
FcChar8* file;
|
|
FcChar8* style;
|
|
FcChar8* family;
|
|
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
|
|
FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch &&
|
|
FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
|
|
{
|
|
std::string fontFull = std::string(reinterpret_cast<char*>(family)) + " " +
|
|
std::string(reinterpret_cast<char*>(style));
|
|
Utils::ToLower(fontFull);
|
|
fontFilesMapping[std::move(fontFull)] = std::filesystem::path(reinterpret_cast<char*>(file));
|
|
}
|
|
}
|
|
return fontFilesMapping;
|
|
}
|
|
}
|