ironpress 1.4.4

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, LaTeX math, tables, images, custom fonts, and streaming output. No browser, no system dependencies.
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
use crate::parser::css::{AncestorInfo, CssRule, PseudoElement, SelectorContext};
use crate::parser::dom::{DomNode, ElementNode, HtmlTag};
use crate::parser::ttf::TtfFont;
use crate::style::computed::{
    ComputedStyle, Display, compute_pseudo_element_style_with_font_metrics,
    compute_style_with_context_with_font_metrics,
};
use crate::style::font_metrics::FontMetrics;
use std::collections::HashMap;

use super::elements::{BoxModel, IntoLayoutNode, LayoutNode, TextBlock};
use super::engine::{
    CounterState, ElementSiblingPosition, TextRun, element_is_empty, element_sibling_list,
    forward_siblings,
};
use super::helpers::{append_pseudo_inline_run, build_pseudo_inline_run};
use super::text::{
    InlineTextSequence, TextWrapOptions, parent_line_strut, text_run_line_height_factor,
    used_font_size, wrap_text_runs,
};

mod flow;
pub(crate) use flow::{IndependentFlowLayout, layout_mixed_flow_children};

/// Source-order selector position for one inline sibling sequence.
///
/// Every inline analysis and layout pass advances this same cursor. This keeps
/// `:first-of-type`, `:last-of-type`, sibling combinators, `:empty`, and the
/// ancestor metadata used by descendant selectors identical at every depth.
pub(crate) struct InlineSiblingCursor {
    siblings: Vec<(String, Vec<String>)>,
    next_index: usize,
}

impl InlineSiblingCursor {
    pub(crate) fn starting_at(nodes: &[DomNode], next_index: usize) -> Self {
        let siblings = element_sibling_list(nodes);
        Self {
            next_index: next_index.min(siblings.len()),
            siblings,
        }
    }

    pub(crate) fn next_context<'a>(
        &mut self,
        element: &'a ElementNode,
        ancestors: &[AncestorInfo<'a>],
    ) -> SelectorContext<'a> {
        let child_index = self.next_index;
        self.next_index = self.next_index.saturating_add(1);
        SelectorContext {
            ancestors: ancestors.to_vec(),
            child_index,
            sibling_count: self.siblings.len(),
            preceding_siblings: self
                .siblings
                .get(..child_index)
                .unwrap_or_default()
                .to_vec(),
            following_siblings: forward_siblings(&self.siblings, child_index).to_vec(),
            is_empty: element_is_empty(element),
        }
    }
}

/// One generated box attached to an originating element.
///
/// Keeping the originating element and computed pseudo style inseparable lets
/// every formatting context resolve attributes, counters, and quotes through
/// the same representation. The box may later participate as inline content or
/// be wrapped by table/block fixup; it is not a synthetic DOM element.
#[derive(Clone, Copy)]
pub(crate) struct GeneratedBox<'a> {
    originating_element: &'a ElementNode,
    style: &'a ComputedStyle,
}

impl<'a> GeneratedBox<'a> {
    pub(crate) const fn new(
        originating_element: &'a ElementNode,
        style: &'a ComputedStyle,
    ) -> Self {
        Self {
            originating_element,
            style,
        }
    }

    pub(crate) const fn style(self) -> &'a ComputedStyle {
        self.style
    }

    pub(crate) const fn originating_element(self) -> &'a ElementNode {
        self.originating_element
    }

    pub(crate) fn append_inline(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        append_pseudo_inline_run(
            runs,
            Some(self.style),
            self.originating_element,
            fonts,
            counter_state,
        );
    }

    /// Append the generated value as measurable text regardless of its outer
    /// display role. Table intrinsic sizing measures the contents of block and
    /// inline generated boxes alike; table fixup decides their final box role.
    pub(crate) fn append_measurement_run(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        runs.push(build_pseudo_inline_run(
            self.style,
            self.originating_element,
            fonts,
            counter_state,
        ));
    }
}

