Skip to content

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

fix(legend): reserve the rendered label width

tt-a1i6f2a051bcbaa
diff --git a/archify/renderers/shared/legend.mjs b/archify/renderers/shared/legend.mjs
index aeeb4426..13bf15e9 100644
--- a/archify/renderers/shared/legend.mjs
+++ b/archify/renderers/shared/legend.mjs
@@ -52,12 +52,18 @@ export function resolveLegend(config, catalog, presentKinds) {
});
}
+// Labels render slightly larger than the layout font size; measure the
+// rendered size so a long label cannot run into the next entry.
+function renderedLegendFontSize(fontSize) {
+ return fontSize < 8 ? fontSize + 0.5 : fontSize + 2;
+}
+
function measuredEntryWidth(entry, fontSize, swatchGap) {
const swatchWidth = entry.swatchWidth ?? 14;
return Math.ceil(
swatchWidth
+ swatchGap
- + textUnits(entry.label) * fontSize * TEXT_ADVANCE_EM
+ + textUnits(entry.label) * renderedLegendFontSize(fontSize) * TEXT_ADVANCE_EM
+ (entry.interactive ? INTERACTIVE_BADGE_ALLOWANCE : 0)
+ (entry.trailingWidth ?? 0),
);
@@ -200,7 +206,7 @@ export function renderLegend({ entries, layout, renderSwatch, locale, labelClass
const measured = measureLegend(entries, layout);
if (!measured) return '';
const hasInteractiveEntries = measured.entries.some((entry) => entry.interactive);
- const renderedFontSize = measured.fontSize < 8 ? measured.fontSize + 0.5 : measured.fontSize + 2;
+ const renderedFontSize = renderedLegendFontSize(measured.fontSize);
const rootAttributes = hasInteractiveEntries ? ' data-legend="" data-legend-bridge=""' : ' data-legend=""';
const parts = [
` <g${rootAttributes}>`,
diff --git a/test/legend-contract.test.mjs b/test/legend-contract.test.mjs
index 98b90f10..c978c6bf 100644
--- a/test/legend-contract.test.mjs
+++ b/test/legend-contract.test.mjs
@@ -410,3 +410,17 @@ test('measured legend rows share baselines and stay within the viewBox for local
});
process.on('exit', () => fs.rmSync(tmp, { recursive: true, force: true }));
+
+test('legend entries reserve the width of their rendered label font', async () => {
+ const { renderLegend } = await import('../archify/renderers/shared/legend.mjs');
+ const label = 'post-start-input-writer';
+ const svg = renderLegend({
+ entries: [{ kind: 'a', label }, { kind: 'b', label: 'next' }],
+ layout: { x: 0, baselineY: 100, width: 2000, fontSize: 8 },
+ renderSwatch: () => '<rect/>',
+ });
+ const [first, second] = [...svg.matchAll(/data-legend-x="([\d.]+)"/g)].map((match) => Number(match[1]));
+ const fontSize = Number(svg.match(/<text x="[\d.]+" y="[\d.]+" class="t-muted" font-size="([\d.]+)"/)[1]);
+ // Monospace label text advances 0.6em per ASCII character after the swatch.
+ assert.ok(second - first >= 14 + 8 + label.length * fontSize * 0.6, JSON.stringify({ first, second, fontSize }));
+});
diff --git a/test/workflow-compiler.test.mjs b/test/workflow-compiler.test.mjs
index bef0cc07..0230c274 100644
--- a/test/workflow-compiler.test.mjs
+++ b/test/workflow-compiler.test.mjs
@@ -357,7 +357,7 @@ test('readable-v2 separates approval branches in the checked-in workflow example
// approval-denied and approved-tool previously shared a mixed-style trunk.
// Node geometry, labels, all other paths and v1 baselines are unchanged.
sha256(result.svg.replace(/ data-(?:composition-routing|edge-role|layout-contract)="[^"]*"/g, '')),
- '7a4effe6000ef4d5c8e2e933186ff225f6398d76216d916600e51f8e05a7e139',
+ 'ee6a341d43479ac4b79deaef11513b4fac00cd984a376bf93b7ccd3a13b7e9a9',
);
});