ferri-pdf 0.1.2

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
#[allow(
    clippy::wildcard_imports,
    reason = "pagination submodules extend the parent paginator and share its scope"
)]
use super::*;

#[derive(Clone, Copy)]
struct LineGeometry {
    x: f32,
    width: f32,
    y: f32,
}

/// An exclusion placed while fitting the lines of a fragment, kept only if the line it
/// belongs to is.
enum FloatTrial {
    /// The float of the paragraph's `anchor`th float anchor, placed at `y`.
    Float { anchor: usize, line: usize, y: f32 },
    /// The exclusion of the initial letter on the paragraph's first line.
    Letter {
        side: crate::style::FloatSide,
        margin: Rect,
    },
}
impl FloatTrial {
    fn line(&self) -> usize {
        match self {
            Self::Float { line, .. } => *line,
            Self::Letter { .. } => 0,
        }
    }
}

/// Retains source progress and inline paint state between physical page fragments.
#[derive(Clone)]
pub(super) struct ParagraphCursor<'a, 't> {
    index: usize,
    source: &'a crate::document::Paragraph,
    style: &'a crate::style::Style,
    prepared: Option<Rc<crate::text::PreparedParagraph<'a, 't, 't>>>,
    positions: std::vec::IntoIter<crate::text::TextPosition<'a>>,
    position: crate::text::TextPosition<'a>,
    lines: std::vec::IntoIter<crate::text::Line>,
    x: f32,
    width: f32,
    containing_height: Option<f32>,
    candidate_complete: bool,
    candidate_page: usize,
    geometry: VecDeque<LineGeometry>,
    anchors: std::iter::Peekable<std::vec::IntoIter<crate::layout::measure::LineAnchor<'a>>>,
    atomics: Vec<Option<crate::layout::inline::MeasuredAtomic<'a>>>,
    metrics: Vec<crate::text::AtomicMetrics>,
    consumed_anchors: usize,
    consumed_events: usize,
    consumed_floats: usize,
    line_index: usize,
    layers: InlineLayers<'a>,
    clamp: Option<crate::text::ParagraphClamp>,
    /// The atomic inline index of an initial letter on the first line, until it is placed.
    letter: Option<usize>,
}

/// Where an initial letter on a fitted first line excludes the lines after it (CSS Inline 3
/// §7.5): its margin box below the line, at the line's start side.
fn letter_exclusion(
    extent: crate::layout::inline::LetterExtent,
    width: f32,
    fragment: &crate::text::AtomicFragment,
    line: &crate::text::Line,
    placement: LineGeometry,
    rtl: bool,
) -> Option<(crate::style::FloatSide, Rect)> {
    let x = placement.x + line.alignment_offset(placement.width, rtl) + fragment.x;
    let top = placement.y + line.height;
    let bottom = placement.y + extent.bottom;
    let side = if rtl {
        crate::style::FloatSide::Right
    } else {
        crate::style::FloatSide::Left
    };
    (bottom > top).then_some((
        side,
        Rect {
            x: if rtl { x } else { x - extent.kerning },
            y: top,
            width: width + extent.kerning,
            height: bottom - top,
        },
    ))
}

impl<'a> Paginator<'a, '_, 'a> {
    fn place_inline_float(
        &mut self,
        measured: floats::MeasuredFloat<'a>,
        anchor: &crate::document::Anchor,
        geometry: [f32; 3],
        layers: &InlineLayers<'a>,
    ) -> Result<(), LayoutError> {
        let chain = inline_chain(layers.paragraph, anchor.inline_box);
        let chain_layers = self.chain_layers(&chain, layers.containing);
        let saved_context = self.context.clone();
        let saved_key = self.key.clone();
        let saved_group = self.group;
        let saved_clip = self.clip;
        let saved_offset = self.offset;
        let saved_has_flow = self.has_flow;
        if let Some(layer) = chain_layers.last() {
            self.context = layer.context.clone();
            self.key = layer.key.clone();
            self.group = layer.group;
            self.clip = layer.clip;
            self.offset[0] += layer.offset[0];
            self.offset[1] += layer.offset[1];
        }
        let opened = self.open_inline_containers(&chain, &chain_layers);
        // Candidate lines preceding this float have not been painted yet.
        self.has_flow |= geometry[2] > self.y;
        let result = self.place_measured_float(measured, geometry[0], geometry[1], geometry[2]);
        self.close_inline_containers(&opened);
        self.context = saved_context;
        self.key = saved_key;
        self.group = saved_group;
        self.clip = saved_clip;
        self.offset = saved_offset;
        self.has_flow = saved_has_flow;
        result
    }

