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 | |
parent | 4dead522e513ffd326101b790b2129595f72ff42 (diff) |
*misc
Diffstat (limited to 'Runtime/GUI')
-rw-r--r-- | Runtime/GUI/Font.cpp | 89 | ||||
-rw-r--r-- | Runtime/GUI/Font.h | 56 |
2 files changed, 121 insertions, 24 deletions
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index f00f33d..67a2e3b 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -13,21 +13,86 @@ using namespace character; //https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078 //https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme -void Font::Setup(TextGeneratingSettings settings) +static std::string s_FontError; + +Font::Font(std::string path, TextGeneratingSettings settings) + : LuaBind::NativeClass<Font>() { - m_AtlasMargin = settings.margin; - m_GlyphPadding = settings.padding; - m_AtlasSize = settings.atlasSize; + m_AtlasMargin = settings.margin; + m_GlyphPadding = settings.padding; + m_AtlasSize = settings.atlasSize; - if (FT_Init_FreeType(&m_FTLibrary)) - { - return; - } + if (FT_Init_FreeType(&m_FTLibrary)) + { + s_FontError = "Init freetype error. Font path " + path; + throw FontException(s_FontError.c_str()); + } - if (FT_New_Face(m_FTLibrary, "Resources/Font/Deng.ttf", 0, &m_FTFace)) - { - return; - } + if (FT_New_Face(m_FTLibrary, path.c_str(), 0, &m_FTFace)) + { + s_FontError = "Create freetype face error. Font path " + path; + throw FontException(s_FontError.c_str()); + } +} + +Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) + : LuaBind::NativeClass<Font>(vm) +{ + m_AtlasMargin = settings.margin; + m_GlyphPadding = settings.padding; + m_AtlasSize = settings.atlasSize; + + if (FT_Init_FreeType(&m_FTLibrary)) + { + s_FontError = "Init freetype error. Font path " + path; + throw FontException(s_FontError.c_str()); + } + + if (FT_New_Face(m_FTLibrary, path.c_str(), 0, &m_FTFace)) + { + s_FontError = "Create freetype face error. Font path " + path; + throw FontException(s_FontError.c_str()); + } +} + +Font::Font(DataBuffer* db, TextGeneratingSettings settings) + : LuaBind::NativeClass<Font>() +{ + m_AtlasMargin = settings.margin; + m_GlyphPadding = settings.padding; + m_AtlasSize = settings.atlasSize; + + if (FT_Init_FreeType(&m_FTLibrary)) + { + s_FontError = "Init freetype error."; + throw FontException(s_FontError.c_str()); + } + + if (FT_New_Memory_Face(m_FTLibrary, db->udata, db->length, 0, &m_FTFace)) + { + s_FontError = "Create freetype face error."; + throw FontException(s_FontError.c_str()); + } +} + +Font::Font(DataBuffer* db, LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) + : LuaBind::NativeClass<Font>(vm) +{ + m_AtlasMargin = settings.margin; + m_GlyphPadding = settings.padding; + m_AtlasSize = settings.atlasSize; + + if (FT_Init_FreeType(&m_FTLibrary)) + { + s_FontError = "Init freetype error."; + throw FontException(s_FontError.c_str()); + } + + if (FT_New_Memory_Face(m_FTLibrary, db->udata, db->length, 0, &m_FTFace)) + { + s_FontError = "Create freetype face error."; + throw FontException(s_FontError.c_str()); + } } character::Hash Font::GetHash(Codepoint codepoint, int pixelSize) 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 { |