blob: 2370b9925c59a82f43a6d86971dcc0164e035df9 [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
reed468b1812014-10-19 11:42:54 -070014#include "SkBlurImageFilter.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000015#include "SkCanvas.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000016#include "SkData.h"
piotaixr4bcc2022014-09-17 14:33:30 -070017#include "SkDecodingImageGenerator.h"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000018#include "SkDocument.h"
reed9fbc3f32014-10-21 07:12:58 -070019#include "SkGradientShader.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000020#include "SkImage.h"
21#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000022#include "SkPaint.h"
23#include "SkPath.h"
reed96affcd2014-10-13 12:38:04 -070024#include "SkPictureRecorder.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000025#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000026#include "SkRRect.h"
27#include "SkString.h"
reed485557f2014-10-12 10:36:47 -070028#include "SkSurface.h"
fmalitab7425172014-08-26 07:56:44 -070029#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000030#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000031
32extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000033 #include "lua.h"
34 #include "lualib.h"
35 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000036}
37
reed@google.comfd345872013-05-22 20:53:42 +000038// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000039template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000040#define DEF_MTNAME(T) \
41 template <> const char* get_mtname<T>() { \
42 return #T "_LuaMetaTableName"; \
43 }
44
45DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000046DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000047DEF_MTNAME(SkImage)
reed468b1812014-10-19 11:42:54 -070048DEF_MTNAME(SkImageFilter)
reed@google.comfd345872013-05-22 20:53:42 +000049DEF_MTNAME(SkMatrix)
50DEF_MTNAME(SkRRect)
51DEF_MTNAME(SkPath)
52DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000053DEF_MTNAME(SkPathEffect)
reed96affcd2014-10-13 12:38:04 -070054DEF_MTNAME(SkPicture)
55DEF_MTNAME(SkPictureRecorder)
reed@google.com5fdc9832013-07-24 15:47:52 +000056DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070057DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070058DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000059DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000060
reed@google.com3597b732013-05-22 20:12:50 +000061template <typename T> T* push_new(lua_State* L) {
62 T* addr = (T*)lua_newuserdata(L, sizeof(T));
63 new (addr) T;
64 luaL_getmetatable(L, get_mtname<T>());
65 lua_setmetatable(L, -2);
66 return addr;
67}
reed@google.com74ce6f02013-05-22 15:13:18 +000068
69template <typename T> void push_obj(lua_State* L, const T& obj) {
70 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000071 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000072 lua_setmetatable(L, -2);
73}
74
reed9fbc3f32014-10-21 07:12:58 -070075template <typename T> T* push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000076 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000077 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000078 lua_setmetatable(L, -2);
reed9fbc3f32014-10-21 07:12:58 -070079 return ref;
reed@google.com74ce6f02013-05-22 15:13:18 +000080}
81
82template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000083 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000084}
85
86template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000087 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000088}
89
reed@google.com88c9ec92013-05-22 15:43:21 +000090static bool lua2bool(lua_State* L, int index) {
91 return !!lua_toboolean(L, index);
92}
93
reed@google.com74ce6f02013-05-22 15:13:18 +000094///////////////////////////////////////////////////////////////////////////////
95
reed@google.com3597b732013-05-22 20:12:50 +000096SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
97 fL = luaL_newstate();
98 luaL_openlibs(fL);
99 SkLua::Load(fL);
100}
101
102SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
103
104SkLua::~SkLua() {
105 if (fWeOwnL) {
106 if (fTermCode.size() > 0) {
107 lua_getglobal(fL, fTermCode.c_str());
108 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
109 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
110 }
111 }
112 lua_close(fL);
113 }
114}
115
116bool SkLua::runCode(const char code[]) {
117 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
118 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000119 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000120 return false;
121 }
122 return true;
123}
124
125bool SkLua::runCode(const void* code, size_t size) {
126 SkString str((const char*)code, size);
127 return this->runCode(str.c_str());
128}
129
130///////////////////////////////////////////////////////////////////////////////
131
132#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
133
reed@google.com29563872013-07-10 21:23:49 +0000134static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
135 if (pred) {
136 lua_pushboolean(L, true);
137 lua_setfield(L, -2, key);
138 }
139}
140
reed@google.com74ce6f02013-05-22 15:13:18 +0000141static void setfield_string(lua_State* L, const char key[], const char value[]) {
142 lua_pushstring(L, value);
143 lua_setfield(L, -2, key);
144}
145
146static void setfield_number(lua_State* L, const char key[], double value) {
147 lua_pushnumber(L, value);
148 lua_setfield(L, -2, key);
149}
150
humper@google.com2815c192013-07-10 22:42:30 +0000151static void setfield_boolean(lua_State* L, const char key[], bool value) {
152 lua_pushboolean(L, value);
153 lua_setfield(L, -2, key);
154}
155
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000156static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
157 setfield_number(L, key, SkScalarToLua(value));
158}
159
reed@google.com3597b732013-05-22 20:12:50 +0000160static void setfield_function(lua_State* L,
161 const char key[], lua_CFunction value) {
162 lua_pushcfunction(L, value);
163 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000164}
165
reed7a72c672014-11-07 10:23:55 -0800166static int lua2int_def(lua_State* L, int index, int defaultValue) {
167 if (lua_isnumber(L, index)) {
168 return (int)lua_tonumber(L, index);
169 } else {
170 return defaultValue;
171 }
172}
173
174static SkScalar lua2scalar(lua_State* L, int index) {
175 SkASSERT(lua_isnumber(L, index));
176 return SkLuaToScalar(lua_tonumber(L, index));
177}
178
179static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
180 if (lua_isnumber(L, index)) {
181 return SkLuaToScalar(lua_tonumber(L, index));
182 } else {
183 return defaultValue;
184 }
185}
186
187static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
188 SkASSERT(lua_istable(L, stackIndex));
189 lua_rawgeti(L, stackIndex, arrayIndex);
mtklein8aacf202014-12-18 13:29:54 -0800190
reed7a72c672014-11-07 10:23:55 -0800191 SkScalar value = lua2scalar(L, -1);
192 lua_pop(L, 1);
193 return value;
194}
195
196static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
197 for (int i = 0; i < count; ++i) {
198 dst[i] = getarray_scalar(L, stackIndex, i + 1);
199 }
200}
201
202static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
203 getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
204}
205
reed@google.come3823fd2013-05-30 18:55:14 +0000206static void setarray_number(lua_State* L, int index, double value) {
207 lua_pushnumber(L, value);
208 lua_rawseti(L, -2, index);
209}
210
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000211static void setarray_scalar(lua_State* L, int index, SkScalar value) {
212 setarray_number(L, index, SkScalarToLua(value));
213}
214
reed@google.com74ce6f02013-05-22 15:13:18 +0000215void SkLua::pushBool(bool value, const char key[]) {
216 lua_pushboolean(fL, value);
217 CHECK_SETFIELD(key);
218}
219
220void SkLua::pushString(const char str[], const char key[]) {
221 lua_pushstring(fL, str);
222 CHECK_SETFIELD(key);
223}
224
reed@google.come3823fd2013-05-30 18:55:14 +0000225void SkLua::pushString(const char str[], size_t length, const char key[]) {
226 // TODO: how to do this w/o making a copy?
227 SkString s(str, length);
228 lua_pushstring(fL, s.c_str());
229 CHECK_SETFIELD(key);
230}
231
reed@google.com74ce6f02013-05-22 15:13:18 +0000232void SkLua::pushString(const SkString& str, const char key[]) {
233 lua_pushstring(fL, str.c_str());
234 CHECK_SETFIELD(key);
235}
236
237void SkLua::pushColor(SkColor color, const char key[]) {
238 lua_newtable(fL);
239 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
240 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
241 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
242 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
243 CHECK_SETFIELD(key);
244}
245
reed@google.come3823fd2013-05-30 18:55:14 +0000246void SkLua::pushU32(uint32_t value, const char key[]) {
247 lua_pushnumber(fL, (double)value);
248 CHECK_SETFIELD(key);
249}
250
reed@google.com74ce6f02013-05-22 15:13:18 +0000251void SkLua::pushScalar(SkScalar value, const char key[]) {
252 lua_pushnumber(fL, SkScalarToLua(value));
253 CHECK_SETFIELD(key);
254}
255
reed@google.come3823fd2013-05-30 18:55:14 +0000256void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
257 lua_newtable(fL);
258 for (int i = 0; i < count; ++i) {
259 // make it base-1 to match lua convention
260 setarray_number(fL, i + 1, (double)array[i]);
261 }
262 CHECK_SETFIELD(key);
263}
264
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000265void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
266 lua_newtable(fL);
267 for (int i = 0; i < count; ++i) {
268 // make it base-1 to match lua convention
269 lua_newtable(fL);
270 this->pushScalar(array[i].fX, "x");
271 this->pushScalar(array[i].fY, "y");
272 lua_rawseti(fL, -2, i + 1);
273 }
274 CHECK_SETFIELD(key);
275}
276
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000277void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
278 lua_newtable(fL);
279 for (int i = 0; i < count; ++i) {
280 // make it base-1 to match lua convention
281 setarray_scalar(fL, i + 1, array[i]);
282 }
283 CHECK_SETFIELD(key);
284}
285
reed@google.com74ce6f02013-05-22 15:13:18 +0000286void SkLua::pushRect(const SkRect& r, const char key[]) {
287 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000288 setfield_scalar(fL, "left", r.fLeft);
289 setfield_scalar(fL, "top", r.fTop);
290 setfield_scalar(fL, "right", r.fRight);
291 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000292 CHECK_SETFIELD(key);
293}
294
295void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
296 push_obj(fL, rr);
297 CHECK_SETFIELD(key);
298}
299
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000300void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
301 lua_newtable(fL);
302 setfield_scalar(fL, "phase", info.fPhase);
303 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
304 CHECK_SETFIELD(key);
305}
306
307
reed@google.com74ce6f02013-05-22 15:13:18 +0000308void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
309 push_obj(fL, matrix);
310 CHECK_SETFIELD(key);
311}
312
313void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
314 push_obj(fL, paint);
315 CHECK_SETFIELD(key);
316}
317
318void SkLua::pushPath(const SkPath& path, const char key[]) {
319 push_obj(fL, path);
320 CHECK_SETFIELD(key);
321}
322
323void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
324 push_ref(fL, canvas);
325 CHECK_SETFIELD(key);
326}
327
fmalitab7425172014-08-26 07:56:44 -0700328void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
329 push_ref(fL, const_cast<SkTextBlob*>(blob));
330 CHECK_SETFIELD(key);
331}
332
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000333static const char* element_type(SkClipStack::Element::Type type) {
334 switch (type) {
335 case SkClipStack::Element::kEmpty_Type:
336 return "empty";
337 case SkClipStack::Element::kRect_Type:
338 return "rect";
339 case SkClipStack::Element::kRRect_Type:
340 return "rrect";
341 case SkClipStack::Element::kPath_Type:
342 return "path";
343 }
344 return "unknown";
345}
346
347static const char* region_op(SkRegion::Op op) {
348 switch (op) {
349 case SkRegion::kDifference_Op:
350 return "difference";
351 case SkRegion::kIntersect_Op:
352 return "intersect";
353 case SkRegion::kUnion_Op:
354 return "union";
355 case SkRegion::kXOR_Op:
356 return "xor";
357 case SkRegion::kReverseDifference_Op:
358 return "reverse-difference";
359 case SkRegion::kReplace_Op:
360 return "replace";
361 }
362 return "unknown";
363}
364
365void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
366 lua_newtable(fL);
367 SkClipStack::B2TIter iter(stack);
368 const SkClipStack::Element* element;
369 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700370 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000371 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000372 lua_rawseti(fL, -2, ++i);
373 }
374 CHECK_SETFIELD(key);
375}
376
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000377void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
378 lua_newtable(fL);
379 SkClipStack::Element::Type type = element.getType();
380 this->pushString(element_type(type), "type");
381 switch (type) {
382 case SkClipStack::Element::kEmpty_Type:
383 break;
384 case SkClipStack::Element::kRect_Type:
385 this->pushRect(element.getRect(), "rect");
386 break;
387 case SkClipStack::Element::kRRect_Type:
388 this->pushRRect(element.getRRect(), "rrect");
389 break;
390 case SkClipStack::Element::kPath_Type:
391 this->pushPath(element.getPath(), "path");
392 break;
393 }
394 this->pushString(region_op(element.getOp()), "op");
395 this->pushBool(element.isAA(), "aa");
396 CHECK_SETFIELD(key);
397}
398
399
reed@google.com74ce6f02013-05-22 15:13:18 +0000400///////////////////////////////////////////////////////////////////////////////
401///////////////////////////////////////////////////////////////////////////////
402
reed@google.com74ce6f02013-05-22 15:13:18 +0000403static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
404 SkASSERT(lua_istable(L, index));
405 lua_pushstring(L, key);
406 lua_gettable(L, index);
mtklein8aacf202014-12-18 13:29:54 -0800407
reed@google.com74ce6f02013-05-22 15:13:18 +0000408 SkScalar value = lua2scalar(L, -1);
409 lua_pop(L, 1);
410 return value;
411}
412
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000413static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
414 SkASSERT(lua_istable(L, index));
415 lua_pushstring(L, key);
416 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000417
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000418 SkScalar value;
419 if (lua_isnil(L, -1)) {
420 value = def;
421 } else {
422 value = lua2scalar(L, -1);
423 }
424 lua_pop(L, 1);
425 return value;
426}
427
reed468b1812014-10-19 11:42:54 -0700428static SkScalar byte2unit(U8CPU byte) {
429 return byte / 255.0f;
430}
431
reed@google.com74ce6f02013-05-22 15:13:18 +0000432static U8CPU unit2byte(SkScalar x) {
433 if (x <= 0) {
434 return 0;
435 } else if (x >= 1) {
436 return 255;
437 } else {
438 return SkScalarRoundToInt(x * 255);
439 }
440}
441
442static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700443 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
444 unit2byte(getfield_scalar_default(L, index, "r", 0)),
445 unit2byte(getfield_scalar_default(L, index, "g", 0)),
446 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000447}
448
449static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000450 rect->set(getfield_scalar_default(L, index, "left", 0),
451 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000452 getfield_scalar(L, index, "right"),
453 getfield_scalar(L, index, "bottom"));
454 return rect;
455}
456
reedf355df52014-10-12 12:18:40 -0700457static int lcanvas_clear(lua_State* L) {
458 get_ref<SkCanvas>(L, 1)->clear(0);
459 return 0;
460}
461
reed@google.com74ce6f02013-05-22 15:13:18 +0000462static int lcanvas_drawColor(lua_State* L) {
463 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
464 return 0;
465}
466
reed9fbc3f32014-10-21 07:12:58 -0700467static int lcanvas_drawPaint(lua_State* L) {
468 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
469 return 0;
470}
471
reed@google.com74ce6f02013-05-22 15:13:18 +0000472static int lcanvas_drawRect(lua_State* L) {
473 SkRect rect;
reed7a72c672014-11-07 10:23:55 -0800474 lua2rect(L, 2, &rect);
475 const SkPaint* paint = get_obj<SkPaint>(L, 3);
476 get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
reed@google.com74ce6f02013-05-22 15:13:18 +0000477 return 0;
478}
479
480static int lcanvas_drawOval(lua_State* L) {
481 SkRect rect;
482 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
483 *get_obj<SkPaint>(L, 3));
484 return 0;
485}
486
487static int lcanvas_drawCircle(lua_State* L) {
488 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
489 lua2scalar(L, 3),
490 lua2scalar(L, 4),
491 *get_obj<SkPaint>(L, 5));
492 return 0;
493}
494
reed485557f2014-10-12 10:36:47 -0700495static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
496 if (lua_isnumber(L, index)) {
497 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
498 return paint;
reedf355df52014-10-12 12:18:40 -0700499 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700500 const SkPaint* ptr = get_obj<SkPaint>(L, index);
501 if (ptr) {
502 *paint = *ptr;
503 return paint;
504 }
505 }
506 return NULL;
507}
508
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000509static int lcanvas_drawImage(lua_State* L) {
510 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
511 SkImage* image = get_ref<SkImage>(L, 2);
512 if (NULL == image) {
513 return 0;
514 }
515 SkScalar x = lua2scalar(L, 3);
516 SkScalar y = lua2scalar(L, 4);
517
518 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700519 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000520 return 0;
521}
522
reedba5fb932014-10-10 15:28:19 -0700523static int lcanvas_drawImageRect(lua_State* L) {
524 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
525 SkImage* image = get_ref<SkImage>(L, 2);
526 if (NULL == image) {
527 return 0;
528 }
529
530 SkRect srcR, dstR;
531 SkRect* srcRPtr = NULL;
532 if (!lua_isnil(L, 3)) {
533 srcRPtr = lua2rect(L, 3, &srcR);
534 }
535 lua2rect(L, 4, &dstR);
mtklein8aacf202014-12-18 13:29:54 -0800536
reedba5fb932014-10-10 15:28:19 -0700537 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700538 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700539 return 0;
540}
541
reed7a72c672014-11-07 10:23:55 -0800542static int lcanvas_drawPatch(lua_State* L) {
543 SkPoint cubics[12];
544 SkColor colorStorage[4];
545 SkPoint texStorage[4];
546
547 const SkColor* colors = NULL;
548 const SkPoint* texs = NULL;
549
550 getarray_points(L, 2, cubics, 12);
551
552 colorStorage[0] = SK_ColorRED;
553 colorStorage[1] = SK_ColorGREEN;
554 colorStorage[2] = SK_ColorBLUE;
555 colorStorage[3] = SK_ColorGRAY;
556
557 if (lua_isnil(L, 4)) {
558 colors = colorStorage;
559 } else {
560 getarray_points(L, 4, texStorage, 4);
561 texs = texStorage;
562 }
563
564 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, NULL, *get_obj<SkPaint>(L, 5));
565 return 0;
566}
567
reed@google.comfd345872013-05-22 20:53:42 +0000568static int lcanvas_drawPath(lua_State* L) {
569 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
570 *get_obj<SkPaint>(L, 3));
571 return 0;
572}
573
reed96affcd2014-10-13 12:38:04 -0700574// drawPicture(pic, x, y, paint)
575static int lcanvas_drawPicture(lua_State* L) {
576 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
577 SkPicture* picture = get_ref<SkPicture>(L, 2);
578 SkScalar x = lua2scalar_def(L, 3, 0);
579 SkScalar y = lua2scalar_def(L, 4, 0);
580 SkMatrix matrix, *matrixPtr = NULL;
581 if (x || y) {
582 matrix.setTranslate(x, y);
583 matrixPtr = &matrix;
584 }
585 SkPaint paint;
586 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
587 return 0;
588}
589
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000590static int lcanvas_drawText(lua_State* L) {
591 if (lua_gettop(L) < 5) {
592 return 0;
593 }
594
595 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
596 size_t len;
597 const char* text = lua_tolstring(L, 2, &len);
598 get_ref<SkCanvas>(L, 1)->drawText(text, len,
599 lua2scalar(L, 3), lua2scalar(L, 4),
600 *get_obj<SkPaint>(L, 5));
601 }
602 return 0;
603}
604
reed1b6ab442014-11-03 19:55:41 -0800605static int lcanvas_drawTextBlob(lua_State* L) {
606 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
607 SkScalar x = lua2scalar(L, 3);
608 SkScalar y = lua2scalar(L, 4);
609 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
610 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
611 return 0;
612}
613
reed@google.com74ce6f02013-05-22 15:13:18 +0000614static int lcanvas_getSaveCount(lua_State* L) {
615 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
616 return 1;
617}
618
619static int lcanvas_getTotalMatrix(lua_State* L) {
620 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
621 return 1;
622}
623
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000624static int lcanvas_getClipStack(lua_State* L) {
625 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
626 return 1;
627}
628
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000629int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
630#if SK_SUPPORT_GPU
631 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
632 SkISize layerSize = canvas->getTopLayerSize();
633 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
634 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
635 layerSize.fWidth, layerSize.fHeight);
636
637 GrReducedClip::ElementList elements;
638 GrReducedClip::InitialState initialState;
639 int32_t genID;
640 SkIRect resultBounds;
641
642 const SkClipStack& stack = *canvas->getClipStack();
643
644 GrReducedClip::ReduceClipStack(stack,
645 queryBounds,
646 &elements,
647 &genID,
648 &initialState,
649 &resultBounds,
650 NULL);
651
652 GrReducedClip::ElementList::Iter iter(elements);
653 int i = 0;
654 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700655 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000656 SkLua(L).pushClipStackElement(*iter.get());
657 iter.next();
658 lua_rawseti(L, -2, ++i);
659 }
660 // Currently this only returns the element list to lua, not the initial state or result bounds.
661 // It could return these as additional items on the lua stack.
662 return 1;
663#else
664 return 0;
665#endif
666}
667
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000668static int lcanvas_save(lua_State* L) {
669 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
670 return 1;
671}
672
reed86217d82014-10-25 20:44:40 -0700673static int lcanvas_saveLayer(lua_State* L) {
674 SkPaint paint;
675 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(NULL, lua2OptionalPaint(L, 2, &paint)));
676 return 1;
677}
678
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000679static int lcanvas_restore(lua_State* L) {
680 get_ref<SkCanvas>(L, 1)->restore();
681 return 0;
682}
683
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000684static int lcanvas_scale(lua_State* L) {
685 SkScalar sx = lua2scalar_def(L, 2, 1);
686 SkScalar sy = lua2scalar_def(L, 3, sx);
687 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
688 return 0;
689}
690
reed@google.com3597b732013-05-22 20:12:50 +0000691static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000692 SkScalar tx = lua2scalar_def(L, 2, 0);
693 SkScalar ty = lua2scalar_def(L, 3, 0);
694 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
695 return 0;
696}
697
698static int lcanvas_rotate(lua_State* L) {
699 SkScalar degrees = lua2scalar_def(L, 2, 0);
700 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000701 return 0;
702}
703
reedbdc49ae2014-10-14 09:34:52 -0700704static int lcanvas_concat(lua_State* L) {
705 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
706 return 0;
707}
708
reed485557f2014-10-12 10:36:47 -0700709static int lcanvas_newSurface(lua_State* L) {
710 int width = lua2int_def(L, 2, 0);
reed7a72c672014-11-07 10:23:55 -0800711 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -0700712 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
713 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
714 if (NULL == surface) {
715 lua_pushnil(L);
716 } else {
reed9fbc3f32014-10-21 07:12:58 -0700717 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -0700718 }
719 return 1;
720}
721
reed@google.com74ce6f02013-05-22 15:13:18 +0000722static int lcanvas_gc(lua_State* L) {
723 get_ref<SkCanvas>(L, 1)->unref();
724 return 0;
725}
726
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000727const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700728 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000729 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700730 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000731 { "drawRect", lcanvas_drawRect },
732 { "drawOval", lcanvas_drawOval },
733 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000734 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700735 { "drawImageRect", lcanvas_drawImageRect },
reed7a72c672014-11-07 10:23:55 -0800736 { "drawPatch", lcanvas_drawPatch },
reed@google.comfd345872013-05-22 20:53:42 +0000737 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700738 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000739 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800740 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000741 { "getSaveCount", lcanvas_getSaveCount },
742 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000743 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000744#if SK_SUPPORT_GPU
745 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
746#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000747 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700748 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000749 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000750 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000751 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000752 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700753 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700754
755 { "newSurface", lcanvas_newSurface },
756
reed@google.com74ce6f02013-05-22 15:13:18 +0000757 { "__gc", lcanvas_gc },
758 { NULL, NULL }
759};
760
761///////////////////////////////////////////////////////////////////////////////
762
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000763static int ldocument_beginPage(lua_State* L) {
764 const SkRect* contentPtr = NULL;
765 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
766 lua2scalar(L, 3),
767 contentPtr));
768 return 1;
769}
770
771static int ldocument_endPage(lua_State* L) {
772 get_ref<SkDocument>(L, 1)->endPage();
773 return 0;
774}
775
776static int ldocument_close(lua_State* L) {
777 get_ref<SkDocument>(L, 1)->close();
778 return 0;
779}
780
781static int ldocument_gc(lua_State* L) {
782 get_ref<SkDocument>(L, 1)->unref();
783 return 0;
784}
785
786static const struct luaL_Reg gSkDocument_Methods[] = {
787 { "beginPage", ldocument_beginPage },
788 { "endPage", ldocument_endPage },
789 { "close", ldocument_close },
790 { "__gc", ldocument_gc },
791 { NULL, NULL }
792};
793
794///////////////////////////////////////////////////////////////////////////////
795
reed@google.com74ce6f02013-05-22 15:13:18 +0000796static int lpaint_isAntiAlias(lua_State* L) {
797 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
798 return 1;
799}
800
801static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000802 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000803 return 0;
804}
805
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000806static int lpaint_isDither(lua_State* L) {
807 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
808 return 1;
809}
810
reedbb8a0ab2014-11-03 22:32:07 -0800811static int lpaint_setDither(lua_State* L) {
812 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
813 return 0;
814}
815
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000816static int lpaint_isUnderlineText(lua_State* L) {
817 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
818 return 1;
819}
820
821static int lpaint_isStrikeThruText(lua_State* L) {
822 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
823 return 1;
824}
825
826static int lpaint_isFakeBoldText(lua_State* L) {
827 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
828 return 1;
829}
830
831static int lpaint_isLinearText(lua_State* L) {
832 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
833 return 1;
834}
835
836static int lpaint_isSubpixelText(lua_State* L) {
837 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
838 return 1;
839}
840
reed09a1d672014-10-11 13:13:11 -0700841static int lpaint_setSubpixelText(lua_State* L) {
842 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
843 return 1;
844}
845
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000846static int lpaint_isDevKernText(lua_State* L) {
847 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
848 return 1;
849}
850
851static int lpaint_isLCDRenderText(lua_State* L) {
852 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
853 return 1;
854}
855
reed36c9c112014-11-04 10:58:42 -0800856static int lpaint_setLCDRenderText(lua_State* L) {
857 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
858 return 1;
859}
860
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000861static int lpaint_isEmbeddedBitmapText(lua_State* L) {
862 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
863 return 1;
864}
865
866static int lpaint_isAutohinted(lua_State* L) {
867 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
868 return 1;
869}
870
871static int lpaint_isVerticalText(lua_State* L) {
872 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
873 return 1;
874}
875
reed468b1812014-10-19 11:42:54 -0700876static int lpaint_getAlpha(lua_State* L) {
877 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
878 return 1;
879}
880
881static int lpaint_setAlpha(lua_State* L) {
882 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
883 return 0;
884}
885
reed@google.com74ce6f02013-05-22 15:13:18 +0000886static int lpaint_getColor(lua_State* L) {
887 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
888 return 1;
889}
890
891static int lpaint_setColor(lua_State* L) {
892 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
893 return 0;
894}
895
reed@google.come3823fd2013-05-30 18:55:14 +0000896static int lpaint_getTextSize(lua_State* L) {
897 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
898 return 1;
899}
900
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000901static int lpaint_getTextScaleX(lua_State* L) {
902 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
903 return 1;
904}
905
906static int lpaint_getTextSkewX(lua_State* L) {
907 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
908 return 1;
909}
910
reed@google.come3823fd2013-05-30 18:55:14 +0000911static int lpaint_setTextSize(lua_State* L) {
912 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
913 return 0;
914}
915
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000916static int lpaint_getTypeface(lua_State* L) {
917 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
918 return 1;
919}
920
921static int lpaint_setTypeface(lua_State* L) {
922 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
923 return 0;
924}
925
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000926static int lpaint_getHinting(lua_State* L) {
927 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
928 return 1;
929}
930
reed7a72c672014-11-07 10:23:55 -0800931static int lpaint_getFilterLevel(lua_State* L) {
932 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterLevel());
933 return 1;
934}
935
936static int lpaint_setFilterLevel(lua_State* L) {
937 int level = lua2int_def(L, 2, -1);
938 if (level >= 0 && level <= 3) {
939 get_obj<SkPaint>(L, 1)->setFilterLevel((SkPaint::FilterLevel)level);
940 }
941 return 0;
942}
943
reed@google.come3823fd2013-05-30 18:55:14 +0000944static int lpaint_getFontID(lua_State* L) {
945 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
946 SkLua(L).pushU32(SkTypeface::UniqueID(face));
947 return 1;
948}
949
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000950static const struct {
951 const char* fLabel;
952 SkPaint::Align fAlign;
953} gAlignRec[] = {
954 { "left", SkPaint::kLeft_Align },
955 { "center", SkPaint::kCenter_Align },
956 { "right", SkPaint::kRight_Align },
957};
958
959static int lpaint_getTextAlign(lua_State* L) {
960 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
961 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
962 if (gAlignRec[i].fAlign == align) {
963 lua_pushstring(L, gAlignRec[i].fLabel);
964 return 1;
965 }
966 }
967 return 0;
968}
969
970static int lpaint_setTextAlign(lua_State* L) {
971 if (lua_isstring(L, 2)) {
972 size_t len;
973 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000974
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000975 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
976 if (!strcmp(gAlignRec[i].fLabel, label)) {
977 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
978 break;
979 }
980 }
981 }
982 return 0;
983}
984
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000985static int lpaint_getStroke(lua_State* L) {
986 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
987 return 1;
988}
989
990static int lpaint_setStroke(lua_State* L) {
991 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000992
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000993 if (lua_toboolean(L, 2)) {
994 style = SkPaint::kStroke_Style;
995 } else {
996 style = SkPaint::kFill_Style;
997 }
998 get_obj<SkPaint>(L, 1)->setStyle(style);
999 return 0;
1000}
1001
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001002static int lpaint_getStrokeCap(lua_State* L) {
1003 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
1004 return 1;
1005}
1006
1007static int lpaint_getStrokeJoin(lua_State* L) {
1008 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
1009 return 1;
1010}
1011
1012static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +00001013 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001014 return 1;
1015}
1016
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001017static int lpaint_getStrokeWidth(lua_State* L) {
1018 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
1019 return 1;
1020}
1021
1022static int lpaint_setStrokeWidth(lua_State* L) {
1023 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
1024 return 0;
1025}
1026
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001027static int lpaint_getStrokeMiter(lua_State* L) {
1028 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
1029 return 1;
1030}
1031
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001032static int lpaint_measureText(lua_State* L) {
1033 if (lua_isstring(L, 2)) {
1034 size_t len;
1035 const char* text = lua_tolstring(L, 2, &len);
1036 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
1037 return 1;
1038 }
1039 return 0;
1040}
1041
1042struct FontMetrics {
1043 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
1044 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
1045 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
1046 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
1047 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
1048 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
1049 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
1050 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
1051 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
1052};
1053
1054static int lpaint_getFontMetrics(lua_State* L) {
1055 SkPaint::FontMetrics fm;
1056 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +00001057
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001058 lua_newtable(L);
1059 setfield_scalar(L, "top", fm.fTop);
1060 setfield_scalar(L, "ascent", fm.fAscent);
1061 setfield_scalar(L, "descent", fm.fDescent);
1062 setfield_scalar(L, "bottom", fm.fBottom);
1063 setfield_scalar(L, "leading", fm.fLeading);
1064 SkLua(L).pushScalar(height);
1065 return 2;
1066}
1067
reed@google.com29563872013-07-10 21:23:49 +00001068static int lpaint_getEffects(lua_State* L) {
1069 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001070
reed@google.com29563872013-07-10 21:23:49 +00001071 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -07001072 setfield_bool_if(L, "looper", !!paint->getLooper());
1073 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
1074 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
1075 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
1076 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +00001077 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1078 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -07001079 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +00001080 return 1;
1081}
1082
reed468b1812014-10-19 11:42:54 -07001083static int lpaint_getImageFilter(lua_State* L) {
1084 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1085 SkImageFilter* imf = paint->getImageFilter();
1086 if (imf) {
1087 push_ref(L, imf);
1088 return 1;
1089 }
1090 return 0;
1091}
1092
1093static int lpaint_setImageFilter(lua_State* L) {
1094 SkPaint* paint = get_obj<SkPaint>(L, 1);
1095 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1096 return 0;
1097}
1098
reed@google.com5fdc9832013-07-24 15:47:52 +00001099static int lpaint_getShader(lua_State* L) {
1100 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1101 SkShader* shader = paint->getShader();
1102 if (shader) {
1103 push_ref(L, shader);
1104 return 1;
1105 }
1106 return 0;
1107}
1108
reed9fbc3f32014-10-21 07:12:58 -07001109static int lpaint_setShader(lua_State* L) {
1110 SkPaint* paint = get_obj<SkPaint>(L, 1);
1111 paint->setShader(get_ref<SkShader>(L, 2));
1112 return 0;
1113}
1114
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001115static int lpaint_getPathEffect(lua_State* L) {
1116 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1117 SkPathEffect* pe = paint->getPathEffect();
1118 if (pe) {
1119 push_ref(L, pe);
1120 return 1;
1121 }
1122 return 0;
1123}
1124
reed@google.com74ce6f02013-05-22 15:13:18 +00001125static int lpaint_gc(lua_State* L) {
1126 get_obj<SkPaint>(L, 1)->~SkPaint();
1127 return 0;
1128}
1129
1130static const struct luaL_Reg gSkPaint_Methods[] = {
1131 { "isAntiAlias", lpaint_isAntiAlias },
1132 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001133 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001134 { "setDither", lpaint_setDither },
reed7a72c672014-11-07 10:23:55 -08001135 { "getFilterLevel", lpaint_getFilterLevel },
1136 { "setFilterLevel", lpaint_setFilterLevel },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001137 { "isUnderlineText", lpaint_isUnderlineText },
1138 { "isStrikeThruText", lpaint_isStrikeThruText },
1139 { "isFakeBoldText", lpaint_isFakeBoldText },
1140 { "isLinearText", lpaint_isLinearText },
1141 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001142 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001143 { "isDevKernText", lpaint_isDevKernText },
1144 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001145 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001146 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1147 { "isAutohinted", lpaint_isAutohinted },
1148 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001149 { "getAlpha", lpaint_getAlpha },
1150 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001151 { "getColor", lpaint_getColor },
1152 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001153 { "getTextSize", lpaint_getTextSize },
1154 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001155 { "getTextScaleX", lpaint_getTextScaleX },
1156 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001157 { "getTypeface", lpaint_getTypeface },
1158 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001159 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001160 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001161 { "getTextAlign", lpaint_getTextAlign },
1162 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001163 { "getStroke", lpaint_getStroke },
1164 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001165 { "getStrokeCap", lpaint_getStrokeCap },
1166 { "getStrokeJoin", lpaint_getStrokeJoin },
1167 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001168 { "getStrokeWidth", lpaint_getStrokeWidth },
1169 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001170 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001171 { "measureText", lpaint_measureText },
1172 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001173 { "getEffects", lpaint_getEffects },
reed468b1812014-10-19 11:42:54 -07001174 { "getImageFilter", lpaint_getImageFilter },
1175 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001176 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001177 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001178 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001179 { "__gc", lpaint_gc },
1180 { NULL, NULL }
1181};
1182
1183///////////////////////////////////////////////////////////////////////////////
1184
reed@google.com5fdc9832013-07-24 15:47:52 +00001185static const char* mode2string(SkShader::TileMode mode) {
1186 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1187 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1188 return gNames[mode];
1189}
1190
1191static const char* gradtype2string(SkShader::GradientType t) {
1192 static const char* gNames[] = {
1193 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1194 };
1195 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1196 return gNames[t];
1197}
1198
1199static int lshader_isOpaque(lua_State* L) {
1200 SkShader* shader = get_ref<SkShader>(L, 1);
1201 return shader && shader->isOpaque();
1202}
1203
1204static int lshader_asABitmap(lua_State* L) {
1205 SkShader* shader = get_ref<SkShader>(L, 1);
1206 if (shader) {
1207 SkBitmap bm;
1208 SkMatrix matrix;
1209 SkShader::TileMode modes[2];
1210 switch (shader->asABitmap(&bm, &matrix, modes)) {
1211 case SkShader::kDefault_BitmapType:
1212 lua_newtable(L);
1213 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1214 setfield_number(L, "width", bm.width());
1215 setfield_number(L, "height", bm.height());
1216 setfield_string(L, "tileX", mode2string(modes[0]));
1217 setfield_string(L, "tileY", mode2string(modes[1]));
1218 return 1;
1219 default:
1220 break;
1221 }
1222 }
1223 return 0;
1224}
1225
1226static int lshader_asAGradient(lua_State* L) {
1227 SkShader* shader = get_ref<SkShader>(L, 1);
1228 if (shader) {
1229 SkShader::GradientInfo info;
1230 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001231
1232 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001233 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001234
1235 info.fColorCount = 3;
1236 info.fColors = &colors[0];
1237 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001238
reed@google.com5fdc9832013-07-24 15:47:52 +00001239 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001240
reed@google.com5fdc9832013-07-24 15:47:52 +00001241 if (SkShader::kNone_GradientType != t) {
1242 lua_newtable(L);
1243 setfield_string(L, "type", gradtype2string(t));
1244 setfield_number(L, "colorCount", info.fColorCount);
1245 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001246
1247 if (info.fColorCount == 3){
1248 setfield_number(L, "midPos", pos[1]);
1249 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001250
reed@google.com5fdc9832013-07-24 15:47:52 +00001251 return 1;
1252 }
1253 }
1254 return 0;
1255}
1256
1257static int lshader_gc(lua_State* L) {
1258 get_ref<SkShader>(L, 1)->unref();
1259 return 0;
1260}
1261
1262static const struct luaL_Reg gSkShader_Methods[] = {
1263 { "isOpaque", lshader_isOpaque },
1264 { "asABitmap", lshader_asABitmap },
1265 { "asAGradient", lshader_asAGradient },
1266 { "__gc", lshader_gc },
1267 { NULL, NULL }
1268};
1269
1270///////////////////////////////////////////////////////////////////////////////
1271
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001272static int lpatheffect_asADash(lua_State* L) {
1273 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1274 if (pe) {
1275 SkPathEffect::DashInfo info;
1276 SkPathEffect::DashType dashType = pe->asADash(&info);
1277 if (SkPathEffect::kDash_DashType == dashType) {
1278 SkAutoTArray<SkScalar> intervals(info.fCount);
1279 info.fIntervals = intervals.get();
1280 pe->asADash(&info);
1281 SkLua(L).pushDash(info);
1282 return 1;
1283 }
1284 }
1285 return 0;
1286}
1287
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001288static int lpatheffect_gc(lua_State* L) {
1289 get_ref<SkPathEffect>(L, 1)->unref();
1290 return 0;
1291}
1292
1293static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001294 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001295 { "__gc", lpatheffect_gc },
1296 { NULL, NULL }
1297};
1298
1299///////////////////////////////////////////////////////////////////////////////
1300
reed468b1812014-10-19 11:42:54 -07001301static int lpimagefilter_gc(lua_State* L) {
1302 get_ref<SkImageFilter>(L, 1)->unref();
1303 return 0;
1304}
1305
1306static const struct luaL_Reg gSkImageFilter_Methods[] = {
1307 { "__gc", lpimagefilter_gc },
1308 { NULL, NULL }
1309};
1310
1311///////////////////////////////////////////////////////////////////////////////
1312
humper@google.com2815c192013-07-10 22:42:30 +00001313static int lmatrix_getType(lua_State* L) {
1314 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001315
humper@google.com2815c192013-07-10 22:42:30 +00001316 lua_newtable(L);
1317 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1318 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1319 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1320 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1321 return 1;
1322}
1323
humper@google.com0f48ee02013-07-26 15:23:43 +00001324static int lmatrix_getScaleX(lua_State* L) {
1325 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1326 return 1;
1327}
1328
1329static int lmatrix_getScaleY(lua_State* L) {
1330 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1331 return 1;
1332}
1333
1334static int lmatrix_getTranslateX(lua_State* L) {
1335 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1336 return 1;
1337}
1338
1339static int lmatrix_getTranslateY(lua_State* L) {
1340 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1341 return 1;
1342}
1343
reed7a72c672014-11-07 10:23:55 -08001344static int lmatrix_invert(lua_State* L) {
1345 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1346 return 1;
1347}
1348
1349static int lmatrix_mapXY(lua_State* L) {
1350 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1351 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1352 lua_pushnumber(L, pt.x());
1353 lua_pushnumber(L, pt.y());
1354 return 2;
1355}
1356
reedbdc49ae2014-10-14 09:34:52 -07001357static int lmatrix_setRectToRect(lua_State* L) {
1358 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1359 SkRect srcR, dstR;
1360 lua2rect(L, 2, &srcR);
1361 lua2rect(L, 3, &dstR);
1362 const char* scaleToFitStr = lua_tostring(L, 4);
1363 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1364
1365 if (scaleToFitStr) {
1366 const struct {
1367 const char* fName;
1368 SkMatrix::ScaleToFit fScaleToFit;
1369 } rec[] = {
1370 { "fill", SkMatrix::kFill_ScaleToFit },
1371 { "start", SkMatrix::kStart_ScaleToFit },
1372 { "center", SkMatrix::kCenter_ScaleToFit },
1373 { "end", SkMatrix::kEnd_ScaleToFit },
1374 };
1375
1376 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1377 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1378 scaleToFit = rec[i].fScaleToFit;
1379 break;
1380 }
1381 }
1382 }
1383
1384 matrix->setRectToRect(srcR, dstR, scaleToFit);
1385 return 0;
1386}
1387
humper@google.com2815c192013-07-10 22:42:30 +00001388static const struct luaL_Reg gSkMatrix_Methods[] = {
1389 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001390 { "getScaleX", lmatrix_getScaleX },
1391 { "getScaleY", lmatrix_getScaleY },
1392 { "getTranslateX", lmatrix_getTranslateX },
1393 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001394 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001395 { "invert", lmatrix_invert },
1396 { "mapXY", lmatrix_mapXY },
humper@google.com2815c192013-07-10 22:42:30 +00001397 { NULL, NULL }
1398};
1399
1400///////////////////////////////////////////////////////////////////////////////
1401
reed@google.com74ce6f02013-05-22 15:13:18 +00001402static int lpath_getBounds(lua_State* L) {
1403 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1404 return 1;
1405}
1406
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001407static const char* fill_type_to_str(SkPath::FillType fill) {
1408 switch (fill) {
1409 case SkPath::kEvenOdd_FillType:
1410 return "even-odd";
1411 case SkPath::kWinding_FillType:
1412 return "winding";
1413 case SkPath::kInverseEvenOdd_FillType:
1414 return "inverse-even-odd";
1415 case SkPath::kInverseWinding_FillType:
1416 return "inverse-winding";
1417 }
1418 return "unknown";
1419}
1420
1421static int lpath_getFillType(lua_State* L) {
1422 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1423 SkLua(L).pushString(fill_type_to_str(fill));
1424 return 1;
1425}
1426
1427static SkString segment_masks_to_str(uint32_t segmentMasks) {
1428 SkString result;
1429 bool first = true;
1430 if (SkPath::kLine_SegmentMask & segmentMasks) {
1431 result.append("line");
1432 first = false;
1433 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1434 }
1435 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1436 if (!first) {
1437 result.append(" ");
1438 }
1439 result.append("quad");
1440 first = false;
1441 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1442 }
1443 if (SkPath::kConic_SegmentMask & segmentMasks) {
1444 if (!first) {
1445 result.append(" ");
1446 }
1447 result.append("conic");
1448 first = false;
1449 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1450 }
1451 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1452 if (!first) {
1453 result.append(" ");
1454 }
1455 result.append("cubic");
1456 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1457 }
1458 SkASSERT(0 == segmentMasks);
1459 return result;
1460}
1461
krajcevski95498ed2014-08-18 08:02:33 -07001462static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001463 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1464 SkLua(L).pushString(segment_masks_to_str(segMasks));
1465 return 1;
1466}
1467
1468static int lpath_isConvex(lua_State* L) {
1469 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1470 SkLua(L).pushBool(isConvex);
1471 return 1;
1472}
1473
reed@google.com74ce6f02013-05-22 15:13:18 +00001474static int lpath_isEmpty(lua_State* L) {
1475 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1476 return 1;
1477}
1478
1479static int lpath_isRect(lua_State* L) {
1480 SkRect r;
1481 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1482 int ret_count = 1;
1483 lua_pushboolean(L, pred);
1484 if (pred) {
1485 SkLua(L).pushRect(r);
1486 ret_count += 1;
1487 }
1488 return ret_count;
1489}
1490
1491static const char* dir2string(SkPath::Direction dir) {
1492 static const char* gStr[] = {
1493 "unknown", "cw", "ccw"
1494 };
1495 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1496 return gStr[dir];
1497}
1498
1499static int lpath_isNestedRects(lua_State* L) {
1500 SkRect rects[2];
1501 SkPath::Direction dirs[2];
1502 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1503 int ret_count = 1;
1504 lua_pushboolean(L, pred);
1505 if (pred) {
1506 SkLua lua(L);
1507 lua.pushRect(rects[0]);
1508 lua.pushRect(rects[1]);
1509 lua_pushstring(L, dir2string(dirs[0]));
1510 lua_pushstring(L, dir2string(dirs[0]));
1511 ret_count += 4;
1512 }
1513 return ret_count;
1514}
1515
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001516static int lpath_countPoints(lua_State* L) {
1517 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1518 return 1;
1519}
1520
reed@google.com74ce6f02013-05-22 15:13:18 +00001521static int lpath_reset(lua_State* L) {
1522 get_obj<SkPath>(L, 1)->reset();
1523 return 0;
1524}
1525
1526static int lpath_moveTo(lua_State* L) {
1527 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1528 return 0;
1529}
1530
1531static int lpath_lineTo(lua_State* L) {
1532 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1533 return 0;
1534}
1535
1536static int lpath_quadTo(lua_State* L) {
1537 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1538 lua2scalar(L, 4), lua2scalar(L, 5));
1539 return 0;
1540}
1541
1542static int lpath_cubicTo(lua_State* L) {
1543 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1544 lua2scalar(L, 4), lua2scalar(L, 5),
1545 lua2scalar(L, 6), lua2scalar(L, 7));
1546 return 0;
1547}
1548
1549static int lpath_close(lua_State* L) {
1550 get_obj<SkPath>(L, 1)->close();
1551 return 0;
1552}
1553
1554static int lpath_gc(lua_State* L) {
1555 get_obj<SkPath>(L, 1)->~SkPath();
1556 return 0;
1557}
1558
1559static const struct luaL_Reg gSkPath_Methods[] = {
1560 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001561 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001562 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001563 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001564 { "isEmpty", lpath_isEmpty },
1565 { "isRect", lpath_isRect },
1566 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001567 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001568 { "reset", lpath_reset },
1569 { "moveTo", lpath_moveTo },
1570 { "lineTo", lpath_lineTo },
1571 { "quadTo", lpath_quadTo },
1572 { "cubicTo", lpath_cubicTo },
1573 { "close", lpath_close },
1574 { "__gc", lpath_gc },
1575 { NULL, NULL }
1576};
1577
1578///////////////////////////////////////////////////////////////////////////////
1579
1580static const char* rrect_type(const SkRRect& rr) {
1581 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001582 case SkRRect::kEmpty_Type: return "empty";
1583 case SkRRect::kRect_Type: return "rect";
1584 case SkRRect::kOval_Type: return "oval";
1585 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001586 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001587 case SkRRect::kComplex_Type: return "complex";
1588 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001589 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001590 return "";
1591}
1592
1593static int lrrect_rect(lua_State* L) {
1594 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1595 return 1;
1596}
1597
1598static int lrrect_type(lua_State* L) {
1599 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1600 return 1;
1601}
1602
1603static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001604 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001605 SkVector v;
1606 if (corner < 0 || corner > 3) {
1607 SkDebugf("bad corner index %d", corner);
1608 v.set(0, 0);
1609 } else {
1610 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1611 }
1612 lua_pushnumber(L, v.fX);
1613 lua_pushnumber(L, v.fY);
1614 return 2;
1615}
1616
1617static int lrrect_gc(lua_State* L) {
1618 get_obj<SkRRect>(L, 1)->~SkRRect();
1619 return 0;
1620}
1621
1622static const struct luaL_Reg gSkRRect_Methods[] = {
1623 { "rect", lrrect_rect },
1624 { "type", lrrect_type },
1625 { "radii", lrrect_radii },
1626 { "__gc", lrrect_gc },
1627 { NULL, NULL }
1628};
1629
1630///////////////////////////////////////////////////////////////////////////////
1631
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001632static int limage_width(lua_State* L) {
1633 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1634 return 1;
1635}
1636
1637static int limage_height(lua_State* L) {
1638 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1639 return 1;
1640}
1641
reed7a72c672014-11-07 10:23:55 -08001642static int limage_newShader(lua_State* L) {
1643 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
1644 const SkMatrix* localM = NULL;
1645 SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM));
1646 push_ref(L, shader.get());
1647 return 1;
1648}
1649
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001650static int limage_gc(lua_State* L) {
1651 get_ref<SkImage>(L, 1)->unref();
1652 return 0;
1653}
1654
1655static const struct luaL_Reg gSkImage_Methods[] = {
1656 { "width", limage_width },
1657 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001658 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001659 { "__gc", limage_gc },
1660 { NULL, NULL }
1661};
1662
1663///////////////////////////////////////////////////////////////////////////////
1664
reed485557f2014-10-12 10:36:47 -07001665static int lsurface_width(lua_State* L) {
1666 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1667 return 1;
1668}
1669
1670static int lsurface_height(lua_State* L) {
1671 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1672 return 1;
1673}
1674
1675static int lsurface_getCanvas(lua_State* L) {
1676 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1677 if (NULL == canvas) {
1678 lua_pushnil(L);
1679 } else {
1680 push_ref(L, canvas);
1681 // note: we don't unref canvas, since getCanvas did not ref it.
1682 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1683 // the real owner (the surface) go away, but still hold onto the canvas?
1684 // *really* we want to sort of ref the surface again, but have the native object
1685 // know that it is supposed to be treated as a canvas...
1686 }
1687 return 1;
1688}
1689
1690static int lsurface_newImageSnapshot(lua_State* L) {
1691 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1692 if (NULL == image) {
1693 lua_pushnil(L);
1694 } else {
reed9fbc3f32014-10-21 07:12:58 -07001695 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001696 }
1697 return 1;
1698}
1699
1700static int lsurface_newSurface(lua_State* L) {
1701 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001702 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001703 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1704 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1705 if (NULL == surface) {
1706 lua_pushnil(L);
1707 } else {
reed9fbc3f32014-10-21 07:12:58 -07001708 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001709 }
1710 return 1;
1711}
1712
1713static int lsurface_gc(lua_State* L) {
1714 get_ref<SkSurface>(L, 1)->unref();
1715 return 0;
1716}
1717
1718static const struct luaL_Reg gSkSurface_Methods[] = {
1719 { "width", lsurface_width },
1720 { "height", lsurface_height },
1721 { "getCanvas", lsurface_getCanvas },
1722 { "newImageSnapshot", lsurface_newImageSnapshot },
1723 { "newSurface", lsurface_newSurface },
1724 { "__gc", lsurface_gc },
1725 { NULL, NULL }
1726};
1727
1728///////////////////////////////////////////////////////////////////////////////
1729
reed96affcd2014-10-13 12:38:04 -07001730static int lpicturerecorder_beginRecording(lua_State* L) {
1731 const SkScalar w = lua2scalar_def(L, 2, -1);
1732 const SkScalar h = lua2scalar_def(L, 3, -1);
1733 if (w <= 0 || h <= 0) {
1734 lua_pushnil(L);
1735 return 1;
1736 }
1737
1738 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1739 if (NULL == canvas) {
1740 lua_pushnil(L);
1741 return 1;
1742 }
1743
1744 push_ref(L, canvas);
1745 return 1;
1746}
1747
1748static int lpicturerecorder_getCanvas(lua_State* L) {
1749 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1750 if (NULL == canvas) {
1751 lua_pushnil(L);
1752 return 1;
1753 }
1754 push_ref(L, canvas);
1755 return 1;
1756}
1757
1758static int lpicturerecorder_endRecording(lua_State* L) {
1759 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1760 if (NULL == pic) {
1761 lua_pushnil(L);
1762 return 1;
1763 }
reed9fbc3f32014-10-21 07:12:58 -07001764 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001765 return 1;
1766}
1767
1768static int lpicturerecorder_gc(lua_State* L) {
1769 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1770 return 0;
1771}
1772
1773static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1774 { "beginRecording", lpicturerecorder_beginRecording },
1775 { "getCanvas", lpicturerecorder_getCanvas },
1776 { "endRecording", lpicturerecorder_endRecording },
1777 { "__gc", lpicturerecorder_gc },
1778 { NULL, NULL }
1779};
1780
1781///////////////////////////////////////////////////////////////////////////////
1782
1783static int lpicture_width(lua_State* L) {
1784 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1785 return 1;
1786}
1787
1788static int lpicture_height(lua_State* L) {
1789 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1790 return 1;
1791}
1792
1793static int lpicture_gc(lua_State* L) {
1794 get_ref<SkPicture>(L, 1)->unref();
1795 return 0;
1796}
1797
1798static const struct luaL_Reg gSkPicture_Methods[] = {
1799 { "width", lpicture_width },
1800 { "height", lpicture_height },
1801 { "__gc", lpicture_gc },
1802 { NULL, NULL }
1803};
1804
1805///////////////////////////////////////////////////////////////////////////////
1806
reed1b6ab442014-11-03 19:55:41 -08001807static int ltextblob_bounds(lua_State* L) {
1808 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1809 return 1;
1810}
1811
1812static int ltextblob_gc(lua_State* L) {
1813 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1814 return 0;
1815}
1816
1817static const struct luaL_Reg gSkTextBlob_Methods[] = {
1818 { "bounds", ltextblob_bounds },
1819 { "__gc", ltextblob_gc },
1820 { NULL, NULL }
1821};
1822
1823///////////////////////////////////////////////////////////////////////////////
1824
reed36c9c112014-11-04 10:58:42 -08001825static int ltypeface_getFamilyName(lua_State* L) {
1826 SkString str;
1827 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1828 lua_pushstring(L, str.c_str());
1829 return 1;
1830}
1831
1832static int ltypeface_getStyle(lua_State* L) {
1833 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1834 return 1;
1835}
1836
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001837static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001838 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001839 return 0;
1840}
1841
1842static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001843 { "getFamilyName", ltypeface_getFamilyName },
1844 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001845 { "__gc", ltypeface_gc },
1846 { NULL, NULL }
1847};
1848
1849///////////////////////////////////////////////////////////////////////////////
1850
reed@google.com74ce6f02013-05-22 15:13:18 +00001851class AutoCallLua {
1852public:
1853 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1854 lua_getglobal(L, func);
1855 if (!lua_isfunction(L, -1)) {
1856 int t = lua_type(L, -1);
1857 SkDebugf("--- expected function %d\n", t);
1858 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001859
reed@google.com74ce6f02013-05-22 15:13:18 +00001860 lua_newtable(L);
1861 setfield_string(L, "verb", verb);
1862 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001863
reed@google.com74ce6f02013-05-22 15:13:18 +00001864 ~AutoCallLua() {
1865 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1866 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1867 }
1868 lua_settop(fL, -1);
1869 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001870
reed@google.com74ce6f02013-05-22 15:13:18 +00001871private:
1872 lua_State* fL;
1873};
1874
1875#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1876
1877///////////////////////////////////////////////////////////////////////////////
1878
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001879static int lsk_newDocumentPDF(lua_State* L) {
1880 const char* file = NULL;
1881 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1882 file = lua_tolstring(L, 1, NULL);
1883 }
1884
1885 SkDocument* doc = SkDocument::CreatePDF(file);
1886 if (NULL == doc) {
1887 // do I need to push a nil on the stack and return 1?
1888 return 0;
1889 } else {
reed9fbc3f32014-10-21 07:12:58 -07001890 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001891 return 1;
1892 }
1893}
1894
reed468b1812014-10-19 11:42:54 -07001895static int lsk_newBlurImageFilter(lua_State* L) {
1896 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1897 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1898 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1899 if (NULL == imf) {
1900 lua_pushnil(L);
1901 } else {
reed9fbc3f32014-10-21 07:12:58 -07001902 push_ref(L, imf)->unref();
1903 }
1904 return 1;
1905}
1906
1907static int lsk_newLinearGradient(lua_State* L) {
1908 SkScalar x0 = lua2scalar_def(L, 1, 0);
1909 SkScalar y0 = lua2scalar_def(L, 2, 0);
1910 SkColor c0 = lua2color(L, 3);
1911 SkScalar x1 = lua2scalar_def(L, 4, 0);
1912 SkScalar y1 = lua2scalar_def(L, 5, 0);
1913 SkColor c1 = lua2color(L, 6);
1914
1915 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1916 SkColor colors[] = { c0, c1 };
1917 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
1918 if (NULL == s) {
1919 lua_pushnil(L);
1920 } else {
1921 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001922 }
1923 return 1;
1924}
1925
reedbdc49ae2014-10-14 09:34:52 -07001926static int lsk_newMatrix(lua_State* L) {
1927 push_new<SkMatrix>(L)->reset();
1928 return 1;
1929}
1930
reed@google.com3597b732013-05-22 20:12:50 +00001931static int lsk_newPaint(lua_State* L) {
1932 push_new<SkPaint>(L);
1933 return 1;
1934}
1935
1936static int lsk_newPath(lua_State* L) {
1937 push_new<SkPath>(L);
1938 return 1;
1939}
1940
reed96affcd2014-10-13 12:38:04 -07001941static int lsk_newPictureRecorder(lua_State* L) {
1942 push_new<SkPictureRecorder>(L);
1943 return 1;
1944}
1945
reed@google.com3597b732013-05-22 20:12:50 +00001946static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001947 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001948 return 1;
1949}
1950
reed1b6ab442014-11-03 19:55:41 -08001951#include "SkTextBox.h"
1952// Sk.newTextBlob(text, rect, paint)
1953static int lsk_newTextBlob(lua_State* L) {
1954 const char* text = lua_tolstring(L, 1, NULL);
1955 SkRect bounds;
1956 lua2rect(L, 2, &bounds);
1957 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1958
1959 SkTextBox box;
1960 box.setMode(SkTextBox::kLineBreak_Mode);
1961 box.setBox(bounds);
1962 box.setText(text, strlen(text), paint);
1963
1964 SkScalar newBottom;
1965 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
1966 push_ref<SkTextBlob>(L, blob);
1967 SkLua(L).pushScalar(newBottom);
1968 return 2;
1969}
1970
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001971static int lsk_newTypeface(lua_State* L) {
1972 const char* name = NULL;
1973 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001974
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001975 int count = lua_gettop(L);
1976 if (count > 0 && lua_isstring(L, 1)) {
1977 name = lua_tolstring(L, 1, NULL);
1978 if (count > 1 && lua_isnumber(L, 2)) {
1979 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1980 }
1981 }
1982
1983 SkTypeface* face = SkTypeface::CreateFromName(name,
1984 (SkTypeface::Style)style);
1985// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1986 if (NULL == face) {
1987 face = SkTypeface::RefDefault();
1988 }
reed9fbc3f32014-10-21 07:12:58 -07001989 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001990 return 1;
1991}
reed@google.com3597b732013-05-22 20:12:50 +00001992
reed485557f2014-10-12 10:36:47 -07001993static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08001994 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07001995 int height = lua2int_def(L, 2, 0);
1996 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1997 SkSurface* surface = SkSurface::NewRaster(info);
1998 if (NULL == surface) {
1999 lua_pushnil(L);
2000 } else {
reed9fbc3f32014-10-21 07:12:58 -07002001 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07002002 }
2003 return 1;
2004}
2005
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002006static int lsk_loadImage(lua_State* L) {
2007 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
2008 const char* name = lua_tolstring(L, 1, NULL);
2009 SkAutoDataUnref data(SkData::NewFromFileName(name));
2010 if (data.get()) {
piotaixr4bcc2022014-09-17 14:33:30 -07002011 SkImage* image = SkImage::NewFromGenerator(
2012 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
2013
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002014 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07002015 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002016 return 1;
2017 }
2018 }
2019 }
2020 return 0;
2021}
2022
reed@google.com3597b732013-05-22 20:12:50 +00002023static void register_Sk(lua_State* L) {
2024 lua_newtable(L);
2025 lua_pushvalue(L, -1);
2026 lua_setglobal(L, "Sk");
2027 // the Sk table is still on top
2028
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002029 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002030 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002031 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002032 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002033 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002034 setfield_function(L, "newPaint", lsk_newPaint);
2035 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002036 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002037 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002038 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002039 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002040 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00002041 lua_pop(L, 1); // pop off the Sk table
2042}
2043
reed@google.com74ce6f02013-05-22 15:13:18 +00002044#define REG_CLASS(L, C) \
2045 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002046 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002047 lua_pushvalue(L, -1); \
2048 lua_setfield(L, -2, "__index"); \
2049 luaL_setfuncs(L, g##C##_Methods, 0); \
2050 lua_pop(L, 1); /* pop off the meta-table */ \
2051 } while (0)
2052
2053void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002054 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002055 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002056 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002057 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002058 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002059 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002060 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002061 REG_CLASS(L, SkPath);
2062 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002063 REG_CLASS(L, SkPicture);
2064 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002065 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002066 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002067 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002068 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002069 REG_CLASS(L, SkTypeface);
reed@google.com74ce6f02013-05-22 15:13:18 +00002070}
zachr@google.com28c27c82013-06-20 17:15:05 +00002071
reed@google.com7bce9982013-06-20 17:40:21 +00002072extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002073extern "C" int luaopen_skia(lua_State* L) {
2074 SkLua::Load(L);
2075 return 0;
2076}