Update SystemFontResolver to std::filesystem::path

This commit is contained in:
Georg Hagen
2025-11-21 20:26:11 +01:00
parent 2e36bd2ca1
commit 79b5bbf165
5 changed files with 42 additions and 26 deletions

View File

@@ -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<std::string, std::string> fontFilesMapping = ReadSystemFonts();
static std::string fallbackString;
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 fallbackString;
return FALLBACK_PATH;
}
std::map<std::string, std::string> SystemFontResolver::ReadSystemFonts()
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);
@@ -38,7 +42,7 @@ namespace OpenVulkano
return {};
}
std::map<std::string, std::string> fontFilesMapping;
std::map<std::string, std::filesystem::path> 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<char*>(family)) + " " +
std::string(reinterpret_cast<char*>(style));
Utils::ToLower(fontFull);
fontFilesMapping[std::move(fontFull)] = std::string(reinterpret_cast<char*>(file));
fontFilesMapping[std::move(fontFull)] = std::filesystem::path(reinterpret_cast<char*>(file));
}
}
return fontFilesMapping;

View File

@@ -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<std::string, std::string> SystemFontResolver::ReadSystemFonts()
const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
{
return FALLBACK_PATH;
}
std::map<std::string, std::filesystem::path> SystemFontResolver::ReadSystemFonts()
{
return {};
}

View File

@@ -6,17 +6,22 @@
#pragma once
#include <string>
#include <filesystem>
#include <map>
#include <string>
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<std::string, std::string> ReadSystemFonts();
static std::map<std::string, std::filesystem::path> ReadSystemFonts();
};
}

View File

@@ -37,16 +37,19 @@ namespace
namespace OpenVulkano
{
const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
namespace
{
// font name -> filename
static std::map<std::string, std::string> 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<std::string, std::string> SystemFontResolver::ReadSystemFonts()
const std::filesystem::path& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
{
static std::map<std::string, std::filesystem::path> fontFileMapping = ReadSystemFonts();
auto it = fontFileMapping.find(Utils::ToLower(fontName));
return it == fontFileMapping.end() ? FALLBACK_PATH : it->second;
}
std::map<std::string, std::filesystem::path> SystemFontResolver::ReadSystemFonts()
{
DirectWriteAutoReleasable<IDWriteFactory7> dwrite;
HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory7), (IUnknown**) &dwrite);
@@ -64,7 +67,7 @@ namespace OpenVulkano
return {};
}
std::map<std::string, std::string> fontFileMapping;
std::map<std::string, std::filesystem::path> 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;

View File

@@ -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<std::string, std::string> SystemFontResolver::ReadSystemFonts()
std::map<std::string, std::filesystem::path> SystemFontResolver::ReadSystemFonts()
{
return {};
}