diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index a4865656..a6caf17b 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -54,6 +54,55 @@ function wrapLines(text: string, font: PDFFont, size: number, max: number): stri return lines } +const SVG_NS = 'http://www.w3.org/2000/svg' +const HALO_FILTER_ID = 'osteo-halo' + +/** + * Adds a Gaussian-blur "halo" filter to the SVG defs the first time it + * is called on a root. The filter widens the visible footprint of the + * highlighted zones on the whole-animal view, where individual point + * markers are otherwise tiny relative to the full body silhouette. + */ +function ensureHaloFilter(root: SVGSVGElement): void { + const doc = root.ownerDocument + if (!doc) return + if (doc.getElementById(HALO_FILTER_ID)) return + + let defs = root.querySelector('defs') + if (!defs) { + defs = doc.createElementNS(SVG_NS, 'defs') + root.insertBefore(defs, root.firstChild) + } + + const filter = doc.createElementNS(SVG_NS, 'filter') + filter.setAttribute('id', HALO_FILTER_ID) + filter.setAttribute('x', '-100%') + filter.setAttribute('y', '-100%') + filter.setAttribute('width', '300%') + filter.setAttribute('height', '300%') + filter.setAttribute('filterUnits', 'userSpaceOnUse') + + const blur = doc.createElementNS(SVG_NS, 'feGaussianBlur') + blur.setAttribute('in', 'SourceGraphic') + blur.setAttribute('stdDeviation', '6') + blur.setAttribute('result', 'halo') + filter.appendChild(blur) + + const merge = doc.createElementNS(SVG_NS, 'feMerge') + const m1 = doc.createElementNS(SVG_NS, 'feMergeNode') + m1.setAttribute('in', 'halo') + const m2 = doc.createElementNS(SVG_NS, 'feMergeNode') + m2.setAttribute('in', 'halo') + const m3 = doc.createElementNS(SVG_NS, 'feMergeNode') + m3.setAttribute('in', 'SourceGraphic') + merge.appendChild(m1) + merge.appendChild(m2) + merge.appendChild(m3) + filter.appendChild(merge) + + defs.appendChild(filter) +} + async function colorizedSvgString( speciesSvgPath: string, observation: Observation, @@ -67,12 +116,21 @@ async function colorizedSvgString( .map((z) => z.boneId), ) + // Whole-animal view: zone hitboxes are tiny markers on a big + // silhouette, so we apply a blurred halo to make selections pop. + // Detail views are already zoomed in and don't need it. + const applyHalo = sourceDetailId === undefined + if (applyHalo) ensureHaloFilter(root) + 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') + el.setAttribute('fill-opacity', '0.85') + if (applyHalo) { + el.setAttribute('filter', `url(#${HALO_FILTER_ID})`) + } } else { el.setAttribute('display', 'none') } @@ -250,9 +308,16 @@ export async function addObservationPage( let cursorY = titleY - IMAGE_TOP_GAP if (rendered.length > 0) { - const cols = rendered.length === 1 ? 1 : 2 + const isSingle = rendered.length === 1 + const cols = isSingle ? 1 : 2 const rows = Math.ceil(rendered.length / cols) - const cellW = (width - MARGIN_X * 2 - IMAGE_COL_GAP * (cols - 1)) / cols + const fullCellW = + (width - MARGIN_X * 2 - IMAGE_COL_GAP * (cols - 1)) / cols + // Single-image observations get rendered at half the available width + // and centered, so they don't dominate the page when the surrounding + // text is short. + const cellW = isSingle ? fullCellW / 2 : fullCellW + const singleOffsetX = isSingle ? (fullCellW - cellW) / 2 : 0 const reservedBottom = MARGIN_BOTTOM + estimateTextHeight() + IMAGE_BOTTOM_GAP const availH = Math.max(IMAGE_MIN_CELL_H, cursorY - reservedBottom) const rowBudget = Math.max( @@ -267,7 +332,7 @@ export async function addObservationPage( ) const rowH = Math.max(0, ...cellHs) rowItems.forEach((it, c) => { - const x = MARGIN_X + c * (cellW + IMAGE_COL_GAP) + const x = MARGIN_X + singleOffsetX + c * (cellW + IMAGE_COL_GAP) page.drawText(it.label, { x, y: cursorY, @@ -278,7 +343,7 @@ export async function addObservationPage( }) cursorY -= IMAGE_LABEL_GAP rowItems.forEach((it, c) => { - const x = MARGIN_X + c * (cellW + IMAGE_COL_GAP) + const x = MARGIN_X + singleOffsetX + c * (cellW + IMAGE_COL_GAP) page.drawImage(it.img, { x, y: cursorY - cellHs[c],