| 1 |
/* |
| 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#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" |
| 20 |
#include "include/utils/SkParsePath.h" |
| 21 |
#include "src/base/SkRandom.h" |
| 22 |
#include "src/core/SkOSFile.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
/* The test below generates a reference image using SVG. To compare the result for correctness, |
| 27 |
enable the define below and then view the generated SVG in a browser. |
| 28 |
*/ |
| 29 |
static constexpr bool GENERATE_SVG_REFERENCE = false; |
| 30 |
|
| 31 |
/* |
| 32 |
The arcto test below should draw the same as this SVG: |
| 33 |
(Note that Skia's arcTo Direction parameter value is opposite SVG's sweep value, e.g. 0 / 1) |
| 34 |
|
| 35 |
<svg width="500" height="600"> |
| 36 |
<path d="M 50,100 A50,50, 0,0,1, 150,200" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 37 |
<path d="M100,100 A50,100, 0,0,1, 200,200" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 38 |
<path d="M150,100 A50,50, 45,0,1, 250,200" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 39 |
<path d="M200,100 A50,100, 45,0,1, 300,200" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 40 |
|
| 41 |
<path d="M150,200 A50,50, 0,1,0, 150,300" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 42 |
<path d="M200,200 A50,100, 0,1,0, 200,300" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 43 |
<path d="M250,200 A50,50, 45,1,0, 250,300" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 44 |
<path d="M300,200 A50,100, 45,1,0, 300,300" style="stroke:#660000; fill:none; stroke-width:2" /> |
| 45 |
|
| 46 |
<path d="M250,400 A120,80 0 0,0 250,500" |
| 47 |
fill="none" stroke="red" stroke-width="5" /> |
| 48 |
|
| 49 |
<path d="M250,400 A120,80 0 1,1 250,500" |
| 50 |
fill="none" stroke="green" stroke-width="5"/> |
| 51 |
|
| 52 |
<path d="M250,400 A120,80 0 1,0 250,500" |
| 53 |
fill="none" stroke="purple" stroke-width="5"/> |
| 54 |
|
| 55 |
<path d="M250,400 A120,80 0 0,1 250,500" |
| 56 |
fill="none" stroke="blue" stroke-width="5"/> |
| 57 |
|
| 58 |
<path d="M100,100 A 0, 0 0 0,1 200,200" |
| 59 |
fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/> |
| 60 |
|
| 61 |
<path d="M200,100 A 80,80 0 0,1 200,100" |
| 62 |
fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/> |
| 63 |
</svg> |
| 64 |
*/ |
| 65 |
|
| 66 |
DEF_SIMPLE_GM(arcto, canvas, 500, 600) { |
| 67 |
SkPaint paint; |
| 68 |
paint.setAntiAlias(true); |
| 69 |
paint.setStyle(SkPaint::kStroke_Style); |
| 70 |
paint.setStrokeWidth(2); |
| 71 |
paint.setColor(0xFF660000); |
| 72 |
// canvas->scale(2, 2); // for testing on retina |
| 73 |
SkRect oval = SkRect::MakeXYWH(100, 100, 100, 100); |
| 74 |
|
| 75 |
for (SkScalar angle = 0; angle <= 45; angle += 45) { |
| 76 |
for (int oHeight = 2; oHeight >= 1; --oHeight) { |
| 77 |
SkPathBuilder svgArc; |
| 78 |
SkScalar ovalHeight = oval.height() / oHeight; |
| 79 |
svgArc.moveTo(oval.fLeft, oval.fTop); |
| 80 |
svgArc.arcTo({oval.width() / 2, ovalHeight}, angle, SkPathBuilder::kSmall_ArcSize, |
| 81 |
SkPathDirection::kCW, {oval.right(), oval.bottom()}); |
| 82 |
canvas->drawPath(svgArc.detach(), paint); |
| 83 |
|
| 84 |
svgArc.moveTo(oval.fLeft + 100, oval.fTop + 100); |
| 85 |
svgArc.arcTo({oval.width() / 2, ovalHeight}, angle, SkPathBuilder::kLarge_ArcSize, |
| 86 |
SkPathDirection::kCCW, {oval.right(), oval.bottom() + 100}); |
| 87 |
canvas->drawPath(svgArc.detach(), paint); |
| 88 |
oval.offset(50, 0); |
| 89 |
|
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
paint.setStrokeWidth(5); |
| 94 |
const SkColor purple = 0xFF800080; |
| 95 |
const SkColor darkgreen = 0xFF008000; |
| 96 |
const SkColor colors[] = { SK_ColorRED, darkgreen, purple, SK_ColorBLUE }; |
| 97 |
const char* arcstrs[] = { |
| 98 |
"M250,400 A120,80 0 0,0 250,500", |
| 99 |
"M250,400 A120,80 0 1,1 250,500", |
| 100 |
"M250,400 A120,80 0 1,0 250,500", |
| 101 |
"M250,400 A120,80 0 0,1 250,500" |
| 102 |
}; |
| 103 |
int cIndex = 0; |
| 104 |
for (const char* arcstr : arcstrs) { |
| 105 |
if (auto path = SkParsePath::FromSVGString(arcstr)) { |
| 106 |
paint.setColor(colors[cIndex++]); |
| 107 |
canvas->drawPath(*path, paint); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// test that zero length arcs still draw round cap |
| 112 |
paint.setStrokeCap(SkPaint::kRound_Cap); |
| 113 |
SkPathBuilder path; |
| 114 |
path.moveTo(100, 100) |
| 115 |
.arcTo({0, 0}, 0, SkPathBuilder::kLarge_ArcSize, SkPathDirection::kCW, {200, 200}); |
| 116 |
canvas->drawPath(path.detach(), paint); |
| 117 |
|
| 118 |
path.moveTo(200, 100) |
| 119 |
.arcTo({80, 80}, 0, SkPathBuilder::kLarge_ArcSize, SkPathDirection::kCW, {200, 100}); |
| 120 |
canvas->drawPath(path.detach(), paint); |
| 121 |
} |
| 122 |
|
| 123 |
enum { |
| 124 |
kParsePathTestDimension = 500 |
| 125 |
}; |
| 126 |
|
| 127 |
const struct Legal { |
| 128 |
char fSymbol; |
| 129 |
int fScalars; |
| 130 |
} gLegal[] = { |
| 131 |
{ 'M', 2 }, |
| 132 |
{ 'H', 1 }, |
| 133 |
{ 'V', 1 }, |
| 134 |
{ 'L', 2 }, |
| 135 |
{ 'Q', 4 }, |
| 136 |
{ 'T', 2 }, |
| 137 |
{ 'C', 6 }, |
| 138 |
{ 'S', 4 }, |
| 139 |
{ 'A', 4 }, |
| 140 |
{ 'Z', 0 }, |
| 141 |
}; |
| 142 |
|
| 143 |
bool gEasy = false; // set to true while debugging to suppress unusual whitespace |
| 144 |
|
| 145 |
// mostly do nothing, then bias towards spaces |
| 146 |
const char gWhiteSpace[] = { 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', 0x09, 0x0D, 0x0A }; |
| 147 |
|
| 148 |
static void add_white(SkRandom* rand, SkString* atom) { |
| 149 |
if (gEasy) { |
| 150 |
atom->append(" "); |
| 151 |
return; |
| 152 |
} |
| 153 |
int reps = rand->nextRangeU(0, 2); |
| 154 |
for (int rep = 0; rep < reps; ++rep) { |
| 155 |
int index = rand->nextRangeU(0, (int) std::size(gWhiteSpace) - 1); |
| 156 |
if (gWhiteSpace[index]) { |
| 157 |
atom->append(&gWhiteSpace[index], 1); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
static void add_comma(SkRandom* rand, SkString* atom) { |
| 163 |
if (gEasy) { |
| 164 |
atom->append(","); |
| 165 |
return; |
| 166 |
} |
| 167 |
size_t count = atom->size(); |
| 168 |
add_white(rand, atom); |
| 169 |
if (rand->nextBool()) { |
| 170 |
atom->append(","); |
| 171 |
} |
| 172 |
do { |
| 173 |
add_white(rand, atom); |
| 174 |
} while (count == atom->size()); |
| 175 |
} |
| 176 |
|
| 177 |
static void add_some_white(SkRandom* rand, SkString* atom) { |
| 178 |
size_t count = atom->size(); |
| 179 |
do { |
| 180 |
add_white(rand, atom); |
| 181 |
} while (count == atom->size()); |
| 182 |
} |
| 183 |
|
| 184 |
static SkString make_random_svg_path(SkRandom* rand) { |
| 185 |
SkString atom; |
| 186 |
int legalIndex = rand->nextRangeU(0, (int) std::size(gLegal) - 1); |
| 187 |
const Legal& legal = gLegal[legalIndex]; |
| 188 |
gEasy ? atom.append("\n") : add_white(rand, &atom); |
| 189 |
char symbol = legal.fSymbol | (rand->nextBool() ? 0x20 : 0); |
| 190 |
atom.append(&symbol, 1); |
| 191 |
int reps = rand->nextRangeU(1, 3); |
| 192 |
for (int rep = 0; rep < reps; ++rep) { |
| 193 |
for (int index = 0; index < legal.fScalars; ++index) { |
| 194 |
SkScalar coord = rand->nextRangeF(0, 100); |
| 195 |
add_white(rand, &atom); |
| 196 |
atom.appendScalar(coord); |
| 197 |
if (rep < reps - 1 && index < legal.fScalars - 1) { |
| 198 |
add_comma(rand, &atom); |
| 199 |
} else { |
| 200 |
add_some_white(rand, &atom); |
| 201 |
} |
| 202 |
if ('A' == legal.fSymbol && 1 == index) { |
| 203 |
atom.appendScalar(rand->nextRangeF(-720, 720)); |
| 204 |
add_comma(rand, &atom); |
| 205 |
atom.appendU32(rand->nextRangeU(0, 1)); |
| 206 |
add_comma(rand, &atom); |
| 207 |
atom.appendU32(rand->nextRangeU(0, 1)); |
| 208 |
add_comma(rand, &atom); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return atom; |
| 213 |
} |
| 214 |
|
| 215 |
DEF_SIMPLE_GM(parsedpaths, canvas, kParsePathTestDimension, kParsePathTestDimension) { |
| 216 |
SkString str; |
| 217 |
FILE* file; |
| 218 |
if (GENERATE_SVG_REFERENCE) { |
| 219 |
file = sk_fopen("svgout.htm", kWrite_SkFILE_Flag); |
| 220 |
str.printf("<svg width=\"%d\" height=\"%d\">\n", kParsePathTestDimension, |
| 221 |
kParsePathTestDimension); |
| 222 |
sk_fwrite(str.c_str(), str.size(), file); |
| 223 |
} |
| 224 |
SkRandom rand; |
| 225 |
SkPaint paint; |
| 226 |
paint.setAntiAlias(true); |
| 227 |
for (int xStart = 0; xStart < kParsePathTestDimension; xStart += 100) { |
| 228 |
canvas->save(); |
| 229 |
for (int yStart = 0; yStart < kParsePathTestDimension; yStart += 100) { |
| 230 |
if (GENERATE_SVG_REFERENCE) { |
| 231 |
str.printf("<g transform='translate(%d,%d) scale(%d,%d)'>\n", xStart, yStart, |
| 232 |
1, 1); |
| 233 |
sk_fwrite(str.c_str(), str.size(), file); |
| 234 |
str.printf("<clipPath id='clip_%d_%d'>\n", xStart, yStart); |
| 235 |
sk_fwrite(str.c_str(), str.size(), file); |
| 236 |
str.printf("<rect width='100' height='100' x='0' y='0'></rect>\n"); |
| 237 |
sk_fwrite(str.c_str(), str.size(), file); |
| 238 |
str.printf("</clipPath>\n"); |
| 239 |
sk_fwrite(str.c_str(), str.size(), file); |
| 240 |
} |
| 241 |
int count = 3; |
| 242 |
do { |
| 243 |
SkString spec; |
| 244 |
uint32_t y = rand.nextRangeU(30, 70); |
| 245 |
uint32_t x = rand.nextRangeU(30, 70); |
| 246 |
spec.printf("M %u,%u\n", x, y); |
| 247 |
for (uint32_t i = rand.nextRangeU(0, 10); i--; ) { |
| 248 |
spec.append(make_random_svg_path(&rand)); |
| 249 |
} |
| 250 |
auto path = SkParsePath::FromSVGString(spec.c_str()); |
| 251 |
SkAssertResult(path.has_value()); |
| 252 |
paint.setColor(rand.nextU()); |
| 253 |
canvas->save(); |
| 254 |
canvas->clipRect(SkRect::MakeIWH(100, 100)); |
| 255 |
canvas->drawPath(*path, paint); |
| 256 |
canvas->restore(); |
| 257 |
if (GENERATE_SVG_REFERENCE) { |
| 258 |
str.printf("<path d='\n"); |
| 259 |
sk_fwrite(str.c_str(), str.size(), file); |
| 260 |
sk_fwrite(spec.c_str(), spec.size(), file); |
| 261 |
str.printf("\n' fill='#%06x' fill-opacity='%g'", paint.getColor() & 0xFFFFFF, |
| 262 |
paint.getAlpha() / 255.f); |
| 263 |
sk_fwrite(str.c_str(), str.size(), file); |
| 264 |
str.printf(" clip-path='url(#clip_%d_%d)'/>\n", xStart, yStart); |
| 265 |
sk_fwrite(str.c_str(), str.size(), file); |
| 266 |
} |
| 267 |
} while (--count > 0); |
| 268 |
if (GENERATE_SVG_REFERENCE) { |
| 269 |
str.printf("</g>\n"); |
| 270 |
sk_fwrite(str.c_str(), str.size(), file); |
| 271 |
} |
| 272 |
canvas->translate(0, 100); |
| 273 |
} |
| 274 |
canvas->restore(); |
| 275 |
canvas->translate(100, 0); |
| 276 |
} |
| 277 |
if (GENERATE_SVG_REFERENCE) { |
| 278 |
const char trailer[] = "</svg>\n"; |
| 279 |
sk_fwrite(trailer, sizeof(trailer) - 1, file); |
| 280 |
sk_fclose(file); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
DEF_SIMPLE_GM(bug593049, canvas, 300, 300) { |
| 285 |
canvas->translate(111, 0); |
| 286 |
|
| 287 |
SkPathBuilder p; |
| 288 |
p.moveTo(-43.44464063610148f, 79.43535936389853f); |
| 289 |
const SkScalar yOffset = 122.88f; |
| 290 |
const SkScalar radius = 61.44f; |
| 291 |
SkRect oval = SkRect::MakeXYWH(-radius, yOffset - radius, 2 * radius, 2 * radius); |
| 292 |
p.arcTo(oval, 1.25f * 180, .5f * 180, false); |
| 293 |
|
| 294 |
SkPaint paint; |
| 295 |
paint.setStyle(SkPaint::kStroke_Style); |
| 296 |
paint.setStrokeCap(SkPaint::kRound_Cap); |
| 297 |
paint.setStrokeWidth(15.36f); |
| 298 |
|
| 299 |
canvas->drawPath(p.detach(), paint); |
| 300 |
} |
| 301 |
|
| 302 |
DEF_SIMPLE_GM(bug583299, canvas, 300, 300) { |
| 303 |
const char* d="M60,60 A50,50 0 0 0 160,60 A50,50 0 0 0 60,60z"; |
| 304 |
SkPaint p; |
| 305 |
p.setStyle(SkPaint::kStroke_Style); |
| 306 |
p.setStrokeWidth(100); |
| 307 |
p.setAntiAlias(true); |
| 308 |
p.setColor(0xFF008200); |
| 309 |
p.setStrokeCap(SkPaint::kSquare_Cap); |
| 310 |
SkPath path = SkParsePath::FromSVGString(d).value_or(SkPath()); |
| 311 |
SkPathMeasure meas(path, false); |
| 312 |
SkScalar length = meas.getLength(); |
| 313 |
SkScalar intervals[] = {0, length }; |
| 314 |
p.setPathEffect(SkDashPathEffect::Make(intervals, 0)); |
| 315 |
canvas->drawPath(path, p); |
| 316 |
} |