mmdflux 2.2.0

Render Mermaid diagrams as Unicode text, ASCII, SVG, and MMDS JSON.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
use super::super::{Point, Rect};
use super::endpoints::{clip_points_to_rect_end, clip_points_to_rect_start, intersect_svg_node};
use super::{
    SegmentAxis, dedup_consecutive_svg_points, point_inside_rect, points_approx_equal,
    points_are_axis_aligned, segment_axis, vectors_share_ray,
};
use crate::graph::geometry::GraphGeometry;
use crate::graph::{Direction, Edge, Graph, Shape, Stroke};

pub(super) fn enforce_basis_visible_terminal_stems(
    points: &[Point],
    min_stem: f64,
    compact_caps: bool,
) -> Vec<Point> {
    let mut adjusted = dedup_consecutive_svg_points(points);
    if adjusted.len() < 2 || min_stem <= 0.0 {
        return adjusted;
    }

    if compact_caps {
        if let Some((seg_idx, cap)) = start_cap_point_on_existing_run(&adjusted, min_stem) {
            adjusted = rebuild_with_start_cap(&adjusted, seg_idx, cap);
        }
        if let Some((seg_idx, cap)) = end_cap_point_on_existing_run(&adjusted, min_stem) {
            adjusted = rebuild_with_end_cap(&adjusted, seg_idx, cap);
        }
    } else {
        insert_basis_start_cap_if_needed(&mut adjusted, min_stem);
        insert_basis_end_cap_if_needed(&mut adjusted, min_stem);
    }
    dedup_consecutive_svg_points(&adjusted)
}

pub(super) fn clamp_basis_edge_endpoints_to_boundaries(
    diagram: &Graph,
    geom: &GraphGeometry,
    edge: &Edge,
    points: &[Point],
) -> Vec<Point> {
    if points.len() < 2 {
        return points.to_vec();
    }

    let mut clamped = points.to_vec();
    if let Some(sg_id) = edge.from_subgraph.as_ref()
        && let Some(sg_geom) = geom.subgraphs.get(sg_id)
    {
        clamped = clip_points_to_rect_start(&clamped, &sg_geom.rect);
    } else if let (Some(node), Some(node_geom)) =
        (diagram.nodes.get(&edge.from), geom.nodes.get(&edge.from))
    {
        let from_rect: Rect = node_geom.rect;
        if !matches!(node.shape, Shape::Diamond | Shape::Hexagon)
            && point_inside_rect(&from_rect, clamped[0])
        {
            clamped[0] = intersect_svg_node(&from_rect, clamped[1], node.shape);
        }
    }

    if clamped.len() < 2 {
        return clamped;
    }

    if let Some(sg_id) = edge.to_subgraph.as_ref()
        && let Some(sg_geom) = geom.subgraphs.get(sg_id)
    {
        clamped = clip_points_to_rect_end(&clamped, &sg_geom.rect);
    } else if let (Some(node), Some(node_geom)) =
        (diagram.nodes.get(&edge.to), geom.nodes.get(&edge.to))
    {
        let to_rect: Rect = node_geom.rect;
        let last = clamped.len() - 1;
        if !matches!(node.shape, Shape::Diamond | Shape::Hexagon)
            && point_inside_rect(&to_rect, clamped[last])
        {
            clamped[last] = intersect_svg_node(&to_rect, clamped[last - 1], node.shape);
        }
    }

    dedup_consecutive_svg_points(&clamped)
}

pub(super) fn edge_rank_span_for_svg(geom: &GraphGeometry, edge: &Edge) -> Option<usize> {
    let crate::graph::geometry::EngineHints::Layered(hints) = geom.engine_hints.as_ref()?;
    let src_rank = *hints.node_ranks.get(&edge.from)?;
    let dst_rank = *hints.node_ranks.get(&edge.to)?;
    Some(src_rank.abs_diff(dst_rank) as usize)
}

