diff options
-rw-r--r-- | include/core/SkData.h | 8 | ||||
-rw-r--r-- | samplecode/SampleLua.cpp | 3 | ||||
-rw-r--r-- | src/core/SkData.cpp | 10 | ||||
-rw-r--r-- | src/utils/SkLua.cpp | 67 |
4 files changed, 86 insertions, 2 deletions
diff --git a/include/core/SkData.h b/include/core/SkData.h index ab7da47743..eae84b028a 100644 --- a/include/core/SkData.h +++ b/include/core/SkData.h @@ -89,7 +89,13 @@ public: * takes ownership of that allocation, and will handling calling sk_free. */ static SkData* NewFromMalloc(const void* data, size_t length); - + + /** + * Create a new dataref the file with the specified path. + * If the file cannot be opened, this returns NULL. + */ + static SkData* NewFromFileName(const char path[]); + /** * Create a new dataref from a SkFILE. * This does not take ownership of the SkFILE, nor close it. diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp index 104a427d7a..00a3227d48 100644 --- a/samplecode/SampleLua.cpp +++ b/samplecode/SampleLua.cpp @@ -27,6 +27,8 @@ static const char gCode[] = "" "local paint = Sk.newPaint();" "paint:setAntiAlias(true);" "" + "local image = Sk.loadImage('/skia/trunk/sailboat.jpg');" + "" "local color = {a = 1, r = 1, g = 0, b = 0};" "" "function rnd(range) " @@ -76,6 +78,7 @@ static const char gCode[] = "" " canvas:translate(x, 0);" " canvas:drawOval(r, paint) " " x = x + 1;" + " canvas:drawImage(image, x, r.bottom + 50, 0.5);" " if x > 100 then x = 0 end;" "end " "" diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp index c1a21365ab..a1b42b0c4f 100644 --- a/src/core/SkData.cpp +++ b/src/core/SkData.cpp @@ -97,6 +97,16 @@ SkData* SkData::NewFromFILE(SkFILE* f) { return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL); } +SkData* SkData::NewFromFileName(const char path[]) { + SkFILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : NULL; + if (NULL == f) { + return NULL; + } + SkData* data = NewFromFILE(f); + sk_fclose(f); + return data; +} + SkData* SkData::NewFromFD(int fd) { size_t size; void* addr = sk_fdmmap(fd, &size); diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp index 3c8c9f5070..6754746033 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 @@ template <typename T> const char* get_mtname(); DEF_MTNAME(SkCanvas) DEF_MTNAME(SkDocument) +DEF_MTNAME(SkImage) DEF_MTNAME(SkMatrix) DEF_MTNAME(SkRRect) DEF_MTNAME(SkPath) @@ -302,6 +305,25 @@ static int lcanvas_drawCircle(lua_State* L) { 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 @@ static const struct luaL_Reg gSkCanvas_Methods[] = { { "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 const struct luaL_Reg gSkRRect_Methods[] = { /////////////////////////////////////////////////////////////////////////////// +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 @@ static int lsk_newTypeface(lua_State* L) { 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 @@ static void register_Sk(lua_State* L) { // 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 @@ void SkLua::Load(lua_State* L) { 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); |