@weasel-js/export 1.19.1 → 2.0.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 +8 -5
- package/dist/index.d.ts +56 -22
- package/dist/index.js +80 -26
- package/dist/index.js.map +1 -1
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -19,18 +19,21 @@ on neither.
|
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
21
|
import { placedPagesOf, viewportWorldRect } from '@weasel-js/core';
|
|
22
|
-
import { exportPages, exportRegions,
|
|
22
|
+
import { exportPages, exportRegions, selectionRegionsSource } from '@weasel-js/export';
|
|
23
23
|
|
|
24
24
|
const pages = placedPagesOf(scene);
|
|
25
25
|
const png = await exportPages(scene, [pages[0]], 'png', { scale: 2, background: '#ffffff' });
|
|
26
26
|
const shown = await exportRegions(scene, [viewportWorldRect(view, { width, height })], 'png');
|
|
27
|
-
const picked = await exportRegions(scene,
|
|
27
|
+
const picked = await exportRegions(scene, selectionRegionsSource(selection.get()), 'svg');
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
A region is an `ExportRegion`: `{ x, y, width, height, name? }` in world
|
|
31
31
|
units. `exportPages` makes one of each `PlacedPage`, named as the page order
|
|
32
32
|
shows it; `viewportWorldRect(view, size)` is the one a view shows;
|
|
33
|
-
`selectionRegion(scene, ids)` is the box the ids draw, strokes
|
|
33
|
+
`selectionRegion(scene, ids, { scale })` is the box the ids draw, strokes
|
|
34
|
+
included, measured at the export's `scale`; `selectionRegionsSource(ids)` reads
|
|
35
|
+
that box from the revision the export draws, as `pageRegionsSource` does for
|
|
36
|
+
pages, and fails the export when the selection draws nothing.
|
|
34
37
|
`name` labels a PDF page and titles an SVG.
|
|
35
38
|
|
|
36
39
|
A region is of the document: every format draws each layer at its document
|
|
@@ -58,8 +61,8 @@ only if no content changed meanwhile, or starts again, up to
|
|
|
58
61
|
`EXPORT_CAPTURE_ATTEMPTS` times before rejecting with `ExportRevisionError`.
|
|
59
62
|
Regions may be a function of the document, `(doc) => regions`, read again each
|
|
60
63
|
time, so bounds measured from it belong to the revision drawn: `exportPages`
|
|
61
|
-
places its pages that way, by id, through `
|
|
62
|
-
`
|
|
64
|
+
places its pages that way, by id, through `pageRegionsSource(pages)`, and
|
|
65
|
+
`pageRegionSource(page)` is the same for one page. A format of your own goes through
|
|
63
66
|
`captureExport` too, warming in `warm` and reading everything it writes in
|
|
64
67
|
`read`.
|
|
65
68
|
What an export draws without reaches `onDiagnostic` once each, as an
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Reflection } from '@weasel-js/registry';
|
|
1
|
+
import { Reflection, PushOptions } from '@weasel-js/registry';
|
|
2
2
|
import { Rect } from '@weasel-js/geom';
|
|
3
3
|
import { Scene, RectPose, RenderDiagnostic, PlacedPage, RenderSceneToPixelsArgs, RenderDiagnosticSink } from '@weasel-js/core';
|
|
4
4
|
|
|
@@ -26,10 +26,16 @@ type ExportRegions = readonly ExportRegion[] | ((doc: ExportScene) => readonly E
|
|
|
26
26
|
type ExportRegionSource = ExportRegion | ((doc: ExportScene) => ExportRegion);
|
|
27
27
|
/** A scene as the exports read it: world-space `RectPose`s. */
|
|
28
28
|
type ExportScene = Scene<unknown, string, RectPose>;
|
|
29
|
+
/** The color painted under a region: a color, `null` for none, or a function reading it from the
|
|
30
|
+
* document the export draws, so a color that comes from it belongs to the revision drawn. */
|
|
31
|
+
type ExportBackground = string | null | ((doc: ExportScene) => string | null);
|
|
32
|
+
/** `background` as of `doc`. */
|
|
33
|
+
declare const exportBackgroundOf: (background: ExportBackground | undefined, doc: ExportScene) => string | null;
|
|
29
34
|
/** What every format takes. */
|
|
30
35
|
interface ExportOptions {
|
|
31
|
-
/** The color painted under each region
|
|
32
|
-
|
|
36
|
+
/** The color painted under each region, or a function reading it from the document the export draws.
|
|
37
|
+
* Default none: core holds no page color. */
|
|
38
|
+
background?: ExportBackground;
|
|
33
39
|
/** Hears each thing the export could not draw or load, once: a picture or face
|
|
34
40
|
* that failed, a paint kind or shader nothing can draw, what the format cannot hold. */
|
|
35
41
|
onDiagnostic?: (diagnostic: RenderDiagnostic) => void;
|
|
@@ -53,6 +59,12 @@ interface ExportFormat<TOptions extends ExportOptions = ExportOptions> {
|
|
|
53
59
|
readonly extension: string;
|
|
54
60
|
/** Whether one file holds several regions, as a PDF's pages do. A single-region format takes exactly one. */
|
|
55
61
|
readonly multiRegion: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The option keys it reads beyond {@link ExportOptions}. `exportRegions` warns in development about a
|
|
64
|
+
* key passed to this format that neither lists, and {@link pickExportOptions} picks these. Left out,
|
|
65
|
+
* nothing is checked.
|
|
66
|
+
*/
|
|
67
|
+
readonly options?: readonly Exclude<keyof TOptions, keyof ExportOptions>[];
|
|
56
68
|
export(scene: ExportScene, regions: ExportRegions, opts: TOptions): Promise<Blob>;
|
|
57
69
|
}
|
|
58
70
|
|
|
@@ -68,19 +80,22 @@ declare const exportFormatRegistry: Reflection<ExportFormat>;
|
|
|
68
80
|
/**
|
|
69
81
|
* Register an export format. Returns a disposer that removes this
|
|
70
82
|
* registration. Registering an id already held overrides it, `png` included;
|
|
71
|
-
* disposing uncovers what it displaced.
|
|
83
|
+
* disposing uncovers what it displaced. `fallback` marks an engine default,
|
|
84
|
+
* which a later registration replaces without the development clash warning.
|
|
72
85
|
*/
|
|
73
|
-
declare function registerExportFormat<TOptions extends ExportOptions>(format: ExportFormat<TOptions>): () => void;
|
|
86
|
+
declare function registerExportFormat<TOptions extends ExportOptions>(format: ExportFormat<TOptions>, options?: Pick<PushOptions, 'fallback'>): () => void;
|
|
74
87
|
/** Every registered format, `png` first, then in registration order. The same array until the registry changes. */
|
|
75
88
|
declare function exportFormats(): readonly ExportFormat[];
|
|
76
89
|
/**
|
|
77
|
-
* Write `regions` of `scene` in the format registered as `formatId`.
|
|
78
|
-
* for an id nothing registered,
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* the export reads them.
|
|
90
|
+
* Write `regions` of `scene` in the format registered as `formatId`. Rejects
|
|
91
|
+
* for an id nothing registered, for more or fewer than one region handed to a
|
|
92
|
+
* format that is not `multiRegion` (a multi-region format takes one or more),
|
|
93
|
+
* and with whatever the format throws, synchronously or not. Regions given as
|
|
94
|
+
* a function of the document are counted each time the export reads them.
|
|
82
95
|
*/
|
|
83
96
|
declare function exportRegions(scene: ExportScene, regions: ExportRegions, formatId: string, opts?: ExportOptions & Readonly<Record<string, unknown>>): Promise<Blob>;
|
|
97
|
+
/** The options `format` reads out of `opts`, the shared ones and those it declares, leaving out any undefined. */
|
|
98
|
+
declare function pickExportOptions<TOptions extends ExportOptions>(format: ExportFormat<TOptions>, opts: TOptions): Partial<TOptions>;
|
|
84
99
|
|
|
85
100
|
/** `page` as an export region, named as the page order shows it. */
|
|
86
101
|
declare function pageRegion(page: PlacedPage): ExportRegion;
|
|
@@ -89,12 +104,12 @@ declare function pageRegion(page: PlacedPage): ExportRegion;
|
|
|
89
104
|
* as that document places it, found by its id, and named for it. A page the
|
|
90
105
|
* document no longer holds fails the export.
|
|
91
106
|
*/
|
|
92
|
-
declare function
|
|
93
|
-
/** One page as {@link
|
|
94
|
-
declare function
|
|
107
|
+
declare function pageRegionsSource(pages: readonly PlacedPage[]): (doc: ExportScene) => readonly ExportRegion[];
|
|
108
|
+
/** One page as {@link pageRegionsSource} reads it: the region source of a single-region export. */
|
|
109
|
+
declare function pageRegionSource(page: PlacedPage): (doc: ExportScene) => ExportRegion;
|
|
95
110
|
/**
|
|
96
111
|
* Write `pages` of `scene` in the format registered as `formatId`, each page
|
|
97
|
-
* a region named for the page: `exportRegions` over {@link
|
|
112
|
+
* a region named for the page: `exportRegions` over {@link pageRegionsSource}. A
|
|
98
113
|
* single-region format takes one page.
|
|
99
114
|
*/
|
|
100
115
|
declare function exportPages(scene: ExportScene, pages: readonly PlacedPage[], formatId: string, opts?: ExportOptions & Readonly<Record<string, unknown>>): Promise<Blob>;
|
|
@@ -103,8 +118,9 @@ declare function exportPages(scene: ExportScene, pages: readonly PlacedPage[], f
|
|
|
103
118
|
interface ExportRegionPngOptions extends Pick<ExportOptions, 'onDiagnostic' | 'strict'> {
|
|
104
119
|
/** Output pixels per world unit: 2 makes an image twice the region's size. */
|
|
105
120
|
scale: number;
|
|
106
|
-
/** The color painted under the region
|
|
107
|
-
|
|
121
|
+
/** The color painted under the region, or a function reading it from the document drawn. Default none: the
|
|
122
|
+
* image is transparent where nothing is drawn. */
|
|
123
|
+
background?: ExportBackground;
|
|
108
124
|
}
|
|
109
125
|
/**
|
|
110
126
|
* Draw `region` of `scene` into a PNG, `region × scale` pixels, through the
|
|
@@ -126,12 +142,30 @@ interface PngExportOptions extends ExportOptions {
|
|
|
126
142
|
scale?: number;
|
|
127
143
|
}
|
|
128
144
|
|
|
145
|
+
/** Options for {@link selectionRegion} and its sources. */
|
|
146
|
+
interface SelectionRegionOptions {
|
|
147
|
+
/** What the region is called: a PDF page's label, an SVG's `<title>`. */
|
|
148
|
+
name?: string;
|
|
149
|
+
/** The output pixels per world unit the export draws at, which a screen-pixel (`{ px }`) stroke is
|
|
150
|
+
* measured at. Default 1. Pass the export's `scale` so such a stroke is not cut at its edge. */
|
|
151
|
+
scale?: number;
|
|
152
|
+
}
|
|
129
153
|
/**
|
|
130
154
|
* The world box `ids` cover as the document draws them, strokes included:
|
|
131
155
|
* the region that exports a selection. Pages and the page run are left out of
|
|
132
|
-
* `ids
|
|
156
|
+
* `ids`, and so is what no export draws: a hidden node, and a node on a
|
|
157
|
+
* hidden or annotation layer (what its children draw still counts). `null`
|
|
158
|
+
* when none of them draws anything. A string third argument is the name.
|
|
159
|
+
*/
|
|
160
|
+
declare function selectionRegion(scene: ExportScene, ids: readonly string[], opts?: string | SelectionRegionOptions): ExportRegion | null;
|
|
161
|
+
/**
|
|
162
|
+
* `ids`' region read from the document an export draws, so the bounds belong to
|
|
163
|
+
* the revision drawn in them: the region source of a single-region export. A
|
|
164
|
+
* selection that draws nothing in that document fails the export.
|
|
133
165
|
*/
|
|
134
|
-
declare function
|
|
166
|
+
declare function selectionRegionSource(ids: readonly string[], opts?: SelectionRegionOptions): (doc: ExportScene) => ExportRegion;
|
|
167
|
+
/** {@link selectionRegionSource} as the regions `exportRegions` takes. */
|
|
168
|
+
declare function selectionRegionsSource(ids: readonly string[], opts?: SelectionRegionOptions): (doc: ExportScene) => readonly ExportRegion[];
|
|
135
169
|
|
|
136
170
|
/** What {@link regionRenderArgs} answers: the part of `renderSceneToPixels`'s arguments a region decides. */
|
|
137
171
|
type RegionRenderArgs = Pick<RenderSceneToPixelsArgs<unknown, string, RectPose>, 'scene' | 'sourceRect' | 'scale' | 'background'>;
|
|
@@ -139,8 +173,8 @@ type RegionRenderArgs = Pick<RenderSceneToPixelsArgs<unknown, string, RectPose>,
|
|
|
139
173
|
interface RegionRenderArgsOptions {
|
|
140
174
|
/** Output pixels per world unit. */
|
|
141
175
|
scale: number;
|
|
142
|
-
/** The color painted under the region. Default none: transparent. */
|
|
143
|
-
background?:
|
|
176
|
+
/** The color painted under the region, or a function reading it from the document drawn. Default none: transparent. */
|
|
177
|
+
background?: ExportBackground;
|
|
144
178
|
}
|
|
145
179
|
/**
|
|
146
180
|
* The arguments `region` of `scene` renders with: the scene's document (not
|
|
@@ -217,5 +251,5 @@ interface ExportReporter {
|
|
|
217
251
|
*/
|
|
218
252
|
declare function exportReporter(opts: Pick<ExportOptions, 'onDiagnostic' | 'strict'>): ExportReporter;
|
|
219
253
|
|
|
220
|
-
export { EXPORT_CAPTURE_ATTEMPTS, ExportIncompleteError, ExportRevisionError, captureExport, exportFormatRegistry, exportFormats, exportPages, exportRegionPng, exportRegions, exportRegionsOf, exportReporter, pageRegion,
|
|
221
|
-
export type { ExportCaptureSteps, ExportDiagnostic, ExportFormat, ExportOptions, ExportRegion, ExportRegionPngOptions, ExportRegionSource, ExportRegions, ExportReporter, ExportScene, PngExportOptions, RegionRenderArgs, RegionRenderArgsOptions };
|
|
254
|
+
export { EXPORT_CAPTURE_ATTEMPTS, ExportIncompleteError, ExportRevisionError, captureExport, exportBackgroundOf, exportFormatRegistry, exportFormats, exportPages, exportRegionPng, exportRegions, exportRegionsOf, exportReporter, pageRegion, pageRegionSource, pageRegionsSource, pickExportOptions, regionRenderArgs, regionsOfOne, registerExportFormat, selectionRegion, selectionRegionSource, selectionRegionsSource };
|
|
255
|
+
export type { ExportBackground, ExportCaptureSteps, ExportDiagnostic, ExportFormat, ExportOptions, ExportRegion, ExportRegionPngOptions, ExportRegionSource, ExportRegions, ExportReporter, ExportScene, PngExportOptions, RegionRenderArgs, RegionRenderArgsOptions, SelectionRegionOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createReflectable } from '@weasel-js/registry';
|
|
1
|
+
import { createReflectable, isDev } from '@weasel-js/registry';
|
|
2
2
|
import { holdImageBitmaps } from '@weasel-js/renderer';
|
|
3
|
-
import { documentOf, createRasterSession, rasterToPng, pageName, placedPagesOf, worldPoseLookup, scenePoseAdapter, drawnBoundsScene, withoutPageStructure,
|
|
3
|
+
import { documentOf, createRasterSession, rasterToPng, pageName, placedPagesOf, worldPoseLookup, scenePoseAdapter, drawnBoundsScene, unionAabb, withoutPageStructure, nodeGates, annotationLayerIds, drawnAabbOf, planPixelRender, warmRender } from '@weasel-js/core';
|
|
4
4
|
|
|
5
5
|
// src/exportFormats.ts
|
|
6
6
|
var EXPORT_CAPTURE_ATTEMPTS = 3;
|
|
@@ -58,12 +58,19 @@ function exportReporter(opts) {
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
+
|
|
62
|
+
// src/exportTypes.ts
|
|
63
|
+
var exportBackgroundOf = (background, doc) => typeof background === "function" ? background(doc) : background ?? null;
|
|
64
|
+
|
|
65
|
+
// src/regionRenderArgs.ts
|
|
61
66
|
function regionRenderArgs(scene, region, opts) {
|
|
67
|
+
const doc = documentOf(scene);
|
|
68
|
+
const background = exportBackgroundOf(opts.background, doc);
|
|
62
69
|
return {
|
|
63
|
-
scene:
|
|
70
|
+
scene: doc,
|
|
64
71
|
sourceRect: { x: region.x, y: region.y, width: region.width, height: region.height },
|
|
65
72
|
scale: { x: opts.scale, y: opts.scale },
|
|
66
|
-
...
|
|
73
|
+
...background !== null ? { background } : {}
|
|
67
74
|
};
|
|
68
75
|
}
|
|
69
76
|
|
|
@@ -96,6 +103,7 @@ var PNG_EXPORT = {
|
|
|
96
103
|
mimeType: "image/png",
|
|
97
104
|
extension: "png",
|
|
98
105
|
multiRegion: false,
|
|
106
|
+
options: ["scale"],
|
|
99
107
|
export: (scene, regions, opts) => exportRegionPng(scene, typeof regions === "function" ? (doc) => regions(doc)[0] : regions[0], {
|
|
100
108
|
scale: opts.scale ?? 1,
|
|
101
109
|
...opts.background !== void 0 ? { background: opts.background } : {},
|
|
@@ -105,14 +113,15 @@ var PNG_EXPORT = {
|
|
|
105
113
|
};
|
|
106
114
|
|
|
107
115
|
// src/exportFormats.ts
|
|
108
|
-
var FORMATS = createReflectable();
|
|
116
|
+
var FORMATS = createReflectable({ name: "export formats" });
|
|
109
117
|
var builtInsRegistered = false;
|
|
110
118
|
function ensureBuiltIns() {
|
|
111
119
|
if (builtInsRegistered) return;
|
|
112
120
|
builtInsRegistered = true;
|
|
113
|
-
FORMATS.push(PNG_EXPORT.id, PNG_EXPORT, { source: "kit" });
|
|
121
|
+
FORMATS.push(PNG_EXPORT.id, PNG_EXPORT, { source: "kit", fallback: true });
|
|
114
122
|
}
|
|
115
123
|
var exportFormatRegistry = {
|
|
124
|
+
name: FORMATS.name,
|
|
116
125
|
get: (id) => {
|
|
117
126
|
ensureBuiltIns();
|
|
118
127
|
return FORMATS.get(id);
|
|
@@ -134,9 +143,9 @@ var exportFormatRegistry = {
|
|
|
134
143
|
return FORMATS.getVersion();
|
|
135
144
|
}
|
|
136
145
|
};
|
|
137
|
-
function registerExportFormat(format) {
|
|
146
|
+
function registerExportFormat(format, options) {
|
|
138
147
|
ensureBuiltIns();
|
|
139
|
-
return FORMATS.push(format.id, format);
|
|
148
|
+
return FORMATS.push(format.id, format, options);
|
|
140
149
|
}
|
|
141
150
|
var listed = null;
|
|
142
151
|
function exportFormats() {
|
|
@@ -150,15 +159,33 @@ function exportRegions(scene, regions, formatId, opts = {}) {
|
|
|
150
159
|
const known = exportFormats().map((f) => f.id).join(", ");
|
|
151
160
|
return Promise.reject(new Error(`exportRegions: no export format "${formatId}" is registered (registered: ${known})`));
|
|
152
161
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
162
|
+
warnUnreadOptions(format, opts);
|
|
163
|
+
try {
|
|
164
|
+
if (typeof regions !== "function") return Promise.resolve(format.export(scene, checkCount(format, regions), opts));
|
|
165
|
+
return Promise.resolve(format.export(scene, (doc) => checkCount(format, regions(doc)), opts));
|
|
166
|
+
} catch (err) {
|
|
167
|
+
return Promise.reject(err);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
var SHARED_OPTIONS = ["background", "onDiagnostic", "strict"];
|
|
171
|
+
var warnedOptions = /* @__PURE__ */ new Set();
|
|
172
|
+
function warnUnreadOptions(format, opts) {
|
|
173
|
+
if (!format.options || !isDev()) return;
|
|
174
|
+
const reads = /* @__PURE__ */ new Set([...SHARED_OPTIONS, ...format.options]);
|
|
175
|
+
for (const key of Object.keys(opts)) {
|
|
176
|
+
if (opts[key] === void 0 || reads.has(key) || warnedOptions.has(`${format.id}\0${key}`)) continue;
|
|
177
|
+
warnedOptions.add(`${format.id}\0${key}`);
|
|
178
|
+
console.warn(
|
|
179
|
+
`exportRegions: the "${format.id}" format does not read the option "${key}", so it is ignored (it reads: ${[...reads].join(", ")}). Check the spelling, or whether another format takes it.`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function pickExportOptions(format, opts) {
|
|
184
|
+
const out = {};
|
|
185
|
+
for (const key of [...SHARED_OPTIONS, ...format.options ?? []]) {
|
|
186
|
+
if (opts[key] !== void 0) out[key] = opts[key];
|
|
160
187
|
}
|
|
161
|
-
return
|
|
188
|
+
return out;
|
|
162
189
|
}
|
|
163
190
|
function checkCount(format, regions) {
|
|
164
191
|
if (regions.length === 0) throw new Error(`exportRegions: "${format.id}" was handed no regions`);
|
|
@@ -170,7 +197,7 @@ function checkCount(format, regions) {
|
|
|
170
197
|
function pageRegion(page) {
|
|
171
198
|
return { x: page.x, y: page.y, width: page.width, height: page.height, name: pageName(page, page.index) };
|
|
172
199
|
}
|
|
173
|
-
function
|
|
200
|
+
function pageRegionsSource(pages) {
|
|
174
201
|
const ids = pages.map((p) => p.id);
|
|
175
202
|
return (doc) => {
|
|
176
203
|
const placed = new Map(placedPagesOf(doc).map((p) => [p.id, p]));
|
|
@@ -181,25 +208,52 @@ function pageRegionsOf(pages) {
|
|
|
181
208
|
});
|
|
182
209
|
};
|
|
183
210
|
}
|
|
184
|
-
function
|
|
185
|
-
const read =
|
|
211
|
+
function pageRegionSource(page) {
|
|
212
|
+
const read = pageRegionsSource([page]);
|
|
186
213
|
return (doc) => read(doc)[0];
|
|
187
214
|
}
|
|
188
215
|
function exportPages(scene, pages, formatId, opts = {}) {
|
|
189
|
-
return exportRegions(scene,
|
|
216
|
+
return exportRegions(scene, pageRegionsSource(pages), formatId, opts);
|
|
190
217
|
}
|
|
191
|
-
function selectionRegion(scene, ids,
|
|
218
|
+
function selectionRegion(scene, ids, opts) {
|
|
219
|
+
const { name, scale } = typeof opts === "string" ? { name: opts, scale: void 0 } : opts ?? {};
|
|
192
220
|
const doc = documentOf(scene);
|
|
193
221
|
const world = worldPoseLookup(scenePoseAdapter(doc), doc.composition.compose);
|
|
194
222
|
const drawn = drawnBoundsScene(doc);
|
|
195
|
-
const
|
|
223
|
+
const boxOf = (id) => {
|
|
196
224
|
const pose = world(id);
|
|
197
|
-
return pose === null
|
|
198
|
-
|
|
199
|
-
|
|
225
|
+
return pose === null ? null : drawnAabbOf(drawn, id, pose, {
|
|
226
|
+
worldPoseOf: (m) => world(m),
|
|
227
|
+
...scale !== void 0 ? { scale } : {}
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
const box = unionAabb(shownIds(doc, withoutPageStructure(doc, ids)).map(boxOf));
|
|
200
231
|
return box === null ? null : { x: box.x, y: box.y, width: box.width, height: box.height, ...name !== void 0 ? { name } : {} };
|
|
201
232
|
}
|
|
233
|
+
function shownIds(doc, ids) {
|
|
234
|
+
const flags = nodeGates({ get: (id) => doc.get(id) });
|
|
235
|
+
const gates = nodeGates(doc);
|
|
236
|
+
const annotations = annotationLayerIds(doc.layers);
|
|
237
|
+
const shown = (id) => {
|
|
238
|
+
const node = doc.get(id);
|
|
239
|
+
if (node === void 0 || flags.hidden(node)) return [];
|
|
240
|
+
return gates.hidden(node) || annotations.has(node.layer) ? doc.childrenOf(id).flatMap(shown) : [id];
|
|
241
|
+
};
|
|
242
|
+
return ids.flatMap((id) => shown(id));
|
|
243
|
+
}
|
|
244
|
+
function selectionRegionSource(ids, opts) {
|
|
245
|
+
const held = [...ids];
|
|
246
|
+
return (doc) => {
|
|
247
|
+
const region = selectionRegion(doc, held, opts);
|
|
248
|
+
if (region === null) throw new Error(`exportRegions: the selection (${held.join(", ")}) draws nothing in the document exported`);
|
|
249
|
+
return region;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function selectionRegionsSource(ids, opts) {
|
|
253
|
+
const read = selectionRegionSource(ids, opts);
|
|
254
|
+
return (doc) => [read(doc)];
|
|
255
|
+
}
|
|
202
256
|
|
|
203
|
-
export { EXPORT_CAPTURE_ATTEMPTS, ExportIncompleteError, ExportRevisionError, captureExport, exportFormatRegistry, exportFormats, exportPages, exportRegionPng, exportRegions, exportRegionsOf, exportReporter, pageRegion,
|
|
257
|
+
export { EXPORT_CAPTURE_ATTEMPTS, ExportIncompleteError, ExportRevisionError, captureExport, exportBackgroundOf, exportFormatRegistry, exportFormats, exportPages, exportRegionPng, exportRegions, exportRegionsOf, exportReporter, pageRegion, pageRegionSource, pageRegionsSource, pickExportOptions, regionRenderArgs, regionsOfOne, registerExportFormat, selectionRegion, selectionRegionSource, selectionRegionsSource };
|
|
204
258
|
//# sourceMappingURL=index.js.map
|
|
205
259
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/captureExport.ts","../src/exportDiagnostics.ts","../src/regionRenderArgs.ts","../src/regionPng.ts","../src/exportFormats.ts","../src/exportPages.ts","../src/selectionRegion.ts"],"names":["documentOf"],"mappings":";;;;;AAQO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,mBAAA,GAAN,cAAkC,KAAA,CAAM;AAAA,EAC7C,YAAqB,QAAA,EAAkB;AACrC,IAAA,KAAA,CAAM,CAAA,oEAAA,EAAuE,QAAQ,CAAA,mCAAA,CAAqC,CAAA;AADvG,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAAA,EAErB;AAAA,EAFqB,QAAA;AAGvB;AAYO,IAAM,YAAA,GAAe,CAAC,MAAA,KAC3B,OAAO,WAAW,UAAA,GAAa,CAAC,GAAA,KAAQ,CAAC,MAAA,CAAO,GAAG,CAAC,CAAA,GAAI,CAAC,MAAM;AAG1D,SAAS,eAAA,CAAgB,SAAwB,GAAA,EAA2C;AACjG,EAAA,MAAM,OAAO,OAAO,OAAA,KAAY,UAAA,GAAa,OAAA,CAAQ,GAAG,CAAA,GAAI,OAAA;AAC5D,EAAA,OAAO,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,KAAM,MAAA,CAAO,MAAA,CAAO,EAAE,GAAG,CAAA,EAAG,CAAC,CAAC,CAAA;AAC/D;AAiBA,eAAsB,aAAA,CACpB,KAAA,EAAoB,OAAA,EAAwB,KAAA,EAA8B,SAA+B,MAAM;AAAC,CAAA,EACpG;AACZ,EAAA,MAAM,GAAA,GAAM,WAA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AA,CAAM,CAAC,GAAG,CAAA;AAAA,EACpF,CAAC,CAAA;AACD,EAAA,MAAM,GAAA,GAAM,UAAU,KAAK,CAAA;AAC3B,EAAA,OAAO,GAAA,KAAQ,OAAO,IAAA,GAAO,EAAE,GAAG,GAAA,CAAI,CAAA,EAAG,CAAA,EAAG,GAAA,CAAI,CAAA,EAAG,KAAA,EAAO,IAAI,KAAA,EAAO,MAAA,EAAQ,GAAA,CAAI,MAAA,EAAQ,GAAI,IAAA,KAAS,SAAY,EAAE,IAAA,EAAK,GAAI,EAAC,EAAG;AACnI","file":"index.js","sourcesContent":["/**\n * The one boundary every export reads the document at: what it draws comes\n * from a single revision, with what that revision needs loaded.\n */\nimport { documentOf, type RenderDiagnostic, type RenderDiagnosticSink } from '@weasel-js/core';\nimport type { ExportRegion, ExportRegionSource, ExportRegions, ExportScene } from './exportTypes';\n\n/** How many times an export reads the document before giving up on it holding still. */\nexport const EXPORT_CAPTURE_ATTEMPTS = 3;\n\n/** An export gave up: the document changed while each of its attempts loaded what it draws. */\nexport class ExportRevisionError extends Error {\n constructor(readonly attempts: number) {\n super(`export: the document changed while the export loaded what it draws, ${attempts} times running; nothing was written`);\n }\n}\n\n/** The two halves of an export's read of the document, as {@link captureExport} runs them. Each\n * reports what the export will draw without to `report`. */\nexport interface ExportCaptureSteps<T> {\n /** Load what drawing `regions` of `doc` needs: fonts, paint kinds, pictures. */\n warm(doc: ExportScene, regions: readonly ExportRegion[], report: RenderDiagnosticSink): Promise<void>;\n /** Read everything the file is written from, synchronously, into values the document cannot change. */\n read(doc: ExportScene, regions: readonly ExportRegion[], report: RenderDiagnosticSink): T;\n}\n\n/** One region source as the {@link ExportRegions} {@link captureExport} takes. */\nexport const regionsOfOne = (region: ExportRegionSource): ExportRegions =>\n typeof region === 'function' ? (doc) => [region(doc)] : [region];\n\n/** `regions` as of `doc`: each a copy, so a caller's later edit to one reaches no export in flight. */\nexport function exportRegionsOf(regions: ExportRegions, doc: ExportScene): readonly ExportRegion[] {\n const list = typeof regions === 'function' ? regions(doc) : regions;\n return Object.freeze(list.map((r) => Object.freeze({ ...r })));\n}\n\n/**\n * Read `regions` of `scene`'s document through `steps`, from one content\n * revision (`getContentVersion`): warm, then, if no content changed while it\n * loaded, read at once, in the same task. Fixed regions are copied when it is\n * called. A change restarts it, with the\n * regions read again when they are a function of the document, so the bounds\n * and the content come from the same revision. After\n * {@link EXPORT_CAPTURE_ATTEMPTS} attempts it rejects with\n * {@link ExportRevisionError}.\n *\n * What a gesture or an open edit shows is never read, and a change of\n * selection restarts nothing. The painters, paint kinds and fonts registered\n * when `read` runs are the ones it draws with. What the steps report reaches\n * `report` only from the attempt that is read, once it has been.\n */\nexport async function captureExport<T>(\n scene: ExportScene, regions: ExportRegions, steps: ExportCaptureSteps<T>, report: RenderDiagnosticSink = () => {},\n): Promise<T> {\n const doc = documentOf(scene);\n const source = typeof regions === 'function' ? regions : exportRegionsOf(regions, doc);\n for (let attempt = 1; ; attempt++) {\n const revision = scene.getContentVersion();\n const heard: RenderDiagnostic[] = [];\n const hear = (d: RenderDiagnostic) => { heard.push(d); };\n await steps.warm(doc, exportRegionsOf(source, doc), hear);\n if (scene.getContentVersion() === revision) {\n const read = steps.read(doc, exportRegionsOf(source, doc), hear);\n heard.forEach(report);\n return read;\n }\n if (attempt >= EXPORT_CAPTURE_ATTEMPTS) throw new ExportRevisionError(attempt);\n }\n}\n","/** How every export reports what it could not draw, and when it refuses to write a file without it. */\nimport type { RenderDiagnostic, RenderDiagnosticSink } from '@weasel-js/core';\nimport type { ExportOptions } from './exportTypes';\n\n/** One thing an export drew without, or drew in part: a `RenderDiagnostic`. */\nexport type ExportDiagnostic = RenderDiagnostic;\n\n/** A strict export (`strict: true`) found something it could not draw, and wrote nothing. */\nexport class ExportIncompleteError extends Error {\n constructor(readonly diagnostics: readonly ExportDiagnostic[]) {\n super(`export: ${diagnostics.length === 1 ? 'one thing' : `${diagnostics.length} things`} could not be drawn, and a strict export writes nothing without them: ${\n diagnostics.map((d) => d.message).join('; ')}`);\n }\n}\n\n/** One export's diagnostics: `report` hears each, `settle` ends the export's reporting. */\nexport interface ExportReporter {\n readonly report: RenderDiagnosticSink;\n /** Throws {@link ExportIncompleteError} for a strict export that reported anything. */\n settle(): void;\n}\n\n/**\n * The reporter an export with `opts` reports through. Each diagnostic reaches\n * `onDiagnostic` once, however often it is found. Best-effort, the default,\n * writes the file without what it reports; `strict` writes none when it\n * reports anything, and `settle` throws instead.\n */\nexport function exportReporter(opts: Pick<ExportOptions, 'onDiagnostic' | 'strict'>): ExportReporter {\n const seen = new Map<string, ExportDiagnostic>();\n return {\n report(d) {\n const key = `${d.kind}\\u0000${d.subject}\\u0000${d.message}`;\n if (seen.has(key)) return;\n seen.set(key, d);\n opts.onDiagnostic?.(d);\n },\n settle() {\n if (opts.strict && seen.size > 0) throw new ExportIncompleteError([...seen.values()]);\n },\n };\n}\n","import { documentOf, type RectPose, type RenderSceneToPixelsArgs } from '@weasel-js/core';\nimport type { ExportRegion, ExportScene } from './exportTypes';\n\n/** What {@link regionRenderArgs} answers: the part of `renderSceneToPixels`'s arguments a region decides. */\nexport type RegionRenderArgs = Pick<RenderSceneToPixelsArgs<unknown, string, RectPose>, 'scene' | 'sourceRect' | 'scale' | 'background'>;\n\n/** Options for {@link regionRenderArgs}. */\nexport interface RegionRenderArgsOptions {\n /** Output pixels per world unit. */\n scale: number;\n /** The color painted under the region. Default none: transparent. */\n background?: string | null;\n}\n\n/**\n * The arguments `region` of `scene` renders with: the scene's document (not\n * what an open gesture or edit is showing), the region as the source rect, and\n * `scale` on both axes. The PNG and PDF exports draw through these.\n *\n * Outside a browser, hand them to `@weasel-js/headless`'s raster host, whose\n * `NodeRenderArgs` they satisfy as they are, to render a page or any other\n * region from Node.\n */\nexport function regionRenderArgs(scene: ExportScene, region: ExportRegion, opts: RegionRenderArgsOptions): RegionRenderArgs {\n return {\n scene: documentOf(scene),\n sourceRect: { x: region.x, y: region.y, width: region.width, height: region.height },\n scale: { x: opts.scale, y: opts.scale },\n ...(opts.background != null ? { background: opts.background } : {}),\n };\n}\n","import { holdImageBitmaps } from '@weasel-js/renderer';\nimport { createRasterSession, planPixelRender, rasterToPng, warmRender, type PixelRenderPlan, type RenderDiagnosticSink } from '@weasel-js/core';\nimport { captureExport, regionsOfOne } from './captureExport';\nimport { exportReporter } from './exportDiagnostics';\nimport type { ExportFormat, ExportOptions, ExportRegionSource, ExportScene } from './exportTypes';\nimport { regionRenderArgs } from './regionRenderArgs';\n\n/** Options for {@link exportRegionPng}. */\nexport interface ExportRegionPngOptions extends Pick<ExportOptions, 'onDiagnostic' | 'strict'> {\n /** Output pixels per world unit: 2 makes an image twice the region's size. */\n scale: number;\n /** The color painted under the region. Default none: the image is transparent where nothing is drawn. */\n background?: string | null;\n}\n\n/** The plan a PNG of `region` draws, read through `captureExport` from one revision of the document, its faces,\n * kinds and pictures loaded, and what it draws without told to `report`. */\nexport function capturePngRegion(\n scene: ExportScene, region: ExportRegionSource, opts: ExportRegionPngOptions, report?: RenderDiagnosticSink,\n): Promise<PixelRenderPlan> {\n return captureExport(scene, regionsOfOne(region), {\n warm: (doc, [r], hear) => warmRender({ render: regionRenderArgs(doc, r!, opts), report: hear }),\n read: (doc, [r]) => planPixelRender(regionRenderArgs(doc, r!, opts)),\n }, report);\n}\n\n/**\n * Draw `region` of `scene` into a PNG, `region × scale` pixels, through the\n * renderer the canvas draws with, so it shows what the canvas shows, cut to\n * the region's edges. It draws the document: a gesture or edit in flight is\n * not shown. Annotation layers are left out.\n *\n * The caller has registered whatever the scene needs painting with, as it\n * must to draw it at all: its painters (`registerNodeShape`) and its fonts.\n * Paint kinds and faces that load on demand are waited for, and the image is\n * of one revision of the document: see `captureExport`. What it draws\n * without goes to `onDiagnostic`, and a `strict` export rejects with\n * `ExportIncompleteError` rather than write it. Needs WebGL2.\n */\nexport async function exportRegionPng(scene: ExportScene, region: ExportRegionSource, opts: ExportRegionPngOptions): Promise<Blob> {\n const reporter = exportReporter(opts);\n // The plan's pictures stay open until it is drawn.\n const release = holdImageBitmaps();\n try {\n const plan = await capturePngRegion(scene, region, opts, reporter.report);\n reporter.settle();\n const session = createRasterSession();\n try {\n return await rasterToPng(session.renderPlan(plan));\n } finally {\n session.dispose();\n }\n } finally {\n release();\n }\n}\n\n/** The `png` format's own option. */\nexport interface PngExportOptions extends ExportOptions {\n /** Output pixels per world unit. Default 1. */\n scale?: number;\n}\n\n/** The built-in `png` format: one region as a PNG image, through {@link exportRegionPng}. */\nexport const PNG_EXPORT: ExportFormat<PngExportOptions> = {\n id: 'png',\n label: 'PNG Image',\n mimeType: 'image/png',\n extension: 'png',\n multiRegion: false,\n export: (scene, regions, opts) => exportRegionPng(scene, typeof regions === 'function' ? (doc) => regions(doc)[0]! : regions[0]!, {\n scale: opts.scale ?? 1,\n ...(opts.background !== undefined ? { background: opts.background } : {}),\n ...(opts.onDiagnostic ? { onDiagnostic: opts.onDiagnostic } : {}),\n ...(opts.strict !== undefined ? { strict: opts.strict } : {}),\n }),\n};\n","/**\n * The export registry: the formats a scene's regions can be written in, each\n * registered by the package that can write it. This package registers `png`;\n * svg and pdf register theirs (`registerSvgExport`, `registerPdfExport`), so it\n * depends on neither.\n */\nimport { createReflectable, type Reflection } from '@weasel-js/registry';\nimport type { ExportFormat, ExportOptions, ExportRegion, ExportRegions, ExportScene } from './exportTypes';\nimport { PNG_EXPORT } from './regionPng';\n\nconst FORMATS = createReflectable<ExportFormat>();\n\n// The built-ins go in on first use, not at module load: a bundler may drop an\n// import kept only for its side effect, and the registry would come up empty.\nlet builtInsRegistered = false;\nfunction ensureBuiltIns(): void {\n if (builtInsRegistered) return;\n builtInsRegistered = true;\n FORMATS.push(PNG_EXPORT.id, PNG_EXPORT as ExportFormat, { source: 'kit' });\n}\n\n/** Every registered export format, keyed by id, with the overrides each displaced. */\nexport const exportFormatRegistry: Reflection<ExportFormat> = {\n get: (id) => { ensureBuiltIns(); return FORMATS.get(id); },\n has: (id) => { ensureBuiltIns(); return FORMATS.has(id); },\n entries: () => { ensureBuiltIns(); return FORMATS.entries(); },\n subscribe: (listener) => { ensureBuiltIns(); return FORMATS.subscribe(listener); },\n getVersion: () => { ensureBuiltIns(); return FORMATS.getVersion(); },\n};\n\n/**\n * Register an export format. Returns a disposer that removes this\n * registration. Registering an id already held overrides it, `png` included;\n * disposing uncovers what it displaced.\n */\nexport function registerExportFormat<TOptions extends ExportOptions>(format: ExportFormat<TOptions>): () => void {\n ensureBuiltIns();\n return FORMATS.push(format.id, format as ExportFormat);\n}\n\nlet listed: { from: readonly unknown[]; formats: readonly ExportFormat[] } | null = null;\n\n/** Every registered format, `png` first, then in registration order. The same array until the registry changes. */\nexport function exportFormats(): readonly ExportFormat[] {\n const entries = exportFormatRegistry.entries();\n if (listed?.from !== entries) listed = { from: entries, formats: Object.freeze(entries.map((e) => e.value)) };\n return listed.formats;\n}\n\n/**\n * Write `regions` of `scene` in the format registered as `formatId`. Throws\n * for an id nothing registered, and for more or fewer than one region handed\n * to a format that is not `multiRegion`; a multi-region format takes one or\n * more. Regions given as a function of the document are counted each time\n * the export reads them.\n */\nexport function exportRegions(\n scene: ExportScene,\n regions: ExportRegions,\n formatId: string,\n opts: ExportOptions & Readonly<Record<string, unknown>> = {},\n): Promise<Blob> {\n const format = exportFormatRegistry.get(formatId);\n if (!format) {\n const known = exportFormats().map((f) => f.id).join(', ');\n return Promise.reject(new Error(`exportRegions: no export format \"${formatId}\" is registered (registered: ${known})`));\n }\n if (typeof regions !== 'function') {\n try {\n checkCount(format, regions);\n } catch (err) {\n return Promise.reject(err);\n }\n return format.export(scene, regions, opts);\n }\n return format.export(scene, (doc) => checkCount(format, regions(doc)), opts);\n}\n\nfunction checkCount(format: ExportFormat, regions: readonly ExportRegion[]): readonly ExportRegion[] {\n if (regions.length === 0) throw new Error(`exportRegions: \"${format.id}\" was handed no regions`);\n if (!format.multiRegion && regions.length > 1) {\n throw new Error(`exportRegions: \"${format.id}\" writes one region to a file and was handed ${regions.length}; export them one at a time`);\n }\n return regions;\n}\n\n/** Drop every registration and the built-ins, so the next use registers them again. */\nexport function _resetExportFormatsForTests(): void {\n FORMATS.clear();\n builtInsRegistered = false;\n listed = null;\n}\n","import { pageName, placedPagesOf, type PlacedPage } from '@weasel-js/core';\nimport { exportRegions } from './exportFormats';\nimport type { ExportOptions, ExportRegion, ExportScene } from './exportTypes';\n\n/** `page` as an export region, named as the page order shows it. */\nexport function pageRegion(page: PlacedPage): ExportRegion {\n return { x: page.x, y: page.y, width: page.width, height: page.height, name: pageName(page, page.index) };\n}\n\n/**\n * `pages` as regions read from the document an export draws: each page placed\n * as that document places it, found by its id, and named for it. A page the\n * document no longer holds fails the export.\n */\nexport function pageRegionsOf(pages: readonly PlacedPage[]): (doc: ExportScene) => readonly ExportRegion[] {\n const ids = pages.map((p) => p.id);\n return (doc) => {\n const placed = new Map(placedPagesOf(doc).map((p) => [p.id, p]));\n return ids.map((id) => {\n const page = placed.get(id);\n if (page === undefined) throw new Error(`exportPages: the document holds no page \"${id}\"`);\n return pageRegion(page);\n });\n };\n}\n\n/** One page as {@link pageRegionsOf} reads it: the region source of a single-region export. */\nexport function pageRegionOf(page: PlacedPage): (doc: ExportScene) => ExportRegion {\n const read = pageRegionsOf([page]);\n return (doc) => read(doc)[0]!;\n}\n\n/**\n * Write `pages` of `scene` in the format registered as `formatId`, each page\n * a region named for the page: `exportRegions` over {@link pageRegionsOf}. A\n * single-region format takes one page.\n */\nexport function exportPages(\n scene: ExportScene,\n pages: readonly PlacedPage[],\n formatId: string,\n opts: ExportOptions & Readonly<Record<string, unknown>> = {},\n): Promise<Blob> {\n return exportRegions(scene, pageRegionsOf(pages), formatId, opts);\n}\n","import { documentOf, drawnAABBOf, drawnBoundsScene, scenePoseAdapter, unionAABB, withoutPageStructure, worldPoseLookup, type NodeId } from '@weasel-js/core';\nimport type { ExportRegion, ExportScene } from './exportTypes';\n\n/**\n * The world box `ids` cover as the document draws them, strokes included:\n * the region that exports a selection. Pages and the page run are left out of\n * `ids`. `null` when none of them draws anything.\n */\nexport function selectionRegion(scene: ExportScene, ids: readonly string[], name?: string): ExportRegion | null {\n const doc = documentOf(scene);\n const world = worldPoseLookup(scenePoseAdapter(doc), doc.composition.compose as (p: unknown, c: unknown) => unknown);\n const drawn = drawnBoundsScene(doc);\n const boxes = withoutPageStructure(doc, ids).map((id) => {\n const pose = world(id);\n return pose === null || doc.get(id as NodeId) === undefined\n ? null : drawnAABBOf(drawn, id as NodeId, pose, { worldPoseOf: (m) => world(m) });\n });\n const box = unionAABB(boxes);\n return box === null ? null : { x: box.x, y: box.y, width: box.width, height: box.height, ...(name !== undefined ? { name } : {}) };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/captureExport.ts","../src/exportDiagnostics.ts","../src/exportTypes.ts","../src/regionRenderArgs.ts","../src/regionPng.ts","../src/exportFormats.ts","../src/exportPages.ts","../src/selectionRegion.ts"],"names":["documentOf"],"mappings":";;;;;AAQO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,mBAAA,GAAN,cAAkC,KAAA,CAAM;AAAA,EAC7C,YAAqB,QAAA,EAAkB;AACrC,IAAA,KAAA,CAAM,CAAA,oEAAA,EAAuE,QAAQ,CAAA,mCAAA,CAAqC,CAAA;AADvG,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAAA,EAErB;AAAA,EAFqB,QAAA;AAGvB;AAYO,IAAM,YAAA,GAAe,CAAC,MAAA,KAC3B,OAAO,WAAW,UAAA,GAAa,CAAC,GAAA,KAAQ,CAAC,MAAA,CAAO,GAAG,CAAC,CAAA,GAAI,CAAC,MAAM;AAG1D,SAAS,eAAA,CAAgB,SAAwB,GAAA,EAA2C;AACjG,EAAA,MAAM,OAAO,OAAO,OAAA,KAAY,UAAA,GAAa,OAAA,CAAQ,GAAG,CAAA,GAAI,OAAA;AAC5D,EAAA,OAAO,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,KAAM,MAAA,CAAO,MAAA,CAAO,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A,MAAM,IAAA,GAAO,qBAAA,CAAsB,GAAA,EAAK,IAAI,CAAA;AAC5C,EAAA,OAAO,CAAC,GAAA,KAAQ,CAAC,IAAA,CAAK,GAAG,CAAC,CAAA;AAC5B","file":"index.js","sourcesContent":["/**\n * The one boundary every export reads the document at: what it draws comes\n * from a single revision, with what that revision needs loaded.\n */\nimport { documentOf, type RenderDiagnostic, type RenderDiagnosticSink } from '@weasel-js/core';\nimport type { ExportRegion, ExportRegionSource, ExportRegions, ExportScene } from './exportTypes';\n\n/** How many times an export reads the document before giving up on it holding still. */\nexport const EXPORT_CAPTURE_ATTEMPTS = 3;\n\n/** An export gave up: the document changed while each of its attempts loaded what it draws. */\nexport class ExportRevisionError extends Error {\n constructor(readonly attempts: number) {\n super(`export: the document changed while the export loaded what it draws, ${attempts} times running; nothing was written`);\n }\n}\n\n/** The two halves of an export's read of the document, as {@link captureExport} runs them. Each\n * reports what the export will draw without to `report`. */\nexport interface ExportCaptureSteps<T> {\n /** Load what drawing `regions` of `doc` needs: fonts, paint kinds, pictures. */\n warm(doc: ExportScene, regions: readonly ExportRegion[], report: RenderDiagnosticSink): Promise<void>;\n /** Read everything the file is written from, synchronously, into values the document cannot change. */\n read(doc: ExportScene, regions: readonly ExportRegion[], report: RenderDiagnosticSink): T;\n}\n\n/** One region source as the {@link ExportRegions} {@link captureExport} takes. */\nexport const regionsOfOne = (region: ExportRegionSource): ExportRegions =>\n typeof region === 'function' ? (doc) => [region(doc)] : [region];\n\n/** `regions` as of `doc`: each a copy, so a caller's later edit to one reaches no export in flight. */\nexport function exportRegionsOf(regions: ExportRegions, doc: ExportScene): readonly ExportRegion[] {\n const list = typeof regions === 'function' ? regions(doc) : regions;\n return Object.freeze(list.map((r) => Object.freeze({ ...r })));\n}\n\n/**\n * Read `regions` of `scene`'s document through `steps`, from one content\n * revision (`getContentVersion`): warm, then, if no content changed while it\n * loaded, read at once, in the same task. Fixed regions are copied when it is\n * called. A change restarts it, with the\n * regions read again when they are a function of the document, so the bounds\n * and the content come from the same revision. After\n * {@link EXPORT_CAPTURE_ATTEMPTS} attempts it rejects with\n * {@link ExportRevisionError}.\n *\n * What a gesture or an open edit shows is never read, and a change of\n * selection restarts nothing. The painters, paint kinds and fonts registered\n * when `read` runs are the ones it draws with. What the steps report reaches\n * `report` only from the attempt that is read, once it has been.\n */\nexport async function captureExport<T>(\n scene: ExportScene, regions: ExportRegions, steps: ExportCaptureSteps<T>, report: RenderDiagnosticSink = () => {},\n): Promise<T> {\n const doc = documentOf(scene);\n const source = typeof regions === 'function' ? regions : exportRegionsOf(regions, doc);\n for (let attempt = 1; ; attempt++) {\n const revision = scene.getContentVersion();\n const heard: RenderDiagnostic[] = [];\n const hear = (d: RenderDiagnostic) => { heard.push(d); };\n await steps.warm(doc, exportRegionsOf(source, doc), hear);\n if (scene.getContentVersion() === revision) {\n const read = steps.read(doc, exportRegionsOf(source, doc), hear);\n heard.forEach(report);\n return read;\n }\n if (attempt >= EXPORT_CAPTURE_ATTEMPTS) throw new ExportRevisionError(attempt);\n }\n}\n","/** How every export reports what it could not draw, and when it refuses to write a file without it. */\nimport type { RenderDiagnostic, RenderDiagnosticSink } from '@weasel-js/core';\nimport type { ExportOptions } from './exportTypes';\n\n/** One thing an export drew without, or drew in part: a `RenderDiagnostic`. */\nexport type ExportDiagnostic = RenderDiagnostic;\n\n/** A strict export (`strict: true`) found something it could not draw, and wrote nothing. */\nexport class ExportIncompleteError extends Error {\n constructor(readonly diagnostics: readonly ExportDiagnostic[]) {\n super(`export: ${diagnostics.length === 1 ? 'one thing' : `${diagnostics.length} things`} could not be drawn, and a strict export writes nothing without them: ${\n diagnostics.map((d) => d.message).join('; ')}`);\n }\n}\n\n/** One export's diagnostics: `report` hears each, `settle` ends the export's reporting. */\nexport interface ExportReporter {\n readonly report: RenderDiagnosticSink;\n /** Throws {@link ExportIncompleteError} for a strict export that reported anything. */\n settle(): void;\n}\n\n/**\n * The reporter an export with `opts` reports through. Each diagnostic reaches\n * `onDiagnostic` once, however often it is found. Best-effort, the default,\n * writes the file without what it reports; `strict` writes none when it\n * reports anything, and `settle` throws instead.\n */\nexport function exportReporter(opts: Pick<ExportOptions, 'onDiagnostic' | 'strict'>): ExportReporter {\n const seen = new Map<string, ExportDiagnostic>();\n return {\n report(d) {\n const key = `${d.kind}\\u0000${d.subject}\\u0000${d.message}`;\n if (seen.has(key)) return;\n seen.set(key, d);\n opts.onDiagnostic?.(d);\n },\n settle() {\n if (opts.strict && seen.size > 0) throw new ExportIncompleteError([...seen.values()]);\n },\n };\n}\n","/** The types the export registry and its formats share. */\nimport type { Rect } from '@weasel-js/geom';\nimport type { RectPose, RenderDiagnostic, Scene } from '@weasel-js/core';\n\n/**\n * A rectangle of the document to export: a page (`exportPages` makes one of\n * each `PlacedPage`), the world rect a view spans (`viewportWorldRect`), a\n * selection's bounds, or any other. Every format draws each layer in it at its\n * document pose: a parallax layer's camera tracking is not applied. `name` is\n * what a format that labels its pages or documents calls it: a PDF page's\n * label, an SVG's `<title>`.\n */\nexport interface ExportRegion extends Rect {\n readonly name?: string;\n}\n\n/**\n * The regions an export writes: fixed rects, or a function reading them from\n * the document. A function is called each time the export reads the document,\n * so bounds that come from it, a page's or a selection's, belong to the same\n * revision as what is drawn in them.\n */\nexport type ExportRegions = readonly ExportRegion[] | ((doc: ExportScene) => readonly ExportRegion[]);\n\n/** One region an export writes, fixed or read from the document, as {@link ExportRegions} holds them. */\nexport type ExportRegionSource = ExportRegion | ((doc: ExportScene) => ExportRegion);\n\n/** A scene as the exports read it: world-space `RectPose`s. */\nexport type ExportScene = Scene<unknown, string, RectPose>;\n\n/** The color painted under a region: a color, `null` for none, or a function reading it from the\n * document the export draws, so a color that comes from it belongs to the revision drawn. */\nexport type ExportBackground = string | null | ((doc: ExportScene) => string | null);\n\n/** `background` as of `doc`. */\nexport const exportBackgroundOf = (background: ExportBackground | undefined, doc: ExportScene): string | null =>\n typeof background === 'function' ? background(doc) : background ?? null;\n\n/** What every format takes. */\nexport interface ExportOptions {\n /** The color painted under each region, or a function reading it from the document the export draws.\n * Default none: core holds no page color. */\n background?: ExportBackground;\n /** Hears each thing the export could not draw or load, once: a picture or face\n * that failed, a paint kind or shader nothing can draw, what the format cannot hold. */\n onDiagnostic?: (diagnostic: RenderDiagnostic) => void;\n /** Write nothing when anything is diagnosed, and reject with `ExportIncompleteError`.\n * Default `false`: best-effort, the file is written without it. */\n strict?: boolean;\n}\n\n/**\n * One way of writing regions of a scene to a file, registered with\n * `registerExportFormat`. `TOptions` is what the format reads beyond\n * {@link ExportOptions}. `exportRegions` hands a format whatever its caller\n * passed, so a format treats every option of its own as optional there.\n */\nexport interface ExportFormat<TOptions extends ExportOptions = ExportOptions> {\n /** The id `exportRegions` names it by: `'png'`, `'svg'`, `'pdf'`. */\n readonly id: string;\n /** What a menu calls it: \"PNG Image\". */\n readonly label: string;\n readonly mimeType: string;\n /** The file extension, without the dot. */\n readonly extension: string;\n /** Whether one file holds several regions, as a PDF's pages do. A single-region format takes exactly one. */\n readonly multiRegion: boolean;\n /**\n * The option keys it reads beyond {@link ExportOptions}. `exportRegions` warns in development about a\n * key passed to this format that neither lists, and {@link pickExportOptions} picks these. Left out,\n * nothing is checked.\n */\n readonly options?: readonly Exclude<keyof TOptions, keyof ExportOptions>[];\n export(scene: ExportScene, regions: ExportRegions, opts: TOptions): Promise<Blob>;\n}\n","import { documentOf, type RectPose, type RenderSceneToPixelsArgs } from '@weasel-js/core';\nimport { exportBackgroundOf, type ExportBackground, type ExportRegion, type ExportScene } from './exportTypes';\n\n/** What {@link regionRenderArgs} answers: the part of `renderSceneToPixels`'s arguments a region decides. */\nexport type RegionRenderArgs = Pick<RenderSceneToPixelsArgs<unknown, string, RectPose>, 'scene' | 'sourceRect' | 'scale' | 'background'>;\n\n/** Options for {@link regionRenderArgs}. */\nexport interface RegionRenderArgsOptions {\n /** Output pixels per world unit. */\n scale: number;\n /** The color painted under the region, or a function reading it from the document drawn. Default none: transparent. */\n background?: ExportBackground;\n}\n\n/**\n * The arguments `region` of `scene` renders with: the scene's document (not\n * what an open gesture or edit is showing), the region as the source rect, and\n * `scale` on both axes. The PNG and PDF exports draw through these.\n *\n * Outside a browser, hand them to `@weasel-js/headless`'s raster host, whose\n * `NodeRenderArgs` they satisfy as they are, to render a page or any other\n * region from Node.\n */\nexport function regionRenderArgs(scene: ExportScene, region: ExportRegion, opts: RegionRenderArgsOptions): RegionRenderArgs {\n const doc = documentOf(scene);\n const background = exportBackgroundOf(opts.background, doc);\n return {\n scene: doc,\n sourceRect: { x: region.x, y: region.y, width: region.width, height: region.height },\n scale: { x: opts.scale, y: opts.scale },\n ...(background !== null ? { background } : {}),\n };\n}\n","import { holdImageBitmaps } from '@weasel-js/renderer';\nimport { createRasterSession, planPixelRender, rasterToPng, warmRender, type PixelRenderPlan, type RenderDiagnosticSink } from '@weasel-js/core';\nimport { captureExport, regionsOfOne } from './captureExport';\nimport { exportReporter } from './exportDiagnostics';\nimport type { ExportBackground, ExportFormat, ExportOptions, ExportRegionSource, ExportScene } from './exportTypes';\nimport { regionRenderArgs } from './regionRenderArgs';\n\n/** Options for {@link exportRegionPng}. */\nexport interface ExportRegionPngOptions extends Pick<ExportOptions, 'onDiagnostic' | 'strict'> {\n /** Output pixels per world unit: 2 makes an image twice the region's size. */\n scale: number;\n /** The color painted under the region, or a function reading it from the document drawn. Default none: the\n * image is transparent where nothing is drawn. */\n background?: ExportBackground;\n}\n\n/** The plan a PNG of `region` draws, read through `captureExport` from one revision of the document, its faces,\n * kinds and pictures loaded, and what it draws without told to `report`. */\nexport function capturePngRegion(\n scene: ExportScene, region: ExportRegionSource, opts: ExportRegionPngOptions, report?: RenderDiagnosticSink,\n): Promise<PixelRenderPlan> {\n return captureExport(scene, regionsOfOne(region), {\n warm: (doc, [r], hear) => warmRender({ render: regionRenderArgs(doc, r!, opts), report: hear }),\n read: (doc, [r]) => planPixelRender(regionRenderArgs(doc, r!, opts)),\n }, report);\n}\n\n/**\n * Draw `region` of `scene` into a PNG, `region × scale` pixels, through the\n * renderer the canvas draws with, so it shows what the canvas shows, cut to\n * the region's edges. It draws the document: a gesture or edit in flight is\n * not shown. Annotation layers are left out.\n *\n * The caller has registered whatever the scene needs painting with, as it\n * must to draw it at all: its painters (`registerNodeShape`) and its fonts.\n * Paint kinds and faces that load on demand are waited for, and the image is\n * of one revision of the document: see `captureExport`. What it draws\n * without goes to `onDiagnostic`, and a `strict` export rejects with\n * `ExportIncompleteError` rather than write it. Needs WebGL2.\n */\nexport async function exportRegionPng(scene: ExportScene, region: ExportRegionSource, opts: ExportRegionPngOptions): Promise<Blob> {\n const reporter = exportReporter(opts);\n // The plan's pictures stay open until it is drawn.\n const release = holdImageBitmaps();\n try {\n const plan = await capturePngRegion(scene, region, opts, reporter.report);\n reporter.settle();\n const session = createRasterSession();\n try {\n return await rasterToPng(session.renderPlan(plan));\n } finally {\n session.dispose();\n }\n } finally {\n release();\n }\n}\n\n/** The `png` format's own option. */\nexport interface PngExportOptions extends ExportOptions {\n /** Output pixels per world unit. Default 1. */\n scale?: number;\n}\n\n/** The built-in `png` format: one region as a PNG image, through {@link exportRegionPng}. */\nexport const PNG_EXPORT: ExportFormat<PngExportOptions> = {\n id: 'png',\n label: 'PNG Image',\n mimeType: 'image/png',\n extension: 'png',\n multiRegion: false,\n options: ['scale'],\n export: (scene, regions, opts) => exportRegionPng(scene, typeof regions === 'function' ? (doc) => regions(doc)[0]! : regions[0]!, {\n scale: opts.scale ?? 1,\n ...(opts.background !== undefined ? { background: opts.background } : {}),\n ...(opts.onDiagnostic ? { onDiagnostic: opts.onDiagnostic } : {}),\n ...(opts.strict !== undefined ? { strict: opts.strict } : {}),\n }),\n};\n","/**\n * The export registry: the formats a scene's regions can be written in, each\n * registered by the package that can write it. This package registers `png`;\n * svg and pdf register theirs (`registerSvgExport`, `registerPdfExport`), so it\n * depends on neither.\n */\nimport { createReflectable, isDev, type PushOptions, type Reflection } from '@weasel-js/registry';\nimport type { ExportFormat, ExportOptions, ExportRegion, ExportRegions, ExportScene } from './exportTypes';\nimport { PNG_EXPORT } from './regionPng';\n\nconst FORMATS = createReflectable<ExportFormat>({ name: 'export formats' });\n\n// The built-ins go in on first use, not at module load: a bundler may drop an\n// import kept only for its side effect, and the registry would come up empty.\nlet builtInsRegistered = false;\nfunction ensureBuiltIns(): void {\n if (builtInsRegistered) return;\n builtInsRegistered = true;\n FORMATS.push(PNG_EXPORT.id, PNG_EXPORT as ExportFormat, { source: 'kit', fallback: true });\n}\n\n/** Every registered export format, keyed by id, with the overrides each displaced. */\nexport const exportFormatRegistry: Reflection<ExportFormat> = {\n name: FORMATS.name,\n get: (id) => { ensureBuiltIns(); return FORMATS.get(id); },\n has: (id) => { ensureBuiltIns(); return FORMATS.has(id); },\n entries: () => { ensureBuiltIns(); return FORMATS.entries(); },\n subscribe: (listener) => { ensureBuiltIns(); return FORMATS.subscribe(listener); },\n getVersion: () => { ensureBuiltIns(); return FORMATS.getVersion(); },\n};\n\n/**\n * Register an export format. Returns a disposer that removes this\n * registration. Registering an id already held overrides it, `png` included;\n * disposing uncovers what it displaced. `fallback` marks an engine default,\n * which a later registration replaces without the development clash warning.\n */\nexport function registerExportFormat<TOptions extends ExportOptions>(\n format: ExportFormat<TOptions>,\n options?: Pick<PushOptions, 'fallback'>,\n): () => void {\n ensureBuiltIns();\n return FORMATS.push(format.id, format as ExportFormat, options);\n}\n\nlet listed: { from: readonly unknown[]; formats: readonly ExportFormat[] } | null = null;\n\n/** Every registered format, `png` first, then in registration order. The same array until the registry changes. */\nexport function exportFormats(): readonly ExportFormat[] {\n const entries = exportFormatRegistry.entries();\n if (listed?.from !== entries) listed = { from: entries, formats: Object.freeze(entries.map((e) => e.value)) };\n return listed.formats;\n}\n\n/**\n * Write `regions` of `scene` in the format registered as `formatId`. Rejects\n * for an id nothing registered, for more or fewer than one region handed to a\n * format that is not `multiRegion` (a multi-region format takes one or more),\n * and with whatever the format throws, synchronously or not. Regions given as\n * a function of the document are counted each time the export reads them.\n */\nexport function exportRegions(\n scene: ExportScene,\n regions: ExportRegions,\n formatId: string,\n opts: ExportOptions & Readonly<Record<string, unknown>> = {},\n): Promise<Blob> {\n const format = exportFormatRegistry.get(formatId);\n if (!format) {\n const known = exportFormats().map((f) => f.id).join(', ');\n return Promise.reject(new Error(`exportRegions: no export format \"${formatId}\" is registered (registered: ${known})`));\n }\n warnUnreadOptions(format, opts);\n // Every failure rejects, a format's own synchronous throw included, so a caller has one channel to hear it on.\n try {\n if (typeof regions !== 'function') return Promise.resolve(format.export(scene, checkCount(format, regions), opts));\n return Promise.resolve(format.export(scene, (doc) => checkCount(format, regions(doc)), opts));\n } catch (err) {\n return Promise.reject(err);\n }\n}\n\n/** The keys every format reads, {@link ExportOptions}'s. */\nconst SHARED_OPTIONS: readonly (keyof ExportOptions)[] = ['background', 'onDiagnostic', 'strict'];\n\nconst warnedOptions = new Set<string>();\n\nfunction warnUnreadOptions(format: ExportFormat, opts: Readonly<Record<string, unknown>>): void {\n if (!format.options || !isDev()) return;\n const reads = new Set<string>([...SHARED_OPTIONS, ...format.options]);\n for (const key of Object.keys(opts)) {\n if (opts[key] === undefined || reads.has(key) || warnedOptions.has(`${format.id}\\0${key}`)) continue;\n warnedOptions.add(`${format.id}\\0${key}`);\n console.warn(\n `exportRegions: the \"${format.id}\" format does not read the option \"${key}\", so it is ignored ` +\n `(it reads: ${[...reads].join(', ')}). Check the spelling, or whether another format takes it.`,\n );\n }\n}\n\n/** The options `format` reads out of `opts`, the shared ones and those it declares, leaving out any undefined. */\nexport function pickExportOptions<TOptions extends ExportOptions>(\n format: ExportFormat<TOptions>, opts: TOptions,\n): Partial<TOptions> {\n const out: Partial<TOptions> = {};\n for (const key of [...SHARED_OPTIONS, ...(format.options ?? [])] as (keyof TOptions)[]) {\n if (opts[key] !== undefined) out[key] = opts[key];\n }\n return out;\n}\n\nfunction checkCount(format: ExportFormat, regions: readonly ExportRegion[]): readonly ExportRegion[] {\n if (regions.length === 0) throw new Error(`exportRegions: \"${format.id}\" was handed no regions`);\n if (!format.multiRegion && regions.length > 1) {\n throw new Error(`exportRegions: \"${format.id}\" writes one region to a file and was handed ${regions.length}; export them one at a time`);\n }\n return regions;\n}\n\n/** Drop every registration and the built-ins, so the next use registers them again. */\nexport function _resetExportFormatsForTests(): void {\n FORMATS.clear();\n builtInsRegistered = false;\n listed = null;\n warnedOptions.clear();\n}\n","import { pageName, placedPagesOf, type PlacedPage } from '@weasel-js/core';\nimport { exportRegions } from './exportFormats';\nimport type { ExportOptions, ExportRegion, ExportScene } from './exportTypes';\n\n/** `page` as an export region, named as the page order shows it. */\nexport function pageRegion(page: PlacedPage): ExportRegion {\n return { x: page.x, y: page.y, width: page.width, height: page.height, name: pageName(page, page.index) };\n}\n\n/**\n * `pages` as regions read from the document an export draws: each page placed\n * as that document places it, found by its id, and named for it. A page the\n * document no longer holds fails the export.\n */\nexport function pageRegionsSource(pages: readonly PlacedPage[]): (doc: ExportScene) => readonly ExportRegion[] {\n const ids = pages.map((p) => p.id);\n return (doc) => {\n const placed = new Map(placedPagesOf(doc).map((p) => [p.id, p]));\n return ids.map((id) => {\n const page = placed.get(id);\n if (page === undefined) throw new Error(`exportPages: the document holds no page \"${id}\"`);\n return pageRegion(page);\n });\n };\n}\n\n/** One page as {@link pageRegionsSource} reads it: the region source of a single-region export. */\nexport function pageRegionSource(page: PlacedPage): (doc: ExportScene) => ExportRegion {\n const read = pageRegionsSource([page]);\n return (doc) => read(doc)[0]!;\n}\n\n/**\n * Write `pages` of `scene` in the format registered as `formatId`, each page\n * a region named for the page: `exportRegions` over {@link pageRegionsSource}. A\n * single-region format takes one page.\n */\nexport function exportPages(\n scene: ExportScene,\n pages: readonly PlacedPage[],\n formatId: string,\n opts: ExportOptions & Readonly<Record<string, unknown>> = {},\n): Promise<Blob> {\n return exportRegions(scene, pageRegionsSource(pages), formatId, opts);\n}\n","import {\n annotationLayerIds, documentOf, drawnAabbOf, drawnBoundsScene, nodeGates, scenePoseAdapter, unionAabb,\n withoutPageStructure, worldPoseLookup, type NodeId,\n} from '@weasel-js/core';\nimport type { ExportRegion, ExportScene } from './exportTypes';\n\n/** Options for {@link selectionRegion} and its sources. */\nexport interface SelectionRegionOptions {\n /** What the region is called: a PDF page's label, an SVG's `<title>`. */\n name?: string;\n /** The output pixels per world unit the export draws at, which a screen-pixel (`{ px }`) stroke is\n * measured at. Default 1. Pass the export's `scale` so such a stroke is not cut at its edge. */\n scale?: number;\n}\n\n/**\n * The world box `ids` cover as the document draws them, strokes included:\n * the region that exports a selection. Pages and the page run are left out of\n * `ids`, and so is what no export draws: a hidden node, and a node on a\n * hidden or annotation layer (what its children draw still counts). `null`\n * when none of them draws anything. A string third argument is the name.\n */\nexport function selectionRegion(\n scene: ExportScene, ids: readonly string[], opts?: string | SelectionRegionOptions,\n): ExportRegion | null {\n const { name, scale } = typeof opts === 'string' ? { name: opts, scale: undefined } : opts ?? {};\n const doc = documentOf(scene);\n const world = worldPoseLookup(scenePoseAdapter(doc), doc.composition.compose as (p: unknown, c: unknown) => unknown);\n const drawn = drawnBoundsScene(doc);\n const boxOf = (id: NodeId) => {\n const pose = world(id);\n return pose === null ? null : drawnAabbOf(drawn, id, pose, {\n worldPoseOf: (m) => world(m),\n ...(scale !== undefined ? { scale } : {}),\n });\n };\n const box = unionAabb(shownIds(doc, withoutPageStructure(doc, ids)).map(boxOf));\n return box === null ? null : { x: box.x, y: box.y, width: box.width, height: box.height, ...(name !== undefined ? { name } : {}) };\n}\n\n/** `ids` as an export draws them: a hidden node and its subtree dropped, and a node on a hidden or\n * annotation layer replaced by its children, as the render walk paints them. */\nfunction shownIds(doc: ExportScene, ids: readonly string[]): NodeId[] {\n const flags = nodeGates({ get: (id) => doc.get(id) });\n const gates = nodeGates(doc);\n const annotations = annotationLayerIds(doc.layers);\n const shown = (id: NodeId): NodeId[] => {\n const node = doc.get(id);\n if (node === undefined || flags.hidden(node)) return [];\n return gates.hidden(node) || annotations.has(node.layer) ? doc.childrenOf(id).flatMap(shown) : [id];\n };\n return ids.flatMap((id) => shown(id as NodeId));\n}\n\n/**\n * `ids`' region read from the document an export draws, so the bounds belong to\n * the revision drawn in them: the region source of a single-region export. A\n * selection that draws nothing in that document fails the export.\n */\nexport function selectionRegionSource(ids: readonly string[], opts?: SelectionRegionOptions): (doc: ExportScene) => ExportRegion {\n const held = [...ids];\n return (doc) => {\n const region = selectionRegion(doc, held, opts);\n if (region === null) throw new Error(`exportRegions: the selection (${held.join(', ')}) draws nothing in the document exported`);\n return region;\n };\n}\n\n/** {@link selectionRegionSource} as the regions `exportRegions` takes. */\nexport function selectionRegionsSource(ids: readonly string[], opts?: SelectionRegionOptions): (doc: ExportScene) => readonly ExportRegion[] {\n const read = selectionRegionSource(ids, opts);\n return (doc) => [read(doc)];\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/export",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Exports regions of a @weasel-js/core scene: a page, a selection or any rect, as PNG or any format another package registers (SVG, PDF), with a diagnostics report and a retry when the scene changes mid-export.",
|
|
5
5
|
"weasel": {
|
|
6
6
|
"tier": "plugin"
|
|
@@ -41,12 +41,15 @@
|
|
|
41
41
|
"test": "node ../../scripts/package-test.mjs"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@weasel-js/geom": "
|
|
45
|
-
"@weasel-js/registry": "
|
|
46
|
-
"@weasel-js/renderer": "
|
|
44
|
+
"@weasel-js/geom": "2.0.0",
|
|
45
|
+
"@weasel-js/registry": "2.0.0",
|
|
46
|
+
"@weasel-js/renderer": "2.0.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@weasel-js/core": "
|
|
49
|
+
"@weasel-js/core": "2.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@weasel-js/paint": "2.0.0"
|
|
50
53
|
},
|
|
51
54
|
"publishConfig": {
|
|
52
55
|
"access": "public",
|