fix(pdf): clear header, wrap zone line, hide non-selected zones, skip main SVG when only details
Four coupled PDF rendering bugs: 1. Title "Observation #N" and "Conclusion" overlapped the header rule (y = height - 80; rule at y = 760; size-16 ascender ate the line). Lowered both titles to y = height - 90. 2. Long zones lines visually wrapped via maxWidth but the cursor only advanced one slot per array entry, so pathologies/sévérité/commentaire collided with the wrapped tail. Added wrapLines + per-line advance. 3. The rasterized anatomy still showed unselected zones (purple spine fills, orange head trigger) because colorizedSvgString only mutated selected elements. Non-selected annotated elements are now hidden via display="none" and every data-anno-* attribute is stripped from the serialized output (data-anno are runtime-only hints). 4. Main SVG was drawn even when every zone lived in a detail popup. Render it only when at least one zone has no detailSourceId. Layout constants extracted at the top of observationPage.ts. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
57c9925578
commit
bc9a51784a
2 files changed
+94
-38
No files matched your search
@@ -36,7 +36,7 @@ export async function addConclusionPage(
|
||||
const muted = rgb(0.42, 0.39, 0.35)
|
||||
const max = width - MARGIN_X * 2
|
||||
|
||||
let y = height - 80
|
||||
let y = height - 90
|
||||
page.drawText('Conclusion', { x: MARGIN_X, y, size: 16, font: fonts.headingRegular, color: ink })
|
||||
y -= 20
|
||||
wrap(report.conclusion || '—', fonts.bodyRegular, 11, max).forEach((line) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { rgb, PageSizes, type PDFDocument } from 'pdf-lib'
|
||||
import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { Observation } from '@/domain/schema/observation'
|
||||
@@ -9,7 +9,39 @@ import { parseSvgBones } from '@/anatomy/svgConvention'
|
||||
import { getSpecies } from '@/domain/species/registry'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const TITLE_Y_OFFSET = 90
|
||||
const IMAGE_TOP_GAP = 14
|
||||
const IMAGE_BOTTOM_GAP = 10
|
||||
const TEXT_TOP_GAP = 8
|
||||
const TEXT_LINE_HEIGHT = 13
|
||||
const TEXT_BLOCK_GAP = 6
|
||||
|
||||
function wrapLines(text: string, font: PDFFont, size: number, max: number): string[] {
|
||||
const words = text.split(/\s+/)
|
||||
const lines: string[] = []
|
||||
let line = ''
|
||||
for (const w of words) {
|
||||
const test = line ? `${line} ${w}` : w
|
||||
if (font.widthOfTextAtSize(test, size) > max && line) {
|
||||
lines.push(line)
|
||||
line = w
|
||||
} else {
|
||||
line = test
|
||||
}
|
||||
}
|
||||
if (line) lines.push(line)
|
||||
return lines
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a colorized + cleaned SVG string for rasterization:
|
||||
* - selected zones (matching `sourceDetailId`) are filled with the accent
|
||||
* - all other annotated elements are hidden via `display="none"`
|
||||
* - every `data-anno-*` attribute is stripped from the output
|
||||
*
|
||||
* Result: the rendered PNG shows only highlighted zones over the bare
|
||||
* anatomy artwork, with no leftover overlay tints.
|
||||
*/
|
||||
async function colorizedSvgString(
|
||||
speciesSvgPath: string,
|
||||
observation: Observation,
|
||||
@@ -17,16 +49,30 @@ async function colorizedSvgString(
|
||||
): Promise<string> {
|
||||
const root = await loadSvg(speciesSvgPath)
|
||||
const bones = parseSvgBones(root)
|
||||
observation.zones
|
||||
.filter((z) => z.detailSourceId === sourceDetailId)
|
||||
.forEach((z) => {
|
||||
const bone = bones.get(z.boneId)
|
||||
if (!bone) return
|
||||
bone.els.forEach((el) => {
|
||||
const selectedIds = new Set(
|
||||
observation.zones
|
||||
.filter((z) => z.detailSourceId === sourceDetailId)
|
||||
.map((z) => z.boneId),
|
||||
)
|
||||
|
||||
bones.forEach((bone, id) => {
|
||||
const selected = selectedIds.has(id)
|
||||
bone.els.forEach((el) => {
|
||||
if (selected) {
|
||||
el.setAttribute('fill', '#b85a3f')
|
||||
el.setAttribute('fill-opacity', '0.65')
|
||||
})
|
||||
} else {
|
||||
el.setAttribute('display', 'none')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
root.querySelectorAll('[data-anno-id]').forEach((el) => {
|
||||
Array.from(el.attributes)
|
||||
.filter((a) => a.name.startsWith('data-anno-'))
|
||||
.forEach((a) => el.removeAttribute(a.name))
|
||||
})
|
||||
|
||||
return new XMLSerializer().serializeToString(root)
|
||||
}
|
||||
|
||||
@@ -43,9 +89,10 @@ export async function addObservationPage(
|
||||
drawHeader(page, report, fonts)
|
||||
|
||||
const ink = rgb(0.12, 0.11, 0.09)
|
||||
const titleY = height - TITLE_Y_OFFSET
|
||||
page.drawText(`Observation #${obsIndex + 1}`, {
|
||||
x: MARGIN_X,
|
||||
y: height - 80,
|
||||
y: titleY,
|
||||
size: 16,
|
||||
font: fonts.headingRegular,
|
||||
color: ink,
|
||||
@@ -54,19 +101,23 @@ export async function addObservationPage(
|
||||
const species = getSpecies(report.animal.speciesId)
|
||||
if (!species) return
|
||||
|
||||
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: height - 100 - imgH,
|
||||
width: imgW,
|
||||
height: imgH,
|
||||
})
|
||||
const hasMainZones = observation.zones.some((z) => !z.detailSourceId)
|
||||
let cursorY = titleY - IMAGE_TOP_GAP
|
||||
|
||||
let cursorY = height - 110 - imgH
|
||||
if (hasMainZones) {
|
||||
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[]),
|
||||
@@ -80,10 +131,10 @@ export async function addObservationPage(
|
||||
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 + 10
|
||||
cursorY -= dH + IMAGE_BOTTOM_GAP
|
||||
}
|
||||
|
||||
cursorY -= 8
|
||||
cursorY -= TEXT_TOP_GAP
|
||||
|
||||
const zonesText = observation.zones
|
||||
.map((z) => {
|
||||
@@ -95,23 +146,28 @@ export async function addObservationPage(
|
||||
})
|
||||
.join(' · ')
|
||||
|
||||
const lines: string[] = [`Zones : ${zonesText}`]
|
||||
const blocks: string[] = [`Zones : ${zonesText}`]
|
||||
if (observation.pathologyIds.length || observation.freeTextPathologies.length) {
|
||||
lines.push(
|
||||
blocks.push(
|
||||
`Pathologies : ${[...observation.pathologyIds, ...observation.freeTextPathologies].join(' · ')}`,
|
||||
)
|
||||
}
|
||||
if (observation.severity) lines.push(`Sévérité : ${observation.severity}`)
|
||||
if (observation.comment.trim()) lines.push(`Commentaire : ${observation.comment.trim()}`)
|
||||
if (observation.severity) blocks.push(`Sévérité : ${observation.severity}`)
|
||||
if (observation.comment.trim()) blocks.push(`Commentaire : ${observation.comment.trim()}`)
|
||||
|
||||
lines.forEach((line, i) => {
|
||||
page.drawText(line, {
|
||||
x: MARGIN_X,
|
||||
y: cursorY - i * 14,
|
||||
size: 10,
|
||||
font: fonts.bodyRegular,
|
||||
color: ink,
|
||||
maxWidth: width - MARGIN_X * 2,
|
||||
})
|
||||
})
|
||||
const max = width - MARGIN_X * 2
|
||||
for (const block of blocks) {
|
||||
const lines = wrapLines(block, fonts.bodyRegular, 10, max)
|
||||
for (const line of lines) {
|
||||
page.drawText(line, {
|
||||
x: MARGIN_X,
|
||||
y: cursorY,
|
||||
size: 10,
|
||||
font: fonts.bodyRegular,
|
||||
color: ink,
|
||||
})
|
||||
cursorY -= TEXT_LINE_HEIGHT
|
||||
}
|
||||
cursorY -= TEXT_BLOCK_GAP
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user