merman-render 0.8.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
use super::bounds::recompute_nested_group_bounds;
use super::config::LR_TITLE_BAND_SIZE;
use super::geometry::Rect;
pub(super) use super::work_budget::{LayoutWorkBudget, unordered_pair_count};
use super::working::{WorkingLayout, WorkingNodeKind};
use crate::Result;
use crate::model::{LayoutPoint, SwimlaneDirection, SwimlaneTitleRect};
use std::collections::HashMap;

mod detour_simplification;
mod endpoint_clip;
pub(super) mod geometry;
mod label_anchoring;
mod materialized_geometry;
mod port_swap;
mod shared_track_nudging;
mod sibling_shared_face_routing;
mod terminal_stub;
#[cfg(test)]
mod validation;

use geometry::{orthogonalize_polyline, simplify_polyline};

fn layout_content_ids(layout: &WorkingLayout) -> Vec<String> {
    layout
        .nodes
        .values()
        .filter(|node| !node.is_group() && node.kind != WorkingNodeKind::Dummy)
        .map(|node| node.id.clone())
        .collect()
}

fn mirror_axis(layout: &mut WorkingLayout, horizontal: bool) {
    let content_ids = layout_content_ids(layout);
    let values: Vec<f64> = content_ids
        .iter()
        .filter_map(|id| layout.nodes.get(id))
        .map(|node| if horizontal { node.x } else { node.y })
        .collect();
    let Some(minimum) = values.iter().copied().reduce(f64::min) else {
        return;
    };
    let Some(maximum) = values.iter().copied().reduce(f64::max) else {
        return;
    };
    let mirror = |value: f64| minimum + maximum - value;
    for node in layout
        .nodes
        .values_mut()
        .filter(|node| node.kind != WorkingNodeKind::Dummy)
    {
        if horizontal {
            node.x = mirror(node.x);
            if let Some(rect) = &mut node.title_rect {
                let left = mirror(rect.right);
                let right = mirror(rect.left);
                rect.left = left;
                rect.right = right;
            }
        } else {
            node.y = mirror(node.y);
            if let Some(rect) = &mut node.title_rect {
                let top = mirror(rect.bottom);
                let bottom = mirror(rect.top);
                rect.top = top;
                rect.bottom = bottom;
            }
        }
    }
    for edge in &mut layout.original_edges {
        for point in &mut edge.points {
            if horizontal {
                point.x = mirror(point.x);
            } else {
                point.y = mirror(point.y);
            }
        }
    }
}

