implement multiline text rendering

This commit is contained in:
ohyzha
2024-08-06 10:22:07 +03:00
parent afccf5dee0
commit cb409268a8
7 changed files with 49 additions and 25 deletions

View File

@@ -56,6 +56,7 @@ namespace OpenVulkano
texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig()));
texts.push_back(std::make_pair("Hello, World!", TextConfig()));
texts.push_back(std::make_pair("\u0410\u0411\u0412\u041F", TextConfig()));
texts.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig()));
texts[0].second.applyBorder = true;
texts[1].second.backgroundColor.a = 1;
@@ -73,7 +74,7 @@ namespace OpenVulkano
}
#ifdef MSDFGEN_AVAILABLE
m_atlasGenerator.GenerateAtlas(fontPath, charset);
m_atlasGenerator.GenerateAtlas(fontPath, charset, "roboto-regular-atlas.png");
#endif
for (int i = 0; i < texts.size(); i++)
@@ -81,9 +82,8 @@ namespace OpenVulkano
#ifdef MSDFGEN_AVAILABLE
TextDrawable* t = new TextDrawable(&m_atlasGenerator, texts[i].second);
#else
auto metadataInfoPath = resourceLoader.GetResourcePath("atlas_metadata");
auto metadataInfo = resourceLoader.GetResource("atlas_metadata");
auto data = resourceLoader.GetResource("roboto-regular-atlas.png");
auto metadataInfo = TextDrawable::ReadAtlasMetadataFile(metadataInfoPath);
Image::ImageLoaderPng loader;
static auto image = loader.loadData(reinterpret_cast<uint8_t*>(data.Data()), data.Size());
static Texture tex;

View File

@@ -18,4 +18,11 @@ namespace OpenVulkano::Scene
Math::Vector2f_SIMD uv[4] = {};
double advance = 0;
};
struct AtlasMetadata
{
// vertical difference between baselines
double lineHeight = 0;
};
}

View File

@@ -55,6 +55,7 @@ namespace OpenVulkano::Scene
return;
}
std::fstream fs(outputFile.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
fs.write(reinterpret_cast<const char*>(&m_meta), sizeof(AtlasMetadata));
for (const auto& [key, val] : m_symbols)
{
fs.write(reinterpret_cast<const char*>(&key), sizeof(uint32_t));
@@ -93,6 +94,8 @@ namespace OpenVulkano::Scene
m_atlasTex.format = OpenVulkano::DataFormat::R8_UNORM;
m_atlasTex.size = storage.width * storage.height * 1; // 1 channel
m_meta.lineHeight = fontGeometry.getMetrics().lineHeight;
struct Bbox
{
double l = 0, r = 0, t = 0, b = 0;

View File

@@ -30,13 +30,15 @@ namespace OpenVulkano::Scene
const std::optional<std::string>& pngOutput = std::nullopt);
void SaveAtlasMetadataInfo(const std::string& outputFile) const;
const Texture& GetAtlas() const { return m_atlasTex; }
std::map<uint32_t, GlyphInfo>& GetAtlasInfo() { return m_symbols; }
std::map<uint32_t, GlyphInfo>& GetGlyphsInfo() { return m_symbols; }
AtlasMetadata& GetAtlasMetadata() { return m_meta; }
private:
void Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
const std::optional<std::string>& pngOutput);
private:
ImmediateAtlasGenerator<float, 1, sdfGenerator, BitmapAtlasStorage<msdfgen::byte, 1>> m_generator;
Texture m_atlasTex;
AtlasMetadata m_meta;
std::map<uint32_t, GlyphInfo> m_symbols;
};
}

View File

