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/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/SkTemplates.h"
20 
21 #include <float.h>
22 
23 #define STROKE_WIDTH    SkIntToScalar(20)
24 
draw_path(SkCanvas * canvas,const SkPath & path,const SkRect & rect,SkPaint::Join join,int doFill)25 static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
26                       SkPaint::Join join, int doFill) {
27     SkPaint paint;
28     paint.setAntiAlias(true);
29     paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
30 
31     paint.setColor(SK_ColorGRAY);
32     paint.setStrokeWidth(STROKE_WIDTH);
33     paint.setStrokeJoin(join);
34     canvas->drawRect(rect, paint);
35 
36     paint.setStyle(SkPaint::kStroke_Style);
37     paint.setStrokeWidth(0);
38     paint.setColor(SK_ColorRED);
39     canvas->drawPath(path, paint);
40 
41     paint.setStrokeWidth(3);
42     paint.setStrokeJoin(SkPaint::kMiter_Join);
43     int n = path.countPoints();
44     SkAutoTArray<SkPoint> points(n);
45     path.getPoints(points.get(), n);
46     canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), 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:
StrokeRectGM()57     StrokeRectGM() {}
58 
59 protected:
60 
onShortName()61     SkString onShortName() override {
62         return SkString("strokerect");
63     }
64 
onISize()65     SkISize onISize() override {
66         return SkISize::Make(1400, 740);
67     }
68 
onDraw(SkCanvas * canvas)69     void onDraw(SkCanvas* canvas) override {
70         canvas->drawColor(SK_ColorWHITE);
71         canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
72 
73         SkPaint paint;
74         paint.setStyle(SkPaint::kStroke_Style);
75         paint.setStrokeWidth(STROKE_WIDTH);
76 
77         constexpr SkPaint::Join gJoins[] = {
78             SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
79         };
80 
81         constexpr SkScalar W = 80;
82         constexpr SkScalar H = 80;
83         constexpr SkRect gRects[] = {
84             { 0, 0, W, H },
85             { W, 0, 0, H },
86             { 0, H, W, 0 },
87             { 0, 0, STROKE_WIDTH, H },
88             { 0, 0, W, STROKE_WIDTH },
89             { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
90             { 0, 0, W, 0 },
91             { 0, 0, 0, H },
92             { 0, 0, 0, 0 },
93             { 0, 0, W, FLT_EPSILON },
94             { 0, 0, FLT_EPSILON, H },
95             { 0, 0, FLT_EPSILON, FLT_EPSILON },
96         };
97 
98         for (int doFill = 0; doFill <= 1; ++doFill) {
99             for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) {
100                 SkPaint::Join join = gJoins[i];
101                 paint.setStrokeJoin(join);
102 
103                 SkAutoCanvasRestore acr(canvas, true);
104                 for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) {
105                     const SkRect& r = gRects[j];
106 
107                     SkPath path, fillPath;
108                     path.addRect(r);
109                     paint.getFillPath(path, &fillPath);
110                     draw_path(canvas, fillPath, r, join, doFill);
111 
112                     canvas->translate(W + 2 * STROKE_WIDTH, 0);
113                 }
114                 acr.restore();
115                 canvas->translate(0, H + 2 * STROKE_WIDTH);
116             }
117             paint.setStyle(SkPaint::kStrokeAndFill_Style);
118         }
119     }
120 
121 private:
122     using INHERITED = GM;
123 };
124 DEF_GM(return new StrokeRectGM;)
125 
126 ///////////////////////////////////////////////////////////////////////////////////////////////////
127 
128 /*
129  *  Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
130  *  non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
131  */
132 DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
133     SkPaint p;
134     p.setStyle(SkPaint::kStroke_Style);
135     p.setStrokeWidth(6);
136 
137     canvas->scale(10, 1);
138     SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
139     canvas->drawRect(r, p);
140 }
141