@flighthq/path-formats 0.5.1-next.973.788d3d7 → 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,361 +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). A failed parse leaves `path`
9
- * unchanged; returns `true` when the whole string parses. An empty or whitespace-only string is
10
- * 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.
11
11
  *
12
- * Elliptic arcs (`A`/`a`) are appended through `appendPathArcTo`, which approximates them as cubic
13
- * bezier segments — a subsequent `forEachPathSegment` walk (and `formatSvgPathData`) therefore sees
14
- * 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.
15
16
  */
16
17
  export function appendSvgPathData(path, d) {
17
- const scratch = createPath();
18
- if (parseSvgPathDataInto(scratch, d) !== null)
18
+ let scratch = createPath('nonZero');
19
+ if (parseSvgPathDataInto(scratch, d) !== undefined) {
19
20
  return false;
20
- path.commands.push(...scratch.commands);
21
- path.data.push(...scratch.data);
21
+ }
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
+ }
22
28
  return true;
23
29
  }
24
30
  /**
25
- * Explains why an SVG path `d` string cannot be parsed. Returns `null` for well-formed input, or
26
- * the input position and a stable reason for malformed input.
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.
27
33
  */
28
34
  export function explainSvgPathData(d) {
29
- return parseSvgPathDataInto(createPath(), d);
35
+ return parseSvgPathDataInto(createPath('nonZero'), d);
30
36
  }
31
- function parseSvgPathDataInto(path, d) {
32
- const length = d.length;
33
- let pos = 0;
34
- // Current point, subpath start (for Z), and the last emitted control points in absolute space.
35
- // `lastControl2` feeds `S` reflection (valid only after C/S); `lastQuadControl` feeds `T`
36
- // reflection (valid only after Q/T). `lastKind` records the previous group's canonical letter so
37
- // a shorthand can decide whether reflection applies.
38
- let currentX = 0;
39
- let currentY = 0;
40
- let startX = 0;
41
- let startY = 0;
42
- let lastControl2X = 0;
43
- let lastControl2Y = 0;
44
- let lastQuadControlX = 0;
45
- let lastQuadControlY = 0;
46
- let lastKind = '';
47
- function skipSeparators() {
48
- while (pos < length) {
49
- const c = d.charCodeAt(pos);
50
- // space, tab, LF, CR, FF, or comma
51
- if (c === 32 || c === 9 || c === 10 || c === 13 || c === 12 || c === 44)
52
- pos++;
53
- else
54
- break;
55
- }
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);
56
43
  }
57
- function readNumber() {
58
- skipSeparators();
59
- const start = pos;
60
- if (pos < length && (d[pos] === '+' || d[pos] === '-'))
61
- pos++;
62
- let sawDigit = false;
63
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
64
- pos++;
65
- sawDigit = true;
66
- }
67
- if (pos < length && d[pos] === '.') {
68
- pos++;
69
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
70
- pos++;
71
- sawDigit = true;
72
- }
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
+ })();
73
68
  }
74
- if (!sawDigit) {
75
- pos = start;
76
- return null;
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
+ })();
77
75
  }
78
- if (pos < length && (d[pos] === 'e' || d[pos] === 'E')) {
79
- const expStart = pos;
80
- pos++;
81
- if (pos < length && (d[pos] === '+' || d[pos] === '-'))
82
- pos++;
83
- let expDigit = false;
84
- while (pos < length && d[pos] >= '0' && d[pos] <= '9') {
85
- pos++;
86
- expDigit = true;
87
- }
88
- if (!expDigit)
89
- pos = expStart;
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
+ })();
90
84
  }
91
- const value = Number.parseFloat(d.slice(start, pos));
92
- return Number.isFinite(value) ? value : null;
93
- }
94
- // An arc flag is a single '0' or '1' character, which may be packed against neighbours with no
95
- // separator (`0110` = four flags), so it is read one char at a time rather than as a number.
96
- function readFlag() {
97
- skipSeparators();
98
- if (pos < length && d[pos] === '0') {
99
- pos++;
100
- return 0;
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
+ })();
101
95
  }
