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