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

@@ -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<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)
@@ -79,11 +106,11 @@ namespace OpenVulkano::Scene
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)
{
return {};
return m_atlasData->GetLineHeight();
}
const std::map<uint32_t, GlyphInfo>& 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<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)
@@ -141,33 +180,21 @@ namespace OpenVulkano::Scene
TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len);
const std::map<uint32_t, GlyphInfo>& 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<float> 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;

View File

@@ -42,7 +42,8 @@ namespace OpenVulkano::Scene
TextConfig m_cfg;
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:
TextDrawable(const TextConfig& config = TextConfig());