blob: 773af54dfd6301119bb744691e07bd1dd10499e3 [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"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000016#include "SkDocument.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000017#include "SkImage.h"
18#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000019#include "SkPaint.h"
20#include "SkPath.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000021#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000022#include "SkRRect.h"
23#include "SkString.h"
fmalitab7425172014-08-26 07:56:44 -070024#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000025#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000026
27extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000028 #include "lua.h"
29 #include "lualib.h"
30 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000031}
32
reed@google.comfd345872013-05-22 20:53:42 +000033// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000034template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000035#define DEF_MTNAME(T) \
36 template <> const char* get_mtname<T>() { \
37 return #T "_LuaMetaTableName"; \
38 }
39
40DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000041DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000042DEF_MTNAME(SkImage)
reed@google.comfd345872013-05-22 20:53:42 +000043DEF_MTNAME(SkMatrix)
44DEF_MTNAME(SkRRect)
45DEF_MTNAME(SkPath)
46DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000047DEF_MTNAME(SkPathEffect)
reed@google.com5fdc9832013-07-24 15:47:52 +000048DEF_MTNAME(SkShader)
fmalitab7425172014-08-26 07:56:44 -070049DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000050DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000051
reed@google.com3597b732013-05-22 20:12:50 +000052template <typename T> T* push_new(lua_State* L) {
53 T* addr = (T*)lua_newuserdata(L, sizeof(T));
54 new (addr) T;
55 luaL_getmetatable(L, get_mtname<T>());
56 lua_setmetatable(L, -2);
57 return addr;
58}
reed@google.com74ce6f02013-05-22 15:13:18 +000059
60template <typename T> void push_obj(lua_State* L, const T& obj) {
61 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000062 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000063 lua_setmetatable(L, -2);
64}
65
66template <typename T> void push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000067 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000068 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000069 lua_setmetatable(L, -2);
70}
71
72template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000073 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000074}
75
76template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000077 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000078}
79
reed@google.com88c9ec92013-05-22 15:43:21 +000080static bool lua2bool(lua_State* L, int index) {
81 return !!lua_toboolean(L, index);
82}
83
reed@google.com74ce6f02013-05-22 15:13:18 +000084///////////////////////////////////////////////////////////////////////////////
85
reed@google.com3597b732013-05-22 20:12:50 +000086SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
87 fL = luaL_newstate();
88 luaL_openlibs(fL);
89 SkLua::Load(fL);
90}
91
92SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
93
94SkLua::~SkLua() {
95 if (fWeOwnL) {
96 if (fTermCode.size() > 0) {
97 lua_getglobal(fL, fTermCode.c_str());
98 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
99 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
100 }
101 }
102 lua_close(fL);
103 }
104}
105
106bool SkLua::runCode(const char code[]) {
107 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
108 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000109 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000110 return false;
111 }
112 return true;
113}
114
115bool SkLua::runCode(const void* code, size_t size) {
116 SkString str((const char*)code, size);
117 return this->runCode(str.c_str());
118}
119
120///////////////////////////////////////////////////////////////////////////////
121
122#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
123
reed@google.com29563872013-07-10 21:23:49 +0000124static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
125 if (pred) {
126 lua_pushboolean(L, true);
127 lua_setfield(L, -2, key);
128 }
129}
130
reed@google.com74ce6f02013-05-22 15:13:18 +0000131static void setfield_string(lua_State* L, const char key[], const char value[]) {
132 lua_pushstring(L, value);
133 lua_setfield(L, -2, key);
134}
135
136static void setfield_number(lua_State* L, const char key[], double value) {
137 lua_pushnumber(L, value);
138 lua_setfield(L, -2, key);
139}
140
humper@google.com2815c192013-07-10 22:42:30 +0000141static void setfield_boolean(lua_State* L, const char key[], bool value) {
142 lua_pushboolean(L, value);
143 lua_setfield(L, -2, key);
144}
145
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000146static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
147 setfield_number(L, key, SkScalarToLua(value));
148}
149
reed@google.com3597b732013-05-22 20:12:50 +0000150static void setfield_function(lua_State* L,
151 const char key[], lua_CFunction value) {
152 lua_pushcfunction(L, value);
153 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000154}
155
reed@google.come3823fd2013-05-30 18:55:14 +0000156static void setarray_number(lua_State* L, int index, double value) {
157 lua_pushnumber(L, value);
158 lua_rawseti(L, -2, index);
159}
160
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000161static void setarray_scalar(lua_State* L, int index, SkScalar value) {
162 setarray_number(L, index, SkScalarToLua(value));
163}
164
reed@google.com74ce6f02013-05-22 15:13:18 +0000165void SkLua::pushBool(bool value, const char key[]) {
166 lua_pushboolean(fL, value);
167 CHECK_SETFIELD(key);
168}
169
170void SkLua::pushString(const char str[], const char key[]) {
171 lua_pushstring(fL, str);
172 CHECK_SETFIELD(key);
173}
174
reed@google.come3823fd2013-05-30 18:55:14 +0000175void SkLua::pushString(const char str[], size_t length, const char key[]) {
176 // TODO: how to do this w/o making a copy?
177 SkString s(str, length);
178 lua_pushstring(fL, s.c_str());
179 CHECK_SETFIELD(key);
180}
181
reed@google.com74ce6f02013-05-22 15:13:18 +0000182void SkLua::pushString(const SkString& str, const char key[]) {
183 lua_pushstring(fL, str.c_str());
184 CHECK_SETFIELD(key);
185}
186
187void SkLua::pushColor(SkColor color, const char key[]) {
188 lua_newtable(fL);
189 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
190 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
191 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
192 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
193 CHECK_SETFIELD(key);
194}
195
reed@google.come3823fd2013-05-30 18:55:14 +0000196void SkLua::pushU32(uint32_t value, const char key[]) {
197 lua_pushnumber(fL, (double)value);
198 CHECK_SETFIELD(key);
199}
200
reed@google.com74ce6f02013-05-22 15:13:18 +0000201void SkLua::pushScalar(SkScalar value, const char key[]) {
202 lua_pushnumber(fL, SkScalarToLua(value));
203 CHECK_SETFIELD(key);
204}
205
reed@google.come3823fd2013-05-30 18:55:14 +0000206void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
207 lua_newtable(fL);
208 for (int i = 0; i < count; ++i) {
209 // make it base-1 to match lua convention
210 setarray_number(fL, i + 1, (double)array[i]);
211 }
212 CHECK_SETFIELD(key);
213}
214
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000215void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
216 lua_newtable(fL);
217 for (int i = 0; i < count; ++i) {
218 // make it base-1 to match lua convention
219 lua_newtable(fL);
220 this->pushScalar(array[i].fX, "x");
221 this->pushScalar(array[i].fY, "y");
222 lua_rawseti(fL, -2, i + 1);
223 }
224 CHECK_SETFIELD(key);
225}
226
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000227void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
228 lua_newtable(fL);
229 for (int i = 0; i < count; ++i) {
230 // make it base-1 to match lua convention
231 setarray_scalar(fL, i + 1, array[i]);
232 }
233 CHECK_SETFIELD(key);
234}
235
reed@google.com74ce6f02013-05-22 15:13:18 +0000236void SkLua::pushRect(const SkRect& r, const char key[]) {
237 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000238 setfield_scalar(fL, "left", r.fLeft);
239 setfield_scalar(fL, "top", r.fTop);
240 setfield_scalar(fL, "right", r.fRight);
241 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000242 CHECK_SETFIELD(key);
243}
244
245void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
246 push_obj(fL, rr);
247 CHECK_SETFIELD(key);
248}
249
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000250void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
251 lua_newtable(fL);
252 setfield_scalar(fL, "phase", info.fPhase);
253 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
254 CHECK_SETFIELD(key);
255}
256
257
reed@google.com74ce6f02013-05-22 15:13:18 +0000258void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
259 push_obj(fL, matrix);
260 CHECK_SETFIELD(key);
261}
262
263void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
264 push_obj(fL, paint);
265 CHECK_SETFIELD(key);
266}
267
268void SkLua::pushPath(const SkPath& path, const char key[]) {
269 push_obj(fL, path);
270 CHECK_SETFIELD(key);
271}
272
273void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
274 push_ref(fL, canvas);
275 CHECK_SETFIELD(key);
276}
277
fmalitab7425172014-08-26 07:56:44 -0700278void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
279 push_ref(fL, const_cast<SkTextBlob*>(blob));
280 CHECK_SETFIELD(key);
281}
282
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000283static const char* element_type(SkClipStack::Element::Type type) {
284 switch (type) {
285 case SkClipStack::Element::kEmpty_Type:
286 return "empty";
287 case SkClipStack::Element::kRect_Type:
288 return "rect";
289 case SkClipStack::Element::kRRect_Type:
290 return "rrect";
291 case SkClipStack::Element::kPath_Type:
292 return "path";
293 }
294 return "unknown";
295}
296
297static const char* region_op(SkRegion::Op op) {
298 switch (op) {
299 case SkRegion::kDifference_Op:
300 return "difference";
301 case SkRegion::kIntersect_Op:
302 return "intersect";
303 case SkRegion::kUnion_Op:
304 return "union";
305 case SkRegion::kXOR_Op:
306 return "xor";
307 case SkRegion::kReverseDifference_Op:
308 return "reverse-difference";
309 case SkRegion::kReplace_Op:
310 return "replace";
311 }
312 return "unknown";
313}
314
315void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
316 lua_newtable(fL);
317 SkClipStack::B2TIter iter(stack);
318 const SkClipStack::Element* element;
319 int i = 0;
320 while (NULL != (element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000321 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000322 lua_rawseti(fL, -2, ++i);
323 }
324 CHECK_SETFIELD(key);
325}
326
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000327void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
328 lua_newtable(fL);
329 SkClipStack::Element::Type type = element.getType();
330 this->pushString(element_type(type), "type");
331 switch (type) {
332 case SkClipStack::Element::kEmpty_Type:
333 break;
334 case SkClipStack::Element::kRect_Type:
335 this->pushRect(element.getRect(), "rect");
336 break;
337 case SkClipStack::Element::kRRect_Type:
338 this->pushRRect(element.getRRect(), "rrect");
339 break;
340 case SkClipStack::Element::kPath_Type:
341 this->pushPath(element.getPath(), "path");
342 break;
343 }
344 this->pushString(region_op(element.getOp()), "op");
345 this->pushBool(element.isAA(), "aa");
346 CHECK_SETFIELD(key);
347}
348
349
reed@google.com74ce6f02013-05-22 15:13:18 +0000350///////////////////////////////////////////////////////////////////////////////
351///////////////////////////////////////////////////////////////////////////////
352
353static SkScalar lua2scalar(lua_State* L, int index) {
354 SkASSERT(lua_isnumber(L, index));
355 return SkLuaToScalar(lua_tonumber(L, index));
356}
357
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000358static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
359 if (lua_isnumber(L, index)) {
360 return SkLuaToScalar(lua_tonumber(L, index));
361 } else {
362 return defaultValue;
363 }
364}
365
reed@google.com74ce6f02013-05-22 15:13:18 +0000366static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
367 SkASSERT(lua_istable(L, index));
368 lua_pushstring(L, key);
369 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000370
reed@google.com74ce6f02013-05-22 15:13:18 +0000371 SkScalar value = lua2scalar(L, -1);
372 lua_pop(L, 1);
373 return value;
374}
375
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000376static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
377 SkASSERT(lua_istable(L, index));
378 lua_pushstring(L, key);
379 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000380
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000381 SkScalar value;
382 if (lua_isnil(L, -1)) {
383 value = def;
384 } else {
385 value = lua2scalar(L, -1);
386 }
387 lua_pop(L, 1);
388 return value;
389}
390
reed@google.com74ce6f02013-05-22 15:13:18 +0000391static U8CPU unit2byte(SkScalar x) {
392 if (x <= 0) {
393 return 0;
394 } else if (x >= 1) {
395 return 255;
396 } else {
397 return SkScalarRoundToInt(x * 255);
398 }
399}
400
401static SkColor lua2color(lua_State* L, int index) {
402 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
403 unit2byte(getfield_scalar(L, index, "r")),
404 unit2byte(getfield_scalar(L, index, "g")),
405 unit2byte(getfield_scalar(L, index, "b")));
406}
407
408static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000409 rect->set(getfield_scalar_default(L, index, "left", 0),
410 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000411 getfield_scalar(L, index, "right"),
412 getfield_scalar(L, index, "bottom"));
413 return rect;
414}
415
416static int lcanvas_drawColor(lua_State* L) {
417 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
418 return 0;
419}
420
421static int lcanvas_drawRect(lua_State* L) {
422 SkRect rect;
423 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
424 *get_obj<SkPaint>(L, 3));
425 return 0;
426}
427
428static int lcanvas_drawOval(lua_State* L) {
429 SkRect rect;
430 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
431 *get_obj<SkPaint>(L, 3));
432 return 0;
433}
434
435static int lcanvas_drawCircle(lua_State* L) {
436 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
437 lua2scalar(L, 3),
438 lua2scalar(L, 4),
439 *get_obj<SkPaint>(L, 5));
440 return 0;
441}
442
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000443static int lcanvas_drawImage(lua_State* L) {
444 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
445 SkImage* image = get_ref<SkImage>(L, 2);
446 if (NULL == image) {
447 return 0;
448 }
449 SkScalar x = lua2scalar(L, 3);
450 SkScalar y = lua2scalar(L, 4);
451
452 SkPaint paint;
453 const SkPaint* paintPtr = NULL;
454 if (lua_isnumber(L, 5)) {
455 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
456 paintPtr = &paint;
457 }
458 image->draw(canvas, x, y, paintPtr);
459 return 0;
460}
461
reed@google.comfd345872013-05-22 20:53:42 +0000462static int lcanvas_drawPath(lua_State* L) {
463 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
464 *get_obj<SkPaint>(L, 3));
465 return 0;
466}
467
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000468static int lcanvas_drawText(lua_State* L) {
469 if (lua_gettop(L) < 5) {
470 return 0;
471 }
472
473 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
474 size_t len;
475 const char* text = lua_tolstring(L, 2, &len);
476 get_ref<SkCanvas>(L, 1)->drawText(text, len,
477 lua2scalar(L, 3), lua2scalar(L, 4),
478 *get_obj<SkPaint>(L, 5));
479 }
480 return 0;
481}
482
reed@google.com74ce6f02013-05-22 15:13:18 +0000483static int lcanvas_getSaveCount(lua_State* L) {
484 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
485 return 1;
486}
487
488static int lcanvas_getTotalMatrix(lua_State* L) {
489 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
490 return 1;
491}
492
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000493static int lcanvas_getClipStack(lua_State* L) {
494 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
495 return 1;
496}
497
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000498int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
499#if SK_SUPPORT_GPU
500 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
501 SkISize layerSize = canvas->getTopLayerSize();
502 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
503 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
504 layerSize.fWidth, layerSize.fHeight);
505
506 GrReducedClip::ElementList elements;
507 GrReducedClip::InitialState initialState;
508 int32_t genID;
509 SkIRect resultBounds;
510
511 const SkClipStack& stack = *canvas->getClipStack();
512
513 GrReducedClip::ReduceClipStack(stack,
514 queryBounds,
515 &elements,
516 &genID,
517 &initialState,
518 &resultBounds,
519 NULL);
520
521 GrReducedClip::ElementList::Iter iter(elements);
522 int i = 0;
523 lua_newtable(L);
524 while(NULL != iter.get()) {
525 SkLua(L).pushClipStackElement(*iter.get());
526 iter.next();
527 lua_rawseti(L, -2, ++i);
528 }
529 // Currently this only returns the element list to lua, not the initial state or result bounds.
530 // It could return these as additional items on the lua stack.
531 return 1;
532#else
533 return 0;
534#endif
535}
536
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000537static int lcanvas_save(lua_State* L) {
538 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
539 return 1;
540}
541
542static int lcanvas_restore(lua_State* L) {
543 get_ref<SkCanvas>(L, 1)->restore();
544 return 0;
545}
546
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000547static int lcanvas_scale(lua_State* L) {
548 SkScalar sx = lua2scalar_def(L, 2, 1);
549 SkScalar sy = lua2scalar_def(L, 3, sx);
550 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
551 return 0;
552}
553
reed@google.com3597b732013-05-22 20:12:50 +0000554static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000555 SkScalar tx = lua2scalar_def(L, 2, 0);
556 SkScalar ty = lua2scalar_def(L, 3, 0);
557 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
558 return 0;
559}
560
561static int lcanvas_rotate(lua_State* L) {
562 SkScalar degrees = lua2scalar_def(L, 2, 0);
563 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000564 return 0;
565}
566
reed@google.com74ce6f02013-05-22 15:13:18 +0000567static int lcanvas_gc(lua_State* L) {
568 get_ref<SkCanvas>(L, 1)->unref();
569 return 0;
570}
571
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000572const struct luaL_Reg gSkCanvas_Methods[] = {
reed@google.com74ce6f02013-05-22 15:13:18 +0000573 { "drawColor", lcanvas_drawColor },
574 { "drawRect", lcanvas_drawRect },
575 { "drawOval", lcanvas_drawOval },
576 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000577 { "drawImage", lcanvas_drawImage },
reed@google.comfd345872013-05-22 20:53:42 +0000578 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000579 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000580 { "getSaveCount", lcanvas_getSaveCount },
581 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000582 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000583#if SK_SUPPORT_GPU
584 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
585#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000586 { "save", lcanvas_save },
587 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000588 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000589 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000590 { "rotate", lcanvas_rotate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000591 { "__gc", lcanvas_gc },
592 { NULL, NULL }
593};
594
595///////////////////////////////////////////////////////////////////////////////
596
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000597static int ldocument_beginPage(lua_State* L) {
598 const SkRect* contentPtr = NULL;
599 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
600 lua2scalar(L, 3),
601 contentPtr));
602 return 1;
603}
604
605static int ldocument_endPage(lua_State* L) {
606 get_ref<SkDocument>(L, 1)->endPage();
607 return 0;
608}
609
610static int ldocument_close(lua_State* L) {
611 get_ref<SkDocument>(L, 1)->close();
612 return 0;
613}
614
615static int ldocument_gc(lua_State* L) {
616 get_ref<SkDocument>(L, 1)->unref();
617 return 0;
618}
619
620static const struct luaL_Reg gSkDocument_Methods[] = {
621 { "beginPage", ldocument_beginPage },
622 { "endPage", ldocument_endPage },
623 { "close", ldocument_close },
624 { "__gc", ldocument_gc },
625 { NULL, NULL }
626};
627
628///////////////////////////////////////////////////////////////////////////////
629
reed@google.com74ce6f02013-05-22 15:13:18 +0000630static int lpaint_isAntiAlias(lua_State* L) {
631 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
632 return 1;
633}
634
635static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000636 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000637 return 0;
638}
639
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000640static int lpaint_isDither(lua_State* L) {
641 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
642 return 1;
643}
644
645static int lpaint_isUnderlineText(lua_State* L) {
646 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
647 return 1;
648}
649
650static int lpaint_isStrikeThruText(lua_State* L) {
651 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
652 return 1;
653}
654
655static int lpaint_isFakeBoldText(lua_State* L) {
656 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
657 return 1;
658}
659
660static int lpaint_isLinearText(lua_State* L) {
661 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
662 return 1;
663}
664
665static int lpaint_isSubpixelText(lua_State* L) {
666 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
667 return 1;
668}
669
670static int lpaint_isDevKernText(lua_State* L) {
671 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
672 return 1;
673}
674
675static int lpaint_isLCDRenderText(lua_State* L) {
676 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
677 return 1;
678}
679
680static int lpaint_isEmbeddedBitmapText(lua_State* L) {
681 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
682 return 1;
683}
684
685static int lpaint_isAutohinted(lua_State* L) {
686 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
687 return 1;
688}
689
690static int lpaint_isVerticalText(lua_State* L) {
691 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
692 return 1;
693}
694
reed@google.com74ce6f02013-05-22 15:13:18 +0000695static int lpaint_getColor(lua_State* L) {
696 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
697 return 1;
698}
699
700static int lpaint_setColor(lua_State* L) {
701 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
702 return 0;
703}
704
reed@google.come3823fd2013-05-30 18:55:14 +0000705static int lpaint_getTextSize(lua_State* L) {
706 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
707 return 1;
708}
709
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000710static int lpaint_getTextScaleX(lua_State* L) {
711 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
712 return 1;
713}
714
715static int lpaint_getTextSkewX(lua_State* L) {
716 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
717 return 1;
718}
719
reed@google.come3823fd2013-05-30 18:55:14 +0000720static int lpaint_setTextSize(lua_State* L) {
721 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
722 return 0;
723}
724
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000725static int lpaint_getTypeface(lua_State* L) {
726 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
727 return 1;
728}
729
730static int lpaint_setTypeface(lua_State* L) {
731 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
732 return 0;
733}
734
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000735static int lpaint_getHinting(lua_State* L) {
736 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
737 return 1;
738}
739
reed@google.come3823fd2013-05-30 18:55:14 +0000740static int lpaint_getFontID(lua_State* L) {
741 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
742 SkLua(L).pushU32(SkTypeface::UniqueID(face));
743 return 1;
744}
745
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000746static const struct {
747 const char* fLabel;
748 SkPaint::Align fAlign;
749} gAlignRec[] = {
750 { "left", SkPaint::kLeft_Align },
751 { "center", SkPaint::kCenter_Align },
752 { "right", SkPaint::kRight_Align },
753};
754
755static int lpaint_getTextAlign(lua_State* L) {
756 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
757 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
758 if (gAlignRec[i].fAlign == align) {
759 lua_pushstring(L, gAlignRec[i].fLabel);
760 return 1;
761 }
762 }
763 return 0;
764}
765
766static int lpaint_setTextAlign(lua_State* L) {
767 if (lua_isstring(L, 2)) {
768 size_t len;
769 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000770
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000771 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
772 if (!strcmp(gAlignRec[i].fLabel, label)) {
773 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
774 break;
775 }
776 }
777 }
778 return 0;
779}
780
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000781static int lpaint_getStroke(lua_State* L) {
782 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
783 return 1;
784}
785
786static int lpaint_setStroke(lua_State* L) {
787 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000788
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000789 if (lua_toboolean(L, 2)) {
790 style = SkPaint::kStroke_Style;
791 } else {
792 style = SkPaint::kFill_Style;
793 }
794 get_obj<SkPaint>(L, 1)->setStyle(style);
795 return 0;
796}
797
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000798static int lpaint_getStrokeCap(lua_State* L) {
799 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
800 return 1;
801}
802
803static int lpaint_getStrokeJoin(lua_State* L) {
804 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
805 return 1;
806}
807
808static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000809 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000810 return 1;
811}
812
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000813static int lpaint_getStrokeWidth(lua_State* L) {
814 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
815 return 1;
816}
817
818static int lpaint_setStrokeWidth(lua_State* L) {
819 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
820 return 0;
821}
822
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000823static int lpaint_getStrokeMiter(lua_State* L) {
824 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
825 return 1;
826}
827
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000828static int lpaint_measureText(lua_State* L) {
829 if (lua_isstring(L, 2)) {
830 size_t len;
831 const char* text = lua_tolstring(L, 2, &len);
832 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
833 return 1;
834 }
835 return 0;
836}
837
838struct FontMetrics {
839 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
840 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
841 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
842 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
843 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
844 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
845 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
846 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
847 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
848};
849
850static int lpaint_getFontMetrics(lua_State* L) {
851 SkPaint::FontMetrics fm;
852 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000853
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000854 lua_newtable(L);
855 setfield_scalar(L, "top", fm.fTop);
856 setfield_scalar(L, "ascent", fm.fAscent);
857 setfield_scalar(L, "descent", fm.fDescent);
858 setfield_scalar(L, "bottom", fm.fBottom);
859 setfield_scalar(L, "leading", fm.fLeading);
860 SkLua(L).pushScalar(height);
861 return 2;
862}
863
reed@google.com29563872013-07-10 21:23:49 +0000864static int lpaint_getEffects(lua_State* L) {
865 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000866
reed@google.com29563872013-07-10 21:23:49 +0000867 lua_newtable(L);
868 setfield_bool_if(L, "looper", !!paint->getLooper());
869 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
870 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
871 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
872 setfield_bool_if(L, "shader", !!paint->getShader());
873 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
874 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
875 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
876 return 1;
877}
878
reed@google.com5fdc9832013-07-24 15:47:52 +0000879static int lpaint_getShader(lua_State* L) {
880 const SkPaint* paint = get_obj<SkPaint>(L, 1);
881 SkShader* shader = paint->getShader();
882 if (shader) {
883 push_ref(L, shader);
884 return 1;
885 }
886 return 0;
887}
888
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000889static int lpaint_getPathEffect(lua_State* L) {
890 const SkPaint* paint = get_obj<SkPaint>(L, 1);
891 SkPathEffect* pe = paint->getPathEffect();
892 if (pe) {
893 push_ref(L, pe);
894 return 1;
895 }
896 return 0;
897}
898
reed@google.com74ce6f02013-05-22 15:13:18 +0000899static int lpaint_gc(lua_State* L) {
900 get_obj<SkPaint>(L, 1)->~SkPaint();
901 return 0;
902}
903
904static const struct luaL_Reg gSkPaint_Methods[] = {
905 { "isAntiAlias", lpaint_isAntiAlias },
906 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000907 { "isDither", lpaint_isDither },
908 { "isUnderlineText", lpaint_isUnderlineText },
909 { "isStrikeThruText", lpaint_isStrikeThruText },
910 { "isFakeBoldText", lpaint_isFakeBoldText },
911 { "isLinearText", lpaint_isLinearText },
912 { "isSubpixelText", lpaint_isSubpixelText },
913 { "isDevKernText", lpaint_isDevKernText },
914 { "isLCDRenderText", lpaint_isLCDRenderText },
915 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
916 { "isAutohinted", lpaint_isAutohinted },
917 { "isVerticalText", lpaint_isVerticalText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000918 { "getColor", lpaint_getColor },
919 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000920 { "getTextSize", lpaint_getTextSize },
921 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000922 { "getTextScaleX", lpaint_getTextScaleX },
923 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000924 { "getTypeface", lpaint_getTypeface },
925 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000926 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +0000927 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000928 { "getTextAlign", lpaint_getTextAlign },
929 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000930 { "getStroke", lpaint_getStroke },
931 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000932 { "getStrokeCap", lpaint_getStrokeCap },
933 { "getStrokeJoin", lpaint_getStrokeJoin },
934 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000935 { "getStrokeWidth", lpaint_getStrokeWidth },
936 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000937 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000938 { "measureText", lpaint_measureText },
939 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +0000940 { "getEffects", lpaint_getEffects },
reed@google.com5fdc9832013-07-24 15:47:52 +0000941 { "getShader", lpaint_getShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000942 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +0000943 { "__gc", lpaint_gc },
944 { NULL, NULL }
945};
946
947///////////////////////////////////////////////////////////////////////////////
948
reed@google.com5fdc9832013-07-24 15:47:52 +0000949static const char* mode2string(SkShader::TileMode mode) {
950 static const char* gNames[] = { "clamp", "repeat", "mirror" };
951 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
952 return gNames[mode];
953}
954
955static const char* gradtype2string(SkShader::GradientType t) {
956 static const char* gNames[] = {
957 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
958 };
959 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
960 return gNames[t];
961}
962
963static int lshader_isOpaque(lua_State* L) {
964 SkShader* shader = get_ref<SkShader>(L, 1);
965 return shader && shader->isOpaque();
966}
967
968static int lshader_asABitmap(lua_State* L) {
969 SkShader* shader = get_ref<SkShader>(L, 1);
970 if (shader) {
971 SkBitmap bm;
972 SkMatrix matrix;
973 SkShader::TileMode modes[2];
974 switch (shader->asABitmap(&bm, &matrix, modes)) {
975 case SkShader::kDefault_BitmapType:
976 lua_newtable(L);
977 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
978 setfield_number(L, "width", bm.width());
979 setfield_number(L, "height", bm.height());
980 setfield_string(L, "tileX", mode2string(modes[0]));
981 setfield_string(L, "tileY", mode2string(modes[1]));
982 return 1;
983 default:
984 break;
985 }
986 }
987 return 0;
988}
989
990static int lshader_asAGradient(lua_State* L) {
991 SkShader* shader = get_ref<SkShader>(L, 1);
992 if (shader) {
993 SkShader::GradientInfo info;
994 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000995
996 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000997 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000998
999 info.fColorCount = 3;
1000 info.fColors = &colors[0];
1001 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001002
reed@google.com5fdc9832013-07-24 15:47:52 +00001003 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001004
reed@google.com5fdc9832013-07-24 15:47:52 +00001005 if (SkShader::kNone_GradientType != t) {
1006 lua_newtable(L);
1007 setfield_string(L, "type", gradtype2string(t));
1008 setfield_number(L, "colorCount", info.fColorCount);
1009 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001010
1011 if (info.fColorCount == 3){
1012 setfield_number(L, "midPos", pos[1]);
1013 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001014
reed@google.com5fdc9832013-07-24 15:47:52 +00001015 return 1;
1016 }
1017 }
1018 return 0;
1019}
1020
1021static int lshader_gc(lua_State* L) {
1022 get_ref<SkShader>(L, 1)->unref();
1023 return 0;
1024}
1025
1026static const struct luaL_Reg gSkShader_Methods[] = {
1027 { "isOpaque", lshader_isOpaque },
1028 { "asABitmap", lshader_asABitmap },
1029 { "asAGradient", lshader_asAGradient },
1030 { "__gc", lshader_gc },
1031 { NULL, NULL }
1032};
1033
1034///////////////////////////////////////////////////////////////////////////////
1035
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001036static int lpatheffect_asADash(lua_State* L) {
1037 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1038 if (pe) {
1039 SkPathEffect::DashInfo info;
1040 SkPathEffect::DashType dashType = pe->asADash(&info);
1041 if (SkPathEffect::kDash_DashType == dashType) {
1042 SkAutoTArray<SkScalar> intervals(info.fCount);
1043 info.fIntervals = intervals.get();
1044 pe->asADash(&info);
1045 SkLua(L).pushDash(info);
1046 return 1;
1047 }
1048 }
1049 return 0;
1050}
1051
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001052static int lpatheffect_gc(lua_State* L) {
1053 get_ref<SkPathEffect>(L, 1)->unref();
1054 return 0;
1055}
1056
1057static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001058 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001059 { "__gc", lpatheffect_gc },
1060 { NULL, NULL }
1061};
1062
1063///////////////////////////////////////////////////////////////////////////////
1064
humper@google.com2815c192013-07-10 22:42:30 +00001065static int lmatrix_getType(lua_State* L) {
1066 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001067
humper@google.com2815c192013-07-10 22:42:30 +00001068 lua_newtable(L);
1069 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1070 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1071 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1072 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1073 return 1;
1074}
1075
humper@google.com0f48ee02013-07-26 15:23:43 +00001076static int lmatrix_getScaleX(lua_State* L) {
1077 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1078 return 1;
1079}
1080
1081static int lmatrix_getScaleY(lua_State* L) {
1082 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1083 return 1;
1084}
1085
1086static int lmatrix_getTranslateX(lua_State* L) {
1087 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1088 return 1;
1089}
1090
1091static int lmatrix_getTranslateY(lua_State* L) {
1092 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1093 return 1;
1094}
1095
humper@google.com2815c192013-07-10 22:42:30 +00001096static const struct luaL_Reg gSkMatrix_Methods[] = {
1097 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001098 { "getScaleX", lmatrix_getScaleX },
1099 { "getScaleY", lmatrix_getScaleY },
1100 { "getTranslateX", lmatrix_getTranslateX },
1101 { "getTranslateY", lmatrix_getTranslateY },
humper@google.com2815c192013-07-10 22:42:30 +00001102 { NULL, NULL }
1103};
1104
1105///////////////////////////////////////////////////////////////////////////////
1106
reed@google.com74ce6f02013-05-22 15:13:18 +00001107static int lpath_getBounds(lua_State* L) {
1108 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1109 return 1;
1110}
1111
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001112static const char* fill_type_to_str(SkPath::FillType fill) {
1113 switch (fill) {
1114 case SkPath::kEvenOdd_FillType:
1115 return "even-odd";
1116 case SkPath::kWinding_FillType:
1117 return "winding";
1118 case SkPath::kInverseEvenOdd_FillType:
1119 return "inverse-even-odd";
1120 case SkPath::kInverseWinding_FillType:
1121 return "inverse-winding";
1122 }
1123 return "unknown";
1124}
1125
1126static int lpath_getFillType(lua_State* L) {
1127 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1128 SkLua(L).pushString(fill_type_to_str(fill));
1129 return 1;
1130}
1131
1132static SkString segment_masks_to_str(uint32_t segmentMasks) {
1133 SkString result;
1134 bool first = true;
1135 if (SkPath::kLine_SegmentMask & segmentMasks) {
1136 result.append("line");
1137 first = false;
1138 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1139 }
1140 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1141 if (!first) {
1142 result.append(" ");
1143 }
1144 result.append("quad");
1145 first = false;
1146 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1147 }
1148 if (SkPath::kConic_SegmentMask & segmentMasks) {
1149 if (!first) {
1150 result.append(" ");
1151 }
1152 result.append("conic");
1153 first = false;
1154 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1155 }
1156 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1157 if (!first) {
1158 result.append(" ");
1159 }
1160 result.append("cubic");
1161 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1162 }
1163 SkASSERT(0 == segmentMasks);
1164 return result;
1165}
1166
krajcevski95498ed2014-08-18 08:02:33 -07001167static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001168 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1169 SkLua(L).pushString(segment_masks_to_str(segMasks));
1170 return 1;
1171}
1172
1173static int lpath_isConvex(lua_State* L) {
1174 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1175 SkLua(L).pushBool(isConvex);
1176 return 1;
1177}
1178
reed@google.com74ce6f02013-05-22 15:13:18 +00001179static int lpath_isEmpty(lua_State* L) {
1180 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1181 return 1;
1182}
1183
1184static int lpath_isRect(lua_State* L) {
1185 SkRect r;
1186 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1187 int ret_count = 1;
1188 lua_pushboolean(L, pred);
1189 if (pred) {
1190 SkLua(L).pushRect(r);
1191 ret_count += 1;
1192 }
1193 return ret_count;
1194}
1195
1196static const char* dir2string(SkPath::Direction dir) {
1197 static const char* gStr[] = {
1198 "unknown", "cw", "ccw"
1199 };
1200 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1201 return gStr[dir];
1202}
1203
1204static int lpath_isNestedRects(lua_State* L) {
1205 SkRect rects[2];
1206 SkPath::Direction dirs[2];
1207 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1208 int ret_count = 1;
1209 lua_pushboolean(L, pred);
1210 if (pred) {
1211 SkLua lua(L);
1212 lua.pushRect(rects[0]);
1213 lua.pushRect(rects[1]);
1214 lua_pushstring(L, dir2string(dirs[0]));
1215 lua_pushstring(L, dir2string(dirs[0]));
1216 ret_count += 4;
1217 }
1218 return ret_count;
1219}
1220
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001221static int lpath_countPoints(lua_State* L) {
1222 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1223 return 1;
1224}
1225
reed@google.com74ce6f02013-05-22 15:13:18 +00001226static int lpath_reset(lua_State* L) {
1227 get_obj<SkPath>(L, 1)->reset();
1228 return 0;
1229}
1230
1231static int lpath_moveTo(lua_State* L) {
1232 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1233 return 0;
1234}
1235
1236static int lpath_lineTo(lua_State* L) {
1237 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1238 return 0;
1239}
1240
1241static int lpath_quadTo(lua_State* L) {
1242 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1243 lua2scalar(L, 4), lua2scalar(L, 5));
1244 return 0;
1245}
1246
1247static int lpath_cubicTo(lua_State* L) {
1248 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1249 lua2scalar(L, 4), lua2scalar(L, 5),
1250 lua2scalar(L, 6), lua2scalar(L, 7));
1251 return 0;
1252}
1253
1254static int lpath_close(lua_State* L) {
1255 get_obj<SkPath>(L, 1)->close();
1256 return 0;
1257}
1258
1259static int lpath_gc(lua_State* L) {
1260 get_obj<SkPath>(L, 1)->~SkPath();
1261 return 0;
1262}
1263
1264static const struct luaL_Reg gSkPath_Methods[] = {
1265 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001266 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001267 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001268 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001269 { "isEmpty", lpath_isEmpty },
1270 { "isRect", lpath_isRect },
1271 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001272 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001273 { "reset", lpath_reset },
1274 { "moveTo", lpath_moveTo },
1275 { "lineTo", lpath_lineTo },
1276 { "quadTo", lpath_quadTo },
1277 { "cubicTo", lpath_cubicTo },
1278 { "close", lpath_close },
1279 { "__gc", lpath_gc },
1280 { NULL, NULL }
1281};
1282
1283///////////////////////////////////////////////////////////////////////////////
1284
1285static const char* rrect_type(const SkRRect& rr) {
1286 switch (rr.getType()) {
1287 case SkRRect::kUnknown_Type: return "unknown";
1288 case SkRRect::kEmpty_Type: return "empty";
1289 case SkRRect::kRect_Type: return "rect";
1290 case SkRRect::kOval_Type: return "oval";
1291 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001292 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001293 case SkRRect::kComplex_Type: return "complex";
1294 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001295 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001296 return "";
1297}
1298
1299static int lrrect_rect(lua_State* L) {
1300 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1301 return 1;
1302}
1303
1304static int lrrect_type(lua_State* L) {
1305 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1306 return 1;
1307}
1308
1309static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001310 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001311 SkVector v;
1312 if (corner < 0 || corner > 3) {
1313 SkDebugf("bad corner index %d", corner);
1314 v.set(0, 0);
1315 } else {
1316 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1317 }
1318 lua_pushnumber(L, v.fX);
1319 lua_pushnumber(L, v.fY);
1320 return 2;
1321}
1322
1323static int lrrect_gc(lua_State* L) {
1324 get_obj<SkRRect>(L, 1)->~SkRRect();
1325 return 0;
1326}
1327
1328static const struct luaL_Reg gSkRRect_Methods[] = {
1329 { "rect", lrrect_rect },
1330 { "type", lrrect_type },
1331 { "radii", lrrect_radii },
1332 { "__gc", lrrect_gc },
1333 { NULL, NULL }
1334};
1335
1336///////////////////////////////////////////////////////////////////////////////
1337
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001338static int limage_width(lua_State* L) {
1339 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1340 return 1;
1341}
1342
1343static int limage_height(lua_State* L) {
1344 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1345 return 1;
1346}
1347
1348static int limage_gc(lua_State* L) {
1349 get_ref<SkImage>(L, 1)->unref();
1350 return 0;
1351}
1352
1353static const struct luaL_Reg gSkImage_Methods[] = {
1354 { "width", limage_width },
1355 { "height", limage_height },
1356 { "__gc", limage_gc },
1357 { NULL, NULL }
1358};
1359
1360///////////////////////////////////////////////////////////////////////////////
1361
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001362static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001363 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001364 return 0;
1365}
1366
1367static const struct luaL_Reg gSkTypeface_Methods[] = {
1368 { "__gc", ltypeface_gc },
1369 { NULL, NULL }
1370};
1371
1372///////////////////////////////////////////////////////////////////////////////
1373
reed@google.com74ce6f02013-05-22 15:13:18 +00001374class AutoCallLua {
1375public:
1376 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1377 lua_getglobal(L, func);
1378 if (!lua_isfunction(L, -1)) {
1379 int t = lua_type(L, -1);
1380 SkDebugf("--- expected function %d\n", t);
1381 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001382
reed@google.com74ce6f02013-05-22 15:13:18 +00001383 lua_newtable(L);
1384 setfield_string(L, "verb", verb);
1385 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001386
reed@google.com74ce6f02013-05-22 15:13:18 +00001387 ~AutoCallLua() {
1388 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1389 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1390 }
1391 lua_settop(fL, -1);
1392 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001393
reed@google.com74ce6f02013-05-22 15:13:18 +00001394private:
1395 lua_State* fL;
1396};
1397
1398#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1399
1400///////////////////////////////////////////////////////////////////////////////
1401
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001402static int lsk_newDocumentPDF(lua_State* L) {
1403 const char* file = NULL;
1404 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1405 file = lua_tolstring(L, 1, NULL);
1406 }
1407
1408 SkDocument* doc = SkDocument::CreatePDF(file);
1409 if (NULL == doc) {
1410 // do I need to push a nil on the stack and return 1?
1411 return 0;
1412 } else {
1413 push_ref(L, doc);
1414 doc->unref();
1415 return 1;
1416 }
1417}
1418
reed@google.com3597b732013-05-22 20:12:50 +00001419static int lsk_newPaint(lua_State* L) {
1420 push_new<SkPaint>(L);
1421 return 1;
1422}
1423
1424static int lsk_newPath(lua_State* L) {
1425 push_new<SkPath>(L);
1426 return 1;
1427}
1428
1429static int lsk_newRRect(lua_State* L) {
1430 SkRRect* rr = push_new<SkRRect>(L);
1431 rr->setEmpty();
1432 return 1;
1433}
1434
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001435static int lsk_newTypeface(lua_State* L) {
1436 const char* name = NULL;
1437 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001438
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001439 int count = lua_gettop(L);
1440 if (count > 0 && lua_isstring(L, 1)) {
1441 name = lua_tolstring(L, 1, NULL);
1442 if (count > 1 && lua_isnumber(L, 2)) {
1443 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1444 }
1445 }
1446
1447 SkTypeface* face = SkTypeface::CreateFromName(name,
1448 (SkTypeface::Style)style);
1449// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1450 if (NULL == face) {
1451 face = SkTypeface::RefDefault();
1452 }
1453 push_ref(L, face);
1454 face->unref();
1455 return 1;
1456}
reed@google.com3597b732013-05-22 20:12:50 +00001457
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001458static int lsk_loadImage(lua_State* L) {
1459 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1460 const char* name = lua_tolstring(L, 1, NULL);
1461 SkAutoDataUnref data(SkData::NewFromFileName(name));
1462 if (data.get()) {
1463 SkImage* image = SkImage::NewEncodedData(data.get());
1464 if (image) {
1465 push_ref(L, image);
1466 image->unref();
1467 return 1;
1468 }
1469 }
1470 }
1471 return 0;
1472}
1473
reed@google.com3597b732013-05-22 20:12:50 +00001474static void register_Sk(lua_State* L) {
1475 lua_newtable(L);
1476 lua_pushvalue(L, -1);
1477 lua_setglobal(L, "Sk");
1478 // the Sk table is still on top
1479
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001480 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001481 setfield_function(L, "loadImage", lsk_loadImage);
reed@google.com3597b732013-05-22 20:12:50 +00001482 setfield_function(L, "newPaint", lsk_newPaint);
1483 setfield_function(L, "newPath", lsk_newPath);
1484 setfield_function(L, "newRRect", lsk_newRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001485 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001486 lua_pop(L, 1); // pop off the Sk table
1487}
1488
reed@google.com74ce6f02013-05-22 15:13:18 +00001489#define REG_CLASS(L, C) \
1490 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001491 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001492 lua_pushvalue(L, -1); \
1493 lua_setfield(L, -2, "__index"); \
1494 luaL_setfuncs(L, g##C##_Methods, 0); \
1495 lua_pop(L, 1); /* pop off the meta-table */ \
1496 } while (0)
1497
1498void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001499 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001500 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001501 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001502 REG_CLASS(L, SkImage);
reed@google.com74ce6f02013-05-22 15:13:18 +00001503 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001504 REG_CLASS(L, SkPath);
1505 REG_CLASS(L, SkPathEffect);
reed@google.com74ce6f02013-05-22 15:13:18 +00001506 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001507 REG_CLASS(L, SkShader);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001508 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001509 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001510}
zachr@google.com28c27c82013-06-20 17:15:05 +00001511
reed@google.com7bce9982013-06-20 17:40:21 +00001512extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001513extern "C" int luaopen_skia(lua_State* L) {
1514 SkLua::Load(L);
1515 return 0;
1516}