blob: c0f39aa06fd74b93d4989234472ea4c7496211cf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.combbff1d52009-06-05 16:21:03 +00008#include "SkParse.h"
9#include "SkParsePath.h"
10
11static inline bool is_between(int c, int min, int max) {
12 return (unsigned)(c - min) <= (unsigned)(max - min);
13}
14
15static inline bool is_ws(int c) {
16 return is_between(c, 1, 32);
17}
18
19static inline bool is_digit(int c) {
20 return is_between(c, '0', '9');
21}
22
23static inline bool is_sep(int c) {
24 return is_ws(c) || c == ',';
25}
26
27static inline bool is_lower(int c) {
28 return is_between(c, 'a', 'z');
29}
30
31static inline int to_upper(int c) {
32 return c - 'a' + 'A';
33}
34
35static const char* skip_ws(const char str[]) {
36 SkASSERT(str);
37 while (is_ws(*str))
38 str++;
39 return str;
40}
41
42static const char* skip_sep(const char str[]) {
43 SkASSERT(str);
44 while (is_sep(*str))
45 str++;
46 return str;
47}
48
49static const char* find_points(const char str[], SkPoint value[], int count,
50 bool isRelative, SkPoint* relative) {
51 str = SkParse::FindScalars(str, &value[0].fX, count * 2);
52 if (isRelative) {
53 for (int index = 0; index < count; index++) {
54 value[index].fX += relative->fX;
55 value[index].fY += relative->fY;
56 }
57 }
58 return str;
59}
60
rmistry@google.comd6176b02012-08-23 18:14:13 +000061static const char* find_scalar(const char str[], SkScalar* value,
reed@android.combbff1d52009-06-05 16:21:03 +000062 bool isRelative, SkScalar relative) {
63 str = SkParse::FindScalar(str, value);
64 if (isRelative) {
65 *value += relative;
66 }
caryclark55d49052016-01-23 05:07:04 -080067 str = skip_sep(str);
reed@android.combbff1d52009-06-05 16:21:03 +000068 return str;
69}
70
71bool SkParsePath::FromSVGString(const char data[], SkPath* result) {
72 SkPath path;
73 SkPoint f = {0, 0};
74 SkPoint c = {0, 0};
75 SkPoint lastc = {0, 0};
76 SkPoint points[3];
77 char op = '\0';
78 char previousOp = '\0';
79 bool relative = false;
80 for (;;) {
scroggo28e80f42015-12-11 09:49:57 -080081 if (!data) {
82 // Truncated data
83 return false;
84 }
reed@android.combbff1d52009-06-05 16:21:03 +000085 data = skip_ws(data);
86 if (data[0] == '\0') {
87 break;
88 }
89 char ch = data[0];
90 if (is_digit(ch) || ch == '-' || ch == '+') {
91 if (op == '\0') {
92 return false;
93 }
caryclark52ee8132015-01-29 09:45:44 -080094 } else if (is_sep(ch)) {
95 data = skip_sep(data);
reed@android.combbff1d52009-06-05 16:21:03 +000096 } else {
97 op = ch;
98 relative = false;
99 if (is_lower(op)) {
100 op = (char) to_upper(op);
101 relative = true;
102 }
103 data++;
104 data = skip_sep(data);
105 }
106 switch (op) {
107 case 'M':
108 data = find_points(data, points, 1, relative, &c);
109 path.moveTo(points[0]);
110 op = 'L';
111 c = points[0];
112 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113 case 'L':
reed@android.combbff1d52009-06-05 16:21:03 +0000114 data = find_points(data, points, 1, relative, &c);
115 path.lineTo(points[0]);
116 c = points[0];
117 break;
118 case 'H': {
119 SkScalar x;
120 data = find_scalar(data, &x, relative, c.fX);
121 path.lineTo(x, c.fY);
122 c.fX = x;
123 } break;
124 case 'V': {
125 SkScalar y;
126 data = find_scalar(data, &y, relative, c.fY);
127 path.lineTo(c.fX, y);
128 c.fY = y;
129 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 case 'C':
reed@android.combbff1d52009-06-05 16:21:03 +0000131 data = find_points(data, points, 3, relative, &c);
132 goto cubicCommon;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133 case 'S':
reed@android.combbff1d52009-06-05 16:21:03 +0000134 data = find_points(data, &points[1], 2, relative, &c);
135 points[0] = c;
136 if (previousOp == 'C' || previousOp == 'S') {
137 points[0].fX -= lastc.fX - c.fX;
138 points[0].fY -= lastc.fY - c.fY;
139 }
140 cubicCommon:
141 path.cubicTo(points[0], points[1], points[2]);
142 lastc = points[1];
143 c = points[2];
144 break;
145 case 'Q': // Quadratic Bezier Curve
146 data = find_points(data, points, 2, relative, &c);
147 goto quadraticCommon;
148 case 'T':
149 data = find_points(data, &points[1], 1, relative, &c);
150 points[0] = points[1];
151 if (previousOp == 'Q' || previousOp == 'T') {
152 points[0].fX = c.fX * 2 - lastc.fX;
153 points[0].fY = c.fY * 2 - lastc.fY;
154 }
155 quadraticCommon:
156 path.quadTo(points[0], points[1]);
157 lastc = points[0];
158 c = points[1];
159 break;
caryclark55d49052016-01-23 05:07:04 -0800160 case 'A': {
161 SkPoint radii;
162 data = find_points(data, &radii, 1, false, nullptr);
163 SkScalar angle, largeArc, sweep;
164 data = find_scalar(data, &angle, false, 0);
165 data = find_scalar(data, &largeArc, false, 0);
166 data = find_scalar(data, &sweep, false, 0);
167 data = find_points(data, &points[0], 1, relative, &c);
168 path.arcTo(radii, angle, (SkPath::ArcSize) SkToBool(largeArc),
169 (SkPath::Direction) !SkToBool(sweep), points[0]);
170 } break;
reed@android.combbff1d52009-06-05 16:21:03 +0000171 case 'Z':
172 path.close();
173#if 0 // !!! still a bug?
174 if (fPath.isEmpty() && (f.fX != 0 || f.fY != 0)) {
175 c.fX -= SkScalar.Epsilon; // !!! enough?
176 fPath.moveTo(c);
177 fPath.lineTo(f);
178 fPath.close();
179 }
180#endif
181 c = f;
182 op = '\0';
183 break;
184 case '~': {
185 SkPoint args[2];
halcanary96fcdcc2015-08-27 07:41:13 -0700186 data = find_points(data, args, 2, false, nullptr);
reed@android.combbff1d52009-06-05 16:21:03 +0000187 path.moveTo(args[0].fX, args[0].fY);
188 path.lineTo(args[1].fX, args[1].fY);
189 } break;
190 default:
191 return false;
192 }
193 if (previousOp == 0) {
194 f = c;
195 }
196 previousOp = op;
197 }
198 // we're good, go ahead and swap in the result
199 result->swap(path);
200 return true;
201}
202
203///////////////////////////////////////////////////////////////////////////////
204
reed220f9262014-12-17 08:21:04 -0800205#include "SkGeometry.h"
reed@android.combbff1d52009-06-05 16:21:03 +0000206#include "SkString.h"
207#include "SkStream.h"
208
209static void write_scalar(SkWStream* stream, SkScalar value) {
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000210 char buffer[64];
reed@android.come191b162009-12-18 21:33:39 +0000211#ifdef SK_BUILD_FOR_WIN32
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212 int len = _snprintf(buffer, sizeof(buffer), "%g", value);
reed@android.come191b162009-12-18 21:33:39 +0000213#else
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000214 int len = snprintf(buffer, sizeof(buffer), "%g", value);
reed@android.come191b162009-12-18 21:33:39 +0000215#endif
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000216 char* stop = buffer + len;
reed@android.combbff1d52009-06-05 16:21:03 +0000217 stream->write(buffer, stop - buffer);
218}
219
220static void append_scalars(SkWStream* stream, char verb, const SkScalar data[],
221 int count) {
222 stream->write(&verb, 1);
223 write_scalar(stream, data[0]);
224 for (int i = 1; i < count; i++) {
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000225 stream->write(" ", 1);
reed@android.combbff1d52009-06-05 16:21:03 +0000226 write_scalar(stream, data[i]);
227 }
228}
229
230void SkParsePath::ToSVGString(const SkPath& path, SkString* str) {
231 SkDynamicMemoryWStream stream;
232
233 SkPath::Iter iter(path, false);
234 SkPoint pts[4];
235
236 for (;;) {
reed@google.com4a3b7142012-05-16 17:16:46 +0000237 switch (iter.next(pts, false)) {
reed220f9262014-12-17 08:21:04 -0800238 case SkPath::kConic_Verb: {
239 const SkScalar tol = SK_Scalar1 / 1024; // how close to a quad
240 SkAutoConicToQuads quadder;
241 const SkPoint* quadPts = quadder.computeQuads(pts, iter.conicWeight(), tol);
242 for (int i = 0; i < quadder.countQuads(); ++i) {
243 append_scalars(&stream, 'Q', &quadPts[i*2 + 1].fX, 4);
244 }
245 } break;
reed@google.com277c3f82013-05-31 15:17:50 +0000246 case SkPath::kMove_Verb:
reed@android.combbff1d52009-06-05 16:21:03 +0000247 append_scalars(&stream, 'M', &pts[0].fX, 2);
248 break;
249 case SkPath::kLine_Verb:
250 append_scalars(&stream, 'L', &pts[1].fX, 2);
251 break;
252 case SkPath::kQuad_Verb:
253 append_scalars(&stream, 'Q', &pts[1].fX, 4);
254 break;
255 case SkPath::kCubic_Verb:
256 append_scalars(&stream, 'C', &pts[1].fX, 6);
257 break;
258 case SkPath::kClose_Verb:
259 stream.write("Z", 1);
260 break;
261 case SkPath::kDone_Verb:
262 str->resize(stream.getOffset());
263 stream.copyTo(str->writable_str());
264 return;
265 }
266 }
267}