blob: 57df014baaec1d357539c87bbeee3e97f6401111 [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
reed@google.come3823fd2013-05-30 18:55:14 +0000166static void setarray_number(lua_State* L, int index, double value) {
167 lua_pushnumber(L, value);
168 lua_rawseti(L, -2, index);
169}
170
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000171static void setarray_scalar(lua_State* L, int index, SkScalar value) {
172 setarray_number(L, index, SkScalarToLua(value));
173}
174
reed@google.com74ce6f02013-05-22 15:13:18 +0000175void SkLua::pushBool(bool value, const char key[]) {
176 lua_pushboolean(fL, value);
177 CHECK_SETFIELD(key);
178}
179
180void SkLua::pushString(const char str[], const char key[]) {
181 lua_pushstring(fL, str);
182 CHECK_SETFIELD(key);
183}
184
reed@google.come3823fd2013-05-30 18:55:14 +0000185void SkLua::pushString(const char str[], size_t length, const char key[]) {
186 // TODO: how to do this w/o making a copy?
187 SkString s(str, length);
188 lua_pushstring(fL, s.c_str());
189 CHECK_SETFIELD(key);
190}
191
reed@google.com74ce6f02013-05-22 15:13:18 +0000192void SkLua::pushString(const SkString& str, const char key[]) {
193 lua_pushstring(fL, str.c_str());
194 CHECK_SETFIELD(key);
195}
196
197void SkLua::pushColor(SkColor color, const char key[]) {
198 lua_newtable(fL);
199 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
200 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
201 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
202 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
203 CHECK_SETFIELD(key);
204}
205
reed@google.come3823fd2013-05-30 18:55:14 +0000206void SkLua::pushU32(uint32_t value, const char key[]) {
207 lua_pushnumber(fL, (double)value);
208 CHECK_SETFIELD(key);
209}
210
reed@google.com74ce6f02013-05-22 15:13:18 +0000211void SkLua::pushScalar(SkScalar value, const char key[]) {
212 lua_pushnumber(fL, SkScalarToLua(value));
213 CHECK_SETFIELD(key);
214}
215
reed@google.come3823fd2013-05-30 18:55:14 +0000216void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
217 lua_newtable(fL);
218 for (int i = 0; i < count; ++i) {
219 // make it base-1 to match lua convention
220 setarray_number(fL, i + 1, (double)array[i]);
221 }
222 CHECK_SETFIELD(key);
223}
224
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000225void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
226 lua_newtable(fL);
227 for (int i = 0; i < count; ++i) {
228 // make it base-1 to match lua convention
229 lua_newtable(fL);
230 this->pushScalar(array[i].fX, "x");
231 this->pushScalar(array[i].fY, "y");
232 lua_rawseti(fL, -2, i + 1);
233 }
234 CHECK_SETFIELD(key);
235}
236
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000237void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
238 lua_newtable(fL);
239 for (int i = 0; i < count; ++i) {
240 // make it base-1 to match lua convention
241 setarray_scalar(fL, i + 1, array[i]);
242 }
243 CHECK_SETFIELD(key);
244}
245
reed@google.com74ce6f02013-05-22 15:13:18 +0000246void SkLua::pushRect(const SkRect& r, const char key[]) {
247 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000248 setfield_scalar(fL, "left", r.fLeft);
249 setfield_scalar(fL, "top", r.fTop);
250 setfield_scalar(fL, "right", r.fRight);
251 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000252 CHECK_SETFIELD(key);
253}
254
255void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
256 push_obj(fL, rr);
257 CHECK_SETFIELD(key);
258}
259
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000260void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
261 lua_newtable(fL);
262 setfield_scalar(fL, "phase", info.fPhase);
263 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
264 CHECK_SETFIELD(key);
265}
266
267
reed@google.com74ce6f02013-05-22 15:13:18 +0000268void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
269 push_obj(fL, matrix);
270 CHECK_SETFIELD(key);
271}
272
273void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
274 push_obj(fL, paint);
275 CHECK_SETFIELD(key);
276}
277
278void SkLua::pushPath(const SkPath& path, const char key[]) {
279 push_obj(fL, path);
280 CHECK_SETFIELD(key);
281}
282
283void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
284 push_ref(fL, canvas);
285 CHECK_SETFIELD(key);
286}
287
fmalitab7425172014-08-26 07:56:44 -0700288void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
289 push_ref(fL, const_cast<SkTextBlob*>(blob));
290 CHECK_SETFIELD(key);
291}
292
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000293static const char* element_type(SkClipStack::Element::Type type) {
294 switch (type) {
295 case SkClipStack::Element::kEmpty_Type:
296 return "empty";
297 case SkClipStack::Element::kRect_Type:
298 return "rect";
299 case SkClipStack::Element::kRRect_Type:
300 return "rrect";
301 case SkClipStack::Element::kPath_Type:
302 return "path";
303 }
304 return "unknown";
305}
306
307static const char* region_op(SkRegion::Op op) {
308 switch (op) {
309 case SkRegion::kDifference_Op:
310 return "difference";
311 case SkRegion::kIntersect_Op:
312 return "intersect";
313 case SkRegion::kUnion_Op:
314 return "union";
315 case SkRegion::kXOR_Op:
316 return "xor";
317 case SkRegion::kReverseDifference_Op:
318 return "reverse-difference";
319 case SkRegion::kReplace_Op:
320 return "replace";
321 }
322 return "unknown";
323}
324
325void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
326 lua_newtable(fL);
327 SkClipStack::B2TIter iter(stack);
328 const SkClipStack::Element* element;
329 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700330 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000331 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000332 lua_rawseti(fL, -2, ++i);
333 }
334 CHECK_SETFIELD(key);
335}
336
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000337void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
338 lua_newtable(fL);
339 SkClipStack::Element::Type type = element.getType();
340 this->pushString(element_type(type), "type");
341 switch (type) {
342 case SkClipStack::Element::kEmpty_Type:
343 break;
344 case SkClipStack::Element::kRect_Type:
345 this->pushRect(element.getRect(), "rect");
346 break;
347 case SkClipStack::Element::kRRect_Type:
348 this->pushRRect(element.getRRect(), "rrect");
349 break;
350 case SkClipStack::Element::kPath_Type:
351 this->pushPath(element.getPath(), "path");
352 break;
353 }
354 this->pushString(region_op(element.getOp()), "op");
355 this->pushBool(element.isAA(), "aa");
356 CHECK_SETFIELD(key);
357}
358
359
reed@google.com74ce6f02013-05-22 15:13:18 +0000360///////////////////////////////////////////////////////////////////////////////
361///////////////////////////////////////////////////////////////////////////////
362
reed485557f2014-10-12 10:36:47 -0700363static int lua2int_def(lua_State* L, int index, int defaultValue) {
364 if (lua_isnumber(L, index)) {
365 return (int)lua_tonumber(L, index);
366 } else {
367 return defaultValue;
368 }
369}
370
reed@google.com74ce6f02013-05-22 15:13:18 +0000371static SkScalar lua2scalar(lua_State* L, int index) {
372 SkASSERT(lua_isnumber(L, index));
373 return SkLuaToScalar(lua_tonumber(L, index));
374}
375
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000376static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
377 if (lua_isnumber(L, index)) {
378 return SkLuaToScalar(lua_tonumber(L, index));
379 } else {
380 return defaultValue;
381 }
382}
383
reed@google.com74ce6f02013-05-22 15:13:18 +0000384static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
385 SkASSERT(lua_istable(L, index));
386 lua_pushstring(L, key);
387 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000388
reed@google.com74ce6f02013-05-22 15:13:18 +0000389 SkScalar value = lua2scalar(L, -1);
390 lua_pop(L, 1);
391 return value;
392}
393
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000394static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
395 SkASSERT(lua_istable(L, index));
396 lua_pushstring(L, key);
397 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000398
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000399 SkScalar value;
400 if (lua_isnil(L, -1)) {
401 value = def;
402 } else {
403 value = lua2scalar(L, -1);
404 }
405 lua_pop(L, 1);
406 return value;
407}
408
reed468b1812014-10-19 11:42:54 -0700409static SkScalar byte2unit(U8CPU byte) {
410 return byte / 255.0f;
411}
412
reed@google.com74ce6f02013-05-22 15:13:18 +0000413static U8CPU unit2byte(SkScalar x) {
414 if (x <= 0) {
415 return 0;
416 } else if (x >= 1) {
417 return 255;
418 } else {
419 return SkScalarRoundToInt(x * 255);
420 }
421}
422
423static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700424 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
425 unit2byte(getfield_scalar_default(L, index, "r", 0)),
426 unit2byte(getfield_scalar_default(L, index, "g", 0)),
427 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000428}
429
430static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000431 rect->set(getfield_scalar_default(L, index, "left", 0),
432 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000433 getfield_scalar(L, index, "right"),
434 getfield_scalar(L, index, "bottom"));
435 return rect;
436}
437
reedf355df52014-10-12 12:18:40 -0700438static int lcanvas_clear(lua_State* L) {
439 get_ref<SkCanvas>(L, 1)->clear(0);
440 return 0;
441}
442
reed@google.com74ce6f02013-05-22 15:13:18 +0000443static int lcanvas_drawColor(lua_State* L) {
444 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
445 return 0;
446}
447
reed9fbc3f32014-10-21 07:12:58 -0700448static int lcanvas_drawPaint(lua_State* L) {
449 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
450 return 0;
451}
452
reed@google.com74ce6f02013-05-22 15:13:18 +0000453static int lcanvas_drawRect(lua_State* L) {
454 SkRect rect;
455 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
456 *get_obj<SkPaint>(L, 3));
457 return 0;
458}
459
460static int lcanvas_drawOval(lua_State* L) {
461 SkRect rect;
462 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
463 *get_obj<SkPaint>(L, 3));
464 return 0;
465}
466
467static int lcanvas_drawCircle(lua_State* L) {
468 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
469 lua2scalar(L, 3),
470 lua2scalar(L, 4),
471 *get_obj<SkPaint>(L, 5));
472 return 0;
473}
474
reed485557f2014-10-12 10:36:47 -0700475static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
476 if (lua_isnumber(L, index)) {
477 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
478 return paint;
reedf355df52014-10-12 12:18:40 -0700479 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700480 const SkPaint* ptr = get_obj<SkPaint>(L, index);
481 if (ptr) {
482 *paint = *ptr;
483 return paint;
484 }
485 }
486 return NULL;
487}
488
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000489static int lcanvas_drawImage(lua_State* L) {
490 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
491 SkImage* image = get_ref<SkImage>(L, 2);
492 if (NULL == image) {
493 return 0;
494 }
495 SkScalar x = lua2scalar(L, 3);
496 SkScalar y = lua2scalar(L, 4);
497
498 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700499 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000500 return 0;
501}
502
reedba5fb932014-10-10 15:28:19 -0700503static int lcanvas_drawImageRect(lua_State* L) {
504 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
505 SkImage* image = get_ref<SkImage>(L, 2);
506 if (NULL == image) {
507 return 0;
508 }
509
510 SkRect srcR, dstR;
511 SkRect* srcRPtr = NULL;
512 if (!lua_isnil(L, 3)) {
513 srcRPtr = lua2rect(L, 3, &srcR);
514 }
515 lua2rect(L, 4, &dstR);
516
517 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700518 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700519 return 0;
520}
521
reed@google.comfd345872013-05-22 20:53:42 +0000522static int lcanvas_drawPath(lua_State* L) {
523 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
524 *get_obj<SkPaint>(L, 3));
525 return 0;
526}
527
reed96affcd2014-10-13 12:38:04 -0700528// drawPicture(pic, x, y, paint)
529static int lcanvas_drawPicture(lua_State* L) {
530 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
531 SkPicture* picture = get_ref<SkPicture>(L, 2);
532 SkScalar x = lua2scalar_def(L, 3, 0);
533 SkScalar y = lua2scalar_def(L, 4, 0);
534 SkMatrix matrix, *matrixPtr = NULL;
535 if (x || y) {
536 matrix.setTranslate(x, y);
537 matrixPtr = &matrix;
538 }
539 SkPaint paint;
540 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
541 return 0;
542}
543
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000544static int lcanvas_drawText(lua_State* L) {
545 if (lua_gettop(L) < 5) {
546 return 0;
547 }
548
549 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
550 size_t len;
551 const char* text = lua_tolstring(L, 2, &len);
552 get_ref<SkCanvas>(L, 1)->drawText(text, len,
553 lua2scalar(L, 3), lua2scalar(L, 4),
554 *get_obj<SkPaint>(L, 5));
555 }
556 return 0;
557}
558
reed@google.com74ce6f02013-05-22 15:13:18 +0000559static int lcanvas_getSaveCount(lua_State* L) {
560 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
561 return 1;
562}
563
564static int lcanvas_getTotalMatrix(lua_State* L) {
565 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
566 return 1;
567}
568
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000569static int lcanvas_getClipStack(lua_State* L) {
570 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
571 return 1;
572}
573
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000574int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
575#if SK_SUPPORT_GPU
576 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
577 SkISize layerSize = canvas->getTopLayerSize();
578 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
579 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
580 layerSize.fWidth, layerSize.fHeight);
581
582 GrReducedClip::ElementList elements;
583 GrReducedClip::InitialState initialState;
584 int32_t genID;
585 SkIRect resultBounds;
586
587 const SkClipStack& stack = *canvas->getClipStack();
588
589 GrReducedClip::ReduceClipStack(stack,
590 queryBounds,
591 &elements,
592 &genID,
593 &initialState,
594 &resultBounds,
595 NULL);
596
597 GrReducedClip::ElementList::Iter iter(elements);
598 int i = 0;
599 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700600 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000601 SkLua(L).pushClipStackElement(*iter.get());
602 iter.next();
603 lua_rawseti(L, -2, ++i);
604 }
605 // Currently this only returns the element list to lua, not the initial state or result bounds.
606 // It could return these as additional items on the lua stack.
607 return 1;
608#else
609 return 0;
610#endif
611}
612
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000613static int lcanvas_save(lua_State* L) {
614 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
615 return 1;
616}
617
618static int lcanvas_restore(lua_State* L) {
619 get_ref<SkCanvas>(L, 1)->restore();
620 return 0;
621}
622
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000623static int lcanvas_scale(lua_State* L) {
624 SkScalar sx = lua2scalar_def(L, 2, 1);
625 SkScalar sy = lua2scalar_def(L, 3, sx);
626 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
627 return 0;
628}
629
reed@google.com3597b732013-05-22 20:12:50 +0000630static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000631 SkScalar tx = lua2scalar_def(L, 2, 0);
632 SkScalar ty = lua2scalar_def(L, 3, 0);
633 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
634 return 0;
635}
636
637static int lcanvas_rotate(lua_State* L) {
638 SkScalar degrees = lua2scalar_def(L, 2, 0);
639 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000640 return 0;
641}
642
reedbdc49ae2014-10-14 09:34:52 -0700643static int lcanvas_concat(lua_State* L) {
644 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
645 return 0;
646}
647
reed485557f2014-10-12 10:36:47 -0700648static int lcanvas_newSurface(lua_State* L) {
649 int width = lua2int_def(L, 2, 0);
650 int height = lua2int_def(L, 2, 0);
651 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
652 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
653 if (NULL == surface) {
654 lua_pushnil(L);
655 } else {
reed9fbc3f32014-10-21 07:12:58 -0700656 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -0700657 }
658 return 1;
659}
660
reed@google.com74ce6f02013-05-22 15:13:18 +0000661static int lcanvas_gc(lua_State* L) {
662 get_ref<SkCanvas>(L, 1)->unref();
663 return 0;
664}
665
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000666const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700667 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000668 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700669 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000670 { "drawRect", lcanvas_drawRect },
671 { "drawOval", lcanvas_drawOval },
672 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000673 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700674 { "drawImageRect", lcanvas_drawImageRect },
reed@google.comfd345872013-05-22 20:53:42 +0000675 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700676 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000677 { "drawText", lcanvas_drawText },
reed@google.com74ce6f02013-05-22 15:13:18 +0000678 { "getSaveCount", lcanvas_getSaveCount },
679 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000680 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000681#if SK_SUPPORT_GPU
682 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
683#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000684 { "save", lcanvas_save },
685 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000686 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000687 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000688 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700689 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700690
691 { "newSurface", lcanvas_newSurface },
692
reed@google.com74ce6f02013-05-22 15:13:18 +0000693 { "__gc", lcanvas_gc },
694 { NULL, NULL }
695};
696
697///////////////////////////////////////////////////////////////////////////////
698
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000699static int ldocument_beginPage(lua_State* L) {
700 const SkRect* contentPtr = NULL;
701 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
702 lua2scalar(L, 3),
703 contentPtr));
704 return 1;
705}
706
707static int ldocument_endPage(lua_State* L) {
708 get_ref<SkDocument>(L, 1)->endPage();
709 return 0;
710}
711
712static int ldocument_close(lua_State* L) {
713 get_ref<SkDocument>(L, 1)->close();
714 return 0;
715}
716
717static int ldocument_gc(lua_State* L) {
718 get_ref<SkDocument>(L, 1)->unref();
719 return 0;
720}
721
722static const struct luaL_Reg gSkDocument_Methods[] = {
723 { "beginPage", ldocument_beginPage },
724 { "endPage", ldocument_endPage },
725 { "close", ldocument_close },
726 { "__gc", ldocument_gc },
727 { NULL, NULL }
728};
729
730///////////////////////////////////////////////////////////////////////////////
731
reed@google.com74ce6f02013-05-22 15:13:18 +0000732static int lpaint_isAntiAlias(lua_State* L) {
733 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
734 return 1;
735}
736
737static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000738 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000739 return 0;
740}
741
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000742static int lpaint_isDither(lua_State* L) {
743 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
744 return 1;
745}
746
747static int lpaint_isUnderlineText(lua_State* L) {
748 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
749 return 1;
750}
751
752static int lpaint_isStrikeThruText(lua_State* L) {
753 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
754 return 1;
755}
756
757static int lpaint_isFakeBoldText(lua_State* L) {
758 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
759 return 1;
760}
761
762static int lpaint_isLinearText(lua_State* L) {
763 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
764 return 1;
765}
766
767static int lpaint_isSubpixelText(lua_State* L) {
768 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
769 return 1;
770}
771
reed09a1d672014-10-11 13:13:11 -0700772static int lpaint_setSubpixelText(lua_State* L) {
773 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
774 return 1;
775}
776
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000777static int lpaint_isDevKernText(lua_State* L) {
778 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
779 return 1;
780}
781
782static int lpaint_isLCDRenderText(lua_State* L) {
783 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
784 return 1;
785}
786
787static int lpaint_isEmbeddedBitmapText(lua_State* L) {
788 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
789 return 1;
790}
791
792static int lpaint_isAutohinted(lua_State* L) {
793 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
794 return 1;
795}
796
797static int lpaint_isVerticalText(lua_State* L) {
798 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
799 return 1;
800}
801
reed468b1812014-10-19 11:42:54 -0700802static int lpaint_getAlpha(lua_State* L) {
803 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
804 return 1;
805}
806
807static int lpaint_setAlpha(lua_State* L) {
808 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
809 return 0;
810}
811
reed@google.com74ce6f02013-05-22 15:13:18 +0000812static int lpaint_getColor(lua_State* L) {
813 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
814 return 1;
815}
816
817static int lpaint_setColor(lua_State* L) {
818 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
819 return 0;
820}
821
reed@google.come3823fd2013-05-30 18:55:14 +0000822static int lpaint_getTextSize(lua_State* L) {
823 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
824 return 1;
825}
826
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000827static int lpaint_getTextScaleX(lua_State* L) {
828 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
829 return 1;
830}
831
832static int lpaint_getTextSkewX(lua_State* L) {
833 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
834 return 1;
835}
836
reed@google.come3823fd2013-05-30 18:55:14 +0000837static int lpaint_setTextSize(lua_State* L) {
838 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
839 return 0;
840}
841
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000842static int lpaint_getTypeface(lua_State* L) {
843 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
844 return 1;
845}
846
847static int lpaint_setTypeface(lua_State* L) {
848 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
849 return 0;
850}
851
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000852static int lpaint_getHinting(lua_State* L) {
853 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
854 return 1;
855}
856
reed@google.come3823fd2013-05-30 18:55:14 +0000857static int lpaint_getFontID(lua_State* L) {
858 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
859 SkLua(L).pushU32(SkTypeface::UniqueID(face));
860 return 1;
861}
862
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000863static const struct {
864 const char* fLabel;
865 SkPaint::Align fAlign;
866} gAlignRec[] = {
867 { "left", SkPaint::kLeft_Align },
868 { "center", SkPaint::kCenter_Align },
869 { "right", SkPaint::kRight_Align },
870};
871
872static int lpaint_getTextAlign(lua_State* L) {
873 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
874 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
875 if (gAlignRec[i].fAlign == align) {
876 lua_pushstring(L, gAlignRec[i].fLabel);
877 return 1;
878 }
879 }
880 return 0;
881}
882
883static int lpaint_setTextAlign(lua_State* L) {
884 if (lua_isstring(L, 2)) {
885 size_t len;
886 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000887
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000888 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
889 if (!strcmp(gAlignRec[i].fLabel, label)) {
890 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
891 break;
892 }
893 }
894 }
895 return 0;
896}
897
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000898static int lpaint_getStroke(lua_State* L) {
899 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
900 return 1;
901}
902
903static int lpaint_setStroke(lua_State* L) {
904 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000905
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000906 if (lua_toboolean(L, 2)) {
907 style = SkPaint::kStroke_Style;
908 } else {
909 style = SkPaint::kFill_Style;
910 }
911 get_obj<SkPaint>(L, 1)->setStyle(style);
912 return 0;
913}
914
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000915static int lpaint_getStrokeCap(lua_State* L) {
916 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
917 return 1;
918}
919
920static int lpaint_getStrokeJoin(lua_State* L) {
921 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
922 return 1;
923}
924
925static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000926 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000927 return 1;
928}
929
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000930static int lpaint_getStrokeWidth(lua_State* L) {
931 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
932 return 1;
933}
934
935static int lpaint_setStrokeWidth(lua_State* L) {
936 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
937 return 0;
938}
939
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000940static int lpaint_getStrokeMiter(lua_State* L) {
941 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
942 return 1;
943}
944
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000945static int lpaint_measureText(lua_State* L) {
946 if (lua_isstring(L, 2)) {
947 size_t len;
948 const char* text = lua_tolstring(L, 2, &len);
949 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
950 return 1;
951 }
952 return 0;
953}
954
955struct FontMetrics {
956 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
957 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
958 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
959 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
960 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
961 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
962 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
963 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
964 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
965};
966
967static int lpaint_getFontMetrics(lua_State* L) {
968 SkPaint::FontMetrics fm;
969 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000970
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000971 lua_newtable(L);
972 setfield_scalar(L, "top", fm.fTop);
973 setfield_scalar(L, "ascent", fm.fAscent);
974 setfield_scalar(L, "descent", fm.fDescent);
975 setfield_scalar(L, "bottom", fm.fBottom);
976 setfield_scalar(L, "leading", fm.fLeading);
977 SkLua(L).pushScalar(height);
978 return 2;
979}
980
reed@google.com29563872013-07-10 21:23:49 +0000981static int lpaint_getEffects(lua_State* L) {
982 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000983
reed@google.com29563872013-07-10 21:23:49 +0000984 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -0700985 setfield_bool_if(L, "looper", !!paint->getLooper());
986 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
987 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
988 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
989 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +0000990 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
991 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -0700992 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +0000993 return 1;
994}
995
reed468b1812014-10-19 11:42:54 -0700996static int lpaint_getImageFilter(lua_State* L) {
997 const SkPaint* paint = get_obj<SkPaint>(L, 1);
998 SkImageFilter* imf = paint->getImageFilter();
999 if (imf) {
1000 push_ref(L, imf);
1001 return 1;
1002 }
1003 return 0;
1004}
1005
1006static int lpaint_setImageFilter(lua_State* L) {
1007 SkPaint* paint = get_obj<SkPaint>(L, 1);
1008 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1009 return 0;
1010}
1011
reed@google.com5fdc9832013-07-24 15:47:52 +00001012static int lpaint_getShader(lua_State* L) {
1013 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1014 SkShader* shader = paint->getShader();
1015 if (shader) {
1016 push_ref(L, shader);
1017 return 1;
1018 }
1019 return 0;
1020}
1021
reed9fbc3f32014-10-21 07:12:58 -07001022static int lpaint_setShader(lua_State* L) {
1023 SkPaint* paint = get_obj<SkPaint>(L, 1);
1024 paint->setShader(get_ref<SkShader>(L, 2));
1025 return 0;
1026}
1027
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001028static int lpaint_getPathEffect(lua_State* L) {
1029 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1030 SkPathEffect* pe = paint->getPathEffect();
1031 if (pe) {
1032 push_ref(L, pe);
1033 return 1;
1034 }
1035 return 0;
1036}
1037
reed@google.com74ce6f02013-05-22 15:13:18 +00001038static int lpaint_gc(lua_State* L) {
1039 get_obj<SkPaint>(L, 1)->~SkPaint();
1040 return 0;
1041}
1042
1043static const struct luaL_Reg gSkPaint_Methods[] = {
1044 { "isAntiAlias", lpaint_isAntiAlias },
1045 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001046 { "isDither", lpaint_isDither },
1047 { "isUnderlineText", lpaint_isUnderlineText },
1048 { "isStrikeThruText", lpaint_isStrikeThruText },
1049 { "isFakeBoldText", lpaint_isFakeBoldText },
1050 { "isLinearText", lpaint_isLinearText },
1051 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001052 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001053 { "isDevKernText", lpaint_isDevKernText },
1054 { "isLCDRenderText", lpaint_isLCDRenderText },
1055 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1056 { "isAutohinted", lpaint_isAutohinted },
1057 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001058 { "getAlpha", lpaint_getAlpha },
1059 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001060 { "getColor", lpaint_getColor },
1061 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001062 { "getTextSize", lpaint_getTextSize },
1063 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001064 { "getTextScaleX", lpaint_getTextScaleX },
1065 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001066 { "getTypeface", lpaint_getTypeface },
1067 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001068 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001069 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001070 { "getTextAlign", lpaint_getTextAlign },
1071 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001072 { "getStroke", lpaint_getStroke },
1073 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001074 { "getStrokeCap", lpaint_getStrokeCap },
1075 { "getStrokeJoin", lpaint_getStrokeJoin },
1076 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001077 { "getStrokeWidth", lpaint_getStrokeWidth },
1078 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001079 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001080 { "measureText", lpaint_measureText },
1081 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001082 { "getEffects", lpaint_getEffects },
reed468b1812014-10-19 11:42:54 -07001083 { "getImageFilter", lpaint_getImageFilter },
1084 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001085 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001086 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001087 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001088 { "__gc", lpaint_gc },
1089 { NULL, NULL }
1090};
1091
1092///////////////////////////////////////////////////////////////////////////////
1093
reed@google.com5fdc9832013-07-24 15:47:52 +00001094static const char* mode2string(SkShader::TileMode mode) {
1095 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1096 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1097 return gNames[mode];
1098}
1099
1100static const char* gradtype2string(SkShader::GradientType t) {
1101 static const char* gNames[] = {
1102 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1103 };
1104 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1105 return gNames[t];
1106}
1107
1108static int lshader_isOpaque(lua_State* L) {
1109 SkShader* shader = get_ref<SkShader>(L, 1);
1110 return shader && shader->isOpaque();
1111}
1112
1113static int lshader_asABitmap(lua_State* L) {
1114 SkShader* shader = get_ref<SkShader>(L, 1);
1115 if (shader) {
1116 SkBitmap bm;
1117 SkMatrix matrix;
1118 SkShader::TileMode modes[2];
1119 switch (shader->asABitmap(&bm, &matrix, modes)) {
1120 case SkShader::kDefault_BitmapType:
1121 lua_newtable(L);
1122 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1123 setfield_number(L, "width", bm.width());
1124 setfield_number(L, "height", bm.height());
1125 setfield_string(L, "tileX", mode2string(modes[0]));
1126 setfield_string(L, "tileY", mode2string(modes[1]));
1127 return 1;
1128 default:
1129 break;
1130 }
1131 }
1132 return 0;
1133}
1134
1135static int lshader_asAGradient(lua_State* L) {
1136 SkShader* shader = get_ref<SkShader>(L, 1);
1137 if (shader) {
1138 SkShader::GradientInfo info;
1139 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001140
1141 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001142 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001143
1144 info.fColorCount = 3;
1145 info.fColors = &colors[0];
1146 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001147
reed@google.com5fdc9832013-07-24 15:47:52 +00001148 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001149
reed@google.com5fdc9832013-07-24 15:47:52 +00001150 if (SkShader::kNone_GradientType != t) {
1151 lua_newtable(L);
1152 setfield_string(L, "type", gradtype2string(t));
1153 setfield_number(L, "colorCount", info.fColorCount);
1154 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001155
1156 if (info.fColorCount == 3){
1157 setfield_number(L, "midPos", pos[1]);
1158 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001159
reed@google.com5fdc9832013-07-24 15:47:52 +00001160 return 1;
1161 }
1162 }
1163 return 0;
1164}
1165
1166static int lshader_gc(lua_State* L) {
1167 get_ref<SkShader>(L, 1)->unref();
1168 return 0;
1169}
1170
1171static const struct luaL_Reg gSkShader_Methods[] = {
1172 { "isOpaque", lshader_isOpaque },
1173 { "asABitmap", lshader_asABitmap },
1174 { "asAGradient", lshader_asAGradient },
1175 { "__gc", lshader_gc },
1176 { NULL, NULL }
1177};
1178
1179///////////////////////////////////////////////////////////////////////////////
1180
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001181static int lpatheffect_asADash(lua_State* L) {
1182 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1183 if (pe) {
1184 SkPathEffect::DashInfo info;
1185 SkPathEffect::DashType dashType = pe->asADash(&info);
1186 if (SkPathEffect::kDash_DashType == dashType) {
1187 SkAutoTArray<SkScalar> intervals(info.fCount);
1188 info.fIntervals = intervals.get();
1189 pe->asADash(&info);
1190 SkLua(L).pushDash(info);
1191 return 1;
1192 }
1193 }
1194 return 0;
1195}
1196
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001197static int lpatheffect_gc(lua_State* L) {
1198 get_ref<SkPathEffect>(L, 1)->unref();
1199 return 0;
1200}
1201
1202static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001203 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001204 { "__gc", lpatheffect_gc },
1205 { NULL, NULL }
1206};
1207
1208///////////////////////////////////////////////////////////////////////////////
1209
reed468b1812014-10-19 11:42:54 -07001210static int lpimagefilter_gc(lua_State* L) {
1211 get_ref<SkImageFilter>(L, 1)->unref();
1212 return 0;
1213}
1214
1215static const struct luaL_Reg gSkImageFilter_Methods[] = {
1216 { "__gc", lpimagefilter_gc },
1217 { NULL, NULL }
1218};
1219
1220///////////////////////////////////////////////////////////////////////////////
1221
humper@google.com2815c192013-07-10 22:42:30 +00001222static int lmatrix_getType(lua_State* L) {
1223 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001224
humper@google.com2815c192013-07-10 22:42:30 +00001225 lua_newtable(L);
1226 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1227 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1228 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1229 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1230 return 1;
1231}
1232
humper@google.com0f48ee02013-07-26 15:23:43 +00001233static int lmatrix_getScaleX(lua_State* L) {
1234 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1235 return 1;
1236}
1237
1238static int lmatrix_getScaleY(lua_State* L) {
1239 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1240 return 1;
1241}
1242
1243static int lmatrix_getTranslateX(lua_State* L) {
1244 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1245 return 1;
1246}
1247
1248static int lmatrix_getTranslateY(lua_State* L) {
1249 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1250 return 1;
1251}
1252
reedbdc49ae2014-10-14 09:34:52 -07001253static int lmatrix_setRectToRect(lua_State* L) {
1254 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1255 SkRect srcR, dstR;
1256 lua2rect(L, 2, &srcR);
1257 lua2rect(L, 3, &dstR);
1258 const char* scaleToFitStr = lua_tostring(L, 4);
1259 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1260
1261 if (scaleToFitStr) {
1262 const struct {
1263 const char* fName;
1264 SkMatrix::ScaleToFit fScaleToFit;
1265 } rec[] = {
1266 { "fill", SkMatrix::kFill_ScaleToFit },
1267 { "start", SkMatrix::kStart_ScaleToFit },
1268 { "center", SkMatrix::kCenter_ScaleToFit },
1269 { "end", SkMatrix::kEnd_ScaleToFit },
1270 };
1271
1272 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1273 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1274 scaleToFit = rec[i].fScaleToFit;
1275 break;
1276 }
1277 }
1278 }
1279
1280 matrix->setRectToRect(srcR, dstR, scaleToFit);
1281 return 0;
1282}
1283
humper@google.com2815c192013-07-10 22:42:30 +00001284static const struct luaL_Reg gSkMatrix_Methods[] = {
1285 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001286 { "getScaleX", lmatrix_getScaleX },
1287 { "getScaleY", lmatrix_getScaleY },
1288 { "getTranslateX", lmatrix_getTranslateX },
1289 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001290 { "setRectToRect", lmatrix_setRectToRect },
humper@google.com2815c192013-07-10 22:42:30 +00001291 { NULL, NULL }
1292};
1293
1294///////////////////////////////////////////////////////////////////////////////
1295
reed@google.com74ce6f02013-05-22 15:13:18 +00001296static int lpath_getBounds(lua_State* L) {
1297 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1298 return 1;
1299}
1300
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001301static const char* fill_type_to_str(SkPath::FillType fill) {
1302 switch (fill) {
1303 case SkPath::kEvenOdd_FillType:
1304 return "even-odd";
1305 case SkPath::kWinding_FillType:
1306 return "winding";
1307 case SkPath::kInverseEvenOdd_FillType:
1308 return "inverse-even-odd";
1309 case SkPath::kInverseWinding_FillType:
1310 return "inverse-winding";
1311 }
1312 return "unknown";
1313}
1314
1315static int lpath_getFillType(lua_State* L) {
1316 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1317 SkLua(L).pushString(fill_type_to_str(fill));
1318 return 1;
1319}
1320
1321static SkString segment_masks_to_str(uint32_t segmentMasks) {
1322 SkString result;
1323 bool first = true;
1324 if (SkPath::kLine_SegmentMask & segmentMasks) {
1325 result.append("line");
1326 first = false;
1327 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1328 }
1329 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1330 if (!first) {
1331 result.append(" ");
1332 }
1333 result.append("quad");
1334 first = false;
1335 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1336 }
1337 if (SkPath::kConic_SegmentMask & segmentMasks) {
1338 if (!first) {
1339 result.append(" ");
1340 }
1341 result.append("conic");
1342 first = false;
1343 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1344 }
1345 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1346 if (!first) {
1347 result.append(" ");
1348 }
1349 result.append("cubic");
1350 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1351 }
1352 SkASSERT(0 == segmentMasks);
1353 return result;
1354}
1355
krajcevski95498ed2014-08-18 08:02:33 -07001356static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001357 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1358 SkLua(L).pushString(segment_masks_to_str(segMasks));
1359 return 1;
1360}
1361
1362static int lpath_isConvex(lua_State* L) {
1363 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1364 SkLua(L).pushBool(isConvex);
1365 return 1;
1366}
1367
reed@google.com74ce6f02013-05-22 15:13:18 +00001368static int lpath_isEmpty(lua_State* L) {
1369 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1370 return 1;
1371}
1372
1373static int lpath_isRect(lua_State* L) {
1374 SkRect r;
1375 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1376 int ret_count = 1;
1377 lua_pushboolean(L, pred);
1378 if (pred) {
1379 SkLua(L).pushRect(r);
1380 ret_count += 1;
1381 }
1382 return ret_count;
1383}
1384
1385static const char* dir2string(SkPath::Direction dir) {
1386 static const char* gStr[] = {
1387 "unknown", "cw", "ccw"
1388 };
1389 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1390 return gStr[dir];
1391}
1392
1393static int lpath_isNestedRects(lua_State* L) {
1394 SkRect rects[2];
1395 SkPath::Direction dirs[2];
1396 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1397 int ret_count = 1;
1398 lua_pushboolean(L, pred);
1399 if (pred) {
1400 SkLua lua(L);
1401 lua.pushRect(rects[0]);
1402 lua.pushRect(rects[1]);
1403 lua_pushstring(L, dir2string(dirs[0]));
1404 lua_pushstring(L, dir2string(dirs[0]));
1405 ret_count += 4;
1406 }
1407 return ret_count;
1408}
1409
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001410static int lpath_countPoints(lua_State* L) {
1411 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1412 return 1;
1413}
1414
reed@google.com74ce6f02013-05-22 15:13:18 +00001415static int lpath_reset(lua_State* L) {
1416 get_obj<SkPath>(L, 1)->reset();
1417 return 0;
1418}
1419
1420static int lpath_moveTo(lua_State* L) {
1421 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1422 return 0;
1423}
1424
1425static int lpath_lineTo(lua_State* L) {
1426 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1427 return 0;
1428}
1429
1430static int lpath_quadTo(lua_State* L) {
1431 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1432 lua2scalar(L, 4), lua2scalar(L, 5));
1433 return 0;
1434}
1435
1436static int lpath_cubicTo(lua_State* L) {
1437 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1438 lua2scalar(L, 4), lua2scalar(L, 5),
1439 lua2scalar(L, 6), lua2scalar(L, 7));
1440 return 0;
1441}
1442
1443static int lpath_close(lua_State* L) {
1444 get_obj<SkPath>(L, 1)->close();
1445 return 0;
1446}
1447
1448static int lpath_gc(lua_State* L) {
1449 get_obj<SkPath>(L, 1)->~SkPath();
1450 return 0;
1451}
1452
1453static const struct luaL_Reg gSkPath_Methods[] = {
1454 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001455 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001456 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001457 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001458 { "isEmpty", lpath_isEmpty },
1459 { "isRect", lpath_isRect },
1460 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001461 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001462 { "reset", lpath_reset },
1463 { "moveTo", lpath_moveTo },
1464 { "lineTo", lpath_lineTo },
1465 { "quadTo", lpath_quadTo },
1466 { "cubicTo", lpath_cubicTo },
1467 { "close", lpath_close },
1468 { "__gc", lpath_gc },
1469 { NULL, NULL }
1470};
1471
1472///////////////////////////////////////////////////////////////////////////////
1473
1474static const char* rrect_type(const SkRRect& rr) {
1475 switch (rr.getType()) {
1476 case SkRRect::kUnknown_Type: return "unknown";
1477 case SkRRect::kEmpty_Type: return "empty";
1478 case SkRRect::kRect_Type: return "rect";
1479 case SkRRect::kOval_Type: return "oval";
1480 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001481 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001482 case SkRRect::kComplex_Type: return "complex";
1483 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001484 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001485 return "";
1486}
1487
1488static int lrrect_rect(lua_State* L) {
1489 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1490 return 1;
1491}
1492
1493static int lrrect_type(lua_State* L) {
1494 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1495 return 1;
1496}
1497
1498static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001499 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001500 SkVector v;
1501 if (corner < 0 || corner > 3) {
1502 SkDebugf("bad corner index %d", corner);
1503 v.set(0, 0);
1504 } else {
1505 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1506 }
1507 lua_pushnumber(L, v.fX);
1508 lua_pushnumber(L, v.fY);
1509 return 2;
1510}
1511
1512static int lrrect_gc(lua_State* L) {
1513 get_obj<SkRRect>(L, 1)->~SkRRect();
1514 return 0;
1515}
1516
1517static const struct luaL_Reg gSkRRect_Methods[] = {
1518 { "rect", lrrect_rect },
1519 { "type", lrrect_type },
1520 { "radii", lrrect_radii },
1521 { "__gc", lrrect_gc },
1522 { NULL, NULL }
1523};
1524
1525///////////////////////////////////////////////////////////////////////////////
1526
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001527static int limage_width(lua_State* L) {
1528 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1529 return 1;
1530}
1531
1532static int limage_height(lua_State* L) {
1533 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1534 return 1;
1535}
1536
1537static int limage_gc(lua_State* L) {
1538 get_ref<SkImage>(L, 1)->unref();
1539 return 0;
1540}
1541
1542static const struct luaL_Reg gSkImage_Methods[] = {
1543 { "width", limage_width },
1544 { "height", limage_height },
1545 { "__gc", limage_gc },
1546 { NULL, NULL }
1547};
1548
1549///////////////////////////////////////////////////////////////////////////////
1550
reed485557f2014-10-12 10:36:47 -07001551static int lsurface_width(lua_State* L) {
1552 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1553 return 1;
1554}
1555
1556static int lsurface_height(lua_State* L) {
1557 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1558 return 1;
1559}
1560
1561static int lsurface_getCanvas(lua_State* L) {
1562 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1563 if (NULL == canvas) {
1564 lua_pushnil(L);
1565 } else {
1566 push_ref(L, canvas);
1567 // note: we don't unref canvas, since getCanvas did not ref it.
1568 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1569 // the real owner (the surface) go away, but still hold onto the canvas?
1570 // *really* we want to sort of ref the surface again, but have the native object
1571 // know that it is supposed to be treated as a canvas...
1572 }
1573 return 1;
1574}
1575
1576static int lsurface_newImageSnapshot(lua_State* L) {
1577 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1578 if (NULL == image) {
1579 lua_pushnil(L);
1580 } else {
reed9fbc3f32014-10-21 07:12:58 -07001581 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001582 }
1583 return 1;
1584}
1585
1586static int lsurface_newSurface(lua_State* L) {
1587 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001588 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001589 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1590 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1591 if (NULL == surface) {
1592 lua_pushnil(L);
1593 } else {
reed9fbc3f32014-10-21 07:12:58 -07001594 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001595 }
1596 return 1;
1597}
1598
1599static int lsurface_gc(lua_State* L) {
1600 get_ref<SkSurface>(L, 1)->unref();
1601 return 0;
1602}
1603
1604static const struct luaL_Reg gSkSurface_Methods[] = {
1605 { "width", lsurface_width },
1606 { "height", lsurface_height },
1607 { "getCanvas", lsurface_getCanvas },
1608 { "newImageSnapshot", lsurface_newImageSnapshot },
1609 { "newSurface", lsurface_newSurface },
1610 { "__gc", lsurface_gc },
1611 { NULL, NULL }
1612};
1613
1614///////////////////////////////////////////////////////////////////////////////
1615
reed96affcd2014-10-13 12:38:04 -07001616static int lpicturerecorder_beginRecording(lua_State* L) {
1617 const SkScalar w = lua2scalar_def(L, 2, -1);
1618 const SkScalar h = lua2scalar_def(L, 3, -1);
1619 if (w <= 0 || h <= 0) {
1620 lua_pushnil(L);
1621 return 1;
1622 }
1623
1624 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1625 if (NULL == canvas) {
1626 lua_pushnil(L);
1627 return 1;
1628 }
1629
1630 push_ref(L, canvas);
1631 return 1;
1632}
1633
1634static int lpicturerecorder_getCanvas(lua_State* L) {
1635 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1636 if (NULL == canvas) {
1637 lua_pushnil(L);
1638 return 1;
1639 }
1640 push_ref(L, canvas);
1641 return 1;
1642}
1643
1644static int lpicturerecorder_endRecording(lua_State* L) {
1645 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1646 if (NULL == pic) {
1647 lua_pushnil(L);
1648 return 1;
1649 }
reed9fbc3f32014-10-21 07:12:58 -07001650 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001651 return 1;
1652}
1653
1654static int lpicturerecorder_gc(lua_State* L) {
1655 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1656 return 0;
1657}
1658
1659static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1660 { "beginRecording", lpicturerecorder_beginRecording },
1661 { "getCanvas", lpicturerecorder_getCanvas },
1662 { "endRecording", lpicturerecorder_endRecording },
1663 { "__gc", lpicturerecorder_gc },
1664 { NULL, NULL }
1665};
1666
1667///////////////////////////////////////////////////////////////////////////////
1668
1669static int lpicture_width(lua_State* L) {
1670 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1671 return 1;
1672}
1673
1674static int lpicture_height(lua_State* L) {
1675 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1676 return 1;
1677}
1678
1679static int lpicture_gc(lua_State* L) {
1680 get_ref<SkPicture>(L, 1)->unref();
1681 return 0;
1682}
1683
1684static const struct luaL_Reg gSkPicture_Methods[] = {
1685 { "width", lpicture_width },
1686 { "height", lpicture_height },
1687 { "__gc", lpicture_gc },
1688 { NULL, NULL }
1689};
1690
1691///////////////////////////////////////////////////////////////////////////////
1692
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001693static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001694 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001695 return 0;
1696}
1697
1698static const struct luaL_Reg gSkTypeface_Methods[] = {
1699 { "__gc", ltypeface_gc },
1700 { NULL, NULL }
1701};
1702
1703///////////////////////////////////////////////////////////////////////////////
1704
reed@google.com74ce6f02013-05-22 15:13:18 +00001705class AutoCallLua {
1706public:
1707 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1708 lua_getglobal(L, func);
1709 if (!lua_isfunction(L, -1)) {
1710 int t = lua_type(L, -1);
1711 SkDebugf("--- expected function %d\n", t);
1712 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001713
reed@google.com74ce6f02013-05-22 15:13:18 +00001714 lua_newtable(L);
1715 setfield_string(L, "verb", verb);
1716 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001717
reed@google.com74ce6f02013-05-22 15:13:18 +00001718 ~AutoCallLua() {
1719 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1720 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1721 }
1722 lua_settop(fL, -1);
1723 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001724
reed@google.com74ce6f02013-05-22 15:13:18 +00001725private:
1726 lua_State* fL;
1727};
1728
1729#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1730
1731///////////////////////////////////////////////////////////////////////////////
1732
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001733static int lsk_newDocumentPDF(lua_State* L) {
1734 const char* file = NULL;
1735 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1736 file = lua_tolstring(L, 1, NULL);
1737 }
1738
1739 SkDocument* doc = SkDocument::CreatePDF(file);
1740 if (NULL == doc) {
1741 // do I need to push a nil on the stack and return 1?
1742 return 0;
1743 } else {
reed9fbc3f32014-10-21 07:12:58 -07001744 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001745 return 1;
1746 }
1747}
1748
reed468b1812014-10-19 11:42:54 -07001749static int lsk_newBlurImageFilter(lua_State* L) {
1750 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1751 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1752 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1753 if (NULL == imf) {
1754 lua_pushnil(L);
1755 } else {
reed9fbc3f32014-10-21 07:12:58 -07001756 push_ref(L, imf)->unref();
1757 }
1758 return 1;
1759}
1760
1761static int lsk_newLinearGradient(lua_State* L) {
1762 SkScalar x0 = lua2scalar_def(L, 1, 0);
1763 SkScalar y0 = lua2scalar_def(L, 2, 0);
1764 SkColor c0 = lua2color(L, 3);
1765 SkScalar x1 = lua2scalar_def(L, 4, 0);
1766 SkScalar y1 = lua2scalar_def(L, 5, 0);
1767 SkColor c1 = lua2color(L, 6);
1768
1769 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1770 SkColor colors[] = { c0, c1 };
1771 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
1772 if (NULL == s) {
1773 lua_pushnil(L);
1774 } else {
1775 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001776 }
1777 return 1;
1778}
1779
reedbdc49ae2014-10-14 09:34:52 -07001780static int lsk_newMatrix(lua_State* L) {
1781 push_new<SkMatrix>(L)->reset();
1782 return 1;
1783}
1784
reed@google.com3597b732013-05-22 20:12:50 +00001785static int lsk_newPaint(lua_State* L) {
1786 push_new<SkPaint>(L);
1787 return 1;
1788}
1789
1790static int lsk_newPath(lua_State* L) {
1791 push_new<SkPath>(L);
1792 return 1;
1793}
1794
reed96affcd2014-10-13 12:38:04 -07001795static int lsk_newPictureRecorder(lua_State* L) {
1796 push_new<SkPictureRecorder>(L);
1797 return 1;
1798}
1799
reed@google.com3597b732013-05-22 20:12:50 +00001800static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001801 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001802 return 1;
1803}
1804
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001805static int lsk_newTypeface(lua_State* L) {
1806 const char* name = NULL;
1807 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001808
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001809 int count = lua_gettop(L);
1810 if (count > 0 && lua_isstring(L, 1)) {
1811 name = lua_tolstring(L, 1, NULL);
1812 if (count > 1 && lua_isnumber(L, 2)) {
1813 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1814 }
1815 }
1816
1817 SkTypeface* face = SkTypeface::CreateFromName(name,
1818 (SkTypeface::Style)style);
1819// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1820 if (NULL == face) {
1821 face = SkTypeface::RefDefault();
1822 }
reed9fbc3f32014-10-21 07:12:58 -07001823 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001824 return 1;
1825}
reed@google.com3597b732013-05-22 20:12:50 +00001826
reed485557f2014-10-12 10:36:47 -07001827static int lsk_newRasterSurface(lua_State* L) {
1828 int width = lua2int_def(L, 2, 0);
1829 int height = lua2int_def(L, 2, 0);
1830 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1831 SkSurface* surface = SkSurface::NewRaster(info);
1832 if (NULL == surface) {
1833 lua_pushnil(L);
1834 } else {
reed9fbc3f32014-10-21 07:12:58 -07001835 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001836 }
1837 return 1;
1838}
1839
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001840static int lsk_loadImage(lua_State* L) {
1841 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1842 const char* name = lua_tolstring(L, 1, NULL);
1843 SkAutoDataUnref data(SkData::NewFromFileName(name));
1844 if (data.get()) {
piotaixr4bcc2022014-09-17 14:33:30 -07001845 SkImage* image = SkImage::NewFromGenerator(
1846 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
1847
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001848 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07001849 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001850 return 1;
1851 }
1852 }
1853 }
1854 return 0;
1855}
1856
reed@google.com3597b732013-05-22 20:12:50 +00001857static void register_Sk(lua_State* L) {
1858 lua_newtable(L);
1859 lua_pushvalue(L, -1);
1860 lua_setglobal(L, "Sk");
1861 // the Sk table is still on top
1862
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001863 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001864 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07001865 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07001866 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07001867 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00001868 setfield_function(L, "newPaint", lsk_newPaint);
1869 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07001870 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00001871 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07001872 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001873 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001874 lua_pop(L, 1); // pop off the Sk table
1875}
1876
reed@google.com74ce6f02013-05-22 15:13:18 +00001877#define REG_CLASS(L, C) \
1878 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001879 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001880 lua_pushvalue(L, -1); \
1881 lua_setfield(L, -2, "__index"); \
1882 luaL_setfuncs(L, g##C##_Methods, 0); \
1883 lua_pop(L, 1); /* pop off the meta-table */ \
1884 } while (0)
1885
1886void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001887 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001888 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001889 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001890 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07001891 REG_CLASS(L, SkImageFilter);
reed@google.com74ce6f02013-05-22 15:13:18 +00001892 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001893 REG_CLASS(L, SkPath);
1894 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07001895 REG_CLASS(L, SkPicture);
1896 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00001897 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001898 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07001899 REG_CLASS(L, SkSurface);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001900 REG_CLASS(L, SkTypeface);
humper@google.com2815c192013-07-10 22:42:30 +00001901 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001902}
zachr@google.com28c27c82013-06-20 17:15:05 +00001903
reed@google.com7bce9982013-06-20 17:40:21 +00001904extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001905extern "C" int luaopen_skia(lua_State* L) {
1906 SkLua::Load(L);
1907 return 0;
1908}