@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.
- package/README.md +2 -2
- package/dist/contract.d.ts +2 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +1 -1
- package/dist/contract.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/svgPathData.d.ts +26 -19
- package/dist/svgPathData.d.ts.map +1 -1
- package/dist/svgPathData.js +497 -286
- package/dist/svgPathData.js.map +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +5 -4
- package/src/svgPathData.test.ts +351 -155
package/src/svgPathData.test.ts
CHANGED
|
@@ -1,222 +1,415 @@
|
|
|
1
|
-
import { createPath, forEachPathSegment } from '@flighthq/path/contract';
|
|
2
|
-
import type { Path, PathSegment } from '@flighthq/
|
|
1
|
+
import { PathCommand, createPath, forEachPathSegment } from '@flighthq/path/contract';
|
|
2
|
+
import type { Path, PathSegment } from '@flighthq/path/contract';
|
|
3
3
|
import { describe, expect, it } from 'vitest';
|
|
4
4
|
|
|
5
|
-
import { appendSvgPathData, formatSvgPathData, parseSvgPathData } from './
|
|
5
|
+
import { appendSvgPathData, explainSvgPathData, formatSvgPathData, parseSvgPathData } from './contract.ts';
|
|
6
|
+
import type { SvgPathDataExplanation, SvgPathDataFormatOptions } from './contract.ts';
|
|
6
7
|
|
|
7
8
|
function collectSegments(path: Readonly<Path>): PathSegment[] {
|
|
8
|
-
|
|
9
|
-
forEachPathSegment(path, (segment) => segments.push(segment));
|
|
9
|
+
let segments: PathSegment[] = [] as never[];
|
|
10
|
+
forEachPathSegment(path, (segment: PathSegment) => segments.push(segment));
|
|
10
11
|
return segments;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
function kinds(segments: Readonly<PathSegment[]>): string[] {
|
|
15
|
+
return segments.map((segment) => segment.kind);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function values(segment: Readonly<PathSegment>): number[] {
|
|
19
|
+
return (() => {
|
|
20
|
+
const matchValue = segment;
|
|
21
|
+
if (matchValue.kind === 'moveTo' || matchValue.kind === 'lineTo') {
|
|
22
|
+
const x = matchValue.x;
|
|
23
|
+
const y = matchValue.y;
|
|
24
|
+
return [x, y];
|
|
25
|
+
} else if (matchValue.kind === 'quadraticCurveTo') {
|
|
26
|
+
const controlX = matchValue.controlX;
|
|
27
|
+
const controlY = matchValue.controlY;
|
|
28
|
+
const x = matchValue.x;
|
|
29
|
+
const y = matchValue.y;
|
|
30
|
+
return [controlX, controlY, x, y];
|
|
31
|
+
} else if (matchValue.kind === 'cubicCurveTo') {
|
|
32
|
+
const controlX1 = matchValue.controlX1;
|
|
33
|
+
const controlY1 = matchValue.controlY1;
|
|
34
|
+
const controlX2 = matchValue.controlX2;
|
|
35
|
+
const controlY2 = matchValue.controlY2;
|
|
36
|
+
const x = matchValue.x;
|
|
37
|
+
const y = matchValue.y;
|
|
38
|
+
return [controlX1, controlY1, controlX2, controlY2, x, y];
|
|
39
|
+
} else if (matchValue.kind === 'close') {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
throw new Error('unknown path segment');
|
|
43
|
+
})();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function lastPoint(segments: Readonly<PathSegment[]>): readonly [number, number] {
|
|
47
|
+
const $letElse = segments[segments.length - 1];
|
|
48
|
+
if (!($letElse.kind === 'cubicCurveTo')) {
|
|
49
|
+
throw new Error(`expected a trailing cubic`);
|
|
50
|
+
}
|
|
51
|
+
const x = $letElse.x;
|
|
52
|
+
const y = $letElse.y;
|
|
53
|
+
return [x, y];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function precision(value: number | undefined): SvgPathDataFormatOptions {
|
|
57
|
+
return { precision: value };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function explained(position: number, reason: string): SvgPathDataExplanation | undefined {
|
|
61
|
+
return { position: position, reason: String(reason) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function parsed(d: string): PathSegment[] {
|
|
65
|
+
return collectSegments(
|
|
66
|
+
(() => {
|
|
67
|
+
const value = parseSvgPathData(d);
|
|
68
|
+
if (value === undefined) throw new Error('well-formed');
|
|
69
|
+
return value;
|
|
70
|
+
})(),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
13
74
|
describe('appendSvgPathData', () => {
|
|
14
75
|
it('appends into an existing path without clearing it', () => {
|
|
15
|
-
|
|
76
|
+
let path = createPath('nonZero');
|
|
16
77
|
appendSvgPathData(path, 'M0 0 L10 0');
|
|
17
78
|
const ok = appendSvgPathData(path, 'M20 20 L30 20');
|
|
18
79
|
expect(ok).toBe(true);
|
|
19
|
-
|
|
20
|
-
expect(segments.map((s) => s.kind)).toEqual(['moveTo', 'lineTo', 'moveTo', 'lineTo']);
|
|
80
|
+
expect(kinds(collectSegments(path))).toEqual(['moveTo', 'lineTo', 'moveTo', 'lineTo']);
|
|
21
81
|
});
|
|
22
|
-
|
|
23
82
|
it('returns false on malformed input', () => {
|
|
24
|
-
|
|
25
|
-
expect(appendSvgPathData(path, 'L10 10')).toBe(
|
|
83
|
+
let path = createPath('nonZero');
|
|
84
|
+
expect(!appendSvgPathData(path, 'L10 10')).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
it('does not partially mutate the destination on malformed input', () => {
|
|
87
|
+
let path = createPath('nonZero');
|
|
88
|
+
appendSvgPathData(path, 'M1 2 L3 4');
|
|
89
|
+
const commands = path.commands;
|
|
90
|
+
const data = path.data;
|
|
91
|
+
expect(!appendSvgPathData(path, 'M10 20 L30')).toBe(true);
|
|
92
|
+
expect(path.commands).toEqual(commands);
|
|
93
|
+
expect(path.data).toEqual(data);
|
|
94
|
+
});
|
|
95
|
+
it('appends nothing for empty input', () => {
|
|
96
|
+
let path = createPath('nonZero');
|
|
97
|
+
expect(appendSvgPathData(path, ' \t,')).toBe(true);
|
|
98
|
+
expect(path.commands.length === 0 && path.data.length === 0).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('explainSvgPathData', () => {
|
|
103
|
+
it('returns null for valid input and locates malformed input', () => {
|
|
104
|
+
expect(explainSvgPathData('M0 0 L1 1')).toEqual(undefined);
|
|
105
|
+
expect(explainSvgPathData('L10 10')).toEqual(explained(0, 'expected-moveto'));
|
|
106
|
+
expect(explainSvgPathData('M0 0 X10 10')).toEqual(explained(5, 'expected-command'));
|
|
107
|
+
});
|
|
108
|
+
it('reports utf16 code unit positions', () => {
|
|
109
|
+
expect(explainSvgPathData('M0 0Z😀')).toEqual(explained(5, 'expected-command'));
|
|
110
|
+
expect(explainSvgPathData('😀')).toEqual(explained(0, 'expected-command'));
|
|
111
|
+
expect(explainSvgPathData('M0 0 é')).toEqual(explained(5, 'expected-coordinate-pair'));
|
|
112
|
+
});
|
|
113
|
+
it('reports each stable reason at the source position', () => {
|
|
114
|
+
expect(explainSvgPathData('M0 0 L10')).toEqual(explained(8, 'expected-coordinate-pair'));
|
|
115
|
+
expect(explainSvgPathData('M')).toEqual(explained(1, 'expected-coordinate-pair'));
|
|
116
|
+
expect(explainSvgPathData('M0 0 H')).toEqual(explained(6, 'expected-coordinate'));
|
|
117
|
+
expect(explainSvgPathData('M0 0 C1 2 3 4 5')).toEqual(explained(15, 'expected-cubic-coordinates'));
|
|
118
|
+
expect(explainSvgPathData('M0 0 S1 2 3')).toEqual(explained(11, 'expected-smooth-cubic-coordinates'));
|
|
119
|
+
expect(explainSvgPathData('M0 0 Q1 2 3')).toEqual(explained(11, 'expected-quadratic-coordinates'));
|
|
120
|
+
expect(explainSvgPathData('M0 0 T1')).toEqual(explained(7, 'expected-coordinate-pair'));
|
|
121
|
+
expect(explainSvgPathData('M0 0 A5 5 0 0 1 10')).toEqual(explained(18, 'expected-arc-parameters'));
|
|
122
|
+
expect(explainSvgPathData(' z')).toEqual(explained(2, 'expected-moveto'));
|
|
123
|
+
});
|
|
124
|
+
it('keeps reading operands after a failed flag as the source does', () => {
|
|
125
|
+
expect(explainSvgPathData('M0 0 A1 1 0 5 6')).toEqual(explained(15, 'expected-arc-parameters'));
|
|
126
|
+
});
|
|
127
|
+
it('stops an operand run at any letter', () => {
|
|
128
|
+
expect(explainSvgPathData('M0 0 1 1 k')).toEqual(explained(9, 'expected-command'));
|
|
129
|
+
expect(explainSvgPathData('M0 0 L1 1 #')).toEqual(explained(10, 'expected-coordinate-pair'));
|
|
26
130
|
});
|
|
27
131
|
});
|
|
28
132
|
|
|
29
133
|
describe('formatSvgPathData', () => {
|
|
30
|
-
it('emits absolute
|
|
31
|
-
|
|
134
|
+
it('emits absolute m l c q z commands per segment kind', () => {
|
|
135
|
+
let path = createPath('nonZero');
|
|
32
136
|
appendSvgPathData(path, 'M0 0 L10 0 Q15 5 20 0 C22 2 24 2 26 0 Z');
|
|
33
|
-
expect(formatSvgPathData(path)).
|
|
137
|
+
expect(formatSvgPathData(path, undefined)).toEqual('M0 0L10 0Q15 5 20 0C22 2 24 2 26 0Z');
|
|
34
138
|
});
|
|
35
|
-
|
|
36
139
|
it('rounds coordinates and trims trailing zeros with a precision option', () => {
|
|
37
|
-
|
|
140
|
+
let path = createPath('nonZero');
|
|
38
141
|
appendSvgPathData(path, 'M0.123456 0.5 L10.987654 20');
|
|
39
|
-
expect(formatSvgPathData(path,
|
|
142
|
+
expect(formatSvgPathData(path, precision(2.0))).toEqual('M0.12 0.5L10.99 20');
|
|
40
143
|
});
|
|
41
|
-
|
|
42
144
|
it('emits full precision by default', () => {
|
|
43
|
-
|
|
145
|
+
let path = createPath('nonZero');
|
|
44
146
|
appendSvgPathData(path, 'M0.5 0.25 L1.5 2.75');
|
|
45
|
-
expect(formatSvgPathData(path)).
|
|
147
|
+
expect(formatSvgPathData(path, undefined)).toEqual('M0.5 0.25L1.5 2.75');
|
|
148
|
+
expect(formatSvgPathData(path, precision(undefined))).toEqual('M0.5 0.25L1.5 2.75');
|
|
46
149
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const path = createPath();
|
|
150
|
+
it('emits no a command arcs are stored as cubics', () => {
|
|
151
|
+
let path = createPath('nonZero');
|
|
50
152
|
appendSvgPathData(path, 'M0 0 A5 5 0 0 1 10 0');
|
|
51
|
-
const d = formatSvgPathData(path);
|
|
52
|
-
expect(d
|
|
153
|
+
const d = formatSvgPathData(path, undefined);
|
|
154
|
+
expect(!d.includes('A')).toBe(true);
|
|
53
155
|
expect(d.startsWith('M0 0C')).toBe(true);
|
|
54
156
|
});
|
|
157
|
+
it('keeps an explicit zero precision distinct from omitted', () => {
|
|
158
|
+
let path = createPath('nonZero');
|
|
159
|
+
appendSvgPathData(path, 'M1.5 -1.5 L2.4 -2.5');
|
|
160
|
+
expect(formatSvgPathData(path, undefined)).toEqual('M1.5 -1.5L2.4 -2.5');
|
|
161
|
+
expect(formatSvgPathData(path, precision(0.0))).toEqual('M2 -1L2 -2');
|
|
162
|
+
});
|
|
163
|
+
it('spells numbers as javascript string does', () => {
|
|
164
|
+
let path = createPath('nonZero');
|
|
165
|
+
appendSvgPathData(path, 'M1e21 1e-7 L123456789012345680000 0.000001 L-0 0.1');
|
|
166
|
+
expect(formatSvgPathData(path, undefined)).toEqual('M1e+21 1e-7L123456789012345680000 0.000001L0 0.1');
|
|
167
|
+
let tiny = createPath('nonZero');
|
|
168
|
+
appendSvgPathData(tiny, 'M1.5e-10 -2.25e30 L5e-324 1.7976931348623157e308');
|
|
169
|
+
expect(formatSvgPathData(tiny, undefined)).toEqual('M1.5e-10 -2.25e+30L5e-324 1.7976931348623157e+308');
|
|
170
|
+
});
|
|
171
|
+
it('normalizes a rounded negative zero to zero', () => {
|
|
172
|
+
let path = createPath('nonZero');
|
|
173
|
+
appendSvgPathData(path, 'M-0.001 -0.004');
|
|
174
|
+
expect(formatSvgPathData(path, precision(2.0))).toEqual('M0 0');
|
|
175
|
+
});
|
|
176
|
+
it('rounds to tens with a negative precision', () => {
|
|
177
|
+
let path = createPath('nonZero');
|
|
178
|
+
appendSvgPathData(path, 'M123 125 L-125 -0.4');
|
|
179
|
+
expect(formatSvgPathData(path, precision(-1.0))).toEqual('M120 130L-120 0');
|
|
180
|
+
});
|
|
181
|
+
it('applies a fractional precision through the power of ten', () => {
|
|
182
|
+
let path = createPath('nonZero');
|
|
183
|
+
appendSvgPathData(path, 'M1 0');
|
|
184
|
+
const d = formatSvgPathData(path, precision(0.5));
|
|
185
|
+
const x: number = ((s) => {
|
|
186
|
+
const v = Number(s);
|
|
187
|
+
return s.trim() !== '' && !Number.isNaN(v) ? v : undefined;
|
|
188
|
+
})(
|
|
189
|
+
d.slice(
|
|
190
|
+
1,
|
|
191
|
+
((value: string, pattern: string): number | undefined => {
|
|
192
|
+
const index = value.indexOf(pattern);
|
|
193
|
+
return index === -1 ? undefined : index;
|
|
194
|
+
})(d, ' ')!,
|
|
195
|
+
),
|
|
196
|
+
)!;
|
|
197
|
+
expect(Math.abs(x - 0.9486832980505138) < 1e-15).toBe(true);
|
|
198
|
+
expect(d.endsWith(' 0')).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
it('yields nan for non finite or overflowing precision', () => {
|
|
201
|
+
let path = createPath('nonZero');
|
|
202
|
+
appendSvgPathData(path, 'M1 0');
|
|
203
|
+
for (const value of [Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, 400.0]) {
|
|
204
|
+
expect(formatSvgPathData(path, precision(value))).toEqual('MNaN NaN');
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
it('spells non finite coordinates as javascript does', () => {
|
|
208
|
+
let path = createPath('nonZero');
|
|
209
|
+
path.commands.push(PathCommand.MOVE_TO & 0xff);
|
|
210
|
+
path.data.push(Number.NaN);
|
|
211
|
+
path.data.push(Number.POSITIVE_INFINITY);
|
|
212
|
+
path.commands.push(PathCommand.LINE_TO & 0xff);
|
|
213
|
+
path.data.push(Number.NEGATIVE_INFINITY);
|
|
214
|
+
path.data.push(-0.0);
|
|
215
|
+
expect(formatSvgPathData(path, undefined)).toEqual('MNaN InfinityL-Infinity 0');
|
|
216
|
+
expect(formatSvgPathData(path, precision(2.0))).toEqual('MNaN InfinityL-Infinity 0');
|
|
217
|
+
});
|
|
218
|
+
it('keeps extreme magnitudes through a precision option', () => {
|
|
219
|
+
let path = createPath('nonZero');
|
|
220
|
+
appendSvgPathData(path, 'M1e300 -1e-300');
|
|
221
|
+
expect(formatSvgPathData(path, precision(2.0))).toEqual('M1e+300 0');
|
|
222
|
+
});
|
|
223
|
+
it('emits an empty string for an empty path', () => {
|
|
224
|
+
expect(formatSvgPathData(createPath('nonZero'), undefined)).toEqual('');
|
|
225
|
+
});
|
|
55
226
|
});
|
|
56
227
|
|
|
57
228
|
describe('parseSvgPathData', () => {
|
|
58
229
|
it('parses absolute moveto and lineto', () => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{ kind: 'moveTo', x: 0, y: 0 },
|
|
63
|
-
{ kind: 'lineTo', x: 10, y: 10 },
|
|
230
|
+
expect(parsed('M0 0 L10 10')).toEqual([
|
|
231
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
232
|
+
{ kind: 'lineTo', x: 10.0, y: 10.0 },
|
|
64
233
|
]);
|
|
65
234
|
});
|
|
66
|
-
|
|
67
235
|
it('treats repeated moveto operands as implicit lineto', () => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{ kind: '
|
|
71
|
-
{ kind: 'lineTo', x:
|
|
72
|
-
{ kind: 'lineTo', x: 2, y: 2 },
|
|
236
|
+
expect(parsed('M0 0 1 1 2 2')).toEqual([
|
|
237
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
238
|
+
{ kind: 'lineTo', x: 1.0, y: 1.0 },
|
|
239
|
+
{ kind: 'lineTo', x: 2.0, y: 2.0 },
|
|
73
240
|
]);
|
|
74
241
|
});
|
|
75
|
-
|
|
76
242
|
it('resolves relative commands against the current point', () => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
{ kind: '
|
|
80
|
-
{ kind: 'lineTo', x:
|
|
81
|
-
{ kind: 'lineTo', x: 10, y: 15 },
|
|
243
|
+
expect(parsed('m10 10 l5 5 l-5 0')).toEqual([
|
|
244
|
+
{ kind: 'moveTo', x: 10.0, y: 10.0 },
|
|
245
|
+
{ kind: 'lineTo', x: 15.0, y: 15.0 },
|
|
246
|
+
{ kind: 'lineTo', x: 10.0, y: 15.0 },
|
|
82
247
|
]);
|
|
83
248
|
});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
{ kind: '
|
|
249
|
+
it('treats repeated relative moveto operands as relative lineto', () => {
|
|
250
|
+
expect(parsed('m1 1 2 2')).toEqual([
|
|
251
|
+
{ kind: 'moveTo', x: 1.0, y: 1.0 },
|
|
252
|
+
{ kind: 'lineTo', x: 3.0, y: 3.0 },
|
|
253
|
+
]);
|
|
254
|
+
});
|
|
255
|
+
it('maps horizontal and vertical lineto absolute and relative', () => {
|
|
256
|
+
expect(parsed('M0 0 H10 V20 h5 v-5')).toEqual([
|
|
257
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
258
|
+
{ kind: 'lineTo', x: 10.0, y: 0.0 },
|
|
259
|
+
{ kind: 'lineTo', x: 10.0, y: 20.0 },
|
|
260
|
+
{ kind: 'lineTo', x: 15.0, y: 20.0 },
|
|
261
|
+
{ kind: 'lineTo', x: 15.0, y: 15.0 },
|
|
93
262
|
]);
|
|
94
263
|
});
|
|
95
|
-
|
|
96
264
|
it('parses cubic curves', () => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
{
|
|
100
|
-
|
|
265
|
+
expect(parsed('M0 0 C1 2 3 4 5 6')).toEqual([
|
|
266
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
267
|
+
{
|
|
268
|
+
kind: 'cubicCurveTo',
|
|
269
|
+
controlX1: 1.0,
|
|
270
|
+
controlY1: 2.0,
|
|
271
|
+
controlX2: 3.0,
|
|
272
|
+
controlY2: 4.0,
|
|
273
|
+
x: 5.0,
|
|
274
|
+
y: 6.0,
|
|
275
|
+
},
|
|
101
276
|
]);
|
|
102
277
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const path = parseSvgPathData('M0 0 C1 2 3 4 5 6 S3 3 4 4');
|
|
106
|
-
const segments = collectSegments(path as Path);
|
|
107
|
-
expect(segments[2]).toEqual({
|
|
278
|
+
it('reflects the previous cubic control for the s shorthand', () => {
|
|
279
|
+
expect(parsed('M0 0 C1 2 3 4 5 6 S3 3 4 4')[2]).toEqual({
|
|
108
280
|
kind: 'cubicCurveTo',
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
x: 4,
|
|
114
|
-
y: 4,
|
|
281
|
+
controlX1: 7.0,
|
|
282
|
+
controlY1: 8.0,
|
|
283
|
+
controlX2: 3.0,
|
|
284
|
+
controlY2: 3.0,
|
|
285
|
+
x: 4.0,
|
|
286
|
+
y: 4.0,
|
|
115
287
|
});
|
|
116
288
|
});
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const path = parseSvgPathData('M0 0 L2 2 S3 3 4 4');
|
|
120
|
-
const segments = collectSegments(path as Path);
|
|
121
|
-
expect(segments[2]).toEqual({
|
|
289
|
+
it('uses the current point as the s control when the previous command was not a cubic', () => {
|
|
290
|
+
expect(parsed('M0 0 L2 2 S3 3 4 4')[2]).toEqual({
|
|
122
291
|
kind: 'cubicCurveTo',
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
x: 4,
|
|
128
|
-
y: 4,
|
|
292
|
+
controlX1: 2.0,
|
|
293
|
+
controlY1: 2.0,
|
|
294
|
+
controlX2: 3.0,
|
|
295
|
+
controlY2: 3.0,
|
|
296
|
+
x: 4.0,
|
|
297
|
+
y: 4.0,
|
|
129
298
|
});
|
|
130
299
|
});
|
|
131
|
-
|
|
132
300
|
it('parses quadratic curves', () => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
{ kind: '
|
|
136
|
-
{ kind: 'curveTo', controlX: 1, controlY: 1, x: 2, y: 2 },
|
|
301
|
+
expect(parsed('M0 0 Q1 1 2 2')).toEqual([
|
|
302
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
303
|
+
{ kind: 'quadraticCurveTo', controlX: 1.0, controlY: 1.0, x: 2.0, y: 2.0 },
|
|
137
304
|
]);
|
|
138
305
|
});
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
306
|
+
it('preserves the quadratic command numeric value in the parsed path', () => {
|
|
307
|
+
const path = parseSvgPathData('M0 0 Q1 1 2 2')!;
|
|
308
|
+
expect(PathCommand.QUADRATIC_CURVE_TO & 0xff).toEqual(3);
|
|
309
|
+
expect(path.commands).toEqual([PathCommand.MOVE_TO & 0xff, 3]);
|
|
310
|
+
});
|
|
311
|
+
it('reflects the previous quadratic control for the t shorthand', () => {
|
|
312
|
+
expect(parsed('M0 0 Q1 1 2 2 T4 4')[2]).toEqual({
|
|
313
|
+
kind: 'quadraticCurveTo',
|
|
314
|
+
controlX: 3.0,
|
|
315
|
+
controlY: 3.0,
|
|
316
|
+
x: 4.0,
|
|
317
|
+
y: 4.0,
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
it('uses the current point as the t control when the previous command was not a quadratic', () => {
|
|
321
|
+
expect(parsed('M0 0 L2 2 T4 4')[2]).toEqual({
|
|
322
|
+
kind: 'quadraticCurveTo',
|
|
323
|
+
controlX: 2.0,
|
|
324
|
+
controlY: 2.0,
|
|
325
|
+
x: 4.0,
|
|
326
|
+
y: 4.0,
|
|
327
|
+
});
|
|
144
328
|
});
|
|
145
|
-
|
|
146
329
|
it('appends an arc as cubic segments ending at the arc endpoint', () => {
|
|
147
|
-
const
|
|
148
|
-
expect(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
expect(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
330
|
+
const segments = parsed('M0 0 A5 5 0 0 1 10 0');
|
|
331
|
+
expect(kinds(segments)[0]).toEqual('moveTo');
|
|
332
|
+
expect(kinds(segments)[1]).toEqual('cubicCurveTo');
|
|
333
|
+
const [x, y] = lastPoint(segments);
|
|
334
|
+
expect(Math.abs(x - 10.0) < 5e-7).toBe(true);
|
|
335
|
+
expect(Math.abs(y) < 5e-7).toBe(true);
|
|
336
|
+
});
|
|
337
|
+
it('starts an arc after close at the closed subpath origin', () => {
|
|
338
|
+
expect(parsed('M0 0 L10 0 Z A5 5 0 0 1 10 0')).toEqual(parsed('M0 0 L10 0 Z M0 0 A5 5 0 0 1 10 0'));
|
|
155
339
|
});
|
|
156
|
-
|
|
157
340
|
it('closes contours and returns the current point to the subpath start', () => {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
expect(segments
|
|
161
|
-
|
|
162
|
-
expect(segments[4]).toEqual({ kind: 'lineTo', x: 5, y: 5 });
|
|
341
|
+
const segments = parsed('M0 0 L10 0 L10 10 Z L5 5');
|
|
342
|
+
expect(kinds(segments)).toEqual(['moveTo', 'lineTo', 'lineTo', 'close', 'lineTo']);
|
|
343
|
+
expect(segments[4]).toEqual({ kind: 'lineTo', x: 5.0, y: 5.0 });
|
|
344
|
+
expect(parsed('M1 1 L9 9 z l1 0')[3]).toEqual({ kind: 'lineTo', x: 2.0, y: 1.0 });
|
|
163
345
|
});
|
|
164
|
-
|
|
165
346
|
it('tokenizes numbers with no space before a negative sign', () => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
{ kind: '
|
|
169
|
-
{ kind: 'lineTo', x: 10, y: -5 },
|
|
347
|
+
expect(parsed('M0 0L10-5')).toEqual([
|
|
348
|
+
{ kind: 'moveTo', x: 0.0, y: 0.0 },
|
|
349
|
+
{ kind: 'lineTo', x: 10.0, y: -5.0 },
|
|
170
350
|
]);
|
|
171
351
|
});
|
|
172
|
-
|
|
173
352
|
it('tokenizes scientific notation and comma separators', () => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
{ kind: '
|
|
177
|
-
{ kind: 'lineTo', x: 20, y: -15 },
|
|
353
|
+
expect(parsed('M1e1,1e1 L2E1,-1.5e1')).toEqual([
|
|
354
|
+
{ kind: 'moveTo', x: 10.0, y: 10.0 },
|
|
355
|
+
{ kind: 'lineTo', x: 20.0, y: -15.0 },
|
|
178
356
|
]);
|
|
179
357
|
});
|
|
180
|
-
|
|
358
|
+
it('tokenizes leading dots packed decimals explicit plus signs and trailing dots', () => {
|
|
359
|
+
expect(parsed('M.5.5 L+2 +3 L10. 20.')).toEqual([
|
|
360
|
+
{ kind: 'moveTo', x: 0.5, y: 0.5 },
|
|
361
|
+
{ kind: 'lineTo', x: 2.0, y: 3.0 },
|
|
362
|
+
{ kind: 'lineTo', x: 10.0, y: 20.0 },
|
|
363
|
+
]);
|
|
364
|
+
});
|
|
365
|
+
it('tokenizes a trailing dot before an exponent and every separator', () => {
|
|
366
|
+
expect(parsed('M1.e1\t\n\r\u{c}2e+1')).toEqual([{ kind: 'moveTo', x: 10.0, y: 20.0 }]);
|
|
367
|
+
});
|
|
368
|
+
it('rejects a dangling exponent after backtracking the numeric token', () => {
|
|
369
|
+
expect(parseSvgPathData('M1e 0') === undefined).toBe(true);
|
|
370
|
+
});
|
|
181
371
|
it('accepts large finite coordinates at both signs', () => {
|
|
182
|
-
|
|
183
|
-
|
|
372
|
+
expect(parsed('M1e308 -1e308')).toEqual([{ kind: 'moveTo', x: 1e308, y: -1e308 }]);
|
|
373
|
+
});
|
|
374
|
+
it('accepts an underflowing token as signed zero', () => {
|
|
375
|
+
const segments = parsed('M1e-400 -1e-400');
|
|
376
|
+
const $letElse = segments[0];
|
|
377
|
+
if (!($letElse.kind === 'moveTo')) {
|
|
378
|
+
throw new Error(`expected moveto`);
|
|
379
|
+
}
|
|
380
|
+
const x = $letElse.x;
|
|
381
|
+
const y = $letElse.y;
|
|
382
|
+
expect(x === 0.0 && y === 0.0).toBe(true);
|
|
383
|
+
expect((x > 0 || Object.is(x, 0)) && (y < 0 || Object.is(y, -0))).toBe(true);
|
|
184
384
|
});
|
|
185
|
-
|
|
186
385
|
it('returns null when a numeric token overflows at either sign', () => {
|
|
187
|
-
expect(parseSvgPathData('M1e999 0')).
|
|
188
|
-
expect(parseSvgPathData('M-1e999 0')).
|
|
386
|
+
expect(parseSvgPathData('M1e999 0') === undefined).toBe(true);
|
|
387
|
+
expect(parseSvgPathData('M-1e999 0') === undefined).toBe(true);
|
|
189
388
|
});
|
|
190
|
-
|
|
191
389
|
it('parses packed arc flags with no separators', () => {
|
|
192
|
-
const
|
|
193
|
-
expect(
|
|
194
|
-
|
|
195
|
-
const last = segments[segments.length - 1] as Extract<PathSegment, { kind: 'cubicCurveTo' }>;
|
|
196
|
-
expect(last.x).toBeCloseTo(10, 6);
|
|
197
|
-
expect(last.y).toBeCloseTo(0, 6);
|
|
390
|
+
const [x, y] = lastPoint(parsed('M0 0A5 5 0 0110 0'));
|
|
391
|
+
expect(Math.abs(x - 10.0) < 5e-7).toBe(true);
|
|
392
|
+
expect(Math.abs(y) < 5e-7).toBe(true);
|
|
198
393
|
});
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
394
|
+
it('resolves a relative arc endpoint against the current point', () => {
|
|
395
|
+
expect(parsed('M2 3 a5 5 0 0 1 10 0')).toEqual(parsed('M2 3 A5 5 0 0 1 12 3'));
|
|
396
|
+
});
|
|
397
|
+
it('returns an empty path for empty or whitespace only input', () => {
|
|
398
|
+
expect(parsed('').length === 0).toBe(true);
|
|
399
|
+
expect(parsed(' \n ').length === 0).toBe(true);
|
|
203
400
|
});
|
|
204
|
-
|
|
205
401
|
it('returns null when the path does not begin with a moveto', () => {
|
|
206
|
-
expect(parseSvgPathData('L10 10')).
|
|
402
|
+
expect(parseSvgPathData('L10 10') === undefined).toBe(true);
|
|
207
403
|
});
|
|
208
|
-
|
|
209
404
|
it('returns null on an unknown command letter', () => {
|
|
210
|
-
expect(parseSvgPathData('M0 0 X10 10')).
|
|
405
|
+
expect(parseSvgPathData('M0 0 X10 10') === undefined).toBe(true);
|
|
211
406
|
});
|
|
212
|
-
|
|
213
407
|
it('returns null on a short coordinate run', () => {
|
|
214
|
-
expect(parseSvgPathData('M0 0 L10')).
|
|
215
|
-
expect(parseSvgPathData('M')).
|
|
216
|
-
expect(parseSvgPathData('M0 0 C1 2 3 4 5')).
|
|
408
|
+
expect(parseSvgPathData('M0 0 L10') === undefined).toBe(true);
|
|
409
|
+
expect(parseSvgPathData('M') === undefined).toBe(true);
|
|
410
|
+
expect(parseSvgPathData('M0 0 C1 2 3 4 5') === undefined).toBe(true);
|
|
217
411
|
});
|
|
218
|
-
|
|
219
|
-
it('round-trips representative path data through format and back', () => {
|
|
412
|
+
it('round trips representative path data through format and back', () => {
|
|
220
413
|
const cases = [
|
|
221
414
|
'M0 0 L10 0 L10 10 Z',
|
|
222
415
|
'M0 0 C1 2 3 4 5 6 S7 8 9 10',
|
|
@@ -225,21 +418,24 @@ describe('parseSvgPathData', () => {
|
|
|
225
418
|
'M0 0 A5 5 0 0 1 10 0 A5 5 0 0 1 20 0',
|
|
226
419
|
];
|
|
227
420
|
for (const d of cases) {
|
|
228
|
-
const first =
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
421
|
+
const first = (() => {
|
|
422
|
+
const value = parseSvgPathData(d);
|
|
423
|
+
if (value === undefined) throw new Error('first parse');
|
|
424
|
+
return value;
|
|
425
|
+
})();
|
|
426
|
+
const reserialized = formatSvgPathData(first, undefined);
|
|
427
|
+
const second = (() => {
|
|
428
|
+
const value = parseSvgPathData(reserialized);
|
|
429
|
+
if (value === undefined) throw new Error('second parse');
|
|
430
|
+
return value;
|
|
431
|
+
})();
|
|
432
|
+
const a = collectSegments(first);
|
|
433
|
+
const b = collectSegments(second);
|
|
434
|
+
expect(b.length).toEqual(a.length);
|
|
435
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
436
|
+
expect(kinds(b.slice(i, i + 1))).toEqual(kinds(a.slice(i, i + 1)));
|
|
437
|
+
for (const [bv, av] of values(b[i]).map((item, index) => [item, values(a[i])[index]] as const)) {
|
|
438
|
+
expect(Math.abs(bv - av) < 5e-7, `${d}: ${bv} vs ${av}`).toBe(true);
|
|
243
439
|
}
|
|
244
440
|
}
|
|
245
441
|
}
|