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

View File

@@ -19,9 +19,15 @@ TEST_CASE("Search system fonts")
std::string path = SystemFontResolver::GetSystemFontPath("Ubuntu Regular"); std::string path = SystemFontResolver::GetSystemFontPath("Ubuntu Regular");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf"); 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"); path = SystemFontResolver::GetSystemFontPath("Ubuntu Mono Bold Italic");
REQUIRE(path == "/usr/share/fonts/truetype/ubuntu/UbuntuMono-BI.ttf"); 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"); path = SystemFontResolver::GetSystemFontPath("NON-EXISTING Font");
REQUIRE(path.empty()); REQUIRE(path.empty());
} }