blob: d1f2633c941d95bfede300da65f1ec5092641cc7 [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"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPath.h"
12#include "SkMatrix.h"
13#include "SkRRect.h"
14#include "SkString.h"
reed@google.come3823fd2013-05-30 18:55:14 +000015#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000016
17extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000018 #include "lua.h"
19 #include "lualib.h"
20 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000021}
22
reed@google.comfd345872013-05-22 20:53:42 +000023// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000024template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000025#define DEF_MTNAME(T) \
26 template <> const char* get_mtname<T>() { \
27 return #T "_LuaMetaTableName"; \
28 }
29
30DEF_MTNAME(SkCanvas)
31DEF_MTNAME(SkMatrix)
32DEF_MTNAME(SkRRect)
33DEF_MTNAME(SkPath)
34DEF_MTNAME(SkPaint)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000035DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000036
reed@google.com3597b732013-05-22 20:12:50 +000037template <typename T> T* push_new(lua_State* L) {
38 T* addr = (T*)lua_newuserdata(L, sizeof(T));
39 new (addr) T;
40 luaL_getmetatable(L, get_mtname<T>());
41 lua_setmetatable(L, -2);
42 return addr;
43}
reed@google.com74ce6f02013-05-22 15:13:18 +000044
45template <typename T> void push_obj(lua_State* L, const T& obj) {
46 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000047 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000048 lua_setmetatable(L, -2);
49}
50
51template <typename T> void push_ref(lua_State* L, T* ref) {
52 *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000053 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000054 lua_setmetatable(L, -2);
55}
56
57template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000058 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000059}
60
61template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000062 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000063}
64
reed@google.com88c9ec92013-05-22 15:43:21 +000065static bool lua2bool(lua_State* L, int index) {
66 return !!lua_toboolean(L, index);
67}
68
reed@google.com74ce6f02013-05-22 15:13:18 +000069///////////////////////////////////////////////////////////////////////////////
70
reed@google.com3597b732013-05-22 20:12:50 +000071SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
72 fL = luaL_newstate();
73 luaL_openlibs(fL);
74 SkLua::Load(fL);
75}
76
77SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
78
79SkLua::~SkLua() {
80 if (fWeOwnL) {
81 if (fTermCode.size() > 0) {
82 lua_getglobal(fL, fTermCode.c_str());
83 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
84 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
85 }
86 }
87 lua_close(fL);
88 }
89}
90
91bool SkLua::runCode(const char code[]) {
92 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
93 if (err) {
94 SkDebugf("--- lua failed\n");
95 return false;
96 }
97 return true;
98}
99
100bool SkLua::runCode(const void* code, size_t size) {
101 SkString str((const char*)code, size);
102 return this->runCode(str.c_str());
103}
104
105///////////////////////////////////////////////////////////////////////////////
106
107#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
108
reed@google.com74ce6f02013-05-22 15:13:18 +0000109static void setfield_string(lua_State* L, const char key[], const char value[]) {
110 lua_pushstring(L, value);
111 lua_setfield(L, -2, key);
112}
113
114static void setfield_number(lua_State* L, const char key[], double value) {
115 lua_pushnumber(L, value);
116 lua_setfield(L, -2, key);
117}
118
reed@google.com3597b732013-05-22 20:12:50 +0000119static void setfield_function(lua_State* L,
120 const char key[], lua_CFunction value) {
121 lua_pushcfunction(L, value);
122 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000123}
124
reed@google.come3823fd2013-05-30 18:55:14 +0000125static void setarray_number(lua_State* L, int index, double value) {
126 lua_pushnumber(L, value);
127 lua_rawseti(L, -2, index);
128}
129
reed@google.com74ce6f02013-05-22 15:13:18 +0000130void SkLua::pushBool(bool value, const char key[]) {
131 lua_pushboolean(fL, value);
132 CHECK_SETFIELD(key);
133}
134
135void SkLua::pushString(const char str[], const char key[]) {
136 lua_pushstring(fL, str);
137 CHECK_SETFIELD(key);
138}
139
reed@google.come3823fd2013-05-30 18:55:14 +0000140void SkLua::pushString(const char str[], size_t length, const char key[]) {
141 // TODO: how to do this w/o making a copy?
142 SkString s(str, length);
143 lua_pushstring(fL, s.c_str());
144 CHECK_SETFIELD(key);
145}
146
reed@google.com74ce6f02013-05-22 15:13:18 +0000147void SkLua::pushString(const SkString& str, const char key[]) {
148 lua_pushstring(fL, str.c_str());
149 CHECK_SETFIELD(key);
150}
151
152void SkLua::pushColor(SkColor color, const char key[]) {
153 lua_newtable(fL);
154 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
155 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
156 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
157 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
158 CHECK_SETFIELD(key);
159}
160
reed@google.come3823fd2013-05-30 18:55:14 +0000161void SkLua::pushU32(uint32_t value, const char key[]) {
162 lua_pushnumber(fL, (double)value);
163 CHECK_SETFIELD(key);
164}
165
reed@google.com74ce6f02013-05-22 15:13:18 +0000166void SkLua::pushScalar(SkScalar value, const char key[]) {
167 lua_pushnumber(fL, SkScalarToLua(value));
168 CHECK_SETFIELD(key);
169}
170
reed@google.come3823fd2013-05-30 18:55:14 +0000171void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
172 lua_newtable(fL);
173 for (int i = 0; i < count; ++i) {
174 // make it base-1 to match lua convention
175 setarray_number(fL, i + 1, (double)array[i]);
176 }
177 CHECK_SETFIELD(key);
178}
179
reed@google.com74ce6f02013-05-22 15:13:18 +0000180void SkLua::pushRect(const SkRect& r, const char key[]) {
181 lua_newtable(fL);
182 setfield_number(fL, "left", SkScalarToLua(r.fLeft));
183 setfield_number(fL, "top", SkScalarToLua(r.fTop));
184 setfield_number(fL, "right", SkScalarToLua(r.fRight));
185 setfield_number(fL, "bottom", SkScalarToLua(r.fBottom));
186 CHECK_SETFIELD(key);
187}
188
189void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
190 push_obj(fL, rr);
191 CHECK_SETFIELD(key);
192}
193
194void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
195 push_obj(fL, matrix);
196 CHECK_SETFIELD(key);
197}
198
199void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
200 push_obj(fL, paint);
201 CHECK_SETFIELD(key);
202}
203
204void SkLua::pushPath(const SkPath& path, const char key[]) {
205 push_obj(fL, path);
206 CHECK_SETFIELD(key);
207}
208
209void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
210 push_ref(fL, canvas);
211 CHECK_SETFIELD(key);
212}
213
214///////////////////////////////////////////////////////////////////////////////
215///////////////////////////////////////////////////////////////////////////////
216
217static SkScalar lua2scalar(lua_State* L, int index) {
218 SkASSERT(lua_isnumber(L, index));
219 return SkLuaToScalar(lua_tonumber(L, index));
220}
221
222static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
223 SkASSERT(lua_istable(L, index));
224 lua_pushstring(L, key);
225 lua_gettable(L, index);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000226
reed@google.com74ce6f02013-05-22 15:13:18 +0000227 SkScalar value = lua2scalar(L, -1);
228 lua_pop(L, 1);
229 return value;
230}
231
232static U8CPU unit2byte(SkScalar x) {
233 if (x <= 0) {
234 return 0;
235 } else if (x >= 1) {
236 return 255;
237 } else {
238 return SkScalarRoundToInt(x * 255);
239 }
240}
241
242static SkColor lua2color(lua_State* L, int index) {
243 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
244 unit2byte(getfield_scalar(L, index, "r")),
245 unit2byte(getfield_scalar(L, index, "g")),
246 unit2byte(getfield_scalar(L, index, "b")));
247}
248
249static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
250 rect->set(getfield_scalar(L, index, "left"),
251 getfield_scalar(L, index, "top"),
252 getfield_scalar(L, index, "right"),
253 getfield_scalar(L, index, "bottom"));
254 return rect;
255}
256
257static int lcanvas_drawColor(lua_State* L) {
258 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
259 return 0;
260}
261
262static int lcanvas_drawRect(lua_State* L) {
263 SkRect rect;
264 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
265 *get_obj<SkPaint>(L, 3));
266 return 0;
267}
268
269static int lcanvas_drawOval(lua_State* L) {
270 SkRect rect;
271 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
272 *get_obj<SkPaint>(L, 3));
273 return 0;
274}
275
276static int lcanvas_drawCircle(lua_State* L) {
277 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
278 lua2scalar(L, 3),
279 lua2scalar(L, 4),
280 *get_obj<SkPaint>(L, 5));
281 return 0;
282}
283
reed@google.comfd345872013-05-22 20:53:42 +0000284static int lcanvas_drawPath(lua_State* L) {
285 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
286 *get_obj<SkPaint>(L, 3));
287 return 0;
288}
289
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000290static int lcanvas_drawText(lua_State* L) {
291 if (lua_gettop(L) < 5) {
292 return 0;
293 }
294
295 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
296 size_t len;
297 const char* text = lua_tolstring(L, 2, &len);
298 get_ref<SkCanvas>(L, 1)->drawText(text, len,
299 lua2scalar(L, 3), lua2scalar(L, 4),
300 *get_obj<SkPaint>(L, 5));
301 }
302 return 0;
303}
304
reed@google.com74ce6f02013-05-22 15:13:18 +0000305static int lcanvas_getSaveCount(lua_State* L) {
306 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
307 return 1;
308}
309
310static int lcanvas_getTotalMatrix(lua_State* L) {
311 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
312 return 1;
313}
314
reed@google.com3597b732013-05-22 20:12:50 +0000315static int lcanvas_translate(lua_State* L) {
316 get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3));
317 return 0;
318}
319
reed@google.com74ce6f02013-05-22 15:13:18 +0000320static int lcanvas_gc(lua_State* L) {
321 get_ref<SkCanvas>(L, 1)->unref();
322 return 0;
323}
324
325static const struct luaL_Reg gSkCanvas_Methods[] = {
326 { "drawColor", lcanvas_drawColor },
327 { "drawRect", lcanvas_drawRect },
328 { "drawOval", lcanvas_drawOval },
329 { "drawCircle", lcanvas_drawCircle },
reed@google.comfd345872013-05-22 20:53:42 +0000330 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000331 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000332 { "getSaveCount", lcanvas_getSaveCount },
333 { "getTotalMatrix", lcanvas_getTotalMatrix },
reed@google.com3597b732013-05-22 20:12:50 +0000334 { "translate", lcanvas_translate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000335 { "__gc", lcanvas_gc },
336 { NULL, NULL }
337};
338
339///////////////////////////////////////////////////////////////////////////////
340
341static int lpaint_isAntiAlias(lua_State* L) {
342 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
343 return 1;
344}
345
346static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000347 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000348 return 0;
349}
350
351static int lpaint_getColor(lua_State* L) {
352 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
353 return 1;
354}
355
356static int lpaint_setColor(lua_State* L) {
357 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
358 return 0;
359}
360
reed@google.come3823fd2013-05-30 18:55:14 +0000361static int lpaint_getTextSize(lua_State* L) {
362 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
363 return 1;
364}
365
366static int lpaint_setTextSize(lua_State* L) {
367 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
368 return 0;
369}
370
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000371static int lpaint_getTypeface(lua_State* L) {
372 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
373 return 1;
374}
375
376static int lpaint_setTypeface(lua_State* L) {
377 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
378 return 0;
379}
380
reed@google.come3823fd2013-05-30 18:55:14 +0000381static int lpaint_getFontID(lua_State* L) {
382 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
383 SkLua(L).pushU32(SkTypeface::UniqueID(face));
384 return 1;
385}
386
reed@google.com74ce6f02013-05-22 15:13:18 +0000387static int lpaint_gc(lua_State* L) {
388 get_obj<SkPaint>(L, 1)->~SkPaint();
389 return 0;
390}
391
392static const struct luaL_Reg gSkPaint_Methods[] = {
393 { "isAntiAlias", lpaint_isAntiAlias },
394 { "setAntiAlias", lpaint_setAntiAlias },
395 { "getColor", lpaint_getColor },
396 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000397 { "getTextSize", lpaint_getTextSize },
398 { "setTextSize", lpaint_setTextSize },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000399 { "getTypeface", lpaint_getTypeface },
400 { "setTypeface", lpaint_setTypeface },
reed@google.come3823fd2013-05-30 18:55:14 +0000401 { "getFontID", lpaint_getFontID },
reed@google.com74ce6f02013-05-22 15:13:18 +0000402 { "__gc", lpaint_gc },
403 { NULL, NULL }
404};
405
406///////////////////////////////////////////////////////////////////////////////
407
408static int lpath_getBounds(lua_State* L) {
409 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
410 return 1;
411}
412
413static int lpath_isEmpty(lua_State* L) {
414 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
415 return 1;
416}
417
418static int lpath_isRect(lua_State* L) {
419 SkRect r;
420 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
421 int ret_count = 1;
422 lua_pushboolean(L, pred);
423 if (pred) {
424 SkLua(L).pushRect(r);
425 ret_count += 1;
426 }
427 return ret_count;
428}
429
430static const char* dir2string(SkPath::Direction dir) {
431 static const char* gStr[] = {
432 "unknown", "cw", "ccw"
433 };
434 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
435 return gStr[dir];
436}
437
438static int lpath_isNestedRects(lua_State* L) {
439 SkRect rects[2];
440 SkPath::Direction dirs[2];
441 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
442 int ret_count = 1;
443 lua_pushboolean(L, pred);
444 if (pred) {
445 SkLua lua(L);
446 lua.pushRect(rects[0]);
447 lua.pushRect(rects[1]);
448 lua_pushstring(L, dir2string(dirs[0]));
449 lua_pushstring(L, dir2string(dirs[0]));
450 ret_count += 4;
451 }
452 return ret_count;
453}
454
455static int lpath_reset(lua_State* L) {
456 get_obj<SkPath>(L, 1)->reset();
457 return 0;
458}
459
460static int lpath_moveTo(lua_State* L) {
461 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
462 return 0;
463}
464
465static int lpath_lineTo(lua_State* L) {
466 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
467 return 0;
468}
469
470static int lpath_quadTo(lua_State* L) {
471 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
472 lua2scalar(L, 4), lua2scalar(L, 5));
473 return 0;
474}
475
476static int lpath_cubicTo(lua_State* L) {
477 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
478 lua2scalar(L, 4), lua2scalar(L, 5),
479 lua2scalar(L, 6), lua2scalar(L, 7));
480 return 0;
481}
482
483static int lpath_close(lua_State* L) {
484 get_obj<SkPath>(L, 1)->close();
485 return 0;
486}
487
488static int lpath_gc(lua_State* L) {
489 get_obj<SkPath>(L, 1)->~SkPath();
490 return 0;
491}
492
493static const struct luaL_Reg gSkPath_Methods[] = {
494 { "getBounds", lpath_getBounds },
495 { "isEmpty", lpath_isEmpty },
496 { "isRect", lpath_isRect },
497 { "isNestedRects", lpath_isNestedRects },
498 { "reset", lpath_reset },
499 { "moveTo", lpath_moveTo },
500 { "lineTo", lpath_lineTo },
501 { "quadTo", lpath_quadTo },
502 { "cubicTo", lpath_cubicTo },
503 { "close", lpath_close },
504 { "__gc", lpath_gc },
505 { NULL, NULL }
506};
507
508///////////////////////////////////////////////////////////////////////////////
509
510static const char* rrect_type(const SkRRect& rr) {
511 switch (rr.getType()) {
512 case SkRRect::kUnknown_Type: return "unknown";
513 case SkRRect::kEmpty_Type: return "empty";
514 case SkRRect::kRect_Type: return "rect";
515 case SkRRect::kOval_Type: return "oval";
516 case SkRRect::kSimple_Type: return "simple";
517 case SkRRect::kComplex_Type: return "complex";
518 }
519 SkASSERT(!"never get here");
520 return "";
521}
522
523static int lrrect_rect(lua_State* L) {
524 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
525 return 1;
526}
527
528static int lrrect_type(lua_State* L) {
529 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
530 return 1;
531}
532
533static int lrrect_radii(lua_State* L) {
534 int corner = lua_tointeger(L, 2);
535 SkVector v;
536 if (corner < 0 || corner > 3) {
537 SkDebugf("bad corner index %d", corner);
538 v.set(0, 0);
539 } else {
540 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
541 }
542 lua_pushnumber(L, v.fX);
543 lua_pushnumber(L, v.fY);
544 return 2;
545}
546
547static int lrrect_gc(lua_State* L) {
548 get_obj<SkRRect>(L, 1)->~SkRRect();
549 return 0;
550}
551
552static const struct luaL_Reg gSkRRect_Methods[] = {
553 { "rect", lrrect_rect },
554 { "type", lrrect_type },
555 { "radii", lrrect_radii },
556 { "__gc", lrrect_gc },
557 { NULL, NULL }
558};
559
560///////////////////////////////////////////////////////////////////////////////
561
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000562static int ltypeface_gc(lua_State* L) {
563 get_ref<SkTypeface>(L, 1)->unref();
564 return 0;
565}
566
567static const struct luaL_Reg gSkTypeface_Methods[] = {
568 { "__gc", ltypeface_gc },
569 { NULL, NULL }
570};
571
572///////////////////////////////////////////////////////////////////////////////
573
reed@google.com74ce6f02013-05-22 15:13:18 +0000574class AutoCallLua {
575public:
576 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
577 lua_getglobal(L, func);
578 if (!lua_isfunction(L, -1)) {
579 int t = lua_type(L, -1);
580 SkDebugf("--- expected function %d\n", t);
581 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000582
reed@google.com74ce6f02013-05-22 15:13:18 +0000583 lua_newtable(L);
584 setfield_string(L, "verb", verb);
585 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000586
reed@google.com74ce6f02013-05-22 15:13:18 +0000587 ~AutoCallLua() {
588 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
589 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
590 }
591 lua_settop(fL, -1);
592 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000593
reed@google.com74ce6f02013-05-22 15:13:18 +0000594private:
595 lua_State* fL;
596};
597
598#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
599
600///////////////////////////////////////////////////////////////////////////////
601
reed@google.com3597b732013-05-22 20:12:50 +0000602static int lsk_newPaint(lua_State* L) {
603 push_new<SkPaint>(L);
604 return 1;
605}
606
607static int lsk_newPath(lua_State* L) {
608 push_new<SkPath>(L);
609 return 1;
610}
611
612static int lsk_newRRect(lua_State* L) {
613 SkRRect* rr = push_new<SkRRect>(L);
614 rr->setEmpty();
615 return 1;
616}
617
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000618static int lsk_newTypeface(lua_State* L) {
619 const char* name = NULL;
620 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +0000621
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000622 int count = lua_gettop(L);
623 if (count > 0 && lua_isstring(L, 1)) {
624 name = lua_tolstring(L, 1, NULL);
625 if (count > 1 && lua_isnumber(L, 2)) {
626 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
627 }
628 }
629
630 SkTypeface* face = SkTypeface::CreateFromName(name,
631 (SkTypeface::Style)style);
632// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
633 if (NULL == face) {
634 face = SkTypeface::RefDefault();
635 }
636 push_ref(L, face);
637 face->unref();
638 return 1;
639}
reed@google.com3597b732013-05-22 20:12:50 +0000640
641static void register_Sk(lua_State* L) {
642 lua_newtable(L);
643 lua_pushvalue(L, -1);
644 lua_setglobal(L, "Sk");
645 // the Sk table is still on top
646
647 setfield_function(L, "newPaint", lsk_newPaint);
648 setfield_function(L, "newPath", lsk_newPath);
649 setfield_function(L, "newRRect", lsk_newRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000650 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +0000651 lua_pop(L, 1); // pop off the Sk table
652}
653
reed@google.com74ce6f02013-05-22 15:13:18 +0000654#define REG_CLASS(L, C) \
655 do { \
reed@google.com3597b732013-05-22 20:12:50 +0000656 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +0000657 lua_pushvalue(L, -1); \
658 lua_setfield(L, -2, "__index"); \
659 luaL_setfuncs(L, g##C##_Methods, 0); \
660 lua_pop(L, 1); /* pop off the meta-table */ \
661 } while (0)
662
663void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +0000664 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +0000665 REG_CLASS(L, SkCanvas);
666 REG_CLASS(L, SkPath);
667 REG_CLASS(L, SkPaint);
668 REG_CLASS(L, SkRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000669 REG_CLASS(L, SkTypeface);
reed@google.com74ce6f02013-05-22 15:13:18 +0000670}