Debian Codegraph @

gm/thinconcavepaths.cpp

raw
1 /*
2 * Copyright 2017 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/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkScalar.h"
14
15 namespace {
16 // Test thin stroked rect (stroked "by hand", not by stroking).
17 void draw_thin_stroked_rect(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
18 SkPath path = SkPathBuilder()
19 .moveTo(10 + width, 10 + width)
20 .lineTo(40, 10 + width)
21 .lineTo(40, 20)
22 .lineTo(10 + width, 20)
23 .moveTo(10, 10)
24 .lineTo(10, 20 + width)
25 .lineTo(40 + width, 20 + width)
26 .lineTo(40 + width, 10)
27 .detach();
28 canvas->drawPath(path, paint);
29 }
30
31 void draw_thin_right_angle(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
32 SkPath path = SkPathBuilder()
33 .moveTo(10 + width, 10 + width)
34 .lineTo(40, 10 + width)
35 .lineTo(40, 20)
36 .lineTo(40 + width, 20 + width)
37 .lineTo(40 + width, 10)
38 .lineTo(10, 10)
39 .detach();
40 canvas->drawPath(path, paint);
41 }
42
43 // Test thin horizontal line (<1 pixel) which should give lower alpha.
44 void draw_golf_club(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
45 SkPath path = SkPathBuilder()
46 .moveTo(20, 10)
47 .lineTo(80, 10)
48 .lineTo(80, 10 + width)
49 .lineTo(30, 10 + width)
50 .lineTo(30, 20)
51 .lineTo(20, 20)
52 .detach();
53 canvas->drawPath(path, paint);
54 }
55
56 // Test thin lines between two filled regions. The outer edges overlap, but
57 // there are no inverted edges to fix.
58 void draw_barbell(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
59 SkScalar offset = width * 0.5f;
60 SkPath path = SkPathBuilder()
61 .moveTo(30, 5)
62 .lineTo(40 - offset, 15 - offset)
63 .lineTo(60 + offset, 15 - offset)
64 .lineTo(70, 5)
65 .lineTo(70, 25)
66 .lineTo(60 + offset, 15 + offset)
67 .lineTo(40 - offset, 15 + offset)
68 .lineTo(30, 25)
69 .detach();
70 canvas->drawPath(path, paint);
71 }
72
73 // Test a thin rectangle and triangle. The top and bottom inner edges of the
74 // rectangle and all inner edges of the triangle invert on stroking.
75 void draw_thin_rect_and_triangle(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
76 SkPath path = SkPathBuilder()
77 .moveTo(30, 5)
78 .lineTo(30 + width, 5)
79 .lineTo(30 + width, 25)
80 .lineTo(30, 25)
81 .moveTo(40, 5)
82 .lineTo(40 + width, 5)
83 .lineTo(40, 25)
84 .detach();
85 canvas->drawPath(path, paint);
86 }
87
88 // Two triangles joined by a very thin bridge. The tiny triangle formed
89 // by the inner edges at the bridge is inverted.
90 // (These are actually now more phat pants than hipster pants.)
91 void draw_hipster_pants(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
92 SkPath path = SkPathBuilder()
93 .moveTo(10, 10)
94 .lineTo(10, 20)
95 .lineTo(50, 10 + width)
96 .lineTo(90, 20)
97 .lineTo(90, 10)
98 .detach();
99 canvas->drawPath(path, paint);
100 }
101
102 // A thin z-shape whose interior inverts on stroking. The top and bottom inner edges invert, and
103 // the connector edges at the "elbows" intersect the inner edges.
104 void draw_skinny_snake(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
105 SkPath path = SkPathBuilder()
106 .moveTo(20 + width, 10)
107 .lineTo(20 + width, 20)
108 .lineTo(10 + width, 30)
109 .lineTo(10 + width, 40)
110 .lineTo(10 - width, 40)
111 .lineTo(10 - width, 30)
112 .lineTo(20 - width, 20)
113 .lineTo(20 - width, 10)
114 .detach();
115 canvas->drawPath(path, paint);
116 }
117
118 // Test pointy features whose outer edges extend far to the right on stroking.
119 void draw_pointy_golf_club(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
120 SkPath path = SkPathBuilder()
121 .moveTo(20, 10)
122 .lineTo(80, 10 + width * 0.5)
123 .lineTo(30, 10 + width)
124 .lineTo(30, 20)
125 .lineTo(20, 20)
126 .detach();
127 canvas->drawPath(path, paint);
128 }
129
130 void draw_small_i(SkCanvas* canvas, const SkPaint& paint, SkScalar width) {
131 SkPath path = SkPathBuilder()
132 .moveTo(1.25 - width, 18.75 + width)
133 .lineTo(1.25 - width, 12.25 - width)
134 .lineTo(2.50 + width, 12.25 - width)
135 .lineTo(2.50 + width, 18.75 + width)
136 .moveTo(1.25 - width, 11.75 + width)
137 .lineTo(1.25 - width, 10.25 - width)
138 .lineTo(2.50 + width, 10.25 - width)
139 .lineTo(2.50 + width, 11.75 + width)
140 .detach();
141 canvas->drawPath(path, paint);
142 }
143
144
145
146 } // namespace
147
148 DEF_SIMPLE_GM(thinconcavepaths, canvas, 550, 400) {
149 SkPaint paint;
150
151 paint.setAntiAlias(true);
152 paint.setStyle(SkPaint::kFill_Style);
153
154 canvas->save();
155 for (SkScalar width = 0.5f; width < 2.05f; width += 0.25f) {
156 draw_thin_stroked_rect(canvas, paint, width);
157 canvas->translate(0, 25);
158 }
159 canvas->restore();
160 canvas->translate(50, 0);
161 canvas->save();
162 for (SkScalar width = 0.5f; width < 2.05f; width += 0.25f) {
163 draw_thin_right_angle(canvas, paint, width);
164 canvas->translate(0, 25);
165 }
166 canvas->restore();
167 canvas->translate(40, 0);
168 canvas->save();
169 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
170 draw_golf_club(canvas, paint, width);
171 canvas->translate(0, 30);
172 }
173 canvas->restore();
174 canvas->translate(70, 0);
175 canvas->save();
176 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
177 draw_thin_rect_and_triangle(canvas, paint, width);
178 canvas->translate(0, 30);
179 }
180 canvas->restore();
181 canvas->translate(30, 0);
182 canvas->save();
183
184 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
185 draw_barbell(canvas, paint, width);
186 canvas->translate(0, 30);
187 }
188 canvas->restore();
189 canvas->translate(80, 0);
190 canvas->save();
191 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
192 draw_hipster_pants(canvas, paint, width);
193 canvas->translate(0, 30);
194 }
195 canvas->restore();
196 canvas->translate(100, 0);
197 canvas->save();
198 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
199 draw_skinny_snake(canvas, paint, width);
200 canvas->translate(0, 30);
201 }
202 canvas->restore();
203 canvas->translate(30, 0);
204 canvas->save();
205 for (SkScalar width = 0.2f; width < 2.1f; width += 0.2f) {
206 draw_pointy_golf_club(canvas, paint, width);
207 canvas->translate(0, 30);
208 }
209 canvas->restore();
210 canvas->translate(100, 0);
211 canvas->save();
212 for (SkScalar width = 0.0f; width < 0.5f; width += 0.05f) {
213 draw_small_i(canvas, paint, width);
214 canvas->translate(0, 30);
215 }
216 canvas->restore();
217 canvas->translate(100, 0);
218 }