merman-render 0.8.0-alpha.3

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
use crate::config::{config_bool, config_f64, config_string, normalize_css_font_family};
use crate::text::{TextStyle, WrapMode};
use serde_json::Value;

const DEFAULT_FLOWCHART_FONT_FAMILY: &str = r#""trebuchet ms",verdana,arial,sans-serif"#;
const DEFAULT_NODE_SPACING: f64 = 50.0;
const DEFAULT_RANK_SPACING: f64 = 50.0;
const DEFAULT_NODE_PADDING: f64 = 15.0;
const DEFAULT_STATE_PADDING: f64 = 8.0;
const DEFAULT_WRAPPING_WIDTH: f64 = 200.0;
const DEFAULT_DIAGRAM_PADDING: f64 = 8.0;
const DEFAULT_TITLE_TOP_MARGIN: f64 = 25.0;
const FIXED_CLUSTER_PADDING: f64 = 8.0;

// Mermaid `createText(...)` defaults its `width` argument to 200. Flowchart edge labels and
// markdown subgraph titles rely on that default instead of `flowchart.wrappingWidth`.
const FLOWCHART_FIXED_LABEL_WRAP_WIDTH: f64 = 200.0;

pub(crate) struct FlowchartConfigView<'a> {
    effective_config: &'a Value,
    flowchart_config: &'a Value,
    state_config: &'a Value,
    theme_variables: &'a Value,
}

impl<'a> FlowchartConfigView<'a> {
    pub(crate) fn new(effective_config: &'a Value) -> Self {
        Self {
            effective_config,
            flowchart_config: effective_config.get("flowchart").unwrap_or(&Value::Null),
            state_config: effective_config.get("state").unwrap_or(&Value::Null),
            theme_variables: effective_config
                .get("themeVariables")
                .unwrap_or(&Value::Null),
        }
    }

    pub(super) fn layout_settings(&self) -> FlowchartLayoutSettings {
        let node_html_labels = self.node_html_labels();
        let edge_html_labels = self.effective_html_labels();
        let cluster_html_labels = edge_html_labels;
        let node_wrap_mode = flowchart_wrap_mode(node_html_labels);
        let edge_wrap_mode = flowchart_wrap_mode(edge_html_labels);
        let cluster_wrap_mode = flowchart_wrap_mode(cluster_html_labels);
        let title_margin_top = self.layout_subgraph_title_margin("top");
        let title_margin_bottom = self.layout_subgraph_title_margin("bottom");
        let title_total_margin = title_margin_top + title_margin_bottom;
        let text_style = self.layout_text_style();
        let html_label_text_style = self.html_label_measurement_base_style(&text_style);

        FlowchartLayoutSettings {
            nodesep: self.dagre_spacing_or_default("nodeSpacing", DEFAULT_NODE_SPACING),
            ranksep: self.dagre_spacing_or_default("rankSpacing", DEFAULT_RANK_SPACING),
            node_padding: self
                .flowchart_compat_f64("padding")
                .unwrap_or(DEFAULT_NODE_PADDING),
            state_padding: self
                .state_compat_f64("padding")
                .unwrap_or(DEFAULT_STATE_PADDING),
            wrapping_width: self
                .flowchart_compat_f64("wrappingWidth")
                .unwrap_or(DEFAULT_WRAPPING_WIDTH),
            edge_label_wrapping_width: FLOWCHART_FIXED_LABEL_WRAP_WIDTH,
            cluster_title_wrapping_width: FLOWCHART_FIXED_LABEL_WRAP_WIDTH,
            edge_html_labels,
            node_html_label_css_parity: node_html_labels && edge_html_labels,
            node_wrap_mode,
            edge_wrap_mode,
            cluster_wrap_mode,
            cluster_padding: FIXED_CLUSTER_PADDING,
            title_margin_top,
            title_margin_bottom,
            title_total_margin,
            y_shift: title_total_margin / 2.0,
            inherit_dir: self.flowchart_bool("inheritDir").unwrap_or(false),
            text_style,
            html_label_text_style,
        }
    }

