@es3d/es3d 1.0.0 → 1.0.1
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/LICENSE +201 -201
- package/README.md +86 -86
- package/docs/SPEC.md +417 -417
- package/package.json +1 -1
- package/scripts/es3d-cli.mjs +377 -377
- package/src/cache/geometry-store.js +392 -392
- package/src/cache/mesh-bundle.js +123 -123
- package/src/cache/sha256.js +73 -73
- package/src/format/ast.js +238 -238
- package/src/format/colors.js +93 -93
- package/src/format/compact-parser.js +171 -171
- package/src/format/compact-tokenizer.js +165 -165
- package/src/format/errors.js +247 -247
- package/src/format/json.js +55 -55
- package/src/format/limits.js +14 -14
- package/src/format/parse.js +92 -92
- package/src/format/schema.js +617 -617
- package/src/format/serializer.js +333 -333
- package/src/format/validator.js +330 -330
- package/src/format/value-error.js +12 -12
- package/src/format/value-format.js +314 -314
- package/src/format/values.js +459 -459
- package/src/format/xml-parser.js +136 -136
- package/src/format/xml-tokenizer.js +237 -237
- package/src/geometry/contour.js +86 -86
- package/src/geometry/extrude.js +175 -175
- package/src/geometry/gear.js +85 -85
- package/src/geometry/geometry-cache.js +159 -159
- package/src/geometry/lod-error.js +178 -178
- package/src/geometry/mesh.js +458 -458
- package/src/geometry/primitives.js +299 -299
- package/src/geometry/registry.js +645 -645
- package/src/geometry/revolve.js +136 -136
- package/src/geometry/sweep.js +441 -441
- package/src/index.js +47 -47
- package/src/io/compression.js +67 -67
- package/src/io/convert.js +236 -236
- package/src/io/download.js +13 -13
- package/src/io/external-scene.js +127 -127
- package/src/io/file-loader.js +28 -28
- package/src/io/gltf.js +630 -630
- package/src/io/mesh-import.js +104 -104
- package/src/io/obj.js +352 -352
- package/src/io/step-brep.js +439 -439
- package/src/io/step-exact.js +1149 -1149
- package/src/io/step.js +1418 -1418
- package/src/io/stl.js +193 -193
- package/src/io/svg-import.js +787 -787
- package/src/material/materials.js +154 -154
- package/src/math/mat4.js +475 -475
- package/src/math/random.js +30 -30
- package/src/math/trig.js +65 -65
- package/src/math/vec3.js +81 -81
- package/src/optimize/optimizer.js +579 -579
- package/src/path/path-flatten.js +322 -322
- package/src/path/path-parser.js +332 -332
- package/src/path/shapes.js +129 -129
- package/src/path/triangulate.js +517 -517
- package/src/pipeline.js +74 -74
- package/src/render/camera.js +168 -168
- package/src/render/frame-builder.js +362 -362
- package/src/render/geometry-pool.js +149 -149
- package/src/render/lighting.js +149 -149
- package/src/render/orbit-controls.js +237 -237
- package/src/render/renderer.js +258 -249
- package/src/render/scroll-timeline.js +104 -104
- package/src/render/shaders/glsl.js +285 -285
- package/src/render/shaders/wgsl.js +252 -252
- package/src/render/stage.js +364 -352
- package/src/render/texture-cache.js +40 -40
- package/src/render/textures.js +223 -223
- package/src/render/webgl2/webgl2-backend.js +586 -532
- package/src/render/webgpu/webgpu-backend.js +482 -480
- package/src/scene/flatten.js +754 -754
- package/src/scene/picking.js +161 -161
- package/src/scene/scene-graph.js +320 -320
- package/src/transform/transform.js +211 -211
- package/src/wasm/kernels-binary.js +3 -3
- package/src/wasm/kernels.js +385 -385
package/src/path/path-parser.js
CHANGED
|
@@ -1,332 +1,332 @@
|
|
|
1
|
-
// SVG-style path data parser.
|
|
2
|
-
//
|
|
3
|
-
// 2D paths accept the full SVG grammar: M L H V C S Q T A Z (absolute and
|
|
4
|
-
// relative), implicit command repetition, optional separators ("M10-5"),
|
|
5
|
-
// compact arc flags ("a1,1 0 00 1,1") and closepath semantics.
|
|
6
|
-
//
|
|
7
|
-
// 3D paths (sweep spines) use the same letters with three coordinates per
|
|
8
|
-
// point: M L C S Q T Z (H, V and A have no unambiguous 3D meaning).
|
|
9
|
-
//
|
|
10
|
-
// The result is a list of absolute segments; H/V become L, S becomes C and
|
|
11
|
-
// T becomes Q. Each segment remembers its source offset for error reporting.
|
|
12
|
-
|
|
13
|
-
import { MAX_PATH_COMMANDS } from '../format/limits.js';
|
|
14
|
-
|
|
15
|
-
export class PathError extends Error {
|
|
16
|
-
constructor(message, offset, length = 1, code = null) {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = 'PathError';
|
|
19
|
-
this.offset = offset;
|
|
20
|
-
this.length = length;
|
|
21
|
-
this.code = code;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const ARG_COUNT_2D = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
|
|
26
|
-
const ARG_COUNT_3D = { M: 3, L: 3, C: 9, S: 6, Q: 6, T: 3, Z: 0 };
|
|
27
|
-
|
|
28
|
-
function isWs(c) {
|
|
29
|
-
return c === 32 || c === 9 || c === 10 || c === 13 || c === 12;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Scan a number at i; returns the end index or -1. */
|
|
33
|
-
export function scanNumber(s, i) {
|
|
34
|
-
return readNumber(s, i);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const POW10 = Array.from({ length: 23 }, (_, k) => Number(`1e${k}`));
|
|
38
|
-
|
|
39
|
-
/** Value of the number that readNumber last read. */
|
|
40
|
-
export let numberValue = 0;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Read the number at i: returns its end index (or -1 when no number starts
|
|
44
|
-
* there) and leaves its value in `numberValue`, exactly as Number() would give
|
|
45
|
-
* it for s.slice(i, end). When the digits form an integer of at most 2^53 and
|
|
46
|
-
* the decimal exponent is within ±22, both are exact in binary64, so one IEEE
|
|
47
|
-
* multiply or divide is correctly rounded (Clinger's fast path) and no
|
|
48
|
-
* substring is made; anything else goes through Number(). Mesh data is
|
|
49
|
-
* hundreds of thousands of such numbers, so this is one pass over the digits.
|
|
50
|
-
*/
|
|
51
|
-
export function readNumber(s, i) {
|
|
52
|
-
const start = i;
|
|
53
|
-
let c = s.charCodeAt(i);
|
|
54
|
-
const neg = c === 45;
|
|
55
|
-
if (c === 43 || c === 45) c = s.charCodeAt(++i);
|
|
56
|
-
// the mantissa only grows, so it is exact whenever it ends at most 2^53
|
|
57
|
-
let m = 0;
|
|
58
|
-
const intStart = i;
|
|
59
|
-
while (c >= 48 && c <= 57) {
|
|
60
|
-
m = m * 10 + (c - 48);
|
|
61
|
-
c = s.charCodeAt(++i);
|
|
62
|
-
}
|
|
63
|
-
let digits = i - intStart, frac = 0;
|
|
64
|
-
if (c === 46) {
|
|
65
|
-
const fracStart = ++i;
|
|
66
|
-
c = s.charCodeAt(i);
|
|
67
|
-
while (c >= 48 && c <= 57) {
|
|
68
|
-
m = m * 10 + (c - 48);
|
|
69
|
-
c = s.charCodeAt(++i);
|
|
70
|
-
}
|
|
71
|
-
frac = i - fracStart;
|
|
72
|
-
digits += frac;
|
|
73
|
-
}
|
|
74
|
-
if (!digits) return -1;
|
|
75
|
-
let e = 0;
|
|
76
|
-
if (c === 101 || c === 69) {
|
|
77
|
-
// the exponent belongs to the number only when digits follow
|
|
78
|
-
let j = i + 1;
|
|
79
|
-
let c2 = s.charCodeAt(j);
|
|
80
|
-
const eneg = c2 === 45;
|
|
81
|
-
if (c2 === 43 || c2 === 45) c2 = s.charCodeAt(++j);
|
|
82
|
-
if (c2 >= 48 && c2 <= 57) {
|
|
83
|
-
while (c2 >= 48 && c2 <= 57) {
|
|
84
|
-
e = e * 10 + (c2 - 48);
|
|
85
|
-
c2 = s.charCodeAt(++j);
|
|
86
|
-
}
|
|
87
|
-
if (eneg) e = -e;
|
|
88
|
-
i = j;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
e -= frac;
|
|
92
|
-
if (m > 9007199254740992 || e > 22 || e < -22) numberValue = Number(s.slice(start, i));
|
|
93
|
-
else {
|
|
94
|
-
const v = e >= 0 ? m * POW10[e] : m / POW10[-e];
|
|
95
|
-
numberValue = neg ? -v : v;
|
|
96
|
-
}
|
|
97
|
-
return i;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* @param {string} s path data
|
|
102
|
-
* @param {2|3} dim
|
|
103
|
-
* @returns {{segments: object[], subpaths: number}}
|
|
104
|
-
*/
|
|
105
|
-
export function parsePathData(s, dim = 2) {
|
|
106
|
-
const counts = dim === 3 ? ARG_COUNT_3D : ARG_COUNT_2D;
|
|
107
|
-
const n = s.length;
|
|
108
|
-
let i = 0;
|
|
109
|
-
const segs = [];
|
|
110
|
-
let cx = 0, cy = 0, cz = 0;
|
|
111
|
-
let sx = 0, sy = 0, sz = 0;
|
|
112
|
-
// previous control point for S/T reflection
|
|
113
|
-
let pcx = 0, pcy = 0, pcz = 0;
|
|
114
|
-
let prev = '';
|
|
115
|
-
let cmd = '';
|
|
116
|
-
let subpaths = 0;
|
|
117
|
-
let pendingMoveAfterZ = false;
|
|
118
|
-
|
|
119
|
-
const skipWs = () => {
|
|
120
|
-
while (i < n && isWs(s.charCodeAt(i))) i++;
|
|
121
|
-
};
|
|
122
|
-
const skipSep = () => {
|
|
123
|
-
skipWs();
|
|
124
|
-
if (s.charCodeAt(i) === 44) {
|
|
125
|
-
i++;
|
|
126
|
-
skipWs();
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
const num = () => {
|
|
130
|
-
skipSep();
|
|
131
|
-
const end = readNumber(s, i);
|
|
132
|
-
if (end < 0) {
|
|
133
|
-
if (i >= n) throw new PathError(`Command '${cmd}' expects ${counts[cmd.toUpperCase()]} numbers but the path data ended.`, Math.max(0, n - 1), 1);
|
|
134
|
-
throw new PathError(`Expected a number in '${cmd}' command but found '${s[i]}'.`, i, 1);
|
|
135
|
-
}
|
|
136
|
-
i = end;
|
|
137
|
-
return numberValue;
|
|
138
|
-
};
|
|
139
|
-
const flag = () => {
|
|
140
|
-
skipSep();
|
|
141
|
-
const c = s.charCodeAt(i);
|
|
142
|
-
if (c !== 48 && c !== 49) throw new PathError("Arc flags must be '0' or '1'.", i, 1);
|
|
143
|
-
i++;
|
|
144
|
-
return c === 49 ? 1 : 0;
|
|
145
|
-
};
|
|
146
|
-
const startsNumber = () => {
|
|
147
|
-
let j = i;
|
|
148
|
-
while (j < n && isWs(s.charCodeAt(j))) j++;
|
|
149
|
-
if (s.charCodeAt(j) === 44) {
|
|
150
|
-
j++;
|
|
151
|
-
while (j < n && isWs(s.charCodeAt(j))) j++;
|
|
152
|
-
}
|
|
153
|
-
const c = s.charCodeAt(j);
|
|
154
|
-
return (c >= 48 && c <= 57) || c === 45 || c === 43 || c === 46;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
skipWs();
|
|
158
|
-
if (i >= n) return { segments: segs, subpaths: 0 };
|
|
159
|
-
|
|
160
|
-
while (true) {
|
|
161
|
-
skipWs();
|
|
162
|
-
if (i >= n) break;
|
|
163
|
-
if (segs.length >= MAX_PATH_COMMANDS) {
|
|
164
|
-
throw new PathError(`Path data has more than ${MAX_PATH_COMMANDS.toLocaleString('en-US')} commands.`, i, 1, 'E_PATH_LIMIT');
|
|
165
|
-
}
|
|
166
|
-
const c = s[i];
|
|
167
|
-
const code = s.charCodeAt(i);
|
|
168
|
-
const isLetter = (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
169
|
-
const segStart = i;
|
|
170
|
-
if (isLetter) {
|
|
171
|
-
const up = c.toUpperCase();
|
|
172
|
-
if (!(up in ARG_COUNT_2D)) throw new PathError(`Unknown path command '${c}'. Valid commands: M L H V C S Q T A Z.`, i, 1);
|
|
173
|
-
if (!(up in counts)) throw new PathError(`Command '${c}' is not available in 3D path data (use M L C S Q T Z).`, i, 1);
|
|
174
|
-
if (!cmd && up !== 'M') throw new PathError('Path data must begin with a moveto command (M or m).', i, 1);
|
|
175
|
-
cmd = c;
|
|
176
|
-
i++;
|
|
177
|
-
if (up === 'Z') {
|
|
178
|
-
// closepath
|
|
179
|
-
segs.push({ c: 'Z', o: segStart });
|
|
180
|
-
cx = sx;
|
|
181
|
-
cy = sy;
|
|
182
|
-
cz = sz;
|
|
183
|
-
prev = 'Z';
|
|
184
|
-
pendingMoveAfterZ = true;
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
if (!startsNumber()) {
|
|
188
|
-
skipWs();
|
|
189
|
-
throw new PathError(`Command '${c}' expects ${counts[up]} numbers.`, Math.min(i, n - 1), 1);
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
if (!cmd) throw new PathError('Path data must begin with a moveto command (M or m).', i, 1);
|
|
193
|
-
if (cmd === 'Z' || cmd === 'z') throw new PathError(`Unexpected '${c}' after closepath; start a new command.`, i, 1);
|
|
194
|
-
if (!startsNumber()) throw new PathError(`Unexpected character '${c}' in path data.`, i, 1);
|
|
195
|
-
// implicit repetition: subsequent moveto pairs are lineto
|
|
196
|
-
if (cmd === 'M') cmd = 'L';
|
|
197
|
-
else if (cmd === 'm') cmd = 'l';
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const rel = cmd === cmd.toLowerCase();
|
|
201
|
-
const up = cmd.toUpperCase();
|
|
202
|
-
if (pendingMoveAfterZ && up !== 'M') {
|
|
203
|
-
// a subpath that continues after Z starts at the previous subpath's start point
|
|
204
|
-
segs.push({ c: 'M', x: sx, y: sy, z: sz, o: segStart, implicit: true });
|
|
205
|
-
subpaths++;
|
|
206
|
-
}
|
|
207
|
-
pendingMoveAfterZ = false;
|
|
208
|
-
const ox = rel ? cx : 0, oy = rel ? cy : 0, oz = rel ? cz : 0;
|
|
209
|
-
|
|
210
|
-
switch (up) {
|
|
211
|
-
case 'M': {
|
|
212
|
-
const x = num() + ox, y = num() + oy;
|
|
213
|
-
const z = dim === 3 ? num() + oz : 0;
|
|
214
|
-
segs.push({ c: 'M', x, y, z, o: segStart });
|
|
215
|
-
cx = sx = x;
|
|
216
|
-
cy = sy = y;
|
|
217
|
-
cz = sz = z;
|
|
218
|
-
subpaths++;
|
|
219
|
-
break;
|
|
220
|
-
}
|
|
221
|
-
case 'L': {
|
|
222
|
-
const x = num() + ox, y = num() + oy;
|
|
223
|
-
const z = dim === 3 ? num() + oz : 0;
|
|
224
|
-
segs.push({ c: 'L', x, y, z, o: segStart });
|
|
225
|
-
cx = x;
|
|
226
|
-
cy = y;
|
|
227
|
-
cz = z;
|
|
228
|
-
break;
|
|
229
|
-
}
|
|
230
|
-
case 'H': {
|
|
231
|
-
const x = num() + ox;
|
|
232
|
-
segs.push({ c: 'L', x, y: cy, z: 0, o: segStart });
|
|
233
|
-
cx = x;
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
case 'V': {
|
|
237
|
-
const y = num() + oy;
|
|
238
|
-
segs.push({ c: 'L', x: cx, y, z: 0, o: segStart });
|
|
239
|
-
cy = y;
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
case 'C': {
|
|
243
|
-
const x1 = num() + ox, y1 = num() + oy, z1 = dim === 3 ? num() + oz : 0;
|
|
244
|
-
const x2 = num() + ox, y2 = num() + oy, z2 = dim === 3 ? num() + oz : 0;
|
|
245
|
-
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
246
|
-
segs.push({ c: 'C', x1, y1, z1, x2, y2, z2, x, y, z, o: segStart });
|
|
247
|
-
pcx = x2; pcy = y2; pcz = z2;
|
|
248
|
-
cx = x; cy = y; cz = z;
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
case 'S': {
|
|
252
|
-
let x1 = cx, y1 = cy, z1 = cz;
|
|
253
|
-
if (prev === 'C' || prev === 'S') {
|
|
254
|
-
x1 = 2 * cx - pcx;
|
|
255
|
-
y1 = 2 * cy - pcy;
|
|
256
|
-
z1 = 2 * cz - pcz;
|
|
257
|
-
}
|
|
258
|
-
const x2 = num() + ox, y2 = num() + oy, z2 = dim === 3 ? num() + oz : 0;
|
|
259
|
-
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
260
|
-
segs.push({ c: 'C', x1, y1, z1, x2, y2, z2, x, y, z, o: segStart });
|
|
261
|
-
pcx = x2; pcy = y2; pcz = z2;
|
|
262
|
-
cx = x; cy = y; cz = z;
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
case 'Q': {
|
|
266
|
-
const x1 = num() + ox, y1 = num() + oy, z1 = dim === 3 ? num() + oz : 0;
|
|
267
|
-
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
268
|
-
segs.push({ c: 'Q', x1, y1, z1, x, y, z, o: segStart });
|
|
269
|
-
pcx = x1; pcy = y1; pcz = z1;
|
|
270
|
-
cx = x; cy = y; cz = z;
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
case 'T': {
|
|
274
|
-
let x1 = cx, y1 = cy, z1 = cz;
|
|
275
|
-
if (prev === 'Q' || prev === 'T') {
|
|
276
|
-
x1 = 2 * cx - pcx;
|
|
277
|
-
y1 = 2 * cy - pcy;
|
|
278
|
-
z1 = 2 * cz - pcz;
|
|
279
|
-
}
|
|
280
|
-
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
281
|
-
segs.push({ c: 'Q', x1, y1, z1, x, y, z, o: segStart });
|
|
282
|
-
pcx = x1; pcy = y1; pcz = z1;
|
|
283
|
-
cx = x; cy = y; cz = z;
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
case 'A': {
|
|
287
|
-
const rx = num();
|
|
288
|
-
const ry = num();
|
|
289
|
-
const rot = num();
|
|
290
|
-
const large = flag();
|
|
291
|
-
const sweep = flag();
|
|
292
|
-
const x = num() + ox, y = num() + oy;
|
|
293
|
-
if (rx < 0 || ry < 0) throw new PathError('Arc radii must not be negative.', segStart, i - segStart);
|
|
294
|
-
segs.push({ c: 'A', rx, ry, rot, large, sweep, x0: cx, y0: cy, x, y, z: 0, o: segStart });
|
|
295
|
-
cx = x;
|
|
296
|
-
cy = y;
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
|
-
default:
|
|
300
|
-
throw new PathError(`Unsupported command '${cmd}'.`, segStart, 1);
|
|
301
|
-
}
|
|
302
|
-
prev = up;
|
|
303
|
-
}
|
|
304
|
-
return { segments: segs, subpaths };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/** Axis-aligned bounds of the control polygon (a conservative bound for curves; arcs use their endpoints and radii). */
|
|
308
|
-
export function pathControlBounds(segments) {
|
|
309
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
310
|
-
const add = (x, y) => {
|
|
311
|
-
if (x < minX) minX = x;
|
|
312
|
-
if (y < minY) minY = y;
|
|
313
|
-
if (x > maxX) maxX = x;
|
|
314
|
-
if (y > maxY) maxY = y;
|
|
315
|
-
};
|
|
316
|
-
for (const s of segments) {
|
|
317
|
-
if (s.c === 'Z') continue;
|
|
318
|
-
add(s.x, s.y);
|
|
319
|
-
if (s.c === 'C') {
|
|
320
|
-
add(s.x1, s.y1);
|
|
321
|
-
add(s.x2, s.y2);
|
|
322
|
-
} else if (s.c === 'Q') add(s.x1, s.y1);
|
|
323
|
-
else if (s.c === 'A') {
|
|
324
|
-
const r = Math.max(s.rx, s.ry);
|
|
325
|
-
add(s.x - r, s.y - r);
|
|
326
|
-
add(s.x + r, s.y + r);
|
|
327
|
-
add(s.x0 - r, s.y0 - r);
|
|
328
|
-
add(s.x0 + r, s.y0 + r);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return { minX, minY, maxX, maxY };
|
|
332
|
-
}
|
|
1
|
+
// SVG-style path data parser.
|
|
2
|
+
//
|
|
3
|
+
// 2D paths accept the full SVG grammar: M L H V C S Q T A Z (absolute and
|
|
4
|
+
// relative), implicit command repetition, optional separators ("M10-5"),
|
|
5
|
+
// compact arc flags ("a1,1 0 00 1,1") and closepath semantics.
|
|
6
|
+
//
|
|
7
|
+
// 3D paths (sweep spines) use the same letters with three coordinates per
|
|
8
|
+
// point: M L C S Q T Z (H, V and A have no unambiguous 3D meaning).
|
|
9
|
+
//
|
|
10
|
+
// The result is a list of absolute segments; H/V become L, S becomes C and
|
|
11
|
+
// T becomes Q. Each segment remembers its source offset for error reporting.
|
|
12
|
+
|
|
13
|
+
import { MAX_PATH_COMMANDS } from '../format/limits.js';
|
|
14
|
+
|
|
15
|
+
export class PathError extends Error {
|
|
16
|
+
constructor(message, offset, length = 1, code = null) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'PathError';
|
|
19
|
+
this.offset = offset;
|
|
20
|
+
this.length = length;
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const ARG_COUNT_2D = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
|
|
26
|
+
const ARG_COUNT_3D = { M: 3, L: 3, C: 9, S: 6, Q: 6, T: 3, Z: 0 };
|
|
27
|
+
|
|
28
|
+
function isWs(c) {
|
|
29
|
+
return c === 32 || c === 9 || c === 10 || c === 13 || c === 12;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Scan a number at i; returns the end index or -1. */
|
|
33
|
+
export function scanNumber(s, i) {
|
|
34
|
+
return readNumber(s, i);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const POW10 = Array.from({ length: 23 }, (_, k) => Number(`1e${k}`));
|
|
38
|
+
|
|
39
|
+
/** Value of the number that readNumber last read. */
|
|
40
|
+
export let numberValue = 0;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read the number at i: returns its end index (or -1 when no number starts
|
|
44
|
+
* there) and leaves its value in `numberValue`, exactly as Number() would give
|
|
45
|
+
* it for s.slice(i, end). When the digits form an integer of at most 2^53 and
|
|
46
|
+
* the decimal exponent is within ±22, both are exact in binary64, so one IEEE
|
|
47
|
+
* multiply or divide is correctly rounded (Clinger's fast path) and no
|
|
48
|
+
* substring is made; anything else goes through Number(). Mesh data is
|
|
49
|
+
* hundreds of thousands of such numbers, so this is one pass over the digits.
|
|
50
|
+
*/
|
|
51
|
+
export function readNumber(s, i) {
|
|
52
|
+
const start = i;
|
|
53
|
+
let c = s.charCodeAt(i);
|
|
54
|
+
const neg = c === 45;
|
|
55
|
+
if (c === 43 || c === 45) c = s.charCodeAt(++i);
|
|
56
|
+
// the mantissa only grows, so it is exact whenever it ends at most 2^53
|
|
57
|
+
let m = 0;
|
|
58
|
+
const intStart = i;
|
|
59
|
+
while (c >= 48 && c <= 57) {
|
|
60
|
+
m = m * 10 + (c - 48);
|
|
61
|
+
c = s.charCodeAt(++i);
|
|
62
|
+
}
|
|
63
|
+
let digits = i - intStart, frac = 0;
|
|
64
|
+
if (c === 46) {
|
|
65
|
+
const fracStart = ++i;
|
|
66
|
+
c = s.charCodeAt(i);
|
|
67
|
+
while (c >= 48 && c <= 57) {
|
|
68
|
+
m = m * 10 + (c - 48);
|
|
69
|
+
c = s.charCodeAt(++i);
|
|
70
|
+
}
|
|
71
|
+
frac = i - fracStart;
|
|
72
|
+
digits += frac;
|
|
73
|
+
}
|
|
74
|
+
if (!digits) return -1;
|
|
75
|
+
let e = 0;
|
|
76
|
+
if (c === 101 || c === 69) {
|
|
77
|
+
// the exponent belongs to the number only when digits follow
|
|
78
|
+
let j = i + 1;
|
|
79
|
+
let c2 = s.charCodeAt(j);
|
|
80
|
+
const eneg = c2 === 45;
|
|
81
|
+
if (c2 === 43 || c2 === 45) c2 = s.charCodeAt(++j);
|
|
82
|
+
if (c2 >= 48 && c2 <= 57) {
|
|
83
|
+
while (c2 >= 48 && c2 <= 57) {
|
|
84
|
+
e = e * 10 + (c2 - 48);
|
|
85
|
+
c2 = s.charCodeAt(++j);
|
|
86
|
+
}
|
|
87
|
+
if (eneg) e = -e;
|
|
88
|
+
i = j;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
e -= frac;
|
|
92
|
+
if (m > 9007199254740992 || e > 22 || e < -22) numberValue = Number(s.slice(start, i));
|
|
93
|
+
else {
|
|
94
|
+
const v = e >= 0 ? m * POW10[e] : m / POW10[-e];
|
|
95
|
+
numberValue = neg ? -v : v;
|
|
96
|
+
}
|
|
97
|
+
return i;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {string} s path data
|
|
102
|
+
* @param {2|3} dim
|
|
103
|
+
* @returns {{segments: object[], subpaths: number}}
|
|
104
|
+
*/
|
|
105
|
+
export function parsePathData(s, dim = 2) {
|
|
106
|
+
const counts = dim === 3 ? ARG_COUNT_3D : ARG_COUNT_2D;
|
|
107
|
+
const n = s.length;
|
|
108
|
+
let i = 0;
|
|
109
|
+
const segs = [];
|
|
110
|
+
let cx = 0, cy = 0, cz = 0;
|
|
111
|
+
let sx = 0, sy = 0, sz = 0;
|
|
112
|
+
// previous control point for S/T reflection
|
|
113
|
+
let pcx = 0, pcy = 0, pcz = 0;
|
|
114
|
+
let prev = '';
|
|
115
|
+
let cmd = '';
|
|
116
|
+
let subpaths = 0;
|
|
117
|
+
let pendingMoveAfterZ = false;
|
|
118
|
+
|
|
119
|
+
const skipWs = () => {
|
|
120
|
+
while (i < n && isWs(s.charCodeAt(i))) i++;
|
|
121
|
+
};
|
|
122
|
+
const skipSep = () => {
|
|
123
|
+
skipWs();
|
|
124
|
+
if (s.charCodeAt(i) === 44) {
|
|
125
|
+
i++;
|
|
126
|
+
skipWs();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const num = () => {
|
|
130
|
+
skipSep();
|
|
131
|
+
const end = readNumber(s, i);
|
|
132
|
+
if (end < 0) {
|
|
133
|
+
if (i >= n) throw new PathError(`Command '${cmd}' expects ${counts[cmd.toUpperCase()]} numbers but the path data ended.`, Math.max(0, n - 1), 1);
|
|
134
|
+
throw new PathError(`Expected a number in '${cmd}' command but found '${s[i]}'.`, i, 1);
|
|
135
|
+
}
|
|
136
|
+
i = end;
|
|
137
|
+
return numberValue;
|
|
138
|
+
};
|
|
139
|
+
const flag = () => {
|
|
140
|
+
skipSep();
|
|
141
|
+
const c = s.charCodeAt(i);
|
|
142
|
+
if (c !== 48 && c !== 49) throw new PathError("Arc flags must be '0' or '1'.", i, 1);
|
|
143
|
+
i++;
|
|
144
|
+
return c === 49 ? 1 : 0;
|
|
145
|
+
};
|
|
146
|
+
const startsNumber = () => {
|
|
147
|
+
let j = i;
|
|
148
|
+
while (j < n && isWs(s.charCodeAt(j))) j++;
|
|
149
|
+
if (s.charCodeAt(j) === 44) {
|
|
150
|
+
j++;
|
|
151
|
+
while (j < n && isWs(s.charCodeAt(j))) j++;
|
|
152
|
+
}
|
|
153
|
+
const c = s.charCodeAt(j);
|
|
154
|
+
return (c >= 48 && c <= 57) || c === 45 || c === 43 || c === 46;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
skipWs();
|
|
158
|
+
if (i >= n) return { segments: segs, subpaths: 0 };
|
|
159
|
+
|
|
160
|
+
while (true) {
|
|
161
|
+
skipWs();
|
|
162
|
+
if (i >= n) break;
|
|
163
|
+
if (segs.length >= MAX_PATH_COMMANDS) {
|
|
164
|
+
throw new PathError(`Path data has more than ${MAX_PATH_COMMANDS.toLocaleString('en-US')} commands.`, i, 1, 'E_PATH_LIMIT');
|
|
165
|
+
}
|
|
166
|
+
const c = s[i];
|
|
167
|
+
const code = s.charCodeAt(i);
|
|
168
|
+
const isLetter = (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
169
|
+
const segStart = i;
|
|
170
|
+
if (isLetter) {
|
|
171
|
+
const up = c.toUpperCase();
|
|
172
|
+
if (!(up in ARG_COUNT_2D)) throw new PathError(`Unknown path command '${c}'. Valid commands: M L H V C S Q T A Z.`, i, 1);
|
|
173
|
+
if (!(up in counts)) throw new PathError(`Command '${c}' is not available in 3D path data (use M L C S Q T Z).`, i, 1);
|
|
174
|
+
if (!cmd && up !== 'M') throw new PathError('Path data must begin with a moveto command (M or m).', i, 1);
|
|
175
|
+
cmd = c;
|
|
176
|
+
i++;
|
|
177
|
+
if (up === 'Z') {
|
|
178
|
+
// closepath
|
|
179
|
+
segs.push({ c: 'Z', o: segStart });
|
|
180
|
+
cx = sx;
|
|
181
|
+
cy = sy;
|
|
182
|
+
cz = sz;
|
|
183
|
+
prev = 'Z';
|
|
184
|
+
pendingMoveAfterZ = true;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (!startsNumber()) {
|
|
188
|
+
skipWs();
|
|
189
|
+
throw new PathError(`Command '${c}' expects ${counts[up]} numbers.`, Math.min(i, n - 1), 1);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
if (!cmd) throw new PathError('Path data must begin with a moveto command (M or m).', i, 1);
|
|
193
|
+
if (cmd === 'Z' || cmd === 'z') throw new PathError(`Unexpected '${c}' after closepath; start a new command.`, i, 1);
|
|
194
|
+
if (!startsNumber()) throw new PathError(`Unexpected character '${c}' in path data.`, i, 1);
|
|
195
|
+
// implicit repetition: subsequent moveto pairs are lineto
|
|
196
|
+
if (cmd === 'M') cmd = 'L';
|
|
197
|
+
else if (cmd === 'm') cmd = 'l';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const rel = cmd === cmd.toLowerCase();
|
|
201
|
+
const up = cmd.toUpperCase();
|
|
202
|
+
if (pendingMoveAfterZ && up !== 'M') {
|
|
203
|
+
// a subpath that continues after Z starts at the previous subpath's start point
|
|
204
|
+
segs.push({ c: 'M', x: sx, y: sy, z: sz, o: segStart, implicit: true });
|
|
205
|
+
subpaths++;
|
|
206
|
+
}
|
|
207
|
+
pendingMoveAfterZ = false;
|
|
208
|
+
const ox = rel ? cx : 0, oy = rel ? cy : 0, oz = rel ? cz : 0;
|
|
209
|
+
|
|
210
|
+
switch (up) {
|
|
211
|
+
case 'M': {
|
|
212
|
+
const x = num() + ox, y = num() + oy;
|
|
213
|
+
const z = dim === 3 ? num() + oz : 0;
|
|
214
|
+
segs.push({ c: 'M', x, y, z, o: segStart });
|
|
215
|
+
cx = sx = x;
|
|
216
|
+
cy = sy = y;
|
|
217
|
+
cz = sz = z;
|
|
218
|
+
subpaths++;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case 'L': {
|
|
222
|
+
const x = num() + ox, y = num() + oy;
|
|
223
|
+
const z = dim === 3 ? num() + oz : 0;
|
|
224
|
+
segs.push({ c: 'L', x, y, z, o: segStart });
|
|
225
|
+
cx = x;
|
|
226
|
+
cy = y;
|
|
227
|
+
cz = z;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case 'H': {
|
|
231
|
+
const x = num() + ox;
|
|
232
|
+
segs.push({ c: 'L', x, y: cy, z: 0, o: segStart });
|
|
233
|
+
cx = x;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case 'V': {
|
|
237
|
+
const y = num() + oy;
|
|
238
|
+
segs.push({ c: 'L', x: cx, y, z: 0, o: segStart });
|
|
239
|
+
cy = y;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case 'C': {
|
|
243
|
+
const x1 = num() + ox, y1 = num() + oy, z1 = dim === 3 ? num() + oz : 0;
|
|
244
|
+
const x2 = num() + ox, y2 = num() + oy, z2 = dim === 3 ? num() + oz : 0;
|
|
245
|
+
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
246
|
+
segs.push({ c: 'C', x1, y1, z1, x2, y2, z2, x, y, z, o: segStart });
|
|
247
|
+
pcx = x2; pcy = y2; pcz = z2;
|
|
248
|
+
cx = x; cy = y; cz = z;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case 'S': {
|
|
252
|
+
let x1 = cx, y1 = cy, z1 = cz;
|
|
253
|
+
if (prev === 'C' || prev === 'S') {
|
|
254
|
+
x1 = 2 * cx - pcx;
|
|
255
|
+
y1 = 2 * cy - pcy;
|
|
256
|
+
z1 = 2 * cz - pcz;
|
|
257
|
+
}
|
|
258
|
+
const x2 = num() + ox, y2 = num() + oy, z2 = dim === 3 ? num() + oz : 0;
|
|
259
|
+
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
260
|
+
segs.push({ c: 'C', x1, y1, z1, x2, y2, z2, x, y, z, o: segStart });
|
|
261
|
+
pcx = x2; pcy = y2; pcz = z2;
|
|
262
|
+
cx = x; cy = y; cz = z;
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case 'Q': {
|
|
266
|
+
const x1 = num() + ox, y1 = num() + oy, z1 = dim === 3 ? num() + oz : 0;
|
|
267
|
+
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
268
|
+
segs.push({ c: 'Q', x1, y1, z1, x, y, z, o: segStart });
|
|
269
|
+
pcx = x1; pcy = y1; pcz = z1;
|
|
270
|
+
cx = x; cy = y; cz = z;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case 'T': {
|
|
274
|
+
let x1 = cx, y1 = cy, z1 = cz;
|
|
275
|
+
if (prev === 'Q' || prev === 'T') {
|
|
276
|
+
x1 = 2 * cx - pcx;
|
|
277
|
+
y1 = 2 * cy - pcy;
|
|
278
|
+
z1 = 2 * cz - pcz;
|
|
279
|
+
}
|
|
280
|
+
const x = num() + ox, y = num() + oy, z = dim === 3 ? num() + oz : 0;
|
|
281
|
+
segs.push({ c: 'Q', x1, y1, z1, x, y, z, o: segStart });
|
|
282
|
+
pcx = x1; pcy = y1; pcz = z1;
|
|
283
|
+
cx = x; cy = y; cz = z;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case 'A': {
|
|
287
|
+
const rx = num();
|
|
288
|
+
const ry = num();
|
|
289
|
+
const rot = num();
|
|
290
|
+
const large = flag();
|
|
291
|
+
const sweep = flag();
|
|
292
|
+
const x = num() + ox, y = num() + oy;
|
|
293
|
+
if (rx < 0 || ry < 0) throw new PathError('Arc radii must not be negative.', segStart, i - segStart);
|
|
294
|
+
segs.push({ c: 'A', rx, ry, rot, large, sweep, x0: cx, y0: cy, x, y, z: 0, o: segStart });
|
|
295
|
+
cx = x;
|
|
296
|
+
cy = y;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
default:
|
|
300
|
+
throw new PathError(`Unsupported command '${cmd}'.`, segStart, 1);
|
|
301
|
+
}
|
|
302
|
+
prev = up;
|
|
303
|
+
}
|
|
304
|
+
return { segments: segs, subpaths };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Axis-aligned bounds of the control polygon (a conservative bound for curves; arcs use their endpoints and radii). */
|
|
308
|
+
export function pathControlBounds(segments) {
|
|
309
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
310
|
+
const add = (x, y) => {
|
|
311
|
+
if (x < minX) minX = x;
|
|
312
|
+
if (y < minY) minY = y;
|
|
313
|
+
if (x > maxX) maxX = x;
|
|
314
|
+
if (y > maxY) maxY = y;
|
|
315
|
+
};
|
|
316
|
+
for (const s of segments) {
|
|
317
|
+
if (s.c === 'Z') continue;
|
|
318
|
+
add(s.x, s.y);
|
|
319
|
+
if (s.c === 'C') {
|
|
320
|
+
add(s.x1, s.y1);
|
|
321
|
+
add(s.x2, s.y2);
|
|
322
|
+
} else if (s.c === 'Q') add(s.x1, s.y1);
|
|
323
|
+
else if (s.c === 'A') {
|
|
324
|
+
const r = Math.max(s.rx, s.ry);
|
|
325
|
+
add(s.x - r, s.y - r);
|
|
326
|
+
add(s.x + r, s.y + r);
|
|
327
|
+
add(s.x0 - r, s.y0 - r);
|
|
328
|
+
add(s.x0 + r, s.y0 + r);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return { minX, minY, maxX, maxY };
|
|
332
|
+
}
|