2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "sk_tool_utils.h"
9 #include "SampleCode.h"
12 #include "SkPathMeasure.h"
15 #include "SkColorPriv.h"
16 #include "SkStrokerPriv.h"
17 #include "SkSurface.h"
19 static bool hittest(const SkPoint& target, SkScalar x, SkScalar y) {
20 const SkScalar TOL = 7;
21 return SkPoint::Distance(target, SkPoint::Make(x, y)) <= TOL;
24 static int getOnCurvePoints(const SkPath& path, SkPoint storage[]) {
25 SkPath::RawIter iter(path);
30 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
32 case SkPath::kMove_Verb:
33 case SkPath::kLine_Verb:
34 case SkPath::kQuad_Verb:
35 case SkPath::kConic_Verb:
36 case SkPath::kCubic_Verb:
37 storage[count++] = pts[0];
46 static void getContourCounts(const SkPath& path, SkTArray<int>* contourCounts) {
47 SkPath::RawIter iter(path);
52 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
54 case SkPath::kMove_Verb:
55 case SkPath::kLine_Verb:
58 case SkPath::kQuad_Verb:
59 case SkPath::kConic_Verb:
62 case SkPath::kCubic_Verb:
65 case SkPath::kClose_Verb:
66 contourCounts->push_back(count);
74 contourCounts->push_back(count);
78 static void erase(SkSurface* surface) {
79 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
82 struct StrokeTypeButton {
88 struct CircleTypeButton : public StrokeTypeButton {
92 class QuadStrokerView : public SampleView {
94 SKELETON_COLOR = 0xFF0000FF,
95 WIREFRAME_COLOR = 0x80FF0000
101 SkPoint fPts[kCount];
102 SkRect fWeightControl;
103 SkRect fErrorControl;
104 SkRect fWidthControl;
106 SkMatrix fMatrix, fInverse;
107 SkAutoTUnref<SkShader> fShader;
108 SkAutoTUnref<SkSurface> fMinSurface;
109 SkAutoTUnref<SkSurface> fMaxSurface;
110 StrokeTypeButton fCubicButton;
111 StrokeTypeButton fConicButton;
112 StrokeTypeButton fQuadButton;
113 StrokeTypeButton fRRectButton;
114 CircleTypeButton fCircleButton;
115 StrokeTypeButton fTextButton;
119 SkScalar fWidth, fDWidth;
120 SkScalar fWidthScale;
125 #if !defined SK_LEGACY_STROKE_CURVES && defined(SK_DEBUG)
126 #define kStrokerErrorMin 0.001f
127 #define kStrokerErrorMax 5
130 #define kWidthMax 100
133 this->setBGColor(SK_ColorLTGRAY);
135 fPts[0].set(50, 200); // cubic
136 fPts[1].set(50, 100);
137 fPts[2].set(150, 50);
138 fPts[3].set(300, 50);
140 fPts[4].set(350, 200); // conic
141 fPts[5].set(350, 100);
142 fPts[6].set(450, 50);
144 fPts[7].set(150, 300); // quad
145 fPts[8].set(150, 200);
146 fPts[9].set(250, 150);
148 fPts[10].set(200, 200); // rrect
149 fPts[11].set(400, 400);
151 fPts[12].set(250, 250); // oval
152 fPts[13].set(450, 450);
160 fCubicButton.fLabel = 'C';
161 fCubicButton.fEnabled = false;
162 fConicButton.fLabel = 'K';
163 fConicButton.fEnabled = true;
164 fQuadButton.fLabel = 'Q';
165 fQuadButton.fEnabled = false;
166 fRRectButton.fLabel = 'R';
167 fRRectButton.fEnabled = false;
168 fCircleButton.fLabel = 'O';
169 fCircleButton.fEnabled = false;
170 fCircleButton.fFill = false;
171 fTextButton.fLabel = 'T';
172 fTextButton.fEnabled = false;
178 bool onQuery(SkEvent* evt) override {
179 if (SampleCode::TitleQ(*evt)) {
180 SampleCode::TitleR(evt, "QuadStroker");
184 if (fTextButton.fEnabled && SampleCode::CharQ(*evt, &uni)) {
190 fTextSize = SkTMax(1.0f, fTextSize - 1);
197 fText.appendUnichar(uni);
202 return this->INHERITED::onQuery(evt);
205 void onSizeChange() override {
206 fWeightControl.setXYWH(this->width() - 150, 30, 30, 400);
207 fErrorControl.setXYWH(this->width() - 100, 30, 30, 400);
208 fWidthControl.setXYWH(this->width() - 50, 30, 30, 400);
209 int buttonOffset = 450;
210 fCubicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
212 fConicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
214 fQuadButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
216 fRRectButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
218 fCircleButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
220 fTextButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30);
221 this->INHERITED::onSizeChange();
224 void copyMinToMax() {
226 SkCanvas* canvas = fMaxSurface->getCanvas();
228 canvas->concat(fMatrix);
229 fMinSurface->draw(canvas, 0, 0, NULL);
233 paint.setXfermodeMode(SkXfermode::kClear_Mode);
234 for (int iy = 1; iy < fH; ++iy) {
235 SkScalar y = SkIntToScalar(iy * fZoom);
236 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
238 for (int ix = 1; ix < fW; ++ix) {
239 SkScalar x = SkIntToScalar(ix * fZoom);
240 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
244 void setWHZ(int width, int height, int zoom) {
246 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
247 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
248 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
249 fShader.reset(sk_tool_utils::create_checkerboard_shader(
250 0xFFCCCCCC, 0xFFFFFFFF, zoom));
252 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
253 fMinSurface.reset(SkSurface::NewRaster(info));
254 info = info.makeWH(width * zoom, height * zoom);
255 fMaxSurface.reset(SkSurface::NewRaster(info));
258 void draw_points(SkCanvas* canvas, const SkPath& path, SkColor color,
261 paint.setColor(color);
262 paint.setAlpha(0x80);
263 paint.setAntiAlias(true);
264 int n = path.countPoints();
265 SkAutoSTArray<32, SkPoint> pts(n);
266 if (show_lines && fDrawTangents) {
267 SkTArray<int> contourCounts;
268 getContourCounts(path, &contourCounts);
269 SkPoint* ptPtr = pts.get();
270 for (int i = 0; i < contourCounts.count(); ++i) {
271 int count = contourCounts[i];
272 path.getPoints(ptPtr, count);
273 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, ptPtr, paint);
277 n = getOnCurvePoints(path, pts.get());
279 paint.setStrokeWidth(5);
280 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts.get(), paint);
283 void draw_ribs(SkCanvas* canvas, const SkPath& path, SkScalar width,
285 const SkScalar radius = width / 2;
287 SkPathMeasure meas(path, false);
288 SkScalar total = meas.getLength();
292 paint.setColor(color);
295 for (SkScalar dist = 0; dist <= total; dist += delta) {
296 if (meas.getPosTan(dist, &pos, &tan)) {
299 canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(),
300 pos.x() - tan.x(), pos.y() - tan.y(), paint);
305 void draw_stroke(SkCanvas* canvas, const SkPath& path, SkScalar width, SkScalar scale,
307 SkRect bounds = path.getBounds();
308 if (bounds.isEmpty()) {
311 this->setWHZ(SkScalarCeilToInt(bounds.right()), drawText
312 ? SkScalarRoundToInt(scale * 3 / 2) : SkScalarRoundToInt(scale),
313 SkScalarRoundToInt(950.0f / scale));
316 paint.setColor(0x1f1f0f0f);
317 paint.setStyle(SkPaint::kStroke_Style);
318 paint.setStrokeWidth(width * scale * scale);
319 paint.setColor(0x3f0f1f3f);
321 fMinSurface->getCanvas()->drawPath(path, paint);
322 this->copyMinToMax();
323 fMaxSurface->draw(canvas, 0, 0, NULL);
325 paint.setAntiAlias(true);
326 paint.setStyle(SkPaint::kStroke_Style);
327 paint.setStrokeWidth(1);
329 paint.setColor(SKELETON_COLOR);
333 matrix.setScale(950 / scale, 950 / scale);
335 path.transform(matrix, &scaled);
339 canvas->drawPath(scaled, paint);
340 draw_points(canvas, scaled, SKELETON_COLOR, true);
343 draw_ribs(canvas, scaled, width, 0xFF00FF00);
349 p.setStyle(SkPaint::kStroke_Style);
351 p.setStrokeWidth(width * scale * scale);
353 p.setStrokeWidth(width);
355 p.getFillPath(path, &fill);
358 fill.transform(matrix, &scaledFill);
362 paint.setColor(WIREFRAME_COLOR);
363 canvas->drawPath(scaledFill, paint);
364 draw_points(canvas, scaledFill, WIREFRAME_COLOR, false);
367 void draw_fill(SkCanvas* canvas, const SkRect& rect, SkScalar width) {
368 if (rect.isEmpty()) {
372 paint.setColor(0x1f1f0f0f);
373 paint.setStyle(SkPaint::kStroke_Style);
374 paint.setStrokeWidth(width);
376 SkScalar maxSide = SkTMax(rect.width(), rect.height()) / 2;
377 SkPoint center = { rect.fLeft + maxSide, rect.fTop + maxSide };
378 path.addCircle(center.fX, center.fY, maxSide);
379 canvas->drawPath(path, paint);
380 paint.setStyle(SkPaint::kFill_Style);
382 path.addCircle(center.fX, center.fY, maxSide - width / 2);
383 paint.setColor(0x3f0f1f3f);
384 canvas->drawPath(path, paint);
386 path.setFillType(SkPath::kEvenOdd_FillType);
387 path.addCircle(center.fX, center.fY, maxSide + width / 2);
388 SkRect outside = SkRect::MakeXYWH(center.fX - maxSide - width, center.fY - maxSide - width,
389 (maxSide + width) * 2, (maxSide + width) * 2);
390 path.addRect(outside);
391 canvas->drawPath(path, paint);
394 void draw_button(SkCanvas* canvas, const StrokeTypeButton& button) {
396 paint.setAntiAlias(true);
397 paint.setStyle(SkPaint::kStroke_Style);
398 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
399 canvas->drawRect(button.fBounds, paint);
400 paint.setTextSize(25.0f);
401 paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
402 paint.setTextAlign(SkPaint::kCenter_Align);
403 paint.setStyle(SkPaint::kFill_Style);
404 canvas->drawText(&button.fLabel, 1, button.fBounds.centerX(), button.fBounds.fBottom - 5,
408 void draw_control(SkCanvas* canvas, const SkRect& bounds, SkScalar value,
409 SkScalar min, SkScalar max, const char* name) {
411 paint.setAntiAlias(true);
412 paint.setStyle(SkPaint::kStroke_Style);
413 canvas->drawRect(bounds, paint);
414 SkScalar scale = max - min;
415 SkScalar yPos = bounds.fTop + (value - min) * bounds.height() / scale;
416 paint.setColor(0xFFFF0000);
417 canvas->drawLine(bounds.fLeft - 5, yPos, bounds.fRight + 5, yPos, paint);
419 label.printf("%0.3g", value);
420 paint.setColor(0xFF000000);
421 paint.setTextSize(11.0f);
422 paint.setStyle(SkPaint::kFill_Style);
423 canvas->drawText(label.c_str(), label.size(), bounds.fLeft + 5, yPos - 5, paint);
424 paint.setTextSize(13.0f);
425 canvas->drawText(name, strlen(name), bounds.fLeft, bounds.bottom() + 11, paint);
428 void setForGeometry() {
430 fDrawTangents = true;
435 fDrawRibs = fDrawTangents = false;
436 fWidthScale = 0.002f;
440 if (fConicButton.fEnabled || fCubicButton.fEnabled || fQuadButton.fEnabled
441 || fRRectButton.fEnabled || fCircleButton.fEnabled) {
448 void onDrawContent(SkCanvas* canvas) override {
450 SkScalar width = fWidth;
452 if (fCubicButton.fEnabled) {
453 path.moveTo(fPts[0]);
454 path.cubicTo(fPts[1], fPts[2], fPts[3]);
456 draw_stroke(canvas, path, width, 950, false);
459 if (fConicButton.fEnabled) {
460 path.moveTo(fPts[4]);
461 path.conicTo(fPts[5], fPts[6], fWeight);
463 draw_stroke(canvas, path, width, 950, false);
466 if (fQuadButton.fEnabled) {
468 path.moveTo(fPts[7]);
469 path.quadTo(fPts[8], fPts[9]);
471 draw_stroke(canvas, path, width, 950, false);
474 if (fRRectButton.fEnabled) {
480 rr.setRectXY(r, rad, rad);
483 draw_stroke(canvas, path, width, 950, false);
487 rr.inset(width/2, width/2, &rr2);
488 path.addRRect(rr2, SkPath::kCCW_Direction);
489 rr.inset(-width/2, -width/2, &rr2);
490 path.addRRect(rr2, SkPath::kCW_Direction);
492 paint.setAntiAlias(true);
493 paint.setColor(0x40FF8844);
494 canvas->drawPath(path, paint);
497 if (fCircleButton.fEnabled) {
503 if (fCircleButton.fFill) {
504 draw_fill(canvas, r, width);
506 draw_stroke(canvas, path, width, 950, false);
510 if (fTextButton.fEnabled) {
513 paint.setAntiAlias(true);
514 paint.setTextSize(fTextSize);
515 paint.getTextPath(fText.c_str(), fText.size(), 0, fTextSize, &path);
517 draw_stroke(canvas, path, width * fWidthScale / fTextSize, fTextSize, true);
522 if (fDWidth > 0 && fWidth > kWidthMax) {
524 } else if (fDWidth < 0 && fWidth < kWidthMin) {
529 if (fConicButton.fEnabled) {
530 draw_control(canvas, fWeightControl, fWeight, 0, 5, "weight");
532 #if !defined SK_LEGACY_STROKE_CURVES && defined(SK_DEBUG)
533 draw_control(canvas, fErrorControl, gDebugStrokerError, kStrokerErrorMin, kStrokerErrorMax,
536 draw_control(canvas, fWidthControl, fWidth * fWidthScale, kWidthMin * fWidthScale,
537 kWidthMax * fWidthScale, "width");
538 draw_button(canvas, fQuadButton);
539 draw_button(canvas, fCubicButton);
540 draw_button(canvas, fConicButton);
541 draw_button(canvas, fRRectButton);
542 draw_button(canvas, fCircleButton);
543 draw_button(canvas, fTextButton);
547 class MyClick : public Click {
550 MyClick(SkView* target, int index) : Click(target), fIndex(index) {}
553 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
554 unsigned modi) override {
555 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); ++i) {
556 if (hittest(fPts[i], x, y)) {
557 return new MyClick(this, (int)i);
560 const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1);
561 if (fWeightControl.contains(rectPt)) {
562 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 1);
564 #if !defined SK_LEGACY_STROKE_CURVES && defined(SK_DEBUG)
565 if (fErrorControl.contains(rectPt)) {
566 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 2);
569 if (fWidthControl.contains(rectPt)) {
570 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 3);
572 if (fCubicButton.fBounds.contains(rectPt)) {
573 fCubicButton.fEnabled ^= true;
574 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 4);
576 if (fConicButton.fBounds.contains(rectPt)) {
577 fConicButton.fEnabled ^= true;
578 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 5);
580 if (fQuadButton.fBounds.contains(rectPt)) {
581 fQuadButton.fEnabled ^= true;
582 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 6);
584 if (fRRectButton.fBounds.contains(rectPt)) {
585 fRRectButton.fEnabled ^= true;
586 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 7);
588 if (fCircleButton.fBounds.contains(rectPt)) {
589 bool wasEnabled = fCircleButton.fEnabled;
590 fCircleButton.fEnabled = !fCircleButton.fFill;
591 fCircleButton.fFill = wasEnabled && !fCircleButton.fFill;
592 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 8);
594 if (fTextButton.fBounds.contains(rectPt)) {
595 fTextButton.fEnabled ^= true;
596 return new MyClick(this, (int) SK_ARRAY_COUNT(fPts) + 9);
598 return this->INHERITED::onFindClickHandler(x, y, modi);
601 static SkScalar MapScreenYtoValue(int y, const SkRect& control, SkScalar min,
603 return (SkIntToScalar(y) - control.fTop) / control.height() * (max - min) + min;
606 bool onClick(Click* click) override {
607 int index = ((MyClick*)click)->fIndex;
608 if (index < (int) SK_ARRAY_COUNT(fPts)) {
609 fPts[index].offset(SkIntToScalar(click->fICurr.fX - click->fIPrev.fX),
610 SkIntToScalar(click->fICurr.fY - click->fIPrev.fY));
612 } else if (index == (int) SK_ARRAY_COUNT(fPts) + 1) {
613 fWeight = MapScreenYtoValue(click->fICurr.fY, fWeightControl, 0, 5);
615 #if !defined SK_LEGACY_STROKE_CURVES && defined(SK_DEBUG)
616 else if (index == (int) SK_ARRAY_COUNT(fPts) + 2) {
617 gDebugStrokerError = SkTMax(FLT_EPSILON, MapScreenYtoValue(click->fICurr.fY,
618 fErrorControl, kStrokerErrorMin, kStrokerErrorMax));
619 gDebugStrokerErrorSet = true;
622 else if (index == (int) SK_ARRAY_COUNT(fPts) + 3) {
623 fWidth = SkTMax(FLT_EPSILON, MapScreenYtoValue(click->fICurr.fY, fWidthControl,
624 kWidthMin, kWidthMax));
625 fAnimate = fWidth <= kWidthMin;
631 typedef SkView INHERITED;
634 ///////////////////////////////////////////////////////////////////////////////
636 static SkView* F2() { return new QuadStrokerView; }
637 static SkViewRegister gR2(F2);