| 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" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkRRect.h" |
| 14 | #include "SkString.h" |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 15 | #include "SkTypeface.h" |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 16 | |
| 17 | extern "C" { |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 18 | #include "lua.h" |
| 19 | #include "lualib.h" |
| 20 | #include "lauxlib.h" |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 23 | // return the metatable name for a given class |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 24 | template <typename T> const char* get_mtname(); |
| reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 25 | #define DEF_MTNAME(T) \ |
| 26 | template <> const char* get_mtname<T>() { \ |
| 27 | return #T "_LuaMetaTableName"; \ |
| 28 | } |
| 29 | |
| 30 | DEF_MTNAME(SkCanvas) |
| 31 | DEF_MTNAME(SkMatrix) |
| 32 | DEF_MTNAME(SkRRect) |
| 33 | DEF_MTNAME(SkPath) |
| 34 | DEF_MTNAME(SkPaint) |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 35 | |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 36 | template <typename T> T* push_new(lua_State* L) { |
| 37 | T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
| 38 | new (addr) T; |
| 39 | luaL_getmetatable(L, get_mtname<T>()); |
| 40 | lua_setmetatable(L, -2); |
| 41 | return addr; |
| 42 | } |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 43 | |
| 44 | template <typename T> void push_obj(lua_State* L, const T& obj) { |
| 45 | new (lua_newuserdata(L, sizeof(T))) T(obj); |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 46 | luaL_getmetatable(L, get_mtname<T>()); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 47 | lua_setmetatable(L, -2); |
| 48 | } |
| 49 | |
| 50 | template <typename T> void push_ref(lua_State* L, T* ref) { |
| 51 | *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref); |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 52 | luaL_getmetatable(L, get_mtname<T>()); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 53 | lua_setmetatable(L, -2); |
| 54 | } |
| 55 | |
| 56 | template <typename T> T* get_ref(lua_State* L, int index) { |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 57 | return *(T**)luaL_checkudata(L, index, get_mtname<T>()); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | template <typename T> T* get_obj(lua_State* L, int index) { |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 61 | return (T*)luaL_checkudata(L, index, get_mtname<T>()); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 64 | static bool lua2bool(lua_State* L, int index) { |
| 65 | return !!lua_toboolean(L, index); |
| 66 | } |
| 67 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 70 | SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) { |
| 71 | fL = luaL_newstate(); |
| 72 | luaL_openlibs(fL); |
| 73 | SkLua::Load(fL); |
| 74 | } |
| 75 | |
| 76 | SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {} |
| 77 | |
| 78 | SkLua::~SkLua() { |
| 79 | if (fWeOwnL) { |
| 80 | if (fTermCode.size() > 0) { |
| 81 | lua_getglobal(fL, fTermCode.c_str()); |
| 82 | if (lua_pcall(fL, 0, 0, 0) != LUA_OK) { |
| 83 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 84 | } |
| 85 | } |
| 86 | lua_close(fL); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | bool SkLua::runCode(const char code[]) { |
| 91 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 92 | if (err) { |
| 93 | SkDebugf("--- lua failed\n"); |
| 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool SkLua::runCode(const void* code, size_t size) { |
| 100 | SkString str((const char*)code, size); |
| 101 | return this->runCode(str.c_str()); |
| 102 | } |
| 103 | |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0) |
| 107 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 108 | static void setfield_string(lua_State* L, const char key[], const char value[]) { |
| 109 | lua_pushstring(L, value); |
| 110 | lua_setfield(L, -2, key); |
| 111 | } |
| 112 | |
| 113 | static void setfield_number(lua_State* L, const char key[], double value) { |
| 114 | lua_pushnumber(L, value); |
| 115 | lua_setfield(L, -2, key); |
| 116 | } |
| 117 | |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 118 | static void setfield_function(lua_State* L, |
| 119 | const char key[], lua_CFunction value) { |
| 120 | lua_pushcfunction(L, value); |
| 121 | lua_setfield(L, -2, key); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 124 | static void setarray_number(lua_State* L, int index, double value) { |
| 125 | lua_pushnumber(L, value); |
| 126 | lua_rawseti(L, -2, index); |
| 127 | } |
| 128 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 129 | void SkLua::pushBool(bool value, const char key[]) { |
| 130 | lua_pushboolean(fL, value); |
| 131 | CHECK_SETFIELD(key); |
| 132 | } |
| 133 | |
| 134 | void SkLua::pushString(const char str[], const char key[]) { |
| 135 | lua_pushstring(fL, str); |
| 136 | CHECK_SETFIELD(key); |
| 137 | } |
| 138 | |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 139 | void SkLua::pushString(const char str[], size_t length, const char key[]) { |
| 140 | // TODO: how to do this w/o making a copy? |
| 141 | SkString s(str, length); |
| 142 | lua_pushstring(fL, s.c_str()); |
| 143 | CHECK_SETFIELD(key); |
| 144 | } |
| 145 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 146 | void SkLua::pushString(const SkString& str, const char key[]) { |
| 147 | lua_pushstring(fL, str.c_str()); |
| 148 | CHECK_SETFIELD(key); |
| 149 | } |
| 150 | |
| 151 | void SkLua::pushColor(SkColor color, const char key[]) { |
| 152 | lua_newtable(fL); |
| 153 | setfield_number(fL, "a", SkColorGetA(color) / 255.0); |
| 154 | setfield_number(fL, "r", SkColorGetR(color) / 255.0); |
| 155 | setfield_number(fL, "g", SkColorGetG(color) / 255.0); |
| 156 | setfield_number(fL, "b", SkColorGetB(color) / 255.0); |
| 157 | CHECK_SETFIELD(key); |
| 158 | } |
| 159 | |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 160 | void SkLua::pushU32(uint32_t value, const char key[]) { |
| 161 | lua_pushnumber(fL, (double)value); |
| 162 | CHECK_SETFIELD(key); |
| 163 | } |
| 164 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 165 | void SkLua::pushScalar(SkScalar value, const char key[]) { |
| 166 | lua_pushnumber(fL, SkScalarToLua(value)); |
| 167 | CHECK_SETFIELD(key); |
| 168 | } |
| 169 | |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 170 | void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { |
| 171 | lua_newtable(fL); |
| 172 | for (int i = 0; i < count; ++i) { |
| 173 | // make it base-1 to match lua convention |
| 174 | setarray_number(fL, i + 1, (double)array[i]); |
| 175 | } |
| 176 | CHECK_SETFIELD(key); |
| 177 | } |
| 178 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 179 | void SkLua::pushRect(const SkRect& r, const char key[]) { |
| 180 | lua_newtable(fL); |
| 181 | setfield_number(fL, "left", SkScalarToLua(r.fLeft)); |
| 182 | setfield_number(fL, "top", SkScalarToLua(r.fTop)); |
| 183 | setfield_number(fL, "right", SkScalarToLua(r.fRight)); |
| 184 | setfield_number(fL, "bottom", SkScalarToLua(r.fBottom)); |
| 185 | CHECK_SETFIELD(key); |
| 186 | } |
| 187 | |
| 188 | void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
| 189 | push_obj(fL, rr); |
| 190 | CHECK_SETFIELD(key); |
| 191 | } |
| 192 | |
| 193 | void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) { |
| 194 | push_obj(fL, matrix); |
| 195 | CHECK_SETFIELD(key); |
| 196 | } |
| 197 | |
| 198 | void SkLua::pushPaint(const SkPaint& paint, const char key[]) { |
| 199 | push_obj(fL, paint); |
| 200 | CHECK_SETFIELD(key); |
| 201 | } |
| 202 | |
| 203 | void SkLua::pushPath(const SkPath& path, const char key[]) { |
| 204 | push_obj(fL, path); |
| 205 | CHECK_SETFIELD(key); |
| 206 | } |
| 207 | |
| 208 | void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) { |
| 209 | push_ref(fL, canvas); |
| 210 | CHECK_SETFIELD(key); |
| 211 | } |
| 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| 216 | static SkScalar lua2scalar(lua_State* L, int index) { |
| 217 | SkASSERT(lua_isnumber(L, index)); |
| 218 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 219 | } |
| 220 | |
| 221 | static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) { |
| 222 | SkASSERT(lua_istable(L, index)); |
| 223 | lua_pushstring(L, key); |
| 224 | lua_gettable(L, index); |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 225 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 226 | SkScalar value = lua2scalar(L, -1); |
| 227 | lua_pop(L, 1); |
| 228 | return value; |
| 229 | } |
| 230 | |
| 231 | static U8CPU unit2byte(SkScalar x) { |
| 232 | if (x <= 0) { |
| 233 | return 0; |
| 234 | } else if (x >= 1) { |
| 235 | return 255; |
| 236 | } else { |
| 237 | return SkScalarRoundToInt(x * 255); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static SkColor lua2color(lua_State* L, int index) { |
| 242 | return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")), |
| 243 | unit2byte(getfield_scalar(L, index, "r")), |
| 244 | unit2byte(getfield_scalar(L, index, "g")), |
| 245 | unit2byte(getfield_scalar(L, index, "b"))); |
| 246 | } |
| 247 | |
| 248 | static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
| 249 | rect->set(getfield_scalar(L, index, "left"), |
| 250 | getfield_scalar(L, index, "top"), |
| 251 | getfield_scalar(L, index, "right"), |
| 252 | getfield_scalar(L, index, "bottom")); |
| 253 | return rect; |
| 254 | } |
| 255 | |
| 256 | static int lcanvas_drawColor(lua_State* L) { |
| 257 | get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2)); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int lcanvas_drawRect(lua_State* L) { |
| 262 | SkRect rect; |
| 263 | get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect), |
| 264 | *get_obj<SkPaint>(L, 3)); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int lcanvas_drawOval(lua_State* L) { |
| 269 | SkRect rect; |
| 270 | get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect), |
| 271 | *get_obj<SkPaint>(L, 3)); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int lcanvas_drawCircle(lua_State* L) { |
| 276 | get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
| 277 | lua2scalar(L, 3), |
| 278 | lua2scalar(L, 4), |
| 279 | *get_obj<SkPaint>(L, 5)); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 283 | static int lcanvas_drawPath(lua_State* L) { |
| 284 | get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 285 | *get_obj<SkPaint>(L, 3)); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 289 | static int lcanvas_getSaveCount(lua_State* L) { |
| 290 | lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | static int lcanvas_getTotalMatrix(lua_State* L) { |
| 295 | SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
| 296 | return 1; |
| 297 | } |
| 298 | |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 299 | static int lcanvas_translate(lua_State* L) { |
| 300 | get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 304 | static int lcanvas_gc(lua_State* L) { |
| 305 | get_ref<SkCanvas>(L, 1)->unref(); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static const struct luaL_Reg gSkCanvas_Methods[] = { |
| 310 | { "drawColor", lcanvas_drawColor }, |
| 311 | { "drawRect", lcanvas_drawRect }, |
| 312 | { "drawOval", lcanvas_drawOval }, |
| 313 | { "drawCircle", lcanvas_drawCircle }, |
| reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 314 | { "drawPath", lcanvas_drawPath }, |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 315 | { "getSaveCount", lcanvas_getSaveCount }, |
| 316 | { "getTotalMatrix", lcanvas_getTotalMatrix }, |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 317 | { "translate", lcanvas_translate }, |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 318 | { "__gc", lcanvas_gc }, |
| 319 | { NULL, NULL } |
| 320 | }; |
| 321 | |
| 322 | /////////////////////////////////////////////////////////////////////////////// |
| 323 | |
| 324 | static int lpaint_isAntiAlias(lua_State* L) { |
| 325 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias()); |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | static int lpaint_setAntiAlias(lua_State* L) { |
| reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 330 | get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2)); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int lpaint_getColor(lua_State* L) { |
| 335 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | static int lpaint_setColor(lua_State* L) { |
| 340 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 344 | static int lpaint_getTextSize(lua_State* L) { |
| 345 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 346 | return 1; |
| 347 | } |
| 348 | |
| 349 | static int lpaint_setTextSize(lua_State* L) { |
| 350 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int lpaint_getFontID(lua_State* L) { |
| 355 | SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface(); |
| 356 | SkLua(L).pushU32(SkTypeface::UniqueID(face)); |
| 357 | return 1; |
| 358 | } |
| 359 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 360 | static int lpaint_gc(lua_State* L) { |
| 361 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 366 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 367 | { "setAntiAlias", lpaint_setAntiAlias }, |
| 368 | { "getColor", lpaint_getColor }, |
| 369 | { "setColor", lpaint_setColor }, |
| reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 370 | { "getTextSize", lpaint_getTextSize }, |
| 371 | { "setTextSize", lpaint_setTextSize }, |
| 372 | { "getFontID", lpaint_getFontID }, |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 373 | { "__gc", lpaint_gc }, |
| 374 | { NULL, NULL } |
| 375 | }; |
| 376 | |
| 377 | /////////////////////////////////////////////////////////////////////////////// |
| 378 | |
| 379 | static int lpath_getBounds(lua_State* L) { |
| 380 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | static int lpath_isEmpty(lua_State* L) { |
| 385 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | static int lpath_isRect(lua_State* L) { |
| 390 | SkRect r; |
| 391 | bool pred = get_obj<SkPath>(L, 1)->isRect(&r); |
| 392 | int ret_count = 1; |
| 393 | lua_pushboolean(L, pred); |
| 394 | if (pred) { |
| 395 | SkLua(L).pushRect(r); |
| 396 | ret_count += 1; |
| 397 | } |
| 398 | return ret_count; |
| 399 | } |
| 400 | |
| 401 | static const char* dir2string(SkPath::Direction dir) { |
| 402 | static const char* gStr[] = { |
| 403 | "unknown", "cw", "ccw" |
| 404 | }; |
| 405 | SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 406 | return gStr[dir]; |
| 407 | } |
| 408 | |
| 409 | static int lpath_isNestedRects(lua_State* L) { |
| 410 | SkRect rects[2]; |
| 411 | SkPath::Direction dirs[2]; |
| 412 | bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs); |
| 413 | int ret_count = 1; |
| 414 | lua_pushboolean(L, pred); |
| 415 | if (pred) { |
| 416 | SkLua lua(L); |
| 417 | lua.pushRect(rects[0]); |
| 418 | lua.pushRect(rects[1]); |
| 419 | lua_pushstring(L, dir2string(dirs[0])); |
| 420 | lua_pushstring(L, dir2string(dirs[0])); |
| 421 | ret_count += 4; |
| 422 | } |
| 423 | return ret_count; |
| 424 | } |
| 425 | |
| 426 | static int lpath_reset(lua_State* L) { |
| 427 | get_obj<SkPath>(L, 1)->reset(); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int lpath_moveTo(lua_State* L) { |
| 432 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int lpath_lineTo(lua_State* L) { |
| 437 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int lpath_quadTo(lua_State* L) { |
| 442 | get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 443 | lua2scalar(L, 4), lua2scalar(L, 5)); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int lpath_cubicTo(lua_State* L) { |
| 448 | get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 449 | lua2scalar(L, 4), lua2scalar(L, 5), |
| 450 | lua2scalar(L, 6), lua2scalar(L, 7)); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int lpath_close(lua_State* L) { |
| 455 | get_obj<SkPath>(L, 1)->close(); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int lpath_gc(lua_State* L) { |
| 460 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 465 | { "getBounds", lpath_getBounds }, |
| 466 | { "isEmpty", lpath_isEmpty }, |
| 467 | { "isRect", lpath_isRect }, |
| 468 | { "isNestedRects", lpath_isNestedRects }, |
| 469 | { "reset", lpath_reset }, |
| 470 | { "moveTo", lpath_moveTo }, |
| 471 | { "lineTo", lpath_lineTo }, |
| 472 | { "quadTo", lpath_quadTo }, |
| 473 | { "cubicTo", lpath_cubicTo }, |
| 474 | { "close", lpath_close }, |
| 475 | { "__gc", lpath_gc }, |
| 476 | { NULL, NULL } |
| 477 | }; |
| 478 | |
| 479 | /////////////////////////////////////////////////////////////////////////////// |
| 480 | |
| 481 | static const char* rrect_type(const SkRRect& rr) { |
| 482 | switch (rr.getType()) { |
| 483 | case SkRRect::kUnknown_Type: return "unknown"; |
| 484 | case SkRRect::kEmpty_Type: return "empty"; |
| 485 | case SkRRect::kRect_Type: return "rect"; |
| 486 | case SkRRect::kOval_Type: return "oval"; |
| 487 | case SkRRect::kSimple_Type: return "simple"; |
| 488 | case SkRRect::kComplex_Type: return "complex"; |
| 489 | } |
| 490 | SkASSERT(!"never get here"); |
| 491 | return ""; |
| 492 | } |
| 493 | |
| 494 | static int lrrect_rect(lua_State* L) { |
| 495 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | static int lrrect_type(lua_State* L) { |
| 500 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | static int lrrect_radii(lua_State* L) { |
| 505 | int corner = lua_tointeger(L, 2); |
| 506 | SkVector v; |
| 507 | if (corner < 0 || corner > 3) { |
| 508 | SkDebugf("bad corner index %d", corner); |
| 509 | v.set(0, 0); |
| 510 | } else { |
| 511 | v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner); |
| 512 | } |
| 513 | lua_pushnumber(L, v.fX); |
| 514 | lua_pushnumber(L, v.fY); |
| 515 | return 2; |
| 516 | } |
| 517 | |
| 518 | static int lrrect_gc(lua_State* L) { |
| 519 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 524 | { "rect", lrrect_rect }, |
| 525 | { "type", lrrect_type }, |
| 526 | { "radii", lrrect_radii }, |
| 527 | { "__gc", lrrect_gc }, |
| 528 | { NULL, NULL } |
| 529 | }; |
| 530 | |
| 531 | /////////////////////////////////////////////////////////////////////////////// |
| 532 | |
| 533 | class AutoCallLua { |
| 534 | public: |
| 535 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) { |
| 536 | lua_getglobal(L, func); |
| 537 | if (!lua_isfunction(L, -1)) { |
| 538 | int t = lua_type(L, -1); |
| 539 | SkDebugf("--- expected function %d\n", t); |
| 540 | } |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 541 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 542 | lua_newtable(L); |
| 543 | setfield_string(L, "verb", verb); |
| 544 | } |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 545 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 546 | ~AutoCallLua() { |
| 547 | if (lua_pcall(fL, 1, 0, 0) != LUA_OK) { |
| 548 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 549 | } |
| 550 | lua_settop(fL, -1); |
| 551 | } |
| skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 552 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 553 | private: |
| 554 | lua_State* fL; |
| 555 | }; |
| 556 | |
| 557 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 558 | |
| 559 | /////////////////////////////////////////////////////////////////////////////// |
| 560 | |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 561 | static int lsk_newPaint(lua_State* L) { |
| 562 | push_new<SkPaint>(L); |
| 563 | return 1; |
| 564 | } |
| 565 | |
| 566 | static int lsk_newPath(lua_State* L) { |
| 567 | push_new<SkPath>(L); |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | static int lsk_newRRect(lua_State* L) { |
| 572 | SkRRect* rr = push_new<SkRRect>(L); |
| 573 | rr->setEmpty(); |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | static const struct luaL_Reg gSk_Functions[] = { |
| 578 | { "newPaint", lsk_newPaint }, |
| 579 | { "newPath", lsk_newPath }, |
| 580 | { "newRRect", lsk_newRRect }, |
| 581 | { NULL, NULL } |
| 582 | }; |
| 583 | |
| 584 | static void register_Sk(lua_State* L) { |
| 585 | lua_newtable(L); |
| 586 | lua_pushvalue(L, -1); |
| 587 | lua_setglobal(L, "Sk"); |
| 588 | // the Sk table is still on top |
| 589 | |
| 590 | setfield_function(L, "newPaint", lsk_newPaint); |
| 591 | setfield_function(L, "newPath", lsk_newPath); |
| 592 | setfield_function(L, "newRRect", lsk_newRRect); |
| 593 | lua_pop(L, 1); // pop off the Sk table |
| 594 | } |
| 595 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 596 | #define REG_CLASS(L, C) \ |
| 597 | do { \ |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 598 | luaL_newmetatable(L, get_mtname<C>()); \ |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 599 | lua_pushvalue(L, -1); \ |
| 600 | lua_setfield(L, -2, "__index"); \ |
| 601 | luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 602 | lua_pop(L, 1); /* pop off the meta-table */ \ |
| 603 | } while (0) |
| 604 | |
| 605 | void SkLua::Load(lua_State* L) { |
| reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 606 | register_Sk(L); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 607 | REG_CLASS(L, SkCanvas); |
| 608 | REG_CLASS(L, SkPath); |
| 609 | REG_CLASS(L, SkPaint); |
| 610 | REG_CLASS(L, SkRRect); |
| 611 | } |