merman-render 0.5.0

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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use crate::config::{config_f64, config_f64_css_px};
use crate::json::from_value_ref;
use crate::model::{
    Bounds, LayoutEdge, LayoutLabel, LayoutNode, LayoutPoint, RequirementDiagramLayout,
};
use crate::text::{TextMeasurer, TextStyle, WrapMode};
use crate::{Error, Result};
use dugong::graphlib::{Graph, GraphOptions};
use dugong::{EdgeLabel, GraphLabel, LabelPos, NodeLabel, RankDir};
use merman_core::diagrams::requirement::RequirementDiagramRenderModel;
use serde_json::Value;

fn config_string(cfg: &Value, path: &[&str]) -> Option<String> {
    let mut cur = cfg;
    for key in path {
        cur = cur.get(*key)?;
    }
    cur.as_str().map(|s| s.to_string()).or_else(|| {
        cur.as_array()
            .and_then(|values| values.first()?.as_str())
            .map(|s| s.to_string())
    })
}

fn normalize_dir(direction: &str) -> String {
    match direction.trim().to_uppercase().as_str() {
        "TB" | "TD" => "TB".to_string(),
        "BT" => "BT".to_string(),
        "LR" => "LR".to_string(),
        "RL" => "RL".to_string(),
        other => other.to_string(),
    }
}

fn rank_dir_from(direction: &str) -> RankDir {
    match normalize_dir(direction).as_str() {
        "TB" => RankDir::TB,
        "BT" => RankDir::BT,
        "LR" => RankDir::LR,
        "RL" => RankDir::RL,
        _ => RankDir::TB,
    }
}

#[derive(Debug, Clone)]
struct RequirementLabelMetrics {
    width: f64,
    height: f64,
}

fn requirement_label_uses_markdown_html(raw: &str) -> bool {
    let lower = raw.to_ascii_lowercase();
    raw.contains('*') || raw.contains('_') || raw.contains('\n') || lower.contains("<br")
}

pub(crate) fn requirement_styles_force_bold(css_styles: &[String]) -> bool {
    css_styles.iter().any(|raw| {
        let s = raw.trim().trim_end_matches(';');
        let Some((key, value)) = s.split_once(':') else {
            return false;
        };
        if !key.trim().eq_ignore_ascii_case("font-weight") {
            return false;
        }
        let value = value
            .split_once("!important")
            .map(|(v, _)| v)
            .unwrap_or(value)
            .trim()
            .to_ascii_lowercase();
        value == "bold"
            || value == "bolder"
            || value
                .parse::<u16>()
                .map(|weight| weight >= 600)
                .unwrap_or(false)
    })
}

fn measure_requirement_label_metrics(
    measurer: &dyn TextMeasurer,
    html_style: &TextStyle,
    display_text: &str,
    bold: bool,
) -> Option<RequirementLabelMetrics> {
    if display_text.trim().is_empty() {
        return None;
    }

    let font_size = html_style.font_size.max(1.0);
    let looks_like_markdown_inline = requirement_label_uses_markdown_html(display_text);
    let measured = if looks_like_markdown_inline {
        crate::text::measure_markdown_with_flowchart_bold_deltas(
            measurer,
            display_text,
            html_style,
            None,
            WrapMode::HtmlLike,
        )
    } else {
        measurer.measure_wrapped(display_text, html_style, None, WrapMode::HtmlLike)
    };
    let height = measured.height.max(1.0);
    let width = if let Some(em) =
        crate::generated::requirement_text_overrides_11_12_2::lookup_requirement_html_label_width_em(
            display_text,
            bold,
        ) {
        (em * font_size).max(1.0)
    } else {
        measured.width.max(1.0)
    };

    Some(RequirementLabelMetrics { width, height })
}

#[derive(Debug, Clone)]
struct RequirementBoxLayout {
    width: f64,
    height: f64,
}

