@flighthq/path-formats 0.5.1-next.920.da8f530 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,331 +1,542 @@
1
- import { appendPathArcTo, appendPathClose, appendPathCubicCurveTo, appendPathCurveTo, appendPathLineTo, appendPathMoveTo, createPath, forEachPathSegment, } from '@flighthq/path/contract';
1
+ import { appendPathClose, appendPathCubicCurveTo, appendPathEllipticalArcTo, appendPathLineTo, appendPathMoveTo, appendPathQuadraticCurveTo, createPath, forEachPathSegment, } from '@flighthq/path/contract';
2
2
  /**
3
3
  * Parses an SVG path `d` string into the end of an existing `path`, appending its contours via the
4
- * `@flighthq/path` builders. Supports the full SVG path grammar: absolute (uppercase) and relative
4
+ * flight-path builders. Supports the full SVG path grammar: absolute (uppercase) and relative
5
5
  * (lowercase) `M L H V C S Q T A Z`, implicit repeated commands, and the smooth-curve shorthands
6
- * `S`/`T` (reflecting the previous cubic/quadratic control point). Returns `false` without further
7
- * mutation guarantee on structurally malformed input (a leading command that is not moveto, an
8
- * unknown command letter, or a missing/short coordinate run); returns `true` when the whole string
9
- * parses. An empty or whitespace-only string is well-formed and appends nothing.
6
+ * `S`/`T` (reflecting the previous cubic/quadratic control point). Returns `false` on structurally
7
+ * malformed input (a leading command that is not moveto, an unknown command letter, or a
8
+ * missing/short coordinate run); the input is parsed into a scratch path first, so a failed parse
9
+ * leaves `path` unchanged. Returns `true` when the whole string parses. An empty or
10
+ * whitespace-only string is well-formed and appends nothing.
10
11
  *
11
- * Elliptic arcs (`A`/`a`) are appended through `appendPathArcTo`, which approximates them as cubic
12
- * bezier segments — a subsequent `forEachPathSegment` walk (and `formatSvgPathData`) therefore sees
13
- * cubics, not an arc verb. The geometry round-trips; the arc command does not.
12
+ * Elliptic arcs (`A`/`a`) are appended through `appendPathEllipticalArcTo`, which approximates
13
+ * them as cubic bezier segments, so a later `forEachPathSegment` walk (and
14
+ * `formatSvgPathData`) sees cubics, not an arc verb. The geometry round-trips; the arc command
15
+ * does not.
14
16
  */
