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