Merge pull request 'Text improvements' (#191) from text_improvements into master
Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/191 Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
@@ -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("AAAA, ____", 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[1].second.backgroundColor.a = 255;
|
||||
|
||||
@@ -86,9 +89,9 @@ namespace OpenVulkano
|
||||
auto bitmapMetadataInfo = resourceLoader.GetResource("bitmap_atlas_packed.png");
|
||||
auto bitmapSubpixelRenderingMetadataInfo = resourceLoader.GetResource("bitmap_subpixel_atlas_packed.png");
|
||||
#endif
|
||||
|
||||
for (int i = 0, xOffset = -5; i < atlasesCount; i++, xOffset += 20)
|
||||
{
|
||||
float totalH = 0;
|
||||
for (int j = 0; j < texts.size(); j++)
|
||||
{
|
||||
TextDrawable* t = nullptr;
|
||||
@@ -108,7 +111,9 @@ namespace OpenVulkano
|
||||
}
|
||||
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)
|
||||
{
|
||||
@@ -137,9 +142,10 @@ namespace OpenVulkano
|
||||
m_drawablesPool[nodeIdx].reset(t);
|
||||
m_nodesPool[nodeIdx].Init();
|
||||
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_scene.GetRoot()->AddChild(&m_nodesPool[nodeIdx]);
|
||||
totalH += t->GetBoundingBox().max.y - t->GetBoundingBox().min.y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,6 +106,67 @@ namespace OpenVulkano::Scene
|
||||
return m_atlasData->GetGlyphs().begin()->first;
|
||||
}
|
||||
|
||||
float TextDrawable::GetHeightBetweenLines(const std::string& text) const
|
||||
{
|
||||
if (!m_cfg.minimalSpacingBetweenMultipleLines)
|
||||
{
|
||||
return m_atlasData->GetLineHeight();
|
||||
}
|
||||
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' || c == '\v')
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (text.empty()) return;
|
||||
@@ -92,23 +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;
|
||||
const double lineHeight = m_atlasData->GetLineHeight();
|
||||
double posY = pos.y;
|
||||
float cursorX = pos.x;
|
||||
float cursorY = pos.y;
|
||||
Math::Vector2f bmin(pos), bmax(pos);
|
||||
float prevGlyphXBound = -INFINITY;
|
||||
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))
|
||||
{
|
||||
posY -= lineHeight;
|
||||
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);
|
||||
@@ -129,11 +215,21 @@ 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 = cursorY - info.pos[i].y;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
|
||||
@@ -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,8 @@ namespace OpenVulkano::Scene
|
||||
TextConfig m_cfg;
|
||||
|
||||
uint32_t GetFallbackGlyph() const;
|
||||
float GetHeightBetweenLines(const std::string& text) const;
|
||||
bool IsSpecialCharacter(uint32_t c) const;
|
||||
|
||||
public:
|
||||
TextDrawable(const TextConfig& config = TextConfig());
|
||||
|
||||
Reference in New Issue
Block a user