Debian Codegraph @

gm/strokerect.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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathUtils.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkTemplates.h"
21
22 #include <float.h>
23
24 using namespace skia_private;
25
26 #define STROKE_WIDTH SkIntToScalar(20)
27
28 static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
29 SkPaint::Join join, int doFill) {
30 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
33
34 paint.setColor(SK_ColorGRAY);
35 paint.setStrokeWidth(STROKE_WIDTH);
36 paint.setStrokeJoin(join);
37 canvas->drawRect(rect, paint);
38
39 paint.setStyle(SkPaint::kStroke_Style);
40 paint.setStrokeWidth(0);
41 paint.setColor(SK_ColorRED);
42 canvas->drawPath(path, paint);
43
44 paint.setStrokeWidth(3);
45 paint.setStrokeJoin(SkPaint::kMiter_Join);
46 canvas->drawPoints(SkCanvas::kPoints_PointMode, path.points(), paint);
47 }
48
49 /*
50 * Test calling SkStroker for rectangles. Cases to cover:
51 *
52 * geometry: normal, small (smaller than stroke-width), empty, inverted
53 * joint-type for the corners
54 */
55 class StrokeRectGM : public skiagm::GM {
56 public:
57 StrokeRectGM() {}
58
59 protected:
60 SkString getName() const override { return SkString("strokerect"); }
61
62 SkISize getISize() override { return SkISize::Make(1400, 740); }
63
64 void onDraw(SkCanvas* canvas) override {
65 canvas->drawColor(SK_ColorWHITE);
66 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
67
68 SkPaint paint;
69 paint.setStyle(SkPaint::kStroke_Style);
70 paint.setStrokeWidth(STROKE_WIDTH);
71
72 constexpr SkPaint::Join gJoins[] = {
73 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
74 };
75
76 constexpr SkScalar W = 80;
77 constexpr SkScalar H = 80;
78 constexpr SkRect gRects[] = {
79 { 0, 0, W, H },
80 { W, 0, 0, H },
81 { 0, H, W, 0 },
82 { 0, 0, STROKE_WIDTH, H },
83 { 0, 0, W, STROKE_WIDTH },
84 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
85 { 0, 0, W, 0 },
86 { 0, 0, 0, H },
87 { 0, 0, 0, 0 },
88 { 0, 0, W, FLT_EPSILON },
89 { 0, 0, FLT_EPSILON, H },
90 { 0, 0, FLT_EPSILON, FLT_EPSILON },
91 };
92
93 for (int doFill = 0; doFill <= 1; ++doFill) {
94 for (size_t i = 0; i < std::size(gJoins); ++i) {
95 SkPaint::Join join = gJoins[i];
96 paint.setStrokeJoin(join);
97
98 SkAutoCanvasRestore acr(canvas, true);
99 for (size_t j = 0; j < std::size(gRects); ++j) {
100 const SkRect& r = gRects[j];
101
102 SkPath fillPath = skpathutils::FillPathWithPaint(SkPath::Rect(r), paint);
103 draw_path(canvas, fillPath, r, join, doFill);
104
105 canvas->translate(W + 2 * STROKE_WIDTH, 0);
106 }
107 acr.restore();
108 canvas->translate(0, H + 2 * STROKE_WIDTH);
109 }
110 paint.setStyle(SkPaint::kStrokeAndFill_Style);
111 }
112 }
113
114 private:
115 using INHERITED = GM;
116 };
117 DEF_GM(return new StrokeRectGM;)
118
119 ///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 /*
122 * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
123 * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
124 */
125 DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
126 SkPaint p;
127 p.setStyle(SkPaint::kStroke_Style);
128 p.setStrokeWidth(6);
129
130 canvas->scale(10, 1);
131 SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
132 canvas->drawRect(r, p);
133 }