bookcover-core 0.22.3 → 0.24.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.
@@ -6,6 +6,27 @@ export interface CustomFontStyle {
6
6
  family: string;
7
7
  style: FontStyle;
8
8
  }
9
+ /** Options for build_schema beyond the form itself */
10
+ export interface BuildSchemaOptions {
11
+ /**
12
+ * Typst markup to put in place of marker text in the blurb, keyed by marker — plain text
13
+ * exactly as the user sees it in the editor (e.g. 'AUTO-COPYRIGHT'). Every occurrence is
14
+ * replaced, including one inside other text; a marker the user styled only part of is split
15
+ * across text nodes and is NOT replaced, nor is one inside inline code. The form is never
16
+ * changed. Values are trusted, inserted verbatim, and must follow the rules below.
17
+ *
18
+ * - Multi-line markup must be wrapped in one `#[…];` block (the `;` ends the expression, so
19
+ * text after the marker can't continue it), or the marker put alone in its own paragraph:
20
+ * a bare newline would break a list item, heading, or bold/italic span around the marker.
21
+ * - Set your own direction (`#set text(dir: rtl)`) if the text needs it: the blurb's
22
+ * direction per paragraph comes from the marker's text, which reads left-to-right.
23
+ * - Keep text as escaped content, not string literals: each CJK run in the blurb is wrapped
24
+ * in a #text(font:) span after this, which would break a string literal around it.
25
+ * - Use no images or named fonts. The compile has no files beyond the cover's own, and
26
+ * loads only the fonts the blurb's text (substitutions included) resolves to.
27
+ */
28
+ typst_substitutions?: Record<string, string>;
29
+ }
9
30
  /** Replace straight quotes with typographic curly quotes */
10
31
  export declare function curly_quotes(text: string): string;
11
32
  /** Parse a font family name from user input. Accepts:
@@ -38,7 +59,8 @@ export declare function font_families_in_form(form: EmbedFormState): string[];
38
59
  *
39
60
  * custom_fonts supplies the sniffed serif/sans style of user-uploaded families (their bytes
40
61
  * travel separately to the compiler). Never reads a background image — that too is passed to
41
- * the generators out-of-band.
62
+ * the generators out-of-band. options.typst_substitutions replaces marker text in the blurb for
63
+ * this render only (see BuildSchemaOptions).
42
64
  */
43
- export declare function build_schema(form: EmbedFormState, custom_fonts?: CustomFontStyle[]): Record<string, unknown>;
65
+ export declare function build_schema(form: EmbedFormState, custom_fonts?: CustomFontStyle[], options?: BuildSchemaOptions): Record<string, unknown>;
44
66
  //# sourceMappingURL=form_schema.d.ts.map
@@ -1,5 +1,6 @@
1
1
  // Pure functions for building the cover generator schema from form state
2
- import { pm_to_typst } from 'pm-to-typst';
2
+ import { pm_to_typst, typst_renderer } from 'pm-to-typst';
3
+ import { escape_typst } from 'typst-utils';
3
4
  import { hex_override_to_hsl } from './colors.js';
4
5
  import { find_pattern } from './patterns.js';
5
6
  import { warn_unknown, text_dir } from './utils.js';
