blob: e09db71a6abeb4fae8c24741a759c6fe997931e1 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed339cdbf2015-02-05 22:02:37 -08009#include "SkAnimTimer.h"
Mike Reed75ae4212018-01-23 11:24:08 -050010#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040017#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkColorPriv.h"
19#include "SkColorFilter.h"
reed@android.comd0d0e652009-10-13 13:31:27 +000020#include "SkParsePath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkTime.h"
22#include "SkTypeface.h"
23
reed@android.coma9640282009-08-28 20:06:54 +000024#include "SkGeometry.h"
25
bungeman60e0fee2015-08-26 05:15:46 -070026#include <stdlib.h>
27
reed@android.coma9640282009-08-28 20:06:54 +000028// http://code.google.com/p/skia/issues/detail?id=32
29static void test_cubic() {
30 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000031 { 556.25000f, 523.03003f },
32 { 556.23999f, 522.96002f },
33 { 556.21997f, 522.89001f },
34 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000035 };
36 SkPoint dst[11];
37 dst[10].set(42, -42); // one past the end, that we don't clobber these
38 SkScalar tval[] = { 0.33333334f, 0.99999994f };
39
40 SkChopCubicAt(src, dst, tval, 2);
41
42#if 0
43 for (int i = 0; i < 11; i++) {
44 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
45 }
46#endif
47}
48
reed@android.comd0d0e652009-10-13 13:31:27 +000049static void test_cubic2() {
50 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
51 SkPath path;
52 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.comd0d0e652009-10-13 13:31:27 +000054 {
reed@android.comd0d0e652009-10-13 13:31:27 +000055 SkRect r = path.getBounds();
56 SkIRect ir;
57 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000058 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
59 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
60 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
61 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000062 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@android.comd0d0e652009-10-13 13:31:27 +000064 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000065 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000066
67 SkCanvas canvas(bitmap);
68 SkPaint paint;
69 paint.setAntiAlias(true);
70 canvas.drawPath(path, paint);
71}
72
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040073class PathView : public Sample {
reed339cdbf2015-02-05 22:02:37 -080074 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000075public:
reed339cdbf2015-02-05 22:02:37 -080076 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 SkPath fPath[6];
78 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000079 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
81 PathView() {
reed339cdbf2015-02-05 22:02:37 -080082 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000083 fOnce = false;
84 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@google.comc9fa63c2012-03-12 21:14:09 +000086 void init() {
87 if (fOnce) {
88 return;
89 }
90 fOnce = true;
91
reed@android.coma9640282009-08-28 20:06:54 +000092 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +000093 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 fDStroke = 1;
98 fStroke = 10;
99 fMinStroke = 10;
100 fMaxStroke = 180;
101
reed339cdbf2015-02-05 22:02:37 -0800102 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
reed339cdbf2015-02-05 22:02:37 -0800104 fPath[0].moveTo(40, 70);
105 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
106 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed339cdbf2015-02-05 22:02:37 -0800108 fPath[1].moveTo(40, 70);
109 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
110 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed339cdbf2015-02-05 22:02:37 -0800112 fPath[2].moveTo(V, V);
113 fPath[2].lineTo(50, V);
114 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed339cdbf2015-02-05 22:02:37 -0800116 fPath[3].moveTo(50, 50);
117 fPath[3].lineTo(50, V);
118 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed339cdbf2015-02-05 22:02:37 -0800120 fPath[4].moveTo(50, 50);
121 fPath[4].lineTo(50, V);
122 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed339cdbf2015-02-05 22:02:37 -0800124 fPath[5].moveTo(52, 50);
125 fPath[5].lineTo(50, V);
126 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@google.com0faac1e2011-05-11 05:58:58 +0000128 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400132 bool onQuery(Sample::Event* evt) override {
133 if (Sample::TitleQ(*evt)) {
134 Sample::TitleR(evt, "Paths");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 return true;
136 }
137 return this->INHERITED::onQuery(evt);
138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@google.com0faac1e2011-05-11 05:58:58 +0000140 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 paint.setAntiAlias(true);
144 paint.setStyle(SkPaint::kStroke_Style);
145 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800146 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
reed@google.com0faac1e2011-05-11 05:58:58 +0000148 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
151 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 paint.setStrokeWidth(0);
153 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000154 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000156 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 paint.setColor(SK_ColorRED);
159 paint.setStrokeWidth(0);
160 canvas->drawPath(path, paint);
161 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
mtkleinf0599002015-07-13 06:18:39 -0700163 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000164 this->init();
reed339cdbf2015-02-05 22:02:37 -0800165 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166
167 static const SkPaint::Join gJoins[] = {
168 SkPaint::kBevel_Join,
169 SkPaint::kMiter_Join,
170 SkPaint::kRound_Join
171 };
172
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000173 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000175 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800177 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
179 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed339cdbf2015-02-05 22:02:37 -0800181 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 }
reed339cdbf2015-02-05 22:02:37 -0800183 }
mtkleinf0599002015-07-13 06:18:39 -0700184
mtklein36352bf2015-03-25 18:17:31 -0700185 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800186 SkScalar currSecs = timer.scaled(100);
187 SkScalar delta = currSecs - fPrevSecs;
188 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000189
reed339cdbf2015-02-05 22:02:37 -0800190 fStroke += fDStroke * delta;
191 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
192 fDStroke = -fDStroke;
193 }
194 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400197 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 fShowHairline = !fShowHairline;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000199 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400203 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204};
reeda7a8b102014-12-16 08:07:43 -0800205DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206
207//////////////////////////////////////////////////////////////////////////////
208
reeda7a8b102014-12-16 08:07:43 -0800209#include "SkCornerPathEffect.h"
210#include "SkRandom.h"
211
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400212class ArcToView : public Sample {
Mike Reed40e7e652017-07-22 22:12:59 -0400213 bool fDoFrame, fDoCorner, fDoConic;
214 SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800215public:
216 enum {
217 N = 4
218 };
219 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800220
reed8b575242014-12-17 01:47:32 -0800221 ArcToView()
Mike Reed40e7e652017-07-22 22:12:59 -0400222 : fDoFrame(false), fDoCorner(false), fDoConic(false)
reed8b575242014-12-17 01:47:32 -0800223 {
reeda7a8b102014-12-16 08:07:43 -0800224 SkRandom rand;
225 for (int i = 0; i < N; ++i) {
226 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
227 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
228 }
mtkleinf0599002015-07-13 06:18:39 -0700229
reed8b575242014-12-17 01:47:32 -0800230 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800231
232 fPtsPaint.setAntiAlias(true);
233 fPtsPaint.setStrokeWidth(15);
234 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
235
reeda7a8b102014-12-16 08:07:43 -0800236 fCornerPaint.setAntiAlias(true);
237 fCornerPaint.setStyle(SkPaint::kStroke_Style);
238 fCornerPaint.setStrokeWidth(13);
239 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700240 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800241
242 fSkeletonPaint.setAntiAlias(true);
243 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
244 fSkeletonPaint.setColor(SK_ColorRED);
245 }
246
reed8b575242014-12-17 01:47:32 -0800247 void toggle(bool& value) {
248 value = !value;
reed8b575242014-12-17 01:47:32 -0800249 }
250
reeda7a8b102014-12-16 08:07:43 -0800251protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400252 bool onQuery(Sample::Event* evt) override {
253 if (Sample::TitleQ(*evt)) {
254 Sample::TitleR(evt, "ArcTo");
reeda7a8b102014-12-16 08:07:43 -0800255 return true;
256 }
reed8b575242014-12-17 01:47:32 -0800257 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400258 if (Sample::CharQ(*evt, &uni)) {
reed8b575242014-12-17 01:47:32 -0800259 switch (uni) {
260 case '1': this->toggle(fDoFrame); return true;
Mike Reed40e7e652017-07-22 22:12:59 -0400261 case '2': this->toggle(fDoCorner); return true;
262 case '3': this->toggle(fDoConic); return true;
reed8b575242014-12-17 01:47:32 -0800263 default: break;
264 }
265 }
reeda7a8b102014-12-16 08:07:43 -0800266 return this->INHERITED::onQuery(evt);
267 }
mtkleinf0599002015-07-13 06:18:39 -0700268
reed8b575242014-12-17 01:47:32 -0800269 void makePath(SkPath* path) {
270 path->moveTo(fPts[0]);
271 for (int i = 1; i < N; ++i) {
272 path->lineTo(fPts[i]);
273 }
274 if (!fDoFrame) {
275 path->close();
276 }
277 }
reeda7a8b102014-12-16 08:07:43 -0800278
mtklein36352bf2015-03-25 18:17:31 -0700279 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800280 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
281
282 SkPath path;
reed8b575242014-12-17 01:47:32 -0800283 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800284
reed8b575242014-12-17 01:47:32 -0800285 if (fDoCorner) {
286 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800287 }
reeda7a8b102014-12-16 08:07:43 -0800288
reed8b575242014-12-17 01:47:32 -0800289 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800290 }
291
mtklein36352bf2015-03-25 18:17:31 -0700292 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800293 int32_t index;
294 if (click->fMeta.findS32("index", &index)) {
295 SkASSERT((unsigned)index < N);
296 fPts[index] = click->fCurr;
reeda7a8b102014-12-16 08:07:43 -0800297 return true;
298 }
299 return false;
300 }
301
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400302 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800303 const SkScalar tol = 4;
304 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
305 for (int i = 0; i < N; ++i) {
306 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
307 Click* click = new Click(this);
308 click->fMeta.setS32("index", i);
309 return click;
310 }
311 }
312 return this->INHERITED::onFindClickHandler(x, y, modi);
313 }
314
315private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400316 typedef Sample INHERITED;
reeda7a8b102014-12-16 08:07:43 -0800317};
318DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400319
320/////////////
321
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400322class FatStroke : public Sample {
Mike Reeda964a292016-11-02 22:09:25 -0400323 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
324 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500325 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400326 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
327public:
328 enum {
329 N = 4
330 };
331 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400332
Mike Reeda964a292016-11-02 22:09:25 -0400333 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
334 fJoinType(0), fCapType(0)
335 {
336 SkRandom rand;
337 for (int i = 0; i < N; ++i) {
338 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
339 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
340 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400341
Mike Reeda964a292016-11-02 22:09:25 -0400342 fPtsPaint.setAntiAlias(true);
343 fPtsPaint.setStrokeWidth(10);
344 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400345
Mike Reeda964a292016-11-02 22:09:25 -0400346 fHiddenPaint.setAntiAlias(true);
347 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
348 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400349
Mike Reeda964a292016-11-02 22:09:25 -0400350 fStrokePaint.setAntiAlias(true);
351 fStrokePaint.setStyle(SkPaint::kStroke_Style);
352 fStrokePaint.setStrokeWidth(50);
353 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400354
Mike Reeda964a292016-11-02 22:09:25 -0400355 fSkeletonPaint.setAntiAlias(true);
356 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
357 fSkeletonPaint.setColor(SK_ColorRED);
358 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400359
Mike Reeda964a292016-11-02 22:09:25 -0400360 void toggle(bool& value) {
361 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400362 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400363
Mike Reeda964a292016-11-02 22:09:25 -0400364 void toggle3(int& value) {
365 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400366 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400367
Mike Reeda964a292016-11-02 22:09:25 -0400368protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400369 bool onQuery(Sample::Event* evt) override {
370 if (Sample::TitleQ(*evt)) {
371 Sample::TitleR(evt, "FatStroke");
Mike Reeda964a292016-11-02 22:09:25 -0400372 return true;
373 }
374 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400375 if (Sample::CharQ(*evt, &uni)) {
Mike Reeda964a292016-11-02 22:09:25 -0400376 switch (uni) {
377 case '1': this->toggle(fShowSkeleton); return true;
378 case '2': this->toggle(fShowStroke); return true;
379 case '3': this->toggle(fShowHidden); return true;
380 case '4': this->toggle3(fJoinType); return true;
381 case '5': this->toggle3(fCapType); return true;
382 case '6': this->toggle(fClosed); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500383 case '-': fWidth -= 5; return true;
384 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400385 default: break;
386 }
387 }
388 return this->INHERITED::onQuery(evt);
389 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400390
Mike Reeda964a292016-11-02 22:09:25 -0400391 void makePath(SkPath* path) {
392 path->moveTo(fPts[0]);
393 for (int i = 1; i < N; ++i) {
394 path->lineTo(fPts[i]);
395 }
396 if (fClosed) {
397 path->close();
398 }
399 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400400
Mike Reeda964a292016-11-02 22:09:25 -0400401 void onDrawContent(SkCanvas* canvas) override {
402 canvas->drawColor(0xFFEEEEEE);
403
404 SkPath path;
405 this->makePath(&path);
406
Mike Reed1fba7022016-12-15 10:23:42 -0500407 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400408 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
409 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
410
411 if (fShowStroke) {
412 canvas->drawPath(path, fStrokePaint);
413 }
414 if (fShowHidden) {
415 SkPath hidden;
416 fStrokePaint.getFillPath(path, &hidden);
417 canvas->drawPath(hidden, fHiddenPaint);
418 }
419 if (fShowSkeleton) {
420 canvas->drawPath(path, fSkeletonPaint);
421 }
422 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
423 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400424
Mike Reeda964a292016-11-02 22:09:25 -0400425 bool onClick(Click* click) override {
426 int32_t index;
427 if (click->fMeta.findS32("index", &index)) {
428 SkASSERT((unsigned)index < N);
429 fPts[index] = click->fCurr;
Mike Reeda964a292016-11-02 22:09:25 -0400430 return true;
431 }
432 return false;
433 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400434
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400435 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reeda964a292016-11-02 22:09:25 -0400436 const SkScalar tol = 4;
437 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
438 for (int i = 0; i < N; ++i) {
439 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
440 Click* click = new Click(this);
441 click->fMeta.setS32("index", i);
442 return click;
443 }
444 }
445 return this->INHERITED::onFindClickHandler(x, y, modi);
446 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400447
Mike Reeda964a292016-11-02 22:09:25 -0400448private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400449 typedef Sample INHERITED;
Mike Reeda964a292016-11-02 22:09:25 -0400450};
451DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400452
453static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
454 // F = At^3 + Bt^2 + Ct + D
455 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
456 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
457 SkVector C = (pts[1] - pts[0]) * 3.0f;
458 SkVector DA = pts[3] - pts[0];
459
460 // F' = 3At^2 + 2Bt + C
461 SkScalar a = 3 * A.cross(DA);
462 SkScalar b = 2 * B.cross(DA);
463 SkScalar c = C.cross(DA);
464
465 int n = SkFindUnitQuadRoots(a, b, c, t);
466 SkString str;
467 for (int i = 0; i < n; ++i) {
468 str.appendf(" %g", t[i]);
469 }
470 SkDebugf("roots %s\n", str.c_str());
471 return n;
472}
473
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400474class CubicCurve : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400475public:
476 enum {
477 N = 4
478 };
479 SkPoint fPts[N];
480
481 CubicCurve() {
482 SkRandom rand;
483 for (int i = 0; i < N; ++i) {
484 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
485 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
486 }
487 }
488
489protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400490 bool onQuery(Sample::Event* evt) override {
491 if (Sample::TitleQ(*evt)) {
492 Sample::TitleR(evt, "CubicCurve");
Mike Reed66fa4562018-04-06 19:56:50 -0400493 return true;
494 }
495 return this->INHERITED::onQuery(evt);
496 }
497
498 void onDrawContent(SkCanvas* canvas) override {
499 SkPaint paint;
500 paint.setAntiAlias(true);
501
502 {
503 SkPath path;
504 path.moveTo(fPts[0]);
505 path.cubicTo(fPts[1], fPts[2], fPts[3]);
506 paint.setStyle(SkPaint::kStroke_Style);
507 canvas->drawPath(path, paint);
508 }
509
510 {
511 paint.setColor(SK_ColorRED);
512 SkScalar t[2];
513 int n = compute_parallel_to_base(fPts, t);
514 SkPoint loc;
515 SkVector tan;
516 for (int i = 0; i < n; ++i) {
517 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
518 tan.setLength(30);
519 canvas->drawLine(loc - tan, loc + tan, paint);
520 }
521 paint.setStrokeWidth(0.5f);
522 canvas->drawLine(fPts[0], fPts[3], paint);
523
524 paint.setColor(SK_ColorBLUE);
525 paint.setStrokeWidth(6);
526 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
527 canvas->drawPoint(loc, paint);
528
529 paint.setColor(0xFF008800);
530 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
531 canvas->drawPoint(loc, paint);
532 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
533 canvas->drawPoint(loc, paint);
534
535 // n = SkFindCubicInflections(fPts, t);
536 // printf("inflections %d %g %g\n", n, t[0], t[1]);
537 }
538
539 {
540 paint.setStyle(SkPaint::kFill_Style);
541 paint.setColor(SK_ColorRED);
542 for (SkPoint p : fPts) {
543 canvas->drawCircle(p.fX, p.fY, 8, paint);
544 }
545 }
546 }
547
548 bool onClick(Click* click) override {
549 int32_t index;
550 if (click->fMeta.findS32("index", &index)) {
551 SkASSERT((unsigned)index < N);
552 fPts[index] = click->fCurr;
553 return true;
554 }
555 return false;
556 }
557
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400558 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400559 const SkScalar tol = 8;
560 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
561 for (int i = 0; i < N; ++i) {
562 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
563 Click* click = new Click(this);
564 click->fMeta.setS32("index", i);
565 return click;
566 }
567 }
568 return this->INHERITED::onFindClickHandler(x, y, modi);
569 }
570
571private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400572 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400573};
574DEF_SAMPLE( return new CubicCurve; )
575
576static SkPoint lerp(SkPoint a, SkPoint b, float t) {
577 return a * (1 - t) + b * t;
578}
579
Mike Reed83dc4732018-04-16 17:07:07 -0400580static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
581 // deviation = F' x (d - a) == 0, solve for t(s)
582 // F = At^3 + Bt^2 + Ct + D
583 // F' = 3At^2 + 2Bt + C
584 // Z = d - a
585 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
586 //
587 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
588 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
589 SkVector C = (src[1] - src[0]) * 3;
590 SkVector Z = src[3] - src[0];
591 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
592 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
593}
594
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400595class CubicCurve2 : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400596public:
597 enum {
598 N = 7
599 };
600 SkPoint fPts[N];
601 SkPoint* fQuad = fPts + 4;
602 SkScalar fT = 0.5f;
603 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400604 bool fShowFlatness = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400605 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400606
607 CubicCurve2() {
608 fPts[0] = { 90, 300 };
609 fPts[1] = { 30, 60 };
610 fPts[2] = { 250, 30 };
611 fPts[3] = { 350, 200 };
612
613 fQuad[0] = fPts[0] + SkVector{ 300, 0};
614 fQuad[1] = fPts[1] + SkVector{ 300, 0};
615 fQuad[2] = fPts[2] + SkVector{ 300, 0};
616 }
617
618protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400619 bool onQuery(Sample::Event* evt) override {
620 if (Sample::TitleQ(*evt)) {
621 Sample::TitleR(evt, "CubicCurve2");
Mike Reed66fa4562018-04-06 19:56:50 -0400622 return true;
623 }
624 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400625 if (Sample::CharQ(*evt, &uni)) {
Mike Reed66fa4562018-04-06 19:56:50 -0400626 switch (uni) {
627 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400628 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400629 case '-': fT -= 1.0f / 32; break;
630 case '=': fT += 1.0f / 32; break;
631 default: goto DONE;
632 }
633 fT = std::min(1.0f, std::max(0.0f, fT));
634 return true;
635 }
636 DONE:
637 return this->INHERITED::onQuery(evt);
638 }
639
640 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
641 SkPaint paint(p);
642 SkPoint storage[3 + 2 + 1];
643 SkPoint* tmp = storage;
644 const SkPoint* prev = pts;
645 int n = count;
646 for (int n = count; n > 0; --n) {
647 for (int i = 0; i < n; ++i) {
648 canvas->drawLine(prev[i], prev[i+1], paint);
649 tmp[i] = lerp(prev[i], prev[i+1], fT);
650 }
651 prev = tmp;
652 tmp += n;
653 }
654
655 paint.setColor(SK_ColorBLUE);
656 paint.setStyle(SkPaint::kFill_Style);
657 n = tmp - storage;
658 for (int i = 0; i < n; ++i) {
659 canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
660 }
661 }
Hal Canaryd4998172018-07-11 11:31:34 -0400662
Mike Reed48bf6ab2018-04-15 20:04:45 -0400663 void showFlattness(SkCanvas* canvas) {
664 SkPaint paint;
665 paint.setStyle(SkPaint::kStroke_Style);
666 paint.setAntiAlias(true);
667
Mike Reeddccfa352018-04-17 17:10:51 -0400668 SkPaint paint2(paint);
669 paint2.setColor(0xFF008800);
670
Mike Reed48bf6ab2018-04-15 20:04:45 -0400671 paint.setColor(0xFF888888);
672 canvas->drawLine(fPts[0], fPts[3], paint);
673 canvas->drawLine(fQuad[0], fQuad[2], paint);
674
675 paint.setColor(0xFF0000FF);
676 SkPoint pts[2];
677 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
678 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
679 canvas->drawLine(pts[0], pts[1], paint);
680
Mike Reed83dc4732018-04-16 17:07:07 -0400681 // cubic
682
683 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
684 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400685 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400686
687 SkPoint anchor;
688 SkScalar ts[2];
689 int n = find_max_deviation_cubic(fPts, ts);
690 if (n > 0) {
691 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400692 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400693 canvas->drawLine(anchor, anchor + v0, paint);
694 if (n == 2) {
695 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400696 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400697 }
698 canvas->drawLine(anchor, anchor + v1, paint);
699 }
700 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400701 }
Mike Reed66fa4562018-04-06 19:56:50 -0400702
703 void onDrawContent(SkCanvas* canvas) override {
704 SkPaint paint;
705 paint.setAntiAlias(true);
706
707 {
708 paint.setStyle(SkPaint::kStroke_Style);
709 SkPath path;
710 path.moveTo(fPts[0]);
711 path.cubicTo(fPts[1], fPts[2], fPts[3]);
712 path.moveTo(fQuad[0]);
713 path.quadTo(fQuad[1], fQuad[2]);
714 canvas->drawPath(path, paint);
715 }
716
717 if (fShowSub) {
718 paint.setColor(SK_ColorRED);
719 paint.setStrokeWidth(1.7f);
720 this->showFrame(canvas, fPts, 3, paint);
721 this->showFrame(canvas, fQuad, 2, paint);
722
723 SkString str;
724 str.printf("t = %g", fT);
725 paint.setColor(SK_ColorBLACK);
726 paint.setStyle(SkPaint::kFill_Style);
727 paint.setTextSize(20);
728 canvas->drawText(str.c_str(), str.size(), 20, 20, paint);
729 }
730
Mike Reed48bf6ab2018-04-15 20:04:45 -0400731 if (fShowFlatness) {
732 this->showFlattness(canvas);
733 }
734
Mike Reed66fa4562018-04-06 19:56:50 -0400735 paint.setStyle(SkPaint::kFill_Style);
736 paint.setColor(SK_ColorRED);
737 for (SkPoint p : fPts) {
738 canvas->drawCircle(p.fX, p.fY, 7, paint);
739 }
Mike Reedfb65db12018-04-19 19:18:35 -0400740
741 {
742 SkScalar ts[2];
743 int n = SkFindCubicInflections(fPts, ts);
744 for (int i = 0; i < n; ++i) {
745 SkPoint p;
746 SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
747 canvas->drawCircle(p.fX, p.fY, 3, paint);
748 }
749 }
750
Mike Reed66fa4562018-04-06 19:56:50 -0400751 }
752
753 bool onClick(Click* click) override {
754 int32_t index;
755 if (click->fMeta.findS32("index", &index)) {
756 SkASSERT((unsigned)index < N);
757 fPts[index] = click->fCurr;
758 return true;
759 }
760 return false;
761 }
762
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400763 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400764 const SkScalar tol = 8;
765 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
766 for (int i = 0; i < N; ++i) {
767 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
768 Click* click = new Click(this);
769 click->fMeta.setS32("index", i);
770 return click;
771 }
772 }
773 return this->INHERITED::onFindClickHandler(x, y, modi);
774 }
775
776private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400777 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400778};
779DEF_SAMPLE( return new CubicCurve2; )
780