working version of glyphs rendering with example

This commit is contained in:
ohyzha
2024-07-31 12:54:24 +03:00
parent 847b8660b5
commit 9b58ba5f55
9 changed files with 352 additions and 1 deletions

View File

@@ -0,0 +1,48 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Text.hpp"
#include "fmt/core.h"
#include "msdfgen.h"
#include "msdfgen-ext.h"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
namespace OpenVulkano::Scene
{
using namespace msdfgen;
using namespace msdf_atlas;
void Text::Init(const std::string_view fontFile, char8_t symbol, const std::string_view outputFile)
{
FreetypeHandle *ft = initializeFreetype();
if (!ft)
{
throw std::runtime_error("Failed to initialize freetype");
}
FontHandle *font = loadFont(ft, fontFile.data());
if (!font)
{
deinitializeFreetype(ft);
throw std::runtime_error(fmt::format("Failed to load font freetype from file {0}", fontFile.data()));
}
Shape shape;
if (loadGlyph(shape, font, symbol, FONT_SCALING_EM_NORMALIZED))
{
shape.normalize();
Bitmap<float, 1> sdf(m_cfg.outputSize, m_cfg.outputSize);
// scale, translation (in em's)
Projection proj(m_cfg.outputSize, Vector2(0.125, 0.125));
// distance mapping
Range rng(0.075);
SDFTransformation t(proj, rng);
generateSDF(sdf, shape, t);
savePng(sdf, outputFile.data());
}
destroyFont(font);
deinitializeFreetype(ft);
}
}