konoma 0.28.0

Terminal file browser built for AI pair-programming — full-screen previews (Markdown, images, PDF, CSV), a git suite (jj/Jujutsu in preview), and an agent-watch mode that follows your AI's edits (macOS and Linux)
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! How big a node is, what its outline looks like, and where an edge should meet it.
//!
//! # The sizes are mermaid's, taken from mermaid's source
//!
//! `docs/FEATURE-MERMAID-RENDERER.md` §0-1 says konoma does not chase a pixel match, but the
//! *proportions* are not a matter of taste: a diamond that is sized like a rectangle makes every
//! branch in the diagram look wrong, and mermaid's own formulas are the only description of those
//! proportions that is not a guess. Each `size_*` function below cites the file it came from in
//! `mermaid@11.17.2`, and the padding they all read is mermaid's `flowchart.padding`, whose schema
//! default is 15 (`flowDb.ts` reads `config.flowchart?.padding || 8`, and the schema always
//! supplies 15, so 8 is unreachable for a flowchart).
//!
//! The one that matters most is the diamond: mermaid gives it `s = w + h` where `w` and `h` are
//! the padded label, and draws a square of side `s` rotated 45°. Sizing it as `w × h` instead
//! makes the branch labels collide with the box on every decision node.
//!
//! # Two families, one interface
//!
//! [`Glyph`] is what a node is drawn as, and it has three kinds of member: the fifteen a
//! flowchart can ask for, wrapped in [`Glyph::Flow`]; the six a state diagram adds — the two
//! `[*]` markers, a choice diamond, a fork/join bar, a note, and a state box with a rule under
//! its first line; and the two compartmented boxes stage 3 adds, [`Glyph::ClassBox`] and
//! [`Glyph::ErBox`]. Everything downstream of this module (layout, clipping, emission, the
//! geometric invariants) is written against `Glyph` and knows nothing about which family a node
//! came from, which is what lets each stage reuse the pipeline whole rather than growing a
//! second one.
//!
//! # Shapes konoma folds together
//!
//! §2-4 settles that all 53 names must be *recognised* but need not be *drawn* apart. The parser
//! already folds the 53 onto the fifteen [`Shape`] variants; this module draws fourteen of them
//! and lets [`Shape::Text`] draw no outline at all. Nothing is folded further, so a shape a reader
//! wrote is a shape they see.
//!
//! # Intersections
//!
//! dagre routes edges between node *rectangles* — it knows nothing about outlines — so its first
//! and last waypoints sit on a bounding box. `edges::clip` throws those two away and asks this
//! module where the line really leaves the shape, which is what makes a branch come out of a
//! diamond's slanted side instead of its bounding box's flat one. The polygon case takes the
//! nearest of *all* the sides' crossings along the ray, which is
//! [`layout::intersect::intersect_polygon`]'s contract.

use std::f64::consts::PI;

use crate::preview::mermaid::flowchart::Shape;
use crate::preview::mermaid::layout::intersect::{
    intersect_ellipse, intersect_polygon, intersect_rect,
};
use crate::preview::mermaid::layout::Point;

/// mermaid's `flowchart.padding`. Schema default 15 (`config.schema.yaml`).
pub const PADDING: f64 = 15.0;

/// Corner radius of `A(text)`. mermaid's `themeVariables.radius`, default 5 (`roundedRect.ts`).
pub const CORNER_RADIUS: f64 = 5.0;

/// Half the gap between the two rings of `A(((text)))` (`doubleCircle.ts`).
pub const DOUBLE_CIRCLE_GAP: f64 = 5.0;

/// Width of the side frames of `A[[text]]` (`subroutine.ts`'s `FRAME_WIDTH`).
pub const SUBROUTINE_FRAME: f64 = 8.0;

/// How many segments approximate each half-circle cap of a stadium when intersecting it.
/// mermaid uses 50; the cap is only ever a few tens of px across, so 24 puts the worst-case
/// deviation well under a tenth of a pixel.
const CAP_SEGMENTS: usize = 24;

/// Radius of the dot `[*]` becomes. `stateStart.ts` draws a circle of `r = 7`.
pub const STATE_MARKER_RADIUS: f64 = 7.0;

/// Radius of the filled core inside the end marker (`stateEnd.ts`'s inner circle).
pub const STATE_END_INNER_RADIUS: f64 = 4.0;

/// Side of the square a `<<choice>>` diamond is cut from. `choice.ts` uses 40 and draws no text.
pub const CHOICE_SIZE: f64 = 40.0;

/// Long side of a `<<fork>>` / `<<join>>` bar (`forkJoin.ts`).
pub const BAR_LENGTH: f64 = 70.0;

/// Short side of a `<<fork>>` / `<<join>>` bar (`forkJoin.ts`).
pub const BAR_THICKNESS: f64 = 10.0;

/// How far a note's folded corner reaches in from its top-right corner.
///
/// konoma's own: mermaid draws a note as a plain rectangle. The fold is the one thing that says
/// "this is an aside, not a state" without a colour the terminal might be showing through
/// (§0-1 is what allows the difference).
pub const NOTE_FOLD: f64 = 10.0;

