add document and textAlign support to lua

git-svn-id: http://skia.googlecode.com/svn/trunk@9480 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mike@reedtribe.org 2013-06-08 13:25:23 +00:00
parent ddc813b0de
commit bdf59df6a1
2 changed files with 123 additions and 5 deletions

View File

@ -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:

View File

@ -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 <typename T> 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<SkCanvas>(L, 1)->save());
return 1;
}
static int lcanvas_restore(lua_State* L) {
get_ref<SkCanvas>(L, 1)->restore();
return 0;
}
static int lcanvas_translate(lua_State* L) {
get_ref<SkCanvas>(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<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
lua2scalar(L, 3),
contentPtr));
return 1;
}
static int ldocument_endPage(lua_State* L) {
get_ref<SkDocument>(L, 1)->endPage();
return 0;
}
static int ldocument_close(lua_State* L) {
get_ref<SkDocument>(L, 1)->close();
return 0;
}
static int ldocument_gc(lua_State* L) {
get_ref<SkDocument>(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<SkPaint>(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<SkPaint>(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<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
break;
}
}
}
return 0;
}
static int lpaint_gc(lua_State* L) {
get_obj<SkPaint>(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<SkPaint>(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);