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