spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
use std::borrow::Cow;

use super::*;

/// Selects which positioned-descendant containing-block stacks a box establishes.
///
/// CSS Positioned Layout defines the absolute-position containing block for
/// positioned ancestors, while layout/paint containment and transforms also
/// establish the containing block for fixed-position descendants:
/// <https://www.w3.org/TR/css-position-3/#def-cb> and
/// <https://drafts.csswg.org/css-transforms-1/#transform-rendering>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::layout) enum PositionedContainingBlockMode {
    AbsoluteOnly,
    FixedAndAbsolute,
}

impl PositionedContainingBlockMode {
    /// Select containment positioning effects from the used principal-box
    /// containment, rather than directly from the authored shorthand.
    pub(in crate::layout) fn for_element(element: &Element, style: &ComputedStyle) -> Option<Self> {
        if used_property_containment(element, style).establishes_fixed_position_containing_block()
            || style.has_transform()
        {
            Some(Self::FixedAndAbsolute)
        } else if matches!(
            style.position,
            Position::Relative | Position::Absolute | Position::Fixed | Position::Sticky
        ) {
            Some(Self::AbsoluteOnly)
        } else {
            None
        }
    }

    pub(in crate::layout) fn for_style(style: &ComputedStyle) -> Option<Self> {
        if style.contain.layout || style.contain.paint || style.has_transform() {
            Some(Self::FixedAndAbsolute)
        } else if matches!(
            style.position,
            Position::Relative | Position::Absolute | Position::Fixed | Position::Sticky
        ) {
            Some(Self::AbsoluteOnly)
        } else {
            None
        }
    }

    fn establishes_fixed_containing_block(self) -> bool {
        matches!(self, Self::FixedAndAbsolute)
    }
}

/// Records the stack depths before a positioned-containing-block scope.
///
/// The token intentionally does not borrow the builder, so a caller can retain
/// it while replaying fragments in a separate temporary layout context.
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct PositionedContainingBlockScope {
    containing_blocks_depth: usize,
    fixed_containing_blocks_depth: usize,
    mode: PositionedContainingBlockMode,
    multicol_span_id: Option<u64>,
}

/// State restored after laying out one atomic inline formatting context on
/// its scratch page.
///
/// The coordinate-space identity and static-position containing block have
/// the same lifetime: descendants may retain either after deferred positioned
/// layout, but an enclosing atom must never observe them as its own state.
/// <https://drafts.csswg.org/css-position-3/#staticpos-rect>
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct AtomicInlineFormattingContextScope {
    coordinate_space: AtomicInlineCoordinateSpaceId,
    previous_axes: WritingModeAxes,
    static_position_context_depth: usize,
}

/// Durable source geometry for a positioned containing block encountered in
/// temporary multicolumn layout. Descendant replay resolves against this
/// continuous source containing block, then uses the shared source-to-
/// destination candidates retained by the multicolumn owner.
#[derive(Debug, Clone)]
pub(in crate::layout) struct MulticolPositionedContainingBlockSpan {
    pub(in crate::layout) id: u64,
    pub(in crate::layout) mode: PositionedContainingBlockMode,
    pub(in crate::layout) containing_block: ContainingBlock,
}

#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct PositionedChildStaticRect {
    left: f32,
    right: f32,
    top: f32,
    containing_block: Option<ContainingBlock>,
    static_alignment: Option<AbsposStaticAlignment>,
}

/// The source-coordinate overflow-clip ancestry of a positioned descendant
/// deferred out of temporary multicolumn layout.
///
/// An empty chain is meaningful: it records that the descendant was captured
/// with no ancestor overflow clips.  Replay must use this durable source
/// geometry rather than whichever clip scopes happen to be active after the
/// temporary column pages have been restored.
/// <https://drafts.csswg.org/css-overflow-3/#overflow-clip-edge>
#[derive(Debug, Clone)]
struct PositionedSourceOverflowClipChain(Vec<OverflowClip>);

impl PositionedSourceOverflowClipChain {
    fn capture(clips: &[OverflowClip]) -> Self {
        Self(clips.to_vec())
    }

    fn clips(&self) -> &[OverflowClip] {
        &self.0
    }
}

/// Durable source context required to replay one positioned descendant after
/// its multicolumn owner restores the enclosing layout state.
///
/// The containing-block owner and overflow-clip ancestry are one source
/// coordinate-system contract. Keeping them together prevents replay from
/// restoring a containing block while silently inheriting unrelated ambient
/// clips from the outer builder.
/// <https://www.w3.org/TR/css-position-3/#abspos-containing-block>
/// <https://drafts.csswg.org/css-overflow-3/#overflow-clipping>
#[derive(Debug, Clone)]
struct DeferredMulticolReplayContext {
    /// Stable owner captured from the normal-flow positioned containing block,
    /// rather than a temporary multicolumn page index.
    containing_block_span_id: Option<u64>,
    source_overflow_clips: PositionedSourceOverflowClipChain,
}

/// An out-of-flow flex descendant measured while a multicolumn container owns
/// temporary fragmentainer pages.
///
/// Column pages are implementation scratch space, so their positioned paint
/// layers cannot be committed directly.  The flex static-position rectangle is
/// nevertheless final geometry and is retained until the enclosing multicol
/// container restores its real containing-block context.
/// <https://www.w3.org/TR/css-flexbox-1/#abspos-items>
/// <https://www.w3.org/TR/css-multicol-1/#the-multi-column-model>
#[derive(Debug, Clone)]
pub(in crate::layout) struct DeferredMulticolPositionedChild {
    element: Element,
    signature: Box<ElementSignature>,
    style: ComputedStyle,
    replay_context: DeferredMulticolReplayContext,
    fragment: PositionedFragmentReplay,
}

impl DeferredMulticolPositionedChild {
    /// Direct children of the principal multicol box use that continuous
    /// containing block, not a fragmented in-flow ancestor span captured
    /// while anonymous columns were being measured.
    pub(in crate::layout) fn with_principal_multicol_ownership(mut self) -> Self {
        self.replay_context.containing_block_span_id = None;
        self
    }
}

/// Committed destination data for an out-of-flow child emitted by a fragmented
/// formatting context.
///
/// The static rectangle stays in source coordinates, while translation and
/// clip describe the owning fragmentainer. Keeping these in one record avoids
/// replaying a child against temporary multicolumn scratch-page coordinates.
/// Both direct positioned children and flex positioned children can use this
/// same payload when their containing context commits a fragment.
/// <https://www.w3.org/TR/css-position-3/#static-position>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
/// One source-to-destination projection available to a positioned child whose
/// final fragment owner depends on resolved inset geometry.
///
/// Normal-flow and already-owned positioned records have one committed
/// projection. An unresolved physical-row flex static position keeps all
/// candidate source slices until positioned layout establishes which paint
/// intersects each slice.
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct PositionedFragmentCandidate {
    source_clip: PaintClip,
    source_block_start: LayoutLength,
    source_block_end: LayoutLength,
    continuous_source_to_local: PaintTranslation,
    source_to_destination: PaintTranslation,
    destination_clip: PaintClip,
}

/// The geometry that selects one or more committed multicolumn projections
/// for a deferred positioned descendant.
///
/// A physical-row flex child selects its owner from the final resolved block
/// inset. A physical-column flex child instead has a static main-axis interval
/// in the fragmented source coordinate system, which can intersect several
/// source fragmentainers.
/// <https://www.w3.org/TR/css-flexbox-1/#abspos-items>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
#[derive(Debug, Clone, Copy, PartialEq)]
enum PositionedFragmentOwnerResolution {
    None,
    /// The positioned principal is resolved in one continuous source
    /// containing-block coordinate system, then its resulting paint is
    /// clipped and projected through every committed multicolumn source
    /// fragmentainer.
    ///
    /// Absolutely positioned descendants do not participate in normal flow,
    /// so no temporary column page can be their single owner.  Selecting one
    /// would either discard overflow that crosses a column boundary or move
    /// the entire principal into a later anonymous column.
    /// <https://www.w3.org/TR/css-position-3/#fragmenting-absolutely-positioned-elements>
    /// <https://www.w3.org/TR/css-break-3/#box-splitting>
    AllCommittedSourceFragments,
    FinalBlockInset(Option<LayoutLength>),
    SourceBlockInterval {
        start: LayoutLength,
        end: LayoutLength,
    },
}

/// Coordinate space of source layers produced while resolving an unresolved
/// positioned-fragment owner.
///
/// An automatic inset uses the flex static-position rectangle, which is
/// already local to its selected source fragmentainer. A definite physical
/// block inset resolves to a source-global coordinate that must be localized
/// before its layer is projected into a destination fragmentainer.
/// <https://www.w3.org/TR/css-position-3/#static-position>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PositionedCandidateSourceSpace {
    StaticPositionLocal,
    DefiniteBlockInsetGlobal,
}

