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