From 0582cbfc265ca8864326723d64565e28e27acda4 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 13 Apr 2019 13:09:53 +0200 Subject: [PATCH] Utility: Replace UInt32 by TextStyleFlags --- ChangeLog.md | 2 ++ SDK/src/NDK/Lua/LuaBinding_Utility.cpp | 4 ++-- include/Nazara/Utility/Enums.hpp | 24 ++++++++++++------- include/Nazara/Utility/Font.hpp | 15 ++++++------ include/Nazara/Utility/FontData.hpp | 5 ++-- include/Nazara/Utility/SimpleTextDrawer.hpp | 10 ++++---- src/Nazara/Utility/Font.cpp | 16 ++++++------- src/Nazara/Utility/Formats/FreeTypeLoader.cpp | 8 +++---- src/Nazara/Utility/SimpleTextDrawer.cpp | 8 +++---- 9 files changed, 52 insertions(+), 40 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 4476c6eb3..5b65ee66d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -180,6 +180,8 @@ Nazara Engine: - ⚠ Default TextureSampler WrapMode is now Clamp (instead of Repeat) - Fixed StateMachine ignoring transitions made in Enter/Leave events of states - Fixed Material::Configure resetting textures +- ⚠ Renamed TextStyleFlags enum to TextStyle, introduced Flags specialization of TextStyle as TextStyleFlags +- ⚠ Font, FontData and SimpleTextDrawer now use a proper TextStyleFlags instead of a UInt32 Nazara Development Kit: - Added ImageWidget (#139) diff --git a/SDK/src/NDK/Lua/LuaBinding_Utility.cpp b/SDK/src/NDK/Lua/LuaBinding_Utility.cpp index 43ea4a8c7..a98bd4ed7 100644 --- a/SDK/src/NDK/Lua/LuaBinding_Utility.cpp +++ b/SDK/src/NDK/Lua/LuaBinding_Utility.cpp @@ -126,7 +126,7 @@ namespace Ndk case 2: { unsigned int characterSize = lua.Check(&argIndex); - Nz::UInt32 style = lua.Check(&argIndex); + Nz::TextStyleFlags style = lua.Check(&argIndex); lua.Push(instance->GetCachedGlyphCount(characterSize, style)); return 1; @@ -146,7 +146,7 @@ namespace Ndk font.BindMethod("IsValid", &Nz::Font::IsValid); - font.BindMethod("Precache", (bool(Nz::Font::*)(unsigned int, Nz::UInt32, const Nz::String&) const) &Nz::Font::Precache); + font.BindMethod("Precache", (bool(Nz::Font::*)(unsigned int, Nz::TextStyleFlags, const Nz::String&) const) &Nz::Font::Precache); font.BindMethod("SetGlyphBorder", &Nz::Font::SetGlyphBorder); font.BindMethod("SetMinimumStepSize", &Nz::Font::SetMinimumStepSize); diff --git a/include/Nazara/Utility/Enums.hpp b/include/Nazara/Utility/Enums.hpp index d6fee2127..f4d343acc 100644 --- a/include/Nazara/Utility/Enums.hpp +++ b/include/Nazara/Utility/Enums.hpp @@ -329,18 +329,26 @@ namespace Nz TextAlign_Max = TextAlign_Right }; - enum TextStyleFlags + enum TextStyle { - TextStyle_Regular = 0x0, + TextStyle_Bold, + TextStyle_Italic, + TextStyle_StrikeThrough, + TextStyle_Underlined, - TextStyle_Bold = 0x1, - TextStyle_Italic = 0x2, - TextStyle_StrikeThrough = 0x4, - TextStyle_Underlined = 0x8, - - TextStyle_Max = TextStyle_Underlined*2-1 + TextStyle_Max = TextStyle_Underlined }; + template<> + struct EnumAsFlags + { + static constexpr TextStyle max = TextStyle_Max; + }; + + using TextStyleFlags = Flags; + + constexpr TextStyleFlags TextStyle_Regular = 0; + enum VertexComponent { VertexComponent_Unused = -1, diff --git a/include/Nazara/Utility/Font.hpp b/include/Nazara/Utility/Font.hpp index be67c92e7..bac5db49c 100644 --- a/include/Nazara/Utility/Font.hpp +++ b/include/Nazara/Utility/Font.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -58,14 +59,14 @@ namespace Nz bool Create(FontData* data); void Destroy(); - bool ExtractGlyph(unsigned int characterSize, char32_t character, UInt32 style, FontGlyph* glyph) const; + bool ExtractGlyph(unsigned int characterSize, char32_t character, TextStyleFlags style, FontGlyph* glyph) const; const std::shared_ptr& GetAtlas() const; - std::size_t GetCachedGlyphCount(unsigned int characterSize, UInt32 style) const; + std::size_t GetCachedGlyphCount(unsigned int characterSize, TextStyleFlags style) const; std::size_t GetCachedGlyphCount() const; String GetFamilyName() const; int GetKerning(unsigned int characterSize, char32_t first, char32_t second) const; - const Glyph& GetGlyph(unsigned int characterSize, UInt32 style, char32_t character) const; + const Glyph& GetGlyph(unsigned int characterSize, TextStyleFlags style, char32_t character) const; unsigned int GetGlyphBorder() const; unsigned int GetMinimumStepSize() const; const SizeInfo& GetSizeInfo(unsigned int characterSize) const; @@ -73,8 +74,8 @@ namespace Nz bool IsValid() const; - bool Precache(unsigned int characterSize, UInt32 style, char32_t character) const; - bool Precache(unsigned int characterSize, UInt32 style, const String& characterSet) const; + bool Precache(unsigned int characterSize, TextStyleFlags style, char32_t character) const; + bool Precache(unsigned int characterSize, TextStyleFlags style, const String& characterSet) const; void SetAtlas(const std::shared_ptr& atlas); void SetGlyphBorder(unsigned int borderSize); @@ -130,11 +131,11 @@ namespace Nz private: using GlyphMap = std::unordered_map; - UInt64 ComputeKey(unsigned int characterSize, UInt32 style) const; + UInt64 ComputeKey(unsigned int characterSize, TextStyleFlags style) const; void OnAtlasCleared(const AbstractAtlas* atlas); void OnAtlasLayerChange(const AbstractAtlas* atlas, AbstractImage* oldLayer, AbstractImage* newLayer); void OnAtlasRelease(const AbstractAtlas* atlas); - const Glyph& PrecacheGlyph(GlyphMap& glyphMap, unsigned int characterSize, UInt32 style, char32_t character) const; + const Glyph& PrecacheGlyph(GlyphMap& glyphMap, unsigned int characterSize, TextStyleFlags style, char32_t character) const; static bool Initialize(); static void Uninitialize(); diff --git a/include/Nazara/Utility/FontData.hpp b/include/Nazara/Utility/FontData.hpp index efd2e6d94..a3ac4901a 100644 --- a/include/Nazara/Utility/FontData.hpp +++ b/include/Nazara/Utility/FontData.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Nz { @@ -21,7 +22,7 @@ namespace Nz FontData() = default; virtual ~FontData(); - virtual bool ExtractGlyph(unsigned int characterSize, char32_t character, UInt32 style, FontGlyph* dst) = 0; + virtual bool ExtractGlyph(unsigned int characterSize, char32_t character, TextStyleFlags style, FontGlyph* dst) = 0; virtual String GetFamilyName() const = 0; virtual String GetStyleName() const = 0; @@ -35,7 +36,7 @@ namespace Nz virtual float QueryUnderlinePosition(unsigned int characterSize) const = 0; virtual float QueryUnderlineThickness(unsigned int characterSize) const = 0; - virtual bool SupportsStyle(UInt32 style) const = 0; + virtual bool SupportsStyle(TextStyleFlags style) const = 0; }; } diff --git a/include/Nazara/Utility/SimpleTextDrawer.hpp b/include/Nazara/Utility/SimpleTextDrawer.hpp index d3c3bd9cc..c41a5d8be 100644 --- a/include/Nazara/Utility/SimpleTextDrawer.hpp +++ b/include/Nazara/Utility/SimpleTextDrawer.hpp @@ -38,20 +38,20 @@ namespace Nz std::size_t GetGlyphCount() const override; const Line& GetLine(std::size_t index) const override; std::size_t GetLineCount() const override; - UInt32 GetStyle() const; + TextStyleFlags GetStyle() const; const String& GetText() const; void SetCharacterSize(unsigned int characterSize); void SetColor(const Color& color); void SetFont(Font* font); - void SetStyle(UInt32 style); + void SetStyle(TextStyleFlags style); void SetText(const String& str); SimpleTextDrawer& operator=(const SimpleTextDrawer& drawer); SimpleTextDrawer& operator=(SimpleTextDrawer&& drawer); - static SimpleTextDrawer Draw(const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White); - static SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White); + static SimpleTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); + static SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); private: void ClearGlyphs() const; @@ -76,8 +76,8 @@ namespace Nz mutable Rectf m_workingBounds; mutable Recti m_bounds; String m_text; + TextStyleFlags m_style; mutable UInt32 m_previousCharacter; - UInt32 m_style; mutable Vector2ui m_drawPos; mutable bool m_colorUpdated; mutable bool m_glyphUpdated; diff --git a/src/Nazara/Utility/Font.cpp b/src/Nazara/Utility/Font.cpp index 731557628..0ccc09269 100644 --- a/src/Nazara/Utility/Font.cpp +++ b/src/Nazara/Utility/Font.cpp @@ -109,7 +109,7 @@ namespace Nz } } - bool Font::ExtractGlyph(unsigned int characterSize, char32_t character, UInt32 style, FontGlyph* glyph) const + bool Font::ExtractGlyph(unsigned int characterSize, char32_t character, TextStyleFlags style, FontGlyph* glyph) const { #if NAZARA_UTILITY_SAFE if (!IsValid()) @@ -127,7 +127,7 @@ namespace Nz return m_atlas; } - std::size_t Font::GetCachedGlyphCount(unsigned int characterSize, UInt32 style) const + std::size_t Font::GetCachedGlyphCount(unsigned int characterSize, TextStyleFlags style) const { UInt64 key = ComputeKey(characterSize, style); auto it = m_glyphes.find(key); @@ -187,7 +187,7 @@ namespace Nz return it->second; // Présent dans le cache, tout va bien } - const Font::Glyph& Font::GetGlyph(unsigned int characterSize, UInt32 style, char32_t character) const + const Font::Glyph& Font::GetGlyph(unsigned int characterSize, TextStyleFlags style, char32_t character) const { UInt64 key = ComputeKey(characterSize, style); return PrecacheGlyph(m_glyphes[key], characterSize, style, character); @@ -256,13 +256,13 @@ namespace Nz return m_data != nullptr; } - bool Font::Precache(unsigned int characterSize, UInt32 style, char32_t character) const + bool Font::Precache(unsigned int characterSize, TextStyleFlags style, char32_t character) const { UInt64 key = ComputeKey(characterSize, style); return PrecacheGlyph(m_glyphes[key], characterSize, style, character).valid; } - bool Font::Precache(unsigned int characterSize, UInt32 style, const String& characterSet) const + bool Font::Precache(unsigned int characterSize, TextStyleFlags style, const String& characterSet) const { ///TODO: Itération UTF-8 => UTF-32 sans allocation de buffer (Exposer utf8cpp ?) std::u32string set = characterSet.GetUtf32String(); @@ -399,7 +399,7 @@ namespace Nz s_defaultMinimumStepSize = minimumStepSize; } - UInt64 Font::ComputeKey(unsigned int characterSize, UInt32 style) const + UInt64 Font::ComputeKey(unsigned int characterSize, TextStyleFlags style) const { // On prend le pas en compte UInt64 sizePart = static_cast((characterSize/m_minimumStepSize)*m_minimumStepSize); @@ -471,7 +471,7 @@ namespace Nz NazaraError("Atlas has been released while in use"); } - const Font::Glyph& Font::PrecacheGlyph(GlyphMap& glyphMap, unsigned int characterSize, UInt32 style, char32_t character) const + const Font::Glyph& Font::PrecacheGlyph(GlyphMap& glyphMap, unsigned int characterSize, TextStyleFlags style, char32_t character) const { auto it = glyphMap.find(character); if (it != glyphMap.end()) // Si le glyphe n'est pas déjà chargé @@ -492,7 +492,7 @@ namespace Nz glyph.requireFauxBold = false; glyph.requireFauxItalic = false; - UInt32 supportedStyle = style; + TextStyleFlags supportedStyle = style; if (style & TextStyle_Bold && !m_data->SupportsStyle(TextStyle_Bold)) { glyph.requireFauxBold = true; diff --git a/src/Nazara/Utility/Formats/FreeTypeLoader.cpp b/src/Nazara/Utility/Formats/FreeTypeLoader.cpp index 2cd2c51e3..9f890385f 100644 --- a/src/Nazara/Utility/Formats/FreeTypeLoader.cpp +++ b/src/Nazara/Utility/Formats/FreeTypeLoader.cpp @@ -25,7 +25,7 @@ namespace Nz FT_Library s_library; std::shared_ptr s_libraryOwner; - float s_invScaleFactor = 1.f / (1 << 6); // 1/64 + constexpr float s_invScaleFactor = 1.f / (1 << 6); // 1/64 extern "C" unsigned long FT_StreamRead(FT_Stream stream, unsigned long offset, unsigned char* buffer, unsigned long count) @@ -96,7 +96,7 @@ namespace Nz return FT_Open_Face(s_library, &m_args, -1, nullptr) == 0; } - bool ExtractGlyph(unsigned int characterSize, char32_t character, UInt32 style, FontGlyph* dst) override + bool ExtractGlyph(unsigned int characterSize, char32_t character, TextStyleFlags style, FontGlyph* dst) override { #ifdef NAZARA_DEBUG if (!dst) @@ -118,7 +118,7 @@ namespace Nz const FT_Pos boldStrength = 2 << 6; - bool embolden = (style & TextStyle_Bold); + bool embolden = (style & TextStyle_Bold) != 0; dst->advance = (embolden) ? boldStrength >> 6 : 0; @@ -312,7 +312,7 @@ namespace Nz m_args.stream = &m_stream; } - bool SupportsStyle(UInt32 style) const override + bool SupportsStyle(TextStyleFlags style) const override { ///TODO return style == TextStyle_Regular || style == TextStyle_Bold; diff --git a/src/Nazara/Utility/SimpleTextDrawer.cpp b/src/Nazara/Utility/SimpleTextDrawer.cpp index 89accf012..ec684a8e0 100644 --- a/src/Nazara/Utility/SimpleTextDrawer.cpp +++ b/src/Nazara/Utility/SimpleTextDrawer.cpp @@ -120,7 +120,7 @@ namespace Nz return m_lines.size(); } - UInt32 SimpleTextDrawer::GetStyle() const + TextStyleFlags SimpleTextDrawer::GetStyle() const { return m_style; } @@ -159,7 +159,7 @@ namespace Nz } } - void SimpleTextDrawer::SetStyle(UInt32 style) + void SimpleTextDrawer::SetStyle(TextStyleFlags style) { m_style = style; @@ -207,7 +207,7 @@ namespace Nz return *this; } - SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, UInt32 style, const Color& color) + SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) { SimpleTextDrawer drawer; drawer.SetCharacterSize(characterSize); @@ -218,7 +218,7 @@ namespace Nz return drawer; } - SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, UInt32 style, const Color& color) + SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) { SimpleTextDrawer drawer; drawer.SetCharacterSize(characterSize);