limu 0.2.0

A diagram layout engine. It reads Mermaid and leaves the drawing to your program.
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
//! A block diagram laid out as a scene, as Pele's renderBlock in render.ts draws it: the
//! blocks in the columns of their composites, arrow blocks, and the edges between blocks,
//! with the labels of edges between the same two blocks stacked where they would cover each
//! other.

use std::collections::HashMap;
use std::f64::consts::PI;

use super::arrows::{OVERHANG, arrow_axis, arrow_mask, arrow_size, draw_arrow};
use super::db::{BlockDb, ROOT};
use super::layout::{Cell, layout as lay_cells};
use crate::common::{class_names, setting, tighten, title_label, title_room};
use crate::css::{Resolved, declarations, opaque, resolve_style};
use crate::draw::{
    EdgePath, Fill, Tip, casing, draw_shape, label_patch, label_patch_box, marker_trim, on_fills,
    shape_inset, shape_size,
};
use crate::errors::Error;
use crate::js::chars::trim;
use crate::js::math::{asin, atan2, cos, hypot, max, min, or1, round, sign, sin};
use crate::layout::Options;
use crate::scene::style::{Cap, Join, Paint, Style};
use crate::scene::text::{Face, Label};
use crate::scene::{
    Edge, Item, Kind as ItemKind, MarkerType, Painting, Part, Path, Placed, Point, Rect, Scene,
    Shape,
};
use crate::text::layout_label;
use crate::value::{Map, Value};

/// The roles of the items of a block diagram. A casing lies under an edge that crosses the
/// border of a fill an author sets, drawn in the background so that the edge stands out on
/// the fill.
pub(crate) const ROLE_TITLE: &str = "block.title";
pub(crate) const ROLE_COMPOSITE: &str = "block.composite";
pub(crate) const ROLE_BLOCK: &str = "block.block";
pub(crate) const ROLE_EDGE: &str = "block.edge";
pub(crate) const ROLE_EDGE_LABEL: &str = "block.edge-label";
pub(crate) const ROLE_CASING: &str = "block.casing";

const GAP: f64 = 12.0;
const WRAP: f64 = 200.0;
const CORNER: f64 = 8.0;
/// How many checks of an edge against a block a diagram may take. Checking every edge
/// against every block is quadratic, so large diagrams route without them.
const OBSTACLE_BUDGET: usize = 2_000_000;
/// The room between labels stacked along a line, Pele's LABEL_GAP.
const LABEL_GAP: f64 = 4.0;
/// The places along a leg a label is tried at, in turn, Pele's SPOTS.
const SPOTS: [f64; 5] = [0.5, 0.3, 0.7, 0.15, 0.85];

/// The shape Pele draws a type of block with, Pele's SHAPES. Any other is a rect.
fn shape_of(kind: &str) -> &'static str {
    match kind {
        "square" => "rect",
        "round" => "rounded",
        "circle" => "circle",
        "doublecircle" => "dbl-circ",
        "diamond" => "diam",
        "hexagon" => "hex",
        "stadium" => "stadium",
        "subroutine" => "fr-rect",
        "cylinder" => "cyl",
        "rect_left_inv_arrow" => "odd",
        "lean_right" => "lean-r",
        "lean_left" => "lean-l",
        "trapezoid" => "trap-b",
        "inv_trapezoid" => "trap-t",
        _ => "rect",
    }
}

/// Whether a shape keeps its own size in a larger cell, where the others stretch to fill it,
/// Pele's FIXED.
fn fixed(shape: &str) -> bool {
    matches!(shape, "circle" | "dbl-circ" | "diam")
}

/// Whether a shape only widens, because its slants and curves are set by its height, Pele's
/// WIDE.
fn wide(shape: &str) -> bool {
    matches!(
        shape,
        "stadium" | "hex" | "odd" | "lean-r" | "lean-l" | "trap-b" | "trap-t"
    )
}

/// The kinds of view, Pele's Kind.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Kind {
    Leaf,
    Arrow,
    Composite,
    Space,
}

/// A block as it is drawn, Pele's View, beside the cell it is placed in, which has the same
/// index: what it is drawn with, and where it is once placed.
struct View {
    block: usize,
    kind: Kind,
    index: usize,
    shape: &'static str,
    label: Label,
    /// The declarations of the block's classes and styles.
    decls: Vec<String>,
    style: Resolved,
    mask: u8,
    dx: f64,
    dy: f64,
    // The center and the size of the cell, as the last layout placed it.
    x: f64,
    y: f64,
    w: f64,
    h: f64,
    /// The size as drawn, which for a fixed shape is smaller than its cell.
    bw: f64,
    bh: f64,
}

/// The box of a container's title, which stacked labels keep clear of, Pele's Box.
struct TitleBox {
    x: f64,
    y: f64,
    bw: f64,
    bh: f64,
}

/// An edge as it is drawn, Pele's Route: two points for a straight line, three for a bend,
/// none for a self-loop.
struct Route {
    edge: usize,
    points: Vec<f64>,
    looped: usize,
    label: Label,
    lx: f64,
    ly: f64,
}

fn is_round(v: &View) -> bool {
    v.kind == Kind::Leaf && (v.shape == "circle" || v.shape == "dbl-circ")
}

/// Where a vertical line at x meets the outline, below the center (sign 1) or above it (sign
/// -1), as Pele's edgeY finds it.
fn edge_y(v: &View, x: f64, sign: f64) -> f64 {
    let hw = v.bw / 2.0;
    let hh = v.bh / 2.0;
    let dx = (x - v.x).abs();
    let mut reach = hh;
    if is_round(v) {
        reach = max(hw * hw - dx * dx, 0.0).sqrt();
    } else if v.kind == Kind::Leaf && v.shape == "diam" {
        reach = hh * max(1.0 - dx / hw, 0.0);
    } else if v.kind == Kind::Leaf {
        let side = if sign > 0.0 { 2 } else { 0 };
        reach = hh - shape_inset(v.shape, v.bw, v.bh, side, 0.0);
    } else if v.kind == Kind::Arrow && arrow_axis(v.mask) == Some('x') {
        reach = hh - OVERHANG;
    }
    v.y + sign * reach
}

