merman-render 0.8.0-alpha.7

Headless layout + SVG renderer for Mermaid (parity-focused; upstream SVG goldens).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Mermaid 12 StateDB projection into the registered ELK renderer.
//!
//! Keep semantic IDs, hierarchy and self loops intact: Dagre's recursive extraction and
//! cyclic-special nodes belong only to its own adapter.

use super::StateDiagramModel;
use super::config::*;
use super::layout::{
    edge_label_metrics, note_group_owner_id, state_fork_join_painted_dimensions,
    state_hidden_prefixes, state_node_dimensions, title_label_metrics,
    validate_state_parent_cycles,
};
use crate::layout_work::OperationLayoutWorkControl;
use crate::model::{
    Bounds, LayoutCluster, LayoutEdge, LayoutLabel, LayoutNode, LayoutPoint, StateDiagramLayout,
};
use crate::text::TextMeasurer;
use crate::{Error, Result};
use merman_layout_elk as elk;
use serde_json::Value;
use std::collections::HashMap;

fn is_self_loop_helper(id: &str) -> bool {
    let Some((prefix, suffix)) = id.rsplit_once("---") else {
        return false;
    };
    if suffix != "1" && suffix != "2" {
        return false;
    }
    let Some((left, right)) = prefix.rsplit_once("---") else {
        return false;
    };
    !left.is_empty() && left == right
}

fn direction(value: &str) -> elk::Direction {
    match normalize_dir(value).as_str() {
        "LR" => elk::Direction::Right,
        "RL" => elk::Direction::Left,
        "BT" => elk::Direction::Up,
        _ => elk::Direction::Down,
    }
}