fn requirement_box_layout(
    measurer: &dyn TextMeasurer,
    html_style_regular: &TextStyle,
    html_style_bold: &TextStyle,
    lines: &[(String, bool)],
    gap: f64,
    padding: f64,
) -> RequirementBoxLayout {
    // Mirrors Mermaid `requirementBox.ts` label stacking and bbox-based sizing.
    let mut html_metrics: Vec<Option<RequirementLabelMetrics>> = Vec::with_capacity(lines.len());
    let mut max_w: f64 = 0.0;

    // First pass: label bbox widths/heights (HTML).
    for (display, bold) in lines {
        let html_style = if *bold {
            html_style_bold
        } else {
            html_style_regular
        };
        let m = measure_requirement_label_metrics(measurer, html_style, display, *bold);
        if let Some(m) = &m {
            max_w = max_w.max(m.width);
        }
        html_metrics.push(m);
    }

    let total_w = max_w + padding;

    // Second pass: vertical extents.
    let mut min_y = 0.0;
    let mut max_y = 0.0;
    let mut y_offset = 0.0;

    for (idx, m) in html_metrics.iter().enumerate() {
        let Some(m) = m else {
            continue;
        };

        if idx == 0 {
            min_y = -m.height / 2.0;
            max_y = m.height / 2.0;
            y_offset = m.height;
            continue;
        }
        if idx == 1 {
            let top = -m.height / 2.0 + y_offset;
            let bottom = m.height / 2.0 + y_offset;
            min_y = min_y.min(top);
            max_y = max_y.max(bottom);
            y_offset += m.height + gap;
            continue;
        }

        let top = -m.height / 2.0 + y_offset;
        let bottom = m.height / 2.0 + y_offset;
        min_y = min_y.min(top);
        max_y = max_y.max(bottom);
        y_offset += m.height;
    }

    let bbox_h = (max_y - min_y).max(1.0);
    let total_h = bbox_h + padding;

    RequirementBoxLayout {
        width: total_w.max(1.0),
        height: total_h.max(1.0),
    }
}

fn requirement_edge_id(src: &str, dst: &str, idx: usize) -> String {
    format!("{src}-{dst}-{idx}")
}

fn prefixed_nonempty_line(prefix: &str, value: &str) -> String {
    let value = value.trim();
    if value.is_empty() {
        String::new()
    } else {
        format!("{prefix}{value}")
    }
}

pub fn layout_requirement_diagram(
    model: &Value,
    effective_config: &Value,
    text_measurer: &dyn TextMeasurer,
) -> Result<RequirementDiagramLayout> {
    let model: RequirementDiagramRenderModel = from_value_ref(model)?;
    layout_requirement_diagram_typed(&model, effective_config, text_measurer)
}

