From 700567fc4f05af70758b8d66c344d064dc26ef47 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Wed, 20 May 2026 05:31:21 +0200 Subject: [PATCH] feat(pdf): render the inter-item separator larger and accent-coloured MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ZONES and CONSTATS sections previously joined their entries with a tiny mid-dot in the same body font. At print scale the separator collapsed into the surrounding text and made dense lists hard to scan. Sections that contain " · " are now drawn segment-by-segment: - Items keep the existing body-regular @10pt ink color. - The separator is rendered as a heavier "•" in body-bold @14pt in the accent color, with 5pt of clear space on either side. - The bullet's baseline is nudged down 30% of the size delta so it visually centers on the surrounding text x-height. Wrapping is handled per-item: an item that won't fit on the current line jumps to the next; the separator never lands at the start of a new line. The height estimator was updated to count the same wrapping the renderer uses, so the image-area / text-area budget stays balanced for both single-line and multi-line section values. Co-Authored-By: Claude --- src/pdf/render/observationPage.ts | 145 +++++++++++++++++++++++++++--- 1 file changed, 132 insertions(+), 13 deletions(-) diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index 934e1297..a3d18262 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -1,4 +1,4 @@ -import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib' +import { rgb, PageSizes, type PDFDocument, type PDFFont, type PDFPage } from 'pdf-lib' import type { EmbeddedFonts } from '@/pdf/fonts' import type { Report } from '@/domain/schema/report' import type { Finding, Observation } from '@/domain/schema/observation' @@ -37,6 +37,107 @@ const SEVERITY_BG: Record> = { 'sévère': rgb(0.78, 0.20, 0.20), } +const SECTION_TEXT_SIZE = 10 +const SEPARATOR_GLYPH = '•' +const SEPARATOR_SIZE = 14 +const SEPARATOR_SIDE_GAP = 5 + +/** Width of the visible separator block including the empty space on each side. */ +function separatorWidth(fonts: EmbeddedFonts): number { + return ( + fonts.bodyBold.widthOfTextAtSize(SEPARATOR_GLYPH, SEPARATOR_SIZE) + + SEPARATOR_SIDE_GAP * 2 + ) +} + +/** Counts the line breaks a list of items would consume when laid out on a maxWidth line. */ +function countSegmentedLines( + items: string[], + fonts: EmbeddedFonts, + maxWidth: number, +): number { + if (items.length === 0) return 1 + const sepW = separatorWidth(fonts) + let lines = 1 + let x = 0 + for (let i = 0; i < items.length; i += 1) { + const itemW = fonts.bodyRegular.widthOfTextAtSize( + items[i], + SECTION_TEXT_SIZE, + ) + if (x + itemW > maxWidth && x > 0) { + lines += 1 + x = 0 + } + x += itemW + if (i < items.length - 1) { + if (x + sepW > maxWidth) { + lines += 1 + x = 0 + } else { + x += sepW + } + } + } + return lines +} + +/** + * Renders a list of items separated by a visibly larger accent-coloured + * bullet. Returns the cursor y *below* the last line drawn so the + * caller can keep advancing the page cursor like a plain wrapLines run. + */ +function drawSegmentedText( + page: PDFPage, + items: string[], + startX: number, + startY: number, + maxWidth: number, + fonts: EmbeddedFonts, +): number { + if (items.length === 0) return startY - TEXT_LINE_HEIGHT + const sepW = separatorWidth(fonts) + // The bullet's larger size pushes its baseline above the surrounding + // text. Shift it down a touch so it visually centers on the x-height + // of the items it separates. + const sepBaselineOffset = (SEPARATOR_SIZE - SECTION_TEXT_SIZE) * 0.3 + + let x = startX + let y = startY + for (let i = 0; i < items.length; i += 1) { + const item = items[i] + const itemW = fonts.bodyRegular.widthOfTextAtSize(item, SECTION_TEXT_SIZE) + if (x + itemW > startX + maxWidth && x > startX) { + x = startX + y -= TEXT_LINE_HEIGHT + } + page.drawText(item, { + x, + y, + size: SECTION_TEXT_SIZE, + font: fonts.bodyRegular, + color: COLOR_INK, + }) + x += itemW + if (i < items.length - 1) { + if (x + sepW > startX + maxWidth) { + x = startX + y -= TEXT_LINE_HEIGHT + } else { + page.drawText(SEPARATOR_GLYPH, { + x: x + SEPARATOR_SIDE_GAP, + y: y - sepBaselineOffset, + size: SEPARATOR_SIZE, + font: fonts.bodyBold, + color: COLOR_ACCENT, + }) + x += sepW + } + } + } + return y - TEXT_LINE_HEIGHT +} + function wrapLines(text: string, font: PDFFont, size: number, max: number): string[] { const words = text.split(/\s+/) const lines: string[] = [] @@ -315,8 +416,10 @@ export async function addObservationPage( function estimateTextHeight(): number { let h = TEXT_TOP_GAP for (const { value } of sections) { - const lines = wrapLines(value, fonts.bodyRegular, 10, maxTextW) - h += SECTION_LABEL_GAP + 9 + lines.length * TEXT_LINE_HEIGHT + SECTION_BLOCK_GAP + const lineCount = value.includes(' · ') + ? countSegmentedLines(value.split(' · '), fonts, maxTextW) + : wrapLines(value, fonts.bodyRegular, SECTION_TEXT_SIZE, maxTextW).length + h += SECTION_LABEL_GAP + 9 + lineCount * TEXT_LINE_HEIGHT + SECTION_BLOCK_GAP } return h } @@ -384,16 +487,32 @@ export async function addObservationPage( color: COLOR_ACCENT, }) cursorY -= SECTION_LABEL_GAP + 9 - const lines = wrapLines(value, fonts.bodyRegular, 10, maxTextW) - for (const line of lines) { - page.drawText(line, { - x: MARGIN_X, - y: cursorY, - size: 10, - font: fonts.bodyRegular, - color: COLOR_INK, - }) - cursorY -= TEXT_LINE_HEIGHT + if (value.includes(' · ')) { + cursorY = drawSegmentedText( + page, + value.split(' · '), + MARGIN_X, + cursorY, + maxTextW, + fonts, + ) + } else { + const lines = wrapLines( + value, + fonts.bodyRegular, + SECTION_TEXT_SIZE, + maxTextW, + ) + for (const line of lines) { + page.drawText(line, { + x: MARGIN_X, + y: cursorY, + size: SECTION_TEXT_SIZE, + font: fonts.bodyRegular, + color: COLOR_INK, + }) + cursorY -= TEXT_LINE_HEIGHT + } } cursorY -= SECTION_BLOCK_GAP }