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