/// Space between a sequence participant's name and the sides of its box.
pub const PARTICIPANT_PAD_X: f64 = 14.0;

/// Space above and below a sequence participant's name.
pub const PARTICIPANT_PAD_Y: f64 = 10.0;

/// The narrowest a sequence participant's box may be.
///
/// Same reasoning as [`super::panel::ER_MIN_WIDTH`]: a row of boxes of wildly different widths
/// reads as noise, and a one-letter participant beside a sentence-long one is the common case.
/// mermaid pins its own at a flat `sequence.width` of 150, which is far too wide for a terminal.
pub const PARTICIPANT_MIN_WIDTH: f64 = 62.0;

/// Height of the band a sequence `actor`'s stick figure is drawn in, above its name.
pub const ACTOR_FIGURE_HEIGHT: f64 = 32.0;

/// Radius of that figure's head.
pub const ACTOR_HEAD_RADIUS: f64 = 6.0;

/// How far a mindmap cloud's bumps and a bang's spikes reach past the box that holds the words.
pub const BUMP: f64 = 6.0;

/// Space above and below the words of a mindmap node drawn with no border.
pub const UNDERLINE_GAP: f64 = 3.0;

/// Radius of a user journey's mood face.
pub const FACE_RADIUS: f64 = 11.0;

/// How far a block arrow's head reaches past its body.
pub const ARROW_HEAD: f64 = 10.0;

/// Radius of a git graph's commit dot.
pub const COMMIT_RADIUS: f64 = 8.0;

/// What a node is drawn as.
///
/// See the module docs: one enum over both diagram families, so that everything downstream is
/// written once.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Glyph {
    /// One of a flowchart's fifteen outlines.
    Flow(Shape),
    /// The `[*]` an arrow leaves: a filled dot.
    StateStart,
    /// The `[*]` an arrow enters: a ring around a filled dot.
    StateEnd,
    /// `state x <<choice>>`: a diamond with no text.
    Choice,
    /// `state x <<fork>>` / `<<join>>`: a solid bar. `horizontal` is true when the bar lies
    /// across the page, which is what a top-to-bottom diagram wants.
    Bar {
        /// Whether the long axis is horizontal.
        horizontal: bool,
    },
    /// A `note left of` / `note right of`: a box with a folded corner.
    Note,
    /// A state box whose label arrived as more than one description, drawn with a rule under
    /// the first of them (mermaid's `SHAPE_STATE_WITH_DESC`).
    TitledBox,
    /// A class diagram's `classBox`: a square-cornered rectangle divided into compartments. What
    /// goes in them is the node's [`Panel`](super::panel::Panel), which is also what sized it.
    ClassBox,
    /// An ER diagram's `erBox`: a name band over a grid of attributes. Sized by its panel too.
    ErBox,
    /// A sequence diagram's `participant`: a rounded box with a minimum width, sitting at the head
    /// of a lifeline.
    Participant,
    /// A sequence diagram's `actor`: a stick figure with the name underneath it, and **no box**.
    ///
    /// mermaid draws the figure too; the crate konoma is replacing draws a second plain box, so
    /// `actor Alice` and `participant Alice` come out identical there. The name lives in the
    /// node's [`Panel`](super::panel::Panel), which is also what sized the node — the figure needs
    /// a band of its own above the text, and a centred label cannot express that.
    Actor,

    // --- stage 5: the marks a data chart is made of -------------------------------------------
    //
    // Seven kinds, and not one of them is a box with a word in it. What they have in common with
    // everything above is that they are *kinds*, with no continuous geometry of their own: an
    // angle, a ribbon's four corners and a graticule's ring count live in [`Mark`], for the same
    // reason a compartment's rows live in a `Panel` — a bounding box cannot say them, and putting
    // them here would cost `Glyph` its `Eq` and `Hash`.
    /// A pie slice. Its angles come from [`Mark::Wedge`] and its radius from the node's box.
    Wedge,
    /// A rectangle painted in a series colour: a bar, a treemap tile, a packet field, a Sankey
    /// node's column, a legend swatch.
    ChartBar,
    /// A small filled disc marking one datum. No outline — at four pixels across, a stroke is
    /// most of the mark.
    ChartPoint,
    /// A Sankey flow: a band whose two ends have different heights, sized by [`Mark::Ribbon`].
    Ribbon,
    /// Words and nothing else, in a box that is exactly the words.
    ///
    /// Not `Flow(Shape::Text)`, which pads its box by `PADDING` on every side: a tick label whose
    /// box is fifteen pixels wider than its ink cannot be checked for overlapping its neighbour,
    /// and overlapping tick labels are the commonest way a chart becomes unreadable.
    ChartLabel,
    /// The rectangle a plot is drawn inside — an outline in the axis colour, with nothing behind
    /// it, because §1 forbids painting over the terminal.
    PlotFrame,
    /// A radar chart's background: concentric rings and the spokes, from [`Mark::Graticule`].
    Graticule,

    // --- stage 5b: the marks the last eleven languages needed --------------------------------
    /// A mindmap's `)text(` — a rounded outline with bumps along it.
    Cloud,
    /// A mindmap's `))text((` — the spiky one.
    Bang,
    /// A mindmap node written with no brackets at all: the words with a rule under them, which
    /// is what mermaid's `no-border` draws and what makes an unadorned mindmap read as a tree.
    Underline,
    /// A user journey's score: a face, from [`Mark::Face`]. Five moods, and the *only* thing that
    /// distinguishes a journey diagram from a table of numbers.
    Face,
    /// A block diagram's `<["Label"]>(right)` — a rectangle with arrowheads on the sides
    /// [`Mark::BlockArrow`] names.
    BlockArrow,
    /// A git graph's `type: REVERSE` — a commit dot with a cross through it.
    Reverted,
}

