Add scale for label

This commit is contained in:
Georg Hagen
2025-02-04 23:21:13 +01:00
parent 4a15c06c6f
commit 0d2bcbbdf6
5 changed files with 22 additions and 21 deletions

View File

@@ -167,7 +167,7 @@ namespace OpenVulkano::Scene
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, float scale)
{
if (text.empty()) return;
if (m_vertexBuffer.data) throw std::runtime_error("Text has already been initialized");
@@ -180,8 +180,7 @@ namespace OpenVulkano::Scene
TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len);
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
float cursorX = pos.x;
float cursorY = pos.y;
Math::Vector2f cursor = pos / scale;
Math::Vector2f bmin(pos), bmax(pos);
float prevGlyphXBound = -INFINITY;
const float heightBetweenLines = GetHeightBetweenLines(text);
@@ -191,7 +190,7 @@ namespace OpenVulkano::Scene
uint32_t c = utf8::next(begin, end);
if (IsSpecialCharacter(c))
{
HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursorX, cursorY, prevGlyphXBound);
HandleSpecialCharacter(symbols, c, heightBetweenLines, pos.x, cursor.x, cursor.y, prevGlyphXBound);
continue;
}
@@ -208,31 +207,32 @@ namespace OpenVulkano::Scene
const bool isBitmap = m_atlasData->GetAtlasType().IsBitmap();
if (prevGlyphXBound != -INFINITY && !isZeroLenGlyph && !isBitmap)
{
offset = prevGlyphXBound - (info.pos[0].x + cursorX);
offset = prevGlyphXBound - (info.pos[0].x + cursor.x);
}
for (int i = 0; i < 4; i++)
{
vertices->position[i].x = info.pos[i].x + cursorX + offset;
vertices->position[i].x = info.pos[i].x + cursor.x + offset;
vertices->uv[i] = info.uv[i];
if (i < 2)
{
vertices->position[i].y = cursorY - info.pos[i].y;
vertices->position[i].y = cursor.y - info.pos[i].y;
}
else
{
vertices->position[i].y = cursorY + info.pos[i].y;
vertices->position[i].y = cursor.y + info.pos[i].y;
}
vertices->color = m_cfg.textColor;
vertices->background = m_cfg.backgroundColor;
vertices->position[i] *= scale;
}
// 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);
cursor.x = std::max(cursor.x, 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;
cursor.x += info.advance + (isBitmap ? 0.05 : 0);
prevGlyphXBound = isZeroLenGlyph ? cursor.x : vertices->position[2].x / scale;
if (!m_symbolCount) bmin.x = vertices->position[0].x;
bmax.x = std::max(bmax.x, vertices->position[1].x);