ferri-pdf 0.2.1

HTML and CSS to PDF renderer for print, in 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
//! Footnote areas (CSS GCPM 3 §2): footnote bodies at the bottom of the page area of the page
//! holding their calls, or continued on the pages after it.
#[allow(
    clippy::wildcard_imports,
    reason = "pagination submodules extend the parent paginator and share its scope"
)]
use super::*;
use crate::layout::multicol::{
    ColumnGeometry, WrapRows,
    slices::{Distribution, Plan, Run, slice_clip, windowed},
};
use crate::style::{BoxSizing, ColumnFill, FootnoteDisplay, FootnotePolicy};
use std::{cell::RefCell, collections::BTreeSet};

/// A footnote body laid out alone in its footnote area.
pub(super) struct FootnoteLayout {
    flow: StandaloneFlow,
    /// The width the body takes in the area: the area's, or its max-content width when
    /// `compact` lets it share a line.
    width: f32,
    height: f32,
    compact: bool,
    /// Offsets in the body between which no line box or image lies, ascending.
    breaks: Vec<f32>,
    /// The extent of each footnote of an inline body, in its order: from the top of the line
    /// it starts on to the bottom of the line the next one starts on when they share it, or
    /// else to that line's top.
    members: Vec<[f32; 2]>,
    /// The body's own border box in the layout, where its box paints.
    body_box: Option<Rect>,
    /// The block-start and block-end border and padding of a body that clones its decoration
    /// at breaks (CSS Fragmentation 3 §5.4), which wrap each of its parts.
    cloned: Option<[f32; 2]>,
}

/// The slice `[top, bottom)` of a footnote body that one page's footnote area holds.
#[derive(Clone, Copy)]
pub(super) struct FootnotePart {
    body: usize,
    top: f32,
    bottom: f32,
    width: f32,
    /// A whole `compact` body, which shares a line with the next when both fit.
    inline: bool,
    /// The cloned block-start and block-end edges of its body that wrap this part.
    repeated: [f32; 2],
}
impl FootnotePart {
    fn height(self) -> f32 {
        self.bottom - self.top + self.repeated[0] + self.repeated[1]
    }
}

/// A line of the footnote area: one part, or `compact` bodies side by side.
struct Row {
    parts: Vec<(FootnotePart, f32)>,
    height: f32,
}

/// Groups parts into the footnote area's lines (CSS GCPM 3 §2.3).
fn rows(parts: &[FootnotePart], width: f32) -> Vec<Row> {
    let mut rows: Vec<Row> = Vec::new();
    for &part in parts {
        if part.inline
            && let Some(row) = rows.last_mut()
            && row.parts.iter().all(|(other, _)| other.inline)
        {
            let used: f32 = row.parts.iter().map(|(other, _)| other.width).sum();
            if !overflows(used + part.width, width) {
                row.parts.push((part, used));
                row.height = row.height.max(part.height());
                continue;
            }
        }
        rows.push(Row {
            parts: vec![(part, 0.0)],
            height: part.height(),
        });
    }
    rows
}

/// A page's footnote parts in the lines of its footnote area and their slices in its column
/// boxes.
struct AreaContent {
    rows: Vec<Row>,
    columns: Distribution,
}
impl AreaContent {
    fn height(&self) -> f32 {
        self.columns.height()
    }
    /// Whether the area's columns hold the content, which then fits a content height of
    /// `limit`.
    fn fits(&self, limit: f32) -> bool {
        !self.columns.overflows && !overflows(self.height(), limit)
    }
    /// Each part as the column boxes slice it, with its offset in the area's content box.
    fn placed(&self, geometry: ColumnGeometry) -> Vec<(FootnotePart, [f32; 2])> {
        let mut placed = Vec::new();
        for (slice, [x, y]) in self.columns.placed(geometry) {
            let row = &self.rows[slice.run];
            if let [(part, _)] = row.parts.as_slice() {
                let part = FootnotePart {
                    top: slice.top,
                    bottom: slice.bottom,
                    repeated: slice.repeated,
                    ..*part
                };
                placed.push((part, [x, y]));
            } else {
                placed.extend(
                    row.parts
                        .iter()
                        .map(|&(part, offset)| (part, [x + offset, y])),
                );
            }
        }
        placed
    }
}

/// Footnote bodies by body and the bits of their area's width.
type FootnoteLayouts = HashMap<(usize, u32), Rc<FootnoteLayout>>;

/// Pages that the calls of footnotes whose policy keeps them with their calls (CSS GCPM 3
/// §2.8) cannot lie on, by footnote and page: the footnote did not fit there, and the call is
/// in a nested layout, which learns that only once its content is placed.
pub(in crate::layout) type FootnoteDeferrals = BTreeMap<(usize, usize), FootnotePolicy>;

