1 /*
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 */
7
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "Resources.h"
11 #include "SkPath.h"
12 #include "SkTypeface.h"
13
14 class SkJSCanvas {
15 public:
16 SkJSCanvas(SkCanvas* target);
17 ~SkJSCanvas();
18
19 void save();
20 void restore();
21
22 double lineWidth;
23 void setLineWidth(double);
24
25 void beginPath();
26 void moveTo(double x, double y);
27 void lineTo(double x, double y);
28 void closePath();
29
30 void fill();
31 void stroke();
32
33 void fillText(const char text[], double x, double y);
34
35 private:
36 SkCanvas* fTarget;
37 SkPaint fFillPaint;
38 SkPaint fStrokePaint;
39 SkPath fPath;
40 };
41
SkJSCanvas(SkCanvas * target)42 SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
43 fFillPaint.setAntiAlias(true);
44 sk_tool_utils::set_portable_typeface(&fFillPaint);
45 fStrokePaint.setAntiAlias(true);
46 fStrokePaint.setStyle(SkPaint::kStroke_Style);
47 fStrokePaint.setStrokeWidth(SK_Scalar1);
48 }
49
~SkJSCanvas()50 SkJSCanvas::~SkJSCanvas() {}
51
save()52 void SkJSCanvas::save() { fTarget->save(); }
restore()53 void SkJSCanvas::restore() { fTarget->restore(); }
54
beginPath()55 void SkJSCanvas::beginPath() { fPath.reset(); }
moveTo(double x,double y)56 void SkJSCanvas::moveTo(double x, double y) {
57 fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
58 }
59
lineTo(double x,double y)60 void SkJSCanvas::lineTo(double x, double y) {
61 fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
62 }
63
closePath()64 void SkJSCanvas::closePath() { fPath.close(); }
65
fill()66 void SkJSCanvas::fill() {
67 fTarget->drawPath(fPath, fFillPaint);
68 }
69
stroke()70 void SkJSCanvas::stroke() {
71 fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
72 fTarget->drawPath(fPath, fStrokePaint);
73 }
74
fillText(const char text[],double x,double y)75 void SkJSCanvas::fillText(const char text[], double x, double y) {
76 fTarget->drawText(text, strlen(text),
77 SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
78 }
79
80 ///////////////////////////////////////////////////////////////////////////////
81
dump(const SkPath & path)82 static void dump(const SkPath& path) {
83 const SkRect& r = path.getBounds();
84 SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(),
85 r.fLeft, r.fTop, r.fRight, r.fBottom);
86 }
87
test_stroke(SkCanvas * canvas)88 static void test_stroke(SkCanvas* canvas) {
89 if (true) {
90 SkPath path;
91 dump(path);
92 path.reset(); path.moveTo(0, 0);
93 dump(path);
94 path.reset(); path.moveTo(100, 100);
95 dump(path);
96 path.reset(); path.moveTo(0, 0); path.moveTo(100, 100);
97 dump(path);
98 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100);
99 dump(path);
100 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200);
101 dump(path);
102 }
103
104 #if 0
105 // TEST 1 - The rectangle as it's expected to look
106 var canvas = document.createElement('canvas');
107 document.body.appendChild(canvas);
108 var ctx = canvas.getContext("2d");
109 #else
110 SkJSCanvas ctx(canvas);
111 #endif
112
113 ctx.save();
114 ctx.lineWidth = 2;
115 ctx.beginPath();
116 ctx.moveTo(10, 100);
117 ctx.lineTo(150, 100);
118 ctx.lineTo(150, 15);
119 ctx.lineTo(10, 15);
120 ctx.closePath();
121
122 // no extra moveTo here
123 // ctx.moveTo(175, 125);
124
125 ctx.stroke();
126 ctx.restore();
127
128 ctx.fillText("As Expected", 10, 10);
129
130 #if 0
131 // TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger
132 canvas = document.createElement('canvas');
133 document.body.appendChild(canvas);
134 ctx = canvas.getContext("2d");
135 #else
136 canvas->translate(200, 0);
137 #endif
138
139 ctx.save();
140 ctx.lineWidth = 2;
141 ctx.beginPath();
142 ctx.moveTo(10, 100);
143 ctx.lineTo(150, 100);
144 ctx.lineTo(150, 15);
145 ctx.lineTo(10, 15);
146 ctx.closePath();
147
148 ctx.moveTo(175, 125);
149
150 ctx.stroke();
151 ctx.restore();
152
153 ctx.fillText("Larger Rectangle", 10, 10);
154
155 #if 0
156 // TEST 3 - Identical to test 2 except the line width is 1
157 canvas = document.createElement('canvas');
158 document.body.appendChild(canvas);
159 ctx = canvas.getContext("2d");
160 #else
161 canvas->translate(200, 0);
162 #endif
163
164 ctx.save();
165 ctx.lineWidth = 1;
166 ctx.beginPath();
167 ctx.moveTo(10, 100);
168 ctx.lineTo(150, 100);
169 ctx.lineTo(150, 15);
170 ctx.lineTo(10, 15);
171 ctx.closePath();
172
173 ctx.moveTo(175, 125);
174
175 ctx.stroke();
176 ctx.restore();
177
178 ctx.fillText("As Expected - line width 1", 10, 10);
179 }
180
181 class Poly2PolyGM : public skiagm::GM {
182 public:
Poly2PolyGM()183 Poly2PolyGM() {}
184
185 protected:
186
onShortName()187 SkString onShortName() override {
188 return SkString("poly2poly");
189 }
190
onISize()191 SkISize onISize() override {
192 return SkISize::Make(835, 840);
193 }
194
doDraw(SkCanvas * canvas,SkPaint * paint,const int isrc[],const int idst[],int count)195 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
196 const int idst[], int count) {
197 SkMatrix matrix;
198 SkPoint src[4], dst[4];
199
200 for (int i = 0; i < count; i++) {
201 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
202 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
203 }
204
205 canvas->save();
206 matrix.setPolyToPoly(src, dst, count);
207 canvas->concat(matrix);
208
209 paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
210 paint->setStyle(SkPaint::kStroke_Style);
211 const SkScalar D = 64;
212 canvas->drawRect(SkRect::MakeWH(D, D), *paint);
213 canvas->drawLine(0, 0, D, D, *paint);
214 canvas->drawLine(0, D, D, 0, *paint);
215
216 SkPaint::FontMetrics fm;
217 paint->getFontMetrics(&fm);
218 paint->setColor(SK_ColorRED);
219 paint->setStyle(SkPaint::kFill_Style);
220 SkScalar x = D/2;
221 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
222 uint16_t glyphID = 3; // X
223 canvas->drawText((void*) &glyphID, sizeof(glyphID), x, y, *paint);
224 canvas->restore();
225 }
226
onOnceBeforeDraw()227 void onOnceBeforeDraw() override {
228 fEmFace = MakeResourceAsTypeface("/fonts/Em.ttf");
229 }
230
onDraw(SkCanvas * canvas)231 void onDraw(SkCanvas* canvas) override {
232 if (false) { test_stroke(canvas); return; }
233
234 SkPaint paint;
235 paint.setAntiAlias(true);
236 paint.setTypeface(fEmFace);
237 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
238 paint.setStrokeWidth(SkIntToScalar(4));
239 paint.setTextSize(SkIntToScalar(40));
240 paint.setTextAlign(SkPaint::kCenter_Align);
241
242 canvas->save();
243 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
244 // translate (1 point)
245 const int src1[] = { 0, 0 };
246 const int dst1[] = { 5, 5 };
247 doDraw(canvas, &paint, src1, dst1, 1);
248 canvas->restore();
249
250 canvas->save();
251 canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
252 // rotate/uniform-scale (2 points)
253 const int src2[] = { 32, 32, 64, 32 };
254 const int dst2[] = { 32, 32, 64, 48 };
255 doDraw(canvas, &paint, src2, dst2, 2);
256 canvas->restore();
257
258 canvas->save();
259 canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
260 // rotate/skew (3 points)
261 const int src3[] = { 0, 0, 64, 0, 0, 64 };
262 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
263 doDraw(canvas, &paint, src3, dst3, 3);
264 canvas->restore();
265
266 canvas->save();
267 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
268 // perspective (4 points)
269 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
270 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
271 doDraw(canvas, &paint, src4, dst4, 4);
272 canvas->restore();
273 }
274
275 private:
276 typedef skiagm::GM INHERITED;
277 sk_sp<SkTypeface> fEmFace;
278 };
279
280 //////////////////////////////////////////////////////////////////////////////
281
282 DEF_GM( return new Poly2PolyGM; )
283