    pub(crate) fn font_family(&self) -> String {
        let theme_font_family = self
            .theme_string("fontFamily")
            .map(|s| normalize_css_font_family(&s));
        let top_font_family = self
            .root_string("fontFamily")
            .map(|s| normalize_css_font_family(&s));

        match (top_font_family, theme_font_family) {
            (Some(top), Some(theme)) if theme == DEFAULT_FLOWCHART_FONT_FAMILY => top,
            (_, Some(theme)) => theme,
            (Some(top), None) => top,
            (None, None) => DEFAULT_FLOWCHART_FONT_FAMILY.to_string(),
        }
    }

    pub(crate) fn render_font_size(&self) -> f64 {
        self.theme_font_size_px().unwrap_or(16.0).max(1.0)
    }

    pub(crate) fn render_wrapping_width(&self) -> f64 {
        self.flowchart_compat_f64("wrappingWidth")
            .unwrap_or(DEFAULT_WRAPPING_WIDTH)
            .max(1.0)
    }

    pub(crate) fn render_diagram_padding(&self) -> f64 {
        self.flowchart_compat_f64("diagramPadding")
            .unwrap_or(DEFAULT_DIAGRAM_PADDING)
            .max(0.0)
    }

    pub(crate) fn render_use_max_width(&self) -> bool {
        self.flowchart_bool("useMaxWidth").unwrap_or(true)
    }

    pub(crate) fn render_title_top_margin(&self) -> f64 {
        self.flowchart_compat_f64("titleTopMargin")
            .unwrap_or(DEFAULT_TITLE_TOP_MARGIN)
            .max(0.0)
    }

    pub(crate) fn render_node_padding(&self) -> f64 {
        self.flowchart_compat_f64("padding")
            .unwrap_or(DEFAULT_NODE_PADDING)
            .max(0.0)
    }

    pub(crate) fn render_subgraph_title_y_shift(&self) -> f64 {
        let top = self.layout_subgraph_title_margin("top").max(0.0);
        let bottom = self.layout_subgraph_title_margin("bottom").max(0.0);
        (top + bottom) / 2.0
    }

    pub(crate) fn render_curve(&self) -> Option<String> {
        self.flowchart_string("curve")
    }

    pub(crate) fn render_text_style(&self, font_family: &str, font_size: f64) -> TextStyle {
        TextStyle {
            font_family: Some(font_family.to_string()),
            font_size,
            font_weight: None,
        }
    }

    pub(crate) fn node_html_labels(&self) -> bool {
        // Mermaid 11.15 node shapes use `evaluate(getConfig()?.htmlLabels)` in labelHelper, while
        // edge and cluster labels use `getEffectiveHtmlLabels(...)` and still honor the deprecated
        // `flowchart.htmlLabels` fallback.
        self.root_bool("htmlLabels").unwrap_or(true)
    }

    pub(crate) fn effective_html_labels(&self) -> bool {
        self.root_bool("htmlLabels")
            .or_else(|| self.flowchart_bool("htmlLabels"))
            .unwrap_or(true)
    }

    pub(crate) fn node_wrap_mode(&self) -> WrapMode {
        flowchart_wrap_mode(self.node_html_labels())
    }

    pub(crate) fn edge_wrap_mode(&self) -> WrapMode {
        flowchart_wrap_mode(self.effective_html_labels())
    }

    pub(crate) fn html_label_measurement_base_style(&self, render_style: &TextStyle) -> TextStyle {
        let mut style = render_style.clone();
        // Mermaid serializes numeric themeVariables.fontSize into CSS without a unit
        // (`font-size:24`), which does not affect foreignObject HTML labels in Chromium. A CSS px
        // string (`"20px"`) is valid and does affect those labels.
        style.font_size = self
            .theme_variables
            .get("fontSize")
            .and_then(Value::as_str)
            .and_then(|raw| {
                let raw = raw.trim();
                if !raw.to_ascii_lowercase().ends_with("px") {
                    return None;
                }
                crate::mermaid_style::parse_css_font_size_px(raw, render_style.font_size)
            })
            .unwrap_or(16.0);
        style
    }

    pub(crate) fn theme_color(&self, key: &str, fallback: &str) -> String {
        self.theme_string(key)
            .unwrap_or_else(|| fallback.to_string())
    }

    fn layout_text_style(&self) -> TextStyle {
        TextStyle {
            font_family: Some(self.font_family()),
            font_size: self.theme_font_size_px().unwrap_or(16.0),
            font_weight: self.root_string("fontWeight"),
        }
    }

