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