aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_ttf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/modules/graphics/je_lua_ttf.cpp')
-rw-r--r--src/lua/modules/graphics/je_lua_ttf.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/lua/modules/graphics/je_lua_ttf.cpp b/src/lua/modules/graphics/je_lua_ttf.cpp
new file mode 100644
index 0000000..c5d922b
--- /dev/null
+++ b/src/lua/modules/graphics/je_lua_ttf.cpp
@@ -0,0 +1,66 @@
+#include "lua/modules/luax.h"
+
+#include "lua/common/je_lua_common.h"
+#include "libjin/jin.h"
+
+#include "je_lua_page.h"
+#include "je_lua_text.h"
+
+using namespace JinEngine::Graphics;
+using namespace JinEngine::Graphics::Fonts;
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ const char* Jin_Lua_TTF = "TTF";
+
+ LUA_IMPLEMENT int l_gc(lua_State* L)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTF);
+ proxy->release();
+ return 0;
+ }
+
+ /* typeset(Text | string, lineheight, spacing) */
+ LUA_IMPLEMENT int l_typeset(lua_State* L)
+ {
+ Proxy* pxyTTF = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTF);
+ Shared<TTF>& shrTTF = pxyTTF->getShared<TTF>();
+ TTF* ttf = pxyTTF->getObject<TTF>();
+ int lineheight = luax_optnumber(L, 3, ttf->getFontSize());
+ 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 = ttf->typeset(text, lineheight, spacing);
+ }
+ else if (luax_istype(L, 2, Jin_Lua_Text))
+ {
+ Proxy* pxyText = (Proxy*)luax_checktype(L, 2, Jin_Lua_Text);
+ Text* text = pxyText->getObject<Text>();
+ page = ttf->typeset(*text, lineheight, spacing);
+ }
+ Proxy* pxyPage = luax_newinstance(L, Jin_Lua_Page);
+ Shared<Page>* refPage = new Shared<Page>(page, Jin_Lua_Page);
+ refPage->setDependency((int)PageDependency::DEP_TTF, &shrTTF);
+ pxyPage->bind(refPage);
+ return 1;
+ }
+
+ LUA_EXPORT void luaopen_TTF(lua_State* L)
+ {
+ luaL_Reg f[] = {
+ { "__gc", l_gc },
+ { "typeset", l_typeset },
+ { 0, 0 }
+ };
+ luax_newtype(L, Jin_Lua_TTF, f);
+ }
+
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file