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