/// Continuous geometry a chart mark needs that its bounding box cannot express.
///
/// The same idea as [`super::panel::Panel`], and for the same reason: [`Glyph`] says *what kind of
/// mark*, the node's `size` says *how big*, and anything left over — an angle, a ribbon's four
/// corners — goes here, so that `Glyph` stays a plain enumeration that a `match` can be exhaustive
/// over and a `HashSet` can hold.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mark {
    /// A pie slice, in degrees clockwise from twelve o'clock.
    Wedge {
        /// Where the slice begins.
        start: f64,
        /// How far round it goes.
        sweep: f64,
    },
    /// A Sankey flow. All four are y offsets from the node's centre; the node's width is the
    /// horizontal span between the two ends.
    Ribbon {
        /// Top of the left end.
        left_top: f64,
        /// Bottom of the left end.
        left_bottom: f64,
        /// Top of the right end.
        right_top: f64,
        /// Bottom of the right end.
        right_bottom: f64,
    },
    /// A user journey's mood, 1 (unhappy) to 5 (happy). Out-of-range values are clamped, which
    /// is what upstream's face picker does too.
    Face {
        /// The score written on the task.
        score: f64,
    },
    /// Which sides of a block arrow carry a head.
    BlockArrow {
        /// A head on the left edge.
        left: bool,
        /// A head on the right edge.
        right: bool,
        /// A head on the top edge.
        up: bool,
        /// A head on the bottom edge.
        down: bool,
    },
    /// A radar chart's rings and spokes.
    Graticule {
        /// How many rings, the outermost included.
        rings: usize,
        /// How many spokes radiate from the centre.
        spokes: usize,
        /// Whether a ring is a copy of the spoke polygon rather than a circle.
        polygon: bool,
    },
}

impl From<Shape> for Glyph {
    fn from(shape: Shape) -> Glyph {
        Glyph::Flow(shape)
    }
}

impl Default for Glyph {
    fn default() -> Glyph {
        Glyph::Flow(Shape::default())
    }
}

/// A width and a height, in px.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Size {
    /// Width.
    pub w: f64,
    /// Height.
    pub h: f64,
}

impl Size {
    /// Convenience constructor.
    pub fn new(w: f64, h: f64) -> Size {
        Size { w, h }
    }
}

/// The bounding box a node of this shape needs for a label of size `label`.
///
/// This is the size handed to dagre, which lays out rectangles; the outline is drawn inside it.
/// For the slanted shapes that means the box is wider than mermaid's intermediate `w`, because
/// mermaid's own `updateNodeBounds` measures the drawn polygon, overhang included.
fn flow_size(shape: Shape, label: Size) -> Size {
    let (lw, lh) = (label.w, label.h);
    match shape {
        // squareRect.ts -> drawRect.ts: labelPaddingX = padding*2, labelPaddingY = padding,
        // and drawRect doubles each again.
        Shape::Rect => Size::new(lw + PADDING * 4.0, lh + PADDING * 2.0),
        // roundedRect.ts -> drawRect.ts: both paddings are padding*1, doubled by drawRect.
        Shape::RoundedRect => Size::new(lw + PADDING * 2.0, lh + PADDING * 2.0),
        // stadium.ts: h gets ONE padding (not two), and w carries a quarter of h for the caps.
        Shape::Stadium => {
            let h = lh + PADDING;
            Size::new(lw + h / 4.0 + PADDING, h)
        }
        // circle.ts. Two things to know about this line:
        //
        //  * Upstream reads `options?.padding ?? halfPadding`, and the flowchart renderer passes
        //    no `padding`, so mermaid effectively uses padding/2. konoma uses the whole padding,
        //    which is the only place in this file that knowingly leaves mermaid's number.
        //  * The radius follows the label's **width alone**, in konoma as in mermaid. A tall
        //    label — several `<br>`s — therefore overflows the ring at top and bottom. That is
        //    upstream's behaviour and it is recorded here (and in
        //    `tests::every_shape_holds_the_label_it_was_sized_for`, which exempts circles for
        //    exactly this reason) rather than quietly diverged from.
        Shape::Circle => {
            let r = lw / 2.0 + PADDING;
            Size::new(r * 2.0, r * 2.0)
        }
        // doubleCircle.ts: the outer ring already uses the whole padding upstream.
        Shape::DoubleCircle => {
            let r = lw / 2.0 + PADDING;
            Size::new(r * 2.0, r * 2.0)
        }
        // question.ts: w and h each get one padding, and the drawn square's side is their SUM.
        Shape::Diamond => {
            let s = (lw + PADDING) + (lh + PADDING);
            Size::new(s, s)
        }
        // hexagon.ts: h gets one padding, the slanted ends are h/4 wide each, and w gets one more.
        Shape::Hexagon => {
            let h = lh + PADDING;
            Size::new(lw + h / 2.0 + PADDING, h)
        }
        // subroutine.ts: one padding, plus a frame on each side.
        Shape::Subroutine => Size::new(lw + 2.0 * SUBROUTINE_FRAME + PADDING, lh + PADDING),
        // cylinder.ts: the ellipse's ry follows the width, and the drawn path stands 2*ry taller
        // than the `h` upstream computes.
        Shape::Cylinder => {
            let w = lw + PADDING;
            let ry = cylinder_ry(w);
            Size::new(w, lh + PADDING + 3.0 * ry)
        }
        // trapezoid.ts / leanRight.ts / leanLeft.ts: one padding each way; the slant adds h/2 of
        // overhang on each side, so the drawn box is `w + h` wide.
        Shape::Trapezoid | Shape::LeanRight | Shape::LeanLeft => {
            let h = lh + PADDING;
            Size::new(lw + PADDING + h, h)
        }
        // invertedTrapezoid.ts is the odd one out: it doubles BOTH paddings where trapezoid.ts
        // doubles neither. That asymmetry is upstream's, not a transcription slip.
        Shape::InvTrapezoid => {
            let h = lh + PADDING * 2.0;
            Size::new(lw + PADDING * 2.0 + h, h)
        }
        // rectLeftInvArrow.ts: the notch on the left is h/4 deep and sticks out of the label box.
        Shape::Odd => {
            let h = lh + PADDING;
            Size::new(lw + PADDING + h / 4.0, h)
        }
        // text.ts: one padding, no outline.
        Shape::Text => Size::new(lw + PADDING, lh + PADDING),
    }
}

