Utility/SimpleTextDrawer: Whitespaces are now glyphs too

This commit is contained in:
Lynix 2016-10-28 17:44:59 +02:00
parent a4eccc9fac
commit 736b31af1b
2 changed files with 58 additions and 42 deletions

View File

@ -149,6 +149,8 @@ namespace Nz
for (unsigned int i = 0; i < glyphCount; ++i) for (unsigned int i = 0; i < glyphCount; ++i)
{ {
const AbstractTextDrawer::Glyph& glyph = drawer.GetGlyph(i); const AbstractTextDrawer::Glyph& glyph = drawer.GetGlyph(i);
if (!glyph.atlas)
continue;
Texture* texture = static_cast<Texture*>(glyph.atlas); Texture* texture = static_cast<Texture*>(glyph.atlas);
if (lastTexture != texture) if (lastTexture != texture)

View File

@ -263,19 +263,21 @@ namespace Nz
m_previousCharacter = character; m_previousCharacter = character;
bool whitespace = true; bool whitespace = true;
int advance = 0;
switch (character) switch (character)
{ {
case ' ': case ' ':
m_drawPos.x += sizeInfo.spaceAdvance; advance = sizeInfo.spaceAdvance;
break; break;
case '\n': case '\n':
advance = 0;
m_drawPos.x = 0; m_drawPos.x = 0;
m_drawPos.y += sizeInfo.lineHeight; m_drawPos.y += sizeInfo.lineHeight;
break; break;
case '\t': case '\t':
m_drawPos.x += sizeInfo.spaceAdvance * 4; advance = sizeInfo.spaceAdvance * 4;
break; break;
default: default:
@ -283,57 +285,69 @@ namespace Nz
break; break;
} }
if (whitespace) Glyph glyph;
continue; // White spaces are blanks and invisible, move the draw position and skip the rest glyph.color = m_color;
if (!whitespace)
{
const Font::Glyph& fontGlyph = m_font->GetGlyph(m_characterSize, m_style, character); const Font::Glyph& fontGlyph = m_font->GetGlyph(m_characterSize, m_style, character);
if (!fontGlyph.valid) if (!fontGlyph.valid)
continue; // Glyph failed to load, just skip it (can't do much) continue; // Glyph failed to load, just skip it (can't do much)
Glyph glyph; advance = fontGlyph.advance;
glyph.atlas = m_font->GetAtlas()->GetLayer(fontGlyph.layerIndex); glyph.atlas = m_font->GetAtlas()->GetLayer(fontGlyph.layerIndex);
glyph.atlasRect = fontGlyph.atlasRect; glyph.atlasRect = fontGlyph.atlasRect;
glyph.color = m_color;
glyph.flipped = fontGlyph.flipped; glyph.flipped = fontGlyph.flipped;
int advance = fontGlyph.advance;
Rectf bounds(fontGlyph.aabb);
bounds.x += m_drawPos.x;
bounds.y += m_drawPos.y;
if (fontGlyph.requireFauxBold) if (fontGlyph.requireFauxBold)
{ {
// Let's simulate bold by enlarging the glyph (not a neat idea, but should work) // Let's simulate bold by enlarging the glyph (not a neat idea, but should work)
Vector2f center = bounds.GetCenter(); Vector2f center = glyph.bounds.GetCenter();
// Enlarge by 10% // Enlarge by 10%
bounds.width *= 1.1f; glyph.bounds.width *= 1.1f;
bounds.height *= 1.1f; glyph.bounds.height *= 1.1f;
// Replace it at the correct height // Replace it at the correct height
Vector2f offset(bounds.GetCenter() - center); Vector2f offset(glyph.bounds.GetCenter() - center);
bounds.y -= offset.y; glyph.bounds.y -= offset.y;
// Adjust advance (+10%) // Adjust advance (+10%)
advance += advance / 10; advance += advance / 10;
} }
glyph.bounds.Set(fontGlyph.aabb);
glyph.bounds.x += m_drawPos.x;
glyph.bounds.y += m_drawPos.y;
// We "lean" the glyph to simulate italics style // We "lean" the glyph to simulate italics style
float italic = (fontGlyph.requireFauxItalic) ? 0.208f : 0.f; float italic = (fontGlyph.requireFauxItalic) ? 0.208f : 0.f;
float italicTop = italic * bounds.y; float italicTop = italic * glyph.bounds.y;
float italicBottom = italic * bounds.GetMaximum().y; float italicBottom = italic * glyph.bounds.GetMaximum().y;
glyph.corners[0].Set(glyph.bounds.x - italicTop, glyph.bounds.y);
glyph.corners[1].Set(glyph.bounds.x + glyph.bounds.width - italicTop, glyph.bounds.y);
glyph.corners[2].Set(glyph.bounds.x - italicBottom, glyph.bounds.y + glyph.bounds.height);
glyph.corners[3].Set(glyph.bounds.x + glyph.bounds.width - italicBottom, glyph.bounds.y + glyph.bounds.height);
}
else
{
glyph.atlas = nullptr;
glyph.bounds.Set(m_drawPos.x - advance, m_drawPos.y - sizeInfo.lineHeight, float(advance), sizeInfo.lineHeight);
glyph.corners[0].Set(glyph.bounds.GetCorner(RectCorner_LeftTop));
glyph.corners[1].Set(glyph.bounds.GetCorner(RectCorner_RightTop));
glyph.corners[2].Set(glyph.bounds.GetCorner(RectCorner_LeftBottom));
glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner_RightBottom));
}
glyph.corners[0].Set(bounds.x - italicTop, bounds.y);
glyph.corners[1].Set(bounds.x + bounds.width - italicTop, bounds.y);
glyph.corners[2].Set(bounds.x - italicBottom, bounds.y + bounds.height);
glyph.corners[3].Set(bounds.x + bounds.width - italicBottom, bounds.y + bounds.height);
if (!m_workingBounds.IsValid()) if (!m_workingBounds.IsValid())
m_workingBounds.Set(glyph.corners[0]); m_workingBounds.Set(glyph.bounds);
else
for (unsigned int i = 0; i < 4; ++i) m_workingBounds.ExtendTo(glyph.bounds);
m_workingBounds.ExtendTo(glyph.corners[i]);
m_drawPos.x += advance; m_drawPos.x += advance;
m_glyphs.push_back(glyph); m_glyphs.push_back(glyph);