@magicpages/kalotyp-core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/canvas/bake-canvas.d.ts +25 -0
  2. package/dist/canvas/bake-canvas.d.ts.map +1 -0
  3. package/dist/canvas/load-image.d.ts +16 -0
  4. package/dist/canvas/load-image.d.ts.map +1 -0
  5. package/dist/canvas/viewport-controller.d.ts +46 -0
  6. package/dist/canvas/viewport-controller.d.ts.map +1 -0
  7. package/dist/canvas/viewport.d.ts +43 -0
  8. package/dist/canvas/viewport.d.ts.map +1 -0
  9. package/dist/events/event-bus.d.ts +10 -0
  10. package/dist/events/event-bus.d.ts.map +1 -0
  11. package/dist/geometry/rect.d.ts +35 -0
  12. package/dist/geometry/rect.d.ts.map +1 -0
  13. package/dist/history/history.d.ts +38 -0
  14. package/dist/history/history.d.ts.map +1 -0
  15. package/dist/index.d.ts +46 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +1976 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/output/state.d.ts +27 -0
  20. package/dist/output/state.d.ts.map +1 -0
  21. package/dist/pipeline/encode.d.ts +27 -0
  22. package/dist/pipeline/encode.d.ts.map +1 -0
  23. package/dist/pipeline/exif.d.ts +16 -0
  24. package/dist/pipeline/exif.d.ts.map +1 -0
  25. package/dist/pipeline/run-chain.d.ts +10 -0
  26. package/dist/pipeline/run-chain.d.ts.map +1 -0
  27. package/dist/plugins/annotate/bake.d.ts +16 -0
  28. package/dist/plugins/annotate/bake.d.ts.map +1 -0
  29. package/dist/plugins/annotate/geometry.d.ts +36 -0
  30. package/dist/plugins/annotate/geometry.d.ts.map +1 -0
  31. package/dist/plugins/annotate/hit-test.d.ts +8 -0
  32. package/dist/plugins/annotate/hit-test.d.ts.map +1 -0
  33. package/dist/plugins/annotate/smooth.d.ts +16 -0
  34. package/dist/plugins/annotate/smooth.d.ts.map +1 -0
  35. package/dist/plugins/annotate/state.d.ts +176 -0
  36. package/dist/plugins/annotate/state.d.ts.map +1 -0
  37. package/dist/plugins/crop/aspect-ratio.d.ts +15 -0
  38. package/dist/plugins/crop/aspect-ratio.d.ts.map +1 -0
  39. package/dist/plugins/crop/bake.d.ts +13 -0
  40. package/dist/plugins/crop/bake.d.ts.map +1 -0
  41. package/dist/plugins/crop/preset-filter.d.ts +10 -0
  42. package/dist/plugins/crop/preset-filter.d.ts.map +1 -0
  43. package/dist/plugins/crop/resize.d.ts +19 -0
  44. package/dist/plugins/crop/resize.d.ts.map +1 -0
  45. package/dist/plugins/crop/state.d.ts +23 -0
  46. package/dist/plugins/crop/state.d.ts.map +1 -0
  47. package/dist/plugins/filter/presets.d.ts +19 -0
  48. package/dist/plugins/filter/presets.d.ts.map +1 -0
  49. package/dist/plugins/finetune/bake.d.ts +5 -0
  50. package/dist/plugins/finetune/bake.d.ts.map +1 -0
  51. package/dist/plugins/finetune/math.d.ts +32 -0
  52. package/dist/plugins/finetune/math.d.ts.map +1 -0
  53. package/dist/plugins/finetune/state.d.ts +31 -0
  54. package/dist/plugins/finetune/state.d.ts.map +1 -0
  55. package/dist/plugins/flip/bake.d.ts +5 -0
  56. package/dist/plugins/flip/bake.d.ts.map +1 -0
  57. package/dist/plugins/flip/state.d.ts +9 -0
  58. package/dist/plugins/flip/state.d.ts.map +1 -0
  59. package/dist/plugins/frame/bake.d.ts +16 -0
  60. package/dist/plugins/frame/bake.d.ts.map +1 -0
  61. package/dist/plugins/frame/state.d.ts +29 -0
  62. package/dist/plugins/frame/state.d.ts.map +1 -0
  63. package/dist/plugins/redact/bake.d.ts +13 -0
  64. package/dist/plugins/redact/bake.d.ts.map +1 -0
  65. package/dist/plugins/redact/state.d.ts +112 -0
  66. package/dist/plugins/redact/state.d.ts.map +1 -0
  67. package/dist/plugins/resize/bake.d.ts +9 -0
  68. package/dist/plugins/resize/bake.d.ts.map +1 -0
  69. package/dist/plugins/resize/state.d.ts +37 -0
  70. package/dist/plugins/resize/state.d.ts.map +1 -0
  71. package/dist/plugins/rotate/bake.d.ts +9 -0
  72. package/dist/plugins/rotate/bake.d.ts.map +1 -0
  73. package/dist/plugins/rotate/inscribe.d.ts +14 -0
  74. package/dist/plugins/rotate/inscribe.d.ts.map +1 -0
  75. package/dist/plugins/rotate/state.d.ts +21 -0
  76. package/dist/plugins/rotate/state.d.ts.map +1 -0
  77. package/dist/plugins/utility.d.ts +59 -0
  78. package/dist/plugins/utility.d.ts.map +1 -0
  79. package/dist/state/store.d.ts +14 -0
  80. package/dist/state/store.d.ts.map +1 -0
  81. package/package.json +52 -0
