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" |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | #include "GrReducedClip.h" |
| 12 | #endif |
| 13 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 14 | #include "SkBlurImageFilter.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 15 | #include "SkCanvas.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 16 | #include "SkData.h" |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 17 | #include "SkDocument.h" |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 18 | #include "SkGradientShader.h" |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 19 | #include "SkImage.h" |
| 20 | #include "SkMatrix.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
| 22 | #include "SkPath.h" |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 23 | #include "SkPictureRecorder.h" |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 24 | #include "SkPixelRef.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 25 | #include "SkRRect.h" |
| 26 | #include "SkString.h" |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 27 | #include "SkSurface.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 28 | #include "SkTextBlob.h" |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 29 | #include "SkTypeface.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 30 | |
| 31 | extern "C" { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 32 | #include "lua.h" |
| 33 | #include "lualib.h" |
| 34 | #include "lauxlib.h" |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 37 | // return the metatable name for a given class |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 38 | template <typename T> const char* get_mtname(); |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 39 | #define DEF_MTNAME(T) \ |
| 40 | template <> const char* get_mtname<T>() { \ |
| 41 | return #T "_LuaMetaTableName"; \ |
| 42 | } |
| 43 | |
| 44 | DEF_MTNAME(SkCanvas) |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 45 | DEF_MTNAME(SkDocument) |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 46 | DEF_MTNAME(SkImage) |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 47 | DEF_MTNAME(SkImageFilter) |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 48 | DEF_MTNAME(SkMatrix) |
| 49 | DEF_MTNAME(SkRRect) |
| 50 | DEF_MTNAME(SkPath) |
| 51 | DEF_MTNAME(SkPaint) |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 52 | DEF_MTNAME(SkPathEffect) |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 53 | DEF_MTNAME(SkPicture) |
| 54 | DEF_MTNAME(SkPictureRecorder) |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 55 | DEF_MTNAME(SkShader) |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 56 | DEF_MTNAME(SkSurface) |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 57 | DEF_MTNAME(SkTextBlob) |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 58 | DEF_MTNAME(SkTypeface) |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 59 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 60 | template <typename T> T* push_new(lua_State* L) { |
| 61 | T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
| 62 | new (addr) T; |
| 63 | luaL_getmetatable(L, get_mtname<T>()); |
| 64 | lua_setmetatable(L, -2); |
| 65 | return addr; |
| 66 | } |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 67 | |
| 68 | template <typename T> void push_obj(lua_State* L, const T& obj) { |
| 69 | new (lua_newuserdata(L, sizeof(T))) T(obj); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 70 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 71 | lua_setmetatable(L, -2); |
| 72 | } |
| 73 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 74 | template <typename T> T* push_ref(lua_State* L, T* ref) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 75 | *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 76 | luaL_getmetatable(L, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 77 | lua_setmetatable(L, -2); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 78 | return ref; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template <typename T> T* get_ref(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 82 | return *(T**)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | template <typename T> T* get_obj(lua_State* L, int index) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 86 | return (T*)luaL_checkudata(L, index, get_mtname<T>()); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 89 | static bool lua2bool(lua_State* L, int index) { |
| 90 | return !!lua_toboolean(L, index); |
| 91 | } |
| 92 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 95 | SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) { |
| 96 | fL = luaL_newstate(); |
| 97 | luaL_openlibs(fL); |
| 98 | SkLua::Load(fL); |
| 99 | } |
| 100 | |
| 101 | SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {} |
| 102 | |
| 103 | SkLua::~SkLua() { |
| 104 | if (fWeOwnL) { |
| 105 | if (fTermCode.size() > 0) { |
| 106 | lua_getglobal(fL, fTermCode.c_str()); |
| 107 | if (lua_pcall(fL, 0, 0, 0) != LUA_OK) { |
| 108 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 109 | } |
| 110 | } |
| 111 | lua_close(fL); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool SkLua::runCode(const char code[]) { |
| 116 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 117 | if (err) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 118 | SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1)); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool SkLua::runCode(const void* code, size_t size) { |
| 125 | SkString str((const char*)code, size); |
| 126 | return this->runCode(str.c_str()); |
| 127 | } |
| 128 | |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0) |
| 132 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 133 | static void setfield_bool_if(lua_State* L, const char key[], bool pred) { |
| 134 | if (pred) { |
| 135 | lua_pushboolean(L, true); |
| 136 | lua_setfield(L, -2, key); |
| 137 | } |
| 138 | } |
| 139 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 140 | static void setfield_string(lua_State* L, const char key[], const char value[]) { |
| 141 | lua_pushstring(L, value); |
| 142 | lua_setfield(L, -2, key); |
| 143 | } |
| 144 | |
| 145 | static void setfield_number(lua_State* L, const char key[], double value) { |
| 146 | lua_pushnumber(L, value); |
| 147 | lua_setfield(L, -2, key); |
| 148 | } |
| 149 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 150 | static void setfield_boolean(lua_State* L, const char key[], bool value) { |
| 151 | lua_pushboolean(L, value); |
| 152 | lua_setfield(L, -2, key); |
| 153 | } |
| 154 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 155 | static void setfield_scalar(lua_State* L, const char key[], SkScalar value) { |
| 156 | setfield_number(L, key, SkScalarToLua(value)); |
| 157 | } |
| 158 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 159 | static void setfield_function(lua_State* L, |
| 160 | const char key[], lua_CFunction value) { |
| 161 | lua_pushcfunction(L, value); |
| 162 | lua_setfield(L, -2, key); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 163 | } |
| 164 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 165 | static int lua2int_def(lua_State* L, int index, int defaultValue) { |
| 166 | if (lua_isnumber(L, index)) { |
| 167 | return (int)lua_tonumber(L, index); |
| 168 | } else { |
| 169 | return defaultValue; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static SkScalar lua2scalar(lua_State* L, int index) { |
| 174 | SkASSERT(lua_isnumber(L, index)); |
| 175 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 176 | } |
| 177 | |
| 178 | static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) { |
| 179 | if (lua_isnumber(L, index)) { |
| 180 | return SkLuaToScalar(lua_tonumber(L, index)); |
| 181 | } else { |
| 182 | return defaultValue; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static SkScalar getarray_scalar(lua_State* L, int stackIndex, int arrayIndex) { |
| 187 | SkASSERT(lua_istable(L, stackIndex)); |
| 188 | lua_rawgeti(L, stackIndex, arrayIndex); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 189 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 190 | SkScalar value = lua2scalar(L, -1); |
| 191 | lua_pop(L, 1); |
| 192 | return value; |
| 193 | } |
| 194 | |
| 195 | static void getarray_scalars(lua_State* L, int stackIndex, SkScalar dst[], int count) { |
| 196 | for (int i = 0; i < count; ++i) { |
| 197 | dst[i] = getarray_scalar(L, stackIndex, i + 1); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void getarray_points(lua_State* L, int stackIndex, SkPoint pts[], int count) { |
| 202 | getarray_scalars(L, stackIndex, &pts[0].fX, count * 2); |
| 203 | } |
| 204 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 205 | static void setarray_number(lua_State* L, int index, double value) { |
| 206 | lua_pushnumber(L, value); |
| 207 | lua_rawseti(L, -2, index); |
| 208 | } |
| 209 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 210 | static void setarray_scalar(lua_State* L, int index, SkScalar value) { |
| 211 | setarray_number(L, index, SkScalarToLua(value)); |
| 212 | } |
| 213 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 214 | void SkLua::pushBool(bool value, const char key[]) { |
| 215 | lua_pushboolean(fL, value); |
| 216 | CHECK_SETFIELD(key); |
| 217 | } |
| 218 | |
| 219 | void SkLua::pushString(const char str[], const char key[]) { |
| 220 | lua_pushstring(fL, str); |
| 221 | CHECK_SETFIELD(key); |
| 222 | } |
| 223 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 224 | void SkLua::pushString(const char str[], size_t length, const char key[]) { |
| 225 | // TODO: how to do this w/o making a copy? |
| 226 | SkString s(str, length); |
| 227 | lua_pushstring(fL, s.c_str()); |
| 228 | CHECK_SETFIELD(key); |
| 229 | } |
| 230 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 231 | void SkLua::pushString(const SkString& str, const char key[]) { |
| 232 | lua_pushstring(fL, str.c_str()); |
| 233 | CHECK_SETFIELD(key); |
| 234 | } |
| 235 | |
| 236 | void SkLua::pushColor(SkColor color, const char key[]) { |
| 237 | lua_newtable(fL); |
| 238 | setfield_number(fL, "a", SkColorGetA(color) / 255.0); |
| 239 | setfield_number(fL, "r", SkColorGetR(color) / 255.0); |
| 240 | setfield_number(fL, "g", SkColorGetG(color) / 255.0); |
| 241 | setfield_number(fL, "b", SkColorGetB(color) / 255.0); |
| 242 | CHECK_SETFIELD(key); |
| 243 | } |
| 244 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 245 | void SkLua::pushU32(uint32_t value, const char key[]) { |
| 246 | lua_pushnumber(fL, (double)value); |
| 247 | CHECK_SETFIELD(key); |
| 248 | } |
| 249 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 250 | void SkLua::pushScalar(SkScalar value, const char key[]) { |
| 251 | lua_pushnumber(fL, SkScalarToLua(value)); |
| 252 | CHECK_SETFIELD(key); |
| 253 | } |
| 254 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 255 | void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { |
| 256 | lua_newtable(fL); |
| 257 | for (int i = 0; i < count; ++i) { |
| 258 | // make it base-1 to match lua convention |
| 259 | setarray_number(fL, i + 1, (double)array[i]); |
| 260 | } |
| 261 | CHECK_SETFIELD(key); |
| 262 | } |
| 263 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 264 | void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) { |
| 265 | lua_newtable(fL); |
| 266 | for (int i = 0; i < count; ++i) { |
| 267 | // make it base-1 to match lua convention |
| 268 | lua_newtable(fL); |
| 269 | this->pushScalar(array[i].fX, "x"); |
| 270 | this->pushScalar(array[i].fY, "y"); |
| 271 | lua_rawseti(fL, -2, i + 1); |
| 272 | } |
| 273 | CHECK_SETFIELD(key); |
| 274 | } |
| 275 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 276 | void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) { |
| 277 | lua_newtable(fL); |
| 278 | for (int i = 0; i < count; ++i) { |
| 279 | // make it base-1 to match lua convention |
| 280 | setarray_scalar(fL, i + 1, array[i]); |
| 281 | } |
| 282 | CHECK_SETFIELD(key); |
| 283 | } |
| 284 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 285 | void SkLua::pushRect(const SkRect& r, const char key[]) { |
| 286 | lua_newtable(fL); |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 287 | setfield_scalar(fL, "left", r.fLeft); |
| 288 | setfield_scalar(fL, "top", r.fTop); |
| 289 | setfield_scalar(fL, "right", r.fRight); |
| 290 | setfield_scalar(fL, "bottom", r.fBottom); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 291 | CHECK_SETFIELD(key); |
| 292 | } |
| 293 | |
| 294 | void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
| 295 | push_obj(fL, rr); |
| 296 | CHECK_SETFIELD(key); |
| 297 | } |
| 298 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 299 | void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) { |
| 300 | lua_newtable(fL); |
| 301 | setfield_scalar(fL, "phase", info.fPhase); |
| 302 | this->pushArrayScalar(info.fIntervals, info.fCount, "intervals"); |
| 303 | CHECK_SETFIELD(key); |
| 304 | } |
| 305 | |
| 306 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 307 | void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) { |
| 308 | push_obj(fL, matrix); |
| 309 | CHECK_SETFIELD(key); |
| 310 | } |
| 311 | |
| 312 | void SkLua::pushPaint(const SkPaint& paint, const char key[]) { |
| 313 | push_obj(fL, paint); |
| 314 | CHECK_SETFIELD(key); |
| 315 | } |
| 316 | |
| 317 | void SkLua::pushPath(const SkPath& path, const char key[]) { |
| 318 | push_obj(fL, path); |
| 319 | CHECK_SETFIELD(key); |
| 320 | } |
| 321 | |
| 322 | void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) { |
| 323 | push_ref(fL, canvas); |
| 324 | CHECK_SETFIELD(key); |
| 325 | } |
| 326 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 327 | void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) { |
| 328 | push_ref(fL, const_cast<SkTextBlob*>(blob)); |
| 329 | CHECK_SETFIELD(key); |
| 330 | } |
| 331 | |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 332 | static const char* element_type(SkClipStack::Element::Type type) { |
| 333 | switch (type) { |
| 334 | case SkClipStack::Element::kEmpty_Type: |
| 335 | return "empty"; |
| 336 | case SkClipStack::Element::kRect_Type: |
| 337 | return "rect"; |
| 338 | case SkClipStack::Element::kRRect_Type: |
| 339 | return "rrect"; |
| 340 | case SkClipStack::Element::kPath_Type: |
| 341 | return "path"; |
| 342 | } |
| 343 | return "unknown"; |
| 344 | } |
| 345 | |
| 346 | static const char* region_op(SkRegion::Op op) { |
| 347 | switch (op) { |
| 348 | case SkRegion::kDifference_Op: |
| 349 | return "difference"; |
| 350 | case SkRegion::kIntersect_Op: |
| 351 | return "intersect"; |
| 352 | case SkRegion::kUnion_Op: |
| 353 | return "union"; |
| 354 | case SkRegion::kXOR_Op: |
| 355 | return "xor"; |
| 356 | case SkRegion::kReverseDifference_Op: |
| 357 | return "reverse-difference"; |
| 358 | case SkRegion::kReplace_Op: |
| 359 | return "replace"; |
| 360 | } |
| 361 | return "unknown"; |
| 362 | } |
| 363 | |
| 364 | void SkLua::pushClipStack(const SkClipStack& stack, const char* key) { |
| 365 | lua_newtable(fL); |
| 366 | SkClipStack::B2TIter iter(stack); |
| 367 | const SkClipStack::Element* element; |
| 368 | int i = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 369 | while ((element = iter.next())) { |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 370 | this->pushClipStackElement(*element); |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 371 | lua_rawseti(fL, -2, ++i); |
| 372 | } |
| 373 | CHECK_SETFIELD(key); |
| 374 | } |
| 375 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 376 | void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) { |
| 377 | lua_newtable(fL); |
| 378 | SkClipStack::Element::Type type = element.getType(); |
| 379 | this->pushString(element_type(type), "type"); |
| 380 | switch (type) { |
| 381 | case SkClipStack::Element::kEmpty_Type: |
| 382 | break; |
| 383 | case SkClipStack::Element::kRect_Type: |
| 384 | this->pushRect(element.getRect(), "rect"); |
| 385 | break; |
| 386 | case SkClipStack::Element::kRRect_Type: |
| 387 | this->pushRRect(element.getRRect(), "rrect"); |
| 388 | break; |
| 389 | case SkClipStack::Element::kPath_Type: |
| 390 | this->pushPath(element.getPath(), "path"); |
| 391 | break; |
| 392 | } |
| 393 | this->pushString(region_op(element.getOp()), "op"); |
| 394 | this->pushBool(element.isAA(), "aa"); |
| 395 | CHECK_SETFIELD(key); |
| 396 | } |
| 397 | |
| 398 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 399 | /////////////////////////////////////////////////////////////////////////////// |
| 400 | /////////////////////////////////////////////////////////////////////////////// |
| 401 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 402 | static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) { |
| 403 | SkASSERT(lua_istable(L, index)); |
| 404 | lua_pushstring(L, key); |
| 405 | lua_gettable(L, index); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 406 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 407 | SkScalar value = lua2scalar(L, -1); |
| 408 | lua_pop(L, 1); |
| 409 | return value; |
| 410 | } |
| 411 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 412 | static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) { |
| 413 | SkASSERT(lua_istable(L, index)); |
| 414 | lua_pushstring(L, key); |
| 415 | lua_gettable(L, index); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 416 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 417 | SkScalar value; |
| 418 | if (lua_isnil(L, -1)) { |
| 419 | value = def; |
| 420 | } else { |
| 421 | value = lua2scalar(L, -1); |
| 422 | } |
| 423 | lua_pop(L, 1); |
| 424 | return value; |
| 425 | } |
| 426 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 427 | static SkScalar byte2unit(U8CPU byte) { |
| 428 | return byte / 255.0f; |
| 429 | } |
| 430 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 431 | static U8CPU unit2byte(SkScalar x) { |
| 432 | if (x <= 0) { |
| 433 | return 0; |
| 434 | } else if (x >= 1) { |
| 435 | return 255; |
| 436 | } else { |
| 437 | return SkScalarRoundToInt(x * 255); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static SkColor lua2color(lua_State* L, int index) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 442 | return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)), |
| 443 | unit2byte(getfield_scalar_default(L, index, "r", 0)), |
| 444 | unit2byte(getfield_scalar_default(L, index, "g", 0)), |
| 445 | unit2byte(getfield_scalar_default(L, index, "b", 0))); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 449 | rect->set(getfield_scalar_default(L, index, "left", 0), |
| 450 | getfield_scalar_default(L, index, "top", 0), |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 451 | getfield_scalar(L, index, "right"), |
| 452 | getfield_scalar(L, index, "bottom")); |
| 453 | return rect; |
| 454 | } |
| 455 | |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 456 | static int lcanvas_clear(lua_State* L) { |
| 457 | get_ref<SkCanvas>(L, 1)->clear(0); |
| 458 | return 0; |
| 459 | } |
| 460 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 461 | static int lcanvas_drawColor(lua_State* L) { |
| 462 | get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2)); |
| 463 | return 0; |
| 464 | } |
| 465 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 466 | static int lcanvas_drawPaint(lua_State* L) { |
| 467 | get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2)); |
| 468 | return 0; |
| 469 | } |
| 470 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 471 | static int lcanvas_drawRect(lua_State* L) { |
| 472 | SkRect rect; |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 473 | lua2rect(L, 2, &rect); |
| 474 | const SkPaint* paint = get_obj<SkPaint>(L, 3); |
| 475 | get_ref<SkCanvas>(L, 1)->drawRect(rect, *paint); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int lcanvas_drawOval(lua_State* L) { |
| 480 | SkRect rect; |
| 481 | get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect), |
| 482 | *get_obj<SkPaint>(L, 3)); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int lcanvas_drawCircle(lua_State* L) { |
| 487 | get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
| 488 | lua2scalar(L, 3), |
| 489 | lua2scalar(L, 4), |
| 490 | *get_obj<SkPaint>(L, 5)); |
| 491 | return 0; |
| 492 | } |
| 493 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 494 | static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) { |
| 495 | if (lua_isnumber(L, index)) { |
| 496 | paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255)); |
| 497 | return paint; |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 498 | } else if (lua_isuserdata(L, index)) { |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 499 | const SkPaint* ptr = get_obj<SkPaint>(L, index); |
| 500 | if (ptr) { |
| 501 | *paint = *ptr; |
| 502 | return paint; |
| 503 | } |
| 504 | } |
| 505 | return NULL; |
| 506 | } |
| 507 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 508 | static int lcanvas_drawImage(lua_State* L) { |
| 509 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 510 | SkImage* image = get_ref<SkImage>(L, 2); |
| 511 | if (NULL == image) { |
| 512 | return 0; |
| 513 | } |
| 514 | SkScalar x = lua2scalar(L, 3); |
| 515 | SkScalar y = lua2scalar(L, 4); |
| 516 | |
| 517 | SkPaint paint; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 518 | canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint)); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 522 | static int lcanvas_drawImageRect(lua_State* L) { |
| 523 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 524 | SkImage* image = get_ref<SkImage>(L, 2); |
| 525 | if (NULL == image) { |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | SkRect srcR, dstR; |
| 530 | SkRect* srcRPtr = NULL; |
| 531 | if (!lua_isnil(L, 3)) { |
| 532 | srcRPtr = lua2rect(L, 3, &srcR); |
| 533 | } |
| 534 | lua2rect(L, 4, &dstR); |
mtklein | 8aacf20 | 2014-12-18 13:29:54 -0800 | [diff] [blame] | 535 | |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 536 | SkPaint paint; |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 537 | canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint)); |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 541 | static int lcanvas_drawPatch(lua_State* L) { |
| 542 | SkPoint cubics[12]; |
| 543 | SkColor colorStorage[4]; |
| 544 | SkPoint texStorage[4]; |
| 545 | |
| 546 | const SkColor* colors = NULL; |
| 547 | const SkPoint* texs = NULL; |
| 548 | |
| 549 | getarray_points(L, 2, cubics, 12); |
| 550 | |
| 551 | colorStorage[0] = SK_ColorRED; |
| 552 | colorStorage[1] = SK_ColorGREEN; |
| 553 | colorStorage[2] = SK_ColorBLUE; |
| 554 | colorStorage[3] = SK_ColorGRAY; |
| 555 | |
| 556 | if (lua_isnil(L, 4)) { |
| 557 | colors = colorStorage; |
| 558 | } else { |
| 559 | getarray_points(L, 4, texStorage, 4); |
| 560 | texs = texStorage; |
| 561 | } |
| 562 | |
| 563 | get_ref<SkCanvas>(L, 1)->drawPatch(cubics, colors, texs, NULL, *get_obj<SkPaint>(L, 5)); |
| 564 | return 0; |
| 565 | } |
| 566 | |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 567 | static int lcanvas_drawPath(lua_State* L) { |
| 568 | get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 569 | *get_obj<SkPaint>(L, 3)); |
| 570 | return 0; |
| 571 | } |
| 572 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 573 | // drawPicture(pic, x, y, paint) |
| 574 | static int lcanvas_drawPicture(lua_State* L) { |
| 575 | SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 576 | SkPicture* picture = get_ref<SkPicture>(L, 2); |
| 577 | SkScalar x = lua2scalar_def(L, 3, 0); |
| 578 | SkScalar y = lua2scalar_def(L, 4, 0); |
| 579 | SkMatrix matrix, *matrixPtr = NULL; |
| 580 | if (x || y) { |
| 581 | matrix.setTranslate(x, y); |
| 582 | matrixPtr = &matrix; |
| 583 | } |
| 584 | SkPaint paint; |
| 585 | canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint)); |
| 586 | return 0; |
| 587 | } |
| 588 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 589 | static int lcanvas_drawText(lua_State* L) { |
| 590 | if (lua_gettop(L) < 5) { |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) { |
| 595 | size_t len; |
| 596 | const char* text = lua_tolstring(L, 2, &len); |
| 597 | get_ref<SkCanvas>(L, 1)->drawText(text, len, |
| 598 | lua2scalar(L, 3), lua2scalar(L, 4), |
| 599 | *get_obj<SkPaint>(L, 5)); |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 604 | static int lcanvas_drawTextBlob(lua_State* L) { |
| 605 | const SkTextBlob* blob = get_ref<SkTextBlob>(L, 2); |
| 606 | SkScalar x = lua2scalar(L, 3); |
| 607 | SkScalar y = lua2scalar(L, 4); |
| 608 | const SkPaint& paint = *get_obj<SkPaint>(L, 5); |
| 609 | get_ref<SkCanvas>(L, 1)->drawTextBlob(blob, x, y, paint); |
| 610 | return 0; |
| 611 | } |
| 612 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 613 | static int lcanvas_getSaveCount(lua_State* L) { |
| 614 | lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
| 615 | return 1; |
| 616 | } |
| 617 | |
| 618 | static int lcanvas_getTotalMatrix(lua_State* L) { |
| 619 | SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
| 620 | return 1; |
| 621 | } |
| 622 | |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 623 | static int lcanvas_getClipStack(lua_State* L) { |
| 624 | SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack()); |
| 625 | return 1; |
| 626 | } |
| 627 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 628 | int SkLua::lcanvas_getReducedClipStack(lua_State* L) { |
| 629 | #if SK_SUPPORT_GPU |
| 630 | const SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 631 | SkISize layerSize = canvas->getTopLayerSize(); |
| 632 | SkIPoint layerOrigin = canvas->getTopLayerOrigin(); |
| 633 | SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY, |
| 634 | layerSize.fWidth, layerSize.fHeight); |
| 635 | |
| 636 | GrReducedClip::ElementList elements; |
| 637 | GrReducedClip::InitialState initialState; |
| 638 | int32_t genID; |
| 639 | SkIRect resultBounds; |
| 640 | |
| 641 | const SkClipStack& stack = *canvas->getClipStack(); |
| 642 | |
| 643 | GrReducedClip::ReduceClipStack(stack, |
| 644 | queryBounds, |
| 645 | &elements, |
| 646 | &genID, |
| 647 | &initialState, |
| 648 | &resultBounds, |
| 649 | NULL); |
| 650 | |
| 651 | GrReducedClip::ElementList::Iter iter(elements); |
| 652 | int i = 0; |
| 653 | lua_newtable(L); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 654 | while(iter.get()) { |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 655 | SkLua(L).pushClipStackElement(*iter.get()); |
| 656 | iter.next(); |
| 657 | lua_rawseti(L, -2, ++i); |
| 658 | } |
| 659 | // Currently this only returns the element list to lua, not the initial state or result bounds. |
| 660 | // It could return these as additional items on the lua stack. |
| 661 | return 1; |
| 662 | #else |
| 663 | return 0; |
| 664 | #endif |
| 665 | } |
| 666 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 667 | static int lcanvas_save(lua_State* L) { |
| 668 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save()); |
| 669 | return 1; |
| 670 | } |
| 671 | |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 672 | static int lcanvas_saveLayer(lua_State* L) { |
| 673 | SkPaint paint; |
| 674 | lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->saveLayer(NULL, lua2OptionalPaint(L, 2, &paint))); |
| 675 | return 1; |
| 676 | } |
| 677 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 678 | static int lcanvas_restore(lua_State* L) { |
| 679 | get_ref<SkCanvas>(L, 1)->restore(); |
| 680 | return 0; |
| 681 | } |
| 682 | |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 683 | static int lcanvas_scale(lua_State* L) { |
| 684 | SkScalar sx = lua2scalar_def(L, 2, 1); |
| 685 | SkScalar sy = lua2scalar_def(L, 3, sx); |
| 686 | get_ref<SkCanvas>(L, 1)->scale(sx, sy); |
| 687 | return 0; |
| 688 | } |
| 689 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 690 | static int lcanvas_translate(lua_State* L) { |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 691 | SkScalar tx = lua2scalar_def(L, 2, 0); |
| 692 | SkScalar ty = lua2scalar_def(L, 3, 0); |
| 693 | get_ref<SkCanvas>(L, 1)->translate(tx, ty); |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static int lcanvas_rotate(lua_State* L) { |
| 698 | SkScalar degrees = lua2scalar_def(L, 2, 0); |
| 699 | get_ref<SkCanvas>(L, 1)->rotate(degrees); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 703 | static int lcanvas_concat(lua_State* L) { |
| 704 | get_ref<SkCanvas>(L, 1)->concat(*get_obj<SkMatrix>(L, 2)); |
| 705 | return 0; |
| 706 | } |
| 707 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 708 | static int lcanvas_newSurface(lua_State* L) { |
| 709 | int width = lua2int_def(L, 2, 0); |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 710 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 711 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 712 | SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info); |
| 713 | if (NULL == surface) { |
| 714 | lua_pushnil(L); |
| 715 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 716 | push_ref(L, surface)->unref(); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 717 | } |
| 718 | return 1; |
| 719 | } |
| 720 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 721 | static int lcanvas_gc(lua_State* L) { |
| 722 | get_ref<SkCanvas>(L, 1)->unref(); |
| 723 | return 0; |
| 724 | } |
| 725 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 726 | const struct luaL_Reg gSkCanvas_Methods[] = { |
reed | f355df5 | 2014-10-12 12:18:40 -0700 | [diff] [blame] | 727 | { "clear", lcanvas_clear }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 728 | { "drawColor", lcanvas_drawColor }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 729 | { "drawPaint", lcanvas_drawPaint }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 730 | { "drawRect", lcanvas_drawRect }, |
| 731 | { "drawOval", lcanvas_drawOval }, |
| 732 | { "drawCircle", lcanvas_drawCircle }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 733 | { "drawImage", lcanvas_drawImage }, |
reed | ba5fb93 | 2014-10-10 15:28:19 -0700 | [diff] [blame] | 734 | { "drawImageRect", lcanvas_drawImageRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 735 | { "drawPatch", lcanvas_drawPatch }, |
reed@google.com | fd34587 | 2013-05-22 20:53:42 +0000 | [diff] [blame] | 736 | { "drawPath", lcanvas_drawPath }, |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 737 | { "drawPicture", lcanvas_drawPicture }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 738 | { "drawText", lcanvas_drawText }, |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 739 | { "drawTextBlob", lcanvas_drawTextBlob }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 740 | { "getSaveCount", lcanvas_getSaveCount }, |
| 741 | { "getTotalMatrix", lcanvas_getTotalMatrix }, |
commit-bot@chromium.org | 5cc2535 | 2014-02-24 18:59:48 +0000 | [diff] [blame] | 742 | { "getClipStack", lcanvas_getClipStack }, |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 743 | #if SK_SUPPORT_GPU |
| 744 | { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, |
| 745 | #endif |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 746 | { "save", lcanvas_save }, |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 747 | { "saveLayer", lcanvas_saveLayer }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 748 | { "restore", lcanvas_restore }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 749 | { "scale", lcanvas_scale }, |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 750 | { "translate", lcanvas_translate }, |
mike@reedtribe.org | 1d32cc6 | 2013-06-13 01:28:56 +0000 | [diff] [blame] | 751 | { "rotate", lcanvas_rotate }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 752 | { "concat", lcanvas_concat }, |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 753 | |
| 754 | { "newSurface", lcanvas_newSurface }, |
| 755 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 756 | { "__gc", lcanvas_gc }, |
| 757 | { NULL, NULL } |
| 758 | }; |
| 759 | |
| 760 | /////////////////////////////////////////////////////////////////////////////// |
| 761 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 762 | static int ldocument_beginPage(lua_State* L) { |
| 763 | const SkRect* contentPtr = NULL; |
| 764 | push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), |
| 765 | lua2scalar(L, 3), |
| 766 | contentPtr)); |
| 767 | return 1; |
| 768 | } |
| 769 | |
| 770 | static int ldocument_endPage(lua_State* L) { |
| 771 | get_ref<SkDocument>(L, 1)->endPage(); |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int ldocument_close(lua_State* L) { |
| 776 | get_ref<SkDocument>(L, 1)->close(); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int ldocument_gc(lua_State* L) { |
| 781 | get_ref<SkDocument>(L, 1)->unref(); |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static const struct luaL_Reg gSkDocument_Methods[] = { |
| 786 | { "beginPage", ldocument_beginPage }, |
| 787 | { "endPage", ldocument_endPage }, |
| 788 | { "close", ldocument_close }, |
| 789 | { "__gc", ldocument_gc }, |
| 790 | { NULL, NULL } |
| 791 | }; |
| 792 | |
| 793 | /////////////////////////////////////////////////////////////////////////////// |
| 794 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 795 | static int lpaint_isAntiAlias(lua_State* L) { |
| 796 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias()); |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | static int lpaint_setAntiAlias(lua_State* L) { |
reed@google.com | 88c9ec9 | 2013-05-22 15:43:21 +0000 | [diff] [blame] | 801 | get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 805 | static int lpaint_isDither(lua_State* L) { |
| 806 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither()); |
| 807 | return 1; |
| 808 | } |
| 809 | |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 810 | static int lpaint_setDither(lua_State* L) { |
| 811 | get_obj<SkPaint>(L, 1)->setDither(lua2bool(L, 2)); |
| 812 | return 0; |
| 813 | } |
| 814 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 815 | static int lpaint_isUnderlineText(lua_State* L) { |
| 816 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText()); |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | static int lpaint_isStrikeThruText(lua_State* L) { |
| 821 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText()); |
| 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | static int lpaint_isFakeBoldText(lua_State* L) { |
| 826 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText()); |
| 827 | return 1; |
| 828 | } |
| 829 | |
| 830 | static int lpaint_isLinearText(lua_State* L) { |
| 831 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText()); |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | static int lpaint_isSubpixelText(lua_State* L) { |
| 836 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText()); |
| 837 | return 1; |
| 838 | } |
| 839 | |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 840 | static int lpaint_setSubpixelText(lua_State* L) { |
| 841 | get_obj<SkPaint>(L, 1)->setSubpixelText(lua2bool(L, 2)); |
| 842 | return 1; |
| 843 | } |
| 844 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 845 | static int lpaint_isDevKernText(lua_State* L) { |
| 846 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText()); |
| 847 | return 1; |
| 848 | } |
| 849 | |
| 850 | static int lpaint_isLCDRenderText(lua_State* L) { |
| 851 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText()); |
| 852 | return 1; |
| 853 | } |
| 854 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 855 | static int lpaint_setLCDRenderText(lua_State* L) { |
| 856 | get_obj<SkPaint>(L, 1)->setLCDRenderText(lua2bool(L, 2)); |
| 857 | return 1; |
| 858 | } |
| 859 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 860 | static int lpaint_isEmbeddedBitmapText(lua_State* L) { |
| 861 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText()); |
| 862 | return 1; |
| 863 | } |
| 864 | |
| 865 | static int lpaint_isAutohinted(lua_State* L) { |
| 866 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted()); |
| 867 | return 1; |
| 868 | } |
| 869 | |
| 870 | static int lpaint_isVerticalText(lua_State* L) { |
| 871 | lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText()); |
| 872 | return 1; |
| 873 | } |
| 874 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 875 | static int lpaint_getAlpha(lua_State* L) { |
| 876 | SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha())); |
| 877 | return 1; |
| 878 | } |
| 879 | |
| 880 | static int lpaint_setAlpha(lua_State* L) { |
| 881 | get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2))); |
| 882 | return 0; |
| 883 | } |
| 884 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 885 | static int lpaint_getColor(lua_State* L) { |
| 886 | SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); |
| 887 | return 1; |
| 888 | } |
| 889 | |
| 890 | static int lpaint_setColor(lua_State* L) { |
| 891 | get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); |
| 892 | return 0; |
| 893 | } |
| 894 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 895 | static int lpaint_getTextSize(lua_State* L) { |
| 896 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); |
| 897 | return 1; |
| 898 | } |
| 899 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 900 | static int lpaint_getTextScaleX(lua_State* L) { |
| 901 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX()); |
| 902 | return 1; |
| 903 | } |
| 904 | |
| 905 | static int lpaint_getTextSkewX(lua_State* L) { |
| 906 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX()); |
| 907 | return 1; |
| 908 | } |
| 909 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 910 | static int lpaint_setTextSize(lua_State* L) { |
| 911 | get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); |
| 912 | return 0; |
| 913 | } |
| 914 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 915 | static int lpaint_getTypeface(lua_State* L) { |
| 916 | push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); |
| 917 | return 1; |
| 918 | } |
| 919 | |
| 920 | static int lpaint_setTypeface(lua_State* L) { |
| 921 | get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2)); |
| 922 | return 0; |
| 923 | } |
| 924 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 925 | static int lpaint_getHinting(lua_State* L) { |
| 926 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting()); |
| 927 | return 1; |
| 928 | } |
| 929 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 930 | static int lpaint_getFilterLevel(lua_State* L) { |
| 931 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getFilterLevel()); |
| 932 | return 1; |
| 933 | } |
| 934 | |
| 935 | static int lpaint_setFilterLevel(lua_State* L) { |
| 936 | int level = lua2int_def(L, 2, -1); |
| 937 | if (level >= 0 && level <= 3) { |
| 938 | get_obj<SkPaint>(L, 1)->setFilterLevel((SkPaint::FilterLevel)level); |
| 939 | } |
| 940 | return 0; |
| 941 | } |
| 942 | |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 943 | static int lpaint_getFontID(lua_State* L) { |
| 944 | SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface(); |
| 945 | SkLua(L).pushU32(SkTypeface::UniqueID(face)); |
| 946 | return 1; |
| 947 | } |
| 948 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 949 | static const struct { |
| 950 | const char* fLabel; |
| 951 | SkPaint::Align fAlign; |
| 952 | } gAlignRec[] = { |
| 953 | { "left", SkPaint::kLeft_Align }, |
| 954 | { "center", SkPaint::kCenter_Align }, |
| 955 | { "right", SkPaint::kRight_Align }, |
| 956 | }; |
| 957 | |
| 958 | static int lpaint_getTextAlign(lua_State* L) { |
| 959 | SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign(); |
| 960 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 961 | if (gAlignRec[i].fAlign == align) { |
| 962 | lua_pushstring(L, gAlignRec[i].fLabel); |
| 963 | return 1; |
| 964 | } |
| 965 | } |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int lpaint_setTextAlign(lua_State* L) { |
| 970 | if (lua_isstring(L, 2)) { |
| 971 | size_t len; |
| 972 | const char* label = lua_tolstring(L, 2, &len); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 973 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 974 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) { |
| 975 | if (!strcmp(gAlignRec[i].fLabel, label)) { |
| 976 | get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign); |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 984 | static int lpaint_getStroke(lua_State* L) { |
| 985 | lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle()); |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | static int lpaint_setStroke(lua_State* L) { |
| 990 | SkPaint::Style style; |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 991 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 992 | if (lua_toboolean(L, 2)) { |
| 993 | style = SkPaint::kStroke_Style; |
| 994 | } else { |
| 995 | style = SkPaint::kFill_Style; |
| 996 | } |
| 997 | get_obj<SkPaint>(L, 1)->setStyle(style); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1001 | static int lpaint_getStrokeCap(lua_State* L) { |
| 1002 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap()); |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | static int lpaint_getStrokeJoin(lua_State* L) { |
| 1007 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin()); |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | static int lpaint_getTextEncoding(lua_State* L) { |
commit-bot@chromium.org | 641bcc3 | 2013-12-19 10:39:59 +0000 | [diff] [blame] | 1012 | SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding()); |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1013 | return 1; |
| 1014 | } |
| 1015 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1016 | static int lpaint_getStrokeWidth(lua_State* L) { |
| 1017 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
| 1021 | static int lpaint_setStrokeWidth(lua_State* L) { |
| 1022 | get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1026 | static int lpaint_getStrokeMiter(lua_State* L) { |
| 1027 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter()); |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1031 | static int lpaint_measureText(lua_State* L) { |
| 1032 | if (lua_isstring(L, 2)) { |
| 1033 | size_t len; |
| 1034 | const char* text = lua_tolstring(L, 2, &len); |
| 1035 | SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len)); |
| 1036 | return 1; |
| 1037 | } |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | struct FontMetrics { |
| 1042 | SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0) |
| 1043 | SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0) |
| 1044 | SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0) |
| 1045 | SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0) |
| 1046 | SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0) |
| 1047 | SkScalar fAvgCharWidth; //!< the average charactor width (>= 0) |
| 1048 | SkScalar fXMin; //!< The minimum bounding box x value for all glyphs |
| 1049 | SkScalar fXMax; //!< The maximum bounding box x value for all glyphs |
| 1050 | SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face |
| 1051 | }; |
| 1052 | |
| 1053 | static int lpaint_getFontMetrics(lua_State* L) { |
| 1054 | SkPaint::FontMetrics fm; |
| 1055 | SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm); |
skia.committer@gmail.com | 370c534 | 2013-06-09 07:01:05 +0000 | [diff] [blame] | 1056 | |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1057 | lua_newtable(L); |
| 1058 | setfield_scalar(L, "top", fm.fTop); |
| 1059 | setfield_scalar(L, "ascent", fm.fAscent); |
| 1060 | setfield_scalar(L, "descent", fm.fDescent); |
| 1061 | setfield_scalar(L, "bottom", fm.fBottom); |
| 1062 | setfield_scalar(L, "leading", fm.fLeading); |
| 1063 | SkLua(L).pushScalar(height); |
| 1064 | return 2; |
| 1065 | } |
| 1066 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1067 | static int lpaint_getEffects(lua_State* L) { |
| 1068 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1069 | |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1070 | lua_newtable(L); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1071 | setfield_bool_if(L, "looper", !!paint->getLooper()); |
| 1072 | setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); |
| 1073 | setfield_bool_if(L, "rasterizer", !!paint->getRasterizer()); |
| 1074 | setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); |
| 1075 | setfield_bool_if(L, "shader", !!paint->getShader()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1076 | setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); |
| 1077 | setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1078 | setfield_bool_if(L, "xfermode", !!paint->getXfermode()); |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1079 | return 1; |
| 1080 | } |
| 1081 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1082 | static int lpaint_getImageFilter(lua_State* L) { |
| 1083 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1084 | SkImageFilter* imf = paint->getImageFilter(); |
| 1085 | if (imf) { |
| 1086 | push_ref(L, imf); |
| 1087 | return 1; |
| 1088 | } |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static int lpaint_setImageFilter(lua_State* L) { |
| 1093 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1094 | paint->setImageFilter(get_ref<SkImageFilter>(L, 2)); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1098 | static int lpaint_getShader(lua_State* L) { |
| 1099 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1100 | SkShader* shader = paint->getShader(); |
| 1101 | if (shader) { |
| 1102 | push_ref(L, shader); |
| 1103 | return 1; |
| 1104 | } |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1108 | static int lpaint_setShader(lua_State* L) { |
| 1109 | SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1110 | paint->setShader(get_ref<SkShader>(L, 2)); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1114 | static int lpaint_getPathEffect(lua_State* L) { |
| 1115 | const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 1116 | SkPathEffect* pe = paint->getPathEffect(); |
| 1117 | if (pe) { |
| 1118 | push_ref(L, pe); |
| 1119 | return 1; |
| 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1124 | static int lpaint_gc(lua_State* L) { |
| 1125 | get_obj<SkPaint>(L, 1)->~SkPaint(); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static const struct luaL_Reg gSkPaint_Methods[] = { |
| 1130 | { "isAntiAlias", lpaint_isAntiAlias }, |
| 1131 | { "setAntiAlias", lpaint_setAntiAlias }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1132 | { "isDither", lpaint_isDither }, |
reed | bb8a0ab | 2014-11-03 22:32:07 -0800 | [diff] [blame] | 1133 | { "setDither", lpaint_setDither }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1134 | { "getFilterLevel", lpaint_getFilterLevel }, |
| 1135 | { "setFilterLevel", lpaint_setFilterLevel }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1136 | { "isUnderlineText", lpaint_isUnderlineText }, |
| 1137 | { "isStrikeThruText", lpaint_isStrikeThruText }, |
| 1138 | { "isFakeBoldText", lpaint_isFakeBoldText }, |
| 1139 | { "isLinearText", lpaint_isLinearText }, |
| 1140 | { "isSubpixelText", lpaint_isSubpixelText }, |
reed | 09a1d67 | 2014-10-11 13:13:11 -0700 | [diff] [blame] | 1141 | { "setSubpixelText", lpaint_setSubpixelText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1142 | { "isDevKernText", lpaint_isDevKernText }, |
| 1143 | { "isLCDRenderText", lpaint_isLCDRenderText }, |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1144 | { "setLCDRenderText", lpaint_setLCDRenderText }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1145 | { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, |
| 1146 | { "isAutohinted", lpaint_isAutohinted }, |
| 1147 | { "isVerticalText", lpaint_isVerticalText }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1148 | { "getAlpha", lpaint_getAlpha }, |
| 1149 | { "setAlpha", lpaint_setAlpha }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1150 | { "getColor", lpaint_getColor }, |
| 1151 | { "setColor", lpaint_setColor }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1152 | { "getTextSize", lpaint_getTextSize }, |
| 1153 | { "setTextSize", lpaint_setTextSize }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1154 | { "getTextScaleX", lpaint_getTextScaleX }, |
| 1155 | { "getTextSkewX", lpaint_getTextSkewX }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1156 | { "getTypeface", lpaint_getTypeface }, |
| 1157 | { "setTypeface", lpaint_setTypeface }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1158 | { "getHinting", lpaint_getHinting }, |
reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame] | 1159 | { "getFontID", lpaint_getFontID }, |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1160 | { "getTextAlign", lpaint_getTextAlign }, |
| 1161 | { "setTextAlign", lpaint_setTextAlign }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1162 | { "getStroke", lpaint_getStroke }, |
| 1163 | { "setStroke", lpaint_setStroke }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1164 | { "getStrokeCap", lpaint_getStrokeCap }, |
| 1165 | { "getStrokeJoin", lpaint_getStrokeJoin }, |
| 1166 | { "getTextEncoding", lpaint_getTextEncoding }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1167 | { "getStrokeWidth", lpaint_getStrokeWidth }, |
| 1168 | { "setStrokeWidth", lpaint_setStrokeWidth }, |
commit-bot@chromium.org | 1cd71fb | 2013-12-18 18:28:07 +0000 | [diff] [blame] | 1169 | { "getStrokeMiter", lpaint_getStrokeMiter }, |
mike@reedtribe.org | 73d9f1c | 2013-06-09 01:54:56 +0000 | [diff] [blame] | 1170 | { "measureText", lpaint_measureText }, |
| 1171 | { "getFontMetrics", lpaint_getFontMetrics }, |
reed@google.com | 2956387 | 2013-07-10 21:23:49 +0000 | [diff] [blame] | 1172 | { "getEffects", lpaint_getEffects }, |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1173 | { "getImageFilter", lpaint_getImageFilter }, |
| 1174 | { "setImageFilter", lpaint_setImageFilter }, |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1175 | { "getShader", lpaint_getShader }, |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1176 | { "setShader", lpaint_setShader }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1177 | { "getPathEffect", lpaint_getPathEffect }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1178 | { "__gc", lpaint_gc }, |
| 1179 | { NULL, NULL } |
| 1180 | }; |
| 1181 | |
| 1182 | /////////////////////////////////////////////////////////////////////////////// |
| 1183 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1184 | static const char* mode2string(SkShader::TileMode mode) { |
| 1185 | static const char* gNames[] = { "clamp", "repeat", "mirror" }; |
| 1186 | SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); |
| 1187 | return gNames[mode]; |
| 1188 | } |
| 1189 | |
| 1190 | static const char* gradtype2string(SkShader::GradientType t) { |
| 1191 | static const char* gNames[] = { |
| 1192 | "none", "color", "linear", "radial", "radial2", "sweep", "conical" |
| 1193 | }; |
| 1194 | SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames)); |
| 1195 | return gNames[t]; |
| 1196 | } |
| 1197 | |
| 1198 | static int lshader_isOpaque(lua_State* L) { |
| 1199 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1200 | return shader && shader->isOpaque(); |
| 1201 | } |
| 1202 | |
| 1203 | static int lshader_asABitmap(lua_State* L) { |
| 1204 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1205 | if (shader) { |
| 1206 | SkBitmap bm; |
| 1207 | SkMatrix matrix; |
| 1208 | SkShader::TileMode modes[2]; |
| 1209 | switch (shader->asABitmap(&bm, &matrix, modes)) { |
| 1210 | case SkShader::kDefault_BitmapType: |
| 1211 | lua_newtable(L); |
| 1212 | setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0); |
| 1213 | setfield_number(L, "width", bm.width()); |
| 1214 | setfield_number(L, "height", bm.height()); |
| 1215 | setfield_string(L, "tileX", mode2string(modes[0])); |
| 1216 | setfield_string(L, "tileY", mode2string(modes[1])); |
| 1217 | return 1; |
| 1218 | default: |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static int lshader_asAGradient(lua_State* L) { |
| 1226 | SkShader* shader = get_ref<SkShader>(L, 1); |
| 1227 | if (shader) { |
| 1228 | SkShader::GradientInfo info; |
| 1229 | sk_bzero(&info, sizeof(info)); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1230 | |
| 1231 | 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] | 1232 | SkScalar pos[3]; |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1233 | |
| 1234 | info.fColorCount = 3; |
| 1235 | info.fColors = &colors[0]; |
| 1236 | info.fColorOffsets = &pos[0]; |
skia.committer@gmail.com | bd74add | 2013-08-02 07:00:59 +0000 | [diff] [blame] | 1237 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1238 | SkShader::GradientType t = shader->asAGradient(&info); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1239 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1240 | if (SkShader::kNone_GradientType != t) { |
| 1241 | lua_newtable(L); |
| 1242 | setfield_string(L, "type", gradtype2string(t)); |
| 1243 | setfield_number(L, "colorCount", info.fColorCount); |
| 1244 | setfield_string(L, "tile", mode2string(info.fTileMode)); |
commit-bot@chromium.org | 74f96b9 | 2013-08-01 17:32:56 +0000 | [diff] [blame] | 1245 | |
| 1246 | if (info.fColorCount == 3){ |
| 1247 | setfield_number(L, "midPos", pos[1]); |
| 1248 | } |
skia.committer@gmail.com | bd74add | 2013-08-02 07:00:59 +0000 | [diff] [blame] | 1249 | |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 1250 | return 1; |
| 1251 | } |
| 1252 | } |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | static int lshader_gc(lua_State* L) { |
| 1257 | get_ref<SkShader>(L, 1)->unref(); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static const struct luaL_Reg gSkShader_Methods[] = { |
| 1262 | { "isOpaque", lshader_isOpaque }, |
| 1263 | { "asABitmap", lshader_asABitmap }, |
| 1264 | { "asAGradient", lshader_asAGradient }, |
| 1265 | { "__gc", lshader_gc }, |
| 1266 | { NULL, NULL } |
| 1267 | }; |
| 1268 | |
| 1269 | /////////////////////////////////////////////////////////////////////////////// |
| 1270 | |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1271 | static int lpatheffect_asADash(lua_State* L) { |
| 1272 | SkPathEffect* pe = get_ref<SkPathEffect>(L, 1); |
| 1273 | if (pe) { |
| 1274 | SkPathEffect::DashInfo info; |
| 1275 | SkPathEffect::DashType dashType = pe->asADash(&info); |
| 1276 | if (SkPathEffect::kDash_DashType == dashType) { |
| 1277 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 1278 | info.fIntervals = intervals.get(); |
| 1279 | pe->asADash(&info); |
| 1280 | SkLua(L).pushDash(info); |
| 1281 | return 1; |
| 1282 | } |
| 1283 | } |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1287 | static int lpatheffect_gc(lua_State* L) { |
| 1288 | get_ref<SkPathEffect>(L, 1)->unref(); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static const struct luaL_Reg gSkPathEffect_Methods[] = { |
commit-bot@chromium.org | 4d803a9 | 2014-05-14 16:03:14 +0000 | [diff] [blame] | 1293 | { "asADash", lpatheffect_asADash }, |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 1294 | { "__gc", lpatheffect_gc }, |
| 1295 | { NULL, NULL } |
| 1296 | }; |
| 1297 | |
| 1298 | /////////////////////////////////////////////////////////////////////////////// |
| 1299 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1300 | static int lpimagefilter_gc(lua_State* L) { |
| 1301 | get_ref<SkImageFilter>(L, 1)->unref(); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | static const struct luaL_Reg gSkImageFilter_Methods[] = { |
| 1306 | { "__gc", lpimagefilter_gc }, |
| 1307 | { NULL, NULL } |
| 1308 | }; |
| 1309 | |
| 1310 | /////////////////////////////////////////////////////////////////////////////// |
| 1311 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1312 | static int lmatrix_getType(lua_State* L) { |
| 1313 | SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 1314 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1315 | lua_newtable(L); |
| 1316 | setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask)); |
| 1317 | setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); |
| 1318 | setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); |
| 1319 | setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask)); |
| 1320 | return 1; |
| 1321 | } |
| 1322 | |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1323 | static int lmatrix_getScaleX(lua_State* L) { |
| 1324 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX()); |
| 1325 | return 1; |
| 1326 | } |
| 1327 | |
| 1328 | static int lmatrix_getScaleY(lua_State* L) { |
| 1329 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY()); |
| 1330 | return 1; |
| 1331 | } |
| 1332 | |
| 1333 | static int lmatrix_getTranslateX(lua_State* L) { |
| 1334 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX()); |
| 1335 | return 1; |
| 1336 | } |
| 1337 | |
| 1338 | static int lmatrix_getTranslateY(lua_State* L) { |
| 1339 | lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY()); |
| 1340 | return 1; |
| 1341 | } |
| 1342 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1343 | static int lmatrix_invert(lua_State* L) { |
| 1344 | lua_pushboolean(L, get_obj<SkMatrix>(L, 1)->invert(get_obj<SkMatrix>(L, 2))); |
| 1345 | return 1; |
| 1346 | } |
| 1347 | |
| 1348 | static int lmatrix_mapXY(lua_State* L) { |
| 1349 | SkPoint pt = { lua2scalar(L, 2), lua2scalar(L, 3) }; |
| 1350 | get_obj<SkMatrix>(L, 1)->mapPoints(&pt, &pt, 1); |
| 1351 | lua_pushnumber(L, pt.x()); |
| 1352 | lua_pushnumber(L, pt.y()); |
| 1353 | return 2; |
| 1354 | } |
| 1355 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1356 | static int lmatrix_setRectToRect(lua_State* L) { |
| 1357 | SkMatrix* matrix = get_obj<SkMatrix>(L, 1); |
| 1358 | SkRect srcR, dstR; |
| 1359 | lua2rect(L, 2, &srcR); |
| 1360 | lua2rect(L, 3, &dstR); |
| 1361 | const char* scaleToFitStr = lua_tostring(L, 4); |
| 1362 | SkMatrix::ScaleToFit scaleToFit = SkMatrix::kFill_ScaleToFit; |
| 1363 | |
| 1364 | if (scaleToFitStr) { |
| 1365 | const struct { |
| 1366 | const char* fName; |
| 1367 | SkMatrix::ScaleToFit fScaleToFit; |
| 1368 | } rec[] = { |
| 1369 | { "fill", SkMatrix::kFill_ScaleToFit }, |
| 1370 | { "start", SkMatrix::kStart_ScaleToFit }, |
| 1371 | { "center", SkMatrix::kCenter_ScaleToFit }, |
| 1372 | { "end", SkMatrix::kEnd_ScaleToFit }, |
| 1373 | }; |
| 1374 | |
| 1375 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 1376 | if (strcmp(rec[i].fName, scaleToFitStr) == 0) { |
| 1377 | scaleToFit = rec[i].fScaleToFit; |
| 1378 | break; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | matrix->setRectToRect(srcR, dstR, scaleToFit); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1387 | static const struct luaL_Reg gSkMatrix_Methods[] = { |
| 1388 | { "getType", lmatrix_getType }, |
humper@google.com | 0f48ee0 | 2013-07-26 15:23:43 +0000 | [diff] [blame] | 1389 | { "getScaleX", lmatrix_getScaleX }, |
| 1390 | { "getScaleY", lmatrix_getScaleY }, |
| 1391 | { "getTranslateX", lmatrix_getTranslateX }, |
| 1392 | { "getTranslateY", lmatrix_getTranslateY }, |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1393 | { "setRectToRect", lmatrix_setRectToRect }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1394 | { "invert", lmatrix_invert }, |
| 1395 | { "mapXY", lmatrix_mapXY }, |
humper@google.com | 2815c19 | 2013-07-10 22:42:30 +0000 | [diff] [blame] | 1396 | { NULL, NULL } |
| 1397 | }; |
| 1398 | |
| 1399 | /////////////////////////////////////////////////////////////////////////////// |
| 1400 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1401 | static int lpath_getBounds(lua_State* L) { |
| 1402 | SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds()); |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1406 | static const char* fill_type_to_str(SkPath::FillType fill) { |
| 1407 | switch (fill) { |
| 1408 | case SkPath::kEvenOdd_FillType: |
| 1409 | return "even-odd"; |
| 1410 | case SkPath::kWinding_FillType: |
| 1411 | return "winding"; |
| 1412 | case SkPath::kInverseEvenOdd_FillType: |
| 1413 | return "inverse-even-odd"; |
| 1414 | case SkPath::kInverseWinding_FillType: |
| 1415 | return "inverse-winding"; |
| 1416 | } |
| 1417 | return "unknown"; |
| 1418 | } |
| 1419 | |
| 1420 | static int lpath_getFillType(lua_State* L) { |
| 1421 | SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType(); |
| 1422 | SkLua(L).pushString(fill_type_to_str(fill)); |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
| 1426 | static SkString segment_masks_to_str(uint32_t segmentMasks) { |
| 1427 | SkString result; |
| 1428 | bool first = true; |
| 1429 | if (SkPath::kLine_SegmentMask & segmentMasks) { |
| 1430 | result.append("line"); |
| 1431 | first = false; |
| 1432 | SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;) |
| 1433 | } |
| 1434 | if (SkPath::kQuad_SegmentMask & segmentMasks) { |
| 1435 | if (!first) { |
| 1436 | result.append(" "); |
| 1437 | } |
| 1438 | result.append("quad"); |
| 1439 | first = false; |
| 1440 | SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;) |
| 1441 | } |
| 1442 | if (SkPath::kConic_SegmentMask & segmentMasks) { |
| 1443 | if (!first) { |
| 1444 | result.append(" "); |
| 1445 | } |
| 1446 | result.append("conic"); |
| 1447 | first = false; |
| 1448 | SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;) |
| 1449 | } |
| 1450 | if (SkPath::kCubic_SegmentMask & segmentMasks) { |
| 1451 | if (!first) { |
| 1452 | result.append(" "); |
| 1453 | } |
| 1454 | result.append("cubic"); |
| 1455 | SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;) |
| 1456 | } |
| 1457 | SkASSERT(0 == segmentMasks); |
| 1458 | return result; |
| 1459 | } |
| 1460 | |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1461 | static int lpath_getSegmentTypes(lua_State* L) { |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1462 | uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks(); |
| 1463 | SkLua(L).pushString(segment_masks_to_str(segMasks)); |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | static int lpath_isConvex(lua_State* L) { |
| 1468 | bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity(); |
| 1469 | SkLua(L).pushBool(isConvex); |
| 1470 | return 1; |
| 1471 | } |
| 1472 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1473 | static int lpath_isEmpty(lua_State* L) { |
| 1474 | lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty()); |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | static int lpath_isRect(lua_State* L) { |
| 1479 | SkRect r; |
| 1480 | bool pred = get_obj<SkPath>(L, 1)->isRect(&r); |
| 1481 | int ret_count = 1; |
| 1482 | lua_pushboolean(L, pred); |
| 1483 | if (pred) { |
| 1484 | SkLua(L).pushRect(r); |
| 1485 | ret_count += 1; |
| 1486 | } |
| 1487 | return ret_count; |
| 1488 | } |
| 1489 | |
| 1490 | static const char* dir2string(SkPath::Direction dir) { |
| 1491 | static const char* gStr[] = { |
| 1492 | "unknown", "cw", "ccw" |
| 1493 | }; |
| 1494 | SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 1495 | return gStr[dir]; |
| 1496 | } |
| 1497 | |
| 1498 | static int lpath_isNestedRects(lua_State* L) { |
| 1499 | SkRect rects[2]; |
| 1500 | SkPath::Direction dirs[2]; |
| 1501 | bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs); |
| 1502 | int ret_count = 1; |
| 1503 | lua_pushboolean(L, pred); |
| 1504 | if (pred) { |
| 1505 | SkLua lua(L); |
| 1506 | lua.pushRect(rects[0]); |
| 1507 | lua.pushRect(rects[1]); |
| 1508 | lua_pushstring(L, dir2string(dirs[0])); |
| 1509 | lua_pushstring(L, dir2string(dirs[0])); |
| 1510 | ret_count += 4; |
| 1511 | } |
| 1512 | return ret_count; |
| 1513 | } |
| 1514 | |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1515 | static int lpath_countPoints(lua_State* L) { |
| 1516 | lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints()); |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1520 | static int lpath_reset(lua_State* L) { |
| 1521 | get_obj<SkPath>(L, 1)->reset(); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | static int lpath_moveTo(lua_State* L) { |
| 1526 | get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | static int lpath_lineTo(lua_State* L) { |
| 1531 | get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3)); |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | static int lpath_quadTo(lua_State* L) { |
| 1536 | get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1537 | lua2scalar(L, 4), lua2scalar(L, 5)); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int lpath_cubicTo(lua_State* L) { |
| 1542 | get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3), |
| 1543 | lua2scalar(L, 4), lua2scalar(L, 5), |
| 1544 | lua2scalar(L, 6), lua2scalar(L, 7)); |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static int lpath_close(lua_State* L) { |
| 1549 | get_obj<SkPath>(L, 1)->close(); |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | static int lpath_gc(lua_State* L) { |
| 1554 | get_obj<SkPath>(L, 1)->~SkPath(); |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static const struct luaL_Reg gSkPath_Methods[] = { |
| 1559 | { "getBounds", lpath_getBounds }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1560 | { "getFillType", lpath_getFillType }, |
krajcevski | 95498ed | 2014-08-18 08:02:33 -0700 | [diff] [blame] | 1561 | { "getSegmentTypes", lpath_getSegmentTypes }, |
commit-bot@chromium.org | d85b822 | 2014-02-24 21:59:29 +0000 | [diff] [blame] | 1562 | { "isConvex", lpath_isConvex }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1563 | { "isEmpty", lpath_isEmpty }, |
| 1564 | { "isRect", lpath_isRect }, |
| 1565 | { "isNestedRects", lpath_isNestedRects }, |
commit-bot@chromium.org | c530208 | 2014-02-26 21:38:47 +0000 | [diff] [blame] | 1566 | { "countPoints", lpath_countPoints }, |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1567 | { "reset", lpath_reset }, |
| 1568 | { "moveTo", lpath_moveTo }, |
| 1569 | { "lineTo", lpath_lineTo }, |
| 1570 | { "quadTo", lpath_quadTo }, |
| 1571 | { "cubicTo", lpath_cubicTo }, |
| 1572 | { "close", lpath_close }, |
| 1573 | { "__gc", lpath_gc }, |
| 1574 | { NULL, NULL } |
| 1575 | }; |
| 1576 | |
| 1577 | /////////////////////////////////////////////////////////////////////////////// |
| 1578 | |
| 1579 | static const char* rrect_type(const SkRRect& rr) { |
| 1580 | switch (rr.getType()) { |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1581 | case SkRRect::kEmpty_Type: return "empty"; |
| 1582 | case SkRRect::kRect_Type: return "rect"; |
| 1583 | case SkRRect::kOval_Type: return "oval"; |
| 1584 | case SkRRect::kSimple_Type: return "simple"; |
commit-bot@chromium.org | f338d7c | 2014-03-17 21:17:30 +0000 | [diff] [blame] | 1585 | case SkRRect::kNinePatch_Type: return "nine-patch"; |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1586 | case SkRRect::kComplex_Type: return "complex"; |
| 1587 | } |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1588 | SkDEBUGFAIL("never get here"); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1589 | return ""; |
| 1590 | } |
| 1591 | |
| 1592 | static int lrrect_rect(lua_State* L) { |
| 1593 | SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect()); |
| 1594 | return 1; |
| 1595 | } |
| 1596 | |
| 1597 | static int lrrect_type(lua_State* L) { |
| 1598 | lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1))); |
| 1599 | return 1; |
| 1600 | } |
| 1601 | |
| 1602 | static int lrrect_radii(lua_State* L) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 1603 | int corner = SkToInt(lua_tointeger(L, 2)); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1604 | SkVector v; |
| 1605 | if (corner < 0 || corner > 3) { |
| 1606 | SkDebugf("bad corner index %d", corner); |
| 1607 | v.set(0, 0); |
| 1608 | } else { |
| 1609 | v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner); |
| 1610 | } |
| 1611 | lua_pushnumber(L, v.fX); |
| 1612 | lua_pushnumber(L, v.fY); |
| 1613 | return 2; |
| 1614 | } |
| 1615 | |
| 1616 | static int lrrect_gc(lua_State* L) { |
| 1617 | get_obj<SkRRect>(L, 1)->~SkRRect(); |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | static const struct luaL_Reg gSkRRect_Methods[] = { |
| 1622 | { "rect", lrrect_rect }, |
| 1623 | { "type", lrrect_type }, |
| 1624 | { "radii", lrrect_radii }, |
| 1625 | { "__gc", lrrect_gc }, |
| 1626 | { NULL, NULL } |
| 1627 | }; |
| 1628 | |
| 1629 | /////////////////////////////////////////////////////////////////////////////// |
| 1630 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1631 | static int limage_width(lua_State* L) { |
| 1632 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->width()); |
| 1633 | return 1; |
| 1634 | } |
| 1635 | |
| 1636 | static int limage_height(lua_State* L) { |
| 1637 | lua_pushinteger(L, get_ref<SkImage>(L, 1)->height()); |
| 1638 | return 1; |
| 1639 | } |
| 1640 | |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1641 | static int limage_newShader(lua_State* L) { |
| 1642 | SkShader::TileMode tmode = SkShader::kClamp_TileMode; |
| 1643 | const SkMatrix* localM = NULL; |
| 1644 | SkAutoTUnref<SkShader> shader(get_ref<SkImage>(L, 1)->newShader(tmode, tmode, localM)); |
| 1645 | push_ref(L, shader.get()); |
| 1646 | return 1; |
| 1647 | } |
| 1648 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1649 | static int limage_gc(lua_State* L) { |
| 1650 | get_ref<SkImage>(L, 1)->unref(); |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | static const struct luaL_Reg gSkImage_Methods[] = { |
| 1655 | { "width", limage_width }, |
| 1656 | { "height", limage_height }, |
reed | 7a72c67 | 2014-11-07 10:23:55 -0800 | [diff] [blame] | 1657 | { "newShader", limage_newShader }, |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 1658 | { "__gc", limage_gc }, |
| 1659 | { NULL, NULL } |
| 1660 | }; |
| 1661 | |
| 1662 | /////////////////////////////////////////////////////////////////////////////// |
| 1663 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1664 | static int lsurface_width(lua_State* L) { |
| 1665 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width()); |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | static int lsurface_height(lua_State* L) { |
| 1670 | lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height()); |
| 1671 | return 1; |
| 1672 | } |
| 1673 | |
| 1674 | static int lsurface_getCanvas(lua_State* L) { |
| 1675 | SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas(); |
| 1676 | if (NULL == canvas) { |
| 1677 | lua_pushnil(L); |
| 1678 | } else { |
| 1679 | push_ref(L, canvas); |
| 1680 | // note: we don't unref canvas, since getCanvas did not ref it. |
| 1681 | // warning: this is weird: now Lua owns a ref on this canvas, but what if they let |
| 1682 | // the real owner (the surface) go away, but still hold onto the canvas? |
| 1683 | // *really* we want to sort of ref the surface again, but have the native object |
| 1684 | // know that it is supposed to be treated as a canvas... |
| 1685 | } |
| 1686 | return 1; |
| 1687 | } |
| 1688 | |
| 1689 | static int lsurface_newImageSnapshot(lua_State* L) { |
| 1690 | SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot(); |
| 1691 | if (NULL == image) { |
| 1692 | lua_pushnil(L); |
| 1693 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1694 | push_ref(L, image)->unref(); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1695 | } |
| 1696 | return 1; |
| 1697 | } |
| 1698 | |
| 1699 | static int lsurface_newSurface(lua_State* L) { |
| 1700 | int width = lua2int_def(L, 2, 0); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1701 | int height = lua2int_def(L, 3, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1702 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 1703 | SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info); |
| 1704 | if (NULL == surface) { |
| 1705 | lua_pushnil(L); |
| 1706 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1707 | push_ref(L, surface)->unref(); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1708 | } |
| 1709 | return 1; |
| 1710 | } |
| 1711 | |
| 1712 | static int lsurface_gc(lua_State* L) { |
| 1713 | get_ref<SkSurface>(L, 1)->unref(); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | static const struct luaL_Reg gSkSurface_Methods[] = { |
| 1718 | { "width", lsurface_width }, |
| 1719 | { "height", lsurface_height }, |
| 1720 | { "getCanvas", lsurface_getCanvas }, |
| 1721 | { "newImageSnapshot", lsurface_newImageSnapshot }, |
| 1722 | { "newSurface", lsurface_newSurface }, |
| 1723 | { "__gc", lsurface_gc }, |
| 1724 | { NULL, NULL } |
| 1725 | }; |
| 1726 | |
| 1727 | /////////////////////////////////////////////////////////////////////////////// |
| 1728 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1729 | static int lpicturerecorder_beginRecording(lua_State* L) { |
| 1730 | const SkScalar w = lua2scalar_def(L, 2, -1); |
| 1731 | const SkScalar h = lua2scalar_def(L, 3, -1); |
| 1732 | if (w <= 0 || h <= 0) { |
| 1733 | lua_pushnil(L); |
| 1734 | return 1; |
| 1735 | } |
| 1736 | |
| 1737 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h); |
| 1738 | if (NULL == canvas) { |
| 1739 | lua_pushnil(L); |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
| 1743 | push_ref(L, canvas); |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
| 1747 | static int lpicturerecorder_getCanvas(lua_State* L) { |
| 1748 | SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas(); |
| 1749 | if (NULL == canvas) { |
| 1750 | lua_pushnil(L); |
| 1751 | return 1; |
| 1752 | } |
| 1753 | push_ref(L, canvas); |
| 1754 | return 1; |
| 1755 | } |
| 1756 | |
| 1757 | static int lpicturerecorder_endRecording(lua_State* L) { |
| 1758 | SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording(); |
| 1759 | if (NULL == pic) { |
| 1760 | lua_pushnil(L); |
| 1761 | return 1; |
| 1762 | } |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1763 | push_ref(L, pic)->unref(); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1764 | return 1; |
| 1765 | } |
| 1766 | |
| 1767 | static int lpicturerecorder_gc(lua_State* L) { |
| 1768 | get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder(); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | static const struct luaL_Reg gSkPictureRecorder_Methods[] = { |
| 1773 | { "beginRecording", lpicturerecorder_beginRecording }, |
| 1774 | { "getCanvas", lpicturerecorder_getCanvas }, |
| 1775 | { "endRecording", lpicturerecorder_endRecording }, |
| 1776 | { "__gc", lpicturerecorder_gc }, |
| 1777 | { NULL, NULL } |
| 1778 | }; |
| 1779 | |
| 1780 | /////////////////////////////////////////////////////////////////////////////// |
| 1781 | |
| 1782 | static int lpicture_width(lua_State* L) { |
| 1783 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width()); |
| 1784 | return 1; |
| 1785 | } |
| 1786 | |
| 1787 | static int lpicture_height(lua_State* L) { |
| 1788 | lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height()); |
| 1789 | return 1; |
| 1790 | } |
| 1791 | |
| 1792 | static int lpicture_gc(lua_State* L) { |
| 1793 | get_ref<SkPicture>(L, 1)->unref(); |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | static const struct luaL_Reg gSkPicture_Methods[] = { |
| 1798 | { "width", lpicture_width }, |
| 1799 | { "height", lpicture_height }, |
| 1800 | { "__gc", lpicture_gc }, |
| 1801 | { NULL, NULL } |
| 1802 | }; |
| 1803 | |
| 1804 | /////////////////////////////////////////////////////////////////////////////// |
| 1805 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1806 | static int ltextblob_bounds(lua_State* L) { |
| 1807 | SkLua(L).pushRect(get_ref<SkTextBlob>(L, 1)->bounds()); |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
| 1811 | static int ltextblob_gc(lua_State* L) { |
| 1812 | SkSafeUnref(get_ref<SkTextBlob>(L, 1)); |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
| 1816 | static const struct luaL_Reg gSkTextBlob_Methods[] = { |
| 1817 | { "bounds", ltextblob_bounds }, |
| 1818 | { "__gc", ltextblob_gc }, |
| 1819 | { NULL, NULL } |
| 1820 | }; |
| 1821 | |
| 1822 | /////////////////////////////////////////////////////////////////////////////// |
| 1823 | |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1824 | static int ltypeface_getFamilyName(lua_State* L) { |
| 1825 | SkString str; |
| 1826 | get_ref<SkTypeface>(L, 1)->getFamilyName(&str); |
| 1827 | lua_pushstring(L, str.c_str()); |
| 1828 | return 1; |
| 1829 | } |
| 1830 | |
| 1831 | static int ltypeface_getStyle(lua_State* L) { |
| 1832 | lua_pushnumber(L, (double)get_ref<SkTypeface>(L, 1)->style()); |
| 1833 | return 1; |
| 1834 | } |
| 1835 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1836 | static int ltypeface_gc(lua_State* L) { |
commit-bot@chromium.org | 77887af | 2013-12-17 14:28:19 +0000 | [diff] [blame] | 1837 | SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | static const struct luaL_Reg gSkTypeface_Methods[] = { |
reed | 36c9c11 | 2014-11-04 10:58:42 -0800 | [diff] [blame] | 1842 | { "getFamilyName", ltypeface_getFamilyName }, |
| 1843 | { "getStyle", ltypeface_getStyle }, |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1844 | { "__gc", ltypeface_gc }, |
| 1845 | { NULL, NULL } |
| 1846 | }; |
| 1847 | |
| 1848 | /////////////////////////////////////////////////////////////////////////////// |
| 1849 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1850 | class AutoCallLua { |
| 1851 | public: |
| 1852 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) { |
| 1853 | lua_getglobal(L, func); |
| 1854 | if (!lua_isfunction(L, -1)) { |
| 1855 | int t = lua_type(L, -1); |
| 1856 | SkDebugf("--- expected function %d\n", t); |
| 1857 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1858 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1859 | lua_newtable(L); |
| 1860 | setfield_string(L, "verb", verb); |
| 1861 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1862 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1863 | ~AutoCallLua() { |
| 1864 | if (lua_pcall(fL, 1, 0, 0) != LUA_OK) { |
| 1865 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 1866 | } |
| 1867 | lua_settop(fL, -1); |
| 1868 | } |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 1869 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 1870 | private: |
| 1871 | lua_State* fL; |
| 1872 | }; |
| 1873 | |
| 1874 | #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb) |
| 1875 | |
| 1876 | /////////////////////////////////////////////////////////////////////////////// |
| 1877 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1878 | static int lsk_newDocumentPDF(lua_State* L) { |
| 1879 | const char* file = NULL; |
| 1880 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
| 1881 | file = lua_tolstring(L, 1, NULL); |
| 1882 | } |
| 1883 | |
| 1884 | SkDocument* doc = SkDocument::CreatePDF(file); |
| 1885 | if (NULL == doc) { |
| 1886 | // do I need to push a nil on the stack and return 1? |
| 1887 | return 0; |
| 1888 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1889 | push_ref(L, doc)->unref(); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 1890 | return 1; |
| 1891 | } |
| 1892 | } |
| 1893 | |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1894 | static int lsk_newBlurImageFilter(lua_State* L) { |
| 1895 | SkScalar sigmaX = lua2scalar_def(L, 1, 0); |
| 1896 | SkScalar sigmaY = lua2scalar_def(L, 2, 0); |
| 1897 | SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY); |
| 1898 | if (NULL == imf) { |
| 1899 | lua_pushnil(L); |
| 1900 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1901 | push_ref(L, imf)->unref(); |
| 1902 | } |
| 1903 | return 1; |
| 1904 | } |
| 1905 | |
| 1906 | static int lsk_newLinearGradient(lua_State* L) { |
| 1907 | SkScalar x0 = lua2scalar_def(L, 1, 0); |
| 1908 | SkScalar y0 = lua2scalar_def(L, 2, 0); |
| 1909 | SkColor c0 = lua2color(L, 3); |
| 1910 | SkScalar x1 = lua2scalar_def(L, 4, 0); |
| 1911 | SkScalar y1 = lua2scalar_def(L, 5, 0); |
| 1912 | SkColor c1 = lua2color(L, 6); |
| 1913 | |
| 1914 | SkPoint pts[] = { { x0, y0 }, { x1, y1 } }; |
| 1915 | SkColor colors[] = { c0, c1 }; |
| 1916 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 1917 | if (NULL == s) { |
| 1918 | lua_pushnil(L); |
| 1919 | } else { |
| 1920 | push_ref(L, s)->unref(); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 1921 | } |
| 1922 | return 1; |
| 1923 | } |
| 1924 | |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1925 | static int lsk_newMatrix(lua_State* L) { |
| 1926 | push_new<SkMatrix>(L)->reset(); |
| 1927 | return 1; |
| 1928 | } |
| 1929 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1930 | static int lsk_newPaint(lua_State* L) { |
| 1931 | push_new<SkPaint>(L); |
| 1932 | return 1; |
| 1933 | } |
| 1934 | |
| 1935 | static int lsk_newPath(lua_State* L) { |
| 1936 | push_new<SkPath>(L); |
| 1937 | return 1; |
| 1938 | } |
| 1939 | |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 1940 | static int lsk_newPictureRecorder(lua_State* L) { |
| 1941 | push_new<SkPictureRecorder>(L); |
| 1942 | return 1; |
| 1943 | } |
| 1944 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1945 | static int lsk_newRRect(lua_State* L) { |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 1946 | push_new<SkRRect>(L)->setEmpty(); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1947 | return 1; |
| 1948 | } |
| 1949 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 1950 | #include "SkTextBox.h" |
| 1951 | // Sk.newTextBlob(text, rect, paint) |
| 1952 | static int lsk_newTextBlob(lua_State* L) { |
| 1953 | const char* text = lua_tolstring(L, 1, NULL); |
| 1954 | SkRect bounds; |
| 1955 | lua2rect(L, 2, &bounds); |
| 1956 | const SkPaint& paint = *get_obj<SkPaint>(L, 3); |
| 1957 | |
| 1958 | SkTextBox box; |
| 1959 | box.setMode(SkTextBox::kLineBreak_Mode); |
| 1960 | box.setBox(bounds); |
| 1961 | box.setText(text, strlen(text), paint); |
| 1962 | |
| 1963 | SkScalar newBottom; |
| 1964 | SkAutoTUnref<SkTextBlob> blob(box.snapshotTextBlob(&newBottom)); |
| 1965 | push_ref<SkTextBlob>(L, blob); |
| 1966 | SkLua(L).pushScalar(newBottom); |
| 1967 | return 2; |
| 1968 | } |
| 1969 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1970 | static int lsk_newTypeface(lua_State* L) { |
| 1971 | const char* name = NULL; |
| 1972 | int style = SkTypeface::kNormal; |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 1973 | |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1974 | int count = lua_gettop(L); |
| 1975 | if (count > 0 && lua_isstring(L, 1)) { |
| 1976 | name = lua_tolstring(L, 1, NULL); |
| 1977 | if (count > 1 && lua_isnumber(L, 2)) { |
| 1978 | style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | SkTypeface* face = SkTypeface::CreateFromName(name, |
| 1983 | (SkTypeface::Style)style); |
| 1984 | // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt()); |
| 1985 | if (NULL == face) { |
| 1986 | face = SkTypeface::RefDefault(); |
| 1987 | } |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 1988 | push_ref(L, face)->unref(); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 1989 | return 1; |
| 1990 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1991 | |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1992 | static int lsk_newRasterSurface(lua_State* L) { |
reed | 7b86466 | 2014-11-04 13:24:47 -0800 | [diff] [blame] | 1993 | int width = lua2int_def(L, 1, 0); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 1994 | int height = lua2int_def(L, 2, 0); |
| 1995 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 1996 | SkSurface* surface = SkSurface::NewRaster(info); |
| 1997 | if (NULL == surface) { |
| 1998 | lua_pushnil(L); |
| 1999 | } else { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2000 | push_ref(L, surface)->unref(); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2001 | } |
| 2002 | return 1; |
| 2003 | } |
| 2004 | |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2005 | static int lsk_loadImage(lua_State* L) { |
| 2006 | if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
| 2007 | const char* name = lua_tolstring(L, 1, NULL); |
| 2008 | SkAutoDataUnref data(SkData::NewFromFileName(name)); |
| 2009 | if (data.get()) { |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame^] | 2010 | SkImage* image = SkImage::NewFromData(data); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2011 | if (image) { |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2012 | push_ref(L, image)->unref(); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2013 | return 1; |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2020 | static void register_Sk(lua_State* L) { |
| 2021 | lua_newtable(L); |
| 2022 | lua_pushvalue(L, -1); |
| 2023 | lua_setglobal(L, "Sk"); |
| 2024 | // the Sk table is still on top |
| 2025 | |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2026 | setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2027 | setfield_function(L, "loadImage", lsk_loadImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2028 | setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter); |
reed | 9fbc3f3 | 2014-10-21 07:12:58 -0700 | [diff] [blame] | 2029 | setfield_function(L, "newLinearGradient", lsk_newLinearGradient); |
reed | bdc49ae | 2014-10-14 09:34:52 -0700 | [diff] [blame] | 2030 | setfield_function(L, "newMatrix", lsk_newMatrix); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2031 | setfield_function(L, "newPaint", lsk_newPaint); |
| 2032 | setfield_function(L, "newPath", lsk_newPath); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2033 | setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2034 | setfield_function(L, "newRRect", lsk_newRRect); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2035 | setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2036 | setfield_function(L, "newTextBlob", lsk_newTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2037 | setfield_function(L, "newTypeface", lsk_newTypeface); |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2038 | lua_pop(L, 1); // pop off the Sk table |
| 2039 | } |
| 2040 | |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2041 | #define REG_CLASS(L, C) \ |
| 2042 | do { \ |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2043 | luaL_newmetatable(L, get_mtname<C>()); \ |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2044 | lua_pushvalue(L, -1); \ |
| 2045 | lua_setfield(L, -2, "__index"); \ |
| 2046 | luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 2047 | lua_pop(L, 1); /* pop off the meta-table */ \ |
| 2048 | } while (0) |
| 2049 | |
| 2050 | void SkLua::Load(lua_State* L) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 2051 | register_Sk(L); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2052 | REG_CLASS(L, SkCanvas); |
mike@reedtribe.org | fb85824 | 2013-06-08 16:39:44 +0000 | [diff] [blame] | 2053 | REG_CLASS(L, SkDocument); |
mike@reedtribe.org | 792bbd1 | 2013-06-11 02:20:28 +0000 | [diff] [blame] | 2054 | REG_CLASS(L, SkImage); |
reed | 468b181 | 2014-10-19 11:42:54 -0700 | [diff] [blame] | 2055 | REG_CLASS(L, SkImageFilter); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2056 | REG_CLASS(L, SkMatrix); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2057 | REG_CLASS(L, SkPaint); |
commit-bot@chromium.org | 1301bf3 | 2014-03-17 23:09:47 +0000 | [diff] [blame] | 2058 | REG_CLASS(L, SkPath); |
| 2059 | REG_CLASS(L, SkPathEffect); |
reed | 96affcd | 2014-10-13 12:38:04 -0700 | [diff] [blame] | 2060 | REG_CLASS(L, SkPicture); |
| 2061 | REG_CLASS(L, SkPictureRecorder); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2062 | REG_CLASS(L, SkRRect); |
reed@google.com | 5fdc983 | 2013-07-24 15:47:52 +0000 | [diff] [blame] | 2063 | REG_CLASS(L, SkShader); |
reed | 485557f | 2014-10-12 10:36:47 -0700 | [diff] [blame] | 2064 | REG_CLASS(L, SkSurface); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 2065 | REG_CLASS(L, SkTextBlob); |
mike@reedtribe.org | e6469f1 | 2013-06-08 03:15:47 +0000 | [diff] [blame] | 2066 | REG_CLASS(L, SkTypeface); |
reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame] | 2067 | } |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2068 | |
reed@google.com | 7bce998 | 2013-06-20 17:40:21 +0000 | [diff] [blame] | 2069 | extern "C" int luaopen_skia(lua_State* L); |
zachr@google.com | 28c27c8 | 2013-06-20 17:15:05 +0000 | [diff] [blame] | 2070 | extern "C" int luaopen_skia(lua_State* L) { |
| 2071 | SkLua::Load(L); |
| 2072 | return 0; |
| 2073 | } |