/// Owned computed styles for an originating element's generated children.
///
/// Style resolution produces this value once. Formatting contexts then borrow
/// [`GeneratedInlineContent`] from it instead of inventing their own
/// before/after pairs and silently diverging in generated-content behavior.
#[derive(Debug, Clone, Default)]
pub(crate) struct GeneratedContentStyles {
    before: Option<ComputedStyle>,
    after: Option<ComputedStyle>,
}

impl GeneratedContentStyles {
    pub(crate) fn resolve(
        element: &ElementNode,
        principal: &ComputedStyle,
        rules: &[CssRule],
        selector: &SelectorContext<'_>,
        fonts: &HashMap<String, TtfFont>,
    ) -> Self {
        let classes = element.class_list();
        let resolve = |pseudo| {
            compute_pseudo_element_style_with_font_metrics(
                principal,
                rules,
                element.tag_name(),
                &classes,
                element.id(),
                &element.attributes,
                selector,
                pseudo,
                FontMetrics::new(fonts),
            )
        };
        Self {
            before: resolve(PseudoElement::Before),
            after: resolve(PseudoElement::After),
        }
    }

    pub(crate) const fn boxes<'a>(
        &'a self,
        element: &'a ElementNode,
    ) -> GeneratedInlineContent<'a> {
        GeneratedInlineContent::new(element, self.before.as_ref(), self.after.as_ref())
    }

    pub(crate) const fn before(&self) -> Option<&ComputedStyle> {
        self.before.as_ref()
    }

    pub(crate) const fn after(&self) -> Option<&ComputedStyle> {
        self.after.as_ref()
    }

    /// Whether either generated child needs box-tree layout instead of the
    /// inline-run path.
    pub(crate) fn requires_box_layout(&self) -> bool {
        [self.before(), self.after()]
            .into_iter()
            .flatten()
            .any(|style| {
                style.position.is_absolute()
                    || matches!(style.display, Display::Block | Display::ListItem)
            })
    }
}

/// Pseudo styles attached to one principal box.
///
/// The generated children and typographic first-fragment styles are resolved
/// together at the element boundary. Downstream formatting contexts borrow
/// this proof instead of independently re-running part of the pseudo cascade.
#[derive(Debug, Clone, Default)]
pub(crate) struct PrincipalPseudoStyles {
    generated: GeneratedContentStyles,
    first_line: Option<ComputedStyle>,
    first_letter: Option<ComputedStyle>,
}

impl PrincipalPseudoStyles {
    pub(crate) fn resolve(
        element: &ElementNode,
        principal: &ComputedStyle,
        rules: &[CssRule],
        selector: &SelectorContext<'_>,
        fonts: &HashMap<String, TtfFont>,
    ) -> Self {
        let classes = element.class_list();
        let resolve = |pseudo| {
            compute_pseudo_element_style_with_font_metrics(
                principal,
                rules,
                element.tag_name(),
                &classes,
                element.id(),
                &element.attributes,
                selector,
                pseudo,
                FontMetrics::new(fonts),
            )
        };
        Self {
            generated: GeneratedContentStyles::resolve(element, principal, rules, selector, fonts),
            first_line: resolve(PseudoElement::FirstLine),
            first_letter: resolve(PseudoElement::FirstLetter),
        }
    }

    pub(crate) const fn generated(&self) -> &GeneratedContentStyles {
        &self.generated
    }

    pub(crate) const fn first_line(&self) -> Option<&ComputedStyle> {
        self.first_line.as_ref()
    }

    pub(crate) const fn first_letter(&self) -> Option<&ComputedStyle> {
        self.first_letter.as_ref()
    }
}

/// Generated content at the two boundaries of an originating element's
/// principal box.
///
/// CSS generated content participates in the same formatting sequence as the
/// originating element's real children. Keeping both boundaries together
/// prevents a layout path from treating `::before` or `::after` as unrelated
/// sibling blocks merely because the sequence also contains an atomic inline.
#[derive(Clone, Copy, Default)]
pub(crate) struct GeneratedInlineContent<'a> {
    before: Option<GeneratedBox<'a>>,
    after: Option<GeneratedBox<'a>>,
}

