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.
8 #include "SkLuaCanvas.h"
16 class AutoCallLua : public SkLua {
18 AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
19 lua_getglobal(L, func);
20 if (!lua_isfunction(L, -1)) {
21 int t = lua_type(L, -1);
22 SkDebugf("--- expected function %d\n", t);
26 this->pushString(verb, "verb");
30 lua_State* L = this->get();
31 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
32 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
37 void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
40 typedef SkLua INHERITED;
43 #define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
46 ///////////////////////////////////////////////////////////////////////////////
48 void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text,
51 case SkPaint::kUTF8_TextEncoding:
52 this->pushString((const char*)text, length, "text");
54 case SkPaint::kUTF16_TextEncoding: {
56 str.setUTF16((const uint16_t*)text, length);
57 this->pushString(str, "text");
59 case SkPaint::kGlyphID_TextEncoding:
60 this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
63 case SkPaint::kUTF32_TextEncoding:
68 ///////////////////////////////////////////////////////////////////////////////
70 void SkLuaCanvas::pushThis() {
71 SkLua(fL).pushCanvas(this);
74 ///////////////////////////////////////////////////////////////////////////////
76 SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
77 : INHERITED(width, height)
82 SkLuaCanvas::~SkLuaCanvas() {}
84 void SkLuaCanvas::willSave(SaveFlags flags) {
86 this->INHERITED::willSave(flags);
89 SkCanvas::SaveLayerStrategy SkLuaCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
91 AUTO_LUA("saveLayer");
93 lua.pushRect(*bounds, "bounds");
96 lua.pushPaint(*paint, "paint");
99 this->INHERITED::willSaveLayer(bounds, paint, flags);
100 // No need for a layer.
101 return kNoLayer_SaveLayerStrategy;
104 void SkLuaCanvas::willRestore() {
106 this->INHERITED::willRestore();
109 void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
110 switch (matrix.getType()) {
111 case SkMatrix::kTranslate_Mask: {
112 AUTO_LUA("translate");
113 lua.pushScalar(matrix.getTranslateX(), "dx");
114 lua.pushScalar(matrix.getTranslateY(), "dy");
117 case SkMatrix::kScale_Mask: {
119 lua.pushScalar(matrix.getScaleX(), "sx");
120 lua.pushScalar(matrix.getScaleY(), "sy");
125 lua.pushMatrix(matrix);
130 this->INHERITED::didConcat(matrix);
133 void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
134 this->INHERITED::didSetMatrix(matrix);
137 void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
138 AUTO_LUA("clipRect");
139 lua.pushRect(r, "rect");
140 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
141 this->INHERITED::onClipRect(r, op, edgeStyle);
144 void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
145 AUTO_LUA("clipRRect");
146 lua.pushRRect(rrect, "rrect");
147 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
148 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
151 void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
152 AUTO_LUA("clipPath");
153 lua.pushPath(path, "path");
154 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
155 this->INHERITED::onClipPath(path, op, edgeStyle);
158 void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
159 AUTO_LUA("clipRegion");
160 this->INHERITED::onClipRegion(deviceRgn, op);
163 void SkLuaCanvas::drawPaint(const SkPaint& paint) {
164 AUTO_LUA("drawPaint");
165 lua.pushPaint(paint, "paint");
168 void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
169 const SkPoint pts[], const SkPaint& paint) {
170 AUTO_LUA("drawPoints");
171 lua.pushArrayPoint(pts, count, "points");
172 lua.pushPaint(paint, "paint");
175 void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
176 AUTO_LUA("drawOval");
177 lua.pushRect(rect, "rect");
178 lua.pushPaint(paint, "paint");
181 void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
182 AUTO_LUA("drawRect");
183 lua.pushRect(rect, "rect");
184 lua.pushPaint(paint, "paint");
187 void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
188 AUTO_LUA("drawRRect");
189 lua.pushRRect(rrect, "rrect");
190 lua.pushPaint(paint, "paint");
193 void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
194 const SkPaint& paint) {
195 AUTO_LUA("drawDRRect");
196 lua.pushRRect(outer, "outer");
197 lua.pushRRect(inner, "inner");
198 lua.pushPaint(paint, "paint");
201 void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
202 AUTO_LUA("drawPath");
203 lua.pushPath(path, "path");
204 lua.pushPaint(paint, "paint");
207 void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
208 const SkPaint* paint) {
209 AUTO_LUA("drawBitmap");
211 lua.pushPaint(*paint, "paint");
215 void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
216 const SkRect& dst, const SkPaint* paint,
217 DrawBitmapRectFlags flags) {
218 AUTO_LUA("drawBitmapRectToRect");
220 lua.pushPaint(*paint, "paint");
224 void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
225 const SkPaint* paint) {
226 AUTO_LUA("drawBitmapMatrix");
228 lua.pushPaint(*paint, "paint");
232 void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
233 const SkPaint* paint) {
234 AUTO_LUA("drawSprite");
236 lua.pushPaint(*paint, "paint");
240 void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
241 SkScalar y, const SkPaint& paint) {
242 AUTO_LUA("drawText");
243 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
244 lua.pushPaint(paint, "paint");
247 void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
248 const SkPoint pos[], const SkPaint& paint) {
249 AUTO_LUA("drawPosText");
250 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
251 lua.pushPaint(paint, "paint");
254 void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
255 const SkScalar xpos[], SkScalar constY,
256 const SkPaint& paint) {
257 AUTO_LUA("drawPosTextH");
258 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
259 lua.pushPaint(paint, "paint");
262 void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
263 const SkPath& path, const SkMatrix* matrix,
264 const SkPaint& paint) {
265 AUTO_LUA("drawTextOnPath");
266 lua.pushPath(path, "path");
267 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
268 lua.pushPaint(paint, "paint");
271 void SkLuaCanvas::drawPicture(SkPicture& picture) {
272 AUTO_LUA("drawPicture");
273 // call through so we can see the nested picture ops
274 this->INHERITED::drawPicture(picture);
277 void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
278 const SkPoint vertices[], const SkPoint texs[],
279 const SkColor colors[], SkXfermode* xmode,
280 const uint16_t indices[], int indexCount,
281 const SkPaint& paint) {
282 AUTO_LUA("drawVertices");
283 lua.pushPaint(paint, "paint");
286 void SkLuaCanvas::drawData(const void* data, size_t length) {
287 AUTO_LUA("drawData");