From 497ec7d09b197768aa0152276e41660677580f07 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Mon, 20 Jan 2025 13:59:46 +0200 Subject: [PATCH] Linux side code refactoring --- .../Host/Linux/SystemFontResolver.cpp | 40 +++++++++---------- .../Linux/SystemFontsSearchTestsUbuntu.cpp | 6 +++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp b/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp index c715e4c..e228a63 100644 --- a/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp +++ b/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp @@ -6,34 +6,31 @@ #include "Host/SystemFontResolver.hpp" #include "Base/Logger.hpp" +#include "Base/Utils.hpp" #include +#include namespace OpenVulkano { - std::string SystemFontResolver::GetSystemFontPath(const std::string& fontName) + const 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 ""; + static std::string fallbackString; + auto it = fontFilesMapping.find(Utils::ToLower(fontName)); + return it == fontFilesMapping.end() ? fallbackString : it->second; } 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); + std::unique_ptr config(FcInitLoadConfigAndFonts(), &FcConfigDestroy); + std::unique_ptr pat(FcPatternCreate(), &FcPatternDestroy); + std::unique_ptr os(FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, NULL), &FcObjectSetDestroy); + std::unique_ptr 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(family)) + " " + - std::string(reinterpret_cast(style))] - = std::string(reinterpret_cast(file)); + std::string fontFull = std::string(reinterpret_cast(family)) + " " + + std::string(reinterpret_cast(style)); + Utils::ToLower(fontFull); + fontFilesMapping[std::move(fontFull)] = std::string(reinterpret_cast(file)); } } - FcFontSetDestroy(fs); - FcObjectSetDestroy(os); - FcPatternDestroy(pat); - FcConfigDestroy(config); return fontFilesMapping; } } diff --git a/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp b/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp index 743631b..7db0018 100644 --- a/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp +++ b/tests/Host/Linux/SystemFontsSearchTestsUbuntu.cpp @@ -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()); }