impl<'a> GeneratedInlineContent<'a> {
    pub(crate) const fn new(
        originating_element: &'a ElementNode,
        before: Option<&'a ComputedStyle>,
        after: Option<&'a ComputedStyle>,
    ) -> Self {
        Self {
            before: match before {
                Some(style) => Some(GeneratedBox::new(originating_element, style)),
                None => None,
            },
            after: match after {
                Some(style) => Some(GeneratedBox::new(originating_element, style)),
                None => None,
            },
        }
    }

    pub(crate) const fn from_boxes(
        before: Option<GeneratedBox<'a>>,
        after: Option<GeneratedBox<'a>>,
    ) -> Self {
        Self { before, after }
    }

    pub(crate) const fn is_empty(self) -> bool {
        self.before.is_none() && self.after.is_none()
    }

    pub(crate) const fn before(self) -> Option<GeneratedBox<'a>> {
        self.before
    }

    pub(crate) const fn after(self) -> Option<GeneratedBox<'a>> {
        self.after
    }

    pub(crate) fn append_before(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(before) = self.before {
            before.append_inline(runs, fonts, counter_state);
        }
    }

    pub(crate) fn append_after(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(after) = self.after {
            after.append_inline(runs, fonts, counter_state);
        }
    }

    pub(crate) fn append_before_measurement(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(before) = self.before {
            before.append_measurement_run(runs, fonts, counter_state);
        }
    }

    pub(crate) fn append_after_measurement(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(after) = self.after {
            after.append_measurement_run(runs, fonts, counter_state);
        }
    }
}

/// The complete source-order content of one inline formatting context.
///
/// DOM children alone are insufficient: generated content forms the first and
/// last inline items of the originating element. This value is shared by the
/// analysis and execution passes so neither pass can silently omit a boundary.
#[derive(Clone, Copy)]
pub(crate) struct InlineContentSequence<'a> {
    source_nodes: &'a [DomNode],
    start: usize,
    end: usize,
    generated: Option<GeneratedInlineContent<'a>>,
}

impl<'a> InlineContentSequence<'a> {
    pub(crate) const fn new(nodes: &'a [DomNode]) -> Self {
        Self {
            source_nodes: nodes,
            start: 0,
            end: nodes.len(),
            generated: None,
        }
    }

    pub(crate) fn segment(nodes: &'a [DomNode], start: usize, end: usize) -> Self {
        let start = start.min(nodes.len());
        let end = end.clamp(start, nodes.len());
        Self {
            source_nodes: nodes,
            start,
            end,
            generated: None,
        }
    }

    /// Select one item while retaining the complete source sibling list.
    ///
    /// Selector matching depends on siblings outside the selected range, so a
    /// one-item sequence must never be rebuilt from a one-node slice.
    pub(crate) fn item(self, offset: usize) -> Self {
        let start = self.start.saturating_add(offset).min(self.end);
        Self::segment(
            self.source_nodes,
            start,
            start.saturating_add(1).min(self.end),
        )
    }

    pub(crate) const fn with_generated(
        nodes: &'a [DomNode],
        generated: GeneratedInlineContent<'a>,
    ) -> Self {
        Self {
            source_nodes: nodes,
            start: 0,
            end: nodes.len(),
            generated: Some(generated),
        }
    }

    pub(crate) fn nodes(self) -> &'a [DomNode] {
        self.source_nodes
            .get(self.start..self.end)
            .unwrap_or_default()
    }

    pub(crate) const fn source_nodes(self) -> &'a [DomNode] {
        self.source_nodes
    }

    pub(crate) fn starting_element_index(self) -> usize {
        self.source_nodes
            .get(..self.start)
            .unwrap_or_default()
            .iter()
            .filter(|node| matches!(node, DomNode::Element(_)))
            .count()
    }

    pub(crate) const fn start(self) -> usize {
        self.start
    }

    pub(crate) const fn end(self) -> usize {
        self.end
    }

    pub(crate) fn append_before(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(generated) = self.generated {
            generated.append_before(runs, fonts, counter_state);
        }
    }

    pub(crate) fn append_after(
        self,
        runs: &mut Vec<TextRun>,
        fonts: &HashMap<String, TtfFont>,
        counter_state: &mut CounterState,
    ) {
        if let Some(generated) = self.generated {
            generated.append_after(runs, fonts, counter_state);
        }
    }
}