pub(super) fn adapt_basis_anchor_points(
    points: &[Point],
    edge: &Edge,
    geom: &GraphGeometry,
    direction: Direction,
    is_backward: bool,
) -> Vec<Point> {
    if points.len() <= 2 {
        return points.to_vec();
    }
    // Self-loop paths need all their points to form the loop shape;
    // the rank-span heuristics below would collapse them to a line.
    if edge.from == edge.to {
        return dedup_consecutive_svg_points(points);
    }
    if !points_are_axis_aligned(points) {
        return dedup_consecutive_svg_points(points);
    }

    let rank_span =
        edge_rank_span_for_svg(geom, edge).unwrap_or_else(|| edge.minlen.max(1) as usize);
    let starts_on_secondary_axis = first_segment_is_secondary_axis(points, direction);
    let compact_backward_return = is_backward
        && rank_span <= 1
        && edge.minlen <= 1
        && is_primary_secondary_primary_return(points, direction);
    if compact_backward_return {
        return dedup_consecutive_svg_points(&[points[0], points[2], points[3]]);
    }
    let preserve_extended_route = is_backward || rank_span >= 2 || edge.minlen > 1;

    let adapted = if preserve_extended_route {
        if points.len() <= 4 {
            points.to_vec()
        } else {
            vec![
                points[0],
                points[1],
                points[points.len() - 2],
                points[points.len() - 1],
            ]
        }
    } else if starts_on_secondary_axis && ends_on_secondary_axis(points, direction) {
        vec![
            points[0],
            points[points.len() - 2],
            points[points.len() - 1],
        ]
    } else if starts_on_secondary_axis {
        vec![points[0], points[1], points[points.len() - 1]]
    } else {
        vec![points[0], points[points.len() - 1]]
    };

    dedup_consecutive_svg_points(&adapted)
}

pub(super) fn apply_reciprocal_lane_offsets(
    start: Point,
    end: Point,
    direction: Direction,
    curve_sign: f64,
    source_rect: Rect,
    target_rect: Rect,
) -> (Point, Point) {
    let mut adjusted_start = start;
    let mut adjusted_end = end;
    let source_upper = (source_rect.height * 0.18).clamp(8.0, 14.0);
    let target_upper = (target_rect.height * 0.18).clamp(8.0, 14.0);
    let source_lower = (source_rect.height * 0.26).clamp(10.0, 18.0);
    let target_lower = (target_rect.height * 0.26).clamp(10.0, 18.0);
    let source_lane = if curve_sign < 0.0 {
        source_upper
    } else {
        source_lower
    };
    let target_lane = if curve_sign < 0.0 {
        target_upper
    } else {
        target_lower
    };

    match direction {
        Direction::LeftRight | Direction::RightLeft => {
            let source_center = source_rect.y + source_rect.height / 2.0;
            let target_center = target_rect.y + target_rect.height / 2.0;
            adjusted_start.y = if curve_sign < 0.0 {
                source_center - source_lane
            } else {
                source_center + source_lane
            }
            .clamp(
                source_rect.y + 1.0,
                source_rect.y + source_rect.height - 1.0,
            );
            adjusted_end.y = if curve_sign < 0.0 {
                target_center - target_lane
            } else {
                target_center + target_lane
            }
            .clamp(
                target_rect.y + 1.0,
                target_rect.y + target_rect.height - 1.0,
            );
        }
        Direction::TopDown | Direction::BottomTop => {
            let source_upper = (source_rect.width * 0.18).clamp(8.0, 14.0);
            let target_upper = (target_rect.width * 0.18).clamp(8.0, 14.0);
            let source_lower = (source_rect.width * 0.26).clamp(10.0, 18.0);
            let target_lower = (target_rect.width * 0.26).clamp(10.0, 18.0);
            let source_lane = if curve_sign < 0.0 {
                source_upper
            } else {
                source_lower
            };
            let target_lane = if curve_sign < 0.0 {
                target_upper
            } else {
                target_lower
            };
            let source_center = source_rect.x + source_rect.width / 2.0;
            let target_center = target_rect.x + target_rect.width / 2.0;
            adjusted_start.x = if curve_sign < 0.0 {
                source_center - source_lane
            } else {
                source_center + source_lane
            }
            .clamp(source_rect.x + 1.0, source_rect.x + source_rect.width - 1.0);
            adjusted_end.x = if curve_sign < 0.0 {
                target_center - target_lane
            } else {
                target_center + target_lane
            }
            .clamp(target_rect.x + 1.0, target_rect.x + target_rect.width - 1.0);
        }
    }

    (adjusted_start, adjusted_end)
}

