1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4
5 REG_FIDDLE(strokerect_gm, 1400, 740, false, 0) {
draw(SkCanvas * canvas)6 void draw(SkCanvas* canvas) {
7 constexpr SkScalar kStrokeWidth = 20;
8 constexpr SkPaint::Join gJoins[] = {SkPaint::kMiter_Join, SkPaint::kRound_Join,
9 SkPaint::kBevel_Join};
10 constexpr SkScalar W = 80;
11 constexpr SkScalar H = 80;
12 constexpr SkRect gRects[] = {
13 {0, 0, W, H},
14 {W, 0, 0, H},
15 {0, H, W, 0},
16 {0, 0, kStrokeWidth, H},
17 {0, 0, W, kStrokeWidth},
18 {0, 0, kStrokeWidth / 2, kStrokeWidth / 2},
19 {0, 0, W, 0},
20 {0, 0, 0, H},
21 {0, 0, 0, 0},
22 {0, 0, W, FLT_EPSILON},
23 {0, 0, FLT_EPSILON, H},
24 {0, 0, FLT_EPSILON, FLT_EPSILON},
25 };
26 canvas->translate(kStrokeWidth * 3 / 2, kStrokeWidth * 3 / 2);
27 for (int doFill = 0; doFill <= 1; ++doFill) {
28 SkPaint::Style style = doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style;
29 for (size_t i = 0; i < std::size(gJoins); ++i) {
30 SkPaint::Join join = gJoins[i];
31 for (size_t j = 0; j < std::size(gRects); ++j) {
32 SkAutoCanvasRestore acr(canvas, true);
33 canvas->translate(
34 j * (W + 2 * kStrokeWidth),
35 (i + doFill * std::size(gJoins)) * (H + 2 * kStrokeWidth));
36 const SkRect& rect = gRects[j];
37
38 SkPath path, fillPath;
39 path.addRect(rect);
40 SkPaint paint;
41
42 paint.setStrokeWidth(kStrokeWidth);
43 paint.setStyle(style);
44 paint.setStrokeJoin(join);
45 skpathutils::FillPathWithPaint(path, paint, &fillPath);
46
47 paint.setAntiAlias(true);
48 paint.setColor(0xFF8C8A8C);
49 canvas->drawRect(rect, paint);
50
51 paint.setStyle(SkPaint::kStroke_Style);
52 paint.setStrokeWidth(0);
53 paint.setColor(SK_ColorRED);
54 canvas->drawPath(fillPath, paint);
55
56 paint.setStrokeWidth(3);
57 paint.setStrokeJoin(SkPaint::kMiter_Join);
58 int n = fillPath.countPoints();
59 AutoTArray<SkPoint> points(n);
60 fillPath.getPoints(points.get(), n);
61 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
62 }
63 }
64 }
65 }
66 } // END FIDDLE
67