@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/dist/svgPathData.js
CHANGED
|
@@ -1,331 +1,542 @@
|
|
|
1
|
-
import {
|
|
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
|
-
*
|
|
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`
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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 `
|
|
12
|
-
* bezier segments
|
|
13
|
-
* cubics, not an arc verb. The geometry round-trips; the arc command
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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 (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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 (
|
|
59
|
-
|
|
60
|
-
|
|
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 (
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
76
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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 =
|
|
193
|
+
lastKind = 90;
|
|
108
194
|
continue;
|
|
109
195
|
}
|
|
110
|
-
|
|
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
|
-
|
|
198
|
+
for (;;) {
|
|
115
199
|
if (!first) {
|
|
116
|
-
skipSeparators();
|
|
117
|
-
if (pos >= length)
|
|
200
|
+
skipSeparators(reader);
|
|
201
|
+
if (reader.pos >= length) {
|
|
118
202
|
break;
|
|
119
|
-
|
|
203
|
+
}
|
|
204
|
+
if (isAsciiLetter(reader.units[reader.pos])) {
|
|
120
205
|
break;
|
|
206
|
+
}
|
|
121
207
|
}
|
|
122
|
-
const relative = active >=
|
|
123
|
-
const upper = relative ? active
|
|
124
|
-
if (upper ===
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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 =
|
|
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
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
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
|
|
450
|
+
return undefined;
|
|
275
451
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
492
|
+
let expDigit = false;
|
|
493
|
+
while (isDigit(reader)) {
|
|
494
|
+
reader.pos += 1;
|
|
495
|
+
expDigit = true;
|
|
303
496
|
}
|
|
304
|
-
|
|
305
|
-
|
|
497
|
+
if (!expDigit) {
|
|
498
|
+
reader.pos = expStart;
|
|
306
499
|
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
|
322
|
-
if (
|
|
323
|
-
return
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
|
329
|
-
|
|
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
|