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