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

@@ -24,16 +24,18 @@ namespace OpenVulkano::Scene
} }
void BitmapFontAtlasGenerator::Generate(const std::span<const uint8_t>& fontData, void BitmapFontAtlasGenerator::Generate(const std::span<const uint8_t>& fontData,
const std::set<uint32_t>& chset, const std::set<uint32_t>& inCs,
const std::optional<std::string>& pngOutput) const std::optional<std::string>& pngOutput)
{ {
if (chset.empty())
{
Logger::APP->info("Charset is empty. Nothing to generate.");
return;
}
const auto& [lib, face] = FontAtlasGeneratorBase::InitFreetype(fontData); const auto& [lib, face] = FontAtlasGeneratorBase::InitFreetype(fontData);
std::set<uint32_t> fallback;
if (inCs.empty())
{
FontAtlasGeneratorBase::LoadAllGlyphs(fallback, face);
}
const auto& charset = inCs.empty() ? fallback : inCs;
FT_Set_Pixel_Sizes(face.get(), 0, m_pixelSizeConfig.CalculatePixelSize()); FT_Set_Pixel_Sizes(face.get(), 0, m_pixelSizeConfig.CalculatePixelSize());
if (m_subpixelLayout != SubpixelLayout::UNKNOWN) if (m_subpixelLayout != SubpixelLayout::UNKNOWN)
{ {
@@ -46,7 +48,7 @@ namespace OpenVulkano::Scene
} }
} }
auto [allGlyphs, atlasWidth] = InitGlyphsForPacking(chset, face); auto [allGlyphs, atlasWidth] = InitGlyphsForPacking(charset, face);
std::vector<Shelf> shelves = Shelf::CreateShelves(atlasWidth, allGlyphs, face.get(), m_channelsCount); std::vector<Shelf> shelves = Shelf::CreateShelves(atlasWidth, allGlyphs, face.get(), m_channelsCount);
uint32_t atlasHeight = 0; uint32_t atlasHeight = 0;
std::for_each(shelves.begin(), shelves.end(), [&](const Shelf& shelf) { atlasHeight += shelf.GetHeight(); }); std::for_each(shelves.begin(), shelves.end(), [&](const Shelf& shelf) { atlasHeight += shelf.GetHeight(); });

View File

@@ -16,35 +16,20 @@ namespace OpenVulkano::Scene
{ {
FontAtlasFactory FontAtlasFactory::INSTANCE = FontAtlasFactory(); // Global factory 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 bool FontAtlasFactory::FontIdentifier::FontIdentifier::operator<(const FontIdentifier& other) const
{ {
return std::tie(atlasType, charsetHash, ptSize, subpixelLayout, font) if (atlasType < other.atlasType) return true;
< std::tie(other.atlasType, other.charsetHash, other.ptSize, other.subpixelLayout, other.font); 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, FontAtlas::Ptr FontAtlasFactory::GetFontAtlasScalable(const std::string& fontIdentifier, bool msdf, const std::set<uint32_t>& charset) const
const std::set<uint32_t>& charset) const
{ {
const auto& fontData = FindFont(fontIdentifier); FontIdentifier id(fontIdentifier, msdf ? FontAtlasType::MSDF : FontAtlasType::SDF);
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);
auto it = m_atlasesCache.find(id); auto it = m_atlasesCache.find(id);
if (it != m_atlasesCache.end()) if (it != m_atlasesCache.end())
@@ -52,33 +37,44 @@ namespace OpenVulkano::Scene
return it->second; 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) if (msdf)
{ {
MsdfFontAtlasGenerator msdfGen; MsdfFontAtlasGenerator msdfGen;
msdfGen.GenerateAtlas(fontData, charset); msdfGen.GenerateAtlas(fontData, charset);
return m_atlasesCache.insert({ id, msdfGen.GetAtlas() }).first->second; atlas = msdfGen.GetAtlas();
} }
else
{
SdfFontAtlasGenerator sdfGen; SdfFontAtlasGenerator sdfGen;
sdfGen.GenerateAtlas(fontData, charset); sdfGen.GenerateAtlas(fontData, charset);
return m_atlasesCache.insert({ id, sdfGen.GetAtlas() }).first->second; atlas = sdfGen.GetAtlas();
}
if (charset.empty()) m_atlasesCache.emplace(id, atlas);
return atlas;
} }
FontAtlas::Ptr FontAtlasFactory::GetFontAtlas(const std::string& fontIdentifier, float ptSize, FontAtlas::Ptr FontAtlasFactory::GetFontAtlas(const std::string& fontIdentifier, float ptSize,
SubpixelLayout subpixelLayout, SubpixelLayout subpixelLayout,
const std::set<uint32_t>& charset) const 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; FontIdentifier id(fontIdentifier, subpixelLayout ? FontAtlasType::BITMAP_SUBPIXEL : FontAtlasType::BITMAP, subpixelLayout, ptSize);
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); auto it = m_atlasesCache.find(id);
if (it != m_atlasesCache.end()) if (it != m_atlasesCache.end())
@@ -86,10 +82,18 @@ namespace OpenVulkano::Scene
return it->second; return it->second;
} }
const auto& fontData = FindFont(fontIdentifier);
if (fontData.Empty())
{
Logger::DATA->warn("Could not find font {}", fontIdentifier);
return nullptr;
}
FontPixelSizeConfig cfg(ptSize); FontPixelSizeConfig cfg(ptSize);
BitmapFontAtlasGenerator bitmapGen(cfg, subpixelLayout); BitmapFontAtlasGenerator bitmapGen(cfg, subpixelLayout);
bitmapGen.GenerateAtlas(fontData, setRef); bitmapGen.GenerateAtlas(fontData, charset);
return m_atlasesCache.insert({ id, bitmapGen.GetAtlas() }).first->second; if (charset.empty()) m_atlasesCache.emplace(id, bitmapGen.GetAtlas());
return bitmapGen.GetAtlas();
} }
Array<char> FontAtlasFactory::FindFont(const std::string& fontIdentifier) const Array<char> FontAtlasFactory::FindFont(const std::string& fontIdentifier) const

