RichText: Add support for outline color/thickness

This commit is contained in:
Lynix 2019-12-28 11:43:48 +01:00
parent 988641b34b
commit 97418bfe04
5 changed files with 155 additions and 11 deletions

View File

@ -29,10 +29,14 @@ namespace Ndk
inline unsigned int GetCharacterSize() const;
inline const Nz::Color& GetTextColor() const;
inline Nz::Font* GetTextFont() const;
inline const Nz::Color& GetTextOutlineColor() const;
inline float GetTextOutlineThickness() const;
inline void SetCharacterSize(unsigned int characterSize);
inline void SetTextColor(const Nz::Color& color);
inline void SetTextFont(Nz::FontRef font);
inline void SetTextOutlineColor(const Nz::Color& color);
inline void SetTextOutlineThickness(float thickness);
void Write(const Nz::String& text, std::size_t glyphPosition) override;

View File

@ -21,6 +21,16 @@ namespace Ndk
return m_drawer.GetDefaultFont();
}
inline const Nz::Color& RichTextAreaWidget::GetTextOutlineColor() const
{
return m_drawer.GetDefaultOutlineColor();
}
inline float RichTextAreaWidget::GetTextOutlineThickness() const
{
return m_drawer.GetDefaultOutlineThickness();
}
inline void RichTextAreaWidget::SetCharacterSize(unsigned int characterSize)
{
m_drawer.SetDefaultCharacterSize(characterSize);
@ -35,4 +45,14 @@ namespace Ndk
{
m_drawer.SetDefaultFont(std::move(font));
}
inline void RichTextAreaWidget::SetTextOutlineColor(const Nz::Color& color)
{
m_drawer.SetDefaultOutlineColor(color);
}
inline void RichTextAreaWidget::SetTextOutlineThickness(float thickness)
{
m_drawer.SetDefaultOutlineThickness(thickness);
}
}

View File

@ -37,6 +37,8 @@ namespace Nz
inline std::size_t GetBlockCount() const;
inline std::size_t GetBlockFirstGlyphIndex(std::size_t index) const;
inline const FontRef& GetBlockFont(std::size_t index) const;
inline const Color& GetBlockOutlineColor(std::size_t index) const;
inline float GetBlockOutlineThickness(std::size_t index) const;
inline TextStyleFlags GetBlockStyle(std::size_t index) const;
inline const String& GetBlockText(std::size_t index) const;
@ -45,6 +47,8 @@ namespace Nz
inline unsigned int GetDefaultCharacterSize() const;
inline const Color& GetDefaultColor() const;
inline const FontRef& GetDefaultFont() const;
inline const Color& GetDefaultOutlineColor() const;
inline float GetDefaultOutlineThickness() const;
inline TextStyleFlags GetDefaultStyle() const;
Font* GetFont(std::size_t index) const override;
std::size_t GetFontCount() const override;
@ -63,12 +67,16 @@ namespace Nz
inline void SetBlockCharacterSize(std::size_t index, unsigned int characterSize);
inline void SetBlockColor(std::size_t index, const Color& color);
inline void SetBlockFont(std::size_t index, FontRef font);
inline void SetBlockOutlineColor(std::size_t index, const Color& color);
inline void SetBlockOutlineThickness(std::size_t index, float thickness);
inline void SetBlockStyle(std::size_t index, TextStyleFlags style);
inline void SetBlockText(std::size_t index, String str);
inline void SetDefaultCharacterSize(unsigned int characterSize);
inline void SetDefaultColor(const Color& color);
inline void SetDefaultFont(const FontRef& font);
inline void SetDefaultOutlineColor(const Color& color);
inline void SetDefaultOutlineThickness(float thickness);
inline void SetDefaultStyle(TextStyleFlags style);
void SetMaxLineWidth(float lineWidth) override;
@ -106,8 +114,10 @@ namespace Nz
std::size_t fontIndex;
std::size_t glyphIndex;
Color color;
Color outlineColor;
String text;
TextStyleFlags style;
float outlineThickness;
unsigned int characterSize;
};
@ -123,6 +133,7 @@ namespace Nz
};
Color m_defaultColor;
Color m_defaultOutlineColor;
TextStyleFlags m_defaultStyle;
FontRef m_defaultFont;
std::unordered_map<FontRef, std::size_t> m_fontIndexes;
@ -134,6 +145,7 @@ namespace Nz
mutable Recti m_bounds;
mutable Vector2ui m_drawPos;
mutable bool m_glyphUpdated;
float m_defaultOutlineThickness;
float m_maxLineWidth;
unsigned int m_defaultCharacterSize;
};
@ -151,12 +163,16 @@ namespace Nz
inline Color GetColor() const;
inline std::size_t GetFirstGlyphIndex() const;
inline const FontRef& GetFont() const;
inline Color GetOutlineColor() const;
inline float GetOutlineThickness() const;
inline TextStyleFlags GetStyle() const;
inline const String& GetText() const;
inline void SetCharacterSize(unsigned int size);
inline void SetColor(Color color);
inline void SetFont(FontRef font);
inline void SetOutlineColor(Color color);
inline void SetOutlineThickness(float thickness);
inline void SetStyle(TextStyleFlags style);
inline void SetText(const String& text);