/// Where a horizontal line at y meets the outline, right of the center (sign 1) or left of
/// it (sign -1), as Pele's edgeX finds it.
fn edge_x(v: &View, y: f64, sign: f64) -> f64 {
    let hw = v.bw / 2.0;
    let hh = v.bh / 2.0;
    let dy = (y - v.y).abs();
    let mut reach = hw;
    if is_round(v) {
        reach = max(hh * hh - dy * dy, 0.0).sqrt();
    } else if v.kind == Kind::Leaf && v.shape == "diam" {
        reach = hw * max(1.0 - dy / hh, 0.0);
    } else if v.kind == Kind::Leaf {
        let side = if sign > 0.0 { 1 } else { 3 };
        reach = hw - shape_inset(v.shape, v.bw, v.bh, side, 0.0);
    } else if v.kind == Kind::Arrow && arrow_axis(v.mask) == Some('y') {
        reach = hw - OVERHANG;
    }
    v.x + sign * reach
}

/// Where the line from the center toward tx, ty leaves the outline, as Pele's border finds
/// it, added to out.
fn border(v: &View, tx: f64, ty: f64, out: &mut Vec<f64>) {
    let dx = tx - v.x;
    let dy = ty - v.y;
    let hw = v.bw / 2.0;
    let hh = v.bh / 2.0;
    let mut k = if is_round(v) {
        hw / or1(hypot(dx, dy))
    } else if v.kind == Kind::Leaf && v.shape == "diam" {
        1.0 / or1(dx.abs() / hw + dy.abs() / hh)
    } else {
        min(
            if dx == 0.0 {
                f64::INFINITY
            } else {
                hw / dx.abs()
            },
            if dy == 0.0 {
                f64::INFINITY
            } else {
                hh / dy.abs()
            },
        )
    };
    if k == f64::INFINITY {
        k = 0.0;
    }
    out.extend([v.x + dx * k, v.y + dy * k]);
}

/// Whether the segment from x1, y1 to x2, y2 crosses the box of v, a little inside its
/// outline, as Pele's crossesBox says.
fn crosses_box(x1: f64, y1: f64, x2: f64, y2: f64, v: &View) -> bool {
    let l = v.x - v.bw / 2.0 + 1.0;
    let r = v.x + v.bw / 2.0 - 1.0;
    let t = v.y - v.bh / 2.0 + 1.0;
    let b = v.y + v.bh / 2.0 - 1.0;
    if (x1 < l && x2 < l) || (x1 > r && x2 > r) || (y1 < t && y2 < t) || (y1 > b && y2 > b) {
        return false;
    }
    if x1 == x2 || y1 == y2 {
        return true;
    }
    // The segment is diagonal and its bounding box overlaps: the box is hit unless all four
    // corners are on one side.
    let dx = x2 - x1;
    let dy = y2 - y1;
    let side = |x: f64, y: f64| sign(dx * (y - y1) - dy * (x - x1));
    let first = side(l, t);
    first == 0.0 || side(r, t) != first || side(r, b) != first || side(l, b) != first
}

/// How many of the obstacles other than s and t a path through points crosses, as Pele's
/// hits counts them.
fn hits(points: &[f64], s: usize, t: usize, views: &[View], obstacles: &[usize]) -> usize {
    let mut count = 0;
    for &v in obstacles {
        if v == s || v == t {
            continue;
        }
        let mut i = 0;
        while i + 3 < points.len() {
            if crosses_box(
                points[i],
                points[i + 1],
                points[i + 2],
                points[i + 3],
                &views[v],
            ) {
                count += 1;
                break;
            }
            i += 2;
        }
    }
    count
}

/// The path of an edge from s to t, as Pele's routeEdge finds it: a straight line where the
/// two blocks share a row or a column, otherwise one bend between facing sides, or a straight
/// line between their outlines where that crosses fewer blocks. obstacles is none where the
/// diagram is too large to check.
fn route_edge(
    views: &[View],
    si: usize,
    ti: usize,
    shift: f64,
    obstacles: Option<&[usize]>,
) -> Vec<f64> {
    let (s, t) = (&views[si], &views[ti]);
    let sl = s.x - s.bw / 2.0;
    let sr = s.x + s.bw / 2.0;
    let st = s.y - s.bh / 2.0;
    let sb = s.y + s.bh / 2.0;
    let tl = t.x - t.bw / 2.0;
    let tr = t.x + t.bw / 2.0;
    let tt = t.y - t.bh / 2.0;
    let tb = t.y + t.bh / 2.0;
    let x1 = max(sl, tl);
    let x2 = min(sr, tr);
    let y1 = max(st, tt);
    let y2 = min(sb, tb);
    let over_x = x2 - x1 > 6.0;
    let over_y = y2 - y1 > 6.0;

    if over_x && over_y {
        // One block is inside the other: join the inner one to the nearest side of the
        // outer one.
        let inner_is_s = s.bw * s.bh <= t.bw * t.bh;
        let (inner, outer) = if inner_is_s { (s, t) } else { (t, s) };
        let up = inner.y - inner.bh / 2.0 - (outer.y - outer.bh / 2.0);
        let down = outer.y + outer.bh / 2.0 - (inner.y + inner.bh / 2.0);
        let left = inner.x - inner.bw / 2.0 - (outer.x - outer.bw / 2.0);
        let right = outer.x + outer.bw / 2.0 - (inner.x + inner.bw / 2.0);
        let least = min(min(min(up, down), left), right);
        let points = if least == down {
            vec![
                inner.x,
                edge_y(inner, inner.x, 1.0),
                inner.x,
                outer.y + outer.bh / 2.0,
            ]
        } else if least == up {
            vec![
                inner.x,
                edge_y(inner, inner.x, -1.0),
                inner.x,
                outer.y - outer.bh / 2.0,
            ]
        } else if least == right {
            vec![
                edge_x(inner, inner.y, 1.0),
                inner.y,
                outer.x + outer.bw / 2.0,
                inner.y,
            ]
        } else {
            vec![
                edge_x(inner, inner.y, -1.0),
                inner.y,
                outer.x - outer.bw / 2.0,
                inner.y,
            ]
        };
        return if inner_is_s {
            points
        } else {
            vec![points[2], points[3], points[0], points[1]]
        };
    }
    if over_x {
        let margin = min(6.0, (x2 - x1) / 2.0);
        let x = min(max((x1 + x2) / 2.0 + shift, x1 + margin), x2 - margin);
        let down = if t.y > s.y { 1.0 } else { -1.0 };
        return vec![x, edge_y(s, x, down), x, edge_y(t, x, -down)];
    }
    if over_y {
        let margin = min(6.0, (y2 - y1) / 2.0);
        let y = min(max((y1 + y2) / 2.0 + shift, y1 + margin), y2 - margin);
        let right = if t.x > s.x { 1.0 } else { -1.0 };
        return vec![edge_x(s, y, right), y, edge_x(t, y, -right), y];
    }

    let right = if t.x > s.x { 1.0 } else { -1.0 };
    let down = if t.y > s.y { 1.0 } else { -1.0 };
    let across = vec![
        edge_x(s, s.y, right),
        s.y,
        t.x,
        s.y,
        t.x,
        edge_y(t, t.x, -down),
    ];
    let along = vec![
        s.x,
        edge_y(s, s.x, down),
        s.x,
        t.y,
        edge_x(t, t.y, -right),
        t.y,
    ];
    let wide = (t.x - s.x).abs() >= (t.y - s.y).abs();
    let (mut best, other) = if wide {
        (across, along)
    } else {
        (along, across)
    };
    let Some(obstacles) = obstacles else {
        return best;
    };
    let mut least = hits(&best, si, ti, views, obstacles);
    if least == 0 {
        return best;
    }
    let count = hits(&other, si, ti, views, obstacles);
    if count < least {
        best = other;
        least = count;
    }
    if least == 0 {
        return best;
    }
    let mut direct = Vec::new();
    border(s, t.x, t.y, &mut direct);
    border(t, s.x, s.y, &mut direct);
    if hits(&direct, si, ti, views, obstacles) < least {
        direct
    } else {
        best
    }
}

