From 4b652abe1d76f1bcbe5d7583a8986256f50c483d Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 1 Nov 2021 19:30:44 +0800 Subject: -LuaBindClass::RegisterNativeClass --- Runtime/Scripting/GUI/Font.bind.cpp | 53 +++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'Runtime/Scripting/GUI/Font.bind.cpp') diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp index 73fb7e6..8259a60 100644 --- a/Runtime/Scripting/GUI/Font.bind.cpp +++ b/Runtime/Scripting/GUI/Font.bind.cpp @@ -3,12 +3,21 @@ #include "Runtime/Debug/Log.h" #include "Runtime/Graphics/GfxDevice.h" #include "Runtime/Common/DataBuffer.h" +#include "Runtime/GUI/utf8.h" +#include "Runtime/Utilities/StaticInitiator.h" + +static std::vector* s_Codepoints; + +InitializeStaticVariables([](){ + s_Codepoints = new std::vector(); +}); LUA_BIND_REGISTRY(Font) { LUA_BIND_REGISTER_METHODS(state, { "New", _New }, { "GetCharacter", _GetCharacter }, + { "GetCharacters", _GetCharacters }, { "GetGlyphAtlas", _GetGlyphAtlas } ); } @@ -33,7 +42,7 @@ LUA_BIND_IMPL_METHOD(Font, _New) setting.margin = state.GetValue(3, 0); setting.padding = state.GetValue(4, 0); try { - font = new Font(path, setting); + font = new Font(state.GetVM(), path, setting); } catch (FontException& e) { @@ -56,7 +65,7 @@ LUA_BIND_IMPL_METHOD(Font, _New) setting.margin = state.GetValue(3, 0); setting.padding = state.GetValue(4, 0); try { - font = new Font(buffer, setting); + font = new Font(state.GetVM(), buffer, setting); } catch (FontException& e) { @@ -75,7 +84,6 @@ LUA_BIND_IMPL_METHOD(Font, _New) LUA_BIND_IMPL_METHOD(Font, _GetCharacter) { LUA_BIND_PREPARE(L, Font); -// Character character = self->GetCharacter(character); return 1; } @@ -83,8 +91,43 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacter) LUA_BIND_IMPL_METHOD(Font, _GetCharacters) { LUA_BIND_PREPARE(L, Font); -// Character character = self->GetCharacter(character); - return 1; + LUA_BIND_CHECK(L, "USN*"); + + char* buf = (char*)state.GetValue(2, ""); + int size = state.GetValue(3, 12); + int encoding = state.GetValue(4, EEncoding::Encoding_UTF8); + + s_Codepoints->clear(); + if (encoding == EEncoding::Encoding_UTF8) + { + while (*buf != 0) { + int err; + s_Codepoints->push_back(utf8::getu8c(&buf, &err)); + if (err != 0) + { + log_warning("Illegal utf8 bytes %d", err); + } + } + } + else if (encoding == EEncoding::Encoding_UTF16) + { + while (*buf != 0) { + unsigned short* s = (unsigned short*)(buf); + s_Codepoints->push_back(*s); + buf += 2; + } + } + else if (encoding == EEncoding::Encoding_ASCII) + { + while (*buf != 0) { + s_Codepoints->push_back(*buf); + buf += 1; + } + } + + self->RenderCharacters(*s_Codepoints, size); + + return 0; } // font:GetAtlas(i) -- cgit v1.1-26-g67d0