package/dist/index.js ADDED
@@ -0,0 +1,1976 @@
1
+ class Dn {
2
+ listeners = /* @__PURE__ */ new Map();
3
+ on(n, e) {
4
+ let i = this.listeners.get(n);
5
+ return i || (i = /* @__PURE__ */ new Set(), this.listeners.set(n, i)), i.add(e), () => {
6
+ i?.delete(e);
7
+ };
8
+ }
9
+ off(n, e) {
10
+ this.listeners.get(n)?.delete(e);
11
+ }
12
+ emit(n, e) {
13
+ const i = this.listeners.get(n);
14
+ if (i)
15
+ for (const r of [...i])
16
+ try {
17
+ r(e);
18
+ } catch (o) {
19
+ queueMicrotask(() => {
20
+ throw o;
21
+ });
22
+ }
23
+ }
24
+ clear() {
25
+ this.listeners.clear();
26
+ }
27
+ }
28
+ async function Ln(t) {
29
+ if (typeof createImageBitmap == "function") {
30
+ const n = await wt(t);
31
+ if (n)
32
+ try {
33
+ const e = await createImageBitmap(n, { imageOrientation: "from-image" });
34
+ return { element: e, width: e.width, height: e.height };
35
+ } catch {
36
+ }
37
+ }
38
+ return xt(t);
39
+ }
40
+ async function wt(t) {
41
+ if (t instanceof Blob) return t;
42
+ if (typeof fetch != "function") return null;
43
+ try {
44
+ const n = await fetch(t, { credentials: "omit" });
45
+ return n.ok ? await n.blob() : null;
46
+ } catch {
47
+ return null;
48
+ }
49
+ }
50
+ async function xt(t) {
51
+ const n = typeof t == "string" ? t : URL.createObjectURL(t), e = typeof t != "string";
52
+ try {
53
+ const i = await new Promise((r, o) => {
54
+ const a = new Image();
55
+ a.crossOrigin = "anonymous", a.onload = () => r(a), a.onerror = () => o(new Error(`Failed to load image: ${n}`)), a.src = n;
56
+ });
57
+ return {
58
+ element: i,
59
+ width: i.naturalWidth,
60
+ height: i.naturalHeight
61
+ };
62
+ } finally {
63
+ e && URL.revokeObjectURL(n);
64
+ }
65
+ }
66
+ const k = Object.freeze({
67
+ zoom: 1,
68
+ panX: 0,
69
+ panY: 0
70
+ });
71
+ function pt(t, n, e = k) {
72
+ const i = Math.max(0, t.width - t.padding * 2), r = Math.max(0, t.height - t.padding * 2);
73
+ if (n.width <= 0 || n.height <= 0 || i <= 0 || r <= 0)
74
+ return {
75
+ displayRect: { x: t.padding, y: t.padding, width: 0, height: 0 },
76
+ scale: 0
77
+ };
78
+ const o = Math.min(i / n.width, r / n.height), a = Math.max(0, e.zoom || 0), c = o * a, l = n.width * c, s = n.height * c, u = t.padding + (i - l) / 2, h = t.padding + (r - s) / 2, f = u + e.panX, d = h + e.panY, m = z(f, u, l, i, t.padding), p = z(d, h, s, r, t.padding);
79
+ return {
80
+ displayRect: { x: m, y: p, width: l, height: s },
81
+ scale: c
82
+ };
83
+ }
84
+ function z(t, n, e, i, r) {
85
+ if (e <= i) return n;
86
+ const o = r, a = r + i, c = o + 1 - e, l = a - 1;
87
+ return t < c ? c : t > l ? l : t;
88
+ }
89
+ function Bn(t, n) {
90
+ return {
91
+ x: n.displayRect.x + t.x * n.scale,
92
+ y: n.displayRect.y + t.y * n.scale
93
+ };
94
+ }
95
+ function Wn(t, n) {
96
+ return n.scale === 0 ? { x: 0, y: 0 } : {
97
+ x: (t.x - n.displayRect.x) / n.scale,
98
+ y: (t.y - n.displayRect.y) / n.scale
99
+ };
100
+ }
101
+ function Yn(t, n) {
102
+ return {
103
+ x: n.displayRect.x + t.x * n.scale,
104
+ y: n.displayRect.y + t.y * n.scale,
105
+ width: t.width * n.scale,
106
+ height: t.height * n.scale
107
+ };
108
+ }
109
+ function Xn(t, n) {
110
+ return n.scale === 0 ? { x: 0, y: 0, width: 0, height: 0 } : {
111
+ x: (t.x - n.displayRect.x) / n.scale,
112
+ y: (t.y - n.displayRect.y) / n.scale,
113
+ width: t.width / n.scale,
114
+ height: t.height / n.scale
115
+ };
116
+ }
117
+ const ot = 8, at = 0.25;
118
+ class Un {
119
+ current;
120
+ isPinching;
121
+ listeners;
122
+ constructor(n = k) {
123
+ this.current = D(n), this.isPinching = !1, this.listeners = /* @__PURE__ */ new Set();
124
+ }
125
+ getTransform() {
126
+ return this.current;
127
+ }
128
+ getSnapshot() {
129
+ return { transform: this.current, pinching: this.isPinching };
130
+ }
131
+ setTransform(n) {
132
+ const e = D(n);
133
+ L(this.current, e) || (this.current = e, this.emit());
134
+ }
135
+ /**
136
+ * Apply a multiplicative zoom delta anchored on a stage CSS-pixel point.
137
+ * The point under `anchor` stays under `anchor` after the zoom.
138
+ *
139
+ * Pan is the displaced image-center's offset from the stage center, so the
140
+ * dolly-zoom reduces to `pan_new = ratio · pan_old + (1 − ratio) · (anchor − stageCenter)`,
141
+ * independent of the active plugin's image intrinsic.
142
+ */
143
+ zoomAt(n, e, i) {
144
+ if (!Number.isFinite(n) || n <= 0) return this.current;
145
+ const r = this.current.zoom, o = r * n, a = ct(o, at, ot);
146
+ if (a === r) return this.current;
147
+ const c = a / r, l = e.x - i.x, s = e.y - i.y, u = c * this.current.panX + (1 - c) * l, h = c * this.current.panY + (1 - c) * s;
148
+ return this.current = { zoom: a, panX: u, panY: h }, this.emit(), this.current;
149
+ }
150
+ /**
151
+ * Translate pan by `(dx, dy)` in stage CSS pixels at the current zoom.
152
+ * Pan is stored raw; `computeViewport` clamps at emission time so the
153
+ * gesture handler's math stays stable when the user pans past the clamp.
154
+ */
155
+ panBy(n, e) {
156
+ n === 0 && e === 0 || (this.current = {
157
+ zoom: this.current.zoom,
158
+ panX: this.current.panX + n,
159
+ panY: this.current.panY + e
160
+ }, this.emit());
161
+ }
162
+ resetToFit() {
163
+ L(this.current, k) || (this.current = k, this.emit());
164
+ }
165
+ /** Reset only the pan, preserving zoom. Used on plugin switch. */
166
+ resetPan() {
167
+ this.current.panX === 0 && this.current.panY === 0 || (this.current = { zoom: this.current.zoom, panX: 0, panY: 0 }, this.emit());
168
+ }
169
+ /** True while a multi-pointer gesture is active. Heavy plugins read this. */
170
+ getPinching() {
171
+ return this.isPinching;
172
+ }
173
+ setPinching(n) {
174
+ this.isPinching !== n && (this.isPinching = n, this.emit());
175
+ }
176
+ computeViewport(n, e) {
177
+ return pt(n, e, this.current);
178
+ }
179
+ /** Subscribe to transform / pinching changes. Handler runs synchronously after every change. */
180
+ subscribe(n) {
181
+ return this.listeners.add(n), () => {
182
+ this.listeners.delete(n);
183
+ };
184
+ }
185
+ clear() {
186
+ this.listeners.clear();
187
+ }
188
+ emit() {
189
+ const n = this.getSnapshot();
190
+ for (const e of this.listeners) e(n);
191
+ }
192
+ }
193
+ function ct(t, n, e) {
194
+ return t < n ? n : t > e ? e : t;
195
+ }
196
+ function D(t) {
197
+ const n = ct(Number.isFinite(t.zoom) ? t.zoom : 1, at, ot), e = Number.isFinite(t.panX) ? t.panX : 0, i = Number.isFinite(t.panY) ? t.panY : 0;
198
+ return { zoom: n, panX: e, panY: i };
199
+ }
200
+ function L(t, n) {
201
+ return t.zoom === n.zoom && t.panX === n.panX && t.panY === n.panY;
202
+ }
203
+ function g(t, n) {
204
+ if (Mt())
205
+ return { kind: "offscreen", canvas: new OffscreenCanvas(t, n) };
206
+ const e = document.createElement("canvas");
207
+ return e.width = t, e.height = n, { kind: "html", canvas: e };
208
+ }
209
+ function y(t) {
210
+ if (t.kind === "offscreen") {
211
+ const e = t.canvas.getContext("2d");
212
+ if (!e) throw new Error("2D canvas context is not available");
213
+ return e;
214
+ }
215
+ const n = t.canvas.getContext("2d");
216
+ if (!n) throw new Error("2D canvas context is not available");
217
+ return n;
218
+ }
219
+ async function st(t, n, e) {
220
+ return t.kind === "offscreen" ? t.canvas.convertToBlob({ type: n, quality: e }) : new Promise((i, r) => {
221
+ t.canvas.toBlob(
222
+ (o) => {
223
+ o ? i(o) : r(new Error("toBlob produced null"));
224
+ },
225
+ n,
226
+ e
227
+ );
228
+ });
229
+ }
230
+ function Mt() {
231
+ return typeof OffscreenCanvas > "u" ? !1 : typeof OffscreenCanvas.prototype.convertToBlob == "function";
232
+ }
233
+ const B = /* @__PURE__ */ new Map();
234
+ function b(t) {
235
+ const n = B.get(t);
236
+ if (n) return n;
237
+ const e = (async () => {
238
+ try {
239
+ const i = g(1, 1), r = await st(i, t, 0.5);
240
+ return r.type === t && r.size > 0;
241
+ } catch {
242
+ return !1;
243
+ }
244
+ })();
245
+ return B.set(t, e), e;
246
+ }
247
+ function Hn(t, n) {
248
+ const e = Math.min(t.x, n.x), i = Math.min(t.y, n.y), r = Math.abs(t.x - n.x), o = Math.abs(t.y - n.y);
249
+ return { x: e, y: i, width: r, height: o };
250
+ }
251
+ function qn(t) {
252
+ return t.x + t.width;
253
+ }
254
+ function jn(t) {
255
+ return t.y + t.height;
256
+ }
257
+ function $n(t) {
258
+ return { x: t.x + t.width / 2, y: t.y + t.height / 2 };
259
+ }
260
+ function Qn(t, n) {
261
+ return t.x >= n.x && t.x <= n.x + n.width && t.y >= n.y && t.y <= n.y + n.height;
262
+ }
263
+ function Kn(t, n) {
264
+ return t.x === n.x && t.y === n.y && t.width === n.width && t.height === n.height;
265
+ }
266
+ function Vn(t, n, e, i) {
267
+ const r = { x: t.x + n, y: t.y + e, width: t.width, height: t.height };
268
+ return P(r, i);
269
+ }
270
+ function P(t, n) {
271
+ let { x: e, y: i, width: r, height: o } = t;
272
+ return r > n.width && (r = n.width), o > n.height && (o = n.height), e < n.x && (e = n.x), i < n.y && (i = n.y), e + r > n.x + n.width && (e = n.x + n.width - r), i + o > n.y + n.height && (i = n.y + n.height - o), { x: e, y: i, width: r, height: o };
273
+ }
274
+ function St(t) {
275
+ return {
276
+ x: Math.round(t.x),
277
+ y: Math.round(t.y),
278
+ width: Math.round(t.width),
279
+ height: Math.round(t.height)
280
+ };
281
+ }
282
+ function Gn(t) {
283
+ let n = t;
284
+ const e = /* @__PURE__ */ new Set();
285
+ function i(r) {
286
+ for (const o of [...e])
287
+ try {
288
+ o(n, r);
289
+ } catch (a) {
290
+ queueMicrotask(() => {
291
+ throw a;
292
+ });
293
+ }
294
+ }
295
+ return {
296
+ get() {
297
+ return n;
298
+ },
299
+ set(r) {
300
+ const o = n;
301
+ n = { ...n, ...r }, i(o);
302
+ },
303
+ update(r) {
304
+ const o = n;
305
+ n = { ...n, ...r(n) }, i(o);
306
+ },
307
+ subscribe(r) {
308
+ return e.add(r), () => e.delete(r);
309
+ }
310
+ };
311
+ }
312
+ const W = 50;
313
+ class Jn {
314
+ shadow;
315
+ undoStack = [];
316
+ redoStack = [];
317
+ constructor(n) {
318
+ this.shadow = E(n);
319
+ }
320
+ /**
321
+ * Capture a commit. Previous shadow → undo stack; `current` → shadow.
322
+ * Redo is cleared because a new commit branches the history. A commit
323
+ * structurally equal to the shadow is a no-op so duplicates (clicking
324
+ * the same preset twice) don't pollute the undo stack.
325
+ */
326
+ commit(n) {
327
+ bt(n, this.shadow) || (this.undoStack.push(this.shadow), this.undoStack.length > W && this.undoStack.shift(), this.shadow = E(n), this.redoStack.length = 0);
328
+ }
329
+ canUndo() {
330
+ return this.undoStack.length > 0;
331
+ }
332
+ canRedo() {
333
+ return this.redoStack.length > 0;
334
+ }
335
+ /** Pop the most recent prior snapshot; `current` goes onto the redo stack. Returns null if undo is empty. */
336
+ undo(n) {
337
+ const e = this.undoStack.pop();
338
+ return e ? (this.redoStack.push(E(n)), this.shadow = e, { snapshot: e, changed: Y(n, e) }) : null;
339
+ }
340
+ /** Pop the most recent redo entry; `current` goes onto the undo stack. Returns null if redo is empty. */
341
+ redo(n) {
342
+ const e = this.redoStack.pop();
343
+ return e ? (this.undoStack.push(E(n)), this.undoStack.length > W && this.undoStack.shift(), this.shadow = e, { snapshot: e, changed: Y(n, e) }) : null;
344
+ }
345
+ /** Test/debug helpers; not part of the editor's runtime API. */
346
+ size() {
347
+ return { undo: this.undoStack.length, redo: this.redoStack.length };
348
+ }
349
+ }
350
+ function bt(t, n) {
351
+ if (t.size !== n.size) return !1;
352
+ for (const [e, i] of t)
353
+ if (!n.has(e) || C(i) !== C(n.get(e))) return !1;
354
+ return !0;
355
+ }
356
+ function Y(t, n) {
357
+ const e = /* @__PURE__ */ new Set();
358
+ for (const [i, r] of n)
359
+ C(r) !== C(t.get(i)) && e.add(i);
360
+ for (const i of t.keys())
361
+ n.has(i) || e.add(i);
362
+ return e;
363
+ }
364
+ function E(t) {
365
+ const n = /* @__PURE__ */ new Map();
366
+ for (const [e, i] of t)
367
+ n.set(e, structuredClone(i));
368
+ return n;
369
+ }
370
+ function C(t) {
371
+ return JSON.stringify(t, (n, e) => {
372
+ if (e && typeof e == "object" && !Array.isArray(e)) {
373
+ const i = {};
374
+ for (const r of Object.keys(e).sort())
375
+ i[r] = e[r];
376
+ return i;
377
+ }
378
+ return e;
379
+ });
380
+ }
381
+ function O(t, n) {
382
+ if (n <= 0 || t.width <= 0 || t.height <= 0)
383
+ return { x: t.x, y: t.y, width: 0, height: 0 };
384
+ const e = t.width / t.height;
385
+ let i, r;
386
+ return n >= e ? (i = t.width, r = i / n) : (r = t.height, i = r * n), {
387
+ x: t.x + (t.width - i) / 2,
388
+ y: t.y + (t.height - r) / 2,
389
+ width: i,
390
+ height: r
391
+ };
392
+ }
393
+ function Et(t, n, e, i) {
394
+ if (n <= 0) return t;
395
+ if (t.width <= 0 || t.height <= 0) return O(i, n);
396
+ const r = t.width / t.height;
397
+ let o, a;
398
+ r > n ? (a = t.height, o = a * n) : (o = t.width, a = o / n);
399
+ const c = lt(t, o, a, e), l = P(c, i), s = l.height === 0 ? 0 : l.width / l.height;
400
+ return Math.abs(s - n) <= Rt ? l : It(l, n, e);
401
+ }
402
+ const Rt = 1e-6;
403
+ function lt(t, n, e, i) {
404
+ switch (i) {
405
+ case "tl":
406
+ return { x: t.x, y: t.y, width: n, height: e };
407
+ case "tr":
408
+ return { x: t.x + t.width - n, y: t.y, width: n, height: e };
409
+ case "bl":
410
+ return { x: t.x, y: t.y + t.height - e, width: n, height: e };
411
+ case "br":
412
+ return {
413
+ x: t.x + t.width - n,
414
+ y: t.y + t.height - e,
415
+ width: n,
416
+ height: e
417
+ };
418
+ case "center":
419
+ return {
420
+ x: t.x + (t.width - n) / 2,
421
+ y: t.y + (t.height - e) / 2,
422
+ width: n,
423
+ height: e
424
+ };
425
+ }
426
+ }
427
+ function It(t, n, e) {
428
+ const i = O(t, n);
429
+ return lt(t, i.width, i.height, e);
430
+ }
431
+ function Zn(t, n, e, i) {
432
+ const r = i.minSize ?? 1, o = t.x, a = t.y, c = t.x + t.width, l = t.y + t.height;
433
+ let s = o, u = a, h = c, f = l;
434
+ (n === "tl" || n === "l" || n === "bl") && (s = e.x), (n === "tr" || n === "r" || n === "br") && (h = e.x), (n === "tl" || n === "t" || n === "tr") && (u = e.y), (n === "bl" || n === "b" || n === "br") && (f = e.y), (n === "l" || n === "r") && (u = a, f = l), (n === "t" || n === "b") && (s = o, h = c);
435
+ let d = Math.min(s, h), m = Math.min(u, f), p = Math.abs(h - s), A = Math.abs(f - u);
436
+ p < r && (p = r, n === "tl" || n === "l" || n === "bl" ? d = c - r : (n === "tr" || n === "r" || n === "br") && (d = o)), A < r && (A = r, n === "tl" || n === "t" || n === "tr" ? m = l - r : (n === "bl" || n === "b" || n === "br") && (m = a));
437
+ let M = { x: d, y: m, width: p, height: A };
438
+ return M = P(M, i.bounds), i.aspectRatio !== void 0 && i.aspectRatio > 0 && (M = Et(M, i.aspectRatio, Tt(n), i.bounds)), M;
439
+ }
440
+ function Tt(t) {
441
+ switch (t) {
442
+ case "tl":
443
+ return "br";
444
+ case "tr":
445
+ return "bl";
446
+ case "bl":
447
+ return "tr";
448
+ case "br":
449
+ return "tl";
450
+ case "t":
451
+ return "bl";
452
+ case "b":
453
+ return "tl";
454
+ case "l":
455
+ return "tr";
456
+ case "r":
457
+ return "tl";
458
+ }
459
+ }
460
+ function kt(t, n) {
461
+ const [e] = t;
462
+ return e === void 0 || n === void 0 ? !0 : n === "landscape" ? e >= 1 : n === "portrait" ? e < 1 : !0;
463
+ }
464
+ function te(t, n) {
465
+ return t.filter((e) => kt(e, n));
466
+ }
467
+ function ne(t) {
468
+ return {
469
+ rect: { x: 0, y: 0, width: t.imageSize.width, height: t.imageSize.height },
470
+ aspectRatio: void 0,
471
+ activePresetIndex: Ct(t.presets),
472
+ presets: t.presets,
473
+ imageSize: t.imageSize
474
+ };
475
+ }
476
+ function ee(t, n) {
477
+ const e = t.presets[n];
478
+ if (!e) return t;
479
+ const [i] = e;
480
+ if (i === void 0)
481
+ return { ...t, aspectRatio: void 0, activePresetIndex: n };
482
+ const r = {
483
+ x: 0,
484
+ y: 0,
485
+ width: t.imageSize.width,
486
+ height: t.imageSize.height
487
+ }, o = O(r, i);
488
+ return { ...t, rect: o, aspectRatio: i, activePresetIndex: n };
489
+ }
490
+ function Ct(t) {
491
+ return t.findIndex(([n]) => n === void 0);
492
+ }
493
+ function ie(t, n) {
494
+ const e = St(n.rect), i = R(e.x, 0, t.width), r = R(e.y, 0, t.height), o = R(e.width, 1, t.width - i), a = R(e.height, 1, t.height - r), c = g(o, a);
495
+ if (c.kind === "offscreen") {
496
+ const s = c.canvas.getContext("2d");
497
+ if (!s) throw new Error("2D canvas context is not available");
498
+ return s.drawImage(t.bitmap, i, r, o, a, 0, 0, o, a), { bitmap: c.canvas, width: o, height: a, mimeType: t.mimeType };
499
+ }
500
+ const l = c.canvas.getContext("2d");
501
+ if (!l) throw new Error("2D canvas context is not available");
502
+ return l.drawImage(t.bitmap, i, r, o, a, 0, 0, o, a), { bitmap: c.canvas, width: o, height: a, mimeType: t.mimeType };
503
+ }
504
+ function R(t, n, e) {
505
+ return Math.max(n, Math.min(e, t));
506
+ }
507
+ const ut = {
508
+ mimeChoice: "auto",
509
+ quality: 0.85,
510
+ stripMetadata: !0
511
+ }, re = [
512
+ "image/png",
513
+ "image/jpeg",
514
+ "image/webp",
515
+ "image/avif"
516
+ ];
517
+ function ht(t) {
518
+ return Number.isFinite(t) ? t < 0 ? 0 : t > 1 ? 1 : t : ut.quality;
519
+ }
520
+ function oe(t, n) {
521
+ return t.mimeChoice === n ? t : { ...t, mimeChoice: n };
522
+ }
523
+ function ae(t, n) {
524
+ const e = ht(n);
525
+ return t.quality === e ? t : { ...t, quality: e };
526
+ }
527
+ function ce(t, n) {
528
+ return t.stripMetadata === n ? t : { ...t, stripMetadata: n };
529
+ }
530
+ const X = [255, 216], U = [255, 225], H = [69, 120, 105, 102, 0, 0];
531
+ async function vt(t) {
532
+ if (t.output.type && t.output.type !== "image/jpeg") return t.output;
533
+ const n = await q(t.source);
534
+ if (!j(n, X)) return t.output;
535
+ const e = At(n);
536
+ if (!e) return t.output;
537
+ const i = await q(t.output);
538
+ if (!j(i, X)) return t.output;
539
+ const r = new Uint8Array(i.length + e.length);
540
+ return r.set(i.subarray(0, 2), 0), r.set(e, 2), r.set(i.subarray(2), 2 + e.length), new Blob([r], { type: "image/jpeg" });
541
+ }
542
+ async function q(t) {
543
+ return typeof t.arrayBuffer == "function" ? new Uint8Array(await t.arrayBuffer()) : new Promise((n, e) => {
544
+ const i = new FileReader();
545
+ i.onload = () => {
546
+ const r = i.result;
547
+ r instanceof ArrayBuffer ? n(new Uint8Array(r)) : e(new Error("FileReader returned a non-ArrayBuffer result"));
548
+ }, i.onerror = () => e(i.error ?? new Error("FileReader failed")), i.readAsArrayBuffer(t);
549
+ });
550
+ }
551
+ function j(t, n) {
552
+ if (t.length < n.length) return !1;
553
+ for (let e = 0; e < n.length; e++)
554
+ if (t[e] !== n[e]) return !1;
555
+ return !0;
556
+ }
557
+ function At(t) {
558
+ let n = 2;
559
+ for (; n + 4 <= t.length; ) {
560
+ if (t[n] !== 255) return;
561
+ const e = t[n + 1];
562
+ if (e === void 0 || e === 218 || e === 217) return;
563
+ const i = t[n + 2] !== void 0 && t[n + 3] !== void 0 ? t[n + 2] * 256 + t[n + 3] : 0;
564
+ if (i < 2) return;
565
+ const r = n + 2 + i;
566
+ if (r > t.length) return;
567
+ if (t[n] === U[0] && t[n + 1] === U[1] && Nt(t, n + 4))
568
+ return t.slice(n, r);
569
+ n = r;
570
+ }
571
+ }
572
+ function Nt(t, n) {
573
+ for (let e = 0; e < H.length; e++)
574
+ if (t[n + e] !== H[e]) return !1;
575
+ return !0;
576
+ }
577
+ const $ = "image/png", Ft = /* @__PURE__ */ new Set(["image/png", "image/webp", "image/avif"]);
578
+ async function Pt(t, n) {
579
+ return t.mimeChoice !== "auto" ? await b(t.mimeChoice) ? t.mimeChoice : await b("image/webp") ? "image/webp" : $ : await b("image/webp") ? "image/webp" : !Ft.has(n.mimeType) && await b("image/jpeg") ? "image/jpeg" : $;
580
+ }
581
+ function Ot(t, n) {
582
+ const e = _t(n);
583
+ if (!t) return `kalotyp-image.${e}`;
584
+ const i = zt(t);
585
+ if (!i) return `kalotyp-image.${e}`;
586
+ const r = Lt(i);
587
+ return r ? `${r}.${e}` : `kalotyp-image.${e}`;
588
+ }
589
+ async function se(t, n = {}) {
590
+ const e = n.output ?? ut, i = await Pt(e, t), r = ht(e.quality), o = Ot(n.sourceName, i), a = g(t.width, t.height);
591
+ if (a.kind === "offscreen") {
592
+ const u = a.canvas.getContext("2d");
593
+ if (!u) throw new Error("2D canvas context is not available");
594
+ u.drawImage(t.bitmap, 0, 0);
595
+ } else {
596
+ const u = a.canvas.getContext("2d");
597
+ if (!u) throw new Error("2D canvas context is not available");
598
+ u.drawImage(t.bitmap, 0, 0);
599
+ }
600
+ const c = await st(a, i, r), s = n.output?.stripMetadata === !1 && i === "image/jpeg" && t.mimeType === "image/jpeg" && n.sourceBlob !== void 0 ? await vt({ source: n.sourceBlob, output: c }) : c;
601
+ return new File([s], o, { type: i });
602
+ }
603
+ function _t(t) {
604
+ if (t === "image/jpeg") return "jpg";
605
+ if (t === "image/png") return "png";
606
+ if (t === "image/webp") return "webp";
607
+ if (t === "image/avif") return "avif";
608
+ const n = t.split("/")[1];
609
+ return n && n.length > 0 ? n : "bin";
610
+ }
611
+ function zt(t) {
612
+ const e = Dt(t).split(/[/\\]/);
613
+ return e[e.length - 1];
614
+ }
615
+ function Dt(t) {
616
+ const n = t.indexOf("?");
617
+ return n === -1 ? t : t.slice(0, n);
618
+ }
619
+ function Lt(t) {
620
+ const n = t.lastIndexOf(".");
621
+ return n <= 0 ? t : t.slice(0, n);
622
+ }
623
+ async function le(t, n) {
624
+ let e = n;
625
+ for (const i of t)
626
+ e = await i.plugin.bake(i.state, e);
627
+ return e;
628
+ }
629
+ function ue() {
630
+ return { horizontal: !1, vertical: !1 };
631
+ }
632
+ function he(t, n) {
633
+ return n === "horizontal" ? { ...t, horizontal: !t.horizontal } : { ...t, vertical: !t.vertical };
634
+ }
635
+ function Bt(t) {
636
+ return !t.horizontal && !t.vertical;
637
+ }
638
+ async function fe(t, n) {
639
+ if (Bt(t)) return n;
640
+ const { width: e, height: i } = n, r = g(e, i), o = y(r), a = t.horizontal ? -1 : 1, c = t.vertical ? -1 : 1, l = t.horizontal ? e : 0, s = t.vertical ? i : 0;
641
+ return o.setTransform(a, 0, 0, c, l, s), o.drawImage(n.bitmap, 0, 0), {
642
+ bitmap: r.canvas,
643
+ width: e,
644
+ height: i,
645
+ mimeType: n.mimeType
646
+ };
647
+ }
648
+ const Wt = -45, Yt = 45, de = 0.1;
649
+ function ge() {
650
+ return { quarterTurns: 0, freeAngle: 0 };
651
+ }
652
+ function me(t) {
653
+ return { ...t, quarterTurns: (t.quarterTurns + 1) % 4 };
654
+ }
655
+ function ye(t) {
656
+ return { ...t, quarterTurns: (t.quarterTurns + 3) % 4 };
657
+ }
658
+ function we(t, n) {
659
+ const e = Ht(n, Wt, Yt), i = Math.round(e * 10) / 10;
660
+ return { ...t, freeAngle: i };
661
+ }
662
+ function Xt(t) {
663
+ return t.quarterTurns === 0 && Math.abs(t.freeAngle) < 1e-6;
664
+ }
665
+ function Ut(t) {
666
+ return t.quarterTurns * 90 + t.freeAngle;
667
+ }
668
+ function Ht(t, n, e) {
669
+ return Math.max(n, Math.min(e, t));
670
+ }
671
+ function qt(t, n) {
672
+ const e = t.width, i = t.height;
673
+ if (e <= 0 || i <= 0) return { width: 0, height: 0 };
674
+ const r = Math.abs(Math.cos(n)), o = Math.abs(Math.sin(n)), a = e * r + i * o, c = e * o + i * r, l = a > Q ? e * e / a : Number.POSITIVE_INFINITY, s = c > Q ? e * i / c : Number.POSITIVE_INFINITY, u = Math.min(l, s), h = u * i / e;
675
+ return { width: u, height: h };
676
+ }
677
+ const Q = 1e-9;
678
+ async function xe(t, n) {
679
+ if (Xt(t)) return n;
680
+ const e = Ut(t), i = e * Math.PI / 180, r = e - t.quarterTurns * 90, o = Math.abs(r) < 1e-6;
681
+ let a, c;
682
+ if (o)
683
+ t.quarterTurns === 1 || t.quarterTurns === 3 ? (a = n.height, c = n.width) : (a = n.width, c = n.height);
684
+ else {
685
+ const u = qt(n, i);
686
+ a = Math.max(1, Math.round(u.width)), c = Math.max(1, Math.round(u.height));
687
+ }
688
+ const l = g(a, c), s = y(l);
689
+ return s.imageSmoothingEnabled = !0, s.imageSmoothingQuality = "high", s.translate(a / 2, c / 2), s.rotate(i), s.drawImage(n.bitmap, -n.width / 2, -n.height / 2), {
690
+ bitmap: l.canvas,
691
+ width: a,
692
+ height: c,
693
+ mimeType: n.mimeType
694
+ };
695
+ }
696
+ const ft = 8e3, K = 1;
697
+ function pe() {
698
+ return { scaleX: 1, scaleY: 1, lockAspect: !0 };
699
+ }
700
+ function jt(t) {
701
+ return Math.abs(t.scaleX - 1) < 1e-9 && Math.abs(t.scaleY - 1) < 1e-9;
702
+ }
703
+ function $t(t, n) {
704
+ const e = v(Math.round(n.width * t.scaleX)), i = v(Math.round(n.height * t.scaleY));
705
+ return { width: e, height: i };
706
+ }
707
+ function Me(t, n, e) {
708
+ if (e.width <= 0) return t;
709
+ const r = v(Math.round(n)) / e.width, o = t.lockAspect ? r : t.scaleY;
710
+ return { ...t, scaleX: r, scaleY: o };
711
+ }
712
+ function Se(t, n, e) {
713
+ if (e.height <= 0) return t;
714
+ const r = v(Math.round(n)) / e.height, o = t.lockAspect ? r : t.scaleX;
715
+ return { ...t, scaleX: o, scaleY: r };
716
+ }
717
+ function be(t, n) {
718
+ const e = Qt(n / 100);
719
+ return { ...t, scaleX: e, scaleY: e };
720
+ }
721
+ function Ee(t, n) {
722
+ if (t.lockAspect === n) return t;
723
+ if (!n) return { ...t, lockAspect: !1 };
724
+ const e = (t.scaleX + t.scaleY) / 2;
725
+ return { scaleX: e, scaleY: e, lockAspect: !0 };
726
+ }
727
+ function Re(t) {
728
+ const n = Math.max(t.scaleX, t.scaleY);
729
+ return Math.round(n * 1e3) / 10;
730
+ }
731
+ function v(t) {
732
+ return Number.isFinite(t) ? Math.max(K, Math.min(ft, Math.trunc(t))) : K;
733
+ }
734
+ function Qt(t) {
735
+ return !Number.isFinite(t) || t <= 0 ? 0.01 : Math.max(0.01, Math.min(t, ft));
736
+ }
737
+ async function Ie(t, n) {
738
+ if (jt(t)) return n;
739
+ const { width: e, height: i } = $t(t, n);
740
+ if (e <= 0 || i <= 0) return n;
741
+ const r = Kt(n.width, n.height, e, i);
742
+ let o = {
743
+ bitmap: n.bitmap,
744
+ width: n.width,
745
+ height: n.height
746
+ };
747
+ for (let c = 0; c < r; c++) {
748
+ const l = Math.max(e, Math.floor(o.width / 2)), s = Math.max(i, Math.floor(o.height / 2));
749
+ o = { bitmap: V(o, l, s).canvas, width: l, height: s };
750
+ }
751
+ return {
752
+ bitmap: V(o, e, i).canvas,
753
+ width: e,
754
+ height: i,
755
+ mimeType: n.mimeType
756
+ };
757
+ }
758
+ function Kt(t, n, e, i) {
759
+ let r = t, o = n, a = 0;
760
+ for (; (r / 2 > e || o / 2 > i) && a < 16; )
761
+ r = Math.floor(r / 2), o = Math.floor(o / 2), a += 1;
762
+ return a;
763
+ }
764
+ function V(t, n, e) {
765
+ const i = g(n, e), r = y(i);
766
+ return r.imageSmoothingEnabled = !0, r.imageSmoothingQuality = "high", r.drawImage(t.bitmap, 0, 0, t.width, t.height, 0, 0, n, e), i;
767
+ }
768
+ const G = -100, J = 100, Z = 1, _ = {
769
+ brightness: 0,
770
+ contrast: 0,
771
+ saturation: 0,
772
+ exposure: 0,
773
+ clarity: 0,
774
+ gamma: 0
775
+ }, Te = [
776
+ { key: "brightness", label: "Brightness" },
777
+ { key: "contrast", label: "Contrast" },
778
+ { key: "saturation", label: "Saturation" },
779
+ { key: "exposure", label: "Exposure" },
780
+ { key: "clarity", label: "Clarity" },
781
+ { key: "gamma", label: "Gamma" }
782
+ ];
783
+ function ke() {
784
+ return _;
785
+ }
786
+ function Vt(t) {
787
+ return t.brightness === 0 && t.contrast === 0 && t.saturation === 0 && t.exposure === 0 && t.clarity === 0 && t.gamma === 0;
788
+ }
789
+ function Ce(t, n, e) {
790
+ const i = Gt(e);
791
+ return t[n] === i ? t : { ...t, [n]: i };
792
+ }
793
+ function ve(t, n) {
794
+ return t[n] === 0 ? t : { ...t, [n]: 0 };
795
+ }
796
+ function Ae() {
797
+ return _;
798
+ }
799
+ function Gt(t) {
800
+ return Number.isNaN(t) ? 0 : t <= G ? G : t >= J ? J : Math.round(t / Z) * Z;
801
+ }
802
+ function Jt(t) {
803
+ const n = new Uint8ClampedArray(256), e = t.brightness / 200, i = 1 + t.contrast / 100, r = 1 + t.exposure / 200, o = Zt(t.gamma);
804
+ for (let a = 0; a < 256; a++) {
805
+ let c = a / 255;
806
+ c = c * r, c = (c - 0.5) * i + 0.5, c = c + e, c < 0 ? c = 0 : c > 1 && (c = 1), c = c ** o, c < 0 ? c = 0 : c > 1 && (c = 1), n[a] = Math.round(c * 255);
807
+ }
808
+ return n;
809
+ }
810
+ function Zt(t) {
811
+ return t === 0 ? 1 : t > 0 ? 1 - 0.5 * (t / 100) : 1 + 1 * (-t / 100);
812
+ }
813
+ function tn(t, n, e, i) {
814
+ const r = t.length;
815
+ if (n.length !== r)
816
+ throw new Error("applyFinetuneLutAndSaturation: src/dst length mismatch");
817
+ const o = 1 + i.saturation / 100;
818
+ if (o === 1) {
819
+ for (let a = 0; a < r; a += 4)
820
+ n[a] = e[t[a]], n[a + 1] = e[t[a + 1]], n[a + 2] = e[t[a + 2]], n[a + 3] = t[a + 3];
821
+ return;
822
+ }
823
+ for (let a = 0; a < r; a += 4) {
824
+ const c = e[t[a]], l = e[t[a + 1]], s = e[t[a + 2]], u = 0.2126 * c + 0.7152 * l + 0.0722 * s;
825
+ let h = u + (c - u) * o, f = u + (l - u) * o, d = u + (s - u) * o;
826
+ h < 0 ? h = 0 : h > 255 && (h = 255), f < 0 ? f = 0 : f > 255 && (f = 255), d < 0 ? d = 0 : d > 255 && (d = 255), n[a] = h, n[a + 1] = f, n[a + 2] = d, n[a + 3] = t[a + 3];
827
+ }
828
+ }
829
+ function nn(t, n, e) {
830
+ if (e === 0) return;
831
+ const i = t.length;
832
+ if (n.length !== i)
833
+ throw new Error("applyClarity: dst/blurred length mismatch");
834
+ const r = e / 100;
835
+ for (let o = 0; o < i; o += 4) {
836
+ const a = t[o], c = t[o + 1], l = t[o + 2];
837
+ let s = a + r * (a - n[o]), u = c + r * (c - n[o + 1]), h = l + r * (l - n[o + 2]);
838
+ s < 0 ? s = 0 : s > 255 && (s = 255), u < 0 ? u = 0 : u > 255 && (u = 255), h < 0 ? h = 0 : h > 255 && (h = 255), t[o] = s, t[o + 1] = u, t[o + 2] = h;
839
+ }
840
+ }
841
+ function en(t, n, e, i, r) {
842
+ if (t.length !== n.length || t.length !== e.length)
843
+ throw new Error("boxBlur3x3: buffer length mismatch");
844
+ for (let o = 0; o < r; o++)
845
+ for (let a = 0; a < i; a++) {
846
+ const c = a === 0 ? 0 : a - 1, l = a === i - 1 ? i - 1 : a + 1, s = (o * i + a) * 4, u = (o * i + c) * 4, h = (o * i + l) * 4;
847
+ n[s] = (t[u] + t[s] + t[h]) / 3, n[s + 1] = (t[u + 1] + t[s + 1] + t[h + 1]) / 3, n[s + 2] = (t[u + 2] + t[s + 2] + t[h + 2]) / 3, n[s + 3] = t[s + 3];
848
+ }
849
+ for (let o = 0; o < r; o++) {
850
+ const a = o === 0 ? 0 : o - 1, c = o === r - 1 ? r - 1 : o + 1;
851
+ for (let l = 0; l < i; l++) {
852
+ const s = (o * i + l) * 4, u = (a * i + l) * 4, h = (c * i + l) * 4;
853
+ e[s] = (n[u] + n[s] + n[h]) / 3, e[s + 1] = (n[u + 1] + n[s + 1] + n[h + 1]) / 3, e[s + 2] = (n[u + 2] + n[s + 2] + n[h + 2]) / 3, e[s + 3] = n[s + 3];
854
+ }
855
+ }
856
+ }
857
+ function tt(t, n, e) {
858
+ if (n.width !== e.width || n.height !== e.height || n.data.length !== e.data.length)
859
+ throw new Error("applyFinetuneToImageData: baseline/dst dimensions mismatch");
860
+ const i = Jt(t);
861
+ if (tn(n.data, e.data, i, t), t.clarity !== 0) {
862
+ const r = new Uint8ClampedArray(n.data.length), o = new Uint8ClampedArray(n.data.length);
863
+ en(n.data, r, o, n.width, n.height), nn(e.data, o, t.clarity);
864
+ }
865
+ }
866
+ async function Ne(t, n) {
867
+ if (Vt(t)) return n;
868
+ const e = g(n.width, n.height), i = y(e);
869
+ i.imageSmoothingEnabled = !0, i.imageSmoothingQuality = "high", i.drawImage(n.bitmap, 0, 0, n.width, n.height);
870
+ const r = i.getImageData(0, 0, n.width, n.height);
871
+ if (t.clarity === 0)
872
+ tt(t, r, r), i.putImageData(r, 0, 0);
873
+ else {
874
+ const o = new ImageData(
875
+ new Uint8ClampedArray(r.data.length),
876
+ r.width,
877
+ r.height
878
+ );
879
+ tt(t, r, o), i.putImageData(o, 0, 0);
880
+ }
881
+ return {
882
+ bitmap: e.canvas,
883
+ width: n.width,
884
+ height: n.height,
885
+ mimeType: n.mimeType
886
+ };
887
+ }
888
+ const rn = [
889
+ {
890
+ id: "none",
891
+ label: "None",
892
+ state: _
893
+ },
894
+ {
895
+ id: "vivid",
896
+ label: "Vivid",
897
+ state: {
898
+ brightness: 0,
899
+ contrast: 10,
900
+ saturation: 40,
901
+ exposure: 0,
902
+ clarity: 5,
903
+ gamma: 0
904
+ }
905
+ },
906
+ {
907
+ id: "mono",
908
+ label: "Mono",
909
+ // -100 saturation is bit-exact grayscale via Rec. 709 luminance.
910
+ state: {
911
+ brightness: 0,
912
+ contrast: 15,
913
+ saturation: -100,
914
+ exposure: 0,
915
+ clarity: 0,
916
+ gamma: 0
917
+ }
918
+ },
919
+ {
920
+ id: "soft",
921
+ label: "Soft",
922
+ state: {
923
+ brightness: 5,
924
+ contrast: -10,
925
+ saturation: 0,
926
+ exposure: 0,
927
+ clarity: -25,
928
+ gamma: 0
929
+ }
930
+ },
931
+ {
932
+ id: "punch",
933
+ label: "Punch",
934
+ state: {
935
+ brightness: 0,
936
+ contrast: 30,
937
+ saturation: 5,
938
+ exposure: 0,
939
+ clarity: 25,
940
+ gamma: 0
941
+ }
942
+ },
943
+ {
944
+ id: "mute",
945
+ label: "Mute",
946
+ state: {
947
+ brightness: 0,
948
+ contrast: 5,
949
+ saturation: -50,
950
+ exposure: 0,
951
+ clarity: -5,
952
+ gamma: 0
953
+ }
954
+ },
955
+ {
956
+ id: "bright",
957
+ label: "Bright",
958
+ state: {
959
+ brightness: 5,
960
+ contrast: 5,
961
+ saturation: 0,
962
+ exposure: 15,
963
+ clarity: 0,
964
+ gamma: 0
965
+ }
966
+ }
967
+ ];
968
+ function on(t, n) {
969
+ return t.brightness === n.brightness && t.contrast === n.contrast && t.saturation === n.saturation && t.exposure === n.exposure && t.clarity === n.clarity && t.gamma === n.gamma;
970
+ }
971
+ function Fe(t) {
972
+ for (const n of rn)
973
+ if (on(n.state, t)) return n;
974
+ }
975
+ const Pe = "rgba(255, 235, 59, 0.35)", Oe = 18, _e = 6, an = 32, cn = "#ff3b30", sn = 4;
976
+ function ln() {
977
+ return {
978
+ color: cn,
979
+ strokeWidth: sn,
980
+ fillColor: null,
981
+ fontSize: an
982
+ };
983
+ }
984
+ function ze(t) {
985
+ return {
986
+ shapes: [],
987
+ selectedId: null,
988
+ activeTool: "select",
989
+ currentStyle: ln(),
990
+ imageSize: t.imageSize,
991
+ nextShapeNumber: 1
992
+ };
993
+ }
994
+ function De(t) {
995
+ return {
996
+ id: `s_${t.nextShapeNumber.toString(36)}`,
997
+ nextShapeNumber: t.nextShapeNumber + 1
998
+ };
999
+ }
1000
+ function Le(t, n) {
1001
+ return t.activeTool === n ? t : { ...t, activeTool: n, selectedId: n === "select" ? t.selectedId : null };
1002
+ }
1003
+ function Be(t, n) {
1004
+ return { ...t, currentStyle: { ...t.currentStyle, ...n } };
1005
+ }
1006
+ function We(t, n) {
1007
+ return t.selectedId === n ? t : { ...t, selectedId: n };
1008
+ }
1009
+ function Ye(t, n) {
1010
+ return { ...t, shapes: [...t.shapes, n], selectedId: n.id };
1011
+ }
1012
+ function Xe(t, n) {
1013
+ let e = !1;
1014
+ const i = t.shapes.map((r) => r.id !== n.id ? r : (e = !0, n));
1015
+ return e ? { ...t, shapes: i } : t;
1016
+ }
1017
+ function Ue(t, n) {
1018
+ const e = t.shapes.filter((i) => i.id !== n);
1019
+ return e.length === t.shapes.length ? t : {
1020
+ ...t,
1021
+ shapes: e,
1022
+ selectedId: t.selectedId === n ? null : t.selectedId
1023
+ };
1024
+ }
1025
+ function He(t, n) {
1026
+ if (n !== null)
1027
+ return t.shapes.find((e) => e.id === n);
1028
+ }
1029
+ function qe(t) {
1030
+ let { x: n, y: e, width: i, height: r } = t;
1031
+ return i < 0 && (n += i, i = -i), r < 0 && (e += r, r = -r), { x: n, y: e, width: i, height: r };
1032
+ }
1033
+ function je(t, n, e) {
1034
+ switch (t.kind) {
1035
+ case "text":
1036
+ return { ...t, x: t.x + n, y: t.y + e };
1037
+ case "rect":
1038
+ case "ellipse":
1039
+ return { ...t, x: t.x + n, y: t.y + e };
1040
+ case "arrow":
1041
+ return {
1042
+ ...t,
1043
+ x1: t.x1 + n,
1044
+ y1: t.y1 + e,
1045
+ x2: t.x2 + n,
1046
+ y2: t.y2 + e
1047
+ };
1048
+ case "freehand":
1049
+ case "highlight":
1050
+ return { ...t, points: t.points.map((i) => ({ x: i.x + n, y: i.y + e })) };
1051
+ default:
1052
+ return w(t);
1053
+ }
1054
+ }
1055
+ function w(t) {
1056
+ throw new Error(`Unhandled annotation shape kind: ${JSON.stringify(t)}`);
1057
+ }
1058
+ function $e(t, n, e) {
1059
+ if (n === "horizontal")
1060
+ switch (t.kind) {
1061
+ case "rect":
1062
+ case "ellipse":
1063
+ return { ...t, x: e.width - t.x - t.width };
1064
+ case "text":
1065
+ return { ...t, x: e.width - t.x };
1066
+ case "arrow":
1067
+ return { ...t, x1: e.width - t.x1, x2: e.width - t.x2 };
1068
+ case "freehand":
1069
+ case "highlight":
1070
+ return {
1071
+ ...t,
1072
+ points: t.points.map((i) => ({ x: e.width - i.x, y: i.y }))
1073
+ };
1074
+ default:
1075
+ return w(t);
1076
+ }
1077
+ switch (t.kind) {
1078
+ case "rect":
1079
+ case "ellipse":
1080
+ return { ...t, y: e.height - t.y - t.height };
1081
+ case "text":
1082
+ return { ...t, y: e.height - t.y };
1083
+ case "arrow":
1084
+ return { ...t, y1: e.height - t.y1, y2: e.height - t.y2 };
1085
+ case "freehand":
1086
+ case "highlight":
1087
+ return {
1088
+ ...t,
1089
+ points: t.points.map((i) => ({ x: i.x, y: e.height - i.y }))
1090
+ };
1091
+ default:
1092
+ return w(t);
1093
+ }
1094
+ }
1095
+ function Qe(t, n, e) {
1096
+ if (n === 0) return t;
1097
+ const i = (r, o) => n === 1 ? { x: e.height - o, y: r } : n === 2 ? { x: e.width - r, y: e.height - o } : { x: o, y: e.width - r };
1098
+ switch (t.kind) {
1099
+ case "rect":
1100
+ case "ellipse": {
1101
+ const r = [
1102
+ i(t.x, t.y),
1103
+ i(t.x + t.width, t.y + t.height)
1104
+ ], o = Math.min(r[0].x, r[1].x), a = Math.min(r[0].y, r[1].y), c = Math.abs(r[1].x - r[0].x), l = Math.abs(r[1].y - r[0].y);
1105
+ return { ...t, x: o, y: a, width: c, height: l };
1106
+ }
1107
+ case "text": {
1108
+ const r = i(t.x, t.y);
1109
+ return { ...t, x: r.x, y: r.y };
1110
+ }
1111
+ case "arrow": {
1112
+ const r = i(t.x1, t.y1), o = i(t.x2, t.y2);
1113
+ return { ...t, x1: r.x, y1: r.y, x2: o.x, y2: o.y };
1114
+ }
1115
+ case "freehand":
1116
+ case "highlight":
1117
+ return { ...t, points: t.points.map((r) => i(r.x, r.y)) };
1118
+ default:
1119
+ return w(t);
1120
+ }
1121
+ }
1122
+ function Ke(t, n) {
1123
+ return t.shapes.length === 0 ? t : { ...t, shapes: t.shapes.map(n) };
1124
+ }
1125
+ const Ve = [
1126
+ "text",
1127
+ "rect",
1128
+ "ellipse",
1129
+ "arrow"
1130
+ ];
1131
+ function Ge(t) {
1132
+ return t === "text" || t === "rect" || t === "ellipse" || t === "arrow";
1133
+ }
1134
+ function Je(t, n) {
1135
+ const { imageSize: e, style: i, id: r } = n, o = Math.min(e.width, e.height), a = e.width / 2, c = e.height / 2;
1136
+ switch (t) {
1137
+ case "rect":
1138
+ case "ellipse": {
1139
+ const l = Math.max(80, Math.round(o * 0.25)), s = Math.round(a - l / 2), u = Math.round(c - l / 2);
1140
+ return t === "rect" ? {
1141
+ id: r,
1142
+ kind: "rect",
1143
+ x: s,
1144
+ y: u,
1145
+ width: l,
1146
+ height: l,
1147
+ strokeColor: i.color,
1148
+ strokeWidth: i.strokeWidth,
1149
+ fillColor: i.fillColor
1150
+ } : {
1151
+ id: r,
1152
+ kind: "ellipse",
1153
+ x: s,
1154
+ y: u,
1155
+ width: l,
1156
+ height: l,
1157
+ strokeColor: i.color,
1158
+ strokeWidth: i.strokeWidth,
1159
+ fillColor: i.fillColor
1160
+ };
1161
+ }
1162
+ case "arrow": {
1163
+ const l = Math.max(100, Math.round(o * 0.3)), s = Math.round(a - l / 2), u = s + l, h = Math.round(c);
1164
+ return {
1165
+ id: r,
1166
+ kind: "arrow",
1167
+ x1: s,
1168
+ y1: h,
1169
+ x2: u,
1170
+ y2: h,
1171
+ color: i.color,
1172
+ strokeWidth: i.strokeWidth
1173
+ };
1174
+ }
1175
+ case "text": {
1176
+ const l = Math.round(a), s = Math.round(c - i.fontSize / 2);
1177
+ return {
1178
+ id: r,
1179
+ kind: "text",
1180
+ x: l,
1181
+ y: s,
1182
+ text: "",
1183
+ fontSize: i.fontSize,
1184
+ color: i.color,
1185
+ textAlign: "center"
1186
+ };
1187
+ }
1188
+ }
1189
+ }
1190
+ function nt(t) {
1191
+ switch (t.kind) {
1192
+ case "text": {
1193
+ const { width: n, height: e } = un(t.text, t.fontSize);
1194
+ return { x: hn(t.x, n, t.textAlign), y: t.y, width: n, height: e };
1195
+ }
1196
+ case "rect":
1197
+ case "ellipse":
1198
+ return { x: t.x, y: t.y, width: t.width, height: t.height };
1199
+ case "arrow": {
1200
+ const n = Math.min(t.x1, t.x2), e = Math.min(t.y1, t.y2);
1201
+ return {
1202
+ x: n,
1203
+ y: e,
1204
+ width: Math.abs(t.x2 - t.x1),
1205
+ height: Math.abs(t.y2 - t.y1)
1206
+ };
1207
+ }
1208
+ case "freehand":
1209
+ case "highlight": {
1210
+ const n = t.points[0];
1211
+ if (!n) return { x: 0, y: 0, width: 0, height: 0 };
1212
+ let e = n.x, i = n.y, r = n.x, o = n.y;
1213
+ for (const a of t.points)
1214
+ a.x < e && (e = a.x), a.x > r && (r = a.x), a.y < i && (i = a.y), a.y > o && (o = a.y);
1215
+ return { x: e, y: i, width: r - e, height: o - i };
1216
+ }
1217
+ default:
1218
+ return w(t);
1219
+ }
1220
+ }
1221
+ const Ze = [
1222
+ "tl",
1223
+ "tr",
1224
+ "bl",
1225
+ "br",
1226
+ "t",
1227
+ "r",
1228
+ "b",
1229
+ "l"
1230
+ ];
1231
+ function ti(t) {
1232
+ const n = t.x, e = t.x + t.width, i = t.y, r = t.y + t.height, o = t.x + t.width / 2, a = t.y + t.height / 2;
1233
+ return {
1234
+ tl: { x: n, y: i },
1235
+ tr: { x: e, y: i },
1236
+ bl: { x: n, y: r },
1237
+ br: { x: e, y: r },
1238
+ t: { x: o, y: i },
1239
+ r: { x: e, y: a },
1240
+ b: { x: o, y: r },
1241
+ l: { x: n, y: a }
1242
+ };
1243
+ }
1244
+ function ni(t, n, e) {
1245
+ let i = t.x, r = t.y, o = t.x + t.width, a = t.y + t.height;
1246
+ return (n === "tl" || n === "l" || n === "bl") && (i = e.x), (n === "tr" || n === "r" || n === "br") && (o = e.x), (n === "tl" || n === "t" || n === "tr") && (r = e.y), (n === "bl" || n === "b" || n === "br") && (a = e.y), { x: i, y: r, width: o - i, height: a - r };
1247
+ }
1248
+ function un(t, n) {
1249
+ const e = t.length === 0 ? [""] : t.split(`
1250
+ `);
1251
+ let i = 0;
1252
+ for (const a of e)
1253
+ a.length > i && (i = a.length);
1254
+ const r = Math.max(n * 0.6, i * n * 0.55), o = e.length * n * 1.2;
1255
+ return { width: r, height: o };
1256
+ }
1257
+ function hn(t, n, e) {
1258
+ switch (e) {
1259
+ case "left":
1260
+ return t;
1261
+ case "center":
1262
+ return t - n / 2;
1263
+ case "right":
1264
+ return t - n;
1265
+ }
1266
+ }
1267
+ const x = 4;
1268
+ function ei(t, n) {
1269
+ for (let e = t.length - 1; e >= 0; e--) {
1270
+ const i = t[e];
1271
+ if (i && fn(i, n)) return i;
1272
+ }
1273
+ }
1274
+ function fn(t, n) {
1275
+ switch (t.kind) {
1276
+ case "text":
1277
+ return S(n, nt(t));
1278
+ case "rect": {
1279
+ const e = S(n, I(t));
1280
+ if (t.fillColor !== null) return e;
1281
+ const i = N(I(t), t.strokeWidth / 2 + x), r = N(I(t), -(t.strokeWidth / 2 + x));
1282
+ return S(n, i) && !S(n, r);
1283
+ }
1284
+ case "ellipse": {
1285
+ const e = I(t), i = e.width / 2, r = e.height / 2, o = e.x + i, a = e.y + r;
1286
+ if (i <= 0 || r <= 0) return !1;
1287
+ const c = (n.x - o) / i, l = (n.y - a) / r, s = c * c + l * l, u = (t.strokeWidth / 2 + x) / Math.min(i, r);
1288
+ return t.fillColor !== null ? s <= (1 + u) ** 2 : s <= (1 + u) ** 2 && s >= (1 - u) ** 2;
1289
+ }
1290
+ case "arrow":
1291
+ return et(
1292
+ n,
1293
+ { x: t.x1, y: t.y1 },
1294
+ { x: t.x2, y: t.y2 },
1295
+ t.strokeWidth / 2 + x
1296
+ );
1297
+ case "freehand":
1298
+ case "highlight": {
1299
+ const e = nt(t), i = N(e, t.strokeWidth / 2 + x);
1300
+ if (!S(n, i)) return !1;
1301
+ const r = t.strokeWidth / 2 + x;
1302
+ for (let o = 1; o < t.points.length; o++) {
1303
+ const a = t.points[o - 1], c = t.points[o];
1304
+ if (a && c && et(n, a, c, r)) return !0;
1305
+ }
1306
+ if (t.points.length === 1) {
1307
+ const o = t.points[0];
1308
+ if (!o) return !1;
1309
+ const a = o.x - n.x, c = o.y - n.y;
1310
+ return a * a + c * c <= r * r;
1311
+ }
1312
+ return !1;
1313
+ }
1314
+ default:
1315
+ return w(t);
1316
+ }
1317
+ }
1318
+ function S(t, n) {
1319
+ return t.x >= n.x && t.y >= n.y && t.x <= n.x + n.width && t.y <= n.y + n.height;
1320
+ }
1321
+ function N(t, n) {
1322
+ return {
1323
+ x: t.x - n,
1324
+ y: t.y - n,
1325
+ width: t.width + n * 2,
1326
+ height: t.height + n * 2
1327
+ };
1328
+ }
1329
+ function I(t) {
1330
+ let { x: n, y: e, width: i, height: r } = t;
1331
+ return i < 0 && (n += i, i = -i), r < 0 && (e += r, r = -r), { x: n, y: e, width: i, height: r };
1332
+ }
1333
+ function et(t, n, e, i) {
1334
+ const r = e.x - n.x, o = e.y - n.y, a = r * r + o * o;
1335
+ if (a === 0) {
1336
+ const f = t.x - n.x, d = t.y - n.y;
1337
+ return f * f + d * d <= i * i;
1338
+ }
1339
+ let c = ((t.x - n.x) * r + (t.y - n.y) * o) / a;
1340
+ c < 0 ? c = 0 : c > 1 && (c = 1);
1341
+ const l = n.x + c * r, s = n.y + c * o, u = t.x - l, h = t.y - s;
1342
+ return u * u + h * h <= i * i;
1343
+ }
1344
+ const it = 2;
1345
+ function ii(t) {
1346
+ if (t.length <= 1) return [...t];
1347
+ const n = t[0];
1348
+ if (!n) return [];
1349
+ const e = [n];
1350
+ let i = n;
1351
+ const r = it * it;
1352
+ for (let a = 1; a < t.length - 1; a++) {
1353
+ const c = t[a];
1354
+ if (!c) continue;
1355
+ const l = c.x - i.x, s = c.y - i.y;
1356
+ l * l + s * s < r || (e.push(c), i = c);
1357
+ }
1358
+ const o = t[t.length - 1];
1359
+ return o && o !== i && e.push(o), e;
1360
+ }
1361
+ function dt(t, n) {
1362
+ if (n.length === 0) return;
1363
+ const e = n[0];
1364
+ if (!e) return;
1365
+ if (n.length === 1) {
1366
+ t.moveTo(e.x, e.y), t.lineTo(e.x, e.y);
1367
+ return;
1368
+ }
1369
+ t.moveTo(e.x, e.y);
1370
+ for (let r = 1; r < n.length - 1; r++) {
1371
+ const o = n[r], a = n[r + 1];
1372
+ if (!o || !a) continue;
1373
+ const c = (o.x + a.x) / 2, l = (o.y + a.y) / 2;
1374
+ t.quadraticCurveTo(o.x, o.y, c, l);
1375
+ }
1376
+ const i = n[n.length - 1];
1377
+ i && t.lineTo(i.x, i.y);
1378
+ }
1379
+ const dn = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif';
1380
+ async function ri(t, n) {
1381
+ if (t.shapes.length === 0) return n;
1382
+ const e = g(n.width, n.height), i = y(e);
1383
+ i.imageSmoothingEnabled = !0, i.imageSmoothingQuality = "high", i.drawImage(n.bitmap, 0, 0, n.width, n.height);
1384
+ for (const r of t.shapes)
1385
+ gn(i, r);
1386
+ return {
1387
+ bitmap: e.canvas,
1388
+ width: n.width,
1389
+ height: n.height,
1390
+ mimeType: n.mimeType
1391
+ };
1392
+ }
1393
+ function gn(t, n) {
1394
+ switch (n.kind) {
1395
+ case "text":
1396
+ mn(t, n);
1397
+ return;
1398
+ case "rect":
1399
+ yn(t, n);
1400
+ return;
1401
+ case "ellipse":
1402
+ wn(t, n);
1403
+ return;
1404
+ case "arrow":
1405
+ xn(t, n);
1406
+ return;
1407
+ case "freehand":
1408
+ pn(t, n);
1409
+ return;
1410
+ case "highlight":
1411
+ Mn(t, n);
1412
+ return;
1413
+ default:
1414
+ w(n);
1415
+ }
1416
+ }
1417
+ function mn(t, n) {
1418
+ t.save(), t.fillStyle = n.color, t.font = `${n.fontSize}px ${dn}`, t.textAlign = n.textAlign, t.textBaseline = "top";
1419
+ const e = n.text.length === 0 ? [""] : n.text.split(`
1420
+ `), i = n.fontSize * 1.2;
1421
+ for (let r = 0; r < e.length; r++) {
1422
+ const o = e[r];
1423
+ o !== void 0 && t.fillText(o, n.x, n.y + r * i);
1424
+ }
1425
+ t.restore();
1426
+ }
1427
+ function yn(t, n) {
1428
+ t.save(), n.fillColor !== null && (t.fillStyle = n.fillColor, t.fillRect(n.x, n.y, n.width, n.height)), n.strokeWidth > 0 && (t.strokeStyle = n.strokeColor, t.lineWidth = n.strokeWidth, t.lineJoin = "miter", t.strokeRect(n.x, n.y, n.width, n.height)), t.restore();
1429
+ }
1430
+ function wn(t, n) {
1431
+ t.save();
1432
+ const e = n.width / 2, i = n.height / 2, r = n.x + e, o = n.y + i;
1433
+ t.beginPath(), t.ellipse(r, o, Math.max(0, e), Math.max(0, i), 0, 0, Math.PI * 2), n.fillColor !== null && (t.fillStyle = n.fillColor, t.fill()), n.strokeWidth > 0 && (t.strokeStyle = n.strokeColor, t.lineWidth = n.strokeWidth, t.stroke()), t.restore();
1434
+ }
1435
+ function xn(t, n) {
1436
+ const e = n.x2 - n.x1, i = n.y2 - n.y1, r = Math.sqrt(e * e + i * i);
1437
+ if (r < 0.5) return;
1438
+ t.save(), t.strokeStyle = n.color, t.fillStyle = n.color, t.lineWidth = n.strokeWidth, t.lineCap = "round", t.lineJoin = "round";
1439
+ const o = Math.min(Math.max(n.strokeWidth * 5, 28), r * 0.6), a = Math.max(n.strokeWidth * 4, 18), c = e / r, l = i / r, s = n.x2 - c * o * 0.6, u = n.y2 - l * o * 0.6;
1440
+ t.beginPath(), t.moveTo(n.x1, n.y1), t.lineTo(s, u), t.stroke();
1441
+ const h = n.x2 - c * o, f = n.y2 - l * o, d = -l, m = c;
1442
+ t.beginPath(), t.moveTo(n.x2, n.y2), t.lineTo(h + d * a / 2, f + m * a / 2), t.lineTo(h - d * a / 2, f - m * a / 2), t.closePath(), t.fill(), t.restore();
1443
+ }
1444
+ function pn(t, n) {
1445
+ n.points.length !== 0 && (t.save(), t.strokeStyle = n.color, t.lineWidth = n.strokeWidth, t.lineCap = "round", t.lineJoin = "round", t.beginPath(), dt(t, n.points), t.stroke(), t.restore());
1446
+ }
1447
+ function Mn(t, n) {
1448
+ n.points.length !== 0 && (t.save(), t.globalCompositeOperation = "multiply", t.strokeStyle = n.color, t.lineWidth = n.strokeWidth, t.lineCap = "round", t.lineJoin = "round", t.beginPath(), dt(t, n.points), t.stroke(), t.restore());
1449
+ }
1450
+ const oi = ["pixelate", "blur", "solid"], Sn = "#000000", bn = "pixelate";
1451
+ function ai(t) {
1452
+ return {
1453
+ regions: [],
1454
+ nextRegionNumber: 1,
1455
+ selectedId: null,
1456
+ currentMode: bn,
1457
+ currentColor: Sn,
1458
+ imageSize: t.imageSize
1459
+ };
1460
+ }
1461
+ function ci(t) {
1462
+ return {
1463
+ id: `r_${t.nextRegionNumber.toString(36)}`,
1464
+ nextRegionNumber: t.nextRegionNumber + 1
1465
+ };
1466
+ }
1467
+ function si(t, n) {
1468
+ return {
1469
+ ...t,
1470
+ regions: [...t.regions, n],
1471
+ selectedId: n.id
1472
+ };
1473
+ }
1474
+ function gt(t, n) {
1475
+ let e = !1;
1476
+ const i = t.regions.map((r) => r.id !== n.id ? r : (e = !0, n));
1477
+ return e ? { ...t, regions: i } : t;
1478
+ }
1479
+ function li(t, n) {
1480
+ const e = t.regions.filter((i) => i.id !== n);
1481
+ return e.length === t.regions.length ? t : {
1482
+ ...t,
1483
+ regions: e,
1484
+ selectedId: t.selectedId === n ? null : t.selectedId
1485
+ };
1486
+ }
1487
+ function ui(t, n, e) {
1488
+ if (t.regions.length === 0) return t;
1489
+ const i = t.regions.map((r) => n === "horizontal" ? { ...r, x: e.width - r.x - r.width } : { ...r, y: e.height - r.y - r.height });
1490
+ return { ...t, regions: i, imageSize: e };
1491
+ }
1492
+ function hi(t, n, e, i) {
1493
+ if (t.regions.length === 0) return { ...t, imageSize: i };
1494
+ if (n === 0 && e === 0 && t.imageSize === i) return t;
1495
+ const r = t.regions.map((o) => ({
1496
+ ...o,
1497
+ x: o.x + n,
1498
+ y: o.y + e
1499
+ }));
1500
+ return { ...t, regions: r, imageSize: i };
1501
+ }
1502
+ function fi(t, n, e) {
1503
+ if (n === 0) return { ...t, imageSize: e };
1504
+ if (t.regions.length === 0) return { ...t, imageSize: e };
1505
+ const i = t.imageSize.width, r = t.imageSize.height, o = t.regions.map((a) => {
1506
+ const { x: c, y: l, width: s, height: u } = a;
1507
+ return n === 1 ? { ...a, x: r - l - u, y: c, width: u, height: s } : n === 2 ? {
1508
+ ...a,
1509
+ x: i - c - s,
1510
+ y: r - l - u
1511
+ } : { ...a, x: l, y: i - c - s, width: u, height: s };
1512
+ });
1513
+ return { ...t, regions: o, imageSize: e };
1514
+ }
1515
+ function di(t, n) {
1516
+ return t.selectedId === n ? t : { ...t, selectedId: n };
1517
+ }
1518
+ function gi(t, n) {
1519
+ return t.currentMode === n ? t : { ...t, currentMode: n };
1520
+ }
1521
+ function mi(t, n) {
1522
+ return t.currentColor === n ? t : { ...t, currentColor: n };
1523
+ }
1524
+ function yi(t, n, e) {
1525
+ const i = t.regions.find((r) => r.id === n);
1526
+ return !i || i.mode === e ? t : gt(t, { ...i, mode: e });
1527
+ }
1528
+ function wi(t, n, e) {
1529
+ const i = t.regions.find((r) => r.id === n);
1530
+ return !i || i.color === e ? t : gt(t, { ...i, color: e });
1531
+ }
1532
+ function xi(t, n) {
1533
+ if (n !== null)
1534
+ return t.regions.find((e) => e.id === n);
1535
+ }
1536
+ function pi(t) {
1537
+ return t.selectedId === null ? null : t.regions.find((n) => n.id === t.selectedId) ?? null;
1538
+ }
1539
+ function Mi(t) {
1540
+ let { x: n, y: e, width: i, height: r } = t;
1541
+ return i < 0 && (n += i, i = -i), r < 0 && (e += r, r = -r), { x: n, y: e, width: i, height: r };
1542
+ }
1543
+ function Si(t) {
1544
+ const { imageSize: n, mode: e, color: i, id: r } = t, o = Math.min(n.width, n.height), a = Math.max(80, Math.round(o * 0.25)), c = n.width / 2, l = n.height / 2, s = Math.round(c - a / 2), u = Math.round(l - a / 2);
1545
+ return {
1546
+ id: r,
1547
+ x: s,
1548
+ y: u,
1549
+ width: a,
1550
+ height: a,
1551
+ mode: e,
1552
+ color: i
1553
+ };
1554
+ }
1555
+ function bi(t, n) {
1556
+ if (t.imageSize.width === n.width && t.imageSize.height === n.height)
1557
+ return t;
1558
+ const e = [];
1559
+ for (const r of t.regions)
1560
+ r.x + r.width <= 0 || r.y + r.height <= 0 || r.x >= n.width || r.y >= n.height || e.push(En(r, n));
1561
+ const i = t.selectedId !== null && !e.some((r) => r.id === t.selectedId);
1562
+ return {
1563
+ ...t,
1564
+ regions: e,
1565
+ imageSize: { width: n.width, height: n.height },
1566
+ selectedId: i ? null : t.selectedId
1567
+ };
1568
+ }
1569
+ function En(t, n) {
1570
+ const e = Math.max(0, t.x), i = Math.max(0, t.y), r = Math.min(n.width, t.x + t.width), o = Math.min(n.height, t.y + t.height);
1571
+ return {
1572
+ ...t,
1573
+ x: e,
1574
+ y: i,
1575
+ width: Math.max(0, r - e),
1576
+ height: Math.max(0, o - i)
1577
+ };
1578
+ }
1579
+ function Ei(t) {
1580
+ return { x: t.x, y: t.y, width: t.width, height: t.height };
1581
+ }
1582
+ async function Ri(t, n) {
1583
+ if (t.regions.length === 0) return n;
1584
+ const e = g(n.width, n.height), i = y(e);
1585
+ i.drawImage(n.bitmap, 0, 0, n.width, n.height);
1586
+ for (const r of t.regions)
1587
+ Rn(i, e.canvas, r, n);
1588
+ return {
1589
+ bitmap: e.canvas,
1590
+ width: n.width,
1591
+ height: n.height,
1592
+ mimeType: n.mimeType
1593
+ };
1594
+ }
1595
+ function Rn(t, n, e, i) {
1596
+ const r = Math.round(e.width), o = Math.round(e.height);
1597
+ if (r < 1 || o < 1) return;
1598
+ const a = Math.round(e.x), c = Math.round(e.y);
1599
+ switch (e.mode) {
1600
+ case "solid":
1601
+ In(t, e, a, c, r, o);
1602
+ return;
1603
+ case "pixelate":
1604
+ Tn(t, n, i, a, c, r, o);
1605
+ return;
1606
+ case "blur":
1607
+ kn(t, n, i, a, c, r, o);
1608
+ return;
1609
+ }
1610
+ }
1611
+ function In(t, n, e, i, r, o) {
1612
+ t.save(), t.fillStyle = n.color, t.fillRect(e, i, r, o), t.restore();
1613
+ }
1614
+ function Tn(t, n, e, i, r, o, a) {
1615
+ const c = Math.max(o, a), l = Math.max(4, Math.round(8 * Math.min(1, c / 240))), s = Math.max(1, Math.round(o / c * l)), u = Math.max(1, Math.round(a / c * l));
1616
+ t.save();
1617
+ const h = F(s, u);
1618
+ if (!h) {
1619
+ t.restore();
1620
+ return;
1621
+ }
1622
+ h.ctx.imageSmoothingEnabled = !0, h.ctx.imageSmoothingQuality = "low", h.ctx.drawImage(n, i, r, o, a, 0, 0, s, u), t.imageSmoothingEnabled = !1, t.drawImage(h.canvas, 0, 0, s, u, i, r, o, a), t.restore();
1623
+ }
1624
+ function kn(t, n, e, i, r, o, a) {
1625
+ const l = Math.max(1, Math.round(o * 0.125)), s = Math.max(1, Math.round(a * 0.125)), u = F(l, s);
1626
+ if (!u) return;
1627
+ u.ctx.imageSmoothingEnabled = !0, u.ctx.imageSmoothingQuality = "high", u.ctx.drawImage(n, i, r, o, a, 0, 0, l, s);
1628
+ const h = Math.max(1, Math.round(l * 0.5)), f = Math.max(1, Math.round(s * 0.5)), d = F(h, f);
1629
+ if (!d) {
1630
+ t.save(), t.imageSmoothingEnabled = !0, t.imageSmoothingQuality = "high", t.drawImage(u.canvas, 0, 0, l, s, i, r, o, a), t.restore();
1631
+ return;
1632
+ }
1633
+ d.ctx.imageSmoothingEnabled = !0, d.ctx.imageSmoothingQuality = "high", d.ctx.drawImage(u.canvas, 0, 0, l, s, 0, 0, h, f), t.save(), t.imageSmoothingEnabled = !0, t.imageSmoothingQuality = "high", t.drawImage(d.canvas, 0, 0, h, f, i, r, o, a), t.restore();
1634
+ }
1635
+ function F(t, n) {
1636
+ if (typeof OffscreenCanvas < "u")
1637
+ try {
1638
+ const r = new OffscreenCanvas(t, n), o = r.getContext("2d");
1639
+ if (o) return { canvas: r, ctx: o };
1640
+ } catch {
1641
+ }
1642
+ if (typeof document > "u") return null;
1643
+ const e = document.createElement("canvas");
1644
+ e.width = t, e.height = n;
1645
+ const i = e.getContext("2d");
1646
+ return i ? { canvas: e, ctx: i } : null;
1647
+ }
1648
+ const Ii = [
1649
+ "none",
1650
+ "solidSharp",
1651
+ "solidRound",
1652
+ "lineSingle",
1653
+ "hook",
1654
+ "polaroid"
1655
+ ], Cn = [
1656
+ {
1657
+ id: "none",
1658
+ label: "None",
1659
+ acceptsColor: !1,
1660
+ defaultColor: "#000000"
1661
+ },
1662
+ {
1663
+ id: "solidSharp",
1664
+ label: "Mat Sharp",
1665
+ acceptsColor: !0,
1666
+ defaultColor: "#000000"
1667
+ },
1668
+ {
1669
+ id: "solidRound",
1670
+ label: "Mat Round",
1671
+ acceptsColor: !0,
1672
+ defaultColor: "#000000"
1673
+ },
1674
+ {
1675
+ id: "lineSingle",
1676
+ label: "Line Single",
1677
+ acceptsColor: !0,
1678
+ defaultColor: "#000000"
1679
+ },
1680
+ {
1681
+ id: "hook",
1682
+ label: "Corner Hooks",
1683
+ acceptsColor: !0,
1684
+ defaultColor: "#000000"
1685
+ },
1686
+ {
1687
+ id: "polaroid",
1688
+ label: "Polaroid",
1689
+ acceptsColor: !0,
1690
+ defaultColor: "#ffffff"
1691
+ }
1692
+ ], vn = {
1693
+ presetId: "none",
1694
+ color: "#000000"
1695
+ };
1696
+ function Ti() {
1697
+ return vn;
1698
+ }
1699
+ function An(t) {
1700
+ return t.presetId === "none";
1701
+ }
1702
+ function ki(t, n) {
1703
+ if (t.presetId === n) return t;
1704
+ const e = rt(t.presetId), i = rt(n);
1705
+ if (!i) return { ...t, presetId: n };
1706
+ const o = e !== void 0 && t.color === e.defaultColor ? i.defaultColor : t.color;
1707
+ return { presetId: n, color: o };
1708
+ }
1709
+ function Ci(t, n) {
1710
+ return t.color === n ? t : { ...t, color: n };
1711
+ }
1712
+ function rt(t) {
1713
+ return Cn.find((n) => n.id === t);
1714
+ }
1715
+ async function vi(t, n) {
1716
+ return An(t) ? n : t.presetId === "polaroid" ? zn(t.color, n) : t.presetId === "none" ? n : Nn(t.presetId, t.color, n);
1717
+ }
1718
+ function Nn(t, n, e) {
1719
+ const i = g(e.width, e.height), r = y(i);
1720
+ return r.drawImage(e.bitmap, 0, 0, e.width, e.height), Fn(r, t, n, e.width, e.height), {
1721
+ bitmap: i.canvas,
1722
+ width: e.width,
1723
+ height: e.height,
1724
+ mimeType: e.mimeType
1725
+ };
1726
+ }
1727
+ function Fn(t, n, e, i, r) {
1728
+ switch (n) {
1729
+ case "solidSharp":
1730
+ mt(t, e, i, r);
1731
+ return;
1732
+ case "solidRound":
1733
+ Pn(t, e, i, r);
1734
+ return;
1735
+ case "lineSingle":
1736
+ On(t, e, i, r);
1737
+ return;
1738
+ case "hook":
1739
+ _n(t, e, i, r);
1740
+ return;
1741
+ }
1742
+ }
1743
+ function mt(t, n, e, i) {
1744
+ const r = yt(e, i);
1745
+ t.save(), t.fillStyle = n, t.fillRect(0, 0, e, r), t.fillRect(0, i - r, e, r), t.fillRect(0, r, r, i - 2 * r), t.fillRect(e - r, r, r, i - 2 * r), t.restore();
1746
+ }
1747
+ function Pn(t, n, e, i) {
1748
+ const o = yt(e, i);
1749
+ mt(t, n, e, i), t.save(), t.globalCompositeOperation = "destination-out", t.fillStyle = "#000", T(t, 0, 0, o, "tl"), T(t, e, 0, o, "tr"), T(t, 0, i, o, "bl"), T(t, e, i, o, "br"), t.restore();
1750
+ }
1751
+ function T(t, n, e, i, r) {
1752
+ switch (t.beginPath(), t.moveTo(n, e), r) {
1753
+ case "tl":
1754
+ t.lineTo(n + i, e), t.arc(n + i, e + i, i, -Math.PI / 2, Math.PI, !0), t.lineTo(n, e);
1755
+ break;
1756
+ case "tr":
1757
+ t.lineTo(n, e + i), t.arc(n - i, e + i, i, 0, -Math.PI / 2, !0), t.lineTo(n, e);
1758
+ break;
1759
+ case "bl":
1760
+ t.lineTo(n + i, e), t.arc(n + i, e - i, i, Math.PI / 2, Math.PI, !1), t.lineTo(n, e);
1761
+ break;
1762
+ case "br":
1763
+ t.lineTo(n - i, e), t.arc(n - i, e - i, i, Math.PI / 2, 0, !0), t.lineTo(n, e);
1764
+ break;
1765
+ }
1766
+ t.closePath(), t.fill();
1767
+ }
1768
+ function On(t, n, e, i) {
1769
+ const r = Math.round(Math.min(e, i) * 0.05), o = Math.max(2, Math.round(Math.min(e, i) * 0.01));
1770
+ t.save(), t.strokeStyle = n, t.lineWidth = o;
1771
+ const a = o / 2;
1772
+ t.strokeRect(
1773
+ r + a,
1774
+ r + a,
1775
+ e - 2 * r - o,
1776
+ i - 2 * r - o
1777
+ ), t.restore();
1778
+ }
1779
+ function _n(t, n, e, i) {
1780
+ const r = Math.round(Math.min(e, i) * 0.08), o = Math.max(2, Math.round(Math.min(e, i) * 0.01)), a = Math.round(Math.min(e, i) * 0.05);
1781
+ t.save(), t.strokeStyle = n, t.lineWidth = o, t.lineCap = "square";
1782
+ const c = o / 2, l = (s, u, h, f) => {
1783
+ t.beginPath(), t.moveTo(s + h * r, u), t.lineTo(s, u), t.lineTo(s, u + f * r), t.stroke();
1784
+ };
1785
+ l(a + c, a + c, 1, 1), l(e - a - c, a + c, -1, 1), l(a + c, i - a - c, 1, -1), l(e - a - c, i - a - c, -1, -1), t.restore();
1786
+ }
1787
+ function zn(t, n) {
1788
+ const e = Math.min(n.width, n.height), i = Math.round(e * 0.05), r = i, o = i, a = Math.round(e * 0.18), c = n.width + r + o, l = n.height + i + a, s = g(c, l), u = y(s);
1789
+ return u.fillStyle = t, u.fillRect(0, 0, c, l), u.drawImage(n.bitmap, r, i, n.width, n.height), {
1790
+ bitmap: s.canvas,
1791
+ width: c,
1792
+ height: l,
1793
+ mimeType: n.mimeType
1794
+ };
1795
+ }
1796
+ function yt(t, n) {
1797
+ return Math.max(4, Math.round(Math.min(t, n) * 0.04));
1798
+ }
1799
+ function Ai(t, n, e) {
1800
+ if (t !== "polaroid")
1801
+ return { width: n, height: e };
1802
+ const i = Math.min(n, e), r = Math.round(i * 0.05), o = Math.round(i * 0.18);
1803
+ return {
1804
+ width: n + 2 * r,
1805
+ height: e + r + o
1806
+ };
1807
+ }
1808
+ export {
1809
+ Ze as ALL_SELECTION_HANDLES,
1810
+ _ as DEFAULT_FINETUNE_STATE,
1811
+ vn as DEFAULT_FRAME_STATE,
1812
+ ut as DEFAULT_OUTPUT_STATE,
1813
+ cn as DEFAULT_PALETTE_COLOR,
1814
+ Sn as DEFAULT_REDACT_COLOR,
1815
+ bn as DEFAULT_REDACT_MODE,
1816
+ sn as DEFAULT_STROKE_WIDTH,
1817
+ re as ENCODABLE_MIMES,
1818
+ Dn as EventBus,
1819
+ rn as FILTER_PRESETS,
1820
+ Te as FINETUNE_ADJUSTMENTS,
1821
+ J as FINETUNE_MAX,
1822
+ G as FINETUNE_MIN,
1823
+ Z as FINETUNE_STEP,
1824
+ Cn as FRAME_PRESETS,
1825
+ Ii as FRAME_PRESET_IDS,
1826
+ _e as FREEHAND_DEFAULT_STROKE,
1827
+ Yt as FREE_ANGLE_MAX,
1828
+ Wt as FREE_ANGLE_MIN,
1829
+ de as FREE_ANGLE_STEP,
1830
+ Pe as HIGHLIGHT_DEFAULT_COLOR,
1831
+ Oe as HIGHLIGHT_DEFAULT_STROKE,
1832
+ W as HISTORY_MAX_ENTRIES,
1833
+ Jn as History,
1834
+ k as IDENTITY_VIEWPORT_TRANSFORM,
1835
+ Ve as KEYBOARD_PLACEABLE_KINDS,
1836
+ ot as MAX_ZOOM,
1837
+ it as MIN_SAMPLE_DISTANCE,
1838
+ at as MIN_ZOOM,
1839
+ x as PICK_TOLERANCE,
1840
+ oi as REDACT_MODES,
1841
+ ft as RESIZE_MAX_DIMENSION,
1842
+ K as RESIZE_MIN_DIMENSION,
1843
+ dn as SYSTEM_FONT_STACK,
1844
+ an as TEXT_DEFAULT_FONT_SIZE,
1845
+ Un as ViewportController,
1846
+ si as addRegion,
1847
+ Ye as addShape,
1848
+ hn as alignToOrigin,
1849
+ Et as applyAspectRatio,
1850
+ nn as applyClarity,
1851
+ tn as applyFinetuneLutAndSaturation,
1852
+ tt as applyFinetuneToImageData,
1853
+ ee as applyPresetByIndex,
1854
+ w as assertNever,
1855
+ ri as bakeAnnotate,
1856
+ st as bakeCanvasToBlob,
1857
+ ie as bakeCrop,
1858
+ Ne as bakeFinetune,
1859
+ fe as bakeFlip,
1860
+ vi as bakeFrame,
1861
+ Ri as bakeRedact,
1862
+ Ie as bakeResize,
1863
+ xe as bakeRotate,
1864
+ nt as boundingBoxOf,
1865
+ en as boxBlur3x3,
1866
+ Jt as buildFinetuneLut,
1867
+ b as canEncodeMime,
1868
+ ht as clampQuality,
1869
+ P as clampRectInside,
1870
+ pt as computeViewport,
1871
+ vt as copyJpegExif,
1872
+ g as createBakeCanvas,
1873
+ Si as createCenteredRedactRegion,
1874
+ Je as createCenteredShape,
1875
+ Gn as createStore,
1876
+ ii as decimatePoints,
1877
+ ln as defaultStylePalette,
1878
+ li as deleteRedactRegion,
1879
+ Ue as deleteShape,
1880
+ Ot as deriveOutputName,
1881
+ Ut as effectiveAngleDeg,
1882
+ Re as effectivePercent,
1883
+ se as encodeSourceImage,
1884
+ un as estimateTextSize,
1885
+ te as filterPresets,
1886
+ Fe as findActivePreset,
1887
+ rt as findFramePreset,
1888
+ xi as findRedactRegion,
1889
+ He as findShape,
1890
+ on as finetuneStatesEqual,
1891
+ O as fitRectToBoundsWithRatio,
1892
+ Ai as frameOutputSize,
1893
+ y as getBakeContext2D,
1894
+ fn as hitTest,
1895
+ ze as initialAnnotateState,
1896
+ ne as initialCropState,
1897
+ ke as initialFinetuneState,
1898
+ ue as initialFlipState,
1899
+ Ti as initialFrameState,
1900
+ ai as initialRedactState,
1901
+ pe as initialResizeState,
1902
+ ge as initialRotateState,
1903
+ Vt as isFinetuneNoOp,
1904
+ Bt as isFlipNoOp,
1905
+ An as isFrameNoOp,
1906
+ Ge as isKeyboardPlaceableKind,
1907
+ kt as isPresetVisible,
1908
+ jt as isResizeNoOp,
1909
+ Xt as isRotateNoOp,
1910
+ qt as largestInscribedRect,
1911
+ Ln as loadImage,
1912
+ ci as mintRegionId,
1913
+ De as mintShapeId,
1914
+ ui as mirrorRedactRegions,
1915
+ $e as mirrorShape,
1916
+ qe as normaliseRectExtent,
1917
+ Mi as normaliseRedactExtent,
1918
+ Fn as paintInsideFrame,
1919
+ Rn as paintRedactRegion,
1920
+ gn as paintShape,
1921
+ ei as pickShape,
1922
+ Wn as pointDisplayToImage,
1923
+ Bn as pointImageToDisplay,
1924
+ Qn as pointInRect,
1925
+ jn as rectBottom,
1926
+ $n as rectCenter,
1927
+ Xn as rectDisplayToImage,
1928
+ ni as rectFromHandleDrag,
1929
+ Hn as rectFromPoints,
1930
+ Yn as rectImageToDisplay,
1931
+ qn as rectRight,
1932
+ Kn as rectsEqual,
1933
+ Ei as regionBoundingBox,
1934
+ gt as replaceRedactRegion,
1935
+ Xe as replaceShape,
1936
+ Ae as resetAllFinetune,
1937
+ ve as resetFinetune,
1938
+ Zn as resizeRectFromHandle,
1939
+ Pt as resolveOutputMime,
1940
+ $t as resolveOutputSize,
1941
+ bi as revalidateRedactAgainstBounds,
1942
+ me as rotateClockwise,
1943
+ ye as rotateCounterClockwise,
1944
+ fi as rotateRedactRegions,
1945
+ Qe as rotateShape,
1946
+ St as roundRect,
1947
+ le as runUtilityChain,
1948
+ di as selectRedactRegion,
1949
+ We as selectShape,
1950
+ pi as selectedRedactRegionOf,
1951
+ ti as selectionHandlePositions,
1952
+ Le as setActiveTool,
1953
+ Ce as setFinetune,
1954
+ Ci as setFrameColor,
1955
+ ki as setFramePreset,
1956
+ we as setFreeAngle,
1957
+ Se as setHeightPx,
1958
+ Ee as setLockAspect,
1959
+ oe as setOutputMime,
1960
+ ae as setOutputQuality,
1961
+ be as setPercent,
1962
+ mi as setRedactCurrentColor,
1963
+ gi as setRedactCurrentMode,
1964
+ wi as setRedactRegionColor,
1965
+ yi as setRedactRegionMode,
1966
+ ce as setStripMetadata,
1967
+ Be as setStyle,
1968
+ Me as setWidthPx,
1969
+ he as toggleFlip,
1970
+ dt as tracePath,
1971
+ Ke as transformShapes,
1972
+ Vn as translateClampedRect,
1973
+ hi as translateRedactRegions,
1974
+ je as translateShape
1975
+ };
1976
+ //# sourceMappingURL=index.js.map