From 143fdc32086d731490e3b4884aa21796935f8e12 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Thu, 21 May 2026 03:33:59 +0200 Subject: [PATCH] =?UTF-8?q?hotfix(pdf):=20swap=20vet-referral=20marker=20?= =?UTF-8?q?=E2=9A=A0=20=E2=86=92=20(!)=20for=20WinAnsi=20compatibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/pdf/render/observationPage.ts | 2 +- tests/unit/pdf.roundtrip.test.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index f579a206..d3b8ef58 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -404,7 +404,7 @@ export async function addObservationPage( const value = group .map((f) => { const lbl = locale === 'en' ? f.labelEnSnapshot : f.labelFrSnapshot - return f.requiresVetReferral ? `⚠ ${lbl}` : lbl + return f.requiresVetReferral ? `(!) ${lbl}` : lbl }) .join(' · ') sections.push({ diff --git a/tests/unit/pdf.roundtrip.test.ts b/tests/unit/pdf.roundtrip.test.ts index 3af90664..54341ddc 100644 --- a/tests/unit/pdf.roundtrip.test.ts +++ b/tests/unit/pdf.roundtrip.test.ts @@ -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()