blob: 7480604ca57e40718fe29c7e72b1e3eeb0180144 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed339cdbf2015-02-05 22:02:37 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
Mike Reed75ae4212018-01-23 11:24:08 -050011#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkCanvas.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
reed@android.comd0d0e652009-10-13 13:31:27 +000021#include "SkParsePath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTime.h"
23#include "SkTypeface.h"
24
reed@android.coma9640282009-08-28 20:06:54 +000025#include "SkGeometry.h"
26
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
reed@google.com0faac1e2011-05-11 05:58:58 +000074class PathView : public SampleView {
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:
133 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700134 bool onQuery(SkEvent* evt) override {
reed@google.com0faac1e2011-05-11 05:58:58 +0000135 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SampleCode::TitleR(evt, "Paths");
137 return true;
138 }
139 return this->INHERITED::onQuery(evt);
140 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@google.com0faac1e2011-05-11 05:58:58 +0000142 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setAntiAlias(true);
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeJoin(j);
reed339cdbf2015-02-05 22:02:37 -0800148 paint.setStrokeWidth(fStroke);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
reed@google.com0faac1e2011-05-11 05:58:58 +0000150 if (fShowHairline) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 SkPath fill;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
153 paint.getFillPath(path, &fill);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 paint.setStrokeWidth(0);
155 canvas->drawPath(fill, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000156 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 canvas->drawPath(path, paint);
reed@google.com0faac1e2011-05-11 05:58:58 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 paint.setColor(SK_ColorRED);
161 paint.setStrokeWidth(0);
162 canvas->drawPath(path, paint);
163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
mtkleinf0599002015-07-13 06:18:39 -0700165 void onDrawContent(SkCanvas* canvas) override {
reed@google.comc9fa63c2012-03-12 21:14:09 +0000166 this->init();
reed339cdbf2015-02-05 22:02:37 -0800167 canvas->translate(50, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168
169 static const SkPaint::Join gJoins[] = {
170 SkPaint::kBevel_Join,
171 SkPaint::kMiter_Join,
172 SkPaint::kRound_Join
173 };
174
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000175 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 canvas->save();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000177 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 this->drawPath(canvas, fPath[j], gJoins[i]);
reed339cdbf2015-02-05 22:02:37 -0800179 canvas->translate(200, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 }
181 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed339cdbf2015-02-05 22:02:37 -0800183 canvas->translate(0, 200);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 }
reed339cdbf2015-02-05 22:02:37 -0800185 }
mtkleinf0599002015-07-13 06:18:39 -0700186
mtklein36352bf2015-03-25 18:17:31 -0700187 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800188 SkScalar currSecs = timer.scaled(100);
189 SkScalar delta = currSecs - fPrevSecs;
190 fPrevSecs = currSecs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed339cdbf2015-02-05 22:02:37 -0800192 fStroke += fDStroke * delta;
193 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
194 fDStroke = -fDStroke;
195 }
196 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 fShowHairline = !fShowHairline;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000201 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000205 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206};
reeda7a8b102014-12-16 08:07:43 -0800207DEF_SAMPLE( return new PathView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
209//////////////////////////////////////////////////////////////////////////////
210
reeda7a8b102014-12-16 08:07:43 -0800211#include "SkCornerPathEffect.h"
212#include "SkRandom.h"
213
214class ArcToView : public SampleView {
Mike Reed40e7e652017-07-22 22:12:59 -0400215 bool fDoFrame, fDoCorner, fDoConic;
216 SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint;
reeda7a8b102014-12-16 08:07:43 -0800217public:
218 enum {
219 N = 4
220 };
221 SkPoint fPts[N];
reeda7a8b102014-12-16 08:07:43 -0800222
reed8b575242014-12-17 01:47:32 -0800223 ArcToView()
Mike Reed40e7e652017-07-22 22:12:59 -0400224 : fDoFrame(false), fDoCorner(false), fDoConic(false)
reed8b575242014-12-17 01:47:32 -0800225 {
reeda7a8b102014-12-16 08:07:43 -0800226 SkRandom rand;
227 for (int i = 0; i < N; ++i) {
228 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
229 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
230 }
mtkleinf0599002015-07-13 06:18:39 -0700231
reed8b575242014-12-17 01:47:32 -0800232 const SkScalar rad = 50;
reeda7a8b102014-12-16 08:07:43 -0800233
234 fPtsPaint.setAntiAlias(true);
235 fPtsPaint.setStrokeWidth(15);
236 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
237
reeda7a8b102014-12-16 08:07:43 -0800238 fCornerPaint.setAntiAlias(true);
239 fCornerPaint.setStyle(SkPaint::kStroke_Style);
240 fCornerPaint.setStrokeWidth(13);
241 fCornerPaint.setColor(SK_ColorGREEN);
reeda4393342016-03-18 11:22:57 -0700242 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
reeda7a8b102014-12-16 08:07:43 -0800243
244 fSkeletonPaint.setAntiAlias(true);
245 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
246 fSkeletonPaint.setColor(SK_ColorRED);
247 }
248
reed8b575242014-12-17 01:47:32 -0800249 void toggle(bool& value) {
250 value = !value;
reed8b575242014-12-17 01:47:32 -0800251 }
252
reeda7a8b102014-12-16 08:07:43 -0800253protected:
254 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700255 bool onQuery(SkEvent* evt) override {
reeda7a8b102014-12-16 08:07:43 -0800256 if (SampleCode::TitleQ(*evt)) {
257 SampleCode::TitleR(evt, "ArcTo");
258 return true;
259 }
reed8b575242014-12-17 01:47:32 -0800260 SkUnichar uni;
261 if (SampleCode::CharQ(*evt, &uni)) {
262 switch (uni) {
263 case '1': this->toggle(fDoFrame); return true;
Mike Reed40e7e652017-07-22 22:12:59 -0400264 case '2': this->toggle(fDoCorner); return true;
265 case '3': this->toggle(fDoConic); return true;
reed8b575242014-12-17 01:47:32 -0800266 default: break;
267 }
268 }
reeda7a8b102014-12-16 08:07:43 -0800269 return this->INHERITED::onQuery(evt);
270 }
mtkleinf0599002015-07-13 06:18:39 -0700271
reed8b575242014-12-17 01:47:32 -0800272 void makePath(SkPath* path) {
273 path->moveTo(fPts[0]);
274 for (int i = 1; i < N; ++i) {
275 path->lineTo(fPts[i]);
276 }
277 if (!fDoFrame) {
278 path->close();
279 }
280 }
reeda7a8b102014-12-16 08:07:43 -0800281
mtklein36352bf2015-03-25 18:17:31 -0700282 void onDrawContent(SkCanvas* canvas) override {
reeda7a8b102014-12-16 08:07:43 -0800283 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
284
285 SkPath path;
reed8b575242014-12-17 01:47:32 -0800286 this->makePath(&path);
reeda7a8b102014-12-16 08:07:43 -0800287
reed8b575242014-12-17 01:47:32 -0800288 if (fDoCorner) {
289 canvas->drawPath(path, fCornerPaint);
reeda7a8b102014-12-16 08:07:43 -0800290 }
reeda7a8b102014-12-16 08:07:43 -0800291
reed8b575242014-12-17 01:47:32 -0800292 canvas->drawPath(path, fSkeletonPaint);
reeda7a8b102014-12-16 08:07:43 -0800293 }
294
mtklein36352bf2015-03-25 18:17:31 -0700295 bool onClick(Click* click) override {
reeda7a8b102014-12-16 08:07:43 -0800296 int32_t index;
297 if (click->fMeta.findS32("index", &index)) {
298 SkASSERT((unsigned)index < N);
299 fPts[index] = click->fCurr;
reeda7a8b102014-12-16 08:07:43 -0800300 return true;
301 }
302 return false;
303 }
304
mtklein36352bf2015-03-25 18:17:31 -0700305 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reeda7a8b102014-12-16 08:07:43 -0800306 const SkScalar tol = 4;
307 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
308 for (int i = 0; i < N; ++i) {
309 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
310 Click* click = new Click(this);
311 click->fMeta.setS32("index", i);
312 return click;
313 }
314 }
315 return this->INHERITED::onFindClickHandler(x, y, modi);
316 }
317
318private:
319 typedef SampleView INHERITED;
320};
321DEF_SAMPLE( return new ArcToView; )
Mike Reeda964a292016-11-02 22:09:25 -0400322
323/////////////
324
325class FatStroke : public SampleView {
326 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
327 int fJoinType, fCapType;
Mike Reed1fba7022016-12-15 10:23:42 -0500328 float fWidth = 30;
Mike Reeda964a292016-11-02 22:09:25 -0400329 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
330public:
331 enum {
332 N = 4
333 };
334 SkPoint fPts[N];
Ben Wagner63fd7602017-10-09 15:45:33 -0400335
Mike Reeda964a292016-11-02 22:09:25 -0400336 FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
337 fJoinType(0), fCapType(0)
338 {
339 SkRandom rand;
340 for (int i = 0; i < N; ++i) {
341 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
342 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
343 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400344
Mike Reeda964a292016-11-02 22:09:25 -0400345 fPtsPaint.setAntiAlias(true);
346 fPtsPaint.setStrokeWidth(10);
347 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
Ben Wagner63fd7602017-10-09 15:45:33 -0400348
Mike Reeda964a292016-11-02 22:09:25 -0400349 fHiddenPaint.setAntiAlias(true);
350 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
351 fHiddenPaint.setColor(0xFF0000FF);
Ben Wagner63fd7602017-10-09 15:45:33 -0400352
Mike Reeda964a292016-11-02 22:09:25 -0400353 fStrokePaint.setAntiAlias(true);
354 fStrokePaint.setStyle(SkPaint::kStroke_Style);
355 fStrokePaint.setStrokeWidth(50);
356 fStrokePaint.setColor(0x8000FF00);
Ben Wagner63fd7602017-10-09 15:45:33 -0400357
Mike Reeda964a292016-11-02 22:09:25 -0400358 fSkeletonPaint.setAntiAlias(true);
359 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
360 fSkeletonPaint.setColor(SK_ColorRED);
361 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400362
Mike Reeda964a292016-11-02 22:09:25 -0400363 void toggle(bool& value) {
364 value = !value;
Mike Reeda964a292016-11-02 22:09:25 -0400365 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400366
Mike Reeda964a292016-11-02 22:09:25 -0400367 void toggle3(int& value) {
368 value = (value + 1) % 3;
Mike Reeda964a292016-11-02 22:09:25 -0400369 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400370
Mike Reeda964a292016-11-02 22:09:25 -0400371protected:
372 // overrides from SkEventSink
373 bool onQuery(SkEvent* evt) override {
374 if (SampleCode::TitleQ(*evt)) {
375 SampleCode::TitleR(evt, "FatStroke");
376 return true;
377 }
378 SkUnichar uni;
379 if (SampleCode::CharQ(*evt, &uni)) {
380 switch (uni) {
381 case '1': this->toggle(fShowSkeleton); return true;
382 case '2': this->toggle(fShowStroke); return true;
383 case '3': this->toggle(fShowHidden); return true;
384 case '4': this->toggle3(fJoinType); return true;
385 case '5': this->toggle3(fCapType); return true;
386 case '6': this->toggle(fClosed); return true;
Brian Osmanede860e2017-11-22 16:36:07 -0500387 case '-': fWidth -= 5; return true;
388 case '=': fWidth += 5; return true;
Mike Reeda964a292016-11-02 22:09:25 -0400389 default: break;
390 }
391 }
392 return this->INHERITED::onQuery(evt);
393 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400394
Mike Reeda964a292016-11-02 22:09:25 -0400395 void makePath(SkPath* path) {
396 path->moveTo(fPts[0]);
397 for (int i = 1; i < N; ++i) {
398 path->lineTo(fPts[i]);
399 }
400 if (fClosed) {
401 path->close();
402 }
403 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400404
Mike Reeda964a292016-11-02 22:09:25 -0400405 void onDrawContent(SkCanvas* canvas) override {
406 canvas->drawColor(0xFFEEEEEE);
407
408 SkPath path;
409 this->makePath(&path);
410
Mike Reed1fba7022016-12-15 10:23:42 -0500411 fStrokePaint.setStrokeWidth(fWidth);
Mike Reeda964a292016-11-02 22:09:25 -0400412 fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
413 fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
414
415 if (fShowStroke) {
416 canvas->drawPath(path, fStrokePaint);
417 }
418 if (fShowHidden) {
419 SkPath hidden;
420 fStrokePaint.getFillPath(path, &hidden);
421 canvas->drawPath(hidden, fHiddenPaint);
422 }
423 if (fShowSkeleton) {
424 canvas->drawPath(path, fSkeletonPaint);
425 }
426 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
427 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400428
Mike Reeda964a292016-11-02 22:09:25 -0400429 bool onClick(Click* click) override {
430 int32_t index;
431 if (click->fMeta.findS32("index", &index)) {
432 SkASSERT((unsigned)index < N);
433 fPts[index] = click->fCurr;
Mike Reeda964a292016-11-02 22:09:25 -0400434 return true;
435 }
436 return false;
437 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400438
Mike Reeda964a292016-11-02 22:09:25 -0400439 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
440 const SkScalar tol = 4;
441 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
442 for (int i = 0; i < N; ++i) {
443 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
444 Click* click = new Click(this);
445 click->fMeta.setS32("index", i);
446 return click;
447 }
448 }
449 return this->INHERITED::onFindClickHandler(x, y, modi);
450 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400451
Mike Reeda964a292016-11-02 22:09:25 -0400452private:
453 typedef SampleView INHERITED;
454};
455DEF_SAMPLE( return new FatStroke; )
Mike Reed66fa4562018-04-06 19:56:50 -0400456
457static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
458 // F = At^3 + Bt^2 + Ct + D
459 SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
460 SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
461 SkVector C = (pts[1] - pts[0]) * 3.0f;
462 SkVector DA = pts[3] - pts[0];
463
464 // F' = 3At^2 + 2Bt + C
465 SkScalar a = 3 * A.cross(DA);
466 SkScalar b = 2 * B.cross(DA);
467 SkScalar c = C.cross(DA);
468
469 int n = SkFindUnitQuadRoots(a, b, c, t);
470 SkString str;
471 for (int i = 0; i < n; ++i) {
472 str.appendf(" %g", t[i]);
473 }
474 SkDebugf("roots %s\n", str.c_str());
475 return n;
476}
477
478class CubicCurve : public SampleView {
479public:
480 enum {
481 N = 4
482 };
483 SkPoint fPts[N];
484
485 CubicCurve() {
486 SkRandom rand;
487 for (int i = 0; i < N; ++i) {
488 fPts[i].fX = 20 + rand.nextUScalar1() * 640;
489 fPts[i].fY = 20 + rand.nextUScalar1() * 480;
490 }
491 }
492
493protected:
494 bool onQuery(SkEvent* evt) override {
495 if (SampleCode::TitleQ(*evt)) {
496 SampleCode::TitleR(evt, "CubicCurve");
497 return true;
498 }
499 return this->INHERITED::onQuery(evt);
500 }
501
502 void onDrawContent(SkCanvas* canvas) override {
503 SkPaint paint;
504 paint.setAntiAlias(true);
505
506 {
507 SkPath path;
508 path.moveTo(fPts[0]);
509 path.cubicTo(fPts[1], fPts[2], fPts[3]);
510 paint.setStyle(SkPaint::kStroke_Style);
511 canvas->drawPath(path, paint);
512 }
513
514 {
515 paint.setColor(SK_ColorRED);
516 SkScalar t[2];
517 int n = compute_parallel_to_base(fPts, t);
518 SkPoint loc;
519 SkVector tan;
520 for (int i = 0; i < n; ++i) {
521 SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
522 tan.setLength(30);
523 canvas->drawLine(loc - tan, loc + tan, paint);
524 }
525 paint.setStrokeWidth(0.5f);
526 canvas->drawLine(fPts[0], fPts[3], paint);
527
528 paint.setColor(SK_ColorBLUE);
529 paint.setStrokeWidth(6);
530 SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
531 canvas->drawPoint(loc, paint);
532
533 paint.setColor(0xFF008800);
534 SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
535 canvas->drawPoint(loc, paint);
536 SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
537 canvas->drawPoint(loc, paint);
538
539 // n = SkFindCubicInflections(fPts, t);
540 // printf("inflections %d %g %g\n", n, t[0], t[1]);
541 }
542
543 {
544 paint.setStyle(SkPaint::kFill_Style);
545 paint.setColor(SK_ColorRED);
546 for (SkPoint p : fPts) {
547 canvas->drawCircle(p.fX, p.fY, 8, paint);
548 }
549 }
550 }
551
552 bool onClick(Click* click) override {
553 int32_t index;
554 if (click->fMeta.findS32("index", &index)) {
555 SkASSERT((unsigned)index < N);
556 fPts[index] = click->fCurr;
557 return true;
558 }
559 return false;
560 }
561
562 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
563 const SkScalar tol = 8;
564 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
565 for (int i = 0; i < N; ++i) {
566 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
567 Click* click = new Click(this);
568 click->fMeta.setS32("index", i);
569 return click;
570 }
571 }
572 return this->INHERITED::onFindClickHandler(x, y, modi);
573 }
574
575private:
576 typedef SampleView INHERITED;
577};
578DEF_SAMPLE( return new CubicCurve; )
579
580static SkPoint lerp(SkPoint a, SkPoint b, float t) {
581 return a * (1 - t) + b * t;
582}
583
Mike Reed83dc4732018-04-16 17:07:07 -0400584static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
585 // deviation = F' x (d - a) == 0, solve for t(s)
586 // F = At^3 + Bt^2 + Ct + D
587 // F' = 3At^2 + 2Bt + C
588 // Z = d - a
589 // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
590 //
591 SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
592 SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
593 SkVector C = (src[1] - src[0]) * 3;
594 SkVector Z = src[3] - src[0];
595 // now forumlate the quadratic coefficients we need to solve for t : F' x Z
596 return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
597}
598
Mike Reed66fa4562018-04-06 19:56:50 -0400599class CubicCurve2 : public SampleView {
600public:
601 enum {
602 N = 7
603 };
604 SkPoint fPts[N];
605 SkPoint* fQuad = fPts + 4;
606 SkScalar fT = 0.5f;
607 bool fShowSub = false;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400608 bool fShowFlatness = false;
Mike Reed83dc4732018-04-16 17:07:07 -0400609 SkScalar fScale = 0.75;
Mike Reed66fa4562018-04-06 19:56:50 -0400610
611 CubicCurve2() {
612 fPts[0] = { 90, 300 };
613 fPts[1] = { 30, 60 };
614 fPts[2] = { 250, 30 };
615 fPts[3] = { 350, 200 };
616
617 fQuad[0] = fPts[0] + SkVector{ 300, 0};
618 fQuad[1] = fPts[1] + SkVector{ 300, 0};
619 fQuad[2] = fPts[2] + SkVector{ 300, 0};
620 }
621
622protected:
623 bool onQuery(SkEvent* evt) override {
624 if (SampleCode::TitleQ(*evt)) {
625 SampleCode::TitleR(evt, "CubicCurve2");
626 return true;
627 }
628 SkUnichar uni;
629 if (SampleCode::CharQ(*evt, &uni)) {
630 switch (uni) {
631 case 's': fShowSub = !fShowSub; break;
Mike Reed48bf6ab2018-04-15 20:04:45 -0400632 case 'f': fShowFlatness = !fShowFlatness; break;
Mike Reed66fa4562018-04-06 19:56:50 -0400633 case '-': fT -= 1.0f / 32; break;
634 case '=': fT += 1.0f / 32; break;
635 default: goto DONE;
636 }
637 fT = std::min(1.0f, std::max(0.0f, fT));
638 return true;
639 }
640 DONE:
641 return this->INHERITED::onQuery(evt);
642 }
643
644 void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
645 SkPaint paint(p);
646 SkPoint storage[3 + 2 + 1];
647 SkPoint* tmp = storage;
648 const SkPoint* prev = pts;
649 int n = count;
650 for (int n = count; n > 0; --n) {
651 for (int i = 0; i < n; ++i) {
652 canvas->drawLine(prev[i], prev[i+1], paint);
653 tmp[i] = lerp(prev[i], prev[i+1], fT);
654 }
655 prev = tmp;
656 tmp += n;
657 }
658
659 paint.setColor(SK_ColorBLUE);
660 paint.setStyle(SkPaint::kFill_Style);
661 n = tmp - storage;
662 for (int i = 0; i < n; ++i) {
663 canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
664 }
665 }
Mike Reed48bf6ab2018-04-15 20:04:45 -0400666
667 void showFlattness(SkCanvas* canvas) {
668 SkPaint paint;
669 paint.setStyle(SkPaint::kStroke_Style);
670 paint.setAntiAlias(true);
671
Mike Reeddccfa352018-04-17 17:10:51 -0400672 SkPaint paint2(paint);
673 paint2.setColor(0xFF008800);
674
Mike Reed48bf6ab2018-04-15 20:04:45 -0400675 paint.setColor(0xFF888888);
676 canvas->drawLine(fPts[0], fPts[3], paint);
677 canvas->drawLine(fQuad[0], fQuad[2], paint);
678
679 paint.setColor(0xFF0000FF);
680 SkPoint pts[2];
681 pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
682 pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
683 canvas->drawLine(pts[0], pts[1], paint);
684
Mike Reed83dc4732018-04-16 17:07:07 -0400685 // cubic
686
687 SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
688 SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
Mike Reeddccfa352018-04-17 17:10:51 -0400689 SkVector v = (v0 + v1) * 0.5f;
Mike Reed83dc4732018-04-16 17:07:07 -0400690
691 SkPoint anchor;
692 SkScalar ts[2];
693 int n = find_max_deviation_cubic(fPts, ts);
694 if (n > 0) {
695 SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400696 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400697 canvas->drawLine(anchor, anchor + v0, paint);
698 if (n == 2) {
699 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
Mike Reeddccfa352018-04-17 17:10:51 -0400700 canvas->drawLine(anchor, anchor + v, paint2);
Mike Reed83dc4732018-04-16 17:07:07 -0400701 }
702 canvas->drawLine(anchor, anchor + v1, paint);
703 }
704 // not sure we can get here
Mike Reed48bf6ab2018-04-15 20:04:45 -0400705 }
Mike Reed66fa4562018-04-06 19:56:50 -0400706
707 void onDrawContent(SkCanvas* canvas) override {
708 SkPaint paint;
709 paint.setAntiAlias(true);
710
711 {
712 paint.setStyle(SkPaint::kStroke_Style);
713 SkPath path;
714 path.moveTo(fPts[0]);
715 path.cubicTo(fPts[1], fPts[2], fPts[3]);
716 path.moveTo(fQuad[0]);
717 path.quadTo(fQuad[1], fQuad[2]);
718 canvas->drawPath(path, paint);
719 }
720
721 if (fShowSub) {
722 paint.setColor(SK_ColorRED);
723 paint.setStrokeWidth(1.7f);
724 this->showFrame(canvas, fPts, 3, paint);
725 this->showFrame(canvas, fQuad, 2, paint);
726
727 SkString str;
728 str.printf("t = %g", fT);
729 paint.setColor(SK_ColorBLACK);
730 paint.setStyle(SkPaint::kFill_Style);
731 paint.setTextSize(20);
732 canvas->drawText(str.c_str(), str.size(), 20, 20, paint);
733 }
734
Mike Reed48bf6ab2018-04-15 20:04:45 -0400735 if (fShowFlatness) {
736 this->showFlattness(canvas);
737 }
738
Mike Reed66fa4562018-04-06 19:56:50 -0400739 paint.setStyle(SkPaint::kFill_Style);
740 paint.setColor(SK_ColorRED);
741 for (SkPoint p : fPts) {
742 canvas->drawCircle(p.fX, p.fY, 7, paint);
743 }
744 }
745
746 bool onClick(Click* click) override {
747 int32_t index;
748 if (click->fMeta.findS32("index", &index)) {
749 SkASSERT((unsigned)index < N);
750 fPts[index] = click->fCurr;
751 return true;
752 }
753 return false;
754 }
755
756 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
757 const SkScalar tol = 8;
758 const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
759 for (int i = 0; i < N; ++i) {
760 if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
761 Click* click = new Click(this);
762 click->fMeta.setS32("index", i);
763 return click;
764 }
765 }
766 return this->INHERITED::onFindClickHandler(x, y, modi);
767 }
768
769private:
770 typedef SampleView INHERITED;
771};
772DEF_SAMPLE( return new CubicCurve2; )
773