Debian Codegraph @

gm/overstroke.cpp

raw
1 /*
2 * Copyright 2016 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
9 /*
10 * This GM exercises stroking of paths with large stroke lengths, which is
11 * referred to as "overstroke" for brevity. In Skia as of 8/2016 we offset
12 * each part of the curve the request amount even if it makes the offsets
13 * overlap and create holes. There is not a really great algorithm for this
14 * and several other 2D graphics engines have the same bug.
15 *
16 * The old Nvidia Path Renderer used to yield correct results, so a possible
17 * direction of attack is to use the GPU and a completely different algorithm.
18 *
19 * See crbug.com/589769 skbug.com/40036571 skbug.com/40036572
20 */
21
22 #include "gm/gm.h"
23 #include "include/core/SkCanvas.h"
24 #include "include/core/SkColor.h"
25 #include "include/core/SkPaint.h"
26 #include "include/core/SkPathBuilder.h"
27 #include "include/core/SkPathMeasure.h"
28 #include "include/core/SkPathUtils.h"
29 #include "include/core/SkPoint.h"
30 #include "include/core/SkRect.h"
31 #include "include/core/SkScalar.h"
32 #include "src/core/SkPointPriv.h"
33
34 #include <cstddef>
35
36 const SkScalar OVERSTROKE_WIDTH = 500.0f;
37 const SkScalar NORMALSTROKE_WIDTH = 3.0f;
38
39 //////// path and paint builders
40
41 SkPaint make_normal_paint() {
42 SkPaint p;
43 p.setAntiAlias(true);
44 p.setStyle(SkPaint::kStroke_Style);
45 p.setStrokeWidth(NORMALSTROKE_WIDTH);
46 p.setColor(SK_ColorBLUE);
47
48 return p;
49 }
50
51 SkPaint make_overstroke_paint() {
52 SkPaint p;
53 p.setAntiAlias(true);
54 p.setStyle(SkPaint::kStroke_Style);
55 p.setStrokeWidth(OVERSTROKE_WIDTH);
56
57 return p;
58 }
59
60 SkPath quad_path() {
61 return SkPathBuilder().moveTo(0, 0)
62 .lineTo(100, 0)
63 .quadTo(50, -40, 0, 0)
64 .close()
65 .detach();
66 }
67
68 SkPath cubic_path() {
69 return SkPathBuilder()
70 .moveTo(0, 0)
71 .cubicTo(25, 75, 75, -50, 100, 0)
72 .detach();
73 }
74
75 SkPath oval_path() {
76 SkRect oval = SkRect::MakeXYWH(0, -25, 100, 50);
77
78 return SkPathBuilder().arcTo(oval, 0, 359, true).close().detach();
79 }
80
81 SkPath ribs_path(SkPath path, SkScalar radius) {
82 SkPathBuilder ribs;
83
84 const SkScalar spacing = 5.0f;
85 float accum = 0.0f;
86
87 SkPathMeasure meas(path, false);
88 SkScalar length = meas.getLength();
89 SkPoint pos;
90 SkVector tan;
91 while (accum < length) {
92 if (meas.getPosTan(accum, &pos, &tan)) {
93 tan.scale(radius);
94 SkPointPriv::RotateCCW(&tan);
95 ribs.addLine(pos + tan, pos - tan);
96 }
97 accum += spacing;
98 }
99
100 return ribs.detach();
101 }
102
103 void draw_ribs(SkCanvas *canvas, SkPath path) {
104 SkPath ribs = ribs_path(path, OVERSTROKE_WIDTH/2.0f);
105 SkPaint p = make_normal_paint();
106 p.setStrokeWidth(1);
107 p.setColor(SK_ColorBLUE);
108 p.setColor(SK_ColorGREEN);
109
110 canvas->drawPath(ribs, p);
111 }
112
113 ///////// quads
114
115 void draw_small_quad(SkCanvas *canvas) {
116 // scaled so it's visible
117 // canvas->scale(8, 8);
118
119 SkPaint p = make_normal_paint();
120 SkPath path = quad_path();
121
122 draw_ribs(canvas, path);
123 canvas->drawPath(path, p);
124 }
125
126 void draw_large_quad(SkCanvas *canvas) {
127 SkPaint p = make_overstroke_paint();
128 SkPath path = quad_path();
129
130 canvas->drawPath(path, p);
131 draw_ribs(canvas, path);
132 }
133
134 void draw_quad_fillpath(SkCanvas *canvas) {
135 SkPath path = quad_path();
136 SkPaint p = make_overstroke_paint();
137
138 SkPaint fillp = make_normal_paint();
139 fillp.setColor(SK_ColorMAGENTA);
140
141 SkPathBuilder fillpath;
142 skpathutils::FillPathWithPaint(path, p, &fillpath);
143
144 canvas->drawPath(fillpath.detach(), fillp);
145 }
146
147 void draw_stroked_quad(SkCanvas *canvas) {
148 canvas->translate(400, 0);
149 draw_large_quad(canvas);
150 draw_quad_fillpath(canvas);
151 }
152
153 ////////// cubics
154
155 void draw_small_cubic(SkCanvas *canvas) {
156 SkPaint p = make_normal_paint();
157 SkPath path = cubic_path();
158
159 draw_ribs(canvas, path);
160 canvas->drawPath(path, p);
161 }
162
163 void draw_large_cubic(SkCanvas *canvas) {
164 SkPaint p = make_overstroke_paint();
165 SkPath path = cubic_path();
166
167 canvas->drawPath(path, p);
168 draw_ribs(canvas, path);
169 }
170
171 void draw_cubic_fillpath(SkCanvas *canvas) {
172 SkPath path = cubic_path();
173 SkPaint p = make_overstroke_paint();
174
175 SkPaint fillp = make_normal_paint();
176 fillp.setColor(SK_ColorMAGENTA);
177
178 SkPathBuilder fillpath;
179 skpathutils::FillPathWithPaint(path, p, &fillpath);
180
181 canvas->drawPath(fillpath.detach(), fillp);
182 }
183
184 void draw_stroked_cubic(SkCanvas *canvas) {
185 canvas->translate(400, 0);
186 draw_large_cubic(canvas);
187 draw_cubic_fillpath(canvas);
188 }
189
190 ////////// ovals
191
192 void draw_small_oval(SkCanvas *canvas) {
193 SkPaint p = make_normal_paint();
194
195 SkPath path = oval_path();
196
197 draw_ribs(canvas, path);
198 canvas->drawPath(path, p);
199 }
200
201 void draw_large_oval(SkCanvas *canvas) {
202 SkPaint p = make_overstroke_paint();
203 SkPath path = oval_path();
204
205 canvas->drawPath(path, p);
206 draw_ribs(canvas, path);
207 }
208
209 void draw_oval_fillpath(SkCanvas *canvas) {
210 SkPath path = oval_path();
211 SkPaint p = make_overstroke_paint();
212
213 SkPaint fillp = make_normal_paint();
214 fillp.setColor(SK_ColorMAGENTA);
215
216 SkPath fillpath = skpathutils::FillPathWithPaint(path, p);
217
218 canvas->drawPath(fillpath, fillp);
219 }
220
221 void draw_stroked_oval(SkCanvas *canvas) {
222 canvas->translate(400, 0);
223 draw_large_oval(canvas);
224 draw_oval_fillpath(canvas);
225 }
226
227 ////////// gm
228
229 void (*examples[])(SkCanvas *canvas) = {
230 draw_small_quad, draw_stroked_quad, draw_small_cubic,
231 draw_stroked_cubic, draw_small_oval, draw_stroked_oval,
232 };
233
234 DEF_SIMPLE_GM(OverStroke, canvas, 500, 500) {
235 const size_t length = sizeof(examples) / sizeof(examples[0]);
236 const size_t width = 2;
237
238 for (size_t i = 0; i < length; i++) {
239 int x = (int)(i % width);
240 int y = (int)(i / width);
241
242 canvas->save();
243 canvas->translate(150.0f * x, 150.0f * y);
244 canvas->scale(0.2f, 0.2f);
245 canvas->translate(300.0f, 400.0f);
246
247 examples[i](canvas);
248
249 canvas->restore();
250 }
251 }