mermaid-text 0.45.0

Render Mermaid diagrams as Unicode box-drawing text — no browser, no image protocols, pure Rust
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
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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
//! 2D character grid used for building the final text output.
//!
//! The grid stores one `char` per cell plus a parallel obstacle layer.
//! The obstacle layer is used by A\* edge routing to distinguish:
//!
//! - **Hard obstacles** — cells that belong to a node bounding box (walls
//!   and interior). Edges must not pass through these.
//! - **Soft obstacles** — cells already occupied by a previously-routed edge.
//!   Edges can cross these but at increased cost.
//!
//! All drawing operations write directly into the grid; the final string is
//! produced by converting the grid to a `String` via its [`std::fmt::Display`]
//! implementation.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use unicode_width::UnicodeWidthChar;

use crate::types::Rgb;

// Box-drawing character sets
/// Rectangle corners and sides. T-junctions and crosses are not listed here
/// because they are derived on demand by the direction-bit canvas via
/// [`DIR_TO_CHAR`].
mod rect {
    pub const TL: char = '';
    pub const TR: char = '';
    pub const BL: char = '';
    pub const BR: char = '';
    pub const H: char = '';
    pub const V: char = '';
}

/// Rounded-corner box characters.
mod rounded {
    pub const TL: char = '';
    pub const TR: char = '';
    pub const BL: char = '';
    pub const BR: char = '';
}

/// Arrow tip characters.
pub mod arrow {
    pub const RIGHT: char = '';
    pub const DOWN: char = '';
    pub const LEFT: char = '';
    pub const UP: char = '';
}

/// Endpoint glyph characters for non-arrow edge terminations.
pub mod endpoint {
    /// Circle endpoint (`--o`).
    pub const CIRCLE: char = '';
    /// Cross endpoint (`--x`).
    pub const CROSS: char = '×';
}

/// Dotted box-drawing characters (┆ for vertical, ┄ for horizontal).
///
/// Unicode's dotted box-drawing characters lack proper junction glyphs, so
/// dotted lines revert to solid junction characters where they meet other
/// edges. This is a documented compromise — see `render/unicode.rs` for the
/// explanation comment at the call site.
mod dotted {
    pub const H: char = '';
    pub const V: char = '';
}

/// Thickness (in cells) of a UML fork/join synchronisation bar.
///
/// A horizontal bar is `BAR_THICKNESS` rows tall; a vertical bar is
/// `BAR_THICKNESS` columns wide. The value 3 matches Mermaid's visual
/// weight for SVG-rendered fork/join bars.
pub const BAR_THICKNESS: usize = 3;

/// Lookup table for thick line junctions: same 4-bit direction mask as
/// `DIR_TO_CHAR` but using thick Unicode glyphs.
const THICK_DIR_TO_CHAR: [char; 16] = [
    ' ', // 0000
    '', // 0001 UP
    '', // 0010 DOWN
    '', // 0011 UP+DOWN
    '', // 0100 LEFT
    '', // 0101 UP+LEFT
    '', // 0110 DOWN+LEFT
    '', // 0111 UP+DOWN+LEFT
    '', // 1000 RIGHT
    '', // 1001 UP+RIGHT
    '', // 1010 DOWN+RIGHT
    '', // 1011 UP+DOWN+RIGHT
    '', // 1100 LEFT+RIGHT
    '', // 1101 UP+LEFT+RIGHT
    '', // 1110 DOWN+LEFT+RIGHT
    '', // 1111 cross
];

// ---------------------------------------------------------------------------
// Direction-bit canvas
// ---------------------------------------------------------------------------
//
// Each cell carries a 4-bit direction mask describing the line segments that
// exit the cell toward its neighbors. Writing a line segment OR-merges the
// appropriate bits into the cell, and the resulting bitmask is used to look
// up the correct box-drawing glyph. This produces correct T-junctions
// (`├ ┤ ┬ ┴`) and crosses (`┼`) for free whenever edges meet — the logic that
// used to live in `merge_h_line`/`merge_v_line`/`merge_corner_*` collapses
// into a single table lookup.

pub(crate) const DIR_UP: u8 = 0b0001;
pub(crate) const DIR_DOWN: u8 = 0b0010;
pub(crate) const DIR_LEFT: u8 = 0b0100;
pub(crate) const DIR_RIGHT: u8 = 0b1000;

/// Lookup table mapping a 4-bit direction mask (UP=1, DOWN=2, LEFT=4, RIGHT=8)
/// to the single box-drawing glyph that represents it.
///
/// Single-direction stubs (`╵╷╴╶`) would render as half-length line fragments
/// in most terminal fonts, so we use the full `│` / `─` instead — matching
/// termaid's chosen behavior for "edge segment that leaves a cell but didn't
/// enter from the expected opposite side".
const DIR_TO_CHAR: [char; 16] = [
    ' ', // 0000 — empty
    '', // 0001 — UP only
    '', // 0010 — DOWN only
    '', // 0011 — UP+DOWN (plain vertical)
    '', // 0100 — LEFT only
    '', // 0101 — UP+LEFT
    '', // 0110 — DOWN+LEFT
    '', // 0111 — UP+DOWN+LEFT
    '', // 1000 — RIGHT only
    '', // 1001 — UP+RIGHT
    '', // 1010 — DOWN+RIGHT
    '', // 1011 — UP+DOWN+RIGHT
    '', // 1100 — LEFT+RIGHT (plain horizontal)
    '', // 1101 — UP+LEFT+RIGHT
    '', // 1110 — DOWN+LEFT+RIGHT
    '', // 1111 — cross
];

// ---------------------------------------------------------------------------
// Grid
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Obstacle classification
// ---------------------------------------------------------------------------

/// Cell-level obstacle classification for A\* routing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Obstacle {
    /// Free cell — no extra routing cost.
    Free,
    /// Cell belongs to a node bounding box. Edges must not enter.
    NodeBox,
    /// Cell already has a routed horizontal edge (`─`, `┄`, etc.).
    ///
    /// A new edge that also runs horizontally through this cell is a
    /// same-axis overlap — A\* charges `SAME_AXIS_COST` (10) so it
    /// prefers a fresh row but will still share when the alternative is
    /// a long detour. A new edge crossing it vertically produces a
    /// perpendicular crossing (`┼`), charged `CROSS_AXIS_COST` (3) —
    /// visually acceptable, so A\* takes the clean crossing instead of
    /// a detour. Both costs are tuned in `Grid::route_edge_with_inner_cost`;
    /// see the comment block there for the rationale (lower than
    /// graph-easy's 30/6 to keep bidirectional pairs inside their
    /// subgraph box).
    EdgeOccupiedHorizontal,
    /// Cell already has a routed vertical edge (`│`, `┆`, etc.).
    ///
    /// Symmetric to [`Obstacle::EdgeOccupiedHorizontal`]: same-axis
    /// (vertical) overlap costs `SAME_AXIS_COST` (10); horizontal
    /// crossing costs `CROSS_AXIS_COST` (3).
    EdgeOccupiedVertical,
    /// Cell is *between* node boxes — inside the convex hull of all
    /// node positions but not on any node itself, not yet edge-occupied.
    /// Routed at standard cost in normal mode; back-edge routing pays a
    /// hefty extra penalty so the perimeter route is preferred over a
    /// shortcut through the diagram body.
    InnerArea,
}

// ---------------------------------------------------------------------------
// A* state
// ---------------------------------------------------------------------------

/// A single entry in the A\* open-set priority queue.
///
/// We use a min-heap via [`BinaryHeap`], so we invert the comparison to turn
/// it into a min-heap (smallest `f_cost` first).
#[derive(Debug, Clone, Copy)]
struct AstarNode {
    /// `f = g + h` (total estimated cost through this node).
    f_cost: f32,
    /// Steps taken to reach this cell from the start.
    g_cost: f32,
    col: usize,
    row: usize,
    /// Direction we arrived from (encoded as 0=R,1=D,2=L,3=U, `u8::MAX`=start).
    dir: u8,
}

impl PartialEq for AstarNode {
    fn eq(&self, other: &Self) -> bool {
        self.f_cost == other.f_cost
    }
}

impl Eq for AstarNode {}

impl Ord for AstarNode {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse order so BinaryHeap is a min-heap.
        other
            .f_cost
            .partial_cmp(&self.f_cost)
            .unwrap_or(Ordering::Equal)
    }
}

impl PartialOrd for AstarNode {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

// ---------------------------------------------------------------------------
// Edge line style
// ---------------------------------------------------------------------------

/// Line style to apply when overwriting a routed path.
///
/// Passed to [`Grid::overdraw_path_style`] after a path has been drawn with
/// solid glyphs by [`Grid::route_edge`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdgeLineStyle {
    /// Leave the path as drawn (solid box-drawing chars from the dir-bit canvas).
    Solid,
    /// Replace horizontal cells with `┄` and vertical cells with `┆`.
    ///
    /// Junctions with other edges are left as solid characters because Unicode
    /// lacks dotted junction glyphs — this is the documented trade-off.
    Dotted,
    /// Replace path cells using thick box-drawing glyphs (`━`, `┃`, `╋`, etc.),
    /// recomputed from the existing direction bitmask.
    Thick,
}

