| 1 |
/* |
| 2 |
* Copyright 2011 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 "include/core/SkPath.h" |
| 9 |
#include "include/core/SkPathBuilder.h" |
| 10 |
#include "include/core/SkPathTypes.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkStream.h" |
| 14 |
#include "include/core/SkString.h" |
| 15 |
#include "include/core/SkTypes.h" |
| 16 |
#include "include/utils/SkParse.h" |
| 17 |
#include "include/utils/SkParsePath.h" |
| 18 |
#include "src/core/SkGeometry.h" |
| 19 |
|
| 20 |
#include <cstdio> |
| 21 |
|
| 22 |
static inline bool is_between(int c, int min, int max) { |
| 23 |
return (unsigned)(c - min) <= (unsigned)(max - min); |
| 24 |
} |
| 25 |
|
| 26 |
static inline bool is_ws(int c) { |
| 27 |
return is_between(c, 1, 32); |
| 28 |
} |
| 29 |
|
| 30 |
static inline bool is_digit(int c) { |
| 31 |
return is_between(c, '0', '9'); |
| 32 |
} |
| 33 |
|
| 34 |
static inline bool is_sep(int c) { |
| 35 |
return is_ws(c) || c == ','; |
| 36 |
} |
| 37 |
|
| 38 |
static inline bool is_lower(int c) { |
| 39 |
return is_between(c, 'a', 'z'); |
| 40 |
} |
| 41 |
|
| 42 |
static inline int to_upper(int c) { |
| 43 |
return c - 'a' + 'A'; |
| 44 |
} |
| 45 |
|
| 46 |
static const char* skip_ws(const char str[]) { |
| 47 |
SkASSERT(str); |
| 48 |
while (is_ws(*str)) |
| 49 |
str++; |
| 50 |
return str; |
| 51 |
} |
| 52 |
|
| 53 |
static const char* skip_sep(const char str[]) { |
| 54 |
if (!str) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
while (is_sep(*str)) |
| 58 |
str++; |
| 59 |
return str; |
| 60 |
} |
| 61 |
|
| 62 |
// If unable to read count points from str into value, this will return nullptr |
| 63 |
// to signal the failure. Otherwise, it will return the next offset to read from. |
| 64 |
static const char* find_points(const char str[], SkPoint value[], int count, |
| 65 |
bool isRelative, SkPoint* relative) { |
| 66 |
str = SkParse::FindScalars(str, &value[0].fX, count * 2); |
| 67 |
if (isRelative) { |
| 68 |
for (int index = 0; index < count; index++) { |
| 69 |
value[index].fX += relative->fX; |
| 70 |
value[index].fY += relative->fY; |
| 71 |
} |
| 72 |
} |
| 73 |
return str; |
| 74 |
} |
| 75 |
|
| 76 |
// If unable to read a scalar from str into value, this will return nullptr |
| 77 |
// to signal the failure. Otherwise, it will return the next offset to read from. |
| 78 |
static const char* find_scalar(const char str[], SkScalar* value, |
| 79 |
bool isRelative, SkScalar relative) { |
| 80 |
str = SkParse::FindScalar(str, value); |
| 81 |
if (!str) { |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
if (isRelative) { |
| 85 |
*value += relative; |
| 86 |
} |
| 87 |
str = skip_sep(str); |
| 88 |
return str; |
| 89 |
} |
| 90 |
|
| 91 |
// https://www.w3.org/TR/SVG11/paths.html#PathDataBNF |
| 92 |
// |
| 93 |
// flag: |
| 94 |
// "0" | "1" |
| 95 |
static const char* find_flag(const char str[], bool* value) { |
| 96 |
if (!str) { |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
if (str[0] != '1' && str[0] != '0') { |
| 100 |
return nullptr; |
| 101 |
} |
| 102 |
*value = str[0] != '0'; |
| 103 |
str = skip_sep(str + 1); |
| 104 |
return str; |
| 105 |
} |
| 106 |
|
| 107 |
std::optional<SkPath> SkParsePath::FromSVGString(const char data[]) { |
| 108 |
// We will write all data to this local path and only write it |
| 109 |
// to result if the whole parsing succeeds. |
| 110 |
SkPathBuilder builder; |
| 111 |
SkPoint first = {0, 0}; |
| 112 |
SkPoint c = {0, 0}; |
| 113 |
SkPoint lastc = {0, 0}; |
| 114 |
// We will use find_points and find_scalar to read into these. |
| 115 |
// There might not be enough data to fill them, so to avoid |
| 116 |
// MSAN warnings about using uninitialized bytes, we initialize |
| 117 |
// them there. |
| 118 |
SkPoint points[3] = {}; |
| 119 |
SkScalar scratch = 0; |
| 120 |
char op = '\0'; |
| 121 |
char previousOp = '\0'; |
| 122 |
bool relative = false; |
| 123 |
for (;;) { |
| 124 |
if (!data) { |
| 125 |
// Truncated data |
| 126 |
return {}; |
| 127 |
} |
| 128 |
data = skip_ws(data); |
| 129 |
if (data[0] == '\0') { |
| 130 |
break; |
| 131 |
} |
| 132 |
char ch = data[0]; |
| 133 |
if (is_digit(ch) || ch == '-' || ch == '+' || ch == '.') { |
| 134 |
if (op == '\0' || op == 'Z') { |
| 135 |
return {}; |
| 136 |
} |
| 137 |
} else if (is_sep(ch)) { |
| 138 |
data = skip_sep(data); |
| 139 |
} else { |
| 140 |
op = ch; |
| 141 |
relative = false; |
| 142 |
if (is_lower(op)) { |
| 143 |
op = (char) to_upper(op); |
| 144 |
relative = true; |
| 145 |
} |
| 146 |
data++; |
| 147 |
data = skip_sep(data); |
| 148 |
} |
| 149 |
switch (op) { |
| 150 |
case 'M': // Move |
| 151 |
data = find_points(data, points, 1, relative, &c); |
| 152 |
// find_points might have failed, so this might be the |
| 153 |
// previous point. However, data will be set to nullptr |
| 154 |
// if it failed, so we will check this at the top of the loop. |
| 155 |
builder.moveTo(points[0]); |
| 156 |
previousOp = '\0'; |
| 157 |
op = 'L'; |
| 158 |
c = points[0]; |
| 159 |
break; |
| 160 |
case 'L': // Line |
| 161 |
data = find_points(data, points, 1, relative, &c); |
| 162 |
builder.lineTo(points[0]); |
| 163 |
c = points[0]; |
| 164 |
break; |
| 165 |
case 'H': // Horizontal Line |
| 166 |
data = find_scalar(data, &scratch, relative, c.fX); |
| 167 |
// Similarly, if there wasn't a scalar to read, data will |
| 168 |
// be set to nullptr and this lineTo is bogus but will |
| 169 |
// be ultimately ignored when the next time through the loop |
| 170 |
// detects that and bails out. |
| 171 |
builder.lineTo(scratch, c.fY); |
| 172 |
c.fX = scratch; |
| 173 |
break; |
| 174 |
case 'V': // Vertical Line |
| 175 |
data = find_scalar(data, &scratch, relative, c.fY); |
| 176 |
builder.lineTo(c.fX, scratch); |
| 177 |
c.fY = scratch; |
| 178 |
break; |
| 179 |
case 'C': // Cubic Bezier Curve |
| 180 |
data = find_points(data, points, 3, relative, &c); |
| 181 |
goto cubicCommon; |
| 182 |
case 'S': // Continued "Smooth" Cubic Bezier Curve |
| 183 |
data = find_points(data, &points[1], 2, relative, &c); |
| 184 |
points[0] = c; |
| 185 |
if (previousOp == 'C' || previousOp == 'S') { |
| 186 |
points[0].fX -= lastc.fX - c.fX; |
| 187 |
points[0].fY -= lastc.fY - c.fY; |
| 188 |
} |
| 189 |
cubicCommon: |
| 190 |
builder.cubicTo(points[0], points[1], points[2]); |
| 191 |
lastc = points[1]; |
| 192 |
c = points[2]; |
| 193 |
break; |
| 194 |
case 'Q': // Quadratic Bezier Curve |
| 195 |
data = find_points(data, points, 2, relative, &c); |
| 196 |
goto quadraticCommon; |
| 197 |
case 'T': // Continued Quadratic Bezier Curve |
| 198 |
data = find_points(data, &points[1], 1, relative, &c); |
| 199 |
points[0] = c; |
| 200 |
if (previousOp == 'Q' || previousOp == 'T') { |
| 201 |
points[0].fX -= lastc.fX - c.fX; |
| 202 |
points[0].fY -= lastc.fY - c.fY; |
| 203 |
} |
| 204 |
quadraticCommon: |
| 205 |
builder.quadTo(points[0], points[1]); |
| 206 |
lastc = points[0]; |
| 207 |
c = points[1]; |
| 208 |
break; |
| 209 |
case 'A': { // Arc (Elliptical) |
| 210 |
SkPoint radii; |
| 211 |
SkScalar angle; |
| 212 |
bool largeArc, sweep; |
| 213 |
if ((data = find_points(data, &radii, 1, false, nullptr)) |
| 214 |
&& (data = skip_sep(data)) |
| 215 |
&& (data = find_scalar(data, &angle, false, 0)) |
| 216 |
&& (data = skip_sep(data)) |
| 217 |
&& (data = find_flag(data, &largeArc)) |
| 218 |
&& (data = skip_sep(data)) |
| 219 |
&& (data = find_flag(data, &sweep)) |
| 220 |
&& (data = skip_sep(data)) |
| 221 |
&& (data = find_points(data, &points[0], 1, relative, &c))) { |
| 222 |
builder.arcTo(radii, angle, (SkPathBuilder::ArcSize) largeArc, |
| 223 |
(SkPathDirection) !sweep, points[0]); |
| 224 |
c = builder.points().back(); |
| 225 |
} |
| 226 |
} break; |
| 227 |
case 'Z': // Close Path |
| 228 |
builder.close(); |
| 229 |
c = first; |
| 230 |
break; |
| 231 |
default: |
| 232 |
return {}; |
| 233 |
} |
| 234 |
if (previousOp == 0) { |
| 235 |
first = c; |
| 236 |
} |
| 237 |
previousOp = op; |
| 238 |
} |
| 239 |
|
| 240 |
return builder.detach(); |
| 241 |
} |
| 242 |
|
| 243 |
/////////////////////////////////////////////////////////////////////////////// |
| 244 |
|
| 245 |
SkString SkParsePath::ToSVGString(const SkPath& path, PathEncoding encoding) { |
| 246 |
SkDynamicMemoryWStream stream; |
| 247 |
|
| 248 |
SkPoint current_point{0,0}; |
| 249 |
const auto rel_selector = encoding == PathEncoding::Relative; |
| 250 |
|
| 251 |
const auto append_command = [&](char cmd, const SkPoint pts[], size_t count) { |
| 252 |
// Use lower case cmds for relative encoding. |
| 253 |
cmd += 32 * rel_selector; |
| 254 |
stream.write(&cmd, 1); |
| 255 |
|
| 256 |
for (size_t i = 0; i < count; ++i) { |
| 257 |
const auto pt = pts[i] - current_point; |
| 258 |
if (i > 0) { |
| 259 |
stream.write(" ", 1); |
| 260 |
} |
| 261 |
stream.writeScalarAsText(pt.fX); |
| 262 |
stream.write(" ", 1); |
| 263 |
stream.writeScalarAsText(pt.fY); |
| 264 |
} |
| 265 |
|
| 266 |
SkASSERT(count > 0); |
| 267 |
// For relative encoding, track the current point (otherwise == origin). |
| 268 |
current_point = pts[count - 1] * rel_selector; |
| 269 |
}; |
| 270 |
|
| 271 |
SkPath::Iter iter(path, false); |
| 272 |
|
| 273 |
while (auto rec = iter.next()) { |
| 274 |
SkSpan<const SkPoint> pts = rec->fPoints; |
| 275 |
switch (rec->fVerb) { |
| 276 |
case SkPathVerb::kConic: { |
| 277 |
const SkScalar tol = SK_Scalar1 / 1024; // how close to a quad |
| 278 |
SkAutoConicToQuads quadder; |
| 279 |
const SkPoint* quadPts = quadder.computeQuads(pts.data(), rec->conicWeight(), tol); |
| 280 |
for (int i = 0; i < quadder.countQuads(); ++i) { |
| 281 |
append_command('Q', &quadPts[i*2 + 1], 2); |
| 282 |
} |
| 283 |
} break; |
| 284 |
case SkPathVerb::kMove: |
| 285 |
append_command('M', &pts[0], 1); |
| 286 |
break; |
| 287 |
case SkPathVerb::kLine: |
| 288 |
append_command('L', &pts[1], 1); |
| 289 |
break; |
| 290 |
case SkPathVerb::kQuad: |
| 291 |
append_command('Q', &pts[1], 2); |
| 292 |
break; |
| 293 |
case SkPathVerb::kCubic: |
| 294 |
append_command('C', &pts[1], 3); |
| 295 |
break; |
| 296 |
case SkPathVerb::kClose: |
| 297 |
stream.write("Z", 1); |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
SkString str; |
| 303 |
str.resize(stream.bytesWritten()); |
| 304 |
stream.copyTo(str.data()); |
| 305 |
return str; |
| 306 |
} |