2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "GrReducedClip.h"
16 #include "SkDecodingImageGenerator.h"
17 #include "SkDocument.h"
22 #include "SkPixelRef.h"
25 #include "SkTextBlob.h"
26 #include "SkTypeface.h"
34 // return the metatable name for a given class
35 template <typename T> const char* get_mtname();
36 #define DEF_MTNAME(T) \
37 template <> const char* get_mtname<T>() { \
38 return #T "_LuaMetaTableName"; \
42 DEF_MTNAME(SkDocument)
48 DEF_MTNAME(SkPathEffect)
50 DEF_MTNAME(SkTextBlob)
51 DEF_MTNAME(SkTypeface)
53 template <typename T> T* push_new(lua_State* L) {
54 T* addr = (T*)lua_newuserdata(L, sizeof(T));
56 luaL_getmetatable(L, get_mtname<T>());
57 lua_setmetatable(L, -2);
61 template <typename T> void push_obj(lua_State* L, const T& obj) {
62 new (lua_newuserdata(L, sizeof(T))) T(obj);
63 luaL_getmetatable(L, get_mtname<T>());
64 lua_setmetatable(L, -2);
67 template <typename T> void push_ref(lua_State* L, T* ref) {
68 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
69 luaL_getmetatable(L, get_mtname<T>());
70 lua_setmetatable(L, -2);
73 template <typename T> T* get_ref(lua_State* L, int index) {
74 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
77 template <typename T> T* get_obj(lua_State* L, int index) {
78 return (T*)luaL_checkudata(L, index, get_mtname<T>());
81 static bool lua2bool(lua_State* L, int index) {
82 return !!lua_toboolean(L, index);
85 ///////////////////////////////////////////////////////////////////////////////
87 SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
93 SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
97 if (fTermCode.size() > 0) {
98 lua_getglobal(fL, fTermCode.c_str());
99 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
100 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
107 bool SkLua::runCode(const char code[]) {
108 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
110 SkDebugf("--- lua failed: %s\n", lua_tostring(fL, -1));
116 bool SkLua::runCode(const void* code, size_t size) {
117 SkString str((const char*)code, size);
118 return this->runCode(str.c_str());
121 ///////////////////////////////////////////////////////////////////////////////
123 #define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
125 static void setfield_bool_if(lua_State* L, const char key[], bool pred) {
127 lua_pushboolean(L, true);
128 lua_setfield(L, -2, key);
132 static void setfield_string(lua_State* L, const char key[], const char value[]) {
133 lua_pushstring(L, value);
134 lua_setfield(L, -2, key);
137 static void setfield_number(lua_State* L, const char key[], double value) {
138 lua_pushnumber(L, value);
139 lua_setfield(L, -2, key);
142 static void setfield_boolean(lua_State* L, const char key[], bool value) {
143 lua_pushboolean(L, value);
144 lua_setfield(L, -2, key);
147 static void setfield_scalar(lua_State* L, const char key[], SkScalar value) {
148 setfield_number(L, key, SkScalarToLua(value));
151 static void setfield_function(lua_State* L,
152 const char key[], lua_CFunction value) {
153 lua_pushcfunction(L, value);
154 lua_setfield(L, -2, key);
157 static void setarray_number(lua_State* L, int index, double value) {
158 lua_pushnumber(L, value);
159 lua_rawseti(L, -2, index);
162 static void setarray_scalar(lua_State* L, int index, SkScalar value) {
163 setarray_number(L, index, SkScalarToLua(value));
166 void SkLua::pushBool(bool value, const char key[]) {
167 lua_pushboolean(fL, value);
171 void SkLua::pushString(const char str[], const char key[]) {
172 lua_pushstring(fL, str);
176 void SkLua::pushString(const char str[], size_t length, const char key[]) {
177 // TODO: how to do this w/o making a copy?
178 SkString s(str, length);
179 lua_pushstring(fL, s.c_str());
183 void SkLua::pushString(const SkString& str, const char key[]) {
184 lua_pushstring(fL, str.c_str());
188 void SkLua::pushColor(SkColor color, const char key[]) {
190 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
191 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
192 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
193 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
197 void SkLua::pushU32(uint32_t value, const char key[]) {
198 lua_pushnumber(fL, (double)value);
202 void SkLua::pushScalar(SkScalar value, const char key[]) {
203 lua_pushnumber(fL, SkScalarToLua(value));
207 void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
209 for (int i = 0; i < count; ++i) {
210 // make it base-1 to match lua convention
211 setarray_number(fL, i + 1, (double)array[i]);
216 void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
218 for (int i = 0; i < count; ++i) {
219 // make it base-1 to match lua convention
221 this->pushScalar(array[i].fX, "x");
222 this->pushScalar(array[i].fY, "y");
223 lua_rawseti(fL, -2, i + 1);
228 void SkLua::pushArrayScalar(const SkScalar array[], int count, const char key[]) {
230 for (int i = 0; i < count; ++i) {
231 // make it base-1 to match lua convention
232 setarray_scalar(fL, i + 1, array[i]);
237 void SkLua::pushRect(const SkRect& r, const char key[]) {
239 setfield_scalar(fL, "left", r.fLeft);
240 setfield_scalar(fL, "top", r.fTop);
241 setfield_scalar(fL, "right", r.fRight);
242 setfield_scalar(fL, "bottom", r.fBottom);
246 void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
251 void SkLua::pushDash(const SkPathEffect::DashInfo& info, const char key[]) {
253 setfield_scalar(fL, "phase", info.fPhase);
254 this->pushArrayScalar(info.fIntervals, info.fCount, "intervals");
259 void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
260 push_obj(fL, matrix);
264 void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
269 void SkLua::pushPath(const SkPath& path, const char key[]) {
274 void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
275 push_ref(fL, canvas);
279 void SkLua::pushTextBlob(const SkTextBlob* blob, const char key[]) {
280 push_ref(fL, const_cast<SkTextBlob*>(blob));
284 static const char* element_type(SkClipStack::Element::Type type) {
286 case SkClipStack::Element::kEmpty_Type:
288 case SkClipStack::Element::kRect_Type:
290 case SkClipStack::Element::kRRect_Type:
292 case SkClipStack::Element::kPath_Type:
298 static const char* region_op(SkRegion::Op op) {
300 case SkRegion::kDifference_Op:
302 case SkRegion::kIntersect_Op:
304 case SkRegion::kUnion_Op:
306 case SkRegion::kXOR_Op:
308 case SkRegion::kReverseDifference_Op:
309 return "reverse-difference";
310 case SkRegion::kReplace_Op:
316 void SkLua::pushClipStack(const SkClipStack& stack, const char* key) {
318 SkClipStack::B2TIter iter(stack);
319 const SkClipStack::Element* element;
321 while ((element = iter.next())) {
322 this->pushClipStackElement(*element);
323 lua_rawseti(fL, -2, ++i);
328 void SkLua::pushClipStackElement(const SkClipStack::Element& element, const char* key) {
330 SkClipStack::Element::Type type = element.getType();
331 this->pushString(element_type(type), "type");
333 case SkClipStack::Element::kEmpty_Type:
335 case SkClipStack::Element::kRect_Type:
336 this->pushRect(element.getRect(), "rect");
338 case SkClipStack::Element::kRRect_Type:
339 this->pushRRect(element.getRRect(), "rrect");
341 case SkClipStack::Element::kPath_Type:
342 this->pushPath(element.getPath(), "path");
345 this->pushString(region_op(element.getOp()), "op");
346 this->pushBool(element.isAA(), "aa");
351 ///////////////////////////////////////////////////////////////////////////////
352 ///////////////////////////////////////////////////////////////////////////////
354 static SkScalar lua2scalar(lua_State* L, int index) {
355 SkASSERT(lua_isnumber(L, index));
356 return SkLuaToScalar(lua_tonumber(L, index));
359 static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) {
360 if (lua_isnumber(L, index)) {
361 return SkLuaToScalar(lua_tonumber(L, index));
367 static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
368 SkASSERT(lua_istable(L, index));
369 lua_pushstring(L, key);
370 lua_gettable(L, index);
372 SkScalar value = lua2scalar(L, -1);
377 static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[], SkScalar def) {
378 SkASSERT(lua_istable(L, index));
379 lua_pushstring(L, key);
380 lua_gettable(L, index);
383 if (lua_isnil(L, -1)) {
386 value = lua2scalar(L, -1);
392 static U8CPU unit2byte(SkScalar x) {
398 return SkScalarRoundToInt(x * 255);
402 static SkColor lua2color(lua_State* L, int index) {
403 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
404 unit2byte(getfield_scalar(L, index, "r")),
405 unit2byte(getfield_scalar(L, index, "g")),
406 unit2byte(getfield_scalar(L, index, "b")));
409 static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
410 rect->set(getfield_scalar_default(L, index, "left", 0),
411 getfield_scalar_default(L, index, "top", 0),
412 getfield_scalar(L, index, "right"),
413 getfield_scalar(L, index, "bottom"));
417 static int lcanvas_drawColor(lua_State* L) {
418 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
422 static int lcanvas_drawRect(lua_State* L) {
424 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
425 *get_obj<SkPaint>(L, 3));
429 static int lcanvas_drawOval(lua_State* L) {
431 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
432 *get_obj<SkPaint>(L, 3));
436 static int lcanvas_drawCircle(lua_State* L) {
437 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
440 *get_obj<SkPaint>(L, 5));
444 static int lcanvas_drawImage(lua_State* L) {
445 SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
446 SkImage* image = get_ref<SkImage>(L, 2);
450 SkScalar x = lua2scalar(L, 3);
451 SkScalar y = lua2scalar(L, 4);
454 const SkPaint* paintPtr = NULL;
455 if (lua_isnumber(L, 5)) {
456 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
459 canvas->drawImage(image, x, y, paintPtr);
463 static int lcanvas_drawPath(lua_State* L) {
464 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
465 *get_obj<SkPaint>(L, 3));
469 static int lcanvas_drawText(lua_State* L) {
470 if (lua_gettop(L) < 5) {
474 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) {
476 const char* text = lua_tolstring(L, 2, &len);
477 get_ref<SkCanvas>(L, 1)->drawText(text, len,
478 lua2scalar(L, 3), lua2scalar(L, 4),
479 *get_obj<SkPaint>(L, 5));
484 static int lcanvas_getSaveCount(lua_State* L) {
485 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
489 static int lcanvas_getTotalMatrix(lua_State* L) {
490 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
494 static int lcanvas_getClipStack(lua_State* L) {
495 SkLua(L).pushClipStack(*get_ref<SkCanvas>(L, 1)->getClipStack());
499 int SkLua::lcanvas_getReducedClipStack(lua_State* L) {
501 const SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
502 SkISize layerSize = canvas->getTopLayerSize();
503 SkIPoint layerOrigin = canvas->getTopLayerOrigin();
504 SkIRect queryBounds = SkIRect::MakeXYWH(layerOrigin.fX, layerOrigin.fY,
505 layerSize.fWidth, layerSize.fHeight);
507 GrReducedClip::ElementList elements;
508 GrReducedClip::InitialState initialState;
510 SkIRect resultBounds;
512 const SkClipStack& stack = *canvas->getClipStack();
514 GrReducedClip::ReduceClipStack(stack,
522 GrReducedClip::ElementList::Iter iter(elements);
526 SkLua(L).pushClipStackElement(*iter.get());
528 lua_rawseti(L, -2, ++i);
530 // Currently this only returns the element list to lua, not the initial state or result bounds.
531 // It could return these as additional items on the lua stack.
538 static int lcanvas_save(lua_State* L) {
539 lua_pushinteger(L, get_ref<SkCanvas>(L, 1)->save());
543 static int lcanvas_restore(lua_State* L) {
544 get_ref<SkCanvas>(L, 1)->restore();
548 static int lcanvas_scale(lua_State* L) {
549 SkScalar sx = lua2scalar_def(L, 2, 1);
550 SkScalar sy = lua2scalar_def(L, 3, sx);
551 get_ref<SkCanvas>(L, 1)->scale(sx, sy);
555 static int lcanvas_translate(lua_State* L) {
556 SkScalar tx = lua2scalar_def(L, 2, 0);
557 SkScalar ty = lua2scalar_def(L, 3, 0);
558 get_ref<SkCanvas>(L, 1)->translate(tx, ty);
562 static int lcanvas_rotate(lua_State* L) {
563 SkScalar degrees = lua2scalar_def(L, 2, 0);
564 get_ref<SkCanvas>(L, 1)->rotate(degrees);
568 static int lcanvas_gc(lua_State* L) {
569 get_ref<SkCanvas>(L, 1)->unref();
573 const struct luaL_Reg gSkCanvas_Methods[] = {
574 { "drawColor", lcanvas_drawColor },
575 { "drawRect", lcanvas_drawRect },
576 { "drawOval", lcanvas_drawOval },
577 { "drawCircle", lcanvas_drawCircle },
578 { "drawImage", lcanvas_drawImage },
579 { "drawPath", lcanvas_drawPath },
580 { "drawText", lcanvas_drawText },
581 { "getSaveCount", lcanvas_getSaveCount },
582 { "getTotalMatrix", lcanvas_getTotalMatrix },
583 { "getClipStack", lcanvas_getClipStack },
585 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack },
587 { "save", lcanvas_save },
588 { "restore", lcanvas_restore },
589 { "scale", lcanvas_scale },
590 { "translate", lcanvas_translate },
591 { "rotate", lcanvas_rotate },
592 { "__gc", lcanvas_gc },
596 ///////////////////////////////////////////////////////////////////////////////
598 static int ldocument_beginPage(lua_State* L) {
599 const SkRect* contentPtr = NULL;
600 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2),
606 static int ldocument_endPage(lua_State* L) {
607 get_ref<SkDocument>(L, 1)->endPage();
611 static int ldocument_close(lua_State* L) {
612 get_ref<SkDocument>(L, 1)->close();
616 static int ldocument_gc(lua_State* L) {
617 get_ref<SkDocument>(L, 1)->unref();
621 static const struct luaL_Reg gSkDocument_Methods[] = {
622 { "beginPage", ldocument_beginPage },
623 { "endPage", ldocument_endPage },
624 { "close", ldocument_close },
625 { "__gc", ldocument_gc },
629 ///////////////////////////////////////////////////////////////////////////////
631 static int lpaint_isAntiAlias(lua_State* L) {
632 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
636 static int lpaint_setAntiAlias(lua_State* L) {
637 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
641 static int lpaint_isDither(lua_State* L) {
642 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDither());
646 static int lpaint_isUnderlineText(lua_State* L) {
647 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isUnderlineText());
651 static int lpaint_isStrikeThruText(lua_State* L) {
652 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isStrikeThruText());
656 static int lpaint_isFakeBoldText(lua_State* L) {
657 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isFakeBoldText());
661 static int lpaint_isLinearText(lua_State* L) {
662 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLinearText());
666 static int lpaint_isSubpixelText(lua_State* L) {
667 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isSubpixelText());
671 static int lpaint_isDevKernText(lua_State* L) {
672 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isDevKernText());
676 static int lpaint_isLCDRenderText(lua_State* L) {
677 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isLCDRenderText());
681 static int lpaint_isEmbeddedBitmapText(lua_State* L) {
682 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isEmbeddedBitmapText());
686 static int lpaint_isAutohinted(lua_State* L) {
687 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
691 static int lpaint_isVerticalText(lua_State* L) {
692 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
696 static int lpaint_getColor(lua_State* L) {
697 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
701 static int lpaint_setColor(lua_State* L) {
702 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
706 static int lpaint_getTextSize(lua_State* L) {
707 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
711 static int lpaint_getTextScaleX(lua_State* L) {
712 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
716 static int lpaint_getTextSkewX(lua_State* L) {
717 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
721 static int lpaint_setTextSize(lua_State* L) {
722 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
726 static int lpaint_getTypeface(lua_State* L) {
727 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
731 static int lpaint_setTypeface(lua_State* L) {
732 get_obj<SkPaint>(L, 1)->setTypeface(get_ref<SkTypeface>(L, 2));
736 static int lpaint_getHinting(lua_State* L) {
737 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getHinting());
741 static int lpaint_getFontID(lua_State* L) {
742 SkTypeface* face = get_obj<SkPaint>(L, 1)->getTypeface();
743 SkLua(L).pushU32(SkTypeface::UniqueID(face));
747 static const struct {
749 SkPaint::Align fAlign;
751 { "left", SkPaint::kLeft_Align },
752 { "center", SkPaint::kCenter_Align },
753 { "right", SkPaint::kRight_Align },
756 static int lpaint_getTextAlign(lua_State* L) {
757 SkPaint::Align align = get_obj<SkPaint>(L, 1)->getTextAlign();
758 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
759 if (gAlignRec[i].fAlign == align) {
760 lua_pushstring(L, gAlignRec[i].fLabel);
767 static int lpaint_setTextAlign(lua_State* L) {
768 if (lua_isstring(L, 2)) {
770 const char* label = lua_tolstring(L, 2, &len);
772 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlignRec); ++i) {
773 if (!strcmp(gAlignRec[i].fLabel, label)) {
774 get_obj<SkPaint>(L, 1)->setTextAlign(gAlignRec[i].fAlign);
782 static int lpaint_getStroke(lua_State* L) {
783 lua_pushboolean(L, SkPaint::kStroke_Style == get_obj<SkPaint>(L, 1)->getStyle());
787 static int lpaint_setStroke(lua_State* L) {
788 SkPaint::Style style;
790 if (lua_toboolean(L, 2)) {
791 style = SkPaint::kStroke_Style;
793 style = SkPaint::kFill_Style;
795 get_obj<SkPaint>(L, 1)->setStyle(style);
799 static int lpaint_getStrokeCap(lua_State* L) {
800 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeCap());
804 static int lpaint_getStrokeJoin(lua_State* L) {
805 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getStrokeJoin());
809 static int lpaint_getTextEncoding(lua_State* L) {
810 SkLua(L).pushU32(get_obj<SkPaint>(L, 1)->getTextEncoding());
814 static int lpaint_getStrokeWidth(lua_State* L) {
815 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
819 static int lpaint_setStrokeWidth(lua_State* L) {
820 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
824 static int lpaint_getStrokeMiter(lua_State* L) {
825 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
829 static int lpaint_measureText(lua_State* L) {
830 if (lua_isstring(L, 2)) {
832 const char* text = lua_tolstring(L, 2, &len);
833 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
840 SkScalar fTop; //!< The greatest distance above the baseline for any glyph (will be <= 0)
841 SkScalar fAscent; //!< The recommended distance above the baseline (will be <= 0)
842 SkScalar fDescent; //!< The recommended distance below the baseline (will be >= 0)
843 SkScalar fBottom; //!< The greatest distance below the baseline for any glyph (will be >= 0)
844 SkScalar fLeading; //!< The recommended distance to add between lines of text (will be >= 0)
845 SkScalar fAvgCharWidth; //!< the average charactor width (>= 0)
846 SkScalar fXMin; //!< The minimum bounding box x value for all glyphs
847 SkScalar fXMax; //!< The maximum bounding box x value for all glyphs
848 SkScalar fXHeight; //!< the height of an 'x' in px, or 0 if no 'x' in face
851 static int lpaint_getFontMetrics(lua_State* L) {
852 SkPaint::FontMetrics fm;
853 SkScalar height = get_obj<SkPaint>(L, 1)->getFontMetrics(&fm);
856 setfield_scalar(L, "top", fm.fTop);
857 setfield_scalar(L, "ascent", fm.fAscent);
858 setfield_scalar(L, "descent", fm.fDescent);
859 setfield_scalar(L, "bottom", fm.fBottom);
860 setfield_scalar(L, "leading", fm.fLeading);
861 SkLua(L).pushScalar(height);
865 static int lpaint_getEffects(lua_State* L) {
866 const SkPaint* paint = get_obj<SkPaint>(L, 1);
869 setfield_bool_if(L, "looper", !!paint->getLooper());
870 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
871 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
872 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
873 setfield_bool_if(L, "shader", !!paint->getShader());
874 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
875 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
876 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
880 static int lpaint_getShader(lua_State* L) {
881 const SkPaint* paint = get_obj<SkPaint>(L, 1);
882 SkShader* shader = paint->getShader();
890 static int lpaint_getPathEffect(lua_State* L) {
891 const SkPaint* paint = get_obj<SkPaint>(L, 1);
892 SkPathEffect* pe = paint->getPathEffect();
900 static int lpaint_gc(lua_State* L) {
901 get_obj<SkPaint>(L, 1)->~SkPaint();
905 static const struct luaL_Reg gSkPaint_Methods[] = {
906 { "isAntiAlias", lpaint_isAntiAlias },
907 { "setAntiAlias", lpaint_setAntiAlias },
908 { "isDither", lpaint_isDither },
909 { "isUnderlineText", lpaint_isUnderlineText },
910 { "isStrikeThruText", lpaint_isStrikeThruText },
911 { "isFakeBoldText", lpaint_isFakeBoldText },
912 { "isLinearText", lpaint_isLinearText },
913 { "isSubpixelText", lpaint_isSubpixelText },
914 { "isDevKernText", lpaint_isDevKernText },
915 { "isLCDRenderText", lpaint_isLCDRenderText },
916 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
917 { "isAutohinted", lpaint_isAutohinted },
918 { "isVerticalText", lpaint_isVerticalText },
919 { "getColor", lpaint_getColor },
920 { "setColor", lpaint_setColor },
921 { "getTextSize", lpaint_getTextSize },
922 { "setTextSize", lpaint_setTextSize },
923 { "getTextScaleX", lpaint_getTextScaleX },
924 { "getTextSkewX", lpaint_getTextSkewX },
925 { "getTypeface", lpaint_getTypeface },
926 { "setTypeface", lpaint_setTypeface },
927 { "getHinting", lpaint_getHinting },
928 { "getFontID", lpaint_getFontID },
929 { "getTextAlign", lpaint_getTextAlign },
930 { "setTextAlign", lpaint_setTextAlign },
931 { "getStroke", lpaint_getStroke },
932 { "setStroke", lpaint_setStroke },
933 { "getStrokeCap", lpaint_getStrokeCap },
934 { "getStrokeJoin", lpaint_getStrokeJoin },
935 { "getTextEncoding", lpaint_getTextEncoding },
936 { "getStrokeWidth", lpaint_getStrokeWidth },
937 { "setStrokeWidth", lpaint_setStrokeWidth },
938 { "getStrokeMiter", lpaint_getStrokeMiter },
939 { "measureText", lpaint_measureText },
940 { "getFontMetrics", lpaint_getFontMetrics },
941 { "getEffects", lpaint_getEffects },
942 { "getShader", lpaint_getShader },
943 { "getPathEffect", lpaint_getPathEffect },
944 { "__gc", lpaint_gc },
948 ///////////////////////////////////////////////////////////////////////////////
950 static const char* mode2string(SkShader::TileMode mode) {
951 static const char* gNames[] = { "clamp", "repeat", "mirror" };
952 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
956 static const char* gradtype2string(SkShader::GradientType t) {
957 static const char* gNames[] = {
958 "none", "color", "linear", "radial", "radial2", "sweep", "conical"
960 SkASSERT((unsigned)t < SK_ARRAY_COUNT(gNames));
964 static int lshader_isOpaque(lua_State* L) {
965 SkShader* shader = get_ref<SkShader>(L, 1);
966 return shader && shader->isOpaque();
969 static int lshader_asABitmap(lua_State* L) {
970 SkShader* shader = get_ref<SkShader>(L, 1);
974 SkShader::TileMode modes[2];
975 switch (shader->asABitmap(&bm, &matrix, modes)) {
976 case SkShader::kDefault_BitmapType:
978 setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
979 setfield_number(L, "width", bm.width());
980 setfield_number(L, "height", bm.height());
981 setfield_string(L, "tileX", mode2string(modes[0]));
982 setfield_string(L, "tileY", mode2string(modes[1]));
991 static int lshader_asAGradient(lua_State* L) {
992 SkShader* shader = get_ref<SkShader>(L, 1);
994 SkShader::GradientInfo info;
995 sk_bzero(&info, sizeof(info));
997 SkColor colors[3]; // hacked in for extracting info on 3 color case.
1000 info.fColorCount = 3;
1001 info.fColors = &colors[0];
1002 info.fColorOffsets = &pos[0];
1004 SkShader::GradientType t = shader->asAGradient(&info);
1006 if (SkShader::kNone_GradientType != t) {
1008 setfield_string(L, "type", gradtype2string(t));
1009 setfield_number(L, "colorCount", info.fColorCount);
1010 setfield_string(L, "tile", mode2string(info.fTileMode));
1012 if (info.fColorCount == 3){
1013 setfield_number(L, "midPos", pos[1]);
1022 static int lshader_gc(lua_State* L) {
1023 get_ref<SkShader>(L, 1)->unref();
1027 static const struct luaL_Reg gSkShader_Methods[] = {
1028 { "isOpaque", lshader_isOpaque },
1029 { "asABitmap", lshader_asABitmap },
1030 { "asAGradient", lshader_asAGradient },
1031 { "__gc", lshader_gc },
1035 ///////////////////////////////////////////////////////////////////////////////
1037 static int lpatheffect_asADash(lua_State* L) {
1038 SkPathEffect* pe = get_ref<SkPathEffect>(L, 1);
1040 SkPathEffect::DashInfo info;
1041 SkPathEffect::DashType dashType = pe->asADash(&info);
1042 if (SkPathEffect::kDash_DashType == dashType) {
1043 SkAutoTArray<SkScalar> intervals(info.fCount);
1044 info.fIntervals = intervals.get();
1046 SkLua(L).pushDash(info);
1053 static int lpatheffect_gc(lua_State* L) {
1054 get_ref<SkPathEffect>(L, 1)->unref();
1058 static const struct luaL_Reg gSkPathEffect_Methods[] = {
1059 { "asADash", lpatheffect_asADash },
1060 { "__gc", lpatheffect_gc },
1064 ///////////////////////////////////////////////////////////////////////////////
1066 static int lmatrix_getType(lua_State* L) {
1067 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
1070 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask));
1071 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1072 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1073 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Mask));
1077 static int lmatrix_getScaleX(lua_State* L) {
1078 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleX());
1082 static int lmatrix_getScaleY(lua_State* L) {
1083 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getScaleY());
1087 static int lmatrix_getTranslateX(lua_State* L) {
1088 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateX());
1092 static int lmatrix_getTranslateY(lua_State* L) {
1093 lua_pushnumber(L, get_obj<SkMatrix>(L,1)->getTranslateY());
1097 static const struct luaL_Reg gSkMatrix_Methods[] = {
1098 { "getType", lmatrix_getType },
1099 { "getScaleX", lmatrix_getScaleX },
1100 { "getScaleY", lmatrix_getScaleY },
1101 { "getTranslateX", lmatrix_getTranslateX },
1102 { "getTranslateY", lmatrix_getTranslateY },
1106 ///////////////////////////////////////////////////////////////////////////////
1108 static int lpath_getBounds(lua_State* L) {
1109 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
1113 static const char* fill_type_to_str(SkPath::FillType fill) {
1115 case SkPath::kEvenOdd_FillType:
1117 case SkPath::kWinding_FillType:
1119 case SkPath::kInverseEvenOdd_FillType:
1120 return "inverse-even-odd";
1121 case SkPath::kInverseWinding_FillType:
1122 return "inverse-winding";
1127 static int lpath_getFillType(lua_State* L) {
1128 SkPath::FillType fill = get_obj<SkPath>(L, 1)->getFillType();
1129 SkLua(L).pushString(fill_type_to_str(fill));
1133 static SkString segment_masks_to_str(uint32_t segmentMasks) {
1136 if (SkPath::kLine_SegmentMask & segmentMasks) {
1137 result.append("line");
1139 SkDEBUGCODE(segmentMasks &= ~SkPath::kLine_SegmentMask;)
1141 if (SkPath::kQuad_SegmentMask & segmentMasks) {
1145 result.append("quad");
1147 SkDEBUGCODE(segmentMasks &= ~SkPath::kQuad_SegmentMask;)
1149 if (SkPath::kConic_SegmentMask & segmentMasks) {
1153 result.append("conic");
1155 SkDEBUGCODE(segmentMasks &= ~SkPath::kConic_SegmentMask;)
1157 if (SkPath::kCubic_SegmentMask & segmentMasks) {
1161 result.append("cubic");
1162 SkDEBUGCODE(segmentMasks &= ~SkPath::kCubic_SegmentMask;)
1164 SkASSERT(0 == segmentMasks);
1168 static int lpath_getSegmentTypes(lua_State* L) {
1169 uint32_t segMasks = get_obj<SkPath>(L, 1)->getSegmentMasks();
1170 SkLua(L).pushString(segment_masks_to_str(segMasks));
1174 static int lpath_isConvex(lua_State* L) {
1175 bool isConvex = SkPath::kConvex_Convexity == get_obj<SkPath>(L, 1)->getConvexity();
1176 SkLua(L).pushBool(isConvex);
1180 static int lpath_isEmpty(lua_State* L) {
1181 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
1185 static int lpath_isRect(lua_State* L) {
1187 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
1189 lua_pushboolean(L, pred);
1191 SkLua(L).pushRect(r);
1197 static const char* dir2string(SkPath::Direction dir) {
1198 static const char* gStr[] = {
1199 "unknown", "cw", "ccw"
1201 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
1205 static int lpath_isNestedRects(lua_State* L) {
1207 SkPath::Direction dirs[2];
1208 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
1210 lua_pushboolean(L, pred);
1213 lua.pushRect(rects[0]);
1214 lua.pushRect(rects[1]);
1215 lua_pushstring(L, dir2string(dirs[0]));
1216 lua_pushstring(L, dir2string(dirs[0]));
1222 static int lpath_countPoints(lua_State* L) {
1223 lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
1227 static int lpath_reset(lua_State* L) {
1228 get_obj<SkPath>(L, 1)->reset();
1232 static int lpath_moveTo(lua_State* L) {
1233 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
1237 static int lpath_lineTo(lua_State* L) {
1238 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
1242 static int lpath_quadTo(lua_State* L) {
1243 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
1244 lua2scalar(L, 4), lua2scalar(L, 5));
1248 static int lpath_cubicTo(lua_State* L) {
1249 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
1250 lua2scalar(L, 4), lua2scalar(L, 5),
1251 lua2scalar(L, 6), lua2scalar(L, 7));
1255 static int lpath_close(lua_State* L) {
1256 get_obj<SkPath>(L, 1)->close();
1260 static int lpath_gc(lua_State* L) {
1261 get_obj<SkPath>(L, 1)->~SkPath();
1265 static const struct luaL_Reg gSkPath_Methods[] = {
1266 { "getBounds", lpath_getBounds },
1267 { "getFillType", lpath_getFillType },
1268 { "getSegmentTypes", lpath_getSegmentTypes },
1269 { "isConvex", lpath_isConvex },
1270 { "isEmpty", lpath_isEmpty },
1271 { "isRect", lpath_isRect },
1272 { "isNestedRects", lpath_isNestedRects },
1273 { "countPoints", lpath_countPoints },
1274 { "reset", lpath_reset },
1275 { "moveTo", lpath_moveTo },
1276 { "lineTo", lpath_lineTo },
1277 { "quadTo", lpath_quadTo },
1278 { "cubicTo", lpath_cubicTo },
1279 { "close", lpath_close },
1280 { "__gc", lpath_gc },
1284 ///////////////////////////////////////////////////////////////////////////////
1286 static const char* rrect_type(const SkRRect& rr) {
1287 switch (rr.getType()) {
1288 case SkRRect::kUnknown_Type: return "unknown";
1289 case SkRRect::kEmpty_Type: return "empty";
1290 case SkRRect::kRect_Type: return "rect";
1291 case SkRRect::kOval_Type: return "oval";
1292 case SkRRect::kSimple_Type: return "simple";
1293 case SkRRect::kNinePatch_Type: return "nine-patch";
1294 case SkRRect::kComplex_Type: return "complex";
1296 SkDEBUGFAIL("never get here");
1300 static int lrrect_rect(lua_State* L) {
1301 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
1305 static int lrrect_type(lua_State* L) {
1306 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
1310 static int lrrect_radii(lua_State* L) {
1311 int corner = SkToInt(lua_tointeger(L, 2));
1313 if (corner < 0 || corner > 3) {
1314 SkDebugf("bad corner index %d", corner);
1317 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
1319 lua_pushnumber(L, v.fX);
1320 lua_pushnumber(L, v.fY);
1324 static int lrrect_gc(lua_State* L) {
1325 get_obj<SkRRect>(L, 1)->~SkRRect();
1329 static const struct luaL_Reg gSkRRect_Methods[] = {
1330 { "rect", lrrect_rect },
1331 { "type", lrrect_type },
1332 { "radii", lrrect_radii },
1333 { "__gc", lrrect_gc },
1337 ///////////////////////////////////////////////////////////////////////////////
1339 static int limage_width(lua_State* L) {
1340 lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
1344 static int limage_height(lua_State* L) {
1345 lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
1349 static int limage_gc(lua_State* L) {
1350 get_ref<SkImage>(L, 1)->unref();
1354 static const struct luaL_Reg gSkImage_Methods[] = {
1355 { "width", limage_width },
1356 { "height", limage_height },
1357 { "__gc", limage_gc },
1361 ///////////////////////////////////////////////////////////////////////////////
1363 static int ltypeface_gc(lua_State* L) {
1364 SkSafeUnref(get_ref<SkTypeface>(L, 1));
1368 static const struct luaL_Reg gSkTypeface_Methods[] = {
1369 { "__gc", ltypeface_gc },
1373 ///////////////////////////////////////////////////////////////////////////////
1377 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
1378 lua_getglobal(L, func);
1379 if (!lua_isfunction(L, -1)) {
1380 int t = lua_type(L, -1);
1381 SkDebugf("--- expected function %d\n", t);
1385 setfield_string(L, "verb", verb);
1389 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
1390 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
1399 #define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
1401 ///////////////////////////////////////////////////////////////////////////////
1403 static int lsk_newDocumentPDF(lua_State* L) {
1404 const char* file = NULL;
1405 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1406 file = lua_tolstring(L, 1, NULL);
1409 SkDocument* doc = SkDocument::CreatePDF(file);
1411 // do I need to push a nil on the stack and return 1?
1420 static int lsk_newPaint(lua_State* L) {
1421 push_new<SkPaint>(L);
1425 static int lsk_newPath(lua_State* L) {
1426 push_new<SkPath>(L);
1430 static int lsk_newRRect(lua_State* L) {
1431 SkRRect* rr = push_new<SkRRect>(L);
1436 static int lsk_newTypeface(lua_State* L) {
1437 const char* name = NULL;
1438 int style = SkTypeface::kNormal;
1440 int count = lua_gettop(L);
1441 if (count > 0 && lua_isstring(L, 1)) {
1442 name = lua_tolstring(L, 1, NULL);
1443 if (count > 1 && lua_isnumber(L, 2)) {
1444 style = lua_tointegerx(L, 2, NULL) & SkTypeface::kBoldItalic;
1448 SkTypeface* face = SkTypeface::CreateFromName(name,
1449 (SkTypeface::Style)style);
1450 // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, face->getRefCnt());
1452 face = SkTypeface::RefDefault();
1459 static int lsk_loadImage(lua_State* L) {
1460 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
1461 const char* name = lua_tolstring(L, 1, NULL);
1462 SkAutoDataUnref data(SkData::NewFromFileName(name));
1464 SkImage* image = SkImage::NewFromGenerator(
1465 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
1477 static void register_Sk(lua_State* L) {
1479 lua_pushvalue(L, -1);
1480 lua_setglobal(L, "Sk");
1481 // the Sk table is still on top
1483 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
1484 setfield_function(L, "loadImage", lsk_loadImage);
1485 setfield_function(L, "newPaint", lsk_newPaint);
1486 setfield_function(L, "newPath", lsk_newPath);
1487 setfield_function(L, "newRRect", lsk_newRRect);
1488 setfield_function(L, "newTypeface", lsk_newTypeface);
1489 lua_pop(L, 1); // pop off the Sk table
1492 #define REG_CLASS(L, C) \
1494 luaL_newmetatable(L, get_mtname<C>()); \
1495 lua_pushvalue(L, -1); \
1496 lua_setfield(L, -2, "__index"); \
1497 luaL_setfuncs(L, g##C##_Methods, 0); \
1498 lua_pop(L, 1); /* pop off the meta-table */ \
1501 void SkLua::Load(lua_State* L) {
1503 REG_CLASS(L, SkCanvas);
1504 REG_CLASS(L, SkDocument);
1505 REG_CLASS(L, SkImage);
1506 REG_CLASS(L, SkPaint);
1507 REG_CLASS(L, SkPath);
1508 REG_CLASS(L, SkPathEffect);
1509 REG_CLASS(L, SkRRect);
1510 REG_CLASS(L, SkShader);
1511 REG_CLASS(L, SkTypeface);
1512 REG_CLASS(L, SkMatrix);
1515 extern "C" int luaopen_skia(lua_State* L);
1516 extern "C" int luaopen_skia(lua_State* L) {