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
use crate::Result;
use crate::config::config_string;
use crate::model::{Bounds, PieDiagramLayout, PieLegendItemLayout, PieSliceLayout};
use crate::text::{TextMeasurer, TextStyle};
use merman_core::diagrams::pie::{PieDiagramRenderModel, PieRenderSection};

pub(crate) const PIE_LEGEND_RECT_SIZE_PX: f64 = 18.0;
pub(crate) const PIE_LEGEND_SPACING_PX: f64 = 4.0;

mod config;

pub(crate) use config::{PieConfigView, PieLegendPosition};

#[derive(Debug, Clone)]
struct ColorScale {
    palette: Vec<String>,
    mapping: std::collections::HashMap<String, usize>,
    next: usize,
}

impl ColorScale {
    fn default_palette() -> Vec<String> {
        // This fallback is only for direct layout callers. Normal rendering consumes the final
        // resolved `pie1..pie12` theme variables produced by merman-core.
        [
            "#ECECFF",
            "#ffffde",
            "hsl(80, 100%, 56.2745098039%)",
            "hsl(240, 100%, 86.2745098039%)",
            "hsl(60, 100%, 63.5294117647%)",
            "hsl(80, 100%, 76.2745098039%)",
            "hsl(300, 100%, 76.2745098039%)",
            "hsl(180, 100%, 56.2745098039%)",
            "hsl(0, 100%, 56.2745098039%)",
            "hsl(300, 100%, 56.2745098039%)",
            "hsl(150, 100%, 56.2745098039%)",
            "hsl(0, 100%, 66.2745098039%)",
        ]
        .into_iter()
        .map(str::to_string)
        .collect()
    }

    fn from_config(effective_config: &serde_json::Value) -> Self {
        let mut palette = Self::default_palette();
        for (idx, color) in palette.iter_mut().enumerate() {
            let key = format!("pie{}", idx + 1);
            if let Some(value) = config_string(effective_config, &["themeVariables", &key]) {
                *color = value;
            }
        }
        Self {
            palette,
            mapping: std::collections::HashMap::new(),
            next: 0,
        }
    }

    fn color_for(&mut self, label: &str) -> String {
        if let Some(idx) = self.mapping.get(label).copied() {
            return self.palette[idx % self.palette.len()].clone();
        }
        let idx = self.next;
        self.next += 1;
        self.mapping.insert(label.to_string(), idx);
        self.palette[idx % self.palette.len()].clone()
    }
}

fn polar_xy(radius: f64, angle: f64) -> (f64, f64) {
    // Mermaid pie charts use a "12 o'clock is zero" convention with y increasing downwards.
    let x = radius * angle.sin();
    let y = -radius * angle.cos();
    (x, y)
}

fn fmt_number(v: f64) -> String {
    if !v.is_finite() {
        return "0".to_string();
    }
    if v.abs() < 0.0005 {
        return "0".to_string();
    }
    let mut r = (v * 1000.0).round() / 1000.0;
    if r.abs() < 0.0005 {
        r = 0.0;
    }
    let mut s = format!("{r:.3}");
    if s.contains('.') {
        while s.ends_with('0') {
            s.pop();
        }
        if s.ends_with('.') {
            s.pop();
        }
    }
    if s == "-0" { "0".to_string() } else { s }
}