fn tip(x: f64, y: f64, dx: f64, dy: f64) -> Tip {
    Tip {
        at: Point::new(x, y),
        dx,
        dy,
    }
}

/// The path through two or three points, its bend rounded and room left for markers, as
/// Pele's trace builds it.
fn trace(p: &[f64], mut start_trim: f64, mut end_trim: f64) -> EdgePath {
    let n = p.len();
    let (sx, sy) = (p[0], p[1]);
    let (ex, ey) = (p[n - 2], p[n - 1]);
    let mut ax = p[2] - sx;
    let mut ay = p[3] - sy;
    let first = hypot(ax, ay);
    ax /= or1(first);
    ay /= or1(first);
    let mut bx = ex - p[n - 4];
    let mut by = ey - p[n - 3];
    let last = hypot(bx, by);
    bx /= or1(last);
    by /= or1(last);
    if n == 4 && start_trim + end_trim > first {
        let scale = first / (start_trim + end_trim);
        start_trim *= scale;
        end_trim *= scale;
    }
    start_trim = min(start_trim, first);
    end_trim = min(end_trim, last);
    let mut path = Path::new();
    path.move_to(sx + ax * start_trim, sy + ay * start_trim);
    if n == 6 {
        let r = max(0.0, min(min(CORNER, first - start_trim), last - end_trim));
        path.line_to(p[2] - ax * r, p[3] - ay * r);
        path.quad_to(p[2], p[3], p[2] + bx * r, p[3] + by * r);
    }
    path.line_to(ex - bx * end_trim, ey - by * end_trim);
    EdgePath {
        path,
        start: tip(sx, sy, -ax, -ay),
        end: tip(ex, ey, bx, by),
    }
}

/// The arc of the kth self-loop on a block, as Pele's loopArc gives it: around the top
/// right corner of the block, or around the matching point on a round or diamond outline. It
/// gives the arc's center and radius and the angles where it leaves and returns.
fn loop_arc(v: &View, k: usize) -> (f64, f64, f64, f64, f64) {
    let hw = v.bw / 2.0;
    let hh = v.bh / 2.0;
    let r = max(1.0, min(min(10.0 + 5.0 * (k as f64 - 1.0), hw), hh));
    if is_round(v) {
        let px = v.x + hw * std::f64::consts::FRAC_1_SQRT_2;
        let py = v.y - hw * std::f64::consts::FRAC_1_SQRT_2;
        let half = 2.0 * asin(min(1.0, r / (2.0 * hw)));
        let angle = |at: f64| atan2(v.y - hw * sin(at) - py, v.x + hw * cos(at) - px);
        return (px, py, r, angle(PI / 4.0 + half), angle(PI / 4.0 - half));
    }
    if v.kind == Kind::Leaf && v.shape == "diam" {
        let slope = atan2(hh, hw);
        return (v.x + hw / 2.0, v.y - hh / 2.0, r, slope + PI, slope);
    }
    (v.x + hw, v.y - hh, r, PI, PI / 2.0)
}

/// The path of the kth self-loop on a block, as Pele's loopPath draws it: an arc, with each
/// marker along the chord it covers, so that it follows the curve instead of the tangent at
/// its tip.
fn loop_path(v: &View, k: usize, start_trim: f64, end_trim: f64) -> EdgePath {
    let (px, py, r, a1, a2) = loop_arc(v, k);
    let from = a1 + start_trim / r;
    let to = a2 - end_trim / r;
    let mut sweep = (to - from) % (2.0 * PI);
    if sweep <= 0.0 {
        sweep += 2.0 * PI;
    }
    let x1 = px + r * cos(from);
    let y1 = py + r * sin(from);
    let x2 = px + r * cos(to);
    let y2 = py + r * sin(to);
    let sx = px + r * cos(a1);
    let sy = py + r * sin(a1);
    let ex = px + r * cos(a2);
    let ey = py + r * sin(a2);
    let sl = hypot(sx - x1, sy - y1);
    let el = hypot(ex - x2, ey - y2);
    let mut path = Path::new();
    path.move_to(x1, y1)
        .arc_to(r, r, 0.0, sweep > PI, true, x2, y2);
    EdgePath {
        path,
        start: if sl > 0.0 {
            tip(sx, sy, (sx - x1) / sl, (sy - y1) / sl)
        } else {
            tip(sx, sy, sin(a1), -cos(a1))
        },
        end: if el > 0.0 {
            tip(ex, ey, (ex - x2) / el, (ey - y2) / el)
        } else {
            tip(ex, ey, -sin(a2), cos(a2))
        },
    }
}

