bookcover-web 0.22.2 → 0.23.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.
@@ -64,6 +64,23 @@ export interface InitMessage {
64
64
  hide_size_section?: boolean;
65
65
  bg_suggestions?: BgSuggestion[];
66
66
  locale?: string;
67
+ /** Typst markup to put in place of marker text in the cover's blurb, keyed by marker (plain
68
+ * text exactly as the user sees it in the editor, e.g. 'AUTO-COPYRIGHT'), so the widget
69
+ * shows what will actually print. Applied only when the widget renders (its previews and
70
+ * its own PDF exports): the form is never changed, so the editor still shows the marker
71
+ * and data/finished messages send it back untouched. The host applies the same map when
72
+ * it renders final output, by passing it to build_schema() as `typst_substitutions`.
73
+ *
74
+ * Values are trusted Typst from the host, inserted verbatim. They follow the rules on
75
+ * BuildSchemaOptions.typst_substitutions in bookcover-core: wrap multi-line markup in one
76
+ * `#[…];` block or put the marker alone in its own paragraph; set your own text direction;
77
+ * keep text as escaped content rather than string literals; use no images or named fonts.
78
+ * A marker the user styles only part of (bolding half of it, say) isn't replaced.
79
+ *
80
+ * If a preview fails to compile with the substitutions, the widget reports it as an
81
+ * 'error' message and shows the preview without them, raw marker and all; a PDF export
82
+ * fails instead. Absent or empty changes nothing. */
83
+ typst_substitutions?: Record<string, string>;
67
84
  }
68
85
  /** Host -> widget. InitMessage is the first and must arrive before any other; later messages are
69
86
  * only accepted from the same origin that sent it.
@@ -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.d.ts CHANGED
@@ -9,7 +9,7 @@ export type { IconCategory } from 'bookcover-core';
9
9
  export { tinted_contrast_text, pick_vivid_tint, synthesize_fill, blend_regions, region_hex, all_image_regions, analyze_pixel_regions, get_builtin_bg, get_builtin_bg_regions, BG_PREVIEW_DIR, VECTOR_BG_AUTO_COLOR, resolve_colors, darken_hsl } from 'bookcover-core';
10
10
  export type { RegionStats, ImageRegions, ResolvedColors, BuiltinBg } from 'bookcover-core';
11
11
  export { make_blank_form_values, build_schema, curly_quotes, parse_font_family, find_pattern, derive_colors, hex_override_to_hsl, hex_to_hsl, is_dark_color, warn_unknown } from 'bookcover-core';
12
- export type { FormState, EmbedFormState, CustomFontStyle, DerivedColors } from 'bookcover-core';
12
+ export type { FormState, EmbedFormState, CustomFontStyle, BuildSchemaOptions, DerivedColors } from 'bookcover-core';
13
13
  export { resolve_dimensions } from 'bookcover-core';
14
14
  export type { GetDimensionsResult, DimensionInputs } from 'bookcover-core';
15
15
  export type { InitMessage, HostMessage, WidgetMessage, BgSuggestion } from './embed_types.js';
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 to a canvas at the desired PPI (typst uses 72pt/inch)
321
- const scale = ppi / 72;
322
- const canvas = document.createElement('canvas');
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: scale,
334
+ pixelPerPt: ppi / 72,
329
335
  backgroundColor: '#ffffff',
330
336
  });
331
337
  // Extract PNG from canvas
332
- const blob = await new Promise((resolve) => {
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.2",
3
+ "version": "0.23.0",
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.2",
43
+ "bookcover-core": "^0.23.0",
43
44
  "typst-fonts": "^0.1.4"
44
45
  },
45
46
  "sideEffects": false