    /// Excludes the lines after the fitted first line `line`, at `placement`, that its
    /// initial letter `letter` reaches, once per fragment's trials.
    fn place_letter_trial(
        &mut self,
        letter: Option<usize>,
        atomics: &[Option<crate::layout::inline::MeasuredAtomic<'a>>],
        line: &crate::text::Line,
        placement: LineGeometry,
        rtl: bool,
        trials: &mut Vec<FloatTrial>,
    ) {
        let Some(index) = letter else {
            return;
        };
        if trials
            .iter()
            .any(|trial| matches!(trial, FloatTrial::Letter { .. }))
        {
            return;
        }
        let atomic = atomics[index]
            .as_ref()
            .expect("the initial letter is measured until its line is placed");
        let fragment = line
            .atomics
            .iter()
            .find(|fragment| fragment.index == index)
            .expect("the initial letter lies on the first line");
        if let Some((side, margin)) = letter_exclusion(
            atomic.letter.expect("an initial letter has an extent"),
            atomic.metrics.width,
            fragment,
            line,
            placement,
            rtl,
        ) {
            self.floats
                .last_mut()
                .expect("flow has a float context")
                .insert_letter(self.page, side, margin);
            trials.push(FloatTrial::Letter { side, margin });
        }
    }

    fn fit_float_line(
        &self,
        prepared: &crate::text::PreparedParagraph<'a, 'a, 'a>,
        position: crate::text::TextPosition<'a>,
        metrics: &[crate::text::AtomicMetrics],
        bounds: [f32; 2],
        mut y: f32,
    ) -> Result<
        Option<(
            crate::text::Line,
            crate::text::TextPosition<'a>,
            LineGeometry,
        )>,
        LayoutError,
    > {
        let context = self.floats.last().expect("flow has a float context");
        let mut height = 0.0f32;
        loop {
            self.measurer.work.spend(metrics.len().max(1))?;
            let (band, met) =
                context.line_band(self.page, y, height, bounds, &self.measurer.work)?;
            let width = (band[1] - band[0]).max(0.0);
            let mut lines =
                prepared.lines_in_block(position, width, bounds[1] - bounds[0], metrics)?;
            let Some(line) = lines.next() else {
                return Ok(None);
            };
            let line = line?;
            self.measurer.work.spend(line.range.len().max(1))?;
            if line.height > height {
                height = line.height;
                continue;
            }
            if line.width + line.indent > width && met {
                y = context.line_fit(
                    self.page,
                    y,
                    height,
                    bounds,
                    line.width + line.indent,
                    &self.measurer.work,
                )?;
                height = 0.0;
                continue;
            }
            return Ok(Some((
                line,
                lines.checkpoint(),
                LineGeometry {
                    x: band[0],
                    width,
                    y,
                },
            )));
        }
    }
    pub(super) fn paragraph_break(&mut self) -> Option<BreakRequest> {
        let scope = self
            .boxes
            .iter()
            .rev()
            .find_map(|active| active.page_scope.clone());
        let changed = scope.is_some_and(|name| self.page_name(name));
        Forced::combine(self.pending_break, changed.then_some(Forced::Page))
            .filter(|_| self.unbroken == 0 && self.has_flow)
            .map(|forced| BreakRequest {
                forced: Some(forced),
                preserve_empty: false,
            })
    }
    pub(super) fn start_paragraph(
        &mut self,
        paragraph: MeasuredParagraph<'a>,
    ) -> ParagraphCursor<'a, 'a> {
        if !paragraph.is_phantom() {
            self.pending_break = None;
            self.flush_margin();
        }
        let prepared = None;
        let mut positions = paragraph.positions.into_iter();
        let position = positions
            .next()
            .expect("paragraph measurement has a start checkpoint");
        let lines = paragraph.lines.into_iter();
        let x = paragraph.x;
        let width = paragraph.width;
        let containing_height = paragraph.containing_height;
        let candidate_complete = true;
        let candidate_page = self.page;
        let style = paragraph.style;
        let mut anchors = paragraph.anchors.into_iter().peekable();
        let atomics = paragraph.atomics;
        let metrics = atomics
            .iter()
            .map(|atomic| {
                atomic
                    .as_ref()
                    .expect("measured atomics have not been placed")
                    .metrics
            })
            .collect::<Vec<_>>();
        let consumed_anchors = 0;
        let consumed_events = 0;
        let line_index = 0;
        let layers = InlineLayers::new(
            paragraph.paragraph,
            InlineContaining {
                width,
                height: containing_height,
                rtl: style.used_rtl(),
            },
        );
        if lines.as_slice().is_empty() {
            for anchor in anchors.by_ref() {
                self.line_anchor(&anchor, x, width, 0.0, 0.0, &layers);
            }
        }
        let source = paragraph.paragraph;
        let index = paragraph.index;
        let clamp = paragraph.clamp;
        let letter = source
            .atomics()
            .first()
            .filter(|atomic| atomic.offset == 0 && atomic.block.initial_letter().is_some())
            .map(|_| 0);
        ParagraphCursor {
            index,
            source,
            style,
            prepared,
            positions,
            position,
            lines,
            x,
            width,
            containing_height,
            candidate_complete,
            candidate_page,
            geometry: VecDeque::new(),
            anchors,
            atomics,
            metrics,
            consumed_anchors,
            consumed_events,
            consumed_floats: 0,
            line_index,
            layers,
            clamp,
            letter,
        }
    }
    pub(super) fn paragraph_fragment(
        &mut self,
        cursor: Box<ParagraphCursor<'a, 'a>>,
        tail: BreakTail,
    ) -> Result<Option<Box<ParagraphCursor<'a, 'a>>>, LayoutError> {
        let text = self.measurer.text;
        let ParagraphCursor {
            index,
            source,
            style,
            mut prepared,
            mut positions,
            mut position,
            mut lines,
            mut x,
            mut width,
            mut containing_height,
            mut candidate_complete,
            mut candidate_page,
            mut geometry,
            mut anchors,
            mut atomics,
            mut metrics,
            mut consumed_anchors,
            mut consumed_events,
            mut consumed_floats,
            mut line_index,
            mut layers,
            clamp,
            mut letter,
        } = *cursor;
        let float_anchors: Vec<_> = source
            .anchors()
            .iter()
            .filter(|anchor| anchor.block.style().floating().is_some())
            .collect();
        let has_inline_floats = consumed_floats < float_anchors.len() || letter.is_some();
        let mut snapshot = has_inline_floats.then(|| self.snapshot()).transpose()?;
        let saved_layers = has_inline_floats.then(|| layers.clone());
        let mut trials: Vec<FloatTrial> = Vec::new();
        'fragment: {
            if lines.as_slice().is_empty() && candidate_complete {
                break 'fragment;
            }
            self.measurer.work.spend(1)?;
            let reserve = self.continuation_reserve()?;
            let closing_reserve = self.closing_reserve(tail, reserve)?;
            {
                let (left, destination_width, height) =
                    self.paragraph_geometry()
                        .unwrap_or((x, width, containing_height));
                x = left;
                let floating = has_inline_floats
                    || !self
                        .floats
                        .last()
                        .expect("flow has a float context")
                        .is_empty();
                if floating
                    || width != destination_width
                    || (!atomics.is_empty() && containing_height != height)
                    || (!candidate_complete && candidate_page != self.page)
                {
                    width = destination_width;
                    containing_height = height;
                    for (index, atomic) in atomics.iter_mut().enumerate() {
                        if atomic.is_some() {
                            let measured = self.measurer.run(|measurer| {
                                crate::layout::inline::measure(
                                    measurer,
                                    &source.inline_block(index).block,
                                    width,
                                    height,
                                    style.used_rtl(),
                                )
                            })?;
                            metrics[index] = measured.metrics;
                            *atomic = Some(measured);
                        }
                    }
                    if prepared.is_none() {
                        self.measurer.work.spend(source.text().len().max(1))?;
                    }
                    self.measurer.work.spend(metrics.len().max(1))?;
                    let prepared = prepared.get_or_insert_with(|| {
                        Rc::new(text.prepare_paragraph(source).clamped(clamp.clone()))
                    });
                    let mut stream = prepared.lines(position, width, &metrics)?;
                    geometry.clear();
                    let mut float_position = position;
                    let mut remainder = Vec::new();
                    let mut checkpoints = Vec::new();
                    candidate_complete = true;
                    candidate_page = self.page;
                    let mut extent = self.y + reserve;
                    let mut available: usize = 0;
                    let mut overflow = false;
                    let mut line_clearance_y = None;
                    loop {
                        let (line, checkpoint) = if floating {
                            let cleared_y = if let Some(y) = line_clearance_y {
                                y
                            } else {
                                let (clear, rtl) = source.clearance_at(float_position.offset());
                                let y = self
                                    .floats
                                    .last()
                                    .expect("flow has a float context")
                                    .clearance_end(self.page, clear, rtl, &self.measurer.work)?
                                    .map_or(extent - reserve, |(page, bottom)| {
                                        if page > self.page {
                                            self.height.max(extent - reserve)
                                        } else {
                                            bottom.max(extent - reserve)
                                        }
                                    });
                                // An initial letter's line clears earlier initial letters
                                // (CSS Inline 3 §7.9.2) and moves down for the letter to
                                // stay below the content edge (§7.6.1).
                                let y = match letter.and_then(|index| atomics[index].as_ref()) {
                                    Some(atomic) if remainder.is_empty() => self
                                        .floats
                                        .last()
                                        .expect("flow has a float context")
                                        .letter_clearance(self.page, y, &self.measurer.work)?
                                        .max(
                                            extent - reserve
                                                + atomic
                                                    .letter
                                                    .expect("an initial letter has an extent")
                                                    .rise(),
                                        ),
                                    _ => y,
                                };
                                line_clearance_y = Some(y);
                                y
                            };
                            let Some((line, checkpoint, placement)) = self.fit_float_line(
                                prepared,
                                float_position,
                                &metrics,
                                [x, x + width],
                                cleared_y,
                            )?
                            else {
                                break;
                            };
                            let next_anchor = consumed_floats
                                + trials
                                    .iter()
                                    .filter(|trial| matches!(trial, FloatTrial::Float { .. }))
                                    .count();
                            if let Some(anchor) = float_anchors.get(next_anchor).filter(|anchor| {
                                anchor.offset < line.range.end
                                    || (anchor.offset == line.range.end
                                        && !source.text()[line.range.clone()]
                                            .ends_with(['\n', '\u{2028}', '\u{2029}']))
                            }) {
                                let measured =
                                    self.measure_float(&anchor.block, width, containing_height)?;
                                let prefix = (line.x_at(anchor.offset, style.used_rtl())
                                    - line.x_at(line.range.start, style.used_rtl()))
                                .abs();
                                let occupied =
                                    prefix + if prefix > 0.0 { line.indent } else { 0.0 };
                                let y = placement.y
                                    + if measured.size[0] + occupied > placement.width {
                                        line.height
                                    } else {
                                        0.0
                                    };
                                // A float after the first line clears the initial letter
                                // beside it, which then excludes the lines it reaches.
                                if y > placement.y && remainder.is_empty() {
                                    self.place_letter_trial(
                                        letter,
                                        &atomics,
                                        &line,
                                        placement,
                                        style.used_rtl(),
                                        &mut trials,
                                    );
                                }
                                self.place_inline_float(measured, anchor, [x, width, y], &layers)?;
                                trials.push(FloatTrial::Float {
                                    anchor: next_anchor,
                                    line: remainder.len(),
                                    y,
                                });
                                continue;
                            }
                            if remainder.is_empty() {
                                self.place_letter_trial(
                                    letter,
                                    &atomics,
                                    &line,
                                    placement,
                                    style.used_rtl(),
                                    &mut trials,
                                );
                            }
                            extent = placement.y + reserve;
                            geometry.push_back(placement);
                            float_position = checkpoint;
                            line_clearance_y = None;
                            (line, checkpoint)
                        } else {
                            let Some(line) = stream.next() else {
                                break;
                            };
                            (line?, stream.checkpoint())
                        };
                        self.measurer.work.spend(line.range.len().max(1))?;
                        extent += line.height;
                        overflow |= overflows(extent, self.height);
                        if !overflow {
                            available += 1;
                        }
                        remainder.push(line);
                        checkpoints.push(checkpoint);
                        if overflow && remainder.len() > available.saturating_add(style.widows) {
                            candidate_complete = false;
                            break;
                        }
                    }
                    anchors = crate::layout::measure::line_anchors(
                        source,
                        &remainder,
                        candidate_complete,
                    )
                    .into_iter()
                    .skip(consumed_anchors)
                    .collect::<Vec<_>>()
                    .into_iter()
                    .peekable();
                    line_index = 0;
                    positions = checkpoints.into_iter();
                    lines = remainder.into_iter();
                }
            }
            layers.set_containing(InlineContaining {
                width,
                height: containing_height,
                rtl: style.used_rtl(),
            });
            let remaining = lines.as_slice().len();
            let mut height = 0.0;
            let available = lines
                .as_slice()
                .iter()
                .enumerate()
                .take_while(|(index, line)| {
                    height = geometry
                        .get(*index)
                        .map_or(height + line.height, |placement| {
                            placement.y - self.y + line.height
                        });
                    !overflows(self.y + height + reserve, self.height)
                })
                .count();
            let mut take = if self.unbroken > 0 {
                remaining
            } else {
                available.min(remaining)
            };
            let next_geometry = if take > 0 && take < remaining && style.widows > 1 {
                self.next_paragraph_geometry()?
                    .filter(|(next_width, next_height)| {
                        *next_width != width
                            || (!atomics.is_empty() && *next_height != containing_height)
                    })
            } else {
                None
            };
            if take < remaining {
                if take < style.orphans
                    || (next_geometry.is_none()
                        && remaining < style.orphans.saturating_add(style.widows))
                {
                    take = 0;
                } else if next_geometry.is_none() && remaining - take < style.widows {
                    take = remaining - style.widows;
                }
            }
            if take > 0
                && let Some((next_width, next_height)) = next_geometry
            {
                let mut next_metrics = metrics.clone();
                for (index, atomic) in atomics.iter().enumerate() {
                    if atomic.is_some() {
                        next_metrics[index] = self
                            .measurer
                            .run(|measurer| {
                                crate::layout::inline::measure(
                                    measurer,
                                    &source.inline_block(index).block,
                                    next_width,
                                    next_height,
                                    style.used_rtl(),
                                )
                            })?
                            .metrics;
                    }
                }
                if prepared.is_none() {
                    self.measurer.work.spend(source.text().len().max(1))?;
                }
                let prepared = prepared.get_or_insert_with(|| {
                    Rc::new(text.prepare_paragraph(source).clamped(clamp.clone()))
                });
                while take > 0 {
                    self.measurer.work.spend(next_metrics.len().max(1))?;
                    let checkpoint = positions.as_slice()[take - 1];
                    let mut next_lines = prepared.lines(checkpoint, next_width, &next_metrics)?;
                    let mut count = 0;
                    for line in next_lines.by_ref().take(style.widows) {
                        self.measurer.work.spend(line?.range.len().max(1))?;
                        count += 1;
                    }
                    if count >= style.widows {
                        break;
                    }
                    take -= 1;
                    if take < style.orphans {
                        take = 0;
                    }
                }
            }
            if self.unbroken == 0
                && candidate_complete
                && take == remaining
                && overflows(self.y + height + closing_reserve, self.height)
            {
                take = if remaining >= style.orphans.saturating_add(style.widows) {
                    remaining - style.widows
                } else {
                    0
                };
            }
            // An initial letter is monolithic, and breaks between the lines beside it are
            // avoided (CSS Inline 3 §7.9.4).
            if take > 0
                && self.unbroken == 0
                && self.has_flow
                && let Some(atomic) = letter.and_then(|index| atomics[index].as_ref())
                && let Some(first) = geometry.front()
            {
                let bottom = first.y
                    + atomic
                        .letter
                        .expect("an initial letter has an extent")
                        .bottom;
                let beside = 1 + geometry
                    .iter()
                    .skip(1)
                    .filter(|line| line.y < bottom)
                    .count();
                if take < beside.min(remaining) || overflows(bottom, self.height) {
                    take = 0;
                }
            }
            if take == 0 && !self.has_flow {
                take = available.min(remaining).max(1);
            }
            let accepted = trials.partition_point(|trial| trial.line() < take);
            if accepted < trials.len() {
                self.restore_state(snapshot.take().expect("inline float trial has a snapshot"));
                layers = saved_layers.expect("inline float trial saves its layers");
                for trial in &trials[..accepted] {
                    match *trial {
                        FloatTrial::Float { anchor, y, .. } => {
                            let anchor = float_anchors[anchor];
                            let measured =
                                self.measure_float(&anchor.block, width, containing_height)?;
                            self.place_inline_float(measured, anchor, [x, width, y], &layers)?;
                        }
                        FloatTrial::Letter { side, margin } => self
                            .floats
                            .last_mut()
                            .expect("flow has a float context")
                            .insert_letter(self.page, side, margin),
                    }
                }
            }
            consumed_floats += trials[..accepted]
                .iter()
                .filter(|trial| matches!(trial, FloatTrial::Float { .. }))
                .count();
            if take == 0 {
                break 'fragment;
            }
            for mut line in lines.by_ref().take(take) {
                self.measurer.work.spend(line.range.len().max(1))?;
                position = positions
                    .next()
                    .expect("each measured line has its source checkpoint");
                let (line_x, line_width) = if let Some(placement) = geometry.pop_front() {
                    self.y = placement.y;
                    (placement.x, placement.width)
                } else {
                    (x, width)
                };
                let offset = line.alignment_offset(line_width, style.used_rtl());
                while let Some(anchor) = anchors.next_if(|anchor| anchor.line == line_index) {
                    consumed_anchors += 1;
                    self.line_anchor(&anchor, line_x, line_width, offset, line.height, &layers);
                }
                self.line_events(source, &line, &mut consumed_events);
                line_index += 1;
                let baseline = self.top + self.y + line.ascent + self.offset[1];
                if !line.phantom {
                    self.record_baselines(self.page, [Some(baseline); 2]);
                }
                self.record_overflow(Rect {
                    x: line_x + offset + self.offset[0],
                    y: self.top + self.y + self.offset[1],
                    width: line.width,
                    height: line.height,
                });
                let markers: Vec<_> = self
                    .boxes
                    .iter_mut()
                    .filter_map(|active| {
                        active
                            .marker
                            .take()
                            .filter(|_| active.visible)
                            .map(|mut marker| {
                                marker.fragment.x += self.offset[0] - active.offset[0];
                                marker
                            })
                    })
                    .collect();
                for mut marker in markers {
                    marker.fragment.baseline = baseline;
                    self.emit_marker(*marker);
                }
                let line_layers = layers.line(&line);
                self.inline_content(
                    &line,
                    line_width,
                    line_x + offset,
                    &mut atomics,
                    line_layers,
                )?;
                if !line.phantom {
                    self.record_baselines(self.page, [Some(baseline); 2]);
                }
                letter = None;
                self.emit_paragraph_line(
                    index,
                    style,
                    &mut line,
                    [line_x + offset + self.offset[0], baseline],
                    line_layers,
                );
                self.y += line.height;
                if !line.phantom && (line.height > 0.0 || !line.text.is_empty()) {
                    self.has_flow = true;
                    self.at_page_start = false;
                }
            }
        }
        let mut trailing_clearance = false;
        if lines.as_slice().is_empty() && candidate_complete {
            let (clear, rtl) = source.clearance_at(source.text().len());
            if let Some((page, bottom)) = self
                .floats
                .last()
                .expect("flow has a float context")
                .clearance_end(self.page, clear, rtl, &self.measurer.work)?
            {
                trailing_clearance =
                    self.unbroken == 0 && (page > self.page || overflows(bottom, self.height));
                self.y = self.y.max(if trailing_clearance {
                    self.height
                } else {
                    bottom
                });
            }
        }
        if !lines.as_slice().is_empty() || !candidate_complete || trailing_clearance {
            return Ok(Some(Box::new(ParagraphCursor {
                index,
                source,
                style,
                prepared,
                positions,
                position,
                lines,
                x,
                width,
                containing_height,
                candidate_complete,
                candidate_page,
                geometry,
                anchors,
                atomics,
                metrics,
                consumed_anchors,
                consumed_events,
                consumed_floats,
                line_index,
                layers,
                clamp,
                letter,
            })));
        }
        if let Some((left, destination_width, height)) = self.paragraph_geometry() {
            x = left;
            width = destination_width;
            containing_height = height;
        }
        for anchor in &float_anchors[consumed_floats..] {
            self.measurer.work.spend(1)?;
            let measured = self.measure_float(&anchor.block, width, containing_height)?;
            self.place_inline_float(measured, anchor, [x, width, self.y], &layers)?;
        }
        for event in &source.semantic_events()[consumed_events..] {
            self.string_locations.record(self.page, &event.event);
        }
        self.finish_inline_backgrounds(&layers);
        Ok(None)
    }
    fn line_events(
        &mut self,
        source: &crate::document::Paragraph,
        line: &crate::text::Line,
        consumed_events: &mut usize,
    ) {
        let text_start = (!line.phantom)
            .then(|| {
                source.text()[line.range.clone()]
                    .char_indices()
                    .find(|(_, ch)| !crate::document::is_css_whitespace(*ch))
                    .map(|(offset, _)| line.range.start + offset)
            })
            .flatten();
        for event in source
            .semantic_events()
            .iter()
            .skip(*consumed_events)
            .take_while(|event| {
                event.offset < line.range.end || line.range.end == source.text().len()
            })
        {
            if text_start.is_some_and(|start| start < event.offset) {
                self.string_locations.text(self.page);
            }
            self.string_locations.record(self.page, &event.event);
            *consumed_events += 1;
        }
        if !line.phantom {
            self.string_locations.text(self.page);
        }
    }

