Update to FontAtlasFactory for more eficent loading

This commit is contained in:
Georg Hagen
2025-03-02 18:35:36 +01:00
parent ba2570d444
commit ee82919293
5 changed files with 69 additions and 56 deletions

View File

@@ -16,57 +16,72 @@ namespace OpenVulkano::Scene
{
FontAtlasFactory FontAtlasFactory::INSTANCE = FontAtlasFactory(); // Global factory
FontAtlasFactory::FontIdentifier::FontIdentifier(const std::string& font_, const std::set<uint32_t>& charset,
SubpixelLayout subpixelLayout_, float ptSize_,
FontAtlasType atlasType_)
: font(font_), subpixelLayout(subpixelLayout_), ptSize(ptSize_), atlasType(atlasType_)
{
std::for_each(charset.begin(), charset.end(), [&](uint32_t c) { charsetHash ^= c; });
}
bool FontAtlasFactory::FontIdentifier::FontIdentifier::operator<(const FontIdentifier& other) const
{
return std::tie(atlasType, charsetHash, ptSize, subpixelLayout, font)
< std::tie(other.atlasType, other.charsetHash, other.ptSize, other.subpixelLayout, other.font);
if (atlasType < other.atlasType) return true;
if (atlasType > other.atlasType) return false;
if (atlasType.IsSDF())
{
return font < other.font;
}
return std::tie(ptSize, subpixelLayout, font) < std::tie(other.ptSize, other.subpixelLayout, other.font);
}
FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier, bool msdf,
const std::set<uint32_t>& charset) const
FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier, bool msdf, const std::set<uint32_t>& charset) const
{
const auto& fontData = FindFont(fontIdentifier);
if (fontData.Empty())
{
Logger::DATA->warn("Could not find font {}", fontIdentifier);
return nullptr;
}
std::set<uint32_t> fallback;
if (charset.empty()) FontAtlasGeneratorBase::LoadAllGlyphs(fallback, fontData.AsBytes());
const std::set<uint32_t>& setRef = (charset.empty() ? fallback : charset);
FontIdentifier id(fontIdentifier, setRef, SubpixelLayout::UNKNOWN, 0,
msdf ? FontAtlasType::MSDF : FontAtlasType::SDF);
FontIdentifier id(fontIdentifier, msdf ? FontAtlasType::MSDF : FontAtlasType::SDF);
auto it = m_atlasesCache.find(id);
if (it != m_atlasesCache.end())
{
return it->second;
}
Array<char> ovFontData = ResourceLoader::GetInstance().GetResource(fontIdentifier + ".ovfont");
if (!ovFontData.Empty())
{
auto atlas = std::make_shared<FontAtlas>(ovFontData);
m_atlasesCache[FontIdentifier(fontIdentifier, atlas->GetAtlasType())] = atlas;
return atlas;
}
const auto& fontData = FindFont(fontIdentifier);
if (fontData.Empty())
{
Logger::DATA->warn("Could not find font {}", fontIdentifier);
return nullptr;
}
FontAtlas::Ptr atlas;
if (msdf)
{
MsdfFontAtlasGenerator msdfGen;
msdfGen.GenerateAtlas(fontData, charset);
return m_atlasesCache.insert({ id, msdfGen.GetAtlas() }).first->second;
atlas = msdfGen.GetAtlas();
}
SdfFontAtlasGenerator sdfGen;
sdfGen.GenerateAtlas(fontData, charset);
return m_atlasesCache.insert({ id, sdfGen.GetAtlas() }).first->second;
else
{
SdfFontAtlasGenerator sdfGen;
sdfGen.GenerateAtlas(fontData, charset);
atlas = sdfGen.GetAtlas();
}
if (charset.empty()) m_atlasesCache.emplace(id, atlas);
return atlas;
}
FontAtlas::Ptr FontAtlasFactory::GetFontAtlas(const std::string& fontIdentifier, float ptSize,
SubpixelLayout subpixelLayout,
const std::set<uint32_t>& charset) const
{
FontIdentifier id(fontIdentifier, subpixelLayout ? FontAtlasType::BITMAP_SUBPIXEL : FontAtlasType::BITMAP, subpixelLayout, ptSize);
auto it = m_atlasesCache.find(id);
if (it != m_atlasesCache.end())
{
return it->second;
}
const auto& fontData = FindFont(fontIdentifier);
if (fontData.Empty())
{
@@ -74,22 +89,11 @@ namespace OpenVulkano::Scene
return nullptr;
}
std::set<uint32_t> fallback;
if (charset.empty()) FontAtlasGeneratorBase::LoadAllGlyphs(fallback, fontData.AsBytes());
const std::set<uint32_t>& setRef = (charset.empty() ? fallback : charset);
FontIdentifier id(fontIdentifier, setRef, subpixelLayout, ptSize,
subpixelLayout ? FontAtlasType::BITMAP_SUBPIXEL : FontAtlasType::BITMAP);
auto it = m_atlasesCache.find(id);
if (it != m_atlasesCache.end())
{
return it->second;
}
FontPixelSizeConfig cfg(ptSize);
BitmapFontAtlasGenerator bitmapGen(cfg, subpixelLayout);
bitmapGen.GenerateAtlas(fontData, setRef);
return m_atlasesCache.insert({ id, bitmapGen.GetAtlas() }).first->second;
bitmapGen.GenerateAtlas(fontData, charset);
if (charset.empty()) m_atlasesCache.emplace(id, bitmapGen.GetAtlas());
return bitmapGen.GetAtlas();
}
Array<char> FontAtlasFactory::FindFont(const std::string& fontIdentifier) const