102
- if (pos < length && d[pos] === '1') {
103
- pos++;
104
- return 1;
96
+ else if (matchValue.kind === 'close') {
97
+ return parts.push('Z');
105
98
  }
106
- return null;
99
+ })());
100
+ return parts.join('');
101
+ }
102
+ function getJsNumberString(value) {
103
+ if (Number.isNaN(value)) {
104
+ return 'NaN';
107
105
  }
108
- while (true) {
109
- skipSeparators();
110
- if (pos >= length)
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;
161
+ }
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) {
111
179
  break;
112
- const commandLetter = d[pos];
113
- if (!isSvgCommandLetter(commandLetter))
114
- return { position: pos, reason: 'expected-command' };
115
- pos++;
116
- // Path data must open with a moveto.
117
- if (lastKind === '' && commandLetter !== 'M' && commandLetter !== 'm') {
118
- return { position: pos - 1, reason: 'expected-moveto' };
119
180
  }
120
- if (commandLetter === 'Z' || commandLetter === 'z') {
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) {
121
190
  appendPathClose(path);
122
191
  currentX = startX;
123
192
  currentY = startY;
124
- lastKind = 'Z';
193
+ lastKind = 90;
125
194
  continue;
126
195
  }
127
- // Execute one or more operand groups. After the first `M`/`m` group, implicit repeats are
128
- // lineto groups (SVG spec), so the active letter shifts from M→L (or m→l).
129
- let active = commandLetter;
196
+ let active = commandLetter & 0xff;
130
197
  let first = true;
131
- while (true) {
198
+ for (;;) {
132
199
  if (!first) {
133
- skipSeparators();
134
- if (pos >= length)
200
+ skipSeparators(reader);
201
+ if (reader.pos >= length) {
135
202
  break;
136
- // Any letter ends the current operand run. The outer command reader then distinguishes a
137
- // supported command from an unknown one and can report the latter at its actual position.
138
- if (isAsciiLetter(d[pos]))
203
+ }
204
+ if (isAsciiLetter(reader.units[reader.pos])) {
139
205
  break;
206
+ }
140
207
  }
141
- const relative = active >= 'a';
142
- const upper = relative ? active.toUpperCase() : active;
143
- if (upper === 'M') {
144
- const nx = readNumber();
145
- const ny = readNumber();
146
- if (nx === null || ny === null)
147
- return { position: pos, reason: 'expected-coordinate-pair' };
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;
148
220
  currentX = relative ? currentX + nx : nx;
149
221
  currentY = relative ? currentY + ny : ny;
150
222
  startX = currentX;
151
223
  startY = currentY;
152
224
  appendPathMoveTo(path, currentX, currentY);
153
- lastKind = 'M';
154
- }
155
- else if (upper === 'L') {
156
- const nx = readNumber();
157
- const ny = readNumber();
158
- if (nx === null || ny === null)
159
- return { position: pos, reason: 'expected-coordinate-pair' };
160
- currentX = relative ? currentX + nx : nx;
161
- currentY = relative ? currentY + ny : ny;
162
- appendPathLineTo(path, currentX, currentY);
163
- lastKind = 'L';
164
- }
165
- else if (upper === 'H') {
166
- const nx = readNumber();
167
- if (nx === null)
168
- return { position: pos, reason: 'expected-coordinate' };
169
- currentX = relative ? currentX + nx : nx;
170
- appendPathLineTo(path, currentX, currentY);
171
- lastKind = 'L';
172
- }
173
- else if (upper === 'V') {
174
- const ny = readNumber();
175
- if (ny === null)
176
- return { position: pos, reason: 'expected-coordinate' };
177
- currentY = relative ? currentY + ny : ny;
178
- appendPathLineTo(path, currentX, currentY);
179
- lastKind = 'L';
225
+ lastKind = 77;
180
226
  }
181
- else if (upper === 'C') {
182
- const x1 = readNumber();
183
- const y1 = readNumber();
184
- const x2 = readNumber();
185
- const y2 = readNumber();
186
- const x = readNumber();
187
- const y = readNumber();
188
- if (x1 === null || y1 === null || x2 === null || y2 === null || x === null || y === null) {
189
- return { position: pos, reason: 'expected-cubic-coordinates' };
190
- }
191
- const c1x = relative ? currentX + x1 : x1;
192
- const c1y = relative ? currentY + y1 : y1;
193
- const c2x = relative ? currentX + x2 : x2;
194
- const c2y = relative ? currentY + y2 : y2;
195
- const ax = relative ? currentX + x : x;
196
- const ay = relative ? currentY + y : y;
197
- appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
198
- lastControl2X = c2x;
199
- lastControl2Y = c2y;
200
- currentX = ax;
201
- currentY = ay;
202
- lastKind = 'C';
203
- }
204
- else if (upper === 'S') {
205
- const x2 = readNumber();
206
- const y2 = readNumber();
207
- const x = readNumber();
208
- const y = readNumber();
209
- if (x2 === null || y2 === null || x === null || y === null) {
210
- return { position: pos, reason: 'expected-smooth-cubic-coordinates' };
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;
211
242
  }
212
- const reflect = lastKind === 'C' || lastKind === 'S';
213
- const c1x = reflect ? 2 * currentX - lastControl2X : currentX;
214
- const c1y = reflect ? 2 * currentY - lastControl2Y : currentY;
215
- const c2x = relative ? currentX + x2 : x2;
216
- const c2y = relative ? currentY + y2 : y2;
217
- const ax = relative ? currentX + x : x;
218
- const ay = relative ? currentY + y : y;
219
- appendPathCubicCurveTo(path, c1x, c1y, c2x, c2y, ax, ay);
220
- lastControl2X = c2x;
221
- lastControl2Y = c2y;
222
- currentX = ax;
223
- currentY = ay;
224
- lastKind = 'S';
225
- }
226
- else if (upper === 'Q') {
227
- const x1 = readNumber();
228
- const y1 = readNumber();
229
- const x = readNumber();
230
- const y = readNumber();
231
- if (x1 === null || y1 === null || x === null || y === null) {
232
- return { position: pos, reason: 'expected-quadratic-coordinates' };
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
+ }
233
437
  }
234
- const cx = relative ? currentX + x1 : x1;
235
- const cy = relative ? currentY + y1 : y1;
236
- const ax = relative ? currentX + x : x;
237
- const ay = relative ? currentY + y : y;
238
- appendPathCurveTo(path, cx, cy, ax, ay);
239
- lastQuadControlX = cx;
240
- lastQuadControlY = cy;
241
- currentX = ax;
242
- currentY = ay;
243
- lastKind = 'Q';
244
- }
245
- else if (upper === 'T') {
246
- const x = readNumber();
247
- const y = readNumber();
248
- if (x === null || y === null)
249
- return { position: pos, reason: 'expected-coordinate-pair' };
250
- const reflect = lastKind === 'Q' || lastKind === 'T';
251
- const cx = reflect ? 2 * currentX - lastQuadControlX : currentX;
252
- const cy = reflect ? 2 * currentY - lastQuadControlY : currentY;
253
- const ax = relative ? currentX + x : x;
254
- const ay = relative ? currentY + y : y;
255
- appendPathCurveTo(path, cx, cy, ax, ay);
256
- lastQuadControlX = cx;
257
- lastQuadControlY = cy;
258
- currentX = ax;
259
- currentY = ay;
260
- lastKind = 'T';
261
438
  }
262
- else if (upper === 'A') {
263
- const rx = readNumber();
264
- const ry = readNumber();
265
- const rotationDegrees = readNumber();
266
- const largeArc = readFlag();
267
- const sweep = readFlag();
268
- const x = readNumber();
269
- const y = readNumber();
270
- if (rx === null ||
271
- ry === null ||
272
- rotationDegrees === null ||
273
- largeArc === null ||
274
- sweep === null ||
275
- x === null ||
276
- y === null) {
277
- return { position: pos, reason: 'expected-arc-parameters' };
278
- }
279
- const ax = relative ? currentX + x : x;
280
- const ay = relative ? currentY + y : y;
281
- // appendPathArcTo discovers its start through the path's last stored point. CLOSE stores no
282
- // coordinates, so anchor the SVG pen at the subpath origin before asking it to build cubics.
283
- if (lastKind === 'Z')
284
- appendPathMoveTo(path, currentX, currentY);
285
- appendPathArcTo(path, rx, ry, (rotationDegrees * Math.PI) / 180, largeArc === 1, sweep === 1, ax, ay);
286
- currentX = ax;
287
- currentY = ay;
288
- lastKind = 'A';
439
+ first = false;
440
+ if (active === 77) {
441
+ active = 76;
289
442
  }
290
443
  else {
291
- return { position: pos - 1, reason: 'unsupported-command' };
444
+ if (active === 109) {
445
+ active = 108;
446
+ }
292
447
  }
293
- first = false;
294
- if (active === 'M')
295
- active = 'L';
296
- else if (active === 'm')
297
- active = 'l';
298
448
  }
299
449
  }
300
- return null;
450
+ return undefined;
301
451
  }
