diff options
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/Common/DataBuffer.h | 6 | ||||
-rw-r--r-- | Runtime/GUI/Font.cpp | 89 | ||||
-rw-r--r-- | Runtime/GUI/Font.h | 56 | ||||
-rw-r--r-- | Runtime/Graphics/Texture.h | 4 | ||||
-rw-r--r-- | Runtime/Lua/LuaHelper.cpp | 1 | ||||
-rw-r--r-- | Runtime/Scripting/GUI/Font.bind.cpp | 103 | ||||
-rw-r--r-- | Runtime/Scripting/GUI/GUI.bind.cpp | 24 |
7 files changed, 258 insertions, 25 deletions
diff --git a/Runtime/Common/DataBuffer.h b/Runtime/Common/DataBuffer.h index 7b89ab5..dbe7c30 100644 --- a/Runtime/Common/DataBuffer.h +++ b/Runtime/Common/DataBuffer.h @@ -23,7 +23,11 @@ public: delete data; } - char* data; + union { + char* data; + unsigned char* udata; + char* sdata; + }; int length; EDataBufferType type; 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 { diff --git a/Runtime/Graphics/Texture.h b/Runtime/Graphics/Texture.h index 6ead2fb..9633db5 100644 --- a/Runtime/Graphics/Texture.h +++ b/Runtime/Graphics/Texture.h @@ -76,6 +76,8 @@ public: private: void Init(TextureSetting setting, ImageData* imgData); + //------------------------------------------------------------------------------ + GLuint m_GPUID; int m_Width, m_Height; @@ -89,6 +91,8 @@ private: LuaBind::MemberRef m_ImageData; //图片像素数据 + //------------------------------------------------------------------------------ + LUA_BIND_DECL_CLASS(Texture); LUA_BIND_DECL_METHOD(_New); diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index e7247c0..2c95043 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -4,6 +4,7 @@ #include "Runtime/Math/Vector3.h"
#include "Runtime/Math/Vector4.h"
#include "Runtime/Math/Matrix44.h"
+#include "Runtime/GUI/Font.h"
using namespace LuaBind;
diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp new file mode 100644 index 0000000..73fb7e6 --- /dev/null +++ b/Runtime/Scripting/GUI/Font.bind.cpp @@ -0,0 +1,103 @@ +#include "Runtime/GUI/Font.h"
+#include "Runtime/Graphics/Shader.h" +#include "Runtime/Debug/Log.h" +#include "Runtime/Graphics/GfxDevice.h" +#include "Runtime/Common/DataBuffer.h"
+
+LUA_BIND_REGISTRY(Font) +{ + LUA_BIND_REGISTER_METHODS(state, + { "New", _New }, + { "GetCharacter", _GetCharacter }, + { "GetGlyphAtlas", _GetGlyphAtlas } + ); +} +
+LUA_BIND_POSTPROCESS(Font) +{ +} +
+// Font.New(ttfPath, atlasSize, margin, padding)
+// Font.New(ttfData, atlasSize, margin, padding)
+LUA_BIND_IMPL_METHOD(Font, _New) +{
+ LUA_BIND_STATE(L);
+ LUA_BIND_CHECK(L, "STNN!|UTNN!");
+
+ Font* font = NULL;
+ if (state.CheckParams(1, "STNN!"))
+ {
+ const char* path = state.GetValue<const char*>(1, "");
+ TextGeneratingSettings setting;
+ setting.atlasSize = state.GetValue<Internal::Vector2>(2, Internal::Vector2(128, 128));
+ setting.margin = state.GetValue<int>(3, 0);
+ setting.padding = state.GetValue<int>(4, 0);
+ try {
+ font = new Font(path, setting);
+ }
+ catch (FontException& e)
+ {
+ log_error(e.what());
+ state.PushNil();
+ return 1;
+ }
+ }
+ else if (state.CheckParams(1, "UTNN!"))
+ {
+ DataBuffer* buffer = state.GetUserdata<DataBuffer>(1);
+ if (buffer == NULL)
+ {
+ log_error("Create font failed, DataBuffer is invalid.");
+ state.PushNil();
+ return 1;
+ }
+ TextGeneratingSettings setting;
+ setting.atlasSize = state.GetValue<Internal::Vector2>(2, Internal::Vector2(128, 128));
+ setting.margin = state.GetValue<int>(3, 0);
+ setting.padding = state.GetValue<int>(4, 0);
+ try {
+ font = new Font(buffer, setting);
+ }
+ catch (FontException& e)
+ {
+ log_error(e.what());
+ state.PushNil();
+ return 1;
+ }
+ }
+
+ font->PushUserdata(state);
+
+ return 1;
+}
+
+// font:GetCharacter(char, size, encoding)
+LUA_BIND_IMPL_METHOD(Font, _GetCharacter) +{
+ LUA_BIND_PREPARE(L, Font);
+// Character character = self->GetCharacter(character);
+ return 1;
+}
+
+// font:GetCharacter(str, size, encoding)
+LUA_BIND_IMPL_METHOD(Font, _GetCharacters) +{
+ LUA_BIND_PREPARE(L, Font);
+// Character character = self->GetCharacter(character);
+ return 1;
+}
+
+// font:GetAtlas(i)
+// return lightuserdata
+LUA_BIND_IMPL_METHOD(Font, _GetGlyphAtlas) +{
+ LUA_BIND_PREPARE(L, Font);
+ //LUA_BIND_CHECK(L, "UN");
+
+ //int i = state.GetValue<int>(2, 0);
+ //GlyphAtals atlas = self->m_Atlases[i];
+
+ //state.Push((const void*)atlas.altas);
+
+ return 1;
+}
\ No newline at end of file diff --git a/Runtime/Scripting/GUI/GUI.bind.cpp b/Runtime/Scripting/GUI/GUI.bind.cpp new file mode 100644 index 0000000..4f6ed7a --- /dev/null +++ b/Runtime/Scripting/GUI/GUI.bind.cpp @@ -0,0 +1,24 @@ +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Debug/Log.h" +#include "Runtime/Common/DataBuffer.h" +#include "Runtime/FileSystem/FileJobs.h" +#include "Runtime/GUI/Font.h"
+
+using namespace std; +using namespace LuaBind; +
+int luaopen_GameLab_Engine_GUI(lua_State* L) +{ + log_info_tag("Scripting", "luaopen_GameLab_Engine_GUI()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Engine"); + state.PushNamespace("GUI"); + + state.RegisterNativeClass<Font>(); + + return 1; +}
\ No newline at end of file |