ferri-pdf 0.3.0

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
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
mod dimensions;
use crate::document::{BlockBox, CounterSnapshot, PageBox, Paragraph, RunningStringAssignment};
use crate::graphics::{BoxDecoration, BoxPaint, DisplayItem, FlowPaint, ImagePaint, Rect};
use crate::layout::measure::{MeasuredBlock, Measurer};
use crate::layout::multicol::{
    ColumnGeometry, WrapRows,
    slices::{Distribution, Plan, Run, line_breaks, slice_clip, windowed},
};
use crate::layout::pagination::{Group, Region, StandaloneFlow};
use crate::layout::{LayoutError, TextLine};
use crate::style::{BoxSizing, MarginLocation, PageMargin, Size, VerticalAlign};
use crate::text::Line;
use dimensions::{Variable, variable};

const HORIZONTAL: usize = 0;
const VERTICAL: usize = 1;
const START: usize = 0;
const CENTER: usize = 1;
const END: usize = 2;
const IDENTITY: [f32; 6] = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
const GROUPS: [[MarginLocation; 3]; 4] = [
    [
        MarginLocation::TopLeft,
        MarginLocation::TopCenter,
        MarginLocation::TopRight,
    ],
    [
        MarginLocation::BottomLeft,
        MarginLocation::BottomCenter,
        MarginLocation::BottomRight,
    ],
    [
        MarginLocation::LeftTop,
        MarginLocation::LeftMiddle,
        MarginLocation::LeftBottom,
    ],
    [
        MarginLocation::RightTop,
        MarginLocation::RightMiddle,
        MarginLocation::RightBottom,
    ],
];
struct Prepared<'a, 't> {
    margin: &'a PageMargin,
    decoration: &'a BoxDecoration,
    paragraph: Option<Paragraph>,
    /// The sizes and line metrics of the paragraph's `content` images.
    images: (Vec<[f32; 2]>, Vec<crate::text::AtomicMetrics>),
    /// The image a `<content-replacement>` replaces the box with (CSS Content 3 ยง1).
    replacement: Option<std::sync::Arc<crate::images::Image>>,
    /// The running element `element()` places (CSS GCPM 3 ยง1.2.2), laid out as the box's
    /// only child.
    element: Option<&'t BlockBox>,
    /// The running element laid out alone, and the block size it takes.
    flow: Option<(StandaloneFlow, f32)>,
    /// The box's block size once fixed or resolved, which its columns fill.
    block_size: Option<f32>,
    /// The column boxes of a multi-column margin box, a block container of its generated
    /// content (CSS Page 3 ยง5.2), and the content laid out in them.
    columns: Option<(ColumnGeometry, Distribution)>,
    /// Text areas collect the box's text: its own backgrounds or its page box's clip to text.
    text_area: bool,
    padding: [f32; 4],
    borders: [f32; 4],
    margins: [Option<f32>; 4],
    specified: [Option<f32>; 2],
    start: [f32; 2],
    content: [f32; 2],
    lines: Vec<Line>,
}
impl<'t> Prepared<'_, 't> {
    fn inline_axis(&self) -> usize {
        if self.margin.style.used_writing_mode().vertical() {
            VERTICAL
        } else {
            HORIZONTAL
        }
    }
    fn sides(axis: usize) -> [usize; 2] {
        if axis == HORIZONTAL { [3, 1] } else { [0, 2] }
    }
    fn edges(&self, axis: usize) -> f32 {
        Self::sides(axis)
            .iter()
            .map(|&i| self.padding[i] + self.borders[i])
            .sum()
    }
    fn outer_edges(&self, axis: usize) -> f32 {
        self.edges(axis)
            + Self::sides(axis)
                .iter()
                .map(|&i| self.margins[i].unwrap_or(0.0))
                .sum::<f32>()
    }
    fn fixed(&mut self, axis: usize, available: f32) {
        let [start, end] = Self::sides(axis).map(|i| self.margins[i].unwrap_or(0.0));
        self.start[axis] = start;
        // The fixed dimension fills the margin area regardless of width/height declarations.
        self.content[axis] = available - self.edges(axis) - start - end;
        self.resolved(axis);
    }
    /// Records the content size along `axis` as resolved, which fixes the block size.
    fn resolved(&mut self, axis: usize) {
        if axis != self.inline_axis() {
            self.block_size = Some(self.content[axis]);
        }
    }
    fn variable(&mut self, axis: usize, available: f32, outer: f32, position: usize) {
        self.content[axis] = (outer - self.outer_edges(axis)).max(0.0);
        let style = &self.margin.style;
        if axis == HORIZONTAL && *style.width.basis() == Size::Auto {
            // CSS Values 5 ยง10.2: a `calc-size()` adjusts the automatic width once resolved.
            self.content[axis] = crate::style::SizeConstraints::new(
                [&style.width, &Size::Auto, &Size::Auto],
                Some(available),
                style.box_sizing,
                self.edges(axis),
            )
            .preferred(self.content[axis]);
        }
        self.resolved(axis);
        let offset = match position {
            START => 0.0,
            CENTER => (available - outer) / 2.0,
            END => available - outer,
            _ => unreachable!(),
        };
        self.start[axis] = offset + self.margins[Self::sides(axis)[0]].unwrap_or(0.0);
    }
    fn validate(&self) -> Result<(), LayoutError> {
        let values = self
            .padding
            .iter()
            .chain(self.borders.iter())
            .chain(self.margins.iter().flatten())
            .chain(self.specified.iter().flatten())
            .chain(self.start.iter())
            .chain(self.content.iter())
            .copied()
            .chain(
                self.lines
                    .iter()
                    .flat_map(|line| [line.ascent, line.height]),
            );
        if values.into_iter().any(|value| !value.is_finite())
            || !self.block_extent().is_finite()
            || [HORIZONTAL, VERTICAL]
                .iter()
                .any(|&axis| !self.outer_edges(axis).is_finite())
        {
            return Err(LayoutError::NonFinitePageMarginGeometry {
                location: self.margin.location,
            });
        }
        Ok(())
    }
    /// Whether the box is a multi-column container: a block container, which a box that an
    /// image replaces is not, whose column count or width is not `auto` (CSS Multi-column
    /// Layout 1 ยง2).
    fn multicol(&self) -> bool {
        self.margin.style.multicol.is_multicol() && self.replacement.is_none()
    }
    /// How the content fills the columns of `geometry`: those of the box's block size once
    /// that is resolved, else balanced (CSS Multi-column Layout 1 ยง7.1).
    fn plan(&self, geometry: &ColumnGeometry) -> Plan {
        let multicol = &self.margin.style.multicol;
        Plan {
            count: geometry.count,
            height: multicol.height.or(self.block_size),
            definite: multicol.height.is_some(),
            fill: multicol.fill,
            wrap: WrapRows::of(&self.margin.style, self.block_size),
        }
    }
    /// The content, `extent` tall with column breaks allowed at `breaks`, in the columns of
    /// `geometry`.
    fn distribute(&self, geometry: &ColumnGeometry, breaks: &[f32], extent: f32) -> Distribution {
        Distribution::of(
            &[Run {
                breaks,
                ..Run::whole(extent)
            }],
            self.plan(geometry),
        )
    }
    /// The generated lines in the columns of `geometry`.
    fn line_columns(&self, geometry: &ColumnGeometry) -> Distribution {
        let mut top = 0.0;
        let boxes: Vec<[f32; 2]> = self
            .lines
            .iter()
            .map(|line| {
                top += line.height;
                [top - line.height, top]
            })
            .collect();
        let style = &self.margin.style;
        let breaks = line_breaks(&boxes, [], [style.orphans, style.widows], top);
        self.distribute(geometry, &breaks, top)
    }
    /// Where each line's box starts in the content box, in the box's logical coordinates:
    /// stacked, or in the slice of the column box holding it.
    fn line_offsets(&self) -> Vec<[f32; 2]> {
        let mut top = 0.0;
        let tops = self.lines.iter().map(|line| {
            top += line.height;
            top - line.height
        });
        let Some((geometry, columns)) = &self.columns else {
            return tops.map(|y| [0.0, y]).collect();
        };
        let placed: Vec<_> = columns.placed(*geometry).collect();
        let mut slice = 0;
        tops.map(|y| {
            while slice + 1 < placed.len() && y >= placed[slice].0.bottom {
                slice += 1;
            }
            let (held, [x, top]) = placed[slice];
            [x, top + y - held.top]
        })
        .collect()
    }
    /// The running element laid out alone at `width` from its measurement.
    fn element_flow(
        &self,
        measurer: &mut Measurer<'t>,
        measured: MeasuredBlock<'t>,
        width: f32,
        destinations: usize,
    ) -> Result<(StandaloneFlow, f32), LayoutError> {
        // The margin box is a block formatting context root, so the element's margins stay
        // inside it.
        let extent = 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 flow = crate::layout::pagination::Paginator::standalone(
            measurer,
            measured,
            Rect {
                x: 0.0,
                y: 0.0,
                width,
                height: extent,
            },
            destinations,
            self.text_area,
        )?;
        Ok((flow, extent))
    }
    fn layout(
        &mut self,
        measurer: &mut Measurer<'t>,
        destinations: usize,
    ) -> Result<(), LayoutError> {
        self.validate()?;
        let inline_axis = self.inline_axis();
        let [inline, extent] = [self.content[inline_axis], self.content[1 - inline_axis]];
        let margin = self.margin;
        let style = &margin.style;
        // CSS Overflow 4 ยง5.3: a margin box, a block container, discards the content after
        // its region break, which follows its columns inside its block size.
        let discards = style.clamp.continuation == crate::style::Continue::Discard;
        let geometry = self
            .multicol()
            .then(|| ColumnGeometry::resolve(style, 0.0, inline));
        let width = geometry.map_or(inline, |geometry| geometry.width);
        self.columns = None;
        if let Some(element) = self.element {
            let rtl = style.used_rtl();
            let orphans_widows = [element.style().orphans, element.style().widows];
            let clipped = |measurer: &mut Measurer<'t>, extent: f32| {
                measurer.discarded_block(element, width, rtl, extent, style.clamp.max_lines)
            };
            let measured = if discards && geometry.is_none() {
                clipped(measurer, extent)?
            } else {
                measurer.block(element, 0.0, width, rtl, false, None)?
            };
            let mut flow = self.element_flow(measurer, measured, width, destinations)?;
            if let Some(geometry) = geometry {
                let mut columns =
                    self.distribute(&geometry, &flow.0.breaks(orphans_widows, flow.1), flow.1);
                if discards {
                    let held = clipped(measurer, columns.held(geometry.count, extent))?;
                    flow = self.element_flow(measurer, held, width, destinations)?;
                    columns =
                        self.distribute(&geometry, &flow.0.breaks(orphans_widows, flow.1), flow.1);
                }
                self.columns = Some((geometry, columns));
            }
            self.flow = Some(flow);
            return Ok(());
        }
        let Some(paragraph) = &self.paragraph else {
            self.lines = Vec::new();
            return Ok(());
        };
        let atomics = &self.images.1;
        self.lines = if discards && geometry.is_none() {
            measurer.discarded_text_lines(paragraph, width, atomics, extent, &style.clamp)?
        } else {
            measurer.text_lines(paragraph, width, atomics)?
        };
        if let Some(geometry) = geometry {
            let mut columns = self.line_columns(&geometry);
            if discards {
                let held = columns.held(geometry.count, extent);
                self.lines =
                    measurer.discarded_text_lines(paragraph, width, atomics, held, &style.clamp)?;
                columns = self.line_columns(&geometry);
            }
            self.columns = Some((geometry, columns));
        }
        Ok(())
    }
    /// The content's min-content and max-content contributions in the inline axis, which a
    /// multi-column box repeats in each column (CSS Multi-column Layout 1 ยง3.4).
    fn intrinsic(
        &self,
        measurer: &mut Measurer<'t>,
        available: f32,
    ) -> Result<[f32; 2], LayoutError> {
        if self.replacement.is_some() {
            let inline = self.inline_axis();
            let width = self.replaced_extent(inline, self.content[1 - inline]);
            return Ok([width; 2]);
        }
        let widths = self.content_intrinsic(measurer, available)?;
        let widths = if self.multicol() {
            crate::layout::multicol::intrinsic(&self.margin.style, widths)
        } else {
            widths
        };
        Ok([widths.min, widths.max])
    }
    /// The box's outer size bounds along variable `axis` of its `containing` margin area,
    /// once it fills the area's fixed axis.
    fn dimension(
        &mut self,
        measurer: &mut Measurer<'t>,
        axis: usize,
        containing: [f32; 2],
        destinations: usize,
    ) -> Result<Variable, LayoutError> {
        self.validate()?;
        let fixed_axis = 1 - axis;
        self.fixed(fixed_axis, containing[fixed_axis]);
        let bounds = if axis == self.inline_axis() {
            self.intrinsic(measurer, containing[axis])?
        } else {
            self.layout(measurer, destinations)?;
            [self.block_extent(); 2]
        };
        if axis == HORIZONTAL && self.margin.style.width.intrinsic().is_some() {
            let available = containing[axis] - self.outer_edges(axis);
            let style = &self.margin.style;
            let sizes = [&style.width, &Size::Auto, &Size::Auto];
            self.specified[axis] = crate::style::SizeConstraints::new(
                sizes,
                Some(containing[axis]),
                style.box_sizing,
                self.edges(axis),
            )
            .with_intrinsic(
                sizes,
                crate::style::IntrinsicWidths {
                    min: bounds[0],
                    max: bounds[1],
                },
                Some(available.max(0.0)),
            )
            .specified;
        }
        let edges = self.outer_edges(axis);
        Ok(Variable {
            min: bounds[0] + edges,
            max: bounds[1] + edges,
            specified: self.specified[axis].map(|size| size + edges),
            generated: true,
        })
    }
    fn content_intrinsic(
        &self,
        measurer: &mut Measurer<'t>,
        available: f32,
    ) -> Result<crate::text::IntrinsicWidths, LayoutError> {
        if let Some(element) = self.element {
            return measurer
                .run(|measurer| crate::layout::intrinsic::measure(element, measurer, available));
        }
        let atomics: Vec<_> = self
            .images
            .0
            .iter()
            .map(|&[width, _]| crate::text::IntrinsicWidths {
                min: width,
                max: width,
            })
            .collect();
        Ok(self
            .paragraph
            .as_ref()
            .map(|p| measurer.intrinsic_widths(p, &atomics, &atomics))
            .transpose()?
            .unwrap_or_default())
    }
    fn block_extent(&self) -> f32 {
        if let Some((_, columns)) = &self.columns {
            return columns.height();
        }
        if let Some((_, extent)) = &self.flow {
            return *extent;
        }
        if self.replacement.is_some() {
            let inline = self.inline_axis();
            return self.replaced_extent(1 - inline, self.content[inline]);
        }
        self.lines.iter().map(|line| line.height).sum()
    }
    /// The replaced content's extent along physical `axis` when the other axis spans `other`:
    /// its natural size, or the size its natural ratio transfers, as for a replaced element
    /// whose other dimension is specified (CSS 2.1 ยง10.3.2, ยง10.6.2).
    fn replaced_extent(&self, axis: usize, other: f32) -> f32 {
        let image = self
            .replacement
            .as_ref()
            .expect("only a replaced margin box has replaced content");
        let natural = crate::paint::natural_points(
            image,
            crate::paint::INITIAL_IMAGE_SCALE,
            self.margin.style.effective_zoom,
        );
        let mut size = [
            crate::paint::SizeComponent::Auto,
            crate::paint::SizeComponent::Auto,
        ];
        size[1 - axis] =
            crate::paint::SizeComponent::Value(crate::paint::LengthPercentage::Length(other));
        crate::paint::image_size(
            &crate::paint::BackgroundSize::Explicit(size),
            natural,
            crate::images::DEFAULT_OBJECT_SIZE
                .map(|extent| extent * crate::style::POINTS_PER_PIXEL),
        )[axis]
    }
    /// The decoration, then the content laid out at its vertical alignment, then the outline.
    fn paint(
        mut self,
        origin: [f32; 2],
        index: usize,
        output: &mut MarginOutput<'_>,
        measurer: &mut Measurer<'t>,
    ) -> Result<(), LayoutError> {
        self.validate()?;
        let rect = Rect {
            x: origin[0] + self.start[0],
            y: origin[1] + self.start[1],
            width: self.content[0] + self.edges(0),
            height: self.content[1] + self.edges(1),
        };
        if [
            rect.x,
            rect.y,
            rect.width,
            rect.height,
            rect.x + rect.width,
            rect.y + rect.height,
        ]
        .iter()
        .any(|value| !value.is_finite())
        {
            return Err(LayoutError::NonFinitePageMarginGeometry {
                location: self.margin.location,
            });
        }
        let margin = self.margin;
        let style = &margin.style;
        let paint = BoxPaint {
            padding: self.padding,
            ..BoxPaint::whole(rect, self.decoration.clone())
        };
        let areas = paint.fragment_areas();
        let transform = style
            .plane(
                &areas,
                output.perspective.as_ref(),
                &crate::style::motion::Reference::rect(output.page),
            )
            .map(|plane| Region::projected(style.projected(&plane, &areas)));
        if transform.as_ref().is_some_and(Region::collapses) {
            return Ok(());
        }
        // CSS Page 3 ยง3.1: every page-margin box establishes a stacking context, which
        // composites as one group.
        let group = Group::of(style);
        let opening: Vec<_> = transform
            .clone()
            .map(Region::push)
            .into_iter()
            .chain(group.pushes(std::slice::from_ref(&paint)))
            .collect();
        output.display.extend(opening.iter().cloned());
        let links = output.links.len();
        // Generated content takes the margin box's style, so its visibility hides the box and
        // all of that content; a running element keeps its own (CSS GCPM 3 ยง1.2.1).
        let visible = style.visibility.is_visible();
        let outline = paint.outline().filter(|_| visible);
        let replaced = self
            .replacement
            .take()
            .map(|image| self.replaced(image, &paint))
            .filter(|_| visible);
        // CSS Overflow 3 ยง3.1: the overflow clips the box's content at its padding or overflow
        // clip edge, and the box's outline paints over them.
        let clips: Vec<_> = style
            .clip_edges()
            .map(|edge| style.overflow_clip(&paint, edge))
            .collect();
        // CSS Backgrounds 4 ยง2.8: `background-clip: text` layers paint within the text of
        // the box's content, which a page box's text holds too.
        let clips_to_text = self.decoration.backgrounds.clips_to_text();
        let text = self.text_area;
        if visible {
            output.display.extend(group.backdrop(&paint));
        }
        let painted = (visible && self.decoration.is_visible()).then(|| {
            output.display.push(DisplayItem::Box(paint));
            output.display.len() - 1
        });
        output
            .display
            .extend(clips.iter().cloned().map(DisplayItem::PushClip));
        let content = output.display.len();
        let extent = self.block_extent();
        let text_area = if let Some((flow, _)) = self.flow.take() {
            self.paint_element(flow, extent, rect, output, measurer)
        } else if let Some(image) = replaced {
            output.display.push(DisplayItem::Image(image));
            text.then(|| text_flow(&[], &[], rect))
        } else if visible {
            self.paint_lines(rect, index, output.lines, output.display);
            text.then(|| text_flow(&output.display[content..], output.lines, rect))
        } else {
            text.then(|| text_flow(&[], &[], rect))
        }
        .map(|area| {
            wrapped_flow(
                area,
                clips.iter().cloned().map(DisplayItem::PushClip).collect(),
                rect,
            )
        });
        output
            .display
            .extend(clips.iter().map(|_| DisplayItem::Pop));
        if let (Some(page_text), Some(area)) = (&mut output.page_text, &text_area) {
            let area = wrapped_flow(
                area.clone(),
                transform.iter().cloned().map(Region::push).collect(),
                output.page,
            );
            page_text.items.push(DisplayItem::Frame(Box::new(area)));
        }
        if clips_to_text
            && let (Some(slot), Some(area)) = (painted, text_area)
            && let DisplayItem::Box(paint) = &mut output.display[slot]
        {
            paint.text_area = Some(std::sync::Arc::new(area));
        }
        // The outline paints over the margin box's content (CSS UI 4 ยง3.1).
        output.display.extend(outline.map(DisplayItem::Box));
        output
            .display
            .extend(opening.iter().map(|_| DisplayItem::Pop));
        if let Some(region) = transform {
            let areas = output.links.split_off(links);
            output.links.extend(transformed_links(areas, &region));
        }
        Ok(())
    }
    /// The replaced content fitted into the content box by `object-fit` and
    /// `object-position`, clipped to the content edge (CSS Images 3 ยง4.5); content without
    /// natural dimensions fills it.
    fn replaced(
        &self,
        image: std::sync::Arc<crate::images::Image>,
        paint: &BoxPaint,
    ) -> ImagePaint {
        let style = &self.margin.style;
        let clip = paint.inner_clip(self.padding, [true; 2]);
        let content = clip.rect;
        let box_size = [content.width, content.height];
        let [width, height] = if image.natural_size() == Default::default() {
            box_size
        } else {
            let scale = crate::style::POINTS_PER_PIXEL * style.effective_zoom;
            let natural = image
                .pixel_size(content.width / scale)
                .map(|extent| extent * scale);
            style.object_fit.size(natural, box_size)
        };
        ImagePaint {
            image,
            rect: Rect {
                x: content.x + style.object_position[0].resolve(content.width - width),
                y: content.y + style.object_position[1].resolve(content.height - height),
                width,
                height,
            },
            clip: Some(clip),
            source_scale: style.effective_zoom,
            rendering: style.image_rendering(),
        }
    }
    fn vertical_offset(&self, physical_height: f32) -> f32 {
        let extra = self.content[VERTICAL] - physical_height;
        match self.margin.style.table.vertical_align {
            VerticalAlign::Middle => extra / 2.0,
            VerticalAlign::Bottom => extra,
            VerticalAlign::Top
            | VerticalAlign::Baseline
            | VerticalAlign::BaselineMiddle
            | VerticalAlign::Sub
            | VerticalAlign::Super
            | VerticalAlign::TextTop
            | VerticalAlign::TextBottom
            | VerticalAlign::Length(_) => 0.0,
        }
    }
    fn content_origin(&self, rect: Rect, physical_height: f32) -> [f32; 2] {
        [
            rect.x + self.padding[3] + self.borders[3],
            rect.y + self.padding[0] + self.borders[0] + self.vertical_offset(physical_height),
        ]
    }
    /// A running element lays out unbroken in the content box, in the margin box's flow, or
    /// sliced into the box's column boxes; `extent` is the block size it takes there.
    fn paint_element(
        &self,
        flow: StandaloneFlow,
        extent: f32,
        rect: Rect,
        output: &mut MarginOutput<'_>,
        measurer: &mut Measurer<'t>,
    ) -> Option<FlowPaint> {
        let vertical = self.inline_axis() == VERTICAL;
        let logical = [self.content[self.inline_axis()], extent];
        let size = if vertical {
            [logical[1], logical[0]]
        } else {
            logical
        };
        let margin_flow = crate::style::logical::Flow::used(&self.margin.style);
        let frame = crate::layout::writing::Frame::physical(margin_flow, size);
        // CSS Images 4 ยง2.7: a running element is copied into each page that places it
        // (CSS GCPM 3 ยง1.2.2); `element()` images the copy on the first page showing it.
        measurer
            .element_targets
            .displayed
            .borrow_mut()
            .adopt(flow.renderings.physical(margin_flow));
        let (mut items, mut text_area, lines, mut navigation) = match &self.columns {
            None => (
                flow.items,
                flow.text_area,
                flow.lines,
                flow.navigation.into_links(),
            ),
            Some((geometry, columns)) => {
                let mut items: Vec<DisplayItem> = columns
                    .rules(geometry, self.margin.style.multicol.rule, [0.0; 2])
                    .into_iter()
                    .map(DisplayItem::Box)
                    .collect();
                let mut text = Vec::new();
                let mut navigation = Vec::new();
                // Each slice is the flow placed in its column box and clipped to the slice.
                let sliced =
                    |items: &[DisplayItem], lines: &[TextLine], origin: [f32; 2], window: Rect| {
                        let (items, lines) =
                            windowed(items, lines, [window.y, window.y + window.height]);
                        DisplayItem::Frame(Box::new(FlowPaint {
                            matrix: [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]],
                            area: Rect {
                                y: 0.0,
                                height: extent,
                                ..window
                            },
                            items: std::iter::once(slice_clip(window))
                                .chain(items)
                                .chain(std::iter::once(DisplayItem::Pop))
                                .collect(),
                            lines,
                        }))
                    };
                for (slice, [x, y]) in columns.placed(*geometry) {
                    let origin = [x, y - slice.top];
                    let window = Rect {
                        x: 0.0,
                        y: slice.top,
                        width: geometry.width,
                        height: slice.bottom - slice.top,
                    };
                    items.push(sliced(&flow.items, &flow.lines, origin, window));
                    if let Some(area) = &flow.text_area {
                        text.push(sliced(&area.items, &area.lines, origin, window));
                    }
                    navigation.extend(flow.navigation.translated(origin, window).into_links());
                }
                let text_area = flow.text_area.map(|area| FlowPaint {
                    items: text,
                    lines: Vec::new(),
                    ..area
                });
                (items, text_area, Vec::new(), navigation)
            }
        };
        if vertical {
            frame.items(&mut items);
            if let Some(area) = &mut text_area {
                frame.items(&mut area.items);
            }
            for area in &mut navigation {
                match &mut area.shape {
                    crate::layout::LinkShape::Rect(rect) => *rect = frame.rect(*rect),
                    crate::layout::LinkShape::Quads(quads) => {
                        for point in quads.iter_mut().flatten() {
                            *point = frame.point(*point);
                        }
                    }
                }
            }
        }
        let origin = self.content_origin(rect, size[1]);
        for mut area in navigation {
            match &mut area.shape {
                crate::layout::LinkShape::Rect(rect) => {
                    rect.x += origin[0];
                    rect.y += origin[1];
                }
                crate::layout::LinkShape::Quads(quads) => {
                    for point in quads.iter_mut().flatten() {
                        point[0] += origin[0];
                        point[1] += origin[1];
                    }
                }
            }
            output.links.push(area);
        }
        let matrix = [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]];
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: size[0],
            height: size[1],
        };
        output.display.push(DisplayItem::Frame(Box::new(FlowPaint {
            matrix,
            area,
            items,
            lines,
        })));
        text_area.map(|text| FlowPaint {
            matrix,
            area,
            ..text
        })
    }
    fn paint_lines(
        self,
        rect: Rect,
        index: usize,
        page: &mut Vec<TextLine>,
        display: &mut Vec<DisplayItem>,
    ) {
        let style = &self.margin.style;
        let vertical = self.inline_axis() == VERTICAL;
        // Lines align in their column; the columns span the content box's inline size.
        let (inline_extent, frame_extent) = match &self.columns {
            Some((geometry, _)) => (geometry.width, self.content[self.inline_axis()]),
            None if vertical => {
                let widest = self.lines.iter().map(|line| line.width).fold(0.0, f32::max);
                (widest, widest)
            }
            None => (self.content[HORIZONTAL], self.content[HORIZONTAL]),
        };
        let physical_height = if vertical {
            frame_extent
        } else {
            self.block_extent()
        };
        let content_origin = self.content_origin(rect, physical_height);
        let mut frame_lines = Vec::new();
        let mut frame_items = Vec::new();
        let (text_lines, text_items) = if vertical {
            (&mut frame_lines, &mut frame_items)
        } else {
            (&mut *page, &mut *display)
        };
        let origin = if vertical { [0.0; 2] } else { content_origin };
        if let Some((geometry, columns)) = &self.columns {
            text_items.extend(
                columns
                    .rules(geometry, style.multicol.rule, origin)
                    .into_iter()
                    .map(DisplayItem::Box),
            );
        }
        let offsets = self.line_offsets();
        for (line, offset) in self.lines.into_iter().zip(offsets) {
            let x = origin[0] + offset[0] + line.alignment_offset(inline_extent, style.used_rtl());
            let baseline = origin[1] + offset[1] + line.ascent;
            text_items.extend(crate::layout::inline::shadow_items(
                &line.runs,
                &line.decorations,
                [x, baseline],
                &|_, _| true,
            ));
            let [below, above] =
                crate::layout::inline::decoration_items(&line.decorations, x, baseline);
            text_items.extend(below);
            text_items.push(DisplayItem::Text {
                line: text_lines.len(),
            });
            text_items.extend(above);
            let paragraph = self
                .paragraph
                .as_ref()
                .expect("a margin box's lines lay out its paragraph");
            text_items.extend(
                crate::layout::markers::content_image_paints(paragraph, &line, &self.images.0)
                    .into_iter()
                    .map(|mut image| {
                        image.rect.x += x;
                        image.rect.y += baseline;
                        DisplayItem::Image(image)
                    }),
            );
            text_lines.push(TextLine {
                paragraph: index,
                text: line.text,
                x,
                baseline,
                width: line.width,
                height: line.height,
                runs: line.runs,
                font_size: style.size,
                color: style.color,
            });
        }
        if vertical {
            let size = [self.content[HORIZONTAL], frame_extent];
            crate::layout::writing::Frame::physical(crate::style::logical::Flow::used(style), size)
                .items(&mut frame_items);
            display.push(DisplayItem::Frame(Box::new(FlowPaint {
                matrix: [1.0, 0.0, 0.0, 1.0, content_origin[0], content_origin[1]],
                area: Rect {
                    x: 0.0,
                    y: 0.0,
                    width: size[0],
                    height: size[1],
                },
                items: frame_items,
                lines: frame_lines,
            })));
        }
    }
}
/// The text and decorations among `items`, whose text lines are `lines`, drawn opaquely in a
/// flow of their own over `area`: the text a margin box's lines paint.
fn text_flow(items: &[DisplayItem], lines: &[TextLine], area: Rect) -> FlowPaint {
    let mut flow = FlowPaint {
        matrix: IDENTITY,
        area,
        items: Vec::new(),
        lines: Vec::new(),
    };
    for item in items {
        match item {
            DisplayItem::Text { line } => {
                flow.lines.push(lines[*line].clone().opaque());
                flow.items.push(DisplayItem::Text {
                    line: flow.lines.len() - 1,
                });
            }
            DisplayItem::Decoration(decoration) => {
                flow.items
                    .push(DisplayItem::Decoration(decoration.opaque()));
            }
            DisplayItem::Frame(frame) => {
                flow.items.push(DisplayItem::Frame(Box::new(FlowPaint {
                    matrix: frame.matrix,
                    ..text_flow(&frame.items, &frame.lines, frame.area)
                })));
            }
            DisplayItem::PushTransform(_) | DisplayItem::PushProjection(_) | DisplayItem::Pop => {
                flow.items.push(item.clone());
            }
            // Text areas hold text and decorations only, not images or column rules.
            DisplayItem::Image(_) | DisplayItem::TextShadow(_) | DisplayItem::Box(_) => {}
            DisplayItem::PushClip(_)
            | DisplayItem::PushOpacity(_)
            | DisplayItem::PushBlend(_)
            | DisplayItem::PushFilter(_)
            | DisplayItem::PushMask(_)
            | DisplayItem::PushBackdropRoot
            | DisplayItem::Scene(_)
            | DisplayItem::Backdrop(_) => {
                unreachable!(
                    "margin lines paint text, shadows, decorations, images, column rules and their transforms"
                )
            }
        }
    }
    flow
}
/// `area`, a margin box's text, inside the regions `pushes` open; `bounds` holds them.
fn wrapped_flow(area: FlowPaint, pushes: Vec<DisplayItem>, bounds: Rect) -> FlowPaint {
    if pushes.is_empty() {
        return area;
    }
    let pops = pushes.len();
    FlowPaint {
        matrix: IDENTITY,
        area: bounds,
        items: pushes
            .into_iter()
            .chain(std::iter::once(DisplayItem::Frame(Box::new(area))))
            .chain(std::iter::repeat_n(DisplayItem::Pop, pops))
            .collect(),
        lines: Vec::new(),
    }
}
/// `areas`, the link areas of a box's content, mapped through the box's transform.
pub(super) fn transformed_links(
    areas: Vec<crate::layout::LinkArea>,
    region: &Region,
) -> Vec<crate::layout::LinkArea> {
    let polygons: Vec<_> = areas
        .into_iter()
        .flat_map(|area| {
            let polygons = match area.shape {
                crate::layout::LinkShape::Rect(rect) => {
                    vec![crate::layout::navigation::rect_corners(rect).to_vec()]
                }
                crate::layout::LinkShape::Quads(quads) => {
                    quads.into_iter().map(|quad| quad.to_vec()).collect()
                }
            };
            polygons.into_iter().map(move |polygon| {
                (
                    area.link,
                    crate::layout::navigation::map_region(polygon, region),
                )
            })
        })
        .collect();
    crate::layout::navigation::link_areas(&polygons)
}
/// What one page's margin boxes paint, and the page's text area when its page box's
/// backgrounds clip to text, which holds their text.
struct MarginOutput<'p> {
    /// The page box, the containing block of its margin boxes.
    page: Rect,
    /// The perspective the page box gives them.
    perspective: Option<crate::style::transform::Parent3d>,
    lines: &'p mut Vec<TextLine>,
    display: &'p mut Vec<DisplayItem>,
    links: &'p mut Vec<crate::layout::LinkArea>,
    page_text: Option<&'p mut FlowPaint>,
}
/// The page-margin boxes' shared inputs: the selected strings and running elements, the
/// document's destination count, the page-based counters and the document's counters where
/// pages start.
pub(super) struct MarginContext<'s, 't> {
    pub resolver: &'s dyn crate::resources::ResourceResolver,
    pub strings: &'s crate::layout::strings::PageStrings,
    pub running: &'t [RunningStringAssignment],
    pub destinations: usize,
    pub counters: &'s crate::layout::page_counters::PageCounters,
    pub document: &'s crate::document::StyledDocument,
}
/// The running element each page-margin box places, with the `page` counter it shows.
pub(super) fn running(
    page_boxes: &[std::sync::Arc<PageBox>],
    running: &[RunningStringAssignment],
    strings: &crate::layout::strings::PageStrings,
    counters: &crate::layout::page_counters::PageCounters,
) -> Vec<(crate::style::PageStyleTarget, i128)> {
    page_boxes
        .iter()
        .enumerate()
        .flat_map(|(page, page_box)| {
            page_box.margins.iter().filter_map(move |margin| {
                let request = margin.margin.content.running_element()?;
                let element = running[strings.element(page, request)?].element.as_ref()?;
                Some((
                    element.target,
                    counters.page(page, Some(margin.margin.location)),
                ))
            })
        })
        .collect()
}
/// Prepares a page-margin box of the page at index `page` of `pages`, whose text the page
/// box collects when `page_text`.
fn prepare<'a, 't>(
    margin: &'a PageMargin,
    decoration: &'a BoxDecoration,
    containing: [f32; 2],
    page: usize,
    pages: usize,
    context: &MarginContext<'_, 't>,
    page_text: bool,
) -> Result<Prepared<'a, 't>, LayoutError> {
    let style = &margin.style;
    let bases = [containing[1], containing[0], containing[1], containing[0]];
    let padding = std::array::from_fn(|i| style.padding[i].resolve(bases[i]));
    let borders = style
        .decoration
        .borders
        .map(crate::graphics::Border::used_width);
    let margins = std::array::from_fn(|i| style.margin[i].resolve(bases[i]));
    let mut specified = [style.width.resolve(containing[0]), None];
    if style.box_sizing == BoxSizing::BorderBox {
        for axis in [HORIZONTAL, VERTICAL] {
            let edges = Prepared::sides(axis)
                .iter()
                .map(|&i| padding[i] + borders[i])
                .sum::<f32>();
            specified[axis] = specified[axis].map(|size| (size - edges).max(0.0));
        }
    }
    let mut replacement = None;
    let (paragraph, element) = if let Some(request) = margin.content.running_element() {
        (
            None,
            context
                .strings
                .element(page, request)
                .and_then(|owner| context.running[owner].element.as_ref())
                .map(|element| {
                    element.block(context.counters.page(page, Some(margin.location)), pages)
                }),
        )
    } else if let Some(image) = margin.content.replacement() {
        replacement = Some(
            context
                .document
                .margin_replacement(image, style, context.resolver),
        );
        (None, None)
    } else {
        let output = margin.content.page_output(
            context.counters.page(page, Some(margin.location)),
            pages,
            crate::document::MAX_INLINE_TEXT_BYTES,
            &style.quotes,
            &mut |request| Ok(context.strings.text(page, request)),
            &mut |request| {
                Ok(context.counters.text(
                    page,
                    margin.location,
                    request,
                    context
                        .document
                        .page_counters(page)
                        .unwrap_or(&CounterSnapshot::default()),
                ))
            },
        )?;
        crate::document::record_text_limit(
            context.document.styles().degradations(),
            output.limited(),
        );
        let images = context
            .document
            .margin_images(&output, style, context.resolver);
        (Paragraph::generated_output(&output, &images, style)?, None)
    };
    // The paragraph holds the box's first and last formatted lines (CSS Inline 3 ยง6.1).
    let paragraph = paragraph.map(|mut paragraph| {
        paragraph.set_text_box_trim(style.text_box.trim);
        paragraph
    });
    let images = paragraph
        .as_ref()
        .map(crate::layout::markers::content_image_atomics)
        .unwrap_or_default();
    Ok(Prepared {
        margin,
        decoration,
        paragraph,
        images,
        replacement,
        element,
        flow: None,
        block_size: None,
        columns: None,
        text_area: decoration.backgrounds.clips_to_text() || page_text,
        padding,
        borders,
        margins,
        specified,
        start: [0.0; 2],
        content: [0.0; 2],
        lines: Vec::new(),
    })
}

