bookcover-web 0.22.2 → 0.22.3
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/embed_types.js +6 -0
- package/dist/index.js +17 -14
- package/package.json +4 -3
package/dist/embed_types.js
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
// InitMessage in reply to the widget's 'ready' message; the widget reports edits back with
|
|
3
3
|
// WidgetMessage, and may send further HostMessages afterwards.
|
|
4
4
|
//
|
|
5
|
+
// The widget waits 300ms for the InitMessage before mounting with its demo cover, so a host
|
|
6
|
+
// that never sends one still gets a working editor. A later InitMessage is still applied in full
|
|
7
|
+
// (preset, background, fonts, config, locale), replacing the demo cover, so a host should answer
|
|
8
|
+
// promptly but loses nothing when it can't. Until the InitMessage arrives the widget sends
|
|
9
|
+
// nothing but 'ready' and error reports: no 'data' from the demo cover ever reaches a host.
|
|
10
|
+
//
|
|
5
11
|
// Binaries are never part of the stored record. The background image and custom font bytes
|
|
6
12
|
// travel as separate structured-clone fields beside the pure-JSON form values, so hosts persist
|
|
7
13
|
// `data` as-is (e.g. to Firestore) and own the storage and identity of the binaries themselves.
|
package/dist/index.js
CHANGED
|
@@ -308,7 +308,7 @@ export class CoverGenerator {
|
|
|
308
308
|
});
|
|
309
309
|
}
|
|
310
310
|
/** Compile to vector format, then render as PNG via offscreen canvas */
|
|
311
|
-
async compile_png(ppi) {
|
|
311
|
+
async compile_png(ppi, dims) {
|
|
312
312
|
this.assert_renderer('PNG');
|
|
313
313
|
const result = await this.compiler.compile({
|
|
314
314
|
mainFilePath: '/cover.typ',
|
|
@@ -317,22 +317,25 @@ export class CoverGenerator {
|
|
|
317
317
|
if (!result.result) {
|
|
318
318
|
throw_compile_error(result.diagnostics);
|
|
319
319
|
}
|
|
320
|
-
// Render
|
|
321
|
-
|
|
322
|
-
|
|
320
|
+
// Render at the desired PPI (typst uses 72pt/inch). The renderer draws into a 2D context
|
|
321
|
+
// (it panics on a canvas element) and never sizes the canvas, so size it to the page here
|
|
322
|
+
// — the full cover spread. An OffscreenCanvas context works too, which is all a worker has
|
|
323
|
+
const px_per_mm = ppi / 25.4;
|
|
324
|
+
const canvas = make_canvas(Math.round(dims.cover_total_width.toNumber() * px_per_mm), Math.round(dims.cover_total_height.toNumber() * px_per_mm));
|
|
325
|
+
const ctx = canvas.getContext('2d');
|
|
326
|
+
if (!ctx) {
|
|
327
|
+
throw new Error('[generator-web] PNG output needs a 2D canvas context');
|
|
328
|
+
}
|
|
323
329
|
await this.renderer.renderCanvas({
|
|
324
330
|
artifactContent: result.result,
|
|
325
331
|
format: 'vector',
|
|
326
|
-
canvas,
|
|
332
|
+
canvas: ctx,
|
|
327
333
|
pageOffset: 0,
|
|
328
|
-
pixelPerPt:
|
|
334
|
+
pixelPerPt: ppi / 72,
|
|
329
335
|
backgroundColor: '#ffffff',
|
|
330
336
|
});
|
|
331
337
|
// Extract PNG from canvas
|
|
332
|
-
|
|
333
|
-
canvas.toBlob((b) => resolve(b), 'image/png');
|
|
334
|
-
});
|
|
335
|
-
return new Uint8Array(await blob.arrayBuffer());
|
|
338
|
+
return canvas_png_bytes(canvas);
|
|
336
339
|
}
|
|
337
340
|
/** Throw if the renderer was not initialised */
|
|
338
341
|
assert_renderer(format) {
|
|
@@ -341,12 +344,12 @@ export class CoverGenerator {
|
|
|
341
344
|
}
|
|
342
345
|
}
|
|
343
346
|
/** Run the compiler (and, for svg/png, the renderer) for the requested output format */
|
|
344
|
-
async compile_format(format, ppi) {
|
|
347
|
+
async compile_format(format, ppi, dims) {
|
|
345
348
|
if (format === 'pdf')
|
|
346
349
|
return this.compile_pdf();
|
|
347
350
|
if (format === 'svg')
|
|
348
351
|
return this.compile_svg();
|
|
349
|
-
return this.compile_png(ppi);
|
|
352
|
+
return this.compile_png(ppi, dims);
|
|
350
353
|
}
|
|
351
354
|
/** Generate a book cover using this instance's compiler and renderer */
|
|
352
355
|
async generate(options) {
|
|
@@ -426,7 +429,7 @@ export class CoverGenerator {
|
|
|
426
429
|
// transparently instead of hard-failing the whole session.
|
|
427
430
|
let data;
|
|
428
431
|
try {
|
|
429
|
-
data = await this.compile_format(format, ppi);
|
|
432
|
+
data = await this.compile_format(format, ppi, dims);
|
|
430
433
|
}
|
|
431
434
|
catch (error) {
|
|
432
435
|
if (!(error instanceof RangeError))
|
|
@@ -435,7 +438,7 @@ export class CoverGenerator {
|
|
|
435
438
|
if (format !== 'pdf')
|
|
436
439
|
await this.reinit_renderer();
|
|
437
440
|
this.load_files(files);
|
|
438
|
-
data = await this.compile_format(format, ppi);
|
|
441
|
+
data = await this.compile_format(format, ppi, dims);
|
|
439
442
|
}
|
|
440
443
|
const result = { data };
|
|
441
444
|
// Split into front/back/spine panels
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bookcover-web",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.3",
|
|
4
4
|
"license": "MIT-0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,13 +33,14 @@
|
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsc",
|
|
36
|
-
"dev": "tsc --watch"
|
|
36
|
+
"dev": "tsc --watch",
|
|
37
|
+
"test": "vitest run"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
40
|
"@myriaddreamin/typst-ts-renderer": "0.7.0",
|
|
40
41
|
"@myriaddreamin/typst-ts-web-compiler": "0.7.0",
|
|
41
42
|
"@myriaddreamin/typst.ts": "0.7.0",
|
|
42
|
-
"bookcover-core": "^0.22.
|
|
43
|
+
"bookcover-core": "^0.22.3",
|
|
43
44
|
"typst-fonts": "^0.1.4"
|
|
44
45
|
},
|
|
45
46
|
"sideEffects": false
|