pub(crate) fn layout_pie_diagram_typed(
    model: &PieDiagramRenderModel,
    diagram_title: Option<&str>,
    effective_config: &serde_json::Value,
    measurer: &dyn TextMeasurer,
) -> Result<PieDiagramLayout> {
    let _ = (
        model.title.as_deref(),
        model.acc_title.as_deref(),
        model.acc_descr.as_deref(),
    );

    // Mermaid@11.16 `packages/mermaid/src/diagrams/pie/pieRenderer.ts` constants.
    let margin: f64 = 40.0;
    let legend_rect_size = PIE_LEGEND_RECT_SIZE_PX;
    let legend_spacing = PIE_LEGEND_SPACING_PX;

    let center: f64 = 225.0;
    let radius: f64 = 185.0;
    let outer_radius = radius + 1.0;
    let cfg = PieConfigView::new(effective_config).layout_settings();
    let title = model.title.as_deref().or(diagram_title);
    let label_radius = radius.max(0.0) * cfg.text_position;
    let legend_step_y: f64 = legend_rect_size + legend_spacing;
    let legend_position = cfg.legend_position;
    let total_legend_height = (model.sections.len() as f64) * legend_step_y;
    let centered_legend_start_y = -(legend_step_y * (model.sections.len().max(1) as f64)) / 2.0;
    let legend_start_y = match legend_position {
        PieLegendPosition::Top => -radius,
        PieLegendPosition::Bottom => radius + legend_step_y,
        _ => centered_legend_start_y,
    };

    let total: f64 = model
        .sections
        .iter()
        .filter(|s| s.value.is_finite() && s.value >= 0.0)
        .map(|s| s.value)
        .sum();

    let mut color_scale = ColorScale::from_config(effective_config);
    for sec in &model.sections {
        let _ = color_scale.color_for(&sec.label);
    }

    let mut slices: Vec<PieSliceLayout> = Vec::new();
    if total.is_finite() && total > 0.0 {
        // Mermaid@11.16 `packages/mermaid/src/diagrams/pie/pieRenderer.ts`:
        //
        // - filter out values < 1% (based on the original total)
        // - preserve input order before D3 pie() computes angles (`sort(null)`)
        // - angles are normalized over the filtered set (so drawn slices fill the whole circle)
        // - percentage labels are still computed using the original total
        let pie_sections: Vec<&PieRenderSection> = model
            .sections
            .iter()
            .filter(|s| s.value.is_finite() && s.value > 0.0)
            .filter(|s| (s.value / total) * 100.0 >= 1.0)
            .collect();

        let pie_total: f64 = pie_sections.iter().map(|s| s.value).sum();
        if !pie_sections.is_empty() && pie_total.is_finite() && pie_total > 0.0 {
            if pie_sections.len() == 1 {
                let s = pie_sections[0];
                let fill = color_scale.color_for(&s.label);
                let (tx, ty) = polar_xy(label_radius, std::f64::consts::PI);
                let percent = ((100.0 * (s.value / total)).max(0.0)).round() as i64;
                slices.push(PieSliceLayout {
                    label: s.label.clone(),
                    value: s.value,
                    start_angle: 0.0,
                    end_angle: std::f64::consts::TAU,
                    is_full_circle: true,
                    percent,
                    text_x: tx,
                    text_y: ty,
                    fill,
                });
            } else {
                let mut start = 0.0;
                for s in pie_sections {
                    let frac = (s.value / pie_total).max(0.0);
                    let delta = (frac * std::f64::consts::TAU).max(0.0);
                    let end = start + delta;
                    let mid = (start + end) / 2.0;
                    let (tx, ty) = polar_xy(label_radius, mid);
                    let fill = color_scale.color_for(&s.label);
                    let percent = ((100.0 * (s.value / total)).max(0.0)).round() as i64;
                    if percent != 0 {
                        slices.push(PieSliceLayout {
                            label: s.label.clone(),
                            value: s.value,
                            start_angle: start,
                            end_angle: end,
                            is_full_circle: false,
                            percent,
                            text_x: tx,
                            text_y: ty,
                            fill,
                        });
                    }
                    start = end;
                }
            }
        }
    }

    // Lock the color scale domain based on the drawn slices first, then compute legend colors in
    // the original section order (this matches Mermaid's zero-slice behavior).
    let mut legend_items: Vec<PieLegendItemLayout> = Vec::new();
    for (i, sec) in model.sections.iter().enumerate() {
        let y = legend_start_y + (i as f64) * legend_step_y;
        let fill = color_scale.color_for(&sec.label);
        legend_items.push(PieLegendItemLayout {
            label: sec.label.clone(),
            value: sec.value,
            fill,
            y,
        });
    }

    let legend_style = TextStyle {
        font_family: None,
        font_size: 17.0,
        font_weight: None,
        font_style: None,
    };
    let title_style = TextStyle {
        font_family: None,
        font_size: 25.0,
        font_weight: None,
        font_style: None,
    };
    let mut max_legend_width: f64 = 0.0;
    for sec in &model.sections {
        let label = if model.show_data {
            format!("{} [{}]", sec.label, fmt_number(sec.value))
        } else {
            sec.label.clone()
        };
        let trimmed = label.trim_end();
        // Mermaid 11.16 measures pie legend text via a single SVG `<text>` node's
        // `getBoundingClientRect().width`.
        let w = if trimmed.is_empty() {
            0.0
        } else {
            measurer.measure_svg_text_bounding_client_rect_width_px(trimmed, &legend_style)
        };
        max_legend_width = max_legend_width.max(w);
    }

    let title_width = title
        .map(|title| measurer.measure_svg_text_bounding_client_rect_width_px(title, &title_style))
        .unwrap_or(0.0);

    let base_w: f64 = center * 2.0;
    let legend_extra_width = legend_rect_size + legend_spacing + max_legend_width;
    let centered_legend_x = -max_legend_width / 2.0 - (legend_rect_size + legend_spacing);

    let chart_and_legend_width = match legend_position {
        PieLegendPosition::Top => (base_w + margin).max(1.0),
        PieLegendPosition::Bottom => (base_w + margin).max(1.0),
        PieLegendPosition::Left => (base_w + margin + legend_extra_width).max(1.0),
        PieLegendPosition::Center => (base_w + margin).max(1.0),
        PieLegendPosition::Right => {
            if model.sections.is_empty() {
                f64::NEG_INFINITY
            } else {
                (base_w + margin + legend_extra_width).max(1.0)
            }
        }
    };

    let height = match legend_position {
        PieLegendPosition::Top | PieLegendPosition::Bottom => {
            (base_w + total_legend_height).max(1.0)
        }
        PieLegendPosition::Left | PieLegendPosition::Center | PieLegendPosition::Right => {
            base_w.max(1.0)
        }
    };

    let legend_x = match legend_position {
        PieLegendPosition::Top | PieLegendPosition::Bottom | PieLegendPosition::Center => {
            centered_legend_x
        }
        PieLegendPosition::Left => -radius - (legend_rect_size + legend_spacing),
        PieLegendPosition::Right => 12.0 * legend_rect_size,
    };

    let title_left = base_w / 2.0 - title_width / 2.0;
    let title_right = base_w / 2.0 + title_width / 2.0;
    let min_x = title_left.min(0.0);
    let max_x = chart_and_legend_width.max(title_right);
    let max_x = if model.sections.is_empty() {
        max_x.max(base_w)
    } else {
        max_x
    };

    Ok(PieDiagramLayout {
        bounds: Some(Bounds {
            min_x,
            min_y: 0.0,
            max_x,
            max_y: height,
        }),
        title: title.map(str::to_owned),
        center_x: center,
        center_y: center,
        radius,
        outer_radius,
        legend_x,
        legend_start_y,
        legend_step_y,
        slices,
        legend_items,
    })
}

