blob: 346899a5dde785b0e00fd233aa1df8df494b7c06 [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"
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000021#include "SkPathEffect.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"
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)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000049DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000050
reed@google.com3597b732013-05-22 20:12:50 +000051template <typename T> T* push_new(lua_State* L) {
52 T* addr = (T*)lua_newuserdata(L, sizeof(T));
53 new (addr) T;
54 luaL_getmetatable(L, get_mtname<T>());
55 lua_setmetatable(L, -2);
56 return addr;
57}
reed@google.com74ce6f02013-05-22 15:13:18 +000058
59template <typename T> void push_obj(lua_State* L, const T& obj) {
60 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000061 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000062 lua_setmetatable(L, -2);
63}
64
65template <typename T> void push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000066 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000067 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000068 lua_setmetatable(L, -2);
69}
70
71template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000072 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000073}
74
75template <typename T> T* get_obj(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
reed@google.com88c9ec92013-05-22 15:43:21 +000079static bool lua2bool(lua_State* L, int index) {
80 return !!lua_toboolean(L, index);
81}
82
reed@google.com74ce6f02013-05-22 15:13:18 +000083///////////////////////////////////////////////////////////////////////////////
84
reed@google.com3597b732013-05-22 20:12:50 +000085SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
86 fL = luaL_newstate();
87 luaL_openlibs(fL);
88 SkLua::Load(fL);
89}
90
91SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
92
93SkLua::~SkLua() {
94 if (fWeOwnL) {
95 if (fTermCode.size() > 0) {
96 lua_getglobal(fL, fTermCode.c_str());
97 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
98 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
99 }
100 }
101 lua_close(fL);
102 }
103}
104
105bool SkLua::runCode(const char code[]) {
106 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
107 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000108 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000109 return false;
110 }
111 return true;
112}
113
114bool SkLua::runCode(const void* code, size_t size) {
115 SkString str((const char*)code, size);
116 return this->runCode(str.c_str());
117}
118
119///////////////////////////////////////////////////////////////////////////////
120
121#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
122
reed@google.com29563872013-07-10 21:23:49 +0000123static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
124 if (pred) {
125 lua_pushboolean(L, true);
126 lua_setfield(L, -2, key);
127 }
128}
129
reed@google.com74ce6f02013-05-22 15:13:18 +0000130static void setfield_string(lua_State* L, const char key[], const char value[]) {
131 lua_pushstring(L, value);
132 lua_setfield(L, -2, key);
133}
134
135static void setfield_number(lua_State* L, const char key[], double value) {
136 lua_pushnumber(L, value);
137 lua_setfield(L, -2, key);
138}
139
humper@google.com2815c192013-07-10 22:42:30 +0000140static void setfield_boolean(lua_State* L, const char key[], bool value) {
141 lua_pushboolean(L, value);
142 lua_setfield(L, -2, key);
143}
144
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000145static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
146 setfield_number(L, key, SkScalarToLua(value));
147}
148
reed@google.com3597b732013-05-22 20:12:50 +0000149static void setfield_function(lua_State* L,
150 const char key[], lua_CFunction value) {
151 lua_pushcfunction(L, value);
152 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000153}
154
reed@google.come3823fd2013-05-30 18:55:14 +0000155static void setarray_number(lua_State* L, int index, double value) {
156 lua_pushnumber(L, value);
157 lua_rawseti(L, -2, index);
158}
159
reed@google.com74ce6f02013-05-22 15:13:18 +0000160void SkLua::pushBool(bool value, const char key[]) {
161 lua_pushboolean(fL, value);
162 CHECK_SETFIELD(key);
163}
164
165void SkLua::pushString(const char str[], const char key[]) {
166 lua_pushstring(fL, str);
167 CHECK_SETFIELD(key);
168}
169
reed@google.come3823fd2013-05-30 18:55:14 +0000170void SkLua::pushString(const char str[], size_t length, const char key[]) {
171 // TODO: how to do this w/o making a copy?
172 SkString s(str, length);
173 lua_pushstring(fL, s.c_str());
174 CHECK_SETFIELD(key);
175}
176
reed@google.com74ce6f02013-05-22 15:13:18 +0000177void SkLua::pushString(const SkString& str, const char key[]) {
178 lua_pushstring(fL, str.c_str());
179 CHECK_SETFIELD(key);
180}
181
182void SkLua::pushColor(SkColor color, const char key[]) {
183 lua_newtable(fL);
184 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
185 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
186 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
187 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
188 CHECK_SETFIELD(key);
189}
190
reed@google.come3823fd2013-05-30 18:55:14 +0000191void SkLua::pushU32(uint32_t value, const char key[]) {
192 lua_pushnumber(fL, (double)value);
193 CHECK_SETFIELD(key);
194}
195
reed@google.com74ce6f02013-05-22 15:13:18 +0000196void SkLua::pushScalar(SkScalar value, const char key[]) {
197 lua_pushnumber(fL, SkScalarToLua(value));
198 CHECK_SETFIELD(key);
199}
200
reed@google.come3823fd2013-05-30 18:55:14 +0000201void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
202 lua_newtable(fL);
203 for (int i = 0; i < count; ++i) {
204 // make it base-1 to match lua convention
205 setarray_number(fL, i + 1, (double)array[i]);
206 }
207 CHECK_SETFIELD(key);
208}
209
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000210void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
211 lua_newtable(fL);
212 for (int i = 0; i < count; ++i) {
213 // make it base-1 to match lua convention
214 lua_newtable(fL);
215 this->pushScalar(array[i].fX, "x");
216 this->pushScalar(array[i].fY, "y");
217 lua_rawseti(fL, -2, i + 1);
218 }
219 CHECK_SETFIELD(key);
220}
221
reed@google.com74ce6f02013-05-22 15:13:18 +0000222void SkLua::pushRect(const SkRect& r, const char key[]) {
223 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000224 setfield_scalar(fL, "left", r.fLeft);
225 setfield_scalar(fL, "top", r.fTop);
226 setfield_scalar(fL, "right", r.fRight);
227 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000228 CHECK_SETFIELD(key);
229}
230
231void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
232 push_obj(fL, rr);
233 CHECK_SETFIELD(key);
234}
235
236void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
237 push_obj(fL, matrix);
238 CHECK_SETFIELD(key);
239}
240
241void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
242 push_obj(fL, paint);
243 CHECK_SETFIELD(key);
244}
245
246void SkLua::pushPath(const SkPath& path, const char key[]) {
247 push_obj(fL, path);
248 CHECK_SETFIELD(key);
249}
250
251void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
252 push_ref(fL, canvas);
253 CHECK_SETFIELD(key);
254}
255
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000256static const char* element_type(SkClipStack::Element::Type type) {
257 switch (type) {
258 case SkClipStack::Element::kEmpty_Type:
259 return "empty";
260 case SkClipStack::Element::kRect_Type:
261 return "rect";
262 case SkClipStack::Element::kRRect_Type:
263 return "rrect";
264 case SkClipStack::Element::kPath_Type:
265 return "path";
266 }
267 return "unknown";
268}
269
270static const char* region_op(SkRegion::Op op) {
271 switch (op) {
272 case SkRegion::kDifference_Op:
273 return "difference";
274 case SkRegion::kIntersect_Op:
275 return "intersect";
276 case SkRegion::kUnion_Op:
277 return "union";
278 case SkRegion::kXOR_Op:
279 return "xor";
280 case SkRegion::kReverseDifference_Op:
281 return "reverse-difference";
282 case SkRegion::kReplace_Op:
283 return "replace";
284 }
285 return "unknown";
286}
287
288void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
289 lua_newtable(fL);
290 SkClipStack::B2TIter iter(stack);
291 const SkClipStack::Element* element;
292 int i = 0;
293 while (NULL != (element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000294 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000295 lua_rawseti(fL, -2, ++i);
296 }
297 CHECK_SETFIELD(key);
298}
299
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000300void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
301 lua_newtable(fL);
302 SkClipStack::Element::Type type = element.getType();
303 this->pushString(element_type(type), "type");
304 switch (type) {
305 case SkClipStack::Element::kEmpty_Type:
306 break;
307 case SkClipStack::Element::kRect_Type:
308 this->pushRect(element.getRect(), "rect");
309 break;
310 case SkClipStack::Element::kRRect_Type:
311 this->pushRRect(element.getRRect(), "rrect");
312 break;
313 case SkClipStack::Element::kPath_Type:
314 this->pushPath(element.getPath(), "path");
315 break;
316 }
317 this->pushString(region_op(element.getOp()), "op");
318 this->pushBool(element.isAA(), "aa");
319 CHECK_SETFIELD(key);
320}
321
322
reed@google.com74ce6f02013-05-22 15:13:18 +0000323///////////////////////////////////////////////////////////////////////////////
324///////////////////////////////////////////////////////////////////////////////
325
326static SkScalar lua2scalar(lua_State* L, int index) {
327 SkASSERT(lua_isnumber(L, index));
328 return SkLuaToScalar(lua_tonumber(L, index));
329}
330
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000331static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
332 if (lua_isnumber(L, index)) {
333 return SkLuaToScalar(lua_tonumber(L, index));
334 } else {
335 return defaultValue;
336 }
337}
338
reed@google.com74ce6f02013-05-22 15:13:18 +0000339static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
340 SkASSERT(lua_istable(L, index));
341 lua_pushstring(L, key);
342 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000343
reed@google.com74ce6f02013-05-22 15:13:18 +0000344 SkScalar value = lua2scalar(L, -1);
345 lua_pop(L, 1);
346 return value;
347}
348
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000349static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
350 SkASSERT(lua_istable(L, index));
351 lua_pushstring(L, key);
352 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000353
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000354 SkScalar value;
355 if (lua_isnil(L, -1)) {
356 value = def;
357 } else {
358 value = lua2scalar(L, -1);
359 }
360 lua_pop(L, 1);
361 return value;
362}
363
reed@google.com74ce6f02013-05-22 15:13:18 +0000364static U8CPU unit2byte(SkScalar x) {
365 if (x <= 0) {
366 return 0;
367 } else if (x >= 1) {
368 return 255;
369 } else {
370 return SkScalarRoundToInt(x * 255);
371 }
372}
373
374static SkColor lua2color(lua_State* L, int index) {
375 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
376 unit2byte(getfield_scalar(L, index, "r")),
377 unit2byte(getfield_scalar(L, index, "g")),
378 unit2byte(getfield_scalar(L, index, "b")));
379}
380
381static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000382 rect->set(getfield_scalar_default(L, index, "left", 0),
383 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000384 getfield_scalar(L, index, "right"),
385 getfield_scalar(L, index, "bottom"));
386 return rect;
387}
388
389static int lcanvas_drawColor(lua_State* L) {
390 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
391 return 0;
392}
393
394static int lcanvas_drawRect(lua_State* L) {
395 SkRect rect;
396 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
397 *get_obj<SkPaint>(L, 3));
398 return 0;
399}
400
401static int lcanvas_drawOval(lua_State* L) {
402 SkRect rect;
403 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
404 *get_obj<SkPaint>(L, 3));
405 return 0;
406}
407
408static int lcanvas_drawCircle(lua_State* L) {
409 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
410 lua2scalar(L, 3),
411 lua2scalar(L, 4),
412 *get_obj<SkPaint>(L, 5));
413 return 0;
414}
415
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000416static int lcanvas_drawImage(lua_State* L) {
417 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
418 SkImage* image = get_ref<SkImage>(L, 2);
419 if (NULL == image) {
420 return 0;
421 }
422 SkScalar x = lua2scalar(L, 3);
423 SkScalar y = lua2scalar(L, 4);
424
425 SkPaint paint;
426 const SkPaint* paintPtr = NULL;
427 if (lua_isnumber(L, 5)) {
428 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
429 paintPtr = &paint;
430 }
431 image->draw(canvas, x, y, paintPtr);
432 return 0;
433}
434
reed@google.comfd345872013-05-22 20:53:42 +0000435static int lcanvas_drawPath(lua_State* L) {
436 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
437 *get_obj<SkPaint>(L, 3));
438 return 0;
439}
440
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000441static int lcanvas_drawText(lua_State* L) {
442 if (lua_gettop(L) < 5) {
443 return 0;
444 }
445
446 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
447 size_t len;
448 const char* text = lua_tolstring(L, 2, &len);
449 get_ref<SkCanvas>(L, 1)->drawText(text, len,
450 lua2scalar(L, 3), lua2scalar(L, 4),
451 *get_obj<SkPaint>(L, 5));
452 }
453 return 0;
454}
455
reed@google.com74ce6f02013-05-22 15:13:18 +0000456static int lcanvas_getSaveCount(lua_State* L) {
457 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
458 return 1;
459}
460
461static int lcanvas_getTotalMatrix(lua_State* L) {
462 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
463 return 1;
464}
465
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000466static int lcanvas_getClipStack(lua_State* L) {
467 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
468 return 1;
469}
470
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000471int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
472#if SK_SUPPORT_GPU
473 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
474 SkISize layerSize = canvas->getTopLayerSize();
475 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
476 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
477 layerSize.fWidth, layerSize.fHeight);
478
479 GrReducedClip::ElementList elements;
480 GrReducedClip::InitialState initialState;
481 int32_t genID;
482 SkIRect resultBounds;
483
484 const SkClipStack& stack = *canvas->getClipStack();
485
486 GrReducedClip::ReduceClipStack(stack,
487 queryBounds,
488 &elements,
489 &genID,
490 &initialState,
491 &resultBounds,
492 NULL);
493
494 GrReducedClip::ElementList::Iter iter(elements);
495 int i = 0;
496 lua_newtable(L);
497 while(NULL != iter.get()) {
498 SkLua(L).pushClipStackElement(*iter.get());
499 iter.next();
500 lua_rawseti(L, -2, ++i);
501 }
502 // Currently this only returns the element list to lua, not the initial state or result bounds.
503 // It could return these as additional items on the lua stack.
504 return 1;
505#else
506 return 0;
507#endif
508}
509
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000510static int lcanvas_save(lua_State* L) {
511 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
512 return 1;
513}
514
515static int lcanvas_restore(lua_State* L) {
516 get_ref<SkCanvas>(L, 1)->restore();
517 return 0;
518}
519
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000520static int lcanvas_scale(lua_State* L) {
521 SkScalar sx = lua2scalar_def(L, 2, 1);
522 SkScalar sy = lua2scalar_def(L, 3, sx);
523 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
524 return 0;
525}
526
reed@google.com3597b732013-05-22 20:12:50 +0000527static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000528 SkScalar tx = lua2scalar_def(L, 2, 0);
529 SkScalar ty = lua2scalar_def(L, 3, 0);
530 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
531 return 0;
532}
533
534static int lcanvas_rotate(lua_State* L) {
535 SkScalar degrees = lua2scalar_def(L, 2, 0);
536 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000537 return 0;
538}
539
reed@google.com74ce6f02013-05-22 15:13:18 +0000540static int lcanvas_gc(lua_State* L) {
541 get_ref<SkCanvas>(L, 1)->unref();
542 return 0;
543}
544
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000545const struct luaL_Reg gSkCanvas_Methods[] = {
reed@google.com74ce6f02013-05-22 15:13:18 +0000546 { "drawColor", lcanvas_drawColor },
547 { "drawRect", lcanvas_drawRect },
548 { "drawOval", lcanvas_drawOval },
549 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000550 { "drawImage", lcanvas_drawImage },
reed@google.comfd345872013-05-22 20:53:42 +0000551 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000552 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000553 { "getSaveCount", lcanvas_getSaveCount },
554 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000555 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000556#if SK_SUPPORT_GPU
557 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
558#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000559 { "save", lcanvas_save },
560 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000561 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000562 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000563 { "rotate", lcanvas_rotate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000564 { "__gc", lcanvas_gc },
565 { NULL, NULL }
566};
567
568///////////////////////////////////////////////////////////////////////////////
569
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000570static int ldocument_beginPage(lua_State* L) {
571 const SkRect* contentPtr = NULL;
572 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
573 lua2scalar(L, 3),
574 contentPtr));
575 return 1;
576}
577
578static int ldocument_endPage(lua_State* L) {
579 get_ref<SkDocument>(L, 1)->endPage();
580 return 0;
581}
582
583static int ldocument_close(lua_State* L) {
584 get_ref<SkDocument>(L, 1)->close();
585 return 0;
586}
587
588static int ldocument_gc(lua_State* L) {
589 get_ref<SkDocument>(L, 1)->unref();
590 return 0;
591}
592
593static const struct luaL_Reg gSkDocument_Methods[] = {
594 { "beginPage", ldocument_beginPage },
595 { "endPage", ldocument_endPage },
596 { "close", ldocument_close },
597 { "__gc", ldocument_gc },
598 { NULL, NULL }
599};
600
601///////////////////////////////////////////////////////////////////////////////
602
reed@google.com74ce6f02013-05-22 15:13:18 +0000603static int lpaint_isAntiAlias(lua_State* L) {
604 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
605 return 1;
606}
607
608static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000609 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000610 return 0;
611}
612
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000613static int lpaint_isDither(lua_State* L) {
614 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
615 return 1;
616}
617
618static int lpaint_isUnderlineText(lua_State* L) {
619 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
620 return 1;
621}
622
623static int lpaint_isStrikeThruText(lua_State* L) {
624 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
625 return 1;
626}
627
628static int lpaint_isFakeBoldText(lua_State* L) {
629 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
630 return 1;
631}
632
633static int lpaint_isLinearText(lua_State* L) {
634 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
635 return 1;
636}
637
638static int lpaint_isSubpixelText(lua_State* L) {
639 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
640 return 1;
641}
642
643static int lpaint_isDevKernText(lua_State* L) {
644 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
645 return 1;
646}
647
648static int lpaint_isLCDRenderText(lua_State* L) {
649 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
650 return 1;
651}
652
653static int lpaint_isEmbeddedBitmapText(lua_State* L) {
654 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
655 return 1;
656}
657
658static int lpaint_isAutohinted(lua_State* L) {
659 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
660 return 1;
661}
662
663static int lpaint_isVerticalText(lua_State* L) {
664 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
665 return 1;
666}
667
reed@google.com74ce6f02013-05-22 15:13:18 +0000668static int lpaint_getColor(lua_State* L) {
669 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
670 return 1;
671}
672
673static int lpaint_setColor(lua_State* L) {
674 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
675 return 0;
676}
677
reed@google.come3823fd2013-05-30 18:55:14 +0000678static int lpaint_getTextSize(lua_State* L) {
679 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
680 return 1;
681}
682
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000683static int lpaint_getTextScaleX(lua_State* L) {
684 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
685 return 1;
686}
687
688static int lpaint_getTextSkewX(lua_State* L) {
689 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
690 return 1;
691}
692
reed@google.come3823fd2013-05-30 18:55:14 +0000693static int lpaint_setTextSize(lua_State* L) {
694 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
695 return 0;
696}
697
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000698static int lpaint_getTypeface(lua_State* L) {
699 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
700 return 1;
701}
702
703static int lpaint_setTypeface(lua_State* L) {
704 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
705 return 0;
706}
707
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000708static int lpaint_getHinting(lua_State* L) {
709 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
710 return 1;
711}
712
reed@google.come3823fd2013-05-30 18:55:14 +0000713static int lpaint_getFontID(lua_State* L) {
714 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
715 SkLua(L).pushU32(SkTypeface::UniqueID(face));
716 return 1;
717}
718
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000719static const struct {
720 const char* fLabel;
721 SkPaint::Align fAlign;
722} gAlignRec[] = {
723 { "left", SkPaint::kLeft_Align },
724 { "center", SkPaint::kCenter_Align },
725 { "right", SkPaint::kRight_Align },
726};
727
728static int lpaint_getTextAlign(lua_State* L) {
729 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
730 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
731 if (gAlignRec[i].fAlign == align) {
732 lua_pushstring(L, gAlignRec[i].fLabel);
733 return 1;
734 }
735 }
736 return 0;
737}
738
739static int lpaint_setTextAlign(lua_State* L) {
740 if (lua_isstring(L, 2)) {
741 size_t len;
742 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000743
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000744 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
745 if (!strcmp(gAlignRec[i].fLabel, label)) {
746 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
747 break;
748 }
749 }
750 }
751 return 0;
752}
753
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000754static int lpaint_getStroke(lua_State* L) {
755 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
756 return 1;
757}
758
759static int lpaint_setStroke(lua_State* L) {
760 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000761
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000762 if (lua_toboolean(L, 2)) {
763 style = SkPaint::kStroke_Style;
764 } else {
765 style = SkPaint::kFill_Style;
766 }
767 get_obj<SkPaint>(L, 1)->setStyle(style);
768 return 0;
769}
770
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000771static int lpaint_getStrokeCap(lua_State* L) {
772 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
773 return 1;
774}
775
776static int lpaint_getStrokeJoin(lua_State* L) {
777 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
778 return 1;
779}
780
781static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000782 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000783 return 1;
784}
785
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000786static int lpaint_getStrokeWidth(lua_State* L) {
787 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
788 return 1;
789}
790
791static int lpaint_setStrokeWidth(lua_State* L) {
792 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
793 return 0;
794}
795
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000796static int lpaint_getStrokeMiter(lua_State* L) {
797 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
798 return 1;
799}
800
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000801static int lpaint_measureText(lua_State* L) {
802 if (lua_isstring(L, 2)) {
803 size_t len;
804 const char* text = lua_tolstring(L, 2, &len);
805 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
806 return 1;
807 }
808 return 0;
809}
810
811struct FontMetrics {
812 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
813 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
814 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
815 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
816 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
817 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
818 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
819 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
820 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
821};
822
823static int lpaint_getFontMetrics(lua_State* L) {
824 SkPaint::FontMetrics fm;
825 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000826
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000827 lua_newtable(L);
828 setfield_scalar(L, "top", fm.fTop);
829 setfield_scalar(L, "ascent", fm.fAscent);
830 setfield_scalar(L, "descent", fm.fDescent);
831 setfield_scalar(L, "bottom", fm.fBottom);
832 setfield_scalar(L, "leading", fm.fLeading);
833 SkLua(L).pushScalar(height);
834 return 2;
835}
836
reed@google.com29563872013-07-10 21:23:49 +0000837static int lpaint_getEffects(lua_State* L) {
838 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000839
reed@google.com29563872013-07-10 21:23:49 +0000840 lua_newtable(L);
841 setfield_bool_if(L, "looper", !!paint->getLooper());
842 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
843 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
844 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
845 setfield_bool_if(L, "shader", !!paint->getShader());
846 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
847 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
848 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
849 return 1;
850}
851
reed@google.com5fdc9832013-07-24 15:47:52 +0000852static int lpaint_getShader(lua_State* L) {
853 const SkPaint* paint = get_obj<SkPaint>(L, 1);
854 SkShader* shader = paint->getShader();
855 if (shader) {
856 push_ref(L, shader);
857 return 1;
858 }
859 return 0;
860}
861
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000862static int lpaint_getPathEffect(lua_State* L) {
863 const SkPaint* paint = get_obj<SkPaint>(L, 1);
864 SkPathEffect* pe = paint->getPathEffect();
865 if (pe) {
866 push_ref(L, pe);
867 return 1;
868 }
869 return 0;
870}
871
reed@google.com74ce6f02013-05-22 15:13:18 +0000872static int lpaint_gc(lua_State* L) {
873 get_obj<SkPaint>(L, 1)->~SkPaint();
874 return 0;
875}
876
877static const struct luaL_Reg gSkPaint_Methods[] = {
878 { "isAntiAlias", lpaint_isAntiAlias },
879 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000880 { "isDither", lpaint_isDither },
881 { "isUnderlineText", lpaint_isUnderlineText },
882 { "isStrikeThruText", lpaint_isStrikeThruText },
883 { "isFakeBoldText", lpaint_isFakeBoldText },
884 { "isLinearText", lpaint_isLinearText },
885 { "isSubpixelText", lpaint_isSubpixelText },
886 { "isDevKernText", lpaint_isDevKernText },
887 { "isLCDRenderText", lpaint_isLCDRenderText },
888 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
889 { "isAutohinted", lpaint_isAutohinted },
890 { "isVerticalText", lpaint_isVerticalText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000891 { "getColor", lpaint_getColor },
892 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000893 { "getTextSize", lpaint_getTextSize },
894 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000895 { "getTextScaleX", lpaint_getTextScaleX },
896 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000897 { "getTypeface", lpaint_getTypeface },
898 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000899 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +0000900 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000901 { "getTextAlign", lpaint_getTextAlign },
902 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000903 { "getStroke", lpaint_getStroke },
904 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000905 { "getStrokeCap", lpaint_getStrokeCap },
906 { "getStrokeJoin", lpaint_getStrokeJoin },
907 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000908 { "getStrokeWidth", lpaint_getStrokeWidth },
909 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000910 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000911 { "measureText", lpaint_measureText },
912 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +0000913 { "getEffects", lpaint_getEffects },
reed@google.com5fdc9832013-07-24 15:47:52 +0000914 { "getShader", lpaint_getShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000915 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +0000916 { "__gc", lpaint_gc },
917 { NULL, NULL }
918};
919
920///////////////////////////////////////////////////////////////////////////////
921
reed@google.com5fdc9832013-07-24 15:47:52 +0000922static const char* mode2string(SkShader::TileMode mode) {
923 static const char* gNames[] = { "clamp", "repeat", "mirror" };
924 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
925 return gNames[mode];
926}
927
928static const char* gradtype2string(SkShader::GradientType t) {
929 static const char* gNames[] = {
930 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
931 };
932 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
933 return gNames[t];
934}
935
936static int lshader_isOpaque(lua_State* L) {
937 SkShader* shader = get_ref<SkShader>(L, 1);
938 return shader && shader->isOpaque();
939}
940
941static int lshader_asABitmap(lua_State* L) {
942 SkShader* shader = get_ref<SkShader>(L, 1);
943 if (shader) {
944 SkBitmap bm;
945 SkMatrix matrix;
946 SkShader::TileMode modes[2];
947 switch (shader->asABitmap(&bm, &matrix, modes)) {
948 case SkShader::kDefault_BitmapType:
949 lua_newtable(L);
950 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
951 setfield_number(L, "width", bm.width());
952 setfield_number(L, "height", bm.height());
953 setfield_string(L, "tileX", mode2string(modes[0]));
954 setfield_string(L, "tileY", mode2string(modes[1]));
955 return 1;
956 default:
957 break;
958 }
959 }
960 return 0;
961}
962
963static int lshader_asAGradient(lua_State* L) {
964 SkShader* shader = get_ref<SkShader>(L, 1);
965 if (shader) {
966 SkShader::GradientInfo info;
967 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000968
969 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000970 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000971
972 info.fColorCount = 3;
973 info.fColors = &colors[0];
974 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000975
reed@google.com5fdc9832013-07-24 15:47:52 +0000976 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000977
reed@google.com5fdc9832013-07-24 15:47:52 +0000978 if (SkShader::kNone_GradientType != t) {
979 lua_newtable(L);
980 setfield_string(L, "type", gradtype2string(t));
981 setfield_number(L, "colorCount", info.fColorCount);
982 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000983
984 if (info.fColorCount == 3){
985 setfield_number(L, "midPos", pos[1]);
986 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000987
reed@google.com5fdc9832013-07-24 15:47:52 +0000988 return 1;
989 }
990 }
991 return 0;
992}
993
994static int lshader_gc(lua_State* L) {
995 get_ref<SkShader>(L, 1)->unref();
996 return 0;
997}
998
999static const struct luaL_Reg gSkShader_Methods[] = {
1000 { "isOpaque", lshader_isOpaque },
1001 { "asABitmap", lshader_asABitmap },
1002 { "asAGradient", lshader_asAGradient },
1003 { "__gc", lshader_gc },
1004 { NULL, NULL }
1005};
1006
1007///////////////////////////////////////////////////////////////////////////////
1008
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001009static int lpatheffect_gc(lua_State* L) {
1010 get_ref<SkPathEffect>(L, 1)->unref();
1011 return 0;
1012}
1013
1014static const struct luaL_Reg gSkPathEffect_Methods[] = {
1015 { "__gc", lpatheffect_gc },
1016 { NULL, NULL }
1017};
1018
1019///////////////////////////////////////////////////////////////////////////////
1020
humper@google.com2815c192013-07-10 22:42:30 +00001021static int lmatrix_getType(lua_State* L) {
1022 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001023
humper@google.com2815c192013-07-10 22:42:30 +00001024 lua_newtable(L);
1025 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1026 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1027 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1028 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1029 return 1;
1030}
1031
humper@google.com0f48ee02013-07-26 15:23:43 +00001032static int lmatrix_getScaleX(lua_State* L) {
1033 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1034 return 1;
1035}
1036
1037static int lmatrix_getScaleY(lua_State* L) {
1038 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1039 return 1;
1040}
1041
1042static int lmatrix_getTranslateX(lua_State* L) {
1043 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1044 return 1;
1045}
1046
1047static int lmatrix_getTranslateY(lua_State* L) {
1048 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1049 return 1;
1050}
1051
humper@google.com2815c192013-07-10 22:42:30 +00001052static const struct luaL_Reg gSkMatrix_Methods[] = {
1053 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001054 { "getScaleX", lmatrix_getScaleX },
1055 { "getScaleY", lmatrix_getScaleY },
1056 { "getTranslateX", lmatrix_getTranslateX },
1057 { "getTranslateY", lmatrix_getTranslateY },
humper@google.com2815c192013-07-10 22:42:30 +00001058 { NULL, NULL }
1059};
1060
1061///////////////////////////////////////////////////////////////////////////////
1062
reed@google.com74ce6f02013-05-22 15:13:18 +00001063static int lpath_getBounds(lua_State* L) {
1064 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1065 return 1;
1066}
1067
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001068static const char* fill_type_to_str(SkPath::FillType fill) {
1069 switch (fill) {
1070 case SkPath::kEvenOdd_FillType:
1071 return "even-odd";
1072 case SkPath::kWinding_FillType:
1073 return "winding";
1074 case SkPath::kInverseEvenOdd_FillType:
1075 return "inverse-even-odd";
1076 case SkPath::kInverseWinding_FillType:
1077 return "inverse-winding";
1078 }
1079 return "unknown";
1080}
1081
1082static int lpath_getFillType(lua_State* L) {
1083 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1084 SkLua(L).pushString(fill_type_to_str(fill));
1085 return 1;
1086}
1087
1088static SkString segment_masks_to_str(uint32_t segmentMasks) {
1089 SkString result;
1090 bool first = true;
1091 if (SkPath::kLine_SegmentMask & segmentMasks) {
1092 result.append("line");
1093 first = false;
1094 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1095 }
1096 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1097 if (!first) {
1098 result.append(" ");
1099 }
1100 result.append("quad");
1101 first = false;
1102 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1103 }
1104 if (SkPath::kConic_SegmentMask & segmentMasks) {
1105 if (!first) {
1106 result.append(" ");
1107 }
1108 result.append("conic");
1109 first = false;
1110 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1111 }
1112 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1113 if (!first) {
1114 result.append(" ");
1115 }
1116 result.append("cubic");
1117 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1118 }
1119 SkASSERT(0 == segmentMasks);
1120 return result;
1121}
1122
1123static int lpath_getSegementTypes(lua_State* L) {
1124 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1125 SkLua(L).pushString(segment_masks_to_str(segMasks));
1126 return 1;
1127}
1128
1129static int lpath_isConvex(lua_State* L) {
1130 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1131 SkLua(L).pushBool(isConvex);
1132 return 1;
1133}
1134
reed@google.com74ce6f02013-05-22 15:13:18 +00001135static int lpath_isEmpty(lua_State* L) {
1136 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1137 return 1;
1138}
1139
1140static int lpath_isRect(lua_State* L) {
1141 SkRect r;
1142 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1143 int ret_count = 1;
1144 lua_pushboolean(L, pred);
1145 if (pred) {
1146 SkLua(L).pushRect(r);
1147 ret_count += 1;
1148 }
1149 return ret_count;
1150}
1151
1152static const char* dir2string(SkPath::Direction dir) {
1153 static const char* gStr[] = {
1154 "unknown", "cw", "ccw"
1155 };
1156 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1157 return gStr[dir];
1158}
1159
1160static int lpath_isNestedRects(lua_State* L) {
1161 SkRect rects[2];
1162 SkPath::Direction dirs[2];
1163 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1164 int ret_count = 1;
1165 lua_pushboolean(L, pred);
1166 if (pred) {
1167 SkLua lua(L);
1168 lua.pushRect(rects[0]);
1169 lua.pushRect(rects[1]);
1170 lua_pushstring(L, dir2string(dirs[0]));
1171 lua_pushstring(L, dir2string(dirs[0]));
1172 ret_count += 4;
1173 }
1174 return ret_count;
1175}
1176
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001177static int lpath_countPoints(lua_State* L) {
1178 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1179 return 1;
1180}
1181
reed@google.com74ce6f02013-05-22 15:13:18 +00001182static int lpath_reset(lua_State* L) {
1183 get_obj<SkPath>(L, 1)->reset();
1184 return 0;
1185}
1186
1187static int lpath_moveTo(lua_State* L) {
1188 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1189 return 0;
1190}
1191
1192static int lpath_lineTo(lua_State* L) {
1193 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1194 return 0;
1195}
1196
1197static int lpath_quadTo(lua_State* L) {
1198 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1199 lua2scalar(L, 4), lua2scalar(L, 5));
1200 return 0;
1201}
1202
1203static int lpath_cubicTo(lua_State* L) {
1204 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1205 lua2scalar(L, 4), lua2scalar(L, 5),
1206 lua2scalar(L, 6), lua2scalar(L, 7));
1207 return 0;
1208}
1209
1210static int lpath_close(lua_State* L) {
1211 get_obj<SkPath>(L, 1)->close();
1212 return 0;
1213}
1214
1215static int lpath_gc(lua_State* L) {
1216 get_obj<SkPath>(L, 1)->~SkPath();
1217 return 0;
1218}
1219
1220static const struct luaL_Reg gSkPath_Methods[] = {
1221 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001222 { "getFillType", lpath_getFillType },
1223 { "getSegmentTypes", lpath_getSegementTypes },
1224 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001225 { "isEmpty", lpath_isEmpty },
1226 { "isRect", lpath_isRect },
1227 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001228 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001229 { "reset", lpath_reset },
1230 { "moveTo", lpath_moveTo },
1231 { "lineTo", lpath_lineTo },
1232 { "quadTo", lpath_quadTo },
1233 { "cubicTo", lpath_cubicTo },
1234 { "close", lpath_close },
1235 { "__gc", lpath_gc },
1236 { NULL, NULL }
1237};
1238
1239///////////////////////////////////////////////////////////////////////////////
1240
1241static const char* rrect_type(const SkRRect& rr) {
1242 switch (rr.getType()) {
1243 case SkRRect::kUnknown_Type: return "unknown";
1244 case SkRRect::kEmpty_Type: return "empty";
1245 case SkRRect::kRect_Type: return "rect";
1246 case SkRRect::kOval_Type: return "oval";
1247 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001248 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001249 case SkRRect::kComplex_Type: return "complex";
1250 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001251 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001252 return "";
1253}
1254
1255static int lrrect_rect(lua_State* L) {
1256 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1257 return 1;
1258}
1259
1260static int lrrect_type(lua_State* L) {
1261 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1262 return 1;
1263}
1264
1265static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001266 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001267 SkVector v;
1268 if (corner < 0 || corner > 3) {
1269 SkDebugf("bad corner index %d", corner);
1270 v.set(0, 0);
1271 } else {
1272 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1273 }
1274 lua_pushnumber(L, v.fX);
1275 lua_pushnumber(L, v.fY);
1276 return 2;
1277}
1278
1279static int lrrect_gc(lua_State* L) {
1280 get_obj<SkRRect>(L, 1)->~SkRRect();
1281 return 0;
1282}
1283
1284static const struct luaL_Reg gSkRRect_Methods[] = {
1285 { "rect", lrrect_rect },
1286 { "type", lrrect_type },
1287 { "radii", lrrect_radii },
1288 { "__gc", lrrect_gc },
1289 { NULL, NULL }
1290};
1291
1292///////////////////////////////////////////////////////////////////////////////
1293
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001294static int limage_width(lua_State* L) {
1295 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1296 return 1;
1297}
1298
1299static int limage_height(lua_State* L) {
1300 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1301 return 1;
1302}
1303
1304static int limage_gc(lua_State* L) {
1305 get_ref<SkImage>(L, 1)->unref();
1306 return 0;
1307}
1308
1309static const struct luaL_Reg gSkImage_Methods[] = {
1310 { "width", limage_width },
1311 { "height", limage_height },
1312 { "__gc", limage_gc },
1313 { NULL, NULL }
1314};
1315
1316///////////////////////////////////////////////////////////////////////////////
1317
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001318static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001319 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001320 return 0;
1321}
1322
1323static const struct luaL_Reg gSkTypeface_Methods[] = {
1324 { "__gc", ltypeface_gc },
1325 { NULL, NULL }
1326};
1327
1328///////////////////////////////////////////////////////////////////////////////
1329
reed@google.com74ce6f02013-05-22 15:13:18 +00001330class AutoCallLua {
1331public:
1332 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1333 lua_getglobal(L, func);
1334 if (!lua_isfunction(L, -1)) {
1335 int t = lua_type(L, -1);
1336 SkDebugf("--- expected function %d\n", t);
1337 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001338
reed@google.com74ce6f02013-05-22 15:13:18 +00001339 lua_newtable(L);
1340 setfield_string(L, "verb", verb);
1341 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001342
reed@google.com74ce6f02013-05-22 15:13:18 +00001343 ~AutoCallLua() {
1344 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1345 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1346 }
1347 lua_settop(fL, -1);
1348 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001349
reed@google.com74ce6f02013-05-22 15:13:18 +00001350private:
1351 lua_State* fL;
1352};
1353
1354#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1355
1356///////////////////////////////////////////////////////////////////////////////
1357
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001358static int lsk_newDocumentPDF(lua_State* L) {
1359 const char* file = NULL;
1360 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1361 file = lua_tolstring(L, 1, NULL);
1362 }
1363
1364 SkDocument* doc = SkDocument::CreatePDF(file);
1365 if (NULL == doc) {
1366 // do I need to push a nil on the stack and return 1?
1367 return 0;
1368 } else {
1369 push_ref(L, doc);
1370 doc->unref();
1371 return 1;
1372 }
1373}
1374
reed@google.com3597b732013-05-22 20:12:50 +00001375static int lsk_newPaint(lua_State* L) {
1376 push_new<SkPaint>(L);
1377 return 1;
1378}
1379
1380static int lsk_newPath(lua_State* L) {
1381 push_new<SkPath>(L);
1382 return 1;
1383}
1384
1385static int lsk_newRRect(lua_State* L) {
1386 SkRRect* rr = push_new<SkRRect>(L);
1387 rr->setEmpty();
1388 return 1;
1389}
1390
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001391static int lsk_newTypeface(lua_State* L) {
1392 const char* name = NULL;
1393 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001394
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001395 int count = lua_gettop(L);
1396 if (count > 0 && lua_isstring(L, 1)) {
1397 name = lua_tolstring(L, 1, NULL);
1398 if (count > 1 && lua_isnumber(L, 2)) {
1399 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1400 }
1401 }
1402
1403 SkTypeface* face = SkTypeface::CreateFromName(name,
1404 (SkTypeface::Style)style);
1405// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1406 if (NULL == face) {
1407 face = SkTypeface::RefDefault();
1408 }
1409 push_ref(L, face);
1410 face->unref();
1411 return 1;
1412}
reed@google.com3597b732013-05-22 20:12:50 +00001413
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001414static int lsk_loadImage(lua_State* L) {
1415 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1416 const char* name = lua_tolstring(L, 1, NULL);
1417 SkAutoDataUnref data(SkData::NewFromFileName(name));
1418 if (data.get()) {
1419 SkImage* image = SkImage::NewEncodedData(data.get());
1420 if (image) {
1421 push_ref(L, image);
1422 image->unref();
1423 return 1;
1424 }
1425 }
1426 }
1427 return 0;
1428}
1429
reed@google.com3597b732013-05-22 20:12:50 +00001430static void register_Sk(lua_State* L) {
1431 lua_newtable(L);
1432 lua_pushvalue(L, -1);
1433 lua_setglobal(L, "Sk");
1434 // the Sk table is still on top
1435
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001436 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001437 setfield_function(L, "loadImage", lsk_loadImage);
reed@google.com3597b732013-05-22 20:12:50 +00001438 setfield_function(L, "newPaint", lsk_newPaint);
1439 setfield_function(L, "newPath", lsk_newPath);
1440 setfield_function(L, "newRRect", lsk_newRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001441 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001442 lua_pop(L, 1); // pop off the Sk table
1443}
1444
reed@google.com74ce6f02013-05-22 15:13:18 +00001445#define REG_CLASS(L, C) \
1446 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001447 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001448 lua_pushvalue(L, -1); \
1449 lua_setfield(L, -2, "__index"); \
1450 luaL_setfuncs(L, g##C##_Methods, 0); \
1451 lua_pop(L, 1); /* pop off the meta-table */ \
1452 } while (0)
1453
1454void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001455 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001456 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001457 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001458 REG_CLASS(L, SkImage);
reed@google.com74ce6f02013-05-22 15:13:18 +00001459 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001460 REG_CLASS(L, SkPath);
1461 REG_CLASS(L, SkPathEffect);
reed@google.com74ce6f02013-05-22 15:13:18 +00001462 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001463 REG_CLASS(L, SkShader);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001464 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001465 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001466}
zachr@google.com28c27c82013-06-20 17:15:05 +00001467
reed@google.com7bce9982013-06-20 17:40:21 +00001468extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001469extern "C" int luaopen_skia(lua_State* L) {
1470 SkLua::Load(L);
1471 return 0;
1472}