@nodetool-ai/image-editor 0.8.5 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Sketch document → layered PSD.
3
+ *
4
+ * Each layer becomes its own Photoshop layer with name, visibility, opacity,
5
+ * blend mode and position. Vector layers, effects and transforms are baked
6
+ * into pixels by the caller's renderer, so Photoshop shows what the editor
7
+ * shows. Groups become Photoshop layer groups. The inpainting mask layer is
8
+ * kept as a hidden layer, matching the flattened export, which leaves it out.
9
+ *
10
+ * The editor's "Export PSD" and the headless renderer both encode through
11
+ * here.
12
+ */
13
+ import { encodePsd } from "./encoder.js";
14
+ const EMPTY_PIXELS = {
15
+ left: 0,
16
+ top: 0,
17
+ width: 0,
18
+ height: 0,
19
+ pixels: new Uint8ClampedArray(0)
20
+ };
21
+ /** Crop document-size pixels to the bounding box of non-transparent pixels. */
22
+ export function cropToOpaqueBounds(image) {
23
+ const { width, height, data } = image;
24
+ let minX = width;
25
+ let minY = height;
26
+ let maxX = -1;
27
+ let maxY = -1;
28
+ for (let y = 0; y < height; y++) {
29
+ const row = y * width * 4;
30
+ for (let x = 0; x < width; x++) {
31
+ if (data[row + x * 4 + 3] !== 0) {
32
+ if (x < minX) {
33
+ minX = x;
34
+ }
35
+ if (x > maxX) {
36
+ maxX = x;
37
+ }
38
+ if (y < minY) {
39
+ minY = y;
40
+ }
41
+ maxY = y;
42
+ }
43
+ }
44
+ }
45
+ if (maxX < 0) {
46
+ return EMPTY_PIXELS;
47
+ }
48
+ const cropW = maxX - minX + 1;
49
+ const cropH = maxY - minY + 1;
50
+ const pixels = new Uint8ClampedArray(cropW * cropH * 4);
51
+ for (let y = 0; y < cropH; y++) {
52
+ const from = ((minY + y) * width + minX) * 4;
53
+ pixels.set(data.subarray(from, from + cropW * 4), y * cropW * 4);
54
+ }
55
+ return { left: minX, top: minY, width: cropW, height: cropH, pixels };
56
+ }
57
+ function layerName(layer) {
58
+ return layer.name.trim() || "Layer";
59
+ }
60
+ /** Build the PSD layer tree, bottom to top, from the document's layer array. */
61
+ export function buildPsdLayerTree(doc, renderLayer) {
62
+ const ids = new Set(doc.layers.map((layer) => layer.id));
63
+ const childrenOf = new Map();
64
+ for (const layer of doc.layers) {
65
+ // A dangling parent id puts the layer at the root, as the panel does not
66
+ // show it otherwise and dropping it would lose pixels.
67
+ const parent = layer.parentId && ids.has(layer.parentId) ? layer.parentId : null;
68
+ const siblings = childrenOf.get(parent);
69
+ if (siblings) {
70
+ siblings.push(layer);
71
+ }
72
+ else {
73
+ childrenOf.set(parent, [layer]);
74
+ }
75
+ }
76
+ const visited = new Set();
77
+ const build = (parentId) => {
78
+ const nodes = [];
79
+ for (const layer of childrenOf.get(parentId) ?? []) {
80
+ if (visited.has(layer.id)) {
81
+ continue;
82
+ }
83
+ visited.add(layer.id);
84
+ if (layer.type === "group") {
85
+ nodes.push({
86
+ kind: "group",
87
+ name: layerName(layer),
88
+ visible: layer.visible,
89
+ opacity: layer.opacity,
90
+ collapsed: Boolean(layer.collapsed),
91
+ children: build(layer.id)
92
+ });
93
+ continue;
94
+ }
95
+ const rendered = renderLayer(layer.id);
96
+ const cropped = rendered ? cropToOpaqueBounds(rendered) : EMPTY_PIXELS;
97
+ nodes.push({
98
+ kind: "layer",
99
+ name: layerName(layer),
100
+ visible: layer.type === "mask" ? false : layer.visible,
101
+ opacity: layer.opacity,
102
+ blendMode: layer.blendMode || "normal",
103
+ transparencyLocked: layer.alphaLock,
104
+ ...cropped
105
+ });
106
+ }
107
+ return nodes;
108
+ };
109
+ return build(null);
110
+ }
111
+ /** Encode the document as a layered PSD file. */
112
+ export function encodeSketchPsd(doc, sources) {
113
+ return encodePsd({
114
+ width: sources.composite.width,
115
+ height: sources.composite.height,
116
+ composite: sources.composite.data,
117
+ layers: buildPsdLayerTree(doc, sources.renderLayer)
118
+ });
119
+ }
120
+ //# sourceMappingURL=sketch.js.map
@@ -0,0 +1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H,SAAS;YACX,CAAC;YACD,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACvC,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;YACvE,KAAK,CAAC,IAAI,CAAC;gBACT,IAAI,EAAE,OAAO;gBACb,IAAI,EAAE,SAAS,CAAC,KAAK,CAAC;gBACtB,OAAO,EAAE,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO;gBACtD,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,SAAS,EAAE,KAAK,CAAC,SAAS,IAAI,QAAQ;gBACtC,kBAAkB,EAAE,KAAK,CAAC,SAAS;gBACnC,GAAG,OAAO;aACX,CAAC,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;AACrB,CAAC;AAED,iDAAiD;AACjD,MAAM,UAAU,eAAe,CAC7B,GAAsB,EACtB,OAAyB;IAEzB,OAAO,SAAS,CAAC;QACf,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,KAAK;QAC9B,MAAM,EAAE,OAAO,CAAC,SAAS,CAAC,MAAM;QAChC,SAAS,EAAE,OAAO,CAAC,SAAS,CAAC,IAAI;QACjC,MAAM,EAAE,iBAAiB,CAAC,GAAG,EAAE,OAAO,CAAC,WAAW,CAAC;KACpD,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * The per-layer draw rules of the sketch compositor.
3
+ *
4
+ * The editor's Canvas2D runtime composites through these, and so does the
5
+ * headless renderer (`document.ts`), so a layer's visibility, opacity,
6
+ * placement, transform and effects come out the same in both hosts.
7
+ */
8
+ import type { RenderBounds, RenderContext2D, RenderImageFit, RenderLayerEffect, RenderLayerTransform, RenderPoint, RenderQuad } from "./types.js";
9
+ interface StackLayer {
10
+ id: string;
11
+ type: string;
12
+ visible: boolean;
13
+ opacity: number;
14
+ parentId?: string | null;
15
+ }
16
+ /**
17
+ * Whether a layer is drawn in the flat composite: it and every ancestor group
18
+ * are visible. Hiding a group hides its descendants without changing their
19
+ * own `visible` flags. An isolated layer is always drawn, so solo view shows
20
+ * a hidden layer's pixels.
21
+ */
22
+ export declare function isLayerCompositeVisible<T extends StackLayer>(layers: readonly T[], layer: T, isolatedLayerId: string | null | undefined, layerMap?: ReadonlyMap<string, T>): boolean;
23
+ /**
24
+ * Product of every ancestor group's opacity, multiplied into the layer's own
25
+ * when it is composited. An isolated layer is not dimmed by its folders.
26
+ */
27
+ export declare function getAncestorGroupOpacityProduct<T extends StackLayer>(layers: readonly T[], layer: T, isolatedLayerId: string | null | undefined, layerMap?: ReadonlyMap<string, T>): number;
28
+ /** Document-space top-left where a layer's raster is drawn. */
29
+ export declare function computeCompositeOffset(transform: RenderLayerTransform, rasterBounds: RenderBounds): RenderPoint;
30
+ /**
31
+ * Where to draw a `srcW`×`srcH` image inside a `dstW`×`dstH` layer so it
32
+ * honors an image reference's fit. With no fit the image keeps its own size
33
+ * at the origin, as a layer's own serialized raster does.
34
+ */
35
+ export declare function fitImageRect(srcW: number, srcH: number, dstW: number, dstH: number, fit: RenderImageFit | undefined): RenderBounds;
36
+ /** Draw `source` warped onto a document-space quad as two affine triangles. */
37
+ export declare function drawImageToQuad(ctx: RenderContext2D, source: {
38
+ width: number;
39
+ height: number;
40
+ }, quad: RenderQuad): void;
41
+ /**
42
+ * Draw a layer raster into `ctx` at its composite offset, applying the layer
43
+ * transform. `drawable` is what is actually sampled at `source`'s size: a
44
+ * vector layer passes its decoded SVG so a scaled draw redraws the geometry
45
+ * instead of magnifying the buffer.
46
+ */
47
+ export declare function drawWithTransform(ctx: RenderContext2D, source: {
48
+ width: number;
49
+ height: number;
50
+ }, compositeOffset: RenderPoint, transform: RenderLayerTransform, drawable?: unknown): void;
51
+ /** Whether any effect in the stack is switched on. */
52
+ export declare function hasEnabledEffects(effects: readonly RenderLayerEffect[] | null | undefined): boolean;
53
+ /**
54
+ * The CSS filter chain that evaluates a layer's effect stack, or null when no
55
+ * enabled effect changes anything.
56
+ */
57
+ export declare function layerEffectsFilter(effects: readonly RenderLayerEffect[] | null | undefined): string | null;
58
+ export {};
59
+ //# sourceMappingURL=compose.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../src/render/compose.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,WAAW,EACX,UAAU,EACX,MAAM,YAAY,CAAC;AAKpB,UAAU,UAAU;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAgBD;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,UAAU,EAC1D,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,KAAK,EAAE,CAAC,EACR,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC1C,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,GAChC,OAAO,CAoBT;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAAC,CAAC,SAAS,UAAU,EACjE,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,KAAK,EAAE,CAAC,EACR,eAAe,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC1C,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,GAChC,MAAM,CAkBR;AASD,+DAA+D;AAC/D,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,oBAAoB,EAC/B,YAAY,EAAE,YAAY,GACzB,WAAW,CAUb;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,cAAc,GAAG,SAAS,GAC9B,YAAY,CAYd;AA6CD,+EAA+E;AAC/E,wBAAgB,eAAe,CAC7B,GAAG,EAAE,eAAe,EACpB,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EACzC,IAAI,EAAE,UAAU,GACf,IAAI,CAON;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EACzC,eAAe,EAAE,WAAW,EAC5B,SAAS,EAAE,oBAAoB,EAC/B,QAAQ,GAAE,OAAgB,GACzB,IAAI,CAuCN;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,SAAS,iBAAiB,EAAE,GAAG,IAAI,GAAG,SAAS,GACvD,OAAO,CAET;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,SAAS,iBAAiB,EAAE,GAAG,IAAI,GAAG,SAAS,GACvD,MAAM,GAAG,IAAI,CA2Cf"}
@@ -0,0 +1,231 @@
1
+ /**
2
+ * The per-layer draw rules of the sketch compositor.
3
+ *
4
+ * The editor's Canvas2D runtime composites through these, and so does the
5
+ * headless renderer (`document.ts`), so a layer's visibility, opacity,
6
+ * placement, transform and effects come out the same in both hosts.
7
+ */
8
+ /** Maximum group nesting walked, so a parent cycle in corrupt data ends. */
9
+ const MAX_LAYER_DEPTH = 20;
10
+ function parentOf(layers, layer, layerMap) {
11
+ const parentId = layer.parentId;
12
+ if (!parentId) {
13
+ return undefined;
14
+ }
15
+ return layerMap
16
+ ? layerMap.get(parentId)
17
+ : layers.find((candidate) => candidate.id === parentId);
18
+ }
19
+ /**
20
+ * Whether a layer is drawn in the flat composite: it and every ancestor group
21
+ * are visible. Hiding a group hides its descendants without changing their
22
+ * own `visible` flags. An isolated layer is always drawn, so solo view shows
23
+ * a hidden layer's pixels.
24
+ */
25
+ export function isLayerCompositeVisible(layers, layer, isolatedLayerId, layerMap) {
26
+ if (isolatedLayerId && layer.id === isolatedLayerId) {
27
+ return true;
28
+ }
29
+ if (!layer.visible) {
30
+ return false;
31
+ }
32
+ let current = layer;
33
+ let depth = 0;
34
+ while (current.parentId) {
35
+ if (depth++ > MAX_LAYER_DEPTH) {
36
+ break;
37
+ }
38
+ const parent = parentOf(layers, current, layerMap);
39
+ if (!parent || !parent.visible) {
40
+ return false;
41
+ }
42
+ current = parent;
43
+ }
44
+ return true;
45
+ }
46
+ /**
47
+ * Product of every ancestor group's opacity, multiplied into the layer's own
48
+ * when it is composited. An isolated layer is not dimmed by its folders.
49
+ */
50
+ export function getAncestorGroupOpacityProduct(layers, layer, isolatedLayerId, layerMap) {
51
+ if (isolatedLayerId && layer.id === isolatedLayerId) {
52
+ return 1;
53
+ }
54
+ let product = 1;
55
+ let current = layer;
56
+ let depth = 0;
57
+ while (current.parentId && depth++ <= MAX_LAYER_DEPTH) {
58
+ const parent = parentOf(layers, current, layerMap);
59
+ if (!parent) {
60
+ break;
61
+ }
62
+ if (parent.type === "group") {
63
+ product *= parent.opacity;
64
+ }
65
+ current = parent;
66
+ }
67
+ return product;
68
+ }
69
+ function quadExtents(quad) {
70
+ return {
71
+ x: Math.min(quad[0].x, quad[1].x, quad[2].x, quad[3].x),
72
+ y: Math.min(quad[0].y, quad[1].y, quad[2].y, quad[3].y)
73
+ };
74
+ }
75
+ /** Document-space top-left where a layer's raster is drawn. */
76
+ export function computeCompositeOffset(transform, rasterBounds) {
77
+ switch (transform.kind) {
78
+ case "affine":
79
+ return {
80
+ x: transform.x + rasterBounds.x,
81
+ y: transform.y + rasterBounds.y
82
+ };
83
+ case "quad":
84
+ return quadExtents(transform.quad);
85
+ }
86
+ }
87
+ /**
88
+ * Where to draw a `srcW`×`srcH` image inside a `dstW`×`dstH` layer so it
89
+ * honors an image reference's fit. With no fit the image keeps its own size
90
+ * at the origin, as a layer's own serialized raster does.
91
+ */
92
+ export function fitImageRect(srcW, srcH, dstW, dstH, fit) {
93
+ if (!fit || srcW <= 0 || srcH <= 0) {
94
+ return { x: 0, y: 0, width: srcW, height: srcH };
95
+ }
96
+ if (fit === "fill") {
97
+ return { x: 0, y: 0, width: dstW, height: dstH };
98
+ }
99
+ const pick = fit === "contain" ? Math.min : Math.max;
100
+ const scale = pick(dstW / srcW, dstH / srcH);
101
+ const width = srcW * scale;
102
+ const height = srcH * scale;
103
+ return { x: (dstW - width) / 2, y: (dstH - height) / 2, width, height };
104
+ }
105
+ function triangleTransform(src, dst) {
106
+ const [s0, s1, s2] = src;
107
+ const [d0, d1, d2] = dst;
108
+ const det = (s1.x - s0.x) * (s2.y - s0.y) - (s2.x - s0.x) * (s1.y - s0.y);
109
+ if (Math.abs(det) <= 1e-6) {
110
+ return null;
111
+ }
112
+ const a = ((d1.x - d0.x) * (s2.y - s0.y) - (d2.x - d0.x) * (s1.y - s0.y)) / det;
113
+ const b = ((d1.y - d0.y) * (s2.y - s0.y) - (d2.y - d0.y) * (s1.y - s0.y)) / det;
114
+ const c = ((d2.x - d0.x) * (s1.x - s0.x) - (d1.x - d0.x) * (s2.x - s0.x)) / det;
115
+ const d = ((d2.y - d0.y) * (s1.x - s0.x) - (d1.y - d0.y) * (s2.x - s0.x)) / det;
116
+ const e = d0.x - a * s0.x - c * s0.y;
117
+ const f = d0.y - b * s0.x - d * s0.y;
118
+ return [a, b, c, d, e, f];
119
+ }
120
+ function drawImageTriangle(ctx, source, src, dst) {
121
+ const transform = triangleTransform(src, dst);
122
+ if (!transform) {
123
+ return;
124
+ }
125
+ ctx.save();
126
+ ctx.beginPath();
127
+ ctx.moveTo(dst[0].x, dst[0].y);
128
+ ctx.lineTo(dst[1].x, dst[1].y);
129
+ ctx.lineTo(dst[2].x, dst[2].y);
130
+ ctx.closePath();
131
+ ctx.clip();
132
+ ctx.setTransform(...transform);
133
+ ctx.drawImage(source, 0, 0);
134
+ ctx.restore();
135
+ }
136
+ /** Draw `source` warped onto a document-space quad as two affine triangles. */
137
+ export function drawImageToQuad(ctx, source, quad) {
138
+ const tl = { x: 0, y: 0 };
139
+ const tr = { x: source.width, y: 0 };
140
+ const br = { x: source.width, y: source.height };
141
+ const bl = { x: 0, y: source.height };
142
+ drawImageTriangle(ctx, source, [tl, tr, br], [quad[0], quad[1], quad[2]]);
143
+ drawImageTriangle(ctx, source, [tl, br, bl], [quad[0], quad[2], quad[3]]);
144
+ }
145
+ /**
146
+ * Draw a layer raster into `ctx` at its composite offset, applying the layer
147
+ * transform. `drawable` is what is actually sampled at `source`'s size: a
148
+ * vector layer passes its decoded SVG so a scaled draw redraws the geometry
149
+ * instead of magnifying the buffer.
150
+ */
151
+ export function drawWithTransform(ctx, source, compositeOffset, transform, drawable = source) {
152
+ switch (transform.kind) {
153
+ case "quad":
154
+ drawImageToQuad(ctx, source, transform.quad);
155
+ return;
156
+ case "affine": {
157
+ const { scaleX, scaleY, rotation } = transform;
158
+ if (scaleX !== 1 || scaleY !== 1 || rotation !== 0) {
159
+ ctx.translate(compositeOffset.x + source.width / 2, compositeOffset.y + source.height / 2);
160
+ ctx.rotate(rotation);
161
+ ctx.scale(scaleX, scaleY);
162
+ if (drawable === source) {
163
+ ctx.drawImage(source, -source.width / 2, -source.height / 2);
164
+ }
165
+ else {
166
+ ctx.drawImage(drawable, -source.width / 2, -source.height / 2, source.width, source.height);
167
+ }
168
+ }
169
+ else if (drawable === source) {
170
+ ctx.drawImage(source, compositeOffset.x, compositeOffset.y);
171
+ }
172
+ else {
173
+ ctx.drawImage(drawable, compositeOffset.x, compositeOffset.y, source.width, source.height);
174
+ }
175
+ return;
176
+ }
177
+ }
178
+ }
179
+ /** Whether any effect in the stack is switched on. */
180
+ export function hasEnabledEffects(effects) {
181
+ return !!effects && effects.some((effect) => effect.enabled);
182
+ }
183
+ /**
184
+ * The CSS filter chain that evaluates a layer's effect stack, or null when no
185
+ * enabled effect changes anything.
186
+ */
187
+ export function layerEffectsFilter(effects) {
188
+ if (!effects) {
189
+ return null;
190
+ }
191
+ const parts = [];
192
+ for (const effect of effects) {
193
+ if (!effect.enabled) {
194
+ continue;
195
+ }
196
+ switch (effect.type) {
197
+ case "brightness_contrast": {
198
+ const { brightness, contrast } = effect.params;
199
+ if (brightness !== 0) {
200
+ parts.push(`brightness(${1 + brightness})`);
201
+ }
202
+ if (contrast !== 0) {
203
+ parts.push(`contrast(${1 + contrast})`);
204
+ }
205
+ break;
206
+ }
207
+ case "hue_saturation": {
208
+ const { hueDegrees, saturation, lightness } = effect.params;
209
+ if (hueDegrees !== 0) {
210
+ parts.push(`hue-rotate(${hueDegrees}deg)`);
211
+ }
212
+ if (saturation !== 0) {
213
+ parts.push(`saturate(${1 + saturation})`);
214
+ }
215
+ if (lightness !== 0) {
216
+ parts.push(`brightness(${1 + lightness})`);
217
+ }
218
+ break;
219
+ }
220
+ case "exposure": {
221
+ const { exposureStops } = effect.params;
222
+ if (exposureStops !== 0) {
223
+ parts.push(`brightness(${Math.pow(2, exposureStops)})`);
224
+ }
225
+ break;
226
+ }
227
+ }
228
+ }
229
+ return parts.length > 0 ? parts.join(" ") : null;
230
+ }
231
+ //# sourceMappingURL=compose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.js","sourceRoot":"","sources":["../../src/render/compose.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAYH,4EAA4E;AAC5E,MAAM,eAAe,GAAG,EAAE,CAAC;AAU3B,SAAS,QAAQ,CACf,MAAoB,EACpB,KAAQ,EACR,QAA4C;IAE5C,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,C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E,GAAG,MAAM,CAAC,MAAM,CAAC;gBAC5D,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;oBACrB,KAAK,CAAC,IAAI,CAAC,cAAc,UAAU,MAAM,CAAC,CAAC;gBAC7C,CAAC;gBACD,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;oBACrB,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC;gBAC5C,CAAC;gBACD,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC;oBACpB,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC;gBAC7C,CAAC;gBACD,MAAM;YACR,CAAC;YACD,KAAK,UAAU,EAAE,CAAC;gBAChB,MAAM,EAAE,aAAa,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC;gBACxC,IAAI,aAAa,KAAK,CAAC,EAAE,CAAC;oBACxB,KAAK,CAAC,IAAI,CAAC,cAAc,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;gBAC1D,CAAC;gBACD,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACnD,CAAC"}
@@ -0,0 +1,79 @@
1
+ /**
2
+ * One-shot compositing of a whole sketch document, for hosts without the
3
+ * live editor: the headless renderer behind `render_sketch` and
4
+ * `nodetool sketch render`.
5
+ *
6
+ * It does what the editor does across its hydration hook and Canvas2D
7
+ * runtime, in one pass: each layer's raster is decoded into a surface the
8
+ * size of its stored bounds (an image reference is fitted into it), then the
9
+ * stack is composited with the shared draw rules in `compose.ts`. The result
10
+ * is what the editor's PNG export produces for the same document.
11
+ *
12
+ * Decoding image bytes is the host's job, so this module stays free of any
13
+ * canvas library and of storage access.
14
+ */
15
+ import type { RenderDocument, RenderImage, RenderLayer, RenderSurface } from "./types.js";
16
+ /** What the compositor needs from the host canvas implementation. */
17
+ export interface SketchRenderHost {
18
+ createSurface(width: number, height: number): RenderSurface;
19
+ /**
20
+ * Decode a layer image source: a data URL, an `asset://` locator, or an
21
+ * http(s) URL. Resolve null, or throw, when it cannot be read; the layer is
22
+ * then reported as skipped and drawn empty.
23
+ */
24
+ loadImage(source: string): Promise<RenderImage>;
25
+ /** Canvas2D `globalCompositeOperation` for a persisted blend mode. */
26
+ compositeOperation(blendMode: string): string;
27
+ /**
28
+ * Whether `ctx.filter` takes CSS filter functions. When false, layer
29
+ * effects are skipped and named in the report instead of drawn wrong.
30
+ */
31
+ supportsFilter: boolean;
32
+ /**
33
+ * Decode an SVG at a given pixel size. A browser image redraws an SVG at
34
+ * whatever size it is drawn, so a browser host leaves this out; a host whose
35
+ * decoded SVG is a fixed raster supplies it, so a vector layer scaled up by
36
+ * its transform stays sharp instead of magnifying its stored size.
37
+ */
38
+ loadVectorImage?(source: string, width: number, height: number): Promise<RenderImage>;
39
+ }
40
+ export interface SketchRenderOptions {
41
+ /** Output size. One dimension alone keeps the canvas aspect ratio. */
42
+ width?: number;
43
+ height?: number;
44
+ /** CSS color painted under the composite. Omitted: transparent, as the editor exports. */
45
+ background?: string;
46
+ /**
47
+ * Render this one layer instead of the flattened stack: its transform and
48
+ * effects, without its opacity or blend mode, whether or not it is visible.
49
+ * This is what the editor's PSD export and mask export draw per layer.
50
+ */
51
+ layerId?: string;
52
+ }
53
+ export interface SketchRenderLayerReport {
54
+ id: string;
55
+ name: string;
56
+ type: RenderLayer["type"];
57
+ /** Whether the layer took part in this render. */
58
+ drawn: boolean;
59
+ /** Why a layer with content drew nothing, or drew without its effects. */
60
+ issue?: string;
61
+ }
62
+ export interface SketchRenderResult {
63
+ surface: RenderSurface;
64
+ width: number;
65
+ height: number;
66
+ /** The document's own canvas size. */
67
+ canvasWidth: number;
68
+ canvasHeight: number;
69
+ document: RenderDocument;
70
+ /** Bottom of the stack first. */
71
+ layers: SketchRenderLayerReport[];
72
+ }
73
+ /**
74
+ * Render a persisted sketch document: the flattened composite by default, or
75
+ * one layer with `options.layerId`. `raw` is `ImageDocumentData` (with its
76
+ * `sketch` key) or the sketch document itself.
77
+ */
78
+ export declare function renderSketchDocument(raw: unknown, host: SketchRenderHost, options?: SketchRenderOptions): Promise<SketchRenderResult>;
79
+ //# sourceMappingURL=document.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../../src/render/document.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAaH,OAAO,KAAK,EAEV,cAAc,EACd,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,YAAY,CAAC;AAEpB,qEAAqE;AACrE,MAAM,WAAW,gBAAgB;IAC/B,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC5D;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAChD,sEAAsE;IACtE,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9C;;;OAGG;IACH,cAAc,EAAE,OAAO,CAAC;IACxB;;;;;OAKG;IACH,eAAe,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACvF;AAED,MAAM,WAAW,mBAAmB;IAClC,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0FAA0F;IAC1F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,uBAAuB;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,E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