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