blob: edd336afdf04c5867e6fe512cf67121932013083 [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"
reed22a517f2015-12-04 20:45:59 -080016#include "SkColorFilter.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000017#include "SkData.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"
robertphillips233bab92016-01-21 09:05:32 -080031#include "SkXfermode.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000032
33extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000034 #include "lua.h"
35 #include "lualib.h"
36 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000037}
38
reed@google.comfd345872013-05-22 20:53:42 +000039// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000040template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000041#define DEF_MTNAME(T) \
42 template <> const char* get_mtname<T>() { \
43 return #T "_LuaMetaTableName"; \
44 }
45
46DEF_MTNAME(SkCanvas)
reed22a517f2015-12-04 20:45:59 -080047DEF_MTNAME(SkColorFilter)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000048DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000049DEF_MTNAME(SkImage)
reed468b1812014-10-19 11:42:54 -070050DEF_MTNAME(SkImageFilter)
reed@google.comfd345872013-05-22 20:53:42 +000051DEF_MTNAME(SkMatrix)
52DEF_MTNAME(SkRRect)
53DEF_MTNAME(SkPath)
54DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000055DEF_MTNAME(SkPathEffect)
reed96affcd2014-10-13 12:38:04 -070056DEF_MTNAME(SkPicture)
57DEF_MTNAME(SkPictureRecorder)
reed@google.com5fdc9832013-07-24 15:47:52 +000058DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070059DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070060DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000061DEF_MTNAME(SkTypeface)
robertphillips233bab92016-01-21 09:05:32 -080062DEF_MTNAME(SkXfermode)
reed@google.com74ce6f02013-05-22 15:13:18 +000063
reed@google.com3597b732013-05-22 20:12:50 +000064template <typename T> T* push_new(lua_State* L) {
65 T* addr = (T*)lua_newuserdata(L, sizeof(T));
66 new (addr) T;
67 luaL_getmetatable(L, get_mtname<T>());
68 lua_setmetatable(L, -2);
69 return addr;
70}
reed@google.com74ce6f02013-05-22 15:13:18 +000071
72template <typename T> void push_obj(lua_State* L, const T& obj) {
73 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000074 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000075 lua_setmetatable(L, -2);
76}
77
reed9fbc3f32014-10-21 07:12:58 -070078template <typename T> T* push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000079 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000080 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000081 lua_setmetatable(L, -2);
reed9fbc3f32014-10-21 07:12:58 -070082 return ref;
reed@google.com74ce6f02013-05-22 15:13:18 +000083}
84
85template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000086 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000087}
88
89template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000090 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000091}
92
reed@google.com88c9ec92013-05-22 15:43:21 +000093static bool lua2bool(lua_State* L, int index) {
94 return !!lua_toboolean(L, index);
95}
96
reed@google.com74ce6f02013-05-22 15:13:18 +000097///////////////////////////////////////////////////////////////////////////////
98
reed@google.com3597b732013-05-22 20:12:50 +000099SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
100 fL = luaL_newstate();
101 luaL_openlibs(fL);
102 SkLua::Load(fL);
103}
104
105SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
106
107SkLua::~SkLua() {
108 if (fWeOwnL) {
109 if (fTermCode.size() > 0) {
110 lua_getglobal(fL, fTermCode.c_str());
111 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
112 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
113 }
114 }
115 lua_close(fL);
116 }
117}
118
119bool SkLua::runCode(const char code[]) {
120 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
121 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000122 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000123 return false;
124 }
125 return true;
126}
127
128bool SkLua::runCode(const void* code, size_t size) {
129 SkString str((const char*)code, size);
130 return this->runCode(str.c_str());
131}
132
133///////////////////////////////////////////////////////////////////////////////
134
135#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
136
reed@google.com29563872013-07-10 21:23:49 +0000137static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
138 if (pred) {
139 lua_pushboolean(L, true);
140 lua_setfield(L, -2, key);
141 }
142}
143
reed@google.com74ce6f02013-05-22 15:13:18 +0000144static void setfield_string(lua_State* L, const char key[], const char value[]) {
145 lua_pushstring(L, value);
146 lua_setfield(L, -2, key);
147}
148
149static void setfield_number(lua_State* L, const char key[], double value) {
150 lua_pushnumber(L, value);
151 lua_setfield(L, -2, key);
152}
153
humper@google.com2815c192013-07-10 22:42:30 +0000154static void setfield_boolean(lua_State* L, const char key[], bool value) {
155 lua_pushboolean(L, value);
156 lua_setfield(L, -2, key);
157}
158
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000159static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
160 setfield_number(L, key, SkScalarToLua(value));
161}
162
reed@google.com3597b732013-05-22 20:12:50 +0000163static void setfield_function(lua_State* L,
164 const char key[], lua_CFunction value) {
165 lua_pushcfunction(L, value);
166 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000167}
168
reed7a72c672014-11-07 10:23:55 -0800169static int lua2int_def(lua_State* L, int index, int defaultValue) {
170 if (lua_isnumber(L, index)) {
171 return (int)lua_tonumber(L, index);
172 } else {
173 return defaultValue;
174 }
175}
176
177static SkScalar lua2scalar(lua_State* L, int index) {
178 SkASSERT(lua_isnumber(L, index));
179 return SkLuaToScalar(lua_tonumber(L, index));
180}
181
182static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
183 if (lua_isnumber(L, index)) {
184 return SkLuaToScalar(lua_tonumber(L, index));
185 } else {
186 return defaultValue;
187 }
188}
189
190static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
191 SkASSERT(lua_istable(L, stackIndex));
192 lua_rawgeti(L, stackIndex, arrayIndex);
mtklein8aacf202014-12-18 13:29:54 -0800193
reed7a72c672014-11-07 10:23:55 -0800194 SkScalar value = lua2scalar(L, -1);
195 lua_pop(L, 1);
196 return value;
197}
198
199static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
200 for (int i = 0; i < count; ++i) {
201 dst[i] = getarray_scalar(L, stackIndex, i + 1);
202 }
203}
204
205static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
206 getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
207}
208
reed@google.come3823fd2013-05-30 18:55:14 +0000209static void setarray_number(lua_State* L, int index, double value) {
210 lua_pushnumber(L, value);
211 lua_rawseti(L, -2, index);
212}
213
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000214static void setarray_scalar(lua_State* L, int index, SkScalar value) {
215 setarray_number(L, index, SkScalarToLua(value));
216}
217
reed@google.com74ce6f02013-05-22 15:13:18 +0000218void SkLua::pushBool(bool value, const char key[]) {
219 lua_pushboolean(fL, value);
220 CHECK_SETFIELD(key);
221}
222
223void SkLua::pushString(const char str[], const char key[]) {
224 lua_pushstring(fL, str);
225 CHECK_SETFIELD(key);
226}
227
reed@google.come3823fd2013-05-30 18:55:14 +0000228void SkLua::pushString(const char str[], size_t length, const char key[]) {
229 // TODO: how to do this w/o making a copy?
230 SkString s(str, length);
231 lua_pushstring(fL, s.c_str());
232 CHECK_SETFIELD(key);
233}
234
reed@google.com74ce6f02013-05-22 15:13:18 +0000235void SkLua::pushString(const SkString& str, const char key[]) {
236 lua_pushstring(fL, str.c_str());
237 CHECK_SETFIELD(key);
238}
239
240void SkLua::pushColor(SkColor color, const char key[]) {
241 lua_newtable(fL);
242 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
243 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
244 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
245 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
246 CHECK_SETFIELD(key);
247}
248
reed@google.come3823fd2013-05-30 18:55:14 +0000249void SkLua::pushU32(uint32_t value, const char key[]) {
250 lua_pushnumber(fL, (double)value);
251 CHECK_SETFIELD(key);
252}
253
reed@google.com74ce6f02013-05-22 15:13:18 +0000254void SkLua::pushScalar(SkScalar value, const char key[]) {
255 lua_pushnumber(fL, SkScalarToLua(value));
256 CHECK_SETFIELD(key);
257}
258
reed@google.come3823fd2013-05-30 18:55:14 +0000259void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
260 lua_newtable(fL);
261 for (int i = 0; i < count; ++i) {
262 // make it base-1 to match lua convention
263 setarray_number(fL, i + 1, (double)array[i]);
264 }
265 CHECK_SETFIELD(key);
266}
267
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000268void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
269 lua_newtable(fL);
270 for (int i = 0; i < count; ++i) {
271 // make it base-1 to match lua convention
272 lua_newtable(fL);
273 this->pushScalar(array[i].fX, "x");
274 this->pushScalar(array[i].fY, "y");
275 lua_rawseti(fL, -2, i + 1);
276 }
277 CHECK_SETFIELD(key);
278}
279
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000280void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
281 lua_newtable(fL);
282 for (int i = 0; i < count; ++i) {
283 // make it base-1 to match lua convention
284 setarray_scalar(fL, i + 1, array[i]);
285 }
286 CHECK_SETFIELD(key);
287}
288
reed@google.com74ce6f02013-05-22 15:13:18 +0000289void SkLua::pushRect(const SkRect& r, const char key[]) {
290 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000291 setfield_scalar(fL, "left", r.fLeft);
292 setfield_scalar(fL, "top", r.fTop);
293 setfield_scalar(fL, "right", r.fRight);
294 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000295 CHECK_SETFIELD(key);
296}
297
298void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
299 push_obj(fL, rr);
300 CHECK_SETFIELD(key);
301}
302
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000303void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
304 lua_newtable(fL);
305 setfield_scalar(fL, "phase", info.fPhase);
306 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
307 CHECK_SETFIELD(key);
308}
309
310
reed@google.com74ce6f02013-05-22 15:13:18 +0000311void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
312 push_obj(fL, matrix);
313 CHECK_SETFIELD(key);
314}
315
316void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
317 push_obj(fL, paint);
318 CHECK_SETFIELD(key);
319}
320
321void SkLua::pushPath(const SkPath& path, const char key[]) {
322 push_obj(fL, path);
323 CHECK_SETFIELD(key);
324}
325
326void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
327 push_ref(fL, canvas);
328 CHECK_SETFIELD(key);
329}
330
fmalitab7425172014-08-26 07:56:44 -0700331void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
332 push_ref(fL, const_cast<SkTextBlob*>(blob));
333 CHECK_SETFIELD(key);
334}
335
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000336static const char* element_type(SkClipStack::Element::Type type) {
337 switch (type) {
338 case SkClipStack::Element::kEmpty_Type:
339 return "empty";
340 case SkClipStack::Element::kRect_Type:
341 return "rect";
342 case SkClipStack::Element::kRRect_Type:
343 return "rrect";
344 case SkClipStack::Element::kPath_Type:
345 return "path";
346 }
347 return "unknown";
348}
349
350static const char* region_op(SkRegion::Op op) {
351 switch (op) {
352 case SkRegion::kDifference_Op:
353 return "difference";
354 case SkRegion::kIntersect_Op:
355 return "intersect";
356 case SkRegion::kUnion_Op:
357 return "union";
358 case SkRegion::kXOR_Op:
359 return "xor";
360 case SkRegion::kReverseDifference_Op:
361 return "reverse-difference";
362 case SkRegion::kReplace_Op:
363 return "replace";
364 }
365 return "unknown";
366}
367
368void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
369 lua_newtable(fL);
370 SkClipStack::B2TIter iter(stack);
371 const SkClipStack::Element* element;
372 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700373 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000374 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000375 lua_rawseti(fL, -2, ++i);
376 }
377 CHECK_SETFIELD(key);
378}
379
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000380void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
381 lua_newtable(fL);
382 SkClipStack::Element::Type type = element.getType();
383 this->pushString(element_type(type), "type");
384 switch (type) {
385 case SkClipStack::Element::kEmpty_Type:
386 break;
387 case SkClipStack::Element::kRect_Type:
388 this->pushRect(element.getRect(), "rect");
389 break;
390 case SkClipStack::Element::kRRect_Type:
391 this->pushRRect(element.getRRect(), "rrect");
392 break;
393 case SkClipStack::Element::kPath_Type:
394 this->pushPath(element.getPath(), "path");
395 break;
396 }
397 this->pushString(region_op(element.getOp()), "op");
398 this->pushBool(element.isAA(), "aa");
399 CHECK_SETFIELD(key);
400}
401
402
reed@google.com74ce6f02013-05-22 15:13:18 +0000403///////////////////////////////////////////////////////////////////////////////
404///////////////////////////////////////////////////////////////////////////////
405
reed@google.com74ce6f02013-05-22 15:13:18 +0000406static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
407 SkASSERT(lua_istable(L, index));
408 lua_pushstring(L, key);
409 lua_gettable(L, index);
mtklein8aacf202014-12-18 13:29:54 -0800410
reed@google.com74ce6f02013-05-22 15:13:18 +0000411 SkScalar value = lua2scalar(L, -1);
412 lua_pop(L, 1);
413 return value;
414}
415
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000416static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
417 SkASSERT(lua_istable(L, index));
418 lua_pushstring(L, key);
419 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000420
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000421 SkScalar value;
422 if (lua_isnil(L, -1)) {
423 value = def;
424 } else {
425 value = lua2scalar(L, -1);
426 }
427 lua_pop(L, 1);
428 return value;
429}
430
reed468b1812014-10-19 11:42:54 -0700431static SkScalar byte2unit(U8CPU byte) {
432 return byte / 255.0f;
433}
434
reed@google.com74ce6f02013-05-22 15:13:18 +0000435static U8CPU unit2byte(SkScalar x) {
436 if (x <= 0) {
437 return 0;
438 } else if (x >= 1) {
439 return 255;
440 } else {
441 return SkScalarRoundToInt(x * 255);
442 }
443}
444
445static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700446 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
447 unit2byte(getfield_scalar_default(L, index, "r", 0)),
448 unit2byte(getfield_scalar_default(L, index, "g", 0)),
449 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000450}
451
452static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000453 rect->set(getfield_scalar_default(L, index, "left", 0),
454 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000455 getfield_scalar(L, index, "right"),
456 getfield_scalar(L, index, "bottom"));
457 return rect;
458}
459
reedf355df52014-10-12 12:18:40 -0700460static int lcanvas_clear(lua_State* L) {
461 get_ref<SkCanvas>(L, 1)->clear(0);
462 return 0;
463}
464
reed@google.com74ce6f02013-05-22 15:13:18 +0000465static int lcanvas_drawColor(lua_State* L) {
466 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
467 return 0;
468}
469
reed9fbc3f32014-10-21 07:12:58 -0700470static int lcanvas_drawPaint(lua_State* L) {
471 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
472 return 0;
473}
474
reed@google.com74ce6f02013-05-22 15:13:18 +0000475static int lcanvas_drawRect(lua_State* L) {
476 SkRect rect;
reed7a72c672014-11-07 10:23:55 -0800477 lua2rect(L, 2, &rect);
478 const SkPaint* paint = get_obj<SkPaint>(L, 3);
479 get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
reed@google.com74ce6f02013-05-22 15:13:18 +0000480 return 0;
481}
482
483static int lcanvas_drawOval(lua_State* L) {
484 SkRect rect;
485 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
486 *get_obj<SkPaint>(L, 3));
487 return 0;
488}
489
490static int lcanvas_drawCircle(lua_State* L) {
491 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
492 lua2scalar(L, 3),
493 lua2scalar(L, 4),
494 *get_obj<SkPaint>(L, 5));
495 return 0;
496}
497
reed485557f2014-10-12 10:36:47 -0700498static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
499 if (lua_isnumber(L, index)) {
500 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
501 return paint;
reedf355df52014-10-12 12:18:40 -0700502 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700503 const SkPaint* ptr = get_obj<SkPaint>(L, index);
504 if (ptr) {
505 *paint = *ptr;
506 return paint;
507 }
508 }
halcanary96fcdcc2015-08-27 07:41:13 -0700509 return nullptr;
reed485557f2014-10-12 10:36:47 -0700510}
511
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000512static int lcanvas_drawImage(lua_State* L) {
513 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
514 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700515 if (nullptr == image) {
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000516 return 0;
517 }
518 SkScalar x = lua2scalar(L, 3);
519 SkScalar y = lua2scalar(L, 4);
520
521 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700522 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000523 return 0;
524}
525
reedba5fb932014-10-10 15:28:19 -0700526static int lcanvas_drawImageRect(lua_State* L) {
527 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
528 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700529 if (nullptr == image) {
reedba5fb932014-10-10 15:28:19 -0700530 return 0;
531 }
532
533 SkRect srcR, dstR;
halcanary96fcdcc2015-08-27 07:41:13 -0700534 SkRect* srcRPtr = nullptr;
reedba5fb932014-10-10 15:28:19 -0700535 if (!lua_isnil(L, 3)) {
536 srcRPtr = lua2rect(L, 3, &srcR);
537 }
538 lua2rect(L, 4, &dstR);
mtklein8aacf202014-12-18 13:29:54 -0800539
reedba5fb932014-10-10 15:28:19 -0700540 SkPaint paint;
reede47829b2015-08-06 10:02:53 -0700541 canvas->legacy_drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700542 return 0;
543}
544
reed7a72c672014-11-07 10:23:55 -0800545static int lcanvas_drawPatch(lua_State* L) {
546 SkPoint cubics[12];
547 SkColor colorStorage[4];
548 SkPoint texStorage[4];
549
halcanary96fcdcc2015-08-27 07:41:13 -0700550 const SkColor* colors = nullptr;
551 const SkPoint* texs = nullptr;
reed7a72c672014-11-07 10:23:55 -0800552
553 getarray_points(L, 2, cubics, 12);
554
555 colorStorage[0] = SK_ColorRED;
556 colorStorage[1] = SK_ColorGREEN;
557 colorStorage[2] = SK_ColorBLUE;
558 colorStorage[3] = SK_ColorGRAY;
559
560 if (lua_isnil(L, 4)) {
561 colors = colorStorage;
562 } else {
563 getarray_points(L, 4, texStorage, 4);
564 texs = texStorage;
565 }
566
halcanary96fcdcc2015-08-27 07:41:13 -0700567 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, nullptr, *get_obj<SkPaint>(L, 5));
reed7a72c672014-11-07 10:23:55 -0800568 return 0;
569}
570
reed@google.comfd345872013-05-22 20:53:42 +0000571static int lcanvas_drawPath(lua_State* L) {
572 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
573 *get_obj<SkPaint>(L, 3));
574 return 0;
575}
576
reed96affcd2014-10-13 12:38:04 -0700577// drawPicture(pic, x, y, paint)
578static int lcanvas_drawPicture(lua_State* L) {
579 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
580 SkPicture* picture = get_ref<SkPicture>(L, 2);
581 SkScalar x = lua2scalar_def(L, 3, 0);
582 SkScalar y = lua2scalar_def(L, 4, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700583 SkMatrix matrix, *matrixPtr = nullptr;
reed96affcd2014-10-13 12:38:04 -0700584 if (x || y) {
585 matrix.setTranslate(x, y);
586 matrixPtr = &matrix;
587 }
588 SkPaint paint;
589 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
590 return 0;
591}
592
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000593static int lcanvas_drawText(lua_State* L) {
594 if (lua_gettop(L) < 5) {
595 return 0;
596 }
597
598 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
599 size_t len;
600 const char* text = lua_tolstring(L, 2, &len);
601 get_ref<SkCanvas>(L, 1)->drawText(text, len,
602 lua2scalar(L, 3), lua2scalar(L, 4),
603 *get_obj<SkPaint>(L, 5));
604 }
605 return 0;
606}
607
reed1b6ab442014-11-03 19:55:41 -0800608static int lcanvas_drawTextBlob(lua_State* L) {
609 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
610 SkScalar x = lua2scalar(L, 3);
611 SkScalar y = lua2scalar(L, 4);
612 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
613 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
614 return 0;
615}
616
reed@google.com74ce6f02013-05-22 15:13:18 +0000617static int lcanvas_getSaveCount(lua_State* L) {
618 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
619 return 1;
620}
621
622static int lcanvas_getTotalMatrix(lua_State* L) {
623 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
624 return 1;
625}
626
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000627static int lcanvas_getClipStack(lua_State* L) {
628 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
629 return 1;
630}
631
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000632int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
633#if SK_SUPPORT_GPU
634 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
635 SkISize layerSize = canvas->getTopLayerSize();
636 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
637 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
638 layerSize.fWidth, layerSize.fHeight);
639
640 GrReducedClip::ElementList elements;
641 GrReducedClip::InitialState initialState;
642 int32_t genID;
643 SkIRect resultBounds;
644
645 const SkClipStack& stack = *canvas->getClipStack();
646
647 GrReducedClip::ReduceClipStack(stack,
648 queryBounds,
649 &elements,
650 &genID,
651 &initialState,
652 &resultBounds,
halcanary96fcdcc2015-08-27 07:41:13 -0700653 nullptr);
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000654
655 GrReducedClip::ElementList::Iter iter(elements);
656 int i = 0;
657 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700658 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000659 SkLua(L).pushClipStackElement(*iter.get());
660 iter.next();
661 lua_rawseti(L, -2, ++i);
662 }
663 // Currently this only returns the element list to lua, not the initial state or result bounds.
664 // It could return these as additional items on the lua stack.
665 return 1;
666#else
667 return 0;
668#endif
669}
670
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000671static int lcanvas_save(lua_State* L) {
672 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
673 return 1;
674}
675
reed86217d82014-10-25 20:44:40 -0700676static int lcanvas_saveLayer(lua_State* L) {
677 SkPaint paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700678 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(nullptr, lua2OptionalPaint(L, 2, &paint)));
reed86217d82014-10-25 20:44:40 -0700679 return 1;
680}
681
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000682static int lcanvas_restore(lua_State* L) {
683 get_ref<SkCanvas>(L, 1)->restore();
684 return 0;
685}
686
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000687static int lcanvas_scale(lua_State* L) {
688 SkScalar sx = lua2scalar_def(L, 2, 1);
689 SkScalar sy = lua2scalar_def(L, 3, sx);
690 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
691 return 0;
692}
693
reed@google.com3597b732013-05-22 20:12:50 +0000694static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000695 SkScalar tx = lua2scalar_def(L, 2, 0);
696 SkScalar ty = lua2scalar_def(L, 3, 0);
697 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
698 return 0;
699}
700
701static int lcanvas_rotate(lua_State* L) {
702 SkScalar degrees = lua2scalar_def(L, 2, 0);
703 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000704 return 0;
705}
706
reedbdc49ae2014-10-14 09:34:52 -0700707static int lcanvas_concat(lua_State* L) {
708 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
709 return 0;
710}
711
reed485557f2014-10-12 10:36:47 -0700712static int lcanvas_newSurface(lua_State* L) {
713 int width = lua2int_def(L, 2, 0);
reed7a72c672014-11-07 10:23:55 -0800714 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -0700715 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
716 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700717 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -0700718 lua_pushnil(L);
719 } else {
reed9fbc3f32014-10-21 07:12:58 -0700720 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -0700721 }
722 return 1;
723}
724
reed@google.com74ce6f02013-05-22 15:13:18 +0000725static int lcanvas_gc(lua_State* L) {
726 get_ref<SkCanvas>(L, 1)->unref();
727 return 0;
728}
729
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000730const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700731 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000732 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700733 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000734 { "drawRect", lcanvas_drawRect },
735 { "drawOval", lcanvas_drawOval },
736 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000737 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700738 { "drawImageRect", lcanvas_drawImageRect },
reed7a72c672014-11-07 10:23:55 -0800739 { "drawPatch", lcanvas_drawPatch },
reed@google.comfd345872013-05-22 20:53:42 +0000740 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700741 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000742 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800743 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000744 { "getSaveCount", lcanvas_getSaveCount },
745 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000746 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000747#if SK_SUPPORT_GPU
748 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
749#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000750 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700751 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000752 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000753 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000754 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000755 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700756 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700757
758 { "newSurface", lcanvas_newSurface },
759
reed@google.com74ce6f02013-05-22 15:13:18 +0000760 { "__gc", lcanvas_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700761 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +0000762};
763
764///////////////////////////////////////////////////////////////////////////////
765
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000766static int ldocument_beginPage(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -0700767 const SkRect* contentPtr = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000768 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
769 lua2scalar(L, 3),
770 contentPtr));
771 return 1;
772}
773
774static int ldocument_endPage(lua_State* L) {
775 get_ref<SkDocument>(L, 1)->endPage();
776 return 0;
777}
778
779static int ldocument_close(lua_State* L) {
780 get_ref<SkDocument>(L, 1)->close();
781 return 0;
782}
783
784static int ldocument_gc(lua_State* L) {
785 get_ref<SkDocument>(L, 1)->unref();
786 return 0;
787}
788
789static const struct luaL_Reg gSkDocument_Methods[] = {
790 { "beginPage", ldocument_beginPage },
791 { "endPage", ldocument_endPage },
792 { "close", ldocument_close },
793 { "__gc", ldocument_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700794 { nullptr, nullptr }
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000795};
796
797///////////////////////////////////////////////////////////////////////////////
798
reed@google.com74ce6f02013-05-22 15:13:18 +0000799static int lpaint_isAntiAlias(lua_State* L) {
800 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
801 return 1;
802}
803
804static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000805 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000806 return 0;
807}
808
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000809static int lpaint_isDither(lua_State* L) {
810 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
811 return 1;
812}
813
reedbb8a0ab2014-11-03 22:32:07 -0800814static int lpaint_setDither(lua_State* L) {
815 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
816 return 0;
817}
818
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000819static int lpaint_isUnderlineText(lua_State* L) {
820 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
821 return 1;
822}
823
824static int lpaint_isStrikeThruText(lua_State* L) {
825 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
826 return 1;
827}
828
829static int lpaint_isFakeBoldText(lua_State* L) {
830 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
831 return 1;
832}
833
834static int lpaint_isLinearText(lua_State* L) {
835 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
836 return 1;
837}
838
839static int lpaint_isSubpixelText(lua_State* L) {
840 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
841 return 1;
842}
843
reed09a1d672014-10-11 13:13:11 -0700844static int lpaint_setSubpixelText(lua_State* L) {
845 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
846 return 1;
847}
848
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000849static int lpaint_isDevKernText(lua_State* L) {
850 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
851 return 1;
852}
853
854static int lpaint_isLCDRenderText(lua_State* L) {
855 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
856 return 1;
857}
858
reed36c9c112014-11-04 10:58:42 -0800859static int lpaint_setLCDRenderText(lua_State* L) {
860 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
861 return 1;
862}
863
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000864static int lpaint_isEmbeddedBitmapText(lua_State* L) {
865 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
866 return 1;
867}
868
869static int lpaint_isAutohinted(lua_State* L) {
870 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
871 return 1;
872}
873
874static int lpaint_isVerticalText(lua_State* L) {
875 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
876 return 1;
877}
878
reed468b1812014-10-19 11:42:54 -0700879static int lpaint_getAlpha(lua_State* L) {
880 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
881 return 1;
882}
883
884static int lpaint_setAlpha(lua_State* L) {
885 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
886 return 0;
887}
888
reed@google.com74ce6f02013-05-22 15:13:18 +0000889static int lpaint_getColor(lua_State* L) {
890 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
891 return 1;
892}
893
894static int lpaint_setColor(lua_State* L) {
895 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
896 return 0;
897}
898
reed@google.come3823fd2013-05-30 18:55:14 +0000899static int lpaint_getTextSize(lua_State* L) {
900 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
901 return 1;
902}
903
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000904static int lpaint_getTextScaleX(lua_State* L) {
905 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
906 return 1;
907}
908
909static int lpaint_getTextSkewX(lua_State* L) {
910 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
911 return 1;
912}
913
reed@google.come3823fd2013-05-30 18:55:14 +0000914static int lpaint_setTextSize(lua_State* L) {
915 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
916 return 0;
917}
918
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000919static int lpaint_getTypeface(lua_State* L) {
920 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
921 return 1;
922}
923
924static int lpaint_setTypeface(lua_State* L) {
925 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
926 return 0;
927}
928
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000929static int lpaint_getHinting(lua_State* L) {
930 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
931 return 1;
932}
933
reed93a12152015-03-16 10:08:34 -0700934static int lpaint_getFilterQuality(lua_State* L) {
935 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality());
reed7a72c672014-11-07 10:23:55 -0800936 return 1;
937}
938
reed93a12152015-03-16 10:08:34 -0700939static int lpaint_setFilterQuality(lua_State* L) {
reed7a72c672014-11-07 10:23:55 -0800940 int level = lua2int_def(L, 2, -1);
941 if (level >= 0 && level <= 3) {
reed93a12152015-03-16 10:08:34 -0700942 get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level);
reed7a72c672014-11-07 10:23:55 -0800943 }
944 return 0;
945}
946
reed@google.come3823fd2013-05-30 18:55:14 +0000947static int lpaint_getFontID(lua_State* L) {
948 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
949 SkLua(L).pushU32(SkTypeface::UniqueID(face));
950 return 1;
951}
952
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000953static const struct {
954 const char* fLabel;
955 SkPaint::Align fAlign;
956} gAlignRec[] = {
957 { "left", SkPaint::kLeft_Align },
958 { "center", SkPaint::kCenter_Align },
959 { "right", SkPaint::kRight_Align },
960};
961
962static int lpaint_getTextAlign(lua_State* L) {
963 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
964 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
965 if (gAlignRec[i].fAlign == align) {
966 lua_pushstring(L, gAlignRec[i].fLabel);
967 return 1;
968 }
969 }
970 return 0;
971}
972
973static int lpaint_setTextAlign(lua_State* L) {
974 if (lua_isstring(L, 2)) {
975 size_t len;
976 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000977
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000978 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
979 if (!strcmp(gAlignRec[i].fLabel, label)) {
980 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
981 break;
982 }
983 }
984 }
985 return 0;
986}
987
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000988static int lpaint_getStroke(lua_State* L) {
989 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
990 return 1;
991}
992
993static int lpaint_setStroke(lua_State* L) {
994 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000995
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000996 if (lua_toboolean(L, 2)) {
997 style = SkPaint::kStroke_Style;
998 } else {
999 style = SkPaint::kFill_Style;
1000 }
1001 get_obj<SkPaint>(L, 1)->setStyle(style);
1002 return 0;
1003}
1004
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001005static int lpaint_getStrokeCap(lua_State* L) {
1006 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
1007 return 1;
1008}
1009
1010static int lpaint_getStrokeJoin(lua_State* L) {
1011 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
1012 return 1;
1013}
1014
1015static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +00001016 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001017 return 1;
1018}
1019
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001020static int lpaint_getStrokeWidth(lua_State* L) {
1021 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
1022 return 1;
1023}
1024
1025static int lpaint_setStrokeWidth(lua_State* L) {
1026 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
1027 return 0;
1028}
1029
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001030static int lpaint_getStrokeMiter(lua_State* L) {
1031 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
1032 return 1;
1033}
1034
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001035static int lpaint_measureText(lua_State* L) {
1036 if (lua_isstring(L, 2)) {
1037 size_t len;
1038 const char* text = lua_tolstring(L, 2, &len);
1039 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
1040 return 1;
1041 }
1042 return 0;
1043}
1044
1045struct FontMetrics {
1046 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
1047 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
1048 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
1049 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
1050 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
1051 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
1052 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
1053 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
1054 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
1055};
1056
1057static int lpaint_getFontMetrics(lua_State* L) {
1058 SkPaint::FontMetrics fm;
1059 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +00001060
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001061 lua_newtable(L);
1062 setfield_scalar(L, "top", fm.fTop);
1063 setfield_scalar(L, "ascent", fm.fAscent);
1064 setfield_scalar(L, "descent", fm.fDescent);
1065 setfield_scalar(L, "bottom", fm.fBottom);
1066 setfield_scalar(L, "leading", fm.fLeading);
1067 SkLua(L).pushScalar(height);
1068 return 2;
1069}
1070
reed@google.com29563872013-07-10 21:23:49 +00001071static int lpaint_getEffects(lua_State* L) {
1072 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001073
reed@google.com29563872013-07-10 21:23:49 +00001074 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -07001075 setfield_bool_if(L, "looper", !!paint->getLooper());
1076 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
1077 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
1078 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
1079 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +00001080 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1081 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -07001082 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +00001083 return 1;
1084}
1085
robertphillips233bab92016-01-21 09:05:32 -08001086static int lpaint_getXfermode(lua_State* L) {
1087 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1088 SkXfermode* xfermode = paint->getXfermode();
1089 if (xfermode) {
1090 push_ref(L, xfermode);
1091 return 1;
1092 }
1093 return 0;
1094}
1095
1096static int lpaint_setXfermode(lua_State* L) {
1097 SkPaint* paint = get_obj<SkPaint>(L, 1);
1098 paint->setXfermode(get_ref<SkXfermode>(L, 2));
1099 return 0;
1100}
1101
reed22a517f2015-12-04 20:45:59 -08001102static int lpaint_getColorFilter(lua_State* L) {
1103 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1104 SkColorFilter* cf = paint->getColorFilter();
1105 if (cf) {
1106 push_ref(L, cf);
1107 return 1;
1108 }
1109 return 0;
1110}
1111
1112static int lpaint_setColorFilter(lua_State* L) {
1113 SkPaint* paint = get_obj<SkPaint>(L, 1);
1114 paint->setColorFilter(get_ref<SkColorFilter>(L, 2));
1115 return 0;
1116}
1117
reed468b1812014-10-19 11:42:54 -07001118static int lpaint_getImageFilter(lua_State* L) {
1119 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1120 SkImageFilter* imf = paint->getImageFilter();
1121 if (imf) {
1122 push_ref(L, imf);
1123 return 1;
1124 }
1125 return 0;
1126}
1127
1128static int lpaint_setImageFilter(lua_State* L) {
1129 SkPaint* paint = get_obj<SkPaint>(L, 1);
1130 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1131 return 0;
1132}
1133
reed@google.com5fdc9832013-07-24 15:47:52 +00001134static int lpaint_getShader(lua_State* L) {
1135 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1136 SkShader* shader = paint->getShader();
1137 if (shader) {
1138 push_ref(L, shader);
1139 return 1;
1140 }
1141 return 0;
1142}
1143
reed9fbc3f32014-10-21 07:12:58 -07001144static int lpaint_setShader(lua_State* L) {
1145 SkPaint* paint = get_obj<SkPaint>(L, 1);
1146 paint->setShader(get_ref<SkShader>(L, 2));
1147 return 0;
1148}
1149
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001150static int lpaint_getPathEffect(lua_State* L) {
1151 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1152 SkPathEffect* pe = paint->getPathEffect();
1153 if (pe) {
1154 push_ref(L, pe);
1155 return 1;
1156 }
1157 return 0;
1158}
1159
reed@google.com74ce6f02013-05-22 15:13:18 +00001160static int lpaint_gc(lua_State* L) {
1161 get_obj<SkPaint>(L, 1)->~SkPaint();
1162 return 0;
1163}
1164
1165static const struct luaL_Reg gSkPaint_Methods[] = {
1166 { "isAntiAlias", lpaint_isAntiAlias },
1167 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001168 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001169 { "setDither", lpaint_setDither },
reed93a12152015-03-16 10:08:34 -07001170 { "getFilterQuality", lpaint_getFilterQuality },
1171 { "setFilterQuality", lpaint_setFilterQuality },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001172 { "isUnderlineText", lpaint_isUnderlineText },
1173 { "isStrikeThruText", lpaint_isStrikeThruText },
1174 { "isFakeBoldText", lpaint_isFakeBoldText },
1175 { "isLinearText", lpaint_isLinearText },
1176 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001177 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001178 { "isDevKernText", lpaint_isDevKernText },
1179 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001180 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001181 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1182 { "isAutohinted", lpaint_isAutohinted },
1183 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001184 { "getAlpha", lpaint_getAlpha },
1185 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001186 { "getColor", lpaint_getColor },
1187 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001188 { "getTextSize", lpaint_getTextSize },
1189 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001190 { "getTextScaleX", lpaint_getTextScaleX },
1191 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001192 { "getTypeface", lpaint_getTypeface },
1193 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001194 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001195 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001196 { "getTextAlign", lpaint_getTextAlign },
1197 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001198 { "getStroke", lpaint_getStroke },
1199 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001200 { "getStrokeCap", lpaint_getStrokeCap },
1201 { "getStrokeJoin", lpaint_getStrokeJoin },
1202 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001203 { "getStrokeWidth", lpaint_getStrokeWidth },
1204 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001205 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001206 { "measureText", lpaint_measureText },
1207 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001208 { "getEffects", lpaint_getEffects },
reed22a517f2015-12-04 20:45:59 -08001209 { "getColorFilter", lpaint_getColorFilter },
1210 { "setColorFilter", lpaint_setColorFilter },
reed468b1812014-10-19 11:42:54 -07001211 { "getImageFilter", lpaint_getImageFilter },
1212 { "setImageFilter", lpaint_setImageFilter },
robertphillips233bab92016-01-21 09:05:32 -08001213 { "getXfermode", lpaint_getXfermode },
1214 { "setXfermode", lpaint_setXfermode },
reed@google.com5fdc9832013-07-24 15:47:52 +00001215 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001216 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001217 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001218 { "__gc", lpaint_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001219 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001220};
1221
1222///////////////////////////////////////////////////////////////////////////////
1223
reed@google.com5fdc9832013-07-24 15:47:52 +00001224static const char* mode2string(SkShader::TileMode mode) {
1225 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1226 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1227 return gNames[mode];
1228}
1229
1230static const char* gradtype2string(SkShader::GradientType t) {
1231 static const char* gNames[] = {
1232 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1233 };
1234 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1235 return gNames[t];
1236}
1237
1238static int lshader_isOpaque(lua_State* L) {
1239 SkShader* shader = get_ref<SkShader>(L, 1);
1240 return shader && shader->isOpaque();
1241}
1242
reedf5822822015-08-19 11:46:38 -07001243static int lshader_isABitmap(lua_State* L) {
reed@google.com5fdc9832013-07-24 15:47:52 +00001244 SkShader* shader = get_ref<SkShader>(L, 1);
1245 if (shader) {
1246 SkBitmap bm;
1247 SkMatrix matrix;
1248 SkShader::TileMode modes[2];
reedf5822822015-08-19 11:46:38 -07001249 if (shader->isABitmap(&bm, &matrix, modes)) {
1250 lua_newtable(L);
1251 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1252 setfield_number(L, "width", bm.width());
1253 setfield_number(L, "height", bm.height());
1254 setfield_string(L, "tileX", mode2string(modes[0]));
1255 setfield_string(L, "tileY", mode2string(modes[1]));
1256 return 1;
reed@google.com5fdc9832013-07-24 15:47:52 +00001257 }
1258 }
1259 return 0;
1260}
1261
1262static int lshader_asAGradient(lua_State* L) {
1263 SkShader* shader = get_ref<SkShader>(L, 1);
1264 if (shader) {
1265 SkShader::GradientInfo info;
1266 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001267
1268 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001269 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001270
1271 info.fColorCount = 3;
1272 info.fColors = &colors[0];
1273 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001274
reed@google.com5fdc9832013-07-24 15:47:52 +00001275 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001276
reed@google.com5fdc9832013-07-24 15:47:52 +00001277 if (SkShader::kNone_GradientType != t) {
1278 lua_newtable(L);
1279 setfield_string(L, "type", gradtype2string(t));
1280 setfield_number(L, "colorCount", info.fColorCount);
1281 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001282
1283 if (info.fColorCount == 3){
1284 setfield_number(L, "midPos", pos[1]);
1285 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001286
reed@google.com5fdc9832013-07-24 15:47:52 +00001287 return 1;
1288 }
1289 }
1290 return 0;
1291}
1292
1293static int lshader_gc(lua_State* L) {
1294 get_ref<SkShader>(L, 1)->unref();
1295 return 0;
1296}
1297
1298static const struct luaL_Reg gSkShader_Methods[] = {
1299 { "isOpaque", lshader_isOpaque },
reedf5822822015-08-19 11:46:38 -07001300 { "isABitmap", lshader_isABitmap },
reed@google.com5fdc9832013-07-24 15:47:52 +00001301 { "asAGradient", lshader_asAGradient },
1302 { "__gc", lshader_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001303 { nullptr, nullptr }
reed@google.com5fdc9832013-07-24 15:47:52 +00001304};
1305
1306///////////////////////////////////////////////////////////////////////////////
1307
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001308static int lpatheffect_asADash(lua_State* L) {
1309 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1310 if (pe) {
1311 SkPathEffect::DashInfo info;
1312 SkPathEffect::DashType dashType = pe->asADash(&info);
1313 if (SkPathEffect::kDash_DashType == dashType) {
1314 SkAutoTArray<SkScalar> intervals(info.fCount);
1315 info.fIntervals = intervals.get();
1316 pe->asADash(&info);
1317 SkLua(L).pushDash(info);
1318 return 1;
1319 }
1320 }
1321 return 0;
1322}
1323
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001324static int lpatheffect_gc(lua_State* L) {
1325 get_ref<SkPathEffect>(L, 1)->unref();
1326 return 0;
1327}
1328
1329static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001330 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001331 { "__gc", lpatheffect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001332 { nullptr, nullptr }
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001333};
1334
1335///////////////////////////////////////////////////////////////////////////////
1336
robertphillips233bab92016-01-21 09:05:32 -08001337static int lpxfermode_getTypeName(lua_State* L) {
1338 lua_pushstring(L, get_ref<SkXfermode>(L, 1)->getTypeName());
1339 return 1;
1340}
1341
1342static int lpxfermode_gc(lua_State* L) {
1343 get_ref<SkXfermode>(L, 1)->unref();
1344 return 0;
1345}
1346
1347static const struct luaL_Reg gSkXfermode_Methods[] = {
1348 { "getTypeName", lpxfermode_getTypeName },
1349 { "__gc", lpxfermode_gc },
1350 { nullptr, nullptr }
1351};
1352
1353///////////////////////////////////////////////////////////////////////////////
1354
reed22a517f2015-12-04 20:45:59 -08001355static int lpcolorfilter_gc(lua_State* L) {
1356 get_ref<SkColorFilter>(L, 1)->unref();
1357 return 0;
1358}
1359
1360static const struct luaL_Reg gSkColorFilter_Methods[] = {
1361 { "__gc", lpcolorfilter_gc },
1362 { nullptr, nullptr }
1363};
1364
1365///////////////////////////////////////////////////////////////////////////////
1366
reed468b1812014-10-19 11:42:54 -07001367static int lpimagefilter_gc(lua_State* L) {
1368 get_ref<SkImageFilter>(L, 1)->unref();
1369 return 0;
1370}
1371
1372static const struct luaL_Reg gSkImageFilter_Methods[] = {
1373 { "__gc", lpimagefilter_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001374 { nullptr, nullptr }
reed468b1812014-10-19 11:42:54 -07001375};
1376
1377///////////////////////////////////////////////////////////////////////////////
1378
humper@google.com2815c192013-07-10 22:42:30 +00001379static int lmatrix_getType(lua_State* L) {
1380 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001381
humper@google.com2815c192013-07-10 22:42:30 +00001382 lua_newtable(L);
1383 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1384 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1385 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1386 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1387 return 1;
1388}
1389
humper@google.com0f48ee02013-07-26 15:23:43 +00001390static int lmatrix_getScaleX(lua_State* L) {
1391 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1392 return 1;
1393}
1394
1395static int lmatrix_getScaleY(lua_State* L) {
1396 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1397 return 1;
1398}
1399
1400static int lmatrix_getTranslateX(lua_State* L) {
1401 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1402 return 1;
1403}
1404
1405static int lmatrix_getTranslateY(lua_State* L) {
1406 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1407 return 1;
1408}
1409
reed7a72c672014-11-07 10:23:55 -08001410static int lmatrix_invert(lua_State* L) {
1411 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1412 return 1;
1413}
1414
1415static int lmatrix_mapXY(lua_State* L) {
1416 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1417 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1418 lua_pushnumber(L, pt.x());
1419 lua_pushnumber(L, pt.y());
1420 return 2;
1421}
1422
reedbdc49ae2014-10-14 09:34:52 -07001423static int lmatrix_setRectToRect(lua_State* L) {
1424 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1425 SkRect srcR, dstR;
1426 lua2rect(L, 2, &srcR);
1427 lua2rect(L, 3, &dstR);
1428 const char* scaleToFitStr = lua_tostring(L, 4);
1429 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1430
1431 if (scaleToFitStr) {
1432 const struct {
1433 const char* fName;
1434 SkMatrix::ScaleToFit fScaleToFit;
1435 } rec[] = {
1436 { "fill", SkMatrix::kFill_ScaleToFit },
1437 { "start", SkMatrix::kStart_ScaleToFit },
1438 { "center", SkMatrix::kCenter_ScaleToFit },
1439 { "end", SkMatrix::kEnd_ScaleToFit },
1440 };
1441
1442 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1443 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1444 scaleToFit = rec[i].fScaleToFit;
1445 break;
1446 }
1447 }
1448 }
1449
1450 matrix->setRectToRect(srcR, dstR, scaleToFit);
1451 return 0;
1452}
1453
humper@google.com2815c192013-07-10 22:42:30 +00001454static const struct luaL_Reg gSkMatrix_Methods[] = {
1455 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001456 { "getScaleX", lmatrix_getScaleX },
1457 { "getScaleY", lmatrix_getScaleY },
1458 { "getTranslateX", lmatrix_getTranslateX },
1459 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001460 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001461 { "invert", lmatrix_invert },
1462 { "mapXY", lmatrix_mapXY },
halcanary96fcdcc2015-08-27 07:41:13 -07001463 { nullptr, nullptr }
humper@google.com2815c192013-07-10 22:42:30 +00001464};
1465
1466///////////////////////////////////////////////////////////////////////////////
1467
reed@google.com74ce6f02013-05-22 15:13:18 +00001468static int lpath_getBounds(lua_State* L) {
1469 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1470 return 1;
1471}
1472
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001473static const char* fill_type_to_str(SkPath::FillType fill) {
1474 switch (fill) {
1475 case SkPath::kEvenOdd_FillType:
1476 return "even-odd";
1477 case SkPath::kWinding_FillType:
1478 return "winding";
1479 case SkPath::kInverseEvenOdd_FillType:
1480 return "inverse-even-odd";
1481 case SkPath::kInverseWinding_FillType:
1482 return "inverse-winding";
1483 }
1484 return "unknown";
1485}
1486
1487static int lpath_getFillType(lua_State* L) {
1488 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1489 SkLua(L).pushString(fill_type_to_str(fill));
1490 return 1;
1491}
1492
1493static SkString segment_masks_to_str(uint32_t segmentMasks) {
1494 SkString result;
1495 bool first = true;
1496 if (SkPath::kLine_SegmentMask & segmentMasks) {
1497 result.append("line");
1498 first = false;
1499 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1500 }
1501 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1502 if (!first) {
1503 result.append(" ");
1504 }
1505 result.append("quad");
1506 first = false;
1507 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1508 }
1509 if (SkPath::kConic_SegmentMask & segmentMasks) {
1510 if (!first) {
1511 result.append(" ");
1512 }
1513 result.append("conic");
1514 first = false;
1515 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1516 }
1517 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1518 if (!first) {
1519 result.append(" ");
1520 }
1521 result.append("cubic");
1522 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1523 }
1524 SkASSERT(0 == segmentMasks);
1525 return result;
1526}
1527
krajcevski95498ed2014-08-18 08:02:33 -07001528static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001529 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1530 SkLua(L).pushString(segment_masks_to_str(segMasks));
1531 return 1;
1532}
1533
1534static int lpath_isConvex(lua_State* L) {
1535 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1536 SkLua(L).pushBool(isConvex);
1537 return 1;
1538}
1539
reed@google.com74ce6f02013-05-22 15:13:18 +00001540static int lpath_isEmpty(lua_State* L) {
1541 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1542 return 1;
1543}
1544
1545static int lpath_isRect(lua_State* L) {
1546 SkRect r;
1547 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1548 int ret_count = 1;
1549 lua_pushboolean(L, pred);
1550 if (pred) {
1551 SkLua(L).pushRect(r);
1552 ret_count += 1;
1553 }
1554 return ret_count;
1555}
1556
1557static const char* dir2string(SkPath::Direction dir) {
1558 static const char* gStr[] = {
1559 "unknown", "cw", "ccw"
1560 };
1561 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1562 return gStr[dir];
1563}
1564
caryclark95bc5f32015-04-08 08:34:15 -07001565static int lpath_isNestedFillRects(lua_State* L) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001566 SkRect rects[2];
1567 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001568 bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
reed@google.com74ce6f02013-05-22 15:13:18 +00001569 int ret_count = 1;
1570 lua_pushboolean(L, pred);
1571 if (pred) {
1572 SkLua lua(L);
1573 lua.pushRect(rects[0]);
1574 lua.pushRect(rects[1]);
1575 lua_pushstring(L, dir2string(dirs[0]));
1576 lua_pushstring(L, dir2string(dirs[0]));
1577 ret_count += 4;
1578 }
1579 return ret_count;
1580}
1581
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001582static int lpath_countPoints(lua_State* L) {
1583 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1584 return 1;
1585}
1586
reed@google.com74ce6f02013-05-22 15:13:18 +00001587static int lpath_reset(lua_State* L) {
1588 get_obj<SkPath>(L, 1)->reset();
1589 return 0;
1590}
1591
1592static int lpath_moveTo(lua_State* L) {
1593 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1594 return 0;
1595}
1596
1597static int lpath_lineTo(lua_State* L) {
1598 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1599 return 0;
1600}
1601
1602static int lpath_quadTo(lua_State* L) {
1603 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1604 lua2scalar(L, 4), lua2scalar(L, 5));
1605 return 0;
1606}
1607
1608static int lpath_cubicTo(lua_State* L) {
1609 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1610 lua2scalar(L, 4), lua2scalar(L, 5),
1611 lua2scalar(L, 6), lua2scalar(L, 7));
1612 return 0;
1613}
1614
1615static int lpath_close(lua_State* L) {
1616 get_obj<SkPath>(L, 1)->close();
1617 return 0;
1618}
1619
1620static int lpath_gc(lua_State* L) {
1621 get_obj<SkPath>(L, 1)->~SkPath();
1622 return 0;
1623}
1624
1625static const struct luaL_Reg gSkPath_Methods[] = {
1626 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001627 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001628 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001629 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001630 { "isEmpty", lpath_isEmpty },
1631 { "isRect", lpath_isRect },
caryclark95bc5f32015-04-08 08:34:15 -07001632 { "isNestedFillRects", lpath_isNestedFillRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001633 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001634 { "reset", lpath_reset },
1635 { "moveTo", lpath_moveTo },
1636 { "lineTo", lpath_lineTo },
1637 { "quadTo", lpath_quadTo },
1638 { "cubicTo", lpath_cubicTo },
1639 { "close", lpath_close },
1640 { "__gc", lpath_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001641 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001642};
1643
1644///////////////////////////////////////////////////////////////////////////////
1645
1646static const char* rrect_type(const SkRRect& rr) {
1647 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001648 case SkRRect::kEmpty_Type: return "empty";
1649 case SkRRect::kRect_Type: return "rect";
1650 case SkRRect::kOval_Type: return "oval";
1651 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001652 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001653 case SkRRect::kComplex_Type: return "complex";
1654 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001655 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001656 return "";
1657}
1658
1659static int lrrect_rect(lua_State* L) {
1660 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1661 return 1;
1662}
1663
1664static int lrrect_type(lua_State* L) {
1665 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1666 return 1;
1667}
1668
1669static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001670 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001671 SkVector v;
1672 if (corner < 0 || corner > 3) {
1673 SkDebugf("bad corner index %d", corner);
1674 v.set(0, 0);
1675 } else {
1676 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1677 }
1678 lua_pushnumber(L, v.fX);
1679 lua_pushnumber(L, v.fY);
1680 return 2;
1681}
1682
1683static int lrrect_gc(lua_State* L) {
1684 get_obj<SkRRect>(L, 1)->~SkRRect();
1685 return 0;
1686}
1687
1688static const struct luaL_Reg gSkRRect_Methods[] = {
1689 { "rect", lrrect_rect },
1690 { "type", lrrect_type },
1691 { "radii", lrrect_radii },
1692 { "__gc", lrrect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001693 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001694};
1695
1696///////////////////////////////////////////////////////////////////////////////
1697
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001698static int limage_width(lua_State* L) {
1699 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1700 return 1;
1701}
1702
1703static int limage_height(lua_State* L) {
1704 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1705 return 1;
1706}
1707
reed7a72c672014-11-07 10:23:55 -08001708static int limage_newShader(lua_State* L) {
1709 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
halcanary96fcdcc2015-08-27 07:41:13 -07001710 const SkMatrix* localM = nullptr;
reed7a72c672014-11-07 10:23:55 -08001711 SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM));
1712 push_ref(L, shader.get());
1713 return 1;
1714}
1715
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001716static int limage_gc(lua_State* L) {
1717 get_ref<SkImage>(L, 1)->unref();
1718 return 0;
1719}
1720
1721static const struct luaL_Reg gSkImage_Methods[] = {
1722 { "width", limage_width },
1723 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001724 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001725 { "__gc", limage_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001726 { nullptr, nullptr }
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001727};
1728
1729///////////////////////////////////////////////////////////////////////////////
1730
reed485557f2014-10-12 10:36:47 -07001731static int lsurface_width(lua_State* L) {
1732 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1733 return 1;
1734}
1735
1736static int lsurface_height(lua_State* L) {
1737 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1738 return 1;
1739}
1740
1741static int lsurface_getCanvas(lua_State* L) {
1742 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001743 if (nullptr == canvas) {
reed485557f2014-10-12 10:36:47 -07001744 lua_pushnil(L);
1745 } else {
1746 push_ref(L, canvas);
1747 // note: we don't unref canvas, since getCanvas did not ref it.
1748 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1749 // the real owner (the surface) go away, but still hold onto the canvas?
1750 // *really* we want to sort of ref the surface again, but have the native object
1751 // know that it is supposed to be treated as a canvas...
1752 }
1753 return 1;
1754}
1755
1756static int lsurface_newImageSnapshot(lua_State* L) {
1757 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
halcanary96fcdcc2015-08-27 07:41:13 -07001758 if (nullptr == image) {
reed485557f2014-10-12 10:36:47 -07001759 lua_pushnil(L);
1760 } else {
reed9fbc3f32014-10-21 07:12:58 -07001761 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001762 }
1763 return 1;
1764}
1765
1766static int lsurface_newSurface(lua_State* L) {
1767 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001768 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001769 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1770 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -07001771 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07001772 lua_pushnil(L);
1773 } else {
reed9fbc3f32014-10-21 07:12:58 -07001774 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001775 }
1776 return 1;
1777}
1778
1779static int lsurface_gc(lua_State* L) {
1780 get_ref<SkSurface>(L, 1)->unref();
1781 return 0;
1782}
1783
1784static const struct luaL_Reg gSkSurface_Methods[] = {
1785 { "width", lsurface_width },
1786 { "height", lsurface_height },
1787 { "getCanvas", lsurface_getCanvas },
1788 { "newImageSnapshot", lsurface_newImageSnapshot },
1789 { "newSurface", lsurface_newSurface },
1790 { "__gc", lsurface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001791 { nullptr, nullptr }
reed485557f2014-10-12 10:36:47 -07001792};
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
reed96affcd2014-10-13 12:38:04 -07001796static int lpicturerecorder_beginRecording(lua_State* L) {
1797 const SkScalar w = lua2scalar_def(L, 2, -1);
1798 const SkScalar h = lua2scalar_def(L, 3, -1);
1799 if (w <= 0 || h <= 0) {
1800 lua_pushnil(L);
1801 return 1;
1802 }
1803
1804 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
halcanary96fcdcc2015-08-27 07:41:13 -07001805 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001806 lua_pushnil(L);
1807 return 1;
1808 }
1809
1810 push_ref(L, canvas);
1811 return 1;
1812}
1813
1814static int lpicturerecorder_getCanvas(lua_State* L) {
1815 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001816 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001817 lua_pushnil(L);
1818 return 1;
1819 }
1820 push_ref(L, canvas);
1821 return 1;
1822}
1823
1824static int lpicturerecorder_endRecording(lua_State* L) {
1825 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
halcanary96fcdcc2015-08-27 07:41:13 -07001826 if (nullptr == pic) {
reed96affcd2014-10-13 12:38:04 -07001827 lua_pushnil(L);
1828 return 1;
1829 }
reed9fbc3f32014-10-21 07:12:58 -07001830 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001831 return 1;
1832}
1833
1834static int lpicturerecorder_gc(lua_State* L) {
1835 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1836 return 0;
1837}
1838
1839static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1840 { "beginRecording", lpicturerecorder_beginRecording },
1841 { "getCanvas", lpicturerecorder_getCanvas },
1842 { "endRecording", lpicturerecorder_endRecording },
1843 { "__gc", lpicturerecorder_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001844 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001845};
1846
1847///////////////////////////////////////////////////////////////////////////////
1848
1849static int lpicture_width(lua_State* L) {
1850 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1851 return 1;
1852}
1853
1854static int lpicture_height(lua_State* L) {
1855 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1856 return 1;
1857}
1858
1859static int lpicture_gc(lua_State* L) {
1860 get_ref<SkPicture>(L, 1)->unref();
1861 return 0;
1862}
1863
1864static const struct luaL_Reg gSkPicture_Methods[] = {
1865 { "width", lpicture_width },
1866 { "height", lpicture_height },
1867 { "__gc", lpicture_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001868 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001869};
1870
1871///////////////////////////////////////////////////////////////////////////////
1872
reed1b6ab442014-11-03 19:55:41 -08001873static int ltextblob_bounds(lua_State* L) {
1874 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1875 return 1;
1876}
1877
1878static int ltextblob_gc(lua_State* L) {
1879 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1880 return 0;
1881}
1882
1883static const struct luaL_Reg gSkTextBlob_Methods[] = {
1884 { "bounds", ltextblob_bounds },
1885 { "__gc", ltextblob_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001886 { nullptr, nullptr }
reed1b6ab442014-11-03 19:55:41 -08001887};
1888
1889///////////////////////////////////////////////////////////////////////////////
1890
reed36c9c112014-11-04 10:58:42 -08001891static int ltypeface_getFamilyName(lua_State* L) {
1892 SkString str;
1893 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1894 lua_pushstring(L, str.c_str());
1895 return 1;
1896}
1897
1898static int ltypeface_getStyle(lua_State* L) {
1899 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1900 return 1;
1901}
1902
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001903static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001904 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001905 return 0;
1906}
1907
1908static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001909 { "getFamilyName", ltypeface_getFamilyName },
1910 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001911 { "__gc", ltypeface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001912 { nullptr, nullptr }
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001913};
1914
1915///////////////////////////////////////////////////////////////////////////////
1916
reed@google.com74ce6f02013-05-22 15:13:18 +00001917class AutoCallLua {
1918public:
1919 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1920 lua_getglobal(L, func);
1921 if (!lua_isfunction(L, -1)) {
1922 int t = lua_type(L, -1);
1923 SkDebugf("--- expected function %d\n", t);
1924 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001925
reed@google.com74ce6f02013-05-22 15:13:18 +00001926 lua_newtable(L);
1927 setfield_string(L, "verb", verb);
1928 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001929
reed@google.com74ce6f02013-05-22 15:13:18 +00001930 ~AutoCallLua() {
1931 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1932 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1933 }
1934 lua_settop(fL, -1);
1935 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001936
reed@google.com74ce6f02013-05-22 15:13:18 +00001937private:
1938 lua_State* fL;
1939};
1940
1941#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1942
1943///////////////////////////////////////////////////////////////////////////////
1944
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001945static int lsk_newDocumentPDF(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001946 const char* file = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001947 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001948 file = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001949 }
1950
1951 SkDocument* doc = SkDocument::CreatePDF(file);
halcanary96fcdcc2015-08-27 07:41:13 -07001952 if (nullptr == doc) {
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001953 // do I need to push a nil on the stack and return 1?
1954 return 0;
1955 } else {
reed9fbc3f32014-10-21 07:12:58 -07001956 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001957 return 1;
1958 }
1959}
1960
reed468b1812014-10-19 11:42:54 -07001961static int lsk_newBlurImageFilter(lua_State* L) {
1962 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1963 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1964 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
halcanary96fcdcc2015-08-27 07:41:13 -07001965 if (nullptr == imf) {
reed468b1812014-10-19 11:42:54 -07001966 lua_pushnil(L);
1967 } else {
reed9fbc3f32014-10-21 07:12:58 -07001968 push_ref(L, imf)->unref();
1969 }
1970 return 1;
1971}
1972
1973static int lsk_newLinearGradient(lua_State* L) {
1974 SkScalar x0 = lua2scalar_def(L, 1, 0);
1975 SkScalar y0 = lua2scalar_def(L, 2, 0);
1976 SkColor c0 = lua2color(L, 3);
1977 SkScalar x1 = lua2scalar_def(L, 4, 0);
1978 SkScalar y1 = lua2scalar_def(L, 5, 0);
1979 SkColor c1 = lua2color(L, 6);
1980
1981 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1982 SkColor colors[] = { c0, c1 };
halcanary96fcdcc2015-08-27 07:41:13 -07001983 SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
1984 if (nullptr == s) {
reed9fbc3f32014-10-21 07:12:58 -07001985 lua_pushnil(L);
1986 } else {
1987 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001988 }
1989 return 1;
1990}
1991
reedbdc49ae2014-10-14 09:34:52 -07001992static int lsk_newMatrix(lua_State* L) {
1993 push_new<SkMatrix>(L)->reset();
1994 return 1;
1995}
1996
reed@google.com3597b732013-05-22 20:12:50 +00001997static int lsk_newPaint(lua_State* L) {
1998 push_new<SkPaint>(L);
1999 return 1;
2000}
2001
2002static int lsk_newPath(lua_State* L) {
2003 push_new<SkPath>(L);
2004 return 1;
2005}
2006
reed96affcd2014-10-13 12:38:04 -07002007static int lsk_newPictureRecorder(lua_State* L) {
2008 push_new<SkPictureRecorder>(L);
2009 return 1;
2010}
2011
reed@google.com3597b732013-05-22 20:12:50 +00002012static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07002013 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00002014 return 1;
2015}
2016
reed1b6ab442014-11-03 19:55:41 -08002017#include "SkTextBox.h"
2018// Sk.newTextBlob(text, rect, paint)
2019static int lsk_newTextBlob(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07002020 const char* text = lua_tolstring(L, 1, nullptr);
reed1b6ab442014-11-03 19:55:41 -08002021 SkRect bounds;
2022 lua2rect(L, 2, &bounds);
2023 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
2024
2025 SkTextBox box;
2026 box.setMode(SkTextBox::kLineBreak_Mode);
2027 box.setBox(bounds);
2028 box.setText(text, strlen(text), paint);
2029
2030 SkScalar newBottom;
2031 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
2032 push_ref<SkTextBlob>(L, blob);
2033 SkLua(L).pushScalar(newBottom);
2034 return 2;
2035}
2036
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002037static int lsk_newTypeface(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07002038 const char* name = nullptr;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002039 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00002040
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002041 int count = lua_gettop(L);
2042 if (count > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002043 name = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002044 if (count > 1 && lua_isnumber(L, 2)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002045 style = lua_tointegerx(L, 2, nullptr) & SkTypeface::kBoldItalic;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002046 }
2047 }
2048
2049 SkTypeface* face = SkTypeface::CreateFromName(name,
2050 (SkTypeface::Style)style);
2051// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
halcanary96fcdcc2015-08-27 07:41:13 -07002052 if (nullptr == face) {
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002053 face = SkTypeface::RefDefault();
2054 }
reed9fbc3f32014-10-21 07:12:58 -07002055 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002056 return 1;
2057}
reed@google.com3597b732013-05-22 20:12:50 +00002058
reed485557f2014-10-12 10:36:47 -07002059static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08002060 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07002061 int height = lua2int_def(L, 2, 0);
2062 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
robertphillips702edbd2015-06-23 06:26:08 -07002063 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
2064 SkSurface* surface = SkSurface::NewRaster(info, &props);
halcanary96fcdcc2015-08-27 07:41:13 -07002065 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07002066 lua_pushnil(L);
2067 } else {
reed9fbc3f32014-10-21 07:12:58 -07002068 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07002069 }
2070 return 1;
2071}
2072
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002073static int lsk_loadImage(lua_State* L) {
2074 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002075 const char* name = lua_tolstring(L, 1, nullptr);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002076 SkAutoDataUnref data(SkData::NewFromFileName(name));
2077 if (data.get()) {
reed871872f2015-06-22 12:48:26 -07002078 SkImage* image = SkImage::NewFromEncoded(data);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002079 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07002080 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002081 return 1;
2082 }
2083 }
2084 }
2085 return 0;
2086}
2087
reed@google.com3597b732013-05-22 20:12:50 +00002088static void register_Sk(lua_State* L) {
2089 lua_newtable(L);
2090 lua_pushvalue(L, -1);
2091 lua_setglobal(L, "Sk");
2092 // the Sk table is still on top
2093
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002094 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002095 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002096 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002097 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002098 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002099 setfield_function(L, "newPaint", lsk_newPaint);
2100 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002101 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002102 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002103 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002104 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002105 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00002106 lua_pop(L, 1); // pop off the Sk table
2107}
2108
reed@google.com74ce6f02013-05-22 15:13:18 +00002109#define REG_CLASS(L, C) \
2110 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002111 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002112 lua_pushvalue(L, -1); \
2113 lua_setfield(L, -2, "__index"); \
2114 luaL_setfuncs(L, g##C##_Methods, 0); \
2115 lua_pop(L, 1); /* pop off the meta-table */ \
2116 } while (0)
2117
2118void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002119 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002120 REG_CLASS(L, SkCanvas);
reed22a517f2015-12-04 20:45:59 -08002121 REG_CLASS(L, SkColorFilter);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002122 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002123 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002124 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002125 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002126 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002127 REG_CLASS(L, SkPath);
2128 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002129 REG_CLASS(L, SkPicture);
2130 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002131 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002132 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002133 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002134 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002135 REG_CLASS(L, SkTypeface);
robertphillips233bab92016-01-21 09:05:32 -08002136 REG_CLASS(L, SkXfermode);
reed@google.com74ce6f02013-05-22 15:13:18 +00002137}
zachr@google.com28c27c82013-06-20 17:15:05 +00002138
reed@google.com7bce9982013-06-20 17:40:21 +00002139extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002140extern "C" int luaopen_skia(lua_State* L) {
2141 SkLua::Load(L);
2142 return 0;
2143}