fn transform_lr(layout: &mut WorkingLayout) {
    let content_ids = layout_content_ids(layout);
    if content_ids.is_empty() {
        return;
    }
    let min_x = content_ids
        .iter()
        .map(|id| layout.nodes[id].x)
        .fold(f64::INFINITY, f64::min);
    let min_y = content_ids
        .iter()
        .map(|id| layout.nodes[id].y)
        .fold(f64::INFINITY, f64::min);
    let (total_width, total_height) = content_ids.iter().fold((0.0, 0.0), |acc, id| {
        let node = &layout.nodes[id];
        (acc.0 + node.width, acc.1 + node.height)
    });
    let average_width = total_width / content_ids.len() as f64;
    let average_height = total_height / content_ids.len() as f64;
    let horizontal_scale = if average_height > 0.0 {
        (average_width / average_height).max(1.0)
    } else {
        1.0
    };
    let transform = |point: &LayoutPoint| LayoutPoint {
        x: (point.y - min_y) * horizontal_scale + LR_TITLE_BAND_SIZE,
        y: point.x - min_x,
    };

    for id in &content_ids {
        if let Some(node) = layout.nodes.get_mut(id) {
            let transformed = transform(&LayoutPoint {
                x: node.x,
                y: node.y,
            });
            node.x = transformed.x;
            node.y = transformed.y;
        }
    }
    for edge in &mut layout.original_edges {
        for point in &mut edge.points {
            *point = transform(point);
        }
    }

    recompute_nested_group_bounds(layout);
    let top_lane_ids: Vec<String> = layout
        .nodes
        .values()
        .filter(|node| node.is_group() && node.parent_id.is_none())
        .map(|node| node.id.clone())
        .collect();
    let mut children_by_lane: HashMap<String, Vec<String>> = HashMap::new();
    for id in &content_ids {
        let Some(lane_id) = layout.nodes[id].top_lane_id.clone() else {
            continue;
        };
        children_by_lane
            .entry(lane_id)
            .or_default()
            .push(id.clone());
    }

    let max_padding = top_lane_ids
        .iter()
        .map(|id| layout.nodes[id].padding)
        .fold(0.0, f64::max);
    let mut lane_bounds = Vec::new();
    let mut global_min_x = f64::INFINITY;
    let mut global_max_x = f64::NEG_INFINITY;
    for id in &top_lane_ids {
        let children = children_by_lane.get(id).cloned().unwrap_or_default();
        let mut bounds: Option<Rect> = None;
        for child_id in children {
            let child = &layout.nodes[&child_id];
            let rect = Rect::from_center(child.x, child.y, child.width, child.height);
            if let Some(current) = &mut bounds {
                current.union(rect);
            } else {
                bounds = Some(rect);
            }
        }
        let Some(bounds) = bounds else {
            continue;
        };
        global_min_x = global_min_x.min(bounds.left);
        global_max_x = global_max_x.max(bounds.right);
        lane_bounds.push((
            id.clone(),
            bounds.top,
            bounds.bottom,
            (bounds.top + bounds.bottom) / 2.0,
        ));
    }
    if !global_min_x.is_finite() || !global_max_x.is_finite() {
        return;
    }

    let horizontal_margin = max_padding.max(10.0);
    let body_width = global_max_x - global_min_x + 2.0 * horizontal_margin;
    let lane_width = LR_TITLE_BAND_SIZE + body_width;
    let body_center = (global_min_x + global_max_x) / 2.0;
    let body_left = body_center - body_width / 2.0;
    let lane_left = body_left - LR_TITLE_BAND_SIZE;
    let center_x = lane_left + lane_width / 2.0;
    let vertical_margin = max_padding.max(LR_TITLE_BAND_SIZE);
    lane_bounds.sort_by(|left, right| left.3.total_cmp(&right.3));

    for index in 0..lane_bounds.len() {
        let (id, content_top, content_bottom, _) = &lane_bounds[index];
        let top = if index == 0 {
            content_top - vertical_margin
        } else {
            (lane_bounds[index - 1].2 + content_top) / 2.0
        };
        let bottom = if index + 1 == lane_bounds.len() {
            content_bottom + vertical_margin
        } else {
            (content_bottom + lane_bounds[index + 1].1) / 2.0
        };
        if let Some(lane) = layout.nodes.get_mut(id) {
            lane.x = center_x;
            lane.y = (top + bottom) / 2.0;
            lane.width = lane_width;
            lane.height = (bottom - top).max(0.0);
            lane.content_top = Some(*content_top);
            lane.title_rect = Some(SwimlaneTitleRect {
                left: lane_left,
                right: lane_left + LR_TITLE_BAND_SIZE,
                top,
                bottom,
            });
        }
    }
}

fn finalize_rendered_edges(
    layout: &mut WorkingLayout,
    work_budget: &mut LayoutWorkBudget,
) -> Result<()> {
    materialized_geometry::resolve_rendered_orthogonal_crossings(layout, work_budget)?;
    materialized_geometry::reassign_crossing_external_rail_channels(layout, work_budget)?;
    materialized_geometry::shortcut_redundant_orthogonal_jogs(layout, work_budget)?;
    label_anchoring::anchor_labels_to_polyline(layout, work_budget)?;
    endpoint_clip::prepare_edge_endpoints_for_renderer(layout);
    materialized_geometry::lift_obstacle_hugging_same_side_rails(layout, work_budget)?;
    label_anchoring::anchor_labels_to_polyline(layout, work_budget)?;
    endpoint_clip::prepare_edge_endpoints_for_renderer(layout);
    Ok(())
}

