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