diff --git a/include/Nazara/Utility/AbstractTextDrawer.hpp b/include/Nazara/Utility/AbstractTextDrawer.hpp index 9f941b9c9..dd05e5af6 100644 --- a/include/Nazara/Utility/AbstractTextDrawer.hpp +++ b/include/Nazara/Utility/AbstractTextDrawer.hpp @@ -22,6 +22,7 @@ namespace Nz { public: struct Glyph; + struct Line; AbstractTextDrawer() = default; virtual ~AbstractTextDrawer(); @@ -31,15 +32,24 @@ namespace Nz virtual std::size_t GetFontCount() const = 0; virtual const Glyph& GetGlyph(std::size_t index) 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 { Color color; + Rectf bounds; Rectui atlasRect; Vector2f corners[4]; AbstractImage* atlas; bool flipped; }; + + struct Line + { + Rectf bounds; + std::size_t glyphIndex; + }; }; } diff --git a/include/Nazara/Utility/SimpleTextDrawer.hpp b/include/Nazara/Utility/SimpleTextDrawer.hpp index b2f2bf4f9..7c2db6c20 100644 --- a/include/Nazara/Utility/SimpleTextDrawer.hpp +++ b/include/Nazara/Utility/SimpleTextDrawer.hpp @@ -36,6 +36,8 @@ namespace Nz std::size_t GetFontCount() const override; const Glyph& GetGlyph(std::size_t index) 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; const String& GetText() const; @@ -68,6 +70,7 @@ namespace Nz NazaraSlot(Font, OnFontRelease, m_fontReleaseSlot); mutable std::vector m_glyphs; + mutable std::vector m_lines; Color m_color; FontRef m_font; mutable Rectf m_workingBounds; diff --git a/src/Nazara/Utility/SimpleTextDrawer.cpp b/src/Nazara/Utility/SimpleTextDrawer.cpp index c21de8a03..b34e141dc 100644 --- a/src/Nazara/Utility/SimpleTextDrawer.cpp +++ b/src/Nazara/Utility/SimpleTextDrawer.cpp @@ -102,6 +102,24 @@ namespace Nz 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 { return m_style; @@ -217,10 +235,16 @@ namespace Nz m_bounds.MakeZero(); m_colorUpdated = true; m_drawPos.Set(0, m_characterSize); //< Our draw "cursor" + m_lines.clear(); m_glyphs.clear(); m_glyphUpdated = true; m_previousCharacter = 0; 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() @@ -271,6 +295,13 @@ namespace Nz break; 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; m_drawPos.x = 0; m_drawPos.y += sizeInfo.lineHeight; @@ -343,6 +374,7 @@ namespace Nz glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner_RightBottom)); } + m_lines.back().bounds.ExtendTo(glyph.bounds); if (!m_workingBounds.IsValid()) m_workingBounds.Set(glyph.bounds);