/// Builds anonymous block boxes for contiguous inline fragments separated by
/// block-level siblings.
///
/// The anonymous box owns only the line formatting. The originating block owns
/// its definite size, margins, padding, border, paint and positioning, so none
/// of those properties may leak into this child.
pub(crate) struct AnonymousInlineFormattingContext<'a> {
    parent_style: &'a ComputedStyle,
    available_width: f32,
    fonts: &'a HashMap<String, TtfFont>,
}

impl<'a> AnonymousInlineFormattingContext<'a> {
    pub(crate) const fn new(
        parent_style: &'a ComputedStyle,
        available_width: f32,
        fonts: &'a HashMap<String, TtfFont>,
    ) -> Self {
        Self {
            parent_style,
            available_width,
            fonts,
        }
    }

    pub(crate) fn layout_runs(&self, mut runs: Vec<TextRun>) -> Option<LayoutNode> {
        if runs.is_empty() {
            return None;
        }
        runs.as_mut_slice()
            .resolve_unclaimed_boundaries(super::elements::TextSpacing::from_style(
                self.parent_style,
            ));
        let lines = wrap_text_runs(
            runs,
            TextWrapOptions::new(
                self.available_width,
                used_font_size(self.parent_style, self.fonts),
                text_run_line_height_factor(self.parent_style, self.fonts),
                self.parent_style.overflow_wrap,
            )
            .with_white_space(self.parent_style.white_space)
            .with_parent_strut(parent_line_strut(self.parent_style, self.fonts))
            .with_rtl(self.parent_style.direction_rtl)
            .with_bidi_override(self.parent_style.bidi_override)
            .with_bidi_plaintext(self.parent_style.bidi_plaintext),
            self.fonts,
        );
        if lines.is_empty() {
            return None;
        }

        let mut block = TextBlock::from_style(lines, self.parent_style, BoxModel::default());
        block.paint = Default::default();
        block.flow = Default::default();
        block.positioning = Default::default();
        block.fragmentation = Default::default();
        block.clipping = Default::default();
        Some(block.boxed())
    }
}

/// How an element participates in its parent's inline formatting context.
///
/// This is the shared contract between the analysis pass that selects the
/// mixed-inline layout path and the layout pass that builds its atomic cells.
/// Keeping the decision here prevents replaced elements from being accepted as
/// inline content by one pass and then silently discarded by text collection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum InlineFormattingRole {
    Hidden,
    Text,
    Atomic(AtomicInlineKind),
    OutOfFlow,
    Outside,
}

/// Which parent inline-layout path owns the paint for atomic children.
///
/// Child traversal must not independently lay out a box already embedded in a
/// text run or mixed inline row. Keeping that ownership explicit avoids
/// tag-specific duplicate-suppression rules.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) enum AtomicInlineEmission {
    #[default]
    Independent,
    InlineBlockRuns,
    MixedRow,
}

impl AtomicInlineEmission {
    const fn owns(self, role: InlineFormattingRole) -> bool {
        match self {
            Self::Independent => false,
            Self::InlineBlockRuns => matches!(
                role,
                InlineFormattingRole::Atomic(AtomicInlineKind::InlineBlock)
            ),
            Self::MixedRow => matches!(role, InlineFormattingRole::Atomic(_)),
        }
    }
}

/// Computed participation of one element child in an inline formatting context.
pub(crate) struct InlineFormattingChild {
    pub(crate) style: ComputedStyle,
    pub(crate) role: InlineFormattingRole,
    source: ElementSiblingPosition,
}

impl InlineFormattingChild {
    pub(crate) const fn source(&self) -> &ElementSiblingPosition {
        &self.source
    }
}

/// One authoritative style/classification pass for an inline sibling sequence.
/// Indices are element-sibling indices, never raw DOM-node indices.
pub(crate) struct InlineFormattingChildren(Vec<InlineFormattingChild>);

impl InlineFormattingChildren {
    pub(crate) fn get(&self, element_index: usize) -> Option<&InlineFormattingChild> {
        self.0.get(element_index)
    }

