blob: 8ba8380b0a9ba8d28b8ad4ff132d1885112b904c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkGraphics.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkTime.h"
18#include "include/core/SkTypeface.h"
19#include "include/effects/SkGradientShader.h"
20#include "include/utils/SkParsePath.h"
21#include "samplecode/Sample.h"
22#include "src/utils/SkUTF.h"
23#include "tools/timer/AnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkGeometry.h"
reed@android.coma9640282009-08-28 20:06:54 +000026
bungeman60e0fee2015-08-26 05:15:46 -070027#include <stdlib.h>
28
reed@android.coma9640282009-08-28 20:06:54 +000029// http://code.google.com/p/skia/issues/detail?id=32
30static void test_cubic() {
31 SkPoint src[4] = {
reed@google.com261b8e22011-04-14 17:53:24 +000032 { 556.25000f, 523.03003f },
33 { 556.23999f, 522.96002f },
34 { 556.21997f, 522.89001f },
35 { 556.21997f, 522.82001f }
reed@android.coma9640282009-08-28 20:06:54 +000036 };
37 SkPoint dst[11];
38 dst[10].set(42, -42); // one past the end, that we don't clobber these
39 SkScalar tval[] = { 0.33333334f, 0.99999994f };
40
41 SkChopCubicAt(src, dst, tval, 2);
42
43#if 0
44 for (int i = 0; i < 11; i++) {
45 SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
46 }
47#endif
48}
49
reed@android.comd0d0e652009-10-13 13:31:27 +000050static void test_cubic2() {
51 const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
52 SkPath path;
53 SkParsePath::FromSVGString(str, &path);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.comd0d0e652009-10-13 13:31:27 +000055 {
reed@android.comd0d0e652009-10-13 13:31:27 +000056 SkRect r = path.getBounds();
57 SkIRect ir;
58 r.round(&ir);
bungeman@google.comfab44db2013-10-11 18:50:45 +000059 SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
60 SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
61 SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
62 ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
reed@android.comd0d0e652009-10-13 13:31:27 +000063 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.comd0d0e652009-10-13 13:31:27 +000065 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000066 bitmap.allocN32Pixels(300, 200);
reed@android.comd0d0e652009-10-13 13:31:27 +000067
68 SkCanvas canvas(bitmap);
69 SkPaint paint;
70 paint.setAntiAlias(true);
71 canvas.drawPath(path, paint);
72}
73
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040074class PathView : public Sample {
reed339cdbf2015-02-05 22:02:37 -080075 SkScalar fPrevSecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
reed339cdbf2015-02-05 22:02:37 -080077 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkPath fPath[6];
79 bool fShowHairline;
reed@google.comc9fa63c2012-03-12 21:14:09 +000080 bool fOnce;
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
82 PathView() {
reed339cdbf2015-02-05 22:02:37 -080083 fPrevSecs = 0;
reed@google.comc9fa63c2012-03-12 21:14:09 +000084 fOnce = false;
85 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@google.comc9fa63c2012-03-12 21:14:09 +000087 void init() {
88 if (fOnce) {
89 return;
90 }
91 fOnce = true;
92
reed@android.coma9640282009-08-28 20:06:54 +000093 test_cubic();
reed@android.comd0d0e652009-10-13 13:31:27 +000094 test_cubic2();
reed@android.coma9640282009-08-28 20:06:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 fShowHairline = false;
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 fDStroke = 1;
99 fStroke = 10;
100 fMinStroke = 10;
101 fMaxStroke = 180;
102
reed339cdbf2015-02-05 22:02:37 -0800103 const SkScalar V = 85;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed339cdbf2015-02-05 22:02:37 -0800105 fPath[0].moveTo(40, 70);
106 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
107 fPath[0].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed339cdbf2015-02-05 22:02:37 -0800109 fPath[1].moveTo(40, 70);
110 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
111 fPath[1].lineTo(110, 70);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed339cdbf2015-02-05 22:02:37 -0800113 fPath[2].moveTo(V, V);
114 fPath[2].lineTo(50, V);
115 fPath[2].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed339cdbf2015-02-05 22:02:37 -0800117 fPath[3].moveTo(50, 50);
118 fPath[3].lineTo(50, V);
119 fPath[3].lineTo(V, V);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed339cdbf2015-02-05 22:02:37 -0800121 fPath[4].moveTo(50, 50);
122 fPath[4].lineTo(50, V);
123 fPath[4].lineTo(52, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed339cdbf2015-02-05 22:02:37 -0800125 fPath[5].moveTo(52, 50);
126 fPath[5].lineTo(50, V);
127 fPath[5].lineTo(50, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com0faac1e2011-05-11 05:58:58 +0000129 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132protected:
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
Mike Kleincd5104e2019-03-20 11:55:08 -0500180 bool onAnimate(const AnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800181 SkScalar currSecs = timer.scaled(100);
182 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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400192 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 fShowHairline = !fShowHairline;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000194 return this->INHERITED::onFindClickHandler(x, y, modi);
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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400249 bool onQuery(Sample::Event* evt) override {
reed8b575242014-12-17 01:47:32 -0800250 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400251 if (Sample::CharQ(*evt, &uni)) {
reed8b575242014-12-17 01:47:32 -0800252 switch (uni) {
253 case '1': this->toggle(fDoFrame); return true;
Mike Reed40e7e652017-07-22 22:12:59 -0400254 case '2': this->toggle(fDoCorner); return true;
255 case '3': this->toggle(fDoConic); return true;
reed8b575242014-12-17 01:47:32 -0800256 default: break;
257 }
258 }
reeda7a8b102014-12-16 08:07:43 -0800259 return this->INHERITED::onQuery(evt);
260 }
mtkleinf0599002015-07-13 06:18:39 -0700261
reed8b575242014-12-17 01:47:32 -0800262 void makePath(SkPath* path) {
263 path->moveTo(fPts[0]);
264 for (int i = 1; i < N; ++i) {
265 path->lineTo(fPts[i]);
266 }
267 if (!fDoFrame) {
268 path->close();
269 }
270 }
reeda7a8b102014-12-16 08:07:43 -0800271
mtklein36352bf2015-03-25 18:17:31 -0700272 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800273 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
274
275 SkPath path;
reed8b575242014-12-17 01:47:32 -0800276 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800277
reed8b575242014-12-17 01:47:32 -0800278 if (fDoCorner) {
279 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800280 }
reeda7a8b102014-12-16 08:07:43 -0800281
reed8b575242014-12-17 01:47:32 -0800282 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800283 }
284
mtklein36352bf2015-03-25 18:17:31 -0700285 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800286 int32_t index;
287 if (click->fMeta.findS32("index", &index)) {
288 SkASSERT((unsigned)index < N);
289 fPts[index] = click->fCurr;
reeda7a8b102014-12-16 08:07:43 -0800290 return true;
291 }
292 return false;
293 }
294
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400295 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800296 const SkScalar tol = 4;
297 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
298 for (int i = 0; i < N; ++i) {
299 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
300 Click* click = new Click(this);
301 click->fMeta.setS32("index", i);
302 return click;
303 }
304 }
305 return this->INHERITED::onFindClickHandler(x, y, modi);
306 }
307
308private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400309 typedef Sample INHERITED;
reeda7a8b102014-12-16 08:07:43 -0800310};
311DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400312
313/////////////
314
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400315class FatStroke : public Sample {
Mike Reeda964a292016-11-02 22:09:25 -0400316 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
317 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500318 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400319 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
320public:
321 enum {
322 N = 4
323 };
324 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400325
Mike Reeda964a292016-11-02 22:09:25 -0400326 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
327 fJoinType(0), fCapType(0)
328 {
329 SkRandom rand;
330 for (int i = 0; i < N; ++i) {
331 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
332 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
333 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400334
Mike Reeda964a292016-11-02 22:09:25 -0400335 fPtsPaint.setAntiAlias(true);
336 fPtsPaint.setStrokeWidth(10);
337 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400338
Mike Reeda964a292016-11-02 22:09:25 -0400339 fHiddenPaint.setAntiAlias(true);
340 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
341 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400342
Mike Reeda964a292016-11-02 22:09:25 -0400343 fStrokePaint.setAntiAlias(true);
344 fStrokePaint.setStyle(SkPaint::kStroke_Style);
345 fStrokePaint.setStrokeWidth(50);
346 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400347
Mike Reeda964a292016-11-02 22:09:25 -0400348 fSkeletonPaint.setAntiAlias(true);
349 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
350 fSkeletonPaint.setColor(SK_ColorRED);
351 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400352
Mike Reeda964a292016-11-02 22:09:25 -0400353 void toggle(bool& value) {
354 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400355 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400356
Mike Reeda964a292016-11-02 22:09:25 -0400357 void toggle3(int& value) {
358 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400359 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400360
Mike Reeda964a292016-11-02 22:09:25 -0400361protected:
Hal Canary8a027312019-07-03 10:55:44 -0400362 SkString name() override { return SkString("FatStroke"); }
363
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400364 bool onQuery(Sample::Event* evt) override {
Mike Reeda964a292016-11-02 22:09:25 -0400365 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400366 if (Sample::CharQ(*evt, &uni)) {
Mike Reeda964a292016-11-02 22:09:25 -0400367 switch (uni) {
368 case '1': this->toggle(fShowSkeleton); return true;
369 case '2': this->toggle(fShowStroke); return true;
370 case '3': this->toggle(fShowHidden); return true;
371 case '4': this->toggle3(fJoinType); return true;
372 case '5': this->toggle3(fCapType); return true;
373 case '6': this->toggle(fClosed); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500374 case '-': fWidth -= 5; return true;
375 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400376 default: break;
377 }
378 }
379 return this->INHERITED::onQuery(evt);
380 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400381
Mike Reeda964a292016-11-02 22:09:25 -0400382 void makePath(SkPath* path) {
383 path->moveTo(fPts[0]);
384 for (int i = 1; i < N; ++i) {
385 path->lineTo(fPts[i]);
386 }
387 if (fClosed) {
388 path->close();
389 }
390 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400391
Mike Reeda964a292016-11-02 22:09:25 -0400392 void onDrawContent(SkCanvas* canvas) override {
393 canvas->drawColor(0xFFEEEEEE);
394
395 SkPath path;
396 this->makePath(&path);
397
Mike Reed1fba7022016-12-15 10:23:42 -0500398 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400399 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
400 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
401
402 if (fShowStroke) {
403 canvas->drawPath(path, fStrokePaint);
404 }
405 if (fShowHidden) {
406 SkPath hidden;
407 fStrokePaint.getFillPath(path, &hidden);
408 canvas->drawPath(hidden, fHiddenPaint);
409 }
410 if (fShowSkeleton) {
411 canvas->drawPath(path, fSkeletonPaint);
412 }
413 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
414 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400415
Mike Reeda964a292016-11-02 22:09:25 -0400416 bool onClick(Click* click) override {
417 int32_t index;
418 if (click->fMeta.findS32("index", &index)) {
419 SkASSERT((unsigned)index < N);
420 fPts[index] = click->fCurr;
Mike Reeda964a292016-11-02 22:09:25 -0400421 return true;
422 }
423 return false;
424 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400425
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400426 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reeda964a292016-11-02 22:09:25 -0400427 const SkScalar tol = 4;
428 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
429 for (int i = 0; i < N; ++i) {
430 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
431 Click* click = new Click(this);
432 click->fMeta.setS32("index", i);
433 return click;
434 }
435 }
436 return this->INHERITED::onFindClickHandler(x, y, modi);
437 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400438
Mike Reeda964a292016-11-02 22:09:25 -0400439private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400440 typedef Sample INHERITED;
Mike Reeda964a292016-11-02 22:09:25 -0400441};
442DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400443
444static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
445 // F = At^3 + Bt^2 + Ct + D
446 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
447 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
448 SkVector C = (pts[1] - pts[0]) * 3.0f;
449 SkVector DA = pts[3] - pts[0];
450
451 // F' = 3At^2 + 2Bt + C
452 SkScalar a = 3 * A.cross(DA);
453 SkScalar b = 2 * B.cross(DA);
454 SkScalar c = C.cross(DA);
455
456 int n = SkFindUnitQuadRoots(a, b, c, t);
457 SkString str;
458 for (int i = 0; i < n; ++i) {
459 str.appendf(" %g", t[i]);
460 }
461 SkDebugf("roots %s\n", str.c_str());
462 return n;
463}
464
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400465class CubicCurve : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400466public:
467 enum {
468 N = 4
469 };
470 SkPoint fPts[N];
471
472 CubicCurve() {
473 SkRandom rand;
474 for (int i = 0; i < N; ++i) {
475 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
476 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
477 }
478 }
479
480protected:
Hal Canary8a027312019-07-03 10:55:44 -0400481 SkString name() override { return SkString("CubicCurve"); }
Mike Reed66fa4562018-04-06 19:56:50 -0400482
483 void onDrawContent(SkCanvas* canvas) override {
484 SkPaint paint;
485 paint.setAntiAlias(true);
486
487 {
488 SkPath path;
489 path.moveTo(fPts[0]);
490 path.cubicTo(fPts[1], fPts[2], fPts[3]);
491 paint.setStyle(SkPaint::kStroke_Style);
492 canvas->drawPath(path, paint);
493 }
494
495 {
496 paint.setColor(SK_ColorRED);
497 SkScalar t[2];
498 int n = compute_parallel_to_base(fPts, t);
499 SkPoint loc;
500 SkVector tan;
501 for (int i = 0; i < n; ++i) {
502 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
503 tan.setLength(30);
504 canvas->drawLine(loc - tan, loc + tan, paint);
505 }
506 paint.setStrokeWidth(0.5f);
507 canvas->drawLine(fPts[0], fPts[3], paint);
508
509 paint.setColor(SK_ColorBLUE);
510 paint.setStrokeWidth(6);
511 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
512 canvas->drawPoint(loc, paint);
513
514 paint.setColor(0xFF008800);
515 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
516 canvas->drawPoint(loc, paint);
517 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
518 canvas->drawPoint(loc, paint);
519
520 // n = SkFindCubicInflections(fPts, t);
521 // printf("inflections %d %g %g\n", n, t[0], t[1]);
522 }
523
524 {
525 paint.setStyle(SkPaint::kFill_Style);
526 paint.setColor(SK_ColorRED);
527 for (SkPoint p : fPts) {
528 canvas->drawCircle(p.fX, p.fY, 8, paint);
529 }
530 }
531 }
532
533 bool onClick(Click* click) override {
534 int32_t index;
535 if (click->fMeta.findS32("index", &index)) {
536 SkASSERT((unsigned)index < N);
537 fPts[index] = click->fCurr;
538 return true;
539 }
540 return false;
541 }
542
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400543 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400544 const SkScalar tol = 8;
545 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
546 for (int i = 0; i < N; ++i) {
547 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
548 Click* click = new Click(this);
549 click->fMeta.setS32("index", i);
550 return click;
551 }
552 }
553 return this->INHERITED::onFindClickHandler(x, y, modi);
554 }
555
556private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400557 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400558};
559DEF_SAMPLE( return new CubicCurve; )
560
561static SkPoint lerp(SkPoint a, SkPoint b, float t) {
562 return a * (1 - t) + b * t;
563}
564
Mike Reed83dc4732018-04-16 17:07:07 -0400565static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
566 // deviation = F' x (d - a) == 0, solve for t(s)
567 // F = At^3 + Bt^2 + Ct + D
568 // F' = 3At^2 + 2Bt + C
569 // Z = d - a
570 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
571 //
572 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
573 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
574 SkVector C = (src[1] - src[0]) * 3;
575 SkVector Z = src[3] - src[0];
576 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
577 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
578}
579
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400580class CubicCurve2 : public Sample {
Mike Reed66fa4562018-04-06 19:56:50 -0400581public:
582 enum {
583 N = 7
584 };
585 SkPoint fPts[N];
586 SkPoint* fQuad = fPts + 4;
587 SkScalar fT = 0.5f;
588 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400589 bool fShowFlatness = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400590 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400591
592 CubicCurve2() {
593 fPts[0] = { 90, 300 };
594 fPts[1] = { 30, 60 };
595 fPts[2] = { 250, 30 };
596 fPts[3] = { 350, 200 };
597
598 fQuad[0] = fPts[0] + SkVector{ 300, 0};
599 fQuad[1] = fPts[1] + SkVector{ 300, 0};
600 fQuad[2] = fPts[2] + SkVector{ 300, 0};
601 }
602
603protected:
Hal Canary8a027312019-07-03 10:55:44 -0400604 SkString name() override { return SkString("CubicCurve2"); }
605
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400606 bool onQuery(Sample::Event* evt) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400607 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400608 if (Sample::CharQ(*evt, &uni)) {
Mike Reed66fa4562018-04-06 19:56:50 -0400609 switch (uni) {
610 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400611 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400612 case '-': fT -= 1.0f / 32; break;
613 case '=': fT += 1.0f / 32; break;
614 default: goto DONE;
615 }
616 fT = std::min(1.0f, std::max(0.0f, fT));
617 return true;
618 }
619 DONE:
620 return this->INHERITED::onQuery(evt);
621 }
622
623 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
624 SkPaint paint(p);
625 SkPoint storage[3 + 2 + 1];
626 SkPoint* tmp = storage;
627 const SkPoint* prev = pts;
628 int n = count;
629 for (int n = count; n > 0; --n) {
630 for (int i = 0; i < n; ++i) {
631 canvas->drawLine(prev[i], prev[i+1], paint);
632 tmp[i] = lerp(prev[i], prev[i+1], fT);
633 }
634 prev = tmp;
635 tmp += n;
636 }
637
638 paint.setColor(SK_ColorBLUE);
639 paint.setStyle(SkPaint::kFill_Style);
640 n = tmp - storage;
641 for (int i = 0; i < n; ++i) {
642 canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
643 }
644 }
Hal Canaryd4998172018-07-11 11:31:34 -0400645
Mike Reed48bf6ab2018-04-15 20:04:45 -0400646 void showFlattness(SkCanvas* canvas) {
647 SkPaint paint;
648 paint.setStyle(SkPaint::kStroke_Style);
649 paint.setAntiAlias(true);
650
Mike Reeddccfa352018-04-17 17:10:51 -0400651 SkPaint paint2(paint);
652 paint2.setColor(0xFF008800);
653
Mike Reed48bf6ab2018-04-15 20:04:45 -0400654 paint.setColor(0xFF888888);
655 canvas->drawLine(fPts[0], fPts[3], paint);
656 canvas->drawLine(fQuad[0], fQuad[2], paint);
657
658 paint.setColor(0xFF0000FF);
659 SkPoint pts[2];
660 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
661 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
662 canvas->drawLine(pts[0], pts[1], paint);
663
Mike Reed83dc4732018-04-16 17:07:07 -0400664 // cubic
665
666 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
667 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400668 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400669
670 SkPoint anchor;
671 SkScalar ts[2];
672 int n = find_max_deviation_cubic(fPts, ts);
673 if (n > 0) {
674 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400675 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400676 canvas->drawLine(anchor, anchor + v0, paint);
677 if (n == 2) {
678 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400679 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400680 }
681 canvas->drawLine(anchor, anchor + v1, paint);
682 }
683 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400684 }
Mike Reed66fa4562018-04-06 19:56:50 -0400685
686 void onDrawContent(SkCanvas* canvas) override {
687 SkPaint paint;
688 paint.setAntiAlias(true);
689
690 {
691 paint.setStyle(SkPaint::kStroke_Style);
692 SkPath path;
693 path.moveTo(fPts[0]);
694 path.cubicTo(fPts[1], fPts[2], fPts[3]);
695 path.moveTo(fQuad[0]);
696 path.quadTo(fQuad[1], fQuad[2]);
697 canvas->drawPath(path, paint);
698 }
699
700 if (fShowSub) {
701 paint.setColor(SK_ColorRED);
702 paint.setStrokeWidth(1.7f);
703 this->showFrame(canvas, fPts, 3, paint);
704 this->showFrame(canvas, fQuad, 2, paint);
705
Mike Reed66fa4562018-04-06 19:56:50 -0400706 paint.setColor(SK_ColorBLACK);
707 paint.setStyle(SkPaint::kFill_Style);
Hal Canary4484b8f2019-01-08 14:00:08 -0500708 SkFont font(nullptr, 20);
709 canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
Mike Reed66fa4562018-04-06 19:56:50 -0400710 }
711
Mike Reed48bf6ab2018-04-15 20:04:45 -0400712 if (fShowFlatness) {
713 this->showFlattness(canvas);
714 }
715
Mike Reed66fa4562018-04-06 19:56:50 -0400716 paint.setStyle(SkPaint::kFill_Style);
717 paint.setColor(SK_ColorRED);
718 for (SkPoint p : fPts) {
719 canvas->drawCircle(p.fX, p.fY, 7, paint);
720 }
Mike Reedfb65db12018-04-19 19:18:35 -0400721
722 {
723 SkScalar ts[2];
724 int n = SkFindCubicInflections(fPts, ts);
725 for (int i = 0; i < n; ++i) {
726 SkPoint p;
727 SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
728 canvas->drawCircle(p.fX, p.fY, 3, paint);
729 }
730 }
731
Mike Reed66fa4562018-04-06 19:56:50 -0400732 }
733
734 bool onClick(Click* click) override {
735 int32_t index;
736 if (click->fMeta.findS32("index", &index)) {
737 SkASSERT((unsigned)index < N);
738 fPts[index] = click->fCurr;
739 return true;
740 }
741 return false;
742 }
743
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400744 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
Mike Reed66fa4562018-04-06 19:56:50 -0400745 const SkScalar tol = 8;
746 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
747 for (int i = 0; i < N; ++i) {
748 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
749 Click* click = new Click(this);
750 click->fMeta.setS32("index", i);
751 return click;
752 }
753 }
754 return this->INHERITED::onFindClickHandler(x, y, modi);
755 }
756
757private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400758 typedef Sample INHERITED;
Mike Reed66fa4562018-04-06 19:56:50 -0400759};
760DEF_SAMPLE( return new CubicCurve2; )
761