code refactoring
This commit is contained in:
@@ -9,20 +9,19 @@
|
||||
#define STBI_MSC_SECURE_CRT
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
#include <ft2build.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
std::pair<FontAtlasGeneratorBase::FT_LIB_REC, FontAtlasGeneratorBase::FT_FACE_REC>
|
||||
std::pair<FtLibraryRecPtr, FtFaceRecPtr>
|
||||
FontAtlasGeneratorBase::InitFreetype(const std::variant<std::string, Array<char>>& source)
|
||||
{
|
||||
FT_Library library;
|
||||
auto error = FT_Init_FreeType(&library);
|
||||
if (error)
|
||||
{
|
||||
throw std::runtime_error("Could not initalize freetype library\n");
|
||||
throw std::runtime_error(fmt::format("Could not initalize freetype library. {}", GetFreetypeErrorDescription(error)));
|
||||
}
|
||||
FT_Face face;
|
||||
if (std::holds_alternative<std::string>(source))
|
||||
@@ -40,7 +39,7 @@ namespace OpenVulkano::Scene
|
||||
}
|
||||
else if (error)
|
||||
{
|
||||
throw std::runtime_error("Font file could not be opened or read or it's corrupted\n");
|
||||
throw std::runtime_error(fmt::format("Font file could not be opened or read or it's corrupted. {}", GetFreetypeErrorDescription(error)));
|
||||
}
|
||||
|
||||
// some fancy font without unicode charmap
|
||||
@@ -48,9 +47,17 @@ namespace OpenVulkano::Scene
|
||||
{
|
||||
throw std::runtime_error("Selected font doesn't contain unicode charmap");
|
||||
}
|
||||
return std::make_pair(FT_LIB_REC(library), FT_FACE_REC(face));
|
||||
return { FtLibraryRecPtr(library), FtFaceRecPtr(face) };
|
||||
}
|
||||
|
||||
std::string FontAtlasGeneratorBase::GetFreetypeErrorDescription(FT_Error error)
|
||||
{
|
||||
if (const char* s = FT_Error_String(error))
|
||||
{
|
||||
return s;
|
||||
}
|
||||
return fmt::format("Error code is {}", error);
|
||||
}
|
||||
|
||||
void FontAtlasGeneratorBase::SaveAtlasMetadataInfo(const std::string& outputFile, bool packIntoSingleFile) const
|
||||
{
|
||||
@@ -81,82 +88,75 @@ namespace OpenVulkano::Scene
|
||||
fs.write(reinterpret_cast<const char*>(&packedFlag), sizeof(uint32_t));
|
||||
}
|
||||
|
||||
void FontAtlasGeneratorBase::SavePng(const std::string& output) const
|
||||
void FontAtlasGeneratorBase::SavePng(std::string output) const
|
||||
{
|
||||
stbi_flip_vertically_on_write(1);
|
||||
if (std::filesystem::path(output).extension() == ".png")
|
||||
if (std::filesystem::path(output).extension() != ".png")
|
||||
{
|
||||
stbi_write_png(output.c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y, m_channelsCount, m_atlasData->img->data.Data(),
|
||||
m_channelsCount * m_atlasData->img->resolution.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
stbi_write_png((output + ".png").c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y,
|
||||
m_channelsCount, m_atlasData->img->data.Data(), m_channelsCount * m_atlasData->img->resolution.x);
|
||||
output += ".png";
|
||||
}
|
||||
stbi_write_png(output.c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y, m_channelsCount,
|
||||
m_atlasData->img->data.Data(), m_channelsCount * m_atlasData->img->resolution.x);
|
||||
}
|
||||
|
||||
void FontAtlasGeneratorBase::SetupAtlasData(Math::Vector2ui textureResolution, double lineHeight,
|
||||
FontAtlasType::Type atlasType)
|
||||
{
|
||||
// generate texture
|
||||
m_atlasData->img = std::make_unique<Image::Image>();
|
||||
m_atlasData->img->resolution = Math::Vector3ui(textureResolution, 1);
|
||||
if (m_channelsCount == 1)
|
||||
{
|
||||
m_atlasData->img = std::make_unique<Image::Image>();
|
||||
m_atlasData->img->data = Array<uint8_t>(textureResolution.x * textureResolution.y);
|
||||
m_atlasData->img->resolution = Math::Vector3ui(textureResolution, 1);
|
||||
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8_UNORM;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_atlasData->img = std::make_unique<Image::Image>();
|
||||
// RGBA
|
||||
m_atlasData->img->data = Array<uint8_t>(textureResolution.x * textureResolution.y * 4);
|
||||
m_atlasData->img->resolution = Math::Vector3ui(textureResolution, 1);
|
||||
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8G8B8A8_UNORM;
|
||||
}
|
||||
m_atlasData->texture.resolution = m_atlasData->img->resolution;
|
||||
m_atlasData->texture.textureBuffer = m_atlasData->img->data.Data();
|
||||
m_atlasData->texture.format = m_atlasData->img->dataFormat;
|
||||
m_atlasData->texture.size = m_atlasData->img->data.Size();
|
||||
m_atlasData->texture.m_samplerConfig = &SamplerConfig::NEAREST;
|
||||
m_atlasData->meta.atlasType = atlasType;
|
||||
m_atlasData->meta.lineHeight = lineHeight;
|
||||
if (atlasType == FontAtlasType::BITMAP)
|
||||
{
|
||||
m_atlasData->texture.m_samplerConfig = &SamplerConfig::NEAREST;
|
||||
}
|
||||
}
|
||||
|
||||
void FontAtlasGeneratorBase::SetGlyphData(GlyphInfo& info, Math::Vector2d bearing, Math::Vector2d size,
|
||||
const Math::AABB& aabb, double advance)
|
||||
{
|
||||
double bearingX = bearing.x;
|
||||
double bearingY = bearing.y;
|
||||
double w = size.x;
|
||||
double h = size.y;
|
||||
double l = aabb.min.x;
|
||||
double r = aabb.max.x;
|
||||
double t = aabb.max.y;
|
||||
double b = aabb.min.y;
|
||||
const double bearingX = bearing.x;
|
||||
const double bearingY = bearing.y;
|
||||
const double w = size.x;
|
||||
const double h = size.y;
|
||||
const double l = aabb.min.x;
|
||||
const double r = aabb.max.x;
|
||||
const double t = aabb.max.y;
|
||||
const double b = aabb.min.y;
|
||||
|
||||
info.xyz[0].x = bearingX;
|
||||
info.xyz[0].y = h - bearingY;
|
||||
info.xyz[0].z = 0;
|
||||
info.uv[0].x = l / m_atlasData->texture.resolution.x;
|
||||
info.uv[0].y = b / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[1].x = bearingX + w;
|
||||
info.xyz[1].y = h - bearingY;
|
||||
info.xyz[1].z = 0;
|
||||
info.uv[1].x = r / m_atlasData->texture.resolution.x;
|
||||
info.uv[1].y = b / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[2].x = bearingX + w;
|
||||
info.xyz[2].y = bearingY; //h - bearingY + h;
|
||||
info.xyz[2].z = 0;
|
||||
info.uv[2].x = r / m_atlasData->texture.resolution.x;
|
||||
info.uv[2].y = t / m_atlasData->texture.resolution.y;
|
||||
|
||||
info.xyz[3].x = bearingX;
|
||||
info.xyz[3].y = bearingY;
|
||||
info.xyz[3].z = 0;
|
||||
info.uv[3].x = l / m_atlasData->texture.resolution.x;
|
||||
info.uv[3].y = t / m_atlasData->texture.resolution.y;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user