blob: 307e5f6152d0e72d445f3b051d7a3a6ea686d8af [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 }
halcanary96fcdcc2015-08-27 07:41:13 -0700505 return nullptr;
reed485557f2014-10-12 10:36:47 -0700506}
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);
halcanary96fcdcc2015-08-27 07:41:13 -0700511 if (nullptr == image) {
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000512 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700525 if (nullptr == image) {
reedba5fb932014-10-10 15:28:19 -0700526 return 0;
527 }
528
529 SkRect srcR, dstR;
halcanary96fcdcc2015-08-27 07:41:13 -0700530 SkRect* srcRPtr = nullptr;
reedba5fb932014-10-10 15:28:19 -0700531 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;
reede47829b2015-08-06 10:02:53 -0700537 canvas->legacy_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
halcanary96fcdcc2015-08-27 07:41:13 -0700546 const SkColor* colors = nullptr;
547 const SkPoint* texs = nullptr;
reed7a72c672014-11-07 10:23:55 -0800548
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
halcanary96fcdcc2015-08-27 07:41:13 -0700563 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, nullptr, *get_obj<SkPaint>(L, 5));
reed7a72c672014-11-07 10:23:55 -0800564 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700579 SkMatrix matrix, *matrixPtr = nullptr;
reed96affcd2014-10-13 12:38:04 -0700580 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,
halcanary96fcdcc2015-08-27 07:41:13 -0700649 nullptr);
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000650
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;
halcanary96fcdcc2015-08-27 07:41:13 -0700674 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(nullptr, lua2OptionalPaint(L, 2, &paint)));
reed86217d82014-10-25 20:44:40 -0700675 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700713 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -0700714 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 },
halcanary96fcdcc2015-08-27 07:41:13 -0700757 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +0000758};
759
760///////////////////////////////////////////////////////////////////////////////
761
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000762static int ldocument_beginPage(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -0700763 const SkRect* contentPtr = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000764 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 },
halcanary96fcdcc2015-08-27 07:41:13 -0700790 { nullptr, nullptr }
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000791};
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 },
halcanary96fcdcc2015-08-27 07:41:13 -07001179 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001180};
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
reedf5822822015-08-19 11:46:38 -07001203static int lshader_isABitmap(lua_State* L) {
reed@google.com5fdc9832013-07-24 15:47:52 +00001204 SkShader* shader = get_ref<SkShader>(L, 1);
1205 if (shader) {
1206 SkBitmap bm;
1207 SkMatrix matrix;
1208 SkShader::TileMode modes[2];
reedf5822822015-08-19 11:46:38 -07001209 if (shader->isABitmap(&bm, &matrix, modes)) {
1210 lua_newtable(L);
1211 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
1212 setfield_number(L, "width", bm.width());
1213 setfield_number(L, "height", bm.height());
1214 setfield_string(L, "tileX", mode2string(modes[0]));
1215 setfield_string(L, "tileY", mode2string(modes[1]));
1216 return 1;
reed@google.com5fdc9832013-07-24 15:47:52 +00001217 }
1218 }
1219 return 0;
1220}
1221
1222static int lshader_asAGradient(lua_State* L) {
1223 SkShader* shader = get_ref<SkShader>(L, 1);
1224 if (shader) {
1225 SkShader::GradientInfo info;
1226 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001227
1228 SkColor colors[3]; // hacked in for extracting info on 3 color case.
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001229 SkScalar pos[3];
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001230
1231 info.fColorCount = 3;
1232 info.fColors = &colors[0];
1233 info.fColorOffsets = &pos[0];
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001234
reed@google.com5fdc9832013-07-24 15:47:52 +00001235 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001236
reed@google.com5fdc9832013-07-24 15:47:52 +00001237 if (SkShader::kNone_GradientType != t) {
1238 lua_newtable(L);
1239 setfield_string(L, "type", gradtype2string(t));
1240 setfield_number(L, "colorCount", info.fColorCount);
1241 setfield_string(L, "tile", mode2string(info.fTileMode));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001242
1243 if (info.fColorCount == 3){
1244 setfield_number(L, "midPos", pos[1]);
1245 }
skia.committer@gmail.combd74add2013-08-02 07:00:59 +00001246
reed@google.com5fdc9832013-07-24 15:47:52 +00001247 return 1;
1248 }
1249 }
1250 return 0;
1251}
1252
1253static int lshader_gc(lua_State* L) {
1254 get_ref<SkShader>(L, 1)->unref();
1255 return 0;
1256}
1257
1258static const struct luaL_Reg gSkShader_Methods[] = {
1259 { "isOpaque", lshader_isOpaque },
reedf5822822015-08-19 11:46:38 -07001260 { "isABitmap", lshader_isABitmap },
reed@google.com5fdc9832013-07-24 15:47:52 +00001261 { "asAGradient", lshader_asAGradient },
1262 { "__gc", lshader_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001263 { nullptr, nullptr }
reed@google.com5fdc9832013-07-24 15:47:52 +00001264};
1265
1266///////////////////////////////////////////////////////////////////////////////
1267
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001268static int lpatheffect_asADash(lua_State* L) {
1269 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1270 if (pe) {
1271 SkPathEffect::DashInfo info;
1272 SkPathEffect::DashType dashType = pe->asADash(&info);
1273 if (SkPathEffect::kDash_DashType == dashType) {
1274 SkAutoTArray<SkScalar> intervals(info.fCount);
1275 info.fIntervals = intervals.get();
1276 pe->asADash(&info);
1277 SkLua(L).pushDash(info);
1278 return 1;
1279 }
1280 }
1281 return 0;
1282}
1283
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001284static int lpatheffect_gc(lua_State* L) {
1285 get_ref<SkPathEffect>(L, 1)->unref();
1286 return 0;
1287}
1288
1289static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001290 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001291 { "__gc", lpatheffect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001292 { nullptr, nullptr }
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001293};
1294
1295///////////////////////////////////////////////////////////////////////////////
1296
reed468b1812014-10-19 11:42:54 -07001297static int lpimagefilter_gc(lua_State* L) {
1298 get_ref<SkImageFilter>(L, 1)->unref();
1299 return 0;
1300}
1301
1302static const struct luaL_Reg gSkImageFilter_Methods[] = {
1303 { "__gc", lpimagefilter_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001304 { nullptr, nullptr }
reed468b1812014-10-19 11:42:54 -07001305};
1306
1307///////////////////////////////////////////////////////////////////////////////
1308
humper@google.com2815c192013-07-10 22:42:30 +00001309static int lmatrix_getType(lua_State* L) {
1310 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001311
humper@google.com2815c192013-07-10 22:42:30 +00001312 lua_newtable(L);
1313 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1314 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1315 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1316 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1317 return 1;
1318}
1319
humper@google.com0f48ee02013-07-26 15:23:43 +00001320static int lmatrix_getScaleX(lua_State* L) {
1321 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1322 return 1;
1323}
1324
1325static int lmatrix_getScaleY(lua_State* L) {
1326 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1327 return 1;
1328}
1329
1330static int lmatrix_getTranslateX(lua_State* L) {
1331 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1332 return 1;
1333}
1334
1335static int lmatrix_getTranslateY(lua_State* L) {
1336 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1337 return 1;
1338}
1339
reed7a72c672014-11-07 10:23:55 -08001340static int lmatrix_invert(lua_State* L) {
1341 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1342 return 1;
1343}
1344
1345static int lmatrix_mapXY(lua_State* L) {
1346 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1347 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1348 lua_pushnumber(L, pt.x());
1349 lua_pushnumber(L, pt.y());
1350 return 2;
1351}
1352
reedbdc49ae2014-10-14 09:34:52 -07001353static int lmatrix_setRectToRect(lua_State* L) {
1354 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1355 SkRect srcR, dstR;
1356 lua2rect(L, 2, &srcR);
1357 lua2rect(L, 3, &dstR);
1358 const char* scaleToFitStr = lua_tostring(L, 4);
1359 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1360
1361 if (scaleToFitStr) {
1362 const struct {
1363 const char* fName;
1364 SkMatrix::ScaleToFit fScaleToFit;
1365 } rec[] = {
1366 { "fill", SkMatrix::kFill_ScaleToFit },
1367 { "start", SkMatrix::kStart_ScaleToFit },
1368 { "center", SkMatrix::kCenter_ScaleToFit },
1369 { "end", SkMatrix::kEnd_ScaleToFit },
1370 };
1371
1372 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1373 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1374 scaleToFit = rec[i].fScaleToFit;
1375 break;
1376 }
1377 }
1378 }
1379
1380 matrix->setRectToRect(srcR, dstR, scaleToFit);
1381 return 0;
1382}
1383
humper@google.com2815c192013-07-10 22:42:30 +00001384static const struct luaL_Reg gSkMatrix_Methods[] = {
1385 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001386 { "getScaleX", lmatrix_getScaleX },
1387 { "getScaleY", lmatrix_getScaleY },
1388 { "getTranslateX", lmatrix_getTranslateX },
1389 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001390 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001391 { "invert", lmatrix_invert },
1392 { "mapXY", lmatrix_mapXY },
halcanary96fcdcc2015-08-27 07:41:13 -07001393 { nullptr, nullptr }
humper@google.com2815c192013-07-10 22:42:30 +00001394};
1395
1396///////////////////////////////////////////////////////////////////////////////
1397
reed@google.com74ce6f02013-05-22 15:13:18 +00001398static int lpath_getBounds(lua_State* L) {
1399 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1400 return 1;
1401}
1402
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001403static const char* fill_type_to_str(SkPath::FillType fill) {
1404 switch (fill) {
1405 case SkPath::kEvenOdd_FillType:
1406 return "even-odd";
1407 case SkPath::kWinding_FillType:
1408 return "winding";
1409 case SkPath::kInverseEvenOdd_FillType:
1410 return "inverse-even-odd";
1411 case SkPath::kInverseWinding_FillType:
1412 return "inverse-winding";
1413 }
1414 return "unknown";
1415}
1416
1417static int lpath_getFillType(lua_State* L) {
1418 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1419 SkLua(L).pushString(fill_type_to_str(fill));
1420 return 1;
1421}
1422
1423static SkString segment_masks_to_str(uint32_t segmentMasks) {
1424 SkString result;
1425 bool first = true;
1426 if (SkPath::kLine_SegmentMask & segmentMasks) {
1427 result.append("line");
1428 first = false;
1429 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1430 }
1431 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1432 if (!first) {
1433 result.append(" ");
1434 }
1435 result.append("quad");
1436 first = false;
1437 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1438 }
1439 if (SkPath::kConic_SegmentMask & segmentMasks) {
1440 if (!first) {
1441 result.append(" ");
1442 }
1443 result.append("conic");
1444 first = false;
1445 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1446 }
1447 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1448 if (!first) {
1449 result.append(" ");
1450 }
1451 result.append("cubic");
1452 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1453 }
1454 SkASSERT(0 == segmentMasks);
1455 return result;
1456}
1457
krajcevski95498ed2014-08-18 08:02:33 -07001458static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001459 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1460 SkLua(L).pushString(segment_masks_to_str(segMasks));
1461 return 1;
1462}
1463
1464static int lpath_isConvex(lua_State* L) {
1465 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1466 SkLua(L).pushBool(isConvex);
1467 return 1;
1468}
1469
reed@google.com74ce6f02013-05-22 15:13:18 +00001470static int lpath_isEmpty(lua_State* L) {
1471 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1472 return 1;
1473}
1474
1475static int lpath_isRect(lua_State* L) {
1476 SkRect r;
1477 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1478 int ret_count = 1;
1479 lua_pushboolean(L, pred);
1480 if (pred) {
1481 SkLua(L).pushRect(r);
1482 ret_count += 1;
1483 }
1484 return ret_count;
1485}
1486
1487static const char* dir2string(SkPath::Direction dir) {
1488 static const char* gStr[] = {
1489 "unknown", "cw", "ccw"
1490 };
1491 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1492 return gStr[dir];
1493}
1494
caryclark95bc5f32015-04-08 08:34:15 -07001495static int lpath_isNestedFillRects(lua_State* L) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001496 SkRect rects[2];
1497 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001498 bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
reed@google.com74ce6f02013-05-22 15:13:18 +00001499 int ret_count = 1;
1500 lua_pushboolean(L, pred);
1501 if (pred) {
1502 SkLua lua(L);
1503 lua.pushRect(rects[0]);
1504 lua.pushRect(rects[1]);
1505 lua_pushstring(L, dir2string(dirs[0]));
1506 lua_pushstring(L, dir2string(dirs[0]));
1507 ret_count += 4;
1508 }
1509 return ret_count;
1510}
1511
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001512static int lpath_countPoints(lua_State* L) {
1513 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1514 return 1;
1515}
1516
reed@google.com74ce6f02013-05-22 15:13:18 +00001517static int lpath_reset(lua_State* L) {
1518 get_obj<SkPath>(L, 1)->reset();
1519 return 0;
1520}
1521
1522static int lpath_moveTo(lua_State* L) {
1523 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1524 return 0;
1525}
1526
1527static int lpath_lineTo(lua_State* L) {
1528 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1529 return 0;
1530}
1531
1532static int lpath_quadTo(lua_State* L) {
1533 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1534 lua2scalar(L, 4), lua2scalar(L, 5));
1535 return 0;
1536}
1537
1538static int lpath_cubicTo(lua_State* L) {
1539 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1540 lua2scalar(L, 4), lua2scalar(L, 5),
1541 lua2scalar(L, 6), lua2scalar(L, 7));
1542 return 0;
1543}
1544
1545static int lpath_close(lua_State* L) {
1546 get_obj<SkPath>(L, 1)->close();
1547 return 0;
1548}
1549
1550static int lpath_gc(lua_State* L) {
1551 get_obj<SkPath>(L, 1)->~SkPath();
1552 return 0;
1553}
1554
1555static const struct luaL_Reg gSkPath_Methods[] = {
1556 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001557 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001558 { "getSegmentTypes", lpath_getSegmentTypes },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001559 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001560 { "isEmpty", lpath_isEmpty },
1561 { "isRect", lpath_isRect },
caryclark95bc5f32015-04-08 08:34:15 -07001562 { "isNestedFillRects", lpath_isNestedFillRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001563 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001564 { "reset", lpath_reset },
1565 { "moveTo", lpath_moveTo },
1566 { "lineTo", lpath_lineTo },
1567 { "quadTo", lpath_quadTo },
1568 { "cubicTo", lpath_cubicTo },
1569 { "close", lpath_close },
1570 { "__gc", lpath_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001571 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001572};
1573
1574///////////////////////////////////////////////////////////////////////////////
1575
1576static const char* rrect_type(const SkRRect& rr) {
1577 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001578 case SkRRect::kEmpty_Type: return "empty";
1579 case SkRRect::kRect_Type: return "rect";
1580 case SkRRect::kOval_Type: return "oval";
1581 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001582 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001583 case SkRRect::kComplex_Type: return "complex";
1584 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001585 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001586 return "";
1587}
1588
1589static int lrrect_rect(lua_State* L) {
1590 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1591 return 1;
1592}
1593
1594static int lrrect_type(lua_State* L) {
1595 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1596 return 1;
1597}
1598
1599static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001600 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001601 SkVector v;
1602 if (corner < 0 || corner > 3) {
1603 SkDebugf("bad corner index %d", corner);
1604 v.set(0, 0);
1605 } else {
1606 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1607 }
1608 lua_pushnumber(L, v.fX);
1609 lua_pushnumber(L, v.fY);
1610 return 2;
1611}
1612
1613static int lrrect_gc(lua_State* L) {
1614 get_obj<SkRRect>(L, 1)->~SkRRect();
1615 return 0;
1616}
1617
1618static const struct luaL_Reg gSkRRect_Methods[] = {
1619 { "rect", lrrect_rect },
1620 { "type", lrrect_type },
1621 { "radii", lrrect_radii },
1622 { "__gc", lrrect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001623 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001624};
1625
1626///////////////////////////////////////////////////////////////////////////////
1627
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001628static int limage_width(lua_State* L) {
1629 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1630 return 1;
1631}
1632
1633static int limage_height(lua_State* L) {
1634 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1635 return 1;
1636}
1637
reed7a72c672014-11-07 10:23:55 -08001638static int limage_newShader(lua_State* L) {
1639 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
halcanary96fcdcc2015-08-27 07:41:13 -07001640 const SkMatrix* localM = nullptr;
reed7a72c672014-11-07 10:23:55 -08001641 SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM));
1642 push_ref(L, shader.get());
1643 return 1;
1644}
1645
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001646static int limage_gc(lua_State* L) {
1647 get_ref<SkImage>(L, 1)->unref();
1648 return 0;
1649}
1650
1651static const struct luaL_Reg gSkImage_Methods[] = {
1652 { "width", limage_width },
1653 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001654 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001655 { "__gc", limage_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001656 { nullptr, nullptr }
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001657};
1658
1659///////////////////////////////////////////////////////////////////////////////
1660
reed485557f2014-10-12 10:36:47 -07001661static int lsurface_width(lua_State* L) {
1662 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1663 return 1;
1664}
1665
1666static int lsurface_height(lua_State* L) {
1667 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1668 return 1;
1669}
1670
1671static int lsurface_getCanvas(lua_State* L) {
1672 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001673 if (nullptr == canvas) {
reed485557f2014-10-12 10:36:47 -07001674 lua_pushnil(L);
1675 } else {
1676 push_ref(L, canvas);
1677 // note: we don't unref canvas, since getCanvas did not ref it.
1678 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1679 // the real owner (the surface) go away, but still hold onto the canvas?
1680 // *really* we want to sort of ref the surface again, but have the native object
1681 // know that it is supposed to be treated as a canvas...
1682 }
1683 return 1;
1684}
1685
1686static int lsurface_newImageSnapshot(lua_State* L) {
1687 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot();
halcanary96fcdcc2015-08-27 07:41:13 -07001688 if (nullptr == image) {
reed485557f2014-10-12 10:36:47 -07001689 lua_pushnil(L);
1690 } else {
reed9fbc3f32014-10-21 07:12:58 -07001691 push_ref(L, image)->unref();
reed485557f2014-10-12 10:36:47 -07001692 }
1693 return 1;
1694}
1695
1696static int lsurface_newSurface(lua_State* L) {
1697 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001698 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001699 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1700 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -07001701 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07001702 lua_pushnil(L);
1703 } else {
reed9fbc3f32014-10-21 07:12:58 -07001704 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001705 }
1706 return 1;
1707}
1708
1709static int lsurface_gc(lua_State* L) {
1710 get_ref<SkSurface>(L, 1)->unref();
1711 return 0;
1712}
1713
1714static const struct luaL_Reg gSkSurface_Methods[] = {
1715 { "width", lsurface_width },
1716 { "height", lsurface_height },
1717 { "getCanvas", lsurface_getCanvas },
1718 { "newImageSnapshot", lsurface_newImageSnapshot },
1719 { "newSurface", lsurface_newSurface },
1720 { "__gc", lsurface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001721 { nullptr, nullptr }
reed485557f2014-10-12 10:36:47 -07001722};
1723
1724///////////////////////////////////////////////////////////////////////////////
1725
reed96affcd2014-10-13 12:38:04 -07001726static int lpicturerecorder_beginRecording(lua_State* L) {
1727 const SkScalar w = lua2scalar_def(L, 2, -1);
1728 const SkScalar h = lua2scalar_def(L, 3, -1);
1729 if (w <= 0 || h <= 0) {
1730 lua_pushnil(L);
1731 return 1;
1732 }
1733
1734 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
halcanary96fcdcc2015-08-27 07:41:13 -07001735 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001736 lua_pushnil(L);
1737 return 1;
1738 }
1739
1740 push_ref(L, canvas);
1741 return 1;
1742}
1743
1744static int lpicturerecorder_getCanvas(lua_State* L) {
1745 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001746 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001747 lua_pushnil(L);
1748 return 1;
1749 }
1750 push_ref(L, canvas);
1751 return 1;
1752}
1753
1754static int lpicturerecorder_endRecording(lua_State* L) {
1755 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording();
halcanary96fcdcc2015-08-27 07:41:13 -07001756 if (nullptr == pic) {
reed96affcd2014-10-13 12:38:04 -07001757 lua_pushnil(L);
1758 return 1;
1759 }
reed9fbc3f32014-10-21 07:12:58 -07001760 push_ref(L, pic)->unref();
reed96affcd2014-10-13 12:38:04 -07001761 return 1;
1762}
1763
1764static int lpicturerecorder_gc(lua_State* L) {
1765 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1766 return 0;
1767}
1768
1769static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1770 { "beginRecording", lpicturerecorder_beginRecording },
1771 { "getCanvas", lpicturerecorder_getCanvas },
1772 { "endRecording", lpicturerecorder_endRecording },
1773 { "__gc", lpicturerecorder_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001774 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001775};
1776
1777///////////////////////////////////////////////////////////////////////////////
1778
1779static int lpicture_width(lua_State* L) {
1780 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1781 return 1;
1782}
1783
1784static int lpicture_height(lua_State* L) {
1785 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1786 return 1;
1787}
1788
1789static int lpicture_gc(lua_State* L) {
1790 get_ref<SkPicture>(L, 1)->unref();
1791 return 0;
1792}
1793
1794static const struct luaL_Reg gSkPicture_Methods[] = {
1795 { "width", lpicture_width },
1796 { "height", lpicture_height },
1797 { "__gc", lpicture_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001798 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001799};
1800
1801///////////////////////////////////////////////////////////////////////////////
1802
reed1b6ab442014-11-03 19:55:41 -08001803static int ltextblob_bounds(lua_State* L) {
1804 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1805 return 1;
1806}
1807
1808static int ltextblob_gc(lua_State* L) {
1809 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1810 return 0;
1811}
1812
1813static const struct luaL_Reg gSkTextBlob_Methods[] = {
1814 { "bounds", ltextblob_bounds },
1815 { "__gc", ltextblob_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001816 { nullptr, nullptr }
reed1b6ab442014-11-03 19:55:41 -08001817};
1818
1819///////////////////////////////////////////////////////////////////////////////
1820
reed36c9c112014-11-04 10:58:42 -08001821static int ltypeface_getFamilyName(lua_State* L) {
1822 SkString str;
1823 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1824 lua_pushstring(L, str.c_str());
1825 return 1;
1826}
1827
1828static int ltypeface_getStyle(lua_State* L) {
1829 lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style());
1830 return 1;
1831}
1832
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001833static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001834 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001835 return 0;
1836}
1837
1838static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001839 { "getFamilyName", ltypeface_getFamilyName },
1840 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001841 { "__gc", ltypeface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001842 { nullptr, nullptr }
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001843};
1844
1845///////////////////////////////////////////////////////////////////////////////
1846
reed@google.com74ce6f02013-05-22 15:13:18 +00001847class AutoCallLua {
1848public:
1849 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1850 lua_getglobal(L, func);
1851 if (!lua_isfunction(L, -1)) {
1852 int t = lua_type(L, -1);
1853 SkDebugf("--- expected function %d\n", t);
1854 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001855
reed@google.com74ce6f02013-05-22 15:13:18 +00001856 lua_newtable(L);
1857 setfield_string(L, "verb", verb);
1858 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001859
reed@google.com74ce6f02013-05-22 15:13:18 +00001860 ~AutoCallLua() {
1861 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1862 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1863 }
1864 lua_settop(fL, -1);
1865 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001866
reed@google.com74ce6f02013-05-22 15:13:18 +00001867private:
1868 lua_State* fL;
1869};
1870
1871#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1872
1873///////////////////////////////////////////////////////////////////////////////
1874
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001875static int lsk_newDocumentPDF(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001876 const char* file = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001877 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001878 file = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001879 }
1880
1881 SkDocument* doc = SkDocument::CreatePDF(file);
halcanary96fcdcc2015-08-27 07:41:13 -07001882 if (nullptr == doc) {
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001883 // do I need to push a nil on the stack and return 1?
1884 return 0;
1885 } else {
reed9fbc3f32014-10-21 07:12:58 -07001886 push_ref(L, doc)->unref();
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001887 return 1;
1888 }
1889}
1890
reed468b1812014-10-19 11:42:54 -07001891static int lsk_newBlurImageFilter(lua_State* L) {
1892 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1893 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1894 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
halcanary96fcdcc2015-08-27 07:41:13 -07001895 if (nullptr == imf) {
reed468b1812014-10-19 11:42:54 -07001896 lua_pushnil(L);
1897 } else {
reed9fbc3f32014-10-21 07:12:58 -07001898 push_ref(L, imf)->unref();
1899 }
1900 return 1;
1901}
1902
1903static int lsk_newLinearGradient(lua_State* L) {
1904 SkScalar x0 = lua2scalar_def(L, 1, 0);
1905 SkScalar y0 = lua2scalar_def(L, 2, 0);
1906 SkColor c0 = lua2color(L, 3);
1907 SkScalar x1 = lua2scalar_def(L, 4, 0);
1908 SkScalar y1 = lua2scalar_def(L, 5, 0);
1909 SkColor c1 = lua2color(L, 6);
1910
1911 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1912 SkColor colors[] = { c0, c1 };
halcanary96fcdcc2015-08-27 07:41:13 -07001913 SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
1914 if (nullptr == s) {
reed9fbc3f32014-10-21 07:12:58 -07001915 lua_pushnil(L);
1916 } else {
1917 push_ref(L, s)->unref();
reed468b1812014-10-19 11:42:54 -07001918 }
1919 return 1;
1920}
1921
reedbdc49ae2014-10-14 09:34:52 -07001922static int lsk_newMatrix(lua_State* L) {
1923 push_new<SkMatrix>(L)->reset();
1924 return 1;
1925}
1926
reed@google.com3597b732013-05-22 20:12:50 +00001927static int lsk_newPaint(lua_State* L) {
1928 push_new<SkPaint>(L);
1929 return 1;
1930}
1931
1932static int lsk_newPath(lua_State* L) {
1933 push_new<SkPath>(L);
1934 return 1;
1935}
1936
reed96affcd2014-10-13 12:38:04 -07001937static int lsk_newPictureRecorder(lua_State* L) {
1938 push_new<SkPictureRecorder>(L);
1939 return 1;
1940}
1941
reed@google.com3597b732013-05-22 20:12:50 +00001942static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001943 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001944 return 1;
1945}
1946
reed1b6ab442014-11-03 19:55:41 -08001947#include "SkTextBox.h"
1948// Sk.newTextBlob(text, rect, paint)
1949static int lsk_newTextBlob(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001950 const char* text = lua_tolstring(L, 1, nullptr);
reed1b6ab442014-11-03 19:55:41 -08001951 SkRect bounds;
1952 lua2rect(L, 2, &bounds);
1953 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1954
1955 SkTextBox box;
1956 box.setMode(SkTextBox::kLineBreak_Mode);
1957 box.setBox(bounds);
1958 box.setText(text, strlen(text), paint);
1959
1960 SkScalar newBottom;
1961 SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom));
1962 push_ref<SkTextBlob>(L, blob);
1963 SkLua(L).pushScalar(newBottom);
1964 return 2;
1965}
1966
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001967static int lsk_newTypeface(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001968 const char* name = nullptr;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001969 int style = SkTypeface::kNormal;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001970
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001971 int count = lua_gettop(L);
1972 if (count > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001973 name = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001974 if (count > 1 && lua_isnumber(L, 2)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001975 style = lua_tointegerx(L, 2, nullptr) & SkTypeface::kBoldItalic;
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001976 }
1977 }
1978
1979 SkTypeface* face = SkTypeface::CreateFromName(name,
1980 (SkTypeface::Style)style);
1981// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
halcanary96fcdcc2015-08-27 07:41:13 -07001982 if (nullptr == face) {
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001983 face = SkTypeface::RefDefault();
1984 }
reed9fbc3f32014-10-21 07:12:58 -07001985 push_ref(L, face)->unref();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001986 return 1;
1987}
reed@google.com3597b732013-05-22 20:12:50 +00001988
reed485557f2014-10-12 10:36:47 -07001989static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08001990 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07001991 int height = lua2int_def(L, 2, 0);
1992 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
robertphillips702edbd2015-06-23 06:26:08 -07001993 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
1994 SkSurface* surface = SkSurface::NewRaster(info, &props);
halcanary96fcdcc2015-08-27 07:41:13 -07001995 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07001996 lua_pushnil(L);
1997 } else {
reed9fbc3f32014-10-21 07:12:58 -07001998 push_ref(L, surface)->unref();
reed485557f2014-10-12 10:36:47 -07001999 }
2000 return 1;
2001}
2002
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002003static int lsk_loadImage(lua_State* L) {
2004 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002005 const char* name = lua_tolstring(L, 1, nullptr);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002006 SkAutoDataUnref data(SkData::NewFromFileName(name));
2007 if (data.get()) {
reed871872f2015-06-22 12:48:26 -07002008 SkImage* image = SkImage::NewFromEncoded(data);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002009 if (image) {
reed9fbc3f32014-10-21 07:12:58 -07002010 push_ref(L, image)->unref();
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002011 return 1;
2012 }
2013 }
2014 }
2015 return 0;
2016}
2017
reed@google.com3597b732013-05-22 20:12:50 +00002018static void register_Sk(lua_State* L) {
2019 lua_newtable(L);
2020 lua_pushvalue(L, -1);
2021 lua_setglobal(L, "Sk");
2022 // the Sk table is still on top
2023
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002024 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002025 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002026 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002027 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002028 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002029 setfield_function(L, "newPaint", lsk_newPaint);
2030 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002031 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002032 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002033 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002034 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002035 setfield_function(L, "newTypeface", lsk_newTypeface);
reed@google.com3597b732013-05-22 20:12:50 +00002036 lua_pop(L, 1); // pop off the Sk table
2037}
2038
reed@google.com74ce6f02013-05-22 15:13:18 +00002039#define REG_CLASS(L, C) \
2040 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002041 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002042 lua_pushvalue(L, -1); \
2043 lua_setfield(L, -2, "__index"); \
2044 luaL_setfuncs(L, g##C##_Methods, 0); \
2045 lua_pop(L, 1); /* pop off the meta-table */ \
2046 } while (0)
2047
2048void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002049 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002050 REG_CLASS(L, SkCanvas);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002051 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002052 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002053 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002054 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002055 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002056 REG_CLASS(L, SkPath);
2057 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002058 REG_CLASS(L, SkPicture);
2059 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002060 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002061 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002062 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002063 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002064 REG_CLASS(L, SkTypeface);
reed@google.com74ce6f02013-05-22 15:13:18 +00002065}
zachr@google.com28c27c82013-06-20 17:15:05 +00002066
reed@google.com7bce9982013-06-20 17:40:21 +00002067extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002068extern "C" int luaopen_skia(lua_State* L) {
2069 SkLua::Load(L);
2070 return 0;
2071}