    fn emit_paragraph_line(
        &mut self,
        index: usize,
        style: &crate::style::Style,
        line: &mut crate::text::Line,
        origin: [f32; 2],
        line_layers: &InlineLayers<'a>,
    ) {
        let paragraph = line_layers.paragraph;
        // Runs inside inline boxes with their own layers paint in them.
        let parts = self.split_line(
            line_layers,
            std::mem::take(&mut line.runs),
            &line.decorations,
        );
        let grouped = parts.len() > 1;
        let decorates = |run: &crate::text::GlyphRun, decoration: &DecorationPaint| {
            decoration.owner.is_none_or(|owner| {
                paragraph.is_within(paragraph.spans()[run.source_span].inline_box, owner)
            })
        };
        for part in parts {
            if self.force_hidden || (grouped && part.runs.is_empty() && part.decorations.is_empty())
            {
                continue;
            }
            let ink_origin = [
                origin[0] + part.layer.offset[0],
                origin[1] + part.layer.offset[1],
            ];
            for run in &part.runs {
                let style = &paragraph.spans()[run.source_span].style;
                self.anchors.text(
                    crate::layout::anchors::ink_key(style),
                    self.page,
                    run,
                    ink_origin,
                    part.layer.clip,
                    crate::layout::writing::Frame::for_style(style),
                );
            }
            for decoration in &part.decorations {
                let style = decoration.owner.map_or_else(
                    || paragraph.style(),
                    |owner| &paragraph.boxes()[owner].style,
                );
                for fill in decoration.fills() {
                    if let Some(rect) = crate::paint::shape_bounds(&fill.shape) {
                        self.anchors.ink(
                            crate::layout::anchors::ink_key(style),
                            self.page,
                            Rect {
                                x: rect.x + ink_origin[0],
                                y: rect.y + ink_origin[1],
                                ..rect
                            },
                            part.layer.clip,
                        );
                    }
                }
            }
            self.emit_line(
                TextLine {
                    paragraph: index,
                    text: line.text.clone(),
                    x: origin[0],
                    baseline: origin[1],
                    width: line.width,
                    height: line.height,
                    runs: part.runs,
                    font_size: style.size,
                    color: style.color,
                },
                line.ink_bounds,
                &part.decorations,
                &part.layer,
                &decorates,
            );
        }
    }

