blob: dc806f30b62d25cd2888c33d5cd26a5bd44fe9d5 [file] [log] [blame]
commit-bot@chromium.org59167052013-11-09 01:37:30 +00001/*
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
12namespace 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.
17class NonClosedPathsGM: public GM {
18public:
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
34protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000035
mtklein36352bf2015-03-25 18:17:31 -070036 SkString onShortName() override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000037 return SkString("nonclosedpaths");
38 }
39
40 // 12 * 18 + 3 cases, every case is 100 * 100 pixels.
mtklein36352bf2015-03-25 18:17:31 -070041 SkISize onISize() override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000042 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.
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
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
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000071 // 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)
mtkleindbfd7ab2016-09-01 11:24:54 -070074 constexpr int kStrokeWidth[] = {0, 10, 40, 50};
reed@google.com7fa2a652014-01-27 13:42:58 +000075 int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000076
mtkleindbfd7ab2016-09-01 11:24:54 -070077 constexpr SkPaint::Style kStyle[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000078 SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
79 };
80
mtkleindbfd7ab2016-09-01 11:24:54 -070081 constexpr SkPaint::Cap kCap[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000082 SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
83 };
84
mtkleindbfd7ab2016-09-01 11:24:54 -070085 constexpr SkPaint::Join kJoin[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000086 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
87 };
88
mtkleindbfd7ab2016-09-01 11:24:54 -070089 constexpr ClosureType kType[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000090 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) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000102 for (int width = 0; width < numWidths; ++width) {
commit-bot@chromium.org59167052013-11-09 01:37:30 +0000103 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
138private:
139 typedef GM INHERITED;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
144DEF_GM(return new NonClosedPathsGM;)
145
146}