Update SystemFontResolver to std::filesystem::path
This commit is contained in:
@@ -12,20 +12,24 @@
|
|||||||
|
|
||||||
namespace OpenVulkano
|
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
|
// fontName -> fontPath
|
||||||
static std::map<std::string, std::string> fontFilesMapping = ReadSystemFonts();
|
static std::map<std::string, std::filesystem::path> fontFilesMapping = ReadSystemFonts();
|
||||||
static std::string fallbackString;
|
|
||||||
std::string fontNameLower = Utils::ToLower(fontName);
|
std::string fontNameLower = Utils::ToLower(fontName);
|
||||||
auto it = fontFilesMapping.find(fontNameLower);
|
auto it = fontFilesMapping.find(fontNameLower);
|
||||||
if (it != fontFilesMapping.end()) return it->second;
|
if (it != fontFilesMapping.end()) return it->second;
|
||||||
it = fontFilesMapping.find(fontNameLower + " regular");
|
it = fontFilesMapping.find(fontNameLower + " regular");
|
||||||
if (it != fontFilesMapping.end()) return it->second;
|
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<FcConfig, decltype(&FcConfigDestroy)> config(FcInitLoadConfigAndFonts(), &FcConfigDestroy);
|
||||||
std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pat(FcPatternCreate(), &FcPatternDestroy);
|
std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pat(FcPatternCreate(), &FcPatternDestroy);
|
||||||
@@ -38,7 +42,7 @@ namespace OpenVulkano
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> fontFilesMapping;
|
std::map<std::string, std::filesystem::path> fontFilesMapping;
|
||||||
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];
|
||||||
@@ -52,7 +56,7 @@ namespace OpenVulkano
|
|||||||
std::string fontFull = 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));
|
||||||
Utils::ToLower(fontFull);
|
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;
|
return fontFilesMapping;
|
||||||
|
|||||||
@@ -9,13 +9,17 @@
|
|||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
|
namespace
|
||||||
{
|
{
|
||||||
static std::string fallbackString;
|
const std::filesystem::path FALLBACK_PATH;
|
||||||
return fallbackString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,22 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <filesystem>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace OpenVulkano
|
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
|
class SystemFontResolver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[deprecated]] static const std::string& GetSystemFontPath(const std::string& fontName);
|
[[nodiscard]] static const std::filesystem::path& GetSystemFontPath(const std::string& fontName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::map<std::string, std::string> ReadSystemFonts();
|
static std::map<std::string, std::filesystem::path> ReadSystemFonts();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,16 +37,19 @@ namespace
|
|||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
const std::string& SystemFontResolver::GetSystemFontPath(const std::string& fontName)
|
namespace
|
||||||
{
|
{
|
||||||
// font name -> filename
|
const std::filesystem::path FALLBACK_PATH;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
DirectWriteAutoReleasable<IDWriteFactory7> dwrite;
|
||||||
HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory7), (IUnknown**) &dwrite);
|
HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory7), (IUnknown**) &dwrite);
|
||||||
@@ -64,7 +67,7 @@ namespace OpenVulkano
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> fontFileMapping;
|
std::map<std::string, std::filesystem::path> fontFileMapping;
|
||||||
const UINT32 familyCount = matchingFonts->GetFontCount();
|
const UINT32 familyCount = matchingFonts->GetFontCount();
|
||||||
// thank you Microsoft for function that is not even documented, but exists and it's the only function that
|
// 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)
|
// 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());
|
const size_t realSize = strlen(fontNameCropped.data());
|
||||||
fontNameCropped.resize(realSize);
|
fontNameCropped.resize(realSize);
|
||||||
Utils::ToLower(fontNameCropped);
|
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;
|
return fontFileMapping;
|
||||||
|
|||||||
@@ -9,13 +9,13 @@
|
|||||||
|
|
||||||
namespace OpenVulkano
|
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;
|
return fallbackString;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> SystemFontResolver::ReadSystemFonts()
|
std::map<std::string, std::filesystem::path> SystemFontResolver::ReadSystemFonts()
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user