" end "
" canvas:drawPath(path, path_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;"
+ " 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);"
"end "
""
"function onDrawContent(canvas) "
" canvas:drawOval(r, paint) "
" x = x + 1;"
" if x > 100 then x = 0 end;"
- "end "
- ""
- "onStartup();";
+ "end";
class LuaView : public SampleView {
public:
#include "SkLua.h"
#include "SkCanvas.h"
-#include "SkDocument.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkMatrix.h"
}
DEF_MTNAME(SkCanvas)
-DEF_MTNAME(SkDocument)
DEF_MTNAME(SkMatrix)
DEF_MTNAME(SkRRect)
DEF_MTNAME(SkPath)
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;
{ "drawText", lcanvas_drawText },
{ "getSaveCount", lcanvas_getSaveCount },
{ "getTotalMatrix", lcanvas_getTotalMatrix },
- { "save", lcanvas_save },
- { "restore", lcanvas_restore },
{ "translate", lcanvas_translate },
{ "__gc", lcanvas_gc },
{ NULL, NULL }
///////////////////////////////////////////////////////////////////////////////
-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;
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;
{ "getTypeface", lpaint_getTypeface },
{ "setTypeface", lpaint_setTypeface },
{ "getFontID", lpaint_getFontID },
- { "getTextAlign", lpaint_getTextAlign },
- { "setTextAlign", lpaint_setTextAlign },
{ "__gc", lpaint_gc },
{ NULL, NULL }
};
///////////////////////////////////////////////////////////////////////////////
-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;
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);
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);