Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(agent): fall back to the estimate when a font's advances cannot be read

Etienne Lescot8c5622388aca
diff --git a/src/lib/ai-edition/annotations/fontTables.test.ts b/src/lib/ai-edition/annotations/fontTables.test.ts
index 892a4450a..ff1305511 100644
--- a/src/lib/ai-edition/annotations/fontTables.test.ts
+++ b/src/lib/ai-edition/annotations/fontTables.test.ts
@@ -91,5 +91,14 @@ describe("measureWithFontTables", () => {
const none: (f: string, w: string) => FontMetrics | null = () => null;
const fallback = measureWithFontTables(none)("Demo OpenScreen", style());
expect(fallback.width).toBeCloseTo(15 * 0.56 * 64, 6);
+ // A font whose advances cannot be read (a truncated `hmtx`) is measured the same way.
+ const truncated: FontMetrics = {
+ unitsPerEm: 1000,
+ lineHeight: 1200,
+ advanceOf: () => {
+ throw new RangeError("Offset is outside the bounds of the DataView");
+ },
+ };
+ expect(measureWithFontTables(() => truncated)("Demo OpenScreen", style())).toEqual(fallback);
});
});
diff --git a/src/lib/ai-edition/annotations/fontTables.ts b/src/lib/ai-edition/annotations/fontTables.ts
index 1f7aa0d33..bda39a5ad 100644
--- a/src/lib/ai-edition/annotations/fontTables.ts
+++ b/src/lib/ai-edition/annotations/fontTables.ts
@@ -154,9 +154,15 @@ export function measureWithFontTables(
return units * scale;
};
const lines = content.split("\n");
- return {
- width: Math.max(0, ...lines.map(lineWidth)),
- height: lines.length * font.lineHeight * scale,
- };
+ try {
+ return {
+ width: Math.max(0, ...lines.map(lineWidth)),
+ height: lines.length * font.lineHeight * scale,
+ };
+ } catch {
+ // Une table tronquée que `readFontMetrics` n'a pas lue (`hmtx` n'est lue qu'à la mesure) :
+ // l'estimation, plutôt qu'une erreur qui ferait échouer l'outil de l'agent.
+ return measureTextBlock(content, style);
+ }
};
}