/// `ry` of a cylinder's end ellipse for a body of width `w` (`cylinder.ts`).
fn cylinder_ry(w: f64) -> f64 {
    let rx = w / 2.0;
    rx / (2.5 + w / 50.0)
}

/// What to draw for a shape whose bounding box is `size`, in coordinates relative to the centre.
#[derive(Debug, Clone, PartialEq)]
pub enum Outline {
    /// Axis-aligned rectangle, corner radius `r` (0 for a square corner).
    Rect {
        /// Width.
        w: f64,
        /// Height.
        h: f64,
        /// Corner radius.
        r: f64,
    },
    /// A circle centred on the node.
    Circle {
        /// Radius.
        r: f64,
    },
    /// Two concentric circles.
    DoubleCircle {
        /// Outer radius.
        outer: f64,
        /// Inner radius.
        inner: f64,
    },
    /// A closed polygon.
    Polygon(Vec<Point>),
    /// A rectangle with a vertical rule `inset` from each side.
    Subroutine {
        /// Width.
        w: f64,
        /// Height.
        h: f64,
        /// Distance from each side to its rule.
        inset: f64,
    },
    /// A cylinder standing on end: a body `w` wide and `h` tall whose caps are ellipses of
    /// vertical radius `ry`.
    Cylinder {
        /// Width.
        w: f64,
        /// Total height, caps included.
        h: f64,
        /// Vertical radius of a cap.
        ry: f64,
    },
    /// A mindmap cloud: a rounded box with bumps along its outline.
    Cloud {
        /// Width.
        w: f64,
        /// Height.
        h: f64,
    },
    /// A rule under the words, and nothing else.
    Underline {
        /// Width of the rule.
        w: f64,
        /// Height of the box the rule sits at the bottom of.
        h: f64,
    },
    /// A journey score, drawn as a face.
    Face {
        /// Radius.
        r: f64,
        /// The mood, 1 to 5.
        score: f64,
    },
    /// A circle with a cross through it — a reverted commit.
    Crossed {
        /// Radius.
        r: f64,
    },
    /// A filled dot with no outline — the `[*]` an arrow leaves.
    Disc {
        /// Radius.
        r: f64,
    },
    /// A ring around a filled dot — the `[*]` an arrow enters.
    Target {
        /// Radius of the ring.
        outer: f64,
        /// Radius of the filled core.
        inner: f64,
    },
    /// A solid bar — `<<fork>>` and `<<join>>`.
    Bar {
        /// Width.
        w: f64,
        /// Height.
        h: f64,
    },
    /// A box with its top-right corner folded over — a note.
    Note {
        /// Width.
        w: f64,
        /// Height.
        h: f64,
        /// How far the fold reaches in from the corner, along both axes.
        fold: f64,
    },
    /// A stick figure standing in the top `figure_h` of the box, with the name below it.
    Actor {
        /// Width of the bounding box.
        w: f64,
        /// Height of the bounding box.
        h: f64,
        /// Height of the band the figure occupies, measured down from the top.
        figure_h: f64,
    },
    /// A pie slice: a sector of radius `r`, from `start` to `start + sweep` degrees clockwise
    /// from twelve o'clock.
    Wedge {
        /// Radius.
        r: f64,
        /// Where the slice begins, in degrees.
        start: f64,
        /// How far round it goes, in degrees.
        sweep: f64,
    },
    /// A Sankey flow, as four y offsets from the centre across a span of `w`.
    Ribbon {
        /// Horizontal span.
        w: f64,
        /// Top of the left end.
        left_top: f64,
        /// Bottom of the left end.
        left_bottom: f64,
        /// Top of the right end.
        right_top: f64,
        /// Bottom of the right end.
        right_bottom: f64,
    },
    /// A radar background: `rings` rings of radius `r * i / rings`, and `spokes` spokes.
    Graticule {
        /// Outer radius.
        r: f64,
        /// How many rings.
        rings: usize,
        /// How many spokes.
        spokes: usize,
        /// Whether the rings are polygons rather than circles.
        polygon: bool,
    },
    /// Nothing is drawn.
    None,
}

