blob: 2a2745916d603e0d9e57c82d53bd674c611ec411 [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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkGraphics.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkTime.h"
18#include "include/core/SkTypeface.h"
19#include "include/effects/SkGradientShader.h"
20#include "include/utils/SkParsePath.h"
21#include "samplecode/Sample.h"
22#include "src/utils/SkUTF.h"
23#include "tools/timer/AnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkGeometry.h"
reed@android.coma9640282009-08-28 20:06:54 +000026
bungeman60e0fee2015-08-26 05:15:46 -070027#include <stdlib.h>
28
reed@android.coma9640282009-08-28 20:06:54 +000029// http://code.google.com/p/skia/issues/detail?id=32
30static void test_cubic() {
31 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000032 { 556.25000f, 523.03003f },
33 { 556.23999f, 522.96002f },
34 { 556.21997f, 522.89001f },
35 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000036 };
37 SkPoint dst[11];
38 dst[10].set(42, -42); // one past the end, that we don't clobber these
39 SkScalar tval[] = { 0.33333334f, 0.99999994f };
40
41 SkChopCubicAt(src, dst, tval, 2);
42
43#if 0
44 for (int i = 0; i < 11; i++) {
45 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
46 }
47#endif
48}
49
reed@android.comd0d0e652009-10-13 13:31:27 +000050static void test_cubic2() {
51 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
52 SkPath path;
53 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.comd0d0e652009-10-13 13:31:27 +000055 {
reed@android.comd0d0e652009-10-13 13:31:27 +000056 SkRect r = path.getBounds();
57 SkIRect ir;
58 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000059 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
60 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
61 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
62 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000063 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.comd0d0e652009-10-13 13:31:27 +000065 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000066 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000067
68 SkCanvas canvas(bitmap);
69 SkPaint paint;
70 paint.setAntiAlias(true);
71 canvas.drawPath(path, paint);
72}
73
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040074class PathView : public Sample {
reed339cdbf2015-02-05 22:02:37 -080075 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
reed339cdbf2015-02-05 22:02:37 -080077 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkPath fPath[6];
79 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000080 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
82 PathView() {
reed339cdbf2015-02-05 22:02:37 -080083 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000084 fOnce = false;
85 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@google.comc9fa63c2012-03-12 21:14:09 +000087 void init() {
88 if (fOnce) {
89 return;
90 }
91 fOnce = true;
92
reed@android.coma9640282009-08-28 20:06:54 +000093 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +000094 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 fDStroke = 1;
99 fStroke = 10;
100 fMinStroke = 10;
101 fMaxStroke = 180;
102
reed339cdbf2015-02-05 22:02:37 -0800103 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed339cdbf2015-02-05 22:02:37 -0800105 fPath[0].moveTo(40, 70);
106 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
107 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed339cdbf2015-02-05 22:02:37 -0800109 fPath[1].moveTo(40, 70);
110 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
111 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed339cdbf2015-02-05 22:02:37 -0800113 fPath[2].moveTo(V, V);
114 fPath[2].lineTo(50, V);
115 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed339cdbf2015-02-05 22:02:37 -0800117 fPath[3].moveTo(50, 50);
118 fPath[3].lineTo(50, V);
119 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed339cdbf2015-02-05 22:02:37 -0800121 fPath[4].moveTo(50, 50);
122 fPath[4].lineTo(50, V);
123 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed339cdbf2015-02-05 22:02:37 -0800125 fPath[5].moveTo(52, 50);
126 fPath[5].lineTo(50, V);
127 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com0faac1e2011-05-11 05:58:58 +0000129 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400133 bool onQuery(Sample::Event* evt) override {
134 if (Sample::TitleQ(*evt)) {
135 Sample::TitleR(evt, "Paths");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 return true;
137 }
138 return this->INHERITED::onQuery(evt);
139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@google.com0faac1e2011-05-11 05:58:58 +0000141 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 paint.setAntiAlias(true);
145 paint.setStyle(SkPaint::kStroke_Style);
146 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800147 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
reed@google.com0faac1e2011-05-11 05:58:58 +0000149 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
152 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 paint.setStrokeWidth(0);
154 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000155 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 paint.setColor(SK_ColorRED);
160 paint.setStrokeWidth(0);
161 canvas->drawPath(path, paint);
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
mtkleinf0599002015-07-13 06:18:39 -0700164 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000165 this->init();
reed339cdbf2015-02-05 22:02:37 -0800166 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167
168 static const SkPaint::Join gJoins[] = {
169 SkPaint::kBevel_Join,
170 SkPaint::kMiter_Join,
171 SkPaint::kRound_Join
172 };
173
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000174 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000176 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800178 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 }
180 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed339cdbf2015-02-05 22:02:37 -0800182 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 }
reed339cdbf2015-02-05 22:02:37 -0800184 }
mtkleinf0599002015-07-13 06:18:39 -0700185
Mike Kleincd5104e2019-03-20 11:55:08 -0500186 bool onAnimate(const AnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800187 SkScalar currSecs = timer.scaled(100);
188 SkScalar delta = currSecs - fPrevSecs;
189 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed339cdbf2015-02-05 22:02:37 -0800191 fStroke += fDStroke * delta;
192 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
193 fDStroke = -fDStroke;
194 }
195 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400198 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 fShowHairline = !fShowHairline;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000200 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400204 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205};
reeda7a8b102014-12-16 08:07:43 -0800206DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207
208//////////////////////////////////////////////////////////////////////////////
209
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500210#include "include/effects/SkCornerPathEffect.h"
211#include "include/utils/SkRandom.h"
reeda7a8b102014-12-16 08:07:43 -0800212
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400213class ArcToView : public Sample {
Mike Reed40e7e652017-07-22 22:12:59 -0400214 bool fDoFrame, fDoCorner, fDoConic;
215 SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800216public:
217 enum {
218 N = 4
219 };
220 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800221
reed8b575242014-12-17 01:47:32 -0800222 ArcToView()
Mike Reed40e7e652017-07-22 22:12:59 -0400223 : fDoFrame(false), fDoCorner(false), fDoConic(false)
reed8b575242014-12-17 01:47:32 -0800224 {
reeda7a8b102014-12-16 08:07:43 -0800225 SkRandom rand;
226 for (int i = 0; i < N; ++i) {
227 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
228 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
229 }
mtkleinf0599002015-07-13 06:18:39 -0700230
reed8b575242014-12-17 01:47:32 -0800231 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800232
233 fPtsPaint.setAntiAlias(true);
234 fPtsPaint.setStrokeWidth(15);
235 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
236
reeda7a8b102014-12-16 08:07:43 -0800237 fCornerPaint.setAntiAlias(true);
238 fCornerPaint.setStyle(SkPaint::kStroke_Style);
239 fCornerPaint.setStrokeWidth(13);
240 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700241 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800242
243 fSkeletonPaint.setAntiAlias(true);
244 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
245 fSkeletonPaint.setColor(SK_ColorRED);
246 }
247
reed8b575242014-12-17 01:47:32 -0800248 void toggle(bool& value) {
249 value = !value;
reed8b575242014-12-17 01:47:32 -0800250 }
251
reeda7a8b102014-12-16 08:07:43 -0800252protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400253 bool onQuery(Sample::Event* evt) override {
254 if (Sample::TitleQ(*evt)) {
255 Sample::TitleR(evt, "ArcTo");
reeda7a8b102014-12-16 08:07:43 -0800256 return true;
257 }
reed8b575242014-12-17 01:47:32 -0800258 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400259 if (Sample::CharQ(*evt, &uni)) {
reed8b575242014-12-17 01:47:32 -0800260 switch (uni) {
261 case '1': this->toggle(fDoFrame); return true;
Mike Reed40e7e652017-07-22 22:12:59 -0400262 case '2': this->toggle(fDoCorner); return true;
263 case '3': this->toggle(fDoConic); return true;
reed8b575242014-12-17 01:47:32 -0800264 default: break;
265 }
266 }
reeda7a8b102014-12-16 08:07:43 -0800267 return this->INHERITED::onQuery(evt);
268 }
mtkleinf0599002015-07-13 06:18:39 -0700269
reed8b575242014-12-17 01:47:32 -0800270 void makePath(SkPath* path) {
271 path->moveTo(fPts[0]);
272 for (int i = 1; i < N; ++i) {
273 path->lineTo(fPts[i]);
274 }
275 if (!fDoFrame) {
276 path->close();
277 }
278 }
reeda7a8b102014-12-16 08:07:43 -0800279
mtklein36352bf2015-03-25 18:17:31 -0700280 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800281 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
282
283 SkPath path;
reed8b575242014-12-17 01:47:32 -0800284 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800285
reed8b575242014-12-17 01:47:32 -0800286 if (fDoCorner) {
287 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800288 }
reeda7a8b102014-12-16 08:07:43 -0800289
reed8b575242014-12-17 01:47:32 -0800290 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800291 }
292
mtklein36352bf2015-03-25 18:17:31 -0700293 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800294 int32_t index;
295 if (click->fMeta.findS32("index", &index)) {
296 SkASSERT((unsigned)index < N);
297 fPts[index] = click->fCurr;
reeda7a8b102014-12-16 08:07:43 -0800298 return true;
299 }
300 return false;
301 }
302
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400303 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800304 const SkScalar tol = 4;
305 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
306 for (int i = 0; i < N; ++i) {
307 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
308 Click* click = new Click(this);
309 click->fMeta.setS32("index", i);
310 return click;
311 }
312 }
313 return this->INHERITED::onFindClickHandler(x, y, modi);
314 }
315
316private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400317 typedef Sample INHERITED;
reeda7a8b102014-12-16 08:07:43 -0800318};
319DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400320
321/////////////
322
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400323class FatStroke : public Sample {
Mike Reeda964a292016-11-02 22:09:25 -0400324 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
325 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500326 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400327 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
328public:
329 enum {
330 N = 4
331 };
332 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400333
Mike Reeda964a292016-11-02 22:09:25 -0400334 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
335 fJoinType(0), fCapType(0)
336 {
337 SkRandom rand;
338 for (int i = 0; i < N; ++i) {
339 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
340 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
341 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400342
Mike Reeda964a292016-11-02 22:09:25 -0400343 fPtsPaint.setAntiAlias(true);
344 fPtsPaint.setStrokeWidth(10);
345 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400346
Mike Reeda964a292016-11-02 22:09:25 -0400347 fHiddenPaint.setAntiAlias(true);
348 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
349 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400350
Mike Reeda964a292016-11-02 22:09:25 -0400351 fStrokePaint.setAntiAlias(true);
352 fStrokePaint.setStyle(SkPaint::kStroke_Style);
353 fStrokePaint.setStrokeWidth(50);
354 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400355
Mike Reeda964a292016-11-02 22:09:25 -0400356 fSkeletonPaint.setAntiAlias(true);
357 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
358 fSkeletonPaint.setColor(SK_ColorRED);
359 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400360
Mike Reeda964a292016-11-02 22:09:25 -0400361 void toggle(bool& value) {
362 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400363 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400364
Mike Reeda964a292016-11-02 22:09:25 -0400365 void toggle3(int& value) {
366 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400367 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400368
Mike Reeda964a292016-11-02 22:09:25 -0400369protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400370 bool onQuery(Sample::Event* evt) override {
371 if (Sample::TitleQ(*evt)) {
372 Sample::TitleR(evt, "FatStroke");
Mike Reeda964a292016-11-02 22:09:25 -0400373 return true;
374 }
375 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400376 if (Sample::CharQ(*evt, &uni)) {
Mike Reeda964a292016-11-02 22:09:25 -0400377 switch (uni) {
378 case '1': this->toggle(fShowSkeleton); return true;
379 case '2': this->toggle(fShowStroke); return true;
380 case '3': this->toggle(fShowHidden); return true;
381 case '4': this->toggle3(fJoinType); return true;
382 case '5': this->toggle3(fCapType); return true;
383 case '6': this->toggle(fClosed); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500384 case '-': fWidth -= 5; return true;
385 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400386 default: break;
387 }
388 }
389 return this->INHERITED::onQuery(evt);
390 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400391
Mike Reeda964a292016-11-02 22:09:25 -0400392 void makePath(SkPath* path) {
393 path->moveTo(fPts[0]);
394 for (int i = 1; i < N; ++i) {
395 path->lineTo(fPts[i]);
396 }
397 if (fClosed) {
398 path->close();
399 }
400 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400401
Mike Reeda964a292016-11-02 22:09:25 -0400402 void onDrawContent(SkCanvas* canvas) override {
403 canvas->drawColor(0xFFEEEEEE);
404
405 SkPath path;
406 this->makePath(&path);
407
Mike Reed1fba7022016-12-15 10:23:42 -0500408 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400409 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
410 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
411
412 if (fShowStroke) {
413 canvas->drawPath(path, fStrokePaint);
414 }
415 if (fShowHidden) {
416 SkPath hidden;
417 fStrokePaint.getFillPath(path, &hidden);
418 canvas->drawPath(hidden, fHiddenPaint);
419 }
420 if (fShowSkeleton) {
421 canvas->drawPath(path, fSkeletonPaint);
422 }
423 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
424 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400425
Mike Reeda964a292016-11-02 22:09:25 -0400426 bool onClick(Click* click) override {
427 int32_t index;
428 if (click->fMeta.findS32("index", &index)) {
429 SkASSERT((unsigned)index < N);
430 fPts[index] = click->fCurr;
Mike Reeda964a292016-11-02 22:09:25 -0400431 return true;
432 }
433 return false;
434 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400435
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400436 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reeda964a292016-11-02 22:09:25 -0400437 const SkScalar tol = 4;
438 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
439 for (int i = 0; i < N; ++i) {
440 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
441 Click* click = new Click(this);
442 click->fMeta.setS32("index", i);
443 return click;
444 }
445 }
446 return this->INHERITED::onFindClickHandler(x, y, modi);
447 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400448
Mike Reeda964a292016-11-02 22:09:25 -0400449private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400450 typedef Sample INHERITED;
Mike Reeda964a292016-11-02 22:09:25 -0400451};
452DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400453
454static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
455 // F = At^3 + Bt^2 + Ct + D
456 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
457 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
458 SkVector C = (pts[1] - pts[0]) * 3.0f;
459 SkVector DA = pts[3] - pts[0];
460
461 // F' = 3At^2 + 2Bt + C
462 SkScalar a = 3 * A.cross(DA);
463 SkScalar b = 2 * B.cross(DA);
464 SkScalar c = C.cross(DA);
465
466 int n = SkFindUnitQuadRoots(a, b, c, t);
467 SkString str;
468 for (int i = 0; i < n; ++i) {
469 str.appendf(" %g", t[i]);
470 }
471 SkDebugf("roots %s\n", str.c_str());
472 return n;
473}
474
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400475class CubicCurve : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400476public:
477 enum {
478 N = 4
479 };
480 SkPoint fPts[N];
481
482 CubicCurve() {
483 SkRandom rand;
484 for (int i = 0; i < N; ++i) {
485 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
486 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
487 }
488 }
489
490protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400491 bool onQuery(Sample::Event* evt) override {
492 if (Sample::TitleQ(*evt)) {
493 Sample::TitleR(evt, "CubicCurve");
Mike Reed66fa4562018-04-06 19:56:50 -0400494 return true;
495 }
496 return this->INHERITED::onQuery(evt);
497 }
498
499 void onDrawContent(SkCanvas* canvas) override {
500 SkPaint paint;
501 paint.setAntiAlias(true);
502
503 {
504 SkPath path;
505 path.moveTo(fPts[0]);
506 path.cubicTo(fPts[1], fPts[2], fPts[3]);
507 paint.setStyle(SkPaint::kStroke_Style);
508 canvas->drawPath(path, paint);
509 }
510
511 {
512 paint.setColor(SK_ColorRED);
513 SkScalar t[2];
514 int n = compute_parallel_to_base(fPts, t);
515 SkPoint loc;
516 SkVector tan;
517 for (int i = 0; i < n; ++i) {
518 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
519 tan.setLength(30);
520 canvas->drawLine(loc - tan, loc + tan, paint);
521 }
522 paint.setStrokeWidth(0.5f);
523 canvas->drawLine(fPts[0], fPts[3], paint);
524
525 paint.setColor(SK_ColorBLUE);
526 paint.setStrokeWidth(6);
527 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
528 canvas->drawPoint(loc, paint);
529
530 paint.setColor(0xFF008800);
531 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
532 canvas->drawPoint(loc, paint);
533 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
534 canvas->drawPoint(loc, paint);
535
536 // n = SkFindCubicInflections(fPts, t);
537 // printf("inflections %d %g %g\n", n, t[0], t[1]);
538 }
539
540 {
541 paint.setStyle(SkPaint::kFill_Style);
542 paint.setColor(SK_ColorRED);
543 for (SkPoint p : fPts) {
544 canvas->drawCircle(p.fX, p.fY, 8, paint);
545 }
546 }
547 }
548
549 bool onClick(Click* click) override {
550 int32_t index;
551 if (click->fMeta.findS32("index", &index)) {
552 SkASSERT((unsigned)index < N);
553 fPts[index] = click->fCurr;
554 return true;
555 }
556 return false;
557 }
558
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400559 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400560 const SkScalar tol = 8;
561 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
562 for (int i = 0; i < N; ++i) {
563 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
564 Click* click = new Click(this);
565 click->fMeta.setS32("index", i);
566 return click;
567 }
568 }
569 return this->INHERITED::onFindClickHandler(x, y, modi);
570 }
571
572private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400573 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400574};
575DEF_SAMPLE( return new CubicCurve; )
576
577static SkPoint lerp(SkPoint a, SkPoint b, float t) {
578 return a * (1 - t) + b * t;
579}
580
Mike Reed83dc4732018-04-16 17:07:07 -0400581static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
582 // deviation = F' x (d - a) == 0, solve for t(s)
583 // F = At^3 + Bt^2 + Ct + D
584 // F' = 3At^2 + 2Bt + C
585 // Z = d - a
586 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
587 //
588 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
589 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
590 SkVector C = (src[1] - src[0]) * 3;
591 SkVector Z = src[3] - src[0];
592 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
593 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
594}
595
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400596class CubicCurve2 : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400597public:
598 enum {
599 N = 7
600 };
601 SkPoint fPts[N];
602 SkPoint* fQuad = fPts + 4;
603 SkScalar fT = 0.5f;
604 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400605 bool fShowFlatness = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400606 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400607
608 CubicCurve2() {
609 fPts[0] = { 90, 300 };
610 fPts[1] = { 30, 60 };
611 fPts[2] = { 250, 30 };
612 fPts[3] = { 350, 200 };
613
614 fQuad[0] = fPts[0] + SkVector{ 300, 0};
615 fQuad[1] = fPts[1] + SkVector{ 300, 0};
616 fQuad[2] = fPts[2] + SkVector{ 300, 0};
617 }
618
619protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400620 bool onQuery(Sample::Event* evt) override {
621 if (Sample::TitleQ(*evt)) {
622 Sample::TitleR(evt, "CubicCurve2");
Mike Reed66fa4562018-04-06 19:56:50 -0400623 return true;
624 }
625 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400626 if (Sample::CharQ(*evt, &uni)) {
Mike Reed66fa4562018-04-06 19:56:50 -0400627 switch (uni) {
628 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400629 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400630 case '-': fT -= 1.0f / 32; break;
631 case '=': fT += 1.0f / 32; break;
632 default: goto DONE;
633 }
634 fT = std::min(1.0f, std::max(0.0f, fT));
635 return true;
636 }
637 DONE:
638 return this->INHERITED::onQuery(evt);
639 }
640
641 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
642 SkPaint paint(p);
643 SkPoint storage[3 + 2 + 1];
644 SkPoint* tmp = storage;
645 const SkPoint* prev = pts;
646 int n = count;
647 for (int n = count; n > 0; --n) {
648 for (int i = 0; i < n; ++i) {
649 canvas->drawLine(prev[i], prev[i+1], paint);
650 tmp[i] = lerp(prev[i], prev[i+1], fT);
651 }
652 prev = tmp;
653 tmp += n;
654 }
655
656 paint.setColor(SK_ColorBLUE);
657 paint.setStyle(SkPaint::kFill_Style);
658 n = tmp - storage;
659 for (int i = 0; i < n; ++i) {
660 canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
661 }
662 }
Hal Canaryd4998172018-07-11 11:31:34 -0400663
Mike Reed48bf6ab2018-04-15 20:04:45 -0400664 void showFlattness(SkCanvas* canvas) {
665 SkPaint paint;
666 paint.setStyle(SkPaint::kStroke_Style);
667 paint.setAntiAlias(true);
668
Mike Reeddccfa352018-04-17 17:10:51 -0400669 SkPaint paint2(paint);
670 paint2.setColor(0xFF008800);
671
Mike Reed48bf6ab2018-04-15 20:04:45 -0400672 paint.setColor(0xFF888888);
673 canvas->drawLine(fPts[0], fPts[3], paint);
674 canvas->drawLine(fQuad[0], fQuad[2], paint);
675
676 paint.setColor(0xFF0000FF);
677 SkPoint pts[2];
678 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
679 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
680 canvas->drawLine(pts[0], pts[1], paint);
681
Mike Reed83dc4732018-04-16 17:07:07 -0400682 // cubic
683
684 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
685 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400686 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400687
688 SkPoint anchor;
689 SkScalar ts[2];
690 int n = find_max_deviation_cubic(fPts, ts);
691 if (n > 0) {
692 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400693 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400694 canvas->drawLine(anchor, anchor + v0, paint);
695 if (n == 2) {
696 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400697 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400698 }
699 canvas->drawLine(anchor, anchor + v1, paint);
700 }
701 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400702 }
Mike Reed66fa4562018-04-06 19:56:50 -0400703
704 void onDrawContent(SkCanvas* canvas) override {
705 SkPaint paint;
706 paint.setAntiAlias(true);
707
708 {
709 paint.setStyle(SkPaint::kStroke_Style);
710 SkPath path;
711 path.moveTo(fPts[0]);
712 path.cubicTo(fPts[1], fPts[2], fPts[3]);
713 path.moveTo(fQuad[0]);
714 path.quadTo(fQuad[1], fQuad[2]);
715 canvas->drawPath(path, paint);
716 }
717
718 if (fShowSub) {
719 paint.setColor(SK_ColorRED);
720 paint.setStrokeWidth(1.7f);
721 this->showFrame(canvas, fPts, 3, paint);
722 this->showFrame(canvas, fQuad, 2, paint);
723
Mike Reed66fa4562018-04-06 19:56:50 -0400724 paint.setColor(SK_ColorBLACK);
725 paint.setStyle(SkPaint::kFill_Style);
Hal Canary4484b8f2019-01-08 14:00:08 -0500726 SkFont font(nullptr, 20);
727 canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
Mike Reed66fa4562018-04-06 19:56:50 -0400728 }
729
Mike Reed48bf6ab2018-04-15 20:04:45 -0400730 if (fShowFlatness) {
731 this->showFlattness(canvas);
732 }
733
Mike Reed66fa4562018-04-06 19:56:50 -0400734 paint.setStyle(SkPaint::kFill_Style);
735 paint.setColor(SK_ColorRED);
736 for (SkPoint p : fPts) {
737 canvas->drawCircle(p.fX, p.fY, 7, paint);
738 }
Mike Reedfb65db12018-04-19 19:18:35 -0400739
740 {
741 SkScalar ts[2];
742 int n = SkFindCubicInflections(fPts, ts);
743 for (int i = 0; i < n; ++i) {
744 SkPoint p;
745 SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
746 canvas->drawCircle(p.fX, p.fY, 3, paint);
747 }
748 }
749
Mike Reed66fa4562018-04-06 19:56:50 -0400750 }
751
752 bool onClick(Click* click) override {
753 int32_t index;
754 if (click->fMeta.findS32("index", &index)) {
755 SkASSERT((unsigned)index < N);
756 fPts[index] = click->fCurr;
757 return true;
758 }
759 return false;
760 }
761
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400762 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400763 const SkScalar tol = 8;
764 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
765 for (int i = 0; i < N; ++i) {
766 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
767 Click* click = new Click(this);
768 click->fMeta.setS32("index", i);
769 return click;
770 }
771 }
772 return this->INHERITED::onFindClickHandler(x, y, modi);
773 }
774
775private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400776 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400777};
778DEF_SAMPLE( return new CubicCurve2; )
779