xref: /aosp_15_r20/external/skia/docs/examples/strokerect_gm.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include <cfloat>
5 
6 REG_FIDDLE(strokerect_gm, 1400, 740, false, 0) {
draw(SkCanvas * canvas)7 void draw(SkCanvas* canvas) {
8     constexpr SkScalar kStrokeWidth = 20;
9     constexpr SkPaint::Join gJoins[] = {SkPaint::kMiter_Join, SkPaint::kRound_Join,
10                                         SkPaint::kBevel_Join};
11     constexpr SkScalar W = 80;
12     constexpr SkScalar H = 80;
13     constexpr SkRect gRects[] = {
14             {0, 0, W, H},
15             {W, 0, 0, H},
16             {0, H, W, 0},
17             {0, 0, kStrokeWidth, H},
18             {0, 0, W, kStrokeWidth},
19             {0, 0, kStrokeWidth / 2, kStrokeWidth / 2},
20             {0, 0, W, 0},
21             {0, 0, 0, H},
22             {0, 0, 0, 0},
23             {0, 0, W, FLT_EPSILON},
24             {0, 0, FLT_EPSILON, H},
25             {0, 0, FLT_EPSILON, FLT_EPSILON},
26     };
27     canvas->translate(kStrokeWidth * 3 / 2, kStrokeWidth * 3 / 2);
28     for (int doFill = 0; doFill <= 1; ++doFill) {
29         SkPaint::Style style = doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style;
30         for (size_t i = 0; i < std::size(gJoins); ++i) {
31             SkPaint::Join join = gJoins[i];
32             for (size_t j = 0; j < std::size(gRects); ++j) {
33                 SkAutoCanvasRestore acr(canvas, true);
34                 canvas->translate(
35                         j * (W + 2 * kStrokeWidth),
36                         (i + doFill * std::size(gJoins)) * (H + 2 * kStrokeWidth));
37                 const SkRect& rect = gRects[j];
38 
39                 SkPath path, fillPath;
40                 path.addRect(rect);
41                 SkPaint paint;
42 
43                 paint.setStrokeWidth(kStrokeWidth);
44                 paint.setStyle(style);
45                 paint.setStrokeJoin(join);
46                 skpathutils::FillPathWithPaint(path, paint, &fillPath);
47 
48                 paint.setAntiAlias(true);
49                 paint.setColor(0xFF8C8A8C);
50                 canvas->drawRect(rect, paint);
51 
52                 paint.setStyle(SkPaint::kStroke_Style);
53                 paint.setStrokeWidth(0);
54                 paint.setColor(SK_ColorRED);
55                 canvas->drawPath(fillPath, paint);
56 
57                 paint.setStrokeWidth(3);
58                 paint.setStrokeJoin(SkPaint::kMiter_Join);
59                 int n = fillPath.countPoints();
60                 SkPoint* points = new SkPoint[n];
61                 fillPath.getPoints(points, n);
62                 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points, paint);
63                 delete[] points;
64             }
65         }
66     }
67 }
68 }  // END FIDDLE
69