bookcover-core 0.23.0 → 0.24.1
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/defaults.d.ts +1 -1
- package/dist/defaults.js +1 -1
- package/dist/frame.d.ts +13 -4
- package/dist/frame.js +19 -5
- package/dist/generated/templates_data.js +2 -2
- package/dist/icon_cache.js +1 -1
- package/dist/index.js +3 -2
- package/dist/pdf_pages.d.ts +11 -0
- package/dist/pdf_pages.js +38 -0
- package/package.json +6 -1
package/dist/defaults.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare const SCHEMA_VERSION = 1;
|
|
|
7
7
|
* the bundled pattern/vector/icon data. Hosts that re-render a frozen record later should
|
|
8
8
|
* record this at freeze time (alongside the typst version their wrapper reports) so a drifted
|
|
9
9
|
* reprint is detectable instead of silent. */
|
|
10
|
-
export declare const RENDER_VERSION =
|
|
10
|
+
export declare const RENDER_VERSION = 7;
|
|
11
11
|
/** What an omitted CoverSchema field resolves to at render time. Derived fields are absent by
|
|
12
12
|
* design — see the note at the top of this file. */
|
|
13
13
|
export declare const SCHEMA_DEFAULTS: {
|
package/dist/defaults.js
CHANGED
|
@@ -19,7 +19,7 @@ export const SCHEMA_VERSION = 1;
|
|
|
19
19
|
* the bundled pattern/vector/icon data. Hosts that re-render a frozen record later should
|
|
20
20
|
* record this at freeze time (alongside the typst version their wrapper reports) so a drifted
|
|
21
21
|
* reprint is detectable instead of silent. */
|
|
22
|
-
export const RENDER_VERSION =
|
|
22
|
+
export const RENDER_VERSION = 7;
|
|
23
23
|
/** What an omitted CoverSchema field resolves to at render time. Derived fields are absent by
|
|
24
24
|
* design — see the note at the top of this file. */
|
|
25
25
|
export const SCHEMA_DEFAULTS = {
|
package/dist/frame.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
export type FrameImageFn = (image: Blob, frame_data: Blob, bg: string,
|
|
1
|
+
export type FrameImageFn = (image: Blob, frame_data: Blob, bg: string, max_width: number, max_height: number) => Promise<Blob>;
|
|
2
2
|
export declare const FRAME_FILES: Record<'painted' | 'torn', string>;
|
|
3
3
|
export declare function frame_asset_path(base: string, frame: 'painted' | 'torn'): string;
|
|
4
|
+
/** Largest whole-pixel size at the image's aspect ratio that fits within the max box
|
|
5
|
+
* (like CSS object-fit: contain) */
|
|
6
|
+
export declare function contain_size(iw: number, ih: number, max_w: number, max_h: number): {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
4
10
|
/**
|
|
5
11
|
* Apply a decorative frame to an image.
|
|
6
12
|
*
|
|
13
|
+
* The output canvas takes the image's own aspect ratio, as large as fits within
|
|
14
|
+
* max_width x max_height, so the image is never cropped or stretched.
|
|
15
|
+
*
|
|
7
16
|
* The frame PNG has a white (opaque) border and transparent center.
|
|
8
17
|
* The opaque border is used to cut away the image edges (destination-out blend),
|
|
9
18
|
* leaving a transparent border with the image showing through the center.
|
|
@@ -16,9 +25,9 @@ export declare function frame_asset_path(base: string, frame: 'painted' | 'torn'
|
|
|
16
25
|
* @param image - Source image as a Blob
|
|
17
26
|
* @param frame_data - Frame PNG as a Blob (loaded by the platform wrapper from assets)
|
|
18
27
|
* @param bg - CSS background color drawn under the image (e.g. '#ffffff')
|
|
19
|
-
* @param
|
|
20
|
-
* @param
|
|
28
|
+
* @param max_width - Largest output canvas width in pixels
|
|
29
|
+
* @param max_height - Largest output canvas height in pixels
|
|
21
30
|
* @returns PNG Blob with frame applied
|
|
22
31
|
*/
|
|
23
|
-
export declare function frame_image(image: Blob, frame_data: Blob, bg: string,
|
|
32
|
+
export declare function frame_image(image: Blob, frame_data: Blob, bg: string, max_width: number, max_height: number): Promise<Blob>;
|
|
24
33
|
//# sourceMappingURL=frame.d.ts.map
|
package/dist/frame.js
CHANGED
|
@@ -45,9 +45,21 @@ function cover_crop(iw, ih, cw, ch) {
|
|
|
45
45
|
const sy = (ih - sh) / 2;
|
|
46
46
|
return { sx, sy, sw, sh };
|
|
47
47
|
}
|
|
48
|
+
/** Largest whole-pixel size at the image's aspect ratio that fits within the max box
|
|
49
|
+
* (like CSS object-fit: contain) */
|
|
50
|
+
export function contain_size(iw, ih, max_w, max_h) {
|
|
51
|
+
const scale = Math.min(max_w / iw, max_h / ih);
|
|
52
|
+
return {
|
|
53
|
+
width: Math.max(1, Math.round(iw * scale)),
|
|
54
|
+
height: Math.max(1, Math.round(ih * scale)),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
48
57
|
/**
|
|
49
58
|
* Apply a decorative frame to an image.
|
|
50
59
|
*
|
|
60
|
+
* The output canvas takes the image's own aspect ratio, as large as fits within
|
|
61
|
+
* max_width x max_height, so the image is never cropped or stretched.
|
|
62
|
+
*
|
|
51
63
|
* The frame PNG has a white (opaque) border and transparent center.
|
|
52
64
|
* The opaque border is used to cut away the image edges (destination-out blend),
|
|
53
65
|
* leaving a transparent border with the image showing through the center.
|
|
@@ -60,19 +72,21 @@ function cover_crop(iw, ih, cw, ch) {
|
|
|
60
72
|
* @param image - Source image as a Blob
|
|
61
73
|
* @param frame_data - Frame PNG as a Blob (loaded by the platform wrapper from assets)
|
|
62
74
|
* @param bg - CSS background color drawn under the image (e.g. '#ffffff')
|
|
63
|
-
* @param
|
|
64
|
-
* @param
|
|
75
|
+
* @param max_width - Largest output canvas width in pixels
|
|
76
|
+
* @param max_height - Largest output canvas height in pixels
|
|
65
77
|
* @returns PNG Blob with frame applied
|
|
66
78
|
*/
|
|
67
|
-
export async function frame_image(image, frame_data, bg,
|
|
79
|
+
export async function frame_image(image, frame_data, bg, max_width, max_height) {
|
|
80
|
+
// Size the output canvas to the image's aspect ratio within the max box
|
|
81
|
+
const img_bitmap = await createImageBitmap(image);
|
|
82
|
+
const { width, height } = contain_size(img_bitmap.width, img_bitmap.height, max_width, max_height);
|
|
68
83
|
// Create output canvas (worker-safe)
|
|
69
84
|
const canvas = make_canvas(width, height);
|
|
70
85
|
const ctx = canvas.getContext('2d');
|
|
71
86
|
// Fill background color
|
|
72
87
|
ctx.fillStyle = bg;
|
|
73
88
|
ctx.fillRect(0, 0, width, height);
|
|
74
|
-
// Draw image covering canvas (
|
|
75
|
-
const img_bitmap = await createImageBitmap(image);
|
|
89
|
+
// Draw image covering canvas (only trims the rounding of contain_size, no distortion)
|
|
76
90
|
const { sx, sy, sw, sh } = cover_crop(img_bitmap.width, img_bitmap.height, width, height);
|
|
77
91
|
ctx.drawImage(img_bitmap, sx, sy, sw, sh, 0, 0, width, height);
|
|
78
92
|
img_bitmap.close();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Edit the source .typ files instead, then rerun the generator build
|
|
3
3
|
/** The cover.typ / _helpers.typ templates baked in at this package version */
|
|
4
4
|
export const DEFAULT_TEMPLATES = {
|
|
5
|
-
cover: "\n// Book cover template — receives all variables from _data.typ\n#import \"_data.typ\": *\n\n// -- Page setup --\n#set page(\n width: total_width,\n height: total_height,\n margin: 0mm,\n)\n\n#set text(font: font_body_family)\n#set quote(block: true)\n#show quote: set pad(x: 3em)\n\n// ============================================================\n// Helper functions\n// ============================================================\n\n#import \"_helpers.typ\": *\n\n\n// ============================================================\n// Layer 1 — Background fills\n// ============================================================\n\n#place(\n top + left,\n rect(\n width: total_width,\n height: total_height,\n fill: if has_front_gradient {\n gradient.linear(\n (color_front_gradient_start, 0%),\n (color_front_bg, 30%),\n (color_front_bg, 45%),\n (color_front_gradient_end, 100%),\n angle: -40deg,\n )\n } else {\n color_front_bg\n },\n ),\n)\n\n\n#if has_pattern {\n let tiled = pattern(\n size: (pattern_tile_w, pattern_tile_h),\n image(\"pattern.svg\", width: pattern_tile_w, height: pattern_tile_h),\n )\n place(\n top + left,\n rect(width: total_width, height: total_height, fill: tiled),\n )\n}\n\n\n// ============================================================\n// Layer 2 — Background image\n// ============================================================\n\n#if has_image {\n if image_coverage == \"full\" {\n // Full spread: image covers entire canvas (back + spine + front)\n place(\n top + left,\n dx: 0mm,\n dy: 0mm,\n image(\n image_filename,\n width: total_width,\n height: total_height,\n fit: if image_is_vector { \"stretch\" } else { \"cover\" },\n ),\n )\n } else if image_coverage == \"painted\" {\n // Painted: image inset on front cover — 15mm margin horizontally and from bottom, 50% trim height\n let painted_w = face_width - 30mm\n let painted_h = face_height * 0.5 // sync: index.ts painted_h = face_height * 0.5\n place(\n top + left,\n dx: front_x + 15mm,\n dy: bleed + face_height * (1/3),\n image(\n image_filename,\n width: painted_w,\n height: painted_h,\n fit: \"stretch\", // Should be exact but just in case, ensures full frame visible\n ),\n )\n } else if image_coverage == \"feature\" {\n // Feature: same size and position as painted but with rounded corners via box clip\n let painted_w = face_width - 30mm\n let painted_h = face_height * 0.5\n place(\n top + left,\n dx: front_x + 15mm,\n dy: bleed + face_height * (1/3),\n box(\n width: painted_w,\n height: painted_h,\n radius: 6mm,\n clip: true,\n image(\n image_filename,\n width: painted_w,\n height: painted_h,\n fit: \"cover\",\n ),\n ),\n )\n } else if image_coverage == \"front_partial\" {\n // Lower 2/3: image covers the full front face width, bottom 2/3 of height, flush to the bottom trim edge\n let w = face_width\n let h = face_height * (2 / 3)\n place(\n top + left,\n dx: front_x,\n dy: bleed + face_height - h,\n image(\n image_filename,\n width: w,\n height: h,\n fit: \"cover\",\n ),\n )\n } else {\n // Front only: image covers the front panel including all bleed margins\n place(\n top + left,\n dx: front_x,\n dy: 0mm,\n image(\n image_filename,\n width: face_width + bleed,\n height: total_height,\n fit: if image_is_vector { \"stretch\" } else { \"cover\" },\n ),\n )\n }\n}\n\n\n// ============================================================\n// Layer 2b — Icon on front cover\n// ============================================================\n\n#if has_icon {\n let icon_size = calc.min(face_height * 0.4, face_width * 0.8)\n if icon_mode != \"center\" {\n icon_size = calc.min(face_height * 0.5, face_width * 0.8)\n }\n // Apply user-controlled size multiplier (1.0 = default)\n icon_size = icon_size * icon_size_mod\n let offset_icon_dx = front_x + (face_width - icon_size) + (icon_size * 0.15)\n let offset_icon_dy = bleed * -1 + (icon_size * 0.15)\n\n // Place ghost icons first (if any) so below main one\n if icon_mode == \"echo\" {\n place(\n bottom + left,\n dx: offset_icon_dx + icon_size * 0.1,\n dy: offset_icon_dy - icon_size * 0.15,\n image(\"icon_ghost.svg\", width: icon_size, height: icon_size),\n )\n place(\n bottom + left,\n dx: offset_icon_dx - icon_size * 0.1,\n dy: offset_icon_dy + icon_size * 0.1,\n image(\"icon_ghost2.svg\", width: icon_size, height: icon_size),\n )\n }\n\n if icon_mode == \"center\" {\n // Center — large icon centered horizontally, positioned in the lower/mid section\n place(\n bottom + left,\n dx: front_x + (face_width - icon_size) / 2,\n dy: (face_height * 0.2 + bleed) * -1,\n image(\"icon_main.svg\", width: icon_size, height: icon_size),\n )\n } else {\n // Offset/echo: large icon offset towards bottom-right, slightly overflowing the trim edge\n place(\n bottom + left,\n dx: offset_icon_dx,\n dy: offset_icon_dy,\n image(\"icon_main.svg\", width: icon_size, height: icon_size),\n )\n }\n}\n\n\n// ============================================================\n// Layer 2c — Spine background\n// ============================================================\n\n// Spine background (only when spread has a spine) — drawn after pattern so it covers it\n#if has_spine and color_spine_bg != none {\n place(\n top + left,\n dx: spine_x,\n dy: 0mm,\n rect(width: spine_width, height: total_height, fill: color_spine_bg),\n )\n}\n\n\n// ============================================================\n// Layer 3 — Back panel content\n// ============================================================\n\n// back_margin, back_content_w, spiral_margin imported from _data.typ\n\n// Blurb container box with background color and 1cm padding around the text\n// Content is scaled down if it would overflow the available back-panel height\n#if has_blurb {\n let blurb_content = {\n set par(justify: true) if blurb_justify\n set par(leading: 1em * blurb_spacing)\n // Compact heading styles for a small blurb box — size and leading are hardcoded\n // independent of blurb_spacing, since headings rarely wrap and need tight leading\n show heading.where(level: 1): it => {\n set block(above: 2em, below: 1em)\n set text(size: 1.3em)\n set par(leading: 0.5em)\n it\n }\n show heading.where(level: 2): it => {\n set block(above: 2em, below: 1em)\n set text(size: 1.15em)\n set par(leading: 0.5em)\n it\n }\n align(blurb_alignment, text(\n font: font_blurb_family,\n size: fs_back_blurb * blurb_size,\n fill: color_blurb,\n blurb,\n ))\n }\n let blurb_max_h = back_content_h - blurb_padding * 2\n place(\n top + left,\n // Centered within the safe area, not the full face — spiral_margin is 0 for other bindings\n dx: back_x + (face_width - spiral_margin - blurb_width) / 2,\n dy: bleed + back_margin,\n rect(\n width: blurb_width,\n fill: color_blurb_bg,\n stroke: none,\n radius: 1.5mm,\n inset: blurb_padding,\n context {\n let avail_w = blurb_width - blurb_padding * 2\n let natural = measure(blurb_content, width: avail_w)\n if natural.height > blurb_max_h {\n let ratio = blurb_max_h / natural.height\n scale(x: ratio * 100%, y: ratio * 100%, reflow: true, blurb_content)\n } else {\n blurb_content\n }\n },\n ),\n )\n}\n\n// ISBN barcode — position and size from printing-services cover_region_barcode\n#if has_barcode {\n place(\n bottom + left,\n dx: barcode_x,\n dy: barcode_y * -1,\n image(\"barcode.svg\", width: barcode_w, height: barcode_h),\n )\n}\n\n// ============================================================\n// Layer 4 — Spine content\n// ============================================================\n\n#if has_spine and has_spine_text and fs_spine_title > 0mm {\n // Each spine item only when it has text, so an empty one leaves no stack spacing gap\n // pushing the other off-center\n let spine_items = (\n if spine_title != \"\" {\n text(\n dir: spine_title_dir,\n font: font_spine_title_family,\n size: fs_spine_title * spine_title_size_mod,\n fill: color_spine_title,\n weight: spine_title_weight,\n style: if spine_title_italic { \"italic\" } else { \"normal\" },\n spine_title,\n )\n },\n if spine_author != \"\" and fs_spine_author > 0mm {\n text(\n dir: spine_author_dir,\n font: font_spine_author_family,\n size: fs_spine_author * spine_author_size_mod,\n fill: color_spine_author,\n weight: spine_author_weight,\n style: if spine_author_italic { \"italic\" } else { \"normal\" },\n spine_author,\n )\n },\n ).filter(x => x != none)\n\n // Title rotated along spine (bottom-to-top reading direction).\n // place() anchors the unrotated bounding box top-left, so we subtract\n // half the unrotated dimensions to keep the visual center on the spine.\n // The title + author pair runs in the title's direction, so for RTL the title\n // comes first reading right-to-left (bottom-to-top once rotated).\n if spine_items.len() > 0 {\n place(\n top + left,\n dx: spine_x + spine_width * 0.5 - face_height * 0.4,\n dy: bleed + face_height * 0.5 - spine_width * 0.5,\n rotate(\n 90deg,\n origin: center,\n box(\n width: face_height * 0.8,\n height: spine_width,\n align(\n center + horizon,\n // Shrink title + author together as one unit if they overflow the spine length\n shrink-to-width(\n stack(dir: spine_dir, spacing: spine_width * 0.3, ..spine_items),\n ),\n ),\n ),\n ),\n )\n }\n}\n\n// Small icon at the bottom of the spine (when spine is wide enough for text)\n#if has_spine_icon {\n let spine_icon_size = spine_width * 0.7\n let spine_icon_side_margin = (spine_width - spine_icon_size) / 2\n // Bottom margin is twice the side margin\n let spine_icon_bottom_margin = spine_icon_side_margin * 2\n place(\n top + left,\n dx: spine_x + spine_icon_side_margin,\n dy: bleed + face_height - spine_icon_size - spine_icon_bottom_margin,\n image(\"icon_spine.svg\", width: spine_icon_size, height: spine_icon_size),\n )\n}\n\n// ============================================================\n// Layer 5 — Front panel content\n// ============================================================\n\n// front_margin, front_content_w, spiral_margin imported from _data.typ\n\n// Base font size — proportional to trim height, multiplied by per-title size modifier\n#let fs_title_base = face_height * 0.05\n#let fs_subtitle = face_height * 0.025 * subtitle_size\n#let fs_author = face_height * 0.035 * author_size\n\n// Render a single title line with its per-title styling, shrunk to fit if too wide\n#let render_title(txt, font_family, size_mod, weight, is_italic, color, txt_dir) = {\n if txt != \"\" {\n box(\n width: front_content_w,\n shrink-to-width(\n align(title_alignment, text(\n dir: txt_dir,\n font: font_family,\n size: fs_title_base * size_mod,\n fill: color,\n weight: weight,\n style: if is_italic { \"italic\" } else { \"normal\" },\n txt,\n )),\n ),\n )\n }\n}\n\n// Render each title line, then filter out empty ones so they don't create stack spacing gaps\n#let title_lines_rendered = stack(\n dir: ttb,\n spacing: face_height * title_spacing / 100,\n ..(\n render_title(title1, font_title1_family, title1_size, title1_weight, title1_italic, color_front_title1, title1_dir),\n render_title(title2, font_title2_family, title2_size, title2_weight, title2_italic, color_front_title2, title2_dir),\n render_title(title3, font_title3_family, title3_size, title3_weight, title3_italic, color_front_title3, title3_dir),\n ).filter(x => x != none),\n)\n\n// All subtitle lines in one container — shrink-to-width scales them all together uniformly\n// text() wraps shrink-to-width so inner content inherits the scaled size (no explicit size inside)\n#let subtitle_lines_rendered = text(\n font: font_subtitle_family,\n size: fs_subtitle,\n weight: subtitle_weight,\n style: if subtitle_italic { \"italic\" } else { \"normal\" },\n fill: color_front_subtitle,\n box(\n width: front_content_w,\n shrink-to-width(\n stack(\n dir: ttb,\n spacing: face_height * subtitle_spacing / 100,\n ..subtitle_lines.zip(subtitle_dirs).map(((line, line_dir)) => align(\n subtitle_alignment, text(dir: line_dir, line),\n )),\n )\n ),\n ),\n)\n\n// Author styled text — single line, shrunk to fit width\n#let author_rendered = box(\n width: front_content_w,\n shrink-to-width(\n align(author_alignment, text(\n dir: author_dir,\n font: font_author_family,\n size: fs_author,\n fill: color_front_author,\n weight: author_weight,\n style: if author_italic { \"italic\" } else { \"normal\" },\n author,\n )),\n ),\n)\n\n// Title group: stacked title lines\n#let title_group = stack(\n dir: ttb,\n spacing: face_height * title_spacing / 100,\n title_lines_rendered,\n)\n\n// Check which elements belong to each position box\n#let top_has_title = title_position == \"top\"\n#let top_has_subtitle = has_subtitle and subtitle_position == \"top\"\n#let top_has_author = has_author and author_position == \"top\"\n#let top_has_any = top_has_title or top_has_subtitle or top_has_author\n\n#let mid_has_title = title_position == \"middle\"\n#let mid_has_subtitle = has_subtitle and subtitle_position == \"middle\"\n#let mid_has_author = has_author and author_position == \"middle\"\n#let mid_has_any = mid_has_title or mid_has_subtitle or mid_has_author\n\n#let bot_has_title = title_position == \"bottom\"\n#let bot_has_subtitle = has_subtitle and subtitle_position == \"bottom\"\n#let bot_has_author = has_author and author_position == \"bottom\"\n#let bot_has_any = bot_has_title or bot_has_subtitle or bot_has_author\n\n// Build a content group for a position box (order within box: title | subtitle | author)\n// Each element gets its own vertical margins inside the box\n#let make_pos_box(has_t, has_s, has_a) = box(\n width: front_content_w,\n stack(\n dir: ttb,\n if has_t { v(face_height * title_margin_top / 100) },\n if has_t { title_group },\n if has_t { v(face_height * title_margin_bottom / 100) },\n if has_s { v(face_height * subtitle_margin_top / 100) },\n if has_s { subtitle_lines_rendered },\n if has_s { v(face_height * subtitle_margin_bottom / 100) },\n if has_a { v(face_height * author_margin_top / 100) },\n if has_a { author_rendered },\n if has_a { v(face_height * author_margin_bottom / 100) },\n ),\n)\n\n// Place each non-empty position box on the front panel (within panel margin on all sides)\n#if top_has_any {\n place(\n top + left,\n dx: front_x + front_margin + spiral_margin,\n dy: bleed + front_margin,\n make_pos_box(top_has_title, top_has_subtitle, top_has_author),\n )\n}\n#if mid_has_any {\n place(\n horizon + left,\n dx: front_x + front_margin + spiral_margin,\n make_pos_box(mid_has_title, mid_has_subtitle, mid_has_author),\n )\n}\n#if bot_has_any {\n place(\n bottom + left,\n dx: front_x + front_margin + spiral_margin,\n dy: (bleed + front_margin) * -1,\n make_pos_box(bot_has_title, bot_has_subtitle, bot_has_author),\n )\n}\n\n\n// ============================================================\n// Layer 6 — Home-print margin (white rounded borders for home inkjet printers)\n// ============================================================\n\n#if home_print {\n // Render the back and front as two separate rounded cards, each with a white\n // border, so a home printer that can't reach the paper edge still prints\n // cleanly. One white matte covers the spread with two rounded windows punched\n // out — one per trim face, each inset by `home_margin` on all sides with\n // `home_corner`-rounded corners. Windows track the trim faces (not a spine-\n // centre split), so the bleed and spine become white gutter and every side —\n // including the inner/gutter edge — gets a real margin. A single matte (vs.\n // two abutting ones) avoids a hairline seam down the gutter.\n let m = home_margin\n let win_w = face_width - 2 * m\n let win_h = face_height - 2 * m\n let win_y = bleed + m\n let back_win = (bleed + m, win_y, win_w, win_h)\n let front_win = (total_width - bleed - face_width + m, win_y, win_w, win_h)\n home-print-matte(total_width, total_height, (back_win, front_win), home_corner)\n}\n",
|
|
6
|
-
helpers: "\n// Shared helper functions for the cover template\n\n// Scale content so it fits within the container width using geometric scaling.\n// Uses scale() rather than text(size: ...) so inner explicit font sizes are also scaled.\n#let fit-to-width(max-text-size: auto, min-text-size: 2mm, it) = context {\n let effective-max = if max-text-size == auto { text.size * 3 } else { max-text-size }\n let effective-min = if min-text-size == auto { text.size * 0.33 } else { min-text-size }\n // Express size limits as dimensionless ratios relative to current text size\n let max-ratio = effective-max / text.size\n let min-ratio = effective-min / text.size\n let contentsize = measure(it)\n layout(size => {\n // Content with no area has nothing to fit, and would divide by zero below. Zero height\n // with a nonzero width happens when a stack's spacing surrounds empty text whose font\n // isn't loaded (the web compiler only loads fonts for non-empty fields)\n if contentsize.width > 0mm and contentsize.height > 0mm {\n let ratio = calc.min(size.width / contentsize.width, size.height / contentsize.height)\n let clamped = calc.max(calc.min(ratio, max-ratio), min-ratio)\n if clamped != 1.0 {\n scale(x: clamped * 100%, y: clamped * 100%, reflow: true, it)\n } else {\n it\n }\n }\n })\n}\n\n// Variant that only shrinks (never grows beyond current text size)\n#let shrink-to-width(min-text-size:
|
|
5
|
+
cover: "\n// Book cover template — receives all variables from _data.typ\n#import \"_data.typ\": *\n\n// -- Page setup --\n#set page(\n width: total_width,\n height: total_height,\n margin: 0mm,\n)\n\n#set text(font: font_body_family)\n#set quote(block: true)\n#show quote: set pad(x: 3em)\n\n// ============================================================\n// Helper functions\n// ============================================================\n\n#import \"_helpers.typ\": *\n\n\n// ============================================================\n// Layer 1 — Background fills\n// ============================================================\n\n#place(\n top + left,\n rect(\n width: total_width,\n height: total_height,\n fill: if has_front_gradient {\n gradient.linear(\n (color_front_gradient_start, 0%),\n (color_front_bg, 30%),\n (color_front_bg, 45%),\n (color_front_gradient_end, 100%),\n angle: -40deg,\n )\n } else {\n color_front_bg\n },\n ),\n)\n\n\n#if has_pattern {\n let tiled = pattern(\n size: (pattern_tile_w, pattern_tile_h),\n image(\"pattern.svg\", width: pattern_tile_w, height: pattern_tile_h),\n )\n place(\n top + left,\n rect(width: total_width, height: total_height, fill: tiled),\n )\n}\n\n\n// ============================================================\n// Layer 2 — Background image\n// ============================================================\n\n#if has_image {\n if image_coverage == \"full\" {\n // Full spread: image covers entire canvas (back + spine + front)\n place(\n top + left,\n dx: 0mm,\n dy: 0mm,\n image(\n image_filename,\n width: total_width,\n height: total_height,\n fit: if image_is_vector { \"stretch\" } else { \"cover\" },\n ),\n )\n } else if image_coverage == \"painted\" or image_coverage == \"feature\" {\n // Painted/feature: image inset on the front cover at its own aspect ratio, as large as\n // fits in a box with 15mm side margins, starting a third of the way down and half the\n // face's height tall, centred in that box (sync: index.ts and dpi.ts inset box)\n let box_w = face_width - 30mm\n let box_h = face_height * 0.5\n context {\n // Fit the image's natural aspect ratio inside the box (like object-fit: contain)\n let natural = measure(image(image_filename))\n let ratio = natural.width / natural.height\n let (w, h) = if ratio > box_w / box_h {\n (box_w, box_w / ratio)\n } else {\n (box_h * ratio, box_h)\n }\n let inset = if image_coverage == \"feature\" {\n // Feature: rounded corners via box clip, the radius 4% of the image's shorter side\n // so it looks the same at any size (~6mm on an A4 cover)\n box(width: w, height: h, radius: calc.min(w, h) * 0.04, clip: true,\n image(image_filename, width: w, height: h, fit: \"stretch\"))\n } else {\n // Painted: the frame was already composited at this aspect ratio by frame_fn\n image(image_filename, width: w, height: h, fit: \"stretch\")\n }\n place(\n top + left,\n dx: front_x + 15mm + (box_w - w) / 2,\n dy: bleed + face_height * (1/3) + (box_h - h) / 2,\n inset,\n )\n }\n } else if image_coverage == \"front_partial\" {\n // Lower 2/3: image covers the full front face width, bottom 2/3 of height, flush to the bottom trim edge\n let w = face_width\n let h = face_height * (2 / 3)\n place(\n top + left,\n dx: front_x,\n dy: bleed + face_height - h,\n image(\n image_filename,\n width: w,\n height: h,\n fit: \"cover\",\n ),\n )\n } else {\n // Front only: image covers the front panel including all bleed margins\n place(\n top + left,\n dx: front_x,\n dy: 0mm,\n image(\n image_filename,\n width: face_width + bleed,\n height: total_height,\n fit: if image_is_vector { \"stretch\" } else { \"cover\" },\n ),\n )\n }\n}\n\n\n// ============================================================\n// Layer 2b — Icon on front cover\n// ============================================================\n\n#if has_icon {\n let icon_size = calc.min(face_height * 0.4, face_width * 0.8)\n if icon_mode != \"center\" {\n icon_size = calc.min(face_height * 0.5, face_width * 0.8)\n }\n // Apply user-controlled size multiplier (1.0 = default)\n icon_size = icon_size * icon_size_mod\n let offset_icon_dx = front_x + (face_width - icon_size) + (icon_size * 0.15)\n let offset_icon_dy = bleed * -1 + (icon_size * 0.15)\n\n // Place ghost icons first (if any) so below main one\n if icon_mode == \"echo\" {\n place(\n bottom + left,\n dx: offset_icon_dx + icon_size * 0.1,\n dy: offset_icon_dy - icon_size * 0.15,\n image(\"icon_ghost.svg\", width: icon_size, height: icon_size),\n )\n place(\n bottom + left,\n dx: offset_icon_dx - icon_size * 0.1,\n dy: offset_icon_dy + icon_size * 0.1,\n image(\"icon_ghost2.svg\", width: icon_size, height: icon_size),\n )\n }\n\n if icon_mode == \"center\" {\n // Center — large icon centered horizontally, positioned in the lower/mid section\n place(\n bottom + left,\n dx: front_x + (face_width - icon_size) / 2,\n dy: (face_height * 0.2 + bleed) * -1,\n image(\"icon_main.svg\", width: icon_size, height: icon_size),\n )\n } else {\n // Offset/echo: large icon offset towards bottom-right, slightly overflowing the trim edge\n place(\n bottom + left,\n dx: offset_icon_dx,\n dy: offset_icon_dy,\n image(\"icon_main.svg\", width: icon_size, height: icon_size),\n )\n }\n}\n\n\n// ============================================================\n// Layer 2c — Spine background\n// ============================================================\n\n// Spine background (only when spread has a spine) — drawn after pattern so it covers it\n#if has_spine and color_spine_bg != none {\n place(\n top + left,\n dx: spine_x,\n dy: 0mm,\n rect(width: spine_width, height: total_height, fill: color_spine_bg),\n )\n}\n\n\n// ============================================================\n// Layer 3 — Back panel content\n// ============================================================\n\n// back_margin, back_content_w, spiral_margin imported from _data.typ\n\n// Blurb container box with background color and 1cm padding around the text\n// Content is scaled down if it would overflow the available back-panel height\n#if has_blurb {\n let blurb_content = {\n set par(justify: true) if blurb_justify\n set par(leading: 1em * blurb_spacing)\n // Compact heading styles for a small blurb box — size and leading are hardcoded\n // independent of blurb_spacing, since headings rarely wrap and need tight leading\n show heading.where(level: 1): it => {\n set block(above: 2em, below: 1em)\n set text(size: 1.3em)\n set par(leading: 0.5em)\n it\n }\n show heading.where(level: 2): it => {\n set block(above: 2em, below: 1em)\n set text(size: 1.15em)\n set par(leading: 0.5em)\n it\n }\n align(blurb_alignment, text(\n font: font_blurb_family,\n size: fs_back_blurb * blurb_size,\n fill: color_blurb,\n blurb,\n ))\n }\n let blurb_max_h = back_content_h - blurb_padding * 2\n place(\n top + left,\n // Centered within the safe area, not the full face — spiral_margin is 0 for other bindings\n dx: back_x + (face_width - spiral_margin - blurb_width) / 2,\n dy: bleed + back_margin,\n rect(\n width: blurb_width,\n fill: color_blurb_bg,\n stroke: none,\n radius: 1.5mm,\n inset: blurb_padding,\n context {\n let avail_w = blurb_width - blurb_padding * 2\n let natural = measure(blurb_content, width: avail_w)\n if natural.height > blurb_max_h {\n let ratio = blurb_max_h / natural.height\n scale(x: ratio * 100%, y: ratio * 100%, reflow: true, blurb_content)\n } else {\n blurb_content\n }\n },\n ),\n )\n}\n\n// ISBN barcode — position and size from printing-services cover_region_barcode\n#if has_barcode {\n place(\n bottom + left,\n dx: barcode_x,\n dy: barcode_y * -1,\n image(\"barcode.svg\", width: barcode_w, height: barcode_h),\n )\n}\n\n// ============================================================\n// Layer 4 — Spine content\n// ============================================================\n\n#if has_spine and has_spine_text and fs_spine_title > 0mm {\n // Each spine item only when it has text, so an empty one leaves no stack spacing gap\n // pushing the other off-center\n let spine_items = (\n if spine_title != \"\" {\n text(\n dir: spine_title_dir,\n font: font_spine_title_family,\n size: fs_spine_title * spine_title_size_mod,\n fill: color_spine_title,\n weight: spine_title_weight,\n style: if spine_title_italic { \"italic\" } else { \"normal\" },\n spine_title,\n )\n },\n if spine_author != \"\" and fs_spine_author > 0mm {\n text(\n dir: spine_author_dir,\n font: font_spine_author_family,\n size: fs_spine_author * spine_author_size_mod,\n fill: color_spine_author,\n weight: spine_author_weight,\n style: if spine_author_italic { \"italic\" } else { \"normal\" },\n spine_author,\n )\n },\n ).filter(x => x != none)\n\n // Title rotated along spine (bottom-to-top reading direction).\n // place() anchors the unrotated bounding box top-left, so we subtract\n // half the unrotated dimensions to keep the visual center on the spine.\n // The title + author pair runs in the title's direction, so for RTL the title\n // comes first reading right-to-left (bottom-to-top once rotated).\n if spine_items.len() > 0 {\n place(\n top + left,\n dx: spine_x + spine_width * 0.5 - face_height * 0.4,\n dy: bleed + face_height * 0.5 - spine_width * 0.5,\n rotate(\n 90deg,\n origin: center,\n box(\n width: face_height * 0.8,\n height: spine_width,\n align(\n center + horizon,\n // Shrink title + author together as one unit if they overflow the spine length\n shrink-to-width(\n stack(dir: spine_dir, spacing: spine_width * 0.3, ..spine_items),\n ),\n ),\n ),\n ),\n )\n }\n}\n\n// Small icon at the bottom of the spine (when spine is wide enough for text)\n#if has_spine_icon {\n let spine_icon_size = spine_width * 0.7\n let spine_icon_side_margin = (spine_width - spine_icon_size) / 2\n // Bottom margin is twice the side margin\n let spine_icon_bottom_margin = spine_icon_side_margin * 2\n place(\n top + left,\n dx: spine_x + spine_icon_side_margin,\n dy: bleed + face_height - spine_icon_size - spine_icon_bottom_margin,\n image(\"icon_spine.svg\", width: spine_icon_size, height: spine_icon_size),\n )\n}\n\n// ============================================================\n// Layer 5 — Front panel content\n// ============================================================\n\n// front_margin, front_content_w, spiral_margin imported from _data.typ\n\n// Base font size — proportional to trim height, multiplied by per-title size modifier\n#let fs_title_base = face_height * 0.05\n#let fs_subtitle = face_height * 0.025 * subtitle_size\n#let fs_author = face_height * 0.035 * author_size\n\n// Render a single title line with its per-title styling, shrunk to fit if too wide. Aligned\n// outside the shrink so a scaled-down line (narrower than the box) still sits where it should\n#let render_title(txt, font_family, size_mod, weight, is_italic, color, txt_dir) = {\n if txt != \"\" {\n box(\n width: front_content_w,\n align(title_alignment, shrink-to-width(\n text(\n dir: txt_dir,\n font: font_family,\n size: fs_title_base * size_mod,\n fill: color,\n weight: weight,\n style: if is_italic { \"italic\" } else { \"normal\" },\n txt,\n ),\n )),\n )\n }\n}\n\n// Render each title line, then filter out empty ones so they don't create stack spacing gaps\n#let title_lines_rendered = stack(\n dir: ttb,\n spacing: face_height * title_spacing / 100,\n ..(\n render_title(title1, font_title1_family, title1_size, title1_weight, title1_italic, color_front_title1, title1_dir),\n render_title(title2, font_title2_family, title2_size, title2_weight, title2_italic, color_front_title2, title2_dir),\n render_title(title3, font_title3_family, title3_size, title3_weight, title3_italic, color_front_title3, title3_dir),\n ).filter(x => x != none),\n)\n\n// All subtitle lines in one container — shrink-to-width scales them all together uniformly\n// text() wraps shrink-to-width so inner content inherits the scaled size (no explicit size inside)\n#let subtitle_lines_rendered = text(\n font: font_subtitle_family,\n size: fs_subtitle,\n weight: subtitle_weight,\n style: if subtitle_italic { \"italic\" } else { \"normal\" },\n fill: color_front_subtitle,\n box(\n width: front_content_w,\n // Aligned outside the shrink too, so a scaled-down block sits where its lines do\n align(subtitle_alignment, shrink-to-width(\n stack(\n dir: ttb,\n spacing: face_height * subtitle_spacing / 100,\n ..subtitle_lines.zip(subtitle_dirs).map(((line, line_dir)) => align(\n subtitle_alignment, text(dir: line_dir, line),\n )),\n )\n )),\n ),\n)\n\n// Author styled text — single line, shrunk to fit width and aligned outside the shrink\n#let author_rendered = box(\n width: front_content_w,\n align(author_alignment, shrink-to-width(\n text(\n dir: author_dir,\n font: font_author_family,\n size: fs_author,\n fill: color_front_author,\n weight: author_weight,\n style: if author_italic { \"italic\" } else { \"normal\" },\n author,\n ),\n )),\n)\n\n// Title group: stacked title lines\n#let title_group = stack(\n dir: ttb,\n spacing: face_height * title_spacing / 100,\n title_lines_rendered,\n)\n\n// Check which elements belong to each position box\n#let top_has_title = title_position == \"top\"\n#let top_has_subtitle = has_subtitle and subtitle_position == \"top\"\n#let top_has_author = has_author and author_position == \"top\"\n#let top_has_any = top_has_title or top_has_subtitle or top_has_author\n\n#let mid_has_title = title_position == \"middle\"\n#let mid_has_subtitle = has_subtitle and subtitle_position == \"middle\"\n#let mid_has_author = has_author and author_position == \"middle\"\n#let mid_has_any = mid_has_title or mid_has_subtitle or mid_has_author\n\n#let bot_has_title = title_position == \"bottom\"\n#let bot_has_subtitle = has_subtitle and subtitle_position == \"bottom\"\n#let bot_has_author = has_author and author_position == \"bottom\"\n#let bot_has_any = bot_has_title or bot_has_subtitle or bot_has_author\n\n// Build a content group for a position box (order within box: title | subtitle | author)\n// Each element gets its own vertical margins inside the box\n#let make_pos_box(has_t, has_s, has_a) = box(\n width: front_content_w,\n stack(\n dir: ttb,\n if has_t { v(face_height * title_margin_top / 100) },\n if has_t { title_group },\n if has_t { v(face_height * title_margin_bottom / 100) },\n if has_s { v(face_height * subtitle_margin_top / 100) },\n if has_s { subtitle_lines_rendered },\n if has_s { v(face_height * subtitle_margin_bottom / 100) },\n if has_a { v(face_height * author_margin_top / 100) },\n if has_a { author_rendered },\n if has_a { v(face_height * author_margin_bottom / 100) },\n ),\n)\n\n// Place each non-empty position box on the front panel (within panel margin on all sides)\n#if top_has_any {\n place(\n top + left,\n dx: front_x + front_margin + spiral_margin,\n dy: bleed + front_margin,\n make_pos_box(top_has_title, top_has_subtitle, top_has_author),\n )\n}\n#if mid_has_any {\n place(\n horizon + left,\n dx: front_x + front_margin + spiral_margin,\n make_pos_box(mid_has_title, mid_has_subtitle, mid_has_author),\n )\n}\n#if bot_has_any {\n place(\n bottom + left,\n dx: front_x + front_margin + spiral_margin,\n dy: (bleed + front_margin) * -1,\n make_pos_box(bot_has_title, bot_has_subtitle, bot_has_author),\n )\n}\n\n\n// ============================================================\n// Layer 6 — Home-print margin (white rounded borders for home inkjet printers)\n// ============================================================\n\n#if home_print {\n // Render the back and front as two separate rounded cards, each with a white\n // border, so a home printer that can't reach the paper edge still prints\n // cleanly. One white matte covers the spread with two rounded windows punched\n // out — one per trim face, each inset by `home_margin` on all sides with\n // `home_corner`-rounded corners. Windows track the trim faces (not a spine-\n // centre split), so the bleed and spine become white gutter and every side —\n // including the inner/gutter edge — gets a real margin. A single matte (vs.\n // two abutting ones) avoids a hairline seam down the gutter.\n let m = home_margin\n let win_w = face_width - 2 * m\n let win_h = face_height - 2 * m\n let win_y = bleed + m\n let back_win = (bleed + m, win_y, win_w, win_h)\n let front_win = (total_width - bleed - face_width + m, win_y, win_w, win_h)\n home-print-matte(total_width, total_height, (back_win, front_win), home_corner)\n}\n",
|
|
6
|
+
helpers: "\n// Shared helper functions for the cover template\n\n// Scale content so it fits within the container width using geometric scaling.\n// Uses scale() rather than text(size: ...) so inner explicit font sizes are also scaled.\n#let fit-to-width(max-text-size: auto, min-text-size: 2mm, it) = context {\n let effective-max = if max-text-size == auto { text.size * 3 } else { max-text-size }\n let effective-min = if min-text-size == auto { text.size * 0.33 } else { min-text-size }\n // Express size limits as dimensionless ratios relative to current text size\n let max-ratio = effective-max / text.size\n let min-ratio = effective-min / text.size\n let contentsize = measure(it)\n layout(size => {\n // Content with no area has nothing to fit, and would divide by zero below. Zero height\n // with a nonzero width happens when a stack's spacing surrounds empty text whose font\n // isn't loaded (the web compiler only loads fonts for non-empty fields)\n if contentsize.width > 0mm and contentsize.height > 0mm {\n let ratio = calc.min(size.width / contentsize.width, size.height / contentsize.height)\n let clamped = calc.max(calc.min(ratio, max-ratio), min-ratio)\n if clamped != 1.0 {\n // Lay the content out at its own measured width before scaling, so it can never\n // wrap: reflow would otherwise re-lay it in the available width. The scaled\n // block is narrower than the container, so callers align it from outside\n scale(x: clamped * 100%, y: clamped * 100%, reflow: true,\n box(width: contentsize.width, it))\n } else {\n it\n }\n }\n })\n}\n\n// Variant that only shrinks (never grows beyond current text size), with no floor by default so\n// the content always fits rather than overflowing its container\n#let shrink-to-width(min-text-size: 0pt, it) = context {\n fit-to-width(max-text-size: text.size, min-text-size: min-text-size, it)\n}\n\n// Curve segments tracing one rounded rectangle (absolute coords), for compositing\n// into a larger path. Corners use a cubic-bezier handle (0.5523r) to approximate arcs.\n#let rounded-subpath(x, y, w, h, r) = {\n let k = 0.5523 * r\n (\n curve.move((x + r, y), relative: false),\n curve.line((x + w - r, y), relative: false),\n curve.cubic((x + w - r + k, y), (x + w, y + r - k), (x + w, y + r), relative: false),\n curve.line((x + w, y + h - r), relative: false),\n curve.cubic((x + w, y + h - r + k), (x + w - r + k, y + h), (x + w - r, y + h), relative: false),\n curve.line((x + r, y + h), relative: false),\n curve.cubic((x + r - k, y + h), (x, y + h - r + k), (x, y + h - r), relative: false),\n curve.line((x, y + r), relative: false),\n curve.cubic((x, y + r - k), (x + r - k, y), (x + r, y), relative: false),\n curve.close(),\n )\n}\n\n// White \"matte\" covering the whole canvas (cw × ch) with rounded-rect windows\n// punched out via even-odd fill, so the artwork shows through each window as a\n// rounded card surrounded by a white border. Used for home-print margins on\n// edge-to-edge-incapable printers. `windows` is an array of (x, y, w, h) tuples.\n#let home-print-matte(cw, ch, windows, r) = {\n let segs = (\n curve.move((0mm, 0mm), relative: false),\n curve.line((cw, 0mm), relative: false),\n curve.line((cw, ch), relative: false),\n curve.line((0mm, ch), relative: false),\n curve.close(),\n )\n for win in windows {\n segs = segs + rounded-subpath(win.at(0), win.at(1), win.at(2), win.at(3), r)\n }\n place(top + left, curve(fill: white, fill-rule: \"even-odd\", ..segs))\n}\n",
|
|
7
7
|
};
|
|
8
8
|
//# sourceMappingURL=templates_data.js.map
|
package/dist/icon_cache.js
CHANGED
|
@@ -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
|
|
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.js
CHANGED
|
@@ -200,12 +200,13 @@ export async function build(schema, image, frame_fn, frame_data, templates = DEF
|
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
// Pre-process image with frame effect when bg_image_coverage is 'painted' and a frame_fn was
|
|
203
|
-
// injected — never applies to a generated vector background (canvas framing needs a photo)
|
|
203
|
+
// injected — never applies to a generated vector background (canvas framing needs a photo).
|
|
204
|
+
// The painted box is the most the frame may fill; frame_fn keeps the image's aspect ratio.
|
|
204
205
|
let processed_image = image ?? vector_image;
|
|
205
206
|
if (image && schema_resolved.bg_image_coverage === 'painted' && frame_fn) {
|
|
206
207
|
const margin_mm = 15;
|
|
207
208
|
const painted_w = dims.cover_face_width.toNumber() - 2 * margin_mm;
|
|
208
|
-
const painted_h = dims.cover_face_height.toNumber() * 0.5; // sync: cover.typ
|
|
209
|
+
const painted_h = dims.cover_face_height.toNumber() * 0.5; // sync: cover.typ box_h = face_height * 0.5
|
|
209
210
|
const width_px = Math.round(painted_w / 25.4 * 300);
|
|
210
211
|
const height_px = Math.round(painted_h / 25.4 * 300);
|
|
211
212
|
const mime = image.ext === '.png' ? 'image/png' : 'image/jpeg';
|
|
@@ -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.
|
|
3
|
+
"version": "0.24.1",
|
|
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",
|