pantograph2d 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,328 +1,169 @@
1
- import { L as p, A as P, a as L, s as C, p as D, t as Q, c as j, f as ct, b as ft, d as A, e as ut, g as S, h as $, T as lt, D as g, F as T, i as M, j as _, k as V, l as ht, m as gt, n as pt, o as mt, q as Pt, r as wt, u as tt, v as dt, w as At, x as nt, y as St, z as vt, S as Lt } from "./exportJSON-26bb92ef.js";
2
- import { B as Zt } from "./exportJSON-26bb92ef.js";
3
- class F {
4
- constructor(n, e) {
5
- this.firstPoint = n, this.lastPoint = e;
6
- }
7
- }
8
- function k(t, n) {
1
+ import { o as ct, D as b } from "./draw-cd61ec6b.js";
2
+ import { d as _t } from "./draw-cd61ec6b.js";
3
+ import { F as g, L as d, D as p, f as O, r as ut, z as lt, s as pt, a as Pt, c as gt, i as mt, l as wt, d as h, b as dt, p as ht, n as B, e as v, g as St, h as At, j as Mt, k as $t, m as w, A as S, o as H, q as A, t as Lt, u as Ft, v as vt, S as K } from "./Diagram-9915ed5e.js";
4
+ import { w as tn } from "./Diagram-9915ed5e.js";
5
+ function M(t) {
6
+ if (t instanceof g)
7
+ return [t];
8
+ if (t instanceof d)
9
+ return [new g(t)];
9
10
  if (t instanceof p)
10
- return Ct(t, n);
11
- if (t instanceof P)
12
- return Tt(t, n);
13
- throw new Error("Not implemented");
14
- }
15
- function Ct(t, n) {
16
- const { firstPoint: e, lastPoint: r } = t, s = t.normalVector;
17
- return new p(
18
- L(e, C(s, n)),
19
- L(r, C(s, n))
20
- );
11
+ return t.figures;
12
+ throw new Error("Unknown shape");
21
13
  }
22
- function Tt(t, n) {
23
- const e = L(
24
- t.firstPoint,
25
- C(D(t.tangentAtFirstPoint), n)
26
- ), r = L(
27
- t.lastPoint,
28
- C(D(t.tangentAtLastPoint), n)
29
- ), s = n * (t.clockwise ? 1 : -1);
30
- return t.radius + s < t.precision ? new F(e, r) : new P(e, r, t.center, t.clockwise);
31
- }
32
- function et(t, n, e) {
33
- const r = j(
34
- t.tangentAtLastPoint,
35
- n.tangentAtFirstPoint
36
- );
37
- if (Math.abs(r) < 1e-10)
38
- return null;
39
- const s = r > 0 ? 1 : -1, i = Math.abs(e) * s, o = k(t, i), u = k(n, i);
40
- if (o instanceof F || u instanceof F)
41
- return null;
42
- let c;
43
- try {
44
- c = ct(o, u, 1e-9).at(-1);
45
- } catch {
46
- return null;
47
- }
48
- if (!c)
49
- return null;
50
- const m = c, a = (v, b) => {
51
- const w = b.tangentAt(m), d = ft(w), h = L(m, C(d, i));
52
- return v.splitAt(h);
53
- }, [f] = a(t, o), [, l] = a(n, u);
54
- return { first: f, second: l, center: m };
55
- }
56
- function B(t, n, e) {
57
- const r = et(t, n, e);
58
- if (!r)
59
- return console.warn(
60
- "Cannot fillet between segments",
61
- t.repr,
62
- n.repr
63
- ), [t, n];
64
- const { first: s, second: i } = r;
65
- return [
66
- s,
67
- Q(s.lastPoint, i.firstPoint, s.tangentAtLastPoint),
68
- i
69
- ];
70
- }
71
- function G(t, n, e) {
72
- const r = et(t, n, e);
73
- if (!r)
74
- return console.warn(
75
- "Cannot chamfer between segments",
76
- t.repr,
77
- n.repr
78
- ), [t, n];
79
- const { first: s, second: i } = r;
80
- return [s, new p(s.lastPoint, i.firstPoint), i];
81
- }
82
- function N(t) {
83
- return new g([new T(new M(t))]);
84
- }
85
- class Et {
86
- constructor(n = [0, 0]) {
87
- this.pointer = n, this.firstPoint = n, this.pendingSegments = [], this._nextCorner = null;
88
- }
89
- movePointerTo(n) {
90
- if (this.pendingSegments.length)
91
- throw new Error(
92
- "You can only move the pointer if there is no segment defined"
14
+ function Q(t, n, e = 1e-9) {
15
+ let r = [];
16
+ const o = [], s = new Array(n.segments.length).fill(0).map(() => []);
17
+ n.segments.forEach((a, u) => {
18
+ t.segments.forEach((f) => {
19
+ const { intersections: l, overlaps: m } = O(
20
+ a,
21
+ f,
22
+ e
93
23
  );
94
- return this.pointer = n, this.firstPoint = n, this;
95
- }
96
- saveSegment(n) {
97
- if (A(n.firstPoint, n.lastPoint))
98
- throw new Error(`Segment has no length, ${n.repr}`);
99
- if (!this._nextCorner)
100
- return this.pendingSegments.push(n), this;
101
- const e = this.pendingSegments.pop();
102
- if (!e)
103
- throw new Error("bug in the custom corner algorithm");
104
- const r = this._nextCorner.mode === "chamfer" ? G : B;
105
- return this.pendingSegments.push(
106
- ...r(e, n, this._nextCorner.radius)
107
- ), this._nextCorner = null, this;
108
- }
109
- lineTo(n) {
110
- const e = new p(this.pointer, n);
111
- return this.pointer = n, this.saveSegment(e);
112
- }
113
- line(n, e) {
114
- return this.lineTo([this.pointer[0] + n, this.pointer[1] + e]);
115
- }
116
- vLine(n) {
117
- return this.line(0, n);
118
- }
119
- hLine(n) {
120
- return this.line(n, 0);
121
- }
122
- vLineTo(n) {
123
- return this.lineTo([this.pointer[0], n]);
124
- }
125
- hLineTo(n) {
126
- return this.lineTo([n, this.pointer[1]]);
127
- }
128
- polarLineTo([n, e]) {
129
- const r = e * V, s = _(n, r);
130
- return this.lineTo(s);
131
- }
132
- polarLine(n, e) {
133
- const r = e * V, [s, i] = _(n, r);
134
- return this.line(s, i);
135
- }
136
- tangentLine(n) {
137
- const e = this.pendingSegments.at(-1);
138
- if (!e)
139
- throw new Error("You need a previous segment to sketch a tangent line");
140
- const [r, s] = e.tangentAtLastPoint;
141
- return this.line(r * n, s * n);
142
- }
143
- threePointsArcTo(n, e) {
144
- return this.saveSegment(ut(this.pointer, e, n)), this.pointer = n, this;
145
- }
146
- threePointsArc(n, e, r, s) {
147
- const [i, o] = this.pointer;
148
- return this.threePointsArcTo(
149
- [i + n, o + e],
150
- [i + r, o + s]
151
- );
152
- }
153
- sagittaArcTo(n, e) {
154
- if (!e)
155
- return this.lineTo(n);
156
- const r = new p(this.pointer, n), s = D(r.tangentAtFirstPoint), i = L(r.midPoint, C(s, e));
157
- return this.threePointsArcTo(n, i);
158
- }
159
- sagittaArc(n, e, r) {
160
- return this.sagittaArcTo(
161
- [n + this.pointer[0], e + this.pointer[1]],
162
- r
163
- );
164
- }
165
- vSagittaArc(n, e) {
166
- return this.sagittaArc(0, n, e);
167
- }
168
- hSagittaArc(n, e) {
169
- return this.sagittaArc(n, 0, e);
170
- }
171
- bulgeArcTo(n, e) {
172
- if (!e)
173
- return this.lineTo(n);
174
- const r = S(this.pointer, n) / 2, s = -e * r;
175
- return this.sagittaArcTo(n, s);
176
- }
177
- bulgeArc(n, e, r) {
178
- return this.bulgeArcTo(
179
- [n + this.pointer[0], e + this.pointer[1]],
180
- r
181
- );
182
- }
183
- vBulgeArc(n, e) {
184
- return this.bulgeArc(0, n, e);
185
- }
186
- hBulgeArc(n, e) {
187
- return this.bulgeArc(n, 0, e);
188
- }
189
- tangentArcTo(n) {
190
- const e = this.pendingSegments.at(-1);
191
- if (!e)
192
- throw new Error("You need a previous curve to sketch a tangent arc");
193
- return this.saveSegment(
194
- Q(this.pointer, n, e.tangentAtLastPoint)
195
- ), this.pointer = n, this;
196
- }
197
- tangentArc(n, e) {
198
- const [r, s] = this.pointer;
199
- return this.tangentArcTo([n + r, e + s]);
200
- }
201
- customCorner(n, e = "fillet") {
202
- if (!this.pendingSegments.length)
203
- throw new Error("You need a segment defined to fillet the angle");
204
- return this._nextCorner = { mode: e, radius: n }, this;
205
- }
206
- _customCornerLastWithFirst(n, e = "fillet") {
207
- if (!n)
208
- return;
209
- const r = this.pendingSegments.pop(), s = this.pendingSegments.shift();
210
- if (!r || !s)
211
- throw new Error("Not enough curves to close and fillet");
212
- const i = e === "chamfer" ? G : B;
213
- this.pendingSegments.push(...i(r, s, n));
214
- }
215
- close() {
216
- if (!this.pendingSegments.length)
217
- throw new Error("No segments to close");
218
- const n = this.pendingSegments[0], e = this.pendingSegments.at(-1);
219
- return A(n.firstPoint, e.lastPoint) || this.lineTo(n.firstPoint), this._nextCorner !== null && (this._customCornerLastWithFirst(
220
- this._nextCorner.radius,
221
- this._nextCorner.mode
222
- ), this._nextCorner = null), N(this.pendingSegments);
223
- }
224
- closeWithMirror() {
225
- if (!this.pendingSegments.length)
226
- throw new Error("No segments to close");
227
- const n = this.pendingSegments[0], e = this.pendingSegments.at(-1), r = $(
228
- e.lastPoint,
229
- n.firstPoint
230
- ), s = new lt().mirrorLine(
24
+ r.push(...l), s[u].push(...l), o.push(...m);
25
+ const P = m.flatMap((c) => [
26
+ c.firstPoint,
27
+ c.lastPoint
28
+ ]);
29
+ r.push(...P), s[u].push(...P);
30
+ });
31
+ }), r = ut(r, e);
32
+ const i = lt([n.segments, s]).flatMap(([a, u]) => u.length ? a.splitAt(u) : [a]);
33
+ return Array.from(
34
+ pt(
35
+ i,
231
36
  r,
232
- n.firstPoint
233
- ), i = this.pendingSegments.map(
234
- (o) => o.transform(s).reverse()
235
- );
236
- return i.reverse(), N([
237
- ...this.pendingSegments,
238
- ...i
239
- ]);
240
- }
37
+ o
38
+ )
39
+ );
241
40
  }
242
- function Gt(t = [0, 0]) {
243
- return new Et(t);
41
+ function C(t, n, e = !1) {
42
+ return Q(n, t).filter((o) => {
43
+ const s = o.segments[0].midPoint;
44
+ return n.onStroke(s) ? !e : !n.contains(s);
45
+ });
244
46
  }
245
- function E(t) {
246
- if (t instanceof T)
247
- return [t];
248
- if (t instanceof M)
249
- return [new T(t)];
250
- if (t instanceof g)
251
- return t.figures;
252
- throw new Error("Unknown shape");
47
+ function G(t, n, e = !1) {
48
+ return Q(n, t).filter((o) => {
49
+ const s = o.segments[0].midPoint;
50
+ return n.onStroke(s) ? !e : n.contains(s);
51
+ });
52
+ }
53
+ function R(t, n, e = !1) {
54
+ const r = C(
55
+ t,
56
+ n.contour,
57
+ e
58
+ ), o = n.holes.flatMap(
59
+ (s) => G(t, s, e)
60
+ );
61
+ return [...r, ...o];
253
62
  }
254
- function Ft(t, n) {
255
- return new g(
256
- ht(E(t), E(n))
63
+ function U(t, n, e = !1) {
64
+ let r = G(
65
+ t,
66
+ n.contour,
67
+ e
68
+ );
69
+ return n.holes.forEach((o) => {
70
+ r = r.flatMap(
71
+ (s) => C(s, o, e)
72
+ );
73
+ }), r;
74
+ }
75
+ function Et(t, n) {
76
+ return new p(
77
+ Pt(M(t), M(n))
257
78
  );
258
79
  }
259
- function U(t) {
80
+ function E(t) {
260
81
  return t.reduce(
261
- (n, e) => Ft(n, e),
262
- new g()
82
+ (n, e) => Et(n, e),
83
+ new p()
263
84
  );
264
85
  }
265
- function Mt(t, n) {
266
- return new g(
267
- gt(E(t), E(n))
86
+ function _(t, n) {
87
+ return new p(
88
+ gt(M(t), M(n))
268
89
  );
269
90
  }
270
91
  function Nt(t, n) {
271
- return new g(
272
- pt(E(t), E(n))
92
+ return new p(
93
+ mt(M(t), M(n))
273
94
  );
274
95
  }
275
- function Y(t) {
96
+ function qt(t, n, e = !0) {
97
+ if (n instanceof d)
98
+ return C(t, n, e);
99
+ if (n instanceof g)
100
+ return R(t, n, e);
101
+ let r = [t];
102
+ return n.figures.forEach((o) => {
103
+ r = r.flatMap((s) => R(s, o, e));
104
+ }), r;
105
+ }
106
+ function Jt(t, n, e = !1) {
107
+ if (n instanceof d)
108
+ return G(t, n, e);
109
+ if (n instanceof g)
110
+ return U(t, n, e);
111
+ let r = [t];
112
+ return n.figures.forEach((o) => {
113
+ r = r.flatMap((s) => U(s, o, e));
114
+ }), r;
115
+ }
116
+ function z(t) {
276
117
  return t < 0 ? "before" : t > 1 ? "after" : "between";
277
118
  }
278
- const q = (t, n, e) => {
119
+ const N = (t, n, e) => {
279
120
  if (e === "before")
280
121
  return t.distanceFrom(n.firstPoint);
281
122
  if (e === "after")
282
123
  return t.distanceFrom(n.lastPoint);
283
124
  throw new Error("Invalid position");
284
125
  };
285
- function bt(t, n) {
286
- const e = mt(t, n);
126
+ function yt(t, n) {
127
+ const e = wt(t, n);
287
128
  if (e === "parallel")
288
129
  return Math.min(
289
130
  t.distanceFrom(n.firstPoint),
290
131
  t.distanceFrom(n.lastPoint)
291
132
  );
292
- const { intersectionParam1: r, intersectionParam2: s } = e, i = Y(r), o = Y(s);
293
- if (i === "between" && o === "between")
133
+ const { intersectionParam1: r, intersectionParam2: o } = e, s = z(r), i = z(o);
134
+ if (s === "between" && i === "between")
294
135
  return 0;
295
- if (i === "between" && o !== "between")
296
- return q(t, n, o);
297
- if (o === "between" && i !== "between")
298
- return q(n, t, i);
299
- if (i === "before" && o === "before")
300
- return S(t.firstPoint, n.firstPoint);
301
- if (i === "after" && o === "after")
302
- return S(t.lastPoint, n.lastPoint);
303
- if (i === "before" && o === "after")
304
- return S(t.firstPoint, n.lastPoint);
305
- if (i === "after" && o === "before")
306
- return S(t.lastPoint, n.firstPoint);
136
+ if (s === "between" && i !== "between")
137
+ return N(t, n, i);
138
+ if (i === "between" && s !== "between")
139
+ return N(n, t, s);
140
+ if (s === "before" && i === "before")
141
+ return h(t.firstPoint, n.firstPoint);
142
+ if (s === "after" && i === "after")
143
+ return h(t.lastPoint, n.lastPoint);
144
+ if (s === "before" && i === "after")
145
+ return h(t.firstPoint, n.lastPoint);
146
+ if (s === "after" && i === "before")
147
+ return h(t.lastPoint, n.firstPoint);
307
148
  throw new Error("Invalid position");
308
149
  }
309
- function z(t, n) {
310
- if (Pt(t, n).length > 0)
150
+ function q(t, n) {
151
+ if (dt(t, n).length > 0)
311
152
  return 0;
312
- const e = wt(t, n.center);
153
+ const e = ht(t, n.center);
313
154
  if (t.isOnSegment(e)) {
314
- const r = S(e, n.center);
155
+ const r = h(e, n.center);
315
156
  if (Math.abs(r - n.radius) < t.precision && n.isOnSegment(e))
316
157
  return 0;
317
158
  if (r - n.radius > t.precision) {
318
- const s = tt(
319
- $(e, n.center)
320
- ), i = L(
159
+ const o = B(
160
+ v(e, n.center)
161
+ ), s = St(
321
162
  n.center,
322
- C(s, n.radius)
163
+ At(o, n.radius)
323
164
  );
324
- if (n.isOnSegment(i))
325
- return S(i, e);
165
+ if (n.isOnSegment(s))
166
+ return h(s, e);
326
167
  }
327
168
  }
328
169
  return Math.min(
@@ -332,277 +173,345 @@ function z(t, n) {
332
173
  t.distanceFrom(n.lastPoint)
333
174
  );
334
175
  }
335
- const $t = (t, n) => {
176
+ const It = (t, n) => {
336
177
  const e = t.angleToParam(n.firstAngle);
337
178
  if (t.isValidParameter(e))
338
179
  return !0;
339
180
  const r = t.angleToParam(n.lastAngle);
340
181
  return !!t.isValidParameter(r);
341
182
  };
342
- function yt(t, n) {
343
- if (dt(t, n, !0).length > 0)
183
+ function bt(t, n) {
184
+ if (Mt(t, n, !0).length > 0)
344
185
  return 0;
345
- const e = S(t.center, n.center);
346
- if (e < t.precision && $t(t, n))
186
+ const e = h(t.center, n.center);
187
+ if (e < t.precision && It(t, n))
347
188
  return Math.abs(t.radius - n.radius);
348
- const r = tt($(n.center, t.center)), s = e - Math.abs(t.radius - n.radius) < t.precision;
349
- let i = At(r);
350
- s && n.radius > t.radius && (i += Math.PI);
351
- const o = s ? i : i + Math.PI, u = t.angleToParam(i), c = n.angleToParam(o);
352
- return t.isValidParameter(u) && n.isValidParameter(c) ? S(t.paramPoint(u), n.paramPoint(c)) : Math.min(
189
+ const r = B(v(n.center, t.center)), o = e - Math.abs(t.radius - n.radius) < t.precision;
190
+ let s = $t(r);
191
+ o && n.radius > t.radius && (s += Math.PI);
192
+ const i = o ? s : s + Math.PI, a = t.angleToParam(s), u = n.angleToParam(i);
193
+ return t.isValidParameter(a) && n.isValidParameter(u) ? h(t.paramPoint(a), n.paramPoint(u)) : Math.min(
353
194
  t.distanceFrom(n.firstPoint),
354
195
  t.distanceFrom(n.lastPoint),
355
196
  n.distanceFrom(t.firstPoint),
356
197
  n.distanceFrom(t.lastPoint)
357
198
  );
358
199
  }
359
- function xt(t, n) {
360
- if (t instanceof p && n instanceof p)
361
- return bt(t, n);
362
- if (t instanceof p && n instanceof P)
363
- return z(t, n);
364
- if (t instanceof P && n instanceof p)
365
- return z(n, t);
366
- if (t instanceof P && n instanceof P)
200
+ function kt(t, n) {
201
+ if (t instanceof w && n instanceof w)
367
202
  return yt(t, n);
203
+ if (t instanceof w && n instanceof S)
204
+ return q(t, n);
205
+ if (t instanceof S && n instanceof w)
206
+ return q(n, t);
207
+ if (t instanceof S && n instanceof S)
208
+ return bt(t, n);
368
209
  throw new Error("Not implemented");
369
210
  }
370
- const I = 1e-8;
371
- function Dt(t, n) {
372
- const e = t.map((c) => ({
373
- offset: k(c, n),
374
- original: c
375
- })), r = [];
376
- let s = null, i = e.at(-1);
377
- if (!i)
378
- return [];
379
- if (r.length === 1)
380
- return r;
381
- const o = (c) => {
382
- s ? c.offset instanceof F ? A(c.offset.firstPoint, c.offset.lastPoint) || r.push(
383
- new p(c.offset.firstPoint, c.offset.lastPoint)
384
- ) : r.push(c.offset) : s = c;
211
+ const V = 1e-8;
212
+ function k(t, n, e = !0) {
213
+ const r = t.map((f) => ({
214
+ offset: ct(f, n),
215
+ original: f
216
+ })), o = [];
217
+ let s = e ? null : r.at(-1), i = e ? r.at(-1) : null;
218
+ if (o.length === 1)
219
+ return o;
220
+ const a = (f) => {
221
+ s ? f.offset instanceof b ? A(f.offset.firstPoint, f.offset.lastPoint) || o.push(
222
+ new w(f.offset.firstPoint, f.offset.lastPoint)
223
+ ) : o.push(f.offset) : s = f;
385
224
  }, u = function* () {
386
- for (const c of e.slice(0, -1))
387
- yield c;
225
+ for (const f of r.slice(0, -1))
226
+ yield f;
388
227
  if (!s)
389
228
  throw new Error("Bug in the offset algorithm");
390
229
  yield s;
391
230
  };
392
- for (const c of u()) {
393
- const m = i.offset.lastPoint, a = c.offset.firstPoint;
394
- if (A(m, a)) {
395
- o(i), i = c;
231
+ for (const f of u()) {
232
+ if (!i) {
233
+ i = f;
396
234
  continue;
397
235
  }
398
- let f = [];
399
- if (!(i.offset instanceof F) && !(c.offset instanceof F)) {
400
- const { intersections: w, overlaps: d } = nt(
236
+ const l = i.offset.lastPoint, m = f.offset.firstPoint;
237
+ if (A(l, m)) {
238
+ a(i), i = f;
239
+ continue;
240
+ }
241
+ let P = [];
242
+ if (!(i.offset instanceof b) && !(f.offset instanceof b)) {
243
+ const { intersections: $, overlaps: I } = O(
401
244
  i.offset,
402
- c.offset,
403
- I / 100
245
+ f.offset,
246
+ V / 100
404
247
  );
405
- f = [
406
- ...w,
407
- ...d.flatMap((h) => [h.firstPoint, h.lastPoint])
248
+ P = [
249
+ ...$,
250
+ ...I.flatMap((L) => [L.firstPoint, L.lastPoint])
408
251
  ];
409
252
  }
410
- if (f.length > 0) {
411
- let w = f[0];
412
- if (f.length > 1) {
413
- const x = i == null ? void 0 : i.original.lastPoint, y = f.map(
414
- (at) => vt(at, x)
253
+ if (P.length > 0) {
254
+ let $ = P[0];
255
+ if (P.length > 1) {
256
+ const ft = i == null ? void 0 : i.original.lastPoint, T = P.map(
257
+ (at) => Lt(at, ft)
415
258
  );
416
- w = f[y.indexOf(Math.min(...y))];
259
+ $ = P[T.indexOf(Math.min(...T))];
417
260
  }
418
- const d = i.offset.splitAt([
419
- w
420
- ])[0], h = c.offset.splitAt([w]).at(-1);
421
- if (!h)
261
+ const I = i.offset.splitAt([
262
+ $
263
+ ])[0], L = f.offset.splitAt([$]).at(-1);
264
+ if (!L)
422
265
  throw new Error("Bug in the splitting algo in offset");
423
- o({
424
- offset: d,
266
+ a({
267
+ offset: I,
425
268
  original: i.original
426
- }), i = { offset: h, original: c.original };
269
+ }), i = { offset: L, original: f.original };
427
270
  continue;
428
271
  }
429
- const l = i.original.lastPoint, v = j(
430
- $(a, l),
431
- $(m, l)
432
- ) > 0, b = new P(m, a, l, v);
433
- o(i), r.push(b), i = c;
434
- }
435
- return o(i), r;
436
- }
437
- function W(t, n) {
438
- const e = t.clockwise ? n : -n, r = Dt(t.segments, e);
439
- if (r.length < 2)
440
- return new g();
441
- const s = /* @__PURE__ */ new Map(), i = (a, f) => {
442
- const l = s.get(a) || [];
443
- s.set(a, [...l, ...f]);
272
+ const c = i.original.lastPoint, F = vt(
273
+ v(m, c),
274
+ v(l, c)
275
+ ) > 0, it = new S(l, m, c, F);
276
+ a(i), o.push(it), i = f;
277
+ }
278
+ return a(i), o;
279
+ }
280
+ function j(t) {
281
+ const n = /* @__PURE__ */ new Map(), e = (r, o) => {
282
+ const s = n.get(r) || [];
283
+ n.set(r, [...s, ...o]);
444
284
  };
445
- if (r.forEach((a, f) => {
446
- r.slice(f + 1).forEach((l, v) => {
447
- const { intersections: b, overlaps: w } = nt(a, l, I), d = [
448
- ...b,
449
- ...w.flatMap((h) => [h.firstPoint, h.lastPoint])
450
- ].filter((h) => {
451
- const x = A(h, a.firstPoint) || A(h, a.lastPoint), y = A(h, l.firstPoint) || A(h, l.lastPoint);
452
- return !(x && y);
285
+ return t.forEach((r, o) => {
286
+ t.slice(o + 1).forEach((s, i) => {
287
+ const { intersections: a, overlaps: u } = O(r, s, V), f = [
288
+ ...a,
289
+ ...u.flatMap((l) => [l.firstPoint, l.lastPoint])
290
+ ].filter((l) => {
291
+ const m = A(l, r.firstPoint) || A(l, r.lastPoint), P = A(l, s.firstPoint) || A(l, s.lastPoint);
292
+ return !(m && P);
453
293
  });
454
- d.length && (i(f, d), i(v + f + 1, d));
294
+ f.length && (e(o, f), e(i + o + 1, f));
455
295
  });
456
- }), !s.size) {
457
- const a = new M(r);
458
- return new g([new T(a)]);
459
- }
460
- const u = r.flatMap((a, f) => {
461
- if (!s.has(f))
462
- return a;
463
- const l = s.get(f) || [];
464
- return a.splitAt(l);
465
- }).filter((a) => !t.segments.some((l) => xt(l, a) < Math.abs(n) - I));
466
- if (!u.length)
467
- return new g();
468
- const m = St(u).filter((a) => a.length > 1).filter((a) => A(a[0].firstPoint, a.at(-1).lastPoint)).map((a) => new M(a));
469
- return m.length ? new g(m.map((a) => new T(a))) : new g();
296
+ }), n;
297
+ }
298
+ function tt(t, n) {
299
+ return n.flatMap((e, r) => {
300
+ if (!t.has(r))
301
+ return e;
302
+ const o = t.get(r);
303
+ return o ? e.splitAt(o) : e;
304
+ });
470
305
  }
471
- function kt(t, n) {
306
+ function nt(t, n, e) {
307
+ return t.filter((r) => !n.segments.some((s) => kt(s, r) < Math.abs(e) - V));
308
+ }
309
+ function y(t, n) {
310
+ const e = t.clockwise ? n : -n, r = k(t.segments, e);
311
+ if (r.length < 2)
312
+ return new p();
313
+ const o = j(r);
314
+ if (!o.size) {
315
+ const f = new d(r);
316
+ return new p([new g(f)]);
317
+ }
318
+ const s = tt(
319
+ o,
320
+ r
321
+ ), i = nt(s, t, n);
322
+ if (!i.length)
323
+ return new p();
324
+ const u = H(i).filter((f) => f.length > 1).filter((f) => A(f[0].firstPoint, f.at(-1).lastPoint)).map((f) => new d(f));
325
+ return u.length ? new p(u.map((f) => new g(f))) : new p();
326
+ }
327
+ function Dt(t, n, e = "round") {
328
+ const r = n / 2, o = k(t.segments, r, !1), s = k(t.segments, -r, !1).map(
329
+ (c) => c.reverse()
330
+ );
331
+ s.reverse();
332
+ const i = (c, F) => e === "round" ? Ft(
333
+ c.lastPoint,
334
+ F.firstPoint,
335
+ c.tangentAtLastPoint
336
+ ) : new w(c.lastPoint, F.firstPoint), a = [
337
+ ...o,
338
+ i(
339
+ o[o.length - 1],
340
+ s[0]
341
+ ),
342
+ ...s,
343
+ i(
344
+ s[s.length - 1],
345
+ o[0]
346
+ )
347
+ ], u = j(a);
348
+ if (!u.size) {
349
+ const c = new d(a);
350
+ return new p([new g(c)]);
351
+ }
352
+ const f = tt(
353
+ u,
354
+ a
355
+ ), l = nt(f, t, r);
356
+ if (!l.length)
357
+ return new p();
358
+ const P = H(l).filter((c) => c.length > 1).filter((c) => A(c[0].firstPoint, c.at(-1).lastPoint)).map((c) => new d(c));
359
+ return P.length ? new p(P.map((c) => new g(c))) : new p();
360
+ }
361
+ function Ot(t, n) {
472
362
  const e = t.map((r) => {
473
- const s = U(
474
- r.holes.map((i) => W(i, n))
363
+ const o = E(
364
+ r.holes.map((s) => y(s, n))
475
365
  );
476
- return Mt(W(r.contour, n), s);
366
+ return _(y(r.contour, n), o);
477
367
  });
478
- return U(e);
368
+ return E(e);
479
369
  }
480
- function Ut(t, n) {
481
- return kt(E(t), n);
370
+ function Ct(t, n) {
371
+ const e = Math.abs(n / 2), r = t.map(
372
+ (o) => E(
373
+ o.allLoops.map((s) => _(y(s, e), y(s, -e)))
374
+ )
375
+ );
376
+ return E(r);
482
377
  }
483
- function R(t) {
484
- if (t instanceof p)
378
+ function Wt(t, n) {
379
+ return Ot(M(t), n);
380
+ }
381
+ function Xt(t, n, { endCap: e = "round" } = {}) {
382
+ return t instanceof K ? Dt(t, n, e) : Ct(M(t), n);
383
+ }
384
+ function x(t) {
385
+ if (t instanceof w)
485
386
  return `L ${t.lastPoint.join(" ")}`;
486
- if (t instanceof P)
387
+ if (t instanceof S)
487
388
  return `A ${t.radius} ${t.radius} 0 ${t.angularLength > Math.PI ? "1" : "0"} ${t.clockwise ? "0" : "1"} ${t.lastPoint.join(" ")}`;
488
389
  throw new Error("Unknown segment type");
489
390
  }
490
- function rt(t) {
491
- const n = `M ${t.firstPoint.join(" ")}`, e = t.segments.map(R).join(" ");
391
+ function et(t) {
392
+ const n = `M ${t.firstPoint.join(" ")}`, e = t.segments.map(x).join(" ");
492
393
  return `${n} ${e} Z`;
493
394
  }
494
- function it(t) {
495
- return `<path d="${t.allLoops.map(rt).join(" ")}" />`;
395
+ function rt(t) {
396
+ return `<path d="${t.allLoops.map(et).join(" ")}" />`;
496
397
  }
497
- function It(t) {
398
+ function Gt(t) {
498
399
  return `<g>
499
- ${t.figures.map(it).join(`
400
+ ${t.figures.map(rt).join(`
500
401
  `)}
501
402
  </g>`;
502
403
  }
503
- function Ot(t) {
504
- const n = `M ${t.firstPoint.join(" ")}`, e = t.segments.map(R).join(" ");
404
+ function Vt(t) {
405
+ const n = `M ${t.firstPoint.join(" ")}`, e = t.segments.map(x).join(" ");
505
406
  return `${n} ${e}`;
506
407
  }
507
- function Rt(t, n = 1) {
408
+ function xt(t, n = 1) {
508
409
  const e = t.xMin - n, r = t.yMin - n;
509
410
  return `${e} ${r} ${t.width + 2 * n} ${t.height + 2 * n}`;
510
411
  }
511
- function J(t, n, e = 1) {
412
+ function J(t, n, e = 1, r) {
413
+ const o = xt(n, e), s = r ? `width="${n.width + 2 * e}${r}" height="${n.height + 2 * e}${r}"` : "";
512
414
  return `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
513
- <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${Rt(n, e)}" fill="none" stroke="black" stroke-width="0.2%" vector-effect="non-scaling-stroke">
415
+ <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${o}" fill="none" stroke="black" stroke-width="0.2%" vector-effect="non-scaling-stroke" ${s}>
514
416
  ${t}
515
417
  </svg>`;
516
418
  }
517
- function X(t) {
419
+ function W(t) {
420
+ if (t instanceof p)
421
+ return Gt(t);
518
422
  if (t instanceof g)
519
- return It(t);
520
- if (t instanceof T)
521
- return it(t);
522
- if (t instanceof M)
523
- return `<path d="${rt(t)}" />`;
524
- if (t instanceof Lt)
525
- return `<path d="${Ot(t)}" />`;
526
- if (t instanceof P || t instanceof p)
527
- return `<path d="${`M ${t.firstPoint.join(" ")}`} ${R(
423
+ return rt(t);
424
+ if (t instanceof d)
425
+ return `<path d="${et(t)}" />`;
426
+ if (t instanceof K)
427
+ return `<path d="${Vt(t)}" />`;
428
+ if (t instanceof S || t instanceof w)
429
+ return `<path d="${`M ${t.firstPoint.join(" ")}`} ${x(
528
430
  t
529
431
  )}" />`;
530
432
  throw new Error("Unknown shape type");
531
433
  }
532
- const Z = (t) => "shape" in t ? t.shape : t, H = (t, n) => {
434
+ const X = (t) => "shape" in t ? t.shape : t, Y = (t, n) => {
533
435
  if (!("shape" in t))
534
436
  return n;
535
437
  const { color: e } = t;
536
438
  return e ? `<g stroke="${e}">${n}</g>` : n;
537
439
  };
538
- function Yt(t, n = 1) {
440
+ function Yt(t, {
441
+ margin: n = 1,
442
+ unit: e = null
443
+ } = {}) {
539
444
  if (Array.isArray(t)) {
540
- const r = t.map((o) => Z(o).mirror()), s = r.map((o, u) => H(t[u], X(o))).join(`
541
- `), i = r.slice(1).reduce((o, u) => o.merge(u.boundingBox), r[0].boundingBox);
542
- return J(s, i);
445
+ const o = t.map((a) => X(a).mirror()), s = o.map((a, u) => Y(t[u], W(a))).join(`
446
+ `), i = o.slice(1).reduce((a, u) => a.merge(u.boundingBox), o[0].boundingBox);
447
+ return J(s, i, n, e);
543
448
  }
544
- const e = Z(t).mirror();
449
+ const r = X(t).mirror();
545
450
  return J(
546
- H(t, X(e)),
547
- e.boundingBox,
548
- n
451
+ Y(t, W(r)),
452
+ r.boundingBox,
453
+ n,
454
+ e
549
455
  );
550
456
  }
551
457
  const st = (t) => {
552
458
  if (t.type === "LINE")
553
- return new p(t.firstPoint, t.lastPoint);
459
+ return new w(t.firstPoint, t.lastPoint);
554
460
  if (t.type === "ARC")
555
- return new P(
461
+ return new S(
556
462
  t.firstPoint,
557
463
  t.lastPoint,
558
464
  t.center,
559
465
  t.clockwise
560
466
  );
561
467
  throw new Error("Unknown segment type");
562
- }, O = (t) => {
468
+ }, D = (t) => {
563
469
  const n = t.segments.map(st);
564
- return new M(n);
470
+ return new d(n);
565
471
  }, ot = (t) => {
566
- const n = O(t.contour), e = t.holes.map(O);
567
- return new T(n, e);
568
- }, _t = (t) => {
472
+ const n = D(t.contour), e = t.holes.map(D);
473
+ return new g(n, e);
474
+ }, Tt = (t) => {
569
475
  const n = t.figures.map(ot);
570
- return new g(n);
476
+ return new p(n);
571
477
  };
572
- function qt(t) {
478
+ function Zt(t) {
573
479
  if (t.type === "DIAGRAM")
574
- return _t(t);
480
+ return Tt(t);
575
481
  if (t.type === "FIGURE")
576
482
  return ot(t);
577
483
  if (t.type === "LOOP")
578
- return O(t);
484
+ return D(t);
579
485
  if (t.type === "LINE" || t.type === "ARC")
580
486
  return st(t);
581
487
  throw new Error("Unknown shape type");
582
488
  }
583
- const K = Math.PI / 180, Vt = 180 / Math.PI;
584
- function zt(t, n) {
585
- const e = Math.cos(n * K) * t, r = Math.sin(n * K) * t;
489
+ const Z = Math.PI / 180, Rt = 180 / Math.PI;
490
+ function Bt(t, n) {
491
+ const e = Math.cos(n * Z) * t, r = Math.sin(n * Z) * t;
586
492
  return [e, r];
587
493
  }
588
- function Wt([t, n]) {
589
- const e = Math.sqrt(t * t + n * n), r = Math.atan2(n, t) * Vt;
494
+ function Ht([t, n]) {
495
+ const e = Math.sqrt(t * t + n * n), r = Math.atan2(n, t) * Rt;
590
496
  return [e, r];
591
497
  }
592
498
  export {
593
- K as DEG2RAD,
594
- Vt as RAD2DEG,
595
- Wt as cartesianToPolar,
596
- Mt as cut,
597
- Gt as draw,
598
- Zt as exportJSON,
499
+ Z as DEG2RAD,
500
+ Rt as RAD2DEG,
501
+ Ht as cartesianToPolar,
502
+ Jt as confineStrand,
503
+ _ as cut,
504
+ _t as draw,
505
+ qt as eraseStrand,
506
+ tn as exportJSON,
599
507
  Yt as exportSVG,
600
- Ft as fuse,
601
- U as fuseAll,
602
- qt as importJSON,
508
+ Et as fuse,
509
+ E as fuseAll,
510
+ Zt as importJSON,
603
511
  Nt as intersect,
604
- Ut as offset,
605
- zt as polarToCartesian,
606
- X as svgBody
512
+ Wt as offset,
513
+ Xt as outlineStroke,
514
+ Bt as polarToCartesian,
515
+ W as svgBody
607
516
  };
608
517
  //# sourceMappingURL=pantograph.js.map