    pub(super) fn row_text(
        &mut self,
        mut text: crate::layout::measure::RowText<'a>,
        row_top: f32,
    ) -> Result<(), LayoutError> {
        let glyphs = text
            .line
            .runs
            .iter()
            .map(|run| run.glyphs().len())
            .sum::<usize>();
        self.measurer.work.spend(
            glyphs
                .saturating_add(text.line.text.len())
                .saturating_add(text.line.boxes.len())
                .saturating_add(text.line.links.len())
                .saturating_add(text.line.decorations.len())
                .saturating_add(text.source.semantic_events().len())
                .max(1),
        )?;
        self.y = row_top + text.baseline - text.line.ascent;
        let mut consumed_events = 0;
        self.line_events(text.source, &text.line, &mut consumed_events);
        let baseline = self.top + row_top + text.baseline + self.offset[1];
        self.record_baselines(self.page, [Some(baseline); 2]);
        self.record_overflow(Rect {
            x: text.x + self.offset[0],
            y: self.top + self.y + self.offset[1],
            width: text.line.width,
            height: text.line.height,
        });
        let containing = InlineContaining::of(self.containing_geometry());
        let mut layers = InlineLayers::new(text.source, containing);
        let line_layers = layers.line(&text.line);
        self.inline_content(&text.line, containing.width, text.x, &mut [], line_layers)?;
        let index = self.measurer.sources.paragraph(text.source);
        self.emit_paragraph_line(
            index,
            text.source.style(),
            &mut text.line,
            [text.x + self.offset[0], baseline],
            line_layers,
        );
        self.finish_inline_backgrounds(&layers);
        Ok(())
    }

    fn finish_inline_backgrounds(&mut self, layers: &InlineLayers<'a>) {
        for (inline_box, fragments) in layers.painted() {
            if !inline_box.style.clone_decoration {
                assemble_background(
                    fragments,
                    &mut self.foreground,
                    true,
                    inline_box.style.used_rtl(),
                );
            }
        }
    }
}