move logic into separate function

This commit is contained in:
ohyzha
2025-01-05 19:04:48 +02:00
parent 58a56560d9
commit d446b9695d
2 changed files with 35 additions and 31 deletions

View File

@@ -48,41 +48,11 @@ namespace OpenVulkano::Scene
// The coordinates will be normalized to the em size, i.e. 1 = 1 em
const double scaleFactor = (1. / face->units_per_EM);
SetupAtlasData(atlasResolution, face->height * scaleFactor, FontAtlasType::BITMAP);
size_t loadedGlyphs = 0;
for (const GlyphForPacking& glyph : allGlyphs)
{
FT_Error error = FT_Load_Char(face.get(), glyph.code, FT_LOAD_RENDER);
if (error)
{
Logger::SCENE->error("FT_Load_Char for codepoint {} has failed. {}", glyph.code,
GetFreetypeErrorDescription(error));
continue;
}
FT_GlyphSlot slot = face->glyph;
for (int row = 0; row < slot->bitmap.rows; row++)
{
std::memcpy(&m_atlasData->img->data[glyph.firstGlyphByteInAtlas + row * atlasResolution.x],
&slot->bitmap.buffer[(slot->bitmap.rows - 1 - row) * slot->bitmap.pitch],
slot->bitmap.width);
}
GlyphInfo& glyphInfo = m_atlasData->glyphs[glyph.code];
const Math::Vector2d glyphMetrics = { slot->metrics.width * scaleFactor,
slot->metrics.height * scaleFactor };
const Math::Vector2d glyphBearing = { slot->metrics.horiBearingX * scaleFactor,
slot->metrics.horiBearingY * scaleFactor };
Math::AABB glyphAtlasAABB(Math::Vector3f(glyph.atlasPos.x, glyph.atlasPos.y, 0), Math::Vector3f(glyph.atlasPos.x + slot->bitmap.width, glyph.atlasPos.y + slot->bitmap.rows, 0));
SetGlyphData(glyphInfo, glyphBearing, glyphMetrics, glyphAtlasAABB, slot->advance.x * scaleFactor);
loadedGlyphs++;
}
FillGlyphsInfo(allGlyphs, face, scaleFactor);
if (pngOutput)
{
SavePng(*pngOutput);
}
Logger::SCENE->debug("Created atlas with {} glyphs, {} glyphs could not be loaded", loadedGlyphs, chset.size() - loadedGlyphs);
}
std::pair<std::vector<GlyphForPacking>, double>
@@ -109,4 +79,37 @@ namespace OpenVulkano::Scene
[](const GlyphForPacking& a, const GlyphForPacking& b) { return a.size.y > b.size.y; });
return { allGlyphs, area };
}
void BitmapFontAtlasGenerator::FillGlyphsInfo(const std::vector<GlyphForPacking>& allGlyphs, const FtFaceRecPtr& face, double scaleFactor)
{
size_t loadedGlyphs = 0;
for (const GlyphForPacking& glyph : allGlyphs)
{
FT_Error error = FT_Load_Char(face.get(), glyph.code, FT_LOAD_RENDER);
if (error)
{
Logger::SCENE->error("FT_Load_Char for codepoint {} has failed. {}", glyph.code,
GetFreetypeErrorDescription(error));
continue;
}
FT_GlyphSlot slot = face->glyph;
for (int row = 0; row < slot->bitmap.rows; row++)
{
std::memcpy(&m_atlasData->img->data[glyph.firstGlyphByteInAtlas + row * m_atlasData->img->resolution.x],
&slot->bitmap.buffer[(slot->bitmap.rows - 1 - row) * slot->bitmap.pitch],
slot->bitmap.width);
}
GlyphInfo& glyphInfo = m_atlasData->glyphs[glyph.code];
const Math::Vector2d glyphMetrics = { slot->metrics.width * scaleFactor,
slot->metrics.height * scaleFactor };
const Math::Vector2d glyphBearing = { slot->metrics.horiBearingX * scaleFactor,
slot->metrics.horiBearingY * scaleFactor };
Math::AABB glyphAtlasAABB(Math::Vector3f(glyph.atlasPos.x, glyph.atlasPos.y, 0), Math::Vector3f(glyph.atlasPos.x + slot->bitmap.width, glyph.atlasPos.y + slot->bitmap.rows, 0));
SetGlyphData(glyphInfo, glyphBearing, glyphMetrics, glyphAtlasAABB, slot->advance.x * scaleFactor);
loadedGlyphs++;
}
Logger::SCENE->debug("Created atlas with {} glyphs, {} glyphs could not be loaded", loadedGlyphs, allGlyphs.size() - loadedGlyphs);
}
}