blob: dccce389d0ea7da42a0661eacb3adae166fc9612 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkPaint.h"
Mike Reed92f6eb12020-08-25 11:48:41 -040011#include "include/core/SkPathBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkScalar.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
15#include "include/core/SkTypes.h"
commit-bot@chromium.org59167052013-11-09 01:37:30 +000016
17namespace skiagm {
18
19// This GM tests a grab-bag of non-closed paths. All these paths look like
20// closed rects, but they don't call path.close(). Depending on the stroke
21// settings these slightly different paths give widely different results.
22class NonClosedPathsGM: public GM {
23public:
24 NonClosedPathsGM() {}
25
26 enum ClosureType {
27 TotallyNonClosed, // The last point doesn't coincide with the first one in the contour.
28 // The path looks not closed at all.
29
30 FakeCloseCorner, // The last point coincides with the first one at a corner.
31 // The path looks closed, but final rendering has 2 ends with cap.
32
33 FakeCloseMiddle, // The last point coincides with the first one in the middle of a line.
34 // The path looks closed, and the final rendering looks closed too.
35
36 kClosureTypeCount
37 };
38
39protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000042 return SkString("nonclosedpaths");
43 }
44
45 // 12 * 18 + 3 cases, every case is 100 * 100 pixels.
mtklein36352bf2015-03-25 18:17:31 -070046 SkISize onISize() override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000047 return SkISize::Make(1220, 1920);
48 }
49
50 // Use rect-like geometry for non-closed path, for right angles make it
51 // easier to show the visual difference of lineCap and lineJoin.
Mike Reed92f6eb12020-08-25 11:48:41 -040052 static SkPath MakePath(ClosureType type) {
53 SkPathBuilder path;
commit-bot@chromium.org59167052013-11-09 01:37:30 +000054 if (FakeCloseMiddle == type) {
Mike Reed92f6eb12020-08-25 11:48:41 -040055 path.moveTo(30, 50);
56 path.lineTo(30, 30);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000057 } else {
Mike Reed92f6eb12020-08-25 11:48:41 -040058 path.moveTo(30, 30);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000059 }
Mike Reed92f6eb12020-08-25 11:48:41 -040060 path.lineTo(70, 30);
61 path.lineTo(70, 70);
62 path.lineTo(30, 70);
63 path.lineTo(30, 50);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000064 if (FakeCloseCorner == type) {
Mike Reed92f6eb12020-08-25 11:48:41 -040065 path.lineTo(30, 30);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000066 }
Mike Reed92f6eb12020-08-25 11:48:41 -040067 return path.detach();
commit-bot@chromium.org59167052013-11-09 01:37:30 +000068 }
69
70 // Set the location for the current test on the canvas
71 static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
72 SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4;
73 SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4;
74 canvas->translate(x, y);
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000078 // Stroke widths are:
79 // 0(may use hairline rendering), 10(common case for stroke-style)
80 // 40 and 50(>= geometry width/height, make the contour filled in fact)
mtkleindbfd7ab2016-09-01 11:24:54 -070081 constexpr int kStrokeWidth[] = {0, 10, 40, 50};
reed@google.com7fa2a652014-01-27 13:42:58 +000082 int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
commit-bot@chromium.org59167052013-11-09 01:37:30 +000083
mtkleindbfd7ab2016-09-01 11:24:54 -070084 constexpr SkPaint::Style kStyle[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000085 SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
86 };
87
mtkleindbfd7ab2016-09-01 11:24:54 -070088 constexpr SkPaint::Cap kCap[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000089 SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
90 };
91
mtkleindbfd7ab2016-09-01 11:24:54 -070092 constexpr SkPaint::Join kJoin[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000093 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
94 };
95
mtkleindbfd7ab2016-09-01 11:24:54 -070096 constexpr ClosureType kType[] = {
commit-bot@chromium.org59167052013-11-09 01:37:30 +000097 TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle
98 };
99
100 int counter = 0;
101 SkPaint paint;
102 paint.setAntiAlias(true);
103
104 // For stroke style painter and fill-and-stroke style painter
105 for (size_t type = 0; type < kClosureTypeCount; ++type) {
106 for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) {
107 for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) {
108 for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000109 for (int width = 0; width < numWidths; ++width) {
commit-bot@chromium.org59167052013-11-09 01:37:30 +0000110 canvas->save();
111 SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
112
Mike Reed92f6eb12020-08-25 11:48:41 -0400113 SkPath path = MakePath(kType[type]);
commit-bot@chromium.org59167052013-11-09 01:37:30 +0000114
115 paint.setStyle(kStyle[style]);
116 paint.setStrokeCap(kCap[cap]);
117 paint.setStrokeJoin(kJoin[join]);
118 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width]));
119
120 canvas->drawPath(path, paint);
121 canvas->restore();
122 ++counter;
123 }
124 }
125 }
126 }
127 }
128
129 // For fill style painter
130 paint.setStyle(SkPaint::kFill_Style);
131 for (size_t type = 0; type < kClosureTypeCount; ++type) {
132 canvas->save();
133 SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
134
Mike Reed92f6eb12020-08-25 11:48:41 -0400135 SkPath path = MakePath(kType[type]);
commit-bot@chromium.org59167052013-11-09 01:37:30 +0000136
137 canvas->drawPath(path, paint);
138 canvas->restore();
139 ++counter;
140 }
141 }
142
143private:
John Stiles7571f9e2020-09-02 22:42:33 -0400144 using INHERITED = GM;
commit-bot@chromium.org59167052013-11-09 01:37:30 +0000145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149DEF_GM(return new NonClosedPathsGM;)
150
John Stilesa6841be2020-08-06 14:11:56 -0400151} // namespace skiagm