1 /*
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
8 #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"
31 #include "src/core/SkPathPriv.h"
32 #include "src/core/SkPointPriv.h"
33 #include "src/core/SkStroke.h"
34 #include "tools/ToolUtils.h"
35
36 #include <cfloat>
37
38 class SkEvent;
39
hittest(const SkPoint & target,SkScalar x,SkScalar y)40 static 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
getOnCurvePoints(const SkPath & path,SkPoint storage[])45 static int getOnCurvePoints(const SkPath& path, SkPoint storage[]) {
46 int count = 0;
47 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
48 switch (verb) {
49 case SkPathVerb::kMove:
50 case SkPathVerb::kLine:
51 case SkPathVerb::kQuad:
52 case SkPathVerb::kConic:
53 case SkPathVerb::kCubic:
54 storage[count++] = pts[0];
55 break;
56 default:
57 break;
58 }
59 }
60 return count;
61 }
62
getContourCounts(const SkPath & path,SkTArray<int> * contourCounts)63 static void getContourCounts(const SkPath& path, SkTArray<int>* contourCounts) {
64 int count = 0;
65 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
66 switch (verb) {
67 case SkPathVerb::kMove:
68 case SkPathVerb::kLine:
69 count += 1;
70 break;
71 case SkPathVerb::kQuad:
72 case SkPathVerb::kConic:
73 count += 2;
74 break;
75 case SkPathVerb::kCubic:
76 count += 3;
77 break;
78 case SkPathVerb::kClose:
79 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
erase(const sk_sp<SkSurface> & surface)91 static void erase(const sk_sp<SkSurface>& surface) {
92 SkCanvas* canvas = surface->getCanvas();
93 if (canvas) {
94 canvas->clear(SK_ColorTRANSPARENT);
95 }
96 }
97
98 struct StrokeTypeButton {
99 SkRect fBounds;
100 char fLabel;
101 bool fEnabled;
102 };
103
104 struct CircleTypeButton : public StrokeTypeButton {
105 bool fFill;
106 };
107
108 class QuadStrokerView : public Sample {
109 enum {
110 SKELETON_COLOR = 0xFF0000FF,
111 WIREFRAME_COLOR = 0x80FF0000
112 };
113
114 enum {
115 kCount = 18
116 };
117 SkPoint fPts[kCount];
118 SkRect fWeightControl;
119 SkRect fRadiusControl;
120 SkRect fErrorControl;
121 SkRect fWidthControl;
122 SkRect fBounds;
123 SkMatrix fMatrix, fInverse;
124 sk_sp<SkShader> fShader;
125 sk_sp<SkSurface> fMinSurface;
126 sk_sp<SkSurface> fMaxSurface;
127 StrokeTypeButton fCubicButton;
128 StrokeTypeButton fConicButton;
129 StrokeTypeButton fQuadButton;
130 StrokeTypeButton fArcButton;
131 StrokeTypeButton fRRectButton;
132 CircleTypeButton fCircleButton;
133 StrokeTypeButton fTextButton;
134 SkString fText;
135 SkScalar fTextSize;
136 SkScalar fWeight;
137 SkScalar fRadius;
138 SkScalar fWidth, fDWidth;
139 SkScalar fWidthScale;
140 int fW, fH, fZoom;
141 bool fAnimate;
142 bool fDrawRibs;
143 bool fDrawTangents;
144 bool fDrawTDivs;
145 #ifdef SK_DEBUG
146 #define kStrokerErrorMin 0.001f
147 #define kStrokerErrorMax 5
148 #endif
149 #define kWidthMin 1
150 #define kWidthMax 100
151 public:
QuadStrokerView()152 QuadStrokerView() {
153 this->setBGColor(SK_ColorLTGRAY);
154
155 fPts[0].set(50, 200); // cubic
156 fPts[1].set(50, 100);
157 fPts[2].set(150, 50);
158 fPts[3].set(300, 50);
159
160 fPts[4].set(350, 200); // conic
161 fPts[5].set(350, 100);
162 fPts[6].set(450, 50);
163
164 fPts[7].set(150, 300); // quad
165 fPts[8].set(150, 200);
166 fPts[9].set(250, 150);
167
168 fPts[10].set(250, 200); // arc
169 fPts[11].set(250, 300);
170 fPts[12].set(150, 350);
171
172 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);
177
178 fText = "a";
179 fTextSize = 12;
180 fWidth = 50;
181 fDWidth = 0.25f;
182 fWeight = 1;
183 fRadius = 150;
184
185 fCubicButton.fLabel = 'C';
186 fCubicButton.fEnabled = false;
187 fConicButton.fLabel = 'K';
188 fConicButton.fEnabled = false;
189 fQuadButton.fLabel = 'Q';
190 fQuadButton.fEnabled = false;
191 fArcButton.fLabel = 'A';
192 fArcButton.fEnabled = true;
193 fRRectButton.fLabel = 'R';
194 fRRectButton.fEnabled = false;
195 fCircleButton.fLabel = 'O';
196 fCircleButton.fEnabled = true;
197 fCircleButton.fFill = true;
198 fTextButton.fLabel = 'T';
199 fTextButton.fEnabled = false;
200 fAnimate = false;
201 setAsNeeded();
202 }
203
204 protected:
name()205 SkString name() override { return SkString("QuadStroker"); }
206
onChar(SkUnichar uni)207 bool onChar(SkUnichar uni) override {
208 if (fTextButton.fEnabled) {
209 switch (uni) {
210 case ' ':
211 fText = "";
212 break;
213 case '-':
214 fTextSize = std::max(1.0f, fTextSize - 1);
215 break;
216 case '+':
217 case '=':
218 fTextSize += 1;
219 break;
220 default:
221 fText.appendUnichar(uni);
222 }
223 return true;
224 }
225 return false;
226 }
227
onSizeChange()228 void onSizeChange() override {
229 fRadiusControl.setXYWH(this->width() - 200, 30, 30, 400);
230 fWeightControl.setXYWH(this->width() - 150, 30, 30, 400);
231 fErrorControl.setXYWH(this->width() - 100, 30, 30, 400);
232 fWidthControl.setXYWH(this->width() - 50, 30, 30, 400);
233 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;
240 fArcButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
241 buttonOffset += 50;
242 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);
247 this->INHERITED::onSizeChange();
248 }
249
copyMinToMax()250 void copyMinToMax() {
251 erase(fMaxSurface);
252 SkCanvas* canvas = fMaxSurface->getCanvas();
253 canvas->save();
254 canvas->concat(fMatrix);
255 fMinSurface->draw(canvas, 0, 0);
256 canvas->restore();
257
258 SkPaint paint;
259 paint.setBlendMode(SkBlendMode::kClear);
260 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
setWHZ(int width,int height,int zoom)270 void setWHZ(int width, int height, int zoom) {
271 fZoom = zoom;
272 fBounds.setIWH(width * zoom, height * zoom);
273 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
274 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
275 fShader = ToolUtils::create_checkerboard_shader(0xFFCCCCCC, 0xFFFFFFFF, zoom);
276
277 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
278 fMinSurface = SkSurface::MakeRaster(info);
279 info = info.makeWH(width * zoom, height * zoom);
280 fMaxSurface = SkSurface::MakeRaster(info);
281 }
282
draw_points(SkCanvas * canvas,const SkPath & path,SkColor color,bool show_lines)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
draw_ribs(SkCanvas * canvas,const SkPath & path,SkScalar width,SkColor color)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;
316 SkPaint paint, labelP;
317 paint.setColor(color);
318 labelP.setColor(color & 0xff5f9f5f);
319 SkFont font;
320 SkPoint pos, tan;
321 int index = 0;
322 for (SkScalar dist = 0; dist <= total; dist += delta) {
323 if (meas.getPosTan(dist, &pos, &tan)) {
324 tan.scale(radius);
325 SkPointPriv::RotateCCW(&tan);
326 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
327 pos.x() - tan.x(), pos.y() - tan.y(), paint);
328 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);
333 canvas->drawString(label,
334 pos.x() - tan.x() * 1.25f, pos.y() - tan.y() * 1.25f, font, labelP);
335 }
336 }
337 ++index;
338 }
339 }
340
draw_t_divs(SkCanvas * canvas,const SkPath & path,SkScalar width,SkColor color)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 }
352 const uint8_t* verbs = SkPathPriv::VerbData(path);
353 if (path.countVerbs() < 2 || SkPath::kMove_Verb != verbs[0]) {
354 SkASSERT(0);
355 return;
356 }
357 auto verb = static_cast<SkPath::Verb>(verbs[1]);
358 SkASSERT(SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb);
359 const SkPoint* pts = SkPathPriv::PointData(path);
360 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: {
375 SkConic conic(pts, SkPathPriv::ConicWeightData(path)[0]);
376 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);
387 SkPointPriv::RotateCCW(&tan);
388 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);
393 canvas->drawString(label,
394 pos.x() + tan.x() * 1.25f, pos.y() + tan.y() * 1.25f, SkFont(), paint);
395 }
396 }
397 }
398
draw_stroke(SkCanvas * canvas,const SkPath & path,SkScalar width,SkScalar scale,bool drawText)399 void draw_stroke(SkCanvas* canvas, const SkPath& path, SkScalar width, SkScalar scale,
400 bool drawText) {
401 if (path.isEmpty()) {
402 return;
403 }
404 SkRect bounds = path.getBounds();
405 this->setWHZ(SkScalarCeilToInt(bounds.right()), drawText
406 ? SkScalarRoundToInt(scale * 3 / 2) : SkScalarRoundToInt(scale),
407 SkScalarRoundToInt(950.0f / scale));
408 erase(fMinSurface);
409 SkPaint paint;
410 paint.setColor(0x1f1f0f0f);
411 paint.setStyle(SkPaint::kStroke_Style);
412 paint.setStrokeWidth(width * scale * scale);
413 paint.setColor(0x3f0f1f3f);
414 if (drawText) {
415 fMinSurface->getCanvas()->drawPath(path, paint);
416 this->copyMinToMax();
417 fMaxSurface->draw(canvas, 0, 0);
418 }
419 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();
427 matrix.setScale(950 / scale, 950 / scale);
428 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
440 if (fDrawTDivs) {
441 draw_t_divs(canvas, scaled, width, 0xFF3F3F00);
442 }
443
444 SkPath fill;
445
446 SkPaint p;
447 p.setStyle(SkPaint::kStroke_Style);
448 if (drawText) {
449 p.setStrokeWidth(width * scale * scale);
450 } else {
451 p.setStrokeWidth(width);
452 }
453 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
draw_fill(SkCanvas * canvas,const SkRect & rect,SkScalar width)465 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;
474 SkScalar maxSide = std::max(rect.width(), rect.height()) / 2;
475 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();
484 path.setFillType(SkPathFillType::kEvenOdd);
485 path.addCircle(center.fX, center.fY, maxSide + width / 2);
486 SkRect outside = SkRect::MakeXYWH(center.fX - maxSide - width, center.fY - maxSide - width,
487 (maxSide + width) * 2, (maxSide + width) * 2);
488 path.addRect(outside);
489 canvas->drawPath(path, paint);
490 }
491
draw_button(SkCanvas * canvas,const StrokeTypeButton & button)492 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);
498 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
499 paint.setStyle(SkPaint::kFill_Style);
500 SkFont font;
501 font.setSize(25.0f);
502 SkTextUtils::Draw(canvas, &button.fLabel, 1, SkTextEncoding::kUTF8,
503 button.fBounds.centerX(), button.fBounds.fBottom - 5,
504 font, paint, SkTextUtils::kCenter_Align);
505 }
506
draw_control(SkCanvas * canvas,const SkRect & bounds,SkScalar value,SkScalar min,SkScalar max,const char * name)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);
520 paint.setStyle(SkPaint::kFill_Style);
521 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);
525 }
526
setForGeometry()527 void setForGeometry() {
528 fDrawRibs = true;
529 fDrawTangents = true;
530 fDrawTDivs = false;
531 fWidthScale = 1;
532 }
533
setForText()534 void setForText() {
535 fDrawRibs = fDrawTangents = fDrawTDivs = false;
536 fWidthScale = 0.002f;
537 }
538
setForSingles()539 void setForSingles() {
540 setForGeometry();
541 fDrawTDivs = true;
542 }
543
setAsNeeded()544 void setAsNeeded() {
545 if (fConicButton.fEnabled || fCubicButton.fEnabled || fQuadButton.fEnabled) {
546 setForSingles();
547 } else if (fRRectButton.fEnabled || fCircleButton.fEnabled || fArcButton.fEnabled) {
548 setForGeometry();
549 } else {
550 setForText();
551 }
552 }
553
arcCenter(SkPoint * center)554 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;
569 SkPointPriv::RotateCCW(before, &beforeCCW);
570 SkPointPriv::RotateCCW(after, &afterCCW);
571 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;
579 SkPointPriv::RotateCW(before, &beforeCW);
580 SkPointPriv::RotateCW(after, &afterCW);
581 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
onDrawContent(SkCanvas * canvas)588 void onDrawContent(SkCanvas* canvas) override {
589 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]);
595 setForSingles();
596 draw_stroke(canvas, path, width, 950, false);
597 }
598
599 if (fConicButton.fEnabled) {
600 path.reset();
601 path.moveTo(fPts[4]);
602 path.conicTo(fPts[5], fPts[6], fWeight);
603 setForSingles();
604 draw_stroke(canvas, path, width, 950, false);
605 }
606
607 if (fQuadButton.fEnabled) {
608 path.reset();
609 path.moveTo(fPts[7]);
610 path.quadTo(fPts[8], fPts[9]);
611 setForSingles();
612 draw_stroke(canvas, path, width, 950, false);
613 }
614
615 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
628 if (fRRectButton.fEnabled) {
629 SkScalar rad = 32;
630 SkRect r;
631 r.setBounds(&fPts[13], 2);
632 path.reset();
633 SkRRect rr;
634 rr.setRectXY(r, rad, rad);
635 path.addRRect(rr);
636 setForGeometry();
637 draw_stroke(canvas, path, width, 950, false);
638
639 path.reset();
640 SkRRect rr2;
641 rr.inset(width/2, width/2, &rr2);
642 path.addRRect(rr2, SkPathDirection::kCCW);
643 rr.inset(-width/2, -width/2, &rr2);
644 path.addRRect(rr2, SkPathDirection::kCW);
645 SkPaint paint;
646 paint.setAntiAlias(true);
647 paint.setColor(0x40FF8844);
648 canvas->drawPath(path, paint);
649 }
650
651 if (fCircleButton.fEnabled) {
652 path.reset();
653 SkRect r;
654 r.setBounds(&fPts[15], 2);
655 path.addOval(r);
656 setForGeometry();
657 if (fCircleButton.fFill) {
658 if (fArcButton.fEnabled) {
659 SkPoint center;
660 if (arcCenter(¢er)) {
661 r.setLTRB(center.fX - fRadius, center.fY - fRadius,
662 center.fX + fRadius, center.fY + fRadius);
663 }
664 }
665 draw_fill(canvas, r, width);
666 } else {
667 draw_stroke(canvas, path, width, 950, false);
668 }
669 }
670
671 if (fTextButton.fEnabled) {
672 path.reset();
673 SkFont font;
674 font.setSize(fTextSize);
675 SkTextUtils::GetPath(fText.c_str(), fText.size(), SkTextEncoding::kUTF8,
676 0, fTextSize, font, &path);
677 setForText();
678 draw_stroke(canvas, path, width * fWidthScale / fTextSize, fTextSize, true);
679 }
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();
690 if (fConicButton.fEnabled) {
691 draw_control(canvas, fWeightControl, fWeight, 0, 5, "weight");
692 }
693 if (fArcButton.fEnabled) {
694 draw_control(canvas, fRadiusControl, fRadius, 0, 500, "radius");
695 }
696 #ifdef SK_DEBUG
697 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);
704 draw_button(canvas, fConicButton);
705 draw_button(canvas, fArcButton);
706 draw_button(canvas, fRRectButton);
707 draw_button(canvas, fCircleButton);
708 draw_button(canvas, fTextButton);
709 }
710
711 class MyClick : public Click {
712 public:
713 int fIndex;
MyClick(int index)714 MyClick(int index) : fIndex(index) {}
715 };
716
onFindClickHandler(SkScalar x,SkScalar y,skui::ModifierKey modi)717 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
718 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); ++i) {
719 if (hittest(fPts[i], x, y)) {
720 return new MyClick((int)i);
721 }
722 }
723 const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1);
724 if (fWeightControl.contains(rectPt)) {
725 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 1);
726 }
727 if (fRadiusControl.contains(rectPt)) {
728 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 2);
729 }
730 #ifdef SK_DEBUG
731 if (fErrorControl.contains(rectPt)) {
732 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 3);
733 }
734 #endif
735 if (fWidthControl.contains(rectPt)) {
736 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 4);
737 }
738 if (fCubicButton.fBounds.contains(rectPt)) {
739 fCubicButton.fEnabled ^= true;
740 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 5);
741 }
742 if (fConicButton.fBounds.contains(rectPt)) {
743 fConicButton.fEnabled ^= true;
744 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 6);
745 }
746 if (fQuadButton.fBounds.contains(rectPt)) {
747 fQuadButton.fEnabled ^= true;
748 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 7);
749 }
750 if (fArcButton.fBounds.contains(rectPt)) {
751 fArcButton.fEnabled ^= true;
752 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 8);
753 }
754 if (fRRectButton.fBounds.contains(rectPt)) {
755 fRRectButton.fEnabled ^= true;
756 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 9);
757 }
758 if (fCircleButton.fBounds.contains(rectPt)) {
759 bool wasEnabled = fCircleButton.fEnabled;
760 fCircleButton.fEnabled = !fCircleButton.fFill;
761 fCircleButton.fFill = wasEnabled && !fCircleButton.fFill;
762 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 10);
763 }
764 if (fTextButton.fBounds.contains(rectPt)) {
765 fTextButton.fEnabled ^= true;
766 return new MyClick((int) SK_ARRAY_COUNT(fPts) + 11);
767 }
768 return nullptr;
769 }
770
MapScreenYtoValue(SkScalar y,const SkRect & control,SkScalar min,SkScalar max)771 static SkScalar MapScreenYtoValue(SkScalar y, const SkRect& control, SkScalar min,
772 SkScalar max) {
773 return (y - control.fTop) / control.height() * (max - min) + min;
774 }
775
onClick(Click * click)776 bool onClick(Click* click) override {
777 int index = ((MyClick*)click)->fIndex;
778 if (index < (int) SK_ARRAY_COUNT(fPts)) {
779 fPts[index].offset(click->fCurr.fX - click->fPrev.fX,
780 click->fCurr.fY - click->fPrev.fY);
781 } else if (index == (int) SK_ARRAY_COUNT(fPts) + 1) {
782 fWeight = MapScreenYtoValue(click->fCurr.fY, fWeightControl, 0, 5);
783 } else if (index == (int) SK_ARRAY_COUNT(fPts) + 2) {
784 fRadius = MapScreenYtoValue(click->fCurr.fY, fRadiusControl, 0, 500);
785 }
786 #ifdef SK_DEBUG
787 else if (index == (int) SK_ARRAY_COUNT(fPts) + 3) {
788 gDebugStrokerError = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY,
789 fErrorControl, kStrokerErrorMin, kStrokerErrorMax));
790 gDebugStrokerErrorSet = true;
791 }
792 #endif
793 else if (index == (int) SK_ARRAY_COUNT(fPts) + 4) {
794 fWidth = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY, fWidthControl,
795 kWidthMin, kWidthMax));
796 fAnimate = fWidth <= kWidthMin;
797 }
798 return true;
799 }
800
801 private:
802 using INHERITED = Sample;
803 };
804
805 ///////////////////////////////////////////////////////////////////////////////
806
807 DEF_SAMPLE( return new QuadStrokerView(); )
808