// ---------------------------------------------------------------------------
// Edge attach point
// ---------------------------------------------------------------------------

/// A pixel-precise attachment point on a node's border.
///
/// Used by the router to identify where an edge begins (source side) and
/// where it ends (destination side). Produced by the attachment-point
/// computation in `render/unicode.rs` and consumed by `layout/router.rs`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Attach {
    /// Column index of the attach cell (0 = leftmost column).
    pub col: usize,
    /// Row index of the attach cell (0 = top row).
    pub row: usize,
}

// ---------------------------------------------------------------------------
// Grid
// ---------------------------------------------------------------------------

/// A mutable 2D grid of characters, used as a canvas for rendering.
///
/// The grid uses `(col, row)` addressing with origin at top-left `(0, 0)`.
/// Writes outside the grid bounds are silently discarded.
#[derive(Debug, Clone)]
pub struct Grid {
    /// Row-major storage: `cells[row][col]`
    cells: Vec<Vec<char>>,
    /// Parallel obstacle layer: `obstacles[row][col]`
    obstacles: Vec<Vec<Obstacle>>,
    /// Parallel direction-bit layer used by [`Grid::add_dirs`] for junction
    /// merging. Each cell holds the OR of the `DIR_*` bits for every line
    /// segment that has been drawn into it.
    directions: Vec<Vec<u8>>,
    /// Cell-protection flags. Writes via [`Grid::add_dirs`] skip protected
    /// cells so that rounded corners, arrow tips, and node labels survive
    /// any subsequent edge routing that happens to cross them.
    protected: Vec<Vec<bool>>,
    /// Optional foreground color per cell. Empty (all `None`) until the
    /// caller paints colors via [`Grid::set_fg`] / [`Grid::paint_fg_rect`].
    /// Consumed only by [`Grid::render_with_colors`].
    fg: Vec<Vec<Option<Rgb>>>,
    /// Optional background color per cell — see [`Grid::fg`].
    bg: Vec<Vec<Option<Rgb>>>,
    /// Hyperlink URL index per cell. `None` means no hyperlink on this cell;
    /// `Some(idx)` indexes into [`Grid::hyperlink_urls`].
    ///
    /// Populated by [`Grid::paint_hyperlink`] when a `click` directive is
    /// present. Consumed by [`Grid::render`] and [`Grid::render_with_colors`]
    /// to emit OSC 8 escape sequences around the linked text runs.
    hyperlink: Vec<Vec<Option<u32>>>,
    /// Deduplicated URL strings for hyperlinks. Indexed by the values in
    /// [`Grid::hyperlink`]. Grows lazily as URLs are interned via
    /// [`Grid::paint_hyperlink`].
    hyperlink_urls: Vec<String>,
    /// Total columns.
    width: usize,
    /// Total rows.
    height: usize,
}

