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