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