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