pub(super) fn collapse_primary_face_fan_channel_for_curved(
    geom: &GraphGeometry,
    edge: &Edge,
    direction: Direction,
    points: &[Point],
    center_eps: f64,
) -> Vec<Point> {
    const MARKER_PULLBACK_TOLERANCE: f64 = 6.0;
    const MIN_STEM_FOR_COLLAPSE: f64 = 8.0;
    const MAX_STEM_FOR_COLLAPSE: f64 = 18.0;
    const MIN_TERMINAL_STEM_FOR_COLLAPSE: f64 = 10.0;
    const MAX_TERMINAL_STEM_FOR_COLLAPSE: f64 = 22.0;
    const TERMINAL_STEM_BIAS: f64 = 3.0;
    const MIN_CHANNEL_SPAN: f64 = 4.0;

    if points.len() != 4 {
        return points.to_vec();
    }

    let Some(target_geom) = geom.nodes.get(&edge.to) else {
        return points.to_vec();
    };
    let target_rect: Rect = target_geom.rect;
    let mut out = points.to_vec();
    match direction {
        Direction::TopDown | Direction::BottomTop => {
            let first_vertical = (out[0].x - out[1].x).abs() <= center_eps
                && (out[0].y - out[1].y).abs() > center_eps;
            let middle_horizontal = (out[1].y - out[2].y).abs() <= center_eps
                && (out[1].x - out[2].x).abs() > center_eps;
            let terminal_vertical = (out[2].x - out[3].x).abs() <= center_eps
                && (out[2].y - out[3].y).abs() > center_eps;
            if !(first_vertical && middle_horizontal && terminal_vertical) {
                return points.to_vec();
            }

            let has_lateral_offset = (out[0].x - out[3].x).abs() > center_eps;
            let target_is_primary_face = match direction {
                Direction::TopDown => out[3].y <= target_rect.y + MARKER_PULLBACK_TOLERANCE,
                Direction::BottomTop => {
                    out[3].y >= target_rect.y + target_rect.height - MARKER_PULLBACK_TOLERANCE
                }
                _ => false,
            };
            if has_lateral_offset && target_is_primary_face {
                let delta = out[3].y - out[0].y;
                if delta.abs()
                    > MIN_STEM_FOR_COLLAPSE + MIN_TERMINAL_STEM_FOR_COLLAPSE + MIN_CHANNEL_SPAN
                {
                    let source_stem =
                        (delta.abs() * 0.28).clamp(MIN_STEM_FOR_COLLAPSE, MAX_STEM_FOR_COLLAPSE);
                    let max_terminal_stem = delta.abs() - source_stem - MIN_CHANNEL_SPAN;
                    if max_terminal_stem < MIN_TERMINAL_STEM_FOR_COLLAPSE {
                        return points.to_vec();
                    }
                    let terminal_stem = (delta.abs() * 0.28 + TERMINAL_STEM_BIAS)
                        .clamp(
                            MIN_TERMINAL_STEM_FOR_COLLAPSE,
                            MAX_TERMINAL_STEM_FOR_COLLAPSE,
                        )
                        .min(max_terminal_stem);
                    let dir = if delta >= 0.0 { 1.0 } else { -1.0 };
                    out[1].y = out[0].y + (dir * source_stem);
                    out[2].y = out[3].y - (dir * terminal_stem);
                }
            }
        }
        Direction::LeftRight | Direction::RightLeft => {
            let first_horizontal = (out[0].y - out[1].y).abs() <= center_eps
                && (out[0].x - out[1].x).abs() > center_eps;
            let middle_vertical = (out[1].x - out[2].x).abs() <= center_eps
                && (out[1].y - out[2].y).abs() > center_eps;
            let terminal_horizontal = (out[2].y - out[3].y).abs() <= center_eps
                && (out[2].x - out[3].x).abs() > center_eps;
            if !(first_horizontal && middle_vertical && terminal_horizontal) {
                return points.to_vec();
            }

            let has_lateral_offset = (out[0].y - out[3].y).abs() > center_eps;
            let target_is_primary_face = match direction {
                Direction::LeftRight => out[3].x <= target_rect.x + MARKER_PULLBACK_TOLERANCE,
                Direction::RightLeft => {
                    out[3].x >= target_rect.x + target_rect.width - MARKER_PULLBACK_TOLERANCE
                }
                _ => false,
            };
            if has_lateral_offset && target_is_primary_face {
                let delta = out[3].x - out[0].x;
                if delta.abs()
                    > MIN_STEM_FOR_COLLAPSE + MIN_TERMINAL_STEM_FOR_COLLAPSE + MIN_CHANNEL_SPAN
                {
                    let source_stem =
                        (delta.abs() * 0.28).clamp(MIN_STEM_FOR_COLLAPSE, MAX_STEM_FOR_COLLAPSE);
                    let max_terminal_stem = delta.abs() - source_stem - MIN_CHANNEL_SPAN;
                    if max_terminal_stem < MIN_TERMINAL_STEM_FOR_COLLAPSE {
                        return points.to_vec();
                    }
                    let terminal_stem = (delta.abs() * 0.28 + TERMINAL_STEM_BIAS)
                        .clamp(
                            MIN_TERMINAL_STEM_FOR_COLLAPSE,
                            MAX_TERMINAL_STEM_FOR_COLLAPSE,
                        )
                        .min(max_terminal_stem);
                    let dir = if delta >= 0.0 { 1.0 } else { -1.0 };
                    out[1].x = out[0].x + (dir * source_stem);
                    out[2].x = out[3].x - (dir * terminal_stem);
                }
            }
        }
    }

    out
}

