From e36e758c1c291e298d4285078d4cc28bcc54f33f Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Thu, 19 Nov 2026 16:26:29 -0700 Subject: [PATCH] Shrink the call number tab, add a seat line, print at actual size The two call number lines now take 0.56 in at the top of the card instead of about 0.9 in, with a full-width line under them that marks where the top edge of the book's pages sits. PDFs ask the viewer to print unscaled, and the calibration page has a 6 inch line to check the print scale. Co-Authored-By: Claude Fable 5.2 --- src/server/pdf/card.test.ts | 71 ++++++++++++++++++++++++--------- src/server/pdf/card.ts | 44 +++++++++++--------- src/server/pdf/render.test.ts | 10 ++++- src/server/pdf/render.ts | 31 ++++++++++++-- src/web/components/Settings.tsx | 2 +- src/web/views/CardsToPrint.tsx | 2 +- 6 files changed, 118 insertions(+), 42 deletions(-) diff --git a/src/server/pdf/card.test.ts b/src/server/pdf/card.test.ts index 886f12a..75c6830 100644 --- a/src/server/pdf/card.test.ts +++ b/src/server/pdf/card.test.ts @@ -1,6 +1,6 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { layoutCard as layout, wrap } from './card.ts'; +import { layoutCard as layout, wrap, SEAT_Y } from './card.ts'; import type { CardData } from '../../shared/types.ts'; const measure = (t: string, size: number, _b: boolean) => Array.from(t).length * size * 0.55; @@ -25,7 +25,10 @@ function checkBounds(runs: ReturnType) { test('call number in top inch; everything inside margins', () => { const runs = layoutCard(base, measure); checkBounds(runs); - assert.ok(runs[0]!.y >= 288 && runs[1]!.y >= 288); + assert.equal(runs[0]!.y, 339); + assert.equal(runs[1]!.y, 324.5); + assert.equal(runs[0]!.size, 16); + assert.equal(runs[1]!.size, 13); assert.equal(runs[0]!.text, 'QA76.73.R87'); assert.equal(runs[1]!.text, 'K53 2018'); }); @@ -34,7 +37,7 @@ test('300 character title takes at most 3 runs and ends with an ellipsis', () => const title = Array.from({ length: 50 }, (_, i) => `word${i}`).join(' ').slice(0, 300); const runs = layoutCard({ ...base, title }, measure); checkBounds(runs); - const t = runs.filter((r) => r.bold && r.size <= 13 && r.size >= 11 && r.y < 288 && r.y > 100 && r.text !== '~'); + const t = runs.filter((r) => r.bold && r.size <= 13 && r.size >= 11 && r.y < 320 && r.y > 100 && r.text !== '~'); assert.ok(t.length <= 3 && t.length >= 1); assert.ok(t[t.length - 1]!.text.endsWith('…')); }); @@ -50,17 +53,17 @@ test('twelve authors end with et al.', () => { test('three-cutter call number shrinks and fits', () => { const runs = layoutCard({ ...base, callNumber: 'QA76.73.C153 S77 K47 2013' }, measure); checkBounds(runs); - assert.ok(runs[0]!.size < 26 || runs[0]!.text.length > 0); + assert.ok(runs[0]!.size < 16 || runs[0]!.text.length > 0); assert.ok(widthOf(runs[0]!) <= 188); - const long = layoutCard({ ...base, callNumber: 'QA76.73.C153999999 S77' }, measure); - assert.ok(long[0]!.size < 26); + const long = layoutCard({ ...base, callNumber: 'QA76.73.C15399999999999 S77' }, measure); + assert.ok(long[0]!.size < 16 && long[0]!.size >= 10); assert.ok(widthOf(long[0]!) <= 188); }); test('unparseable call number prints whole on line 1', () => { const runs = layoutCard({ ...base, callNumber: 'garbage' }, measure); assert.equal(runs[0]!.text, 'garbage'); - assert.ok(!runs.some((r) => r.y === 300)); + assert.ok(!runs.some((r) => r.y === 324.5)); }); test('wrap breaks a word wider than the line and terminates', () => { @@ -77,19 +80,19 @@ test('omitted pieces: no duplicate level, no isbn, no tilde', () => { assert.ok(!runs.some((r) => r.text.startsWith('ISBN') || r.text === '~')); }); -test('no text block goes below y 30 (above the ISBN line)', () => { +test('no text block goes below y 24 (above the ISBN line)', () => { const many: CardData = { ...base, title: 'A long title '.repeat(12), authors: ['One Author', 'Two Author', 'Three Author'], }; const runs = layoutCard(many, measure); - assert.ok(runs.filter((r) => r.y > 20).every((r) => r.y > 30)); - assert.ok(runs.some((r) => r.text.startsWith('ISBN') && r.y === 16)); + assert.ok(runs.filter((r) => r.y > 15).every((r) => r.y > 24)); + assert.ok(runs.some((r) => r.text.startsWith('ISBN') && r.y === 10)); }); // ---- the hierarchy tree, the rule and the space priority ---- -const levelRuns = (runs: ReturnType, rule: number) => - runs.filter((r) => r.size <= 8.5 && r.size >= 7 && r.y < 292 && r.y > rule && r.text !== '~' && !r.text.startsWith('ISBN')); +const levelRuns = (runs: ReturnType, rule: number | null) => + runs.filter((r) => r.size <= 8.5 && r.size >= 7 && r.y < 310 && r.y > (rule ?? 0) && r.text !== '~' && !r.text.startsWith('ISBN')); const GV = ['Recreation. Leisure', 'Outdoor life. Outdoor recreation', 'Hiking. Pedestrian tours', 'Utah', 'Uinta Mountains']; const gv = (outline: string[], tail: string[] = []): CardData => ({ ...base, callNumber: 'GV199.42.U82 U564 2024', hierarchy: { outline, tail } }); @@ -107,9 +110,9 @@ test('a five-level tree: one level per line, general first, each 7 pt further in }); assert.equal(l.connectors.length, 8); // an elbow (down, across) from each parent to its child for (const c of l.connectors) { assert.equal(c.thickness, 0.5); assert.ok(c.x1 === c.x2 || c.y1 === c.y2); } - assert.ok(l.ruleY < lv[4]!.y && Math.abs(lv[4]!.y - l.ruleY - 8) < 1e-9, 'the rule sits 8 pt under the last line'); + assert.ok(l.ruleY! < lv[4]!.y && Math.abs(lv[4]!.y - l.ruleY! - 8) < 1e-9, 'the rule sits 8 pt under the last line'); const title = l.runs.find((r) => r.text.startsWith('The Rust'))!; - assert.ok(title.y < l.ruleY, 'WHAT is below the rule'); + assert.ok(title.y < l.ruleY!, 'WHAT is below the rule'); assert.ok(!l.runs.some((r) => r.text.startsWith('Geography')), 'starts at the subclass'); }); @@ -121,7 +124,7 @@ test('one level: a single plain gray line, no connector', () => { assert.equal(lv[0]!.gray, 0.35); assert.equal(l.connectors.length, 0); const none = layout({ ...base, hierarchy: { outline: [], tail: [] } }, measure); - assert.equal(none.ruleY, 286); + assert.equal(none.ruleY, null); assert.equal(none.connectors.length, 0); }); @@ -156,7 +159,7 @@ test('title and authors are kept: the publisher goes first, then tree levels (no const make = (levels: string[], pub = 'No Starch Press'): CardData => ({ ...gv(['Main', ...levels.slice(0, -1)], levels.slice(-1)), title, authors: ['First Author', 'Second Author'], publisher: pub, }); - const inWhat = (l: ReturnType) => l.runs.filter((r) => r.y < l.ruleY && r.y > 30); + const inWhat = (l: ReturnType) => l.runs.filter((r) => r.y < l.ruleY! && r.y > 24); const levelCount = (l: ReturnType) => new Set(levelRuns(l.runs, l.ruleY).map((r) => r.x)).size; // Plenty of room: everything prints, including the publisher. @@ -165,7 +168,7 @@ test('title and authors are kept: the publisher goes first, then tree levels (no assert.ok(inWhat(roomy).some((r) => r.text.includes('No Starch'))); // Five tall levels: the publisher is the first thing to go; the tree and title and authors stay. - const tall = layout(make(levels5(3, 24)), measure); + const tall = layout(make(levels5(3, 28)), measure); assert.equal(inWhat(tall).filter((r) => r.bold && r.size === 13).length, 3, 'title'); assert.ok(inWhat(tall).some((r) => r.text.includes('Author')), 'authors'); assert.ok(!inWhat(tall).some((r) => r.text.includes('No Starch')), 'publisher dropped first'); @@ -178,5 +181,37 @@ test('title and authors are kept: the publisher goes first, then tree levels (no assert.ok(inWhat(huge).some((r) => r.bold && r.size >= 11), 'title'); assert.ok(inWhat(huge).some((r) => r.text.includes('Author')), 'authors'); assert.equal(levelRuns(huge.runs, huge.ruleY).at(-1)!.text, 'E', 'the specific end is kept'); - assert.ok(huge.runs.filter((r) => r.y > 20).every((r) => r.y > 30)); + assert.ok(huge.runs.filter((r) => r.y > 15).every((r) => r.y > 24)); +}); + +test('the seat line is at 320, always present, even with no tree', () => { + assert.equal(SEAT_Y, 320); + assert.equal(layout(base, measure).seatY, 320); + assert.equal(layout({ ...base, hierarchy: { outline: [], tail: [] } }, measure).seatY, 320); + // nothing in the tab straddles it: call number text sits above, the tree below + const l = layout(gv(['Main', ...GV.slice(0, 3)], GV.slice(3)), measure); + for (const r of l.runs) assert.ok(r.y > 320 || r.y + r.size < 320 || r.y < 320 - r.size * 0.2, r.text); +}); + +test('no tree: no hairline, and the title starts about 12 pt under the seat line', () => { + const l = layout({ ...base, hierarchy: { outline: [], tail: [] } }, measure); + assert.equal(l.ruleY, null); + const title = l.runs.find((r) => r.text.startsWith('The Rust'))!; + const gap = 320 - (title.y + title.size * 0.73); // cap height ~0.73 em + assert.ok(gap >= 11 && gap <= 13, `gap ${gap}`); + const withTree = layout(base, measure); + assert.ok(withTree.ruleY != null && withTree.ruleY < 310); +}); + +test('worst case: five levels with one wrapped, 3-line title, two authors fits above the floor, publisher dropped first', () => { + const words = Array.from({ length: 14 }, (_, i) => `word${i}`).join(' '); + const l = layout({ + ...gv(['Main', 'A', 'B'], ['C', words]), title: 'A long title '.repeat(12), + authors: ['First Author', 'Second Author'], publisher: 'No Starch Press', + }, measure); + const body = l.runs.filter((r) => r.y < l.ruleY! && r.y > 15 && r.text !== '~'); + assert.ok(body.every((r) => r.y - r.size * 0.2 >= 24 - 1e-9), 'above the floor'); + assert.ok(body.some((r) => r.text.includes('Author'))); + const ruleToTitleTop = l.ruleY! - 18; + assert.ok(ruleToTitleTop > 24); }); diff --git a/src/server/pdf/card.ts b/src/server/pdf/card.ts index b2e5650..2ee0528 100644 --- a/src/server/pdf/card.ts +++ b/src/server/pdf/card.ts @@ -7,12 +7,15 @@ export type Measure = (text: string, size: number, bold: boolean) => number; export const MARGIN_X = 14; export const TEXT_WIDTH = 188; -/** Where the rule sits when there is no hierarchy tree above it. */ -export const RULE_Y = 286; +/** The seat line: where the top edge of the book's pages sits. Full card width, always drawn. */ +export const SEAT_Y = 320; +/** Top of the title block when there is no tree (about 12 pt from the seat line to the top of the capitals). */ +const NO_TREE_TOP = SEAT_Y - 10.5; const GRAY = 0.35; const BLOCK_GAP = 8; -/** Text stays above the ISBN line (baseline 16, 8 pt). */ -const BLOCK_FLOOR = 30; +/** Text stays above the ISBN line (baseline 10, 8 pt). */ +const BLOCK_FLOOR = 24; +const ISBN_Y = 10; const ELLIPSIS = '…'; /** Largest size (1 pt steps, down to min) at which text fits; text is truncated if min still overflows. */ @@ -81,12 +84,14 @@ export interface CardLayout { runs: TextRun[]; /** The tree's elbow connectors, as thin gray vector lines. */ connectors: Segment[]; - /** Where the hairline rule between WHERE (call number, tree) and WHAT (title, authors) goes. */ - ruleY: number; + /** Where the hairline rule between WHERE (call number, tree) and WHAT (title, authors) goes; null when there is no tree. */ + ruleY: number | null; + /** The full-width seat line marking the top edge of the book's pages. */ + seatY: number; } // The hierarchy tree under the call number: one level per line, general at the top, each level indented further. -export const TREE_TOP = 292; +export const TREE_TOP = 310; export const TREE_PITCH = 10.5; const TREE_INDENT = 7; const TREE_SIZE = 8.5; @@ -97,7 +102,7 @@ const TREE_GRAY = 0.35; const CONNECTOR_GRAY = 0.5; const RULE_GAP = 8; -interface Tree { runs: TextRun[]; connectors: Segment[]; ruleY: number } +interface Tree { runs: TextRun[]; connectors: Segment[]; ruleY: number | null } /** One level: the largest size down to 7 pt at which it fits on one line; else wrapped (shrunk as needed to fit two lines). */ function fitLevel(text: string, width: number, bold: boolean, m: Measure): { size: number; lines: string[] } { @@ -134,10 +139,13 @@ function layoutTree(levels: string[], m: Measure): Tree { prev = { x, lastBaseline: baselineOf(line + lines.length - 1) }; line += lines.length; } - const ruleY = prev ? prev.lastBaseline - RULE_GAP : RULE_Y; + const ruleY = prev ? prev.lastBaseline - RULE_GAP : null; return { runs, connectors, ruleY }; } +/** Top of the title block: 18 pt under the hairline, or just under the seat line when there is no tree. */ +const blocksTop = (t: Tree) => (t.ruleY == null ? NO_TREE_TOP : t.ruleY - 18); + /** What goes below the rule, in priority order; each block is dropped whole if it does not fit above the ISBN line. */ function whatBlocks(card: CardData, measure: Measure): { required: Block[]; publisher: Block | null } { const required: Block[] = []; @@ -172,11 +180,11 @@ export function layoutCard(card: CardData, measure: Measure): CardLayout { const runs: TextRun[] = []; const cn = parseCallNumber(card.callNumber); const [l1, l2] = cn ? cardLines(cn) : [card.callNumber, '']; - const a = fitLine(l1, 26, 12, true, measure); - runs.push({ text: a.text, x: MARGIN_X, y: 326, size: a.size, bold: true, gray: 0 }); + const a = fitLine(l1, 16, 10, true, measure); + runs.push({ text: a.text, x: MARGIN_X, y: 339, size: a.size, bold: true, gray: 0 }); if (l2) { - const b = fitLine(l2, 20, 11, true, measure); - runs.push({ text: b.text, x: MARGIN_X, y: 300, size: b.size, bold: true, gray: 0 }); + const b = fitLine(l2, 13, 9, true, measure); + runs.push({ text: b.text, x: MARGIN_X, y: 324.5, size: b.size, bold: true, gray: 0 }); } const all = displayLevels(card.hierarchy).slice(-TREE_MAX_LEVELS); @@ -184,12 +192,12 @@ export function layoutCard(card: CardData, measure: Measure): CardLayout { let tree = layoutTree(all, measure); for (let drop = 0; drop <= all.length - TREE_MIN_LEVELS; drop++) { tree = layoutTree(all.slice(drop), measure); - if (stackBottom(tree.ruleY - 18, required) >= BLOCK_FLOOR) break; + if (stackBottom(blocksTop(tree), required) >= BLOCK_FLOOR) break; } runs.push(...tree.runs); const blocks = publisher ? [...required, publisher] : required; - let top = tree.ruleY - 18; + let top = blocksTop(tree); for (const b of blocks) { const lh = b.size * LEADING; const bottom = top - b.lines.length * lh; @@ -201,11 +209,11 @@ export function layoutCard(card: CardData, measure: Measure): CardLayout { } if (card.isbn13) { - runs.push({ text: `ISBN ${card.isbn13}`, x: MARGIN_X, y: 16, size: 8, bold: false, gray: 0 }); + runs.push({ text: `ISBN ${card.isbn13}`, x: MARGIN_X, y: ISBN_Y, size: 8, bold: false, gray: 0 }); } if (card.approximate) { const w = measure('~', 12, true); - runs.push({ text: '~', x: MARGIN_X + TEXT_WIDTH - w, y: 16, size: 12, bold: true, gray: 0 }); + runs.push({ text: '~', x: MARGIN_X + TEXT_WIDTH - w, y: ISBN_Y, size: 12, bold: true, gray: 0 }); } - return { runs, connectors: tree.connectors, ruleY: tree.ruleY }; + return { runs, connectors: tree.connectors, ruleY: tree.ruleY, seatY: SEAT_Y }; } diff --git a/src/server/pdf/render.test.ts b/src/server/pdf/render.test.ts index ff4fe2c..378e05a 100644 --- a/src/server/pdf/render.test.ts +++ b/src/server/pdf/render.test.ts @@ -1,6 +1,6 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { PDFDocument } from 'pdf-lib'; +import { PDFDocument, PrintScaling } from 'pdf-lib'; import { renderCardsPdf, renderCalibrationPdf } from './render.ts'; import type { CardData } from '../../shared/types.ts'; @@ -29,3 +29,11 @@ test('calibration page renders one page', async () => { const bytes = await renderCalibrationPdf({ offsetXmm: 0.5, offsetYmm: -0.3 }); assert.equal((await PDFDocument.load(bytes)).getPageCount(), 1); }); + +test('both PDFs ask the print dialog for actual size', async () => { + const cards = await PDFDocument.load(await renderCardsPdf([card], { ...zero, startPosition: 1 })); + const calib = await PDFDocument.load(await renderCalibrationPdf(zero)); + for (const d of [cards, calib]) { + assert.equal(d.catalog.getViewerPreferences()?.getPrintScaling(), PrintScaling.None); + } +}); diff --git a/src/server/pdf/render.ts b/src/server/pdf/render.ts index 2074376..dee5f15 100644 --- a/src/server/pdf/render.ts +++ b/src/server/pdf/render.ts @@ -1,7 +1,7 @@ import { readFile } from 'node:fs/promises'; import { createRequire } from 'node:module'; import path from 'node:path'; -import { PDFDocument, rgb, pushGraphicsState, popGraphicsState, concatTransformationMatrix } from 'pdf-lib'; +import { PDFDocument, PrintScaling, rgb, pushGraphicsState, popGraphicsState, concatTransformationMatrix } from 'pdf-lib'; import type { PDFFont, PDFPage } from 'pdf-lib'; import fontkit from '@pdf-lib/fontkit'; import type { CardData, Settings } from '../../shared/types.ts'; @@ -26,6 +26,8 @@ function loadFontBytes() { async function setup(): Promise<{ doc: PDFDocument; fonts: Fonts }> { const bytes = await loadFontBytes(); const doc = await PDFDocument.create(); + // Ask the print dialog for "actual size" so the page is not shrunk to the printable area. + doc.catalog.getOrCreateViewerPreferences().setPrintScaling(PrintScaling.None); doc.registerFontkit(fontkit); const regular = await doc.embedFont(bytes.regular, { subset: false }); const bold = await doc.embedFont(bytes.bold, { subset: false }); @@ -69,9 +71,15 @@ function drawCard(page: PDFPage, card: CardData, fonts: Fonts) { for (const c of layout.connectors) { page.drawLine({ start: { x: c.x1, y: c.y1 }, end: { x: c.x2, y: c.y2 }, thickness: c.thickness, color: rgb(c.gray, c.gray, c.gray) }); } + if (layout.ruleY != null) { + page.drawLine({ + start: { x: MARGIN_X, y: layout.ruleY }, end: { x: MARGIN_X + TEXT_WIDTH, y: layout.ruleY }, + thickness: 0.75, color: rgb(0, 0, 0), + }); + } + // The seat line: full card width, heavier than the hairline; the book's page edge sits on it. page.drawLine({ - start: { x: MARGIN_X, y: layout.ruleY }, end: { x: MARGIN_X + TEXT_WIDTH, y: layout.ruleY }, - thickness: 0.75, color: rgb(0, 0, 0), + start: { x: 0, y: layout.seatY }, end: { x: CARD.width, y: layout.seatY }, thickness: 0.75, color: rgb(0, 0, 0), }); } @@ -90,6 +98,22 @@ export async function renderCardsPdf( return doc.save(); } +/** A 6 in line in plain sheet coordinates (unaffected by offsets), in the free 72 pt strip at the bottom of the sheet. */ +function drawScaleCheck(page: PDFPage, fonts: Fonts) { + const len = 432, x0 = (SHEET.width - len) / 2, y = 30, black = rgb(0, 0, 0); + page.drawLine({ start: { x: x0, y }, end: { x: x0 + len, y }, thickness: 0.5, color: black }); + for (const x of [x0, x0 + len]) page.drawLine({ start: { x, y: y - 5 }, end: { x, y: y + 5 }, thickness: 0.5, color: black }); + const msg = 'This line must measure exactly 6 in (152.4 mm). If it is shorter, the print dialog is shrinking the page: print at 100% / "Actual size".'; + let line = '', ty = y - 14; + for (const w of msg.split(' ')) { + const next = line ? line + ' ' + w : w; + if (fonts.regular.widthOfTextAtSize(next, 8) > len && line) { + page.drawText(line, { x: x0, y: ty, size: 8, font: fonts.regular }); ty -= 10; line = w; + } else line = next; + } + page.drawText(line, { x: x0, y: ty, size: 8, font: fonts.regular }); +} + export async function renderCalibrationPdf(s: Settings): Promise { const { doc, fonts } = await setup(); const page = doc.addPage([SHEET.width, SHEET.height]); @@ -116,5 +140,6 @@ export async function renderCalibrationPdf(s: Settings): Promise { } }); } + drawScaleCheck(page, fonts); return doc.save(); } diff --git a/src/web/components/Settings.tsx b/src/web/components/Settings.tsx index be02aa0..25ca035 100644 --- a/src/web/components/Settings.tsx +++ b/src/web/components/Settings.tsx @@ -57,7 +57,7 @@ export function Settings({ onClose }: { onClose: () => void }) { Print calibration page -

Print the calibration page on plain paper, hold it over a real sheet up to a light, and enter how far the lines are off, up to 10 mm.

+

Print the calibration page on plain paper, hold it over a real sheet up to a light, and enter how far the lines are off, up to 10 mm. Always print at 100% scale ("Actual size"), not "Fit to page"; the calibration page has a 6 inch line to check it.

); diff --git a/src/web/views/CardsToPrint.tsx b/src/web/views/CardsToPrint.tsx index cd4d368..5fef0c0 100644 --- a/src/web/views/CardsToPrint.tsx +++ b/src/web/views/CardsToPrint.tsx @@ -117,7 +117,7 @@ function PrintPanel({ ready, made, setMade, pdfUrl, forget, onClose }: { ))} - +

{ready} {ready === 1 ? 'card' : 'cards'} on {sheets} {sheets === 1 ? 'sheet' : 'sheets'}