@hussmartinez/image-sdk 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -6
- package/dist/assets/image/render.worker.js +2 -1
- package/dist/element.d.ts +879 -339
- package/dist/element.js +5 -3
- package/dist/element.js.map +1 -1
- package/dist/index.d.ts +825 -180
- package/dist/index.js +76 -11
- package/dist/index.js.map +1 -1
- package/dist/lazy/inline-worker-DsBNkr8Q.js +30 -0
- package/dist/lazy/{inline-worker-Cvw0Gpxf.js.map → inline-worker-DsBNkr8Q.js.map} +1 -1
- package/dist/react.d.ts +977 -341
- package/dist/react.js +1 -1
- package/dist/shared/core-BlFeJWak.js +2612 -0
- package/dist/shared/core-BlFeJWak.js.map +1 -0
- package/dist/shared/editor-Bjv32UmV.js +3817 -0
- package/dist/shared/editor-Bjv32UmV.js.map +1 -0
- package/dist/styles.css +288 -33
- package/formats/hm-image-command/v2.schema.json +266 -0
- package/formats/hm-image-plan/v2.schema.json +877 -0
- package/package.json +4 -4
- package/dist/lazy/inline-worker-Cvw0Gpxf.js +0 -30
- package/dist/shared/core-CAX3GOaj.js +0 -1444
- package/dist/shared/core-CAX3GOaj.js.map +0 -1
- package/dist/shared/editor-DziYqEP3.js +0 -1748
- package/dist/shared/editor-DziYqEP3.js.map +0 -1
|
@@ -0,0 +1,2612 @@
|
|
|
1
|
+
import { SdkError, applyCommands, coreLimits, createAssetsResolver, createErrorHelpers, createHistory, createStore, createWorkerClient, defineLimits, defineMessages, isSdkError, plural, sdkCoreMessages, withProcessingTime } from "@hussmartinez/sdk-core";
|
|
2
|
+
// src/core/errors/image-error.ts
|
|
3
|
+
var ImageError = class extends SdkError {
|
|
4
|
+
name = "ImageError";
|
|
5
|
+
};
|
|
6
|
+
function isImageError(value, code) {
|
|
7
|
+
return isSdkError(value, code) && value.name === "ImageError";
|
|
8
|
+
}
|
|
9
|
+
// src/core/errors/error-helpers.ts
|
|
10
|
+
var { adapt: asImageError, abortError, throwIfAborted } = createErrorHelpers(ImageError, {
|
|
11
|
+
fallbackCode: "UNKNOWN",
|
|
12
|
+
fallbackMessage: "Image processing failed."
|
|
13
|
+
});
|
|
14
|
+
// src/core/errors/image-limits.ts
|
|
15
|
+
var imageLimitDefinitions = {
|
|
16
|
+
...coreLimits,
|
|
17
|
+
maxDecodedSize: {
|
|
18
|
+
...coreLimits.maxDecodedSize,
|
|
19
|
+
default: 1e9,
|
|
20
|
+
warning: 4e8
|
|
21
|
+
},
|
|
22
|
+
maxImagePixels: {
|
|
23
|
+
code: "IMAGE_TOO_LARGE",
|
|
24
|
+
unit: "pixels",
|
|
25
|
+
default: 5e7,
|
|
26
|
+
warning: 24e6
|
|
27
|
+
},
|
|
28
|
+
maxImageDimension: {
|
|
29
|
+
code: "IMAGE_DIMENSION_TOO_LARGE",
|
|
30
|
+
unit: "pixels",
|
|
31
|
+
default: 16384
|
|
32
|
+
},
|
|
33
|
+
maxFrames: {
|
|
34
|
+
code: "TOO_MANY_FRAMES",
|
|
35
|
+
unit: "count",
|
|
36
|
+
default: 300,
|
|
37
|
+
warning: 100
|
|
38
|
+
},
|
|
39
|
+
maxSessionSize: {
|
|
40
|
+
code: "SESSION_TOO_LARGE",
|
|
41
|
+
unit: "bytes",
|
|
42
|
+
default: 1e9,
|
|
43
|
+
warning: 5e8
|
|
44
|
+
},
|
|
45
|
+
maxLayers: {
|
|
46
|
+
code: "TOO_MANY_LAYERS",
|
|
47
|
+
unit: "count",
|
|
48
|
+
default: 256
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var imageLimits = /* @__PURE__ */ defineLimits(imageLimitDefinitions, { errorClass: ImageError });
|
|
52
|
+
var PER_FILE_LIMITS = [
|
|
53
|
+
"maxFileSize",
|
|
54
|
+
"maxImageDimension",
|
|
55
|
+
"maxImagePixels",
|
|
56
|
+
"maxFrames",
|
|
57
|
+
"maxDecodedSize"
|
|
58
|
+
];
|
|
59
|
+
function checkFileLimits(policy, fileSize, probe) {
|
|
60
|
+
const { width, height } = probe.oriented;
|
|
61
|
+
const actual = {
|
|
62
|
+
maxFileSize: fileSize,
|
|
63
|
+
maxImageDimension: Math.max(width, height),
|
|
64
|
+
maxImagePixels: width * height,
|
|
65
|
+
maxFrames: probe.frames,
|
|
66
|
+
maxDecodedSize: probe.decodedPixelBound * 4
|
|
67
|
+
};
|
|
68
|
+
return PER_FILE_LIMITS.filter((limit) => policy.check(limit, actual[limit]) === "warning");
|
|
69
|
+
}
|
|
70
|
+
var OUTPUT_LIMITS = ["maxImageDimension", "maxImagePixels"];
|
|
71
|
+
function checkOutputLimits(policy, size) {
|
|
72
|
+
policy.check("maxImageDimension", Math.max(size.width, size.height));
|
|
73
|
+
policy.check("maxImagePixels", size.width * size.height);
|
|
74
|
+
}
|
|
75
|
+
// src/adapters/worker-lifecycle.ts
|
|
76
|
+
function createWorkerLifecycle(clearSources, submitted) {
|
|
77
|
+
let current;
|
|
78
|
+
let sequence = 0;
|
|
79
|
+
let terminal = false;
|
|
80
|
+
let destroyed = false;
|
|
81
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
82
|
+
const invalidated = () => new ImageError("WORKER_TERMINATED", "Recreate the image engine before reopening the image.", { details: { reason: "engine-invalidated" } });
|
|
83
|
+
const check = (generation) => {
|
|
84
|
+
if (destroyed) throw new Error("The image engine has been destroyed.");
|
|
85
|
+
if (terminal || generation && (generation.closed || generation !== current)) throw invalidated();
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
check,
|
|
89
|
+
current: () => current,
|
|
90
|
+
getSnapshot: () => terminal,
|
|
91
|
+
subscribe(listener) {
|
|
92
|
+
const notify = () => listener();
|
|
93
|
+
listeners.add(notify);
|
|
94
|
+
return () => {
|
|
95
|
+
listeners.delete(notify);
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
destroy() {
|
|
99
|
+
destroyed = true;
|
|
100
|
+
listeners.clear();
|
|
101
|
+
clearSources();
|
|
102
|
+
},
|
|
103
|
+
wrap(worker) {
|
|
104
|
+
check();
|
|
105
|
+
const generation = {
|
|
106
|
+
number: ++sequence,
|
|
107
|
+
closed: false,
|
|
108
|
+
ownsResources: false
|
|
109
|
+
};
|
|
110
|
+
current = generation;
|
|
111
|
+
return {
|
|
112
|
+
postMessage(message, transfer) {
|
|
113
|
+
check(generation);
|
|
114
|
+
const call = message;
|
|
115
|
+
if (call?.type === "call" && (call.method === "register" || call.method === "attachCanvas")) generation.ownsResources = true;
|
|
116
|
+
if (call?.type === "call" && call.method === "register" && typeof call.input?.id === "string") submitted(call.input.id, generation);
|
|
117
|
+
worker.postMessage(message, transfer);
|
|
118
|
+
},
|
|
119
|
+
addEventListener(type, listener) {
|
|
120
|
+
worker.addEventListener(type, listener);
|
|
121
|
+
},
|
|
122
|
+
removeEventListener(type, listener) {
|
|
123
|
+
worker.removeEventListener(type, listener);
|
|
124
|
+
},
|
|
125
|
+
terminate() {
|
|
126
|
+
if (generation.closed) return;
|
|
127
|
+
generation.closed = true;
|
|
128
|
+
try {
|
|
129
|
+
if (current !== generation) return;
|
|
130
|
+
clearSources();
|
|
131
|
+
if (destroyed || !generation.ownsResources || terminal) return;
|
|
132
|
+
terminal = true;
|
|
133
|
+
const notified = [...listeners];
|
|
134
|
+
queueMicrotask(() => {
|
|
135
|
+
if (destroyed) return;
|
|
136
|
+
for (const listener of notified) {
|
|
137
|
+
if (!listeners.has(listener)) continue;
|
|
138
|
+
try {
|
|
139
|
+
listener();
|
|
140
|
+
} catch {}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
} finally {
|
|
144
|
+
worker.terminate();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function closeRejectedResult(value) {
|
|
152
|
+
if (value && typeof value === "object" && "close" in value && typeof value.close === "function") try {
|
|
153
|
+
value.close();
|
|
154
|
+
} catch {}
|
|
155
|
+
}
|
|
156
|
+
// src/adapters/image-engine.ts
|
|
157
|
+
var ENGINE_INTERNALS = Symbol("hm-image-engine");
|
|
158
|
+
function engineInternals(engine) {
|
|
159
|
+
return engine[ENGINE_INTERNALS];
|
|
160
|
+
}
|
|
161
|
+
function createImageEngine(options = {}) {
|
|
162
|
+
const asset = options.assetsUrl === void 0 ? void 0 : createAssetsResolver(options.assetsUrl);
|
|
163
|
+
let workerUrl;
|
|
164
|
+
return createEngine({
|
|
165
|
+
loadWorker: asset ? async () => () => {
|
|
166
|
+
workerUrl = asset("image/render.worker.js");
|
|
167
|
+
return new Worker(workerUrl, {
|
|
168
|
+
type: "module",
|
|
169
|
+
name: "hm-image-render"
|
|
170
|
+
});
|
|
171
|
+
} : async () => (await import("../lazy/inline-worker-DsBNkr8Q.js")).createInlineWorker,
|
|
172
|
+
version: "0.4.0",
|
|
173
|
+
get asset() {
|
|
174
|
+
return workerUrl ?? "inline";
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function createEngine(environment) {
|
|
179
|
+
let startWorker;
|
|
180
|
+
let destroyed = false;
|
|
181
|
+
const ids = /* @__PURE__ */ new Map();
|
|
182
|
+
const lifecycle = createWorkerLifecycle(() => ids.clear(), (id, generation) => {
|
|
183
|
+
const reservation = ids.get(id);
|
|
184
|
+
if (reservation) reservation.generation = generation;
|
|
185
|
+
});
|
|
186
|
+
const client = createWorkerClient({
|
|
187
|
+
createWorker: () => {
|
|
188
|
+
if (!startWorker) throw new ImageError("WORKER_START_FAILED", "The render worker hasn't loaded.");
|
|
189
|
+
return lifecycle.wrap(startWorker());
|
|
190
|
+
},
|
|
191
|
+
errorClasses: { ImageError },
|
|
192
|
+
idleTimeout: 0,
|
|
193
|
+
cancelTimeout: 3e4,
|
|
194
|
+
packageVersion: environment.version,
|
|
195
|
+
get workerAsset() {
|
|
196
|
+
return environment.asset ?? "inline";
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
async function ready() {
|
|
200
|
+
if (destroyed) throw new Error("The image engine has been destroyed.");
|
|
201
|
+
startWorker ??= await environment.loadWorker();
|
|
202
|
+
}
|
|
203
|
+
const call = async (method, input, options) => {
|
|
204
|
+
lifecycle.check();
|
|
205
|
+
throwIfAborted(options?.signal);
|
|
206
|
+
await ready();
|
|
207
|
+
lifecycle.check();
|
|
208
|
+
const pending = client.call(method, input, options);
|
|
209
|
+
const generation = lifecycle.current();
|
|
210
|
+
const result = await pending.catch((error) => {
|
|
211
|
+
throw error instanceof TypeError ? error : asImageError(error);
|
|
212
|
+
});
|
|
213
|
+
try {
|
|
214
|
+
lifecycle.check(generation);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
closeRejectedResult(result);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
};
|
|
221
|
+
const prepare = async () => {
|
|
222
|
+
lifecycle.check();
|
|
223
|
+
await ready();
|
|
224
|
+
lifecycle.check();
|
|
225
|
+
const pending = client.start();
|
|
226
|
+
const generation = lifecycle.current();
|
|
227
|
+
try {
|
|
228
|
+
await pending;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
throw asImageError(error);
|
|
231
|
+
}
|
|
232
|
+
lifecycle.check(generation);
|
|
233
|
+
if (!generation) throw new Error("The worker did not create a generation.");
|
|
234
|
+
return generation;
|
|
235
|
+
};
|
|
236
|
+
const callOptions = (options) => options?.signal ? { signal: options.signal } : {};
|
|
237
|
+
return {
|
|
238
|
+
[ENGINE_INTERNALS]: {
|
|
239
|
+
call,
|
|
240
|
+
prepare,
|
|
241
|
+
check: lifecycle.check,
|
|
242
|
+
getSnapshot: lifecycle.getSnapshot,
|
|
243
|
+
subscribe: lifecycle.subscribe,
|
|
244
|
+
has: (id) => ids.get(id)?.registered === true
|
|
245
|
+
},
|
|
246
|
+
probe(file, options) {
|
|
247
|
+
return call("probe", { file }, callOptions(options));
|
|
248
|
+
},
|
|
249
|
+
async register(id, file, probe, options) {
|
|
250
|
+
lifecycle.check();
|
|
251
|
+
if (ids.has(id)) throw new TypeError(`The source id "${id}" is already in use.`);
|
|
252
|
+
const reservation = {
|
|
253
|
+
registered: false,
|
|
254
|
+
released: false
|
|
255
|
+
};
|
|
256
|
+
ids.set(id, reservation);
|
|
257
|
+
try {
|
|
258
|
+
const size = await call("register", {
|
|
259
|
+
id,
|
|
260
|
+
file,
|
|
261
|
+
probe,
|
|
262
|
+
limits: Object.fromEntries(PER_FILE_LIMITS.map((limit) => [limit, (options?.filePolicy ?? imageLimits.resolve()).limits[limit].maximum]))
|
|
263
|
+
}, callOptions(options));
|
|
264
|
+
lifecycle.check(reservation.generation);
|
|
265
|
+
if (reservation.released) throw new TypeError("The source was released during registration.");
|
|
266
|
+
reservation.registered = true;
|
|
267
|
+
options?.onProgress?.(1);
|
|
268
|
+
lifecycle.check(reservation.generation);
|
|
269
|
+
return {
|
|
270
|
+
id,
|
|
271
|
+
format: probe.format,
|
|
272
|
+
width: size.width,
|
|
273
|
+
height: size.height,
|
|
274
|
+
frames: 1,
|
|
275
|
+
hasAlpha: probe.hasAlpha,
|
|
276
|
+
decodedBytes: size.width * size.height * 4
|
|
277
|
+
};
|
|
278
|
+
} catch (error) {
|
|
279
|
+
if (ids.get(id) === reservation) ids.delete(id);
|
|
280
|
+
if ((options?.signal?.aborted || reservation.released) && reservation.generation && !reservation.generation.closed && !destroyed && !lifecycle.getSnapshot()) call("release", { id }).catch(() => void 0);
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
async thumbnail(id, maxSize, options) {
|
|
285
|
+
lifecycle.check();
|
|
286
|
+
if (!ids.get(id)?.registered) throw new TypeError(`There's no source with the id "${id}".`);
|
|
287
|
+
if (!(maxSize >= 1)) throw new RangeError("maxSize must be at least 1.");
|
|
288
|
+
return call("thumbnail", {
|
|
289
|
+
id,
|
|
290
|
+
maxSize
|
|
291
|
+
}, callOptions(options));
|
|
292
|
+
},
|
|
293
|
+
async release(id) {
|
|
294
|
+
const reservation = ids.get(id);
|
|
295
|
+
if (!reservation) return;
|
|
296
|
+
if (!reservation.registered) {
|
|
297
|
+
reservation.released = true;
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
ids.delete(id);
|
|
301
|
+
if (destroyed || lifecycle.getSnapshot() || reservation.generation?.closed) return;
|
|
302
|
+
await call("release", { id });
|
|
303
|
+
},
|
|
304
|
+
async destroy() {
|
|
305
|
+
if (destroyed) return;
|
|
306
|
+
destroyed = true;
|
|
307
|
+
lifecycle.destroy();
|
|
308
|
+
client.destroy();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
// src/adapters/image-processor.ts
|
|
313
|
+
var FORMATS = /* @__PURE__ */ new Set([
|
|
314
|
+
"png",
|
|
315
|
+
"jpeg",
|
|
316
|
+
"webp"
|
|
317
|
+
]);
|
|
318
|
+
function createImageProcessor(engine) {
|
|
319
|
+
const internals = engineInternals(engine);
|
|
320
|
+
if (!internals) throw new TypeError("createImageProcessor needs an engine from createImageEngine.");
|
|
321
|
+
let destroyed = false;
|
|
322
|
+
return {
|
|
323
|
+
async export(job, options) {
|
|
324
|
+
if (destroyed) throw new Error("The image processor has been destroyed.");
|
|
325
|
+
const { plan, format } = job;
|
|
326
|
+
if (!FORMATS.has(format.type)) throw new TypeError(`"${String(format.type)}" isn't an export format.`);
|
|
327
|
+
if (format.type !== "png" && format.quality !== void 0) {
|
|
328
|
+
if (!(format.quality >= 0 && format.quality <= 1)) throw new RangeError("quality must be from 0 to 1.");
|
|
329
|
+
}
|
|
330
|
+
internals.check();
|
|
331
|
+
const missing = [plan.base.source, ...layerSources(plan)].find((id) => !internals.has(id));
|
|
332
|
+
if (missing !== void 0) throw new TypeError(`There's no source with the id "${missing}".`);
|
|
333
|
+
const signal = options?.signal;
|
|
334
|
+
throwIfAborted(signal);
|
|
335
|
+
const policy = options?.filePolicy ?? imageLimits.resolve();
|
|
336
|
+
try {
|
|
337
|
+
const blob = await internals.call("export", {
|
|
338
|
+
plan,
|
|
339
|
+
format,
|
|
340
|
+
limits: Object.fromEntries(OUTPUT_LIMITS.map((limit) => [limit, policy.limits[limit].maximum]))
|
|
341
|
+
}, signal ? { signal } : {});
|
|
342
|
+
options?.onProgress?.(1);
|
|
343
|
+
internals.check();
|
|
344
|
+
return blob;
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (error instanceof TypeError) throw error;
|
|
347
|
+
throw asImageError(error);
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
async destroy() {
|
|
351
|
+
destroyed = true;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function layerSources(plan) {
|
|
356
|
+
return Array.isArray(plan.layers) ? plan.layers.flatMap((layer) => layer.kind === "image" ? [layer.source] : []) : [];
|
|
357
|
+
}
|
|
358
|
+
// src/core/ports/image-renderer.ts
|
|
359
|
+
var MAX_PIXEL_RATIO = 4;
|
|
360
|
+
function backingSize(view) {
|
|
361
|
+
const ratio = Math.min(view.pixelRatio, 4);
|
|
362
|
+
return {
|
|
363
|
+
width: Math.max(1, Math.round(view.width * ratio)),
|
|
364
|
+
height: Math.max(1, Math.round(view.height * ratio))
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function fittedView(width, height, pixelRatio = 1) {
|
|
368
|
+
return {
|
|
369
|
+
width,
|
|
370
|
+
height,
|
|
371
|
+
pixelRatio,
|
|
372
|
+
zoom: 1,
|
|
373
|
+
pan: {
|
|
374
|
+
x: 0,
|
|
375
|
+
y: 0
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function placement(composition, view) {
|
|
380
|
+
const inset = view.inset ?? 0;
|
|
381
|
+
const fit = Math.min((view.width - 2 * inset) / composition.width, (view.height - 2 * inset) / composition.height);
|
|
382
|
+
const scale = Math.max(0, fit) * view.zoom;
|
|
383
|
+
const width = composition.width * scale;
|
|
384
|
+
const height = composition.height * scale;
|
|
385
|
+
return {
|
|
386
|
+
x: (view.width - width) / 2 + view.pan.x,
|
|
387
|
+
y: (view.height - height) / 2 + view.pan.y,
|
|
388
|
+
width,
|
|
389
|
+
height,
|
|
390
|
+
scale
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
// src/adapters/image-renderer.ts
|
|
394
|
+
var RENDERER_INTERNALS = Symbol("hm-image-renderer");
|
|
395
|
+
var nextCanvasId = 0;
|
|
396
|
+
function checkView(view) {
|
|
397
|
+
const positive = (n) => Number.isFinite(n) && n > 0;
|
|
398
|
+
if (!positive(view.width) || !positive(view.height) || !positive(view.pixelRatio) || !positive(view.zoom) || !Number.isFinite(view.pan.x) || !Number.isFinite(view.pan.y) || view.inset !== void 0 && !(view.inset >= 0 && 2 * view.inset < Math.min(view.width, view.height))) throw new RangeError("A view needs a positive size, pixel ratio and zoom, a finite pan, and an inset under half its size.");
|
|
399
|
+
}
|
|
400
|
+
function createImageRenderer(engine) {
|
|
401
|
+
const internals = engineInternals(engine);
|
|
402
|
+
if (!internals) throw new TypeError("createImageRenderer needs an engine from createImageEngine.");
|
|
403
|
+
nextCanvasId += 1;
|
|
404
|
+
const id = `preview-${nextCanvasId}`;
|
|
405
|
+
let attached = false;
|
|
406
|
+
let destroyed = false;
|
|
407
|
+
let drawn = 0;
|
|
408
|
+
let pending;
|
|
409
|
+
let inFlight;
|
|
410
|
+
const pump = () => {
|
|
411
|
+
if (inFlight || !pending) return;
|
|
412
|
+
const request = pending;
|
|
413
|
+
pending = void 0;
|
|
414
|
+
const controller = new AbortController();
|
|
415
|
+
const onAbort = () => controller.abort(request.signal?.reason);
|
|
416
|
+
request.signal?.addEventListener("abort", onAbort, { once: true });
|
|
417
|
+
inFlight = { controller };
|
|
418
|
+
internals.call("render", {
|
|
419
|
+
id,
|
|
420
|
+
plan: request.plan,
|
|
421
|
+
view: request.view,
|
|
422
|
+
limits: request.limits
|
|
423
|
+
}, { signal: controller.signal }).then(() => {
|
|
424
|
+
try {
|
|
425
|
+
internals.check();
|
|
426
|
+
} catch (error) {
|
|
427
|
+
request.reject(error);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
drawn += 1;
|
|
431
|
+
request.resolve("drawn");
|
|
432
|
+
}, (error) => {
|
|
433
|
+
if (!internals.getSnapshot() && controller.signal.aborted && !request.signal?.aborted && isImageError(error, "ABORTED")) request.resolve("superseded");
|
|
434
|
+
else request.reject(error instanceof TypeError ? error : asImageError(error));
|
|
435
|
+
}).finally(() => {
|
|
436
|
+
request.signal?.removeEventListener("abort", onAbort);
|
|
437
|
+
inFlight = void 0;
|
|
438
|
+
pump();
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
return {
|
|
442
|
+
[RENDERER_INTERNALS]: {
|
|
443
|
+
snapshot: async () => internals.call("snapshot", { id }),
|
|
444
|
+
drawn: () => drawn
|
|
445
|
+
},
|
|
446
|
+
async attach(canvas) {
|
|
447
|
+
if (destroyed) throw new Error("The image renderer has been destroyed.");
|
|
448
|
+
internals.check();
|
|
449
|
+
if (attached) throw new TypeError("The renderer already has a canvas.");
|
|
450
|
+
if (typeof canvas.transferControlToOffscreen !== "function") throw new ImageError("UNSUPPORTED_ENVIRONMENT", "This browser can't draw the preview in a worker.", { details: { feature: "transferControlToOffscreen" } });
|
|
451
|
+
try {
|
|
452
|
+
const generation = await internals.prepare();
|
|
453
|
+
internals.check(generation);
|
|
454
|
+
if (destroyed) throw new Error("The image renderer has been destroyed.");
|
|
455
|
+
const offscreen = canvas.transferControlToOffscreen();
|
|
456
|
+
await internals.call("attachCanvas", {
|
|
457
|
+
id,
|
|
458
|
+
canvas: offscreen
|
|
459
|
+
}, { transfer: true });
|
|
460
|
+
internals.check(generation);
|
|
461
|
+
if (destroyed) {
|
|
462
|
+
await internals.call("detachCanvas", { id });
|
|
463
|
+
throw new Error("The image renderer has been destroyed.");
|
|
464
|
+
}
|
|
465
|
+
attached = true;
|
|
466
|
+
} catch (error) {
|
|
467
|
+
throw asImageError(error);
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
render(plan, view, options) {
|
|
471
|
+
if (destroyed) return Promise.reject(/* @__PURE__ */ new Error("The image renderer has been destroyed."));
|
|
472
|
+
try {
|
|
473
|
+
internals.check();
|
|
474
|
+
} catch (error) {
|
|
475
|
+
return Promise.reject(error);
|
|
476
|
+
}
|
|
477
|
+
if (!attached) return Promise.reject(/* @__PURE__ */ new TypeError("Attach a canvas before rendering."));
|
|
478
|
+
const missing = [plan.base.source, ...layerSources(plan)].find((id) => !internals.has(id));
|
|
479
|
+
if (missing !== void 0) return Promise.reject(/* @__PURE__ */ new TypeError(`There's no source with the id "${missing}".`));
|
|
480
|
+
const policy = options?.filePolicy ?? imageLimits.resolve();
|
|
481
|
+
try {
|
|
482
|
+
checkView(view);
|
|
483
|
+
checkOutputLimits(policy, backingSize(view));
|
|
484
|
+
throwIfAborted(options?.signal);
|
|
485
|
+
} catch (error) {
|
|
486
|
+
return Promise.reject(error);
|
|
487
|
+
}
|
|
488
|
+
const limits = Object.fromEntries(OUTPUT_LIMITS.map((limit) => [limit, policy.limits[limit].maximum]));
|
|
489
|
+
return new Promise((resolve, reject) => {
|
|
490
|
+
pending?.resolve("superseded");
|
|
491
|
+
pending = {
|
|
492
|
+
plan,
|
|
493
|
+
view,
|
|
494
|
+
resolve,
|
|
495
|
+
reject,
|
|
496
|
+
signal: options?.signal,
|
|
497
|
+
limits
|
|
498
|
+
};
|
|
499
|
+
inFlight?.controller.abort(new DOMException("A later render replaced this one.", "AbortError"));
|
|
500
|
+
pump();
|
|
501
|
+
});
|
|
502
|
+
},
|
|
503
|
+
async destroy() {
|
|
504
|
+
if (destroyed) return;
|
|
505
|
+
destroyed = true;
|
|
506
|
+
if (pending) try {
|
|
507
|
+
internals.check();
|
|
508
|
+
pending.resolve("superseded");
|
|
509
|
+
} catch (error) {
|
|
510
|
+
pending.reject(error);
|
|
511
|
+
}
|
|
512
|
+
pending = void 0;
|
|
513
|
+
inFlight?.controller.abort(new DOMException("The renderer was destroyed.", "AbortError"));
|
|
514
|
+
if (attached && !internals.getSnapshot()) try {
|
|
515
|
+
await internals.call("detachCanvas", { id });
|
|
516
|
+
} catch {}
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
// src/core/model/plan.ts
|
|
521
|
+
var DEVELOP_ADJUSTMENTS = [
|
|
522
|
+
"temperature",
|
|
523
|
+
"tint",
|
|
524
|
+
"exposure",
|
|
525
|
+
"highlights",
|
|
526
|
+
"shadows",
|
|
527
|
+
"contrast",
|
|
528
|
+
"saturation",
|
|
529
|
+
"vibrance"
|
|
530
|
+
];
|
|
531
|
+
var NEUTRAL_DEVELOP = Object.freeze({
|
|
532
|
+
temperature: 0,
|
|
533
|
+
tint: 0,
|
|
534
|
+
exposure: 0,
|
|
535
|
+
highlights: 0,
|
|
536
|
+
shadows: 0,
|
|
537
|
+
contrast: 0,
|
|
538
|
+
saturation: 0,
|
|
539
|
+
vibrance: 0
|
|
540
|
+
});
|
|
541
|
+
var FILTER_PRESETS = [
|
|
542
|
+
"vivid",
|
|
543
|
+
"warm",
|
|
544
|
+
"cool",
|
|
545
|
+
"fade",
|
|
546
|
+
"mono",
|
|
547
|
+
"noir",
|
|
548
|
+
"sepia"
|
|
549
|
+
];
|
|
550
|
+
var MAX_PLAN_ITEMS = 1024;
|
|
551
|
+
var MAX_KEY_LENGTH = 64;
|
|
552
|
+
var MAX_SCENE_COORDINATE = 65536;
|
|
553
|
+
var REDACTION_MODES = ["solid", "pixelate"];
|
|
554
|
+
var MAX_LAYER_TEXT_LENGTH = 1e3;
|
|
555
|
+
var MAX_LAYER_NAME_LENGTH = 100;
|
|
556
|
+
var MAX_STROKE_WIDTH = 1024;
|
|
557
|
+
var LAYER_FONTS = [
|
|
558
|
+
"sans-serif",
|
|
559
|
+
"serif",
|
|
560
|
+
"monospace"
|
|
561
|
+
];
|
|
562
|
+
var LAYER_ALIGNMENTS = [
|
|
563
|
+
"start",
|
|
564
|
+
"center",
|
|
565
|
+
"end"
|
|
566
|
+
];
|
|
567
|
+
var LAYER_SHAPES = ["rectangle", "ellipse"];
|
|
568
|
+
var LAYER_KINDS = [
|
|
569
|
+
"text",
|
|
570
|
+
"shape",
|
|
571
|
+
"image"
|
|
572
|
+
];
|
|
573
|
+
var UPRIGHT = Object.freeze({
|
|
574
|
+
rotate: 0,
|
|
575
|
+
flipX: false,
|
|
576
|
+
flipY: false
|
|
577
|
+
});
|
|
578
|
+
var FULL_SIZE = Object.freeze({
|
|
579
|
+
kind: "scale",
|
|
580
|
+
scale: 1
|
|
581
|
+
});
|
|
582
|
+
function createPlan(source) {
|
|
583
|
+
return {
|
|
584
|
+
base: {
|
|
585
|
+
source,
|
|
586
|
+
crop: null,
|
|
587
|
+
orientation: UPRIGHT,
|
|
588
|
+
develop: NEUTRAL_DEVELOP,
|
|
589
|
+
filter: null
|
|
590
|
+
},
|
|
591
|
+
layers: [],
|
|
592
|
+
redactions: [],
|
|
593
|
+
animation: null,
|
|
594
|
+
output: FULL_SIZE,
|
|
595
|
+
metadata: {
|
|
596
|
+
title: "",
|
|
597
|
+
description: "",
|
|
598
|
+
artist: "",
|
|
599
|
+
copyright: ""
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function isValidCrop(crop, source) {
|
|
604
|
+
const { x, y, width, height } = crop;
|
|
605
|
+
return [
|
|
606
|
+
x,
|
|
607
|
+
y,
|
|
608
|
+
width,
|
|
609
|
+
height
|
|
610
|
+
].every(Number.isSafeInteger) && x >= 0 && y >= 0 && width >= 1 && height >= 1 && x + width <= source.width && y + height <= source.height;
|
|
611
|
+
}
|
|
612
|
+
// src/core/model/source.ts
|
|
613
|
+
var NO_METADATA = Object.freeze({
|
|
614
|
+
location: false,
|
|
615
|
+
camera: false,
|
|
616
|
+
timestamps: false,
|
|
617
|
+
thumbnail: false,
|
|
618
|
+
colourProfile: false,
|
|
619
|
+
other: false
|
|
620
|
+
});
|
|
621
|
+
var EMPTY_KEEPABLE_METADATA = Object.freeze({
|
|
622
|
+
title: "",
|
|
623
|
+
description: "",
|
|
624
|
+
artist: "",
|
|
625
|
+
copyright: ""
|
|
626
|
+
});
|
|
627
|
+
var MAX_METADATA_LENGTH = 2e3;
|
|
628
|
+
function longerThan(text, max) {
|
|
629
|
+
if (text.length <= max) return false;
|
|
630
|
+
let count = 0;
|
|
631
|
+
for (const _ of text) {
|
|
632
|
+
count += 1;
|
|
633
|
+
if (count > max) return true;
|
|
634
|
+
}
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
// src/core/operations/checks.ts
|
|
638
|
+
var Invalid = class extends Error {
|
|
639
|
+
reason;
|
|
640
|
+
constructor(reason, message) {
|
|
641
|
+
super(message);
|
|
642
|
+
this.reason = reason;
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
function invalid(reason, message) {
|
|
646
|
+
throw new Invalid(reason, message);
|
|
647
|
+
}
|
|
648
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
649
|
+
function onlyFields(value, fields, what) {
|
|
650
|
+
for (const name of Object.keys(value)) if (!fields.includes(name)) invalid("malformed-command", `${what} has no field "${name}"`);
|
|
651
|
+
}
|
|
652
|
+
function allFields(value, fields, what) {
|
|
653
|
+
onlyFields(value, fields, what);
|
|
654
|
+
for (const name of fields) if (!Object.hasOwn(value, name)) invalid("malformed-command", `${what} needs "${name}"`);
|
|
655
|
+
}
|
|
656
|
+
var isWhole = (value, min, max = Number.MAX_SAFE_INTEGER) => Number.isSafeInteger(value) && value >= min && value <= max;
|
|
657
|
+
function checkCrop(value) {
|
|
658
|
+
if (!isRecord(value)) invalid("malformed-command", "a crop is an object");
|
|
659
|
+
allFields(value, [
|
|
660
|
+
"x",
|
|
661
|
+
"y",
|
|
662
|
+
"width",
|
|
663
|
+
"height"
|
|
664
|
+
], "a crop");
|
|
665
|
+
const { x, y, width, height } = value;
|
|
666
|
+
if (!isWhole(x, 0) || !isWhole(y, 0) || !isWhole(width, 1) || !isWhole(height, 1)) invalid("malformed-command", "a crop is whole pixels from 0, at least 1 × 1");
|
|
667
|
+
return {
|
|
668
|
+
x,
|
|
669
|
+
y,
|
|
670
|
+
width,
|
|
671
|
+
height
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
function checkCropInSource(crop, source) {
|
|
675
|
+
if (!isValidCrop(crop, source)) invalid("crop-outside-source", "a crop must fit inside its source");
|
|
676
|
+
}
|
|
677
|
+
// src/core/operations/keyed-list.ts
|
|
678
|
+
function checkKey(value) {
|
|
679
|
+
if (typeof value !== "string") invalid("malformed-command", "a key is text");
|
|
680
|
+
if (value === "" || longerThan(value, 64)) invalid("invalid-key", `a key has 1 to 64 code points`);
|
|
681
|
+
return value;
|
|
682
|
+
}
|
|
683
|
+
function checkList(value, what) {
|
|
684
|
+
if (!Array.isArray(value)) invalid("malformed-command", `${what} is a list`);
|
|
685
|
+
if (value.length === 0) invalid("malformed-command", `${what} has at least one item`);
|
|
686
|
+
return value;
|
|
687
|
+
}
|
|
688
|
+
function checkKeys(value) {
|
|
689
|
+
const keys = checkList(value, "keys").map(checkKey);
|
|
690
|
+
if (new Set(keys).size !== keys.length) invalid("malformed-command", "a key is listed twice");
|
|
691
|
+
return keys;
|
|
692
|
+
}
|
|
693
|
+
function checkIndex(value, max) {
|
|
694
|
+
if (!isWhole(value, 0)) invalid("malformed-command", "an index is a whole number from 0");
|
|
695
|
+
if (value > max) invalid("invalid-index", `an index is from 0 to ${max}`);
|
|
696
|
+
return value;
|
|
697
|
+
}
|
|
698
|
+
function indexes(list, keys, names) {
|
|
699
|
+
return keys.map((key) => {
|
|
700
|
+
const index = list.findIndex((item) => item.key === key);
|
|
701
|
+
if (index < 0) invalid(names.unknown, `there's no ${names.item} "${key}"`);
|
|
702
|
+
return index;
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
function checkUnused(keys, taken) {
|
|
706
|
+
const seen = new Set(taken);
|
|
707
|
+
for (const key of keys) {
|
|
708
|
+
if (seen.has(key)) invalid("duplicate-key", `the key "${key}" is already used`);
|
|
709
|
+
seen.add(key);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
function insertItems(list, at, items, taken) {
|
|
713
|
+
const index = checkIndex(at, list.length);
|
|
714
|
+
const keys = items.map((item) => item.key);
|
|
715
|
+
checkUnused(keys, taken);
|
|
716
|
+
return {
|
|
717
|
+
list: [
|
|
718
|
+
...list.slice(0, index),
|
|
719
|
+
...items,
|
|
720
|
+
...list.slice(index)
|
|
721
|
+
],
|
|
722
|
+
keys
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function removeItems(list, keys, names) {
|
|
726
|
+
const removed = new Set(indexes(list, checkKeys(keys), names));
|
|
727
|
+
const entries = [];
|
|
728
|
+
list.forEach((item, at) => {
|
|
729
|
+
if (removed.has(at)) entries.push({
|
|
730
|
+
at,
|
|
731
|
+
item
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
return {
|
|
735
|
+
list: list.filter((_, at) => !removed.has(at)),
|
|
736
|
+
entries
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
function restoreItems(list, entries, taken) {
|
|
740
|
+
const keys = entries.map(({ item }) => item.key);
|
|
741
|
+
checkUnused(keys, taken);
|
|
742
|
+
const next = [...list];
|
|
743
|
+
let last = -1;
|
|
744
|
+
for (const { at, item } of entries) {
|
|
745
|
+
const index = checkIndex(at, next.length);
|
|
746
|
+
if (index <= last) invalid("invalid-index", "restored items are listed by rising index");
|
|
747
|
+
last = index;
|
|
748
|
+
next.splice(index, 0, item);
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
list: next,
|
|
752
|
+
keys
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function moveItems(list, keys, to, names) {
|
|
756
|
+
const moving = new Set(indexes(list, checkKeys(keys), names));
|
|
757
|
+
const moved = list.filter((_, at) => moving.has(at));
|
|
758
|
+
const rest = list.filter((_, at) => !moving.has(at));
|
|
759
|
+
const index = checkIndex(to, rest.length);
|
|
760
|
+
return {
|
|
761
|
+
list: [
|
|
762
|
+
...rest.slice(0, index),
|
|
763
|
+
...moved,
|
|
764
|
+
...rest.slice(index)
|
|
765
|
+
],
|
|
766
|
+
order: list.map((item) => item.key)
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
function orderItems(list, keys, names) {
|
|
770
|
+
const order = indexes(list, checkKeys(keys), names);
|
|
771
|
+
if (order.length !== list.length) invalid("invalid-order", "an order lists every key once");
|
|
772
|
+
return {
|
|
773
|
+
list: order.map((at) => list[at]),
|
|
774
|
+
order: list.map((item) => item.key)
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
function updateItems(list, updates, names, patch) {
|
|
778
|
+
const entries = checkList(updates, "updates").map((update) => {
|
|
779
|
+
if (!isRecord(update)) invalid("malformed-command", "an update is an object");
|
|
780
|
+
allFields(update, ["key", "props"], "an update");
|
|
781
|
+
return {
|
|
782
|
+
key: checkKey(update.key),
|
|
783
|
+
props: update.props
|
|
784
|
+
};
|
|
785
|
+
});
|
|
786
|
+
const keys = entries.map(({ key }) => key);
|
|
787
|
+
if (new Set(keys).size !== keys.length) invalid("duplicate-key", "a key is updated twice");
|
|
788
|
+
const at = indexes(list, keys, names);
|
|
789
|
+
const next = [...list];
|
|
790
|
+
return {
|
|
791
|
+
list: next,
|
|
792
|
+
previous: entries.map(({ key, props }, i) => {
|
|
793
|
+
const index = at[i];
|
|
794
|
+
const patched = patch(next[index], props);
|
|
795
|
+
next[index] = patched.item;
|
|
796
|
+
return {
|
|
797
|
+
key,
|
|
798
|
+
props: patched.previous
|
|
799
|
+
};
|
|
800
|
+
})
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
// src/core/operations/redactions.ts
|
|
804
|
+
var REDACTION_NAMES = {
|
|
805
|
+
unknown: "unknown-redaction",
|
|
806
|
+
item: "redaction"
|
|
807
|
+
};
|
|
808
|
+
var MODES = REDACTION_MODES;
|
|
809
|
+
var REGION = [
|
|
810
|
+
"x",
|
|
811
|
+
"y",
|
|
812
|
+
"width",
|
|
813
|
+
"height"
|
|
814
|
+
];
|
|
815
|
+
var PROPS = [
|
|
816
|
+
"mode",
|
|
817
|
+
...REGION,
|
|
818
|
+
"colour"
|
|
819
|
+
];
|
|
820
|
+
var COLOUR = /^#[0-9a-f]{6}$/;
|
|
821
|
+
function checkColour(value) {
|
|
822
|
+
if (typeof value !== "string") invalid("malformed-command", "a colour is text");
|
|
823
|
+
if (!COLOUR.test(value)) invalid("invalid-colour", "a colour is #rrggbb, in lower case");
|
|
824
|
+
return value;
|
|
825
|
+
}
|
|
826
|
+
function checkRedaction(value) {
|
|
827
|
+
if (!isRecord(value)) invalid("malformed-command", "a redaction is an object");
|
|
828
|
+
const { mode } = value;
|
|
829
|
+
if (typeof mode !== "string") invalid("malformed-command", "a redaction's mode is text");
|
|
830
|
+
if (!MODES.includes(mode)) invalid("unknown-redaction-mode", `a redaction's mode is one of ${REDACTION_MODES.join(", ")}`);
|
|
831
|
+
const solid = mode === "solid";
|
|
832
|
+
allFields(value, [
|
|
833
|
+
"key",
|
|
834
|
+
"mode",
|
|
835
|
+
...REGION,
|
|
836
|
+
...solid ? ["colour"] : []
|
|
837
|
+
], "a redaction");
|
|
838
|
+
const key = checkKey(value["key"]);
|
|
839
|
+
const { x, y, width, height } = value;
|
|
840
|
+
if (![
|
|
841
|
+
x,
|
|
842
|
+
y,
|
|
843
|
+
width,
|
|
844
|
+
height
|
|
845
|
+
].every(Number.isInteger)) invalid("malformed-command", "a redaction's region is whole pixels");
|
|
846
|
+
if (!isWhole(x, -65536, 65536) || !isWhole(y, -65536, 65536) || !isWhole(width, 1, 65536) || !isWhole(height, 1, 65536)) invalid("invalid-region", `a redaction's region is at least 1 × 1, within ${MAX_SCENE_COORDINATE} pixels of the scene's origin`);
|
|
847
|
+
return solid ? {
|
|
848
|
+
key,
|
|
849
|
+
mode: "solid",
|
|
850
|
+
x,
|
|
851
|
+
y,
|
|
852
|
+
width,
|
|
853
|
+
height,
|
|
854
|
+
colour: checkColour(value["colour"])
|
|
855
|
+
} : {
|
|
856
|
+
key,
|
|
857
|
+
mode: "pixelate",
|
|
858
|
+
x,
|
|
859
|
+
y,
|
|
860
|
+
width,
|
|
861
|
+
height
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function patchRedaction(item, props) {
|
|
865
|
+
if (!isRecord(props)) invalid("malformed-command", "an update's props are an object");
|
|
866
|
+
onlyFields(props, PROPS, "a redaction's props");
|
|
867
|
+
const merged = { ...item };
|
|
868
|
+
const previous = {};
|
|
869
|
+
const before = item;
|
|
870
|
+
for (const name of PROPS) {
|
|
871
|
+
if (!Object.hasOwn(props, name)) continue;
|
|
872
|
+
merged[name] = props[name];
|
|
873
|
+
if (Object.hasOwn(before, name)) previous[name] = before[name];
|
|
874
|
+
}
|
|
875
|
+
if (merged["mode"] === "pixelate") {
|
|
876
|
+
if (Object.hasOwn(props, "colour")) invalid("unexpected-colour", "a pixelated redaction has no colour");
|
|
877
|
+
if (item.mode === "solid") previous["colour"] = item.colour;
|
|
878
|
+
delete merged["colour"];
|
|
879
|
+
} else if (merged["mode"] === "solid" && !Object.hasOwn(merged, "colour")) invalid("missing-colour", "a redaction set to solid needs a colour in the same update");
|
|
880
|
+
return {
|
|
881
|
+
item: checkRedaction(merged),
|
|
882
|
+
previous
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
// src/core/operations/layers.ts
|
|
886
|
+
var LAYER_NAMES = {
|
|
887
|
+
unknown: "unknown-layer",
|
|
888
|
+
item: "layer"
|
|
889
|
+
};
|
|
890
|
+
var COMMON = [
|
|
891
|
+
"key",
|
|
892
|
+
"kind",
|
|
893
|
+
"name",
|
|
894
|
+
"visible",
|
|
895
|
+
"opacity"
|
|
896
|
+
];
|
|
897
|
+
var BOX = [
|
|
898
|
+
"x",
|
|
899
|
+
"y",
|
|
900
|
+
"width",
|
|
901
|
+
"height",
|
|
902
|
+
"rotation",
|
|
903
|
+
"flipX"
|
|
904
|
+
];
|
|
905
|
+
var OWN = {
|
|
906
|
+
text: [
|
|
907
|
+
"text",
|
|
908
|
+
"font",
|
|
909
|
+
"bold",
|
|
910
|
+
"colour",
|
|
911
|
+
"align"
|
|
912
|
+
],
|
|
913
|
+
shape: [
|
|
914
|
+
"shape",
|
|
915
|
+
"fill",
|
|
916
|
+
"stroke"
|
|
917
|
+
],
|
|
918
|
+
image: ["source", "crop"]
|
|
919
|
+
};
|
|
920
|
+
var KINDS = LAYER_KINDS;
|
|
921
|
+
var ANY_OWN = new Set(Object.values(OWN).flat());
|
|
922
|
+
var FORBIDDEN_IN_TEXT = /[\u0000-\u0009\u000B-\u001F\u007F--]/u;
|
|
923
|
+
function checkText(value, max, what) {
|
|
924
|
+
if (typeof value !== "string") invalid("malformed-command", `${what} is text`);
|
|
925
|
+
if (longerThan(value, max)) invalid("text-too-long", `${what} has at most ${max} code points`);
|
|
926
|
+
if (FORBIDDEN_IN_TEXT.test(value)) invalid("invalid-text", `${what} can't hold control characters other than a line feed, or bidirectional controls`);
|
|
927
|
+
return value;
|
|
928
|
+
}
|
|
929
|
+
function checkOneOf(value, options, what) {
|
|
930
|
+
if (typeof value !== "string" || !options.includes(value)) invalid("malformed-command", `${what} is one of ${options.join(", ")}`);
|
|
931
|
+
return value;
|
|
932
|
+
}
|
|
933
|
+
function checkBoolean(value, what) {
|
|
934
|
+
if (typeof value !== "boolean") invalid("malformed-command", `${what} is true or false`);
|
|
935
|
+
return value;
|
|
936
|
+
}
|
|
937
|
+
function checkLayer(value, sourceSize) {
|
|
938
|
+
if (!isRecord(value)) invalid("malformed-command", "a layer is an object");
|
|
939
|
+
const { kind } = value;
|
|
940
|
+
if (typeof kind !== "string") invalid("malformed-command", "a layer's kind is text");
|
|
941
|
+
if (!KINDS.includes(kind)) invalid("unknown-layer-kind", `a layer's kind is one of ${LAYER_KINDS.join(", ")}`);
|
|
942
|
+
const own = OWN[kind];
|
|
943
|
+
allFields(value, [
|
|
944
|
+
...COMMON,
|
|
945
|
+
...BOX,
|
|
946
|
+
...own
|
|
947
|
+
], `a ${kind} layer`);
|
|
948
|
+
const key = checkKey(value["key"]);
|
|
949
|
+
const name = checkText(value["name"], 100, "a layer's name");
|
|
950
|
+
const visible = checkBoolean(value["visible"], "a layer's visible");
|
|
951
|
+
const { opacity, x, y, width, height, rotation } = value;
|
|
952
|
+
if (![
|
|
953
|
+
opacity,
|
|
954
|
+
x,
|
|
955
|
+
y,
|
|
956
|
+
width,
|
|
957
|
+
height,
|
|
958
|
+
rotation
|
|
959
|
+
].every(Number.isInteger)) invalid("malformed-command", "a layer's opacity and box are whole numbers");
|
|
960
|
+
if (!isWhole(opacity, 0, 100)) invalid("invalid-opacity", "a layer's opacity is from 0 to 100");
|
|
961
|
+
if (!isWhole(x, -65536, 65536) || !isWhole(y, -65536, 65536) || !isWhole(width, 1, 65536) || !isWhole(height, 1, 65536) || !isWhole(rotation, 0, 3599)) invalid("invalid-box", `a layer's box is at least 1 × 1, within ${MAX_SCENE_COORDINATE} pixels of the scene's origin, turned by 0 to 3599 tenths of a degree`);
|
|
962
|
+
const common = {
|
|
963
|
+
key,
|
|
964
|
+
name,
|
|
965
|
+
visible,
|
|
966
|
+
opacity,
|
|
967
|
+
x,
|
|
968
|
+
y,
|
|
969
|
+
width,
|
|
970
|
+
height,
|
|
971
|
+
rotation,
|
|
972
|
+
flipX: checkBoolean(value["flipX"], "a layer's flipX")
|
|
973
|
+
};
|
|
974
|
+
if (kind === "text") return {
|
|
975
|
+
...common,
|
|
976
|
+
kind,
|
|
977
|
+
text: checkText(value["text"], MAX_LAYER_TEXT_LENGTH, "a layer's text"),
|
|
978
|
+
font: checkOneOf(value["font"], LAYER_FONTS, "a layer's font"),
|
|
979
|
+
bold: checkBoolean(value["bold"], "a layer's bold"),
|
|
980
|
+
colour: checkColour(value["colour"]),
|
|
981
|
+
align: checkOneOf(value["align"], LAYER_ALIGNMENTS, "a layer's align")
|
|
982
|
+
};
|
|
983
|
+
if (kind === "shape") {
|
|
984
|
+
const shape = checkOneOf(value["shape"], LAYER_SHAPES, "a layer's shape");
|
|
985
|
+
const fill = value["fill"] === null ? null : checkColour(value["fill"]);
|
|
986
|
+
let stroke = null;
|
|
987
|
+
if (value["stroke"] !== null) {
|
|
988
|
+
const given = value["stroke"];
|
|
989
|
+
if (!isRecord(given)) invalid("malformed-command", "a stroke is an object or null");
|
|
990
|
+
allFields(given, ["colour", "width"], "a stroke");
|
|
991
|
+
if (!isWhole(given.width, 1, 1024)) invalid("malformed-command", `a stroke's width is from 1 to ${MAX_STROKE_WIDTH} pixels`);
|
|
992
|
+
stroke = {
|
|
993
|
+
colour: checkColour(given.colour),
|
|
994
|
+
width: given.width
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
if (fill === null && stroke === null) invalid("empty-shape", "a shape has a fill or a stroke");
|
|
998
|
+
return {
|
|
999
|
+
...common,
|
|
1000
|
+
kind,
|
|
1001
|
+
shape,
|
|
1002
|
+
fill,
|
|
1003
|
+
stroke
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
const source = value["source"];
|
|
1007
|
+
if (typeof source !== "string" || source === "") invalid("malformed-command", "an image layer's source is a source id");
|
|
1008
|
+
const size = sourceSize(source);
|
|
1009
|
+
if (size === void 0) invalid("unknown-source", `unknown source "${source}"`);
|
|
1010
|
+
const crop = value["crop"] === null ? null : checkCrop(value["crop"]);
|
|
1011
|
+
if (crop !== null) checkCropInSource(crop, size);
|
|
1012
|
+
return {
|
|
1013
|
+
...common,
|
|
1014
|
+
kind: "image",
|
|
1015
|
+
source,
|
|
1016
|
+
crop
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
function patchLayer(item, props, sourceSize) {
|
|
1020
|
+
if (!isRecord(props)) invalid("malformed-command", "an update's props are an object");
|
|
1021
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
1022
|
+
...COMMON.slice(2),
|
|
1023
|
+
...BOX,
|
|
1024
|
+
...OWN[item.kind]
|
|
1025
|
+
]);
|
|
1026
|
+
const before = item;
|
|
1027
|
+
const merged = { ...item };
|
|
1028
|
+
const previous = {};
|
|
1029
|
+
for (const name of Object.keys(props)) {
|
|
1030
|
+
if (!allowed.has(name)) {
|
|
1031
|
+
if (ANY_OWN.has(name)) invalid("wrong-kind", `a ${item.kind} layer has no "${name}"`);
|
|
1032
|
+
invalid("malformed-command", `a layer's props have no field "${name}"`);
|
|
1033
|
+
}
|
|
1034
|
+
merged[name] = props[name];
|
|
1035
|
+
previous[name] = before[name];
|
|
1036
|
+
}
|
|
1037
|
+
return {
|
|
1038
|
+
item: checkLayer(merged, sourceSize),
|
|
1039
|
+
previous
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
// src/core/operations/commands.ts
|
|
1043
|
+
function checkOrientation(value) {
|
|
1044
|
+
if (!isRecord(value)) invalid("malformed-command", "an orientation is an object");
|
|
1045
|
+
allFields(value, [
|
|
1046
|
+
"rotate",
|
|
1047
|
+
"flipX",
|
|
1048
|
+
"flipY"
|
|
1049
|
+
], "an orientation");
|
|
1050
|
+
const { rotate, flipX, flipY } = value;
|
|
1051
|
+
if (typeof flipX !== "boolean" || typeof flipY !== "boolean") invalid("malformed-command", "an orientation's flips are booleans");
|
|
1052
|
+
if (![
|
|
1053
|
+
0,
|
|
1054
|
+
90,
|
|
1055
|
+
180,
|
|
1056
|
+
270
|
|
1057
|
+
].includes(rotate)) invalid("invalid-orientation", "a rotation is 0, 90, 180 or 270 degrees");
|
|
1058
|
+
return {
|
|
1059
|
+
rotate,
|
|
1060
|
+
flipX,
|
|
1061
|
+
flipY
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
function checkOutput(value) {
|
|
1065
|
+
if (!isRecord(value)) invalid("malformed-command", "an output is an object");
|
|
1066
|
+
if (value.kind === "scale") {
|
|
1067
|
+
allFields(value, ["kind", "scale"], "a scale output");
|
|
1068
|
+
if (typeof value.scale !== "number") invalid("malformed-command", "a scale is a number");
|
|
1069
|
+
if (!(Number.isFinite(value.scale) && value.scale > 0 && value.scale <= 1)) invalid("invalid-output", "an output scale is more than 0 and at most 1");
|
|
1070
|
+
return {
|
|
1071
|
+
kind: "scale",
|
|
1072
|
+
scale: value.scale
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
if (value.kind === "fit") {
|
|
1076
|
+
allFields(value, [
|
|
1077
|
+
"kind",
|
|
1078
|
+
"width",
|
|
1079
|
+
"height"
|
|
1080
|
+
], "a box output");
|
|
1081
|
+
const { width, height } = value;
|
|
1082
|
+
if (!Number.isInteger(width) || !Number.isInteger(height)) invalid("malformed-command", "a box's width and height are whole numbers");
|
|
1083
|
+
if (!isWhole(width, 1) || !isWhole(height, 1)) invalid("invalid-output", "an output box is at least 1 × 1 pixel");
|
|
1084
|
+
return {
|
|
1085
|
+
kind: "fit",
|
|
1086
|
+
width,
|
|
1087
|
+
height
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
return invalid("malformed-command", "an output is either a scale or a box to fit");
|
|
1091
|
+
}
|
|
1092
|
+
var METADATA_FIELDS = [
|
|
1093
|
+
"title",
|
|
1094
|
+
"description",
|
|
1095
|
+
"artist",
|
|
1096
|
+
"copyright"
|
|
1097
|
+
];
|
|
1098
|
+
function checkMetadataValue(field, value) {
|
|
1099
|
+
if (typeof value !== "string") invalid("malformed-command", `metadata.${field} is text`);
|
|
1100
|
+
if (longerThan(value, 2e3)) invalid("metadata-too-long", `metadata.${field} has more than ${MAX_METADATA_LENGTH} code points`);
|
|
1101
|
+
return value;
|
|
1102
|
+
}
|
|
1103
|
+
function checkMetadata(value) {
|
|
1104
|
+
if (!isRecord(value)) invalid("malformed-command", "metadata is an object");
|
|
1105
|
+
allFields(value, METADATA_FIELDS, "metadata");
|
|
1106
|
+
return Object.fromEntries(METADATA_FIELDS.map((field) => [field, checkMetadataValue(field, value[field])]));
|
|
1107
|
+
}
|
|
1108
|
+
var ADJUSTMENTS = DEVELOP_ADJUSTMENTS;
|
|
1109
|
+
var PRESETS = FILTER_PRESETS;
|
|
1110
|
+
function checkDevelop(value, all = false) {
|
|
1111
|
+
if (!isRecord(value)) invalid("malformed-command", "develop values are an object");
|
|
1112
|
+
const values = {};
|
|
1113
|
+
for (const [name, level] of Object.entries(value)) {
|
|
1114
|
+
if (!ADJUSTMENTS.includes(name)) invalid("unknown-adjustment", `"${name}" isn't a develop adjustment`);
|
|
1115
|
+
if (typeof level !== "number") invalid("malformed-command", `${name} is a number`);
|
|
1116
|
+
if (!isWhole(level, -100, 100)) invalid("invalid-level", `${name} is a whole number from -100 to 100`);
|
|
1117
|
+
values[name] = level;
|
|
1118
|
+
}
|
|
1119
|
+
if (all) allFields(value, ADJUSTMENTS, "develop");
|
|
1120
|
+
return Object.fromEntries(DEVELOP_ADJUSTMENTS.filter((name) => name in values).map((name) => [name, values[name]]));
|
|
1121
|
+
}
|
|
1122
|
+
function checkFilter(value) {
|
|
1123
|
+
if (value === null) return null;
|
|
1124
|
+
if (!isRecord(value)) invalid("malformed-command", "a filter is an object or null");
|
|
1125
|
+
allFields(value, ["preset", "strength"], "a filter");
|
|
1126
|
+
if (typeof value.preset !== "string") invalid("malformed-command", "a filter's preset is text");
|
|
1127
|
+
if (!PRESETS.includes(value.preset)) invalid("unknown-preset", `a filter's preset is one of ${FILTER_PRESETS.join(", ")}`);
|
|
1128
|
+
if (typeof value.strength !== "number") invalid("malformed-command", "a filter's strength is a number");
|
|
1129
|
+
if (!isWhole(value.strength, 0, 100)) invalid("invalid-strength", "a filter's strength is a whole number from 0 to 100");
|
|
1130
|
+
return {
|
|
1131
|
+
preset: value.preset,
|
|
1132
|
+
strength: value.strength
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
var COMMAND_FIELDS = {
|
|
1136
|
+
"image.setCrop": ["type", "crop"],
|
|
1137
|
+
"image.setOrientation": ["type", "orientation"],
|
|
1138
|
+
"image.setOutput": ["type", "output"],
|
|
1139
|
+
"image.setMetadata": [
|
|
1140
|
+
"type",
|
|
1141
|
+
"field",
|
|
1142
|
+
"value"
|
|
1143
|
+
],
|
|
1144
|
+
"image.setDevelop": ["type", "values"],
|
|
1145
|
+
"image.setFilter": ["type", "filter"],
|
|
1146
|
+
"image.insertLayers": [
|
|
1147
|
+
"type",
|
|
1148
|
+
"at",
|
|
1149
|
+
"layers"
|
|
1150
|
+
],
|
|
1151
|
+
"image.removeLayers": ["type", "keys"],
|
|
1152
|
+
"image.moveLayers": [
|
|
1153
|
+
"type",
|
|
1154
|
+
"keys",
|
|
1155
|
+
"to"
|
|
1156
|
+
],
|
|
1157
|
+
"image.updateLayers": ["type", "updates"],
|
|
1158
|
+
"image.restoreLayers": ["type", "entries"],
|
|
1159
|
+
"image.setLayerOrder": ["type", "keys"],
|
|
1160
|
+
"image.insertRedactions": [
|
|
1161
|
+
"type",
|
|
1162
|
+
"at",
|
|
1163
|
+
"redactions"
|
|
1164
|
+
],
|
|
1165
|
+
"image.removeRedactions": ["type", "keys"],
|
|
1166
|
+
"image.moveRedactions": [
|
|
1167
|
+
"type",
|
|
1168
|
+
"keys",
|
|
1169
|
+
"to"
|
|
1170
|
+
],
|
|
1171
|
+
"image.updateRedactions": ["type", "updates"],
|
|
1172
|
+
"image.restoreRedactions": ["type", "entries"],
|
|
1173
|
+
"image.setRedactionOrder": ["type", "keys"]
|
|
1174
|
+
};
|
|
1175
|
+
function planKeys(plan) {
|
|
1176
|
+
return new Set([...plan.layers, ...plan.redactions].map((item) => item.key));
|
|
1177
|
+
}
|
|
1178
|
+
function checkRoom(plan, added) {
|
|
1179
|
+
if (plan.layers.length + plan.redactions.length + added > 1024) invalid("too-many-layers", `a plan holds at most ${MAX_PLAN_ITEMS} layers and redactions together`);
|
|
1180
|
+
}
|
|
1181
|
+
function checkLayers(value, sourceSize) {
|
|
1182
|
+
if (!Array.isArray(value)) invalid("malformed-command", "layers is a list");
|
|
1183
|
+
if (value.length === 0) invalid("malformed-command", "layers has at least one item");
|
|
1184
|
+
return value.map((layer) => checkLayer(layer, sourceSize));
|
|
1185
|
+
}
|
|
1186
|
+
function checkLayerEntries(value, sourceSize) {
|
|
1187
|
+
if (!Array.isArray(value)) invalid("malformed-command", "entries is a list");
|
|
1188
|
+
if (value.length === 0) invalid("malformed-command", "entries has at least one item");
|
|
1189
|
+
return value.map((entry) => {
|
|
1190
|
+
if (!isRecord(entry)) invalid("malformed-command", "an entry is an object");
|
|
1191
|
+
allFields(entry, ["at", "layer"], "an entry");
|
|
1192
|
+
return {
|
|
1193
|
+
at: entry.at,
|
|
1194
|
+
item: checkLayer(entry.layer, sourceSize)
|
|
1195
|
+
};
|
|
1196
|
+
});
|
|
1197
|
+
}
|
|
1198
|
+
function checkRedactions(value) {
|
|
1199
|
+
if (!Array.isArray(value)) invalid("malformed-command", "redactions is a list");
|
|
1200
|
+
if (value.length === 0) invalid("malformed-command", "redactions has at least one item");
|
|
1201
|
+
return value.map(checkRedaction);
|
|
1202
|
+
}
|
|
1203
|
+
function checkRedactionEntries(value) {
|
|
1204
|
+
if (!Array.isArray(value)) invalid("malformed-command", "entries is a list");
|
|
1205
|
+
if (value.length === 0) invalid("malformed-command", "entries has at least one item");
|
|
1206
|
+
return value.map((entry) => {
|
|
1207
|
+
if (!isRecord(entry)) invalid("malformed-command", "an entry is an object");
|
|
1208
|
+
allFields(entry, ["at", "redaction"], "an entry");
|
|
1209
|
+
return {
|
|
1210
|
+
at: entry.at,
|
|
1211
|
+
item: checkRedaction(entry.redaction)
|
|
1212
|
+
};
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
function apply(plan, input, sourceSize) {
|
|
1216
|
+
if (!isRecord(input)) invalid("malformed-command", "a command is an object");
|
|
1217
|
+
const { type } = input;
|
|
1218
|
+
if (typeof type !== "string") invalid("malformed-command", "a command's type is text");
|
|
1219
|
+
if (!Object.hasOwn(COMMAND_FIELDS, type)) invalid("unknown-command", `"${type}" isn't a command`);
|
|
1220
|
+
const command = input;
|
|
1221
|
+
allFields(command, COMMAND_FIELDS[command.type], `${command.type}`);
|
|
1222
|
+
switch (command.type) {
|
|
1223
|
+
case "image.setCrop": {
|
|
1224
|
+
const crop = command.crop === null ? null : checkCrop(command.crop);
|
|
1225
|
+
if (crop !== null) {
|
|
1226
|
+
const size = sourceSize(plan.base.source);
|
|
1227
|
+
if (size === void 0) invalid("unknown-source", `unknown source "${plan.base.source}"`);
|
|
1228
|
+
checkCropInSource(crop, size);
|
|
1229
|
+
}
|
|
1230
|
+
return {
|
|
1231
|
+
state: {
|
|
1232
|
+
...plan,
|
|
1233
|
+
base: {
|
|
1234
|
+
...plan.base,
|
|
1235
|
+
crop
|
|
1236
|
+
}
|
|
1237
|
+
},
|
|
1238
|
+
inverse: {
|
|
1239
|
+
type: "image.setCrop",
|
|
1240
|
+
crop: plan.base.crop
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
case "image.setOrientation": {
|
|
1245
|
+
const orientation = checkOrientation(command.orientation);
|
|
1246
|
+
return {
|
|
1247
|
+
state: {
|
|
1248
|
+
...plan,
|
|
1249
|
+
base: {
|
|
1250
|
+
...plan.base,
|
|
1251
|
+
orientation
|
|
1252
|
+
}
|
|
1253
|
+
},
|
|
1254
|
+
inverse: {
|
|
1255
|
+
type: "image.setOrientation",
|
|
1256
|
+
orientation: plan.base.orientation
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
}
|
|
1260
|
+
case "image.setOutput": {
|
|
1261
|
+
const output = checkOutput(command.output);
|
|
1262
|
+
return {
|
|
1263
|
+
state: {
|
|
1264
|
+
...plan,
|
|
1265
|
+
output
|
|
1266
|
+
},
|
|
1267
|
+
inverse: {
|
|
1268
|
+
type: "image.setOutput",
|
|
1269
|
+
output: plan.output
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1273
|
+
case "image.setMetadata": {
|
|
1274
|
+
const { field } = command;
|
|
1275
|
+
if (typeof field !== "string") invalid("malformed-command", "a metadata field is text");
|
|
1276
|
+
if (!METADATA_FIELDS.includes(field)) invalid("unknown-metadata-field", "only title, description, artist and copyright are kept");
|
|
1277
|
+
const value = checkMetadataValue(field, command.value);
|
|
1278
|
+
return {
|
|
1279
|
+
state: {
|
|
1280
|
+
...plan,
|
|
1281
|
+
metadata: {
|
|
1282
|
+
...plan.metadata,
|
|
1283
|
+
[field]: value
|
|
1284
|
+
}
|
|
1285
|
+
},
|
|
1286
|
+
inverse: {
|
|
1287
|
+
type: "image.setMetadata",
|
|
1288
|
+
field,
|
|
1289
|
+
value: plan.metadata[field]
|
|
1290
|
+
}
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
case "image.setDevelop": {
|
|
1294
|
+
const values = checkDevelop(command.values);
|
|
1295
|
+
const previous = {};
|
|
1296
|
+
for (const name of Object.keys(values)) previous[name] = plan.base.develop[name];
|
|
1297
|
+
return {
|
|
1298
|
+
state: {
|
|
1299
|
+
...plan,
|
|
1300
|
+
base: {
|
|
1301
|
+
...plan.base,
|
|
1302
|
+
develop: {
|
|
1303
|
+
...plan.base.develop,
|
|
1304
|
+
...values
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
},
|
|
1308
|
+
inverse: {
|
|
1309
|
+
type: "image.setDevelop",
|
|
1310
|
+
values: previous
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
}
|
|
1314
|
+
case "image.setFilter": {
|
|
1315
|
+
const filter = checkFilter(command.filter);
|
|
1316
|
+
return {
|
|
1317
|
+
state: {
|
|
1318
|
+
...plan,
|
|
1319
|
+
base: {
|
|
1320
|
+
...plan.base,
|
|
1321
|
+
filter
|
|
1322
|
+
}
|
|
1323
|
+
},
|
|
1324
|
+
inverse: {
|
|
1325
|
+
type: "image.setFilter",
|
|
1326
|
+
filter: plan.base.filter
|
|
1327
|
+
}
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
case "image.insertLayers": {
|
|
1331
|
+
const layers = checkLayers(command.layers, sourceSize);
|
|
1332
|
+
const { list, keys } = insertItems(plan.layers, command.at, layers, planKeys(plan));
|
|
1333
|
+
checkRoom(plan, layers.length);
|
|
1334
|
+
return {
|
|
1335
|
+
state: {
|
|
1336
|
+
...plan,
|
|
1337
|
+
layers: list
|
|
1338
|
+
},
|
|
1339
|
+
inverse: {
|
|
1340
|
+
type: "image.removeLayers",
|
|
1341
|
+
keys
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
case "image.removeLayers": {
|
|
1346
|
+
const { list, entries } = removeItems(plan.layers, command.keys, LAYER_NAMES);
|
|
1347
|
+
return {
|
|
1348
|
+
state: {
|
|
1349
|
+
...plan,
|
|
1350
|
+
layers: list
|
|
1351
|
+
},
|
|
1352
|
+
inverse: {
|
|
1353
|
+
type: "image.restoreLayers",
|
|
1354
|
+
entries: entries.map(({ at, item }) => ({
|
|
1355
|
+
at,
|
|
1356
|
+
layer: item
|
|
1357
|
+
}))
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
case "image.restoreLayers": {
|
|
1362
|
+
const entries = checkLayerEntries(command.entries, sourceSize);
|
|
1363
|
+
const { list, keys } = restoreItems(plan.layers, entries, planKeys(plan));
|
|
1364
|
+
checkRoom(plan, entries.length);
|
|
1365
|
+
return {
|
|
1366
|
+
state: {
|
|
1367
|
+
...plan,
|
|
1368
|
+
layers: list
|
|
1369
|
+
},
|
|
1370
|
+
inverse: {
|
|
1371
|
+
type: "image.removeLayers",
|
|
1372
|
+
keys
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
case "image.moveLayers": {
|
|
1377
|
+
const { list, order } = moveItems(plan.layers, command.keys, command.to, LAYER_NAMES);
|
|
1378
|
+
return {
|
|
1379
|
+
state: {
|
|
1380
|
+
...plan,
|
|
1381
|
+
layers: list
|
|
1382
|
+
},
|
|
1383
|
+
inverse: {
|
|
1384
|
+
type: "image.setLayerOrder",
|
|
1385
|
+
keys: order
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1388
|
+
}
|
|
1389
|
+
case "image.setLayerOrder": {
|
|
1390
|
+
const { list, order } = orderItems(plan.layers, command.keys, LAYER_NAMES);
|
|
1391
|
+
return {
|
|
1392
|
+
state: {
|
|
1393
|
+
...plan,
|
|
1394
|
+
layers: list
|
|
1395
|
+
},
|
|
1396
|
+
inverse: {
|
|
1397
|
+
type: "image.setLayerOrder",
|
|
1398
|
+
keys: order
|
|
1399
|
+
}
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
case "image.updateLayers": {
|
|
1403
|
+
const { list, previous } = updateItems(plan.layers, command.updates, LAYER_NAMES, (item, props) => patchLayer(item, props, sourceSize));
|
|
1404
|
+
return {
|
|
1405
|
+
state: {
|
|
1406
|
+
...plan,
|
|
1407
|
+
layers: list
|
|
1408
|
+
},
|
|
1409
|
+
inverse: {
|
|
1410
|
+
type: "image.updateLayers",
|
|
1411
|
+
updates: previous
|
|
1412
|
+
}
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
case "image.insertRedactions": {
|
|
1416
|
+
const redactions = checkRedactions(command.redactions);
|
|
1417
|
+
const { list, keys } = insertItems(plan.redactions, command.at, redactions, planKeys(plan));
|
|
1418
|
+
checkRoom(plan, redactions.length);
|
|
1419
|
+
return {
|
|
1420
|
+
state: {
|
|
1421
|
+
...plan,
|
|
1422
|
+
redactions: list
|
|
1423
|
+
},
|
|
1424
|
+
inverse: {
|
|
1425
|
+
type: "image.removeRedactions",
|
|
1426
|
+
keys
|
|
1427
|
+
}
|
|
1428
|
+
};
|
|
1429
|
+
}
|
|
1430
|
+
case "image.removeRedactions": {
|
|
1431
|
+
const { list, entries } = removeItems(plan.redactions, command.keys, REDACTION_NAMES);
|
|
1432
|
+
return {
|
|
1433
|
+
state: {
|
|
1434
|
+
...plan,
|
|
1435
|
+
redactions: list
|
|
1436
|
+
},
|
|
1437
|
+
inverse: {
|
|
1438
|
+
type: "image.restoreRedactions",
|
|
1439
|
+
entries: entries.map(({ at, item }) => ({
|
|
1440
|
+
at,
|
|
1441
|
+
redaction: item
|
|
1442
|
+
}))
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
}
|
|
1446
|
+
case "image.restoreRedactions": {
|
|
1447
|
+
const entries = checkRedactionEntries(command.entries);
|
|
1448
|
+
const { list, keys } = restoreItems(plan.redactions, entries, planKeys(plan));
|
|
1449
|
+
checkRoom(plan, entries.length);
|
|
1450
|
+
return {
|
|
1451
|
+
state: {
|
|
1452
|
+
...plan,
|
|
1453
|
+
redactions: list
|
|
1454
|
+
},
|
|
1455
|
+
inverse: {
|
|
1456
|
+
type: "image.removeRedactions",
|
|
1457
|
+
keys
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
case "image.moveRedactions": {
|
|
1462
|
+
const { list, order } = moveItems(plan.redactions, command.keys, command.to, REDACTION_NAMES);
|
|
1463
|
+
return {
|
|
1464
|
+
state: {
|
|
1465
|
+
...plan,
|
|
1466
|
+
redactions: list
|
|
1467
|
+
},
|
|
1468
|
+
inverse: {
|
|
1469
|
+
type: "image.setRedactionOrder",
|
|
1470
|
+
keys: order
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
case "image.setRedactionOrder": {
|
|
1475
|
+
const { list, order } = orderItems(plan.redactions, command.keys, REDACTION_NAMES);
|
|
1476
|
+
return {
|
|
1477
|
+
state: {
|
|
1478
|
+
...plan,
|
|
1479
|
+
redactions: list
|
|
1480
|
+
},
|
|
1481
|
+
inverse: {
|
|
1482
|
+
type: "image.setRedactionOrder",
|
|
1483
|
+
keys: order
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
case "image.updateRedactions": {
|
|
1488
|
+
const { list, previous } = updateItems(plan.redactions, command.updates, REDACTION_NAMES, patchRedaction);
|
|
1489
|
+
return {
|
|
1490
|
+
state: {
|
|
1491
|
+
...plan,
|
|
1492
|
+
redactions: list
|
|
1493
|
+
},
|
|
1494
|
+
inverse: {
|
|
1495
|
+
type: "image.updateRedactions",
|
|
1496
|
+
updates: previous
|
|
1497
|
+
}
|
|
1498
|
+
};
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
function createImageReducer(sourceSize) {
|
|
1503
|
+
return (plan, command) => {
|
|
1504
|
+
try {
|
|
1505
|
+
return apply(plan, command, sourceSize);
|
|
1506
|
+
} catch (error) {
|
|
1507
|
+
if (!(error instanceof Invalid)) throw error;
|
|
1508
|
+
throw new ImageError("INVALID_COMMAND", `Invalid command: ${error.message}.`, { details: { reason: error.reason } });
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
// src/core/model/geometry.ts
|
|
1513
|
+
function orientedSize(stored, orientation) {
|
|
1514
|
+
return orientation >= 5 ? {
|
|
1515
|
+
width: stored.height,
|
|
1516
|
+
height: stored.width
|
|
1517
|
+
} : stored;
|
|
1518
|
+
}
|
|
1519
|
+
function cropRect(crop, source) {
|
|
1520
|
+
return crop ?? {
|
|
1521
|
+
x: 0,
|
|
1522
|
+
y: 0,
|
|
1523
|
+
width: source.width,
|
|
1524
|
+
height: source.height
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
function compositionSize(plan, source) {
|
|
1528
|
+
const { width, height } = cropRect(plan.base.crop, source);
|
|
1529
|
+
return plan.base.orientation.rotate % 180 !== 0 ? {
|
|
1530
|
+
width: height,
|
|
1531
|
+
height: width
|
|
1532
|
+
} : {
|
|
1533
|
+
width,
|
|
1534
|
+
height
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
function outputSize(output, composition) {
|
|
1538
|
+
const factor = output.kind === "scale" ? output.scale : Math.min(1, output.width / composition.width, output.height / composition.height);
|
|
1539
|
+
return {
|
|
1540
|
+
width: Math.max(1, Math.round(composition.width * factor)),
|
|
1541
|
+
height: Math.max(1, Math.round(composition.height * factor))
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
function sourceToComposition(point, crop, orientation) {
|
|
1545
|
+
const x = point.x - crop.x;
|
|
1546
|
+
const y = point.y - crop.y;
|
|
1547
|
+
const { width, height } = crop;
|
|
1548
|
+
let turned;
|
|
1549
|
+
switch (orientation.rotate) {
|
|
1550
|
+
case 90:
|
|
1551
|
+
turned = {
|
|
1552
|
+
x: height - y,
|
|
1553
|
+
y: x
|
|
1554
|
+
};
|
|
1555
|
+
break;
|
|
1556
|
+
case 180:
|
|
1557
|
+
turned = {
|
|
1558
|
+
x: width - x,
|
|
1559
|
+
y: height - y
|
|
1560
|
+
};
|
|
1561
|
+
break;
|
|
1562
|
+
case 270:
|
|
1563
|
+
turned = {
|
|
1564
|
+
x: y,
|
|
1565
|
+
y: width - x
|
|
1566
|
+
};
|
|
1567
|
+
break;
|
|
1568
|
+
default: turned = {
|
|
1569
|
+
x,
|
|
1570
|
+
y
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
const size = orientation.rotate % 180 === 0 ? {
|
|
1574
|
+
width,
|
|
1575
|
+
height
|
|
1576
|
+
} : {
|
|
1577
|
+
width: height,
|
|
1578
|
+
height: width
|
|
1579
|
+
};
|
|
1580
|
+
return {
|
|
1581
|
+
x: orientation.flipX ? size.width - turned.x : turned.x,
|
|
1582
|
+
y: orientation.flipY ? size.height - turned.y : turned.y
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
function compositionToSource(point, crop, orientation) {
|
|
1586
|
+
const { width, height } = crop;
|
|
1587
|
+
const size = orientation.rotate % 180 === 0 ? {
|
|
1588
|
+
width,
|
|
1589
|
+
height
|
|
1590
|
+
} : {
|
|
1591
|
+
width: height,
|
|
1592
|
+
height: width
|
|
1593
|
+
};
|
|
1594
|
+
const x = orientation.flipX ? size.width - point.x : point.x;
|
|
1595
|
+
const y = orientation.flipY ? size.height - point.y : point.y;
|
|
1596
|
+
let source;
|
|
1597
|
+
switch (orientation.rotate) {
|
|
1598
|
+
case 90:
|
|
1599
|
+
source = {
|
|
1600
|
+
x: y,
|
|
1601
|
+
y: height - x
|
|
1602
|
+
};
|
|
1603
|
+
break;
|
|
1604
|
+
case 180:
|
|
1605
|
+
source = {
|
|
1606
|
+
x: width - x,
|
|
1607
|
+
y: height - y
|
|
1608
|
+
};
|
|
1609
|
+
break;
|
|
1610
|
+
case 270:
|
|
1611
|
+
source = {
|
|
1612
|
+
x: width - y,
|
|
1613
|
+
y: x
|
|
1614
|
+
};
|
|
1615
|
+
break;
|
|
1616
|
+
default: source = {
|
|
1617
|
+
x,
|
|
1618
|
+
y
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
return {
|
|
1622
|
+
x: source.x + crop.x,
|
|
1623
|
+
y: source.y + crop.y
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1626
|
+
// src/core/model/crop.ts
|
|
1627
|
+
var ASPECT_PRESETS = [
|
|
1628
|
+
"free",
|
|
1629
|
+
"original",
|
|
1630
|
+
[1, 1],
|
|
1631
|
+
[4, 3],
|
|
1632
|
+
[3, 2],
|
|
1633
|
+
[16, 9],
|
|
1634
|
+
[3, 4],
|
|
1635
|
+
[2, 3],
|
|
1636
|
+
[9, 16]
|
|
1637
|
+
];
|
|
1638
|
+
function aspectRatio(aspect, source) {
|
|
1639
|
+
if (aspect === "free") return void 0;
|
|
1640
|
+
if (aspect === "original") return source.width / source.height;
|
|
1641
|
+
return aspect[0] / aspect[1];
|
|
1642
|
+
}
|
|
1643
|
+
var wholeCrop = (source) => ({
|
|
1644
|
+
x: 0,
|
|
1645
|
+
y: 0,
|
|
1646
|
+
width: source.width,
|
|
1647
|
+
height: source.height
|
|
1648
|
+
});
|
|
1649
|
+
function fitAspect(within, ratio) {
|
|
1650
|
+
if (ratio === void 0) return within;
|
|
1651
|
+
let width = within.width;
|
|
1652
|
+
let height = Math.round(width / ratio);
|
|
1653
|
+
if (height > within.height) {
|
|
1654
|
+
height = within.height;
|
|
1655
|
+
width = Math.round(height * ratio);
|
|
1656
|
+
}
|
|
1657
|
+
width = Math.max(1, Math.min(within.width, width));
|
|
1658
|
+
height = Math.max(1, Math.min(within.height, height));
|
|
1659
|
+
return {
|
|
1660
|
+
x: within.x + Math.floor((within.width - width) / 2),
|
|
1661
|
+
y: within.y + Math.floor((within.height - height) / 2),
|
|
1662
|
+
width,
|
|
1663
|
+
height
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
function moveCrop(crop, dx, dy, source) {
|
|
1667
|
+
return {
|
|
1668
|
+
...crop,
|
|
1669
|
+
x: Math.min(source.width - crop.width, Math.max(0, crop.x + Math.round(dx))),
|
|
1670
|
+
y: Math.min(source.height - crop.height, Math.max(0, crop.y + Math.round(dy)))
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
function dragHandle(crop, handle, dx, dy, source, ratio) {
|
|
1674
|
+
let left = crop.x;
|
|
1675
|
+
let top = crop.y;
|
|
1676
|
+
let right = crop.x + crop.width;
|
|
1677
|
+
let bottom = crop.y + crop.height;
|
|
1678
|
+
if (handle.includes("w")) left = Math.min(right - 1, Math.max(0, left + dx));
|
|
1679
|
+
if (handle.includes("e")) right = Math.max(left + 1, Math.min(source.width, right + dx));
|
|
1680
|
+
if (handle.includes("n")) top = Math.min(bottom - 1, Math.max(0, top + dy));
|
|
1681
|
+
if (handle.includes("s")) bottom = Math.max(top + 1, Math.min(source.height, bottom + dy));
|
|
1682
|
+
left = Math.round(left);
|
|
1683
|
+
right = Math.round(right);
|
|
1684
|
+
top = Math.round(top);
|
|
1685
|
+
bottom = Math.round(bottom);
|
|
1686
|
+
if (ratio === void 0) return {
|
|
1687
|
+
x: left,
|
|
1688
|
+
y: top,
|
|
1689
|
+
width: right - left,
|
|
1690
|
+
height: bottom - top
|
|
1691
|
+
};
|
|
1692
|
+
const horizontal = handle.includes("e") || handle.includes("w");
|
|
1693
|
+
const vertical = handle.includes("n") || handle.includes("s");
|
|
1694
|
+
let width = right - left;
|
|
1695
|
+
let height = bottom - top;
|
|
1696
|
+
if (horizontal && !vertical) height = width / ratio;
|
|
1697
|
+
else if (vertical && !horizontal) width = height * ratio;
|
|
1698
|
+
else if (width / height > ratio) width = height * ratio;
|
|
1699
|
+
else height = width / ratio;
|
|
1700
|
+
const roomRight = handle.includes("w") ? right : source.width - left;
|
|
1701
|
+
const roomDown = handle.includes("n") ? bottom : source.height - top;
|
|
1702
|
+
const scale = Math.min(1, roomRight / width, roomDown / height);
|
|
1703
|
+
width = Math.max(1, Math.round(width * scale));
|
|
1704
|
+
height = Math.max(1, Math.round(height * scale));
|
|
1705
|
+
const x = handle.includes("w") ? right - width : left;
|
|
1706
|
+
let y = handle.includes("n") ? bottom - height : top;
|
|
1707
|
+
let cx = x;
|
|
1708
|
+
if (vertical && !horizontal) cx = Math.round(crop.x + (crop.width - width) / 2);
|
|
1709
|
+
if (horizontal && !vertical) y = Math.round(crop.y + (crop.height - height) / 2);
|
|
1710
|
+
return {
|
|
1711
|
+
x: Math.min(source.width - width, Math.max(0, cx)),
|
|
1712
|
+
y: Math.min(source.height - height, Math.max(0, y)),
|
|
1713
|
+
width,
|
|
1714
|
+
height
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
function cropInComposition(crop, orientation, source) {
|
|
1718
|
+
const whole = wholeCrop(source);
|
|
1719
|
+
const a = sourceToComposition({
|
|
1720
|
+
x: crop.x,
|
|
1721
|
+
y: crop.y
|
|
1722
|
+
}, whole, orientation);
|
|
1723
|
+
const b = sourceToComposition({
|
|
1724
|
+
x: crop.x + crop.width,
|
|
1725
|
+
y: crop.y + crop.height
|
|
1726
|
+
}, whole, orientation);
|
|
1727
|
+
return {
|
|
1728
|
+
x: Math.min(a.x, b.x),
|
|
1729
|
+
y: Math.min(a.y, b.y),
|
|
1730
|
+
width: Math.abs(b.x - a.x),
|
|
1731
|
+
height: Math.abs(b.y - a.y)
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
function compositionDeltaToSource(dx, dy, orientation, source) {
|
|
1735
|
+
const whole = wholeCrop(source);
|
|
1736
|
+
const origin = compositionToSource({
|
|
1737
|
+
x: 0,
|
|
1738
|
+
y: 0
|
|
1739
|
+
}, whole, orientation);
|
|
1740
|
+
const moved = compositionToSource({
|
|
1741
|
+
x: dx,
|
|
1742
|
+
y: dy
|
|
1743
|
+
}, whole, orientation);
|
|
1744
|
+
return {
|
|
1745
|
+
dx: moved.x - origin.x,
|
|
1746
|
+
dy: moved.y - origin.y
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
function handleInSource(handle, orientation) {
|
|
1750
|
+
const vector = {
|
|
1751
|
+
x: 0,
|
|
1752
|
+
y: 0
|
|
1753
|
+
};
|
|
1754
|
+
if (handle.includes("e")) vector.x = 1;
|
|
1755
|
+
if (handle.includes("w")) vector.x = -1;
|
|
1756
|
+
if (handle.includes("s")) vector.y = 1;
|
|
1757
|
+
if (handle.includes("n")) vector.y = -1;
|
|
1758
|
+
const unit = compositionDeltaToSource(vector.x, vector.y, orientation, {
|
|
1759
|
+
width: 2,
|
|
1760
|
+
height: 2
|
|
1761
|
+
});
|
|
1762
|
+
return `${unit.dy > 0 ? "s" : unit.dy < 0 ? "n" : ""}${unit.dx > 0 ? "e" : unit.dx < 0 ? "w" : ""}`;
|
|
1763
|
+
}
|
|
1764
|
+
function nudgeCropOnScreen(crop, key, step, resize, orientation, source, ratio) {
|
|
1765
|
+
const dx = key === "ArrowLeft" ? -step : key === "ArrowRight" ? step : 0;
|
|
1766
|
+
const moved = compositionDeltaToSource(dx, key === "ArrowUp" ? -step : key === "ArrowDown" ? step : 0, orientation, source);
|
|
1767
|
+
if (!resize) return moveCrop(crop, moved.dx, moved.dy, source);
|
|
1768
|
+
return dragHandle(crop, handleInSource(dx === 0 ? "s" : "e", orientation), moved.dx, moved.dy, source, ratio);
|
|
1769
|
+
}
|
|
1770
|
+
// src/core/model/keys.ts
|
|
1771
|
+
function createImageKey(used) {
|
|
1772
|
+
for (;;) {
|
|
1773
|
+
const bytes = globalThis.crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(12));
|
|
1774
|
+
const key = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1775
|
+
if (!used?.has(key)) return key;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
// src/core/model/layer-geometry.ts
|
|
1779
|
+
function then(first, second) {
|
|
1780
|
+
const [a1, b1, c1, d1, e1, f1] = first;
|
|
1781
|
+
const [a2, b2, c2, d2, e2, f2] = second;
|
|
1782
|
+
return [
|
|
1783
|
+
a2 * a1 + c2 * b1,
|
|
1784
|
+
b2 * a1 + d2 * b1,
|
|
1785
|
+
a2 * c1 + c2 * d1,
|
|
1786
|
+
b2 * c1 + d2 * d1,
|
|
1787
|
+
a2 * e1 + c2 * f1 + e2,
|
|
1788
|
+
b2 * e1 + d2 * f1 + f2
|
|
1789
|
+
];
|
|
1790
|
+
}
|
|
1791
|
+
function sceneToOutput(crop, orientation, output) {
|
|
1792
|
+
const composition = orientation.rotate % 180 !== 0 ? {
|
|
1793
|
+
width: crop.height,
|
|
1794
|
+
height: crop.width
|
|
1795
|
+
} : {
|
|
1796
|
+
width: crop.width,
|
|
1797
|
+
height: crop.height
|
|
1798
|
+
};
|
|
1799
|
+
const scaleX = output.width / composition.width;
|
|
1800
|
+
const scaleY = output.height / composition.height;
|
|
1801
|
+
const at = (x, y) => {
|
|
1802
|
+
const point = sourceToComposition({
|
|
1803
|
+
x,
|
|
1804
|
+
y
|
|
1805
|
+
}, crop, orientation);
|
|
1806
|
+
return {
|
|
1807
|
+
x: point.x * scaleX,
|
|
1808
|
+
y: point.y * scaleY
|
|
1809
|
+
};
|
|
1810
|
+
};
|
|
1811
|
+
const origin = at(0, 0);
|
|
1812
|
+
const alongX = at(1, 0);
|
|
1813
|
+
const alongY = at(0, 1);
|
|
1814
|
+
return [
|
|
1815
|
+
alongX.x - origin.x,
|
|
1816
|
+
alongX.y - origin.y,
|
|
1817
|
+
alongY.x - origin.x,
|
|
1818
|
+
alongY.y - origin.y,
|
|
1819
|
+
origin.x,
|
|
1820
|
+
origin.y
|
|
1821
|
+
];
|
|
1822
|
+
}
|
|
1823
|
+
function layerToScene(box) {
|
|
1824
|
+
const radians = box.rotation * Math.PI / 1800;
|
|
1825
|
+
const quarter = box.rotation % 900 === 0 ? box.rotation / 900 % 4 : -1;
|
|
1826
|
+
const cos = quarter >= 0 ? [
|
|
1827
|
+
1,
|
|
1828
|
+
0,
|
|
1829
|
+
-1,
|
|
1830
|
+
0
|
|
1831
|
+
][quarter] : Math.cos(radians);
|
|
1832
|
+
const sin = quarter >= 0 ? [
|
|
1833
|
+
0,
|
|
1834
|
+
1,
|
|
1835
|
+
0,
|
|
1836
|
+
-1
|
|
1837
|
+
][quarter] : Math.sin(radians);
|
|
1838
|
+
const negative = (value) => value === 0 ? 0 : -value;
|
|
1839
|
+
const a = box.flipX ? negative(cos) : cos;
|
|
1840
|
+
const b = box.flipX ? negative(sin) : sin;
|
|
1841
|
+
const c = negative(sin);
|
|
1842
|
+
const d = cos;
|
|
1843
|
+
return [
|
|
1844
|
+
a,
|
|
1845
|
+
b,
|
|
1846
|
+
c,
|
|
1847
|
+
d,
|
|
1848
|
+
box.x + box.width / 2 - (a * box.width + c * box.height) / 2,
|
|
1849
|
+
box.y + box.height / 2 - (b * box.width + d * box.height) / 2
|
|
1850
|
+
];
|
|
1851
|
+
}
|
|
1852
|
+
function layerToOutput(box, crop, orientation, output) {
|
|
1853
|
+
return then(layerToScene(box), sceneToOutput(crop, orientation, output));
|
|
1854
|
+
}
|
|
1855
|
+
function invertMatrix(matrix) {
|
|
1856
|
+
const [a, b, c, d, e, f] = matrix;
|
|
1857
|
+
const determinant = a * d - b * c;
|
|
1858
|
+
return [
|
|
1859
|
+
d / determinant,
|
|
1860
|
+
-b / determinant,
|
|
1861
|
+
-c / determinant,
|
|
1862
|
+
a / determinant,
|
|
1863
|
+
(c * f - d * e) / determinant,
|
|
1864
|
+
(b * e - a * f) / determinant
|
|
1865
|
+
];
|
|
1866
|
+
}
|
|
1867
|
+
function sceneToLayerSource(box, crop) {
|
|
1868
|
+
const boxToSource = [
|
|
1869
|
+
crop.width / box.width,
|
|
1870
|
+
0,
|
|
1871
|
+
0,
|
|
1872
|
+
crop.height / box.height,
|
|
1873
|
+
crop.x,
|
|
1874
|
+
crop.y
|
|
1875
|
+
];
|
|
1876
|
+
return then(invertMatrix(layerToScene(box)), boxToSource);
|
|
1877
|
+
}
|
|
1878
|
+
function applyMatrix(matrix, point) {
|
|
1879
|
+
const [a, b, c, d, e, f] = matrix;
|
|
1880
|
+
return {
|
|
1881
|
+
x: a * point.x + c * point.y + e,
|
|
1882
|
+
y: b * point.x + d * point.y + f
|
|
1883
|
+
};
|
|
1884
|
+
}
|
|
1885
|
+
function fitTextLines(lines, lineHeight, ascent, box, align, rightToLeft) {
|
|
1886
|
+
const widest = Math.max(0, ...lines.map((line) => line.width));
|
|
1887
|
+
if (!(widest > 0) || !(lineHeight > 0)) return null;
|
|
1888
|
+
const scale = Math.min(box.width / widest, box.height / (lineHeight * lines.length));
|
|
1889
|
+
const top = (box.height - lineHeight * lines.length * scale) / 2;
|
|
1890
|
+
const side = align === "center" ? .5 : align === "start" === rightToLeft ? 1 : 0;
|
|
1891
|
+
return {
|
|
1892
|
+
scale,
|
|
1893
|
+
lines: lines.map((line, index) => ({
|
|
1894
|
+
x: (box.width - line.width * scale) * side,
|
|
1895
|
+
baseline: top + (index * lineHeight + ascent) * scale
|
|
1896
|
+
}))
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1899
|
+
function isRightToLeft(text) {
|
|
1900
|
+
for (const character of text) {
|
|
1901
|
+
if (RIGHT_TO_LEFT.test(character)) return true;
|
|
1902
|
+
if (LEFT_TO_RIGHT.test(character)) return false;
|
|
1903
|
+
}
|
|
1904
|
+
return false;
|
|
1905
|
+
}
|
|
1906
|
+
var RIGHT_TO_LEFT = /[-ࣿיִ-﷿ﹰ-\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;
|
|
1907
|
+
var LEFT_TO_RIGHT = /\p{L}/u;
|
|
1908
|
+
// src/core/model/redaction-geometry.ts
|
|
1909
|
+
var MIN_BLOCK = 8;
|
|
1910
|
+
var MAX_BLOCKS = 12;
|
|
1911
|
+
function redactionBlockSize(region) {
|
|
1912
|
+
return Math.max(MIN_BLOCK, Math.ceil(Math.max(region.width, region.height) / MAX_BLOCKS));
|
|
1913
|
+
}
|
|
1914
|
+
function boundaries(start, length, block) {
|
|
1915
|
+
const edges = [start];
|
|
1916
|
+
for (let at = start + block; at < start + length; at += block) edges.push(at);
|
|
1917
|
+
edges.push(start + length);
|
|
1918
|
+
return edges;
|
|
1919
|
+
}
|
|
1920
|
+
function cells(xs, ys) {
|
|
1921
|
+
const rects = [];
|
|
1922
|
+
for (let row = 0; row + 1 < ys.length; row += 1) {
|
|
1923
|
+
const y = ys[row];
|
|
1924
|
+
const height = ys[row + 1] - y;
|
|
1925
|
+
if (height <= 0) continue;
|
|
1926
|
+
for (let column = 0; column + 1 < xs.length; column += 1) {
|
|
1927
|
+
const x = xs[column];
|
|
1928
|
+
const width = xs[column + 1] - x;
|
|
1929
|
+
if (width > 0) rects.push({
|
|
1930
|
+
x,
|
|
1931
|
+
y,
|
|
1932
|
+
width,
|
|
1933
|
+
height
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
return rects;
|
|
1938
|
+
}
|
|
1939
|
+
var clamp$1 = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
1940
|
+
function redactionBlocksInScene(region, within) {
|
|
1941
|
+
const block = redactionBlockSize(region);
|
|
1942
|
+
const clip = (edges, min, max) => edges.map((edge) => clamp$1(edge, min, max));
|
|
1943
|
+
return cells(clip(boundaries(region.x, region.width, block), within.x, within.x + within.width), clip(boundaries(region.y, region.height, block), within.y, within.y + within.height));
|
|
1944
|
+
}
|
|
1945
|
+
function toOutput(crop, orientation, output) {
|
|
1946
|
+
const turned = orientation.rotate % 180 !== 0;
|
|
1947
|
+
const composition = turned ? {
|
|
1948
|
+
width: crop.height,
|
|
1949
|
+
height: crop.width
|
|
1950
|
+
} : {
|
|
1951
|
+
width: crop.width,
|
|
1952
|
+
height: crop.height
|
|
1953
|
+
};
|
|
1954
|
+
const scaleX = output.width / composition.width;
|
|
1955
|
+
const scaleY = output.height / composition.height;
|
|
1956
|
+
const at = (x, y) => {
|
|
1957
|
+
const point = sourceToComposition({
|
|
1958
|
+
x,
|
|
1959
|
+
y
|
|
1960
|
+
}, crop, orientation);
|
|
1961
|
+
return {
|
|
1962
|
+
x: point.x * scaleX,
|
|
1963
|
+
y: point.y * scaleY
|
|
1964
|
+
};
|
|
1965
|
+
};
|
|
1966
|
+
return {
|
|
1967
|
+
turned,
|
|
1968
|
+
fromX: (value) => turned ? at(value, crop.y).y : at(value, crop.y).x,
|
|
1969
|
+
fromY: (value) => turned ? at(crop.x, value).x : at(crop.x, value).y
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
function outputEdges(values, size) {
|
|
1973
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
1974
|
+
const last = sorted.length - 1;
|
|
1975
|
+
let previous = 0;
|
|
1976
|
+
return sorted.map((value, index) => {
|
|
1977
|
+
previous = Math.max(previous, clamp$1(index === 0 ? Math.floor(value) : index === last ? Math.ceil(value) : Math.round(value), 0, size));
|
|
1978
|
+
return previous;
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
function redactionRectInOutput(region, crop, orientation, output) {
|
|
1982
|
+
const { turned, fromX, fromY } = toOutput(crop, orientation, output);
|
|
1983
|
+
const alongX = [fromX(region.x), fromX(region.x + region.width)];
|
|
1984
|
+
const alongY = [fromY(region.y), fromY(region.y + region.height)];
|
|
1985
|
+
const [left, right] = outputEdges(turned ? alongY : alongX, output.width);
|
|
1986
|
+
const [top, bottom] = outputEdges(turned ? alongX : alongY, output.height);
|
|
1987
|
+
if (right <= left || bottom <= top) return null;
|
|
1988
|
+
return {
|
|
1989
|
+
x: left,
|
|
1990
|
+
y: top,
|
|
1991
|
+
width: right - left,
|
|
1992
|
+
height: bottom - top
|
|
1993
|
+
};
|
|
1994
|
+
}
|
|
1995
|
+
function redactionBlocksInOutput(region, crop, orientation, output) {
|
|
1996
|
+
const block = redactionBlockSize(region);
|
|
1997
|
+
const { turned, fromX, fromY } = toOutput(crop, orientation, output);
|
|
1998
|
+
const alongX = boundaries(region.x, region.width, block).map(fromX);
|
|
1999
|
+
const alongY = boundaries(region.y, region.height, block).map(fromY);
|
|
2000
|
+
return cells(outputEdges(turned ? alongY : alongX, output.width), outputEdges(turned ? alongX : alongY, output.height));
|
|
2001
|
+
}
|
|
2002
|
+
function between(a, b) {
|
|
2003
|
+
const x = Math.min(a.x, b.x);
|
|
2004
|
+
const y = Math.min(a.y, b.y);
|
|
2005
|
+
return {
|
|
2006
|
+
x,
|
|
2007
|
+
y,
|
|
2008
|
+
width: Math.max(a.x, b.x) - x,
|
|
2009
|
+
height: Math.max(a.y, b.y) - y
|
|
2010
|
+
};
|
|
2011
|
+
}
|
|
2012
|
+
function sceneRectToComposition(rect, crop, orientation) {
|
|
2013
|
+
return between(sourceToComposition({
|
|
2014
|
+
x: rect.x,
|
|
2015
|
+
y: rect.y
|
|
2016
|
+
}, crop, orientation), sourceToComposition({
|
|
2017
|
+
x: rect.x + rect.width,
|
|
2018
|
+
y: rect.y + rect.height
|
|
2019
|
+
}, crop, orientation));
|
|
2020
|
+
}
|
|
2021
|
+
function compositionRectToScene(rect, crop, orientation) {
|
|
2022
|
+
return between(compositionToSource({
|
|
2023
|
+
x: rect.x,
|
|
2024
|
+
y: rect.y
|
|
2025
|
+
}, crop, orientation), compositionToSource({
|
|
2026
|
+
x: rect.x + rect.width,
|
|
2027
|
+
y: rect.y + rect.height
|
|
2028
|
+
}, crop, orientation));
|
|
2029
|
+
}
|
|
2030
|
+
function fitRedactionRegion(rect) {
|
|
2031
|
+
const limit = MAX_SCENE_COORDINATE;
|
|
2032
|
+
const x = clamp$1(Math.floor(rect.x), -limit, limit - 1);
|
|
2033
|
+
const y = clamp$1(Math.floor(rect.y), -limit, limit - 1);
|
|
2034
|
+
const right = clamp$1(Math.ceil(rect.x + rect.width), x + 1, limit);
|
|
2035
|
+
const bottom = clamp$1(Math.ceil(rect.y + rect.height), y + 1, limit);
|
|
2036
|
+
return {
|
|
2037
|
+
x,
|
|
2038
|
+
y,
|
|
2039
|
+
width: right - x,
|
|
2040
|
+
height: bottom - y
|
|
2041
|
+
};
|
|
2042
|
+
}
|
|
2043
|
+
// src/core/model/layer-placement.ts
|
|
2044
|
+
var QUARTERS = [
|
|
2045
|
+
0,
|
|
2046
|
+
900,
|
|
2047
|
+
1800,
|
|
2048
|
+
2700
|
|
2049
|
+
];
|
|
2050
|
+
var clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
2051
|
+
function newLayerBox(plan, source, seen) {
|
|
2052
|
+
const crop = cropRect(plan.base.crop, source);
|
|
2053
|
+
const { orientation } = plan.base;
|
|
2054
|
+
const composition = compositionSize(plan, source);
|
|
2055
|
+
const width = clamp(Math.round(seen.width), 1, MAX_SCENE_COORDINATE);
|
|
2056
|
+
const height = clamp(Math.round(seen.height), 1, MAX_SCENE_COORDINATE);
|
|
2057
|
+
const asSeen = (rotation, flipX) => {
|
|
2058
|
+
const [a, , , d] = layerToOutput({
|
|
2059
|
+
x: 0,
|
|
2060
|
+
y: 0,
|
|
2061
|
+
width,
|
|
2062
|
+
height,
|
|
2063
|
+
rotation,
|
|
2064
|
+
flipX
|
|
2065
|
+
}, crop, orientation, composition);
|
|
2066
|
+
return a > .5 && d > .5;
|
|
2067
|
+
};
|
|
2068
|
+
const flipX = !QUARTERS.some((rotation) => asSeen(rotation, false));
|
|
2069
|
+
const rotation = QUARTERS.find((quarter) => asSeen(quarter, flipX)) ?? 0;
|
|
2070
|
+
const centre = compositionToSource({
|
|
2071
|
+
x: composition.width / 2,
|
|
2072
|
+
y: composition.height / 2
|
|
2073
|
+
}, crop, orientation);
|
|
2074
|
+
return {
|
|
2075
|
+
x: Math.round(centre.x - width / 2) || 0,
|
|
2076
|
+
y: Math.round(centre.y - height / 2) || 0,
|
|
2077
|
+
width,
|
|
2078
|
+
height,
|
|
2079
|
+
rotation,
|
|
2080
|
+
flipX
|
|
2081
|
+
};
|
|
2082
|
+
}
|
|
2083
|
+
function fittedImageSize(plan, source, image) {
|
|
2084
|
+
const { width, height } = compositionSize(plan, source);
|
|
2085
|
+
const scale = Math.min(1, width / 2 / image.width, height / 2 / image.height);
|
|
2086
|
+
return {
|
|
2087
|
+
width: image.width * scale,
|
|
2088
|
+
height: image.height * scale
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
// src/core/session/session.ts
|
|
2092
|
+
function sourcesIn(command) {
|
|
2093
|
+
const of = (layer) => layer.kind === "image" && typeof layer.source === "string" ? [layer.source] : [];
|
|
2094
|
+
switch (command.type) {
|
|
2095
|
+
case "image.insertLayers": return command.layers.flatMap(of);
|
|
2096
|
+
case "image.restoreLayers": return command.entries.flatMap(({ layer }) => of(layer));
|
|
2097
|
+
case "image.updateLayers": return command.updates.flatMap(({ props }) => typeof props["source"] === "string" ? [props["source"]] : []);
|
|
2098
|
+
default: return [];
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
var sourcesOf = (plan) => plan.layers.flatMap((layer) => layer.kind === "image" ? [layer.source] : []);
|
|
2102
|
+
function defaultLayer(source, plan, base) {
|
|
2103
|
+
return {
|
|
2104
|
+
key: createImageKey(new Set([...plan.layers, ...plan.redactions].map((item) => item.key))),
|
|
2105
|
+
kind: "image",
|
|
2106
|
+
name: "",
|
|
2107
|
+
visible: true,
|
|
2108
|
+
opacity: 100,
|
|
2109
|
+
...newLayerBox(plan, base, fittedImageSize(plan, base, source)),
|
|
2110
|
+
source: source.id,
|
|
2111
|
+
crop: null
|
|
2112
|
+
};
|
|
2113
|
+
}
|
|
2114
|
+
var same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
2115
|
+
function commandsBetween(from, to) {
|
|
2116
|
+
const commands = [];
|
|
2117
|
+
if (!same(from.base.crop, to.base.crop)) commands.push({
|
|
2118
|
+
type: "image.setCrop",
|
|
2119
|
+
crop: to.base.crop
|
|
2120
|
+
});
|
|
2121
|
+
if (!same(from.base.orientation, to.base.orientation)) commands.push({
|
|
2122
|
+
type: "image.setOrientation",
|
|
2123
|
+
orientation: to.base.orientation
|
|
2124
|
+
});
|
|
2125
|
+
const develop = Object.fromEntries(Object.entries(to.base.develop).filter(([name, value]) => from.base.develop[name] !== value));
|
|
2126
|
+
if (Object.keys(develop).length > 0) commands.push({
|
|
2127
|
+
type: "image.setDevelop",
|
|
2128
|
+
values: develop
|
|
2129
|
+
});
|
|
2130
|
+
if (!same(from.base.filter, to.base.filter)) commands.push({
|
|
2131
|
+
type: "image.setFilter",
|
|
2132
|
+
filter: to.base.filter
|
|
2133
|
+
});
|
|
2134
|
+
if (!same(from.output, to.output)) commands.push({
|
|
2135
|
+
type: "image.setOutput",
|
|
2136
|
+
output: to.output
|
|
2137
|
+
});
|
|
2138
|
+
for (const field of Object.keys(to.metadata)) if (from.metadata[field] !== to.metadata[field]) commands.push({
|
|
2139
|
+
type: "image.setMetadata",
|
|
2140
|
+
field,
|
|
2141
|
+
value: to.metadata[field]
|
|
2142
|
+
});
|
|
2143
|
+
if (!same(from.layers, to.layers)) {
|
|
2144
|
+
if (from.layers.length > 0) commands.push({
|
|
2145
|
+
type: "image.removeLayers",
|
|
2146
|
+
keys: from.layers.map((layer) => layer.key)
|
|
2147
|
+
});
|
|
2148
|
+
if (to.layers.length > 0) commands.push({
|
|
2149
|
+
type: "image.insertLayers",
|
|
2150
|
+
at: 0,
|
|
2151
|
+
layers: to.layers
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
if (!same(from.redactions, to.redactions)) {
|
|
2155
|
+
if (from.redactions.length > 0) commands.push({
|
|
2156
|
+
type: "image.removeRedactions",
|
|
2157
|
+
keys: from.redactions.map((redaction) => redaction.key)
|
|
2158
|
+
});
|
|
2159
|
+
if (to.redactions.length > 0) commands.push({
|
|
2160
|
+
type: "image.insertRedactions",
|
|
2161
|
+
at: 0,
|
|
2162
|
+
redactions: to.redactions
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
return commands;
|
|
2166
|
+
}
|
|
2167
|
+
var PRESENCE = [
|
|
2168
|
+
"location",
|
|
2169
|
+
"camera",
|
|
2170
|
+
"timestamps",
|
|
2171
|
+
"thumbnail",
|
|
2172
|
+
"colourProfile",
|
|
2173
|
+
"other"
|
|
2174
|
+
];
|
|
2175
|
+
var decodedBytes = (probe) => probe.oriented.width * probe.oriented.height * 4;
|
|
2176
|
+
function checkSource(policy, file, probe) {
|
|
2177
|
+
const near = checkFileLimits(policy, file.size, probe);
|
|
2178
|
+
const bytes = decodedBytes(probe);
|
|
2179
|
+
if (policy.check("maxSessionSize", bytes) === "warning") near.push("maxSessionSize");
|
|
2180
|
+
return near;
|
|
2181
|
+
}
|
|
2182
|
+
function createImageSession(options) {
|
|
2183
|
+
const { engine, processor } = options;
|
|
2184
|
+
const policy = imageLimits.resolve(options.filePolicy);
|
|
2185
|
+
const history = createHistory();
|
|
2186
|
+
let source = null;
|
|
2187
|
+
const extras = /* @__PURE__ */ new Map();
|
|
2188
|
+
const exports = /* @__PURE__ */ new Set();
|
|
2189
|
+
const sourceOf = (id) => source?.id === id ? source : extras.get(id);
|
|
2190
|
+
const reducer = createImageReducer(sourceOf);
|
|
2191
|
+
let plan = null;
|
|
2192
|
+
let opens = 0;
|
|
2193
|
+
let opening = 0;
|
|
2194
|
+
let exporting = 0;
|
|
2195
|
+
let inserting = 0;
|
|
2196
|
+
let generation = 0;
|
|
2197
|
+
let warnings = [];
|
|
2198
|
+
let error = null;
|
|
2199
|
+
let nextId = 0;
|
|
2200
|
+
let destroyed = false;
|
|
2201
|
+
const snapshot = () => ({
|
|
2202
|
+
status: plan !== null ? "ready" : !destroyed && opening > 0 ? "opening" : "empty",
|
|
2203
|
+
plan,
|
|
2204
|
+
source,
|
|
2205
|
+
sources: source ? [source, ...extras.values()] : [],
|
|
2206
|
+
canUndo: history.canUndo,
|
|
2207
|
+
canRedo: history.canRedo,
|
|
2208
|
+
historyTrimmed: history.trimmed,
|
|
2209
|
+
inserting: !destroyed && inserting > 0,
|
|
2210
|
+
opening: !destroyed && opening > 0,
|
|
2211
|
+
exporting: !destroyed && exporting > 0,
|
|
2212
|
+
warnings,
|
|
2213
|
+
error
|
|
2214
|
+
});
|
|
2215
|
+
const store = createStore(snapshot());
|
|
2216
|
+
const publish = () => store.set(snapshot());
|
|
2217
|
+
const emit = (event) => {
|
|
2218
|
+
try {
|
|
2219
|
+
options.onEvent?.(event);
|
|
2220
|
+
} catch {}
|
|
2221
|
+
};
|
|
2222
|
+
const assertAlive = () => {
|
|
2223
|
+
if (destroyed) throw new Error("The image session has been destroyed.");
|
|
2224
|
+
};
|
|
2225
|
+
const fail = (reason) => {
|
|
2226
|
+
error = isImageError(reason) ? reason : asImageError(reason);
|
|
2227
|
+
return error;
|
|
2228
|
+
};
|
|
2229
|
+
const superseded = () => new ImageError("ABORTED", "A later open replaced this one.", { details: { reason: "superseded" } });
|
|
2230
|
+
const referenced = (dropped = 0) => {
|
|
2231
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2232
|
+
for (const held of [plan, ...exports]) if (held) for (const id of sourcesOf(held)) ids.add(id);
|
|
2233
|
+
const gone = new Set(history.dropOrder.slice(0, dropped));
|
|
2234
|
+
for (const step of history.steps) {
|
|
2235
|
+
if (gone.has(step)) continue;
|
|
2236
|
+
for (const command of [...step.commands, ...step.inverses]) for (const id of sourcesIn(command)) ids.add(id);
|
|
2237
|
+
}
|
|
2238
|
+
return ids;
|
|
2239
|
+
};
|
|
2240
|
+
const releaseUnreferenced = () => {
|
|
2241
|
+
const kept = referenced();
|
|
2242
|
+
for (const id of [...extras.keys()]) {
|
|
2243
|
+
if (kept.has(id)) continue;
|
|
2244
|
+
extras.delete(id);
|
|
2245
|
+
engine.release(id).catch(() => {});
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
const heldBytes = (without = /* @__PURE__ */ new Set()) => {
|
|
2249
|
+
let bytes = source?.decodedBytes ?? 0;
|
|
2250
|
+
for (const [id, extra] of extras) if (!without.has(id)) bytes += extra.decodedBytes;
|
|
2251
|
+
return bytes;
|
|
2252
|
+
};
|
|
2253
|
+
const fits = (bytes) => {
|
|
2254
|
+
try {
|
|
2255
|
+
return policy.check("maxSessionSize", bytes);
|
|
2256
|
+
} catch {
|
|
2257
|
+
return false;
|
|
2258
|
+
}
|
|
2259
|
+
};
|
|
2260
|
+
const makeRoom = (bytes) => {
|
|
2261
|
+
const warned = (outcome) => outcome === "warning" ? ["maxSessionSize"] : [];
|
|
2262
|
+
const now = fits(heldBytes() + bytes);
|
|
2263
|
+
if (now !== false) return warned(now);
|
|
2264
|
+
for (let dropped = 1; dropped <= history.dropOrder.length; dropped += 1) {
|
|
2265
|
+
const kept = referenced(dropped);
|
|
2266
|
+
const freed = new Set([...extras.keys()].filter((id) => !kept.has(id)));
|
|
2267
|
+
const after = fits(heldBytes(freed) + bytes);
|
|
2268
|
+
if (after === false) continue;
|
|
2269
|
+
history.drop(dropped);
|
|
2270
|
+
releaseUnreferenced();
|
|
2271
|
+
return warned(after);
|
|
2272
|
+
}
|
|
2273
|
+
policy.check("maxSessionSize", heldBytes() + bytes);
|
|
2274
|
+
return [];
|
|
2275
|
+
};
|
|
2276
|
+
const attempt = (from, commands) => {
|
|
2277
|
+
const { state, inverses } = applyCommands(reducer, from, commands);
|
|
2278
|
+
if (source) checkOutputLimits(policy, outputSize(state.output, compositionSize(state, source)));
|
|
2279
|
+
const items = state.layers.length + state.redactions.length;
|
|
2280
|
+
if (items > from.layers.length + from.redactions.length) policy.check("maxLayers", items);
|
|
2281
|
+
return {
|
|
2282
|
+
state,
|
|
2283
|
+
step: {
|
|
2284
|
+
commands: [...commands],
|
|
2285
|
+
inverses: [...inverses]
|
|
2286
|
+
}
|
|
2287
|
+
};
|
|
2288
|
+
};
|
|
2289
|
+
const replay = (direction) => {
|
|
2290
|
+
assertAlive();
|
|
2291
|
+
const step = direction === "undo" ? history.undo() : history.redo();
|
|
2292
|
+
if (!step || plan === null) return false;
|
|
2293
|
+
const commands = direction === "undo" ? step.inverses : step.commands;
|
|
2294
|
+
try {
|
|
2295
|
+
plan = applyCommands(reducer, plan, commands).state;
|
|
2296
|
+
} catch (reason) {
|
|
2297
|
+
history.restore(direction);
|
|
2298
|
+
publish();
|
|
2299
|
+
emit({
|
|
2300
|
+
type: direction,
|
|
2301
|
+
outcome: "failed",
|
|
2302
|
+
commands: commands.map((c) => c.type)
|
|
2303
|
+
});
|
|
2304
|
+
throw fail(reason);
|
|
2305
|
+
}
|
|
2306
|
+
error = null;
|
|
2307
|
+
releaseUnreferenced();
|
|
2308
|
+
publish();
|
|
2309
|
+
emit({
|
|
2310
|
+
type: direction,
|
|
2311
|
+
outcome: "done",
|
|
2312
|
+
commands: commands.map((c) => c.type)
|
|
2313
|
+
});
|
|
2314
|
+
return true;
|
|
2315
|
+
};
|
|
2316
|
+
const executeCommands = (input) => {
|
|
2317
|
+
assertAlive();
|
|
2318
|
+
const commands = Array.isArray(input) ? input : [input];
|
|
2319
|
+
try {
|
|
2320
|
+
if (plan === null) throw new ImageError("INVALID_COMMAND", "There's nothing to edit before a file is open.");
|
|
2321
|
+
const result = attempt(plan, commands);
|
|
2322
|
+
plan = result.state;
|
|
2323
|
+
history.push(result.step);
|
|
2324
|
+
error = null;
|
|
2325
|
+
releaseUnreferenced();
|
|
2326
|
+
emit({
|
|
2327
|
+
type: "execute",
|
|
2328
|
+
outcome: "done",
|
|
2329
|
+
commands: commands.map((c) => c.type)
|
|
2330
|
+
});
|
|
2331
|
+
} catch (reason) {
|
|
2332
|
+
emit({
|
|
2333
|
+
type: "execute",
|
|
2334
|
+
outcome: "failed",
|
|
2335
|
+
commands: commands.map((c) => c.type)
|
|
2336
|
+
});
|
|
2337
|
+
throw fail(reason);
|
|
2338
|
+
} finally {
|
|
2339
|
+
publish();
|
|
2340
|
+
}
|
|
2341
|
+
};
|
|
2342
|
+
return {
|
|
2343
|
+
getSnapshot: () => store.getSnapshot(),
|
|
2344
|
+
subscribe: (listener) => store.subscribe(listener),
|
|
2345
|
+
async open(file, openOptions = {}) {
|
|
2346
|
+
assertAlive();
|
|
2347
|
+
opens += 1;
|
|
2348
|
+
const ticket = opens;
|
|
2349
|
+
nextId += 1;
|
|
2350
|
+
const id = `source-${nextId}`;
|
|
2351
|
+
let registered = false;
|
|
2352
|
+
opening += 1;
|
|
2353
|
+
publish();
|
|
2354
|
+
try {
|
|
2355
|
+
const { probe, near, info } = await withProcessingTime(policy, async (signal) => {
|
|
2356
|
+
throwIfAborted(signal);
|
|
2357
|
+
policy.check("maxFileSize", file.size);
|
|
2358
|
+
const probe = await engine.probe(file, { signal });
|
|
2359
|
+
throwIfAborted(signal);
|
|
2360
|
+
const near = checkSource(policy, file, probe);
|
|
2361
|
+
if (ticket !== opens) throw superseded();
|
|
2362
|
+
const info = await engine.register(id, file, probe, {
|
|
2363
|
+
filePolicy: policy,
|
|
2364
|
+
signal
|
|
2365
|
+
});
|
|
2366
|
+
registered = true;
|
|
2367
|
+
throwIfAborted(signal);
|
|
2368
|
+
return {
|
|
2369
|
+
probe,
|
|
2370
|
+
near,
|
|
2371
|
+
info
|
|
2372
|
+
};
|
|
2373
|
+
}, openOptions);
|
|
2374
|
+
assertAlive();
|
|
2375
|
+
if (ticket !== opens) throw superseded();
|
|
2376
|
+
const previous = source;
|
|
2377
|
+
source = {
|
|
2378
|
+
...info,
|
|
2379
|
+
fileSize: file.size,
|
|
2380
|
+
metadata: probe.metadata,
|
|
2381
|
+
keepable: probe.keepable
|
|
2382
|
+
};
|
|
2383
|
+
plan = createPlan(id);
|
|
2384
|
+
history.clear();
|
|
2385
|
+
generation += 1;
|
|
2386
|
+
warnings = near;
|
|
2387
|
+
error = null;
|
|
2388
|
+
if (previous) engine.release(previous.id).catch(() => {});
|
|
2389
|
+
releaseUnreferenced();
|
|
2390
|
+
emit({
|
|
2391
|
+
type: "open",
|
|
2392
|
+
outcome: "done",
|
|
2393
|
+
commands: []
|
|
2394
|
+
});
|
|
2395
|
+
return source;
|
|
2396
|
+
} catch (reason) {
|
|
2397
|
+
if (registered && source?.id !== id) await engine.release(id).catch(() => {});
|
|
2398
|
+
const failure = isImageError(reason) ? reason : asImageError(reason);
|
|
2399
|
+
const cancelled = failure.code === "ABORTED";
|
|
2400
|
+
if (!cancelled && !destroyed && ticket === opens) fail(failure);
|
|
2401
|
+
emit({
|
|
2402
|
+
type: "open",
|
|
2403
|
+
outcome: cancelled ? "cancelled" : "failed",
|
|
2404
|
+
commands: []
|
|
2405
|
+
});
|
|
2406
|
+
throw failure;
|
|
2407
|
+
} finally {
|
|
2408
|
+
opening -= 1;
|
|
2409
|
+
if (!destroyed) publish();
|
|
2410
|
+
}
|
|
2411
|
+
},
|
|
2412
|
+
async insertImage(file, insertOptions = {}) {
|
|
2413
|
+
assertAlive();
|
|
2414
|
+
if (plan === null || source === null) throw new ImageError("INVALID_COMMAND", "There's no image to add a layer to yet.");
|
|
2415
|
+
const started = generation;
|
|
2416
|
+
nextId += 1;
|
|
2417
|
+
const id = `source-${nextId}`;
|
|
2418
|
+
let registered = false;
|
|
2419
|
+
let held = false;
|
|
2420
|
+
inserting += 1;
|
|
2421
|
+
publish();
|
|
2422
|
+
try {
|
|
2423
|
+
const { probe, info } = await withProcessingTime(policy, async (signal) => {
|
|
2424
|
+
throwIfAborted(signal);
|
|
2425
|
+
policy.check("maxFileSize", file.size);
|
|
2426
|
+
const probe = await engine.probe(file, { signal });
|
|
2427
|
+
throwIfAborted(signal);
|
|
2428
|
+
checkFileLimits(policy, file.size, probe);
|
|
2429
|
+
const info = await engine.register(id, file, probe, {
|
|
2430
|
+
filePolicy: policy,
|
|
2431
|
+
signal
|
|
2432
|
+
});
|
|
2433
|
+
registered = true;
|
|
2434
|
+
throwIfAborted(signal);
|
|
2435
|
+
return {
|
|
2436
|
+
probe,
|
|
2437
|
+
info
|
|
2438
|
+
};
|
|
2439
|
+
}, insertOptions);
|
|
2440
|
+
assertAlive();
|
|
2441
|
+
if (started !== generation || plan === null || source === null) throw superseded();
|
|
2442
|
+
const near = checkFileLimits(policy, file.size, probe);
|
|
2443
|
+
if (policy.check("maxFiles", extras.size + 2) === "warning") near.push("maxFiles");
|
|
2444
|
+
near.push(...makeRoom(info.decodedBytes));
|
|
2445
|
+
const added = {
|
|
2446
|
+
...info,
|
|
2447
|
+
fileSize: file.size,
|
|
2448
|
+
metadata: probe.metadata,
|
|
2449
|
+
keepable: probe.keepable
|
|
2450
|
+
};
|
|
2451
|
+
extras.set(id, added);
|
|
2452
|
+
held = true;
|
|
2453
|
+
try {
|
|
2454
|
+
const placed = insertOptions.place?.(added, plan) ?? {
|
|
2455
|
+
type: "image.insertLayers",
|
|
2456
|
+
at: plan.layers.length,
|
|
2457
|
+
layers: [defaultLayer(added, plan, source)]
|
|
2458
|
+
};
|
|
2459
|
+
const commands = Array.isArray(placed) ? placed : [placed];
|
|
2460
|
+
if (!commands.some((command) => sourcesIn(command).includes(id))) throw new ImageError("INVALID_COMMAND", "The commands don't add the image to the plan.");
|
|
2461
|
+
executeCommands(commands);
|
|
2462
|
+
} finally {
|
|
2463
|
+
releaseUnreferenced();
|
|
2464
|
+
}
|
|
2465
|
+
warnings = near;
|
|
2466
|
+
emit({
|
|
2467
|
+
type: "insert",
|
|
2468
|
+
outcome: "done",
|
|
2469
|
+
commands: []
|
|
2470
|
+
});
|
|
2471
|
+
return added;
|
|
2472
|
+
} catch (reason) {
|
|
2473
|
+
if (registered && !held) await engine.release(id).catch(() => {});
|
|
2474
|
+
const failure = isImageError(reason) ? reason : asImageError(reason);
|
|
2475
|
+
const cancelled = failure.code === "ABORTED";
|
|
2476
|
+
if (!cancelled && !destroyed && started === generation) fail(failure);
|
|
2477
|
+
emit({
|
|
2478
|
+
type: "insert",
|
|
2479
|
+
outcome: cancelled ? "cancelled" : "failed",
|
|
2480
|
+
commands: []
|
|
2481
|
+
});
|
|
2482
|
+
throw failure;
|
|
2483
|
+
} finally {
|
|
2484
|
+
inserting -= 1;
|
|
2485
|
+
if (!destroyed) publish();
|
|
2486
|
+
}
|
|
2487
|
+
},
|
|
2488
|
+
execute: (input) => executeCommands(input),
|
|
2489
|
+
applyPlan(next) {
|
|
2490
|
+
assertAlive();
|
|
2491
|
+
const shaped = Array.isArray(next.redactions) && Array.isArray(next.layers) && next.animation === null;
|
|
2492
|
+
if (plan === null || !shaped || next.base.source !== plan.base.source) {
|
|
2493
|
+
const failure = new ImageError("INVALID_COMMAND", shaped ? "The plan isn't for the open image." : "The plan needs layers, redactions and animation, as createPlan gives them.");
|
|
2494
|
+
error = failure;
|
|
2495
|
+
publish();
|
|
2496
|
+
throw failure;
|
|
2497
|
+
}
|
|
2498
|
+
const commands = commandsBetween(plan, next);
|
|
2499
|
+
if (commands.length > 0) executeCommands(commands);
|
|
2500
|
+
},
|
|
2501
|
+
group(fn) {
|
|
2502
|
+
assertAlive();
|
|
2503
|
+
return history.group(fn);
|
|
2504
|
+
},
|
|
2505
|
+
undo: () => replay("undo"),
|
|
2506
|
+
redo: () => replay("redo"),
|
|
2507
|
+
async export(format, exportOptions = {}) {
|
|
2508
|
+
assertAlive();
|
|
2509
|
+
const { onProgress } = exportOptions;
|
|
2510
|
+
if (plan === null || source === null) throw new ImageError("INVALID_COMMAND", "There's nothing to export before a file is open.");
|
|
2511
|
+
const started = generation;
|
|
2512
|
+
const exported = plan;
|
|
2513
|
+
const from = source;
|
|
2514
|
+
exporting += 1;
|
|
2515
|
+
exports.add(exported);
|
|
2516
|
+
publish();
|
|
2517
|
+
try {
|
|
2518
|
+
const size = outputSize(exported.output, compositionSize(exported, from));
|
|
2519
|
+
const blob = await withProcessingTime(policy, async (signal) => {
|
|
2520
|
+
throwIfAborted(signal);
|
|
2521
|
+
const blob = await processor.export({
|
|
2522
|
+
plan: exported,
|
|
2523
|
+
format
|
|
2524
|
+
}, {
|
|
2525
|
+
filePolicy: policy,
|
|
2526
|
+
signal,
|
|
2527
|
+
...onProgress ? { onProgress } : {}
|
|
2528
|
+
});
|
|
2529
|
+
throwIfAborted(signal);
|
|
2530
|
+
return blob;
|
|
2531
|
+
}, exportOptions);
|
|
2532
|
+
if (started !== generation || destroyed) throw new ImageError("ABORTED", "Another file was opened during the export.", { details: { reason: "superseded" } });
|
|
2533
|
+
error = null;
|
|
2534
|
+
emit({
|
|
2535
|
+
type: "export",
|
|
2536
|
+
outcome: "done",
|
|
2537
|
+
commands: []
|
|
2538
|
+
});
|
|
2539
|
+
return {
|
|
2540
|
+
blob,
|
|
2541
|
+
width: size.width,
|
|
2542
|
+
height: size.height,
|
|
2543
|
+
removed: PRESENCE.filter((kind) => from.metadata[kind]),
|
|
2544
|
+
kept: Object.keys(exported.metadata).filter((field) => exported.metadata[field] !== "")
|
|
2545
|
+
};
|
|
2546
|
+
} catch (reason) {
|
|
2547
|
+
const failure = isImageError(reason) ? reason : asImageError(reason);
|
|
2548
|
+
const cancelled = failure.code === "ABORTED";
|
|
2549
|
+
if (!cancelled && !destroyed && started === generation) fail(failure);
|
|
2550
|
+
emit({
|
|
2551
|
+
type: "export",
|
|
2552
|
+
outcome: cancelled ? "cancelled" : "failed",
|
|
2553
|
+
commands: []
|
|
2554
|
+
});
|
|
2555
|
+
throw failure;
|
|
2556
|
+
} finally {
|
|
2557
|
+
exporting -= 1;
|
|
2558
|
+
exports.delete(exported);
|
|
2559
|
+
if (!destroyed) {
|
|
2560
|
+
releaseUnreferenced();
|
|
2561
|
+
publish();
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
},
|
|
2565
|
+
async destroy() {
|
|
2566
|
+
if (destroyed) return;
|
|
2567
|
+
destroyed = true;
|
|
2568
|
+
const held = [...source ? [source.id] : [], ...extras.keys()];
|
|
2569
|
+
source = null;
|
|
2570
|
+
extras.clear();
|
|
2571
|
+
plan = null;
|
|
2572
|
+
history.clear();
|
|
2573
|
+
publish();
|
|
2574
|
+
await Promise.all(held.map((id) => engine.release(id).catch(() => {})));
|
|
2575
|
+
}
|
|
2576
|
+
};
|
|
2577
|
+
}
|
|
2578
|
+
// src/messages/en.ts
|
|
2579
|
+
var imageMessages = /* @__PURE__ */ defineMessages({
|
|
2580
|
+
...sdkCoreMessages,
|
|
2581
|
+
"errors.INVALID_IMAGE": "This file isn't a valid image, or it's too damaged to open.",
|
|
2582
|
+
"errors.UNSUPPORTED_FORMAT": "This image format isn't supported. Try converting the file to PNG or JPEG, then open it again.",
|
|
2583
|
+
"errors.DECODE_FAILED": "This browser can't open this kind of image. Try another browser, or convert the file to PNG or JPEG.",
|
|
2584
|
+
"errors.INVALID_COMMAND": "This edit couldn't be made. Please try again.",
|
|
2585
|
+
"errors.INVALID_DOCUMENT": "This project is damaged, or it isn't an image project.",
|
|
2586
|
+
"errors.UNSUPPORTED_PLAN": "This image has edits this version can't show. Update the application and try again.",
|
|
2587
|
+
"errors.IMAGE_TOO_LARGE": "This image has more pixels than the editor can handle. Try a smaller version of it.",
|
|
2588
|
+
"errors.IMAGE_DIMENSION_TOO_LARGE": "This image is {actual, number} pixels across, which is over the {maximum, number}-pixel limit.",
|
|
2589
|
+
"errors.TOO_MANY_FRAMES": /* @__PURE__ */ plural("maximum", {
|
|
2590
|
+
one: "This animation has {actual, number} frames, which is over the {maximum}-frame limit.",
|
|
2591
|
+
other: "This animation has {actual, number} frames, which is over the {maximum, number}-frame limit."
|
|
2592
|
+
}),
|
|
2593
|
+
"errors.SESSION_TOO_LARGE": "The open images would use {actual, fileSize} of memory, which is over the {maximum, fileSize} limit. Close an image, or clear the undo history, and try again.",
|
|
2594
|
+
"errors.TOO_MANY_LAYERS": /* @__PURE__ */ plural("maximum", {
|
|
2595
|
+
one: "An image can have {maximum} layer. Merge or delete layers, and try again.",
|
|
2596
|
+
other: "An image can have up to {maximum, number} layers. Merge or delete layers, and try again."
|
|
2597
|
+
}),
|
|
2598
|
+
"warnings.maxImagePixels": "This image is large, and may be slow to edit on some devices.",
|
|
2599
|
+
"warnings.maxImageDimension": "This image is very wide or tall, and may be slow to edit.",
|
|
2600
|
+
"warnings.maxFrames": /* @__PURE__ */ plural("actual", {
|
|
2601
|
+
one: "This animation has {actual} frame.",
|
|
2602
|
+
other: "This animation has {actual, number} frames, and may be slow to edit and export."
|
|
2603
|
+
}),
|
|
2604
|
+
"warnings.maxSessionSize": "The open images use {actual, fileSize} of memory. Clearing the undo history frees memory.",
|
|
2605
|
+
"warnings.maxLayers": /* @__PURE__ */ plural("actual", {
|
|
2606
|
+
one: "This image has {actual} layer.",
|
|
2607
|
+
other: "This image has {actual, number} layers, and may be slow to edit and export."
|
|
2608
|
+
})
|
|
2609
|
+
});
|
|
2610
|
+
export { NO_METADATA as $, compositionSize as A, imageLimitDefinitions as At, checkOutput as B, cropInComposition as C, createImageProcessor as Ct, moveCrop as D, PER_FILE_LIMITS as Dt, handleInSource as E, OUTPUT_LIMITS as Et, sourceToComposition as F, Invalid as G, FORBIDDEN_IN_TEXT as H, checkDevelop as I, invalid as J, checkCrop as K, checkFilter as L, cropRect as M, asImageError as Mt, orientedSize as N, ImageError as Nt, nudgeCropOnScreen as O, checkFileLimits as Ot, outputSize as P, isImageError as Pt, MAX_METADATA_LENGTH as Q, checkMetadata as R, compositionDeltaToSource as S, placement as St, fitAspect as T, engineInternals as Tt, checkLayer as U, createImageReducer as V, checkRedaction as W, onlyFields as X, isRecord as Y, EMPTY_KEEPABLE_METADATA as Z, sceneToLayerSource as _, createPlan as _t, fitRedactionRegion as a, LAYER_FONTS as at, ASPECT_PRESETS as b, backingSize as bt, redactionBlocksInScene as c, MAX_KEY_LENGTH as ct, applyMatrix as d, MAX_PLAN_ITEMS as dt, longerThan as et, fitTextLines as f, MAX_SCENE_COORDINATE as ft, layerToScene as g, UPRIGHT as gt, layerToOutput as h, REDACTION_MODES as ht, compositionRectToScene as i, LAYER_ALIGNMENTS as it, compositionToSource as j, imageLimits as jt, wholeCrop as k, checkOutputLimits as kt, redactionRectInOutput as l, MAX_LAYER_NAME_LENGTH as lt, isRightToLeft as m, NEUTRAL_DEVELOP as mt, createImageSession as n, FILTER_PRESETS as nt, redactionBlockSize as o, LAYER_KINDS as ot, invertMatrix as p, MAX_STROKE_WIDTH as pt, checkCropInSource as q, newLayerBox as r, FULL_SIZE as rt, redactionBlocksInOutput as s, LAYER_SHAPES as st, imageMessages as t, DEVELOP_ADJUSTMENTS as tt, sceneRectToComposition as u, MAX_LAYER_TEXT_LENGTH as ut, sceneToOutput as v, createImageRenderer as vt, dragHandle as w, createImageEngine as wt, aspectRatio as x, fittedView as xt, createImageKey as y, MAX_PIXEL_RATIO as yt, checkOrientation as z };
|
|
2611
|
+
|
|
2612
|
+
//# sourceMappingURL=core-BlFeJWak.js.map
|