/// The footnotes each page's area holds, the remainders waiting for the next page, and the
/// footnotes already placed or waiting.
#[derive(Clone, Default)]
pub(super) struct Footnotes {
    pages: Vec<Vec<FootnotePart>>,
    carried: VecDeque<FootnotePart>,
    placed: BTreeSet<usize>,
    /// How many recorded calls have been taken up, and those taken up whose pages the flow
    /// has not reached.
    recorded: usize,
    ahead: Vec<(usize, usize)>,
    /// Calls in nested layouts that pagination must move off their pages.
    deferred: FootnoteDeferrals,
    /// How far the content of each inline body is queued.
    queued: HashMap<usize, f32>,
    /// Where this pagination placed the inline formatting contexts of footnote areas.
    inline: crate::document::InlineFootnotePlacement,
    /// Bodies by body and area width; laying a body out does not depend on its page.
    layouts: Rc<RefCell<FootnoteLayouts>>,
}
impl Footnotes {
    fn parts(&self, page: usize) -> &[FootnotePart] {
        self.pages.get(page).map_or(&[], Vec::as_slice)
    }
    /// The last page whose footnote area holds a part.
    pub(super) fn last_page(&self) -> Option<usize> {
        self.pages.iter().rposition(|parts| !parts.is_empty())
    }
    pub(super) fn has_carried(&self) -> bool {
        !self.carried.is_empty()
    }
    /// The calls this pagination found on pages that their footnotes do not fit.
    pub(super) fn take_deferred(&mut self) -> FootnoteDeferrals {
        std::mem::take(&mut self.deferred)
    }
    /// Where this pagination placed the inline formatting contexts of footnote areas.
    pub(super) fn take_inline_placement(&mut self) -> crate::document::InlineFootnotePlacement {
        std::mem::take(&mut self.inline)
    }
}

/// The policy among `policies` that moves most: `block`, then `line`, then `auto`.
fn strictest(policies: impl Iterator<Item = FootnotePolicy>) -> FootnotePolicy {
    policies
        .max_by_key(|policy| match policy {
            FootnotePolicy::Auto => 0,
            FootnotePolicy::Line => 1,
            FootnotePolicy::Block => 2,
        })
        .unwrap_or_default()
}

/// A page's footnote area box in the page's flow coordinates.
struct Area {
    x: f32,
    width: f32,
    /// The column boxes of its content box, from its left edge: the area is a block
    /// container (CSS GCPM 3 §2.4), so its column properties make it a multi-column one.
    columns: ColumnGeometry,
    /// How its content fills its columns: as the multi-column properties of a multi-column
    /// area set, and else whole in one column.
    fill: ColumnFill,
    column_height: Option<f32>,
    wrap: Option<WrapRows>,
    rule: crate::graphics::Border,
    padding: [f32; 4],
    borders: [f32; 4],
    margins: [f32; 4],
    /// Content height bounds from `min-height` and from `height` or `max-height`.
    minimum: f32,
    maximum: Option<f32>,
}
impl Area {
    fn edges(&self, side: usize) -> f32 {
        self.padding[side] + self.borders[side] + self.margins[side]
    }
    /// Whether layout can form the area: its geometry, and its extent along each axis,
    /// are finite.
    fn is_finite(&self) -> bool {
        [
            self.x,
            self.width + self.edges(LEFT) + self.edges(RIGHT),
            self.minimum + self.edges(TOP) + self.edges(BOTTOM),
            self.columns.width + self.columns.gap,
        ]
        .into_iter()
        .chain(self.maximum)
        .chain(self.column_height)
        .all(f32::is_finite)
    }
    fn content_height(&self, parts: f32, unbounded: bool) -> f32 {
        let height = parts.max(self.minimum);
        match self.maximum {
            Some(maximum) if !unbounded => height.min(maximum.max(self.minimum)),
            _ => height,
        }
    }
    /// The margin-box height around `parts` of content; an empty area is not generated.
    fn outer_height(&self, parts: Option<f32>, unbounded: bool) -> f32 {
        parts.map_or(0.0, |parts| {
            self.edges(TOP) + self.content_height(parts, unbounded) + self.edges(BOTTOM)
        })
    }
    /// How the content fills the columns: `column-fill: auto` fills them to the area's
    /// maximum height where that applies (CSS Multi-column Layout 1 §7.1).
    fn plan(&self, unbounded: bool) -> Plan {
        let filled = self
            .maximum
            .filter(|_| !unbounded && self.fill == ColumnFill::Auto);
        Plan {
            count: self.columns.count,
            height: self.column_height.or(filled),
            definite: self.column_height.is_some(),
            fill: self.fill,
            wrap: self.wrap,
        }
    }
}
const NON_FINITE_AREA: &str = "non-finite footnote area geometry";
const TOP: usize = 0;
const RIGHT: usize = 1;
const BOTTOM: usize = 2;
const LEFT: usize = 3;

