diff --git a/examples/ExampleApps/TextExampleApp.cpp b/examples/ExampleApps/TextExampleApp.cpp index 0558dc4..55d6099 100644 --- a/examples/ExampleApps/TextExampleApp.cpp +++ b/examples/ExampleApps/TextExampleApp.cpp @@ -55,6 +55,9 @@ namespace OpenVulkano texts.push_back(std::make_pair("Unsupported glyphs \u1E30\u1E31 are coming", TextConfig())); texts.push_back(std::make_pair("AAAA, ____", TextConfig())); texts.push_back(std::make_pair("0123456789", TextConfig())); + texts.push_back(std::make_pair("One\tTwo\t\tThree", TextConfig())); + texts.push_back(std::make_pair("Vertical\vTabs\vHere", TextConfig())); + texts.push_back(std::make_pair("\"\\\"", TextConfig())); texts.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig())); texts[1].second.backgroundColor.a = 255; @@ -86,9 +89,9 @@ namespace OpenVulkano auto bitmapMetadataInfo = resourceLoader.GetResource("bitmap_atlas_packed.png"); auto bitmapSubpixelRenderingMetadataInfo = resourceLoader.GetResource("bitmap_subpixel_atlas_packed.png"); #endif - for (int i = 0, xOffset = -5; i < atlasesCount; i++, xOffset += 20) { + float totalH = 0; for (int j = 0; j < texts.size(); j++) { TextDrawable* t = nullptr; @@ -139,9 +142,10 @@ namespace OpenVulkano m_drawablesPool[nodeIdx].reset(t); m_nodesPool[nodeIdx].Init(); m_nodesPool[nodeIdx].SetMatrix( - Math::Utils::translate(glm::mat4x4(1.f), Vector3f(xOffset, 2 - j * 2, 0))); + Math::Utils::translate(glm::mat4x4(1.f), Vector3f(xOffset, 2.f - totalH - (0.5f * j), 0))); m_nodesPool[nodeIdx].AddDrawable(m_drawablesPool[nodeIdx].get()); m_scene.GetRoot()->AddChild(&m_nodesPool[nodeIdx]); + totalH += t->GetBoundingBox().max.y - t->GetBoundingBox().min.y; } } diff --git a/openVulkanoCpp/Scene/TextDrawable.cpp b/openVulkanoCpp/Scene/TextDrawable.cpp index 0181f6a..3178384 100644 --- a/openVulkanoCpp/Scene/TextDrawable.cpp +++ b/openVulkanoCpp/Scene/TextDrawable.cpp @@ -22,6 +22,33 @@ namespace OpenVulkano::Scene Shader DEFAULT_SHADER_BITMAP_SUBPIXEL = TextDrawable::MakeDefaultShader(FontAtlasType::BITMAP_SUBPIXEL); Shader DEFAULT_SHADER_SDF = TextDrawable::MakeDefaultShader(FontAtlasType::SDF); Shader DEFAULT_SHADER_MSDF = TextDrawable::MakeDefaultShader(FontAtlasType::MSDF); + + void HandleSpecialCharacter(const std::map& symbols, uint32_t c, float heightBetweenLines, + float initialPosX, float& cursorX, float& cursorY, float& prevGlyphXBound) + { + if (c == '\n') + { + cursorY -= heightBetweenLines; + prevGlyphXBound = -INFINITY; + cursorX = initialPosX; + } + else if (c == '\t') + { + if (!symbols.contains(' ')) + { + Logger::RENDER->warn("Tab special character can't be handled since no space glyph is available", c); + return; + } + const GlyphInfo& info = symbols.at(' '); + cursorX += info.advance * 4; + prevGlyphXBound = cursorX; + } + else if (c == '\v') + { + cursorY -= heightBetweenLines; + } + + } } Shader TextDrawable::MakeDefaultShader(const FontAtlasType type) @@ -79,11 +106,11 @@ namespace OpenVulkano::Scene return m_atlasData->GetGlyphs().begin()->first; } - std::vector TextDrawable::GetHeightsBetweenLines(const std::string& text) const + float TextDrawable::GetHeightBetweenLines(const std::string& text) const { if (!m_cfg.minimalSpacingBetweenMultipleLines) { - return {}; + return m_atlasData->GetLineHeight(); } const std::map& symbols = m_atlasData->GetGlyphs(); const uint32_t fallbackGlyph = GetFallbackGlyph(); @@ -97,7 +124,7 @@ namespace OpenVulkano::Scene for (auto begin = text.begin(), end = text.end(); begin != end;) { uint32_t c = utf8::next(begin, end); - if (c == '\n') + if (c == '\n' || c == '\v') { isMultiline = true; if (prevLineHeightBelowBaseline != -INFINITY) @@ -125,7 +152,19 @@ namespace OpenVulkano::Scene heightBetweenLines.push_back(std::abs(prevLineHeightBelowBaseline) + std::abs(currentLineHeightAboveBaseline) + extraOffset); } - return heightBetweenLines; + + if (heightBetweenLines.empty()) + { + return 0; + } + + float maxH = *std::max_element(heightBetweenLines.begin(), heightBetweenLines.end()); + return std::min(m_atlasData->GetLineHeight(), maxH); + } + + bool TextDrawable::IsSpecialCharacter(uint32_t c) const + { + return c == '\t' || c == '\n' || c == '\v'; } void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos) @@ -141,33 +180,21 @@ namespace OpenVulkano::Scene TextGlyphVertex* vertices = m_vertexBuffer.Init(len); const std::map& symbols = m_atlasData->GetGlyphs(); - double cursorX = pos.x; - double posY = pos.y; + float cursorX = pos.x; + float cursorY = pos.y; Math::Vector2f bmin(pos), bmax(pos); float prevGlyphXBound = -INFINITY; - int currentLine = 0; - const std::vector heightsBetweenLines = GetHeightsBetweenLines(text); + const float heightBetweenLines = GetHeightBetweenLines(text); for (auto begin = text.begin(), end = text.end(); begin != end;) { uint32_t c = utf8::next(begin, end); - if (c == '\n') + if (IsSpecialCharacter(c)) { - // use fixed line height - if (heightsBetweenLines.empty()) - { - posY -= m_atlasData->GetLineHeight(); - } - else - { - posY -= heightsBetweenLines[currentLine++]; - } - prevGlyphXBound = -INFINITY; - cursorX = pos.x; + HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursorX, cursorY, prevGlyphXBound); continue; } - // TODO handle special chars - + if (!symbols.contains(c)) { Logger::RENDER->warn("Could not find glyph for character {}, using fallback", c); @@ -190,15 +217,19 @@ namespace OpenVulkano::Scene vertices->uv[i] = info.uv[i]; if (i < 2) { - vertices->position[i].y = posY - info.pos[i].y; + vertices->position[i].y = cursorY - info.pos[i].y; } else { - vertices->position[i].y = posY + info.pos[i].y; + vertices->position[i].y = cursorY + info.pos[i].y; } vertices->color = m_cfg.textColor; vertices->background = m_cfg.backgroundColor; } + + // somehow it's possible that cursorX can be less than prevGlyphXBound for sdf atlases + // e.g. string `A, _` where space is not noticeable in the resulting output + cursorX = std::max(cursorX, prevGlyphXBound); // slight offset for bitmap atlas since it's tightly packed without any extra padding, while sdf has additional padding cursorX += info.advance + (isBitmap ? 0.05 : 0); prevGlyphXBound = isZeroLenGlyph ? cursorX : vertices->position[2].x; diff --git a/openVulkanoCpp/Scene/TextDrawable.hpp b/openVulkanoCpp/Scene/TextDrawable.hpp index cad19dc..90ea18b 100644 --- a/openVulkanoCpp/Scene/TextDrawable.hpp +++ b/openVulkanoCpp/Scene/TextDrawable.hpp @@ -42,7 +42,8 @@ namespace OpenVulkano::Scene TextConfig m_cfg; uint32_t GetFallbackGlyph() const; - std::vector GetHeightsBetweenLines(const std::string& text) const; + float GetHeightBetweenLines(const std::string& text) const; + bool IsSpecialCharacter(uint32_t c) const; public: TextDrawable(const TextConfig& config = TextConfig());