OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLuaCanvas.h" | 8 #include "SkLuaCanvas.h" |
9 #include "SkRRect.h" | 9 #include "SkRRect.h" |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // sets [1]...[count] in the table on the top of the stack | 31 // sets [1]...[count] in the table on the top of the stack |
32 static void setfield_arrayf(lua_State* L, const SkScalar array[], int count) { | 32 static void setfield_arrayf(lua_State* L, const SkScalar array[], int count) { |
33 for (int i = 0; i < count; ++i) { | 33 for (int i = 0; i < count; ++i) { |
34 lua_pushnumber(L, SkScalarToDouble(i + 1)); // key | 34 lua_pushnumber(L, SkScalarToDouble(i + 1)); // key |
35 lua_pushnumber(L, SkScalarToDouble(array[i])); // value | 35 lua_pushnumber(L, SkScalarToDouble(array[i])); // value |
36 lua_settable(L, -3); | 36 lua_settable(L, -3); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 static void setfield_rect(lua_State* L, const char key[], const SkRect& r) { | 40 static void push_rect(lua_State* L, const SkRect& r) { |
41 lua_newtable(L); | 41 lua_newtable(L); |
42 setfield_number(L, "left", r.fLeft); | 42 setfield_number(L, "left", r.fLeft); |
43 setfield_number(L, "top", r.fTop); | 43 setfield_number(L, "top", r.fTop); |
44 setfield_number(L, "right", r.fRight); | 44 setfield_number(L, "right", r.fRight); |
45 setfield_number(L, "bottom", r.fBottom); | 45 setfield_number(L, "bottom", r.fBottom); |
| 46 } |
| 47 |
| 48 static void setfield_rect(lua_State* L, const char key[], const SkRect& r) { |
| 49 push_rect(L, r); |
46 lua_setfield(L, -2, key); | 50 lua_setfield(L, -2, key); |
47 } | 51 } |
48 | 52 |
49 static const char* rrect_type(const SkRRect& rr) { | 53 static const char* rrect_type(const SkRRect& rr) { |
50 switch (rr.getType()) { | 54 switch (rr.getType()) { |
51 case SkRRect::kUnknown_Type: return "unknown"; | 55 case SkRRect::kUnknown_Type: return "unknown"; |
52 case SkRRect::kEmpty_Type: return "empty"; | 56 case SkRRect::kEmpty_Type: return "empty"; |
53 case SkRRect::kRect_Type: return "rect"; | 57 case SkRRect::kRect_Type: return "rect"; |
54 case SkRRect::kOval_Type: return "oval"; | 58 case SkRRect::kOval_Type: return "oval"; |
55 case SkRRect::kSimple_Type: return "simple"; | 59 case SkRRect::kSimple_Type: return "simple"; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 { "__gc", lcanvas_gc }, | 169 { "__gc", lcanvas_gc }, |
166 { NULL, NULL } | 170 { NULL, NULL } |
167 }; | 171 }; |
168 | 172 |
169 static void ensure_canvas_metatable(lua_State* L) { | 173 static void ensure_canvas_metatable(lua_State* L) { |
170 static bool gOnce; | 174 static bool gOnce; |
171 if (gOnce) { | 175 if (gOnce) { |
172 return; | 176 return; |
173 } | 177 } |
174 gOnce = true; | 178 gOnce = true; |
175 | 179 |
176 luaL_newmetatable(L, gCanvasMetaTableName); | 180 luaL_newmetatable(L, gCanvasMetaTableName); |
177 lua_pushvalue(L, -1); | 181 lua_pushvalue(L, -1); |
178 lua_setfield(L, -2, "__index"); | 182 lua_setfield(L, -2, "__index"); |
179 | 183 |
180 luaL_setfuncs(L, gLuaCanvasMethods, 0); | 184 luaL_setfuncs(L, gLuaCanvasMethods, 0); |
181 lua_settop(L, -2); // pop off the meta-table | 185 lua_settop(L, -2); // pop off the meta-table |
182 } | 186 } |
183 | 187 |
| 188 /////////////////////////////////////////////////////////////////////////////// |
| 189 |
| 190 static const char gPathMetaTableName[] = "SkPath_MetaTable"; |
| 191 |
| 192 static int lpath_getBounds(lua_State* L) { |
| 193 SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName); |
| 194 push_rect(L, p->getBounds()); |
| 195 return 1; |
| 196 } |
| 197 |
| 198 static int lpath_isEmpty(lua_State* L) { |
| 199 SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName); |
| 200 lua_pushboolean(L, p->isEmpty()); |
| 201 return 1; |
| 202 } |
| 203 |
| 204 static int lpath_isRect(lua_State* L) { |
| 205 SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName); |
| 206 SkRect r; |
| 207 bool pred = p->isRect(&r); |
| 208 int ret_count = 1; |
| 209 lua_pushboolean(L, pred); |
| 210 if (pred) { |
| 211 push_rect(L, r); |
| 212 ret_count += 1; |
| 213 } |
| 214 return ret_count; |
| 215 } |
| 216 |
| 217 static const char* dir2string(SkPath::Direction dir) { |
| 218 static const char* gStr[] = { |
| 219 "unknown", "cw", "ccw" |
| 220 }; |
| 221 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr)); |
| 222 return gStr[dir]; |
| 223 } |
| 224 |
| 225 static int lpath_isNestedRects(lua_State* L) { |
| 226 SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName); |
| 227 SkRect rects[2]; |
| 228 SkPath::Direction dirs[2]; |
| 229 bool pred = p->isNestedRects(rects, dirs); |
| 230 int ret_count = 1; |
| 231 lua_pushboolean(L, pred); |
| 232 if (pred) { |
| 233 push_rect(L, rects[0]); |
| 234 push_rect(L, rects[1]); |
| 235 lua_pushstring(L, dir2string(dirs[0])); |
| 236 lua_pushstring(L, dir2string(dirs[0])); |
| 237 ret_count += 4; |
| 238 } |
| 239 return ret_count; |
| 240 return 1; |
| 241 } |
| 242 |
| 243 static int lpath_gc(lua_State* L) { |
| 244 SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName); |
| 245 p->~SkPath(); |
| 246 return 0; |
| 247 } |
| 248 |
| 249 static const struct luaL_Reg gLuaPathMethods[] = { |
| 250 { "getBounds", lpath_getBounds }, |
| 251 { "isEmpty", lpath_isEmpty }, |
| 252 { "isRect", lpath_isRect }, |
| 253 { "isNestedRects", lpath_isNestedRects }, |
| 254 { "__gc", lpath_gc }, |
| 255 { NULL, NULL } |
| 256 }; |
| 257 |
| 258 static void ensure_path_metatable(lua_State* L) { |
| 259 static bool gOnce; |
| 260 if (gOnce) { |
| 261 return; |
| 262 } |
| 263 gOnce = true; |
| 264 |
| 265 luaL_newmetatable(L, gPathMetaTableName); |
| 266 lua_pushvalue(L, -1); |
| 267 lua_setfield(L, -2, "__index"); |
| 268 |
| 269 luaL_setfuncs(L, gLuaPathMethods, 0); |
| 270 lua_settop(L, -2); // pop off the meta-table |
| 271 } |
| 272 |
| 273 static void push_path(lua_State* L, const SkPath& src) { |
| 274 ensure_path_metatable(L); |
| 275 |
| 276 SkPath* path = (SkPath*)lua_newuserdata(L, sizeof(SkPath)); |
| 277 new (path) SkPath(src); |
| 278 |
| 279 luaL_getmetatable(L, gPathMetaTableName); |
| 280 lua_setmetatable(L, -2); |
| 281 } |
| 282 |
| 283 static void setfield_path(lua_State* L, const char key[], const SkPath& path) { |
| 284 push_path(L, path); |
| 285 lua_setfield(L, -2, key); |
| 286 } |
| 287 |
| 288 /////////////////////////////////////////////////////////////////////////////// |
| 289 |
184 void SkLuaCanvas::pushThis() { | 290 void SkLuaCanvas::pushThis() { |
185 ensure_canvas_metatable(fL); | 291 ensure_canvas_metatable(fL); |
186 | 292 |
187 SkCanvas** canvasPtr = (SkCanvas**)lua_newuserdata(fL, sizeof(SkCanvas*)); | 293 SkCanvas** canvasPtr = (SkCanvas**)lua_newuserdata(fL, sizeof(SkCanvas*)); |
188 luaL_getmetatable(fL, gCanvasMetaTableName); | 294 luaL_getmetatable(fL, gCanvasMetaTableName); |
189 lua_setmetatable(fL, -2); | 295 lua_setmetatable(fL, -2); |
190 | 296 |
191 this->ref(); | 297 this->ref(); |
192 *canvasPtr = this; | 298 *canvasPtr = this; |
193 } | 299 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 381 |
276 bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { | 382 bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
277 AUTO_LUA("clipRRect"); | 383 AUTO_LUA("clipRRect"); |
278 setfield_rrect(fL, "rrect", rrect); | 384 setfield_rrect(fL, "rrect", rrect); |
279 setfield_bool(fL, "aa", doAA); | 385 setfield_bool(fL, "aa", doAA); |
280 return this->INHERITED::clipRRect(rrect, op, doAA); | 386 return this->INHERITED::clipRRect(rrect, op, doAA); |
281 } | 387 } |
282 | 388 |
283 bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { | 389 bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
284 AUTO_LUA("clipPath"); | 390 AUTO_LUA("clipPath"); |
| 391 setfield_path(fL, "path", path); |
285 setfield_bool(fL, "aa", doAA); | 392 setfield_bool(fL, "aa", doAA); |
286 return this->INHERITED::clipPath(path, op, doAA); | 393 return this->INHERITED::clipPath(path, op, doAA); |
287 } | 394 } |
288 | 395 |
289 bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { | 396 bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { |
290 AUTO_LUA("clipRegion"); | 397 AUTO_LUA("clipRegion"); |
291 return this->INHERITED::clipRegion(deviceRgn, op); | 398 return this->INHERITED::clipRegion(deviceRgn, op); |
292 } | 399 } |
293 | 400 |
294 void SkLuaCanvas::drawPaint(const SkPaint& paint) { | 401 void SkLuaCanvas::drawPaint(const SkPaint& paint) { |
(...skipping 20 matching lines...) Expand all Loading... |
315 } | 422 } |
316 | 423 |
317 void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { | 424 void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
318 AUTO_LUA("drawRRect"); | 425 AUTO_LUA("drawRRect"); |
319 setfield_rrect(fL, "rrect", rrect); | 426 setfield_rrect(fL, "rrect", rrect); |
320 setfield_paint(fL, paint); | 427 setfield_paint(fL, paint); |
321 } | 428 } |
322 | 429 |
323 void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { | 430 void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
324 AUTO_LUA("drawPath"); | 431 AUTO_LUA("drawPath"); |
325 setfield_rect(fL, "bounds", path.getBounds()); | 432 setfield_path(fL, "path", path); |
326 setfield_bool(fL, "isRect", path.isRect(NULL)); | |
327 setfield_bool(fL, "isOval", path.isOval(NULL)); | |
328 setfield_paint(fL, paint); | 433 setfield_paint(fL, paint); |
329 } | 434 } |
330 | 435 |
331 void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, | 436 void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
332 const SkPaint* paint) { | 437 const SkPaint* paint) { |
333 AUTO_LUA("drawBitmap"); | 438 AUTO_LUA("drawBitmap"); |
334 if (paint) { | 439 if (paint) { |
335 setfield_paint(fL, *paint, kImage_PaintUsage); | 440 setfield_paint(fL, *paint, kImage_PaintUsage); |
336 } | 441 } |
337 } | 442 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 const SkScalar xpos[], SkScalar constY, | 481 const SkScalar xpos[], SkScalar constY, |
377 const SkPaint& paint) { | 482 const SkPaint& paint) { |
378 AUTO_LUA("drawPosTextH"); | 483 AUTO_LUA("drawPosTextH"); |
379 setfield_paint(fL, paint, kText_PaintUsage); | 484 setfield_paint(fL, paint, kText_PaintUsage); |
380 } | 485 } |
381 | 486 |
382 void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength, | 487 void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength, |
383 const SkPath& path, const SkMatrix* matrix, | 488 const SkPath& path, const SkMatrix* matrix, |
384 const SkPaint& paint) { | 489 const SkPaint& paint) { |
385 AUTO_LUA("drawTextOnPath"); | 490 AUTO_LUA("drawTextOnPath"); |
| 491 setfield_path(fL, "path", path); |
386 setfield_paint(fL, paint, kText_PaintUsage); | 492 setfield_paint(fL, paint, kText_PaintUsage); |
387 } | 493 } |
388 | 494 |
389 void SkLuaCanvas::drawPicture(SkPicture& picture) { | 495 void SkLuaCanvas::drawPicture(SkPicture& picture) { |
390 AUTO_LUA("drawPicture"); | 496 AUTO_LUA("drawPicture"); |
391 // call through so we can see the nested picture ops | 497 // call through so we can see the nested picture ops |
392 this->INHERITED::drawPicture(picture); | 498 this->INHERITED::drawPicture(picture); |
393 } | 499 } |
394 | 500 |
395 void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount, | 501 void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount, |
396 const SkPoint vertices[], const SkPoint texs[], | 502 const SkPoint vertices[], const SkPoint texs[], |
397 const SkColor colors[], SkXfermode* xmode, | 503 const SkColor colors[], SkXfermode* xmode, |
398 const uint16_t indices[], int indexCount, | 504 const uint16_t indices[], int indexCount, |
399 const SkPaint& paint) { | 505 const SkPaint& paint) { |
400 AUTO_LUA("drawVertices"); | 506 AUTO_LUA("drawVertices"); |
401 setfield_paint(fL, paint); | 507 setfield_paint(fL, paint); |
402 } | 508 } |
403 | 509 |
404 void SkLuaCanvas::drawData(const void* data, size_t length) { | 510 void SkLuaCanvas::drawData(const void* data, size_t length) { |
405 AUTO_LUA("drawData"); | 511 AUTO_LUA("drawData"); |
406 } | 512 } |
OLD | NEW |