fix wrong charset being used for atlas generation and use namespaces explicitly because of broken intellisense

This commit is contained in:
ohyzha
2025-01-21 16:24:33 +02:00
parent 16d866a203
commit c81e63a8ff

View File

@@ -14,9 +14,6 @@
namespace OpenVulkano::Scene
{
using namespace msdfgen;
using namespace msdf_atlas;
SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::sdfDefaultConfig = { 42, 1.0, 5 };
SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::msdfDefaultConfig = { 32, 1.0, 3 };
@@ -24,11 +21,11 @@ namespace OpenVulkano::Scene
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset,
const std::optional<std::string>& pngOutput)
{
FreetypeHandle* ft;
FontHandle* font;
msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font;
InitFreetypeFromFile(ft, font, fontFile);
Charset s;
std::for_each(s.begin(), s.end(), [&](uint32_t unicode) { s.add(unicode); });
msdf_atlas::Charset s;
std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); });
Generate(ft, font, s, pngOutput);
}
@@ -42,42 +39,47 @@ namespace OpenVulkano::Scene
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset,
const std::optional<std::string>& pngOutput)
{
FreetypeHandle* ft;
FontHandle* font;
msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font;
InitFreetypeFromBuffer(ft, font, (const msdfgen::byte*)(fontData.Data()), fontData.Size());
Charset s;
std::for_each(s.begin(), s.end(), [&](uint32_t unicode) { s.add(unicode); });
msdf_atlas::Charset s;
std::for_each(charset.begin(), charset.end(), [&](uint32_t unicode) { s.add(unicode); });
Generate(ft, font, s, pngOutput);
}
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const Charset& charset,
const std::optional<std::string>& pngOutput)
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile,
const msdf_atlas::Charset& charset,
const std::optional<std::string>& pngOutput)
{
// TODO: dynamic atlas and add only those symbols which are not present yet in current atlas
FreetypeHandle* ft;
FontHandle* font;
msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font;
InitFreetypeFromFile(ft, font, fontFile);
Generate(ft, font, charset, pngOutput);
}
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const msdfgen::byte* fontData, int length,
const Charset& charset,
const std::optional<std::string>& pngOutput)
const msdf_atlas::Charset& charset,
const std::optional<std::string>& pngOutput)
{
FreetypeHandle* ft;
FontHandle* font;
msdfgen::FreetypeHandle* ft;
msdfgen::FontHandle* font;
InitFreetypeFromBuffer(ft, font, fontData, length);
Generate(ft, font, charset, pngOutput);
}
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromFile(FreetypeHandle*& ft, FontHandle*& font,
const std::string& fontFile)
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft,
msdfgen::FontHandle*& font,
const std::string& fontFile)
{
ft = initializeFreetype();
if (!ft) { throw std::runtime_error("Failed to initialize freetype"); }
ft = msdfgen::initializeFreetype();
if (!ft)
{
throw std::runtime_error("Failed to initialize freetype");
}
font = loadFont(ft, fontFile.data());
if (!font)
{
@@ -88,11 +90,15 @@ namespace OpenVulkano::Scene
}
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromBuffer(FreetypeHandle*& ft, FontHandle*& font,
const msdfgen::byte* fontData, int length)
void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft,
msdfgen::FontHandle*& font,
const msdfgen::byte* fontData, int length)
{
ft = initializeFreetype();
if (!ft) { throw std::runtime_error("Failed to initialize freetype"); }
ft = msdfgen::initializeFreetype();
if (!ft)
{
throw std::runtime_error("Failed to initialize freetype");
}
font = loadFontData(ft, fontData, length);
if (!font)
{
@@ -103,24 +109,24 @@ namespace OpenVulkano::Scene
}
template<int Channels>
void SdfFontAtlasGeneratorGeneric<Channels>::Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
void SdfFontAtlasGeneratorGeneric<Channels>::Generate(msdfgen::FreetypeHandle* ft, msdfgen::FontHandle* font,
const msdf_atlas::Charset& chset,
const std::optional<std::string>& pngOutput)
{
std::vector<GlyphGeometry> glyphsGeometry;
std::vector<msdf_atlas::GlyphGeometry> glyphsGeometry;
// FontGeometry is a helper class that loads a set of glyphs from a single font.
FontGeometry fontGeometry(&glyphsGeometry);
msdf_atlas::FontGeometry fontGeometry(&glyphsGeometry);
fontGeometry.loadCharset(font, 1, chset);
if constexpr (Channels == 3)
{
const double maxCornerAngle = 3.0;
for (GlyphGeometry& glyph: glyphsGeometry)
for (msdf_atlas::GlyphGeometry& glyph : glyphsGeometry)
glyph.edgeColoring(&msdfgen::edgeColoringByDistance, maxCornerAngle, 0);
}
TightAtlasPacker packer;
packer.setDimensionsConstraint(DimensionsConstraint::SQUARE);
msdf_atlas::TightAtlasPacker packer;
packer.setDimensionsConstraint(msdf_atlas::DimensionsConstraint::SQUARE);
int width, height;
const int glyphsPerRow = std::sqrt(glyphsGeometry.size());
const int glyphSize = m_config.glyphSize;
@@ -135,7 +141,7 @@ namespace OpenVulkano::Scene
Generator generator;
generator.resize(width, height);
GeneratorAttributes attributes;
msdf_atlas::GeneratorAttributes attributes;
generator.setAttributes(attributes);
generator.setThreadCount(4);
generator.generate(glyphsGeometry.data(), glyphsGeometry.size());
@@ -147,7 +153,7 @@ namespace OpenVulkano::Scene
if constexpr (Channels == 3)
{
// store RGB as RGBA
const BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
const msdfgen::BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
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)
{
@@ -171,7 +177,7 @@ namespace OpenVulkano::Scene
for (const auto& glyph: glyphsGeometry)
{
GlyphInfo& info = m_atlasData->GetGlyphs()[glyph.getCodepoint()];
const GlyphBox& glyphBox = generator.getLayout()[idx++];
const msdf_atlas::GlyphBox& glyphBox = generator.getLayout()[idx++];
Bbox glyphBaselineBbox, glyphAtlasBbox;
glyph.getQuadPlaneBounds(glyphBaselineBbox.l, glyphBaselineBbox.b, glyphBaselineBbox.r,