fix inconsistent spacing for multiline text and support special characters

This commit is contained in:
ohyzha
2025-01-23 16:02:00 +02:00
parent 3a874f1ea6
commit 0583a7e25a
3 changed files with 63 additions and 27 deletions

View File

@@ -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("Unsupported glyphs \u1E30\u1E31 are coming", TextConfig()));
texts.push_back(std::make_pair("AAAA, ____", TextConfig())); texts.push_back(std::make_pair("AAAA, ____", TextConfig()));
texts.push_back(std::make_pair("0123456789", 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.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig()));
texts[1].second.backgroundColor.a = 255; texts[1].second.backgroundColor.a = 255;
@@ -86,9 +89,9 @@ namespace OpenVulkano
auto bitmapMetadataInfo = resourceLoader.GetResource("bitmap_atlas_packed.png"); auto bitmapMetadataInfo = resourceLoader.GetResource("bitmap_atlas_packed.png");
auto bitmapSubpixelRenderingMetadataInfo = resourceLoader.GetResource("bitmap_subpixel_atlas_packed.png"); auto bitmapSubpixelRenderingMetadataInfo = resourceLoader.GetResource("bitmap_subpixel_atlas_packed.png");
#endif #endif
for (int i = 0, xOffset = -5; i < atlasesCount; i++, xOffset += 20) for (int i = 0, xOffset = -5; i < atlasesCount; i++, xOffset += 20)
{ {
float totalH = 0;
for (int j = 0; j < texts.size(); j++) for (int j = 0; j < texts.size(); j++)
{ {
TextDrawable* t = nullptr; TextDrawable* t = nullptr;
@@ -139,9 +142,10 @@ namespace OpenVulkano
m_drawablesPool[nodeIdx].reset(t); m_drawablesPool[nodeIdx].reset(t);
m_nodesPool[nodeIdx].Init(); m_nodesPool[nodeIdx].Init();
m_nodesPool[nodeIdx].SetMatrix( 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_nodesPool[nodeIdx].AddDrawable(m_drawablesPool[nodeIdx].get());
m_scene.GetRoot()->AddChild(&m_nodesPool[nodeIdx]); m_scene.GetRoot()->AddChild(&m_nodesPool[nodeIdx]);
totalH += t->GetBoundingBox().max.y - t->GetBoundingBox().min.y;
} }
} }

View File

@@ -22,6 +22,33 @@ namespace OpenVulkano::Scene
Shader DEFAULT_SHADER_BITMAP_SUBPIXEL = TextDrawable::MakeDefaultShader(FontAtlasType::BITMAP_SUBPIXEL); Shader DEFAULT_SHADER_BITMAP_SUBPIXEL = TextDrawable::MakeDefaultShader(FontAtlasType::BITMAP_SUBPIXEL);
Shader DEFAULT_SHADER_SDF = TextDrawable::MakeDefaultShader(FontAtlasType::SDF); Shader DEFAULT_SHADER_SDF = TextDrawable::MakeDefaultShader(FontAtlasType::SDF);
Shader DEFAULT_SHADER_MSDF = TextDrawable::MakeDefaultShader(FontAtlasType::MSDF); Shader DEFAULT_SHADER_MSDF = TextDrawable::MakeDefaultShader(FontAtlasType::MSDF);
void HandleSpecialCharacter(const std::map<uint32_t, GlyphInfo>& 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) Shader TextDrawable::MakeDefaultShader(const FontAtlasType type)
@@ -79,11 +106,11 @@ namespace OpenVulkano::Scene
return m_atlasData->GetGlyphs().begin()->first; return m_atlasData->GetGlyphs().begin()->first;
} }
std::vector<float> TextDrawable::GetHeightsBetweenLines(const std::string& text) const float TextDrawable::GetHeightBetweenLines(const std::string& text) const
{ {
if (!m_cfg.minimalSpacingBetweenMultipleLines) if (!m_cfg.minimalSpacingBetweenMultipleLines)
{ {
return {}; return m_atlasData->GetLineHeight();
} }
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs(); const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
const uint32_t fallbackGlyph = GetFallbackGlyph(); const uint32_t fallbackGlyph = GetFallbackGlyph();
@@ -97,7 +124,7 @@ namespace OpenVulkano::Scene
for (auto begin = text.begin(), end = text.end(); begin != end;) for (auto begin = text.begin(), end = text.end(); begin != end;)
{ {
uint32_t c = utf8::next(begin, end); uint32_t c = utf8::next(begin, end);
if (c == '\n') if (c == '\n' || c == '\v')
{ {
isMultiline = true; isMultiline = true;
if (prevLineHeightBelowBaseline != -INFINITY) if (prevLineHeightBelowBaseline != -INFINITY)
@@ -125,7 +152,19 @@ namespace OpenVulkano::Scene
heightBetweenLines.push_back(std::abs(prevLineHeightBelowBaseline) heightBetweenLines.push_back(std::abs(prevLineHeightBelowBaseline)
+ std::abs(currentLineHeightAboveBaseline) + extraOffset); + std::abs(currentLineHeightAboveBaseline) + extraOffset);
} }
return heightBetweenLines;
if (heightBetweenLines.empty())
{
return 0;
}
float maxH = *std::max_element(heightBetweenLines.begin(), heightBetweenLines.end());
return std::min<float>(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) void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos)
@@ -141,32 +180,20 @@ namespace OpenVulkano::Scene
TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len); TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len);
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs(); const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
double cursorX = pos.x; float cursorX = pos.x;
double posY = pos.y; float cursorY = pos.y;
Math::Vector2f bmin(pos), bmax(pos); Math::Vector2f bmin(pos), bmax(pos);
float prevGlyphXBound = -INFINITY; float prevGlyphXBound = -INFINITY;
int currentLine = 0; const float heightBetweenLines = GetHeightBetweenLines(text);
const std::vector<float> heightsBetweenLines = GetHeightsBetweenLines(text);
for (auto begin = text.begin(), end = text.end(); begin != end;) for (auto begin = text.begin(), end = text.end(); begin != end;)
{ {
uint32_t c = utf8::next(begin, end); uint32_t c = utf8::next(begin, end);
if (c == '\n') if (IsSpecialCharacter(c))
{ {
// use fixed line height HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursorX, cursorY, prevGlyphXBound);
if (heightsBetweenLines.empty())
{
posY -= m_atlasData->GetLineHeight();
}
else
{
posY -= heightsBetweenLines[currentLine++];
}
prevGlyphXBound = -INFINITY;
cursorX = pos.x;
continue; continue;
} }
// TODO handle special chars
if (!symbols.contains(c)) if (!symbols.contains(c))
{ {
@@ -190,15 +217,19 @@ namespace OpenVulkano::Scene
vertices->uv[i] = info.uv[i]; vertices->uv[i] = info.uv[i];
if (i < 2) if (i < 2)
{ {
vertices->position[i].y = posY - info.pos[i].y; vertices->position[i].y = cursorY - info.pos[i].y;
} }
else 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->color = m_cfg.textColor;
vertices->background = m_cfg.backgroundColor; 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 // 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); cursorX += info.advance + (isBitmap ? 0.05 : 0);
prevGlyphXBound = isZeroLenGlyph ? cursorX : vertices->position[2].x; prevGlyphXBound = isZeroLenGlyph ? cursorX : vertices->position[2].x;

View File

@@ -42,7 +42,8 @@ namespace OpenVulkano::Scene
TextConfig m_cfg; TextConfig m_cfg;
uint32_t GetFallbackGlyph() const; uint32_t GetFallbackGlyph() const;
std::vector<float> GetHeightsBetweenLines(const std::string& text) const; float GetHeightBetweenLines(const std::string& text) const;
bool IsSpecialCharacter(uint32_t c) const;
public: public:
TextDrawable(const TextConfig& config = TextConfig()); TextDrawable(const TextConfig& config = TextConfig());