reduce spacing between multiple lines
This commit is contained in:
@@ -108,7 +108,9 @@ namespace OpenVulkano
|
|||||||
}
|
}
|
||||||
else if (i == 1)
|
else if (i == 1)
|
||||||
{
|
{
|
||||||
t = new TextDrawable(msdfMetadataInfo, texts[j].second);
|
TextConfig cfg = texts[j].second;
|
||||||
|
cfg.minimalSpacingBetweenMultipleLines = false;
|
||||||
|
t = new TextDrawable(msdfMetadataInfo, cfg);
|
||||||
}
|
}
|
||||||
else if (i == 2)
|
else if (i == 2)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -79,6 +79,55 @@ 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
|
||||||
|
{
|
||||||
|
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)
|
void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos)
|
||||||
{
|
{
|
||||||
if (text.empty()) return;
|
if (text.empty()) return;
|
||||||
@@ -93,16 +142,26 @@ namespace OpenVulkano::Scene
|
|||||||
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
|
const std::map<uint32_t, GlyphInfo>& symbols = m_atlasData->GetGlyphs();
|
||||||
|
|
||||||
double cursorX = pos.x;
|
double cursorX = pos.x;
|
||||||
const double lineHeight = m_atlasData->GetLineHeight();
|
|
||||||
double posY = pos.y;
|
double posY = pos.y;
|
||||||
Math::Vector2f bmin(pos), bmax(pos);
|
Math::Vector2f bmin(pos), bmax(pos);
|
||||||
float prevGlyphXBound = -INFINITY;
|
float prevGlyphXBound = -INFINITY;
|
||||||
|
int currentLine = 0;
|
||||||
|
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 (c == '\n')
|
||||||
{
|
{
|
||||||
posY -= lineHeight;
|
// use fixed line height
|
||||||
|
if (heightsBetweenLines.empty())
|
||||||
|
{
|
||||||
|
posY -= m_atlasData->GetLineHeight();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
posY -= heightsBetweenLines[currentLine++];
|
||||||
|
}
|
||||||
prevGlyphXBound = -INFINITY;
|
prevGlyphXBound = -INFINITY;
|
||||||
cursorX = pos.x;
|
cursorX = pos.x;
|
||||||
continue;
|
continue;
|
||||||
@@ -129,8 +188,14 @@ namespace OpenVulkano::Scene
|
|||||||
{
|
{
|
||||||
vertices->position[i].x = info.pos[i].x + cursorX + offset;
|
vertices->position[i].x = info.pos[i].x + cursorX + offset;
|
||||||
vertices->uv[i] = info.uv[i];
|
vertices->uv[i] = info.uv[i];
|
||||||
if (i < 2) vertices->position[i].y = posY - info.pos[i].y;
|
if (i < 2)
|
||||||
else vertices->position[i].y = posY + info.pos[i].y;
|
{
|
||||||
|
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->color = m_cfg.textColor;
|
||||||
vertices->background = m_cfg.backgroundColor;
|
vertices->background = m_cfg.backgroundColor;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ namespace OpenVulkano::Scene
|
|||||||
{
|
{
|
||||||
Math::Vector4uc textColor = { 255, 255, 255, 255 };
|
Math::Vector4uc textColor = { 255, 255, 255, 255 };
|
||||||
Math::Vector4uc backgroundColor = { 0, 255, 0, 0 };
|
Math::Vector4uc backgroundColor = { 0, 255, 0, 0 };
|
||||||
|
bool minimalSpacingBetweenMultipleLines = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TextGlyphVertex
|
struct TextGlyphVertex
|
||||||
@@ -41,6 +42,7 @@ 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;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TextDrawable(const TextConfig& config = TextConfig());
|
TextDrawable(const TextConfig& config = TextConfig());
|
||||||
|
|||||||
Reference in New Issue
Block a user