Utility/TextDrawer: Expose lines

This commit is contained in:
Lynix 2016-10-28 17:45:29 +02:00
parent 736b31af1b
commit 38da351e9b
3 changed files with 45 additions and 0 deletions

View File

@ -22,6 +22,7 @@ namespace Nz
{ {
public: public:
struct Glyph; struct Glyph;
struct Line;
AbstractTextDrawer() = default; AbstractTextDrawer() = default;
virtual ~AbstractTextDrawer(); virtual ~AbstractTextDrawer();
@ -31,15 +32,24 @@ namespace Nz
virtual std::size_t GetFontCount() const = 0; virtual std::size_t GetFontCount() const = 0;
virtual const Glyph& GetGlyph(std::size_t index) const = 0; virtual const Glyph& GetGlyph(std::size_t index) const = 0;
virtual std::size_t GetGlyphCount() const = 0; virtual std::size_t GetGlyphCount() const = 0;
virtual const Line& GetLine(std::size_t index) const = 0;
virtual std::size_t GetLineCount() const = 0;
struct Glyph struct Glyph
{ {
Color color; Color color;
Rectf bounds;
Rectui atlasRect; Rectui atlasRect;
Vector2f corners[4]; Vector2f corners[4];
AbstractImage* atlas; AbstractImage* atlas;
bool flipped; bool flipped;
}; };
struct Line
{
Rectf bounds;
std::size_t glyphIndex;
};
}; };
} }

View File

@ -36,6 +36,8 @@ namespace Nz
std::size_t GetFontCount() const override; std::size_t GetFontCount() const override;
const Glyph& GetGlyph(std::size_t index) const override; const Glyph& GetGlyph(std::size_t index) const override;
std::size_t GetGlyphCount() const override; std::size_t GetGlyphCount() const override;
const Line& GetLine(std::size_t index) const override;
std::size_t GetLineCount() const override;
UInt32 GetStyle() const; UInt32 GetStyle() const;
const String& GetText() const; const String& GetText() const;
@ -68,6 +70,7 @@ namespace Nz
NazaraSlot(Font, OnFontRelease, m_fontReleaseSlot); NazaraSlot(Font, OnFontRelease, m_fontReleaseSlot);
mutable std::vector<Glyph> m_glyphs; mutable std::vector<Glyph> m_glyphs;
mutable std::vector<Line> m_lines;
Color m_color; Color m_color;
FontRef m_font; FontRef m_font;
mutable Rectf m_workingBounds; mutable Rectf m_workingBounds;

View File

@ -102,6 +102,24 @@ namespace Nz
return m_glyphs.size(); return m_glyphs.size();
} }
const AbstractTextDrawer::Line& SimpleTextDrawer::GetLine(std::size_t index) const
{
NazaraAssert(index < m_lines.size(), "Line index out of range");
if (!m_glyphUpdated)
UpdateGlyphs();
return m_lines[index];
}
std::size_t SimpleTextDrawer::GetLineCount() const
{
if (!m_glyphUpdated)
UpdateGlyphs();
return m_lines.size();
}
UInt32 SimpleTextDrawer::GetStyle() const UInt32 SimpleTextDrawer::GetStyle() const
{ {
return m_style; return m_style;
@ -217,10 +235,16 @@ namespace Nz
m_bounds.MakeZero(); m_bounds.MakeZero();
m_colorUpdated = true; m_colorUpdated = true;
m_drawPos.Set(0, m_characterSize); //< Our draw "cursor" m_drawPos.Set(0, m_characterSize); //< Our draw "cursor"
m_lines.clear();
m_glyphs.clear(); m_glyphs.clear();
m_glyphUpdated = true; m_glyphUpdated = true;
m_previousCharacter = 0; m_previousCharacter = 0;
m_workingBounds.MakeZero(); //< Compute bounds as float to speedup bounds computation (as casting between floats and integers is costly) m_workingBounds.MakeZero(); //< Compute bounds as float to speedup bounds computation (as casting between floats and integers is costly)
if (m_font)
m_lines.emplace_back(Line{Rectf(0.f, 0.f, 0.f, m_font->GetSizeInfo(m_characterSize).lineHeight), 0});
else
m_lines.emplace_back(Line{Rectf::Zero(), 0});
} }
void SimpleTextDrawer::ConnectFontSlots() void SimpleTextDrawer::ConnectFontSlots()
@ -271,6 +295,13 @@ namespace Nz
break; break;
case '\n': case '\n':
if (!m_glyphs.empty())
{
Glyph& glyph = m_glyphs.back();
m_lines.back().bounds.ExtendTo(glyph.bounds);
}
m_lines.emplace_back(Line{Rectf(0.f, m_drawPos.y, 0.f, sizeInfo.lineHeight), m_glyphs.size()});
advance = 0; advance = 0;
m_drawPos.x = 0; m_drawPos.x = 0;
m_drawPos.y += sizeInfo.lineHeight; m_drawPos.y += sizeInfo.lineHeight;
@ -343,6 +374,7 @@ namespace Nz
glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner_RightBottom)); glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner_RightBottom));
} }
m_lines.back().bounds.ExtendTo(glyph.bounds);
if (!m_workingBounds.IsValid()) if (!m_workingBounds.IsValid())
m_workingBounds.Set(glyph.bounds); m_workingBounds.Set(glyph.bounds);