Skip to main content

layout/flow/inline/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! # Inline Formatting Context Layout
6//!
7//! Inline layout is divided into three phases:
8//!
9//! 1. Box Tree Construction
10//! 2. Box to Line Layout
11//! 3. Line to Fragment Layout
12//!
13//! The first phase happens during normal box tree constrution, while the second two phases happen
14//! during fragment tree construction (sometimes called just "layout").
15//!
16//! ## Box Tree Construction
17//!
18//! During box tree construction, DOM elements are transformed into a box tree. This phase collects
19//! all of the inline boxes, text, atomic inline elements (boxes with `display: inline-block` or
20//! `display: inline-table` as well as things like images and canvas), absolutely positioned blocks,
21//! and floated blocks.
22//!
23//! During the last part of this phase, whitespace is collapsed and text is segmented into
24//! [`TextRun`]s based on script, chosen font, and line breaking opportunities. In addition, default
25//! fonts are selected for every inline box. Each segment of text is shaped using HarfBuzz and
26//! turned into a series of glyphs, which all have a size and a position relative to the origin of
27//! the [`TextRun`] (calculated in later phases).
28//!
29//! The code for this phase is mainly in `construct.rs`, but text handling can also be found in
30//! `text_runs.rs.`
31//!
32//! ## Box to Line Layout
33//!
34//! During the first phase of fragment tree construction, box tree items are laid out into
35//! [`LineItem`]s and fragmented based on line boundaries. This is where line breaking happens. This
36//! part of layout fragments boxes and their contents across multiple lines while positioning floats
37//! and making sure non-floated contents flow around them. In addition, all atomic elements are laid
38//! out, which may descend into their respective trees and create fragments. Finally, absolutely
39//! positioned content is collected in order to later hoist it to the containing block for
40//! absolutes.
41//!
42//! Note that during this phase, layout does not know the final block position of content. Only
43//! during line to fragment layout, are the final block positions calculated based on the line's
44//! final content and its vertical alignment. Instead, positions and line heights are calculated
45//! relative to the line's final baseline which will be determined in the final phase.
46//!
47//! [`LineItem`]s represent a particular set of content on a line. Currently this is represented by
48//! a linear series of items that describe the line's hierarchy of inline boxes and content. The
49//! item types are:
50//!
51//!  - [`LineItem::InlineStartBoxPaddingBorderMargin`]
52//!  - [`LineItem::InlineEndBoxPaddingBorderMargin`]
53//!  - [`LineItem::TextRun`]
54//!  - [`LineItem::Atomic`]
55//!  - [`LineItem::AbsolutelyPositioned`]
56//!  - [`LineItem::Float`]
57//!
58//! The code for this can be found by looking for methods of the form `layout_into_line_item()`.
59//!
60//! ## Line to Fragment Layout
61//!
62//! During the second phase of fragment tree construction, the final block position of [`LineItem`]s
63//! is calculated and they are converted into [`Fragment`]s. After layout, the [`LineItem`]s are
64//! discarded and the new fragments are incorporated into the fragment tree. The final static
65//! position of absolutely positioned content is calculated and it is hoisted to its containing
66//! block via [`PositioningContext`].
67//!
68//! The code for this phase, can mainly be found in `line.rs`.
69//!
70
71pub mod construct;
72pub mod inline_box;
73pub mod line;
74mod line_breaker;
75pub mod text_run;
76
77use std::cell::{OnceCell, RefCell};
78use std::mem;
79use std::rc::Rc;
80use std::sync::{Arc, OnceLock};
81
82use app_units::{Au, MAX_AU};
83use atomic_refcell::AtomicRef;
84use bitflags::bitflags;
85use construct::InlineFormattingContextBuilder;
86use fonts::{FontMetrics, FontRef, ShapedTextSlice};
87use icu_locid::LanguageIdentifier;
88use icu_locid::subtags::{Language, language};
89use icu_properties::{self, LineBreak as ICULineBreak};
90use icu_segmenter::{LineBreakOptions, LineBreakStrictness, LineBreakWordOption};
91use inline_box::{InlineBox, InlineBoxContainerState, InlineBoxIdentifier, InlineBoxes};
92use layout_api::{LayoutNode, SharedSelection};
93use line::{
94    AbsolutelyPositionedLineItem, AtomicLineItem, FloatLineItem, LineItem, LineItemLayout,
95    TextRunLineItem,
96};
97use line_breaker::LineBreaker;
98use malloc_size_of_derive::MallocSizeOf;
99use script::layout_dom::ServoLayoutNode;
100use servo_arc::Arc as ServoArc;
101use style::Zero;
102use style::computed_values::line_break::T as LineBreak;
103use style::computed_values::text_wrap_mode::T as TextWrapMode;
104use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
105use style::computed_values::word_break::T as WordBreak;
106use style::context::{QuirksMode, SharedStyleContext};
107use style::properties::ComputedValues;
108use style::properties::style_structs::InheritedText;
109use style::values::computed::BaselineShift;
110use style::values::generics::box_::BaselineShiftKeyword;
111use style::values::generics::font::LineHeight;
112use style::values::specified::box_::BaselineSource;
113use style::values::specified::text::TextAlignKeyword;
114use style::values::specified::{AlignmentBaseline, TextAlignLast, TextJustify};
115use text_run::{TextRun, get_font_for_first_font_for_style};
116use unicode_bidi::{BidiInfo, Level};
117
118use super::float::{Clear, PlacementAmongFloats};
119use super::{IndependentFloatOrAtomicLayoutResult, IndependentFormattingContextLayoutResult};
120use crate::cell::{ArcRefCell, WeakRefCell};
121use crate::context::LayoutContext;
122use crate::dom::WeakLayoutBox;
123use crate::dom_traversal::NodeAndStyleInfo;
124use crate::flow::float::{FloatBox, SequentialLayoutState};
125use crate::flow::inline::line::TextRunOffsets;
126use crate::flow::inline::text_run::{FontAndScriptInfo, TextRunItem, TextRunSegment};
127use crate::flow::{
128    BlockLevelBox, CollapsibleWithParentStartMargin, FloatSide, PlacementState,
129    compute_inline_content_sizes_for_block_level_boxes, layout_block_level_child,
130};
131use crate::formatting_contexts::{Baselines, IndependentFormattingContext};
132use crate::fragment_tree::{
133    BaseFragmentInfo, BoxFragment, CollapsedMargin, Fragment, FragmentFlags, PositioningFragment,
134};
135use crate::geom::{LogicalRect, LogicalSides1D, LogicalVec2, ToLogical};
136use crate::layout_box_base::LayoutBoxBase;
137use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
138use crate::sizing::{ComputeInlineContentSizes, ContentSizes, InlineContentSizesResult};
139use crate::style_ext::{ComputedValuesExt, PaddingBorderMargin};
140use crate::{ConstraintSpace, ContainingBlock, IndefiniteContainingBlock, SharedStyle};
141
142// From gfxFontConstants.h in Firefox.
143static FONT_SUBSCRIPT_OFFSET_RATIO: f32 = 0.20;
144static FONT_SUPERSCRIPT_OFFSET_RATIO: f32 = 0.34;
145
146#[derive(Debug, MallocSizeOf)]
147pub(crate) struct InlineFormattingContext {
148    /// All [`InlineItem`]s in this [`InlineFormattingContext`] stored in a flat array.
149    /// [`InlineItem::StartInlineBox`] and [`InlineItem::EndInlineBox`] allow representing
150    /// the tree of inline boxes within the formatting context, but a flat array allows
151    /// easy iteration through all inline items.
152    inline_items: Vec<InlineItem>,
153
154    /// The tree of inline boxes in this [`InlineFormattingContext`]. These are stored in
155    /// a flat array with each being given a [`InlineBoxIdentifier`].
156    inline_boxes: InlineBoxes,
157
158    /// The text content of this inline formatting context.
159    text_content: String,
160
161    /// The [`SharedInlineStyles`] for the root of this [`InlineFormattingContext`] that are used to
162    /// share styles with all [`TextRun`] children.
163    shared_inline_styles: SharedInlineStyles,
164
165    /// The default font that is used for the root of this [`InlineFormattingContext`]. This is the
166    /// font used when the font fallback code path is not taken. It may be `None` if no default
167    /// font was found (this typically means that no characters can be rendered).
168    default_font: Option<FontRef>,
169
170    /// Whether this IFC contains the 1st formatted line of an element:
171    /// <https://www.w3.org/TR/css-pseudo-4/#first-formatted-line>.
172    has_first_formatted_line: bool,
173
174    /// Whether or not this [`InlineFormattingContext`] contains floats.
175    pub(super) contains_floats: bool,
176
177    /// Whether or not this is an [`InlineFormattingContext`] for a single line text input's inner
178    /// text container.
179    is_single_line_text_input: bool,
180
181    /// Whether or not this is an [`InlineFormattingContext`] has right-to-left content, which
182    /// will require reordering during layout.
183    has_right_to_left_content: bool,
184
185    /// If this [`InlineFormattingContext`] has a selection shared with its originating
186    /// node in the DOM, this will not be `None`.
187    #[ignore_malloc_size_of = "This is stored primarily in the DOM"]
188    shared_selection: Option<SharedSelection>,
189
190    /// The cached tab stop inline advance used when finding final tab stops for preserved tabs.
191    tab_stop_advance: OnceLock<Au>,
192}
193
194/// [`TextRun`] and `TextFragment`s need a handle on their parent inline box (or inline
195/// formatting context root)'s style. In order to implement incremental layout, these are
196/// wrapped in [`SharedStyle`]. This allows updating the parent box tree element without
197/// updating every single descendant box tree node and fragment.
198#[derive(Clone, Debug, MallocSizeOf)]
199pub(crate) struct SharedInlineStyles {
200    pub style: SharedStyle,
201    pub selected: SharedStyle,
202}
203
204impl SharedInlineStyles {
205    pub(crate) fn ptr_eq(&self, other: &Self) -> bool {
206        self.style.ptr_eq(&other.style) && self.selected.ptr_eq(&other.selected)
207    }
208
209    pub(crate) fn from_info_and_context(info: &NodeAndStyleInfo, context: &LayoutContext) -> Self {
210        Self {
211            style: SharedStyle::new(info.style.clone()),
212            selected: SharedStyle::new(info.node.selected_style(&context.style_context)),
213        }
214    }
215}
216
217impl BlockLevelBox {
218    fn layout_into_line_items(&self, layout: &mut InlineFormattingContextLayout) {
219        layout.process_soft_wrap_opportunity();
220        layout.commit_current_segment_to_line();
221        layout.process_line_break(true);
222        layout.current_line.for_block_level = true;
223
224        let fragment = layout_block_level_child(
225            layout.layout_context,
226            layout.positioning_context,
227            self,
228            layout.sequential_layout_state.as_deref_mut(),
229            &mut layout.placement_state,
230            // Under discussion in <https://github.com/w3c/csswg-drafts/issues/13260>.
231            LogicalSides1D::new(false, false),
232            true, /* has_inline_parent */
233        );
234
235        let Fragment::Box(fragment) = fragment else {
236            unreachable!("The fragment should be a Fragment::Box()");
237        };
238
239        // If this Fragment's layout depends on the block size of the containing block,
240        // then the entire layout of the inline formatting context does as well.
241        layout.depends_on_block_constraints |= fragment.borrow().base.flags.contains(
242            FragmentFlags::SIZE_DEPENDS_ON_BLOCK_CONSTRAINTS_AND_CAN_BE_CHILD_OF_FLEX_ITEM,
243        );
244
245        layout.push_line_item_to_unbreakable_segment(LineItem::BlockLevel(
246            layout.current_inline_box_identifier(),
247            fragment,
248        ));
249
250        layout.commit_current_segment_to_line();
251        layout.process_line_break(true);
252        layout.current_line.for_block_level = false;
253    }
254}
255
256#[derive(Clone, Debug, MallocSizeOf)]
257pub(crate) enum InlineItem {
258    StartInlineBox(ArcRefCell<InlineBox>),
259    EndInlineBox,
260    TextRun(ArcRefCell<TextRun>),
261    OutOfFlowAbsolutelyPositionedBox(
262        ArcRefCell<AbsolutelyPositionedBox>,
263        usize, /* offset_in_text */
264    ),
265    OutOfFlowFloatBox(ArcRefCell<FloatBox>),
266    Atomic(
267        ArcRefCell<IndependentFormattingContext>,
268        usize, /* offset_in_text */
269        Level, /* bidi_level */
270    ),
271    BlockLevel(ArcRefCell<BlockLevelBox>),
272}
273
274impl InlineItem {
275    pub(crate) fn repair_style(
276        &self,
277        context: &SharedStyleContext,
278        node: &ServoLayoutNode,
279        new_style: &ServoArc<ComputedValues>,
280    ) {
281        match self {
282            InlineItem::StartInlineBox(inline_box) => {
283                inline_box
284                    .borrow_mut()
285                    .repair_style(context, node, new_style);
286            },
287            InlineItem::EndInlineBox => {},
288            // TextRun holds a handle the `InlineSharedStyles` which is updated when repairing inline box
289            // and `display: contents` styles.
290            InlineItem::TextRun(..) => {},
291            InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => positioned_box
292                .borrow_mut()
293                .context
294                .repair_style(context, node, new_style),
295            InlineItem::OutOfFlowFloatBox(float_box) => float_box
296                .borrow_mut()
297                .contents
298                .repair_style(context, node, new_style),
299            InlineItem::Atomic(atomic, ..) => {
300                atomic.borrow_mut().repair_style(context, node, new_style)
301            },
302            InlineItem::BlockLevel(block_level) => block_level
303                .borrow_mut()
304                .repair_style(context, node, new_style),
305        }
306    }
307
308    pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> T {
309        match self {
310            InlineItem::StartInlineBox(inline_box) => callback(&inline_box.borrow().base),
311            InlineItem::EndInlineBox | InlineItem::TextRun(..) => {
312                unreachable!("Should never have these kind of fragments attached to a DOM node")
313            },
314            InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => {
315                callback(&positioned_box.borrow().context.base)
316            },
317            InlineItem::OutOfFlowFloatBox(float_box) => callback(&float_box.borrow().contents.base),
318            InlineItem::Atomic(independent_formatting_context, ..) => {
319                callback(&independent_formatting_context.borrow().base)
320            },
321            InlineItem::BlockLevel(block_level) => block_level.borrow().with_base(callback),
322        }
323    }
324
325    pub(crate) fn with_base_mut<T>(&self, callback: impl FnOnce(&mut LayoutBoxBase) -> T) -> T {
326        match self {
327            InlineItem::StartInlineBox(inline_box) => callback(&mut inline_box.borrow_mut().base),
328            InlineItem::EndInlineBox | InlineItem::TextRun(..) => {
329                unreachable!("Should never have these kind of fragments attached to a DOM node")
330            },
331            InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => {
332                callback(&mut positioned_box.borrow_mut().context.base)
333            },
334            InlineItem::OutOfFlowFloatBox(float_box) => {
335                callback(&mut float_box.borrow_mut().contents.base)
336            },
337            InlineItem::Atomic(independent_formatting_context, ..) => {
338                callback(&mut independent_formatting_context.borrow_mut().base)
339            },
340            InlineItem::BlockLevel(block_level) => block_level.borrow_mut().with_base_mut(callback),
341        }
342    }
343
344    pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
345        match self {
346            Self::StartInlineBox(_) | InlineItem::EndInlineBox => {
347                // The parentage of inline items within an inline box is handled when the entire
348                // inline formatting context is attached to the tree.
349            },
350            Self::TextRun(_) => {
351                // Text runs can't have children, so no need to do anything.
352            },
353            Self::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => {
354                positioned_box.borrow().context.attached_to_tree(layout_box)
355            },
356            Self::OutOfFlowFloatBox(float_box) => {
357                float_box.borrow().contents.attached_to_tree(layout_box)
358            },
359            Self::Atomic(atomic, ..) => atomic.borrow().attached_to_tree(layout_box),
360            Self::BlockLevel(block_level) => block_level.borrow().attached_to_tree(layout_box),
361        }
362    }
363
364    pub(crate) fn downgrade(&self) -> WeakInlineItem {
365        match self {
366            Self::StartInlineBox(inline_box) => {
367                WeakInlineItem::StartInlineBox(inline_box.downgrade())
368            },
369            Self::EndInlineBox => WeakInlineItem::EndInlineBox,
370            Self::TextRun(text_run) => WeakInlineItem::TextRun(text_run.downgrade()),
371            Self::OutOfFlowAbsolutelyPositionedBox(positioned_box, offset_in_text) => {
372                WeakInlineItem::OutOfFlowAbsolutelyPositionedBox(
373                    positioned_box.downgrade(),
374                    *offset_in_text,
375                )
376            },
377            Self::OutOfFlowFloatBox(float_box) => {
378                WeakInlineItem::OutOfFlowFloatBox(float_box.downgrade())
379            },
380            Self::Atomic(atomic, offset_in_text, bidi_level) => {
381                WeakInlineItem::Atomic(atomic.downgrade(), *offset_in_text, *bidi_level)
382            },
383            Self::BlockLevel(block_level) => WeakInlineItem::BlockLevel(block_level.downgrade()),
384        }
385    }
386}
387
388#[derive(Clone, Debug, MallocSizeOf)]
389pub(crate) enum WeakInlineItem {
390    StartInlineBox(WeakRefCell<InlineBox>),
391    EndInlineBox,
392    TextRun(WeakRefCell<TextRun>),
393    OutOfFlowAbsolutelyPositionedBox(
394        WeakRefCell<AbsolutelyPositionedBox>,
395        usize, /* offset_in_text */
396    ),
397    OutOfFlowFloatBox(WeakRefCell<FloatBox>),
398    Atomic(
399        WeakRefCell<IndependentFormattingContext>,
400        usize, /* offset_in_text */
401        Level, /* bidi_level */
402    ),
403    BlockLevel(WeakRefCell<BlockLevelBox>),
404}
405
406impl WeakInlineItem {
407    pub(crate) fn upgrade(&self) -> Option<InlineItem> {
408        Some(match self {
409            Self::StartInlineBox(inline_box) => InlineItem::StartInlineBox(inline_box.upgrade()?),
410            Self::EndInlineBox => InlineItem::EndInlineBox,
411            Self::TextRun(text_run) => InlineItem::TextRun(text_run.upgrade()?),
412            Self::OutOfFlowAbsolutelyPositionedBox(positioned_box, offset_in_text) => {
413                InlineItem::OutOfFlowAbsolutelyPositionedBox(
414                    positioned_box.upgrade()?,
415                    *offset_in_text,
416                )
417            },
418            Self::OutOfFlowFloatBox(float_box) => {
419                InlineItem::OutOfFlowFloatBox(float_box.upgrade()?)
420            },
421            Self::Atomic(atomic, offset_in_text, bidi_level) => {
422                InlineItem::Atomic(atomic.upgrade()?, *offset_in_text, *bidi_level)
423            },
424            Self::BlockLevel(block_level) => InlineItem::BlockLevel(block_level.upgrade()?),
425        })
426    }
427}
428
429/// Information about the current line under construction for a particular
430/// [`InlineFormattingContextLayout`]. This tracks position and size information while
431/// [`LineItem`]s are collected and is used as input when those [`LineItem`]s are
432/// converted into [`Fragment`]s during the final phase of line layout. Note that this
433/// does not store the [`LineItem`]s themselves, as they are stored as part of the
434/// nesting state in the [`InlineFormattingContextLayout`].
435struct LineUnderConstruction {
436    /// The position where this line will start once it is laid out. This includes any
437    /// offset from `text-indent`.
438    start_position: LogicalVec2<Au>,
439
440    /// The current inline position in the line being laid out into [`LineItem`]s in this
441    /// [`InlineFormattingContext`] independent of the depth in the nesting level.
442    inline_position: Au,
443
444    /// The maximum block size of all boxes that ended and are in progress in this line.
445    /// This uses [`LineBlockSizes`] instead of a simple value, because the final block size
446    /// depends on vertical alignment.
447    max_block_size: LineBlockSizes,
448
449    /// Whether any active linebox has added a glyph or atomic element to this line, which
450    /// indicates that the next run that exceeds the line length can cause a line break.
451    has_content: bool,
452
453    /// Whether any active linebox has added some inline-axis padding, border or margin
454    /// to this line.
455    has_inline_pbm: bool,
456
457    /// Whether or not there are floats that did not fit on the current line. Before
458    /// the [`LineItem`]s of this line are laid out, these floats will need to be
459    /// placed directly below this line, but still as children of this line's Fragments.
460    has_floats_waiting_to_be_placed: bool,
461
462    /// A rectangular area (relative to the containing block / inline formatting
463    /// context boundaries) where we can fit the line box without overlapping floats.
464    /// Note that when this is not empty, its start corner takes precedence over
465    /// [`LineUnderConstruction::start_position`].
466    placement_among_floats: OnceCell<LogicalRect<Au>>,
467
468    /// The LineItems for the current line under construction that have already
469    /// been committed to this line.
470    line_items: Vec<LineItem>,
471
472    /// Whether the current line is for a block-level box.
473    for_block_level: bool,
474
475    /// The starting character offset of this line.
476    ///
477    /// This is used to generate empty `TextRunLineItem` to hold text carets on otherwise
478    /// empty lines.
479    ///
480    /// TODO: This is only guaranteed to be accurate for the first line or when the previous line
481    /// ended with a hard line break. Eventually this should be updated during content processing so
482    /// that text carets work outside of text inputs.
483    starting_character_offset: usize,
484}
485
486impl LineUnderConstruction {
487    fn new(start_position: LogicalVec2<Au>) -> Self {
488        Self {
489            inline_position: start_position.inline,
490            start_position,
491            max_block_size: LineBlockSizes::zero(),
492            has_content: false,
493            has_inline_pbm: false,
494            has_floats_waiting_to_be_placed: false,
495            placement_among_floats: OnceCell::new(),
496            line_items: Vec::new(),
497            for_block_level: false,
498            starting_character_offset: 0,
499        }
500    }
501
502    fn replace_placement_among_floats(&mut self, new_placement: LogicalRect<Au>) {
503        self.placement_among_floats.take();
504        let _ = self.placement_among_floats.set(new_placement);
505    }
506
507    /// Trim the trailing whitespace in this line and return the width of the whitespace trimmed.
508    fn trim_trailing_whitespace(&mut self) -> Au {
509        // From <https://www.w3.org/TR/css-text-3/#white-space-phase-2>:
510        // > 3. A sequence of collapsible spaces at the end of a line is removed,
511        // >    as well as any trailing U+1680   OGHAM SPACE MARK whose white-space
512        // >    property is normal, nowrap, or pre-line.
513        let mut whitespace_trimmed = Au::zero();
514        for item in self.line_items.iter_mut().rev() {
515            if !item.trim_whitespace_at_end(&mut whitespace_trimmed) {
516                break;
517            }
518        }
519
520        whitespace_trimmed
521    }
522
523    /// Count the number of justification opportunities in this line.
524    fn count_justification_opportunities(&self) -> usize {
525        self.line_items
526            .iter()
527            .filter_map(|item| match item {
528                LineItem::TextRun(_, text_run) => Some(
529                    text_run
530                        .text
531                        .iter()
532                        .map(|shaped_text_slice| shaped_text_slice.total_word_separators())
533                        .sum::<usize>(),
534                ),
535                _ => None,
536            })
537            .sum()
538    }
539
540    /// Whether this is a phantom line box.
541    /// <https://drafts.csswg.org/css-inline-3/#invisible-line-boxes>
542    fn is_phantom(&self) -> bool {
543        // Keep this logic in sync with `UnbreakableSegmentUnderConstruction::is_phantom()`.
544        !self.has_content && !self.has_inline_pbm
545    }
546}
547
548/// A block size relative to a line's final baseline. This is to track the size
549/// contribution of a particular element of a line above and below the baseline.
550/// These sizes can be combined with other baseline relative sizes before the
551/// final baseline position is known. The values here are relative to the
552/// overall line's baseline and *not* the nested baseline of an inline box.
553#[derive(Clone, Debug)]
554struct BaselineRelativeSize {
555    /// The ascent above the baseline, where a positive value means a larger
556    /// ascent. Thus, the top of this size contribution is `baseline_offset -
557    /// ascent`.
558    ascent: Au,
559
560    /// The descent below the baseline, where a positive value means a larger
561    /// descent. Thus, the bottom of this size contribution is `baseline_offset +
562    /// descent`.
563    descent: Au,
564}
565
566impl BaselineRelativeSize {
567    fn zero() -> Self {
568        Self {
569            ascent: Au::zero(),
570            descent: Au::zero(),
571        }
572    }
573
574    fn max(&self, other: &Self) -> Self {
575        BaselineRelativeSize {
576            ascent: self.ascent.max(other.ascent),
577            descent: self.descent.max(other.descent),
578        }
579    }
580
581    /// Given an offset from the line's root baseline, adjust this [`BaselineRelativeSize`]
582    /// by that offset. This is used to adjust a [`BaselineRelativeSize`] for different kinds
583    /// of baseline-relative `vertical-align`. This will "move" measured size of a particular
584    /// inline box's block size. For example, in the following HTML:
585    ///
586    /// ```html
587    ///     <div>
588    ///         <span style="vertical-align: 5px">child content</span>
589    ///     </div>
590    /// ````
591    ///
592    /// If this [`BaselineRelativeSize`] is for the `<span>` then the adjustment
593    /// passed here would be equivalent to -5px.
594    fn adjust_for_nested_baseline_offset(&mut self, baseline_offset: Au) {
595        self.ascent -= baseline_offset;
596        self.descent += baseline_offset;
597    }
598}
599
600#[derive(Clone, Debug)]
601struct LineBlockSizes {
602    line_height: Au,
603    baseline_relative_size_for_line_height: Option<BaselineRelativeSize>,
604    size_for_baseline_positioning: BaselineRelativeSize,
605}
606
607impl LineBlockSizes {
608    fn zero() -> Self {
609        LineBlockSizes {
610            line_height: Au::zero(),
611            baseline_relative_size_for_line_height: None,
612            size_for_baseline_positioning: BaselineRelativeSize::zero(),
613        }
614    }
615
616    fn resolve(&self) -> Au {
617        let height_from_ascent_and_descent = self
618            .baseline_relative_size_for_line_height
619            .as_ref()
620            .map(|size| (size.ascent + size.descent).abs())
621            .unwrap_or_else(Au::zero);
622        self.line_height.max(height_from_ascent_and_descent)
623    }
624
625    fn max(&self, other: &LineBlockSizes) -> LineBlockSizes {
626        let baseline_relative_size = match (
627            self.baseline_relative_size_for_line_height.as_ref(),
628            other.baseline_relative_size_for_line_height.as_ref(),
629        ) {
630            (Some(our_size), Some(other_size)) => Some(our_size.max(other_size)),
631            (our_size, other_size) => our_size.or(other_size).cloned(),
632        };
633        Self {
634            line_height: self.line_height.max(other.line_height),
635            baseline_relative_size_for_line_height: baseline_relative_size,
636            size_for_baseline_positioning: self
637                .size_for_baseline_positioning
638                .max(&other.size_for_baseline_positioning),
639        }
640    }
641
642    fn max_assign(&mut self, other: &LineBlockSizes) {
643        *self = self.max(other);
644    }
645
646    fn adjust_for_baseline_offset(&mut self, baseline_offset: Au) {
647        if let Some(size) = self.baseline_relative_size_for_line_height.as_mut() {
648            size.adjust_for_nested_baseline_offset(baseline_offset)
649        }
650        self.size_for_baseline_positioning
651            .adjust_for_nested_baseline_offset(baseline_offset);
652    }
653
654    /// From <https://drafts.csswg.org/css2/visudet.html#line-height>:
655    ///  > The inline-level boxes are aligned vertically according to their 'vertical-align'
656    ///  > property. In case they are aligned 'top' or 'bottom', they must be aligned so as
657    ///  > to minimize the line box height. If such boxes are tall enough, there are multiple
658    ///  > solutions and CSS 2 does not define the position of the line box's baseline (i.e.,
659    ///  > the position of the strut, see below).
660    fn find_baseline_offset(&self) -> Au {
661        match self.baseline_relative_size_for_line_height.as_ref() {
662            Some(size) => size.ascent,
663            None => {
664                // This is the case mentinoned above where there are multiple solutions.
665                // This code is putting the baseline roughly in the middle of the line.
666                let leading = self.resolve() -
667                    (self.size_for_baseline_positioning.ascent +
668                        self.size_for_baseline_positioning.descent);
669                leading.scale_by(0.5) + self.size_for_baseline_positioning.ascent
670            },
671        }
672    }
673}
674
675/// The current unbreakable segment under construction for an inline formatting context.
676/// Items accumulate here until we reach a soft line break opportunity during processing
677/// of inline content or we reach the end of the formatting context.
678struct UnbreakableSegmentUnderConstruction {
679    /// The size of this unbreakable segment in both dimension.
680    inline_size: Au,
681
682    /// The maximum block size that this segment has. This uses [`LineBlockSizes`] instead of a
683    /// simple value, because the final block size depends on vertical alignment.
684    max_block_size: LineBlockSizes,
685
686    /// The LineItems for the segment under construction
687    line_items: Vec<LineItem>,
688
689    /// The depth in the inline box hierarchy at the start of this segment. This is used
690    /// to prefix this segment when it is pushed to a new line.
691    inline_box_hierarchy_depth: Option<usize>,
692
693    /// Whether any active linebox has added a glyph or atomic element to this line
694    /// segment, which indicates that the next run that exceeds the line length can cause
695    /// a line break.
696    has_content: bool,
697
698    /// Whether any active linebox has added some inline-axis padding, border or margin
699    /// to this line segment.
700    has_inline_pbm: bool,
701
702    /// The inline size of any trailing whitespace in this segment.
703    trailing_whitespace_size: Au,
704}
705
706impl UnbreakableSegmentUnderConstruction {
707    fn new() -> Self {
708        Self {
709            inline_size: Au::zero(),
710            max_block_size: LineBlockSizes {
711                line_height: Au::zero(),
712                baseline_relative_size_for_line_height: None,
713                size_for_baseline_positioning: BaselineRelativeSize::zero(),
714            },
715            line_items: Vec::new(),
716            inline_box_hierarchy_depth: None,
717            has_content: false,
718            has_inline_pbm: false,
719            trailing_whitespace_size: Au::zero(),
720        }
721    }
722
723    /// Reset this segment after its contents have been committed to a line.
724    fn reset(&mut self) {
725        assert!(self.line_items.is_empty()); // Preserve allocated memory.
726        self.inline_size = Au::zero();
727        self.max_block_size = LineBlockSizes::zero();
728        self.inline_box_hierarchy_depth = None;
729        self.has_content = false;
730        self.has_inline_pbm = false;
731        self.trailing_whitespace_size = Au::zero();
732    }
733
734    /// Push a single line item to this segment. In addition, record the inline box
735    /// hierarchy depth if this is the first segment. The hierarchy depth is used to
736    /// duplicate the necessary `StartInlineBox` tokens if this segment is ultimately
737    /// placed on a new empty line.
738    fn push_line_item(&mut self, line_item: LineItem, inline_box_hierarchy_depth: usize) {
739        if self.line_items.is_empty() {
740            self.inline_box_hierarchy_depth = Some(inline_box_hierarchy_depth);
741        }
742        self.line_items.push(line_item);
743    }
744
745    /// Trim whitespace from the beginning of this UnbreakbleSegmentUnderConstruction.
746    ///
747    /// From <https://www.w3.org/TR/css-text-3/#white-space-phase-2>:
748    ///
749    /// > Then, the entire block is rendered. Inlines are laid out, taking bidi
750    /// > reordering into account, and wrapping as specified by the text-wrap
751    /// > property. As each line is laid out,
752    /// >  1. A sequence of collapsible spaces at the beginning of a line is removed.
753    ///
754    /// This prevents whitespace from being added to the beginning of a line.
755    fn trim_leading_whitespace(&mut self) {
756        let mut whitespace_trimmed = Au::zero();
757        for item in self.line_items.iter_mut() {
758            if !item.trim_whitespace_at_start(&mut whitespace_trimmed) {
759                break;
760            }
761        }
762        self.inline_size -= whitespace_trimmed;
763    }
764
765    /// Whether this is segment is phantom. If false, its line box won't be phantom.
766    /// <https://drafts.csswg.org/css-inline-3/#invisible-line-boxes>
767    fn is_phantom(&self) -> bool {
768        // Keep this logic in sync with `LineUnderConstruction::is_phantom()`.
769        !self.has_content && !self.has_inline_pbm
770    }
771}
772
773bitflags! {
774    struct InlineContainerStateFlags: u8 {
775        const CREATE_STRUT = 0b0001;
776        const IS_SINGLE_LINE_TEXT_INPUT = 0b0010;
777    }
778}
779
780struct InlineContainerState {
781    /// The style of this inline container.
782    style: ServoArc<ComputedValues>,
783
784    /// Flags which describe details of this [`InlineContainerState`].
785    flags: InlineContainerStateFlags,
786
787    /// Whether or not we have processed any content (an atomic element or text) for
788    /// this inline box on the current line OR any previous line.
789    has_content: RefCell<bool>,
790
791    /// The block size contribution of this container's default font ie the size of the
792    /// "strut." Whether this is integrated into the [`Self::nested_strut_block_sizes`]
793    /// depends on the line-height quirk described in
794    /// <https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk>.
795    strut_block_sizes: LineBlockSizes,
796
797    /// The strut block size of this inline container maxed with the strut block
798    /// sizes of all inline container ancestors. In quirks mode, this will be
799    /// zero, until we know that an element has inline content.
800    nested_strut_block_sizes: LineBlockSizes,
801
802    /// The baseline offset of this container from the baseline of the line. The is the
803    /// cumulative offset of this container and all of its parents. In contrast to the
804    /// `vertical-align` property a positive value indicates an offset "below" the
805    /// baseline while a negative value indicates one "above" it (when the block direction
806    /// is vertical).
807    pub baseline_offset: Au,
808
809    /// The primary font used for this container, if one exists. This is the font that is
810    /// used when not falling back.
811    default_font: Option<FontRef>,
812
813    /// The font metrics of the non-fallback font for this container.
814    font_metrics: Arc<FontMetrics>,
815}
816
817struct InlineFormattingContextLayout<'layout_data> {
818    positioning_context: &'layout_data mut PositioningContext,
819    placement_state: PlacementState<'layout_data>,
820    sequential_layout_state: Option<&'layout_data mut SequentialLayoutState>,
821    layout_context: &'layout_data LayoutContext<'layout_data>,
822
823    /// The [`InlineFormattingContext`] that we are laying out.
824    ifc: &'layout_data InlineFormattingContext,
825
826    /// The [`InlineContainerState`] for the container formed by the root of the
827    /// [`InlineFormattingContext`]. This is effectively the "root inline box" described
828    /// by <https://drafts.csswg.org/css-inline/#model>:
829    ///
830    /// > The block container also generates a root inline box, which is an anonymous
831    /// > inline box that holds all of its inline-level contents. (Thus, all text in an
832    /// > inline formatting context is directly contained by an inline box, whether the root
833    /// > inline box or one of its descendants.) The root inline box inherits from its
834    /// > parent block container, but is otherwise unstyleable.
835    root_nesting_level: InlineContainerState,
836
837    /// A stack of [`InlineBoxContainerState`] that is used to produce [`LineItem`]s either when we
838    /// reach the end of an inline box or when we reach the end of a line. Only at the end
839    /// of the inline box is the state popped from the stack.
840    inline_box_state_stack: Vec<Rc<InlineBoxContainerState>>,
841
842    /// A collection of [`InlineBoxContainerState`] of all the inlines that are present
843    /// in this inline formatting context. We keep this as well as the stack, so that we
844    /// can access them during line layout, which may happen after relevant [`InlineBoxContainerState`]s
845    /// have been popped of the stack.
846    inline_box_states: Vec<Rc<InlineBoxContainerState>>,
847
848    /// A vector of fragment that are laid out. This includes one [`Fragment::Positioning`]
849    /// per line that is currently laid out plus fragments for all floats, which
850    /// are currently laid out at the top-level of each [`InlineFormattingContext`].
851    fragments: Vec<Fragment>,
852
853    /// Information about the line currently being laid out into [`LineItem`]s.
854    current_line: LineUnderConstruction,
855
856    /// Information about the unbreakable line segment currently being laid out into [`LineItem`]s.
857    current_line_segment: UnbreakableSegmentUnderConstruction,
858
859    /// After a forced line break (for instance from a `<br>` element) we wait to actually
860    /// break the line until seeing more content. This allows ongoing inline boxes to finish,
861    /// since in the case where they have no more content they should not be on the next
862    /// line.
863    ///
864    /// For instance:
865    ///
866    /// ``` html
867    ///    <span style="border-right: 30px solid blue;">
868    ///         first line<br>
869    ///    </span>
870    ///    second line
871    /// ```
872    ///
873    /// In this case, the `<span>` should not extend to the second line. If we linebreak
874    /// as soon as we encounter the `<br>` the `<span>`'s ending inline borders would be
875    /// placed on the second line, because we add those borders in
876    /// [`InlineFormattingContextLayout::finish_inline_box()`].
877    ///
878    /// If this field is `Some`, a hard line break should be processed before any new content. The
879    /// `usize` stores the character offset of the originating hard line break, which is used to
880    /// generate placeholders for carets on otherwise empty lines.
881    force_line_break_before_new_content: Option<usize>,
882
883    /// When a `<br>` element has `clear`, this needs to be applied after the linebreak,
884    /// which will be processed *after* the `<br>` element is processed. This member
885    /// stores any deferred `clear` to apply after a linebreak.
886    deferred_br_clear: Clear,
887
888    /// Whether or not a soft wrap opportunity is queued. Soft wrap opportunities are
889    /// queued after replaced content and they are processed when the next text content
890    /// is encountered.
891    pub have_deferred_soft_wrap_opportunity: bool,
892
893    /// Whether or not the layout of this InlineFormattingContext depends on the block size
894    /// of its container for the purposes of flexbox layout.
895    depends_on_block_constraints: bool,
896
897    /// The currently white-space-collapse setting of this line. This is stored on the
898    /// [`InlineFormattingContextLayout`] because when a soft wrap opportunity is defined
899    /// by the boundary between two characters, the white-space-collapse property of their
900    /// nearest common ancestor is used.
901    white_space_collapse: WhiteSpaceCollapse,
902
903    /// The currently text-wrap-mode setting of this line. This is stored on the
904    /// [`InlineFormattingContextLayout`] because when a soft wrap opportunity is defined
905    /// by the boundary between two characters, the text-wrap-mode property of their nearest
906    /// common ancestor is used.
907    text_wrap_mode: TextWrapMode,
908}
909
910impl InlineFormattingContextLayout<'_> {
911    fn current_inline_container_state(&self) -> &InlineContainerState {
912        match self.inline_box_state_stack.last() {
913            Some(inline_box_state) => &inline_box_state.base,
914            None => &self.root_nesting_level,
915        }
916    }
917
918    fn current_inline_box_identifier(&self) -> Option<InlineBoxIdentifier> {
919        self.inline_box_state_stack
920            .last()
921            .map(|state| state.identifier)
922    }
923
924    fn current_line_max_block_size_including_nested_containers(&self) -> LineBlockSizes {
925        self.current_inline_container_state()
926            .nested_strut_block_sizes
927            .max(&self.current_line.max_block_size)
928    }
929
930    fn current_line_block_start_considering_placement_among_floats(&self) -> Au {
931        self.current_line.placement_among_floats.get().map_or(
932            self.current_line.start_position.block,
933            |placement_among_floats| placement_among_floats.start_corner.block,
934        )
935    }
936
937    fn propagate_current_nesting_level_white_space_style(&mut self) {
938        let style = match self.inline_box_state_stack.last() {
939            Some(inline_box_state) => &inline_box_state.base.style,
940            None => self.placement_state.containing_block.style,
941        };
942        let style_text = style.get_inherited_text();
943        self.white_space_collapse = style_text.white_space_collapse;
944        self.text_wrap_mode = style_text.text_wrap_mode;
945    }
946
947    fn processing_br_element(&self) -> bool {
948        self.inline_box_state_stack.last().is_some_and(|state| {
949            state
950                .base_fragment_info
951                .flags
952                .contains(FragmentFlags::IS_BR_ELEMENT)
953        })
954    }
955
956    /// Start laying out a particular [`InlineBox`] into line items. This will push
957    /// a new [`InlineBoxContainerState`] onto [`Self::inline_box_state_stack`].
958    fn start_inline_box(&mut self, inline_box: &InlineBox) {
959        let containing_block = self.containing_block();
960        let inline_box_state = InlineBoxContainerState::new(
961            inline_box,
962            containing_block,
963            self.layout_context,
964            self.current_inline_container_state(),
965            inline_box.default_font.clone(),
966        );
967
968        self.depends_on_block_constraints |= inline_box
969            .base
970            .style
971            .depends_on_block_constraints_due_to_relative_positioning(
972                containing_block.style.writing_mode,
973            );
974
975        // If we are starting a `<br>` element prepare to clear after its deferred linebreak has been
976        // processed. Note that a `<br>` is composed of the element itself and the inner pseudo-element
977        // with the actual linebreak. Both will have this `FragmentFlag`; that's why this code only
978        // sets `deferred_br_clear` if it isn't set yet.
979        if inline_box_state
980            .base_fragment_info
981            .flags
982            .contains(FragmentFlags::IS_BR_ELEMENT) &&
983            self.deferred_br_clear == Clear::None
984        {
985            self.deferred_br_clear = Clear::from_style_and_container_writing_mode(
986                &inline_box_state.base.style,
987                self.containing_block().style.writing_mode,
988            );
989        }
990
991        let padding = inline_box_state.pbm.padding.inline_start;
992        let border = inline_box_state.pbm.border.inline_start;
993        let margin = inline_box_state.pbm.margin.inline_start.auto_is(Au::zero);
994        // We can't just check if the sum is zero because the margin can be negative,
995        // we need to check the values separately.
996        if !padding.is_zero() || !border.is_zero() || !margin.is_zero() {
997            self.current_line_segment.has_inline_pbm = true;
998        }
999        self.current_line_segment.inline_size += padding + border + margin;
1000        self.current_line_segment
1001            .line_items
1002            .push(LineItem::InlineStartBoxPaddingBorderMargin(
1003                inline_box.identifier,
1004            ));
1005
1006        let inline_box_state = Rc::new(inline_box_state);
1007
1008        // Push the state onto the IFC-wide collection of states. Inline boxes are numbered in
1009        // the order that they are encountered, so this should correspond to the order they
1010        // are pushed onto `self.inline_box_states`.
1011        assert_eq!(
1012            self.inline_box_states.len(),
1013            inline_box.identifier.index_in_inline_boxes as usize
1014        );
1015        self.inline_box_states.push(inline_box_state.clone());
1016        self.inline_box_state_stack.push(inline_box_state);
1017    }
1018
1019    /// Finish laying out a particular [`InlineBox`] into line items. This will
1020    /// pop its state off of [`Self::inline_box_state_stack`].
1021    fn finish_inline_box(&mut self) {
1022        let inline_box_state = match self.inline_box_state_stack.pop() {
1023            Some(inline_box_state) => inline_box_state,
1024            None => return, // We are at the root.
1025        };
1026
1027        self.current_line_segment
1028            .max_block_size
1029            .max_assign(&inline_box_state.base.nested_strut_block_sizes);
1030
1031        // If the inline box that we just finished had any content at all, we want to propagate
1032        // the `white-space` property of its parent to future inline children. This is because
1033        // when a soft wrap opportunity is defined by the boundary between two elements, the
1034        // `white-space` used is that of their nearest common ancestor.
1035        if *inline_box_state.base.has_content.borrow() {
1036            self.propagate_current_nesting_level_white_space_style();
1037        }
1038
1039        let padding = inline_box_state.pbm.padding.inline_end;
1040        let border = inline_box_state.pbm.border.inline_end;
1041        let margin = inline_box_state.pbm.margin.inline_end.auto_is(Au::zero);
1042        // We can't just check if the sum is zero because the margin can be negative,
1043        // we need to check the values separately.
1044        if !padding.is_zero() || !border.is_zero() || !margin.is_zero() {
1045            self.current_line_segment.has_inline_pbm = true;
1046        }
1047        self.current_line_segment.inline_size += padding + border + margin;
1048        self.current_line_segment
1049            .line_items
1050            .push(LineItem::InlineEndBoxPaddingBorderMargin(
1051                inline_box_state.identifier,
1052            ))
1053    }
1054
1055    fn finish_last_line(&mut self) {
1056        // First, process any deferred forced line breaks.
1057        self.possibly_flush_deferred_forced_line_break();
1058
1059        // We are at the end of the IFC, and we need to do a few things to make sure that
1060        // the current segment is committed and that the final line is finished.
1061        //
1062        // A soft wrap opportunity makes it so the current segment is placed on a new line
1063        // if it doesn't fit on the current line under construction.
1064        self.process_soft_wrap_opportunity();
1065
1066        // `process_soft_line_wrap_opportunity` does not commit the segment to a line if
1067        // there is no line wrapping, so this forces the segment into the current line.
1068        self.commit_current_segment_to_line();
1069
1070        // Finally we finish the line itself and convert all of the LineItems into
1071        // fragments.
1072        self.finish_current_line_and_reset(true /* last_line_or_forced_line_break */);
1073    }
1074
1075    /// Finish layout of all inline boxes for the current line. This will gather all
1076    /// [`LineItem`]s and turn them into [`Fragment`]s, then reset the
1077    /// [`InlineFormattingContextLayout`] preparing it for laying out a new line.
1078    fn finish_current_line_and_reset(&mut self, last_line_or_forced_line_break: bool) {
1079        self.possibly_push_empty_text_run_to_line_for_text_caret();
1080
1081        let whitespace_trimmed = self.current_line.trim_trailing_whitespace();
1082        let (inline_start_position, justification_adjustment) = self
1083            .calculate_current_line_inline_start_and_justification_adjustment(
1084                whitespace_trimmed,
1085                last_line_or_forced_line_break,
1086            );
1087
1088        // https://drafts.csswg.org/css-inline-3/#invisible-line-boxes
1089        // > Line boxes that contain no text, no preserved white space, no inline boxes with non-zero
1090        // > inline-axis margins, padding, or borders, and no other in-flow content (such as atomic
1091        // > inlines or ruby annotations), and do not end with a forced line break are phantom line boxes.
1092        // > Such boxes must be treated as zero-height line boxes for the purposes of determining the
1093        // > positions of any descendant content (such as absolutely positioned boxes), and both the
1094        // > line box and its in-flow content must be treated as not existing for any other layout or
1095        // > rendering purpose.
1096        let is_phantom_line = self.current_line.is_phantom();
1097        if !is_phantom_line {
1098            self.current_line.start_position.block += self.placement_state.current_margin.solve();
1099            self.placement_state.current_margin = CollapsedMargin::zero();
1100        }
1101        let block_start_position =
1102            self.current_line_block_start_considering_placement_among_floats();
1103
1104        let effective_block_advance = if is_phantom_line {
1105            LineBlockSizes::zero()
1106        } else {
1107            self.current_line_max_block_size_including_nested_containers()
1108        };
1109
1110        let resolved_block_advance = effective_block_advance.resolve();
1111        let block_end_position = if self.current_line.for_block_level {
1112            self.placement_state.current_block_direction_position
1113        } else {
1114            let mut block_end_position = block_start_position + resolved_block_advance;
1115            if let Some(sequential_layout_state) = self.sequential_layout_state.as_mut() {
1116                if !is_phantom_line {
1117                    sequential_layout_state.commit_margin();
1118                }
1119
1120                // This amount includes both the block size of the line and any extra space
1121                // added to move the line down in order to avoid overlapping floats.
1122                let increment = block_end_position - self.current_line.start_position.block;
1123                sequential_layout_state.advance_block_position(increment);
1124
1125                // This newline may have been triggered by a `<br>` with clearance, in which case we
1126                // want to make sure that we make space not only for the current line, but any clearance
1127                // from floats.
1128                if let Some(clearance) = sequential_layout_state
1129                    .calculate_clearance(self.deferred_br_clear, &CollapsedMargin::zero())
1130                {
1131                    sequential_layout_state.advance_block_position(clearance);
1132                    block_end_position += clearance;
1133                };
1134                self.deferred_br_clear = Clear::None;
1135            }
1136            block_end_position
1137        };
1138
1139        // Set up the new line now that we no longer need the old one.
1140        let mut line_to_layout = std::mem::replace(
1141            &mut self.current_line,
1142            LineUnderConstruction::new(LogicalVec2 {
1143                inline: Au::zero(),
1144                block: block_end_position,
1145            }),
1146        );
1147        if !line_to_layout.for_block_level {
1148            self.placement_state.current_block_direction_position = block_end_position;
1149        }
1150
1151        if line_to_layout.has_floats_waiting_to_be_placed {
1152            place_pending_floats(self, &mut line_to_layout.line_items);
1153        }
1154
1155        let start_position = LogicalVec2 {
1156            block: block_start_position,
1157            inline: inline_start_position,
1158        };
1159
1160        let baseline_offset = effective_block_advance.find_baseline_offset();
1161        let start_positioning_context_length = self.positioning_context.len();
1162        let fragments = LineItemLayout::layout_line_items(
1163            self,
1164            line_to_layout.line_items,
1165            start_position,
1166            &effective_block_advance,
1167            justification_adjustment,
1168            is_phantom_line,
1169        );
1170
1171        if !is_phantom_line {
1172            let baseline = baseline_offset + block_start_position;
1173            self.placement_state
1174                .inflow_baselines
1175                .first
1176                .get_or_insert(baseline);
1177            self.placement_state.inflow_baselines.last = Some(baseline);
1178            self.placement_state
1179                .next_in_flow_margin_collapses_with_parent_start_margin = false;
1180        }
1181
1182        // If the line doesn't have any fragments, we don't need to add a containing fragment for it.
1183        if fragments.is_empty() &&
1184            self.positioning_context.len() == start_positioning_context_length
1185        {
1186            return;
1187        }
1188
1189        // The inline part of this start offset was taken into account when determining
1190        // the inline start of the line in `calculate_inline_start_for_current_line` so
1191        // we do not need to include it in the `start_corner` of the line's main Fragment.
1192        let start_corner = LogicalVec2 {
1193            inline: Au::zero(),
1194            block: block_start_position,
1195        };
1196
1197        let logical_origin_in_physical_coordinates =
1198            start_corner.to_physical_vector(self.containing_block().style.writing_mode);
1199        self.positioning_context
1200            .adjust_static_position_of_hoisted_fragments_with_offset(
1201                &logical_origin_in_physical_coordinates,
1202                start_positioning_context_length,
1203            );
1204
1205        let containing_block = self.containing_block();
1206        let physical_line_rect = LogicalRect {
1207            start_corner,
1208            size: LogicalVec2 {
1209                inline: containing_block.size.inline,
1210                block: effective_block_advance.resolve(),
1211            },
1212        }
1213        .as_physical(Some(containing_block));
1214        self.fragments
1215            .push(Fragment::Positioning(PositioningFragment::new_anonymous(
1216                self.root_nesting_level.style.clone(),
1217                physical_line_rect,
1218                fragments,
1219            )));
1220    }
1221
1222    /// Given the amount of whitespace trimmed from the line and taking into consideration
1223    /// the `text-align` property, calculate where the line under construction starts in
1224    /// the inline axis as well as the adjustment needed for every justification opportunity
1225    /// to account for `text-align: justify`.
1226    fn calculate_current_line_inline_start_and_justification_adjustment(
1227        &self,
1228        whitespace_trimmed: Au,
1229        last_line_or_forced_line_break: bool,
1230    ) -> (Au, Au) {
1231        enum TextAlign {
1232            Start,
1233            Center,
1234            End,
1235        }
1236        let containing_block = self.containing_block();
1237        let style = containing_block.style;
1238        let mut text_align_keyword = style.clone_text_align();
1239
1240        if last_line_or_forced_line_break {
1241            text_align_keyword = match style.clone_text_align_last() {
1242                TextAlignLast::Auto if text_align_keyword == TextAlignKeyword::Justify => {
1243                    TextAlignKeyword::Start
1244                },
1245                TextAlignLast::Auto => text_align_keyword,
1246                TextAlignLast::Start => TextAlignKeyword::Start,
1247                TextAlignLast::End => TextAlignKeyword::End,
1248                TextAlignLast::Left => TextAlignKeyword::Left,
1249                TextAlignLast::Right => TextAlignKeyword::Right,
1250                TextAlignLast::Center => TextAlignKeyword::Center,
1251                TextAlignLast::Justify => TextAlignKeyword::Justify,
1252            };
1253        }
1254
1255        let text_align = match text_align_keyword {
1256            TextAlignKeyword::Start => TextAlign::Start,
1257            TextAlignKeyword::Center | TextAlignKeyword::MozCenter => TextAlign::Center,
1258            TextAlignKeyword::End => TextAlign::End,
1259            TextAlignKeyword::Left | TextAlignKeyword::MozLeft => {
1260                if style.writing_mode.line_left_is_inline_start() {
1261                    TextAlign::Start
1262                } else {
1263                    TextAlign::End
1264                }
1265            },
1266            TextAlignKeyword::Right | TextAlignKeyword::MozRight => {
1267                if style.writing_mode.line_left_is_inline_start() {
1268                    TextAlign::End
1269                } else {
1270                    TextAlign::Start
1271                }
1272            },
1273            TextAlignKeyword::Justify => TextAlign::Start,
1274        };
1275
1276        let (line_start, available_space) = match self.current_line.placement_among_floats.get() {
1277            Some(placement_among_floats) => (
1278                placement_among_floats.start_corner.inline,
1279                placement_among_floats.size.inline,
1280            ),
1281            None => (Au::zero(), containing_block.size.inline),
1282        };
1283
1284        // Properly handling text-indent requires that we do not align the text
1285        // into the text-indent.
1286        // See <https://drafts.csswg.org/css-text/#text-indent-property>
1287        // "This property specifies the indentation applied to lines of inline content in
1288        // a block. The indent is treated as a margin applied to the start edge of the
1289        // line box."
1290        let text_indent = self.current_line.start_position.inline;
1291        let line_length = self.current_line.inline_position - whitespace_trimmed - text_indent;
1292        let adjusted_line_start = line_start +
1293            match text_align {
1294                TextAlign::Start => text_indent,
1295                TextAlign::End => (available_space - line_length).max(text_indent),
1296                TextAlign::Center => (available_space - line_length + text_indent)
1297                    .scale_by(0.5)
1298                    .max(text_indent),
1299            };
1300
1301        // Calculate the justification adjustment. This is simply the remaining space on the line,
1302        // dividided by the number of justficiation opportunities that we recorded when building
1303        // the line.
1304        let text_justify = containing_block.style.clone_text_justify();
1305        let justification_adjustment = match (text_align_keyword, text_justify) {
1306            // `text-justify: none` should disable text justification.
1307            // TODO: Handle more `text-justify` values.
1308            (TextAlignKeyword::Justify, TextJustify::None) => Au::zero(),
1309            (TextAlignKeyword::Justify, _) => {
1310                match self.current_line.count_justification_opportunities() {
1311                    0 => Au::zero(),
1312                    num_justification_opportunities => {
1313                        (available_space - text_indent - line_length)
1314                            .scale_by(1. / num_justification_opportunities as f32)
1315                    },
1316                }
1317            },
1318            _ => Au::zero(),
1319        };
1320
1321        // If the content overflows the line, then justification adjustment will become negative. In
1322        // that case, do not make any adjustment for justification.
1323        let justification_adjustment = justification_adjustment.max(Au::zero());
1324
1325        (adjusted_line_start, justification_adjustment)
1326    }
1327
1328    fn place_float_fragment(&mut self, fragment: &mut BoxFragment) {
1329        let state = self
1330            .sequential_layout_state
1331            .as_mut()
1332            .expect("Tried to lay out a float with no sequential placement state!");
1333
1334        let block_offset_from_containining_block_top = state
1335            .current_block_position_including_margins() -
1336            state.current_containing_block_offset();
1337        state.place_float_fragment(
1338            fragment,
1339            self.placement_state.containing_block,
1340            CollapsedMargin::zero(),
1341            block_offset_from_containining_block_top,
1342        );
1343    }
1344
1345    /// Place a FloatLineItem. This is done when an unbreakable segment is committed to
1346    /// the current line. Placement of FloatLineItems might need to be deferred until the
1347    /// line is complete in the case that floats stop fitting on the current line.
1348    ///
1349    /// When placing floats we do not want to take into account any trailing whitespace on
1350    /// the line, because that whitespace will be trimmed in the case that the line is
1351    /// broken. Thus this function takes as an argument the new size (without whitespace) of
1352    /// the line that these floats are joining.
1353    fn place_float_line_item_for_commit_to_line(
1354        &mut self,
1355        float_item: &mut FloatLineItem,
1356        line_inline_size_without_trailing_whitespace: Au,
1357    ) {
1358        let containing_block = self.containing_block();
1359        let mut float_fragment = float_item.fragment.borrow_mut();
1360        let logical_margin_rect_size = float_fragment
1361            .margin_rect()
1362            .size
1363            .to_logical(containing_block.style.writing_mode);
1364        let inline_size = logical_margin_rect_size.inline.max(Au::zero());
1365
1366        let available_inline_size = match self.current_line.placement_among_floats.get() {
1367            Some(placement_among_floats) => placement_among_floats.size.inline,
1368            None => containing_block.size.inline,
1369        } - line_inline_size_without_trailing_whitespace;
1370
1371        // If this float doesn't fit on the current line or a previous float didn't fit on
1372        // the current line, we need to place it starting at the next line BUT still as
1373        // children of this line's hierarchy of inline boxes (for the purposes of properly
1374        // parenting in their stacking contexts). Once all the line content is gathered we
1375        // will place them later.
1376        let has_content = self.current_line.has_content || self.current_line_segment.has_content;
1377        let fits_on_line = !has_content || inline_size <= available_inline_size;
1378        let needs_placement_later =
1379            self.current_line.has_floats_waiting_to_be_placed || !fits_on_line;
1380
1381        if needs_placement_later {
1382            self.current_line.has_floats_waiting_to_be_placed = true;
1383        } else {
1384            self.place_float_fragment(&mut float_fragment);
1385            float_item.needs_placement = false;
1386        }
1387
1388        // We've added a new float to the IFC, but this may have actually changed the
1389        // position of the current line. In order to determine that we regenerate the
1390        // placement among floats for the current line, which may adjust its inline
1391        // start position.
1392        let new_placement = self.place_line_among_floats(&LogicalVec2 {
1393            inline: line_inline_size_without_trailing_whitespace,
1394            block: self.current_line.max_block_size.resolve(),
1395        });
1396        self.current_line
1397            .replace_placement_among_floats(new_placement);
1398    }
1399
1400    /// Given a new potential line size for the current line, create a "placement" for that line.
1401    /// This tells us whether or not the new potential line will fit in the current block position
1402    /// or need to be moved. In addition, the placement rect determines the inline start and end
1403    /// of the line if it's used as the final placement among floats.
1404    fn place_line_among_floats(&self, potential_line_size: &LogicalVec2<Au>) -> LogicalRect<Au> {
1405        let sequential_layout_state = self
1406            .sequential_layout_state
1407            .as_ref()
1408            .expect("Should not have called this function without having floats.");
1409
1410        let ifc_offset_in_float_container = LogicalVec2 {
1411            inline: sequential_layout_state
1412                .floats
1413                .containing_block_info
1414                .inline_start,
1415            block: sequential_layout_state.current_containing_block_offset(),
1416        };
1417
1418        let ceiling = self.current_line_block_start_considering_placement_among_floats();
1419        let mut placement = PlacementAmongFloats::new(
1420            &sequential_layout_state.floats,
1421            ceiling + ifc_offset_in_float_container.block,
1422            LogicalVec2 {
1423                inline: potential_line_size.inline,
1424                block: potential_line_size.block,
1425            },
1426            &PaddingBorderMargin::zero(),
1427        );
1428
1429        let mut placement_rect = placement.place();
1430        placement_rect.start_corner -= ifc_offset_in_float_container;
1431        placement_rect
1432    }
1433
1434    /// Returns true if a new potential line size for the current line would require a line
1435    /// break. This takes into account floats and will also update the "placement among
1436    /// floats" for this line if the potential line size would not cause a line break.
1437    /// Thus, calling this method has side effects and should only be done while in the
1438    /// process of laying out line content that is always going to be committed to this
1439    /// line or the next.
1440    fn new_potential_line_size_causes_line_break(
1441        &mut self,
1442        potential_line_size: &LogicalVec2<Au>,
1443    ) -> bool {
1444        let containing_block = self.containing_block();
1445        let available_line_space = if self.sequential_layout_state.is_some() {
1446            self.current_line
1447                .placement_among_floats
1448                .get_or_init(|| self.place_line_among_floats(potential_line_size))
1449                .size
1450        } else {
1451            LogicalVec2 {
1452                inline: containing_block.size.inline,
1453                block: MAX_AU,
1454            }
1455        };
1456
1457        let inline_would_overflow = potential_line_size.inline > available_line_space.inline;
1458        let block_would_overflow = potential_line_size.block > available_line_space.block;
1459
1460        // The first content that is added to a line cannot trigger a line break and
1461        // the `white-space` propertly can also prevent all line breaking.
1462        let can_break = self.current_line.has_content;
1463
1464        // If this is the first content on the line and we already have a float placement,
1465        // that means that the placement was initialized by a leading float in the IFC.
1466        // This placement needs to be updated, because the first line content might push
1467        // the block start of the line downward. If there is no float placement, we want
1468        // to make one to properly set the block position of the line.
1469        if !can_break {
1470            // Even if we cannot break, adding content to this line might change its position.
1471            // In that case we need to redo our placement among floats.
1472            if self.sequential_layout_state.is_some() &&
1473                (inline_would_overflow || block_would_overflow)
1474            {
1475                let new_placement = self.place_line_among_floats(potential_line_size);
1476                self.current_line
1477                    .replace_placement_among_floats(new_placement);
1478            }
1479
1480            return false;
1481        }
1482
1483        // If the potential line is larger than the containing block we do not even need to consider
1484        // floats. We definitely have to do a linebreak.
1485        if potential_line_size.inline > containing_block.size.inline {
1486            return true;
1487        }
1488
1489        // Not fitting in the block space means that our block size has changed and we had a
1490        // placement among floats that is no longer valid. This same placement might just
1491        // need to be expanded or perhaps we need to line break.
1492        if block_would_overflow {
1493            // If we have a limited block size then we are wedging this line between floats.
1494            assert!(self.sequential_layout_state.is_some());
1495            let new_placement = self.place_line_among_floats(potential_line_size);
1496            if new_placement.start_corner.block !=
1497                self.current_line_block_start_considering_placement_among_floats()
1498            {
1499                return true;
1500            } else {
1501                self.current_line
1502                    .replace_placement_among_floats(new_placement);
1503                return false;
1504            }
1505        }
1506
1507        // Otherwise the new potential line size will require a newline if it fits in the
1508        // inline space available for this line. This space may be smaller than the
1509        // containing block if floats shrink the available inline space.
1510        inline_would_overflow
1511    }
1512
1513    fn defer_forced_line_break_at_character_offset(&mut self, line_break_offset: usize) {
1514        // If the current portion of the unbreakable segment does not fit on the current line
1515        // we need to put it on a new line *before* actually triggering the hard line break.
1516        if !self.unbreakable_segment_fits_on_line() {
1517            self.process_line_break(false /* forced_line_break */);
1518        }
1519
1520        // Defer the actual line break until we've cleared all ending inline boxes.
1521        self.force_line_break_before_new_content = Some(line_break_offset);
1522
1523        // In quirks mode, the line-height isn't automatically added to the line. If we consider a
1524        // forced line break a kind of preserved white space, quirks mode requires that we add the
1525        // line-height of the current element to the line box height.
1526        //
1527        // The exception here is `<br>` elements. They are implemented with `pre-line` in Servo, but
1528        // this is an implementation detail. The "magic" behavior of `<br>` elements is that they
1529        // add line-height to the line conditionally: only when they are on an otherwise empty line.
1530        let line_is_empty =
1531            !self.current_line_segment.has_content && !self.current_line.has_content;
1532        if !self.processing_br_element() || line_is_empty {
1533            let strut_size = self
1534                .current_inline_container_state()
1535                .strut_block_sizes
1536                .clone();
1537            self.update_unbreakable_segment_for_new_content(
1538                &strut_size,
1539                Au::zero(),
1540                SegmentContentFlags::empty(),
1541            );
1542        }
1543    }
1544
1545    fn possibly_flush_deferred_forced_line_break(&mut self) {
1546        let Some(line_break_character_offset) = self.force_line_break_before_new_content.take()
1547        else {
1548            return;
1549        };
1550
1551        self.commit_current_segment_to_line();
1552        self.process_line_break(true /* forced_line_break */);
1553
1554        self.current_line.starting_character_offset = line_break_character_offset + 1;
1555    }
1556
1557    fn push_line_item_to_unbreakable_segment(&mut self, line_item: LineItem) {
1558        self.current_line_segment
1559            .push_line_item(line_item, self.inline_box_state_stack.len());
1560    }
1561
1562    fn push_glyph_store_to_unbreakable_segment(
1563        &mut self,
1564        glyph_store: Arc<ShapedTextSlice>,
1565        text_run: &TextRun,
1566        info: &Arc<FontAndScriptInfo>,
1567        offsets: Option<TextRunOffsets>,
1568    ) {
1569        let inline_advance = glyph_store.total_advance();
1570        let flags = if glyph_store.is_whitespace() {
1571            SegmentContentFlags::from(text_run.inline_styles.style.borrow().get_inherited_text())
1572        } else {
1573            SegmentContentFlags::empty()
1574        };
1575
1576        let mut block_contribution = LineBlockSizes::zero();
1577        let quirks_mode = self.layout_context.style_context.quirks_mode() != QuirksMode::NoQuirks;
1578        if quirks_mode && !flags.is_collapsible_whitespace() {
1579            // Normally, the strut is incorporated into the nested block size. In quirks mode though
1580            // if we find any text that isn't collapsed whitespace, we need to incorporate the strut.
1581            // TODO(mrobinson): This isn't quite right for situations where collapsible white space
1582            // ultimately does not collapse because it is between two other pieces of content.
1583            block_contribution.max_assign(&self.current_inline_container_state().strut_block_sizes);
1584        }
1585
1586        // If the metrics of this font don't match the default font, we are likely using another
1587        // font from the font list or a fallback and should incorporate its block size into the block
1588        // size of the container.
1589        let font_metrics = &info.font.metrics;
1590        if self
1591            .current_inline_container_state()
1592            .font_metrics
1593            .block_metrics_meaningfully_differ(font_metrics)
1594        {
1595            // TODO(mrobinson): This value should probably be cached somewhere.
1596            let container_state = self.current_inline_container_state();
1597            let baseline_shift = effective_baseline_shift(
1598                &container_state.style,
1599                self.inline_box_state_stack.last().map(|c| &c.base),
1600            );
1601            let mut font_block_conribution = container_state.get_block_size_contribution(
1602                baseline_shift,
1603                font_metrics,
1604                &container_state.font_metrics,
1605            );
1606            font_block_conribution.adjust_for_baseline_offset(container_state.baseline_offset);
1607            block_contribution.max_assign(&font_block_conribution);
1608        }
1609
1610        self.update_unbreakable_segment_for_new_content(&block_contribution, inline_advance, flags);
1611
1612        let current_inline_box_identifier = self.current_inline_box_identifier();
1613        if let Some(LineItem::TextRun(inline_box_identifier, line_item)) =
1614            self.current_line_segment.line_items.last_mut()
1615        {
1616            if *inline_box_identifier == current_inline_box_identifier &&
1617                line_item.merge_if_possible(
1618                    info,
1619                    &glyph_store,
1620                    &offsets,
1621                    &text_run.inline_styles,
1622                )
1623            {
1624                return;
1625            }
1626        }
1627
1628        self.push_line_item_to_unbreakable_segment(LineItem::TextRun(
1629            current_inline_box_identifier,
1630            TextRunLineItem {
1631                text: vec![glyph_store],
1632                base_fragment_info: text_run.base_fragment_info,
1633                inline_styles: text_run.inline_styles.clone(),
1634                info: info.clone(),
1635                offsets: offsets.map(Box::new),
1636                is_empty_for_text_cursor: false,
1637            },
1638        ));
1639    }
1640
1641    /// If the current line is empty and this [`InlineFormattingContext`] has a selection, push an
1642    /// empty [`LineItem::TextRun`] so that text carets can be placed on otherwise empty lines.
1643    fn possibly_push_empty_text_run_to_line_for_text_caret(&mut self) {
1644        let line_start_offset = self.current_line.starting_character_offset;
1645        let Some(shared_selection) = self.ifc.shared_selection.clone() else {
1646            return;
1647        };
1648        let offsets = TextRunOffsets {
1649            shared_selection,
1650            character_range: line_start_offset..line_start_offset + 1,
1651        };
1652
1653        // If the last content line item is a text item, then the placeholder for the text caret is not necessary.
1654        if self
1655            .current_line
1656            .line_items
1657            .iter()
1658            .rev()
1659            .find(|line_item| line_item.is_in_flow_content())
1660            .is_some_and(|line_item| matches!(line_item, LineItem::TextRun(..)))
1661        {
1662            return;
1663        }
1664
1665        let inline_container_state = self.current_inline_container_state();
1666        let Some(font) = inline_container_state.default_font.clone() else {
1667            return;
1668        };
1669
1670        self.push_line_item_to_unbreakable_segment(LineItem::TextRun(
1671            self.current_inline_box_identifier(),
1672            TextRunLineItem {
1673                text: Default::default(),
1674                base_fragment_info: BaseFragmentInfo::anonymous(),
1675                inline_styles: self.ifc.shared_inline_styles.clone(),
1676                info: Arc::new(FontAndScriptInfo::simple_for_font(font)),
1677                offsets: Some(Box::new(offsets)),
1678                is_empty_for_text_cursor: true,
1679            },
1680        ));
1681        self.current_line_segment.has_content = true;
1682        self.commit_current_segment_to_line();
1683    }
1684
1685    fn update_unbreakable_segment_for_new_content(
1686        &mut self,
1687        block_sizes_of_content: &LineBlockSizes,
1688        inline_size: Au,
1689        flags: SegmentContentFlags,
1690    ) {
1691        if flags.is_collapsible_whitespace() || flags.is_wrappable_and_hangable() {
1692            self.current_line_segment.trailing_whitespace_size = inline_size;
1693        } else {
1694            self.current_line_segment.trailing_whitespace_size = Au::zero();
1695        }
1696        if !flags.is_collapsible_whitespace() {
1697            self.current_line_segment.has_content = true;
1698        }
1699
1700        // This may or may not include the size of the strut depending on the quirks mode setting.
1701        let container_max_block_size = &self
1702            .current_inline_container_state()
1703            .nested_strut_block_sizes
1704            .clone();
1705        self.current_line_segment
1706            .max_block_size
1707            .max_assign(container_max_block_size);
1708        self.current_line_segment
1709            .max_block_size
1710            .max_assign(block_sizes_of_content);
1711
1712        self.current_line_segment.inline_size += inline_size;
1713
1714        // Propagate the whitespace setting to the current nesting level.
1715        *self
1716            .current_inline_container_state()
1717            .has_content
1718            .borrow_mut() = true;
1719        self.propagate_current_nesting_level_white_space_style();
1720    }
1721
1722    fn process_line_break(&mut self, forced_line_break: bool) {
1723        self.current_line_segment.trim_leading_whitespace();
1724        self.finish_current_line_and_reset(forced_line_break);
1725    }
1726
1727    fn potential_line_size(&self) -> LogicalVec2<Au> {
1728        LogicalVec2 {
1729            inline: self.current_line.inline_position + self.current_line_segment.inline_size,
1730            block: self
1731                .current_line_max_block_size_including_nested_containers()
1732                .max(&self.current_line_segment.max_block_size)
1733                .resolve(),
1734        }
1735    }
1736
1737    fn unbreakable_segment_fits_on_line(&mut self) -> bool {
1738        let potential_line_size_without_hanging_whitespace = self.potential_line_size() -
1739            LogicalVec2 {
1740                inline: self.current_line_segment.trailing_whitespace_size,
1741                block: Au::zero(),
1742            };
1743        !self.new_potential_line_size_causes_line_break(
1744            &potential_line_size_without_hanging_whitespace,
1745        )
1746    }
1747
1748    /// Process a soft wrap opportunity. This will either commit the current unbreakble
1749    /// segment to the current line, if it fits within the containing block and float
1750    /// placement boundaries, or do a line break and then commit the segment.
1751    fn process_soft_wrap_opportunity(&mut self) {
1752        if self.current_line_segment.line_items.is_empty() {
1753            return;
1754        }
1755        if self.text_wrap_mode == TextWrapMode::Nowrap {
1756            return;
1757        }
1758        if !self.unbreakable_segment_fits_on_line() {
1759            self.process_line_break(false /* forced_line_break */);
1760        }
1761        self.commit_current_segment_to_line();
1762    }
1763
1764    /// Commit the current unbrekable segment to the current line. In addition, this will
1765    /// place all floats in the unbreakable segment and expand the line dimensions.
1766    fn commit_current_segment_to_line(&mut self) {
1767        // The line segments might have no items and have content after processing a forced
1768        // linebreak on an empty line.
1769        if self.current_line_segment.line_items.is_empty() && !self.current_line_segment.has_content
1770        {
1771            return;
1772        }
1773
1774        if !self.current_line.has_content {
1775            self.current_line_segment.trim_leading_whitespace();
1776        }
1777
1778        self.current_line.inline_position += self.current_line_segment.inline_size;
1779        self.current_line.max_block_size = self
1780            .current_line_max_block_size_including_nested_containers()
1781            .max(&self.current_line_segment.max_block_size);
1782        let line_inline_size_without_trailing_whitespace =
1783            self.current_line.inline_position - self.current_line_segment.trailing_whitespace_size;
1784
1785        // Place all floats in this unbreakable segment.
1786        let mut segment_items = mem::take(&mut self.current_line_segment.line_items);
1787        for item in segment_items.iter_mut() {
1788            if let LineItem::Float(_, float_item) = item {
1789                self.place_float_line_item_for_commit_to_line(
1790                    float_item,
1791                    line_inline_size_without_trailing_whitespace,
1792                );
1793            }
1794        }
1795
1796        // If the current line was never placed among floats, we need to do that now based on the
1797        // new size. Calling `new_potential_line_size_causes_line_break()` here triggers the
1798        // new line to be positioned among floats. This should never ask for a line
1799        // break because it is the first content on the line.
1800        if self.current_line.line_items.is_empty() {
1801            let will_break = self.new_potential_line_size_causes_line_break(&LogicalVec2 {
1802                inline: line_inline_size_without_trailing_whitespace,
1803                block: self.current_line_segment.max_block_size.resolve(),
1804            });
1805            assert!(!will_break);
1806        }
1807
1808        self.current_line.line_items.extend(segment_items);
1809        self.current_line.has_content |= self.current_line_segment.has_content;
1810        self.current_line.has_inline_pbm |= self.current_line_segment.has_inline_pbm;
1811
1812        self.current_line_segment.reset();
1813    }
1814
1815    #[inline]
1816    fn containing_block(&self) -> &ContainingBlock<'_> {
1817        self.placement_state.containing_block
1818    }
1819}
1820
1821bitflags! {
1822    struct SegmentContentFlags: u8 {
1823        const COLLAPSIBLE_WHITESPACE = 0b00000001;
1824        const WRAPPABLE_AND_HANGABLE_WHITESPACE = 0b00000010;
1825    }
1826}
1827
1828impl SegmentContentFlags {
1829    fn is_collapsible_whitespace(&self) -> bool {
1830        self.contains(Self::COLLAPSIBLE_WHITESPACE)
1831    }
1832
1833    fn is_wrappable_and_hangable(&self) -> bool {
1834        self.contains(Self::WRAPPABLE_AND_HANGABLE_WHITESPACE)
1835    }
1836}
1837
1838impl From<&InheritedText> for SegmentContentFlags {
1839    fn from(style_text: &InheritedText) -> Self {
1840        let mut flags = Self::empty();
1841
1842        // White-space with `white-space-collapse: break-spaces` or `white-space-collapse: preserve`
1843        // never collapses.
1844        if !matches!(
1845            style_text.white_space_collapse,
1846            WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
1847        ) {
1848            flags.insert(Self::COLLAPSIBLE_WHITESPACE);
1849        }
1850
1851        // White-space with `white-space-collapse: break-spaces` never hangs and always takes up
1852        // space.
1853        if style_text.text_wrap_mode == TextWrapMode::Wrap &&
1854            style_text.white_space_collapse != WhiteSpaceCollapse::BreakSpaces
1855        {
1856            flags.insert(Self::WRAPPABLE_AND_HANGABLE_WHITESPACE);
1857        }
1858        flags
1859    }
1860}
1861
1862impl InlineFormattingContext {
1863    #[servo_tracing::instrument(name = "InlineFormattingContext::new_with_builder", skip_all)]
1864    fn new_with_builder(
1865        mut builder: InlineFormattingContextBuilder,
1866        layout_context: &LayoutContext,
1867        has_first_formatted_line: bool,
1868        is_single_line_text_input: bool,
1869        starting_bidi_level: Level,
1870    ) -> Self {
1871        // This is to prevent a double borrow.
1872        let text_content: String = builder.text_segments.into_iter().collect();
1873
1874        let bidi_info = BidiInfo::new(&text_content, Some(starting_bidi_level));
1875        let has_right_to_left_content = bidi_info.has_rtl();
1876        let shared_inline_styles = builder
1877            .shared_inline_styles_stack
1878            .last()
1879            .expect("Should have at least one SharedInlineStyle for the root of an IFC")
1880            .clone();
1881        let (word_break, line_break, lang) = {
1882            let styles = shared_inline_styles.style.borrow();
1883            let text_style = styles.get_inherited_text();
1884            (
1885                text_style.word_break,
1886                text_style.line_break,
1887                styles.get_font()._x_lang.clone(),
1888            )
1889        };
1890
1891        let mut options = LineBreakOptions::default();
1892
1893        options.strictness = match line_break {
1894            LineBreak::Loose => LineBreakStrictness::Loose,
1895            LineBreak::Normal => LineBreakStrictness::Normal,
1896            LineBreak::Strict => LineBreakStrictness::Strict,
1897            LineBreak::Anywhere => LineBreakStrictness::Anywhere,
1898            // For `auto`, the UA determines the set of line-breaking restrictions to use.
1899            // So it's fine if we always treat it as `normal`.
1900            LineBreak::Auto => LineBreakStrictness::Normal,
1901        };
1902        options.word_option = match word_break {
1903            WordBreak::Normal => LineBreakWordOption::Normal,
1904            WordBreak::BreakAll => LineBreakWordOption::BreakAll,
1905            WordBreak::KeepAll => LineBreakWordOption::KeepAll,
1906        };
1907        // Enable Chinese/Japanese line breaking behavior when this inline formatting context
1908        // has a Japanese or Chinese language set.
1909        options.ja_zh = {
1910            lang.0.parse::<LanguageIdentifier>().is_ok_and(|lang_id| {
1911                const JA: Language = language!("ja");
1912                const ZH: Language = language!("zh");
1913                matches!(lang_id.language, JA | ZH)
1914            })
1915        };
1916
1917        let mut new_linebreaker = LineBreaker::new(text_content.as_str(), options);
1918        for item in &mut builder.inline_items {
1919            match item {
1920                InlineItem::TextRun(text_run) => {
1921                    text_run.borrow_mut().segment_and_shape(
1922                        &text_content,
1923                        layout_context,
1924                        &mut new_linebreaker,
1925                        &bidi_info,
1926                    );
1927                },
1928                InlineItem::StartInlineBox(inline_box) => {
1929                    let inline_box = &mut *inline_box.borrow_mut();
1930                    if let Some(font) = get_font_for_first_font_for_style(
1931                        &inline_box.base.style,
1932                        &layout_context.font_context,
1933                    ) {
1934                        inline_box.default_font = Some(font);
1935                    }
1936                },
1937                InlineItem::Atomic(_, index_in_text, bidi_level) => {
1938                    *bidi_level = bidi_info.levels[*index_in_text];
1939                },
1940                InlineItem::OutOfFlowAbsolutelyPositionedBox(..) |
1941                InlineItem::OutOfFlowFloatBox(_) |
1942                InlineItem::EndInlineBox |
1943                InlineItem::BlockLevel { .. } => {},
1944            }
1945        }
1946
1947        let default_font = get_font_for_first_font_for_style(
1948            &shared_inline_styles.style.borrow(),
1949            &layout_context.font_context,
1950        );
1951
1952        InlineFormattingContext {
1953            text_content,
1954            inline_items: builder.inline_items,
1955            inline_boxes: builder.inline_boxes,
1956            shared_inline_styles,
1957            default_font,
1958            has_first_formatted_line,
1959            contains_floats: builder.contains_floats,
1960            is_single_line_text_input,
1961            has_right_to_left_content,
1962            shared_selection: builder.shared_selection,
1963            tab_stop_advance: Default::default(),
1964        }
1965    }
1966
1967    pub(crate) fn repair_style(
1968        &self,
1969        context: &SharedStyleContext,
1970        node: &ServoLayoutNode,
1971        new_style: &ServoArc<ComputedValues>,
1972    ) {
1973        *self.shared_inline_styles.style.borrow_mut() = new_style.clone();
1974        *self.shared_inline_styles.selected.borrow_mut() = node.selected_style(context);
1975    }
1976
1977    fn inline_start_for_first_line(&self, containing_block: IndefiniteContainingBlock) -> Au {
1978        if !self.has_first_formatted_line {
1979            return Au::zero();
1980        }
1981        containing_block
1982            .style
1983            .get_inherited_text()
1984            .text_indent
1985            .length
1986            .to_used_value(containing_block.size.inline.unwrap_or_default())
1987    }
1988
1989    pub(super) fn layout(
1990        &self,
1991        layout_context: &LayoutContext,
1992        positioning_context: &mut PositioningContext,
1993        containing_block: &ContainingBlock,
1994        sequential_layout_state: Option<&mut SequentialLayoutState>,
1995        collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin,
1996    ) -> IndependentFormattingContextLayoutResult {
1997        // Clear any cached inline fragments from previous layouts.
1998        for inline_box in self.inline_boxes.iter() {
1999            inline_box.borrow().base.clear_fragments();
2000        }
2001
2002        let style = containing_block.style;
2003
2004        let style_text = containing_block.style.get_inherited_text();
2005        let mut inline_container_state_flags = InlineContainerStateFlags::empty();
2006        if inline_container_needs_strut(style, layout_context, None) {
2007            inline_container_state_flags.insert(InlineContainerStateFlags::CREATE_STRUT);
2008        }
2009        if self.is_single_line_text_input {
2010            inline_container_state_flags
2011                .insert(InlineContainerStateFlags::IS_SINGLE_LINE_TEXT_INPUT);
2012        }
2013        let placement_state =
2014            PlacementState::new(collapsible_with_parent_start_margin, containing_block);
2015
2016        let mut layout = InlineFormattingContextLayout {
2017            positioning_context,
2018            placement_state,
2019            sequential_layout_state,
2020            layout_context,
2021            ifc: self,
2022            fragments: Vec::new(),
2023            current_line: LineUnderConstruction::new(LogicalVec2 {
2024                inline: self.inline_start_for_first_line(containing_block.into()),
2025                block: Au::zero(),
2026            }),
2027            root_nesting_level: InlineContainerState::new(
2028                style.to_arc(),
2029                inline_container_state_flags,
2030                None, /* parent_container */
2031                self.default_font.clone(),
2032            ),
2033            inline_box_state_stack: Vec::new(),
2034            inline_box_states: Vec::with_capacity(self.inline_boxes.len()),
2035            current_line_segment: UnbreakableSegmentUnderConstruction::new(),
2036            force_line_break_before_new_content: None,
2037            deferred_br_clear: Clear::None,
2038            have_deferred_soft_wrap_opportunity: false,
2039            depends_on_block_constraints: false,
2040            white_space_collapse: style_text.white_space_collapse,
2041            text_wrap_mode: style_text.text_wrap_mode,
2042        };
2043
2044        for item in self.inline_items.iter() {
2045            // Any new box should flush a pending hard line break.
2046            if !matches!(item, InlineItem::EndInlineBox) {
2047                layout.possibly_flush_deferred_forced_line_break();
2048            }
2049
2050            match item {
2051                InlineItem::StartInlineBox(inline_box) => {
2052                    layout.start_inline_box(&inline_box.borrow());
2053                },
2054                InlineItem::EndInlineBox => layout.finish_inline_box(),
2055                InlineItem::TextRun(run) => run.borrow().layout_into_line_items(&mut layout),
2056                InlineItem::Atomic(atomic_formatting_context, offset_in_text, bidi_level) => {
2057                    atomic_formatting_context.borrow().layout_into_line_items(
2058                        &mut layout,
2059                        *offset_in_text,
2060                        *bidi_level,
2061                    );
2062                },
2063                InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, _) => {
2064                    layout.push_line_item_to_unbreakable_segment(LineItem::AbsolutelyPositioned(
2065                        layout.current_inline_box_identifier(),
2066                        AbsolutelyPositionedLineItem {
2067                            absolutely_positioned_box: positioned_box.clone(),
2068                            preceding_line_content_would_produce_phantom_line: layout
2069                                .current_line
2070                                .is_phantom() &&
2071                                layout.current_line_segment.is_phantom(),
2072                        },
2073                    ));
2074                },
2075                InlineItem::OutOfFlowFloatBox(float_box) => {
2076                    float_box.borrow().layout_into_line_items(&mut layout);
2077                },
2078                InlineItem::BlockLevel(block_level) => {
2079                    block_level.borrow().layout_into_line_items(&mut layout);
2080                },
2081            }
2082        }
2083
2084        layout.finish_last_line();
2085        let (content_block_size, collapsible_margins_in_children, baselines) =
2086            layout.placement_state.finish();
2087
2088        IndependentFormattingContextLayoutResult {
2089            fragments: layout.fragments,
2090            content_block_size,
2091            collapsible_margins_in_children,
2092            baselines,
2093            depends_on_block_constraints: layout.depends_on_block_constraints,
2094            content_inline_size_for_table: None,
2095            specific_layout_info: None,
2096        }
2097    }
2098
2099    fn next_character_prevents_soft_wrap_opportunity(&self, index: usize) -> bool {
2100        let Some(character) = self.text_content[index..].chars().nth(1) else {
2101            return false;
2102        };
2103        char_prevents_soft_wrap_opportunity_when_before_or_after_atomic(character)
2104    }
2105
2106    fn previous_character_prevents_soft_wrap_opportunity(&self, index: usize) -> bool {
2107        let Some(character) = self.text_content[0..index].chars().next_back() else {
2108            return false;
2109        };
2110        char_prevents_soft_wrap_opportunity_when_before_or_after_atomic(character)
2111    }
2112
2113    pub(crate) fn find_block_margin_collapsing_with_parent(
2114        &self,
2115        layout_context: &LayoutContext,
2116        collected_margin: &mut CollapsedMargin,
2117        containing_block_for_children: &ContainingBlock,
2118    ) -> bool {
2119        // Margins can't collapse through line boxes, unless they are phantom line boxes.
2120        // <https://drafts.csswg.org/css-inline-3/#invisible-line-boxes>
2121        // > Line boxes that contain no text, no preserved white space, no inline boxes with non-zero
2122        // > inline-axis margins, padding, or borders, and no other in-flow content (such as atomic
2123        // > inlines or ruby annotations), and do not end with a forced line break are phantom line boxes.
2124        let mut nesting_levels_from_nonzero_end_pbm: u32 = 1;
2125        let mut items_iter = self.inline_items.iter();
2126        items_iter.all(|inline_item| match inline_item {
2127            InlineItem::StartInlineBox(inline_box) => {
2128                let pbm = inline_box
2129                    .borrow()
2130                    .layout_style()
2131                    .padding_border_margin(containing_block_for_children);
2132                if pbm.padding.inline_end.is_zero() &&
2133                    pbm.border.inline_end.is_zero() &&
2134                    pbm.margin.inline_end.auto_is(Au::zero).is_zero()
2135                {
2136                    nesting_levels_from_nonzero_end_pbm += 1;
2137                } else {
2138                    nesting_levels_from_nonzero_end_pbm = 0;
2139                }
2140                pbm.padding.inline_start.is_zero() &&
2141                    pbm.border.inline_start.is_zero() &&
2142                    pbm.margin.inline_start.auto_is(Au::zero).is_zero()
2143            },
2144            InlineItem::EndInlineBox => {
2145                if nesting_levels_from_nonzero_end_pbm == 0 {
2146                    false
2147                } else {
2148                    nesting_levels_from_nonzero_end_pbm -= 1;
2149                    true
2150                }
2151            },
2152            InlineItem::TextRun(text_run) => {
2153                let text_run = &*text_run.borrow();
2154                let parent_style = text_run.inline_styles.style.borrow();
2155                text_run.items.iter().all(|item| match item {
2156                    TextRunItem::LineBreak { .. } => false,
2157                    TextRunItem::Tab { .. } => false,
2158                    TextRunItem::TextSegment(segment) => segment.runs.iter().all(|run| {
2159                        run.is_whitespace() &&
2160                            !matches!(
2161                                parent_style.get_inherited_text().white_space_collapse,
2162                                WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
2163                            )
2164                    }),
2165                })
2166            },
2167            InlineItem::OutOfFlowAbsolutelyPositionedBox(..) => true,
2168            InlineItem::OutOfFlowFloatBox(..) => true,
2169            InlineItem::Atomic(..) => false,
2170            InlineItem::BlockLevel(block_level) => block_level
2171                .borrow()
2172                .find_block_margin_collapsing_with_parent(
2173                    layout_context,
2174                    collected_margin,
2175                    containing_block_for_children,
2176                ),
2177        })
2178    }
2179
2180    pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
2181        let mut parent_box_stack = Vec::new();
2182        let current_parent_box = |parent_box_stack: &[WeakLayoutBox]| {
2183            parent_box_stack.last().unwrap_or(&layout_box).clone()
2184        };
2185        for inline_item in &self.inline_items {
2186            match inline_item {
2187                InlineItem::StartInlineBox(inline_box) => {
2188                    inline_box
2189                        .borrow_mut()
2190                        .base
2191                        .parent_box
2192                        .replace(current_parent_box(&parent_box_stack));
2193                    parent_box_stack.push(WeakLayoutBox::InlineLevel(
2194                        WeakInlineItem::StartInlineBox(inline_box.downgrade()),
2195                    ));
2196                },
2197                InlineItem::EndInlineBox => {
2198                    parent_box_stack.pop();
2199                },
2200                InlineItem::TextRun(text_run) => {
2201                    text_run
2202                        .borrow_mut()
2203                        .parent_box
2204                        .replace(current_parent_box(&parent_box_stack));
2205                },
2206                _ => inline_item.with_base_mut(|base| {
2207                    base.parent_box
2208                        .replace(current_parent_box(&parent_box_stack));
2209                }),
2210            }
2211        }
2212    }
2213
2214    pub(crate) fn next_tab_stop_after_inline_advance(&self, current_inline_advance: Au) -> Au {
2215        let Some(font) = self.default_font.as_ref() else {
2216            return Au::zero();
2217        };
2218
2219        let tab_stop_advance = *self.tab_stop_advance.get_or_init(|| {
2220            let root_style = self.shared_inline_styles.style.borrow();
2221            let inherited_text_style = root_style.get_inherited_text();
2222            let font_size = root_style.get_font().font_size.computed_size().into();
2223            let letter_spacing = inherited_text_style
2224                .letter_spacing
2225                .0
2226                .to_used_value(font_size);
2227            let word_spacing = inherited_text_style.word_spacing.to_used_value(font_size);
2228
2229            match root_style.get_inherited_text().tab_size {
2230                // Each "space" character in the tab is considered both a letter and a word separator for
2231                // the purposes of applying word spacing and letter spacing.
2232                style::values::generics::length::LengthOrNumber::Number(number_of_spaces) => {
2233                    (font.metrics.space_advance + word_spacing + letter_spacing)
2234                        .scale_by(number_of_spaces.0)
2235                },
2236                // When a length is provided we do not apply word spacing or letter spacing.
2237                style::values::generics::length::LengthOrNumber::Length(length) => length.into(),
2238            }
2239        });
2240
2241        if tab_stop_advance.is_zero() {
2242            return Au::zero();
2243        }
2244
2245        // From <https://drafts.csswg.org/css-text-4/#ref-for-tab-size-dfn>
2246        // > If this distance is less than 0.5ch, then the subsequent tab stop is used instead.
2247        // From <https://drafts.csswg.org/css-values/#ch>
2248        // > In the cases where it is impossible or impractical to determine the measure of the “0”
2249        // > glyph, it must be assumed to be 0.5em wide by 1em tall.
2250        let half_ch_advance = font
2251            .metrics
2252            .zero_horizontal_advance
2253            .unwrap_or(font.metrics.em_size.scale_by(0.5))
2254            .scale_by(0.5);
2255        let number_of_tab_stops =
2256            (current_inline_advance + half_ch_advance).to_f32_px() / tab_stop_advance.to_f32_px();
2257        let number_of_tab_stops = number_of_tab_stops.ceil();
2258        tab_stop_advance.scale_by(number_of_tab_stops) - current_inline_advance
2259    }
2260}
2261
2262impl InlineContainerState {
2263    fn new(
2264        style: ServoArc<ComputedValues>,
2265        flags: InlineContainerStateFlags,
2266        parent_container: Option<&InlineContainerState>,
2267        default_font: Option<FontRef>,
2268    ) -> Self {
2269        let font_metrics = default_font
2270            .as_ref()
2271            .map(|font| font.metrics.clone())
2272            .unwrap_or_else(FontMetrics::empty);
2273        let mut baseline_offset = Au::zero();
2274        let mut strut_block_sizes = {
2275            Self::get_block_sizes_with_style(
2276                effective_baseline_shift(&style, parent_container),
2277                &style,
2278                &font_metrics,
2279                &font_metrics,
2280                &flags,
2281            )
2282        };
2283
2284        if let Some(parent_container) = parent_container {
2285            // The baseline offset from `vertical-align` might adjust where our block size contribution is
2286            // within the line.
2287            baseline_offset = parent_container.get_cumulative_baseline_offset_for_child(
2288                style.clone_alignment_baseline(),
2289                style.clone_baseline_shift(),
2290                &strut_block_sizes,
2291            );
2292            strut_block_sizes.adjust_for_baseline_offset(baseline_offset);
2293        }
2294
2295        let mut nested_block_sizes = parent_container
2296            .map(|container| container.nested_strut_block_sizes.clone())
2297            .unwrap_or_else(LineBlockSizes::zero);
2298        if flags.contains(InlineContainerStateFlags::CREATE_STRUT) {
2299            nested_block_sizes.max_assign(&strut_block_sizes);
2300        }
2301
2302        Self {
2303            style,
2304            flags,
2305            has_content: RefCell::new(false),
2306            nested_strut_block_sizes: nested_block_sizes,
2307            strut_block_sizes,
2308            baseline_offset,
2309            default_font,
2310            font_metrics,
2311        }
2312    }
2313
2314    fn get_block_sizes_with_style(
2315        baseline_shift: BaselineShift,
2316        style: &ComputedValues,
2317        font_metrics: &FontMetrics,
2318        font_metrics_of_first_font: &FontMetrics,
2319        flags: &InlineContainerStateFlags,
2320    ) -> LineBlockSizes {
2321        let line_height = line_height(style, font_metrics, flags);
2322
2323        if !is_baseline_relative(baseline_shift) {
2324            return LineBlockSizes {
2325                line_height,
2326                baseline_relative_size_for_line_height: None,
2327                size_for_baseline_positioning: BaselineRelativeSize::zero(),
2328            };
2329        }
2330
2331        // From https://drafts.csswg.org/css-inline/#inline-height
2332        // > If line-height computes to `normal` and either `text-box-edge` is `leading` or this
2333        // > is the root inline box, the font’s line gap metric may also be incorporated
2334        // > into A and D by adding half to each side as half-leading.
2335        //
2336        // `text-box-edge` isn't implemented (and this is a draft specification), so it's
2337        // always effectively `leading`, which means we always take into account the line gap
2338        // when `line-height` is normal.
2339        let mut ascent = font_metrics.ascent;
2340        let mut descent = font_metrics.descent;
2341        if style.get_font().line_height == LineHeight::Normal {
2342            let half_leading_from_line_gap =
2343                (font_metrics.line_gap - descent - ascent).scale_by(0.5);
2344            ascent += half_leading_from_line_gap;
2345            descent += half_leading_from_line_gap;
2346        }
2347
2348        // The ascent and descent we use for computing the line's final line height isn't
2349        // the same the ascent and descent we use for finding the baseline. For finding
2350        // the baseline we want the content rect.
2351        let size_for_baseline_positioning = BaselineRelativeSize { ascent, descent };
2352
2353        // From https://drafts.csswg.org/css-inline/#inline-height
2354        // > When its computed line-height is not normal, its layout bounds are derived solely
2355        // > from metrics of its first available font (ignoring glyphs from other fonts), and
2356        // > leading is used to adjust the effective A and D to add up to the used line-height.
2357        // > Calculate the leading L as L = line-height - (A + D). Half the leading (its
2358        // > half-leading) is added above A of the first available font, and the other half
2359        // > below D of the first available font, giving an effective ascent above the baseline
2360        // > of A′ = A + L/2, and an effective descent of D′ = D + L/2.
2361        //
2362        // Note that leading might be negative here and the line-height might be zero. In
2363        // the case where the height is zero, ascent and descent will move to the same
2364        // point in the block axis.  Even though the contribution to the line height is
2365        // zero in this case, the line may get some height when taking them into
2366        // considering with other zero line height boxes that converge on other block axis
2367        // locations when using the above formula.
2368        if style.get_font().line_height != LineHeight::Normal {
2369            ascent = font_metrics_of_first_font.ascent;
2370            descent = font_metrics_of_first_font.descent;
2371            let half_leading = (line_height - (ascent + descent)).scale_by(0.5);
2372            // We want the sum of `ascent` and `descent` to equal `line_height`.
2373            // If we just add `half_leading` to both, then we may not get `line_height`
2374            // due to precision limitations of `Au`. Instead, we set `descent` to
2375            // the value that will guarantee the correct sum.
2376            ascent += half_leading;
2377            descent = line_height - ascent;
2378        }
2379
2380        LineBlockSizes {
2381            line_height,
2382            baseline_relative_size_for_line_height: Some(BaselineRelativeSize { ascent, descent }),
2383            size_for_baseline_positioning,
2384        }
2385    }
2386
2387    fn get_block_size_contribution(
2388        &self,
2389        baseline_shift: BaselineShift,
2390        font_metrics: &FontMetrics,
2391        font_metrics_of_first_font: &FontMetrics,
2392    ) -> LineBlockSizes {
2393        Self::get_block_sizes_with_style(
2394            baseline_shift,
2395            &self.style,
2396            font_metrics,
2397            font_metrics_of_first_font,
2398            &self.flags,
2399        )
2400    }
2401
2402    fn get_cumulative_baseline_offset_for_child(
2403        &self,
2404        child_alignment_baseline: AlignmentBaseline,
2405        child_baseline_shift: BaselineShift,
2406        child_block_size: &LineBlockSizes,
2407    ) -> Au {
2408        let block_size = self.get_block_size_contribution(
2409            child_baseline_shift.clone(),
2410            &self.font_metrics,
2411            &self.font_metrics,
2412        );
2413        self.baseline_offset +
2414            match child_alignment_baseline {
2415                AlignmentBaseline::Baseline => Au::zero(),
2416                AlignmentBaseline::TextTop => {
2417                    child_block_size.size_for_baseline_positioning.ascent - self.font_metrics.ascent
2418                },
2419                AlignmentBaseline::Middle => {
2420                    // "Align the vertical midpoint of the box with the baseline of the parent
2421                    // box plus half the x-height of the parent."
2422                    (child_block_size.size_for_baseline_positioning.ascent -
2423                        child_block_size.size_for_baseline_positioning.descent -
2424                        self.font_metrics.x_height)
2425                        .scale_by(0.5)
2426                },
2427                AlignmentBaseline::TextBottom => {
2428                    self.font_metrics.descent -
2429                        child_block_size.size_for_baseline_positioning.descent
2430                },
2431                AlignmentBaseline::Alphabetic |
2432                AlignmentBaseline::Ideographic |
2433                AlignmentBaseline::Central |
2434                AlignmentBaseline::Mathematical |
2435                AlignmentBaseline::Hanging => {
2436                    unreachable!("Got alignment-baseline value that should be disabled in Stylo")
2437                },
2438            } +
2439            match child_baseline_shift {
2440                // `top` and `bottom are not actually relative to the baseline, but this value is unused
2441                // in those cases.
2442                // TODO: We should distinguish these from `baseline` in order to implement "aligned subtrees" properly.
2443                // See https://drafts.csswg.org/css2/#aligned-subtree.
2444                BaselineShift::Keyword(
2445                    BaselineShiftKeyword::Top |
2446                    BaselineShiftKeyword::Bottom |
2447                    BaselineShiftKeyword::Center,
2448                ) => Au::zero(),
2449                BaselineShift::Keyword(BaselineShiftKeyword::Sub) => {
2450                    block_size.resolve().scale_by(FONT_SUBSCRIPT_OFFSET_RATIO)
2451                },
2452                BaselineShift::Keyword(BaselineShiftKeyword::Super) => {
2453                    -block_size.resolve().scale_by(FONT_SUPERSCRIPT_OFFSET_RATIO)
2454                },
2455                BaselineShift::Length(length_percentage) => {
2456                    -length_percentage.to_used_value(child_block_size.line_height)
2457                },
2458            }
2459    }
2460}
2461
2462impl IndependentFormattingContext {
2463    fn layout_into_line_items(
2464        &self,
2465        layout: &mut InlineFormattingContextLayout,
2466        offset_in_text: usize,
2467        bidi_level: Level,
2468    ) {
2469        // We need to know the inline size of the atomic before deciding whether to do the line break.
2470        let mut child_positioning_context = PositioningContext::default();
2471        let IndependentFloatOrAtomicLayoutResult {
2472            mut fragment,
2473            baselines,
2474            pbm_sums,
2475        } = self.layout_float_or_atomic_inline(
2476            layout.layout_context,
2477            &mut child_positioning_context,
2478            layout.containing_block(),
2479        );
2480
2481        // If this Fragment's layout depends on the block size of the containing block,
2482        // then the entire layout of the inline formatting context does as well.
2483        layout.depends_on_block_constraints |= fragment.base.flags.contains(
2484            FragmentFlags::SIZE_DEPENDS_ON_BLOCK_CONSTRAINTS_AND_CAN_BE_CHILD_OF_FLEX_ITEM,
2485        );
2486
2487        // Offset the content rectangle by the physical offset of the padding, border, and margin.
2488        let container_writing_mode = layout.containing_block().style.writing_mode;
2489        let pbm_physical_offset = pbm_sums
2490            .start_offset()
2491            .to_physical_size(container_writing_mode);
2492        fragment.base.rect.origin += pbm_physical_offset.to_vector();
2493
2494        // Apply baselines.
2495        fragment = fragment.with_baselines(baselines);
2496
2497        // Lay out absolutely positioned children if this new atomic establishes a containing block
2498        // for absolutes.
2499        let positioning_context = if self.is_replaced() {
2500            None
2501        } else {
2502            if fragment
2503                .style()
2504                .establishes_containing_block_for_absolute_descendants(fragment.base.flags)
2505            {
2506                child_positioning_context
2507                    .layout_collected_children(layout.layout_context, &mut fragment);
2508            }
2509            Some(child_positioning_context)
2510        };
2511
2512        if layout.text_wrap_mode == TextWrapMode::Wrap &&
2513            !layout
2514                .ifc
2515                .previous_character_prevents_soft_wrap_opportunity(offset_in_text)
2516        {
2517            layout.process_soft_wrap_opportunity();
2518        }
2519
2520        let size = pbm_sums.sum() + fragment.base.rect.size.to_logical(container_writing_mode);
2521        let baseline_offset = self
2522            .pick_baseline(&fragment.baselines(container_writing_mode))
2523            .map(|baseline| pbm_sums.block_start + baseline)
2524            .unwrap_or(size.block);
2525
2526        let (block_sizes, baseline_offset_in_parent) =
2527            self.get_block_sizes_and_baseline_offset(layout, size.block, baseline_offset);
2528        layout.update_unbreakable_segment_for_new_content(
2529            &block_sizes,
2530            size.inline,
2531            SegmentContentFlags::empty(),
2532        );
2533
2534        let fragment = ArcRefCell::new(fragment);
2535        self.base.set_fragment(Fragment::Box(fragment.clone()));
2536
2537        layout.push_line_item_to_unbreakable_segment(LineItem::Atomic(
2538            layout.current_inline_box_identifier(),
2539            AtomicLineItem {
2540                fragment,
2541                size,
2542                positioning_context,
2543                baseline_offset_in_parent,
2544                baseline_offset_in_item: baseline_offset,
2545                bidi_level,
2546            },
2547        ));
2548
2549        // If there's a soft wrap opportunity following this atomic, defer a soft wrap opportunity
2550        // for when we next process text content.
2551        if !layout
2552            .ifc
2553            .next_character_prevents_soft_wrap_opportunity(offset_in_text)
2554        {
2555            layout.have_deferred_soft_wrap_opportunity = true;
2556        }
2557    }
2558
2559    /// Picks either the first or the last baseline, depending on `baseline-source`.
2560    /// TODO: clarify that this is not to be used for box alignment in flex/grid
2561    /// <https://drafts.csswg.org/css-inline/#baseline-source>
2562    fn pick_baseline(&self, baselines: &Baselines) -> Option<Au> {
2563        match self.style().clone_baseline_source() {
2564            BaselineSource::First => baselines.first,
2565            BaselineSource::Last => baselines.last,
2566            BaselineSource::Auto if self.is_block_container() => baselines.last,
2567            BaselineSource::Auto => baselines.first,
2568        }
2569    }
2570
2571    fn get_block_sizes_and_baseline_offset(
2572        &self,
2573        ifc: &InlineFormattingContextLayout,
2574        block_size: Au,
2575        baseline_offset_in_content_area: Au,
2576    ) -> (LineBlockSizes, Au) {
2577        let mut contribution = if !is_baseline_relative(self.style().clone_baseline_shift()) {
2578            LineBlockSizes {
2579                line_height: block_size,
2580                baseline_relative_size_for_line_height: None,
2581                size_for_baseline_positioning: BaselineRelativeSize::zero(),
2582            }
2583        } else {
2584            let baseline_relative_size = BaselineRelativeSize {
2585                ascent: baseline_offset_in_content_area,
2586                descent: block_size - baseline_offset_in_content_area,
2587            };
2588            LineBlockSizes {
2589                line_height: block_size,
2590                baseline_relative_size_for_line_height: Some(baseline_relative_size.clone()),
2591                size_for_baseline_positioning: baseline_relative_size,
2592            }
2593        };
2594
2595        let style = self.style();
2596        let baseline_offset = ifc
2597            .current_inline_container_state()
2598            .get_cumulative_baseline_offset_for_child(
2599                style.clone_alignment_baseline(),
2600                style.clone_baseline_shift(),
2601                &contribution,
2602            );
2603        contribution.adjust_for_baseline_offset(baseline_offset);
2604
2605        (contribution, baseline_offset)
2606    }
2607}
2608
2609impl FloatBox {
2610    fn layout_into_line_items(&self, layout: &mut InlineFormattingContextLayout) {
2611        let fragment = ArcRefCell::new(self.layout(
2612            layout.layout_context,
2613            layout.positioning_context,
2614            layout.placement_state.containing_block,
2615        ));
2616
2617        self.contents
2618            .base
2619            .set_fragment(Fragment::Box(fragment.clone()));
2620        layout.push_line_item_to_unbreakable_segment(LineItem::Float(
2621            layout.current_inline_box_identifier(),
2622            FloatLineItem {
2623                fragment,
2624                needs_placement: true,
2625            },
2626        ));
2627    }
2628}
2629
2630fn place_pending_floats(ifc: &mut InlineFormattingContextLayout, line_items: &mut [LineItem]) {
2631    for item in line_items.iter_mut() {
2632        if let LineItem::Float(_, float_line_item) = item {
2633            if float_line_item.needs_placement {
2634                ifc.place_float_fragment(&mut float_line_item.fragment.borrow_mut());
2635            }
2636        }
2637    }
2638}
2639
2640fn line_height(
2641    parent_style: &ComputedValues,
2642    font_metrics: &FontMetrics,
2643    flags: &InlineContainerStateFlags,
2644) -> Au {
2645    let font = parent_style.get_font();
2646    let font_size = font.font_size.computed_size();
2647    let mut line_height = match font.line_height {
2648        LineHeight::Normal => font_metrics.line_gap,
2649        LineHeight::Number(number) => (font_size * number.0).into(),
2650        LineHeight::Length(length) => length.0.into(),
2651    };
2652
2653    // The line height of a single-line text input's inner text container is clamped to
2654    // the size of `normal`.
2655    // <https://html.spec.whatwg.org/multipage/#the-input-element-as-a-text-entry-widget>
2656    if flags.contains(InlineContainerStateFlags::IS_SINGLE_LINE_TEXT_INPUT) {
2657        line_height.max_assign(font_metrics.line_gap);
2658    }
2659
2660    line_height
2661}
2662
2663fn effective_baseline_shift(
2664    style: &ComputedValues,
2665    container: Option<&InlineContainerState>,
2666) -> BaselineShift {
2667    if container.is_none() {
2668        // If we are at the root of the inline formatting context, we shouldn't use the
2669        // computed `baseline-shift`, since it has no effect on the contents of this IFC
2670        // (it can just affect how the block container is aligned within the parent IFC).
2671        BaselineShift::zero()
2672    } else {
2673        style.clone_baseline_shift()
2674    }
2675}
2676
2677fn is_baseline_relative(baseline_shift: BaselineShift) -> bool {
2678    !matches!(
2679        baseline_shift,
2680        BaselineShift::Keyword(
2681            BaselineShiftKeyword::Top | BaselineShiftKeyword::Bottom | BaselineShiftKeyword::Center
2682        )
2683    )
2684}
2685
2686/// Whether or not a strut should be created for an inline container. Normally
2687/// all inline containers get struts. In quirks mode this isn't always the case
2688/// though.
2689///
2690/// From <https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk>
2691///
2692/// > ### § 3.3. The line height calculation quirk
2693/// > In quirks mode and limited-quirks mode, an inline box that matches the following
2694/// > conditions, must, for the purpose of line height calculation, act as if the box had a
2695/// > line-height of zero.
2696/// >
2697/// >  - The border-top-width, border-bottom-width, padding-top and padding-bottom
2698/// >    properties have a used value of zero and the box has a vertical writing mode, or the
2699/// >    border-right-width, border-left-width, padding-right and padding-left properties have
2700/// >    a used value of zero and the box has a horizontal writing mode.
2701/// >  - It either contains no text or it contains only collapsed whitespace.
2702/// >
2703/// > ### § 3.4. The blocks ignore line-height quirk
2704/// > In quirks mode and limited-quirks mode, for a block container element whose content is
2705/// > composed of inline-level elements, the element’s line-height must be ignored for the
2706/// > purpose of calculating the minimal height of line boxes within the element.
2707///
2708/// Since we incorporate the size of the strut into the line-height calculation when
2709/// adding text, we can simply not incorporate the strut at the start of inline box
2710/// processing. This also works the same for the root of the IFC.
2711fn inline_container_needs_strut(
2712    style: &ComputedValues,
2713    layout_context: &LayoutContext,
2714    pbm: Option<&PaddingBorderMargin>,
2715) -> bool {
2716    if layout_context.style_context.quirks_mode() == QuirksMode::NoQuirks {
2717        return true;
2718    }
2719
2720    // This is not in a standard yet, but all browsers disable this quirk for list items.
2721    // See https://github.com/whatwg/quirks/issues/38.
2722    if style.get_box().display.is_list_item() {
2723        return true;
2724    }
2725
2726    pbm.is_some_and(|pbm| !pbm.padding_border_sums.inline.is_zero())
2727}
2728
2729impl ComputeInlineContentSizes for InlineFormattingContext {
2730    // This works on an already-constructed `InlineFormattingContext`,
2731    // Which would have to change if/when
2732    // `BlockContainer::construct` parallelize their construction.
2733    fn compute_inline_content_sizes(
2734        &self,
2735        layout_context: &LayoutContext,
2736        constraint_space: &ConstraintSpace,
2737    ) -> InlineContentSizesResult {
2738        ContentSizesComputation::compute(self, layout_context, constraint_space)
2739    }
2740}
2741
2742/// A struct which takes care of computing [`ContentSizes`] for an [`InlineFormattingContext`].
2743struct ContentSizesComputation<'layout_data> {
2744    layout_context: &'layout_data LayoutContext<'layout_data>,
2745    constraint_space: &'layout_data ConstraintSpace<'layout_data>,
2746    paragraph: ContentSizes,
2747    current_line: ContentSizes,
2748    /// Size for whitespace pending to be added to this line.
2749    pending_whitespace: ContentSizes,
2750    /// The size of the not yet cleared floats in the inline axis of the containing block.
2751    uncleared_floats: LogicalSides1D<ContentSizes>,
2752    /// The size of the already cleared floats in the inline axis of the containing block.
2753    cleared_floats: LogicalSides1D<ContentSizes>,
2754    /// Whether or not the current line has seen any content (excluding collapsed whitespace),
2755    /// when sizing under a min-content constraint.
2756    had_content_yet_for_min_content: bool,
2757    /// Whether or not the current line has seen any content (excluding collapsed whitespace),
2758    /// when sizing under a max-content constraint.
2759    had_content_yet_for_max_content: bool,
2760    /// Stack of ending padding, margin, and border to add to the length
2761    /// when an inline box finishes.
2762    ending_inline_pbm_stack: Vec<Au>,
2763    /// Whether the inline content size depends on block constraints.
2764    depends_on_block_constraints: bool,
2765}
2766
2767impl<'layout_data> ContentSizesComputation<'layout_data> {
2768    fn traverse(
2769        mut self,
2770        inline_formatting_context: &InlineFormattingContext,
2771    ) -> InlineContentSizesResult {
2772        self.add_inline_size(
2773            inline_formatting_context.inline_start_for_first_line(self.constraint_space.into()),
2774        );
2775        for inline_item in &inline_formatting_context.inline_items {
2776            self.process_item(inline_item, inline_formatting_context);
2777        }
2778        self.forced_line_break();
2779        self.flush_floats();
2780
2781        InlineContentSizesResult {
2782            sizes: self.paragraph,
2783            depends_on_block_constraints: self.depends_on_block_constraints,
2784        }
2785    }
2786
2787    fn process_item(
2788        &mut self,
2789        inline_item: &InlineItem,
2790        inline_formatting_context: &InlineFormattingContext,
2791    ) {
2792        match inline_item {
2793            InlineItem::StartInlineBox(inline_box) => {
2794                // For margins and paddings, a cyclic percentage is resolved against zero
2795                // for determining intrinsic size contributions.
2796                // https://drafts.csswg.org/css-sizing-3/#min-percentage-contribution
2797                let inline_box = inline_box.borrow();
2798                let zero = Au::zero();
2799                let writing_mode = self.constraint_space.style.writing_mode;
2800                let layout_style = inline_box.layout_style();
2801                let padding = layout_style
2802                    .padding(writing_mode)
2803                    .percentages_relative_to(zero);
2804                let border = layout_style.border_width(writing_mode);
2805                let margin = inline_box
2806                    .base
2807                    .style
2808                    .margin(writing_mode)
2809                    .percentages_relative_to(zero)
2810                    .auto_is(Au::zero);
2811
2812                let pbm = margin + padding + border;
2813                self.add_inline_size(pbm.inline_start);
2814                self.ending_inline_pbm_stack.push(pbm.inline_end);
2815            },
2816            InlineItem::EndInlineBox => {
2817                let length = self.ending_inline_pbm_stack.pop().unwrap_or_else(Au::zero);
2818                self.add_inline_size(length);
2819            },
2820            InlineItem::TextRun(text_run) => {
2821                let text_run = &*text_run.borrow();
2822                let parent_style = text_run.inline_styles.style.borrow();
2823                for item in text_run.items.iter() {
2824                    match item {
2825                        TextRunItem::LineBreak { .. } => {
2826                            // If this run is a forced line break, we *must* break the line
2827                            // and start measuring from the inline origin once more.
2828                            self.forced_line_break();
2829                        },
2830                        TextRunItem::Tab { .. } => {
2831                            self.process_preserved_tab(&parent_style, inline_formatting_context)
2832                        },
2833                        TextRunItem::TextSegment(segment) => {
2834                            self.process_text_segment(&parent_style, segment)
2835                        },
2836                    }
2837                }
2838            },
2839            InlineItem::Atomic(atomic, offset_in_text, _level) => {
2840                // TODO: need to handle TextWrapMode::Nowrap.
2841                if self.had_content_yet_for_min_content &&
2842                    !inline_formatting_context
2843                        .previous_character_prevents_soft_wrap_opportunity(*offset_in_text)
2844                {
2845                    self.line_break_opportunity();
2846                }
2847
2848                self.commit_pending_whitespace();
2849                let outer = self.outer_inline_content_sizes_of_float_or_atomic(&atomic.borrow());
2850                self.current_line += outer;
2851
2852                // TODO: need to handle TextWrapMode::Nowrap.
2853                if !inline_formatting_context
2854                    .next_character_prevents_soft_wrap_opportunity(*offset_in_text)
2855                {
2856                    self.line_break_opportunity();
2857                }
2858            },
2859            InlineItem::OutOfFlowFloatBox(float_box) => {
2860                let float_box = float_box.borrow();
2861                let sizes = self.outer_inline_content_sizes_of_float_or_atomic(&float_box.contents);
2862                let style = &float_box.contents.style();
2863                let container_writing_mode = self.constraint_space.style.writing_mode;
2864                let clear =
2865                    Clear::from_style_and_container_writing_mode(style, container_writing_mode);
2866                self.clear_floats(clear);
2867                let float_side =
2868                    FloatSide::from_style_and_container_writing_mode(style, container_writing_mode);
2869                match float_side.expect("A float box needs to float to some side") {
2870                    FloatSide::InlineStart => self.uncleared_floats.start.union_assign(&sizes),
2871                    FloatSide::InlineEnd => self.uncleared_floats.end.union_assign(&sizes),
2872                }
2873            },
2874            InlineItem::BlockLevel(block_level) => {
2875                self.forced_line_break();
2876                self.flush_floats();
2877                let inline_content_sizes_result =
2878                    compute_inline_content_sizes_for_block_level_boxes(
2879                        std::slice::from_ref(block_level),
2880                        self.layout_context,
2881                        &self.constraint_space.into(),
2882                    );
2883                self.depends_on_block_constraints |=
2884                    inline_content_sizes_result.depends_on_block_constraints;
2885                self.current_line = inline_content_sizes_result.sizes;
2886                self.forced_line_break();
2887            },
2888            InlineItem::OutOfFlowAbsolutelyPositionedBox(..) => {},
2889        }
2890    }
2891
2892    fn process_text_segment(
2893        &mut self,
2894        parent_style: &AtomicRef<'_, ServoArc<ComputedValues>>,
2895        segment: &TextRunSegment,
2896    ) {
2897        let style_text = parent_style.get_inherited_text();
2898        let can_wrap = style_text.text_wrap_mode == TextWrapMode::Wrap;
2899
2900        // TODO: This should take account whether or not the first and last character prevent
2901        // linebreaks after atomics as in layout.
2902        let break_at_start = segment.break_at_start && self.had_content_yet_for_min_content;
2903
2904        for (run_index, run) in segment.runs.iter().enumerate() {
2905            // Break before each unbreakable run in this TextRun, except the first unless the
2906            // linebreaker was set to break before the first run.
2907            if can_wrap && (run_index != 0 || break_at_start) {
2908                self.line_break_opportunity();
2909            }
2910
2911            let advance = run.total_advance();
2912            if run.is_whitespace() {
2913                if !matches!(
2914                    style_text.white_space_collapse,
2915                    WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces
2916                ) {
2917                    if self.had_content_yet_for_min_content {
2918                        if can_wrap {
2919                            self.line_break_opportunity();
2920                        } else {
2921                            self.pending_whitespace.min_content += advance;
2922                        }
2923                    }
2924                    if self.had_content_yet_for_max_content {
2925                        self.pending_whitespace.max_content += advance;
2926                    }
2927                    continue;
2928                }
2929                if can_wrap {
2930                    self.pending_whitespace.max_content += advance;
2931                    self.commit_pending_whitespace();
2932                    self.line_break_opportunity();
2933                    continue;
2934                }
2935            }
2936
2937            self.commit_pending_whitespace();
2938            self.add_inline_size(advance);
2939
2940            // Typically whitespace glyphs are placed in a separate store,
2941            // but for `white-space: break-spaces` we place the first whitespace
2942            // with the preceding text. That prevents a line break before that
2943            // first space, but we still need to allow a line break after it.
2944            if can_wrap && run.ends_with_whitespace() {
2945                self.line_break_opportunity();
2946            }
2947        }
2948    }
2949
2950    fn process_preserved_tab(
2951        &mut self,
2952        parent_style: &AtomicRef<'_, ServoArc<ComputedValues>>,
2953        inline_formatting_context: &InlineFormattingContext,
2954    ) {
2955        // If there is a preserved tab, that means that all whitespace is preserved.
2956        self.commit_pending_whitespace();
2957
2958        self.current_line.min_content += inline_formatting_context
2959            .next_tab_stop_after_inline_advance(self.current_line.min_content);
2960        self.current_line.max_content += inline_formatting_context
2961            .next_tab_stop_after_inline_advance(self.current_line.max_content);
2962        if parent_style.get_inherited_text().text_wrap_mode == TextWrapMode::Wrap {
2963            self.line_break_opportunity();
2964        }
2965    }
2966
2967    fn add_inline_size(&mut self, l: Au) {
2968        self.current_line.min_content += l;
2969        self.current_line.max_content += l;
2970    }
2971
2972    fn line_break_opportunity(&mut self) {
2973        // Clear the pending whitespace, assuming that at the end of the line
2974        // it needs to either hang or be removed. If that isn't the case,
2975        // `commit_pending_whitespace()` should be called first.
2976        self.pending_whitespace.min_content = Au::zero();
2977        let current_min_content = mem::take(&mut self.current_line.min_content);
2978        self.paragraph.min_content.max_assign(current_min_content);
2979        self.had_content_yet_for_min_content = false;
2980    }
2981
2982    fn forced_line_break(&mut self) {
2983        // Handle the line break for min-content sizes.
2984        self.line_break_opportunity();
2985
2986        // Repeat the same logic, but now for max-content sizes.
2987        self.pending_whitespace.max_content = Au::zero();
2988        let current_max_content = mem::take(&mut self.current_line.max_content);
2989        self.paragraph.max_content.max_assign(current_max_content);
2990        self.had_content_yet_for_max_content = false;
2991    }
2992
2993    fn commit_pending_whitespace(&mut self) {
2994        self.current_line += mem::take(&mut self.pending_whitespace);
2995        self.had_content_yet_for_min_content = true;
2996        self.had_content_yet_for_max_content = true;
2997    }
2998
2999    fn outer_inline_content_sizes_of_float_or_atomic(
3000        &mut self,
3001        context: &IndependentFormattingContext,
3002    ) -> ContentSizes {
3003        let result = context.outer_inline_content_sizes(
3004            self.layout_context,
3005            &self.constraint_space.into(),
3006            &LogicalVec2::zero(),
3007            false, /* auto_block_size_stretches_to_containing_block */
3008        );
3009        self.depends_on_block_constraints |= result.depends_on_block_constraints;
3010        result.sizes
3011    }
3012
3013    fn clear_floats(&mut self, clear: Clear) {
3014        match clear {
3015            Clear::InlineStart => {
3016                let start_floats = mem::take(&mut self.uncleared_floats.start);
3017                self.cleared_floats.start.max_assign(start_floats);
3018            },
3019            Clear::InlineEnd => {
3020                let end_floats = mem::take(&mut self.uncleared_floats.end);
3021                self.cleared_floats.end.max_assign(end_floats);
3022            },
3023            Clear::Both => {
3024                let start_floats = mem::take(&mut self.uncleared_floats.start);
3025                let end_floats = mem::take(&mut self.uncleared_floats.end);
3026                self.cleared_floats.start.max_assign(start_floats);
3027                self.cleared_floats.end.max_assign(end_floats);
3028            },
3029            Clear::None => {},
3030        }
3031    }
3032
3033    fn flush_floats(&mut self) {
3034        self.clear_floats(Clear::Both);
3035        let start_floats = mem::take(&mut self.cleared_floats.start);
3036        let end_floats = mem::take(&mut self.cleared_floats.end);
3037        self.paragraph.union_assign(&start_floats);
3038        self.paragraph.union_assign(&end_floats);
3039    }
3040
3041    /// Compute the [`ContentSizes`] of the given [`InlineFormattingContext`].
3042    fn compute(
3043        inline_formatting_context: &InlineFormattingContext,
3044        layout_context: &'layout_data LayoutContext,
3045        constraint_space: &'layout_data ConstraintSpace,
3046    ) -> InlineContentSizesResult {
3047        Self {
3048            layout_context,
3049            constraint_space,
3050            paragraph: ContentSizes::zero(),
3051            current_line: ContentSizes::zero(),
3052            pending_whitespace: ContentSizes::zero(),
3053            uncleared_floats: LogicalSides1D::default(),
3054            cleared_floats: LogicalSides1D::default(),
3055            had_content_yet_for_min_content: false,
3056            had_content_yet_for_max_content: false,
3057            ending_inline_pbm_stack: Vec::new(),
3058            depends_on_block_constraints: false,
3059        }
3060        .traverse(inline_formatting_context)
3061    }
3062}
3063
3064/// Whether or not this character will rpevent a soft wrap opportunity when it
3065/// comes before or after an atomic inline element.
3066///
3067/// From <https://www.w3.org/TR/css-text-3/#line-break-details>:
3068///
3069/// > For Web-compatibility there is a soft wrap opportunity before and after each
3070/// > replaced element or other atomic inline, even when adjacent to a character that
3071/// > would normally suppress them, including U+00A0 NO-BREAK SPACE. However, with
3072/// > the exception of U+00A0 NO-BREAK SPACE, there must be no soft wrap opportunity
3073/// > between atomic inlines and adjacent characters belonging to the Unicode GL, WJ,
3074/// > or ZWJ line breaking classes.
3075fn char_prevents_soft_wrap_opportunity_when_before_or_after_atomic(character: char) -> bool {
3076    if character == '\u{00A0}' {
3077        return false;
3078    }
3079    matches!(
3080        icu_properties::maps::line_break().get(character),
3081        ICULineBreak::Glue | ICULineBreak::WordJoiner | ICULineBreak::ZWJ
3082    )
3083}