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