impl<'a: 't, 't: 'a> Paginator<'a, '_, 't> {
    /// The page's footnote area box and decoration in the paginator's frame: the page flow's,
    /// given as horizontal, in which the area lies at the block end of the page area.
    fn area_box(
        &self,
        page: usize,
    ) -> Option<(
        std::borrow::Cow<'_, crate::style::Style>,
        std::borrow::Cow<'_, BoxDecoration>,
    )> {
        let Fragmentainers::Pages(pages) = &self.fragmentainers else {
            return None;
        };
        let footnote = &pages.boxes[page].footnote;
        let flow = crate::style::logical::Flow::used(pages.document.root().style());
        if !flow.writing_mode.vertical() {
            return Some((
                std::borrow::Cow::Borrowed(&footnote.style),
                std::borrow::Cow::Borrowed(&footnote.decoration),
            ));
        }
        let logical = flow.horizontal();
        Some((
            std::borrow::Cow::Owned(crate::style::logical::reflowed_from(
                &footnote.style,
                flow,
                logical,
            )),
            std::borrow::Cow::Owned(crate::style::logical::decoration(
                &footnote.decoration,
                flow,
                logical,
            )),
        ))
    }

    fn area(&self, page: usize) -> Option<Area> {
        let (style, _) = self.area_box(page)?;
        let content = self.fragmentainers.content_box(page);
        let padding = std::array::from_fn(|side| style.padding[side].resolve(content.width));
        let borders = style
            .decoration
            .borders
            .map(crate::graphics::Border::used_width);
        let margins =
            std::array::from_fn(|side| style.margin[side].resolve(content.width).unwrap_or(0.0));
        let vertical = padding[TOP] + padding[BOTTOM] + borders[TOP] + borders[BOTTOM];
        let horizontal = padding[LEFT] + padding[RIGHT] + borders[LEFT] + borders[RIGHT];
        let content_size = |size: &crate::style::Size, basis: f32, edges: f32| {
            size.resolve_definite(Some(basis)).map(|size| {
                if style.box_sizing == BoxSizing::BorderBox {
                    (size - edges).max(0.0)
                } else {
                    size
                }
            })
        };
        let width = content_size(&style.width, content.width, horizontal)
            .unwrap_or(content.width - horizontal - margins[LEFT] - margins[RIGHT]);
        let height = content_size(&style.height, content.height, vertical);
        let minimum = content_size(&style.min_height, content.height, vertical)
            .unwrap_or(0.0)
            .max(height.unwrap_or(0.0));
        let maximum = match (
            height,
            content_size(&style.max_height, content.height, vertical),
        ) {
            (Some(height), Some(maximum)) => Some(height.min(maximum)),
            (height, maximum) => height.or(maximum),
        };
        let width = width.max(0.0);
        let multicol = style.multicol.is_multicol();
        let area = Area {
            x: content.x + margins[LEFT],
            width,
            columns: ColumnGeometry::resolve(&style, 0.0, width),
            fill: if multicol {
                style.multicol.fill
            } else {
                ColumnFill::Balance
            },
            column_height: style.multicol.height,
            wrap: WrapRows::of(&style, height).filter(|_| multicol),
            rule: style.multicol.rule,
            padding,
            borders,
            margins,
            minimum,
            maximum,
        };
        // An area layout cannot form is omitted, with the footnotes it would hold, as layout
        // omits page-margin boxes it cannot form.
        if !area.is_finite() {
            self.measurer
                .diagnostics
                .record(crate::layout::LayoutDiagnostic::OmittedBox {
                    reason: NON_FINITE_AREA.to_owned(),
                });
            return None;
        }
        Some(area)
    }

    /// Whether the page holds only footnotes, which `max-height` then does not limit (CSS
    /// GCPM 3 §2.4.2).
    fn footnotes_only(&self, page: usize) -> bool {
        self.footnote_pages.is_some_and(|first| page >= first)
    }

    /// `parts` laid out in the lines and column boxes of `area`; `None` without parts.
    fn area_content(
        &self,
        parts: &[FootnotePart],
        area: &Area,
        unbounded: bool,
    ) -> Option<AreaContent> {
        if parts.is_empty() {
            return None;
        }
        let width = area.columns.width;
        let rows = rows(parts, width);
        let layouts: Vec<_> = rows
            .iter()
            .map(|row| match row.parts.as_slice() {
                [(part, _)] => Some(self.laid_out(part.body, width)),
                _ => None,
            })
            .collect();
        let runs: Vec<Run<'_>> = rows
            .iter()
            .zip(&layouts)
            .map(|(row, layout)| match layout {
                Some(layout) => {
                    let (part, _) = row.parts[0];
                    Run {
                        top: part.top,
                        bottom: part.bottom,
                        breaks: &layout.breaks,
                        cloned: layout.cloned.unwrap_or_default(),
                        repeated: part.repeated,
                    }
                }
                None => Run::whole(row.height),
            })
            .collect();
        let columns = Distribution::of(&runs, area.plan(unbounded));
        Some(AreaContent { rows, columns })
    }

    /// The layout of body `body` at `width`, which laying out its parts cached.
    fn laid_out(&self, body: usize, width: f32) -> Rc<FootnoteLayout> {
        self.footnotes
            .layouts
            .borrow()
            .get(&(body, width.to_bits()))
            .cloned()
            .expect("laying out a body's parts caches its layout")
    }

    fn content_height(&self, page: usize) -> Option<f32> {
        let area = self.area(page)?;
        let parts = self.footnotes.parts(page);
        self.measurer.work.charge(parts.len());
        self.area_content(parts, &area, self.footnotes_only(page))
            .map(|content| content.height())
    }

    /// The height the page's footnote area takes from the page area.
    pub(super) fn footnote_reserve(&self, page: usize) -> f32 {
        self.area(page).map_or(0.0, |area| {
            area.outer_height(self.content_height(page), self.footnotes_only(page))
        })
    }

    fn footnote_layout(
        &mut self,
        index: usize,
        area_width: f32,
    ) -> Result<Rc<FootnoteLayout>, LayoutError> {
        let key = (index, area_width.to_bits());
        if let Some(layout) = self.footnotes.layouts.borrow().get(&key) {
            return Ok(layout.clone());
        }
        let Fragmentainers::Pages(pages) = &self.fragmentainers else {
            unreachable!("footnote areas belong to pages");
        };
        let document = pages.document;
        let footnotes = document.footnotes();
        let body = &footnotes.bodies[index].block;
        let inline = footnotes.bodies[index].inline;
        let compact = !inline && body.style().footnote_display == FootnoteDisplay::Compact;
        let width = if compact {
            self.measurer
                .run(|measurer| crate::layout::intrinsic::measure(body, measurer, area_width))?
                .max
                .min(area_width)
        } else {
            area_width
        };
        let measured =
            self.measurer
                .block(body, 0.0, width, body.style().used_rtl(), false, None)?;
        let height = if measured.through {
            let mut margins = measured.before.clone();
            margins.merge(measured.after.clone());
            margins.value()
        } else {
            measured.before.value() + measured.height + measured.after.value()
        };
        let measured_after = measured.after.value();
        let body_box = (!inline && !measured.through)
            .then_some(measured.paint.as_ref())
            .flatten()
            .map(|paint| Rect {
                y: measured.before.value(),
                height: measured.height,
                ..paint.rect
            });
        let cloned = body_box.filter(|_| body.style().clone_decoration).map(|_| {
            let geometry = &measured.geometry;
            [
                geometry.padding[0] + geometry.border[0],
                geometry.padding[2] + geometry.border[2],
            ]
        });
        let flow = Paginator::standalone(
            &mut *self.measurer,
            measured,
            Rect {
                x: 0.0,
                y: 0.0,
                width,
                height,
            },
            document.navigation().destinations,
            // A footnote area's backgrounds may clip to the text of the bodies it holds.
            true,
        )?;
        // The body is a formatting context root whose floats extend it (CSS 2.1 §10.6.7).
        let height = height.max(flow.extent + measured_after);
        let breaks = flow.breaks([body.style().orphans, body.style().widows], height);
        let mut members = Vec::new();
        if inline {
            let elements = &footnotes.bodies[index].members;
            members.resize(elements.len(), [0.0, height]);
            let mut starts = HashMap::new();
            for line in flow.event_lines.iter().rev() {
                starts.insert(line.element, line);
            }
            let mut end = height;
            for (position, &member) in elements.iter().enumerate().skip(1).rev() {
                let element = footnotes.elements[member].element;
                // A footnote that starts on no line starts where the one after it does.
                if let Some(start) = starts.get(&element) {
                    members[position][0] = start.extent[0];
                    end = if start.shared {
                        start.extent[1]
                    } else {
                        start.extent[0]
                    };
                } else {
                    members[position][0] = end;
                }
                members[position - 1][1] = end;
            }
        }
        let layout = Rc::new(FootnoteLayout {
            flow,
            width,
            height,
            compact,
            breaks,
            members,
            body_box,
            cloned,
        });
        self.footnotes
            .layouts
            .borrow_mut()
            .insert(key, layout.clone());
        Ok(layout)
    }

    fn document_footnotes(&self) -> &'t crate::document::Footnotes {
        let Fragmentainers::Pages(pages) = &self.fragmentainers else {
            unreachable!("footnote areas belong to pages");
        };
        pages.document.footnotes()
    }

    /// The part of its body that `footnote` adds to the area of `area_width`: a block
    /// footnote's whole body, or the lines of an inline body after those queued through
    /// `queued` up to the end of the footnote's content, if any.
    fn footnote_part(
        &mut self,
        footnote: usize,
        area_width: f32,
        queued: f32,
    ) -> Result<Option<FootnotePart>, LayoutError> {
        let footnotes = self.document_footnotes();
        let body = footnotes.elements[footnote].body;
        let layout = self.footnote_layout(body, area_width)?;
        let Some(position) = footnotes.bodies[body].inline.then(|| {
            footnotes.bodies[body]
                .members
                .iter()
                .position(|&member| member == footnote)
                .expect("an inline body holds its footnotes")
        }) else {
            return Ok(Some(FootnotePart {
                body,
                top: 0.0,
                bottom: layout.height,
                width: layout.width,
                inline: layout.compact,
                repeated: [0.0; 2],
            }));
        };
        let end = layout.members[position][1];
        Ok((end > queued).then_some(FootnotePart {
            body,
            top: queued,
            bottom: end,
            width: layout.width,
            inline: false,
            repeated: [0.0; 2],
        }))
    }

    /// Whether the content of `footnote` still waits for a later page.
    fn footnote_waits(&mut self, footnote: usize, area_width: f32) -> Result<bool, LayoutError> {
        let footnotes = self.document_footnotes();
        let body = footnotes.elements[footnote].body;
        let end = match footnotes.bodies[body]
            .members
            .iter()
            .position(|&member| member == footnote)
        {
            Some(position) if footnotes.bodies[body].inline => {
                self.footnote_layout(body, area_width)?.members[position][1]
            }
            _ => f32::INFINITY,
        };
        Ok(self
            .footnotes
            .carried
            .iter()
            .any(|part| part.body == body && part.top < end))
    }

    fn footnote_policy(&self, footnote: usize) -> FootnotePolicy {
        match &self.fragmentainers {
            Fragmentainers::Pages(pages) => pages.document.footnotes().elements[footnote].policy,
            Fragmentainers::Columns(_) => FootnotePolicy::Auto,
        }
    }

    /// Whether this paginator places footnotes: the page flow's, outside unbroken and
    /// parallel layout.
    pub(super) fn places_footnotes(&self) -> bool {
        matches!(self.fragmentainers, Fragmentainers::Pages(_))
            && self.unbroken == 0
            && self.parallel_depth == 0
    }

    /// How much more of the page area the footnote area takes with `footnotes` whole after
    /// the `pending` parts of earlier lines; `None` when they do not fit it whole, or must
    /// follow footnotes already waiting for the next page. `pending` gains their parts. A
    /// page whose area is omitted places no footnotes, so they take none of it.
    pub(super) fn footnote_growth(
        &mut self,
        footnotes: &[usize],
        pending: &mut Vec<FootnotePart>,
    ) -> Result<Option<f32>, LayoutError> {
        let Some(area) = self.area(self.page) else {
            return Ok(Some(0.0));
        };
        if self.footnotes.has_carried() {
            return Ok(None);
        }
        let mut parts: Vec<FootnotePart> = self
            .footnotes
            .parts(self.page)
            .iter()
            .chain(pending.iter())
            .copied()
            .collect();
        let before = self
            .area_content(&parts, &area, false)
            .map(|content| content.height());
        let start = parts.len();
        for &footnote in footnotes {
            let body = self.document_footnotes().elements[footnote].body;
            let queued = parts
                .iter()
                .filter(|part| part.body == body)
                .map(|part| part.bottom)
                .fold(
                    self.footnotes.queued.get(&body).copied().unwrap_or(0.0),
                    f32::max,
                );
            parts.extend(self.footnote_part(footnote, area.columns.width, queued)?);
        }
        let after = self.area_content(&parts, &area, false);
        if after
            .as_ref()
            .is_some_and(|after| !after.fits(area.maximum.unwrap_or(f32::INFINITY)))
        {
            return Ok(None);
        }
        pending.extend_from_slice(&parts[start..]);
        Ok(Some(
            area.outer_height(after.map(|after| after.height()), false)
                - area.outer_height(before, false),
        ))
    }

    /// The policy that keeps a footnote with its call when it does not fit: `line` or
    /// `block`, whichever moves more.
    pub(super) fn strictest_policy(&self, footnotes: &[usize]) -> FootnotePolicy {
        strictest(
            footnotes
                .iter()
                .map(|&footnote| self.footnote_policy(footnote)),
        )
    }

    /// The policy that moves `line` of `source` off the physical page it is on, because an
    /// earlier pagination found that a footnote called in a nested layout on the line does
    /// not fit there: the line calls it inside its atomic inlines or, in a nested layout,
    /// among the `footnotes` it calls directly.
    pub(super) fn deferred_policy(
        &self,
        source: &crate::document::Paragraph,
        line: &crate::text::Line,
        footnotes: &[usize],
    ) -> Result<Option<FootnotePolicy>, LayoutError> {
        let deferrals = &self.measurer.footnote_deferrals;
        if deferrals.is_empty() {
            return Ok(None);
        }
        let Some(page) = self.physical_page() else {
            return Ok(None);
        };
        let mut calls = Vec::new();
        for fragment in line.atomics.iter().chain(&line.annotations) {
            let (events, visited) = source.inline_block(fragment.index).block().subtree_events();
            self.measurer.work.spend(visited)?;
            calls.extend(events.into_iter().filter_map(|event| event.footnote));
        }
        if !self.places_footnotes() {
            calls.extend_from_slice(footnotes);
        }
        let policy = strictest(
            calls
                .into_iter()
                .filter_map(|footnote| deferrals.get(&(footnote, page)).copied()),
        );
        Ok((policy != FootnotePolicy::Auto).then_some(policy))
    }

    /// Queues footnotes not yet placed after those waiting, then fills the current page's
    /// area with them.
    fn queue_footnotes(
        &mut self,
        footnotes: impl IntoIterator<Item = usize>,
    ) -> Result<(), LayoutError> {
        let Some(area) = self.area(self.page) else {
            return Ok(());
        };
        for footnote in footnotes {
            if !self.footnotes.placed.insert(footnote) {
                continue;
            }
            let footnotes = self.document_footnotes();
            let body = footnotes.elements[footnote].body;
            let mut queued = self.footnotes.queued.get(&body).copied().unwrap_or(0.0);
            let members = &footnotes.bodies[body].members;
            if let Some(position) = members.iter().position(|&member| member == footnote)
                && position > 0
            {
                // An inline footnote continues the inline formatting context of the one
                // before it only right after its lines, at the end of the area.
                let layout = self.footnote_layout(body, area.columns.width)?;
                let last = self
                    .footnotes
                    .carried
                    .back()
                    .or_else(|| self.footnotes.parts(self.page).last());
                if queued != layout.members[position - 1][1]
                    || last.is_none_or(|part| part.body != body)
                {
                    self.footnotes
                        .inline
                        .starts
                        .insert(footnotes.elements[footnote].element);
                    // The rest of this pagination approximates the footnote starting its own
                    // inline formatting context with its lines from the one it starts on.
                    queued = queued.min(layout.members[position][0]);
                }
            }
            let Some(part) = self.footnote_part(footnote, area.columns.width, queued)? else {
                continue;
            };
            self.footnotes.queued.insert(body, part.bottom);
            match self.footnotes.carried.back_mut() {
                Some(last) if last.body == body && last.bottom == part.top => {
                    last.bottom = part.bottom;
                }
                _ => self.footnotes.carried.push_back(part),
            }
        }
        Ok(())
    }

    /// Records `page` as the page of the inline formatting contexts that start in `part` of an
    /// inline body placed there, those of its first footnote and of the footnotes that start
    /// their own, whose anonymous blocks inherit from its footnote area.
    fn record_inline_pages(
        &mut self,
        part: FootnotePart,
        page: usize,
        area_width: f32,
    ) -> Result<(), LayoutError> {
        let footnotes = self.document_footnotes();
        let body = &footnotes.bodies[part.body];
        if !body.inline {
            return Ok(());
        }
        let layout = self.footnote_layout(part.body, area_width)?;
        let Fragmentainers::Pages(pages) = &self.fragmentainers else {
            unreachable!("footnote areas belong to pages");
        };
        let request = &pages.boxes[page].request;
        // Formatting takes the first page's area until a pagination places a context.
        if *request == pages.document.page_box().request {
            return Ok(());
        }
        for (position, &member) in body.members.iter().enumerate() {
            let element = footnotes.elements[member].element;
            let start = layout.members[position][0];
            if (position == 0 || self.footnotes.inline.starts.contains(&element))
                && part.top <= start
                && (start < part.bottom || part.top == part.bottom)
            {
                self.footnotes
                    .inline
                    .pages
                    .entry(element)
                    .or_insert_with(|| request.clone());
            }
        }
        Ok(())
    }

    /// Places the footnotes of calls on the current page where they fit below the flow, as
    /// much as fits of the first that does not, the rest waiting for the next page.
    pub(super) fn place_footnotes(&mut self, footnotes: &[usize]) -> Result<(), LayoutError> {
        self.queue_footnotes(footnotes.iter().copied())?;
        self.place_carried_footnotes()
    }

    /// Queues the footnotes whose calls the flow recorded on this page or an earlier one
    /// without placing them: calls in unbroken, parallel or nested fragmentation contexts,
    /// such as table cells, columns and positioned boxes. Returns those it queued, with the
    /// pages of their calls.
    fn queue_recorded_footnotes(&mut self) -> Result<Vec<(usize, usize)>, LayoutError> {
        let calls = self
            .string_locations
            .footnote_calls(self.footnotes.recorded)
            .to_vec();
        self.footnotes.recorded += calls.len();
        self.footnotes.ahead.extend(calls);
        let page = self.page;
        let (reached, ahead) = std::mem::take(&mut self.footnotes.ahead)
            .into_iter()
            .partition(|(_, call)| *call <= page);
        self.footnotes.ahead = ahead;
        let queued: Vec<(usize, usize)> = reached
            .into_iter()
            .filter(|(footnote, _)| !self.footnotes.placed.contains(footnote))
            .collect();
        self.queue_footnotes(queued.iter().map(|&(footnote, _)| footnote))?;
        Ok(queued)
    }

    /// Places the footnotes of recorded calls below the content of the current page, where
    /// they fit, before more flow follows them or the page ends. A footnote that must stay
    /// with its call (CSS GCPM 3 §2.8) and does not fit defers its call to a later page,
    /// which a new pagination settles; a call that was already deferred from its page and
    /// stays on it lies where no break can move it, and its footnote continues after it.
    pub(super) fn place_recorded_footnotes(&mut self) -> Result<(), LayoutError> {
        let queued = self.queue_recorded_footnotes()?;
        if queued.is_empty() {
            return Ok(());
        }
        self.fill_footnote_area(false)?;
        let Some(area) = self.area(self.page) else {
            return Ok(());
        };
        for (footnote, page) in queued {
            let policy = self.footnote_policy(footnote);
            if policy != FootnotePolicy::Auto
                && self.footnote_waits(footnote, area.columns.width)?
                && !self
                    .measurer
                    .footnote_deferrals
                    .contains_key(&(footnote, page))
            {
                self.footnotes.deferred.insert((footnote, page), policy);
            }
        }
        Ok(())
    }

    /// Fills the current page's footnote area with waiting footnote content in order, as far
    /// as the space below the flow and the area's maximum height allow; a page without flow
    /// content takes at least one break interval so that every page advances.
    pub(super) fn place_carried_footnotes(&mut self) -> Result<(), LayoutError> {
        self.fill_footnote_area(true)
    }

    /// Fills the current page's footnote area with waiting footnote content; `advance` lets a
    /// page without flow content take a slice that does not fit, so that pagination ends.
    fn fill_footnote_area(&mut self, advance: bool) -> Result<(), LayoutError> {
        let page = self.page;
        let Some(area) = self.area(page) else {
            // The footnote content waiting for an omitted area is omitted with it.
            self.footnotes.carried.clear();
            return Ok(());
        };
        let page_height = self.fragmentainers.height(page);
        let room = page_height - self.y - area.edges(TOP) - area.edges(BOTTOM);
        let limit = match area.maximum {
            Some(maximum) if !self.footnotes_only(page) => room.min(maximum),
            _ => room,
        };
        if self.footnotes.pages.len() <= page {
            self.footnotes.pages.resize_with(page + 1, Vec::new);
        }
        let unbounded = self.footnotes_only(page);
        let width = area.columns.width;
        while let Some(&waiting) = self.footnotes.carried.front() {
            let used = self.footnotes.pages[page].clone();
            let holds = |paginator: &Self, part: FootnotePart| {
                let mut parts = used.clone();
                parts.push(part);
                paginator
                    .area_content(&parts, &area, unbounded)
                    .is_some_and(|content| content.fits(limit))
            };
            if holds(self, waiting) {
                self.record_inline_pages(waiting, page, width)?;
                self.footnotes.pages[page].push(waiting);
                self.footnotes.carried.pop_front();
                continue;
            }
            let layout = self.footnote_layout(waiting.body, width)?;
            let edges = layout.cloned.unwrap_or_default();
            // A slice of a body takes a line of its own; a cloned body's slice ends with its
            // block-end edges as well.
            let slice = |bottom: f32| FootnotePart {
                bottom,
                inline: false,
                repeated: [
                    waiting.repeated[0],
                    if bottom < layout.height {
                        edges[1]
                    } else {
                        0.0
                    },
                ],
                ..waiting
            };
            let breaks = &layout.breaks;
            // The breaks within the part; none when it is empty, as an empty body's is.
            let start = breaks.partition_point(|&offset| offset <= waiting.top);
            let end = breaks
                .partition_point(|&offset| offset < waiting.bottom)
                .max(start);
            let candidates = &breaks[start..end];
            let fitting = candidates.partition_point(|&offset| holds(self, slice(offset)));
            let forced = advance && used.is_empty() && !self.has_flow;
            let Some(bottom) = fitting
                .checked_sub(1)
                .map(|last| candidates[last])
                .or_else(|| forced.then(|| candidates.first().copied().unwrap_or(waiting.bottom)))
            else {
                break;
            };
            let slice = slice(bottom);
            self.record_inline_pages(slice, page, width)?;
            self.footnotes.pages[page].push(slice);
            if bottom >= waiting.bottom {
                self.footnotes.carried.pop_front();
            } else {
                let rest = self
                    .footnotes
                    .carried
                    .front_mut()
                    .expect("the waiting footnote continues");
                rest.top = bottom;
                rest.repeated[0] = edges[0];
                break;
            }
        }
        self.height = fragmentainer_height(page_height - self.footnote_reserve(page));
        Ok(())
    }

    /// Paints each page's footnote area at the bottom of its page area, returning the
    /// navigation of the footnote bodies by page, and the text of each page's bodies when its
    /// page box's backgrounds clip to text.
    pub(super) fn paint_footnotes(
        &mut self,
        page_count: usize,
    ) -> Result<FootnotePaint, LayoutError> {
        let mut navigation = Vec::new();
        let mut page_text: Box<[Vec<DisplayItem>]> = vec![Vec::new(); page_count].into();
        for page in 0..page_count.min(self.footnotes.pages.len()) {
            let Some(area) = self.area(page) else {
                continue;
            };
            let unbounded = self.footnotes_only(page);
            let Some(content) = self.area_content(self.footnotes.parts(page), &area, unbounded)
            else {
                continue;
            };
            let (style, decoration) = self.area_box(page).expect("footnote areas belong to pages");
            let Fragmentainers::Pages(pages) = &self.fragmentainers else {
                unreachable!("footnote areas belong to pages");
            };
            let flow = crate::style::logical::Flow::used(pages.document.root().style());
            let page_clips = pages.boxes[page].decoration.backgrounds.clips_to_text();
            let decoration = decoration.into_owned();
            let page_area = self.fragmentainers.content_box(page);
            let height = area.content_height(content.height(), unbounded);
            let border_box = Rect {
                x: area.x,
                y: page_area.y + page_area.height
                    - area.margins[BOTTOM]
                    - area.borders[BOTTOM]
                    - area.padding[BOTTOM]
                    - height
                    - area.padding[TOP]
                    - area.borders[TOP],
                width: area.width
                    + area.padding[LEFT]
                    + area.padding[RIGHT]
                    + area.borders[LEFT]
                    + area.borders[RIGHT],
                height: height
                    + area.padding[TOP]
                    + area.padding[BOTTOM]
                    + area.borders[TOP]
                    + area.borders[BOTTOM],
            };
            // CSS Backgrounds 4 §2.8: the area's text-clipped backgrounds paint within the
            // text of the footnote bodies it holds.
            let clips_to_text = decoration.backgrounds.clips_to_text();
            let visible = style.visibility.is_visible();
            let paint = BoxPaint {
                padding: area.padding,
                ..BoxPaint::whole(border_box, decoration)
            };
            // The area transforms about its border box like a block box, and composites as one
            // group, which its bodies' boxes join.
            let transform = self.transform_on(&style, &paint.fragment_areas(), None, page)?;
            if transform.as_ref().is_some_and(Region::collapses) {
                continue;
            }
            // CSS Transforms 2 §4.1.4: the area's perspective projects its transformed bodies.
            let perspective = {
                let frame = crate::layout::writing::Frame::for_style(&style);
                style
                    .parent_3d(&frame.boxes(&paint.fragment_areas()))
                    .map(|parent| parent.mapped(frame.inverse().matrix))
            };
            let group = Group::of(&style);
            let pushes: Vec<_> = transform
                .clone()
                .map(Region::push)
                .into_iter()
                .chain(group.pushes(std::slice::from_ref(&paint)))
                .collect();
            let backdrop = group.backdrop(&paint);
            let mut items = Vec::new();
            if visible {
                items.extend(backdrop);
                if paint.decoration.is_visible() {
                    items.push(DisplayItem::Box(paint.clone()));
                }
            }
            let mut text = Vec::new();
            let x = border_box.x + area.borders[LEFT] + area.padding[LEFT];
            let y = border_box.y + area.borders[TOP] + area.padding[TOP];
            if visible {
                items.extend(
                    content
                        .columns
                        .rules(&area.columns, area.rule, [x, y])
                        .into_iter()
                        .map(DisplayItem::Box),
                );
            }
            for (part, offset) in content.placed(area.columns) {
                let layout = self.footnote_layout(part.body, area.columns.width)?;
                let body_style = self.document_footnotes().bodies[part.body].block.style();
                let window = Rect {
                    x: 0.0,
                    y: part.top,
                    width: layout.width,
                    height: part.bottom - part.top,
                };
                let origin = [x + offset[0], y + offset[1] + part.repeated[0] - part.top];
                let sliced = part.top > 0.0 || part.bottom < layout.height;
                let clip = slice_clip(window);
                // The part's slice of the body's items, placed in the area; a cloned
                // body's box wraps the slice whole, outside its clip.
                let frame = |mut items: Vec<DisplayItem>, lines: Vec<TextLine>| {
                    let body = layout
                        .body_box
                        .and_then(|rect| body_box(&items, rect).map(|index| (index, rect)));
                    if let (Some(perspective), Some((index, _))) = (perspective, body) {
                        project_body(
                            &mut items,
                            index,
                            body_style,
                            perspective,
                            [origin[0], origin[1], layout.width, layout.height],
                        );
                    }
                    if let Some((index, rect)) = body.filter(|_| sliced && layout.cloned.is_some())
                    {
                        let DisplayItem::Box(paint) = &mut items[index] else {
                            unreachable!("the body's box is a box paint");
                        };
                        let top = rect.y.max(part.top) - part.repeated[0];
                        let bottom = (rect.y + rect.height).min(part.bottom) + part.repeated[1];
                        paint.rect = Rect {
                            y: top,
                            height: bottom - top,
                            ..paint.rect
                        };
                        paint.background_box = None;
                        paint.first_fragment = true;
                        paint.last_fragment = true;
                        let end = content_end(&items, index);
                        items.insert(end, DisplayItem::Pop);
                        items.insert(index + 1, clip.clone());
                    } else if sliced {
                        items.insert(0, clip.clone());
                        items.push(DisplayItem::Pop);
                    }
                    DisplayItem::Frame(Box::new(crate::graphics::FlowPaint {
                        matrix: [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]],
                        area: Rect {
                            height: layout.height,
                            y: 0.0,
                            ..window
                        },
                        items,
                        lines,
                    }))
                };
                // The part copies the lines it shows, which its frame clips to it.
                let shown = windowed(
                    &layout.flow.items,
                    &layout.flow.lines,
                    [part.top, part.bottom],
                );
                self.measurer.work.spend(shown.1.len())?;
                items.push(frame(shown.0, shown.1));
                if (clips_to_text || page_clips)
                    && let Some(area) = &layout.flow.text_area
                {
                    let shown = windowed(&area.items, &area.lines, [part.top, part.bottom]);
                    self.measurer.work.spend(shown.1.len())?;
                    text.push(frame(shown.0, shown.1));
                }
                let placed = layout.flow.navigation.translated(origin, window);
                navigation.push((
                    page,
                    match &transform {
                        Some(region) => placed.transformed(region),
                        None => placed,
                    },
                ));
                // CSS Images 4 §2.7: a body broken across footnote areas is imaged as laid
                // out whole, which its first part starts.
                if part.top == 0.0 {
                    self.measurer
                        .element_targets
                        .displayed
                        .borrow_mut()
                        .adopt((*layout.flow.renderings).clone().physical(flow));
                }
            }
            if clips_to_text
                && let Some(DisplayItem::Box(paint)) = items
                    .iter_mut()
                    .find(|item| matches!(item, DisplayItem::Box(_)))
            {
                paint.text_area = Some(std::sync::Arc::new(crate::graphics::FlowPaint {
                    matrix: IDENTITY,
                    area: page_area,
                    items: text.clone(),
                    lines: Vec::new(),
                }));
            }
            if page_clips {
                page_text[page] = match &transform {
                    Some(region) => std::iter::once(region.clone().push())
                        .chain(text)
                        .chain(std::iter::once(DisplayItem::Pop))
                        .collect(),
                    None => text,
                };
            }
            let pops = pushes.len();
            let items = pushes
                .into_iter()
                .chain(items)
                .chain(std::iter::repeat_n(DisplayItem::Pop, pops));
            let scope: PaintKey = Rc::from([FLOW_LEVEL]);
            let (clip, group) = self.root_paint.unwrap_or_default();
            for item in items {
                self.foreground[page].push(ScopedPaint {
                    native: None,
                    visible: true,
                    order: self.orders.take(),
                    scope: scope.clone(),
                    clip,
                    group,
                    ink: None,
                    item,
                    navigation: BoxNavigation::default(),
                });
            }
        }
        Ok((navigation, page_text))
    }
}

