blob: 11d9b2938c66afab3b6a837086a78dfdbeb58f03 [file] [log] [blame]
reed@google.com74ce6f02013-05-22 15:13:18 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkLua.h"
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00009
10#if SK_SUPPORT_GPU
11#include "GrReducedClip.h"
12#endif
13
reed468b1812014-10-19 11:42:54 -070014#include "SkBlurImageFilter.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000015#include "SkCanvas.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000016#include "SkData.h"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000017#include "SkDocument.h"
reed9fbc3f32014-10-21 07:12:58 -070018#include "SkGradientShader.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000019#include "SkImage.h"
20#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000021#include "SkPaint.h"
22#include "SkPath.h"
reed96affcd2014-10-13 12:38:04 -070023#include "SkPictureRecorder.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000024#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000025#include "SkRRect.h"
26#include "SkString.h"
reed485557f2014-10-12 10:36:47 -070027#include "SkSurface.h"
fmalitab7425172014-08-26 07:56:44 -070028#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000029#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000030
31extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000032 #include "lua.h"
33 #include "lualib.h"
34 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000035}
36
reed@google.comfd345872013-05-22 20:53:42 +000037// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000038template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000039#define DEF_MTNAME(T) \
40 template <> const char* get_mtname<T>() { \
41 return #T "_LuaMetaTableName"; \
42 }
43
44DEF_MTNAME(SkCanvas)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000045DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000046DEF_MTNAME(SkImage)
reed468b1812014-10-19 11:42:54 -070047DEF_MTNAME(SkImageFilter)
reed@google.comfd345872013-05-22 20:53:42 +000048DEF_MTNAME(SkMatrix)
49DEF_MTNAME(SkRRect)
50DEF_MTNAME(SkPath)
51DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000052DEF_MTNAME(SkPathEffect)
reed96affcd2014-10-13 12:38:04 -070053DEF_MTNAME(SkPicture)
54DEF_MTNAME(SkPictureRecorder)
reed@google.com5fdc9832013-07-24 15:47:52 +000055DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070056DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070057DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000058DEF_MTNAME(SkTypeface)
reed@google.com74ce6f02013-05-22 15:13:18 +000059
reed@google.com3597b732013-05-22 20:12:50 +000060template <typename T> T* push_new(lua_State* L) {
61 T* addr = (T*)lua_newuserdata(L, sizeof(T));
62 new (addr) T;
63 luaL_getmetatable(L, get_mtname<T>());
64 lua_setmetatable(L, -2);
65 return addr;
66}
reed@google.com74ce6f02013-05-22 15:13:18 +000067
68template <typename T> void push_obj(lua_State* L, const T& obj) {
69 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000070 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000071 lua_setmetatable(L, -2);
72}
73
reed9fbc3f32014-10-21 07:12:58 -070074template <typename T> T* push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000075 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000076 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000077 lua_setmetatable(L, -2);
reed9fbc3f32014-10-21 07:12:58 -070078 return ref;
reed@google.com74ce6f02013-05-22 15:13:18 +000079}
80
81template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000082 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000083}
84
85template <typename T> T* get_obj(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
reed@google.com88c9ec92013-05-22 15:43:21 +000089static bool lua2bool(lua_State* L, int index) {
90 return !!lua_toboolean(L, index);
91}
92
reed@google.com74ce6f02013-05-22 15:13:18 +000093///////////////////////////////////////////////////////////////////////////////
94
reed@google.com3597b732013-05-22 20:12:50 +000095SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
96 fL = luaL_newstate();
97 luaL_openlibs(fL);
98 SkLua::Load(fL);
99}
100
101SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
102
103SkLua::~SkLua() {
104 if (fWeOwnL) {
105 if (fTermCode.size() > 0) {
106 lua_getglobal(fL, fTermCode.c_str());
107 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
108 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
109 }
110 }
111 lua_close(fL);
112 }
113}
114
115bool SkLua::runCode(const char code[]) {
116 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
117 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000118 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000119 return false;
120 }
121 return true;
122}
123
124bool SkLua::runCode(const void* code, size_t size) {
125 SkString str((const char*)code, size);
126 return this->runCode(str.c_str());
127}
128
129///////////////////////////////////////////////////////////////////////////////
130
131#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
132
reed@google.com29563872013-07-10 21:23:49 +0000133static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
134 if (pred) {
135 lua_pushboolean(L, true);
136 lua_setfield(L, -2, key);
137 }
138}
139
reed@google.com74ce6f02013-05-22 15:13:18 +0000140static void setfield_string(lua_State* L, const char key[], const char value[]) {
141 lua_pushstring(L, value);
142 lua_setfield(L, -2, key);
143}
144
145static void setfield_number(lua_State* L, const char key[], double value) {
146 lua_pushnumber(L, value);
147 lua_setfield(L, -2, key);
148}
149
humper@google.com2815c192013-07-10 22:42:30 +0000150static void setfield_boolean(lua_State* L, const char key[], bool value) {
151 lua_pushboolean(L, value);
152 lua_setfield(L, -2, key);
153}
154
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000155static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
156 setfield_number(L, key, SkScalarToLua(value));
157}
158
reed@google.com3597b732013-05-22 20:12:50 +0000159static void setfield_function(lua_State* L,
160 const char key[], lua_CFunction value) {
161 lua_pushcfunction(L, value);
162 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000163}
164
reed7a72c672014-11-07 10:23:55 -0800165static int lua2int_def(lua_State* L, int index, int defaultValue) {
166 if (lua_isnumber(L, index)) {
167 return (int)lua_tonumber(L, index);
168 } else {
169 return defaultValue;
170 }
171}
172
173static SkScalar lua2scalar(lua_State* L, int index) {
174 SkASSERT(lua_isnumber(L, index));
175 return SkLuaToScalar(lua_tonumber(L, index));
176}
177
178static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
179 if (lua_isnumber(L, index)) {
180 return SkLuaToScalar(lua_tonumber(L, index));
181 } else {
182 return defaultValue;
183 }
184}
185
186static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
187 SkASSERT(lua_istable(L, stackIndex));
188 lua_rawgeti(L, stackIndex, arrayIndex);
mtklein8aacf202014-12-18 13:29:54 -0800189
reed7a72c672014-11-07 10:23:55 -0800190 SkScalar value = lua2scalar(L, -1);
191 lua_pop(L, 1);
192 return value;
193}
194
195static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
196 for (int i = 0; i < count; ++i) {
197 dst[i] = getarray_scalar(L, stackIndex, i + 1);
198 }
199}
200
201static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
202 getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
203}
204
reed@google.come3823fd2013-05-30 18:55:14 +0000205static void setarray_number(lua_State* L, int index, double value) {
206 lua_pushnumber(L, value);
207 lua_rawseti(L, -2, index);
208}
209
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000210static void setarray_scalar(lua_State* L, int index, SkScalar value) {
211 setarray_number(L, index, SkScalarToLua(value));
212}
213
reed@google.com74ce6f02013-05-22 15:13:18 +0000214void SkLua::pushBool(bool value, const char key[]) {
215 lua_pushboolean(fL, value);
216 CHECK_SETFIELD(key);
217}
218
219void SkLua::pushString(const char str[], const char key[]) {
220 lua_pushstring(fL, str);
221 CHECK_SETFIELD(key);
222}
223
reed@google.come3823fd2013-05-30 18:55:14 +0000224void SkLua::pushString(const char str[], size_t length, const char key[]) {
225 // TODO: how to do this w/o making a copy?
226 SkString s(str, length);
227 lua_pushstring(fL, s.c_str());
228 CHECK_SETFIELD(key);
229}
230
reed@google.com74ce6f02013-05-22 15:13:18 +0000231void SkLua::pushString(const SkString& str, const char key[]) {
232 lua_pushstring(fL, str.c_str());
233 CHECK_SETFIELD(key);
234}
235
236void SkLua::pushColor(SkColor color, const char key[]) {
237 lua_newtable(fL);
238 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
239 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
240 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
241 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
242 CHECK_SETFIELD(key);
243}
244
reed@google.come3823fd2013-05-30 18:55:14 +0000245void SkLua::pushU32(uint32_t value, const char key[]) {
246 lua_pushnumber(fL, (double)value);
247 CHECK_SETFIELD(key);
248}
249
reed@google.com74ce6f02013-05-22 15:13:18 +0000250void SkLua::pushScalar(SkScalar value, const char key[]) {
251 lua_pushnumber(fL, SkScalarToLua(value));
252 CHECK_SETFIELD(key);
253}
254
reed@google.come3823fd2013-05-30 18:55:14 +0000255void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
256 lua_newtable(fL);
257 for (int i = 0; i < count; ++i) {
258 // make it base-1 to match lua convention
259 setarray_number(fL, i + 1, (double)array[i]);
260 }
261 CHECK_SETFIELD(key);
262}
263
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000264void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
265 lua_newtable(fL);
266 for (int i = 0; i < count; ++i) {
267 // make it base-1 to match lua convention
268 lua_newtable(fL);
269 this->pushScalar(array[i].fX, "x");
270 this->pushScalar(array[i].fY, "y");
271 lua_rawseti(fL, -2, i + 1);
272 }
273 CHECK_SETFIELD(key);
274}
275
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000276void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
277 lua_newtable(fL);
278 for (int i = 0; i < count; ++i) {
279 // make it base-1 to match lua convention
280 setarray_scalar(fL, i + 1, array[i]);
281 }
282 CHECK_SETFIELD(key);
283}
284
reed@google.com74ce6f02013-05-22 15:13:18 +0000285void SkLua::pushRect(const SkRect& r, const char key[]) {
286 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000287 setfield_scalar(fL, "left", r.fLeft);
288 setfield_scalar(fL, "top", r.fTop);
289 setfield_scalar(fL, "right", r.fRight);
290 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000291 CHECK_SETFIELD(key);
292}
293
294void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
295 push_obj(fL, rr);
296 CHECK_SETFIELD(key);
297}
298
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000299void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
300 lua_newtable(fL);
301 setfield_scalar(fL, "phase", info.fPhase);
302 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
303 CHECK_SETFIELD(key);
304}
305
306
reed@google.com74ce6f02013-05-22 15:13:18 +0000307void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
308 push_obj(fL, matrix);
309 CHECK_SETFIELD(key);
310}
311
312void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
313 push_obj(fL, paint);
314 CHECK_SETFIELD(key);
315}
316
317void SkLua::pushPath(const SkPath& path, const char key[]) {
318 push_obj(fL, path);
319 CHECK_SETFIELD(key);
320}
321
322void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
323 push_ref(fL, canvas);
324 CHECK_SETFIELD(key);
325}
326
fmalitab7425172014-08-26 07:56:44 -0700327void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
328 push_ref(fL, const_cast<SkTextBlob*>(blob));
329 CHECK_SETFIELD(key);
330}
331
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000332static const char* element_type(SkClipStack::Element::Type type) {
333 switch (type) {
334 case SkClipStack::Element::kEmpty_Type:
335 return "empty";
336 case SkClipStack::Element::kRect_Type:
337 return "rect";
338 case SkClipStack::Element::kRRect_Type:
339 return "rrect";
340 case SkClipStack::Element::kPath_Type:
341 return "path";
342 }
343 return "unknown";
344}
345
346static const char* region_op(SkRegion::Op op) {
347 switch (op) {
348 case SkRegion::kDifference_Op:
349 return "difference";
350 case SkRegion::kIntersect_Op:
351 return "intersect";
352 case SkRegion::kUnion_Op:
353 return "union";
354 case SkRegion::kXOR_Op:
355 return "xor";
356 case SkRegion::kReverseDifference_Op:
357 return "reverse-difference";
358 case SkRegion::kReplace_Op:
359 return "replace";
360 }
361 return "unknown";
362}
363
364void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
365 lua_newtable(fL);
366 SkClipStack::B2TIter iter(stack);
367 const SkClipStack::Element* element;
368 int i = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700369 while ((element = iter.next())) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000370 this->pushClipStackElement(*element);
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000371 lua_rawseti(fL, -2, ++i);
372 }
373 CHECK_SETFIELD(key);
374}
375
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000376void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
377 lua_newtable(fL);
378 SkClipStack::Element::Type type = element.getType();
379 this->pushString(element_type(type), "type");
380 switch (type) {
381 case SkClipStack::Element::kEmpty_Type:
382 break;
383 case SkClipStack::Element::kRect_Type:
384 this->pushRect(element.getRect(), "rect");
385 break;
386 case SkClipStack::Element::kRRect_Type:
387 this->pushRRect(element.getRRect(), "rrect");
388 break;
389 case SkClipStack::Element::kPath_Type:
390 this->pushPath(element.getPath(), "path");
391 break;
392 }
393 this->pushString(region_op(element.getOp()), "op");
394 this->pushBool(element.isAA(), "aa");
395 CHECK_SETFIELD(key);
396}
397
398
reed@google.com74ce6f02013-05-22 15:13:18 +0000399///////////////////////////////////////////////////////////////////////////////
400///////////////////////////////////////////////////////////////////////////////
401
reed@google.com74ce6f02013-05-22 15:13:18 +0000402static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
403 SkASSERT(lua_istable(L, index));
404 lua_pushstring(L, key);
405 lua_gettable(L, index);
mtklein8aacf202014-12-18 13:29:54 -0800406
reed@google.com74ce6f02013-05-22 15:13:18 +0000407 SkScalar value = lua2scalar(L, -1);
408 lua_pop(L, 1);
409 return value;
410}
411
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000412static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
413 SkASSERT(lua_istable(L, index));
414 lua_pushstring(L, key);
415 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000416
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000417 SkScalar value;
418 if (lua_isnil(L, -1)) {
419 value = def;
420 } else {
421 value = lua2scalar(L, -1);
422 }
423 lua_pop(L, 1);
424 return value;
425}
426
reed468b1812014-10-19 11:42:54 -0700427static SkScalar byte2unit(U8CPU byte) {
428 return byte / 255.0f;
429}
430
reed@google.com74ce6f02013-05-22 15:13:18 +0000431static U8CPU unit2byte(SkScalar x) {
432 if (x <= 0) {
433 return 0;
434 } else if (x >= 1) {
435 return 255;
436 } else {
437 return SkScalarRoundToInt(x * 255);
438 }
439}
440
441static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700442 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
443 unit2byte(getfield_scalar_default(L, index, "r", 0)),
444 unit2byte(getfield_scalar_default(L, index, "g", 0)),
445 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000446}
447
448static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000449 rect->set(getfield_scalar_default(L, index, "left", 0),
450 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000451 getfield_scalar(L, index, "right"),
452 getfield_scalar(L, index, "bottom"));
453 return rect;
454}
455
reedf355df52014-10-12 12:18:40 -0700456static int lcanvas_clear(lua_State* L) {
457 get_ref<SkCanvas>(L, 1)->clear(0);
458 return 0;
459}
460
reed@google.com74ce6f02013-05-22 15:13:18 +0000461static int lcanvas_drawColor(lua_State* L) {
462 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
463 return 0;
464}
465
reed9fbc3f32014-10-21 07:12:58 -0700466static int lcanvas_drawPaint(lua_State* L) {
467 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
468 return 0;
469}
470
reed@google.com74ce6f02013-05-22 15:13:18 +0000471static int lcanvas_drawRect(lua_State* L) {
472 SkRect rect;
reed7a72c672014-11-07 10:23:55 -0800473 lua2rect(L, 2, &rect);
474 const SkPaint* paint = get_obj<SkPaint>(L, 3);
475 get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
reed@google.com74ce6f02013-05-22 15:13:18 +0000476 return 0;
477}
478
479static int lcanvas_drawOval(lua_State* L) {
480 SkRect rect;
481 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
482 *get_obj<SkPaint>(L, 3));
483 return 0;
484}
485
486static int lcanvas_drawCircle(lua_State* L) {
487 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
488 lua2scalar(L, 3),
489 lua2scalar(L, 4),
490 *get_obj<SkPaint>(L, 5));
491 return 0;
492}
493
reed485557f2014-10-12 10:36:47 -0700494static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
495 if (lua_isnumber(L, index)) {
496 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
497 return paint;
reedf355df52014-10-12 12:18:40 -0700498 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700499 const SkPaint* ptr = get_obj<SkPaint>(L, index);
500 if (ptr) {
501 *paint = *ptr;
502 return paint;
503 }
504 }
505 return NULL;
506}
507
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000508static int lcanvas_drawImage(lua_State* L) {
509 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
510 SkImage* image = get_ref<SkImage>(L, 2);
511 if (NULL == image) {
512 return 0;
513 }
514 SkScalar x = lua2scalar(L, 3);
515 SkScalar y = lua2scalar(L, 4);
516
517 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700518 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000519 return 0;
520}
521
reedba5fb932014-10-10 15:28:19 -0700522static int lcanvas_drawImageRect(lua_State* L) {
523 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
524 SkImage* image = get_ref<SkImage>(L, 2);
525 if (NULL == image) {
526 return 0;
527 }
528
529 SkRect srcR, dstR;
530 SkRect* srcRPtr = NULL;
531 if (!lua_isnil(L, 3)) {
532 srcRPtr = lua2rect(L, 3, &srcR);
533 }
534 lua2rect(L, 4, &dstR);
mtklein8aacf202014-12-18 13:29:54 -0800535
reedba5fb932014-10-10 15:28:19 -0700536 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700537 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700538 return 0;
539}
540
reed7a72c672014-11-07 10:23:55 -0800541static int lcanvas_drawPatch(lua_State* L) {
542 SkPoint cubics[12];
543 SkColor colorStorage[4];
544 SkPoint texStorage[4];
545
546 const SkColor* colors = NULL;
547 const SkPoint* texs = NULL;
548
549 getarray_points(L, 2, cubics, 12);
550
551 colorStorage[0] = SK_ColorRED;
552 colorStorage[1] = SK_ColorGREEN;
553 colorStorage[2] = SK_ColorBLUE;
554 colorStorage[3] = SK_ColorGRAY;
555
556 if (lua_isnil(L, 4)) {
557 colors = colorStorage;
558 } else {
559 getarray_points(L, 4, texStorage, 4);
560 texs = texStorage;
561 }
562
563 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, NULL, *get_obj<SkPaint>(L, 5));
564 return 0;
565}
566
reed@google.comfd345872013-05-22 20:53:42 +0000567static int lcanvas_drawPath(lua_State* L) {
568 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
569 *get_obj<SkPaint>(L, 3));
570 return 0;
571}
572
reed96affcd2014-10-13 12:38:04 -0700573// drawPicture(pic, x, y, paint)
574static int lcanvas_drawPicture(lua_State* L) {
575 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
576 SkPicture* picture = get_ref<SkPicture>(L, 2);
577 SkScalar x = lua2scalar_def(L, 3, 0);
578 SkScalar y = lua2scalar_def(L, 4, 0);
579 SkMatrix matrix, *matrixPtr = NULL;
580 if (x || y) {
581 matrix.setTranslate(x, y);
582 matrixPtr = &matrix;
583 }
584 SkPaint paint;
585 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
586 return 0;
587}
588
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000589static int lcanvas_drawText(lua_State* L) {
590 if (lua_gettop(L) < 5) {
591 return 0;
592 }
593
594 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
595 size_t len;
596 const char* text = lua_tolstring(L, 2, &len);
597 get_ref<SkCanvas>(L, 1)->drawText(text, len,
598 lua2scalar(L, 3), lua2scalar(L, 4),
599 *get_obj<SkPaint>(L, 5));
600 }
601 return 0;
602}
603
reed1b6ab442014-11-03 19:55:41 -0800604static int lcanvas_drawTextBlob(lua_State* L) {
605 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
606 SkScalar x = lua2scalar(L, 3);
607 SkScalar y = lua2scalar(L, 4);
608 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
609 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
610 return 0;
611}
612
reed@google.com74ce6f02013-05-22 15:13:18 +0000613static int lcanvas_getSaveCount(lua_State* L) {
614 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
615 return 1;
616}
617
618static int lcanvas_getTotalMatrix(lua_State* L) {
619 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
620 return 1;
621}
622
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000623static int lcanvas_getClipStack(lua_State* L) {
624 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
625 return 1;
626}
627
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000628int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
629#if SK_SUPPORT_GPU
630 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
631 SkISize layerSize = canvas->getTopLayerSize();
632 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
633 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
634 layerSize.fWidth, layerSize.fHeight);
635
636 GrReducedClip::ElementList elements;
637 GrReducedClip::InitialState initialState;
638 int32_t genID;
639 SkIRect resultBounds;
640
641 const SkClipStack& stack = *canvas->getClipStack();
642
643 GrReducedClip::ReduceClipStack(stack,
644 queryBounds,
645 &elements,
646 &genID,
647 &initialState,
648 &resultBounds,
649 NULL);
650
651 GrReducedClip::ElementList::Iter iter(elements);
652 int i = 0;
653 lua_newtable(L);
bsalomon49f085d2014-09-05 13:34:00 -0700654 while(iter.get()) {
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000655 SkLua(L).pushClipStackElement(*iter.get());
656 iter.next();
657 lua_rawseti(L, -2, ++i);
658 }
659 // Currently this only returns the element list to lua, not the initial state or result bounds.
660 // It could return these as additional items on the lua stack.
661 return 1;
662#else
663 return 0;
664#endif
665}
666
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000667static int lcanvas_save(lua_State* L) {
668 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
669 return 1;
670}
671
reed86217d82014-10-25 20:44:40 -0700672static int lcanvas_saveLayer(lua_State* L) {
673 SkPaint paint;
674 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(NULL, lua2OptionalPaint(L, 2, &paint)));
675 return 1;
676}
677
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000678static int lcanvas_restore(lua_State* L) {
679 get_ref<SkCanvas>(L, 1)->restore();
680 return 0;
681}
682
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000683static int lcanvas_scale(lua_State* L) {
684 SkScalar sx = lua2scalar_def(L, 2, 1);
685 SkScalar sy = lua2scalar_def(L, 3, sx);
686 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
687 return 0;
688}
689
reed@google.com3597b732013-05-22 20:12:50 +0000690static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000691 SkScalar tx = lua2scalar_def(L, 2, 0);
692 SkScalar ty = lua2scalar_def(L, 3, 0);
693 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
694 return 0;
695}
696
697static int lcanvas_rotate(lua_State* L) {
698 SkScalar degrees = lua2scalar_def(L, 2, 0);
699 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000700 return 0;
701}
702
reedbdc49ae2014-10-14 09:34:52 -0700703static int lcanvas_concat(lua_State* L) {
704 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
705 return 0;
706}
707
reed485557f2014-10-12 10:36:47 -0700708static int lcanvas_newSurface(lua_State* L) {
709 int width = lua2int_def(L, 2, 0);
reed7a72c672014-11-07 10:23:55 -0800710 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -0700711 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
712 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info);
713 if (NULL == surface) {
714 lua_pushnil(L);
715 } else {
reed9fbc3f32014-10-21 07:12:58 -0700716 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -0700717 }
718 return 1;
719}
720
reed@google.com74ce6f02013-05-22 15:13:18 +0000721static int lcanvas_gc(lua_State* L) {
722 get_ref<SkCanvas>(L, 1)->unref();
723 return 0;
724}
725
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000726const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700727 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000728 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700729 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000730 { "drawRect", lcanvas_drawRect },
731 { "drawOval", lcanvas_drawOval },
732 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000733 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700734 { "drawImageRect", lcanvas_drawImageRect },
reed7a72c672014-11-07 10:23:55 -0800735 { "drawPatch", lcanvas_drawPatch },
reed@google.comfd345872013-05-22 20:53:42 +0000736 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700737 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000738 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800739 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000740 { "getSaveCount", lcanvas_getSaveCount },
741 { "getTotalMatrix", lcanvas_getTotalMatrix },
commit-bot@chromium.org5cc25352014-02-24 18:59:48 +0000742 { "getClipStack", lcanvas_getClipStack },
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000743#if SK_SUPPORT_GPU
744 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
745#endif
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000746 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700747 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000748 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000749 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000750 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000751 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700752 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700753
754 { "newSurface", lcanvas_newSurface },
755
reed@google.com74ce6f02013-05-22 15:13:18 +0000756 { "__gc", lcanvas_gc },
757 { NULL, NULL }
758};
759
760///////////////////////////////////////////////////////////////////////////////
761
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000762static int ldocument_beginPage(lua_State* L) {
763 const SkRect* contentPtr = NULL;
764 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
765 lua2scalar(L, 3),
766 contentPtr));
767 return 1;
768}
769
770static int ldocument_endPage(lua_State* L) {
771 get_ref<SkDocument>(L, 1)->endPage();
772 return 0;
773}
774
775static int ldocument_close(lua_State* L) {
776 get_ref<SkDocument>(L, 1)->close();
777 return 0;
778}
779
780static int ldocument_gc(lua_State* L) {
781 get_ref<SkDocument>(L, 1)->unref();
782 return 0;
783}
784
785static const struct luaL_Reg gSkDocument_Methods[] = {
786 { "beginPage", ldocument_beginPage },
787 { "endPage", ldocument_endPage },
788 { "close", ldocument_close },
789 { "__gc", ldocument_gc },
790 { NULL, NULL }
791};
792
793///////////////////////////////////////////////////////////////////////////////
794
reed@google.com74ce6f02013-05-22 15:13:18 +0000795static int lpaint_isAntiAlias(lua_State* L) {
796 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
797 return 1;
798}
799
800static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000801 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000802 return 0;
803}
804
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000805static int lpaint_isDither(lua_State* L) {
806 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
807 return 1;
808}
809
reedbb8a0ab2014-11-03 22:32:07 -0800810static int lpaint_setDither(lua_State* L) {
811 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
812 return 0;
813}
814
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000815static int lpaint_isUnderlineText(lua_State* L) {
816 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
817 return 1;
818}
819
820static int lpaint_isStrikeThruText(lua_State* L) {
821 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
822 return 1;
823}
824
825static int lpaint_isFakeBoldText(lua_State* L) {
826 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
827 return 1;
828}
829
830static int lpaint_isLinearText(lua_State* L) {
831 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
832 return 1;
833}
834
835static int lpaint_isSubpixelText(lua_State* L) {
836 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
837 return 1;
838}
839
reed09a1d672014-10-11 13:13:11 -0700840static int lpaint_setSubpixelText(lua_State* L) {
841 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
842 return 1;
843}
844
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000845static int lpaint_isDevKernText(lua_State* L) {
846 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
847 return 1;
848}
849
850static int lpaint_isLCDRenderText(lua_State* L) {
851 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
852 return 1;
853}
854
reed36c9c112014-11-04 10:58:42 -0800855static int lpaint_setLCDRenderText(lua_State* L) {
856 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
857 return 1;
858}
859
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000860static int lpaint_isEmbeddedBitmapText(lua_State* L) {
861 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
862 return 1;
863}
864
865static int lpaint_isAutohinted(lua_State* L) {
866 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
867 return 1;
868}
869
870static int lpaint_isVerticalText(lua_State* L) {
871 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
872 return 1;
873}
874
reed468b1812014-10-19 11:42:54 -0700875static int lpaint_getAlpha(lua_State* L) {
876 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
877 return 1;
878}
879
880static int lpaint_setAlpha(lua_State* L) {
881 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
882 return 0;
883}
884
reed@google.com74ce6f02013-05-22 15:13:18 +0000885static int lpaint_getColor(lua_State* L) {
886 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
887 return 1;
888}
889
890static int lpaint_setColor(lua_State* L) {
891 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
892 return 0;
893}
894
reed@google.come3823fd2013-05-30 18:55:14 +0000895static int lpaint_getTextSize(lua_State* L) {
896 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
897 return 1;
898}
899
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000900static int lpaint_getTextScaleX(lua_State* L) {
901 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
902 return 1;
903}
904
905static int lpaint_getTextSkewX(lua_State* L) {
906 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
907 return 1;
908}
909
reed@google.come3823fd2013-05-30 18:55:14 +0000910static int lpaint_setTextSize(lua_State* L) {
911 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
912 return 0;
913}
914
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000915static int lpaint_getTypeface(lua_State* L) {
916 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
917 return 1;
918}
919
920static int lpaint_setTypeface(lua_State* L) {
921 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
922 return 0;
923}
924
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000925static int lpaint_getHinting(lua_State* L) {
926 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
927 return 1;
928}
929
reed93a12152015-03-16 10:08:34 -0700930static int lpaint_getFilterQuality(lua_State* L) {
931 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality());
reed7a72c672014-11-07 10:23:55 -0800932 return 1;
933}
934
reed93a12152015-03-16 10:08:34 -0700935static int lpaint_setFilterQuality(lua_State* L) {
reed7a72c672014-11-07 10:23:55 -0800936 int level = lua2int_def(L, 2, -1);
937 if (level >= 0 && level <= 3) {
reed93a12152015-03-16 10:08:34 -0700938 get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level);
reed7a72c672014-11-07 10:23:55 -0800939 }
940 return 0;
941}
942
reed@google.come3823fd2013-05-30 18:55:14 +0000943static int lpaint_getFontID(lua_State* L) {
944 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
945 SkLua(L).pushU32(SkTypeface::UniqueID(face));
946 return 1;
947}
948
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000949static const struct {
950 const char* fLabel;
951 SkPaint::Align fAlign;
952} gAlignRec[] = {
953 { "left", SkPaint::kLeft_Align },
954 { "center", SkPaint::kCenter_Align },
955 { "right", SkPaint::kRight_Align },
956};
957
958static int lpaint_getTextAlign(lua_State* L) {
959 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
960 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
961 if (gAlignRec[i].fAlign == align) {
962 lua_pushstring(L, gAlignRec[i].fLabel);
963 return 1;
964 }
965 }
966 return 0;
967}
968
969static int lpaint_setTextAlign(lua_State* L) {
970 if (lua_isstring(L, 2)) {
971 size_t len;
972 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000973
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000974 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
975 if (!strcmp(gAlignRec[i].fLabel, label)) {
976 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
977 break;
978 }
979 }
980 }
981 return 0;
982}
983
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000984static int lpaint_getStroke(lua_State* L) {
985 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
986 return 1;
987}
988
989static int lpaint_setStroke(lua_State* L) {
990 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000991
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000992 if (lua_toboolean(L, 2)) {
993 style = SkPaint::kStroke_Style;
994 } else {
995 style = SkPaint::kFill_Style;
996 }
997 get_obj<SkPaint>(L, 1)->setStyle(style);
998 return 0;
999}
1000
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001001static int lpaint_getStrokeCap(lua_State* L) {
1002 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
1003 return 1;
1004}
1005
1006static int lpaint_getStrokeJoin(lua_State* L) {
1007 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
1008 return 1;
1009}
1010
1011static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +00001012 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001013 return 1;
1014}
1015
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001016static int lpaint_getStrokeWidth(lua_State* L) {
1017 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
1018 return 1;
1019}
1020
1021static int lpaint_setStrokeWidth(lua_State* L) {
1022 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
1023 return 0;
1024}
1025
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001026static int lpaint_getStrokeMiter(lua_State* L) {
1027 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
1028 return 1;
1029}
1030
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001031static int lpaint_measureText(lua_State* L) {
1032 if (lua_isstring(L, 2)) {
1033 size_t len;
1034 const char* text = lua_tolstring(L, 2, &len);
1035 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
1036 return 1;
1037 }
1038 return 0;
1039}
1040
1041struct FontMetrics {
1042 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
1043 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
1044 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
1045 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
1046 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
1047 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
1048 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
1049 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
1050 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
1051};
1052
1053static int lpaint_getFontMetrics(lua_State* L) {
1054 SkPaint::FontMetrics fm;
1055 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +00001056
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001057 lua_newtable(L);
1058 setfield_scalar(L, "top", fm.fTop);
1059 setfield_scalar(L, "ascent", fm.fAscent);
1060 setfield_scalar(L, "descent", fm.fDescent);
1061 setfield_scalar(L, "bottom", fm.fBottom);
1062 setfield_scalar(L, "leading", fm.fLeading);
1063 SkLua(L).pushScalar(height);
1064 return 2;
1065}
1066
reed@google.com29563872013-07-10 21:23:49 +00001067static int lpaint_getEffects(lua_State* L) {
1068 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001069
reed@google.com29563872013-07-10 21:23:49 +00001070 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -07001071 setfield_bool_if(L, "looper", !!paint->getLooper());
1072 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
1073 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
1074 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
1075 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +00001076 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
1077 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed468b1812014-10-19 11:42:54 -07001078 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
reed@google.com29563872013-07-10 21:23:49 +00001079 return 1;
1080}
1081
reed468b1812014-10-19 11:42:54 -07001082static int lpaint_getImageFilter(lua_State* L) {
1083 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1084 SkImageFilter* imf = paint->getImageFilter();
1085 if (imf) {
1086 push_ref(L, imf);
1087 return 1;
1088 }
1089 return 0;
1090}
1091
1092static int lpaint_setImageFilter(lua_State* L) {
1093 SkPaint* paint = get_obj<SkPaint>(L, 1);
1094 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1095 return 0;
1096}
1097
reed@google.com5fdc9832013-07-24 15:47:52 +00001098static int lpaint_getShader(lua_State* L) {
1099 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1100 SkShader* shader = paint->getShader();
1101 if (shader) {
1102 push_ref(L, shader);
1103 return 1;
1104 }
1105 return 0;
1106}
1107
reed9fbc3f32014-10-21 07:12:58 -07001108static int lpaint_setShader(lua_State* L) {
1109 SkPaint* paint = get_obj<SkPaint>(L, 1);
1110 paint->setShader(get_ref<SkShader>(L, 2));
1111 return 0;
1112}
1113
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001114static int lpaint_getPathEffect(lua_State* L) {
1115 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1116 SkPathEffect* pe = paint->getPathEffect();
1117 if (pe) {
1118 push_ref(L, pe);
1119 return 1;
1120 }
1121 return 0;
1122}
1123
reed@google.com74ce6f02013-05-22 15:13:18 +00001124static int lpaint_gc(lua_State* L) {
1125 get_obj<SkPaint>(L, 1)->~SkPaint();
1126 return 0;
1127}
1128
1129static const struct luaL_Reg gSkPaint_Methods[] = {
1130 { "isAntiAlias", lpaint_isAntiAlias },
1131 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001132 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001133 { "setDither", lpaint_setDither },
reed93a12152015-03-16 10:08:34 -07001134 { "getFilterQuality", lpaint_getFilterQuality },
1135 { "setFilterQuality", lpaint_setFilterQuality },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001136 { "isUnderlineText", lpaint_isUnderlineText },
1137 { "isStrikeThruText", lpaint_isStrikeThruText },
1138 { "isFakeBoldText", lpaint_isFakeBoldText },
1139 { "isLinearText", lpaint_isLinearText },
1140 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001141 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001142 { "isDevKernText", lpaint_isDevKernText },
1143 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001144 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001145 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1146 { "isAutohinted", lpaint_isAutohinted },
1147 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001148 { "getAlpha", lpaint_getAlpha },
1149 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001150 { "getColor", lpaint_getColor },
1151 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001152 { "getTextSize", lpaint_getTextSize },
1153 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001154 { "getTextScaleX", lpaint_getTextScaleX },
1155 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001156 { "getTypeface", lpaint_getTypeface },
1157 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001158 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001159 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001160 { "getTextAlign", lpaint_getTextAlign },
1161 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001162 { "getStroke", lpaint_getStroke },
1163 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001164 { "getStrokeCap", lpaint_getStrokeCap },
1165 { "getStrokeJoin", lpaint_getStrokeJoin },
1166 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001167 { "getStrokeWidth", lpaint_getStrokeWidth },
1168 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001169 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001170 { "measureText", lpaint_measureText },
1171 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001172 { "getEffects", lpaint_getEffects },
reed468b1812014-10-19 11:42:54 -07001173 { "getImageFilter", lpaint_getImageFilter },
1174 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001175 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001176 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001177 { "getPathEffect", lpaint_getPathEffect },
reed@google.com74ce6f02013-05-22 15:13:18 +00001178 { "__gc", lpaint_gc },
1179 { NULL, NULL }
1180};
1181
1182///////////////////////////////////////////////////////////////////////////////
1183
reed@google.com5fdc9832013-07-24 15:47:52 +00001184static const char* mode2string(SkShader::TileMode mode) {
1185 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1186 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1187 return gNames[mode];
1188}
1189
1190static const char* gradtype2string(SkShader::GradientType t) {
1191 static const char* gNames[] = {
1192 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1193 };
1194 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1195 return gNames[t];
1196}
1197
1198static int lshader_isOpaque(lua_State* L) {
1199 SkShader* shader = get_ref<SkShader>(L, 1);
1200 return shader && shader->isOpaque();
1201}
1202
1203static int lshader_asABitmap(lua_State* L) {
1204 SkShader* shader = get_ref<SkShader>(L, 1);
1205 if (shader) {
1206 SkBitmap bm;
1207 SkMatrix matrix;
1208 SkShader::TileMode modes[2];
1209 switch (shader->asABitmap(&bm, &matrix, modes)) {
1210 case SkShader::kDefault_BitmapType:
1211 lua_newtable(L);
1212 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1213 setfield_number(L, "width", bm.width());
1214 setfield_number(L, "height", bm.height());
1215 setfield_string(L, "tileX", mode2string(modes[0]));
1216 setfield_string(L, "tileY", mode2string(modes[1]));
1217 return 1;
1218 default:
1219 break;
1220 }
1221 }
1222 return 0;
1223}
1224
1225static int lshader_asAGradient(lua_State* L) {
1226 SkShader* shader = get_ref<SkShader>(L, 1);
1227 if (shader) {
1228 SkShader::GradientInfo info;
1229 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001230
1231 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001232 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001233
1234 info.fColorCount = 3;
1235 info.fColors = &colors[0];
1236 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001237
reed@google.com5fdc9832013-07-24 15:47:52 +00001238 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001239
reed@google.com5fdc9832013-07-24 15:47:52 +00001240 if (SkShader::kNone_GradientType != t) {
1241 lua_newtable(L);
1242 setfield_string(L, "type", gradtype2string(t));
1243 setfield_number(L, "colorCount", info.fColorCount);
1244 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001245
1246 if (info.fColorCount == 3){
1247 setfield_number(L, "midPos", pos[1]);
1248 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001249
reed@google.com5fdc9832013-07-24 15:47:52 +00001250 return 1;
1251 }
1252 }
1253 return 0;
1254}
1255
1256static int lshader_gc(lua_State* L) {
1257 get_ref<SkShader>(L, 1)->unref();
1258 return 0;
1259}
1260
1261static const struct luaL_Reg gSkShader_Methods[] = {
1262 { "isOpaque", lshader_isOpaque },
1263 { "asABitmap", lshader_asABitmap },
1264 { "asAGradient", lshader_asAGradient },
1265 { "__gc", lshader_gc },
1266 { NULL, NULL }
1267};
1268
1269///////////////////////////////////////////////////////////////////////////////
1270
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001271static int lpatheffect_asADash(lua_State* L) {
1272 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1273 if (pe) {
1274 SkPathEffect::DashInfo info;
1275 SkPathEffect::DashType dashType = pe->asADash(&info);
1276 if (SkPathEffect::kDash_DashType == dashType) {
1277 SkAutoTArray<SkScalar> intervals(info.fCount);
1278 info.fIntervals = intervals.get();
1279 pe->asADash(&info);
1280 SkLua(L).pushDash(info);
1281 return 1;
1282 }
1283 }
1284 return 0;
1285}
1286
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001287static int lpatheffect_gc(lua_State* L) {
1288 get_ref<SkPathEffect>(L, 1)->unref();
1289 return 0;
1290}
1291
1292static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001293 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001294 { "__gc", lpatheffect_gc },
1295 { NULL, NULL }
1296};
1297
1298///////////////////////////////////////////////////////////////////////////////
1299
reed468b1812014-10-19 11:42:54 -07001300static int lpimagefilter_gc(lua_State* L) {
1301 get_ref<SkImageFilter>(L, 1)->unref();
1302 return 0;
1303}
1304
1305static const struct luaL_Reg gSkImageFilter_Methods[] = {
1306 { "__gc", lpimagefilter_gc },
1307 { NULL, NULL }
1308};
1309
1310///////////////////////////////////////////////////////////////////////////////
1311
humper@google.com2815c192013-07-10 22:42:30 +00001312static int lmatrix_getType(lua_State* L) {
1313 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001314
humper@google.com2815c192013-07-10 22:42:30 +00001315 lua_newtable(L);
1316 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1317 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1318 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1319 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1320 return 1;
1321}
1322
humper@google.com0f48ee02013-07-26 15:23:43 +00001323static int lmatrix_getScaleX(lua_State* L) {
1324 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1325 return 1;
1326}
1327
1328static int lmatrix_getScaleY(lua_State* L) {
1329 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1330 return 1;
1331}
1332
1333static int lmatrix_getTranslateX(lua_State* L) {
1334 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1335 return 1;
1336}
1337
1338static int lmatrix_getTranslateY(lua_State* L) {
1339 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1340 return 1;
1341}
1342
reed7a72c672014-11-07 10:23:55 -08001343static int lmatrix_invert(lua_State* L) {
1344 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1345 return 1;
1346}
1347
1348static int lmatrix_mapXY(lua_State* L) {
1349 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1350 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1351 lua_pushnumber(L, pt.x());
1352 lua_pushnumber(L, pt.y());
1353 return 2;
1354}
1355
reedbdc49ae2014-10-14 09:34:52 -07001356static int lmatrix_setRectToRect(lua_State* L) {
1357 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1358 SkRect srcR, dstR;
1359 lua2rect(L, 2, &srcR);
1360 lua2rect(L, 3, &dstR);
1361 const char* scaleToFitStr = lua_tostring(L, 4);
1362 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1363
1364 if (scaleToFitStr) {
1365 const struct {
1366 const char* fName;
1367 SkMatrix::ScaleToFit fScaleToFit;
1368 } rec[] = {
1369 { "fill", SkMatrix::kFill_ScaleToFit },
1370 { "start", SkMatrix::kStart_ScaleToFit },
1371 { "center", SkMatrix::kCenter_ScaleToFit },
1372 { "end", SkMatrix::kEnd_ScaleToFit },
1373 };
1374
1375 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1376 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1377 scaleToFit = rec[i].fScaleToFit;
1378 break;
1379 }
1380 }
1381 }
1382
1383 matrix->setRectToRect(srcR, dstR, scaleToFit);
1384 return 0;
1385}
1386
humper@google.com2815c192013-07-10 22:42:30 +00001387static const struct luaL_Reg gSkMatrix_Methods[] = {
1388 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001389 { "getScaleX", lmatrix_getScaleX },
1390 { "getScaleY", lmatrix_getScaleY },
1391 { "getTranslateX", lmatrix_getTranslateX },
1392 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001393 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001394 { "invert", lmatrix_invert },
1395 { "mapXY", lmatrix_mapXY },
humper@google.com2815c192013-07-10 22:42:30 +00001396 { NULL, NULL }
1397};
1398
1399///////////////////////////////////////////////////////////////////////////////
1400
reed@google.com74ce6f02013-05-22 15:13:18 +00001401static int lpath_getBounds(lua_State* L) {
1402 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1403 return 1;
1404}
1405
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001406static const char* fill_type_to_str(SkPath::FillType fill) {
1407 switch (fill) {
1408 case SkPath::kEvenOdd_FillType:
1409 return "even-odd";
1410 case SkPath::kWinding_FillType:
1411 return "winding";
1412 case SkPath::kInverseEvenOdd_FillType:
1413 return "inverse-even-odd";
1414 case SkPath::kInverseWinding_FillType:
1415 return "inverse-winding";
1416 }
1417 return "unknown";
1418}
1419
1420static int lpath_getFillType(lua_State* L) {
1421 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1422 SkLua(L).pushString(fill_type_to_str(fill));
1423 return 1;
1424}
1425
1426static SkString segment_masks_to_str(uint32_t segmentMasks) {
1427 SkString result;
1428 bool first = true;
1429 if (SkPath::kLine_SegmentMask & segmentMasks) {
1430 result.append("line");
1431 first = false;
1432 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1433 }
1434 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1435 if (!first) {
1436 result.append(" ");
1437 }
1438 result.append("quad");
1439 first = false;
1440 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1441 }
1442 if (SkPath::kConic_SegmentMask & segmentMasks) {
1443 if (!first) {
1444 result.append(" ");
1445 }
1446 result.append("conic");
1447 first = false;
1448 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1449 }
1450 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1451 if (!first) {
1452 result.append(" ");
1453 }
1454 result.append("cubic");
1455 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1456 }
1457 SkASSERT(0 == segmentMasks);
1458 return result;
1459}
1460
krajcevski95498ed2014-08-18 08:02:33 -07001461static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001462 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1463 SkLua(L).pushString(segment_masks_to_str(segMasks));
1464 return 1;
1465}
1466
1467static int lpath_isConvex(lua_State* L) {
1468 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1469 SkLua(L).pushBool(isConvex);
1470 return 1;
1471}
1472
reed@google.com74ce6f02013-05-22 15:13:18 +00001473static int lpath_isEmpty(lua_State* L) {
1474 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1475 return 1;
1476}
1477
1478static int lpath_isRect(lua_State* L) {
1479 SkRect r;
1480 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1481 int ret_count = 1;
1482 lua_pushboolean(L, pred);
1483 if (pred) {
1484 SkLua(L).pushRect(r);
1485 ret_count += 1;
1486 }
1487 return ret_count;
1488}
1489
1490static const char* dir2string(SkPath::Direction dir) {
1491 static const char* gStr[] = {
1492 "unknown", "cw", "ccw"
1493 };
1494 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1495 return gStr[dir];
1496}
1497
caryclark95bc5f32015-04-08 08:34:15 -07001498static int lpath_isNestedFillRects(lua_State* L) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001499 SkRect rects[2];
1500 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001501 bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
reed@google.com74ce6f02013-05-22 15:13:18 +00001502 int ret_count = 1;
1503 lua_pushboolean(L, pred);
1504 if (pred) {
1505 SkLua lua(L);
1506 lua.pushRect(rects[0]);
1507 lua.pushRect(rects[1]);
1508 lua_pushstring(L, dir2string(dirs[0]));
1509 lua_pushstring(L, dir2string(dirs[0]));
1510 ret_count += 4;
1511 }
1512 return ret_count;
1513}
1514
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001515static int lpath_countPoints(lua_State* L) {
1516 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1517 return 1;
1518}
1519
reed@google.com74ce6f02013-05-22 15:13:18 +00001520static int lpath_reset(lua_State* L) {
1521 get_obj<SkPath>(L, 1)->reset();
1522 return 0;
1523}
1524
1525static int lpath_moveTo(lua_State* L) {
1526 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1527 return 0;
1528}
1529
1530static int lpath_lineTo(lua_State* L) {
1531 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1532 return 0;
1533}
1534
1535static int lpath_quadTo(lua_State* L) {
1536 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1537 lua2scalar(L, 4), lua2scalar(L, 5));
1538 return 0;
1539}
1540
1541static int lpath_cubicTo(lua_State* L) {
1542 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1543 lua2scalar(L, 4), lua2scalar(L, 5),
1544 lua2scalar(L, 6), lua2scalar(L, 7));
1545 return 0;
1546}
1547
1548static int lpath_close(lua_State* L) {
1549 get_obj<SkPath>(L, 1)->close();
1550 return 0;
1551}
1552
1553static int lpath_gc(lua_State* L) {
1554 get_obj<SkPath>(L, 1)->~SkPath();
1555 return 0;
1556}
1557
1558static const struct luaL_Reg gSkPath_Methods[] = {
1559 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001560 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001561 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001562 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001563 { "isEmpty", lpath_isEmpty },
1564 { "isRect", lpath_isRect },
caryclark95bc5f32015-04-08 08:34:15 -07001565 { "isNestedFillRects", lpath_isNestedFillRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001566 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001567 { "reset", lpath_reset },
1568 { "moveTo", lpath_moveTo },
1569 { "lineTo", lpath_lineTo },
1570 { "quadTo", lpath_quadTo },
1571 { "cubicTo", lpath_cubicTo },
1572 { "close", lpath_close },
1573 { "__gc", lpath_gc },
1574 { NULL, NULL }
1575};
1576
1577///////////////////////////////////////////////////////////////////////////////
1578
1579static const char* rrect_type(const SkRRect& rr) {
1580 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001581 case SkRRect::kEmpty_Type: return "empty";
1582 case SkRRect::kRect_Type: return "rect";
1583 case SkRRect::kOval_Type: return "oval";
1584 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001585 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001586 case SkRRect::kComplex_Type: return "complex";
1587 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001588 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001589 return "";
1590}
1591
1592static int lrrect_rect(lua_State* L) {
1593 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1594 return 1;
1595}
1596
1597static int lrrect_type(lua_State* L) {
1598 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1599 return 1;
1600}
1601
1602static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001603 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001604 SkVector v;
1605 if (corner < 0 || corner > 3) {
1606 SkDebugf("bad corner index %d", corner);
1607 v.set(0, 0);
1608 } else {
1609 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1610 }
1611 lua_pushnumber(L, v.fX);
1612 lua_pushnumber(L, v.fY);
1613 return 2;
1614}
1615
1616static int lrrect_gc(lua_State* L) {
1617 get_obj<SkRRect>(L, 1)->~SkRRect();
1618 return 0;
1619}
1620
1621static const struct luaL_Reg gSkRRect_Methods[] = {
1622 { "rect", lrrect_rect },
1623 { "type", lrrect_type },
1624 { "radii", lrrect_radii },
1625 { "__gc", lrrect_gc },
1626 { NULL, NULL }
1627};
1628
1629///////////////////////////////////////////////////////////////////////////////
1630
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001631static int limage_width(lua_State* L) {
1632 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1633 return 1;
1634}
1635
1636static int limage_height(lua_State* L) {
1637 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1638 return 1;
1639}
1640
reed7a72c672014-11-07 10:23:55 -08001641static int limage_newShader(lua_State* L) {
1642 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
1643 const SkMatrix* localM = NULL;
1644 SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM));
1645 push_ref(L, shader.get());
1646 return 1;
1647}
1648
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001649static int limage_gc(lua_State* L) {
1650 get_ref<SkImage>(L, 1)->unref();
1651 return 0;
1652}
1653
1654static const struct luaL_Reg gSkImage_Methods[] = {
1655 { "width", limage_width },
1656 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001657 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001658 { "__gc", limage_gc },
1659 { NULL, NULL }
1660};
1661
1662///////////////////////////////////////////////////////////////////////////////
1663
reed485557f2014-10-12 10:36:47 -07001664static int lsurface_width(lua_State* L) {
1665 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1666 return 1;
1667}
1668
1669static int lsurface_height(lua_State* L) {
1670 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1671 return 1;
1672}
1673
1674static int lsurface_getCanvas(lua_State* L) {
1675 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
1676 if (NULL == canvas) {
1677 lua_pushnil(L);
1678 } else {
1679 push_ref(L, canvas);
1680 // note: we don't unref canvas, since getCanvas did not ref it.
1681 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1682 // the real owner (the surface) go away, but still hold onto the canvas?
1683 // *really* we want to sort of ref the surface again, but have the native object
1684 // know that it is supposed to be treated as a canvas...
1685 }
1686 return 1;
1687}
1688
1689static int lsurface_newImageSnapshot(lua_State* L) {
1690 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
1691 if (NULL == image) {
1692 lua_pushnil(L);
1693 } else {
reed9fbc3f32014-10-21 07:12:58 -07001694 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001695 }
1696 return 1;
1697}
1698
1699static int lsurface_newSurface(lua_State* L) {
1700 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001701 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001702 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1703 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
1704 if (NULL == surface) {
1705 lua_pushnil(L);
1706 } else {
reed9fbc3f32014-10-21 07:12:58 -07001707 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001708 }
1709 return 1;
1710}
1711
1712static int lsurface_gc(lua_State* L) {
1713 get_ref<SkSurface>(L, 1)->unref();
1714 return 0;
1715}
1716
1717static const struct luaL_Reg gSkSurface_Methods[] = {
1718 { "width", lsurface_width },
1719 { "height", lsurface_height },
1720 { "getCanvas", lsurface_getCanvas },
1721 { "newImageSnapshot", lsurface_newImageSnapshot },
1722 { "newSurface", lsurface_newSurface },
1723 { "__gc", lsurface_gc },
1724 { NULL, NULL }
1725};
1726
1727///////////////////////////////////////////////////////////////////////////////
1728
reed96affcd2014-10-13 12:38:04 -07001729static int lpicturerecorder_beginRecording(lua_State* L) {
1730 const SkScalar w = lua2scalar_def(L, 2, -1);
1731 const SkScalar h = lua2scalar_def(L, 3, -1);
1732 if (w <= 0 || h <= 0) {
1733 lua_pushnil(L);
1734 return 1;
1735 }
1736
1737 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
1738 if (NULL == canvas) {
1739 lua_pushnil(L);
1740 return 1;
1741 }
1742
1743 push_ref(L, canvas);
1744 return 1;
1745}
1746
1747static int lpicturerecorder_getCanvas(lua_State* L) {
1748 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
1749 if (NULL == canvas) {
1750 lua_pushnil(L);
1751 return 1;
1752 }
1753 push_ref(L, canvas);
1754 return 1;
1755}
1756
1757static int lpicturerecorder_endRecording(lua_State* L) {
1758 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
1759 if (NULL == pic) {
1760 lua_pushnil(L);
1761 return 1;
1762 }
reed9fbc3f32014-10-21 07:12:58 -07001763 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001764 return 1;
1765}
1766
1767static int lpicturerecorder_gc(lua_State* L) {
1768 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1769 return 0;
1770}
1771
1772static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1773 { "beginRecording", lpicturerecorder_beginRecording },
1774 { "getCanvas", lpicturerecorder_getCanvas },
1775 { "endRecording", lpicturerecorder_endRecording },
1776 { "__gc", lpicturerecorder_gc },
1777 { NULL, NULL }
1778};
1779
1780///////////////////////////////////////////////////////////////////////////////
1781
1782static int lpicture_width(lua_State* L) {
1783 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1784 return 1;
1785}
1786
1787static int lpicture_height(lua_State* L) {
1788 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1789 return 1;
1790}
1791
1792static int lpicture_gc(lua_State* L) {
1793 get_ref<SkPicture>(L, 1)->unref();
1794 return 0;
1795}
1796
1797static const struct luaL_Reg gSkPicture_Methods[] = {
1798 { "width", lpicture_width },
1799 { "height", lpicture_height },
1800 { "__gc", lpicture_gc },
1801 { NULL, NULL }
1802};
1803
1804///////////////////////////////////////////////////////////////////////////////
1805
reed1b6ab442014-11-03 19:55:41 -08001806static int ltextblob_bounds(lua_State* L) {
1807 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1808 return 1;
1809}
1810
1811static int ltextblob_gc(lua_State* L) {
1812 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1813 return 0;
1814}
1815
1816static const struct luaL_Reg gSkTextBlob_Methods[] = {
1817 { "bounds", ltextblob_bounds },
1818 { "__gc", ltextblob_gc },
1819 { NULL, NULL }
1820};
1821
1822///////////////////////////////////////////////////////////////////////////////
1823
reed36c9c112014-11-04 10:58:42 -08001824static int ltypeface_getFamilyName(lua_State* L) {
1825 SkString str;
1826 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1827 lua_pushstring(L, str.c_str());
1828 return 1;
1829}
1830
1831static int ltypeface_getStyle(lua_State* L) {
1832 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1833 return 1;
1834}
1835
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001836static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001837 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001838 return 0;
1839}
1840
1841static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001842 { "getFamilyName", ltypeface_getFamilyName },
1843 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001844 { "__gc", ltypeface_gc },
1845 { NULL, NULL }
1846};
1847
1848///////////////////////////////////////////////////////////////////////////////
1849
reed@google.com74ce6f02013-05-22 15:13:18 +00001850class AutoCallLua {
1851public:
1852 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1853 lua_getglobal(L, func);
1854 if (!lua_isfunction(L, -1)) {
1855 int t = lua_type(L, -1);
1856 SkDebugf("--- expected function %d\n", t);
1857 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001858
reed@google.com74ce6f02013-05-22 15:13:18 +00001859 lua_newtable(L);
1860 setfield_string(L, "verb", verb);
1861 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001862
reed@google.com74ce6f02013-05-22 15:13:18 +00001863 ~AutoCallLua() {
1864 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1865 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1866 }
1867 lua_settop(fL, -1);
1868 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001869
reed@google.com74ce6f02013-05-22 15:13:18 +00001870private:
1871 lua_State* fL;
1872};
1873
1874#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1875
1876///////////////////////////////////////////////////////////////////////////////
1877
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001878static int lsk_newDocumentPDF(lua_State* L) {
1879 const char* file = NULL;
1880 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1881 file = lua_tolstring(L, 1, NULL);
1882 }
1883
1884 SkDocument* doc = SkDocument::CreatePDF(file);
1885 if (NULL == doc) {
1886 // do I need to push a nil on the stack and return 1?
1887 return 0;
1888 } else {
reed9fbc3f32014-10-21 07:12:58 -07001889 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001890 return 1;
1891 }
1892}
1893
reed468b1812014-10-19 11:42:54 -07001894static int lsk_newBlurImageFilter(lua_State* L) {
1895 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1896 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1897 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1898 if (NULL == imf) {
1899 lua_pushnil(L);
1900 } else {
reed9fbc3f32014-10-21 07:12:58 -07001901 push_ref(L, imf)->unref();
1902 }
1903 return 1;
1904}
1905
1906static int lsk_newLinearGradient(lua_State* L) {
1907 SkScalar x0 = lua2scalar_def(L, 1, 0);
1908 SkScalar y0 = lua2scalar_def(L, 2, 0);
1909 SkColor c0 = lua2color(L, 3);
1910 SkScalar x1 = lua2scalar_def(L, 4, 0);
1911 SkScalar y1 = lua2scalar_def(L, 5, 0);
1912 SkColor c1 = lua2color(L, 6);
1913
1914 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1915 SkColor colors[] = { c0, c1 };
1916 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
1917 if (NULL == s) {
1918 lua_pushnil(L);
1919 } else {
1920 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001921 }
1922 return 1;
1923}
1924
reedbdc49ae2014-10-14 09:34:52 -07001925static int lsk_newMatrix(lua_State* L) {
1926 push_new<SkMatrix>(L)->reset();
1927 return 1;
1928}
1929
reed@google.com3597b732013-05-22 20:12:50 +00001930static int lsk_newPaint(lua_State* L) {
1931 push_new<SkPaint>(L);
1932 return 1;
1933}
1934
1935static int lsk_newPath(lua_State* L) {
1936 push_new<SkPath>(L);
1937 return 1;
1938}
1939
reed96affcd2014-10-13 12:38:04 -07001940static int lsk_newPictureRecorder(lua_State* L) {
1941 push_new<SkPictureRecorder>(L);
1942 return 1;
1943}
1944
reed@google.com3597b732013-05-22 20:12:50 +00001945static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001946 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001947 return 1;
1948}
1949
reed1b6ab442014-11-03 19:55:41 -08001950#include "SkTextBox.h"
1951// Sk.newTextBlob(text, rect, paint)
1952static int lsk_newTextBlob(lua_State* L) {
1953 const char* text = lua_tolstring(L, 1, NULL);
1954 SkRect bounds;
1955 lua2rect(L, 2, &bounds);
1956 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1957
1958 SkTextBox box;
1959 box.setMode(SkTextBox::kLineBreak_Mode);
1960 box.setBox(bounds);
1961 box.setText(text, strlen(text), paint);
1962
1963 SkScalar newBottom;
1964 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
1965 push_ref<SkTextBlob>(L, blob);
1966 SkLua(L).pushScalar(newBottom);
1967 return 2;
1968}
1969
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001970static int lsk_newTypeface(lua_State* L) {
1971 const char* name = NULL;
1972 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001973
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001974 int count = lua_gettop(L);
1975 if (count > 0 && lua_isstring(L, 1)) {
1976 name = lua_tolstring(L, 1, NULL);
1977 if (count > 1 && lua_isnumber(L, 2)) {
1978 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1979 }
1980 }
1981
1982 SkTypeface* face = SkTypeface::CreateFromName(name,
1983 (SkTypeface::Style)style);
1984// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1985 if (NULL == face) {
1986 face = SkTypeface::RefDefault();
1987 }
reed9fbc3f32014-10-21 07:12:58 -07001988 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001989 return 1;
1990}
reed@google.com3597b732013-05-22 20:12:50 +00001991
reed485557f2014-10-12 10:36:47 -07001992static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08001993 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07001994 int height = lua2int_def(L, 2, 0);
1995 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1996 SkSurface* surface = SkSurface::NewRaster(info);
1997 if (NULL == surface) {
1998 lua_pushnil(L);
1999 } else {
reed9fbc3f32014-10-21 07:12:58 -07002000 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07002001 }
2002 return 1;
2003}
2004
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002005static int lsk_loadImage(lua_State* L) {
2006 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
2007 const char* name = lua_tolstring(L, 1, NULL);
2008 SkAutoDataUnref data(SkData::NewFromFileName(name));
2009 if (data.get()) {
reed5965c8a2015-01-07 18:04:45 -08002010 SkImage* image = SkImage::NewFromData(data);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002011 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07002012 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002013 return 1;
2014 }
2015 }
2016 }
2017 return 0;
2018}
2019
reed@google.com3597b732013-05-22 20:12:50 +00002020static void register_Sk(lua_State* L) {
2021 lua_newtable(L);
2022 lua_pushvalue(L, -1);
2023 lua_setglobal(L, "Sk");
2024 // the Sk table is still on top
2025
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002026 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002027 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002028 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002029 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002030 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002031 setfield_function(L, "newPaint", lsk_newPaint);
2032 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002033 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002034 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002035 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002036 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002037 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00002038 lua_pop(L, 1); // pop off the Sk table
2039}
2040
reed@google.com74ce6f02013-05-22 15:13:18 +00002041#define REG_CLASS(L, C) \
2042 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002043 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002044 lua_pushvalue(L, -1); \
2045 lua_setfield(L, -2, "__index"); \
2046 luaL_setfuncs(L, g##C##_Methods, 0); \
2047 lua_pop(L, 1); /* pop off the meta-table */ \
2048 } while (0)
2049
2050void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002051 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002052 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002053 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002054 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002055 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002056 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002057 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002058 REG_CLASS(L, SkPath);
2059 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002060 REG_CLASS(L, SkPicture);
2061 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002062 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002063 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002064 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002065 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002066 REG_CLASS(L, SkTypeface);
reed@google.com74ce6f02013-05-22 15:13:18 +00002067}
zachr@google.com28c27c82013-06-20 17:15:05 +00002068
reed@google.com7bce9982013-06-20 17:40:21 +00002069extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002070extern "C" int luaopen_skia(lua_State* L) {
2071 SkLua::Load(L);
2072 return 0;
2073}