blob: 445a5a8b95a0e01db60a54b37d9b79748d3e3be8 [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
Mike Reed4f364fd2017-01-19 14:34:51 -050011//#include "GrReducedClip.h"
bsalomon@google.com4ebe3822014-02-26 20:22:32 +000012#endif
13
reed468b1812014-10-19 11:42:54 -070014#include "SkBlurImageFilter.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000015#include "SkCanvas.h"
reed22a517f2015-12-04 20:45:59 -080016#include "SkColorFilter.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000017#include "SkData.h"
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000018#include "SkDocument.h"
Ben Wagnerc6c10b42017-08-07 09:56:21 -040019#include "SkFontStyle.h"
reed9fbc3f32014-10-21 07:12:58 -070020#include "SkGradientShader.h"
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000021#include "SkImage.h"
22#include "SkMatrix.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000023#include "SkPaint.h"
24#include "SkPath.h"
reed96affcd2014-10-13 12:38:04 -070025#include "SkPictureRecorder.h"
reed@google.com5fdc9832013-07-24 15:47:52 +000026#include "SkPixelRef.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000027#include "SkRRect.h"
28#include "SkString.h"
reed485557f2014-10-12 10:36:47 -070029#include "SkSurface.h"
fmalitab7425172014-08-26 07:56:44 -070030#include "SkTextBlob.h"
reed@google.come3823fd2013-05-30 18:55:14 +000031#include "SkTypeface.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000032
33extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000034 #include "lua.h"
35 #include "lualib.h"
36 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000037}
38
reed@google.comfd345872013-05-22 20:53:42 +000039// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000040template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000041#define DEF_MTNAME(T) \
42 template <> const char* get_mtname<T>() { \
43 return #T "_LuaMetaTableName"; \
44 }
45
46DEF_MTNAME(SkCanvas)
reed22a517f2015-12-04 20:45:59 -080047DEF_MTNAME(SkColorFilter)
mike@reedtribe.orgfb858242013-06-08 16:39:44 +000048DEF_MTNAME(SkDocument)
mike@reedtribe.org792bbd12013-06-11 02:20:28 +000049DEF_MTNAME(SkImage)
reed468b1812014-10-19 11:42:54 -070050DEF_MTNAME(SkImageFilter)
reed@google.comfd345872013-05-22 20:53:42 +000051DEF_MTNAME(SkMatrix)
52DEF_MTNAME(SkRRect)
53DEF_MTNAME(SkPath)
54DEF_MTNAME(SkPaint)
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +000055DEF_MTNAME(SkPathEffect)
reed96affcd2014-10-13 12:38:04 -070056DEF_MTNAME(SkPicture)
57DEF_MTNAME(SkPictureRecorder)
reed@google.com5fdc9832013-07-24 15:47:52 +000058DEF_MTNAME(SkShader)
reed485557f2014-10-12 10:36:47 -070059DEF_MTNAME(SkSurface)
fmalitab7425172014-08-26 07:56:44 -070060DEF_MTNAME(SkTextBlob)
mike@reedtribe.orge6469f12013-06-08 03:15:47 +000061DEF_MTNAME(SkTypeface)
Ben Wagnerc6c10b42017-08-07 09:56:21 -040062DEF_MTNAME(SkFontStyle)
reed@google.com74ce6f02013-05-22 15:13:18 +000063
Ben Wagnerc6c10b42017-08-07 09:56:21 -040064template <typename T, typename... Args> T* push_new(lua_State* L, Args&&... args) {
reed@google.com3597b732013-05-22 20:12:50 +000065 T* addr = (T*)lua_newuserdata(L, sizeof(T));
Ben Wagnerc6c10b42017-08-07 09:56:21 -040066 new (addr) T(std::forward<Args>(args)...);
reed@google.com3597b732013-05-22 20:12:50 +000067 luaL_getmetatable(L, get_mtname<T>());
68 lua_setmetatable(L, -2);
69 return addr;
70}
reed@google.com74ce6f02013-05-22 15:13:18 +000071
72template <typename T> void push_obj(lua_State* L, const T& obj) {
73 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000074 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000075 lua_setmetatable(L, -2);
76}
77
Mike Reed5df49342016-11-12 08:06:55 -060078template <typename T> T* push_ptr(lua_State* L, T* ptr) {
79 *(T**)lua_newuserdata(L, sizeof(T*)) = ptr;
80 luaL_getmetatable(L, get_mtname<T>());
81 lua_setmetatable(L, -2);
82 return ptr;
83}
84
reed9fbc3f32014-10-21 07:12:58 -070085template <typename T> T* push_ref(lua_State* L, T* ref) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +000086 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000087 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000088 lua_setmetatable(L, -2);
reed9fbc3f32014-10-21 07:12:58 -070089 return ref;
reed@google.com74ce6f02013-05-22 15:13:18 +000090}
91
reed2ad1aa62016-03-09 09:50:50 -080092template <typename T> void push_ref(lua_State* L, sk_sp<T> sp) {
93 *(T**)lua_newuserdata(L, sizeof(T*)) = sp.release();
94 luaL_getmetatable(L, get_mtname<T>());
95 lua_setmetatable(L, -2);
96}
97
reed@google.com74ce6f02013-05-22 15:13:18 +000098template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000099 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +0000100}
101
102template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +0000103 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +0000104}
105
reed@google.com88c9ec92013-05-22 15:43:21 +0000106static bool lua2bool(lua_State* L, int index) {
107 return !!lua_toboolean(L, index);
108}
109
reed@google.com74ce6f02013-05-22 15:13:18 +0000110///////////////////////////////////////////////////////////////////////////////
111
reed@google.com3597b732013-05-22 20:12:50 +0000112SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
113 fL = luaL_newstate();
114 luaL_openlibs(fL);
115 SkLua::Load(fL);
116}
117
118SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
119
120SkLua::~SkLua() {
121 if (fWeOwnL) {
122 if (fTermCode.size() > 0) {
123 lua_getglobal(fL, fTermCode.c_str());
124 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
125 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
126 }
127 }
128 lua_close(fL);
129 }
130}
131
132bool SkLua::runCode(const char code[]) {
133 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
134 if (err) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000135 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
reed@google.com3597b732013-05-22 20:12:50 +0000136 return false;
137 }
138 return true;
139}
140
141bool SkLua::runCode(const void* code, size_t size) {
142 SkString str((const char*)code, size);
143 return this->runCode(str.c_str());
144}
145
146///////////////////////////////////////////////////////////////////////////////
147
148#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
149
reed@google.com29563872013-07-10 21:23:49 +0000150static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
151 if (pred) {
152 lua_pushboolean(L, true);
153 lua_setfield(L, -2, key);
154 }
155}
156
reed@google.com74ce6f02013-05-22 15:13:18 +0000157static void setfield_string(lua_State* L, const char key[], const char value[]) {
158 lua_pushstring(L, value);
159 lua_setfield(L, -2, key);
160}
161
162static void setfield_number(lua_State* L, const char key[], double value) {
163 lua_pushnumber(L, value);
164 lua_setfield(L, -2, key);
165}
166
humper@google.com2815c192013-07-10 22:42:30 +0000167static void setfield_boolean(lua_State* L, const char key[], bool value) {
168 lua_pushboolean(L, value);
169 lua_setfield(L, -2, key);
170}
171
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000172static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
173 setfield_number(L, key, SkScalarToLua(value));
174}
175
reed@google.com3597b732013-05-22 20:12:50 +0000176static void setfield_function(lua_State* L,
177 const char key[], lua_CFunction value) {
178 lua_pushcfunction(L, value);
179 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000180}
181
reed7a72c672014-11-07 10:23:55 -0800182static int lua2int_def(lua_State* L, int index, int defaultValue) {
183 if (lua_isnumber(L, index)) {
184 return (int)lua_tonumber(L, index);
185 } else {
186 return defaultValue;
187 }
188}
189
190static SkScalar lua2scalar(lua_State* L, int index) {
191 SkASSERT(lua_isnumber(L, index));
192 return SkLuaToScalar(lua_tonumber(L, index));
193}
194
195static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
196 if (lua_isnumber(L, index)) {
197 return SkLuaToScalar(lua_tonumber(L, index));
198 } else {
199 return defaultValue;
200 }
201}
202
203static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) {
204 SkASSERT(lua_istable(L, stackIndex));
205 lua_rawgeti(L, stackIndex, arrayIndex);
mtklein8aacf202014-12-18 13:29:54 -0800206
reed7a72c672014-11-07 10:23:55 -0800207 SkScalar value = lua2scalar(L, -1);
208 lua_pop(L, 1);
209 return value;
210}
211
212static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) {
213 for (int i = 0; i < count; ++i) {
214 dst[i] = getarray_scalar(L, stackIndex, i + 1);
215 }
216}
217
218static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) {
219 getarray_scalars(L, stackIndex, &pts[0].fX, count * 2);
220}
221
reed@google.come3823fd2013-05-30 18:55:14 +0000222static void setarray_number(lua_State* L, int index, double value) {
223 lua_pushnumber(L, value);
224 lua_rawseti(L, -2, index);
225}
226
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000227static void setarray_scalar(lua_State* L, int index, SkScalar value) {
228 setarray_number(L, index, SkScalarToLua(value));
229}
230
hstern0b401ce2016-08-02 09:17:59 -0700231static void setarray_string(lua_State* L, int index, const char str[]) {
232 lua_pushstring(L, str);
233 lua_rawseti(L, -2, index);
234}
235
reed@google.com74ce6f02013-05-22 15:13:18 +0000236void SkLua::pushBool(bool value, const char key[]) {
237 lua_pushboolean(fL, value);
238 CHECK_SETFIELD(key);
239}
240
241void SkLua::pushString(const char str[], const char key[]) {
242 lua_pushstring(fL, str);
243 CHECK_SETFIELD(key);
244}
245
reed@google.come3823fd2013-05-30 18:55:14 +0000246void SkLua::pushString(const char str[], size_t length, const char key[]) {
247 // TODO: how to do this w/o making a copy?
248 SkString s(str, length);
249 lua_pushstring(fL, s.c_str());
250 CHECK_SETFIELD(key);
251}
252
reed@google.com74ce6f02013-05-22 15:13:18 +0000253void SkLua::pushString(const SkString& str, const char key[]) {
254 lua_pushstring(fL, str.c_str());
255 CHECK_SETFIELD(key);
256}
257
258void SkLua::pushColor(SkColor color, const char key[]) {
259 lua_newtable(fL);
260 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
261 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
262 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
263 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
264 CHECK_SETFIELD(key);
265}
266
reed@google.come3823fd2013-05-30 18:55:14 +0000267void SkLua::pushU32(uint32_t value, const char key[]) {
268 lua_pushnumber(fL, (double)value);
269 CHECK_SETFIELD(key);
270}
271
reed@google.com74ce6f02013-05-22 15:13:18 +0000272void SkLua::pushScalar(SkScalar value, const char key[]) {
273 lua_pushnumber(fL, SkScalarToLua(value));
274 CHECK_SETFIELD(key);
275}
276
reed@google.come3823fd2013-05-30 18:55:14 +0000277void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
278 lua_newtable(fL);
279 for (int i = 0; i < count; ++i) {
280 // make it base-1 to match lua convention
281 setarray_number(fL, i + 1, (double)array[i]);
282 }
283 CHECK_SETFIELD(key);
284}
285
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +0000286void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
287 lua_newtable(fL);
288 for (int i = 0; i < count; ++i) {
289 // make it base-1 to match lua convention
290 lua_newtable(fL);
291 this->pushScalar(array[i].fX, "x");
292 this->pushScalar(array[i].fY, "y");
293 lua_rawseti(fL, -2, i + 1);
294 }
295 CHECK_SETFIELD(key);
296}
297
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000298void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
299 lua_newtable(fL);
300 for (int i = 0; i < count; ++i) {
301 // make it base-1 to match lua convention
302 setarray_scalar(fL, i + 1, array[i]);
303 }
304 CHECK_SETFIELD(key);
305}
306
reed@google.com74ce6f02013-05-22 15:13:18 +0000307void SkLua::pushRect(const SkRect& r, const char key[]) {
308 lua_newtable(fL);
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000309 setfield_scalar(fL, "left", r.fLeft);
310 setfield_scalar(fL, "top", r.fTop);
311 setfield_scalar(fL, "right", r.fRight);
312 setfield_scalar(fL, "bottom", r.fBottom);
reed@google.com74ce6f02013-05-22 15:13:18 +0000313 CHECK_SETFIELD(key);
314}
315
316void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
317 push_obj(fL, rr);
318 CHECK_SETFIELD(key);
319}
320
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +0000321void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
322 lua_newtable(fL);
323 setfield_scalar(fL, "phase", info.fPhase);
324 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
325 CHECK_SETFIELD(key);
326}
327
328
reed@google.com74ce6f02013-05-22 15:13:18 +0000329void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
330 push_obj(fL, matrix);
331 CHECK_SETFIELD(key);
332}
333
334void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
335 push_obj(fL, paint);
336 CHECK_SETFIELD(key);
337}
338
339void SkLua::pushPath(const SkPath& path, const char key[]) {
340 push_obj(fL, path);
341 CHECK_SETFIELD(key);
342}
343
344void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
Mike Reed5df49342016-11-12 08:06:55 -0600345 push_ptr(fL, canvas);
reed@google.com74ce6f02013-05-22 15:13:18 +0000346 CHECK_SETFIELD(key);
347}
348
fmalitab7425172014-08-26 07:56:44 -0700349void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
350 push_ref(fL, const_cast<SkTextBlob*>(blob));
351 CHECK_SETFIELD(key);
352}
353
reed@google.com74ce6f02013-05-22 15:13:18 +0000354///////////////////////////////////////////////////////////////////////////////
355///////////////////////////////////////////////////////////////////////////////
356
reed@google.com74ce6f02013-05-22 15:13:18 +0000357static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
358 SkASSERT(lua_istable(L, index));
359 lua_pushstring(L, key);
360 lua_gettable(L, index);
mtklein8aacf202014-12-18 13:29:54 -0800361
reed@google.com74ce6f02013-05-22 15:13:18 +0000362 SkScalar value = lua2scalar(L, -1);
363 lua_pop(L, 1);
364 return value;
365}
366
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000367static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
368 SkASSERT(lua_istable(L, index));
369 lua_pushstring(L, key);
370 lua_gettable(L, index);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000371
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000372 SkScalar value;
373 if (lua_isnil(L, -1)) {
374 value = def;
375 } else {
376 value = lua2scalar(L, -1);
377 }
378 lua_pop(L, 1);
379 return value;
380}
381
reed468b1812014-10-19 11:42:54 -0700382static SkScalar byte2unit(U8CPU byte) {
383 return byte / 255.0f;
384}
385
reed@google.com74ce6f02013-05-22 15:13:18 +0000386static U8CPU unit2byte(SkScalar x) {
387 if (x <= 0) {
388 return 0;
389 } else if (x >= 1) {
390 return 255;
391 } else {
392 return SkScalarRoundToInt(x * 255);
393 }
394}
395
396static SkColor lua2color(lua_State* L, int index) {
reed485557f2014-10-12 10:36:47 -0700397 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)),
398 unit2byte(getfield_scalar_default(L, index, "r", 0)),
399 unit2byte(getfield_scalar_default(L, index, "g", 0)),
400 unit2byte(getfield_scalar_default(L, index, "b", 0)));
reed@google.com74ce6f02013-05-22 15:13:18 +0000401}
402
403static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000404 rect->set(getfield_scalar_default(L, index, "left", 0),
405 getfield_scalar_default(L, index, "top", 0),
reed@google.com74ce6f02013-05-22 15:13:18 +0000406 getfield_scalar(L, index, "right"),
407 getfield_scalar(L, index, "bottom"));
408 return rect;
409}
410
reedf355df52014-10-12 12:18:40 -0700411static int lcanvas_clear(lua_State* L) {
412 get_ref<SkCanvas>(L, 1)->clear(0);
413 return 0;
414}
415
reed@google.com74ce6f02013-05-22 15:13:18 +0000416static int lcanvas_drawColor(lua_State* L) {
417 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
418 return 0;
419}
420
reed9fbc3f32014-10-21 07:12:58 -0700421static int lcanvas_drawPaint(lua_State* L) {
422 get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
423 return 0;
424}
425
reed@google.com74ce6f02013-05-22 15:13:18 +0000426static int lcanvas_drawRect(lua_State* L) {
427 SkRect rect;
reed7a72c672014-11-07 10:23:55 -0800428 lua2rect(L, 2, &rect);
429 const SkPaint* paint = get_obj<SkPaint>(L, 3);
430 get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint);
reed@google.com74ce6f02013-05-22 15:13:18 +0000431 return 0;
432}
433
434static int lcanvas_drawOval(lua_State* L) {
435 SkRect rect;
436 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
437 *get_obj<SkPaint>(L, 3));
438 return 0;
439}
440
441static int lcanvas_drawCircle(lua_State* L) {
442 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
443 lua2scalar(L, 3),
444 lua2scalar(L, 4),
445 *get_obj<SkPaint>(L, 5));
446 return 0;
447}
448
reed485557f2014-10-12 10:36:47 -0700449static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) {
450 if (lua_isnumber(L, index)) {
451 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255));
452 return paint;
reedf355df52014-10-12 12:18:40 -0700453 } else if (lua_isuserdata(L, index)) {
reed485557f2014-10-12 10:36:47 -0700454 const SkPaint* ptr = get_obj<SkPaint>(L, index);
455 if (ptr) {
456 *paint = *ptr;
457 return paint;
458 }
459 }
halcanary96fcdcc2015-08-27 07:41:13 -0700460 return nullptr;
reed485557f2014-10-12 10:36:47 -0700461}
462
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000463static int lcanvas_drawImage(lua_State* L) {
464 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
465 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700466 if (nullptr == image) {
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000467 return 0;
468 }
469 SkScalar x = lua2scalar(L, 3);
470 SkScalar y = lua2scalar(L, 4);
471
472 SkPaint paint;
reed485557f2014-10-12 10:36:47 -0700473 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000474 return 0;
475}
476
reedba5fb932014-10-10 15:28:19 -0700477static int lcanvas_drawImageRect(lua_State* L) {
478 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
479 SkImage* image = get_ref<SkImage>(L, 2);
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == image) {
reedba5fb932014-10-10 15:28:19 -0700481 return 0;
482 }
483
484 SkRect srcR, dstR;
halcanary96fcdcc2015-08-27 07:41:13 -0700485 SkRect* srcRPtr = nullptr;
reedba5fb932014-10-10 15:28:19 -0700486 if (!lua_isnil(L, 3)) {
487 srcRPtr = lua2rect(L, 3, &srcR);
488 }
489 lua2rect(L, 4, &dstR);
mtklein8aacf202014-12-18 13:29:54 -0800490
reedba5fb932014-10-10 15:28:19 -0700491 SkPaint paint;
reede47829b2015-08-06 10:02:53 -0700492 canvas->legacy_drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint));
reedba5fb932014-10-10 15:28:19 -0700493 return 0;
494}
495
reed7a72c672014-11-07 10:23:55 -0800496static int lcanvas_drawPatch(lua_State* L) {
497 SkPoint cubics[12];
498 SkColor colorStorage[4];
499 SkPoint texStorage[4];
500
halcanary96fcdcc2015-08-27 07:41:13 -0700501 const SkColor* colors = nullptr;
502 const SkPoint* texs = nullptr;
reed7a72c672014-11-07 10:23:55 -0800503
504 getarray_points(L, 2, cubics, 12);
505
506 colorStorage[0] = SK_ColorRED;
507 colorStorage[1] = SK_ColorGREEN;
508 colorStorage[2] = SK_ColorBLUE;
509 colorStorage[3] = SK_ColorGRAY;
510
511 if (lua_isnil(L, 4)) {
512 colors = colorStorage;
513 } else {
514 getarray_points(L, 4, texStorage, 4);
515 texs = texStorage;
516 }
517
Mike Reed7d954ad2016-10-28 15:42:34 -0400518 get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, *get_obj<SkPaint>(L, 5));
reed7a72c672014-11-07 10:23:55 -0800519 return 0;
520}
521
reed@google.comfd345872013-05-22 20:53:42 +0000522static int lcanvas_drawPath(lua_State* L) {
523 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
524 *get_obj<SkPaint>(L, 3));
525 return 0;
526}
527
reed96affcd2014-10-13 12:38:04 -0700528// drawPicture(pic, x, y, paint)
529static int lcanvas_drawPicture(lua_State* L) {
530 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
531 SkPicture* picture = get_ref<SkPicture>(L, 2);
532 SkScalar x = lua2scalar_def(L, 3, 0);
533 SkScalar y = lua2scalar_def(L, 4, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700534 SkMatrix matrix, *matrixPtr = nullptr;
reed96affcd2014-10-13 12:38:04 -0700535 if (x || y) {
536 matrix.setTranslate(x, y);
537 matrixPtr = &matrix;
538 }
539 SkPaint paint;
540 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint));
541 return 0;
542}
543
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000544static int lcanvas_drawText(lua_State* L) {
545 if (lua_gettop(L) < 5) {
546 return 0;
547 }
548
549 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
550 size_t len;
551 const char* text = lua_tolstring(L, 2, &len);
552 get_ref<SkCanvas>(L, 1)->drawText(text, len,
553 lua2scalar(L, 3), lua2scalar(L, 4),
554 *get_obj<SkPaint>(L, 5));
555 }
556 return 0;
557}
558
reed1b6ab442014-11-03 19:55:41 -0800559static int lcanvas_drawTextBlob(lua_State* L) {
560 const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2);
561 SkScalar x = lua2scalar(L, 3);
562 SkScalar y = lua2scalar(L, 4);
563 const SkPaint& paint = *get_obj<SkPaint>(L, 5);
564 get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint);
565 return 0;
566}
567
reed@google.com74ce6f02013-05-22 15:13:18 +0000568static int lcanvas_getSaveCount(lua_State* L) {
569 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
570 return 1;
571}
572
573static int lcanvas_getTotalMatrix(lua_State* L) {
574 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
575 return 1;
576}
577
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000578static int lcanvas_save(lua_State* L) {
579 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
580 return 1;
581}
582
reed86217d82014-10-25 20:44:40 -0700583static int lcanvas_saveLayer(lua_State* L) {
584 SkPaint paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700585 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(nullptr, lua2OptionalPaint(L, 2, &paint)));
reed86217d82014-10-25 20:44:40 -0700586 return 1;
587}
588
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000589static int lcanvas_restore(lua_State* L) {
590 get_ref<SkCanvas>(L, 1)->restore();
591 return 0;
592}
593
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000594static int lcanvas_scale(lua_State* L) {
595 SkScalar sx = lua2scalar_def(L, 2, 1);
596 SkScalar sy = lua2scalar_def(L, 3, sx);
597 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
598 return 0;
599}
600
reed@google.com3597b732013-05-22 20:12:50 +0000601static int lcanvas_translate(lua_State* L) {
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000602 SkScalar tx = lua2scalar_def(L, 2, 0);
603 SkScalar ty = lua2scalar_def(L, 3, 0);
604 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
605 return 0;
606}
607
608static int lcanvas_rotate(lua_State* L) {
609 SkScalar degrees = lua2scalar_def(L, 2, 0);
610 get_ref<SkCanvas>(L, 1)->rotate(degrees);
reed@google.com3597b732013-05-22 20:12:50 +0000611 return 0;
612}
613
reedbdc49ae2014-10-14 09:34:52 -0700614static int lcanvas_concat(lua_State* L) {
615 get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2));
616 return 0;
617}
618
reed485557f2014-10-12 10:36:47 -0700619static int lcanvas_newSurface(lua_State* L) {
620 int width = lua2int_def(L, 2, 0);
reed7a72c672014-11-07 10:23:55 -0800621 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -0700622 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -0700623 auto surface = get_ref<SkCanvas>(L, 1)->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700624 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -0700625 lua_pushnil(L);
626 } else {
reede8f30622016-03-23 18:59:25 -0700627 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -0700628 }
629 return 1;
630}
631
reed@google.com74ce6f02013-05-22 15:13:18 +0000632static int lcanvas_gc(lua_State* L) {
Mike Reed5df49342016-11-12 08:06:55 -0600633 // don't know how to track a ptr...
reed@google.com74ce6f02013-05-22 15:13:18 +0000634 return 0;
635}
636
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000637const struct luaL_Reg gSkCanvas_Methods[] = {
reedf355df52014-10-12 12:18:40 -0700638 { "clear", lcanvas_clear },
reed@google.com74ce6f02013-05-22 15:13:18 +0000639 { "drawColor", lcanvas_drawColor },
reed9fbc3f32014-10-21 07:12:58 -0700640 { "drawPaint", lcanvas_drawPaint },
reed@google.com74ce6f02013-05-22 15:13:18 +0000641 { "drawRect", lcanvas_drawRect },
642 { "drawOval", lcanvas_drawOval },
643 { "drawCircle", lcanvas_drawCircle },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +0000644 { "drawImage", lcanvas_drawImage },
reedba5fb932014-10-10 15:28:19 -0700645 { "drawImageRect", lcanvas_drawImageRect },
reed7a72c672014-11-07 10:23:55 -0800646 { "drawPatch", lcanvas_drawPatch },
reed@google.comfd345872013-05-22 20:53:42 +0000647 { "drawPath", lcanvas_drawPath },
reed96affcd2014-10-13 12:38:04 -0700648 { "drawPicture", lcanvas_drawPicture },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000649 { "drawText", lcanvas_drawText },
reed1b6ab442014-11-03 19:55:41 -0800650 { "drawTextBlob", lcanvas_drawTextBlob },
reed@google.com74ce6f02013-05-22 15:13:18 +0000651 { "getSaveCount", lcanvas_getSaveCount },
652 { "getTotalMatrix", lcanvas_getTotalMatrix },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000653 { "save", lcanvas_save },
reed86217d82014-10-25 20:44:40 -0700654 { "saveLayer", lcanvas_saveLayer },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000655 { "restore", lcanvas_restore },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000656 { "scale", lcanvas_scale },
reed@google.com3597b732013-05-22 20:12:50 +0000657 { "translate", lcanvas_translate },
mike@reedtribe.org1d32cc62013-06-13 01:28:56 +0000658 { "rotate", lcanvas_rotate },
reedbdc49ae2014-10-14 09:34:52 -0700659 { "concat", lcanvas_concat },
reed485557f2014-10-12 10:36:47 -0700660
661 { "newSurface", lcanvas_newSurface },
662
reed@google.com74ce6f02013-05-22 15:13:18 +0000663 { "__gc", lcanvas_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700664 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +0000665};
666
667///////////////////////////////////////////////////////////////////////////////
668
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000669static int ldocument_beginPage(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -0700670 const SkRect* contentPtr = nullptr;
Mike Reed5df49342016-11-12 08:06:55 -0600671 push_ptr(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000672 lua2scalar(L, 3),
673 contentPtr));
674 return 1;
675}
676
677static int ldocument_endPage(lua_State* L) {
678 get_ref<SkDocument>(L, 1)->endPage();
679 return 0;
680}
681
682static int ldocument_close(lua_State* L) {
683 get_ref<SkDocument>(L, 1)->close();
684 return 0;
685}
686
687static int ldocument_gc(lua_State* L) {
688 get_ref<SkDocument>(L, 1)->unref();
689 return 0;
690}
691
692static const struct luaL_Reg gSkDocument_Methods[] = {
693 { "beginPage", ldocument_beginPage },
694 { "endPage", ldocument_endPage },
695 { "close", ldocument_close },
696 { "__gc", ldocument_gc },
halcanary96fcdcc2015-08-27 07:41:13 -0700697 { nullptr, nullptr }
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000698};
699
700///////////////////////////////////////////////////////////////////////////////
701
reed@google.com74ce6f02013-05-22 15:13:18 +0000702static int lpaint_isAntiAlias(lua_State* L) {
703 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
704 return 1;
705}
706
707static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000708 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000709 return 0;
710}
711
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000712static int lpaint_isDither(lua_State* L) {
713 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
714 return 1;
715}
716
reedbb8a0ab2014-11-03 22:32:07 -0800717static int lpaint_setDither(lua_State* L) {
718 get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2));
719 return 0;
720}
721
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000722static int lpaint_isFakeBoldText(lua_State* L) {
723 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
724 return 1;
725}
726
727static int lpaint_isLinearText(lua_State* L) {
728 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
729 return 1;
730}
731
732static int lpaint_isSubpixelText(lua_State* L) {
733 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
734 return 1;
735}
736
reed09a1d672014-10-11 13:13:11 -0700737static int lpaint_setSubpixelText(lua_State* L) {
738 get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2));
739 return 1;
740}
741
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000742static int lpaint_isDevKernText(lua_State* L) {
743 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
744 return 1;
745}
746
747static int lpaint_isLCDRenderText(lua_State* L) {
748 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
749 return 1;
750}
751
reed36c9c112014-11-04 10:58:42 -0800752static int lpaint_setLCDRenderText(lua_State* L) {
753 get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2));
754 return 1;
755}
756
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000757static int lpaint_isEmbeddedBitmapText(lua_State* L) {
758 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
759 return 1;
760}
761
762static int lpaint_isAutohinted(lua_State* L) {
763 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
764 return 1;
765}
766
767static int lpaint_isVerticalText(lua_State* L) {
768 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
769 return 1;
770}
771
reed468b1812014-10-19 11:42:54 -0700772static int lpaint_getAlpha(lua_State* L) {
773 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
774 return 1;
775}
776
777static int lpaint_setAlpha(lua_State* L) {
778 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
779 return 0;
780}
781
reed@google.com74ce6f02013-05-22 15:13:18 +0000782static int lpaint_getColor(lua_State* L) {
783 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
784 return 1;
785}
786
787static int lpaint_setColor(lua_State* L) {
788 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
789 return 0;
790}
791
reed@google.come3823fd2013-05-30 18:55:14 +0000792static int lpaint_getTextSize(lua_State* L) {
793 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
794 return 1;
795}
796
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000797static int lpaint_getTextScaleX(lua_State* L) {
798 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
799 return 1;
800}
801
802static int lpaint_getTextSkewX(lua_State* L) {
803 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
804 return 1;
805}
806
reed@google.come3823fd2013-05-30 18:55:14 +0000807static int lpaint_setTextSize(lua_State* L) {
808 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
809 return 0;
810}
811
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000812static int lpaint_getTypeface(lua_State* L) {
813 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
814 return 1;
815}
816
817static int lpaint_setTypeface(lua_State* L) {
bungeman13b9c952016-05-12 10:09:30 -0700818 get_obj<SkPaint>(L, 1)->setTypeface(sk_ref_sp(get_ref<SkTypeface>(L, 2)));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +0000819 return 0;
820}
821
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000822static int lpaint_getHinting(lua_State* L) {
823 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
824 return 1;
825}
826
reed93a12152015-03-16 10:08:34 -0700827static int lpaint_getFilterQuality(lua_State* L) {
828 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality());
reed7a72c672014-11-07 10:23:55 -0800829 return 1;
830}
831
reed93a12152015-03-16 10:08:34 -0700832static int lpaint_setFilterQuality(lua_State* L) {
reed7a72c672014-11-07 10:23:55 -0800833 int level = lua2int_def(L, 2, -1);
834 if (level >= 0 && level <= 3) {
reed93a12152015-03-16 10:08:34 -0700835 get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level);
reed7a72c672014-11-07 10:23:55 -0800836 }
837 return 0;
838}
839
reed@google.come3823fd2013-05-30 18:55:14 +0000840static int lpaint_getFontID(lua_State* L) {
841 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
842 SkLua(L).pushU32(SkTypeface::UniqueID(face));
843 return 1;
844}
845
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000846static const struct {
847 const char* fLabel;
848 SkPaint::Align fAlign;
849} gAlignRec[] = {
850 { "left", SkPaint::kLeft_Align },
851 { "center", SkPaint::kCenter_Align },
852 { "right", SkPaint::kRight_Align },
853};
854
855static int lpaint_getTextAlign(lua_State* L) {
856 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
857 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
858 if (gAlignRec[i].fAlign == align) {
859 lua_pushstring(L, gAlignRec[i].fLabel);
860 return 1;
861 }
862 }
863 return 0;
864}
865
866static int lpaint_setTextAlign(lua_State* L) {
867 if (lua_isstring(L, 2)) {
868 size_t len;
869 const char* label = lua_tolstring(L, 2, &len);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000870
mike@reedtribe.orgfb858242013-06-08 16:39:44 +0000871 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
872 if (!strcmp(gAlignRec[i].fLabel, label)) {
873 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
874 break;
875 }
876 }
877 }
878 return 0;
879}
880
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000881static int lpaint_getStroke(lua_State* L) {
882 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
883 return 1;
884}
885
886static int lpaint_setStroke(lua_State* L) {
887 SkPaint::Style style;
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000888
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000889 if (lua_toboolean(L, 2)) {
890 style = SkPaint::kStroke_Style;
891 } else {
892 style = SkPaint::kFill_Style;
893 }
894 get_obj<SkPaint>(L, 1)->setStyle(style);
895 return 0;
896}
897
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000898static int lpaint_getStrokeCap(lua_State* L) {
899 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
900 return 1;
901}
902
903static int lpaint_getStrokeJoin(lua_State* L) {
904 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
905 return 1;
906}
907
908static int lpaint_getTextEncoding(lua_State* L) {
commit-bot@chromium.org641bcc32013-12-19 10:39:59 +0000909 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000910 return 1;
911}
912
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000913static int lpaint_getStrokeWidth(lua_State* L) {
914 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
915 return 1;
916}
917
918static int lpaint_setStrokeWidth(lua_State* L) {
919 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
920 return 0;
921}
922
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +0000923static int lpaint_getStrokeMiter(lua_State* L) {
924 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
925 return 1;
926}
927
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000928static int lpaint_measureText(lua_State* L) {
929 if (lua_isstring(L, 2)) {
930 size_t len;
931 const char* text = lua_tolstring(L, 2, &len);
932 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
933 return 1;
934 }
935 return 0;
936}
937
938struct FontMetrics {
939 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
940 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
941 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
942 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
943 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
944 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
945 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
946 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
947 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
948};
949
950static int lpaint_getFontMetrics(lua_State* L) {
951 SkPaint::FontMetrics fm;
952 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
skia.committer@gmail.com370c5342013-06-09 07:01:05 +0000953
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +0000954 lua_newtable(L);
955 setfield_scalar(L, "top", fm.fTop);
956 setfield_scalar(L, "ascent", fm.fAscent);
957 setfield_scalar(L, "descent", fm.fDescent);
958 setfield_scalar(L, "bottom", fm.fBottom);
959 setfield_scalar(L, "leading", fm.fLeading);
960 SkLua(L).pushScalar(height);
961 return 2;
962}
963
reed@google.com29563872013-07-10 21:23:49 +0000964static int lpaint_getEffects(lua_State* L) {
965 const SkPaint* paint = get_obj<SkPaint>(L, 1);
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000966
reed@google.com29563872013-07-10 21:23:49 +0000967 lua_newtable(L);
reed468b1812014-10-19 11:42:54 -0700968 setfield_bool_if(L, "looper", !!paint->getLooper());
969 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
970 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
971 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
972 setfield_bool_if(L, "shader", !!paint->getShader());
reed@google.com29563872013-07-10 21:23:49 +0000973 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
974 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
reed@google.com29563872013-07-10 21:23:49 +0000975 return 1;
976}
977
reed22a517f2015-12-04 20:45:59 -0800978static int lpaint_getColorFilter(lua_State* L) {
979 const SkPaint* paint = get_obj<SkPaint>(L, 1);
980 SkColorFilter* cf = paint->getColorFilter();
981 if (cf) {
982 push_ref(L, cf);
983 return 1;
984 }
985 return 0;
986}
987
988static int lpaint_setColorFilter(lua_State* L) {
989 SkPaint* paint = get_obj<SkPaint>(L, 1);
reedd053ce92016-03-22 10:17:23 -0700990 paint->setColorFilter(sk_ref_sp(get_ref<SkColorFilter>(L, 2)));
reed22a517f2015-12-04 20:45:59 -0800991 return 0;
992}
993
reed468b1812014-10-19 11:42:54 -0700994static int lpaint_getImageFilter(lua_State* L) {
995 const SkPaint* paint = get_obj<SkPaint>(L, 1);
996 SkImageFilter* imf = paint->getImageFilter();
997 if (imf) {
998 push_ref(L, imf);
999 return 1;
1000 }
1001 return 0;
1002}
1003
1004static int lpaint_setImageFilter(lua_State* L) {
1005 SkPaint* paint = get_obj<SkPaint>(L, 1);
Mike Reed5e257172016-11-01 11:22:05 -04001006 paint->setImageFilter(sk_ref_sp(get_ref<SkImageFilter>(L, 2)));
reed468b1812014-10-19 11:42:54 -07001007 return 0;
1008}
1009
reed@google.com5fdc9832013-07-24 15:47:52 +00001010static int lpaint_getShader(lua_State* L) {
1011 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1012 SkShader* shader = paint->getShader();
1013 if (shader) {
1014 push_ref(L, shader);
1015 return 1;
1016 }
1017 return 0;
1018}
1019
reed9fbc3f32014-10-21 07:12:58 -07001020static int lpaint_setShader(lua_State* L) {
1021 SkPaint* paint = get_obj<SkPaint>(L, 1);
reedfe630452016-03-25 09:08:00 -07001022 paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2)));
reed9fbc3f32014-10-21 07:12:58 -07001023 return 0;
1024}
1025
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001026static int lpaint_getPathEffect(lua_State* L) {
1027 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1028 SkPathEffect* pe = paint->getPathEffect();
1029 if (pe) {
1030 push_ref(L, pe);
1031 return 1;
1032 }
1033 return 0;
1034}
1035
hstern0b401ce2016-08-02 09:17:59 -07001036static int lpaint_getFillPath(lua_State* L) {
1037 const SkPaint* paint = get_obj<SkPaint>(L, 1);
1038 const SkPath* path = get_obj<SkPath>(L, 2);
1039
1040 SkPath fillpath;
1041 paint->getFillPath(*path, &fillpath);
1042
1043 SkLua lua(L);
1044 lua.pushPath(fillpath);
1045
1046 return 1;
1047}
1048
reed@google.com74ce6f02013-05-22 15:13:18 +00001049static int lpaint_gc(lua_State* L) {
1050 get_obj<SkPaint>(L, 1)->~SkPaint();
1051 return 0;
1052}
1053
1054static const struct luaL_Reg gSkPaint_Methods[] = {
1055 { "isAntiAlias", lpaint_isAntiAlias },
1056 { "setAntiAlias", lpaint_setAntiAlias },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001057 { "isDither", lpaint_isDither },
reedbb8a0ab2014-11-03 22:32:07 -08001058 { "setDither", lpaint_setDither },
reed93a12152015-03-16 10:08:34 -07001059 { "getFilterQuality", lpaint_getFilterQuality },
1060 { "setFilterQuality", lpaint_setFilterQuality },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001061 { "isFakeBoldText", lpaint_isFakeBoldText },
1062 { "isLinearText", lpaint_isLinearText },
1063 { "isSubpixelText", lpaint_isSubpixelText },
reed09a1d672014-10-11 13:13:11 -07001064 { "setSubpixelText", lpaint_setSubpixelText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001065 { "isDevKernText", lpaint_isDevKernText },
1066 { "isLCDRenderText", lpaint_isLCDRenderText },
reed36c9c112014-11-04 10:58:42 -08001067 { "setLCDRenderText", lpaint_setLCDRenderText },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001068 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1069 { "isAutohinted", lpaint_isAutohinted },
1070 { "isVerticalText", lpaint_isVerticalText },
reed468b1812014-10-19 11:42:54 -07001071 { "getAlpha", lpaint_getAlpha },
1072 { "setAlpha", lpaint_setAlpha },
reed@google.com74ce6f02013-05-22 15:13:18 +00001073 { "getColor", lpaint_getColor },
1074 { "setColor", lpaint_setColor },
reed@google.come3823fd2013-05-30 18:55:14 +00001075 { "getTextSize", lpaint_getTextSize },
1076 { "setTextSize", lpaint_setTextSize },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001077 { "getTextScaleX", lpaint_getTextScaleX },
1078 { "getTextSkewX", lpaint_getTextSkewX },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001079 { "getTypeface", lpaint_getTypeface },
1080 { "setTypeface", lpaint_setTypeface },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001081 { "getHinting", lpaint_getHinting },
reed@google.come3823fd2013-05-30 18:55:14 +00001082 { "getFontID", lpaint_getFontID },
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001083 { "getTextAlign", lpaint_getTextAlign },
1084 { "setTextAlign", lpaint_setTextAlign },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001085 { "getStroke", lpaint_getStroke },
1086 { "setStroke", lpaint_setStroke },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001087 { "getStrokeCap", lpaint_getStrokeCap },
1088 { "getStrokeJoin", lpaint_getStrokeJoin },
1089 { "getTextEncoding", lpaint_getTextEncoding },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001090 { "getStrokeWidth", lpaint_getStrokeWidth },
1091 { "setStrokeWidth", lpaint_setStrokeWidth },
commit-bot@chromium.org1cd71fb2013-12-18 18:28:07 +00001092 { "getStrokeMiter", lpaint_getStrokeMiter },
mike@reedtribe.org73d9f1c2013-06-09 01:54:56 +00001093 { "measureText", lpaint_measureText },
1094 { "getFontMetrics", lpaint_getFontMetrics },
reed@google.com29563872013-07-10 21:23:49 +00001095 { "getEffects", lpaint_getEffects },
reed22a517f2015-12-04 20:45:59 -08001096 { "getColorFilter", lpaint_getColorFilter },
1097 { "setColorFilter", lpaint_setColorFilter },
reed468b1812014-10-19 11:42:54 -07001098 { "getImageFilter", lpaint_getImageFilter },
1099 { "setImageFilter", lpaint_setImageFilter },
reed@google.com5fdc9832013-07-24 15:47:52 +00001100 { "getShader", lpaint_getShader },
reed9fbc3f32014-10-21 07:12:58 -07001101 { "setShader", lpaint_setShader },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001102 { "getPathEffect", lpaint_getPathEffect },
hstern0b401ce2016-08-02 09:17:59 -07001103 { "getFillPath", lpaint_getFillPath },
reed@google.com74ce6f02013-05-22 15:13:18 +00001104 { "__gc", lpaint_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001105 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001106};
1107
1108///////////////////////////////////////////////////////////////////////////////
1109
reed@google.com5fdc9832013-07-24 15:47:52 +00001110static const char* mode2string(SkShader::TileMode mode) {
1111 static const char* gNames[] = { "clamp", "repeat", "mirror" };
1112 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
1113 return gNames[mode];
1114}
1115
1116static const char* gradtype2string(SkShader::GradientType t) {
1117 static const char* gNames[] = {
1118 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
1119 };
1120 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
1121 return gNames[t];
1122}
1123
1124static int lshader_isOpaque(lua_State* L) {
1125 SkShader* shader = get_ref<SkShader>(L, 1);
1126 return shader && shader->isOpaque();
1127}
1128
Mike Reed627778d2016-09-28 17:13:38 -04001129static int lshader_isAImage(lua_State* L) {
reed@google.com5fdc9832013-07-24 15:47:52 +00001130 SkShader* shader = get_ref<SkShader>(L, 1);
1131 if (shader) {
reed@google.com5fdc9832013-07-24 15:47:52 +00001132 SkMatrix matrix;
1133 SkShader::TileMode modes[2];
Mike Reed627778d2016-09-28 17:13:38 -04001134 if (SkImage* image = shader->isAImage(&matrix, modes)) {
reedf5822822015-08-19 11:46:38 -07001135 lua_newtable(L);
Mike Reed627778d2016-09-28 17:13:38 -04001136 setfield_number(L, "id", image->uniqueID());
1137 setfield_number(L, "width", image->width());
1138 setfield_number(L, "height", image->height());
reedf5822822015-08-19 11:46:38 -07001139 setfield_string(L, "tileX", mode2string(modes[0]));
1140 setfield_string(L, "tileY", mode2string(modes[1]));
1141 return 1;
reed@google.com5fdc9832013-07-24 15:47:52 +00001142 }
1143 }
1144 return 0;
1145}
1146
1147static int lshader_asAGradient(lua_State* L) {
1148 SkShader* shader = get_ref<SkShader>(L, 1);
1149 if (shader) {
1150 SkShader::GradientInfo info;
1151 sk_bzero(&info, sizeof(info));
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001152
reed@google.com5fdc9832013-07-24 15:47:52 +00001153 SkShader::GradientType t = shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001154
reed@google.com5fdc9832013-07-24 15:47:52 +00001155 if (SkShader::kNone_GradientType != t) {
fmenozzib4f254e2016-06-28 14:03:03 -07001156 SkAutoTArray<SkScalar> pos(info.fColorCount);
1157 info.fColorOffsets = pos.get();
1158 shader->asAGradient(&info);
commit-bot@chromium.org74f96b92013-08-01 17:32:56 +00001159
fmenozzib4f254e2016-06-28 14:03:03 -07001160 lua_newtable(L);
fmenozzi7f2c85e2016-07-12 09:17:39 -07001161 setfield_string(L, "type", gradtype2string(t));
1162 setfield_string(L, "tile", mode2string(info.fTileMode));
1163 setfield_number(L, "colorCount", info.fColorCount);
fmenozzib4f254e2016-06-28 14:03:03 -07001164
1165 lua_newtable(L);
1166 for (int i = 0; i < info.fColorCount; i++) {
1167 // Lua uses 1-based indexing
1168 setarray_scalar(L, i+1, pos[i]);
1169 }
1170 lua_setfield(L, -2, "positions");
1171
reed@google.com5fdc9832013-07-24 15:47:52 +00001172 return 1;
1173 }
1174 }
1175 return 0;
1176}
1177
1178static int lshader_gc(lua_State* L) {
1179 get_ref<SkShader>(L, 1)->unref();
1180 return 0;
1181}
1182
1183static const struct luaL_Reg gSkShader_Methods[] = {
1184 { "isOpaque", lshader_isOpaque },
Mike Reed627778d2016-09-28 17:13:38 -04001185 { "isAImage", lshader_isAImage },
reed@google.com5fdc9832013-07-24 15:47:52 +00001186 { "asAGradient", lshader_asAGradient },
1187 { "__gc", lshader_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001188 { nullptr, nullptr }
reed@google.com5fdc9832013-07-24 15:47:52 +00001189};
1190
1191///////////////////////////////////////////////////////////////////////////////
1192
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001193static int lpatheffect_asADash(lua_State* L) {
1194 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1195 if (pe) {
1196 SkPathEffect::DashInfo info;
1197 SkPathEffect::DashType dashType = pe->asADash(&info);
1198 if (SkPathEffect::kDash_DashType == dashType) {
1199 SkAutoTArray<SkScalar> intervals(info.fCount);
1200 info.fIntervals = intervals.get();
1201 pe->asADash(&info);
1202 SkLua(L).pushDash(info);
1203 return 1;
1204 }
1205 }
1206 return 0;
1207}
1208
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001209static int lpatheffect_gc(lua_State* L) {
1210 get_ref<SkPathEffect>(L, 1)->unref();
1211 return 0;
1212}
1213
1214static const struct luaL_Reg gSkPathEffect_Methods[] = {
commit-bot@chromium.org4d803a92014-05-14 16:03:14 +00001215 { "asADash", lpatheffect_asADash },
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001216 { "__gc", lpatheffect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001217 { nullptr, nullptr }
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00001218};
1219
1220///////////////////////////////////////////////////////////////////////////////
1221
reed22a517f2015-12-04 20:45:59 -08001222static int lpcolorfilter_gc(lua_State* L) {
1223 get_ref<SkColorFilter>(L, 1)->unref();
1224 return 0;
1225}
1226
1227static const struct luaL_Reg gSkColorFilter_Methods[] = {
1228 { "__gc", lpcolorfilter_gc },
1229 { nullptr, nullptr }
1230};
1231
1232///////////////////////////////////////////////////////////////////////////////
1233
reed468b1812014-10-19 11:42:54 -07001234static int lpimagefilter_gc(lua_State* L) {
1235 get_ref<SkImageFilter>(L, 1)->unref();
1236 return 0;
1237}
1238
1239static const struct luaL_Reg gSkImageFilter_Methods[] = {
1240 { "__gc", lpimagefilter_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001241 { nullptr, nullptr }
reed468b1812014-10-19 11:42:54 -07001242};
1243
1244///////////////////////////////////////////////////////////////////////////////
1245
humper@google.com2815c192013-07-10 22:42:30 +00001246static int lmatrix_getType(lua_State* L) {
1247 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +00001248
humper@google.com2815c192013-07-10 22:42:30 +00001249 lua_newtable(L);
1250 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1251 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1252 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1253 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1254 return 1;
1255}
1256
humper@google.com0f48ee02013-07-26 15:23:43 +00001257static int lmatrix_getScaleX(lua_State* L) {
1258 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1259 return 1;
1260}
1261
1262static int lmatrix_getScaleY(lua_State* L) {
1263 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1264 return 1;
1265}
1266
1267static int lmatrix_getTranslateX(lua_State* L) {
1268 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1269 return 1;
1270}
1271
1272static int lmatrix_getTranslateY(lua_State* L) {
1273 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1274 return 1;
1275}
1276
reed7a72c672014-11-07 10:23:55 -08001277static int lmatrix_invert(lua_State* L) {
1278 lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2)));
1279 return 1;
1280}
1281
1282static int lmatrix_mapXY(lua_State* L) {
1283 SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) };
1284 get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1);
1285 lua_pushnumber(L, pt.x());
1286 lua_pushnumber(L, pt.y());
1287 return 2;
1288}
1289
reedbdc49ae2014-10-14 09:34:52 -07001290static int lmatrix_setRectToRect(lua_State* L) {
1291 SkMatrix* matrix = get_obj<SkMatrix>(L, 1);
1292 SkRect srcR, dstR;
1293 lua2rect(L, 2, &srcR);
1294 lua2rect(L, 3, &dstR);
1295 const char* scaleToFitStr = lua_tostring(L, 4);
1296 SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit;
1297
1298 if (scaleToFitStr) {
1299 const struct {
1300 const char* fName;
1301 SkMatrix::ScaleToFit fScaleToFit;
1302 } rec[] = {
1303 { "fill", SkMatrix::kFill_ScaleToFit },
1304 { "start", SkMatrix::kStart_ScaleToFit },
1305 { "center", SkMatrix::kCenter_ScaleToFit },
1306 { "end", SkMatrix::kEnd_ScaleToFit },
1307 };
1308
1309 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
1310 if (strcmp(rec[i].fName, scaleToFitStr) == 0) {
1311 scaleToFit = rec[i].fScaleToFit;
1312 break;
1313 }
1314 }
1315 }
1316
1317 matrix->setRectToRect(srcR, dstR, scaleToFit);
1318 return 0;
1319}
1320
humper@google.com2815c192013-07-10 22:42:30 +00001321static const struct luaL_Reg gSkMatrix_Methods[] = {
1322 { "getType", lmatrix_getType },
humper@google.com0f48ee02013-07-26 15:23:43 +00001323 { "getScaleX", lmatrix_getScaleX },
1324 { "getScaleY", lmatrix_getScaleY },
1325 { "getTranslateX", lmatrix_getTranslateX },
1326 { "getTranslateY", lmatrix_getTranslateY },
reedbdc49ae2014-10-14 09:34:52 -07001327 { "setRectToRect", lmatrix_setRectToRect },
reed7a72c672014-11-07 10:23:55 -08001328 { "invert", lmatrix_invert },
1329 { "mapXY", lmatrix_mapXY },
halcanary96fcdcc2015-08-27 07:41:13 -07001330 { nullptr, nullptr }
humper@google.com2815c192013-07-10 22:42:30 +00001331};
1332
1333///////////////////////////////////////////////////////////////////////////////
1334
reed@google.com74ce6f02013-05-22 15:13:18 +00001335static int lpath_getBounds(lua_State* L) {
1336 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1337 return 1;
1338}
1339
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001340static const char* fill_type_to_str(SkPath::FillType fill) {
1341 switch (fill) {
1342 case SkPath::kEvenOdd_FillType:
1343 return "even-odd";
1344 case SkPath::kWinding_FillType:
1345 return "winding";
1346 case SkPath::kInverseEvenOdd_FillType:
1347 return "inverse-even-odd";
1348 case SkPath::kInverseWinding_FillType:
1349 return "inverse-winding";
1350 }
1351 return "unknown";
1352}
1353
1354static int lpath_getFillType(lua_State* L) {
1355 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1356 SkLua(L).pushString(fill_type_to_str(fill));
1357 return 1;
1358}
1359
1360static SkString segment_masks_to_str(uint32_t segmentMasks) {
1361 SkString result;
1362 bool first = true;
1363 if (SkPath::kLine_SegmentMask & segmentMasks) {
1364 result.append("line");
1365 first = false;
1366 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1367 }
1368 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1369 if (!first) {
1370 result.append(" ");
1371 }
1372 result.append("quad");
1373 first = false;
1374 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1375 }
1376 if (SkPath::kConic_SegmentMask & segmentMasks) {
1377 if (!first) {
1378 result.append(" ");
1379 }
1380 result.append("conic");
1381 first = false;
1382 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1383 }
1384 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1385 if (!first) {
1386 result.append(" ");
1387 }
1388 result.append("cubic");
1389 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1390 }
1391 SkASSERT(0 == segmentMasks);
1392 return result;
1393}
1394
krajcevski95498ed2014-08-18 08:02:33 -07001395static int lpath_getSegmentTypes(lua_State* L) {
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001396 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1397 SkLua(L).pushString(segment_masks_to_str(segMasks));
1398 return 1;
1399}
1400
1401static int lpath_isConvex(lua_State* L) {
1402 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1403 SkLua(L).pushBool(isConvex);
1404 return 1;
1405}
1406
reed@google.com74ce6f02013-05-22 15:13:18 +00001407static int lpath_isEmpty(lua_State* L) {
1408 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1409 return 1;
1410}
1411
1412static int lpath_isRect(lua_State* L) {
1413 SkRect r;
1414 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1415 int ret_count = 1;
1416 lua_pushboolean(L, pred);
1417 if (pred) {
1418 SkLua(L).pushRect(r);
1419 ret_count += 1;
1420 }
1421 return ret_count;
1422}
1423
1424static const char* dir2string(SkPath::Direction dir) {
1425 static const char* gStr[] = {
1426 "unknown", "cw", "ccw"
1427 };
1428 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1429 return gStr[dir];
1430}
1431
caryclark95bc5f32015-04-08 08:34:15 -07001432static int lpath_isNestedFillRects(lua_State* L) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001433 SkRect rects[2];
1434 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001435 bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs);
reed@google.com74ce6f02013-05-22 15:13:18 +00001436 int ret_count = 1;
1437 lua_pushboolean(L, pred);
1438 if (pred) {
1439 SkLua lua(L);
1440 lua.pushRect(rects[0]);
1441 lua.pushRect(rects[1]);
1442 lua_pushstring(L, dir2string(dirs[0]));
1443 lua_pushstring(L, dir2string(dirs[0]));
1444 ret_count += 4;
1445 }
1446 return ret_count;
1447}
1448
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001449static int lpath_countPoints(lua_State* L) {
1450 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1451 return 1;
1452}
1453
hstern0b401ce2016-08-02 09:17:59 -07001454static int lpath_getVerbs(lua_State* L) {
1455 const SkPath* path = get_obj<SkPath>(L, 1);
1456 SkPath::Iter iter(*path, false);
1457 SkPoint pts[4];
1458
1459 lua_newtable(L);
1460
1461 bool done = false;
1462 int i = 0;
1463 do {
1464 switch (iter.next(pts, true)) {
1465 case SkPath::kMove_Verb:
1466 setarray_string(L, ++i, "move");
1467 break;
1468 case SkPath::kClose_Verb:
1469 setarray_string(L, ++i, "close");
1470 break;
1471 case SkPath::kLine_Verb:
1472 setarray_string(L, ++i, "line");
1473 break;
1474 case SkPath::kQuad_Verb:
1475 setarray_string(L, ++i, "quad");
1476 break;
1477 case SkPath::kConic_Verb:
1478 setarray_string(L, ++i, "conic");
1479 break;
1480 case SkPath::kCubic_Verb:
1481 setarray_string(L, ++i, "cubic");
1482 break;
1483 case SkPath::kDone_Verb:
1484 setarray_string(L, ++i, "done");
1485 done = true;
1486 break;
1487 }
1488 } while (!done);
1489
1490 return 1;
1491}
1492
reed@google.com74ce6f02013-05-22 15:13:18 +00001493static int lpath_reset(lua_State* L) {
1494 get_obj<SkPath>(L, 1)->reset();
1495 return 0;
1496}
1497
1498static int lpath_moveTo(lua_State* L) {
1499 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1500 return 0;
1501}
1502
1503static int lpath_lineTo(lua_State* L) {
1504 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1505 return 0;
1506}
1507
1508static int lpath_quadTo(lua_State* L) {
1509 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1510 lua2scalar(L, 4), lua2scalar(L, 5));
1511 return 0;
1512}
1513
1514static int lpath_cubicTo(lua_State* L) {
1515 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1516 lua2scalar(L, 4), lua2scalar(L, 5),
1517 lua2scalar(L, 6), lua2scalar(L, 7));
1518 return 0;
1519}
1520
1521static int lpath_close(lua_State* L) {
1522 get_obj<SkPath>(L, 1)->close();
1523 return 0;
1524}
1525
1526static int lpath_gc(lua_State* L) {
1527 get_obj<SkPath>(L, 1)->~SkPath();
1528 return 0;
1529}
1530
1531static const struct luaL_Reg gSkPath_Methods[] = {
1532 { "getBounds", lpath_getBounds },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001533 { "getFillType", lpath_getFillType },
krajcevski95498ed2014-08-18 08:02:33 -07001534 { "getSegmentTypes", lpath_getSegmentTypes },
hstern0b401ce2016-08-02 09:17:59 -07001535 { "getVerbs", lpath_getVerbs },
commit-bot@chromium.orgd85b8222014-02-24 21:59:29 +00001536 { "isConvex", lpath_isConvex },
reed@google.com74ce6f02013-05-22 15:13:18 +00001537 { "isEmpty", lpath_isEmpty },
1538 { "isRect", lpath_isRect },
caryclark95bc5f32015-04-08 08:34:15 -07001539 { "isNestedFillRects", lpath_isNestedFillRects },
commit-bot@chromium.orgc5302082014-02-26 21:38:47 +00001540 { "countPoints", lpath_countPoints },
reed@google.com74ce6f02013-05-22 15:13:18 +00001541 { "reset", lpath_reset },
1542 { "moveTo", lpath_moveTo },
1543 { "lineTo", lpath_lineTo },
1544 { "quadTo", lpath_quadTo },
1545 { "cubicTo", lpath_cubicTo },
1546 { "close", lpath_close },
1547 { "__gc", lpath_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001548 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001549};
1550
1551///////////////////////////////////////////////////////////////////////////////
1552
1553static const char* rrect_type(const SkRRect& rr) {
1554 switch (rr.getType()) {
reed@google.com74ce6f02013-05-22 15:13:18 +00001555 case SkRRect::kEmpty_Type: return "empty";
1556 case SkRRect::kRect_Type: return "rect";
1557 case SkRRect::kOval_Type: return "oval";
1558 case SkRRect::kSimple_Type: return "simple";
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +00001559 case SkRRect::kNinePatch_Type: return "nine-patch";
reed@google.com74ce6f02013-05-22 15:13:18 +00001560 case SkRRect::kComplex_Type: return "complex";
1561 }
mtklein@google.com330313a2013-08-22 15:37:26 +00001562 SkDEBUGFAIL("never get here");
reed@google.com74ce6f02013-05-22 15:13:18 +00001563 return "";
1564}
1565
1566static int lrrect_rect(lua_State* L) {
1567 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1568 return 1;
1569}
1570
1571static int lrrect_type(lua_State* L) {
1572 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1573 return 1;
1574}
1575
1576static int lrrect_radii(lua_State* L) {
reed@google.com7fa2a652014-01-27 13:42:58 +00001577 int corner = SkToInt(lua_tointeger(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +00001578 SkVector v;
1579 if (corner < 0 || corner > 3) {
1580 SkDebugf("bad corner index %d", corner);
1581 v.set(0, 0);
1582 } else {
1583 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1584 }
1585 lua_pushnumber(L, v.fX);
1586 lua_pushnumber(L, v.fY);
1587 return 2;
1588}
1589
1590static int lrrect_gc(lua_State* L) {
1591 get_obj<SkRRect>(L, 1)->~SkRRect();
1592 return 0;
1593}
1594
1595static const struct luaL_Reg gSkRRect_Methods[] = {
1596 { "rect", lrrect_rect },
1597 { "type", lrrect_type },
1598 { "radii", lrrect_radii },
1599 { "__gc", lrrect_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001600 { nullptr, nullptr }
reed@google.com74ce6f02013-05-22 15:13:18 +00001601};
1602
1603///////////////////////////////////////////////////////////////////////////////
1604
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001605static int limage_width(lua_State* L) {
1606 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1607 return 1;
1608}
1609
1610static int limage_height(lua_State* L) {
1611 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1612 return 1;
1613}
1614
reed7a72c672014-11-07 10:23:55 -08001615static int limage_newShader(lua_State* L) {
1616 SkShader::TileMode tmode = SkShader::kClamp_TileMode;
halcanary96fcdcc2015-08-27 07:41:13 -07001617 const SkMatrix* localM = nullptr;
reed5671c5b2016-03-09 14:47:34 -08001618 push_ref(L, get_ref<SkImage>(L, 1)->makeShader(tmode, tmode, localM));
reed7a72c672014-11-07 10:23:55 -08001619 return 1;
1620}
1621
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001622static int limage_gc(lua_State* L) {
1623 get_ref<SkImage>(L, 1)->unref();
1624 return 0;
1625}
1626
1627static const struct luaL_Reg gSkImage_Methods[] = {
1628 { "width", limage_width },
1629 { "height", limage_height },
reed7a72c672014-11-07 10:23:55 -08001630 { "newShader", limage_newShader },
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001631 { "__gc", limage_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001632 { nullptr, nullptr }
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00001633};
1634
1635///////////////////////////////////////////////////////////////////////////////
1636
reed485557f2014-10-12 10:36:47 -07001637static int lsurface_width(lua_State* L) {
1638 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width());
1639 return 1;
1640}
1641
1642static int lsurface_height(lua_State* L) {
1643 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height());
1644 return 1;
1645}
1646
1647static int lsurface_getCanvas(lua_State* L) {
1648 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001649 if (nullptr == canvas) {
reed485557f2014-10-12 10:36:47 -07001650 lua_pushnil(L);
1651 } else {
Mike Reed5df49342016-11-12 08:06:55 -06001652 push_ptr(L, canvas);
reed485557f2014-10-12 10:36:47 -07001653 // note: we don't unref canvas, since getCanvas did not ref it.
1654 // warning: this is weird: now Lua owns a ref on this canvas, but what if they let
1655 // the real owner (the surface) go away, but still hold onto the canvas?
1656 // *really* we want to sort of ref the surface again, but have the native object
1657 // know that it is supposed to be treated as a canvas...
1658 }
1659 return 1;
1660}
1661
1662static int lsurface_newImageSnapshot(lua_State* L) {
reed9ce9d672016-03-17 10:51:11 -07001663 sk_sp<SkImage> image = get_ref<SkSurface>(L, 1)->makeImageSnapshot();
1664 if (!image) {
reed485557f2014-10-12 10:36:47 -07001665 lua_pushnil(L);
1666 } else {
reed9ce9d672016-03-17 10:51:11 -07001667 push_ref(L, image);
reed485557f2014-10-12 10:36:47 -07001668 }
1669 return 1;
1670}
1671
1672static int lsurface_newSurface(lua_State* L) {
1673 int width = lua2int_def(L, 2, 0);
reed96affcd2014-10-13 12:38:04 -07001674 int height = lua2int_def(L, 3, 0);
reed485557f2014-10-12 10:36:47 -07001675 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -07001676 auto surface = get_ref<SkSurface>(L, 1)->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -07001677 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07001678 lua_pushnil(L);
1679 } else {
reede8f30622016-03-23 18:59:25 -07001680 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -07001681 }
1682 return 1;
1683}
1684
1685static int lsurface_gc(lua_State* L) {
1686 get_ref<SkSurface>(L, 1)->unref();
1687 return 0;
1688}
1689
1690static const struct luaL_Reg gSkSurface_Methods[] = {
1691 { "width", lsurface_width },
1692 { "height", lsurface_height },
1693 { "getCanvas", lsurface_getCanvas },
1694 { "newImageSnapshot", lsurface_newImageSnapshot },
1695 { "newSurface", lsurface_newSurface },
1696 { "__gc", lsurface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001697 { nullptr, nullptr }
reed485557f2014-10-12 10:36:47 -07001698};
1699
1700///////////////////////////////////////////////////////////////////////////////
1701
reed96affcd2014-10-13 12:38:04 -07001702static int lpicturerecorder_beginRecording(lua_State* L) {
1703 const SkScalar w = lua2scalar_def(L, 2, -1);
1704 const SkScalar h = lua2scalar_def(L, 3, -1);
1705 if (w <= 0 || h <= 0) {
1706 lua_pushnil(L);
1707 return 1;
1708 }
1709
1710 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h);
halcanary96fcdcc2015-08-27 07:41:13 -07001711 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001712 lua_pushnil(L);
1713 return 1;
1714 }
1715
Mike Reed5df49342016-11-12 08:06:55 -06001716 push_ptr(L, canvas);
reed96affcd2014-10-13 12:38:04 -07001717 return 1;
1718}
1719
1720static int lpicturerecorder_getCanvas(lua_State* L) {
1721 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas();
halcanary96fcdcc2015-08-27 07:41:13 -07001722 if (nullptr == canvas) {
reed96affcd2014-10-13 12:38:04 -07001723 lua_pushnil(L);
1724 return 1;
1725 }
Mike Reed5df49342016-11-12 08:06:55 -06001726 push_ptr(L, canvas);
reed96affcd2014-10-13 12:38:04 -07001727 return 1;
1728}
1729
1730static int lpicturerecorder_endRecording(lua_State* L) {
reedca2622b2016-03-18 07:25:55 -07001731 sk_sp<SkPicture> pic = get_obj<SkPictureRecorder>(L, 1)->finishRecordingAsPicture();
1732 if (!pic) {
reed96affcd2014-10-13 12:38:04 -07001733 lua_pushnil(L);
1734 return 1;
1735 }
reedca2622b2016-03-18 07:25:55 -07001736 push_ref(L, std::move(pic));
reed96affcd2014-10-13 12:38:04 -07001737 return 1;
1738}
1739
1740static int lpicturerecorder_gc(lua_State* L) {
1741 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder();
1742 return 0;
1743}
1744
1745static const struct luaL_Reg gSkPictureRecorder_Methods[] = {
1746 { "beginRecording", lpicturerecorder_beginRecording },
1747 { "getCanvas", lpicturerecorder_getCanvas },
1748 { "endRecording", lpicturerecorder_endRecording },
1749 { "__gc", lpicturerecorder_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001750 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001751};
1752
1753///////////////////////////////////////////////////////////////////////////////
1754
1755static int lpicture_width(lua_State* L) {
1756 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width());
1757 return 1;
1758}
1759
1760static int lpicture_height(lua_State* L) {
1761 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height());
1762 return 1;
1763}
1764
1765static int lpicture_gc(lua_State* L) {
1766 get_ref<SkPicture>(L, 1)->unref();
1767 return 0;
1768}
1769
1770static const struct luaL_Reg gSkPicture_Methods[] = {
1771 { "width", lpicture_width },
1772 { "height", lpicture_height },
1773 { "__gc", lpicture_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001774 { nullptr, nullptr }
reed96affcd2014-10-13 12:38:04 -07001775};
1776
1777///////////////////////////////////////////////////////////////////////////////
1778
reed1b6ab442014-11-03 19:55:41 -08001779static int ltextblob_bounds(lua_State* L) {
1780 SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds());
1781 return 1;
1782}
1783
1784static int ltextblob_gc(lua_State* L) {
1785 SkSafeUnref(get_ref<SkTextBlob>(L, 1));
1786 return 0;
1787}
1788
1789static const struct luaL_Reg gSkTextBlob_Methods[] = {
1790 { "bounds", ltextblob_bounds },
1791 { "__gc", ltextblob_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001792 { nullptr, nullptr }
reed1b6ab442014-11-03 19:55:41 -08001793};
1794
1795///////////////////////////////////////////////////////////////////////////////
1796
reed36c9c112014-11-04 10:58:42 -08001797static int ltypeface_getFamilyName(lua_State* L) {
1798 SkString str;
1799 get_ref<SkTypeface>(L, 1)->getFamilyName(&str);
1800 lua_pushstring(L, str.c_str());
1801 return 1;
1802}
1803
1804static int ltypeface_getStyle(lua_State* L) {
Ben Wagner26308e12017-08-08 15:23:47 -04001805 push_obj(L, get_ref<SkTypeface>(L, 1)->fontStyle());
reed36c9c112014-11-04 10:58:42 -08001806 return 1;
1807}
1808
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001809static int ltypeface_gc(lua_State* L) {
commit-bot@chromium.org77887af2013-12-17 14:28:19 +00001810 SkSafeUnref(get_ref<SkTypeface>(L, 1));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001811 return 0;
1812}
1813
1814static const struct luaL_Reg gSkTypeface_Methods[] = {
reed36c9c112014-11-04 10:58:42 -08001815 { "getFamilyName", ltypeface_getFamilyName },
1816 { "getStyle", ltypeface_getStyle },
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001817 { "__gc", ltypeface_gc },
halcanary96fcdcc2015-08-27 07:41:13 -07001818 { nullptr, nullptr }
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001819};
1820
1821///////////////////////////////////////////////////////////////////////////////
1822
Ben Wagnerc6c10b42017-08-07 09:56:21 -04001823static int lfontstyle_weight(lua_State* L) {
1824 lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->weight());
1825 return 1;
1826}
1827
1828static int lfontstyle_width(lua_State* L) {
1829 lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->width());
1830 return 1;
1831}
1832
1833static int lfontstyle_slant(lua_State* L) {
1834 lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->slant());
1835 return 1;
1836}
1837
1838static int lfontstyle_gc(lua_State* L) {
1839 get_obj<SkFontStyle>(L, 1)->~SkFontStyle();
1840 return 0;
1841}
1842
1843static const struct luaL_Reg gSkFontStyle_Methods[] = {
1844 { "weight", lfontstyle_weight },
1845 { "width", lfontstyle_width },
1846 { "slant", lfontstyle_slant },
1847 { "__gc", lfontstyle_gc },
1848 { nullptr, nullptr }
1849};
1850
1851///////////////////////////////////////////////////////////////////////////////
1852
reed@google.com74ce6f02013-05-22 15:13:18 +00001853class AutoCallLua {
1854public:
1855 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1856 lua_getglobal(L, func);
1857 if (!lua_isfunction(L, -1)) {
1858 int t = lua_type(L, -1);
1859 SkDebugf("--- expected function %d\n", t);
1860 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001861
reed@google.com74ce6f02013-05-22 15:13:18 +00001862 lua_newtable(L);
1863 setfield_string(L, "verb", verb);
1864 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001865
reed@google.com74ce6f02013-05-22 15:13:18 +00001866 ~AutoCallLua() {
1867 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1868 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1869 }
1870 lua_settop(fL, -1);
1871 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +00001872
reed@google.com74ce6f02013-05-22 15:13:18 +00001873private:
1874 lua_State* fL;
1875};
1876
1877#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1878
1879///////////////////////////////////////////////////////////////////////////////
1880
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001881static int lsk_newDocumentPDF(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001882 const char* file = nullptr;
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001883 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001884 file = lua_tolstring(L, 1, nullptr);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001885 }
1886
halcanary676ab682016-05-03 12:10:04 -07001887 sk_sp<SkDocument> doc = SkDocument::MakePDF(file);
halcanary96fcdcc2015-08-27 07:41:13 -07001888 if (nullptr == doc) {
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001889 // do I need to push a nil on the stack and return 1?
1890 return 0;
1891 } else {
halcanary676ab682016-05-03 12:10:04 -07001892 push_ref(L, std::move(doc));
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00001893 return 1;
1894 }
1895}
1896
reed468b1812014-10-19 11:42:54 -07001897static int lsk_newBlurImageFilter(lua_State* L) {
1898 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1899 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
robertphillips6e7025a2016-04-04 04:31:25 -07001900 sk_sp<SkImageFilter> imf(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr));
1901 if (!imf) {
reed468b1812014-10-19 11:42:54 -07001902 lua_pushnil(L);
1903 } else {
robertphillips6e7025a2016-04-04 04:31:25 -07001904 push_ref(L, std::move(imf));
reed9fbc3f32014-10-21 07:12:58 -07001905 }
1906 return 1;
1907}
1908
1909static int lsk_newLinearGradient(lua_State* L) {
1910 SkScalar x0 = lua2scalar_def(L, 1, 0);
1911 SkScalar y0 = lua2scalar_def(L, 2, 0);
1912 SkColor c0 = lua2color(L, 3);
1913 SkScalar x1 = lua2scalar_def(L, 4, 0);
1914 SkScalar y1 = lua2scalar_def(L, 5, 0);
1915 SkColor c1 = lua2color(L, 6);
1916
1917 SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
1918 SkColor colors[] = { c0, c1 };
robertphillips6e7025a2016-04-04 04:31:25 -07001919 sk_sp<SkShader> s(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
1920 SkShader::kClamp_TileMode));
reed2ad1aa62016-03-09 09:50:50 -08001921 if (!s) {
reed9fbc3f32014-10-21 07:12:58 -07001922 lua_pushnil(L);
1923 } else {
reed2ad1aa62016-03-09 09:50:50 -08001924 push_ref(L, std::move(s));
reed468b1812014-10-19 11:42:54 -07001925 }
1926 return 1;
1927}
1928
reedbdc49ae2014-10-14 09:34:52 -07001929static int lsk_newMatrix(lua_State* L) {
1930 push_new<SkMatrix>(L)->reset();
1931 return 1;
1932}
1933
reed@google.com3597b732013-05-22 20:12:50 +00001934static int lsk_newPaint(lua_State* L) {
1935 push_new<SkPaint>(L);
1936 return 1;
1937}
1938
1939static int lsk_newPath(lua_State* L) {
1940 push_new<SkPath>(L);
1941 return 1;
1942}
1943
reed96affcd2014-10-13 12:38:04 -07001944static int lsk_newPictureRecorder(lua_State* L) {
1945 push_new<SkPictureRecorder>(L);
1946 return 1;
1947}
1948
reed@google.com3597b732013-05-22 20:12:50 +00001949static int lsk_newRRect(lua_State* L) {
reedbdc49ae2014-10-14 09:34:52 -07001950 push_new<SkRRect>(L)->setEmpty();
reed@google.com3597b732013-05-22 20:12:50 +00001951 return 1;
1952}
1953
reed1b6ab442014-11-03 19:55:41 -08001954#include "SkTextBox.h"
1955// Sk.newTextBlob(text, rect, paint)
1956static int lsk_newTextBlob(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001957 const char* text = lua_tolstring(L, 1, nullptr);
reed1b6ab442014-11-03 19:55:41 -08001958 SkRect bounds;
1959 lua2rect(L, 2, &bounds);
1960 const SkPaint& paint = *get_obj<SkPaint>(L, 3);
1961
1962 SkTextBox box;
1963 box.setMode(SkTextBox::kLineBreak_Mode);
1964 box.setBox(bounds);
1965 box.setText(text, strlen(text), paint);
1966
1967 SkScalar newBottom;
fmalita37283c22016-09-13 10:00:23 -07001968 push_ref<SkTextBlob>(L, box.snapshotTextBlob(&newBottom));
reed1b6ab442014-11-03 19:55:41 -08001969 SkLua(L).pushScalar(newBottom);
1970 return 2;
1971}
1972
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001973static int lsk_newTypeface(lua_State* L) {
halcanary96fcdcc2015-08-27 07:41:13 -07001974 const char* name = nullptr;
Ben Wagnerc6c10b42017-08-07 09:56:21 -04001975 SkFontStyle style;
skia.committer@gmail.com63193672013-06-08 07:01:13 +00001976
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001977 int count = lua_gettop(L);
1978 if (count > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001979 name = lua_tolstring(L, 1, nullptr);
Ben Wagnerc6c10b42017-08-07 09:56:21 -04001980 if (count > 1) {
1981 SkFontStyle* passedStyle = get_obj<SkFontStyle>(L, 2);
1982 if (passedStyle) {
1983 style = *passedStyle;
1984 }
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001985 }
1986 }
1987
Ben Wagnerc6c10b42017-08-07 09:56:21 -04001988 sk_sp<SkTypeface> face(SkTypeface::MakeFromName(name, style));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001989// SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
halcanary96fcdcc2015-08-27 07:41:13 -07001990 if (nullptr == face) {
bungeman13b9c952016-05-12 10:09:30 -07001991 face = SkTypeface::MakeDefault();
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001992 }
bungeman13b9c952016-05-12 10:09:30 -07001993 push_ref(L, std::move(face));
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00001994 return 1;
1995}
reed@google.com3597b732013-05-22 20:12:50 +00001996
Ben Wagnerc6c10b42017-08-07 09:56:21 -04001997static int lsk_newFontStyle(lua_State* L) {
1998 int count = lua_gettop(L);
1999 int weight = SkFontStyle::kNormal_Weight;
2000 int width = SkFontStyle::kNormal_Width;
2001 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
2002 if (count >= 1 && lua_isnumber(L, 1)) {
2003 weight = lua_tointegerx(L, 1, nullptr);
2004 }
2005 if (count >= 2 && lua_isnumber(L, 2)) {
2006 width = lua_tointegerx(L, 2, nullptr);
2007 }
2008 if (count >= 3 && lua_isnumber(L, 3)) {
2009 slant = static_cast<SkFontStyle::Slant>(lua_tointegerx(L, 3, nullptr));
2010 }
2011 push_new<SkFontStyle>(L, weight, width, slant);
2012 return 1;
2013}
2014
reed485557f2014-10-12 10:36:47 -07002015static int lsk_newRasterSurface(lua_State* L) {
reed7b864662014-11-04 13:24:47 -08002016 int width = lua2int_def(L, 1, 0);
reed485557f2014-10-12 10:36:47 -07002017 int height = lua2int_def(L, 2, 0);
2018 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
robertphillips702edbd2015-06-23 06:26:08 -07002019 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -07002020 auto surface = SkSurface::MakeRaster(info, &props);
halcanary96fcdcc2015-08-27 07:41:13 -07002021 if (nullptr == surface) {
reed485557f2014-10-12 10:36:47 -07002022 lua_pushnil(L);
2023 } else {
reede8f30622016-03-23 18:59:25 -07002024 push_ref(L, surface);
reed485557f2014-10-12 10:36:47 -07002025 }
2026 return 1;
2027}
2028
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002029static int lsk_loadImage(lua_State* L) {
2030 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
halcanary96fcdcc2015-08-27 07:41:13 -07002031 const char* name = lua_tolstring(L, 1, nullptr);
reed9ce9d672016-03-17 10:51:11 -07002032 sk_sp<SkData> data(SkData::MakeFromFileName(name));
2033 if (data) {
2034 auto image = SkImage::MakeFromEncoded(std::move(data));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002035 if (image) {
reed9ce9d672016-03-17 10:51:11 -07002036 push_ref(L, std::move(image));
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002037 return 1;
2038 }
2039 }
2040 }
2041 return 0;
2042}
2043
reed@google.com3597b732013-05-22 20:12:50 +00002044static void register_Sk(lua_State* L) {
2045 lua_newtable(L);
2046 lua_pushvalue(L, -1);
2047 lua_setglobal(L, "Sk");
2048 // the Sk table is still on top
2049
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002050 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002051 setfield_function(L, "loadImage", lsk_loadImage);
reed468b1812014-10-19 11:42:54 -07002052 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
reed9fbc3f32014-10-21 07:12:58 -07002053 setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
reedbdc49ae2014-10-14 09:34:52 -07002054 setfield_function(L, "newMatrix", lsk_newMatrix);
reed@google.com3597b732013-05-22 20:12:50 +00002055 setfield_function(L, "newPaint", lsk_newPaint);
2056 setfield_function(L, "newPath", lsk_newPath);
reed96affcd2014-10-13 12:38:04 -07002057 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
reed@google.com3597b732013-05-22 20:12:50 +00002058 setfield_function(L, "newRRect", lsk_newRRect);
reed485557f2014-10-12 10:36:47 -07002059 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
reed1b6ab442014-11-03 19:55:41 -08002060 setfield_function(L, "newTextBlob", lsk_newTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002061 setfield_function(L, "newTypeface", lsk_newTypeface);
Ben Wagnerc6c10b42017-08-07 09:56:21 -04002062 setfield_function(L, "newFontStyle", lsk_newFontStyle);
reed@google.com3597b732013-05-22 20:12:50 +00002063 lua_pop(L, 1); // pop off the Sk table
2064}
2065
reed@google.com74ce6f02013-05-22 15:13:18 +00002066#define REG_CLASS(L, C) \
2067 do { \
reed@google.com3597b732013-05-22 20:12:50 +00002068 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +00002069 lua_pushvalue(L, -1); \
2070 lua_setfield(L, -2, "__index"); \
2071 luaL_setfuncs(L, g##C##_Methods, 0); \
2072 lua_pop(L, 1); /* pop off the meta-table */ \
2073 } while (0)
2074
2075void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +00002076 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +00002077 REG_CLASS(L, SkCanvas);
reed22a517f2015-12-04 20:45:59 -08002078 REG_CLASS(L, SkColorFilter);
mike@reedtribe.orgfb858242013-06-08 16:39:44 +00002079 REG_CLASS(L, SkDocument);
mike@reedtribe.org792bbd12013-06-11 02:20:28 +00002080 REG_CLASS(L, SkImage);
reed468b1812014-10-19 11:42:54 -07002081 REG_CLASS(L, SkImageFilter);
reed1b6ab442014-11-03 19:55:41 -08002082 REG_CLASS(L, SkMatrix);
reed@google.com74ce6f02013-05-22 15:13:18 +00002083 REG_CLASS(L, SkPaint);
commit-bot@chromium.org1301bf32014-03-17 23:09:47 +00002084 REG_CLASS(L, SkPath);
2085 REG_CLASS(L, SkPathEffect);
reed96affcd2014-10-13 12:38:04 -07002086 REG_CLASS(L, SkPicture);
2087 REG_CLASS(L, SkPictureRecorder);
reed@google.com74ce6f02013-05-22 15:13:18 +00002088 REG_CLASS(L, SkRRect);
reed@google.com5fdc9832013-07-24 15:47:52 +00002089 REG_CLASS(L, SkShader);
reed485557f2014-10-12 10:36:47 -07002090 REG_CLASS(L, SkSurface);
reed1b6ab442014-11-03 19:55:41 -08002091 REG_CLASS(L, SkTextBlob);
mike@reedtribe.orge6469f12013-06-08 03:15:47 +00002092 REG_CLASS(L, SkTypeface);
Ben Wagnerc6c10b42017-08-07 09:56:21 -04002093 REG_CLASS(L, SkFontStyle);
reed@google.com74ce6f02013-05-22 15:13:18 +00002094}
zachr@google.com28c27c82013-06-20 17:15:05 +00002095
reed@google.com7bce9982013-06-20 17:40:21 +00002096extern "C" int luaopen_skia(lua_State* L);
zachr@google.com28c27c82013-06-20 17:15:05 +00002097extern "C" int luaopen_skia(lua_State* L) {
2098 SkLua::Load(L);
2099 return 0;
2100}