rendx-path 0.1.1 → 0.2.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/dist/main.cjs +228 -2
- package/dist/main.d.cts +12 -1
- package/dist/main.d.ts +12 -1
- package/dist/main.js +226 -1
- package/package.json +1 -1
package/dist/main.cjs
CHANGED
|
@@ -27,7 +27,8 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
|
|
|
27
27
|
// src/main.ts
|
|
28
28
|
var main_exports = {};
|
|
29
29
|
__export(main_exports, {
|
|
30
|
-
Path: () => Path
|
|
30
|
+
Path: () => Path,
|
|
31
|
+
pathBBox: () => pathBBox
|
|
31
32
|
});
|
|
32
33
|
module.exports = __toCommonJS(main_exports);
|
|
33
34
|
|
|
@@ -125,7 +126,232 @@ var Path = class {
|
|
|
125
126
|
}
|
|
126
127
|
};
|
|
127
128
|
_d = new WeakMap();
|
|
129
|
+
|
|
130
|
+
// src/bbox.ts
|
|
131
|
+
function pathBBox(d) {
|
|
132
|
+
if (!d) return null;
|
|
133
|
+
let minX = Infinity;
|
|
134
|
+
let minY = Infinity;
|
|
135
|
+
let maxX = -Infinity;
|
|
136
|
+
let maxY = -Infinity;
|
|
137
|
+
let hasPoint = false;
|
|
138
|
+
let cx = 0;
|
|
139
|
+
let cy = 0;
|
|
140
|
+
let mx = 0;
|
|
141
|
+
let my = 0;
|
|
142
|
+
const mark = (x, y) => {
|
|
143
|
+
if (x < minX) minX = x;
|
|
144
|
+
if (x > maxX) maxX = x;
|
|
145
|
+
if (y < minY) minY = y;
|
|
146
|
+
if (y > maxY) maxY = y;
|
|
147
|
+
hasPoint = true;
|
|
148
|
+
};
|
|
149
|
+
const tokens = d.match(/[a-zA-Z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/g);
|
|
150
|
+
if (!tokens) return null;
|
|
151
|
+
let i = 0;
|
|
152
|
+
const num = () => {
|
|
153
|
+
if (i >= tokens.length) return 0;
|
|
154
|
+
return parseFloat(tokens[i++]);
|
|
155
|
+
};
|
|
156
|
+
const isNum = () => {
|
|
157
|
+
if (i >= tokens.length) return false;
|
|
158
|
+
const t = tokens[i];
|
|
159
|
+
return t !== "" && !/^[a-zA-Z]$/.test(t);
|
|
160
|
+
};
|
|
161
|
+
while (i < tokens.length) {
|
|
162
|
+
const cmd = tokens[i++];
|
|
163
|
+
switch (cmd) {
|
|
164
|
+
// ── MoveTo ──
|
|
165
|
+
case "M":
|
|
166
|
+
cx = num();
|
|
167
|
+
cy = num();
|
|
168
|
+
mx = cx;
|
|
169
|
+
my = cy;
|
|
170
|
+
mark(cx, cy);
|
|
171
|
+
while (isNum()) {
|
|
172
|
+
cx = num();
|
|
173
|
+
cy = num();
|
|
174
|
+
mark(cx, cy);
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
case "m":
|
|
178
|
+
cx += num();
|
|
179
|
+
cy += num();
|
|
180
|
+
mx = cx;
|
|
181
|
+
my = cy;
|
|
182
|
+
mark(cx, cy);
|
|
183
|
+
while (isNum()) {
|
|
184
|
+
cx += num();
|
|
185
|
+
cy += num();
|
|
186
|
+
mark(cx, cy);
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
// ── LineTo ──
|
|
190
|
+
case "L":
|
|
191
|
+
while (isNum()) {
|
|
192
|
+
cx = num();
|
|
193
|
+
cy = num();
|
|
194
|
+
mark(cx, cy);
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
case "l":
|
|
198
|
+
while (isNum()) {
|
|
199
|
+
cx += num();
|
|
200
|
+
cy += num();
|
|
201
|
+
mark(cx, cy);
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
// ── Horizontal LineTo ──
|
|
205
|
+
case "H":
|
|
206
|
+
while (isNum()) {
|
|
207
|
+
cx = num();
|
|
208
|
+
mark(cx, cy);
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
case "h":
|
|
212
|
+
while (isNum()) {
|
|
213
|
+
cx += num();
|
|
214
|
+
mark(cx, cy);
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
// ── Vertical LineTo ──
|
|
218
|
+
case "V":
|
|
219
|
+
while (isNum()) {
|
|
220
|
+
cy = num();
|
|
221
|
+
mark(cx, cy);
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
case "v":
|
|
225
|
+
while (isNum()) {
|
|
226
|
+
cy += num();
|
|
227
|
+
mark(cx, cy);
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
// ── Cubic Bézier ──
|
|
231
|
+
case "C":
|
|
232
|
+
while (isNum()) {
|
|
233
|
+
const x1 = num(), y1 = num();
|
|
234
|
+
const x2 = num(), y2 = num();
|
|
235
|
+
const x = num(), y = num();
|
|
236
|
+
mark(x1, y1);
|
|
237
|
+
mark(x2, y2);
|
|
238
|
+
mark(x, y);
|
|
239
|
+
cx = x;
|
|
240
|
+
cy = y;
|
|
241
|
+
}
|
|
242
|
+
break;
|
|
243
|
+
case "c":
|
|
244
|
+
while (isNum()) {
|
|
245
|
+
const x1 = cx + num(), y1 = cy + num();
|
|
246
|
+
const x2 = cx + num(), y2 = cy + num();
|
|
247
|
+
const x = cx + num(), y = cy + num();
|
|
248
|
+
mark(x1, y1);
|
|
249
|
+
mark(x2, y2);
|
|
250
|
+
mark(x, y);
|
|
251
|
+
cx = x;
|
|
252
|
+
cy = y;
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
// ── Quadratic Bézier ──
|
|
256
|
+
case "Q":
|
|
257
|
+
while (isNum()) {
|
|
258
|
+
const x1 = num(), y1 = num();
|
|
259
|
+
const x = num(), y = num();
|
|
260
|
+
mark(x1, y1);
|
|
261
|
+
mark(x, y);
|
|
262
|
+
cx = x;
|
|
263
|
+
cy = y;
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
case "q":
|
|
267
|
+
while (isNum()) {
|
|
268
|
+
const x1 = cx + num(), y1 = cy + num();
|
|
269
|
+
const x = cx + num(), y = cy + num();
|
|
270
|
+
mark(x1, y1);
|
|
271
|
+
mark(x, y);
|
|
272
|
+
cx = x;
|
|
273
|
+
cy = y;
|
|
274
|
+
}
|
|
275
|
+
break;
|
|
276
|
+
// ── Smooth Cubic Bézier ──
|
|
277
|
+
case "S":
|
|
278
|
+
while (isNum()) {
|
|
279
|
+
const x2 = num(), y2 = num();
|
|
280
|
+
const x = num(), y = num();
|
|
281
|
+
mark(x2, y2);
|
|
282
|
+
mark(x, y);
|
|
283
|
+
cx = x;
|
|
284
|
+
cy = y;
|
|
285
|
+
}
|
|
286
|
+
break;
|
|
287
|
+
case "s":
|
|
288
|
+
while (isNum()) {
|
|
289
|
+
const x2 = cx + num(), y2 = cy + num();
|
|
290
|
+
const x = cx + num(), y = cy + num();
|
|
291
|
+
mark(x2, y2);
|
|
292
|
+
mark(x, y);
|
|
293
|
+
cx = x;
|
|
294
|
+
cy = y;
|
|
295
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
// ── Smooth Quadratic Bézier ──
|
|
298
|
+
case "T":
|
|
299
|
+
while (isNum()) {
|
|
300
|
+
cx = num();
|
|
301
|
+
cy = num();
|
|
302
|
+
mark(cx, cy);
|
|
303
|
+
}
|
|
304
|
+
break;
|
|
305
|
+
case "t":
|
|
306
|
+
while (isNum()) {
|
|
307
|
+
cx += num();
|
|
308
|
+
cy += num();
|
|
309
|
+
mark(cx, cy);
|
|
310
|
+
}
|
|
311
|
+
break;
|
|
312
|
+
// ── Arc ──
|
|
313
|
+
case "A":
|
|
314
|
+
while (isNum()) {
|
|
315
|
+
const rx = num(), ry = num();
|
|
316
|
+
num();
|
|
317
|
+
num();
|
|
318
|
+
num();
|
|
319
|
+
const x = num(), y = num();
|
|
320
|
+
mark(cx - rx, cy - ry);
|
|
321
|
+
mark(cx + rx, cy + ry);
|
|
322
|
+
mark(x - rx, y - ry);
|
|
323
|
+
mark(x + rx, y + ry);
|
|
324
|
+
cx = x;
|
|
325
|
+
cy = y;
|
|
326
|
+
}
|
|
327
|
+
break;
|
|
328
|
+
case "a":
|
|
329
|
+
while (isNum()) {
|
|
330
|
+
const rx = num(), ry = num();
|
|
331
|
+
num();
|
|
332
|
+
num();
|
|
333
|
+
num();
|
|
334
|
+
const x = cx + num(), y = cy + num();
|
|
335
|
+
mark(cx - rx, cy - ry);
|
|
336
|
+
mark(cx + rx, cy + ry);
|
|
337
|
+
mark(x - rx, y - ry);
|
|
338
|
+
mark(x + rx, y + ry);
|
|
339
|
+
cx = x;
|
|
340
|
+
cy = y;
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
// ── ClosePath ──
|
|
344
|
+
case "Z":
|
|
345
|
+
case "z":
|
|
346
|
+
cx = mx;
|
|
347
|
+
cy = my;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return hasPoint ? [minX, minY, maxX, maxY] : null;
|
|
352
|
+
}
|
|
128
353
|
// Annotate the CommonJS export names for ESM import in node:
|
|
129
354
|
0 && (module.exports = {
|
|
130
|
-
Path
|
|
355
|
+
Path,
|
|
356
|
+
pathBBox
|
|
131
357
|
});
|
package/dist/main.d.cts
CHANGED
|
@@ -57,4 +57,15 @@ declare class Path {
|
|
|
57
57
|
Z(): this;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
/**
|
|
61
|
+
* 从 SVG path `d` 字符串中解析所有坐标点,计算轴对齐包围盒 (AABB)。
|
|
62
|
+
*
|
|
63
|
+
* 支持的命令:M/m、L/l、H/h、V/v、C/c、Q/q、A/a、Z/z
|
|
64
|
+
* 对于贝塞尔曲线,包含控制点在内的保守包围盒(不做精确曲线求解,
|
|
65
|
+
* 控制点保证凸包包含曲线,作为 AABB 的上界已经足够紧凑)。
|
|
66
|
+
*
|
|
67
|
+
* @returns `[minX, minY, maxX, maxY]`,空路径返回 `null`
|
|
68
|
+
*/
|
|
69
|
+
declare function pathBBox(d: string): [number, number, number, number] | null;
|
|
70
|
+
|
|
71
|
+
export { Path, pathBBox };
|
package/dist/main.d.ts
CHANGED
|
@@ -57,4 +57,15 @@ declare class Path {
|
|
|
57
57
|
Z(): this;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
/**
|
|
61
|
+
* 从 SVG path `d` 字符串中解析所有坐标点,计算轴对齐包围盒 (AABB)。
|
|
62
|
+
*
|
|
63
|
+
* 支持的命令:M/m、L/l、H/h、V/v、C/c、Q/q、A/a、Z/z
|
|
64
|
+
* 对于贝塞尔曲线,包含控制点在内的保守包围盒(不做精确曲线求解,
|
|
65
|
+
* 控制点保证凸包包含曲线,作为 AABB 的上界已经足够紧凑)。
|
|
66
|
+
*
|
|
67
|
+
* @returns `[minX, minY, maxX, maxY]`,空路径返回 `null`
|
|
68
|
+
*/
|
|
69
|
+
declare function pathBBox(d: string): [number, number, number, number] | null;
|
|
70
|
+
|
|
71
|
+
export { Path, pathBBox };
|
package/dist/main.js
CHANGED
|
@@ -100,6 +100,231 @@ var Path = class {
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
_d = new WeakMap();
|
|
103
|
+
|
|
104
|
+
// src/bbox.ts
|
|
105
|
+
function pathBBox(d) {
|
|
106
|
+
if (!d) return null;
|
|
107
|
+
let minX = Infinity;
|
|
108
|
+
let minY = Infinity;
|
|
109
|
+
let maxX = -Infinity;
|
|
110
|
+
let maxY = -Infinity;
|
|
111
|
+
let hasPoint = false;
|
|
112
|
+
let cx = 0;
|
|
113
|
+
let cy = 0;
|
|
114
|
+
let mx = 0;
|
|
115
|
+
let my = 0;
|
|
116
|
+
const mark = (x, y) => {
|
|
117
|
+
if (x < minX) minX = x;
|
|
118
|
+
if (x > maxX) maxX = x;
|
|
119
|
+
if (y < minY) minY = y;
|
|
120
|
+
if (y > maxY) maxY = y;
|
|
121
|
+
hasPoint = true;
|
|
122
|
+
};
|
|
123
|
+
const tokens = d.match(/[a-zA-Z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/g);
|
|
124
|
+
if (!tokens) return null;
|
|
125
|
+
let i = 0;
|
|
126
|
+
const num = () => {
|
|
127
|
+
if (i >= tokens.length) return 0;
|
|
128
|
+
return parseFloat(tokens[i++]);
|
|
129
|
+
};
|
|
130
|
+
const isNum = () => {
|
|
131
|
+
if (i >= tokens.length) return false;
|
|
132
|
+
const t = tokens[i];
|
|
133
|
+
return t !== "" && !/^[a-zA-Z]$/.test(t);
|
|
134
|
+
};
|
|
135
|
+
while (i < tokens.length) {
|
|
136
|
+
const cmd = tokens[i++];
|
|
137
|
+
switch (cmd) {
|
|
138
|
+
// ── MoveTo ──
|
|
139
|
+
case "M":
|
|
140
|
+
cx = num();
|
|
141
|
+
cy = num();
|
|
142
|
+
mx = cx;
|
|
143
|
+
my = cy;
|
|
144
|
+
mark(cx, cy);
|
|
145
|
+
while (isNum()) {
|
|
146
|
+
cx = num();
|
|
147
|
+
cy = num();
|
|
148
|
+
mark(cx, cy);
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
case "m":
|
|
152
|
+
cx += num();
|
|
153
|
+
cy += num();
|
|
154
|
+
mx = cx;
|
|
155
|
+
my = cy;
|
|
156
|
+
mark(cx, cy);
|
|
157
|
+
while (isNum()) {
|
|
158
|
+
cx += num();
|
|
159
|
+
cy += num();
|
|
160
|
+
mark(cx, cy);
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
// ── LineTo ──
|
|
164
|
+
case "L":
|
|
165
|
+
while (isNum()) {
|
|
166
|
+
cx = num();
|
|
167
|
+
cy = num();
|
|
168
|
+
mark(cx, cy);
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
case "l":
|
|
172
|
+
while (isNum()) {
|
|
173
|
+
cx += num();
|
|
174
|
+
cy += num();
|
|
175
|
+
mark(cx, cy);
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
// ── Horizontal LineTo ──
|
|
179
|
+
case "H":
|
|
180
|
+
while (isNum()) {
|
|
181
|
+
cx = num();
|
|
182
|
+
mark(cx, cy);
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
case "h":
|
|
186
|
+
while (isNum()) {
|
|
187
|
+
cx += num();
|
|
188
|
+
mark(cx, cy);
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
// ── Vertical LineTo ──
|
|
192
|
+
case "V":
|
|
193
|
+
while (isNum()) {
|
|
194
|
+
cy = num();
|
|
195
|
+
mark(cx, cy);
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
case "v":
|
|
199
|
+
while (isNum()) {
|
|
200
|
+
cy += num();
|
|
201
|
+
mark(cx, cy);
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
// ── Cubic Bézier ──
|
|
205
|
+
case "C":
|
|
206
|
+
while (isNum()) {
|
|
207
|
+
const x1 = num(), y1 = num();
|
|
208
|
+
const x2 = num(), y2 = num();
|
|
209
|
+
const x = num(), y = num();
|
|
210
|
+
mark(x1, y1);
|
|
211
|
+
mark(x2, y2);
|
|
212
|
+
mark(x, y);
|
|
213
|
+
cx = x;
|
|
214
|
+
cy = y;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case "c":
|
|
218
|
+
while (isNum()) {
|
|
219
|
+
const x1 = cx + num(), y1 = cy + num();
|
|
220
|
+
const x2 = cx + num(), y2 = cy + num();
|
|
221
|
+
const x = cx + num(), y = cy + num();
|
|
222
|
+
mark(x1, y1);
|
|
223
|
+
mark(x2, y2);
|
|
224
|
+
mark(x, y);
|
|
225
|
+
cx = x;
|
|
226
|
+
cy = y;
|
|
227
|
+
}
|
|
228
|
+
break;
|
|
229
|
+
// ── Quadratic Bézier ──
|
|
230
|
+
case "Q":
|
|
231
|
+
while (isNum()) {
|
|
232
|
+
const x1 = num(), y1 = num();
|
|
233
|
+
const x = num(), y = num();
|
|
234
|
+
mark(x1, y1);
|
|
235
|
+
mark(x, y);
|
|
236
|
+
cx = x;
|
|
237
|
+
cy = y;
|
|
238
|
+
}
|
|
239
|
+
break;
|
|
240
|
+
case "q":
|
|
241
|
+
while (isNum()) {
|
|
242
|
+
const x1 = cx + num(), y1 = cy + num();
|
|
243
|
+
const x = cx + num(), y = cy + num();
|
|
244
|
+
mark(x1, y1);
|
|
245
|
+
mark(x, y);
|
|
246
|
+
cx = x;
|
|
247
|
+
cy = y;
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
// ── Smooth Cubic Bézier ──
|
|
251
|
+
case "S":
|
|
252
|
+
while (isNum()) {
|
|
253
|
+
const x2 = num(), y2 = num();
|
|
254
|
+
const x = num(), y = num();
|
|
255
|
+
mark(x2, y2);
|
|
256
|
+
mark(x, y);
|
|
257
|
+
cx = x;
|
|
258
|
+
cy = y;
|
|
259
|
+
}
|
|
260
|
+
break;
|
|
261
|
+
case "s":
|
|
262
|
+
while (isNum()) {
|
|
263
|
+
const x2 = cx + num(), y2 = cy + num();
|
|
264
|
+
const x = cx + num(), y = cy + num();
|
|
265
|
+
mark(x2, y2);
|
|
266
|
+
mark(x, y);
|
|
267
|
+
cx = x;
|
|
268
|
+
cy = y;
|
|
269
|
+
}
|
|
270
|
+
break;
|
|
271
|
+
// ── Smooth Quadratic Bézier ──
|
|
272
|
+
case "T":
|
|
273
|
+
while (isNum()) {
|
|
274
|
+
cx = num();
|
|
275
|
+
cy = num();
|
|
276
|
+
mark(cx, cy);
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
case "t":
|
|
280
|
+
while (isNum()) {
|
|
281
|
+
cx += num();
|
|
282
|
+
cy += num();
|
|
283
|
+
mark(cx, cy);
|
|
284
|
+
}
|
|
285
|
+
break;
|
|
286
|
+
// ── Arc ──
|
|
287
|
+
case "A":
|
|
288
|
+
while (isNum()) {
|
|
289
|
+
const rx = num(), ry = num();
|
|
290
|
+
num();
|
|
291
|
+
num();
|
|
292
|
+
num();
|
|
293
|
+
const x = num(), y = num();
|
|
294
|
+
mark(cx - rx, cy - ry);
|
|
295
|
+
mark(cx + rx, cy + ry);
|
|
296
|
+
mark(x - rx, y - ry);
|
|
297
|
+
mark(x + rx, y + ry);
|
|
298
|
+
cx = x;
|
|
299
|
+
cy = y;
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
case "a":
|
|
303
|
+
while (isNum()) {
|
|
304
|
+
const rx = num(), ry = num();
|
|
305
|
+
num();
|
|
306
|
+
num();
|
|
307
|
+
num();
|
|
308
|
+
const x = cx + num(), y = cy + num();
|
|
309
|
+
mark(cx - rx, cy - ry);
|
|
310
|
+
mark(cx + rx, cy + ry);
|
|
311
|
+
mark(x - rx, y - ry);
|
|
312
|
+
mark(x + rx, y + ry);
|
|
313
|
+
cx = x;
|
|
314
|
+
cy = y;
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
// ── ClosePath ──
|
|
318
|
+
case "Z":
|
|
319
|
+
case "z":
|
|
320
|
+
cx = mx;
|
|
321
|
+
cy = my;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return hasPoint ? [minX, minY, maxX, maxY] : null;
|
|
326
|
+
}
|
|
103
327
|
export {
|
|
104
|
-
Path
|
|
328
|
+
Path,
|
|
329
|
+
pathBBox
|
|
105
330
|
};
|