diff --git a/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.cpp b/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.cpp index f4a2fcc..119bd7b 100644 --- a/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.cpp +++ b/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.cpp @@ -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, 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& 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); + } } diff --git a/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.hpp b/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.hpp index 93acf7f..7b8c59f 100644 --- a/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.hpp +++ b/openVulkanoCpp/Scene/BitmapFontAtlasGenerator.hpp @@ -41,6 +41,7 @@ namespace OpenVulkano::Scene const std::optional& pngOutput = std::nullopt) override; private: void Generate(const std::variant>& source, const std::set& chset, const std::optional& pngOutput); + void FillGlyphsInfo(const std::vector& allGlyphs, const FtFaceRecPtr& face, double scaleFactor); std::pair, double> InitGlyphsForPacking(const std::set& chset, const FtFaceRecPtr& face); private: FontPixelSizeConfig m_pixelSizeConfig;