fn post_process_staged(
    layout: &mut WorkingLayout,
    work_budget: &mut LayoutWorkBudget,
) -> Result<()> {
    match layout.direction {
        SwimlaneDirection::Tb => {}
        SwimlaneDirection::Bt => mirror_axis(layout, false),
        SwimlaneDirection::Lr => transform_lr(layout),
        SwimlaneDirection::Rl => {
            transform_lr(layout);
            mirror_axis(layout, true);
        }
    }
    for edge in &mut layout.original_edges {
        edge.points = simplify_polyline(&orthogonalize_polyline(&edge.points));
    }
    detour_simplification::simplify_detoured_edges(layout, work_budget)?;
    sibling_shared_face_routing::straighten_collinear_sibling_detours(layout, work_budget)?;
    port_swap::port_swap_to_l_shape(layout, work_budget)?;
    label_anchoring::anchor_labels_to_polyline(layout, work_budget)?;
    endpoint_clip::clip_edge_endpoints_to_node_boundaries(layout);
    terminal_stub::collapse_short_terminal_stub(layout, work_budget)?;
    shared_track_nudging::nudge_shared_interior_subpaths(layout, work_budget)?;
    materialized_geometry::separate_shared_rendered_terminal_lanes(layout, work_budget)?;
    materialized_geometry::collapse_redundant_rectangular_doglegs(layout, work_budget)?;
    materialized_geometry::lift_obstacle_hugging_same_side_rails(layout, work_budget)?;
    materialized_geometry::swap_destination_terminal_tails_to_reduce_crossings(
        layout,
        work_budget,
    )?;

    finalize_rendered_edges(layout, work_budget)?;
    shared_track_nudging::nudge_shared_interior_subpaths(layout, work_budget)?;
    finalize_rendered_edges(layout, work_budget)?;

    for _ in 0..2 {
        materialized_geometry::lift_top_lane_title_bands_above_rails(layout, work_budget)?;
        materialized_geometry::shift_left_lane_title_bands_left_of_rails(layout, work_budget)?;
    }
    Ok(())
}

fn post_process_snapshot_ticks(layout: &WorkingLayout) -> usize {
    let edge_points = layout
        .graph_edges
        .iter()
        .chain(&layout.original_edges)
        .map(|edge| edge.points.len())
        .fold(0usize, usize::saturating_add);
    layout
        .nodes
        .len()
        .saturating_add(layout.graph_edges.len())
        .saturating_add(layout.original_edges.len())
        .saturating_add(layout.top_lane_order.len())
        .saturating_add(edge_points)
}

pub(super) fn post_process(
    layout: &mut WorkingLayout,
    work_budget: &mut LayoutWorkBudget,
) -> Result<()> {
    // Reserve the snapshot and initial transform cost before cloning any geometry.
    work_budget.charge(post_process_snapshot_ticks(layout))?;
    work_budget.finish()?;

    // One transaction-wide snapshot keeps resource failures atomic without
    // cloning the complete layout again inside each finalization pass.
    let mut staged = layout.clone();
    post_process_staged(&mut staged, work_budget)?;
    work_budget.finish()?;
    *layout = staged;
    Ok(())
}

#[cfg(test)]
mod resource_tests {
    use super::{LayoutWorkBudget, finalize_rendered_edges, post_process};
    use crate::model::{LayoutPoint, SwimlaneDirection};
    use crate::resources::{RenderResourcePolicy, ResourceLimitId};
    use crate::swimlane::working::{WorkingEdge, WorkingLayout, WorkingNode, WorkingNodeKind};
    use indexmap::IndexMap;

    fn policy(max: usize) -> RenderResourcePolicy {
        RenderResourcePolicy::unbounded_for_trusted_input()
            .with_limit(ResourceLimitId::MaxLayoutWorkUnits, max)
            .unwrap()
    }