302
- /**
303
- * Serializes a `Path` to an SVG path `d` string, emitting absolute commands. Each `PathSegment`
304
- * maps to its SVG verb: `moveTo`→`M`, `lineTo`→`L`, `curveTo` (quadratic)→`Q`, `cubicCurveTo`→`C`,
305
- * `close`→`Z`. Arcs are not a distinct segment kind — the path stores them as cubics — so no `A`
306
- * command is produced.
307
- *
308
- * `options.precision`, when given, rounds every coordinate to that many decimal places and drops
309
- * trailing zeros; the default emits full-precision numbers (also trailing-zero-free).
310
- */
311
- export function formatSvgPathData(path, options) {
312
- const precision = options?.precision;
313
- const parts = [];
314
- forEachPathSegment(path, (segment) => {
315
- if (segment.kind === 'moveTo') {
316
- parts.push(`M${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
317
- }
318
- else if (segment.kind === 'lineTo') {
319
- 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;
320
480
  }
321
- else if (segment.kind === 'curveTo') {
322
- parts.push(`Q${formatSvgNumber(segment.controlX, precision)} ${formatSvgNumber(segment.controlY, precision)} ` +
323
- `${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;
324
491
  }
325
- else if (segment.kind === 'cubicCurveTo') {
326
- parts.push(`C${formatSvgNumber(segment.control1X, precision)} ${formatSvgNumber(segment.control1Y, precision)} ` +
327
- `${formatSvgNumber(segment.control2X, precision)} ${formatSvgNumber(segment.control2Y, precision)} ` +
328
- `${formatSvgNumber(segment.x, precision)} ${formatSvgNumber(segment.y, precision)}`);
492
+ let expDigit = false;
493
+ while (isDigit(reader)) {
494
+ reader.pos += 1;
495
+ expDigit = true;
329
496
  }
330
- else if (segment.kind === 'close') {
331
- parts.push('Z');
497
+ if (!expDigit) {
498
+ reader.pos = expStart;
332
499
  }
333
- });
334
- return parts.join('');
335
- }
336
- /**
337
- * Parses an SVG path `d` string into a freshly allocated `Path`, or returns `null` on structurally
338
- * malformed input (see `appendSvgPathData` for the malformed cases). An empty or whitespace-only
339
- * string yields an empty path, not `null`.
340
- */
341
- export function parseSvgPathData(d) {
342
- const path = createPath();
343
- if (parseSvgPathDataInto(path, d) !== null)
344
- return null;
345
- return path;
346
- }
347
- function formatSvgNumber(value, precision) {
348
- if (precision === undefined)
349
- return String(value);
350
- const factor = 10 ** precision;
351
- // String() of the rounded number drops trailing zeros and normalizes -0 to "0".
352
- return String(Math.round(value * factor) / factor);
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;
353
515
  }
354
- function isSvgCommandLetter(c) {
355
- return 'MmLlHhVvCcSsQqTtAaZz'.indexOf(c) !== -1;
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;
356
523
  }
357
- function isAsciiLetter(c) {
358
- const code = c.charCodeAt(0);
359
- return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
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
+ }
360
541
  }
361
542
  //# sourceMappingURL=svgPathData.js.map