reduce spacing between multiple lines

This commit is contained in:
ohyzha
2025-01-23 13:21:02 +02:00
parent 4832c3f6f2
commit 3a874f1ea6
3 changed files with 74 additions and 5 deletions

View File

@@ -79,6 +79,55 @@ namespace OpenVulkano::Scene
return m_atlasData->GetGlyphs().begin()->first;
}
std::vector<float> TextDrawable::GetHeightsBetweenLines(const std::string& text) const
{
if (!m_cfg.minimalSpacingBetweenMultipleLines)
{
return {};
}
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
const uint32_t fallbackGlyph = GetFallbackGlyph();
std::vector<float> heightBetweenLines;
float currentLineHeightAboveBaseline = 0;
float currentLineHeightBelowBaseline = 0;
float prevLineHeightBelowBaseline = -INFINITY;
float extraOffset = m_atlasData->GetAtlasType().IsBitmap() ? 0.1 : 0.05;
bool isMultiline = false;
for (auto begin = text.begin(), end = text.end(); begin != end;)
{
uint32_t c = utf8::next(begin, end);
if (c == '\n')
{
isMultiline = true;
if (prevLineHeightBelowBaseline != -INFINITY)
{
heightBetweenLines.push_back(std::abs(prevLineHeightBelowBaseline)
+ std::abs(currentLineHeightAboveBaseline) + extraOffset);
prevLineHeightBelowBaseline = currentLineHeightBelowBaseline;
}
prevLineHeightBelowBaseline = currentLineHeightBelowBaseline;
currentLineHeightAboveBaseline = currentLineHeightBelowBaseline = 0;
continue;
}
if (!symbols.contains(c))
{
c = fallbackGlyph;
}
const GlyphInfo& info = symbols.at(c);
currentLineHeightAboveBaseline = std::max(currentLineHeightAboveBaseline, info.pos[2].y);
currentLineHeightBelowBaseline = std::min(currentLineHeightBelowBaseline, -info.pos[0].y);
}
if (isMultiline && text.back() != '\n')
{
heightBetweenLines.push_back(std::abs(prevLineHeightBelowBaseline)
+ std::abs(currentLineHeightAboveBaseline) + extraOffset);
}
return heightBetweenLines;
}
void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos)
{
if (text.empty()) return;
@@ -93,16 +142,26 @@ namespace OpenVulkano::Scene
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
double cursorX = pos.x;
const double lineHeight = m_atlasData->GetLineHeight();
double posY = pos.y;
Math::Vector2f bmin(pos), bmax(pos);
float prevGlyphXBound = -INFINITY;
int currentLine = 0;
const std::vector<float> heightsBetweenLines = GetHeightsBetweenLines(text);
for (auto begin = text.begin(), end = text.end(); begin != end;)
{
uint32_t c = utf8::next(begin, end);
if (c == '\n')
{
posY -= lineHeight;
// use fixed line height
if (heightsBetweenLines.empty())
{
posY -= m_atlasData->GetLineHeight();
}
else
{
posY -= heightsBetweenLines[currentLine++];
}
prevGlyphXBound = -INFINITY;
cursorX = pos.x;
continue;
@@ -129,8 +188,14 @@ namespace OpenVulkano::Scene
{
vertices->position[i].x = info.pos[i].x + cursorX + offset;
vertices->uv[i] = info.uv[i];
if (i < 2) vertices->position[i].y = posY - info.pos[i].y;
else vertices->position[i].y = posY + info.pos[i].y;
if (i < 2)
{
vertices->position[i].y = posY - info.pos[i].y;
}
else
{
vertices->position[i].y = posY + info.pos[i].y;
}
vertices->color = m_cfg.textColor;
vertices->background = m_cfg.backgroundColor;
}

View File

@@ -21,6 +21,7 @@ namespace OpenVulkano::Scene
{
Math::Vector4uc textColor = { 255, 255, 255, 255 };
Math::Vector4uc backgroundColor = { 0, 255, 0, 0 };
bool minimalSpacingBetweenMultipleLines = true;
};
struct TextGlyphVertex
@@ -41,6 +42,7 @@ namespace OpenVulkano::Scene
TextConfig m_cfg;
uint32_t GetFallbackGlyph() const;
std::vector<float> GetHeightsBetweenLines(const std::string& text) const;
public:
TextDrawable(const TextConfig& config = TextConfig());