Refactor FontAtlas class

This commit is contained in:
Georg Hagen
2025-01-11 01:25:52 +01:00
parent 6a3c31346f
commit 9cb3d4de85
14 changed files with 246 additions and 247 deletions

View File

@@ -5,7 +5,7 @@
*/
#if __has_include("msdfgen.h")
#include "Text/FontAtlas.hpp"
#include "SdfFontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include <msdfgen.h>
@@ -106,7 +106,6 @@ namespace OpenVulkano::Scene
void SdfFontAtlasGeneratorGeneric<Channels>::Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
const std::optional<std::string>& pngOutput)
{
m_atlasData.reset(new AtlasData);
std::vector<GlyphGeometry> glyphsGeometry;
// FontGeometry is a helper class that loads a set of glyphs from a single font.
@@ -142,14 +141,14 @@ namespace OpenVulkano::Scene
generator.generate(glyphsGeometry.data(), glyphsGeometry.size());
int idx = 0;
SetupAtlasData(Math::Vector3ui(width, height, 1), fontGeometry.getMetrics().lineHeight,
channelsCount == 1 ? FontAtlasType::SDF : FontAtlasType::MSDF);
m_atlasData = std::make_shared<FontAtlas>(Math::Vector2ui{ width, height }, fontGeometry.getMetrics().lineHeight,
channelsCount == 1 ? FontAtlasType::SDF : FontAtlasType::MSDF);
if constexpr (Channels == 3)
{
// store RGB as RGBA
const BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
msdfgen::byte* data = static_cast<msdfgen::byte*>(m_atlasData->img->data.Data());
msdfgen::byte* data = static_cast<msdfgen::byte*>(m_atlasData->GetTexture()->textureBuffer);
for (size_t srcPos = 0, dstPos = 0; srcPos < width * height * 3; srcPos += 3, dstPos += 4)
{
data[dstPos] = storage.pixels[srcPos];
@@ -161,7 +160,7 @@ namespace OpenVulkano::Scene
else
{
const msdfgen::BitmapConstRef<msdfgen::byte, 1>& storage = generator.atlasStorage();
memcpy(m_atlasData->img->data.Data(), storage.pixels, width * height);
memcpy(m_atlasData->GetTexture()->textureBuffer, storage.pixels, width * height);
}
struct Bbox
@@ -171,7 +170,7 @@ namespace OpenVulkano::Scene
for (const auto& glyph: glyphsGeometry)
{
GlyphInfo& info = m_atlasData->glyphs[glyph.getCodepoint()];
GlyphInfo& info = m_atlasData->GetGlyphs()[glyph.getCodepoint()];
const GlyphBox& glyphBox = generator.getLayout()[idx++];
Bbox glyphBaselineBbox, glyphAtlasBbox;
@@ -195,7 +194,7 @@ namespace OpenVulkano::Scene
SetGlyphData(info, { bearingX, bearingY }, { w, h }, glyphAtlasAABB, glyphBox.advance);
}
if (pngOutput && !pngOutput->empty()) { SavePng(pngOutput.value()); }
if (pngOutput && !pngOutput->empty()) { m_atlasData->Save(*pngOutput); }
destroyFont(font);
deinitializeFreetype(ft);
}