blob: bd98ad7f5194ac53f282caf511d17c07298892cb [file] [log] [blame]
caryclark88c748a2015-02-18 10:56:00 -08001/*
2 * Copyright 2012 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/SkBlendMode.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPath.h"
16#include "include/core/SkPathMeasure.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRRect.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkShader.h"
23#include "include/core/SkString.h"
24#include "include/core/SkSurface.h"
25#include "include/core/SkTypes.h"
26#include "include/private/SkTArray.h"
27#include "include/private/SkTemplates.h"
28#include "include/utils/SkTextUtils.h"
29#include "samplecode/Sample.h"
30#include "src/core/SkGeometry.h"
Chris Daltonde500372020-05-05 15:06:30 -060031#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/core/SkPointPriv.h"
33#include "src/core/SkStroke.h"
34#include "tools/ToolUtils.h"
Ben Wagnercb3d49c2018-03-14 15:07:43 -040035
36#include <cfloat>
37
38class SkEvent;
caryclark88c748a2015-02-18 10:56:00 -080039
40static bool hittest(const SkPoint& target, SkScalar x, SkScalar y) {
41 const SkScalar TOL = 7;
42 return SkPoint::Distance(target, SkPoint::Make(x, y)) <= TOL;
43}
44
45static int getOnCurvePoints(const SkPath& path, SkPoint storage[]) {
caryclark88c748a2015-02-18 10:56:00 -080046 int count = 0;
Chris Daltonde500372020-05-05 15:06:30 -060047 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
caryclark88c748a2015-02-18 10:56:00 -080048 switch (verb) {
Chris Daltonde500372020-05-05 15:06:30 -060049 case SkPathVerb::kMove:
50 case SkPathVerb::kLine:
51 case SkPathVerb::kQuad:
52 case SkPathVerb::kConic:
53 case SkPathVerb::kCubic:
caryclark88c748a2015-02-18 10:56:00 -080054 storage[count++] = pts[0];
55 break;
56 default:
57 break;
58 }
59 }
60 return count;
61}
62
63static void getContourCounts(const SkPath& path, SkTArray<int>* contourCounts) {
caryclark88c748a2015-02-18 10:56:00 -080064 int count = 0;
Chris Daltonde500372020-05-05 15:06:30 -060065 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
caryclark88c748a2015-02-18 10:56:00 -080066 switch (verb) {
Chris Daltonde500372020-05-05 15:06:30 -060067 case SkPathVerb::kMove:
68 case SkPathVerb::kLine:
caryclark88c748a2015-02-18 10:56:00 -080069 count += 1;
70 break;
Chris Daltonde500372020-05-05 15:06:30 -060071 case SkPathVerb::kQuad:
72 case SkPathVerb::kConic:
caryclark88c748a2015-02-18 10:56:00 -080073 count += 2;
74 break;
Chris Daltonde500372020-05-05 15:06:30 -060075 case SkPathVerb::kCubic:
caryclark88c748a2015-02-18 10:56:00 -080076 count += 3;
77 break;
Chris Daltonde500372020-05-05 15:06:30 -060078 case SkPathVerb::kClose:
caryclark88c748a2015-02-18 10:56:00 -080079 contourCounts->push_back(count);
80 count = 0;
81 break;
82 default:
83 break;
84 }
85 }
86 if (count > 0) {
87 contourCounts->push_back(count);
88 }
89}
90
reede8f30622016-03-23 18:59:25 -070091static void erase(const sk_sp<SkSurface>& surface) {
caryclark88651ae2016-01-20 11:55:11 -080092 SkCanvas* canvas = surface->getCanvas();
93 if (canvas) {
94 canvas->clear(SK_ColorTRANSPARENT);
95 }
caryclark88c748a2015-02-18 10:56:00 -080096}
97
98struct StrokeTypeButton {
99 SkRect fBounds;
100 char fLabel;
101 bool fEnabled;
102};
103
caryclark04e4d082015-02-20 06:33:57 -0800104struct CircleTypeButton : public StrokeTypeButton {
105 bool fFill;
106};
107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108class QuadStrokerView : public Sample {
caryclark88c748a2015-02-18 10:56:00 -0800109 enum {
110 SKELETON_COLOR = 0xFF0000FF,
111 WIREFRAME_COLOR = 0x80FF0000
112 };
113
114 enum {
caryclark88651ae2016-01-20 11:55:11 -0800115 kCount = 18
caryclark88c748a2015-02-18 10:56:00 -0800116 };
117 SkPoint fPts[kCount];
caryclark04e4d082015-02-20 06:33:57 -0800118 SkRect fWeightControl;
caryclark88651ae2016-01-20 11:55:11 -0800119 SkRect fRadiusControl;
caryclark88c748a2015-02-18 10:56:00 -0800120 SkRect fErrorControl;
121 SkRect fWidthControl;
122 SkRect fBounds;
123 SkMatrix fMatrix, fInverse;
reed8a21c9f2016-03-08 18:50:00 -0800124 sk_sp<SkShader> fShader;
reede8f30622016-03-23 18:59:25 -0700125 sk_sp<SkSurface> fMinSurface;
126 sk_sp<SkSurface> fMaxSurface;
caryclark88c748a2015-02-18 10:56:00 -0800127 StrokeTypeButton fCubicButton;
caryclark04e4d082015-02-20 06:33:57 -0800128 StrokeTypeButton fConicButton;
caryclark88c748a2015-02-18 10:56:00 -0800129 StrokeTypeButton fQuadButton;
caryclark88651ae2016-01-20 11:55:11 -0800130 StrokeTypeButton fArcButton;
caryclark88c748a2015-02-18 10:56:00 -0800131 StrokeTypeButton fRRectButton;
caryclark04e4d082015-02-20 06:33:57 -0800132 CircleTypeButton fCircleButton;
caryclark88c748a2015-02-18 10:56:00 -0800133 StrokeTypeButton fTextButton;
134 SkString fText;
135 SkScalar fTextSize;
caryclark04e4d082015-02-20 06:33:57 -0800136 SkScalar fWeight;
caryclark88651ae2016-01-20 11:55:11 -0800137 SkScalar fRadius;
caryclark88c748a2015-02-18 10:56:00 -0800138 SkScalar fWidth, fDWidth;
139 SkScalar fWidthScale;
140 int fW, fH, fZoom;
141 bool fAnimate;
142 bool fDrawRibs;
143 bool fDrawTangents;
caryclarkb6474dd2016-01-19 08:07:49 -0800144 bool fDrawTDivs;
caryclarka76b7a3b2015-05-22 06:26:52 -0700145#ifdef SK_DEBUG
caryclark88c748a2015-02-18 10:56:00 -0800146 #define kStrokerErrorMin 0.001f
147 #define kStrokerErrorMax 5
148#endif
149 #define kWidthMin 1
150 #define kWidthMax 100
151public:
152 QuadStrokerView() {
153 this->setBGColor(SK_ColorLTGRAY);
154
caryclark04e4d082015-02-20 06:33:57 -0800155 fPts[0].set(50, 200); // cubic
caryclark88c748a2015-02-18 10:56:00 -0800156 fPts[1].set(50, 100);
157 fPts[2].set(150, 50);
158 fPts[3].set(300, 50);
159
caryclark04e4d082015-02-20 06:33:57 -0800160 fPts[4].set(350, 200); // conic
caryclark88c748a2015-02-18 10:56:00 -0800161 fPts[5].set(350, 100);
162 fPts[6].set(450, 50);
163
caryclark04e4d082015-02-20 06:33:57 -0800164 fPts[7].set(150, 300); // quad
165 fPts[8].set(150, 200);
166 fPts[9].set(250, 150);
caryclark88c748a2015-02-18 10:56:00 -0800167
caryclark88651ae2016-01-20 11:55:11 -0800168 fPts[10].set(250, 200); // arc
169 fPts[11].set(250, 300);
170 fPts[12].set(150, 350);
caryclark04e4d082015-02-20 06:33:57 -0800171
caryclark88651ae2016-01-20 11:55:11 -0800172 fPts[13].set(200, 200); // rrect
173 fPts[14].set(400, 400);
174
175 fPts[15].set(250, 250); // oval
176 fPts[16].set(450, 450);
caryclark04e4d082015-02-20 06:33:57 -0800177
caryclark88c748a2015-02-18 10:56:00 -0800178 fText = "a";
179 fTextSize = 12;
180 fWidth = 50;
181 fDWidth = 0.25f;
caryclark04e4d082015-02-20 06:33:57 -0800182 fWeight = 1;
caryclark88651ae2016-01-20 11:55:11 -0800183 fRadius = 150;
caryclark88c748a2015-02-18 10:56:00 -0800184
185 fCubicButton.fLabel = 'C';
186 fCubicButton.fEnabled = false;
caryclark04e4d082015-02-20 06:33:57 -0800187 fConicButton.fLabel = 'K';
caryclark88651ae2016-01-20 11:55:11 -0800188 fConicButton.fEnabled = false;
caryclark88c748a2015-02-18 10:56:00 -0800189 fQuadButton.fLabel = 'Q';
190 fQuadButton.fEnabled = false;
caryclark88651ae2016-01-20 11:55:11 -0800191 fArcButton.fLabel = 'A';
192 fArcButton.fEnabled = true;
caryclark88c748a2015-02-18 10:56:00 -0800193 fRRectButton.fLabel = 'R';
194 fRRectButton.fEnabled = false;
caryclark04e4d082015-02-20 06:33:57 -0800195 fCircleButton.fLabel = 'O';
caryclark88651ae2016-01-20 11:55:11 -0800196 fCircleButton.fEnabled = true;
197 fCircleButton.fFill = true;
caryclark88c748a2015-02-18 10:56:00 -0800198 fTextButton.fLabel = 'T';
caryclark04e4d082015-02-20 06:33:57 -0800199 fTextButton.fEnabled = false;
caryclark88651ae2016-01-20 11:55:11 -0800200 fAnimate = false;
caryclark88c748a2015-02-18 10:56:00 -0800201 setAsNeeded();
202 }
203
204protected:
Hal Canary8a027312019-07-03 10:55:44 -0400205 SkString name() override { return SkString("QuadStroker"); }
206
Hal Canary6cc65e12019-07-03 15:53:04 -0400207 bool onChar(SkUnichar uni) override {
208 if (fTextButton.fEnabled) {
caryclark88c748a2015-02-18 10:56:00 -0800209 switch (uni) {
210 case ' ':
211 fText = "";
212 break;
213 case '-':
Brian Osman788b9162020-02-07 10:36:46 -0500214 fTextSize = std::max(1.0f, fTextSize - 1);
caryclark88c748a2015-02-18 10:56:00 -0800215 break;
216 case '+':
217 case '=':
218 fTextSize += 1;
219 break;
220 default:
221 fText.appendUnichar(uni);
222 }
caryclark88c748a2015-02-18 10:56:00 -0800223 return true;
224 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400225 return false;
caryclark88c748a2015-02-18 10:56:00 -0800226 }
227
mtklein36352bf2015-03-25 18:17:31 -0700228 void onSizeChange() override {
caryclark88651ae2016-01-20 11:55:11 -0800229 fRadiusControl.setXYWH(this->width() - 200, 30, 30, 400);
caryclark04e4d082015-02-20 06:33:57 -0800230 fWeightControl.setXYWH(this->width() - 150, 30, 30, 400);
caryclark88c748a2015-02-18 10:56:00 -0800231 fErrorControl.setXYWH(this->width() - 100, 30, 30, 400);
232 fWidthControl.setXYWH(this->width() - 50, 30, 30, 400);
caryclark04e4d082015-02-20 06:33:57 -0800233 int buttonOffset = 450;
234 fCubicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
235 buttonOffset += 50;
236 fConicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
237 buttonOffset += 50;
238 fQuadButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
239 buttonOffset += 50;
caryclark88651ae2016-01-20 11:55:11 -0800240 fArcButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
241 buttonOffset += 50;
caryclark04e4d082015-02-20 06:33:57 -0800242 fRRectButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
243 buttonOffset += 50;
244 fCircleButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
245 buttonOffset += 50;
246 fTextButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
caryclark88c748a2015-02-18 10:56:00 -0800247 this->INHERITED::onSizeChange();
248 }
249
250 void copyMinToMax() {
251 erase(fMaxSurface);
252 SkCanvas* canvas = fMaxSurface->getCanvas();
253 canvas->save();
254 canvas->concat(fMatrix);
Mike Reedb746b1f2021-01-06 08:43:51 -0500255 fMinSurface->draw(canvas, 0, 0);
caryclark88c748a2015-02-18 10:56:00 -0800256 canvas->restore();
257
258 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700259 paint.setBlendMode(SkBlendMode::kClear);
caryclark88c748a2015-02-18 10:56:00 -0800260 for (int iy = 1; iy < fH; ++iy) {
261 SkScalar y = SkIntToScalar(iy * fZoom);
262 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
263 }
264 for (int ix = 1; ix < fW; ++ix) {
265 SkScalar x = SkIntToScalar(ix * fZoom);
266 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
267 }
268 }
269
270 void setWHZ(int width, int height, int zoom) {
271 fZoom = zoom;
Mike Reed92b33352019-08-24 19:39:13 -0400272 fBounds.setIWH(width * zoom, height * zoom);
caryclark88c748a2015-02-18 10:56:00 -0800273 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
274 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
Mike Kleinea3f0142019-03-20 11:12:10 -0500275 fShader = ToolUtils::create_checkerboard_shader(0xFFCCCCCC, 0xFFFFFFFF, zoom);
caryclark88c748a2015-02-18 10:56:00 -0800276
277 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -0700278 fMinSurface = SkSurface::MakeRaster(info);
caryclark88c748a2015-02-18 10:56:00 -0800279 info = info.makeWH(width * zoom, height * zoom);
reede8f30622016-03-23 18:59:25 -0700280 fMaxSurface = SkSurface::MakeRaster(info);
caryclark88c748a2015-02-18 10:56:00 -0800281 }
282
283 void draw_points(SkCanvas* canvas, const SkPath& path, SkColor color,
284 bool show_lines) {
285 SkPaint paint;
286 paint.setColor(color);
287 paint.setAlpha(0x80);
288 paint.setAntiAlias(true);
289 int n = path.countPoints();
290 SkAutoSTArray<32, SkPoint> pts(n);
291 if (show_lines && fDrawTangents) {
292 SkTArray<int> contourCounts;
293 getContourCounts(path, &contourCounts);
294 SkPoint* ptPtr = pts.get();
295 for (int i = 0; i < contourCounts.count(); ++i) {
296 int count = contourCounts[i];
297 path.getPoints(ptPtr, count);
298 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, ptPtr, paint);
299 ptPtr += count;
300 }
301 } else {
302 n = getOnCurvePoints(path, pts.get());
303 }
304 paint.setStrokeWidth(5);
305 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts.get(), paint);
306 }
307
308 void draw_ribs(SkCanvas* canvas, const SkPath& path, SkScalar width,
309 SkColor color) {
310 const SkScalar radius = width / 2;
311
312 SkPathMeasure meas(path, false);
313 SkScalar total = meas.getLength();
314
315 SkScalar delta = 8;
caryclarkb6474dd2016-01-19 08:07:49 -0800316 SkPaint paint, labelP;
caryclark88c748a2015-02-18 10:56:00 -0800317 paint.setColor(color);
caryclarkb6474dd2016-01-19 08:07:49 -0800318 labelP.setColor(color & 0xff5f9f5f);
Hal Canary4484b8f2019-01-08 14:00:08 -0500319 SkFont font;
caryclark88c748a2015-02-18 10:56:00 -0800320 SkPoint pos, tan;
caryclarkb6474dd2016-01-19 08:07:49 -0800321 int index = 0;
caryclark88c748a2015-02-18 10:56:00 -0800322 for (SkScalar dist = 0; dist <= total; dist += delta) {
323 if (meas.getPosTan(dist, &pos, &tan)) {
324 tan.scale(radius);
Cary Clarkdf429f32017-11-08 11:44:31 -0500325 SkPointPriv::RotateCCW(&tan);
caryclark88c748a2015-02-18 10:56:00 -0800326 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
327 pos.x() - tan.x(), pos.y() - tan.y(), paint);
caryclarkb6474dd2016-01-19 08:07:49 -0800328 if (0 == index % 10) {
329 SkString label;
330 label.appendS32(index);
331 SkRect dot = SkRect::MakeXYWH(pos.x() - 2, pos.y() - 2, 4, 4);
332 canvas->drawRect(dot, labelP);
Cary Clark2a475ea2017-04-28 15:35:12 -0400333 canvas->drawString(label,
Hal Canary4484b8f2019-01-08 14:00:08 -0500334 pos.x() - tan.x() * 1.25f, pos.y() - tan.y() * 1.25f, font, labelP);
caryclarkb6474dd2016-01-19 08:07:49 -0800335 }
336 }
337 ++index;
338 }
339 }
340
341 void draw_t_divs(SkCanvas* canvas, const SkPath& path, SkScalar width, SkColor color) {
342 const SkScalar radius = width / 2;
343 SkPaint paint;
344 paint.setColor(color);
345 SkPathMeasure meas(path, false);
346 SkScalar total = meas.getLength();
347 SkScalar delta = 8;
348 int ribs = 0;
349 for (SkScalar dist = 0; dist <= total; dist += delta) {
350 ++ribs;
351 }
Chris Daltonde500372020-05-05 15:06:30 -0600352 const uint8_t* verbs = SkPathPriv::VerbData(path);
353 if (path.countVerbs() < 2 || SkPath::kMove_Verb != verbs[0]) {
caryclarkb6474dd2016-01-19 08:07:49 -0800354 SkASSERT(0);
355 return;
356 }
Chris Daltonde500372020-05-05 15:06:30 -0600357 auto verb = static_cast<SkPath::Verb>(verbs[1]);
caryclarkb6474dd2016-01-19 08:07:49 -0800358 SkASSERT(SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb);
Chris Daltonde500372020-05-05 15:06:30 -0600359 const SkPoint* pts = SkPathPriv::PointData(path);
caryclarkb6474dd2016-01-19 08:07:49 -0800360 SkPoint pos, tan;
361 for (int index = 0; index < ribs; ++index) {
362 SkScalar t = (SkScalar) index / ribs;
363 switch (verb) {
364 case SkPath::kLine_Verb:
365 tan = pts[1] - pts[0];
366 pos = pts[0];
367 pos.fX += tan.fX * t;
368 pos.fY += tan.fY * t;
369 break;
370 case SkPath::kQuad_Verb:
371 pos = SkEvalQuadAt(pts, t);
372 tan = SkEvalQuadTangentAt(pts, t);
373 break;
374 case SkPath::kConic_Verb: {
Chris Daltonde500372020-05-05 15:06:30 -0600375 SkConic conic(pts, SkPathPriv::ConicWeightData(path)[0]);
caryclarkb6474dd2016-01-19 08:07:49 -0800376 pos = conic.evalAt(t);
377 tan = conic.evalTangentAt(t);
378 } break;
379 case SkPath::kCubic_Verb:
380 SkEvalCubicAt(pts, t, &pos, &tan, nullptr);
381 break;
382 default:
383 SkASSERT(0);
384 return;
385 }
386 tan.setLength(radius);
Cary Clarkdf429f32017-11-08 11:44:31 -0500387 SkPointPriv::RotateCCW(&tan);
caryclarkb6474dd2016-01-19 08:07:49 -0800388 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
389 pos.x() - tan.x(), pos.y() - tan.y(), paint);
390 if (0 == index % 10) {
391 SkString label;
392 label.appendS32(index);
Cary Clark2a475ea2017-04-28 15:35:12 -0400393 canvas->drawString(label,
Hal Canary4484b8f2019-01-08 14:00:08 -0500394 pos.x() + tan.x() * 1.25f, pos.y() + tan.y() * 1.25f, SkFont(), paint);
caryclark88c748a2015-02-18 10:56:00 -0800395 }
396 }
397 }
398
caryclark04e4d082015-02-20 06:33:57 -0800399 void draw_stroke(SkCanvas* canvas, const SkPath& path, SkScalar width, SkScalar scale,
400 bool drawText) {
caryclark612f70d2015-05-19 11:05:37 -0700401 if (path.isEmpty()) {
caryclark88c748a2015-02-18 10:56:00 -0800402 return;
403 }
caryclark612f70d2015-05-19 11:05:37 -0700404 SkRect bounds = path.getBounds();
halcanary9d524f22016-03-29 09:03:52 -0700405 this->setWHZ(SkScalarCeilToInt(bounds.right()), drawText
caryclark04e4d082015-02-20 06:33:57 -0800406 ? SkScalarRoundToInt(scale * 3 / 2) : SkScalarRoundToInt(scale),
407 SkScalarRoundToInt(950.0f / scale));
caryclark88c748a2015-02-18 10:56:00 -0800408 erase(fMinSurface);
409 SkPaint paint;
410 paint.setColor(0x1f1f0f0f);
caryclark88c748a2015-02-18 10:56:00 -0800411 paint.setStyle(SkPaint::kStroke_Style);
caryclark04e4d082015-02-20 06:33:57 -0800412 paint.setStrokeWidth(width * scale * scale);
caryclark88c748a2015-02-18 10:56:00 -0800413 paint.setColor(0x3f0f1f3f);
caryclark04e4d082015-02-20 06:33:57 -0800414 if (drawText) {
415 fMinSurface->getCanvas()->drawPath(path, paint);
416 this->copyMinToMax();
Mike Reedb746b1f2021-01-06 08:43:51 -0500417 fMaxSurface->draw(canvas, 0, 0);
caryclark04e4d082015-02-20 06:33:57 -0800418 }
caryclark88c748a2015-02-18 10:56:00 -0800419 paint.setAntiAlias(true);
420 paint.setStyle(SkPaint::kStroke_Style);
421 paint.setStrokeWidth(1);
422
423 paint.setColor(SKELETON_COLOR);
424 SkPath scaled;
425 SkMatrix matrix;
426 matrix.reset();
caryclark04e4d082015-02-20 06:33:57 -0800427 matrix.setScale(950 / scale, 950 / scale);
caryclark88c748a2015-02-18 10:56:00 -0800428 if (drawText) {
429 path.transform(matrix, &scaled);
430 } else {
431 scaled = path;
432 }
433 canvas->drawPath(scaled, paint);
434 draw_points(canvas, scaled, SKELETON_COLOR, true);
435
436 if (fDrawRibs) {
437 draw_ribs(canvas, scaled, width, 0xFF00FF00);
438 }
439
caryclarkb6474dd2016-01-19 08:07:49 -0800440 if (fDrawTDivs) {
441 draw_t_divs(canvas, scaled, width, 0xFF3F3F00);
442 }
443
caryclark88c748a2015-02-18 10:56:00 -0800444 SkPath fill;
445
446 SkPaint p;
447 p.setStyle(SkPaint::kStroke_Style);
caryclark04e4d082015-02-20 06:33:57 -0800448 if (drawText) {
449 p.setStrokeWidth(width * scale * scale);
450 } else {
451 p.setStrokeWidth(width);
452 }
caryclark88c748a2015-02-18 10:56:00 -0800453 p.getFillPath(path, &fill);
454 SkPath scaledFill;
455 if (drawText) {
456 fill.transform(matrix, &scaledFill);
457 } else {
458 scaledFill = fill;
459 }
460 paint.setColor(WIREFRAME_COLOR);
461 canvas->drawPath(scaledFill, paint);
462 draw_points(canvas, scaledFill, WIREFRAME_COLOR, false);
463 }
464
caryclark04e4d082015-02-20 06:33:57 -0800465 void draw_fill(SkCanvas* canvas, const SkRect& rect, SkScalar width) {
466 if (rect.isEmpty()) {
467 return;
468 }
469 SkPaint paint;
470 paint.setColor(0x1f1f0f0f);
471 paint.setStyle(SkPaint::kStroke_Style);
472 paint.setStrokeWidth(width);
473 SkPath path;
Brian Osman788b9162020-02-07 10:36:46 -0500474 SkScalar maxSide = std::max(rect.width(), rect.height()) / 2;
caryclark04e4d082015-02-20 06:33:57 -0800475 SkPoint center = { rect.fLeft + maxSide, rect.fTop + maxSide };
476 path.addCircle(center.fX, center.fY, maxSide);
477 canvas->drawPath(path, paint);
478 paint.setStyle(SkPaint::kFill_Style);
479 path.reset();
480 path.addCircle(center.fX, center.fY, maxSide - width / 2);
481 paint.setColor(0x3f0f1f3f);
482 canvas->drawPath(path, paint);
483 path.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -0500484 path.setFillType(SkPathFillType::kEvenOdd);
caryclark04e4d082015-02-20 06:33:57 -0800485 path.addCircle(center.fX, center.fY, maxSide + width / 2);
halcanary9d524f22016-03-29 09:03:52 -0700486 SkRect outside = SkRect::MakeXYWH(center.fX - maxSide - width, center.fY - maxSide - width,
caryclark04e4d082015-02-20 06:33:57 -0800487 (maxSide + width) * 2, (maxSide + width) * 2);
488 path.addRect(outside);
489 canvas->drawPath(path, paint);
490 }
491
caryclark88c748a2015-02-18 10:56:00 -0800492 void draw_button(SkCanvas* canvas, const StrokeTypeButton& button) {
493 SkPaint paint;
494 paint.setAntiAlias(true);
495 paint.setStyle(SkPaint::kStroke_Style);
496 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
497 canvas->drawRect(button.fBounds, paint);
caryclark88c748a2015-02-18 10:56:00 -0800498 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
caryclark88c748a2015-02-18 10:56:00 -0800499 paint.setStyle(SkPaint::kFill_Style);
Mike Reedb579f072019-01-03 15:45:53 -0500500 SkFont font;
501 font.setSize(25.0f);
Ben Wagner51e15a62019-05-07 15:38:46 -0400502 SkTextUtils::Draw(canvas, &button.fLabel, 1, SkTextEncoding::kUTF8,
Mike Reedb579f072019-01-03 15:45:53 -0500503 button.fBounds.centerX(), button.fBounds.fBottom - 5,
504 font, paint, SkTextUtils::kCenter_Align);
caryclark88c748a2015-02-18 10:56:00 -0800505 }
506
507 void draw_control(SkCanvas* canvas, const SkRect& bounds, SkScalar value,
508 SkScalar min, SkScalar max, const char* name) {
509 SkPaint paint;
510 paint.setAntiAlias(true);
511 paint.setStyle(SkPaint::kStroke_Style);
512 canvas->drawRect(bounds, paint);
513 SkScalar scale = max - min;
514 SkScalar yPos = bounds.fTop + (value - min) * bounds.height() / scale;
515 paint.setColor(0xFFFF0000);
516 canvas->drawLine(bounds.fLeft - 5, yPos, bounds.fRight + 5, yPos, paint);
517 SkString label;
518 label.printf("%0.3g", value);
519 paint.setColor(0xFF000000);
caryclark88c748a2015-02-18 10:56:00 -0800520 paint.setStyle(SkPaint::kFill_Style);
Hal Canary4484b8f2019-01-08 14:00:08 -0500521 SkFont font(nullptr, 11.0f);
522 canvas->drawString(label, bounds.fLeft + 5, yPos - 5, font, paint);
523 font.setSize(13.0f);
524 canvas->drawString(name, bounds.fLeft, bounds.bottom() + 11, font, paint);
caryclark88c748a2015-02-18 10:56:00 -0800525 }
526
527 void setForGeometry() {
528 fDrawRibs = true;
529 fDrawTangents = true;
caryclarkb6474dd2016-01-19 08:07:49 -0800530 fDrawTDivs = false;
caryclark88c748a2015-02-18 10:56:00 -0800531 fWidthScale = 1;
532 }
533
534 void setForText() {
caryclarkb6474dd2016-01-19 08:07:49 -0800535 fDrawRibs = fDrawTangents = fDrawTDivs = false;
caryclark88c748a2015-02-18 10:56:00 -0800536 fWidthScale = 0.002f;
537 }
538
caryclarkb6474dd2016-01-19 08:07:49 -0800539 void setForSingles() {
540 setForGeometry();
541 fDrawTDivs = true;
542 }
543
caryclark88c748a2015-02-18 10:56:00 -0800544 void setAsNeeded() {
caryclarkb6474dd2016-01-19 08:07:49 -0800545 if (fConicButton.fEnabled || fCubicButton.fEnabled || fQuadButton.fEnabled) {
546 setForSingles();
caryclark88651ae2016-01-20 11:55:11 -0800547 } else if (fRRectButton.fEnabled || fCircleButton.fEnabled || fArcButton.fEnabled) {
caryclark88c748a2015-02-18 10:56:00 -0800548 setForGeometry();
549 } else {
550 setForText();
551 }
552 }
553
caryclark88651ae2016-01-20 11:55:11 -0800554 bool arcCenter(SkPoint* center) {
555 SkPath path;
556 path.moveTo(fPts[10]);
557 path.arcTo(fPts[11], fPts[12], fRadius);
558 SkPath::Iter iter(path, false);
559 SkPoint pts[4];
560 iter.next(pts);
561 if (SkPath::kLine_Verb == iter.next(pts)) {
562 iter.next(pts);
563 }
564 SkVector before = pts[0] - pts[1];
565 SkVector after = pts[1] - pts[2];
566 before.setLength(fRadius);
567 after.setLength(fRadius);
568 SkVector beforeCCW, afterCCW;
Cary Clarkdf429f32017-11-08 11:44:31 -0500569 SkPointPriv::RotateCCW(before, &beforeCCW);
570 SkPointPriv::RotateCCW(after, &afterCCW);
caryclark88651ae2016-01-20 11:55:11 -0800571 beforeCCW += pts[0];
572 afterCCW += pts[2];
573 *center = beforeCCW;
574 if (SkScalarNearlyEqual(beforeCCW.fX, afterCCW.fX)
575 && SkScalarNearlyEqual(beforeCCW.fY, afterCCW.fY)) {
576 return true;
577 }
578 SkVector beforeCW, afterCW;
Cary Clarkdf429f32017-11-08 11:44:31 -0500579 SkPointPriv::RotateCW(before, &beforeCW);
580 SkPointPriv::RotateCW(after, &afterCW);
caryclark88651ae2016-01-20 11:55:11 -0800581 beforeCW += pts[0];
582 afterCW += pts[2];
583 *center = beforeCW;
584 return SkScalarNearlyEqual(beforeCW.fX, afterCW.fX)
585 && SkScalarNearlyEqual(beforeCCW.fY, afterCW.fY);
586 }
587
mtklein36352bf2015-03-25 18:17:31 -0700588 void onDrawContent(SkCanvas* canvas) override {
caryclark88c748a2015-02-18 10:56:00 -0800589 SkPath path;
590 SkScalar width = fWidth;
591
592 if (fCubicButton.fEnabled) {
593 path.moveTo(fPts[0]);
594 path.cubicTo(fPts[1], fPts[2], fPts[3]);
caryclarkb6474dd2016-01-19 08:07:49 -0800595 setForSingles();
caryclark04e4d082015-02-20 06:33:57 -0800596 draw_stroke(canvas, path, width, 950, false);
597 }
598
599 if (fConicButton.fEnabled) {
caryclarkb6474dd2016-01-19 08:07:49 -0800600 path.reset();
caryclark04e4d082015-02-20 06:33:57 -0800601 path.moveTo(fPts[4]);
602 path.conicTo(fPts[5], fPts[6], fWeight);
caryclarkb6474dd2016-01-19 08:07:49 -0800603 setForSingles();
caryclark04e4d082015-02-20 06:33:57 -0800604 draw_stroke(canvas, path, width, 950, false);
caryclark88c748a2015-02-18 10:56:00 -0800605 }
606
607 if (fQuadButton.fEnabled) {
608 path.reset();
caryclark04e4d082015-02-20 06:33:57 -0800609 path.moveTo(fPts[7]);
610 path.quadTo(fPts[8], fPts[9]);
caryclarkb6474dd2016-01-19 08:07:49 -0800611 setForSingles();
caryclark04e4d082015-02-20 06:33:57 -0800612 draw_stroke(canvas, path, width, 950, false);
caryclark88c748a2015-02-18 10:56:00 -0800613 }
614
caryclark88651ae2016-01-20 11:55:11 -0800615 if (fArcButton.fEnabled) {
616 path.reset();
617 path.moveTo(fPts[10]);
618 path.arcTo(fPts[11], fPts[12], fRadius);
619 setForGeometry();
620 draw_stroke(canvas, path, width, 950, false);
621 SkPath pathPts;
622 pathPts.moveTo(fPts[10]);
623 pathPts.lineTo(fPts[11]);
624 pathPts.lineTo(fPts[12]);
625 draw_points(canvas, pathPts, SK_ColorDKGRAY, true);
626 }
627
caryclark88c748a2015-02-18 10:56:00 -0800628 if (fRRectButton.fEnabled) {
629 SkScalar rad = 32;
630 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400631 r.setBounds(&fPts[13], 2);
caryclark88c748a2015-02-18 10:56:00 -0800632 path.reset();
633 SkRRect rr;
634 rr.setRectXY(r, rad, rad);
635 path.addRRect(rr);
636 setForGeometry();
caryclark04e4d082015-02-20 06:33:57 -0800637 draw_stroke(canvas, path, width, 950, false);
caryclark88c748a2015-02-18 10:56:00 -0800638
639 path.reset();
640 SkRRect rr2;
641 rr.inset(width/2, width/2, &rr2);
Mike Reed30bc5272019-11-22 18:34:02 +0000642 path.addRRect(rr2, SkPathDirection::kCCW);
caryclark88c748a2015-02-18 10:56:00 -0800643 rr.inset(-width/2, -width/2, &rr2);
Mike Reed30bc5272019-11-22 18:34:02 +0000644 path.addRRect(rr2, SkPathDirection::kCW);
caryclark88c748a2015-02-18 10:56:00 -0800645 SkPaint paint;
646 paint.setAntiAlias(true);
647 paint.setColor(0x40FF8844);
648 canvas->drawPath(path, paint);
649 }
650
caryclark04e4d082015-02-20 06:33:57 -0800651 if (fCircleButton.fEnabled) {
652 path.reset();
653 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400654 r.setBounds(&fPts[15], 2);
caryclark04e4d082015-02-20 06:33:57 -0800655 path.addOval(r);
656 setForGeometry();
657 if (fCircleButton.fFill) {
caryclark88651ae2016-01-20 11:55:11 -0800658 if (fArcButton.fEnabled) {
659 SkPoint center;
660 if (arcCenter(&center)) {
Mike Reed92b33352019-08-24 19:39:13 -0400661 r.setLTRB(center.fX - fRadius, center.fY - fRadius,
662 center.fX + fRadius, center.fY + fRadius);
caryclark88651ae2016-01-20 11:55:11 -0800663 }
664 }
caryclark04e4d082015-02-20 06:33:57 -0800665 draw_fill(canvas, r, width);
666 } else {
667 draw_stroke(canvas, path, width, 950, false);
668 }
669 }
670
caryclark88c748a2015-02-18 10:56:00 -0800671 if (fTextButton.fEnabled) {
672 path.reset();
Mike Reedf78b7ea2018-12-25 22:06:17 -0500673 SkFont font;
674 font.setSize(fTextSize);
Ben Wagner51e15a62019-05-07 15:38:46 -0400675 SkTextUtils::GetPath(fText.c_str(), fText.size(), SkTextEncoding::kUTF8,
Mike Reedf78b7ea2018-12-25 22:06:17 -0500676 0, fTextSize, font, &path);
caryclark88c748a2015-02-18 10:56:00 -0800677 setForText();
caryclark04e4d082015-02-20 06:33:57 -0800678 draw_stroke(canvas, path, width * fWidthScale / fTextSize, fTextSize, true);
caryclark88c748a2015-02-18 10:56:00 -0800679 }
680
681 if (fAnimate) {
682 fWidth += fDWidth;
683 if (fDWidth > 0 && fWidth > kWidthMax) {
684 fDWidth = -fDWidth;
685 } else if (fDWidth < 0 && fWidth < kWidthMin) {
686 fDWidth = -fDWidth;
687 }
688 }
689 setAsNeeded();
caryclark04e4d082015-02-20 06:33:57 -0800690 if (fConicButton.fEnabled) {
691 draw_control(canvas, fWeightControl, fWeight, 0, 5, "weight");
692 }
caryclark88651ae2016-01-20 11:55:11 -0800693 if (fArcButton.fEnabled) {
694 draw_control(canvas, fRadiusControl, fRadius, 0, 500, "radius");
695 }
caryclarka76b7a3b2015-05-22 06:26:52 -0700696#ifdef SK_DEBUG
caryclark88c748a2015-02-18 10:56:00 -0800697 draw_control(canvas, fErrorControl, gDebugStrokerError, kStrokerErrorMin, kStrokerErrorMax,
698 "error");
699#endif
700 draw_control(canvas, fWidthControl, fWidth * fWidthScale, kWidthMin * fWidthScale,
701 kWidthMax * fWidthScale, "width");
702 draw_button(canvas, fQuadButton);
703 draw_button(canvas, fCubicButton);
caryclark04e4d082015-02-20 06:33:57 -0800704 draw_button(canvas, fConicButton);
caryclark88651ae2016-01-20 11:55:11 -0800705 draw_button(canvas, fArcButton);
caryclark88c748a2015-02-18 10:56:00 -0800706 draw_button(canvas, fRRectButton);
caryclark04e4d082015-02-20 06:33:57 -0800707 draw_button(canvas, fCircleButton);
caryclark88c748a2015-02-18 10:56:00 -0800708 draw_button(canvas, fTextButton);
caryclark88c748a2015-02-18 10:56:00 -0800709 }
710
711 class MyClick : public Click {
712 public:
713 int fIndex;
Hal Canaryfcf63592019-07-12 11:32:43 -0400714 MyClick(int index) : fIndex(index) {}
caryclark88c748a2015-02-18 10:56:00 -0800715 };
716
John Stiles1cf2c8d2020-08-13 22:58:04 -0400717 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
caryclark88c748a2015-02-18 10:56:00 -0800718 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); ++i) {
719 if (hittest(fPts[i], x, y)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400720 return new MyClick((int)i);
caryclark88c748a2015-02-18 10:56:00 -0800721 }
722 }
723 const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1);
caryclark04e4d082015-02-20 06:33:57 -0800724 if (fWeightControl.contains(rectPt)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400725 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 1);
caryclark04e4d082015-02-20 06:33:57 -0800726 }
caryclark88651ae2016-01-20 11:55:11 -0800727 if (fRadiusControl.contains(rectPt)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400728 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 2);
caryclark88651ae2016-01-20 11:55:11 -0800729 }
caryclarka76b7a3b2015-05-22 06:26:52 -0700730#ifdef SK_DEBUG
caryclark88c748a2015-02-18 10:56:00 -0800731 if (fErrorControl.contains(rectPt)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400732 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 3);
caryclark88c748a2015-02-18 10:56:00 -0800733 }
734#endif
735 if (fWidthControl.contains(rectPt)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400736 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 4);
caryclark88c748a2015-02-18 10:56:00 -0800737 }
738 if (fCubicButton.fBounds.contains(rectPt)) {
739 fCubicButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400740 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 5);
caryclark88c748a2015-02-18 10:56:00 -0800741 }
caryclark04e4d082015-02-20 06:33:57 -0800742 if (fConicButton.fBounds.contains(rectPt)) {
743 fConicButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400744 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 6);
caryclark04e4d082015-02-20 06:33:57 -0800745 }
caryclark88c748a2015-02-18 10:56:00 -0800746 if (fQuadButton.fBounds.contains(rectPt)) {
747 fQuadButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400748 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 7);
caryclark88651ae2016-01-20 11:55:11 -0800749 }
750 if (fArcButton.fBounds.contains(rectPt)) {
751 fArcButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400752 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 8);
caryclark88c748a2015-02-18 10:56:00 -0800753 }
754 if (fRRectButton.fBounds.contains(rectPt)) {
755 fRRectButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400756 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 9);
caryclark04e4d082015-02-20 06:33:57 -0800757 }
758 if (fCircleButton.fBounds.contains(rectPt)) {
759 bool wasEnabled = fCircleButton.fEnabled;
760 fCircleButton.fEnabled = !fCircleButton.fFill;
761 fCircleButton.fFill = wasEnabled && !fCircleButton.fFill;
Hal Canaryfcf63592019-07-12 11:32:43 -0400762 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 10);
caryclark88c748a2015-02-18 10:56:00 -0800763 }
764 if (fTextButton.fBounds.contains(rectPt)) {
765 fTextButton.fEnabled ^= true;
Hal Canaryfcf63592019-07-12 11:32:43 -0400766 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 11);
caryclark88c748a2015-02-18 10:56:00 -0800767 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400768 return nullptr;
caryclark88c748a2015-02-18 10:56:00 -0800769 }
770
Hal Canaryfcf63592019-07-12 11:32:43 -0400771 static SkScalar MapScreenYtoValue(SkScalar y, const SkRect& control, SkScalar min,
caryclark88c748a2015-02-18 10:56:00 -0800772 SkScalar max) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400773 return (y - control.fTop) / control.height() * (max - min) + min;
caryclark88c748a2015-02-18 10:56:00 -0800774 }
775
mtklein36352bf2015-03-25 18:17:31 -0700776 bool onClick(Click* click) override {
caryclark88c748a2015-02-18 10:56:00 -0800777 int index = ((MyClick*)click)->fIndex;
778 if (index < (int) SK_ARRAY_COUNT(fPts)) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400779 fPts[index].offset(click->fCurr.fX - click->fPrev.fX,
780 click->fCurr.fY - click->fPrev.fY);
caryclark04e4d082015-02-20 06:33:57 -0800781 } else if (index == (int) SK_ARRAY_COUNT(fPts) + 1) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400782 fWeight = MapScreenYtoValue(click->fCurr.fY, fWeightControl, 0, 5);
caryclark88651ae2016-01-20 11:55:11 -0800783 } else if (index == (int) SK_ARRAY_COUNT(fPts) + 2) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400784 fRadius = MapScreenYtoValue(click->fCurr.fY, fRadiusControl, 0, 500);
caryclark88c748a2015-02-18 10:56:00 -0800785 }
caryclarka76b7a3b2015-05-22 06:26:52 -0700786#ifdef SK_DEBUG
caryclark88651ae2016-01-20 11:55:11 -0800787 else if (index == (int) SK_ARRAY_COUNT(fPts) + 3) {
Brian Osman788b9162020-02-07 10:36:46 -0500788 gDebugStrokerError = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY,
caryclark88c748a2015-02-18 10:56:00 -0800789 fErrorControl, kStrokerErrorMin, kStrokerErrorMax));
790 gDebugStrokerErrorSet = true;
791 }
792#endif
caryclark88651ae2016-01-20 11:55:11 -0800793 else if (index == (int) SK_ARRAY_COUNT(fPts) + 4) {
Brian Osman788b9162020-02-07 10:36:46 -0500794 fWidth = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY, fWidthControl,
caryclark88c748a2015-02-18 10:56:00 -0800795 kWidthMin, kWidthMax));
796 fAnimate = fWidth <= kWidthMin;
797 }
798 return true;
799 }
800
801private:
John Stiles7571f9e2020-09-02 22:42:33 -0400802 using INHERITED = Sample;
caryclark88c748a2015-02-18 10:56:00 -0800803};
804
805///////////////////////////////////////////////////////////////////////////////
806
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400807DEF_SAMPLE( return new QuadStrokerView(); )