atlas factory Windows implementation

This commit is contained in:
ohyzha
2025-01-17 13:11:04 +02:00
parent 45ca54feb7
commit 169d6c4129
9 changed files with 260 additions and 2 deletions

View File

@@ -394,4 +394,10 @@ namespace OpenVulkano
{
return InterfaceOrientation::Landscape; // TODO?
}
std::string SystemInfo::GetSystemFontPath(const std::string& fontName)
{
return "";
}
}

View File

@@ -202,4 +202,9 @@ namespace OpenVulkano
{
return InterfaceOrientation::Landscape; //TODO?
}
std::string SystemInfo::GetSystemFontPath(const std::string& fontName)
{
return "";
}
}

View File

@@ -69,6 +69,7 @@ namespace OpenVulkano
static DeviceOrientation GetDeviceOrientation();
static void EnableDeviceOrientationEvents();
static InterfaceOrientation GetInterfaceOrientation();
static std::string GetSystemFontPath(const std::string& fontName);
static Event<> OnLowPowerModeChanged;
static Event<> OnBatteryStateChanged;

View File

@@ -19,6 +19,9 @@
#include <guiddef.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <map>
#include <fstream>
#include <filesystem>
// NOTE(vb): Windows defines macros like GetUserName that are used to automatically select the appropriate function version (GetUserNameA for ANSI and GetUserNameW for Unicode)
// based on whether the _UNICODE macro is defined, so we manually undefine these macros to avoid naming collisions.
@@ -28,6 +31,8 @@
#pragma comment(lib, "PowrProf.lib")
#pragma comment(lib, "wbemuuid.lib")
#define QFR_DESCRIPTION 1
namespace OpenVulkano
{
namespace
@@ -585,4 +590,56 @@ namespace OpenVulkano
return InterfaceOrientation::Landscape;
}
}
std::string SystemInfo::GetSystemFontPath(const std::string& fontName)
{
// font name -> filename
static std::map<std::string, std::string> fontFileMapping;
if (fontFileMapping.empty())
{
// 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)
int(WINAPI * GetFontResourceInfoW)(wchar_t*, unsigned long*, void*, unsigned long);
*(FARPROC*) &GetFontResourceInfoW = GetProcAddress(GetModuleHandleA("gdi32"), "GetFontResourceInfoW");
std::wstring winDir;
winDir.resize(MAX_PATH);
UINT len = GetWindowsDirectoryW(winDir.data(), MAX_PATH);
winDir.resize(len);
std::filesystem::path fontsDir = std::filesystem::path(winDir) / "Fonts";
for (const auto& fontFilename : std::filesystem::directory_iterator(fontsDir))
{
unsigned long size = 0;
std::wstring ws = fontFilename.path().wstring();
if (!GetFontResourceInfoW(ws.data(), &size, NULL, QFR_DESCRIPTION))
{
continue;
}
std::wstring fontName;
fontName.resize(size);
if (GetFontResourceInfoW(ws.data(), &size, fontName.data(), QFR_DESCRIPTION))
{
// remove null-terminated characters since size is always bigger than needed
std::string fontNameCropped(fontName.begin(), fontName.end());
size_t realSize = 0;
for (; realSize < fontNameCropped.size(); realSize++)
{
if (fontNameCropped[realSize] == '\0')
{
break;
}
}
fontNameCropped.resize(realSize);
fontFileMapping[std::move(fontNameCropped)] = fontFilename.path().string();
}
}
}
// maybe check everything in lower case ? so that we can use Arial/arial as input parameter
if (fontFileMapping.contains(fontName))
{
return fontFileMapping.at(fontName);
}
return "";
}
}

View File

@@ -288,4 +288,9 @@ namespace OpenVulkano
}
return InterfaceOrientation::Landscape;
}
std::string SystemInfo::GetSystemFontPath(const std::string& fontName)
{
return "";
}
}

View File

@@ -45,6 +45,8 @@ namespace OpenVulkano::Scene
void DeserializeMetadata(const std::span<char>& data);
public:
using Ptr = std::shared_ptr<FontAtlas>;
FontAtlas() = default;
FontAtlas(const Math::Vector2ui textureResolution, const double lineHeight, const FontAtlasType atlasType,
DataFormat dataFormat)

View File