/// What a label is kept clear of: the blocks other than the two an edge joins, and the
/// titles of containers, as Pele's obstacles are. It is none where the diagram is too large
/// to check, and then a label covers nothing.
#[derive(Clone, Copy)]
struct Blocked<'a> {
    views: &'a [View],
    obstacles: &'a [usize],
    titles: &'a [TitleBox],
}

/// Whether a label w by h centered on x, y covers a block other than s and t, or a title, as
/// Pele's covers says.
fn covers(x: f64, y: f64, w: f64, h: f64, s: usize, t: usize, blocked: Option<Blocked>) -> bool {
    let Some(b) = blocked else {
        return false;
    };
    for &i in b.obstacles {
        let v = &b.views[i];
        if i != s
            && i != t
            && (x - v.x).abs() < (w + v.bw) / 2.0
            && (y - v.y).abs() < (h + v.bh) / 2.0
        {
            return true;
        }
    }
    b.titles
        .iter()
        .any(|v| (x - v.x).abs() < (w + v.bw) / 2.0 && (y - v.y).abs() < (h + v.bh) / 2.0)
}

/// The point along the leg of p from at where a label w by h covers no block other than the
/// two it joins, as Pele's labelSpot picks it.
fn label_spot(
    p: &[f64],
    at: usize,
    w: f64,
    h: f64,
    s: usize,
    t: usize,
    blocked: Option<Blocked>,
) -> f64 {
    if blocked.is_none() {
        return 0.5;
    }
    for spot in SPOTS {
        let x = p[at] + (p[at + 2] - p[at]) * spot;
        let y = p[at + 1] + (p[at + 3] - p[at + 1]) * spot;
        if !covers(x, y, w, h, s, t, blocked) {
            return spot;
        }
    }
    0.5
}

/// Stacks the labels of the edges of stack, which join the same two blocks, along the line
/// they share where they cover each other, if they fit between its markers and cover no
/// other block or title, as Pele's stackLabels does. It gives how much longer the line would
/// have to be for them to fit, or 0.
fn stack_labels(
    stack: &[usize],
    routes: &mut [Route],
    legs: &[Vec<f64>],
    s: usize,
    t: usize,
    blocked: Option<Blocked>,
) -> f64 {
    let vertical = legs[stack[0]][0] == legs[stack[0]][2];
    let mut lo = f64::NEG_INFINITY;
    let mut hi = f64::INFINITY;
    let mut need = -LABEL_GAP;
    let mut crowded = false;
    let along = |r: &Route| {
        if vertical {
            r.label.height
        } else {
            r.label.width + 8.0
        }
    };
    for (a, &i) in stack.iter().enumerate() {
        let leg = &legs[i];
        if if vertical {
            leg[0] != leg[2]
        } else {
            leg[1] != leg[3]
        } {
            return 0.0;
        }
        if vertical {
            lo = max(lo, min(leg[1], leg[3]));
            hi = min(hi, max(leg[1], leg[3]));
        } else {
            lo = max(lo, min(leg[0], leg[2]));
            hi = min(hi, max(leg[0], leg[2]));
        }
        let p = &routes[i];
        need += along(p) + LABEL_GAP;
        for &j in &stack[..a] {
            let q = &routes[j];
            if (p.lx - q.lx).abs() < (p.label.width + q.label.width) / 2.0 + 8.0
                && (p.ly - q.ly).abs() < (p.label.height + q.label.height) / 2.0
            {
                crowded = true;
            }
        }
    }
    if !crowded {
        return 0.0;
    }
    let room = hi - lo - 2.0 * crate::scene::MARKER_SIZE;
    if need > room {
        return need - room;
    }
    let mut places = Vec::with_capacity(stack.len());
    let mut next = (lo + hi - need) / 2.0;
    for &i in stack {
        let r = &routes[i];
        let size = along(r);
        let at = next + size / 2.0;
        let (x, y) = if vertical { (r.lx, at) } else { (at, r.ly) };
        if covers(x, y, r.label.width + 8.0, r.label.height, s, t, blocked) {
            return 0.0;
        }
        places.push(at);
        next += size + LABEL_GAP;
    }
    for (k, &i) in stack.iter().enumerate() {
        if vertical {
            routes[i].ly = places[k];
        } else {
            routes[i].lx = places[k];
        }
    }
    0.0
}

/// Widens the gap between s and t in the nearest container they share by short, between rows
/// where vertical is set and between columns otherwise, as Pele's widen does, and says
/// whether there was a gap to widen. base keeps the gaps as they were when the labels were
/// measured, so that widening the same gap again does not add to what it added.
fn widen(
    cells: &mut [Cell],
    parents: &[Option<usize>],
    s: usize,
    t: usize,
    vertical: bool,
    short: f64,
    base: &mut HashMap<(usize, bool), Vec<f64>>,
) -> bool {
    let mut chain = std::collections::HashSet::new();
    let mut v = Some(s);
    while let Some(i) = v {
        chain.insert(i);
        v = parents[i];
    }
    let mut tb = None;
    let mut container = Some(t);
    while let Some(c) = container.filter(|c| !chain.contains(c)) {
        tb = Some(c);
        container = parents[c];
    }
    let (Some(container), Some(tb)) = (container, tb) else {
        return false;
    };
    if container == s {
        return false;
    }
    let mut sb = s;
    while parents[sb] != Some(container) {
        sb = parents[sb].expect("a parent");
    }
    let (sbc, tbc) = (&cells[sb], &cells[tb]);
    let at = if vertical {
        if sbc.row == tbc.row {
            return false;
        }
        max(sbc.row, tbc.row) - 1.0
    } else {
        let (l, r) = if sbc.col < tbc.col {
            (sbc, tbc)
        } else {
            (tbc, sbc)
        };
        if r.col < l.col + l.span {
            return false;
        }
        r.col - 1.0
    } as usize;
    let gaps = if vertical {
        &mut cells[container].row_gaps
    } else {
        &mut cells[container].col_gaps
    };
    let was = base
        .entry((container, vertical))
        .or_insert_with(|| gaps.clone());
    let before = was.get(at).copied().unwrap_or(0.0);
    if gaps.len() <= at {
        gaps.resize(at + 1, 0.0);
    }
    gaps[at] = max(gaps[at], before + short);
    true
}