pub(super) fn synthesize_bezier_control_points(
    start: Point,
    end: Point,
    direction: Direction,
) -> (Point, Point) {
    match direction {
        Direction::TopDown | Direction::BottomTop => {
            let dy = end.y - start.y;
            (
                Point {
                    x: start.x,
                    y: start.y + dy / 3.0,
                },
                Point {
                    x: end.x,
                    y: start.y + 2.0 * dy / 3.0,
                },
            )
        }
        Direction::LeftRight | Direction::RightLeft => {
            let dx = end.x - start.x;
            (
                Point {
                    x: start.x + dx / 3.0,
                    y: start.y,
                },
                Point {
                    x: start.x + 2.0 * dx / 3.0,
                    y: end.y,
                },
            )
        }
    }
}

pub(super) fn synthesize_reciprocal_bezier_control_points(
    start: Point,
    end: Point,
    direction: Direction,
    curve_sign: f64,
) -> (Point, Point) {
    match direction {
        Direction::TopDown | Direction::BottomTop => {
            let dy = end.y - start.y;
            let bow = (dy.abs() * 0.25).clamp(12.0, 28.0) * curve_sign;
            (
                Point {
                    x: start.x + bow,
                    y: start.y + dy / 3.0,
                },
                Point {
                    x: end.x + bow,
                    y: start.y + 2.0 * dy / 3.0,
                },
            )
        }
        Direction::LeftRight | Direction::RightLeft => {
            let dx = end.x - start.x;
            let bow = (dx.abs() * 0.25).clamp(12.0, 28.0) * curve_sign;
            (
                Point {
                    x: start.x + dx / 3.0,
                    y: start.y + bow,
                },
                Point {
                    x: start.x + 2.0 * dx / 3.0,
                    y: end.y + bow,
                },
            )
        }
    }
}

fn insert_basis_start_cap_if_needed(points: &mut Vec<Point>, min_stem: f64) {
    const EPS: f64 = 1e-6;
    if points.len() < 2 || min_stem <= 0.0 {
        return;
    }

    let base_vec = Point {
        x: points[1].x - points[0].x,
        y: points[1].y - points[0].y,
    };
    let first_segment_len = (base_vec.x * base_vec.x + base_vec.y * base_vec.y).sqrt();
    if first_segment_len <= EPS || first_segment_len + EPS >= min_stem {
        return;
    }

    let mut traversed = 0.0;
    for seg_idx in 0..(points.len() - 1) {
        let seg_vec = Point {
            x: points[seg_idx + 1].x - points[seg_idx].x,
            y: points[seg_idx + 1].y - points[seg_idx].y,
        };
        let seg_len = (seg_vec.x * seg_vec.x + seg_vec.y * seg_vec.y).sqrt();
        if seg_len <= EPS {
            continue;
        }
        if !vectors_share_ray(base_vec, seg_vec) {
            break;
        }
        if traversed + seg_len + EPS >= min_stem {
            let t = ((min_stem - traversed) / seg_len).clamp(0.0, 1.0);
            if t >= 1.0 - EPS {
                return;
            }
            let cap = Point {
                x: points[seg_idx].x + seg_vec.x * t,
                y: points[seg_idx].y + seg_vec.y * t,
            };
            if !points_approx_equal(cap, points[seg_idx])
                && !points_approx_equal(cap, points[seg_idx + 1])
            {
                points.insert(seg_idx + 1, cap);
            }
            return;
        }
        traversed += seg_len;
    }
}

