blob: c36ac8548398b6170c407470be668c3bd391c120 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@google.com8ea69832013-06-21 15:11:40 +00007
reed@google.com67981c52011-02-17 14:21:01 +00008#include "gm.h"
9
reed@google.com8ea69832013-06-21 15:11:40 +000010class SkJSCanvas {
11public:
12 SkJSCanvas(SkCanvas* target);
13 ~SkJSCanvas();
reed@google.com67981c52011-02-17 14:21:01 +000014
reed@google.com8ea69832013-06-21 15:11:40 +000015 void save();
16 void restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000017
reed@google.com8ea69832013-06-21 15:11:40 +000018 double lineWidth;
19 void setLineWidth(double);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000020
reed@google.com8ea69832013-06-21 15:11:40 +000021 void beginPath();
22 void moveTo(double x, double y);
23 void lineTo(double x, double y);
24 void closePath();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000025
reed@google.com8ea69832013-06-21 15:11:40 +000026 void fill();
27 void stroke();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000028
reed@google.com8ea69832013-06-21 15:11:40 +000029 void fillText(const char text[], double x, double y);
30
31private:
32 SkCanvas* fTarget;
33 SkPaint fFillPaint;
34 SkPaint fStrokePaint;
35 SkPath fPath;
36};
37
38SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
39 fFillPaint.setAntiAlias(true);
40 fStrokePaint.setAntiAlias(true);
41 fStrokePaint.setStyle(SkPaint::kStroke_Style);
42 fStrokePaint.setStrokeWidth(SK_Scalar1);
43}
44
45SkJSCanvas::~SkJSCanvas() {}
46
47void SkJSCanvas::save() { fTarget->save(); }
48void SkJSCanvas::restore() { fTarget->restore(); }
49
50void SkJSCanvas::beginPath() { fPath.reset(); }
51void SkJSCanvas::moveTo(double x, double y) {
52 fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
53}
54
55void SkJSCanvas::lineTo(double x, double y) {
56 fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
57}
58
59void SkJSCanvas::closePath() { fPath.close(); }
60
61void SkJSCanvas::fill() {
62 fTarget->drawPath(fPath, fFillPaint);
63}
64
65void SkJSCanvas::stroke() {
66 fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
67 fTarget->drawPath(fPath, fStrokePaint);
68}
69
70void SkJSCanvas::fillText(const char text[], double x, double y) {
71 fTarget->drawText(text, strlen(text),
72 SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
73}
74
75///////////////////////////////////////////////////////////////////////////////
76
77static void dump(const SkPath& path) {
78 const SkRect& r = path.getBounds();
79 SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(),
80 r.fLeft, r.fTop, r.fRight, r.fBottom);
81}
82
83static void test_stroke(SkCanvas* canvas) {
84 if (true) {
85 SkPath path;
86 dump(path);
87 path.reset(); path.moveTo(0, 0);
88 dump(path);
89 path.reset(); path.moveTo(100, 100);
90 dump(path);
91 path.reset(); path.moveTo(0, 0); path.moveTo(100, 100);
92 dump(path);
93 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100);
94 dump(path);
95 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200);
96 dump(path);
97 }
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000098
reed@google.com8ea69832013-06-21 15:11:40 +000099#if 0
100 // TEST 1 - The rectangle as it's expected to look
101 var canvas = document.createElement('canvas');
102 document.body.appendChild(canvas);
103 var ctx = canvas.getContext("2d");
104#else
105 SkJSCanvas ctx(canvas);
106#endif
107
108 ctx.save();
109 ctx.lineWidth = 2;
110 ctx.beginPath();
111 ctx.moveTo(10, 100);
112 ctx.lineTo(150, 100);
113 ctx.lineTo(150, 15);
114 ctx.lineTo(10, 15);
115 ctx.closePath();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000116
reed@google.com8ea69832013-06-21 15:11:40 +0000117 // no extra moveTo here
118 // ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000119
reed@google.com8ea69832013-06-21 15:11:40 +0000120 ctx.stroke();
121 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000122
reed@google.com8ea69832013-06-21 15:11:40 +0000123 ctx.fillText("As Expected", 10, 10);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000124
reed@google.com8ea69832013-06-21 15:11:40 +0000125#if 0
126 // TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger
127 canvas = document.createElement('canvas');
128 document.body.appendChild(canvas);
129 ctx = canvas.getContext("2d");
130#else
131 canvas->translate(200, 0);
132#endif
133
134 ctx.save();
135 ctx.lineWidth = 2;
136 ctx.beginPath();
137 ctx.moveTo(10, 100);
138 ctx.lineTo(150, 100);
139 ctx.lineTo(150, 15);
140 ctx.lineTo(10, 15);
141 ctx.closePath();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000142
reed@google.com8ea69832013-06-21 15:11:40 +0000143 ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000144
reed@google.com8ea69832013-06-21 15:11:40 +0000145 ctx.stroke();
146 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000147
reed@google.com8ea69832013-06-21 15:11:40 +0000148 ctx.fillText("Larger Rectangle", 10, 10);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000149
reed@google.com8ea69832013-06-21 15:11:40 +0000150#if 0
151 // TEST 3 - Identical to test 2 except the line width is 1
152 canvas = document.createElement('canvas');
153 document.body.appendChild(canvas);
154 ctx = canvas.getContext("2d");
155#else
156 canvas->translate(200, 0);
157#endif
158
159 ctx.save();
160 ctx.lineWidth = 1;
161 ctx.beginPath();
162 ctx.moveTo(10, 100);
163 ctx.lineTo(150, 100);
164 ctx.lineTo(150, 15);
165 ctx.lineTo(10, 15);
166 ctx.closePath();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000167
reed@google.com8ea69832013-06-21 15:11:40 +0000168 ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000169
reed@google.com8ea69832013-06-21 15:11:40 +0000170 ctx.stroke();
171 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000172
reed@google.com8ea69832013-06-21 15:11:40 +0000173 ctx.fillText("As Expected - line width 1", 10, 10);
174}
175
176class Poly2PolyGM : public skiagm::GM {
reed@google.com67981c52011-02-17 14:21:01 +0000177public:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178 Poly2PolyGM() {}
179
reed@google.com67981c52011-02-17 14:21:01 +0000180protected:
reed@google.com8ea69832013-06-21 15:11:40 +0000181 virtual SkString onShortName() SK_OVERRIDE {
reed@google.com67981c52011-02-17 14:21:01 +0000182 return SkString("poly2poly");
183 }
184
reed@google.com8ea69832013-06-21 15:11:40 +0000185 virtual SkISize onISize() SK_OVERRIDE {
186 return SkISize::Make(835, 840);
reed@google.com67981c52011-02-17 14:21:01 +0000187 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
reed@google.com67981c52011-02-17 14:21:01 +0000189 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
190 const int idst[], int count) {
191 SkMatrix matrix;
192 SkPoint src[4], dst[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
reed@google.com67981c52011-02-17 14:21:01 +0000194 for (int i = 0; i < count; i++) {
195 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
196 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
197 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@google.com67981c52011-02-17 14:21:01 +0000199 canvas->save();
200 matrix.setPolyToPoly(src, dst, count);
201 canvas->concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000202
reed@google.com67981c52011-02-17 14:21:01 +0000203 paint->setColor(SK_ColorGRAY);
204 paint->setStyle(SkPaint::kStroke_Style);
205 const SkScalar D = SkIntToScalar(64);
206 canvas->drawRectCoords(0, 0, D, D, *paint);
207 canvas->drawLine(0, 0, D, D, *paint);
208 canvas->drawLine(0, D, D, 0, *paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209
reed@google.com67981c52011-02-17 14:21:01 +0000210 SkPaint::FontMetrics fm;
211 paint->getFontMetrics(&fm);
212 paint->setColor(SK_ColorRED);
213 paint->setStyle(SkPaint::kFill_Style);
214 SkScalar x = D/2;
bsalomon@google.com72e49b82011-10-27 21:47:03 +0000215 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
reed@google.com67981c52011-02-17 14:21:01 +0000216 SkString str;
217 str.appendS32(count);
218 canvas->drawText(str.c_str(), str.size(), x, y, *paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000219
reed@google.com67981c52011-02-17 14:21:01 +0000220 canvas->restore();
221 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
reed@google.com8ea69832013-06-21 15:11:40 +0000223 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
224 if (false) { test_stroke(canvas); return; }
225
reed@google.com67981c52011-02-17 14:21:01 +0000226 SkPaint paint;
227 paint.setAntiAlias(true);
228 paint.setStrokeWidth(SkIntToScalar(4));
229 paint.setTextSize(SkIntToScalar(40));
230 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000231
reed@google.com67981c52011-02-17 14:21:01 +0000232 canvas->save();
233 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
234 // translate (1 point)
235 const int src1[] = { 0, 0 };
236 const int dst1[] = { 5, 5 };
237 doDraw(canvas, &paint, src1, dst1, 1);
238 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000239
reed@google.com67981c52011-02-17 14:21:01 +0000240 canvas->save();
241 canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
242 // rotate/uniform-scale (2 points)
243 const int src2[] = { 32, 32, 64, 32 };
244 const int dst2[] = { 32, 32, 64, 48 };
245 doDraw(canvas, &paint, src2, dst2, 2);
246 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247
reed@google.com67981c52011-02-17 14:21:01 +0000248 canvas->save();
249 canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
250 // rotate/skew (3 points)
251 const int src3[] = { 0, 0, 64, 0, 0, 64 };
252 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
253 doDraw(canvas, &paint, src3, dst3, 3);
254 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000255
reed@google.com67981c52011-02-17 14:21:01 +0000256 canvas->save();
257 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
258 // perspective (4 points)
259 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
260 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
261 doDraw(canvas, &paint, src4, dst4, 4);
262 canvas->restore();
263 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
reed@google.com67981c52011-02-17 14:21:01 +0000265private:
reed@google.com8ea69832013-06-21 15:11:40 +0000266 typedef skiagm::GM INHERITED;
reed@google.com67981c52011-02-17 14:21:01 +0000267};
268
269//////////////////////////////////////////////////////////////////////////////
270
reed@google.com8ea69832013-06-21 15:11:40 +0000271DEF_GM( return new Poly2PolyGM; )