15
17
  export function appendSvgPathData(path, d) {
16
- const length = d.length;
17
- let pos = 0;
18
- // Current point, subpath start (for Z), and the last emitted control points in absolute space.
19
- // `lastControl2` feeds `S` reflection (valid only after C/S); `lastQuadControl` feeds `T`
20
- // reflection (valid only after Q/T). `lastKind` records the previous group's canonical letter so
21
- // a shorthand can decide whether reflection applies.
22
- let currentX = 0;
23
- let currentY = 0;
24
- let startX = 0;
25
- let startY = 0;
26
- let lastControl2X = 0;
27
- let lastControl2Y = 0;
28
- let lastQuadControlX = 0;
29
- let lastQuadControlY = 0;
30
- let lastKind = '';
31
- function skipSeparators() {
32
- while (pos < length) {
33
- const c = d.charCodeAt(pos);
34
- // space, tab, LF, CR, FF, or comma
35
- if (c === 32 || c === 9 || c === 10 || c === 13 || c === 12 || c === 44)
36
- pos++;
37
- else
38
- break;
39
- }
18
+ let scratch = createPath('nonZero');
19
+ if (parseSvgPathDataInto(scratch, d) !== undefined) {
20
+ return false;
40
21
  }
41
- function readNumber() {
42
- skipSeparators();
43
- const start = pos;
44
- if (pos < length && (d[pos] === '+' || d[pos] === '-'))
45
- pos++;
46
- let sawDigit = false;
47
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
48
- pos++;
49
- sawDigit = true;
22
+ for (let index = 0, count = scratch.commands.length; index < count; index++) {
23
+ path.commands.push(scratch.commands[index]);
24
+ }
25
+ for (let index = 0, count = scratch.data.length; index < count; index++) {
26
+ path.data.push(scratch.data[index]);
27
+ }
28
+ return true;
29
+ }
30
+ /**
31
+ * Explains why an SVG path `d` string cannot be parsed: `undefined` for well-formed input, or the input
32
+ * position (UTF-16 code-unit index) and a stable reason for malformed input.
33
+ */
34
+ export function explainSvgPathData(d) {
35
+ return parseSvgPathDataInto(createPath('nonZero'), d);
36
+ }
37
+ function explanation(position, reason) {
38
+ return { position: position, reason: String(reason) };
39
+ }
40
+ function formatSvgNumber(value, precision) {
41
+ if (precision === undefined) {
42
+ return getJsNumberString(value);
43
+ }
44
+ const factor = Math.pow(10.0, precision);
45
+ return getJsNumberString(roundJs(value * factor) / factor);
46
+ }
47
+ /**
48
+ * Serializes a `Path` to an SVG path `d` string, emitting absolute commands. Each `PathSegment`
49
+ * maps to its SVG verb: `MoveTo`→`M`, `LineTo`→`L`, `QuadraticCurveTo`→`Q`, `CubicCurveTo`→`C`,
50
+ * `Close`→`Z`. Arcs are not a distinct segment kind (the path stores them as cubics), so no `A`
51
+ * command is produced.
52
+ *
53
+ * `options.precision`, when given, rounds every coordinate to that many decimal places and drops
54
+ * trailing zeros; the default emits full-precision numbers (also trailing-zero-free). Numbers are
55
+ * spelled as JavaScript's `String(number)` spells them.
56
+ */
57
+ export function formatSvgPathData(path, options) {
58
+ const precision = options?.precision;
59
+ let parts = [];
60
+ forEachPathSegment(path, (segment) => (() => {
61
+ const matchValue = segment;
62
+ if (matchValue.kind === 'moveTo') {
63
+ const x = matchValue.x;
64
+ const y = matchValue.y;
65
+ return (() => {
66
+ parts.push(`M${formatSvgNumber(x, precision)} ${formatSvgNumber(y, precision)}`);
67
+ })();
50
68
  }
51
- if (pos < length && d[pos] === '.') {
52
- pos++;
53
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
54
- pos++;
55
- sawDigit = true;
56
- }
69
+ else if (matchValue.kind === 'lineTo') {
70
+ const x = matchValue.x;
71
+ const y = matchValue.y;
72
+ return (() => {
73
+ parts.push(`L${formatSvgNumber(x, precision)} ${formatSvgNumber(y, precision)}`);
74
+ })();
57
75
  }
58
- if (!sawDigit) {
59
- pos = start;
60
- return null;
76
+ else if (matchValue.kind === 'quadraticCurveTo') {
77
+ const controlX = matchValue.controlX;
78
+ const controlY = matchValue.controlY;
79
+ const x = matchValue.x;
80
+ const y = matchValue.y;
81
+ return (() => {
82
+ parts.push(`Q${formatSvgNumber(controlX, precision)} ${formatSvgNumber(controlY, precision)} ${formatSvgNumber(x, precision)} ${formatSvgNumber(y, precision)}`);
83
+ })();
61
84
  }
62
- if (pos < length && (d[pos] === 'e' || d[pos] === 'E')) {
63
- const expStart = pos;
64
- pos++;
65
- if (pos < length && (d[pos] === '+' || d[pos] === '-'))
66
- pos++;
67
- let expDigit = false;
68
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
69
- pos++;
70
- expDigit = true;
71
- }
72
- if (!expDigit)
73
- pos = expStart;
85
+ else if (matchValue.kind === 'cubicCurveTo') {
86
+ const controlX1 = matchValue.controlX1;
87
+ const controlY1 = matchValue.controlY1;
88
+ const controlX2 = matchValue.controlX2;
89
+ const controlY2 = matchValue.controlY2;
90
+ const x = matchValue.x;
91
+ const y = matchValue.y;
92
+ return (() => {
93
+ parts.push(`C${formatSvgNumber(controlX1, precision)} ${formatSvgNumber(controlY1, precision)} ${formatSvgNumber(controlX2, precision)} ${formatSvgNumber(controlY2, precision)} ${formatSvgNumber(x, precision)} ${formatSvgNumber(y, precision)}`);
94
+ })();
74
95
  }
75
- const value = Number.parseFloat(d.slice(start, pos));
76
- return Number.isFinite(value) ? value : null;
77
- }
78
- // An arc flag is a single '0' or '1' character, which may be packed against neighbours with no
79
- // separator (`0110` = four flags), so it is read one char at a time rather than as a number.
80
- function readFlag() {
81
- skipSeparators();
82
- if (pos < length && d[pos] === '0') {
83
- pos++;
84
- return 0;
96
+ else if (matchValue.kind === 'close') {
97
+ return parts.push('Z');
85
98
  }
86
- if (pos < length && d[pos] === '1') {
87
- pos++;
88
- return 1;
89
- }
90
- return null;
99
+ })());
100
+ return parts.join('');
101
+ }
102
+ function getJsNumberString(value) {
103
+ if (Number.isNaN(value)) {
104
+ return 'NaN';
105
+ }
106
+ if (value === 0.0) {
107
+ return '0';
108
+ }
109
+ if (!Number.isFinite(value) && !Number.isNaN(value)) {
110
+ return String(value > 0.0 ? 'Infinity' : '-Infinity');
111
+ }
112
+ const sign = value < 0.0 ? '-' : '';
113
+ const scientific = `${Math.abs(value).toExponential()}`;
114
+ const [mantissa, exponent] = ((value, delimiter) => {
115
+ const index = value.indexOf(delimiter);
116
+ return index === -1 ? undefined : [value.slice(0, index), value.slice(index + delimiter.length)];
117
+ })(scientific, 'e');
118
+ const digits = [...mantissa].filter((c) => c !== '.').join('');
119
+ const k = digits.length;
120
+ const n = ((s) => {
121
+ const v = Number(s);
122
+ return s.trim() !== '' && Number.isInteger(v) ? v : undefined;
123
+ })(exponent) + 1;
124
+ const body = k <= n && n <= 21
125
+ ? `${digits}${'0'.repeat((n - k) >>> 0)}`
126
+ : 0 < n && n <= 21
127
+ ? `${digits.slice(0, n >>> 0)}.${digits.slice(n >>> 0)}`
128
+ : -6 < n && n <= 0
129
+ ? `0.${'0'.repeat(-n >>> 0)}${digits}`
130
+ : (() => {
131
+ const e = n - 1;
132
+ const exponentSign = e < 0 ? '-' : '+';
133
+ return k === 1
134
+ ? `${digits}e${exponentSign}${Math.abs(e)}`
135
+ : `${digits.slice(0, 1)}.${digits.slice(1)}e${exponentSign}${Math.abs(e)}`;
136
+ })();
137
+ return `${sign}${body}`;
138
+ }
139
+ function isAsciiLetter(unit) {
140
+ return (Array.from({ length: 90 - 65 + 1 }, (_, index) => 65 + index).includes(unit) ||
141
+ Array.from({ length: 122 - 97 + 1 }, (_, index) => 97 + index).includes(unit));
142
+ }
143
+ function isDigit(reader) {
144
+ return reader.pos < reader.units.length && reader.units[reader.pos] >= 48 && reader.units[reader.pos] <= 57;
145
+ }
146
+ function isSvgCommandLetter(unit) {
147
+ return Array.from({ length: 'MmLlHhVvCcSsQqTtAaZz'.length }, (_, index) => 'MmLlHhVvCcSsQqTtAaZz'.charCodeAt(index)).some((letter) => letter === unit);
148
+ }
149
+ function isUnit(reader, a, b) {
150
+ return reader.pos < reader.units.length && (reader.units[reader.pos] === a || reader.units[reader.pos] === b);
151
+ }
152
+ /**
153
+ * Parses an SVG path `d` string into a freshly allocated `Path`, or `undefined` on structurally
154
+ * malformed input (see `appendSvgPathData` for the malformed cases). An empty or
155
+ * whitespace-only string yields an empty path, not `undefined`.
156
+ */
157
+ export function parseSvgPathData(d) {
158
+ let path = createPath('nonZero');
159
+ if (parseSvgPathDataInto(path, d) !== undefined) {
160
+ return undefined;
91
161
  }
92
- while (true) {
93
- skipSeparators();
94
- if (pos >= length)
162
+ return path;
163
+ }
164
+ function parseSvgPathDataInto(path, d) {
165
+ let reader = { units: Array.from({ length: d.length }, (_, index) => d.charCodeAt(index)), pos: 0 };
166
+ const length = reader.units.length;
167
+ let currentX = 0.0;
168
+ let currentY = 0.0;
169
+ let startX = 0.0;
170
+ let startY = 0.0;
171
+ let lastControl2X = 0.0;
172
+ let lastControl2Y = 0.0;
173
+ let lastQuadControlX = 0.0;
174
+ let lastQuadControlY = 0.0;
175
+ let lastKind = 0;
176
+ for (;;) {
177
+ skipSeparators(reader);
178
+ if (reader.pos >= length) {
95
179
  break;
96
- const commandLetter = d[pos];
97
- if (!isSvgCommandLetter(commandLetter))
98
- return false;
99
- pos++;
100
- // Path data must open with a moveto.
101
- if (lastKind === '' && commandLetter !== 'M' && commandLetter !== 'm')
102
- return false;
103
- if (commandLetter === 'Z' || commandLetter === 'z') {
180
+ }
181
+ const commandLetter = reader.units[reader.pos];
182
+ if (!isSvgCommandLetter(commandLetter)) {
183
+ return explanation(reader.pos, 'expected-command');
184
+ }
185
+ reader.pos += 1;
186
+ if (lastKind === 0 && commandLetter !== 77 && commandLetter !== 109) {
187
+ return explanation(reader.pos - 1, 'expected-moveto');
188
+ }
189
+ if (commandLetter === 90 || commandLetter === 122) {
104
190
  appendPathClose(path);
105
191
  currentX = startX;
106
192
  currentY = startY;
107
- lastKind = 'Z';
193
+ lastKind = 90;
108
194
  continue;
109
195
  }
110
- // Execute one or more operand groups. After the first `M`/`m` group, implicit repeats are
111
- // lineto groups (SVG spec), so the active letter shifts from M→L (or m→l).
112
- let active = commandLetter;
196
+ let active = commandLetter & 0xff;
113
197
  let first = true;
114
- while (true) {
198
+ for (;;) {
115
199
  if (!first) {
116
- skipSeparators();
117
- if (pos >= length)
200
+ skipSeparators(reader);
201
+ if (reader.pos >= length) {
118
202
  break;
119
- if (isSvgCommandLetter(d[pos]))
203
+ }
204
+ if (isAsciiLetter(reader.units[reader.pos])) {
120
205
  break;
206
+ }
121
207
  }
122
- const relative = active >= 'a';
123
- const upper = relative ? active.toUpperCase() : active;
124
- if (upper === 'M') {
125
- const nx = readNumber();
126
- const ny = readNumber();
127
- if (nx === null || ny === null)
128
- return false;
208
+ const relative = active >= 97;
209
+ const upper = relative && active <= 122 ? active - (97 - 65) : active;
210
+ if (upper === 77) {
211
+ let nx = readNumber(reader);
212
+ let ny = readNumber(reader);
213
+ const $t0 = nx;
214
+ const $t1 = ny;
215
+ if ($t0 === undefined || $t1 === undefined) {
216
+ return explanation(reader.pos, 'expected-coordinate-pair');
217
+ }
218
+ nx = $t0;
219
+ ny = $t1;
129
220
  currentX = relative ? currentX + nx : nx;
130
221
  currentY = relative ? currentY + ny : ny;
131
222
  startX = currentX;
132
223
  startY = currentY;
133
224
  appendPathMoveTo(path, currentX, currentY);
134
- lastKind = 'M';
135
- }
136
- else if (upper === 'L') {
137
- const nx = readNumber();
138
- const ny = readNumber();
139
- if (nx === null || ny === null)
140
- return false;
141
- currentX = relative ? currentX + nx : nx;
142
- currentY = relative ? currentY + ny : ny;
143
- appendPathLineTo(path, currentX, currentY);
144
- lastKind = 'L';
145
- }
146
- else if (upper === 'H') {
147
- const nx = readNumber();
148
- if (nx === null)
149
- return false;
150
- currentX = relative ? currentX + nx : nx;
151
- appendPathLineTo(path, currentX, currentY);
152
- lastKind = 'L';
153
- }
154
- else if (upper === 'V') {
155
- const ny = readNumber();
156
- if (ny === null)
157
- return false;
158
- currentY = relative ? currentY + ny : ny;
159
- appendPathLineTo(path, currentX, currentY);
160
- lastKind = 'L';
161
- }
162
- else if (upper === 'C') {
163
- const x1 = readNumber();
164
- const y1 = readNumber();
165
- const x2 = readNumber();
166
- const y2 = readNumber();
167
- const x = readNumber();
168
- const y = readNumber();
169
- if (x1 === null || y1 === null || x2 === null || y2 === null || x === null || y === null)
170
- return false;
171
- const c1x = relative ? currentX + x1 : x1;
172
- const c1y = relative ? currentY + y1 : y1;
173
- const c2x = relative ? currentX + x2 : x2;
174
- const c2y = relative ? currentY + y2 : y2;
175
- const ax = relative ? currentX + x : x;
176
- const ay = relative ? currentY + y : y;
177
- appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
178
- lastControl2X = c2x;
179
- lastControl2Y = c2y;
180
- currentX = ax;
181
- currentY = ay;
182
- lastKind = 'C';
183
- }
184
- else if (upper === 'S') {
185
- const x2 = readNumber();
186
- const y2 = readNumber();
187
- const x = readNumber();
188
- const y = readNumber();
189
- if (x2 === null || y2 === null || x === null || y === null)
190
- return false;
191
- const reflect = lastKind === 'C' || lastKind === 'S';
192
- const c1x = reflect ? 2 * currentX - lastControl2X : currentX;
193
- const c1y = reflect ? 2 * currentY - lastControl2Y : currentY;
194
- const c2x = relative ? currentX + x2 : x2;
195
- const c2y = relative ? currentY + y2 : y2;
196
- const ax = relative ? currentX + x : x;
197
- const ay = relative ? currentY + y : y;
198
- appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
199
- lastControl2X = c2x;
200
- lastControl2Y = c2y;
201
- currentX = ax;
202
- currentY = ay;
203
- lastKind = 'S';
225
+ lastKind = 77;
204
226
  }
205
- else if (upper === 'Q') {
206
- const x1 = readNumber();
207
- const y1 = readNumber();
208
- const x = readNumber();
209
- const y = readNumber();
210
- if (x1 === null || y1 === null || x === null || y === null)
211
- return false;
212
- const cx = relative ? currentX + x1 : x1;
213
- const cy = relative ? currentY + y1 : y1;
214
- const ax = relative ? currentX + x : x;
215
- const ay = relative ? currentY + y : y;
216
- appendPathCurveTo(path, cx, cy, ax, ay);
217
- lastQuadControlX = cx;
218
- lastQuadControlY = cy;
219
- currentX = ax;
220
- currentY = ay;
221
- lastKind = 'Q';
222
- }
223
- else if (upper === 'T') {
224
- const x = readNumber();
225
- const y = readNumber();
226
- if (x === null || y === null)
227
- return false;
228
- const reflect = lastKind === 'Q' || lastKind === 'T';
229
- const cx = reflect ? 2 * currentX - lastQuadControlX : currentX;
230
- const cy = reflect ? 2 * currentY - lastQuadControlY : currentY;
231
- const ax = relative ? currentX + x : x;
232
- const ay = relative ? currentY + y : y;
233
- appendPathCurveTo(path, cx, cy, ax, ay);
234
- lastQuadControlX = cx;
235
- lastQuadControlY = cy;
236
- currentX = ax;
237
- currentY = ay;
238
- lastKind = 'T';
239
- }
240
- else if (upper === 'A') {
241
- const rx = readNumber();
242
- const ry = readNumber();
243
- const rotationDegrees = readNumber();
244
- const largeArc = readFlag();
245
- const sweep = readFlag();
246
- const x = readNumber();
247
- const y = readNumber();
248
- if (rx === null ||
249
- ry === null ||
250
- rotationDegrees === null ||
251
- largeArc === null ||
252
- sweep === null ||
253
- x === null ||
254
- y === null) {
255
- return false;
227
+ else {
228
+ if (upper === 76) {
229
+ let nx = readNumber(reader);
230
+ let ny = readNumber(reader);
231
+ const $t0 = nx;
232
+ const $t1 = ny;
233
+ if ($t0 === undefined || $t1 === undefined) {
234
+ return explanation(reader.pos, 'expected-coordinate-pair');
235
+ }
236
+ nx = $t0;
237
+ ny = $t1;
238
+ currentX = relative ? currentX + nx : nx;
239
+ currentY = relative ? currentY + ny : ny;
240
+ appendPathLineTo(path, currentX, currentY);
241
+ lastKind = 76;
256
242
  }
257
- const ax = relative ? currentX + x : x;
258
- const ay = relative ? currentY + y : y;
259
- appendPathArcTo(path, rx, ry, (rotationDegrees * Math.PI) / 180, largeArc === 1, sweep === 1, ax, ay);
260
- currentX = ax;
261
- currentY = ay;
262
- lastKind = 'A';
243
+ else {
244
+ if (upper === 72) {
245
+ const nx = readNumber(reader);
246
+ if (nx === undefined) {
247
+ return explanation(reader.pos, 'expected-coordinate');
248
+ }
249
+ currentX = relative ? currentX + nx : nx;
250
+ appendPathLineTo(path, currentX, currentY);
251
+ lastKind = 76;
252
+ }
253
+ else {
254
+ if (upper === 86) {
255
+ const ny = readNumber(reader);
256
+ if (ny === undefined) {
257
+ return explanation(reader.pos, 'expected-coordinate');
258
+ }
259
+ currentY = relative ? currentY + ny : ny;
260
+ appendPathLineTo(path, currentX, currentY);
261
+ lastKind = 76;
262
+ }
263
+ else {
264
+ if (upper === 67) {
265
+ let x1 = readNumber(reader);
266
+ let y1 = readNumber(reader);
267
+ let x2 = readNumber(reader);
268
+ let y2 = readNumber(reader);
269
+ let x = readNumber(reader);
270
+ let y = readNumber(reader);
271
+ const $t0 = x1;
272
+ const $t1 = y1;
273
+ const $t2 = x2;
274
+ const $t3 = y2;
275
+ const $t4 = x;
276
+ const $t5 = y;
277
+ if ($t0 === undefined ||
278
+ $t1 === undefined ||
279
+ $t2 === undefined ||
280
+ $t3 === undefined ||
281
+ $t4 === undefined ||
282
+ $t5 === undefined) {
283
+ return explanation(reader.pos, 'expected-cubic-coordinates');
284
+ }
285
+ x1 = $t0;
286
+ y1 = $t1;
287
+ x2 = $t2;
288
+ y2 = $t3;
289
+ x = $t4;
290
+ y = $t5;
291
+ const c1x = relative ? currentX + x1 : x1;
292
+ const c1y = relative ? currentY + y1 : y1;
293
+ const c2x = relative ? currentX + x2 : x2;
294
+ const c2y = relative ? currentY + y2 : y2;
295
+ const ax = relative ? currentX + x : x;
296
+ const ay = relative ? currentY + y : y;
297
+ appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
298
+ lastControl2X = c2x;
299
+ lastControl2Y = c2y;
300
+ currentX = ax;
301
+ currentY = ay;
302
+ lastKind = 67;
303
+ }
304
+ else {
305
+ if (upper === 83) {
306
+ let x2 = readNumber(reader);
307
+ let y2 = readNumber(reader);
308
+ let x = readNumber(reader);
309
+ let y = readNumber(reader);
310
+ const $t0 = x2;
311
+ const $t1 = y2;
312
+ const $t2 = x;
313
+ const $t3 = y;
314
+ if ($t0 === undefined || $t1 === undefined || $t2 === undefined || $t3 === undefined) {
315
+ return explanation(reader.pos, 'expected-smooth-cubic-coordinates');
316
+ }
317
+ x2 = $t0;
318
+ y2 = $t1;
319
+ x = $t2;
320
+ y = $t3;
321
+ const reflect = lastKind === 67 || lastKind === 83;
322
+ const c1x = reflect ? 2.0 * currentX - lastControl2X : currentX;
323
+ const c1y = reflect ? 2.0 * currentY - lastControl2Y : currentY;
324
+ const c2x = relative ? currentX + x2 : x2;
325
+ const c2y = relative ? currentY + y2 : y2;
326
+ const ax = relative ? currentX + x : x;
327
+ const ay = relative ? currentY + y : y;
328
+ appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
329
+ lastControl2X = c2x;
330
+ lastControl2Y = c2y;
331
+ currentX = ax;
332
+ currentY = ay;
333
+ lastKind = 83;
334
+ }
335
+ else {
336
+ if (upper === 81) {
337
+ let x1 = readNumber(reader);
338
+ let y1 = readNumber(reader);
339
+ let x = readNumber(reader);
340
+ let y = readNumber(reader);
341
+ const $t0 = x1;
342
+ const $t1 = y1;
343
+ const $t2 = x;
344
+ const $t3 = y;
345
+ if ($t0 === undefined || $t1 === undefined || $t2 === undefined || $t3 === undefined) {
346
+ return explanation(reader.pos, 'expected-quadratic-coordinates');
347
+ }
348
+ x1 = $t0;
349
+ y1 = $t1;
350
+ x = $t2;
351
+ y = $t3;
352
+ const cx = relative ? currentX + x1 : x1;
353
+ const cy = relative ? currentY + y1 : y1;
354
+ const ax = relative ? currentX + x : x;
355
+ const ay = relative ? currentY + y : y;
356
+ appendPathQuadraticCurveTo(path, cx, cy, ax, ay);
357
+ lastQuadControlX = cx;
358
+ lastQuadControlY = cy;
359
+ currentX = ax;
360
+ currentY = ay;
361
+ lastKind = 81;
362
+ }
363
+ else {
364
+ if (upper === 84) {
365
+ let x = readNumber(reader);
366
+ let y = readNumber(reader);
367
+ const $t0 = x;
368
+ const $t1 = y;
369
+ if ($t0 === undefined || $t1 === undefined) {
370
+ return explanation(reader.pos, 'expected-coordinate-pair');
371
+ }
372
+ x = $t0;
373
+ y = $t1;
374
+ const reflect = lastKind === 81 || lastKind === 84;
375
+ const cx = reflect ? 2.0 * currentX - lastQuadControlX : currentX;
376
+ const cy = reflect ? 2.0 * currentY - lastQuadControlY : currentY;
377
+ const ax = relative ? currentX + x : x;
378
+ const ay = relative ? currentY + y : y;
379
+ appendPathQuadraticCurveTo(path, cx, cy, ax, ay);
380
+ lastQuadControlX = cx;
381
+ lastQuadControlY = cy;
382
+ currentX = ax;
383
+ currentY = ay;
384
+ lastKind = 84;
385
+ }
386
+ else {
387
+ if (upper === 65) {
388
+ let rx = readNumber(reader);
389
+ let ry = readNumber(reader);
390
+ let rotationDegrees = readNumber(reader);
391
+ let largeArc = readFlag(reader);
392
+ let sweep = readFlag(reader);
393
+ let x = readNumber(reader);
394
+ let y = readNumber(reader);
395
+ const $t0 = rx;
396
+ const $t1 = ry;
397
+ const $t2 = rotationDegrees;
398
+ const $t3 = largeArc;
399
+ const $t4 = sweep;
400
+ const $t5 = x;
401
+ const $t6 = y;
402
+ if ($t0 === undefined ||
403
+ $t1 === undefined ||
404
+ $t2 === undefined ||
405
+ $t3 === undefined ||
406
+ $t4 === undefined ||
407
+ $t5 === undefined ||
408
+ $t6 === undefined) {
409
+ return explanation(reader.pos, 'expected-arc-parameters');
410
+ }
411
+ rx = $t0;
412
+ ry = $t1;
413
+ rotationDegrees = $t2;
414
+ largeArc = $t3;
415
+ sweep = $t4;
416
+ x = $t5;
417
+ y = $t6;
418
+ const ax = relative ? currentX + x : x;
419
+ const ay = relative ? currentY + y : y;
420
+ if (lastKind === 90) {
421
+ appendPathMoveTo(path, currentX, currentY);
422
+ }
423
+ appendPathEllipticalArcTo(path, rx, ry, (rotationDegrees * Math.PI) / 180.0, largeArc, sweep, ax, ay);
424
+ currentX = ax;
425
+ currentY = ay;
426
+ lastKind = 65;
427
+ }
428
+ else {
429
+ return explanation(reader.pos - 1, 'unsupported-command');
430
+ }
431
+ }
432
+ }
433
+ }
434
+ }
435
+ }
436
+ }
437
+ }
438
+ }
439
+ first = false;
440
+ if (active === 77) {
441
+ active = 76;
263
442
  }
264
443
  else {
265
- return false;
444
+ if (active === 109) {
445
+ active = 108;
446
+ }
266
447
  }
267
- first = false;
268
- if (active === 'M')
269
- active = 'L';
270
- else if (active === 'm')
271
- active = 'l';
272
448
  }
273
449
  }
274
- return true;
450
+ return undefined;
275
451
  }
276
- /**
277
- * Serializes a `Path` to an SVG path `d` string, emitting absolute commands. Each `PathSegment`
278
- * maps to its SVG verb: `moveTo`→`M`, `lineTo`→`L`, `curveTo` (quadratic)→`Q`, `cubicCurveTo`→`C`,
279
- * `close`→`Z`. Arcs are not a distinct segment kind — the path stores them as cubics — so no `A`
280
- * command is produced.
281
- *
282
- * `options.precision`, when given, rounds every coordinate to that many decimal places and drops
283
- * trailing zeros; the default emits full-precision numbers (also trailing-zero-free).
284
- */
285
- export function formatSvgPathData(path, options) {
286
- const precision = options?.precision;
287
- const parts = [];
288
- forEachPathSegment(path, (segment) => {
289
- if (segment.kind === 'moveTo') {
290
- parts.push(`M${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
291
- }
292
- else if (segment.kind === 'lineTo') {
293
- parts.push(`L${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
452
+ function readFlag(reader) {
453
+ skipSeparators(reader);
454
+ if (isUnit(reader, 48, 48)) {
455
+ reader.pos += 1;
456
+ return false;
457
+ }
458
+ if (isUnit(reader, 49, 49)) {
459
+ reader.pos += 1;
460
+ return true;
461
+ }
462
+ return undefined;
463
+ }
464
+ function readNumber(reader) {
465
+ skipSeparators(reader);
466
+ const start = reader.pos;
467
+ if (isUnit(reader, 43, 45)) {
468
+ reader.pos += 1;
469
+ }
470
+ let sawDigit = false;
471
+ while (isDigit(reader)) {
472
+ reader.pos += 1;
473
+ sawDigit = true;
474
+ }
475
+ if (isUnit(reader, 46, 46)) {
476
+ reader.pos += 1;
477
+ while (isDigit(reader)) {
478
+ reader.pos += 1;
479
+ sawDigit = true;
294
480
  }
295
- else if (segment.kind === 'curveTo') {
296
- parts.push(`Q${formatSvgNumber(segment.controlX, precision)} ${formatSvgNumber(segment.controlY, precision)} ` +
297
- `${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
481
+ }
482
+ if (!sawDigit) {
483
+ reader.pos = start;
484
+ return undefined;
485
+ }
486
+ if (isUnit(reader, 101, 69)) {
487
+ const expStart = reader.pos;
488
+ reader.pos += 1;
489
+ if (isUnit(reader, 43, 45)) {
490
+ reader.pos += 1;
298
491
  }
299
- else if (segment.kind === 'cubicCurveTo') {
300
- parts.push(`C${formatSvgNumber(segment.control1X, precision)} ${formatSvgNumber(segment.control1Y, precision)} ` +
301
- `${formatSvgNumber(segment.control2X, precision)} ${formatSvgNumber(segment.control2Y, precision)} ` +
302
- `${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
492
+ let expDigit = false;
493
+ while (isDigit(reader)) {
494
+ reader.pos += 1;
495
+ expDigit = true;
303
496
  }
304
- else if (segment.kind === 'close') {
305
- parts.push('Z');
497
+ if (!expDigit) {
498
+ reader.pos = expStart;
306
499
  }
307
- });
308
- return parts.join('');
309
- }
310
- /**
311
- * Parses an SVG path `d` string into a freshly allocated `Path`, or returns `null` on structurally
312
- * malformed input (see `appendSvgPathData` for the malformed cases). An empty or whitespace-only
313
- * string yields an empty path, not `null`.
314
- */
315
- export function parseSvgPathData(d) {
316
- const path = createPath();
317
- if (!appendSvgPathData(path, d))
318
- return null;
319
- return path;
500
+ }
501
+ const token = ((units) => {
502
+ let value = '';
503
+ for (let index = 0; index < units.length; index += 0x8000)
504
+ value += String.fromCharCode(...units.slice(index, index + 0x8000));
505
+ return value;
506
+ })(reader.units.slice(start, reader.pos));
507
+ const value = ((s) => {
508
+ const v = Number(s);
509
+ return s.trim() !== '' && !Number.isNaN(v) ? v : undefined;
510
+ })(token);
511
+ if (value === undefined) {
512
+ return undefined;
513
+ }
514
+ return Number.isFinite(value) ? value : undefined;
320
515
  }
321
- function formatSvgNumber(value, precision) {
322
- if (precision === undefined)
323
- return String(value);
324
- const factor = 10 ** precision;
325
- // String() of the rounded number drops trailing zeros and normalizes -0 to "0".
326
- return String(Math.round(value * factor) / factor);
516
+ function roundJs(value) {
517
+ if (!Number.isFinite(value)) {
518
+ return value;
519
+ }
520
+ const floor = Math.floor(value);
521
+ const rounded = value - floor >= 0.5 ? floor + 1.0 : floor;
522
+ return rounded === 0.0 && value < 0.0 ? -0.0 : rounded;
327
523
  }
328
- function isSvgCommandLetter(c) {
329
- return 'MmLlHhVvCcSsQqTtAaZz'.indexOf(c) !== -1;
524
+ function skipSeparators(reader) {
525
+ while (reader.pos < reader.units.length) {
526
+ {
527
+ const matchValue = reader.units[reader.pos];
528
+ if (matchValue === 32 ||
529
+ matchValue === 9 ||
530
+ matchValue === 10 ||
531
+ matchValue === 13 ||
532
+ matchValue === 12 ||
533
+ matchValue === 44) {
534
+ reader.pos += 1;
535
+ }
536
+ else {
537
+ break;
538
+ }
539
+ }
540
+ }
330
541
  }
331
542
  //# sourceMappingURL=svgPathData.js.map