fn insert_basis_end_cap_if_needed(points: &mut Vec<Point>, min_stem: f64) {
    const EPS: f64 = 1e-6;
    if points.len() < 2 || min_stem <= 0.0 {
        return;
    }

    let n = points.len();
    let base_vec = Point {
        x: points[n - 2].x - points[n - 1].x,
        y: points[n - 2].y - points[n - 1].y,
    };
    let last_segment_len = (base_vec.x * base_vec.x + base_vec.y * base_vec.y).sqrt();
    if last_segment_len <= EPS || last_segment_len + EPS >= min_stem {
        return;
    }

    let mut traversed = 0.0;
    for seg_idx in (0..(points.len() - 1)).rev() {
        let seg_vec = Point {
            x: points[seg_idx].x - points[seg_idx + 1].x,
            y: points[seg_idx].y - points[seg_idx + 1].y,
        };
        let seg_len = (seg_vec.x * seg_vec.x + seg_vec.y * seg_vec.y).sqrt();
        if seg_len <= EPS {
            continue;
        }
        if !vectors_share_ray(base_vec, seg_vec) {
            break;
        }
        if traversed + seg_len + EPS >= min_stem {
            let t = ((min_stem - traversed) / seg_len).clamp(0.0, 1.0);
            if t >= 1.0 - EPS {
                return;
            }
            let cap = Point {
                x: points[seg_idx + 1].x + seg_vec.x * t,
                y: points[seg_idx + 1].y + seg_vec.y * t,
            };
            if !points_approx_equal(cap, points[seg_idx + 1])
                && !points_approx_equal(cap, points[seg_idx])
            {
                points.insert(seg_idx + 1, cap);
            }
            return;
        }
        traversed += seg_len;
    }
}

fn start_cap_point_on_existing_run(points: &[Point], min_stem: f64) -> Option<(usize, Point)> {
    const EPS: f64 = 1e-6;
    if points.len() < 2 || min_stem <= 0.0 {
        return None;
    }

    let base_vec = Point {
        x: points[1].x - points[0].x,
        y: points[1].y - points[0].y,
    };
    let first_segment_len = (base_vec.x * base_vec.x + base_vec.y * base_vec.y).sqrt();
    if first_segment_len <= EPS {
        return None;
    }

    let mut traversed = 0.0;
    let mut last_seg_idx_on_ray = 0usize;
    for seg_idx in 0..(points.len() - 1) {
        let seg_vec = Point {
            x: points[seg_idx + 1].x - points[seg_idx].x,
            y: points[seg_idx + 1].y - points[seg_idx].y,
        };
        let seg_len = (seg_vec.x * seg_vec.x + seg_vec.y * seg_vec.y).sqrt();
        if seg_len <= EPS {
            continue;
        }
        if !vectors_share_ray(base_vec, seg_vec) {
            break;
        }
        last_seg_idx_on_ray = seg_idx;
        if traversed + seg_len + EPS >= min_stem {
            let t = ((min_stem - traversed) / seg_len).clamp(0.0, 1.0);
            let cap = Point {
                x: points[seg_idx].x + seg_vec.x * t,
                y: points[seg_idx].y + seg_vec.y * t,
            };
            return Some((seg_idx, cap));
        }
        traversed += seg_len;
    }

    Some((last_seg_idx_on_ray, points[last_seg_idx_on_ray + 1]))
}

fn rebuild_with_start_cap(points: &[Point], seg_idx: usize, cap: Point) -> Vec<Point> {
    if points.len() < 2 || seg_idx >= points.len() - 1 {
        return points.to_vec();
    }

    let mut rebuilt = Vec::with_capacity(points.len() + 1);
    rebuilt.push(points[0]);
    rebuilt.push(cap);
    rebuilt.extend_from_slice(&points[(seg_idx + 1)..]);
    dedup_consecutive_svg_points(&rebuilt)
}

