blob: 0b6be197c1c351a4c1db203ad3f541fcdb466841 [file] [log] [blame]
reed@google.comdff7e112013-05-15 19:34:20 +00001/*
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 "SkLuaCanvas.h"
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +00009#include "SkRRect.h"
reed@google.comdff7e112013-05-15 19:34:20 +000010
11extern "C" {
12 #include "lua.h"
13}
14
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000015static void setfield_string(lua_State* L, const char key[], const char value[]) {
16 lua_pushstring(L, value);
17 lua_setfield(L, -2, key);
reed@google.comdff7e112013-05-15 19:34:20 +000018}
19
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000020static void setfield_number(lua_State* L, const char key[], double value) {
21 lua_pushnumber(L, value);
22 lua_setfield(L, -2, key);
23}
24
25static void setfield_bool(lua_State* L, const char key[], bool value) {
26 lua_pushboolean(L, value);
27 lua_setfield(L, -2, key);
28}
29
30static void setfield_rect(lua_State* L, const char key[], const SkRect& r) {
31 lua_newtable(L);
32 setfield_number(L, "left", r.fLeft);
33 setfield_number(L, "top", r.fTop);
34 setfield_number(L, "right", r.fRight);
35 setfield_number(L, "bottom", r.fBottom);
36 lua_setfield(L, -2, key);
37}
38
39enum PaintUsage {
40 kText_PaintUsage,
41 kImage_PaintUsage,
42 kGeometry_PaintUsage
43};
44
45static const char* color2string(SkColor c, SkString* str) {
46 str->printf("0x%08X", c);
47 return str->c_str();
48}
49
50static void setfield_paint(lua_State* L, const SkPaint& p,
51 PaintUsage pu = kGeometry_PaintUsage) {
52 SkString str;
53
54 lua_newtable(L);
55 setfield_bool(L, "aa", p.isAntiAlias());
56 setfield_string(L, "color", color2string(p.getColor(), &str));
57
58 if (kGeometry_PaintUsage == pu) {
59 if (SkPaint::kFill_Style != p.getStyle()) {
60 setfield_number(L, "stroke-width", p.getStrokeWidth());
61 }
62 }
63 lua_setfield(L, -2, "paint");
64}
65
66class AutoCallLua {
67public:
68 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
69 lua_getglobal(L, func);
70 if (!lua_isfunction(L, -1)) {
71 int t = lua_type(L, -1);
72 SkDebugf("--- expected function %d\n", t);
73 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000074
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000075 lua_newtable(L);
76 setfield_string(L, "verb", verb);
77 }
78
79 ~AutoCallLua() {
80 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
81 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
82 }
83 lua_settop(fL, -1);
84 }
85
86private:
87 lua_State* fL;
88};
89
90#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
91
reed@google.comdff7e112013-05-15 19:34:20 +000092///////////////////////////////////////////////////////////////////////////////
93
94static SkBitmap make_bm(int width, int height) {
95 SkBitmap bm;
96 bm.setConfig(SkBitmap::kNo_Config, width, height);
97 return bm;
98}
99
100SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
101 : INHERITED(make_bm(width, height))
102 , fL(L)
103 , fFunc(func) {
104}
105
106SkLuaCanvas::~SkLuaCanvas() {}
107
108int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000109 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +0000110 return this->INHERITED::save(flags);
111}
112
113int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
114 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000115 AUTO_LUA("saveLayer");
116 if (bounds) {
117 setfield_rect(fL, "bounds", *bounds);
118 }
119 if (paint) {
120 setfield_paint(fL, *paint);
121 }
reed@google.comdff7e112013-05-15 19:34:20 +0000122 return this->INHERITED::save(flags);
123}
124
125void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000126 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +0000127 this->INHERITED::restore();
128}
129
130bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000131 AUTO_LUA("translate");
132 setfield_number(fL, "dx", dx);
133 setfield_number(fL, "dy", dy);
reed@google.comdff7e112013-05-15 19:34:20 +0000134 return this->INHERITED::translate(dx, dy);
135}
136
137bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000138 AUTO_LUA("scale");
139 setfield_number(fL, "sx", sx);
140 setfield_number(fL, "sy", sy);
reed@google.comdff7e112013-05-15 19:34:20 +0000141 return this->INHERITED::scale(sx, sy);
142}
143
144bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000145 AUTO_LUA("rotate");
146 setfield_number(fL, "degrees", degrees);
reed@google.comdff7e112013-05-15 19:34:20 +0000147 return this->INHERITED::rotate(degrees);
148}
149
150bool SkLuaCanvas::skew(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000151 AUTO_LUA("skew");
152 setfield_number(fL, "sx", sx);
153 setfield_number(fL, "sy", sy);
reed@google.comdff7e112013-05-15 19:34:20 +0000154 return this->INHERITED::skew(sx, sy);
155}
156
157bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000158 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000159 return this->INHERITED::concat(matrix);
160}
161
162void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
163 this->INHERITED::setMatrix(matrix);
164}
165
166bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000167 AUTO_LUA("clipRect");
168 setfield_rect(fL, "rect", r);
169 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000170 return this->INHERITED::clipRect(r, op, doAA);
171}
172
173bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000174 AUTO_LUA("clipRRect");
175 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000176 return this->INHERITED::clipRRect(rrect, op, doAA);
177}
178
179bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000180 AUTO_LUA("clipPath");
181 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000182 return this->INHERITED::clipPath(path, op, doAA);
183}
184
185bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000186 AUTO_LUA("clipRegion");
reed@google.comdff7e112013-05-15 19:34:20 +0000187 return this->INHERITED::clipRegion(deviceRgn, op);
188}
189
190void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000191 AUTO_LUA("drawPaint");
192 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000193}
194
195void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
196 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000197 AUTO_LUA("drawPoints");
198 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000199}
200
201void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000202 AUTO_LUA("drawOval");
203 setfield_rect(fL, "rect", rect);
204 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000205}
206
207void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000208 AUTO_LUA("drawRect");
209 setfield_rect(fL, "rect", rect);
210 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000211}
212
213void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000214 AUTO_LUA("drawRRect");
215 setfield_rect(fL, "rect", rrect.getBounds());
216 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000217}
218
219void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000220 AUTO_LUA("drawPath");
221 setfield_rect(fL, "bounds", path.getBounds());
222 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000223}
224
225void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
226 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000227 AUTO_LUA("drawBitmap");
228 if (paint) {
229 setfield_paint(fL, *paint, kImage_PaintUsage);
230 }
reed@google.comdff7e112013-05-15 19:34:20 +0000231}
232
233void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
234 const SkRect& dst, const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000235 AUTO_LUA("drawBitmapRectToRect");
236 if (paint) {
237 setfield_paint(fL, *paint, kImage_PaintUsage);
238 }
reed@google.comdff7e112013-05-15 19:34:20 +0000239}
240
241void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
242 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000243 AUTO_LUA("drawBitmapMatrix");
244 if (paint) {
245 setfield_paint(fL, *paint, kImage_PaintUsage);
246 }
reed@google.comdff7e112013-05-15 19:34:20 +0000247}
248
249void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
250 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000251 AUTO_LUA("drawSprite");
252 if (paint) {
253 setfield_paint(fL, *paint, kImage_PaintUsage);
254 }
reed@google.comdff7e112013-05-15 19:34:20 +0000255}
256
257void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
258 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000259 AUTO_LUA("drawText");
260 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000261}
262
263void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
264 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000265 AUTO_LUA("drawPosText");
266 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000267}
268
269void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
270 const SkScalar xpos[], SkScalar constY,
271 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000272 AUTO_LUA("drawPosTextH");
273 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000274}
275
276void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
277 const SkPath& path, const SkMatrix* matrix,
278 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000279 AUTO_LUA("drawTextOnPath");
280 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000281}
282
283void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000284 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000285 // call through so we can see the nested picture ops
286 this->INHERITED::drawPicture(picture);
287}
288
289void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
290 const SkPoint vertices[], const SkPoint texs[],
291 const SkColor colors[], SkXfermode* xmode,
292 const uint16_t indices[], int indexCount,
293 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000294 AUTO_LUA("drawVertices");
295 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000296}
297
298void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000299 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000300}