@snowcone-app/canvas 0.46.10 → 0.46.13

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.
Files changed (52) hide show
  1. package/ERROR_CONTRACT.md +13 -0
  2. package/dist/{CanvasRenderer-CmIQSVCK.js → CanvasRenderer-CV8fDS6m.js} +4 -4
  3. package/dist/{CanvasRenderer-CmIQSVCK.js.map → CanvasRenderer-CV8fDS6m.js.map} +1 -1
  4. package/dist/{ContextualToolbars-BG90V6-s.js → ContextualToolbars-DsQ83_VR.js} +138 -137
  5. package/dist/ContextualToolbars-DsQ83_VR.js.map +1 -0
  6. package/dist/{EditorCanvasImpl-C7y7UNl1.js → EditorCanvasImpl-CbKLAe8P.js} +8 -8
  7. package/dist/{EditorCanvasImpl-C7y7UNl1.js.map → EditorCanvasImpl-CbKLAe8P.js.map} +1 -1
  8. package/dist/ExportManager-Q4GQE68T.js +5584 -0
  9. package/dist/ExportManager-Q4GQE68T.js.map +1 -0
  10. package/dist/{ImagePanel-BaEJnUvS.js → ImagePanel-DNgvLouH.js} +6 -6
  11. package/dist/{ImagePanel-BaEJnUvS.js.map → ImagePanel-DNgvLouH.js.map} +1 -1
  12. package/dist/{SnowconeCanvas-C6FmL0pj.js → SnowconeCanvas-Cu3IuAhp.js} +10 -10
  13. package/dist/{SnowconeCanvas-C6FmL0pj.js.map → SnowconeCanvas-Cu3IuAhp.js.map} +1 -1
  14. package/dist/advanced.mjs +16 -16
  15. package/dist/{compose-Ce53EW1v.js → compose-CatWJeVf.js} +3 -3
  16. package/dist/{compose-Ce53EW1v.js.map → compose-CatWJeVf.js.map} +1 -1
  17. package/dist/constants.d.ts +0 -1
  18. package/dist/{custom-icons-BA-MX7Y2.js → custom-icons-DP6uSyX-.js} +2 -2
  19. package/dist/{custom-icons-BA-MX7Y2.js.map → custom-icons-DP6uSyX-.js.map} +1 -1
  20. package/dist/{design-builder-HKRo18WT.js → design-builder-CCPB3WRx.js} +4 -4
  21. package/dist/design-builder-CCPB3WRx.js.map +1 -0
  22. package/dist/{embed-CYlEe_Xv.js → embed-DobFUit8.js} +9 -9
  23. package/dist/{embed-CYlEe_Xv.js.map → embed-DobFUit8.js.map} +1 -1
  24. package/dist/embed.mjs +6 -6
  25. package/dist/errors/editorError.d.ts +3 -0
  26. package/dist/{export-worker.bundle.string-CyMb_XjB.js → export-worker.bundle.string-DG2f-C1t.js} +1490 -1487
  27. package/dist/{export-worker.bundle.string-CyMb_XjB.js.map → export-worker.bundle.string-DG2f-C1t.js.map} +1 -1
  28. package/dist/{FontAnalyzer-CLMJi2Sw.js → fontAnalysisCache-B2lh7X-_.js} +1408 -3329
  29. package/dist/fontAnalysisCache-B2lh7X-_.js.map +1 -0
  30. package/dist/{guards-Dk86a-kH.js → guards-2oykBUel.js} +2 -2
  31. package/dist/{guards-Dk86a-kH.js.map → guards-2oykBUel.js.map} +1 -1
  32. package/dist/index.mjs +9 -9
  33. package/dist/internals.mjs +5 -5
  34. package/dist/{template-fields-3CH15MFC.js → template-fields-BqGWVbJo.js} +6 -6
  35. package/dist/template-fields-BqGWVbJo.js.map +1 -0
  36. package/dist/testing.mjs +3 -3
  37. package/dist/{useCommands-C6nE770t.js → useCommands-DHwYOhSp.js} +17 -17
  38. package/dist/{useCommands-C6nE770t.js.map → useCommands-DHwYOhSp.js.map} +1 -1
  39. package/dist/{usePieceGuideBundle-DJfjOVEd.js → usePieceGuideBundle-BuxwVcb5.js} +2 -2
  40. package/dist/{usePieceGuideBundle-DJfjOVEd.js.map → usePieceGuideBundle-BuxwVcb5.js.map} +1 -1
  41. package/dist/utils/fontAnalysisCache.d.ts +14 -0
  42. package/dist/{validation-C3FHoCbQ.js → validation-EoPS9fDC.js} +11 -11
  43. package/dist/{validation-C3FHoCbQ.js.map → validation-EoPS9fDC.js.map} +1 -1
  44. package/dist/workers/export-worker.bundle.string.d.ts +1 -1
  45. package/example/package.json +1 -1
  46. package/package.json +4 -4
  47. package/dist/ContextualToolbars-BG90V6-s.js.map +0 -1
  48. package/dist/ExportManager-DEJgxB0S.js +0 -3647
  49. package/dist/ExportManager-DEJgxB0S.js.map +0 -1
  50. package/dist/FontAnalyzer-CLMJi2Sw.js.map +0 -1
  51. package/dist/design-builder-HKRo18WT.js.map +0 -1
  52. package/dist/template-fields-3CH15MFC.js.map +0 -1