impl PositionedFragmentCandidate {
    pub(in crate::layout) fn new(
        source_clip: PaintClip,
        source_block_start: LayoutLength,
        source_block_end: LayoutLength,
        continuous_source_to_local: PaintTranslation,
        source_to_destination: PaintTranslation,
        destination_clip: PaintClip,
    ) -> Self {
        Self {
            source_clip,
            source_block_start,
            source_block_end,
            continuous_source_to_local,
            source_to_destination,
            destination_clip,
        }
    }

    fn continuous_source_to_destination(self) -> PaintTranslation {
        PaintTranslation::new(
            self.continuous_source_to_local.x + self.source_to_destination.x,
            self.continuous_source_to_local.y + self.source_to_destination.y,
        )
    }

    /// Continue an equal-sized committed column projection from the preceding
    /// pair. The source-local clip intentionally remains the same temporary
    /// column canvas; only the continuous-source and destination mappings
    /// advance.
    fn continuation_after(self, previous: Self) -> Self {
        let source_to_destination_step = PaintTranslation::new(
            self.source_to_destination.x - previous.source_to_destination.x,
            self.source_to_destination.y - previous.source_to_destination.y,
        );
        let continuous_to_local_step = PaintTranslation::new(
            self.continuous_source_to_local.x - previous.continuous_source_to_local.x,
            self.continuous_source_to_local.y - previous.continuous_source_to_local.y,
        );
        let source_block_extent = self.source_block_end - self.source_block_start;
        Self {
            source_clip: self.source_clip,
            source_block_start: self.source_block_end,
            source_block_end: self.source_block_end + source_block_extent,
            continuous_source_to_local: PaintTranslation::new(
                self.continuous_source_to_local.x + continuous_to_local_step.x,
                self.continuous_source_to_local.y + continuous_to_local_step.y,
            ),
            source_to_destination: PaintTranslation::new(
                self.source_to_destination.x + source_to_destination_step.x,
                self.source_to_destination.y + source_to_destination_step.y,
            ),
            destination_clip: self.destination_clip.translated(source_to_destination_step),
        }
    }
}

#[derive(Debug, Clone)]
pub(in crate::layout) struct PositionedFragmentReplay {
    source_static_rect: PositionedChildStaticRect,
    positioning_containing_block: Option<(PositionedContainingBlockMode, ContainingBlock)>,
    source_fragment_block_offset: LayoutLength,
    owner_resolution: PositionedFragmentOwnerResolution,
    candidate_source_space: PositionedCandidateSourceSpace,
    unresolved_candidates: Vec<PositionedFragmentCandidate>,
}

impl PositionedFragmentReplay {
    pub(in crate::layout) fn unfragmented(
        source_static_rect: PositionedChildStaticRect,
        positioning_containing_block: Option<(PositionedContainingBlockMode, ContainingBlock)>,
    ) -> Self {
        Self {
            source_static_rect,
            positioning_containing_block,
            source_fragment_block_offset: layout_pt(0.0),
            owner_resolution: PositionedFragmentOwnerResolution::None,
            candidate_source_space: PositionedCandidateSourceSpace::StaticPositionLocal,
            unresolved_candidates: Vec::new(),
        }
    }

    /// Retain the temporary source fragmentainer that supplied the captured
    /// containing-block geometry. Candidate replay translates that geometry
    /// into each selected source fragmentainer before projecting paint back to
    /// its final multicolumn destination.
    /// <https://www.w3.org/TR/css-position-3/#def-cb>
    pub(in crate::layout) fn with_source_fragment_block_offset(
        mut self,
        source_fragment_block_offset: LayoutLength,
    ) -> Self {
        self.source_fragment_block_offset = source_fragment_block_offset;
        self
    }

    /// Mark a record whose physical-row fragment owner depends on a definite
    /// physical block inset resolved by positioned layout.
    ///
    /// Auto block insets retain their source static placement and do not need
    /// candidate replay; projecting them through every multicolumn slice
    /// would duplicate or displace a valid static-position descendant.
    /// <https://www.w3.org/TR/css-position-3/#inset-properties>
    pub(in crate::layout) fn resolving_owner_from_final_block_inset(
        mut self,
        final_block_inset_from_start: Option<LayoutLength>,
    ) -> Self {
        self.owner_resolution =
            PositionedFragmentOwnerResolution::FinalBlockInset(final_block_inset_from_start);
        self
    }

    /// Replay a positioned principal through every committed multicolumn
    /// source slice that intersects its resolved source paint.
    ///
    /// Candidate clips eliminate non-intersecting ink after positioned layout,
    /// so this intentionally retains all source slices rather than predicting
    /// ownership from the static-position rectangle.  The latter is only a
    /// fallback position and is not the fragmentation extent of an abspos box.
    /// <https://www.w3.org/TR/css-position-3/#static-position>
    /// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
    pub(in crate::layout) fn across_committed_multicolumn_fragments(mut self) -> Self {
        debug_assert!(matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::None
        ));
        self.owner_resolution = PositionedFragmentOwnerResolution::AllCommittedSourceFragments;
        self
    }

    /// Mark a record whose physical-column static main-axis interval intersects
    /// one or more committed source fragmentainers.
    ///
    /// The static interval is supplied in the flex source block coordinate
    /// system. It remains independent from the page-space static rectangle,
    /// which positioned layout uses for inset resolution.
    /// <https://www.w3.org/TR/css-flexbox-1/#abspos-items>
    /// <https://www.w3.org/TR/css-break-3/#box-splitting>
    pub(in crate::layout) fn resolving_owner_from_source_block_interval(
        mut self,
        start: LayoutLength,
        end: LayoutLength,
    ) -> Self {
        debug_assert!(end >= start, "a positioned source interval is monotonic");
        self.owner_resolution =
            PositionedFragmentOwnerResolution::SourceBlockInterval { start, end };
        self
    }

    /// The resolved positioned layer uses a source-global physical block
    /// coordinate rather than the selected fragmentainer's local static
    /// position.
    pub(in crate::layout) fn with_definite_block_inset_source_coordinates(mut self) -> Self {
        self.candidate_source_space = PositionedCandidateSourceSpace::DefiniteBlockInsetGlobal;
        self
    }

    /// Retain a candidate projection while final positioned geometry is still
    /// unresolved. Candidates are source-slice disjoint and retain the exact
    /// destination clip used by normal-flow multicolumn paint.
    pub(in crate::layout) fn add_unresolved_candidate(
        &mut self,
        candidate: PositionedFragmentCandidate,
    ) {
        if !matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::None
        ) && !self.unresolved_candidates.iter().any(|existing| {
            existing.source_clip == candidate.source_clip
                && existing.source_block_start == candidate.source_block_start
                && existing.source_block_end == candidate.source_block_end
                && existing.source_to_destination == candidate.source_to_destination
        }) {
            self.unresolved_candidates.push(candidate);
        }
    }

    pub(in crate::layout) fn has_unresolved_candidates(&self) -> bool {
        !matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::None
        ) && !self.unresolved_candidates.is_empty()
    }

    fn candidates_for_owner(&self) -> Vec<PositionedFragmentCandidate> {
        let selected = self
            .unresolved_candidates
            .iter()
            .copied()
            .filter(|candidate| match self.owner_resolution {
                PositionedFragmentOwnerResolution::None => false,
                PositionedFragmentOwnerResolution::AllCommittedSourceFragments => true,
                PositionedFragmentOwnerResolution::FinalBlockInset(Some(inset)) => {
                    inset >= candidate.source_block_start
                        && inset < candidate.source_block_end - layout_pt(0.01)
                }
                PositionedFragmentOwnerResolution::FinalBlockInset(None) => true,
                PositionedFragmentOwnerResolution::SourceBlockInterval { start, end } => {
                    start < candidate.source_block_end - layout_pt(0.01)
                        && candidate.source_block_start < end - layout_pt(0.01)
                }
            })
            .collect::<Vec<_>>();
        if selected.is_empty() {
            self.unresolved_candidates.clone()
        } else {
            selected
        }
    }

    fn localizes_static_rect_per_candidate(&self) -> bool {
        matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::SourceBlockInterval { .. }
        )
    }

    fn source_layers_select_candidates(&self) -> bool {
        !matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::AllCommittedSourceFragments
        )
    }

    /// Extend an equal-sized multicolumn projection sequence when the resolved
    /// principal has finite, axis-aligned source ink beyond the normal-flow
    /// columns that happened to be committed. Out-of-flow boxes do not create
    /// normal-flow columns, but CSS still fragments their overflowing paint.
    ///
    /// Effects with indeterminate bounds deliberately do not synthesize a
    /// destination continuation here; those retain every already-committed
    /// candidate in `append_multicol_positioned_candidate_layers`.
    /// <https://www.w3.org/TR/css-position-3/#fragmenting-absolutely-positioned-elements>
    fn candidates_extended_for_source_layers(
        &self,
        mut candidates: Vec<PositionedFragmentCandidate>,
        source_layers: &[PositionedPaintLayer],
        pages: &[Page],
        current_page: &Page,
    ) -> Vec<PositionedFragmentCandidate> {
        if !matches!(
            self.owner_resolution,
            PositionedFragmentOwnerResolution::AllCommittedSourceFragments
        ) || candidates.len() < 2
        {
            return candidates;
        }
        let second = candidates[1];
        let first = candidates[0];
        let continuous_block_step = second.source_block_end - second.source_block_start;
        if continuous_block_step <= layout_pt(0.01) {
            return candidates;
        }
        let block_uses_vertical_paint_axis =
            (second.continuous_source_to_local.y - first.continuous_source_to_local.y).abs()
                >= (second.continuous_source_to_local.x - first.continuous_source_to_local.x).abs();
        let local_fragmentainer_extent = if block_uses_vertical_paint_axis {
            first.source_clip.height()
        } else {
            first.source_clip.width()
        };
        if local_fragmentainer_extent <= 0.01 {
            return candidates;
        }
        let mut required_source_extent = 0.0f32;
        for layer in source_layers {
            let page = pages.get(layer.page_index).unwrap_or(current_page);
            let source_context = layer.context.clone().into_primitive_nodes(page);
            let Ok(Some(bounds)) = source_context.recorded_paint_bounds(page) else {
                continue;
            };
            let extent = if block_uses_vertical_paint_axis {
                bounds.height()
            } else {
                bounds.width()
            };
            required_source_extent = required_source_extent.max(extent);
        }
        let required_candidates = (required_source_extent / local_fragmentainer_extent)
            .ceil()
            .max(1.0) as usize;
        while candidates.len() < required_candidates {
            let previous = candidates[candidates.len() - 2];
            let current = candidates[candidates.len() - 1];
            candidates.push(current.continuation_after(previous));
        }
        candidates
    }

    fn source_containing_block(&self) -> Option<(PositionedContainingBlockMode, ContainingBlock)> {
        self.positioning_containing_block
    }

    fn containing_block_local_to_candidate(
        &self,
        candidate: PositionedFragmentCandidate,
    ) -> Option<(PositionedContainingBlockMode, ContainingBlock)> {
        self.positioning_containing_block
            .map(|(mode, containing_block)| {
                let source_translation = PaintTranslation::new(
                    0.0,
                    candidate.source_block_start.points()
                        - self.source_fragment_block_offset.points(),
                );
                (mode, containing_block.translated(source_translation))
            })
    }
}

