Linux side code refactoring

This commit is contained in:
ohyzha
2025-01-20 13:59:46 +02:00
parent 635860b0c2
commit 497ec7d09b
2 changed files with 24 additions and 22 deletions

View File

@@ -6,34 +6,31 @@
#include "Host/SystemFontResolver.hpp"
#include "Base/Logger.hpp"
#include "Base/Utils.hpp"
#include <fontconfig/fontconfig.h>
#include <memory>
namespace OpenVulkano
{
std::string SystemFontResolver::GetSystemFontPath(const std::string& fontName)
const 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 "";
static std::string fallbackString;
auto it = fontFilesMapping.find(Utils::ToLower(fontName));
return it == fontFilesMapping.end() ? fallbackString : it->second;
}
std::map<std::string, std::string> 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);
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");
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
return {};
}
@@ -41,20 +38,19 @@ namespace OpenVulkano
for (int i = 0; i < fs->nfont; ++i)
{
FcPattern* font = fs->fonts[i];
FcChar8* file, *style, *family;
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)
{
fontFilesMapping[std::string(reinterpret_cast<char*>(family)) + " " +
std::string(reinterpret_cast<char*>(style))]
= std::string(reinterpret_cast<char*>(file));
std::string fontFull = std::string(reinterpret_cast<char*>(family)) + " " +
std::string(reinterpret_cast<char*>(style));
Utils::ToLower(fontFull);
fontFilesMapping[std::move(fontFull)] = std::string(reinterpret_cast<char*>(file));
}
}
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
FcConfigDestroy(config);
return fontFilesMapping;
}
}

View File

@@ -19,9 +19,15 @@ TEST_CASE("Search system fonts")
std::string path = SystemFontResolver::GetSystemFontPath("Ubuntu Regular");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf");
path = SystemFontResolver::GetSystemFontPath("ubuntu regular");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf");
path = SystemFontResolver::GetSystemFontPath("Ubuntu Mono Bold Italic");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/UbuntuMono-BI.ttf");
path = SystemFontResolver::GetSystemFontPath("ubuntu mono bold italic");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/UbuntuMono-BI.ttf");
path = SystemFontResolver::GetSystemFontPath("NON-EXISTING Font");
REQUIRE(path.empty());
}