@@ -27,10 +28,20 @@ function pm_text(node) {
27
28
  * its own first strong letter, so an RTL paragraph gets RTL punctuation, list markers and
28
29
  * justified last line while its LTR neighbours are untouched. Blocks nested inside a list or
29
30
  * quote follow the enclosing block's direction.
31
+ * Marker text is replaced with its substitution (see BuildSchemaOptions) as each text node is
32
+ * rendered, so the marker is matched as the user typed it and only the text around it escaped.
30
33
  * Returns undefined when the blurb is empty.
31
34
  */
32
- function build_blurb(doc) {
33
- const typst = pm_to_typst(doc, { nodes: {
35
+ function build_blurb(doc, substitutions = {}) {
36
+ const marker_regex = substitution_regex(substitutions);
37
+ const typst = pm_to_typst(doc, { text: node => {
38
+ // Inline code is literal, so a marker inside it stays as typed
39
+ if (!marker_regex || (node.marks ?? []).some(mark => mark.type === 'code'))
40
+ return typst_renderer.text(node);
41
+ // Splitting on a capturing regex alternates text, marker, text, … (text may be empty)
42
+ return (node.text ?? '').split(marker_regex)
43
+ .map((piece, i) => i % 2 ? substitutions[piece] : escape_typst(piece)).join('');
44
+ }, nodes: {
34
45
  doc: (node, ctx) => (node.content ?? []).map(child => {
35
46
  const rendered = ctx.node(child);
36
47
  return text_dir(pm_text(child)) === 'rtl'
@@ -39,6 +50,15 @@ function build_blurb(doc) {
39
50
  } });
40
51
  return typst.trim() ? typst : undefined;
41
52
  }
53
+ // One capturing regex matching any marker, longest first so a marker containing another wins.
54
+ // Null when there is nothing to replace
55
+ function substitution_regex(substitutions) {
56
+ const markers = Object.keys(substitutions).filter(marker => marker)
57
+ .sort((a, b) => b.length - a.length);
58
+ if (markers.length === 0)
59
+ return null;
60
+ return new RegExp(`(${markers.map(m => m.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`);
61
+ }
42
62
  /** Parse a font family name from user input. Accepts:
43
63
  * - Plain font name: "Playwrite AR Guides"
44
64
  * - Specimen URL: https://fonts.google.com/specimen/Playwrite+AR+Guides
@@ -106,9 +126,10 @@ function build_font_config(raw, custom_fonts) {
106
126
  *
107
127
  * custom_fonts supplies the sniffed serif/sans style of user-uploaded families (their bytes
108
128
  * travel separately to the compiler). Never reads a background image — that too is passed to
109
- * the generators out-of-band.
129
+ * the generators out-of-band. options.typst_substitutions replaces marker text in the blurb for
130
+ * this render only (see BuildSchemaOptions).
110
131
  */
111
- export function build_schema(form, custom_fonts) {
132
+ export function build_schema(form, custom_fonts, options = {}) {
112
133
  const q = curly_quotes;
113
134
  // Size/print fields. A 'custom' service has no print spec, so bleed and spine come from the
114
135
  // form; every real service computes them from the page count instead
@@ -190,7 +211,7 @@ export function build_schema(form, custom_fonts) {
190
211
  author_margin_top: form.author_margin_top,
191
212
  author_margin_bottom: form.author_margin_bottom,
192
213
  // An empty blurb stays absent — there is nothing to render, not a default to apply
193
- blurb: build_blurb(form.blurb),
214
+ blurb: build_blurb(form.blurb, options.typst_substitutions),
194
215
  blurb_font: build_font_config(form.blurb_font, custom_fonts),
195
216
  blurb_size: form.blurb_size,
196
217
  blurb_color: hex_override_to_hsl(form.blurb_color) ?? undefined,
@@ -4,7 +4,7 @@ import { find_builtin_icon } from './builtin_icons.js';
4
4
  import { find_preset_icon_svg } from './preset_icons_svg.js';
5
5
  // Module-level in-memory cache: iconify ID → raw SVG string
6
6
  const svg_cache = new Map();
7
- // This package has no i18n/UI concerns (see repo CLAUDE.md — generator is pure TS core), so
7
+ // This package has no i18n/UI concerns (see repo AGENTS.md — generator is pure TS core), so
8
8
  // errors reachable via user input (a mistyped icon field) carry a translation code + params
9
9
  // alongside a default English message; the widget translates at the point it catches them
10
10
  // (generator_client.ts / PreviewPane.vue), falling back to `message` for anything else.
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ export type { FormState, EmbedFormState } from './form_state.js';
27
27
  export { SCHEMA_VERSION, RENDER_VERSION, SCHEMA_DEFAULTS, FORM_DEFAULTS } from './defaults.js';
28
28
  export { warn_unknown } from './utils.js';
29
29
  export { build_schema, curly_quotes, parse_font_family, normalize_font_family, font_families_in_form } from './form_schema.js';
30
- export type { CustomFontStyle } from './form_schema.js';
30
+ export type { CustomFontStyle, BuildSchemaOptions } from './form_schema.js';
31
31
  export { analyze_pixel_regions, get_builtin_bg, get_builtin_bg_regions, BG_PREVIEW_DIR } from './image_regions.js';
32
32
  export type { BuiltinBg } from './generated/builtin_bg_regions.js';
33
33
  export { derive_colors, hex_override_to_hsl, hex_to_hsl, is_dark_color } from './colors.js';
@@ -0,0 +1,11 @@
1
+ import type { GetDimensionsResult } from './dimensions.js';
2
+ /**
3
+ * Turn a one-page wraparound cover PDF into two pages: the front, then the back. Any spine is left
4
+ * out, as there's nothing to print it on once the faces are apart. Each page keeps the cover's
5
+ * bleed on every side, as each is trimmed on its own (on the inner side that bleed is the spine's
6
+ * or the other face's artwork). Both pages share one copy of the source page's content and
7
+ * resources, each just boxed to its own face. `dims` must be in mm, as resolve_dimensions()
8
+ * returns them.
9
+ */
10
+ export declare function split_pdf_pages(pdf: Uint8Array, dims: GetDimensionsResult): Promise<Uint8Array>;
11
+ //# sourceMappingURL=pdf_pages.d.ts.map
@@ -0,0 +1,38 @@
1
+ // Re-cutting a wraparound cover PDF into a page for the front and one for the back, for printing
2
+ // them separately (generate()'s `pdf_pages` option). Not re-exported from the main barrel, since
3
+ // it brings pdf-lib with it: reach it via the `bookcover-core/pdf-pages` subpath, which the
4
+ // platform wrappers import lazily only when asked for pages
5
+ import { PDFDocument } from 'pdf-lib';
6
+ /** Convert mm to pt — the PDF coordinate system uses pt (1mm = 72/25.4pt) */
7
+ function mm_to_pt(mm) {
8
+ return mm / 25.4 * 72;
9
+ }
10
+ /**
11
+ * Turn a one-page wraparound cover PDF into two pages: the front, then the back. Any spine is left
12
+ * out, as there's nothing to print it on once the faces are apart. Each page keeps the cover's
13
+ * bleed on every side, as each is trimmed on its own (on the inner side that bleed is the spine's
14
+ * or the other face's artwork). Both pages share one copy of the source page's content and
15
+ * resources, each just boxed to its own face. `dims` must be in mm, as resolve_dimensions()
16
+ * returns them.
17
+ */
18
+ export async function split_pdf_pages(pdf, dims) {
19
+ const bleed = dims.cover_has_bleed ? dims.cover_bleed.toNumber() : 0;
20
+ const source = await PDFDocument.load(pdf);
21
+ const sheet = source.getPage(0).getMediaBox();
22
+ // One copier call for both pages, so the background image etc. is only embedded once
23
+ const out = await PDFDocument.create();
24
+ const [front, back] = await out.copyPages(source, [0, 0]);
25
+ const faces = [[front, dims.cover_region_front], [back, dims.cover_region_back]];
26
+ for (const [page, region] of faces) {
27
+ // Regions are mm from the top-left, PDF boxes are pt from the bottom-left
28
+ const x = sheet.x + mm_to_pt(region.x.toNumber() - bleed);
29
+ const width = mm_to_pt(region.w.toNumber() + bleed * 2);
30
+ const height = mm_to_pt(region.h.toNumber() + bleed * 2);
31
+ const y = sheet.y + sheet.height - mm_to_pt(region.y.toNumber() - bleed) - height;
32
+ page.setMediaBox(x, y, width, height);
33
+ page.setCropBox(x, y, width, height);
34
+ out.addPage(page);
35
+ }
36
+ return await out.save();
37
+ }
38
+ //# sourceMappingURL=pdf_pages.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bookcover-core",
3
- "version": "0.22.3",
3
+ "version": "0.24.0",
4
4
  "license": "MIT-0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,6 +25,10 @@
25
25
  "types": "./dist/preset_icons_svg.d.ts",
26
26
  "default": "./dist/preset_icons_svg.js"
27
27
  },
28
+ "./pdf-pages": {
29
+ "types": "./dist/pdf_pages.d.ts",
30
+ "default": "./dist/pdf_pages.js"
31
+ },
28
32
  "./package.json": "./package.json"
29
33
  },
30
34
  "files": [
@@ -41,6 +45,7 @@
41
45
  "big.js": "^7.0.1",
42
46
  "bwip-js": "^4.9.0",
43
47
  "chroma-js": "^3.2.0",
48
+ "pdf-lib": "^1.17.1",
44
49
  "pm-to-typst": "^0.1.2",
45
50
  "printing-services": "^1.5.1",
46
51
  "typst-fonts": "^0.1.4",