rework label drawable and text drawable's API

This commit is contained in:
ohyzha
2024-08-26 17:42:43 +03:00
parent a2923966fa
commit e2ae1687ac
13 changed files with 160 additions and 217 deletions

View File

@@ -8,7 +8,7 @@
#include "FontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include "Scene/AtlasMetadata.hpp"
#include "Scene/AtlasData.hpp"
#include <msdfgen.h>
#include <msdfgen-ext.h>
#include <msdf-atlas-gen/msdf-atlas-gen.h>
@@ -120,7 +120,7 @@ namespace OpenVulkano::Scene
void FontAtlasGenerator<Channels>::SaveAtlasMetadataInfo(const std::string& outputFile,
bool packIntoSingleFile) const
{
if (m_symbols.empty())
if (m_atlasData->glyphs.empty())
{
Logger::DATA->info("No glyphs loaded. Nothing to save.");
return;
@@ -134,9 +134,9 @@ namespace OpenVulkano::Scene
SavePng(fileName);
}
std::fstream fs(fileName.c_str(), std::ios_base::out | std::ios_base::binary | (packedFlag ? std::ios_base::app : std::ios_base::trunc));
fs.write(reinterpret_cast<const char*>(&m_meta), sizeof(AtlasMetadata));
fs.write(reinterpret_cast<const char*>(&m_atlasData->meta), sizeof(AtlasMetadata));
uint64_t metadataBytes = sizeof(AtlasMetadata);
for (const auto& [key, val] : m_symbols)
for (const auto& [key, val]: m_atlasData->glyphs)
{
fs.write(reinterpret_cast<const char*>(&key), sizeof(uint32_t));
fs.write(reinterpret_cast<const char*>(&val), sizeof(GlyphInfo));
@@ -181,7 +181,8 @@ namespace OpenVulkano::Scene
void FontAtlasGenerator<Channels>::Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
const std::optional<std::string>& pngOutput)
{
m_symbols.clear();
m_atlasData.reset(new AtlasData);
std::vector<GlyphGeometry> glyphsGeometry;
// FontGeometry is a helper class that loads a set of glyphs from a single font.
FontGeometry fontGeometry(&glyphsGeometry);
@@ -219,9 +220,12 @@ namespace OpenVulkano::Scene
if constexpr (Channels == 3)
{
// store RGB as RGBA
m_atlasData->img = std::make_unique<Image::Image>();
m_atlasData->img->data = Array<uint8_t>(width * height * 4);
m_atlasData->img->resolution = Math::Vector3ui(width, height, 1);
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8G8B8A8_UNORM;
const BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
msdfgen::Bitmap<msdfgen::byte, 4> bitmap(width, height);
msdfgen::byte* data = static_cast<msdfgen::byte*>(bitmap);
msdfgen::byte* data = static_cast<msdfgen::byte*>(m_atlasData->img->data.Data());
for (size_t srcPos = 0, dstPos = 0; srcPos < width * height * 3; srcPos += 3, dstPos += 4)
{
data[dstPos] = storage.pixels[srcPos];
@@ -229,19 +233,23 @@ namespace OpenVulkano::Scene
data[dstPos + 2] = storage.pixels[srcPos + 2];
data[dstPos + 3] = 255;
}
m_atlasStorage = std::move(bitmap);
}
else
else
{
m_atlasStorage = generator.atlasStorage();
m_atlasData->img = std::make_unique<Image::Image>();
m_atlasData->img->data = Array<uint8_t>(width * height);
m_atlasData->img->resolution = Math::Vector3ui(width, height, 1);
m_atlasData->img->dataFormat = OpenVulkano::DataFormat::R8_UNORM;
const msdfgen::BitmapConstRef<msdfgen::byte, 1>& storage = generator.atlasStorage();
memcpy(m_atlasData->img->data.Data(), storage.pixels, width * height);
}
m_atlasTex.resolution = Math::Vector3ui(m_atlasStorage.width(), m_atlasStorage.height(), 1);
m_atlasTex.textureBuffer = (msdfgen::byte*) m_atlasStorage;
m_atlasTex.format = (channelsCount == 1 ? OpenVulkano::DataFormat::R8_UNORM : OpenVulkano::DataFormat::R8G8B8A8_UNORM);
m_atlasTex.size = m_atlasStorage.width() * m_atlasStorage.height() * channelsCount;
m_meta.lineHeight = fontGeometry.getMetrics().lineHeight;
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 = width * height * channelsCount;
m_atlasData->meta.lineHeight = fontGeometry.getMetrics().lineHeight;
m_atlasData->meta.atlasType = channelsCount == 1 ? FontAtlasType::SDF : FontAtlasType::MSDF;
struct Bbox
{
@@ -250,7 +258,7 @@ namespace OpenVulkano::Scene
for (const auto& glyph: glyphsGeometry)
{
GlyphInfo& info = m_symbols[glyph.getCodepoint()];
GlyphInfo& info = m_atlasData->glyphs[glyph.getCodepoint()];
const GlyphBox& glyphBox = generator.getLayout()[idx++];
Bbox glyphBaselineBbox, glyphAtlasBbox;
@@ -269,26 +277,26 @@ namespace OpenVulkano::Scene
info.xyz[0].x = bearingX;
info.xyz[0].y = h - bearingY;
info.xyz[0].z = 0;
info.uv[0].x = l / m_atlasTex.resolution.x;
info.uv[0].y = b / m_atlasTex.resolution.y;
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_atlasTex.resolution.x;
info.uv[1].y = b / m_atlasTex.resolution.y;
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_atlasTex.resolution.x;
info.uv[2].y = t / m_atlasTex.resolution.y;
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_atlasTex.resolution.x;
info.uv[3].y = t / m_atlasTex.resolution.y;
info.uv[3].x = l / m_atlasData->texture.resolution.x;
info.uv[3].y = t / m_atlasData->texture.resolution.y;
info.advance = glyphBox.advance;
}
@@ -304,13 +312,13 @@ namespace OpenVulkano::Scene
stbi_flip_vertically_on_write(1);
if (std::filesystem::path(output).extension() == ".png")
{
stbi_write_png(output.c_str(), m_atlasStorage.width(), m_atlasStorage.height(), channelsCount,
m_atlasStorage.operator const msdfgen::byte*(), channelsCount * m_atlasStorage.width());
stbi_write_png(output.c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y,
channelsCount, m_atlasData->img->data.Data(), channelsCount * m_atlasData->img->resolution.x);
}
else
{
stbi_write_png((output + ".png").c_str(), m_atlasStorage.width(), m_atlasStorage.height(), channelsCount,
m_atlasStorage.operator const msdfgen::byte*(), channelsCount * m_atlasStorage.width());
stbi_write_png((output + ".png").c_str(), m_atlasData->img->resolution.x, m_atlasData->img->resolution.y,
channelsCount, m_atlasData->img->data.Data(), channelsCount * m_atlasData->img->resolution.x);
}
}
template class FontAtlasGenerator<1>;