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" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 10 | #include "SkDocument.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 11 | #include "SkPaint.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkMatrix.h" |
| 14 | #include "SkRRect.h" |
| 15 | #include "SkString.h" |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 16 | #include "SkTypeface.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 17 | |
| 18 | extern "C" { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 19 | #include "lua.h" |
| 20 | #include "lualib.h" |
| 21 | #include "lauxlib.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 22 | } |
| 23 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 24 | // return the metatable name for a given class |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 25 | template <typename T> const char* get_mtname(); |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 26 | #define DEF_MTNAME(T) \ |
| 27 | template <> const char* get_mtname<T>() { \ |
| 28 | return #T "_LuaMetaTableName"; \ |
| 29 | } |
| 30 | |
| 31 | DEF_MTNAME(SkCanvas) |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 32 | DEF_MTNAME(SkDocument) |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 33 | DEF_MTNAME(SkMatrix) |
| 34 | DEF_MTNAME(SkRRect) |
| 35 | DEF_MTNAME(SkPath) |
| 36 | DEF_MTNAME(SkPaint) |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 37 | DEF_MTNAME(SkTypeface) |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 38 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 39 | template <typename T> T* push_new(lua_State* L) { |
| 40 | T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
| 41 | new (addr) T; |
| 42 | luaL_getmetatable(L, get_mtname<T>()); |
| 43 | lua_setmetatable(L, -2); |
| 44 | return addr; |
| 45 | } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 46 | |
| 47 | template <typename T> void push_obj(lua_State* L, const T& obj) { |
| 48 | new (lua_newuserdata(L, sizeof(T))) T(obj); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 49 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 50 | lua_setmetatable(L, -2); |
| 51 | } |
| 52 | |
| 53 | template <typename T> void push_ref(lua_State* L, T* ref) { |
| 54 | *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref); |
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> T* get_ref(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 60 | return *(T**)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | template <typename T> T* get_obj(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 64 | return (T*)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 65 | } |
| 66 | |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 67 | static bool lua2bool(lua_State* L, int index) { |
| 68 | return !!lua_toboolean(L, index); |
| 69 | } |
| 70 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 73 | SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) { |
| 74 | fL = luaL_newstate(); |
| 75 | luaL_openlibs(fL); |
| 76 | SkLua::Load(fL); |
| 77 | } |
| 78 | |
| 79 | SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {} |
| 80 | |
| 81 | SkLua::~SkLua() { |
| 82 | if (fWeOwnL) { |
| 83 | if (fTermCode.size() > 0) { |
| 84 | lua_getglobal(fL, fTermCode.c_str()); |
| 85 | if (lua_pcall(fL, 0, 0, 0) != LUA_OK) { |
| 86 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 87 | } |
| 88 | } |
| 89 | lua_close(fL); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool SkLua::runCode(const char code[]) { |
| 94 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 95 | if (err) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 96 | SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1)); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool SkLua::runCode(const void* code, size_t size) { |
| 103 | SkString str((const char*)code, size); |
| 104 | return this->runCode(str.c_str()); |
| 105 | } |
| 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0) |
| 110 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 111 | static void setfield_string(lua_State* L, const char key[], const char value[]) { |
| 112 | lua_pushstring(L, value); |
| 113 | lua_setfield(L, -2, key); |
| 114 | } |
| 115 | |
| 116 | static void setfield_number(lua_State* L, const char key[], double value) { |
| 117 | lua_pushnumber(L, value); |
| 118 | lua_setfield(L, -2, key); |
| 119 | } |
| 120 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 121 | static void setfield_scalar(lua_State* L, const char key[], SkScalar value) { |
| 122 | setfield_number(L, key, SkScalarToLua(value)); |
| 123 | } |
| 124 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 125 | static void setfield_function(lua_State* L, |
| 126 | const char key[], lua_CFunction value) { |
| 127 | lua_pushcfunction(L, value); |
| 128 | lua_setfield(L, -2, key); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 129 | } |
| 130 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 131 | static void setarray_number(lua_State* L, int index, double value) { |
| 132 | lua_pushnumber(L, value); |
| 133 | lua_rawseti(L, -2, index); |
| 134 | } |
| 135 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 136 | void SkLua::pushBool(bool value, const char key[]) { |
| 137 | lua_pushboolean(fL, value); |
| 138 | CHECK_SETFIELD(key); |
| 139 | } |
| 140 | |
| 141 | void SkLua::pushString(const char str[], const char key[]) { |
| 142 | lua_pushstring(fL, str); |
| 143 | CHECK_SETFIELD(key); |
| 144 | } |
| 145 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 146 | void SkLua::pushString(const char str[], size_t length, const char key[]) { |
| 147 | // TODO: how to do this w/o making a copy? |
| 148 | SkString s(str, length); |
| 149 | lua_pushstring(fL, s.c_str()); |
| 150 | CHECK_SETFIELD(key); |
| 151 | } |
| 152 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 153 | void SkLua::pushString(const SkString& str, const char key[]) { |
| 154 | lua_pushstring(fL, str.c_str()); |
| 155 | CHECK_SETFIELD(key); |
| 156 | } |
| 157 | |
| 158 | void SkLua::pushColor(SkColor color, const char key[]) { |
| 159 | lua_newtable(fL); |
| 160 | setfield_number(fL, "a", SkColorGetA(color) / 255.0); |
| 161 | setfield_number(fL, "r", SkColorGetR(color) / 255.0); |
| 162 | setfield_number(fL, "g", SkColorGetG(color) / 255.0); |
| 163 | setfield_number(fL, "b", SkColorGetB(color) / 255.0); |
| 164 | CHECK_SETFIELD(key); |
| 165 | } |
| 166 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 167 | void SkLua::pushU32(uint32_t value, const char key[]) { |
| 168 | lua_pushnumber(fL, (double)value); |
| 169 | CHECK_SETFIELD(key); |
| 170 | } |
| 171 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 172 | void SkLua::pushScalar(SkScalar value, const char key[]) { |
| 173 | lua_pushnumber(fL, SkScalarToLua(value)); |
| 174 | CHECK_SETFIELD(key); |
| 175 | } |
| 176 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 177 | void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { |
| 178 | lua_newtable(fL); |
| 179 | for (int i = 0; i < count; ++i) { |
| 180 | // make it base-1 to match lua convention |
| 181 | setarray_number(fL, i + 1, (double)array[i]); |
| 182 | } |
| 183 | CHECK_SETFIELD(key); |
| 184 | } |
| 185 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 186 | void SkLua::pushRect(const SkRect& r, const char key[]) { |
| 187 | lua_newtable(fL); |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 188 | setfield_scalar(fL, "left", r.fLeft); |
| 189 | setfield_scalar(fL, "top", r.fTop); |
| 190 | setfield_scalar(fL, "right", r.fRight); |
| 191 | setfield_scalar(fL, "bottom", r.fBottom); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 192 | CHECK_SETFIELD(key); |
| 193 | } |
| 194 | |
| 195 | void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
| 196 | push_obj(fL, rr); |
| 197 | CHECK_SETFIELD(key); |
| 198 | } |
| 199 | |
| 200 | void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) { |
| 201 | push_obj(fL, matrix); |
| 202 | CHECK_SETFIELD(key); |
| 203 | } |
| 204 | |
| 205 | void SkLua::pushPaint(const SkPaint& paint, const char key[]) { |
| 206 | push_obj(fL, paint); |
| 207 | CHECK_SETFIELD(key); |
| 208 | } |
| 209 | |
| 210 | void SkLua::pushPath(const SkPath& path, const char key[]) { |
| 211 | push_obj(fL, path); |
| 212 | CHECK_SETFIELD(key); |
| 213 | } |
| 214 | |
| 215 | void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) { |
| 216 | push_ref(fL, canvas); |
| 217 | CHECK_SETFIELD(key); |
| 218 | } |
| 219 | |
| 220 | /////////////////////////////////////////////////////////////////////////////// |
| 221 | /////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | static SkScalar lua2scalar(lua_State* L, int index) { |
| 224 | SkASSERT(lua_isnumber(L, index)); |
| 225 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 226 | } |
| 227 | |
| 228 | static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) { |
| 229 | SkASSERT(lua_istable(L, index)); |
| 230 | lua_pushstring(L, key); |
| 231 | lua_gettable(L, index); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame^] | 232 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 233 | SkScalar value = lua2scalar(L, -1); |
| 234 | lua_pop(L, 1); |
| 235 | return value; |
| 236 | } |
| 237 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 238 | static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) { |
| 239 | SkASSERT(lua_istable(L, index)); |
| 240 | lua_pushstring(L, key); |
| 241 | lua_gettable(L, index); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame^] | 242 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 243 | SkScalar value; |
| 244 | if (lua_isnil(L, -1)) { |
| 245 | value = def; |
| 246 | } else { |
| 247 | value = lua2scalar(L, -1); |
| 248 | } |
| 249 | lua_pop(L, 1); |
| 250 | return value; |
| 251 | } |
| 252 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 253 | static U8CPU unit2byte(SkScalar x) { |
| 254 | if (x <= 0) { |
| 255 | return 0; |
| 256 | } else if (x >= 1) { |
| 257 | return 255; |
| 258 | } else { |
| 259 | return SkScalarRoundToInt(x * 255); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static SkColor lua2color(lua_State* L, int index) { |
| 264 | return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")), |
| 265 | unit2byte(getfield_scalar(L, index, "r")), |
| 266 | unit2byte(getfield_scalar(L, index, "g")), |
| 267 | unit2byte(getfield_scalar(L, index, "b"))); |
| 268 | } |
| 269 | |
| 270 | static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 271 | rect->set(getfield_scalar_default(L, index, "left", 0), |
| 272 | getfield_scalar_default(L, index, "top", 0), |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 273 | getfield_scalar(L, index, "right"), |
| 274 | getfield_scalar(L, index, "bottom")); |
| 275 | return rect; |
| 276 | } |
| 277 | |
| 278 | static int lcanvas_drawColor(lua_State* L) { |
| 279 | get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2)); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int lcanvas_drawRect(lua_State* L) { |
| 284 | SkRect rect; |
| 285 | get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect), |
| 286 | *get_obj<SkPaint>(L, 3)); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int lcanvas_drawOval(lua_State* L) { |
| 291 | SkRect rect; |
| 292 | get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect), |
| 293 | *get_obj<SkPaint>(L, 3)); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int lcanvas_drawCircle(lua_State* L) { |
| 298 | get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
| 299 | lua2scalar(L, 3), |
| 300 | lua2scalar(L, 4), |
| 301 | *get_obj<SkPaint>(L, 5)); |
| 302 | return 0; |
| 303 | } |
| 304 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 305 | static int lcanvas_drawPath(lua_State* L) { |
| 306 | get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 307 | *get_obj<SkPaint>(L, 3)); |
| 308 | return 0; |
| 309 | } |
| 310 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 311 | static int lcanvas_drawText(lua_State* L) { |
| 312 | if (lua_gettop(L) < 5) { |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) { |
| 317 | size_t len; |
| 318 | const char* text = lua_tolstring(L, 2, &len); |
| 319 | get_ref<SkCanvas>(L, 1)->drawText(text, len, |
| 320 | lua2scalar(L, 3), lua2scalar(L, 4), |
| 321 | *get_obj<SkPaint>(L, 5)); |
| 322 | } |
| 323 | return 0; |
| 324 | } |
| 325 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 326 | static int lcanvas_getSaveCount(lua_State* L) { |
| 327 | lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | static int lcanvas_getTotalMatrix(lua_State* L) { |
| 332 | SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
| 333 | return 1; |
| 334 | } |
| 335 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 336 | static int lcanvas_save(lua_State* L) { |
| 337 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save()); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | static int lcanvas_restore(lua_State* L) { |
| 342 | get_ref<SkCanvas>(L, 1)->restore(); |
| 343 | return 0; |
| 344 | } |
| 345 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 346 | static int lcanvas_translate(lua_State* L) { |
| 347 | get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 348 | return 0; |
| 349 | } |
| 350 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 351 | static int lcanvas_gc(lua_State* L) { |
| 352 | get_ref<SkCanvas>(L, 1)->unref(); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static const struct luaL_Reg gSkCanvas_Methods[] = { |
| 357 | { "drawColor", lcanvas_drawColor }, |
| 358 | { "drawRect", lcanvas_drawRect }, |
| 359 | { "drawOval", lcanvas_drawOval }, |
| 360 | { "drawCircle", lcanvas_drawCircle }, |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 361 | { "drawPath", lcanvas_drawPath }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 362 | { "drawText", lcanvas_drawText }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 363 | { "getSaveCount", lcanvas_getSaveCount }, |
| 364 | { "getTotalMatrix", lcanvas_getTotalMatrix }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 365 | { "save", lcanvas_save }, |
| 366 | { "restore", lcanvas_restore }, |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 367 | { "translate", lcanvas_translate }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 368 | { "__gc", lcanvas_gc }, |
| 369 | { NULL, NULL } |
| 370 | }; |
| 371 | |
| 372 | /////////////////////////////////////////////////////////////////////////////// |
| 373 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 374 | static int ldocument_beginPage(lua_State* L) { |
| 375 | const SkRect* contentPtr = NULL; |
| 376 | push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), |
| 377 | lua2scalar(L, 3), |
| 378 | contentPtr)); |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | static int ldocument_endPage(lua_State* L) { |
| 383 | get_ref<SkDocument>(L, 1)->endPage(); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int ldocument_close(lua_State* L) { |
| 388 | get_ref<SkDocument>(L, 1)->close(); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static int ldocument_gc(lua_State* L) { |
| 393 | get_ref<SkDocument>(L, 1)->unref(); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static const struct luaL_Reg gSkDocument_Methods[] = { |
| 398 | { "beginPage", ldocument_beginPage }, |
| 399 | { "endPage", ldocument_endPage }, |
| 400 | { "close", ldocument_close }, |
| 401 | { "__gc", ldocument_gc }, |
| 402 | { NULL, NULL } |
| 403 | }; |
| 404 | |
| 405 | /////////////////////////////////////////////////////////////////////////////// |
| 406 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 407 | static int lpaint_isAntiAlias(lua_State* L) { |
| 408 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias()); |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | static int lpaint_setAntiAlias(lua_State* L) { |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 413 | get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int lpaint_getColor(lua_State* L) { |
| 418 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | static int lpaint_setColor(lua_State* L) { |
| 423 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 424 | return 0; |
| 425 | } |
| 426 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 427 | static int lpaint_getTextSize(lua_State* L) { |
| 428 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | static int lpaint_setTextSize(lua_State* L) { |
| 433 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 434 | return 0; |
| 435 | } |
| 436 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 437 | static int lpaint_getTypeface(lua_State* L) { |
| 438 | push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | static int lpaint_setTypeface(lua_State* L) { |
| 443 | get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2)); |
| 444 | return 0; |
| 445 | } |
| 446 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 447 | static int lpaint_getFontID(lua_State* L) { |
| 448 | SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface(); |
| 449 | SkLua(L).pushU32(SkTypeface::UniqueID(face)); |
| 450 | return 1; |
| 451 | } |
| 452 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 453 | static const struct { |
| 454 | const char* fLabel; |
| 455 | SkPaint::Align fAlign; |
| 456 | } gAlignRec[] = { |
| 457 | { "left", SkPaint::kLeft_Align }, |
| 458 | { "center", SkPaint::kCenter_Align }, |
| 459 | { "right", SkPaint::kRight_Align }, |
| 460 | }; |
| 461 | |
| 462 | static int lpaint_getTextAlign(lua_State* L) { |
| 463 | SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign(); |
| 464 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 465 | if (gAlignRec[i].fAlign == align) { |
| 466 | lua_pushstring(L, gAlignRec[i].fLabel); |
| 467 | return 1; |
| 468 | } |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int lpaint_setTextAlign(lua_State* L) { |
| 474 | if (lua_isstring(L, 2)) { |
| 475 | size_t len; |
| 476 | const char* label = lua_tolstring(L, 2, &len); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame^] | 477 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 478 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 479 | if (!strcmp(gAlignRec[i].fLabel, label)) { |
| 480 | get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign); |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 488 | static int lpaint_getStroke(lua_State* L) { |
| 489 | lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle()); |
| 490 | return 1; |
| 491 | } |
| 492 | |
| 493 | static int lpaint_setStroke(lua_State* L) { |
| 494 | SkPaint::Style style; |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame^] | 495 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 496 | if (lua_toboolean(L, 2)) { |
| 497 | style = SkPaint::kStroke_Style; |
| 498 | } else { |
| 499 | style = SkPaint::kFill_Style; |
| 500 | } |
| 501 | get_obj<SkPaint>(L, 1)->setStyle(style); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int lpaint_getStrokeWidth(lua_State* L) { |
| 506 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); |
| 507 | return 1; |
| 508 | } |
| 509 | |
| 510 | static int lpaint_setStrokeWidth(lua_State* L) { |
| 511 | get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int lpaint_measureText(lua_State* L) { |
| 516 | if (lua_isstring(L, 2)) { |
| 517 | size_t len; |
| 518 | const char* text = lua_tolstring(L, 2, &len); |
| 519 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len)); |
| 520 | return 1; |
| 521 | } |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | struct FontMetrics { |
| 526 | SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) |
| 527 | SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) |
| 528 | SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) |
| 529 | SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) |
| 530 | SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) |
| 531 | SkScalar fAvgCharWidth; //!< the average charactor width (>= 0) |
| 532 | SkScalar fXMin; //!< The minimum bounding box x value for all glyphs |
| 533 | SkScalar fXMax; //!< The maximum bounding box x value for all glyphs |
| 534 | SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face |
| 535 | }; |
| 536 | |
| 537 | static int lpaint_getFontMetrics(lua_State* L) { |
| 538 | SkPaint::FontMetrics fm; |
| 539 | SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame^] | 540 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 541 | lua_newtable(L); |
| 542 | setfield_scalar(L, "top", fm.fTop); |
| 543 | setfield_scalar(L, "ascent", fm.fAscent); |
| 544 | setfield_scalar(L, "descent", fm.fDescent); |
| 545 | setfield_scalar(L, "bottom", fm.fBottom); |
| 546 | setfield_scalar(L, "leading", fm.fLeading); |
| 547 | SkLua(L).pushScalar(height); |
| 548 | return 2; |
| 549 | } |
| 550 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 551 | static int lpaint_gc(lua_State* L) { |
| 552 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 557 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 558 | { "setAntiAlias", lpaint_setAntiAlias }, |
| 559 | { "getColor", lpaint_getColor }, |
| 560 | { "setColor", lpaint_setColor }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 561 | { "getTextSize", lpaint_getTextSize }, |
| 562 | { "setTextSize", lpaint_setTextSize }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 563 | { "getTypeface", lpaint_getTypeface }, |
| 564 | { "setTypeface", lpaint_setTypeface }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 565 | { "getFontID", lpaint_getFontID }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 566 | { "getTextAlign", lpaint_getTextAlign }, |
| 567 | { "setTextAlign", lpaint_setTextAlign }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 568 | { "getStroke", lpaint_getStroke }, |
| 569 | { "setStroke", lpaint_setStroke }, |
| 570 | { "getStrokeWidth", lpaint_getStrokeWidth }, |
| 571 | { "setStrokeWidth", lpaint_setStrokeWidth }, |
| 572 | { "measureText", lpaint_measureText }, |
| 573 | { "getFontMetrics", lpaint_getFontMetrics }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 574 | { "__gc", lpaint_gc }, |
| 575 | { NULL, NULL } |
| 576 | }; |
| 577 | |
| 578 | /////////////////////////////////////////////////////////////////////////////// |
| 579 | |
| 580 | static int lpath_getBounds(lua_State* L) { |
| 581 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 582 | return 1; |
| 583 | } |
| 584 | |
| 585 | static int lpath_isEmpty(lua_State* L) { |
| 586 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | static int lpath_isRect(lua_State* L) { |
| 591 | SkRect r; |
| 592 | bool pred = get_obj<SkPath>(L, 1)->isRect(&r); |
| 593 | int ret_count = 1; |
| 594 | lua_pushboolean(L, pred); |
| 595 | if (pred) { |
| 596 | SkLua(L).pushRect(r); |
| 597 | ret_count += 1; |
| 598 | } |
| 599 | return ret_count; |
| 600 | } |
| 601 | |
| 602 | static const char* dir2string(SkPath::Direction dir) { |
| 603 | static const char* gStr[] = { |
| 604 | "unknown", "cw", "ccw" |
| 605 | }; |
| 606 | SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 607 | return gStr[dir]; |
| 608 | } |
| 609 | |
| 610 | static int lpath_isNestedRects(lua_State* L) { |
| 611 | SkRect rects[2]; |
| 612 | SkPath::Direction dirs[2]; |
| 613 | bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs); |
| 614 | int ret_count = 1; |
| 615 | lua_pushboolean(L, pred); |
| 616 | if (pred) { |
| 617 | SkLua lua(L); |
| 618 | lua.pushRect(rects[0]); |
| 619 | lua.pushRect(rects[1]); |
| 620 | lua_pushstring(L, dir2string(dirs[0])); |
| 621 | lua_pushstring(L, dir2string(dirs[0])); |
| 622 | ret_count += 4; |
| 623 | } |
| 624 | return ret_count; |
| 625 | } |
| 626 | |
| 627 | static int lpath_reset(lua_State* L) { |
| 628 | get_obj<SkPath>(L, 1)->reset(); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int lpath_moveTo(lua_State* L) { |
| 633 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int lpath_lineTo(lua_State* L) { |
| 638 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int lpath_quadTo(lua_State* L) { |
| 643 | get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 644 | lua2scalar(L, 4), lua2scalar(L, 5)); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int lpath_cubicTo(lua_State* L) { |
| 649 | get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 650 | lua2scalar(L, 4), lua2scalar(L, 5), |
| 651 | lua2scalar(L, 6), lua2scalar(L, 7)); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int lpath_close(lua_State* L) { |
| 656 | get_obj<SkPath>(L, 1)->close(); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int lpath_gc(lua_State* L) { |
| 661 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 666 | { "getBounds", lpath_getBounds }, |
| 667 | { "isEmpty", lpath_isEmpty }, |
| 668 | { "isRect", lpath_isRect }, |
| 669 | { "isNestedRects", lpath_isNestedRects }, |
| 670 | { "reset", lpath_reset }, |
| 671 | { "moveTo", lpath_moveTo }, |
| 672 | { "lineTo", lpath_lineTo }, |
| 673 | { "quadTo", lpath_quadTo }, |
| 674 | { "cubicTo", lpath_cubicTo }, |
| 675 | { "close", lpath_close }, |
| 676 | { "__gc", lpath_gc }, |
| 677 | { NULL, NULL } |
| 678 | }; |
| 679 | |
| 680 | /////////////////////////////////////////////////////////////////////////////// |
| 681 | |
| 682 | static const char* rrect_type(const SkRRect& rr) { |
| 683 | switch (rr.getType()) { |
| 684 | case SkRRect::kUnknown_Type: return "unknown"; |
| 685 | case SkRRect::kEmpty_Type: return "empty"; |
| 686 | case SkRRect::kRect_Type: return "rect"; |
| 687 | case SkRRect::kOval_Type: return "oval"; |
| 688 | case SkRRect::kSimple_Type: return "simple"; |
| 689 | case SkRRect::kComplex_Type: return "complex"; |
| 690 | } |
| 691 | SkASSERT(!"never get here"); |
| 692 | return ""; |
| 693 | } |
| 694 | |
| 695 | static int lrrect_rect(lua_State* L) { |
| 696 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | static int lrrect_type(lua_State* L) { |
| 701 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | static int lrrect_radii(lua_State* L) { |
| 706 | int corner = lua_tointeger(L, 2); |
| 707 | SkVector v; |
| 708 | if (corner < 0 || corner > 3) { |
| 709 | SkDebugf("bad corner index %d", corner); |
| 710 | v.set(0, 0); |
| 711 | } else { |
| 712 | v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner); |
| 713 | } |
| 714 | lua_pushnumber(L, v.fX); |
| 715 | lua_pushnumber(L, v.fY); |
| 716 | return 2; |
| 717 | } |
| 718 | |
| 719 | static int lrrect_gc(lua_State* L) { |
| 720 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 725 | { "rect", lrrect_rect }, |
| 726 | { "type", lrrect_type }, |
| 727 | { "radii", lrrect_radii }, |
| 728 | { "__gc", lrrect_gc }, |
| 729 | { NULL, NULL } |
| 730 | }; |
| 731 | |
| 732 | /////////////////////////////////////////////////////////////////////////////// |
| 733 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 734 | static int ltypeface_gc(lua_State* L) { |
| 735 | get_ref<SkTypeface>(L, 1)->unref(); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static const struct luaL_Reg gSkTypeface_Methods[] = { |
| 740 | { "__gc", ltypeface_gc }, |
| 741 | { NULL, NULL } |
| 742 | }; |
| 743 | |
| 744 | /////////////////////////////////////////////////////////////////////////////// |
| 745 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 746 | class AutoCallLua { |
| 747 | public: |
| 748 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) { |
| 749 | lua_getglobal(L, func); |
| 750 | if (!lua_isfunction(L, -1)) { |
| 751 | int t = lua_type(L, -1); |
| 752 | SkDebugf("--- expected function %d\n", t); |
| 753 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 754 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 755 | lua_newtable(L); |
| 756 | setfield_string(L, "verb", verb); |
| 757 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 758 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 759 | ~AutoCallLua() { |
| 760 | if (lua_pcall(fL, 1, 0, 0) != LUA_OK) { |
| 761 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 762 | } |
| 763 | lua_settop(fL, -1); |
| 764 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 765 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 766 | private: |
| 767 | lua_State* fL; |
| 768 | }; |
| 769 | |
| 770 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 771 | |
| 772 | /////////////////////////////////////////////////////////////////////////////// |
| 773 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 774 | static int lsk_newDocumentPDF(lua_State* L) { |
| 775 | const char* file = NULL; |
| 776 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
| 777 | file = lua_tolstring(L, 1, NULL); |
| 778 | } |
| 779 | |
| 780 | SkDocument* doc = SkDocument::CreatePDF(file); |
| 781 | if (NULL == doc) { |
| 782 | // do I need to push a nil on the stack and return 1? |
| 783 | return 0; |
| 784 | } else { |
| 785 | push_ref(L, doc); |
| 786 | doc->unref(); |
| 787 | return 1; |
| 788 | } |
| 789 | } |
| 790 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 791 | static int lsk_newPaint(lua_State* L) { |
| 792 | push_new<SkPaint>(L); |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | static int lsk_newPath(lua_State* L) { |
| 797 | push_new<SkPath>(L); |
| 798 | return 1; |
| 799 | } |
| 800 | |
| 801 | static int lsk_newRRect(lua_State* L) { |
| 802 | SkRRect* rr = push_new<SkRRect>(L); |
| 803 | rr->setEmpty(); |
| 804 | return 1; |
| 805 | } |
| 806 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 807 | static int lsk_newTypeface(lua_State* L) { |
| 808 | const char* name = NULL; |
| 809 | int style = SkTypeface::kNormal; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 810 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 811 | int count = lua_gettop(L); |
| 812 | if (count > 0 && lua_isstring(L, 1)) { |
| 813 | name = lua_tolstring(L, 1, NULL); |
| 814 | if (count > 1 && lua_isnumber(L, 2)) { |
| 815 | style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | SkTypeface* face = SkTypeface::CreateFromName(name, |
| 820 | (SkTypeface::Style)style); |
| 821 | // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt()); |
| 822 | if (NULL == face) { |
| 823 | face = SkTypeface::RefDefault(); |
| 824 | } |
| 825 | push_ref(L, face); |
| 826 | face->unref(); |
| 827 | return 1; |
| 828 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 829 | |
| 830 | static void register_Sk(lua_State* L) { |
| 831 | lua_newtable(L); |
| 832 | lua_pushvalue(L, -1); |
| 833 | lua_setglobal(L, "Sk"); |
| 834 | // the Sk table is still on top |
| 835 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 836 | setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 837 | setfield_function(L, "newPaint", lsk_newPaint); |
| 838 | setfield_function(L, "newPath", lsk_newPath); |
| 839 | setfield_function(L, "newRRect", lsk_newRRect); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 840 | setfield_function(L, "newTypeface", lsk_newTypeface); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 841 | lua_pop(L, 1); // pop off the Sk table |
| 842 | } |
| 843 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 844 | #define REG_CLASS(L, C) \ |
| 845 | do { \ |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 846 | luaL_newmetatable(L, get_mtname<C>()); \ |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 847 | lua_pushvalue(L, -1); \ |
| 848 | lua_setfield(L, -2, "__index"); \ |
| 849 | luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 850 | lua_pop(L, 1); /* pop off the meta-table */ \ |
| 851 | } while (0) |
| 852 | |
| 853 | void SkLua::Load(lua_State* L) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 854 | register_Sk(L); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 855 | REG_CLASS(L, SkCanvas); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 856 | REG_CLASS(L, SkDocument); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 857 | REG_CLASS(L, SkPath); |
| 858 | REG_CLASS(L, SkPaint); |
| 859 | REG_CLASS(L, SkRRect); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 860 | REG_CLASS(L, SkTypeface); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 861 | } |