@@ -0,0 +1,79 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "FontAtlasFactory.hpp"
#include "Scene/SdfFontAtlasGenerator.hpp"
#include "Scene/BitmapFontAtlasGenerator.hpp"
#include "Host/SystemInfo.hpp"
namespace OpenVulkano::Scene
{
FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier,
const std::set<uint32_t>& charset, bool msdf) const
{
const std::string fileName = FindFont(fontIdentifier);
if (fileName.empty())
{
return nullptr;
}
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontIdentifier) : charset);
FontIdentifier id(fontIdentifier, setRef, SubpixelLayout::UNKNOWN, 0, msdf);
if (m_atlasesCache.contains(id))
{
return m_atlasesCache.at(id);
}
if (msdf)
{
MsdfFontAtlasGenerator msdfGen;
msdfGen.GenerateAtlas(fileName, setRef);
m_atlasesCache[id] = msdfGen.GetAtlas();
return m_atlasesCache.at(id);
}
SdfFontAtlasGenerator sdfGen;
sdfGen.GenerateAtlas(fileName, setRef);
m_atlasesCache[id] = sdfGen.GetAtlas();
return m_atlasesCache.at(id);
}
FontAtlas::Ptr FontAtlasFactory::GetFontAtlas(const std::string& fontIdentifier, float ptSize,
const std::set<uint32_t>& charset,
std::optional<SubpixelLayout> subpixelLayout) const
{
const std::string fileName = FindFont(fontIdentifier);
if (fileName.empty())
{
return nullptr;
}
const std::set<uint32_t>& setRef = (charset.empty() ? FontAtlasGeneratorBase::LoadAllGlyphs(fontIdentifier) : charset);
FontIdentifier id(fontIdentifier, setRef, subpixelLayout.value_or(SubpixelLayout::UNKNOWN), ptSize, false);
if (m_atlasesCache.contains(id))
{
return m_atlasesCache.at(id);
}
FontPixelSizeConfig cfg(ptSize);
BitmapFontAtlasGenerator bitmapGen(cfg, subpixelLayout);
bitmapGen.GenerateAtlas(fileName, setRef);
m_atlasesCache[id] = bitmapGen.GetAtlas();
return m_atlasesCache.at(id);
}
std::string FontAtlasFactory::FindFont(const std::string& fontIdentifier) const
{
if (!std::filesystem::exists(fontIdentifier))
{
if (!m_allowSystemFonts)
{
return "";
}
return SystemInfo::GetSystemFontPath(fontIdentifier);
}
return fontIdentifier;
}
}

View File

@@ -0,0 +1,69 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "FontAtlas.hpp"
#include "Scene/SubpixelLayout.hpp"
namespace OpenVulkano::Scene
{
class FontAtlasFactory final
{
struct FontIdentifier
{
FontIdentifier(const std::string& font_, const std::set<uint32_t>& charset, SubpixelLayout subpixelLayout_,
float ptSize_, bool msdf_)
: font(font_), subpixelLayout(subpixelLayout_), ptSize(ptSize_), msdf(msdf_)
{
std::for_each(charset.begin(), charset.end(), [&](uint32_t c) { charsetHash ^= c; });
}
std::string font;
size_t charsetHash = 0;
SubpixelLayout subpixelLayout = SubpixelLayout::UNKNOWN;
float ptSize = 0;
bool msdf = true;
bool operator<(const FontIdentifier& other) const
{
if (font != other.font)
{
return font < other.font;
}
if (charsetHash != other.charsetHash)
{
return charsetHash < other.charsetHash;
}
if (subpixelLayout != other.subpixelLayout)
{
return subpixelLayout < other.subpixelLayout;
}
if (ptSize != other.ptSize)
{
return ptSize < other.ptSize;
}
return msdf < other.msdf;
}
};
public:
FontAtlasFactory(bool allowSystemFonts = true) : m_allowSystemFonts(allowSystemFonts) {}
[[nodiscard]] FontAtlas::Ptr GetFontAtlasScalable(const std::string& fontIdentifier,
const std::set<uint32_t>& charset = {},
bool msdf = true) const;
[[nodiscard]] FontAtlas::Ptr GetFontAtlas(const std::string& fontIdentifier, float ptSize,
const std::set<uint32_t>& charset = {},
std::optional<SubpixelLayout> subpixelLayout = std::nullopt) const;
private:
std::string FindFont(const std::string& fontFile) const;
private:
bool m_allowSystemFonts;
mutable std::map<FontIdentifier, FontAtlas::Ptr> m_atlasesCache;
};
}

View File

@@ -0,0 +1,34 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include "Base/Logger.hpp"
#include "Host/SystemInfo.hpp"
#include <filesystem>
#include "Scene/Text/FontAtlasFactory.hpp"
#include "Host/ResourceLoader.hpp"
using namespace OpenVulkano;
TEST_CASE("Search system fonts")
{
Logger::SetupLogger("", "tests.log");
// assume these fonts are present since they are default
std::filesystem::path path = SystemInfo::GetSystemFontPath("Arial");
REQUIRE(path.filename() == "arial.ttf");
// assume these fonts are present since they are default
path = SystemInfo::GetSystemFontPath("Times New Roman");
REQUIRE(path.filename() == "times.ttf");
path = SystemInfo::GetSystemFontPath("Arial Bold Italic");
REQUIRE(path.filename() == "arialbi.ttf");
path = SystemInfo::GetSystemFontPath("NON-EXISTING Font");
REQUIRE(path.empty());
}