reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 9 | |
| 10 | #if SK_SUPPORT_GPU |
Mike Reed | 4f364fd | 2017-01-19 14:34:51 -0500 | [diff] [blame] | 11 | //#include "GrReducedClip.h" |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 14 | #include "SkBlurImageFilter.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 15 | #include "SkCanvas.h" |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 16 | #include "SkColorFilter.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 17 | #include "SkData.h" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 18 | #include "SkDocument.h" |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 19 | #include "SkFontStyle.h" |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 20 | #include "SkGradientShader.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 21 | #include "SkImage.h" |
| 22 | #include "SkMatrix.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 23 | #include "SkPaint.h" |
| 24 | #include "SkPath.h" |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 25 | #include "SkPictureRecorder.h" |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 26 | #include "SkPixelRef.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 27 | #include "SkRRect.h" |
| 28 | #include "SkString.h" |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 29 | #include "SkSurface.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 30 | #include "SkTextBlob.h" |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 31 | #include "SkTypeface.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 32 | |
| 33 | extern "C" { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 34 | #include "lua.h" |
| 35 | #include "lualib.h" |
| 36 | #include "lauxlib.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 39 | // return the metatable name for a given class |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 40 | template <typename T> const char* get_mtname(); |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 41 | #define DEF_MTNAME(T) \ |
| 42 | template <> const char* get_mtname<T>() { \ |
| 43 | return #T "_LuaMetaTableName"; \ |
| 44 | } |
| 45 | |
| 46 | DEF_MTNAME(SkCanvas) |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 47 | DEF_MTNAME(SkColorFilter) |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 48 | DEF_MTNAME(SkDocument) |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 49 | DEF_MTNAME(SkImage) |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 50 | DEF_MTNAME(SkImageFilter) |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 51 | DEF_MTNAME(SkMatrix) |
| 52 | DEF_MTNAME(SkRRect) |
| 53 | DEF_MTNAME(SkPath) |
| 54 | DEF_MTNAME(SkPaint) |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 55 | DEF_MTNAME(SkPathEffect) |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 56 | DEF_MTNAME(SkPicture) |
| 57 | DEF_MTNAME(SkPictureRecorder) |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 58 | DEF_MTNAME(SkShader) |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 59 | DEF_MTNAME(SkSurface) |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 60 | DEF_MTNAME(SkTextBlob) |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 61 | DEF_MTNAME(SkTypeface) |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 62 | DEF_MTNAME(SkFontStyle) |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 63 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 64 | template <typename T, typename... Args> T* push_new(lua_State* L, Args&&... args) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 65 | T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 66 | new (addr) T(std::forward<Args>(args)...); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 67 | luaL_getmetatable(L, get_mtname<T>()); |
| 68 | lua_setmetatable(L, -2); |
| 69 | return addr; |
| 70 | } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 71 | |
| 72 | template <typename T> void push_obj(lua_State* L, const T& obj) { |
| 73 | new (lua_newuserdata(L, sizeof(T))) T(obj); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 74 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 75 | lua_setmetatable(L, -2); |
| 76 | } |
| 77 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 78 | template <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 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 85 | template <typename T> T* push_ref(lua_State* L, T* ref) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 86 | *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 87 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 88 | lua_setmetatable(L, -2); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 89 | return ref; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 90 | } |
| 91 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 92 | template <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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 98 | template <typename T> T* get_ref(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 99 | return *(T**)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | template <typename T> T* get_obj(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 103 | return (T*)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 106 | static bool lua2bool(lua_State* L, int index) { |
| 107 | return !!lua_toboolean(L, index); |
| 108 | } |
| 109 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 112 | SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) { |
| 113 | fL = luaL_newstate(); |
| 114 | luaL_openlibs(fL); |
| 115 | SkLua::Load(fL); |
| 116 | } |
| 117 | |
| 118 | SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {} |
| 119 | |
| 120 | SkLua::~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 | |
| 132 | bool SkLua::runCode(const char code[]) { |
| 133 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 134 | if (err) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 135 | SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1)); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool 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.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 150 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 157 | static 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 | |
| 162 | static 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.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 167 | static 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.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 172 | static void setfield_scalar(lua_State* L, const char key[], SkScalar value) { |
| 173 | setfield_number(L, key, SkScalarToLua(value)); |
| 174 | } |
| 175 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 176 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 182 | static 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 | |
| 190 | static SkScalar lua2scalar(lua_State* L, int index) { |
| 191 | SkASSERT(lua_isnumber(L, index)); |
| 192 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 193 | } |
| 194 | |
| 195 | static 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 | |
| 203 | static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) { |
| 204 | SkASSERT(lua_istable(L, stackIndex)); |
| 205 | lua_rawgeti(L, stackIndex, arrayIndex); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 206 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 207 | SkScalar value = lua2scalar(L, -1); |
| 208 | lua_pop(L, 1); |
| 209 | return value; |
| 210 | } |
| 211 | |
| 212 | static 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 | |
| 218 | static 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.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 222 | static 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.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 227 | static void setarray_scalar(lua_State* L, int index, SkScalar value) { |
| 228 | setarray_number(L, index, SkScalarToLua(value)); |
| 229 | } |
| 230 | |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 231 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 236 | void SkLua::pushBool(bool value, const char key[]) { |
| 237 | lua_pushboolean(fL, value); |
| 238 | CHECK_SETFIELD(key); |
| 239 | } |
| 240 | |
| 241 | void SkLua::pushString(const char str[], const char key[]) { |
| 242 | lua_pushstring(fL, str); |
| 243 | CHECK_SETFIELD(key); |
| 244 | } |
| 245 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 246 | void 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 253 | void SkLua::pushString(const SkString& str, const char key[]) { |
| 254 | lua_pushstring(fL, str.c_str()); |
| 255 | CHECK_SETFIELD(key); |
| 256 | } |
| 257 | |
| 258 | void 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.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 267 | void SkLua::pushU32(uint32_t value, const char key[]) { |
| 268 | lua_pushnumber(fL, (double)value); |
| 269 | CHECK_SETFIELD(key); |
| 270 | } |
| 271 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 272 | void SkLua::pushScalar(SkScalar value, const char key[]) { |
| 273 | lua_pushnumber(fL, SkScalarToLua(value)); |
| 274 | CHECK_SETFIELD(key); |
| 275 | } |
| 276 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 277 | void 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.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 286 | void 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.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 298 | void 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 307 | void SkLua::pushRect(const SkRect& r, const char key[]) { |
| 308 | lua_newtable(fL); |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 309 | 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 313 | CHECK_SETFIELD(key); |
| 314 | } |
| 315 | |
| 316 | void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
| 317 | push_obj(fL, rr); |
| 318 | CHECK_SETFIELD(key); |
| 319 | } |
| 320 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 321 | void 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 329 | void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) { |
| 330 | push_obj(fL, matrix); |
| 331 | CHECK_SETFIELD(key); |
| 332 | } |
| 333 | |
| 334 | void SkLua::pushPaint(const SkPaint& paint, const char key[]) { |
| 335 | push_obj(fL, paint); |
| 336 | CHECK_SETFIELD(key); |
| 337 | } |
| 338 | |
| 339 | void SkLua::pushPath(const SkPath& path, const char key[]) { |
| 340 | push_obj(fL, path); |
| 341 | CHECK_SETFIELD(key); |
| 342 | } |
| 343 | |
| 344 | void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 345 | push_ptr(fL, canvas); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 346 | CHECK_SETFIELD(key); |
| 347 | } |
| 348 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 349 | void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) { |
| 350 | push_ref(fL, const_cast<SkTextBlob*>(blob)); |
| 351 | CHECK_SETFIELD(key); |
| 352 | } |
| 353 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 354 | /////////////////////////////////////////////////////////////////////////////// |
| 355 | /////////////////////////////////////////////////////////////////////////////// |
| 356 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 357 | static 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); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 361 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 362 | SkScalar value = lua2scalar(L, -1); |
| 363 | lua_pop(L, 1); |
| 364 | return value; |
| 365 | } |
| 366 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 367 | static 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.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 371 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 372 | 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 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 382 | static SkScalar byte2unit(U8CPU byte) { |
| 383 | return byte / 255.0f; |
| 384 | } |
| 385 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 386 | static 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 | |
| 396 | static SkColor lua2color(lua_State* L, int index) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 397 | 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 404 | rect->set(getfield_scalar_default(L, index, "left", 0), |
| 405 | getfield_scalar_default(L, index, "top", 0), |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 406 | getfield_scalar(L, index, "right"), |
| 407 | getfield_scalar(L, index, "bottom")); |
| 408 | return rect; |
| 409 | } |
| 410 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 411 | static int lcanvas_clear(lua_State* L) { |
| 412 | get_ref<SkCanvas>(L, 1)->clear(0); |
| 413 | return 0; |
| 414 | } |
| 415 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 416 | static int lcanvas_drawColor(lua_State* L) { |
| 417 | get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2)); |
| 418 | return 0; |
| 419 | } |
| 420 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 421 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 426 | static int lcanvas_drawRect(lua_State* L) { |
| 427 | SkRect rect; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 428 | lua2rect(L, 2, &rect); |
| 429 | const SkPaint* paint = get_obj<SkPaint>(L, 3); |
| 430 | get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static 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 | |
| 441 | static 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 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 449 | static 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; |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 453 | } else if (lua_isuserdata(L, index)) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 454 | const SkPaint* ptr = get_obj<SkPaint>(L, index); |
| 455 | if (ptr) { |
| 456 | *paint = *ptr; |
| 457 | return paint; |
| 458 | } |
| 459 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 460 | return nullptr; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 461 | } |
| 462 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 463 | static int lcanvas_drawImage(lua_State* L) { |
| 464 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 465 | SkImage* image = get_ref<SkImage>(L, 2); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 466 | if (nullptr == image) { |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | SkScalar x = lua2scalar(L, 3); |
| 470 | SkScalar y = lua2scalar(L, 4); |
| 471 | |
| 472 | SkPaint paint; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 473 | canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 477 | static int lcanvas_drawImageRect(lua_State* L) { |
| 478 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 479 | SkImage* image = get_ref<SkImage>(L, 2); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 480 | if (nullptr == image) { |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | SkRect srcR, dstR; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 485 | SkRect* srcRPtr = nullptr; |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 486 | if (!lua_isnil(L, 3)) { |
| 487 | srcRPtr = lua2rect(L, 3, &srcR); |
| 488 | } |
| 489 | lua2rect(L, 4, &dstR); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 490 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 491 | SkPaint paint; |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 492 | canvas->legacy_drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint)); |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 496 | static int lcanvas_drawPatch(lua_State* L) { |
| 497 | SkPoint cubics[12]; |
| 498 | SkColor colorStorage[4]; |
| 499 | SkPoint texStorage[4]; |
| 500 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 501 | const SkColor* colors = nullptr; |
| 502 | const SkPoint* texs = nullptr; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 503 | |
| 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 Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 518 | get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, *get_obj<SkPaint>(L, 5)); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 522 | static 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 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 528 | // drawPicture(pic, x, y, paint) |
| 529 | static 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 534 | SkMatrix matrix, *matrixPtr = nullptr; |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 535 | 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.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 544 | static 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 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 559 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 568 | static int lcanvas_getSaveCount(lua_State* L) { |
| 569 | lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | static int lcanvas_getTotalMatrix(lua_State* L) { |
| 574 | SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
| 575 | return 1; |
| 576 | } |
| 577 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 578 | static int lcanvas_save(lua_State* L) { |
| 579 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save()); |
| 580 | return 1; |
| 581 | } |
| 582 | |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 583 | static int lcanvas_saveLayer(lua_State* L) { |
| 584 | SkPaint paint; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 585 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(nullptr, lua2OptionalPaint(L, 2, &paint))); |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 586 | return 1; |
| 587 | } |
| 588 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 589 | static int lcanvas_restore(lua_State* L) { |
| 590 | get_ref<SkCanvas>(L, 1)->restore(); |
| 591 | return 0; |
| 592 | } |
| 593 | |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 594 | static 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.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 601 | static int lcanvas_translate(lua_State* L) { |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 602 | 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 | |
| 608 | static int lcanvas_rotate(lua_State* L) { |
| 609 | SkScalar degrees = lua2scalar_def(L, 2, 0); |
| 610 | get_ref<SkCanvas>(L, 1)->rotate(degrees); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 614 | static int lcanvas_concat(lua_State* L) { |
| 615 | get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2)); |
| 616 | return 0; |
| 617 | } |
| 618 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 619 | static int lcanvas_newSurface(lua_State* L) { |
| 620 | int width = lua2int_def(L, 2, 0); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 621 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 622 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 623 | auto surface = get_ref<SkCanvas>(L, 1)->makeSurface(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 624 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 625 | lua_pushnil(L); |
| 626 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 627 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 628 | } |
| 629 | return 1; |
| 630 | } |
| 631 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 632 | static int lcanvas_gc(lua_State* L) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 633 | // don't know how to track a ptr... |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 637 | const struct luaL_Reg gSkCanvas_Methods[] = { |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 638 | { "clear", lcanvas_clear }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 639 | { "drawColor", lcanvas_drawColor }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 640 | { "drawPaint", lcanvas_drawPaint }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 641 | { "drawRect", lcanvas_drawRect }, |
| 642 | { "drawOval", lcanvas_drawOval }, |
| 643 | { "drawCircle", lcanvas_drawCircle }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 644 | { "drawImage", lcanvas_drawImage }, |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 645 | { "drawImageRect", lcanvas_drawImageRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 646 | { "drawPatch", lcanvas_drawPatch }, |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 647 | { "drawPath", lcanvas_drawPath }, |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 648 | { "drawPicture", lcanvas_drawPicture }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 649 | { "drawText", lcanvas_drawText }, |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 650 | { "drawTextBlob", lcanvas_drawTextBlob }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 651 | { "getSaveCount", lcanvas_getSaveCount }, |
| 652 | { "getTotalMatrix", lcanvas_getTotalMatrix }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 653 | { "save", lcanvas_save }, |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 654 | { "saveLayer", lcanvas_saveLayer }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 655 | { "restore", lcanvas_restore }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 656 | { "scale", lcanvas_scale }, |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 657 | { "translate", lcanvas_translate }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 658 | { "rotate", lcanvas_rotate }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 659 | { "concat", lcanvas_concat }, |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 660 | |
| 661 | { "newSurface", lcanvas_newSurface }, |
| 662 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 663 | { "__gc", lcanvas_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 664 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 665 | }; |
| 666 | |
| 667 | /////////////////////////////////////////////////////////////////////////////// |
| 668 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 669 | static int ldocument_beginPage(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 670 | const SkRect* contentPtr = nullptr; |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 671 | push_ptr(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 672 | lua2scalar(L, 3), |
| 673 | contentPtr)); |
| 674 | return 1; |
| 675 | } |
| 676 | |
| 677 | static int ldocument_endPage(lua_State* L) { |
| 678 | get_ref<SkDocument>(L, 1)->endPage(); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int ldocument_close(lua_State* L) { |
| 683 | get_ref<SkDocument>(L, 1)->close(); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int ldocument_gc(lua_State* L) { |
| 688 | get_ref<SkDocument>(L, 1)->unref(); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static const struct luaL_Reg gSkDocument_Methods[] = { |
| 693 | { "beginPage", ldocument_beginPage }, |
| 694 | { "endPage", ldocument_endPage }, |
| 695 | { "close", ldocument_close }, |
| 696 | { "__gc", ldocument_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 697 | { nullptr, nullptr } |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | /////////////////////////////////////////////////////////////////////////////// |
| 701 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 702 | static int lpaint_isAntiAlias(lua_State* L) { |
| 703 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias()); |
| 704 | return 1; |
| 705 | } |
| 706 | |
| 707 | static int lpaint_setAntiAlias(lua_State* L) { |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 708 | get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 712 | static int lpaint_isDither(lua_State* L) { |
| 713 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither()); |
| 714 | return 1; |
| 715 | } |
| 716 | |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 717 | static int lpaint_setDither(lua_State* L) { |
| 718 | get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2)); |
| 719 | return 0; |
| 720 | } |
| 721 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 722 | static int lpaint_isFakeBoldText(lua_State* L) { |
| 723 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText()); |
| 724 | return 1; |
| 725 | } |
| 726 | |
| 727 | static int lpaint_isLinearText(lua_State* L) { |
| 728 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText()); |
| 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | static int lpaint_isSubpixelText(lua_State* L) { |
| 733 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText()); |
| 734 | return 1; |
| 735 | } |
| 736 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 737 | static int lpaint_setSubpixelText(lua_State* L) { |
| 738 | get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2)); |
| 739 | return 1; |
| 740 | } |
| 741 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 742 | static int lpaint_isDevKernText(lua_State* L) { |
| 743 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText()); |
| 744 | return 1; |
| 745 | } |
| 746 | |
| 747 | static int lpaint_isLCDRenderText(lua_State* L) { |
| 748 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText()); |
| 749 | return 1; |
| 750 | } |
| 751 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 752 | static int lpaint_setLCDRenderText(lua_State* L) { |
| 753 | get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2)); |
| 754 | return 1; |
| 755 | } |
| 756 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 757 | static int lpaint_isEmbeddedBitmapText(lua_State* L) { |
| 758 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText()); |
| 759 | return 1; |
| 760 | } |
| 761 | |
| 762 | static int lpaint_isAutohinted(lua_State* L) { |
| 763 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted()); |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | static int lpaint_isVerticalText(lua_State* L) { |
| 768 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText()); |
| 769 | return 1; |
| 770 | } |
| 771 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 772 | static int lpaint_getAlpha(lua_State* L) { |
| 773 | SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha())); |
| 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | static int lpaint_setAlpha(lua_State* L) { |
| 778 | get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2))); |
| 779 | return 0; |
| 780 | } |
| 781 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 782 | static int lpaint_getColor(lua_State* L) { |
| 783 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 784 | return 1; |
| 785 | } |
| 786 | |
| 787 | static int lpaint_setColor(lua_State* L) { |
| 788 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 789 | return 0; |
| 790 | } |
| 791 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 792 | static int lpaint_getTextSize(lua_State* L) { |
| 793 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 794 | return 1; |
| 795 | } |
| 796 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 797 | static int lpaint_getTextScaleX(lua_State* L) { |
| 798 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX()); |
| 799 | return 1; |
| 800 | } |
| 801 | |
| 802 | static int lpaint_getTextSkewX(lua_State* L) { |
| 803 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX()); |
| 804 | return 1; |
| 805 | } |
| 806 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 807 | static int lpaint_setTextSize(lua_State* L) { |
| 808 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 809 | return 0; |
| 810 | } |
| 811 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 812 | static int lpaint_getTypeface(lua_State* L) { |
| 813 | push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); |
| 814 | return 1; |
| 815 | } |
| 816 | |
| 817 | static int lpaint_setTypeface(lua_State* L) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 818 | get_obj<SkPaint>(L, 1)->setTypeface(sk_ref_sp(get_ref<SkTypeface>(L, 2))); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 822 | static int lpaint_getHinting(lua_State* L) { |
| 823 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting()); |
| 824 | return 1; |
| 825 | } |
| 826 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 827 | static int lpaint_getFilterQuality(lua_State* L) { |
| 828 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality()); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 829 | return 1; |
| 830 | } |
| 831 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 832 | static int lpaint_setFilterQuality(lua_State* L) { |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 833 | int level = lua2int_def(L, 2, -1); |
| 834 | if (level >= 0 && level <= 3) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 835 | get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 836 | } |
| 837 | return 0; |
| 838 | } |
| 839 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 840 | static 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.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 846 | static 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 | |
| 855 | static 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 | |
| 866 | static 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.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 870 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 871 | 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.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 881 | static int lpaint_getStroke(lua_State* L) { |
| 882 | lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle()); |
| 883 | return 1; |
| 884 | } |
| 885 | |
| 886 | static int lpaint_setStroke(lua_State* L) { |
| 887 | SkPaint::Style style; |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 888 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 889 | 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.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 898 | static int lpaint_getStrokeCap(lua_State* L) { |
| 899 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap()); |
| 900 | return 1; |
| 901 | } |
| 902 | |
| 903 | static int lpaint_getStrokeJoin(lua_State* L) { |
| 904 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin()); |
| 905 | return 1; |
| 906 | } |
| 907 | |
| 908 | static int lpaint_getTextEncoding(lua_State* L) { |
commit-bot@chromium.org | 641bcc3 | 2013-12-19 10:39:59 +0000 | [diff] [blame] | 909 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding()); |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 910 | return 1; |
| 911 | } |
| 912 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 913 | static int lpaint_getStrokeWidth(lua_State* L) { |
| 914 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); |
| 915 | return 1; |
| 916 | } |
| 917 | |
| 918 | static int lpaint_setStrokeWidth(lua_State* L) { |
| 919 | get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); |
| 920 | return 0; |
| 921 | } |
| 922 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 923 | static int lpaint_getStrokeMiter(lua_State* L) { |
| 924 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter()); |
| 925 | return 1; |
| 926 | } |
| 927 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 928 | static 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 | |
| 938 | struct 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 | |
| 950 | static int lpaint_getFontMetrics(lua_State* L) { |
| 951 | SkPaint::FontMetrics fm; |
| 952 | SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 953 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 954 | 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.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 964 | static int lpaint_getEffects(lua_State* L) { |
| 965 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 966 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 967 | lua_newtable(L); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 968 | 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.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 973 | setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); |
| 974 | setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 975 | return 1; |
| 976 | } |
| 977 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 978 | static 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 | |
| 988 | static int lpaint_setColorFilter(lua_State* L) { |
| 989 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 990 | paint->setColorFilter(sk_ref_sp(get_ref<SkColorFilter>(L, 2))); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 991 | return 0; |
| 992 | } |
| 993 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 994 | static 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 | |
| 1004 | static int lpaint_setImageFilter(lua_State* L) { |
| 1005 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 1006 | paint->setImageFilter(sk_ref_sp(get_ref<SkImageFilter>(L, 2))); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1007 | return 0; |
| 1008 | } |
| 1009 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1010 | static 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 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1020 | static int lpaint_setShader(lua_State* L) { |
| 1021 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1022 | paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2))); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1026 | static 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 | |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1036 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1049 | static int lpaint_gc(lua_State* L) { |
| 1050 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 1055 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 1056 | { "setAntiAlias", lpaint_setAntiAlias }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1057 | { "isDither", lpaint_isDither }, |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 1058 | { "setDither", lpaint_setDither }, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 1059 | { "getFilterQuality", lpaint_getFilterQuality }, |
| 1060 | { "setFilterQuality", lpaint_setFilterQuality }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1061 | { "isFakeBoldText", lpaint_isFakeBoldText }, |
| 1062 | { "isLinearText", lpaint_isLinearText }, |
| 1063 | { "isSubpixelText", lpaint_isSubpixelText }, |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 1064 | { "setSubpixelText", lpaint_setSubpixelText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1065 | { "isDevKernText", lpaint_isDevKernText }, |
| 1066 | { "isLCDRenderText", lpaint_isLCDRenderText }, |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1067 | { "setLCDRenderText", lpaint_setLCDRenderText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1068 | { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, |
| 1069 | { "isAutohinted", lpaint_isAutohinted }, |
| 1070 | { "isVerticalText", lpaint_isVerticalText }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1071 | { "getAlpha", lpaint_getAlpha }, |
| 1072 | { "setAlpha", lpaint_setAlpha }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1073 | { "getColor", lpaint_getColor }, |
| 1074 | { "setColor", lpaint_setColor }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1075 | { "getTextSize", lpaint_getTextSize }, |
| 1076 | { "setTextSize", lpaint_setTextSize }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1077 | { "getTextScaleX", lpaint_getTextScaleX }, |
| 1078 | { "getTextSkewX", lpaint_getTextSkewX }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1079 | { "getTypeface", lpaint_getTypeface }, |
| 1080 | { "setTypeface", lpaint_setTypeface }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1081 | { "getHinting", lpaint_getHinting }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1082 | { "getFontID", lpaint_getFontID }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1083 | { "getTextAlign", lpaint_getTextAlign }, |
| 1084 | { "setTextAlign", lpaint_setTextAlign }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1085 | { "getStroke", lpaint_getStroke }, |
| 1086 | { "setStroke", lpaint_setStroke }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1087 | { "getStrokeCap", lpaint_getStrokeCap }, |
| 1088 | { "getStrokeJoin", lpaint_getStrokeJoin }, |
| 1089 | { "getTextEncoding", lpaint_getTextEncoding }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1090 | { "getStrokeWidth", lpaint_getStrokeWidth }, |
| 1091 | { "setStrokeWidth", lpaint_setStrokeWidth }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1092 | { "getStrokeMiter", lpaint_getStrokeMiter }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1093 | { "measureText", lpaint_measureText }, |
| 1094 | { "getFontMetrics", lpaint_getFontMetrics }, |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1095 | { "getEffects", lpaint_getEffects }, |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1096 | { "getColorFilter", lpaint_getColorFilter }, |
| 1097 | { "setColorFilter", lpaint_setColorFilter }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1098 | { "getImageFilter", lpaint_getImageFilter }, |
| 1099 | { "setImageFilter", lpaint_setImageFilter }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1100 | { "getShader", lpaint_getShader }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1101 | { "setShader", lpaint_setShader }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1102 | { "getPathEffect", lpaint_getPathEffect }, |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1103 | { "getFillPath", lpaint_getFillPath }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1104 | { "__gc", lpaint_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1105 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | /////////////////////////////////////////////////////////////////////////////// |
| 1109 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1110 | static 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 | |
| 1116 | static 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 | |
| 1124 | static int lshader_isOpaque(lua_State* L) { |
| 1125 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1126 | return shader && shader->isOpaque(); |
| 1127 | } |
| 1128 | |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1129 | static int lshader_isAImage(lua_State* L) { |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1130 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1131 | if (shader) { |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1132 | SkMatrix matrix; |
| 1133 | SkShader::TileMode modes[2]; |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1134 | if (SkImage* image = shader->isAImage(&matrix, modes)) { |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1135 | lua_newtable(L); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1136 | setfield_number(L, "id", image->uniqueID()); |
| 1137 | setfield_number(L, "width", image->width()); |
| 1138 | setfield_number(L, "height", image->height()); |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1139 | setfield_string(L, "tileX", mode2string(modes[0])); |
| 1140 | setfield_string(L, "tileY", mode2string(modes[1])); |
| 1141 | return 1; |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static 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.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1152 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1153 | SkShader::GradientType t = shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1154 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1155 | if (SkShader::kNone_GradientType != t) { |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1156 | SkAutoTArray<SkScalar> pos(info.fColorCount); |
| 1157 | info.fColorOffsets = pos.get(); |
| 1158 | shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1159 | |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1160 | lua_newtable(L); |
fmenozzi | 7f2c85e | 2016-07-12 09:17:39 -0700 | [diff] [blame] | 1161 | setfield_string(L, "type", gradtype2string(t)); |
| 1162 | setfield_string(L, "tile", mode2string(info.fTileMode)); |
| 1163 | setfield_number(L, "colorCount", info.fColorCount); |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1164 | |
| 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.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static int lshader_gc(lua_State* L) { |
| 1179 | get_ref<SkShader>(L, 1)->unref(); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | static const struct luaL_Reg gSkShader_Methods[] = { |
| 1184 | { "isOpaque", lshader_isOpaque }, |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1185 | { "isAImage", lshader_isAImage }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1186 | { "asAGradient", lshader_asAGradient }, |
| 1187 | { "__gc", lshader_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1188 | { nullptr, nullptr } |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1189 | }; |
| 1190 | |
| 1191 | /////////////////////////////////////////////////////////////////////////////// |
| 1192 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1193 | static 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.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1209 | static int lpatheffect_gc(lua_State* L) { |
| 1210 | get_ref<SkPathEffect>(L, 1)->unref(); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | static const struct luaL_Reg gSkPathEffect_Methods[] = { |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1215 | { "asADash", lpatheffect_asADash }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1216 | { "__gc", lpatheffect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1217 | { nullptr, nullptr } |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1218 | }; |
| 1219 | |
| 1220 | /////////////////////////////////////////////////////////////////////////////// |
| 1221 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1222 | static int lpcolorfilter_gc(lua_State* L) { |
| 1223 | get_ref<SkColorFilter>(L, 1)->unref(); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static const struct luaL_Reg gSkColorFilter_Methods[] = { |
| 1228 | { "__gc", lpcolorfilter_gc }, |
| 1229 | { nullptr, nullptr } |
| 1230 | }; |
| 1231 | |
| 1232 | /////////////////////////////////////////////////////////////////////////////// |
| 1233 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1234 | static int lpimagefilter_gc(lua_State* L) { |
| 1235 | get_ref<SkImageFilter>(L, 1)->unref(); |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static const struct luaL_Reg gSkImageFilter_Methods[] = { |
| 1240 | { "__gc", lpimagefilter_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1241 | { nullptr, nullptr } |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1242 | }; |
| 1243 | |
| 1244 | /////////////////////////////////////////////////////////////////////////////// |
| 1245 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1246 | static int lmatrix_getType(lua_State* L) { |
| 1247 | SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1248 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1249 | 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.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1257 | static int lmatrix_getScaleX(lua_State* L) { |
| 1258 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX()); |
| 1259 | return 1; |
| 1260 | } |
| 1261 | |
| 1262 | static int lmatrix_getScaleY(lua_State* L) { |
| 1263 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY()); |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
| 1267 | static int lmatrix_getTranslateX(lua_State* L) { |
| 1268 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX()); |
| 1269 | return 1; |
| 1270 | } |
| 1271 | |
| 1272 | static int lmatrix_getTranslateY(lua_State* L) { |
| 1273 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY()); |
| 1274 | return 1; |
| 1275 | } |
| 1276 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1277 | static 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 | |
| 1282 | static 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 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1290 | static 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.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1321 | static const struct luaL_Reg gSkMatrix_Methods[] = { |
| 1322 | { "getType", lmatrix_getType }, |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1323 | { "getScaleX", lmatrix_getScaleX }, |
| 1324 | { "getScaleY", lmatrix_getScaleY }, |
| 1325 | { "getTranslateX", lmatrix_getTranslateX }, |
| 1326 | { "getTranslateY", lmatrix_getTranslateY }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1327 | { "setRectToRect", lmatrix_setRectToRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1328 | { "invert", lmatrix_invert }, |
| 1329 | { "mapXY", lmatrix_mapXY }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1330 | { nullptr, nullptr } |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1331 | }; |
| 1332 | |
| 1333 | /////////////////////////////////////////////////////////////////////////////// |
| 1334 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1335 | static int lpath_getBounds(lua_State* L) { |
| 1336 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1340 | static 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 | |
| 1354 | static 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 | |
| 1360 | static 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 | |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1395 | static int lpath_getSegmentTypes(lua_State* L) { |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1396 | uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks(); |
| 1397 | SkLua(L).pushString(segment_masks_to_str(segMasks)); |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1407 | static int lpath_isEmpty(lua_State* L) { |
| 1408 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 1409 | return 1; |
| 1410 | } |
| 1411 | |
| 1412 | static 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 | |
| 1424 | static 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 | |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1432 | static int lpath_isNestedFillRects(lua_State* L) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1433 | SkRect rects[2]; |
| 1434 | SkPath::Direction dirs[2]; |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1435 | bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1436 | 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.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1449 | static int lpath_countPoints(lua_State* L) { |
| 1450 | lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints()); |
| 1451 | return 1; |
| 1452 | } |
| 1453 | |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1454 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1493 | static int lpath_reset(lua_State* L) { |
| 1494 | get_obj<SkPath>(L, 1)->reset(); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | static int lpath_moveTo(lua_State* L) { |
| 1499 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static int lpath_lineTo(lua_State* L) { |
| 1504 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | static 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 | |
| 1514 | static 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 | |
| 1521 | static int lpath_close(lua_State* L) { |
| 1522 | get_obj<SkPath>(L, 1)->close(); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | static int lpath_gc(lua_State* L) { |
| 1527 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 1532 | { "getBounds", lpath_getBounds }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1533 | { "getFillType", lpath_getFillType }, |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1534 | { "getSegmentTypes", lpath_getSegmentTypes }, |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1535 | { "getVerbs", lpath_getVerbs }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1536 | { "isConvex", lpath_isConvex }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1537 | { "isEmpty", lpath_isEmpty }, |
| 1538 | { "isRect", lpath_isRect }, |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1539 | { "isNestedFillRects", lpath_isNestedFillRects }, |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1540 | { "countPoints", lpath_countPoints }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1541 | { "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 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1548 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1549 | }; |
| 1550 | |
| 1551 | /////////////////////////////////////////////////////////////////////////////// |
| 1552 | |
| 1553 | static const char* rrect_type(const SkRRect& rr) { |
| 1554 | switch (rr.getType()) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1555 | 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.org | f338d7c | 2014-03-17 21:17:30 +0000 | [diff] [blame] | 1559 | case SkRRect::kNinePatch_Type: return "nine-patch"; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1560 | case SkRRect::kComplex_Type: return "complex"; |
| 1561 | } |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1562 | SkDEBUGFAIL("never get here"); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1563 | return ""; |
| 1564 | } |
| 1565 | |
| 1566 | static int lrrect_rect(lua_State* L) { |
| 1567 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 1568 | return 1; |
| 1569 | } |
| 1570 | |
| 1571 | static int lrrect_type(lua_State* L) { |
| 1572 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
| 1576 | static int lrrect_radii(lua_State* L) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 1577 | int corner = SkToInt(lua_tointeger(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1578 | 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 | |
| 1590 | static int lrrect_gc(lua_State* L) { |
| 1591 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 1596 | { "rect", lrrect_rect }, |
| 1597 | { "type", lrrect_type }, |
| 1598 | { "radii", lrrect_radii }, |
| 1599 | { "__gc", lrrect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1600 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1601 | }; |
| 1602 | |
| 1603 | /////////////////////////////////////////////////////////////////////////////// |
| 1604 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1605 | static int limage_width(lua_State* L) { |
| 1606 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->width()); |
| 1607 | return 1; |
| 1608 | } |
| 1609 | |
| 1610 | static int limage_height(lua_State* L) { |
| 1611 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->height()); |
| 1612 | return 1; |
| 1613 | } |
| 1614 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1615 | static int limage_newShader(lua_State* L) { |
| 1616 | SkShader::TileMode tmode = SkShader::kClamp_TileMode; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1617 | const SkMatrix* localM = nullptr; |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 1618 | push_ref(L, get_ref<SkImage>(L, 1)->makeShader(tmode, tmode, localM)); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1619 | return 1; |
| 1620 | } |
| 1621 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1622 | static int limage_gc(lua_State* L) { |
| 1623 | get_ref<SkImage>(L, 1)->unref(); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static const struct luaL_Reg gSkImage_Methods[] = { |
| 1628 | { "width", limage_width }, |
| 1629 | { "height", limage_height }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1630 | { "newShader", limage_newShader }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1631 | { "__gc", limage_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1632 | { nullptr, nullptr } |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1633 | }; |
| 1634 | |
| 1635 | /////////////////////////////////////////////////////////////////////////////// |
| 1636 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1637 | static int lsurface_width(lua_State* L) { |
| 1638 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width()); |
| 1639 | return 1; |
| 1640 | } |
| 1641 | |
| 1642 | static int lsurface_height(lua_State* L) { |
| 1643 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height()); |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
| 1647 | static int lsurface_getCanvas(lua_State* L) { |
| 1648 | SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1649 | if (nullptr == canvas) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1650 | lua_pushnil(L); |
| 1651 | } else { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1652 | push_ptr(L, canvas); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1653 | // 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 | |
| 1662 | static int lsurface_newImageSnapshot(lua_State* L) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1663 | sk_sp<SkImage> image = get_ref<SkSurface>(L, 1)->makeImageSnapshot(); |
| 1664 | if (!image) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1665 | lua_pushnil(L); |
| 1666 | } else { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1667 | push_ref(L, image); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1668 | } |
| 1669 | return 1; |
| 1670 | } |
| 1671 | |
| 1672 | static int lsurface_newSurface(lua_State* L) { |
| 1673 | int width = lua2int_def(L, 2, 0); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1674 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1675 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1676 | auto surface = get_ref<SkSurface>(L, 1)->makeSurface(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1677 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1678 | lua_pushnil(L); |
| 1679 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1680 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1681 | } |
| 1682 | return 1; |
| 1683 | } |
| 1684 | |
| 1685 | static int lsurface_gc(lua_State* L) { |
| 1686 | get_ref<SkSurface>(L, 1)->unref(); |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static 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 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1697 | { nullptr, nullptr } |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1698 | }; |
| 1699 | |
| 1700 | /////////////////////////////////////////////////////////////////////////////// |
| 1701 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1702 | static 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1711 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1712 | lua_pushnil(L); |
| 1713 | return 1; |
| 1714 | } |
| 1715 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1716 | push_ptr(L, canvas); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1717 | return 1; |
| 1718 | } |
| 1719 | |
| 1720 | static int lpicturerecorder_getCanvas(lua_State* L) { |
| 1721 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1722 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1723 | lua_pushnil(L); |
| 1724 | return 1; |
| 1725 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1726 | push_ptr(L, canvas); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1727 | return 1; |
| 1728 | } |
| 1729 | |
| 1730 | static int lpicturerecorder_endRecording(lua_State* L) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1731 | sk_sp<SkPicture> pic = get_obj<SkPictureRecorder>(L, 1)->finishRecordingAsPicture(); |
| 1732 | if (!pic) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1733 | lua_pushnil(L); |
| 1734 | return 1; |
| 1735 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1736 | push_ref(L, std::move(pic)); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1737 | return 1; |
| 1738 | } |
| 1739 | |
| 1740 | static int lpicturerecorder_gc(lua_State* L) { |
| 1741 | get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder(); |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | static const struct luaL_Reg gSkPictureRecorder_Methods[] = { |
| 1746 | { "beginRecording", lpicturerecorder_beginRecording }, |
| 1747 | { "getCanvas", lpicturerecorder_getCanvas }, |
| 1748 | { "endRecording", lpicturerecorder_endRecording }, |
| 1749 | { "__gc", lpicturerecorder_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1750 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1751 | }; |
| 1752 | |
| 1753 | /////////////////////////////////////////////////////////////////////////////// |
| 1754 | |
| 1755 | static int lpicture_width(lua_State* L) { |
| 1756 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width()); |
| 1757 | return 1; |
| 1758 | } |
| 1759 | |
| 1760 | static int lpicture_height(lua_State* L) { |
| 1761 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height()); |
| 1762 | return 1; |
| 1763 | } |
| 1764 | |
| 1765 | static int lpicture_gc(lua_State* L) { |
| 1766 | get_ref<SkPicture>(L, 1)->unref(); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | static const struct luaL_Reg gSkPicture_Methods[] = { |
| 1771 | { "width", lpicture_width }, |
| 1772 | { "height", lpicture_height }, |
| 1773 | { "__gc", lpicture_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1774 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1775 | }; |
| 1776 | |
| 1777 | /////////////////////////////////////////////////////////////////////////////// |
| 1778 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1779 | static int ltextblob_bounds(lua_State* L) { |
| 1780 | SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds()); |
| 1781 | return 1; |
| 1782 | } |
| 1783 | |
| 1784 | static int ltextblob_gc(lua_State* L) { |
| 1785 | SkSafeUnref(get_ref<SkTextBlob>(L, 1)); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | static const struct luaL_Reg gSkTextBlob_Methods[] = { |
| 1790 | { "bounds", ltextblob_bounds }, |
| 1791 | { "__gc", ltextblob_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1792 | { nullptr, nullptr } |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1793 | }; |
| 1794 | |
| 1795 | /////////////////////////////////////////////////////////////////////////////// |
| 1796 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1797 | static 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 | |
| 1804 | static int ltypeface_getStyle(lua_State* L) { |
Ben Wagner | 26308e1 | 2017-08-08 15:23:47 -0400 | [diff] [blame^] | 1805 | push_obj(L, get_ref<SkTypeface>(L, 1)->fontStyle()); |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1806 | return 1; |
| 1807 | } |
| 1808 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1809 | static int ltypeface_gc(lua_State* L) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 1810 | SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | static const struct luaL_Reg gSkTypeface_Methods[] = { |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1815 | { "getFamilyName", ltypeface_getFamilyName }, |
| 1816 | { "getStyle", ltypeface_getStyle }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1817 | { "__gc", ltypeface_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1818 | { nullptr, nullptr } |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1819 | }; |
| 1820 | |
| 1821 | /////////////////////////////////////////////////////////////////////////////// |
| 1822 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1823 | static int lfontstyle_weight(lua_State* L) { |
| 1824 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->weight()); |
| 1825 | return 1; |
| 1826 | } |
| 1827 | |
| 1828 | static int lfontstyle_width(lua_State* L) { |
| 1829 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->width()); |
| 1830 | return 1; |
| 1831 | } |
| 1832 | |
| 1833 | static int lfontstyle_slant(lua_State* L) { |
| 1834 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->slant()); |
| 1835 | return 1; |
| 1836 | } |
| 1837 | |
| 1838 | static int lfontstyle_gc(lua_State* L) { |
| 1839 | get_obj<SkFontStyle>(L, 1)->~SkFontStyle(); |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | static 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.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1853 | class AutoCallLua { |
| 1854 | public: |
| 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.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1861 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1862 | lua_newtable(L); |
| 1863 | setfield_string(L, "verb", verb); |
| 1864 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1865 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1866 | ~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.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1872 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1873 | private: |
| 1874 | lua_State* fL; |
| 1875 | }; |
| 1876 | |
| 1877 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 1878 | |
| 1879 | /////////////////////////////////////////////////////////////////////////////// |
| 1880 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1881 | static int lsk_newDocumentPDF(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1882 | const char* file = nullptr; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1883 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1884 | file = lua_tolstring(L, 1, nullptr); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 1887 | sk_sp<SkDocument> doc = SkDocument::MakePDF(file); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1888 | if (nullptr == doc) { |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1889 | // do I need to push a nil on the stack and return 1? |
| 1890 | return 0; |
| 1891 | } else { |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 1892 | push_ref(L, std::move(doc)); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1893 | return 1; |
| 1894 | } |
| 1895 | } |
| 1896 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1897 | static int lsk_newBlurImageFilter(lua_State* L) { |
| 1898 | SkScalar sigmaX = lua2scalar_def(L, 1, 0); |
| 1899 | SkScalar sigmaY = lua2scalar_def(L, 2, 0); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1900 | sk_sp<SkImageFilter> imf(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr)); |
| 1901 | if (!imf) { |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1902 | lua_pushnil(L); |
| 1903 | } else { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1904 | push_ref(L, std::move(imf)); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1905 | } |
| 1906 | return 1; |
| 1907 | } |
| 1908 | |
| 1909 | static 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 }; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1919 | sk_sp<SkShader> s(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 1920 | SkShader::kClamp_TileMode)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 1921 | if (!s) { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1922 | lua_pushnil(L); |
| 1923 | } else { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 1924 | push_ref(L, std::move(s)); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1925 | } |
| 1926 | return 1; |
| 1927 | } |
| 1928 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1929 | static int lsk_newMatrix(lua_State* L) { |
| 1930 | push_new<SkMatrix>(L)->reset(); |
| 1931 | return 1; |
| 1932 | } |
| 1933 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1934 | static int lsk_newPaint(lua_State* L) { |
| 1935 | push_new<SkPaint>(L); |
| 1936 | return 1; |
| 1937 | } |
| 1938 | |
| 1939 | static int lsk_newPath(lua_State* L) { |
| 1940 | push_new<SkPath>(L); |
| 1941 | return 1; |
| 1942 | } |
| 1943 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1944 | static int lsk_newPictureRecorder(lua_State* L) { |
| 1945 | push_new<SkPictureRecorder>(L); |
| 1946 | return 1; |
| 1947 | } |
| 1948 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1949 | static int lsk_newRRect(lua_State* L) { |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1950 | push_new<SkRRect>(L)->setEmpty(); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1951 | return 1; |
| 1952 | } |
| 1953 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1954 | #include "SkTextBox.h" |
| 1955 | // Sk.newTextBlob(text, rect, paint) |
| 1956 | static int lsk_newTextBlob(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1957 | const char* text = lua_tolstring(L, 1, nullptr); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1958 | 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; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 1968 | push_ref<SkTextBlob>(L, box.snapshotTextBlob(&newBottom)); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1969 | SkLua(L).pushScalar(newBottom); |
| 1970 | return 2; |
| 1971 | } |
| 1972 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1973 | static int lsk_newTypeface(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1974 | const char* name = nullptr; |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1975 | SkFontStyle style; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 1976 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1977 | int count = lua_gettop(L); |
| 1978 | if (count > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1979 | name = lua_tolstring(L, 1, nullptr); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1980 | if (count > 1) { |
| 1981 | SkFontStyle* passedStyle = get_obj<SkFontStyle>(L, 2); |
| 1982 | if (passedStyle) { |
| 1983 | style = *passedStyle; |
| 1984 | } |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1988 | sk_sp<SkTypeface> face(SkTypeface::MakeFromName(name, style)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1989 | // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1990 | if (nullptr == face) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 1991 | face = SkTypeface::MakeDefault(); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1992 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 1993 | push_ref(L, std::move(face)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1994 | return 1; |
| 1995 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1996 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1997 | static 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 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2015 | static int lsk_newRasterSurface(lua_State* L) { |
reed | 7b86466 | 2014-11-04 13:24:47 -0800 | [diff] [blame] | 2016 | int width = lua2int_def(L, 1, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2017 | int height = lua2int_def(L, 2, 0); |
| 2018 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 2019 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2020 | auto surface = SkSurface::MakeRaster(info, &props); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2021 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2022 | lua_pushnil(L); |
| 2023 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2024 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2025 | } |
| 2026 | return 1; |
| 2027 | } |
| 2028 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2029 | static int lsk_loadImage(lua_State* L) { |
| 2030 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2031 | const char* name = lua_tolstring(L, 1, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2032 | sk_sp<SkData> data(SkData::MakeFromFileName(name)); |
| 2033 | if (data) { |
| 2034 | auto image = SkImage::MakeFromEncoded(std::move(data)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2035 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2036 | push_ref(L, std::move(image)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2037 | return 1; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | return 0; |
| 2042 | } |
| 2043 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2044 | static 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.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2050 | setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2051 | setfield_function(L, "loadImage", lsk_loadImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2052 | setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2053 | setfield_function(L, "newLinearGradient", lsk_newLinearGradient); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2054 | setfield_function(L, "newMatrix", lsk_newMatrix); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2055 | setfield_function(L, "newPaint", lsk_newPaint); |
| 2056 | setfield_function(L, "newPath", lsk_newPath); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2057 | setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2058 | setfield_function(L, "newRRect", lsk_newRRect); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2059 | setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2060 | setfield_function(L, "newTextBlob", lsk_newTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2061 | setfield_function(L, "newTypeface", lsk_newTypeface); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 2062 | setfield_function(L, "newFontStyle", lsk_newFontStyle); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2063 | lua_pop(L, 1); // pop off the Sk table |
| 2064 | } |
| 2065 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2066 | #define REG_CLASS(L, C) \ |
| 2067 | do { \ |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2068 | luaL_newmetatable(L, get_mtname<C>()); \ |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2069 | 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 | |
| 2075 | void SkLua::Load(lua_State* L) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2076 | register_Sk(L); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2077 | REG_CLASS(L, SkCanvas); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 2078 | REG_CLASS(L, SkColorFilter); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2079 | REG_CLASS(L, SkDocument); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2080 | REG_CLASS(L, SkImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2081 | REG_CLASS(L, SkImageFilter); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2082 | REG_CLASS(L, SkMatrix); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2083 | REG_CLASS(L, SkPaint); |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 2084 | REG_CLASS(L, SkPath); |
| 2085 | REG_CLASS(L, SkPathEffect); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2086 | REG_CLASS(L, SkPicture); |
| 2087 | REG_CLASS(L, SkPictureRecorder); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2088 | REG_CLASS(L, SkRRect); |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 2089 | REG_CLASS(L, SkShader); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2090 | REG_CLASS(L, SkSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2091 | REG_CLASS(L, SkTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2092 | REG_CLASS(L, SkTypeface); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 2093 | REG_CLASS(L, SkFontStyle); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2094 | } |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2095 | |
reed@google.com | 7bce998 | 2013-06-20 17:40:21 +0000 | [diff] [blame] | 2096 | extern "C" int luaopen_skia(lua_State* L); |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2097 | extern "C" int luaopen_skia(lua_State* L) { |
| 2098 | SkLua::Load(L); |
| 2099 | return 0; |
| 2100 | } |