pub(super) fn layout(
    model: &StateDiagramModel,
    config: &Value,
    measurer: &dyn TextMeasurer,
    operation_seed: elk::ElkOperationSeed,
    work: &mut OperationLayoutWorkControl,
) -> Result<StateDiagramLayout> {
    validate_state_parent_cycles(model)?;
    let hidden = state_hidden_prefixes(model);
    let settings = StateConfigView::new(config).layout_settings(&model.direction);
    // The parser retains each note declaration, while StateDB overwrites the physical
    // note-group metadata. Preserve the final group identity without orphaning earlier notes.
    let mut note_groups = HashMap::new();
    for node in &model.nodes {
        if node.shape == "noteGroup"
            && let Some(owner) = note_group_owner_id(&node.id)
        {
            note_groups.insert(owner, node.id.as_str());
        }
    }
    let canonical_groups: HashMap<_, _> = model
        .nodes
        .iter()
        .filter(|node| node.shape == "noteGroup")
        .filter_map(|node| {
            note_group_owner_id(&node.id)
                .and_then(|owner| note_groups.get(owner).copied())
                .map(|canonical| (node.id.as_str(), canonical))
        })
        .collect();
    let canonical =
        |id: &str| -> String { canonical_groups.get(id).copied().unwrap_or(id).to_owned() };
    let mut nodes = Vec::with_capacity(model.nodes.len());
    let mut source_nodes = HashMap::new();
    let mut painted_group_labels = HashMap::new();
    for node in &model.nodes {
        if hidden.is_hidden(&node.id) || canonical(&node.id) != node.id {
            continue;
        }
        let group = state_node_is_effective_group(node);
        let title = node
            .label
            .as_ref()
            .map(value_to_label_text)
            .unwrap_or_else(|| node.id.clone());
        let (width, height) = if group {
            (0.0, 0.0)
        } else if matches!(node.shape.as_str(), "fork" | "join") {
            // ELK insertMeasuredNode replaces forkJoin's padded dimensions with the
            // painted element bbox. Dagre retains that shape function's padding.
            state_fork_join_painted_dimensions(settings.graph.rankdir)
        } else {
            state_node_dimensions(node, &settings, measurer)?
        };
        let label = if group {
            let (width, height) = if title.is_empty() {
                (0.0, 0.0)
            } else {
                title_label_metrics(&title, measurer, &settings.text_style, settings.wrap_mode)
            };
            painted_group_labels.insert(node.id.as_str(), elk::Label { width, height });
            // Mermaid ELK's getMeasuredLabelData removes the 2px labelBBox adjustment
            // before reserving the title strip; the cluster painter retains the full bbox.
            Some(elk::Label {
                width,
                height: (height - 2.0).max(0.0),
            })
        } else {
            None
        };
        nodes.push(elk::Node {
            id: node.id.clone(),
            kind: if group {
                elk::NodeKind::Group
            } else {
                elk::NodeKind::Leaf
            },
            container: elk::ContainerNodeOptions {
                padding: node.padding.unwrap_or(settings.state_padding).max(0.0),
                ..Default::default()
            },
            label_text: group.then_some(title),
            width,
            height,
            parent: node
                .parent_id
                .as_deref()
                .filter(|id| !hidden.is_hidden(id))
                .map(canonical),
            direction: node.dir.as_deref().filter(|_| group).map(direction),
            hierarchy_handling: group.then_some(if node.dir.is_some() {
                elk::HierarchyHandling::SeparateChildren
            } else {
                elk::HierarchyHandling::IncludeChildren
            }),
            layer_constraint: None,
            port_alignment: None,
            label,
        });
        source_nodes.insert(node.id.as_str(), node);
    }
    let mut source_edges = HashMap::new();
    let mut edges = Vec::with_capacity(model.edges.len());
    for edge in &model.edges {
        if hidden.is_hidden(&edge.id)
            || hidden.is_hidden(&edge.start)
            || hidden.is_hidden(&edge.end)
        {
            continue;
        }
        let label = crate::text::mermaid_html_breaks_to_newlines(&edge.label);
        let (width, height) =
            edge_label_metrics(&label, measurer, &settings.text_style, settings.wrap_mode);
        edges.push(elk::Edge {
            id: edge.id.clone(),
            source: canonical(&edge.start),
            target: canonical(&edge.end),
            label: (!edge.label.trim().is_empty()).then_some(elk::Label { width, height }),
            minlen: 1,
            inside_self_loops_yo: false,
        });
        source_edges.insert(edge.id.as_str(), edge);
    }
    let mut graph = elk::Graph {
        id: "root".to_owned(),
        direction: direction(&model.direction),
        nodes,
        edges,
        spacing: elk::Spacing {
            node_node: settings.graph.nodesep,
            layer_layer: settings.graph.ranksep,
            ..Default::default()
        },
        options: crate::elk_options::layout_options(config),
    };
    crate::elk_hierarchy::apply_to_graph(&mut graph, work)?;
    let placed = elk::layout_with_operation_seed_and_work_control(&graph, operation_seed, work)
        .map_err(|error| work.map_elk_error_with_context(error, "State ELK"))?;
    let graph_nodes: HashMap<_, _> = graph
        .nodes
        .iter()
        .map(|node| (node.id.as_str(), node))
        .collect();
    let graph_edges: HashMap<_, _> = graph
        .edges
        .iter()
        .map(|edge| (edge.id.as_str(), edge))
        .collect();
    let mut output = StateDiagramLayout {
        nodes: Vec::with_capacity(placed.nodes.len()),
        edges: Vec::with_capacity(placed.edges.len()),
        clusters: Vec::new(),
        bounds: None,
        uses_elk_adapter_dom: true,
        elk_edge_paths: HashMap::new(),
    };
    for node in placed.nodes {
        // ELK may materialize helper vertices for native self-loops. Mermaid's public
        // StateDB layout exposes only semantic nodes; retain their route sections in the
        // edge sidecar but never leak helper IDs into compatibility layout JSON.
        if !source_nodes.contains_key(node.id.as_str()) && is_self_loop_helper(&node.id) {
            continue;
        }
        let source = graph_nodes
            .get(node.id.as_str())
            .ok_or_else(|| Error::InvalidModel {
                message: format!("ELK returned unknown state node {}", node.id),
            })?;
        if source.kind == elk::NodeKind::Group {
            let semantic = source_nodes[node.id.as_str()];
            let label = painted_group_labels[node.id.as_str()];
            output.clusters.push(LayoutCluster {
                id: node.id.clone(),
                x: node.x,
                y: node.y,
                width: node.width,
                height: node.height,
                diff: 0.0,
                offset_y: 0.0,
                title: source.label_text.clone().unwrap_or_default(),
                title_label: LayoutLabel {
                    x: node.x,
                    y: node.y - node.height / 2.0 + label.height / 2.0,
                    width: label.width,
                    height: label.height,
                },
                requested_dir: semantic.dir.clone(),
                effective_dir: normalize_dir(semantic.dir.as_deref().unwrap_or(&model.direction)),
                padding: source.container.padding,
                title_margin_top: 0.0,
                title_margin_bottom: 0.0,
            });
        }
        output.nodes.push(LayoutNode {
            id: node.id,
            x: node.x,
            y: node.y,
            width: node.width,
            height: node.height,
            is_cluster: source.kind == elk::NodeKind::Group,
            label_width: source.label.map(|label| label.width),
            label_height: source.label.map(|label| label.height),
        });
    }
    for edge in placed.edges {
        let source = graph_edges
            .get(edge.id.as_str())
            .ok_or_else(|| Error::InvalidModel {
                message: format!("ELK returned unknown state edge {}", edge.id),
            })?;
        let label = source.label.map(|size| {
            edge.labels
                .first()
                .map(|label| LayoutLabel {
                    x: label.x + label.width / 2.0,
                    y: label.y + label.height / 2.0,
                    width: label.width,
                    height: label.height,
                })
                .unwrap_or(LayoutLabel {
                    x: 0.0,
                    y: 0.0,
                    width: size.width,
                    height: size.height,
                })
        });
        output.edges.push(LayoutEdge {
            id: edge.id,
            from: source.source.clone(),
            to: source.target.clone(),
            from_cluster: (graph_nodes[source.source.as_str()].kind == elk::NodeKind::Group)
                .then(|| source.source.clone()),
            to_cluster: (graph_nodes[source.target.as_str()].kind == elk::NodeKind::Group)
                .then(|| source.target.clone()),
            points: edge
                .points
                .into_iter()
                .map(|p| LayoutPoint { x: p.x, y: p.y })
                .collect(),
            label,
            start_label_left: None,
            start_label_right: None,
            end_label_left: None,
            end_label_right: None,
            start_marker: None,
            end_marker: None,
            stroke_dasharray: None,
        });
    }
    prepare_paint_paths(&mut output, &source_nodes, &source_edges, config, work)?;
    output.nodes.sort_by(|a, b| a.id.cmp(&b.id));
    output.edges.sort_by(|a, b| a.id.cmp(&b.id));
    output.clusters.sort_by(|a, b| a.id.cmp(&b.id));
    Ok(output)
}

