feat(pdf): addObservationPage with rasterized colorized SVG + summary

This commit is contained in:
Bastien Chanot committed 2026-05-18 21:45:58 +02:00
1 parent 6cbc6ce328
commit 384d2918d4
1 file changed
+114 -12
+114 -12
View File
@@ -1,19 +1,121 @@
import type { PDFDocument } from 'pdf-lib'
import { rgb, PageSizes, type PDFDocument } from 'pdf-lib'
import type { EmbeddedFonts } from '@/pdf/fonts'
import type { Report } from '@/domain/schema/report'
import type { Observation } from '@/domain/schema/observation'
import { drawHeader } from './header'
import { rasterizeSvg } from './svgToPng'
import { loadSvg } from '@/anatomy/svgLoader'
import { parseSvgBones } from '@/anatomy/svgConvention'
import { getSpecies } from '@/domain/species/registry'
/**
* Appends one page per observation to the PDF document.
* Each page includes: rasterized main SVG, optional detail SVG raster,
* zones block, pathologies + severity + comment, abbreviated citations.
*/
const MARGIN_X = 36
async function colorizedSvgString(
speciesSvgPath: string,
observation: Observation,
sourceDetailId: string | undefined,
): 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.visibleEls.forEach((el) => {
el.setAttribute('fill', '#b85a3f')
el.setAttribute('fill-opacity', '0.85')
})
bone.hitboxEls.forEach((el) => {
el.setAttribute('fill', '#b85a3f')
el.setAttribute('fill-opacity', '0.4')
})
})
return new XMLSerializer().serializeToString(root)
}
export async function addObservationPage(
_pdfDoc: PDFDocument,
_report: Report,
_observation: Observation,
_obsIndex: number,
_fonts: EmbeddedFonts,
pdfDoc: PDFDocument,
report: Report,
observation: Observation,
obsIndex: number,
fonts: EmbeddedFonts,
zoneLabels: Map<string, string>,
): Promise<void> {
throw new Error('not implemented')
const page = pdfDoc.addPage(PageSizes.A4)
const { width, height } = page.getSize()
drawHeader(page, report, fonts)
const ink = rgb(0.12, 0.11, 0.09)
page.drawText(`Observation #${obsIndex + 1}`, {
x: MARGIN_X,
y: height - 80,
size: 16,
font: fonts.headingRegular,
color: ink,
})
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,
})
let cursorY = height - 110 - imgH
const detailIds = Array.from(
new Set(observation.zones.map((z) => z.detailSourceId).filter(Boolean) as string[]),
)
for (const detailId of detailIds) {
const detailPath = species.details[detailId]
if (!detailPath) continue
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 + 10
}
cursorY -= 8
const zonesText = observation.zones
.map((z) => {
const label = zoneLabels.get(z.boneId) ?? z.boneId
const v = z.vertebrae?.length
? ` (${z.vertebrae.map((x) => `${x.region}${x.index}`).join(', ')})`
: ''
return `${label}${v}`
})
.join(' · ')
const lines: string[] = [`Zones : ${zonesText}`]
if (observation.pathologyIds.length || observation.freeTextPathologies.length) {
lines.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()}`)
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,
})
})
}