/// The outline of `shape` inside a bounding box of `size`.
fn flow_outline(shape: Shape, size: Size) -> Outline {
    let (w, h) = (size.w, size.h);
    match shape {
        Shape::Rect => Outline::Rect { w, h, r: 0.0 },
        Shape::RoundedRect => Outline::Rect {
            w,
            h,
            r: CORNER_RADIUS,
        },
        Shape::Stadium => Outline::Rect { w, h, r: h / 2.0 },
        Shape::Circle => Outline::Circle { r: w / 2.0 },
        Shape::DoubleCircle => Outline::DoubleCircle {
            outer: w / 2.0,
            inner: (w / 2.0 - DOUBLE_CIRCLE_GAP).max(0.0),
        },
        Shape::Subroutine => Outline::Subroutine {
            w,
            h,
            inset: SUBROUTINE_FRAME,
        },
        Shape::Cylinder => Outline::Cylinder {
            w,
            h,
            ry: cylinder_ry(w),
        },
        Shape::Text => Outline::None,
        _ => Outline::Polygon(flow_polygon(shape, size)),
    }
}

/// The vertices of a polygonal shape, relative to the node centre, in boundary order.
///
/// Shapes that are not polygons return their own boundary sampled densely enough to intersect
/// against (the stadium) or an empty list.
fn flow_polygon(shape: Shape, size: Size) -> Vec<Point> {
    let (w, h) = (size.w, size.h);
    let (hw, hh) = (w / 2.0, h / 2.0);
    match shape {
        // question.ts, recentred: a square of side `s` on its corner.
        Shape::Diamond => vec![
            Point::new(0.0, -hh),
            Point::new(hw, 0.0),
            Point::new(0.0, hh),
            Point::new(-hw, 0.0),
        ],
        // hexagon.ts with m = h/4.
        Shape::Hexagon => {
            let m = h / 4.0;
            vec![
                Point::new(-hw + m, -hh),
                Point::new(hw - m, -hh),
                Point::new(hw, 0.0),
                Point::new(hw - m, hh),
                Point::new(-hw + m, hh),
                Point::new(-hw, 0.0),
            ]
        }
        // trapezoid.ts: the wide side is the bottom. The drawn box is `inner + h` wide, so the
        // inner top edge spans +/- (w - h)/2.
        Shape::Trapezoid => {
            let t = (hw - hh).max(0.0);
            vec![
                Point::new(-hw, hh),
                Point::new(hw, hh),
                Point::new(t, -hh),
                Point::new(-t, -hh),
            ]
        }
        // invertedTrapezoid.ts: the wide side is the top.
        Shape::InvTrapezoid => {
            let t = (hw - hh).max(0.0);
            vec![
                Point::new(-t, hh),
                Point::new(t, hh),
                Point::new(hw, -hh),
                Point::new(-hw, -hh),
            ]
        }
        // leanRight.ts: a parallelogram leaning right.
        Shape::LeanRight => {
            let t = (hw - hh).max(0.0);
            vec![
                Point::new(-hw, hh),
                Point::new(t, hh),
                Point::new(hw, -hh),
                Point::new(-t, -hh),
            ]
        }
        // leanLeft.ts: the same, mirrored.
        Shape::LeanLeft => {
            let t = (hw - hh).max(0.0);
            vec![
                Point::new(-t, hh),
                Point::new(hw, hh),
                Point::new(t, -hh),
                Point::new(-hw, -hh),
            ]
        }
        // rectLeftInvArrow.ts: a rectangle with a V notched into its left side, h/4 deep.
        Shape::Odd => {
            let notch = h / 4.0;
            vec![
                Point::new(-hw, -hh),
                Point::new(-hw + notch, 0.0),
                Point::new(-hw, hh),
                Point::new(hw, hh),
                Point::new(hw, -hh),
            ]
        }
        // stadium.ts approximates its own caps the same way, with 50 segments.
        Shape::Stadium => {
            let r = hh;
            let a = (hw - r).max(0.0);
            let mut pts = vec![Point::new(-a, -r), Point::new(a, -r)];
            for i in 1..CAP_SEGMENTS {
                let t = -PI / 2.0 + PI * i as f64 / CAP_SEGMENTS as f64;
                pts.push(Point::new(a + r * t.cos(), r * t.sin()));
            }
            pts.push(Point::new(a, r));
            pts.push(Point::new(-a, r));
            for i in 1..CAP_SEGMENTS {
                let t = PI / 2.0 + PI * i as f64 / CAP_SEGMENTS as f64;
                pts.push(Point::new(-a + r * t.cos(), r * t.sin()));
            }
            pts
        }
        _ => Vec::new(),
    }
}