impl Grid {
    /// Construct a new grid filled with spaces.
    ///
    /// # Arguments
    ///
    /// * `width`  — number of columns
    /// * `height` — number of rows
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            cells: vec![vec![' '; width]; height],
            obstacles: vec![vec![Obstacle::Free; width]; height],
            directions: vec![vec![0u8; width]; height],
            protected: vec![vec![false; width]; height],
            fg: vec![vec![None; width]; height],
            bg: vec![vec![None; width]; height],
            hyperlink: vec![vec![None; width]; height],
            hyperlink_urls: Vec::new(),
            width,
            height,
        }
    }

    /// Return the number of rows in this grid.
    pub(crate) fn rows(&self) -> usize {
        self.height
    }

    /// Return the number of columns in this grid.
    pub(crate) fn cols(&self) -> usize {
        self.width
    }

    /// OR the given direction bits into the cell at `(col, row)` and update
    /// the cell's glyph from the direction-to-char lookup table.
    ///
    /// Protected cells (rounded corners, arrow tips, labels) are left alone —
    /// their glyph is preserved and the direction bits are not recorded.
    /// Out-of-bounds writes are silently ignored.
    pub(crate) fn add_dirs(&mut self, col: usize, row: usize, bits: u8) {
        if row >= self.height || col >= self.width {
            return;
        }
        // Protected cells with NO direction bits are label text, arrow tips,
        // or rounded corners — leave them alone. Protected cells WITH
        // direction bits are box-drawing lines (subgraph border edges
        // explicitly seed their bits via `seed_border_dirs` so an edge
        // crossing one can OR in its own direction and produce a proper
        // junction glyph (┴ ┬ ├ ┤ ┼) instead of the bare border line.
        if self.protected[row][col] && self.directions[row][col] == 0 {
            return;
        }
        self.directions[row][col] |= bits;
        self.cells[row][col] = DIR_TO_CHAR[self.directions[row][col] as usize];
    }

    /// Record direction bits at `(col, row)` *without* writing the glyph or
    /// honouring protection. Used by [`crate::render::unicode`]'s subgraph
    /// border drawer to seed the bit map at border-line cells: subsequent
    /// edge-routing writes via [`add_dirs`] then OR their own direction bits
    /// in, turning the crossing into a proper junction (`┴`/`┬`/`├`/`┤`/`┼`)
    /// instead of leaving the bare border line in place.
    pub fn seed_border_dirs(&mut self, col: usize, row: usize, bits: u8) {
        if row < self.height && col < self.width {
            self.directions[row][col] |= bits;
        }
    }

    /// Clear all direction bits at `(col, row)` without touching the glyph or
    /// protection flag.
    ///
    /// Used by the subgraph-border drawer after writing label text into the top
    /// border row: `seed_border_dirs` had previously seeded `DIR_LEFT|DIR_RIGHT`
    /// on every top-border cell (including the cells that will hold label chars).
    /// When edge routing later calls `add_dirs` on a protected cell, it only
    /// skips the update when `directions == 0`.  Without clearing, those seeded
    /// bits cause `add_dirs` to bypass protection and overwrite the title
    /// character with a junction glyph (`┼`/`┬`/`┴`), corrupting the label (B-
    /// title bug).  Clearing the bits restores the invariant that protected label
    /// cells have `directions == 0` and are therefore immune to `add_dirs`.
    pub(crate) fn clear_dirs(&mut self, col: usize, row: usize) {
        if row < self.height && col < self.width {
            self.directions[row][col] = 0;
        }
    }

    /// Mark a cell as protected — subsequent [`Grid::add_dirs`] calls will
    /// not touch it. Used for rounded corners, arrow tips, and label text
    /// that must survive edge routing.
    fn protect(&mut self, col: usize, row: usize) {
        if row < self.height && col < self.width {
            self.protected[row][col] = true;
        }
    }

    /// Mark all cells of a node bounding box as hard obstacles.
    ///
    /// This must be called for every node *before* routing any edges so that
    /// A\* routing can avoid node boxes.
    ///
    /// # Arguments
    ///
    /// * `col`, `row` — top-left corner of the node box
    /// * `w`, `h`     — bounding-box dimensions (including border cells)
    pub fn mark_node_box(&mut self, col: usize, row: usize, w: usize, h: usize) {
        for dy in 0..h {
            for dx in 0..w {
                let r = row + dy;
                let c = col + dx;
                if r < self.height && c < self.width {
                    self.obstacles[r][c] = Obstacle::NodeBox;
                }
            }
        }
    }

    /// Mark a single cell as a hard obstacle (equivalent to a node-box cell).
    ///
    /// Used by the subgraph renderer to mark border cells so A\* routing
    /// avoids routing edges through the subgraph border lines.
    pub fn mark_obstacle(&mut self, col: usize, row: usize) {
        if row < self.height && col < self.width {
            self.obstacles[row][col] = Obstacle::NodeBox;
        }
    }

    /// Mark every currently-`Free` cell inside the rectangular area
    /// `[col, col+w) × [row, row+h)` as `InnerArea`. Cells already
    /// classified as `NodeBox` or `EdgeOccupiedHorizontal/Vertical` are
    /// left untouched (their classifications are stronger). Used by the
    /// renderer to flag the bounding-box interior so back-edge A* routing
    /// knows to prefer the perimeter outside this rectangle.
    pub fn mark_inner_area(&mut self, col: usize, row: usize, w: usize, h: usize) {
        for dy in 0..h {
            let r = row + dy;
            if r >= self.height {
                break;
            }
            for dx in 0..w {
                let c = col + dx;
                if c >= self.width {
                    break;
                }
                if self.obstacles[r][c] == Obstacle::Free {
                    self.obstacles[r][c] = Obstacle::InnerArea;
                }
            }
        }
    }

    /// Expose the internal `protect` method as a public API.
    ///
    /// Protected cells are skipped by the direction-bit canvas writer so that
    /// subgraph border characters and labels survive subsequent edge routing.
    pub fn protect_cell(&mut self, col: usize, row: usize) {
        self.protect(col, row);
    }

    /// Remove the protection flag from a cell so that subsequent writes
    /// (including the direction-bit canvas writer) can modify it again.
    ///
    /// Used after [`Grid::route_edge`] places a tip glyph that we want to
    /// replace (e.g. converting an arrow tip to a circle endpoint or removing
    /// it for plain no-arrow lines).
    pub fn unprotect_cell(&mut self, col: usize, row: usize) {
        if row < self.height && col < self.width {
            self.protected[row][col] = false;
        }
    }

    /// Recompute the glyph for cell `(col, row)` from its direction-bit mask.
    ///
    /// Call this after [`Grid::unprotect_cell`] to let the direction-bit canvas
    /// produce the correct box-drawing character for a cell whose protection
    /// was previously holding a different glyph (e.g. an arrow tip that should
    /// now be a path character because the edge has no endpoint marker).
    pub fn recompute_cell_glyph(&mut self, col: usize, row: usize) {
        if row < self.height && col < self.width {
            let bits = self.directions[row][col];
            self.cells[row][col] = DIR_TO_CHAR[bits as usize];
        }
    }

    /// Write `ch` at position `(col, row)`.
    ///
    /// Out-of-bounds writes are silently ignored.
    pub fn set(&mut self, col: usize, row: usize, ch: char) {
        if row < self.height && col < self.width {
            self.cells[row][col] = ch;
        }
    }

    /// Write `ch` at position `(col, row)` *unless* the cell is protected.
    ///
    /// Used by box-drawing primitives to lay down border glyphs without
    /// overwriting cells already claimed by arrow tips, label text, or
    /// other survive-edge-routing content. The unconditional [`set`]
    /// variant is preserved for primitives that legitimately need to
    /// stomp (corners, recomputed glyphs, etc.).
    pub fn set_unless_protected(&mut self, col: usize, row: usize, ch: char) {
        if row < self.height && col < self.width && !self.protected[row][col] {
            self.cells[row][col] = ch;
        }
    }

    /// Return `true` if the cell at `(col, row)` is a hard obstacle (NodeBox).
    ///
    /// Used by the router's fast-path checks to detect whether a straight or
    /// L-shaped route is clear before committing to a full A\* search.
    /// Out-of-bounds cells are treated as obstacles so routes don't wander off
    /// the grid edge.
    pub(crate) fn is_node_box(&self, col: usize, row: usize) -> bool {
        if row >= self.height || col >= self.width {
            return true; // treat out-of-bounds as impassable
        }
        self.obstacles[row][col] == Obstacle::NodeBox
    }

    /// Return `true` if a routed path cell can visibly stamp direction bits.
    ///
    /// Protected cells with zero direction bits are reserved for labels,
    /// rounded corners, or tips and would silently drop `add_dirs` writes.
    /// Protected cells that already carry direction bits are border-line cells
    /// and remain legal path cells because later writes merge into junctions.
    pub(crate) fn can_draw_path_cell(&self, col: usize, row: usize) -> bool {
        if row >= self.height || col >= self.width {
            return false;
        }
        if self.obstacles[row][col] == Obstacle::NodeBox {
            return false;
        }
        !self.protected[row][col] || self.directions[row][col] != 0
    }

    /// Return a soft-obstacle weight for cell `(col, row)`.
    ///
    /// Used by the router's L-route cost estimation to compare two bend
    /// orientations without running a full A\*. Returns 0 for free/InnerArea
    /// cells and 1 for already-edge-occupied cells (directional variant
    /// doesn't matter for gross cost comparison). Returns `u32::MAX / 2` for
    /// NodeBox cells so that the router treats a blocked L-route as
    /// infinitely expensive.
    ///
    /// Out-of-bounds cells return `u32::MAX / 2` (treated as blocked).
    pub(crate) fn edge_occupied_cost(&self, col: usize, row: usize) -> u32 {
        if row >= self.height || col >= self.width {
            return u32::MAX / 2;
        }
        match self.obstacles[row][col] {
            Obstacle::Free | Obstacle::InnerArea => 0,
            Obstacle::EdgeOccupiedHorizontal | Obstacle::EdgeOccupiedVertical => 1,
            Obstacle::NodeBox => u32::MAX / 2,
        }
    }

    /// Read the character at `(col, row)`, returning `' '` for out-of-bounds.
    pub fn get(&self, col: usize, row: usize) -> char {
        if row < self.height && col < self.width {
            self.cells[row][col]
        } else {
            ' '
        }
    }

    // -----------------------------------------------------------------------
    // Color layer
    // -----------------------------------------------------------------------

    /// Paint the foreground color of cell `(col, row)`. Out-of-bounds writes
    /// are silently ignored.
    pub fn set_fg(&mut self, col: usize, row: usize, c: Rgb) {
        if row < self.height && col < self.width {
            self.fg[row][col] = Some(c);
        }
    }

    /// Paint the background color of cell `(col, row)`.
    pub fn set_bg(&mut self, col: usize, row: usize, c: Rgb) {
        if row < self.height && col < self.width {
            self.bg[row][col] = Some(c);
        }
    }

    /// Paint the foreground color over every cell in the rectangle anchored at
    /// `(col, row)` with size `w × h`. Cells outside the grid are skipped.
    pub fn paint_fg_rect(&mut self, col: usize, row: usize, w: usize, h: usize, c: Rgb) {
        for dy in 0..h {
            for dx in 0..w {
                self.set_fg(col + dx, row + dy, c);
            }
        }
    }

    /// Paint the background color over every cell in the rectangle anchored at
    /// `(col, row)` with size `w × h`.
    pub fn paint_bg_rect(&mut self, col: usize, row: usize, w: usize, h: usize, c: Rgb) {
        for dy in 0..h {
            for dx in 0..w {
                self.set_bg(col + dx, row + dy, c);
            }
        }
    }

    /// Paint the foreground color along every cell of `path`. `path` is a list
    /// of `(col, row)` pairs as produced by the A\* edge router.
    pub fn paint_fg_path(&mut self, path: &[(usize, usize)], c: Rgb) {
        for &(col, row) in path {
            self.set_fg(col, row, c);
        }
    }

    /// Record `url` as the hyperlink for all cells in the rectangle anchored at
    /// `(col, row)` with size `w × h`. Cells outside the grid are skipped.
    ///
    /// Identical URLs are interned: if the same URL string was already painted
    /// somewhere else on the grid, the existing index is reused so the
    /// `hyperlink_urls` table stays compact.
    ///
    /// Consumed by [`Grid::render`] and [`Grid::render_with_colors`] to emit
    /// OSC 8 hyperlink escape sequences around the linked cell runs.
    pub fn paint_hyperlink(&mut self, col: usize, row: usize, w: usize, h: usize, url: &str) {
        // Intern the URL: find or insert.
        let idx = self
            .hyperlink_urls
            .iter()
            .position(|u| u == url)
            .map(|i| i as u32)
            .unwrap_or_else(|| {
                let i = self.hyperlink_urls.len() as u32;
                self.hyperlink_urls.push(url.to_string());
                i
            });

        for dy in 0..h {
            for dx in 0..w {
                let r = row + dy;
                let c = col + dx;
                if r < self.height && c < self.width {
                    self.hyperlink[r][c] = Some(idx);
                }
            }
        }
    }

    /// Render the grid as a string with embedded ANSI 24-bit truecolor SGR
    /// sequences for any cells with non-`None` `fg`/`bg`.
    ///
    /// Trailing whitespace on each row is trimmed (matching [`std::fmt::Display`]).
    /// SGR runs are coalesced — only color changes between adjacent visible
    /// cells emit an escape sequence — and every row ends with `\x1b[0m` so
    /// the trim and the colors do not interfere.
    ///
    /// If a `click` directive painted hyperlink URLs onto this grid via
    /// [`Grid::paint_hyperlink`], OSC 8 hyperlink escape sequences are also
    /// emitted around the linked cell runs — compatible with iTerm2, kitty,
    /// WezTerm, foot, and other modern terminals. In terminals without OSC 8
    /// support the sequences are harmlessly ignored.
    ///
    /// If no cell carries a color or hyperlink (the default), the output is
    /// byte-for-byte identical to [`std::fmt::Display`]. Callers that want a
    /// hard guarantee of zero ANSI bytes should use [`Grid::render`] instead.
    pub fn render_with_colors(&self) -> String {
        self.render_inner(true)
    }

    /// Convert the grid to a `String`, stripping trailing spaces from each row.
    ///
    /// When `click` directives have painted hyperlink URLs via
    /// [`Grid::paint_hyperlink`], OSC 8 escape sequences are emitted around the
    /// linked label runs so they become clickable in OSC-8-capable terminals.
    ///
    /// Charts with **no** `click` directives produce output that is
    /// byte-for-byte identical to the pre-hyperlink renderer — all existing
    /// snapshot tests continue to pass unchanged.
    pub fn render(&self) -> String {
        if self.hyperlink_urls.is_empty() {
            // Fast path: no hyperlinks — preserve the historical byte-exact
            // output without going through the inner render loop.
            self.to_string()
        } else {
            self.render_inner(false)
        }
    }

    /// Shared rendering loop for [`Grid::render`] and [`Grid::render_with_colors`].
    ///
    /// When `with_color` is `true`, ANSI SGR 24-bit truecolor sequences are
    /// emitted for cells with non-`None` `fg`/`bg`.  Regardless of
    /// `with_color`, OSC 8 hyperlink sequences are emitted whenever the
    /// hyperlink index changes between adjacent cells on the same row — each
    /// row starts with no active hyperlink and the open sequence is always
    /// closed before a newline so that hyperlinks never bleed across rows.
    fn render_inner(&self, with_color: bool) -> String {
        use std::fmt::Write as FmtWrite;

        let has_hyperlinks = !self.hyperlink_urls.is_empty();
        let mut out = String::with_capacity(self.height * (self.width + 32));
        let mut row_buf = String::with_capacity(self.width + 64);

        for row in 0..self.height {
            row_buf.clear();
            let mut current_fg: Option<Rgb> = None;
            let mut current_bg: Option<Rgb> = None;
            let mut any_sgr_in_row = false;
            // `current_hl` tracks the active hyperlink index. `None` means no
            // OSC 8 link is open; `Some(u32::MAX)` is a sentinel for "we
            // explicitly closed a link and are between links on the same row".
            let mut current_hl: Option<u32> = None;

            for col in 0..self.width {
                // --- OSC 8 hyperlink transition ---
                // Emit the close/open sequence whenever the hyperlink changes.
                // We compare `Option<u32>` indices rather than raw URL strings
                // to avoid a string lookup in the hot path.
                if has_hyperlinks {
                    let cell_hl = self.hyperlink[row][col];
                    if cell_hl != current_hl {
                        if current_hl.is_some() {
                            // Close the previous hyperlink.
                            row_buf.push_str("\x1b]8;;\x1b\\");
                        }
                        if let Some(idx) = cell_hl {
                            // Open the new hyperlink.
                            let url = &self.hyperlink_urls[idx as usize];
                            let _ = write!(row_buf, "\x1b]8;;{url}\x1b\\");
                        }
                        current_hl = cell_hl;
                    }
                }

                // --- ANSI SGR color ---
                if with_color {
                    let fg = self.fg[row][col];
                    let bg = self.bg[row][col];
                    if fg != current_fg || bg != current_bg {
                        // Reset before emitting a new combo so transitions from
                        // colored back to uncolored cleanly drop attributes.
                        if any_sgr_in_row {
                            row_buf.push_str("\x1b[0m");
                        }
                        if let Some(Rgb(r, g, b)) = fg {
                            let _ = write!(row_buf, "\x1b[38;2;{r};{g};{b}m");
                            any_sgr_in_row = true;
                        }
                        if let Some(Rgb(r, g, b)) = bg {
                            let _ = write!(row_buf, "\x1b[48;2;{r};{g};{b}m");
                            any_sgr_in_row = true;
                        }
                        current_fg = fg;
                        current_bg = bg;
                    }
                }

                row_buf.push(self.cells[row][col]);
            }

            // Close any open hyperlink before trimming and the row-end reset.
            if has_hyperlinks && current_hl.is_some() {
                row_buf.push_str("\x1b]8;;\x1b\\");
            }

            // Trim trailing ASCII spaces *before* the optional final reset, so
            // padding that the no-color renderer would have stripped does not
            // leak through as visible whitespace once the SGR is stripped.
            while row_buf.ends_with(' ') {
                row_buf.pop();
            }
            if with_color && any_sgr_in_row {
                row_buf.push_str("\x1b[0m");
            }
            out.push_str(&row_buf);
            out.push('\n');
        }

        // Strip the same trailing-blank-line pattern as `Display`.
        while out.ends_with("\n\n") {
            out.pop();
        }
        // Strip leading blank rows too (mirror of `Display::fmt`). ANSI
        // SGR / OSC 8 sequences are emitted INSIDE a row's `row_buf` after
        // any content writes, so an empty row carries no escape bytes —
        // a byte-0 `\n` is therefore unambiguously a blank-row artifact
        // even on the colour path.
        while out.starts_with('\n') {
            out.remove(0);
        }
        out
    }

    // -----------------------------------------------------------------------
    // Box drawing
    // -----------------------------------------------------------------------

    /// Draw a rectangle box with square corners at `(col, row)` with the given
    /// `width` and `height` (in characters, including the border).
    ///
    /// Minimum usable size is 2×2 (all corners, no interior).
    pub fn draw_box(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        // Corners always go down — arrow tips never land on a corner cell.
        self.set(col, row, rect::TL);
        self.set(col + w - 1, row, rect::TR);
        self.set(col, row + h - 1, rect::BL);
        self.set(col + w - 1, row + h - 1, rect::BR);

        // Edge cells use protection-respecting writes so an arrow tip
        // (`▾`, `▴`, `◂`, `▸`) terminating ON the border survives the
        // box redraw — the visible difference between "arrow floating
        // one cell from the box" and "arrow merging into the box edge".
        for x in (col + 1)..(col + w - 1) {
            self.set_unless_protected(x, row, rect::H);
            self.set_unless_protected(x, row + h - 1, rect::H);
        }
        for y in (row + 1)..(row + h - 1) {
            self.set_unless_protected(col, y, rect::V);
            self.set_unless_protected(col + w - 1, y, rect::V);
        }
    }

    /// Draw a rounded-corner box at `(col, row)`.
    pub fn draw_rounded_box(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        // Corners always go down — arrow tips never land on a corner cell.
        self.set(col, row, rounded::TL);
        self.set(col + w - 1, row, rounded::TR);
        self.set(col, row + h - 1, rounded::BL);
        self.set(col + w - 1, row + h - 1, rounded::BR);

        for x in (col + 1)..(col + w - 1) {
            self.set_unless_protected(x, row, rect::H);
            self.set_unless_protected(x, row + h - 1, rect::H);
        }
        for y in (row + 1)..(row + h - 1) {
            self.set_unless_protected(col, y, rect::V);
            self.set_unless_protected(col + w - 1, y, rect::V);
        }
    }

    /// Draw a diamond-style (rhombus) node box using diagonal corner characters.
    ///
    /// The visual style is:
    /// ```text
    /// ╱────────╲
    /// │  label  │
    /// ╲────────╱
    /// ```
    ///
    /// `╱` (U+2571) and `╲` (U+2572) at the four corners clearly distinguish a
    /// rhombus from a plain rectangle at any terminal width.  The horizontal
    /// edges remain `─` and the vertical sides remain `│`, so routing logic
    /// that already understands rectangles continues to work without changes.
    ///
    /// `w` and `h` are the total bounding-box dimensions.
    pub fn draw_diamond(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        // Draw the standard rectangle skeleton first, then overwrite the four
        // corner characters with diagonal glyphs.
        self.draw_box(col, row, w, h);

        // Top corners: ╱ top-left, ╲ top-right.
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        // Bottom corners: ╲ bottom-left, ╱ bottom-right.
        self.set(col, row + h - 1, '');
        self.set(col + w - 1, row + h - 1, '');
    }

    /// Fill a `w × h` rectangular block at `(col, row)` with full-block `█`
    /// glyphs. Internal helper for the multi-cell fork/join synchronisation
    /// bars — gives the same visual weight as Mermaid's SVG `rect` fill
    /// without needing a separate border primitive.
    fn fill_block(&mut self, col: usize, row: usize, w: usize, h: usize) {
        for y in row..(row + h) {
            for x in col..(col + w) {
                self.set(x, y, '');
            }
        }
    }

    /// Draw a multi-row filled horizontal bar at `(col, row)` of length `w`
    /// cells and thickness [`BAR_THICKNESS`] rows. Used for UML fork/join
    /// synchronisation bars in TD/BT-flow state diagrams.
    ///
    /// Bars don't participate in the direction-bit canvas — they're static
    /// character fills, not connectable orthogonal lines.
    pub fn draw_horizontal_bar(&mut self, col: usize, row: usize, w: usize) {
        self.fill_block(col, row, w, BAR_THICKNESS);
    }

    /// Draw a multi-column filled vertical bar at `(col, row)` of length `h`
    /// cells and thickness [`BAR_THICKNESS`] columns. Used for UML fork/join
    /// synchronisation bars in LR/RL-flow state diagrams.
    pub fn draw_vertical_bar(&mut self, col: usize, row: usize, h: usize) {
        self.fill_block(col, row, BAR_THICKNESS, h);
    }

    /// Draw a stadium (capsule/pill) node: rounded box with `(` / `)` markers
    /// replacing the border cells at the vertical midpoint of the left and
    /// right edges.
    ///
    /// The markers overwrite the border characters directly (same pattern as
    /// `NodeShape::Circle`) so the interior label region stays clean and no
    /// literal parens appear inside the text.
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╭─────────╮
    /// (  Stadium  )
    ///  ╰─────────╯
    /// ```
    ///
    /// Mermaid syntax: `([label])`
    pub fn draw_stadium(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 4 || h < 2 {
            return;
        }
        self.draw_rounded_box(col, row, w, h);
        // Overwrite the left and right border cells at the vertical midpoint
        // with `(` / `)`. Placing them ON the border (not one cell inside)
        // keeps the interior label region clear — identical to the Circle fix.
        let mid_row = row + h / 2;
        self.set(col, mid_row, '(');
        self.set(col + w - 1, mid_row, ')');
        self.protect(col, mid_row);
        self.protect(col + w - 1, mid_row);
    }

    /// Draw a subroutine node: rectangle with an extra inner vertical bar (`│`)
    /// one cell inside each left and right border.
    ///
    /// Mermaid syntax: `[[label]]`
    pub fn draw_subroutine(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 4 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // Place inner vertical bars for all interior rows.
        for y in (row + 1)..(row + h - 1) {
            self.set(col + 1, y, rect::V);
            self.set(col + w - 2, y, rect::V);
        }
    }

    /// Draw a cylinder (database) node: a rounded rectangle with an interior
    /// "lip" line one row below the top border to suggest a barrel/cylinder cap.
    ///
    /// The lip is drawn with `─` characters only (no `├`/`┤` T-junctions) so
    /// it reads as a decorative depth cue rather than a dividing partition.
    ///
    /// Rendered appearance (4-row example):
    /// ```text
    ///  ╭──────────╮
    /// │ ──────── │
    /// │ Database │
    ///  ╰──────────╯
    /// ```
    ///
    /// Minimum height is 4 rows: top border + lip row + text row + bottom border.
    /// For multi-line labels, `h` grows by the number of extra label lines.
    ///
    /// Mermaid syntax: `[(label)]`
    pub fn draw_cylinder(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 4 || h < 4 {
            return;
        }
        // Draw the full rounded outline first.
        self.draw_rounded_box(col, row, w, h);
        // Interior lip: `─` characters at row+1, inset by 2 cells on each side
        // so they sit visually "inside" the border without touching the walls.
        // This avoids the misleading `├`/`┤` T-junction glyphs that made the
        // previous rendering look like a split-panel divider.
        for x in (col + 2)..(col + w - 2) {
            self.set(x, row + 1, rect::H);
        }
    }

    /// Draw a hexagon node: rectangle with slanted `╱`/`╲` corner glyphs at all
    /// four corners plus `<` / `>` markers at the vertical midpoint of the left
    /// and right edges.
    ///
    /// This gives 6 visual edges: two horizontal (top/bottom between the slanted
    /// corners), two slanted diagonals (the four corners), and two side points
    /// (`<`/`>`), approximating a true hexagon in monospace.
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╱─────────╲
    /// <  Hexagon  >
    ///  ╲─────────╱
    /// ```
    ///
    /// Mermaid syntax: `{{label}}`
    pub fn draw_hexagon(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 4 || h < 2 {
            return;
        }
        // Start with the standard rectangle skeleton (horizontal/vertical edges).
        self.draw_box(col, row, w, h);
        // Overwrite the four corners with diagonal glyphs (same as `draw_diamond`).
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        self.set(col, row + h - 1, '');
        self.set(col + w - 1, row + h - 1, '');
        // Overwrite left/right border cells at the vertical midpoint with
        // `<` / `>` to suggest the protruding hex side-points.
        let mid_row = row + h / 2;
        self.set(col, mid_row, '<');
        self.set(col + w - 1, mid_row, '>');
        self.protect(col, mid_row);
        self.protect(col + w - 1, mid_row);
    }

    /// Draw an asymmetric (flag) node: rectangle with a `⟩` marker at the
    /// vertical midpoint of the right border.
    ///
    /// Mermaid syntax: `>label]`
    pub fn draw_asymmetric(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // Replace the vertical midpoint of the right border with `⟩`.
        let mid_row = row + h / 2;
        self.set(col + w - 1, mid_row, '');
        self.protect(col + w - 1, mid_row);
    }

    /// Draw a parallelogram (lean-right) node: rectangle with `╱` at all four
    /// corners. Both the top and bottom horizontal edges terminate with the same
    /// slant direction, giving the parallelogram silhouette.
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╱─────────────────╱
    /// │  Parallelogram  │
    ///  ╱─────────────────╱
    /// ```
    ///
    /// Mermaid syntax: `[/label/]`
    pub fn draw_parallelogram(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // Overwrite all four corners with `╱` so both horizontal edges lean
        // consistently rightward — the defining trait of a lean-right parallelogram.
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        self.set(col, row + h - 1, '');
        self.set(col + w - 1, row + h - 1, '');
    }

    /// Draw a trapezoid (wider top) node: rectangle with `╱` at top-left and
    /// `╲` at top-right. The bottom corners remain square, giving the trapezoid
    /// its characteristic "hat" silhouette.
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╱─────────────╲
    /// │  Trapezoid    │
    ///  └─────────────┘
    /// ```
    ///
    /// Mermaid syntax: `[/label\]`
    pub fn draw_trapezoid(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // Slant markers at top corners only — `╱` left, `╲` right.
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        self.protect(col, row);
        self.protect(col + w - 1, row);
    }

    /// Draw a parallelogram-backslash (lean-left) node: rectangle with `╲` at
    /// all four corners. Both horizontal edges lean left, the mirror image of
    /// [`draw_parallelogram`].
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╲─────────────────╲
    /// │  BackSlash       │
    ///  ╲─────────────────╲
    /// ```
    ///
    /// Mermaid syntax: `[\label\]`
    pub fn draw_parallelogram_backslash(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // All four corners are `╲` — consistent leftward lean.
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        self.set(col, row + h - 1, '');
        self.set(col + w - 1, row + h - 1, '');
    }

    /// Draw an inverted trapezoid (wider bottom) node: rectangle with `╲` at
    /// top-left and `╱` at top-right. The bottom corners remain square.
    ///
    /// Rendered appearance (3-row example):
    /// ```text
    ///  ╲─────────────╱
    /// │  InvTrap      │
    ///  └─────────────┘
    /// ```
    ///
    /// Mermaid syntax: `[\label/]`
    pub fn draw_trapezoid_inverted(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 2 || h < 2 {
            return;
        }
        self.draw_box(col, row, w, h);
        // Slant markers at top corners only — `╲` left, `╱` right (mirror of trapezoid).
        self.set(col, row, '');
        self.set(col + w - 1, row, '');
        self.protect(col, row);
        self.protect(col + w - 1, row);
    }

    /// Draw a double-circle node: two concentric rounded boxes, with the inner
    /// one drawn 1 cell inside the outer on all sides.
    ///
    /// Minimum useful size is 5 wide × 5 tall to leave a visible inner ring.
    ///
    /// Mermaid syntax: `(((label)))`
    pub fn draw_double_circle(&mut self, col: usize, row: usize, w: usize, h: usize) {
        if w < 5 || h < 5 {
            return;
        }
        // Outer rounded box.
        self.draw_rounded_box(col, row, w, h);
        // Inner rounded box, 1 cell inside on all sides.
        self.draw_rounded_box(col + 1, row + 1, w - 2, h - 2);
    }

    // -----------------------------------------------------------------------
    // Text writing
    // -----------------------------------------------------------------------

    /// Write `text` starting at `(col, row)`.
    ///
    /// Each character advances the column by its display width (via
    /// `unicode-width`), so multi-byte characters are handled correctly.
    pub fn write_text(&mut self, col: usize, row: usize, text: &str) {
        let mut x = col;
        for ch in text.chars() {
            if x >= self.width {
                break;
            }
            self.set(x, row, ch);
            // Advance by Unicode display width (most chars = 1, CJK = 2)
            x += UnicodeWidthChar::width(ch).unwrap_or(1);
        }
    }

    /// Write `text` starting at `(col, row)` and protect every cell written
    /// so that subsequent direction-bit canvas writes (from edge routing) cannot
    /// overwrite the label characters.
    ///
    /// Use this for edge labels that must survive later routing passes.
    pub fn write_text_protected(&mut self, col: usize, row: usize, text: &str) {
        let mut x = col;
        for ch in text.chars() {
            if x >= self.width {
                break;
            }
            self.set(x, row, ch);
            self.protect(x, row);
            x += UnicodeWidthChar::width(ch).unwrap_or(1);
        }
    }

    // -----------------------------------------------------------------------
    // Arrow / path drawing
    // -----------------------------------------------------------------------

    /// Draw a horizontal line with an arrow tip at the right end.
    ///
    /// Draws `─` from `(col1, row)` to `(col2-1, row)` then `▸` at `col2`.
    /// If `col1 >= col2` nothing is drawn.
    pub fn draw_h_arrow(&mut self, col1: usize, row: usize, col2: usize) {
        if col1 >= col2 {
            return;
        }
        for x in col1..col2 {
            self.add_dirs(x, row, DIR_LEFT | DIR_RIGHT);
        }
        self.set(col2, row, arrow::RIGHT);
        self.protect(col2, row);
    }

    /// Draw a vertical line with an arrow tip at the bottom.
    ///
    /// Draws `│` from `(col, row1)` to `(col, row2-1)` then `▾` at `row2`.
    /// If `row1 >= row2` nothing is drawn.
    pub fn draw_v_arrow(&mut self, col: usize, row1: usize, row2: usize) {
        if row1 >= row2 {
            return;
        }
        for y in row1..row2 {
            self.add_dirs(col, y, DIR_UP | DIR_DOWN);
        }
        self.set(col, row2, arrow::DOWN);
        self.protect(col, row2);
    }

    /// Draw a right-angle path from `(col1, row1)` to `(col2, row2)`.
    ///
    /// For horizontal-primary flow (LR/RL): horizontal segment first, then
    /// vertical. The corner is drawn as a junction character. An arrow tip
    /// is placed at the destination.
    ///
    /// For vertical-primary flow (TD/BT): vertical segment first, then
    /// horizontal.
    ///
    /// `horizontal_first` controls which axis is traversed first.
    pub fn draw_manhattan(
        &mut self,
        col1: usize,
        row1: usize,
        col2: usize,
        row2: usize,
        horizontal_first: bool,
        arrow_direction: char,
    ) {
        if col1 == col2 && row1 == row2 {
            return;
        }

        if horizontal_first {
            // Horizontal segment from (col1, row1) up to (but not including)
            // the corner at (col2, row1).
            if col1 != col2 {
                let (lo, hi) = order(col1, col2);
                for x in lo..hi {
                    self.add_dirs(x, row1, DIR_LEFT | DIR_RIGHT);
                }
            }

            if row1 == row2 {
                // Pure horizontal — arrow tip at the destination end.
                self.set(col2, row2, arrow_direction);
                self.protect(col2, row2);
            } else {
                // Corner at (col2, row1): incoming-horizontal side + outgoing-vertical side.
                let h_in = if col2 > col1 { DIR_LEFT } else { DIR_RIGHT };
                let v_out = if row2 > row1 { DIR_DOWN } else { DIR_UP };
                self.add_dirs(col2, row1, h_in | v_out);

                // Vertical segment between the corner and the tip (exclusive of both).
                let (vlo, vhi) = order(row1, row2);
                // `order` always gives (min, max). The corner sits at the min or max
                // depending on direction; the line cells are strictly between them.
                for y in (vlo + 1)..vhi {
                    self.add_dirs(col2, y, DIR_UP | DIR_DOWN);
                }

                self.set(col2, row2, arrow_direction);
                self.protect(col2, row2);
            }
        } else {
            // Vertical segment up to (but not including) the corner at (col1, row2).
            if row1 != row2 {
                let (lo, hi) = order(row1, row2);
                for y in lo..hi {
                    self.add_dirs(col1, y, DIR_UP | DIR_DOWN);
                }
            }

            if col1 == col2 {
                self.set(col2, row2, arrow_direction);
                self.protect(col2, row2);
            } else {
                let v_in = if row2 > row1 { DIR_UP } else { DIR_DOWN };
                let h_out = if col2 > col1 { DIR_RIGHT } else { DIR_LEFT };
                self.add_dirs(col1, row2, v_in | h_out);

                let (hlo, hhi) = order(col1, col2);
                for x in (hlo + 1)..hhi {
                    self.add_dirs(x, row2, DIR_LEFT | DIR_RIGHT);
                }

                self.set(col2, row2, arrow_direction);
                self.protect(col2, row2);
            }
        }
    }

    // -----------------------------------------------------------------------
    // A* obstacle-aware edge routing
    // -----------------------------------------------------------------------

    /// Route an edge from `(col1, row1)` to `(col2, row2)` using A\* pathfinding
    /// and draw the result on the grid with box-drawing characters.
    ///
    /// The router:
    /// - Treats `NodeBox` cells as impassable hard obstacles.
    /// - Applies a soft penalty (`EDGE_SOFT_COST = 2.0`) when crossing cells
    ///   already occupied by another edge, to reduce clutter.
    /// - Applies a corner penalty (`CORNER_PENALTY = 0.5`) when the routing
    ///   direction changes, to favour straighter paths.
    ///
    /// After finding the path, the method draws it using `─`/`│` for straight
    /// segments and junction characters at corners, placing the arrow tip at
    /// the destination.
    ///
    /// If A\* cannot find any path (e.g. the destination is completely
    /// surrounded by obstacles), the method falls back to the simple Manhattan
    /// routing used by [`Grid::draw_manhattan`].
    ///
    /// # Arguments
    ///
    /// * `col1`, `row1` — source cell (just outside the source node border)
    /// * `col2`, `row2` — destination cell (just outside the destination node
    ///   border, where the arrow tip will be placed)
    /// * `horizontal_first` — hint: prefer horizontal movement first (LR/RL
    ///   flows). A\* may still deviate when obstacles block the preferred path.
    /// * `arrow_direction` — arrow tip character placed at `(col2, row2)`
    ///
    /// # Returns
    ///
    /// The full pixel path as `(col, row)` pairs from source to destination,
    /// including the arrow-tip cell. Returns `None` only when both endpoints
    /// are the same cell.
    pub fn route_edge(
        &mut self,
        col1: usize,
        row1: usize,
        col2: usize,
        row2: usize,
        horizontal_first: bool,
        arrow_direction: char,
    ) -> Option<Vec<(usize, usize)>> {
        // Forward edges treat InnerArea cells as free space — the
        // shortest path through the diagram body is fine when the
        // edge naturally lives there.
        self.route_edge_with_inner_cost(
            col1,
            row1,
            col2,
            row2,
            horizontal_first,
            arrow_direction,
            0.0,
        )
    }

    /// Route a back-edge that should prefer the perimeter over a
    /// shortcut through the diagram body. Same parameters as
    /// [`route_edge`] but charges a hefty penalty for crossing
    /// `InnerArea` cells (the bounding-box interior between nodes),
    /// steering the path outward to the corridor reserved by the
    /// canvas-bounds calculation.
    pub fn route_back_edge(
        &mut self,
        col1: usize,
        row1: usize,
        col2: usize,
        row2: usize,
        horizontal_first: bool,
        arrow_direction: char,
    ) -> Option<Vec<(usize, usize)>> {
        // Tuned high enough to push back-edges to the perimeter when
        // a clean path exists, but not so high that A* refuses to
        // take a shortcut when no perimeter route is reachable.
        // 8.0 is roughly 2× `EDGE_SOFT_COST` so an InnerArea cell
        // crossing costs about the same as crossing two existing
        // edges — meaningful but not prohibitive.
        const BACK_EDGE_INNER_COST: f32 = 8.0;
        self.route_edge_with_inner_cost(
            col1,
            row1,
            col2,
            row2,
            horizontal_first,
            arrow_direction,
            BACK_EDGE_INNER_COST,
        )
    }

    /// Shared A* core for [`route_edge`] and [`route_back_edge`].
    /// `inner_area_cost` is added per cell when the path crosses an
    /// `Obstacle::InnerArea` (zero for forward edges).
    #[allow(clippy::too_many_arguments)]
    fn route_edge_with_inner_cost(
        &mut self,
        col1: usize,
        row1: usize,
        col2: usize,
        row2: usize,
        horizontal_first: bool,
        arrow_direction: char,
        inner_area_cost: f32,
    ) -> Option<Vec<(usize, usize)>> {
        // Cost constants.
        //
        // Direction-aware crossing costs (tuned from graph-easy):
        //   SAME_AXIS_COST  — penalty for a new edge running *along* the same
        //     axis as an already-routed edge in a cell (e.g. two horizontal
        //     lines sharing a cell). Hard to read; A* prefers a fresh
        //     column/row but will still share when no alternative is reachable
        //     without a very long detour (kept at 10 rather than graph-easy's
        //     30 so that bidirectional pairs in tight subgraphs don't route
        //     outside the subgraph box).
        //   CROSS_AXIS_COST — penalty for a new edge *crossing* an existing
        //     edge perpendicularly (producing `┼`). Visually acceptable; a
        //     low cost lets A* take a clean crossing instead of a long detour.
        const SAME_AXIS_COST: f32 = 10.0;
        const CROSS_AXIS_COST: f32 = 3.0;
        const CORNER_PENALTY: f32 = 0.5;
        // 4-directional movement: Right, Down, Left, Up (indices 0..3).
        const DIRS: [(isize, isize); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)];

        if col1 == col2 && row1 == row2 {
            return None;
        }

        // Manhattan distance heuristic (admissible — never overestimates).
        let h = |c: usize, r: usize| -> f32 { (c.abs_diff(col2) + r.abs_diff(row2)) as f32 };

        // `came_from[row][col]` encodes the direction we arrived from
        // (0–3) or `u8::MAX` for unvisited.  We also store the g_cost.
        let mut g_cost: Vec<Vec<f32>> = vec![vec![f32::INFINITY; self.width]; self.height];
        let mut came_from: Vec<Vec<u8>> = vec![vec![u8::MAX; self.width]; self.height];

        g_cost[row1][col1] = 0.0;

        let mut open: BinaryHeap<AstarNode> = BinaryHeap::new();
        // Preferred initial direction based on `horizontal_first`.
        let start_dir = if horizontal_first { 0u8 } else { 1u8 };
        open.push(AstarNode {
            f_cost: h(col1, row1),
            g_cost: 0.0,
            col: col1,
            row: row1,
            dir: start_dir,
        });

        'outer: while let Some(current) = open.pop() {
            // Skip stale entries (a cheaper path was already found).
            if current.g_cost > g_cost[current.row][current.col] {
                continue;
            }

            if current.col == col2 && current.row == row2 {
                break 'outer;
            }

            for (dir_idx, &(dc, dr)) in DIRS.iter().enumerate() {
                let nc = current.col.wrapping_add_signed(dc);
                let nr = current.row.wrapping_add_signed(dr);
                if nc >= self.width || nr >= self.height {
                    continue;
                }
                // Hard obstacle check.
                if self.obstacles[nr][nc] == Obstacle::NodeBox {
                    // Allow the destination cell even if it is marked as a
                    // node box (the tip sits on the node border).
                    if nc != col2 || nr != row2 {
                        continue;
                    }
                }

                // Base step cost.
                let mut step = 1.0f32;
                // Direction-aware edge-crossing cost.
                // Moving in direction `dir_idx`: 0=Right, 1=Down, 2=Left, 3=Up.
                // Directions 0/2 are horizontal; 1/3 are vertical.
                let moving_horizontal = dir_idx == 0 || dir_idx == 2;
                match self.obstacles[nr][nc] {
                    Obstacle::EdgeOccupiedHorizontal => {
                        step += if moving_horizontal {
                            SAME_AXIS_COST // overlap — strongly avoid
                        } else {
                            CROSS_AXIS_COST // clean cross — acceptable
                        };
                    }
                    Obstacle::EdgeOccupiedVertical => {
                        step += if moving_horizontal {
                            CROSS_AXIS_COST // clean cross — acceptable
                        } else {
                            SAME_AXIS_COST // overlap — strongly avoid
                        };
                    }
                    _ => {}
                }
                // InnerArea penalty (back-edges only; zero for forward
                // edges): biases A* to prefer the perimeter corridor
                // over a shortcut through the diagram body.
                if self.obstacles[nr][nc] == Obstacle::InnerArea {
                    step += inner_area_cost;
                }
                // Corner penalty: direction change from previous step.
                if dir_idx as u8 != current.dir {
                    step += CORNER_PENALTY;
                }

                let new_g = current.g_cost + step;
                if new_g < g_cost[nr][nc] {
                    g_cost[nr][nc] = new_g;
                    // Store the direction of the move INTO (nr, nc) — the
                    // reconstruction walks back by reversing this vector.
                    came_from[nr][nc] = dir_idx as u8;
                    open.push(AstarNode {
                        f_cost: new_g + h(nc, nr),
                        g_cost: new_g,
                        col: nc,
                        row: nr,
                        dir: dir_idx as u8,
                    });
                }
            }
        }

        // Reconstruct path by walking `came_from` backwards from the goal.
        if came_from[row2][col2] == u8::MAX && (col1 != col2 || row1 != row2) {
            // A* found no path — fall back to simple Manhattan routing.
            self.draw_manhattan(col1, row1, col2, row2, horizontal_first, arrow_direction);
            // Return a two-point path for label placement.
            return Some(vec![(col1, row1), (col2, row2)]);
        }

        // Collect waypoints (in reverse order, then reverse).
        let mut path: Vec<(usize, usize)> = Vec::new();
        let mut cc = col2;
        let mut cr = row2;
        path.push((cc, cr));
        while cc != col1 || cr != row1 {
            let dir = came_from[cr][cc];
            if dir == u8::MAX {
                break;
            }
            // `came_from` stores the direction of the move INTO this cell —
            // stepping back means reversing that vector.
            let (dc, dr) = DIRS[dir as usize];
            cc = cc.wrapping_add_signed(-dc);
            cr = cr.wrapping_add_signed(-dr);
            path.push((cc, cr));
        }
        path.reverse();

        // Draw the path on the grid.
        self.draw_routed_path(&path, arrow_direction);
        Some(path)
    }

    /// Overwrite the glyphs of an already-drawn path with a different line style.
    ///
    /// This must be called **after** [`Grid::route_edge`] has drawn the path
    /// using solid glyphs and populated the direction-bit canvas. The method
    /// walks the path (excluding the tip cell, which is handled separately) and
    /// replaces each non-protected cell's glyph according to `style`:
    ///
    /// - [`EdgeLineStyle::Solid`] — no-op (already solid).
    /// - [`EdgeLineStyle::Dotted`] — single-direction cells become `┄`/`┆`;
    ///   multi-direction junction cells are left as solid (see `dotted` module).
    /// - [`EdgeLineStyle::Thick`] — all cells are recomputed from the
    ///   direction-bit canvas using `THICK_DIR_TO_CHAR`.
    ///
    /// The `tip` and `back_tip` cells must be placed by the caller after this
    /// call — they are not in `path_cells` (the path slice passed here should
    /// exclude the terminal arrow cell).
    pub fn overdraw_path_style(&mut self, path_cells: &[(usize, usize)], style: EdgeLineStyle) {
        if style == EdgeLineStyle::Solid {
            return;
        }
        for &(c, r) in path_cells {
            if r >= self.height || c >= self.width {
                continue;
            }
            if self.protected[r][c] {
                continue;
            }
            let bits = self.directions[r][c];
            match style {
                EdgeLineStyle::Solid => {}
                EdgeLineStyle::Dotted => {
                    // Only single-axis cells (pure horizontal or pure vertical)
                    // get dotted glyphs; junctions stay solid to avoid
                    // mismatched box-drawing characters.
                    self.cells[r][c] = match bits {
                        0b0001..=0b0011 => dotted::V,          // any vertical-only
                        0b0100 | 0b1000 | 0b1100 => dotted::H, // any horizontal-only
                        _ => DIR_TO_CHAR[bits as usize],       // junction → stay solid
                    };
                }
                EdgeLineStyle::Thick => {
                    self.cells[r][c] = THICK_DIR_TO_CHAR[bits as usize];
                }
            }
        }
    }

    /// Draw a pre-computed list of `(col, row)` waypoints as box-drawing
    /// chars using the direction-bit canvas.
    ///
    /// For each waypoint, the direction bits pointing toward its path
    /// neighbors (previous and next) are OR'd into the cell; the
    /// direction-to-char table then produces the correct glyph — straight
    /// segments render as `─`/`│`, turns render as corner chars, and
    /// whenever another edge has already painted the same cell the result
    /// merges naturally into a T-junction (`├┤┬┴`) or cross (`┼`).
    ///
    /// The final waypoint is overwritten with the arrow tip and protected
    /// so later edges can't erase it. Each drawn cell is marked as
    /// [`Obstacle::EdgeOccupiedHorizontal`] or
    /// [`Obstacle::EdgeOccupiedVertical`] based on the cell's axis in the
    /// path, so subsequent edges pay a lower cost for perpendicular crossings
    /// than for same-axis overlaps.
    fn draw_routed_path(&mut self, path: &[(usize, usize)], tip: char) {
        if path.len() < 2 {
            return;
        }
        let last = path.len() - 1;

        for i in 0..=last {
            let (c, r) = path[i];

            // Mark as edge-occupied with the correct axis variant so that
            // subsequent edges pay a lower penalty for perpendicular crossings
            // (6) than for same-axis overlaps (30). We determine the axis from
            // the path segments adjacent to this cell — at a corner cell the
            // cell has both H and V neighbors, so we classify by the outbound
            // direction (toward the next cell) which is the dominant segment.
            if r < self.height && c < self.width && self.obstacles[r][c] != Obstacle::NodeBox {
                let obstacle = if i < last {
                    let (nc, nr) = path[i + 1];
                    // At corners, use the outbound direction for classification.
                    if nc != c {
                        Obstacle::EdgeOccupiedHorizontal
                    } else if nr != r {
                        Obstacle::EdgeOccupiedVertical
                    } else {
                        // Same cell (degenerate) — keep whatever was there.
                        self.obstacles[r][c]
                    }
                } else {
                    // Arrow-tip cell: classify by the inbound direction.
                    let (pc, _) = path[i - 1];
                    if pc != c {
                        Obstacle::EdgeOccupiedHorizontal
                    } else {
                        Obstacle::EdgeOccupiedVertical
                    }
                };
                // Only upgrade Free / InnerArea cells — don't downgrade an
                // already-classified EdgeOccupied* to the wrong axis.
                if !matches!(
                    self.obstacles[r][c],
                    Obstacle::EdgeOccupiedHorizontal | Obstacle::EdgeOccupiedVertical
                ) {
                    self.obstacles[r][c] = obstacle;
                }
            }

            if i == last {
                // Arrow tip — fixed glyph, protected against later merges.
                self.set(c, r, tip);
                self.protect(c, r);
                continue;
            }

            let mut bits = 0u8;
            if i > 0 {
                let (pc, pr) = path[i - 1];
                bits |= neighbor_bit(c, r, pc, pr);
            }
            let (nc, nr) = path[i + 1];
            bits |= neighbor_bit(c, r, nc, nr);
            self.add_dirs(c, r, bits);
        }
    }

    /// Inverse of [`draw_routed_path`]: subtract `path`'s direction-bit
    /// contributions from each cell so the cell re-renders with whatever
    /// other paths' bits survive. Used by the post-routing nudging pass
    /// (`crate::layout::nudge`) before re-drawing a shifted path.
    ///
    /// # Algorithm
    ///
    /// For each cell `(c, r)` in `path`:
    /// 1. Compute the bits this path contributed at the cell using the
    ///    same `neighbor_bit` derivation as `draw_routed_path`. Source
    ///    cell (i=0) contributes only the next-direction bit; tip cell
    ///    (i=last) contributes none (tip is set as a glyph, not via
    ///    direction bits — special-cased below); interior cells
    ///    contribute prev-bit OR next-bit.
    /// 2. Defensive guard: only subtract if `directions[r][c] &
    ///    our_bits == our_bits` — i.e., all our bits are present. If
    ///    protected-with-zero-bits (label text, rounded corners) blocked
    ///    the original `add_dirs`, our bits aren't there and we leave
    ///    the cell alone. This preserves protected glyphs that we
    ///    couldn't have stamped onto in the first place.
    /// 3. Subtract: `directions[r][c] &= !our_bits`; rewrite glyph from
    ///    LUT[surviving_bits] (mirrors `add_dirs`'s glyph derivation).
    /// 4. Tip cell: unprotect, then either blank (if no surviving bits
    ///    from other paths) or recompute glyph from survivors.
    ///
    /// # Obstacle layer (`EdgeOccupied*`)
    ///
    /// Left UNTOUCHED. The obstacle layer is read-only after `route_all`
    /// returns — only A\* consumes it, and A\* finishes before the
    /// nudging pass runs. Staleness is invisible to the renderer. If
    /// future code reads obstacles after nudging, this assumption breaks
    /// and a per-cell ref counter would be needed.
    #[allow(dead_code)] // Wired in by `crate::layout::nudge` in Phase C.
    pub(crate) fn erase_path(&mut self, path: &[(usize, usize)]) {
        if path.len() < 2 {
            return;
        }
        let last = path.len() - 1;
        for i in 0..=last {
            let (c, r) = path[i];
            if c >= self.width || r >= self.height {
                continue;
            }
            if i == last {
                self.protected[r][c] = false;
                if self.directions[r][c] == 0 {
                    self.cells[r][c] = ' ';
                } else {
                    self.cells[r][c] = DIR_TO_CHAR[self.directions[r][c] as usize];
                }
                continue;
            }
            let mut our_bits = 0u8;
            if i > 0 {
                let (pc, pr) = path[i - 1];
                our_bits |= neighbor_bit(c, r, pc, pr);
            }
            let (nc, nr) = path[i + 1];
            our_bits |= neighbor_bit(c, r, nc, nr);
            if our_bits != 0 && self.directions[r][c] & our_bits == our_bits {
                self.directions[r][c] &= !our_bits;
                self.cells[r][c] = DIR_TO_CHAR[self.directions[r][c] as usize];
            }
        }
    }

    /// Draw a pre-computed path of `(col, row)` waypoints on the grid and
    /// return the path.  The final waypoint receives the arrow `tip` glyph and
    /// is protected against overwriting by later edges.
    ///
    /// This is the crate-visible entry point for custom routing strategies
    /// (e.g. `try_u_route` in `router.rs`) that build their own waypoint lists
    /// without going through A\*.  The path must contain at least two cells and
    /// must not pass through any `NodeBox` cell — the caller is responsible for
    /// that precondition.
    pub(crate) fn draw_path(
        &mut self,
        path: Vec<(usize, usize)>,
        tip: char,
    ) -> Option<Vec<(usize, usize)>> {
        if path.len() < 2 {
            return None;
        }
        self.draw_routed_path(&path, tip);
        Some(path)
    }
}

