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
use super::super::super::*;
use super::HypotheticalClearBorderEdge;
use super::exclusions::FLOAT_EPSILON;
use super::model::*;

/// Resolve the auto border-box width available to a BFC root beside floats.
///
/// A negative margin on the float-facing side belongs outside the collision
/// rectangle and must not re-expand the border box into the float. A negative
/// margin on the opposite side remains part of the normal block-width
/// equation, so it may extend the border box away from the float.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
pub(in crate::layout) fn float_avoiding_auto_border_box_width(
    band: PageInlineSpan,
    containing: PageInlineSpan,
    margin_left: f32,
    margin_right: f32,
) -> BorderBoxLength {
    let left_margin = if band.left_x() > containing.left_x() + FLOAT_EPSILON {
        0.0
    } else {
        margin_left
    };
    let right_margin = if band.right_x() < containing.right_x() - FLOAT_EPSILON {
        0.0
    } else {
        margin_right
    };
    border_box_pt((band.width() - left_margin - right_margin).max(0.0))
}

impl FloatContext {
    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn avoiding_position(
        &self,
        page_index: usize,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        containing_inline_span: PageInlineSpan,
    ) -> FloatBandPlacement {
        self.avoiding_position_with_role(
            page_index,
            top,
            margin_box_size,
            clear,
            placement_axes,
            containing_inline_span,
            true,
        )
    }

