Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(check): validate legend text with the renderers' shared textUnits model

MincongZhou92cfca654371
diff --git a/archify.zip b/archify.zip
index b4900dce..8b8d4915 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/scripts/check-render-output.mjs b/archify/scripts/check-render-output.mjs
index 57dc8f2e..b7c9b8ea 100644
--- a/archify/scripts/check-render-output.mjs
+++ b/archify/scripts/check-render-output.mjs
@@ -2,6 +2,7 @@
import fs from 'node:fs';
import { analyzeSvgPath } from '../renderers/shared/svg-path-analysis.mjs';
+import { textUnits } from '../renderers/shared/utils.mjs';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { collectAmbiguousCorridors, collectArrowheadCollisions, collectBorderRuns, collectLabelCanvasOverflow, collectLabelRouteClearance, collectRouteRhythmIssues, describeLabelCanvasOverflow, formatRect, forwardCollinearAnalysisSegments, minimumLabelRouteClearance, normalizeRoutePoints, routeBudgetMetrics } from '../renderers/shared/geometry.mjs';
@@ -1056,7 +1057,7 @@ function collectLegendBoxes(fragment) {
for (const match of fragment.matchAll(/<text\b([^>]*)>([\s\S]*?)<\/text>/gi)) {
const attrs = parseAttrs(match[1]);
- const box = textBox(attrs, stripTags(match[2]).trim());
+ const box = textBox(attrs, stripTags(match[2]).trim(), legendTextWidth);
if (box) boxes.push(box);
}
@@ -1077,12 +1078,20 @@ function collectLegendCollisions(arrows, boxes) {
return collisions;
}
-function textBox(attrs, text) {
+// The footprint a legend entry reserves is the renderer's own: textUnits() at
+// TEXT_ADVANCE_EM (renderers/shared/legend.mjs). The default estimate below stays
+// as it is for the readability projection, which is deliberately conservative.
+function legendTextWidth(text, fontSize) {
+ const TEXT_ADVANCE_EM = 0.62;
+ return Math.max(fontSize, textUnits(text) * fontSize * TEXT_ADVANCE_EM);
+}
+
+function textBox(attrs, text, measure = estimatedTextWidth) {
const x = numberAttr(attrs, 'x');
const y = numberAttr(attrs, 'y');
const fontSize = Number.parseFloat(attrs['font-size'] || '10');
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(fontSize)) return null;
- const width = estimatedTextWidth(text, fontSize);
+ const width = measure(text, fontSize);
const anchor = attrs['text-anchor'] || 'start';
let x1 = x;
if (anchor === 'middle') x1 = x - width / 2;
diff --git a/test/render-output-checks.test.mjs b/test/render-output-checks.test.mjs
index 17308df4..c4c82fdc 100644
--- a/test/render-output-checks.test.mjs
+++ b/test/render-output-checks.test.mjs
@@ -314,6 +314,23 @@ test('render output check: rejects a diagonal segment inside a polyline', () =>
assert.match(check.details[0], /segment 1/);
});
+test('render output check: measures a non-Latin legend label with the shared text model', () => {
+ // The renderer's footprint for "\u03a3\u03cd\u03c3\u03c4\u03b7\u03bc\u03b1" at font-size 10 ends near x=103
+ // (measured with textUnits), so a route that begins at x=160 is clear of it. A
+ // Latin-1-only heuristic bills those seven characters at 1.8 units each and
+ // stretches the box to x=186, reporting a crossing that does not exist.
+ const { code, result } = checkHtml('legend-non-latin-clear', `
+ <path d="M 200 140 L 160 140" class="a-default" stroke-width="1.4" marker-end="url(#arrowhead)"/>
+ <!-- Legend -->
+ <text x="40" y="120" class="t-primary" font-size="10">Legend</text>
+ <rect x="40" y="132" width="14" height="9" class="c-backend"/>
+ <text x="60" y="140" class="t-muted" font-size="10">\u03a3\u03cd\u03c3\u03c4\u03b7\u03bc\u03b1</text>
+ `);
+ const clearance = result.checks.find((entry) => entry.name === 'legend_clearance');
+ assert.equal(code, 0, JSON.stringify(clearance, null, 2));
+ assert.equal(result.ok, true);
+});
+
test('render output check: rejects arrows crossing legend text', () => {
const { code, result } = checkHtml('legend-crossing', `
<path d="M 20 112 L 180 112" class="a-dashed" stroke-width="1.4" marker-end="url(#arrowhead-dashed)"/>