2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
12 SkJSCanvas(SkCanvas* target);
19 void setLineWidth(double);
22 void moveTo(double x, double y);
23 void lineTo(double x, double y);
29 void fillText(const char text[], double x, double y);
38 SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
39 fFillPaint.setAntiAlias(true);
40 sk_tool_utils::set_portable_typeface(&fFillPaint);
41 fStrokePaint.setAntiAlias(true);
42 fStrokePaint.setStyle(SkPaint::kStroke_Style);
43 fStrokePaint.setStrokeWidth(SK_Scalar1);
46 SkJSCanvas::~SkJSCanvas() {}
48 void SkJSCanvas::save() { fTarget->save(); }
49 void SkJSCanvas::restore() { fTarget->restore(); }
51 void SkJSCanvas::beginPath() { fPath.reset(); }
52 void SkJSCanvas::moveTo(double x, double y) {
53 fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
56 void SkJSCanvas::lineTo(double x, double y) {
57 fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
60 void SkJSCanvas::closePath() { fPath.close(); }
62 void SkJSCanvas::fill() {
63 fTarget->drawPath(fPath, fFillPaint);
66 void SkJSCanvas::stroke() {
67 fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
68 fTarget->drawPath(fPath, fStrokePaint);
71 void SkJSCanvas::fillText(const char text[], double x, double y) {
72 fTarget->drawText(text, strlen(text),
73 SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
76 ///////////////////////////////////////////////////////////////////////////////
78 static void dump(const SkPath& path) {
79 const SkRect& r = path.getBounds();
80 SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(),
81 r.fLeft, r.fTop, r.fRight, r.fBottom);
84 static void test_stroke(SkCanvas* canvas) {
88 path.reset(); path.moveTo(0, 0);
90 path.reset(); path.moveTo(100, 100);
92 path.reset(); path.moveTo(0, 0); path.moveTo(100, 100);
94 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100);
96 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200);
101 // TEST 1 - The rectangle as it's expected to look
102 var canvas = document.createElement('canvas');
103 document.body.appendChild(canvas);
104 var ctx = canvas.getContext("2d");
106 SkJSCanvas ctx(canvas);
113 ctx.lineTo(150, 100);
118 // no extra moveTo here
119 // ctx.moveTo(175, 125);
124 ctx.fillText("As Expected", 10, 10);
127 // TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger
128 canvas = document.createElement('canvas');
129 document.body.appendChild(canvas);
130 ctx = canvas.getContext("2d");
132 canvas->translate(200, 0);
139 ctx.lineTo(150, 100);
144 ctx.moveTo(175, 125);
149 ctx.fillText("Larger Rectangle", 10, 10);
152 // TEST 3 - Identical to test 2 except the line width is 1
153 canvas = document.createElement('canvas');
154 document.body.appendChild(canvas);
155 ctx = canvas.getContext("2d");
157 canvas->translate(200, 0);
164 ctx.lineTo(150, 100);
169 ctx.moveTo(175, 125);
174 ctx.fillText("As Expected - line width 1", 10, 10);
177 class Poly2PolyGM : public skiagm::GM {
182 virtual uint32_t onGetFlags() const SK_OVERRIDE {
183 return kSkipTiled_Flag;
186 virtual SkString onShortName() SK_OVERRIDE {
187 return SkString("poly2poly");
190 virtual SkISize onISize() SK_OVERRIDE {
191 return SkISize::Make(835, 840);
194 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
195 const int idst[], int count) {
197 SkPoint src[4], dst[4];
199 for (int i = 0; i < count; i++) {
200 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
201 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
205 matrix.setPolyToPoly(src, dst, count);
206 canvas->concat(matrix);
208 paint->setColor(SK_ColorGRAY);
209 paint->setStyle(SkPaint::kStroke_Style);
210 const SkScalar D = SkIntToScalar(64);
211 canvas->drawRectCoords(0, 0, D, D, *paint);
212 canvas->drawLine(0, 0, D, D, *paint);
213 canvas->drawLine(0, D, D, 0, *paint);
215 SkPaint::FontMetrics fm;
216 paint->getFontMetrics(&fm);
217 paint->setColor(SK_ColorRED);
218 paint->setStyle(SkPaint::kFill_Style);
220 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
222 str.appendS32(count);
223 canvas->drawText(str.c_str(), str.size(), x, y, *paint);
228 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
229 if (false) { test_stroke(canvas); return; }
232 paint.setAntiAlias(true);
233 sk_tool_utils::set_portable_typeface(&paint);
234 paint.setStrokeWidth(SkIntToScalar(4));
235 paint.setTextSize(SkIntToScalar(40));
236 paint.setTextAlign(SkPaint::kCenter_Align);
239 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
240 // translate (1 point)
241 const int src1[] = { 0, 0 };
242 const int dst1[] = { 5, 5 };
243 doDraw(canvas, &paint, src1, dst1, 1);
247 canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
248 // rotate/uniform-scale (2 points)
249 const int src2[] = { 32, 32, 64, 32 };
250 const int dst2[] = { 32, 32, 64, 48 };
251 doDraw(canvas, &paint, src2, dst2, 2);
255 canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
256 // rotate/skew (3 points)
257 const int src3[] = { 0, 0, 64, 0, 0, 64 };
258 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
259 doDraw(canvas, &paint, src3, dst3, 3);
263 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
264 // perspective (4 points)
265 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
266 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
267 doDraw(canvas, &paint, src4, dst4, 4);
272 typedef skiagm::GM INHERITED;
275 //////////////////////////////////////////////////////////////////////////////
277 DEF_GM( return new Poly2PolyGM; )