blob: 3dbcf8e629c91791628094d57a7c383dea2a002d [file] [log] [blame]
reed@google.com74ce6f02013-05-22 15:13:18 +00001/*
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"
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00009
10#if SK_SUPPORT_GPU
11#include "GrReducedClip.h"
12#endif
13
reed@google.com74ce6f02013-05-22 15:13:18 +000014#include "SkCanvas.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000015#include "SkData.h"
piotaixr4bcc2022014-09-17 14:33:30 -070016#include "SkDecodingImageGenerator.h"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000017#include "SkDocument.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000018#include "SkImage.h"
19#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000020#include "SkPaint.h"
21#include "SkPath.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000022#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000023#include "SkRRect.h"
24#include "SkString.h"
reed485557f2014-10-12 10:36:47 -070025#include "SkSurface.h"
fmalitab7425172014-08-26 07:56:44 -070026#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000027#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000028
29extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000030 #include "lua.h"
31 #include "lualib.h"
32 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000033}
34
reed@google.comfd345872013-05-22 20:53:42 +000035// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000036template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000037#define DEF_MTNAME(T) \
38 template <> const char* get_mtname<T>() { \
39 return #T "_LuaMetaTableName"; \
40 }
41
42DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000043DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000044DEF_MTNAME(SkImage)
reed@google.comfd345872013-05-22 20:53:42 +000045DEF_MTNAME(SkMatrix)
46DEF_MTNAME(SkRRect)
47DEF_MTNAME(SkPath)
48DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000049DEF_MTNAME(SkPathEffect)
reed@google.com5fdc9832013-07-24 15:47:52 +000050DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070051DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070052DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000053DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000054
reed@google.com3597b732013-05-22 20:12:50 +000055template <typename T> T* push_new(lua_State* L) {
56 T* addr = (T*)lua_newuserdata(L, sizeof(T));
57 new (addr) T;
58 luaL_getmetatable(L, get_mtname<T>());
59 lua_setmetatable(L, -2);
60 return addr;
61}
reed@google.com74ce6f02013-05-22 15:13:18 +000062
63template <typename T> void push_obj(lua_State* L, const T& obj) {
64 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000065 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000066 lua_setmetatable(L, -2);
67}
68
69template <typename T> void push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000070 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000071 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000072 lua_setmetatable(L, -2);
73}
74
75template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000076 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000077}
78
79template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000080 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000081}
82
reed@google.com88c9ec92013-05-22 15:43:21 +000083static bool lua2bool(lua_State* L, int index) {
84 return !!lua_toboolean(L, index);
85}
86
reed@google.com74ce6f02013-05-22 15:13:18 +000087///////////////////////////////////////////////////////////////////////////////
88
reed@google.com3597b732013-05-22 20:12:50 +000089SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
90 fL = luaL_newstate();
91 luaL_openlibs(fL);
92 SkLua::Load(fL);
93}
94
95SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
96
97SkLua::~SkLua() {
98 if (fWeOwnL) {
99 if (fTermCode.size() > 0) {
100 lua_getglobal(fL, fTermCode.c_str());
101 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
102 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
103 }
104 }
105 lua_close(fL);
106 }
107}
108
109bool SkLua::runCode(const char code[]) {
110 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
111 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000112 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000113 return false;
114 }
115 return true;
116}
117
118bool SkLua::runCode(const void* code, size_t size) {
119 SkString str((const char*)code, size);
120 return this->runCode(str.c_str());
121}
122
123///////////////////////////////////////////////////////////////////////////////
124
125#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
126
reed@google.com29563872013-07-10 21:23:49 +0000127static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
128 if (pred) {
129 lua_pushboolean(L, true);
130 lua_setfield(L, -2, key);
131 }
132}
133
reed@google.com74ce6f02013-05-22 15:13:18 +0000134static void setfield_string(lua_State* L, const char key[], const char value[]) {
135 lua_pushstring(L, value);
136 lua_setfield(L, -2, key);
137}
138
139static void setfield_number(lua_State* L, const char key[], double value) {
140 lua_pushnumber(L, value);
141 lua_setfield(L, -2, key);
142}
143
humper@google.com2815c192013-07-10 22:42:30 +0000144static void setfield_boolean(lua_State* L, const char key[], bool value) {
145 lua_pushboolean(L, value);
146 lua_setfield(L, -2, key);
147}
148
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000149static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
150 setfield_number(L, key, SkScalarToLua(value));
151}
152
reed@google.com3597b732013-05-22 20:12:50 +0000153static void setfield_function(lua_State* L,
154 const char key[], lua_CFunction value) {
155 lua_pushcfunction(L, value);
156 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000157}
158
reed@google.come3823fd2013-05-30 18:55:14 +0000159static void setarray_number(lua_State* L, int index, double value) {
160 lua_pushnumber(L, value);
161 lua_rawseti(L, -2, index);
162}
163
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000164static void setarray_scalar(lua_State* L, int index, SkScalar value) {
165 setarray_number(L, index, SkScalarToLua(value));
166}
167
reed@google.com74ce6f02013-05-22 15:13:18 +0000168void SkLua::pushBool(bool value, const char key[]) {
169 lua_pushboolean(fL, value);
170 CHECK_SETFIELD(key);
171}
172
173void SkLua::pushString(const char str[], const char key[]) {
174 lua_pushstring(fL, str);
175 CHECK_SETFIELD(key);
176}
177
reed@google.come3823fd2013-05-30 18:55:14 +0000178void SkLua::pushString(const char str[], size_t length, const char key[]) {
179 // TODO: how to do this w/o making a copy?
180 SkString s(str, length);
181 lua_pushstring(fL, s.c_str());
182 CHECK_SETFIELD(key);
183}
184
reed@google.com74ce6f02013-05-22 15:13:18 +0000185void SkLua::pushString(const SkString& str, const char key[]) {
186 lua_pushstring(fL, str.c_str());
187 CHECK_SETFIELD(key);
188}
189
190void SkLua::pushColor(SkColor color, const char key[]) {
191 lua_newtable(fL);
192 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
193 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
194 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
195 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
196 CHECK_SETFIELD(key);
197}
198
reed@google.come3823fd2013-05-30 18:55:14 +0000199void SkLua::pushU32(uint32_t value, const char key[]) {
200 lua_pushnumber(fL, (double)value);
201 CHECK_SETFIELD(key);
202}
203
reed@google.com74ce6f02013-05-22 15:13:18 +0000204void SkLua::pushScalar(SkScalar value, const char key[]) {
205 lua_pushnumber(fL, SkScalarToLua(value));
206 CHECK_SETFIELD(key);
207}
208
reed@google.come3823fd2013-05-30 18:55:14 +0000209void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
210 lua_newtable(fL);
211 for (int i = 0; i < count; ++i) {
212 // make it base-1 to match lua convention
213 setarray_number(fL, i + 1, (double)array[i]);
214 }
215 CHECK_SETFIELD(key);
216}
217
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000218void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
219 lua_newtable(fL);
220 for (int i = 0; i < count; ++i) {
221 // make it base-1 to match lua convention
222 lua_newtable(fL);
223 this->pushScalar(array[i].fX, "x");
224 this->pushScalar(array[i].fY, "y");
225 lua_rawseti(fL, -2, i + 1);
226 }
227 CHECK_SETFIELD(key);
228}
229
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000230void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
231 lua_newtable(fL);
232 for (int i = 0; i < count; ++i) {
233 // make it base-1 to match lua convention
234 setarray_scalar(fL, i + 1, array[i]);
235 }
236 CHECK_SETFIELD(key);
237}
238
reed@google.com74ce6f02013-05-22 15:13:18 +0000239void SkLua::pushRect(const SkRect& r, const char key[]) {
240 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000241 setfield_scalar(fL, "left", r.fLeft);
242 setfield_scalar(fL, "top", r.fTop);
243 setfield_scalar(fL, "right", r.fRight);
244 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000245 CHECK_SETFIELD(key);
246}
247
248void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
249 push_obj(fL, rr);
250 CHECK_SETFIELD(key);
251}
252
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000253void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
254 lua_newtable(fL);
255 setfield_scalar(fL, "phase", info.fPhase);
256 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
257 CHECK_SETFIELD(key);
258}
259
260
reed@google.com74ce6f02013-05-22 15:13:18 +0000261void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
262 push_obj(fL, matrix);
263 CHECK_SETFIELD(key);
264}
265
266void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
267 push_obj(fL, paint);
268 CHECK_SETFIELD(key);
269}
270
271void SkLua::pushPath(const SkPath& path, const char key[]) {
272 push_obj(fL, path);
273 CHECK_SETFIELD(key);
274}
275
276void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
277 push_ref(fL, canvas);
278 CHECK_SETFIELD(key);
279}
280
fmalitab7425172014-08-26 07:56:44 -0700281void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
282 push_ref(fL, const_cast<SkTextBlob*>(blob));
283 CHECK_SETFIELD(key);
284}
285
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000286static const char* element_type(SkClipStack::Element::Type type) {
287 switch (type) {
288 case SkClipStack::Element::kEmpty_Type:
289 return "empty";
290 case SkClipStack::Element::kRect_Type:
291 return "rect";
292 case SkClipStack::Element::kRRect_Type:
293 return "rrect";
294 case SkClipStack::Element::kPath_Type:
295 return "path";
296 }
297 return "unknown";
298}
299
300static const char* region_op(SkRegion::Op op) {
301 switch (op) {
302 case SkRegion::kDifference_Op:
303 return "difference";
304 case SkRegion::kIntersect_Op:
305 return "intersect";
306 case SkRegion::kUnion_Op:
307 return "union";
308 case SkRegion::kXOR_Op:
309 return "xor";
310 case SkRegion::kReverseDifference_Op:
311 return "reverse-difference";
312 case SkRegion::kReplace_Op:
313 return "replace";
314 }
315 return "unknown";
316}
317
318void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
319 lua_newtable(fL);
320 SkClipStack::B2TIter iter(stack);
321 const SkClipStack::Element* element;
322 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700323 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000324 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000325 lua_rawseti(fL, -2, ++i);
326 }
327 CHECK_SETFIELD(key);
328}
329
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000330void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
331 lua_newtable(fL);
332 SkClipStack::Element::Type type = element.getType();
333 this->pushString(element_type(type), "type");
334 switch (type) {
335 case SkClipStack::Element::kEmpty_Type:
336 break;
337 case SkClipStack::Element::kRect_Type:
338 this->pushRect(element.getRect(), "rect");
339 break;
340 case SkClipStack::Element::kRRect_Type:
341 this->pushRRect(element.getRRect(), "rrect");
342 break;
343 case SkClipStack::Element::kPath_Type:
344 this->pushPath(element.getPath(), "path");
345 break;
346 }
347 this->pushString(region_op(element.getOp()), "op");
348 this->pushBool(element.isAA(), "aa");
349 CHECK_SETFIELD(key);
350}
351
352
reed@google.com74ce6f02013-05-22 15:13:18 +0000353///////////////////////////////////////////////////////////////////////////////
354///////////////////////////////////////////////////////////////////////////////
355
reed485557f2014-10-12 10:36:47 -0700356static int lua2int_def(lua_State* L, int index, int defaultValue) {
357 if (lua_isnumber(L, index)) {
358 return (int)lua_tonumber(L, index);
359 } else {
360 return defaultValue;
361 }
362}
363
reed@google.com74ce6f02013-05-22 15:13:18 +0000364static SkScalar lua2scalar(lua_State* L, int index) {
365 SkASSERT(lua_isnumber(L, index));
366 return SkLuaToScalar(lua_tonumber(L, index));
367}
368
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000369static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
370 if (lua_isnumber(L, index)) {
371 return SkLuaToScalar(lua_tonumber(L, index));
372 } else {
373 return defaultValue;
374 }
375}
376
reed@google.com74ce6f02013-05-22 15:13:18 +0000377static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
378 SkASSERT(lua_istable(L, index));
379 lua_pushstring(L, key);
380 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000381
reed@google.com74ce6f02013-05-22 15:13:18 +0000382 SkScalar value = lua2scalar(L, -1);
383 lua_pop(L, 1);
384 return value;
385}
386
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000387static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
388 SkASSERT(lua_istable(L, index));
389 lua_pushstring(L, key);
390 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000391
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000392 SkScalar value;
393 if (lua_isnil(L, -1)) {
394 value = def;
395 } else {
396 value = lua2scalar(L, -1);
397 }
398 lua_pop(L, 1);
399 return value;
400}
401
reed@google.com74ce6f02013-05-22 15:13:18 +0000402static U8CPU unit2byte(SkScalar x) {
403 if (x <= 0) {
404 return 0;
405 } else if (x >= 1) {
406 return 255;
407 } else {
408 return SkScalarRoundToInt(x * 255);
409 }
410}
411
412static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700413 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
414 unit2byte(getfield_scalar_default(L, index, "r", 0)),
415 unit2byte(getfield_scalar_default(L, index, "g", 0)),
416 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000417}
418
419static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000420 rect->set(getfield_scalar_default(L, index, "left", 0),
421 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000422 getfield_scalar(L, index, "right"),
423 getfield_scalar(L, index, "bottom"));
424 return rect;
425}
426
reedf355df52014-10-12 12:18:40 -0700427static int lcanvas_clear(lua_State* L) {
428 get_ref<SkCanvas>(L, 1)->clear(0);
429 return 0;
430}
431
reed@google.com74ce6f02013-05-22 15:13:18 +0000432static int lcanvas_drawColor(lua_State* L) {
433 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
434 return 0;
435}
436
437static int lcanvas_drawRect(lua_State* L) {
438 SkRect rect;
439 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
440 *get_obj<SkPaint>(L, 3));
441 return 0;
442}
443
444static int lcanvas_drawOval(lua_State* L) {
445 SkRect rect;
446 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
447 *get_obj<SkPaint>(L, 3));
448 return 0;
449}
450
451static int lcanvas_drawCircle(lua_State* L) {
452 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
453 lua2scalar(L, 3),
454 lua2scalar(L, 4),
455 *get_obj<SkPaint>(L, 5));
456 return 0;
457}
458
reed485557f2014-10-12 10:36:47 -0700459static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
460 if (lua_isnumber(L, index)) {
461 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
462 return paint;
reedf355df52014-10-12 12:18:40 -0700463 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700464 const SkPaint* ptr = get_obj<SkPaint>(L, index);
465 if (ptr) {
466 *paint = *ptr;
467 return paint;
468 }
469 }
470 return NULL;
471}
472
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000473static int lcanvas_drawImage(lua_State* L) {
474 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
475 SkImage* image = get_ref<SkImage>(L, 2);
476 if (NULL == image) {
477 return 0;
478 }
479 SkScalar x = lua2scalar(L, 3);
480 SkScalar y = lua2scalar(L, 4);
481
482 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700483 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000484 return 0;
485}
486
reedba5fb932014-10-10 15:28:19 -0700487static int lcanvas_drawImageRect(lua_State* L) {
488 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
489 SkImage* image = get_ref<SkImage>(L, 2);
490 if (NULL == image) {
491 return 0;
492 }
493
494 SkRect srcR, dstR;
495 SkRect* srcRPtr = NULL;
496 if (!lua_isnil(L, 3)) {
497 srcRPtr = lua2rect(L, 3, &srcR);
498 }
499 lua2rect(L, 4, &dstR);
500
501 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700502 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700503 return 0;
504}
505
reed@google.comfd345872013-05-22 20:53:42 +0000506static int lcanvas_drawPath(lua_State* L) {
507 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
508 *get_obj<SkPaint>(L, 3));
509 return 0;
510}
511
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000512static int lcanvas_drawText(lua_State* L) {
513 if (lua_gettop(L) < 5) {
514 return 0;
515 }
516
517 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
518 size_t len;
519 const char* text = lua_tolstring(L, 2, &len);
520 get_ref<SkCanvas>(L, 1)->drawText(text, len,
521 lua2scalar(L, 3), lua2scalar(L, 4),
522 *get_obj<SkPaint>(L, 5));
523 }
524 return 0;
525}
526
reed@google.com74ce6f02013-05-22 15:13:18 +0000527static int lcanvas_getSaveCount(lua_State* L) {
528 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
529 return 1;
530}
531
532static int lcanvas_getTotalMatrix(lua_State* L) {
533 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
534 return 1;
535}
536
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000537static int lcanvas_getClipStack(lua_State* L) {
538 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
539 return 1;
540}
541
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000542int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
543#if SK_SUPPORT_GPU
544 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
545 SkISize layerSize = canvas->getTopLayerSize();
546 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
547 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
548 layerSize.fWidth, layerSize.fHeight);
549
550 GrReducedClip::ElementList elements;
551 GrReducedClip::InitialState initialState;
552 int32_t genID;
553 SkIRect resultBounds;
554
555 const SkClipStack& stack = *canvas->getClipStack();
556
557 GrReducedClip::ReduceClipStack(stack,
558 queryBounds,
559 &elements,
560 &genID,
561 &initialState,
562 &resultBounds,
563 NULL);
564
565 GrReducedClip::ElementList::Iter iter(elements);
566 int i = 0;
567 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700568 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000569 SkLua(L).pushClipStackElement(*iter.get());
570 iter.next();
571 lua_rawseti(L, -2, ++i);
572 }
573 // Currently this only returns the element list to lua, not the initial state or result bounds.
574 // It could return these as additional items on the lua stack.
575 return 1;
576#else
577 return 0;
578#endif
579}
580
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000581static int lcanvas_save(lua_State* L) {
582 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
583 return 1;
584}
585
586static int lcanvas_restore(lua_State* L) {
587 get_ref<SkCanvas>(L, 1)->restore();
588 return 0;
589}
590
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000591static int lcanvas_scale(lua_State* L) {
592 SkScalar sx = lua2scalar_def(L, 2, 1);
593 SkScalar sy = lua2scalar_def(L, 3, sx);
594 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
595 return 0;
596}
597
reed@google.com3597b732013-05-22 20:12:50 +0000598static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000599 SkScalar tx = lua2scalar_def(L, 2, 0);
600 SkScalar ty = lua2scalar_def(L, 3, 0);
601 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
602 return 0;
603}
604
605static int lcanvas_rotate(lua_State* L) {
606 SkScalar degrees = lua2scalar_def(L, 2, 0);
607 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000608 return 0;
609}
610
reed485557f2014-10-12 10:36:47 -0700611static int lcanvas_newSurface(lua_State* L) {
612 int width = lua2int_def(L, 2, 0);
613 int height = lua2int_def(L, 2, 0);
614 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
615 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
616 if (NULL == surface) {
617 lua_pushnil(L);
618 } else {
619 push_ref(L, surface);
620 surface->unref();
621 }
622 return 1;
623}
624
reed@google.com74ce6f02013-05-22 15:13:18 +0000625static int lcanvas_gc(lua_State* L) {
626 get_ref<SkCanvas>(L, 1)->unref();
627 return 0;
628}
629
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000630const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700631 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000632 { "drawColor", lcanvas_drawColor },
633 { "drawRect", lcanvas_drawRect },
634 { "drawOval", lcanvas_drawOval },
635 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000636 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700637 { "drawImageRect", lcanvas_drawImageRect },
reed@google.comfd345872013-05-22 20:53:42 +0000638 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000639 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000640 { "getSaveCount", lcanvas_getSaveCount },
641 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000642 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000643#if SK_SUPPORT_GPU
644 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
645#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000646 { "save", lcanvas_save },
647 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000648 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000649 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000650 { "rotate", lcanvas_rotate },
reed485557f2014-10-12 10:36:47 -0700651
652 { "newSurface", lcanvas_newSurface },
653
reed@google.com74ce6f02013-05-22 15:13:18 +0000654 { "__gc", lcanvas_gc },
655 { NULL, NULL }
656};
657
658///////////////////////////////////////////////////////////////////////////////
659
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000660static int ldocument_beginPage(lua_State* L) {
661 const SkRect* contentPtr = NULL;
662 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
663 lua2scalar(L, 3),
664 contentPtr));
665 return 1;
666}
667
668static int ldocument_endPage(lua_State* L) {
669 get_ref<SkDocument>(L, 1)->endPage();
670 return 0;
671}
672
673static int ldocument_close(lua_State* L) {
674 get_ref<SkDocument>(L, 1)->close();
675 return 0;
676}
677
678static int ldocument_gc(lua_State* L) {
679 get_ref<SkDocument>(L, 1)->unref();
680 return 0;
681}
682
683static const struct luaL_Reg gSkDocument_Methods[] = {
684 { "beginPage", ldocument_beginPage },
685 { "endPage", ldocument_endPage },
686 { "close", ldocument_close },
687 { "__gc", ldocument_gc },
688 { NULL, NULL }
689};
690
691///////////////////////////////////////////////////////////////////////////////
692
reed@google.com74ce6f02013-05-22 15:13:18 +0000693static int lpaint_isAntiAlias(lua_State* L) {
694 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
695 return 1;
696}
697
698static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000699 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000700 return 0;
701}
702
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000703static int lpaint_isDither(lua_State* L) {
704 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
705 return 1;
706}
707
708static int lpaint_isUnderlineText(lua_State* L) {
709 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
710 return 1;
711}
712
713static int lpaint_isStrikeThruText(lua_State* L) {
714 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
715 return 1;
716}
717
718static int lpaint_isFakeBoldText(lua_State* L) {
719 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
720 return 1;
721}
722
723static int lpaint_isLinearText(lua_State* L) {
724 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
725 return 1;
726}
727
728static int lpaint_isSubpixelText(lua_State* L) {
729 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
730 return 1;
731}
732
reed09a1d672014-10-11 13:13:11 -0700733static int lpaint_setSubpixelText(lua_State* L) {
734 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
735 return 1;
736}
737
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000738static int lpaint_isDevKernText(lua_State* L) {
739 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
740 return 1;
741}
742
743static int lpaint_isLCDRenderText(lua_State* L) {
744 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
745 return 1;
746}
747
748static int lpaint_isEmbeddedBitmapText(lua_State* L) {
749 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
750 return 1;
751}
752
753static int lpaint_isAutohinted(lua_State* L) {
754 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
755 return 1;
756}
757
758static int lpaint_isVerticalText(lua_State* L) {
759 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
760 return 1;
761}
762
reed@google.com74ce6f02013-05-22 15:13:18 +0000763static int lpaint_getColor(lua_State* L) {
764 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
765 return 1;
766}
767
768static int lpaint_setColor(lua_State* L) {
769 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
770 return 0;
771}
772
reed@google.come3823fd2013-05-30 18:55:14 +0000773static int lpaint_getTextSize(lua_State* L) {
774 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
775 return 1;
776}
777
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000778static int lpaint_getTextScaleX(lua_State* L) {
779 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
780 return 1;
781}
782
783static int lpaint_getTextSkewX(lua_State* L) {
784 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
785 return 1;
786}
787
reed@google.come3823fd2013-05-30 18:55:14 +0000788static int lpaint_setTextSize(lua_State* L) {
789 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
790 return 0;
791}
792
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000793static int lpaint_getTypeface(lua_State* L) {
794 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
795 return 1;
796}
797
798static int lpaint_setTypeface(lua_State* L) {
799 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
800 return 0;
801}
802
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000803static int lpaint_getHinting(lua_State* L) {
804 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
805 return 1;
806}
807
reed@google.come3823fd2013-05-30 18:55:14 +0000808static int lpaint_getFontID(lua_State* L) {
809 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
810 SkLua(L).pushU32(SkTypeface::UniqueID(face));
811 return 1;
812}
813
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000814static const struct {
815 const char* fLabel;
816 SkPaint::Align fAlign;
817} gAlignRec[] = {
818 { "left", SkPaint::kLeft_Align },
819 { "center", SkPaint::kCenter_Align },
820 { "right", SkPaint::kRight_Align },
821};
822
823static int lpaint_getTextAlign(lua_State* L) {
824 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
825 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
826 if (gAlignRec[i].fAlign == align) {
827 lua_pushstring(L, gAlignRec[i].fLabel);
828 return 1;
829 }
830 }
831 return 0;
832}
833
834static int lpaint_setTextAlign(lua_State* L) {
835 if (lua_isstring(L, 2)) {
836 size_t len;
837 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000838
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000839 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
840 if (!strcmp(gAlignRec[i].fLabel, label)) {
841 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
842 break;
843 }
844 }
845 }
846 return 0;
847}
848
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000849static int lpaint_getStroke(lua_State* L) {
850 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
851 return 1;
852}
853
854static int lpaint_setStroke(lua_State* L) {
855 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000856
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000857 if (lua_toboolean(L, 2)) {
858 style = SkPaint::kStroke_Style;
859 } else {
860 style = SkPaint::kFill_Style;
861 }
862 get_obj<SkPaint>(L, 1)->setStyle(style);
863 return 0;
864}
865
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000866static int lpaint_getStrokeCap(lua_State* L) {
867 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
868 return 1;
869}
870
871static int lpaint_getStrokeJoin(lua_State* L) {
872 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
873 return 1;
874}
875
876static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000877 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000878 return 1;
879}
880
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000881static int lpaint_getStrokeWidth(lua_State* L) {
882 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
883 return 1;
884}
885
886static int lpaint_setStrokeWidth(lua_State* L) {
887 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
888 return 0;
889}
890
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000891static int lpaint_getStrokeMiter(lua_State* L) {
892 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
893 return 1;
894}
895
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000896static int lpaint_measureText(lua_State* L) {
897 if (lua_isstring(L, 2)) {
898 size_t len;
899 const char* text = lua_tolstring(L, 2, &len);
900 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
901 return 1;
902 }
903 return 0;
904}
905
906struct FontMetrics {
907 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
908 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
909 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
910 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
911 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
912 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
913 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
914 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
915 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
916};
917
918static int lpaint_getFontMetrics(lua_State* L) {
919 SkPaint::FontMetrics fm;
920 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000921
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000922 lua_newtable(L);
923 setfield_scalar(L, "top", fm.fTop);
924 setfield_scalar(L, "ascent", fm.fAscent);
925 setfield_scalar(L, "descent", fm.fDescent);
926 setfield_scalar(L, "bottom", fm.fBottom);
927 setfield_scalar(L, "leading", fm.fLeading);
928 SkLua(L).pushScalar(height);
929 return 2;
930}
931
reed@google.com29563872013-07-10 21:23:49 +0000932static int lpaint_getEffects(lua_State* L) {
933 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000934
reed@google.com29563872013-07-10 21:23:49 +0000935 lua_newtable(L);
936 setfield_bool_if(L, "looper", !!paint->getLooper());
937 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
938 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
939 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
940 setfield_bool_if(L, "shader", !!paint->getShader());
941 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
942 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
943 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
944 return 1;
945}
946
reed@google.com5fdc9832013-07-24 15:47:52 +0000947static int lpaint_getShader(lua_State* L) {
948 const SkPaint* paint = get_obj<SkPaint>(L, 1);
949 SkShader* shader = paint->getShader();
950 if (shader) {
951 push_ref(L, shader);
952 return 1;
953 }
954 return 0;
955}
956
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000957static int lpaint_getPathEffect(lua_State* L) {
958 const SkPaint* paint = get_obj<SkPaint>(L, 1);
959 SkPathEffect* pe = paint->getPathEffect();
960 if (pe) {
961 push_ref(L, pe);
962 return 1;
963 }
964 return 0;
965}
966
reed@google.com74ce6f02013-05-22 15:13:18 +0000967static int lpaint_gc(lua_State* L) {
968 get_obj<SkPaint>(L, 1)->~SkPaint();
969 return 0;
970}
971
972static const struct luaL_Reg gSkPaint_Methods[] = {
973 { "isAntiAlias", lpaint_isAntiAlias },
974 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000975 { "isDither", lpaint_isDither },
976 { "isUnderlineText", lpaint_isUnderlineText },
977 { "isStrikeThruText", lpaint_isStrikeThruText },
978 { "isFakeBoldText", lpaint_isFakeBoldText },
979 { "isLinearText", lpaint_isLinearText },
980 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -0700981 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000982 { "isDevKernText", lpaint_isDevKernText },
983 { "isLCDRenderText", lpaint_isLCDRenderText },
984 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
985 { "isAutohinted", lpaint_isAutohinted },
986 { "isVerticalText", lpaint_isVerticalText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000987 { "getColor", lpaint_getColor },
988 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000989 { "getTextSize", lpaint_getTextSize },
990 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000991 { "getTextScaleX", lpaint_getTextScaleX },
992 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000993 { "getTypeface", lpaint_getTypeface },
994 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000995 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +0000996 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000997 { "getTextAlign", lpaint_getTextAlign },
998 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000999 { "getStroke", lpaint_getStroke },
1000 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001001 { "getStrokeCap", lpaint_getStrokeCap },
1002 { "getStrokeJoin", lpaint_getStrokeJoin },
1003 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001004 { "getStrokeWidth", lpaint_getStrokeWidth },
1005 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001006 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001007 { "measureText", lpaint_measureText },
1008 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001009 { "getEffects", lpaint_getEffects },
reed@google.com5fdc9832013-07-24 15:47:52 +00001010 { "getShader", lpaint_getShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001011 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001012 { "__gc", lpaint_gc },
1013 { NULL, NULL }
1014};
1015
1016///////////////////////////////////////////////////////////////////////////////
1017
reed@google.com5fdc9832013-07-24 15:47:52 +00001018static const char* mode2string(SkShader::TileMode mode) {
1019 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1020 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1021 return gNames[mode];
1022}
1023
1024static const char* gradtype2string(SkShader::GradientType t) {
1025 static const char* gNames[] = {
1026 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1027 };
1028 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1029 return gNames[t];
1030}
1031
1032static int lshader_isOpaque(lua_State* L) {
1033 SkShader* shader = get_ref<SkShader>(L, 1);
1034 return shader && shader->isOpaque();
1035}
1036
1037static int lshader_asABitmap(lua_State* L) {
1038 SkShader* shader = get_ref<SkShader>(L, 1);
1039 if (shader) {
1040 SkBitmap bm;
1041 SkMatrix matrix;
1042 SkShader::TileMode modes[2];
1043 switch (shader->asABitmap(&bm, &matrix, modes)) {
1044 case SkShader::kDefault_BitmapType:
1045 lua_newtable(L);
1046 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1047 setfield_number(L, "width", bm.width());
1048 setfield_number(L, "height", bm.height());
1049 setfield_string(L, "tileX", mode2string(modes[0]));
1050 setfield_string(L, "tileY", mode2string(modes[1]));
1051 return 1;
1052 default:
1053 break;
1054 }
1055 }
1056 return 0;
1057}
1058
1059static int lshader_asAGradient(lua_State* L) {
1060 SkShader* shader = get_ref<SkShader>(L, 1);
1061 if (shader) {
1062 SkShader::GradientInfo info;
1063 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001064
1065 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001066 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001067
1068 info.fColorCount = 3;
1069 info.fColors = &colors[0];
1070 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001071
reed@google.com5fdc9832013-07-24 15:47:52 +00001072 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001073
reed@google.com5fdc9832013-07-24 15:47:52 +00001074 if (SkShader::kNone_GradientType != t) {
1075 lua_newtable(L);
1076 setfield_string(L, "type", gradtype2string(t));
1077 setfield_number(L, "colorCount", info.fColorCount);
1078 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001079
1080 if (info.fColorCount == 3){
1081 setfield_number(L, "midPos", pos[1]);
1082 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001083
reed@google.com5fdc9832013-07-24 15:47:52 +00001084 return 1;
1085 }
1086 }
1087 return 0;
1088}
1089
1090static int lshader_gc(lua_State* L) {
1091 get_ref<SkShader>(L, 1)->unref();
1092 return 0;
1093}
1094
1095static const struct luaL_Reg gSkShader_Methods[] = {
1096 { "isOpaque", lshader_isOpaque },
1097 { "asABitmap", lshader_asABitmap },
1098 { "asAGradient", lshader_asAGradient },
1099 { "__gc", lshader_gc },
1100 { NULL, NULL }
1101};
1102
1103///////////////////////////////////////////////////////////////////////////////
1104
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001105static int lpatheffect_asADash(lua_State* L) {
1106 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1107 if (pe) {
1108 SkPathEffect::DashInfo info;
1109 SkPathEffect::DashType dashType = pe->asADash(&info);
1110 if (SkPathEffect::kDash_DashType == dashType) {
1111 SkAutoTArray<SkScalar> intervals(info.fCount);
1112 info.fIntervals = intervals.get();
1113 pe->asADash(&info);
1114 SkLua(L).pushDash(info);
1115 return 1;
1116 }
1117 }
1118 return 0;
1119}
1120
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001121static int lpatheffect_gc(lua_State* L) {
1122 get_ref<SkPathEffect>(L, 1)->unref();
1123 return 0;
1124}
1125
1126static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001127 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001128 { "__gc", lpatheffect_gc },
1129 { NULL, NULL }
1130};
1131
1132///////////////////////////////////////////////////////////////////////////////
1133
humper@google.com2815c192013-07-10 22:42:30 +00001134static int lmatrix_getType(lua_State* L) {
1135 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001136
humper@google.com2815c192013-07-10 22:42:30 +00001137 lua_newtable(L);
1138 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1139 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1140 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1141 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1142 return 1;
1143}
1144
humper@google.com0f48ee02013-07-26 15:23:43 +00001145static int lmatrix_getScaleX(lua_State* L) {
1146 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1147 return 1;
1148}
1149
1150static int lmatrix_getScaleY(lua_State* L) {
1151 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1152 return 1;
1153}
1154
1155static int lmatrix_getTranslateX(lua_State* L) {
1156 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1157 return 1;
1158}
1159
1160static int lmatrix_getTranslateY(lua_State* L) {
1161 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1162 return 1;
1163}
1164
humper@google.com2815c192013-07-10 22:42:30 +00001165static const struct luaL_Reg gSkMatrix_Methods[] = {
1166 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001167 { "getScaleX", lmatrix_getScaleX },
1168 { "getScaleY", lmatrix_getScaleY },
1169 { "getTranslateX", lmatrix_getTranslateX },
1170 { "getTranslateY", lmatrix_getTranslateY },
humper@google.com2815c192013-07-10 22:42:30 +00001171 { NULL, NULL }
1172};
1173
1174///////////////////////////////////////////////////////////////////////////////
1175
reed@google.com74ce6f02013-05-22 15:13:18 +00001176static int lpath_getBounds(lua_State* L) {
1177 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1178 return 1;
1179}
1180
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001181static const char* fill_type_to_str(SkPath::FillType fill) {
1182 switch (fill) {
1183 case SkPath::kEvenOdd_FillType:
1184 return "even-odd";
1185 case SkPath::kWinding_FillType:
1186 return "winding";
1187 case SkPath::kInverseEvenOdd_FillType:
1188 return "inverse-even-odd";
1189 case SkPath::kInverseWinding_FillType:
1190 return "inverse-winding";
1191 }
1192 return "unknown";
1193}
1194
1195static int lpath_getFillType(lua_State* L) {
1196 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1197 SkLua(L).pushString(fill_type_to_str(fill));
1198 return 1;
1199}
1200
1201static SkString segment_masks_to_str(uint32_t segmentMasks) {
1202 SkString result;
1203 bool first = true;
1204 if (SkPath::kLine_SegmentMask & segmentMasks) {
1205 result.append("line");
1206 first = false;
1207 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1208 }
1209 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1210 if (!first) {
1211 result.append(" ");
1212 }
1213 result.append("quad");
1214 first = false;
1215 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1216 }
1217 if (SkPath::kConic_SegmentMask & segmentMasks) {
1218 if (!first) {
1219 result.append(" ");
1220 }
1221 result.append("conic");
1222 first = false;
1223 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1224 }
1225 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1226 if (!first) {
1227 result.append(" ");
1228 }
1229 result.append("cubic");
1230 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1231 }
1232 SkASSERT(0 == segmentMasks);
1233 return result;
1234}
1235
krajcevski95498ed2014-08-18 08:02:33 -07001236static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001237 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1238 SkLua(L).pushString(segment_masks_to_str(segMasks));
1239 return 1;
1240}
1241
1242static int lpath_isConvex(lua_State* L) {
1243 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1244 SkLua(L).pushBool(isConvex);
1245 return 1;
1246}
1247
reed@google.com74ce6f02013-05-22 15:13:18 +00001248static int lpath_isEmpty(lua_State* L) {
1249 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1250 return 1;
1251}
1252
1253static int lpath_isRect(lua_State* L) {
1254 SkRect r;
1255 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1256 int ret_count = 1;
1257 lua_pushboolean(L, pred);
1258 if (pred) {
1259 SkLua(L).pushRect(r);
1260 ret_count += 1;
1261 }
1262 return ret_count;
1263}
1264
1265static const char* dir2string(SkPath::Direction dir) {
1266 static const char* gStr[] = {
1267 "unknown", "cw", "ccw"
1268 };
1269 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1270 return gStr[dir];
1271}
1272
1273static int lpath_isNestedRects(lua_State* L) {
1274 SkRect rects[2];
1275 SkPath::Direction dirs[2];
1276 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1277 int ret_count = 1;
1278 lua_pushboolean(L, pred);
1279 if (pred) {
1280 SkLua lua(L);
1281 lua.pushRect(rects[0]);
1282 lua.pushRect(rects[1]);
1283 lua_pushstring(L, dir2string(dirs[0]));
1284 lua_pushstring(L, dir2string(dirs[0]));
1285 ret_count += 4;
1286 }
1287 return ret_count;
1288}
1289
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001290static int lpath_countPoints(lua_State* L) {
1291 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1292 return 1;
1293}
1294
reed@google.com74ce6f02013-05-22 15:13:18 +00001295static int lpath_reset(lua_State* L) {
1296 get_obj<SkPath>(L, 1)->reset();
1297 return 0;
1298}
1299
1300static int lpath_moveTo(lua_State* L) {
1301 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1302 return 0;
1303}
1304
1305static int lpath_lineTo(lua_State* L) {
1306 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1307 return 0;
1308}
1309
1310static int lpath_quadTo(lua_State* L) {
1311 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1312 lua2scalar(L, 4), lua2scalar(L, 5));
1313 return 0;
1314}
1315
1316static int lpath_cubicTo(lua_State* L) {
1317 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1318 lua2scalar(L, 4), lua2scalar(L, 5),
1319 lua2scalar(L, 6), lua2scalar(L, 7));
1320 return 0;
1321}
1322
1323static int lpath_close(lua_State* L) {
1324 get_obj<SkPath>(L, 1)->close();
1325 return 0;
1326}
1327
1328static int lpath_gc(lua_State* L) {
1329 get_obj<SkPath>(L, 1)->~SkPath();
1330 return 0;
1331}
1332
1333static const struct luaL_Reg gSkPath_Methods[] = {
1334 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001335 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001336 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001337 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001338 { "isEmpty", lpath_isEmpty },
1339 { "isRect", lpath_isRect },
1340 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001341 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001342 { "reset", lpath_reset },
1343 { "moveTo", lpath_moveTo },
1344 { "lineTo", lpath_lineTo },
1345 { "quadTo", lpath_quadTo },
1346 { "cubicTo", lpath_cubicTo },
1347 { "close", lpath_close },
1348 { "__gc", lpath_gc },
1349 { NULL, NULL }
1350};
1351
1352///////////////////////////////////////////////////////////////////////////////
1353
1354static const char* rrect_type(const SkRRect& rr) {
1355 switch (rr.getType()) {
1356 case SkRRect::kUnknown_Type: return "unknown";
1357 case SkRRect::kEmpty_Type: return "empty";
1358 case SkRRect::kRect_Type: return "rect";
1359 case SkRRect::kOval_Type: return "oval";
1360 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001361 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001362 case SkRRect::kComplex_Type: return "complex";
1363 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001364 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001365 return "";
1366}
1367
1368static int lrrect_rect(lua_State* L) {
1369 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1370 return 1;
1371}
1372
1373static int lrrect_type(lua_State* L) {
1374 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1375 return 1;
1376}
1377
1378static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001379 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001380 SkVector v;
1381 if (corner < 0 || corner > 3) {
1382 SkDebugf("bad corner index %d", corner);
1383 v.set(0, 0);
1384 } else {
1385 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1386 }
1387 lua_pushnumber(L, v.fX);
1388 lua_pushnumber(L, v.fY);
1389 return 2;
1390}
1391
1392static int lrrect_gc(lua_State* L) {
1393 get_obj<SkRRect>(L, 1)->~SkRRect();
1394 return 0;
1395}
1396
1397static const struct luaL_Reg gSkRRect_Methods[] = {
1398 { "rect", lrrect_rect },
1399 { "type", lrrect_type },
1400 { "radii", lrrect_radii },
1401 { "__gc", lrrect_gc },
1402 { NULL, NULL }
1403};
1404
1405///////////////////////////////////////////////////////////////////////////////
1406
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001407static int limage_width(lua_State* L) {
1408 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1409 return 1;
1410}
1411
1412static int limage_height(lua_State* L) {
1413 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1414 return 1;
1415}
1416
1417static int limage_gc(lua_State* L) {
1418 get_ref<SkImage>(L, 1)->unref();
1419 return 0;
1420}
1421
1422static const struct luaL_Reg gSkImage_Methods[] = {
1423 { "width", limage_width },
1424 { "height", limage_height },
1425 { "__gc", limage_gc },
1426 { NULL, NULL }
1427};
1428
1429///////////////////////////////////////////////////////////////////////////////
1430
reed485557f2014-10-12 10:36:47 -07001431static int lsurface_width(lua_State* L) {
1432 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1433 return 1;
1434}
1435
1436static int lsurface_height(lua_State* L) {
1437 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1438 return 1;
1439}
1440
1441static int lsurface_getCanvas(lua_State* L) {
1442 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1443 if (NULL == canvas) {
1444 lua_pushnil(L);
1445 } else {
1446 push_ref(L, canvas);
1447 // note: we don't unref canvas, since getCanvas did not ref it.
1448 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1449 // the real owner (the surface) go away, but still hold onto the canvas?
1450 // *really* we want to sort of ref the surface again, but have the native object
1451 // know that it is supposed to be treated as a canvas...
1452 }
1453 return 1;
1454}
1455
1456static int lsurface_newImageSnapshot(lua_State* L) {
1457 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1458 if (NULL == image) {
1459 lua_pushnil(L);
1460 } else {
1461 push_ref(L, image);
1462 image->unref();
1463 }
1464 return 1;
1465}
1466
1467static int lsurface_newSurface(lua_State* L) {
1468 int width = lua2int_def(L, 2, 0);
1469 int height = lua2int_def(L, 2, 0);
1470 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1471 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1472 if (NULL == surface) {
1473 lua_pushnil(L);
1474 } else {
1475 push_ref(L, surface);
1476 surface->unref();
1477 }
1478 return 1;
1479}
1480
1481static int lsurface_gc(lua_State* L) {
1482 get_ref<SkSurface>(L, 1)->unref();
1483 return 0;
1484}
1485
1486static const struct luaL_Reg gSkSurface_Methods[] = {
1487 { "width", lsurface_width },
1488 { "height", lsurface_height },
1489 { "getCanvas", lsurface_getCanvas },
1490 { "newImageSnapshot", lsurface_newImageSnapshot },
1491 { "newSurface", lsurface_newSurface },
1492 { "__gc", lsurface_gc },
1493 { NULL, NULL }
1494};
1495
1496///////////////////////////////////////////////////////////////////////////////
1497
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001498static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001499 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001500 return 0;
1501}
1502
1503static const struct luaL_Reg gSkTypeface_Methods[] = {
1504 { "__gc", ltypeface_gc },
1505 { NULL, NULL }
1506};
1507
1508///////////////////////////////////////////////////////////////////////////////
1509
reed@google.com74ce6f02013-05-22 15:13:18 +00001510class AutoCallLua {
1511public:
1512 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1513 lua_getglobal(L, func);
1514 if (!lua_isfunction(L, -1)) {
1515 int t = lua_type(L, -1);
1516 SkDebugf("--- expected function %d\n", t);
1517 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001518
reed@google.com74ce6f02013-05-22 15:13:18 +00001519 lua_newtable(L);
1520 setfield_string(L, "verb", verb);
1521 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001522
reed@google.com74ce6f02013-05-22 15:13:18 +00001523 ~AutoCallLua() {
1524 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1525 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1526 }
1527 lua_settop(fL, -1);
1528 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001529
reed@google.com74ce6f02013-05-22 15:13:18 +00001530private:
1531 lua_State* fL;
1532};
1533
1534#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1535
1536///////////////////////////////////////////////////////////////////////////////
1537
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001538static int lsk_newDocumentPDF(lua_State* L) {
1539 const char* file = NULL;
1540 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1541 file = lua_tolstring(L, 1, NULL);
1542 }
1543
1544 SkDocument* doc = SkDocument::CreatePDF(file);
1545 if (NULL == doc) {
1546 // do I need to push a nil on the stack and return 1?
1547 return 0;
1548 } else {
1549 push_ref(L, doc);
1550 doc->unref();
1551 return 1;
1552 }
1553}
1554
reed@google.com3597b732013-05-22 20:12:50 +00001555static int lsk_newPaint(lua_State* L) {
1556 push_new<SkPaint>(L);
1557 return 1;
1558}
1559
1560static int lsk_newPath(lua_State* L) {
1561 push_new<SkPath>(L);
1562 return 1;
1563}
1564
1565static int lsk_newRRect(lua_State* L) {
1566 SkRRect* rr = push_new<SkRRect>(L);
1567 rr->setEmpty();
1568 return 1;
1569}
1570
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001571static int lsk_newTypeface(lua_State* L) {
1572 const char* name = NULL;
1573 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001574
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001575 int count = lua_gettop(L);
1576 if (count > 0 && lua_isstring(L, 1)) {
1577 name = lua_tolstring(L, 1, NULL);
1578 if (count > 1 && lua_isnumber(L, 2)) {
1579 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1580 }
1581 }
1582
1583 SkTypeface* face = SkTypeface::CreateFromName(name,
1584 (SkTypeface::Style)style);
1585// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1586 if (NULL == face) {
1587 face = SkTypeface::RefDefault();
1588 }
1589 push_ref(L, face);
1590 face->unref();
1591 return 1;
1592}
reed@google.com3597b732013-05-22 20:12:50 +00001593
reed485557f2014-10-12 10:36:47 -07001594static int lsk_newRasterSurface(lua_State* L) {
1595 int width = lua2int_def(L, 2, 0);
1596 int height = lua2int_def(L, 2, 0);
1597 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1598 SkSurface* surface = SkSurface::NewRaster(info);
1599 if (NULL == surface) {
1600 lua_pushnil(L);
1601 } else {
1602 push_ref(L, surface);
1603 surface->unref();
1604 }
1605 return 1;
1606}
1607
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001608static int lsk_loadImage(lua_State* L) {
1609 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1610 const char* name = lua_tolstring(L, 1, NULL);
1611 SkAutoDataUnref data(SkData::NewFromFileName(name));
1612 if (data.get()) {
piotaixr4bcc2022014-09-17 14:33:30 -07001613 SkImage* image = SkImage::NewFromGenerator(
1614 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
1615
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001616 if (image) {
1617 push_ref(L, image);
1618 image->unref();
1619 return 1;
1620 }
1621 }
1622 }
1623 return 0;
1624}
1625
reed@google.com3597b732013-05-22 20:12:50 +00001626static void register_Sk(lua_State* L) {
1627 lua_newtable(L);
1628 lua_pushvalue(L, -1);
1629 lua_setglobal(L, "Sk");
1630 // the Sk table is still on top
1631
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001632 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001633 setfield_function(L, "loadImage", lsk_loadImage);
reed@google.com3597b732013-05-22 20:12:50 +00001634 setfield_function(L, "newPaint", lsk_newPaint);
1635 setfield_function(L, "newPath", lsk_newPath);
1636 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07001637 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001638 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001639 lua_pop(L, 1); // pop off the Sk table
1640}
1641
reed@google.com74ce6f02013-05-22 15:13:18 +00001642#define REG_CLASS(L, C) \
1643 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001644 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001645 lua_pushvalue(L, -1); \
1646 lua_setfield(L, -2, "__index"); \
1647 luaL_setfuncs(L, g##C##_Methods, 0); \
1648 lua_pop(L, 1); /* pop off the meta-table */ \
1649 } while (0)
1650
1651void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001652 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001653 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001654 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001655 REG_CLASS(L, SkImage);
reed@google.com74ce6f02013-05-22 15:13:18 +00001656 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001657 REG_CLASS(L, SkPath);
1658 REG_CLASS(L, SkPathEffect);
reed@google.com74ce6f02013-05-22 15:13:18 +00001659 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001660 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07001661 REG_CLASS(L, SkSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001662 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001663 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001664}
zachr@google.com28c27c82013-06-20 17:15:05 +00001665
reed@google.com7bce9982013-06-20 17:40:21 +00001666extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001667extern "C" int luaopen_skia(lua_State* L) {
1668 SkLua::Load(L);
1669 return 0;
1670}