blob: 0ea9c9dd5d3a045daccc907e349c26a328471058 [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"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000010#include "SkData.h"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000011#include "SkDocument.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000012#include "SkImage.h"
13#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000014#include "SkPaint.h"
15#include "SkPath.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000016#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000017#include "SkRRect.h"
18#include "SkString.h"
reed@google.come3823fd2013-05-30 18:55:14 +000019#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000020
21extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000022 #include "lua.h"
23 #include "lualib.h"
24 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000025}
26
reed@google.comfd345872013-05-22 20:53:42 +000027// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000028template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000029#define DEF_MTNAME(T) \
30 template <> const char* get_mtname<T>() { \
31 return #T "_LuaMetaTableName"; \
32 }
33
34DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000035DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000036DEF_MTNAME(SkImage)
reed@google.comfd345872013-05-22 20:53:42 +000037DEF_MTNAME(SkMatrix)
38DEF_MTNAME(SkRRect)
39DEF_MTNAME(SkPath)
40DEF_MTNAME(SkPaint)
reed@google.com5fdc9832013-07-24 15:47:52 +000041DEF_MTNAME(SkShader)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000042DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000043
reed@google.com3597b732013-05-22 20:12:50 +000044template <typename T> T* push_new(lua_State* L) {
45 T* addr = (T*)lua_newuserdata(L, sizeof(T));
46 new (addr) T;
47 luaL_getmetatable(L, get_mtname<T>());
48 lua_setmetatable(L, -2);
49 return addr;
50}
reed@google.com74ce6f02013-05-22 15:13:18 +000051
52template <typename T> void push_obj(lua_State* L, const T& obj) {
53 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000054 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000055 lua_setmetatable(L, -2);
56}
57
58template <typename T> void push_ref(lua_State* L, T* ref) {
59 *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000060 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000061 lua_setmetatable(L, -2);
62}
63
64template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000065 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000066}
67
68template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000069 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000070}
71
reed@google.com88c9ec92013-05-22 15:43:21 +000072static bool lua2bool(lua_State* L, int index) {
73 return !!lua_toboolean(L, index);
74}
75
reed@google.com74ce6f02013-05-22 15:13:18 +000076///////////////////////////////////////////////////////////////////////////////
77
reed@google.com3597b732013-05-22 20:12:50 +000078SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
79 fL = luaL_newstate();
80 luaL_openlibs(fL);
81 SkLua::Load(fL);
82}
83
84SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
85
86SkLua::~SkLua() {
87 if (fWeOwnL) {
88 if (fTermCode.size() > 0) {
89 lua_getglobal(fL, fTermCode.c_str());
90 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
91 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
92 }
93 }
94 lua_close(fL);
95 }
96}
97
98bool SkLua::runCode(const char code[]) {
99 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
100 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000101 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000102 return false;
103 }
104 return true;
105}
106
107bool SkLua::runCode(const void* code, size_t size) {
108 SkString str((const char*)code, size);
109 return this->runCode(str.c_str());
110}
111
112///////////////////////////////////////////////////////////////////////////////
113
114#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
115
reed@google.com29563872013-07-10 21:23:49 +0000116static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
117 if (pred) {
118 lua_pushboolean(L, true);
119 lua_setfield(L, -2, key);
120 }
121}
122
reed@google.com74ce6f02013-05-22 15:13:18 +0000123static void setfield_string(lua_State* L, const char key[], const char value[]) {
124 lua_pushstring(L, value);
125 lua_setfield(L, -2, key);
126}
127
128static void setfield_number(lua_State* L, const char key[], double value) {
129 lua_pushnumber(L, value);
130 lua_setfield(L, -2, key);
131}
132
humper@google.com2815c192013-07-10 22:42:30 +0000133static void setfield_boolean(lua_State* L, const char key[], bool value) {
134 lua_pushboolean(L, value);
135 lua_setfield(L, -2, key);
136}
137
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000138static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
139 setfield_number(L, key, SkScalarToLua(value));
140}
141
reed@google.com3597b732013-05-22 20:12:50 +0000142static void setfield_function(lua_State* L,
143 const char key[], lua_CFunction value) {
144 lua_pushcfunction(L, value);
145 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000146}
147
reed@google.come3823fd2013-05-30 18:55:14 +0000148static void setarray_number(lua_State* L, int index, double value) {
149 lua_pushnumber(L, value);
150 lua_rawseti(L, -2, index);
151}
152
reed@google.com74ce6f02013-05-22 15:13:18 +0000153void SkLua::pushBool(bool value, const char key[]) {
154 lua_pushboolean(fL, value);
155 CHECK_SETFIELD(key);
156}
157
158void SkLua::pushString(const char str[], const char key[]) {
159 lua_pushstring(fL, str);
160 CHECK_SETFIELD(key);
161}
162
reed@google.come3823fd2013-05-30 18:55:14 +0000163void SkLua::pushString(const char str[], size_t length, const char key[]) {
164 // TODO: how to do this w/o making a copy?
165 SkString s(str, length);
166 lua_pushstring(fL, s.c_str());
167 CHECK_SETFIELD(key);
168}
169
reed@google.com74ce6f02013-05-22 15:13:18 +0000170void SkLua::pushString(const SkString& str, const char key[]) {
171 lua_pushstring(fL, str.c_str());
172 CHECK_SETFIELD(key);
173}
174
175void SkLua::pushColor(SkColor color, const char key[]) {
176 lua_newtable(fL);
177 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
178 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
179 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
180 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
181 CHECK_SETFIELD(key);
182}
183
reed@google.come3823fd2013-05-30 18:55:14 +0000184void SkLua::pushU32(uint32_t value, const char key[]) {
185 lua_pushnumber(fL, (double)value);
186 CHECK_SETFIELD(key);
187}
188
reed@google.com74ce6f02013-05-22 15:13:18 +0000189void SkLua::pushScalar(SkScalar value, const char key[]) {
190 lua_pushnumber(fL, SkScalarToLua(value));
191 CHECK_SETFIELD(key);
192}
193
reed@google.come3823fd2013-05-30 18:55:14 +0000194void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
195 lua_newtable(fL);
196 for (int i = 0; i < count; ++i) {
197 // make it base-1 to match lua convention
198 setarray_number(fL, i + 1, (double)array[i]);
199 }
200 CHECK_SETFIELD(key);
201}
202
reed@google.com74ce6f02013-05-22 15:13:18 +0000203void SkLua::pushRect(const SkRect& r, const char key[]) {
204 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000205 setfield_scalar(fL, "left", r.fLeft);
206 setfield_scalar(fL, "top", r.fTop);
207 setfield_scalar(fL, "right", r.fRight);
208 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000209 CHECK_SETFIELD(key);
210}
211
212void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
213 push_obj(fL, rr);
214 CHECK_SETFIELD(key);
215}
216
217void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
218 push_obj(fL, matrix);
219 CHECK_SETFIELD(key);
220}
221
222void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
223 push_obj(fL, paint);
224 CHECK_SETFIELD(key);
225}
226
227void SkLua::pushPath(const SkPath& path, const char key[]) {
228 push_obj(fL, path);
229 CHECK_SETFIELD(key);
230}
231
232void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
233 push_ref(fL, canvas);
234 CHECK_SETFIELD(key);
235}
236
237///////////////////////////////////////////////////////////////////////////////
238///////////////////////////////////////////////////////////////////////////////
239
240static SkScalar lua2scalar(lua_State* L, int index) {
241 SkASSERT(lua_isnumber(L, index));
242 return SkLuaToScalar(lua_tonumber(L, index));
243}
244
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000245static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
246 if (lua_isnumber(L, index)) {
247 return SkLuaToScalar(lua_tonumber(L, index));
248 } else {
249 return defaultValue;
250 }
251}
252
reed@google.com74ce6f02013-05-22 15:13:18 +0000253static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
254 SkASSERT(lua_istable(L, index));
255 lua_pushstring(L, key);
256 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000257
reed@google.com74ce6f02013-05-22 15:13:18 +0000258 SkScalar value = lua2scalar(L, -1);
259 lua_pop(L, 1);
260 return value;
261}
262
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000263static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
264 SkASSERT(lua_istable(L, index));
265 lua_pushstring(L, key);
266 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000267
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000268 SkScalar value;
269 if (lua_isnil(L, -1)) {
270 value = def;
271 } else {
272 value = lua2scalar(L, -1);
273 }
274 lua_pop(L, 1);
275 return value;
276}
277
reed@google.com74ce6f02013-05-22 15:13:18 +0000278static U8CPU unit2byte(SkScalar x) {
279 if (x <= 0) {
280 return 0;
281 } else if (x >= 1) {
282 return 255;
283 } else {
284 return SkScalarRoundToInt(x * 255);
285 }
286}
287
288static SkColor lua2color(lua_State* L, int index) {
289 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
290 unit2byte(getfield_scalar(L, index, "r")),
291 unit2byte(getfield_scalar(L, index, "g")),
292 unit2byte(getfield_scalar(L, index, "b")));
293}
294
295static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000296 rect->set(getfield_scalar_default(L, index, "left", 0),
297 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000298 getfield_scalar(L, index, "right"),
299 getfield_scalar(L, index, "bottom"));
300 return rect;
301}
302
303static int lcanvas_drawColor(lua_State* L) {
304 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
305 return 0;
306}
307
308static int lcanvas_drawRect(lua_State* L) {
309 SkRect rect;
310 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
311 *get_obj<SkPaint>(L, 3));
312 return 0;
313}
314
315static int lcanvas_drawOval(lua_State* L) {
316 SkRect rect;
317 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
318 *get_obj<SkPaint>(L, 3));
319 return 0;
320}
321
322static int lcanvas_drawCircle(lua_State* L) {
323 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
324 lua2scalar(L, 3),
325 lua2scalar(L, 4),
326 *get_obj<SkPaint>(L, 5));
327 return 0;
328}
329
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000330static int lcanvas_drawImage(lua_State* L) {
331 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
332 SkImage* image = get_ref<SkImage>(L, 2);
333 if (NULL == image) {
334 return 0;
335 }
336 SkScalar x = lua2scalar(L, 3);
337 SkScalar y = lua2scalar(L, 4);
338
339 SkPaint paint;
340 const SkPaint* paintPtr = NULL;
341 if (lua_isnumber(L, 5)) {
342 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
343 paintPtr = &paint;
344 }
345 image->draw(canvas, x, y, paintPtr);
346 return 0;
347}
348
reed@google.comfd345872013-05-22 20:53:42 +0000349static int lcanvas_drawPath(lua_State* L) {
350 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
351 *get_obj<SkPaint>(L, 3));
352 return 0;
353}
354
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000355static int lcanvas_drawText(lua_State* L) {
356 if (lua_gettop(L) < 5) {
357 return 0;
358 }
359
360 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
361 size_t len;
362 const char* text = lua_tolstring(L, 2, &len);
363 get_ref<SkCanvas>(L, 1)->drawText(text, len,
364 lua2scalar(L, 3), lua2scalar(L, 4),
365 *get_obj<SkPaint>(L, 5));
366 }
367 return 0;
368}
369
reed@google.com74ce6f02013-05-22 15:13:18 +0000370static int lcanvas_getSaveCount(lua_State* L) {
371 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
372 return 1;
373}
374
375static int lcanvas_getTotalMatrix(lua_State* L) {
376 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
377 return 1;
378}
379
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000380static int lcanvas_save(lua_State* L) {
381 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
382 return 1;
383}
384
385static int lcanvas_restore(lua_State* L) {
386 get_ref<SkCanvas>(L, 1)->restore();
387 return 0;
388}
389
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000390static int lcanvas_scale(lua_State* L) {
391 SkScalar sx = lua2scalar_def(L, 2, 1);
392 SkScalar sy = lua2scalar_def(L, 3, sx);
393 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
394 return 0;
395}
396
reed@google.com3597b732013-05-22 20:12:50 +0000397static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000398 SkScalar tx = lua2scalar_def(L, 2, 0);
399 SkScalar ty = lua2scalar_def(L, 3, 0);
400 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
401 return 0;
402}
403
404static int lcanvas_rotate(lua_State* L) {
405 SkScalar degrees = lua2scalar_def(L, 2, 0);
406 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000407 return 0;
408}
409
reed@google.com74ce6f02013-05-22 15:13:18 +0000410static int lcanvas_gc(lua_State* L) {
411 get_ref<SkCanvas>(L, 1)->unref();
412 return 0;
413}
414
415static const struct luaL_Reg gSkCanvas_Methods[] = {
416 { "drawColor", lcanvas_drawColor },
417 { "drawRect", lcanvas_drawRect },
418 { "drawOval", lcanvas_drawOval },
419 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000420 { "drawImage", lcanvas_drawImage },
reed@google.comfd345872013-05-22 20:53:42 +0000421 { "drawPath", lcanvas_drawPath },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000422 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000423 { "getSaveCount", lcanvas_getSaveCount },
424 { "getTotalMatrix", lcanvas_getTotalMatrix },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000425 { "save", lcanvas_save },
426 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000427 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000428 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000429 { "rotate", lcanvas_rotate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000430 { "__gc", lcanvas_gc },
431 { NULL, NULL }
432};
433
434///////////////////////////////////////////////////////////////////////////////
435
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000436static int ldocument_beginPage(lua_State* L) {
437 const SkRect* contentPtr = NULL;
438 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
439 lua2scalar(L, 3),
440 contentPtr));
441 return 1;
442}
443
444static int ldocument_endPage(lua_State* L) {
445 get_ref<SkDocument>(L, 1)->endPage();
446 return 0;
447}
448
449static int ldocument_close(lua_State* L) {
450 get_ref<SkDocument>(L, 1)->close();
451 return 0;
452}
453
454static int ldocument_gc(lua_State* L) {
455 get_ref<SkDocument>(L, 1)->unref();
456 return 0;
457}
458
459static const struct luaL_Reg gSkDocument_Methods[] = {
460 { "beginPage", ldocument_beginPage },
461 { "endPage", ldocument_endPage },
462 { "close", ldocument_close },
463 { "__gc", ldocument_gc },
464 { NULL, NULL }
465};
466
467///////////////////////////////////////////////////////////////////////////////
468
reed@google.com74ce6f02013-05-22 15:13:18 +0000469static int lpaint_isAntiAlias(lua_State* L) {
470 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
471 return 1;
472}
473
474static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000475 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000476 return 0;
477}
478
479static int lpaint_getColor(lua_State* L) {
480 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
481 return 1;
482}
483
484static int lpaint_setColor(lua_State* L) {
485 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
486 return 0;
487}
488
reed@google.come3823fd2013-05-30 18:55:14 +0000489static int lpaint_getTextSize(lua_State* L) {
490 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
491 return 1;
492}
493
494static int lpaint_setTextSize(lua_State* L) {
495 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
496 return 0;
497}
498
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000499static int lpaint_getTypeface(lua_State* L) {
500 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
501 return 1;
502}
503
504static int lpaint_setTypeface(lua_State* L) {
505 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
506 return 0;
507}
508
reed@google.come3823fd2013-05-30 18:55:14 +0000509static int lpaint_getFontID(lua_State* L) {
510 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
511 SkLua(L).pushU32(SkTypeface::UniqueID(face));
512 return 1;
513}
514
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000515static const struct {
516 const char* fLabel;
517 SkPaint::Align fAlign;
518} gAlignRec[] = {
519 { "left", SkPaint::kLeft_Align },
520 { "center", SkPaint::kCenter_Align },
521 { "right", SkPaint::kRight_Align },
522};
523
524static int lpaint_getTextAlign(lua_State* L) {
525 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
526 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
527 if (gAlignRec[i].fAlign == align) {
528 lua_pushstring(L, gAlignRec[i].fLabel);
529 return 1;
530 }
531 }
532 return 0;
533}
534
535static int lpaint_setTextAlign(lua_State* L) {
536 if (lua_isstring(L, 2)) {
537 size_t len;
538 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000539
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000540 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
541 if (!strcmp(gAlignRec[i].fLabel, label)) {
542 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
543 break;
544 }
545 }
546 }
547 return 0;
548}
549
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000550static int lpaint_getStroke(lua_State* L) {
551 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
552 return 1;
553}
554
555static int lpaint_setStroke(lua_State* L) {
556 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000557
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000558 if (lua_toboolean(L, 2)) {
559 style = SkPaint::kStroke_Style;
560 } else {
561 style = SkPaint::kFill_Style;
562 }
563 get_obj<SkPaint>(L, 1)->setStyle(style);
564 return 0;
565}
566
567static int lpaint_getStrokeWidth(lua_State* L) {
568 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
569 return 1;
570}
571
572static int lpaint_setStrokeWidth(lua_State* L) {
573 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
574 return 0;
575}
576
577static int lpaint_measureText(lua_State* L) {
578 if (lua_isstring(L, 2)) {
579 size_t len;
580 const char* text = lua_tolstring(L, 2, &len);
581 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
582 return 1;
583 }
584 return 0;
585}
586
587struct FontMetrics {
588 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
589 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
590 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
591 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
592 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
593 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
594 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
595 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
596 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
597};
598
599static int lpaint_getFontMetrics(lua_State* L) {
600 SkPaint::FontMetrics fm;
601 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000602
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000603 lua_newtable(L);
604 setfield_scalar(L, "top", fm.fTop);
605 setfield_scalar(L, "ascent", fm.fAscent);
606 setfield_scalar(L, "descent", fm.fDescent);
607 setfield_scalar(L, "bottom", fm.fBottom);
608 setfield_scalar(L, "leading", fm.fLeading);
609 SkLua(L).pushScalar(height);
610 return 2;
611}
612
reed@google.com29563872013-07-10 21:23:49 +0000613static int lpaint_getEffects(lua_State* L) {
614 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000615
reed@google.com29563872013-07-10 21:23:49 +0000616 lua_newtable(L);
617 setfield_bool_if(L, "looper", !!paint->getLooper());
618 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
619 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
620 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
621 setfield_bool_if(L, "shader", !!paint->getShader());
622 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
623 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
624 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
625 return 1;
626}
627
reed@google.com5fdc9832013-07-24 15:47:52 +0000628static int lpaint_getShader(lua_State* L) {
629 const SkPaint* paint = get_obj<SkPaint>(L, 1);
630 SkShader* shader = paint->getShader();
631 if (shader) {
632 push_ref(L, shader);
633 return 1;
634 }
635 return 0;
636}
637
reed@google.com74ce6f02013-05-22 15:13:18 +0000638static int lpaint_gc(lua_State* L) {
639 get_obj<SkPaint>(L, 1)->~SkPaint();
640 return 0;
641}
642
643static const struct luaL_Reg gSkPaint_Methods[] = {
644 { "isAntiAlias", lpaint_isAntiAlias },
645 { "setAntiAlias", lpaint_setAntiAlias },
646 { "getColor", lpaint_getColor },
647 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +0000648 { "getTextSize", lpaint_getTextSize },
649 { "setTextSize", lpaint_setTextSize },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000650 { "getTypeface", lpaint_getTypeface },
651 { "setTypeface", lpaint_setTypeface },
reed@google.come3823fd2013-05-30 18:55:14 +0000652 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000653 { "getTextAlign", lpaint_getTextAlign },
654 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000655 { "getStroke", lpaint_getStroke },
656 { "setStroke", lpaint_setStroke },
657 { "getStrokeWidth", lpaint_getStrokeWidth },
658 { "setStrokeWidth", lpaint_setStrokeWidth },
659 { "measureText", lpaint_measureText },
660 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +0000661 { "getEffects", lpaint_getEffects },
reed@google.com5fdc9832013-07-24 15:47:52 +0000662 { "getShader", lpaint_getShader },
reed@google.com74ce6f02013-05-22 15:13:18 +0000663 { "__gc", lpaint_gc },
664 { NULL, NULL }
665};
666
667///////////////////////////////////////////////////////////////////////////////
668
reed@google.com5fdc9832013-07-24 15:47:52 +0000669static const char* mode2string(SkShader::TileMode mode) {
670 static const char* gNames[] = { "clamp", "repeat", "mirror" };
671 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
672 return gNames[mode];
673}
674
675static const char* gradtype2string(SkShader::GradientType t) {
676 static const char* gNames[] = {
677 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
678 };
679 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
680 return gNames[t];
681}
682
683static int lshader_isOpaque(lua_State* L) {
684 SkShader* shader = get_ref<SkShader>(L, 1);
685 return shader && shader->isOpaque();
686}
687
688static int lshader_asABitmap(lua_State* L) {
689 SkShader* shader = get_ref<SkShader>(L, 1);
690 if (shader) {
691 SkBitmap bm;
692 SkMatrix matrix;
693 SkShader::TileMode modes[2];
694 switch (shader->asABitmap(&bm, &matrix, modes)) {
695 case SkShader::kDefault_BitmapType:
696 lua_newtable(L);
697 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
698 setfield_number(L, "width", bm.width());
699 setfield_number(L, "height", bm.height());
700 setfield_string(L, "tileX", mode2string(modes[0]));
701 setfield_string(L, "tileY", mode2string(modes[1]));
702 return 1;
703 default:
704 break;
705 }
706 }
707 return 0;
708}
709
710static int lshader_asAGradient(lua_State* L) {
711 SkShader* shader = get_ref<SkShader>(L, 1);
712 if (shader) {
713 SkShader::GradientInfo info;
714 sk_bzero(&info, sizeof(info));
715 SkShader::GradientType t = shader->asAGradient(&info);
716 if (SkShader::kNone_GradientType != t) {
717 lua_newtable(L);
718 setfield_string(L, "type", gradtype2string(t));
719 setfield_number(L, "colorCount", info.fColorCount);
720 setfield_string(L, "tile", mode2string(info.fTileMode));
721 return 1;
722 }
723 }
724 return 0;
725}
726
727static int lshader_gc(lua_State* L) {
728 get_ref<SkShader>(L, 1)->unref();
729 return 0;
730}
731
732static const struct luaL_Reg gSkShader_Methods[] = {
733 { "isOpaque", lshader_isOpaque },
734 { "asABitmap", lshader_asABitmap },
735 { "asAGradient", lshader_asAGradient },
736 { "__gc", lshader_gc },
737 { NULL, NULL }
738};
739
740///////////////////////////////////////////////////////////////////////////////
741
humper@google.com2815c192013-07-10 22:42:30 +0000742static int lmatrix_getType(lua_State* L) {
743 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000744
humper@google.com2815c192013-07-10 22:42:30 +0000745 lua_newtable(L);
746 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
747 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
748 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
749 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
750 return 1;
751}
752
753static const struct luaL_Reg gSkMatrix_Methods[] = {
754 { "getType", lmatrix_getType },
755 { NULL, NULL }
756};
757
758///////////////////////////////////////////////////////////////////////////////
759
reed@google.com74ce6f02013-05-22 15:13:18 +0000760static int lpath_getBounds(lua_State* L) {
761 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
762 return 1;
763}
764
765static int lpath_isEmpty(lua_State* L) {
766 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
767 return 1;
768}
769
770static int lpath_isRect(lua_State* L) {
771 SkRect r;
772 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
773 int ret_count = 1;
774 lua_pushboolean(L, pred);
775 if (pred) {
776 SkLua(L).pushRect(r);
777 ret_count += 1;
778 }
779 return ret_count;
780}
781
782static const char* dir2string(SkPath::Direction dir) {
783 static const char* gStr[] = {
784 "unknown", "cw", "ccw"
785 };
786 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
787 return gStr[dir];
788}
789
790static int lpath_isNestedRects(lua_State* L) {
791 SkRect rects[2];
792 SkPath::Direction dirs[2];
793 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
794 int ret_count = 1;
795 lua_pushboolean(L, pred);
796 if (pred) {
797 SkLua lua(L);
798 lua.pushRect(rects[0]);
799 lua.pushRect(rects[1]);
800 lua_pushstring(L, dir2string(dirs[0]));
801 lua_pushstring(L, dir2string(dirs[0]));
802 ret_count += 4;
803 }
804 return ret_count;
805}
806
807static int lpath_reset(lua_State* L) {
808 get_obj<SkPath>(L, 1)->reset();
809 return 0;
810}
811
812static int lpath_moveTo(lua_State* L) {
813 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
814 return 0;
815}
816
817static int lpath_lineTo(lua_State* L) {
818 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
819 return 0;
820}
821
822static int lpath_quadTo(lua_State* L) {
823 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
824 lua2scalar(L, 4), lua2scalar(L, 5));
825 return 0;
826}
827
828static int lpath_cubicTo(lua_State* L) {
829 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
830 lua2scalar(L, 4), lua2scalar(L, 5),
831 lua2scalar(L, 6), lua2scalar(L, 7));
832 return 0;
833}
834
835static int lpath_close(lua_State* L) {
836 get_obj<SkPath>(L, 1)->close();
837 return 0;
838}
839
840static int lpath_gc(lua_State* L) {
841 get_obj<SkPath>(L, 1)->~SkPath();
842 return 0;
843}
844
845static const struct luaL_Reg gSkPath_Methods[] = {
846 { "getBounds", lpath_getBounds },
847 { "isEmpty", lpath_isEmpty },
848 { "isRect", lpath_isRect },
849 { "isNestedRects", lpath_isNestedRects },
850 { "reset", lpath_reset },
851 { "moveTo", lpath_moveTo },
852 { "lineTo", lpath_lineTo },
853 { "quadTo", lpath_quadTo },
854 { "cubicTo", lpath_cubicTo },
855 { "close", lpath_close },
856 { "__gc", lpath_gc },
857 { NULL, NULL }
858};
859
860///////////////////////////////////////////////////////////////////////////////
861
862static const char* rrect_type(const SkRRect& rr) {
863 switch (rr.getType()) {
864 case SkRRect::kUnknown_Type: return "unknown";
865 case SkRRect::kEmpty_Type: return "empty";
866 case SkRRect::kRect_Type: return "rect";
867 case SkRRect::kOval_Type: return "oval";
868 case SkRRect::kSimple_Type: return "simple";
869 case SkRRect::kComplex_Type: return "complex";
870 }
871 SkASSERT(!"never get here");
872 return "";
873}
874
875static int lrrect_rect(lua_State* L) {
876 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
877 return 1;
878}
879
880static int lrrect_type(lua_State* L) {
881 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
882 return 1;
883}
884
885static int lrrect_radii(lua_State* L) {
886 int corner = lua_tointeger(L, 2);
887 SkVector v;
888 if (corner < 0 || corner > 3) {
889 SkDebugf("bad corner index %d", corner);
890 v.set(0, 0);
891 } else {
892 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
893 }
894 lua_pushnumber(L, v.fX);
895 lua_pushnumber(L, v.fY);
896 return 2;
897}
898
899static int lrrect_gc(lua_State* L) {
900 get_obj<SkRRect>(L, 1)->~SkRRect();
901 return 0;
902}
903
904static const struct luaL_Reg gSkRRect_Methods[] = {
905 { "rect", lrrect_rect },
906 { "type", lrrect_type },
907 { "radii", lrrect_radii },
908 { "__gc", lrrect_gc },
909 { NULL, NULL }
910};
911
912///////////////////////////////////////////////////////////////////////////////
913
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000914static int limage_width(lua_State* L) {
915 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
916 return 1;
917}
918
919static int limage_height(lua_State* L) {
920 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
921 return 1;
922}
923
924static int limage_gc(lua_State* L) {
925 get_ref<SkImage>(L, 1)->unref();
926 return 0;
927}
928
929static const struct luaL_Reg gSkImage_Methods[] = {
930 { "width", limage_width },
931 { "height", limage_height },
932 { "__gc", limage_gc },
933 { NULL, NULL }
934};
935
936///////////////////////////////////////////////////////////////////////////////
937
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000938static int ltypeface_gc(lua_State* L) {
939 get_ref<SkTypeface>(L, 1)->unref();
940 return 0;
941}
942
943static const struct luaL_Reg gSkTypeface_Methods[] = {
944 { "__gc", ltypeface_gc },
945 { NULL, NULL }
946};
947
948///////////////////////////////////////////////////////////////////////////////
949
reed@google.com74ce6f02013-05-22 15:13:18 +0000950class AutoCallLua {
951public:
952 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
953 lua_getglobal(L, func);
954 if (!lua_isfunction(L, -1)) {
955 int t = lua_type(L, -1);
956 SkDebugf("--- expected function %d\n", t);
957 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000958
reed@google.com74ce6f02013-05-22 15:13:18 +0000959 lua_newtable(L);
960 setfield_string(L, "verb", verb);
961 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000962
reed@google.com74ce6f02013-05-22 15:13:18 +0000963 ~AutoCallLua() {
964 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
965 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
966 }
967 lua_settop(fL, -1);
968 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000969
reed@google.com74ce6f02013-05-22 15:13:18 +0000970private:
971 lua_State* fL;
972};
973
974#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
975
976///////////////////////////////////////////////////////////////////////////////
977
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000978static int lsk_newDocumentPDF(lua_State* L) {
979 const char* file = NULL;
980 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
981 file = lua_tolstring(L, 1, NULL);
982 }
983
984 SkDocument* doc = SkDocument::CreatePDF(file);
985 if (NULL == doc) {
986 // do I need to push a nil on the stack and return 1?
987 return 0;
988 } else {
989 push_ref(L, doc);
990 doc->unref();
991 return 1;
992 }
993}
994
reed@google.com3597b732013-05-22 20:12:50 +0000995static int lsk_newPaint(lua_State* L) {
996 push_new<SkPaint>(L);
997 return 1;
998}
999
1000static int lsk_newPath(lua_State* L) {
1001 push_new<SkPath>(L);
1002 return 1;
1003}
1004
1005static int lsk_newRRect(lua_State* L) {
1006 SkRRect* rr = push_new<SkRRect>(L);
1007 rr->setEmpty();
1008 return 1;
1009}
1010
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001011static int lsk_newTypeface(lua_State* L) {
1012 const char* name = NULL;
1013 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001014
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001015 int count = lua_gettop(L);
1016 if (count > 0 && lua_isstring(L, 1)) {
1017 name = lua_tolstring(L, 1, NULL);
1018 if (count > 1 && lua_isnumber(L, 2)) {
1019 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1020 }
1021 }
1022
1023 SkTypeface* face = SkTypeface::CreateFromName(name,
1024 (SkTypeface::Style)style);
1025// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1026 if (NULL == face) {
1027 face = SkTypeface::RefDefault();
1028 }
1029 push_ref(L, face);
1030 face->unref();
1031 return 1;
1032}
reed@google.com3597b732013-05-22 20:12:50 +00001033
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001034static int lsk_loadImage(lua_State* L) {
1035 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1036 const char* name = lua_tolstring(L, 1, NULL);
1037 SkAutoDataUnref data(SkData::NewFromFileName(name));
1038 if (data.get()) {
1039 SkImage* image = SkImage::NewEncodedData(data.get());
1040 if (image) {
1041 push_ref(L, image);
1042 image->unref();
1043 return 1;
1044 }
1045 }
1046 }
1047 return 0;
1048}
1049
reed@google.com3597b732013-05-22 20:12:50 +00001050static void register_Sk(lua_State* L) {
1051 lua_newtable(L);
1052 lua_pushvalue(L, -1);
1053 lua_setglobal(L, "Sk");
1054 // the Sk table is still on top
1055
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001056 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001057 setfield_function(L, "loadImage", lsk_loadImage);
reed@google.com3597b732013-05-22 20:12:50 +00001058 setfield_function(L, "newPaint", lsk_newPaint);
1059 setfield_function(L, "newPath", lsk_newPath);
1060 setfield_function(L, "newRRect", lsk_newRRect);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001061 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001062 lua_pop(L, 1); // pop off the Sk table
1063}
1064
reed@google.com74ce6f02013-05-22 15:13:18 +00001065#define REG_CLASS(L, C) \
1066 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001067 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001068 lua_pushvalue(L, -1); \
1069 lua_setfield(L, -2, "__index"); \
1070 luaL_setfuncs(L, g##C##_Methods, 0); \
1071 lua_pop(L, 1); /* pop off the meta-table */ \
1072 } while (0)
1073
1074void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001075 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001076 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001077 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001078 REG_CLASS(L, SkImage);
reed@google.com74ce6f02013-05-22 15:13:18 +00001079 REG_CLASS(L, SkPath);
1080 REG_CLASS(L, SkPaint);
1081 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001082 REG_CLASS(L, SkShader);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001083 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001084 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001085}
zachr@google.com28c27c82013-06-20 17:15:05 +00001086
reed@google.com7bce9982013-06-20 17:40:21 +00001087extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001088extern "C" int luaopen_skia(lua_State* L) {
1089 SkLua::Load(L);
1090 return 0;
1091}