blob: ba8a4c841ea73c9020155be53fbf9ff298e44cc5 [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"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000019#include "SkImage.h"
20#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000021#include "SkPaint.h"
22#include "SkPath.h"
reed96affcd2014-10-13 12:38:04 -070023#include "SkPictureRecorder.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000024#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000025#include "SkRRect.h"
26#include "SkString.h"
reed485557f2014-10-12 10:36:47 -070027#include "SkSurface.h"
fmalitab7425172014-08-26 07:56:44 -070028#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000029#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000030
31extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000032 #include "lua.h"
33 #include "lualib.h"
34 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000035}
36
reed@google.comfd345872013-05-22 20:53:42 +000037// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000038template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000039#define DEF_MTNAME(T) \
40 template <> const char* get_mtname<T>() { \
41 return #T "_LuaMetaTableName"; \
42 }
43
44DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000045DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000046DEF_MTNAME(SkImage)
reed468b1812014-10-19 11:42:54 -070047DEF_MTNAME(SkImageFilter)
reed@google.comfd345872013-05-22 20:53:42 +000048DEF_MTNAME(SkMatrix)
49DEF_MTNAME(SkRRect)
50DEF_MTNAME(SkPath)
51DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000052DEF_MTNAME(SkPathEffect)
reed96affcd2014-10-13 12:38:04 -070053DEF_MTNAME(SkPicture)
54DEF_MTNAME(SkPictureRecorder)
reed@google.com5fdc9832013-07-24 15:47:52 +000055DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070056DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070057DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000058DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000059
reed@google.com3597b732013-05-22 20:12:50 +000060template <typename T> T* push_new(lua_State* L) {
61 T* addr = (T*)lua_newuserdata(L, sizeof(T));
62 new (addr) T;
63 luaL_getmetatable(L, get_mtname<T>());
64 lua_setmetatable(L, -2);
65 return addr;
66}
reed@google.com74ce6f02013-05-22 15:13:18 +000067
68template <typename T> void push_obj(lua_State* L, const T& obj) {
69 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000070 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000071 lua_setmetatable(L, -2);
72}
73
74template <typename T> void push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000075 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000076 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000077 lua_setmetatable(L, -2);
78}
79
80template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000081 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000082}
83
84template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000085 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000086}
87
reed@google.com88c9ec92013-05-22 15:43:21 +000088static bool lua2bool(lua_State* L, int index) {
89 return !!lua_toboolean(L, index);
90}
91
reed@google.com74ce6f02013-05-22 15:13:18 +000092///////////////////////////////////////////////////////////////////////////////
93
reed@google.com3597b732013-05-22 20:12:50 +000094SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
95 fL = luaL_newstate();
96 luaL_openlibs(fL);
97 SkLua::Load(fL);
98}
99
100SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
101
102SkLua::~SkLua() {
103 if (fWeOwnL) {
104 if (fTermCode.size() > 0) {
105 lua_getglobal(fL, fTermCode.c_str());
106 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
107 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
108 }
109 }
110 lua_close(fL);
111 }
112}
113
114bool SkLua::runCode(const char code[]) {
115 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
116 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000117 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000118 return false;
119 }
120 return true;
121}
122
123bool SkLua::runCode(const void* code, size_t size) {
124 SkString str((const char*)code, size);
125 return this->runCode(str.c_str());
126}
127
128///////////////////////////////////////////////////////////////////////////////
129
130#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
131
reed@google.com29563872013-07-10 21:23:49 +0000132static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
133 if (pred) {
134 lua_pushboolean(L, true);
135 lua_setfield(L, -2, key);
136 }
137}
138
reed@google.com74ce6f02013-05-22 15:13:18 +0000139static void setfield_string(lua_State* L, const char key[], const char value[]) {
140 lua_pushstring(L, value);
141 lua_setfield(L, -2, key);
142}
143
144static void setfield_number(lua_State* L, const char key[], double value) {
145 lua_pushnumber(L, value);
146 lua_setfield(L, -2, key);
147}
148
humper@google.com2815c192013-07-10 22:42:30 +0000149static void setfield_boolean(lua_State* L, const char key[], bool value) {
150 lua_pushboolean(L, value);
151 lua_setfield(L, -2, key);
152}
153
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000154static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
155 setfield_number(L, key, SkScalarToLua(value));
156}
157
reed@google.com3597b732013-05-22 20:12:50 +0000158static void setfield_function(lua_State* L,
159 const char key[], lua_CFunction value) {
160 lua_pushcfunction(L, value);
161 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000162}
163
reed@google.come3823fd2013-05-30 18:55:14 +0000164static void setarray_number(lua_State* L, int index, double value) {
165 lua_pushnumber(L, value);
166 lua_rawseti(L, -2, index);
167}
168
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000169static void setarray_scalar(lua_State* L, int index, SkScalar value) {
170 setarray_number(L, index, SkScalarToLua(value));
171}
172
reed@google.com74ce6f02013-05-22 15:13:18 +0000173void SkLua::pushBool(bool value, const char key[]) {
174 lua_pushboolean(fL, value);
175 CHECK_SETFIELD(key);
176}
177
178void SkLua::pushString(const char str[], const char key[]) {
179 lua_pushstring(fL, str);
180 CHECK_SETFIELD(key);
181}
182
reed@google.come3823fd2013-05-30 18:55:14 +0000183void SkLua::pushString(const char str[], size_t length, const char key[]) {
184 // TODO: how to do this w/o making a copy?
185 SkString s(str, length);
186 lua_pushstring(fL, s.c_str());
187 CHECK_SETFIELD(key);
188}
189
reed@google.com74ce6f02013-05-22 15:13:18 +0000190void SkLua::pushString(const SkString& str, const char key[]) {
191 lua_pushstring(fL, str.c_str());
192 CHECK_SETFIELD(key);
193}
194
195void SkLua::pushColor(SkColor color, const char key[]) {
196 lua_newtable(fL);
197 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
198 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
199 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
200 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
201 CHECK_SETFIELD(key);
202}
203
reed@google.come3823fd2013-05-30 18:55:14 +0000204void SkLua::pushU32(uint32_t value, const char key[]) {
205 lua_pushnumber(fL, (double)value);
206 CHECK_SETFIELD(key);
207}
208
reed@google.com74ce6f02013-05-22 15:13:18 +0000209void SkLua::pushScalar(SkScalar value, const char key[]) {
210 lua_pushnumber(fL, SkScalarToLua(value));
211 CHECK_SETFIELD(key);
212}
213
reed@google.come3823fd2013-05-30 18:55:14 +0000214void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
215 lua_newtable(fL);
216 for (int i = 0; i < count; ++i) {
217 // make it base-1 to match lua convention
218 setarray_number(fL, i + 1, (double)array[i]);
219 }
220 CHECK_SETFIELD(key);
221}
222
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000223void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
224 lua_newtable(fL);
225 for (int i = 0; i < count; ++i) {
226 // make it base-1 to match lua convention
227 lua_newtable(fL);
228 this->pushScalar(array[i].fX, "x");
229 this->pushScalar(array[i].fY, "y");
230 lua_rawseti(fL, -2, i + 1);
231 }
232 CHECK_SETFIELD(key);
233}
234
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000235void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
236 lua_newtable(fL);
237 for (int i = 0; i < count; ++i) {
238 // make it base-1 to match lua convention
239 setarray_scalar(fL, i + 1, array[i]);
240 }
241 CHECK_SETFIELD(key);
242}
243
reed@google.com74ce6f02013-05-22 15:13:18 +0000244void SkLua::pushRect(const SkRect& r, const char key[]) {
245 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000246 setfield_scalar(fL, "left", r.fLeft);
247 setfield_scalar(fL, "top", r.fTop);
248 setfield_scalar(fL, "right", r.fRight);
249 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000250 CHECK_SETFIELD(key);
251}
252
253void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
254 push_obj(fL, rr);
255 CHECK_SETFIELD(key);
256}
257
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000258void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
259 lua_newtable(fL);
260 setfield_scalar(fL, "phase", info.fPhase);
261 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
262 CHECK_SETFIELD(key);
263}
264
265
reed@google.com74ce6f02013-05-22 15:13:18 +0000266void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
267 push_obj(fL, matrix);
268 CHECK_SETFIELD(key);
269}
270
271void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
272 push_obj(fL, paint);
273 CHECK_SETFIELD(key);
274}
275
276void SkLua::pushPath(const SkPath& path, const char key[]) {
277 push_obj(fL, path);
278 CHECK_SETFIELD(key);
279}
280
281void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
282 push_ref(fL, canvas);
283 CHECK_SETFIELD(key);
284}
285
fmalitab7425172014-08-26 07:56:44 -0700286void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
287 push_ref(fL, const_cast<SkTextBlob*>(blob));
288 CHECK_SETFIELD(key);
289}
290
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000291static const char* element_type(SkClipStack::Element::Type type) {
292 switch (type) {
293 case SkClipStack::Element::kEmpty_Type:
294 return "empty";
295 case SkClipStack::Element::kRect_Type:
296 return "rect";
297 case SkClipStack::Element::kRRect_Type:
298 return "rrect";
299 case SkClipStack::Element::kPath_Type:
300 return "path";
301 }
302 return "unknown";
303}
304
305static const char* region_op(SkRegion::Op op) {
306 switch (op) {
307 case SkRegion::kDifference_Op:
308 return "difference";
309 case SkRegion::kIntersect_Op:
310 return "intersect";
311 case SkRegion::kUnion_Op:
312 return "union";
313 case SkRegion::kXOR_Op:
314 return "xor";
315 case SkRegion::kReverseDifference_Op:
316 return "reverse-difference";
317 case SkRegion::kReplace_Op:
318 return "replace";
319 }
320 return "unknown";
321}
322
323void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
324 lua_newtable(fL);
325 SkClipStack::B2TIter iter(stack);
326 const SkClipStack::Element* element;
327 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700328 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000329 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000330 lua_rawseti(fL, -2, ++i);
331 }
332 CHECK_SETFIELD(key);
333}
334
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000335void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
336 lua_newtable(fL);
337 SkClipStack::Element::Type type = element.getType();
338 this->pushString(element_type(type), "type");
339 switch (type) {
340 case SkClipStack::Element::kEmpty_Type:
341 break;
342 case SkClipStack::Element::kRect_Type:
343 this->pushRect(element.getRect(), "rect");
344 break;
345 case SkClipStack::Element::kRRect_Type:
346 this->pushRRect(element.getRRect(), "rrect");
347 break;
348 case SkClipStack::Element::kPath_Type:
349 this->pushPath(element.getPath(), "path");
350 break;
351 }
352 this->pushString(region_op(element.getOp()), "op");
353 this->pushBool(element.isAA(), "aa");
354 CHECK_SETFIELD(key);
355}
356
357
reed@google.com74ce6f02013-05-22 15:13:18 +0000358///////////////////////////////////////////////////////////////////////////////
359///////////////////////////////////////////////////////////////////////////////
360
reed485557f2014-10-12 10:36:47 -0700361static int lua2int_def(lua_State* L, int index, int defaultValue) {
362 if (lua_isnumber(L, index)) {
363 return (int)lua_tonumber(L, index);
364 } else {
365 return defaultValue;
366 }
367}
368
reed@google.com74ce6f02013-05-22 15:13:18 +0000369static SkScalar lua2scalar(lua_State* L, int index) {
370 SkASSERT(lua_isnumber(L, index));
371 return SkLuaToScalar(lua_tonumber(L, index));
372}
373
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000374static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
375 if (lua_isnumber(L, index)) {
376 return SkLuaToScalar(lua_tonumber(L, index));
377 } else {
378 return defaultValue;
379 }
380}
381
reed@google.com74ce6f02013-05-22 15:13:18 +0000382static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
383 SkASSERT(lua_istable(L, index));
384 lua_pushstring(L, key);
385 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000386
reed@google.com74ce6f02013-05-22 15:13:18 +0000387 SkScalar value = lua2scalar(L, -1);
388 lua_pop(L, 1);
389 return value;
390}
391
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000392static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
393 SkASSERT(lua_istable(L, index));
394 lua_pushstring(L, key);
395 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000396
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000397 SkScalar value;
398 if (lua_isnil(L, -1)) {
399 value = def;
400 } else {
401 value = lua2scalar(L, -1);
402 }
403 lua_pop(L, 1);
404 return value;
405}
406
reed468b1812014-10-19 11:42:54 -0700407static SkScalar byte2unit(U8CPU byte) {
408 return byte / 255.0f;
409}
410
reed@google.com74ce6f02013-05-22 15:13:18 +0000411static U8CPU unit2byte(SkScalar x) {
412 if (x <= 0) {
413 return 0;
414 } else if (x >= 1) {
415 return 255;
416 } else {
417 return SkScalarRoundToInt(x * 255);
418 }
419}
420
421static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700422 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
423 unit2byte(getfield_scalar_default(L, index, "r", 0)),
424 unit2byte(getfield_scalar_default(L, index, "g", 0)),
425 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000426}
427
428static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000429 rect->set(getfield_scalar_default(L, index, "left", 0),
430 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000431 getfield_scalar(L, index, "right"),
432 getfield_scalar(L, index, "bottom"));
433 return rect;
434}
435
reedf355df52014-10-12 12:18:40 -0700436static int lcanvas_clear(lua_State* L) {
437 get_ref<SkCanvas>(L, 1)->clear(0);
438 return 0;
439}
440
reed@google.com74ce6f02013-05-22 15:13:18 +0000441static int lcanvas_drawColor(lua_State* L) {
442 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
443 return 0;
444}
445
446static int lcanvas_drawRect(lua_State* L) {
447 SkRect rect;
448 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
449 *get_obj<SkPaint>(L, 3));
450 return 0;
451}
452
453static int lcanvas_drawOval(lua_State* L) {
454 SkRect rect;
455 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
456 *get_obj<SkPaint>(L, 3));
457 return 0;
458}
459
460static int lcanvas_drawCircle(lua_State* L) {
461 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
462 lua2scalar(L, 3),
463 lua2scalar(L, 4),
464 *get_obj<SkPaint>(L, 5));
465 return 0;
466}
467
reed485557f2014-10-12 10:36:47 -0700468static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
469 if (lua_isnumber(L, index)) {
470 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
471 return paint;
reedf355df52014-10-12 12:18:40 -0700472 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700473 const SkPaint* ptr = get_obj<SkPaint>(L, index);
474 if (ptr) {
475 *paint = *ptr;
476 return paint;
477 }
478 }
479 return NULL;
480}
481
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000482static int lcanvas_drawImage(lua_State* L) {
483 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
484 SkImage* image = get_ref<SkImage>(L, 2);
485 if (NULL == image) {
486 return 0;
487 }
488 SkScalar x = lua2scalar(L, 3);
489 SkScalar y = lua2scalar(L, 4);
490
491 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700492 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000493 return 0;
494}
495
reedba5fb932014-10-10 15:28:19 -0700496static int lcanvas_drawImageRect(lua_State* L) {
497 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
498 SkImage* image = get_ref<SkImage>(L, 2);
499 if (NULL == image) {
500 return 0;
501 }
502
503 SkRect srcR, dstR;
504 SkRect* srcRPtr = NULL;
505 if (!lua_isnil(L, 3)) {
506 srcRPtr = lua2rect(L, 3, &srcR);
507 }
508 lua2rect(L, 4, &dstR);
509
510 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700511 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700512 return 0;
513}
514
reed@google.comfd345872013-05-22 20:53:42 +0000515static int lcanvas_drawPath(lua_State* L) {
516 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
517 *get_obj<SkPaint>(L, 3));
518 return 0;
519}
520
reed96affcd2014-10-13 12:38:04 -0700521// drawPicture(pic, x, y, paint)
522static int lcanvas_drawPicture(lua_State* L) {
523 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
524 SkPicture* picture = get_ref<SkPicture>(L, 2);
525 SkScalar x = lua2scalar_def(L, 3, 0);
526 SkScalar y = lua2scalar_def(L, 4, 0);
527 SkMatrix matrix, *matrixPtr = NULL;
528 if (x || y) {
529 matrix.setTranslate(x, y);
530 matrixPtr = &matrix;
531 }
532 SkPaint paint;
533 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
534 return 0;
535}
536
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000537static int lcanvas_drawText(lua_State* L) {
538 if (lua_gettop(L) < 5) {
539 return 0;
540 }
541
542 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
543 size_t len;
544 const char* text = lua_tolstring(L, 2, &len);
545 get_ref<SkCanvas>(L, 1)->drawText(text, len,
546 lua2scalar(L, 3), lua2scalar(L, 4),
547 *get_obj<SkPaint>(L, 5));
548 }
549 return 0;
550}
551
reed@google.com74ce6f02013-05-22 15:13:18 +0000552static int lcanvas_getSaveCount(lua_State* L) {
553 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
554 return 1;
555}
556
557static int lcanvas_getTotalMatrix(lua_State* L) {
558 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
559 return 1;
560}
561
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000562static int lcanvas_getClipStack(lua_State* L) {
563 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
564 return 1;
565}
566
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000567int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
568#if SK_SUPPORT_GPU
569 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
570 SkISize layerSize = canvas->getTopLayerSize();
571 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
572 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
573 layerSize.fWidth, layerSize.fHeight);
574
575 GrReducedClip::ElementList elements;
576 GrReducedClip::InitialState initialState;
577 int32_t genID;
578 SkIRect resultBounds;
579
580 const SkClipStack& stack = *canvas->getClipStack();
581
582 GrReducedClip::ReduceClipStack(stack,
583 queryBounds,
584 &elements,
585 &genID,
586 &initialState,
587 &resultBounds,
588 NULL);
589
590 GrReducedClip::ElementList::Iter iter(elements);
591 int i = 0;
592 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700593 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000594 SkLua(L).pushClipStackElement(*iter.get());
595 iter.next();
596 lua_rawseti(L, -2, ++i);
597 }
598 // Currently this only returns the element list to lua, not the initial state or result bounds.
599 // It could return these as additional items on the lua stack.
600 return 1;
601#else
602 return 0;
603#endif
604}
605
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000606static int lcanvas_save(lua_State* L) {
607 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
608 return 1;
609}
610
611static int lcanvas_restore(lua_State* L) {
612 get_ref<SkCanvas>(L, 1)->restore();
613 return 0;
614}
615
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000616static int lcanvas_scale(lua_State* L) {
617 SkScalar sx = lua2scalar_def(L, 2, 1);
618 SkScalar sy = lua2scalar_def(L, 3, sx);
619 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
620 return 0;
621}
622
reed@google.com3597b732013-05-22 20:12:50 +0000623static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000624 SkScalar tx = lua2scalar_def(L, 2, 0);
625 SkScalar ty = lua2scalar_def(L, 3, 0);
626 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
627 return 0;
628}
629
630static int lcanvas_rotate(lua_State* L) {
631 SkScalar degrees = lua2scalar_def(L, 2, 0);
632 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000633 return 0;
634}
635
reedbdc49ae2014-10-14 09:34:52 -0700636static int lcanvas_concat(lua_State* L) {
637 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
638 return 0;
639}
640
reed485557f2014-10-12 10:36:47 -0700641static int lcanvas_newSurface(lua_State* L) {
642 int width = lua2int_def(L, 2, 0);
643 int height = lua2int_def(L, 2, 0);
644 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
645 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
646 if (NULL == surface) {
647 lua_pushnil(L);
648 } else {
649 push_ref(L, surface);
650 surface->unref();
651 }
652 return 1;
653}
654
reed@google.com74ce6f02013-05-22 15:13:18 +0000655static int lcanvas_gc(lua_State* L) {
656 get_ref<SkCanvas>(L, 1)->unref();
657 return 0;
658}
659
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000660const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700661 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000662 { "drawColor", lcanvas_drawColor },
663 { "drawRect", lcanvas_drawRect },
664 { "drawOval", lcanvas_drawOval },
665 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000666 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700667 { "drawImageRect", lcanvas_drawImageRect },
reed@google.comfd345872013-05-22 20:53:42 +0000668 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700669 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000670 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000671 { "getSaveCount", lcanvas_getSaveCount },
672 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000673 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000674#if SK_SUPPORT_GPU
675 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
676#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000677 { "save", lcanvas_save },
678 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000679 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000680 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000681 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700682 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700683
684 { "newSurface", lcanvas_newSurface },
685
reed@google.com74ce6f02013-05-22 15:13:18 +0000686 { "__gc", lcanvas_gc },
687 { NULL, NULL }
688};
689
690///////////////////////////////////////////////////////////////////////////////
691
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000692static int ldocument_beginPage(lua_State* L) {
693 const SkRect* contentPtr = NULL;
694 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
695 lua2scalar(L, 3),
696 contentPtr));
697 return 1;
698}
699
700static int ldocument_endPage(lua_State* L) {
701 get_ref<SkDocument>(L, 1)->endPage();
702 return 0;
703}
704
705static int ldocument_close(lua_State* L) {
706 get_ref<SkDocument>(L, 1)->close();
707 return 0;
708}
709
710static int ldocument_gc(lua_State* L) {
711 get_ref<SkDocument>(L, 1)->unref();
712 return 0;
713}
714
715static const struct luaL_Reg gSkDocument_Methods[] = {
716 { "beginPage", ldocument_beginPage },
717 { "endPage", ldocument_endPage },
718 { "close", ldocument_close },
719 { "__gc", ldocument_gc },
720 { NULL, NULL }
721};
722
723///////////////////////////////////////////////////////////////////////////////
724
reed@google.com74ce6f02013-05-22 15:13:18 +0000725static int lpaint_isAntiAlias(lua_State* L) {
726 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
727 return 1;
728}
729
730static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000731 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000732 return 0;
733}
734
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000735static int lpaint_isDither(lua_State* L) {
736 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
737 return 1;
738}
739
740static int lpaint_isUnderlineText(lua_State* L) {
741 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
742 return 1;
743}
744
745static int lpaint_isStrikeThruText(lua_State* L) {
746 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
747 return 1;
748}
749
750static int lpaint_isFakeBoldText(lua_State* L) {
751 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
752 return 1;
753}
754
755static int lpaint_isLinearText(lua_State* L) {
756 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
757 return 1;
758}
759
760static int lpaint_isSubpixelText(lua_State* L) {
761 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
762 return 1;
763}
764
reed09a1d672014-10-11 13:13:11 -0700765static int lpaint_setSubpixelText(lua_State* L) {
766 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
767 return 1;
768}
769
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000770static int lpaint_isDevKernText(lua_State* L) {
771 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
772 return 1;
773}
774
775static int lpaint_isLCDRenderText(lua_State* L) {
776 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
777 return 1;
778}
779
780static int lpaint_isEmbeddedBitmapText(lua_State* L) {
781 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
782 return 1;
783}
784
785static int lpaint_isAutohinted(lua_State* L) {
786 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
787 return 1;
788}
789
790static int lpaint_isVerticalText(lua_State* L) {
791 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
792 return 1;
793}
794
reed468b1812014-10-19 11:42:54 -0700795static int lpaint_getAlpha(lua_State* L) {
796 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
797 return 1;
798}
799
800static int lpaint_setAlpha(lua_State* L) {
801 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
802 return 0;
803}
804
reed@google.com74ce6f02013-05-22 15:13:18 +0000805static int lpaint_getColor(lua_State* L) {
806 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
807 return 1;
808}
809
810static int lpaint_setColor(lua_State* L) {
811 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
812 return 0;
813}
814
reed@google.come3823fd2013-05-30 18:55:14 +0000815static int lpaint_getTextSize(lua_State* L) {
816 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
817 return 1;
818}
819
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000820static int lpaint_getTextScaleX(lua_State* L) {
821 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
822 return 1;
823}
824
825static int lpaint_getTextSkewX(lua_State* L) {
826 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
827 return 1;
828}
829
reed@google.come3823fd2013-05-30 18:55:14 +0000830static int lpaint_setTextSize(lua_State* L) {
831 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
832 return 0;
833}
834
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000835static int lpaint_getTypeface(lua_State* L) {
836 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
837 return 1;
838}
839
840static int lpaint_setTypeface(lua_State* L) {
841 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
842 return 0;
843}
844
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000845static int lpaint_getHinting(lua_State* L) {
846 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
847 return 1;
848}
849
reed@google.come3823fd2013-05-30 18:55:14 +0000850static int lpaint_getFontID(lua_State* L) {
851 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
852 SkLua(L).pushU32(SkTypeface::UniqueID(face));
853 return 1;
854}
855
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000856static const struct {
857 const char* fLabel;
858 SkPaint::Align fAlign;
859} gAlignRec[] = {
860 { "left", SkPaint::kLeft_Align },
861 { "center", SkPaint::kCenter_Align },
862 { "right", SkPaint::kRight_Align },
863};
864
865static int lpaint_getTextAlign(lua_State* L) {
866 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
867 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
868 if (gAlignRec[i].fAlign == align) {
869 lua_pushstring(L, gAlignRec[i].fLabel);
870 return 1;
871 }
872 }
873 return 0;
874}
875
876static int lpaint_setTextAlign(lua_State* L) {
877 if (lua_isstring(L, 2)) {
878 size_t len;
879 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000880
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000881 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
882 if (!strcmp(gAlignRec[i].fLabel, label)) {
883 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
884 break;
885 }
886 }
887 }
888 return 0;
889}
890
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000891static int lpaint_getStroke(lua_State* L) {
892 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
893 return 1;
894}
895
896static int lpaint_setStroke(lua_State* L) {
897 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000898
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000899 if (lua_toboolean(L, 2)) {
900 style = SkPaint::kStroke_Style;
901 } else {
902 style = SkPaint::kFill_Style;
903 }
904 get_obj<SkPaint>(L, 1)->setStyle(style);
905 return 0;
906}
907
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000908static int lpaint_getStrokeCap(lua_State* L) {
909 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
910 return 1;
911}
912
913static int lpaint_getStrokeJoin(lua_State* L) {
914 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
915 return 1;
916}
917
918static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000919 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000920 return 1;
921}
922
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000923static int lpaint_getStrokeWidth(lua_State* L) {
924 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
925 return 1;
926}
927
928static int lpaint_setStrokeWidth(lua_State* L) {
929 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
930 return 0;
931}
932
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000933static int lpaint_getStrokeMiter(lua_State* L) {
934 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
935 return 1;
936}
937
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000938static int lpaint_measureText(lua_State* L) {
939 if (lua_isstring(L, 2)) {
940 size_t len;
941 const char* text = lua_tolstring(L, 2, &len);
942 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
943 return 1;
944 }
945 return 0;
946}
947
948struct FontMetrics {
949 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
950 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
951 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
952 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
953 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
954 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
955 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
956 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
957 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
958};
959
960static int lpaint_getFontMetrics(lua_State* L) {
961 SkPaint::FontMetrics fm;
962 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000963
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000964 lua_newtable(L);
965 setfield_scalar(L, "top", fm.fTop);
966 setfield_scalar(L, "ascent", fm.fAscent);
967 setfield_scalar(L, "descent", fm.fDescent);
968 setfield_scalar(L, "bottom", fm.fBottom);
969 setfield_scalar(L, "leading", fm.fLeading);
970 SkLua(L).pushScalar(height);
971 return 2;
972}
973
reed@google.com29563872013-07-10 21:23:49 +0000974static int lpaint_getEffects(lua_State* L) {
975 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000976
reed@google.com29563872013-07-10 21:23:49 +0000977 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -0700978 setfield_bool_if(L, "looper", !!paint->getLooper());
979 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
980 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
981 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
982 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +0000983 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
984 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -0700985 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +0000986 return 1;
987}
988
reed468b1812014-10-19 11:42:54 -0700989static int lpaint_getImageFilter(lua_State* L) {
990 const SkPaint* paint = get_obj<SkPaint>(L, 1);
991 SkImageFilter* imf = paint->getImageFilter();
992 if (imf) {
993 push_ref(L, imf);
994 return 1;
995 }
996 return 0;
997}
998
999static int lpaint_setImageFilter(lua_State* L) {
1000 SkPaint* paint = get_obj<SkPaint>(L, 1);
1001 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1002 return 0;
1003}
1004
reed@google.com5fdc9832013-07-24 15:47:52 +00001005static int lpaint_getShader(lua_State* L) {
1006 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1007 SkShader* shader = paint->getShader();
1008 if (shader) {
1009 push_ref(L, shader);
1010 return 1;
1011 }
1012 return 0;
1013}
1014
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001015static int lpaint_getPathEffect(lua_State* L) {
1016 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1017 SkPathEffect* pe = paint->getPathEffect();
1018 if (pe) {
1019 push_ref(L, pe);
1020 return 1;
1021 }
1022 return 0;
1023}
1024
reed@google.com74ce6f02013-05-22 15:13:18 +00001025static int lpaint_gc(lua_State* L) {
1026 get_obj<SkPaint>(L, 1)->~SkPaint();
1027 return 0;
1028}
1029
1030static const struct luaL_Reg gSkPaint_Methods[] = {
1031 { "isAntiAlias", lpaint_isAntiAlias },
1032 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001033 { "isDither", lpaint_isDither },
1034 { "isUnderlineText", lpaint_isUnderlineText },
1035 { "isStrikeThruText", lpaint_isStrikeThruText },
1036 { "isFakeBoldText", lpaint_isFakeBoldText },
1037 { "isLinearText", lpaint_isLinearText },
1038 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001039 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001040 { "isDevKernText", lpaint_isDevKernText },
1041 { "isLCDRenderText", lpaint_isLCDRenderText },
1042 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1043 { "isAutohinted", lpaint_isAutohinted },
1044 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001045 { "getAlpha", lpaint_getAlpha },
1046 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001047 { "getColor", lpaint_getColor },
1048 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001049 { "getTextSize", lpaint_getTextSize },
1050 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001051 { "getTextScaleX", lpaint_getTextScaleX },
1052 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001053 { "getTypeface", lpaint_getTypeface },
1054 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001055 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001056 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001057 { "getTextAlign", lpaint_getTextAlign },
1058 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001059 { "getStroke", lpaint_getStroke },
1060 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001061 { "getStrokeCap", lpaint_getStrokeCap },
1062 { "getStrokeJoin", lpaint_getStrokeJoin },
1063 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001064 { "getStrokeWidth", lpaint_getStrokeWidth },
1065 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001066 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001067 { "measureText", lpaint_measureText },
1068 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001069 { "getEffects", lpaint_getEffects },
reed468b1812014-10-19 11:42:54 -07001070 { "getImageFilter", lpaint_getImageFilter },
1071 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001072 { "getShader", lpaint_getShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001073 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001074 { "__gc", lpaint_gc },
1075 { NULL, NULL }
1076};
1077
1078///////////////////////////////////////////////////////////////////////////////
1079
reed@google.com5fdc9832013-07-24 15:47:52 +00001080static const char* mode2string(SkShader::TileMode mode) {
1081 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1082 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1083 return gNames[mode];
1084}
1085
1086static const char* gradtype2string(SkShader::GradientType t) {
1087 static const char* gNames[] = {
1088 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1089 };
1090 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1091 return gNames[t];
1092}
1093
1094static int lshader_isOpaque(lua_State* L) {
1095 SkShader* shader = get_ref<SkShader>(L, 1);
1096 return shader && shader->isOpaque();
1097}
1098
1099static int lshader_asABitmap(lua_State* L) {
1100 SkShader* shader = get_ref<SkShader>(L, 1);
1101 if (shader) {
1102 SkBitmap bm;
1103 SkMatrix matrix;
1104 SkShader::TileMode modes[2];
1105 switch (shader->asABitmap(&bm, &matrix, modes)) {
1106 case SkShader::kDefault_BitmapType:
1107 lua_newtable(L);
1108 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1109 setfield_number(L, "width", bm.width());
1110 setfield_number(L, "height", bm.height());
1111 setfield_string(L, "tileX", mode2string(modes[0]));
1112 setfield_string(L, "tileY", mode2string(modes[1]));
1113 return 1;
1114 default:
1115 break;
1116 }
1117 }
1118 return 0;
1119}
1120
1121static int lshader_asAGradient(lua_State* L) {
1122 SkShader* shader = get_ref<SkShader>(L, 1);
1123 if (shader) {
1124 SkShader::GradientInfo info;
1125 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001126
1127 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001128 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001129
1130 info.fColorCount = 3;
1131 info.fColors = &colors[0];
1132 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001133
reed@google.com5fdc9832013-07-24 15:47:52 +00001134 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001135
reed@google.com5fdc9832013-07-24 15:47:52 +00001136 if (SkShader::kNone_GradientType != t) {
1137 lua_newtable(L);
1138 setfield_string(L, "type", gradtype2string(t));
1139 setfield_number(L, "colorCount", info.fColorCount);
1140 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001141
1142 if (info.fColorCount == 3){
1143 setfield_number(L, "midPos", pos[1]);
1144 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001145
reed@google.com5fdc9832013-07-24 15:47:52 +00001146 return 1;
1147 }
1148 }
1149 return 0;
1150}
1151
1152static int lshader_gc(lua_State* L) {
1153 get_ref<SkShader>(L, 1)->unref();
1154 return 0;
1155}
1156
1157static const struct luaL_Reg gSkShader_Methods[] = {
1158 { "isOpaque", lshader_isOpaque },
1159 { "asABitmap", lshader_asABitmap },
1160 { "asAGradient", lshader_asAGradient },
1161 { "__gc", lshader_gc },
1162 { NULL, NULL }
1163};
1164
1165///////////////////////////////////////////////////////////////////////////////
1166
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001167static int lpatheffect_asADash(lua_State* L) {
1168 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1169 if (pe) {
1170 SkPathEffect::DashInfo info;
1171 SkPathEffect::DashType dashType = pe->asADash(&info);
1172 if (SkPathEffect::kDash_DashType == dashType) {
1173 SkAutoTArray<SkScalar> intervals(info.fCount);
1174 info.fIntervals = intervals.get();
1175 pe->asADash(&info);
1176 SkLua(L).pushDash(info);
1177 return 1;
1178 }
1179 }
1180 return 0;
1181}
1182
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001183static int lpatheffect_gc(lua_State* L) {
1184 get_ref<SkPathEffect>(L, 1)->unref();
1185 return 0;
1186}
1187
1188static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001189 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001190 { "__gc", lpatheffect_gc },
1191 { NULL, NULL }
1192};
1193
1194///////////////////////////////////////////////////////////////////////////////
1195
reed468b1812014-10-19 11:42:54 -07001196static int lpimagefilter_gc(lua_State* L) {
1197 get_ref<SkImageFilter>(L, 1)->unref();
1198 return 0;
1199}
1200
1201static const struct luaL_Reg gSkImageFilter_Methods[] = {
1202 { "__gc", lpimagefilter_gc },
1203 { NULL, NULL }
1204};
1205
1206///////////////////////////////////////////////////////////////////////////////
1207
humper@google.com2815c192013-07-10 22:42:30 +00001208static int lmatrix_getType(lua_State* L) {
1209 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001210
humper@google.com2815c192013-07-10 22:42:30 +00001211 lua_newtable(L);
1212 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1213 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1214 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1215 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1216 return 1;
1217}
1218
humper@google.com0f48ee02013-07-26 15:23:43 +00001219static int lmatrix_getScaleX(lua_State* L) {
1220 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1221 return 1;
1222}
1223
1224static int lmatrix_getScaleY(lua_State* L) {
1225 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1226 return 1;
1227}
1228
1229static int lmatrix_getTranslateX(lua_State* L) {
1230 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1231 return 1;
1232}
1233
1234static int lmatrix_getTranslateY(lua_State* L) {
1235 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1236 return 1;
1237}
1238
reedbdc49ae2014-10-14 09:34:52 -07001239static int lmatrix_setRectToRect(lua_State* L) {
1240 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1241 SkRect srcR, dstR;
1242 lua2rect(L, 2, &srcR);
1243 lua2rect(L, 3, &dstR);
1244 const char* scaleToFitStr = lua_tostring(L, 4);
1245 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1246
1247 if (scaleToFitStr) {
1248 const struct {
1249 const char* fName;
1250 SkMatrix::ScaleToFit fScaleToFit;
1251 } rec[] = {
1252 { "fill", SkMatrix::kFill_ScaleToFit },
1253 { "start", SkMatrix::kStart_ScaleToFit },
1254 { "center", SkMatrix::kCenter_ScaleToFit },
1255 { "end", SkMatrix::kEnd_ScaleToFit },
1256 };
1257
1258 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1259 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1260 scaleToFit = rec[i].fScaleToFit;
1261 break;
1262 }
1263 }
1264 }
1265
1266 matrix->setRectToRect(srcR, dstR, scaleToFit);
1267 return 0;
1268}
1269
humper@google.com2815c192013-07-10 22:42:30 +00001270static const struct luaL_Reg gSkMatrix_Methods[] = {
1271 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001272 { "getScaleX", lmatrix_getScaleX },
1273 { "getScaleY", lmatrix_getScaleY },
1274 { "getTranslateX", lmatrix_getTranslateX },
1275 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001276 { "setRectToRect", lmatrix_setRectToRect },
humper@google.com2815c192013-07-10 22:42:30 +00001277 { NULL, NULL }
1278};
1279
1280///////////////////////////////////////////////////////////////////////////////
1281
reed@google.com74ce6f02013-05-22 15:13:18 +00001282static int lpath_getBounds(lua_State* L) {
1283 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1284 return 1;
1285}
1286
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001287static const char* fill_type_to_str(SkPath::FillType fill) {
1288 switch (fill) {
1289 case SkPath::kEvenOdd_FillType:
1290 return "even-odd";
1291 case SkPath::kWinding_FillType:
1292 return "winding";
1293 case SkPath::kInverseEvenOdd_FillType:
1294 return "inverse-even-odd";
1295 case SkPath::kInverseWinding_FillType:
1296 return "inverse-winding";
1297 }
1298 return "unknown";
1299}
1300
1301static int lpath_getFillType(lua_State* L) {
1302 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1303 SkLua(L).pushString(fill_type_to_str(fill));
1304 return 1;
1305}
1306
1307static SkString segment_masks_to_str(uint32_t segmentMasks) {
1308 SkString result;
1309 bool first = true;
1310 if (SkPath::kLine_SegmentMask & segmentMasks) {
1311 result.append("line");
1312 first = false;
1313 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1314 }
1315 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1316 if (!first) {
1317 result.append(" ");
1318 }
1319 result.append("quad");
1320 first = false;
1321 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1322 }
1323 if (SkPath::kConic_SegmentMask & segmentMasks) {
1324 if (!first) {
1325 result.append(" ");
1326 }
1327 result.append("conic");
1328 first = false;
1329 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1330 }
1331 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1332 if (!first) {
1333 result.append(" ");
1334 }
1335 result.append("cubic");
1336 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1337 }
1338 SkASSERT(0 == segmentMasks);
1339 return result;
1340}
1341
krajcevski95498ed2014-08-18 08:02:33 -07001342static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001343 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1344 SkLua(L).pushString(segment_masks_to_str(segMasks));
1345 return 1;
1346}
1347
1348static int lpath_isConvex(lua_State* L) {
1349 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1350 SkLua(L).pushBool(isConvex);
1351 return 1;
1352}
1353
reed@google.com74ce6f02013-05-22 15:13:18 +00001354static int lpath_isEmpty(lua_State* L) {
1355 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1356 return 1;
1357}
1358
1359static int lpath_isRect(lua_State* L) {
1360 SkRect r;
1361 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1362 int ret_count = 1;
1363 lua_pushboolean(L, pred);
1364 if (pred) {
1365 SkLua(L).pushRect(r);
1366 ret_count += 1;
1367 }
1368 return ret_count;
1369}
1370
1371static const char* dir2string(SkPath::Direction dir) {
1372 static const char* gStr[] = {
1373 "unknown", "cw", "ccw"
1374 };
1375 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1376 return gStr[dir];
1377}
1378
1379static int lpath_isNestedRects(lua_State* L) {
1380 SkRect rects[2];
1381 SkPath::Direction dirs[2];
1382 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1383 int ret_count = 1;
1384 lua_pushboolean(L, pred);
1385 if (pred) {
1386 SkLua lua(L);
1387 lua.pushRect(rects[0]);
1388 lua.pushRect(rects[1]);
1389 lua_pushstring(L, dir2string(dirs[0]));
1390 lua_pushstring(L, dir2string(dirs[0]));
1391 ret_count += 4;
1392 }
1393 return ret_count;
1394}
1395
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001396static int lpath_countPoints(lua_State* L) {
1397 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1398 return 1;
1399}
1400
reed@google.com74ce6f02013-05-22 15:13:18 +00001401static int lpath_reset(lua_State* L) {
1402 get_obj<SkPath>(L, 1)->reset();
1403 return 0;
1404}
1405
1406static int lpath_moveTo(lua_State* L) {
1407 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1408 return 0;
1409}
1410
1411static int lpath_lineTo(lua_State* L) {
1412 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1413 return 0;
1414}
1415
1416static int lpath_quadTo(lua_State* L) {
1417 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1418 lua2scalar(L, 4), lua2scalar(L, 5));
1419 return 0;
1420}
1421
1422static int lpath_cubicTo(lua_State* L) {
1423 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1424 lua2scalar(L, 4), lua2scalar(L, 5),
1425 lua2scalar(L, 6), lua2scalar(L, 7));
1426 return 0;
1427}
1428
1429static int lpath_close(lua_State* L) {
1430 get_obj<SkPath>(L, 1)->close();
1431 return 0;
1432}
1433
1434static int lpath_gc(lua_State* L) {
1435 get_obj<SkPath>(L, 1)->~SkPath();
1436 return 0;
1437}
1438
1439static const struct luaL_Reg gSkPath_Methods[] = {
1440 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001441 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001442 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001443 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001444 { "isEmpty", lpath_isEmpty },
1445 { "isRect", lpath_isRect },
1446 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001447 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001448 { "reset", lpath_reset },
1449 { "moveTo", lpath_moveTo },
1450 { "lineTo", lpath_lineTo },
1451 { "quadTo", lpath_quadTo },
1452 { "cubicTo", lpath_cubicTo },
1453 { "close", lpath_close },
1454 { "__gc", lpath_gc },
1455 { NULL, NULL }
1456};
1457
1458///////////////////////////////////////////////////////////////////////////////
1459
1460static const char* rrect_type(const SkRRect& rr) {
1461 switch (rr.getType()) {
1462 case SkRRect::kUnknown_Type: return "unknown";
1463 case SkRRect::kEmpty_Type: return "empty";
1464 case SkRRect::kRect_Type: return "rect";
1465 case SkRRect::kOval_Type: return "oval";
1466 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001467 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001468 case SkRRect::kComplex_Type: return "complex";
1469 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001470 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001471 return "";
1472}
1473
1474static int lrrect_rect(lua_State* L) {
1475 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1476 return 1;
1477}
1478
1479static int lrrect_type(lua_State* L) {
1480 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1481 return 1;
1482}
1483
1484static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001485 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001486 SkVector v;
1487 if (corner < 0 || corner > 3) {
1488 SkDebugf("bad corner index %d", corner);
1489 v.set(0, 0);
1490 } else {
1491 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1492 }
1493 lua_pushnumber(L, v.fX);
1494 lua_pushnumber(L, v.fY);
1495 return 2;
1496}
1497
1498static int lrrect_gc(lua_State* L) {
1499 get_obj<SkRRect>(L, 1)->~SkRRect();
1500 return 0;
1501}
1502
1503static const struct luaL_Reg gSkRRect_Methods[] = {
1504 { "rect", lrrect_rect },
1505 { "type", lrrect_type },
1506 { "radii", lrrect_radii },
1507 { "__gc", lrrect_gc },
1508 { NULL, NULL }
1509};
1510
1511///////////////////////////////////////////////////////////////////////////////
1512
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001513static int limage_width(lua_State* L) {
1514 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1515 return 1;
1516}
1517
1518static int limage_height(lua_State* L) {
1519 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1520 return 1;
1521}
1522
1523static int limage_gc(lua_State* L) {
1524 get_ref<SkImage>(L, 1)->unref();
1525 return 0;
1526}
1527
1528static const struct luaL_Reg gSkImage_Methods[] = {
1529 { "width", limage_width },
1530 { "height", limage_height },
1531 { "__gc", limage_gc },
1532 { NULL, NULL }
1533};
1534
1535///////////////////////////////////////////////////////////////////////////////
1536
reed485557f2014-10-12 10:36:47 -07001537static int lsurface_width(lua_State* L) {
1538 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1539 return 1;
1540}
1541
1542static int lsurface_height(lua_State* L) {
1543 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1544 return 1;
1545}
1546
1547static int lsurface_getCanvas(lua_State* L) {
1548 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1549 if (NULL == canvas) {
1550 lua_pushnil(L);
1551 } else {
1552 push_ref(L, canvas);
1553 // note: we don't unref canvas, since getCanvas did not ref it.
1554 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1555 // the real owner (the surface) go away, but still hold onto the canvas?
1556 // *really* we want to sort of ref the surface again, but have the native object
1557 // know that it is supposed to be treated as a canvas...
1558 }
1559 return 1;
1560}
1561
1562static int lsurface_newImageSnapshot(lua_State* L) {
1563 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1564 if (NULL == image) {
1565 lua_pushnil(L);
1566 } else {
1567 push_ref(L, image);
1568 image->unref();
1569 }
1570 return 1;
1571}
1572
1573static int lsurface_newSurface(lua_State* L) {
1574 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001575 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001576 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1577 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1578 if (NULL == surface) {
1579 lua_pushnil(L);
1580 } else {
1581 push_ref(L, surface);
1582 surface->unref();
1583 }
1584 return 1;
1585}
1586
1587static int lsurface_gc(lua_State* L) {
1588 get_ref<SkSurface>(L, 1)->unref();
1589 return 0;
1590}
1591
1592static const struct luaL_Reg gSkSurface_Methods[] = {
1593 { "width", lsurface_width },
1594 { "height", lsurface_height },
1595 { "getCanvas", lsurface_getCanvas },
1596 { "newImageSnapshot", lsurface_newImageSnapshot },
1597 { "newSurface", lsurface_newSurface },
1598 { "__gc", lsurface_gc },
1599 { NULL, NULL }
1600};
1601
1602///////////////////////////////////////////////////////////////////////////////
1603
reed96affcd2014-10-13 12:38:04 -07001604static int lpicturerecorder_beginRecording(lua_State* L) {
1605 const SkScalar w = lua2scalar_def(L, 2, -1);
1606 const SkScalar h = lua2scalar_def(L, 3, -1);
1607 if (w <= 0 || h <= 0) {
1608 lua_pushnil(L);
1609 return 1;
1610 }
1611
1612 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1613 if (NULL == canvas) {
1614 lua_pushnil(L);
1615 return 1;
1616 }
1617
1618 push_ref(L, canvas);
1619 return 1;
1620}
1621
1622static int lpicturerecorder_getCanvas(lua_State* L) {
1623 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1624 if (NULL == canvas) {
1625 lua_pushnil(L);
1626 return 1;
1627 }
1628 push_ref(L, canvas);
1629 return 1;
1630}
1631
1632static int lpicturerecorder_endRecording(lua_State* L) {
1633 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1634 if (NULL == pic) {
1635 lua_pushnil(L);
1636 return 1;
1637 }
1638 push_ref(L, pic);
1639 pic->unref(); // lua is the only owner, so we unref ours
1640 return 1;
1641}
1642
1643static int lpicturerecorder_gc(lua_State* L) {
1644 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1645 return 0;
1646}
1647
1648static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1649 { "beginRecording", lpicturerecorder_beginRecording },
1650 { "getCanvas", lpicturerecorder_getCanvas },
1651 { "endRecording", lpicturerecorder_endRecording },
1652 { "__gc", lpicturerecorder_gc },
1653 { NULL, NULL }
1654};
1655
1656///////////////////////////////////////////////////////////////////////////////
1657
1658static int lpicture_width(lua_State* L) {
1659 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1660 return 1;
1661}
1662
1663static int lpicture_height(lua_State* L) {
1664 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1665 return 1;
1666}
1667
1668static int lpicture_gc(lua_State* L) {
1669 get_ref<SkPicture>(L, 1)->unref();
1670 return 0;
1671}
1672
1673static const struct luaL_Reg gSkPicture_Methods[] = {
1674 { "width", lpicture_width },
1675 { "height", lpicture_height },
1676 { "__gc", lpicture_gc },
1677 { NULL, NULL }
1678};
1679
1680///////////////////////////////////////////////////////////////////////////////
1681
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001682static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001683 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001684 return 0;
1685}
1686
1687static const struct luaL_Reg gSkTypeface_Methods[] = {
1688 { "__gc", ltypeface_gc },
1689 { NULL, NULL }
1690};
1691
1692///////////////////////////////////////////////////////////////////////////////
1693
reed@google.com74ce6f02013-05-22 15:13:18 +00001694class AutoCallLua {
1695public:
1696 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1697 lua_getglobal(L, func);
1698 if (!lua_isfunction(L, -1)) {
1699 int t = lua_type(L, -1);
1700 SkDebugf("--- expected function %d\n", t);
1701 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001702
reed@google.com74ce6f02013-05-22 15:13:18 +00001703 lua_newtable(L);
1704 setfield_string(L, "verb", verb);
1705 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001706
reed@google.com74ce6f02013-05-22 15:13:18 +00001707 ~AutoCallLua() {
1708 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1709 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1710 }
1711 lua_settop(fL, -1);
1712 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001713
reed@google.com74ce6f02013-05-22 15:13:18 +00001714private:
1715 lua_State* fL;
1716};
1717
1718#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1719
1720///////////////////////////////////////////////////////////////////////////////
1721
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001722static int lsk_newDocumentPDF(lua_State* L) {
1723 const char* file = NULL;
1724 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1725 file = lua_tolstring(L, 1, NULL);
1726 }
1727
1728 SkDocument* doc = SkDocument::CreatePDF(file);
1729 if (NULL == doc) {
1730 // do I need to push a nil on the stack and return 1?
1731 return 0;
1732 } else {
1733 push_ref(L, doc);
1734 doc->unref();
1735 return 1;
1736 }
1737}
1738
reed468b1812014-10-19 11:42:54 -07001739static int lsk_newBlurImageFilter(lua_State* L) {
1740 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1741 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1742 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1743 if (NULL == imf) {
1744 lua_pushnil(L);
1745 } else {
1746 push_ref(L, imf);
1747 imf->unref();
1748 }
1749 return 1;
1750}
1751
reedbdc49ae2014-10-14 09:34:52 -07001752static int lsk_newMatrix(lua_State* L) {
1753 push_new<SkMatrix>(L)->reset();
1754 return 1;
1755}
1756
reed@google.com3597b732013-05-22 20:12:50 +00001757static int lsk_newPaint(lua_State* L) {
1758 push_new<SkPaint>(L);
1759 return 1;
1760}
1761
1762static int lsk_newPath(lua_State* L) {
1763 push_new<SkPath>(L);
1764 return 1;
1765}
1766
reed96affcd2014-10-13 12:38:04 -07001767static int lsk_newPictureRecorder(lua_State* L) {
1768 push_new<SkPictureRecorder>(L);
1769 return 1;
1770}
1771
reed@google.com3597b732013-05-22 20:12:50 +00001772static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001773 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001774 return 1;
1775}
1776
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001777static int lsk_newTypeface(lua_State* L) {
1778 const char* name = NULL;
1779 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001780
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001781 int count = lua_gettop(L);
1782 if (count > 0 && lua_isstring(L, 1)) {
1783 name = lua_tolstring(L, 1, NULL);
1784 if (count > 1 && lua_isnumber(L, 2)) {
1785 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1786 }
1787 }
1788
1789 SkTypeface* face = SkTypeface::CreateFromName(name,
1790 (SkTypeface::Style)style);
1791// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1792 if (NULL == face) {
1793 face = SkTypeface::RefDefault();
1794 }
1795 push_ref(L, face);
1796 face->unref();
1797 return 1;
1798}
reed@google.com3597b732013-05-22 20:12:50 +00001799
reed485557f2014-10-12 10:36:47 -07001800static int lsk_newRasterSurface(lua_State* L) {
1801 int width = lua2int_def(L, 2, 0);
1802 int height = lua2int_def(L, 2, 0);
1803 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1804 SkSurface* surface = SkSurface::NewRaster(info);
1805 if (NULL == surface) {
1806 lua_pushnil(L);
1807 } else {
1808 push_ref(L, surface);
1809 surface->unref();
1810 }
1811 return 1;
1812}
1813
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001814static int lsk_loadImage(lua_State* L) {
1815 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1816 const char* name = lua_tolstring(L, 1, NULL);
1817 SkAutoDataUnref data(SkData::NewFromFileName(name));
1818 if (data.get()) {
piotaixr4bcc2022014-09-17 14:33:30 -07001819 SkImage* image = SkImage::NewFromGenerator(
1820 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
1821
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001822 if (image) {
1823 push_ref(L, image);
1824 image->unref();
1825 return 1;
1826 }
1827 }
1828 }
1829 return 0;
1830}
1831
reed@google.com3597b732013-05-22 20:12:50 +00001832static void register_Sk(lua_State* L) {
1833 lua_newtable(L);
1834 lua_pushvalue(L, -1);
1835 lua_setglobal(L, "Sk");
1836 // the Sk table is still on top
1837
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001838 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001839 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07001840 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reedbdc49ae2014-10-14 09:34:52 -07001841 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00001842 setfield_function(L, "newPaint", lsk_newPaint);
1843 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07001844 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00001845 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07001846 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001847 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001848 lua_pop(L, 1); // pop off the Sk table
1849}
1850
reed@google.com74ce6f02013-05-22 15:13:18 +00001851#define REG_CLASS(L, C) \
1852 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001853 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001854 lua_pushvalue(L, -1); \
1855 lua_setfield(L, -2, "__index"); \
1856 luaL_setfuncs(L, g##C##_Methods, 0); \
1857 lua_pop(L, 1); /* pop off the meta-table */ \
1858 } while (0)
1859
1860void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001861 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001862 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001863 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001864 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07001865 REG_CLASS(L, SkImageFilter);
reed@google.com74ce6f02013-05-22 15:13:18 +00001866 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001867 REG_CLASS(L, SkPath);
1868 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07001869 REG_CLASS(L, SkPicture);
1870 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00001871 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001872 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07001873 REG_CLASS(L, SkSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001874 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001875 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001876}
zachr@google.com28c27c82013-06-20 17:15:05 +00001877
reed@google.com7bce9982013-06-20 17:40:21 +00001878extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001879extern "C" int luaopen_skia(lua_State* L) {
1880 SkLua::Load(L);
1881 return 0;
1882}