/// What a margin box's layout or paint `result` yields, or nothing when its geometry or
/// content cannot form: the box is then omitted, as layout omits such boxes of the document,
/// and the rest of its page renders.
fn unless_failed<T>(
    measurer: &Measurer<'_>,
    result: Result<T, LayoutError>,
) -> Result<Option<T>, LayoutError> {
    match result {
        Ok(value) => Ok(Some(value)),
        Err(error)
            if error.fails_at_box()
                || matches!(error, LayoutError::NonFinitePageMarginGeometry { .. }) =>
        {
            measurer
                .diagnostics
                .record(crate::layout::LayoutDiagnostic::OmittedBox {
                    reason: error.to_string(),
                });
            Ok(None)
        }
        Err(error) => Err(error),
    }
}

pub(super) fn render<'t>(
    page_boxes: &[std::sync::Arc<PageBox>],
    context: &MarginContext<'_, 't>,
    measurer: &mut Measurer<'t>,
    first_index: usize,
    (pages, display, links, text_areas, underlays): crate::layout::PageOutputs<'_>,
) -> Result<(), LayoutError> {
    let total = pages.len();
    for (number, ((((page_lines, display), links), page_text), underlay)) in pages
        .iter_mut()
        .zip(display)
        .zip(links.iter_mut())
        .zip(text_areas.iter_mut())
        .zip(underlays.iter_mut())
        .enumerate()
    {
        let page = &page_boxes[number];
        let perspective = crate::layout::page_paint::page_perspective(page);
        let margins: Vec<_> = page
            .margins
            .iter()
            .map(|margin| (&margin.margin, &margin.decoration))
            .collect();
        if margins.is_empty() {
            continue;
        }
        let page_box = page.geometry.margin_box();
        let [width, height] = [page_box.width, page_box.height];
        let [top, right, bottom, left] = page.geometry.margin;
        let groups = [
            ([left, 0.0], [width - left - right, top], HORIZONTAL),
            (
                [left, height - bottom],
                [width - left - right, bottom],
                HORIZONTAL,
            ),
            ([0.0, top], [left, height - top - bottom], VERTICAL),
            (
                [width - right, top],
                [right, height - top - bottom],
                VERTICAL,
            ),
        ];
        let page = page_lines;
        let mut stacked = Vec::with_capacity(margins.len());
        for (locations, (origin, containing, axis)) in GROUPS.iter().zip(groups) {
            let mut boxes: [Option<Prepared<'_, '_>>; 3] = [None, None, None];
            let mut dimensions = [Variable::default(); 3];
            for (position, location) in locations.iter().enumerate() {
                let Some((margin, decoration)) = margins
                    .iter()
                    .find(|(margin, _)| margin.location == *location)
                else {
                    continue;
                };
                measurer.work.spend(1)?;
                let sized = prepare(
                    margin,
                    decoration,
                    containing,
                    number,
                    total,
                    context,
                    page_text.is_some(),
                )
                .and_then(|mut prepared| {
                    let dimension =
                        prepared.dimension(measurer, axis, containing, context.destinations)?;
                    Ok((prepared, dimension))
                });
                if let Some((prepared, dimension)) = unless_failed(measurer, sized)? {
                    dimensions[position] = dimension;
                    boxes[position] = Some(prepared);
                }
            }
            let sizes = variable(dimensions, containing[axis]);
            for (position, prepared) in boxes.into_iter().enumerate() {
                if let Some(mut prepared) = prepared {
                    prepared.variable(axis, containing[axis], sizes[position], position);
                    let margin = prepared.margin;
                    let mut items = Vec::new();
                    let painted = prepared
                        .layout(measurer, context.destinations)
                        .and_then(|()| {
                            prepared.paint(
                                origin,
                                first_index + margin.location as usize,
                                &mut MarginOutput {
                                    page: page_box,
                                    perspective,
                                    lines: page,
                                    display: &mut items,
                                    links,
                                    page_text: page_text.as_mut(),
                                },
                                measurer,
                            )
                        });
                    if unless_failed(measurer, painted)?.is_some() {
                        stacked.push((margin, items));
                    }
                }
            }
        }
        for (location, origin, containing) in [
            (MarginLocation::TopLeftCorner, [0.0, 0.0], [left, top]),
            (
                MarginLocation::TopRightCorner,
                [width - right, 0.0],
                [right, top],
            ),
            (
                MarginLocation::BottomLeftCorner,
                [0.0, height - bottom],
                [left, bottom],
            ),
            (
                MarginLocation::BottomRightCorner,
                [width - right, height - bottom],
                [right, bottom],
            ),
        ] {
            if let Some((margin, decoration)) = margins
                .iter()
                .find(|(margin, _)| margin.location == location)
            {
                let mut items = Vec::new();
                let painted = prepare(
                    margin,
                    decoration,
                    containing,
                    number,
                    total,
                    context,
                    page_text.is_some(),
                )
                .and_then(|mut prepared| {
                    prepared.validate()?;
                    for axis in [HORIZONTAL, VERTICAL] {
                        prepared.fixed(axis, containing[axis]);
                    }
                    prepared.layout(measurer, context.destinations)?;
                    prepared.paint(
                        origin,
                        first_index + location as usize,
                        &mut MarginOutput {
                            page: page_box,
                            perspective,
                            lines: page,
                            display: &mut items,
                            links,
                            page_text: page_text.as_mut(),
                        },
                        measurer,
                    )
                });
                if unless_failed(measurer, painted)?.is_some() {
                    stacked.push((*margin, items));
                }
            }
        }
        // CSS Page 3 ยง3.1: margin boxes stack by `z-index`, then in their default painting
        // order, before the document's canvas when negative and over its content otherwise.
        stacked.sort_by_key(|(margin, _)| {
            (
                margin.style.z_index.unwrap_or_default(),
                margin.location.painting_order(),
            )
        });
        for (margin, items) in stacked {
            if margin.style.z_index.unwrap_or_default() < 0 {
                underlay.extend(items);
            } else {
                display.extend(items);
            }
        }
    }
    Ok(())
}