    /// Find a normal-flow BFC root's position without changing its
    /// hypothetical source position to the preceding float's top.
    ///
    /// Unlike a new float, a following BFC root does not participate in float
    /// stacking. It starts at its normal-flow position and only moves toward
    /// the block end when its own margin box cannot fit an active exclusion
    /// band. This is the table/grid/flex/replaced-box form of CSS 2.2 float
    /// avoidance:
    /// <https://www.w3.org/TR/CSS22/visuren.html#floats>.
    #[allow(clippy::too_many_arguments)]
    fn avoiding_bfc_position(
        &self,
        page_index: usize,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        containing_inline_span: PageInlineSpan,
    ) -> FloatBandPlacement {
        self.avoiding_position_with_role(
            page_index,
            top,
            margin_box_size,
            clear,
            placement_axes,
            containing_inline_span,
            false,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn avoiding_position_with_role(
        &self,
        page_index: usize,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        containing_inline_span: PageInlineSpan,
        is_float: bool,
    ) -> FloatBandPlacement {
        let width = margin_box_size.width;
        let height = margin_box_size.height;
        let mut top = self.clearance_top(
            clear,
            placement_axes,
            page_index,
            HypotheticalClearBorderEdge::new(top),
        );
        // A later float may share a row with an earlier float, but its outer
        // top may not be above that earlier float's outer top. Resolve
        // `clear` first, then apply this independent source-order constraint:
        // a cleared left float can still follow an earlier right float whose
        // top is lower than the cleared-left boundary.
        // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
        if is_float
            && let Some(last) = self
                .shapes
                .iter()
                .rev()
                .find(|shape| shape.is_css_float() && shape.page_index == page_index)
        {
            top = top.min(PageTopBlockPosition::new(
                last.margin_box_block_span().top_y(),
            ));
        }

        for _ in 0..self.shapes.len().saturating_add(2) {
            let block_span = PageBlockSpan::new(top.points(), height);
            let band = self.placement_band(page_index, block_span, containing_inline_span);
            let available_width = band.width();
            if width <= available_width + FLOAT_EPSILON {
                return FloatBandPlacement::new(band, top);
            }

            let next_top = self
                .active_placement_shapes(page_index, block_span)
                .map(|shape| PageTopBlockPosition::new(shape.margin_box_block_span().bottom_y()))
                .fold(top, PageTopBlockPosition::min);
            if next_top.points() >= top.points() - FLOAT_EPSILON {
                return FloatBandPlacement::new(band, top);
            }
            top = next_top;
        }

        let band = self.placement_band(
            page_index,
            PageBlockSpan::new(top.points(), height),
            containing_inline_span,
        );
        FloatBandPlacement::new(band, top)
    }

    /// Find a float-avoiding placement for a normal-flow BFC root whose size
    /// depends on the available band.
    ///
    /// CSS 2.2 requires a BFC root's border box to avoid earlier float margin
    /// boxes in the same block formatting context, and allows the BFC root to
    /// become narrower than the normal block-width equation would otherwise
    /// make it. Because that narrower width can change the root's block size,
    /// this solves placement as a fixed point between the active float band and
    /// the measured border box:
    /// <https://www.w3.org/TR/CSS22/visuren.html#floats>.
    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn avoiding_bfc_root_position<F>(
        &self,
        page_index: usize,
        top: PageTopBlockPosition,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        left: f32,
        right: f32,
        mut measure: F,
    ) -> FloatAvoidingBfcPlacement
    where
        F: FnMut(FloatBand, PageTopBlockPosition) -> FloatAvoidanceCandidate,
    {
        let mut top = self.clearance_top(
            clear,
            placement_axes,
            page_index,
            HypotheticalClearBorderEdge::new(top),
        );
        let inline_span = PageInlineSpan::from_edges(left, right);
        let mut last_placement = None;
        for _ in 0..self.shapes.len().saturating_add(2) {
            let mut band = self.content_band(
                page_index,
                PageBlockSpan::new(top.points(), FLOAT_EPSILON),
                inline_span,
            );
            let mut measured = measure(band, top);
            let mut stable = false;

            for _ in 0..self.shapes.len().saturating_add(3) {
                let height = measured
                    .normal_flow_border_box_block_size
                    .points()
                    .max(FLOAT_EPSILON);
                let next_band = self.content_band(
                    page_index,
                    PageBlockSpan::new(top.points(), height),
                    inline_span,
                );
                let next_measured = measure(next_band, top);
                stable = (next_band.left() - band.left()).abs() <= FLOAT_EPSILON
                    && (next_band.width() - band.width()).abs() <= FLOAT_EPSILON
                    && (next_measured.normal_flow_border_box_inline_span.width()
                        - measured.normal_flow_border_box_inline_span.width())
                    .abs()
                        <= FLOAT_EPSILON
                    && (next_measured.normal_flow_border_box_block_size.points()
                        - measured.normal_flow_border_box_block_size.points())
                    .abs()
                        <= FLOAT_EPSILON;
                band = next_band;
                measured = next_measured;
                if stable {
                    break;
                }
            }

            let placement = FloatAvoidingBfcPlacement {
                placement: FloatBandPlacement::new(band, top),
                candidate: measured,
            };
            last_placement = Some(placement);
            // A BFC root avoids float margin boxes with its normal-flow border
            // box. Its border box need not be contained in the residual band:
            // a negative margin may extend it past the opposite side of the
            // containing block while still leaving it disjoint from every
            // float. Test actual rectangle collision rather than comparing
            // the box to the band's two edges.
            // <https://www.w3.org/TR/CSS22/visuren.html#floats>
            let border_box_span = measured.normal_flow_border_box_inline_span;
            let border_box_block_span = PageBlockSpan::new(
                top.points(),
                measured
                    .normal_flow_border_box_block_size
                    .points()
                    .max(FLOAT_EPSILON),
            );
            let avoids_active_floats =
                self.active_shapes(page_index, border_box_block_span)
                    .all(|shape| {
                        // A zero-inline-size float still participates in
                        // `clear` and establishes its block position, but it
                        // has no horizontal collision area. Treating its two
                        // coincident edges as a rectangle would incorrectly
                        // force an otherwise adjacent BFC root below it.
                        // <https://www.w3.org/TR/CSS22/visuren.html#floats>
                        if shape.margin_box_inline_span().width() <= FLOAT_EPSILON {
                            return true;
                        }
                        let Some(shape_span) = shape
                            .area
                            .horizontal_edges(shape.rect, border_box_block_span)
                        else {
                            return true;
                        };
                        border_box_span.right_x() <= shape_span.left_x() + FLOAT_EPSILON
                            || border_box_span.left_x() >= shape_span.right_x() - FLOAT_EPSILON
                    });
            // Avoidance is a collision constraint, but a fixed-width BFC
            // root that merely overflows its containing block must still
            // clear the float. A negative physical margin is the exception:
            // CSS's normal block-width equation may legally put the border
            // box outside the containing block while it remains adjacent to
            // the float.
            // <https://www.w3.org/TR/CSS22/visuren.html#floats>
            let fits_containing_inline_span = (measured.permits_inline_start_overflow()
                || border_box_span.left_x() >= left - FLOAT_EPSILON)
                && (measured.permits_inline_end_overflow()
                    || border_box_span.right_x() <= right + FLOAT_EPSILON);
            if stable && avoids_active_floats && fits_containing_inline_span {
                return placement;
            }

            let block_span = PageBlockSpan::new(
                top.points(),
                measured
                    .normal_flow_border_box_block_size
                    .points()
                    .max(FLOAT_EPSILON),
            );
            let rectangular_next_top = self
                .active_shapes(page_index, block_span)
                .map(|shape| PageTopBlockPosition::new(shape.margin_box_block_span().bottom_y()))
                .fold(top, PageTopBlockPosition::min);
            let shaped_next_top = self.next_content_slab_with_width(
                page_index,
                block_span,
                inline_span,
                measured.normal_flow_border_box_inline_span.width(),
            );
            let next_top = shaped_next_top
                .filter(|next_top| next_top.points() < top.points() - FLOAT_EPSILON)
                .unwrap_or(rectangular_next_top);
            if next_top.points() >= top.points() - FLOAT_EPSILON {
                return placement;
            }
            top = next_top;
        }

        last_placement.unwrap_or_else(|| {
            let band = self.content_band(
                page_index,
                PageBlockSpan::new(top.points(), FLOAT_EPSILON),
                inline_span,
            );
            let measured = measure(band, top);
            FloatAvoidingBfcPlacement {
                placement: FloatBandPlacement::new(band, top),
                candidate: measured,
            }
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn vertical_avoiding_position(
        &self,
        page_index: usize,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        avoidance_writing_mode: WritingMode,
        direction: Direction,
        block_slab: PageInlineSpan,
        page_bottom: PageTopBlockPosition,
        side: Option<UsedFloatSide>,
    ) -> FloatBandPlacement {
        let width = margin_box_size.width;
        let height = margin_box_size.height;
        let top = self.clearance_top(
            clear,
            placement_axes,
            page_index,
            HypotheticalClearBorderEdge::new(top),
        );
        let inline_size = (top.points() - page_bottom.points()).max(height).max(1.0);
        let inline_physical_start = match inline_start_side(avoidance_writing_mode, direction) {
            PhysicalSide::Top => top,
            PhysicalSide::Bottom => page_bottom,
            PhysicalSide::Left | PhysicalSide::Right => {
                unreachable!("a vertical containing block has a vertical inline axis")
            }
        };
        let block_start = block_start_side(avoidance_writing_mode);
        let initial_block_slab = match block_start {
            PhysicalSide::Left => PageInlineSpan::new(block_slab.left_x(), width.max(1.0)),
            PhysicalSide::Right => PageInlineSpan::from_edges(
                (block_slab.right_x() - width.max(1.0)).max(block_slab.left_x()),
                block_slab.right_x(),
            ),
            PhysicalSide::Top | PhysicalSide::Bottom => {
                unreachable!("a vertical writing mode has a horizontal block axis")
            }
        };
        // With no exclusions, a BFC root remains at its hypothetical physical
        // top regardless of whether the vertical inline axis starts at the
        // page top or bottom.  Deriving the position from a bottom-to-top
        // empty logical band would instead select that band's far physical
        // edge and displace the box by the complete available inline span.
        // <https://www.w3.org/TR/css-writing-modes-4/#block-flow>
        // <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
        if !self.shapes.iter().any(FloatShape::is_css_float) {
            let placed_top = match side {
                // A vertical inline-end float is anchored at the physical
                // inline-end edge even before it has any sibling exclusion.
                // `top` is the block cursor, not the vertical inline-axis
                // start for this float.
                // <https://drafts.csswg.org/css-writing-modes-4/#logical-to-physical>
                Some(UsedFloatSide::Bottom) => page_bottom.points() + height,
                Some(UsedFloatSide::Top)
                | Some(UsedFloatSide::Left | UsedFloatSide::Right)
                | None => top.points(),
            };
            return FloatBandPlacement::new(
                FloatBand::from_span(PageInlineSpan::new(
                    initial_block_slab.left_x(),
                    inline_size,
                )),
                PageTopBlockPosition::new(placed_top),
            );
        }
        let mut block_slab = initial_block_slab;

        for _ in 0..self.shapes.len().saturating_add(2) {
            let vertical_slab = vertical_physical_inline_span(
                avoidance_writing_mode,
                direction,
                inline_physical_start,
                layout_pt(inline_size),
            );
            let band = self.logical_band(
                avoidance_writing_mode,
                direction,
                page_index,
                FloatBandQuery {
                    horizontal_slab: block_slab,
                    vertical_slab,
                },
            );
            let placed_top = match side {
                Some(UsedFloatSide::Bottom) => band.block_span.bottom_y() + height,
                Some(UsedFloatSide::Top) | None => band.block_span.top_y(),
                Some(UsedFloatSide::Left | UsedFloatSide::Right) => band.block_span.top_y(),
            };
            if height <= band.inline_span.size() + FLOAT_EPSILON {
                return FloatBandPlacement::new(
                    FloatBand::from_span(PageInlineSpan::new(
                        block_slab.left_x(),
                        band.inline_span.size(),
                    )),
                    PageTopBlockPosition::new(placed_top),
                );
            }

            let Some(next_start) = self.next_vertical_float_slab_start(
                page_index,
                block_slab,
                vertical_slab,
                block_start,
            ) else {
                return FloatBandPlacement::new(
                    FloatBand::from_span(PageInlineSpan::new(
                        block_slab.left_x(),
                        band.inline_span.size(),
                    )),
                    PageTopBlockPosition::new(placed_top),
                );
            };
            let advances_toward_block_end = match block_start {
                PhysicalSide::Left => next_start.left_x() > block_slab.left_x() + FLOAT_EPSILON,
                PhysicalSide::Right => next_start.right_x() < block_slab.right_x() - FLOAT_EPSILON,
                PhysicalSide::Top | PhysicalSide::Bottom => unreachable!(),
            };
            if !advances_toward_block_end {
                return FloatBandPlacement::new(
                    FloatBand::from_span(PageInlineSpan::new(
                        block_slab.left_x(),
                        band.inline_span.size(),
                    )),
                    PageTopBlockPosition::new(placed_top),
                );
            }
            block_slab = next_start;
        }

        let band = self.logical_band(
            avoidance_writing_mode,
            direction,
            page_index,
            FloatBandQuery {
                horizontal_slab: block_slab,
                vertical_slab: vertical_physical_inline_span(
                    avoidance_writing_mode,
                    direction,
                    inline_physical_start,
                    layout_pt(inline_size),
                ),
            },
        );
        let placed_top = match side {
            Some(UsedFloatSide::Bottom) => band.block_span.bottom_y() + height,
            Some(UsedFloatSide::Top) | None => band.block_span.top_y(),
            Some(UsedFloatSide::Left | UsedFloatSide::Right) => band.block_span.top_y(),
        };
        FloatBandPlacement::new(
            FloatBand::from_span(PageInlineSpan::new(
                block_slab.left_x(),
                band.inline_span.size(),
            )),
            PageTopBlockPosition::new(placed_top),
        )
    }

    pub(in crate::layout) fn next_vertical_float_slab_start(
        &self,
        page_index: usize,
        block_slab: PageInlineSpan,
        inline_slab: PageBlockSpan,
        block_start: PhysicalSide,
    ) -> Option<PageInlineSpan> {
        let candidates = self
            .shapes
            .iter()
            .filter(|shape| {
                let shape_block_span = shape.margin_box_block_span();
                let shape_inline_span = shape.margin_box_inline_span();
                shape.is_css_float()
                    && shape.page_index == page_index
                    && matches!(shape.side, UsedFloatSide::Top | UsedFloatSide::Bottom)
                    && shape_inline_span.right_x() > block_slab.left_x() + FLOAT_EPSILON
                    && shape_inline_span.left_x() < block_slab.right_x() - FLOAT_EPSILON
                    && shape_block_span.top_y() > inline_slab.bottom_y() + FLOAT_EPSILON
                    && shape_block_span.bottom_y() < inline_slab.top_y() - FLOAT_EPSILON
            })
            .map(|shape| shape.margin_box_inline_span());
        match block_start {
            PhysicalSide::Left => candidates
                .map(|shape| PageInlineSpan::new(shape.right_x(), block_slab.width()))
                .filter(|next| next.left_x() > block_slab.left_x() + FLOAT_EPSILON)
                .min_by(|left, right| {
                    left.left_x()
                        .partial_cmp(&right.left_x())
                        .unwrap_or(std::cmp::Ordering::Equal)
                }),
            PhysicalSide::Right => candidates
                .map(|shape| {
                    PageInlineSpan::from_edges(shape.left_x() - block_slab.width(), shape.left_x())
                })
                .filter(|next| next.right_x() < block_slab.right_x() - FLOAT_EPSILON)
                .max_by(|left, right| {
                    left.right_x()
                        .partial_cmp(&right.right_x())
                        .unwrap_or(std::cmp::Ordering::Equal)
                }),
            PhysicalSide::Top | PhysicalSide::Bottom => {
                unreachable!("a vertical writing mode has a horizontal block axis")
            }
        }
    }
}

pub(in crate::layout) fn vertical_physical_inline_span(
    writing_mode: WritingMode,
    direction: Direction,
    inline_physical_start: PageTopBlockPosition,
    inline_size: LayoutLength,
) -> PageBlockSpan {
    match inline_start_side(writing_mode, direction) {
        PhysicalSide::Top => PageBlockSpan::from_edges(
            inline_physical_start.points(),
            inline_physical_start.toward_block_end(inline_size).points(),
        ),
        PhysicalSide::Bottom => PageBlockSpan::from_edges(
            PageTopBlockPosition::new(inline_physical_start.points() + inline_size.points())
                .points(),
            inline_physical_start.points(),
        ),
        PhysicalSide::Left | PhysicalSide::Right => {
            unreachable!("vertical inline axis must map to top or bottom")
        }
    }
}

impl<'a> LayoutBuilder<'a> {
    /// Finds the normal-flow margin-box position for a BFC root beside active floats.
    ///
    /// CSS 2.2 requires block formatting context roots, table wrappers, and
    /// replaced block boxes to avoid overlap with earlier floats in the same
    /// block formatting context; `clear` first moves the box below matching
    /// floats, then collision search finds the highest band wide enough for
    /// the margin box:
    /// <https://www.w3.org/TR/CSS22/visuren.html#floats> and
    /// <https://www.w3.org/TR/CSS22/visuren.html#flow-control>.
    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn place_float_avoiding_margin_box(
        &self,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_direction: Direction,
    ) -> FloatPlacement {
        let width = margin_box_size.width;
        let placement_axes = FloatPlacementAxes::new(
            self.containing_block_writing_mode,
            self.containing_block_direction,
        );
        if self.containing_block_writing_mode != WritingMode::HorizontalTb {
            let placement = self.find_vertical_float_avoiding_position(
                top,
                margin_box_size,
                clear,
                placement_axes,
                self.current_content_logical_inline_content_size(),
                None,
            );
            return FloatPlacement::new(placement.origin, placement.available_span);
        }
        let placement =
            self.find_bfc_avoiding_position(top, margin_box_size, clear, placement_axes);
        let x = if placement_direction == Direction::Rtl {
            placement.origin.x() + (placement.available_span.width() - width).max(0.0)
        } else {
            placement.origin.x()
        };
        FloatPlacement::new(
            PageTopPoint::new(x, placement.origin.top_y()),
            placement.available_span,
        )
    }

    fn find_bfc_avoiding_position(
        &self,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
    ) -> FloatBandPlacement {
        let page_index = self.current_float_page_index();
        let context = self
            .float_contexts
            .last()
            .expect("root float context exists");
        context.avoiding_bfc_position(
            page_index,
            top,
            margin_box_size,
            clear,
            placement_axes,
            PageInlineSpan::from_edges(self.content_left, self.content_right),
        )
    }

    /// Find a CSS float position after resolving collision with an earlier
    /// same-side initial letter.
    ///
    /// The initial-letter adjustment is deliberately outside the normal
    /// `clear` resolution so `clear` continues to see CSS floats only.
    /// <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
    pub(in crate::layout) fn find_inline_float_avoiding_position(
        &self,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        side: UsedFloatSide,
    ) -> FloatBandPlacement {
        let page_index = self.current_float_page_index();
        let context = self
            .float_contexts
            .last()
            .expect("root float context exists");
        let top = context.initial_letter_float_avoidance_top(page_index, top, side);
        context.avoiding_position(
            page_index,
            top,
            margin_box_size,
            clear,
            placement_axes,
            PageInlineSpan::from_edges(self.content_left, self.content_right),
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn find_vertical_float_avoiding_position(
        &self,
        top: PageTopBlockPosition,
        margin_box_size: MarginBoxSize,
        clear: Clear,
        placement_axes: FloatPlacementAxes,
        containing_logical_inline_size: LogicalInlineContentSize,
        side: Option<UsedFloatSide>,
    ) -> FloatBandPlacement {
        let page_index = self.current_float_page_index();
        let context = self
            .float_contexts
            .last()
            .expect("root float context exists");
        let avoidance_writing_mode = self.containing_block_writing_mode;
        let containing_physical_bottom = if avoidance_writing_mode.has_vertical_lines() {
            // A vertical containing block's logical inline axis is local
            // physical Y. Bottom-side floats align to that box's physical
            // bottom edge; the page fragmentainer edge belongs to the outer
            // block flow and must not turn local inline placement into a page
            // transition.
            // <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
            // <https://www.w3.org/TR/css-break-3/#fragmentation-model>
            PageTopBlockPosition::new(
                top.points() - containing_logical_inline_size.points().max(0.0),
            )
        } else {
            PageTopBlockPosition::new(self.page_bottom())
        };
        context.vertical_avoiding_position(
            page_index,
            top,
            margin_box_size,
            clear,
            placement_axes,
            avoidance_writing_mode,
            self.containing_block_direction,
            PageInlineSpan::from_edges(self.content_left, self.content_right),
            containing_physical_bottom,
            side,
        )
    }

    pub(in crate::layout) fn prebreak_bfc_margin_box_if_needed(
        &mut self,
        margin_box_height: MarginBoxLength,
        reapplied_margin_top: f32,
    ) {
        // Out-of-flow roots resolve their static/absolute position first; the
        // normal-flow BFC prebreak heuristic would incorrectly move a
        // bottom-anchored positioned table or block to the next page.
        if margin_box_height.points() <= FLOAT_EPSILON
            || self.out_of_flow_prebreak_suppression_depth > 0
            || margin_box_height.points() > self.page_area_height() + FLOAT_EPSILON
            || self.cursor_y - margin_box_height.points() >= self.page_bottom() - FLOAT_EPSILON
            || self.cursor_is_at_page_top()
            || !self.current_page_has_content()
        {
            return;
        }
        self.push_page();
        self.cursor_y -= reapplied_margin_top;
    }

    pub(in crate::layout) fn current_float_page_index(&self) -> usize {
        self.pages.len()
    }
}