// ---------------------------------------------------------------------------
// Display impl
// ---------------------------------------------------------------------------

impl std::fmt::Display for Grid {
    /// Format the grid as a multi-line string, stripping trailing spaces.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut out = String::with_capacity(self.height * (self.width + 1));
        for row in &self.cells {
            let line: String = row.iter().collect();
            out.push_str(line.trim_end());
            out.push('\n');
        }
        // Remove trailing blank lines
        while out.ends_with("\n\n") {
            out.pop();
        }
        // Mirror of the trailing-trim above: strip leading blank rows.
        // The Sugiyama backend reserves a top corridor for back-edge
        // routing that often goes unused, leaving 1–5 empty rows above
        // the first content row. Any byte-0 `\n` is unambiguously a
        // blank-row artifact because each `out.push_str(line.trim_end())`
        // pushes content first, then `\n` — so a leading `\n` can only
        // come from a row whose `trim_end()` produced an empty string.
        while out.starts_with('\n') {
            out.remove(0);
        }
        write!(f, "{out}")
    }
}

// ---------------------------------------------------------------------------
// Free helpers
// ---------------------------------------------------------------------------

/// Sort `(a, b)` into `(min, max)` ascending.
fn order(a: usize, b: usize) -> (usize, usize) {
    if a <= b { (a, b) } else { (b, a) }
}