fn prepare_paint_paths(
    layout: &mut StateDiagramLayout,
    source_nodes: &HashMap<&str, &super::StateNode>,
    source_edges: &HashMap<&str, &merman_core::diagrams::state::StateDiagramRenderEdge>,
    config: &Value,
    work: &mut OperationLayoutWorkControl,
) -> Result<()> {
    use crate::elk_edge_geometry::{self as geometry, Outline, Shape};
    let indices: HashMap<_, _> = layout
        .nodes
        .iter()
        .enumerate()
        .map(|(i, n)| (n.id.clone(), i))
        .collect();
    // The upstream adapter centers small shapes on the first attached side anchor before
    // computing any route. This matters for start/end circles and fork/join bars.
    let mut aligned = std::collections::HashSet::new();
    for edge in &layout.edges {
        for (id, anchor) in [
            (&edge.from, edge.points.first()),
            (&edge.to, edge.points.last()),
        ] {
            work.charge_adapter(1)?;
            let (Some(&index), Some(anchor)) = (indices.get(id), anchor) else {
                continue;
            };
            let node = &mut layout.nodes[index];
            let horizontal_side = (anchor.y - node.y + node.height / 2.0).abs() <= 0.5
                || (anchor.y - node.y - node.height / 2.0).abs() <= 0.5;
            if (if horizontal_side {
                node.width
            } else {
                node.height
            }) >= 24.0
                || !aligned.insert(index)
            {
                continue;
            }
            let delta = if horizontal_side {
                anchor.x - node.x
            } else {
                anchor.y - node.y
            };
            if delta.abs() >= 0.01 {
                if horizontal_side {
                    node.x += delta;
                } else {
                    node.y += delta;
                }
            }
        }
    }
    let mut paths = Vec::with_capacity(layout.edges.len());
    for edge in &layout.edges {
        work.charge_adapter(work.checked_add(edge.points.len(), 50)?)?;
        let (Some(&start_index), Some(&end_index), Some(start_source), Some(end_source)) = (
            indices.get(&edge.from),
            indices.get(&edge.to),
            source_nodes.get(edge.from.as_str()),
            source_nodes.get(edge.to.as_str()),
        ) else {
            paths.push(edge.points.clone());
            continue;
        };
        let start = Shape {
            intersection: None,
            node: &layout.nodes[start_index],
            outline: match start_source.shape.as_str() {
                "stateStart" | "stateEnd" => Outline::Ellipse,
                "choice" => Outline::Diamond,
                _ => Outline::Rect,
            },
        };
        let end = Shape {
            intersection: None,
            node: &layout.nodes[end_index],
            outline: match end_source.shape.as_str() {
                "stateStart" | "stateEnd" => Outline::Ellipse,
                "choice" => Outline::Diamond,
                _ => Outline::Rect,
            },
        };
        let mut input = Vec::with_capacity(edge.points.len() + 2);
        input.push(LayoutPoint {
            x: start.node.x,
            y: start.node.y,
        });
        input.extend_from_slice(&edge.points);
        input.push(LayoutPoint {
            x: end.node.x,
            y: end.node.y,
        });
        let mut points = geometry::sanitize(&input, start, end);
        if !edge.points.is_empty()
            && let Some(source_edge) = source_edges.get(edge.id.as_str())
        {
            geometry::marker_segment(&mut points, end, Some(&source_edge.arrow_type_end), false);
        }
        paths.push(points);
    }
    if config
        .pointer("/elk/straightenEdges")
        .and_then(Value::as_bool)
        != Some(false)
    {
        geometry::straighten_routes(&mut paths, work)?;
    }
    for (edge, points) in layout.edges.iter_mut().zip(paths) {
        if edge.points.is_empty()
            && let (Some(label), Some(first), Some(last)) =
                (edge.label.as_mut(), points.first(), points.last())
        {
            label.x = (first.x + last.x) / 2.0;
            label.y = (first.y + last.y) / 2.0;
        }
        layout.elk_edge_paths.insert(edge.id.clone(), points);
    }
    layout.bounds = Bounds::from_points(
        layout
            .nodes
            .iter()
            .flat_map(|node| {
                [
                    (node.x - node.width / 2.0, node.y - node.height / 2.0),
                    (node.x + node.width / 2.0, node.y + node.height / 2.0),
                ]
            })
            .chain(
                layout
                    .elk_edge_paths
                    .values()
                    .flatten()
                    .map(|point| (point.x, point.y)),
            )
            .chain(
                layout
                    .edges
                    .iter()
                    .filter_map(|edge| edge.label.as_ref())
                    .flat_map(|label| {
                        [
                            (label.x - label.width / 2.0, label.y - label.height / 2.0),
                            (label.x + label.width / 2.0, label.y + label.height / 2.0),
                        ]
                    }),
            ),
    );
    Ok(())
}