    #[test]
    fn repeated_finalizers_share_one_cumulative_operation_budget() {
        let nodes = [
            ("A", -20.0, 0.0),
            ("B", 20.0, 0.0),
            ("C", -20.0, 20.0),
            ("D", 20.0, 20.0),
        ]
        .into_iter()
        .map(|(id, x, y)| {
            (
                id.to_string(),
                WorkingNode {
                    id: id.to_string(),
                    label: id.to_string(),
                    label_type: "text".to_string(),
                    shape: "rect".to_string(),
                    kind: WorkingNodeKind::Content,
                    parent_id: None,
                    top_lane_id: None,
                    requested_dir: None,
                    padding: 0.0,
                    x,
                    y,
                    width: 10.0,
                    height: 10.0,
                    label_width: 10.0,
                    label_height: 10.0,
                    layer: 0,
                    order: 0,
                    content_top: None,
                    title_rect: None,
                },
            )
        })
        .collect::<IndexMap<_, _>>();
        let edges = [("A_B", "A", "B", 0.0), ("C_D", "C", "D", 20.0)]
            .into_iter()
            .map(|(id, from, to, y)| WorkingEdge {
                id: id.to_string(),
                from: from.to_string(),
                to: to.to_string(),
                reference_id: id.to_string(),
                label_node_id: None,
                reversed_for_layout: false,
                points: vec![LayoutPoint { x: -15.0, y }, LayoutPoint { x: 15.0, y }],
            })
            .collect();
        let mut layout = WorkingLayout {
            direction: SwimlaneDirection::Tb,
            nodes,
            graph_edges: Vec::new(),
            original_edges: edges,
            top_lane_order: Vec::new(),
        };
        let mut budget = LayoutWorkBudget::new(policy(60), 0).unwrap();

        finalize_rendered_edges(&mut layout, &mut budget).unwrap();
        assert_eq!(budget.used(), 60);
        let before_second = layout
            .original_edges
            .iter()
            .map(|edge| {
                edge.points
                    .iter()
                    .map(|point| (point.x, point.y))
                    .collect::<Vec<_>>()
            })
            .collect::<Vec<_>>();
        let error = finalize_rendered_edges(&mut layout, &mut budget).unwrap_err();
        let crate::Error::ResourceLimitExceeded(error) = error else {
            panic!("expected max_layout_work_units resource limit error");
        };
        assert_eq!(error.actual, 64);
        assert_eq!(error.max, 60);
        assert_eq!(budget.used(), 60);
        assert_eq!(
            layout
                .original_edges
                .iter()
                .map(|edge| {
                    edge.points
                        .iter()
                        .map(|point| (point.x, point.y))
                        .collect::<Vec<_>>()
                })
                .collect::<Vec<_>>(),
            before_second
        );
    }

    #[test]
    fn post_process_budget_error_does_not_commit_staged_geometry() {
        let nodes = [("A", 0.0), ("B", 20.0)]
            .into_iter()
            .map(|(id, y)| {
                (
                    id.to_string(),
                    WorkingNode {
                        id: id.to_string(),
                        label: id.to_string(),
                        label_type: "text".to_string(),
                        shape: "rect".to_string(),
                        kind: WorkingNodeKind::Content,
                        parent_id: None,
                        top_lane_id: None,
                        requested_dir: None,
                        padding: 0.0,
                        x: 0.0,
                        y,
                        width: 10.0,
                        height: 10.0,
                        label_width: 10.0,
                        label_height: 10.0,
                        layer: 0,
                        order: 0,
                        content_top: None,
                        title_rect: None,
                    },
                )
            })
            .collect::<IndexMap<_, _>>();
        let mut layout = WorkingLayout {
            direction: SwimlaneDirection::Bt,
            nodes,
            graph_edges: Vec::new(),
            original_edges: vec![WorkingEdge {
                id: "A_B".to_string(),
                from: "A".to_string(),
                to: "B".to_string(),
                reference_id: "A_B".to_string(),
                label_node_id: None,
                reversed_for_layout: false,
                points: vec![
                    LayoutPoint { x: 0.0, y: 5.0 },
                    LayoutPoint { x: 0.0, y: 15.0 },
                ],
            }],
            top_lane_order: Vec::new(),
        };
        let before_nodes = layout
            .nodes
            .values()
            .map(|node| (node.id.clone(), node.x, node.y))
            .collect::<Vec<_>>();
        let before_edges = layout
            .original_edges
            .iter()
            .map(|edge| {
                edge.points
                    .iter()
                    .map(|point| (point.x, point.y))
                    .collect::<Vec<_>>()
            })
            .collect::<Vec<_>>();
        // Model and routing work may consume the operation budget before post-processing starts.
        // Start at the legal positive boundary so snapshot cost is rejected before cloning.
        let mut budget = LayoutWorkBudget::new(policy(1), 1).unwrap();

        let error = post_process(&mut layout, &mut budget).unwrap_err();
        let crate::Error::ResourceLimitExceeded(error) = error else {
            panic!("expected max_layout_work_units resource limit error");
        };
        assert_eq!(budget.used(), 1);
        assert_eq!(error.actual, 6);
        assert_eq!(error.max, 1);
        assert_eq!(
            layout
                .nodes
                .values()
                .map(|node| (node.id.clone(), node.x, node.y))
                .collect::<Vec<_>>(),
            before_nodes
        );
        assert_eq!(
            layout
                .original_edges
                .iter()
                .map(|edge| {
                    edge.points
                        .iter()
                        .map(|point| (point.x, point.y))
                        .collect::<Vec<_>>()
                })
                .collect::<Vec<_>>(),
            before_edges
        );
    }
}