blob: 967233d21b379ebfd3437d1a1c7dcbae52ff02df [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
reed1b6ab442014-11-03 19:55:41 -0800559static int lcanvas_drawTextBlob(lua_State* L) {
560 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
561 SkScalar x = lua2scalar(L, 3);
562 SkScalar y = lua2scalar(L, 4);
563 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
564 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
565 return 0;
566}
567
reed@google.com74ce6f02013-05-22 15:13:18 +0000568static int lcanvas_getSaveCount(lua_State* L) {
569 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
570 return 1;
571}
572
573static int lcanvas_getTotalMatrix(lua_State* L) {
574 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
575 return 1;
576}
577
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000578static int lcanvas_getClipStack(lua_State* L) {
579 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
580 return 1;
581}
582
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000583int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
584#if SK_SUPPORT_GPU
585 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
586 SkISize layerSize = canvas->getTopLayerSize();
587 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
588 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
589 layerSize.fWidth, layerSize.fHeight);
590
591 GrReducedClip::ElementList elements;
592 GrReducedClip::InitialState initialState;
593 int32_t genID;
594 SkIRect resultBounds;
595
596 const SkClipStack& stack = *canvas->getClipStack();
597
598 GrReducedClip::ReduceClipStack(stack,
599 queryBounds,
600 &elements,
601 &genID,
602 &initialState,
603 &resultBounds,
604 NULL);
605
606 GrReducedClip::ElementList::Iter iter(elements);
607 int i = 0;
608 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700609 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000610 SkLua(L).pushClipStackElement(*iter.get());
611 iter.next();
612 lua_rawseti(L, -2, ++i);
613 }
614 // Currently this only returns the element list to lua, not the initial state or result bounds.
615 // It could return these as additional items on the lua stack.
616 return 1;
617#else
618 return 0;
619#endif
620}
621
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000622static int lcanvas_save(lua_State* L) {
623 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
624 return 1;
625}
626
reed86217d82014-10-25 20:44:40 -0700627static int lcanvas_saveLayer(lua_State* L) {
628 SkPaint paint;
629 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(NULL, lua2OptionalPaint(L, 2, &paint)));
630 return 1;
631}
632
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000633static int lcanvas_restore(lua_State* L) {
634 get_ref<SkCanvas>(L, 1)->restore();
635 return 0;
636}
637
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000638static int lcanvas_scale(lua_State* L) {
639 SkScalar sx = lua2scalar_def(L, 2, 1);
640 SkScalar sy = lua2scalar_def(L, 3, sx);
641 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
642 return 0;
643}
644
reed@google.com3597b732013-05-22 20:12:50 +0000645static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000646 SkScalar tx = lua2scalar_def(L, 2, 0);
647 SkScalar ty = lua2scalar_def(L, 3, 0);
648 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
649 return 0;
650}
651
652static int lcanvas_rotate(lua_State* L) {
653 SkScalar degrees = lua2scalar_def(L, 2, 0);
654 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000655 return 0;
656}
657
reedbdc49ae2014-10-14 09:34:52 -0700658static int lcanvas_concat(lua_State* L) {
659 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
660 return 0;
661}
662
reed485557f2014-10-12 10:36:47 -0700663static int lcanvas_newSurface(lua_State* L) {
664 int width = lua2int_def(L, 2, 0);
665 int height = lua2int_def(L, 2, 0);
666 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
667 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
668 if (NULL == surface) {
669 lua_pushnil(L);
670 } else {
reed9fbc3f32014-10-21 07:12:58 -0700671 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -0700672 }
673 return 1;
674}
675
reed@google.com74ce6f02013-05-22 15:13:18 +0000676static int lcanvas_gc(lua_State* L) {
677 get_ref<SkCanvas>(L, 1)->unref();
678 return 0;
679}
680
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000681const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700682 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000683 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700684 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000685 { "drawRect", lcanvas_drawRect },
686 { "drawOval", lcanvas_drawOval },
687 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000688 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700689 { "drawImageRect", lcanvas_drawImageRect },
reed@google.comfd345872013-05-22 20:53:42 +0000690 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700691 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000692 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800693 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000694 { "getSaveCount", lcanvas_getSaveCount },
695 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000696 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000697#if SK_SUPPORT_GPU
698 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
699#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000700 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700701 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000702 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000703 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000704 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000705 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700706 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700707
708 { "newSurface", lcanvas_newSurface },
709
reed@google.com74ce6f02013-05-22 15:13:18 +0000710 { "__gc", lcanvas_gc },
711 { NULL, NULL }
712};
713
714///////////////////////////////////////////////////////////////////////////////
715
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000716static int ldocument_beginPage(lua_State* L) {
717 const SkRect* contentPtr = NULL;
718 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
719 lua2scalar(L, 3),
720 contentPtr));
721 return 1;
722}
723
724static int ldocument_endPage(lua_State* L) {
725 get_ref<SkDocument>(L, 1)->endPage();
726 return 0;
727}
728
729static int ldocument_close(lua_State* L) {
730 get_ref<SkDocument>(L, 1)->close();
731 return 0;
732}
733
734static int ldocument_gc(lua_State* L) {
735 get_ref<SkDocument>(L, 1)->unref();
736 return 0;
737}
738
739static const struct luaL_Reg gSkDocument_Methods[] = {
740 { "beginPage", ldocument_beginPage },
741 { "endPage", ldocument_endPage },
742 { "close", ldocument_close },
743 { "__gc", ldocument_gc },
744 { NULL, NULL }
745};
746
747///////////////////////////////////////////////////////////////////////////////
748
reed@google.com74ce6f02013-05-22 15:13:18 +0000749static int lpaint_isAntiAlias(lua_State* L) {
750 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
751 return 1;
752}
753
754static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000755 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000756 return 0;
757}
758
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000759static int lpaint_isDither(lua_State* L) {
760 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
761 return 1;
762}
763
reedbb8a0ab2014-11-03 22:32:07 -0800764static int lpaint_setDither(lua_State* L) {
765 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
766 return 0;
767}
768
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000769static int lpaint_isUnderlineText(lua_State* L) {
770 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
771 return 1;
772}
773
774static int lpaint_isStrikeThruText(lua_State* L) {
775 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
776 return 1;
777}
778
779static int lpaint_isFakeBoldText(lua_State* L) {
780 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
781 return 1;
782}
783
784static int lpaint_isLinearText(lua_State* L) {
785 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
786 return 1;
787}
788
789static int lpaint_isSubpixelText(lua_State* L) {
790 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
791 return 1;
792}
793
reed09a1d672014-10-11 13:13:11 -0700794static int lpaint_setSubpixelText(lua_State* L) {
795 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
796 return 1;
797}
798
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000799static int lpaint_isDevKernText(lua_State* L) {
800 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
801 return 1;
802}
803
804static int lpaint_isLCDRenderText(lua_State* L) {
805 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
806 return 1;
807}
808
reed36c9c112014-11-04 10:58:42 -0800809static int lpaint_setLCDRenderText(lua_State* L) {
810 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
811 return 1;
812}
813
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000814static int lpaint_isEmbeddedBitmapText(lua_State* L) {
815 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
816 return 1;
817}
818
819static int lpaint_isAutohinted(lua_State* L) {
820 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
821 return 1;
822}
823
824static int lpaint_isVerticalText(lua_State* L) {
825 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
826 return 1;
827}
828
reed468b1812014-10-19 11:42:54 -0700829static int lpaint_getAlpha(lua_State* L) {
830 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
831 return 1;
832}
833
834static int lpaint_setAlpha(lua_State* L) {
835 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
836 return 0;
837}
838
reed@google.com74ce6f02013-05-22 15:13:18 +0000839static int lpaint_getColor(lua_State* L) {
840 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
841 return 1;
842}
843
844static int lpaint_setColor(lua_State* L) {
845 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
846 return 0;
847}
848
reed@google.come3823fd2013-05-30 18:55:14 +0000849static int lpaint_getTextSize(lua_State* L) {
850 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
851 return 1;
852}
853
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000854static int lpaint_getTextScaleX(lua_State* L) {
855 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
856 return 1;
857}
858
859static int lpaint_getTextSkewX(lua_State* L) {
860 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
861 return 1;
862}
863
reed@google.come3823fd2013-05-30 18:55:14 +0000864static int lpaint_setTextSize(lua_State* L) {
865 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
866 return 0;
867}
868
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000869static int lpaint_getTypeface(lua_State* L) {
870 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
871 return 1;
872}
873
874static int lpaint_setTypeface(lua_State* L) {
875 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
876 return 0;
877}
878
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000879static int lpaint_getHinting(lua_State* L) {
880 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
881 return 1;
882}
883
reed@google.come3823fd2013-05-30 18:55:14 +0000884static int lpaint_getFontID(lua_State* L) {
885 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
886 SkLua(L).pushU32(SkTypeface::UniqueID(face));
887 return 1;
888}
889
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000890static const struct {
891 const char* fLabel;
892 SkPaint::Align fAlign;
893} gAlignRec[] = {
894 { "left", SkPaint::kLeft_Align },
895 { "center", SkPaint::kCenter_Align },
896 { "right", SkPaint::kRight_Align },
897};
898
899static int lpaint_getTextAlign(lua_State* L) {
900 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
901 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
902 if (gAlignRec[i].fAlign == align) {
903 lua_pushstring(L, gAlignRec[i].fLabel);
904 return 1;
905 }
906 }
907 return 0;
908}
909
910static int lpaint_setTextAlign(lua_State* L) {
911 if (lua_isstring(L, 2)) {
912 size_t len;
913 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000914
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000915 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
916 if (!strcmp(gAlignRec[i].fLabel, label)) {
917 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
918 break;
919 }
920 }
921 }
922 return 0;
923}
924
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000925static int lpaint_getStroke(lua_State* L) {
926 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
927 return 1;
928}
929
930static int lpaint_setStroke(lua_State* L) {
931 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000932
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000933 if (lua_toboolean(L, 2)) {
934 style = SkPaint::kStroke_Style;
935 } else {
936 style = SkPaint::kFill_Style;
937 }
938 get_obj<SkPaint>(L, 1)->setStyle(style);
939 return 0;
940}
941
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000942static int lpaint_getStrokeCap(lua_State* L) {
943 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
944 return 1;
945}
946
947static int lpaint_getStrokeJoin(lua_State* L) {
948 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
949 return 1;
950}
951
952static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000953 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000954 return 1;
955}
956
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000957static int lpaint_getStrokeWidth(lua_State* L) {
958 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
959 return 1;
960}
961
962static int lpaint_setStrokeWidth(lua_State* L) {
963 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
964 return 0;
965}
966
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000967static int lpaint_getStrokeMiter(lua_State* L) {
968 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
969 return 1;
970}
971
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000972static int lpaint_measureText(lua_State* L) {
973 if (lua_isstring(L, 2)) {
974 size_t len;
975 const char* text = lua_tolstring(L, 2, &len);
976 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
977 return 1;
978 }
979 return 0;
980}
981
982struct FontMetrics {
983 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
984 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
985 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
986 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
987 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
988 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
989 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
990 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
991 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
992};
993
994static int lpaint_getFontMetrics(lua_State* L) {
995 SkPaint::FontMetrics fm;
996 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000997
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000998 lua_newtable(L);
999 setfield_scalar(L, "top", fm.fTop);
1000 setfield_scalar(L, "ascent", fm.fAscent);
1001 setfield_scalar(L, "descent", fm.fDescent);
1002 setfield_scalar(L, "bottom", fm.fBottom);
1003 setfield_scalar(L, "leading", fm.fLeading);
1004 SkLua(L).pushScalar(height);
1005 return 2;
1006}
1007
reed@google.com29563872013-07-10 21:23:49 +00001008static int lpaint_getEffects(lua_State* L) {
1009 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001010
reed@google.com29563872013-07-10 21:23:49 +00001011 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -07001012 setfield_bool_if(L, "looper", !!paint->getLooper());
1013 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
1014 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
1015 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
1016 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +00001017 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1018 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -07001019 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +00001020 return 1;
1021}
1022
reed468b1812014-10-19 11:42:54 -07001023static int lpaint_getImageFilter(lua_State* L) {
1024 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1025 SkImageFilter* imf = paint->getImageFilter();
1026 if (imf) {
1027 push_ref(L, imf);
1028 return 1;
1029 }
1030 return 0;
1031}
1032
1033static int lpaint_setImageFilter(lua_State* L) {
1034 SkPaint* paint = get_obj<SkPaint>(L, 1);
1035 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1036 return 0;
1037}
1038
reed@google.com5fdc9832013-07-24 15:47:52 +00001039static int lpaint_getShader(lua_State* L) {
1040 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1041 SkShader* shader = paint->getShader();
1042 if (shader) {
1043 push_ref(L, shader);
1044 return 1;
1045 }
1046 return 0;
1047}
1048
reed9fbc3f32014-10-21 07:12:58 -07001049static int lpaint_setShader(lua_State* L) {
1050 SkPaint* paint = get_obj<SkPaint>(L, 1);
1051 paint->setShader(get_ref<SkShader>(L, 2));
1052 return 0;
1053}
1054
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001055static int lpaint_getPathEffect(lua_State* L) {
1056 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1057 SkPathEffect* pe = paint->getPathEffect();
1058 if (pe) {
1059 push_ref(L, pe);
1060 return 1;
1061 }
1062 return 0;
1063}
1064
reed@google.com74ce6f02013-05-22 15:13:18 +00001065static int lpaint_gc(lua_State* L) {
1066 get_obj<SkPaint>(L, 1)->~SkPaint();
1067 return 0;
1068}
1069
1070static const struct luaL_Reg gSkPaint_Methods[] = {
1071 { "isAntiAlias", lpaint_isAntiAlias },
1072 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001073 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001074 { "setDither", lpaint_setDither },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001075 { "isUnderlineText", lpaint_isUnderlineText },
1076 { "isStrikeThruText", lpaint_isStrikeThruText },
1077 { "isFakeBoldText", lpaint_isFakeBoldText },
1078 { "isLinearText", lpaint_isLinearText },
1079 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001080 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001081 { "isDevKernText", lpaint_isDevKernText },
1082 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001083 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001084 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1085 { "isAutohinted", lpaint_isAutohinted },
1086 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001087 { "getAlpha", lpaint_getAlpha },
1088 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001089 { "getColor", lpaint_getColor },
1090 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001091 { "getTextSize", lpaint_getTextSize },
1092 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001093 { "getTextScaleX", lpaint_getTextScaleX },
1094 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001095 { "getTypeface", lpaint_getTypeface },
1096 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001097 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001098 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001099 { "getTextAlign", lpaint_getTextAlign },
1100 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001101 { "getStroke", lpaint_getStroke },
1102 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001103 { "getStrokeCap", lpaint_getStrokeCap },
1104 { "getStrokeJoin", lpaint_getStrokeJoin },
1105 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001106 { "getStrokeWidth", lpaint_getStrokeWidth },
1107 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001108 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001109 { "measureText", lpaint_measureText },
1110 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001111 { "getEffects", lpaint_getEffects },
reed468b1812014-10-19 11:42:54 -07001112 { "getImageFilter", lpaint_getImageFilter },
1113 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001114 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001115 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001116 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001117 { "__gc", lpaint_gc },
1118 { NULL, NULL }
1119};
1120
1121///////////////////////////////////////////////////////////////////////////////
1122
reed@google.com5fdc9832013-07-24 15:47:52 +00001123static const char* mode2string(SkShader::TileMode mode) {
1124 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1125 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1126 return gNames[mode];
1127}
1128
1129static const char* gradtype2string(SkShader::GradientType t) {
1130 static const char* gNames[] = {
1131 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1132 };
1133 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1134 return gNames[t];
1135}
1136
1137static int lshader_isOpaque(lua_State* L) {
1138 SkShader* shader = get_ref<SkShader>(L, 1);
1139 return shader && shader->isOpaque();
1140}
1141
1142static int lshader_asABitmap(lua_State* L) {
1143 SkShader* shader = get_ref<SkShader>(L, 1);
1144 if (shader) {
1145 SkBitmap bm;
1146 SkMatrix matrix;
1147 SkShader::TileMode modes[2];
1148 switch (shader->asABitmap(&bm, &matrix, modes)) {
1149 case SkShader::kDefault_BitmapType:
1150 lua_newtable(L);
1151 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1152 setfield_number(L, "width", bm.width());
1153 setfield_number(L, "height", bm.height());
1154 setfield_string(L, "tileX", mode2string(modes[0]));
1155 setfield_string(L, "tileY", mode2string(modes[1]));
1156 return 1;
1157 default:
1158 break;
1159 }
1160 }
1161 return 0;
1162}
1163
1164static int lshader_asAGradient(lua_State* L) {
1165 SkShader* shader = get_ref<SkShader>(L, 1);
1166 if (shader) {
1167 SkShader::GradientInfo info;
1168 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001169
1170 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001171 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001172
1173 info.fColorCount = 3;
1174 info.fColors = &colors[0];
1175 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001176
reed@google.com5fdc9832013-07-24 15:47:52 +00001177 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001178
reed@google.com5fdc9832013-07-24 15:47:52 +00001179 if (SkShader::kNone_GradientType != t) {
1180 lua_newtable(L);
1181 setfield_string(L, "type", gradtype2string(t));
1182 setfield_number(L, "colorCount", info.fColorCount);
1183 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001184
1185 if (info.fColorCount == 3){
1186 setfield_number(L, "midPos", pos[1]);
1187 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001188
reed@google.com5fdc9832013-07-24 15:47:52 +00001189 return 1;
1190 }
1191 }
1192 return 0;
1193}
1194
1195static int lshader_gc(lua_State* L) {
1196 get_ref<SkShader>(L, 1)->unref();
1197 return 0;
1198}
1199
1200static const struct luaL_Reg gSkShader_Methods[] = {
1201 { "isOpaque", lshader_isOpaque },
1202 { "asABitmap", lshader_asABitmap },
1203 { "asAGradient", lshader_asAGradient },
1204 { "__gc", lshader_gc },
1205 { NULL, NULL }
1206};
1207
1208///////////////////////////////////////////////////////////////////////////////
1209
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001210static int lpatheffect_asADash(lua_State* L) {
1211 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1212 if (pe) {
1213 SkPathEffect::DashInfo info;
1214 SkPathEffect::DashType dashType = pe->asADash(&info);
1215 if (SkPathEffect::kDash_DashType == dashType) {
1216 SkAutoTArray<SkScalar> intervals(info.fCount);
1217 info.fIntervals = intervals.get();
1218 pe->asADash(&info);
1219 SkLua(L).pushDash(info);
1220 return 1;
1221 }
1222 }
1223 return 0;
1224}
1225
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001226static int lpatheffect_gc(lua_State* L) {
1227 get_ref<SkPathEffect>(L, 1)->unref();
1228 return 0;
1229}
1230
1231static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001232 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001233 { "__gc", lpatheffect_gc },
1234 { NULL, NULL }
1235};
1236
1237///////////////////////////////////////////////////////////////////////////////
1238
reed468b1812014-10-19 11:42:54 -07001239static int lpimagefilter_gc(lua_State* L) {
1240 get_ref<SkImageFilter>(L, 1)->unref();
1241 return 0;
1242}
1243
1244static const struct luaL_Reg gSkImageFilter_Methods[] = {
1245 { "__gc", lpimagefilter_gc },
1246 { NULL, NULL }
1247};
1248
1249///////////////////////////////////////////////////////////////////////////////
1250
humper@google.com2815c192013-07-10 22:42:30 +00001251static int lmatrix_getType(lua_State* L) {
1252 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001253
humper@google.com2815c192013-07-10 22:42:30 +00001254 lua_newtable(L);
1255 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1256 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1257 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1258 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1259 return 1;
1260}
1261
humper@google.com0f48ee02013-07-26 15:23:43 +00001262static int lmatrix_getScaleX(lua_State* L) {
1263 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1264 return 1;
1265}
1266
1267static int lmatrix_getScaleY(lua_State* L) {
1268 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1269 return 1;
1270}
1271
1272static int lmatrix_getTranslateX(lua_State* L) {
1273 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1274 return 1;
1275}
1276
1277static int lmatrix_getTranslateY(lua_State* L) {
1278 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1279 return 1;
1280}
1281
reedbdc49ae2014-10-14 09:34:52 -07001282static int lmatrix_setRectToRect(lua_State* L) {
1283 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1284 SkRect srcR, dstR;
1285 lua2rect(L, 2, &srcR);
1286 lua2rect(L, 3, &dstR);
1287 const char* scaleToFitStr = lua_tostring(L, 4);
1288 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1289
1290 if (scaleToFitStr) {
1291 const struct {
1292 const char* fName;
1293 SkMatrix::ScaleToFit fScaleToFit;
1294 } rec[] = {
1295 { "fill", SkMatrix::kFill_ScaleToFit },
1296 { "start", SkMatrix::kStart_ScaleToFit },
1297 { "center", SkMatrix::kCenter_ScaleToFit },
1298 { "end", SkMatrix::kEnd_ScaleToFit },
1299 };
1300
1301 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1302 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1303 scaleToFit = rec[i].fScaleToFit;
1304 break;
1305 }
1306 }
1307 }
1308
1309 matrix->setRectToRect(srcR, dstR, scaleToFit);
1310 return 0;
1311}
1312
humper@google.com2815c192013-07-10 22:42:30 +00001313static const struct luaL_Reg gSkMatrix_Methods[] = {
1314 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001315 { "getScaleX", lmatrix_getScaleX },
1316 { "getScaleY", lmatrix_getScaleY },
1317 { "getTranslateX", lmatrix_getTranslateX },
1318 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001319 { "setRectToRect", lmatrix_setRectToRect },
humper@google.com2815c192013-07-10 22:42:30 +00001320 { NULL, NULL }
1321};
1322
1323///////////////////////////////////////////////////////////////////////////////
1324
reed@google.com74ce6f02013-05-22 15:13:18 +00001325static int lpath_getBounds(lua_State* L) {
1326 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1327 return 1;
1328}
1329
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001330static const char* fill_type_to_str(SkPath::FillType fill) {
1331 switch (fill) {
1332 case SkPath::kEvenOdd_FillType:
1333 return "even-odd";
1334 case SkPath::kWinding_FillType:
1335 return "winding";
1336 case SkPath::kInverseEvenOdd_FillType:
1337 return "inverse-even-odd";
1338 case SkPath::kInverseWinding_FillType:
1339 return "inverse-winding";
1340 }
1341 return "unknown";
1342}
1343
1344static int lpath_getFillType(lua_State* L) {
1345 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1346 SkLua(L).pushString(fill_type_to_str(fill));
1347 return 1;
1348}
1349
1350static SkString segment_masks_to_str(uint32_t segmentMasks) {
1351 SkString result;
1352 bool first = true;
1353 if (SkPath::kLine_SegmentMask & segmentMasks) {
1354 result.append("line");
1355 first = false;
1356 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1357 }
1358 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1359 if (!first) {
1360 result.append(" ");
1361 }
1362 result.append("quad");
1363 first = false;
1364 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1365 }
1366 if (SkPath::kConic_SegmentMask & segmentMasks) {
1367 if (!first) {
1368 result.append(" ");
1369 }
1370 result.append("conic");
1371 first = false;
1372 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1373 }
1374 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1375 if (!first) {
1376 result.append(" ");
1377 }
1378 result.append("cubic");
1379 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1380 }
1381 SkASSERT(0 == segmentMasks);
1382 return result;
1383}
1384
krajcevski95498ed2014-08-18 08:02:33 -07001385static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001386 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1387 SkLua(L).pushString(segment_masks_to_str(segMasks));
1388 return 1;
1389}
1390
1391static int lpath_isConvex(lua_State* L) {
1392 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1393 SkLua(L).pushBool(isConvex);
1394 return 1;
1395}
1396
reed@google.com74ce6f02013-05-22 15:13:18 +00001397static int lpath_isEmpty(lua_State* L) {
1398 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1399 return 1;
1400}
1401
1402static int lpath_isRect(lua_State* L) {
1403 SkRect r;
1404 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1405 int ret_count = 1;
1406 lua_pushboolean(L, pred);
1407 if (pred) {
1408 SkLua(L).pushRect(r);
1409 ret_count += 1;
1410 }
1411 return ret_count;
1412}
1413
1414static const char* dir2string(SkPath::Direction dir) {
1415 static const char* gStr[] = {
1416 "unknown", "cw", "ccw"
1417 };
1418 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1419 return gStr[dir];
1420}
1421
1422static int lpath_isNestedRects(lua_State* L) {
1423 SkRect rects[2];
1424 SkPath::Direction dirs[2];
1425 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1426 int ret_count = 1;
1427 lua_pushboolean(L, pred);
1428 if (pred) {
1429 SkLua lua(L);
1430 lua.pushRect(rects[0]);
1431 lua.pushRect(rects[1]);
1432 lua_pushstring(L, dir2string(dirs[0]));
1433 lua_pushstring(L, dir2string(dirs[0]));
1434 ret_count += 4;
1435 }
1436 return ret_count;
1437}
1438
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001439static int lpath_countPoints(lua_State* L) {
1440 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1441 return 1;
1442}
1443
reed@google.com74ce6f02013-05-22 15:13:18 +00001444static int lpath_reset(lua_State* L) {
1445 get_obj<SkPath>(L, 1)->reset();
1446 return 0;
1447}
1448
1449static int lpath_moveTo(lua_State* L) {
1450 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1451 return 0;
1452}
1453
1454static int lpath_lineTo(lua_State* L) {
1455 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1456 return 0;
1457}
1458
1459static int lpath_quadTo(lua_State* L) {
1460 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1461 lua2scalar(L, 4), lua2scalar(L, 5));
1462 return 0;
1463}
1464
1465static int lpath_cubicTo(lua_State* L) {
1466 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1467 lua2scalar(L, 4), lua2scalar(L, 5),
1468 lua2scalar(L, 6), lua2scalar(L, 7));
1469 return 0;
1470}
1471
1472static int lpath_close(lua_State* L) {
1473 get_obj<SkPath>(L, 1)->close();
1474 return 0;
1475}
1476
1477static int lpath_gc(lua_State* L) {
1478 get_obj<SkPath>(L, 1)->~SkPath();
1479 return 0;
1480}
1481
1482static const struct luaL_Reg gSkPath_Methods[] = {
1483 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001484 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001485 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001486 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001487 { "isEmpty", lpath_isEmpty },
1488 { "isRect", lpath_isRect },
1489 { "isNestedRects", lpath_isNestedRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001490 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001491 { "reset", lpath_reset },
1492 { "moveTo", lpath_moveTo },
1493 { "lineTo", lpath_lineTo },
1494 { "quadTo", lpath_quadTo },
1495 { "cubicTo", lpath_cubicTo },
1496 { "close", lpath_close },
1497 { "__gc", lpath_gc },
1498 { NULL, NULL }
1499};
1500
1501///////////////////////////////////////////////////////////////////////////////
1502
1503static const char* rrect_type(const SkRRect& rr) {
1504 switch (rr.getType()) {
1505 case SkRRect::kUnknown_Type: return "unknown";
1506 case SkRRect::kEmpty_Type: return "empty";
1507 case SkRRect::kRect_Type: return "rect";
1508 case SkRRect::kOval_Type: return "oval";
1509 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001510 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001511 case SkRRect::kComplex_Type: return "complex";
1512 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001513 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001514 return "";
1515}
1516
1517static int lrrect_rect(lua_State* L) {
1518 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1519 return 1;
1520}
1521
1522static int lrrect_type(lua_State* L) {
1523 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1524 return 1;
1525}
1526
1527static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001528 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001529 SkVector v;
1530 if (corner < 0 || corner > 3) {
1531 SkDebugf("bad corner index %d", corner);
1532 v.set(0, 0);
1533 } else {
1534 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1535 }
1536 lua_pushnumber(L, v.fX);
1537 lua_pushnumber(L, v.fY);
1538 return 2;
1539}
1540
1541static int lrrect_gc(lua_State* L) {
1542 get_obj<SkRRect>(L, 1)->~SkRRect();
1543 return 0;
1544}
1545
1546static const struct luaL_Reg gSkRRect_Methods[] = {
1547 { "rect", lrrect_rect },
1548 { "type", lrrect_type },
1549 { "radii", lrrect_radii },
1550 { "__gc", lrrect_gc },
1551 { NULL, NULL }
1552};
1553
1554///////////////////////////////////////////////////////////////////////////////
1555
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001556static int limage_width(lua_State* L) {
1557 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1558 return 1;
1559}
1560
1561static int limage_height(lua_State* L) {
1562 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1563 return 1;
1564}
1565
1566static int limage_gc(lua_State* L) {
1567 get_ref<SkImage>(L, 1)->unref();
1568 return 0;
1569}
1570
1571static const struct luaL_Reg gSkImage_Methods[] = {
1572 { "width", limage_width },
1573 { "height", limage_height },
1574 { "__gc", limage_gc },
1575 { NULL, NULL }
1576};
1577
1578///////////////////////////////////////////////////////////////////////////////
1579
reed485557f2014-10-12 10:36:47 -07001580static int lsurface_width(lua_State* L) {
1581 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1582 return 1;
1583}
1584
1585static int lsurface_height(lua_State* L) {
1586 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1587 return 1;
1588}
1589
1590static int lsurface_getCanvas(lua_State* L) {
1591 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1592 if (NULL == canvas) {
1593 lua_pushnil(L);
1594 } else {
1595 push_ref(L, canvas);
1596 // note: we don't unref canvas, since getCanvas did not ref it.
1597 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1598 // the real owner (the surface) go away, but still hold onto the canvas?
1599 // *really* we want to sort of ref the surface again, but have the native object
1600 // know that it is supposed to be treated as a canvas...
1601 }
1602 return 1;
1603}
1604
1605static int lsurface_newImageSnapshot(lua_State* L) {
1606 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1607 if (NULL == image) {
1608 lua_pushnil(L);
1609 } else {
reed9fbc3f32014-10-21 07:12:58 -07001610 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001611 }
1612 return 1;
1613}
1614
1615static int lsurface_newSurface(lua_State* L) {
1616 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001617 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001618 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1619 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1620 if (NULL == surface) {
1621 lua_pushnil(L);
1622 } else {
reed9fbc3f32014-10-21 07:12:58 -07001623 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001624 }
1625 return 1;
1626}
1627
1628static int lsurface_gc(lua_State* L) {
1629 get_ref<SkSurface>(L, 1)->unref();
1630 return 0;
1631}
1632
1633static const struct luaL_Reg gSkSurface_Methods[] = {
1634 { "width", lsurface_width },
1635 { "height", lsurface_height },
1636 { "getCanvas", lsurface_getCanvas },
1637 { "newImageSnapshot", lsurface_newImageSnapshot },
1638 { "newSurface", lsurface_newSurface },
1639 { "__gc", lsurface_gc },
1640 { NULL, NULL }
1641};
1642
1643///////////////////////////////////////////////////////////////////////////////
1644
reed96affcd2014-10-13 12:38:04 -07001645static int lpicturerecorder_beginRecording(lua_State* L) {
1646 const SkScalar w = lua2scalar_def(L, 2, -1);
1647 const SkScalar h = lua2scalar_def(L, 3, -1);
1648 if (w <= 0 || h <= 0) {
1649 lua_pushnil(L);
1650 return 1;
1651 }
1652
1653 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1654 if (NULL == canvas) {
1655 lua_pushnil(L);
1656 return 1;
1657 }
1658
1659 push_ref(L, canvas);
1660 return 1;
1661}
1662
1663static int lpicturerecorder_getCanvas(lua_State* L) {
1664 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1665 if (NULL == canvas) {
1666 lua_pushnil(L);
1667 return 1;
1668 }
1669 push_ref(L, canvas);
1670 return 1;
1671}
1672
1673static int lpicturerecorder_endRecording(lua_State* L) {
1674 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1675 if (NULL == pic) {
1676 lua_pushnil(L);
1677 return 1;
1678 }
reed9fbc3f32014-10-21 07:12:58 -07001679 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001680 return 1;
1681}
1682
1683static int lpicturerecorder_gc(lua_State* L) {
1684 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1685 return 0;
1686}
1687
1688static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1689 { "beginRecording", lpicturerecorder_beginRecording },
1690 { "getCanvas", lpicturerecorder_getCanvas },
1691 { "endRecording", lpicturerecorder_endRecording },
1692 { "__gc", lpicturerecorder_gc },
1693 { NULL, NULL }
1694};
1695
1696///////////////////////////////////////////////////////////////////////////////
1697
1698static int lpicture_width(lua_State* L) {
1699 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1700 return 1;
1701}
1702
1703static int lpicture_height(lua_State* L) {
1704 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1705 return 1;
1706}
1707
1708static int lpicture_gc(lua_State* L) {
1709 get_ref<SkPicture>(L, 1)->unref();
1710 return 0;
1711}
1712
1713static const struct luaL_Reg gSkPicture_Methods[] = {
1714 { "width", lpicture_width },
1715 { "height", lpicture_height },
1716 { "__gc", lpicture_gc },
1717 { NULL, NULL }
1718};
1719
1720///////////////////////////////////////////////////////////////////////////////
1721
reed1b6ab442014-11-03 19:55:41 -08001722static int ltextblob_bounds(lua_State* L) {
1723 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1724 return 1;
1725}
1726
1727static int ltextblob_gc(lua_State* L) {
1728 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1729 return 0;
1730}
1731
1732static const struct luaL_Reg gSkTextBlob_Methods[] = {
1733 { "bounds", ltextblob_bounds },
1734 { "__gc", ltextblob_gc },
1735 { NULL, NULL }
1736};
1737
1738///////////////////////////////////////////////////////////////////////////////
1739
reed36c9c112014-11-04 10:58:42 -08001740static int ltypeface_getFamilyName(lua_State* L) {
1741 SkString str;
1742 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1743 lua_pushstring(L, str.c_str());
1744 return 1;
1745}
1746
1747static int ltypeface_getStyle(lua_State* L) {
1748 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1749 return 1;
1750}
1751
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001752static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001753 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001754 return 0;
1755}
1756
1757static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001758 { "getFamilyName", ltypeface_getFamilyName },
1759 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001760 { "__gc", ltypeface_gc },
1761 { NULL, NULL }
1762};
1763
1764///////////////////////////////////////////////////////////////////////////////
1765
reed@google.com74ce6f02013-05-22 15:13:18 +00001766class AutoCallLua {
1767public:
1768 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1769 lua_getglobal(L, func);
1770 if (!lua_isfunction(L, -1)) {
1771 int t = lua_type(L, -1);
1772 SkDebugf("--- expected function %d\n", t);
1773 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001774
reed@google.com74ce6f02013-05-22 15:13:18 +00001775 lua_newtable(L);
1776 setfield_string(L, "verb", verb);
1777 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001778
reed@google.com74ce6f02013-05-22 15:13:18 +00001779 ~AutoCallLua() {
1780 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1781 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1782 }
1783 lua_settop(fL, -1);
1784 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001785
reed@google.com74ce6f02013-05-22 15:13:18 +00001786private:
1787 lua_State* fL;
1788};
1789
1790#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1791
1792///////////////////////////////////////////////////////////////////////////////
1793
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001794static int lsk_newDocumentPDF(lua_State* L) {
1795 const char* file = NULL;
1796 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1797 file = lua_tolstring(L, 1, NULL);
1798 }
1799
1800 SkDocument* doc = SkDocument::CreatePDF(file);
1801 if (NULL == doc) {
1802 // do I need to push a nil on the stack and return 1?
1803 return 0;
1804 } else {
reed9fbc3f32014-10-21 07:12:58 -07001805 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001806 return 1;
1807 }
1808}
1809
reed468b1812014-10-19 11:42:54 -07001810static int lsk_newBlurImageFilter(lua_State* L) {
1811 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1812 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1813 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1814 if (NULL == imf) {
1815 lua_pushnil(L);
1816 } else {
reed9fbc3f32014-10-21 07:12:58 -07001817 push_ref(L, imf)->unref();
1818 }
1819 return 1;
1820}
1821
1822static int lsk_newLinearGradient(lua_State* L) {
1823 SkScalar x0 = lua2scalar_def(L, 1, 0);
1824 SkScalar y0 = lua2scalar_def(L, 2, 0);
1825 SkColor c0 = lua2color(L, 3);
1826 SkScalar x1 = lua2scalar_def(L, 4, 0);
1827 SkScalar y1 = lua2scalar_def(L, 5, 0);
1828 SkColor c1 = lua2color(L, 6);
1829
1830 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1831 SkColor colors[] = { c0, c1 };
1832 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
1833 if (NULL == s) {
1834 lua_pushnil(L);
1835 } else {
1836 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001837 }
1838 return 1;
1839}
1840
reedbdc49ae2014-10-14 09:34:52 -07001841static int lsk_newMatrix(lua_State* L) {
1842 push_new<SkMatrix>(L)->reset();
1843 return 1;
1844}
1845
reed@google.com3597b732013-05-22 20:12:50 +00001846static int lsk_newPaint(lua_State* L) {
1847 push_new<SkPaint>(L);
1848 return 1;
1849}
1850
1851static int lsk_newPath(lua_State* L) {
1852 push_new<SkPath>(L);
1853 return 1;
1854}
1855
reed96affcd2014-10-13 12:38:04 -07001856static int lsk_newPictureRecorder(lua_State* L) {
1857 push_new<SkPictureRecorder>(L);
1858 return 1;
1859}
1860
reed@google.com3597b732013-05-22 20:12:50 +00001861static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001862 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001863 return 1;
1864}
1865
reed1b6ab442014-11-03 19:55:41 -08001866#include "SkTextBox.h"
1867// Sk.newTextBlob(text, rect, paint)
1868static int lsk_newTextBlob(lua_State* L) {
1869 const char* text = lua_tolstring(L, 1, NULL);
1870 SkRect bounds;
1871 lua2rect(L, 2, &bounds);
1872 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1873
1874 SkTextBox box;
1875 box.setMode(SkTextBox::kLineBreak_Mode);
1876 box.setBox(bounds);
1877 box.setText(text, strlen(text), paint);
1878
1879 SkScalar newBottom;
1880 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
1881 push_ref<SkTextBlob>(L, blob);
1882 SkLua(L).pushScalar(newBottom);
1883 return 2;
1884}
1885
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001886static int lsk_newTypeface(lua_State* L) {
1887 const char* name = NULL;
1888 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001889
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001890 int count = lua_gettop(L);
1891 if (count > 0 && lua_isstring(L, 1)) {
1892 name = lua_tolstring(L, 1, NULL);
1893 if (count > 1 && lua_isnumber(L, 2)) {
1894 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1895 }
1896 }
1897
1898 SkTypeface* face = SkTypeface::CreateFromName(name,
1899 (SkTypeface::Style)style);
1900// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1901 if (NULL == face) {
1902 face = SkTypeface::RefDefault();
1903 }
reed9fbc3f32014-10-21 07:12:58 -07001904 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001905 return 1;
1906}
reed@google.com3597b732013-05-22 20:12:50 +00001907
reed485557f2014-10-12 10:36:47 -07001908static int lsk_newRasterSurface(lua_State* L) {
1909 int width = lua2int_def(L, 2, 0);
1910 int height = lua2int_def(L, 2, 0);
1911 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1912 SkSurface* surface = SkSurface::NewRaster(info);
1913 if (NULL == surface) {
1914 lua_pushnil(L);
1915 } else {
reed9fbc3f32014-10-21 07:12:58 -07001916 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001917 }
1918 return 1;
1919}
1920
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001921static int lsk_loadImage(lua_State* L) {
1922 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1923 const char* name = lua_tolstring(L, 1, NULL);
1924 SkAutoDataUnref data(SkData::NewFromFileName(name));
1925 if (data.get()) {
piotaixr4bcc2022014-09-17 14:33:30 -07001926 SkImage* image = SkImage::NewFromGenerator(
1927 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
1928
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001929 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07001930 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001931 return 1;
1932 }
1933 }
1934 }
1935 return 0;
1936}
1937
reed@google.com3597b732013-05-22 20:12:50 +00001938static void register_Sk(lua_State* L) {
1939 lua_newtable(L);
1940 lua_pushvalue(L, -1);
1941 lua_setglobal(L, "Sk");
1942 // the Sk table is still on top
1943
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001944 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001945 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07001946 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07001947 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07001948 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00001949 setfield_function(L, "newPaint", lsk_newPaint);
1950 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07001951 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00001952 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07001953 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08001954 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001955 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00001956 lua_pop(L, 1); // pop off the Sk table
1957}
1958
reed@google.com74ce6f02013-05-22 15:13:18 +00001959#define REG_CLASS(L, C) \
1960 do { \
reed@google.com3597b732013-05-22 20:12:50 +00001961 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00001962 lua_pushvalue(L, -1); \
1963 lua_setfield(L, -2, "__index"); \
1964 luaL_setfuncs(L, g##C##_Methods, 0); \
1965 lua_pop(L, 1); /* pop off the meta-table */ \
1966 } while (0)
1967
1968void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00001969 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00001970 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001971 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001972 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07001973 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08001974 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00001975 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001976 REG_CLASS(L, SkPath);
1977 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07001978 REG_CLASS(L, SkPicture);
1979 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00001980 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00001981 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07001982 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08001983 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001984 REG_CLASS(L, SkTypeface);
reed@google.com74ce6f02013-05-22 15:13:18 +00001985}
zachr@google.com28c27c82013-06-20 17:15:05 +00001986
reed@google.com7bce9982013-06-20 17:40:21 +00001987extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00001988extern "C" int luaopen_skia(lua_State* L) {
1989 SkLua::Load(L);
1990 return 0;
1991}