pub fn layout_requirement_diagram_typed(
    model: &RequirementDiagramRenderModel,
    effective_config: &Value,
    text_measurer: &dyn TextMeasurer,
) -> Result<RequirementDiagramLayout> {
    let direction = if model.direction.trim().is_empty() {
        normalize_dir("TB")
    } else {
        normalize_dir(&model.direction)
    };

    let nodesep = config_f64(effective_config, &["nodeSpacing"])
        .or_else(|| config_f64(effective_config, &["flowchart", "nodeSpacing"]))
        .unwrap_or(50.0);
    let ranksep = config_f64(effective_config, &["rankSpacing"])
        .or_else(|| config_f64(effective_config, &["flowchart", "rankSpacing"]))
        .unwrap_or(50.0);

    let font_family = config_string(effective_config, &["themeVariables", "fontFamily"])
        .or_else(|| config_string(effective_config, &["fontFamily"]))
        .or_else(|| Some("\"trebuchet ms\", verdana, arial, sans-serif".to_string()));
    let font_size = config_f64_css_px(effective_config, &["themeVariables", "fontSize"])
        .or_else(|| config_f64_css_px(effective_config, &["fontSize"]))
        .unwrap_or(16.0);
    let html_style_regular = TextStyle {
        font_family: font_family.clone(),
        font_size,
        font_weight: None,
    };
    let html_style_bold = TextStyle {
        font_family,
        font_size,
        font_weight: Some("bold".to_string()),
    };

    let padding = 20.0;
    let gap = 20.0;

    let mut g = Graph::<NodeLabel, EdgeLabel, GraphLabel>::new(GraphOptions {
        directed: true,
        multigraph: true,
        compound: true,
    });
    g.set_graph(GraphLabel {
        rankdir: rank_dir_from(&direction),
        nodesep,
        ranksep,
        marginx: 8.0,
        marginy: 8.0,
        ..Default::default()
    });

    for r in &model.requirements {
        // Mermaid's underlying graph data structures historically used plain JS objects in a few
        // places. The `__proto__` id can still trigger prototype pollution safeguards, effectively
        // dropping the node from the rendered graph. Mirror the upstream SVG baselines.
        if r.name == "__proto__" {
            continue;
        }

        let type_disp = format!("<<{}>>", r.node_type);
        let style_bold = requirement_styles_force_bold(&r.css_styles);
        let mut lines: Vec<(String, bool)> = Vec::new();
        lines.push((type_disp, style_bold));
        lines.push((r.name.clone(), true));

        let id_line = prefixed_nonempty_line("ID: ", &r.requirement_id);
        lines.push((id_line, style_bold));

        let text_line = prefixed_nonempty_line("Text: ", &r.text);
        lines.push((text_line, style_bold));

        let risk_line = prefixed_nonempty_line("Risk: ", &r.risk);
        lines.push((risk_line, style_bold));

        let verify_line = prefixed_nonempty_line("Verification: ", &r.verify_method);
        lines.push((verify_line, style_bold));

        let box_layout = requirement_box_layout(
            text_measurer,
            &html_style_regular,
            &html_style_bold,
            &lines,
            gap,
            padding,
        );
        g.set_node(
            r.name.clone(),
            NodeLabel {
                width: box_layout.width,
                height: box_layout.height,
                ..Default::default()
            },
        );
    }

    for e in &model.elements {
        if e.name == "__proto__" {
            continue;
        }

        let type_disp = "<<Element>>".to_string();
        let style_bold = requirement_styles_force_bold(&e.css_styles);
        let mut lines: Vec<(String, bool)> = Vec::new();
        lines.push((type_disp, style_bold));
        lines.push((e.name.clone(), true));

        let type_line = e.element_type.trim().to_string();
        let type_line = if type_line.is_empty() {
            String::new()
        } else {
            format!("Type: {type_line}")
        };
        lines.push((type_line, style_bold));

        let doc_line = prefixed_nonempty_line("Doc Ref: ", &e.doc_ref);
        lines.push((doc_line, style_bold));

        let box_layout = requirement_box_layout(
            text_measurer,
            &html_style_regular,
            &html_style_bold,
            &lines,
            gap,
            padding,
        );
        g.set_node(
            e.name.clone(),
            NodeLabel {
                width: box_layout.width,
                height: box_layout.height,
                ..Default::default()
            },
        );
    }

    for rel in &model.relationships {
        if !g.has_node(&rel.src) {
            return Err(Error::InvalidModel {
                message: format!("relationship src node not found: {}", rel.src),
            });
        }
        if !g.has_node(&rel.dst) {
            return Err(Error::InvalidModel {
                message: format!("relationship dst node not found: {}", rel.dst),
            });
        }

        // Mermaid's requirement diagram edge ids currently collide for multiple relationships
        // between the same nodes (the upstream DB resets the counter for every relation).
        // Downstream graphlib layout will overwrite earlier edges; only the last relation survives.
        // Mirror this behavior to match the upstream SVG baselines.
        let edge_id = requirement_edge_id(&rel.src, &rel.dst, 0);

        let label_display = format!("<<{}>>", rel.rel_type);
        let metrics = measure_requirement_label_metrics(
            text_measurer,
            &html_style_regular,
            &label_display,
            false,
        )
        .unwrap_or(RequirementLabelMetrics {
            width: 0.0,
            height: 0.0,
        });

        let el = EdgeLabel {
            width: metrics.width.max(0.0),
            height: metrics.height.max(0.0),
            labelpos: LabelPos::C,
            // Dagre defaults to 10 when unspecified.
            labeloffset: 10.0,
            minlen: 1,
            weight: 1.0,
            ..Default::default()
        };
        g.set_edge_named(rel.src.clone(), rel.dst.clone(), Some(edge_id), Some(el));
    }

    dugong::layout_dagreish(&mut g);

    let mut out_nodes: Vec<LayoutNode> = Vec::new();
    for v in g.nodes() {
        let Some(n) = g.node(v) else {
            continue;
        };
        let (Some(cx), Some(cy)) = (n.x, n.y) else {
            continue;
        };
        out_nodes.push(LayoutNode {
            id: v.to_string(),
            x: cx - n.width / 2.0,
            y: cy - n.height / 2.0,
            width: n.width,
            height: n.height,
            is_cluster: false,
            label_width: None,
            label_height: None,
        });
    }

    let mut out_edges: Vec<LayoutEdge> = Vec::new();
    for ek in g.edge_keys() {
        let Some(e) = g.edge_by_key(&ek) else {
            continue;
        };

        let points = e
            .points
            .iter()
            .map(|p| LayoutPoint { x: p.x, y: p.y })
            .collect::<Vec<_>>();

        let label = match (e.x, e.y) {
            (Some(x), Some(y)) if e.width > 0.0 && e.height > 0.0 => Some(LayoutLabel {
                x,
                y,
                width: e.width,
                height: e.height,
            }),
            _ => None,
        };

        out_edges.push(LayoutEdge {
            id: ek
                .name
                .clone()
                .unwrap_or_else(|| format!("{}-{}", ek.v, ek.w)),
            from: ek.v.clone(),
            to: ek.w.clone(),
            from_cluster: None,
            to_cluster: None,
            points,
            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,
        });
    }

    fn bounds_for_nodes_edges(nodes: &[LayoutNode], edges: &[LayoutEdge]) -> Option<Bounds> {
        if nodes.is_empty() && edges.is_empty() {
            return None;
        }
        let mut min_x = f64::INFINITY;
        let mut min_y = f64::INFINITY;
        let mut max_x = f64::NEG_INFINITY;
        let mut max_y = f64::NEG_INFINITY;

        for n in nodes {
            min_x = min_x.min(n.x);
            min_y = min_y.min(n.y);
            max_x = max_x.max(n.x + n.width);
            max_y = max_y.max(n.y + n.height);
        }
        for e in edges {
            for p in &e.points {
                min_x = min_x.min(p.x);
                min_y = min_y.min(p.y);
                max_x = max_x.max(p.x);
                max_y = max_y.max(p.y);
            }
            if let Some(l) = &e.label {
                min_x = min_x.min(l.x - l.width / 2.0);
                max_x = max_x.max(l.x + l.width / 2.0);
                min_y = min_y.min(l.y - l.height / 2.0);
                max_y = max_y.max(l.y + l.height / 2.0);
            }
        }

        if !min_x.is_finite() || !min_y.is_finite() || !max_x.is_finite() || !max_y.is_finite() {
            return None;
        }
        Some(Bounds {
            min_x,
            min_y,
            max_x,
            max_y,
        })
    }

    let bounds = bounds_for_nodes_edges(&out_nodes, &out_edges);

    Ok(RequirementDiagramLayout {
        nodes: out_nodes,
        edges: out_edges,
        bounds,
    })
}

#[cfg(test)]
mod tests {
    #[test]
    fn requirement_styles_detect_bold_font_weight() {
        assert!(super::requirement_styles_force_bold(&[
            "fill:#f9f".to_string(),
            " font-weight:bold".to_string(),
        ]));
        assert!(super::requirement_styles_force_bold(&[
            "font-weight: 700 !important".to_string(),
        ]));
        assert!(!super::requirement_styles_force_bold(&[
            "font-weight: normal".to_string(),
            "stroke:blue".to_string(),
        ]));
    }
}