48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
/*
|
|
* 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);
|
|
}
|
|
} |