Utility: RichTextDrawer now works (WIP)

This commit is contained in:
Lynix
2019-07-09 22:53:06 +02:00
committed by Jérôme Leclercq
parent 6bc99a6d5e
commit 451b3de69c
3 changed files with 768 additions and 44 deletions

View File

@@ -7,7 +7,7 @@
#ifndef NAZARA_RICHTEXTDRAWER_HPP
#define NAZARA_RICHTEXTDRAWER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Utility/AbstractTextDrawer.hpp>
#include <Nazara/Utility/Enums.hpp>
@@ -24,23 +24,23 @@ namespace Nz
RichTextDrawer();
RichTextDrawer(const RichTextDrawer& drawer);
RichTextDrawer(RichTextDrawer&& drawer);
virtual ~RichTextDrawer();
~RichTextDrawer();
BlockRef Append(const String& str);
BlockRef AppendText(const String& str);
void Clear();
inline void Clear();
unsigned int GetBlockCharacterSize(std::size_t index) const;
const Color& GetBlockColor(std::size_t index) const;
std::size_t GetBlockCount() const;
const FontRef& GetBlockFont(std::size_t index) const;
UInt32 GetBlockStyle(std::size_t index) const;
const String& GetBlockText(std::size_t index) const;
inline unsigned int GetBlockCharacterSize(std::size_t index) const;
inline const Color& GetBlockColor(std::size_t index) const;
inline std::size_t GetBlockCount() const;
inline const FontRef& GetBlockFont(std::size_t index) const;
inline TextStyleFlags GetBlockStyle(std::size_t index) const;
inline const String& GetBlockText(std::size_t index) const;
unsigned int GetDefaultCharacterSize() const;
const Color& GetDefaultColor() const;
const FontRef& GetDefaultFont() const;
UInt32 GetDefaultStyle() const;
inline unsigned int GetDefaultCharacterSize() const;
inline const Color& GetDefaultColor() const;
inline const FontRef& GetDefaultFont() const;
inline TextStyleFlags GetDefaultStyle() const;
const Recti& GetBounds() const override;
Font* GetFont(std::size_t index) const override;
@@ -54,62 +54,82 @@ namespace Nz
void RemoveBlock(std::size_t index);
void SetBlockCharacterSize(std::size_t index, unsigned int characterSize);
void SetBlockColor(std::size_t index, const Color& color);
void SetBlockFont(std::size_t index, FontRef font);
void SetBlockStyle(std::size_t index, UInt32 style);
void SetBlockText(std::size_t index, const String& str);
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 SetBlockStyle(std::size_t index, TextStyleFlags style);
inline void SetBlockText(std::size_t index, const String& str);
void SetDefaultCharacterSize(unsigned int characterSize);
void SetDefaultColor(const Color& color);
void SetDefaultFont(FontRef font);
void SetDefaultStyle(UInt32 style);
inline void SetDefaultCharacterSize(unsigned int characterSize);
inline void SetDefaultColor(const Color& color);
inline void SetDefaultFont(const FontRef& font);
inline void SetDefaultStyle(TextStyleFlags style);
RichTextDrawer& operator=(const RichTextDrawer& drawer);
RichTextDrawer& operator=(RichTextDrawer&& drawer);
static RichTextDrawer Draw(const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White);
static RichTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White);
//static RichTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White);
//static RichTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White);
private:
void ClearGlyphs() const;
void ConnectFontSlots();
void DisconnectFontSlots();
void GenerateGlyphs(const String& text) const;
struct Block;
inline void AppendNewLine(const Font* font, unsigned int characterSize) 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();
void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer);
void OnFontInvalidated(const Font* font);
void OnFontRelease(const Font* object);
void UpdateGlyphs() const;
NazaraSlot(Font, OnFontAtlasChanged, m_atlasChangedSlot);
NazaraSlot(Font, OnFontAtlasLayerChanged, m_atlasLayerChangedSlot);
NazaraSlot(Font, OnFontGlyphCacheCleared, m_glyphCacheClearedSlot);
NazaraSlot(Font, OnFontRelease, m_fontReleaseSlot);
void UpdateGlyphs() const;
struct Block
{
std::size_t fontIndex;
Color color;
String text;
UInt32 style;
TextStyleFlags style;
unsigned int characterSize;
unsigned int fontIndex;
};
struct FontData
{
FontRef font;
std::size_t useCount = 0;
NazaraSlot(Font, OnFontAtlasChanged, atlasChangedSlot);
NazaraSlot(Font, OnFontAtlasLayerChanged, atlasLayerChangedSlot);
NazaraSlot(Font, OnFontGlyphCacheCleared, glyphCacheClearedSlot);
NazaraSlot(Font, OnFontRelease, fontReleaseSlot);
};
Color m_defaultColor;
TextStyleFlags m_defaultStyle;
FontRef m_defaultFont;
UInt32 m_defaultStyle;
unsigned int m_defaultCharacterSize;
std::unordered_map<FontRef, unsigned int> m_fonts;
std::unordered_map<FontRef, std::size_t> m_fontIndexes;
std::vector<Block> m_blocks;
std::vector<FontData> m_fonts;
mutable std::vector<Glyph> m_glyphs;
mutable std::vector<Line> m_lines;
mutable Rectf m_workingBounds;
mutable Rectui m_bounds;
mutable Recti m_bounds;
mutable Vector2ui m_drawPos;
mutable bool m_glyphUpdated;
unsigned int m_defaultCharacterSize;
};
class RichTextDrawer::BlockRef
{
friend RichTextDrawer;
public:
BlockRef(const BlockRef&) = default;
BlockRef(BlockRef&&) = default;
@@ -118,13 +138,13 @@ namespace Nz
inline unsigned int GetCharacterSize() const;
inline Color GetColor() const;
inline const FontRef& GetFont() const;
inline UInt32 GetStyle() 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 SetStyle(UInt32 style);
inline void SetStyle(TextStyleFlags style);
inline void SetText(const String& text);
BlockRef& operator=(const BlockRef&) = default;

View File

@@ -2,10 +2,238 @@
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/RichTextDrawer.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
inline void RichTextDrawer::Clear()
{
m_fontIndexes.clear();
m_blocks.clear();
m_fonts.clear();
m_glyphs.clear();
ClearGlyphs();
}
inline unsigned int RichTextDrawer::GetBlockCharacterSize(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].characterSize;
}
inline const Color& RichTextDrawer::GetBlockColor(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].color;
}
inline std::size_t RichTextDrawer::GetBlockCount() const
{
return m_blocks.size();
}
inline const FontRef& RichTextDrawer::GetBlockFont(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
std::size_t fontIndex = m_blocks[index].fontIndex;
assert(fontIndex < m_fonts.size());
return m_fonts[fontIndex].font;
}
inline TextStyleFlags RichTextDrawer::GetBlockStyle(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].style;
}
inline const String& RichTextDrawer::GetBlockText(std::size_t index) const
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
return m_blocks[index].text;
}
inline unsigned int RichTextDrawer::GetDefaultCharacterSize() const
{
return m_defaultCharacterSize;
}
inline const Color& RichTextDrawer::GetDefaultColor() const
{
return m_defaultColor;
}
inline const FontRef& RichTextDrawer::GetDefaultFont() const
{
return m_defaultFont;
}
inline TextStyleFlags RichTextDrawer::GetDefaultStyle() const
{
return m_defaultStyle;
}
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);
unsigned int previousDrawPos = m_drawPos.x;
// 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 });
}
inline void RichTextDrawer::ClearGlyphs() const
{
m_bounds.MakeZero();
m_lines.clear();
m_glyphs.clear();
m_glyphUpdated = true;
m_workingBounds.MakeZero(); //< Compute bounds as float to speedup bounds computation (as casting between floats and integers is costly)
}
inline void RichTextDrawer::ConnectFontSlots()
{
for (auto& fontData : m_fonts)
{
fontData.atlasChangedSlot.Connect(fontData.font->OnFontAtlasChanged, this, &RichTextDrawer::OnFontInvalidated);
fontData.atlasLayerChangedSlot.Connect(fontData.font->OnFontAtlasLayerChanged, this, &RichTextDrawer::OnFontAtlasLayerChanged);
fontData.fontReleaseSlot.Connect(fontData.font->OnFontDestroy, this, &RichTextDrawer::OnFontRelease);
fontData.glyphCacheClearedSlot.Connect(fontData.font->OnFontGlyphCacheCleared, this, &RichTextDrawer::OnFontInvalidated);
}
}
inline void RichTextDrawer::DisconnectFontSlots()
{
for (auto& fontData : m_fonts)
{
fontData.atlasChangedSlot.Disconnect();
fontData.atlasLayerChangedSlot.Disconnect();
fontData.fontReleaseSlot.Disconnect();
fontData.glyphCacheClearedSlot.Disconnect();
}
}
inline std::size_t RichTextDrawer::HandleFontAddition(const FontRef& font)
{
auto it = m_fontIndexes.find(font);
if (it == m_fontIndexes.end())
{
std::size_t fontIndex = m_fonts.size();
m_fonts.emplace_back();
auto& fontData = m_fonts.back();
fontData.font = font;
fontData.atlasChangedSlot.Connect(font->OnFontAtlasChanged, this, &RichTextDrawer::OnFontInvalidated);
fontData.atlasLayerChangedSlot.Connect(font->OnFontAtlasLayerChanged, this, &RichTextDrawer::OnFontAtlasLayerChanged);
fontData.fontReleaseSlot.Connect(font->OnFontDestroy, this, &RichTextDrawer::OnFontRelease);
fontData.glyphCacheClearedSlot.Connect(font->OnFontGlyphCacheCleared, this, &RichTextDrawer::OnFontInvalidated);
it = m_fontIndexes.emplace(font, fontIndex).first;
}
return it->second;
}
inline void RichTextDrawer::ReleaseFont(std::size_t fontIndex)
{
assert(fontIndex < m_fonts.size());
FontData& fontData = m_fonts[fontIndex];
assert(fontData.useCount > 0);
if (--fontData.useCount == 0)
{
// Shift font indexes
m_fontIndexes.erase(fontData.font);
for (auto it = m_fontIndexes.begin(); it != m_fontIndexes.end(); ++it)
{
if (it->second > fontIndex)
it->second--;
}
m_fonts.erase(m_fonts.begin() + fontIndex);
}
}
inline void RichTextDrawer::SetBlockCharacterSize(std::size_t index, unsigned int characterSize)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].characterSize = characterSize;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockColor(std::size_t index, const Color& color)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].color = color;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockFont(std::size_t index, FontRef font)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
std::size_t fontIndex = HandleFontAddition(font);
std::size_t oldFontIndex = m_blocks[index].fontIndex;
if (oldFontIndex != fontIndex)
{
ReleaseFont(oldFontIndex);
m_fonts[fontIndex].useCount++;
m_blocks[index].fontIndex = fontIndex;
}
}
inline void RichTextDrawer::SetBlockStyle(std::size_t index, TextStyleFlags style)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].style = style;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockText(std::size_t index, const String& str)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
m_blocks[index].text = str;
InvalidateGlyphs();
}
inline void RichTextDrawer::SetDefaultCharacterSize(unsigned int characterSize)
{
m_defaultCharacterSize = characterSize;
}
inline void RichTextDrawer::SetDefaultColor(const Color& color)
{
m_defaultColor = color;
}
inline void RichTextDrawer::SetDefaultFont(const FontRef& font)
{
m_defaultFont = font;
}
inline void RichTextDrawer::SetDefaultStyle(TextStyleFlags style)
{
m_defaultStyle = style;
}
inline void RichTextDrawer::InvalidateGlyphs()
{
m_glyphUpdated = false;
}
/*!
* \class Nz::RichTextDrawer::BlockRef
* \brief Helper class representing a block inside a RichTextDrawer, allowing easier access.
@@ -58,7 +286,7 @@ namespace Nz
*
* \see GetCharacterSize, GetColor, GetFont, GetText, SetStyle
*/
inline UInt32 RichTextDrawer::BlockRef::GetStyle() const
inline TextStyleFlags RichTextDrawer::BlockRef::GetStyle() const
{
return m_drawer.GetBlockStyle(m_blockIndex);
}
@@ -113,7 +341,7 @@ namespace Nz
*
* \see GetStyle, SetCharacterSize, SetColor, SetFont, SetText
*/
inline void RichTextDrawer::BlockRef::SetStyle(UInt32 style)
inline void RichTextDrawer::BlockRef::SetStyle(TextStyleFlags style)
{
m_drawer.SetBlockStyle(m_blockIndex, style);
}