blob: 44f2211b8f4f37253678a44d120fb0f9885d32df [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"
reed@google.come3823fd2013-05-30 18:55:14 +000024#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000025
26extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000027 #include "lua.h"
28 #include "lualib.h"
29 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000030}
31
reed@google.comfd345872013-05-22 20:53:42 +000032// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000033template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000034#define DEF_MTNAME(T) \
35 template <> const char* get_mtname<T>() { \
36 return #T "_LuaMetaTableName"; \
37 }
38
39DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000040DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000041DEF_MTNAME(SkImage)
reed@google.comfd345872013-05-22 20:53:42 +000042DEF_MTNAME(SkMatrix)
43DEF_MTNAME(SkRRect)
44DEF_MTNAME(SkPath)
45DEF_MTNAME(SkPaint)
reed@google.com5fdc9832013-07-24 15:47:52 +000046DEF_MTNAME(SkShader)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000047DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000048
reed@google.com3597b732013-05-22 20:12:50 +000049template <typename T> T* push_new(lua_State* L) {
50 T* addr = (T*)lua_newuserdata(L, sizeof(T));
51 new (addr) T;
52 luaL_getmetatable(L, get_mtname<T>());
53 lua_setmetatable(L, -2);
54 return addr;
55}
reed@google.com74ce6f02013-05-22 15:13:18 +000056
57template <typename T> void push_obj(lua_State* L, const T& obj) {
58 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000059 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000060 lua_setmetatable(L, -2);
61}
62
63template <typename T> void push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000064 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000065 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000066 lua_setmetatable(L, -2);
67}
68
69template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000070 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000071}
72
73template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000074 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000075}
76
reed@google.com88c9ec92013-05-22 15:43:21 +000077static bool lua2bool(lua_State* L, int index) {
78 return !!lua_toboolean(L, index);
79}
80
reed@google.com74ce6f02013-05-22 15:13:18 +000081///////////////////////////////////////////////////////////////////////////////
82
reed@google.com3597b732013-05-22 20:12:50 +000083SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
84 fL = luaL_newstate();
85 luaL_openlibs(fL);
86 SkLua::Load(fL);
87}
88
89SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
90
91SkLua::~SkLua() {
92 if (fWeOwnL) {
93 if (fTermCode.size() > 0) {
94 lua_getglobal(fL, fTermCode.c_str());
95 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
96 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
97 }
98 }
99 lua_close(fL);
100 }
101}
102
103bool SkLua::runCode(const char code[]) {
104 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
105 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000106 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000107 return false;
108 }
109 return true;
110}
111
112bool SkLua::runCode(const void* code, size_t size) {
113 SkString str((const char*)code, size);
114 return this->runCode(str.c_str());
115}
116
117///////////////////////////////////////////////////////////////////////////////
118
119#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
120
reed@google.com29563872013-07-10 21:23:49 +0000121static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
122 if (pred) {
123 lua_pushboolean(L, true);
124 lua_setfield(L, -2, key);
125 }
126}
127
reed@google.com74ce6f02013-05-22 15:13:18 +0000128static void setfield_string(lua_State* L, const char key[], const char value[]) {
129 lua_pushstring(L, value);
130 lua_setfield(L, -2, key);
131}
132
133static void setfield_number(lua_State* L, const char key[], double value) {
134 lua_pushnumber(L, value);
135 lua_setfield(L, -2, key);
136}
137
humper@google.com2815c192013-07-10 22:42:30 +0000138static void setfield_boolean(lua_State* L, const char key[], bool value) {
139 lua_pushboolean(L, value);
140 lua_setfield(L, -2, key);
141}
142
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000143static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
144 setfield_number(L, key, SkScalarToLua(value));
145}
146
reed@google.com3597b732013-05-22 20:12:50 +0000147static void setfield_function(lua_State* L,
148 const char key[], lua_CFunction value) {
149 lua_pushcfunction(L, value);
150 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000151}
152
reed@google.come3823fd2013-05-30 18:55:14 +0000153static void setarray_number(lua_State* L, int index, double value) {
154 lua_pushnumber(L, value);
155 lua_rawseti(L, -2, index);
156}
157
reed@google.com74ce6f02013-05-22 15:13:18 +0000158void SkLua::pushBool(bool value, const char key[]) {
159 lua_pushboolean(fL, value);
160 CHECK_SETFIELD(key);
161}
162
163void SkLua::pushString(const char str[], const char key[]) {
164 lua_pushstring(fL, str);
165 CHECK_SETFIELD(key);
166}
167
reed@google.come3823fd2013-05-30 18:55:14 +0000168void SkLua::pushString(const char str[], size_t length, const char key[]) {
169 // TODO: how to do this w/o making a copy?
170 SkString s(str, length);
171 lua_pushstring(fL, s.c_str());
172 CHECK_SETFIELD(key);
173}
174
reed@google.com74ce6f02013-05-22 15:13:18 +0000175void SkLua::pushString(const SkString& str, const char key[]) {
176 lua_pushstring(fL, str.c_str());
177 CHECK_SETFIELD(key);
178}
179
180void SkLua::pushColor(SkColor color, const char key[]) {
181 lua_newtable(fL);
182 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
183 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
184 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
185 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
186 CHECK_SETFIELD(key);
187}
188
reed@google.come3823fd2013-05-30 18:55:14 +0000189void SkLua::pushU32(uint32_t value, const char key[]) {
190 lua_pushnumber(fL, (double)value);
191 CHECK_SETFIELD(key);
192}
193
reed@google.com74ce6f02013-05-22 15:13:18 +0000194void SkLua::pushScalar(SkScalar value, const char key[]) {
195 lua_pushnumber(fL, SkScalarToLua(value));
196 CHECK_SETFIELD(key);
197}
198
reed@google.come3823fd2013-05-30 18:55:14 +0000199void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
200 lua_newtable(fL);
201 for (int i = 0; i < count; ++i) {
202 // make it base-1 to match lua convention
203 setarray_number(fL, i + 1, (double)array[i]);
204 }
205 CHECK_SETFIELD(key);
206}
207
reed@google.com74ce6f02013-05-22 15:13:18 +0000208void SkLua::pushRect(const SkRect& r, const char key[]) {
209 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000210 setfield_scalar(fL, "left", r.fLeft);
211 setfield_scalar(fL, "top", r.fTop);
212 setfield_scalar(fL, "right", r.fRight);
213 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000214 CHECK_SETFIELD(key);
215}
216
217void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
218 push_obj(fL, rr);
219 CHECK_SETFIELD(key);
220}
221
222void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
223 push_obj(fL, matrix);
224 CHECK_SETFIELD(key);
225}
226
227void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
228 push_obj(fL, paint);
229 CHECK_SETFIELD(key);
230}
231
232void SkLua::pushPath(const SkPath& path, const char key[]) {
233 push_obj(fL, path);
234 CHECK_SETFIELD(key);
235}
236
237void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
238 push_ref(fL, canvas);
239 CHECK_SETFIELD(key);
240}
241
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000242static const char* element_type(SkClipStack::Element::Type type) {
243 switch (type) {
244 case SkClipStack::Element::kEmpty_Type:
245 return "empty";
246 case SkClipStack::Element::kRect_Type:
247 return "rect";
248 case SkClipStack::Element::kRRect_Type:
249 return "rrect";
250 case SkClipStack::Element::kPath_Type:
251 return "path";
252 }
253 return "unknown";
254}
255
256static const char* region_op(SkRegion::Op op) {
257 switch (op) {
258 case SkRegion::kDifference_Op:
259 return "difference";
260 case SkRegion::kIntersect_Op:
261 return "intersect";
262 case SkRegion::kUnion_Op:
263 return "union";
264 case SkRegion::kXOR_Op:
265 return "xor";
266 case SkRegion::kReverseDifference_Op:
267 return "reverse-difference";
268 case SkRegion::kReplace_Op:
269 return "replace";
270 }
271 return "unknown";
272}
273
274void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
275 lua_newtable(fL);
276 SkClipStack::B2TIter iter(stack);
277 const SkClipStack::Element* element;
278 int i = 0;
279 while (NULL != (element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000280 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000281 lua_rawseti(fL, -2, ++i);
282 }
283 CHECK_SETFIELD(key);
284}
285
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000286void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
287 lua_newtable(fL);
288 SkClipStack::Element::Type type = element.getType();
289 this->pushString(element_type(type), "type");
290 switch (type) {
291 case SkClipStack::Element::kEmpty_Type:
292 break;
293 case SkClipStack::Element::kRect_Type:
294 this->pushRect(element.getRect(), "rect");
295 break;
296 case SkClipStack::Element::kRRect_Type:
297 this->pushRRect(element.getRRect(), "rrect");
298 break;
299 case SkClipStack::Element::kPath_Type:
300 this->pushPath(element.getPath(), "path");
301 break;
302 }
303 this->pushString(region_op(element.getOp()), "op");
304 this->pushBool(element.isAA(), "aa");
305 CHECK_SETFIELD(key);
306}
307
308
reed@google.com74ce6f02013-05-22 15:13:18 +0000309///////////////////////////////////////////////////////////////////////////////
310///////////////////////////////////////////////////////////////////////////////
311
312static SkScalar lua2scalar(lua_State* L, int index) {
313 SkASSERT(lua_isnumber(L, index));
314 return SkLuaToScalar(lua_tonumber(L, index));
315}
316
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000317static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
318 if (lua_isnumber(L, index)) {
319 return SkLuaToScalar(lua_tonumber(L, index));
320 } else {
321 return defaultValue;
322 }
323}
324
reed@google.com74ce6f02013-05-22 15:13:18 +0000325static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
326 SkASSERT(lua_istable(L, index));
327 lua_pushstring(L, key);
328 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000329
reed@google.com74ce6f02013-05-22 15:13:18 +0000330 SkScalar value = lua2scalar(L, -1);
331 lua_pop(L, 1);
332 return value;
333}
334
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000335static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
336 SkASSERT(lua_istable(L, index));
337 lua_pushstring(L, key);
338 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000339
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000340 SkScalar value;
341 if (lua_isnil(L, -1)) {
342 value = def;
343 } else {
344 value = lua2scalar(L, -1);
345 }
346 lua_pop(L, 1);
347 return value;
348}
349
reed@google.com74ce6f02013-05-22 15:13:18 +0000350static U8CPU unit2byte(SkScalar x) {
351 if (x <= 0) {
352 return 0;
353 } else if (x >= 1) {
354 return 255;
355 } else {
356 return SkScalarRoundToInt(x * 255);
357 }
358}
359
360static SkColor lua2color(lua_State* L, int index) {
361 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
362 unit2byte(getfield_scalar(L, index, "r")),
363 unit2byte(getfield_scalar(L, index, "g")),
364 unit2byte(getfield_scalar(L, index, "b")));
365}
366
367static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000368 rect->set(getfield_scalar_default(L, index, "left", 0),
369 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000370 getfield_scalar(L, index, "right"),
371 getfield_scalar(L, index, "bottom"));
372 return rect;
373}
374
375static int lcanvas_drawColor(lua_State* L) {
376 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
377 return 0;
378}
379
380static int lcanvas_drawRect(lua_State* L) {
381 SkRect rect;
382 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
383 *get_obj<SkPaint>(L, 3));
384 return 0;
385}
386
387static int lcanvas_drawOval(lua_State* L) {
388 SkRect rect;
389 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
390 *get_obj<SkPaint>(L, 3));
391 return 0;
392}
393
394static int lcanvas_drawCircle(lua_State* L) {
395 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
396 lua2scalar(L, 3),
397 lua2scalar(L, 4),
398 *get_obj<SkPaint>(L, 5));
399 return 0;
400}
401
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000402static int lcanvas_drawImage(lua_State* L) {
403 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
404 SkImage* image = get_ref<SkImage>(L, 2);
405 if (NULL == image) {
406 return 0;
407 }
408 SkScalar x = lua2scalar(L, 3);
409 SkScalar y = lua2scalar(L, 4);
410
411 SkPaint paint;
412 const SkPaint* paintPtr = NULL;
413 if (lua_isnumber(L, 5)) {
414 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
415 paintPtr = &paint;
416 }
417 image->draw(canvas, x, y, paintPtr);
418 return 0;
419}
420
reed@google.comfd345872013-05-22 20:53:42 +0000421static int lcanvas_drawPath(lua_State* L) {
422 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
423 *get_obj<SkPaint>(L, 3));
424 return 0;
425}
426
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000427static int lcanvas_drawText(lua_State* L) {
428 if (lua_gettop(L) < 5) {
429 return 0;
430 }
431
432 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
433 size_t len;
434 const char* text = lua_tolstring(L, 2, &len);
435 get_ref<SkCanvas>(L, 1)->drawText(text, len,
436 lua2scalar(L, 3), lua2scalar(L, 4),
437 *get_obj<SkPaint>(L, 5));
438 }
439 return 0;
440}
441
reed@google.com74ce6f02013-05-22 15:13:18 +0000442static int lcanvas_getSaveCount(lua_State* L) {
443 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
444 return 1;
445}
446
447static int lcanvas_getTotalMatrix(lua_State* L) {
448 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
449 return 1;
450}
451
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000452static int lcanvas_getClipStack(lua_State* L) {
453 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
454 return 1;
455}
456
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000457int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
458#if SK_SUPPORT_GPU
459 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
460 SkISize layerSize = canvas->getTopLayerSize();
461 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
462 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
463 layerSize.fWidth, layerSize.fHeight);
464
465 GrReducedClip::ElementList elements;
466 GrReducedClip::InitialState initialState;
467 int32_t genID;
468 SkIRect resultBounds;
469
470 const SkClipStack& stack = *canvas->getClipStack();
471
472 GrReducedClip::ReduceClipStack(stack,
473 queryBounds,
474 &elements,
475 &genID,
476 &initialState,
477 &resultBounds,
478 NULL);
479
480 GrReducedClip::ElementList::Iter iter(elements);
481 int i = 0;
482 lua_newtable(L);
483 while(NULL != iter.get()) {
484 SkLua(L).pushClipStackElement(*iter.get());
485 iter.next();
486 lua_rawseti(L, -2, ++i);
487 }
488 // Currently this only returns the element list to lua, not the initial state or result bounds.
489 // It could return these as additional items on the lua stack.
490 return 1;
491#else
492 return 0;
493#endif
494}
495
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000496static int lcanvas_save(lua_State* L) {
497 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
498 return 1;
499}
500
501static int lcanvas_restore(lua_State* L) {
502 get_ref<SkCanvas>(L, 1)->restore();
503 return 0;
504}
505
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000506static int lcanvas_scale(lua_State* L) {
507 SkScalar sx = lua2scalar_def(L, 2, 1);
508 SkScalar sy = lua2scalar_def(L, 3, sx);
509 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
510 return 0;
511}
512
reed@google.com3597b732013-05-22 20:12:50 +0000513static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000514 SkScalar tx = lua2scalar_def(L, 2, 0);
515 SkScalar ty = lua2scalar_def(L, 3, 0);
516 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
517 return 0;
518}
519
520static int lcanvas_rotate(lua_State* L) {
521 SkScalar degrees = lua2scalar_def(L, 2, 0);
522 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000523 return 0;
524}
525
reed@google.com74ce6f02013-05-22 15:13:18 +0000526static int lcanvas_gc(lua_State* L) {
527 get_ref<SkCanvas>(L, 1)->unref();
528 return 0;
529}
530
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000531const struct luaL_Reg gSkCanvas_Methods[] = {
reed@google.com74ce6f02013-05-22 15:13:18 +0000532 { "drawColor", lcanvas_drawColor },
533 { "drawRect", lcanvas_drawRect },
534 { "drawOval", lcanvas_drawOval },
535 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000536 { "drawImage", lcanvas_drawImage },
reed@google.comfd345872013-05-22 20:53:42 +0000537 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000538 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000539 { "getSaveCount", lcanvas_getSaveCount },
540 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000541 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000542#if SK_SUPPORT_GPU
543 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
544#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000545 { "save", lcanvas_save },
546 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000547 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000548 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000549 { "rotate", lcanvas_rotate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000550 { "__gc", lcanvas_gc },
551 { NULL, NULL }
552};
553
554///////////////////////////////////////////////////////////////////////////////
555
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000556static int ldocument_beginPage(lua_State* L) {
557 const SkRect* contentPtr = NULL;
558 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
559 lua2scalar(L, 3),
560 contentPtr));
561 return 1;
562}
563
564static int ldocument_endPage(lua_State* L) {
565 get_ref<SkDocument>(L, 1)->endPage();
566 return 0;
567}
568
569static int ldocument_close(lua_State* L) {
570 get_ref<SkDocument>(L, 1)->close();
571 return 0;
572}
573
574static int ldocument_gc(lua_State* L) {
575 get_ref<SkDocument>(L, 1)->unref();
576 return 0;
577}
578
579static const struct luaL_Reg gSkDocument_Methods[] = {
580 { "beginPage", ldocument_beginPage },
581 { "endPage", ldocument_endPage },
582 { "close", ldocument_close },
583 { "__gc", ldocument_gc },
584 { NULL, NULL }
585};
586
587///////////////////////////////////////////////////////////////////////////////
588
reed@google.com74ce6f02013-05-22 15:13:18 +0000589static int lpaint_isAntiAlias(lua_State* L) {
590 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
591 return 1;
592}
593
594static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000595 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000596 return 0;
597}
598
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000599static int lpaint_isDither(lua_State* L) {
600 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
601 return 1;
602}
603
604static int lpaint_isUnderlineText(lua_State* L) {
605 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
606 return 1;
607}
608
609static int lpaint_isStrikeThruText(lua_State* L) {
610 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
611 return 1;
612}
613
614static int lpaint_isFakeBoldText(lua_State* L) {
615 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
616 return 1;
617}
618
619static int lpaint_isLinearText(lua_State* L) {
620 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
621 return 1;
622}
623
624static int lpaint_isSubpixelText(lua_State* L) {
625 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
626 return 1;
627}
628
629static int lpaint_isDevKernText(lua_State* L) {
630 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
631 return 1;
632}
633
634static int lpaint_isLCDRenderText(lua_State* L) {
635 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
636 return 1;
637}
638
639static int lpaint_isEmbeddedBitmapText(lua_State* L) {
640 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
641 return 1;
642}
643
644static int lpaint_isAutohinted(lua_State* L) {
645 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
646 return 1;
647}
648
649static int lpaint_isVerticalText(lua_State* L) {
650 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
651 return 1;
652}
653
reed@google.com74ce6f02013-05-22 15:13:18 +0000654static int lpaint_getColor(lua_State* L) {
655 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
656 return 1;
657}
658
659static int lpaint_setColor(lua_State* L) {
660 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
661 return 0;
662}
663
reed@google.come3823fd2013-05-30 18:55:14 +0000664static int lpaint_getTextSize(lua_State* L) {
665 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
666 return 1;
667}
668
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000669static int lpaint_getTextScaleX(lua_State* L) {
670 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
671 return 1;
672}
673
674static int lpaint_getTextSkewX(lua_State* L) {
675 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
676 return 1;
677}
678
reed@google.come3823fd2013-05-30 18:55:14 +0000679static int lpaint_setTextSize(lua_State* L) {
680 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
681 return 0;
682}
683
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000684static int lpaint_getTypeface(lua_State* L) {
685 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
686 return 1;
687}
688
689static int lpaint_setTypeface(lua_State* L) {
690 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
691 return 0;
692}
693
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000694static int lpaint_getHinting(lua_State* L) {
695 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
696 return 1;
697}
698
reed@google.come3823fd2013-05-30 18:55:14 +0000699static int lpaint_getFontID(lua_State* L) {
700 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
701 SkLua(L).pushU32(SkTypeface::UniqueID(face));
702 return 1;
703}
704
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000705static const struct {
706 const char* fLabel;
707 SkPaint::Align fAlign;
708} gAlignRec[] = {
709 { "left", SkPaint::kLeft_Align },
710 { "center", SkPaint::kCenter_Align },
711 { "right", SkPaint::kRight_Align },
712};
713
714static int lpaint_getTextAlign(lua_State* L) {
715 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
716 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
717 if (gAlignRec[i].fAlign == align) {
718 lua_pushstring(L, gAlignRec[i].fLabel);
719 return 1;
720 }
721 }
722 return 0;
723}
724
725static int lpaint_setTextAlign(lua_State* L) {
726 if (lua_isstring(L, 2)) {
727 size_t len;
728 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000729
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000730 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
731 if (!strcmp(gAlignRec[i].fLabel, label)) {
732 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
733 break;
734 }
735 }
736 }
737 return 0;
738}
739
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000740static int lpaint_getStroke(lua_State* L) {
741 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
742 return 1;
743}
744
745static int lpaint_setStroke(lua_State* L) {
746 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000747
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000748 if (lua_toboolean(L, 2)) {
749 style = SkPaint::kStroke_Style;
750 } else {
751 style = SkPaint::kFill_Style;
752 }
753 get_obj<SkPaint>(L, 1)->setStyle(style);
754 return 0;
755}
756
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000757static int lpaint_getStrokeCap(lua_State* L) {
758 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
759 return 1;
760}
761
762static int lpaint_getStrokeJoin(lua_State* L) {
763 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
764 return 1;
765}
766
767static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000768 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000769 return 1;
770}
771
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000772static int lpaint_getStrokeWidth(lua_State* L) {
773 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
774 return 1;
775}
776
777static int lpaint_setStrokeWidth(lua_State* L) {
778 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
779 return 0;
780}
781
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000782static int lpaint_getStrokeMiter(lua_State* L) {
783 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
784 return 1;
785}
786
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000787static int lpaint_measureText(lua_State* L) {
788 if (lua_isstring(L, 2)) {
789 size_t len;
790 const char* text = lua_tolstring(L, 2, &len);
791 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
792 return 1;
793 }
794 return 0;
795}
796
797struct FontMetrics {
798 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
799 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
800 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
801 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
802 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
803 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
804 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
805 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
806 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
807};
808
809static int lpaint_getFontMetrics(lua_State* L) {
810 SkPaint::FontMetrics fm;
811 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000812
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000813 lua_newtable(L);
814 setfield_scalar(L, "top", fm.fTop);
815 setfield_scalar(L, "ascent", fm.fAscent);
816 setfield_scalar(L, "descent", fm.fDescent);
817 setfield_scalar(L, "bottom", fm.fBottom);
818 setfield_scalar(L, "leading", fm.fLeading);
819 SkLua(L).pushScalar(height);
820 return 2;
821}
822
reed@google.com29563872013-07-10 21:23:49 +0000823static int lpaint_getEffects(lua_State* L) {
824 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000825
reed@google.com29563872013-07-10 21:23:49 +0000826 lua_newtable(L);
827 setfield_bool_if(L, "looper", !!paint->getLooper());
828 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
829 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
830 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
831 setfield_bool_if(L, "shader", !!paint->getShader());
832 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
833 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
834 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
835 return 1;
836}
837
reed@google.com5fdc9832013-07-24 15:47:52 +0000838static int lpaint_getShader(lua_State* L) {
839 const SkPaint* paint = get_obj<SkPaint>(L, 1);
840 SkShader* shader = paint->getShader();
841 if (shader) {
842 push_ref(L, shader);
843 return 1;
844 }
845 return 0;
846}
847
reed@google.com74ce6f02013-05-22 15:13:18 +0000848static int lpaint_gc(lua_State* L) {
849 get_obj<SkPaint>(L, 1)->~SkPaint();
850 return 0;
851}
852
853static const struct luaL_Reg gSkPaint_Methods[] = {
854 { "isAntiAlias", lpaint_isAntiAlias },
855 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000856 { "isDither", lpaint_isDither },
857 { "isUnderlineText", lpaint_isUnderlineText },
858 { "isStrikeThruText", lpaint_isStrikeThruText },
859 { "isFakeBoldText", lpaint_isFakeBoldText },
860 { "isLinearText", lpaint_isLinearText },
861 { "isSubpixelText", lpaint_isSubpixelText },
862 { "isDevKernText", lpaint_isDevKernText },
863 { "isLCDRenderText", lpaint_isLCDRenderText },
864 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
865 { "isAutohinted", lpaint_isAutohinted },
866 { "isVerticalText", lpaint_isVerticalText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000867 { "getColor", lpaint_getColor },
868 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000869 { "getTextSize", lpaint_getTextSize },
870 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000871 { "getTextScaleX", lpaint_getTextScaleX },
872 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000873 { "getTypeface", lpaint_getTypeface },
874 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000875 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +0000876 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000877 { "getTextAlign", lpaint_getTextAlign },
878 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000879 { "getStroke", lpaint_getStroke },
880 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000881 { "getStrokeCap", lpaint_getStrokeCap },
882 { "getStrokeJoin", lpaint_getStrokeJoin },
883 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000884 { "getStrokeWidth", lpaint_getStrokeWidth },
885 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000886 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000887 { "measureText", lpaint_measureText },
888 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +0000889 { "getEffects", lpaint_getEffects },
reed@google.com5fdc9832013-07-24 15:47:52 +0000890 { "getShader", lpaint_getShader },
reed@google.com74ce6f02013-05-22 15:13:18 +0000891 { "__gc", lpaint_gc },
892 { NULL, NULL }
893};
894
895///////////////////////////////////////////////////////////////////////////////
896
reed@google.com5fdc9832013-07-24 15:47:52 +0000897static const char* mode2string(SkShader::TileMode mode) {
898 static const char* gNames[] = { "clamp", "repeat", "mirror" };
899 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
900 return gNames[mode];
901}
902
903static const char* gradtype2string(SkShader::GradientType t) {
904 static const char* gNames[] = {
905 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
906 };
907 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
908 return gNames[t];
909}
910
911static int lshader_isOpaque(lua_State* L) {
912 SkShader* shader = get_ref<SkShader>(L, 1);
913 return shader && shader->isOpaque();
914}
915
916static int lshader_asABitmap(lua_State* L) {
917 SkShader* shader = get_ref<SkShader>(L, 1);
918 if (shader) {
919 SkBitmap bm;
920 SkMatrix matrix;
921 SkShader::TileMode modes[2];
922 switch (shader->asABitmap(&bm, &matrix, modes)) {
923 case SkShader::kDefault_BitmapType:
924 lua_newtable(L);
925 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
926 setfield_number(L, "width", bm.width());
927 setfield_number(L, "height", bm.height());
928 setfield_string(L, "tileX", mode2string(modes[0]));
929 setfield_string(L, "tileY", mode2string(modes[1]));
930 return 1;
931 default:
932 break;
933 }
934 }
935 return 0;
936}
937
938static int lshader_asAGradient(lua_State* L) {
939 SkShader* shader = get_ref<SkShader>(L, 1);
940 if (shader) {
941 SkShader::GradientInfo info;
942 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000943
944 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000945 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000946
947 info.fColorCount = 3;
948 info.fColors = &colors[0];
949 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000950
reed@google.com5fdc9832013-07-24 15:47:52 +0000951 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000952
reed@google.com5fdc9832013-07-24 15:47:52 +0000953 if (SkShader::kNone_GradientType != t) {
954 lua_newtable(L);
955 setfield_string(L, "type", gradtype2string(t));
956 setfield_number(L, "colorCount", info.fColorCount);
957 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +0000958
959 if (info.fColorCount == 3){
960 setfield_number(L, "midPos", pos[1]);
961 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +0000962
reed@google.com5fdc9832013-07-24 15:47:52 +0000963 return 1;
964 }
965 }
966 return 0;
967}
968
969static int lshader_gc(lua_State* L) {
970 get_ref<SkShader>(L, 1)->unref();
971 return 0;
972}
973
974static const struct luaL_Reg gSkShader_Methods[] = {
975 { "isOpaque", lshader_isOpaque },
976 { "asABitmap", lshader_asABitmap },
977 { "asAGradient", lshader_asAGradient },
978 { "__gc", lshader_gc },
979 { NULL, NULL }
980};
981
982///////////////////////////////////////////////////////////////////////////////
983
humper@google.com2815c192013-07-10 22:42:30 +0000984static int lmatrix_getType(lua_State* L) {
985 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000986
humper@google.com2815c192013-07-10 22:42:30 +0000987 lua_newtable(L);
988 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
989 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
990 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
991 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
992 return 1;
993}
994
humper@google.com0f48ee02013-07-26 15:23:43 +0000995static int lmatrix_getScaleX(lua_State* L) {
996 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
997 return 1;
998}
999
1000static int lmatrix_getScaleY(lua_State* L) {
1001 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1002 return 1;
1003}
1004
1005static int lmatrix_getTranslateX(lua_State* L) {
1006 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1007 return 1;
1008}
1009
1010static int lmatrix_getTranslateY(lua_State* L) {
1011 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1012 return 1;
1013}
1014
humper@google.com2815c192013-07-10 22:42:30 +00001015static const struct luaL_Reg gSkMatrix_Methods[] = {
1016 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001017 { "getScaleX", lmatrix_getScaleX },
1018 { "getScaleY", lmatrix_getScaleY },
1019 { "getTranslateX", lmatrix_getTranslateX },
1020 { "getTranslateY", lmatrix_getTranslateY },
humper@google.com2815c192013-07-10 22:42:30 +00001021 { NULL, NULL }
1022};
1023
1024///////////////////////////////////////////////////////////////////////////////
1025
reed@google.com74ce6f02013-05-22 15:13:18 +00001026static int lpath_getBounds(lua_State* L) {
1027 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1028 return 1;
1029}
1030
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001031static const char* fill_type_to_str(SkPath::FillType fill) {
1032 switch (fill) {
1033 case SkPath::kEvenOdd_FillType:
1034 return "even-odd";
1035 case SkPath::kWinding_FillType:
1036 return "winding";
1037 case SkPath::kInverseEvenOdd_FillType:
1038 return "inverse-even-odd";
1039 case SkPath::kInverseWinding_FillType:
1040 return "inverse-winding";
1041 }
1042 return "unknown";
1043}
1044
1045static int lpath_getFillType(lua_State* L) {
1046 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1047 SkLua(L).pushString(fill_type_to_str(fill));
1048 return 1;
1049}
1050
1051static SkString segment_masks_to_str(uint32_t segmentMasks) {
1052 SkString result;
1053 bool first = true;
1054 if (SkPath::kLine_SegmentMask & segmentMasks) {
1055 result.append("line");
1056 first = false;
1057 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1058 }
1059 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1060 if (!first) {
1061 result.append(" ");
1062 }
1063 result.append("quad");
1064 first = false;
1065 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1066 }
1067 if (SkPath::kConic_SegmentMask & segmentMasks) {
1068 if (!first) {
1069 result.append(" ");
1070 }
1071 result.append("conic");
1072 first = false;
1073 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1074 }
1075 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1076 if (!first) {
1077 result.append(" ");
1078 }
1079 result.append("cubic");
1080 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1081 }
1082 SkASSERT(0 == segmentMasks);
1083 return result;
1084}
1085
1086static int lpath_getSegementTypes(lua_State* L) {
1087 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1088 SkLua(L).pushString(segment_masks_to_str(segMasks));
1089 return 1;
1090}
1091
1092static int lpath_isConvex(lua_State* L) {
1093 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1094 SkLua(L).pushBool(isConvex);
1095 return 1;
1096}
1097
reed@google.com74ce6f02013-05-22 15:13:18 +00001098static int lpath_isEmpty(lua_State* L) {
1099 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1100 return 1;
1101}
1102
1103static int lpath_isRect(lua_State* L) {
1104 SkRect r;
1105 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1106 int ret_count = 1;
1107 lua_pushboolean(L, pred);
1108 if (pred) {
1109 SkLua(L).pushRect(r);
1110 ret_count += 1;
1111 }
1112 return ret_count;
1113}
1114
1115static const char* dir2string(SkPath::Direction dir) {
1116 static const char* gStr[] = {
1117 "unknown", "cw", "ccw"
1118 };
1119 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1120 return gStr[dir];
1121}
1122
1123static int lpath_isNestedRects(lua_State* L) {
1124 SkRect rects[2];
1125 SkPath::Direction dirs[2];
1126 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1127 int ret_count = 1;
1128 lua_pushboolean(L, pred);
1129 if (pred) {
1130 SkLua lua(L);
1131 lua.pushRect(rects[0]);
1132 lua.pushRect(rects[1]);
1133 lua_pushstring(L, dir2string(dirs[0]));
1134 lua_pushstring(L, dir2string(dirs[0]));
1135 ret_count += 4;
1136 }
1137 return ret_count;
1138}
1139
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001140static int lpath_countPoints(lua_State* L) {
1141 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1142 return 1;
1143}
1144
reed@google.com74ce6f02013-05-22 15:13:18 +00001145static int lpath_reset(lua_State* L) {
1146 get_obj<SkPath>(L, 1)->reset();
1147 return 0;
1148}
1149
1150static int lpath_moveTo(lua_State* L) {
1151 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1152 return 0;
1153}
1154
1155static int lpath_lineTo(lua_State* L) {
1156 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1157 return 0;
1158}
1159
1160static int lpath_quadTo(lua_State* L) {
1161 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1162 lua2scalar(L, 4), lua2scalar(L, 5));
1163 return 0;
1164}
1165
1166static int lpath_cubicTo(lua_State* L) {
1167 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1168 lua2scalar(L, 4), lua2scalar(L, 5),
1169 lua2scalar(L, 6), lua2scalar(L, 7));
1170 return 0;
1171}
1172
1173static int lpath_close(lua_State* L) {
1174 get_obj<SkPath>(L, 1)->close();
1175 return 0;
1176}
1177
1178static int lpath_gc(lua_State* L) {
1179 get_obj<SkPath>(L, 1)->~SkPath();
1180 return 0;
1181}
1182
1183static const struct luaL_Reg gSkPath_Methods[] = {
1184 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001185 { "getFillType", lpath_getFillType },
1186 { "getSegmentTypes", lpath_getSegementTypes },
1187 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001188 { "isEmpty", lpath_isEmpty },
1189 { "isRect", lpath_isRect },
1190 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001191 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001192 { "reset", lpath_reset },
1193 { "moveTo", lpath_moveTo },
1194 { "lineTo", lpath_lineTo },
1195 { "quadTo", lpath_quadTo },
1196 { "cubicTo", lpath_cubicTo },
1197 { "close", lpath_close },
1198 { "__gc", lpath_gc },
1199 { NULL, NULL }
1200};
1201
1202///////////////////////////////////////////////////////////////////////////////
1203
1204static const char* rrect_type(const SkRRect& rr) {
1205 switch (rr.getType()) {
1206 case SkRRect::kUnknown_Type: return "unknown";
1207 case SkRRect::kEmpty_Type: return "empty";
1208 case SkRRect::kRect_Type: return "rect";
1209 case SkRRect::kOval_Type: return "oval";
1210 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001211 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001212 case SkRRect::kComplex_Type: return "complex";
1213 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001214 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001215 return "";
1216}
1217
1218static int lrrect_rect(lua_State* L) {
1219 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1220 return 1;
1221}
1222
1223static int lrrect_type(lua_State* L) {
1224 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1225 return 1;
1226}
1227
1228static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001229 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001230 SkVector v;
1231 if (corner < 0 || corner > 3) {
1232 SkDebugf("bad corner index %d", corner);
1233 v.set(0, 0);
1234 } else {
1235 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1236 }
1237 lua_pushnumber(L, v.fX);
1238 lua_pushnumber(L, v.fY);
1239 return 2;
1240}
1241
1242static int lrrect_gc(lua_State* L) {
1243 get_obj<SkRRect>(L, 1)->~SkRRect();
1244 return 0;
1245}
1246
1247static const struct luaL_Reg gSkRRect_Methods[] = {
1248 { "rect", lrrect_rect },
1249 { "type", lrrect_type },
1250 { "radii", lrrect_radii },
1251 { "__gc", lrrect_gc },
1252 { NULL, NULL }
1253};
1254
1255///////////////////////////////////////////////////////////////////////////////
1256
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001257static int limage_width(lua_State* L) {
1258 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1259 return 1;
1260}
1261
1262static int limage_height(lua_State* L) {
1263 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1264 return 1;
1265}
1266
1267static int limage_gc(lua_State* L) {
1268 get_ref<SkImage>(L, 1)->unref();
1269 return 0;
1270}
1271
1272static const struct luaL_Reg gSkImage_Methods[] = {
1273 { "width", limage_width },
1274 { "height", limage_height },
1275 { "__gc", limage_gc },
1276 { NULL, NULL }
1277};
1278
1279///////////////////////////////////////////////////////////////////////////////
1280
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001281static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001282 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001283 return 0;
1284}
1285
1286static const struct luaL_Reg gSkTypeface_Methods[] = {
1287 { "__gc", ltypeface_gc },
1288 { NULL, NULL }
1289};
1290
1291///////////////////////////////////////////////////////////////////////////////
1292
reed@google.com74ce6f02013-05-22 15:13:18 +00001293class AutoCallLua {
1294public:
1295 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1296 lua_getglobal(L, func);
1297 if (!lua_isfunction(L, -1)) {
1298 int t = lua_type(L, -1);
1299 SkDebugf("--- expected function %d\n", t);
1300 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001301
reed@google.com74ce6f02013-05-22 15:13:18 +00001302 lua_newtable(L);
1303 setfield_string(L, "verb", verb);
1304 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001305
reed@google.com74ce6f02013-05-22 15:13:18 +00001306 ~AutoCallLua() {
1307 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1308 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1309 }
1310 lua_settop(fL, -1);
1311 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001312
reed@google.com74ce6f02013-05-22 15:13:18 +00001313private:
1314 lua_State* fL;
1315};
1316
1317#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1318
1319///////////////////////////////////////////////////////////////////////////////
1320
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001321static int lsk_newDocumentPDF(lua_State* L) {
1322 const char* file = NULL;
1323 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1324 file = lua_tolstring(L, 1, NULL);
1325 }
1326
1327 SkDocument* doc = SkDocument::CreatePDF(file);
1328 if (NULL == doc) {
1329 // do I need to push a nil on the stack and return 1?
1330 return 0;
1331 } else {
1332 push_ref(L, doc);
1333 doc->unref();
1334 return 1;
1335 }
1336}
1337
reed@google.com3597b732013-05-22 20:12:50 +00001338static int lsk_newPaint(lua_State* L) {
1339 push_new<SkPaint>(L);
1340 return 1;
1341}
1342
1343static int lsk_newPath(lua_State* L) {
1344 push_new<SkPath>(L);
1345 return 1;
1346}
1347
1348static int lsk_newRRect(lua_State* L) {
1349 SkRRect* rr = push_new<SkRRect>(L);
1350 rr->setEmpty();
1351 return 1;
1352}
1353
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001354static int lsk_newTypeface(lua_State* L) {
1355 const char* name = NULL;
1356 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001357
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001358 int count = lua_gettop(L);
1359 if (count > 0 && lua_isstring(L, 1)) {
1360 name = lua_tolstring(L, 1, NULL);
1361 if (count > 1 && lua_isnumber(L, 2)) {
1362 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1363 }
1364 }
1365
1366 SkTypeface* face = SkTypeface::CreateFromName(name,
1367 (SkTypeface::Style)style);
1368// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1369 if (NULL == face) {
1370 face = SkTypeface::RefDefault();
1371 }
1372 push_ref(L, face);
1373 face->unref();
1374 return 1;
1375}
reed@google.com3597b732013-05-22 20:12:50 +00001376
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001377static int lsk_loadImage(lua_State* L) {
1378 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1379 const char* name = lua_tolstring(L, 1, NULL);
1380 SkAutoDataUnref data(SkData::NewFromFileName(name));
1381 if (data.get()) {
1382 SkImage* image = SkImage::NewEncodedData(data.get());
1383 if (image) {
1384 push_ref(L, image);
1385 image->unref();
1386 return 1;
1387 }
1388 }
1389 }
1390 return 0;
1391}
1392
reed@google.com3597b732013-05-22 20:12:50 +00001393static void register_Sk(lua_State* L) {
1394 lua_newtable(L);
1395 lua_pushvalue(L, -1);
1396 lua_setglobal(L, "Sk");
1397 // the Sk table is still on top
1398
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001399 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001400 setfield_function(L, "loadImage", lsk_loadImage);
reed@google.com3597b732013-05-22 20:12:50 +00001401 setfield_function(L, "newPaint", lsk_newPaint);
1402 setfield_function(L, "newPath", lsk_newPath);
1403 setfield_function(L, "newRRect", lsk_newRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001404 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001405 lua_pop(L, 1); // pop off the Sk table
1406}
1407
reed@google.com74ce6f02013-05-22 15:13:18 +00001408#define REG_CLASS(L, C) \
1409 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001410 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001411 lua_pushvalue(L, -1); \
1412 lua_setfield(L, -2, "__index"); \
1413 luaL_setfuncs(L, g##C##_Methods, 0); \
1414 lua_pop(L, 1); /* pop off the meta-table */ \
1415 } while (0)
1416
1417void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001418 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001419 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001420 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001421 REG_CLASS(L, SkImage);
reed@google.com74ce6f02013-05-22 15:13:18 +00001422 REG_CLASS(L, SkPath);
1423 REG_CLASS(L, SkPaint);
1424 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001425 REG_CLASS(L, SkShader);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001426 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001427 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001428}
zachr@google.com28c27c82013-06-20 17:15:05 +00001429
reed@google.com7bce9982013-06-20 17:40:21 +00001430extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001431extern "C" int luaopen_skia(lua_State* L) {
1432 SkLua::Load(L);
1433 return 0;
1434}