/// Lays out a parsed block diagram as a scene, as Pele's renderBlock draws it: with room to
/// spare, and tightly instead when that does not fit the host's width and is narrower. Its
/// error is one of a composite of no columns.
pub(crate) fn layout(db: &BlockDb, config: &Map, o: &Options) -> Result<Scene, Error> {
    let mut err = None;
    let sc = tighten(o, |tight| match draw(db, config, o, tight) {
        Ok(sc) => sc,
        Err(e) => {
            err = Some(e);
            Scene::default()
        }
    });
    match err {
        Some(e) => Err(e),
        None => Ok(sc),
    }
}

/// Lays the diagram out once, as Pele's draw does.
fn draw(db: &BlockDb, config: &Map, o: &Options, tight: bool) -> Result<Scene, Error> {
    let size = o.font_size;
    let edge_size = round(size * 0.875);
    let set = setting(o);
    let pad = o.pad();
    let configured = match config
        .get("block")
        .and_then(Value::as_object)
        .and_then(|s| s.get("padding"))
    {
        Some(&Value::Number(v)) if v >= 0.0 => min(v, 200.0),
        _ => GAP,
    };
    let gap = min(configured, if tight { 6.0 } else { f64::INFINITY });
    let wrap = if tight { 80.0 } else { WRAP };

    // The declarations of a block's classes and styles, as Pele's styleOf reads them. A
    // style list is split on commas only, so one entry can still hold several declarations.
    let decls_of = |b: usize| {
        let block = &db.arena[b];
        let mut decls: Vec<String> = Vec::new();
        let mut add = |styles: &[String]| {
            for style in styles {
                decls.extend(style.split(';').map(str::to_string));
            }
        };
        let default = ["default".to_string()];
        let names = match &block.classes {
            Some(c) if !c.is_empty() => c.as_slice(),
            _ => &default,
        };
        for name in names {
            if let Some((_, def)) = db.classes.get(name) {
                add(&def.styles);
            }
        }
        if let Some(styles) = &block.styles {
            add(styles);
        }
        decls
    };

    let view = |b: usize, index: usize| -> (View, Cell) {
        let block = &db.arena[b];
        let kind = block.r#type.as_str();
        let decls = if kind == "space" {
            Vec::new()
        } else {
            decls_of(b)
        };
        let style = resolve_style(&decls);
        let mut face = Face::REGULAR;
        if style.bold {
            face |= Face::BOLD;
        }
        if style.italic {
            face |= Face::ITALIC;
        }
        let font_size = style.font_size.unwrap_or(size);
        let raw = block.label.as_deref().unwrap_or("");
        let mut v = View {
            block: b,
            kind: Kind::Leaf,
            index,
            shape: "",
            label: Label::default(),
            decls: Vec::new(),
            style: Resolved::default(),
            mask: 0,
            dx: 0.0,
            dy: 0.0,
            x: 0.0,
            y: 0.0,
            w: 0.0,
            h: 0.0,
            bw: 0.0,
            bh: 0.0,
        };
        let mut c = Cell::default();
        match kind {
            "space" => v.kind = Kind::Space,
            "composite" => {
                v.kind = Kind::Composite;
                let room = if tight { title_room(o) } else { 4000.0 };
                v.label = layout_label(raw, false, &set, font_size, room, face);
                c.head = if v.label.height > 0.0 {
                    v.label.height + 4.0
                } else {
                    0.0
                };
                c.nw = v.label.width + 2.0 * gap;
                c.nh = c.head + 2.0 * gap;
            }
            "block_arrow" => {
                v.kind = Kind::Arrow;
                v.label = layout_label(raw, false, &set, font_size, wrap, face);
                let blank = v
                    .label
                    .lines
                    .iter()
                    .flatten()
                    .all(|part| part.icon.is_none() && trim(&part.text).is_empty());
                v.mask = arrow_mask(block.directions.as_deref());
                let s = if blank {
                    arrow_size(v.mask, 0.0, 0.0)
                } else {
                    arrow_size(v.mask, v.label.width, v.label.height)
                };
                (c.nw, c.nh, v.dx, v.dy) = (s.w, s.h, s.dx, s.dy);
            }
            _ => {
                v.shape = shape_of(kind);
                v.label = layout_label(raw, false, &set, font_size, wrap, face);
                let s = shape_size(v.shape, v.label.width, v.label.height);
                c.nw = s.w;
                // A cylinder's caps deepen as it widens, so it is sized for the deepest ones.
                c.nh = if v.shape == "cyl" {
                    max(s.h, v.label.height + 40.0)
                } else {
                    s.h
                };
                v.dy = s.dy;
            }
        }
        c.columns = if v.kind == Kind::Composite {
            block.columns.unwrap_or(-1.0)
        } else {
            -1.0
        };
        c.span = if v.kind == Kind::Space {
            1.0
        } else {
            block.width_in_columns.unwrap_or(1.0)
        };
        c.space = v.kind == Kind::Space;
        v.decls = decls;
        v.style = style;
        (v, c)
    };

    let (mut root, mut root_cell) = view(ROOT, 0);
    root_cell.nw = 0.0;
    root_cell.nh = 0.0;
    root.kind = Kind::Composite;
    let mut views = vec![root];
    let mut cells = vec![root_cell];
    let mut parents: Vec<Option<usize>> = vec![None];
    let mut by_id: HashMap<&str, usize> = HashMap::new();
    let mut i = 0;
    while i < views.len() {
        if views[i].kind == Kind::Composite {
            let children = db.arena[views[i].block]
                .children
                .as_deref()
                .unwrap_or_default();
            for &child in children {
                let index = views.len();
                let (v, c) = view(child, index);
                cells[i].children.push(index);
                parents.push(Some(i));
                if v.kind != Kind::Space {
                    by_id.insert(db.arena[child].id.as_deref().unwrap_or(""), index);
                }
                views.push(v);
                cells.push(c);
            }
        }
        i += 1;
    }

    // The layout keeps the widest a container has been, so each run starts from the widths
    // it needs.
    let widths: Vec<f64> = cells.iter().map(|c| c.nw).collect();
    let mut obstacles: Vec<usize> = Vec::new();
    let mut titles: Vec<TitleBox> = Vec::new();
    // Lays the blocks out in their gaps, as Pele's place does.
    let place = |views: &mut [View],
                 cells: &mut [Cell],
                 obstacles: &mut Vec<usize>,
                 titles: &mut Vec<TitleBox>|
     -> Result<(f64, f64), Error> {
        for (c, &w) in cells.iter_mut().zip(&widths) {
            c.nw = w;
        }
        let content = lay_cells(cells, gap)?;
        obstacles.clear();
        titles.clear();
        for (v, c) in views.iter_mut().zip(cells.iter()) {
            (v.x, v.y, v.w, v.h) = (c.x, c.y, c.w, c.h);
            if v.kind == Kind::Leaf && fixed(v.shape) {
                v.bw = c.nw;
                v.bh = c.nh;
            } else if v.kind == Kind::Arrow {
                v.bw = if c.span > 1.0 { c.w } else { c.nw };
                v.bh = c.nh;
            } else {
                v.bw = c.w;
                v.bh = if v.kind == Kind::Leaf && wide(v.shape) {
                    c.nh
                } else {
                    c.h
                };
            }
            if v.kind == Kind::Leaf && v.shape == "cyl" {
                v.dy = min(12.0, max(6.0, v.bw / 14.0)) / 2.0;
            }
            if v.kind == Kind::Leaf || v.kind == Kind::Arrow {
                obstacles.push(v.index);
            } else if c.head > 0.0 {
                titles.push(TitleBox {
                    x: c.x,
                    y: c.y - c.h / 2.0 + (c.head + gap) / 2.0 + 2.0,
                    bw: v.label.width,
                    bh: v.label.height,
                });
            }
        }
        Ok(content)
    };
    let mut content = place(&mut views, &mut cells, &mut obstacles, &mut titles)?;

    // Edges between the same two blocks are spread apart instead of drawn on top of each
    // other.
    let order_len = views.len();
    let key_of = |s: usize, t: usize| s.min(t) * order_len + s.max(t);
    let mut pairs: HashMap<usize, usize> = HashMap::new();
    let mut labelled = std::collections::HashSet::new();
    let mut ends: Vec<(usize, usize)> = Vec::new();
    let mut drawn: Vec<usize> = Vec::new();
    for &edge in &db.edges {
        let e = &db.arena[edge];
        let s = by_id.get(e.start.as_deref().unwrap_or(""));
        let t = by_id.get(e.end.as_deref().unwrap_or(""));
        let (Some(&s), Some(&t)) = (s, t) else {
            continue;
        };
        let key = key_of(s, t);
        *pairs.entry(key).or_insert(0) += 1;
        if e.label.as_deref().is_some_and(|l| !l.is_empty()) {
            labelled.insert(key);
        }
        ends.push((s, t));
        drawn.push(edge);
    }
    let checking = drawn.len() * obstacles.len() <= OBSTACLE_BUDGET;
    let mut routes: Vec<Route> = Vec::new();
    // The labelled edges between each pair of blocks joined by more than one, in the order of
    // the first.
    let mut stacks: HashMap<usize, Vec<usize>> = HashMap::new();
    let mut stack_keys: Vec<usize> = Vec::new();
    let mut legs: Vec<Vec<f64>> = vec![Vec::new(); drawn.len()];

    // Routes every edge, as Pele's route does.
    let route = |views: &[View],
                 obstacles: &[usize],
                 routes: &mut Vec<Route>,
                 stacks: &mut HashMap<usize, Vec<usize>>,
                 stack_keys: &mut Vec<usize>,
                 legs: &mut Vec<Vec<f64>>| {
        let mut seen: HashMap<usize, usize> = HashMap::new();
        routes.clear();
        stacks.clear();
        stack_keys.clear();
        let check = checking.then_some(obstacles);
        let blocked = check.map(|obstacles| Blocked {
            views,
            obstacles,
            titles: &[],
        });
        for i in 0..drawn.len() {
            let (s, t) = ends[i];
            let key = key_of(s, t);
            let total = pairs.get(&key).copied().unwrap_or(1);
            let k = seen.get(&key).copied().unwrap_or(0);
            seen.insert(key, k + 1);
            let raw = db.arena[drawn[i]].label.as_deref().unwrap_or("");
            let label = layout_label(raw, false, &set, edge_size, WRAP, Face::REGULAR);
            let mut points = Vec::new();
            let (lx, ly);
            if s == t {
                let (px, py, r, _, _) = loop_arc(&views[s], k + 1);
                lx = px + r + 6.0 + label.width / 2.0;
                ly = py - r / 2.0;
            } else {
                let step = if labelled.contains(&key) {
                    round(edge_size * 1.5) + 2.0
                } else {
                    14.0
                };
                let shift = (k as f64 - (total as f64 - 1.0) / 2.0) * step;
                points = route_edge(views, s, t, shift, check);
                let n = points.len();
                // The label goes on the longer leg.
                let first_leg = (points[2] - points[0]).abs() + (points[3] - points[1]).abs();
                let last_leg =
                    (points[n - 2] - points[n - 4]).abs() + (points[n - 1] - points[n - 3]).abs();
                let at = if n == 6 && last_leg > first_leg { 2 } else { 0 };
                let spot = if label.width > 0.0 {
                    label_spot(&points, at, label.width + 8.0, label.height, s, t, blocked)
                } else {
                    0.5
                };
                lx = points[at] + (points[at + 2] - points[at]) * spot;
                ly = points[at + 1] + (points[at + 3] - points[at + 1]) * spot;
                if label.width > 0.0 && total > 1 {
                    legs[i] = points[at..at + 4].to_vec();
                    stacks
                        .entry(key)
                        .or_insert_with(|| {
                            stack_keys.push(key);
                            Vec::new()
                        })
                        .push(i);
                }
            }
            routes.push(Route {
                edge: drawn[i],
                points,
                looped: if s == t { k + 1 } else { 0 },
                label,
                lx,
                ly,
            });
        }
    };

    // Routes the edges and stacks the labels of each pair of blocks that cover each other,
    // as Pele's settle does. Where they do not fit and grow is set, it widens the gap between
    // the blocks, across the rows always and across the columns unless the layout is tight,
    // which keeps columns narrow to fit the host, and says whether it did.
    let settle = |grow: bool,
                  views: &[View],
                  cells: &mut [Cell],
                  obstacles: &[usize],
                  titles: &[TitleBox],
                  routes: &mut Vec<Route>,
                  stacks: &mut HashMap<usize, Vec<usize>>,
                  stack_keys: &mut Vec<usize>,
                  legs: &mut Vec<Vec<f64>>| {
        route(views, obstacles, routes, stacks, stack_keys, legs);
        let blocked = checking.then_some(Blocked {
            views,
            obstacles,
            titles,
        });
        let mut base = HashMap::new();
        let mut grown = false;
        for key in stack_keys.iter() {
            let stack = &stacks[key];
            if stack.len() < 2 {
                continue;
            }
            let (s, t) = ends[stack[0]];
            let short = stack_labels(stack, routes, legs, s, t, blocked);
            let vertical = legs[stack[0]][0] == legs[stack[0]][2];
            if grow
                && short > 0.0
                && (vertical || !tight)
                && widen(cells, &parents, s, t, vertical, short.ceil(), &mut base)
            {
                grown = true;
            }
        }
        grown
    };

    // The area of the edge labels that cover each other, as Pele's overlap sums it.
    let overlap = |routes: &[Route]| {
        let mut area = 0.0;
        for (i, p) in routes.iter().enumerate() {
            for q in &routes[i + 1..] {
                let pw = (p.label.width + 8.0) / 2.0;
                let qw = (q.label.width + 8.0) / 2.0;
                let w = min(p.lx + pw, q.lx + qw) - max(p.lx - pw, q.lx - qw);
                let h = min(p.ly + p.label.height / 2.0, q.ly + q.label.height / 2.0)
                    - max(p.ly - p.label.height / 2.0, q.ly - q.label.height / 2.0);
                if p.label.width > 0.0 && q.label.width > 0.0 && w > 0.0 && h > 0.0 {
                    area += w * h;
                }
            }
        }
        area
    };
    // The extent of the drawing, as Pele's bound finds it: its left, top, right and bottom.
    let bound = |content: (f64, f64), views: &[View], routes: &[Route]| {
        let mut b = [0.0, 0.0, content.0, content.1];
        let mut grow = |x: f64, y: f64, w: f64, h: f64| {
            if x - w / 2.0 < b[0] {
                b[0] = x - w / 2.0;
            }
            if x + w / 2.0 > b[2] {
                b[2] = x + w / 2.0;
            }
            if y - h / 2.0 < b[1] {
                b[1] = y - h / 2.0;
            }
            if y + h / 2.0 > b[3] {
                b[3] = y + h / 2.0;
            }
        };
        for (i, r) in routes.iter().enumerate() {
            if r.looped > 0 {
                let (px, py, rr, _, _) = loop_arc(&views[ends[i].0], r.looped);
                grow(px, py, 2.0 * rr + 2.0, 2.0 * rr + 2.0);
            }
            if r.label.width > 0.0 {
                grow(r.lx, r.ly, r.label.width + 8.0, r.label.height);
            }
        }
        b
    };

    if settle(
        drawn.len() * drawn.len() <= OBSTACLE_BUDGET,
        &views,
        &mut cells,
        &obstacles,
        &titles,
        &mut routes,
        &mut stacks,
        &mut stack_keys,
        &mut legs,
    ) {
        let before = overlap(&routes);
        let b = bound(content, &views, &routes);
        let narrow = b[2] - b[0];
        // A container stretched by its row gives some of a new gap back, so the gap may take
        // a few rounds.
        let mut grown = true;
        let mut round = 0;
        while round < 3 && grown {
            content = place(&mut views, &mut cells, &mut obstacles, &mut titles)?;
            grown = settle(
                round < 2,
                &views,
                &mut cells,
                &obstacles,
                &titles,
                &mut routes,
                &mut stacks,
                &mut stack_keys,
                &mut legs,
            );
            round += 1;
        }
        // The drawing only grows if that leaves less of the labels covering each other, and
        // within the host's width.
        let b = bound(content, &views, &routes);
        let wide = b[2] - b[0];
        let room = if o.max_width > 0.0 {
            o.max_width
        } else {
            f64::INFINITY
        };
        if overlap(&routes) > before - 1.0 || (wide > narrow && (wide + 2.0 * pad).ceil() > room) {
            for c in &mut cells {
                c.row_gaps = Vec::new();
                c.col_gaps = Vec::new();
            }
            content = place(&mut views, &mut cells, &mut obstacles, &mut titles)?;
            settle(
                false,
                &views,
                &mut cells,
                &obstacles,
                &titles,
                &mut routes,
                &mut stacks,
                &mut stack_keys,
                &mut legs,
            );
        }
    }
    let [min_x, min_y, max_x, max_y] = bound(content, &views, &routes);

    let room = if tight { title_room(o) } else { 4000.0 };
    let title = title_label(db.title.as_deref(), &set, size, room);
    let title_height = if title.height > 0.0 {
        title.height + 12.0
    } else {
        0.0
    };
    let inner = max(max_x - min_x, title.width);
    let ox = pad - min_x + (inner - (max_x - min_x)) / 2.0;
    let oy = pad + title_height - min_y;
    for v in &mut views {
        v.x += ox;
        v.y += oy;
    }
    let width = (inner + 2.0 * pad).ceil();
    let height = (max_y - min_y + title_height + 2.0 * pad).ceil();

    let mut heading = Vec::new();
    let mut clusters = Vec::new();
    let mut casings = Vec::new();
    let mut nodes = Vec::new();
    let mut edges = Vec::new();
    let mut labels = Vec::new();
    // The composites whose authors paint them a fill, which edges inside them are drawn on,
    // as Pele's fills.
    let mut fills: Vec<Fill> = Vec::new();
    if !title.is_empty() {
        let mut it = Item::new(ItemKind::Text, ROLE_TITLE);
        let at = Point::new(width / 2.0, pad + title.height / 2.0);
        it.label = Some(Placed::new(title, at));
        heading.push(it);
    }
    for (v, c) in views.iter().zip(&cells).skip(1) {
        if v.kind == Kind::Space {
            continue;
        }
        let b = &db.arena[v.block];
        let classes = class_names(&b.classes.as_deref().unwrap_or_default().join(" "));
        let author = declarations(&v.decls).style;
        let inks = v.style.inks_text(&v.style);
        if v.kind == Kind::Composite {
            let top = v.y - v.h / 2.0;
            let bounds = Rect::new(v.x - v.w / 2.0, top, v.w, v.h);
            if v.style.paints() {
                fills.push(Fill {
                    bounds: [v.x - v.w / 2.0, top, v.x + v.w / 2.0, top + v.h],
                    decls: v.decls.clone(),
                });
            }
            let mut it = Item::new(ItemKind::Group, ROLE_COMPOSITE);
            it.id = b.id.clone();
            it.class = classes;
            it.bounds = bounds;
            it.parts = vec![Part::rect(bounds, true, Painting::FillAndStroke)];
            it.style = Style {
                fill: Some(Paint::SURFACE_ALT),
                // A composite is drawn at half its opacity, unless its author sets a fill, as
                // opaque makes the author's style.
                fill_opacity: Some(0.5),
                stroke: Some(Paint::BORDER),
                text: Some(Paint::MUTED),
                on_fill: inks,
                ..Style::default()
            };
            it.author = opaque(author, v.style.fill_declared());
            if !v.label.is_empty() {
                let at = Point::new(v.x, top + (c.head + gap) / 2.0 + 2.0);
                it.label = Some(Placed::new(v.label.clone(), at));
            }
            clusters.push(it);
            continue;
        }
        let mut it = Item::new(ItemKind::Node, ROLE_BLOCK);
        it.shape = Some(Shape::new(v.shape));
        it.id = b.id.clone();
        it.class = classes;
        it.bounds = Rect::new(v.x - v.bw / 2.0, v.y - v.bh / 2.0, v.bw, v.bh);
        it.style = Style {
            fill: Some(Paint::SURFACE),
            stroke: Some(Paint::BORDER),
            on_fill: inks,
            ..Style::default()
        };
        it.author = author;
        if v.kind == Kind::Arrow {
            it.shape = Some(Shape::new("block-arrow"));
            let p = draw_arrow(v.mask, v.x, v.y, v.bw, v.bh);
            it.parts = vec![Part::path(p, Painting::FillAndStroke)];
            // Pele turns the corners of an arrow round.
            it.style.join = Some(Join::Round);
        } else {
            it.parts = draw_shape(v.shape, v.x, v.y, v.bw, v.bh);
        }
        let mut label = v.label.clone();
        // A label that spans more than one column takes the room of its wider cell, without
        // changing the node's size or the routes found from it, as Pele's draw does.
        if c.span > 1.0
            && v.label.width > 0.0
            && v.label.lines.len() > 1
            && v.bw > c.nw
            && (v.kind == Kind::Arrow || !fixed(v.shape))
        {
            let mut face = Face::REGULAR;
            if v.style.bold {
                face |= Face::BOLD;
            }
            if v.style.italic {
                face |= Face::ITALIC;
            }
            let room = v.label.width + (v.bw - c.nw);
            let raw = b.label.as_deref().unwrap_or("");
            label = layout_label(raw, false, &set, v.label.size, room, face);
        }
        if !label.is_empty() {
            let at = Point::new(v.x + v.dx, v.y + v.dy);
            it.label = Some(Placed::new(label, at));
        }
        nodes.push(it);
    }

    for (i, r) in routes.iter_mut().enumerate() {
        let edge = &db.arena[r.edge];
        let start = edge
            .arrow_type_start
            .as_deref()
            .and_then(MarkerType::from_name);
        let end = edge
            .arrow_type_end
            .as_deref()
            .and_then(MarkerType::from_name);
        let path = if r.looped > 0 {
            loop_path(
                &views[ends[i].0],
                r.looped,
                marker_trim(start),
                marker_trim(end),
            )
        } else {
            for k in (0..r.points.len()).step_by(2) {
                r.points[k] += ox;
                r.points[k + 1] += oy;
            }
            trace(&r.points, marker_trim(start), marker_trim(end))
        };
        let thick = edge.thickness.as_deref() == Some("thick");
        // Pele draws the edges in a group with round caps.
        let mut own = Style {
            stroke: Some(Paint::LINE),
            fill: Some(Paint::NONE),
            cap: Some(Cap::Round),
            ..Style::default()
        };
        if thick {
            own.stroke_width = Some(2.5);
        }
        if edge.pattern.as_deref() == Some("dotted") {
            own.dash = Some(vec![3.0, 4.0]);
        }
        // An edge inside one fill is drawn in the color of its labels, and one that crosses
        // the border of a fill is laid on a casing, as Pele's overFills finds.
        let marks = path.ends(start, end);
        let tips = [
            path.start.at.x,
            path.start.at.y,
            path.end.at.x,
            path.end.at.y,
        ];
        let (ground, cased) = on_fills(&path.path, &tips, &fills, "");
        let id = edge.id.clone();
        if cased && let Some(mut c) = casing(ROLE_CASING, &path.path, &marks, &[], thick) {
            c.id = id.clone();
            casings.push(c);
        }
        own.ground = ground;
        let mut route: Vec<Point> = r.points.chunks(2).map(|p| Point::new(p[0], p[1])).collect();
        if route.is_empty() {
            // A self-loop, which the layout gives no route: the corners its curve is drawn
            // about.
            route = crate::items::corners(&path.path);
        }
        let mut it = Item::new(ItemKind::Edge(Edge { route, ends: marks }), ROLE_EDGE);
        it.id = id.clone();
        it.bounds = path.path.bounds();
        it.parts = vec![Part::path(path.path, Painting::StrokeOnly)];
        it.style = own;
        edges.push(it);
        if r.label.width > 0.0 {
            let (x, y) = (r.lx + ox, r.ly + oy);
            let l = &r.label;
            let mut it = Item::new(ItemKind::Text, ROLE_EDGE_LABEL);
            it.id = id;
            it.bounds = label_patch_box(l.width, l.height, x, y);
            it.parts = vec![label_patch(l.width, l.height, x, y)];
            it.label = Some(Placed::new(l.clone(), Point::new(x, y)));
            it.style = Style {
                fill: Some(Paint::BG),
                stroke: Some(Paint::NONE),
                ..Style::default()
            };
            labels.push(it);
        }
    }

    let mut sc = Scene {
        diagram: "block".into(),
        width,
        height,
        font_size: size,
        title: None,
        desc: None,
        items: Vec::new(),
    };
    for layer in [heading, clusters, casings, edges, nodes, labels] {
        sc.items.extend(layer);
    }
    Ok(sc)
}