impl PositionedChildStaticRect {
    pub(in crate::layout) fn new(left: f32, right: f32, top: f32) -> Self {
        Self {
            left,
            right,
            top,
            containing_block: None,
            static_alignment: None,
        }
    }

    pub(in crate::layout) fn with_containing_block(
        left: f32,
        right: f32,
        top: f32,
        containing_block: ContainingBlock,
    ) -> Self {
        Self {
            left,
            right,
            top,
            containing_block: Some(containing_block),
            static_alignment: None,
        }
    }

    pub(in crate::layout) fn with_static_alignment(
        mut self,
        static_alignment: AbsposStaticAlignment,
    ) -> Self {
        self.static_alignment = Some(static_alignment);
        self
    }

    /// Replace selected physical edges of the static-position rectangle while
    /// retaining its independently resolved containing block.
    ///
    /// Grid's automatic placement lines may lie on the opposite padding edge
    /// of an explicit line outside the explicit grid. The geometric area is
    /// normalized for the containing block, but the static corner must retain
    /// that explicit logical edge. CSS Grid §9.1 defines these separately.
    pub(in crate::layout) fn with_static_physical_edges(
        mut self,
        left: Option<f32>,
        right: Option<f32>,
        top: Option<f32>,
    ) -> Self {
        if let Some(left) = left {
            self.left = left;
        }
        if let Some(right) = right {
            self.right = right;
        }
        if let Some(top) = top {
            self.top = top;
        }
        self
    }

    fn layout_right(self) -> f32 {
        self.left + (self.right - self.left).max(1.0)
    }

    /// Translate the static-position fallback edges into one committed
    /// fragmentainer coordinate system.
    ///
    /// Alignment areas remain expressed against the positioned-layout
    /// containing block retained separately by the replay record. They must
    /// not be translated with this fallback geometry.
    /// <https://www.w3.org/TR/css-position-3/#static-position>
    pub(in crate::layout) fn translated(mut self, translation: PaintTranslation) -> Self {
        self.left += translation.x;
        self.right += translation.x;
        self.top += translation.y;
        self
    }
}

impl<'a> LayoutBuilder<'a> {
    pub(in crate::layout) fn begin_atomic_inline_formatting_context(
        &mut self,
        style: &ComputedStyle,
        content_rect: PageTopRect,
    ) -> AtomicInlineFormattingContextScope {
        let coordinate_space =
            AtomicInlineCoordinateSpaceId::new(self.next_atomic_inline_coordinate_space_id);
        self.next_atomic_inline_coordinate_space_id += 1;
        self.active_atomic_inline_coordinate_spaces
            .push(coordinate_space);

        let previous_axes = WritingModeAxes::new(
            self.containing_block_writing_mode,
            self.containing_block_direction,
        );
        let axes = WritingModeAxes::new(style.writing_mode, style.used_direction());
        self.containing_block_writing_mode = axes.writing_mode();
        self.containing_block_direction = axes.direction();
        let static_position_context_depth = self.static_position_containing_blocks.len();
        self.static_position_containing_blocks
            .push(StaticPositionContainingBlock::new(
                axes,
                content_rect,
                style.justify_items,
            ));

        AtomicInlineFormattingContextScope {
            coordinate_space,
            previous_axes,
            static_position_context_depth,
        }
    }

    pub(in crate::layout) fn end_atomic_inline_formatting_context(
        &mut self,
        scope: AtomicInlineFormattingContextScope,
    ) {
        debug_assert_eq!(
            self.static_position_containing_blocks.len(),
            scope.static_position_context_depth + 1,
        );
        self.static_position_containing_blocks.pop();
        debug_assert_eq!(
            self.active_atomic_inline_coordinate_spaces.last(),
            Some(&scope.coordinate_space),
        );
        self.active_atomic_inline_coordinate_spaces.pop();
        self.containing_block_writing_mode = scope.previous_axes.writing_mode();
        self.containing_block_direction = scope.previous_axes.direction();
    }

    pub(in crate::layout) fn current_positioned_coordinate_space(
        &self,
    ) -> PositionedCoordinateSpace {
        self.active_atomic_inline_coordinate_spaces
            .last()
            .copied()
            .map(PositionedCoordinateSpace::AtomicInline)
            .unwrap_or(PositionedCoordinateSpace::Page)
    }

    pub(in crate::layout) fn positioned_containing_block_context(
        &self,
        geometry: ContainingBlock,
    ) -> PositionedContainingBlockContext {
        PositionedContainingBlockContext::in_space(
            geometry,
            self.current_positioned_coordinate_space(),
        )
    }

    /// Lay out one deferred multicolumn positioned principal into a private
    /// layer vector, then restore the enclosing replay's layers unchanged.
    ///
    /// Nested multicolumn layout uses the builder's positioned-layer vector as
    /// temporary scratch state. Keeping an enclosing deferred replay in that
    /// vector lets an inner column set clear or consume the outer layers,
    /// which both loses paint and invalidates a later `split_off` index.
    /// <https://www.w3.org/TR/css-position-3/#fragmenting-absolutely-positioned-elements>
    fn capture_deferred_multicol_source_layers(
        &mut self,
        layout: impl FnOnce(&mut Self),
    ) -> Vec<PositionedPaintLayer> {
        let parent_positioned_layers = std::mem::take(&mut self.positioned_layers);
        layout(self);
        let source_layers =
            std::mem::replace(&mut self.positioned_layers, parent_positioned_layers);
        // Fixed layers have independent document-level ownership. In
        // particular, a viewport-fixed descendant discovered while resolving
        // this source principal must remain in that queue rather than being
        // folded into its fragmentainer-local continuation layers.
        source_layers
    }