    pub(crate) fn iter(&self) -> impl Iterator<Item = &InlineFormattingChild> {
        self.0.iter()
    }

    pub(crate) fn has_out_of_flow(&self) -> bool {
        self.0
            .iter()
            .any(|child| child.role == InlineFormattingRole::OutOfFlow)
    }

    pub(crate) fn requires_independent_layout(&self, element_index: usize) -> bool {
        self.0.get(element_index).is_some_and(|child| {
            matches!(
                child.role,
                InlineFormattingRole::OutOfFlow | InlineFormattingRole::Outside
            )
        })
    }

    pub(crate) fn is_grouped_atomic(&self, element_index: usize) -> bool {
        self.0.get(element_index).is_some_and(|child| {
            matches!(
                child.role,
                InlineFormattingRole::Atomic(
                    AtomicInlineKind::InlineBlock
                        | AtomicInlineKind::InlineFlex
                        | AtomicInlineKind::InlineGrid
                        | AtomicInlineKind::InlineTable
                )
            )
        })
    }

    pub(crate) fn atomic_is_emitted(
        &self,
        element_index: usize,
        emission: AtomicInlineEmission,
    ) -> bool {
        self.0
            .get(element_index)
            .is_some_and(|child| emission.owns(child.role))
    }

    pub(crate) fn is_inline_text(&self, element_index: usize) -> bool {
        self.0.get(element_index).map(|child| child.role) == Some(InlineFormattingRole::Text)
    }
}

/// The layout mechanism required by an atomic inline-level element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AtomicInlineKind {
    ReplacedImage,
    InlineBlock,
    InlineFlex,
    InlineGrid,
    InlineTable,
}

impl AtomicInlineKind {
    const fn requires_environment_aware_layout(self) -> bool {
        !matches!(self, Self::InlineBlock)
    }
}

impl InlineFormattingRole {
    pub(crate) fn of(element: &ElementNode, style: &ComputedStyle) -> Self {
        if style.display == Display::None {
            return Self::Hidden;
        }
        if style.position.is_absolute() {
            return Self::OutOfFlow;
        }
        if element.tag == HtmlTag::Svg {
            return Self::Outside;
        }

        let inline_level = matches!(
            style.display,
            Display::Inline
                | Display::InlineBlock
                | Display::InlineFlex
                | Display::InlineGrid
                | Display::InlineTable
        );
        if element.tag == HtmlTag::Img && inline_level {
            return Self::Atomic(AtomicInlineKind::ReplacedImage);
        }

        match style.display {
            Display::InlineBlock => Self::Atomic(AtomicInlineKind::InlineBlock),
            Display::InlineFlex => Self::Atomic(AtomicInlineKind::InlineFlex),
            Display::InlineGrid => Self::Atomic(AtomicInlineKind::InlineGrid),
            Display::InlineTable => Self::Atomic(AtomicInlineKind::InlineTable),
            Display::Inline => Self::Text,
            _ => Self::Outside,
        }
    }
}

/// Immutable style context used to decide whether a sibling sequence needs the
/// environment-aware atomic-inline layout path.
pub(crate) struct InlineFormattingContext<'a> {
    parent_style: &'a ComputedStyle,
    rules: &'a [CssRule],
    ancestors: &'a [AncestorInfo<'a>],
    font_metrics: FontMetrics<'a>,
}

