blob: f298412bfd8b4f898003fd6e278218c492b2f47a [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
caryclarkf597c422015-07-28 10:37:53 -070010#include "Resources.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
Mike Reed71f5a0b2018-10-25 16:12:39 -040012#include "SkTextUtils.h"
caryclarkf597c422015-07-28 10:37:53 -070013#include "SkTypeface.h"
reed@google.com67981c52011-02-17 14:21:01 +000014
reed@google.com8ea69832013-06-21 15:11:40 +000015class SkJSCanvas {
16public:
17 SkJSCanvas(SkCanvas* target);
18 ~SkJSCanvas();
reed@google.com67981c52011-02-17 14:21:01 +000019
reed@google.com8ea69832013-06-21 15:11:40 +000020 void save();
21 void restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000022
reed@google.com8ea69832013-06-21 15:11:40 +000023 double lineWidth;
24 void setLineWidth(double);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000025
reed@google.com8ea69832013-06-21 15:11:40 +000026 void beginPath();
27 void moveTo(double x, double y);
28 void lineTo(double x, double y);
29 void closePath();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000030
reed@google.com8ea69832013-06-21 15:11:40 +000031 void fill();
32 void stroke();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +000033
reed@google.com8ea69832013-06-21 15:11:40 +000034 void fillText(const char text[], double x, double y);
35
36private:
37 SkCanvas* fTarget;
38 SkPaint fFillPaint;
39 SkPaint fStrokePaint;
40 SkPath fPath;
41};
42
43SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
44 fFillPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070045 sk_tool_utils::set_portable_typeface(&fFillPaint);
reed@google.com8ea69832013-06-21 15:11:40 +000046 fStrokePaint.setAntiAlias(true);
47 fStrokePaint.setStyle(SkPaint::kStroke_Style);
48 fStrokePaint.setStrokeWidth(SK_Scalar1);
49}
50
51SkJSCanvas::~SkJSCanvas() {}
52
53void SkJSCanvas::save() { fTarget->save(); }
54void SkJSCanvas::restore() { fTarget->restore(); }
55
56void SkJSCanvas::beginPath() { fPath.reset(); }
57void SkJSCanvas::moveTo(double x, double y) {
58 fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
59}
60
61void SkJSCanvas::lineTo(double x, double y) {
62 fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
63}
64
65void SkJSCanvas::closePath() { fPath.close(); }
66
67void SkJSCanvas::fill() {
68 fTarget->drawPath(fPath, fFillPaint);
69}
70
71void SkJSCanvas::stroke() {
72 fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
73 fTarget->drawPath(fPath, fStrokePaint);
74}
75
76void SkJSCanvas::fillText(const char text[], double x, double y) {
Cary Clark2a475ea2017-04-28 15:35:12 -040077 fTarget->drawString(text, SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
reed@google.com8ea69832013-06-21 15:11:40 +000078}
79
80///////////////////////////////////////////////////////////////////////////////
81
82static 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
88static 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 }
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000103
reed@google.com8ea69832013-06-21 15:11:40 +0000104#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();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000121
reed@google.com8ea69832013-06-21 15:11:40 +0000122 // no extra moveTo here
123 // ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000124
reed@google.com8ea69832013-06-21 15:11:40 +0000125 ctx.stroke();
126 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000127
reed@google.com8ea69832013-06-21 15:11:40 +0000128 ctx.fillText("As Expected", 10, 10);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000129
reed@google.com8ea69832013-06-21 15:11:40 +0000130#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();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000147
reed@google.com8ea69832013-06-21 15:11:40 +0000148 ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000149
reed@google.com8ea69832013-06-21 15:11:40 +0000150 ctx.stroke();
151 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000152
reed@google.com8ea69832013-06-21 15:11:40 +0000153 ctx.fillText("Larger Rectangle", 10, 10);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000154
reed@google.com8ea69832013-06-21 15:11:40 +0000155#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();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000172
reed@google.com8ea69832013-06-21 15:11:40 +0000173 ctx.moveTo(175, 125);
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000174
reed@google.com8ea69832013-06-21 15:11:40 +0000175 ctx.stroke();
176 ctx.restore();
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000177
reed@google.com8ea69832013-06-21 15:11:40 +0000178 ctx.fillText("As Expected - line width 1", 10, 10);
179}
180
181class Poly2PolyGM : public skiagm::GM {
reed@google.com67981c52011-02-17 14:21:01 +0000182public:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183 Poly2PolyGM() {}
184
reed@google.com67981c52011-02-17 14:21:01 +0000185protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000186
mtklein36352bf2015-03-25 18:17:31 -0700187 SkString onShortName() override {
reed@google.com67981c52011-02-17 14:21:01 +0000188 return SkString("poly2poly");
189 }
190
mtklein36352bf2015-03-25 18:17:31 -0700191 SkISize onISize() override {
reed@google.com8ea69832013-06-21 15:11:40 +0000192 return SkISize::Make(835, 840);
reed@google.com67981c52011-02-17 14:21:01 +0000193 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
reed@google.com67981c52011-02-17 14:21:01 +0000195 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];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
reed@google.com67981c52011-02-17 14:21:01 +0000200 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@google.com67981c52011-02-17 14:21:01 +0000205 canvas->save();
206 matrix.setPolyToPoly(src, dst, count);
207 canvas->concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000208
Mike Kleind46dce32018-08-16 10:17:03 -0400209 paint->setColor(SK_ColorGRAY);
reed@google.com67981c52011-02-17 14:21:01 +0000210 paint->setStyle(SkPaint::kStroke_Style);
Mike Reed3661bc92017-02-22 13:21:42 -0500211 const SkScalar D = 64;
212 canvas->drawRect(SkRect::MakeWH(D, D), *paint);
reed@google.com67981c52011-02-17 14:21:01 +0000213 canvas->drawLine(0, 0, D, D, *paint);
214 canvas->drawLine(0, D, D, 0, *paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215
Mike Reedb5784ac2018-11-12 09:35:15 -0500216 SkFontMetrics fm;
reed@google.com67981c52011-02-17 14:21:01 +0000217 paint->getFontMetrics(&fm);
218 paint->setColor(SK_ColorRED);
219 paint->setStyle(SkPaint::kFill_Style);
220 SkScalar x = D/2;
bsalomon@google.com72e49b82011-10-27 21:47:03 +0000221 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
caryclarkf597c422015-07-28 10:37:53 -0700222 uint16_t glyphID = 3; // X
Mike Reed71f5a0b2018-10-25 16:12:39 -0400223 SkTextUtils::DrawText(canvas, &glyphID, sizeof(glyphID), x, y, *paint,
Mike Reed3a42ec02018-10-30 12:53:21 -0400224 SkTextUtils::kCenter_Align);
reed@google.com67981c52011-02-17 14:21:01 +0000225 canvas->restore();
226 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000227
caryclarkf597c422015-07-28 10:37:53 -0700228 void onOnceBeforeDraw() override {
Hal Canary53e5e7d2017-12-08 14:25:14 -0500229 fEmFace = MakeResourceAsTypeface("fonts/Em.ttf");
caryclarkf597c422015-07-28 10:37:53 -0700230 }
231
mtklein36352bf2015-03-25 18:17:31 -0700232 void onDraw(SkCanvas* canvas) override {
reed@google.com8ea69832013-06-21 15:11:40 +0000233 if (false) { test_stroke(canvas); return; }
234
reed@google.com67981c52011-02-17 14:21:01 +0000235 SkPaint paint;
236 paint.setAntiAlias(true);
caryclarkf597c422015-07-28 10:37:53 -0700237 paint.setTypeface(fEmFace);
Mike Reed97f3cc22018-12-03 09:45:17 -0500238 paint.setTextEncoding(kGlyphID_SkTextEncoding);
reed@google.com67981c52011-02-17 14:21:01 +0000239 paint.setStrokeWidth(SkIntToScalar(4));
240 paint.setTextSize(SkIntToScalar(40));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241
reed@google.com67981c52011-02-17 14:21:01 +0000242 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000249
reed@google.com67981c52011-02-17 14:21:01 +0000250 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000257
reed@google.com67981c52011-02-17 14:21:01 +0000258 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000265
reed@google.com67981c52011-02-17 14:21:01 +0000266 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000274
reed@google.com67981c52011-02-17 14:21:01 +0000275private:
reed@google.com8ea69832013-06-21 15:11:40 +0000276 typedef skiagm::GM INHERITED;
bungeman13b9c952016-05-12 10:09:30 -0700277 sk_sp<SkTypeface> fEmFace;
reed@google.com67981c52011-02-17 14:21:01 +0000278};
279
280//////////////////////////////////////////////////////////////////////////////
281
reed@google.com8ea69832013-06-21 15:11:40 +0000282DEF_GM( return new Poly2PolyGM; )