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

@@ -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;
};
}