    /// Capture the source state that a deferred positioned child must replay
    /// after temporary multicolumn layout restores the enclosing builder.
    fn deferred_multicol_replay_context(
        &self,
        position: &Position,
    ) -> DeferredMulticolReplayContext {
        DeferredMulticolReplayContext {
            containing_block_span_id: self
                .active_multicol_positioned_containing_block_span_id(position),
            source_overflow_clips: PositionedSourceOverflowClipChain::capture(&self.overflow_clips),
        }
    }

    pub(in crate::layout) fn deferred_multicol_positioned_child(
        &self,
        element: &Element,
        signature: &ElementSignature,
        style: ComputedStyle,
        fragment: PositionedFragmentReplay,
    ) -> DeferredMulticolPositionedChild {
        let replay_context = self.deferred_multicol_replay_context(&style.position);
        debug_assert!(replay_context.containing_block_span_id.is_none_or(|id| {
            self.multicol_positioned_containing_block_spans
                .iter()
                .any(|span| span.id == id)
        }));
        DeferredMulticolPositionedChild {
            element: element.clone(),
            signature: Box::new(signature.clone()),
            style,
            replay_context,
            fragment,
        }
    }

    /// Run deferred source layout under its captured containing-block and
    /// overflow-clip context, then restore the enclosing builder state.
    fn with_deferred_multicol_replay_context<T>(
        &mut self,
        context: &DeferredMulticolReplayContext,
        containing_block: Option<(PositionedContainingBlockMode, ContainingBlock)>,
        layout: impl FnOnce(&mut Self) -> T,
    ) -> T {
        let saved_overflow_clips = std::mem::replace(
            &mut self.overflow_clips,
            context.source_overflow_clips.clips().to_vec(),
        );
        let containing_block_scope = containing_block.map(|(mode, containing_block)| {
            self.push_positioned_containing_block(mode, containing_block)
        });
        let result = layout(self);
        if let Some(scope) = containing_block_scope {
            self.pop_positioned_containing_block(scope);
        }
        self.overflow_clips = saved_overflow_clips;
        result
    }

    fn replay_deferred_multicol_source_layers(
        &mut self,
        context: &DeferredMulticolReplayContext,
        containing_block: Option<(PositionedContainingBlockMode, ContainingBlock)>,
        child: &FormattingContextChild<'_>,
        stylesheets: &Stylesheets<'_>,
        static_rect: PositionedChildStaticRect,
    ) -> Vec<PositionedPaintLayer> {
        self.with_deferred_multicol_replay_context(context, containing_block, |layout| {
            layout.capture_deferred_multicol_source_layers(|layout| {
                layout.layout_positioned_formatting_context_child(child, stylesheets, static_rect);
            })
        })
    }

    /// Queue a positioned child with its already committed fragment payload.
    pub(in crate::layout) fn defer_multicol_positioned_fragment_child(
        &mut self,
        child: &FormattingContextChild<'_>,
        fragment: PositionedFragmentReplay,
    ) {
        let Some((element, signature, _)) = child.element_parts() else {
            return;
        };
        self.defer_multicol_positioned_fragment_element(
            element,
            signature,
            child.style.clone(),
            fragment,
        );
    }

    /// Queue a direct positioned descendant with a committed multicolumn
    /// fragment payload.
    ///
    /// Direct and flex-positioned children differ only in how they derive
    /// their source static rectangle. Once the containing multicolumn
    /// fragment is committed, both must use the same replay record so neither
    /// can accidentally bind to temporary column-page coordinates.
    /// <https://www.w3.org/TR/css-position-3/#static-position>
    /// <https://www.w3.org/TR/css-multicol-1/#the-multi-column-model>
    pub(in crate::layout) fn defer_multicol_positioned_fragment_element(
        &mut self,
        element: &Element,
        signature: &ElementSignature,
        style: ComputedStyle,
        fragment: PositionedFragmentReplay,
    ) {
        self.deferred_multicol_positioned_children
            .push(self.deferred_multicol_positioned_child(element, signature, style, fragment));
    }

    /// Remove scratch-column captures superseded by direct principal-box
    /// positioned records.
    ///
    /// Direct children are encountered once while the anonymous column flow
    /// is speculative and again when the multicol principal containing block
    /// is committed. The latter record is authoritative: retaining both
    /// duplicates the positioned principal, while retaining only the scratch
    /// record incorrectly clips it through anonymous columns. Descendants
    /// whose containing blocks live inside fragmented content have different
    /// element identities and remain queued.
    /// <https://drafts.csswg.org/css-multicol-2/#multi-column-model>
    /// <https://drafts.csswg.org/css-position-3/#fragmenting-absolutely-positioned-elements>
    pub(in crate::layout) fn remove_superseded_direct_multicol_positioned_captures(
        &mut self,
        start: usize,
        replacements: &[DeferredMulticolPositionedChild],
    ) {
        if start >= self.deferred_multicol_positioned_children.len() || replacements.is_empty() {
            return;
        }
        let replacement_ids = replacements
            .iter()
            .map(|child| child.element.id)
            .collect::<Vec<_>>();
        let mut captured = self.deferred_multicol_positioned_children.split_off(start);
        captured.retain(|child| !replacement_ids.contains(&child.element.id));
        self.deferred_multicol_positioned_children.extend(captured);
    }

    /// Capture a positioned principal whose containing block was established
    /// while multicolumn layout owns temporary fragmentainer pages.
    ///
    /// The frozen element/style identity and static rectangle survive the
    /// temporary layout transaction; replay later binds them to the committed
    /// projections of the actual containing-block scope instead of the page
    /// on which the speculative callback happened to run.
    pub(in crate::layout) fn capture_multicol_positioned_principal(
        &mut self,
        element: &Element,
        style: &ComputedStyle,
        source_static_rect: PositionedChildStaticRect,
    ) -> bool {
        if self.multicol_positioned_replay_capture_depth == 0 {
            return false;
        }
        let Some(containing_block_span_id) =
            self.active_multicol_positioned_containing_block_span_id(&style.position)
        else {
            // Only a positioned containing block that was itself captured by
            // the temporary multicolumn transaction may be replayed as an
            // independent positioned fragment.  In particular, opacity and
            // the other effect scopes establish stacking contexts without
            // establishing an absolute-position containing block.  Capturing
            // one of their descendants here would detach its paint from the
            // ancestor's compositing group.
            //
            // A genuine direct principal child is captured explicitly when
            // the multicol principal is committed.  All other descendants
            // remain in their captured ancestor paint/effect scope.
            // <https://drafts.csswg.org/css-multicol-2/#multi-column-model>
            // <https://drafts.csswg.org/css-position-3/#fragmenting-absolutely-positioned-elements>
            // <https://drafts.csswg.org/css-color-4/#transparency>
            return false;
        };
        if self
            .deferred_multicol_positioned_children
            .iter()
            .any(|child| {
                child.element.id == element.id
                    && child.replay_context.containing_block_span_id
                        == Some(containing_block_span_id)
            })
        {
            return true;
        }
        let signature = self
            .ancestors
            .last()
            .cloned()
            .unwrap_or_else(|| element_signature(element));
        self.deferred_multicol_positioned_children.push(
            self.deferred_multicol_positioned_child(
                element,
                &signature,
                style.clone(),
                PositionedFragmentReplay::unfragmented(source_static_rect, None)
                    .across_committed_multicolumn_fragments(),
            ),
        );
        true
    }

    fn active_multicol_positioned_containing_block_span_id(
        &self,
        position: &Position,
    ) -> Option<u64> {
        if matches!(position, Position::Fixed) {
            self.active_multicol_positioned_containing_block_spans
                .iter()
                .rev()
                .find(|&&id| {
                    self.multicol_positioned_containing_block_spans
                        .iter()
                        .any(|span| span.id == id && span.mode.establishes_fixed_containing_block())
                })
                .copied()
        } else {
            self.active_multicol_positioned_containing_block_spans
                .last()
                .copied()
        }
    }

    /// Retain one committed multicolumn source slice for positioned records
    /// whose final owner cannot be selected from their static rectangle alone.
    ///
    /// The deferred replay lays such a child out once in source coordinates,
    /// then projects each intersecting positioned layer through these exact
    /// normal-flow slice mappings.
    /// <https://www.w3.org/TR/css-position-3/#static-position>
    /// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
    pub(in crate::layout) fn retain_deferred_multicol_positioned_candidate(
        &mut self,
        start: usize,
        projection: FragmentainerProjection,
        source_block_start: LayoutLength,
        source_block_end: LayoutLength,
        continuous_source_to_local: PaintTranslation,
    ) {
        let candidate = PositionedFragmentCandidate::new(
            projection.source_clip(),
            source_block_start,
            source_block_end,
            continuous_source_to_local,
            projection.destination_translation(),
            projection.destination_clip(),
        );
        for child in self.deferred_multicol_positioned_children[start..].iter_mut() {
            child.fragment.add_unresolved_candidate(candidate);
        }
    }

