hotfix(pdf): swap vet-referral marker ⚠ → (!) for WinAnsi compatibility

Root cause: src/pdf/fonts.ts embeds StandardFonts.Helvetica /
HelveticaBold / TimesRoman, which use WinAnsi encoding (Latin-1
only). PDF export threw `WinAnsi cannot encode "⚠" (0x26a0)` the
moment the text-height estimator measured a vet-flagged leaf
label (countSegmentedLines → widthOfTextAtSize).

Fix: replace the PDF prefix "⚠ " with WinAnsi-safe "(!) ". The
picker DOM keeps the original "⚠" since browsers render U+26A0
natively — the limitation is purely on the PDF side.

Regression test exercises embedFonts + widthOfTextAtSize on a
realistic prefixed label; would catch any future non-WinAnsi
marker re-introduction.

Follow-up (not in scope): switch the PDF body to embedded TTF
(Inter / Fraunces per CLAUDE.md spec) so the catalog can carry
arbitrary glyphs. CLAUDE.md states TTFs should be used; current
StandardFonts is a drift from intent.

155/155 tests green, typecheck clean.
This commit is contained in:
Bastien Chanot committed 2026-05-21 03:33:59 +02:00
1 parent f5307ce61c
commit 143fdc3208
2 files changed
+17 -1

No files matched your search

+16
View File
@@ -2,6 +2,7 @@ import { describe, it, expect, vi } from 'vitest'
import { AFRelationship, PDFDocument } from 'pdf-lib'
import { exportReportToPdf } from '@/pdf/export'
import { importReportFromPdf, ImportError } from '@/pdf/import'
import { embedFonts } from '@/pdf/fonts'
import type { Report } from '@/domain/schema/report'
vi.mock('@/pdf/render/svgToPng', () => ({ rasterizeSvg: async () => new Uint8Array([137, 80, 78, 71]) }))
@@ -61,6 +62,21 @@ describe('PDF roundtrip', () => {
expect(frImported.version).toBe(enImported.version)
})
it('vet-referral marker is encodable by the body font (regression: WinAnsi cannot encode U+26A0)', async () => {
// observationPage prefixes vet-referral leaves with a marker that
// is rendered using the WinAnsi-encoded StandardFonts.Helvetica.
// The previous "⚠ " marker (U+26A0) threw `WinAnsi cannot encode "⚠"`
// the moment the text-height estimator measured the leaf label.
// Guard against re-introducing a non-WinAnsi marker by exercising
// the same path: load the body font and measure a label that
// carries the prefix.
const doc = await PDFDocument.create()
const fonts = await embedFonts(doc)
expect(() =>
fonts.bodyRegular.widthOfTextAtSize('(!) Difficulté de déglutition', 10),
).not.toThrow()
})
it('rejects a legacy v1 PDF with a version-specific message', async () => {
const doc = await PDFDocument.create()
doc.addPage()