From 58ad98213763348b49d8313675203b287a5442bb Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 20 Oct 2018 10:13:31 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E5=A4=B4=E6=96=87=E4=BB=B6=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/graphics.cpp | 41 ++++++++++++++----- src/lua/modules/graphics/textureFont.cpp | 67 ------------------------------- src/lua/modules/graphics/texture_font.cpp | 67 +++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 76 deletions(-) delete mode 100644 src/lua/modules/graphics/textureFont.cpp create mode 100644 src/lua/modules/graphics/texture_font.cpp (limited to 'src/lua/modules') diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 4ef2b56..31f6fba 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -1,18 +1,16 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/common/common.h" - #include #include -using namespace std; +#include "libjin/jin.h" +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" namespace jin { namespace lua { - + using namespace std; using namespace jin::graphics; using jin::filesystem::Filesystem; using jin::filesystem::Buffer; @@ -655,8 +653,23 @@ namespace lua { Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); Bitmap* bitmap = p->getObject(); - Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = pt->getObject(); + Text* text; + if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + text = pt->getObject(); + } + else if (luax_isstring(L, 2)) + { + unsigned len; + const char* str = luax_checklstring(L, 2, &len); + text = new Text(Encode::UTF8, str, len); + } + else + { + luax_typerror(L, 2, "Text or string"); + return 1; + } float cellh = luax_checknumber(L, 4); TextureFont* textureFont = nullptr; if (luax_istable(L, 3)) @@ -672,6 +685,16 @@ namespace lua float cellw = luax_checknumber(L, 3); textureFont = TextureFont::createTextureFont(bitmap, *text, cellw, cellh); } + else + { + luax_error(L, "bad arguments #3 to 'newTextureFont', need to be table or number"); + return 0; + } + if (luax_isstring(L, 2)) + { + // Delete temporary text. + delete text; + } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy)); proxy->bind(new Ref(textureFont, JIN_GRAPHICS_TEXTUREFONT)); return 1; diff --git a/src/lua/modules/graphics/textureFont.cpp b/src/lua/modules/graphics/textureFont.cpp deleted file mode 100644 index 4cf5bdd..0000000 --- a/src/lua/modules/graphics/textureFont.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::graphics; - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - proxy->release(); - return 0; - } - - /* typeset(Text | string, lineheight, spacing) */ - static int l_typeset(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - TextureFont* tf = p->getObject(); - int lineheight = luax_checkinteger(L, 3); - int spacing = luax_optnumber(L, 4, 0); - Page* page = nullptr; - if (luax_isstring(L, 2)) - { - unsigned length; - const char* str = luax_checklstring(L, 2, &length); - Text text(Encode::UTF8, str, length); - page = tf->typeset(text, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject(); - page = tf->typeset(*text, lineheight, spacing); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Ref& refTF = p->getRef(); - refTF.retain(); - refPage->setUserdata(&refTF); - } - proxy->bind(refPage); - return 1; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "typeset", l_typeset }, - { 0, 0 } - }; - - int luaopen_TextureFont(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXTUREFONT, f); - - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/graphics/texture_font.cpp b/src/lua/modules/graphics/texture_font.cpp new file mode 100644 index 0000000..4cf5bdd --- /dev/null +++ b/src/lua/modules/graphics/texture_font.cpp @@ -0,0 +1,67 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::graphics; + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + proxy->release(); + return 0; + } + + /* typeset(Text | string, lineheight, spacing) */ + static int l_typeset(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject(); + int lineheight = luax_checkinteger(L, 3); + int spacing = luax_optnumber(L, 4, 0); + Page* page = nullptr; + if (luax_isstring(L, 2)) + { + unsigned length; + const char* str = luax_checklstring(L, 2, &length); + Text text(Encode::UTF8, str, length); + page = tf->typeset(text, lineheight, spacing); + } + else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + Text* text = p2->getObject(); + page = tf->typeset(*text, lineheight, spacing); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); + { + /* retain related ttf */ + Ref& refTF = p->getRef(); + refTF.retain(); + refPage->setUserdata(&refTF); + } + proxy->bind(refPage); + return 1; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "typeset", l_typeset }, + { 0, 0 } + }; + + int luaopen_TextureFont(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TEXTUREFONT, f); + + return 0; + } + +} // lua +} // jin \ No newline at end of file -- cgit v1.1-26-g67d0