    /// Commit every destination-local slice of one resolved positioned
    /// principal as one page-local stacking layer.
    ///
    /// Each layer retains its native effects and clip. A slice discriminator
    /// extends the page-local commit key, so normal speculative retries still
    /// coalesce while distinct source-to-destination continuations do not
    /// discard one another.
    /// <https://www.w3.org/TR/css-position-3/#fragmenting-absolutely-positioned-elements>
    /// <https://www.w3.org/TR/CSS22/zindex.html>
    fn append_multicol_positioned_candidate_layers(
        &mut self,
        fragment: &PositionedFragmentReplay,
        source_layers: Vec<PositionedPaintLayer>,
        monolithic_principal_paint: bool,
    ) {
        let candidates = fragment.candidates_extended_for_source_layers(
            fragment.candidates_for_owner(),
            &source_layers,
            &self.pages,
            &self.current_page,
        );
        for (candidate_index, candidate) in candidates.iter().enumerate() {
            // Size containment makes the principal box monolithic. It is
            // assigned to its originating fragmentainer as one paint unit;
            // later candidate slices describe descendant fragmentation, not
            // independent clipped pieces of the contained principal.
            // <https://drafts.csswg.org/css-contain-1/#containment-size>
            // <https://www.w3.org/TR/css-break-3/#monolithic>
            if monolithic_principal_paint && candidate_index != 0 {
                break;
            }
            for layer in &source_layers {
                // Source replay can still reference recorded page operations.
                // Materialize those operations while their source page is
                // authoritative, before translating and clipping each
                // continuation. This makes the projection's primitive
                // geometry, rather than an unrelated later page operation,
                // the source of truth for slice-edge coverage.
                // <https://www.w3.org/TR/css-break-3/#fragmentation-model>
                let source_page = self
                    .pages
                    .get(layer.page_index)
                    .unwrap_or(&self.current_page);
                let source_context = layer.context.clone().into_primitive_nodes(source_page);
                let candidate_local_source = source_context
                    .clone()
                    .translated(candidate.continuous_source_to_local);
                if fragment.source_layers_select_candidates() {
                    match candidate_local_source.recorded_paint_bounds(source_page) {
                        Ok(Some(bounds)) if bounds.intersect(candidate.source_clip).is_none() => {
                            continue;
                        }
                        Ok(None) => continue,
                        // Transforms, masks, and other non-rectangular effects
                        // have indeterminate source bounds. Keep every candidate
                        // rather than risking discarded paint.
                        Ok(Some(_)) | Err(()) => {}
                    }
                }
                let mut projected_layer = layer.clone();
                projected_layer.context =
                    source_context.translated(candidate.continuous_source_to_destination());
                projected_layer.page_index = self.pages.len();
                let effective_clip = (!monolithic_principal_paint).then(|| {
                    projected_layer
                        .context
                        .effects
                        .overflow_clip_bounds()
                        .and_then(|existing| existing.intersect(candidate.destination_clip))
                        .unwrap_or(candidate.destination_clip)
                });
                // Materialized rectangular ink is cut to the projected
                // fragmentainer before serialization. The rectangular effect
                // below still clips complex and deferred ink, but trimming
                // simple primitives avoids a PDF clip-edge seam and gives a
                // continuation the same coverage as an independently painted
                // fragment.
                // <https://www.w3.org/TR/css-break-3/#fragmentation-model>
                if let Some(effective_clip) = effective_clip {
                    projected_layer.context = projected_layer
                        .context
                        .sliced_primitives_to_fragmentainer_rect(effective_clip);
                }
                // The source layer was captured while a scratch column page
                // was active. Its original paint cursor is consequently
                // older than normal-flow descendants committed into later
                // real columns. Reassign it at final replay so auto-level
                // positioned paint remains in the positioned phase of CSS
                // painting order, while stack-level ordering is unchanged.
                // <https://www.w3.org/TR/CSS22/zindex.html>
                projected_layer.context.source_order = self.next_paint_source_order();
                // Rectangles that were reduced above already have the exact
                // continuation edge. Do not wrap that narrow, fully
                // materialized case in a redundant PDF clip, because PDF clip
                // coverage differs from an independently painted rectangle.
                // Retain the clip for all existing effects, deferred
                // operations, paths, images, and nested contexts.
                if let Some(effective_clip) = effective_clip {
                    if projected_layer
                        .context
                        .can_elide_overflow_clip_after_materialization(effective_clip)
                    {
                        projected_layer.context.effects.overflow_clip_effect = None;
                    } else {
                        projected_layer
                            .context
                            .effects
                            .intersect_overflow_clip_bounds(effective_clip);
                    }
                }
                self.positioned_layers
                    .push(projected_layer.with_multicol_fragment_index(candidate_index));
            }
        }
    }

    /// Replay queued positioned descendants after the outermost multicolumn
    /// container has restored its principal containing-block context.
    ///
    /// A nested column set leaves its records queued for the outermost owner;
    /// replaying against an intermediate scratch page would reintroduce the
    /// coordinate and clipping error this queue avoids.
    pub(in crate::layout) fn replay_deferred_multicol_positioned_children(&mut self, start: usize) {
        if self.multicol_positioned_replay_capture_depth != 0
            || start >= self.deferred_multicol_positioned_children.len()
        {
            return;
        }
        debug_assert!(
            self.deferred_multicol_positioned_children[start..]
                .iter()
                .all(|child| {
                    child
                        .replay_context
                        .containing_block_span_id
                        .is_none_or(|id| {
                            self.multicol_positioned_containing_block_spans
                                .iter()
                                .any(|span| span.id == id)
                        })
                }),
            "deferred multicol positioned descendants must reference a durable containing-block span"
        );
        let deferred = self.deferred_multicol_positioned_children.split_off(start);
        for child in deferred {
            let monolithic_principal_paint =
                used_property_containment(&child.element, &child.style).size;
            let containing_block_span =
                child
                    .replay_context
                    .containing_block_span_id
                    .and_then(|id| {
                        self.multicol_positioned_containing_block_spans
                            .iter()
                            .find(|span| span.id == id)
                            .cloned()
                    });
            let static_rect = child.fragment.source_static_rect;
            let stylesheets = self.stylesheets;
            let child_boxes = self.build_frozen_child_boxes_with_current_ancestors(
                &child.element,
                &stylesheets,
                &child.style,
            );
            let replay_child = FormattingContextChild {
                kind: FormattingContextChildKind::Element {
                    element: &child.element,
                    signature: child.signature,
                    generated_pseudo: None,
                    children: Some(Cow::Owned(child_boxes)),
                    table_fragment: None,
                },
                style: child.style,
            };
            if let Some(span) = containing_block_span {
                // A positioned principal is laid out in the continuous source
                // containing block.  Its paint, rather than its fallback
                // static rectangle or the temporary column that happened to
                // discover it, determines which committed column slices own
                // the result.
                // <https://www.w3.org/TR/css-position-3/#fragmenting-absolutely-positioned-elements>
                let stylesheets = self.stylesheets;
                let source_layers = self.replay_deferred_multicol_source_layers(
                    &child.replay_context,
                    Some((span.mode, span.containing_block)),
                    &replay_child,
                    &stylesheets,
                    static_rect,
                );
                self.append_multicol_positioned_candidate_layers(
                    &child.fragment,
                    source_layers,
                    monolithic_principal_paint,
                );
                continue;
            }
            if child.fragment.localizes_static_rect_per_candidate() {
                for candidate in child.fragment.candidates_for_owner() {
                    let global_block_inset = matches!(
                        child.fragment.candidate_source_space,
                        PositionedCandidateSourceSpace::DefiniteBlockInsetGlobal
                    );
                    let containing_block = if global_block_inset {
                        child.fragment.source_containing_block()
                    } else {
                        child
                            .fragment
                            .containing_block_local_to_candidate(candidate)
                    };
                    let candidate_static_rect = if global_block_inset {
                        static_rect
                    } else {
                        static_rect.translated(PaintTranslation::new(
                            0.0,
                            candidate.source_block_start.points(),
                        ))
                    };
                    let stylesheets = self.stylesheets;
                    let source_layers = self.replay_deferred_multicol_source_layers(
                        &child.replay_context,
                        containing_block,
                        &replay_child,
                        &stylesheets,
                        candidate_static_rect,
                    );
                    for mut layer in source_layers {
                        // Positioned layout produces source-coordinate ink.
                        // A global definite inset retains its original source
                        // block offset until this point, so its source slice
                        // must advance to the committed destination block
                        // start. Translating the static rectangle instead
                        // would move a later fragment's `top` inset below its
                        // own clip.
                        // <https://www.w3.org/TR/css-break-3/#fragmentation-model>
                        // <https://www.w3.org/TR/css-position-3/#static-position>
                        layer = layer.translated(candidate.source_to_destination);
                        layer.page_index = self.pages.len();
                        let destination_clip = layer
                            .context
                            .effects
                            .overflow_clip_bounds()
                            .and_then(|existing| existing.intersect(candidate.destination_clip))
                            .unwrap_or(candidate.destination_clip);
                        layer
                            .context
                            .effects
                            .intersect_overflow_clip_bounds(destination_clip);
                        self.positioned_layers.push(layer);
                    }
                }
                continue;
            }
            let stylesheets = self.stylesheets;
            let source_layers = self.replay_deferred_multicol_source_layers(
                &child.replay_context,
                child.fragment.positioning_containing_block,
                &replay_child,
                &stylesheets,
                static_rect,
            );
            if child.fragment.has_unresolved_candidates() {
                self.append_multicol_positioned_candidate_layers(
                    &child.fragment,
                    source_layers,
                    monolithic_principal_paint,
                );
            } else {
                self.positioned_layers.extend(source_layers);
            }
        }
    }