@@ -40,28 +40,29 @@ namespace OpenVulkano::Scene
return textDefaultShader;
}
std::map<uint32_t, GlyphInfo> TextDrawable::ReadAtlasMetadataFile(const std::string& atlasMetadataFile)
TextDrawable::TextDrawable(const std::string& atlasMetadataFile, Texture* atlasTex, const TextConfig& config)
: TextDrawable(OpenVulkano::Utils::ReadFile(atlasMetadataFile), atlasTex, config)
{
OpenVulkano::Array<char> data = OpenVulkano::Utils::ReadFile(atlasMetadataFile);
return ReadAtlasMetadata(data);
}
std::map<uint32_t, GlyphInfo> TextDrawable::ReadAtlasMetadata(const Array<char>& atlasMetadata)
TextDrawable::TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config)
{
size_t len = atlasMetadata.Size();
size_t read_bytes = 0;
uint32_t unicode = 0;
GlyphInfo info;
std::map<uint32_t, GlyphInfo> glyphs;
std::memcpy(&m_meta, atlasMetadata.Data(), sizeof(AtlasMetadata));
read_bytes += sizeof(AtlasMetadata);
while (read_bytes < len)
{
std::memcpy(&unicode, atlasMetadata.Data() + read_bytes, sizeof(uint32_t));
read_bytes += sizeof(uint32_t);
GlyphInfo& info = m_glyphs[unicode];
std::memcpy(&info, atlasMetadata.Data() + read_bytes, sizeof(GlyphInfo));
read_bytes += sizeof(GlyphInfo);
glyphs[unicode] = std::move(info);
}
return glyphs;
m_material.texture = atlasTex;
m_cfg = config;
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
}
TextDrawable::TextDrawable(const std::map<uint32_t, GlyphInfo>& glyphData, Texture* atlasTex,
@@ -79,7 +80,7 @@ namespace OpenVulkano::Scene
TextDrawable::TextDrawable(FontAtlasGenerator* fontAtlasGenerator, const TextConfig& config)
{
if (!fontAtlasGenerator) { throw std::runtime_error("FontAtlasGenerator is nullptr"); }
if (fontAtlasGenerator->GetAtlasInfo().empty()) { throw std::runtime_error("Glyphs are not loaded"); }
if (fontAtlasGenerator->GetGlyphsInfo().empty()) { throw std::runtime_error("Glyphs are not loaded"); }
m_fontAtlasGenerator = fontAtlasGenerator;
m_cfg = config;
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
@@ -101,7 +102,7 @@ namespace OpenVulkano::Scene
// TODO: better implementation to decide what to use: data from atlas generator or data read from file
// we have msdf but loaded glyphs metadata from file before
std::optional<std::reference_wrapper<std::map<unicode_t, GlyphInfo>>> glyphData;
if (m_fontAtlasGenerator->GetAtlasInfo().empty() && !m_glyphs.empty())
if (m_fontAtlasGenerator->GetGlyphsInfo().empty() && !m_glyphs.empty())
{
// texture is set in ctor
glyphData = m_glyphs;
@@ -110,19 +111,29 @@ namespace OpenVulkano::Scene
{
// just in case if FontAtlasGenerator changed it's atlas
m_material.texture = const_cast<Texture*>(&m_fontAtlasGenerator->GetAtlas());
glyphData = m_fontAtlasGenerator->GetAtlasInfo();
glyphData = m_fontAtlasGenerator->GetGlyphsInfo();
}
std::map<unicode_t, GlyphInfo>& symbols = glyphData.value();
AtlasMetadata& meta = m_fontAtlasGenerator->GetAtlasMetadata();
#else
std::map<unicode_t, GlyphInfo>& symbols = m_glyphs;
AtlasMetadata& meta = m_meta;
#endif
const Texture& atlasTex = *m_material.texture;
double cursorX = pos.x;
auto begin = text.begin();
auto end = text.end();
const double lineHeight = meta.lineHeight;
double posY = pos.y;
for (size_t i = 0; begin != end; i++)
{
uint32_t c = utf8::next(begin, end);
if (c == '\n')
{
posY -= lineHeight;
cursorX = pos.x;
continue;
}
if (symbols.find(c) != symbols.end())
{
uint32_t vIdx = i * 4;
@@ -131,25 +142,25 @@ namespace OpenVulkano::Scene
// left bottom
m_geometry.vertices[vIdx].position.x = info.xyz[0].x + cursorX;
m_geometry.vertices[vIdx].position.y = pos.y - info.xyz[0].y;
m_geometry.vertices[vIdx].position.y = posY - info.xyz[0].y;
m_geometry.vertices[vIdx].position.z = info.xyz[0].z;
m_geometry.vertices[vIdx].textureCoordinates = Math::Vector3f(info.uv[0], 0);
// right bottom
m_geometry.vertices[vIdx + 1].position.x = info.xyz[1].x + cursorX;
m_geometry.vertices[vIdx + 1].position.y = pos.y - info.xyz[1].y;
m_geometry.vertices[vIdx + 1].position.y = posY - info.xyz[1].y;
m_geometry.vertices[vIdx + 1].position.z = info.xyz[1].z;
m_geometry.vertices[vIdx + 1].textureCoordinates = Math::Vector3f(info.uv[1], 0);
// top right
m_geometry.vertices[vIdx + 2].position.x = info.xyz[2].x + cursorX;
m_geometry.vertices[vIdx + 2].position.y = pos.y + info.xyz[2].y;
m_geometry.vertices[vIdx + 2].position.y = posY + info.xyz[2].y;
m_geometry.vertices[vIdx + 2].position.z = info.xyz[2].z;
m_geometry.vertices[vIdx + 2].textureCoordinates = Math::Vector3f(info.uv[2], 0);
// top left
m_geometry.vertices[vIdx + 3].position.x = info.xyz[3].x + cursorX;
m_geometry.vertices[vIdx + 3].position.y = pos.y + info.xyz[3].y;
m_geometry.vertices[vIdx + 3].position.y = posY + info.xyz[3].y;
m_geometry.vertices[vIdx + 3].position.z = info.xyz[3].z;
m_geometry.vertices[vIdx + 3].textureCoordinates = Math::Vector3f(info.uv[3], 0);
m_geometry.SetIndices(indices, 6, 6 * i);

View File

@@ -48,8 +48,8 @@ namespace OpenVulkano::Scene
{
public:
static Shader& GetDefaultShader();
static std::map<uint32_t, GlyphInfo> ReadAtlasMetadataFile(const std::string& atlasMetadataFile);
static std::map<uint32_t, GlyphInfo> ReadAtlasMetadata(const Array<char>& atlasMetadata);
TextDrawable(const std::string& atlasMetadataFile, Texture* atlasTex, const TextConfig& config = TextConfig());
TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config = TextConfig());
TextDrawable(const std::map<uint32_t, GlyphInfo>& glyphData, Texture* atlasTex, const TextConfig& config = TextConfig());
#ifdef MSDFGEN_AVAILABLE
TextDrawable(FontAtlasGenerator* fontAtlasGenerator, const TextConfig& config = TextConfig());
@@ -62,7 +62,7 @@ namespace OpenVulkano::Scene
#ifdef MSDFGEN_AVAILABLE
void SetFontAtlasGenerator(FontAtlasGenerator* fontAtlasGenerator)
{
if (!fontAtlasGenerator || fontAtlasGenerator->GetAtlasInfo().empty())
if (!fontAtlasGenerator || fontAtlasGenerator->GetGlyphsInfo().empty())
{
Logger::RENDER->error("FontAtlasGenerator is either nullptr or doesn't contain glyphs info");
return;
@@ -76,6 +76,7 @@ namespace OpenVulkano::Scene
Material m_material;
UniformBuffer m_uniBuffer;
std::map<uint32_t, GlyphInfo> m_glyphs;
AtlasMetadata m_meta;
#ifdef MSDFGEN_AVAILABLE
FontAtlasGenerator* m_fontAtlasGenerator = nullptr;
#endif