diff options
author | chai <chaifix@163.com> | 2021-11-01 16:53:51 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-01 16:53:51 +0800 |
commit | b7310c9cce95bfc9bfe135063ee0e97fbc654928 (patch) | |
tree | 459d37f0acd9b9dd6719b71d1ecc8620dc1f5fef /Runtime/GUI/Font.h | |
parent | 4dead522e513ffd326101b790b2129595f72ff42 (diff) |
*misc
Diffstat (limited to 'Runtime/GUI/Font.h')
-rw-r--r-- | Runtime/GUI/Font.h | 56 |
1 files changed, 44 insertions, 12 deletions
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 <string> #include <unordered_map> +#include <exception> #include <vector> //https://learnopengl.com/In-Practice/Text-Rendering @@ -64,10 +66,30 @@ struct TextGeneratingSettings int padding; // glyph相互之间的间距,防止采样的时候越界 }; -class Font : public Singleton<Font> +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<Font> +{ +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<character::Hash, Character> m_Characters; // 渲染完的文字 - std::vector<GlyphAtals> m_Atlases; // 当前所有的atlas - std::unordered_map<int, GlyphAtals*> m_AtlasCache; // 快速找到可用的atlas + std::vector<GlyphAtals> m_Atlases; // 当前所有的atlas,由font完全拥有所有权,所以是lightuserdata + std::unordered_map<int/*pixelSize*/, GlyphAtals*> 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 { |