    /// Push the positioned-containing-block stacks established by one box.
    ///
    /// Callers retain ownership of the box geometry; this only centralizes the
    /// paired stack lifecycle required by CSS Positioned Layout and Transforms.
    /// <https://www.w3.org/TR/css-position-3/#def-cb> and
    /// <https://drafts.csswg.org/css-transforms-1/#transform-rendering>.
    pub(in crate::layout) fn push_positioned_containing_block(
        &mut self,
        mode: PositionedContainingBlockMode,
        containing_block: ContainingBlock,
    ) -> PositionedContainingBlockScope {
        let containing_block = self.positioned_containing_block_context(containing_block);
        let multicol_span_id = (self.multicol_positioned_replay_capture_depth > 0).then(|| {
            let id = self.next_multicol_positioned_containing_block_span_id;
            self.next_multicol_positioned_containing_block_span_id += 1;
            self.multicol_positioned_containing_block_spans.push(
                MulticolPositionedContainingBlockSpan {
                    id,
                    mode,
                    containing_block: containing_block.geometry,
                },
            );
            id
        });
        let scope = PositionedContainingBlockScope {
            containing_blocks_depth: self.containing_blocks.len(),
            fixed_containing_blocks_depth: self.fixed_containing_blocks.len(),
            mode,
            multicol_span_id,
        };
        if let Some(id) = multicol_span_id {
            self.active_multicol_positioned_containing_block_spans
                .push(id);
        }
        self.containing_blocks.push(containing_block);
        if mode.establishes_fixed_containing_block() {
            self.fixed_containing_blocks.push(containing_block);
        }
        scope
    }

    /// Restore the positioned-containing-block stacks recorded by `scope`.
    ///
    /// The depth assertions make leaked nested scopes visible at their owning
    /// layout boundary rather than silently popping an ancestor's geometry.
    pub(in crate::layout) fn pop_positioned_containing_block(
        &mut self,
        scope: PositionedContainingBlockScope,
    ) {
        debug_assert_eq!(
            self.containing_blocks.len(),
            scope.containing_blocks_depth + 1,
            "positioned containing-block scopes must be popped in nesting order",
        );
        debug_assert_eq!(
            self.fixed_containing_blocks.len(),
            scope.fixed_containing_blocks_depth
                + usize::from(scope.mode.establishes_fixed_containing_block()),
            "fixed containing-block scopes must be popped in nesting order",
        );
        if scope.mode.establishes_fixed_containing_block() {
            self.fixed_containing_blocks.pop();
        }
        self.containing_blocks.pop();
        if let Some(id) = scope.multicol_span_id {
            debug_assert_eq!(
                self.active_multicol_positioned_containing_block_spans.pop(),
                Some(id),
                "multicol positioned-containing-block spans must be popped in nesting order",
            );
        }
        debug_assert_eq!(self.containing_blocks.len(), scope.containing_blocks_depth,);
        debug_assert_eq!(
            self.fixed_containing_blocks.len(),
            scope.fixed_containing_blocks_depth,
        );
    }

    /// Replace the geometry of the innermost positioned containing block
    /// after its auto-sized principal flow has committed.
    ///
    /// Atomic formatting contexts initially need a containing block while
    /// collecting descendants, but their final padding-box height is only
    /// known after principal-flow layout.  Updating both stacks together
    /// keeps fixed descendants viewport-owned for `AbsoluteOnly` scopes.
    /// <https://www.w3.org/TR/css-position-3/#def-cb>
    pub(in crate::layout) fn finalize_positioned_containing_block(
        &mut self,
        scope: PositionedContainingBlockScope,
        containing_block: ContainingBlock,
    ) {
        debug_assert_eq!(
            self.containing_blocks.len(),
            scope.containing_blocks_depth + 1,
            "positioned containing-block scopes must be finalized in nesting order",
        );
        self.containing_blocks
            .last_mut()
            .expect("positioned containing-block scope has one absolute stack entry")
            .geometry = containing_block;
        if scope.mode.establishes_fixed_containing_block() {
            debug_assert_eq!(
                self.fixed_containing_blocks.len(),
                scope.fixed_containing_blocks_depth + 1,
                "fixed containing-block scope must match absolute scope",
            );
            self.fixed_containing_blocks
                .last_mut()
                .expect("fixed containing-block scope has one fixed stack entry")
                .geometry = containing_block;
        }
        if let Some(id) = scope.multicol_span_id
            && let Some(span) = self
                .multicol_positioned_containing_block_spans
                .iter_mut()
                .find(|span| span.id == id)
        {
            span.containing_block = containing_block;
        }
    }

