@ggraphic/editor 0.1.0-alpha.4 → 0.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CanvasEditorProvider.d.ts +6 -1
- package/dist/collab/useYjsPageStateSync.d.ts +1 -1
- package/dist/collab/useYjsPageStateSync.js +5 -2
- package/dist/components/CanvasEditor/CanvasEditorShell.js +139 -36
- package/dist/components/CanvasEditor/editorShell.d.ts +6 -0
- package/dist/components/CanvasEditor/types.d.ts +28 -0
- package/dist/components/GenericCanvas.d.ts +19 -2
- package/dist/components/GenericCanvas.js +30 -5
- package/dist/components/PagePreview.d.ts +8 -1
- package/dist/components/PagePreview.js +4 -1
- package/dist/components/TopBar/DownloadSection.d.ts +24 -3
- package/dist/components/TopBar/DownloadSection.js +45 -7
- package/dist/components/TopBar/ShareDropdown.d.ts +4 -2
- package/dist/components/TopBar/ShareDropdown.js +2 -2
- package/dist/entries/export.d.ts +14 -1
- package/dist/entries/export.js +14 -1
- package/dist/entries/services.d.ts +1 -1
- package/dist/export/fonts/fontFiles.d.ts +100 -0
- package/dist/export/fonts/fontFiles.js +333 -0
- package/dist/export/fonts/glyphs.d.ts +113 -0
- package/dist/export/fonts/glyphs.js +120 -0
- package/dist/export/fonts/variation.d.ts +23 -0
- package/dist/export/fonts/variation.js +43 -0
- package/dist/export/pdf/pdfContent.d.ts +108 -0
- package/dist/export/pdf/pdfContent.js +566 -0
- package/dist/export/pdf/pdfFontkit.d.ts +54 -0
- package/dist/export/pdf/pdfFontkit.js +159 -0
- package/dist/export/pdf/pdfGeometry.d.ts +54 -0
- package/dist/export/pdf/pdfGeometry.js +228 -0
- package/dist/export/pdf/pdfResources.d.ts +109 -0
- package/dist/export/pdf/pdfResources.js +314 -0
- package/dist/export/pdf/writePdf.d.ts +34 -0
- package/dist/export/pdf/writePdf.js +165 -0
- package/dist/export/pptx/exportPptxDeck.d.ts +10 -2
- package/dist/export/pptx/exportPptxDeck.js +5 -2
- package/dist/export/pptx/svgPicture.d.ts +19 -0
- package/dist/export/pptx/svgPicture.js +64 -0
- package/dist/export/pptx/vectorizeElement.d.ts +9 -0
- package/dist/export/pptx/vectorizeElement.js +118 -0
- package/dist/export/pptx/writePptx.d.ts +13 -0
- package/dist/export/pptx/writePptx.js +17 -0
- package/dist/export/printPdf.d.ts +1 -0
- package/dist/export/printPdf.js +1 -1
- package/dist/export/scene/browserDeps.d.ts +32 -0
- package/dist/export/scene/browserDeps.js +174 -0
- package/dist/export/scene/imageBytes.d.ts +79 -0
- package/dist/export/scene/imageBytes.js +330 -0
- package/dist/export/scene/itemBounds.d.ts +9 -0
- package/dist/export/scene/itemBounds.js +40 -0
- package/dist/export/scene/matrix.d.ts +13 -0
- package/dist/export/scene/matrix.js +32 -0
- package/dist/export/scene/rasterLimit.d.ts +13 -0
- package/dist/export/scene/rasterLimit.js +24 -0
- package/dist/export/scene/rebaseOps.d.ts +8 -0
- package/dist/export/scene/rebaseOps.js +51 -0
- package/dist/export/scene/recordPageScene.d.ts +28 -0
- package/dist/export/scene/recordPageScene.js +224 -0
- package/dist/export/scene/recordingCanvas.d.ts +55 -0
- package/dist/export/scene/recordingCanvas.js +71 -0
- package/dist/export/scene/recordingContext.d.ts +162 -0
- package/dist/export/scene/recordingContext.js +1031 -0
- package/dist/export/scene/replayScene.d.ts +31 -0
- package/dist/export/scene/replayScene.js +213 -0
- package/dist/export/scene/resolveScene.d.ts +48 -0
- package/dist/export/scene/resolveScene.js +236 -0
- package/dist/export/scene/trustedSvgImages.d.ts +3 -0
- package/dist/export/scene/trustedSvgImages.js +21 -0
- package/dist/export/scene/types.d.ts +241 -0
- package/dist/export/scene/types.js +38 -0
- package/dist/export/slidesPdf.d.ts +10 -2
- package/dist/export/slidesPdf.js +8 -4
- package/dist/export/svg/__fixtures__/barChart.svg +1 -0
- package/dist/export/svg/__golden__/fixture-editable.svg +87 -0
- package/dist/export/svg/__golden__/fixture-outlines.svg +88 -0
- package/dist/export/svg/inlineSvg.d.ts +10 -0
- package/dist/export/svg/inlineSvg.js +119 -0
- package/dist/export/svg/scopeCss.d.ts +45 -0
- package/dist/export/svg/scopeCss.js +214 -0
- package/dist/export/svg/svgColor.d.ts +8 -0
- package/dist/export/svg/svgColor.js +46 -0
- package/dist/export/svg/svgImageScene.d.ts +41 -0
- package/dist/export/svg/svgImageScene.js +128 -0
- package/dist/export/svg/svgPathData.d.ts +31 -0
- package/dist/export/svg/svgPathData.js +466 -0
- package/dist/export/svg/svgToScene.d.ts +19 -0
- package/dist/export/svg/svgToScene.js +1183 -0
- package/dist/export/svg/writeSvg.d.ts +33 -0
- package/dist/export/svg/writeSvg.js +594 -0
- package/dist/export/svg/xml.d.ts +23 -0
- package/dist/export/svg/xml.js +49 -0
- package/dist/export/vectorDeps.d.ts +27 -0
- package/dist/export/vectorDeps.js +37 -0
- package/dist/export/vectorExport.d.ts +85 -0
- package/dist/export/vectorExport.js +287 -0
- package/dist/hostServices.d.ts +15 -0
- package/dist/i18n/de.d.ts +26 -9
- package/dist/i18n/de.js +26 -10
- package/dist/i18n/en.js +24 -0
- package/dist/primitives/AssetPrimitive.js +7 -2
- package/dist/primitives/CanvasIcons.js +6 -0
- package/dist/primitives/CanvasStage.js +2 -2
- package/dist/primitives/ChartPrimitive.d.ts +11 -1
- package/dist/primitives/ChartPrimitive.js +73 -19
- package/dist/primitives/IconPrimitive.js +3 -0
- package/dist/primitives/IllustrationPrimitive.js +14 -1
- package/dist/primitives/chartFont.d.ts +30 -0
- package/dist/primitives/chartFont.js +127 -0
- package/dist/sidebar/sections/ImageAdjustSection.js +5 -4
- package/dist/sidebar/sections/assets/ElementsPanel.js +4 -2
- package/dist/sidebar/sections/assets/assetAudience.d.ts +12 -0
- package/dist/sidebar/sections/assets/assetAudience.js +18 -0
- package/dist/sidebar/sections/assets/useElementCatalog.js +3 -2
- package/dist/stores/createCanvasEditorStore.d.ts +5 -2
- package/dist/stores/createCanvasEditorStore.js +9 -5
- package/dist/styles/compiled.css +1 -1
- package/dist/utils/canvasAssets.d.ts +11 -1
- package/dist/utils/canvasAssets.js +8 -0
- package/dist/utils/canvasIcons.js +5 -3
- package/dist/utils/captureStage.d.ts +19 -0
- package/dist/utils/captureStage.js +14 -1
- package/dist/utils/externalIcons.d.ts +7 -0
- package/dist/utils/externalIcons.js +70 -3
- package/dist/utils/illustrations/svgCache.js +6 -1
- package/dist/utils/imageFilters.d.ts +13 -0
- package/dist/utils/imageFilters.js +11 -0
- package/dist/utils/rebaseHistoryState.d.ts +9 -0
- package/dist/utils/rebaseHistoryState.js +26 -0
- package/dist/utils/zipPageImages.d.ts +8 -0
- package/dist/utils/zipPageImages.js +16 -4
- package/package.json +6 -2
- package/skills/ggraphic-embed/SKILL.md +33 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Draws a scene (`types.ts`) onto a 2D canvas context.
|
|
3
|
+
*
|
|
4
|
+
* Writers do not use this: it is the proof that the recorder lost nothing. Replaying a recorded
|
|
5
|
+
* page onto a canvas has to give the pixels Konva itself paints, and `replayDifferential.vitest.tsx`
|
|
6
|
+
* compares the two. Every op is replayed from the state it carries (matrix, alpha, clips, shadow),
|
|
7
|
+
* so the context is saved and restored around each one and left as it was found.
|
|
8
|
+
*/
|
|
9
|
+
import type { RecordedScene, SceneImageSource, SceneItem } from './types.js';
|
|
10
|
+
export interface ReplayOptions {
|
|
11
|
+
/** Replay only the items this accepts; all by default. */
|
|
12
|
+
items?: (item: SceneItem) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Decoded images for the sources a scene cannot draw by itself: `svg` and `bitmap` image refs,
|
|
15
|
+
* and the `png` of a `raster` op (keyed by the bytes object). `element` refs draw the element.
|
|
16
|
+
* A source missing here is not drawn.
|
|
17
|
+
*/
|
|
18
|
+
images?: ReadonlyMap<SceneImageSource | Uint8Array, CanvasImageSource>;
|
|
19
|
+
/**
|
|
20
|
+
* A blank canvas for a `group` op, which is composited from a buffer. By default an
|
|
21
|
+
* `OffscreenCanvas`, else a canvas element of the context's document.
|
|
22
|
+
*/
|
|
23
|
+
createCanvas?: (width: number, height: number) => CanvasImageSource & ReplayCanvas;
|
|
24
|
+
}
|
|
25
|
+
/** What replay needs of the buffer canvas it draws a group into. */
|
|
26
|
+
export interface ReplayCanvas {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
getContext: (id: '2d') => CanvasRenderingContext2D | null;
|
|
30
|
+
}
|
|
31
|
+
export declare function replayScene(ctx: CanvasRenderingContext2D, scene: RecordedScene, options?: ReplayOptions): void;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
export function replayScene(ctx, scene, options = {}) {
|
|
2
|
+
for (const item of scene.items) {
|
|
3
|
+
if (options.items && !options.items(item))
|
|
4
|
+
continue;
|
|
5
|
+
for (const op of item.ops)
|
|
6
|
+
replayOp(ctx, op, options, scene);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function replayOp(ctx, op, options, scene) {
|
|
10
|
+
ctx.save();
|
|
11
|
+
try {
|
|
12
|
+
applyClips(ctx, op.clips);
|
|
13
|
+
ctx.setTransform(...op.matrix);
|
|
14
|
+
ctx.globalAlpha = op.alpha;
|
|
15
|
+
applyShadow(ctx, op);
|
|
16
|
+
switch (op.kind) {
|
|
17
|
+
case 'path':
|
|
18
|
+
tracePath(ctx, op.commands);
|
|
19
|
+
paintPath(ctx, op.mode, op.paint, op.stroke, op.rule, options);
|
|
20
|
+
break;
|
|
21
|
+
case 'text':
|
|
22
|
+
replayText(ctx, op, options);
|
|
23
|
+
break;
|
|
24
|
+
case 'image': {
|
|
25
|
+
const image = resolveImage(op.image, options);
|
|
26
|
+
if (image) {
|
|
27
|
+
ctx.drawImage(image, op.sx, op.sy, op.sw, op.sh, op.dx, op.dy, op.dw, op.dh);
|
|
28
|
+
}
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case 'group':
|
|
32
|
+
replayGroup(ctx, op.ops, options, scene);
|
|
33
|
+
break;
|
|
34
|
+
case 'raster': {
|
|
35
|
+
const png = op.png && options.images?.get(op.png);
|
|
36
|
+
if (png)
|
|
37
|
+
ctx.drawImage(png, op.x, op.y, op.width, op.height);
|
|
38
|
+
// An island without bytes (or whose bytes were not decoded) falls back to its ops.
|
|
39
|
+
else if (op.source)
|
|
40
|
+
for (const inner of op.source)
|
|
41
|
+
replayOp(ctx, inner, options, scene);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
ctx.restore();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** The clips, each in the space it was made in, then back to the identity for the caller. */
|
|
51
|
+
function applyClips(ctx, clips) {
|
|
52
|
+
for (const clip of clips) {
|
|
53
|
+
ctx.setTransform(...clip.matrix);
|
|
54
|
+
tracePath(ctx, clip.commands);
|
|
55
|
+
ctx.clip(clip.rule);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function applyShadow(ctx, op) {
|
|
59
|
+
if (!op.shadow)
|
|
60
|
+
return;
|
|
61
|
+
ctx.shadowColor = rgba(op.shadow.hex, op.shadow.alpha);
|
|
62
|
+
ctx.shadowBlur = op.shadow.blur;
|
|
63
|
+
ctx.shadowOffsetX = op.shadow.offsetX;
|
|
64
|
+
ctx.shadowOffsetY = op.shadow.offsetY;
|
|
65
|
+
}
|
|
66
|
+
function tracePath(ctx, commands) {
|
|
67
|
+
ctx.beginPath();
|
|
68
|
+
for (const command of commands) {
|
|
69
|
+
switch (command.op) {
|
|
70
|
+
case 'M':
|
|
71
|
+
ctx.moveTo(command.x, command.y);
|
|
72
|
+
break;
|
|
73
|
+
case 'L':
|
|
74
|
+
ctx.lineTo(command.x, command.y);
|
|
75
|
+
break;
|
|
76
|
+
case 'C':
|
|
77
|
+
ctx.bezierCurveTo(command.x1, command.y1, command.x2, command.y2, command.x, command.y);
|
|
78
|
+
break;
|
|
79
|
+
case 'Q':
|
|
80
|
+
ctx.quadraticCurveTo(command.x1, command.y1, command.x, command.y);
|
|
81
|
+
break;
|
|
82
|
+
case 'Z':
|
|
83
|
+
ctx.closePath();
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function paintPath(ctx, mode, paint, stroke, rule, options) {
|
|
89
|
+
const style = styleOf(ctx, paint, options);
|
|
90
|
+
if (!style)
|
|
91
|
+
return;
|
|
92
|
+
if (mode === 'fill') {
|
|
93
|
+
ctx.fillStyle = style;
|
|
94
|
+
ctx.fill(rule);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
ctx.strokeStyle = style;
|
|
98
|
+
if (stroke)
|
|
99
|
+
applyStroke(ctx, stroke);
|
|
100
|
+
ctx.stroke();
|
|
101
|
+
}
|
|
102
|
+
function applyStroke(ctx, stroke) {
|
|
103
|
+
ctx.lineWidth = stroke.width;
|
|
104
|
+
ctx.lineCap = stroke.cap;
|
|
105
|
+
ctx.lineJoin = stroke.join;
|
|
106
|
+
ctx.miterLimit = stroke.miterLimit;
|
|
107
|
+
ctx.setLineDash(stroke.dash);
|
|
108
|
+
ctx.lineDashOffset = stroke.dashOffset;
|
|
109
|
+
}
|
|
110
|
+
function replayText(ctx, op, options) {
|
|
111
|
+
const style = styleOf(ctx, op.paint, options);
|
|
112
|
+
if (!style)
|
|
113
|
+
return;
|
|
114
|
+
ctx.font = fontString(op.font);
|
|
115
|
+
// `xs` and `y` are absolute for every glyph: the alphabetic baseline, no alignment.
|
|
116
|
+
ctx.textBaseline = 'alphabetic';
|
|
117
|
+
ctx.textAlign = 'left';
|
|
118
|
+
if (op.mode === 'fill')
|
|
119
|
+
ctx.fillStyle = style;
|
|
120
|
+
else {
|
|
121
|
+
ctx.strokeStyle = style;
|
|
122
|
+
if (op.stroke)
|
|
123
|
+
applyStroke(ctx, op.stroke);
|
|
124
|
+
}
|
|
125
|
+
Array.from(op.text).forEach((glyph, index) => {
|
|
126
|
+
const x = op.xs[index];
|
|
127
|
+
if (x === undefined)
|
|
128
|
+
return;
|
|
129
|
+
if (op.mode === 'fill')
|
|
130
|
+
ctx.fillText(glyph, x, op.y);
|
|
131
|
+
else
|
|
132
|
+
ctx.strokeText(glyph, x, op.y);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/** A group is composited from a buffer: its ops, then the buffer with the group's alpha and shadow. */
|
|
136
|
+
function replayGroup(ctx, ops, options, scene) {
|
|
137
|
+
const buffer = createBuffer(ctx, options, scene);
|
|
138
|
+
const bufferCtx = buffer.getContext('2d');
|
|
139
|
+
if (!bufferCtx)
|
|
140
|
+
throw new Error('the replay buffer has no 2D context');
|
|
141
|
+
for (const op of ops)
|
|
142
|
+
replayOp(bufferCtx, op, options, scene);
|
|
143
|
+
// Recorded in page px; the group's own matrix is the one Konva drew the buffer under.
|
|
144
|
+
ctx.drawImage(buffer, 0, 0);
|
|
145
|
+
}
|
|
146
|
+
function createBuffer(ctx, options, scene) {
|
|
147
|
+
const width = Math.ceil(scene.width);
|
|
148
|
+
const height = Math.ceil(scene.height);
|
|
149
|
+
if (options.createCanvas)
|
|
150
|
+
return options.createCanvas(width, height);
|
|
151
|
+
if (typeof OffscreenCanvas !== 'undefined') {
|
|
152
|
+
return new OffscreenCanvas(width, height);
|
|
153
|
+
}
|
|
154
|
+
const document = ctx.canvas.ownerDocument;
|
|
155
|
+
if (document) {
|
|
156
|
+
const canvas = document.createElement('canvas');
|
|
157
|
+
canvas.width = width;
|
|
158
|
+
canvas.height = height;
|
|
159
|
+
return canvas;
|
|
160
|
+
}
|
|
161
|
+
throw new Error('replaying a group needs a canvas: pass options.createCanvas');
|
|
162
|
+
}
|
|
163
|
+
// ---------------------------------------------------------------------------------------------
|
|
164
|
+
// Paint, images, fonts
|
|
165
|
+
function styleOf(ctx, paint, options) {
|
|
166
|
+
switch (paint.type) {
|
|
167
|
+
case 'solid':
|
|
168
|
+
return rgba(paint.hex, paint.alpha);
|
|
169
|
+
case 'linear': {
|
|
170
|
+
const gradient = ctx.createLinearGradient(paint.x0, paint.y0, paint.x1, paint.y1);
|
|
171
|
+
for (const stop of paint.stops)
|
|
172
|
+
gradient.addColorStop(stop.offset, rgba(stop.hex, stop.alpha));
|
|
173
|
+
return gradient;
|
|
174
|
+
}
|
|
175
|
+
case 'radial': {
|
|
176
|
+
const gradient = ctx.createRadialGradient(paint.x0, paint.y0, paint.r0, paint.x1, paint.y1, paint.r1);
|
|
177
|
+
for (const stop of paint.stops)
|
|
178
|
+
gradient.addColorStop(stop.offset, rgba(stop.hex, stop.alpha));
|
|
179
|
+
return gradient;
|
|
180
|
+
}
|
|
181
|
+
case 'pattern': {
|
|
182
|
+
const image = resolveImage(paint.image, options);
|
|
183
|
+
if (!image)
|
|
184
|
+
return null;
|
|
185
|
+
const pattern = ctx.createPattern(image, paint.repeat);
|
|
186
|
+
if (!pattern)
|
|
187
|
+
return null;
|
|
188
|
+
const [a, b, c, d, e, f] = paint.matrix;
|
|
189
|
+
pattern.setTransform({ a, b, c, d, e, f });
|
|
190
|
+
return pattern;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function resolveImage(ref, options) {
|
|
195
|
+
if (ref.kind === 'element')
|
|
196
|
+
return ref.element;
|
|
197
|
+
return options.images?.get(ref) ?? null;
|
|
198
|
+
}
|
|
199
|
+
function rgba(hex, alpha) {
|
|
200
|
+
const n = Number.parseInt(hex.slice(1), 16);
|
|
201
|
+
return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${alpha})`;
|
|
202
|
+
}
|
|
203
|
+
/** The font shorthand, the fallback list included: a missing face falls to the next one. */
|
|
204
|
+
function fontString(font) {
|
|
205
|
+
const families = [font.family, ...(font.fallback ?? [])].map(cssFamily).join(', ');
|
|
206
|
+
return `${font.style} ${font.weight} ${font.size}px ${families}`;
|
|
207
|
+
}
|
|
208
|
+
/** A family that is not a plain identifier (spaces, a digit at the start, quotes) has to be quoted. */
|
|
209
|
+
function cssFamily(family) {
|
|
210
|
+
return /^-?[A-Za-z_][A-Za-z0-9_-]*$/.test(family)
|
|
211
|
+
? family
|
|
212
|
+
: `"${family.replace(/(["\\])/g, '\\$1')}"`;
|
|
213
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a recorded scene into one a writer can use without a page: no DOM image elements, and a
|
|
3
|
+
* PNG behind every raster op (`ExportScene`).
|
|
4
|
+
*
|
|
5
|
+
* - An `element` image ref becomes an SVG or bitmap source (`imageBytes.ts`). A canvas drawn whole
|
|
6
|
+
* becomes a raster op, as the pixels of a Konva cache are an island, not a picture.
|
|
7
|
+
* - A raster op the recorder left without pixels is rendered by the dependency that can: the
|
|
8
|
+
* stage by element id, the Konva node when the item has no id, or a replay of the island's ops.
|
|
9
|
+
* - A source that cannot be read, or an SVG that cannot be inlined faithfully (a foreign one that
|
|
10
|
+
* is drawn with a style sheet, say, which the sanitiser strips), is drawn from the stage
|
|
11
|
+
* instead, as a raster of its whole element. If that fails too the op is removed and the
|
|
12
|
+
* element is listed in `report.missingElements`: an export may lose a picture, it may not lose
|
|
13
|
+
* it silently.
|
|
14
|
+
*
|
|
15
|
+
* Items are resolved one after the other, so the report lists things in the order of the page.
|
|
16
|
+
*/
|
|
17
|
+
import { type ImageByteDeps } from './imageBytes.js';
|
|
18
|
+
import { type ExportScene, type RecordedScene, type SceneOp } from './types.js';
|
|
19
|
+
type Box = {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
};
|
|
25
|
+
export interface ResolveDeps extends ImageByteDeps {
|
|
26
|
+
/** The page this scene is, as the report names it. */
|
|
27
|
+
page: number;
|
|
28
|
+
/**
|
|
29
|
+
* The element drawn from the stage at `pixelRatio` (`rasterizeStageElement`), with the box it
|
|
30
|
+
* covers in page px, or null. The box is only used for an element drawn instead of an image it
|
|
31
|
+
* could not read; an island keeps the box it was recorded with.
|
|
32
|
+
*/
|
|
33
|
+
rasterizeElement: (elementId: string, pixelRatio: number) => Promise<{
|
|
34
|
+
png: Uint8Array;
|
|
35
|
+
box: Box;
|
|
36
|
+
} | null>;
|
|
37
|
+
/**
|
|
38
|
+
* A PNG of the node a raster op kept as `nodeRef`, for an island whose item has no element id
|
|
39
|
+
* to look the node up by.
|
|
40
|
+
*/
|
|
41
|
+
rasterizeNode: (nodeRef: unknown, pixelRatio: number) => Promise<Uint8Array | null>;
|
|
42
|
+
/** PNG of ops replayed at pixelRatio (replayScene onto an offscreen canvas), or null. */
|
|
43
|
+
rasterizeOps: (ops: SceneOp[], box: Box, pixelRatio: number) => Promise<Uint8Array | null>;
|
|
44
|
+
/** print: exportDpi / 25.4 / mmToPx(format, 1); else 2 */
|
|
45
|
+
rasterPixelRatio: number;
|
|
46
|
+
}
|
|
47
|
+
export declare function resolveScene(recorded: RecordedScene, deps: ResolveDeps): Promise<ExportScene>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* Turns a recorded scene into one a writer can use without a page: no DOM image elements, and a
|
|
4
|
+
* PNG behind every raster op (`ExportScene`).
|
|
5
|
+
*
|
|
6
|
+
* - An `element` image ref becomes an SVG or bitmap source (`imageBytes.ts`). A canvas drawn whole
|
|
7
|
+
* becomes a raster op, as the pixels of a Konva cache are an island, not a picture.
|
|
8
|
+
* - A raster op the recorder left without pixels is rendered by the dependency that can: the
|
|
9
|
+
* stage by element id, the Konva node when the item has no id, or a replay of the island's ops.
|
|
10
|
+
* - A source that cannot be read, or an SVG that cannot be inlined faithfully (a foreign one that
|
|
11
|
+
* is drawn with a style sheet, say, which the sanitiser strips), is drawn from the stage
|
|
12
|
+
* instead, as a raster of its whole element. If that fails too the op is removed and the
|
|
13
|
+
* element is listed in `report.missingElements`: an export may lose a picture, it may not lose
|
|
14
|
+
* it silently.
|
|
15
|
+
*
|
|
16
|
+
* Items are resolved one after the other, so the report lists things in the order of the page.
|
|
17
|
+
*/
|
|
18
|
+
import { attempt, sourceFromCanvas, sourceFromUrl, } from './imageBytes.js';
|
|
19
|
+
import { itemBounds } from './itemBounds.js';
|
|
20
|
+
import { IDENTITY } from './matrix.js';
|
|
21
|
+
import { fitPixelRatio } from './rasterLimit.js';
|
|
22
|
+
import { isTrustedSvgImage } from './trustedSvgImages.js';
|
|
23
|
+
import { emptyReport, mergeReports, } from './types.js';
|
|
24
|
+
export async function resolveScene(recorded, deps) {
|
|
25
|
+
// A copy: the recording is not written to, and the arrays are the ones this appends to.
|
|
26
|
+
const report = mergeReports(recorded.report, emptyReport());
|
|
27
|
+
const resolution = { deps, report, sources: new Map() };
|
|
28
|
+
const items = [];
|
|
29
|
+
for (const item of recorded.items) {
|
|
30
|
+
const resolved = await resolveItem(item, resolution);
|
|
31
|
+
if (resolved)
|
|
32
|
+
items.push(resolved);
|
|
33
|
+
}
|
|
34
|
+
return { width: recorded.width, height: recorded.height, items, report };
|
|
35
|
+
}
|
|
36
|
+
async function resolveItem(item, resolution) {
|
|
37
|
+
const state = { elementId: item.elementId, stageFallback: null, missing: false };
|
|
38
|
+
const ops = await resolveOps(item.ops, resolution, state);
|
|
39
|
+
if (state.stageFallback) {
|
|
40
|
+
const island = await drawnElement(item.elementId, resolution, state.stageFallback);
|
|
41
|
+
if (island) {
|
|
42
|
+
resolution.report.rasterIslands.push({
|
|
43
|
+
page: resolution.deps.page,
|
|
44
|
+
elementId: item.elementId,
|
|
45
|
+
reason: state.stageFallback,
|
|
46
|
+
});
|
|
47
|
+
// It shows the whole element, so the ops that did resolve would draw it a second time.
|
|
48
|
+
return { ...item, ops: [island] };
|
|
49
|
+
}
|
|
50
|
+
state.missing = true;
|
|
51
|
+
}
|
|
52
|
+
if (state.missing) {
|
|
53
|
+
resolution.report.missingElements.push({
|
|
54
|
+
page: resolution.deps.page,
|
|
55
|
+
elementId: item.elementId,
|
|
56
|
+
reason: 'image-unreadable',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return ops.length > 0 ? { ...item, ops } : null;
|
|
60
|
+
}
|
|
61
|
+
/** The element as the stage draws it: the last resort for a picture nothing else can read. */
|
|
62
|
+
async function drawnElement(elementId, { deps }, reason) {
|
|
63
|
+
if (elementId === null)
|
|
64
|
+
return null;
|
|
65
|
+
const drawn = await attempt(() => deps.rasterizeElement(elementId, deps.rasterPixelRatio));
|
|
66
|
+
if (!drawn)
|
|
67
|
+
return null;
|
|
68
|
+
return {
|
|
69
|
+
kind: 'raster',
|
|
70
|
+
matrix: IDENTITY,
|
|
71
|
+
alpha: 1,
|
|
72
|
+
clips: [],
|
|
73
|
+
shadow: null,
|
|
74
|
+
png: drawn.png,
|
|
75
|
+
...drawn.box,
|
|
76
|
+
reason,
|
|
77
|
+
source: null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function resolveOps(ops, resolution, state) {
|
|
81
|
+
const resolved = [];
|
|
82
|
+
for (const op of ops) {
|
|
83
|
+
const next = await resolveOp(op, resolution, state);
|
|
84
|
+
if (next)
|
|
85
|
+
resolved.push(next);
|
|
86
|
+
}
|
|
87
|
+
return resolved;
|
|
88
|
+
}
|
|
89
|
+
async function resolveOp(op, resolution, state) {
|
|
90
|
+
switch (op.kind) {
|
|
91
|
+
case 'path':
|
|
92
|
+
case 'text': {
|
|
93
|
+
const paint = await resolvePaint(op.paint, resolution, state);
|
|
94
|
+
return paint ? { ...op, paint } : null;
|
|
95
|
+
}
|
|
96
|
+
case 'image':
|
|
97
|
+
return resolveImage(op, resolution, state);
|
|
98
|
+
case 'group': {
|
|
99
|
+
const ops = await resolveOps(op.ops, resolution, state);
|
|
100
|
+
return ops.length > 0 ? { ...op, ops } : null;
|
|
101
|
+
}
|
|
102
|
+
case 'raster':
|
|
103
|
+
return resolveRaster(op, resolution, state);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function resolvePaint(paint, resolution, state) {
|
|
107
|
+
if (paint.type !== 'pattern')
|
|
108
|
+
return paint;
|
|
109
|
+
const outcome = await resolveRef(paint.image, resolution);
|
|
110
|
+
if (!outcome.ok) {
|
|
111
|
+
state.stageFallback ??= outcome.reason;
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return { ...paint, image: outcome.source };
|
|
115
|
+
}
|
|
116
|
+
async function resolveImage(op, resolution, state) {
|
|
117
|
+
const ref = op.image;
|
|
118
|
+
const outcome = await resolveRef(ref, resolution);
|
|
119
|
+
if (!outcome.ok) {
|
|
120
|
+
// An item without an id (a page background) has no stage element to draw: raster the op.
|
|
121
|
+
if (state.elementId === null)
|
|
122
|
+
return rasterizedImage(op, outcome.reason, resolution, state);
|
|
123
|
+
state.stageFallback ??= outcome.reason;
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const source = outcome.source;
|
|
127
|
+
if (ref.kind === 'element' && isCanvas(ref.element) && source.kind === 'bitmap') {
|
|
128
|
+
const whole = op.sx === 0 && op.sy === 0 && op.sw >= ref.width && op.sh >= ref.height;
|
|
129
|
+
// A raster op has no source rectangle; a canvas drawn in part stays an image.
|
|
130
|
+
if (whole) {
|
|
131
|
+
resolution.report.rasterIslands.push({
|
|
132
|
+
page: resolution.deps.page,
|
|
133
|
+
elementId: state.elementId,
|
|
134
|
+
reason: 'filter',
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
kind: 'raster',
|
|
138
|
+
matrix: op.matrix,
|
|
139
|
+
alpha: op.alpha,
|
|
140
|
+
clips: op.clips,
|
|
141
|
+
shadow: op.shadow,
|
|
142
|
+
png: source.bytes,
|
|
143
|
+
x: op.dx,
|
|
144
|
+
y: op.dy,
|
|
145
|
+
width: op.dw,
|
|
146
|
+
height: op.dh,
|
|
147
|
+
reason: 'filter',
|
|
148
|
+
source: null,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { ...op, image: source };
|
|
153
|
+
}
|
|
154
|
+
/** An image nothing can embed, drawn by the browser from the op itself: an island of its box. */
|
|
155
|
+
async function rasterizedImage(op, reason, resolution, state) {
|
|
156
|
+
const bounds = itemBounds({ elementId: null, label: null, ops: [op] });
|
|
157
|
+
if (!bounds) {
|
|
158
|
+
state.missing = true;
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const island = await resolveRaster({
|
|
162
|
+
kind: 'raster',
|
|
163
|
+
matrix: IDENTITY,
|
|
164
|
+
alpha: 1,
|
|
165
|
+
clips: [],
|
|
166
|
+
shadow: null,
|
|
167
|
+
png: null,
|
|
168
|
+
x: bounds.x0,
|
|
169
|
+
y: bounds.y0,
|
|
170
|
+
width: bounds.x1 - bounds.x0,
|
|
171
|
+
height: bounds.y1 - bounds.y0,
|
|
172
|
+
reason,
|
|
173
|
+
source: [op],
|
|
174
|
+
}, resolution, state);
|
|
175
|
+
if (island) {
|
|
176
|
+
resolution.report.rasterIslands.push({
|
|
177
|
+
page: resolution.deps.page,
|
|
178
|
+
elementId: null,
|
|
179
|
+
reason,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return island;
|
|
183
|
+
}
|
|
184
|
+
async function resolveRaster(op, { deps, report }, state) {
|
|
185
|
+
// `nodeRef` and `source` are recorded forms: the node, and ops that still name DOM elements.
|
|
186
|
+
const { nodeRef, source, ...island } = op;
|
|
187
|
+
let png = op.png;
|
|
188
|
+
if (!png) {
|
|
189
|
+
// A page-sized island at print resolution is more pixels than a canvas may have.
|
|
190
|
+
const fitted = fitPixelRatio(op.width, op.height, deps.rasterPixelRatio);
|
|
191
|
+
const ratio = fitted.pixelRatio;
|
|
192
|
+
if (source) {
|
|
193
|
+
const box = { x: op.x, y: op.y, width: op.width, height: op.height };
|
|
194
|
+
png = await attempt(() => deps.rasterizeOps(source, box, ratio));
|
|
195
|
+
}
|
|
196
|
+
else if (state.elementId !== null) {
|
|
197
|
+
const id = state.elementId;
|
|
198
|
+
png = (await attempt(() => deps.rasterizeElement(id, ratio)))?.png ?? null;
|
|
199
|
+
}
|
|
200
|
+
else if (nodeRef !== undefined) {
|
|
201
|
+
png = await attempt(() => deps.rasterizeNode(nodeRef, ratio));
|
|
202
|
+
}
|
|
203
|
+
if (png && fitted.reduced)
|
|
204
|
+
report.reducedRasters.push({ page: deps.page, elementId: state.elementId });
|
|
205
|
+
}
|
|
206
|
+
if (!png) {
|
|
207
|
+
state.missing = true;
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return { ...island, png, source: null };
|
|
211
|
+
}
|
|
212
|
+
const isCanvas = (element) => typeof element?.toDataURL === 'function';
|
|
213
|
+
function resolveRef(ref, resolution) {
|
|
214
|
+
if (ref.kind !== 'element')
|
|
215
|
+
return Promise.resolve({ ok: true, source: ref });
|
|
216
|
+
const cached = resolution.sources.get(ref.element);
|
|
217
|
+
if (cached)
|
|
218
|
+
return cached;
|
|
219
|
+
const loading = loadElement(ref, resolution.deps);
|
|
220
|
+
resolution.sources.set(ref.element, loading);
|
|
221
|
+
return loading;
|
|
222
|
+
}
|
|
223
|
+
const UNREADABLE = { ok: false, reason: 'image-unreadable' };
|
|
224
|
+
async function loadElement(ref, deps) {
|
|
225
|
+
const element = ref.element;
|
|
226
|
+
const size = { width: ref.width, height: ref.height };
|
|
227
|
+
if (isCanvas(element))
|
|
228
|
+
return sourceFromCanvas(element, size);
|
|
229
|
+
// An `HTMLImageElement`; anything without a url (an `ImageBitmap`, a video) cannot be refetched.
|
|
230
|
+
const { currentSrc, src } = (element ?? {});
|
|
231
|
+
const url = typeof currentSrc === 'string' && currentSrc ? currentSrc : src;
|
|
232
|
+
if (typeof url !== 'string' || url === '')
|
|
233
|
+
return UNREADABLE;
|
|
234
|
+
// Whether this SVG is the editor's own is a fact about who made the element, not about its url.
|
|
235
|
+
return sourceFromUrl(url, size, deps, isTrustedSvgImage(element));
|
|
236
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* Which image elements show an SVG the editor wrote itself.
|
|
4
|
+
*
|
|
5
|
+
* An export inlines an SVG into its own output, and an SVG that came from a document (a user
|
|
6
|
+
* image, an asset, an uploaded logo) is a trust boundary: it goes through `sanitizeSvg`. The
|
|
7
|
+
* editor's own SVGs (charts, icons, illustrations) do not, because the sanitiser strips what they
|
|
8
|
+
* are drawn with (`<style>`, `<text>`).
|
|
9
|
+
*
|
|
10
|
+
* What tells the two apart is who made the element, never what its `src` looks like: a document
|
|
11
|
+
* can write any data URL it likes into a user image's `src`. So the code that builds an image
|
|
12
|
+
* from an SVG it generated marks that element here, and nothing unmarked is trusted.
|
|
13
|
+
*/
|
|
14
|
+
const trusted = new WeakSet();
|
|
15
|
+
/** Call on an image element whose SVG source the editor generated, right where it is made. */
|
|
16
|
+
export function markTrustedSvgImage(img) {
|
|
17
|
+
trusted.add(img);
|
|
18
|
+
}
|
|
19
|
+
export function isTrustedSvgImage(img) {
|
|
20
|
+
return typeof img === 'object' && img !== null && trusted.has(img);
|
|
21
|
+
}
|