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,
const std::set<uint32_t>& chset,
const std::set<uint32_t>& inCs,
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);
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());
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);
uint32_t atlasHeight = 0;
std::for_each(shelves.begin(), shelves.end(), [&](const Shelf& shelf) { atlasHeight += shelf.GetHeight(); });

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

View File

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

View File

@@ -36,13 +36,19 @@ namespace OpenVulkano::Scene
}
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)
{
msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font;
InitFreetypeFromBuffer(ft, font, (const msdfgen::byte*)(fontData.Data()), fontData.Size());
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); });
Generate(ft, font, s, pngOutput);
}

View File

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