blob: 3eb9e1ef798e26abfcbe6c87345e75592919a712 [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 }
67 return str;
68}
69
70bool SkParsePath::FromSVGString(const char data[], SkPath* result) {
71 SkPath path;
72 SkPoint f = {0, 0};
73 SkPoint c = {0, 0};
74 SkPoint lastc = {0, 0};
75 SkPoint points[3];
76 char op = '\0';
77 char previousOp = '\0';
78 bool relative = false;
79 for (;;) {
scroggo28e80f42015-12-11 09:49:57 -080080 if (!data) {
81 // Truncated data
82 return false;
83 }
reed@android.combbff1d52009-06-05 16:21:03 +000084 data = skip_ws(data);
85 if (data[0] == '\0') {
86 break;
87 }
88 char ch = data[0];
89 if (is_digit(ch) || ch == '-' || ch == '+') {
90 if (op == '\0') {
91 return false;
92 }
caryclark52ee8132015-01-29 09:45:44 -080093 } else if (is_sep(ch)) {
94 data = skip_sep(data);
reed@android.combbff1d52009-06-05 16:21:03 +000095 } else {
96 op = ch;
97 relative = false;
98 if (is_lower(op)) {
99 op = (char) to_upper(op);
100 relative = true;
101 }
102 data++;
103 data = skip_sep(data);
104 }
105 switch (op) {
106 case 'M':
107 data = find_points(data, points, 1, relative, &c);
108 path.moveTo(points[0]);
109 op = 'L';
110 c = points[0];
111 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112 case 'L':
reed@android.combbff1d52009-06-05 16:21:03 +0000113 data = find_points(data, points, 1, relative, &c);
114 path.lineTo(points[0]);
115 c = points[0];
116 break;
117 case 'H': {
118 SkScalar x;
119 data = find_scalar(data, &x, relative, c.fX);
120 path.lineTo(x, c.fY);
121 c.fX = x;
122 } break;
123 case 'V': {
124 SkScalar y;
125 data = find_scalar(data, &y, relative, c.fY);
126 path.lineTo(c.fX, y);
127 c.fY = y;
128 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129 case 'C':
reed@android.combbff1d52009-06-05 16:21:03 +0000130 data = find_points(data, points, 3, relative, &c);
131 goto cubicCommon;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132 case 'S':
reed@android.combbff1d52009-06-05 16:21:03 +0000133 data = find_points(data, &points[1], 2, relative, &c);
134 points[0] = c;
135 if (previousOp == 'C' || previousOp == 'S') {
136 points[0].fX -= lastc.fX - c.fX;
137 points[0].fY -= lastc.fY - c.fY;
138 }
139 cubicCommon:
140 path.cubicTo(points[0], points[1], points[2]);
141 lastc = points[1];
142 c = points[2];
143 break;
144 case 'Q': // Quadratic Bezier Curve
145 data = find_points(data, points, 2, relative, &c);
146 goto quadraticCommon;
147 case 'T':
148 data = find_points(data, &points[1], 1, relative, &c);
149 points[0] = points[1];
150 if (previousOp == 'Q' || previousOp == 'T') {
151 points[0].fX = c.fX * 2 - lastc.fX;
152 points[0].fY = c.fY * 2 - lastc.fY;
153 }
154 quadraticCommon:
155 path.quadTo(points[0], points[1]);
156 lastc = points[0];
157 c = points[1];
158 break;
159 case 'Z':
160 path.close();
161#if 0 // !!! still a bug?
162 if (fPath.isEmpty() && (f.fX != 0 || f.fY != 0)) {
163 c.fX -= SkScalar.Epsilon; // !!! enough?
164 fPath.moveTo(c);
165 fPath.lineTo(f);
166 fPath.close();
167 }
168#endif
169 c = f;
170 op = '\0';
171 break;
172 case '~': {
173 SkPoint args[2];
halcanary96fcdcc2015-08-27 07:41:13 -0700174 data = find_points(data, args, 2, false, nullptr);
reed@android.combbff1d52009-06-05 16:21:03 +0000175 path.moveTo(args[0].fX, args[0].fY);
176 path.lineTo(args[1].fX, args[1].fY);
177 } break;
178 default:
179 return false;
180 }
181 if (previousOp == 0) {
182 f = c;
183 }
184 previousOp = op;
185 }
186 // we're good, go ahead and swap in the result
187 result->swap(path);
188 return true;
189}
190
191///////////////////////////////////////////////////////////////////////////////
192
reed220f9262014-12-17 08:21:04 -0800193#include "SkGeometry.h"
reed@android.combbff1d52009-06-05 16:21:03 +0000194#include "SkString.h"
195#include "SkStream.h"
196
197static void write_scalar(SkWStream* stream, SkScalar value) {
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000198 char buffer[64];
reed@android.come191b162009-12-18 21:33:39 +0000199#ifdef SK_BUILD_FOR_WIN32
rmistry@google.comd6176b02012-08-23 18:14:13 +0000200 int len = _snprintf(buffer, sizeof(buffer), "%g", value);
reed@android.come191b162009-12-18 21:33:39 +0000201#else
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000202 int len = snprintf(buffer, sizeof(buffer), "%g", value);
reed@android.come191b162009-12-18 21:33:39 +0000203#endif
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000204 char* stop = buffer + len;
reed@android.combbff1d52009-06-05 16:21:03 +0000205 stream->write(buffer, stop - buffer);
206}
207
208static void append_scalars(SkWStream* stream, char verb, const SkScalar data[],
209 int count) {
210 stream->write(&verb, 1);
211 write_scalar(stream, data[0]);
212 for (int i = 1; i < count; i++) {
reed@android.coma8cf0aa2009-09-24 20:06:03 +0000213 stream->write(" ", 1);
reed@android.combbff1d52009-06-05 16:21:03 +0000214 write_scalar(stream, data[i]);
215 }
216}
217
218void SkParsePath::ToSVGString(const SkPath& path, SkString* str) {
219 SkDynamicMemoryWStream stream;
220
221 SkPath::Iter iter(path, false);
222 SkPoint pts[4];
223
224 for (;;) {
reed@google.com4a3b7142012-05-16 17:16:46 +0000225 switch (iter.next(pts, false)) {
reed220f9262014-12-17 08:21:04 -0800226 case SkPath::kConic_Verb: {
227 const SkScalar tol = SK_Scalar1 / 1024; // how close to a quad
228 SkAutoConicToQuads quadder;
229 const SkPoint* quadPts = quadder.computeQuads(pts, iter.conicWeight(), tol);
230 for (int i = 0; i < quadder.countQuads(); ++i) {
231 append_scalars(&stream, 'Q', &quadPts[i*2 + 1].fX, 4);
232 }
233 } break;
reed@google.com277c3f82013-05-31 15:17:50 +0000234 case SkPath::kMove_Verb:
reed@android.combbff1d52009-06-05 16:21:03 +0000235 append_scalars(&stream, 'M', &pts[0].fX, 2);
236 break;
237 case SkPath::kLine_Verb:
238 append_scalars(&stream, 'L', &pts[1].fX, 2);
239 break;
240 case SkPath::kQuad_Verb:
241 append_scalars(&stream, 'Q', &pts[1].fX, 4);
242 break;
243 case SkPath::kCubic_Verb:
244 append_scalars(&stream, 'C', &pts[1].fX, 6);
245 break;
246 case SkPath::kClose_Verb:
247 stream.write("Z", 1);
248 break;
249 case SkPath::kDone_Verb:
250 str->resize(stream.getOffset());
251 stream.copyTo(str->writable_str());
252 return;
253 }
254 }
255}