    fn dagre_spacing_or_default(&self, key: &str, default: f64) -> f64 {
        let Some(raw) = self.flowchart_config.get(key) else {
            return default;
        };

        // Mermaid Flowchart assigns `conf?.nodeSpacing || 50` and `conf?.rankSpacing || 50`,
        // so numeric zero falls back to the default instead of becoming a real Dagre separation.
        if raw.is_number() {
            return raw
                .as_f64()
                .filter(|value| *value != 0.0)
                .unwrap_or(default);
        }

        self.flowchart_compat_f64(key).unwrap_or(default)
    }

    fn theme_font_size_px(&self) -> Option<f64> {
        self.theme_variables
            .get("fontSize")
            .and_then(parse_font_size_px)
    }

    fn layout_subgraph_title_margin(&self, key: &str) -> f64 {
        self.flowchart_config
            .get("subGraphTitleMargin")
            .and_then(|margin| margin.get(key))
            .and_then(crate::config::json_f64)
            .unwrap_or(0.0)
    }

    fn root_bool(&self, key: &str) -> Option<bool> {
        config_bool(self.effective_config, &[key])
    }

    fn flowchart_bool(&self, key: &str) -> Option<bool> {
        config_bool(self.flowchart_config, &[key])
    }

    fn root_string(&self, key: &str) -> Option<String> {
        config_string(self.effective_config, &[key])
    }

    fn theme_string(&self, key: &str) -> Option<String> {
        config_string(self.theme_variables, &[key])
    }

    fn flowchart_string(&self, key: &str) -> Option<String> {
        config_string(self.flowchart_config, &[key])
    }

    fn flowchart_compat_f64(&self, key: &str) -> Option<f64> {
        config_f64(self.flowchart_config, &[key])
    }

    fn state_compat_f64(&self, key: &str) -> Option<f64> {
        config_f64(self.state_config, &[key])
    }
}

pub(super) struct FlowchartLayoutSettings {
    pub(super) nodesep: f64,
    pub(super) ranksep: f64,
    pub(super) node_padding: f64,
    pub(super) state_padding: f64,
    pub(super) wrapping_width: f64,
    pub(super) edge_label_wrapping_width: f64,
    pub(super) cluster_title_wrapping_width: f64,
    pub(super) edge_html_labels: bool,
    pub(super) node_html_label_css_parity: bool,
    pub(super) node_wrap_mode: WrapMode,
    pub(super) edge_wrap_mode: WrapMode,
    pub(super) cluster_wrap_mode: WrapMode,
    pub(super) cluster_padding: f64,
    pub(super) title_margin_top: f64,
    pub(super) title_margin_bottom: f64,
    pub(super) title_total_margin: f64,
    pub(super) y_shift: f64,
    pub(super) inherit_dir: bool,
    pub(super) text_style: TextStyle,
    pub(super) html_label_text_style: TextStyle,
}

fn flowchart_wrap_mode(html_labels: bool) -> WrapMode {
    if html_labels {
        WrapMode::HtmlLike
    } else {
        WrapMode::SvgLike
    }
}