/// Where a line aimed at `target` leaves a `shape` of `size` centred on `center`.
///
/// This is the step that makes an edge come out of the shape the reader sees rather than out of
/// the bounding box dagre reasoned about.
fn flow_intersect(shape: Shape, center: Point, size: Size, target: &Point) -> Point {
    match shape {
        // circle.ts / doubleCircle.ts both clip against the outer ring.
        Shape::Circle | Shape::DoubleCircle => {
            intersect_ellipse(center.x, center.y, size.w / 2.0, size.h / 2.0, target)
        }
        // drawRect.ts, subroutine's frame and cylinder.ts all clip against the box. (Upstream's
        // cylinder nudges the result towards the cap; the difference is under a pixel and the
        // nudge can land a point inside the shape, which invariant (2) forbids.)
        Shape::Rect | Shape::RoundedRect | Shape::Subroutine | Shape::Cylinder | Shape::Text => {
            intersect_rect(center.x, center.y, size.w, size.h, target)
        }
        _ => {
            let verts: Vec<Point> = flow_polygon(shape, size)
                .into_iter()
                .map(|p| Point::new(center.x + p.x, center.y + p.y))
                .collect();
            if verts.len() < 3 {
                return intersect_rect(center.x, center.y, size.w, size.h, target);
            }
            intersect_polygon(&verts, &center, target)
        }
    }
}

// ---------------------------------------------------------------------------------------------
// The `Glyph` layer: one interface over both diagram families
// ---------------------------------------------------------------------------------------------

/// The bounding box a node drawn as `glyph` needs for a label of size `label`.
///
/// The state markers ignore the label entirely, because mermaid draws no text inside them —
/// see [`Glyph`].
pub fn size(glyph: Glyph, label: Size) -> Size {
    match glyph {
        Glyph::Flow(shape) => flow_size(shape, label),
        // stateStart.ts / stateEnd.ts: a fixed dot, whatever the state was called.
        Glyph::StateStart | Glyph::StateEnd => {
            Size::new(STATE_MARKER_RADIUS * 2.0, STATE_MARKER_RADIUS * 2.0)
        }
        // choice.ts: a fixed 40x40 square on its corner, with no text.
        Glyph::Choice => Size::new(CHOICE_SIZE, CHOICE_SIZE),
        // forkJoin.ts: a bar whose long axis follows the diagram's own axis.
        Glyph::Bar { horizontal } => {
            if horizontal {
                Size::new(BAR_LENGTH, BAR_THICKNESS)
            } else {
                Size::new(BAR_THICKNESS, BAR_LENGTH)
            }
        }
        // A note is a rounded-rect-shaped box with room for the fold at its corner.
        Glyph::Note => Size::new(
            (label.w + PADDING * 2.0).max(NOTE_FOLD * 3.0),
            (label.h + PADDING * 2.0).max(NOTE_FOLD * 2.0),
        ),
        // The rule sits between the label's first and second line, so no extra height is needed.
        Glyph::TitledBox => Size::new(label.w + PADDING * 2.0, label.h + PADDING * 2.0),
        // A box that holds a table is measured by its table. `panel::class_panel` /
        // `panel::er_panel` decide the width from the widest row and the height from how many
        // rows there are, and the caller passes that size straight through here — a second
        // opinion about it would be a second thing to keep in step (see `panel`'s module docs).
        Glyph::ClassBox | Glyph::ErBox => label,
        // A participant's box is its name plus padding, floored at a width that keeps a row of
        // them even.
        Glyph::Participant => Size::new(
            (label.w + PARTICIPANT_PAD_X * 2.0).max(PARTICIPANT_MIN_WIDTH),
            label.h + PARTICIPANT_PAD_Y * 2.0,
        ),
        // Sized by its panel, like the two compartmented boxes, and for the same reason: the
        // figure's band and the name's band are two rows, not one centred label.
        Glyph::Actor => label,
        // Every chart mark is sized by the chart, not by a label: a bar's height is its value
        // against the axis scale, a tile's area is its share of the whole, a wedge's radius is the
        // room the pie was given. Passing the size straight through is the same contract the two
        // compartmented boxes have — one place decides, and nothing here second-guesses it.
        Glyph::Wedge
        | Glyph::ChartBar
        | Glyph::ChartPoint
        | Glyph::Ribbon
        | Glyph::PlotFrame
        | Glyph::Graticule => label,
        // …with one exception, and it is the point of the glyph: a chart label's box **is** its
        // ink, with no padding at all, so that "two tick labels do not overlap" is a statement
        // about what a reader sees.
        Glyph::ChartLabel => label,

        // A cloud and a bang are a rectangle plus the room their bumps and spikes stick out into.
        // Without it the outline crosses its own words, which is the one thing a decorated shape
        // must not do.
        Glyph::Cloud | Glyph::Bang => Size::new(
            label.w + PADDING * 2.0 + BUMP * 2.0,
            label.h + PADDING * 2.0 + BUMP * 2.0,
        ),
        // The rule is drawn at the bottom of the box, so the box is the words plus a little room
        // for it and no side padding at all — a mindmap's default node is *text*.
        Glyph::Underline => Size::new(label.w + PADDING, label.h + UNDERLINE_GAP * 2.0),
        // A face is a fixed disc: its size says nothing, its expression says everything.
        Glyph::Face => Size::new(FACE_RADIUS * 2.0, FACE_RADIUS * 2.0),
        // A block arrow is its label plus a head's worth of room on every side that has one. The
        // heads are in the mark, which `size` does not see, so all four are allowed for — an
        // arrow whose label ran into its own point would be worse than one that is slightly wide.
        Glyph::BlockArrow => Size::new(
            label.w + PADDING * 2.0 + ARROW_HEAD * 2.0,
            label.h + PADDING * 2.0 + ARROW_HEAD * 2.0,
        ),
        // A commit dot, whatever it is called.
        Glyph::Reverted => Size::new(COMMIT_RADIUS * 2.0, COMMIT_RADIUS * 2.0),
    }
}

