Fix RichTextDrawer ignoring max line width

This commit is contained in:
Lynix
2019-12-28 12:03:41 +01:00
parent 453c7a7e77
commit fcfcc94c49
5 changed files with 81 additions and 28 deletions

View File

@@ -93,15 +93,16 @@ namespace Nz
struct Block;
inline void AppendNewLine(const Font* font, unsigned int characterSize) const;
void AppendNewLine(const Font* font, unsigned int characterSize, std::size_t glyphIndex, unsigned int glyphPosition) const;
inline void ClearGlyphs() const;
inline void ConnectFontSlots();
inline void DisconnectFontSlots();
bool GenerateGlyph(Glyph& glyph, char32_t character, float outlineThickness, bool lineWrap, const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, int renderOrder, int* advance) const;
void GenerateGlyphs(const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, const Color& outlineColor, float outlineThickness, const String& text) const;
inline std::size_t HandleFontAddition(const FontRef& font);
inline void ReleaseFont(std::size_t fontIndex);
inline void InvalidateGlyphs();
inline void ReleaseFont(std::size_t fontIndex);
inline bool ShouldLineWrap(float size) const;
void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer);
void OnFontInvalidated(const Font* font);
@@ -109,6 +110,8 @@ namespace Nz
void UpdateGlyphs() const;
static constexpr std::size_t InvalidGlyph = std::numeric_limits<std::size_t>::max();
struct Block
{
std::size_t fontIndex;
@@ -136,6 +139,7 @@ namespace Nz
Color m_defaultOutlineColor;
TextStyleFlags m_defaultStyle;
FontRef m_defaultFont;
mutable std::size_t m_lastSeparatorGlyph;
std::unordered_map<FontRef, std::size_t> m_fontIndexes;
std::vector<Block> m_blocks;
std::vector<FontData> m_fonts;
@@ -148,6 +152,7 @@ namespace Nz
float m_defaultOutlineThickness;
float m_maxLineWidth;
unsigned int m_defaultCharacterSize;
mutable unsigned int m_lastSeparatorPosition;
};
class RichTextDrawer::BlockRef

View File

@@ -138,22 +138,13 @@ namespace Nz
inline void RichTextDrawer::AppendNewLine(const Font* font, unsigned int characterSize) const
{
// Ensure we're appending from last line
Line& lastLine = m_lines.back();
const Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize);
// Reset cursor
m_drawPos.x = 0;
m_drawPos.y += sizeInfo.lineHeight;
m_workingBounds.ExtendTo(lastLine.bounds);
m_lines.emplace_back(Line{ Rectf(0.f, float(sizeInfo.lineHeight * m_lines.size()), 0.f, float(sizeInfo.lineHeight)), m_glyphs.size() + 1 });
AppendNewLine(font, characterSize, InvalidGlyph, 0);
}
inline void RichTextDrawer::ClearGlyphs() const
{
m_bounds.MakeZero();
m_lastSeparatorGlyph = InvalidGlyph;
m_lines.clear();
m_glyphs.clear();
m_glyphUpdated = true;
@@ -223,6 +214,14 @@ namespace Nz
}
}
inline bool RichTextDrawer::ShouldLineWrap(float size) const
{
if (m_lines.back().glyphIndex > m_glyphs.size())
return false;
return m_lines.back().bounds.GetMaximum().x + size > m_maxLineWidth;
}
inline bool RichTextDrawer::HasBlocks() const
{
return !m_blocks.empty();

View File

@@ -72,7 +72,7 @@ namespace Nz
void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer);
void OnFontInvalidated(const Font* font);
void OnFontRelease(const Font* object);
bool ShouldLineWrap(Glyph& glyph, float size, bool checkFirstGlyph = true) const;
bool ShouldLineWrap(float size) const;
void UpdateGlyphColor() const;
void UpdateGlyphs() const;