impl<'a> InlineFormattingContext<'a> {
    pub(crate) const fn new(
        parent_style: &'a ComputedStyle,
        rules: &'a [CssRule],
        ancestors: &'a [AncestorInfo<'a>],
        font_metrics: FontMetrics<'a>,
    ) -> Self {
        Self {
            parent_style,
            rules,
            ancestors,
            font_metrics,
        }
    }

    pub(crate) fn children(&self, sequence: InlineContentSequence<'_>) -> InlineFormattingChildren {
        let mut siblings = InlineSiblingCursor::starting_at(
            sequence.source_nodes(),
            sequence.starting_element_index(),
        );
        let roles = sequence
            .nodes()
            .iter()
            .filter_map(|node| {
                let DomNode::Element(element) = node else {
                    return None;
                };
                let selector_context = siblings.next_context(element, self.ancestors);
                let classes = element.class_list();
                let style = compute_style_with_context_with_font_metrics(
                    element.tag,
                    element.style_attr(),
                    self.parent_style,
                    self.rules,
                    element.tag_name(),
                    &classes,
                    element.id(),
                    &element.attributes,
                    &selector_context,
                    self.font_metrics,
                );
                Some(InlineFormattingChild {
                    role: InlineFormattingRole::of(element, &style),
                    style,
                    source: ElementSiblingPosition::from_selector_context(&selector_context),
                })
            })
            .collect();
        InlineFormattingChildren(roles)
    }

    /// Returns true only when every visible child can share one inline
    /// formatting context and at least one child requires an atomic cell.
    pub(crate) fn requires_atomic_layout(&self, sequence: InlineContentSequence<'_>) -> bool {
        let nodes = sequence.nodes();
        let mut siblings = InlineSiblingCursor::starting_at(
            sequence.source_nodes(),
            sequence.starting_element_index(),
        );
        let mut saw_atomic = false;

        for node in nodes {
            let DomNode::Element(element) = node else {
                continue;
            };
            let classes = element.class_list();
            let selector_context = siblings.next_context(element, self.ancestors);
            let style = compute_style_with_context_with_font_metrics(
                element.tag,
                element.style_attr(),
                self.parent_style,
                self.rules,
                element.tag_name(),
                &classes,
                element.id(),
                &element.attributes,
                &selector_context,
                self.font_metrics,
            );

            match InlineFormattingRole::of(element, &style) {
                InlineFormattingRole::Atomic(kind) => {
                    saw_atomic |= kind.requires_environment_aware_layout();
                }
                // Out-of-flow inline children are laid out independently, but
                // their static source position does not split the surrounding
                // inline formatting context.
                InlineFormattingRole::OutOfFlow
                | InlineFormattingRole::Hidden
                | InlineFormattingRole::Text => {}
                InlineFormattingRole::Outside => return false,
            }
        }

        saw_atomic
    }

    /// Maximal source-order ranges that remain inside one inline formatting
    /// context and contain an environment-aware atomic inline.
    ///
    /// Block/table participants split the ranges but do not force later inline
    /// content onto the legacy per-element path. Each returned sequence retains
    /// the complete sibling list for selector matching.
    pub(crate) fn atomic_layout_segments<'b>(
        &self,
        sequence: InlineContentSequence<'b>,
    ) -> Vec<InlineContentSequence<'b>> {
        let source = sequence.source_nodes();
        let mut siblings =
            InlineSiblingCursor::starting_at(source, sequence.starting_element_index());
        let mut segment_start = sequence.start();
        let mut segment_has_atomic = false;
        let mut segments = Vec::new();

        for node_index in sequence.start()..sequence.end() {
            let Some(DomNode::Element(element)) = source.get(node_index) else {
                continue;
            };
            let classes = element.class_list();
            let selector_context = siblings.next_context(element, self.ancestors);
            let style = compute_style_with_context_with_font_metrics(
                element.tag,
                element.style_attr(),
                self.parent_style,
                self.rules,
                element.tag_name(),
                &classes,
                element.id(),
                &element.attributes,
                &selector_context,
                self.font_metrics,
            );

            match InlineFormattingRole::of(element, &style) {
                InlineFormattingRole::Atomic(kind) => {
                    segment_has_atomic |= kind.requires_environment_aware_layout();
                }
                InlineFormattingRole::OutOfFlow => {}
                InlineFormattingRole::Outside => {
                    if segment_has_atomic && segment_start < node_index {
                        segments.push(InlineContentSequence::segment(
                            source,
                            segment_start,
                            node_index,
                        ));
                    }
                    segment_start = node_index + 1;
                    segment_has_atomic = false;
                }
                InlineFormattingRole::Hidden | InlineFormattingRole::Text => {}
            }
        }

        if segment_has_atomic && segment_start < sequence.end() {
            segments.push(InlineContentSequence::segment(
                source,
                segment_start,
                sequence.end(),
            ));
        }
        segments
    }
}