/// The outline of `glyph` inside a bounding box of `size`.
///
/// `mark` carries the continuous geometry the box cannot say — see [`Mark`]. Every glyph that
/// needs one draws **nothing** without it, rather than guessing: a wedge with no angles is not a
/// full circle, it is a bug, and a silent full circle would be a lying picture.
pub fn outline(glyph: Glyph, size: Size, mark: Option<Mark>) -> Outline {
    match glyph {
        Glyph::Wedge => match mark {
            Some(Mark::Wedge { start, sweep }) => Outline::Wedge {
                r: size.w / 2.0,
                start,
                sweep,
            },
            _ => Outline::None,
        },
        Glyph::Ribbon => match mark {
            Some(Mark::Ribbon {
                left_top,
                left_bottom,
                right_top,
                right_bottom,
            }) => Outline::Ribbon {
                w: size.w,
                left_top,
                left_bottom,
                right_top,
                right_bottom,
            },
            _ => Outline::None,
        },
        Glyph::Graticule => match mark {
            Some(Mark::Graticule {
                rings,
                spokes,
                polygon,
            }) => Outline::Graticule {
                r: size.w / 2.0,
                rings,
                spokes,
                polygon,
            },
            _ => Outline::None,
        },
        Glyph::ChartBar | Glyph::PlotFrame => Outline::Rect {
            w: size.w,
            h: size.h,
            r: 0.0,
        },
        Glyph::ChartPoint => Outline::Disc {
            r: size.w.min(size.h) / 2.0,
        },
        Glyph::ChartLabel => Outline::None,
        Glyph::Face => match mark {
            Some(Mark::Face { score }) => Outline::Face {
                r: size.w.min(size.h) / 2.0,
                score,
            },
            _ => Outline::None,
        },
        Glyph::BlockArrow => match mark {
            Some(Mark::BlockArrow {
                left,
                right,
                up,
                down,
            }) => Outline::Polygon(block_arrow_polygon(size, left, right, up, down)),
            _ => Outline::None,
        },
        _ => flow_outline_or_glyph(glyph, size),
    }
}

/// The outline of a block arrow: a rectangle whose sides with a head are pushed out into one.
///
/// Walked clockwise from the top-left corner, inserting a triangle wherever a head was asked for,
/// so the result is one closed polygon and the invariant tests can treat it like any other.
fn block_arrow_polygon(size: Size, left: bool, right: bool, up: bool, down: bool) -> Vec<Point> {
    let (hw, hh) = (size.w / 2.0, size.h / 2.0);
    let (bw, bh) = (
        hw - if left || right { ARROW_HEAD } else { 0.0 },
        hh - if up || down { ARROW_HEAD } else { 0.0 },
    );
    let mut p: Vec<Point> = Vec::new();
    p.push(Point::new(-bw, -bh));
    if up {
        p.push(Point::new(-bw * 0.5, -bh));
        p.push(Point::new(0.0, -hh));
        p.push(Point::new(bw * 0.5, -bh));
    }
    p.push(Point::new(bw, -bh));
    if right {
        p.push(Point::new(bw, -bh * 0.5));
        p.push(Point::new(hw, 0.0));
        p.push(Point::new(bw, bh * 0.5));
    }
    p.push(Point::new(bw, bh));
    if down {
        p.push(Point::new(bw * 0.5, bh));
        p.push(Point::new(0.0, hh));
        p.push(Point::new(-bw * 0.5, bh));
    }
    p.push(Point::new(-bw, bh));
    if left {
        p.push(Point::new(-bw, bh * 0.5));
        p.push(Point::new(-hw, 0.0));
        p.push(Point::new(-bw, -bh * 0.5));
    }
    p
}

