From b7310c9cce95bfc9bfe135063ee0e97fbc654928 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 1 Nov 2021 16:53:51 +0800 Subject: *misc --- Runtime/GUI/Font.h | 56 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'Runtime/GUI/Font.h') diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index da9ef55..242bd19 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -4,9 +4,11 @@ #include "Runtime/Graphics/Texture.h" #include "freetype.h" #include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Common/DataBuffer.h" #include #include +#include #include //https://learnopengl.com/In-Practice/Text-Rendering @@ -64,10 +66,30 @@ struct TextGeneratingSettings int padding; // glyph相互之间的间距,防止采样的时候越界 }; -class Font : public Singleton +class FontException : public std::exception { public: - void Setup(TextGeneratingSettings settings); + FontException(const char* what) + : std::exception(what) + { + } +}; + +enum EEncoding +{ + Encoding_ASCII, + Encoding_UTF8, + Encoding_UTF16, +}; + +// 单个字体 +class Font : public LuaBind::NativeClass +{ +public: + Font(std::string path, TextGeneratingSettings setting)/*throw FontException*/; + Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings setting)/*throw FontException*/; + Font(DataBuffer* db, TextGeneratingSettings setting)/*throw FontException*/; + Font(DataBuffer* db, LuaBind::VM* vm, std::string path, TextGeneratingSettings setting)/*throw FontException*/; const Character* GetCharacter(character::Codepoint codepoint, int pixelSize); const GlyphAtals* GetGlyphAtlas(int index); @@ -76,31 +98,41 @@ public: void RenderCharacter(character::Codepoint codepoint, int pixelSize); void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize); -private: - +private: Texture* CreateAtlas(); GlyphAtals* RequestAtlas(int pixelSize, Internal::Vector2 preferSize); - Internal::Rect GetRenderChartAndMove(GlyphAtals* atlas, Internal::Vector2 preferSize); bool HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSize); - character::Hash GetHash(character::Codepoint Codepoint, int pixelSize); + //------------------------------------------------------------------------- + std::unordered_map m_Characters; // 渲染完的文字 - std::vector m_Atlases; // 当前所有的atlas - std::unordered_map m_AtlasCache; // 快速找到可用的atlas + std::vector m_Atlases; // 当前所有的atlas,由font完全拥有所有权,所以是lightuserdata + std::unordered_map m_AtlasCache; // 快速找到可用的atlas + + bool m_IsEnabled; - int m_AtlasMargin; - int m_GlyphPadding; Internal::Vector2 m_AtlasSize; + int m_AtlasMargin; + int m_GlyphPadding; FT_Library m_FTLibrary; - FT_Face m_FTFace; + FT_Face m_FTFace; + + //------------------------------------------------------------------------- + + LUA_BIND_DECL_CLASS(Font); + + LUA_BIND_DECL_METHOD(_New); + LUA_BIND_DECL_METHOD(_GetCharacter); + LUA_BIND_DECL_METHOD(_GetCharacters); + LUA_BIND_DECL_METHOD(_GetGlyphAtlas); }; -#define g_TextGenerator (*Font::Instance()) +//#define g_TextGenerator (*Font::Instance()) namespace TextHelper { -- cgit v1.1-26-g67d0