reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkLua.h" |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 9 | |
| 10 | #if SK_SUPPORT_GPU |
Mike Reed | 4f364fd | 2017-01-19 14:34:51 -0500 | [diff] [blame] | 11 | //#include "GrReducedClip.h" |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 14 | #include "SkBlurImageFilter.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 15 | #include "SkCanvas.h" |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 16 | #include "SkColorFilter.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 17 | #include "SkData.h" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 18 | #include "SkDocument.h" |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 19 | #include "SkFontStyle.h" |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 20 | #include "SkGradientShader.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 21 | #include "SkImage.h" |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 22 | #include "SkMakeUnique.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 23 | #include "SkMatrix.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 24 | #include "SkPaint.h" |
| 25 | #include "SkPath.h" |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 26 | #include "SkPictureRecorder.h" |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 27 | #include "SkPixelRef.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 28 | #include "SkRRect.h" |
Herb Derby | 1724db1 | 2018-05-22 12:01:50 -0400 | [diff] [blame] | 29 | #include "SkShaper.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 30 | #include "SkString.h" |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 31 | #include "SkSurface.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 32 | #include "SkTextBlob.h" |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 33 | #include "SkTypeface.h" |
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_isDevKernText(lua_State* L) { |
| 751 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText()); |
| 752 | return 1; |
| 753 | } |
| 754 | |
| 755 | static int lpaint_isLCDRenderText(lua_State* L) { |
| 756 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText()); |
| 757 | return 1; |
| 758 | } |
| 759 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 760 | static int lpaint_setLCDRenderText(lua_State* L) { |
| 761 | get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2)); |
| 762 | return 1; |
| 763 | } |
| 764 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 765 | static int lpaint_isEmbeddedBitmapText(lua_State* L) { |
| 766 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText()); |
| 767 | return 1; |
| 768 | } |
| 769 | |
| 770 | static int lpaint_isAutohinted(lua_State* L) { |
| 771 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted()); |
| 772 | return 1; |
| 773 | } |
| 774 | |
| 775 | static int lpaint_isVerticalText(lua_State* L) { |
| 776 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText()); |
| 777 | return 1; |
| 778 | } |
| 779 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 780 | static int lpaint_getAlpha(lua_State* L) { |
| 781 | SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha())); |
| 782 | return 1; |
| 783 | } |
| 784 | |
| 785 | static int lpaint_setAlpha(lua_State* L) { |
| 786 | get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2))); |
| 787 | return 0; |
| 788 | } |
| 789 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 790 | static int lpaint_getColor(lua_State* L) { |
| 791 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 792 | return 1; |
| 793 | } |
| 794 | |
| 795 | static int lpaint_setColor(lua_State* L) { |
| 796 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 797 | return 0; |
| 798 | } |
| 799 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 800 | static int lpaint_getTextSize(lua_State* L) { |
| 801 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 802 | return 1; |
| 803 | } |
| 804 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 805 | static int lpaint_getTextScaleX(lua_State* L) { |
| 806 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX()); |
| 807 | return 1; |
| 808 | } |
| 809 | |
| 810 | static int lpaint_getTextSkewX(lua_State* L) { |
| 811 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX()); |
| 812 | return 1; |
| 813 | } |
| 814 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 815 | static int lpaint_setTextSize(lua_State* L) { |
| 816 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 817 | return 0; |
| 818 | } |
| 819 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 820 | static int lpaint_getTypeface(lua_State* L) { |
| 821 | push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); |
| 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | static int lpaint_setTypeface(lua_State* L) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 826 | 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] | 827 | return 0; |
| 828 | } |
| 829 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 830 | static int lpaint_getHinting(lua_State* L) { |
| 831 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting()); |
| 832 | return 1; |
| 833 | } |
| 834 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 835 | static int lpaint_getFilterQuality(lua_State* L) { |
| 836 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterQuality()); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 837 | return 1; |
| 838 | } |
| 839 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 840 | static int lpaint_setFilterQuality(lua_State* L) { |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 841 | int level = lua2int_def(L, 2, -1); |
| 842 | if (level >= 0 && level <= 3) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 843 | get_obj<SkPaint>(L, 1)->setFilterQuality((SkFilterQuality)level); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 844 | } |
| 845 | return 0; |
| 846 | } |
| 847 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 848 | static int lpaint_getFontID(lua_State* L) { |
| 849 | SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface(); |
| 850 | SkLua(L).pushU32(SkTypeface::UniqueID(face)); |
| 851 | return 1; |
| 852 | } |
| 853 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 854 | static const struct { |
| 855 | const char* fLabel; |
| 856 | SkPaint::Align fAlign; |
| 857 | } gAlignRec[] = { |
| 858 | { "left", SkPaint::kLeft_Align }, |
| 859 | { "center", SkPaint::kCenter_Align }, |
| 860 | { "right", SkPaint::kRight_Align }, |
| 861 | }; |
| 862 | |
| 863 | static int lpaint_getTextAlign(lua_State* L) { |
| 864 | SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign(); |
| 865 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 866 | if (gAlignRec[i].fAlign == align) { |
| 867 | lua_pushstring(L, gAlignRec[i].fLabel); |
| 868 | return 1; |
| 869 | } |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int lpaint_setTextAlign(lua_State* L) { |
| 875 | if (lua_isstring(L, 2)) { |
| 876 | size_t len; |
| 877 | const char* label = lua_tolstring(L, 2, &len); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 878 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 879 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 880 | if (!strcmp(gAlignRec[i].fLabel, label)) { |
| 881 | get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign); |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | return 0; |
| 887 | } |
| 888 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 889 | static int lpaint_getStroke(lua_State* L) { |
| 890 | lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle()); |
| 891 | return 1; |
| 892 | } |
| 893 | |
| 894 | static int lpaint_setStroke(lua_State* L) { |
| 895 | SkPaint::Style style; |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 896 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 897 | if (lua_toboolean(L, 2)) { |
| 898 | style = SkPaint::kStroke_Style; |
| 899 | } else { |
| 900 | style = SkPaint::kFill_Style; |
| 901 | } |
| 902 | get_obj<SkPaint>(L, 1)->setStyle(style); |
| 903 | return 0; |
| 904 | } |
| 905 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 906 | static int lpaint_getStrokeCap(lua_State* L) { |
| 907 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap()); |
| 908 | return 1; |
| 909 | } |
| 910 | |
| 911 | static int lpaint_getStrokeJoin(lua_State* L) { |
| 912 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin()); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | static int lpaint_getTextEncoding(lua_State* L) { |
commit-bot@chromium.org | 641bcc3 | 2013-12-19 10:39:59 +0000 | [diff] [blame] | 917 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding()); |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 918 | return 1; |
| 919 | } |
| 920 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 921 | static int lpaint_getStrokeWidth(lua_State* L) { |
| 922 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); |
| 923 | return 1; |
| 924 | } |
| 925 | |
| 926 | static int lpaint_setStrokeWidth(lua_State* L) { |
| 927 | get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); |
| 928 | return 0; |
| 929 | } |
| 930 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 931 | static int lpaint_getStrokeMiter(lua_State* L) { |
| 932 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter()); |
| 933 | return 1; |
| 934 | } |
| 935 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 936 | static int lpaint_measureText(lua_State* L) { |
| 937 | if (lua_isstring(L, 2)) { |
| 938 | size_t len; |
| 939 | const char* text = lua_tolstring(L, 2, &len); |
| 940 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len)); |
| 941 | return 1; |
| 942 | } |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | struct FontMetrics { |
| 947 | SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) |
| 948 | SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) |
| 949 | SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) |
| 950 | SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) |
| 951 | SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) |
| 952 | SkScalar fAvgCharWidth; //!< the average charactor width (>= 0) |
| 953 | SkScalar fXMin; //!< The minimum bounding box x value for all glyphs |
| 954 | SkScalar fXMax; //!< The maximum bounding box x value for all glyphs |
| 955 | SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face |
| 956 | }; |
| 957 | |
| 958 | static int lpaint_getFontMetrics(lua_State* L) { |
| 959 | SkPaint::FontMetrics fm; |
| 960 | SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 961 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 962 | lua_newtable(L); |
| 963 | setfield_scalar(L, "top", fm.fTop); |
| 964 | setfield_scalar(L, "ascent", fm.fAscent); |
| 965 | setfield_scalar(L, "descent", fm.fDescent); |
| 966 | setfield_scalar(L, "bottom", fm.fBottom); |
| 967 | setfield_scalar(L, "leading", fm.fLeading); |
| 968 | SkLua(L).pushScalar(height); |
| 969 | return 2; |
| 970 | } |
| 971 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 972 | static int lpaint_getEffects(lua_State* L) { |
| 973 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 974 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 975 | lua_newtable(L); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 976 | setfield_bool_if(L, "looper", !!paint->getLooper()); |
| 977 | setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 978 | setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); |
| 979 | setfield_bool_if(L, "shader", !!paint->getShader()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 980 | setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); |
| 981 | setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 982 | return 1; |
| 983 | } |
| 984 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 985 | static int lpaint_getColorFilter(lua_State* L) { |
| 986 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 987 | SkColorFilter* cf = paint->getColorFilter(); |
| 988 | if (cf) { |
| 989 | push_ref(L, cf); |
| 990 | return 1; |
| 991 | } |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | static int lpaint_setColorFilter(lua_State* L) { |
| 996 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 997 | paint->setColorFilter(sk_ref_sp(get_ref<SkColorFilter>(L, 2))); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 998 | return 0; |
| 999 | } |
| 1000 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1001 | static int lpaint_getImageFilter(lua_State* L) { |
| 1002 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1003 | SkImageFilter* imf = paint->getImageFilter(); |
| 1004 | if (imf) { |
| 1005 | push_ref(L, imf); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static int lpaint_setImageFilter(lua_State* L) { |
| 1012 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 1013 | paint->setImageFilter(sk_ref_sp(get_ref<SkImageFilter>(L, 2))); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1014 | return 0; |
| 1015 | } |
| 1016 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1017 | static int lpaint_getShader(lua_State* L) { |
| 1018 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1019 | SkShader* shader = paint->getShader(); |
| 1020 | if (shader) { |
| 1021 | push_ref(L, shader); |
| 1022 | return 1; |
| 1023 | } |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1027 | static int lpaint_setShader(lua_State* L) { |
| 1028 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1029 | paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2))); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1033 | static int lpaint_getPathEffect(lua_State* L) { |
| 1034 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1035 | SkPathEffect* pe = paint->getPathEffect(); |
| 1036 | if (pe) { |
| 1037 | push_ref(L, pe); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1043 | static int lpaint_getFillPath(lua_State* L) { |
| 1044 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1045 | const SkPath* path = get_obj<SkPath>(L, 2); |
| 1046 | |
| 1047 | SkPath fillpath; |
| 1048 | paint->getFillPath(*path, &fillpath); |
| 1049 | |
| 1050 | SkLua lua(L); |
| 1051 | lua.pushPath(fillpath); |
| 1052 | |
| 1053 | return 1; |
| 1054 | } |
| 1055 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1056 | static int lpaint_gc(lua_State* L) { |
| 1057 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 1062 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 1063 | { "setAntiAlias", lpaint_setAntiAlias }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1064 | { "isDither", lpaint_isDither }, |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 1065 | { "setDither", lpaint_setDither }, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 1066 | { "getFilterQuality", lpaint_getFilterQuality }, |
| 1067 | { "setFilterQuality", lpaint_setFilterQuality }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1068 | { "isFakeBoldText", lpaint_isFakeBoldText }, |
| 1069 | { "isLinearText", lpaint_isLinearText }, |
| 1070 | { "isSubpixelText", lpaint_isSubpixelText }, |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 1071 | { "setSubpixelText", lpaint_setSubpixelText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1072 | { "isDevKernText", lpaint_isDevKernText }, |
| 1073 | { "isLCDRenderText", lpaint_isLCDRenderText }, |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1074 | { "setLCDRenderText", lpaint_setLCDRenderText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1075 | { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, |
| 1076 | { "isAutohinted", lpaint_isAutohinted }, |
| 1077 | { "isVerticalText", lpaint_isVerticalText }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1078 | { "getAlpha", lpaint_getAlpha }, |
| 1079 | { "setAlpha", lpaint_setAlpha }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1080 | { "getColor", lpaint_getColor }, |
| 1081 | { "setColor", lpaint_setColor }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1082 | { "getTextSize", lpaint_getTextSize }, |
| 1083 | { "setTextSize", lpaint_setTextSize }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1084 | { "getTextScaleX", lpaint_getTextScaleX }, |
| 1085 | { "getTextSkewX", lpaint_getTextSkewX }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1086 | { "getTypeface", lpaint_getTypeface }, |
| 1087 | { "setTypeface", lpaint_setTypeface }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1088 | { "getHinting", lpaint_getHinting }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1089 | { "getFontID", lpaint_getFontID }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1090 | { "getTextAlign", lpaint_getTextAlign }, |
| 1091 | { "setTextAlign", lpaint_setTextAlign }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1092 | { "getStroke", lpaint_getStroke }, |
| 1093 | { "setStroke", lpaint_setStroke }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1094 | { "getStrokeCap", lpaint_getStrokeCap }, |
| 1095 | { "getStrokeJoin", lpaint_getStrokeJoin }, |
| 1096 | { "getTextEncoding", lpaint_getTextEncoding }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1097 | { "getStrokeWidth", lpaint_getStrokeWidth }, |
| 1098 | { "setStrokeWidth", lpaint_setStrokeWidth }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1099 | { "getStrokeMiter", lpaint_getStrokeMiter }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1100 | { "measureText", lpaint_measureText }, |
| 1101 | { "getFontMetrics", lpaint_getFontMetrics }, |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1102 | { "getEffects", lpaint_getEffects }, |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1103 | { "getColorFilter", lpaint_getColorFilter }, |
| 1104 | { "setColorFilter", lpaint_setColorFilter }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1105 | { "getImageFilter", lpaint_getImageFilter }, |
| 1106 | { "setImageFilter", lpaint_setImageFilter }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1107 | { "getShader", lpaint_getShader }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1108 | { "setShader", lpaint_setShader }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1109 | { "getPathEffect", lpaint_getPathEffect }, |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1110 | { "getFillPath", lpaint_getFillPath }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1111 | { "__gc", lpaint_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1112 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | |
| 1115 | /////////////////////////////////////////////////////////////////////////////// |
| 1116 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1117 | static const char* mode2string(SkShader::TileMode mode) { |
| 1118 | static const char* gNames[] = { "clamp", "repeat", "mirror" }; |
| 1119 | SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); |
| 1120 | return gNames[mode]; |
| 1121 | } |
| 1122 | |
| 1123 | static const char* gradtype2string(SkShader::GradientType t) { |
| 1124 | static const char* gNames[] = { |
| 1125 | "none", "color", "linear", "radial", "radial2", "sweep", "conical" |
| 1126 | }; |
| 1127 | SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames)); |
| 1128 | return gNames[t]; |
| 1129 | } |
| 1130 | |
| 1131 | static int lshader_isOpaque(lua_State* L) { |
| 1132 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1133 | return shader && shader->isOpaque(); |
| 1134 | } |
| 1135 | |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1136 | static int lshader_isAImage(lua_State* L) { |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1137 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1138 | if (shader) { |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1139 | SkMatrix matrix; |
| 1140 | SkShader::TileMode modes[2]; |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1141 | if (SkImage* image = shader->isAImage(&matrix, modes)) { |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1142 | lua_newtable(L); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1143 | setfield_number(L, "id", image->uniqueID()); |
| 1144 | setfield_number(L, "width", image->width()); |
| 1145 | setfield_number(L, "height", image->height()); |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 1146 | setfield_string(L, "tileX", mode2string(modes[0])); |
| 1147 | setfield_string(L, "tileY", mode2string(modes[1])); |
| 1148 | return 1; |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static int lshader_asAGradient(lua_State* L) { |
| 1155 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1156 | if (shader) { |
| 1157 | SkShader::GradientInfo info; |
| 1158 | sk_bzero(&info, sizeof(info)); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1159 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1160 | SkShader::GradientType t = shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1161 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1162 | if (SkShader::kNone_GradientType != t) { |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1163 | SkAutoTArray<SkScalar> pos(info.fColorCount); |
| 1164 | info.fColorOffsets = pos.get(); |
| 1165 | shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1166 | |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1167 | lua_newtable(L); |
fmenozzi | 7f2c85e | 2016-07-12 09:17:39 -0700 | [diff] [blame] | 1168 | setfield_string(L, "type", gradtype2string(t)); |
| 1169 | setfield_string(L, "tile", mode2string(info.fTileMode)); |
| 1170 | setfield_number(L, "colorCount", info.fColorCount); |
fmenozzi | b4f254e | 2016-06-28 14:03:03 -0700 | [diff] [blame] | 1171 | |
| 1172 | lua_newtable(L); |
| 1173 | for (int i = 0; i < info.fColorCount; i++) { |
| 1174 | // Lua uses 1-based indexing |
| 1175 | setarray_scalar(L, i+1, pos[i]); |
| 1176 | } |
| 1177 | lua_setfield(L, -2, "positions"); |
| 1178 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1179 | return 1; |
| 1180 | } |
| 1181 | } |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static int lshader_gc(lua_State* L) { |
| 1186 | get_ref<SkShader>(L, 1)->unref(); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static const struct luaL_Reg gSkShader_Methods[] = { |
| 1191 | { "isOpaque", lshader_isOpaque }, |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 1192 | { "isAImage", lshader_isAImage }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1193 | { "asAGradient", lshader_asAGradient }, |
| 1194 | { "__gc", lshader_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1195 | { nullptr, nullptr } |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1196 | }; |
| 1197 | |
| 1198 | /////////////////////////////////////////////////////////////////////////////// |
| 1199 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1200 | static int lpatheffect_asADash(lua_State* L) { |
| 1201 | SkPathEffect* pe = get_ref<SkPathEffect>(L, 1); |
| 1202 | if (pe) { |
| 1203 | SkPathEffect::DashInfo info; |
| 1204 | SkPathEffect::DashType dashType = pe->asADash(&info); |
| 1205 | if (SkPathEffect::kDash_DashType == dashType) { |
| 1206 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 1207 | info.fIntervals = intervals.get(); |
| 1208 | pe->asADash(&info); |
| 1209 | SkLua(L).pushDash(info); |
| 1210 | return 1; |
| 1211 | } |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1216 | static int lpatheffect_gc(lua_State* L) { |
| 1217 | get_ref<SkPathEffect>(L, 1)->unref(); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static const struct luaL_Reg gSkPathEffect_Methods[] = { |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1222 | { "asADash", lpatheffect_asADash }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1223 | { "__gc", lpatheffect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1224 | { nullptr, nullptr } |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1225 | }; |
| 1226 | |
| 1227 | /////////////////////////////////////////////////////////////////////////////// |
| 1228 | |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 1229 | static int lpcolorfilter_gc(lua_State* L) { |
| 1230 | get_ref<SkColorFilter>(L, 1)->unref(); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static const struct luaL_Reg gSkColorFilter_Methods[] = { |
| 1235 | { "__gc", lpcolorfilter_gc }, |
| 1236 | { nullptr, nullptr } |
| 1237 | }; |
| 1238 | |
| 1239 | /////////////////////////////////////////////////////////////////////////////// |
| 1240 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1241 | static int lpimagefilter_gc(lua_State* L) { |
| 1242 | get_ref<SkImageFilter>(L, 1)->unref(); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static const struct luaL_Reg gSkImageFilter_Methods[] = { |
| 1247 | { "__gc", lpimagefilter_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1248 | { nullptr, nullptr } |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1249 | }; |
| 1250 | |
| 1251 | /////////////////////////////////////////////////////////////////////////////// |
| 1252 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1253 | static int lmatrix_getType(lua_State* L) { |
| 1254 | SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1255 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1256 | lua_newtable(L); |
| 1257 | setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask)); |
| 1258 | setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); |
| 1259 | setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); |
| 1260 | setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask)); |
| 1261 | return 1; |
| 1262 | } |
| 1263 | |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1264 | static int lmatrix_getScaleX(lua_State* L) { |
| 1265 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX()); |
| 1266 | return 1; |
| 1267 | } |
| 1268 | |
| 1269 | static int lmatrix_getScaleY(lua_State* L) { |
| 1270 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY()); |
| 1271 | return 1; |
| 1272 | } |
| 1273 | |
| 1274 | static int lmatrix_getTranslateX(lua_State* L) { |
| 1275 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX()); |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
| 1279 | static int lmatrix_getTranslateY(lua_State* L) { |
| 1280 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY()); |
| 1281 | return 1; |
| 1282 | } |
| 1283 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1284 | static int lmatrix_invert(lua_State* L) { |
| 1285 | lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2))); |
| 1286 | return 1; |
| 1287 | } |
| 1288 | |
| 1289 | static int lmatrix_mapXY(lua_State* L) { |
| 1290 | SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) }; |
| 1291 | get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1); |
| 1292 | lua_pushnumber(L, pt.x()); |
| 1293 | lua_pushnumber(L, pt.y()); |
| 1294 | return 2; |
| 1295 | } |
| 1296 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1297 | static int lmatrix_setRectToRect(lua_State* L) { |
| 1298 | SkMatrix* matrix = get_obj<SkMatrix>(L, 1); |
| 1299 | SkRect srcR, dstR; |
| 1300 | lua2rect(L, 2, &srcR); |
| 1301 | lua2rect(L, 3, &dstR); |
| 1302 | const char* scaleToFitStr = lua_tostring(L, 4); |
| 1303 | SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit; |
| 1304 | |
| 1305 | if (scaleToFitStr) { |
| 1306 | const struct { |
| 1307 | const char* fName; |
| 1308 | SkMatrix::ScaleToFit fScaleToFit; |
| 1309 | } rec[] = { |
| 1310 | { "fill", SkMatrix::kFill_ScaleToFit }, |
| 1311 | { "start", SkMatrix::kStart_ScaleToFit }, |
| 1312 | { "center", SkMatrix::kCenter_ScaleToFit }, |
| 1313 | { "end", SkMatrix::kEnd_ScaleToFit }, |
| 1314 | }; |
| 1315 | |
| 1316 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 1317 | if (strcmp(rec[i].fName, scaleToFitStr) == 0) { |
| 1318 | scaleToFit = rec[i].fScaleToFit; |
| 1319 | break; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | matrix->setRectToRect(srcR, dstR, scaleToFit); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1328 | static const struct luaL_Reg gSkMatrix_Methods[] = { |
| 1329 | { "getType", lmatrix_getType }, |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1330 | { "getScaleX", lmatrix_getScaleX }, |
| 1331 | { "getScaleY", lmatrix_getScaleY }, |
| 1332 | { "getTranslateX", lmatrix_getTranslateX }, |
| 1333 | { "getTranslateY", lmatrix_getTranslateY }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1334 | { "setRectToRect", lmatrix_setRectToRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1335 | { "invert", lmatrix_invert }, |
| 1336 | { "mapXY", lmatrix_mapXY }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1337 | { nullptr, nullptr } |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1338 | }; |
| 1339 | |
| 1340 | /////////////////////////////////////////////////////////////////////////////// |
| 1341 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1342 | static int lpath_getBounds(lua_State* L) { |
| 1343 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 1344 | return 1; |
| 1345 | } |
| 1346 | |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1347 | static const char* fill_type_to_str(SkPath::FillType fill) { |
| 1348 | switch (fill) { |
| 1349 | case SkPath::kEvenOdd_FillType: |
| 1350 | return "even-odd"; |
| 1351 | case SkPath::kWinding_FillType: |
| 1352 | return "winding"; |
| 1353 | case SkPath::kInverseEvenOdd_FillType: |
| 1354 | return "inverse-even-odd"; |
| 1355 | case SkPath::kInverseWinding_FillType: |
| 1356 | return "inverse-winding"; |
| 1357 | } |
| 1358 | return "unknown"; |
| 1359 | } |
| 1360 | |
| 1361 | static int lpath_getFillType(lua_State* L) { |
| 1362 | SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType(); |
| 1363 | SkLua(L).pushString(fill_type_to_str(fill)); |
| 1364 | return 1; |
| 1365 | } |
| 1366 | |
| 1367 | static SkString segment_masks_to_str(uint32_t segmentMasks) { |
| 1368 | SkString result; |
| 1369 | bool first = true; |
| 1370 | if (SkPath::kLine_SegmentMask & segmentMasks) { |
| 1371 | result.append("line"); |
| 1372 | first = false; |
| 1373 | SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;) |
| 1374 | } |
| 1375 | if (SkPath::kQuad_SegmentMask & segmentMasks) { |
| 1376 | if (!first) { |
| 1377 | result.append(" "); |
| 1378 | } |
| 1379 | result.append("quad"); |
| 1380 | first = false; |
| 1381 | SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;) |
| 1382 | } |
| 1383 | if (SkPath::kConic_SegmentMask & segmentMasks) { |
| 1384 | if (!first) { |
| 1385 | result.append(" "); |
| 1386 | } |
| 1387 | result.append("conic"); |
| 1388 | first = false; |
| 1389 | SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;) |
| 1390 | } |
| 1391 | if (SkPath::kCubic_SegmentMask & segmentMasks) { |
| 1392 | if (!first) { |
| 1393 | result.append(" "); |
| 1394 | } |
| 1395 | result.append("cubic"); |
| 1396 | SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;) |
| 1397 | } |
| 1398 | SkASSERT(0 == segmentMasks); |
| 1399 | return result; |
| 1400 | } |
| 1401 | |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1402 | static int lpath_getSegmentTypes(lua_State* L) { |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1403 | uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks(); |
| 1404 | SkLua(L).pushString(segment_masks_to_str(segMasks)); |
| 1405 | return 1; |
| 1406 | } |
| 1407 | |
| 1408 | static int lpath_isConvex(lua_State* L) { |
| 1409 | bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity(); |
| 1410 | SkLua(L).pushBool(isConvex); |
| 1411 | return 1; |
| 1412 | } |
| 1413 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1414 | static int lpath_isEmpty(lua_State* L) { |
| 1415 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 1416 | return 1; |
| 1417 | } |
| 1418 | |
| 1419 | static int lpath_isRect(lua_State* L) { |
| 1420 | SkRect r; |
| 1421 | bool pred = get_obj<SkPath>(L, 1)->isRect(&r); |
| 1422 | int ret_count = 1; |
| 1423 | lua_pushboolean(L, pred); |
| 1424 | if (pred) { |
| 1425 | SkLua(L).pushRect(r); |
| 1426 | ret_count += 1; |
| 1427 | } |
| 1428 | return ret_count; |
| 1429 | } |
| 1430 | |
| 1431 | static const char* dir2string(SkPath::Direction dir) { |
| 1432 | static const char* gStr[] = { |
| 1433 | "unknown", "cw", "ccw" |
| 1434 | }; |
| 1435 | SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 1436 | return gStr[dir]; |
| 1437 | } |
| 1438 | |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1439 | static int lpath_isNestedFillRects(lua_State* L) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1440 | SkRect rects[2]; |
| 1441 | SkPath::Direction dirs[2]; |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1442 | bool pred = get_obj<SkPath>(L, 1)->isNestedFillRects(rects, dirs); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1443 | int ret_count = 1; |
| 1444 | lua_pushboolean(L, pred); |
| 1445 | if (pred) { |
| 1446 | SkLua lua(L); |
| 1447 | lua.pushRect(rects[0]); |
| 1448 | lua.pushRect(rects[1]); |
| 1449 | lua_pushstring(L, dir2string(dirs[0])); |
| 1450 | lua_pushstring(L, dir2string(dirs[0])); |
| 1451 | ret_count += 4; |
| 1452 | } |
| 1453 | return ret_count; |
| 1454 | } |
| 1455 | |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1456 | static int lpath_countPoints(lua_State* L) { |
| 1457 | lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints()); |
| 1458 | return 1; |
| 1459 | } |
| 1460 | |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1461 | static int lpath_getVerbs(lua_State* L) { |
| 1462 | const SkPath* path = get_obj<SkPath>(L, 1); |
| 1463 | SkPath::Iter iter(*path, false); |
| 1464 | SkPoint pts[4]; |
| 1465 | |
| 1466 | lua_newtable(L); |
| 1467 | |
| 1468 | bool done = false; |
| 1469 | int i = 0; |
| 1470 | do { |
| 1471 | switch (iter.next(pts, true)) { |
| 1472 | case SkPath::kMove_Verb: |
| 1473 | setarray_string(L, ++i, "move"); |
| 1474 | break; |
| 1475 | case SkPath::kClose_Verb: |
| 1476 | setarray_string(L, ++i, "close"); |
| 1477 | break; |
| 1478 | case SkPath::kLine_Verb: |
| 1479 | setarray_string(L, ++i, "line"); |
| 1480 | break; |
| 1481 | case SkPath::kQuad_Verb: |
| 1482 | setarray_string(L, ++i, "quad"); |
| 1483 | break; |
| 1484 | case SkPath::kConic_Verb: |
| 1485 | setarray_string(L, ++i, "conic"); |
| 1486 | break; |
| 1487 | case SkPath::kCubic_Verb: |
| 1488 | setarray_string(L, ++i, "cubic"); |
| 1489 | break; |
| 1490 | case SkPath::kDone_Verb: |
| 1491 | setarray_string(L, ++i, "done"); |
| 1492 | done = true; |
| 1493 | break; |
| 1494 | } |
| 1495 | } while (!done); |
| 1496 | |
| 1497 | return 1; |
| 1498 | } |
| 1499 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1500 | static int lpath_reset(lua_State* L) { |
| 1501 | get_obj<SkPath>(L, 1)->reset(); |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | static int lpath_moveTo(lua_State* L) { |
| 1506 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | static int lpath_lineTo(lua_State* L) { |
| 1511 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static int lpath_quadTo(lua_State* L) { |
| 1516 | get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1517 | lua2scalar(L, 4), lua2scalar(L, 5)); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | static int lpath_cubicTo(lua_State* L) { |
| 1522 | get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1523 | lua2scalar(L, 4), lua2scalar(L, 5), |
| 1524 | lua2scalar(L, 6), lua2scalar(L, 7)); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int lpath_close(lua_State* L) { |
| 1529 | get_obj<SkPath>(L, 1)->close(); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | static int lpath_gc(lua_State* L) { |
| 1534 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 1539 | { "getBounds", lpath_getBounds }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1540 | { "getFillType", lpath_getFillType }, |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1541 | { "getSegmentTypes", lpath_getSegmentTypes }, |
hstern | 0b401ce | 2016-08-02 09:17:59 -0700 | [diff] [blame] | 1542 | { "getVerbs", lpath_getVerbs }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1543 | { "isConvex", lpath_isConvex }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1544 | { "isEmpty", lpath_isEmpty }, |
| 1545 | { "isRect", lpath_isRect }, |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1546 | { "isNestedFillRects", lpath_isNestedFillRects }, |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1547 | { "countPoints", lpath_countPoints }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1548 | { "reset", lpath_reset }, |
| 1549 | { "moveTo", lpath_moveTo }, |
| 1550 | { "lineTo", lpath_lineTo }, |
| 1551 | { "quadTo", lpath_quadTo }, |
| 1552 | { "cubicTo", lpath_cubicTo }, |
| 1553 | { "close", lpath_close }, |
| 1554 | { "__gc", lpath_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1555 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1556 | }; |
| 1557 | |
| 1558 | /////////////////////////////////////////////////////////////////////////////// |
| 1559 | |
| 1560 | static const char* rrect_type(const SkRRect& rr) { |
| 1561 | switch (rr.getType()) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1562 | case SkRRect::kEmpty_Type: return "empty"; |
| 1563 | case SkRRect::kRect_Type: return "rect"; |
| 1564 | case SkRRect::kOval_Type: return "oval"; |
| 1565 | case SkRRect::kSimple_Type: return "simple"; |
commit-bot@chromium.org | f338d7c | 2014-03-17 21:17:30 +0000 | [diff] [blame] | 1566 | case SkRRect::kNinePatch_Type: return "nine-patch"; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1567 | case SkRRect::kComplex_Type: return "complex"; |
| 1568 | } |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1569 | SkDEBUGFAIL("never get here"); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1570 | return ""; |
| 1571 | } |
| 1572 | |
| 1573 | static int lrrect_rect(lua_State* L) { |
| 1574 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
| 1578 | static int lrrect_type(lua_State* L) { |
| 1579 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 1580 | return 1; |
| 1581 | } |
| 1582 | |
| 1583 | static int lrrect_radii(lua_State* L) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 1584 | int corner = SkToInt(lua_tointeger(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1585 | SkVector v; |
| 1586 | if (corner < 0 || corner > 3) { |
| 1587 | SkDebugf("bad corner index %d", corner); |
| 1588 | v.set(0, 0); |
| 1589 | } else { |
| 1590 | v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner); |
| 1591 | } |
| 1592 | lua_pushnumber(L, v.fX); |
| 1593 | lua_pushnumber(L, v.fY); |
| 1594 | return 2; |
| 1595 | } |
| 1596 | |
| 1597 | static int lrrect_gc(lua_State* L) { |
| 1598 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 1603 | { "rect", lrrect_rect }, |
| 1604 | { "type", lrrect_type }, |
| 1605 | { "radii", lrrect_radii }, |
| 1606 | { "__gc", lrrect_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1607 | { nullptr, nullptr } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1608 | }; |
| 1609 | |
| 1610 | /////////////////////////////////////////////////////////////////////////////// |
| 1611 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1612 | static int limage_width(lua_State* L) { |
| 1613 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->width()); |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
| 1617 | static int limage_height(lua_State* L) { |
| 1618 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->height()); |
| 1619 | return 1; |
| 1620 | } |
| 1621 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1622 | static int limage_newShader(lua_State* L) { |
| 1623 | SkShader::TileMode tmode = SkShader::kClamp_TileMode; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1624 | const SkMatrix* localM = nullptr; |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 1625 | push_ref(L, get_ref<SkImage>(L, 1)->makeShader(tmode, tmode, localM)); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1626 | return 1; |
| 1627 | } |
| 1628 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1629 | static int limage_gc(lua_State* L) { |
| 1630 | get_ref<SkImage>(L, 1)->unref(); |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | static const struct luaL_Reg gSkImage_Methods[] = { |
| 1635 | { "width", limage_width }, |
| 1636 | { "height", limage_height }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1637 | { "newShader", limage_newShader }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1638 | { "__gc", limage_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1639 | { nullptr, nullptr } |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1640 | }; |
| 1641 | |
| 1642 | /////////////////////////////////////////////////////////////////////////////// |
| 1643 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1644 | static int lsurface_width(lua_State* L) { |
| 1645 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width()); |
| 1646 | return 1; |
| 1647 | } |
| 1648 | |
| 1649 | static int lsurface_height(lua_State* L) { |
| 1650 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height()); |
| 1651 | return 1; |
| 1652 | } |
| 1653 | |
| 1654 | static int lsurface_getCanvas(lua_State* L) { |
| 1655 | SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1656 | if (nullptr == canvas) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1657 | lua_pushnil(L); |
| 1658 | } else { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1659 | push_ptr(L, canvas); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1660 | // note: we don't unref canvas, since getCanvas did not ref it. |
| 1661 | // warning: this is weird: now Lua owns a ref on this canvas, but what if they let |
| 1662 | // the real owner (the surface) go away, but still hold onto the canvas? |
| 1663 | // *really* we want to sort of ref the surface again, but have the native object |
| 1664 | // know that it is supposed to be treated as a canvas... |
| 1665 | } |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | static int lsurface_newImageSnapshot(lua_State* L) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1670 | sk_sp<SkImage> image = get_ref<SkSurface>(L, 1)->makeImageSnapshot(); |
| 1671 | if (!image) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1672 | lua_pushnil(L); |
| 1673 | } else { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1674 | push_ref(L, image); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1675 | } |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | static int lsurface_newSurface(lua_State* L) { |
| 1680 | int width = lua2int_def(L, 2, 0); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1681 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1682 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1683 | auto surface = get_ref<SkSurface>(L, 1)->makeSurface(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1684 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1685 | lua_pushnil(L); |
| 1686 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1687 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1688 | } |
| 1689 | return 1; |
| 1690 | } |
| 1691 | |
| 1692 | static int lsurface_gc(lua_State* L) { |
| 1693 | get_ref<SkSurface>(L, 1)->unref(); |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | static const struct luaL_Reg gSkSurface_Methods[] = { |
| 1698 | { "width", lsurface_width }, |
| 1699 | { "height", lsurface_height }, |
| 1700 | { "getCanvas", lsurface_getCanvas }, |
| 1701 | { "newImageSnapshot", lsurface_newImageSnapshot }, |
| 1702 | { "newSurface", lsurface_newSurface }, |
| 1703 | { "__gc", lsurface_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1704 | { nullptr, nullptr } |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1705 | }; |
| 1706 | |
| 1707 | /////////////////////////////////////////////////////////////////////////////// |
| 1708 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1709 | static int lpicturerecorder_beginRecording(lua_State* L) { |
| 1710 | const SkScalar w = lua2scalar_def(L, 2, -1); |
| 1711 | const SkScalar h = lua2scalar_def(L, 3, -1); |
| 1712 | if (w <= 0 || h <= 0) { |
| 1713 | lua_pushnil(L); |
| 1714 | return 1; |
| 1715 | } |
| 1716 | |
| 1717 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1718 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1719 | lua_pushnil(L); |
| 1720 | return 1; |
| 1721 | } |
| 1722 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1723 | push_ptr(L, canvas); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1724 | return 1; |
| 1725 | } |
| 1726 | |
| 1727 | static int lpicturerecorder_getCanvas(lua_State* L) { |
| 1728 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1729 | if (nullptr == canvas) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1730 | lua_pushnil(L); |
| 1731 | return 1; |
| 1732 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 1733 | push_ptr(L, canvas); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1734 | return 1; |
| 1735 | } |
| 1736 | |
| 1737 | static int lpicturerecorder_endRecording(lua_State* L) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1738 | sk_sp<SkPicture> pic = get_obj<SkPictureRecorder>(L, 1)->finishRecordingAsPicture(); |
| 1739 | if (!pic) { |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1740 | lua_pushnil(L); |
| 1741 | return 1; |
| 1742 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1743 | push_ref(L, std::move(pic)); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1744 | return 1; |
| 1745 | } |
| 1746 | |
| 1747 | static int lpicturerecorder_gc(lua_State* L) { |
| 1748 | get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder(); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | static const struct luaL_Reg gSkPictureRecorder_Methods[] = { |
| 1753 | { "beginRecording", lpicturerecorder_beginRecording }, |
| 1754 | { "getCanvas", lpicturerecorder_getCanvas }, |
| 1755 | { "endRecording", lpicturerecorder_endRecording }, |
| 1756 | { "__gc", lpicturerecorder_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1757 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1758 | }; |
| 1759 | |
| 1760 | /////////////////////////////////////////////////////////////////////////////// |
| 1761 | |
| 1762 | static int lpicture_width(lua_State* L) { |
| 1763 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width()); |
| 1764 | return 1; |
| 1765 | } |
| 1766 | |
| 1767 | static int lpicture_height(lua_State* L) { |
| 1768 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height()); |
| 1769 | return 1; |
| 1770 | } |
| 1771 | |
| 1772 | static int lpicture_gc(lua_State* L) { |
| 1773 | get_ref<SkPicture>(L, 1)->unref(); |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | static const struct luaL_Reg gSkPicture_Methods[] = { |
| 1778 | { "width", lpicture_width }, |
| 1779 | { "height", lpicture_height }, |
| 1780 | { "__gc", lpicture_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1781 | { nullptr, nullptr } |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1782 | }; |
| 1783 | |
| 1784 | /////////////////////////////////////////////////////////////////////////////// |
| 1785 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1786 | static int ltextblob_bounds(lua_State* L) { |
| 1787 | SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds()); |
| 1788 | return 1; |
| 1789 | } |
| 1790 | |
| 1791 | static int ltextblob_gc(lua_State* L) { |
| 1792 | SkSafeUnref(get_ref<SkTextBlob>(L, 1)); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | static const struct luaL_Reg gSkTextBlob_Methods[] = { |
| 1797 | { "bounds", ltextblob_bounds }, |
| 1798 | { "__gc", ltextblob_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1799 | { nullptr, nullptr } |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1800 | }; |
| 1801 | |
| 1802 | /////////////////////////////////////////////////////////////////////////////// |
| 1803 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1804 | static int ltypeface_getFamilyName(lua_State* L) { |
| 1805 | SkString str; |
| 1806 | get_ref<SkTypeface>(L, 1)->getFamilyName(&str); |
| 1807 | lua_pushstring(L, str.c_str()); |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
| 1811 | static int ltypeface_getStyle(lua_State* L) { |
Ben Wagner | 26308e1 | 2017-08-08 15:23:47 -0400 | [diff] [blame] | 1812 | push_obj(L, get_ref<SkTypeface>(L, 1)->fontStyle()); |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1813 | return 1; |
| 1814 | } |
| 1815 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1816 | static int ltypeface_gc(lua_State* L) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 1817 | SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | static const struct luaL_Reg gSkTypeface_Methods[] = { |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1822 | { "getFamilyName", ltypeface_getFamilyName }, |
| 1823 | { "getStyle", ltypeface_getStyle }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1824 | { "__gc", ltypeface_gc }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1825 | { nullptr, nullptr } |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1826 | }; |
| 1827 | |
| 1828 | /////////////////////////////////////////////////////////////////////////////// |
| 1829 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1830 | static int lfontstyle_weight(lua_State* L) { |
| 1831 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->weight()); |
| 1832 | return 1; |
| 1833 | } |
| 1834 | |
| 1835 | static int lfontstyle_width(lua_State* L) { |
| 1836 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->width()); |
| 1837 | return 1; |
| 1838 | } |
| 1839 | |
| 1840 | static int lfontstyle_slant(lua_State* L) { |
| 1841 | lua_pushnumber(L, get_ref<SkFontStyle>(L, 1)->slant()); |
| 1842 | return 1; |
| 1843 | } |
| 1844 | |
| 1845 | static int lfontstyle_gc(lua_State* L) { |
| 1846 | get_obj<SkFontStyle>(L, 1)->~SkFontStyle(); |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static const struct luaL_Reg gSkFontStyle_Methods[] = { |
| 1851 | { "weight", lfontstyle_weight }, |
| 1852 | { "width", lfontstyle_width }, |
| 1853 | { "slant", lfontstyle_slant }, |
| 1854 | { "__gc", lfontstyle_gc }, |
| 1855 | { nullptr, nullptr } |
| 1856 | }; |
| 1857 | |
| 1858 | /////////////////////////////////////////////////////////////////////////////// |
| 1859 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1860 | class AutoCallLua { |
| 1861 | public: |
| 1862 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) { |
| 1863 | lua_getglobal(L, func); |
| 1864 | if (!lua_isfunction(L, -1)) { |
| 1865 | int t = lua_type(L, -1); |
| 1866 | SkDebugf("--- expected function %d\n", t); |
| 1867 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1868 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1869 | lua_newtable(L); |
| 1870 | setfield_string(L, "verb", verb); |
| 1871 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1872 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1873 | ~AutoCallLua() { |
| 1874 | if (lua_pcall(fL, 1, 0, 0) != LUA_OK) { |
| 1875 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 1876 | } |
| 1877 | lua_settop(fL, -1); |
| 1878 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1879 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1880 | private: |
| 1881 | lua_State* fL; |
| 1882 | }; |
| 1883 | |
| 1884 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 1885 | |
| 1886 | /////////////////////////////////////////////////////////////////////////////// |
| 1887 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1888 | static int lsk_newDocumentPDF(lua_State* L) { |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 1889 | const char* filename = nullptr; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1890 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 1891 | filename = lua_tolstring(L, 1, nullptr); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1892 | } |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 1893 | if (!filename) { |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1894 | return 0; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1895 | } |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 1896 | auto file = skstd::make_unique<SkFILEWStream>(filename); |
| 1897 | if (!file->isValid()) { |
| 1898 | return 0; |
| 1899 | } |
| 1900 | sk_sp<SkDocument> doc = SkDocument::MakePDF(file.get()); |
| 1901 | if (!doc) { |
| 1902 | return 0; |
| 1903 | } |
| 1904 | push_ptr(L, new DocHolder{std::move(doc), std::move(file)}); |
| 1905 | return 1; |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1908 | static int lsk_newBlurImageFilter(lua_State* L) { |
| 1909 | SkScalar sigmaX = lua2scalar_def(L, 1, 0); |
| 1910 | SkScalar sigmaY = lua2scalar_def(L, 2, 0); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1911 | sk_sp<SkImageFilter> imf(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr)); |
| 1912 | if (!imf) { |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1913 | lua_pushnil(L); |
| 1914 | } else { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1915 | push_ref(L, std::move(imf)); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1916 | } |
| 1917 | return 1; |
| 1918 | } |
| 1919 | |
| 1920 | static int lsk_newLinearGradient(lua_State* L) { |
| 1921 | SkScalar x0 = lua2scalar_def(L, 1, 0); |
| 1922 | SkScalar y0 = lua2scalar_def(L, 2, 0); |
| 1923 | SkColor c0 = lua2color(L, 3); |
| 1924 | SkScalar x1 = lua2scalar_def(L, 4, 0); |
| 1925 | SkScalar y1 = lua2scalar_def(L, 5, 0); |
| 1926 | SkColor c1 = lua2color(L, 6); |
| 1927 | |
| 1928 | SkPoint pts[] = { { x0, y0 }, { x1, y1 } }; |
| 1929 | SkColor colors[] = { c0, c1 }; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1930 | sk_sp<SkShader> s(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 1931 | SkShader::kClamp_TileMode)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 1932 | if (!s) { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1933 | lua_pushnil(L); |
| 1934 | } else { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 1935 | push_ref(L, std::move(s)); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1936 | } |
| 1937 | return 1; |
| 1938 | } |
| 1939 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1940 | static int lsk_newMatrix(lua_State* L) { |
| 1941 | push_new<SkMatrix>(L)->reset(); |
| 1942 | return 1; |
| 1943 | } |
| 1944 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1945 | static int lsk_newPaint(lua_State* L) { |
| 1946 | push_new<SkPaint>(L); |
| 1947 | return 1; |
| 1948 | } |
| 1949 | |
| 1950 | static int lsk_newPath(lua_State* L) { |
| 1951 | push_new<SkPath>(L); |
| 1952 | return 1; |
| 1953 | } |
| 1954 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1955 | static int lsk_newPictureRecorder(lua_State* L) { |
| 1956 | push_new<SkPictureRecorder>(L); |
| 1957 | return 1; |
| 1958 | } |
| 1959 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1960 | static int lsk_newRRect(lua_State* L) { |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1961 | push_new<SkRRect>(L)->setEmpty(); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1962 | return 1; |
| 1963 | } |
| 1964 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1965 | // Sk.newTextBlob(text, rect, paint) |
| 1966 | static int lsk_newTextBlob(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1967 | const char* text = lua_tolstring(L, 1, nullptr); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1968 | SkRect bounds; |
| 1969 | lua2rect(L, 2, &bounds); |
| 1970 | const SkPaint& paint = *get_obj<SkPaint>(L, 3); |
| 1971 | |
Herb Derby | 1724db1 | 2018-05-22 12:01:50 -0400 | [diff] [blame] | 1972 | SkShaper shaper(nullptr); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1973 | |
Herb Derby | 1724db1 | 2018-05-22 12:01:50 -0400 | [diff] [blame] | 1974 | SkTextBlobBuilder builder; |
| 1975 | SkPoint end = shaper.shape(&builder, paint, text, strlen(text), true, |
| 1976 | { bounds.left(), bounds.top() }, bounds.width()); |
| 1977 | |
| 1978 | push_ref<SkTextBlob>(L, builder.make()); |
| 1979 | SkLua(L).pushScalar(end.fY); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1980 | return 2; |
| 1981 | } |
| 1982 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1983 | static int lsk_newTypeface(lua_State* L) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1984 | const char* name = nullptr; |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1985 | SkFontStyle style; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 1986 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1987 | int count = lua_gettop(L); |
| 1988 | if (count > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1989 | name = lua_tolstring(L, 1, nullptr); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1990 | if (count > 1) { |
| 1991 | SkFontStyle* passedStyle = get_obj<SkFontStyle>(L, 2); |
| 1992 | if (passedStyle) { |
| 1993 | style = *passedStyle; |
| 1994 | } |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 1998 | sk_sp<SkTypeface> face(SkTypeface::MakeFromName(name, style)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1999 | // 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] | 2000 | if (nullptr == face) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 2001 | face = SkTypeface::MakeDefault(); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2002 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 2003 | push_ref(L, std::move(face)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2004 | return 1; |
| 2005 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2006 | |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 2007 | static int lsk_newFontStyle(lua_State* L) { |
| 2008 | int count = lua_gettop(L); |
| 2009 | int weight = SkFontStyle::kNormal_Weight; |
| 2010 | int width = SkFontStyle::kNormal_Width; |
| 2011 | SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant; |
| 2012 | if (count >= 1 && lua_isnumber(L, 1)) { |
| 2013 | weight = lua_tointegerx(L, 1, nullptr); |
| 2014 | } |
| 2015 | if (count >= 2 && lua_isnumber(L, 2)) { |
| 2016 | width = lua_tointegerx(L, 2, nullptr); |
| 2017 | } |
| 2018 | if (count >= 3 && lua_isnumber(L, 3)) { |
| 2019 | slant = static_cast<SkFontStyle::Slant>(lua_tointegerx(L, 3, nullptr)); |
| 2020 | } |
| 2021 | push_new<SkFontStyle>(L, weight, width, slant); |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2025 | static int lsk_newRasterSurface(lua_State* L) { |
reed | 7b86466 | 2014-11-04 13:24:47 -0800 | [diff] [blame] | 2026 | int width = lua2int_def(L, 1, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2027 | int height = lua2int_def(L, 2, 0); |
| 2028 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 2029 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2030 | auto surface = SkSurface::MakeRaster(info, &props); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2031 | if (nullptr == surface) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2032 | lua_pushnil(L); |
| 2033 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2034 | push_ref(L, surface); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2035 | } |
| 2036 | return 1; |
| 2037 | } |
| 2038 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2039 | static int lsk_loadImage(lua_State* L) { |
| 2040 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2041 | const char* name = lua_tolstring(L, 1, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2042 | sk_sp<SkData> data(SkData::MakeFromFileName(name)); |
| 2043 | if (data) { |
| 2044 | auto image = SkImage::MakeFromEncoded(std::move(data)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2045 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 2046 | push_ref(L, std::move(image)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2047 | return 1; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2054 | static void register_Sk(lua_State* L) { |
| 2055 | lua_newtable(L); |
| 2056 | lua_pushvalue(L, -1); |
| 2057 | lua_setglobal(L, "Sk"); |
| 2058 | // the Sk table is still on top |
| 2059 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2060 | setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2061 | setfield_function(L, "loadImage", lsk_loadImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2062 | setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2063 | setfield_function(L, "newLinearGradient", lsk_newLinearGradient); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2064 | setfield_function(L, "newMatrix", lsk_newMatrix); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2065 | setfield_function(L, "newPaint", lsk_newPaint); |
| 2066 | setfield_function(L, "newPath", lsk_newPath); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2067 | setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2068 | setfield_function(L, "newRRect", lsk_newRRect); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2069 | setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2070 | setfield_function(L, "newTextBlob", lsk_newTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2071 | setfield_function(L, "newTypeface", lsk_newTypeface); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 2072 | setfield_function(L, "newFontStyle", lsk_newFontStyle); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2073 | lua_pop(L, 1); // pop off the Sk table |
| 2074 | } |
| 2075 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2076 | #define REG_CLASS(L, C) \ |
| 2077 | do { \ |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2078 | luaL_newmetatable(L, get_mtname<C>()); \ |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2079 | lua_pushvalue(L, -1); \ |
| 2080 | lua_setfield(L, -2, "__index"); \ |
| 2081 | luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 2082 | lua_pop(L, 1); /* pop off the meta-table */ \ |
| 2083 | } while (0) |
| 2084 | |
| 2085 | void SkLua::Load(lua_State* L) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2086 | register_Sk(L); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2087 | REG_CLASS(L, SkCanvas); |
reed | 22a517f | 2015-12-04 20:45:59 -0800 | [diff] [blame] | 2088 | REG_CLASS(L, SkColorFilter); |
Mike Reed | 7ff6ca5 | 2018-01-08 14:45:31 -0500 | [diff] [blame] | 2089 | REG_CLASS(L, DocHolder); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2090 | REG_CLASS(L, SkImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2091 | REG_CLASS(L, SkImageFilter); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2092 | REG_CLASS(L, SkMatrix); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2093 | REG_CLASS(L, SkPaint); |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 2094 | REG_CLASS(L, SkPath); |
| 2095 | REG_CLASS(L, SkPathEffect); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2096 | REG_CLASS(L, SkPicture); |
| 2097 | REG_CLASS(L, SkPictureRecorder); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2098 | REG_CLASS(L, SkRRect); |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 2099 | REG_CLASS(L, SkShader); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2100 | REG_CLASS(L, SkSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2101 | REG_CLASS(L, SkTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2102 | REG_CLASS(L, SkTypeface); |
Ben Wagner | c6c10b4 | 2017-08-07 09:56:21 -0400 | [diff] [blame] | 2103 | REG_CLASS(L, SkFontStyle); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2104 | } |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2105 | |
reed@google.com | 7bce998 | 2013-06-20 17:40:21 +0000 | [diff] [blame] | 2106 | extern "C" int luaopen_skia(lua_State* L); |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2107 | extern "C" int luaopen_skia(lua_State* L) { |
| 2108 | SkLua::Load(L); |
| 2109 | return 0; |
| 2110 | } |