@overpunch/threadtext 0.4.6

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