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