#[cfg(test)]
mod tests {
    use crate::text::{TextMeasurer, TextMetrics, TextStyle};
    use merman_core::diagrams::pie::{PieDiagramRenderModel, PieRenderSection};
    use std::sync::Mutex;

    #[derive(Default)]
    struct RecordingBoundingClientRectMeasurer {
        calls: Mutex<Vec<String>>,
    }

    impl TextMeasurer for RecordingBoundingClientRectMeasurer {
        fn measure(&self, _text: &str, _style: &TextStyle) -> TextMetrics {
            panic!("pie legend and title must use the source-backed browser primitive")
        }

        fn measure_svg_simple_text_bbox_width_px(&self, _text: &str, _style: &TextStyle) -> f64 {
            panic!("getBBox must not stand in for getBoundingClientRect")
        }

        fn measure_svg_text_bounding_client_rect_width_px(
            &self,
            text: &str,
            _style: &TextStyle,
        ) -> f64 {
            self.calls
                .lock()
                .expect("measurement calls")
                .push(text.to_string());
            match text {
                "Legend" => 123.456_789,
                "Title" => 1_000.123_456,
                "  Title  " => 1_100.123_456,
                "\u{a0}Title\u{a0}" => 1_200.123_456,
                other => panic!("unexpected measurement: {other}"),
            }
        }
    }