View File

@ -82,6 +82,18 @@ namespace Nz
return m_fonts[fontIndex].font;
}
inline const Color& RichTextDrawer::GetBlockOutlineColor(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].outlineColor;
}
inline float RichTextDrawer::GetBlockOutlineThickness(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].outlineThickness;
}
inline TextStyleFlags RichTextDrawer::GetBlockStyle(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
@ -109,6 +121,16 @@ namespace Nz
return m_defaultFont;
}
inline const Color& RichTextDrawer::GetDefaultOutlineColor() const
{
return m_defaultOutlineColor;
}
inline float RichTextDrawer::GetDefaultOutlineThickness() const
{
return m_defaultOutlineThickness;
}
inline TextStyleFlags RichTextDrawer::GetDefaultStyle() const
{
return m_defaultStyle;
@ -235,6 +257,24 @@ namespace Nz
m_fonts[fontIndex].useCount++;
m_blocks[index].fontIndex = fontIndex;
}
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockOutlineColor(std::size_t index, const Color& color)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].outlineColor = color;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockOutlineThickness(std::size_t index, float thickness)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].outlineThickness = thickness;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockStyle(std::size_t index, TextStyleFlags style)
@ -279,6 +319,16 @@ namespace Nz
m_defaultFont = font;
}
inline void RichTextDrawer::SetDefaultOutlineColor(const Color& color)
{
m_defaultOutlineColor = color;
}
inline void RichTextDrawer::SetDefaultOutlineThickness(float thickness)
{
m_defaultOutlineThickness = thickness;
}
inline void RichTextDrawer::SetDefaultStyle(TextStyleFlags style)
{
m_defaultStyle = style;
@ -335,6 +385,28 @@ namespace Nz
return m_drawer.GetBlockFont(m_blockIndex);
}
/*!
* Returns the outline color used for the characters of the referenced block
* \return The referenced block outline color
*
* \see GetCharacterSize, GetColor, GetStyle, GetText, SetFont
*/
inline Color RichTextDrawer::BlockRef::GetOutlineColor() const
{
return m_drawer.GetBlockOutlineColor(m_blockIndex);
}
/*!
* Returns the outline thickness used for the characters of the referenced block
* \return The referenced block outline thickness
*
* \see GetCharacterSize, GetColor, GetStyle, GetText, SetFont
*/
inline float RichTextDrawer::BlockRef::GetOutlineThickness() const
{
return m_drawer.GetBlockOutlineThickness(m_blockIndex);
}
/*!
* Returns the style flags used for the characters of the referenced block
* \return The referenced block style flags (see TextStyleFlags)
@ -401,6 +473,28 @@ namespace Nz
m_drawer.SetBlockFont(m_blockIndex, std::move(font));
}
/*!
* Changes the outline color of the referenced block characters
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
*
* \see GetCharacterSize, SetCharacterSize, SetColor, SetStyle, SetText
*/
inline void RichTextDrawer::BlockRef::SetOutlineColor(Color color)
{
m_drawer.SetBlockOutlineColor(m_blockIndex, std::move(color));
}
/*!
* Changes the outline thickness of the referenced block characters
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
*
* \see GetCharacterSize, SetCharacterSize, SetColor, SetStyle, SetText
*/
inline void RichTextDrawer::BlockRef::SetOutlineThickness(float thickness)
{
m_drawer.SetBlockOutlineThickness(m_blockIndex, thickness);
}
/*!
* Changes the style flags of the referenced block characters
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.

View File

@ -11,11 +11,11 @@ namespace Nz
{
RichTextDrawer::RichTextDrawer() :
m_defaultColor(Color::White),
//m_outlineColor(Color::Black),
m_defaultOutlineColor(Color::Black),
m_defaultStyle(TextStyle_Regular),
m_glyphUpdated(false),
//m_maxLineWidth(std::numeric_limits<float>::infinity()),
//m_outlineThickness(0.f),
m_maxLineWidth(std::numeric_limits<float>::infinity()),
m_defaultOutlineThickness(0.f),
m_defaultCharacterSize(24)
{
SetDefaultFont(Font::GetDefault());
@ -27,9 +27,9 @@ namespace Nz
m_fontIndexes(drawer.m_fontIndexes),
m_blocks(drawer.m_blocks),
m_glyphUpdated(false),
//m_outlineColor(drawer.m_outlineColor),
//m_maxLineWidth(drawer.m_maxLineWidth),
//m_outlineThickness(drawer.m_outlineThickness),
m_defaultOutlineColor(drawer.m_defaultOutlineColor),
m_maxLineWidth(drawer.m_maxLineWidth),
m_defaultOutlineThickness(drawer.m_defaultOutlineThickness),
m_defaultCharacterSize(drawer.m_defaultCharacterSize)
{
m_fonts.resize(drawer.m_fonts.size());
@ -59,10 +59,12 @@ namespace Nz
auto HasDefaultProperties = [&](const Block& block)
{
return block.characterSize == m_defaultCharacterSize &&
block.color == m_defaultColor &&
block.fontIndex == defaultFontIndex &&
block.style == m_defaultStyle;
return block.characterSize == m_defaultCharacterSize &&
block.color == m_defaultColor &&
block.fontIndex == defaultFontIndex &&
block.outlineColor == m_defaultOutlineColor &&
block.outlineThickness == m_defaultOutlineThickness &&
block.style == m_defaultStyle;
};
// Check if last block has the same property as default, else create a new block
@ -83,6 +85,8 @@ namespace Nz
newBlock.color = m_defaultColor;
newBlock.fontIndex = defaultFontIndex;
newBlock.glyphIndex = glyphIndex;
newBlock.outlineColor = m_defaultOutlineColor;
newBlock.outlineThickness = m_defaultOutlineThickness;
newBlock.style = m_defaultStyle;
newBlock.text = str;
@ -171,6 +175,8 @@ namespace Nz
return lhs.characterSize == rhs.characterSize &&
lhs.color == rhs.color &&
lhs.fontIndex == rhs.fontIndex &&
lhs.outlineColor == rhs.outlineColor &&
lhs.outlineThickness == rhs.outlineThickness &&
lhs.style == rhs.style;
};
@ -220,6 +226,8 @@ namespace Nz
m_defaultCharacterSize = drawer.m_defaultCharacterSize;
m_defaultColor = drawer.m_defaultColor;
m_defaultFont = drawer.m_defaultFont;
m_defaultOutlineColor = drawer.m_defaultOutlineColor;
m_defaultOutlineThickness = drawer.m_defaultOutlineThickness;
m_defaultStyle = drawer.m_defaultStyle;
m_fontIndexes = drawer.m_fontIndexes;
@ -243,6 +251,8 @@ namespace Nz
m_defaultCharacterSize = std::move(drawer.m_defaultCharacterSize);
m_defaultColor = std::move(drawer.m_defaultColor);
m_defaultFont = std::move(drawer.m_defaultFont);
m_defaultOutlineColor = std::move(drawer.m_defaultOutlineColor);
m_defaultOutlineThickness = std::move(drawer.m_defaultOutlineThickness);
m_defaultStyle = std::move(drawer.m_defaultStyle);
m_drawPos = std::move(m_drawPos);
m_fontIndexes = std::move(drawer.m_fontIndexes);
@ -501,7 +511,7 @@ namespace Nz
assert(block.fontIndex < m_fonts.size());
const auto& fontData = m_fonts[block.fontIndex];
GenerateGlyphs(fontData.font, block.color, block.style, block.characterSize, block.color, 0.f, block.text);
GenerateGlyphs(fontData.font, block.color, block.style, block.characterSize, block.outlineColor, block.outlineThickness, block.text);
}
}
else