blob: 03942fe1cd2d8d6a664ac5ecc0344e76a44fc60f [file] [log] [blame]
caryclark55d49052016-01-23 05:07:04 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BD-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"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040011#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040013#include "include/core/SkPathEffect.h"
14#include "include/core/SkPathMeasure.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
19#include "include/effects/SkDashPathEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/utils/SkParsePath.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040021#include "include/utils/SkRandom.h"
Mike Kleinad44dd52019-05-14 14:01:39 -050022#include "src/core/SkOSFile.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040023#include "tools/random_parse_path.h"
24
25#include <stdio.h>
26
27/* The test below generates a reference image using SVG. To compare the result for correctness,
28 enable the define below and then view the generated SVG in a browser.
29 */
30static constexpr bool GENERATE_SVG_REFERENCE = false;
caryclark55d49052016-01-23 05:07:04 -080031
32/*
33The arcto test below should draw the same as this SVG:
34(Note that Skia's arcTo Direction parameter value is opposite SVG's sweep value, e.g. 0 / 1)
35
36<svg width="500" height="600">
37<path d="M 50,100 A50,50, 0,0,1, 150,200" style="stroke:#660000; fill:none; stroke-width:2" />
38<path d="M100,100 A50,100, 0,0,1, 200,200" style="stroke:#660000; fill:none; stroke-width:2" />
39<path d="M150,100 A50,50, 45,0,1, 250,200" style="stroke:#660000; fill:none; stroke-width:2" />
40<path d="M200,100 A50,100, 45,0,1, 300,200" style="stroke:#660000; fill:none; stroke-width:2" />
41
42<path d="M150,200 A50,50, 0,1,0, 150,300" style="stroke:#660000; fill:none; stroke-width:2" />
43<path d="M200,200 A50,100, 0,1,0, 200,300" style="stroke:#660000; fill:none; stroke-width:2" />
44<path d="M250,200 A50,50, 45,1,0, 250,300" style="stroke:#660000; fill:none; stroke-width:2" />
45<path d="M300,200 A50,100, 45,1,0, 300,300" style="stroke:#660000; fill:none; stroke-width:2" />
46
47<path d="M250,400 A120,80 0 0,0 250,500"
48 fill="none" stroke="red" stroke-width="5" />
49
50<path d="M250,400 A120,80 0 1,1 250,500"
51 fill="none" stroke="green" stroke-width="5"/>
52
53<path d="M250,400 A120,80 0 1,0 250,500"
54 fill="none" stroke="purple" stroke-width="5"/>
55
56<path d="M250,400 A120,80 0 0,1 250,500"
57 fill="none" stroke="blue" stroke-width="5"/>
caryclarkfc752532016-01-25 05:39:04 -080058
59<path d="M100,100 A 0, 0 0 0,1 200,200"
60 fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
61
62<path d="M200,100 A 80,80 0 0,1 200,100"
63 fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
caryclark55d49052016-01-23 05:07:04 -080064</svg>
65 */
66
67DEF_SIMPLE_GM(arcto, canvas, 500, 600) {
68 SkPaint paint;
69 paint.setAntiAlias(true);
70 paint.setStyle(SkPaint::kStroke_Style);
71 paint.setStrokeWidth(2);
72 paint.setColor(0xFF660000);
caryclarkfc752532016-01-25 05:39:04 -080073// canvas->scale(2, 2); // for testing on retina
caryclark55d49052016-01-23 05:07:04 -080074 SkRect oval = SkRect::MakeXYWH(100, 100, 100, 100);
75 SkPath svgArc;
76
77 for (int angle = 0; angle <= 45; angle += 45) {
78 for (int oHeight = 2; oHeight >= 1; --oHeight) {
79 SkScalar ovalHeight = oval.height() / oHeight;
80 svgArc.moveTo(oval.fLeft, oval.fTop);
81 svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kSmall_ArcSize,
Mike Reed30bc5272019-11-22 18:34:02 +000082 SkPathDirection::kCW, oval.right(), oval.bottom());
caryclark55d49052016-01-23 05:07:04 -080083 canvas->drawPath(svgArc, paint);
84 svgArc.reset();
85
86 svgArc.moveTo(oval.fLeft + 100, oval.fTop + 100);
87 svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kLarge_ArcSize,
Mike Reed30bc5272019-11-22 18:34:02 +000088 SkPathDirection::kCCW, oval.right(), oval.bottom() + 100);
caryclark55d49052016-01-23 05:07:04 -080089 canvas->drawPath(svgArc, paint);
90 oval.offset(50, 0);
91 svgArc.reset();
92
93 }
94 }
95
96 paint.setStrokeWidth(5);
97 const SkColor purple = 0xFF800080;
98 const SkColor darkgreen = 0xFF008000;
99 const SkColor colors[] = { SK_ColorRED, darkgreen, purple, SK_ColorBLUE };
100 const char* arcstrs[] = {
101 "M250,400 A120,80 0 0,0 250,500",
102 "M250,400 A120,80 0 1,1 250,500",
103 "M250,400 A120,80 0 1,0 250,500",
104 "M250,400 A120,80 0 0,1 250,500"
105 };
106 int cIndex = 0;
107 for (const char* arcstr : arcstrs) {
108 SkParsePath::FromSVGString(arcstr, &svgArc);
109 paint.setColor(colors[cIndex++]);
110 canvas->drawPath(svgArc, paint);
111 }
caryclarkfc752532016-01-25 05:39:04 -0800112
113 // test that zero length arcs still draw round cap
114 paint.setStrokeCap(SkPaint::kRound_Cap);
115 SkPath path;
116 path.moveTo(100, 100);
Mike Reed30bc5272019-11-22 18:34:02 +0000117 path.arcTo(0, 0, 0, SkPath::kLarge_ArcSize, SkPathDirection::kCW, 200, 200);
caryclarkfc752532016-01-25 05:39:04 -0800118 canvas->drawPath(path, paint);
119
120 path.reset();
121 path.moveTo(200, 100);
Mike Reed30bc5272019-11-22 18:34:02 +0000122 path.arcTo(80, 80, 0, SkPath::kLarge_ArcSize, SkPathDirection::kCW, 200, 100);
caryclarkfc752532016-01-25 05:39:04 -0800123 canvas->drawPath(path, paint);
caryclark55d49052016-01-23 05:07:04 -0800124}
caryclarkf1d41512016-02-09 10:30:22 -0800125
caryclarkf1d41512016-02-09 10:30:22 -0800126enum {
127 kParsePathTestDimension = 500
128};
129
130DEF_SIMPLE_GM(parsedpaths, canvas, kParsePathTestDimension, kParsePathTestDimension) {
caryclarkf1d41512016-02-09 10:30:22 -0800131 SkString str;
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400132 FILE* file;
133 if (GENERATE_SVG_REFERENCE) {
134 file = sk_fopen("svgout.htm", kWrite_SkFILE_Flag);
135 str.printf("<svg width=\"%d\" height=\"%d\">\n", kParsePathTestDimension,
136 kParsePathTestDimension);
137 sk_fwrite(str.c_str(), str.size(), file);
138 }
caryclarkf1d41512016-02-09 10:30:22 -0800139 SkRandom rand;
140 SkPaint paint;
141 paint.setAntiAlias(true);
142 for (int xStart = 0; xStart < kParsePathTestDimension; xStart += 100) {
143 canvas->save();
144 for (int yStart = 0; yStart < kParsePathTestDimension; yStart += 100) {
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400145 if (GENERATE_SVG_REFERENCE) {
146 str.printf("<g transform='translate(%d,%d) scale(%d,%d)'>\n", xStart, yStart,
147 1, 1);
148 sk_fwrite(str.c_str(), str.size(), file);
149 str.printf("<clipPath id='clip_%d_%d'>\n", xStart, yStart);
150 sk_fwrite(str.c_str(), str.size(), file);
151 str.printf("<rect width='100' height='100' x='0' y='0'></rect>\n");
152 sk_fwrite(str.c_str(), str.size(), file);
153 str.printf("</clipPath>\n");
154 sk_fwrite(str.c_str(), str.size(), file);
155 }
caryclarkf1d41512016-02-09 10:30:22 -0800156 int count = 3;
157 do {
158 SkPath path;
159 SkString spec;
caryclark58f4e1d2016-02-09 14:32:42 -0800160 uint32_t y = rand.nextRangeU(30, 70);
161 uint32_t x = rand.nextRangeU(30, 70);
162 spec.printf("M %d,%d\n", x, y);
caryclarkf1d41512016-02-09 10:30:22 -0800163 uint32_t count = rand.nextRangeU(0, 10);
164 for (uint32_t i = 0; i < count; ++i) {
165 spec.append(MakeRandomParsePathPiece(&rand));
166 }
167 SkAssertResult(SkParsePath::FromSVGString(spec.c_str(), &path));
168 paint.setColor(rand.nextU());
169 canvas->save();
170 canvas->clipRect(SkRect::MakeIWH(100, 100));
171 canvas->drawPath(path, paint);
172 canvas->restore();
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400173 if (GENERATE_SVG_REFERENCE) {
174 str.printf("<path d='\n");
175 sk_fwrite(str.c_str(), str.size(), file);
176 sk_fwrite(spec.c_str(), spec.size(), file);
177 str.printf("\n' fill='#%06x' fill-opacity='%g'", paint.getColor() & 0xFFFFFF,
178 paint.getAlpha() / 255.f);
179 sk_fwrite(str.c_str(), str.size(), file);
180 str.printf(" clip-path='url(#clip_%d_%d)'/>\n", xStart, yStart);
181 sk_fwrite(str.c_str(), str.size(), file);
182 }
caryclarkf1d41512016-02-09 10:30:22 -0800183 } while (--count > 0);
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400184 if (GENERATE_SVG_REFERENCE) {
185 str.printf("</g>\n");
186 sk_fwrite(str.c_str(), str.size(), file);
187 }
caryclarkf1d41512016-02-09 10:30:22 -0800188 canvas->translate(0, 100);
189 }
190 canvas->restore();
191 canvas->translate(100, 0);
192 }
Ben Wagnerd1701ba2019-04-30 13:44:26 -0400193 if (GENERATE_SVG_REFERENCE) {
194 const char trailer[] = "</svg>\n";
195 sk_fwrite(trailer, sizeof(trailer) - 1, file);
196 sk_fclose(file);
197 }
caryclarkf1d41512016-02-09 10:30:22 -0800198}
caryclark1b6934f2016-03-16 06:46:50 -0700199
200DEF_SIMPLE_GM(bug593049, canvas, 300, 300) {
201 canvas->translate(111, 0);
202
203 SkPath p;
204 p.moveTo(-43.44464063610148f, 79.43535936389853f);
205 const SkScalar yOffset = 122.88f;
206 const SkScalar radius = 61.44f;
207 SkRect oval = SkRect::MakeXYWH(-radius, yOffset - radius, 2 * radius, 2 * radius);
208 p.arcTo(oval, 1.25f * 180, .5f * 180, false);
209
210 SkPaint paint;
211 paint.setStyle(SkPaint::kStroke_Style);
212 paint.setStrokeCap(SkPaint::kRound_Cap);
213 paint.setStrokeWidth(15.36f);
214
215 canvas->drawPath(p, paint);
216}
caryclarkeb75c7d2016-03-18 06:04:26 -0700217
caryclarkeb75c7d2016-03-18 06:04:26 -0700218DEF_SIMPLE_GM(bug583299, canvas, 300, 300) {
219 const char* d="M60,60 A50,50 0 0 0 160,60 A50,50 0 0 0 60,60z";
220 SkPaint p;
221 p.setStyle(SkPaint::kStroke_Style);
222 p.setStrokeWidth(100);
223 p.setAntiAlias(true);
224 p.setColor(0xFF008200);
225 p.setStrokeCap(SkPaint::kSquare_Cap);
226 SkPath path;
227 SkParsePath::FromSVGString(d, &path);
228 SkPathMeasure meas(path, false);
229 SkScalar length = meas.getLength();
230 SkScalar intervals[] = {0, length };
231 int intervalCount = (int) SK_ARRAY_COUNT(intervals);
reeda4393342016-03-18 11:22:57 -0700232 p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
caryclarkeb75c7d2016-03-18 06:04:26 -0700233 canvas->drawPath(path, p);
234}