View File

@@ -16,15 +16,16 @@ namespace OpenVulkano::Scene
{ {
struct FontIdentifier struct FontIdentifier
{ {
FontIdentifier(const std::string& font_, const std::set<uint32_t>& charset, SubpixelLayout subpixelLayout_, FontIdentifier(const std::string_view& font, FontAtlasType atlasType) : font(font), atlasType(atlasType) { assert(atlasType.IsSDF()); }
float ptSize_, FontAtlasType atlasType_); FontIdentifier(const std::string_view& font, FontAtlasType atlasType, SubpixelLayout subpixelLayout, float ptSize)
: font(font), atlasType(atlasType), subpixelLayout(subpixelLayout), ptSize(ptSize) {}
bool operator<(const FontIdentifier& other) const; bool operator<(const FontIdentifier& other) const;
std::string font; std::string font;
uint32_t charsetHash = 0; FontAtlasType atlasType;
SubpixelLayout subpixelLayout = SubpixelLayout::UNKNOWN; SubpixelLayout subpixelLayout = SubpixelLayout::UNKNOWN;
float ptSize = 0; float ptSize = 0;
FontAtlasType atlasType;
}; };
static FontAtlasFactory INSTANCE; static FontAtlasFactory INSTANCE;

View File

@@ -36,13 +36,19 @@ namespace OpenVulkano::Scene
} }
template<int Channels> template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset, void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& inCs,
const std::optional<std::string>& pngOutput) const std::optional<std::string>& pngOutput)
{ {
msdfgen::FreetypeHandle* ft; msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font; msdfgen::FontHandle* font;
InitFreetypeFromBuffer(ft, font, (const msdfgen::byte*)(fontData.Data()), fontData.Size()); InitFreetypeFromBuffer(ft, font, (const msdfgen::byte*)(fontData.Data()), fontData.Size());
msdf_atlas::Charset s; msdf_atlas::Charset s;
std::set<uint32_t> fallback;
if (inCs.empty())
{
FontAtlasGeneratorBase::LoadAllGlyphs(fallback, fontData.AsBytes());
}
const auto& charset = inCs.empty() ? fallback : inCs;
std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); }); std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); });
Generate(ft, font, s, pngOutput); Generate(ft, font, s, pngOutput);
} }

View File

@@ -16,7 +16,7 @@ namespace OpenVulkano
class SubpixelLayout class SubpixelLayout
{ {
public: public:
enum Layout : uint32_t enum Layout : uint16_t
{ {
RGB, RGB,
BGR, BGR,