    /// Replay an absolutely positioned flex/grid child from a precomputed
    /// static-position rectangle.
    ///
    /// CSS Flexbox and CSS Grid compute different hypothetical positions for
    /// out-of-flow children, but both replay the same child under that temporary
    /// static-position geometry:
    /// <https://www.w3.org/TR/css-flexbox-1/#abspos-items> and
    /// <https://www.w3.org/TR/css-grid-1/#abspos-items>.
    pub(in crate::layout) fn layout_positioned_formatting_context_child(
        &mut self,
        child: &FormattingContextChild<'_>,
        stylesheets: &Stylesheets<'_>,
        static_rect: PositionedChildStaticRect,
    ) {
        let previous_left = self.content_left;
        let previous_right = self.content_right;
        let previous_cursor_y = self.cursor_y;
        let previous_absolute_static_position = self.absolute_static_position;
        let pushed_containing_block = if let Some(containing_block) = static_rect.containing_block {
            let context = self.positioned_containing_block_context(containing_block);
            self.containing_blocks.push(context);
            true
        } else {
            false
        };

        self.content_left = static_rect.left;
        self.content_right = static_rect.layout_right();
        self.cursor_y = static_rect.top;
        let static_position = AbsoluteStaticPosition::from_page_rect_with_horizontal_outside(
            static_rect.left,
            static_rect.right,
            static_rect.top,
            true,
        );
        self.absolute_static_position = Some(match static_rect.static_alignment {
            Some(static_alignment) => static_position.with_static_alignment(static_alignment),
            None => static_position,
        });

        let mut positioned_style = child.style.clone();
        if positioned_style.display.is_inline_level() {
            positioned_style.display = positioned_style.display.blockified();
        }

        if let Some((child_element, signature, child_boxes)) = child.element_parts() {
            self.push_ancestor_signature(signature.clone());
            self.layout_element_with_child_boxes(
                child_element,
                &positioned_style,
                stylesheets,
                child_boxes,
            );
            self.ancestors.pop();
        }

        if pushed_containing_block {
            self.containing_blocks.pop();
        }
        self.absolute_static_position = previous_absolute_static_position;
        self.content_left = previous_left;
        self.content_right = previous_right;
        self.cursor_y = previous_cursor_y;
    }
}

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

    fn test_layout_builder<'a, Collection: crate::css::StylesheetCollection + ?Sized>(
        options: &'a RenderOptions,
        stylesheets: &'a Collection,
        resource_cache: &'a ResourceCache,
    ) -> LayoutBuilder<'a> {
        let stylesheets = crate::css::StylesheetCollection::stylesheet_view(stylesheets);
        LayoutBuilder::new(LayoutBuilderConfig {
            options,
            stylesheets,
            base_url: None,
            root_url: None,
            resource_cache,
            iframe_documents: Box::leak(Box::new(HashMap::new())),
            iframe_viewport: None,
            page_progression_direction: Direction::Ltr,
            page_counter_initial_values: HashMap::new(),
            target_references: crate::layout::TargetReferenceSnapshot::default(),
            font_system: FontSystem::new(),
        })
    }

    #[test]
    fn positioned_containing_block_mode_follows_positioning_and_effects() {
        let mut style = ComputedStyle::initial();
        assert_eq!(PositionedContainingBlockMode::for_style(&style), None);

        style.position = Position::Relative;
        assert_eq!(
            PositionedContainingBlockMode::for_style(&style),
            Some(PositionedContainingBlockMode::AbsoluteOnly),
        );

        style.position = Position::Absolute;
        assert_eq!(
            PositionedContainingBlockMode::for_style(&style),
            Some(PositionedContainingBlockMode::AbsoluteOnly),
        );

        style.position = Position::Fixed;
        assert_eq!(
            PositionedContainingBlockMode::for_style(&style),
            Some(PositionedContainingBlockMode::AbsoluteOnly),
        );

        style.position = Position::Static;
        style.contain.layout = true;
        assert_eq!(
            PositionedContainingBlockMode::for_style(&style),
            Some(PositionedContainingBlockMode::FixedAndAbsolute),
        );
    }

    #[test]
    fn positioned_containing_block_scope_restores_both_stack_variants() {
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let mut builder = test_layout_builder(&options, &stylesheets, &resource_cache);
        let containing_block =
            ContainingBlock::from_page_top_rect(PageTopRect::new(10.0, 20.0, 30.0, 40.0));

        let initial_containing_blocks = builder.containing_blocks.len();
        let initial_fixed_containing_blocks = builder.fixed_containing_blocks.len();
        let absolute_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::AbsoluteOnly,
            containing_block,
        );
        assert_eq!(
            builder.containing_blocks.len(),
            initial_containing_blocks + 1
        );
        assert_eq!(
            builder.fixed_containing_blocks.len(),
            initial_fixed_containing_blocks
        );
        builder.pop_positioned_containing_block(absolute_scope);
        assert_eq!(builder.containing_blocks.len(), initial_containing_blocks);
        assert_eq!(
            builder.fixed_containing_blocks.len(),
            initial_fixed_containing_blocks
        );

        let fixed_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::FixedAndAbsolute,
            containing_block,
        );
        assert_eq!(
            builder.containing_blocks.len(),
            initial_containing_blocks + 1
        );
        assert_eq!(
            builder.fixed_containing_blocks.len(),
            initial_fixed_containing_blocks + 1
        );
        builder.pop_positioned_containing_block(fixed_scope);
        assert_eq!(builder.containing_blocks.len(), initial_containing_blocks);
        assert_eq!(
            builder.fixed_containing_blocks.len(),
            initial_fixed_containing_blocks
        );
    }

    #[test]
    fn positioned_containing_blocks_inherit_atomic_coordinate_space_provenance() {
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let mut builder = test_layout_builder(&options, &stylesheets, &resource_cache);
        let outer_geometry =
            ContainingBlock::from_page_top_rect(PageTopRect::new(10.0, 20.0, 30.0, 40.0));
        let nested_geometry =
            ContainingBlock::from_page_top_rect(PageTopRect::new(50.0, 60.0, 70.0, 80.0));

        let page_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::AbsoluteOnly,
            outer_geometry,
        );
        assert_eq!(
            builder.containing_blocks.last().unwrap().coordinate_space,
            PositionedCoordinateSpace::Page
        );
        builder.pop_positioned_containing_block(page_scope);

        let outer_space = AtomicInlineCoordinateSpaceId::new(41);
        builder
            .active_atomic_inline_coordinate_spaces
            .push(outer_space);
        let outer_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::FixedAndAbsolute,
            outer_geometry,
        );
        let nested_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::AbsoluteOnly,
            nested_geometry,
        );
        assert_eq!(
            builder.containing_blocks.last().unwrap().coordinate_space,
            PositionedCoordinateSpace::AtomicInline(outer_space)
        );
        assert_eq!(
            builder
                .fixed_containing_blocks
                .last()
                .unwrap()
                .coordinate_space,
            PositionedCoordinateSpace::AtomicInline(outer_space)
        );

        builder.pop_positioned_containing_block(nested_scope);
        builder.pop_positioned_containing_block(outer_scope);
        builder.active_atomic_inline_coordinate_spaces.pop();

        let inner_space = AtomicInlineCoordinateSpaceId::new(42);
        builder
            .active_atomic_inline_coordinate_spaces
            .extend([outer_space, inner_space]);
        let inner_scope = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::AbsoluteOnly,
            nested_geometry,
        );
        assert_eq!(
            builder.containing_blocks.last().unwrap().coordinate_space,
            PositionedCoordinateSpace::AtomicInline(inner_space)
        );
        builder.pop_positioned_containing_block(inner_scope);
    }

    #[test]
    fn atomic_inline_formatting_context_scope_nests_and_restores_state() {
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let mut builder = test_layout_builder(&options, &stylesheets, &resource_cache);
        let initial_axes = WritingModeAxes::new(
            builder.containing_block_writing_mode,
            builder.containing_block_direction,
        );
        let initial_static_depth = builder.static_position_containing_blocks.len();
        let initial_atomic_depth = builder.active_atomic_inline_coordinate_spaces.len();

        let mut outer_style = ComputedStyle::initial();
        outer_style.writing_mode = WritingMode::SidewaysRl;
        outer_style.direction = Direction::Rtl;
        let outer_rect = PageTopRect::new(7.0, 91.0, 31.0, 47.0);
        let outer = builder.begin_atomic_inline_formatting_context(&outer_style, outer_rect);
        assert_eq!(
            builder.active_atomic_inline_coordinate_spaces.last(),
            Some(&outer.coordinate_space)
        );
        let outer_context = builder.static_position_containing_blocks.last().unwrap();
        assert_eq!(outer_context.content_rect, outer_rect);
        assert_eq!(
            outer_context.axes,
            WritingModeAxes::new(WritingMode::SidewaysRl, Direction::Rtl)
        );

        let mut inner_style = ComputedStyle::initial();
        inner_style.writing_mode = WritingMode::SidewaysLr;
        let inner = builder.begin_atomic_inline_formatting_context(
            &inner_style,
            PageTopRect::new(13.0, 73.0, 19.0, 29.0),
        );
        assert_ne!(inner.coordinate_space, outer.coordinate_space);
        builder.end_atomic_inline_formatting_context(inner);
        assert_eq!(
            WritingModeAxes::new(
                builder.containing_block_writing_mode,
                builder.containing_block_direction,
            ),
            WritingModeAxes::new(WritingMode::SidewaysRl, Direction::Rtl)
        );
        assert_eq!(
            builder.active_atomic_inline_coordinate_spaces.last(),
            Some(&outer.coordinate_space)
        );

        builder.end_atomic_inline_formatting_context(outer);
        assert_eq!(
            builder.static_position_containing_blocks.len(),
            initial_static_depth
        );
        assert_eq!(
            builder.active_atomic_inline_coordinate_spaces.len(),
            initial_atomic_depth
        );
        assert_eq!(
            WritingModeAxes::new(
                builder.containing_block_writing_mode,
                builder.containing_block_direction,
            ),
            initial_axes
        );

        let snapshot = builder.snapshot();
        let first_retry = builder.begin_atomic_inline_formatting_context(&outer_style, outer_rect);
        let first_retry_id = first_retry.coordinate_space;
        builder.end_atomic_inline_formatting_context(first_retry);
        builder.restore(snapshot);
        let second_retry = builder.begin_atomic_inline_formatting_context(&outer_style, outer_rect);
        assert_ne!(second_retry.coordinate_space, first_retry_id);
        builder.end_atomic_inline_formatting_context(second_retry);
    }

    #[test]
    fn deferred_multicol_replay_context_restores_captured_clip_chain_and_parent_state() {
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let mut builder = test_layout_builder(&options, &stylesheets, &resource_cache);
        let source_clip = OverflowClip::from_paint_rect_with_axes_and_non_scrollable(
            paint_space_rect(1.0, 2.0, 30.0, 40.0),
            true,
            true,
            false,
            true,
        );
        let enclosing_clip = OverflowClip::from_paint_rect(paint_space_rect(5.0, 6.0, 7.0, 8.0));
        let containing_block =
            ContainingBlock::from_page_top_rect(PageTopRect::new(10.0, 20.0, 30.0, 40.0));

        builder.overflow_clips = vec![source_clip];
        let clipped_context = builder.deferred_multicol_replay_context(&Position::Absolute);
        builder.overflow_clips = vec![enclosing_clip];
        let initial_containing_block_depth = builder.containing_blocks.len();
        let observed = builder.with_deferred_multicol_replay_context(
            &clipped_context,
            Some((
                PositionedContainingBlockMode::AbsoluteOnly,
                containing_block,
            )),
            |layout| {
                (
                    layout.overflow_clips.clone(),
                    layout.containing_blocks.len(),
                )
            },
        );
        assert_eq!(observed.0, vec![source_clip]);
        assert_eq!(observed.1, initial_containing_block_depth + 1);
        assert_eq!(builder.overflow_clips, vec![enclosing_clip]);
        assert_eq!(
            builder.containing_blocks.len(),
            initial_containing_block_depth
        );

        builder.overflow_clips.clear();
        let empty_context = builder.deferred_multicol_replay_context(&Position::Absolute);
        builder.overflow_clips = vec![enclosing_clip];
        let observed_empty =
            builder.with_deferred_multicol_replay_context(&empty_context, None, |layout| {
                layout.overflow_clips.clone()
            });
        assert!(observed_empty.is_empty());
        assert_eq!(builder.overflow_clips, vec![enclosing_clip]);
    }

    #[test]
    fn multicol_containing_block_spans_survive_snapshot_restore_without_paint() {
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let mut builder = test_layout_builder(&options, &stylesheets, &resource_cache);
        builder.multicol_positioned_replay_capture_depth = 1;
        let snapshot = builder.snapshot();
        let containing_block =
            ContainingBlock::from_page_top_rect(PageTopRect::new(10.0, 20.0, 30.0, 40.0));
        let initial_positioned_layers = builder.positioned_layers.len();
        let outer = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::AbsoluteOnly,
            containing_block,
        );
        let inner = builder.push_positioned_containing_block(
            PositionedContainingBlockMode::FixedAndAbsolute,
            containing_block,
        );
        assert_ne!(outer.multicol_span_id, inner.multicol_span_id);
        assert_eq!(
            builder.active_multicol_positioned_containing_block_spans,
            vec![
                outer.multicol_span_id.unwrap(),
                inner.multicol_span_id.unwrap()
            ],
        );
        builder.pop_positioned_containing_block(inner);
        builder.pop_positioned_containing_block(outer);

        builder.restore(snapshot);
        assert_eq!(builder.multicol_positioned_containing_block_spans.len(), 2);
        assert!(
            builder
                .active_multicol_positioned_containing_block_spans
                .is_empty()
        );
        assert_eq!(builder.positioned_layers.len(), initial_positioned_layers);
    }

    #[test]
    fn unresolved_positioned_replay_retains_distinct_multicol_candidates() {
        let mut replay = PositionedFragmentReplay::unfragmented(
            PositionedChildStaticRect::new(1.0, 4.0, 3.0),
            None,
        )
        .resolving_owner_from_final_block_inset(Some(layout_pt(55.0)));
        replay.add_unresolved_candidate(PositionedFragmentCandidate::new(
            PaintClip::new(0.0, 100.0, 20.0, 40.0),
            layout_pt(0.0),
            layout_pt(40.0),
            PaintTranslation::identity(),
            PaintTranslation::new(30.0, -20.0),
            PaintClip::new(30.0, 80.0, 20.0, 40.0),
        ));
        replay.add_unresolved_candidate(PositionedFragmentCandidate::new(
            PaintClip::new(0.0, 100.0, 20.0, 40.0),
            layout_pt(0.0),
            layout_pt(40.0),
            PaintTranslation::identity(),
            PaintTranslation::new(30.0, -20.0),
            PaintClip::new(30.0, 80.0, 20.0, 40.0),
        ));
        replay.add_unresolved_candidate(PositionedFragmentCandidate::new(
            PaintClip::new(0.0, 140.0, 20.0, 40.0),
            layout_pt(40.0),
            layout_pt(80.0),
            PaintTranslation::new(0.0, 40.0),
            PaintTranslation::new(60.0, -40.0),
            PaintClip::new(60.0, 100.0, 20.0, 40.0),
        ));

        assert!(replay.has_unresolved_candidates());
        assert_eq!(replay.unresolved_candidates.len(), 2);
        assert_eq!(
            replay.unresolved_candidates[0].destination_clip,
            PaintClip::new(30.0, 80.0, 20.0, 40.0),
        );
        assert_eq!(
            replay.candidates_for_owner(),
            vec![replay.unresolved_candidates[1]],
            "a resolved top inset selects its owning source fragmentainer interval",
        );
    }

    #[test]
    fn source_static_interval_selects_each_intersected_multicol_candidate() {
        let mut replay = PositionedFragmentReplay::unfragmented(
            PositionedChildStaticRect::new(1.0, 4.0, 3.0),
            None,
        )
        .resolving_owner_from_source_block_interval(layout_pt(35.0), layout_pt(85.0));
        for (start, end) in [(0.0, 40.0), (40.0, 80.0), (80.0, 120.0)] {
            replay.add_unresolved_candidate(PositionedFragmentCandidate::new(
                PaintClip::new(start, 100.0, 20.0, 40.0),
                layout_pt(start),
                layout_pt(end),
                PaintTranslation::identity(),
                PaintTranslation::identity(),
                PaintClip::new(start, 100.0, 20.0, 40.0),
            ));
        }

        assert_eq!(
            replay.candidates_for_owner(),
            replay.unresolved_candidates[..3],
            "a column-axis flex static rectangle retains every intersected source fragment",
        );
    }

    #[test]
    fn positioned_principal_replays_through_every_committed_multicol_candidate() {
        let mut replay = PositionedFragmentReplay::unfragmented(
            PositionedChildStaticRect::new(1.0, 4.0, 3.0),
            None,
        )
        .across_committed_multicolumn_fragments();
        for (start, end) in [(0.0, 40.0), (40.0, 80.0)] {
            replay.add_unresolved_candidate(PositionedFragmentCandidate::new(
                PaintClip::new(0.0, start, 20.0, end - start),
                layout_pt(start),
                layout_pt(end),
                PaintTranslation::new(0.0, start),
                PaintTranslation::identity(),
                PaintClip::new(0.0, start, 20.0, end - start),
            ));
        }

        assert_eq!(replay.candidates_for_owner(), replay.unresolved_candidates);
    }

    #[test]
    fn definite_block_inset_marks_candidate_layers_as_source_global() {
        let local = PositionedFragmentReplay::unfragmented(
            PositionedChildStaticRect::new(1.0, 4.0, 3.0),
            None,
        )
        .resolving_owner_from_source_block_interval(layout_pt(0.0), layout_pt(40.0));
        let global = local.clone().with_definite_block_inset_source_coordinates();
        assert_eq!(
            local.candidate_source_space,
            PositionedCandidateSourceSpace::StaticPositionLocal
        );
        assert_eq!(
            global.candidate_source_space,
            PositionedCandidateSourceSpace::DefiniteBlockInsetGlobal
        );
    }

    #[test]
    fn continuous_source_projection_localizes_before_destination_translation() {
        let candidate = PositionedFragmentCandidate::new(
            PaintClip::new(0.0, 100.0, 20.0, 40.0),
            layout_pt(40.0),
            layout_pt(80.0),
            PaintTranslation::new(0.0, 40.0),
            PaintTranslation::new(30.0, -20.0),
            PaintClip::new(30.0, 80.0, 20.0, 40.0),
        );
        assert_eq!(
            candidate.continuous_source_to_destination(),
            PaintTranslation::new(30.0, 20.0),
            "continuous positioned paint localizes itself to each source column slice",
        );
    }

    #[test]
    fn projected_candidate_continuation_preserves_source_local_clip_and_step() {
        let first = PositionedFragmentCandidate::new(
            PaintClip::new(4.0, 100.0, 20.0, 40.0),
            layout_pt(0.0),
            layout_pt(40.0),
            PaintTranslation::identity(),
            PaintTranslation::new(10.0, -5.0),
            PaintClip::new(14.0, 95.0, 20.0, 40.0),
        );
        let second = PositionedFragmentCandidate::new(
            first.source_clip,
            layout_pt(40.0),
            layout_pt(80.0),
            PaintTranslation::new(0.0, 40.0),
            PaintTranslation::new(30.0, -5.0),
            PaintClip::new(34.0, 95.0, 20.0, 40.0),
        );

        let continuation = second.continuation_after(first);
        assert_eq!(continuation.source_clip, first.source_clip);
        assert_eq!(continuation.source_block_start, layout_pt(80.0));
        assert_eq!(continuation.source_block_end, layout_pt(120.0));
        assert_eq!(
            continuation.continuous_source_to_local,
            PaintTranslation::new(0.0, 80.0)
        );
        assert_eq!(
            continuation.destination_clip,
            PaintClip::new(54.0, 95.0, 20.0, 40.0)
        );
    }
}