blob: 478f6a0c630dffc0995d27256fdadb467f191e56 [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"
Hal Canary41248072019-07-11 16:32:53 -040023#include "tools/timer/TimeUtils.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:
Hal Canary8a027312019-07-03 10:55:44 -0400133 SkString name() override { return SkString("Paths"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@google.com0faac1e2011-05-11 05:58:58 +0000135 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 paint.setAntiAlias(true);
139 paint.setStyle(SkPaint::kStroke_Style);
140 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800141 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
reed@google.com0faac1e2011-05-11 05:58:58 +0000143 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
146 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 paint.setStrokeWidth(0);
148 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000149 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000151 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 paint.setColor(SK_ColorRED);
154 paint.setStrokeWidth(0);
155 canvas->drawPath(path, paint);
156 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
mtkleinf0599002015-07-13 06:18:39 -0700158 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000159 this->init();
reed339cdbf2015-02-05 22:02:37 -0800160 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
162 static const SkPaint::Join gJoins[] = {
163 SkPaint::kBevel_Join,
164 SkPaint::kMiter_Join,
165 SkPaint::kRound_Join
166 };
167
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000168 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000170 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800172 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 }
174 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
reed339cdbf2015-02-05 22:02:37 -0800176 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 }
reed339cdbf2015-02-05 22:02:37 -0800178 }
mtkleinf0599002015-07-13 06:18:39 -0700179
Hal Canary41248072019-07-11 16:32:53 -0400180 bool onAnimate(double nanos) override {
181 SkScalar currSecs = TimeUtils::Scaled(1e-9 * nanos, 100);
reed339cdbf2015-02-05 22:02:37 -0800182 SkScalar delta = currSecs - fPrevSecs;
183 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
reed339cdbf2015-02-05 22:02:37 -0800185 fStroke += fDStroke * delta;
186 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
187 fDStroke = -fDStroke;
188 }
189 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
Hal Canaryb1f411a2019-08-29 10:39:22 -0400192 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 fShowHairline = !fShowHairline;
Hal Canaryfcf63592019-07-12 11:32:43 -0400194 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400198 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199};
reeda7a8b102014-12-16 08:07:43 -0800200DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201
202//////////////////////////////////////////////////////////////////////////////
203
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500204#include "include/effects/SkCornerPathEffect.h"
205#include "include/utils/SkRandom.h"
reeda7a8b102014-12-16 08:07:43 -0800206
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400207class ArcToView : public Sample {
Mike Reed40e7e652017-07-22 22:12:59 -0400208 bool fDoFrame, fDoCorner, fDoConic;
209 SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800210public:
211 enum {
212 N = 4
213 };
214 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800215
reed8b575242014-12-17 01:47:32 -0800216 ArcToView()
Mike Reed40e7e652017-07-22 22:12:59 -0400217 : fDoFrame(false), fDoCorner(false), fDoConic(false)
reed8b575242014-12-17 01:47:32 -0800218 {
reeda7a8b102014-12-16 08:07:43 -0800219 SkRandom rand;
220 for (int i = 0; i < N; ++i) {
221 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
222 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
223 }
mtkleinf0599002015-07-13 06:18:39 -0700224
reed8b575242014-12-17 01:47:32 -0800225 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800226
227 fPtsPaint.setAntiAlias(true);
228 fPtsPaint.setStrokeWidth(15);
229 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
230
reeda7a8b102014-12-16 08:07:43 -0800231 fCornerPaint.setAntiAlias(true);
232 fCornerPaint.setStyle(SkPaint::kStroke_Style);
233 fCornerPaint.setStrokeWidth(13);
234 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700235 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800236
237 fSkeletonPaint.setAntiAlias(true);
238 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
239 fSkeletonPaint.setColor(SK_ColorRED);
240 }
241
reed8b575242014-12-17 01:47:32 -0800242 void toggle(bool& value) {
243 value = !value;
reed8b575242014-12-17 01:47:32 -0800244 }
245
reeda7a8b102014-12-16 08:07:43 -0800246protected:
Hal Canary8a027312019-07-03 10:55:44 -0400247 SkString name() override { return SkString("ArcTo"); }
248
Hal Canary6cc65e12019-07-03 15:53:04 -0400249 bool onChar(SkUnichar uni) override {
reed8b575242014-12-17 01:47:32 -0800250 switch (uni) {
251 case '1': this->toggle(fDoFrame); return true;
Mike Reed40e7e652017-07-22 22:12:59 -0400252 case '2': this->toggle(fDoCorner); return true;
253 case '3': this->toggle(fDoConic); return true;
reed8b575242014-12-17 01:47:32 -0800254 default: break;
255 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400256 return false;
reeda7a8b102014-12-16 08:07:43 -0800257 }
mtkleinf0599002015-07-13 06:18:39 -0700258
reed8b575242014-12-17 01:47:32 -0800259 void makePath(SkPath* path) {
260 path->moveTo(fPts[0]);
261 for (int i = 1; i < N; ++i) {
262 path->lineTo(fPts[i]);
263 }
264 if (!fDoFrame) {
265 path->close();
266 }
267 }
reeda7a8b102014-12-16 08:07:43 -0800268
mtklein36352bf2015-03-25 18:17:31 -0700269 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800270 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
271
272 SkPath path;
reed8b575242014-12-17 01:47:32 -0800273 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800274
reed8b575242014-12-17 01:47:32 -0800275 if (fDoCorner) {
276 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800277 }
reeda7a8b102014-12-16 08:07:43 -0800278
reed8b575242014-12-17 01:47:32 -0800279 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800280 }
281
mtklein36352bf2015-03-25 18:17:31 -0700282 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800283 int32_t index;
284 if (click->fMeta.findS32("index", &index)) {
285 SkASSERT((unsigned)index < N);
286 fPts[index] = click->fCurr;
reeda7a8b102014-12-16 08:07:43 -0800287 return true;
288 }
289 return false;
290 }
291
Hal Canaryb1f411a2019-08-29 10:39:22 -0400292 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
reeda7a8b102014-12-16 08:07:43 -0800293 const SkScalar tol = 4;
294 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
295 for (int i = 0; i < N; ++i) {
296 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400297 Click* click = new Click();
reeda7a8b102014-12-16 08:07:43 -0800298 click->fMeta.setS32("index", i);
299 return click;
300 }
301 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400302 return nullptr;
reeda7a8b102014-12-16 08:07:43 -0800303 }
304
305private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400306 typedef Sample INHERITED;
reeda7a8b102014-12-16 08:07:43 -0800307};
308DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400309
310/////////////
311
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400312class FatStroke : public Sample {
Mike Reeda964a292016-11-02 22:09:25 -0400313 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
314 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500315 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400316 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
317public:
318 enum {
319 N = 4
320 };
321 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400322
Mike Reeda964a292016-11-02 22:09:25 -0400323 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
324 fJoinType(0), fCapType(0)
325 {
326 SkRandom rand;
327 for (int i = 0; i < N; ++i) {
328 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
329 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
330 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400331
Mike Reeda964a292016-11-02 22:09:25 -0400332 fPtsPaint.setAntiAlias(true);
333 fPtsPaint.setStrokeWidth(10);
334 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400335
Mike Reeda964a292016-11-02 22:09:25 -0400336 fHiddenPaint.setAntiAlias(true);
337 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
338 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400339
Mike Reeda964a292016-11-02 22:09:25 -0400340 fStrokePaint.setAntiAlias(true);
341 fStrokePaint.setStyle(SkPaint::kStroke_Style);
342 fStrokePaint.setStrokeWidth(50);
343 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400344
Mike Reeda964a292016-11-02 22:09:25 -0400345 fSkeletonPaint.setAntiAlias(true);
346 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
347 fSkeletonPaint.setColor(SK_ColorRED);
348 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400349
Mike Reeda964a292016-11-02 22:09:25 -0400350 void toggle(bool& value) {
351 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400352 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400353
Mike Reeda964a292016-11-02 22:09:25 -0400354 void toggle3(int& value) {
355 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400356 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400357
Mike Reeda964a292016-11-02 22:09:25 -0400358protected:
Hal Canary8a027312019-07-03 10:55:44 -0400359 SkString name() override { return SkString("FatStroke"); }
360
Hal Canary6cc65e12019-07-03 15:53:04 -0400361 bool onChar(SkUnichar uni) override {
Mike Reeda964a292016-11-02 22:09:25 -0400362 switch (uni) {
363 case '1': this->toggle(fShowSkeleton); return true;
364 case '2': this->toggle(fShowStroke); return true;
365 case '3': this->toggle(fShowHidden); return true;
366 case '4': this->toggle3(fJoinType); return true;
367 case '5': this->toggle3(fCapType); return true;
368 case '6': this->toggle(fClosed); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500369 case '-': fWidth -= 5; return true;
370 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400371 default: break;
372 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400373 return false;
Mike Reeda964a292016-11-02 22:09:25 -0400374 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400375
Mike Reeda964a292016-11-02 22:09:25 -0400376 void makePath(SkPath* path) {
377 path->moveTo(fPts[0]);
378 for (int i = 1; i < N; ++i) {
379 path->lineTo(fPts[i]);
380 }
381 if (fClosed) {
382 path->close();
383 }
384 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400385
Mike Reeda964a292016-11-02 22:09:25 -0400386 void onDrawContent(SkCanvas* canvas) override {
387 canvas->drawColor(0xFFEEEEEE);
388
389 SkPath path;
390 this->makePath(&path);
391
Mike Reed1fba7022016-12-15 10:23:42 -0500392 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400393 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
394 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
395
396 if (fShowStroke) {
397 canvas->drawPath(path, fStrokePaint);
398 }
399 if (fShowHidden) {
400 SkPath hidden;
401 fStrokePaint.getFillPath(path, &hidden);
402 canvas->drawPath(hidden, fHiddenPaint);
403 }
404 if (fShowSkeleton) {
405 canvas->drawPath(path, fSkeletonPaint);
406 }
407 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
408 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400409
Mike Reeda964a292016-11-02 22:09:25 -0400410 bool onClick(Click* click) override {
411 int32_t index;
412 if (click->fMeta.findS32("index", &index)) {
413 SkASSERT((unsigned)index < N);
414 fPts[index] = click->fCurr;
Mike Reeda964a292016-11-02 22:09:25 -0400415 return true;
416 }
417 return false;
418 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400419
Hal Canaryb1f411a2019-08-29 10:39:22 -0400420 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reeda964a292016-11-02 22:09:25 -0400421 const SkScalar tol = 4;
422 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
423 for (int i = 0; i < N; ++i) {
424 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400425 Click* click = new Click();
Mike Reeda964a292016-11-02 22:09:25 -0400426 click->fMeta.setS32("index", i);
427 return click;
428 }
429 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400430 return nullptr;
Mike Reeda964a292016-11-02 22:09:25 -0400431 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400432
Mike Reeda964a292016-11-02 22:09:25 -0400433private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400434 typedef Sample INHERITED;
Mike Reeda964a292016-11-02 22:09:25 -0400435};
436DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400437
438static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
439 // F = At^3 + Bt^2 + Ct + D
440 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
441 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
442 SkVector C = (pts[1] - pts[0]) * 3.0f;
443 SkVector DA = pts[3] - pts[0];
444
445 // F' = 3At^2 + 2Bt + C
446 SkScalar a = 3 * A.cross(DA);
447 SkScalar b = 2 * B.cross(DA);
448 SkScalar c = C.cross(DA);
449
450 int n = SkFindUnitQuadRoots(a, b, c, t);
451 SkString str;
452 for (int i = 0; i < n; ++i) {
453 str.appendf(" %g", t[i]);
454 }
455 SkDebugf("roots %s\n", str.c_str());
456 return n;
457}
458
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400459class CubicCurve : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400460public:
461 enum {
462 N = 4
463 };
464 SkPoint fPts[N];
465
466 CubicCurve() {
467 SkRandom rand;
468 for (int i = 0; i < N; ++i) {
469 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
470 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
471 }
472 }
473
474protected:
Hal Canary8a027312019-07-03 10:55:44 -0400475 SkString name() override { return SkString("CubicCurve"); }
Mike Reed66fa4562018-04-06 19:56:50 -0400476
477 void onDrawContent(SkCanvas* canvas) override {
478 SkPaint paint;
479 paint.setAntiAlias(true);
480
481 {
482 SkPath path;
483 path.moveTo(fPts[0]);
484 path.cubicTo(fPts[1], fPts[2], fPts[3]);
485 paint.setStyle(SkPaint::kStroke_Style);
486 canvas->drawPath(path, paint);
487 }
488
489 {
490 paint.setColor(SK_ColorRED);
491 SkScalar t[2];
492 int n = compute_parallel_to_base(fPts, t);
493 SkPoint loc;
494 SkVector tan;
495 for (int i = 0; i < n; ++i) {
496 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
497 tan.setLength(30);
498 canvas->drawLine(loc - tan, loc + tan, paint);
499 }
500 paint.setStrokeWidth(0.5f);
501 canvas->drawLine(fPts[0], fPts[3], paint);
502
503 paint.setColor(SK_ColorBLUE);
504 paint.setStrokeWidth(6);
505 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
506 canvas->drawPoint(loc, paint);
507
508 paint.setColor(0xFF008800);
509 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
510 canvas->drawPoint(loc, paint);
511 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
512 canvas->drawPoint(loc, paint);
513
514 // n = SkFindCubicInflections(fPts, t);
515 // printf("inflections %d %g %g\n", n, t[0], t[1]);
516 }
517
518 {
519 paint.setStyle(SkPaint::kFill_Style);
520 paint.setColor(SK_ColorRED);
521 for (SkPoint p : fPts) {
522 canvas->drawCircle(p.fX, p.fY, 8, paint);
523 }
524 }
525 }
526
527 bool onClick(Click* click) override {
528 int32_t index;
529 if (click->fMeta.findS32("index", &index)) {
530 SkASSERT((unsigned)index < N);
531 fPts[index] = click->fCurr;
532 return true;
533 }
534 return false;
535 }
536
Hal Canaryb1f411a2019-08-29 10:39:22 -0400537 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400538 const SkScalar tol = 8;
539 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
540 for (int i = 0; i < N; ++i) {
541 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400542 Click* click = new Click();
Mike Reed66fa4562018-04-06 19:56:50 -0400543 click->fMeta.setS32("index", i);
544 return click;
545 }
546 }
547 return this->INHERITED::onFindClickHandler(x, y, modi);
548 }
549
550private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400551 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400552};
553DEF_SAMPLE( return new CubicCurve; )
554
555static SkPoint lerp(SkPoint a, SkPoint b, float t) {
556 return a * (1 - t) + b * t;
557}
558
Mike Reed83dc4732018-04-16 17:07:07 -0400559static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
560 // deviation = F' x (d - a) == 0, solve for t(s)
561 // F = At^3 + Bt^2 + Ct + D
562 // F' = 3At^2 + 2Bt + C
563 // Z = d - a
564 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
565 //
566 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
567 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
568 SkVector C = (src[1] - src[0]) * 3;
569 SkVector Z = src[3] - src[0];
570 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
571 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
572}
573
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400574class CubicCurve2 : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400575public:
576 enum {
577 N = 7
578 };
579 SkPoint fPts[N];
580 SkPoint* fQuad = fPts + 4;
581 SkScalar fT = 0.5f;
582 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400583 bool fShowFlatness = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400584 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400585
586 CubicCurve2() {
587 fPts[0] = { 90, 300 };
588 fPts[1] = { 30, 60 };
589 fPts[2] = { 250, 30 };
590 fPts[3] = { 350, 200 };
591
592 fQuad[0] = fPts[0] + SkVector{ 300, 0};
593 fQuad[1] = fPts[1] + SkVector{ 300, 0};
594 fQuad[2] = fPts[2] + SkVector{ 300, 0};
595 }
596
597protected:
Hal Canary8a027312019-07-03 10:55:44 -0400598 SkString name() override { return SkString("CubicCurve2"); }
599
Hal Canary6cc65e12019-07-03 15:53:04 -0400600 bool onChar(SkUnichar uni) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400601 switch (uni) {
602 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400603 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400604 case '-': fT -= 1.0f / 32; break;
605 case '=': fT += 1.0f / 32; break;
Hal Canary6cc65e12019-07-03 15:53:04 -0400606 default: return false;
Mike Reed66fa4562018-04-06 19:56:50 -0400607 }
608 fT = std::min(1.0f, std::max(0.0f, fT));
609 return true;
Mike Reed66fa4562018-04-06 19:56:50 -0400610 }
611
612 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
613 SkPaint paint(p);
614 SkPoint storage[3 + 2 + 1];
615 SkPoint* tmp = storage;
616 const SkPoint* prev = pts;
617 int n = count;
618 for (int n = count; n > 0; --n) {
619 for (int i = 0; i < n; ++i) {
620 canvas->drawLine(prev[i], prev[i+1], paint);
621 tmp[i] = lerp(prev[i], prev[i+1], fT);
622 }
623 prev = tmp;
624 tmp += n;
625 }
626
627 paint.setColor(SK_ColorBLUE);
628 paint.setStyle(SkPaint::kFill_Style);
629 n = tmp - storage;
630 for (int i = 0; i < n; ++i) {
631 canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
632 }
633 }
Hal Canaryd4998172018-07-11 11:31:34 -0400634
Mike Reed48bf6ab2018-04-15 20:04:45 -0400635 void showFlattness(SkCanvas* canvas) {
636 SkPaint paint;
637 paint.setStyle(SkPaint::kStroke_Style);
638 paint.setAntiAlias(true);
639
Mike Reeddccfa352018-04-17 17:10:51 -0400640 SkPaint paint2(paint);
641 paint2.setColor(0xFF008800);
642
Mike Reed48bf6ab2018-04-15 20:04:45 -0400643 paint.setColor(0xFF888888);
644 canvas->drawLine(fPts[0], fPts[3], paint);
645 canvas->drawLine(fQuad[0], fQuad[2], paint);
646
647 paint.setColor(0xFF0000FF);
648 SkPoint pts[2];
649 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
650 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
651 canvas->drawLine(pts[0], pts[1], paint);
652
Mike Reed83dc4732018-04-16 17:07:07 -0400653 // cubic
654
655 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
656 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400657 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400658
659 SkPoint anchor;
660 SkScalar ts[2];
661 int n = find_max_deviation_cubic(fPts, ts);
662 if (n > 0) {
663 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400664 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400665 canvas->drawLine(anchor, anchor + v0, paint);
666 if (n == 2) {
667 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400668 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400669 }
670 canvas->drawLine(anchor, anchor + v1, paint);
671 }
672 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400673 }
Mike Reed66fa4562018-04-06 19:56:50 -0400674
675 void onDrawContent(SkCanvas* canvas) override {
676 SkPaint paint;
677 paint.setAntiAlias(true);
678
679 {
680 paint.setStyle(SkPaint::kStroke_Style);
681 SkPath path;
682 path.moveTo(fPts[0]);
683 path.cubicTo(fPts[1], fPts[2], fPts[3]);
684 path.moveTo(fQuad[0]);
685 path.quadTo(fQuad[1], fQuad[2]);
686 canvas->drawPath(path, paint);
687 }
688
689 if (fShowSub) {
690 paint.setColor(SK_ColorRED);
691 paint.setStrokeWidth(1.7f);
692 this->showFrame(canvas, fPts, 3, paint);
693 this->showFrame(canvas, fQuad, 2, paint);
694
Mike Reed66fa4562018-04-06 19:56:50 -0400695 paint.setColor(SK_ColorBLACK);
696 paint.setStyle(SkPaint::kFill_Style);
Hal Canary4484b8f2019-01-08 14:00:08 -0500697 SkFont font(nullptr, 20);
698 canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
Mike Reed66fa4562018-04-06 19:56:50 -0400699 }
700
Mike Reed48bf6ab2018-04-15 20:04:45 -0400701 if (fShowFlatness) {
702 this->showFlattness(canvas);
703 }
704
Mike Reed66fa4562018-04-06 19:56:50 -0400705 paint.setStyle(SkPaint::kFill_Style);
706 paint.setColor(SK_ColorRED);
707 for (SkPoint p : fPts) {
708 canvas->drawCircle(p.fX, p.fY, 7, paint);
709 }
Mike Reedfb65db12018-04-19 19:18:35 -0400710
711 {
712 SkScalar ts[2];
713 int n = SkFindCubicInflections(fPts, ts);
714 for (int i = 0; i < n; ++i) {
715 SkPoint p;
716 SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
717 canvas->drawCircle(p.fX, p.fY, 3, paint);
718 }
719 }
720
Mike Reed66fa4562018-04-06 19:56:50 -0400721 }
722
723 bool onClick(Click* click) override {
724 int32_t index;
725 if (click->fMeta.findS32("index", &index)) {
726 SkASSERT((unsigned)index < N);
727 fPts[index] = click->fCurr;
728 return true;
729 }
730 return false;
731 }
732
Hal Canaryb1f411a2019-08-29 10:39:22 -0400733 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400734 const SkScalar tol = 8;
735 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
736 for (int i = 0; i < N; ++i) {
737 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400738 Click* click = new Click();
Mike Reed66fa4562018-04-06 19:56:50 -0400739 click->fMeta.setS32("index", i);
740 return click;
741 }
742 }
743 return this->INHERITED::onFindClickHandler(x, y, modi);
744 }
745
746private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400747 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400748};
749DEF_SAMPLE( return new CubicCurve2; )
750