/// Return the direction bit that points from cell `(c, r)` toward cell
/// `(nc, nr)` — `DIR_LEFT` if the neighbor is to the left, `DIR_RIGHT` if to
/// the right, etc. Returns `0` if the coordinates are equal or diagonal (the
/// latter should never happen in orthogonal routing).
fn neighbor_bit(c: usize, r: usize, nc: usize, nr: usize) -> u8 {
    if nc < c {
        DIR_LEFT
    } else if nc > c {
        DIR_RIGHT
    } else if nr < r {
        DIR_UP
    } else if nr > r {
        DIR_DOWN
    } else {
        0
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn grid_set_and_get() {
        let mut g = Grid::new(5, 5);
        g.set(2, 3, 'X');
        assert_eq!(g.get(2, 3), 'X');
        assert_eq!(g.get(0, 0), ' ');
    }

    #[test]
    fn out_of_bounds_ignored() {
        let mut g = Grid::new(3, 3);
        g.set(10, 10, 'X'); // should not panic
        assert_eq!(g.get(10, 10), ' ');
    }

    #[test]
    fn draw_box_corners() {
        let mut g = Grid::new(10, 5);
        g.draw_box(0, 0, 5, 3);
        assert_eq!(g.get(0, 0), '');
        assert_eq!(g.get(4, 0), '');
        assert_eq!(g.get(0, 2), '');
        assert_eq!(g.get(4, 2), '');
    }

    #[test]
    fn write_text_respects_width() {
        let mut g = Grid::new(20, 3);
        g.write_text(1, 1, "Hello");
        assert_eq!(g.get(1, 1), 'H');
        assert_eq!(g.get(5, 1), 'o');
    }

    #[test]
    fn to_string_strips_trailing_spaces() {
        let g = Grid::new(10, 2);
        let s = g.to_string();
        for line in s.lines() {
            assert!(!line.ends_with(' '));
        }
    }

    #[test]
    fn erase_path_clears_isolated_segment() {
        let mut g = Grid::new(10, 5);
        let path = vec![(2, 2), (3, 2), (4, 2), (5, 2)];
        g.draw_routed_path(&path, '');
        // Sanity: path was drawn.
        assert_eq!(g.get(3, 2), '');
        assert_eq!(g.get(5, 2), '');
        g.erase_path(&path);
        // All cells blanked.
        for (c, r) in &path {
            assert_eq!(g.get(*c, *r), ' ', "cell ({c},{r}) not cleared after erase");
        }
        // Tip cell is unprotected — adding direction bits should write a glyph.
        g.add_dirs(5, 2, DIR_LEFT | DIR_RIGHT);
        assert_eq!(g.get(5, 2), '');
    }

    #[test]
    fn erase_path_preserves_shared_junction() {
        let mut g = Grid::new(10, 5);
        // Horizontal path at row 2.
        let h_path = vec![(1, 2), (2, 2), (3, 2), (4, 2)];
        g.draw_routed_path(&h_path, '');
        // Vertical path through (2, 2). draw_routed_path stamps only via
        // direction bits at interior cells; (2, 2) becomes a junction
        // when the vertical path's bits OR with the horizontal's.
        let v_path = vec![(2, 0), (2, 1), (2, 2), (2, 3)];
        g.draw_routed_path(&v_path, '');
        // Junction at (2, 2) — horizontal LEFT|RIGHT plus vertical UP|DOWN = ┼.
        assert_eq!(g.get(2, 2), '');
        // Erase the horizontal. Vertical's UP|DOWN bits survive at (2,2).
        g.erase_path(&h_path);
        assert_eq!(g.get(2, 2), '', "junction collapsed to vertical bit only");
        // (1, 2) and (3, 2) are pure-horizontal cells — fully blanked.
        assert_eq!(g.get(1, 2), ' ');
        assert_eq!(g.get(3, 2), ' ');
        // Vertical path's other cells unaffected.
        assert_eq!(g.get(2, 1), '');
    }

    #[test]
    fn erase_path_handles_tip_unprotect() {
        let mut g = Grid::new(10, 5);
        let path = vec![(2, 2), (3, 2), (4, 2)];
        g.draw_routed_path(&path, '');
        assert_eq!(g.get(4, 2), '');
        g.erase_path(&path);
        assert_eq!(g.get(4, 2), ' ');
        // Tip is unprotected: a subsequent add_dirs writes through.
        g.add_dirs(4, 2, DIR_LEFT | DIR_RIGHT);
        assert_eq!(g.get(4, 2), '');
    }

    #[test]
    fn draw_h_arrow_places_tip() {
        let mut g = Grid::new(20, 3);
        g.draw_h_arrow(2, 1, 8);
        assert_eq!(g.get(8, 1), arrow::RIGHT);
        assert_eq!(g.get(2, 1), '');
    }

    #[test]
    fn draw_v_arrow_places_tip() {
        let mut g = Grid::new(10, 10);
        g.draw_v_arrow(3, 1, 5);
        assert_eq!(g.get(3, 5), arrow::DOWN);
        assert_eq!(g.get(3, 1), '');
    }
}