@@ -1,3647 +0,0 @@
1
- import { c as j, G as Vt, ae as Ke, v as Je, u as Qe, ai as Ee, aQ as Kt, ag as ze, ad as D, s as Ze, w as De, af as at, ah as Jt, K as ee, av as xe, aj as tr, aR as K, aS as er, b as rr, aT as vt, aU as Se, P as nr, S as or, I as Wt, F as ar, W as sr, f as ir, d as lr, L as cr, C as hr, g as fr, A as re, q as gt } from "./FontAnalyzer-CLMJi2Sw.js";
2
- import { imageFrameSize as dr, hasImageMasks as Ce, effectiveImageCrop as ur, isFullCrop as gr, isNeutralAdjustments as pr } from "@snowcone-app/design/wire";
3
- let Pe = null;
4
- function mr(e) {
5
- Pe = e;
6
- }
7
- function Zn() {
8
- return Pe;
9
- }
10
- const yr = j("TextureManager"), it = class it {
11
- /**
12
- * Load a texture by ID or URL
13
- */
14
- static async loadTexture(t) {
15
- if (this.cache.has(t))
16
- return this.cache.get(t);
17
- if (this.loading.has(t))
18
- return this.loading.get(t);
19
- const r = this.resolveTextureUrl(t), n = new Promise((o, i) => {
20
- const a = new Image();
21
- a.crossOrigin = "anonymous", a.onload = () => {
22
- this.cache.set(t, a), this.cache.set(r, a), this.loading.delete(t), this.loading.delete(r), o(a);
23
- }, a.onerror = () => {
24
- yr.error("Failed to load texture:", r), this.loading.delete(t), this.loading.delete(r), i(new Error(`Failed to load texture: ${r}`));
25
- }, a.src = r;
26
- });
27
- return this.loading.set(t, n), r !== t && this.loading.set(r, n), n;
28
- }
29
- /**
30
- * Get a texture synchronously (returns null if not loaded)
31
- */
32
- static getTexture(t) {
33
- const r = this.resolveTextureUrl(t);
34
- return this.cache.get(t) || this.cache.get(r) || null;
35
- }
36
- /**
37
- * Preload multiple textures
38
- */
39
- static async preloadTextures(t) {
40
- await Promise.all(t.map((r) => this.loadTexture(r)));
41
- }
42
- /**
43
- * Preload all built-in textures
44
- */
45
- static async preloadAllTextures() {
46
- await this.preloadTextures(this.TEXTURES.map((t) => t.id));
47
- }
48
- /**
49
- * Clear cache
50
- */
51
- static clearCache() {
52
- this.cache.clear(), this.loading.clear();
53
- }
54
- /**
55
- * Resolve texture URL from ID
56
- */
57
- static resolveTextureUrl(t) {
58
- if (t.startsWith("http://") || t.startsWith("https://") || t.startsWith("/") || t.startsWith("data:"))
59
- return t;
60
- const r = this.TEXTURES.find((n) => n.id === t);
61
- return r ? r.url : `/textures/${t}`;
62
- }
63
- /**
64
- * Get texture info by ID
65
- */
66
- static getTextureInfo(t) {
67
- return this.TEXTURES.find((r) => r.id === t);
68
- }
69
- /**
70
- * Get all available textures
71
- */
72
- static getAllTextures() {
73
- return [...this.TEXTURES];
74
- }
75
- /**
76
- * Get textures by category
77
- */
78
- static getTexturesByCategory(t) {
79
- return this.TEXTURES.filter((r) => r.category === t);
80
- }
81
- /**
82
- * Get preset color for a texture
83
- * Each texture has a fixed color that's part of its preset
84
- */
85
- static getTexturePresetColor(t) {
86
- const r = this.TEXTURES.find((n) => n.id === t);
87
- return (r == null ? void 0 : r.presetColor) || "#ffffff";
88
- }
89
- };
90
- it.cache = /* @__PURE__ */ new Map(), it.loading = /* @__PURE__ */ new Map(), it.TEXTURES = [
91
- {
92
- id: "glass-frame",
93
- name: "Glass Frame",
94
- url: "/textures/glass-frame.svg",
95
- category: "glass",
96
- presetColor: "#ccc"
97
- // Light gray
98
- }
99
- ];
100
- let pt = it;
101
- const wr = j("ElementPreviewRenderer"), br = {
102
- width: 40,
103
- height: 40,
104
- padding: 4,
105
- backgroundColor: "transparent"
106
- };
107
- function kr(e, t = {}) {
108
- zt();
109
- const r = { ...br, ...t }, n = document.createElement("canvas");
110
- n.width = r.width, n.height = r.height;
111
- const o = n.getContext("2d");
112
- if (!o)
113
- return "";
114
- r.backgroundColor !== "transparent" && (o.fillStyle = r.backgroundColor, o.fillRect(0, 0, r.width, r.height)), o.save();
115
- try {
116
- e instanceof Vt ? vr(o, e, r) : Tr(o, e, r);
117
- } catch (i) {
118
- wr.warn("Failed to generate preview:", i);
119
- }
120
- return o.restore(), n.toDataURL("image/png");
121
- }
122
- function Tr(e, t, r) {
123
- var d;
124
- const n = ((d = t.getBoundingBox) == null ? void 0 : d.call(t)) || { x: 0, y: 0, width: 100, height: 100 }, o = n.width, i = n.height;
125
- if (o === 0 || i === 0) {
126
- ne(e, t, r);
127
- return;
128
- }
129
- const a = r.width - r.padding * 2, l = r.height - r.padding * 2, s = Math.min(
130
- a / o,
131
- l / i,
132
- 1
133
- // Don't scale up, only down
134
- ), c = o * s, h = i * s, f = r.padding + (a - c) / 2, u = r.padding + (l - h) / 2;
135
- e.save(), e.translate(f - n.x * s, u - n.y * s), e.scale(s, s), typeof t.render == "function" ? t.render(e, !1, !1) : ne(e, t, r), e.restore();
136
- }
137
- function ne(e, t, r) {
138
- e.fillStyle = "#888888", e.font = "10px Arial", e.textAlign = "center", e.textBaseline = "middle";
139
- const n = t.transformType || "Element";
140
- e.fillText(n, r.width / 2, r.height / 2);
141
- }
142
- function vr(e, t, r) {
143
- const n = r.width - r.padding * 2, o = r.height - r.padding * 2, i = r.padding, a = r.padding;
144
- e.fillStyle = "#888888", e.strokeStyle = "#666666", e.lineWidth = 1;
145
- const l = o * 0.7, s = a + o * 0.3, c = n * 0.4, h = o * 0.2;
146
- e.fillRect(i, s, n, l), e.fillRect(i, s - h, c, h), t.children && t.children.length > 0 && (e.fillStyle = "#FFFFFF", e.font = "bold 10px Arial", e.textAlign = "center", e.textBaseline = "middle", e.fillText(
147
- t.children.length.toString(),
148
- i + n / 2,
149
- s + l / 2
150
- ));
151
- }
152
- function Dn(e, t, r) {
153
- var c;
154
- e.translate(t.x || 0, t.y || 0), e.rotate(-(t.rotation || 0) * Math.PI / 180);
155
- const n = t, o = n.fontSize || 24, i = n.fontFamily || "Arial", a = n.bold || !1, l = n.italic || !1;
156
- e.font = Ke(o, i, a, l);
157
- const s = n.text || "";
158
- if (t.transformType === "custom") {
159
- const f = ((c = t.transformData) == null ? void 0 : c.width) || 200, u = Je(s, o, i, a, l), d = Math.min(u, f), p = o * 1.2;
160
- e.rect(-d / 2, -p / 2, d, p);
161
- } else {
162
- const h = e.measureText(s).width;
163
- e.beginPath(), e.moveTo(-h / 2, 0), e.lineTo(h / 2, 0), e.moveTo(-h / 2, -o), e.lineTo(h / 2, -o), e.moveTo(-h / 2, o * 0.2), e.lineTo(h / 2, o * 0.2);
164
- }
165
- }
166
- function Mr(e, t) {
167
- if (!t.transformData) return;
168
- const { borderRadius: r = 0 } = t.transformData, n = t.transformData.width ?? 100, o = t.transformData.height ?? 100;
169
- if (e.translate(t.x || 0, t.y || 0), e.rotate(-(t.rotation || 0) * Math.PI / 180), e.beginPath(), r > 0) {
170
- const i = Math.min(n, o) / 2, a = Math.min(r, i), l = -n / 2, s = -o / 2;
171
- e.moveTo(l + a, s), e.lineTo(l + n - a, s), e.arcTo(l + n, s, l + n, s + a, a), e.lineTo(l + n, s + o - a), e.arcTo(l + n, s + o, l + n - a, s + o, a), e.lineTo(l + a, s + o), e.arcTo(l, s + o, l, s + o - a, a), e.lineTo(l, s + a), e.arcTo(l, s, l + a, s, a), e.closePath();
172
- } else
173
- e.rect(-n / 2, -o / 2, n, o);
174
- }
175
- function xn(e, t, r, n) {
176
- e.beginPath(), e.arc(t, r, n, 0, Math.PI * 2), e.closePath();
177
- }
178
- function to(e, t, r, n, o) {
179
- e.beginPath(), e.rect(t, r, n, o), e.closePath();
180
- }
181
- function Ie(e, t) {
182
- e.strokeStyle = t.color, e.lineWidth = t.width, e.lineCap = t.lineCap || "butt", e.lineJoin = t.lineJoin || "miter", t.miterLimit !== void 0 && (e.miterLimit = t.miterLimit), t.dashArray && t.dashArray.length > 0 ? e.setLineDash(t.dashArray) : e.setLineDash([]), t.opacity !== void 0 && (e.globalAlpha = t.opacity);
183
- }
184
- function Er(e, t) {
185
- if (typeof OffscreenCanvas < "u")
186
- return new OffscreenCanvas(e, t);
187
- const r = document.createElement("canvas");
188
- return r.width = e, r.height = t, r;
189
- }
190
- function Ae(e, t, r) {
191
- var s;
192
- if (!((s = t.stroke) != null && s.enabled)) return;
193
- const n = t;
194
- if (!(n.text || "")) return;
195
- const i = (r == null ? void 0 : r.isKnockout) === !0, a = t.stroke.opacity ?? 1;
196
- if (!i && a < 1)
197
- try {
198
- Sr(e, t, n, r, a);
199
- } catch {
200
- st(e, t, n, r, !1);
201
- }
202
- else
203
- st(e, t, n, r, i);
204
- }
205
- function Sr(e, t, r, n, o) {
206
- var P, I;
207
- const i = r.text || "", a = r.fontSize || 24, l = t.stroke.width, s = t.stroke.feather || 0, c = t.transformType === "custom" && ((P = t.transformData) == null ? void 0 : P.width), h = c ? ((I = t.transformData) == null ? void 0 : I.width) ?? 200 : 0, f = c ? h + l * 2 + s * 2 + 40 : a * i.length * 0.7 + l * 2 + s * 2 + 40, u = a * 3 + l * 2 + s * 2 + 40, d = e.getTransform(), p = Math.max(Math.abs(d.a), Math.abs(d.d), 1), m = Math.ceil(f * p / 2) * 2, w = Math.ceil(u * p / 2) * 2;
208
- if (m <= 0 || w <= 0) {
209
- st(e, t, r, n, !1);
210
- return;
211
- }
212
- const k = Er(m, w), b = k.getContext("2d");
213
- if (!b) {
214
- st(e, t, r, n, !1);
215
- return;
216
- }
217
- const T = t.x || 0, g = t.y || 0, y = d.a * T + d.c * g + d.e, S = d.b * T + d.d * g + d.f;
218
- b.setTransform(
219
- d.a,
220
- d.b,
221
- d.c,
222
- d.d,
223
- m / 2 - y + d.e,
224
- w / 2 - S + d.f
225
- );
226
- const C = t.stroke.opacity;
227
- t.stroke.opacity = 1, st(b, t, r, n, !1), t.stroke.opacity = C, e.save(), e.resetTransform(), e.globalAlpha = o, e.drawImage(
228
- k,
229
- Math.round(y - m / 2),
230
- Math.round(S - w / 2)
231
- ), e.restore();
232
- }
233
- function st(e, t, r, n, o) {
234
- var d;
235
- const i = r.text || "";
236
- e.save(), n != null && n.positionApplied || (e.translate(t.x || 0, t.y || 0), e.rotate(-(t.rotation || 0) * Math.PI / 180));
237
- const a = r.fontSize || 24, l = r.fontFamily || "Arial", s = r.bold || !1, c = r.italic || !1, h = r.textAlign || "center";
238
- e.font = `${c ? "italic " : ""}${s ? "bold " : ""}${a}px ${l}`, e.textAlign = h, e.textBaseline = "alphabetic", o ? (e.strokeStyle = "#000000", e.globalAlpha = 1) : (e.strokeStyle = t.stroke.color, t.stroke.opacity !== void 0 && (e.globalAlpha = t.stroke.opacity)), e.lineWidth = t.stroke.width, e.lineCap = t.stroke.lineCap || "round", e.lineJoin = t.stroke.lineJoin || "round", t.stroke.miterLimit !== void 0 && (e.miterLimit = t.stroke.miterLimit), !o && t.stroke.feather && t.stroke.feather > 0 && "filter" in e && (e.filter = `blur(${t.stroke.feather}px)`);
239
- const f = Qe(a, l, s, c);
240
- if (t.transformType === "custom" && ((d = t.transformData) == null ? void 0 : d.width)) {
241
- const p = t.transformData, m = (p == null ? void 0 : p.width) ?? 200, w = m - D * 2, k = r.letterSpacing ?? 0, { lines: b, height: T } = Ee(
242
- i,
243
- w,
244
- a,
245
- l,
246
- s,
247
- c,
248
- void 0,
249
- // No locked line count for stroke
250
- !0,
251
- // strict: wrap exactly at maxWidth
252
- { letterSpacing: k, lineHeight: r.lineHeight }
253
- ), g = -m / 2, y = -T / 2, S = a * (r.lineHeight ?? Kt);
254
- b.forEach((C, P) => {
255
- const I = P === 0, M = P === b.length - 1;
256
- let A = C;
257
- if (!I) {
258
- const R = C.match(/^ +/), U = R ? R[0].length : 0;
259
- U > 0 && (A = A.substring(U));
260
- }
261
- if (!M) {
262
- const R = A.match(/ +$/), U = R ? R[0].length : 0;
263
- U > 0 && (A = A.substring(0, A.length - U));
264
- }
265
- let E = g + D;
266
- h === "center" ? E = g + m / 2 : h === "right" && (E = g + m - D);
267
- const v = y + f.ascent + P * S;
268
- ze(e, A, E, v, k);
269
- });
270
- } else {
271
- const w = -f.height / 2 + f.ascent;
272
- e.strokeText(i, 0, w);
273
- }
274
- e.restore();
275
- }
276
- function Cr(e, t, r) {
277
- var o;
278
- if (!((o = t.stroke) != null && o.enabled) || !t.transformData) return;
279
- const n = (r == null ? void 0 : r.isKnockout) === !0;
280
- e.save(), n ? (e.strokeStyle = "#000000", e.lineWidth = t.stroke.width, e.lineCap = t.stroke.lineCap || "butt", e.lineJoin = t.stroke.lineJoin || "miter", e.globalAlpha = 1, t.stroke.miterLimit !== void 0 && (e.miterLimit = t.stroke.miterLimit)) : Ie(e, t.stroke), !n && t.stroke.feather && t.stroke.feather > 0 && "filter" in e && (e.filter = `blur(${t.stroke.feather}px)`), Mr(e, t), e.stroke(), e.restore();
281
- }
282
- function eo(e, t, r) {
283
- t.enabled && (e.save(), Ie(e, t), t.feather && t.feather > 0 && "filter" in e && (e.filter = `blur(${t.feather}px)`), e.beginPath(), r(), e.stroke(), e.restore());
284
- }
285
- const oe = j("GlyphRenderer");
286
- function Pr(e, t, r, n, o, i, a, l = {}) {
287
- try {
288
- const s = [];
289
- Object.entries(a).forEach(([w, k]) => {
290
- k && s.push(w);
291
- });
292
- const c = [];
293
- t.GSUB && t.GSUB.featureList && t.GSUB.featureList.forEach((w) => {
294
- c.push(w.tag);
295
- });
296
- const h = {};
297
- h.ccmp = !0, h.locl = !0, h.kern = !0, s.forEach((w) => {
298
- h[w] = !0;
299
- }), h.calt && (h.liga = !0, h.init = !0, h.fina = !0);
300
- const f = t.layout(r, h, "latn"), u = i / t.unitsPerEm;
301
- let d = 0;
302
- f.glyphs.forEach((w) => {
303
- d += w.advanceWidth * u;
304
- });
305
- let p = n;
306
- l.align === "center" ? p = n - d / 2 : l.align === "right" && (p = n - d), e.save(), e.fillStyle = l.color || "#000000";
307
- let m = p;
308
- f.glyphs.forEach((w, k) => {
309
- const b = f.positions[k];
310
- let T = null;
311
- try {
312
- if (w.path && typeof w.path.toPath == "function")
313
- T = w.path.toPath();
314
- else if (w.path && typeof w.path.toSVG == "function") {
315
- const g = w.path.toSVG();
316
- if (g && /^[MmLlHhVvCcSsQqTtAaZz]/.test(g))
317
- T = g;
318
- else {
319
- const y = g == null ? void 0 : g.match(/\bd=["']([^"']+)["']/);
320
- T = y ? y[1] : null;
321
- }
322
- }
323
- } catch (g) {
324
- oe.warn("Error getting path for glyph:", w.name, g);
325
- }
326
- if (T) {
327
- const g = new Path2D(T);
328
- e.save(), e.translate(m + b.xOffset * u, o + b.yOffset * u), e.scale(u, -u), e.fill(g), e.restore();
329
- }
330
- m += w.advanceWidth * u;
331
- }), e.restore();
332
- } catch (s) {
333
- oe.error("Error rendering with fontkit:", s), e.fillStyle = l.color || "#000000", e.textAlign = l.align || "left", e.fillText(r, n, o);
334
- }
335
- }
336
- function Ir(e, t, r, n, o, i, a, l = {}) {
337
- const c = ((w) => {
338
- for (const k of w) {
339
- const b = k.codePointAt(0);
340
- if (b && b >= 983040 && b <= 1048573) return !0;
341
- }
342
- return !1;
343
- })(r);
344
- if ((!a || a.length === 0) && !c) {
345
- e.fillStyle = l.color || "#000000", e.textAlign = l.align || "left", e.fillText(r, n, o);
346
- return;
347
- }
348
- const h = /* @__PURE__ */ new Map();
349
- a && a.forEach((w) => {
350
- h.set(w.charIndex, w.glyphIndex);
351
- });
352
- const f = i / t.unitsPerEm, u = Array.from(r);
353
- let d = 0;
354
- u.forEach((w, k) => {
355
- const b = w.codePointAt(0) || 0;
356
- let T;
357
- if (b >= 983040 && b <= 1048573)
358
- T = b - 983040;
359
- else if (T = h.get(k), T === void 0) {
360
- const y = t.glyphForCodePoint(b);
361
- T = y ? y.id : 0;
362
- }
363
- const g = t.getGlyph(T);
364
- g && (d += g.advanceWidth * f);
365
- });
366
- let p = n;
367
- l.align === "center" ? p = n - d / 2 : l.align === "right" && (p = n - d), e.save(), e.fillStyle = l.color || "#000000";
368
- let m = p;
369
- u.forEach((w, k) => {
370
- const b = w.codePointAt(0) || 0;
371
- let T;
372
- if (b >= 983040 && b <= 1048573)
373
- T = b - 983040;
374
- else if (T = h.get(k), T === void 0) {
375
- const y = t.glyphForCodePoint(b);
376
- T = y ? y.id : 0;
377
- }
378
- const g = t.getGlyph(T);
379
- if (g) {
380
- const y = g.path.toSVG();
381
- if (y) {
382
- const S = new Path2D(y);
383
- e.save(), e.translate(m, o), e.scale(f, -f), e.fill(S), e.restore();
384
- }
385
- m += g.advanceWidth * f;
386
- }
387
- }), e.restore();
388
- }
389
- function Ar(e, t, r, n, o) {
390
- e.fillStyle = r.color;
391
- const i = r.textAlign || "center", a = (m) => m.letterSpacing !== void 0 ? m.letterSpacing : r.letterSpacing ?? 0, l = Ze(t);
392
- let s, c;
393
- n !== void 0 && n > 0 && !o ? (s = [], c = [], l.forEach((m) => {
394
- const w = De(m, n, r);
395
- w.forEach((k, b) => {
396
- s.push(k), c.push({
397
- isParagraphStart: b === 0,
398
- isParagraphEnd: b === w.length - 1
399
- });
400
- });
401
- })) : (s = l, c = l.map(() => ({
402
- isParagraphStart: !0,
403
- isParagraphEnd: !0
404
- })));
405
- const h = [];
406
- s.forEach((m, w) => {
407
- const k = [];
408
- let b = 0, T = 0;
409
- const g = c[w].isParagraphStart, y = c[w].isParagraphEnd;
410
- m.forEach((v) => {
411
- const R = v.style.fontSize !== void 0 ? v.style.fontSize : r.fontSize, U = v.style.fontFamily !== void 0 ? v.style.fontFamily : r.fontFamily, W = v.style.bold !== void 0 ? v.style.bold : r.bold || !1, F = v.style.italic !== void 0 ? v.style.italic : r.italic || !1;
412
- e.font = q(R, U, W, F);
413
- const B = at(e.measureText(v.text).width, v.text, a(v.style));
414
- k.push(B);
415
- const $ = ct(R, U, W, F);
416
- b = Math.max(b, $.height), T = Math.max(T, $.ascent);
417
- });
418
- const S = m.map((v) => v.text).join(""), C = S.match(/^ +/), P = S.match(/ +$/), I = C ? C[0].length : 0, M = P ? P[0].length : 0;
419
- let A = 0, E = 0;
420
- m.forEach((v) => {
421
- const R = v.style.fontSize !== void 0 ? v.style.fontSize : r.fontSize, U = v.style.fontFamily !== void 0 ? v.style.fontFamily : r.fontFamily, W = v.style.bold !== void 0 ? v.style.bold : r.bold || !1, F = v.style.italic !== void 0 ? v.style.italic : r.italic || !1;
422
- let B = v.text;
423
- const $ = E, St = E + v.text.length;
424
- if (!g && I > 0 && $ < I) {
425
- const L = Math.min(I - $, v.text.length);
426
- B = B.substring(L);
427
- }
428
- if (!y && M > 0) {
429
- const L = S.length - M;
430
- if (St > L) {
431
- const G = !g && I > 0 && $ < I ? Math.min(I - $, v.text.length) : 0, Ct = Math.max(0, L - $ - G);
432
- B = B.substring(0, Ct);
433
- }
434
- }
435
- E += v.text.length, B.length > 0 && (e.save(), e.font = q(R, U, W, F), A += at(
436
- e.measureText(B).width,
437
- B,
438
- a(v.style)
439
- ), e.restore());
440
- }), h.push({
441
- spans: m,
442
- width: A,
443
- // Use adjusted width for alignment (may be 0 for empty lines)
444
- height: b,
445
- ascent: T,
446
- spanWidths: k,
447
- isParagraphStart: g,
448
- // Store metadata with line
449
- isParagraphEnd: y
450
- });
451
- });
452
- const f = ct(r.fontSize, r.fontFamily, r.bold, r.italic), u = r.fontSize * (r.lineHeight ?? Kt);
453
- let d;
454
- h.length === 0 ? d = 0 : h.length === 1 ? d = f.height : d = (h.length - 1) * u + f.height;
455
- const p = -d / 2;
456
- h.forEach((m, w) => {
457
- const { spans: k, width: b, isParagraphStart: T, isParagraphEnd: g } = m;
458
- let y;
459
- i === "center" ? y = -b / 2 : i === "right" ? y = n !== void 0 ? n / 2 - b : -b : y = n !== void 0 ? -n / 2 : 0;
460
- const S = p + f.ascent + w * u, C = k.map((v) => v.text).join(""), P = C.match(/^ +/), I = C.match(/ +$/), M = P ? P[0].length : 0, A = I ? I[0].length : 0;
461
- let E = 0;
462
- k.forEach((v) => {
463
- const R = v.style.fontSize !== void 0 ? v.style.fontSize : r.fontSize, U = v.style.fontFamily !== void 0 ? v.style.fontFamily : r.fontFamily, W = v.style.color !== void 0 ? v.style.color : r.color, F = v.style.bold !== void 0 ? v.style.bold : r.bold || !1, B = v.style.italic !== void 0 ? v.style.italic : r.italic || !1, $ = v.style.underline !== void 0 ? v.style.underline : r.underline || !1, St = v.style.strikethrough !== void 0 ? v.style.strikethrough : r.strikethrough || !1;
464
- let L = v.text, G = E, Ct = E + v.text.length;
465
- if (!T && M > 0 && G < M) {
466
- const Y = Math.min(M - G, v.text.length);
467
- L = L.substring(Y);
468
- }
469
- if (!g && A > 0) {
470
- const Y = C.length - A;
471
- if (Ct > Y) {
472
- const rt = !T && M > 0 && G < M ? Math.min(M - G, v.text.length) : 0, V = Math.max(0, Y - G - rt);
473
- L = L.substring(0, V);
474
- }
475
- }
476
- if (E += v.text.length, L.length > 0) {
477
- const Y = a(v.style);
478
- e.font = q(R, U, F, B), e.fillStyle = W, e.textBaseline = "alphabetic", e.textAlign = "left", Jt(e, L, y, S, Y);
479
- const rt = at(e.measureText(L).width, L, Y);
480
- if ($) {
481
- const V = S + R * 0.1;
482
- e.beginPath(), e.moveTo(y, V), e.lineTo(y + rt, V), e.lineWidth = Math.max(1, R * 0.05), e.strokeStyle = W, e.stroke();
483
- }
484
- if (St) {
485
- const V = S - R * 0.25;
486
- e.beginPath(), e.moveTo(y, V), e.lineTo(y + rt, V), e.lineWidth = Math.max(1, R * 0.05), e.strokeStyle = W, e.stroke();
487
- }
488
- y += rt;
489
- }
490
- });
491
- });
492
- }
493
- const Ft = j("canvas-renderer");
494
- let dt = null;
495
- function Qt() {
496
- if (!dt)
497
- if (typeof OffscreenCanvas < "u")
498
- dt = new OffscreenCanvas(1, 1).getContext("2d");
499
- else if (typeof document < "u")
500
- dt = document.createElement("canvas").getContext("2d");
501
- else
502
- throw new Error("No canvas context available");
503
- return dt;
504
- }
505
- function q(e, t, r = !1, n = !1) {
506
- const o = [];
507
- return n && o.push("italic"), r && o.push("bold"), o.push(`${e}px`), o.push(t), o.join(" ");
508
- }
509
- function ae(e, t, r, n = !1, o = !1, i = 0) {
510
- const a = Qt();
511
- return a.font = q(t, r, n, o), at(a.measureText(e).width, e, i);
512
- }
513
- function ct(e, t, r = !1, n = !1) {
514
- const o = Qt();
515
- o.font = q(e, t, r, n);
516
- const a = o.measureText("ÁÉÍgjpqy"), l = a.actualBoundingBoxAscent || e * 0.8, s = a.actualBoundingBoxDescent || e * 0.2, c = l + s;
517
- return { ascent: l, descent: s, height: c };
518
- }
519
- function ro(e, t) {
520
- const r = e.map((o, i) => ({
521
- isParagraphStart: !t && i === 0,
522
- isParagraphEnd: !t && i === e.length - 1
523
- }));
524
- return e.map((o, i) => {
525
- if (o.trim().length === 0)
526
- return "";
527
- const a = r[i].isParagraphStart, l = r[i].isParagraphEnd;
528
- let s = o;
529
- return a || (s = s.replace(/^ +/, "")), l || (s = s.replace(/ +$/, "")), s;
530
- }).filter((o) => o.length > 0);
531
- }
532
- function Rr(e, t) {
533
- const r = t.fontSize, n = t.fontFamily, o = t.bold || !1, i = t.italic || !1, a = t.textAlign || "center", l = t.text;
534
- e.font = q(r, n, o, i), e.textAlign = a, e.textBaseline = "alphabetic", e.fillStyle = t.color;
535
- const s = ct(r, n, o, i), f = -s.height / 2 + s.ascent;
536
- Jt(e, l, 0, f, t.letterSpacing);
537
- }
538
- function Wr(e, t, r, n, o = !1, i = !1, a) {
539
- const l = Qt();
540
- if (l.font = q(r, n, o, i), e.trim().length === 0)
541
- return [e];
542
- if (e.includes(`
543
- `)) {
544
- const P = e.split(`
545
- `), I = [];
546
- for (let M = 0; M < P.length; M++) {
547
- const A = P[M], E = Wr(A, t, r, n, o, i);
548
- I.push(...E);
549
- }
550
- return I;
551
- }
552
- const s = e.match(/^\s*/), c = e.match(/\s*$/), h = s ? s[0] : "", f = c ? c[0] : "", d = e.substring(h.length, e.length - f.length).split(" ");
553
- if (a !== void 0 && a > 0) {
554
- if (a === 1)
555
- return [h + d.join(" ") + f];
556
- const P = [], I = Math.ceil(d.length / a);
557
- for (let M = 0; M < d.length; M += I) {
558
- const E = d.slice(M, M + I).join(" ");
559
- M === 0 && M + I >= d.length ? P.push(h + E + f) : M === 0 ? P.push(h + E) : M + I >= d.length ? P.push(E + f) : P.push(E);
560
- }
561
- return P;
562
- }
563
- const p = d.join(" "), m = h + p + f, w = l.measureText(m).width, k = t < 200, T = Math.max(20, t * (k ? 0.08 : 0.05));
564
- if (w <= t + T)
565
- return [m];
566
- const g = [];
567
- let y = d[0];
568
- const C = Math.max(15, t * (k ? 0.05 : 0.03));
569
- for (let P = 1; P < d.length; P++) {
570
- const I = d[P], M = y + " " + I;
571
- l.measureText(M).width > t + C ? (g.push(y), y = I) : y = M;
572
- }
573
- return g.push(y), g.length > 0 && (g[0] = h + g[0], g[g.length - 1] = g[g.length - 1] + f), g;
574
- }
575
- function no(e, t, r, n, o, i, a = "left", l = 0, s = 0, c = 1.2, h = !1, f = !1, u = !1, d = !1, p, m) {
576
- e.font = q(o, i, h, f), e.textBaseline = "alphabetic", e.textAlign = a;
577
- const w = (M) => {
578
- for (const A of M) {
579
- const E = A.codePointAt(0);
580
- if (E && E >= 983040 && E <= 1048573)
581
- return !0;
582
- }
583
- return !1;
584
- }, k = p && Object.values(p).some((M) => M), b = m && m.length > 0, T = t.join(" "), g = w(T), y = k || b || g, S = y ? ee.getFontSync(i, h ? 700 : 400) : null;
585
- y && !S && (Ft.warn(
586
- `[renderMultilineText] Font ${i} not loaded for fontkit rendering. Text may not render correctly. Has PUA: ${g}, Text: "${T}"`
587
- ), ee.loadFont(i, h ? 700 : 400).catch((M) => {
588
- Ft.error("[renderMultilineText] Error loading font:", M);
589
- }));
590
- const C = ct(o, i, h, f), P = o * c;
591
- let I = 0;
592
- t.forEach((M, A) => {
593
- let E = r + s;
594
- a === "center" ? E = r + l / 2 : a === "right" && (E = r + l - s);
595
- const v = n + C.ascent + A * P, R = Array.from(M).length, U = b ? m.filter((W) => {
596
- const F = W.charIndex - I;
597
- return F >= 0 && F < R;
598
- }).map((W) => ({
599
- ...W,
600
- charIndex: W.charIndex - I
601
- // Adjust to line-relative index
602
- })) : [];
603
- if (S && (k || U.length > 0 || g) ? U.length > 0 || w(M) ? Ir(e, S, M, E, v, o, U, {
604
- color: e.fillStyle,
605
- align: a === "start" || a === "end" ? "left" : a
606
- }) : p && Pr(e, S, M, E, v, o, p, {
607
- color: e.fillStyle,
608
- align: a === "start" || a === "end" ? "left" : a
609
- }) : e.fillText(M, E, v), I += R + 1, u) {
610
- const W = ae(M, o, i, h, f);
611
- let F = E;
612
- a === "center" ? F = E - W / 2 : a === "right" && (F = E - W);
613
- const B = v + o * 0.1;
614
- e.beginPath(), e.moveTo(F, B), e.lineTo(F + W, B), e.lineWidth = Math.max(1, o * 0.05), e.strokeStyle = e.fillStyle, e.stroke();
615
- }
616
- if (d) {
617
- const W = ae(M, o, i, h, f);
618
- let F = E;
619
- a === "center" ? F = E - W / 2 : a === "right" && (F = E - W);
620
- const B = v - o * 0.25;
621
- e.beginPath(), e.moveTo(F, B), e.lineTo(F + W, B), e.lineWidth = Math.max(1, o * 0.05), e.strokeStyle = e.fillStyle, e.stroke();
622
- }
623
- });
624
- }
625
- function Ut(e, t) {
626
- Fr(e, t);
627
- }
628
- function Fr(e, t) {
629
- var o;
630
- (o = t.stroke) != null && o.enabled && Ae(e, t), e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180);
631
- const r = t.transformData, n = ((r == null ? void 0 : r.width) ?? 100) - D * 2;
632
- if (t.richText) {
633
- const i = xe.fromJSON(t.richText);
634
- e.save(), e.translate(0, 0);
635
- const a = t._lockedLineCount;
636
- Ar(
637
- e,
638
- i,
639
- {
640
- fontSize: t.fontSize,
641
- fontFamily: t.fontFamily,
642
- color: t.color,
643
- bold: t.bold,
644
- italic: t.italic,
645
- underline: t.underline,
646
- strikethrough: t.strikethrough,
647
- textAlign: t.textAlign,
648
- letterSpacing: t.letterSpacing,
649
- lineHeight: t.lineHeight
650
- },
651
- n,
652
- a
653
- ), e.restore();
654
- } else if (t.text) {
655
- e.fillStyle = t.color;
656
- const i = t._lockedLineCount, a = t.letterSpacing ?? 0, { lines: l, height: s } = Ee(
657
- t.text,
658
- n,
659
- t.fontSize,
660
- t.fontFamily,
661
- t.bold,
662
- t.italic,
663
- i,
664
- !0,
665
- // strict: true
666
- { letterSpacing: a, lineHeight: t.lineHeight }
667
- ), c = ct(
668
- t.fontSize,
669
- t.fontFamily,
670
- t.bold,
671
- t.italic
672
- ), h = -((r == null ? void 0 : r.width) ?? 100) / 2, f = -s / 2, u = t.fontSize * (t.lineHeight ?? Kt);
673
- e.font = `${t.italic ? "italic " : ""}${t.bold ? "bold " : ""}${t.fontSize}px ${t.fontFamily}`, e.textBaseline = "alphabetic", e.textAlign = t.textAlign, l.forEach((d, p) => {
674
- const m = p === 0, w = p === l.length - 1;
675
- let k = d;
676
- if (!m) {
677
- const g = d.match(/^ +/), y = g ? g[0].length : 0;
678
- y > 0 && (k = k.substring(y));
679
- }
680
- if (!w) {
681
- const g = k.match(/ +$/), y = g ? g[0].length : 0;
682
- y > 0 && (k = k.substring(0, k.length - y));
683
- }
684
- let b = h + D;
685
- t.textAlign === "center" ? b = h + ((r == null ? void 0 : r.width) ?? 100) / 2 : t.textAlign === "right" && (b = h + ((r == null ? void 0 : r.width) ?? 100) - D);
686
- const T = f + c.ascent + p * u;
687
- if (Jt(e, k, b, T, a), k.length > 0) {
688
- const g = at(e.measureText(k).width, k, a);
689
- let y = b;
690
- if (t.textAlign === "center" ? y -= g / 2 : t.textAlign === "right" && (y -= g), t.underline) {
691
- const S = T + t.fontSize * 0.1;
692
- e.fillRect(y, S, g, Math.max(1, t.fontSize * 0.05));
693
- }
694
- if (t.strikethrough) {
695
- const S = T - c.ascent * 0.4;
696
- e.fillRect(y, S, g, Math.max(1, t.fontSize * 0.05));
697
- }
698
- }
699
- });
700
- }
701
- e.restore();
702
- }
703
- function oo(e, t) {
704
- var a;
705
- const r = t.opacity ?? 1, n = !!((a = t.stroke) != null && a.enabled), o = t.type === "custom";
706
- if (r < 1 && n && o) {
707
- Ur(e, t, r);
708
- return;
709
- }
710
- const i = e.globalAlpha;
711
- r !== 1 && (e.globalAlpha = r), Bt(e, t), e.globalAlpha = i;
712
- }
713
- function Ur(e, t, r) {
714
- var w;
715
- const n = t.transformData, o = t.fontSize || 24, i = ((w = t.stroke) == null ? void 0 : w.width) || 0, a = ((n == null ? void 0 : n.width) ?? o * t.text.length * 0.7) + i * 2 + 40, l = o * 3 + i * 2 + 40, s = e.getTransform(), c = Math.max(Math.abs(s.a), Math.abs(s.d), 1), h = Math.ceil(a * c), f = Math.ceil(l * c);
716
- let u;
717
- typeof OffscreenCanvas < "u" ? u = new OffscreenCanvas(h, f) : (u = document.createElement("canvas"), u.width = h, u.height = f);
718
- const d = u.getContext("2d");
719
- if (!d) {
720
- const k = e.globalAlpha;
721
- e.globalAlpha = r, Bt(e, t), e.globalAlpha = k;
722
- return;
723
- }
724
- const p = s.a * t.x + s.e, m = s.d * t.y + s.f;
725
- d.setTransform(
726
- s.a,
727
- s.b,
728
- s.c,
729
- s.d,
730
- s.e - p + h / 2,
731
- s.f - m + f / 2
732
- ), Bt(d, t), e.save(), e.setTransform(1, 0, 0, 1, 0, 0), e.globalAlpha = r, e.drawImage(u, p - h / 2, m - f / 2), e.restore();
733
- }
734
- function Bt(e, t) {
735
- switch (t.type) {
736
- case "custom":
737
- Ut(e, t);
738
- break;
739
- case "circle":
740
- Re(e, t);
741
- break;
742
- case "wave":
743
- We(e, t);
744
- break;
745
- case "arch":
746
- Fe(e, t);
747
- break;
748
- case "ascend":
749
- Ue(e, t);
750
- break;
751
- case "lean":
752
- Be(e, t);
753
- break;
754
- case "flag":
755
- Oe(e, t);
756
- break;
757
- default:
758
- Ft.warn(`[canvas-renderer] Unsupported transform type: ${t.type}`), Ut(e, t);
759
- }
760
- }
761
- function Br(e, t) {
762
- if (typeof OffscreenCanvas < "u")
763
- return new OffscreenCanvas(e, t);
764
- const r = document.createElement("canvas");
765
- return r.width = e, r.height = t, r;
766
- }
767
- function tt(e, t, r) {
768
- if (r != null && r.enabled)
769
- if (e.strokeStyle = r.color, e.lineWidth = r.width, e.lineCap = r.lineCap || "round", e.lineJoin = r.lineJoin || "round", r.miterLimit !== void 0 && (e.miterLimit = r.miterLimit), r.opacity !== void 0) {
770
- const n = e.globalAlpha;
771
- e.globalAlpha = r.opacity, e.strokeText(t, 0, 0), e.globalAlpha = n;
772
- } else
773
- e.strokeText(t, 0, 0);
774
- }
775
- function et(e, t, r) {
776
- const n = t.stroke, o = (n == null ? void 0 : n.opacity) ?? 1, i = e.globalAlpha, a = (n == null ? void 0 : n.enabled) && o < 1, l = (n == null ? void 0 : n.enabled) && i < 1;
777
- if (!(a || l)) {
778
- r(e, t);
779
- return;
780
- }
781
- const c = t.fontSize || 24, h = n.width || 0, f = t.transformData, u = c * t.text.length * 0.7, d = f.width ?? (f.radius ? f.radius * 2 : 0), p = Math.max(d, u), m = (n.feather || 0) * 2, w = h * 4 + m + c + 40, k = p + w, b = f.archHeight ? Math.abs(f.archHeight) * c * 2 : 0, T = f.radius ? f.radius : 0, g = c * 3 + w + b + T, y = e.getTransform(), S = Math.max(Math.abs(y.a), Math.abs(y.d), 1), C = Math.ceil(k * S / 2) * 2, P = Math.ceil(g * S / 2) * 2;
782
- if (C <= 0 || P <= 0) {
783
- r(e, t);
784
- return;
785
- }
786
- const I = Br(C, P), M = I.getContext("2d");
787
- if (!M) {
788
- r(e, t);
789
- return;
790
- }
791
- const A = t.x || 0, E = t.y || 0, v = y.a * A + y.c * E + y.e, R = y.b * A + y.d * E + y.f, U = Math.round(v - C / 2), W = Math.round(R - P / 2);
792
- if (M.setTransform(
793
- y.a,
794
- y.b,
795
- y.c,
796
- y.d,
797
- C / 2 - v + y.e,
798
- P / 2 - R + y.f
799
- ), l) {
800
- M.globalAlpha = 1;
801
- const $ = n.opacity;
802
- a && (n.opacity = 1), r(M, t), a && (n.opacity = $), e.save(), e.resetTransform(), e.globalAlpha = i, e.drawImage(I, U, W), e.restore();
803
- return;
804
- }
805
- const F = n.opacity;
806
- n.opacity = 1, r(M, t), n.opacity = F;
807
- const B = n.enabled;
808
- n.enabled = !1, r(e, t), n.enabled = B, e.save(), e.resetTransform(), e.globalAlpha = o, e.drawImage(
809
- I,
810
- U,
811
- W
812
- ), e.restore();
813
- }
814
- function Re(e, t) {
815
- et(e, t, Or);
816
- }
817
- function Or(e, t) {
818
- const r = t.transformData;
819
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180), e.scale(r.scale, r.scale);
820
- const n = t.bold ? "bold" : "normal", o = t.italic ? "italic" : "normal";
821
- e.font = `${o} ${n} ${t.fontSize}px ${t.fontFamily}`, e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle";
822
- const i = t.text.split(""), a = i.map((s) => e.measureText(s).width), l = a.reduce((s, c) => s + c, 0);
823
- if (r.reverse) {
824
- let s = Math.PI / 2 + l / (2 * r.radius);
825
- i.forEach((c, h) => {
826
- const u = a[h] / r.radius, d = Math.cos(s - u / 2) * r.radius, p = Math.sin(s - u / 2) * r.radius;
827
- e.save(), e.translate(d, p), e.rotate(s - u / 2 - Math.PI / 2), tt(e, c, t.stroke), e.fillText(c, 0, 0), e.restore(), s -= u;
828
- });
829
- } else {
830
- let s = -Math.PI / 2 - l / (2 * r.radius);
831
- i.forEach((c, h) => {
832
- const u = a[h] / r.radius, d = Math.cos(s + u / 2) * r.radius, p = Math.sin(s + u / 2) * r.radius;
833
- e.save(), e.translate(d, p), e.rotate(s + u / 2 + Math.PI / 2), tt(e, c, t.stroke), e.fillText(c, 0, 0), e.restore(), s += u;
834
- });
835
- }
836
- e.restore();
837
- }
838
- function We(e, t) {
839
- et(e, t, Lr);
840
- }
841
- function Lr(e, t) {
842
- const r = t.transformData;
843
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180), e.font = q(t.fontSize, t.fontFamily, t.bold, t.italic), e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle";
844
- const n = t.text.split("");
845
- let i = -n.reduce((a, l) => a + e.measureText(l).width, 0) / 2;
846
- n.forEach((a) => {
847
- const l = e.measureText(a).width, s = i + l / 2, c = s / (r.width / 2), h = r.amplitude * t.fontSize * Math.sin(r.frequency * Math.PI * c), f = r.amplitude * r.frequency * Math.PI * Math.cos(r.frequency * Math.PI * c);
848
- e.save(), e.translate(s, h), e.transform(1, f * tr, 0, 1, 0, 0), tt(e, a, t.stroke), e.fillText(a, 0, 0), e.restore(), i += l;
849
- }), e.restore();
850
- }
851
- function Fe(e, t) {
852
- et(e, t, qr);
853
- }
854
- function qr(e, t) {
855
- const r = t.transformData;
856
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180), e.font = q(t.fontSize, t.fontFamily, t.bold, t.italic), e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle";
857
- const n = t.text.split(""), o = n.reduce((l, s) => l + e.measureText(s).width, 0), i = r.width / 2;
858
- let a = -o / 2;
859
- n.forEach((l) => {
860
- const s = e.measureText(l).width, c = a + s / 2, h = c / i, f = (Math.pow(h, 2) - 1) * r.archHeight * t.fontSize, u = 2 * h * r.archHeight;
861
- e.save(), e.translate(c, f), e.transform(1, u * 0.3, 0, 1, 0, 0), tt(e, l, t.stroke), e.fillText(l, 0, 0), e.restore(), a += s;
862
- }), e.restore();
863
- }
864
- function Ue(e, t) {
865
- et(e, t, $r);
866
- }
867
- function $r(e, t) {
868
- const r = t.transformData;
869
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180), e.font = q(t.fontSize, t.fontFamily, t.bold, t.italic), e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle";
870
- const n = t.text.split(""), o = n.reduce((l, s) => l + e.measureText(s).width, 0), i = r.ascendAngle * Math.PI / 180;
871
- let a = -o / 2;
872
- n.forEach((l) => {
873
- const s = e.measureText(l).width, c = a + s / 2, h = c * Math.tan(i), f = Math.tan(i);
874
- e.save(), e.translate(c, h), e.transform(1, f * 1, 0, 1, 0, 0), tt(e, l, t.stroke), e.fillText(l, 0, 0), e.restore(), a += s;
875
- }), e.restore();
876
- }
877
- function Be(e, t) {
878
- et(e, t, Hr);
879
- }
880
- function Hr(e, t) {
881
- var o;
882
- const r = t.transformData;
883
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180);
884
- const n = -r.leanAmount * Math.PI / 4;
885
- e.transform(1, 0, Math.tan(n), 1, 0, 0), e.font = q(t.fontSize, t.fontFamily, t.bold, t.italic), e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle", (o = t.stroke) != null && o.enabled && (e.save(), e.strokeStyle = t.stroke.color, e.lineWidth = t.stroke.width, e.lineCap = t.stroke.lineCap || "round", e.lineJoin = t.stroke.lineJoin || "round", t.stroke.opacity !== void 0 && (e.globalAlpha = t.stroke.opacity), e.strokeText(t.text, 0, 0), e.restore()), e.fillText(t.text, 0, 0), e.restore();
886
- }
887
- function Oe(e, t) {
888
- et(e, t, jr);
889
- }
890
- function jr(e, t) {
891
- const r = t.transformData;
892
- e.save(), e.translate(t.x, t.y), e.rotate(-t.rotation * Math.PI / 180), e.font = q(t.fontSize, t.fontFamily, t.bold, t.italic), e.fillStyle = t.color, e.textAlign = "center", e.textBaseline = "middle";
893
- const n = t.text.split("");
894
- let i = -n.reduce((a, l) => a + e.measureText(l).width, 0) / 2;
895
- n.forEach((a) => {
896
- const l = e.measureText(a).width, s = i + l / 2, c = s / (r.width / 2), h = Math.abs(c), f = r.amplitude * h, u = f * t.fontSize * Math.sin(r.frequency * Math.PI * c), d = f * r.frequency * Math.PI * Math.cos(r.frequency * Math.PI * c), p = r.amplitude * Math.sign(c) * Math.sin(r.frequency * Math.PI * c), m = d + p;
897
- e.save(), e.translate(s, u), e.transform(1, m * 0.3, 0, 1, 0, 0), tt(e, a, t.stroke), e.fillText(a, 0, 0), e.restore(), i += l;
898
- }), e.restore();
899
- }
900
- function Q(e) {
901
- return (t, r) => e(t, r);
902
- }
903
- function _r(e, t) {
904
- e.save();
905
- const r = e.canvas, n = r.width, o = r.height, i = e.getTransform(), a = [];
906
- let l = [];
907
- for (const s of t)
908
- s.isClipping ? l.length > 0 ? (a.push({ content: l, mask: s }), l = []) : a.push({ content: [s] }) : l.push(s);
909
- l.length > 0 && a.push({ content: l });
910
- for (const s of a)
911
- if (!s.mask)
912
- s.content.forEach((c) => c.render(e, !1, !1));
913
- else {
914
- const c = document.createElement("canvas");
915
- c.width = n, c.height = o;
916
- const h = c.getContext("2d");
917
- if (!h) continue;
918
- h.setTransform(i), s.content.forEach((d) => {
919
- d.render(h, !1, !1);
920
- });
921
- const f = document.createElement("canvas");
922
- f.width = n, f.height = o;
923
- const u = f.getContext("2d");
924
- if (!u) continue;
925
- if (u.setTransform(i), s.mask.render(u, !1, !1), s.mask.transformType !== "image" && s.mask.transformType !== "shape") {
926
- const d = s.mask.getBoundingBox();
927
- u.save(), u.fillStyle = "#FFFFFF", u.fillRect(d.x, d.y, d.width, d.height), u.restore();
928
- }
929
- h.globalCompositeOperation = "destination-in", h.setTransform(1, 0, 0, 1, 0, 0), h.drawImage(f, 0, 0), e.setTransform(1, 0, 0, 1, 0, 0), e.drawImage(c, 0, 0), e.setTransform(i);
930
- }
931
- e.restore();
932
- }
933
- var se = !1;
934
- function zt() {
935
- se || (se = !0, K("arch", Q(Fe)), K("ascend", Q(Ue)), K("circle", Q(Re)), K("flag", Q(Oe)), K("lean", Q(Be)), K("wave", Q(We)), K(
936
- "custom",
937
- (e, t) => Ut(e, t)
938
- ), er(_r), mr(
939
- (e, t) => kr(e, t)
940
- ), globalThis.__scElementPaintersInstalled = !0);
941
- }
942
- zt();
943
- function Zt(e, t, r = {}) {
944
- const n = r.mode || "both";
945
- switch (e.save(), e.translate(t.x || 0, t.y || 0), e.rotate(-(t.rotation || 0) * Math.PI / 180), t.transformType) {
946
- case "image":
947
- Xr(e, t, n, r);
948
- break;
949
- case "shape":
950
- Gr(e, t, n, r);
951
- break;
952
- case "group":
953
- break;
954
- default:
955
- Nr(e, t, n, r);
956
- break;
957
- }
958
- e.restore();
959
- }
960
- function Nr(e, t, r, n) {
961
- var a;
962
- const o = t, i = o.text || "";
963
- i && ((r === "stroke" || r === "both") && ((a = t.stroke) != null && a.enabled) && (e.save(), Ae(e, t, { ...n.renderingContext, positionApplied: !0 }), e.restore()), (r === "fill" || r === "both") && (e.save(), Rr(e, {
964
- text: i,
965
- fontSize: o.fontSize || 24,
966
- fontFamily: o.fontFamily || "Arial",
967
- bold: o.bold || !1,
968
- italic: o.italic || !1,
969
- textAlign: o.textAlign || "center",
970
- color: o.color || "#000000",
971
- letterSpacing: o.letterSpacing
972
- }), e.restore()));
973
- }
974
- function Xr(e, t, r, n) {
975
- var i;
976
- if (!t.transformData) return;
977
- const o = t.transformData;
978
- if (r === "fill" || r === "both")
979
- if (Qr(e, t), n.loadedImage) {
980
- e.save(), e.clip();
981
- const a = o.flipHorizontal ? -1 : 1, l = o.flipVertical ? -1 : 1;
982
- e.scale(a, l);
983
- const s = o.width ?? 100, c = o.height ?? 100;
984
- e.drawImage(n.loadedImage, -s / 2, -c / 2, s, c), e.restore();
985
- } else
986
- e.fillStyle = "#cccccc", e.fill();
987
- (r === "stroke" || r === "both") && ((i = t.stroke) != null && i.enabled) && (e.save(), Cr(e, t, n.renderingContext), e.restore());
988
- }
989
- function Gr(e, t, r, n) {
990
- var a, l;
991
- if (!t.transformData) return;
992
- const o = t.transformData, i = ((a = n.renderingContext) == null ? void 0 : a.isKnockout) === !0;
993
- e.beginPath(), Le(e, o), (r === "fill" || r === "both") && (e.fillStyle = i ? "#000000" : rr(e, o), e.globalAlpha = i ? 1 : o.fillOpacity ?? 1, e.fill()), (r === "stroke" || r === "both") && ((l = t.stroke) != null && l.enabled) && (e.save(), Jr(e, t, n.renderingContext), e.restore());
994
- }
995
- function Le(e, t) {
996
- const { shapeType: r } = t, n = t.width ?? 100, o = t.height ?? 100;
997
- switch (r) {
998
- case "rectangle":
999
- ie(e, t, n, o);
1000
- break;
1001
- case "circle":
1002
- Yr(e, Math.min(n, o) / 2);
1003
- break;
1004
- case "ellipse":
1005
- Vr(e, n / 2, o / 2);
1006
- break;
1007
- case "star":
1008
- Kr(e, t, Math.min(n, o) / 2);
1009
- break;
1010
- default:
1011
- ie(e, t, n, o);
1012
- }
1013
- }
1014
- function ie(e, t, r, n) {
1015
- const o = t.borderRadius || 0, i = -r / 2, a = -n / 2;
1016
- if (o > 0) {
1017
- const l = Math.min(o / 100 * Math.min(r, n), r / 2, n / 2);
1018
- e.roundRect(i, a, r, n, l);
1019
- } else
1020
- e.rect(i, a, r, n);
1021
- }
1022
- function Yr(e, t) {
1023
- e.arc(0, 0, t, 0, Math.PI * 2);
1024
- }
1025
- function Vr(e, t, r) {
1026
- e.ellipse(0, 0, t, r, 0, 0, Math.PI * 2);
1027
- }
1028
- function Kr(e, t, r) {
1029
- const n = Math.max(3, Math.min(20, t.points || 5)), o = Math.max(0.1, Math.min(0.9, t.innerRadius || 0.4)), i = r * o, a = Math.PI / n, l = -Math.PI / 2;
1030
- for (let s = 0; s < n * 2; s++) {
1031
- const c = l + a * s, h = s % 2 === 0 ? r : i, f = Math.cos(c) * h, u = Math.sin(c) * h;
1032
- s === 0 ? e.moveTo(f, u) : e.lineTo(f, u);
1033
- }
1034
- e.closePath();
1035
- }
1036
- function Jr(e, t, r) {
1037
- var o;
1038
- if (!((o = t.stroke) != null && o.enabled) || !t.transformData) return;
1039
- const n = (r == null ? void 0 : r.isKnockout) === !0;
1040
- e.save(), n ? (e.strokeStyle = "#000000", e.lineWidth = t.stroke.width, e.lineCap = t.stroke.lineCap || "butt", e.lineJoin = t.stroke.lineJoin || "miter", e.globalAlpha = 1) : (e.strokeStyle = t.stroke.color, e.lineWidth = t.stroke.width, e.lineCap = t.stroke.lineCap || "butt", e.lineJoin = t.stroke.lineJoin || "miter", t.stroke.opacity !== void 0 && (e.globalAlpha = t.stroke.opacity)), t.stroke.miterLimit !== void 0 && (e.miterLimit = t.stroke.miterLimit), t.stroke.dashArray && t.stroke.dashArray.length > 0 && e.setLineDash(t.stroke.dashArray), !n && t.stroke.feather && t.stroke.feather > 0 && "filter" in e && (e.filter = `blur(${t.stroke.feather}px)`), e.beginPath(), Le(e, t.transformData), e.stroke(), e.restore();
1041
- }
1042
- function Qr(e, t) {
1043
- if (!t.transformData) return;
1044
- const { borderRadius: r } = t.transformData, n = t.transformData.width ?? 100, o = t.transformData.height ?? 100, i = -n / 2, a = -o / 2;
1045
- if (e.beginPath(), r && r > 0) {
1046
- const l = Math.min(r / 100 * Math.min(n, o), n / 2, o / 2);
1047
- e.roundRect(i, a, n, o, l);
1048
- } else
1049
- e.rect(i, a, n, o);
1050
- }
1051
- function qe(e, t, r, n, o, i, a = "destination-out") {
1052
- var T;
1053
- if (!t.knockoutParts) return;
1054
- if (!(t.knockoutParts.fill || t.knockoutParts.stroke)) {
1055
- n(e, t);
1056
- return;
1057
- }
1058
- const s = e.getTransform();
1059
- e.save();
1060
- const c = t.knockoutParts.scope || "group", h = zr(t, c, r);
1061
- if (h.length === 0 && c === "artboard") {
1062
- Dr(e, t, n), e.restore();
1063
- return;
1064
- }
1065
- if (h.length === 0) {
1066
- n(e, t), e.restore();
1067
- return;
1068
- }
1069
- const f = e.canvas, d = e.getTransform().a, p = xr(f.width, f.height), m = p.getContext("2d");
1070
- if (!m) return;
1071
- m.scale(d, d), o && i && (m.fillStyle = o, m.fillRect(i.x, i.y, i.width, i.height));
1072
- for (const g of h)
1073
- n(m, g);
1074
- t.knockoutParts.fill && !t.knockoutParts.stroke && ((T = t.stroke) != null && T.enabled) && (m.save(), Lt(m, t), m.restore()), m.save(), m.globalCompositeOperation = a, t.knockoutParts.fill && Ot(m, t), t.knockoutParts.stroke && Lt(m, t), m.restore(), t.knockoutParts.fill || (m.save(), Ot(m, t), m.restore());
1075
- const w = f.width / d, k = f.height / d;
1076
- e.drawImage(p, 0, 0, w, k), e.restore();
1077
- const b = e.getTransform();
1078
- (Math.abs(b.a - s.a) > 1e-3 || Math.abs(b.d - s.d) > 1e-3 || Math.abs(b.e - s.e) > 1e-3 || Math.abs(b.f - s.f) > 1e-3) && e.setTransform(
1079
- s.a,
1080
- s.b,
1081
- s.c,
1082
- s.d,
1083
- s.e,
1084
- s.f
1085
- );
1086
- }
1087
- function ao(e, t, r, n, o, i) {
1088
- qe(e, t, r, n, o, i, "destination-out");
1089
- }
1090
- function zr(e, t, r) {
1091
- return t === "artboard" ? $e(e, r) : Zr(e, r);
1092
- }
1093
- function $e(e, t) {
1094
- const r = t.findIndex((n) => n.id === e.id);
1095
- return r === -1 ? [] : t.slice(0, r);
1096
- }
1097
- function Zr(e, t) {
1098
- const r = He(e, t);
1099
- if (!r)
1100
- return $e(e, t);
1101
- if (r.transformType !== "group" || !r.children)
1102
- return [];
1103
- const n = r.children.findIndex((o) => o.id === e.id);
1104
- return n === -1 ? [] : r.children.slice(0, n);
1105
- }
1106
- function He(e, t) {
1107
- for (const r of t)
1108
- if (r.transformType === "group" && r.children) {
1109
- if (r.children.some((o) => o.id === e.id))
1110
- return r;
1111
- const n = He(e, r.children);
1112
- if (n)
1113
- return n;
1114
- }
1115
- return null;
1116
- }
1117
- function Ot(e, t, r) {
1118
- Zt(e, t, {
1119
- mode: "fill",
1120
- renderingContext: { isKnockout: !0 }
1121
- });
1122
- }
1123
- function Lt(e, t) {
1124
- var r;
1125
- (r = t.stroke) != null && r.enabled && Zt(e, t, {
1126
- mode: "stroke",
1127
- renderingContext: { isKnockout: !0 }
1128
- });
1129
- }
1130
- function Dr(e, t, r) {
1131
- var o;
1132
- const n = t.knockoutParts;
1133
- if (!n) {
1134
- r(e, t);
1135
- return;
1136
- }
1137
- n.fill || (e.save(), Ot(e, t), e.restore()), !n.stroke && ((o = t.stroke) != null && o.enabled) && (e.save(), Lt(e, t), e.restore());
1138
- }
1139
- function xr(e, t) {
1140
- if (typeof OffscreenCanvas < "u")
1141
- return new OffscreenCanvas(e, t);
1142
- if (typeof document < "u") {
1143
- const r = document.createElement("canvas");
1144
- return r.width = e, r.height = t, r;
1145
- }
1146
- throw new Error("No canvas context available");
1147
- }
1148
- function le(e, t, r, n, o = 0) {
1149
- if (r <= 0 || n <= 0 || e <= 0 || t <= 0)
1150
- return { width: e + o, height: t + o };
1151
- const i = r / n, a = e / t;
1152
- let l, s;
1153
- return a > i ? (s = t, l = t * i) : (l = e, s = e / i), {
1154
- width: l + o,
1155
- height: s + o
1156
- };
1157
- }
1158
- const nt = 64, Pt = 512;
1159
- function tn(e, t) {
1160
- e.beginPath(), e.arc(t / 2, t / 2, t * 0.45, 0, Math.PI * 2), e.fill();
1161
- }
1162
- function en(e, t) {
1163
- const r = t / 2, n = t / 2, o = t * 0.45, i = o * 0.4, a = 5;
1164
- e.beginPath();
1165
- for (let l = 0; l < a * 2; l++) {
1166
- const s = -Math.PI / 2 + Math.PI / a * l, c = l % 2 === 0 ? o : i, h = r + Math.cos(s) * c, f = n + Math.sin(s) * c;
1167
- l === 0 ? e.moveTo(h, f) : e.lineTo(h, f);
1168
- }
1169
- e.closePath(), e.fill();
1170
- }
1171
- function rn(e, t) {
1172
- const r = t / 2, n = t * 0.42;
1173
- e.beginPath(), e.moveTo(r, t * 0.82), e.bezierCurveTo(r - n * 1.1, t * 0.6, r - n * 1.3, t * 0.2, r, t * 0.35), e.bezierCurveTo(r + n * 1.3, t * 0.2, r + n * 1.1, t * 0.6, r, t * 0.82), e.closePath(), e.fill();
1174
- }
1175
- function nn(e, t) {
1176
- const r = t / 2, n = t / 2, o = t * 0.4, i = t * 0.48;
1177
- e.beginPath(), e.moveTo(r, n - i), e.lineTo(r + o, n), e.lineTo(r, n + i), e.lineTo(r - o, n), e.closePath(), e.fill();
1178
- }
1179
- function on(e, t) {
1180
- const r = t / 2, n = t / 2, o = t * 0.44;
1181
- e.beginPath();
1182
- for (let i = 0; i < 6; i++) {
1183
- const a = -Math.PI / 2 + Math.PI / 3 * i, l = r + Math.cos(a) * o, s = n + Math.sin(a) * o;
1184
- i === 0 ? e.moveTo(l, s) : e.lineTo(l, s);
1185
- }
1186
- e.closePath(), e.fill();
1187
- }
1188
- function an(e, t) {
1189
- const r = t / 2, n = t * 0.4, o = t * 0.12;
1190
- e.beginPath(), e.moveTo(r, o), e.lineTo(r + n, t * 0.25), e.lineTo(r + n, t * 0.55), e.quadraticCurveTo(r + n * 0.3, t * 0.8, r, t * 0.9), e.quadraticCurveTo(r - n * 0.3, t * 0.8, r - n, t * 0.55), e.lineTo(r - n, t * 0.25), e.closePath(), e.fill();
1191
- }
1192
- function sn(e, t) {
1193
- const r = t / 2, n = t / 2;
1194
- let o = 42;
1195
- const i = () => (o = (o * 16807 + 0) % 2147483647, o / 2147483647);
1196
- e.beginPath(), e.arc(r, n, t * 0.28, 0, Math.PI * 2), e.fill();
1197
- for (let a = 0; a < 30; a++) {
1198
- const l = i() * Math.PI * 2, s = t * 0.15 + i() * t * 0.3, c = t * 0.02 + i() * t * 0.06;
1199
- e.beginPath(), e.arc(r + Math.cos(l) * s, n + Math.sin(l) * s, c, 0, Math.PI * 2), e.fill();
1200
- }
1201
- }
1202
- function ln(e, t) {
1203
- let r = 123;
1204
- const n = () => (r = (r * 16807 + 0) % 2147483647, r / 2147483647), o = t * 0.08, i = 40;
1205
- e.beginPath(), e.moveTo(o, o);
1206
- for (let a = 1; a <= i; a++) {
1207
- const l = o + (t - 2 * o) * (a / i), s = o + (n() - 0.5) * t * 0.08;
1208
- e.lineTo(l, s);
1209
- }
1210
- for (let a = 1; a <= i; a++) {
1211
- const l = t - o + (n() - 0.5) * t * 0.08, s = o + (t - 2 * o) * (a / i);
1212
- e.lineTo(l, s);
1213
- }
1214
- for (let a = i; a >= 0; a--) {
1215
- const l = o + (t - 2 * o) * (a / i), s = t - o + (n() - 0.5) * t * 0.08;
1216
- e.lineTo(l, s);
1217
- }
1218
- for (let a = i; a >= 0; a--) {
1219
- const l = o + (n() - 0.5) * t * 0.08, s = o + (t - 2 * o) * (a / i);
1220
- e.lineTo(l, s);
1221
- }
1222
- e.closePath(), e.fill();
1223
- }
1224
- function cn(e, t) {
1225
- let r = 77;
1226
- const n = () => (r = (r * 16807 + 0) % 2147483647, r / 2147483647), o = t * 0.25, i = t * 0.75, a = t * 0.1;
1227
- e.beginPath(), e.moveTo(a, o + n() * t * 0.05);
1228
- for (let l = 1; l <= 10; l++) {
1229
- const s = a + (t - 2 * a) * (l / 10), c = o + (n() - 0.5) * t * 0.1;
1230
- e.lineTo(s, c);
1231
- }
1232
- for (let l = 10; l >= 0; l--) {
1233
- const s = a + (t - 2 * a) * (l / 10), c = i + (n() - 0.5) * t * 0.1;
1234
- e.lineTo(s, c);
1235
- }
1236
- e.closePath(), e.fill();
1237
- }
1238
- function hn(e, t) {
1239
- const r = t / 2, n = t * 0.55;
1240
- e.beginPath(), e.arc(r, n, t * 0.2, 0, Math.PI * 2), e.fill(), e.beginPath(), e.arc(r - t * 0.18, n + t * 0.02, t * 0.15, 0, Math.PI * 2), e.fill(), e.beginPath(), e.arc(r + t * 0.18, n + t * 0.02, t * 0.16, 0, Math.PI * 2), e.fill(), e.beginPath(), e.arc(r - t * 0.06, n - t * 0.14, t * 0.14, 0, Math.PI * 2), e.fill(), e.beginPath(), e.arc(r + t * 0.1, n - t * 0.12, t * 0.12, 0, Math.PI * 2), e.fill();
1241
- }
1242
- const fn = [
1243
- // Geometric
1244
- { id: "circle", name: "Circle", description: "Perfect circle", imageUrl: "builtin-mask:circle", thumbnailUrl: "", category: "geometric", draw: tn },
1245
- { id: "star", name: "Star", description: "5-pointed star", imageUrl: "builtin-mask:star", thumbnailUrl: "", category: "geometric", draw: en },
1246
- { id: "heart", name: "Heart", description: "Heart shape", imageUrl: "builtin-mask:heart", thumbnailUrl: "", category: "geometric", draw: rn },
1247
- { id: "diamond", name: "Diamond", description: "Diamond/rhombus", imageUrl: "builtin-mask:diamond", thumbnailUrl: "", category: "geometric", draw: nn },
1248
- { id: "hexagon", name: "Hexagon", description: "Regular hexagon", imageUrl: "builtin-mask:hexagon", thumbnailUrl: "", category: "geometric", draw: on },
1249
- { id: "shield", name: "Shield", description: "Shield/crest shape", imageUrl: "builtin-mask:shield", thumbnailUrl: "", category: "geometric", draw: an },
1250
- // Organic
1251
- { id: "splatter", name: "Splatter", description: "Ink splatter", imageUrl: "builtin-mask:splatter", thumbnailUrl: "", category: "organic", draw: sn },
1252
- { id: "torn-edge", name: "Torn Edge", description: "Rough torn paper edge", imageUrl: "builtin-mask:torn-edge", thumbnailUrl: "", category: "organic", draw: ln },
1253
- { id: "paint-stroke", name: "Paint Stroke", description: "Horizontal brush stroke", imageUrl: "builtin-mask:paint-stroke", thumbnailUrl: "", category: "organic", draw: cn },
1254
- { id: "cloud", name: "Cloud", description: "Soft cloud shape", imageUrl: "builtin-mask:cloud", thumbnailUrl: "", category: "organic", draw: hn }
1255
- ];
1256
- let ce = !1;
1257
- function he(e, t) {
1258
- if (typeof document > "u") return null;
1259
- const r = document.createElement("canvas");
1260
- return r.width = e, r.height = t, r;
1261
- }
1262
- function dn() {
1263
- if (typeof document > "u" || ce) return;
1264
- ce = !0;
1265
- const e = vt.getInstance();
1266
- for (const t of fn) {
1267
- const r = he(Pt, Pt);
1268
- if (!r) continue;
1269
- const n = r.getContext("2d");
1270
- if (!n) continue;
1271
- n.fillStyle = "white", t.draw(n, Pt);
1272
- const o = r.toDataURL("image/png"), i = e.acquire(t.imageUrl);
1273
- i.crossOrigin = "anonymous", i.src = o;
1274
- const a = he(nt, nt);
1275
- if (a) {
1276
- const l = a.getContext("2d");
1277
- l && (l.fillStyle = "#1a1a2e", l.fillRect(0, 0, nt, nt), l.fillStyle = "white", t.draw(l, nt), t.thumbnailUrl = a.toDataURL("image/png"));
1278
- }
1279
- }
1280
- }
1281
- const un = j("MaskRenderer"), qt = /* @__PURE__ */ new Map(), gn = /* @__PURE__ */ new Map();
1282
- function so(e) {
1283
- const t = [];
1284
- qt.forEach((r, n) => {
1285
- n.startsWith(e) && t.push(n);
1286
- }), t.forEach((r) => qt.delete(r));
1287
- }
1288
- function io() {
1289
- qt.clear();
1290
- }
1291
- function fe(e, t, r) {
1292
- if (!t.masks || t.masks.length === 0) {
1293
- r(e);
1294
- return;
1295
- }
1296
- const n = e.canvas, o = mt(n.width, n.height), i = o.getContext("2d");
1297
- if (!i) {
1298
- un.error("[renderWithMasks] Failed to get offscreen context");
1299
- return;
1300
- }
1301
- const a = e.getTransform();
1302
- i.setTransform(a), r(i);
1303
- for (const l of t.masks)
1304
- pn(i, l, t);
1305
- e.save(), e.setTransform(1, 0, 0, 1, 0, 0), e.drawImage(o, 0, 0), e.restore();
1306
- }
1307
- function pn(e, t, r) {
1308
- switch (t.type) {
1309
- case "clip":
1310
- mn(e, t, r);
1311
- break;
1312
- case "alpha":
1313
- yn(e, t, r);
1314
- break;
1315
- case "luma":
1316
- wn(e, t, r);
1317
- break;
1318
- case "distress":
1319
- bn(e, t, r);
1320
- break;
1321
- }
1322
- }
1323
- function mn(e, t, r) {
1324
- e.save(), e.globalCompositeOperation = t.inverted ? "destination-out" : "destination-in", t.opacity !== void 0 && (e.globalAlpha = t.opacity), Mt(e, t.maskElement, r), e.restore();
1325
- }
1326
- function yn(e, t, r) {
1327
- e.save(), e.globalCompositeOperation = t.inverted ? "destination-out" : "destination-in", t.opacity !== void 0 && (e.globalAlpha = t.opacity), Mt(e, t.maskElement, r), t.feather && t.feather > 0 && je(e, t.feather), e.restore();
1328
- }
1329
- function wn(e, t, r) {
1330
- const n = mt(e.canvas.width, e.canvas.height), o = n.getContext("2d");
1331
- if (!o) return;
1332
- Mt(o, t.maskElement, r);
1333
- const i = o.getImageData(0, 0, n.width, n.height), a = i.data;
1334
- for (let l = 0; l < a.length; l += 4) {
1335
- const s = a[l], c = a[l + 1], h = a[l + 2], f = 0.299 * s + 0.587 * c + 0.114 * h;
1336
- a[l + 3] = t.inverted ? 255 - f : f;
1337
- }
1338
- o.putImageData(i, 0, 0), e.save(), e.globalCompositeOperation = "destination-in", t.opacity !== void 0 && (e.globalAlpha = t.opacity), e.drawImage(n, 0, 0), e.restore();
1339
- }
1340
- function bn(e, t, r) {
1341
- e.save(), e.globalCompositeOperation = t.inverted ? "destination-in" : "destination-out", t.opacity !== void 0 && (e.globalAlpha = t.opacity), Mt(e, t.maskElement, r), t.feather && t.feather > 0 && je(e, t.feather), e.restore();
1342
- }
1343
- function kn(e) {
1344
- const t = e.transformData, r = e, n = t && t.type === "image" ? dr(t, Ce(e)) : null;
1345
- return {
1346
- width: (n == null ? void 0 : n.width) || (t == null ? void 0 : t.width) || r.width || 0,
1347
- height: (n == null ? void 0 : n.height) || (t == null ? void 0 : t.height) || r.height || 0
1348
- };
1349
- }
1350
- function Mt(e, t, r) {
1351
- const n = r.x || 0, o = r.y || 0, i = t.x || 0, a = t.y || 0, l = {
1352
- ...t,
1353
- x: n + i,
1354
- y: o + a,
1355
- // Inherit parent rotation so mask rotates with the element
1356
- rotation: (t.rotation || 0) + (r.rotation || 0)
1357
- }, s = 4, { width: c, height: h } = kn(r), f = t.transformData;
1358
- c && h && f && (l.transformData = {
1359
- ...f,
1360
- width: c + s,
1361
- height: h + s
1362
- });
1363
- let u, d;
1364
- if (t.transformType === "image") {
1365
- const b = t;
1366
- if (b.imageUrl)
1367
- if (typeof document < "u") {
1368
- b.imageUrl.startsWith("builtin-mask:") && dn();
1369
- const T = vt.getInstance(), g = T.acquire(b.imageUrl);
1370
- g.complete && g.naturalWidth > 0 && (u = g), T.release(b.imageUrl);
1371
- } else
1372
- d = gn.get(b.imageUrl);
1373
- }
1374
- const p = l.transformData, m = (f == null ? void 0 : f.width) || 0, w = (f == null ? void 0 : f.height) || 0, k = m > 0 && w > 0 && c > 0 && h > 0 && Math.abs(m / w - c / h) > 1e-3;
1375
- if (d) {
1376
- e.save();
1377
- const b = (p == null ? void 0 : p.width) || d.width, T = (p == null ? void 0 : p.height) || d.height;
1378
- if (e.translate(l.x || 0, l.y || 0), e.rotate(-(l.rotation || 0) * Math.PI / 180), k) {
1379
- const g = le(
1380
- b,
1381
- T,
1382
- m,
1383
- w,
1384
- 0
1385
- );
1386
- e.drawImage(d, -g.width / 2, -g.height / 2, g.width, g.height);
1387
- } else
1388
- e.drawImage(d, -b / 2, -T / 2, b, T);
1389
- e.restore();
1390
- } else if (k && u) {
1391
- const b = (p == null ? void 0 : p.width) || c, T = (p == null ? void 0 : p.height) || h, g = le(
1392
- b,
1393
- T,
1394
- m,
1395
- w,
1396
- 0
1397
- );
1398
- e.save(), e.translate(l.x || 0, l.y || 0), e.rotate(-(l.rotation || 0) * Math.PI / 180), e.drawImage(u, -g.width / 2, -g.height / 2, g.width, g.height), e.restore();
1399
- } else
1400
- Zt(e, l, { mode: "both", loadedImage: u });
1401
- }
1402
- function je(e, t) {
1403
- "filter" in e && (e.filter = `blur(${t}px)`);
1404
- }
1405
- function mt(e, t) {
1406
- if (typeof OffscreenCanvas < "u")
1407
- return new OffscreenCanvas(e, t);
1408
- if (typeof document < "u") {
1409
- const r = document.createElement("canvas");
1410
- return r.width = e, r.height = t, r;
1411
- }
1412
- throw new Error("No canvas context available");
1413
- }
1414
- function Tn(e, t, r, n) {
1415
- if (!t.enabled || !t.imageUrl || t.opacity <= 0) return;
1416
- const o = vt.getInstance(), i = o.acquire(t.imageUrl);
1417
- if (!i.complete || i.naturalWidth === 0) {
1418
- o.release(t.imageUrl);
1419
- return;
1420
- }
1421
- const { x: a, y: l, width: s, height: c } = r, h = e.getTransform(), f = h.a * a + h.e, u = h.d * l + h.f, d = h.a * s, p = h.d * c, m = Math.ceil(d), w = Math.ceil(p);
1422
- if (m <= 0 || w <= 0) {
1423
- o.release(t.imageUrl);
1424
- return;
1425
- }
1426
- const k = mt(m, w), b = k.getContext("2d");
1427
- if (!b) {
1428
- o.release(t.imageUrl);
1429
- return;
1430
- }
1431
- b.drawImage(
1432
- e.canvas,
1433
- f,
1434
- u,
1435
- d,
1436
- p,
1437
- 0,
1438
- 0,
1439
- m,
1440
- w
1441
- );
1442
- let T;
1443
- if (t.maskType === "luma" ? T = "destination-in" : T = t.inverted ? "destination-out" : "destination-in", t.maskType === "luma") {
1444
- const g = mt(m, w), y = g.getContext("2d");
1445
- if (y) {
1446
- y.drawImage(i, 0, 0, m, w);
1447
- const S = y.getImageData(0, 0, m, w), C = S.data;
1448
- for (let P = 0; P < C.length; P += 4) {
1449
- const I = 0.299 * C[P] + 0.587 * C[P + 1] + 0.114 * C[P + 2];
1450
- C[P + 3] = t.inverted ? 255 - I : I;
1451
- }
1452
- y.putImageData(S, 0, 0), b.globalCompositeOperation = "destination-in", b.globalAlpha = t.opacity / 100, b.drawImage(g, 0, 0);
1453
- }
1454
- } else
1455
- b.globalCompositeOperation = T, b.globalAlpha = t.opacity / 100, b.drawImage(i, 0, 0, m, w);
1456
- b.globalCompositeOperation = "source-over", b.globalAlpha = 1, e.save(), e.setTransform(1, 0, 0, 1, 0, 0), e.clearRect(f, u, d, p), n && (e.fillStyle = n, e.fillRect(f, u, d, p)), e.drawImage(k, f, u), e.restore(), o.release(t.imageUrl);
1457
- }
1458
- function vn(e) {
1459
- var t;
1460
- return e ? ((t = e.shadow) == null ? void 0 : t.enabled) === !0 || Se(e.blendMode) : !1;
1461
- }
1462
- function Mn(e, t, r) {
1463
- var c;
1464
- if (!vn(t)) {
1465
- r(e);
1466
- return;
1467
- }
1468
- const n = (c = t.shadow) != null && c.enabled ? t.shadow : void 0, o = Se(t.blendMode) ? t.blendMode : void 0, i = e.canvas, a = e.getTransform(), l = Sn(i.width, i.height), s = l.getContext("2d");
1469
- if (!s)
1470
- throw new Error(
1471
- "[canvas] renderWithLayerEffects: could not acquire a 2D context for the layer-effects raster"
1472
- );
1473
- if (s.setTransform(a), r(s), e.save(), e.setTransform(1, 0, 0, 1, 0, 0), o && (e.globalCompositeOperation = o), n) {
1474
- const h = Math.hypot(a.a, a.b) || 1, f = i.width + 1;
1475
- e.save(), e.globalAlpha = e.globalAlpha * En(n.opacity ?? 1), e.shadowColor = n.color, e.shadowBlur = n.blur * h, e.shadowOffsetX = n.offsetX * h + f, e.shadowOffsetY = n.offsetY * h, e.drawImage(l, -f, 0), e.restore();
1476
- }
1477
- e.drawImage(l, 0, 0), e.restore();
1478
- }
1479
- function En(e) {
1480
- return Math.min(1, Math.max(0, e));
1481
- }
1482
- function Sn(e, t) {
1483
- if (typeof OffscreenCanvas < "u")
1484
- return new OffscreenCanvas(e, t);
1485
- if (typeof document < "u") {
1486
- const r = document.createElement("canvas");
1487
- return r.width = e, r.height = t, r;
1488
- }
1489
- throw new Error("No canvas context available");
1490
- }
1491
- class Cn {
1492
- constructor(t = Math.random()) {
1493
- this.perm = [];
1494
- for (let n = 0; n < 256; n++)
1495
- this.perm[n] = n;
1496
- const r = this.seededRandom(t);
1497
- for (let n = 255; n > 0; n--) {
1498
- const o = Math.floor(r() * (n + 1));
1499
- [this.perm[n], this.perm[o]] = [this.perm[o], this.perm[n]];
1500
- }
1501
- for (let n = 0; n < 256; n++)
1502
- this.perm[256 + n] = this.perm[n];
1503
- }
1504
- seededRandom(t) {
1505
- let r = Math.sin(t++) * 1e4;
1506
- return () => (r = Math.sin(t++) * 1e4, r - Math.floor(r));
1507
- }
1508
- fade(t) {
1509
- return t * t * t * (t * (t * 6 - 15) + 10);
1510
- }
1511
- lerp(t, r, n) {
1512
- return r + t * (n - r);
1513
- }
1514
- grad(t, r, n) {
1515
- const o = t & 15, i = o < 8 ? r : n, a = o < 4 ? n : o === 12 || o === 14 ? r : 0;
1516
- return ((o & 1) === 0 ? i : -i) + ((o & 2) === 0 ? a : -a);
1517
- }
1518
- noise(t, r) {
1519
- const n = Math.floor(t) & 255, o = Math.floor(r) & 255;
1520
- t -= Math.floor(t), r -= Math.floor(r);
1521
- const i = this.fade(t), a = this.fade(r), l = this.perm[n] + o, s = this.perm[n + 1] + o;
1522
- return this.lerp(
1523
- a,
1524
- this.lerp(i, this.grad(this.perm[l], t, r), this.grad(this.perm[s], t - 1, r)),
1525
- this.lerp(i, this.grad(this.perm[l + 1], t, r - 1), this.grad(this.perm[s + 1], t - 1, r - 1))
1526
- );
1527
- }
1528
- }
1529
- function It(e, t, r, n = Date.now()) {
1530
- const a = Et(e, t).getContext("2d").createImageData(e, t), l = a.data, s = new Cn(n), c = 0.02, h = 1 - r / 100;
1531
- for (let f = 0; f < t; f++)
1532
- for (let u = 0; u < e; u++) {
1533
- const d = (f * e + u) * 4;
1534
- let p = 0;
1535
- p += s.noise(u * c, f * c) * 0.5, p += s.noise(u * c * 2, f * c * 2) * 0.25, p += s.noise(u * c * 4, f * c * 4) * 0.125, p = (p + 1) / 2;
1536
- const m = Math.min(u / e, (e - u) / e) * 2, w = Math.min(f / t, (t - f) / t) * 2, k = Math.min(m, w);
1537
- p *= 0.7 + k * 0.3;
1538
- const b = p > h ? 255 : 0;
1539
- l[d] = 255, l[d + 1] = 255, l[d + 2] = 255, l[d + 3] = b;
1540
- }
1541
- return a;
1542
- }
1543
- function Pn(e, t, r, n = Date.now()) {
1544
- const i = Et(e, t).getContext("2d");
1545
- i.clearRect(0, 0, e, t), i.strokeStyle = "#000000", i.lineWidth = 1 + r / 50, i.lineCap = "round";
1546
- const a = Dt(n), l = Math.floor(r / 100 * 30 + 5);
1547
- for (let s = 0; s < l; s++) {
1548
- const c = a() * e, h = a() * t;
1549
- $t(i, c, h, a() * Math.PI * 2, 50 + a() * 100, a, 0);
1550
- }
1551
- return i.getImageData(0, 0, e, t);
1552
- }
1553
- function $t(e, t, r, n, o, i, a) {
1554
- if (a > 3 || o < 10) return;
1555
- const l = t + Math.cos(n) * o, s = r + Math.sin(n) * o;
1556
- if (e.beginPath(), e.moveTo(t, r), e.lineTo(l, s), e.stroke(), i() > 0.5) {
1557
- const c = n + (i() - 0.5) * Math.PI / 2;
1558
- $t(e, l, s, c, o * 0.6, i, a + 1);
1559
- }
1560
- if (i() > 0.7) {
1561
- const c = n - (i() - 0.5) * Math.PI / 2;
1562
- $t(e, l, s, c, o * 0.6, i, a + 1);
1563
- }
1564
- }
1565
- function ut(e, t, r, n = Date.now()) {
1566
- const a = Et(e, t).getContext("2d").createImageData(e, t), l = a.data, s = Dt(n), c = r / 100 * 0.5;
1567
- for (let h = 0; h < t; h++)
1568
- for (let f = 0; f < e; f++) {
1569
- const u = (h * e + f) * 4, p = s() < c ? 255 : 0;
1570
- l[u] = 0, l[u + 1] = 0, l[u + 2] = 0, l[u + 3] = p;
1571
- }
1572
- return a;
1573
- }
1574
- function In(e, t, r, n = Date.now()) {
1575
- const i = Et(e, t).getContext("2d");
1576
- i.fillStyle = "#000000";
1577
- const a = Dt(n), l = 4 + r / 100 * 6, s = l * 2;
1578
- for (let c = 0; c < t; c += s)
1579
- for (let h = 0; h < e; h += s)
1580
- if (a() < r / 100) {
1581
- const f = l * (0.5 + a() * 0.5);
1582
- i.beginPath(), i.arc(h, c, f / 2, 0, Math.PI * 2), i.fill();
1583
- }
1584
- return i.getImageData(0, 0, e, t);
1585
- }
1586
- function Et(e, t) {
1587
- if (typeof OffscreenCanvas < "u")
1588
- return new OffscreenCanvas(e, t);
1589
- if (typeof document < "u") {
1590
- const r = document.createElement("canvas");
1591
- return r.width = e, r.height = t, r;
1592
- }
1593
- throw new Error("No canvas context available");
1594
- }
1595
- function Dt(e) {
1596
- let t = Math.sin(e++) * 1e4;
1597
- return () => (t = Math.sin(e++) * 1e4, t - Math.floor(t));
1598
- }
1599
- const H = 42, z = 512, Z = 64;
1600
- function Ht(e) {
1601
- const t = e.data;
1602
- for (let r = 0; r < t.length; r += 4)
1603
- t[r] = 255, t[r + 1] = 255, t[r + 2] = 255;
1604
- return e;
1605
- }
1606
- function An(e, t) {
1607
- const r = e.data, n = t.data;
1608
- for (let o = 0; o < r.length; o += 4)
1609
- r[o] = 255, r[o + 1] = 255, r[o + 2] = 255, r[o + 3] = Math.min(255, r[o + 3] + n[o + 3]);
1610
- return e;
1611
- }
1612
- const Rn = [
1613
- {
1614
- id: "grunge-heavy",
1615
- name: "Heavy Grunge",
1616
- textureUrl: "builtin:grunge-heavy",
1617
- thumbnailUrl: "",
1618
- description: "Dense random splatters",
1619
- generate: (e, t) => ut(e, t, 80, H)
1620
- },
1621
- {
1622
- id: "grunge-fine",
1623
- name: "Fine Grunge",
1624
- textureUrl: "builtin:grunge-fine",
1625
- thumbnailUrl: "",
1626
- description: "Sparse fine grain",
1627
- generate: (e, t) => ut(e, t, 25, H + 1)
1628
- },
1629
- {
1630
- id: "concrete",
1631
- name: "Concrete",
1632
- textureUrl: "builtin:concrete",
1633
- thumbnailUrl: "",
1634
- description: "Rough surface via Perlin noise",
1635
- generate: (e, t) => It(e, t, 55, H + 2)
1636
- },
1637
- {
1638
- id: "paper",
1639
- name: "Old Paper",
1640
- textureUrl: "builtin:paper",
1641
- thumbnailUrl: "",
1642
- description: "Subtle aging",
1643
- generate: (e, t) => It(e, t, 20, H + 3)
1644
- },
1645
- {
1646
- id: "rust",
1647
- name: "Rust",
1648
- textureUrl: "builtin:rust",
1649
- thumbnailUrl: "",
1650
- description: "Noise + splatter combined",
1651
- generate: (e, t) => {
1652
- const r = It(e, t, 45, H + 4), n = ut(e, t, 35, H + 5);
1653
- return An(Ht(r), Ht(n));
1654
- }
1655
- },
1656
- {
1657
- id: "scratches",
1658
- name: "Scratches",
1659
- textureUrl: "builtin:scratches",
1660
- thumbnailUrl: "",
1661
- description: "Branching crack lines",
1662
- generate: (e, t) => Pn(e, t, 55, H + 6)
1663
- },
1664
- {
1665
- id: "halftone",
1666
- name: "Halftone",
1667
- textureUrl: "builtin:halftone",
1668
- thumbnailUrl: "",
1669
- description: "Dot grid pattern",
1670
- generate: (e, t) => In(e, t, 55, H + 7)
1671
- },
1672
- {
1673
- id: "spray",
1674
- name: "Spray Paint",
1675
- textureUrl: "builtin:spray",
1676
- thumbnailUrl: "",
1677
- description: "Medium random splatter",
1678
- generate: (e, t) => ut(e, t, 55, H + 8)
1679
- }
1680
- ];
1681
- let de = !1;
1682
- function Wn(e, t) {
1683
- if (typeof OffscreenCanvas < "u")
1684
- return new OffscreenCanvas(e, t);
1685
- const r = document.createElement("canvas");
1686
- return r.width = e, r.height = t, r;
1687
- }
1688
- function ue(e, t) {
1689
- if (typeof document > "u") return null;
1690
- const r = document.createElement("canvas");
1691
- return r.width = e, r.height = t, r;
1692
- }
1693
- function Fn(e) {
1694
- const t = ue(e.width, e.height);
1695
- if (!t) return "";
1696
- const r = t.getContext("2d");
1697
- if (!r) return "";
1698
- r.putImageData(e, 0, 0);
1699
- const n = ue(Z, Z);
1700
- if (!n) return "";
1701
- const o = n.getContext("2d");
1702
- if (!o) return "";
1703
- o.drawImage(t, 0, 0, e.width, e.height, 0, 0, Z, Z);
1704
- const i = o.getImageData(0, 0, Z, Z), a = i.data;
1705
- for (let l = 0; l < a.length; l += 4) {
1706
- const s = a[l + 3], c = 255 - Math.round(s / 255 * 195);
1707
- a[l] = c, a[l + 1] = c, a[l + 2] = c, a[l + 3] = 255;
1708
- }
1709
- return o.putImageData(i, 0, 0), n.toDataURL("image/png");
1710
- }
1711
- function Un() {
1712
- if (!de) {
1713
- de = !0;
1714
- for (const e of Rn) {
1715
- const t = e.generate(z, z);
1716
- Ht(t);
1717
- const r = Wn(z, z), n = r.getContext("2d");
1718
- n && (n.putImageData(t, 0, 0), Bn(e.textureUrl, r, z, z)), typeof document < "u" && (e.thumbnailUrl = Fn(t));
1719
- }
1720
- }
1721
- }
1722
- const N = /* @__PURE__ */ new Set(), X = /* @__PURE__ */ new Map(), yt = /* @__PURE__ */ new Map();
1723
- function ge(e, t) {
1724
- if (typeof OffscreenCanvas < "u")
1725
- return new OffscreenCanvas(e, t);
1726
- const r = document.createElement("canvas");
1727
- return r.width = e, r.height = t, r;
1728
- }
1729
- function pe(e) {
1730
- return e.getContext("2d");
1731
- }
1732
- function me(e) {
1733
- const { width: t, height: r } = e, n = ge(t, r), o = pe(n);
1734
- if (!o) throw new Error("Failed to get 2d context for temp canvas");
1735
- o.drawImage(e, 0, 0);
1736
- const i = o.getImageData(0, 0, t, r), a = i.data;
1737
- for (let c = 0; c < a.length; c += 4) {
1738
- const h = a[c], f = a[c + 1], u = a[c + 2], p = 255 - (0.299 * h + 0.587 * f + 0.114 * u);
1739
- a[c] = 255, a[c + 1] = 255, a[c + 2] = 255, a[c + 3] = p;
1740
- }
1741
- const l = ge(t, r), s = pe(l);
1742
- if (!s) throw new Error("Failed to get 2d context for mask canvas");
1743
- return s.putImageData(i, 0, 0), { alphaMask: l, width: t, height: r };
1744
- }
1745
- function _e(e) {
1746
- const t = X.get(e);
1747
- if (t) return t;
1748
- if (e.startsWith("builtin:")) {
1749
- Un();
1750
- const r = X.get(e);
1751
- return r || null;
1752
- }
1753
- return N.has(e) || (N.add(e), Ne(e)), null;
1754
- }
1755
- function lo(e) {
1756
- return X.has(e) ? Promise.resolve() : new Promise((t) => {
1757
- if (X.has(e)) {
1758
- t();
1759
- return;
1760
- }
1761
- const r = yt.get(e) || [];
1762
- r.push(t), yt.set(e, r), N.has(e) || (N.add(e), Ne(e));
1763
- });
1764
- }
1765
- function Bn(e, t, r, n) {
1766
- X.has(e) || (X.set(e, { alphaMask: t, width: r, height: n }), ot(e));
1767
- }
1768
- function ot(e) {
1769
- const t = yt.get(e);
1770
- t && (t.forEach((r) => r()), yt.delete(e));
1771
- }
1772
- function Ne(e) {
1773
- if (typeof document < "u") {
1774
- const t = new Image();
1775
- t.crossOrigin = "anonymous", t.onload = () => {
1776
- const r = me(t);
1777
- X.set(e, r), N.delete(e), ot(e);
1778
- }, t.onerror = () => {
1779
- N.delete(e), ot(e);
1780
- }, t.src = e;
1781
- } else
1782
- fetch(e).then((t) => t.blob()).then((t) => createImageBitmap(t)).then((t) => {
1783
- const r = me(t);
1784
- X.set(e, r), N.delete(e), ot(e);
1785
- }).catch(() => {
1786
- N.delete(e), ot(e);
1787
- });
1788
- }
1789
- function Xe(e, t) {
1790
- if (typeof OffscreenCanvas < "u")
1791
- return new OffscreenCanvas(e, t);
1792
- const r = document.createElement("canvas");
1793
- return r.width = e, r.height = t, r;
1794
- }
1795
- function On(e, t, r, n) {
1796
- if (!t.enabled || !t.textureUrl || t.intensity <= 0) return;
1797
- const o = _e(t.textureUrl);
1798
- if (!o) return;
1799
- const { x: i, y: a, width: l, height: s } = r, c = e.getTransform(), h = c.a * i + c.e, f = c.d * a + c.f, u = c.a * l, d = c.d * s, p = Math.ceil(u), m = Math.ceil(d);
1800
- if (p <= 0 || m <= 0) return;
1801
- const w = Xe(p, m), k = w.getContext("2d");
1802
- if (!k) return;
1803
- k.drawImage(
1804
- e.canvas,
1805
- h,
1806
- f,
1807
- u,
1808
- d,
1809
- 0,
1810
- 0,
1811
- p,
1812
- m
1813
- );
1814
- const b = o.alphaMask.getContext("2d");
1815
- if (b) {
1816
- const T = t.intensity / 100, g = b.getImageData(0, 0, o.width, o.height).data, y = o.width, S = o.height, C = k.getImageData(0, 0, p, m), P = C.data;
1817
- for (let I = 0; I < m; I++) {
1818
- const M = Math.floor(I / m * S);
1819
- for (let A = 0; A < p; A++) {
1820
- const E = Math.floor(A / p * y), v = g[(M * y + E) * 4 + 3] / 255, R = (I * p + A) * 4;
1821
- P[R + 3] = Math.round(P[R + 3] * (1 - v * T));
1822
- }
1823
- }
1824
- k.putImageData(C, 0, 0);
1825
- } else
1826
- k.globalCompositeOperation = "destination-out", k.globalAlpha = t.intensity / 100, k.drawImage(o.alphaMask, 0, 0, p, m), k.globalCompositeOperation = "source-over";
1827
- k.globalAlpha = 1, e.save(), e.setTransform(1, 0, 0, 1, 0, 0), e.clearRect(h, f, u, d), n && (e.fillStyle = n, e.fillRect(h, f, u, d)), e.drawImage(w, h, f), e.restore();
1828
- }
1829
- function ye(e, t, r, n) {
1830
- if (!t.enabled || t.style !== "custom" || !t.textureUrl) return;
1831
- const o = _e(t.textureUrl);
1832
- if (!o) {
1833
- n(e);
1834
- return;
1835
- }
1836
- const i = t.textureOpacity ?? t.intensity / 100, a = e.getTransform(), l = Math.sqrt(r.width ** 2 + r.height ** 2), s = (l - r.width) / 2, c = (l - r.height) / 2, h = 20, f = s + h, u = c + h, d = {
1837
- x: r.x - f,
1838
- y: r.y - u,
1839
- width: r.width + f * 2,
1840
- height: r.height + u * 2
1841
- }, p = a.a * d.x + a.e, m = a.d * d.y + a.f, w = a.a * d.width, k = a.d * d.height, b = Math.ceil(w), T = Math.ceil(k);
1842
- if (b <= 0 || T <= 0) {
1843
- n(e);
1844
- return;
1845
- }
1846
- const g = Xe(b, T), y = g.getContext("2d");
1847
- if (!y) {
1848
- n(e);
1849
- return;
1850
- }
1851
- y.setTransform(
1852
- a.a,
1853
- a.b,
1854
- a.c,
1855
- a.d,
1856
- a.e - p,
1857
- a.f - m
1858
- ), n(y), y.setTransform(1, 0, 0, 1, 0, 0);
1859
- const S = o.alphaMask.getContext("2d");
1860
- if (S) {
1861
- const C = S.getImageData(0, 0, o.width, o.height).data, P = o.width, I = o.height, M = y.getImageData(0, 0, b, T), A = M.data;
1862
- for (let E = 0; E < T; E++) {
1863
- const v = Math.floor(E / T * I);
1864
- for (let R = 0; R < b; R++) {
1865
- const U = Math.floor(R / b * P), W = C[(v * P + U) * 4 + 3] / 255, F = (E * b + R) * 4;
1866
- A[F + 3] = Math.round(A[F + 3] * (1 - W * i));
1867
- }
1868
- }
1869
- y.putImageData(M, 0, 0);
1870
- } else
1871
- y.globalCompositeOperation = "destination-out", y.globalAlpha = i, y.drawImage(o.alphaMask, 0, 0, b, T), y.globalCompositeOperation = "source-over", y.globalAlpha = 1;
1872
- e.save(), e.setTransform(1, 0, 0, 1, 0, 0), e.drawImage(g, p, m), e.restore();
1873
- }
1874
- class wt {
1875
- /**
1876
- * Create an element instance from a JSON configuration
1877
- * TypeScript ensures all transform types are handled
1878
- *
1879
- * @param config - Element configuration object
1880
- * @returns BaseElement instance (TextElement, ImageElement, GroupElement, ShapeElement, or PathElement)
1881
- * @throws Error if transform type is unknown
1882
- */
1883
- static createFromJSON(t) {
1884
- const { transformType: r } = t;
1885
- switch (r) {
1886
- case "custom":
1887
- return new fr(t);
1888
- case "distort":
1889
- throw new Error("DistortTransform is not yet implemented");
1890
- case "circle":
1891
- return new hr(t);
1892
- case "lean":
1893
- return new cr(t);
1894
- case "arch":
1895
- return new lr(t);
1896
- case "ascend":
1897
- return new ir(t);
1898
- case "wave":
1899
- return new sr(t);
1900
- case "flag":
1901
- return new ar(t);
1902
- case "image":
1903
- return new Wt(t);
1904
- case "group": {
1905
- const o = (t.children || []).map(
1906
- (s) => wt.createFromJSON(s)
1907
- ), { children: i, ...a } = t, l = new Vt(a);
1908
- return o.forEach((s) => l.addChild(s)), l;
1909
- }
1910
- case "shape":
1911
- return new or(t);
1912
- case "path":
1913
- return new nr(t);
1914
- default:
1915
- const n = r;
1916
- throw new Error(`Unknown transform type: ${n}`);
1917
- }
1918
- }
1919
- /**
1920
- * Create multiple elements from an array of JSON configurations
1921
- * Useful for bulk deserialization (e.g., loading a document from DB)
1922
- *
1923
- * @param configs - Array of element configurations
1924
- * @returns Array of BaseElement instances
1925
- */
1926
- static createManyFromJSON(t) {
1927
- return t.map((r) => this.createFromJSON(r));
1928
- }
1929
- /**
1930
- * Type guard to check if an object is a valid TextElement instance
1931
- * Useful for runtime validation
1932
- *
1933
- * @param obj - Object to check
1934
- * @returns true if obj is a TextElement
1935
- */
1936
- static isTextElement(t) {
1937
- const r = t;
1938
- return !!t && typeof t == "object" && typeof r.id == "string" && typeof r.transformType == "string" && typeof r.getBoundingBox == "function" && typeof r.getVisualBoundingBox == "function" && typeof r.hitTest == "function" && typeof r.render == "function" && typeof r.toJSON == "function" && typeof r.clone == "function";
1939
- }
1940
- /**
1941
- * Validate an element configuration object
1942
- * Returns true if the config has all required properties
1943
- *
1944
- * @param config - Configuration to validate
1945
- * @returns true if valid
1946
- */
1947
- static isValidConfig(t) {
1948
- const r = t;
1949
- return !!t && typeof t == "object" && typeof r.transformType == "string" && (r.id === void 0 || typeof r.id == "string");
1950
- }
1951
- /**
1952
- * Create a default element of a given type
1953
- * Useful for UI "Add Element" buttons
1954
- *
1955
- * @param transformType - Type of transform to create
1956
- * @returns Default element configuration
1957
- */
1958
- static createDefaultConfig(t) {
1959
- const r = {
1960
- id: `${t}-${Date.now()}`,
1961
- x: window.innerWidth / 2,
1962
- y: window.innerHeight / 2,
1963
- rotation: 0
1964
- };
1965
- switch (t) {
1966
- case "custom":
1967
- return {
1968
- ...r,
1969
- transformType: "custom",
1970
- text: "Custom Text",
1971
- transformData: {
1972
- type: "custom",
1973
- controlPoints: []
1974
- }
1975
- };
1976
- case "distort":
1977
- return {
1978
- ...r,
1979
- transformType: "distort",
1980
- text: "Distorted Text",
1981
- transformData: {
1982
- type: "distort"
1983
- }
1984
- };
1985
- case "circle":
1986
- return {
1987
- ...r,
1988
- transformType: "circle",
1989
- text: "Circular Text",
1990
- transformData: {
1991
- type: "circle",
1992
- radius: 100,
1993
- scale: 1,
1994
- reverse: !1
1995
- }
1996
- };
1997
- case "lean":
1998
- return {
1999
- ...r,
2000
- transformType: "lean",
2001
- text: "Leaning Text",
2002
- transformData: {
2003
- type: "lean",
2004
- leanAmount: 0
2005
- }
2006
- };
2007
- case "arch":
2008
- return {
2009
- ...r,
2010
- transformType: "arch",
2011
- text: "Arched Text",
2012
- transformData: {
2013
- type: "arch",
2014
- archHeight: 0.5
2015
- }
2016
- };
2017
- case "ascend":
2018
- return {
2019
- ...r,
2020
- transformType: "ascend",
2021
- text: "Ascending Text",
2022
- transformData: {
2023
- type: "ascend",
2024
- ascendAngle: -15
2025
- }
2026
- };
2027
- case "wave":
2028
- return {
2029
- ...r,
2030
- transformType: "wave",
2031
- text: "Wavy Text",
2032
- transformData: {
2033
- type: "wave",
2034
- amplitude: 0.5,
2035
- frequency: 2
2036
- }
2037
- };
2038
- case "flag":
2039
- return {
2040
- ...r,
2041
- transformType: "flag",
2042
- text: "Flag Text",
2043
- transformData: {
2044
- type: "flag",
2045
- amplitude: 0.5,
2046
- frequency: 2
2047
- }
2048
- };
2049
- case "image":
2050
- return {
2051
- ...r,
2052
- transformType: "image",
2053
- imageUrl: "",
2054
- transformData: {
2055
- type: "image",
2056
- width: 200,
2057
- height: 200,
2058
- cropX: 0,
2059
- cropY: 0,
2060
- cropWidth: 1,
2061
- cropHeight: 1,
2062
- flipHorizontal: !1,
2063
- flipVertical: !1,
2064
- borderRadius: 0
2065
- }
2066
- };
2067
- case "group":
2068
- return {
2069
- ...r,
2070
- transformType: "group",
2071
- children: [],
2072
- transformData: {
2073
- type: "group"
2074
- }
2075
- };
2076
- case "shape":
2077
- return {
2078
- ...r,
2079
- transformType: "shape",
2080
- transformData: {
2081
- type: "shape",
2082
- shapeType: "rectangle",
2083
- width: 200,
2084
- height: 200,
2085
- borderRadius: 0,
2086
- fillColor: "#3b82f6",
2087
- fillOpacity: 1
2088
- }
2089
- };
2090
- case "path":
2091
- return {
2092
- ...r,
2093
- transformType: "path",
2094
- transformData: {
2095
- type: "path",
2096
- points: [],
2097
- closed: !1,
2098
- width: 200,
2099
- height: 200,
2100
- fillEnabled: !1,
2101
- fillColor: "#3b82f6",
2102
- fillOpacity: 1,
2103
- strokeEnabled: !0,
2104
- strokeColor: "#000000",
2105
- strokeWidth: 2
2106
- }
2107
- };
2108
- case "artboard":
2109
- throw new Error("Artboards cannot be created via ElementFactory");
2110
- default:
2111
- const n = t;
2112
- throw new Error(`Unknown transform type: ${n}`);
2113
- }
2114
- }
2115
- /**
2116
- * Clone an element by serializing and deserializing
2117
- * Ensures a deep copy with proper class instance
2118
- *
2119
- * @param element - Element to clone
2120
- * @returns Cloned element instance
2121
- */
2122
- static clone(t) {
2123
- const r = t.toJSON();
2124
- return this.createFromJSON(r);
2125
- }
2126
- }
2127
- const we = j("ImportManager"), te = class te {
2128
- /**
2129
- * Import workspace from JSON file
2130
- */
2131
- static async importFromJSON(t) {
2132
- try {
2133
- const r = await this.readFileAsText(t), n = JSON.parse(r);
2134
- if (this.validateDocument(n), !this.SUPPORTED_VERSIONS.includes(n.metadata.version))
2135
- throw new Error(
2136
- `Unsupported version: ${n.metadata.version}. Supported versions: ${this.SUPPORTED_VERSIONS.join(", ")}`
2137
- );
2138
- if (n.metadata.version === "1.0.0")
2139
- return this.migrateV1Document(n);
2140
- const o = [], i = [];
2141
- return n.artboards.forEach((a) => {
2142
- const l = re.fromJSON({
2143
- id: a.id,
2144
- name: a.name,
2145
- x: a.x,
2146
- y: a.y,
2147
- width: a.width,
2148
- height: a.height,
2149
- backgroundColor: a.backgroundColor,
2150
- elementIds: a.elements.map((c) => c.id || "")
2151
- }), s = a.elements.map((c) => {
2152
- try {
2153
- return wt.createFromJSON(c);
2154
- } catch (h) {
2155
- return we.error("Failed to create element:", c, h), null;
2156
- }
2157
- }).filter((c) => c !== null);
2158
- o.push(l), i.push(...s);
2159
- }), await this.preloadAllImages(i), {
2160
- success: !0,
2161
- artboards: o,
2162
- elements: i,
2163
- activeArtboardId: n.activeArtboardId
2164
- };
2165
- } catch (r) {
2166
- return {
2167
- success: !1,
2168
- error: r instanceof Error ? r.message : "Unknown error"
2169
- };
2170
- }
2171
- }
2172
- /**
2173
- * Migrate v1 document (no artboards) to v2 (with artboards)
2174
- */
2175
- static async migrateV1Document(t) {
2176
- var r, n, o;
2177
- try {
2178
- const i = t.elements || [], a = new re({
2179
- id: "artboard-migrated",
2180
- name: "Artboard 1",
2181
- x: 0,
2182
- y: 0,
2183
- width: ((r = t.canvas) == null ? void 0 : r.width) || 1920,
2184
- height: ((n = t.canvas) == null ? void 0 : n.height) || 1080,
2185
- backgroundColor: ((o = t.canvas) == null ? void 0 : o.backgroundColor) || "#ffffff"
2186
- }), l = i.map((s) => {
2187
- try {
2188
- return wt.createFromJSON(s);
2189
- } catch (c) {
2190
- return we.error("Failed to create element:", s, c), null;
2191
- }
2192
- }).filter((s) => s !== null);
2193
- return l.forEach((s) => {
2194
- a.addElementId(s.id);
2195
- }), await this.preloadAllImages(l), {
2196
- success: !0,
2197
- artboards: [a],
2198
- elements: l,
2199
- activeArtboardId: a.id
2200
- };
2201
- } catch (i) {
2202
- return {
2203
- success: !1,
2204
- error: i instanceof Error ? i.message : "Migration failed"
2205
- };
2206
- }
2207
- }
2208
- /**
2209
- * Import image file
2210
- */
2211
- static async importImage(t) {
2212
- try {
2213
- if (!t.type.startsWith("image/"))
2214
- throw new Error("File is not an image");
2215
- const r = 10 * 1024 * 1024;
2216
- if (t.size > r)
2217
- throw new Error("Image file is too large (max 10MB)");
2218
- const n = await this.readFileAsDataURL(t), { width: o, height: i } = await this.getImageDimensions(n);
2219
- return {
2220
- success: !0,
2221
- dataUrl: n,
2222
- filename: t.name,
2223
- width: o,
2224
- height: i
2225
- };
2226
- } catch (r) {
2227
- return {
2228
- success: !1,
2229
- error: r instanceof Error ? r.message : "Unknown error"
2230
- };
2231
- }
2232
- }
2233
- /**
2234
- * Validate document structure
2235
- */
2236
- static validateDocument(t) {
2237
- if (!t || typeof t != "object")
2238
- throw new Error("Invalid document");
2239
- const r = t;
2240
- if (!r.metadata)
2241
- throw new Error("Missing metadata");
2242
- if (!r.metadata.version)
2243
- throw new Error("Missing version in metadata");
2244
- if (!r.artboards && !r.elements)
2245
- throw new Error("Missing artboards or elements array");
2246
- if (r.artboards && !Array.isArray(r.artboards))
2247
- throw new Error("Invalid artboards array");
2248
- }
2249
- /**
2250
- * Read file as text
2251
- */
2252
- static readFileAsText(t) {
2253
- return new Promise((r, n) => {
2254
- const o = new FileReader();
2255
- o.onload = (i) => {
2256
- var a;
2257
- return r((a = i.target) == null ? void 0 : a.result);
2258
- }, o.onerror = () => n(new Error("Failed to read file")), o.readAsText(t);
2259
- });
2260
- }
2261
- /**
2262
- * Read file as data URL
2263
- */
2264
- static readFileAsDataURL(t) {
2265
- return new Promise((r, n) => {
2266
- const o = new FileReader();
2267
- o.onload = (i) => {
2268
- var a;
2269
- return r((a = i.target) == null ? void 0 : a.result);
2270
- }, o.onerror = () => n(new Error("Failed to read file")), o.readAsDataURL(t);
2271
- });
2272
- }
2273
- /**
2274
- * Get image dimensions from data URL
2275
- */
2276
- static getImageDimensions(t) {
2277
- return new Promise((r, n) => {
2278
- const o = new Image();
2279
- o.onload = () => {
2280
- r({ width: o.width, height: o.height });
2281
- }, o.onerror = () => n(new Error("Failed to load image")), o.src = t;
2282
- });
2283
- }
2284
- /**
2285
- * Validate imported data before applying to state
2286
- */
2287
- static validateImportedData(t, r) {
2288
- const n = [], o = /* @__PURE__ */ new Set();
2289
- t.forEach((a) => {
2290
- o.has(a.id) && n.push(`Duplicate artboard ID: ${a.id}`), o.add(a.id);
2291
- });
2292
- const i = /* @__PURE__ */ new Set();
2293
- return r.forEach((a) => {
2294
- i.has(a.id) && n.push(`Duplicate element ID: ${a.id}`), i.add(a.id);
2295
- }), t.forEach((a) => {
2296
- a.getElementIds().forEach((l) => {
2297
- i.has(l) || n.push(`Artboard "${a.name}" references missing element: ${l}`);
2298
- });
2299
- }), {
2300
- valid: n.length === 0,
2301
- errors: n
2302
- };
2303
- }
2304
- /**
2305
- * Get import summary (for UI display before confirming)
2306
- */
2307
- static getImportSummary(t, r) {
2308
- const n = this.validateImportedData(t, r);
2309
- return {
2310
- artboardCount: t.length,
2311
- elementCount: r.length,
2312
- artboardNames: t.map((o) => o.name),
2313
- warnings: n.errors
2314
- };
2315
- }
2316
- /**
2317
- * Preload all images in parallel for fast artboard switching
2318
- * Recursively finds all ImageElements (including those in groups)
2319
- */
2320
- static async preloadAllImages(t) {
2321
- const r = [], n = (i) => {
2322
- i.transformType === "image" ? r.push(i) : i.transformType === "group" && "children" in i && i.children.forEach((a) => n(a));
2323
- };
2324
- t.forEach(n);
2325
- const o = r.map((i) => new Promise((a) => {
2326
- if (i.imageLoaded)
2327
- a();
2328
- else if (i.imageElement) {
2329
- const l = i.imageElement, s = () => {
2330
- l.removeEventListener("load", s), l.removeEventListener("error", c), a();
2331
- }, c = () => {
2332
- l.removeEventListener("load", s), l.removeEventListener("error", c), a();
2333
- };
2334
- l.addEventListener("load", s), l.addEventListener("error", c);
2335
- } else
2336
- a();
2337
- }));
2338
- await Promise.all(o);
2339
- }
2340
- };
2341
- te.SUPPORTED_VERSIONS = ["1.0.0", "2.0.0"];
2342
- let be = te;
2343
- const ke = j("element-serializer"), Ge = 4e3, ht = /* @__PURE__ */ new Set();
2344
- function Ln(e, t) {
2345
- const r = t ? qn(t) : "no-src";
2346
- return `${e}:${r}`;
2347
- }
2348
- function qn(e) {
2349
- let t = 0;
2350
- for (let r = 0; r < e.length; r++) {
2351
- const n = e.charCodeAt(r);
2352
- t = (t << 5) - t + n, t = t & t;
2353
- }
2354
- return t.toString(36);
2355
- }
2356
- function jt() {
2357
- ht.clear();
2358
- }
2359
- function Ye(e) {
2360
- for (const t of ht)
2361
- t.startsWith(`${e}:`) && ht.delete(t);
2362
- }
2363
- function $n(e) {
2364
- ht.add(e);
2365
- }
2366
- function Hn(e) {
2367
- return ht.has(e);
2368
- }
2369
- async function jn(e, t = Ge) {
2370
- const { naturalWidth: r, naturalHeight: n } = e;
2371
- if (r <= t && n <= t)
2372
- return createImageBitmap(e);
2373
- const o = t / Math.max(r, n), i = Math.round(r * o), a = Math.round(n * o);
2374
- return createImageBitmap(e, {
2375
- resizeWidth: i,
2376
- resizeHeight: a,
2377
- resizeQuality: "high"
2378
- });
2379
- }
2380
- let bt = null, kt = null;
2381
- function _n(e, t) {
2382
- bt = e, kt = t;
2383
- }
2384
- function Nn() {
2385
- bt = null, kt = null;
2386
- }
2387
- function co() {
2388
- jt(), bt && bt();
2389
- }
2390
- function ho(e) {
2391
- Ye(e), kt && kt(e);
2392
- }
2393
- function _t(e, t) {
2394
- const r = e;
2395
- if (typeof r.fontFamily == "string" && t.add(r.fontFamily), r.richText && typeof r.richText == "object") {
2396
- const n = r.richText;
2397
- if (n.spans && Array.isArray(n.spans))
2398
- for (const o of n.spans) {
2399
- const i = o;
2400
- if (i.style && typeof i.style == "object") {
2401
- const a = i.style;
2402
- typeof a.fontFamily == "string" && t.add(a.fontFamily);
2403
- }
2404
- }
2405
- }
2406
- if (r.children && Array.isArray(r.children))
2407
- for (const n of r.children)
2408
- _t(n, t);
2409
- if (e.masks && Array.isArray(e.masks))
2410
- for (const n of e.masks)
2411
- n.maskElement && _t(n.maskElement, t);
2412
- }
2413
- function Te(e) {
2414
- if (typeof document > "u") return e;
2415
- const t = vt.getInstance();
2416
- return e.map((r) => {
2417
- if (!(r != null && r.maskElement) || r.maskElement.transformType !== "image") return r;
2418
- const n = r.maskElement.imageUrl;
2419
- if (!n || !n.startsWith("builtin-mask:")) return r;
2420
- const o = t.get(n);
2421
- return o != null && o.src && o.src.startsWith("data:") ? {
2422
- ...r,
2423
- maskElement: {
2424
- ...r.maskElement,
2425
- imageUrl: o.src
2426
- // Replace with data URL
2427
- }
2428
- } : r;
2429
- });
2430
- }
2431
- async function Nt(e, t, r = Ge) {
2432
- const n = t.filter((s) => e.hasElementId(s.id)), o = [], i = [], a = /* @__PURE__ */ new Set();
2433
- for (const s of n) {
2434
- if (s.visible === !1) continue;
2435
- const c = s.toJSON();
2436
- if (_t(c, a), s instanceof Wt && !s.imageElement) {
2437
- ke.warn(`[element-serializer] Skipping image ${s.id}: image not loaded (state: ${s.imageLoadState})`);
2438
- continue;
2439
- }
2440
- if (s instanceof Wt && s.imageElement)
2441
- try {
2442
- const h = Ln(s.id, s.imageUrl);
2443
- if (!Hn(h)) {
2444
- const g = await jn(s.imageElement, r);
2445
- i.push({ cacheKey: h, bitmap: g }), $n(h);
2446
- }
2447
- const u = s.transformData || {}, d = Ce(c), p = ur(u, d), m = u.width * p.cropWidth, w = u.height * p.cropHeight, k = {
2448
- id: s.id,
2449
- type: "image",
2450
- x: s.x,
2451
- y: s.y,
2452
- width: m,
2453
- height: w,
2454
- rotation: s.rotation || 0,
2455
- bitmapCacheKey: h
2456
- };
2457
- if (!gr(p)) {
2458
- const g = s.imageElement.naturalWidth, y = s.imageElement.naturalHeight;
2459
- let S = 1;
2460
- (g > r || y > r) && (S = r / Math.max(g, y)), k.cropX = p.cropX * g * S, k.cropY = p.cropY * y * S, k.cropWidth = p.cropWidth * g * S, k.cropHeight = p.cropHeight * y * S;
2461
- }
2462
- u.flipHorizontal && (k.flipHorizontal = u.flipHorizontal), u.flipVertical && (k.flipVertical = u.flipVertical), u.borderRadius && (k.borderRadius = u.borderRadius), pr(u.adjustments) || (k.adjustments = { ...u.adjustments }), s.opacity !== void 0 && (k.opacity = s.opacity);
2463
- const T = k;
2464
- s.distressEffect && (T.distressEffect = { ...s.distressEffect }), c.masks && c.masks.length > 0 && (T.masks = Te(c.masks)), c.blendMode && (T.blendMode = c.blendMode), c.knockoutParts && (T.knockoutParts = c.knockoutParts), c.shadow && (T.shadow = { ...c.shadow }), "getVisualBoundingBox" in s && typeof s.getVisualBoundingBox == "function" ? T._boundingBox = s.getVisualBoundingBox() : "getBoundingBox" in s && typeof s.getBoundingBox == "function" && (T._boundingBox = s.getBoundingBox()), o.push(T);
2465
- } catch (h) {
2466
- ke.error(`[element-serializer] Failed to create ImageBitmap for ${c.id}:`, h);
2467
- }
2468
- else {
2469
- const h = {
2470
- ...c,
2471
- type: c.transformType
2472
- // Map transformType to type for worker compatibility
2473
- };
2474
- if (s instanceof Vt) {
2475
- const f = await Nt(
2476
- { ...e, hasElementId: () => !0 },
2477
- s.children,
2478
- r
2479
- );
2480
- h.children = f.data.elements, i.push(...f.newBitmaps);
2481
- for (const u of f.data.fonts) a.add(u);
2482
- }
2483
- h.masks && Array.isArray(h.masks) && h.masks.length > 0 && (h.masks = Te(h.masks)), "getVisualBoundingBox" in s && typeof s.getVisualBoundingBox == "function" ? h._boundingBox = s.getVisualBoundingBox() : "getBoundingBox" in s && typeof s.getBoundingBox == "function" && (h._boundingBox = s.getBoundingBox()), o.push(h);
2484
- }
2485
- }
2486
- const l = Array.from(a);
2487
- return {
2488
- data: {
2489
- version: "1.0.0",
2490
- artboard: {
2491
- id: e.id,
2492
- name: e.name,
2493
- width: e.width,
2494
- height: e.height,
2495
- backgroundColor: e.backgroundColor,
2496
- exportBackground: e.exportBackground,
2497
- x: e.x,
2498
- y: e.y,
2499
- ...e.distressTexture && { distressTexture: { ...e.distressTexture } },
2500
- ...e.imageMask && { imageMask: { ...e.imageMask } }
2501
- },
2502
- elements: o,
2503
- fonts: l
2504
- },
2505
- newBitmaps: i
2506
- };
2507
- }
2508
- async function Xn(e, t, r = fetch) {
2509
- try {
2510
- const n = await r(t.fontFileUrl, { mode: "cors" });
2511
- if (!n.ok) return null;
2512
- const o = await n.arrayBuffer(), i = new FontFace(e, o, t.descriptors);
2513
- return await i.load(), i;
2514
- } catch {
2515
- return null;
2516
- }
2517
- }
2518
- function J(e) {
2519
- return e.trim().toLowerCase();
2520
- }
2521
- function xt(e) {
2522
- return `${J(e.family)}|${e.weight ?? 400}|${e.style ?? "normal"}`;
2523
- }
2524
- class Gn {
2525
- constructor() {
2526
- this.fonts = /* @__PURE__ */ new Map(), this.listeners = /* @__PURE__ */ new Set(), this.snapshot = [], this.list = () => this.snapshot, this.subscribe = (t) => (this.listeners.add(t), () => {
2527
- this.listeners.delete(t);
2528
- });
2529
- }
2530
- /**
2531
- * Register fonts (idempotent; re-registering the same family/weight/style
2532
- * replaces the URL). Returns true when anything changed.
2533
- */
2534
- register(t) {
2535
- let r = !1;
2536
- for (const n of t) {
2537
- const o = xt(n), i = this.fonts.get(o);
2538
- i && i.url === n.url && i.family === n.family || (this.fonts.set(o, { ...n }), r = !0);
2539
- }
2540
- if (r) {
2541
- this.snapshot = Object.freeze([...this.fonts.values()]);
2542
- for (const n of this.listeners) n();
2543
- }
2544
- return r;
2545
- }
2546
- /** Is this family registered as a custom font? (Shadowing check.) */
2547
- has(t) {
2548
- const r = J(t);
2549
- for (const n of this.fonts.values())
2550
- if (J(n.family) === r) return !0;
2551
- return !1;
2552
- }
2553
- /** All registered faces of one family (possibly several weights/styles). */
2554
- facesOf(t) {
2555
- const r = J(t);
2556
- return [...this.fonts.values()].filter((n) => J(n.family) === r);
2557
- }
2558
- /** Registered faces whose family appears in `families` — what the export
2559
- * path sends to the worker alongside the used-font list. */
2560
- forFamilies(t) {
2561
- const r = new Set(t.map(J));
2562
- return [...this.fonts.values()].filter((n) => r.has(J(n.family)));
2563
- }
2564
- /** @internal Test helper — wipe registry state between tests. */
2565
- clear() {
2566
- this.fonts.clear(), this.snapshot = Object.freeze([]);
2567
- for (const t of this.listeners) t();
2568
- }
2569
- }
2570
- const Xt = new Gn();
2571
- function Yn(e) {
2572
- Xt.register(e);
2573
- }
2574
- const Ve = /* @__PURE__ */ new Set();
2575
- async function Vn(e, t = {}) {
2576
- const r = await Xn(
2577
- e.family,
2578
- {
2579
- fontFileUrl: e.url,
2580
- descriptors: {
2581
- weight: String(e.weight ?? 400),
2582
- style: e.style ?? "normal"
2583
- }
2584
- },
2585
- t.fetchImpl ?? fetch
2586
- );
2587
- if (!r)
2588
- throw new gt(
2589
- "E_FONT_LOAD_FAILED",
2590
- `Custom font "${e.family}" failed to load from ${e.url}. The URL must serve the font binary over a plain cross-origin GET (CORS). Verify the URL is reachable and sends Access-Control-Allow-Origin, then re-register the font. Custom fonts never fall back silently — the design would render with the wrong typeface.`,
2591
- {
2592
- family: e.family,
2593
- url: e.url,
2594
- weight: e.weight ?? 400,
2595
- style: e.style ?? "normal"
2596
- }
2597
- );
2598
- const n = t.fonts ?? (typeof document < "u" ? document.fonts : void 0);
2599
- return n == null || n.add(r), Ve.add(xt(e)), r;
2600
- }
2601
- async function fo(e, t = {}) {
2602
- Yn(e);
2603
- const { onLoadError: r, ...n } = t;
2604
- await Promise.all(
2605
- e.map(async (o) => {
2606
- if (!Ve.has(xt(o)))
2607
- try {
2608
- await Vn(o, n);
2609
- } catch (i) {
2610
- if (r && i instanceof gt) {
2611
- r(i, o);
2612
- return;
2613
- }
2614
- throw i;
2615
- }
2616
- })
2617
- );
2618
- }
2619
- const O = j("WorkerExport");
2620
- let Gt = null, At = null;
2621
- function Kn() {
2622
- return At ?? (At = (typeof window > "u" ? Promise.reject(new Error("The export worker runs in a browser page only (no window here).")) : import("./export-worker.bundle.string-CyMb_XjB.js")).then((e) => Gt = e.default).catch((e) => {
2623
- throw At = null, e;
2624
- })), At;
2625
- }
2626
- class ft {
2627
- constructor() {
2628
- this.worker = null, this.workerBlobUrl = null, this.pendingRequests = /* @__PURE__ */ new Map(), this.requestQueue = [], this.blobRequestQueue = [], this.isProcessingQueue = !1, this.isReady = !1, this.readyPromise = null, this.initGeneration = 0, this.requestIdCounter = 0, this.workerCrashCount = 0, this.MAX_CRASHES_BEFORE_FALLBACK = 3, this.lastCrashTime = 0, this.CRASH_RESET_TIMEOUT = 6e4, this.progressListeners = /* @__PURE__ */ new Set(), this.idleTimeout = null, this.IDLE_TIMEOUT_MS = 3e4;
2629
- }
2630
- // 30 seconds
2631
- /**
2632
- * Add progress listener
2633
- */
2634
- onProgress(t) {
2635
- return this.progressListeners.add(t), () => {
2636
- this.progressListeners.delete(t);
2637
- };
2638
- }
2639
- /**
2640
- * Remove progress listener
2641
- */
2642
- offProgress(t) {
2643
- this.progressListeners.delete(t);
2644
- }
2645
- /**
2646
- * Emit progress event to all listeners
2647
- */
2648
- emitProgress(t) {
2649
- for (const r of this.progressListeners)
2650
- try {
2651
- r(t);
2652
- } catch (n) {
2653
- O.error("Error in progress listener:", n);
2654
- }
2655
- }
2656
- /**
2657
- * Reset idle timer
2658
- */
2659
- resetIdleTimer() {
2660
- this.idleTimeout && clearTimeout(this.idleTimeout), this.idleTimeout = window.setTimeout(() => {
2661
- this.terminate();
2662
- }, this.IDLE_TIMEOUT_MS);
2663
- }
2664
- // ============================================================================
2665
- // Worker Bitmap Cache Management
2666
- // ============================================================================
2667
- /**
2668
- * Register bitmaps with the worker's cache.
2669
- * This transfers the bitmaps to the worker (zero-copy) where they are cached.
2670
- */
2671
- registerBitmapsWithWorker(t) {
2672
- if (!(!this.worker || t.length === 0))
2673
- for (const { cacheKey: r, bitmap: n } of t) {
2674
- const o = {
2675
- type: "register-bitmap",
2676
- cacheKey: r,
2677
- bitmap: n
2678
- };
2679
- this.worker.postMessage(o, [n]);
2680
- }
2681
- }
2682
- /**
2683
- * Clear all bitmaps from the worker's cache.
2684
- * Also clears the main thread's registration tracking.
2685
- * Call this when exiting editor mode to free memory.
2686
- */
2687
- clearWorkerBitmapCache() {
2688
- if (jt(), this.worker && this.isReady) {
2689
- const t = {
2690
- type: "clear-all-bitmaps"
2691
- };
2692
- this.worker.postMessage(t);
2693
- }
2694
- }
2695
- /**
2696
- * Clear a specific element's bitmaps from the worker's cache.
2697
- * Call this when an element is removed or its image source changes.
2698
- */
2699
- clearWorkerBitmap(t) {
2700
- if (Ye(t), this.worker && this.isReady) {
2701
- const r = {
2702
- type: "clear-bitmap",
2703
- elementId: t
2704
- };
2705
- this.worker.postMessage(r);
2706
- }
2707
- }
2708
- /**
2709
- * Check if browser supports OffscreenCanvas and Web Workers
2710
- */
2711
- static supportsWorkerExport() {
2712
- try {
2713
- return typeof OffscreenCanvas > "u" ? (O.warn("OffscreenCanvas not supported"), !1) : typeof Worker > "u" ? (O.warn("Web Workers not supported"), !1) : new OffscreenCanvas(1, 1).getContext("2d") ? !0 : (O.warn("OffscreenCanvas 2D context not supported"), !1);
2714
- } catch (t) {
2715
- return O.warn("Feature detection failed:", t), !1;
2716
- }
2717
- }
2718
- /**
2719
- * Initialize the worker
2720
- */
2721
- async initialize() {
2722
- if (this.worker)
2723
- return this.readyPromise || Promise.resolve();
2724
- if (this.readyPromise)
2725
- return this.readyPromise;
2726
- if (!ft.supportsWorkerExport())
2727
- throw new Error("Browser does not support OffscreenCanvas or Web Workers");
2728
- if (Gt !== null)
2729
- return this.readyPromise = this.startWorker(Gt), this.readyPromise;
2730
- const t = this.initGeneration, r = Kn().then((n) => {
2731
- if (t !== this.initGeneration)
2732
- throw new Error("Worker terminated");
2733
- return this.startWorker(n);
2734
- });
2735
- return this.readyPromise = r, r.catch(() => {
2736
- this.readyPromise === r && !this.worker && (this.readyPromise = null);
2737
- }), r;
2738
- }
2739
- /** Spawn the worker from its code and resolve once it reports `ready`. */
2740
- startWorker(t) {
2741
- return new Promise((r, n) => {
2742
- try {
2743
- let o;
2744
- try {
2745
- const l = new Blob([t], { type: "application/javascript" });
2746
- o = URL.createObjectURL(l), this.workerBlobUrl = o;
2747
- } catch (l) {
2748
- O.error("Failed to create worker blob:", l), n(new Error(`Worker creation failed: ${l instanceof Error ? l.message : String(l)}`));
2749
- return;
2750
- }
2751
- this.worker = new Worker(o, { type: "classic" }), this.worker.onmessage = (l) => {
2752
- this.handleWorkerMessage(l.data);
2753
- }, this.worker.onerror = (l) => {
2754
- O.error("Worker error:", l), this.handleWorkerError(l);
2755
- };
2756
- const i = setTimeout(() => {
2757
- n(new Error("Worker initialization timeout"));
2758
- }, 15e3), a = (l) => {
2759
- l.data.type === "ready" && (clearTimeout(i), this.isReady = !0, _n(
2760
- () => this.clearWorkerBitmapCache(),
2761
- (s) => this.clearWorkerBitmap(s)
2762
- ), r());
2763
- };
2764
- this.worker.addEventListener("message", a, { once: !0 });
2765
- } catch (o) {
2766
- O.error("Failed to create worker:", o), n(o);
2767
- }
2768
- });
2769
- }
2770
- /**
2771
- * Restart worker after crash
2772
- */
2773
- async restartWorker() {
2774
- const t = Date.now();
2775
- if (t - this.lastCrashTime > this.CRASH_RESET_TIMEOUT && (this.workerCrashCount = 0), this.workerCrashCount++, this.lastCrashTime = t, O.warn(`Worker crashed (${this.workerCrashCount}/${this.MAX_CRASHES_BEFORE_FALLBACK})`), this.workerCrashCount >= this.MAX_CRASHES_BEFORE_FALLBACK)
2776
- throw O.error("Worker crashed too many times, giving up"), new Error("Worker has crashed too many times. Please refresh the page or use main thread export.");
2777
- this.worker && (this.worker.terminate(), this.worker = null, this.isReady = !1, this.readyPromise = null, this.workerBlobUrl && (URL.revokeObjectURL(this.workerBlobUrl), this.workerBlobUrl = null));
2778
- const r = Math.min(1e3 * Math.pow(2, this.workerCrashCount - 1), 5e3);
2779
- await new Promise((n) => setTimeout(n, r)), await this.initialize();
2780
- }
2781
- /**
2782
- * Export artboard to data URL using worker
2783
- * Uses request queue for concurrent exports
2784
- */
2785
- async exportArtboardToDataURL(t, r, n = {}) {
2786
- return this.resetIdleTimer(), new Promise((o, i) => {
2787
- this.requestQueue.push({
2788
- artboard: t,
2789
- elements: r,
2790
- config: { ...n, returnFormat: "dataUrl" },
2791
- resolve: o,
2792
- reject: i
2793
- }), this.processQueue();
2794
- });
2795
- }
2796
- /**
2797
- * Export artboard to Blob using worker
2798
- * Returns a Blob directly - more efficient for API uploads
2799
- * Uses request queue for concurrent exports
2800
- */
2801
- async exportArtboardToBlob(t, r, n = {}) {
2802
- return this.resetIdleTimer(), new Promise((o, i) => {
2803
- this.blobRequestQueue.push({
2804
- artboard: t,
2805
- elements: r,
2806
- config: { ...n, returnFormat: "blob" },
2807
- resolve: o,
2808
- reject: i
2809
- }), this.processBlobQueue();
2810
- });
2811
- }
2812
- /**
2813
- * Process the request queue
2814
- */
2815
- async processQueue() {
2816
- if (!(this.isProcessingQueue || this.requestQueue.length === 0)) {
2817
- for (this.isProcessingQueue = !0; this.requestQueue.length > 0; ) {
2818
- const t = this.requestQueue.shift();
2819
- try {
2820
- const r = await this.executeExportRequest(t);
2821
- t.resolve(r);
2822
- } catch (r) {
2823
- t.reject(r instanceof Error ? r : new Error(String(r)));
2824
- }
2825
- }
2826
- this.isProcessingQueue = !1;
2827
- }
2828
- }
2829
- /**
2830
- * Execute a single export request
2831
- */
2832
- async executeExportRequest(t) {
2833
- const { artboard: r, elements: n, config: o } = t;
2834
- if ((!this.worker || !this.isReady) && await this.initialize(), !this.worker)
2835
- throw new Error("Worker is not initialized");
2836
- const i = `export-${++this.requestIdCounter}-${Date.now()}`, a = performance.now(), { data: l, newBitmaps: s } = await Nt(r, n);
2837
- s.length > 0 && this.registerBitmapsWithWorker(s);
2838
- const c = {
2839
- type: "export",
2840
- requestId: i,
2841
- artboardData: l.artboard,
2842
- elements: l.elements,
2843
- fonts: l.fonts,
2844
- // Custom (brand) fonts among the used families — the worker loads
2845
- // these from their hosted URL instead of Google Fonts (deterministic
2846
- // shadowing; see utils/custom-fonts.ts).
2847
- customFonts: Xt.forFamilies(l.fonts ?? []),
2848
- scale: o.scale || 1,
2849
- format: o.format || "png",
2850
- quality: o.quality || 0.95
2851
- }, h = new Promise((p, m) => {
2852
- this.pendingRequests.set(i, {
2853
- requestId: i,
2854
- resolve: p,
2855
- reject: m,
2856
- startTime: a,
2857
- returnFormat: "dataUrl"
2858
- }), setTimeout(() => {
2859
- this.pendingRequests.has(i) && (this.pendingRequests.delete(i), m(new Error("Export request timeout")));
2860
- }, 3e4);
2861
- });
2862
- this.worker.postMessage(c);
2863
- const f = await h, d = performance.now() - a;
2864
- return {
2865
- dataUrl: f.dataUrl,
2866
- artboardId: f.artboardId,
2867
- artboardName: f.artboardName,
2868
- renderTime: f.renderTime,
2869
- totalTime: f.totalTime,
2870
- mainThreadBlockTime: d
2871
- };
2872
- }
2873
- /**
2874
- * Process the blob request queue
2875
- */
2876
- async processBlobQueue() {
2877
- if (!(this.isProcessingQueue || this.blobRequestQueue.length === 0)) {
2878
- for (this.isProcessingQueue = !0; this.blobRequestQueue.length > 0; ) {
2879
- const t = this.blobRequestQueue.shift();
2880
- try {
2881
- const r = await this.executeBlobExportRequest(t);
2882
- t.resolve(r);
2883
- } catch (r) {
2884
- t.reject(r instanceof Error ? r : new Error(String(r)));
2885
- }
2886
- }
2887
- this.isProcessingQueue = !1;
2888
- }
2889
- }
2890
- /**
2891
- * Execute a single blob export request
2892
- */
2893
- async executeBlobExportRequest(t) {
2894
- const { artboard: r, elements: n, config: o } = t;
2895
- if ((!this.worker || !this.isReady) && await this.initialize(), !this.worker)
2896
- throw new Error("Worker is not initialized");
2897
- const i = `export-blob-${++this.requestIdCounter}-${Date.now()}`, a = performance.now(), { data: l, newBitmaps: s } = await Nt(r, n);
2898
- s.length > 0 && this.registerBitmapsWithWorker(s);
2899
- const c = {
2900
- type: "export",
2901
- requestId: i,
2902
- artboardData: l.artboard,
2903
- elements: l.elements,
2904
- fonts: l.fonts,
2905
- // Same custom-font threading as the dataUrl path above.
2906
- customFonts: Xt.forFamilies(l.fonts ?? []),
2907
- scale: o.scale || 1,
2908
- format: o.format || "png",
2909
- quality: o.quality || 0.95,
2910
- returnFormat: "blob"
2911
- }, h = new Promise((m, w) => {
2912
- this.pendingRequests.set(i, {
2913
- requestId: i,
2914
- resolve: m,
2915
- reject: w,
2916
- startTime: a,
2917
- returnFormat: "blob"
2918
- }), setTimeout(() => {
2919
- this.pendingRequests.has(i) && (this.pendingRequests.delete(i), w(new Error("Export request timeout")));
2920
- }, 3e4);
2921
- });
2922
- this.worker.postMessage(c);
2923
- const f = await h, d = performance.now() - a;
2924
- return {
2925
- blob: new Blob([f.blobData], { type: f.mimeType }),
2926
- artboardId: f.artboardId,
2927
- artboardName: f.artboardName,
2928
- renderTime: f.renderTime,
2929
- totalTime: f.totalTime,
2930
- mainThreadBlockTime: d
2931
- };
2932
- }
2933
- /**
2934
- * Handle messages from worker
2935
- */
2936
- handleWorkerMessage(t) {
2937
- switch (t.type) {
2938
- case "exportComplete":
2939
- this.handleExportComplete(t);
2940
- break;
2941
- case "exportBlobComplete":
2942
- this.handleBlobExportComplete(t);
2943
- break;
2944
- case "exportError":
2945
- this.handleExportError(t);
2946
- break;
2947
- case "exportProgress":
2948
- this.emitProgress({
2949
- requestId: t.requestId,
2950
- progress: t.progress,
2951
- message: t.message
2952
- });
2953
- break;
2954
- case "ready":
2955
- break;
2956
- default:
2957
- O.warn("Unknown message type:", t);
2958
- }
2959
- }
2960
- /**
2961
- * Handle successful export completion (dataUrl)
2962
- */
2963
- handleExportComplete(t) {
2964
- const r = this.pendingRequests.get(t.requestId);
2965
- r && r.returnFormat === "dataUrl" ? (r.resolve(t), this.pendingRequests.delete(t.requestId)) : r ? (O.warn("Received dataUrl response for blob request:", t.requestId), r.reject(new Error("Received dataUrl response when expecting blob")), this.pendingRequests.delete(t.requestId)) : O.warn("Received response for unknown request:", t.requestId);
2966
- }
2967
- /**
2968
- * Handle successful blob export completion
2969
- */
2970
- handleBlobExportComplete(t) {
2971
- const r = this.pendingRequests.get(t.requestId);
2972
- r && r.returnFormat === "blob" ? (r.resolve(t), this.pendingRequests.delete(t.requestId)) : r ? (O.warn("Received blob response for dataUrl request:", t.requestId), r.reject(new Error("Received blob response when expecting dataUrl")), this.pendingRequests.delete(t.requestId)) : O.warn("Received blob response for unknown request:", t.requestId);
2973
- }
2974
- /**
2975
- * Handle export error
2976
- */
2977
- handleExportError(t) {
2978
- const r = this.pendingRequests.get(t.requestId);
2979
- if (r) {
2980
- const n = new Error(t.error);
2981
- t.stack && (n.stack = t.stack), r.reject(n), this.pendingRequests.delete(t.requestId);
2982
- } else
2983
- O.warn("Received error for unknown request:", t.requestId);
2984
- }
2985
- /**
2986
- * Handle worker errors
2987
- */
2988
- async handleWorkerError(t) {
2989
- O.error("Worker error:", t);
2990
- const r = Array.from(this.pendingRequests.values());
2991
- for (const [n, o] of this.pendingRequests.entries())
2992
- this.pendingRequests.delete(n);
2993
- this.isReady = !1;
2994
- try {
2995
- await this.restartWorker();
2996
- } catch (n) {
2997
- O.error("Failed to restart worker:", n);
2998
- for (const o of r)
2999
- o.reject(new Error(`Worker crashed and could not be restarted: ${t.message}`));
3000
- for (; this.requestQueue.length > 0; )
3001
- this.requestQueue.shift().reject(new Error("Worker crashed and could not be restarted"));
3002
- }
3003
- }
3004
- /**
3005
- * Terminate the worker
3006
- */
3007
- terminate() {
3008
- if (this.idleTimeout && (clearTimeout(this.idleTimeout), this.idleTimeout = null), this.initGeneration++, this.worker || (this.readyPromise = null), this.worker) {
3009
- for (const [t, r] of this.pendingRequests.entries())
3010
- r.reject(new Error("Worker terminated")), this.pendingRequests.delete(t);
3011
- for (; this.requestQueue.length > 0; )
3012
- this.requestQueue.shift().reject(new Error("Worker terminated"));
3013
- this.worker.terminate(), this.worker = null, this.isReady = !1, this.readyPromise = null, jt(), Nn(), this.workerBlobUrl && (URL.revokeObjectURL(this.workerBlobUrl), this.workerBlobUrl = null);
3014
- }
3015
- }
3016
- /**
3017
- * Get worker status
3018
- */
3019
- getStatus() {
3020
- return {
3021
- isReady: this.isReady,
3022
- pendingRequests: this.pendingRequests.size,
3023
- queuedRequests: this.requestQueue.length,
3024
- crashCount: this.workerCrashCount
3025
- };
3026
- }
3027
- }
3028
- let x = null;
3029
- function uo() {
3030
- return x || (x = new ft()), x;
3031
- }
3032
- function go() {
3033
- x && (x.terminate(), x = null);
3034
- }
3035
- zt();
3036
- const _ = j("Export"), ve = (e) => e === "jpg" || e === "jpeg" ? "image/jpeg" : e === "webp" ? "image/webp" : "image/png", Me = (e, t, r) => new Promise((n) => e.toBlob((o) => n(o), t, r));
3037
- function Rt(e) {
3038
- e.width = 0, e.height = 0;
3039
- }
3040
- function Yt(e, t) {
3041
- var r;
3042
- if (!(!e || typeof e != "object")) {
3043
- if (typeof e.fontFamily == "string" && e.fontFamily.trim() && t.add(e.fontFamily), e.richText && Array.isArray(e.richText.spans))
3044
- for (const n of e.richText.spans)
3045
- (r = n == null ? void 0 : n.style) != null && r.fontFamily && typeof n.style.fontFamily == "string" && t.add(n.style.fontFamily);
3046
- if (Array.isArray(e.children))
3047
- for (const n of e.children) Yt(n, t);
3048
- if (Array.isArray(e.masks))
3049
- for (const n of e.masks)
3050
- n != null && n.maskElement && Yt(n.maskElement, t);
3051
- }
3052
- }
3053
- async function Jn(e) {
3054
- if (typeof document > "u" || !document.fonts) return;
3055
- const t = /* @__PURE__ */ new Set();
3056
- for (const n of e) Yt(n, t);
3057
- if (t.size === 0) return;
3058
- const r = [];
3059
- for (const n of t)
3060
- r.push(document.fonts.load(`16px "${n}"`).catch(() => {
3061
- }));
3062
- await Promise.all(r), document.fonts.ready && await document.fonts.ready;
3063
- }
3064
- const lt = class lt {
3065
- /**
3066
- * Check if worker export is supported in this browser
3067
- */
3068
- static supportsWorkerExport() {
3069
- return ft.supportsWorkerExport();
3070
- }
3071
- /**
3072
- * Get or initialize the worker manager
3073
- */
3074
- static async getWorkerManager() {
3075
- return this.supportsWorkerExport() ? this.workerManager ? this.workerManager : this.workerInitPromise ? (await this.workerInitPromise, this.workerManager) : (this.workerInitPromise = (async () => {
3076
- try {
3077
- this.workerManager = new ft(), await this.workerManager.initialize();
3078
- } catch (t) {
3079
- _.error("Failed to initialize worker:", t), this.workerManager = null;
3080
- }
3081
- })(), await this.workerInitPromise, this.workerInitPromise = null, this.workerManager) : null;
3082
- }
3083
- /**
3084
- * Terminate the worker (for cleanup)
3085
- */
3086
- static terminateWorker() {
3087
- this.workerManager && (this.workerManager.terminate(), this.workerManager = null);
3088
- }
3089
- /**
3090
- * Export entire workspace to JSON
3091
- */
3092
- static exportToJSON(t, r, n, o) {
3093
- const i = {
3094
- metadata: {
3095
- version: this.CURRENT_VERSION,
3096
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3097
- appVersion: "1.0.0"
3098
- },
3099
- artboards: t.map((h) => {
3100
- const f = new Set(h.getElementIds()), u = r.filter((d) => f.has(d.id));
3101
- return {
3102
- id: h.id,
3103
- name: h.name,
3104
- x: h.x,
3105
- y: h.y,
3106
- width: h.width,
3107
- height: h.height,
3108
- backgroundColor: h.backgroundColor,
3109
- elements: u.map((d) => d.toJSON())
3110
- };
3111
- }),
3112
- activeArtboardId: n.getActiveArtboardId()
3113
- }, a = JSON.stringify(i, null, 2), l = new Blob([a], { type: "application/json" }), s = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").split("T")[0], c = o || `canvas-${s}.json`;
3114
- this.downloadBlob(l, c);
3115
- }
3116
- /**
3117
- * Export artboard to image
3118
- * Uses worker if supported, falls back to main thread
3119
- */
3120
- static async exportArtboardToImage(t, r, n, o = {}) {
3121
- const {
3122
- format: i = "png",
3123
- quality: a = 0.95,
3124
- scale: l = 1,
3125
- transparentBackground: s = !1,
3126
- backgroundColor: c = t.backgroundColor,
3127
- filename: h,
3128
- forceMainThread: f = !1
3129
- } = o, u = await this.exportArtboardToDataURL(t, r, n, {
3130
- format: i,
3131
- quality: a,
3132
- scale: l,
3133
- transparentBackground: s,
3134
- backgroundColor: c,
3135
- forceMainThread: f
3136
- }), d = await this.dataUrlToBlob(u), p = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").split("T")[0], m = h || `${t.name}-${p}.${i}`;
3137
- this.downloadBlob(d, m);
3138
- }
3139
- /**
3140
- * Export artboard to data URL (for preview/testing)
3141
- * Uses worker if supported, falls back to main thread
3142
- */
3143
- static async exportArtboardToDataURL(t, r, n, o = {}) {
3144
- const {
3145
- format: i = "png",
3146
- quality: a = 0.95,
3147
- scale: l = 1,
3148
- transparentBackground: s = !1,
3149
- backgroundColor: c = t.backgroundColor,
3150
- forceMainThread: h = !1
3151
- } = o;
3152
- if (!h && this.supportsWorkerExport())
3153
- try {
3154
- const f = await this.getWorkerManager();
3155
- if (f) {
3156
- const u = new Set(t.getElementIds()), d = r.filter((m) => u.has(m.id));
3157
- return (await f.exportArtboardToDataURL(t, d, {
3158
- scale: l,
3159
- format: i === "jpg" ? "jpeg" : i,
3160
- quality: a
3161
- })).dataUrl;
3162
- }
3163
- } catch (f) {
3164
- _.warn("Worker export failed, falling back to main thread:", f);
3165
- }
3166
- return this.mainThreadExportToDataURL(t, r, n, {
3167
- format: i,
3168
- quality: a,
3169
- scale: l,
3170
- transparentBackground: s,
3171
- backgroundColor: c
3172
- });
3173
- }
3174
- /**
3175
- * Export artboard as Blob (more efficient for API uploads)
3176
- * Returns a Blob instead of data URL - smaller payload, faster for network operations
3177
- * Uses worker if supported, falls back to main thread
3178
- */
3179
- static async exportArtboardToBlob(t, r, n, o = {}) {
3180
- const {
3181
- format: i = "png",
3182
- quality: a = 0.95,
3183
- scale: l = 1,
3184
- transparentBackground: s = !1,
3185
- backgroundColor: c = t.backgroundColor,
3186
- forceMainThread: h = !1
3187
- } = o;
3188
- if (!h && this.supportsWorkerExport())
3189
- try {
3190
- const f = await this.getWorkerManager();
3191
- if (f) {
3192
- const u = new Set(t.getElementIds()), d = r.filter((m) => u.has(m.id));
3193
- return (await f.exportArtboardToBlob(t, d, {
3194
- scale: l,
3195
- format: i === "jpg" ? "jpeg" : i,
3196
- quality: a
3197
- })).blob;
3198
- }
3199
- } catch (f) {
3200
- _.warn("Worker blob export failed, falling back to main thread:", f);
3201
- }
3202
- return this.mainThreadExportToBlob(t, r, n, {
3203
- format: i,
3204
- quality: a,
3205
- scale: l,
3206
- transparentBackground: s,
3207
- backgroundColor: c
3208
- });
3209
- }
3210
- /**
3211
- * Main thread export to Blob (fallback/legacy method).
3212
- *
3213
- * On iOS WebKit this is also the *primary* path (`isIOSWebKit()` skips the
3214
- * worker), so font readiness matters here even when no fallback occurred.
3215
- */
3216
- static async mainThreadExportToBlob(t, r, n, o = {}) {
3217
- const { format: i = "png", quality: a = 0.95 } = o, l = await this.renderOnMainThread(t, r, o);
3218
- try {
3219
- const s = await Me(l, ve(i), a);
3220
- if (!s) throw new Error("Failed to create blob from canvas");
3221
- return s;
3222
- } finally {
3223
- Rt(l);
3224
- }
3225
- }
3226
- /**
3227
- * Main thread export to data URL (fallback/legacy method).
3228
- *
3229
- * On iOS WebKit this is also the *primary* path (`isIOSWebKit()` skips the
3230
- * worker), so font readiness matters here even when no fallback occurred.
3231
- */
3232
- static async mainThreadExportToDataURL(t, r, n, o = {}) {
3233
- const { format: i = "png", quality: a = 0.95 } = o, l = await this.renderOnMainThread(t, r, o);
3234
- try {
3235
- return l.toDataURL(ve(i), a);
3236
- } finally {
3237
- Rt(l);
3238
- }
3239
- }
3240
- /**
3241
- * One artboard's PRINT file: a lossless PNG of the artboard (or of `region`, a rect of it in artboard units) at
3242
- * `scale` pixels per artboard unit, painted from every image's ORIGINAL pixels (the worker's 4,000 px bitmap cap,
3243
- * `DEFAULT_MAX_BITMAP_DIMENSION`, keeps the live preview light; a print needs what the shopper uploaded).
3244
- *
3245
- * Painted on the main thread (the worker would cache an uncapped copy of every picture), one canvas, released as
3246
- * soon as it is encoded: the caller asks for files one at a time, so a phone holds one print canvas at most.
3247
- * Throws `E_PRINT_FILE_FAILED` (typed, with the size asked for) when the device could not hold or encode it.
3248
- */
3249
- static async exportPrintFile(t, r, n) {
3250
- const { scale: o, region: i } = n;
3251
- if (!(o > 0) || !Number.isFinite(o))
3252
- throw new gt("E_PRINT_FILE_FAILED", `scale must be a finite number above 0 (got ${o}).`, { artboard: t.name, scale: o });
3253
- const a = await this.renderOnMainThread(t, r, { scale: o, transparentBackground: !0 }, i), l = a.width, s = a.height;
3254
- try {
3255
- const c = await Me(a, "image/png");
3256
- if (!c || c.size === 0)
3257
- throw new gt(
3258
- "E_PRINT_FILE_FAILED",
3259
- `the device could not make the ${l} x ${s} px print file of "${t.name}"${i ? ` (region ${i.id})` : ""}: its canvas was refused. Ask for a smaller scale for this device.`,
3260
- { artboard: t.name, region: i == null ? void 0 : i.id, width: l, height: s, scale: o }
3261
- );
3262
- return { blob: c, width: l, height: s, scale: o };
3263
- } finally {
3264
- Rt(a);
3265
- }
3266
- }
3267
- /**
3268
- * Paint an artboard (or a rect of it) on a detached main-thread canvas: background, compositing (knockout / clip),
3269
- * every visible element with its masks / distress / layer effects, then the artboard's distress texture and image
3270
- * mask. The one main-thread painter: the blob, data URL and print exports all use it (the blob path used to skip
3271
- * masks, distress and the artboard effects the data URL path drew). A hidden layer is not part of the design and is
3272
- * never painted (as the worker serializer drops it).
3273
- */
3274
- static async renderOnMainThread(t, r, n, o) {
3275
- var b, T;
3276
- const {
3277
- scale: i = 1,
3278
- transparentBackground: a = !1,
3279
- backgroundColor: l = t.backgroundColor
3280
- } = n, s = r.filter((g) => g.visible !== !1);
3281
- await Jn(s);
3282
- const c = o ?? { x: 0, y: 0, width: t.width, height: t.height }, h = document.createElement("canvas");
3283
- h.width = Math.max(1, Math.round(c.width * i)), h.height = Math.max(1, Math.round(c.height * i));
3284
- const f = h.getContext("2d");
3285
- if (f.scale(i, i), (c.x !== 0 || c.y !== 0) && f.translate(-c.x, -c.y), !a && t.exportBackground) {
3286
- if (f.save(), this.applyExportBackgroundClipPath(f, t), t.backgroundType === "texture" && t.backgroundTexture) {
3287
- const g = pt.getTexturePresetColor(t.backgroundTexture);
3288
- f.fillStyle = g, f.fillRect(0, 0, t.width, t.height);
3289
- const y = pt.getTexture(t.backgroundTexture);
3290
- y && y.complete ? f.drawImage(y, 0, 0, t.width, t.height) : _.warn("Texture not loaded for export:", t.backgroundTexture);
3291
- } else
3292
- f.fillStyle = l || t.backgroundColor, f.fillRect(0, 0, t.width, t.height);
3293
- f.restore();
3294
- }
3295
- f.save(), f.translate(-t.x, -t.y), this.applyExportClipPath(f, t);
3296
- const u = new Set(t.getElementIds()), d = s.filter((g) => u.has(g.id)), p = /* @__PURE__ */ new Map();
3297
- d.forEach((g) => p.set(g.id, g));
3298
- const m = (g, y) => {
3299
- if (!y.id) return;
3300
- const S = p.get(y.id);
3301
- S ? S.render(g, !1) : _.warn("Element not found in map:", y.id);
3302
- }, w = d.map((g) => g.toJSON()), k = /* @__PURE__ */ new Set();
3303
- return d.forEach((g, y) => {
3304
- var S;
3305
- if (g.blendMode === "knockout" || g.blendMode === "clip") {
3306
- const C = w[y], P = g.blendMode === "knockout" ? "destination-out" : "destination-in", I = ((S = C.knockoutParts) == null ? void 0 : S.scope) || "group", M = w.findIndex((A) => A.id === C.id);
3307
- if (M !== -1 && I === "artboard")
3308
- for (let A = 0; A < M; A++) {
3309
- const E = w[A].id;
3310
- E && k.add(E);
3311
- }
3312
- qe(
3313
- f,
3314
- C,
3315
- w,
3316
- m,
3317
- a || !t.exportBackground ? void 0 : l,
3318
- t,
3319
- P
3320
- ), k.add(g.id);
3321
- }
3322
- }), d.forEach((g, y) => {
3323
- var S;
3324
- if (!k.has(g.id)) {
3325
- const C = w[y], P = ((S = C == null ? void 0 : C.distressEffect) == null ? void 0 : S.enabled) && C.distressEffect.style === "custom" && C.distressEffect.textureUrl, I = (C == null ? void 0 : C.masks) && C.masks.length > 0, M = (A) => {
3326
- g.render(A, !1);
3327
- };
3328
- Mn(f, C, (A) => {
3329
- if (I && P)
3330
- fe(A, C, (E) => {
3331
- const v = g.getVisualBoundingBox();
3332
- ye(E, C.distressEffect, v, M);
3333
- });
3334
- else if (I)
3335
- fe(A, C, M);
3336
- else if (P) {
3337
- const E = g.getVisualBoundingBox();
3338
- ye(A, C.distressEffect, E, M);
3339
- } else
3340
- g.render(A, !1);
3341
- });
3342
- }
3343
- }), (b = t.distressTexture) != null && b.enabled && On(f, t.distressTexture, {
3344
- x: t.x,
3345
- y: t.y,
3346
- width: t.width,
3347
- height: t.height
3348
- }), (T = t.imageMask) != null && T.enabled && Tn(f, t.imageMask, {
3349
- x: t.x,
3350
- y: t.y,
3351
- width: t.width,
3352
- height: t.height
3353
- }), f.restore(), h;
3354
- }
3355
- /**
3356
- * Apply clip path for export background (uses 0,0 as origin since background is drawn before translate)
3357
- */
3358
- static applyExportBackgroundClipPath(t, r) {
3359
- const { width: n, height: o, clipShape: i } = r, a = 0, l = 0;
3360
- if (t.beginPath(), !i || i === "rectangle")
3361
- t.rect(a, l, n, o);
3362
- else if (i === "circle") {
3363
- const s = a + n / 2, c = l + o / 2, h = Math.min(n, o) / 2;
3364
- t.arc(s, c, h, 0, Math.PI * 2);
3365
- } else if (typeof i == "object" && i.type === "rounded") {
3366
- const s = Math.min(i.radius, Math.min(n, o) / 2);
3367
- typeof t.roundRect == "function" ? t.roundRect(a, l, n, o, s) : (t.moveTo(a + s, l), t.lineTo(a + n - s, l), t.arcTo(a + n, l, a + n, l + s, s), t.lineTo(a + n, l + o - s), t.arcTo(a + n, l + o, a + n - s, l + o, s), t.lineTo(a + s, l + o), t.arcTo(a, l + o, a, l + o - s, s), t.lineTo(a, l + s), t.arcTo(a, l, a + s, l, s));
3368
- } else if (typeof i == "object" && i.type === "path")
3369
- try {
3370
- const s = new Path2D(i.d);
3371
- t.clip(s);
3372
- return;
3373
- } catch (s) {
3374
- _.warn("Invalid SVG path, falling back to rectangle:", s), t.rect(a, l, n, o);
3375
- }
3376
- t.clip();
3377
- }
3378
- /**
3379
- * Apply clip path for export based on artboard's clipShape
3380
- * This creates the clip region for the given artboard during export
3381
- */
3382
- static applyExportClipPath(t, r) {
3383
- const { x: n, y: o, width: i, height: a, clipShape: l } = r;
3384
- if (t.beginPath(), !l || l === "rectangle")
3385
- t.rect(n, o, i, a);
3386
- else if (l === "circle") {
3387
- const s = n + i / 2, c = o + a / 2, h = Math.min(i, a) / 2;
3388
- t.arc(s, c, h, 0, Math.PI * 2);
3389
- } else if (typeof l == "object" && l.type === "rounded") {
3390
- const s = Math.min(l.radius, Math.min(i, a) / 2);
3391
- typeof t.roundRect == "function" ? t.roundRect(n, o, i, a, s) : (t.moveTo(n + s, o), t.lineTo(n + i - s, o), t.arcTo(n + i, o, n + i, o + s, s), t.lineTo(n + i, o + a - s), t.arcTo(n + i, o + a, n + i - s, o + a, s), t.lineTo(n + s, o + a), t.arcTo(n, o + a, n, o + a - s, s), t.lineTo(n, o + s), t.arcTo(n, o, n + s, o, s));
3392
- } else if (typeof l == "object" && l.type === "path")
3393
- try {
3394
- const s = new Path2D(l.d);
3395
- t.translate(n, o), t.clip(s), t.translate(-n, -o);
3396
- return;
3397
- } catch (s) {
3398
- _.warn("Invalid SVG path, falling back to rectangle:", s), t.rect(n, o, i, a);
3399
- }
3400
- t.clip();
3401
- }
3402
- /**
3403
- * Export all artboards as separate images (HIGH PERFORMANCE - parallel rendering)
3404
- */
3405
- static async exportAllArtboardsToImages(t, r, n, o = {}) {
3406
- const i = t.map(
3407
- (a) => this.exportArtboardToImage(a, r, n, {
3408
- ...o,
3409
- filename: o.filename ? `${o.filename}-${a.name}.${o.format || "png"}` : void 0
3410
- })
3411
- );
3412
- await Promise.all(i);
3413
- }
3414
- /**
3415
- * Download a blob as a file
3416
- */
3417
- static downloadBlob(t, r) {
3418
- const n = URL.createObjectURL(t), o = document.createElement("a");
3419
- o.href = n, o.download = r, document.body.appendChild(o), o.click(), document.body.removeChild(o), URL.revokeObjectURL(n);
3420
- }
3421
- /**
3422
- * Convert data URL to blob
3423
- */
3424
- static async dataUrlToBlob(t) {
3425
- return (await fetch(t)).blob();
3426
- }
3427
- /**
3428
- * Get export summary (for UI display)
3429
- */
3430
- static getExportSummary(t, r) {
3431
- const n = t.length, o = r.length, l = n * 200 + o * 500, s = Math.ceil(l / 1024);
3432
- return {
3433
- artboardCount: n,
3434
- totalElementCount: o,
3435
- estimatedFileSize: s < 1024 ? `${s} KB` : `${(s / 1024).toFixed(1)} MB`
3436
- };
3437
- }
3438
- /**
3439
- * Performance benchmark: Compare worker vs main thread export
3440
- * Returns timing data for both methods
3441
- */
3442
- static async benchmarkExport(t, r, n, o = {}) {
3443
- const i = performance.now(), a = await this.exportArtboardToDataURL(t, r, n, {
3444
- ...o,
3445
- forceMainThread: !0
3446
- }), l = performance.now() - i, s = {
3447
- workerSupported: this.supportsWorkerExport(),
3448
- mainThread: {
3449
- time: l,
3450
- dataUrl: a
3451
- },
3452
- recommendation: ""
3453
- };
3454
- if (this.supportsWorkerExport())
3455
- try {
3456
- const c = performance.now(), h = await this.getWorkerManager();
3457
- if (h) {
3458
- const f = new Set(t.getElementIds()), u = r.filter((w) => f.has(w.id)), d = await h.exportArtboardToDataURL(t, u, {
3459
- scale: o.scale || 1,
3460
- format: (o.format === "jpg" ? "jpeg" : o.format) || "png",
3461
- quality: o.quality || 0.95
3462
- }), p = performance.now() - c, m = l / p;
3463
- return {
3464
- ...s,
3465
- worker: {
3466
- time: p,
3467
- dataUrl: d.dataUrl,
3468
- mainThreadBlockTime: d.mainThreadBlockTime
3469
- },
3470
- speedup: m,
3471
- recommendation: m > 1.2 ? `Worker export is ${m.toFixed(2)}x faster - recommended for continuous export` : m < 0.8 ? `Main thread export is ${(1 / m).toFixed(2)}x faster - worker overhead not worth it for single exports` : "Performance is similar, but worker keeps UI responsive during export"
3472
- };
3473
- }
3474
- } catch (c) {
3475
- _.error("Worker benchmark failed:", c), s.recommendation = "Worker export not available - using main thread export";
3476
- }
3477
- else
3478
- s.recommendation = "Worker export not supported in this browser - using main thread export";
3479
- return s;
3480
- }
3481
- /**
3482
- * Performance benchmark: Test continuous exports to measure UI impact
3483
- * Returns statistics about export performance over time
3484
- */
3485
- static async benchmarkContinuousExport(t, r, n, o = {}) {
3486
- const {
3487
- duration: i = 5e3,
3488
- // 5 seconds
3489
- interval: a = 500,
3490
- // Every 500ms
3491
- useWorker: l = this.supportsWorkerExport()
3492
- } = o, s = [];
3493
- let c = 0, h = !0;
3494
- for (setTimeout(() => {
3495
- h = !1;
3496
- }, i); h; ) {
3497
- const b = performance.now();
3498
- await this.exportArtboardToDataURL(t, r, n, {
3499
- forceMainThread: !l
3500
- });
3501
- const T = performance.now() - b;
3502
- s.push(T), l || (c += T), await new Promise((g) => setTimeout(g, Math.max(0, a - T)));
3503
- }
3504
- const f = s.length, u = s.reduce((b, T) => b + T, 0) / f, d = Math.min(...s), p = Math.max(...s), m = f / i * 1e3, w = c / i * 100, k = w > 20 ? `Main thread blocked ${w.toFixed(1)}% of the time - STRONGLY recommend worker export` : w > 10 ? `Main thread blocked ${w.toFixed(1)}% of the time - consider worker export` : `Main thread blocked ${w.toFixed(1)}% of the time - performance is acceptable`;
3505
- return {
3506
- totalExports: f,
3507
- duration: i,
3508
- averageTime: u,
3509
- minTime: d,
3510
- maxTime: p,
3511
- totalMainThreadBlockTime: c,
3512
- exportsPerSecond: m,
3513
- recommendation: k
3514
- };
3515
- }
3516
- /**
3517
- * Get performance statistics for the worker (if initialized)
3518
- */
3519
- static getWorkerStats() {
3520
- var t;
3521
- return {
3522
- workerSupported: this.supportsWorkerExport(),
3523
- workerInitialized: this.workerManager !== null,
3524
- workerStatus: (t = this.workerManager) == null ? void 0 : t.getStatus()
3525
- };
3526
- }
3527
- /**
3528
- * Get recommended export options for t-shirt designs
3529
- * T-shirt designs should ALWAYS use transparent backgrounds with knockout effects
3530
- */
3531
- static getTShirtExportOptions(t = {}) {
3532
- return {
3533
- format: "png",
3534
- // PNG for transparency support
3535
- scale: 2,
3536
- // 2x for print quality
3537
- quality: 1,
3538
- // Maximum quality
3539
- ...t,
3540
- // Force transparent background - critical for knockout effects
3541
- transparentBackground: !0
3542
- };
3543
- }
3544
- /**
3545
- * Get recommended export options for pillow/pattern designs
3546
- * These may use group-scoped knockout with pattern backgrounds
3547
- */
3548
- static getPatternExportOptions(t = {}) {
3549
- return {
3550
- format: "png",
3551
- scale: 2,
3552
- transparentBackground: !1,
3553
- // Patterns often include background
3554
- quality: 1,
3555
- ...t
3556
- };
3557
- }
3558
- /**
3559
- * Validate export options for common issues
3560
- * Returns warnings if options might cause problems
3561
- */
3562
- static validateExportOptions(t, r) {
3563
- const n = [];
3564
- return r && !t.transparentBackground && n.push(
3565
- "WARNING: Document has knockout effects but transparentBackground is false. Knockout effects work best with transparent backgrounds for t-shirt designs."
3566
- ), t.transparentBackground && (t.format === "jpg" || t.format === "jpeg") && n.push("WARNING: JPEG format does not support transparency. Use PNG format for transparent backgrounds."), t.quality && t.quality < 0.9 && n.push("NOTE: Using quality < 0.9 may reduce the appearance of distress and other effects."), t.scale && t.scale < 2 && n.push("NOTE: For print-ready designs, recommend scale of 2x or higher."), n;
3567
- }
3568
- /**
3569
- * Check if any elements in the list have knockout effects
3570
- */
3571
- static hasKnockoutEffects(t) {
3572
- const r = (n) => (n.blendMode === "knockout" || n.blendMode === "clip") && n.knockoutParts ? !0 : "children" in n && Array.isArray(n.children) ? n.children.some((o) => r(o)) : !1;
3573
- return t.some((n) => r(n));
3574
- }
3575
- /**
3576
- * CRITICAL: Ensure artboard is export-safe
3577
- * Strips preview background color to prevent accidental export
3578
- * Returns a copy of the artboard safe for export
3579
- */
3580
- static makeArtboardExportSafe(t) {
3581
- const r = Object.assign(Object.create(Object.getPrototypeOf(t)), t);
3582
- return "previewBackgroundColor" in r && delete r.previewBackgroundColor, r;
3583
- }
3584
- };
3585
- lt.CURRENT_VERSION = "2.0.0", lt.workerManager = null, lt.workerInitPromise = null;
3586
- let Tt = lt;
3587
- const po = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
3588
- __proto__: null,
3589
- ExportManager: Tt,
3590
- default: Tt
3591
- }, Symbol.toStringTag, { value: "Module" }));
3592
- export {
3593
- fe as A,
3594
- go as B,
3595
- Ye as C,
3596
- Ge as D,
3597
- wt as E,
3598
- Wr as F,
3599
- Xt as G,
3600
- fo as H,
3601
- be as I,
3602
- Vn as J,
3603
- Yn as K,
3604
- qe as L,
3605
- Mn as M,
3606
- On as N,
3607
- Tn as O,
3608
- zt as P,
3609
- ye as Q,
3610
- Zn as R,
3611
- Un as S,
3612
- pt as T,
3613
- Rn as U,
3614
- lo as V,
3615
- ft as W,
3616
- dn as X,
3617
- fn as Y,
3618
- kr as Z,
3619
- po as _,
3620
- Tt as a,
3621
- ro as b,
3622
- Ie as c,
3623
- q as d,
3624
- co as e,
3625
- io as f,
3626
- jt as g,
3627
- xn as h,
3628
- Mr as i,
3629
- to as j,
3630
- Dn as k,
3631
- ct as l,
3632
- uo as m,
3633
- so as n,
3634
- Kn as o,
3635
- ae as p,
3636
- ho as q,
3637
- Cr as r,
3638
- Ut as s,
3639
- no as t,
3640
- eo as u,
3641
- Ar as v,
3642
- oo as w,
3643
- Rr as x,
3644
- Ae as y,
3645
- ao as z
3646
- };
3647
- //# sourceMappingURL=ExportManager-DEJgxB0S.js.map