blob: d3717516d6009aeb29d9ea54938facd18b7256d5 [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"
Mike Reed65dc5792020-10-23 16:25:41 -040014#include "include/core/SkPathBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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:
John Stiles7571f9e2020-09-02 22:42:33 -0400198 using INHERITED = Sample;
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
Hal Canaryb1f411a2019-08-29 10:39:22 -0400282 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
reeda7a8b102014-12-16 08:07:43 -0800283 const SkScalar tol = 4;
284 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
285 for (int i = 0; i < N; ++i) {
286 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Mike Reed0ce3e882020-04-22 11:09:30 -0400287 return new Click([this, i](Click* c) {
288 fPts[i] = c->fCurr;
289 return true;
290 });
reeda7a8b102014-12-16 08:07:43 -0800291 }
292 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400293 return nullptr;
reeda7a8b102014-12-16 08:07:43 -0800294 }
295
296private:
John Stiles7571f9e2020-09-02 22:42:33 -0400297 using INHERITED = Sample;
reeda7a8b102014-12-16 08:07:43 -0800298};
299DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400300
301/////////////
302
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400303class FatStroke : public Sample {
Mike Reed439fe602020-06-26 14:33:13 -0400304 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton, fAsCurves = false;
Mike Reeda964a292016-11-02 22:09:25 -0400305 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500306 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400307 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
308public:
309 enum {
310 N = 4
311 };
312 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400313
Mike Reeda964a292016-11-02 22:09:25 -0400314 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
315 fJoinType(0), fCapType(0)
316 {
317 SkRandom rand;
318 for (int i = 0; i < N; ++i) {
319 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
320 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
321 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400322
Mike Reeda964a292016-11-02 22:09:25 -0400323 fPtsPaint.setAntiAlias(true);
324 fPtsPaint.setStrokeWidth(10);
325 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400326
Mike Reeda964a292016-11-02 22:09:25 -0400327 fHiddenPaint.setAntiAlias(true);
328 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
329 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400330
Mike Reeda964a292016-11-02 22:09:25 -0400331 fStrokePaint.setAntiAlias(true);
332 fStrokePaint.setStyle(SkPaint::kStroke_Style);
333 fStrokePaint.setStrokeWidth(50);
334 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400335
Mike Reeda964a292016-11-02 22:09:25 -0400336 fSkeletonPaint.setAntiAlias(true);
337 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
338 fSkeletonPaint.setColor(SK_ColorRED);
339 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400340
Mike Reeda964a292016-11-02 22:09:25 -0400341 void toggle(bool& value) {
342 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400343 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400344
Mike Reeda964a292016-11-02 22:09:25 -0400345 void toggle3(int& value) {
346 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400347 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400348
Mike Reeda964a292016-11-02 22:09:25 -0400349protected:
Hal Canary8a027312019-07-03 10:55:44 -0400350 SkString name() override { return SkString("FatStroke"); }
351
Hal Canary6cc65e12019-07-03 15:53:04 -0400352 bool onChar(SkUnichar uni) override {
Mike Reeda964a292016-11-02 22:09:25 -0400353 switch (uni) {
354 case '1': this->toggle(fShowSkeleton); return true;
355 case '2': this->toggle(fShowStroke); return true;
356 case '3': this->toggle(fShowHidden); return true;
357 case '4': this->toggle3(fJoinType); return true;
358 case '5': this->toggle3(fCapType); return true;
359 case '6': this->toggle(fClosed); return true;
Mike Reed439fe602020-06-26 14:33:13 -0400360 case 'c': this->toggle(fAsCurves); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500361 case '-': fWidth -= 5; return true;
362 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400363 default: break;
364 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400365 return false;
Mike Reeda964a292016-11-02 22:09:25 -0400366 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400367
Mike Reeda964a292016-11-02 22:09:25 -0400368 void makePath(SkPath* path) {
369 path->moveTo(fPts[0]);
Mike Reed439fe602020-06-26 14:33:13 -0400370 if (fAsCurves) {
371 for (int i = 1; i < N-2; ++i) {
372 path->quadTo(fPts[i], (fPts[i+1] + fPts[i]) * 0.5f);
373 }
374 path->quadTo(fPts[N-2], fPts[N-1]);
375 } else {
376 for (int i = 1; i < N; ++i) {
377 path->lineTo(fPts[i]);
378 }
Mike Reeda964a292016-11-02 22:09:25 -0400379 }
380 if (fClosed) {
381 path->close();
382 }
383 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400384
Mike Reeda964a292016-11-02 22:09:25 -0400385 void onDrawContent(SkCanvas* canvas) override {
386 canvas->drawColor(0xFFEEEEEE);
387
388 SkPath path;
389 this->makePath(&path);
390
Mike Reed1fba7022016-12-15 10:23:42 -0500391 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400392 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
393 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
394
395 if (fShowStroke) {
396 canvas->drawPath(path, fStrokePaint);
397 }
398 if (fShowHidden) {
399 SkPath hidden;
400 fStrokePaint.getFillPath(path, &hidden);
401 canvas->drawPath(hidden, fHiddenPaint);
402 }
403 if (fShowSkeleton) {
404 canvas->drawPath(path, fSkeletonPaint);
405 }
406 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
407 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400408
Hal Canaryb1f411a2019-08-29 10:39:22 -0400409 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reeda964a292016-11-02 22:09:25 -0400410 const SkScalar tol = 4;
411 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
412 for (int i = 0; i < N; ++i) {
413 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Mike Reed0ce3e882020-04-22 11:09:30 -0400414 return new Click([this, i](Click* c) {
415 fPts[i] = c->fCurr;
416 return true;
417 });
Mike Reeda964a292016-11-02 22:09:25 -0400418 }
419 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400420 return nullptr;
Mike Reeda964a292016-11-02 22:09:25 -0400421 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400422
Mike Reeda964a292016-11-02 22:09:25 -0400423private:
John Stiles7571f9e2020-09-02 22:42:33 -0400424 using INHERITED = Sample;
Mike Reeda964a292016-11-02 22:09:25 -0400425};
426DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400427
428static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
429 // F = At^3 + Bt^2 + Ct + D
430 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
431 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
432 SkVector C = (pts[1] - pts[0]) * 3.0f;
433 SkVector DA = pts[3] - pts[0];
434
435 // F' = 3At^2 + 2Bt + C
436 SkScalar a = 3 * A.cross(DA);
437 SkScalar b = 2 * B.cross(DA);
438 SkScalar c = C.cross(DA);
439
440 int n = SkFindUnitQuadRoots(a, b, c, t);
441 SkString str;
442 for (int i = 0; i < n; ++i) {
443 str.appendf(" %g", t[i]);
444 }
445 SkDebugf("roots %s\n", str.c_str());
446 return n;
447}
448
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400449class CubicCurve : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400450public:
451 enum {
452 N = 4
453 };
454 SkPoint fPts[N];
455
456 CubicCurve() {
457 SkRandom rand;
458 for (int i = 0; i < N; ++i) {
459 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
460 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
461 }
462 }
463
464protected:
Hal Canary8a027312019-07-03 10:55:44 -0400465 SkString name() override { return SkString("CubicCurve"); }
Mike Reed66fa4562018-04-06 19:56:50 -0400466
467 void onDrawContent(SkCanvas* canvas) override {
468 SkPaint paint;
469 paint.setAntiAlias(true);
470
471 {
472 SkPath path;
473 path.moveTo(fPts[0]);
474 path.cubicTo(fPts[1], fPts[2], fPts[3]);
475 paint.setStyle(SkPaint::kStroke_Style);
476 canvas->drawPath(path, paint);
477 }
478
479 {
480 paint.setColor(SK_ColorRED);
481 SkScalar t[2];
482 int n = compute_parallel_to_base(fPts, t);
483 SkPoint loc;
484 SkVector tan;
485 for (int i = 0; i < n; ++i) {
486 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
487 tan.setLength(30);
488 canvas->drawLine(loc - tan, loc + tan, paint);
489 }
490 paint.setStrokeWidth(0.5f);
491 canvas->drawLine(fPts[0], fPts[3], paint);
492
493 paint.setColor(SK_ColorBLUE);
494 paint.setStrokeWidth(6);
495 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
496 canvas->drawPoint(loc, paint);
497
498 paint.setColor(0xFF008800);
499 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
500 canvas->drawPoint(loc, paint);
501 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
502 canvas->drawPoint(loc, paint);
503
504 // n = SkFindCubicInflections(fPts, t);
505 // printf("inflections %d %g %g\n", n, t[0], t[1]);
506 }
507
508 {
509 paint.setStyle(SkPaint::kFill_Style);
510 paint.setColor(SK_ColorRED);
511 for (SkPoint p : fPts) {
512 canvas->drawCircle(p.fX, p.fY, 8, paint);
513 }
514 }
515 }
516
Hal Canaryb1f411a2019-08-29 10:39:22 -0400517 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400518 const SkScalar tol = 8;
519 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
520 for (int i = 0; i < N; ++i) {
521 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Mike Reed0ce3e882020-04-22 11:09:30 -0400522 return new Click([this, i](Click* c) {
523 fPts[i] = c->fCurr;
524 return true;
525 });
Mike Reed66fa4562018-04-06 19:56:50 -0400526 }
527 }
528 return this->INHERITED::onFindClickHandler(x, y, modi);
529 }
530
531private:
John Stiles7571f9e2020-09-02 22:42:33 -0400532 using INHERITED = Sample;
Mike Reed66fa4562018-04-06 19:56:50 -0400533};
534DEF_SAMPLE( return new CubicCurve; )
535
536static SkPoint lerp(SkPoint a, SkPoint b, float t) {
537 return a * (1 - t) + b * t;
538}
539
Mike Reed83dc4732018-04-16 17:07:07 -0400540static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
541 // deviation = F' x (d - a) == 0, solve for t(s)
542 // F = At^3 + Bt^2 + Ct + D
543 // F' = 3At^2 + 2Bt + C
544 // Z = d - a
545 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
546 //
547 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
548 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
549 SkVector C = (src[1] - src[0]) * 3;
550 SkVector Z = src[3] - src[0];
551 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
552 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
553}
554
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400555class CubicCurve2 : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400556public:
557 enum {
558 N = 7
559 };
560 SkPoint fPts[N];
561 SkPoint* fQuad = fPts + 4;
562 SkScalar fT = 0.5f;
563 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400564 bool fShowFlatness = false;
Mike Reed65dc5792020-10-23 16:25:41 -0400565 bool fShowInnerQuads = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400566 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400567
568 CubicCurve2() {
569 fPts[0] = { 90, 300 };
570 fPts[1] = { 30, 60 };
571 fPts[2] = { 250, 30 };
572 fPts[3] = { 350, 200 };
573
574 fQuad[0] = fPts[0] + SkVector{ 300, 0};
575 fQuad[1] = fPts[1] + SkVector{ 300, 0};
576 fQuad[2] = fPts[2] + SkVector{ 300, 0};
577 }
578
579protected:
Hal Canary8a027312019-07-03 10:55:44 -0400580 SkString name() override { return SkString("CubicCurve2"); }
581
Hal Canary6cc65e12019-07-03 15:53:04 -0400582 bool onChar(SkUnichar uni) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400583 switch (uni) {
584 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400585 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400586 case '-': fT -= 1.0f / 32; break;
587 case '=': fT += 1.0f / 32; break;
Mike Reed65dc5792020-10-23 16:25:41 -0400588 case 'q': fShowInnerQuads = !fShowInnerQuads; break;
Hal Canary6cc65e12019-07-03 15:53:04 -0400589 default: return false;
Mike Reed66fa4562018-04-06 19:56:50 -0400590 }
591 fT = std::min(1.0f, std::max(0.0f, fT));
592 return true;
Mike Reed66fa4562018-04-06 19:56:50 -0400593 }
594
Mike Reed65dc5792020-10-23 16:25:41 -0400595 static void Dot(SkCanvas* canvas, SkPoint p, SkScalar radius, SkColor c) {
596 SkPaint paint;
597 paint.setAntiAlias(true);
598 paint.setColor(c);
599 canvas->drawCircle(p.fX, p.fY, radius, paint);
600 }
601
Mike Reed66fa4562018-04-06 19:56:50 -0400602 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
603 SkPaint paint(p);
604 SkPoint storage[3 + 2 + 1];
605 SkPoint* tmp = storage;
606 const SkPoint* prev = pts;
607 int n = count;
608 for (int n = count; n > 0; --n) {
609 for (int i = 0; i < n; ++i) {
610 canvas->drawLine(prev[i], prev[i+1], paint);
611 tmp[i] = lerp(prev[i], prev[i+1], fT);
612 }
613 prev = tmp;
614 tmp += n;
615 }
616
617 paint.setColor(SK_ColorBLUE);
618 paint.setStyle(SkPaint::kFill_Style);
619 n = tmp - storage;
620 for (int i = 0; i < n; ++i) {
Mike Reed65dc5792020-10-23 16:25:41 -0400621 Dot(canvas, storage[i], 4, SK_ColorBLUE);
Mike Reed66fa4562018-04-06 19:56:50 -0400622 }
623 }
Hal Canaryd4998172018-07-11 11:31:34 -0400624
Mike Reed48bf6ab2018-04-15 20:04:45 -0400625 void showFlattness(SkCanvas* canvas) {
626 SkPaint paint;
627 paint.setStyle(SkPaint::kStroke_Style);
628 paint.setAntiAlias(true);
629
Mike Reeddccfa352018-04-17 17:10:51 -0400630 SkPaint paint2(paint);
631 paint2.setColor(0xFF008800);
632
Mike Reed48bf6ab2018-04-15 20:04:45 -0400633 paint.setColor(0xFF888888);
634 canvas->drawLine(fPts[0], fPts[3], paint);
635 canvas->drawLine(fQuad[0], fQuad[2], paint);
636
637 paint.setColor(0xFF0000FF);
638 SkPoint pts[2];
639 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
640 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
641 canvas->drawLine(pts[0], pts[1], paint);
642
Mike Reed83dc4732018-04-16 17:07:07 -0400643 // cubic
644
645 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
646 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400647 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400648
649 SkPoint anchor;
650 SkScalar ts[2];
651 int n = find_max_deviation_cubic(fPts, ts);
652 if (n > 0) {
653 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400654 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400655 canvas->drawLine(anchor, anchor + v0, paint);
656 if (n == 2) {
657 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400658 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400659 }
660 canvas->drawLine(anchor, anchor + v1, paint);
661 }
662 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400663 }
Mike Reed66fa4562018-04-06 19:56:50 -0400664
Mike Reed65dc5792020-10-23 16:25:41 -0400665 void showInnerQuads(SkCanvas* canvas) {
666 auto draw_quad = [canvas](SkPoint a, SkPoint b, SkPoint c, SkColor color) {
667 SkPaint paint;
668 paint.setAntiAlias(true);
669 paint.setStroke(true);
670 paint.setColor(color);
671
672 canvas->drawPath(SkPathBuilder().moveTo(a).quadTo(b, c).detach(), paint);
673 };
674
675 SkPoint p0 = SkEvalQuadAt(&fPts[0], fT),
676 p1 = SkEvalQuadAt(&fPts[1], fT),
677 p2 = lerp(p0, p1, fT);
678
679 draw_quad(fPts[0], fPts[1], fPts[2], SK_ColorRED);
680 Dot(canvas, p0, 4, SK_ColorRED);
681
682 draw_quad(fPts[1], fPts[2], fPts[3], SK_ColorBLUE);
683 Dot(canvas, p1, 4, SK_ColorBLUE);
684
685 SkPaint paint;
686 paint.setAntiAlias(true);
687 paint.setColor(0xFF008800);
688 canvas->drawLine(p0, p1, paint);
689 Dot(canvas, p2, 4, 0xFF00AA00);
690 }
691
Mike Reed66fa4562018-04-06 19:56:50 -0400692 void onDrawContent(SkCanvas* canvas) override {
693 SkPaint paint;
694 paint.setAntiAlias(true);
695
696 {
697 paint.setStyle(SkPaint::kStroke_Style);
698 SkPath path;
699 path.moveTo(fPts[0]);
700 path.cubicTo(fPts[1], fPts[2], fPts[3]);
701 path.moveTo(fQuad[0]);
702 path.quadTo(fQuad[1], fQuad[2]);
703 canvas->drawPath(path, paint);
704 }
705
706 if (fShowSub) {
707 paint.setColor(SK_ColorRED);
708 paint.setStrokeWidth(1.7f);
709 this->showFrame(canvas, fPts, 3, paint);
710 this->showFrame(canvas, fQuad, 2, paint);
711
Mike Reed66fa4562018-04-06 19:56:50 -0400712 paint.setColor(SK_ColorBLACK);
713 paint.setStyle(SkPaint::kFill_Style);
Hal Canary4484b8f2019-01-08 14:00:08 -0500714 SkFont font(nullptr, 20);
715 canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
Mike Reed66fa4562018-04-06 19:56:50 -0400716 }
717
Mike Reed48bf6ab2018-04-15 20:04:45 -0400718 if (fShowFlatness) {
719 this->showFlattness(canvas);
720 }
721
Mike Reed65dc5792020-10-23 16:25:41 -0400722 if (fShowInnerQuads) {
723 this->showInnerQuads(canvas);
Mike Reed66fa4562018-04-06 19:56:50 -0400724 }
Mike Reedfb65db12018-04-19 19:18:35 -0400725
Mike Reed65dc5792020-10-23 16:25:41 -0400726 paint.setColor(SK_ColorGRAY);
727 paint.setStroke(true);
728 canvas->drawPath(SkPathBuilder().addPolygon(fPts, 4, false).detach(), paint);
729 canvas->drawPath(SkPathBuilder().addPolygon(fQuad, 3, false).detach(), paint);
730
731 for (SkPoint p : fPts) {
732 Dot(canvas, p, 7, SK_ColorBLACK);
733 }
734
735 if (false) {
Mike Reedfb65db12018-04-19 19:18:35 -0400736 SkScalar ts[2];
737 int n = SkFindCubicInflections(fPts, ts);
738 for (int i = 0; i < n; ++i) {
739 SkPoint p;
740 SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
741 canvas->drawCircle(p.fX, p.fY, 3, paint);
742 }
743 }
744
Mike Reed66fa4562018-04-06 19:56:50 -0400745 }
746
Hal Canaryb1f411a2019-08-29 10:39:22 -0400747 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400748 const SkScalar tol = 8;
749 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
750 for (int i = 0; i < N; ++i) {
751 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
Mike Reed0ce3e882020-04-22 11:09:30 -0400752 return new Click([this, i](Click* c) {
753 fPts[i] = c->fCurr;
754 return true;
755 });
Mike Reed66fa4562018-04-06 19:56:50 -0400756 }
757 }
758 return this->INHERITED::onFindClickHandler(x, y, modi);
759 }
760
761private:
John Stiles7571f9e2020-09-02 22:42:33 -0400762 using INHERITED = Sample;
Mike Reed66fa4562018-04-06 19:56:50 -0400763};
764DEF_SAMPLE( return new CubicCurve2; )
765