diff --git a/gyp/tools.gyp b/gyp/tools.gyp index 62bbbd08c..aeeafef13 100644 --- a/gyp/tools.gyp +++ b/gyp/tools.gyp @@ -101,6 +101,7 @@ 'images.gyp:images', 'tools.gyp:picture_renderer', 'tools.gyp:picture_utils', + 'pdf.gyp:pdf', 'ports.gyp:ports', 'flags.gyp:flags', 'lua.gyp:lua', diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp index fb40087a1..104a427d7 100644 --- a/samplecode/SampleLua.cpp +++ b/samplecode/SampleLua.cpp @@ -50,10 +50,23 @@ static const char gCode[] = "" " end " " canvas:drawPath(path, path_paint);" "" - " paint:setTypeface(Sk.newTypeface('Times', 1));" - " paint:setColor{a = 1, r=0, g=0, b = 1};" - " paint:setTextSize(70);" - " canvas:drawText('Hamburgefons', 50, 200, paint);" + " paint:setColor{a=1,r=0,g=0,b=1};" + " local align = { 'left', 'center', 'right' };" + " paint:setTextSize(30);" + " for k, v in next, align do " + " paint:setTextAlign(v);" + " canvas:drawText('Hamburgefons', 320, 200 + 30*k, paint);" + " end " + "end " + "" + "function onStartup() " + " local paint = Sk.newPaint();" + " paint:setColor{a=1, r=1, g=0, b=0};" + " local doc = Sk.newDocumentPDF('/skia/trunk/test.pdf');" + " local canvas = doc:beginPage(72*8.5, 72*11);" + " canvas:drawText('Hello Lua', 300, 300, paint);" + " doc:close();" + " doc = nil;" "end " "" "function onDrawContent(canvas) " @@ -64,7 +77,9 @@ static const char gCode[] = "" " canvas:drawOval(r, paint) " " x = x + 1;" " if x > 100 then x = 0 end;" - "end"; + "end " + "" + "onStartup();"; class LuaView : public SampleView { public: diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp index d1f2633c9..99b157a2b 100644 --- a/src/utils/SkLua.cpp +++ b/src/utils/SkLua.cpp @@ -7,6 +7,7 @@ #include "SkLua.h" #include "SkCanvas.h" +#include "SkDocument.h" #include "SkPaint.h" #include "SkPath.h" #include "SkMatrix.h" @@ -28,6 +29,7 @@ template const char* get_mtname(); } DEF_MTNAME(SkCanvas) +DEF_MTNAME(SkDocument) DEF_MTNAME(SkMatrix) DEF_MTNAME(SkRRect) DEF_MTNAME(SkPath) @@ -312,6 +314,16 @@ static int lcanvas_getTotalMatrix(lua_State* L) { return 1; } +static int lcanvas_save(lua_State* L) { + lua_pushinteger(L, get_ref(L, 1)->save()); + return 1; +} + +static int lcanvas_restore(lua_State* L) { + get_ref(L, 1)->restore(); + return 0; +} + static int lcanvas_translate(lua_State* L) { get_ref(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3)); return 0; @@ -331,6 +343,8 @@ static const struct luaL_Reg gSkCanvas_Methods[] = { { "drawText", lcanvas_drawText }, { "getSaveCount", lcanvas_getSaveCount }, { "getTotalMatrix", lcanvas_getTotalMatrix }, + { "save", lcanvas_save }, + { "restore", lcanvas_restore }, { "translate", lcanvas_translate }, { "__gc", lcanvas_gc }, { NULL, NULL } @@ -338,6 +352,39 @@ static const struct luaL_Reg gSkCanvas_Methods[] = { /////////////////////////////////////////////////////////////////////////////// +static int ldocument_beginPage(lua_State* L) { + const SkRect* contentPtr = NULL; + push_ref(L, get_ref(L, 1)->beginPage(lua2scalar(L, 2), + lua2scalar(L, 3), + contentPtr)); + return 1; +} + +static int ldocument_endPage(lua_State* L) { + get_ref(L, 1)->endPage(); + return 0; +} + +static int ldocument_close(lua_State* L) { + get_ref(L, 1)->close(); + return 0; +} + +static int ldocument_gc(lua_State* L) { + get_ref(L, 1)->unref(); + return 0; +} + +static const struct luaL_Reg gSkDocument_Methods[] = { + { "beginPage", ldocument_beginPage }, + { "endPage", ldocument_endPage }, + { "close", ldocument_close }, + { "__gc", ldocument_gc }, + { NULL, NULL } +}; + +/////////////////////////////////////////////////////////////////////////////// + static int lpaint_isAntiAlias(lua_State* L) { lua_pushboolean(L, get_obj(L, 1)->isAntiAlias()); return 1; @@ -384,6 +431,41 @@ static int lpaint_getFontID(lua_State* L) { return 1; } +static const struct { + const char* fLabel; + SkPaint::Align fAlign; +} gAlignRec[] = { + { "left", SkPaint::kLeft_Align }, + { "center", SkPaint::kCenter_Align }, + { "right", SkPaint::kRight_Align }, +}; + +static int lpaint_getTextAlign(lua_State* L) { + SkPaint::Align align = get_obj(L, 1)->getTextAlign(); + for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { + if (gAlignRec[i].fAlign == align) { + lua_pushstring(L, gAlignRec[i].fLabel); + return 1; + } + } + return 0; +} + +static int lpaint_setTextAlign(lua_State* L) { + if (lua_isstring(L, 2)) { + size_t len; + const char* label = lua_tolstring(L, 2, &len); + + for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { + if (!strcmp(gAlignRec[i].fLabel, label)) { + get_obj(L, 1)->setTextAlign(gAlignRec[i].fAlign); + break; + } + } + } + return 0; +} + static int lpaint_gc(lua_State* L) { get_obj(L, 1)->~SkPaint(); return 0; @@ -399,6 +481,8 @@ static const struct luaL_Reg gSkPaint_Methods[] = { { "getTypeface", lpaint_getTypeface }, { "setTypeface", lpaint_setTypeface }, { "getFontID", lpaint_getFontID }, + { "getTextAlign", lpaint_getTextAlign }, + { "setTextAlign", lpaint_setTextAlign }, { "__gc", lpaint_gc }, { NULL, NULL } }; @@ -599,6 +683,23 @@ private: /////////////////////////////////////////////////////////////////////////////// +static int lsk_newDocumentPDF(lua_State* L) { + const char* file = NULL; + if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { + file = lua_tolstring(L, 1, NULL); + } + + SkDocument* doc = SkDocument::CreatePDF(file); + if (NULL == doc) { + // do I need to push a nil on the stack and return 1? + return 0; + } else { + push_ref(L, doc); + doc->unref(); + return 1; + } +} + static int lsk_newPaint(lua_State* L) { push_new(L); return 1; @@ -644,6 +745,7 @@ static void register_Sk(lua_State* L) { lua_setglobal(L, "Sk"); // the Sk table is still on top + setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); setfield_function(L, "newPaint", lsk_newPaint); setfield_function(L, "newPath", lsk_newPath); setfield_function(L, "newRRect", lsk_newRRect); @@ -663,6 +765,7 @@ static void register_Sk(lua_State* L) { void SkLua::Load(lua_State* L) { register_Sk(L); REG_CLASS(L, SkCanvas); + REG_CLASS(L, SkDocument); REG_CLASS(L, SkPath); REG_CLASS(L, SkPaint); REG_CLASS(L, SkRRect);