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