fn parse_font_size_px(v: &Value) -> Option<f64> {
    if let Some(n) = v.as_f64() {
        return Some(n);
    }
    if let Some(n) = v.as_i64() {
        return Some(n as f64);
    }
    if let Some(n) = v.as_u64() {
        return Some(n as f64);
    }
    let s = v.as_str()?.trim();
    if s.is_empty() {
        return None;
    }
    let mut num = String::new();
    for (idx, ch) in s.chars().enumerate() {
        if ch.is_ascii_digit() {
            num.push(ch);
            continue;
        }
        if idx == 0 && (ch == '-' || ch == '+') {
            num.push(ch);
            continue;
        }
        break;
    }
    if num.trim().is_empty() {
        return None;
    }
    num.parse::<f64>().ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn flowchart_layout_settings_preserve_dagre_spacing_compatibility() {
        let cfg = json!({
            "flowchart": {
                "nodeSpacing": 0,
                "rankSpacing": "70"
            }
        });

        let settings = FlowchartConfigView::new(&cfg).layout_settings();

        assert_eq!(settings.nodesep, 50.0);
        assert_eq!(settings.ranksep, 70.0);
    }

    #[test]
    fn flowchart_layout_settings_preserve_html_label_split_semantics() {
        let cfg = json!({
            "htmlLabels": false,
            "flowchart": {
                "htmlLabels": true
            }
        });

        let config = FlowchartConfigView::new(&cfg);
        let settings = config.layout_settings();

        assert!(!config.node_html_labels());
        assert!(!config.effective_html_labels());
        assert_eq!(settings.node_wrap_mode, WrapMode::SvgLike);
        assert_eq!(settings.edge_wrap_mode, WrapMode::SvgLike);
        assert!(!settings.edge_html_labels);

        let cfg = json!({
            "flowchart": {
                "htmlLabels": false
            }
        });
        let config = FlowchartConfigView::new(&cfg);
        let settings = config.layout_settings();

        assert!(config.node_html_labels());
        assert!(!config.effective_html_labels());
        assert_eq!(settings.node_wrap_mode, WrapMode::HtmlLike);
        assert_eq!(settings.edge_wrap_mode, WrapMode::SvgLike);
        assert!(!settings.node_html_label_css_parity);
    }

    #[test]
    fn flowchart_font_family_preserves_theme_default_root_override() {
        let cfg = json!({
            "fontFamily": "Root Sans, Arial",
            "themeVariables": {
                "fontFamily": "\"trebuchet ms\", verdana, arial, sans-serif"
            }
        });

        let config = FlowchartConfigView::new(&cfg);

        assert_eq!(config.font_family(), "Root Sans,Arial");

        let cfg = json!({
            "fontFamily": "Root Sans",
            "themeVariables": {
                "fontFamily": "Theme Sans, Arial"
            }
        });

        assert_eq!(
            FlowchartConfigView::new(&cfg).font_family(),
            "Theme Sans,Arial"
        );
    }

    #[test]
    fn flowchart_font_size_keeps_legacy_leading_integer_parsing() {
        let cfg = json!({
            "themeVariables": {
                "fontSize": "24.5px"
            }
        });
        let settings = FlowchartConfigView::new(&cfg).layout_settings();

        assert_eq!(settings.text_style.font_size, 24.0);
        assert_eq!(settings.html_label_text_style.font_size, 24.5);
    }

    #[test]
    fn flowchart_render_settings_keep_numeric_boundaries() {
        let cfg = json!({
            "themeVariables": {
                "fontSize": 0,
                "mainBkg": "#112233",
                "nodeBorder": "#445566"
            },
            "flowchart": {
                "wrappingWidth": 0,
                "diagramPadding": "-2",
                "useMaxWidth": false,
                "titleTopMargin": "-4",
                "padding": "-6",
                "curve": "linear",
                "subGraphTitleMargin": {
                    "top": "-3",
                    "bottom": "8"
                }
            }
        });
        let config = FlowchartConfigView::new(&cfg);

        assert_eq!(config.render_font_size(), 1.0);
        assert_eq!(config.render_wrapping_width(), 1.0);
        assert_eq!(config.render_diagram_padding(), 0.0);
        assert!(!config.render_use_max_width());
        assert_eq!(config.render_title_top_margin(), 0.0);
        assert_eq!(config.render_node_padding(), 0.0);
        assert_eq!(config.render_curve().as_deref(), Some("linear"));
        assert_eq!(config.render_subgraph_title_y_shift(), 4.0);
        assert_eq!(config.theme_color("mainBkg", "#ECECFF"), "#112233");
        assert_eq!(config.theme_color("nodeBorder", "#9370DB"), "#445566");
    }

    #[test]
    fn flowchart_layout_settings_keep_fixed_label_wrap_widths() {
        let cfg = json!({
            "flowchart": {
                "wrappingWidth": 320
            }
        });

        let settings = FlowchartConfigView::new(&cfg).layout_settings();

        assert_eq!(settings.wrapping_width, 320.0);
        assert_eq!(
            settings.edge_label_wrapping_width,
            FLOWCHART_FIXED_LABEL_WRAP_WIDTH
        );
        assert_eq!(
            settings.cluster_title_wrapping_width,
            FLOWCHART_FIXED_LABEL_WRAP_WIDTH
        );
    }
}