fn flow_outline_or_glyph(glyph: Glyph, size: Size) -> Outline {
    match glyph {
        Glyph::Flow(shape) => flow_outline(shape, size),
        Glyph::StateStart => Outline::Disc { r: size.w / 2.0 },
        Glyph::StateEnd => Outline::Target {
            outer: size.w / 2.0,
            inner: STATE_END_INNER_RADIUS.min(size.w / 2.0),
        },
        Glyph::Choice => Outline::Polygon(flow_polygon(Shape::Diamond, size)),
        Glyph::Bar { .. } => Outline::Bar {
            w: size.w,
            h: size.h,
        },
        Glyph::Note => Outline::Note {
            w: size.w,
            h: size.h,
            fold: NOTE_FOLD.min(size.w / 2.0).min(size.h / 2.0),
        },
        Glyph::TitledBox => Outline::Rect {
            w: size.w,
            h: size.h,
            r: CORNER_RADIUS,
        },
        // Square corners, unlike everything else konoma rounds: a UML class and an ER entity are
        // drawn square everywhere they are drawn at all, and the corner is the only thing that
        // tells a compartmented box from a subgraph frame at a glance.
        Glyph::ClassBox | Glyph::ErBox => Outline::Rect {
            w: size.w,
            h: size.h,
            r: 0.0,
        },
        Glyph::Participant => Outline::Rect {
            w: size.w,
            h: size.h,
            r: CORNER_RADIUS,
        },
        Glyph::Actor => Outline::Actor {
            w: size.w,
            h: size.h,
            figure_h: ACTOR_FIGURE_HEIGHT.min(size.h),
        },
        Glyph::Cloud => Outline::Cloud {
            w: size.w,
            h: size.h,
        },
        Glyph::Bang => Outline::Polygon(bang_polygon(size)),
        Glyph::Underline => Outline::Underline {
            w: size.w,
            h: size.h,
        },
        Glyph::Reverted => Outline::Crossed {
            r: size.w.min(size.h) / 2.0,
        },
        // Handled by `outline` before it delegates here.
        Glyph::Wedge
        | Glyph::ChartBar
        | Glyph::ChartPoint
        | Glyph::Ribbon
        | Glyph::ChartLabel
        | Glyph::PlotFrame
        | Glyph::Graticule
        | Glyph::Face
        | Glyph::BlockArrow => Outline::None,
    }
}

/// The spikes of a `))text((`.
///
/// Sixteen points alternating between the box and a little outside it. Enough teeth to read as an
/// explosion at the size a terminal shows, few enough that each one is still a triangle.
fn bang_polygon(size: Size) -> Vec<Point> {
    const TEETH: usize = 16;
    let (rx, ry) = (size.w / 2.0, size.h / 2.0);
    (0..TEETH * 2)
        .map(|i| {
            let a = std::f64::consts::TAU * i as f64 / (TEETH * 2) as f64;
            let k = if i % 2 == 0 {
                1.0
            } else {
                1.0 - BUMP / rx.max(1.0)
            };
            Point::new(rx * k * a.cos(), ry * k * a.sin())
        })
        .collect()
}

/// The vertices of a polygonal glyph, relative to the node centre, in boundary order. Empty for
/// a glyph whose boundary is not a polygon.
pub fn polygon(glyph: Glyph, size: Size) -> Vec<Point> {
    match glyph {
        Glyph::Flow(shape) => flow_polygon(shape, size),
        Glyph::Choice => flow_polygon(Shape::Diamond, size),
        Glyph::Bang => bang_polygon(size),
        _ => Vec::new(),
    }
}

/// Where a line aimed at `target` leaves a `glyph` of `size` centred on `center`.
pub fn intersect(glyph: Glyph, center: Point, size: Size, target: &Point) -> Point {
    match glyph {
        Glyph::Flow(shape) => flow_intersect(shape, center, size, target),
        Glyph::StateStart | Glyph::StateEnd => {
            intersect_ellipse(center.x, center.y, size.w / 2.0, size.h / 2.0, target)
        }
        Glyph::Choice => flow_intersect(Shape::Diamond, center, size, target),
        // A bar, a note and a titled box all clip against their box. The note's folded corner is
        // a detail of the drawing: cutting the line there would leave a visible gap where the
        // fold is, and the fold is at most ten pixels of a corner an edge rarely meets.
        Glyph::Bar { .. }
        | Glyph::Note
        | Glyph::TitledBox
        | Glyph::ClassBox
        | Glyph::ErBox
        | Glyph::Participant
        | Glyph::Actor
        // A chart mark is never an edge's endpoint: a grid line runs the width of the plot, a
        // line plot passes *through* its own points, and a radar curve closes on itself. Clipping
        // against the box is the harmless answer for a question nothing asks.
        | Glyph::Wedge
        | Glyph::ChartBar
        | Glyph::ChartPoint
        | Glyph::Ribbon
        | Glyph::ChartLabel
        | Glyph::PlotFrame
        | Glyph::Graticule
        // A cloud, a bang, an underline and a block arrow all clip against their box: the bumps
        // and the spikes are a decoration a few pixels deep, and cutting a line to them would put
        // an arrowhead inside a tooth.
        | Glyph::Cloud
        | Glyph::Bang
        | Glyph::Underline
        | Glyph::BlockArrow => intersect_rect(center.x, center.y, size.w, size.h, target),
        // A commit dot is a circle, and a line between two commits ends on it.
        Glyph::Face | Glyph::Reverted => {
            intersect_ellipse(center.x, center.y, size.w / 2.0, size.h / 2.0, target)
        }
    }
}