/// Replaces the transform of the footnote body whose box paints at `index` of its display
/// list by its plane under the footnote area's `perspective`, given in page space; the body's
/// layout of `[width, height]` is placed at `[x, y]` (CSS Transforms 2 §4.1.4).
fn project_body(
    items: &mut [DisplayItem],
    index: usize,
    style: &crate::style::Style,
    perspective: crate::style::Parent3d,
    [x, y, width, height]: [f32; 4],
) {
    let DisplayItem::Box(paint) = &items[index] else {
        unreachable!("the body's box is a box paint");
    };
    let areas = paint.fragment_areas();
    let Some(push) = items[..index].iter().rposition(|item| {
        matches!(
            item,
            DisplayItem::PushTransform(_) | DisplayItem::PushProjection(_)
        )
    }) else {
        return;
    };
    let frame = crate::layout::writing::Frame::for_style(style);
    let parent = perspective
        .mapped([1.0, 0.0, 0.0, 1.0, -x, -y])
        .mapped(frame.matrix);
    let reference = crate::style::motion::Reference::rect(Rect {
        x: 0.0,
        y: 0.0,
        width,
        height,
    });
    if let Some(plane) = style.plane(&frame.boxes(&areas), Some(&parent), &reference) {
        let plane =
            crate::style::transform::conjugated(&plane, frame.inverse().matrix, frame.matrix);
        items[push] = Region::projected(style.projected(&plane, &areas)).push();
    }
}

/// The index in a footnote body's display list of its own box, whose border box is `rect`.
fn body_box(items: &[DisplayItem], rect: Rect) -> Option<usize> {
    items
        .iter()
        .position(|item| matches!(item, DisplayItem::Box(paint) if paint.rect == rect))
}

/// Where the content after the box at `index` ends: before the pop closing the first group
/// still open at the box, or at the end.
fn content_end(items: &[DisplayItem], index: usize) -> usize {
    let depth = |item: &DisplayItem| match item {
        DisplayItem::Pop => -1,
        item if item.is_push() => 1,
        _ => 0,
    };
    let open: i32 = items[..index].iter().map(depth).sum();
    let mut level = open;
    for (offset, item) in items[index + 1..].iter().enumerate() {
        level += depth(item);
        if level < open {
            return index + 1 + offset;
        }
    }
    items.len()
}

/// The navigation of the footnote bodies by page, and the text of each page's bodies.
type FootnotePaint = (Vec<(usize, StandaloneNavigation)>, Box<[Vec<DisplayItem>]>);