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 |
| 11 | #include "GrReducedClip.h" |
| 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" |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 19 | #include "SkGradientShader.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 20 | #include "SkImage.h" |
| 21 | #include "SkMatrix.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 22 | #include "SkPaint.h" |
| 23 | #include "SkPath.h" |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 24 | #include "SkPictureRecorder.h" |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 25 | #include "SkPixelRef.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 26 | #include "SkRRect.h" |
| 27 | #include "SkString.h" |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 28 | #include "SkSurface.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 29 | #include "SkTextBlob.h" |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 30 | #include "SkTypeface.h" |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 31 | #include "SkXfermode.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) |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 62 | DEF_MTNAME(SkXfermode) |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 63 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 64 | template <typename T> T* push_new(lua_State* L) { |
| 65 | T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
| 66 | new (addr) T; |
| 67 | luaL_getmetatable(L, get_mtname<T>()); |
| 68 | lua_setmetatable(L, -2); |
| 69 | return addr; |
| 70 | } |
reed@google.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 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 78 | 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] | 79 | *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 80 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 81 | lua_setmetatable(L, -2); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 82 | return ref; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 83 | } |
| 84 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 85 | template <typename T> void push_ref(lua_State* L, sk_sp<T> sp) { |
| 86 | *(T**)lua_newuserdata(L, sizeof(T*)) = sp.release(); |
| 87 | luaL_getmetatable(L, get_mtname<T>()); |
| 88 | lua_setmetatable(L, -2); |
| 89 | } |
| 90 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 91 | template <typename T> T* get_ref(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 92 | return *(T**)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | template <typename T> T* get_obj(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 96 | return (T*)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 99 | static bool lua2bool(lua_State* L, int index) { |
| 100 | return !!lua_toboolean(L, index); |
| 101 | } |
| 102 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 105 | SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) { |
| 106 | fL = luaL_newstate(); |
| 107 | luaL_openlibs(fL); |
| 108 | SkLua::Load(fL); |
| 109 | } |
| 110 | |
| 111 | SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {} |
| 112 | |
| 113 | SkLua::~SkLua() { |
| 114 | if (fWeOwnL) { |
| 115 | if (fTermCode.size() > 0) { |
| 116 | lua_getglobal(fL, fTermCode.c_str()); |
| 117 | if (lua_pcall(fL, 0, 0, 0) != LUA_OK) { |
| 118 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 119 | } |
| 120 | } |
| 121 | lua_close(fL); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool SkLua::runCode(const char code[]) { |
| 126 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 127 | if (err) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 128 | SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1)); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool SkLua::runCode(const void* code, size_t size) { |
| 135 | SkString str((const char*)code, size); |
| 136 | return this->runCode(str.c_str()); |
| 137 | } |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0) |
| 142 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 143 | static void setfield_bool_if(lua_State* L, const char key[], bool pred) { |
| 144 | if (pred) { |
| 145 | lua_pushboolean(L, true); |
| 146 | lua_setfield(L, -2, key); |
| 147 | } |
| 148 | } |
| 149 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 150 | static void setfield_string(lua_State* L, const char key[], const char value[]) { |
| 151 | lua_pushstring(L, value); |
| 152 | lua_setfield(L, -2, key); |
| 153 | } |
| 154 | |
| 155 | static void setfield_number(lua_State* L, const char key[], double value) { |
| 156 | lua_pushnumber(L, value); |
| 157 | lua_setfield(L, -2, key); |
| 158 | } |
| 159 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 160 | static void setfield_boolean(lua_State* L, const char key[], bool value) { |
| 161 | lua_pushboolean(L, value); |
| 162 | lua_setfield(L, -2, key); |
| 163 | } |
| 164 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 165 | static void setfield_scalar(lua_State* L, const char key[], SkScalar value) { |
| 166 | setfield_number(L, key, SkScalarToLua(value)); |
| 167 | } |
| 168 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 169 | static void setfield_function(lua_State* L, |
| 170 | const char key[], lua_CFunction value) { |
| 171 | lua_pushcfunction(L, value); |
| 172 | lua_setfield(L, -2, key); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 173 | } |
| 174 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 175 | static int lua2int_def(lua_State* L, int index, int defaultValue) { |
| 176 | if (lua_isnumber(L, index)) { |
| 177 | return (int)lua_tonumber(L, index); |
| 178 | } else { |
| 179 | return defaultValue; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static SkScalar lua2scalar(lua_State* L, int index) { |
| 184 | SkASSERT(lua_isnumber(L, index)); |
| 185 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 186 | } |
| 187 | |
| 188 | static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) { |
| 189 | if (lua_isnumber(L, index)) { |
| 190 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 191 | } else { |
| 192 | return defaultValue; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) { |
| 197 | SkASSERT(lua_istable(L, stackIndex)); |
| 198 | lua_rawgeti(L, stackIndex, arrayIndex); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 199 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 200 | SkScalar value = lua2scalar(L, -1); |
| 201 | lua_pop(L, 1); |
| 202 | return value; |
| 203 | } |
| 204 | |
| 205 | static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) { |
| 206 | for (int i = 0; i < count; ++i) { |
| 207 | dst[i] = getarray_scalar(L, stackIndex, i + 1); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) { |
| 212 | getarray_scalars(L, stackIndex, &pts[0].fX, count * 2); |
| 213 | } |
| 214 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 215 | static void setarray_number(lua_State* L, int index, double value) { |
| 216 | lua_pushnumber(L, value); |
| 217 | lua_rawseti(L, -2, index); |
| 218 | } |
| 219 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 220 | static void setarray_scalar(lua_State* L, int index, SkScalar value) { |
| 221 | setarray_number(L, index, SkScalarToLua(value)); |
| 222 | } |
| 223 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 224 | void SkLua::pushBool(bool value, const char key[]) { |
| 225 | lua_pushboolean(fL, value); |
| 226 | CHECK_SETFIELD(key); |
| 227 | } |
| 228 | |
| 229 | void SkLua::pushString(const char str[], const char key[]) { |
| 230 | lua_pushstring(fL, str); |
| 231 | CHECK_SETFIELD(key); |
| 232 | } |
| 233 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 234 | void SkLua::pushString(const char str[], size_t length, const char key[]) { |
| 235 | // TODO: how to do this w/o making a copy? |
| 236 | SkString s(str, length); |
| 237 | lua_pushstring(fL, s.c_str()); |
| 238 | CHECK_SETFIELD(key); |
| 239 | } |
| 240 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 241 | void SkLua::pushString(const SkString& str, const char key[]) { |
| 242 | lua_pushstring(fL, str.c_str()); |
| 243 | CHECK_SETFIELD(key); |
| 244 | } |
| 245 | |
| 246 | void SkLua::pushColor(SkColor color, const char key[]) { |
| 247 | lua_newtable(fL); |
| 248 | setfield_number(fL, "a", SkColorGetA(color) / 255.0); |
| 249 | setfield_number(fL, "r", SkColorGetR(color) / 255.0); |
| 250 | setfield_number(fL, "g", SkColorGetG(color) / 255.0); |
| 251 | setfield_number(fL, "b", SkColorGetB(color) / 255.0); |
| 252 | CHECK_SETFIELD(key); |
| 253 | } |
| 254 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 255 | void SkLua::pushU32(uint32_t value, const char key[]) { |
| 256 | lua_pushnumber(fL, (double)value); |
| 257 | CHECK_SETFIELD(key); |
| 258 | } |
| 259 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 260 | void SkLua::pushScalar(SkScalar value, const char key[]) { |
| 261 | lua_pushnumber(fL, SkScalarToLua(value)); |
| 262 | CHECK_SETFIELD(key); |
| 263 | } |
| 264 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 265 | void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { |
| 266 | lua_newtable(fL); |
| 267 | for (int i = 0; i < count; ++i) { |
| 268 | // make it base-1 to match lua convention |
| 269 | setarray_number(fL, i + 1, (double)array[i]); |
| 270 | } |
| 271 | CHECK_SETFIELD(key); |
| 272 | } |
| 273 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 274 | void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) { |
| 275 | lua_newtable(fL); |
| 276 | for (int i = 0; i < count; ++i) { |
| 277 | // make it base-1 to match lua convention |
| 278 | lua_newtable(fL); |
| 279 | this->pushScalar(array[i].fX, "x"); |
| 280 | this->pushScalar(array[i].fY, "y"); |
| 281 | lua_rawseti(fL, -2, i + 1); |
| 282 | } |
| 283 | CHECK_SETFIELD(key); |
| 284 | } |
| 285 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 286 | void SkLua::pushArrayScalar(const SkScalar 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 | setarray_scalar(fL, i + 1, array[i]); |
| 291 | } |
| 292 | CHECK_SETFIELD(key); |
| 293 | } |
| 294 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 295 | void SkLua::pushRect(const SkRect& r, const char key[]) { |
| 296 | lua_newtable(fL); |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 297 | setfield_scalar(fL, "left", r.fLeft); |
| 298 | setfield_scalar(fL, "top", r.fTop); |
| 299 | setfield_scalar(fL, "right", r.fRight); |
| 300 | setfield_scalar(fL, "bottom", r.fBottom); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 301 | CHECK_SETFIELD(key); |
| 302 | } |
| 303 | |
| 304 | void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
| 305 | push_obj(fL, rr); |
| 306 | CHECK_SETFIELD(key); |
| 307 | } |
| 308 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 309 | void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) { |
| 310 | lua_newtable(fL); |
| 311 | setfield_scalar(fL, "phase", info.fPhase); |
| 312 | this->pushArrayScalar(info.fIntervals, info.fCount, "intervals"); |
| 313 | CHECK_SETFIELD(key); |
| 314 | } |
| 315 | |
| 316 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 317 | void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) { |
| 318 | push_obj(fL, matrix); |
| 319 | CHECK_SETFIELD(key); |
| 320 | } |
| 321 | |
| 322 | void SkLua::pushPaint(const SkPaint& paint, const char key[]) { |
| 323 | push_obj(fL, paint); |
| 324 | CHECK_SETFIELD(key); |
| 325 | } |
| 326 | |
| 327 | void SkLua::pushPath(const SkPath& path, const char key[]) { |
| 328 | push_obj(fL, path); |
| 329 | CHECK_SETFIELD(key); |
| 330 | } |
| 331 | |
| 332 | void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) { |
| 333 | push_ref(fL, canvas); |
| 334 | CHECK_SETFIELD(key); |
| 335 | } |
| 336 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 337 | void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) { |
| 338 | push_ref(fL, const_cast<SkTextBlob*>(blob)); |
| 339 | CHECK_SETFIELD(key); |
| 340 | } |
| 341 | |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 342 | static const char* element_type(SkClipStack::Element::Type type) { |
| 343 | switch (type) { |
| 344 | case SkClipStack::Element::kEmpty_Type: |
| 345 | return "empty"; |
| 346 | case SkClipStack::Element::kRect_Type: |
| 347 | return "rect"; |
| 348 | case SkClipStack::Element::kRRect_Type: |
| 349 | return "rrect"; |
| 350 | case SkClipStack::Element::kPath_Type: |
| 351 | return "path"; |
| 352 | } |
| 353 | return "unknown"; |
| 354 | } |
| 355 | |
| 356 | static const char* region_op(SkRegion::Op op) { |
| 357 | switch (op) { |
| 358 | case SkRegion::kDifference_Op: |
| 359 | return "difference"; |
| 360 | case SkRegion::kIntersect_Op: |
| 361 | return "intersect"; |
| 362 | case SkRegion::kUnion_Op: |
| 363 | return "union"; |
| 364 | case SkRegion::kXOR_Op: |
| 365 | return "xor"; |
| 366 | case SkRegion::kReverseDifference_Op: |
| 367 | return "reverse-difference"; |
| 368 | case SkRegion::kReplace_Op: |
| 369 | return "replace"; |
| 370 | } |
| 371 | return "unknown"; |
| 372 | } |
| 373 | |
| 374 | void SkLua::pushClipStack(const SkClipStack& stack, const char* key) { |
| 375 | lua_newtable(fL); |
| 376 | SkClipStack::B2TIter iter(stack); |
| 377 | const SkClipStack::Element* element; |
| 378 | int i = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 379 | while ((element = iter.next())) { |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 380 | this->pushClipStackElement(*element); |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 381 | lua_rawseti(fL, -2, ++i); |
| 382 | } |
| 383 | CHECK_SETFIELD(key); |
| 384 | } |
| 385 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 386 | void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) { |
| 387 | lua_newtable(fL); |
| 388 | SkClipStack::Element::Type type = element.getType(); |
| 389 | this->pushString(element_type(type), "type"); |
| 390 | switch (type) { |
| 391 | case SkClipStack::Element::kEmpty_Type: |
| 392 | break; |
| 393 | case SkClipStack::Element::kRect_Type: |
| 394 | this->pushRect(element.getRect(), "rect"); |
| 395 | break; |
| 396 | case SkClipStack::Element::kRRect_Type: |
| 397 | this->pushRRect(element.getRRect(), "rrect"); |
| 398 | break; |
| 399 | case SkClipStack::Element::kPath_Type: |
| 400 | this->pushPath(element.getPath(), "path"); |
| 401 | break; |
| 402 | } |
| 403 | this->pushString(region_op(element.getOp()), "op"); |
| 404 | this->pushBool(element.isAA(), "aa"); |
| 405 | CHECK_SETFIELD(key); |
| 406 | } |
| 407 | |
| 408 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 409 | /////////////////////////////////////////////////////////////////////////////// |
| 410 | /////////////////////////////////////////////////////////////////////////////// |
| 411 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 412 | static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) { |
| 413 | SkASSERT(lua_istable(L, index)); |
| 414 | lua_pushstring(L, key); |
| 415 | lua_gettable(L, index); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 416 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 417 | SkScalar value = lua2scalar(L, -1); |
| 418 | lua_pop(L, 1); |
| 419 | return value; |
| 420 | } |
| 421 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 422 | static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) { |
| 423 | SkASSERT(lua_istable(L, index)); |
| 424 | lua_pushstring(L, key); |
| 425 | lua_gettable(L, index); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 426 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 427 | SkScalar value; |
| 428 | if (lua_isnil(L, -1)) { |
| 429 | value = def; |
| 430 | } else { |
| 431 | value = lua2scalar(L, -1); |
| 432 | } |
| 433 | lua_pop(L, 1); |
| 434 | return value; |
| 435 | } |
| 436 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 437 | static SkScalar byte2unit(U8CPU byte) { |
| 438 | return byte / 255.0f; |
| 439 | } |
| 440 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 441 | static U8CPU unit2byte(SkScalar x) { |
| 442 | if (x <= 0) { |
| 443 | return 0; |
| 444 | } else if (x >= 1) { |
| 445 | return 255; |
| 446 | } else { |
| 447 | return SkScalarRoundToInt(x * 255); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static SkColor lua2color(lua_State* L, int index) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 452 | return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)), |
| 453 | unit2byte(getfield_scalar_default(L, index, "r", 0)), |
| 454 | unit2byte(getfield_scalar_default(L, index, "g", 0)), |
| 455 | unit2byte(getfield_scalar_default(L, index, "b", 0))); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 459 | rect->set(getfield_scalar_default(L, index, "left", 0), |
| 460 | getfield_scalar_default(L, index, "top", 0), |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 461 | getfield_scalar(L, index, "right"), |
| 462 | getfield_scalar(L, index, "bottom")); |
| 463 | return rect; |
| 464 | } |
| 465 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 466 | static int lcanvas_clear(lua_State* L) { |
| 467 | get_ref<SkCanvas>(L, 1)->clear(0); |
| 468 | return 0; |
| 469 | } |
| 470 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 471 | static int lcanvas_drawColor(lua_State* L) { |
| 472 | get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2)); |
| 473 | return 0; |
| 474 | } |
| 475 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 476 | static int lcanvas_drawPaint(lua_State* L) { |
| 477 | get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2)); |
| 478 | return 0; |
| 479 | } |
| 480 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 481 | static int lcanvas_drawRect(lua_State* L) { |
| 482 | SkRect rect; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 483 | lua2rect(L, 2, &rect); |
| 484 | const SkPaint* paint = get_obj<SkPaint>(L, 3); |
| 485 | get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int lcanvas_drawOval(lua_State* L) { |
| 490 | SkRect rect; |
| 491 | get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect), |
| 492 | *get_obj<SkPaint>(L, 3)); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int lcanvas_drawCircle(lua_State* L) { |
| 497 | get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
| 498 | lua2scalar(L, 3), |
| 499 | lua2scalar(L, 4), |
| 500 | *get_obj<SkPaint>(L, 5)); |
| 501 | return 0; |
| 502 | } |
| 503 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 504 | static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) { |
| 505 | if (lua_isnumber(L, index)) { |
| 506 | paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255)); |
| 507 | return paint; |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 508 | } else if (lua_isuserdata(L, index)) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 509 | const SkPaint* ptr = get_obj<SkPaint>(L, index); |
| 510 | if (ptr) { |
| 511 | *paint = *ptr; |
| 512 | return paint; |
| 513 | } |
| 514 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 515 | return nullptr; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 516 | } |
| 517 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 518 | static int lcanvas_drawImage(lua_State* L) { |
| 519 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 520 | SkImage* image = get_ref<SkImage>(L, 2); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 521 | if (nullptr == image) { |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | SkScalar x = lua2scalar(L, 3); |
| 525 | SkScalar y = lua2scalar(L, 4); |
| 526 | |
| 527 | SkPaint paint; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 528 | canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 532 | static int lcanvas_drawImageRect(lua_State* L) { |
| 533 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 534 | SkImage* image = get_ref<SkImage>(L, 2); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 535 | if (nullptr == image) { |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | SkRect srcR, dstR; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 540 | SkRect* srcRPtr = nullptr; |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 541 | if (!lua_isnil(L, 3)) { |
| 542 | srcRPtr = lua2rect(L, 3, &srcR); |
| 543 | } |
| 544 | lua2rect(L, 4, &dstR); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 545 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 546 | SkPaint paint; |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 547 | canvas->legacy_drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint)); |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 551 | static int lcanvas_drawPatch(lua_State* L) { |
| 552 | SkPoint cubics[12]; |
| 553 | SkColor colorStorage[4]; |
| 554 | SkPoint texStorage[4]; |
| 555 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 556 | const SkColor* colors = nullptr; |
| 557 | const SkPoint* texs = nullptr; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 558 | |
| 559 | getarray_points(L, 2, cubics, 12); |
| 560 | |
| 561 | colorStorage[0] = SK_ColorRED; |
| 562 | colorStorage[1] = SK_ColorGREEN; |
| 563 | colorStorage[2] = SK_ColorBLUE; |
| 564 | colorStorage[3] = SK_ColorGRAY; |
| 565 | |
| 566 | if (lua_isnil(L, 4)) { |
| 567 | colors = colorStorage; |
| 568 | } else { |
| 569 | getarray_points(L, 4, texStorage, 4); |
| 570 | texs = texStorage; |
| 571 | } |
| 572 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 573 | get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, nullptr, *get_obj<SkPaint>(L, 5)); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 577 | static int lcanvas_drawPath(lua_State* L) { |
| 578 | get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 579 | *get_obj<SkPaint>(L, 3)); |
| 580 | return 0; |
| 581 | } |
| 582 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 583 | // drawPicture(pic, x, y, paint) |
| 584 | static int lcanvas_drawPicture(lua_State* L) { |
| 585 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 586 | SkPicture* picture = get_ref<SkPicture>(L, 2); |
| 587 | SkScalar x = lua2scalar_def(L, 3, 0); |
| 588 | SkScalar y = lua2scalar_def(L, 4, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 589 | SkMatrix matrix, *matrixPtr = nullptr; |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 590 | if (x || y) { |
| 591 | matrix.setTranslate(x, y); |
| 592 | matrixPtr = &matrix; |
| 593 | } |
| 594 | SkPaint paint; |
| 595 | canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint)); |
| 596 | return 0; |
| 597 | } |
| 598 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 599 | static int lcanvas_drawText(lua_State* L) { |
| 600 | if (lua_gettop(L) < 5) { |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) { |
| 605 | size_t len; |
| 606 | const char* text = lua_tolstring(L, 2, &len); |
| 607 | get_ref<SkCanvas>(L, 1)->drawText(text, len, |
| 608 | lua2scalar(L, 3), lua2scalar(L, 4), |
| 609 | *get_obj<SkPaint>(L, 5)); |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 614 | static int lcanvas_drawTextBlob(lua_State* L) { |
| 615 | const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2); |
| 616 | SkScalar x = lua2scalar(L, 3); |
| 617 | SkScalar y = lua2scalar(L, 4); |
| 618 | const SkPaint& paint = *get_obj<SkPaint>(L, 5); |
| 619 | get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint); |
| 620 | return 0; |
| 621 | } |
| 622 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 623 | static int lcanvas_getSaveCount(lua_State* L) { |
| 624 | lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | static int lcanvas_getTotalMatrix(lua_State* L) { |
| 629 | SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
| 630 | return 1; |
| 631 | } |
| 632 | |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 633 | static int lcanvas_getClipStack(lua_State* L) { |
| 634 | SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack()); |
| 635 | return 1; |
| 636 | } |
| 637 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 638 | int SkLua::lcanvas_getReducedClipStack(lua_State* L) { |
| 639 | #if SK_SUPPORT_GPU |
| 640 | const SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 641 | SkIRect queryBounds = canvas->getTopLayerBounds(); |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 642 | |
| 643 | GrReducedClip::ElementList elements; |
| 644 | GrReducedClip::InitialState initialState; |
| 645 | int32_t genID; |
| 646 | SkIRect resultBounds; |
| 647 | |
| 648 | const SkClipStack& stack = *canvas->getClipStack(); |
| 649 | |
| 650 | GrReducedClip::ReduceClipStack(stack, |
| 651 | queryBounds, |
| 652 | &elements, |
| 653 | &genID, |
| 654 | &initialState, |
| 655 | &resultBounds, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 656 | nullptr); |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 657 | |
| 658 | GrReducedClip::ElementList::Iter iter(elements); |
| 659 | int i = 0; |
| 660 | lua_newtable(L); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 661 | while(iter.get()) { |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 662 | SkLua(L).pushClipStackElement(*iter.get()); |
| 663 | iter.next(); |
| 664 | lua_rawseti(L, -2, ++i); |
| 665 | } |
| 666 | // Currently this only returns the element list to lua, not the initial state or result bounds. |
| 667 | // It could return these as additional items on the lua stack. |
| 668 | return 1; |
| 669 | #else |
| 670 | return 0; |
| 671 | #endif |
| 672 | } |
| 673 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 674 | static int lcanvas_save(lua_State* L) { |
| 675 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save()); |
| 676 | return 1; |
| 677 | } |
| 678 | |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 679 | static int lcanvas_saveLayer(lua_State* L) { |
| 680 | SkPaint paint; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 681 | 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] | 682 | return 1; |
| 683 | } |
| 684 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 685 | static int lcanvas_restore(lua_State* L) { |
| 686 | get_ref<SkCanvas>(L, 1)->restore(); |
| 687 | return 0; |
| 688 | } |
| 689 | |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 690 | static int lcanvas_scale(lua_State* L) { |
| 691 | SkScalar sx = lua2scalar_def(L, 2, 1); |
| 692 | SkScalar sy = lua2scalar_def(L, 3, sx); |
| 693 | get_ref<SkCanvas>(L, 1)->scale(sx, sy); |
| 694 | return 0; |
| 695 | } |
| 696 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 697 | static int lcanvas_translate(lua_State* L) { |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 698 | SkScalar tx = lua2scalar_def(L, 2, 0); |
| 699 | SkScalar ty = lua2scalar_def(L, 3, 0); |
| 700 | get_ref<SkCanvas>(L, 1)->translate(tx, ty); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int lcanvas_rotate(lua_State* L) { |
| 705 | SkScalar degrees = lua2scalar_def(L, 2, 0); |
| 706 | get_ref<SkCanvas>(L, 1)->rotate(degrees); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 710 | static int lcanvas_concat(lua_State* L) { |
| 711 | get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2)); |
| 712 | return 0; |
| 713 | } |
| 714 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 715 | static int lcanvas_newSurface(lua_State* L) { |
| 716 | int width = lua2int_def(L, 2, 0); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 717 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 718 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 719 | auto surface = get_ref<SkCanvas>(L, 1)->makeSurface(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 720 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 721 | lua_pushnil(L); |
| 722 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 723 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 724 | } |
| 725 | return 1; |
| 726 | } |
| 727 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 728 | static int lcanvas_gc(lua_State* L) { |
| 729 | get_ref<SkCanvas>(L, 1)->unref(); |
| 730 | return 0; |
| 731 | } |
| 732 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 733 | const struct luaL_Reg gSkCanvas_Methods[] = { |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 734 | { "clear", lcanvas_clear }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 735 | { "drawColor", lcanvas_drawColor }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 736 | { "drawPaint", lcanvas_drawPaint }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 737 | { "drawRect", lcanvas_drawRect }, |
| 738 | { "drawOval", lcanvas_drawOval }, |
| 739 | { "drawCircle", lcanvas_drawCircle }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 740 | { "drawImage", lcanvas_drawImage }, |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 741 | { "drawImageRect", lcanvas_drawImageRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 742 | { "drawPatch", lcanvas_drawPatch }, |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 743 | { "drawPath", lcanvas_drawPath }, |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 744 | { "drawPicture", lcanvas_drawPicture }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 745 | { "drawText", lcanvas_drawText }, |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 746 | { "drawTextBlob", lcanvas_drawTextBlob }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 747 | { "getSaveCount", lcanvas_getSaveCount }, |
| 748 | { "getTotalMatrix", lcanvas_getTotalMatrix }, |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 749 | { "getClipStack", lcanvas_getClipStack }, |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 750 | #if SK_SUPPORT_GPU |
| 751 | { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, |
| 752 | #endif |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 753 | { "save", lcanvas_save }, |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 754 | { "saveLayer", lcanvas_saveLayer }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 755 | { "restore", lcanvas_restore }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 756 | { "scale", lcanvas_scale }, |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 757 | { "translate", lcanvas_translate }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 758 | { "rotate", lcanvas_rotate }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 759 | { "concat", lcanvas_concat }, |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 760 | |
| 761 | { "newSurface", lcanvas_newSurface }, |
| 762 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 763 | { "__gc", lcanvas_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 764 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | /////////////////////////////////////////////////////////////////////////////// |
| 768 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 769 | static int ldocument_beginPage(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 770 | const SkRect* contentPtr = nullptr; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 771 | push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), |
| 772 | lua2scalar(L, 3), |
| 773 | contentPtr)); |
| 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | static int ldocument_endPage(lua_State* L) { |
| 778 | get_ref<SkDocument>(L, 1)->endPage(); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int ldocument_close(lua_State* L) { |
| 783 | get_ref<SkDocument>(L, 1)->close(); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static int ldocument_gc(lua_State* L) { |
| 788 | get_ref<SkDocument>(L, 1)->unref(); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | static const struct luaL_Reg gSkDocument_Methods[] = { |
| 793 | { "beginPage", ldocument_beginPage }, |
| 794 | { "endPage", ldocument_endPage }, |
| 795 | { "close", ldocument_close }, |
| 796 | { "__gc", ldocument_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 797 | { nullptr, nullptr } |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 798 | }; |
| 799 | |
| 800 | /////////////////////////////////////////////////////////////////////////////// |
| 801 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 802 | static int lpaint_isAntiAlias(lua_State* L) { |
| 803 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias()); |
| 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | static int lpaint_setAntiAlias(lua_State* L) { |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 808 | get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 812 | static int lpaint_isDither(lua_State* L) { |
| 813 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither()); |
| 814 | return 1; |
| 815 | } |
| 816 | |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 817 | static int lpaint_setDither(lua_State* L) { |
| 818 | get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2)); |
| 819 | return 0; |
| 820 | } |
| 821 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 822 | static int lpaint_isUnderlineText(lua_State* L) { |
| 823 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText()); |
| 824 | return 1; |
| 825 | } |
| 826 | |
| 827 | static int lpaint_isStrikeThruText(lua_State* L) { |
| 828 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText()); |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | static int lpaint_isFakeBoldText(lua_State* L) { |
| 833 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText()); |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | static int lpaint_isLinearText(lua_State* L) { |
| 838 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText()); |
| 839 | return 1; |
| 840 | } |
| 841 | |
| 842 | static int lpaint_isSubpixelText(lua_State* L) { |
| 843 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText()); |
| 844 | return 1; |
| 845 | } |
| 846 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 847 | static int lpaint_setSubpixelText(lua_State* L) { |
| 848 | get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2)); |
| 849 | return 1; |
| 850 | } |
| 851 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 852 | static int lpaint_isDevKernText(lua_State* L) { |
| 853 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText()); |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | static int lpaint_isLCDRenderText(lua_State* L) { |
| 858 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText()); |
| 859 | return 1; |
| 860 | } |
| 861 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 862 | static int lpaint_setLCDRenderText(lua_State* L) { |
| 863 | get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2)); |
| 864 | return 1; |
| 865 | } |
| 866 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 867 | static int lpaint_isEmbeddedBitmapText(lua_State* L) { |
| 868 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText()); |
| 869 | return 1; |
| 870 | } |
| 871 | |
| 872 | static int lpaint_isAutohinted(lua_State* L) { |
| 873 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted()); |
| 874 | return 1; |
| 875 | } |
| 876 | |
| 877 | static int lpaint_isVerticalText(lua_State* L) { |
| 878 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText()); |
| 879 | return 1; |
| 880 | } |
| 881 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 882 | static int lpaint_getAlpha(lua_State* L) { |
| 883 | SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha())); |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | static int lpaint_setAlpha(lua_State* L) { |
| 888 | get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2))); |
| 889 | return 0; |
| 890 | } |
| 891 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 892 | static int lpaint_getColor(lua_State* L) { |
| 893 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 894 | return 1; |
| 895 | } |
| 896 | |
| 897 | static int lpaint_setColor(lua_State* L) { |
| 898 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 899 | return 0; |
| 900 | } |
| 901 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 902 | static int lpaint_getTextSize(lua_State* L) { |
| 903 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 904 | return 1; |
| 905 | } |
| 906 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 907 | static int lpaint_getTextScaleX(lua_State* L) { |
| 908 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX()); |
| 909 | return 1; |
| 910 | } |
| 911 | |
| 912 | static int lpaint_getTextSkewX(lua_State* L) { |
| 913 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX()); |
| 914 | return 1; |
| 915 | } |
| 916 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 917 | static int lpaint_setTextSize(lua_State* L) { |
| 918 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 919 | return 0; |
| 920 | } |
| 921 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 922 | static int lpaint_getTypeface(lua_State* L) { |
| 923 | push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); |
| 924 | return 1; |
| 925 | } |
| 926 | |
| 927 | static int lpaint_setTypeface(lua_State* L) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 928 | 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] | 929 | return 0; |
| 930 | } |
| 931 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 932 | static int lpaint_getHinting(lua_State* L) { |
| 933 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting()); |
| 934 | return 1; |
| 935 | } |
| 936 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 937 | static int lpaint_getFilterQuality(lua_State* L) { |
| 938 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality()); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 939 | return 1; |
| 940 | } |
| 941 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 942 | static int lpaint_setFilterQuality(lua_State* L) { |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 943 | int level = lua2int_def(L, 2, -1); |
| 944 | if (level >= 0 && level <= 3) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 945 | get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 946 | } |
| 947 | return 0; |
| 948 | } |
| 949 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 950 | static int lpaint_getFontID(lua_State* L) { |
| 951 | SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface(); |
| 952 | SkLua(L).pushU32(SkTypeface::UniqueID(face)); |
| 953 | return 1; |
| 954 | } |
| 955 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 956 | static const struct { |
| 957 | const char* fLabel; |
| 958 | SkPaint::Align fAlign; |
| 959 | } gAlignRec[] = { |
| 960 | { "left", SkPaint::kLeft_Align }, |
| 961 | { "center", SkPaint::kCenter_Align }, |
| 962 | { "right", SkPaint::kRight_Align }, |
| 963 | }; |
| 964 | |
| 965 | static int lpaint_getTextAlign(lua_State* L) { |
| 966 | SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign(); |
| 967 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 968 | if (gAlignRec[i].fAlign == align) { |
| 969 | lua_pushstring(L, gAlignRec[i].fLabel); |
| 970 | return 1; |
| 971 | } |
| 972 | } |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static int lpaint_setTextAlign(lua_State* L) { |
| 977 | if (lua_isstring(L, 2)) { |
| 978 | size_t len; |
| 979 | const char* label = lua_tolstring(L, 2, &len); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 980 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 981 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 982 | if (!strcmp(gAlignRec[i].fLabel, label)) { |
| 983 | get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign); |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | return 0; |
| 989 | } |
| 990 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 991 | static int lpaint_getStroke(lua_State* L) { |
| 992 | lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle()); |
| 993 | return 1; |
| 994 | } |
| 995 | |
| 996 | static int lpaint_setStroke(lua_State* L) { |
| 997 | SkPaint::Style style; |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 998 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 999 | if (lua_toboolean(L, 2)) { |
| 1000 | style = SkPaint::kStroke_Style; |
| 1001 | } else { |
| 1002 | style = SkPaint::kFill_Style; |
| 1003 | } |
| 1004 | get_obj<SkPaint>(L, 1)->setStyle(style); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1008 | static int lpaint_getStrokeCap(lua_State* L) { |
| 1009 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap()); |
| 1010 | return 1; |
| 1011 | } |
| 1012 | |
| 1013 | static int lpaint_getStrokeJoin(lua_State* L) { |
| 1014 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin()); |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
| 1018 | static int lpaint_getTextEncoding(lua_State* L) { |
commit-bot@chromium.org | 641bcc3 | 2013-12-19 10:39:59 +0000 | [diff] [blame] | 1019 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding()); |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1020 | return 1; |
| 1021 | } |
| 1022 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1023 | static int lpaint_getStrokeWidth(lua_State* L) { |
| 1024 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
| 1028 | static int lpaint_setStrokeWidth(lua_State* L) { |
| 1029 | get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1033 | static int lpaint_getStrokeMiter(lua_State* L) { |
| 1034 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter()); |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1038 | static int lpaint_measureText(lua_State* L) { |
| 1039 | if (lua_isstring(L, 2)) { |
| 1040 | size_t len; |
| 1041 | const char* text = lua_tolstring(L, 2, &len); |
| 1042 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len)); |
| 1043 | return 1; |
| 1044 | } |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | struct FontMetrics { |
| 1049 | SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) |
| 1050 | SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) |
| 1051 | SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) |
| 1052 | SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) |
| 1053 | SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) |
| 1054 | SkScalar fAvgCharWidth; //!< the average charactor width (>= 0) |
| 1055 | SkScalar fXMin; //!< The minimum bounding box x value for all glyphs |
| 1056 | SkScalar fXMax; //!< The maximum bounding box x value for all glyphs |
| 1057 | SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face |
| 1058 | }; |
| 1059 | |
| 1060 | static int lpaint_getFontMetrics(lua_State* L) { |
| 1061 | SkPaint::FontMetrics fm; |
| 1062 | SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 1063 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1064 | lua_newtable(L); |
| 1065 | setfield_scalar(L, "top", fm.fTop); |
| 1066 | setfield_scalar(L, "ascent", fm.fAscent); |
| 1067 | setfield_scalar(L, "descent", fm.fDescent); |
| 1068 | setfield_scalar(L, "bottom", fm.fBottom); |
| 1069 | setfield_scalar(L, "leading", fm.fLeading); |
| 1070 | SkLua(L).pushScalar(height); |
| 1071 | return 2; |
| 1072 | } |
| 1073 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1074 | static int lpaint_getEffects(lua_State* L) { |
| 1075 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1076 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1077 | lua_newtable(L); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1078 | setfield_bool_if(L, "looper", !!paint->getLooper()); |
| 1079 | setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); |
| 1080 | setfield_bool_if(L, "rasterizer", !!paint->getRasterizer()); |
| 1081 | setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); |
| 1082 | setfield_bool_if(L, "shader", !!paint->getShader()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1083 | setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); |
| 1084 | setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1085 | setfield_bool_if(L, "xfermode", !!paint->getXfermode()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1086 | return 1; |
| 1087 | } |
| 1088 | |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 1089 | static int lpaint_getXfermode(lua_State* L) { |
| 1090 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1091 | SkXfermode* xfermode = paint->getXfermode(); |
| 1092 | if (xfermode) { |
| 1093 | push_ref(L, xfermode); |
| 1094 | return 1; |
| 1095 | } |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int lpaint_setXfermode(lua_State* L) { |
| 1100 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 1101 | paint->setXfermode(sk_ref_sp(get_ref<SkXfermode>(L, 2))); |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1105 | static int lpaint_getColorFilter(lua_State* L) { |
| 1106 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1107 | SkColorFilter* cf = paint->getColorFilter(); |
| 1108 | if (cf) { |
| 1109 | push_ref(L, cf); |
| 1110 | return 1; |
| 1111 | } |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int lpaint_setColorFilter(lua_State* L) { |
| 1116 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 1117 | paint->setColorFilter(sk_ref_sp(get_ref<SkColorFilter>(L, 2))); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1121 | static int lpaint_getImageFilter(lua_State* L) { |
| 1122 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1123 | SkImageFilter* imf = paint->getImageFilter(); |
| 1124 | if (imf) { |
| 1125 | push_ref(L, imf); |
| 1126 | return 1; |
| 1127 | } |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | static int lpaint_setImageFilter(lua_State* L) { |
| 1132 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1133 | paint->setImageFilter(get_ref<SkImageFilter>(L, 2)); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1137 | static int lpaint_getShader(lua_State* L) { |
| 1138 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1139 | SkShader* shader = paint->getShader(); |
| 1140 | if (shader) { |
| 1141 | push_ref(L, shader); |
| 1142 | return 1; |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1147 | static int lpaint_setShader(lua_State* L) { |
| 1148 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1149 | paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2))); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1150 | return 0; |
| 1151 | } |
| 1152 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1153 | static int lpaint_getPathEffect(lua_State* L) { |
| 1154 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1155 | SkPathEffect* pe = paint->getPathEffect(); |
| 1156 | if (pe) { |
| 1157 | push_ref(L, pe); |
| 1158 | return 1; |
| 1159 | } |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1163 | static int lpaint_gc(lua_State* L) { |
| 1164 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 1169 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 1170 | { "setAntiAlias", lpaint_setAntiAlias }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1171 | { "isDither", lpaint_isDither }, |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 1172 | { "setDither", lpaint_setDither }, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 1173 | { "getFilterQuality", lpaint_getFilterQuality }, |
| 1174 | { "setFilterQuality", lpaint_setFilterQuality }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1175 | { "isUnderlineText", lpaint_isUnderlineText }, |
| 1176 | { "isStrikeThruText", lpaint_isStrikeThruText }, |
| 1177 | { "isFakeBoldText", lpaint_isFakeBoldText }, |
| 1178 | { "isLinearText", lpaint_isLinearText }, |
| 1179 | { "isSubpixelText", lpaint_isSubpixelText }, |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 1180 | { "setSubpixelText", lpaint_setSubpixelText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1181 | { "isDevKernText", lpaint_isDevKernText }, |
| 1182 | { "isLCDRenderText", lpaint_isLCDRenderText }, |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1183 | { "setLCDRenderText", lpaint_setLCDRenderText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1184 | { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, |
| 1185 | { "isAutohinted", lpaint_isAutohinted }, |
| 1186 | { "isVerticalText", lpaint_isVerticalText }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1187 | { "getAlpha", lpaint_getAlpha }, |
| 1188 | { "setAlpha", lpaint_setAlpha }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1189 | { "getColor", lpaint_getColor }, |
| 1190 | { "setColor", lpaint_setColor }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1191 | { "getTextSize", lpaint_getTextSize }, |
| 1192 | { "setTextSize", lpaint_setTextSize }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1193 | { "getTextScaleX", lpaint_getTextScaleX }, |
| 1194 | { "getTextSkewX", lpaint_getTextSkewX }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1195 | { "getTypeface", lpaint_getTypeface }, |
| 1196 | { "setTypeface", lpaint_setTypeface }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1197 | { "getHinting", lpaint_getHinting }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1198 | { "getFontID", lpaint_getFontID }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1199 | { "getTextAlign", lpaint_getTextAlign }, |
| 1200 | { "setTextAlign", lpaint_setTextAlign }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1201 | { "getStroke", lpaint_getStroke }, |
| 1202 | { "setStroke", lpaint_setStroke }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1203 | { "getStrokeCap", lpaint_getStrokeCap }, |
| 1204 | { "getStrokeJoin", lpaint_getStrokeJoin }, |
| 1205 | { "getTextEncoding", lpaint_getTextEncoding }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1206 | { "getStrokeWidth", lpaint_getStrokeWidth }, |
| 1207 | { "setStrokeWidth", lpaint_setStrokeWidth }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1208 | { "getStrokeMiter", lpaint_getStrokeMiter }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1209 | { "measureText", lpaint_measureText }, |
| 1210 | { "getFontMetrics", lpaint_getFontMetrics }, |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1211 | { "getEffects", lpaint_getEffects }, |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1212 | { "getColorFilter", lpaint_getColorFilter }, |
| 1213 | { "setColorFilter", lpaint_setColorFilter }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1214 | { "getImageFilter", lpaint_getImageFilter }, |
| 1215 | { "setImageFilter", lpaint_setImageFilter }, |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 1216 | { "getXfermode", lpaint_getXfermode }, |
| 1217 | { "setXfermode", lpaint_setXfermode }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1218 | { "getShader", lpaint_getShader }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1219 | { "setShader", lpaint_setShader }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1220 | { "getPathEffect", lpaint_getPathEffect }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1221 | { "__gc", lpaint_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1222 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | /////////////////////////////////////////////////////////////////////////////// |
| 1226 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1227 | static const char* mode2string(SkShader::TileMode mode) { |
| 1228 | static const char* gNames[] = { "clamp", "repeat", "mirror" }; |
| 1229 | SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); |
| 1230 | return gNames[mode]; |
| 1231 | } |
| 1232 | |
| 1233 | static const char* gradtype2string(SkShader::GradientType t) { |
| 1234 | static const char* gNames[] = { |
| 1235 | "none", "color", "linear", "radial", "radial2", "sweep", "conical" |
| 1236 | }; |
| 1237 | SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames)); |
| 1238 | return gNames[t]; |
| 1239 | } |
| 1240 | |
| 1241 | static int lshader_isOpaque(lua_State* L) { |
| 1242 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1243 | return shader && shader->isOpaque(); |
| 1244 | } |
| 1245 | |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1246 | static int lshader_isABitmap(lua_State* L) { |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1247 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1248 | if (shader) { |
| 1249 | SkBitmap bm; |
| 1250 | SkMatrix matrix; |
| 1251 | SkShader::TileMode modes[2]; |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1252 | if (shader->isABitmap(&bm, &matrix, modes)) { |
| 1253 | lua_newtable(L); |
| 1254 | setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0); |
| 1255 | setfield_number(L, "width", bm.width()); |
| 1256 | setfield_number(L, "height", bm.height()); |
| 1257 | setfield_string(L, "tileX", mode2string(modes[0])); |
| 1258 | setfield_string(L, "tileY", mode2string(modes[1])); |
| 1259 | return 1; |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static int lshader_asAGradient(lua_State* L) { |
| 1266 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1267 | if (shader) { |
| 1268 | SkShader::GradientInfo info; |
| 1269 | sk_bzero(&info, sizeof(info)); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1270 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1271 | SkShader::GradientType t = shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1272 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1273 | if (SkShader::kNone_GradientType != t) { |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame^] | 1274 | SkAutoTArray<SkScalar> pos(info.fColorCount); |
| 1275 | info.fColorOffsets = pos.get(); |
| 1276 | shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1277 | |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame^] | 1278 | bool containsHardStops = false; |
| 1279 | bool isEvenlySpaced = true; |
| 1280 | for (int i = 1; i < info.fColorCount; i++) { |
| 1281 | if (SkScalarNearlyEqual(info.fColorOffsets[i], info.fColorOffsets[i-1])) { |
| 1282 | containsHardStops = true; |
| 1283 | } |
| 1284 | if (!SkScalarNearlyEqual(info.fColorOffsets[i], i/(info.fColorCount - 1.0f))) { |
| 1285 | isEvenlySpaced = false; |
| 1286 | } |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1287 | } |
skia.committer@gmail.com | bd74add | 2013-08-02 07:00:59 +0000 | [diff] [blame] | 1288 | |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame^] | 1289 | lua_newtable(L); |
| 1290 | setfield_string(L, "type", gradtype2string(t)); |
| 1291 | setfield_string(L, "tile", mode2string(info.fTileMode)); |
| 1292 | setfield_number(L, "colorCount", info.fColorCount); |
| 1293 | setfield_boolean(L, "containsHardStops", containsHardStops); |
| 1294 | setfield_boolean(L, "isEvenlySpaced", isEvenlySpaced); |
| 1295 | |
| 1296 | lua_newtable(L); |
| 1297 | for (int i = 0; i < info.fColorCount; i++) { |
| 1298 | // Lua uses 1-based indexing |
| 1299 | setarray_scalar(L, i+1, pos[i]); |
| 1300 | } |
| 1301 | lua_setfield(L, -2, "positions"); |
| 1302 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1303 | return 1; |
| 1304 | } |
| 1305 | } |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static int lshader_gc(lua_State* L) { |
| 1310 | get_ref<SkShader>(L, 1)->unref(); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | static const struct luaL_Reg gSkShader_Methods[] = { |
| 1315 | { "isOpaque", lshader_isOpaque }, |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1316 | { "isABitmap", lshader_isABitmap }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1317 | { "asAGradient", lshader_asAGradient }, |
| 1318 | { "__gc", lshader_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1319 | { nullptr, nullptr } |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1320 | }; |
| 1321 | |
| 1322 | /////////////////////////////////////////////////////////////////////////////// |
| 1323 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1324 | static int lpatheffect_asADash(lua_State* L) { |
| 1325 | SkPathEffect* pe = get_ref<SkPathEffect>(L, 1); |
| 1326 | if (pe) { |
| 1327 | SkPathEffect::DashInfo info; |
| 1328 | SkPathEffect::DashType dashType = pe->asADash(&info); |
| 1329 | if (SkPathEffect::kDash_DashType == dashType) { |
| 1330 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 1331 | info.fIntervals = intervals.get(); |
| 1332 | pe->asADash(&info); |
| 1333 | SkLua(L).pushDash(info); |
| 1334 | return 1; |
| 1335 | } |
| 1336 | } |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1340 | static int lpatheffect_gc(lua_State* L) { |
| 1341 | get_ref<SkPathEffect>(L, 1)->unref(); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static const struct luaL_Reg gSkPathEffect_Methods[] = { |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1346 | { "asADash", lpatheffect_asADash }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1347 | { "__gc", lpatheffect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1348 | { nullptr, nullptr } |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1349 | }; |
| 1350 | |
| 1351 | /////////////////////////////////////////////////////////////////////////////// |
| 1352 | |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 1353 | static int lpxfermode_getTypeName(lua_State* L) { |
| 1354 | lua_pushstring(L, get_ref<SkXfermode>(L, 1)->getTypeName()); |
| 1355 | return 1; |
| 1356 | } |
| 1357 | |
| 1358 | static int lpxfermode_gc(lua_State* L) { |
| 1359 | get_ref<SkXfermode>(L, 1)->unref(); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static const struct luaL_Reg gSkXfermode_Methods[] = { |
| 1364 | { "getTypeName", lpxfermode_getTypeName }, |
| 1365 | { "__gc", lpxfermode_gc }, |
| 1366 | { nullptr, nullptr } |
| 1367 | }; |
| 1368 | |
| 1369 | /////////////////////////////////////////////////////////////////////////////// |
| 1370 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1371 | static int lpcolorfilter_gc(lua_State* L) { |
| 1372 | get_ref<SkColorFilter>(L, 1)->unref(); |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | static const struct luaL_Reg gSkColorFilter_Methods[] = { |
| 1377 | { "__gc", lpcolorfilter_gc }, |
| 1378 | { nullptr, nullptr } |
| 1379 | }; |
| 1380 | |
| 1381 | /////////////////////////////////////////////////////////////////////////////// |
| 1382 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1383 | static int lpimagefilter_gc(lua_State* L) { |
| 1384 | get_ref<SkImageFilter>(L, 1)->unref(); |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | static const struct luaL_Reg gSkImageFilter_Methods[] = { |
| 1389 | { "__gc", lpimagefilter_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1390 | { nullptr, nullptr } |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1391 | }; |
| 1392 | |
| 1393 | /////////////////////////////////////////////////////////////////////////////// |
| 1394 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1395 | static int lmatrix_getType(lua_State* L) { |
| 1396 | SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1397 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1398 | lua_newtable(L); |
| 1399 | setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask)); |
| 1400 | setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); |
| 1401 | setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); |
| 1402 | setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask)); |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1406 | static int lmatrix_getScaleX(lua_State* L) { |
| 1407 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX()); |
| 1408 | return 1; |
| 1409 | } |
| 1410 | |
| 1411 | static int lmatrix_getScaleY(lua_State* L) { |
| 1412 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY()); |
| 1413 | return 1; |
| 1414 | } |
| 1415 | |
| 1416 | static int lmatrix_getTranslateX(lua_State* L) { |
| 1417 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX()); |
| 1418 | return 1; |
| 1419 | } |
| 1420 | |
| 1421 | static int lmatrix_getTranslateY(lua_State* L) { |
| 1422 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY()); |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1426 | static int lmatrix_invert(lua_State* L) { |
| 1427 | lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2))); |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | static int lmatrix_mapXY(lua_State* L) { |
| 1432 | SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) }; |
| 1433 | get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1); |
| 1434 | lua_pushnumber(L, pt.x()); |
| 1435 | lua_pushnumber(L, pt.y()); |
| 1436 | return 2; |
| 1437 | } |
| 1438 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1439 | static int lmatrix_setRectToRect(lua_State* L) { |
| 1440 | SkMatrix* matrix = get_obj<SkMatrix>(L, 1); |
| 1441 | SkRect srcR, dstR; |
| 1442 | lua2rect(L, 2, &srcR); |
| 1443 | lua2rect(L, 3, &dstR); |
| 1444 | const char* scaleToFitStr = lua_tostring(L, 4); |
| 1445 | SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit; |
| 1446 | |
| 1447 | if (scaleToFitStr) { |
| 1448 | const struct { |
| 1449 | const char* fName; |
| 1450 | SkMatrix::ScaleToFit fScaleToFit; |
| 1451 | } rec[] = { |
| 1452 | { "fill", SkMatrix::kFill_ScaleToFit }, |
| 1453 | { "start", SkMatrix::kStart_ScaleToFit }, |
| 1454 | { "center", SkMatrix::kCenter_ScaleToFit }, |
| 1455 | { "end", SkMatrix::kEnd_ScaleToFit }, |
| 1456 | }; |
| 1457 | |
| 1458 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 1459 | if (strcmp(rec[i].fName, scaleToFitStr) == 0) { |
| 1460 | scaleToFit = rec[i].fScaleToFit; |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | matrix->setRectToRect(srcR, dstR, scaleToFit); |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1470 | static const struct luaL_Reg gSkMatrix_Methods[] = { |
| 1471 | { "getType", lmatrix_getType }, |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1472 | { "getScaleX", lmatrix_getScaleX }, |
| 1473 | { "getScaleY", lmatrix_getScaleY }, |
| 1474 | { "getTranslateX", lmatrix_getTranslateX }, |
| 1475 | { "getTranslateY", lmatrix_getTranslateY }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1476 | { "setRectToRect", lmatrix_setRectToRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1477 | { "invert", lmatrix_invert }, |
| 1478 | { "mapXY", lmatrix_mapXY }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1479 | { nullptr, nullptr } |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1480 | }; |
| 1481 | |
| 1482 | /////////////////////////////////////////////////////////////////////////////// |
| 1483 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1484 | static int lpath_getBounds(lua_State* L) { |
| 1485 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1489 | static const char* fill_type_to_str(SkPath::FillType fill) { |
| 1490 | switch (fill) { |
| 1491 | case SkPath::kEvenOdd_FillType: |
| 1492 | return "even-odd"; |
| 1493 | case SkPath::kWinding_FillType: |
| 1494 | return "winding"; |
| 1495 | case SkPath::kInverseEvenOdd_FillType: |
| 1496 | return "inverse-even-odd"; |
| 1497 | case SkPath::kInverseWinding_FillType: |
| 1498 | return "inverse-winding"; |
| 1499 | } |
| 1500 | return "unknown"; |
| 1501 | } |
| 1502 | |
| 1503 | static int lpath_getFillType(lua_State* L) { |
| 1504 | SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType(); |
| 1505 | SkLua(L).pushString(fill_type_to_str(fill)); |
| 1506 | return 1; |
| 1507 | } |
| 1508 | |
| 1509 | static SkString segment_masks_to_str(uint32_t segmentMasks) { |
| 1510 | SkString result; |
| 1511 | bool first = true; |
| 1512 | if (SkPath::kLine_SegmentMask & segmentMasks) { |
| 1513 | result.append("line"); |
| 1514 | first = false; |
| 1515 | SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;) |
| 1516 | } |
| 1517 | if (SkPath::kQuad_SegmentMask & segmentMasks) { |
| 1518 | if (!first) { |
| 1519 | result.append(" "); |
| 1520 | } |
| 1521 | result.append("quad"); |
| 1522 | first = false; |
| 1523 | SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;) |
| 1524 | } |
| 1525 | if (SkPath::kConic_SegmentMask & segmentMasks) { |
| 1526 | if (!first) { |
| 1527 | result.append(" "); |
| 1528 | } |
| 1529 | result.append("conic"); |
| 1530 | first = false; |
| 1531 | SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;) |
| 1532 | } |
| 1533 | if (SkPath::kCubic_SegmentMask & segmentMasks) { |
| 1534 | if (!first) { |
| 1535 | result.append(" "); |
| 1536 | } |
| 1537 | result.append("cubic"); |
| 1538 | SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;) |
| 1539 | } |
| 1540 | SkASSERT(0 == segmentMasks); |
| 1541 | return result; |
| 1542 | } |
| 1543 | |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1544 | static int lpath_getSegmentTypes(lua_State* L) { |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1545 | uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks(); |
| 1546 | SkLua(L).pushString(segment_masks_to_str(segMasks)); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | static int lpath_isConvex(lua_State* L) { |
| 1551 | bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity(); |
| 1552 | SkLua(L).pushBool(isConvex); |
| 1553 | return 1; |
| 1554 | } |
| 1555 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1556 | static int lpath_isEmpty(lua_State* L) { |
| 1557 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 1558 | return 1; |
| 1559 | } |
| 1560 | |
| 1561 | static int lpath_isRect(lua_State* L) { |
| 1562 | SkRect r; |
| 1563 | bool pred = get_obj<SkPath>(L, 1)->isRect(&r); |
| 1564 | int ret_count = 1; |
| 1565 | lua_pushboolean(L, pred); |
| 1566 | if (pred) { |
| 1567 | SkLua(L).pushRect(r); |
| 1568 | ret_count += 1; |
| 1569 | } |
| 1570 | return ret_count; |
| 1571 | } |
| 1572 | |
| 1573 | static const char* dir2string(SkPath::Direction dir) { |
| 1574 | static const char* gStr[] = { |
| 1575 | "unknown", "cw", "ccw" |
| 1576 | }; |
| 1577 | SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 1578 | return gStr[dir]; |
| 1579 | } |
| 1580 | |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1581 | static int lpath_isNestedFillRects(lua_State* L) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1582 | SkRect rects[2]; |
| 1583 | SkPath::Direction dirs[2]; |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1584 | bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1585 | int ret_count = 1; |
| 1586 | lua_pushboolean(L, pred); |
| 1587 | if (pred) { |
| 1588 | SkLua lua(L); |
| 1589 | lua.pushRect(rects[0]); |
| 1590 | lua.pushRect(rects[1]); |
| 1591 | lua_pushstring(L, dir2string(dirs[0])); |
| 1592 | lua_pushstring(L, dir2string(dirs[0])); |
| 1593 | ret_count += 4; |
| 1594 | } |
| 1595 | return ret_count; |
| 1596 | } |
| 1597 | |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1598 | static int lpath_countPoints(lua_State* L) { |
| 1599 | lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints()); |
| 1600 | return 1; |
| 1601 | } |
| 1602 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1603 | static int lpath_reset(lua_State* L) { |
| 1604 | get_obj<SkPath>(L, 1)->reset(); |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | static int lpath_moveTo(lua_State* L) { |
| 1609 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static int lpath_lineTo(lua_State* L) { |
| 1614 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | static int lpath_quadTo(lua_State* L) { |
| 1619 | get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1620 | lua2scalar(L, 4), lua2scalar(L, 5)); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | static int lpath_cubicTo(lua_State* L) { |
| 1625 | get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1626 | lua2scalar(L, 4), lua2scalar(L, 5), |
| 1627 | lua2scalar(L, 6), lua2scalar(L, 7)); |
| 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | static int lpath_close(lua_State* L) { |
| 1632 | get_obj<SkPath>(L, 1)->close(); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | static int lpath_gc(lua_State* L) { |
| 1637 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
| 1641 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 1642 | { "getBounds", lpath_getBounds }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1643 | { "getFillType", lpath_getFillType }, |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1644 | { "getSegmentTypes", lpath_getSegmentTypes }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1645 | { "isConvex", lpath_isConvex }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1646 | { "isEmpty", lpath_isEmpty }, |
| 1647 | { "isRect", lpath_isRect }, |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1648 | { "isNestedFillRects", lpath_isNestedFillRects }, |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1649 | { "countPoints", lpath_countPoints }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1650 | { "reset", lpath_reset }, |
| 1651 | { "moveTo", lpath_moveTo }, |
| 1652 | { "lineTo", lpath_lineTo }, |
| 1653 | { "quadTo", lpath_quadTo }, |
| 1654 | { "cubicTo", lpath_cubicTo }, |
| 1655 | { "close", lpath_close }, |
| 1656 | { "__gc", lpath_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1657 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1658 | }; |
| 1659 | |
| 1660 | /////////////////////////////////////////////////////////////////////////////// |
| 1661 | |
| 1662 | static const char* rrect_type(const SkRRect& rr) { |
| 1663 | switch (rr.getType()) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1664 | case SkRRect::kEmpty_Type: return "empty"; |
| 1665 | case SkRRect::kRect_Type: return "rect"; |
| 1666 | case SkRRect::kOval_Type: return "oval"; |
| 1667 | case SkRRect::kSimple_Type: return "simple"; |
commit-bot@chromium.org | f338d7c | 2014-03-17 21:17:30 +0000 | [diff] [blame] | 1668 | case SkRRect::kNinePatch_Type: return "nine-patch"; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1669 | case SkRRect::kComplex_Type: return "complex"; |
| 1670 | } |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1671 | SkDEBUGFAIL("never get here"); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1672 | return ""; |
| 1673 | } |
| 1674 | |
| 1675 | static int lrrect_rect(lua_State* L) { |
| 1676 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 1677 | return 1; |
| 1678 | } |
| 1679 | |
| 1680 | static int lrrect_type(lua_State* L) { |
| 1681 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 1682 | return 1; |
| 1683 | } |
| 1684 | |
| 1685 | static int lrrect_radii(lua_State* L) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 1686 | int corner = SkToInt(lua_tointeger(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1687 | SkVector v; |
| 1688 | if (corner < 0 || corner > 3) { |
| 1689 | SkDebugf("bad corner index %d", corner); |
| 1690 | v.set(0, 0); |
| 1691 | } else { |
| 1692 | v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner); |
| 1693 | } |
| 1694 | lua_pushnumber(L, v.fX); |
| 1695 | lua_pushnumber(L, v.fY); |
| 1696 | return 2; |
| 1697 | } |
| 1698 | |
| 1699 | static int lrrect_gc(lua_State* L) { |
| 1700 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 1705 | { "rect", lrrect_rect }, |
| 1706 | { "type", lrrect_type }, |
| 1707 | { "radii", lrrect_radii }, |
| 1708 | { "__gc", lrrect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1709 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1710 | }; |
| 1711 | |
| 1712 | /////////////////////////////////////////////////////////////////////////////// |
| 1713 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1714 | static int limage_width(lua_State* L) { |
| 1715 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->width()); |
| 1716 | return 1; |
| 1717 | } |
| 1718 | |
| 1719 | static int limage_height(lua_State* L) { |
| 1720 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->height()); |
| 1721 | return 1; |
| 1722 | } |
| 1723 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1724 | static int limage_newShader(lua_State* L) { |
| 1725 | SkShader::TileMode tmode = SkShader::kClamp_TileMode; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1726 | const SkMatrix* localM = nullptr; |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 1727 | push_ref(L, get_ref<SkImage>(L, 1)->makeShader(tmode, tmode, localM)); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1728 | return 1; |
| 1729 | } |
| 1730 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1731 | static int limage_gc(lua_State* L) { |
| 1732 | get_ref<SkImage>(L, 1)->unref(); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | static const struct luaL_Reg gSkImage_Methods[] = { |
| 1737 | { "width", limage_width }, |
| 1738 | { "height", limage_height }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1739 | { "newShader", limage_newShader }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1740 | { "__gc", limage_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1741 | { nullptr, nullptr } |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1742 | }; |
| 1743 | |
| 1744 | /////////////////////////////////////////////////////////////////////////////// |
| 1745 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1746 | static int lsurface_width(lua_State* L) { |
| 1747 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width()); |
| 1748 | return 1; |
| 1749 | } |
| 1750 | |
| 1751 | static int lsurface_height(lua_State* L) { |
| 1752 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height()); |
| 1753 | return 1; |
| 1754 | } |
| 1755 | |
| 1756 | static int lsurface_getCanvas(lua_State* L) { |
| 1757 | SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1758 | if (nullptr == canvas) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1759 | lua_pushnil(L); |
| 1760 | } else { |
| 1761 | push_ref(L, canvas); |
| 1762 | // note: we don't unref canvas, since getCanvas did not ref it. |
| 1763 | // warning: this is weird: now Lua owns a ref on this canvas, but what if they let |
| 1764 | // the real owner (the surface) go away, but still hold onto the canvas? |
| 1765 | // *really* we want to sort of ref the surface again, but have the native object |
| 1766 | // know that it is supposed to be treated as a canvas... |
| 1767 | } |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | static int lsurface_newImageSnapshot(lua_State* L) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1772 | sk_sp<SkImage> image = get_ref<SkSurface>(L, 1)->makeImageSnapshot(); |
| 1773 | if (!image) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1774 | lua_pushnil(L); |
| 1775 | } else { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1776 | push_ref(L, image); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1777 | } |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
| 1781 | static int lsurface_newSurface(lua_State* L) { |
| 1782 | int width = lua2int_def(L, 2, 0); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1783 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1784 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1785 | auto surface = get_ref<SkSurface>(L, 1)->makeSurface(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1786 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1787 | lua_pushnil(L); |
| 1788 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1789 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1790 | } |
| 1791 | return 1; |
| 1792 | } |
| 1793 | |
| 1794 | static int lsurface_gc(lua_State* L) { |
| 1795 | get_ref<SkSurface>(L, 1)->unref(); |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | static const struct luaL_Reg gSkSurface_Methods[] = { |
| 1800 | { "width", lsurface_width }, |
| 1801 | { "height", lsurface_height }, |
| 1802 | { "getCanvas", lsurface_getCanvas }, |
| 1803 | { "newImageSnapshot", lsurface_newImageSnapshot }, |
| 1804 | { "newSurface", lsurface_newSurface }, |
| 1805 | { "__gc", lsurface_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1806 | { nullptr, nullptr } |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1807 | }; |
| 1808 | |
| 1809 | /////////////////////////////////////////////////////////////////////////////// |
| 1810 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1811 | static int lpicturerecorder_beginRecording(lua_State* L) { |
| 1812 | const SkScalar w = lua2scalar_def(L, 2, -1); |
| 1813 | const SkScalar h = lua2scalar_def(L, 3, -1); |
| 1814 | if (w <= 0 || h <= 0) { |
| 1815 | lua_pushnil(L); |
| 1816 | return 1; |
| 1817 | } |
| 1818 | |
| 1819 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1820 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1821 | lua_pushnil(L); |
| 1822 | return 1; |
| 1823 | } |
| 1824 | |
| 1825 | push_ref(L, canvas); |
| 1826 | return 1; |
| 1827 | } |
| 1828 | |
| 1829 | static int lpicturerecorder_getCanvas(lua_State* L) { |
| 1830 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1831 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1832 | lua_pushnil(L); |
| 1833 | return 1; |
| 1834 | } |
| 1835 | push_ref(L, canvas); |
| 1836 | return 1; |
| 1837 | } |
| 1838 | |
| 1839 | static int lpicturerecorder_endRecording(lua_State* L) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1840 | sk_sp<SkPicture> pic = get_obj<SkPictureRecorder>(L, 1)->finishRecordingAsPicture(); |
| 1841 | if (!pic) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1842 | lua_pushnil(L); |
| 1843 | return 1; |
| 1844 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1845 | push_ref(L, std::move(pic)); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1846 | return 1; |
| 1847 | } |
| 1848 | |
| 1849 | static int lpicturerecorder_gc(lua_State* L) { |
| 1850 | get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder(); |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | static const struct luaL_Reg gSkPictureRecorder_Methods[] = { |
| 1855 | { "beginRecording", lpicturerecorder_beginRecording }, |
| 1856 | { "getCanvas", lpicturerecorder_getCanvas }, |
| 1857 | { "endRecording", lpicturerecorder_endRecording }, |
| 1858 | { "__gc", lpicturerecorder_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1859 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1860 | }; |
| 1861 | |
| 1862 | /////////////////////////////////////////////////////////////////////////////// |
| 1863 | |
| 1864 | static int lpicture_width(lua_State* L) { |
| 1865 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width()); |
| 1866 | return 1; |
| 1867 | } |
| 1868 | |
| 1869 | static int lpicture_height(lua_State* L) { |
| 1870 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height()); |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
| 1874 | static int lpicture_gc(lua_State* L) { |
| 1875 | get_ref<SkPicture>(L, 1)->unref(); |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
| 1879 | static const struct luaL_Reg gSkPicture_Methods[] = { |
| 1880 | { "width", lpicture_width }, |
| 1881 | { "height", lpicture_height }, |
| 1882 | { "__gc", lpicture_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1883 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1884 | }; |
| 1885 | |
| 1886 | /////////////////////////////////////////////////////////////////////////////// |
| 1887 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1888 | static int ltextblob_bounds(lua_State* L) { |
| 1889 | SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds()); |
| 1890 | return 1; |
| 1891 | } |
| 1892 | |
| 1893 | static int ltextblob_gc(lua_State* L) { |
| 1894 | SkSafeUnref(get_ref<SkTextBlob>(L, 1)); |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | static const struct luaL_Reg gSkTextBlob_Methods[] = { |
| 1899 | { "bounds", ltextblob_bounds }, |
| 1900 | { "__gc", ltextblob_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1901 | { nullptr, nullptr } |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1902 | }; |
| 1903 | |
| 1904 | /////////////////////////////////////////////////////////////////////////////// |
| 1905 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1906 | static int ltypeface_getFamilyName(lua_State* L) { |
| 1907 | SkString str; |
| 1908 | get_ref<SkTypeface>(L, 1)->getFamilyName(&str); |
| 1909 | lua_pushstring(L, str.c_str()); |
| 1910 | return 1; |
| 1911 | } |
| 1912 | |
| 1913 | static int ltypeface_getStyle(lua_State* L) { |
| 1914 | lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style()); |
| 1915 | return 1; |
| 1916 | } |
| 1917 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1918 | static int ltypeface_gc(lua_State* L) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 1919 | SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1920 | return 0; |
| 1921 | } |
| 1922 | |
| 1923 | static const struct luaL_Reg gSkTypeface_Methods[] = { |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1924 | { "getFamilyName", ltypeface_getFamilyName }, |
| 1925 | { "getStyle", ltypeface_getStyle }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1926 | { "__gc", ltypeface_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1927 | { nullptr, nullptr } |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1928 | }; |
| 1929 | |
| 1930 | /////////////////////////////////////////////////////////////////////////////// |
| 1931 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1932 | class AutoCallLua { |
| 1933 | public: |
| 1934 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) { |
| 1935 | lua_getglobal(L, func); |
| 1936 | if (!lua_isfunction(L, -1)) { |
| 1937 | int t = lua_type(L, -1); |
| 1938 | SkDebugf("--- expected function %d\n", t); |
| 1939 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1940 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1941 | lua_newtable(L); |
| 1942 | setfield_string(L, "verb", verb); |
| 1943 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1944 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1945 | ~AutoCallLua() { |
| 1946 | if (lua_pcall(fL, 1, 0, 0) != LUA_OK) { |
| 1947 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 1948 | } |
| 1949 | lua_settop(fL, -1); |
| 1950 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1951 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1952 | private: |
| 1953 | lua_State* fL; |
| 1954 | }; |
| 1955 | |
| 1956 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 1957 | |
| 1958 | /////////////////////////////////////////////////////////////////////////////// |
| 1959 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1960 | static int lsk_newDocumentPDF(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1961 | const char* file = nullptr; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1962 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1963 | file = lua_tolstring(L, 1, nullptr); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 1966 | sk_sp<SkDocument> doc = SkDocument::MakePDF(file); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1967 | if (nullptr == doc) { |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1968 | // do I need to push a nil on the stack and return 1? |
| 1969 | return 0; |
| 1970 | } else { |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 1971 | push_ref(L, std::move(doc)); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1972 | return 1; |
| 1973 | } |
| 1974 | } |
| 1975 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1976 | static int lsk_newBlurImageFilter(lua_State* L) { |
| 1977 | SkScalar sigmaX = lua2scalar_def(L, 1, 0); |
| 1978 | SkScalar sigmaY = lua2scalar_def(L, 2, 0); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1979 | sk_sp<SkImageFilter> imf(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr)); |
| 1980 | if (!imf) { |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1981 | lua_pushnil(L); |
| 1982 | } else { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1983 | push_ref(L, std::move(imf)); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1984 | } |
| 1985 | return 1; |
| 1986 | } |
| 1987 | |
| 1988 | static int lsk_newLinearGradient(lua_State* L) { |
| 1989 | SkScalar x0 = lua2scalar_def(L, 1, 0); |
| 1990 | SkScalar y0 = lua2scalar_def(L, 2, 0); |
| 1991 | SkColor c0 = lua2color(L, 3); |
| 1992 | SkScalar x1 = lua2scalar_def(L, 4, 0); |
| 1993 | SkScalar y1 = lua2scalar_def(L, 5, 0); |
| 1994 | SkColor c1 = lua2color(L, 6); |
| 1995 | |
| 1996 | SkPoint pts[] = { { x0, y0 }, { x1, y1 } }; |
| 1997 | SkColor colors[] = { c0, c1 }; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1998 | sk_sp<SkShader> s(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 1999 | SkShader::kClamp_TileMode)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 2000 | if (!s) { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2001 | lua_pushnil(L); |
| 2002 | } else { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 2003 | push_ref(L, std::move(s)); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2004 | } |
| 2005 | return 1; |
| 2006 | } |
| 2007 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2008 | static int lsk_newMatrix(lua_State* L) { |
| 2009 | push_new<SkMatrix>(L)->reset(); |
| 2010 | return 1; |
| 2011 | } |
| 2012 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2013 | static int lsk_newPaint(lua_State* L) { |
| 2014 | push_new<SkPaint>(L); |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
| 2018 | static int lsk_newPath(lua_State* L) { |
| 2019 | push_new<SkPath>(L); |
| 2020 | return 1; |
| 2021 | } |
| 2022 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2023 | static int lsk_newPictureRecorder(lua_State* L) { |
| 2024 | push_new<SkPictureRecorder>(L); |
| 2025 | return 1; |
| 2026 | } |
| 2027 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2028 | static int lsk_newRRect(lua_State* L) { |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2029 | push_new<SkRRect>(L)->setEmpty(); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2030 | return 1; |
| 2031 | } |
| 2032 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2033 | #include "SkTextBox.h" |
| 2034 | // Sk.newTextBlob(text, rect, paint) |
| 2035 | static int lsk_newTextBlob(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2036 | const char* text = lua_tolstring(L, 1, nullptr); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2037 | SkRect bounds; |
| 2038 | lua2rect(L, 2, &bounds); |
| 2039 | const SkPaint& paint = *get_obj<SkPaint>(L, 3); |
| 2040 | |
| 2041 | SkTextBox box; |
| 2042 | box.setMode(SkTextBox::kLineBreak_Mode); |
| 2043 | box.setBox(bounds); |
| 2044 | box.setText(text, strlen(text), paint); |
| 2045 | |
| 2046 | SkScalar newBottom; |
| 2047 | SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom)); |
| 2048 | push_ref<SkTextBlob>(L, blob); |
| 2049 | SkLua(L).pushScalar(newBottom); |
| 2050 | return 2; |
| 2051 | } |
| 2052 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2053 | static int lsk_newTypeface(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2054 | const char* name = nullptr; |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2055 | int style = SkTypeface::kNormal; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 2056 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2057 | int count = lua_gettop(L); |
| 2058 | if (count > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2059 | name = lua_tolstring(L, 1, nullptr); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2060 | if (count > 1 && lua_isnumber(L, 2)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2061 | style = lua_tointegerx(L, 2, nullptr) & SkTypeface::kBoldItalic; |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2062 | } |
| 2063 | } |
| 2064 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 2065 | sk_sp<SkTypeface> face(SkTypeface::MakeFromName(name, SkFontStyle::FromOldStyle(style))); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2066 | // 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] | 2067 | if (nullptr == face) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 2068 | face = SkTypeface::MakeDefault(); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2069 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 2070 | push_ref(L, std::move(face)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2071 | return 1; |
| 2072 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2073 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2074 | static int lsk_newRasterSurface(lua_State* L) { |
reed | 7b86466 | 2014-11-04 13:24:47 -0800 | [diff] [blame] | 2075 | int width = lua2int_def(L, 1, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2076 | int height = lua2int_def(L, 2, 0); |
| 2077 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 2078 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2079 | auto surface = SkSurface::MakeRaster(info, &props); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2080 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2081 | lua_pushnil(L); |
| 2082 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2083 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2084 | } |
| 2085 | return 1; |
| 2086 | } |
| 2087 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2088 | static int lsk_loadImage(lua_State* L) { |
| 2089 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2090 | const char* name = lua_tolstring(L, 1, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2091 | sk_sp<SkData> data(SkData::MakeFromFileName(name)); |
| 2092 | if (data) { |
| 2093 | auto image = SkImage::MakeFromEncoded(std::move(data)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2094 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2095 | push_ref(L, std::move(image)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2096 | return 1; |
| 2097 | } |
| 2098 | } |
| 2099 | } |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2103 | static void register_Sk(lua_State* L) { |
| 2104 | lua_newtable(L); |
| 2105 | lua_pushvalue(L, -1); |
| 2106 | lua_setglobal(L, "Sk"); |
| 2107 | // the Sk table is still on top |
| 2108 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2109 | setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2110 | setfield_function(L, "loadImage", lsk_loadImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2111 | setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2112 | setfield_function(L, "newLinearGradient", lsk_newLinearGradient); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2113 | setfield_function(L, "newMatrix", lsk_newMatrix); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2114 | setfield_function(L, "newPaint", lsk_newPaint); |
| 2115 | setfield_function(L, "newPath", lsk_newPath); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2116 | setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2117 | setfield_function(L, "newRRect", lsk_newRRect); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2118 | setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2119 | setfield_function(L, "newTextBlob", lsk_newTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2120 | setfield_function(L, "newTypeface", lsk_newTypeface); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2121 | lua_pop(L, 1); // pop off the Sk table |
| 2122 | } |
| 2123 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2124 | #define REG_CLASS(L, C) \ |
| 2125 | do { \ |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2126 | luaL_newmetatable(L, get_mtname<C>()); \ |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2127 | lua_pushvalue(L, -1); \ |
| 2128 | lua_setfield(L, -2, "__index"); \ |
| 2129 | luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 2130 | lua_pop(L, 1); /* pop off the meta-table */ \ |
| 2131 | } while (0) |
| 2132 | |
| 2133 | void SkLua::Load(lua_State* L) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2134 | register_Sk(L); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2135 | REG_CLASS(L, SkCanvas); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 2136 | REG_CLASS(L, SkColorFilter); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2137 | REG_CLASS(L, SkDocument); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2138 | REG_CLASS(L, SkImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2139 | REG_CLASS(L, SkImageFilter); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2140 | REG_CLASS(L, SkMatrix); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2141 | REG_CLASS(L, SkPaint); |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 2142 | REG_CLASS(L, SkPath); |
| 2143 | REG_CLASS(L, SkPathEffect); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2144 | REG_CLASS(L, SkPicture); |
| 2145 | REG_CLASS(L, SkPictureRecorder); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2146 | REG_CLASS(L, SkRRect); |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 2147 | REG_CLASS(L, SkShader); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2148 | REG_CLASS(L, SkSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2149 | REG_CLASS(L, SkTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2150 | REG_CLASS(L, SkTypeface); |
robertphillips | 233bab9 | 2016-01-21 09:05:32 -0800 | [diff] [blame] | 2151 | REG_CLASS(L, SkXfermode); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2152 | } |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2153 | |
reed@google.com | 7bce998 | 2013-06-20 17:40:21 +0000 | [diff] [blame] | 2154 | extern "C" int luaopen_skia(lua_State* L); |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2155 | extern "C" int luaopen_skia(lua_State* L) { |
| 2156 | SkLua::Load(L); |
| 2157 | return 0; |
| 2158 | } |