fn end_cap_point_on_existing_run(points: &[Point], min_stem: f64) -> Option<(usize, Point)> {
    const EPS: f64 = 1e-6;
    if points.len() < 2 || min_stem <= 0.0 {
        return None;
    }

    let n = points.len();
    let base_vec = Point {
        x: points[n - 2].x - points[n - 1].x,
        y: points[n - 2].y - points[n - 1].y,
    };
    let last_segment_len = (base_vec.x * base_vec.x + base_vec.y * base_vec.y).sqrt();
    if last_segment_len <= EPS {
        return None;
    }

    let mut traversed = 0.0;
    let mut first_seg_idx_on_ray = n - 2;
    for seg_idx in (0..(points.len() - 1)).rev() {
        let seg_vec = Point {
            x: points[seg_idx].x - points[seg_idx + 1].x,
            y: points[seg_idx].y - points[seg_idx + 1].y,
        };
        let seg_len = (seg_vec.x * seg_vec.x + seg_vec.y * seg_vec.y).sqrt();
        if seg_len <= EPS {
            continue;
        }
        if !vectors_share_ray(base_vec, seg_vec) {
            break;
        }
        first_seg_idx_on_ray = seg_idx;
        if traversed + seg_len + EPS >= min_stem {
            let t = ((min_stem - traversed) / seg_len).clamp(0.0, 1.0);
            let cap = Point {
                x: points[seg_idx + 1].x + seg_vec.x * t,
                y: points[seg_idx + 1].y + seg_vec.y * t,
            };
            return Some((seg_idx, cap));
        }
        traversed += seg_len;
    }

    Some((first_seg_idx_on_ray, points[first_seg_idx_on_ray]))
}

fn rebuild_with_end_cap(points: &[Point], seg_idx: usize, cap: Point) -> Vec<Point> {
    if points.len() < 2 || seg_idx >= points.len() - 1 {
        return points.to_vec();
    }

    let mut rebuilt = Vec::with_capacity(points.len() + 1);
    rebuilt.extend_from_slice(&points[..=seg_idx]);
    rebuilt.push(cap);
    rebuilt.push(points[points.len() - 1]);
    dedup_consecutive_svg_points(&rebuilt)
}

fn primary_axis_for_direction(direction: Direction) -> SegmentAxis {
    match direction {
        Direction::TopDown | Direction::BottomTop => SegmentAxis::Vertical,
        Direction::LeftRight | Direction::RightLeft => SegmentAxis::Horizontal,
    }
}

fn is_primary_secondary_primary_return(points: &[Point], direction: Direction) -> bool {
    if points.len() != 4 {
        return false;
    }
    let primary = primary_axis_for_direction(direction);
    let secondary = match primary {
        SegmentAxis::Horizontal => SegmentAxis::Vertical,
        SegmentAxis::Vertical => SegmentAxis::Horizontal,
    };
    matches!(
        (
            segment_axis(points[0], points[1]),
            segment_axis(points[1], points[2]),
            segment_axis(points[2], points[3]),
        ),
        (Some(a), Some(b), Some(c)) if a == primary && b == secondary && c == primary
    )
}

fn first_segment_is_secondary_axis(points: &[Point], direction: Direction) -> bool {
    if points.len() < 2 {
        return false;
    }
    match segment_axis(points[0], points[1]) {
        Some(SegmentAxis::Horizontal) => {
            matches!(direction, Direction::TopDown | Direction::BottomTop)
        }
        Some(SegmentAxis::Vertical) => {
            matches!(direction, Direction::LeftRight | Direction::RightLeft)
        }
        None => false,
    }
}

fn ends_on_secondary_axis(points: &[Point], direction: Direction) -> bool {
    if points.len() < 2 {
        return false;
    }
    let last = points.len() - 1;
    match segment_axis(points[last - 1], points[last]) {
        Some(SegmentAxis::Horizontal) => {
            matches!(direction, Direction::TopDown | Direction::BottomTop)
        }
        Some(SegmentAxis::Vertical) => {
            matches!(direction, Direction::LeftRight | Direction::RightLeft)
        }
        None => false,
    }
}

pub(super) fn is_simple_two_node_reciprocal_pair(diagram: &Graph, edge: &Edge) -> bool {
    if edge.from == edge.to || edge.from_subgraph.is_some() || edge.to_subgraph.is_some() {
        return false;
    }

    let mut pair_edges = 0usize;
    let mut has_forward = false;
    let mut has_backward = false;

    for other in &diagram.edges {
        if other.stroke == Stroke::Invisible {
            continue;
        }

        let touches_endpoints = other.from == edge.from
            || other.to == edge.from
            || other.from == edge.to
            || other.to == edge.to;
        if !touches_endpoints {
            continue;
        }

        if other.from_subgraph.is_some() || other.to_subgraph.is_some() {
            return false;
        }

        let is_forward = other.from == edge.from && other.to == edge.to;
        let is_backward = other.from == edge.to && other.to == edge.from;
        if !is_forward && !is_backward {
            return false;
        }

        pair_edges += 1;
        has_forward |= is_forward;
        has_backward |= is_backward;
    }

    has_forward && has_backward && pair_edges == 2
}