@overpunch/threadtext 0.4.6 → 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.
package/dist/core.js ADDED
@@ -0,0 +1,778 @@
1
+ const Dt = {
2
+ /** Base canvas: the baked satin stitches (transparent ground). */
3
+ bg: "tt-bg",
4
+ /** Overlay canvas: cursor sheen (`mix-blend-mode: screen`). */
5
+ fx: "tt-fx",
6
+ /** The visually hidden real text (the element's own content, or a copy of `text`) for assistive tech. */
7
+ text: "tt-text",
8
+ /** Blinking text caret shown in editable mode. */
9
+ caret: "tt-caret"
10
+ };
11
+ function lt(m, o) {
12
+ let i = Math.imul(m | 0, 374761393) + Math.imul(o | 0, 668265263) | 0;
13
+ return i = Math.imul(i ^ i >>> 13, 1274126177), ((i ^ i >>> 16) >>> 0) / 4294967295;
14
+ }
15
+ function xe(m, o) {
16
+ const i = Math.floor(m), u = Math.floor(o), f = m - i, b = o - u, p = f * f * (3 - 2 * f), w = b * b * (3 - 2 * b), F = lt(i, u), j = lt(i + 1, u), R = lt(i, u + 1), x = lt(i + 1, u + 1);
17
+ return F + (j - F) * p + (R - F) * w + (F - j - R + x) * p * w;
18
+ }
19
+ function Me(m, o, i, u, f) {
20
+ let p = 0;
21
+ u[0] = 0, f[0] = -1e20, f[1] = 1e20;
22
+ for (let w = 1; w < o; w++) {
23
+ let F = (m[w] + w * w - (m[u[p]] + u[p] * u[p])) / (2 * w - 2 * u[p]);
24
+ for (; F <= f[p]; )
25
+ p--, F = (m[w] + w * w - (m[u[p]] + u[p] * u[p])) / (2 * w - 2 * u[p]);
26
+ p++, u[p] = w, f[p] = F, f[p + 1] = 1e20;
27
+ }
28
+ p = 0;
29
+ for (let w = 0; w < o; w++) {
30
+ for (; f[p + 1] < w; ) p++;
31
+ i[w] = (w - u[p]) * (w - u[p]) + m[u[p]];
32
+ }
33
+ }
34
+ function qe(m, o, i) {
35
+ const f = new Float64Array(o * i);
36
+ for (let x = 0; x < o * i; x++) f[x] = m[x] ? 1e20 : 0;
37
+ const b = Math.max(o, i), p = new Float64Array(b), w = new Float64Array(b), F = new Int32Array(b), j = new Float64Array(b + 1);
38
+ for (let x = 0; x < o; x++) {
39
+ for (let M = 0; M < i; M++) p[M] = f[M * o + x];
40
+ Me(p, i, w, F, j);
41
+ for (let M = 0; M < i; M++) f[M * o + x] = w[M];
42
+ }
43
+ for (let x = 0; x < i; x++) {
44
+ for (let M = 0; M < o; M++) p[M] = f[x * o + M];
45
+ Me(p, o, w, F, j);
46
+ for (let M = 0; M < o; M++) f[x * o + M] = w[M];
47
+ }
48
+ const R = new Float32Array(o * i);
49
+ for (let x = 0; x < o * i; x++) R[x] = Math.sqrt(f[x]);
50
+ return R;
51
+ }
52
+ function we(m, o, i, u, f) {
53
+ let b = Float32Array.from(m);
54
+ const p = new Float32Array(o * i), w = 1 / (2 * u + 1), F = (j, R) => j < 0 ? 0 : j > R ? R : j;
55
+ for (let j = 0; j < f; j++) {
56
+ for (let R = 0; R < i; R++) {
57
+ const x = R * o;
58
+ let M = 0;
59
+ for (let z = -u; z <= u; z++) M += b[x + F(z, o - 1)];
60
+ for (let z = 0; z < o; z++)
61
+ p[x + z] = M * w, M += b[x + F(z + u + 1, o - 1)] - b[x + F(z - u, o - 1)];
62
+ }
63
+ for (let R = 0; R < o; R++) {
64
+ let x = 0;
65
+ for (let M = -u; M <= u; M++) x += p[F(M, i - 1) * o + R];
66
+ for (let M = 0; M < i; M++)
67
+ b[M * o + R] = x * w, x += p[F(M + u + 1, i - 1) * o + R] - p[F(M - u, i - 1) * o + R];
68
+ }
69
+ }
70
+ return b;
71
+ }
72
+ function me(m, o) {
73
+ if (!m) return o;
74
+ const i = m.trim();
75
+ if (i[0] === "#") {
76
+ let f = i.slice(1);
77
+ if (f.length === 3 && (f = f[0] + f[0] + f[1] + f[1] + f[2] + f[2]), f.length >= 6) {
78
+ const b = parseInt(f.slice(0, 2), 16), p = parseInt(f.slice(2, 4), 16), w = parseInt(f.slice(4, 6), 16);
79
+ if (!Number.isNaN(b) && !Number.isNaN(p) && !Number.isNaN(w)) return [b, p, w];
80
+ }
81
+ return o;
82
+ }
83
+ const u = i.match(/rgba?\(([^)]+)\)/i);
84
+ if (u) {
85
+ const f = u[1].split(",").map((b) => parseFloat(b));
86
+ if (f.length >= 3 && f.slice(0, 3).every((b) => !Number.isNaN(b)))
87
+ return [Math.round(f[0]), Math.round(f[1]), Math.round(f[2])];
88
+ }
89
+ return o;
90
+ }
91
+ function at(m) {
92
+ return m < 0 ? 0 : m > 255 ? 255 : Math.round(m);
93
+ }
94
+ function ee(m, o, i) {
95
+ return m < o ? o : m > i ? i : m;
96
+ }
97
+ function Ut(m, o) {
98
+ return typeof m == "number" && Number.isFinite(m) ? m : o;
99
+ }
100
+ function ye(m) {
101
+ const o = "Georgia, serif";
102
+ if (typeof m != "string" || !m.trim()) return o;
103
+ try {
104
+ const i = document.createElement("canvas").getContext("2d");
105
+ if (!i) return m;
106
+ if (i.font = "13px serif", i.font = `100px ${m}`, /13px/.test(i.font))
107
+ return console.warn(`[threadText] the font ${JSON.stringify(m)} isn't a valid font-family list; using ${o}`), o;
108
+ } catch {
109
+ }
110
+ return m;
111
+ }
112
+ const ze = "position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0";
113
+ function Xe(m) {
114
+ return (m.split(",")[0] || "serif").trim().replace(/^["']|["']$/g, "");
115
+ }
116
+ function pe(m, o, i) {
117
+ const u = new Uint8Array(o * i);
118
+ for (let A = 0; A < i; A++) for (let L = 0; L < o; L++) {
119
+ const T = A * o + L, X = m[T * 4 + 3] / 255, K = xe(L * 0.5, A * 0.5) * 0.22 + xe(L * 1.8, A * 1.8) * 0.1;
120
+ u[T] = X > 0.5 + (K - 0.16) ? 1 : 0;
121
+ }
122
+ const f = qe(u, o, i), b = new Float32Array(o * i);
123
+ for (let A = 1; A < i - 1; A++) for (let L = 1; L < o - 1; L++) {
124
+ const T = A * o + L;
125
+ b[T] = (f[T] * 4 + f[T - 1] + f[T + 1] + f[T - o] + f[T + o]) / 8;
126
+ }
127
+ const p = new Float32Array(o * i), w = new Float32Array(o * i), F = new Float32Array(o * i), j = new Float32Array(o * i);
128
+ for (let A = 1; A < i - 1; A++) for (let L = 1; L < o - 1; L++) {
129
+ const T = A * o + L;
130
+ if (!u[T]) continue;
131
+ const X = (b[T + 1] - b[T - 1]) * 0.5, K = (b[T + o] - b[T - o]) * 0.5;
132
+ F[T] = X, j[T] = K, p[T] = X * X - K * K, w[T] = 2 * X * K;
133
+ }
134
+ const R = Math.max(7, Math.round(i * 0.05)), x = we(p, o, i, R, 2), M = we(w, o, i, R, 2), z = new Float32Array(o * i), q = new Float32Array(o * i);
135
+ for (let A = 0; A < o * i; A++) {
136
+ if (!u[A]) continue;
137
+ const L = 0.5 * Math.atan2(M[A], x[A]);
138
+ z[A] = Math.cos(L), q[A] = Math.sin(L);
139
+ }
140
+ const Ct = Math.max(9, i * 0.04), yt = new Float32Array(o * i), Gt = -0.45, W = -0.62, vt = 0.64;
141
+ for (let A = 1; A < i - 1; A++) for (let L = 1; L < o - 1; L++) {
142
+ const T = A * o + L;
143
+ if (!u[T]) continue;
144
+ let X = F[T], K = j[T];
145
+ const bt = Math.hypot(X, K) || 1;
146
+ X /= bt, K /= bt;
147
+ const zt = b[T], pt = (1 - Math.min(zt / Ct, 1)) * 1.25;
148
+ let B = -X * pt, J = -K * pt;
149
+ const ot = 1.5, Lt = Math.hypot(B, J, ot);
150
+ B /= Lt, J /= Lt;
151
+ let rt = B * Gt + J * W + ot / Lt * vt;
152
+ rt < 0 && (rt = 0);
153
+ const it = Math.min(1, 0.45 + 0.55 * Math.min(zt / (Ct * 0.7), 1));
154
+ yt[T] = Math.min(1.12, (0.5 + 0.75 * rt) * it);
155
+ }
156
+ return { mask: u, ex: z, ey: q, shade: yt };
157
+ }
158
+ function dn(m, o) {
159
+ var te, Ue, Ge;
160
+ if (typeof window > "u" || typeof document > "u" || !m)
161
+ return { setText() {
162
+ }, replay() {
163
+ }, resize() {
164
+ }, update() {
165
+ }, focus() {
166
+ }, destroy() {
167
+ }, get text() {
168
+ return o.text ?? "";
169
+ } };
170
+ let i = o.text ?? "", u = ye(o.font), f = ee(Ut(o.weight, 680), 1, 1e3), b = ee(Ut(o.fill, 0.9), 0.05, 1), p = o.align === "left" || o.align === "right" ? o.align : "center";
171
+ const w = (t, e) => {
172
+ const n = Ut(t, e);
173
+ return n >= 1 ? Math.min(n, 1e5) : e;
174
+ };
175
+ let F = w(o.sewRate, 110), j = o.sewStyle === "hand" ? "hand" : "machine";
176
+ const R = ["satin", "cross", "chain", "running"];
177
+ let x = R.includes(o.stitchMode ?? "") ? o.stitchMode : "satin", M = o.sheen ?? !0, z = o.animate ?? !0, q = o.editable ?? !1;
178
+ const Ct = (t) => typeof t == "number" && Number.isFinite(t) && t >= 1 && t <= 200 ? t : void 0;
179
+ let yt = Ct(o.pitch);
180
+ const Gt = ["solid", "twotone", "gradient"];
181
+ let W = Gt.includes(o.colorMode ?? "") ? o.colorMode : "solid", vt = o.threadColor ?? "#fffbf3", A = o.threadColor2 ?? vt, L = o.backstitch ?? !1, T = o.outlineColor, X = o.axes, K = o.onTextChange, bt = Xe(u);
182
+ function zt() {
183
+ if (!X) return "";
184
+ const t = [];
185
+ for (const e in X) {
186
+ const n = X[e];
187
+ typeof n == "number" && isFinite(n) && t.push(`"${e}" ${n}`);
188
+ }
189
+ return t.join(", ");
190
+ }
191
+ function pt(t) {
192
+ "fontVariationSettings" in t && (t.fontVariationSettings = zt() || "normal");
193
+ }
194
+ const B = o.reducedMotion === void 0 && window.matchMedia ? window.matchMedia("(prefers-reduced-motion: reduce)") : null;
195
+ let J = o.reducedMotion ?? !!(B != null && B.matches);
196
+ const ot = (t) => ({
197
+ peak: t,
198
+ mid: [t[0] * 0.585, t[1] * 0.585, t[2] * 0.585],
199
+ end: [t[0] * 0.47, t[1] * 0.47, t[2] * 0.47]
200
+ }), Lt = (t, e, n) => [t[0] + (e[0] - t[0]) * n, t[1] + (e[1] - t[1]) * n, t[2] + (e[2] - t[2]) * n], rt = 6;
201
+ let it = [255, 251, 243], ct = [], ne = ot([0, 0, 0]);
202
+ function Ce() {
203
+ const t = me(vt, [255, 251, 243]), e = me(A, t);
204
+ if (W === "twotone") ct = [ot(t), ot(e)];
205
+ else if (W === "gradient") {
206
+ ct = [];
207
+ for (let n = 0; n < rt; n++) ct.push(ot(Lt(t, e, n / (rt - 1))));
208
+ } else ct = [ot(t)];
209
+ it = ct[0].peak, ne = ot(T ? me(T, [0, 0, 0]) : [t[0] * 0.32, t[1] * 0.32, t[2] * 0.32]);
210
+ }
211
+ Ce();
212
+ const Ft = [];
213
+ let O, C, _ = null, U = null;
214
+ if (m instanceof HTMLCanvasElement)
215
+ O = m, C = m.parentElement ?? m;
216
+ else {
217
+ C = m;
218
+ const t = (n) => n.replace(/\s+/g, " ").trim(), e = Array.from(C.childNodes);
219
+ if (e.some((n) => n.nodeType === 1 || (n.textContent ?? "").trim())) {
220
+ _ = document.createElement("span"), _.className = Dt.text, _.style.cssText = ze;
221
+ for (const n of e) _.appendChild(n);
222
+ C.appendChild(_), t(i) && t(_.textContent ?? "") !== t(i) && (_.style.display = "none");
223
+ }
224
+ (!_ || _.style.display === "none") && (U = document.createElement("span"), U.className = Dt.text, U.style.cssText = ze, U.textContent = i, C.appendChild(U)), O = document.createElement("canvas"), Ft.push(O), C.appendChild(O);
225
+ }
226
+ const Rt = { height: C.style.height, position: C.style.position, hadStyle: C.hasAttribute("style") };
227
+ O.setAttribute("aria-hidden", "true"), O.classList.add(Dt.bg);
228
+ const Y = document.createElement("canvas");
229
+ Ft.push(Y), Y.classList.add(Dt.fx), Y.setAttribute("aria-hidden", "true"), C.appendChild(Y), getComputedStyle(C).position === "static" && (C.style.position = "relative"), O.style.display = "block", O.style.width = "100%", Object.assign(Y.style, { position: "absolute", top: "0", left: "0", width: "100%", pointerEvents: "none", mixBlendMode: "screen" });
230
+ let E = null, N = null, St = !1, S = 0, I = 0, xt = 0, oe = 0, tt = 4, It = [], Et = 0, Tt = 0, re = [], ve = 0, be = -1, Se = "", Ee = -1, Te = -1, Z = new Uint8Array(0), Xt = new Float32Array(0), ie = new Float32Array(0), se = new Float32Array(0), ft = null, At = null, qt = null, $t = [], dt = [], Ot = [], Mt = 3;
231
+ const $ = { rows: [], idx: 0, acc: 0, rate: 0, on: !1 }, et = { x: 0, y: 0, set: !1 };
232
+ let ut = !0, ae = 0, Nt = !1, Pt = 0, le = -1, D = !1, Q = !1, nt = null, Bt = "", Kt = !1, ce = 0, fe = !1, H;
233
+ function Ke() {
234
+ const t = C.getBoundingClientRect(), e = Math.max(1, Math.round(t.width || Math.min(window.innerWidth * 0.95, 1240))), n = i || "Ag", r = O.getContext("2d");
235
+ r && (r.textBaseline = "middle", r.direction = Le());
236
+ let a = 100;
237
+ r && (r.font = `${f} 100px ${u}`, pt(r), a = r.measureText(n).width || 100);
238
+ const s = Math.max(8, 100 * (e * b) / a);
239
+ let l = s * 0.62, v = s * 0.62, c = e * b;
240
+ if (r) {
241
+ r.font = `${f} ${s}px ${u}`, pt(r);
242
+ const y = r.measureText(n);
243
+ c = y.width || c, y.actualBoundingBoxAscent > 0 && (l = y.actualBoundingBoxAscent), y.actualBoundingBoxDescent > 0 && (v = y.actualBoundingBoxDescent);
244
+ }
245
+ const h = Math.max(1, Math.round(2 * Math.max(l, v) * 1.1));
246
+ let g = Math.min(window.devicePixelRatio || 1, 1.7);
247
+ for (; e * g * h * g > 9e5 && g > 0.4; ) g -= 0.1;
248
+ S = Math.max(1, Math.round(e * g)), I = Math.max(1, Math.round(h * g)), xt = Math.max(4, Math.round(s * g));
249
+ const d = c * g, k = Math.max(2, Math.round(xt * 0.08));
250
+ oe = Math.round(p === "left" ? k : p === "right" ? S - d - k : S / 2 - d / 2);
251
+ for (const y of [O, Y])
252
+ y.width = S, y.height = I, y.style.height = h + "px";
253
+ C.style.height = h + "px", Y.style.left = O.offsetLeft + "px", Y.style.top = O.offsetTop + "px", Y.style.width = (O.offsetWidth || O.getBoundingClientRect().width) + "px", tt = yt ?? Math.max(3, I * 95e-4);
254
+ }
255
+ function Ve() {
256
+ At && Ee === S && Te === I || (At = document.createElement("canvas"), At.width = S, At.height = I, qt = At.getContext("2d", { willReadFrequently: !0 }), Ee = S, Te = I);
257
+ }
258
+ function Ae() {
259
+ return ct.map((t) => t.peak.join(",")).join(";") + "|" + x + "|" + W + (L ? "|bs" + ne.peak.join(",") : "");
260
+ }
261
+ function ke(t, e) {
262
+ const n = Math.max(7, tt * 2.5), r = Math.max(3.2, tt * 1.3), a = [];
263
+ for (let s = 0; s < 20; s++) {
264
+ const l = 0.34 + s / 19 * 0.82, v = document.createElement("canvas");
265
+ v.width = Et, v.height = Tt;
266
+ const c = v.getContext("2d");
267
+ if (!c) {
268
+ a.push(v);
269
+ continue;
270
+ }
271
+ const h = Et / 2, g = Tt / 2, d = (y, P) => `rgba(${at(y[0] * l)},${at(y[1] * l)},${at(y[2] * l)},${P})`, k = (y, P, gt, st) => {
272
+ c.save(), c.translate(h, g), c.rotate(gt);
273
+ const V = c.createLinearGradient(0, -P / 2, 0, P / 2);
274
+ V.addColorStop(0, d(t.end, 0)), V.addColorStop(0.16, d(t.mid, 1)), V.addColorStop(0.5, d(t.peak, 1)), V.addColorStop(0.84, d(t.mid, 1)), V.addColorStop(1, d(t.end, 0)), c.fillStyle = V;
275
+ const G = P / 2;
276
+ if (c.beginPath(), c.moveTo(-y / 2 + G, -G), c.lineTo(y / 2 - G, -G), c.arc(y / 2 - G, 0, G, -Math.PI / 2, Math.PI / 2), c.lineTo(-y / 2 + G, G), c.arc(-y / 2 + G, 0, G, Math.PI / 2, -Math.PI / 2), c.closePath(), c.fill(), st) {
277
+ c.globalCompositeOperation = "destination-in";
278
+ const mt = c.createLinearGradient(-y / 2, 0, y / 2, 0);
279
+ mt.addColorStop(0, "rgba(0,0,0,0)"), mt.addColorStop(0.22, "rgba(0,0,0,1)"), mt.addColorStop(0.78, "rgba(0,0,0,1)"), mt.addColorStop(1, "rgba(0,0,0,0)"), c.fillStyle = mt, c.fillRect(-y / 2 - 1, -P / 2 - 1, y + 2, P + 2), c.globalCompositeOperation = "source-over";
280
+ }
281
+ c.restore();
282
+ };
283
+ if (e === "cross")
284
+ k(n * 0.92, r * 0.82, Math.PI / 4, !1), k(n * 0.92, r * 0.82, -Math.PI / 4, !1);
285
+ else if (e === "chain") {
286
+ c.save(), c.translate(h, g);
287
+ const y = c.createLinearGradient(0, -r, 0, r);
288
+ y.addColorStop(0, d(t.mid, 1)), y.addColorStop(0.5, d(t.peak, 1)), y.addColorStop(1, d(t.mid, 1)), c.strokeStyle = y, c.lineWidth = Math.max(1.6, r * 0.7), c.lineCap = "round", c.beginPath(), c.ellipse(0, 0, n * 0.34, r * 1.05, 0, 0, Math.PI * 2), c.stroke(), c.restore();
289
+ } else e === "running" ? k(n * 0.5, r, 0, !1) : k(n, r, 0, !0);
290
+ a.push(v);
291
+ }
292
+ return a;
293
+ }
294
+ function de() {
295
+ const t = Math.max(7, tt * 2.5), e = Math.max(3.2, tt * 1.3);
296
+ x === "cross" || x === "chain" ? Et = Tt = Math.ceil(t) + 2 : (Et = Math.ceil(t) + 2, Tt = Math.ceil(e) + 2), ve = x === "chain" || x === "running" ? Math.PI / 2 : 0, It = [];
297
+ for (const n of ct) for (const r of ke(n, x)) It.push(r);
298
+ re = L ? ke(ne, "running") : [], be = tt, Se = Ae();
299
+ }
300
+ function Ye() {
301
+ It.length && be === tt && Se === Ae() || de();
302
+ }
303
+ function Le() {
304
+ try {
305
+ return getComputedStyle(C).direction === "rtl" ? "rtl" : "ltr";
306
+ } catch {
307
+ return "ltr";
308
+ }
309
+ }
310
+ function Fe() {
311
+ if (!qt) return null;
312
+ const t = qt;
313
+ return t.clearRect(0, 0, S, I), t.fillStyle = "#fff", t.textAlign = "left", t.textBaseline = "middle", t.direction = Le(), t.font = `${f} ${xt}px ${u}`, pt(t), t.fillText(i, oe, I * 0.5), t.getImageData(0, 0, S, I).data;
314
+ }
315
+ function ue(t) {
316
+ Z = t.mask, ie = t.ex, se = t.ey, Xt = t.shade, ft = document.createElement("canvas"), ft.width = S, ft.height = I;
317
+ const e = ft.getContext("2d");
318
+ if (e) {
319
+ const n = e.createImageData(S, I);
320
+ for (let r = 0; r < S * I; r++) Z[r] && (n.data[r * 4] = 255, n.data[r * 4 + 1] = 255, n.data[r * 4 + 2] = 255, n.data[r * 4 + 3] = 255);
321
+ e.putImageData(n, 0, 0);
322
+ }
323
+ }
324
+ function Re() {
325
+ const t = Math.max(1.6, tt * 0.6);
326
+ Mt = t;
327
+ const e = [], n = [];
328
+ let r = 1 / 0, a = -1 / 0;
329
+ for (let s = 0; s < I; s += t) for (let l = 0; l < S; l += t) {
330
+ const v = (lt(Math.floor(l), Math.floor(s)) - 0.5) * t * 0.9, c = (lt(Math.floor(s), Math.floor(l)) - 0.5) * t * 0.9, h = Math.round(l + v), g = Math.round(s + c);
331
+ if (h < 1 || g < 1 || h >= S - 1 || g >= I - 1) continue;
332
+ const d = g * S + h;
333
+ if (!Z[d]) continue;
334
+ const k = Math.atan2(se[d], ie[d]), y = 0.86 + lt(h * 7, g * 13) * 0.18;
335
+ let P = Xt[d] * y;
336
+ P = P < 0 ? 0 : P > 1.12 ? 1.12 : P, e.push({ x: h, y: g, ang: k, idx: Math.min(19, Math.max(0, Math.round(P / 1.12 * 19))) }), h < r && (r = h), h > a && (a = h);
337
+ }
338
+ if (W === "twotone")
339
+ if (x === "satin") {
340
+ const s = Math.max(4, tt * 1.6);
341
+ for (const l of e) {
342
+ const v = -Math.sin(l.ang) * l.x + Math.cos(l.ang) * l.y;
343
+ l.pal = Math.floor(v / s) & 1;
344
+ }
345
+ } else
346
+ for (const s of e) s.pal = Math.round(s.x / t) + Math.round(s.y / t) & 1;
347
+ else if (W === "gradient") {
348
+ const s = Math.max(1, a - r);
349
+ for (const l of e) {
350
+ const v = (l.x - r) / s * (rt - 1), c = Math.min(rt - 1, Math.max(0, Math.floor(v)));
351
+ l.pal = c, c < rt - 1 && lt(l.x * 3 + 1, l.y * 5 + 2) < v - c && n.push({ x: l.x, y: l.y, ang: l.ang, idx: l.idx, pal: c + 1 });
352
+ }
353
+ }
354
+ $t = e, Ot = n, Je();
355
+ }
356
+ function Je() {
357
+ if (dt = [], !L || !Z.length) return;
358
+ const t = Math.max(3, tt * 1.5), e = Math.max(1, Math.ceil(S / t)), n = /* @__PURE__ */ new Set(), r = (a, s) => Z[s * S + a] ? 1 : 0;
359
+ for (let a = 1; a < I - 1; a++) for (let s = 1; s < S - 1; s++) {
360
+ const l = a * S + s;
361
+ if (!Z[l] || Z[l - 1] && Z[l + 1] && Z[l - S] && Z[l + S]) continue;
362
+ const v = Math.floor(a / t) * e + Math.floor(s / t);
363
+ if (n.has(v)) continue;
364
+ n.add(v);
365
+ const c = r(s + 1, a - 1) + 2 * r(s + 1, a) + r(s + 1, a + 1) - (r(s - 1, a - 1) + 2 * r(s - 1, a) + r(s - 1, a + 1)), h = r(s - 1, a + 1) + 2 * r(s, a + 1) + r(s + 1, a + 1) - (r(s - 1, a - 1) + 2 * r(s, a - 1) + r(s + 1, a - 1)), g = Math.atan2(c, -h);
366
+ let d = Xt[l];
367
+ d = d < 0 ? 0 : d > 1.12 ? 1.12 : d, dt.push({ x: s, y: a, ang: g, idx: Math.min(19, Math.max(8, Math.round(d / 1.12 * 19))), outline: 1 });
368
+ }
369
+ }
370
+ function Vt(t, e) {
371
+ t.save(), t.translate(e.x, e.y), e.outline ? (t.rotate(e.ang), t.drawImage(re[e.idx], -Et / 2, -Tt / 2)) : (t.rotate(e.ang + ve), t.drawImage(It[(e.pal || 0) * 20 + e.idx], -Et / 2, -Tt / 2)), t.restore();
372
+ }
373
+ function jt() {
374
+ const t = O.getContext("2d");
375
+ if (t) {
376
+ for (const e of $t) Vt(t, e);
377
+ for (const e of Ot) Vt(t, e);
378
+ for (const e of dt) Vt(t, e);
379
+ }
380
+ }
381
+ function _t() {
382
+ const t = O.getContext("2d");
383
+ t && t.clearRect(0, 0, S, I);
384
+ }
385
+ function Ie(t) {
386
+ if (!t.length) return [];
387
+ const e = Math.max(4, Mt * 1.5), n = /* @__PURE__ */ new Map(), r = (h, g) => h + "," + g;
388
+ t.forEach((h, g) => {
389
+ const d = r(Math.floor(h.x / e), Math.floor(h.y / e));
390
+ let k = n.get(d);
391
+ k || n.set(d, k = []), k.push(g);
392
+ });
393
+ const a = Mt * 1.8 * (Mt * 1.8);
394
+ function s(h) {
395
+ const g = t[h], d = Math.floor(g.x / e), k = Math.floor(g.y / e), y = [];
396
+ for (let P = -1; P <= 1; P++) for (let gt = -1; gt <= 1; gt++) {
397
+ const st = n.get(r(d + P, k + gt));
398
+ if (st)
399
+ for (const V of st) {
400
+ if (V === h) continue;
401
+ const G = t[V].x - g.x, mt = t[V].y - g.y;
402
+ G * G + mt * mt <= a && y.push(V);
403
+ }
404
+ }
405
+ return y;
406
+ }
407
+ const l = new Uint8Array(t.length), v = [], c = t.map((h, g) => g).sort((h, g) => t[h].x + t[h].y - (t[g].x + t[g].y));
408
+ for (const h of c) {
409
+ if (l[h]) continue;
410
+ let g = [h];
411
+ for (l[h] = 1; g.length; ) {
412
+ v.push(g.map((k) => t[k]));
413
+ const d = [];
414
+ for (const k of g) for (const y of s(k)) l[y] || (l[y] = 1, d.push(y));
415
+ g = d;
416
+ }
417
+ }
418
+ return v;
419
+ }
420
+ function Ze(t) {
421
+ const e = Math.max(4, Mt * 1.5), n = /* @__PURE__ */ new Map(), r = (c, h) => c + "," + h;
422
+ t.forEach((c, h) => {
423
+ const g = r(Math.floor(c.x / e), Math.floor(c.y / e));
424
+ let d = n.get(g);
425
+ d || n.set(g, d = []), d.push(h);
426
+ });
427
+ const a = Mt * 1.8 * (Mt * 1.8), s = (c) => {
428
+ const h = t[c], g = Math.floor(h.x / e), d = Math.floor(h.y / e), k = [];
429
+ for (let y = -1; y <= 1; y++) for (let P = -1; P <= 1; P++) {
430
+ const gt = n.get(r(g + y, d + P));
431
+ if (gt)
432
+ for (const st of gt) {
433
+ if (st === c) continue;
434
+ const V = t[st].x - h.x, G = t[st].y - h.y;
435
+ V * V + G * G <= a && k.push(st);
436
+ }
437
+ }
438
+ return k;
439
+ }, l = new Uint8Array(t.length), v = [];
440
+ for (let c = 0; c < t.length; c++) {
441
+ if (l[c]) continue;
442
+ const h = [];
443
+ let g = [c];
444
+ for (l[c] = 1; g.length; ) {
445
+ const d = [];
446
+ for (const k of g) {
447
+ h.push(t[k]);
448
+ for (const y of s(k)) l[y] || (l[y] = 1, d.push(y));
449
+ }
450
+ g = d;
451
+ }
452
+ v.push(h);
453
+ }
454
+ return v;
455
+ }
456
+ function Qe(t) {
457
+ if (!t.length) return [];
458
+ const e = (s) => {
459
+ let l = 1 / 0;
460
+ for (const v of s) v.x < l && (l = v.x);
461
+ return l;
462
+ }, n = (s) => {
463
+ let l = 0;
464
+ for (const v of s) l += v.y;
465
+ return l / s.length;
466
+ }, r = Ze(t).sort((s, l) => e(s) - e(l)), a = [];
467
+ for (const s of r) {
468
+ const l = Ie(s);
469
+ let v = 1;
470
+ for (const d of l) d.length > v && (v = d.length);
471
+ const c = Math.max(2, v * 0.45), h = l.filter((d) => d.length >= c).sort((d, k) => n(d) - n(k)), g = l.filter((d) => d.length < c).sort((d, k) => n(d) - n(k));
472
+ for (const d of h) a.push(d);
473
+ for (const d of g) a.push(d);
474
+ }
475
+ return a;
476
+ }
477
+ function He() {
478
+ if (_t(), J || !z) {
479
+ jt(), $.on = !1;
480
+ return;
481
+ }
482
+ const t = j === "hand" ? Qe : Ie;
483
+ let e;
484
+ if (W === "gradient" && ct.length > 1) {
485
+ e = [];
486
+ const n = /* @__PURE__ */ new Map();
487
+ for (const r of $t) {
488
+ const a = r.pal || 0, s = n.get(a);
489
+ s ? s.push(r) : n.set(a, [r]);
490
+ }
491
+ for (const r of [...n.keys()].sort((a, s) => a - s)) for (const a of t(n.get(r))) e.push(a);
492
+ } else
493
+ e = t($t);
494
+ if (Ot.length) for (const n of t(Ot)) e.push(n);
495
+ if (dt.length) {
496
+ const r = Math.ceil(dt.length / 24);
497
+ for (let a = 0; a < dt.length; a += r) e.push(dt.slice(a, a + r));
498
+ }
499
+ $.rows = e, $.idx = 0, $.acc = 0, $.rate = F, $.on = $.rows.length > 0;
500
+ }
501
+ function We(t) {
502
+ const e = O.getContext("2d");
503
+ if (e)
504
+ for (t > $.rows.length && (t = $.rows.length); $.idx < t; ) {
505
+ const n = $.rows[$.idx++];
506
+ for (const r of n) Vt(e, r);
507
+ }
508
+ }
509
+ function tn(t, e) {
510
+ const n = Y.getContext("2d");
511
+ if (!n || (n.clearRect(0, 0, S, I), !M || !ft)) return;
512
+ const r = S * 0.32, a = n.createRadialGradient(t, e, 0, t, e, r);
513
+ a.addColorStop(0, "rgba(255,250,238,0.18)"), a.addColorStop(0.5, "rgba(255,250,238,0.055)"), a.addColorStop(1, "rgba(255,250,238,0)"), n.fillStyle = a, n.fillRect(0, 0, S, I), n.globalCompositeOperation = "destination-in", n.drawImage(ft, 0, 0), n.globalCompositeOperation = "source-over";
514
+ }
515
+ function en() {
516
+ N || !q || (N = document.createElement("span"), N.className = Dt.caret, N.setAttribute("aria-hidden", "true"), Object.assign(N.style, { position: "absolute", pointerEvents: "none", borderRadius: "1px", opacity: "0", background: `rgb(${at(it[0])},${at(it[1])},${at(it[2])})` }), C.appendChild(N), Ft.push(N));
517
+ }
518
+ function Yt() {
519
+ if (!q || !N) return;
520
+ const t = O.getContext("2d");
521
+ if (!t) return;
522
+ t.font = `${f} ${xt}px ${u}`, pt(t);
523
+ const e = t.measureText(i).width, n = (O.clientWidth || S) / S, r = xt * 0.7;
524
+ N.style.left = (oe + e + xt * 0.05) * n + "px", N.style.top = 0.5 * (O.clientHeight || I) - r * n / 2 + "px", N.style.height = r * n + "px", N.style.width = Math.max(2, Math.round(xt * 0.02 * n)) + "px", N.style.background = `rgb(${at(it[0])},${at(it[1])},${at(it[2])})`;
525
+ }
526
+ function ht() {
527
+ D || Nt || (Nt = !0, Pt = 0, ae = requestAnimationFrame($e));
528
+ }
529
+ function $e(t) {
530
+ if (D) {
531
+ Nt = !1;
532
+ return;
533
+ }
534
+ Pt || (Pt = t);
535
+ const e = Math.min(0.25, (t - Pt) / 1e3);
536
+ Pt = t, $.on && ($.acc += $.rate * e, We($.acc), $.idx >= $.rows.length && ($.on = !1)), ut && ft && (tn(et.set ? et.x : S * 0.5, et.set ? et.y : I * 0.45), ut = !1);
537
+ const n = q && St && !J;
538
+ if (N && (N.style.opacity = q && St && (J || Math.floor(t / 530) % 2 === 0) ? "1" : "0"), !$.on && !ut && !n) {
539
+ Nt = !1;
540
+ return;
541
+ }
542
+ ae = requestAnimationFrame($e);
543
+ }
544
+ function Jt(t) {
545
+ if (J) return;
546
+ const e = C.getBoundingClientRect();
547
+ et.x = (t.clientX - e.left) / (e.width || 1) * S, et.y = (t.clientY - e.top) / (e.height || 1) * I, et.set = !0, ut = !0, ht();
548
+ }
549
+ function Zt() {
550
+ J || (et.set = !1, ut = !0, ht());
551
+ }
552
+ function nn() {
553
+ E && Be(E.value, !0);
554
+ }
555
+ function on(t) {
556
+ t.key === "Enter" && (t.preventDefault(), Wt.replay());
557
+ }
558
+ function rn() {
559
+ St = !0, ht(), Oe();
560
+ }
561
+ function Oe() {
562
+ J && N && q && St && (N.style.opacity = "1");
563
+ }
564
+ function sn() {
565
+ St = !1, N && (N.style.opacity = "0");
566
+ }
567
+ function Ne(t) {
568
+ if (!(t === q && E))
569
+ if (q = t, t) {
570
+ E || (E = document.createElement("input"), E.type = "text", E.value = i, E.maxLength = 64, E.setAttribute("aria-label", "Embroidered text — type to change it"), E.setAttribute("autocomplete", "off"), E.setAttribute("autocapitalize", "off"), E.spellcheck = !1, Object.assign(E.style, { position: "absolute", inset: "0", width: "100%", height: "100%", margin: "0", padding: "0", border: "0", outline: "none", background: "transparent", color: "transparent", caretColor: "transparent", font: "inherit", textAlign: "center", cursor: "text" }), E.addEventListener("input", nn), E.addEventListener("keydown", on), E.addEventListener("focus", rn), E.addEventListener("blur", sn), C.appendChild(E), Ft.push(E)), en(), Yt();
571
+ for (const e of [_, U]) e == null || e.setAttribute("aria-hidden", "true");
572
+ } else {
573
+ for (const e of [_, U]) e == null || e.removeAttribute("aria-hidden");
574
+ E && (E.remove(), E = null), St = !1, N && (N.style.opacity = "0");
575
+ }
576
+ }
577
+ function an(t) {
578
+ if (t !== M)
579
+ if (M = t, t)
580
+ C.addEventListener("pointermove", Jt, { passive: !0 }), C.addEventListener("pointerleave", Zt, { passive: !0 }), et.set = !1, ut = !0, ht();
581
+ else {
582
+ C.removeEventListener("pointermove", Jt), C.removeEventListener("pointerleave", Zt);
583
+ const e = Y.getContext("2d");
584
+ e && e.clearRect(0, 0, S, I);
585
+ }
586
+ }
587
+ function Qt(t) {
588
+ Re(), t ? He() : (_t(), jt(), $.on = !1), E && E.value !== i && (E.value = i), Yt(), ut = !0, ht();
589
+ }
590
+ function wt(t) {
591
+ if (!i.trim() && !q) {
592
+ for (const n of [O, Y])
593
+ n.width = 0, n.height = 0, n.style.height = "0px";
594
+ C.style.height = Rt.height;
595
+ return;
596
+ }
597
+ Ke(), Ve(), Ye();
598
+ const e = Fe();
599
+ if (!e) {
600
+ Qt(t);
601
+ return;
602
+ }
603
+ if (nt && Kt) {
604
+ const n = ++ce;
605
+ fe = t, H && clearTimeout(H), H = setTimeout(() => Pe(n), 800);
606
+ try {
607
+ nt.postMessage({ id: n, W: S, H: I, alpha: e }, [e.buffer]);
608
+ } catch {
609
+ Pe(n);
610
+ }
611
+ return;
612
+ }
613
+ ue(pe(e, S, I)), Qt(t);
614
+ }
615
+ function Pe(t) {
616
+ if (t !== ce || D) return;
617
+ H && (clearTimeout(H), H = void 0), Kt = !1;
618
+ const e = Fe();
619
+ e && (ue(pe(e, S, I)), Qt(fe));
620
+ }
621
+ function Be(t, e) {
622
+ t = t ?? "", U && (U.textContent = t), !(t === i && Q) && (i = t, Q && (wt(!1), e && K && K(i)));
623
+ }
624
+ const Ht = /* @__PURE__ */ new Set();
625
+ function je(t, e, n) {
626
+ const r = `${e} 200px ${t}`;
627
+ let a = !1;
628
+ try {
629
+ a = !!document.fonts && document.fonts.check(r);
630
+ } catch {
631
+ a = !0;
632
+ }
633
+ if (!document.fonts || a) {
634
+ n();
635
+ return;
636
+ }
637
+ let s = !1, l;
638
+ const v = () => {
639
+ s || (s = !0, clearTimeout(l), Ht.delete(l), D || n());
640
+ };
641
+ document.fonts.load(r, i).then(() => document.fonts.ready).then(v, v), l = setTimeout(v, 1500), Ht.add(l);
642
+ }
643
+ function ln(t) {
644
+ const e = t.data;
645
+ if (e && e.type === "pong") {
646
+ Kt = !0;
647
+ return;
648
+ }
649
+ !e || D || e.id !== ce || (H && (clearTimeout(H), H = void 0), ue({ mask: e.mask, ex: e.ex, ey: e.ey, shade: e.shade }), Qt(fe));
650
+ }
651
+ function cn() {
652
+ if (!(typeof Worker > "u" || typeof Blob > "u" || typeof URL > "u" || !URL.createObjectURL))
653
+ try {
654
+ const t = `${lt}
655
+ ${xe}
656
+ ${Me}
657
+ ${qe}
658
+ ${we}
659
+ var __cg=${pe};
660
+ self.onmessage=function(ev){var d=ev.data;if(d&&d.type==='ping'){try{__cg(new Uint8ClampedArray(4),1,1);self.postMessage({type:'pong'})}catch(e){}return}var g=__cg(d.alpha,d.W,d.H);self.postMessage({id:d.id,mask:g.mask,ex:g.ex,ey:g.ey,shade:g.shade},[g.mask.buffer,g.ex.buffer,g.ey.buffer,g.shade.buffer])};`;
661
+ Bt = URL.createObjectURL(new Blob([t], { type: "application/javascript" })), nt = new Worker(Bt), nt.onmessage = ln, nt.onerror = () => {
662
+ Kt = !1;
663
+ }, nt.postMessage({ type: "ping" });
664
+ } catch {
665
+ nt = null;
666
+ }
667
+ }
668
+ function fn() {
669
+ if (!D) {
670
+ if (!O.getContext("2d")) {
671
+ const t = U ?? _;
672
+ t && (t.style.cssText = "");
673
+ for (const e of [O, Y]) e.style.display = "none";
674
+ C.style.height = Rt.height, Q = !0;
675
+ return;
676
+ }
677
+ wt(!0), le = Math.round(C.getBoundingClientRect().width), Q = !0;
678
+ }
679
+ }
680
+ cn(), q && (q = !1, Ne(!0)), M && (C.addEventListener("pointermove", Jt, { passive: !0 }), C.addEventListener("pointerleave", Zt, { passive: !0 })), je(bt, f, fn);
681
+ const _e = () => {
682
+ if (!(D || !Q))
683
+ try {
684
+ document.fonts.check(`${f} 200px ${u}`) && wt(!1);
685
+ } catch {
686
+ }
687
+ };
688
+ (Ue = (te = document.fonts) == null ? void 0 : te.addEventListener) == null || Ue.call(te, "loadingdone", _e);
689
+ const De = () => {
690
+ D || (J = !!(B != null && B.matches), J && Q && ($.on && ($.on = !1, _t(), jt()), et.set = !1, ut = !0, ht(), Oe()));
691
+ };
692
+ (Ge = B == null ? void 0 : B.addEventListener) == null || Ge.call(B, "change", De);
693
+ let he;
694
+ function ge() {
695
+ clearTimeout(he), he = setTimeout(() => {
696
+ if (D) return;
697
+ if (!C.isConnected) {
698
+ Wt.destroy();
699
+ return;
700
+ }
701
+ if (!Q) return;
702
+ const t = Math.round(C.getBoundingClientRect().width);
703
+ t !== le && (le = t, wt(!1));
704
+ }, 200);
705
+ }
706
+ window.addEventListener("resize", ge);
707
+ const kt = typeof ResizeObserver < "u" ? new ResizeObserver(ge) : null;
708
+ kt == null || kt.observe(C);
709
+ const Wt = {
710
+ get text() {
711
+ return i;
712
+ },
713
+ setText(t) {
714
+ if (!D) {
715
+ if (!Q) {
716
+ i = t ?? "", E && (E.value = i), U && (U.textContent = i);
717
+ return;
718
+ }
719
+ Be(t, !1);
720
+ }
721
+ },
722
+ replay() {
723
+ D || !Q || wt(!0);
724
+ },
725
+ resize() {
726
+ D || !Q || wt(!1);
727
+ },
728
+ update(t) {
729
+ if (D) return;
730
+ t.text !== void 0 && Wt.setText(t.text);
731
+ let e = !1, n = !1, r = !1, a = !1;
732
+ if (t.font !== void 0 && ye(t.font) !== u && (u = ye(t.font), bt = Xe(u), e = !0), t.weight !== void 0) {
733
+ const s = ee(Ut(t.weight, f), 1, 1e3);
734
+ s !== f && (f = s, e = !0);
735
+ }
736
+ if (t.fill !== void 0) {
737
+ const s = ee(Ut(t.fill, b), 0.05, 1);
738
+ s !== b && (b = s, e = !0);
739
+ }
740
+ if (t.align !== void 0) {
741
+ const s = t.align === "left" || t.align === "right" ? t.align : "center";
742
+ s !== p && (p = s, e = !0);
743
+ }
744
+ t.pitch !== void 0 && Ct(t.pitch) !== yt && (yt = Ct(t.pitch), e = !0), t.axes !== void 0 && (X = t.axes, e = !0), t.threadColor !== void 0 && t.threadColor !== vt && (vt = t.threadColor, a = !0), t.threadColor2 !== void 0 && t.threadColor2 !== A && (A = t.threadColor2, a = !0), t.outlineColor !== void 0 && t.outlineColor !== T && (T = t.outlineColor, a = !0), t.colorMode !== void 0 && t.colorMode !== W && Gt.includes(t.colorMode) && (W = t.colorMode, a = !0, r = !0), t.backstitch !== void 0 && t.backstitch !== L && (L = t.backstitch, r = !0), t.stitchMode !== void 0 && t.stitchMode !== x && R.includes(t.stitchMode) && (x = t.stitchMode, n = !0), t.sewRate !== void 0 && (F = w(t.sewRate, F)), t.sewStyle !== void 0 && (j = t.sewStyle === "hand" ? "hand" : "machine"), t.animate !== void 0 && (z = t.animate), t.onTextChange !== void 0 && (K = t.onTextChange), t.sheen !== void 0 && an(t.sheen), t.editable !== void 0 && Ne(t.editable), a && Ce(), Q && (e ? je(bt, f, () => {
745
+ D || wt(!1);
746
+ }) : r ? (de(), Re(), _t(), jt(), Yt(), ht()) : (n || a) && (de(), _t(), jt(), Yt(), ht()));
747
+ },
748
+ focus() {
749
+ !D && q && (E == null || E.focus());
750
+ },
751
+ destroy() {
752
+ var t, e, n;
753
+ if (!D) {
754
+ D = !0, Nt = !1, cancelAnimationFrame(ae), clearTimeout(he), H && clearTimeout(H);
755
+ for (const r of Ht) clearTimeout(r);
756
+ if (Ht.clear(), nt && (nt.terminate(), nt = null), Bt) {
757
+ try {
758
+ URL.revokeObjectURL(Bt);
759
+ } catch {
760
+ }
761
+ Bt = "";
762
+ }
763
+ window.removeEventListener("resize", ge), kt == null || kt.disconnect(), (t = B == null ? void 0 : B.removeEventListener) == null || t.call(B, "change", De), (n = (e = document.fonts) == null ? void 0 : e.removeEventListener) == null || n.call(e, "loadingdone", _e), C.removeEventListener("pointermove", Jt), C.removeEventListener("pointerleave", Zt);
764
+ for (const r of Ft) r.remove();
765
+ if (_) {
766
+ for (; _.firstChild; ) C.insertBefore(_.firstChild, _);
767
+ _.remove(), _ = null;
768
+ }
769
+ U == null || U.remove(), U = null, C.style.height = Rt.height, C.style.position = Rt.position, !Rt.hadStyle && !C.getAttribute("style") && C.removeAttribute("style"), It = [], re = [], $t = [], dt = [], Ot = [], $.rows = [], Z = new Uint8Array(0), Xt = new Float32Array(0), ie = new Float32Array(0), se = new Float32Array(0), ft = At = null, qt = null, N = null, E = null;
770
+ }
771
+ }
772
+ };
773
+ return Wt;
774
+ }
775
+ export {
776
+ Dt as THREAD_TEXT_CLASSES,
777
+ dn as createThreadText
778
+ };