diff --git a/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp b/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp index 1d09aa5..e2df436 100644 --- a/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp +++ b/openVulkanoCpp/Host/Linux/SystemFontResolver.cpp @@ -12,20 +12,24 @@ namespace OpenVulkano { - const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + namespace + { + const std::filesystem::path FALLBACK_PATH; + } + + const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName) { // fontName -> fontPath - static std::map fontFilesMapping = ReadSystemFonts(); - static std::string fallbackString; + static std::map 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 fallbackString; + return FALLBACK_PATH; } - std::map SystemFontResolver::ReadSystemFonts() + std::map SystemFontResolver::ReadSystemFonts() { std::unique_ptr config(FcInitLoadConfigAndFonts(), &FcConfigDestroy); std::unique_ptr pat(FcPatternCreate(), &FcPatternDestroy); @@ -38,7 +42,7 @@ namespace OpenVulkano return {}; } - std::map fontFilesMapping; + std::map fontFilesMapping; for (int i = 0; i < fs->nfont; ++i) { FcPattern* font = fs->fonts[i]; @@ -52,7 +56,7 @@ namespace OpenVulkano 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)); + fontFilesMapping[std::move(fontFull)] = std::filesystem::path(reinterpret_cast(file)); } } return fontFilesMapping; diff --git a/openVulkanoCpp/Host/MacOS/SystemFontResolver.cpp b/openVulkanoCpp/Host/MacOS/SystemFontResolver.cpp index 617c36e..97ec8de 100644 --- a/openVulkanoCpp/Host/MacOS/SystemFontResolver.cpp +++ b/openVulkanoCpp/Host/MacOS/SystemFontResolver.cpp @@ -9,13 +9,17 @@ namespace OpenVulkano { - const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + namespace { - static std::string fallbackString; - return fallbackString; + const std::filesystem::path FALLBACK_PATH; } - std::map SystemFontResolver::ReadSystemFonts() + const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + { + return FALLBACK_PATH; + } + + std::map SystemFontResolver::ReadSystemFonts() { return {}; } diff --git a/openVulkanoCpp/Host/SystemFontResolver.hpp b/openVulkanoCpp/Host/SystemFontResolver.hpp index 110ea52..06b756b 100644 --- a/openVulkanoCpp/Host/SystemFontResolver.hpp +++ b/openVulkanoCpp/Host/SystemFontResolver.hpp @@ -6,17 +6,22 @@ #pragma once -#include +#include #include +#include namespace OpenVulkano { + /** + * This class is only to look up installed system fonts. + * For a more extended font lookup, please use the FontResolver.hpp + */ class SystemFontResolver { public: - [[deprecated]] static const std::string& GetSystemFontPath(const std::string& fontName); + [[nodiscard]] static const std::filesystem::path& GetSystemFontPath(const std::string& fontName); private: - static std::map ReadSystemFonts(); + static std::map ReadSystemFonts(); }; } diff --git a/openVulkanoCpp/Host/Windows/SystemFontResolver.cpp b/openVulkanoCpp/Host/Windows/SystemFontResolver.cpp index 111cc00..2e74ed0 100644 --- a/openVulkanoCpp/Host/Windows/SystemFontResolver.cpp +++ b/openVulkanoCpp/Host/Windows/SystemFontResolver.cpp @@ -37,16 +37,19 @@ namespace namespace OpenVulkano { - const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + namespace { - // font name -> filename - static std::map fontFileMapping = ReadSystemFonts(); - static std::string fallbackString; - auto it = fontFileMapping.find(Utils::ToLower(fontName)); - return it == fontFileMapping.end() ? fallbackString : it->second; + const std::filesystem::path FALLBACK_PATH; } - std::map SystemFontResolver::ReadSystemFonts() + const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + { + static std::map fontFileMapping = ReadSystemFonts(); + auto it = fontFileMapping.find(Utils::ToLower(fontName)); + return it == fontFileMapping.end() ? FALLBACK_PATH : it->second; + } + + std::map SystemFontResolver::ReadSystemFonts() { DirectWriteAutoReleasable dwrite; HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory7), (IUnknown**) &dwrite); @@ -64,7 +67,7 @@ namespace OpenVulkano return {}; } - std::map fontFileMapping; + std::map fontFileMapping; const UINT32 familyCount = matchingFonts->GetFontCount(); // thank you Microsoft for function that is not even documented, but exists and it's the only function that // can return real font name from font filename (e.g. font filename is times.ttf that corresponds to Times New Roman) @@ -133,7 +136,7 @@ namespace OpenVulkano const size_t realSize = strlen(fontNameCropped.data()); fontNameCropped.resize(realSize); Utils::ToLower(fontNameCropped); - fontFileMapping[std::move(fontNameCropped)] = std::string(filePath, filePath + wcslen(filePath)); + fontFileMapping[std::move(fontNameCropped)] = std::filesystem::path(std::wstring(filePath, filePath + wcslen(filePath))); } } return fontFileMapping; diff --git a/openVulkanoCpp/Host/iOS/SystemFontResolver.cpp b/openVulkanoCpp/Host/iOS/SystemFontResolver.cpp index 3aa69e1..59f45ef 100644 --- a/openVulkanoCpp/Host/iOS/SystemFontResolver.cpp +++ b/openVulkanoCpp/Host/iOS/SystemFontResolver.cpp @@ -9,13 +9,13 @@ namespace OpenVulkano { - const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName) + const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName) { - static std::string fallbackString; + static std::filesystem::path fallbackString; return fallbackString; } - std::map SystemFontResolver::ReadSystemFonts() + std::map SystemFontResolver::ReadSystemFonts() { return {}; }