add drawImage to lua
git-svn-id: http://skia.googlecode.com/svn/trunk@9500 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index 3c8c9f5..6754746 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -7,10 +7,12 @@
#include "SkLua.h"
#include "SkCanvas.h"
+#include "SkData.h"
#include "SkDocument.h"
+#include "SkImage.h"
+#include "SkMatrix.h"
#include "SkPaint.h"
#include "SkPath.h"
-#include "SkMatrix.h"
#include "SkRRect.h"
#include "SkString.h"
#include "SkTypeface.h"
@@ -30,6 +32,7 @@
DEF_MTNAME(SkCanvas)
DEF_MTNAME(SkDocument)
+DEF_MTNAME(SkImage)
DEF_MTNAME(SkMatrix)
DEF_MTNAME(SkRRect)
DEF_MTNAME(SkPath)
@@ -302,6 +305,25 @@
return 0;
}
+static int lcanvas_drawImage(lua_State* L) {
+ SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
+ SkImage* image = get_ref<SkImage>(L, 2);
+ if (NULL == image) {
+ return 0;
+ }
+ SkScalar x = lua2scalar(L, 3);
+ SkScalar y = lua2scalar(L, 4);
+
+ SkPaint paint;
+ const SkPaint* paintPtr = NULL;
+ if (lua_isnumber(L, 5)) {
+ paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
+ paintPtr = &paint;
+ }
+ image->draw(canvas, x, y, paintPtr);
+ return 0;
+}
+
static int lcanvas_drawPath(lua_State* L) {
get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
*get_obj<SkPaint>(L, 3));
@@ -358,6 +380,7 @@
{ "drawRect", lcanvas_drawRect },
{ "drawOval", lcanvas_drawOval },
{ "drawCircle", lcanvas_drawCircle },
+ { "drawImage", lcanvas_drawImage },
{ "drawPath", lcanvas_drawPath },
{ "drawText", lcanvas_drawText },
{ "getSaveCount", lcanvas_getSaveCount },
@@ -731,6 +754,30 @@
///////////////////////////////////////////////////////////////////////////////
+static int limage_width(lua_State* L) {
+ lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
+ return 1;
+}
+
+static int limage_height(lua_State* L) {
+ lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
+ return 1;
+}
+
+static int limage_gc(lua_State* L) {
+ get_ref<SkImage>(L, 1)->unref();
+ return 0;
+}
+
+static const struct luaL_Reg gSkImage_Methods[] = {
+ { "width", limage_width },
+ { "height", limage_height },
+ { "__gc", limage_gc },
+ { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
static int ltypeface_gc(lua_State* L) {
get_ref<SkTypeface>(L, 1)->unref();
return 0;
@@ -827,6 +874,22 @@
return 1;
}
+static int lsk_loadImage(lua_State* L) {
+ if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
+ const char* name = lua_tolstring(L, 1, NULL);
+ SkAutoDataUnref data(SkData::NewFromFileName(name));
+ if (data.get()) {
+ SkImage* image = SkImage::NewEncodedData(data.get());
+ if (image) {
+ push_ref(L, image);
+ image->unref();
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
static void register_Sk(lua_State* L) {
lua_newtable(L);
lua_pushvalue(L, -1);
@@ -834,6 +897,7 @@
// the Sk table is still on top
setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
+ setfield_function(L, "loadImage", lsk_loadImage);
setfield_function(L, "newPaint", lsk_newPaint);
setfield_function(L, "newPath", lsk_newPath);
setfield_function(L, "newRRect", lsk_newRRect);
@@ -854,6 +918,7 @@
register_Sk(L);
REG_CLASS(L, SkCanvas);
REG_CLASS(L, SkDocument);
+ REG_CLASS(L, SkImage);
REG_CLASS(L, SkPath);
REG_CLASS(L, SkPaint);
REG_CLASS(L, SkRRect);