msdfgen-ts 0.1.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.
@@ -0,0 +1,2114 @@
1
+ function io(e, n, t, s) {
2
+ return e * t + n * s;
3
+ }
4
+ function J(e, n, t, s) {
5
+ return e * s - n * t;
6
+ }
7
+ function eo(e, n, t) {
8
+ return (1 - t) * e + t * n;
9
+ }
10
+ function rt(e) {
11
+ return +(e > 0) - +(e < 0);
12
+ }
13
+ function Dt(e) {
14
+ return 2 * +(e > 0) - 1;
15
+ }
16
+ function $s(e, n, t, s) {
17
+ if (n === 0 || Math.abs(t) > 1e12 * Math.abs(n))
18
+ return t === 0 ? s === 0 ? -1 : 0 : (e[0] = -s / t, 1);
19
+ const i = t * t - 4 * n * s;
20
+ if (i > 0) {
21
+ const o = Math.sqrt(i);
22
+ return e[0] = (-t + o) / (2 * n), e[1] = (-t - o) / (2 * n), 2;
23
+ } else if (i === 0)
24
+ return e[0] = -t / (2 * n), 1;
25
+ return 0;
26
+ }
27
+ function co(e, n, t, s) {
28
+ const i = n * n, o = 1 / 9 * (i - 3 * t), p = 1 / 54 * (n * (2 * i - 9 * t) + 27 * s), c = p * p, r = o * o * o, h = n * (1 / 3);
29
+ if (c < r) {
30
+ let l = p / Math.sqrt(r);
31
+ l < -1 && (l = -1), l > 1 && (l = 1), l = Math.acos(l);
32
+ const a = -2 * Math.sqrt(o);
33
+ return e[0] = a * Math.cos(1 / 3 * l) - h, e[1] = a * Math.cos(1 / 3 * (l + 2 * Math.PI)) - h, e[2] = a * Math.cos(1 / 3 * (l - 2 * Math.PI)) - h, 3;
34
+ } else {
35
+ const l = (p < 0 ? 1 : -1) * Math.pow(Math.abs(p) + Math.sqrt(c - r), 0.3333333333333333), a = l === 0 ? 0 : o / l;
36
+ return e[0] = l + a - h, l === a || Math.abs(l - a) < 1e-12 * Math.abs(l + a) ? (e[1] = -0.5 * (l + a) - h, 2) : 1;
37
+ }
38
+ }
39
+ function ro(e, n, t, s, i) {
40
+ if (n !== 0) {
41
+ const o = t / n;
42
+ if (Math.abs(o) < 1e6)
43
+ return co(e, o, s / n, i / n);
44
+ }
45
+ return $s(e, t, s, i);
46
+ }
47
+ const ao = 24;
48
+ function Gt(e, n, t, s) {
49
+ return ((s + e) * s + n) * s + t;
50
+ }
51
+ function ho(e, n, t, s, i, o, p) {
52
+ const c = o < 0;
53
+ let r = s - o * (i - s) / (p - o);
54
+ r > s && r < i || (r = 0.5 * (s + i));
55
+ for (let h = 0; h < ao; h++) {
56
+ const l = Gt(e, n, t, r);
57
+ if (l === 0) return r;
58
+ l < 0 === c ? s = r : i = r;
59
+ const a = (3 * r + 2 * e) * r + n;
60
+ let f = r - l / a;
61
+ f > s && f < i || (f = 0.5 * (s + i));
62
+ const y = f - r;
63
+ if (r = f, (y < 0 ? -y : y) <= 1e-15 * (r < 0 ? -r : r)) break;
64
+ }
65
+ return r;
66
+ }
67
+ function Bt(e, n, t, s, i, o, p, c, r) {
68
+ return o === 0 ? c[r++] = s : o < 0 != p < 0 && (c[r++] = ho(e, n, t, s, i, o, p)), r;
69
+ }
70
+ function po(e, n, t, s, i, o) {
71
+ let p = 0;
72
+ if (e === 0) {
73
+ if (n === 0) return 0;
74
+ const f = -t / n;
75
+ return f >= s && f <= i && (o[p++] = f), p;
76
+ }
77
+ const c = n * n - 4 * e * t;
78
+ if (c < 0) return 0;
79
+ const r = Math.sqrt(c), h = -0.5 * (n + (n < 0 ? -r : r));
80
+ let l, a;
81
+ if (h === 0)
82
+ l = 0, a = 0;
83
+ else if (l = h / e, a = t / h, l > a) {
84
+ const f = l;
85
+ l = a, a = f;
86
+ }
87
+ return l >= s && l <= i && (o[p++] = l), a !== l && a >= s && a <= i && (o[p++] = a), p;
88
+ }
89
+ function lo(e, n, t, s, i, o, p) {
90
+ if (e === 0) return po(n, t, s, i, o, p);
91
+ const c = 1 / e;
92
+ n *= c, t *= c, s *= c;
93
+ const r = Gt(n, t, s, i), h = Gt(n, t, s, o), l = n * n - 3 * t;
94
+ let a = 0;
95
+ if (l <= 0)
96
+ a = Bt(n, t, s, i, o, r, h, p, 0);
97
+ else {
98
+ const f = Math.sqrt(l) / 3, y = -n / 3, _ = y - f, u = y + f;
99
+ let x = i, M = r;
100
+ if (_ > i && _ < o) {
101
+ const w = Gt(n, t, s, _);
102
+ a = Bt(n, t, s, x, _, M, w, p, a), x = _, M = w;
103
+ }
104
+ if (u > i && u < o) {
105
+ const w = Gt(n, t, s, u);
106
+ a = Bt(n, t, s, x, u, M, w, p, a), x = u, M = w;
107
+ }
108
+ a = Bt(n, t, s, x, o, M, h, p, a);
109
+ }
110
+ return h === 0 && (p[a++] = o), a;
111
+ }
112
+ const at = 0, it = 1, Ot = 2, Rs = 4, fo = 4, U = [0, 0, 0];
113
+ class et {
114
+ /**
115
+ * @param type Segment type (LINEAR=0, QUADRATIC=1, CUBIC=2).
116
+ * @param p0x Start x.
117
+ * @param p0y Start y.
118
+ * @param p1x LINEAR: end x. QUADRATIC: control x. CUBIC: first control x.
119
+ * @param p1y LINEAR: end y. QUADRATIC: control y. CUBIC: first control y.
120
+ * @param p2x QUADRATIC: end x. CUBIC: second control x. LINEAR: 0.
121
+ * @param p2y QUADRATIC: end y. CUBIC: second control y. LINEAR: 0.
122
+ * @param p3x CUBIC: end x. Others: 0.
123
+ * @param p3y CUBIC: end y. Others: 0.
124
+ */
125
+ constructor(n, t, s, i, o, p, c, r, h) {
126
+ this.type = n, this.p0x = t, this.p0y = s, this.p1x = i, this.p1y = o, this.p2x = p, this.p2y = c, this.p3x = r, this.p3y = h, this.color = 7;
127
+ }
128
+ /** Endpoint x (the on-curve destination of this segment). */
129
+ endX() {
130
+ switch (this.type) {
131
+ case at:
132
+ return this.p1x;
133
+ case it:
134
+ return this.p2x;
135
+ default:
136
+ return this.p3x;
137
+ }
138
+ }
139
+ /** Endpoint y. */
140
+ endY() {
141
+ switch (this.type) {
142
+ case at:
143
+ return this.p1y;
144
+ case it:
145
+ return this.p2y;
146
+ default:
147
+ return this.p3y;
148
+ }
149
+ }
150
+ /**
151
+ * Returns the point on the segment at parameter t ∈ [0,1].
152
+ * port of core/edge-segments.cpp: LinearSegment::point, QuadraticSegment::point, CubicSegment::point
153
+ *
154
+ * @param param Parameter t.
155
+ * @param out Two-element array [x, y] to write into (reuse to avoid allocation).
156
+ */
157
+ point(n, t) {
158
+ const s = n;
159
+ switch (this.type) {
160
+ case at: {
161
+ t[0] = this.p0x + s * (this.p1x - this.p0x), t[1] = this.p0y + s * (this.p1y - this.p0y);
162
+ return;
163
+ }
164
+ case it: {
165
+ const i = this.p0x + s * (this.p1x - this.p0x), o = this.p0y + s * (this.p1y - this.p0y), p = this.p1x + s * (this.p2x - this.p1x), c = this.p1y + s * (this.p2y - this.p1y);
166
+ t[0] = i + s * (p - i), t[1] = o + s * (c - o);
167
+ return;
168
+ }
169
+ default: {
170
+ const i = this.p0x + s * (this.p1x - this.p0x), o = this.p0y + s * (this.p1y - this.p0y), p = this.p1x + s * (this.p2x - this.p1x), c = this.p1y + s * (this.p2y - this.p1y), r = this.p2x + s * (this.p3x - this.p2x), h = this.p2y + s * (this.p3y - this.p2y), l = i + s * (p - i), a = o + s * (c - o), f = p + s * (r - p), y = c + s * (h - c);
171
+ t[0] = l + s * (f - l), t[1] = a + s * (y - a);
172
+ return;
173
+ }
174
+ }
175
+ }
176
+ /**
177
+ * Returns the (unnormalized) direction tangent at parameter t.
178
+ * port of core/edge-segments.cpp: LinearSegment::direction, QuadraticSegment::direction, CubicSegment::direction
179
+ *
180
+ * @param param Parameter t.
181
+ * @param out Two-element array [dx, dy] to write into.
182
+ */
183
+ direction(n, t) {
184
+ const s = n;
185
+ switch (this.type) {
186
+ case at: {
187
+ t[0] = this.p1x - this.p0x, t[1] = this.p1y - this.p0y;
188
+ return;
189
+ }
190
+ case it: {
191
+ const i = this.p1x - this.p0x + s * (this.p2x - this.p1x - (this.p1x - this.p0x)), o = this.p1y - this.p0y + s * (this.p2y - this.p1y - (this.p1y - this.p0y));
192
+ i !== 0 || o !== 0 ? (t[0] = i, t[1] = o) : (t[0] = this.p2x - this.p0x, t[1] = this.p2y - this.p0y);
193
+ return;
194
+ }
195
+ default: {
196
+ const i = this.p1x - this.p0x, o = this.p1y - this.p0y, p = this.p2x - this.p1x, c = this.p2y - this.p1y, r = this.p3x - this.p2x, h = this.p3y - this.p2y, l = i + s * (p - i) + s * (p + s * (r - p) - (i + s * (p - i))), a = o + s * (c - o) + s * (c + s * (h - c) - (o + s * (c - o)));
197
+ l !== 0 || a !== 0 ? (t[0] = l, t[1] = a) : s === 0 ? (t[0] = this.p2x - this.p0x, t[1] = this.p2y - this.p0y) : s === 1 ? (t[0] = this.p3x - this.p1x, t[1] = this.p3y - this.p1y) : (t[0] = l, t[1] = a);
198
+ return;
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * Splits the segment into thirds. Each of the 3 returned segments covers
204
+ * [0,1/3], [1/3,2/3], [2/3,1] of the original parameter range.
205
+ * port of core/edge-segments.cpp: LinearSegment::splitInThirds, QuadraticSegment::splitInThirds, CubicSegment::splitInThirds
206
+ *
207
+ * @returns Tuple [part0, part1, part2].
208
+ */
209
+ splitInThirds() {
210
+ const n = [0, 0], t = [0, 0];
211
+ switch (this.type) {
212
+ case at: {
213
+ this.point(1 / 3, n), this.point(2 / 3, t);
214
+ const s = new et(at, this.p0x, this.p0y, n[0], n[1], 0, 0, 0, 0), i = new et(at, n[0], n[1], t[0], t[1], 0, 0, 0, 0), o = new et(at, t[0], t[1], this.p1x, this.p1y, 0, 0, 0, 0);
215
+ return s.color = i.color = o.color = this.color, [s, i, o];
216
+ }
217
+ case it: {
218
+ this.point(1 / 3, n), this.point(2 / 3, t);
219
+ const s = this.p0x + 1 / 3 * (this.p1x - this.p0x), i = this.p0y + 1 / 3 * (this.p1y - this.p0y), o = this.p0x + 5 / 9 * (this.p1x - this.p0x), p = this.p0y + 5 / 9 * (this.p1y - this.p0y), c = this.p1x + 4 / 9 * (this.p2x - this.p1x), r = this.p1y + 4 / 9 * (this.p2y - this.p1y), h = o + 0.5 * (c - o), l = p + 0.5 * (r - p), a = this.p1x + 2 / 3 * (this.p2x - this.p1x), f = this.p1y + 2 / 3 * (this.p2y - this.p1y), y = new et(
220
+ it,
221
+ this.p0x,
222
+ this.p0y,
223
+ s,
224
+ i,
225
+ n[0],
226
+ n[1],
227
+ 0,
228
+ 0
229
+ ), _ = new et(
230
+ it,
231
+ n[0],
232
+ n[1],
233
+ h,
234
+ l,
235
+ t[0],
236
+ t[1],
237
+ 0,
238
+ 0
239
+ ), u = new et(
240
+ it,
241
+ t[0],
242
+ t[1],
243
+ a,
244
+ f,
245
+ this.p2x,
246
+ this.p2y,
247
+ 0,
248
+ 0
249
+ );
250
+ return y.color = _.color = u.color = this.color, [y, _, u];
251
+ }
252
+ default: {
253
+ this.point(1 / 3, n), this.point(2 / 3, t);
254
+ const s = this.p0x === this.p1x && this.p0y === this.p1y ? this.p0x : this.p0x + 1 / 3 * (this.p1x - this.p0x), i = this.p0x === this.p1x && this.p0y === this.p1y ? this.p0y : this.p0y + 1 / 3 * (this.p1y - this.p0y), o = this.p0x + 1 / 3 * (this.p1x - this.p0x), p = this.p0y + 1 / 3 * (this.p1y - this.p0y), c = this.p1x + 1 / 3 * (this.p2x - this.p1x), r = this.p1y + 1 / 3 * (this.p2y - this.p1y), h = this.p2x + 1 / 3 * (this.p3x - this.p2x), l = this.p2y + 1 / 3 * (this.p3y - this.p2y), a = o + 1 / 3 * (c - o), f = p + 1 / 3 * (r - p), y = c + 1 / 3 * (h - c), _ = r + 1 / 3 * (l - r), u = a + 2 / 3 * (y - a), x = f + 2 / 3 * (_ - f), M = this.p0x + 2 / 3 * (this.p1x - this.p0x), w = this.p0y + 2 / 3 * (this.p1y - this.p0y), v = this.p1x + 2 / 3 * (this.p2x - this.p1x), C = this.p1y + 2 / 3 * (this.p2y - this.p1y), A = this.p2x + 2 / 3 * (this.p3x - this.p2x), D = this.p2y + 2 / 3 * (this.p3y - this.p2y), I = M + 2 / 3 * (v - M), F = w + 2 / 3 * (C - w), T = v + 2 / 3 * (A - v), b = C + 2 / 3 * (D - C), N = I + 1 / 3 * (T - I), R = F + 1 / 3 * (b - F), q = this.p2x === this.p3x && this.p2y === this.p3y ? this.p3x : this.p2x + 2 / 3 * (this.p3x - this.p2x), P = this.p2x === this.p3x && this.p2y === this.p3y ? this.p3y : this.p2y + 2 / 3 * (this.p3y - this.p2y), X = new et(
255
+ Ot,
256
+ this.p0x,
257
+ this.p0y,
258
+ s,
259
+ i,
260
+ a,
261
+ f,
262
+ n[0],
263
+ n[1]
264
+ ), Q = new et(
265
+ Ot,
266
+ n[0],
267
+ n[1],
268
+ u,
269
+ x,
270
+ N,
271
+ R,
272
+ t[0],
273
+ t[1]
274
+ ), V = new et(
275
+ Ot,
276
+ t[0],
277
+ t[1],
278
+ T,
279
+ b,
280
+ q,
281
+ P,
282
+ this.p3x,
283
+ this.p3y
284
+ );
285
+ return X.color = Q.color = V.color = this.color, [X, Q, V];
286
+ }
287
+ }
288
+ }
289
+ /**
290
+ * Computes the signed distance from `origin` to this segment, writing the
291
+ * result (distance, alignment dot, nearest parameter) into `out`.
292
+ * port of core/edge-segments.cpp:
293
+ * LinearSegment / QuadraticSegment / CubicSegment ::signedDistance
294
+ *
295
+ * The sign follows the edge winding via `nonZeroSign(crossProduct(...))`.
296
+ * Hot path: uses only local number variables and the module-scope root buffer.
297
+ *
298
+ * @param ox Query point x (shape space).
299
+ * @param oy Query point y (shape space).
300
+ * @param out Reusable result object (mutated).
301
+ */
302
+ signedDistance(n, t, s) {
303
+ switch (this.type) {
304
+ case at: {
305
+ const i = this.p0x, o = this.p0y, p = this.p1x, c = this.p1y, r = n - i, h = t - o, l = p - i, a = c - o, f = l * l + a * a, y = (r * l + h * a) / f, _ = y > 0.5, u = (_ ? p : i) - n, x = (_ ? c : o) - t, M = Math.sqrt(u * u + x * x);
306
+ if (y > 0 && y < 1) {
307
+ const C = Math.sqrt(l * l + a * a), A = l / C, I = a / C * r - A * h;
308
+ if (Math.abs(I) < M) {
309
+ s.distance = I, s.dot = 0, s.param = y;
310
+ return;
311
+ }
312
+ }
313
+ const w = r * a - h * l;
314
+ s.distance = Dt(w) * M;
315
+ const v = Math.sqrt(l * l + a * a);
316
+ s.dot = M === 0 || v === 0 ? 0 : Math.abs((l * u + a * x) / (v * M)), s.param = y;
317
+ return;
318
+ }
319
+ case it: {
320
+ const i = this.p0x, o = this.p0y, p = this.p1x, c = this.p1y, r = this.p2x, h = this.p2y, l = i - n, a = o - t, f = p - i, y = c - o, _ = r - p - f, u = h - c - y, x = _ * _ + u * u, M = 3 * (f * _ + y * u), w = 2 * (f * f + y * y) + (l * _ + a * u), v = l * f + a * y, C = lo(x, M, w, v, 0, 1, U);
321
+ let A = f, D = y;
322
+ const I = l * l + a * a;
323
+ let F = Dt(A * a - D * l), T = I, b = -(l * A + a * D) / (A * A + D * D);
324
+ {
325
+ const R = r - n, q = h - t, P = R * R + q * q;
326
+ P < T && (A = r - p, D = h - c, F = Dt(A * q - D * R), T = P, b = ((n - p) * A + (t - c) * D) / (A * A + D * D));
327
+ }
328
+ for (let R = 0; R < C; ++R) {
329
+ const q = U[R];
330
+ if (q > 0 && q < 1) {
331
+ const P = l + 2 * q * f + q * q * _, X = a + 2 * q * y + q * q * u, Q = P * P + X * X;
332
+ if (Q <= T) {
333
+ const V = f + q * _, nt = y + q * u;
334
+ F = Dt(V * X - nt * P), T = Q, b = q;
335
+ }
336
+ }
337
+ }
338
+ const N = F * Math.sqrt(T);
339
+ if (b >= 0 && b <= 1) {
340
+ s.distance = N, s.dot = 0, s.param = b;
341
+ return;
342
+ }
343
+ if (s.distance = N, s.param = b, b < 0.5) {
344
+ const R = Math.sqrt(f * f + y * y), q = Math.sqrt(I);
345
+ s.dot = q === 0 || R === 0 ? 0 : Math.abs((f * l + y * a) / (R * q));
346
+ } else {
347
+ const R = r - p, q = h - c, P = r - n, X = h - t, Q = Math.sqrt(R * R + q * q), V = Math.sqrt(P * P + X * X);
348
+ s.dot = Q === 0 || V === 0 ? 0 : Math.abs((R * P + q * X) / (Q * V));
349
+ }
350
+ return;
351
+ }
352
+ default: {
353
+ const i = this.p0x, o = this.p0y, p = this.p1x, c = this.p1y, r = this.p2x, h = this.p2y, l = this.p3x, a = this.p3y, f = i - n, y = o - t, _ = p - i, u = c - o, x = r - p - _, M = h - c - u, w = l - r - (r - p) - x, v = a - h - (h - c) - M;
354
+ let C = _, A = u;
355
+ C === 0 && A === 0 && (C = r - i, A = h - o);
356
+ const D = Math.sqrt(f * f + y * y);
357
+ let I = Dt(C * y - A * f) * D, F = -(f * C + y * A) / (C * C + A * A);
358
+ {
359
+ const T = l - n, b = a - t, N = Math.sqrt(T * T + b * b);
360
+ if (N < Math.abs(I)) {
361
+ let R = l - r, q = a - h;
362
+ R === 0 && q === 0 && (R = l - p, q = a - c), C = R, A = q, I = Dt(C * b - A * T) * N, F = ((C - T) * C + (A - b) * A) / (C * C + A * A);
363
+ }
364
+ }
365
+ for (let T = 0; T <= Rs; ++T) {
366
+ let b = 1 / Rs * T, N = f + 3 * b * _ + 3 * b * b * x + b * b * b * w, R = y + 3 * b * u + 3 * b * b * M + b * b * b * v, q = 3 * _ + 6 * b * x + 3 * b * b * w, P = 3 * u + 6 * b * M + 3 * b * b * v, X = 6 * x + 6 * b * w, Q = 6 * M + 6 * b * v, V = b - (N * q + R * P) / (q * q + P * P + (N * X + R * Q));
367
+ if (V > 0 && V < 1) {
368
+ let nt = fo;
369
+ do {
370
+ if (b = V, N = f + 3 * b * _ + 3 * b * b * x + b * b * b * w, R = y + 3 * b * u + 3 * b * b * M + b * b * b * v, q = 3 * _ + 6 * b * x + 3 * b * b * w, P = 3 * u + 6 * b * M + 3 * b * b * v, !--nt) break;
371
+ X = 6 * x + 6 * b * w, Q = 6 * M + 6 * b * v, V = b - (N * q + R * P) / (q * q + P * P + (N * X + R * Q));
372
+ } while (V > 0 && V < 1);
373
+ const H = Math.sqrt(N * N + R * R);
374
+ H < Math.abs(I) && (I = Dt(q * R - P * N) * H, F = b);
375
+ }
376
+ }
377
+ if (F >= 0 && F <= 1) {
378
+ s.distance = I, s.dot = 0, s.param = F;
379
+ return;
380
+ }
381
+ if (s.distance = I, s.param = F, F < 0.5) {
382
+ let T = _, b = u;
383
+ T === 0 && b === 0 && (T = r - i, b = h - o);
384
+ const N = Math.sqrt(T * T + b * b);
385
+ s.dot = D === 0 || N === 0 ? 0 : Math.abs((T * f + b * y) / (N * D));
386
+ } else {
387
+ let T = l - r, b = a - h;
388
+ T === 0 && b === 0 && (T = l - p, b = a - c);
389
+ const N = l - n, R = a - t, q = Math.sqrt(T * T + b * b), P = Math.sqrt(N * N + R * R);
390
+ s.dot = q === 0 || P === 0 ? 0 : Math.abs((T * N + b * R) / (q * P));
391
+ }
392
+ return;
393
+ }
394
+ }
395
+ }
396
+ /**
397
+ * Computes the x-coordinates where a horizontal scanline at height `y`
398
+ * crosses this segment, together with the vertical crossing direction.
399
+ * port of core/edge-segments.cpp:
400
+ * LinearSegment / QuadraticSegment / CubicSegment ::scanlineIntersections
401
+ *
402
+ * @param y Scanline height (shape space).
403
+ * @param xOut Output array (length >= 3) — crossing x-coordinates.
404
+ * @param dyOut Output array (length >= 3) — crossing directions (±1).
405
+ * @returns Number of crossings written (0..3).
406
+ */
407
+ scanlineIntersections(n, t, s) {
408
+ switch (this.type) {
409
+ case at: {
410
+ const i = this.p0y, o = this.p1y;
411
+ if (n >= i && n < o || n >= o && n < i) {
412
+ const p = (n - i) / (o - i);
413
+ return t[0] = eo(this.p0x, this.p1x, p), s[0] = rt(o - i), 1;
414
+ }
415
+ return 0;
416
+ }
417
+ case it: {
418
+ const i = this.p0x, o = this.p0y, p = this.p1x, c = this.p1y, r = this.p2x, h = this.p2y;
419
+ let l = 0, a = n > o ? 1 : -1;
420
+ t[l] = i, o === n && (o < c || o === c && o < h ? s[l++] = 1 : a = 1);
421
+ {
422
+ const f = p - i, y = c - o, _ = r - p - f, u = h - c - y, x = $s(U, u, 2 * y, o - n);
423
+ if (x >= 2 && U[0] > U[1]) {
424
+ const M = U[0];
425
+ U[0] = U[1], U[1] = M;
426
+ }
427
+ for (let M = 0; M < x && l < 2; ++M) {
428
+ const w = U[M];
429
+ w >= 0 && w <= 1 && (t[l] = i + 2 * w * f + w * w * _, a * (y + w * u) >= 0 && (s[l++] = a, a = -a));
430
+ }
431
+ }
432
+ return h === n && (a > 0 && l > 0 && (--l, a = -1), (h < c || h === c && h < o) && l < 2 && (t[l] = r, a < 0 && (s[l++] = -1, a = 1))), a !== (n >= h ? 1 : -1) && (l > 0 ? --l : (Math.abs(h - n) < Math.abs(o - n) && (t[l] = r), s[l++] = a)), l;
433
+ }
434
+ default: {
435
+ const i = this.p0x, o = this.p0y, p = this.p1x, c = this.p1y, r = this.p2x, h = this.p2y, l = this.p3x, a = this.p3y;
436
+ let f = 0, y = n > o ? 1 : -1;
437
+ t[f] = i, o === n && (o < c || o === c && (o < h || o === h && o < a) ? s[f++] = 1 : y = 1);
438
+ {
439
+ const _ = l - 3 * r + 3 * p - i, u = a - 3 * h + 3 * c - o, x = 3 * r - 6 * p + 3 * i, M = 3 * h - 6 * c + 3 * o, w = 3 * p - 3 * i, v = 3 * c - 3 * o, C = ro(U, u, M, v, o - n);
440
+ if (C >= 2) {
441
+ if (U[0] > U[1]) {
442
+ const A = U[0];
443
+ U[0] = U[1], U[1] = A;
444
+ }
445
+ if (C >= 3 && U[1] > U[2]) {
446
+ const A = U[1];
447
+ if (U[1] = U[2], U[2] = A, U[0] > U[1]) {
448
+ const D = U[0];
449
+ U[0] = U[1], U[1] = D;
450
+ }
451
+ }
452
+ }
453
+ for (let A = 0; A < C && f < 3; ++A) {
454
+ const D = U[A];
455
+ D >= 0 && D <= 1 && (t[f] = ((_ * D + x) * D + w) * D + i, y * ((3 * u * D + 2 * M) * D + v) >= 0 && (s[f++] = y, y = -y));
456
+ }
457
+ }
458
+ return a === n && (y > 0 && f > 0 && (--f, y = -1), (a < h || a === h && (a < c || a === c && a < o)) && f < 3 && (t[f] = l, y < 0 && (s[f++] = -1, y = 1))), y !== (n >= a ? 1 : -1) && (f > 0 ? --f : (Math.abs(a - n) < Math.abs(o - n) && (t[f] = l), s[f++] = y)), f;
459
+ }
460
+ }
461
+ }
462
+ /**
463
+ * Converts a QUADRATIC segment to a degree-elevated CUBIC equivalent.
464
+ * port of core/edge-segments.cpp: QuadraticSegment::convertToCubic
465
+ *
466
+ * Only valid when `this.type === QUADRATIC`.
467
+ * @returns A new CUBIC EdgeSegment with identical geometry.
468
+ */
469
+ convertToCubic() {
470
+ const n = this.p0x + 0.6666666666666666 * (this.p1x - this.p0x), t = this.p0y + 2 / 3 * (this.p1y - this.p0y), s = this.p1x + 1 / 3 * (this.p2x - this.p1x), i = this.p1y + 1 / 3 * (this.p2y - this.p1y), o = new et(
471
+ Ot,
472
+ this.p0x,
473
+ this.p0y,
474
+ n,
475
+ t,
476
+ s,
477
+ i,
478
+ this.p2x,
479
+ this.p2y
480
+ );
481
+ return o.color = this.color, o;
482
+ }
483
+ }
484
+ class gt {
485
+ /**
486
+ * @param buffer Source buffer (the full font file).
487
+ * @param byteOffset Starting position (default 0).
488
+ */
489
+ constructor(n, t = 0) {
490
+ this._view = new DataView(n), this._pos = t;
491
+ }
492
+ /** Current byte position. */
493
+ get pos() {
494
+ return this._pos;
495
+ }
496
+ /** Move to an absolute byte position. */
497
+ seek(n) {
498
+ this._pos = n;
499
+ }
500
+ /** Advance position by `n` bytes without reading. */
501
+ skip(n) {
502
+ this._pos += n;
503
+ }
504
+ /** Read uint8 (1 byte). */
505
+ u8() {
506
+ return this._view.getUint8(this._pos++);
507
+ }
508
+ /** Read uint16 big-endian (2 bytes). */
509
+ u16() {
510
+ const n = this._view.getUint16(this._pos, !1);
511
+ return this._pos += 2, n;
512
+ }
513
+ /** Read int16 big-endian (2 bytes). */
514
+ i16() {
515
+ const n = this._view.getInt16(this._pos, !1);
516
+ return this._pos += 2, n;
517
+ }
518
+ /** Read uint32 big-endian (4 bytes). */
519
+ u32() {
520
+ const n = this._view.getUint32(this._pos, !1);
521
+ return this._pos += 4, n;
522
+ }
523
+ /** Read int32 big-endian (4 bytes). */
524
+ i32() {
525
+ const n = this._view.getInt32(this._pos, !1);
526
+ return this._pos += 4, n;
527
+ }
528
+ /**
529
+ * Read F2Dot14 (2 bytes): 2-bit signed integer + 14-bit fraction.
530
+ * Used for 2×2 scale components in composite glyphs.
531
+ * @returns Value in [-2, 2).
532
+ */
533
+ f2dot14() {
534
+ return this.i16() / 16384;
535
+ }
536
+ /**
537
+ * Read a 16-bit offset (uint16). Alias for u16(), documents intent.
538
+ * @returns Byte offset relative to some base (caller's responsibility).
539
+ */
540
+ offset16() {
541
+ return this.u16();
542
+ }
543
+ /**
544
+ * Read a 32-bit offset (uint32). Alias for u32(), documents intent.
545
+ * @returns Byte offset relative to some base (caller's responsibility).
546
+ */
547
+ offset32() {
548
+ return this.u32();
549
+ }
550
+ // ── peek helpers (non-advancing reads at absolute positions) ──────────────
551
+ /** Read uint16 big-endian at an absolute byte offset without moving pos. */
552
+ peekU16(n) {
553
+ return this._view.getUint16(n, !1);
554
+ }
555
+ /** Read uint32 big-endian at an absolute byte offset without moving pos. */
556
+ peekU32(n) {
557
+ return this._view.getUint32(n, !1);
558
+ }
559
+ /** Read int16 big-endian at an absolute byte offset without moving pos. */
560
+ peekI16(n) {
561
+ return this._view.getInt16(n, !1);
562
+ }
563
+ }
564
+ function yo(e, n) {
565
+ const t = new gt(e, n);
566
+ t.skip(2);
567
+ const s = t.u16();
568
+ let i = null;
569
+ for (let p = 0; p < s; p++) {
570
+ const c = t.u16(), r = t.u16(), h = t.u32(), l = n + h, a = t.peekU16(l);
571
+ let f = -1;
572
+ c === 3 && r === 10 && a === 12 ? f = 5 : c === 0 && r === 6 && a === 12 ? f = 4 : c === 0 && r === 4 && a === 12 ? f = 3 : c === 3 && r === 1 && a === 4 ? f = 2 : c === 0 && r <= 3 && a === 4 && (f = 1), f > ((i == null ? void 0 : i.priority) ?? -1) && (i = { priority: f, subtableAbsOffset: l });
573
+ }
574
+ if (!i) return () => 0;
575
+ const o = t.peekU16(i.subtableAbsOffset);
576
+ return o === 12 ? uo(e, i.subtableAbsOffset) : o === 4 ? _o(e, i.subtableAbsOffset) : () => 0;
577
+ }
578
+ function uo(e, n) {
579
+ const t = new gt(e, n);
580
+ t.skip(2), t.skip(2), t.skip(4), t.skip(4);
581
+ const s = t.u32(), i = new Uint32Array(s), o = new Uint32Array(s), p = new Uint32Array(s);
582
+ for (let c = 0; c < s; c++)
583
+ i[c] = t.u32(), o[c] = t.u32(), p[c] = t.u32();
584
+ return (c) => {
585
+ let r = 0, h = s - 1;
586
+ for (; r <= h; ) {
587
+ const l = r + h >>> 1, a = i[l] ?? 0, f = o[l] ?? 0;
588
+ if (c < a) h = l - 1;
589
+ else if (c > f) r = l + 1;
590
+ else return (p[l] ?? 0) + (c - a);
591
+ }
592
+ return 0;
593
+ };
594
+ }
595
+ function _o(e, n) {
596
+ const t = new gt(e, n);
597
+ t.skip(2), t.skip(2), t.skip(2);
598
+ const i = t.u16() >>> 1;
599
+ t.skip(6);
600
+ const o = new Uint16Array(i);
601
+ for (let a = 0; a < i; a++) o[a] = t.u16();
602
+ t.skip(2);
603
+ const p = new Uint16Array(i);
604
+ for (let a = 0; a < i; a++) p[a] = t.u16();
605
+ const c = new Int16Array(i);
606
+ for (let a = 0; a < i; a++) c[a] = t.i16();
607
+ const r = new Int32Array(i), h = new Uint16Array(i);
608
+ for (let a = 0; a < i; a++)
609
+ r[a] = t.pos, h[a] = t.u16();
610
+ const l = new DataView(e);
611
+ return (a) => {
612
+ if (a > 65535) return 0;
613
+ let f = 0, y = i - 1;
614
+ for (; f <= y; ) {
615
+ const _ = f + y >>> 1, u = o[_] ?? 0;
616
+ if (a > u)
617
+ f = _ + 1;
618
+ else if (a < (p[_] ?? 0))
619
+ y = _ - 1;
620
+ else {
621
+ if ((o[_] ?? 0) === 65535) return 0;
622
+ const M = h[_] ?? 0, w = c[_] ?? 0, v = p[_] ?? 0;
623
+ if (M === 0)
624
+ return a + w & 65535;
625
+ const C = (r[_] ?? 0) + M + (a - v) * 2, A = l.getUint16(C, !1);
626
+ return A === 0 ? 0 : A + w & 65535;
627
+ }
628
+ }
629
+ return 0;
630
+ };
631
+ }
632
+ function xo(e, n) {
633
+ const t = new gt(e, n);
634
+ t.skip(4), t.skip(4), t.skip(4), t.skip(4), t.skip(2);
635
+ const s = t.u16();
636
+ t.skip(16), t.skip(8), t.skip(2), t.skip(2), t.skip(2);
637
+ const i = t.i16();
638
+ return { unitsPerEm: s, indexToLocFormat: i };
639
+ }
640
+ function Mo(e, n) {
641
+ const t = new gt(e, n);
642
+ t.skip(4);
643
+ const s = t.i16(), i = t.i16(), o = t.i16();
644
+ t.skip(2), t.skip(2), t.skip(2), t.skip(2), t.skip(2), t.skip(2), t.skip(2), t.skip(8), t.skip(2);
645
+ const p = t.u16();
646
+ return { ascender: s, descender: i, lineGap: o, numberOfHMetrics: p };
647
+ }
648
+ function go(e, n, t, s) {
649
+ const i = new gt(e, n), o = new Array(t);
650
+ let p = 0;
651
+ for (let c = 0; c < s; c++) {
652
+ const r = i.u16(), h = i.i16();
653
+ o[c] = { advanceWidth: r, lsb: h }, p = r;
654
+ }
655
+ for (let c = s; c < t; c++) {
656
+ const r = i.i16();
657
+ o[c] = { advanceWidth: p, lsb: r };
658
+ }
659
+ return o;
660
+ }
661
+ function bo(e, n, t, s) {
662
+ const i = new gt(e, n), o = t + 1, p = new Array(o);
663
+ if (s === 0)
664
+ for (let c = 0; c < o; c++) p[c] = i.u16() * 2;
665
+ else
666
+ for (let c = 0; c < o; c++) p[c] = i.u32();
667
+ return p;
668
+ }
669
+ function Ao(e, n) {
670
+ const t = new gt(e, n);
671
+ return t.skip(4), { numGlyphs: t.u16() };
672
+ }
673
+ const wo = 1, Do = 2, Eo = 4, qo = 8, Ts = 16, ms = 32, vo = 1, ks = 2, Co = 8, Io = 32, Ro = 64, To = 128;
674
+ function mo(e, n, t, s, i) {
675
+ const o = Ks(e, n, t, s, i), p = [];
676
+ for (const c of o) {
677
+ const r = So(c);
678
+ r.length > 0 && p.push(r);
679
+ }
680
+ return { contours: p, inverseYAxis: !1 };
681
+ }
682
+ function Ks(e, n, t, s, i) {
683
+ if (s === 0) return [];
684
+ const o = n + t, p = new gt(e, o), c = p.i16();
685
+ return p.skip(8), c >= 0 ? ko(p, c) : Lo(e, p, n, i);
686
+ }
687
+ function ko(e, n) {
688
+ if (n === 0) return [];
689
+ const t = new Array(n);
690
+ for (let a = 0; a < n; a++) t[a] = e.u16();
691
+ const s = (t[n - 1] ?? 0) + 1, i = e.u16();
692
+ e.skip(i);
693
+ const o = new Uint8Array(s);
694
+ for (let a = 0; a < s; ) {
695
+ const f = e.u8();
696
+ if (o[a++] = f, f & qo) {
697
+ let y = e.u8();
698
+ for (; y-- > 0; ) o[a++] = f;
699
+ }
700
+ }
701
+ const p = new Float64Array(s);
702
+ let c = 0;
703
+ for (let a = 0; a < s; a++) {
704
+ const f = o[a] ?? 0;
705
+ if (f & Do) {
706
+ const y = e.u8();
707
+ c += f & Ts ? y : -y;
708
+ } else f & Ts || (c += e.i16());
709
+ p[a] = c;
710
+ }
711
+ const r = new Float64Array(s);
712
+ c = 0;
713
+ for (let a = 0; a < s; a++) {
714
+ const f = o[a] ?? 0;
715
+ if (f & Eo) {
716
+ const y = e.u8();
717
+ c += f & ms ? y : -y;
718
+ } else f & ms || (c += e.i16());
719
+ r[a] = c;
720
+ }
721
+ const h = [];
722
+ let l = 0;
723
+ for (let a = 0; a < n; a++) {
724
+ const f = t[a] ?? 0, y = Fo(o, p, r, l, f);
725
+ y.length > 0 && h.push(y), l = f + 1;
726
+ }
727
+ return h;
728
+ }
729
+ function Fo(e, n, t, s, i) {
730
+ const o = i - s + 1;
731
+ if (o <= 0) return [];
732
+ const p = [];
733
+ for (let c = 0; c < o; c++) {
734
+ const r = e[s + c] ?? 0, h = n[s + c] ?? 0, l = t[s + c] ?? 0;
735
+ p.push({ x: h, y: l, onCurve: (r & wo) !== 0 });
736
+ }
737
+ return p;
738
+ }
739
+ function So(e) {
740
+ const n = e.length;
741
+ if (n <= 0) return [];
742
+ const t = [];
743
+ for (let c = 0; c < n; c++) {
744
+ const r = e[c], h = e[(c + 1) % n];
745
+ t.push(r), !r.onCurve && !h.onCurve && t.push({
746
+ x: Math.trunc((r.x + h.x) / 2),
747
+ y: Math.trunc((r.y + h.y) / 2),
748
+ onCurve: !0
749
+ });
750
+ }
751
+ const s = t.length;
752
+ let i = 0;
753
+ if (!t[0].onCurve) {
754
+ for (let c = 1; c < s; c++)
755
+ if (t[c].onCurve) {
756
+ i = c;
757
+ break;
758
+ }
759
+ }
760
+ const o = [];
761
+ let p = 0;
762
+ for (; p < s; ) {
763
+ const c = t[(i + p) % s], r = t[(i + p + 1) % s];
764
+ if (r.onCurve)
765
+ o.push(new et(at, c.x, c.y, r.x, r.y, 0, 0, 0, 0)), p += 1;
766
+ else {
767
+ const h = t[(i + p + 2) % s];
768
+ o.push(new et(it, c.x, c.y, r.x, r.y, h.x, h.y, 0, 0)), p += 2;
769
+ }
770
+ }
771
+ return o;
772
+ }
773
+ function Lo(e, n, t, s) {
774
+ const i = [];
775
+ let o = 0;
776
+ do {
777
+ o = n.u16();
778
+ const p = n.u16();
779
+ let c, r;
780
+ o & vo ? (c = n.i16(), r = n.i16()) : (c = n.u8(), r = n.u8(), o & ks && (c > 127 && (c -= 256), r > 127 && (r -= 256)));
781
+ let h = 0, l = 0;
782
+ o & ks && (h = c, l = r);
783
+ let a = 1, f = 0, y = 0, _ = 1;
784
+ o & Co ? a = _ = n.f2dot14() : o & Ro ? (a = n.f2dot14(), _ = n.f2dot14()) : o & To && (a = n.f2dot14(), f = n.f2dot14(), y = n.f2dot14(), _ = n.f2dot14());
785
+ const u = s(p), x = Go(u, a, f, y, _, h, l);
786
+ for (const M of x) i.push(M);
787
+ } while (o & Io);
788
+ return i;
789
+ }
790
+ function Fs(e) {
791
+ return e >= 0 ? Math.floor(e + 0.5) : Math.ceil(e - 0.5);
792
+ }
793
+ function Go(e, n, t, s, i, o, p) {
794
+ if (n === 1 && t === 0 && s === 0 && i === 1 && o === 0 && p === 0) return e;
795
+ const r = n === 1 && t === 0 && s === 0 && i === 1;
796
+ return e.map(
797
+ (h) => h.map((l) => r ? { x: l.x + o, y: l.y + p, onCurve: l.onCurve } : {
798
+ x: Fs(n * l.x + t * l.y + o),
799
+ y: Fs(s * l.x + i * l.y + p),
800
+ onCurve: l.onCurve
801
+ })
802
+ );
803
+ }
804
+ function Oo(e, n) {
805
+ const t = new gt(e, n);
806
+ if (t.u16() !== 0)
807
+ return Ss;
808
+ const i = t.u16(), o = /* @__PURE__ */ new Map();
809
+ for (let p = 0; p < i; p++) {
810
+ const c = t.pos;
811
+ t.skip(2);
812
+ const r = t.u16(), h = t.u16(), l = h >> 8 & 255, a = !(h & 2), f = !!(h & 4);
813
+ if (l !== 0 || !a || f) {
814
+ t.seek(c + r);
815
+ continue;
816
+ }
817
+ const y = t.u16();
818
+ t.skip(6);
819
+ for (let _ = 0; _ < y; _++) {
820
+ const u = t.u16(), x = t.u16(), M = t.i16(), w = (u & 65535) << 16 | x & 65535;
821
+ o.set(w, M);
822
+ }
823
+ t.seek(c + r);
824
+ }
825
+ return o.size === 0 ? Ss : (p, c) => {
826
+ const r = (p & 65535) << 16 | c & 65535;
827
+ return o.get(r) ?? 0;
828
+ };
829
+ }
830
+ const Ss = () => 0;
831
+ function Po(e) {
832
+ const n = new gt(e);
833
+ if (n.u32() === 1953784678) {
834
+ if (n.skip(4), n.u32() === 0) throw new Error("TTC contains no fonts");
835
+ const i = n.u32();
836
+ n.seek(i);
837
+ } else
838
+ n.seek(0);
839
+ return No(e, n);
840
+ }
841
+ function No(e, n) {
842
+ n.skip(4);
843
+ const t = n.u16();
844
+ n.skip(6);
845
+ const s = /* @__PURE__ */ new Map();
846
+ for (let i = 0; i < t; i++) {
847
+ const o = String.fromCharCode(n.u8(), n.u8(), n.u8(), n.u8());
848
+ n.skip(4);
849
+ const p = n.u32(), c = n.u32();
850
+ s.set(o, { offset: p, length: c });
851
+ }
852
+ return s;
853
+ }
854
+ class Di {
855
+ /**
856
+ * Parses a TrueType font from an ArrayBuffer.
857
+ *
858
+ * Supports single TTF files and TTC collections (first font only).
859
+ * Variable fonts (gvar/fvar) are accepted; variation deltas are ignored —
860
+ * the default-instance glyf outlines are used as-is.
861
+ *
862
+ * @param buffer Raw font file bytes.
863
+ */
864
+ constructor(n) {
865
+ this._buffer = n, this._tables = Po(n);
866
+ const t = this._tables.get("head"), s = this._tables.get("maxp"), i = this._tables.get("hhea"), o = this._tables.get("hmtx"), p = this._tables.get("loca"), c = this._tables.get("cmap");
867
+ if (!t) throw new Error("Font missing required `head` table");
868
+ if (!s) throw new Error("Font missing required `maxp` table");
869
+ if (!i) throw new Error("Font missing required `hhea` table");
870
+ if (!o) throw new Error("Font missing required `hmtx` table");
871
+ if (!p) throw new Error("Font missing required `loca` table");
872
+ if (!c) throw new Error("Font missing required `cmap` table");
873
+ this._head = xo(n, t.offset);
874
+ const r = Ao(n, s.offset);
875
+ this._hhea = Mo(n, i.offset), this._hmtx = go(n, o.offset, r.numGlyphs, this._hhea.numberOfHMetrics), this._loca = bo(n, p.offset, r.numGlyphs, this._head.indexToLocFormat), this._cmap = yo(n, c.offset);
876
+ const h = this._tables.get("kern");
877
+ this._kern = h ? Oo(n, h.offset) : () => 0, this.metrics = {
878
+ unitsPerEm: this._head.unitsPerEm,
879
+ ascender: this._hhea.ascender,
880
+ descender: this._hhea.descender,
881
+ lineGap: this._hhea.lineGap
882
+ };
883
+ }
884
+ /**
885
+ * Maps a Unicode codepoint to its glyph ID.
886
+ * Returns 0 (.notdef) if the codepoint is not in the font.
887
+ *
888
+ * @param codepoint Unicode scalar value.
889
+ */
890
+ glyphId(n) {
891
+ return this._cmap(n);
892
+ }
893
+ /**
894
+ * Total glyph count (`maxp.numGlyphs`), including `.notdef` at index 0.
895
+ * Valid glyph IDs for {@link Font.shape}/{@link Font.advance} are
896
+ * `0..numGlyphs-1`. Icon fonts (e.g. Lucide) often have far more glyphs
897
+ * than reachable Unicode codepoints — this is how a consumer enumerates
898
+ * "every glyph" rather than "every mapped character"; derived from
899
+ * `loca` (always `numGlyphs + 1` entries), no extra stored state.
900
+ */
901
+ get numGlyphs() {
902
+ return this._loca.length - 1;
903
+ }
904
+ /**
905
+ * Advance width of a glyph in font units.
906
+ *
907
+ * @param glyphId Glyph index.
908
+ */
909
+ advance(n) {
910
+ return (this._hmtx[n] ?? this._hmtx[this._hmtx.length - 1] ?? { advanceWidth: 0 }).advanceWidth;
911
+ }
912
+ /**
913
+ * Left side bearing of a glyph in font units.
914
+ *
915
+ * @param glyphId Glyph index.
916
+ */
917
+ lsb(n) {
918
+ return (this._hmtx[n] ?? this._hmtx[this._hmtx.length - 1] ?? { lsb: 0 }).lsb;
919
+ }
920
+ /**
921
+ * Kerning adjustment between two glyphs in font units.
922
+ * Returns 0 if the font has no kern table or the pair is not listed.
923
+ *
924
+ * @param leftGlyphId Left glyph index.
925
+ * @param rightGlyphId Right glyph index.
926
+ */
927
+ kerning(n, t) {
928
+ return this._kern(n, t);
929
+ }
930
+ /**
931
+ * Parses and returns the outline of a glyph as a Shape (raw font units, y-up).
932
+ *
933
+ * Composite glyphs are recursively resolved. The returned Shape has all
934
+ * contours in absolute coordinates of the root glyph's coordinate space.
935
+ *
936
+ * Returns an empty Shape for glyphs with no outline (space, .notdef with no
937
+ * outline, etc.).
938
+ *
939
+ * @param glyphId Glyph index.
940
+ * @returns Shape with contours in font units, inverseYAxis=false.
941
+ */
942
+ shape(n) {
943
+ const t = this._tables.get("glyf");
944
+ if (!t) return { contours: [], inverseYAxis: !1 };
945
+ const s = this._loca, i = s[n] ?? 0, p = (s[n + 1] ?? i) - i;
946
+ return mo(
947
+ this._buffer,
948
+ t.offset,
949
+ i,
950
+ p,
951
+ (c) => this._glyphRaw(c)
952
+ );
953
+ }
954
+ /**
955
+ * Parses a glyph to its raw (pre-midpoint) outline, used as the composite
956
+ * recursion primitive so implied midpoints are expanded only on the final
957
+ * merged outline (matching FreeType).
958
+ * @internal
959
+ */
960
+ _glyphRaw(n) {
961
+ const t = this._tables.get("glyf");
962
+ if (!t) return [];
963
+ const s = this._loca, i = s[n] ?? 0, p = (s[n + 1] ?? i) - i;
964
+ return Ks(
965
+ this._buffer,
966
+ t.offset,
967
+ i,
968
+ p,
969
+ (c) => this._glyphRaw(c)
970
+ );
971
+ }
972
+ }
973
+ function Uo(e, n) {
974
+ const t = 1 / n;
975
+ for (const s of e.contours)
976
+ for (let i = 0; i < s.length; i++) {
977
+ const o = s[i];
978
+ if (o.p0x *= t, o.p0y *= t, o.p1x *= t, o.p1y *= t, o.p2x *= t, o.p2y *= t, o.p3x *= t, o.p3y *= t, o.type === it) {
979
+ const p = o.p1x - o.p0x, c = o.p1y - o.p0y, r = o.p2x - o.p1x, h = o.p2y - o.p1y;
980
+ J(p, c, r, h) === 0 && (s[i] = new et(at, o.p0x, o.p0y, o.p2x, o.p2y, 0, 0, 0, 0));
981
+ }
982
+ }
983
+ }
984
+ const Ls = 1e-6, Yo = 1.1111111111111112, Xo = new Float64Array(16);
985
+ function Gs(e, n, t) {
986
+ switch (n[t] = e.p0x, n[t + 1] = e.p0y, n[t + 2] = e.p1x, n[t + 3] = e.p1y, e.type) {
987
+ case at:
988
+ return 1;
989
+ case it:
990
+ return n[t + 4] = e.p2x, n[t + 5] = e.p2y, 2;
991
+ default:
992
+ return n[t + 4] = e.p2x, n[t + 5] = e.p2y, n[t + 6] = e.p3x, n[t + 7] = e.p3y, 3;
993
+ }
994
+ }
995
+ function Os(e, n, t) {
996
+ if (t === 3) {
997
+ const s = e[n], i = e[n + 1], o = e[n + 2], p = e[n + 3], c = e[n + 4], r = e[n + 5], h = e[n + 6], l = e[n + 7];
998
+ (o === s && p === i || o === h && p === l) && (c === s && r === i || c === h && r === l) && (e[n + 2] = h, e[n + 3] = l, t = 1);
999
+ }
1000
+ if (t === 2) {
1001
+ const s = e[n], i = e[n + 1], o = e[n + 2], p = e[n + 3], c = e[n + 4], r = e[n + 5];
1002
+ (o === s && p === i || o === c && p === r) && (e[n + 2] = c, e[n + 3] = r, t = 1);
1003
+ }
1004
+ return t === 1 && e[n] === e[n + 2] && e[n + 1] === e[n + 3] && (t = 0), t;
1005
+ }
1006
+ function Wo(e, n) {
1007
+ const t = Xo;
1008
+ let s = Gs(e, t, 0), i = Gs(n, t, 8);
1009
+ if (!(s >= 1 && s <= 3 && i >= 1 && i <= 3) || t[s * 2] !== t[8] || t[s * 2 + 1] !== t[9]) return 0;
1010
+ s = Os(t, 0, s), i = Os(t, 8, i);
1011
+ const o = t[s * 2], p = t[s * 2 + 1];
1012
+ let c = 0, r = 0, h = 0, l = 0, a = 0, f = 0, y = 0, _ = 0, u = 0, x = 0, M = 0, w = 0;
1013
+ if (s >= 1 && (c = t[(s - 1) * 2] - o, r = t[(s - 1) * 2 + 1] - p), s >= 2) {
1014
+ const H = t[(s - 2) * 2] - t[(s - 1) * 2], z = t[(s - 2) * 2 + 1] - t[(s - 1) * 2 + 1];
1015
+ h = H - c, l = z - r;
1016
+ }
1017
+ if (s >= 3) {
1018
+ const H = t[(s - 3) * 2] - t[(s - 2) * 2], z = t[(s - 3) * 2 + 1] - t[(s - 2) * 2 + 1], ht = t[(s - 2) * 2] - t[(s - 1) * 2], st = t[(s - 2) * 2 + 1] - t[(s - 1) * 2 + 1];
1019
+ a = H - ht - h, f = z - st - l, h *= 3, l *= 3;
1020
+ }
1021
+ if (i >= 1 && (y = t[10] - o, _ = t[11] - p), i >= 2) {
1022
+ const H = t[12] - t[10], z = t[13] - t[11];
1023
+ u = H - y, x = z - _;
1024
+ }
1025
+ if (i >= 3) {
1026
+ const H = t[14] - t[12], z = t[15] - t[13], ht = t[12] - t[10], st = t[13] - t[11];
1027
+ M = H - ht - u, w = z - st - x, u *= 3, x *= 3;
1028
+ }
1029
+ c *= s, r *= s, y *= i, _ *= i;
1030
+ const v = c !== 0 || r !== 0;
1031
+ if (v && (y !== 0 || _ !== 0)) {
1032
+ const H = Math.sqrt(c * c + r * r), z = Math.sqrt(y * y + _ * _), ht = H * J(c, r, u, x) + z * J(h, l, y, _);
1033
+ if (ht) return rt(ht);
1034
+ const st = H * H * J(c, r, M, w) + H * z * J(h, l, u, x) + z * z * J(a, f, y, _);
1035
+ if (st) return rt(st);
1036
+ const kt = H * J(h, l, M, w) + z * J(a, f, u, x);
1037
+ return rt(kt || J(a, f, M, w));
1038
+ }
1039
+ let A = 1, D = h, I = l, F = a, T = f, b = y, N = _, R = u, q = x, P = M, X = w;
1040
+ if (v && (D = u, I = x, F = M, T = w, b = c, N = r, R = h, q = l, P = a, X = f, A = -1), b !== 0 || N !== 0) {
1041
+ const H = J(F, T, b, N);
1042
+ if (H) return A * rt(H);
1043
+ const z = J(D, I, R, q);
1044
+ if (z) return A * rt(z);
1045
+ const ht = J(F, T, R, q);
1046
+ if (ht) return A * rt(ht);
1047
+ const st = J(D, I, P, X);
1048
+ return st ? A * rt(st) : A * rt(J(F, T, P, X));
1049
+ }
1050
+ const Q = Math.sqrt(D * D + I * I), V = Math.sqrt(R * R + q * q), nt = Math.sqrt(Q) * J(D, I, P, X) + Math.sqrt(V) * J(F, T, R, q);
1051
+ return rt(nt || J(F, T, P, X));
1052
+ }
1053
+ function Ps(e, n, t, s, i) {
1054
+ let o = e[n];
1055
+ if (o.type === it) {
1056
+ const p = o.convertToCubic();
1057
+ e[n] = p, o = p;
1058
+ }
1059
+ if (o.type === Ot)
1060
+ if (t === 0) {
1061
+ const p = Math.sqrt((o.p1x - o.p0x) ** 2 + (o.p1y - o.p0y) ** 2);
1062
+ o.p1x += p * s, o.p1y += p * i;
1063
+ } else {
1064
+ const p = Math.sqrt((o.p2x - o.p3x) ** 2 + (o.p2y - o.p3y) ** 2);
1065
+ o.p2x += p * s, o.p2y += p * i;
1066
+ }
1067
+ }
1068
+ const Ft = [0, 0], St = [0, 0];
1069
+ function Ho(e) {
1070
+ for (let n = 0; n < e.contours.length; n++) {
1071
+ const t = e.contours[n];
1072
+ let s = 0;
1073
+ for (let i = 0; i < t.length; i++) {
1074
+ const o = t[i];
1075
+ (o.p0x !== o.endX() || o.p0y !== o.endY()) && (t[s++] = o);
1076
+ }
1077
+ t.length = s;
1078
+ }
1079
+ for (const n of e.contours)
1080
+ if (n.length === 1) {
1081
+ const [t, s, i] = n[0].splitInThirds();
1082
+ n.length = 0, n.push(t, s, i);
1083
+ } else if (n.length > 0) {
1084
+ const t = n.length;
1085
+ for (let s = 0; s < t; s++) {
1086
+ const i = (s + t - 1) % t, o = n[i], p = n[s];
1087
+ o.direction(1, Ft), p.direction(0, St);
1088
+ const c = Math.sqrt(Ft[0] ** 2 + Ft[1] ** 2), r = Math.sqrt(St[0] ** 2 + St[1] ** 2);
1089
+ if (c === 0 || r === 0) continue;
1090
+ const h = Ft[0] / c, l = Ft[1] / c, a = St[0] / r, f = St[1] / r;
1091
+ if (io(h, l, a, f) < Ls - 1) {
1092
+ const y = Ls - 1, _ = Yo * Math.sqrt(1 - y * y) / y;
1093
+ let u = a - h, x = f - l;
1094
+ const M = Math.sqrt(u * u + x * x);
1095
+ if (M === 0) continue;
1096
+ u = _ * u / M, x = _ * x / M, Wo(o, p) < 0 && (u = -u, x = -x), Ps(n, i, 1, -x, u), Ps(n, s, 0, x, -u);
1097
+ }
1098
+ }
1099
+ }
1100
+ }
1101
+ const Jt = 1, tn = 2, Vo = 3, nn = 4, zo = 5, Bo = 6, Qn = 7, ot = [0, 0], Et = [0, 0];
1102
+ function Qo(e, n) {
1103
+ return Math.trunc(3 + 2.875 * e / (n - 1) - 1.4375 + 0.5) - 3;
1104
+ }
1105
+ function $o(e, n, t, s, i) {
1106
+ return e * t + n * s <= 0 || Math.abs(e * s - n * t) > i;
1107
+ }
1108
+ function Ko(e) {
1109
+ const n = Number(e.v & 1n);
1110
+ return e.v >>= 1n, n;
1111
+ }
1112
+ function Zo(e) {
1113
+ const n = Number(e.v % 3n);
1114
+ return e.v /= 3n, n;
1115
+ }
1116
+ function Ns(e) {
1117
+ return [Bo, zo, Vo][Zo(e)];
1118
+ }
1119
+ function Pt(e, n) {
1120
+ const t = e.v << 1 + Ko(n);
1121
+ e.v = (t | t >> 3) & Qn;
1122
+ }
1123
+ function jo(e, n, t) {
1124
+ const s = e.v & t;
1125
+ s === Jt || s === tn || s === nn ? e.v = s ^ Qn : Pt(e, n);
1126
+ }
1127
+ function Jo(e, n, t = 0n) {
1128
+ const s = Math.sin(n), i = { v: Ns({ v: t }) }, o = { v: t };
1129
+ o.v = t, i.v = Ns(o);
1130
+ const p = [];
1131
+ for (const c of e.contours) {
1132
+ if (c.length === 0) continue;
1133
+ p.length = 0, c[c.length - 1].direction(1, ot);
1134
+ const h = Math.sqrt(ot[0] * ot[0] + ot[1] * ot[1]);
1135
+ let l = h > 0 ? ot[0] / h : 0, a = h > 0 ? ot[1] / h : 0;
1136
+ for (let f = 0; f < c.length; f++) {
1137
+ const y = c[f];
1138
+ y.direction(0, Et);
1139
+ const _ = Math.sqrt(Et[0] * Et[0] + Et[1] * Et[1]), u = _ > 0 ? Et[0] / _ : 0, x = _ > 0 ? Et[1] / _ : 0;
1140
+ $o(l, a, u, x, s) && p.push(f), y.direction(1, ot);
1141
+ const M = Math.sqrt(ot[0] * ot[0] + ot[1] * ot[1]);
1142
+ l = M > 0 ? ot[0] / M : 0, a = M > 0 ? ot[1] / M : 0;
1143
+ }
1144
+ if (p.length === 0) {
1145
+ Pt(i, o);
1146
+ for (const f of c) f.color = i.v;
1147
+ } else if (p.length === 1) {
1148
+ const f = [0, Qn, 0];
1149
+ Pt(i, o), f[0] = i.v, Pt(i, o), f[2] = i.v;
1150
+ const y = p[0], _ = c.length;
1151
+ if (_ >= 3)
1152
+ for (let u = 0; u < _; u++) {
1153
+ const x = Qo(u, _);
1154
+ c[(y + u) % _].color = f[1 + x];
1155
+ }
1156
+ else if (_ >= 1) {
1157
+ const u = [null, null, null, null, null, null, null], x = c[0].splitInThirds();
1158
+ if (u[0 + 3 * y] = x[0], u[1 + 3 * y] = x[1], u[2 + 3 * y] = x[2], _ >= 2) {
1159
+ const M = c[1].splitInThirds();
1160
+ u[3 - 3 * y] = M[0], u[4 - 3 * y] = M[1], u[5 - 3 * y] = M[2], u[0].color = u[1].color = f[0], u[2].color = u[3].color = f[1], u[4].color = u[5].color = f[2];
1161
+ } else
1162
+ u[0].color = f[0], u[1].color = f[1], u[2].color = f[2];
1163
+ c.length = 0;
1164
+ for (const M of u) M !== null && c.push(M);
1165
+ }
1166
+ } else {
1167
+ const f = p.length;
1168
+ let y = 0;
1169
+ const _ = p[0], u = c.length;
1170
+ Pt(i, o);
1171
+ const x = i.v;
1172
+ for (let M = 0; M < u; M++) {
1173
+ const w = (_ + M) % u;
1174
+ y + 1 < f && p[y + 1] === w && (++y, jo(i, o, y === f - 1 ? x : 0)), c[w].color = i.v;
1175
+ }
1176
+ }
1177
+ }
1178
+ }
1179
+ const Y = Number.MAX_VALUE, m = [0, 0], yt = [0, 0];
1180
+ let Us = 0, B = new Float64Array(0), K = new Float64Array(0), G = new Float64Array(0), O = new Float64Array(0), Z = new Int32Array(0), j = new Float64Array(0), It = new Float64Array(0), Rt = new Float64Array(0), Tt = new Float64Array(0), qt = new Int32Array(0);
1181
+ const $ = [0, 0];
1182
+ let Ys = 0, Rn = new Float64Array(0), Tn = new Float64Array(0), mn = new Float64Array(0), kn = new Float64Array(0), Fn = new Float64Array(0), Sn = new Float64Array(0), Ln = new Float64Array(0), Gn = new Float64Array(0), On = new Float64Array(0), Pn = new Float64Array(0), Nn = new Float64Array(0), Un = new Float64Array(0), Xs = 0, Lt = new Int32Array(0);
1183
+ function ti(e) {
1184
+ const n = e.length;
1185
+ if (n === 0) return 0;
1186
+ let t = 0;
1187
+ if (n === 1) {
1188
+ e[0].point(0, $);
1189
+ const s = $[0], i = $[1];
1190
+ e[0].point(1 / 3, $);
1191
+ const o = $[0], p = $[1];
1192
+ e[0].point(2 / 3, $);
1193
+ const c = $[0], r = $[1];
1194
+ t += s * p - i * o, t += o * r - p * c, t += c * i - r * s;
1195
+ } else if (n === 2) {
1196
+ e[0].point(0, $);
1197
+ const s = $[0], i = $[1];
1198
+ e[0].point(0.5, $);
1199
+ const o = $[0], p = $[1];
1200
+ e[1].point(0, $);
1201
+ const c = $[0], r = $[1];
1202
+ e[1].point(0.5, $);
1203
+ const h = $[0], l = $[1];
1204
+ t += s * p - i * o, t += o * r - p * c, t += c * l - r * h, t += h * i - l * s;
1205
+ } else {
1206
+ const s = e[n - 1];
1207
+ let i = s.p0x, o = s.p0y;
1208
+ for (let p = 0; p < n; p++) {
1209
+ const c = e[p].p0x, r = e[p].p0y;
1210
+ t += i * r - o * c, i = c, o = r;
1211
+ }
1212
+ }
1213
+ return t > 0 ? -1 : t < 0 ? 1 : 0;
1214
+ }
1215
+ function ni(e, n, t, s, i, o, p) {
1216
+ const c = p[e][n];
1217
+ let r = t;
1218
+ if (o < 0) {
1219
+ c.direction(0, m);
1220
+ const h = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1221
+ if (h > 0) {
1222
+ const l = m[0] / h, a = m[1] / h;
1223
+ c.point(0, yt);
1224
+ const f = s - yt[0], y = i - yt[1];
1225
+ if (f * l + y * a < 0) {
1226
+ const u = f * a - y * l;
1227
+ Math.abs(u) <= Math.abs(r) && (r = u);
1228
+ }
1229
+ }
1230
+ }
1231
+ if (o > 1) {
1232
+ c.direction(1, m);
1233
+ const h = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1234
+ if (h > 0) {
1235
+ const l = m[0] / h, a = m[1] / h;
1236
+ c.point(1, yt);
1237
+ const f = s - yt[0], y = i - yt[1];
1238
+ if (f * l + y * a > 0) {
1239
+ const u = f * a - y * l;
1240
+ Math.abs(u) <= Math.abs(r) && (r = u);
1241
+ }
1242
+ }
1243
+ }
1244
+ return r;
1245
+ }
1246
+ function lt(e, n, t, s, i, o, p, c, r) {
1247
+ let h = e < 0 ? n : t;
1248
+ if (i >= 0) {
1249
+ const l = ni(s, i, e, p, c, o, r);
1250
+ Math.abs(l) < Math.abs(h) && (h = l);
1251
+ }
1252
+ return h;
1253
+ }
1254
+ function Mt(e, n, t) {
1255
+ return Math.max(Math.min(e, n), Math.min(Math.max(e, n), t));
1256
+ }
1257
+ function si(e, n, t, s, i, o, p, c) {
1258
+ if (c.length !== n * t * 3)
1259
+ throw new Error(`generateMSDF: out.length ${c.length} !== ${n * t * 3}`);
1260
+ const r = e.contours, h = r.length;
1261
+ if (h === 0) {
1262
+ c.fill(0);
1263
+ return;
1264
+ }
1265
+ const l = s / p;
1266
+ qt.length < h && (qt = new Int32Array(h));
1267
+ for (let y = 0; y < h; y++) qt[y] = ti(r[y]);
1268
+ Xs < h + 1 && (Lt = new Int32Array(h + 1), Xs = h + 1);
1269
+ let a = 0;
1270
+ for (let y = 0; y < h; y++)
1271
+ Lt[y] = a, a += r[y].length;
1272
+ Lt[h] = a, Ys < a && (Rn = new Float64Array(a), Tn = new Float64Array(a), mn = new Float64Array(a), kn = new Float64Array(a), Fn = new Float64Array(a), Sn = new Float64Array(a), Ln = new Float64Array(a), Gn = new Float64Array(a), On = new Float64Array(a), Pn = new Float64Array(a), Nn = new Float64Array(a), Un = new Float64Array(a), Ys = a);
1273
+ for (let y = 0; y < h; y++) {
1274
+ const _ = r[y], u = _.length, x = Lt[y];
1275
+ for (let M = 0; M < u; M++) {
1276
+ const w = _[M], v = _[(M + u - 1) % u], C = _[(M + 1) % u], A = x + M;
1277
+ w.point(0, yt), Rn[A] = yt[0], Tn[A] = yt[1], w.point(1, yt), mn[A] = yt[0], kn[A] = yt[1], w.direction(0, m);
1278
+ let D = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1279
+ const I = D > 0 ? m[0] / D : 0, F = D > 0 ? m[1] / D : 0;
1280
+ Fn[A] = I, Sn[A] = F, w.direction(1, m), D = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1281
+ const T = D > 0 ? m[0] / D : 0, b = D > 0 ? m[1] / D : 0;
1282
+ Ln[A] = T, Gn[A] = b, v.direction(1, m), D = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1283
+ const N = D > 0 ? m[0] / D : 0, R = D > 0 ? m[1] / D : 0;
1284
+ C.direction(0, m), D = Math.sqrt(m[0] * m[0] + m[1] * m[1]);
1285
+ const q = D > 0 ? m[0] / D : 0, P = D > 0 ? m[1] / D : 0, X = N + I, Q = R + F, V = Math.sqrt(X * X + Q * Q);
1286
+ On[A] = V > 0 ? X / V : 0, Pn[A] = V > 0 ? Q / V : 0;
1287
+ const nt = T + q, H = b + P, z = Math.sqrt(nt * nt + H * H);
1288
+ Nn[A] = z > 0 ? -(nt / z) : 0, Un[A] = z > 0 ? -(H / z) : 0;
1289
+ }
1290
+ }
1291
+ if (Us < h) {
1292
+ const y = h * 3;
1293
+ B = new Float64Array(y), K = new Float64Array(y), G = new Float64Array(y), O = new Float64Array(y), Z = new Int32Array(y), j = new Float64Array(y), It = new Float64Array(h), Rt = new Float64Array(h), Tt = new Float64Array(h), Us = h;
1294
+ }
1295
+ const f = { distance: 0, dot: 0, param: 0 };
1296
+ for (let y = 0; y < t; y++)
1297
+ for (let _ = 0; _ < n; _++) {
1298
+ const u = (_ + 0.5) / s - i, x = (y + 0.5) / s - o;
1299
+ for (let E = 0; E < h; E++) {
1300
+ const d = r[E], ct = d.length, g = E * 3, S = Lt[E];
1301
+ B[g] = B[g + 1] = B[g + 2] = -Y, K[g] = K[g + 1] = K[g + 2] = 0, G[g] = G[g + 1] = G[g + 2] = -Y, O[g] = O[g + 1] = O[g + 2] = Y, Z[g] = Z[g + 1] = Z[g + 2] = -1, j[g] = j[g + 1] = j[g + 2] = 0;
1302
+ for (let L = 0; L < ct; L++) {
1303
+ const W = d[L], bn = W.color, An = (bn & Jt) !== 0, wn = (bn & tn) !== 0, Dn = (bn & nn) !== 0;
1304
+ W.signedDistance(u, x, f);
1305
+ const zt = f.distance, Ct = f.dot, En = f.param, At = Math.abs(zt);
1306
+ if (An) {
1307
+ const dt = Math.abs(B[g]);
1308
+ (At < dt || At === dt && Ct < K[g]) && (B[g] = zt, K[g] = Ct, Z[g] = L, j[g] = En);
1309
+ }
1310
+ if (wn) {
1311
+ const dt = Math.abs(B[g + 1]);
1312
+ (At < dt || At === dt && Ct < K[g + 1]) && (B[g + 1] = zt, K[g + 1] = Ct, Z[g + 1] = L, j[g + 1] = En);
1313
+ }
1314
+ if (Dn) {
1315
+ const dt = Math.abs(B[g + 2]);
1316
+ (At < dt || At === dt && Ct < K[g + 2]) && (B[g + 2] = zt, K[g + 2] = Ct, Z[g + 2] = L, j[g + 2] = En);
1317
+ }
1318
+ const pt = S + L, qn = u - Rn[pt], vn = x - Tn[pt], Cn = u - mn[pt], In = x - kn[pt], qs = Fn[pt], vs = Sn[pt], Cs = Ln[pt], Is = Gn[pt], so = qn * On[pt] + vn * Pn[pt], oo = Cn * Nn[pt] + In * Un[pt];
1319
+ if (so > 0 && -(qn * qs + vn * vs) > 0) {
1320
+ const k = qn * vs - vn * qs;
1321
+ Math.abs(k) < At && (An && (k <= 0 && k > G[g] ? G[g] = k : k > 0 && k < O[g] && (O[g] = k)), wn && (k <= 0 && k > G[g + 1] ? G[g + 1] = k : k > 0 && k < O[g + 1] && (O[g + 1] = k)), Dn && (k <= 0 && k > G[g + 2] ? G[g + 2] = k : k > 0 && k < O[g + 2] && (O[g + 2] = k)));
1322
+ }
1323
+ if (oo > 0 && Cn * Cs + In * Is > 0) {
1324
+ const k = Cn * Is - In * Cs;
1325
+ Math.abs(k) < At && (An && (k <= 0 && k > G[g] ? G[g] = k : k > 0 && k < O[g] && (O[g] = k)), wn && (k <= 0 && k > G[g + 1] ? G[g + 1] = k : k > 0 && k < O[g + 1] && (O[g + 1] = k)), Dn && (k <= 0 && k > G[g + 2] ? G[g + 2] = k : k > 0 && k < O[g + 2] && (O[g + 2] = k)));
1326
+ }
1327
+ }
1328
+ It[E] = lt(
1329
+ B[g],
1330
+ G[g],
1331
+ O[g],
1332
+ E,
1333
+ Z[g],
1334
+ j[g],
1335
+ u,
1336
+ x,
1337
+ r
1338
+ ), Rt[E] = lt(
1339
+ B[g + 1],
1340
+ G[g + 1],
1341
+ O[g + 1],
1342
+ E,
1343
+ Z[g + 1],
1344
+ j[g + 1],
1345
+ u,
1346
+ x,
1347
+ r
1348
+ ), Tt[E] = lt(
1349
+ B[g + 2],
1350
+ G[g + 2],
1351
+ O[g + 2],
1352
+ E,
1353
+ Z[g + 2],
1354
+ j[g + 2],
1355
+ u,
1356
+ x,
1357
+ r
1358
+ );
1359
+ }
1360
+ let M = -Y, w = 0, v = -Y, C = Y, A = -1, D = -1, I = 0, F = -Y, T = 0, b = -Y, N = Y, R = -1, q = -1, P = 0, X = -Y, Q = 0, V = -Y, nt = Y, H = -1, z = -1, ht = 0, st = -Y, kt = 0, sn = -Y, on = Y, Kn = -1, Zn = -1, jn = 0, Nt = -Y, Jn = 0, en = -Y, cn = Y, ts = -1, ns = -1, ss = 0, Ut = -Y, os = 0, rn = -Y, an = Y, is = -1, es = -1, cs = 0, Yt = -Y, rs = 0, hn = -Y, pn = Y, as = -1, hs = -1, ps = 0, Xt = -Y, ls = 0, ln = -Y, fn = Y, fs = -1, ys = -1, us = 0, Wt = -Y, _s = 0, yn = -Y, un = Y, xs = -1, Ms = -1, gs = 0;
1361
+ for (let E = 0; E < h; E++) {
1362
+ const d = E * 3, ct = qt[E], g = Mt(It[E], Rt[E], Tt[E]);
1363
+ {
1364
+ const S = B[d], L = K[d], W = Math.abs(S);
1365
+ (W < Math.abs(M) || W === Math.abs(M) && L < w) && (M = S, w = L, A = E, D = Z[d], I = j[d]), G[d] > v && (v = G[d]), O[d] < C && (C = O[d]);
1366
+ }
1367
+ {
1368
+ const S = B[d + 1], L = K[d + 1], W = Math.abs(S);
1369
+ (W < Math.abs(F) || W === Math.abs(F) && L < T) && (F = S, T = L, R = E, q = Z[d + 1], P = j[d + 1]), G[d + 1] > b && (b = G[d + 1]), O[d + 1] < N && (N = O[d + 1]);
1370
+ }
1371
+ {
1372
+ const S = B[d + 2], L = K[d + 2], W = Math.abs(S);
1373
+ (W < Math.abs(X) || W === Math.abs(X) && L < Q) && (X = S, Q = L, H = E, z = Z[d + 2], ht = j[d + 2]), G[d + 2] > V && (V = G[d + 2]), O[d + 2] < nt && (nt = O[d + 2]);
1374
+ }
1375
+ if (ct > 0 && g >= 0) {
1376
+ {
1377
+ const S = B[d], L = K[d], W = Math.abs(S);
1378
+ (W < Math.abs(st) || W === Math.abs(st) && L < kt) && (st = S, kt = L, Kn = E, Zn = Z[d], jn = j[d]), G[d] > sn && (sn = G[d]), O[d] < on && (on = O[d]);
1379
+ }
1380
+ {
1381
+ const S = B[d + 1], L = K[d + 1], W = Math.abs(S);
1382
+ (W < Math.abs(Nt) || W === Math.abs(Nt) && L < Jn) && (Nt = S, Jn = L, ts = E, ns = Z[d + 1], ss = j[d + 1]), G[d + 1] > en && (en = G[d + 1]), O[d + 1] < cn && (cn = O[d + 1]);
1383
+ }
1384
+ {
1385
+ const S = B[d + 2], L = K[d + 2], W = Math.abs(S);
1386
+ (W < Math.abs(Ut) || W === Math.abs(Ut) && L < os) && (Ut = S, os = L, is = E, es = Z[d + 2], cs = j[d + 2]), G[d + 2] > rn && (rn = G[d + 2]), O[d + 2] < an && (an = O[d + 2]);
1387
+ }
1388
+ }
1389
+ if (ct < 0 && g <= 0) {
1390
+ {
1391
+ const S = B[d], L = K[d], W = Math.abs(S);
1392
+ (W < Math.abs(Yt) || W === Math.abs(Yt) && L < rs) && (Yt = S, rs = L, as = E, hs = Z[d], ps = j[d]), G[d] > hn && (hn = G[d]), O[d] < pn && (pn = O[d]);
1393
+ }
1394
+ {
1395
+ const S = B[d + 1], L = K[d + 1], W = Math.abs(S);
1396
+ (W < Math.abs(Xt) || W === Math.abs(Xt) && L < ls) && (Xt = S, ls = L, fs = E, ys = Z[d + 1], us = j[d + 1]), G[d + 1] > ln && (ln = G[d + 1]), O[d + 1] < fn && (fn = O[d + 1]);
1397
+ }
1398
+ {
1399
+ const S = B[d + 2], L = K[d + 2], W = Math.abs(S);
1400
+ (W < Math.abs(Wt) || W === Math.abs(Wt) && L < _s) && (Wt = S, _s = L, xs = E, Ms = Z[d + 2], gs = j[d + 2]), G[d + 2] > yn && (yn = G[d + 2]), O[d + 2] < un && (un = O[d + 2]);
1401
+ }
1402
+ }
1403
+ }
1404
+ const _n = lt(
1405
+ M,
1406
+ v,
1407
+ C,
1408
+ A,
1409
+ D,
1410
+ I,
1411
+ u,
1412
+ x,
1413
+ r
1414
+ ), xn = lt(
1415
+ F,
1416
+ b,
1417
+ N,
1418
+ R,
1419
+ q,
1420
+ P,
1421
+ u,
1422
+ x,
1423
+ r
1424
+ ), Mn = lt(
1425
+ X,
1426
+ V,
1427
+ nt,
1428
+ H,
1429
+ z,
1430
+ ht,
1431
+ u,
1432
+ x,
1433
+ r
1434
+ ), no = Mt(_n, xn, Mn), ds = lt(
1435
+ st,
1436
+ sn,
1437
+ on,
1438
+ Kn,
1439
+ Zn,
1440
+ jn,
1441
+ u,
1442
+ x,
1443
+ r
1444
+ ), bs = lt(
1445
+ Nt,
1446
+ en,
1447
+ cn,
1448
+ ts,
1449
+ ns,
1450
+ ss,
1451
+ u,
1452
+ x,
1453
+ r
1454
+ ), As = lt(
1455
+ Ut,
1456
+ rn,
1457
+ an,
1458
+ is,
1459
+ es,
1460
+ cs,
1461
+ u,
1462
+ x,
1463
+ r
1464
+ ), Ht = Mt(ds, bs, As), ws = lt(
1465
+ Yt,
1466
+ hn,
1467
+ pn,
1468
+ as,
1469
+ hs,
1470
+ ps,
1471
+ u,
1472
+ x,
1473
+ r
1474
+ ), Ds = lt(
1475
+ Xt,
1476
+ ln,
1477
+ fn,
1478
+ fs,
1479
+ ys,
1480
+ us,
1481
+ u,
1482
+ x,
1483
+ r
1484
+ ), Es = lt(
1485
+ Wt,
1486
+ yn,
1487
+ un,
1488
+ xs,
1489
+ Ms,
1490
+ gs,
1491
+ u,
1492
+ x,
1493
+ r
1494
+ ), Vt = Mt(ws, Ds, Es);
1495
+ let ut, _t, xt, gn = 0;
1496
+ if (Ht >= 0 && Math.abs(Ht) <= Math.abs(Vt)) {
1497
+ ut = ds, _t = bs, xt = As, gn = 1;
1498
+ for (let E = 0; E < h; E++)
1499
+ if (qt[E] > 0) {
1500
+ const d = It[E], ct = Rt[E], g = Tt[E], S = Mt(d, ct, g);
1501
+ Math.abs(S) < Math.abs(Vt) && S > Mt(ut, _t, xt) && (ut = d, _t = ct, xt = g);
1502
+ }
1503
+ } else if (Vt <= 0 && Math.abs(Vt) < Math.abs(Ht)) {
1504
+ ut = ws, _t = Ds, xt = Es, gn = -1;
1505
+ for (let E = 0; E < h; E++)
1506
+ if (qt[E] < 0) {
1507
+ const d = It[E], ct = Rt[E], g = Tt[E], S = Mt(d, ct, g);
1508
+ Math.abs(S) < Math.abs(Ht) && S < Mt(ut, _t, xt) && (ut = d, _t = ct, xt = g);
1509
+ }
1510
+ } else {
1511
+ const E = (y * n + _) * 3;
1512
+ c[E + 0] = _n * l + 0.5, c[E + 1] = xn * l + 0.5, c[E + 2] = Mn * l + 0.5;
1513
+ continue;
1514
+ }
1515
+ for (let E = 0; E < h; E++)
1516
+ if (qt[E] !== gn) {
1517
+ const d = It[E], ct = Rt[E], g = Tt[E], S = Mt(d, ct, g), L = Mt(ut, _t, xt);
1518
+ S * L >= 0 && Math.abs(S) < Math.abs(L) && (ut = d, _t = ct, xt = g);
1519
+ }
1520
+ Mt(ut, _t, xt) === no && (ut = _n, _t = xn, xt = Mn);
1521
+ const dn = (y * n + _) * 3;
1522
+ c[dn + 0] = ut * l + 0.5, c[dn + 1] = _t * l + 0.5, c[dn + 2] = xt * l + 0.5;
1523
+ }
1524
+ }
1525
+ const Zs = 0, oi = 1, ii = 2, ei = 3;
1526
+ function ci(e, n) {
1527
+ switch (n) {
1528
+ case Zs:
1529
+ return e !== 0;
1530
+ case oi:
1531
+ return (e & 1) !== 0;
1532
+ case ii:
1533
+ return e > 0;
1534
+ case ei:
1535
+ return e < 0;
1536
+ }
1537
+ return !1;
1538
+ }
1539
+ class ri {
1540
+ constructor() {
1541
+ this._intersections = [], this._lastIndex = 0;
1542
+ }
1543
+ /**
1544
+ * Replaces the crossings, sorting by x and prefix-summing the directions.
1545
+ * port of core/Scanline.cpp: Scanline::setIntersections + preprocess
1546
+ *
1547
+ * @param intersections Unsorted crossings (ownership transferred; mutated).
1548
+ */
1549
+ setIntersections(n) {
1550
+ if (this._intersections = n, this._lastIndex = 0, n.length > 0) {
1551
+ n.sort((s, i) => rt(s.x - i.x));
1552
+ let t = 0;
1553
+ for (let s = 0; s < n.length; s++)
1554
+ t += n[s].direction, n[s].direction = t;
1555
+ }
1556
+ }
1557
+ /**
1558
+ * Finds the index of the last crossing at or before x (or -1).
1559
+ * port of core/Scanline.cpp: Scanline::moveTo
1560
+ * @param x Query x-coordinate.
1561
+ * @returns Index into the sorted crossings, or -1 when x precedes all.
1562
+ */
1563
+ _moveTo(n) {
1564
+ const t = this._intersections.length;
1565
+ if (t === 0) return -1;
1566
+ let s = this._lastIndex;
1567
+ if (n < this._intersections[s].x)
1568
+ do {
1569
+ if (s === 0)
1570
+ return this._lastIndex = 0, -1;
1571
+ --s;
1572
+ } while (n < this._intersections[s].x);
1573
+ else
1574
+ for (; s < t - 1 && n >= this._intersections[s + 1].x; ) ++s;
1575
+ return this._lastIndex = s, s;
1576
+ }
1577
+ /**
1578
+ * Returns the accumulated winding number at x.
1579
+ * port of core/Scanline.cpp: Scanline::sumIntersections
1580
+ * @param x Query x-coordinate.
1581
+ * @returns Signed winding number.
1582
+ */
1583
+ sumIntersections(n) {
1584
+ const t = this._moveTo(n);
1585
+ return t >= 0 ? this._intersections[t].direction : 0;
1586
+ }
1587
+ /**
1588
+ * Tests whether the point at x is filled under the given fill rule.
1589
+ * port of core/Scanline.cpp: Scanline::filled
1590
+ * @param x Query x-coordinate.
1591
+ * @param fillRule Fill rule to apply.
1592
+ * @returns True when filled.
1593
+ */
1594
+ filled(n, t) {
1595
+ return ci(this.sumIntersections(n), t);
1596
+ }
1597
+ }
1598
+ const Ws = [0, 0, 0], Hs = [0, 0, 0];
1599
+ function ai(e, n, t) {
1600
+ const s = [];
1601
+ for (const i of e.contours)
1602
+ for (const o of i) {
1603
+ const p = o.scanlineIntersections(n, Ws, Hs);
1604
+ for (let c = 0; c < p; c++)
1605
+ s.push({ x: Ws[c], direction: Hs[c] });
1606
+ }
1607
+ t.setIntersections(s);
1608
+ }
1609
+ const mt = 2, js = 1, Yn = 1.001, hi = 1.1111111111111112, jt = 0.01;
1610
+ function pi(e, n, t, s, i, o, p) {
1611
+ if (t === 0 || s === 0) return;
1612
+ const c = new ri(), r = new Int8Array(t * s);
1613
+ for (let h = 0; h < s; h++) {
1614
+ const l = (h + 0.5) / i - p;
1615
+ ai(n, l, c);
1616
+ for (let a = 0; a < t; a++) {
1617
+ const f = (a + 0.5) / i - o, y = c.filled(f, Zs), _ = (h * t + a) * 3, u = e[_], x = e[_ + 1], M = e[_ + 2], w = tt(u, x, M), v = h * t + a;
1618
+ w === 0.5 ? r[v] = 0 : w > 0.5 !== y ? (e[_] = 1 - u, e[_ + 1] = 1 - x, e[_ + 2] = 1 - M, r[v] = -1) : r[v] = 1;
1619
+ }
1620
+ }
1621
+ for (let h = 0; h < s; h++)
1622
+ for (let l = 0; l < t; l++) {
1623
+ const a = h * t + l;
1624
+ if (r[a] !== 0) continue;
1625
+ let f = 0;
1626
+ if (l > 0 && (f += r[a - 1]), l < t - 1 && (f += r[a + 1]), h > 0 && (f += r[a - t]), h < s - 1 && (f += r[a + t]), f < 0) {
1627
+ const y = (h * t + l) * 3;
1628
+ e[y] = 1 - e[y], e[y + 1] = 1 - e[y + 1], e[y + 2] = 1 - e[y + 2];
1629
+ }
1630
+ }
1631
+ }
1632
+ const Xn = [0, 0];
1633
+ function li(e, n, t, s, i, o, p, c) {
1634
+ const r = new Uint8Array(t * s);
1635
+ fi(r, n, t, s, i, o, p), yi(r, e, t, s, i, c), Mi(r, e, t, s, c, hi), gi(r, e, t, s);
1636
+ }
1637
+ function fi(e, n, t, s, i, o, p) {
1638
+ for (const c of n.contours) {
1639
+ if (c.length === 0) continue;
1640
+ let r = c[c.length - 1];
1641
+ for (const h of c) {
1642
+ const l = r.color & h.color;
1643
+ if (!(l & l - 1)) {
1644
+ h.point(0, Xn);
1645
+ const a = i * (Xn[0] + o), f = i * (Xn[1] + p), y = Math.floor(a - 0.5) | 0, _ = Math.floor(f - 0.5) | 0, u = y + 1, x = _ + 1;
1646
+ y < t && _ < s && u >= 0 && x >= 0 && (y >= 0 && _ >= 0 && (e[_ * t + y] |= mt), u < t && _ >= 0 && (e[_ * t + u] |= mt), y >= 0 && x < s && (e[x * t + y] |= mt), u < t && x < s && (e[x * t + u] |= mt));
1647
+ }
1648
+ r = h;
1649
+ }
1650
+ }
1651
+ }
1652
+ function Qt(e, n, t) {
1653
+ let s = 0;
1654
+ for (let i = 0; i < 3; i++) {
1655
+ const o = e[n + i], p = e[t + i], c = o - p;
1656
+ if (c === 0) continue;
1657
+ const r = (o - 0.5) / c;
1658
+ if (r > 0 && r < 1) {
1659
+ const h = 1 - r, l = h * e[n] + r * e[t], a = h * e[n + 1] + r * e[t + 1], f = h * e[n + 2] + r * e[t + 2], y = i === 0 ? l : i === 1 ? a : f;
1660
+ tt(l, a, f) === y && (s |= i === 0 ? Jt : i === 1 ? tn : nn);
1661
+ }
1662
+ }
1663
+ return s;
1664
+ }
1665
+ function wt(e, n, t, s, i, o) {
1666
+ (o & Jt && t[s] !== i || o & tn && t[s + 1] !== i || o & nn && t[s + 2] !== i) && (e[n] |= mt);
1667
+ }
1668
+ function yi(e, n, t, s, i, o) {
1669
+ const p = Yn / o, c = Yn / o, r = Yn * Math.SQRT2 / o;
1670
+ for (let h = 0; h < s; h++)
1671
+ for (let l = 0; l < t - 1; l++) {
1672
+ const a = (h * t + l) * 3, f = a + 3, y = tt(n[a], n[a + 1], n[a + 2]), _ = tt(n[f], n[f + 1], n[f + 2]);
1673
+ if (Math.abs(y - 0.5) + Math.abs(_ - 0.5) < p) {
1674
+ const u = Qt(n, a, f);
1675
+ wt(e, h * t + l, n, a, y, u), wt(e, h * t + l + 1, n, f, _, u);
1676
+ }
1677
+ }
1678
+ for (let h = 0; h < s - 1; h++)
1679
+ for (let l = 0; l < t; l++) {
1680
+ const a = (h * t + l) * 3, f = ((h + 1) * t + l) * 3, y = tt(n[a], n[a + 1], n[a + 2]), _ = tt(n[f], n[f + 1], n[f + 2]);
1681
+ if (Math.abs(y - 0.5) + Math.abs(_ - 0.5) < c) {
1682
+ const u = Qt(n, a, f);
1683
+ wt(e, h * t + l, n, a, y, u), wt(e, (h + 1) * t + l, n, f, _, u);
1684
+ }
1685
+ }
1686
+ for (let h = 0; h < s - 1; h++)
1687
+ for (let l = 0; l < t - 1; l++) {
1688
+ const a = (h * t + l) * 3, f = (h * t + l + 1) * 3, y = ((h + 1) * t + l) * 3, _ = ((h + 1) * t + l + 1) * 3, u = tt(n[a], n[a + 1], n[a + 2]), x = tt(n[f], n[f + 1], n[f + 2]), M = tt(n[y], n[y + 1], n[y + 2]), w = tt(n[_], n[_ + 1], n[_ + 2]);
1689
+ if (Math.abs(u - 0.5) + Math.abs(w - 0.5) < r) {
1690
+ const v = Qt(n, a, _);
1691
+ wt(e, h * t + l, n, a, u, v), wt(e, (h + 1) * t + l + 1, n, _, w, v);
1692
+ }
1693
+ if (Math.abs(x - 0.5) + Math.abs(M - 0.5) < r) {
1694
+ const v = Qt(n, f, y);
1695
+ wt(e, h * t + l + 1, n, f, x, v), wt(e, (h + 1) * t + l, n, y, M, v);
1696
+ }
1697
+ }
1698
+ }
1699
+ function ui(e, n, t) {
1700
+ const s = n < e ? n : e, i = e < n ? n : e, o = t < i ? t : i;
1701
+ return s < o ? o : s;
1702
+ }
1703
+ function _i(e, n, t, s) {
1704
+ return tt(
1705
+ e[n] + s * (e[t] - e[n]),
1706
+ e[n + 1] + s * (e[t + 1] - e[n + 1]),
1707
+ e[n + 2] + s * (e[t + 2] - e[n + 2])
1708
+ );
1709
+ }
1710
+ function Bn(e, n, t, s) {
1711
+ return ui(
1712
+ Math.fround(s * (s * t[0] + n[0]) + e[0]),
1713
+ Math.fround(s * (s * t[1] + n[1]) + e[1]),
1714
+ Math.fround(s * (s * t[2] + n[2]) + e[2])
1715
+ );
1716
+ }
1717
+ const vt = [0, 0, 0], bt = [0, 0, 0], Wn = [0, 0, 0], ft = [0, 0, 0], Vs = 1, Js = 2;
1718
+ function $n(e, n, t, s, i, o, p, c) {
1719
+ if (s > 0.5 && i > 0.5 && o <= 0.5 || s < 0.5 && i < 0.5 && o >= 0.5 || !c && xi(s, i, o) !== o) {
1720
+ const r = (t - e) * p, h = (n - t) * p;
1721
+ return o >= s - r && o <= s + r && o >= i - h && o <= i + h ? Vs : Vs | Js;
1722
+ }
1723
+ return 0;
1724
+ }
1725
+ function to(e) {
1726
+ return (e & Js) !== 0;
1727
+ }
1728
+ function xi(e, n, t) {
1729
+ return Math.max(Math.min(e, n), Math.min(Math.max(e, n), t));
1730
+ }
1731
+ function $t(e, n, t, s, i, o) {
1732
+ const p = tt(e[t], e[t + 1], e[t + 2]);
1733
+ if (Math.abs(s - 0.5) < Math.abs(p - 0.5)) return !1;
1734
+ for (let c = 0; c < 3; c++) {
1735
+ const r = e[n + c] - e[n + (c + 1) % 3], h = e[t + c] - e[t + (c + 1) % 3], l = r - h;
1736
+ if (l === 0) continue;
1737
+ const a = r / l;
1738
+ if (a > jt && a < 1 - jt) {
1739
+ const f = _i(e, n, t, a), y = $n(0, 1, a, s, p, f, i, o);
1740
+ if (to(y)) return !0;
1741
+ }
1742
+ }
1743
+ return !1;
1744
+ }
1745
+ function zs(e, n, t, s, i, o, p, c, r) {
1746
+ if (!(r > 0 && r < 1)) return 0;
1747
+ const h = r > p ? 0 : r, l = r > p ? r : 1;
1748
+ ft[0] = e[n], ft[1] = e[n + 1], ft[2] = e[n + 2];
1749
+ const a = r > p ? t : Bn(ft, vt, bt, r);
1750
+ ft[0] = e[n], ft[1] = e[n + 1], ft[2] = e[n + 2];
1751
+ const f = r > p ? Bn(ft, vt, bt, r) : s;
1752
+ return $n(h, l, p, a, f, c, i, o);
1753
+ }
1754
+ function Kt(e, n, t, s, i, o, p, c, r) {
1755
+ if (!(p > jt && p < 1 - jt)) return !1;
1756
+ ft[0] = e[n], ft[1] = e[n + 1], ft[2] = e[n + 2];
1757
+ const h = Bn(ft, vt, bt, p);
1758
+ let l = $n(0, 1, p, t, s, h, i, o);
1759
+ return l |= zs(e, n, t, s, i, o, p, h, c), l |= zs(e, n, t, s, i, o, p, h, r), to(l);
1760
+ }
1761
+ function Hn(e, n, t, s, i, o, p, c, r, h, l, a, f) {
1762
+ const y = Math.fround(e[n + h] - e[n + l]), _ = Math.fround(
1763
+ Math.fround(e[t + h] - e[t + l]) + Math.fround(e[s + h] - e[s + l])
1764
+ ), u = Math.fround(e[i + h] - e[i + l]), x = Math.fround(Math.fround(u - _) + y), M = Math.fround(Math.fround(_ - y) - y), w = y;
1765
+ if (x === 0 || Math.abs(M) > 1e12 * Math.abs(x)) {
1766
+ if (M !== 0) {
1767
+ const C = -w / M;
1768
+ if (Kt(e, n, o, p, c, r, C, a, f)) return !0;
1769
+ }
1770
+ return !1;
1771
+ }
1772
+ const v = M * M - 4 * x * w;
1773
+ if (v > 0) {
1774
+ const C = Math.sqrt(v), A = (-M + C) / (2 * x);
1775
+ if (Kt(e, n, o, p, c, r, A, a, f)) return !0;
1776
+ const D = (-M - C) / (2 * x);
1777
+ if (Kt(e, n, o, p, c, r, D, a, f)) return !0;
1778
+ } else if (v === 0) {
1779
+ const C = -M / (2 * x);
1780
+ if (Kt(e, n, o, p, c, r, C, a, f)) return !0;
1781
+ }
1782
+ return !1;
1783
+ }
1784
+ function Zt(e, n, t, s, i, o, p, c) {
1785
+ const r = tt(e[i], e[i + 1], e[i + 2]);
1786
+ if (Math.abs(o - 0.5) < Math.abs(r - 0.5)) return !1;
1787
+ for (let f = 0; f < 3; f++)
1788
+ Wn[f] = Math.fround(
1789
+ Math.fround(e[n + f] - e[t + f]) - e[s + f]
1790
+ ), vt[f] = Math.fround(-e[n + f] - Wn[f]), bt[f] = Math.fround(e[i + f] + Wn[f]);
1791
+ const h = bt[0] !== 0 ? -0.5 * vt[0] / bt[0] : -1, l = bt[1] !== 0 ? -0.5 * vt[1] / bt[1] : -1, a = bt[2] !== 0 ? -0.5 * vt[2] / bt[2] : -1;
1792
+ return !!(Hn(
1793
+ e,
1794
+ n,
1795
+ t,
1796
+ s,
1797
+ i,
1798
+ o,
1799
+ r,
1800
+ p,
1801
+ c,
1802
+ 0,
1803
+ 1,
1804
+ h,
1805
+ l
1806
+ ) || Hn(
1807
+ e,
1808
+ n,
1809
+ t,
1810
+ s,
1811
+ i,
1812
+ o,
1813
+ r,
1814
+ p,
1815
+ c,
1816
+ 1,
1817
+ 2,
1818
+ l,
1819
+ a
1820
+ ) || Hn(
1821
+ e,
1822
+ n,
1823
+ t,
1824
+ s,
1825
+ i,
1826
+ o,
1827
+ r,
1828
+ p,
1829
+ c,
1830
+ 2,
1831
+ 0,
1832
+ a,
1833
+ h
1834
+ ));
1835
+ }
1836
+ function Mi(e, n, t, s, i, o) {
1837
+ const p = o / i, c = o / i, r = o * Math.SQRT2 / i;
1838
+ for (let h = 0; h < s; h++)
1839
+ for (let l = 0; l < t; l++) {
1840
+ const a = h * t + l, f = a * 3, y = tt(n[f], n[f + 1], n[f + 2]), _ = (e[a] & mt) !== 0, u = l > 0, x = l < t - 1, M = h > 0, w = h < s - 1, v = u ? (a - 1) * 3 : -1, C = x ? (a + 1) * 3 : -1, A = M ? (a - t) * 3 : -1, D = w ? (a + t) * 3 : -1;
1841
+ let I = !1;
1842
+ if (u && $t(n, f, v, y, p, _) && (I = !0), !I && M && $t(n, f, A, y, c, _) && (I = !0), !I && x && $t(n, f, C, y, p, _) && (I = !0), !I && w && $t(n, f, D, y, c, _) && (I = !0), !I && u && M) {
1843
+ const F = (a - t - 1) * 3;
1844
+ Zt(n, f, v, A, F, y, r, _) && (I = !0);
1845
+ }
1846
+ if (!I && x && M) {
1847
+ const F = (a - t + 1) * 3;
1848
+ Zt(n, f, C, A, F, y, r, _) && (I = !0);
1849
+ }
1850
+ if (!I && u && w) {
1851
+ const F = (a + t - 1) * 3;
1852
+ Zt(n, f, v, D, F, y, r, _) && (I = !0);
1853
+ }
1854
+ if (!I && x && w) {
1855
+ const F = (a + t + 1) * 3;
1856
+ Zt(n, f, C, D, F, y, r, _) && (I = !0);
1857
+ }
1858
+ I && (e[a] |= js);
1859
+ }
1860
+ }
1861
+ function gi(e, n, t, s) {
1862
+ const i = t * s;
1863
+ for (let o = 0; o < i; o++)
1864
+ if (e[o] & js) {
1865
+ const p = o * 3, c = tt(n[p], n[p + 1], n[p + 2]);
1866
+ n[p] = c, n[p + 1] = c, n[p + 2] = c;
1867
+ }
1868
+ }
1869
+ function tt(e, n, t) {
1870
+ return Math.max(Math.min(e, n), Math.min(Math.max(e, n), t));
1871
+ }
1872
+ function di(e) {
1873
+ let n = 0, t = 0;
1874
+ for (const c of e)
1875
+ n += c.w * c.h, t = Math.max(t, c.w);
1876
+ e.sort((c, r) => r.h - c.h);
1877
+ const i = [{ x: 0, y: 0, w: Math.max(Math.ceil(Math.sqrt(n / 0.95)), t), h: 1 / 0 }];
1878
+ let o = 0, p = 0;
1879
+ for (const c of e)
1880
+ for (let r = i.length - 1; r >= 0; r--) {
1881
+ const h = i[r];
1882
+ if (!(c.w > h.w || c.h > h.h)) {
1883
+ if (c.x = h.x, c.y = h.y, p = Math.max(p, c.y + c.h), o = Math.max(o, c.x + c.w), c.w === h.w && c.h === h.h) {
1884
+ const l = i.pop();
1885
+ l && r < i.length && (i[r] = l);
1886
+ } else c.h === h.h ? (h.x += c.w, h.w -= c.w) : c.w === h.w ? (h.y += c.h, h.h -= c.h) : (i.push({
1887
+ x: h.x + c.w,
1888
+ y: h.y,
1889
+ w: h.w - c.w,
1890
+ h: c.h
1891
+ }), h.y += c.h, h.h -= c.h);
1892
+ break;
1893
+ }
1894
+ }
1895
+ return {
1896
+ w: o,
1897
+ // container width
1898
+ h: p,
1899
+ // container height
1900
+ fill: n / (o * p) || 0
1901
+ // space utilization
1902
+ };
1903
+ }
1904
+ const bi = 3, Ai = 0n, Bs = 4, Qs = 16;
1905
+ function Vn(e) {
1906
+ const n = e < 0 ? 0 : e > 1 ? 1 : e;
1907
+ return ~Math.trunc(255.5 - 255 * n) & 255;
1908
+ }
1909
+ class Ei {
1910
+ constructor(n, t = {}) {
1911
+ this._width = 0, this._height = 0, this._pixels = new Uint8Array(0), this._cache = /* @__PURE__ */ new Map(), this._cacheByGid = /* @__PURE__ */ new Map(), this._font = n, this._pxPerEm = t.pixelsPerEm ?? 40, this._pxrange = t.pxrange ?? 4, this.pxrangeEm = this._pxrange / this._pxPerEm;
1912
+ }
1913
+ /** Packed atlas texture width in texels (chosen by potpack). */
1914
+ get width() {
1915
+ return this._width;
1916
+ }
1917
+ /** Packed atlas texture height in texels (chosen by potpack). */
1918
+ get height() {
1919
+ return this._height;
1920
+ }
1921
+ /** Packed atlas texture, flat RGBA8 bytes, row-major, y-down. */
1922
+ get texture() {
1923
+ return this._pixels;
1924
+ }
1925
+ /**
1926
+ * Batched generate + pack. Generates MSDFs for any codepoints not
1927
+ * already cached, then runs potpack **once** over the whole cache.
1928
+ * Returns the `AtlasGlyph` for each requested codepoint, in order.
1929
+ */
1930
+ glyphs(n) {
1931
+ const t = [];
1932
+ let s = 0;
1933
+ for (const i of n) {
1934
+ const o = this._cache.get(i);
1935
+ if (o) {
1936
+ t.push(o.glyph);
1937
+ continue;
1938
+ }
1939
+ const p = this._generateForGlyphId(this._font.glyphId(i));
1940
+ this._cache.set(i, p), t.push(p.glyph), s++;
1941
+ }
1942
+ return s > 0 && this._repack(), t;
1943
+ }
1944
+ /**
1945
+ * Single-glyph convenience: generates + packs if missing. Repacks the
1946
+ * whole atlas on every new insert — for more than one glyph, use
1947
+ * {@link Atlas.glyphs} instead.
1948
+ */
1949
+ glyph(n) {
1950
+ return this.glyphs([n])[0];
1951
+ }
1952
+ /**
1953
+ * Batched generate + pack, addressed by raw glyph index instead of
1954
+ * Unicode codepoint — the {@link Atlas.glyphs} twin for glyphs with no
1955
+ * reachable cmap entry. Icon fonts (e.g. Lucide) commonly have far more
1956
+ * glyphs than mapped codepoints; this is how the rest of the set gets
1957
+ * generated. Shares one packed atlas texture with the codepoint-addressed
1958
+ * cache — {@link Atlas.texture} always reflects everything requested
1959
+ * through either method. Cached separately from {@link Atlas.glyphs} (a
1960
+ * codepoint and a glyph index are different numbers in the same integer
1961
+ * space — sharing one cache would collide them).
1962
+ */
1963
+ glyphsByIndex(n) {
1964
+ const t = [];
1965
+ let s = 0;
1966
+ for (const i of n) {
1967
+ const o = this._cacheByGid.get(i);
1968
+ if (o) {
1969
+ t.push(o.glyph);
1970
+ continue;
1971
+ }
1972
+ const p = this._generateForGlyphId(i);
1973
+ this._cacheByGid.set(i, p), t.push(p.glyph), s++;
1974
+ }
1975
+ return s > 0 && this._repack(), t;
1976
+ }
1977
+ /** Single-glyph convenience for {@link Atlas.glyphsByIndex} — see its
1978
+ * doc. For more than one glyph, prefer {@link Atlas.glyphsByIndex}
1979
+ * (one potpack for the whole batch instead of one per insert). */
1980
+ glyphByIndex(n) {
1981
+ return this.glyphsByIndex([n])[0];
1982
+ }
1983
+ /**
1984
+ * Generates the MSDF for `glyphId`. Does not cache and does not pack —
1985
+ * callers own the cache key (a codepoint for {@link Atlas.glyphs}, the
1986
+ * glyph ID itself for {@link Atlas.glyphsByIndex}) and the repack.
1987
+ */
1988
+ _generateForGlyphId(n) {
1989
+ const t = this._font, s = t.metrics.unitsPerEm, i = t.advance(n) / s, o = t.shape(n);
1990
+ if (Uo(o, s), o.contours.length === 0)
1991
+ return { glyph: {
1992
+ x: 0,
1993
+ y: 0,
1994
+ w: 0,
1995
+ h: 0,
1996
+ advance: i,
1997
+ planeLeft: 0,
1998
+ planeBottom: 0,
1999
+ planeRight: 0,
2000
+ planeTop: 0
2001
+ }, msdf: null };
2002
+ const p = wi(o), c = this._pxPerEm, r = this._pxrange, h = Math.max(1, Math.ceil((p.maxX - p.minX) * c)) + 2 * r, l = Math.max(1, Math.ceil((p.maxY - p.minY) * c)) + 2 * r, a = r / c - p.minX, f = r / c - p.minY;
2003
+ Ho(o), Jo(o, bi, Ai);
2004
+ const y = new Float32Array(h * l * 3);
2005
+ return si(o, h, l, c, a, f, r, y), pi(y, o, h, l, c, a, f), li(y, o, h, l, c, a, f, r), { glyph: {
2006
+ x: 0,
2007
+ y: 0,
2008
+ w: h,
2009
+ h: l,
2010
+ advance: i,
2011
+ planeLeft: -a,
2012
+ planeBottom: -f,
2013
+ planeRight: h / c - a,
2014
+ planeTop: l / c - f
2015
+ }, msdf: y };
2016
+ }
2017
+ /**
2018
+ * Lays out `text` left-to-right on a single line, applying kerning.
2019
+ * Generates any missing glyphs and packs the atlas **once** for the
2020
+ * whole run. Baseline is y=0 (`penY` on every result is 0); each `penX`
2021
+ * is the pen position in em. `text` must not contain `\n` — use
2022
+ * {@link Atlas.layoutMultiline} for multi-line input.
2023
+ */
2024
+ layout(n) {
2025
+ const t = [];
2026
+ for (const r of n) t.push(r.codePointAt(0));
2027
+ this.glyphs(t);
2028
+ const s = [], i = this._font, o = i.metrics.unitsPerEm;
2029
+ let p = 0, c = -1;
2030
+ for (const r of t) {
2031
+ const h = i.glyphId(r);
2032
+ c >= 0 && (p += i.kerning(c, h) / o);
2033
+ const l = this._cache.get(r).glyph;
2034
+ s.push({ glyph: l, penX: p, penY: 0 }), p += l.advance, c = h;
2035
+ }
2036
+ return { glyphs: s, widthEm: p };
2037
+ }
2038
+ /**
2039
+ * Lays out `text` as one or more `\n`-separated lines, each run through
2040
+ * {@link Atlas.layout} independently (so kerning never crosses a line
2041
+ * break) and stacked with `penY` decreasing by one line height per line
2042
+ * (y-up, matching this codebase's convention — line 0's baseline stays
2043
+ * at y=0). Line height is the font's own metrics:
2044
+ * `(ascender − descender + lineGap) / unitsPerEm`, matching typical
2045
+ * single-spaced typesetting — not a msdfgen concept, original to this
2046
+ * file (msdfgen has no text layout at all).
2047
+ *
2048
+ * `widthEm` is the widest line's; `heightEm` spans from line 0's top to
2049
+ * the last line's bottom, both approximate (ascender/descender-based,
2050
+ * not per-glyph ink bounds) — fine for camera framing, not for tight
2051
+ * cropping.
2052
+ */
2053
+ layoutMultiline(n) {
2054
+ const t = this._font, s = t.metrics.unitsPerEm, i = (t.metrics.ascender - t.metrics.descender + t.metrics.lineGap) / s, o = n.split(`
2055
+ `), p = [];
2056
+ let c = 0;
2057
+ for (let r = 0; r < o.length; r++) {
2058
+ const { glyphs: h, widthEm: l } = this.layout(o[r]), a = -r * i;
2059
+ for (const f of h) p.push({ glyph: f.glyph, penX: f.penX, penY: a });
2060
+ l > c && (c = l);
2061
+ }
2062
+ return { glyphs: p, widthEm: c, heightEm: o.length * i };
2063
+ }
2064
+ /** Repacks every cached glyph (both the codepoint- and glyph-index-keyed
2065
+ * caches — one shared texture) with potpack, reallocates the texture,
2066
+ * and re-blits each MSDF at its assigned rect. Called by `glyphs()`/
2067
+ * `glyphsByIndex()` after any batch of new inserts — once per batch. */
2068
+ _repack() {
2069
+ const n = [];
2070
+ for (const i of this._cache.values())
2071
+ i.msdf !== null && n.push({ w: i.glyph.w, h: i.glyph.h, x: 0, y: 0, entry: i });
2072
+ for (const i of this._cacheByGid.values())
2073
+ i.msdf !== null && n.push({ w: i.glyph.w, h: i.glyph.h, x: 0, y: 0, entry: i });
2074
+ const { w: t, h: s } = di(n);
2075
+ this._width = t, this._height = s, this._pixels = new Uint8Array(t * s * Bs);
2076
+ for (const i of n)
2077
+ i.entry.glyph.x = i.x, i.entry.glyph.y = i.y, this._blit(i.entry.msdf, i.x, i.y, i.w, i.h);
2078
+ }
2079
+ /**
2080
+ * Quantise `msdf` (float, y-up, row 0 = bottom, w×h×3) into `_pixels` at
2081
+ * atlas rect (`ax`, `ay`, `w`, `h`) — the single y-flip point.
2082
+ */
2083
+ _blit(n, t, s, i, o) {
2084
+ const p = this._width, c = this._pixels;
2085
+ for (let r = 0; r < o; r++) {
2086
+ const h = s + (o - 1 - r);
2087
+ for (let l = 0; l < i; l++) {
2088
+ const a = (r * i + l) * 3, f = (h * p + (t + l)) * Bs;
2089
+ c[f] = Vn(n[a]), c[f + 1] = Vn(n[a + 1]), c[f + 2] = Vn(n[a + 2]), c[f + 3] = 255;
2090
+ }
2091
+ }
2092
+ }
2093
+ }
2094
+ const zn = [0, 0];
2095
+ function wi(e) {
2096
+ let n = 1 / 0, t = 1 / 0, s = -1 / 0, i = -1 / 0;
2097
+ for (const o of e.contours)
2098
+ for (const p of o)
2099
+ for (let c = 0; c <= Qs; c++) {
2100
+ p.point(c / Qs, zn);
2101
+ const r = zn[0], h = zn[1];
2102
+ r < n && (n = r), h < t && (t = h), r > s && (s = r), h > i && (i = h);
2103
+ }
2104
+ return { minX: n, minY: t, maxX: s, maxY: i };
2105
+ }
2106
+ export {
2107
+ Ei as Atlas,
2108
+ Ot as CUBIC,
2109
+ et as EdgeSegment,
2110
+ Di as Font,
2111
+ at as LINEAR,
2112
+ it as QUADRATIC,
2113
+ Vn as pixelFloatToByte
2114
+ };