Debian Codegraph @

tools/viewer/QuadStrokerSlide.cpp

raw
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/SkPathUtils.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRRect.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/private/base/SkTArray.h"
28 #include "include/private/base/SkTemplates.h"
29 #include "include/utils/SkTextUtils.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 #include "tools/fonts/FontToolUtils.h"
36 #include "tools/viewer/ClickHandlerSlide.h"
37
38 #include <cfloat>
39
40 using namespace skia_private;
41
42 class SkEvent;
43
44 static bool hittest(const SkPoint& target, SkScalar x, SkScalar y) {
45 const SkScalar TOL = 7;
46 return SkPoint::Distance(target, SkPoint::Make(x, y)) <= TOL;
47 }
48
49 static int getOnCurvePoints(const SkPath& path, SkPoint storage[]) {
50 int count = 0;
51 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
52 switch (verb) {
53 case SkPathVerb::kMove:
54 case SkPathVerb::kLine:
55 case SkPathVerb::kQuad:
56 case SkPathVerb::kConic:
57 case SkPathVerb::kCubic:
58 storage[count++] = pts[0];
59 break;
60 default:
61 break;
62 }
63 }
64 return count;
65 }
66
67 static void getContourCounts(const SkPath& path, TArray<int>* contourCounts) {
68 int count = 0;
69 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
70 switch (verb) {
71 case SkPathVerb::kMove:
72 case SkPathVerb::kLine:
73 count += 1;
74 break;
75 case SkPathVerb::kQuad:
76 case SkPathVerb::kConic:
77 count += 2;
78 break;
79 case SkPathVerb::kCubic:
80 count += 3;
81 break;
82 case SkPathVerb::kClose:
83 contourCounts->push_back(count);
84 count = 0;
85 break;
86 default:
87 break;
88 }
89 }
90 if (count > 0) {
91 contourCounts->push_back(count);
92 }
93 }
94
95 static void erase(const sk_sp<SkSurface>& surface) {
96 SkCanvas* canvas = surface->getCanvas();
97 if (canvas) {
98 canvas->clear(SK_ColorTRANSPARENT);
99 }
100 }
101
102 struct StrokeTypeButton {
103 SkRect fBounds;
104 char fLabel;
105 bool fEnabled;
106 };
107
108 struct CircleTypeButton : public StrokeTypeButton {
109 bool fFill;
110 };
111
112 class QuadStrokerSlide : public ClickHandlerSlide {
113 enum {
114 SKELETON_COLOR = 0xFF0000FF,
115 WIREFRAME_COLOR = 0x80FF0000
116 };
117
118 enum {
119 kCount = 18
120 };
121 SkPoint fPts[kCount];
122 SkRect fWeightControl;
123 SkRect fRadiusControl;
124 SkRect fErrorControl;
125 SkRect fWidthControl;
126 SkRect fBounds;
127 SkMatrix fMatrix, fInverse;
128 sk_sp<SkShader> fShader;
129 sk_sp<SkSurface> fMinSurface;
130 sk_sp<SkSurface> fMaxSurface;
131 StrokeTypeButton fCubicButton;
132 StrokeTypeButton fConicButton;
133 StrokeTypeButton fQuadButton;
134 StrokeTypeButton fArcButton;
135 StrokeTypeButton fRRectButton;
136 CircleTypeButton fCircleButton;
137 StrokeTypeButton fTextButton;
138 SkString fText;
139 SkScalar fTextSize;
140 SkScalar fWeight;
141 SkScalar fRadius;
142 SkScalar fWidth, fDWidth;
143 SkScalar fWidthScale;
144 int fW, fH, fZoom;
145 bool fAnimate;
146 bool fDrawRibs;
147 bool fDrawTangents;
148 bool fDrawTDivs;
149 #ifdef SK_DEBUG
150 #define kStrokerErrorMin 0.001f
151 #define kStrokerErrorMax 5
152 #endif
153 #define kWidthMin 1
154 #define kWidthMax 100
155 public:
156 QuadStrokerSlide() {
157 fPts[0].set(50, 200); // cubic
158 fPts[1].set(50, 100);
159 fPts[2].set(150, 50);
160 fPts[3].set(300, 50);
161
162 fPts[4].set(350, 200); // conic
163 fPts[5].set(350, 100);
164 fPts[6].set(450, 50);
165
166 fPts[7].set(150, 300); // quad
167 fPts[8].set(150, 200);
168 fPts[9].set(250, 150);
169
170 fPts[10].set(250, 200); // arc
171 fPts[11].set(250, 300);
172 fPts[12].set(150, 350);
173
174 fPts[13].set(200, 200); // rrect
175 fPts[14].set(400, 400);
176
177 fPts[15].set(250, 250); // oval
178 fPts[16].set(450, 450);
179
180 fText = "a";
181 fTextSize = 12;
182 fWidth = 50;
183 fDWidth = 0.25f;
184 fWeight = 1;
185 fRadius = 150;
186
187 fCubicButton.fLabel = 'C';
188 fCubicButton.fEnabled = false;
189 fConicButton.fLabel = 'K';
190 fConicButton.fEnabled = false;
191 fQuadButton.fLabel = 'Q';
192 fQuadButton.fEnabled = false;
193 fArcButton.fLabel = 'A';
194 fArcButton.fEnabled = true;
195 fRRectButton.fLabel = 'R';
196 fRRectButton.fEnabled = false;
197 fCircleButton.fLabel = 'O';
198 fCircleButton.fEnabled = true;
199 fCircleButton.fFill = true;
200 fTextButton.fLabel = 'T';
201 fTextButton.fEnabled = false;
202 fAnimate = false;
203 setAsNeeded();
204 fName = "QuadStroker";
205 }
206
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
228 void load(SkScalar w, SkScalar h) override { this->layout(w, h); }
229
230 void resize(SkScalar w, SkScalar h) override { this->layout(w, h); }
231
232 void draw(SkCanvas* canvas) override {
233 canvas->clear(SK_ColorLTGRAY);
234
235 SkScalar width = fWidth;
236
237 if (fCubicButton.fEnabled) {
238 SkPath path = SkPathBuilder()
239 .moveTo(fPts[0])
240 .cubicTo(fPts[1], fPts[2], fPts[3])
241 .detach();
242 setForSingles();
243 draw_stroke(canvas, path, width, 950, false);
244 }
245
246 if (fConicButton.fEnabled) {
247 SkPath path = SkPathBuilder()
248 .moveTo(fPts[4])
249 .conicTo(fPts[5], fPts[6], fWeight)
250 .detach();
251 setForSingles();
252 draw_stroke(canvas, path, width, 950, false);
253 }
254
255 if (fQuadButton.fEnabled) {
256 SkPath path = SkPathBuilder()
257 .moveTo(fPts[7])
258 .quadTo(fPts[8], fPts[9])
259 .detach();
260 setForSingles();
261 draw_stroke(canvas, path, width, 950, false);
262 }
263
264 if (fArcButton.fEnabled) {
265 SkPath path = SkPathBuilder()
266 .moveTo(fPts[10])
267 .arcTo(fPts[11], fPts[12], fRadius)
268 .detach();
269 setForGeometry();
270 draw_stroke(canvas, path, width, 950, false);
271 SkPath pathPts = SkPath::Polygon({&fPts[10], 3}, false);
272 draw_points(canvas, pathPts, SK_ColorDKGRAY, true);
273 }
274
275 if (fRRectButton.fEnabled) {
276 SkScalar rad = 32;
277 SkRect r = SkRect::BoundsOrEmpty({&fPts[13], 2});
278 SkRRect rr;
279 rr.setRectXY(r, rad, rad);
280 setForGeometry();
281 draw_stroke(canvas, SkPath::RRect(rr), width, 950, false);
282
283 SkPathBuilder builder;
284 SkRRect rr2;
285 rr.inset(width/2, width/2, &rr2);
286 builder.addRRect(rr2, SkPathDirection::kCCW);
287 rr.inset(-width/2, -width/2, &rr2);
288 builder.addRRect(rr2, SkPathDirection::kCW);
289 SkPath path = builder.detach();
290 SkPaint paint;
291 paint.setAntiAlias(true);
292 paint.setColor(0x40FF8844);
293 canvas->drawPath(path, paint);
294 }
295
296 if (fCircleButton.fEnabled) {
297 SkRect r = SkRect::BoundsOrEmpty({&fPts[15], 2});
298 SkPath path = SkPath::Oval(r);
299 setForGeometry();
300 if (fCircleButton.fFill) {
301 if (fArcButton.fEnabled) {
302 SkPoint center;
303 if (arcCenter(&center)) {
304 r.setLTRB(center.fX - fRadius, center.fY - fRadius,
305 center.fX + fRadius, center.fY + fRadius);
306 }
307 }
308 draw_fill(canvas, r, width);
309 } else {
310 draw_stroke(canvas, path, width, 950, false);
311 }
312 }
313
314 if (fTextButton.fEnabled) {
315 SkPath path;
316 SkFont font = ToolUtils::DefaultFont();
317 font.setSize(fTextSize);
318 SkTextUtils::GetPath(fText.c_str(), fText.size(), SkTextEncoding::kUTF8,
319 0, fTextSize, font, &path);
320 setForText();
321 draw_stroke(canvas, path, width * fWidthScale / fTextSize, fTextSize, true);
322 }
323
324 if (fAnimate) {
325 fWidth += fDWidth;
326 if (fDWidth > 0 && fWidth > kWidthMax) {
327 fDWidth = -fDWidth;
328 } else if (fDWidth < 0 && fWidth < kWidthMin) {
329 fDWidth = -fDWidth;
330 }
331 }
332 setAsNeeded();
333 if (fConicButton.fEnabled) {
334 draw_control(canvas, fWeightControl, fWeight, 0, 5, "weight");
335 }
336 if (fArcButton.fEnabled) {
337 draw_control(canvas, fRadiusControl, fRadius, 0, 500, "radius");
338 }
339 #ifdef SK_DEBUG
340 draw_control(canvas, fErrorControl, gDebugStrokerError, kStrokerErrorMin, kStrokerErrorMax,
341 "error");
342 #endif
343 draw_control(canvas, fWidthControl, fWidth * fWidthScale, kWidthMin * fWidthScale,
344 kWidthMax * fWidthScale, "width");
345 draw_button(canvas, fQuadButton);
346 draw_button(canvas, fCubicButton);
347 draw_button(canvas, fConicButton);
348 draw_button(canvas, fArcButton);
349 draw_button(canvas, fRRectButton);
350 draw_button(canvas, fCircleButton);
351 draw_button(canvas, fTextButton);
352 }
353 protected:
354 class MyClick : public Click {
355 public:
356 int fIndex;
357 MyClick(int index) : fIndex(index) {}
358 };
359
360 Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
361 for (size_t i = 0; i < std::size(fPts); ++i) {
362 if (hittest(fPts[i], x, y)) {
363 return new MyClick((int)i);
364 }
365 }
366 const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1);
367 if (fWeightControl.contains(rectPt)) {
368 return new MyClick((int) std::size(fPts) + 1);
369 }
370 if (fRadiusControl.contains(rectPt)) {
371 return new MyClick((int) std::size(fPts) + 2);
372 }
373 #ifdef SK_DEBUG
374 if (fErrorControl.contains(rectPt)) {
375 return new MyClick((int) std::size(fPts) + 3);
376 }
377 #endif
378 if (fWidthControl.contains(rectPt)) {
379 return new MyClick((int) std::size(fPts) + 4);
380 }
381 if (fCubicButton.fBounds.contains(rectPt)) {
382 fCubicButton.fEnabled ^= true;
383 return new MyClick((int) std::size(fPts) + 5);
384 }
385 if (fConicButton.fBounds.contains(rectPt)) {
386 fConicButton.fEnabled ^= true;
387 return new MyClick((int) std::size(fPts) + 6);
388 }
389 if (fQuadButton.fBounds.contains(rectPt)) {
390 fQuadButton.fEnabled ^= true;
391 return new MyClick((int) std::size(fPts) + 7);
392 }
393 if (fArcButton.fBounds.contains(rectPt)) {
394 fArcButton.fEnabled ^= true;
395 return new MyClick((int) std::size(fPts) + 8);
396 }
397 if (fRRectButton.fBounds.contains(rectPt)) {
398 fRRectButton.fEnabled ^= true;
399 return new MyClick((int) std::size(fPts) + 9);
400 }
401 if (fCircleButton.fBounds.contains(rectPt)) {
402 bool wasEnabled = fCircleButton.fEnabled;
403 fCircleButton.fEnabled = !fCircleButton.fFill;
404 fCircleButton.fFill = wasEnabled && !fCircleButton.fFill;
405 return new MyClick((int) std::size(fPts) + 10);
406 }
407 if (fTextButton.fBounds.contains(rectPt)) {
408 fTextButton.fEnabled ^= true;
409 return new MyClick((int) std::size(fPts) + 11);
410 }
411 return nullptr;
412 }
413
414 bool onClick(Click* click) override {
415 int index = ((MyClick*)click)->fIndex;
416 if (index < (int) std::size(fPts)) {
417 fPts[index].offset(click->fCurr.fX - click->fPrev.fX,
418 click->fCurr.fY - click->fPrev.fY);
419 } else if (index == (int) std::size(fPts) + 1) {
420 fWeight = MapScreenYtoValue(click->fCurr.fY, fWeightControl, 0, 5);
421 } else if (index == (int) std::size(fPts) + 2) {
422 fRadius = MapScreenYtoValue(click->fCurr.fY, fRadiusControl, 0, 500);
423 }
424 #ifdef SK_DEBUG
425 else if (index == (int) std::size(fPts) + 3) {
426 gDebugStrokerError = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY,
427 fErrorControl, kStrokerErrorMin, kStrokerErrorMax));
428 gDebugStrokerErrorSet = true;
429 }
430 #endif
431 else if (index == (int) std::size(fPts) + 4) {
432 fWidth = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY, fWidthControl,
433 kWidthMin, kWidthMax));
434 fAnimate = fWidth <= kWidthMin;
435 }
436 return true;
437 }
438
439 private:
440 void layout(SkScalar w, SkScalar h) {
441 fRadiusControl.setXYWH(w - 200, 30, 30, 400);
442 fWeightControl.setXYWH(w - 150, 30, 30, 400);
443 fErrorControl.setXYWH(w - 100, 30, 30, 400);
444 fWidthControl.setXYWH(w - 50, 30, 30, 400);
445 int buttonOffset = 450;
446 fCubicButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
447 buttonOffset += 50;
448 fConicButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
449 buttonOffset += 50;
450 fQuadButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
451 buttonOffset += 50;
452 fArcButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
453 buttonOffset += 50;
454 fRRectButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
455 buttonOffset += 50;
456 fCircleButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
457 buttonOffset += 50;
458 fTextButton.fBounds.setXYWH(w - 50, SkIntToScalar(buttonOffset), 30, 30);
459 }
460
461 void copyMinToMax() {
462 erase(fMaxSurface);
463 SkCanvas* canvas = fMaxSurface->getCanvas();
464 canvas->save();
465 canvas->concat(fMatrix);
466 fMinSurface->draw(canvas, 0, 0);
467 canvas->restore();
468
469 SkPaint paint;
470 paint.setBlendMode(SkBlendMode::kClear);
471 for (int iy = 1; iy < fH; ++iy) {
472 SkScalar y = SkIntToScalar(iy * fZoom);
473 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
474 }
475 for (int ix = 1; ix < fW; ++ix) {
476 SkScalar x = SkIntToScalar(ix * fZoom);
477 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
478 }
479 }
480
481 void setWHZ(int width, int height, int zoom) {
482 fZoom = zoom;
483 fBounds.setIWH(width * zoom, height * zoom);
484 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
485 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
486 fShader = ToolUtils::create_checkerboard_shader(0xFFCCCCCC, 0xFFFFFFFF, zoom);
487
488 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
489 fMinSurface = SkSurfaces::Raster(info);
490 info = info.makeWH(width * zoom, height * zoom);
491 fMaxSurface = SkSurfaces::Raster(info);
492 }
493
494 void draw_points(SkCanvas* canvas, const SkPath& path, SkColor color,
495 bool show_lines) {
496 SkPaint paint;
497 paint.setColor(color);
498 paint.setAlpha(0x80);
499 paint.setAntiAlias(true);
500 size_t n = path.countPoints();
501 std::unique_ptr<SkPoint[]> pts{new SkPoint[n]};
502 if (show_lines && fDrawTangents) {
503 TArray<int> contourCounts;
504 getContourCounts(path, &contourCounts);
505 SkPoint* ptPtr = pts.get();
506 for (int i = 0; i < contourCounts.size(); ++i) {
507 size_t count = contourCounts[i];
508
509 const SkPoint* src = path.points().data();
510 std::copy(src, src + count, ptPtr);
511
512 canvas->drawPoints(SkCanvas::kPolygon_PointMode, {ptPtr, count}, paint);
513 ptPtr += count;
514 }
515 } else {
516 n = getOnCurvePoints(path, pts.get());
517 }
518 paint.setStrokeWidth(5);
519 canvas->drawPoints(SkCanvas::kPoints_PointMode, {pts.get(), n}, paint);
520 }
521
522 void draw_ribs(SkCanvas* canvas, const SkPath& path, SkScalar width,
523 SkColor color) {
524 const SkScalar radius = width / 2;
525
526 SkPathMeasure meas(path, false);
527 SkScalar total = meas.getLength();
528
529 SkScalar delta = 8;
530 SkPaint paint, labelP;
531 paint.setColor(color);
532 labelP.setColor(color & 0xff5f9f5f);
533 SkFont font = ToolUtils::DefaultFont();
534 SkPoint pos, tan;
535 int index = 0;
536 for (SkScalar dist = 0; dist <= total; dist += delta) {
537 if (meas.getPosTan(dist, &pos, &tan)) {
538 tan.scale(radius);
539 SkPointPriv::RotateCCW(&tan);
540 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
541 pos.x() - tan.x(), pos.y() - tan.y(), paint);
542 if (0 == index % 10) {
543 SkString label;
544 label.appendS32(index);
545 SkRect dot = SkRect::MakeXYWH(pos.x() - 2, pos.y() - 2, 4, 4);
546 canvas->drawRect(dot, labelP);
547 canvas->drawString(label,
548 pos.x() - tan.x() * 1.25f, pos.y() - tan.y() * 1.25f, font, labelP);
549 }
550 }
551 ++index;
552 }
553 }
554
555 void draw_t_divs(SkCanvas* canvas, const SkPath& path, SkScalar width, SkColor color) {
556 const SkScalar radius = width / 2;
557 SkPaint paint;
558 paint.setColor(color);
559 SkPathMeasure meas(path, false);
560 SkScalar total = meas.getLength();
561 SkScalar delta = 8;
562 int ribs = 0;
563 for (SkScalar dist = 0; dist <= total; dist += delta) {
564 ++ribs;
565 }
566 SkSpan<const SkPathVerb> verbs = path.verbs();
567 if (verbs.size() < 2 || SkPathVerb::kMove != verbs[0]) {
568 SkASSERT(0);
569 return;
570 }
571 auto verb = static_cast<SkPath::Verb>(verbs[1]);
572 SkASSERT(SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb);
573 SkSpan<const SkPoint> pts = path.points();
574 SkPoint pos, tan;
575 for (int index = 0; index < ribs; ++index) {
576 SkScalar t = (SkScalar) index / ribs;
577 switch (verb) {
578 case SkPath::kLine_Verb:
579 tan = pts[1] - pts[0];
580 pos = pts[0];
581 pos.fX += tan.fX * t;
582 pos.fY += tan.fY * t;
583 break;
584 case SkPath::kQuad_Verb:
585 pos = SkEvalQuadAt(pts.data(), t);
586 tan = SkEvalQuadTangentAt(pts.data(), t);
587 break;
588 case SkPath::kConic_Verb: {
589 SkConic conic(pts.data(), path.conicWeights()[0]);
590 pos = conic.evalAt(t);
591 tan = conic.evalTangentAt(t);
592 } break;
593 case SkPath::kCubic_Verb:
594 SkEvalCubicAt(pts.data(), t, &pos, &tan, nullptr);
595 break;
596 default:
597 SkASSERT(0);
598 return;
599 }
600 tan.setLength(radius);
601 SkPointPriv::RotateCCW(&tan);
602 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
603 pos.x() - tan.x(), pos.y() - tan.y(), paint);
604 if (0 == index % 10) {
605 SkString label;
606 label.appendS32(index);
607 canvas->drawString(label,
608 pos.x() + tan.x() * 1.25f,
609 pos.y() + tan.y() * 1.25f,
610 ToolUtils::DefaultFont(),
611 paint);
612 }
613 }
614 }
615
616 void draw_stroke(SkCanvas* canvas, const SkPath& path, SkScalar width, SkScalar scale,
617 bool drawText) {
618 if (path.isEmpty()) {
619 return;
620 }
621 SkRect bounds = path.getBounds();
622 this->setWHZ(SkScalarCeilToInt(bounds.right()), drawText
623 ? SkScalarRoundToInt(scale * 3 / 2) : SkScalarRoundToInt(scale),
624 SkScalarRoundToInt(950.0f / scale));
625 erase(fMinSurface);
626 SkPaint paint;
627 paint.setColor(0x1f1f0f0f);
628 paint.setStyle(SkPaint::kStroke_Style);
629 paint.setStrokeWidth(width * scale * scale);
630 paint.setColor(0x3f0f1f3f);
631 if (drawText) {
632 fMinSurface->getCanvas()->drawPath(path, paint);
633 this->copyMinToMax();
634 fMaxSurface->draw(canvas, 0, 0);
635 }
636 paint.setAntiAlias(true);
637 paint.setStyle(SkPaint::kStroke_Style);
638 paint.setStrokeWidth(1);
639
640 paint.setColor(SKELETON_COLOR);
641 SkPath scaled;
642 SkMatrix matrix;
643 matrix.reset();
644 matrix.setScale(950 / scale, 950 / scale);
645 if (drawText) {
646 scaled = path.makeTransform(matrix);
647 } else {
648 scaled = path;
649 }
650 canvas->drawPath(scaled, paint);
651 draw_points(canvas, scaled, SKELETON_COLOR, true);
652
653 if (fDrawRibs) {
654 draw_ribs(canvas, scaled, width, 0xFF00FF00);
655 }
656
657 if (fDrawTDivs) {
658 draw_t_divs(canvas, scaled, width, 0xFF3F3F00);
659 }
660
661 SkPaint p;
662 p.setStyle(SkPaint::kStroke_Style);
663 if (drawText) {
664 p.setStrokeWidth(width * scale * scale);
665 } else {
666 p.setStrokeWidth(width);
667 }
668 SkPath fill = skpathutils::FillPathWithPaint(path, p);
669 SkPath scaledFill;
670 if (drawText) {
671 scaledFill = fill.makeTransform(matrix);
672 } else {
673 scaledFill = fill;
674 }
675 paint.setColor(WIREFRAME_COLOR);
676 canvas->drawPath(scaledFill, paint);
677 draw_points(canvas, scaledFill, WIREFRAME_COLOR, false);
678 }
679
680 void draw_fill(SkCanvas* canvas, const SkRect& rect, SkScalar width) {
681 if (rect.isEmpty()) {
682 return;
683 }
684 SkPaint paint;
685 paint.setColor(0x1f1f0f0f);
686 paint.setStyle(SkPaint::kStroke_Style);
687 paint.setStrokeWidth(width);
688 SkScalar maxSide = std::max(rect.width(), rect.height()) / 2;
689 SkPoint center = { rect.fLeft + maxSide, rect.fTop + maxSide };
690 SkPath path = SkPath::Circle(center.fX, center.fY, maxSide);
691 canvas->drawPath(path, paint);
692 paint.setStyle(SkPaint::kFill_Style);
693 path = SkPath::Circle(center.fX, center.fY, maxSide - width / 2);
694 paint.setColor(0x3f0f1f3f);
695 canvas->drawPath(path, paint);
696
697 SkPathBuilder builder(SkPathFillType::kEvenOdd);
698 builder.addCircle(center.fX, center.fY, maxSide + width / 2);
699 SkRect outside = SkRect::MakeXYWH(center.fX - maxSide - width, center.fY - maxSide - width,
700 (maxSide + width) * 2, (maxSide + width) * 2);
701 builder.addRect(outside);
702 canvas->drawPath(builder.detach(), paint);
703 }
704
705 void draw_button(SkCanvas* canvas, const StrokeTypeButton& button) {
706 SkPaint paint;
707 paint.setAntiAlias(true);
708 paint.setStyle(SkPaint::kStroke_Style);
709 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
710 canvas->drawRect(button.fBounds, paint);
711 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
712 paint.setStyle(SkPaint::kFill_Style);
713 SkFont font = ToolUtils::DefaultFont();
714 font.setSize(25.0f);
715 SkTextUtils::Draw(canvas, &button.fLabel, 1, SkTextEncoding::kUTF8,
716 button.fBounds.centerX(), button.fBounds.fBottom - 5,
717 font, paint, SkTextUtils::kCenter_Align);
718 }
719
720 void draw_control(SkCanvas* canvas, const SkRect& bounds, SkScalar value,
721 SkScalar min, SkScalar max, const char* name) {
722 SkPaint paint;
723 paint.setAntiAlias(true);
724 paint.setStyle(SkPaint::kStroke_Style);
725 canvas->drawRect(bounds, paint);
726 SkScalar scale = max - min;
727 SkScalar yPos = bounds.fTop + (value - min) * bounds.height() / scale;
728 paint.setColor(0xFFFF0000);
729 canvas->drawLine(bounds.fLeft - 5, yPos, bounds.fRight + 5, yPos, paint);
730 SkString label;
731 label.printf("%0.3g", value);
732 paint.setColor(0xFF000000);
733 paint.setStyle(SkPaint::kFill_Style);
734 SkFont font(ToolUtils::DefaultTypeface(), 11.0f);
735 canvas->drawString(label, bounds.fLeft + 5, yPos - 5, font, paint);
736 font.setSize(13.0f);
737 canvas->drawString(name, bounds.fLeft, bounds.bottom() + 11, font, paint);
738 }
739
740 void setForGeometry() {
741 fDrawRibs = true;
742 fDrawTangents = true;
743 fDrawTDivs = false;
744 fWidthScale = 1;
745 }
746
747 void setForText() {
748 fDrawRibs = fDrawTangents = fDrawTDivs = false;
749 fWidthScale = 0.002f;
750 }
751
752 void setForSingles() {
753 setForGeometry();
754 fDrawTDivs = true;
755 }
756
757 void setAsNeeded() {
758 if (fConicButton.fEnabled || fCubicButton.fEnabled || fQuadButton.fEnabled) {
759 setForSingles();
760 } else if (fRRectButton.fEnabled || fCircleButton.fEnabled || fArcButton.fEnabled) {
761 setForGeometry();
762 } else {
763 setForText();
764 }
765 }
766
767 bool arcCenter(SkPoint* center) {
768 SkPath path = SkPathBuilder()
769 .moveTo(fPts[10])
770 .arcTo(fPts[11], fPts[12], fRadius)
771 .detach();
772 SkPath::Iter iter(path, false);
773 SkPoint pts[4];
774 iter.next(pts);
775 if (SkPath::kLine_Verb == iter.next(pts)) {
776 iter.next(pts);
777 }
778 SkVector before = pts[0] - pts[1];
779 SkVector after = pts[1] - pts[2];
780 before.setLength(fRadius);
781 after.setLength(fRadius);
782 SkVector beforeCCW, afterCCW;
783 SkPointPriv::RotateCCW(before, &beforeCCW);
784 SkPointPriv::RotateCCW(after, &afterCCW);
785 beforeCCW += pts[0];
786 afterCCW += pts[2];
787 *center = beforeCCW;
788 if (SkScalarNearlyEqual(beforeCCW.fX, afterCCW.fX)
789 && SkScalarNearlyEqual(beforeCCW.fY, afterCCW.fY)) {
790 return true;
791 }
792 SkVector beforeCW, afterCW;
793 SkPointPriv::RotateCW(before, &beforeCW);
794 SkPointPriv::RotateCW(after, &afterCW);
795 beforeCW += pts[0];
796 afterCW += pts[2];
797 *center = beforeCW;
798 return SkScalarNearlyEqual(beforeCW.fX, afterCW.fX)
799 && SkScalarNearlyEqual(beforeCCW.fY, afterCW.fY);
800 }
801
802 static SkScalar MapScreenYtoValue(SkScalar y, const SkRect& control, SkScalar min,
803 SkScalar max) {
804 return (y - control.fTop) / control.height() * (max - min) + min;
805 }
806 };
807
808 ///////////////////////////////////////////////////////////////////////////////
809
810 DEF_SLIDE( return new QuadStrokerSlide(); )