• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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.h"
9 #include "SkCanvas.h"
10 #include "SkPath.h"
11 
12 namespace skiagm {
13 
14 // This GM tests a grab-bag of non-closed paths. All these paths look like
15 // closed rects, but they don't call path.close(). Depending on the stroke
16 // settings these slightly different paths give widely different results.
17 class NonClosedPathsGM: public GM {
18 public:
NonClosedPathsGM()19     NonClosedPathsGM() {}
20 
21     enum ClosureType {
22         TotallyNonClosed,  // The last point doesn't coincide with the first one in the contour.
23                            // The path looks not closed at all.
24 
25         FakeCloseCorner,   // The last point coincides with the first one at a corner.
26                            // The path looks closed, but final rendering has 2 ends with cap.
27 
28         FakeCloseMiddle,   // The last point coincides with the first one in the middle of a line.
29                            // The path looks closed, and the final rendering looks closed too.
30 
31         kClosureTypeCount
32     };
33 
34 protected:
35 
onShortName()36     SkString onShortName() override {
37         return SkString("nonclosedpaths");
38     }
39 
40     // 12 * 18 + 3 cases, every case is 100 * 100 pixels.
onISize()41     SkISize onISize() override {
42         return SkISize::Make(1220, 1920);
43     }
44 
45     // Use rect-like geometry for non-closed path, for right angles make it
46     // easier to show the visual difference of lineCap and lineJoin.
MakePath(SkPath * path,ClosureType type)47     static void MakePath(SkPath* path, ClosureType type) {
48         if (FakeCloseMiddle == type) {
49             path->moveTo(30, 50);
50             path->lineTo(30, 30);
51         } else {
52             path->moveTo(30, 30);
53         }
54         path->lineTo(70, 30);
55         path->lineTo(70, 70);
56         path->lineTo(30, 70);
57         path->lineTo(30, 50);
58         if (FakeCloseCorner == type) {
59             path->lineTo(30, 30);
60         }
61     }
62 
63     // Set the location for the current test on the canvas
SetLocation(SkCanvas * canvas,int counter,int lineNum)64     static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
65         SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4;
66         SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4;
67         canvas->translate(x, y);
68     }
69 
onDraw(SkCanvas * canvas)70     void onDraw(SkCanvas* canvas) override {
71         // Stroke widths are:
72         // 0(may use hairline rendering), 10(common case for stroke-style)
73         // 40 and 50(>= geometry width/height, make the contour filled in fact)
74         constexpr int kStrokeWidth[] = {0, 10, 40, 50};
75         int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
76 
77         constexpr SkPaint::Style kStyle[] = {
78             SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
79         };
80 
81         constexpr SkPaint::Cap kCap[] = {
82             SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
83         };
84 
85         constexpr SkPaint::Join kJoin[] = {
86             SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
87         };
88 
89         constexpr ClosureType kType[] = {
90             TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle
91         };
92 
93         int counter = 0;
94         SkPaint paint;
95         paint.setAntiAlias(true);
96 
97         // For stroke style painter and fill-and-stroke style painter
98         for (size_t type = 0; type < kClosureTypeCount; ++type) {
99             for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) {
100                 for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) {
101                     for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) {
102                         for (int width = 0; width < numWidths; ++width) {
103                             canvas->save();
104                             SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
105 
106                             SkPath path;
107                             MakePath(&path, kType[type]);
108 
109                             paint.setStyle(kStyle[style]);
110                             paint.setStrokeCap(kCap[cap]);
111                             paint.setStrokeJoin(kJoin[join]);
112                             paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width]));
113 
114                             canvas->drawPath(path, paint);
115                             canvas->restore();
116                             ++counter;
117                         }
118                     }
119                 }
120             }
121         }
122 
123         // For fill style painter
124         paint.setStyle(SkPaint::kFill_Style);
125         for (size_t type = 0; type < kClosureTypeCount; ++type) {
126             canvas->save();
127             SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
128 
129             SkPath path;
130             MakePath(&path, kType[type]);
131 
132             canvas->drawPath(path, paint);
133             canvas->restore();
134             ++counter;
135         }
136     }
137 
138 private:
139     typedef GM INHERITED;
140 };
141 
142 //////////////////////////////////////////////////////////////////////////////
143 
144 DEF_GM(return new NonClosedPathsGM;)
145 
146 }
147