1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkLua.h"
9 
10 #if SK_SUPPORT_GPU
11 #include "GrReducedClip.h"
12 #endif
13 
14 #include "SkBlurImageFilter.h"
15 #include "SkCanvas.h"
16 #include "SkData.h"
17 #include "SkDocument.h"
18 #include "SkGradientShader.h"
19 #include "SkImage.h"
20 #include "SkMatrix.h"
21 #include "SkPaint.h"
22 #include "SkPath.h"
23 #include "SkPictureRecorder.h"
24 #include "SkPixelRef.h"
25 #include "SkRRect.h"
26 #include "SkString.h"
27 #include "SkSurface.h"
28 #include "SkTextBlob.h"
29 #include "SkTypeface.h"
30 
31 extern "C" {
32     #include "lua.h"
33     #include "lualib.h"
34     #include "lauxlib.h"
35 }
36 
37 // return the metatable name for a given class
38 template <typename T> const char* get_mtname();
39 #define DEF_MTNAME(T)                           \
40     template <> const char* get_mtname<T>() {   \
41         return #T "_LuaMetaTableName";          \
42     }
43 
44 DEF_MTNAME(SkCanvas)
DEF_MTNAME(SkDocument)45 DEF_MTNAME(SkDocument)
46 DEF_MTNAME(SkImage)
47 DEF_MTNAME(SkImageFilter)
48 DEF_MTNAME(SkMatrix)
49 DEF_MTNAME(SkRRect)
50 DEF_MTNAME(SkPath)
51 DEF_MTNAME(SkPaint)
52 DEF_MTNAME(SkPathEffect)
53 DEF_MTNAME(SkPicture)
54 DEF_MTNAME(SkPictureRecorder)
55 DEF_MTNAME(SkShader)
56 DEF_MTNAME(SkSurface)
57 DEF_MTNAME(SkTextBlob)
58 DEF_MTNAME(SkTypeface)
59 
60 template <typename T> T* push_new(lua_State* L) {
61     T* addr = (T*)lua_newuserdata(L, sizeof(T));
62     new (addr) T;
63     luaL_getmetatable(L, get_mtname<T>());
64     lua_setmetatable(L, -2);
65     return addr;
66 }
67 
push_obj(lua_State * L,const T & obj)68 template <typename T> void push_obj(lua_State* L, const T& obj) {
69     new (lua_newuserdata(L, sizeof(T))) T(obj);
70     luaL_getmetatable(L, get_mtname<T>());
71     lua_setmetatable(L, -2);
72 }
73 
push_ref(lua_State * L,T * ref)74 template <typename T> T* push_ref(lua_State* L, T* ref) {
75     *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
76     luaL_getmetatable(L, get_mtname<T>());
77     lua_setmetatable(L, -2);
78     return ref;
79 }
80 
get_ref(lua_State * L,int index)81 template <typename T> T* get_ref(lua_State* L, int index) {
82     return *(T**)luaL_checkudata(L, index, get_mtname<T>());
83 }
84 
get_obj(lua_State * L,int index)85 template <typename T> T* get_obj(lua_State* L, int index) {
86     return (T*)luaL_checkudata(L, index, get_mtname<T>());
87 }
88 
lua2bool(lua_State * L,int index)89 static bool lua2bool(lua_State* L, int index) {
90     return !!lua_toboolean(L, index);
91 }
92 
93 ///////////////////////////////////////////////////////////////////////////////
94 
SkLua(const char termCode[])95 SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
96     fL = luaL_newstate();
97     luaL_openlibs(fL);
98     SkLua::Load(fL);
99 }
100 
SkLua(lua_State * L)101 SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
102 
~SkLua()103 SkLua::~SkLua() {
104     if (fWeOwnL) {
105         if (fTermCode.size() > 0) {
106             lua_getglobal(fL, fTermCode.c_str());
107             if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
108                 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
109             }
110         }
111         lua_close(fL);
112     }
113 }
114 
runCode(const char code[])115 bool SkLua::runCode(const char code[]) {
116     int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
117     if (err) {
118         SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
119         return false;
120     }
121     return true;
122 }
123 
runCode(const void * code,size_t size)124 bool SkLua::runCode(const void* code, size_t size) {
125     SkString str((const char*)code, size);
126     return this->runCode(str.c_str());
127 }
128 
129 ///////////////////////////////////////////////////////////////////////////////
130 
131 #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
132 
setfield_bool_if(lua_State * L,const char key[],bool pred)133 static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
134     if (pred) {
135         lua_pushboolean(L, true);
136         lua_setfield(L, -2, key);
137     }
138 }
139 
setfield_string(lua_State * L,const char key[],const char value[])140 static void setfield_string(lua_State* L, const char key[], const char value[]) {
141     lua_pushstring(L, value);
142     lua_setfield(L, -2, key);
143 }
144 
setfield_number(lua_State * L,const char key[],double value)145 static void setfield_number(lua_State* L, const char key[], double value) {
146     lua_pushnumber(L, value);
147     lua_setfield(L, -2, key);
148 }
149 
setfield_boolean(lua_State * L,const char key[],bool value)150 static void setfield_boolean(lua_State* L, const char key[], bool value) {
151     lua_pushboolean(L, value);
152     lua_setfield(L, -2, key);
153 }
154 
setfield_scalar(lua_State * L,const char key[],SkScalar value)155 static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
156     setfield_number(L, key, SkScalarToLua(value));
157 }
158 
setfield_function(lua_State * L,const char key[],lua_CFunction value)159 static void setfield_function(lua_State* L,
160                               const char key[], lua_CFunction value) {
161     lua_pushcfunction(L, value);
162     lua_setfield(L, -2, key);
163 }
164 
lua2int_def(lua_State * L,int index,int defaultValue)165 static int lua2int_def(lua_State* L, int index, int defaultValue) {
166     if (lua_isnumber(L, index)) {
167         return (int)lua_tonumber(L, index);
168     } else {
169         return defaultValue;
170     }
171 }
172 
lua2scalar(lua_State * L,int index)173 static SkScalar lua2scalar(lua_State* L, int index) {
174     SkASSERT(lua_isnumber(L, index));
175     return SkLuaToScalar(lua_tonumber(L, index));
176 }
177 
lua2scalar_def(lua_State * L,int index,SkScalar defaultValue)178 static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
179     if (lua_isnumber(L, index)) {
180         return SkLuaToScalar(lua_tonumber(L, index));
181     } else {
182         return defaultValue;
183     }
184 }
185 
getarray_scalar(lua_State * L,int stackIndex,int arrayIndex)186 static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
187     SkASSERT(lua_istable(L, stackIndex));
188     lua_rawgeti(L, stackIndex, arrayIndex);
189 
190     SkScalar value = lua2scalar(L, -1);
191     lua_pop(L, 1);
192     return value;
193 }
194 
getarray_scalars(lua_State * L,int stackIndex,SkScalar dst[],int count)195 static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
196     for (int i = 0; i < count; ++i) {
197         dst[i] = getarray_scalar(L, stackIndex, i + 1);
198     }
199 }
200 
getarray_points(lua_State * L,int stackIndex,SkPoint pts[],int count)201 static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
202     getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
203 }
204 
setarray_number(lua_State * L,int index,double value)205 static void setarray_number(lua_State* L, int index, double value) {
206     lua_pushnumber(L, value);
207     lua_rawseti(L, -2, index);
208 }
209 
setarray_scalar(lua_State * L,int index,SkScalar value)210 static void setarray_scalar(lua_State* L, int index, SkScalar value) {
211     setarray_number(L, index, SkScalarToLua(value));
212 }
213 
pushBool(bool value,const char key[])214 void SkLua::pushBool(bool value, const char key[]) {
215     lua_pushboolean(fL, value);
216     CHECK_SETFIELD(key);
217 }
218 
pushString(const char str[],const char key[])219 void SkLua::pushString(const char str[], const char key[]) {
220     lua_pushstring(fL, str);
221     CHECK_SETFIELD(key);
222 }
223 
pushString(const char str[],size_t length,const char key[])224 void SkLua::pushString(const char str[], size_t length, const char key[]) {
225     // TODO: how to do this w/o making a copy?
226     SkString s(str, length);
227     lua_pushstring(fL, s.c_str());
228     CHECK_SETFIELD(key);
229 }
230 
pushString(const SkString & str,const char key[])231 void SkLua::pushString(const SkString& str, const char key[]) {
232     lua_pushstring(fL, str.c_str());
233     CHECK_SETFIELD(key);
234 }
235 
pushColor(SkColor color,const char key[])236 void SkLua::pushColor(SkColor color, const char key[]) {
237     lua_newtable(fL);
238     setfield_number(fL, "a", SkColorGetA(color) / 255.0);
239     setfield_number(fL, "r", SkColorGetR(color) / 255.0);
240     setfield_number(fL, "g", SkColorGetG(color) / 255.0);
241     setfield_number(fL, "b", SkColorGetB(color) / 255.0);
242     CHECK_SETFIELD(key);
243 }
244 
pushU32(uint32_t value,const char key[])245 void SkLua::pushU32(uint32_t value, const char key[]) {
246     lua_pushnumber(fL, (double)value);
247     CHECK_SETFIELD(key);
248 }
249 
pushScalar(SkScalar value,const char key[])250 void SkLua::pushScalar(SkScalar value, const char key[]) {
251     lua_pushnumber(fL, SkScalarToLua(value));
252     CHECK_SETFIELD(key);
253 }
254 
pushArrayU16(const uint16_t array[],int count,const char key[])255 void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
256     lua_newtable(fL);
257     for (int i = 0; i < count; ++i) {
258         // make it base-1 to match lua convention
259         setarray_number(fL, i + 1, (double)array[i]);
260     }
261     CHECK_SETFIELD(key);
262 }
263 
pushArrayPoint(const SkPoint array[],int count,const char key[])264 void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
265     lua_newtable(fL);
266     for (int i = 0; i < count; ++i) {
267         // make it base-1 to match lua convention
268         lua_newtable(fL);
269         this->pushScalar(array[i].fX, "x");
270         this->pushScalar(array[i].fY, "y");
271         lua_rawseti(fL, -2, i + 1);
272     }
273     CHECK_SETFIELD(key);
274 }
275 
pushArrayScalar(const SkScalar array[],int count,const char key[])276 void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
277     lua_newtable(fL);
278     for (int i = 0; i < count; ++i) {
279         // make it base-1 to match lua convention
280         setarray_scalar(fL, i + 1, array[i]);
281     }
282     CHECK_SETFIELD(key);
283 }
284 
pushRect(const SkRect & r,const char key[])285 void SkLua::pushRect(const SkRect& r, const char key[]) {
286     lua_newtable(fL);
287     setfield_scalar(fL, "left", r.fLeft);
288     setfield_scalar(fL, "top", r.fTop);
289     setfield_scalar(fL, "right", r.fRight);
290     setfield_scalar(fL, "bottom", r.fBottom);
291     CHECK_SETFIELD(key);
292 }
293 
pushRRect(const SkRRect & rr,const char key[])294 void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
295     push_obj(fL, rr);
296     CHECK_SETFIELD(key);
297 }
298 
pushDash(const SkPathEffect::DashInfo & info,const char key[])299 void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
300     lua_newtable(fL);
301     setfield_scalar(fL, "phase", info.fPhase);
302     this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
303     CHECK_SETFIELD(key);
304 }
305 
306 
pushMatrix(const SkMatrix & matrix,const char key[])307 void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
308     push_obj(fL, matrix);
309     CHECK_SETFIELD(key);
310 }
311 
pushPaint(const SkPaint & paint,const char key[])312 void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
313     push_obj(fL, paint);
314     CHECK_SETFIELD(key);
315 }
316 
pushPath(const SkPath & path,const char key[])317 void SkLua::pushPath(const SkPath& path, const char key[]) {
318     push_obj(fL, path);
319     CHECK_SETFIELD(key);
320 }
321 
pushCanvas(SkCanvas * canvas,const char key[])322 void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
323     push_ref(fL, canvas);
324     CHECK_SETFIELD(key);
325 }
326 
pushTextBlob(const SkTextBlob * blob,const char key[])327 void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
328     push_ref(fL, const_cast<SkTextBlob*>(blob));
329     CHECK_SETFIELD(key);
330 }
331 
element_type(SkClipStack::Element::Type type)332 static const char* element_type(SkClipStack::Element::Type type) {
333     switch (type) {
334         case SkClipStack::Element::kEmpty_Type:
335             return "empty";
336         case SkClipStack::Element::kRect_Type:
337             return "rect";
338         case SkClipStack::Element::kRRect_Type:
339             return "rrect";
340         case SkClipStack::Element::kPath_Type:
341             return "path";
342     }
343     return "unknown";
344 }
345 
region_op(SkRegion::Op op)346 static const char* region_op(SkRegion::Op op) {
347     switch (op) {
348         case SkRegion::kDifference_Op:
349             return "difference";
350         case SkRegion::kIntersect_Op:
351             return "intersect";
352         case SkRegion::kUnion_Op:
353             return "union";
354         case SkRegion::kXOR_Op:
355             return "xor";
356         case SkRegion::kReverseDifference_Op:
357             return "reverse-difference";
358         case SkRegion::kReplace_Op:
359             return "replace";
360     }
361     return "unknown";
362 }
363 
pushClipStack(const SkClipStack & stack,const char * key)364 void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
365     lua_newtable(fL);
366     SkClipStack::B2TIter iter(stack);
367     const SkClipStack::Element* element;
368     int i = 0;
369     while ((element = iter.next())) {
370         this->pushClipStackElement(*element);
371         lua_rawseti(fL, -2, ++i);
372     }
373     CHECK_SETFIELD(key);
374 }
375 
pushClipStackElement(const SkClipStack::Element & element,const char * key)376 void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
377     lua_newtable(fL);
378     SkClipStack::Element::Type type = element.getType();
379     this->pushString(element_type(type), "type");
380     switch (type) {
381         case SkClipStack::Element::kEmpty_Type:
382             break;
383         case SkClipStack::Element::kRect_Type:
384             this->pushRect(element.getRect(), "rect");
385             break;
386         case SkClipStack::Element::kRRect_Type:
387             this->pushRRect(element.getRRect(), "rrect");
388             break;
389         case SkClipStack::Element::kPath_Type:
390             this->pushPath(element.getPath(), "path");
391             break;
392     }
393     this->pushString(region_op(element.getOp()), "op");
394     this->pushBool(element.isAA(), "aa");
395     CHECK_SETFIELD(key);
396 }
397 
398 
399 ///////////////////////////////////////////////////////////////////////////////
400 ///////////////////////////////////////////////////////////////////////////////
401 
getfield_scalar(lua_State * L,int index,const char key[])402 static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
403     SkASSERT(lua_istable(L, index));
404     lua_pushstring(L, key);
405     lua_gettable(L, index);
406 
407     SkScalar value = lua2scalar(L, -1);
408     lua_pop(L, 1);
409     return value;
410 }
411 
getfield_scalar_default(lua_State * L,int index,const char key[],SkScalar def)412 static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
413     SkASSERT(lua_istable(L, index));
414     lua_pushstring(L, key);
415     lua_gettable(L, index);
416 
417     SkScalar value;
418     if (lua_isnil(L, -1)) {
419         value = def;
420     } else {
421         value = lua2scalar(L, -1);
422     }
423     lua_pop(L, 1);
424     return value;
425 }
426 
byte2unit(U8CPU byte)427 static SkScalar byte2unit(U8CPU byte) {
428     return byte / 255.0f;
429 }
430 
unit2byte(SkScalar x)431 static U8CPU unit2byte(SkScalar x) {
432     if (x <= 0) {
433         return 0;
434     } else if (x >= 1) {
435         return 255;
436     } else {
437         return SkScalarRoundToInt(x * 255);
438     }
439 }
440 
lua2color(lua_State * L,int index)441 static SkColor lua2color(lua_State* L, int index) {
442     return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
443                           unit2byte(getfield_scalar_default(L, index, "r", 0)),
444                           unit2byte(getfield_scalar_default(L, index, "g", 0)),
445                           unit2byte(getfield_scalar_default(L, index, "b", 0)));
446 }
447 
lua2rect(lua_State * L,int index,SkRect * rect)448 static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
449     rect->set(getfield_scalar_default(L, index, "left", 0),
450               getfield_scalar_default(L, index, "top", 0),
451               getfield_scalar(L, index, "right"),
452               getfield_scalar(L, index, "bottom"));
453     return rect;
454 }
455 
lcanvas_clear(lua_State * L)456 static int lcanvas_clear(lua_State* L) {
457     get_ref<SkCanvas>(L, 1)->clear(0);
458     return 0;
459 }
460 
lcanvas_drawColor(lua_State * L)461 static int lcanvas_drawColor(lua_State* L) {
462     get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
463     return 0;
464 }
465 
lcanvas_drawPaint(lua_State * L)466 static int lcanvas_drawPaint(lua_State* L) {
467     get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
468     return 0;
469 }
470 
lcanvas_drawRect(lua_State * L)471 static int lcanvas_drawRect(lua_State* L) {
472     SkRect rect;
473     lua2rect(L, 2, &rect);
474     const SkPaint* paint = get_obj<SkPaint>(L, 3);
475     get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
476     return 0;
477 }
478 
lcanvas_drawOval(lua_State * L)479 static int lcanvas_drawOval(lua_State* L) {
480     SkRect rect;
481     get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
482                                       *get_obj<SkPaint>(L, 3));
483     return 0;
484 }
485 
lcanvas_drawCircle(lua_State * L)486 static int lcanvas_drawCircle(lua_State* L) {
487     get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
488                                         lua2scalar(L, 3),
489                                         lua2scalar(L, 4),
490                                         *get_obj<SkPaint>(L, 5));
491     return 0;
492 }
493 
lua2OptionalPaint(lua_State * L,int index,SkPaint * paint)494 static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
495     if (lua_isnumber(L, index)) {
496         paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
497         return paint;
498     } else if (lua_isuserdata(L, index)) {
499         const SkPaint* ptr = get_obj<SkPaint>(L, index);
500         if (ptr) {
501             *paint = *ptr;
502             return paint;
503         }
504     }
505     return NULL;
506 }
507 
lcanvas_drawImage(lua_State * L)508 static int lcanvas_drawImage(lua_State* L) {
509     SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
510     SkImage* image = get_ref<SkImage>(L, 2);
511     if (NULL == image) {
512         return 0;
513     }
514     SkScalar x = lua2scalar(L, 3);
515     SkScalar y = lua2scalar(L, 4);
516 
517     SkPaint paint;
518     canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
519     return 0;
520 }
521 
lcanvas_drawImageRect(lua_State * L)522 static int lcanvas_drawImageRect(lua_State* L) {
523     SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
524     SkImage* image = get_ref<SkImage>(L, 2);
525     if (NULL == image) {
526         return 0;
527     }
528 
529     SkRect srcR, dstR;
530     SkRect* srcRPtr = NULL;
531     if (!lua_isnil(L, 3)) {
532         srcRPtr = lua2rect(L, 3, &srcR);
533     }
534     lua2rect(L, 4, &dstR);
535 
536     SkPaint paint;
537     canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
538     return 0;
539 }
540 
lcanvas_drawPatch(lua_State * L)541 static int lcanvas_drawPatch(lua_State* L) {
542     SkPoint cubics[12];
543     SkColor colorStorage[4];
544     SkPoint texStorage[4];
545 
546     const SkColor* colors = NULL;
547     const SkPoint* texs = NULL;
548 
549     getarray_points(L, 2, cubics, 12);
550 
551     colorStorage[0] = SK_ColorRED;
552     colorStorage[1] = SK_ColorGREEN;
553     colorStorage[2] = SK_ColorBLUE;
554     colorStorage[3] = SK_ColorGRAY;
555 
556     if (lua_isnil(L, 4)) {
557         colors = colorStorage;
558     } else {
559         getarray_points(L, 4, texStorage, 4);
560         texs = texStorage;
561     }
562 
563     get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, NULL, *get_obj<SkPaint>(L, 5));
564     return 0;
565 }
566 
lcanvas_drawPath(lua_State * L)567 static int lcanvas_drawPath(lua_State* L) {
568     get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
569                                       *get_obj<SkPaint>(L, 3));
570     return 0;
571 }
572 
573 // drawPicture(pic, x, y, paint)
lcanvas_drawPicture(lua_State * L)574 static int lcanvas_drawPicture(lua_State* L) {
575     SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
576     SkPicture* picture = get_ref<SkPicture>(L, 2);
577     SkScalar x = lua2scalar_def(L, 3, 0);
578     SkScalar y = lua2scalar_def(L, 4, 0);
579     SkMatrix matrix, *matrixPtr = NULL;
580     if (x || y) {
581         matrix.setTranslate(x, y);
582         matrixPtr = &matrix;
583     }
584     SkPaint paint;
585     canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
586     return 0;
587 }
588 
lcanvas_drawText(lua_State * L)589 static int lcanvas_drawText(lua_State* L) {
590     if (lua_gettop(L) < 5) {
591         return 0;
592     }
593 
594     if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
595         size_t len;
596         const char* text = lua_tolstring(L, 2, &len);
597         get_ref<SkCanvas>(L, 1)->drawText(text, len,
598                                           lua2scalar(L, 3), lua2scalar(L, 4),
599                                           *get_obj<SkPaint>(L, 5));
600     }
601     return 0;
602 }
603 
lcanvas_drawTextBlob(lua_State * L)604 static int lcanvas_drawTextBlob(lua_State* L) {
605     const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
606     SkScalar x = lua2scalar(L, 3);
607     SkScalar y = lua2scalar(L, 4);
608     const SkPaint& paint = *get_obj<SkPaint>(L, 5);
609     get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
610     return 0;
611 }
612 
lcanvas_getSaveCount(lua_State * L)613 static int lcanvas_getSaveCount(lua_State* L) {
614     lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
615     return 1;
616 }
617 
lcanvas_getTotalMatrix(lua_State * L)618 static int lcanvas_getTotalMatrix(lua_State* L) {
619     SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
620     return 1;
621 }
622 
lcanvas_getClipStack(lua_State * L)623 static int lcanvas_getClipStack(lua_State* L) {
624     SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
625     return 1;
626 }
627 
lcanvas_getReducedClipStack(lua_State * L)628 int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
629 #if SK_SUPPORT_GPU
630     const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
631     SkISize layerSize = canvas->getTopLayerSize();
632     SkIPoint layerOrigin = canvas->getTopLayerOrigin();
633     SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
634                                             layerSize.fWidth, layerSize.fHeight);
635 
636     GrReducedClip::ElementList elements;
637     GrReducedClip::InitialState initialState;
638     int32_t genID;
639     SkIRect resultBounds;
640 
641     const SkClipStack& stack = *canvas->getClipStack();
642 
643     GrReducedClip::ReduceClipStack(stack,
644                                    queryBounds,
645                                    &elements,
646                                    &genID,
647                                    &initialState,
648                                    &resultBounds,
649                                    NULL);
650 
651     GrReducedClip::ElementList::Iter iter(elements);
652     int i = 0;
653     lua_newtable(L);
654     while(iter.get()) {
655         SkLua(L).pushClipStackElement(*iter.get());
656         iter.next();
657         lua_rawseti(L, -2, ++i);
658     }
659     // Currently this only returns the element list to lua, not the initial state or result bounds.
660     // It could return these as additional items on the lua stack.
661     return 1;
662 #else
663     return 0;
664 #endif
665 }
666 
lcanvas_save(lua_State * L)667 static int lcanvas_save(lua_State* L) {
668     lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
669     return 1;
670 }
671 
lcanvas_saveLayer(lua_State * L)672 static int lcanvas_saveLayer(lua_State* L) {
673     SkPaint paint;
674     lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(NULL, lua2OptionalPaint(L, 2, &paint)));
675     return 1;
676 }
677 
lcanvas_restore(lua_State * L)678 static int lcanvas_restore(lua_State* L) {
679     get_ref<SkCanvas>(L, 1)->restore();
680     return 0;
681 }
682 
lcanvas_scale(lua_State * L)683 static int lcanvas_scale(lua_State* L) {
684     SkScalar sx = lua2scalar_def(L, 2, 1);
685     SkScalar sy = lua2scalar_def(L, 3, sx);
686     get_ref<SkCanvas>(L, 1)->scale(sx, sy);
687     return 0;
688 }
689 
lcanvas_translate(lua_State * L)690 static int lcanvas_translate(lua_State* L) {
691     SkScalar tx = lua2scalar_def(L, 2, 0);
692     SkScalar ty = lua2scalar_def(L, 3, 0);
693     get_ref<SkCanvas>(L, 1)->translate(tx, ty);
694     return 0;
695 }
696 
lcanvas_rotate(lua_State * L)697 static int lcanvas_rotate(lua_State* L) {
698     SkScalar degrees = lua2scalar_def(L, 2, 0);
699     get_ref<SkCanvas>(L, 1)->rotate(degrees);
700     return 0;
701 }
702 
lcanvas_concat(lua_State * L)703 static int lcanvas_concat(lua_State* L) {
704     get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
705     return 0;
706 }
707 
lcanvas_newSurface(lua_State * L)708 static int lcanvas_newSurface(lua_State* L) {
709     int width = lua2int_def(L, 2, 0);
710     int height = lua2int_def(L, 3, 0);
711     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
712     SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
713     if (NULL == surface) {
714         lua_pushnil(L);
715     } else {
716         push_ref(L, surface)->unref();
717     }
718     return 1;
719 }
720 
lcanvas_gc(lua_State * L)721 static int lcanvas_gc(lua_State* L) {
722     get_ref<SkCanvas>(L, 1)->unref();
723     return 0;
724 }
725 
726 const struct luaL_Reg gSkCanvas_Methods[] = {
727     { "clear", lcanvas_clear },
728     { "drawColor", lcanvas_drawColor },
729     { "drawPaint", lcanvas_drawPaint },
730     { "drawRect", lcanvas_drawRect },
731     { "drawOval", lcanvas_drawOval },
732     { "drawCircle", lcanvas_drawCircle },
733     { "drawImage", lcanvas_drawImage },
734     { "drawImageRect", lcanvas_drawImageRect },
735     { "drawPatch", lcanvas_drawPatch },
736     { "drawPath", lcanvas_drawPath },
737     { "drawPicture", lcanvas_drawPicture },
738     { "drawText", lcanvas_drawText },
739     { "drawTextBlob", lcanvas_drawTextBlob },
740     { "getSaveCount", lcanvas_getSaveCount },
741     { "getTotalMatrix", lcanvas_getTotalMatrix },
742     { "getClipStack", lcanvas_getClipStack },
743 #if SK_SUPPORT_GPU
744     { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
745 #endif
746     { "save", lcanvas_save },
747     { "saveLayer", lcanvas_saveLayer },
748     { "restore", lcanvas_restore },
749     { "scale", lcanvas_scale },
750     { "translate", lcanvas_translate },
751     { "rotate", lcanvas_rotate },
752     { "concat", lcanvas_concat },
753 
754     { "newSurface", lcanvas_newSurface },
755 
756     { "__gc", lcanvas_gc },
757     { NULL, NULL }
758 };
759 
760 ///////////////////////////////////////////////////////////////////////////////
761 
ldocument_beginPage(lua_State * L)762 static int ldocument_beginPage(lua_State* L) {
763     const SkRect* contentPtr = NULL;
764     push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
765                                                      lua2scalar(L, 3),
766                                                      contentPtr));
767     return 1;
768 }
769 
ldocument_endPage(lua_State * L)770 static int ldocument_endPage(lua_State* L) {
771     get_ref<SkDocument>(L, 1)->endPage();
772     return 0;
773 }
774 
ldocument_close(lua_State * L)775 static int ldocument_close(lua_State* L) {
776     get_ref<SkDocument>(L, 1)->close();
777     return 0;
778 }
779 
ldocument_gc(lua_State * L)780 static int ldocument_gc(lua_State* L) {
781     get_ref<SkDocument>(L, 1)->unref();
782     return 0;
783 }
784 
785 static const struct luaL_Reg gSkDocument_Methods[] = {
786     { "beginPage", ldocument_beginPage },
787     { "endPage", ldocument_endPage },
788     { "close", ldocument_close },
789     { "__gc", ldocument_gc },
790     { NULL, NULL }
791 };
792 
793 ///////////////////////////////////////////////////////////////////////////////
794 
lpaint_isAntiAlias(lua_State * L)795 static int lpaint_isAntiAlias(lua_State* L) {
796     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
797     return 1;
798 }
799 
lpaint_setAntiAlias(lua_State * L)800 static int lpaint_setAntiAlias(lua_State* L) {
801     get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
802     return 0;
803 }
804 
lpaint_isDither(lua_State * L)805 static int lpaint_isDither(lua_State* L) {
806     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
807     return 1;
808 }
809 
lpaint_setDither(lua_State * L)810 static int lpaint_setDither(lua_State* L) {
811     get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
812     return 0;
813 }
814 
lpaint_isUnderlineText(lua_State * L)815 static int lpaint_isUnderlineText(lua_State* L) {
816     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
817     return 1;
818 }
819 
lpaint_isStrikeThruText(lua_State * L)820 static int lpaint_isStrikeThruText(lua_State* L) {
821     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
822     return 1;
823 }
824 
lpaint_isFakeBoldText(lua_State * L)825 static int lpaint_isFakeBoldText(lua_State* L) {
826     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
827     return 1;
828 }
829 
lpaint_isLinearText(lua_State * L)830 static int lpaint_isLinearText(lua_State* L) {
831     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
832     return 1;
833 }
834 
lpaint_isSubpixelText(lua_State * L)835 static int lpaint_isSubpixelText(lua_State* L) {
836     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
837     return 1;
838 }
839 
lpaint_setSubpixelText(lua_State * L)840 static int lpaint_setSubpixelText(lua_State* L) {
841     get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
842     return 1;
843 }
844 
lpaint_isDevKernText(lua_State * L)845 static int lpaint_isDevKernText(lua_State* L) {
846     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
847     return 1;
848 }
849 
lpaint_isLCDRenderText(lua_State * L)850 static int lpaint_isLCDRenderText(lua_State* L) {
851     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
852     return 1;
853 }
854 
lpaint_setLCDRenderText(lua_State * L)855 static int lpaint_setLCDRenderText(lua_State* L) {
856     get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
857     return 1;
858 }
859 
lpaint_isEmbeddedBitmapText(lua_State * L)860 static int lpaint_isEmbeddedBitmapText(lua_State* L) {
861     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
862     return 1;
863 }
864 
lpaint_isAutohinted(lua_State * L)865 static int lpaint_isAutohinted(lua_State* L) {
866     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
867     return 1;
868 }
869 
lpaint_isVerticalText(lua_State * L)870 static int lpaint_isVerticalText(lua_State* L) {
871     lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
872     return 1;
873 }
874 
lpaint_getAlpha(lua_State * L)875 static int lpaint_getAlpha(lua_State* L) {
876     SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
877     return 1;
878 }
879 
lpaint_setAlpha(lua_State * L)880 static int lpaint_setAlpha(lua_State* L) {
881     get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
882     return 0;
883 }
884 
lpaint_getColor(lua_State * L)885 static int lpaint_getColor(lua_State* L) {
886     SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
887     return 1;
888 }
889 
lpaint_setColor(lua_State * L)890 static int lpaint_setColor(lua_State* L) {
891     get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
892     return 0;
893 }
894 
lpaint_getTextSize(lua_State * L)895 static int lpaint_getTextSize(lua_State* L) {
896     SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
897     return 1;
898 }
899 
lpaint_getTextScaleX(lua_State * L)900 static int lpaint_getTextScaleX(lua_State* L) {
901     SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
902     return 1;
903 }
904 
lpaint_getTextSkewX(lua_State * L)905 static int lpaint_getTextSkewX(lua_State* L) {
906     SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
907     return 1;
908 }
909 
lpaint_setTextSize(lua_State * L)910 static int lpaint_setTextSize(lua_State* L) {
911     get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
912     return 0;
913 }
914 
lpaint_getTypeface(lua_State * L)915 static int lpaint_getTypeface(lua_State* L) {
916     push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
917     return 1;
918 }
919 
lpaint_setTypeface(lua_State * L)920 static int lpaint_setTypeface(lua_State* L) {
921     get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
922     return 0;
923 }
924 
lpaint_getHinting(lua_State * L)925 static int lpaint_getHinting(lua_State* L) {
926     SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
927     return 1;
928 }
929 
lpaint_getFilterQuality(lua_State * L)930 static int lpaint_getFilterQuality(lua_State* L) {
931     SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality());
932     return 1;
933 }
934 
lpaint_setFilterQuality(lua_State * L)935 static int lpaint_setFilterQuality(lua_State* L) {
936     int level = lua2int_def(L, 2, -1);
937     if (level >= 0 && level <= 3) {
938         get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level);
939     }
940     return 0;
941 }
942 
lpaint_getFontID(lua_State * L)943 static int lpaint_getFontID(lua_State* L) {
944     SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
945     SkLua(L).pushU32(SkTypeface::UniqueID(face));
946     return 1;
947 }
948 
949 static const struct {
950     const char*     fLabel;
951     SkPaint::Align  fAlign;
952 } gAlignRec[] = {
953     { "left",   SkPaint::kLeft_Align },
954     { "center", SkPaint::kCenter_Align },
955     { "right",  SkPaint::kRight_Align },
956 };
957 
lpaint_getTextAlign(lua_State * L)958 static int lpaint_getTextAlign(lua_State* L) {
959     SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
960     for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
961         if (gAlignRec[i].fAlign == align) {
962             lua_pushstring(L, gAlignRec[i].fLabel);
963             return 1;
964         }
965     }
966     return 0;
967 }
968 
lpaint_setTextAlign(lua_State * L)969 static int lpaint_setTextAlign(lua_State* L) {
970     if (lua_isstring(L, 2)) {
971         size_t len;
972         const char* label = lua_tolstring(L, 2, &len);
973 
974         for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
975             if (!strcmp(gAlignRec[i].fLabel, label)) {
976                 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
977                 break;
978             }
979         }
980     }
981     return 0;
982 }
983 
lpaint_getStroke(lua_State * L)984 static int lpaint_getStroke(lua_State* L) {
985     lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
986     return 1;
987 }
988 
lpaint_setStroke(lua_State * L)989 static int lpaint_setStroke(lua_State* L) {
990     SkPaint::Style style;
991 
992     if (lua_toboolean(L, 2)) {
993         style = SkPaint::kStroke_Style;
994     } else {
995         style = SkPaint::kFill_Style;
996     }
997     get_obj<SkPaint>(L, 1)->setStyle(style);
998     return 0;
999 }
1000 
lpaint_getStrokeCap(lua_State * L)1001 static int lpaint_getStrokeCap(lua_State* L) {
1002     SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
1003     return 1;
1004 }
1005 
lpaint_getStrokeJoin(lua_State * L)1006 static int lpaint_getStrokeJoin(lua_State* L) {
1007     SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
1008     return 1;
1009 }
1010 
lpaint_getTextEncoding(lua_State * L)1011 static int lpaint_getTextEncoding(lua_State* L) {
1012     SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
1013     return 1;
1014 }
1015 
lpaint_getStrokeWidth(lua_State * L)1016 static int lpaint_getStrokeWidth(lua_State* L) {
1017     SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
1018     return 1;
1019 }
1020 
lpaint_setStrokeWidth(lua_State * L)1021 static int lpaint_setStrokeWidth(lua_State* L) {
1022     get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
1023     return 0;
1024 }
1025 
lpaint_getStrokeMiter(lua_State * L)1026 static int lpaint_getStrokeMiter(lua_State* L) {
1027     SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
1028     return 1;
1029 }
1030 
lpaint_measureText(lua_State * L)1031 static int lpaint_measureText(lua_State* L) {
1032     if (lua_isstring(L, 2)) {
1033         size_t len;
1034         const char* text = lua_tolstring(L, 2, &len);
1035         SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
1036         return 1;
1037     }
1038     return 0;
1039 }
1040 
1041 struct FontMetrics {
1042     SkScalar    fTop;       //!< The greatest distance above the baseline for any glyph (will be <= 0)
1043     SkScalar    fAscent;    //!< The recommended distance above the baseline (will be <= 0)
1044     SkScalar    fDescent;   //!< The recommended distance below the baseline (will be >= 0)
1045     SkScalar    fBottom;    //!< The greatest distance below the baseline for any glyph (will be >= 0)
1046     SkScalar    fLeading;   //!< The recommended distance to add between lines of text (will be >= 0)
1047     SkScalar    fAvgCharWidth;  //!< the average charactor width (>= 0)
1048     SkScalar    fXMin;      //!< The minimum bounding box x value for all glyphs
1049     SkScalar    fXMax;      //!< The maximum bounding box x value for all glyphs
1050     SkScalar    fXHeight;   //!< the height of an 'x' in px, or 0 if no 'x' in face
1051 };
1052 
lpaint_getFontMetrics(lua_State * L)1053 static int lpaint_getFontMetrics(lua_State* L) {
1054     SkPaint::FontMetrics fm;
1055     SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
1056 
1057     lua_newtable(L);
1058     setfield_scalar(L, "top", fm.fTop);
1059     setfield_scalar(L, "ascent", fm.fAscent);
1060     setfield_scalar(L, "descent", fm.fDescent);
1061     setfield_scalar(L, "bottom", fm.fBottom);
1062     setfield_scalar(L, "leading", fm.fLeading);
1063     SkLua(L).pushScalar(height);
1064     return 2;
1065 }
1066 
lpaint_getEffects(lua_State * L)1067 static int lpaint_getEffects(lua_State* L) {
1068     const SkPaint* paint = get_obj<SkPaint>(L, 1);
1069 
1070     lua_newtable(L);
1071     setfield_bool_if(L, "looper",      !!paint->getLooper());
1072     setfield_bool_if(L, "pathEffect",  !!paint->getPathEffect());
1073     setfield_bool_if(L, "rasterizer",  !!paint->getRasterizer());
1074     setfield_bool_if(L, "maskFilter",  !!paint->getMaskFilter());
1075     setfield_bool_if(L, "shader",      !!paint->getShader());
1076     setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1077     setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
1078     setfield_bool_if(L, "xfermode",    !!paint->getXfermode());
1079     return 1;
1080 }
1081 
lpaint_getImageFilter(lua_State * L)1082 static int lpaint_getImageFilter(lua_State* L) {
1083     const SkPaint* paint = get_obj<SkPaint>(L, 1);
1084     SkImageFilter* imf = paint->getImageFilter();
1085     if (imf) {
1086         push_ref(L, imf);
1087         return 1;
1088     }
1089     return 0;
1090 }
1091 
lpaint_setImageFilter(lua_State * L)1092 static int lpaint_setImageFilter(lua_State* L) {
1093     SkPaint* paint = get_obj<SkPaint>(L, 1);
1094     paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1095     return 0;
1096 }
1097 
lpaint_getShader(lua_State * L)1098 static int lpaint_getShader(lua_State* L) {
1099     const SkPaint* paint = get_obj<SkPaint>(L, 1);
1100     SkShader* shader = paint->getShader();
1101     if (shader) {
1102         push_ref(L, shader);
1103         return 1;
1104     }
1105     return 0;
1106 }
1107 
lpaint_setShader(lua_State * L)1108 static int lpaint_setShader(lua_State* L) {
1109     SkPaint* paint = get_obj<SkPaint>(L, 1);
1110     paint->setShader(get_ref<SkShader>(L, 2));
1111     return 0;
1112 }
1113 
lpaint_getPathEffect(lua_State * L)1114 static int lpaint_getPathEffect(lua_State* L) {
1115     const SkPaint* paint = get_obj<SkPaint>(L, 1);
1116     SkPathEffect* pe = paint->getPathEffect();
1117     if (pe) {
1118         push_ref(L, pe);
1119         return 1;
1120     }
1121     return 0;
1122 }
1123 
lpaint_gc(lua_State * L)1124 static int lpaint_gc(lua_State* L) {
1125     get_obj<SkPaint>(L, 1)->~SkPaint();
1126     return 0;
1127 }
1128 
1129 static const struct luaL_Reg gSkPaint_Methods[] = {
1130     { "isAntiAlias", lpaint_isAntiAlias },
1131     { "setAntiAlias", lpaint_setAntiAlias },
1132     { "isDither", lpaint_isDither },
1133     { "setDither", lpaint_setDither },
1134     { "getFilterQuality", lpaint_getFilterQuality },
1135     { "setFilterQuality", lpaint_setFilterQuality },
1136     { "isUnderlineText", lpaint_isUnderlineText },
1137     { "isStrikeThruText", lpaint_isStrikeThruText },
1138     { "isFakeBoldText", lpaint_isFakeBoldText },
1139     { "isLinearText", lpaint_isLinearText },
1140     { "isSubpixelText", lpaint_isSubpixelText },
1141     { "setSubpixelText", lpaint_setSubpixelText },
1142     { "isDevKernText", lpaint_isDevKernText },
1143     { "isLCDRenderText", lpaint_isLCDRenderText },
1144     { "setLCDRenderText", lpaint_setLCDRenderText },
1145     { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1146     { "isAutohinted", lpaint_isAutohinted },
1147     { "isVerticalText", lpaint_isVerticalText },
1148     { "getAlpha", lpaint_getAlpha },
1149     { "setAlpha", lpaint_setAlpha },
1150     { "getColor", lpaint_getColor },
1151     { "setColor", lpaint_setColor },
1152     { "getTextSize", lpaint_getTextSize },
1153     { "setTextSize", lpaint_setTextSize },
1154     { "getTextScaleX", lpaint_getTextScaleX },
1155     { "getTextSkewX", lpaint_getTextSkewX },
1156     { "getTypeface", lpaint_getTypeface },
1157     { "setTypeface", lpaint_setTypeface },
1158     { "getHinting", lpaint_getHinting },
1159     { "getFontID", lpaint_getFontID },
1160     { "getTextAlign", lpaint_getTextAlign },
1161     { "setTextAlign", lpaint_setTextAlign },
1162     { "getStroke", lpaint_getStroke },
1163     { "setStroke", lpaint_setStroke },
1164     { "getStrokeCap", lpaint_getStrokeCap },
1165     { "getStrokeJoin", lpaint_getStrokeJoin },
1166     { "getTextEncoding", lpaint_getTextEncoding },
1167     { "getStrokeWidth", lpaint_getStrokeWidth },
1168     { "setStrokeWidth", lpaint_setStrokeWidth },
1169     { "getStrokeMiter", lpaint_getStrokeMiter },
1170     { "measureText", lpaint_measureText },
1171     { "getFontMetrics", lpaint_getFontMetrics },
1172     { "getEffects", lpaint_getEffects },
1173     { "getImageFilter", lpaint_getImageFilter },
1174     { "setImageFilter", lpaint_setImageFilter },
1175     { "getShader", lpaint_getShader },
1176     { "setShader", lpaint_setShader },
1177     { "getPathEffect", lpaint_getPathEffect },
1178     { "__gc", lpaint_gc },
1179     { NULL, NULL }
1180 };
1181 
1182 ///////////////////////////////////////////////////////////////////////////////
1183 
mode2string(SkShader::TileMode mode)1184 static const char* mode2string(SkShader::TileMode mode) {
1185     static const char* gNames[] = { "clamp", "repeat", "mirror" };
1186     SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1187     return gNames[mode];
1188 }
1189 
gradtype2string(SkShader::GradientType t)1190 static const char* gradtype2string(SkShader::GradientType t) {
1191     static const char* gNames[] = {
1192         "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1193     };
1194     SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1195     return gNames[t];
1196 }
1197 
lshader_isOpaque(lua_State * L)1198 static int lshader_isOpaque(lua_State* L) {
1199     SkShader* shader = get_ref<SkShader>(L, 1);
1200     return shader && shader->isOpaque();
1201 }
1202 
lshader_asABitmap(lua_State * L)1203 static int lshader_asABitmap(lua_State* L) {
1204     SkShader* shader = get_ref<SkShader>(L, 1);
1205     if (shader) {
1206         SkBitmap bm;
1207         SkMatrix matrix;
1208         SkShader::TileMode modes[2];
1209         switch (shader->asABitmap(&bm, &matrix, modes)) {
1210             case SkShader::kDefault_BitmapType:
1211                 lua_newtable(L);
1212                 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1213                 setfield_number(L, "width", bm.width());
1214                 setfield_number(L, "height", bm.height());
1215                 setfield_string(L, "tileX", mode2string(modes[0]));
1216                 setfield_string(L, "tileY", mode2string(modes[1]));
1217                 return 1;
1218             default:
1219                 break;
1220         }
1221     }
1222     return 0;
1223 }
1224 
lshader_asAGradient(lua_State * L)1225 static int lshader_asAGradient(lua_State* L) {
1226     SkShader* shader = get_ref<SkShader>(L, 1);
1227     if (shader) {
1228         SkShader::GradientInfo info;
1229         sk_bzero(&info, sizeof(info));
1230 
1231         SkColor colors[3];  // hacked in for extracting info on 3 color case.
1232         SkScalar pos[3];
1233 
1234         info.fColorCount = 3;
1235         info.fColors = &colors[0];
1236         info.fColorOffsets = &pos[0];
1237 
1238         SkShader::GradientType t = shader->asAGradient(&info);
1239 
1240         if (SkShader::kNone_GradientType != t) {
1241             lua_newtable(L);
1242             setfield_string(L, "type", gradtype2string(t));
1243             setfield_number(L, "colorCount", info.fColorCount);
1244             setfield_string(L, "tile", mode2string(info.fTileMode));
1245 
1246             if (info.fColorCount == 3){
1247                 setfield_number(L, "midPos", pos[1]);
1248             }
1249 
1250             return 1;
1251         }
1252     }
1253     return 0;
1254 }
1255 
lshader_gc(lua_State * L)1256 static int lshader_gc(lua_State* L) {
1257     get_ref<SkShader>(L, 1)->unref();
1258     return 0;
1259 }
1260 
1261 static const struct luaL_Reg gSkShader_Methods[] = {
1262     { "isOpaque",       lshader_isOpaque },
1263     { "asABitmap",      lshader_asABitmap },
1264     { "asAGradient",    lshader_asAGradient },
1265     { "__gc",           lshader_gc },
1266     { NULL, NULL }
1267 };
1268 
1269 ///////////////////////////////////////////////////////////////////////////////
1270 
lpatheffect_asADash(lua_State * L)1271 static int lpatheffect_asADash(lua_State* L) {
1272     SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1273     if (pe) {
1274         SkPathEffect::DashInfo info;
1275         SkPathEffect::DashType dashType = pe->asADash(&info);
1276         if (SkPathEffect::kDash_DashType == dashType) {
1277             SkAutoTArray<SkScalar> intervals(info.fCount);
1278             info.fIntervals = intervals.get();
1279             pe->asADash(&info);
1280             SkLua(L).pushDash(info);
1281             return 1;
1282         }
1283     }
1284     return 0;
1285 }
1286 
lpatheffect_gc(lua_State * L)1287 static int lpatheffect_gc(lua_State* L) {
1288     get_ref<SkPathEffect>(L, 1)->unref();
1289     return 0;
1290 }
1291 
1292 static const struct luaL_Reg gSkPathEffect_Methods[] = {
1293     { "asADash",        lpatheffect_asADash },
1294     { "__gc",           lpatheffect_gc },
1295     { NULL, NULL }
1296 };
1297 
1298 ///////////////////////////////////////////////////////////////////////////////
1299 
lpimagefilter_gc(lua_State * L)1300 static int lpimagefilter_gc(lua_State* L) {
1301     get_ref<SkImageFilter>(L, 1)->unref();
1302     return 0;
1303 }
1304 
1305 static const struct luaL_Reg gSkImageFilter_Methods[] = {
1306     { "__gc",       lpimagefilter_gc },
1307     { NULL, NULL }
1308 };
1309 
1310 ///////////////////////////////////////////////////////////////////////////////
1311 
lmatrix_getType(lua_State * L)1312 static int lmatrix_getType(lua_State* L) {
1313     SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
1314 
1315     lua_newtable(L);
1316     setfield_boolean(L, "translate",   SkToBool(mask & SkMatrix::kTranslate_Mask));
1317     setfield_boolean(L, "scale",       SkToBool(mask & SkMatrix::kScale_Mask));
1318     setfield_boolean(L, "affine",      SkToBool(mask & SkMatrix::kAffine_Mask));
1319     setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1320     return 1;
1321 }
1322 
lmatrix_getScaleX(lua_State * L)1323 static int lmatrix_getScaleX(lua_State* L) {
1324     lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1325     return 1;
1326 }
1327 
lmatrix_getScaleY(lua_State * L)1328 static int lmatrix_getScaleY(lua_State* L) {
1329     lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1330     return 1;
1331 }
1332 
lmatrix_getTranslateX(lua_State * L)1333 static int lmatrix_getTranslateX(lua_State* L) {
1334     lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1335     return 1;
1336 }
1337 
lmatrix_getTranslateY(lua_State * L)1338 static int lmatrix_getTranslateY(lua_State* L) {
1339     lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1340     return 1;
1341 }
1342 
lmatrix_invert(lua_State * L)1343 static int lmatrix_invert(lua_State* L) {
1344     lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1345     return 1;
1346 }
1347 
lmatrix_mapXY(lua_State * L)1348 static int lmatrix_mapXY(lua_State* L) {
1349     SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1350     get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1351     lua_pushnumber(L, pt.x());
1352     lua_pushnumber(L, pt.y());
1353     return 2;
1354 }
1355 
lmatrix_setRectToRect(lua_State * L)1356 static int lmatrix_setRectToRect(lua_State* L) {
1357     SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1358     SkRect srcR, dstR;
1359     lua2rect(L, 2, &srcR);
1360     lua2rect(L, 3, &dstR);
1361     const char* scaleToFitStr = lua_tostring(L, 4);
1362     SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1363 
1364     if (scaleToFitStr) {
1365         const struct {
1366             const char* fName;
1367             SkMatrix::ScaleToFit fScaleToFit;
1368         } rec[] = {
1369             { "fill",   SkMatrix::kFill_ScaleToFit },
1370             { "start",  SkMatrix::kStart_ScaleToFit },
1371             { "center", SkMatrix::kCenter_ScaleToFit },
1372             { "end",    SkMatrix::kEnd_ScaleToFit },
1373         };
1374 
1375         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1376             if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1377                 scaleToFit = rec[i].fScaleToFit;
1378                 break;
1379             }
1380         }
1381     }
1382 
1383     matrix->setRectToRect(srcR, dstR, scaleToFit);
1384     return 0;
1385 }
1386 
1387 static const struct luaL_Reg gSkMatrix_Methods[] = {
1388     { "getType", lmatrix_getType },
1389     { "getScaleX", lmatrix_getScaleX },
1390     { "getScaleY", lmatrix_getScaleY },
1391     { "getTranslateX", lmatrix_getTranslateX },
1392     { "getTranslateY", lmatrix_getTranslateY },
1393     { "setRectToRect", lmatrix_setRectToRect },
1394     { "invert", lmatrix_invert },
1395     { "mapXY", lmatrix_mapXY },
1396     { NULL, NULL }
1397 };
1398 
1399 ///////////////////////////////////////////////////////////////////////////////
1400 
lpath_getBounds(lua_State * L)1401 static int lpath_getBounds(lua_State* L) {
1402     SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1403     return 1;
1404 }
1405 
fill_type_to_str(SkPath::FillType fill)1406 static const char* fill_type_to_str(SkPath::FillType fill) {
1407     switch (fill) {
1408         case SkPath::kEvenOdd_FillType:
1409             return "even-odd";
1410         case SkPath::kWinding_FillType:
1411             return "winding";
1412         case SkPath::kInverseEvenOdd_FillType:
1413             return "inverse-even-odd";
1414         case SkPath::kInverseWinding_FillType:
1415             return "inverse-winding";
1416     }
1417     return "unknown";
1418 }
1419 
lpath_getFillType(lua_State * L)1420 static int lpath_getFillType(lua_State* L) {
1421     SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1422     SkLua(L).pushString(fill_type_to_str(fill));
1423     return 1;
1424 }
1425 
segment_masks_to_str(uint32_t segmentMasks)1426 static SkString segment_masks_to_str(uint32_t segmentMasks) {
1427     SkString result;
1428     bool first = true;
1429     if (SkPath::kLine_SegmentMask & segmentMasks) {
1430         result.append("line");
1431         first = false;
1432         SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1433     }
1434     if (SkPath::kQuad_SegmentMask & segmentMasks) {
1435         if (!first) {
1436             result.append(" ");
1437         }
1438         result.append("quad");
1439         first = false;
1440         SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1441     }
1442     if (SkPath::kConic_SegmentMask & segmentMasks) {
1443         if (!first) {
1444             result.append(" ");
1445         }
1446         result.append("conic");
1447         first = false;
1448         SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1449     }
1450     if (SkPath::kCubic_SegmentMask & segmentMasks) {
1451         if (!first) {
1452             result.append(" ");
1453         }
1454         result.append("cubic");
1455         SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1456     }
1457     SkASSERT(0 == segmentMasks);
1458     return result;
1459 }
1460 
lpath_getSegmentTypes(lua_State * L)1461 static int lpath_getSegmentTypes(lua_State* L) {
1462     uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1463     SkLua(L).pushString(segment_masks_to_str(segMasks));
1464     return 1;
1465 }
1466 
lpath_isConvex(lua_State * L)1467 static int lpath_isConvex(lua_State* L) {
1468     bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1469     SkLua(L).pushBool(isConvex);
1470     return 1;
1471 }
1472 
lpath_isEmpty(lua_State * L)1473 static int lpath_isEmpty(lua_State* L) {
1474     lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1475     return 1;
1476 }
1477 
lpath_isRect(lua_State * L)1478 static int lpath_isRect(lua_State* L) {
1479     SkRect r;
1480     bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1481     int ret_count = 1;
1482     lua_pushboolean(L, pred);
1483     if (pred) {
1484         SkLua(L).pushRect(r);
1485         ret_count += 1;
1486     }
1487     return ret_count;
1488 }
1489 
dir2string(SkPath::Direction dir)1490 static const char* dir2string(SkPath::Direction dir) {
1491     static const char* gStr[] = {
1492         "unknown", "cw", "ccw"
1493     };
1494     SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1495     return gStr[dir];
1496 }
1497 
lpath_isNestedFillRects(lua_State * L)1498 static int lpath_isNestedFillRects(lua_State* L) {
1499     SkRect rects[2];
1500     SkPath::Direction dirs[2];
1501     bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
1502     int ret_count = 1;
1503     lua_pushboolean(L, pred);
1504     if (pred) {
1505         SkLua lua(L);
1506         lua.pushRect(rects[0]);
1507         lua.pushRect(rects[1]);
1508         lua_pushstring(L, dir2string(dirs[0]));
1509         lua_pushstring(L, dir2string(dirs[0]));
1510         ret_count += 4;
1511     }
1512     return ret_count;
1513 }
1514 
lpath_countPoints(lua_State * L)1515 static int lpath_countPoints(lua_State* L) {
1516     lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1517     return 1;
1518 }
1519 
lpath_reset(lua_State * L)1520 static int lpath_reset(lua_State* L) {
1521     get_obj<SkPath>(L, 1)->reset();
1522     return 0;
1523 }
1524 
lpath_moveTo(lua_State * L)1525 static int lpath_moveTo(lua_State* L) {
1526     get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1527     return 0;
1528 }
1529 
lpath_lineTo(lua_State * L)1530 static int lpath_lineTo(lua_State* L) {
1531     get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1532     return 0;
1533 }
1534 
lpath_quadTo(lua_State * L)1535 static int lpath_quadTo(lua_State* L) {
1536     get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1537                                   lua2scalar(L, 4), lua2scalar(L, 5));
1538     return 0;
1539 }
1540 
lpath_cubicTo(lua_State * L)1541 static int lpath_cubicTo(lua_State* L) {
1542     get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1543                                    lua2scalar(L, 4), lua2scalar(L, 5),
1544                                    lua2scalar(L, 6), lua2scalar(L, 7));
1545     return 0;
1546 }
1547 
lpath_close(lua_State * L)1548 static int lpath_close(lua_State* L) {
1549     get_obj<SkPath>(L, 1)->close();
1550     return 0;
1551 }
1552 
lpath_gc(lua_State * L)1553 static int lpath_gc(lua_State* L) {
1554     get_obj<SkPath>(L, 1)->~SkPath();
1555     return 0;
1556 }
1557 
1558 static const struct luaL_Reg gSkPath_Methods[] = {
1559     { "getBounds", lpath_getBounds },
1560     { "getFillType", lpath_getFillType },
1561     { "getSegmentTypes", lpath_getSegmentTypes },
1562     { "isConvex", lpath_isConvex },
1563     { "isEmpty", lpath_isEmpty },
1564     { "isRect", lpath_isRect },
1565     { "isNestedFillRects", lpath_isNestedFillRects },
1566     { "countPoints", lpath_countPoints },
1567     { "reset", lpath_reset },
1568     { "moveTo", lpath_moveTo },
1569     { "lineTo", lpath_lineTo },
1570     { "quadTo", lpath_quadTo },
1571     { "cubicTo", lpath_cubicTo },
1572     { "close", lpath_close },
1573     { "__gc", lpath_gc },
1574     { NULL, NULL }
1575 };
1576 
1577 ///////////////////////////////////////////////////////////////////////////////
1578 
rrect_type(const SkRRect & rr)1579 static const char* rrect_type(const SkRRect& rr) {
1580     switch (rr.getType()) {
1581         case SkRRect::kEmpty_Type: return "empty";
1582         case SkRRect::kRect_Type: return "rect";
1583         case SkRRect::kOval_Type: return "oval";
1584         case SkRRect::kSimple_Type: return "simple";
1585         case SkRRect::kNinePatch_Type: return "nine-patch";
1586         case SkRRect::kComplex_Type: return "complex";
1587     }
1588     SkDEBUGFAIL("never get here");
1589     return "";
1590 }
1591 
lrrect_rect(lua_State * L)1592 static int lrrect_rect(lua_State* L) {
1593     SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1594     return 1;
1595 }
1596 
lrrect_type(lua_State * L)1597 static int lrrect_type(lua_State* L) {
1598     lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1599     return 1;
1600 }
1601 
lrrect_radii(lua_State * L)1602 static int lrrect_radii(lua_State* L) {
1603     int corner = SkToInt(lua_tointeger(L, 2));
1604     SkVector v;
1605     if (corner < 0 || corner > 3) {
1606         SkDebugf("bad corner index %d", corner);
1607         v.set(0, 0);
1608     } else {
1609         v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1610     }
1611     lua_pushnumber(L, v.fX);
1612     lua_pushnumber(L, v.fY);
1613     return 2;
1614 }
1615 
lrrect_gc(lua_State * L)1616 static int lrrect_gc(lua_State* L) {
1617     get_obj<SkRRect>(L, 1)->~SkRRect();
1618     return 0;
1619 }
1620 
1621 static const struct luaL_Reg gSkRRect_Methods[] = {
1622     { "rect", lrrect_rect },
1623     { "type", lrrect_type },
1624     { "radii", lrrect_radii },
1625     { "__gc", lrrect_gc },
1626     { NULL, NULL }
1627 };
1628 
1629 ///////////////////////////////////////////////////////////////////////////////
1630 
limage_width(lua_State * L)1631 static int limage_width(lua_State* L) {
1632     lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1633     return 1;
1634 }
1635 
limage_height(lua_State * L)1636 static int limage_height(lua_State* L) {
1637     lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1638     return 1;
1639 }
1640 
limage_newShader(lua_State * L)1641 static int limage_newShader(lua_State* L) {
1642     SkShader::TileMode tmode = SkShader::kClamp_TileMode;
1643     const SkMatrix* localM = NULL;
1644     SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM));
1645     push_ref(L, shader.get());
1646     return 1;
1647 }
1648 
limage_gc(lua_State * L)1649 static int limage_gc(lua_State* L) {
1650     get_ref<SkImage>(L, 1)->unref();
1651     return 0;
1652 }
1653 
1654 static const struct luaL_Reg gSkImage_Methods[] = {
1655     { "width", limage_width },
1656     { "height", limage_height },
1657     { "newShader", limage_newShader },
1658     { "__gc", limage_gc },
1659     { NULL, NULL }
1660 };
1661 
1662 ///////////////////////////////////////////////////////////////////////////////
1663 
lsurface_width(lua_State * L)1664 static int lsurface_width(lua_State* L) {
1665     lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1666     return 1;
1667 }
1668 
lsurface_height(lua_State * L)1669 static int lsurface_height(lua_State* L) {
1670     lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1671     return 1;
1672 }
1673 
lsurface_getCanvas(lua_State * L)1674 static int lsurface_getCanvas(lua_State* L) {
1675     SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1676     if (NULL == canvas) {
1677         lua_pushnil(L);
1678     } else {
1679         push_ref(L, canvas);
1680         // note: we don't unref canvas, since getCanvas did not ref it.
1681         // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1682         // the real owner (the surface) go away, but still hold onto the canvas?
1683         // *really* we want to sort of ref the surface again, but have the native object
1684         // know that it is supposed to be treated as a canvas...
1685     }
1686     return 1;
1687 }
1688 
lsurface_newImageSnapshot(lua_State * L)1689 static int lsurface_newImageSnapshot(lua_State* L) {
1690     SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1691     if (NULL == image) {
1692         lua_pushnil(L);
1693     } else {
1694         push_ref(L, image)->unref();
1695     }
1696     return 1;
1697 }
1698 
lsurface_newSurface(lua_State * L)1699 static int lsurface_newSurface(lua_State* L) {
1700     int width = lua2int_def(L, 2, 0);
1701     int height = lua2int_def(L, 3, 0);
1702     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1703     SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1704     if (NULL == surface) {
1705         lua_pushnil(L);
1706     } else {
1707         push_ref(L, surface)->unref();
1708     }
1709     return 1;
1710 }
1711 
lsurface_gc(lua_State * L)1712 static int lsurface_gc(lua_State* L) {
1713     get_ref<SkSurface>(L, 1)->unref();
1714     return 0;
1715 }
1716 
1717 static const struct luaL_Reg gSkSurface_Methods[] = {
1718     { "width", lsurface_width },
1719     { "height", lsurface_height },
1720     { "getCanvas", lsurface_getCanvas },
1721     { "newImageSnapshot", lsurface_newImageSnapshot },
1722     { "newSurface", lsurface_newSurface },
1723     { "__gc", lsurface_gc },
1724     { NULL, NULL }
1725 };
1726 
1727 ///////////////////////////////////////////////////////////////////////////////
1728 
lpicturerecorder_beginRecording(lua_State * L)1729 static int lpicturerecorder_beginRecording(lua_State* L) {
1730     const SkScalar w = lua2scalar_def(L, 2, -1);
1731     const SkScalar h = lua2scalar_def(L, 3, -1);
1732     if (w <= 0 || h <= 0) {
1733         lua_pushnil(L);
1734         return 1;
1735     }
1736 
1737     SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1738     if (NULL == canvas) {
1739         lua_pushnil(L);
1740         return 1;
1741     }
1742 
1743     push_ref(L, canvas);
1744     return 1;
1745 }
1746 
lpicturerecorder_getCanvas(lua_State * L)1747 static int lpicturerecorder_getCanvas(lua_State* L) {
1748     SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1749     if (NULL == canvas) {
1750         lua_pushnil(L);
1751         return 1;
1752     }
1753     push_ref(L, canvas);
1754     return 1;
1755 }
1756 
lpicturerecorder_endRecording(lua_State * L)1757 static int lpicturerecorder_endRecording(lua_State* L) {
1758     SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1759     if (NULL == pic) {
1760         lua_pushnil(L);
1761         return 1;
1762     }
1763     push_ref(L, pic)->unref();
1764     return 1;
1765 }
1766 
lpicturerecorder_gc(lua_State * L)1767 static int lpicturerecorder_gc(lua_State* L) {
1768     get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1769     return 0;
1770 }
1771 
1772 static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1773     { "beginRecording", lpicturerecorder_beginRecording },
1774     { "getCanvas", lpicturerecorder_getCanvas },
1775     { "endRecording", lpicturerecorder_endRecording },
1776     { "__gc", lpicturerecorder_gc },
1777     { NULL, NULL }
1778 };
1779 
1780 ///////////////////////////////////////////////////////////////////////////////
1781 
lpicture_width(lua_State * L)1782 static int lpicture_width(lua_State* L) {
1783     lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1784     return 1;
1785 }
1786 
lpicture_height(lua_State * L)1787 static int lpicture_height(lua_State* L) {
1788     lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1789     return 1;
1790 }
1791 
lpicture_gc(lua_State * L)1792 static int lpicture_gc(lua_State* L) {
1793     get_ref<SkPicture>(L, 1)->unref();
1794     return 0;
1795 }
1796 
1797 static const struct luaL_Reg gSkPicture_Methods[] = {
1798     { "width", lpicture_width },
1799     { "height", lpicture_height },
1800     { "__gc", lpicture_gc },
1801     { NULL, NULL }
1802 };
1803 
1804 ///////////////////////////////////////////////////////////////////////////////
1805 
ltextblob_bounds(lua_State * L)1806 static int ltextblob_bounds(lua_State* L) {
1807     SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1808     return 1;
1809 }
1810 
ltextblob_gc(lua_State * L)1811 static int ltextblob_gc(lua_State* L) {
1812     SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1813     return 0;
1814 }
1815 
1816 static const struct luaL_Reg gSkTextBlob_Methods[] = {
1817     { "bounds", ltextblob_bounds },
1818     { "__gc", ltextblob_gc },
1819     { NULL, NULL }
1820 };
1821 
1822 ///////////////////////////////////////////////////////////////////////////////
1823 
ltypeface_getFamilyName(lua_State * L)1824 static int ltypeface_getFamilyName(lua_State* L) {
1825     SkString str;
1826     get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1827     lua_pushstring(L, str.c_str());
1828     return 1;
1829 }
1830 
ltypeface_getStyle(lua_State * L)1831 static int ltypeface_getStyle(lua_State* L) {
1832     lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1833     return 1;
1834 }
1835 
ltypeface_gc(lua_State * L)1836 static int ltypeface_gc(lua_State* L) {
1837     SkSafeUnref(get_ref<SkTypeface>(L, 1));
1838     return 0;
1839 }
1840 
1841 static const struct luaL_Reg gSkTypeface_Methods[] = {
1842     { "getFamilyName", ltypeface_getFamilyName },
1843     { "getStyle", ltypeface_getStyle },
1844     { "__gc", ltypeface_gc },
1845     { NULL, NULL }
1846 };
1847 
1848 ///////////////////////////////////////////////////////////////////////////////
1849 
1850 class AutoCallLua {
1851 public:
AutoCallLua(lua_State * L,const char func[],const char verb[])1852     AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1853         lua_getglobal(L, func);
1854         if (!lua_isfunction(L, -1)) {
1855             int t = lua_type(L, -1);
1856             SkDebugf("--- expected function %d\n", t);
1857         }
1858 
1859         lua_newtable(L);
1860         setfield_string(L, "verb", verb);
1861     }
1862 
~AutoCallLua()1863     ~AutoCallLua() {
1864         if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1865             SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1866         }
1867         lua_settop(fL, -1);
1868     }
1869 
1870 private:
1871     lua_State* fL;
1872 };
1873 
1874 #define AUTO_LUA(verb)  AutoCallLua acl(fL, fFunc.c_str(), verb)
1875 
1876 ///////////////////////////////////////////////////////////////////////////////
1877 
lsk_newDocumentPDF(lua_State * L)1878 static int lsk_newDocumentPDF(lua_State* L) {
1879     const char* file = NULL;
1880     if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1881         file = lua_tolstring(L, 1, NULL);
1882     }
1883 
1884     SkDocument* doc = SkDocument::CreatePDF(file);
1885     if (NULL == doc) {
1886         // do I need to push a nil on the stack and return 1?
1887         return 0;
1888     } else {
1889         push_ref(L, doc)->unref();
1890         return 1;
1891     }
1892 }
1893 
lsk_newBlurImageFilter(lua_State * L)1894 static int lsk_newBlurImageFilter(lua_State* L) {
1895     SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1896     SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1897     SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1898     if (NULL == imf) {
1899         lua_pushnil(L);
1900     } else {
1901         push_ref(L, imf)->unref();
1902     }
1903     return 1;
1904 }
1905 
lsk_newLinearGradient(lua_State * L)1906 static int lsk_newLinearGradient(lua_State* L) {
1907     SkScalar x0 = lua2scalar_def(L, 1, 0);
1908     SkScalar y0 = lua2scalar_def(L, 2, 0);
1909     SkColor  c0 = lua2color(L, 3);
1910     SkScalar x1 = lua2scalar_def(L, 4, 0);
1911     SkScalar y1 = lua2scalar_def(L, 5, 0);
1912     SkColor  c1 = lua2color(L, 6);
1913 
1914     SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1915     SkColor colors[] = { c0, c1 };
1916     SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
1917     if (NULL == s) {
1918         lua_pushnil(L);
1919     } else {
1920         push_ref(L, s)->unref();
1921     }
1922     return 1;
1923 }
1924 
lsk_newMatrix(lua_State * L)1925 static int lsk_newMatrix(lua_State* L) {
1926     push_new<SkMatrix>(L)->reset();
1927     return 1;
1928 }
1929 
lsk_newPaint(lua_State * L)1930 static int lsk_newPaint(lua_State* L) {
1931     push_new<SkPaint>(L);
1932     return 1;
1933 }
1934 
lsk_newPath(lua_State * L)1935 static int lsk_newPath(lua_State* L) {
1936     push_new<SkPath>(L);
1937     return 1;
1938 }
1939 
lsk_newPictureRecorder(lua_State * L)1940 static int lsk_newPictureRecorder(lua_State* L) {
1941     push_new<SkPictureRecorder>(L);
1942     return 1;
1943 }
1944 
lsk_newRRect(lua_State * L)1945 static int lsk_newRRect(lua_State* L) {
1946     push_new<SkRRect>(L)->setEmpty();
1947     return 1;
1948 }
1949 
1950 #include "SkTextBox.h"
1951 // Sk.newTextBlob(text, rect, paint)
lsk_newTextBlob(lua_State * L)1952 static int lsk_newTextBlob(lua_State* L) {
1953     const char* text = lua_tolstring(L, 1, NULL);
1954     SkRect bounds;
1955     lua2rect(L, 2, &bounds);
1956     const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1957 
1958     SkTextBox box;
1959     box.setMode(SkTextBox::kLineBreak_Mode);
1960     box.setBox(bounds);
1961     box.setText(text, strlen(text), paint);
1962 
1963     SkScalar newBottom;
1964     SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
1965     push_ref<SkTextBlob>(L, blob);
1966     SkLua(L).pushScalar(newBottom);
1967     return 2;
1968 }
1969 
lsk_newTypeface(lua_State * L)1970 static int lsk_newTypeface(lua_State* L) {
1971     const char* name = NULL;
1972     int style = SkTypeface::kNormal;
1973 
1974     int count = lua_gettop(L);
1975     if (count > 0 && lua_isstring(L, 1)) {
1976         name = lua_tolstring(L, 1, NULL);
1977         if (count > 1 && lua_isnumber(L, 2)) {
1978             style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1979         }
1980     }
1981 
1982     SkTypeface* face = SkTypeface::CreateFromName(name,
1983                                                   (SkTypeface::Style)style);
1984 //    SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1985     if (NULL == face) {
1986         face = SkTypeface::RefDefault();
1987     }
1988     push_ref(L, face)->unref();
1989     return 1;
1990 }
1991 
lsk_newRasterSurface(lua_State * L)1992 static int lsk_newRasterSurface(lua_State* L) {
1993     int width = lua2int_def(L, 1, 0);
1994     int height = lua2int_def(L, 2, 0);
1995     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1996     SkSurface* surface = SkSurface::NewRaster(info);
1997     if (NULL == surface) {
1998         lua_pushnil(L);
1999     } else {
2000         push_ref(L, surface)->unref();
2001     }
2002     return 1;
2003 }
2004 
lsk_loadImage(lua_State * L)2005 static int lsk_loadImage(lua_State* L) {
2006     if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
2007         const char* name = lua_tolstring(L, 1, NULL);
2008         SkAutoDataUnref data(SkData::NewFromFileName(name));
2009         if (data.get()) {
2010             SkImage* image = SkImage::NewFromData(data);
2011             if (image) {
2012                 push_ref(L, image)->unref();
2013                 return 1;
2014             }
2015         }
2016     }
2017     return 0;
2018 }
2019 
register_Sk(lua_State * L)2020 static void register_Sk(lua_State* L) {
2021     lua_newtable(L);
2022     lua_pushvalue(L, -1);
2023     lua_setglobal(L, "Sk");
2024     // the Sk table is still on top
2025 
2026     setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
2027     setfield_function(L, "loadImage", lsk_loadImage);
2028     setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
2029     setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
2030     setfield_function(L, "newMatrix", lsk_newMatrix);
2031     setfield_function(L, "newPaint", lsk_newPaint);
2032     setfield_function(L, "newPath", lsk_newPath);
2033     setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
2034     setfield_function(L, "newRRect", lsk_newRRect);
2035     setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
2036     setfield_function(L, "newTextBlob", lsk_newTextBlob);
2037     setfield_function(L, "newTypeface", lsk_newTypeface);
2038     lua_pop(L, 1);  // pop off the Sk table
2039 }
2040 
2041 #define REG_CLASS(L, C)                             \
2042     do {                                            \
2043         luaL_newmetatable(L, get_mtname<C>());      \
2044         lua_pushvalue(L, -1);                       \
2045         lua_setfield(L, -2, "__index");             \
2046         luaL_setfuncs(L, g##C##_Methods, 0);        \
2047         lua_pop(L, 1); /* pop off the meta-table */ \
2048     } while (0)
2049 
Load(lua_State * L)2050 void SkLua::Load(lua_State* L) {
2051     register_Sk(L);
2052     REG_CLASS(L, SkCanvas);
2053     REG_CLASS(L, SkDocument);
2054     REG_CLASS(L, SkImage);
2055     REG_CLASS(L, SkImageFilter);
2056     REG_CLASS(L, SkMatrix);
2057     REG_CLASS(L, SkPaint);
2058     REG_CLASS(L, SkPath);
2059     REG_CLASS(L, SkPathEffect);
2060     REG_CLASS(L, SkPicture);
2061     REG_CLASS(L, SkPictureRecorder);
2062     REG_CLASS(L, SkRRect);
2063     REG_CLASS(L, SkShader);
2064     REG_CLASS(L, SkSurface);
2065     REG_CLASS(L, SkTextBlob);
2066     REG_CLASS(L, SkTypeface);
2067 }
2068 
2069 extern "C" int luaopen_skia(lua_State* L);
luaopen_skia(lua_State * L)2070 extern "C" int luaopen_skia(lua_State* L) {
2071     SkLua::Load(L);
2072     return 0;
2073 }
2074