This document describes the fix for subgraph title truncation in Mermaid flowcharts. Prior to this fix, subgraph titles would be truncated when they exceeded the content width.
Subgraph titles were being cut off when rendered. For example:
'Outer Group'displayed as'Outer Grou...''Inner Group'showed similar truncation
The root cause was that the subgraph bounding box calculation only considered the content (nodes and child subgraphs) when determining width, without accounting for the title text width.
Modified compute_subgraph_layouts() in src/markdown/mermaid/flowchart.rs to:
- Measure title text width using the
TextMeasurertrait - Expand subgraph width if the title requires more space than the content provides
- Apply consistent padding (12px left + 12px right = 24px total) around the title
The function signature was updated to accept text measurement capabilities:
fn compute_subgraph_layouts(
layout: &mut FlowchartLayout,
flowchart: &Flowchart,
config: &FlowLayoutConfig,
font_size: f32, // NEW
text_measurer: &impl TextMeasurer, // NEW
)Title width check is applied in two places:
- Pre-computed layouts: After updating the title from the flowchart
- Fallback path: After computing bounds from node positions
// Ensure subgraph width accommodates the title text
if let Some(title) = &subgraph.title {
let title_text_size = text_measurer.measure(title, font_size);
let min_width_for_title = title_text_size.width + 24.0; // 12px padding each side
if existing.size.x < min_width_for_title {
existing.size.x = min_width_for_title;
}
}Two new tests verify the fix:
test_subgraph_title_width_expansion: Verifies that long titles expand the subgraph widthtest_subgraph_short_title_no_extra_expansion: Verifies that short titles don't unnecessarily expand subgraphs
let source = r#"flowchart TD
subgraph veryLongTitle[This Is A Very Long Subgraph Title]
A[Small] --> B[Node]
end"#;The test verifies that sg_layout.size.x >= title_width + 24.0.
| File | Purpose |
|---|---|
src/markdown/mermaid/flowchart.rs |
Layout computation and rendering |
src/markdown/mermaid/text.rs |
Text measurement traits |
- Flowchart Subgraphs - General subgraph parsing and layout
- Subgraph Internal Layout - Internal node positioning
- Mermaid Text Measurement - Text measurement utilities