From 2cfc49df048f80f312cfdd1086de820a70622760 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Tue, 19 May 2026 06:42:47 +0200 Subject: [PATCH] fix(pdf): pack observation SVGs into 2-column grid, reserve text space Multi-SVG observations previously stacked vertically and pushed the zones/pathologies/severity/comment sections off the page. Now SVGs are laid out 2 per row with cell width = (page - 2*margin - col-gap)/2. Each cell scales to its own aspect, capped by a per-row height budget that subtracts the reserved bottom (text sections + bottom margin) from the available image area. Single- SVG observations keep a full-width row. Safety: the text-area reservation guarantees the sections always fit; the cell-height cap prevents runaway when many details are selected. A floor (IMAGE_MIN_CELL_H) avoids zero-height cells in pathological cases. Co-Authored-By: Claude --- src/pdf/render/observationPage.ts | 123 ++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 42 deletions(-) diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index b25cf4d9..5894161f 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -9,6 +9,7 @@ import { parseSvgBones } from '@/anatomy/svgConvention' import { getSpecies } from '@/domain/species/registry' const MARGIN_X = 36 +const MARGIN_BOTTOM = 48 const EYEBROW_Y_OFFSET = 95 const TITLE_Y_OFFSET = 122 const TITLE_RULE_OFFSET = 12 @@ -16,6 +17,9 @@ const TITLE_RULE_WIDTH = 48 const IMAGE_TOP_GAP = 32 const IMAGE_LABEL_GAP = 14 const IMAGE_BOTTOM_GAP = 14 +const IMAGE_COL_GAP = 16 +const IMAGE_ROW_GAP = 12 +const IMAGE_MIN_CELL_H = 60 const TEXT_TOP_GAP = 10 const TEXT_LINE_HEIGHT = 13 const SECTION_LABEL_GAP = 4 @@ -150,55 +154,33 @@ export async function addObservationPage( if (!species) return const hasMainZones = observation.zones.some((z) => !z.detailSourceId) - let cursorY = titleY - IMAGE_TOP_GAP - - if (hasMainZones) { - page.drawText('VUE GÉNÉRALE', { - x: MARGIN_X, - y: cursorY, - size: 8, - font: fonts.bodyBold, - color: COLOR_ACCENT, - }) - cursorY -= IMAGE_LABEL_GAP - const mainSvgStr = await colorizedSvgString(species.mainSvg, observation, undefined) - const mainPng = await rasterizeSvg(mainSvgStr, 2) - const mainImg = await pdfDoc.embedPng(mainPng) - const imgW = width - MARGIN_X * 2 - const imgH = imgW * (mainImg.height / mainImg.width) - page.drawImage(mainImg, { - x: MARGIN_X, - y: cursorY - imgH, - width: imgW, - height: imgH, - }) - cursorY -= imgH + IMAGE_BOTTOM_GAP - } - const detailIds = Array.from( new Set(observation.zones.map((z) => z.detailSourceId).filter(Boolean) as string[]), ) + + const items: Array<{ label: string; src: string; sourceId: string | undefined }> = [] + if (hasMainZones) { + items.push({ label: 'VUE GÉNÉRALE', src: species.mainSvg, sourceId: undefined }) + } for (const detailId of detailIds) { const detailPath = species.details[detailId] if (!detailPath) continue - page.drawText(`VUE DÉTAIL — ${detailId.toUpperCase()}`, { - x: MARGIN_X, - y: cursorY, - size: 8, - font: fonts.bodyBold, - color: COLOR_ACCENT, + items.push({ + label: `VUE DÉTAIL — ${detailId.toUpperCase()}`, + src: detailPath, + sourceId: detailId, }) - cursorY -= IMAGE_LABEL_GAP - const detailSvg = await colorizedSvgString(detailPath, observation, detailId) - const detailPng = await rasterizeSvg(detailSvg, 2) - const dImg = await pdfDoc.embedPng(detailPng) - const dW = (width - MARGIN_X * 2) / 2 - const dH = dW * (dImg.height / dImg.width) - page.drawImage(dImg, { x: MARGIN_X, y: cursorY - dH, width: dW, height: dH }) - cursorY -= dH + IMAGE_BOTTOM_GAP } - cursorY -= TEXT_TOP_GAP + const rendered = await Promise.all( + items.map(async (it) => { + const svgStr = await colorizedSvgString(it.src, observation, it.sourceId) + const png = await rasterizeSvg(svgStr, 2) + const img = await pdfDoc.embedPng(png) + const aspect = img.width > 0 ? img.height / img.width : 0.6 + return { label: it.label, img, aspect } + }), + ) const zonesText = observation.zones .map((z) => { @@ -227,7 +209,64 @@ export async function addObservationPage( sections.push({ label: 'COMMENTAIRE', value: observation.comment.trim() }) } - const max = width - MARGIN_X * 2 + const maxTextW = width - MARGIN_X * 2 + + 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 + } + return h + } + + let cursorY = titleY - IMAGE_TOP_GAP + + if (rendered.length > 0) { + const cols = rendered.length === 1 ? 1 : 2 + const rows = Math.ceil(rendered.length / cols) + const cellW = (width - MARGIN_X * 2 - IMAGE_COL_GAP * (cols - 1)) / cols + const reservedBottom = MARGIN_BOTTOM + estimateTextHeight() + IMAGE_BOTTOM_GAP + const availH = Math.max(IMAGE_MIN_CELL_H, cursorY - reservedBottom) + const rowBudget = Math.max( + IMAGE_MIN_CELL_H, + (availH - rows * IMAGE_LABEL_GAP - (rows - 1) * IMAGE_ROW_GAP) / rows, + ) + + for (let r = 0; r < rows; r += 1) { + const rowItems = rendered.slice(r * cols, r * cols + cols) + const cellHs = rowItems.map((it) => + Math.min(cellW * it.aspect, rowBudget), + ) + const rowH = Math.max(0, ...cellHs) + rowItems.forEach((it, c) => { + const x = MARGIN_X + c * (cellW + IMAGE_COL_GAP) + page.drawText(it.label, { + x, + y: cursorY, + size: 8, + font: fonts.bodyBold, + color: COLOR_ACCENT, + }) + }) + cursorY -= IMAGE_LABEL_GAP + rowItems.forEach((it, c) => { + const x = MARGIN_X + c * (cellW + IMAGE_COL_GAP) + page.drawImage(it.img, { + x, + y: cursorY - cellHs[c], + width: cellW, + height: cellHs[c], + }) + }) + cursorY -= rowH + if (r < rows - 1) cursorY -= IMAGE_ROW_GAP + } + cursorY -= IMAGE_BOTTOM_GAP + } + + cursorY -= TEXT_TOP_GAP + for (const { label, value } of sections) { page.drawText(label, { x: MARGIN_X, @@ -237,7 +276,7 @@ export async function addObservationPage( color: COLOR_ACCENT, }) cursorY -= SECTION_LABEL_GAP + 9 - const lines = wrapLines(value, fonts.bodyRegular, 10, max) + const lines = wrapLines(value, fonts.bodyRegular, 10, maxTextW) for (const line of lines) { page.drawText(line, { x: MARGIN_X,