    #[test]
    fn pie_legend_geometry_constants_match_mermaid() {
        assert_eq!(super::PIE_LEGEND_RECT_SIZE_PX, 18.0);
        assert_eq!(super::PIE_LEGEND_SPACING_PX, 4.0);
    }

    #[test]
    fn pie_legend_and_title_use_exact_bounding_client_rect_results() {
        let measurer = RecordingBoundingClientRectMeasurer::default();
        let mut legend_model = PieDiagramRenderModel::default();
        legend_model.sections = vec![PieRenderSection {
            label: "Legend".to_string(),
            value: 1.0,
        }];
        let legend_layout =
            super::layout_pie_diagram_typed(&legend_model, None, &serde_json::json!({}), &measurer)
                .expect("legend layout");
        let legend_max_x = legend_layout.bounds.expect("legend bounds").max_x;
        assert!((legend_max_x - (512.0 + 123.456_789)).abs() < 1e-12);

        let mut title_model = PieDiagramRenderModel::default();
        title_model.title = Some("Title".to_string());
        let title_layout = super::layout_pie_diagram_typed(
            &title_model,
            None,
            &serde_json::json!({"pie": {"legendPosition": "top"}}),
            &measurer,
        )
        .expect("title layout");
        let title_max_x = title_layout.bounds.expect("title bounds").max_x;
        assert!((title_max_x - (225.0 + 1_000.123_456 / 2.0)).abs() < 1e-12);
        assert_eq!(
            *measurer.calls.lock().expect("measurement calls"),
            ["Legend".to_string(), "Title".to_string()]
        );
    }

    #[test]
    fn empty_pie_bounds_still_contain_the_painted_circle() {
        let measurer = RecordingBoundingClientRectMeasurer::default();
        for config in [
            serde_json::json!({}),
            serde_json::json!({"pie": {"legendPosition": "top"}}),
        ] {
            let layout = super::layout_pie_diagram_typed(
                &PieDiagramRenderModel::default(),
                None,
                &config,
                &measurer,
            )
            .expect("empty pie layout");
            let bounds = layout.bounds.expect("empty pie bounds");
            assert!(bounds.min_x <= layout.center_x - layout.outer_radius);
            assert!(bounds.max_x >= layout.center_x * 2.0);
        }
    }

    #[test]
    fn pie_frontmatter_title_preserves_boundary_whitespace_for_layout_measurement() {
        for (title, expected_max_x) in [
            ("  Title  ", 225.0 + 1_100.123_456 / 2.0),
            ("\u{a0}Title\u{a0}", 225.0 + 1_200.123_456 / 2.0),
        ] {
            let measurer = RecordingBoundingClientRectMeasurer::default();
            let layout = super::layout_pie_diagram_typed(
                &PieDiagramRenderModel::default(),
                Some(title),
                &serde_json::json!({"pie": {"legendPosition": "top"}}),
                &measurer,
            )
            .expect("title layout");

            assert_eq!(layout.title.as_deref(), Some(title));
            let max_x = layout.bounds.expect("title bounds").max_x;
            assert!((max_x - expected_max_x).abs() < 1e-12);
            assert_eq!(
                *measurer.calls.lock().expect("measurement calls"),
                [title.to_string()]
            );
        }
    }
}