blob: 3a335ad17c589441574fbe3b608fac4f8f2bd1eb [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
reed2ad1aa62016-03-09 09:50:50 -080085template <typename T> void push_ref(lua_State* L, sk_sp<T> sp) {
86 *(T**)lua_newuserdata(L, sizeof(T*)) = sp.release();
87 luaL_getmetatable(L, get_mtname<T>());
88 lua_setmetatable(L, -2);
89}
90
reed@google.com74ce6f02013-05-22 15:13:18 +000091template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000092 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000093}
94
95template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000096 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000097}
98
reed@google.com88c9ec92013-05-22 15:43:21 +000099static bool lua2bool(lua_State* L, int index) {
100 return !!lua_toboolean(L, index);
101}
102
reed@google.com74ce6f02013-05-22 15:13:18 +0000103///////////////////////////////////////////////////////////////////////////////
104
reed@google.com3597b732013-05-22 20:12:50 +0000105SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
106 fL = luaL_newstate();
107 luaL_openlibs(fL);
108 SkLua::Load(fL);
109}
110
111SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
112
113SkLua::~SkLua() {
114 if (fWeOwnL) {
115 if (fTermCode.size() > 0) {
116 lua_getglobal(fL, fTermCode.c_str());
117 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
118 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
119 }
120 }
121 lua_close(fL);
122 }
123}
124
125bool SkLua::runCode(const char code[]) {
126 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
127 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000128 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000129 return false;
130 }
131 return true;
132}
133
134bool SkLua::runCode(const void* code, size_t size) {
135 SkString str((const char*)code, size);
136 return this->runCode(str.c_str());
137}
138
139///////////////////////////////////////////////////////////////////////////////
140
141#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
142
reed@google.com29563872013-07-10 21:23:49 +0000143static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
144 if (pred) {
145 lua_pushboolean(L, true);
146 lua_setfield(L, -2, key);
147 }
148}
149
reed@google.com74ce6f02013-05-22 15:13:18 +0000150static void setfield_string(lua_State* L, const char key[], const char value[]) {
151 lua_pushstring(L, value);
152 lua_setfield(L, -2, key);
153}
154
155static void setfield_number(lua_State* L, const char key[], double value) {
156 lua_pushnumber(L, value);
157 lua_setfield(L, -2, key);
158}
159
humper@google.com2815c192013-07-10 22:42:30 +0000160static void setfield_boolean(lua_State* L, const char key[], bool value) {
161 lua_pushboolean(L, value);
162 lua_setfield(L, -2, key);
163}
164
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000165static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
166 setfield_number(L, key, SkScalarToLua(value));
167}
168
reed@google.com3597b732013-05-22 20:12:50 +0000169static void setfield_function(lua_State* L,
170 const char key[], lua_CFunction value) {
171 lua_pushcfunction(L, value);
172 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000173}
174
reed7a72c672014-11-07 10:23:55 -0800175static int lua2int_def(lua_State* L, int index, int defaultValue) {
176 if (lua_isnumber(L, index)) {
177 return (int)lua_tonumber(L, index);
178 } else {
179 return defaultValue;
180 }
181}
182
183static SkScalar lua2scalar(lua_State* L, int index) {
184 SkASSERT(lua_isnumber(L, index));
185 return SkLuaToScalar(lua_tonumber(L, index));
186}
187
188static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
189 if (lua_isnumber(L, index)) {
190 return SkLuaToScalar(lua_tonumber(L, index));
191 } else {
192 return defaultValue;
193 }
194}
195
196static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
197 SkASSERT(lua_istable(L, stackIndex));
198 lua_rawgeti(L, stackIndex, arrayIndex);
mtklein8aacf202014-12-18 13:29:54 -0800199
reed7a72c672014-11-07 10:23:55 -0800200 SkScalar value = lua2scalar(L, -1);
201 lua_pop(L, 1);
202 return value;
203}
204
205static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
206 for (int i = 0; i < count; ++i) {
207 dst[i] = getarray_scalar(L, stackIndex, i + 1);
208 }
209}
210
211static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
212 getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
213}
214
reed@google.come3823fd2013-05-30 18:55:14 +0000215static void setarray_number(lua_State* L, int index, double value) {
216 lua_pushnumber(L, value);
217 lua_rawseti(L, -2, index);
218}
219
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000220static void setarray_scalar(lua_State* L, int index, SkScalar value) {
221 setarray_number(L, index, SkScalarToLua(value));
222}
223
hstern0b401ce2016-08-02 09:17:59 -0700224static void setarray_string(lua_State* L, int index, const char str[]) {
225 lua_pushstring(L, str);
226 lua_rawseti(L, -2, index);
227}
228
reed@google.com74ce6f02013-05-22 15:13:18 +0000229void SkLua::pushBool(bool value, const char key[]) {
230 lua_pushboolean(fL, value);
231 CHECK_SETFIELD(key);
232}
233
234void SkLua::pushString(const char str[], const char key[]) {
235 lua_pushstring(fL, str);
236 CHECK_SETFIELD(key);
237}
238
reed@google.come3823fd2013-05-30 18:55:14 +0000239void SkLua::pushString(const char str[], size_t length, const char key[]) {
240 // TODO: how to do this w/o making a copy?
241 SkString s(str, length);
242 lua_pushstring(fL, s.c_str());
243 CHECK_SETFIELD(key);
244}
245
reed@google.com74ce6f02013-05-22 15:13:18 +0000246void SkLua::pushString(const SkString& str, const char key[]) {
247 lua_pushstring(fL, str.c_str());
248 CHECK_SETFIELD(key);
249}
250
251void SkLua::pushColor(SkColor color, const char key[]) {
252 lua_newtable(fL);
253 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
254 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
255 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
256 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
257 CHECK_SETFIELD(key);
258}
259
reed@google.come3823fd2013-05-30 18:55:14 +0000260void SkLua::pushU32(uint32_t value, const char key[]) {
261 lua_pushnumber(fL, (double)value);
262 CHECK_SETFIELD(key);
263}
264
reed@google.com74ce6f02013-05-22 15:13:18 +0000265void SkLua::pushScalar(SkScalar value, const char key[]) {
266 lua_pushnumber(fL, SkScalarToLua(value));
267 CHECK_SETFIELD(key);
268}
269
reed@google.come3823fd2013-05-30 18:55:14 +0000270void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
271 lua_newtable(fL);
272 for (int i = 0; i < count; ++i) {
273 // make it base-1 to match lua convention
274 setarray_number(fL, i + 1, (double)array[i]);
275 }
276 CHECK_SETFIELD(key);
277}
278
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000279void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
280 lua_newtable(fL);
281 for (int i = 0; i < count; ++i) {
282 // make it base-1 to match lua convention
283 lua_newtable(fL);
284 this->pushScalar(array[i].fX, "x");
285 this->pushScalar(array[i].fY, "y");
286 lua_rawseti(fL, -2, i + 1);
287 }
288 CHECK_SETFIELD(key);
289}
290
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000291void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
292 lua_newtable(fL);
293 for (int i = 0; i < count; ++i) {
294 // make it base-1 to match lua convention
295 setarray_scalar(fL, i + 1, array[i]);
296 }
297 CHECK_SETFIELD(key);
298}
299
reed@google.com74ce6f02013-05-22 15:13:18 +0000300void SkLua::pushRect(const SkRect& r, const char key[]) {
301 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000302 setfield_scalar(fL, "left", r.fLeft);
303 setfield_scalar(fL, "top", r.fTop);
304 setfield_scalar(fL, "right", r.fRight);
305 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000306 CHECK_SETFIELD(key);
307}
308
309void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
310 push_obj(fL, rr);
311 CHECK_SETFIELD(key);
312}
313
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000314void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
315 lua_newtable(fL);
316 setfield_scalar(fL, "phase", info.fPhase);
317 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
318 CHECK_SETFIELD(key);
319}
320
321
reed@google.com74ce6f02013-05-22 15:13:18 +0000322void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
323 push_obj(fL, matrix);
324 CHECK_SETFIELD(key);
325}
326
327void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
328 push_obj(fL, paint);
329 CHECK_SETFIELD(key);
330}
331
332void SkLua::pushPath(const SkPath& path, const char key[]) {
333 push_obj(fL, path);
334 CHECK_SETFIELD(key);
335}
336
337void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
338 push_ref(fL, canvas);
339 CHECK_SETFIELD(key);
340}
341
fmalitab7425172014-08-26 07:56:44 -0700342void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
343 push_ref(fL, const_cast<SkTextBlob*>(blob));
344 CHECK_SETFIELD(key);
345}
346
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000347static const char* element_type(SkClipStack::Element::Type type) {
348 switch (type) {
349 case SkClipStack::Element::kEmpty_Type:
350 return "empty";
351 case SkClipStack::Element::kRect_Type:
352 return "rect";
353 case SkClipStack::Element::kRRect_Type:
354 return "rrect";
355 case SkClipStack::Element::kPath_Type:
356 return "path";
357 }
358 return "unknown";
359}
360
361static const char* region_op(SkRegion::Op op) {
362 switch (op) {
363 case SkRegion::kDifference_Op:
364 return "difference";
365 case SkRegion::kIntersect_Op:
366 return "intersect";
367 case SkRegion::kUnion_Op:
368 return "union";
369 case SkRegion::kXOR_Op:
370 return "xor";
371 case SkRegion::kReverseDifference_Op:
372 return "reverse-difference";
373 case SkRegion::kReplace_Op:
374 return "replace";
375 }
376 return "unknown";
377}
378
379void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
380 lua_newtable(fL);
381 SkClipStack::B2TIter iter(stack);
382 const SkClipStack::Element* element;
383 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700384 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000385 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000386 lua_rawseti(fL, -2, ++i);
387 }
388 CHECK_SETFIELD(key);
389}
390
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000391void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
392 lua_newtable(fL);
393 SkClipStack::Element::Type type = element.getType();
394 this->pushString(element_type(type), "type");
395 switch (type) {
396 case SkClipStack::Element::kEmpty_Type:
397 break;
398 case SkClipStack::Element::kRect_Type:
399 this->pushRect(element.getRect(), "rect");
400 break;
401 case SkClipStack::Element::kRRect_Type:
402 this->pushRRect(element.getRRect(), "rrect");
403 break;
404 case SkClipStack::Element::kPath_Type:
405 this->pushPath(element.getPath(), "path");
406 break;
407 }
408 this->pushString(region_op(element.getOp()), "op");
409 this->pushBool(element.isAA(), "aa");
410 CHECK_SETFIELD(key);
411}
412
413
reed@google.com74ce6f02013-05-22 15:13:18 +0000414///////////////////////////////////////////////////////////////////////////////
415///////////////////////////////////////////////////////////////////////////////
416
reed@google.com74ce6f02013-05-22 15:13:18 +0000417static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
418 SkASSERT(lua_istable(L, index));
419 lua_pushstring(L, key);
420 lua_gettable(L, index);
mtklein8aacf202014-12-18 13:29:54 -0800421
reed@google.com74ce6f02013-05-22 15:13:18 +0000422 SkScalar value = lua2scalar(L, -1);
423 lua_pop(L, 1);
424 return value;
425}
426
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000427static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
428 SkASSERT(lua_istable(L, index));
429 lua_pushstring(L, key);
430 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000431
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000432 SkScalar value;
433 if (lua_isnil(L, -1)) {
434 value = def;
435 } else {
436 value = lua2scalar(L, -1);
437 }
438 lua_pop(L, 1);
439 return value;
440}
441
reed468b1812014-10-19 11:42:54 -0700442static SkScalar byte2unit(U8CPU byte) {
443 return byte / 255.0f;
444}
445
reed@google.com74ce6f02013-05-22 15:13:18 +0000446static U8CPU unit2byte(SkScalar x) {
447 if (x <= 0) {
448 return 0;
449 } else if (x >= 1) {
450 return 255;
451 } else {
452 return SkScalarRoundToInt(x * 255);
453 }
454}
455
456static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700457 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
458 unit2byte(getfield_scalar_default(L, index, "r", 0)),
459 unit2byte(getfield_scalar_default(L, index, "g", 0)),
460 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000461}
462
463static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000464 rect->set(getfield_scalar_default(L, index, "left", 0),
465 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000466 getfield_scalar(L, index, "right"),
467 getfield_scalar(L, index, "bottom"));
468 return rect;
469}
470
reedf355df52014-10-12 12:18:40 -0700471static int lcanvas_clear(lua_State* L) {
472 get_ref<SkCanvas>(L, 1)->clear(0);
473 return 0;
474}
475
reed@google.com74ce6f02013-05-22 15:13:18 +0000476static int lcanvas_drawColor(lua_State* L) {
477 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
478 return 0;
479}
480
reed9fbc3f32014-10-21 07:12:58 -0700481static int lcanvas_drawPaint(lua_State* L) {
482 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
483 return 0;
484}
485
reed@google.com74ce6f02013-05-22 15:13:18 +0000486static int lcanvas_drawRect(lua_State* L) {
487 SkRect rect;
reed7a72c672014-11-07 10:23:55 -0800488 lua2rect(L, 2, &rect);
489 const SkPaint* paint = get_obj<SkPaint>(L, 3);
490 get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
reed@google.com74ce6f02013-05-22 15:13:18 +0000491 return 0;
492}
493
494static int lcanvas_drawOval(lua_State* L) {
495 SkRect rect;
496 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
497 *get_obj<SkPaint>(L, 3));
498 return 0;
499}
500
501static int lcanvas_drawCircle(lua_State* L) {
502 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
503 lua2scalar(L, 3),
504 lua2scalar(L, 4),
505 *get_obj<SkPaint>(L, 5));
506 return 0;
507}
508
reed485557f2014-10-12 10:36:47 -0700509static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
510 if (lua_isnumber(L, index)) {
511 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
512 return paint;
reedf355df52014-10-12 12:18:40 -0700513 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700514 const SkPaint* ptr = get_obj<SkPaint>(L, index);
515 if (ptr) {
516 *paint = *ptr;
517 return paint;
518 }
519 }
halcanary96fcdcc2015-08-27 07:41:13 -0700520 return nullptr;
reed485557f2014-10-12 10:36:47 -0700521}
522
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000523static int lcanvas_drawImage(lua_State* L) {
524 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
525 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700526 if (nullptr == image) {
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000527 return 0;
528 }
529 SkScalar x = lua2scalar(L, 3);
530 SkScalar y = lua2scalar(L, 4);
531
532 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700533 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000534 return 0;
535}
536
reedba5fb932014-10-10 15:28:19 -0700537static int lcanvas_drawImageRect(lua_State* L) {
538 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
539 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700540 if (nullptr == image) {
reedba5fb932014-10-10 15:28:19 -0700541 return 0;
542 }
543
544 SkRect srcR, dstR;
halcanary96fcdcc2015-08-27 07:41:13 -0700545 SkRect* srcRPtr = nullptr;
reedba5fb932014-10-10 15:28:19 -0700546 if (!lua_isnil(L, 3)) {
547 srcRPtr = lua2rect(L, 3, &srcR);
548 }
549 lua2rect(L, 4, &dstR);
mtklein8aacf202014-12-18 13:29:54 -0800550
reedba5fb932014-10-10 15:28:19 -0700551 SkPaint paint;
reede47829b2015-08-06 10:02:53 -0700552 canvas->legacy_drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700553 return 0;
554}
555
reed7a72c672014-11-07 10:23:55 -0800556static int lcanvas_drawPatch(lua_State* L) {
557 SkPoint cubics[12];
558 SkColor colorStorage[4];
559 SkPoint texStorage[4];
560
halcanary96fcdcc2015-08-27 07:41:13 -0700561 const SkColor* colors = nullptr;
562 const SkPoint* texs = nullptr;
reed7a72c672014-11-07 10:23:55 -0800563
564 getarray_points(L, 2, cubics, 12);
565
566 colorStorage[0] = SK_ColorRED;
567 colorStorage[1] = SK_ColorGREEN;
568 colorStorage[2] = SK_ColorBLUE;
569 colorStorage[3] = SK_ColorGRAY;
570
571 if (lua_isnil(L, 4)) {
572 colors = colorStorage;
573 } else {
574 getarray_points(L, 4, texStorage, 4);
575 texs = texStorage;
576 }
577
halcanary96fcdcc2015-08-27 07:41:13 -0700578 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, nullptr, *get_obj<SkPaint>(L, 5));
reed7a72c672014-11-07 10:23:55 -0800579 return 0;
580}
581
reed@google.comfd345872013-05-22 20:53:42 +0000582static int lcanvas_drawPath(lua_State* L) {
583 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
584 *get_obj<SkPaint>(L, 3));
585 return 0;
586}
587
reed96affcd2014-10-13 12:38:04 -0700588// drawPicture(pic, x, y, paint)
589static int lcanvas_drawPicture(lua_State* L) {
590 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
591 SkPicture* picture = get_ref<SkPicture>(L, 2);
592 SkScalar x = lua2scalar_def(L, 3, 0);
593 SkScalar y = lua2scalar_def(L, 4, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700594 SkMatrix matrix, *matrixPtr = nullptr;
reed96affcd2014-10-13 12:38:04 -0700595 if (x || y) {
596 matrix.setTranslate(x, y);
597 matrixPtr = &matrix;
598 }
599 SkPaint paint;
600 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
601 return 0;
602}
603
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000604static int lcanvas_drawText(lua_State* L) {
605 if (lua_gettop(L) < 5) {
606 return 0;
607 }
608
609 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
610 size_t len;
611 const char* text = lua_tolstring(L, 2, &len);
612 get_ref<SkCanvas>(L, 1)->drawText(text, len,
613 lua2scalar(L, 3), lua2scalar(L, 4),
614 *get_obj<SkPaint>(L, 5));
615 }
616 return 0;
617}
618
reed1b6ab442014-11-03 19:55:41 -0800619static int lcanvas_drawTextBlob(lua_State* L) {
620 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
621 SkScalar x = lua2scalar(L, 3);
622 SkScalar y = lua2scalar(L, 4);
623 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
624 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
625 return 0;
626}
627
reed@google.com74ce6f02013-05-22 15:13:18 +0000628static int lcanvas_getSaveCount(lua_State* L) {
629 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
630 return 1;
631}
632
633static int lcanvas_getTotalMatrix(lua_State* L) {
634 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
635 return 1;
636}
637
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000638static int lcanvas_getClipStack(lua_State* L) {
639 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
640 return 1;
641}
642
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000643int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
644#if SK_SUPPORT_GPU
645 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
csmartdaltoncbecb082016-07-22 08:59:08 -0700646 SkRect queryBounds = SkRect::Make(canvas->getTopLayerBounds());
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000647
648 GrReducedClip::ElementList elements;
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000649 int32_t genID;
650 SkIRect resultBounds;
csmartdaltoncbecb082016-07-22 08:59:08 -0700651 bool requiresAA;
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000652
653 const SkClipStack& stack = *canvas->getClipStack();
654
655 GrReducedClip::ReduceClipStack(stack,
656 queryBounds,
657 &elements,
658 &genID,
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000659 &resultBounds,
csmartdaltoncbecb082016-07-22 08:59:08 -0700660 &requiresAA);
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000661
662 GrReducedClip::ElementList::Iter iter(elements);
663 int i = 0;
664 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700665 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000666 SkLua(L).pushClipStackElement(*iter.get());
667 iter.next();
668 lua_rawseti(L, -2, ++i);
669 }
670 // Currently this only returns the element list to lua, not the initial state or result bounds.
671 // It could return these as additional items on the lua stack.
672 return 1;
673#else
674 return 0;
675#endif
676}
677
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000678static int lcanvas_save(lua_State* L) {
679 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
680 return 1;
681}
682
reed86217d82014-10-25 20:44:40 -0700683static int lcanvas_saveLayer(lua_State* L) {
684 SkPaint paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700685 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(nullptr, lua2OptionalPaint(L, 2, &paint)));
reed86217d82014-10-25 20:44:40 -0700686 return 1;
687}
688
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000689static int lcanvas_restore(lua_State* L) {
690 get_ref<SkCanvas>(L, 1)->restore();
691 return 0;
692}
693
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000694static int lcanvas_scale(lua_State* L) {
695 SkScalar sx = lua2scalar_def(L, 2, 1);
696 SkScalar sy = lua2scalar_def(L, 3, sx);
697 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
698 return 0;
699}
700
reed@google.com3597b732013-05-22 20:12:50 +0000701static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000702 SkScalar tx = lua2scalar_def(L, 2, 0);
703 SkScalar ty = lua2scalar_def(L, 3, 0);
704 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
705 return 0;
706}
707
708static int lcanvas_rotate(lua_State* L) {
709 SkScalar degrees = lua2scalar_def(L, 2, 0);
710 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000711 return 0;
712}
713
reedbdc49ae2014-10-14 09:34:52 -0700714static int lcanvas_concat(lua_State* L) {
715 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
716 return 0;
717}
718
reed485557f2014-10-12 10:36:47 -0700719static int lcanvas_newSurface(lua_State* L) {
720 int width = lua2int_def(L, 2, 0);
reed7a72c672014-11-07 10:23:55 -0800721 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -0700722 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -0700723 auto surface = get_ref<SkCanvas>(L, 1)->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700724 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -0700725 lua_pushnil(L);
726 } else {
reede8f30622016-03-23 18:59:25 -0700727 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -0700728 }
729 return 1;
730}
731
reed@google.com74ce6f02013-05-22 15:13:18 +0000732static int lcanvas_gc(lua_State* L) {
733 get_ref<SkCanvas>(L, 1)->unref();
734 return 0;
735}
736
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000737const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700738 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000739 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700740 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000741 { "drawRect", lcanvas_drawRect },
742 { "drawOval", lcanvas_drawOval },
743 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000744 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700745 { "drawImageRect", lcanvas_drawImageRect },
reed7a72c672014-11-07 10:23:55 -0800746 { "drawPatch", lcanvas_drawPatch },
reed@google.comfd345872013-05-22 20:53:42 +0000747 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700748 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000749 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800750 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000751 { "getSaveCount", lcanvas_getSaveCount },
752 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000753 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000754#if SK_SUPPORT_GPU
755 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
756#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000757 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700758 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000759 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000760 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000761 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000762 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700763 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700764
765 { "newSurface", lcanvas_newSurface },
766
reed@google.com74ce6f02013-05-22 15:13:18 +0000767 { "__gc", lcanvas_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700768 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +0000769};
770
771///////////////////////////////////////////////////////////////////////////////
772
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000773static int ldocument_beginPage(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -0700774 const SkRect* contentPtr = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000775 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
776 lua2scalar(L, 3),
777 contentPtr));
778 return 1;
779}
780
781static int ldocument_endPage(lua_State* L) {
782 get_ref<SkDocument>(L, 1)->endPage();
783 return 0;
784}
785
786static int ldocument_close(lua_State* L) {
787 get_ref<SkDocument>(L, 1)->close();
788 return 0;
789}
790
791static int ldocument_gc(lua_State* L) {
792 get_ref<SkDocument>(L, 1)->unref();
793 return 0;
794}
795
796static const struct luaL_Reg gSkDocument_Methods[] = {
797 { "beginPage", ldocument_beginPage },
798 { "endPage", ldocument_endPage },
799 { "close", ldocument_close },
800 { "__gc", ldocument_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700801 { nullptr, nullptr }
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000802};
803
804///////////////////////////////////////////////////////////////////////////////
805
reed@google.com74ce6f02013-05-22 15:13:18 +0000806static int lpaint_isAntiAlias(lua_State* L) {
807 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
808 return 1;
809}
810
811static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000812 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000813 return 0;
814}
815
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000816static int lpaint_isDither(lua_State* L) {
817 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
818 return 1;
819}
820
reedbb8a0ab2014-11-03 22:32:07 -0800821static int lpaint_setDither(lua_State* L) {
822 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
823 return 0;
824}
825
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000826static int lpaint_isUnderlineText(lua_State* L) {
827 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
828 return 1;
829}
830
831static int lpaint_isStrikeThruText(lua_State* L) {
832 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
833 return 1;
834}
835
836static int lpaint_isFakeBoldText(lua_State* L) {
837 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
838 return 1;
839}
840
841static int lpaint_isLinearText(lua_State* L) {
842 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
843 return 1;
844}
845
846static int lpaint_isSubpixelText(lua_State* L) {
847 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
848 return 1;
849}
850
reed09a1d672014-10-11 13:13:11 -0700851static int lpaint_setSubpixelText(lua_State* L) {
852 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
853 return 1;
854}
855
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000856static int lpaint_isDevKernText(lua_State* L) {
857 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
858 return 1;
859}
860
861static int lpaint_isLCDRenderText(lua_State* L) {
862 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
863 return 1;
864}
865
reed36c9c112014-11-04 10:58:42 -0800866static int lpaint_setLCDRenderText(lua_State* L) {
867 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
868 return 1;
869}
870
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000871static int lpaint_isEmbeddedBitmapText(lua_State* L) {
872 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
873 return 1;
874}
875
876static int lpaint_isAutohinted(lua_State* L) {
877 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
878 return 1;
879}
880
881static int lpaint_isVerticalText(lua_State* L) {
882 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
883 return 1;
884}
885
reed468b1812014-10-19 11:42:54 -0700886static int lpaint_getAlpha(lua_State* L) {
887 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
888 return 1;
889}
890
891static int lpaint_setAlpha(lua_State* L) {
892 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
893 return 0;
894}
895
reed@google.com74ce6f02013-05-22 15:13:18 +0000896static int lpaint_getColor(lua_State* L) {
897 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
898 return 1;
899}
900
901static int lpaint_setColor(lua_State* L) {
902 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
903 return 0;
904}
905
reed@google.come3823fd2013-05-30 18:55:14 +0000906static int lpaint_getTextSize(lua_State* L) {
907 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
908 return 1;
909}
910
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000911static int lpaint_getTextScaleX(lua_State* L) {
912 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
913 return 1;
914}
915
916static int lpaint_getTextSkewX(lua_State* L) {
917 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
918 return 1;
919}
920
reed@google.come3823fd2013-05-30 18:55:14 +0000921static int lpaint_setTextSize(lua_State* L) {
922 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
923 return 0;
924}
925
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000926static int lpaint_getTypeface(lua_State* L) {
927 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
928 return 1;
929}
930
931static int lpaint_setTypeface(lua_State* L) {
bungeman13b9c952016-05-12 10:09:30 -0700932 get_obj<SkPaint>(L, 1)->setTypeface(sk_ref_sp(get_ref<SkTypeface>(L, 2)));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000933 return 0;
934}
935
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000936static int lpaint_getHinting(lua_State* L) {
937 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
938 return 1;
939}
940
reed93a12152015-03-16 10:08:34 -0700941static int lpaint_getFilterQuality(lua_State* L) {
942 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality());
reed7a72c672014-11-07 10:23:55 -0800943 return 1;
944}
945
reed93a12152015-03-16 10:08:34 -0700946static int lpaint_setFilterQuality(lua_State* L) {
reed7a72c672014-11-07 10:23:55 -0800947 int level = lua2int_def(L, 2, -1);
948 if (level >= 0 && level <= 3) {
reed93a12152015-03-16 10:08:34 -0700949 get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level);
reed7a72c672014-11-07 10:23:55 -0800950 }
951 return 0;
952}
953
reed@google.come3823fd2013-05-30 18:55:14 +0000954static int lpaint_getFontID(lua_State* L) {
955 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
956 SkLua(L).pushU32(SkTypeface::UniqueID(face));
957 return 1;
958}
959
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000960static const struct {
961 const char* fLabel;
962 SkPaint::Align fAlign;
963} gAlignRec[] = {
964 { "left", SkPaint::kLeft_Align },
965 { "center", SkPaint::kCenter_Align },
966 { "right", SkPaint::kRight_Align },
967};
968
969static int lpaint_getTextAlign(lua_State* L) {
970 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
971 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
972 if (gAlignRec[i].fAlign == align) {
973 lua_pushstring(L, gAlignRec[i].fLabel);
974 return 1;
975 }
976 }
977 return 0;
978}
979
980static int lpaint_setTextAlign(lua_State* L) {
981 if (lua_isstring(L, 2)) {
982 size_t len;
983 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000984
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000985 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
986 if (!strcmp(gAlignRec[i].fLabel, label)) {
987 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
988 break;
989 }
990 }
991 }
992 return 0;
993}
994
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000995static int lpaint_getStroke(lua_State* L) {
996 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
997 return 1;
998}
999
1000static int lpaint_setStroke(lua_State* L) {
1001 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +00001002
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001003 if (lua_toboolean(L, 2)) {
1004 style = SkPaint::kStroke_Style;
1005 } else {
1006 style = SkPaint::kFill_Style;
1007 }
1008 get_obj<SkPaint>(L, 1)->setStyle(style);
1009 return 0;
1010}
1011
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001012static int lpaint_getStrokeCap(lua_State* L) {
1013 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
1014 return 1;
1015}
1016
1017static int lpaint_getStrokeJoin(lua_State* L) {
1018 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
1019 return 1;
1020}
1021
1022static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +00001023 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001024 return 1;
1025}
1026
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001027static int lpaint_getStrokeWidth(lua_State* L) {
1028 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
1029 return 1;
1030}
1031
1032static int lpaint_setStrokeWidth(lua_State* L) {
1033 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
1034 return 0;
1035}
1036
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001037static int lpaint_getStrokeMiter(lua_State* L) {
1038 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
1039 return 1;
1040}
1041
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001042static int lpaint_measureText(lua_State* L) {
1043 if (lua_isstring(L, 2)) {
1044 size_t len;
1045 const char* text = lua_tolstring(L, 2, &len);
1046 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
1047 return 1;
1048 }
1049 return 0;
1050}
1051
1052struct FontMetrics {
1053 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
1054 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
1055 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
1056 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
1057 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
1058 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
1059 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
1060 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
1061 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
1062};
1063
1064static int lpaint_getFontMetrics(lua_State* L) {
1065 SkPaint::FontMetrics fm;
1066 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +00001067
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001068 lua_newtable(L);
1069 setfield_scalar(L, "top", fm.fTop);
1070 setfield_scalar(L, "ascent", fm.fAscent);
1071 setfield_scalar(L, "descent", fm.fDescent);
1072 setfield_scalar(L, "bottom", fm.fBottom);
1073 setfield_scalar(L, "leading", fm.fLeading);
1074 SkLua(L).pushScalar(height);
1075 return 2;
1076}
1077
reed@google.com29563872013-07-10 21:23:49 +00001078static int lpaint_getEffects(lua_State* L) {
1079 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001080
reed@google.com29563872013-07-10 21:23:49 +00001081 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -07001082 setfield_bool_if(L, "looper", !!paint->getLooper());
1083 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
1084 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
1085 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
1086 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +00001087 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1088 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -07001089 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +00001090 return 1;
1091}
1092
robertphillips233bab92016-01-21 09:05:32 -08001093static int lpaint_getXfermode(lua_State* L) {
1094 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1095 SkXfermode* xfermode = paint->getXfermode();
1096 if (xfermode) {
1097 push_ref(L, xfermode);
1098 return 1;
1099 }
1100 return 0;
1101}
1102
1103static int lpaint_setXfermode(lua_State* L) {
1104 SkPaint* paint = get_obj<SkPaint>(L, 1);
reedcfb6bdf2016-03-29 11:32:50 -07001105 paint->setXfermode(sk_ref_sp(get_ref<SkXfermode>(L, 2)));
robertphillips233bab92016-01-21 09:05:32 -08001106 return 0;
1107}
1108
reed22a517f2015-12-04 20:45:59 -08001109static int lpaint_getColorFilter(lua_State* L) {
1110 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1111 SkColorFilter* cf = paint->getColorFilter();
1112 if (cf) {
1113 push_ref(L, cf);
1114 return 1;
1115 }
1116 return 0;
1117}
1118
1119static int lpaint_setColorFilter(lua_State* L) {
1120 SkPaint* paint = get_obj<SkPaint>(L, 1);
reedd053ce92016-03-22 10:17:23 -07001121 paint->setColorFilter(sk_ref_sp(get_ref<SkColorFilter>(L, 2)));
reed22a517f2015-12-04 20:45:59 -08001122 return 0;
1123}
1124
reed468b1812014-10-19 11:42:54 -07001125static int lpaint_getImageFilter(lua_State* L) {
1126 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1127 SkImageFilter* imf = paint->getImageFilter();
1128 if (imf) {
1129 push_ref(L, imf);
1130 return 1;
1131 }
1132 return 0;
1133}
1134
1135static int lpaint_setImageFilter(lua_State* L) {
1136 SkPaint* paint = get_obj<SkPaint>(L, 1);
1137 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1138 return 0;
1139}
1140
reed@google.com5fdc9832013-07-24 15:47:52 +00001141static int lpaint_getShader(lua_State* L) {
1142 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1143 SkShader* shader = paint->getShader();
1144 if (shader) {
1145 push_ref(L, shader);
1146 return 1;
1147 }
1148 return 0;
1149}
1150
reed9fbc3f32014-10-21 07:12:58 -07001151static int lpaint_setShader(lua_State* L) {
1152 SkPaint* paint = get_obj<SkPaint>(L, 1);
reedfe630452016-03-25 09:08:00 -07001153 paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2)));
reed9fbc3f32014-10-21 07:12:58 -07001154 return 0;
1155}
1156
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001157static int lpaint_getPathEffect(lua_State* L) {
1158 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1159 SkPathEffect* pe = paint->getPathEffect();
1160 if (pe) {
1161 push_ref(L, pe);
1162 return 1;
1163 }
1164 return 0;
1165}
1166
hstern0b401ce2016-08-02 09:17:59 -07001167static int lpaint_getFillPath(lua_State* L) {
1168 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1169 const SkPath* path = get_obj<SkPath>(L, 2);
1170
1171 SkPath fillpath;
1172 paint->getFillPath(*path, &fillpath);
1173
1174 SkLua lua(L);
1175 lua.pushPath(fillpath);
1176
1177 return 1;
1178}
1179
reed@google.com74ce6f02013-05-22 15:13:18 +00001180static int lpaint_gc(lua_State* L) {
1181 get_obj<SkPaint>(L, 1)->~SkPaint();
1182 return 0;
1183}
1184
1185static const struct luaL_Reg gSkPaint_Methods[] = {
1186 { "isAntiAlias", lpaint_isAntiAlias },
1187 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001188 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001189 { "setDither", lpaint_setDither },
reed93a12152015-03-16 10:08:34 -07001190 { "getFilterQuality", lpaint_getFilterQuality },
1191 { "setFilterQuality", lpaint_setFilterQuality },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001192 { "isUnderlineText", lpaint_isUnderlineText },
1193 { "isStrikeThruText", lpaint_isStrikeThruText },
1194 { "isFakeBoldText", lpaint_isFakeBoldText },
1195 { "isLinearText", lpaint_isLinearText },
1196 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001197 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001198 { "isDevKernText", lpaint_isDevKernText },
1199 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001200 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001201 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1202 { "isAutohinted", lpaint_isAutohinted },
1203 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001204 { "getAlpha", lpaint_getAlpha },
1205 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001206 { "getColor", lpaint_getColor },
1207 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001208 { "getTextSize", lpaint_getTextSize },
1209 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001210 { "getTextScaleX", lpaint_getTextScaleX },
1211 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001212 { "getTypeface", lpaint_getTypeface },
1213 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001214 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001215 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001216 { "getTextAlign", lpaint_getTextAlign },
1217 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001218 { "getStroke", lpaint_getStroke },
1219 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001220 { "getStrokeCap", lpaint_getStrokeCap },
1221 { "getStrokeJoin", lpaint_getStrokeJoin },
1222 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001223 { "getStrokeWidth", lpaint_getStrokeWidth },
1224 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001225 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001226 { "measureText", lpaint_measureText },
1227 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001228 { "getEffects", lpaint_getEffects },
reed22a517f2015-12-04 20:45:59 -08001229 { "getColorFilter", lpaint_getColorFilter },
1230 { "setColorFilter", lpaint_setColorFilter },
reed468b1812014-10-19 11:42:54 -07001231 { "getImageFilter", lpaint_getImageFilter },
1232 { "setImageFilter", lpaint_setImageFilter },
robertphillips233bab92016-01-21 09:05:32 -08001233 { "getXfermode", lpaint_getXfermode },
1234 { "setXfermode", lpaint_setXfermode },
reed@google.com5fdc9832013-07-24 15:47:52 +00001235 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001236 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001237 { "getPathEffect", lpaint_getPathEffect },
hstern0b401ce2016-08-02 09:17:59 -07001238 { "getFillPath", lpaint_getFillPath },
reed@google.com74ce6f02013-05-22 15:13:18 +00001239 { "__gc", lpaint_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001240 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001241};
1242
1243///////////////////////////////////////////////////////////////////////////////
1244
reed@google.com5fdc9832013-07-24 15:47:52 +00001245static const char* mode2string(SkShader::TileMode mode) {
1246 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1247 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1248 return gNames[mode];
1249}
1250
1251static const char* gradtype2string(SkShader::GradientType t) {
1252 static const char* gNames[] = {
1253 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1254 };
1255 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1256 return gNames[t];
1257}
1258
1259static int lshader_isOpaque(lua_State* L) {
1260 SkShader* shader = get_ref<SkShader>(L, 1);
1261 return shader && shader->isOpaque();
1262}
1263
reedf5822822015-08-19 11:46:38 -07001264static int lshader_isABitmap(lua_State* L) {
reed@google.com5fdc9832013-07-24 15:47:52 +00001265 SkShader* shader = get_ref<SkShader>(L, 1);
1266 if (shader) {
1267 SkBitmap bm;
1268 SkMatrix matrix;
1269 SkShader::TileMode modes[2];
reedf5822822015-08-19 11:46:38 -07001270 if (shader->isABitmap(&bm, &matrix, modes)) {
1271 lua_newtable(L);
1272 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1273 setfield_number(L, "width", bm.width());
1274 setfield_number(L, "height", bm.height());
1275 setfield_string(L, "tileX", mode2string(modes[0]));
1276 setfield_string(L, "tileY", mode2string(modes[1]));
1277 return 1;
reed@google.com5fdc9832013-07-24 15:47:52 +00001278 }
1279 }
1280 return 0;
1281}
1282
1283static int lshader_asAGradient(lua_State* L) {
1284 SkShader* shader = get_ref<SkShader>(L, 1);
1285 if (shader) {
1286 SkShader::GradientInfo info;
1287 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001288
reed@google.com5fdc9832013-07-24 15:47:52 +00001289 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001290
reed@google.com5fdc9832013-07-24 15:47:52 +00001291 if (SkShader::kNone_GradientType != t) {
fmenozzib4f254e2016-06-28 14:03:03 -07001292 SkAutoTArray<SkScalar> pos(info.fColorCount);
1293 info.fColorOffsets = pos.get();
1294 shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001295
fmenozzib4f254e2016-06-28 14:03:03 -07001296 lua_newtable(L);
fmenozzi7f2c85e2016-07-12 09:17:39 -07001297 setfield_string(L, "type", gradtype2string(t));
1298 setfield_string(L, "tile", mode2string(info.fTileMode));
1299 setfield_number(L, "colorCount", info.fColorCount);
fmenozzib4f254e2016-06-28 14:03:03 -07001300
1301 lua_newtable(L);
1302 for (int i = 0; i < info.fColorCount; i++) {
1303 // Lua uses 1-based indexing
1304 setarray_scalar(L, i+1, pos[i]);
1305 }
1306 lua_setfield(L, -2, "positions");
1307
reed@google.com5fdc9832013-07-24 15:47:52 +00001308 return 1;
1309 }
1310 }
1311 return 0;
1312}
1313
1314static int lshader_gc(lua_State* L) {
1315 get_ref<SkShader>(L, 1)->unref();
1316 return 0;
1317}
1318
1319static const struct luaL_Reg gSkShader_Methods[] = {
1320 { "isOpaque", lshader_isOpaque },
reedf5822822015-08-19 11:46:38 -07001321 { "isABitmap", lshader_isABitmap },
reed@google.com5fdc9832013-07-24 15:47:52 +00001322 { "asAGradient", lshader_asAGradient },
1323 { "__gc", lshader_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001324 { nullptr, nullptr }
reed@google.com5fdc9832013-07-24 15:47:52 +00001325};
1326
1327///////////////////////////////////////////////////////////////////////////////
1328
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001329static int lpatheffect_asADash(lua_State* L) {
1330 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1331 if (pe) {
1332 SkPathEffect::DashInfo info;
1333 SkPathEffect::DashType dashType = pe->asADash(&info);
1334 if (SkPathEffect::kDash_DashType == dashType) {
1335 SkAutoTArray<SkScalar> intervals(info.fCount);
1336 info.fIntervals = intervals.get();
1337 pe->asADash(&info);
1338 SkLua(L).pushDash(info);
1339 return 1;
1340 }
1341 }
1342 return 0;
1343}
1344
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001345static int lpatheffect_gc(lua_State* L) {
1346 get_ref<SkPathEffect>(L, 1)->unref();
1347 return 0;
1348}
1349
1350static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001351 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001352 { "__gc", lpatheffect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001353 { nullptr, nullptr }
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001354};
1355
1356///////////////////////////////////////////////////////////////////////////////
1357
robertphillips233bab92016-01-21 09:05:32 -08001358static int lpxfermode_getTypeName(lua_State* L) {
1359 lua_pushstring(L, get_ref<SkXfermode>(L, 1)->getTypeName());
1360 return 1;
1361}
1362
1363static int lpxfermode_gc(lua_State* L) {
1364 get_ref<SkXfermode>(L, 1)->unref();
1365 return 0;
1366}
1367
1368static const struct luaL_Reg gSkXfermode_Methods[] = {
1369 { "getTypeName", lpxfermode_getTypeName },
1370 { "__gc", lpxfermode_gc },
1371 { nullptr, nullptr }
1372};
1373
1374///////////////////////////////////////////////////////////////////////////////
1375
reed22a517f2015-12-04 20:45:59 -08001376static int lpcolorfilter_gc(lua_State* L) {
1377 get_ref<SkColorFilter>(L, 1)->unref();
1378 return 0;
1379}
1380
1381static const struct luaL_Reg gSkColorFilter_Methods[] = {
1382 { "__gc", lpcolorfilter_gc },
1383 { nullptr, nullptr }
1384};
1385
1386///////////////////////////////////////////////////////////////////////////////
1387
reed468b1812014-10-19 11:42:54 -07001388static int lpimagefilter_gc(lua_State* L) {
1389 get_ref<SkImageFilter>(L, 1)->unref();
1390 return 0;
1391}
1392
1393static const struct luaL_Reg gSkImageFilter_Methods[] = {
1394 { "__gc", lpimagefilter_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001395 { nullptr, nullptr }
reed468b1812014-10-19 11:42:54 -07001396};
1397
1398///////////////////////////////////////////////////////////////////////////////
1399
humper@google.com2815c192013-07-10 22:42:30 +00001400static int lmatrix_getType(lua_State* L) {
1401 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001402
humper@google.com2815c192013-07-10 22:42:30 +00001403 lua_newtable(L);
1404 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1405 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1406 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1407 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1408 return 1;
1409}
1410
humper@google.com0f48ee02013-07-26 15:23:43 +00001411static int lmatrix_getScaleX(lua_State* L) {
1412 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1413 return 1;
1414}
1415
1416static int lmatrix_getScaleY(lua_State* L) {
1417 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1418 return 1;
1419}
1420
1421static int lmatrix_getTranslateX(lua_State* L) {
1422 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1423 return 1;
1424}
1425
1426static int lmatrix_getTranslateY(lua_State* L) {
1427 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1428 return 1;
1429}
1430
reed7a72c672014-11-07 10:23:55 -08001431static int lmatrix_invert(lua_State* L) {
1432 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1433 return 1;
1434}
1435
1436static int lmatrix_mapXY(lua_State* L) {
1437 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1438 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1439 lua_pushnumber(L, pt.x());
1440 lua_pushnumber(L, pt.y());
1441 return 2;
1442}
1443
reedbdc49ae2014-10-14 09:34:52 -07001444static int lmatrix_setRectToRect(lua_State* L) {
1445 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1446 SkRect srcR, dstR;
1447 lua2rect(L, 2, &srcR);
1448 lua2rect(L, 3, &dstR);
1449 const char* scaleToFitStr = lua_tostring(L, 4);
1450 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1451
1452 if (scaleToFitStr) {
1453 const struct {
1454 const char* fName;
1455 SkMatrix::ScaleToFit fScaleToFit;
1456 } rec[] = {
1457 { "fill", SkMatrix::kFill_ScaleToFit },
1458 { "start", SkMatrix::kStart_ScaleToFit },
1459 { "center", SkMatrix::kCenter_ScaleToFit },
1460 { "end", SkMatrix::kEnd_ScaleToFit },
1461 };
1462
1463 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1464 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1465 scaleToFit = rec[i].fScaleToFit;
1466 break;
1467 }
1468 }
1469 }
1470
1471 matrix->setRectToRect(srcR, dstR, scaleToFit);
1472 return 0;
1473}
1474
humper@google.com2815c192013-07-10 22:42:30 +00001475static const struct luaL_Reg gSkMatrix_Methods[] = {
1476 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001477 { "getScaleX", lmatrix_getScaleX },
1478 { "getScaleY", lmatrix_getScaleY },
1479 { "getTranslateX", lmatrix_getTranslateX },
1480 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001481 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001482 { "invert", lmatrix_invert },
1483 { "mapXY", lmatrix_mapXY },
halcanary96fcdcc2015-08-27 07:41:13 -07001484 { nullptr, nullptr }
humper@google.com2815c192013-07-10 22:42:30 +00001485};
1486
1487///////////////////////////////////////////////////////////////////////////////
1488
reed@google.com74ce6f02013-05-22 15:13:18 +00001489static int lpath_getBounds(lua_State* L) {
1490 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1491 return 1;
1492}
1493
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001494static const char* fill_type_to_str(SkPath::FillType fill) {
1495 switch (fill) {
1496 case SkPath::kEvenOdd_FillType:
1497 return "even-odd";
1498 case SkPath::kWinding_FillType:
1499 return "winding";
1500 case SkPath::kInverseEvenOdd_FillType:
1501 return "inverse-even-odd";
1502 case SkPath::kInverseWinding_FillType:
1503 return "inverse-winding";
1504 }
1505 return "unknown";
1506}
1507
1508static int lpath_getFillType(lua_State* L) {
1509 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1510 SkLua(L).pushString(fill_type_to_str(fill));
1511 return 1;
1512}
1513
1514static SkString segment_masks_to_str(uint32_t segmentMasks) {
1515 SkString result;
1516 bool first = true;
1517 if (SkPath::kLine_SegmentMask & segmentMasks) {
1518 result.append("line");
1519 first = false;
1520 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1521 }
1522 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1523 if (!first) {
1524 result.append(" ");
1525 }
1526 result.append("quad");
1527 first = false;
1528 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1529 }
1530 if (SkPath::kConic_SegmentMask & segmentMasks) {
1531 if (!first) {
1532 result.append(" ");
1533 }
1534 result.append("conic");
1535 first = false;
1536 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1537 }
1538 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1539 if (!first) {
1540 result.append(" ");
1541 }
1542 result.append("cubic");
1543 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1544 }
1545 SkASSERT(0 == segmentMasks);
1546 return result;
1547}
1548
krajcevski95498ed2014-08-18 08:02:33 -07001549static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001550 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1551 SkLua(L).pushString(segment_masks_to_str(segMasks));
1552 return 1;
1553}
1554
1555static int lpath_isConvex(lua_State* L) {
1556 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1557 SkLua(L).pushBool(isConvex);
1558 return 1;
1559}
1560
reed@google.com74ce6f02013-05-22 15:13:18 +00001561static int lpath_isEmpty(lua_State* L) {
1562 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1563 return 1;
1564}
1565
1566static int lpath_isRect(lua_State* L) {
1567 SkRect r;
1568 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1569 int ret_count = 1;
1570 lua_pushboolean(L, pred);
1571 if (pred) {
1572 SkLua(L).pushRect(r);
1573 ret_count += 1;
1574 }
1575 return ret_count;
1576}
1577
1578static const char* dir2string(SkPath::Direction dir) {
1579 static const char* gStr[] = {
1580 "unknown", "cw", "ccw"
1581 };
1582 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1583 return gStr[dir];
1584}
1585
caryclark95bc5f32015-04-08 08:34:15 -07001586static int lpath_isNestedFillRects(lua_State* L) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001587 SkRect rects[2];
1588 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001589 bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
reed@google.com74ce6f02013-05-22 15:13:18 +00001590 int ret_count = 1;
1591 lua_pushboolean(L, pred);
1592 if (pred) {
1593 SkLua lua(L);
1594 lua.pushRect(rects[0]);
1595 lua.pushRect(rects[1]);
1596 lua_pushstring(L, dir2string(dirs[0]));
1597 lua_pushstring(L, dir2string(dirs[0]));
1598 ret_count += 4;
1599 }
1600 return ret_count;
1601}
1602
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001603static int lpath_countPoints(lua_State* L) {
1604 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1605 return 1;
1606}
1607
hstern0b401ce2016-08-02 09:17:59 -07001608static int lpath_getVerbs(lua_State* L) {
1609 const SkPath* path = get_obj<SkPath>(L, 1);
1610 SkPath::Iter iter(*path, false);
1611 SkPoint pts[4];
1612
1613 lua_newtable(L);
1614
1615 bool done = false;
1616 int i = 0;
1617 do {
1618 switch (iter.next(pts, true)) {
1619 case SkPath::kMove_Verb:
1620 setarray_string(L, ++i, "move");
1621 break;
1622 case SkPath::kClose_Verb:
1623 setarray_string(L, ++i, "close");
1624 break;
1625 case SkPath::kLine_Verb:
1626 setarray_string(L, ++i, "line");
1627 break;
1628 case SkPath::kQuad_Verb:
1629 setarray_string(L, ++i, "quad");
1630 break;
1631 case SkPath::kConic_Verb:
1632 setarray_string(L, ++i, "conic");
1633 break;
1634 case SkPath::kCubic_Verb:
1635 setarray_string(L, ++i, "cubic");
1636 break;
1637 case SkPath::kDone_Verb:
1638 setarray_string(L, ++i, "done");
1639 done = true;
1640 break;
1641 }
1642 } while (!done);
1643
1644 return 1;
1645}
1646
reed@google.com74ce6f02013-05-22 15:13:18 +00001647static int lpath_reset(lua_State* L) {
1648 get_obj<SkPath>(L, 1)->reset();
1649 return 0;
1650}
1651
1652static int lpath_moveTo(lua_State* L) {
1653 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1654 return 0;
1655}
1656
1657static int lpath_lineTo(lua_State* L) {
1658 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1659 return 0;
1660}
1661
1662static int lpath_quadTo(lua_State* L) {
1663 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1664 lua2scalar(L, 4), lua2scalar(L, 5));
1665 return 0;
1666}
1667
1668static int lpath_cubicTo(lua_State* L) {
1669 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1670 lua2scalar(L, 4), lua2scalar(L, 5),
1671 lua2scalar(L, 6), lua2scalar(L, 7));
1672 return 0;
1673}
1674
1675static int lpath_close(lua_State* L) {
1676 get_obj<SkPath>(L, 1)->close();
1677 return 0;
1678}
1679
1680static int lpath_gc(lua_State* L) {
1681 get_obj<SkPath>(L, 1)->~SkPath();
1682 return 0;
1683}
1684
1685static const struct luaL_Reg gSkPath_Methods[] = {
1686 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001687 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001688 { "getSegmentTypes", lpath_getSegmentTypes },
hstern0b401ce2016-08-02 09:17:59 -07001689 { "getVerbs", lpath_getVerbs },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001690 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001691 { "isEmpty", lpath_isEmpty },
1692 { "isRect", lpath_isRect },
caryclark95bc5f32015-04-08 08:34:15 -07001693 { "isNestedFillRects", lpath_isNestedFillRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001694 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001695 { "reset", lpath_reset },
1696 { "moveTo", lpath_moveTo },
1697 { "lineTo", lpath_lineTo },
1698 { "quadTo", lpath_quadTo },
1699 { "cubicTo", lpath_cubicTo },
1700 { "close", lpath_close },
1701 { "__gc", lpath_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001702 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001703};
1704
1705///////////////////////////////////////////////////////////////////////////////
1706
1707static const char* rrect_type(const SkRRect& rr) {
1708 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001709 case SkRRect::kEmpty_Type: return "empty";
1710 case SkRRect::kRect_Type: return "rect";
1711 case SkRRect::kOval_Type: return "oval";
1712 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001713 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001714 case SkRRect::kComplex_Type: return "complex";
1715 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001716 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001717 return "";
1718}
1719
1720static int lrrect_rect(lua_State* L) {
1721 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1722 return 1;
1723}
1724
1725static int lrrect_type(lua_State* L) {
1726 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1727 return 1;
1728}
1729
1730static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001731 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001732 SkVector v;
1733 if (corner < 0 || corner > 3) {
1734 SkDebugf("bad corner index %d", corner);
1735 v.set(0, 0);
1736 } else {
1737 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1738 }
1739 lua_pushnumber(L, v.fX);
1740 lua_pushnumber(L, v.fY);
1741 return 2;
1742}
1743
1744static int lrrect_gc(lua_State* L) {
1745 get_obj<SkRRect>(L, 1)->~SkRRect();
1746 return 0;
1747}
1748
1749static const struct luaL_Reg gSkRRect_Methods[] = {
1750 { "rect", lrrect_rect },
1751 { "type", lrrect_type },
1752 { "radii", lrrect_radii },
1753 { "__gc", lrrect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001754 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001755};
1756
1757///////////////////////////////////////////////////////////////////////////////
1758
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001759static int limage_width(lua_State* L) {
1760 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1761 return 1;
1762}
1763
1764static int limage_height(lua_State* L) {
1765 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1766 return 1;
1767}
1768
reed7a72c672014-11-07 10:23:55 -08001769static int limage_newShader(lua_State* L) {
1770 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
halcanary96fcdcc2015-08-27 07:41:13 -07001771 const SkMatrix* localM = nullptr;
reed5671c5b2016-03-09 14:47:34 -08001772 push_ref(L, get_ref<SkImage>(L, 1)->makeShader(tmode, tmode, localM));
reed7a72c672014-11-07 10:23:55 -08001773 return 1;
1774}
1775
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001776static int limage_gc(lua_State* L) {
1777 get_ref<SkImage>(L, 1)->unref();
1778 return 0;
1779}
1780
1781static const struct luaL_Reg gSkImage_Methods[] = {
1782 { "width", limage_width },
1783 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001784 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001785 { "__gc", limage_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001786 { nullptr, nullptr }
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001787};
1788
1789///////////////////////////////////////////////////////////////////////////////
1790
reed485557f2014-10-12 10:36:47 -07001791static int lsurface_width(lua_State* L) {
1792 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1793 return 1;
1794}
1795
1796static int lsurface_height(lua_State* L) {
1797 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1798 return 1;
1799}
1800
1801static int lsurface_getCanvas(lua_State* L) {
1802 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001803 if (nullptr == canvas) {
reed485557f2014-10-12 10:36:47 -07001804 lua_pushnil(L);
1805 } else {
1806 push_ref(L, canvas);
1807 // note: we don't unref canvas, since getCanvas did not ref it.
1808 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1809 // the real owner (the surface) go away, but still hold onto the canvas?
1810 // *really* we want to sort of ref the surface again, but have the native object
1811 // know that it is supposed to be treated as a canvas...
1812 }
1813 return 1;
1814}
1815
1816static int lsurface_newImageSnapshot(lua_State* L) {
reed9ce9d672016-03-17 10:51:11 -07001817 sk_sp<SkImage> image = get_ref<SkSurface>(L, 1)->makeImageSnapshot();
1818 if (!image) {
reed485557f2014-10-12 10:36:47 -07001819 lua_pushnil(L);
1820 } else {
reed9ce9d672016-03-17 10:51:11 -07001821 push_ref(L, image);
reed485557f2014-10-12 10:36:47 -07001822 }
1823 return 1;
1824}
1825
1826static int lsurface_newSurface(lua_State* L) {
1827 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001828 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001829 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -07001830 auto surface = get_ref<SkSurface>(L, 1)->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -07001831 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07001832 lua_pushnil(L);
1833 } else {
reede8f30622016-03-23 18:59:25 -07001834 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -07001835 }
1836 return 1;
1837}
1838
1839static int lsurface_gc(lua_State* L) {
1840 get_ref<SkSurface>(L, 1)->unref();
1841 return 0;
1842}
1843
1844static const struct luaL_Reg gSkSurface_Methods[] = {
1845 { "width", lsurface_width },
1846 { "height", lsurface_height },
1847 { "getCanvas", lsurface_getCanvas },
1848 { "newImageSnapshot", lsurface_newImageSnapshot },
1849 { "newSurface", lsurface_newSurface },
1850 { "__gc", lsurface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001851 { nullptr, nullptr }
reed485557f2014-10-12 10:36:47 -07001852};
1853
1854///////////////////////////////////////////////////////////////////////////////
1855
reed96affcd2014-10-13 12:38:04 -07001856static int lpicturerecorder_beginRecording(lua_State* L) {
1857 const SkScalar w = lua2scalar_def(L, 2, -1);
1858 const SkScalar h = lua2scalar_def(L, 3, -1);
1859 if (w <= 0 || h <= 0) {
1860 lua_pushnil(L);
1861 return 1;
1862 }
1863
1864 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
halcanary96fcdcc2015-08-27 07:41:13 -07001865 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001866 lua_pushnil(L);
1867 return 1;
1868 }
1869
1870 push_ref(L, canvas);
1871 return 1;
1872}
1873
1874static int lpicturerecorder_getCanvas(lua_State* L) {
1875 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001876 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001877 lua_pushnil(L);
1878 return 1;
1879 }
1880 push_ref(L, canvas);
1881 return 1;
1882}
1883
1884static int lpicturerecorder_endRecording(lua_State* L) {
reedca2622b2016-03-18 07:25:55 -07001885 sk_sp<SkPicture> pic = get_obj<SkPictureRecorder>(L, 1)->finishRecordingAsPicture();
1886 if (!pic) {
reed96affcd2014-10-13 12:38:04 -07001887 lua_pushnil(L);
1888 return 1;
1889 }
reedca2622b2016-03-18 07:25:55 -07001890 push_ref(L, std::move(pic));
reed96affcd2014-10-13 12:38:04 -07001891 return 1;
1892}
1893
1894static int lpicturerecorder_gc(lua_State* L) {
1895 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1896 return 0;
1897}
1898
1899static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1900 { "beginRecording", lpicturerecorder_beginRecording },
1901 { "getCanvas", lpicturerecorder_getCanvas },
1902 { "endRecording", lpicturerecorder_endRecording },
1903 { "__gc", lpicturerecorder_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001904 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001905};
1906
1907///////////////////////////////////////////////////////////////////////////////
1908
1909static int lpicture_width(lua_State* L) {
1910 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1911 return 1;
1912}
1913
1914static int lpicture_height(lua_State* L) {
1915 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1916 return 1;
1917}
1918
1919static int lpicture_gc(lua_State* L) {
1920 get_ref<SkPicture>(L, 1)->unref();
1921 return 0;
1922}
1923
1924static const struct luaL_Reg gSkPicture_Methods[] = {
1925 { "width", lpicture_width },
1926 { "height", lpicture_height },
1927 { "__gc", lpicture_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001928 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001929};
1930
1931///////////////////////////////////////////////////////////////////////////////
1932
reed1b6ab442014-11-03 19:55:41 -08001933static int ltextblob_bounds(lua_State* L) {
1934 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1935 return 1;
1936}
1937
1938static int ltextblob_gc(lua_State* L) {
1939 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1940 return 0;
1941}
1942
1943static const struct luaL_Reg gSkTextBlob_Methods[] = {
1944 { "bounds", ltextblob_bounds },
1945 { "__gc", ltextblob_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001946 { nullptr, nullptr }
reed1b6ab442014-11-03 19:55:41 -08001947};
1948
1949///////////////////////////////////////////////////////////////////////////////
1950
reed36c9c112014-11-04 10:58:42 -08001951static int ltypeface_getFamilyName(lua_State* L) {
1952 SkString str;
1953 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1954 lua_pushstring(L, str.c_str());
1955 return 1;
1956}
1957
1958static int ltypeface_getStyle(lua_State* L) {
1959 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1960 return 1;
1961}
1962
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001963static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001964 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001965 return 0;
1966}
1967
1968static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001969 { "getFamilyName", ltypeface_getFamilyName },
1970 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001971 { "__gc", ltypeface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001972 { nullptr, nullptr }
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001973};
1974
1975///////////////////////////////////////////////////////////////////////////////
1976
reed@google.com74ce6f02013-05-22 15:13:18 +00001977class AutoCallLua {
1978public:
1979 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1980 lua_getglobal(L, func);
1981 if (!lua_isfunction(L, -1)) {
1982 int t = lua_type(L, -1);
1983 SkDebugf("--- expected function %d\n", t);
1984 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001985
reed@google.com74ce6f02013-05-22 15:13:18 +00001986 lua_newtable(L);
1987 setfield_string(L, "verb", verb);
1988 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001989
reed@google.com74ce6f02013-05-22 15:13:18 +00001990 ~AutoCallLua() {
1991 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1992 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1993 }
1994 lua_settop(fL, -1);
1995 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001996
reed@google.com74ce6f02013-05-22 15:13:18 +00001997private:
1998 lua_State* fL;
1999};
2000
2001#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
2002
2003///////////////////////////////////////////////////////////////////////////////
2004
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002005static int lsk_newDocumentPDF(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07002006 const char* file = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002007 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002008 file = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002009 }
2010
halcanary676ab682016-05-03 12:10:04 -07002011 sk_sp<SkDocument> doc = SkDocument::MakePDF(file);
halcanary96fcdcc2015-08-27 07:41:13 -07002012 if (nullptr == doc) {
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002013 // do I need to push a nil on the stack and return 1?
2014 return 0;
2015 } else {
halcanary676ab682016-05-03 12:10:04 -07002016 push_ref(L, std::move(doc));
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002017 return 1;
2018 }
2019}
2020
reed468b1812014-10-19 11:42:54 -07002021static int lsk_newBlurImageFilter(lua_State* L) {
2022 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
2023 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
robertphillips6e7025a2016-04-04 04:31:25 -07002024 sk_sp<SkImageFilter> imf(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr));
2025 if (!imf) {
reed468b1812014-10-19 11:42:54 -07002026 lua_pushnil(L);
2027 } else {
robertphillips6e7025a2016-04-04 04:31:25 -07002028 push_ref(L, std::move(imf));
reed9fbc3f32014-10-21 07:12:58 -07002029 }
2030 return 1;
2031}
2032
2033static int lsk_newLinearGradient(lua_State* L) {
2034 SkScalar x0 = lua2scalar_def(L, 1, 0);
2035 SkScalar y0 = lua2scalar_def(L, 2, 0);
2036 SkColor c0 = lua2color(L, 3);
2037 SkScalar x1 = lua2scalar_def(L, 4, 0);
2038 SkScalar y1 = lua2scalar_def(L, 5, 0);
2039 SkColor c1 = lua2color(L, 6);
2040
2041 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
2042 SkColor colors[] = { c0, c1 };
robertphillips6e7025a2016-04-04 04:31:25 -07002043 sk_sp<SkShader> s(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
2044 SkShader::kClamp_TileMode));
reed2ad1aa62016-03-09 09:50:50 -08002045 if (!s) {
reed9fbc3f32014-10-21 07:12:58 -07002046 lua_pushnil(L);
2047 } else {
reed2ad1aa62016-03-09 09:50:50 -08002048 push_ref(L, std::move(s));
reed468b1812014-10-19 11:42:54 -07002049 }
2050 return 1;
2051}
2052
reedbdc49ae2014-10-14 09:34:52 -07002053static int lsk_newMatrix(lua_State* L) {
2054 push_new<SkMatrix>(L)->reset();
2055 return 1;
2056}
2057
reed@google.com3597b732013-05-22 20:12:50 +00002058static int lsk_newPaint(lua_State* L) {
2059 push_new<SkPaint>(L);
2060 return 1;
2061}
2062
2063static int lsk_newPath(lua_State* L) {
2064 push_new<SkPath>(L);
2065 return 1;
2066}
2067
reed96affcd2014-10-13 12:38:04 -07002068static int lsk_newPictureRecorder(lua_State* L) {
2069 push_new<SkPictureRecorder>(L);
2070 return 1;
2071}
2072
reed@google.com3597b732013-05-22 20:12:50 +00002073static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07002074 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00002075 return 1;
2076}
2077
reed1b6ab442014-11-03 19:55:41 -08002078#include "SkTextBox.h"
2079// Sk.newTextBlob(text, rect, paint)
2080static int lsk_newTextBlob(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07002081 const char* text = lua_tolstring(L, 1, nullptr);
reed1b6ab442014-11-03 19:55:41 -08002082 SkRect bounds;
2083 lua2rect(L, 2, &bounds);
2084 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
2085
2086 SkTextBox box;
2087 box.setMode(SkTextBox::kLineBreak_Mode);
2088 box.setBox(bounds);
2089 box.setText(text, strlen(text), paint);
2090
2091 SkScalar newBottom;
2092 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
2093 push_ref<SkTextBlob>(L, blob);
2094 SkLua(L).pushScalar(newBottom);
2095 return 2;
2096}
2097
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002098static int lsk_newTypeface(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07002099 const char* name = nullptr;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002100 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00002101
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002102 int count = lua_gettop(L);
2103 if (count > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002104 name = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002105 if (count > 1 && lua_isnumber(L, 2)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002106 style = lua_tointegerx(L, 2, nullptr) & SkTypeface::kBoldItalic;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002107 }
2108 }
2109
mbocee6a9912016-05-31 11:42:36 -07002110 sk_sp<SkTypeface> face(SkTypeface::MakeFromName(name, SkFontStyle::FromOldStyle(style)));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002111// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
halcanary96fcdcc2015-08-27 07:41:13 -07002112 if (nullptr == face) {
bungeman13b9c952016-05-12 10:09:30 -07002113 face = SkTypeface::MakeDefault();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002114 }
bungeman13b9c952016-05-12 10:09:30 -07002115 push_ref(L, std::move(face));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002116 return 1;
2117}
reed@google.com3597b732013-05-22 20:12:50 +00002118
reed485557f2014-10-12 10:36:47 -07002119static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08002120 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07002121 int height = lua2int_def(L, 2, 0);
2122 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
robertphillips702edbd2015-06-23 06:26:08 -07002123 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -07002124 auto surface = SkSurface::MakeRaster(info, &props);
halcanary96fcdcc2015-08-27 07:41:13 -07002125 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07002126 lua_pushnil(L);
2127 } else {
reede8f30622016-03-23 18:59:25 -07002128 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -07002129 }
2130 return 1;
2131}
2132
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002133static int lsk_loadImage(lua_State* L) {
2134 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002135 const char* name = lua_tolstring(L, 1, nullptr);
reed9ce9d672016-03-17 10:51:11 -07002136 sk_sp<SkData> data(SkData::MakeFromFileName(name));
2137 if (data) {
2138 auto image = SkImage::MakeFromEncoded(std::move(data));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002139 if (image) {
reed9ce9d672016-03-17 10:51:11 -07002140 push_ref(L, std::move(image));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002141 return 1;
2142 }
2143 }
2144 }
2145 return 0;
2146}
2147
reed@google.com3597b732013-05-22 20:12:50 +00002148static void register_Sk(lua_State* L) {
2149 lua_newtable(L);
2150 lua_pushvalue(L, -1);
2151 lua_setglobal(L, "Sk");
2152 // the Sk table is still on top
2153
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002154 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002155 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002156 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002157 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002158 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002159 setfield_function(L, "newPaint", lsk_newPaint);
2160 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002161 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002162 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002163 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002164 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002165 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00002166 lua_pop(L, 1); // pop off the Sk table
2167}
2168
reed@google.com74ce6f02013-05-22 15:13:18 +00002169#define REG_CLASS(L, C) \
2170 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002171 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002172 lua_pushvalue(L, -1); \
2173 lua_setfield(L, -2, "__index"); \
2174 luaL_setfuncs(L, g##C##_Methods, 0); \
2175 lua_pop(L, 1); /* pop off the meta-table */ \
2176 } while (0)
2177
2178void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002179 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002180 REG_CLASS(L, SkCanvas);
reed22a517f2015-12-04 20:45:59 -08002181 REG_CLASS(L, SkColorFilter);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002182 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002183 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002184 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002185 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002186 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002187 REG_CLASS(L, SkPath);
2188 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002189 REG_CLASS(L, SkPicture);
2190 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002191 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002192 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002193 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002194 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002195 REG_CLASS(L, SkTypeface);
robertphillips233bab92016-01-21 09:05:32 -08002196 REG_CLASS(L, SkXfermode);
reed@google.com74ce6f02013-05-22 15:13:18 +00002197}
zachr@google.com28c27c82013-06-20 17:15:05 +00002198
reed@google.com7bce9982013-06-20 17:40:21 +00002199extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002200extern "C" int luaopen_skia(lua_State* L) {
2201 SkLua::Load(L);
2202 return 0;
2203}