Blame servo/components/layout/floats.rs

Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
use app_units::{Au, MAX_AU};
Packit f0b94e
use block::FormattingContextType;
Packit f0b94e
use flow::{Flow, FlowFlags, GetBaseFlow, ImmutableFlowUtils};
Packit f0b94e
use persistent_list::PersistentList;
Packit f0b94e
use std::cmp::{max, min};
Packit f0b94e
use std::fmt;
Packit f0b94e
use style::computed_values::float::T as StyleFloat;
Packit f0b94e
use style::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
Packit f0b94e
use style::values::computed::LengthOrPercentageOrAuto;
Packit f0b94e
Packit f0b94e
/// The kind of float: left or right.
Packit f0b94e
#[derive(Clone, Copy, Debug, Serialize)]
Packit f0b94e
pub enum FloatKind {
Packit f0b94e
    Left,
Packit f0b94e
    Right
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl FloatKind {
Packit f0b94e
    pub fn from_property(property: StyleFloat) -> Option<FloatKind> {
Packit f0b94e
        match property {
Packit f0b94e
            StyleFloat::None => None,
Packit f0b94e
            StyleFloat::Left => Some(FloatKind::Left),
Packit f0b94e
            StyleFloat::Right => Some(FloatKind::Right),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// The kind of clearance: left, right, or both.
Packit f0b94e
#[derive(Clone, Copy)]
Packit f0b94e
pub enum ClearType {
Packit f0b94e
    Left,
Packit f0b94e
    Right,
Packit f0b94e
    Both,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// Information about a single float.
Packit f0b94e
#[derive(Clone, Copy)]
Packit f0b94e
struct Float {
Packit f0b94e
    /// The boundaries of this float.
Packit f0b94e
    bounds: LogicalRect<Au>,
Packit f0b94e
    /// The kind of float: left or right.
Packit f0b94e
    kind: FloatKind,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl fmt::Debug for Float {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        write!(f, "bounds={:?} kind={:?}", self.bounds, self.kind)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// Information about the floats next to a flow.
Packit f0b94e
#[derive(Clone)]
Packit f0b94e
struct FloatList {
Packit f0b94e
    /// Information about each of the floats here.
Packit f0b94e
    floats: PersistentList<Float>,
Packit f0b94e
    /// Cached copy of the maximum block-start offset of the float.
Packit f0b94e
    max_block_start: Option<Au>,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl FloatList {
Packit f0b94e
    fn new() -> FloatList {
Packit f0b94e
        FloatList {
Packit f0b94e
            floats: PersistentList::new(),
Packit f0b94e
            max_block_start: None,
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Returns true if the list is allocated and false otherwise. If false, there are guaranteed
Packit f0b94e
    /// not to be any floats.
Packit f0b94e
    fn is_present(&self) -> bool {
Packit f0b94e
        self.floats.len() > 0
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl fmt::Debug for FloatList {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        write!(f, "max_block_start={:?} floats={}", self.max_block_start, self.floats.len())?;
Packit f0b94e
        for float in self.floats.iter() {
Packit f0b94e
            write!(f, " {:?}", float)?;
Packit f0b94e
        }
Packit f0b94e
        Ok(())
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// All the information necessary to place a float.
Packit f0b94e
pub struct PlacementInfo {
Packit f0b94e
    /// The dimensions of the float.
Packit f0b94e
    pub size: LogicalSize<Au>,
Packit f0b94e
    /// The minimum block-start of the float, as determined by earlier elements.
Packit f0b94e
    pub ceiling: Au,
Packit f0b94e
    /// The maximum inline-end position of the float, generally determined by the containing block.
Packit f0b94e
    pub max_inline_size: Au,
Packit f0b94e
    /// The kind of float.
Packit f0b94e
    pub kind: FloatKind
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl fmt::Debug for PlacementInfo {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        write!(f,
Packit f0b94e
               "size={:?} ceiling={:?} max_inline_size={:?} kind={:?}",
Packit f0b94e
               self.size,
Packit f0b94e
               self.ceiling,
Packit f0b94e
               self.max_inline_size,
Packit f0b94e
               self.kind)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
fn range_intersect(block_start_1: Au, block_end_1: Au, block_start_2: Au, block_end_2: Au) -> (Au, Au) {
Packit f0b94e
    (max(block_start_1, block_start_2), min(block_end_1, block_end_2))
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// Encapsulates information about floats. This is optimized to avoid allocation if there are
Packit f0b94e
/// no floats, and to avoid copying when translating the list of floats downward.
Packit f0b94e
#[derive(Clone)]
Packit f0b94e
pub struct Floats {
Packit f0b94e
    /// The list of floats.
Packit f0b94e
    list: FloatList,
Packit f0b94e
    /// The offset of the flow relative to the first float.
Packit f0b94e
    offset: LogicalSize<Au>,
Packit f0b94e
    /// The writing mode of these floats.
Packit f0b94e
    pub writing_mode: WritingMode,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl fmt::Debug for Floats {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        if !self.list.is_present() {
Packit f0b94e
            write!(f, "[empty]")
Packit f0b94e
        } else {
Packit f0b94e
            write!(f, "offset={:?} floats={:?}", self.offset, self.list)
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl Floats {
Packit f0b94e
    /// Creates a new `Floats` object.
Packit f0b94e
    pub fn new(writing_mode: WritingMode) -> Floats {
Packit f0b94e
        Floats {
Packit f0b94e
            list: FloatList::new(),
Packit f0b94e
            offset: LogicalSize::zero(writing_mode),
Packit f0b94e
            writing_mode: writing_mode,
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Adjusts the recorded offset of the flow relative to the first float.
Packit f0b94e
    pub fn translate(&mut self, delta: LogicalSize<Au>) {
Packit f0b94e
        self.offset = self.offset + delta
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Returns the position of the last float in flow coordinates.
Packit f0b94e
    pub fn last_float_pos(&self) -> Option<LogicalRect<Au>> {
Packit f0b94e
        match self.list.floats.front() {
Packit f0b94e
            None => None,
Packit f0b94e
            Some(float) => Some(float.bounds.translate_by_size(self.offset)),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Returns a rectangle that encloses the region from block-start to block-start + block-size,
Packit f0b94e
    /// with inline-size small enough that it doesn't collide with any floats. max_x is the
Packit f0b94e
    /// inline-size beyond which floats have no effect. (Generally this is the containing block
Packit f0b94e
    /// inline-size.)
Packit f0b94e
    pub fn available_rect(&self, block_start: Au, block_size: Au, max_x: Au)
Packit f0b94e
                          -> Option<LogicalRect<Au>> {
Packit f0b94e
        let list = &self.list;
Packit f0b94e
        let block_start = block_start - self.offset.block;
Packit f0b94e
Packit f0b94e
        debug!("available_rect: trying to find space at {:?}", block_start);
Packit f0b94e
Packit f0b94e
        // Relevant dimensions for the inline-end-most inline-start float
Packit f0b94e
        let mut max_inline_start = Au(0) - self.offset.inline;
Packit f0b94e
        let mut l_block_start = None;
Packit f0b94e
        let mut l_block_end = None;
Packit f0b94e
        // Relevant dimensions for the inline-start-most inline-end float
Packit f0b94e
        let mut min_inline_end = max_x - self.offset.inline;
Packit f0b94e
        let mut r_block_start = None;
Packit f0b94e
        let mut r_block_end = None;
Packit f0b94e
Packit f0b94e
        // Find the float collisions for the given range in the block direction.
Packit f0b94e
        for float in list.floats.iter() {
Packit f0b94e
            debug!("available_rect: Checking for collision against float");
Packit f0b94e
            let float_pos = float.bounds.start;
Packit f0b94e
            let float_size = float.bounds.size;
Packit f0b94e
Packit f0b94e
            debug!("float_pos: {:?}, float_size: {:?}", float_pos, float_size);
Packit f0b94e
            match float.kind {
Packit f0b94e
                FloatKind::Left if float_pos.i + float_size.inline > max_inline_start &&
Packit f0b94e
                        float_pos.b + float_size.block > block_start &&
Packit f0b94e
                        float_pos.b < block_start + block_size => {
Packit f0b94e
                    max_inline_start = float_pos.i + float_size.inline;
Packit f0b94e
Packit f0b94e
                    l_block_start = Some(float_pos.b);
Packit f0b94e
                    l_block_end = Some(float_pos.b + float_size.block);
Packit f0b94e
Packit f0b94e
                    debug!("available_rect: collision with inline_start float: new \
Packit f0b94e
                            max_inline_start is {:?}",
Packit f0b94e
                           max_inline_start);
Packit f0b94e
                }
Packit f0b94e
                FloatKind::Right if float_pos.i < min_inline_end &&
Packit f0b94e
                       float_pos.b + float_size.block > block_start &&
Packit f0b94e
                       float_pos.b < block_start + block_size => {
Packit f0b94e
                    min_inline_end = float_pos.i;
Packit f0b94e
Packit f0b94e
                    r_block_start = Some(float_pos.b);
Packit f0b94e
                    r_block_end = Some(float_pos.b + float_size.block);
Packit f0b94e
                    debug!("available_rect: collision with inline_end float: new min_inline_end \
Packit f0b94e
                            is {:?}",
Packit f0b94e
                            min_inline_end);
Packit f0b94e
                }
Packit f0b94e
                FloatKind::Left | FloatKind::Right => {}
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        // Extend the vertical range of the rectangle to the closest floats.
Packit f0b94e
        // If there are floats on both sides, take the intersection of the
Packit f0b94e
        // two areas. Also make sure we never return a block-start smaller than the
Packit f0b94e
        // given upper bound.
Packit f0b94e
        let (block_start, block_end) = match (r_block_start,
Packit f0b94e
                                              r_block_end,
Packit f0b94e
                                              l_block_start,
Packit f0b94e
                                              l_block_end) {
Packit f0b94e
            (Some(r_block_start), Some(r_block_end), Some(l_block_start), Some(l_block_end)) => {
Packit f0b94e
                range_intersect(max(block_start, r_block_start),
Packit f0b94e
                                r_block_end,
Packit f0b94e
                                max(block_start, l_block_start),
Packit f0b94e
                                l_block_end)
Packit f0b94e
            }
Packit f0b94e
            (None, None, Some(l_block_start), Some(l_block_end)) => {
Packit f0b94e
                (max(block_start, l_block_start), l_block_end)
Packit f0b94e
            }
Packit f0b94e
            (Some(r_block_start), Some(r_block_end), None, None) => {
Packit f0b94e
                (max(block_start, r_block_start), r_block_end)
Packit f0b94e
            }
Packit f0b94e
            (None, None, None, None) => return None,
Packit f0b94e
            _ => panic!("Reached unreachable state when computing float area")
Packit f0b94e
        };
Packit f0b94e
Packit f0b94e
        // FIXME(eatkinson): This assertion is too strong and fails in some cases. It is OK to
Packit f0b94e
        // return negative inline-sizes since we check against that inline-end away, but we should
Packit f0b94e
        // still understand why they occur and add a stronger assertion here.
Packit f0b94e
        // assert!(max_inline-start < min_inline-end);
Packit f0b94e
Packit f0b94e
        assert!(block_start <= block_end, "Float position error");
Packit f0b94e
Packit f0b94e
        Some(LogicalRect::new(self.writing_mode,
Packit f0b94e
                              max_inline_start + self.offset.inline,
Packit f0b94e
                              block_start + self.offset.block,
Packit f0b94e
                              min_inline_end - max_inline_start,
Packit f0b94e
                              block_end - block_start))
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Adds a new float to the list.
Packit f0b94e
    pub fn add_float(&mut self, info: &PlacementInfo) {
Packit f0b94e
        let new_info = PlacementInfo {
Packit f0b94e
            size: info.size,
Packit f0b94e
            ceiling: match self.list.max_block_start {
Packit f0b94e
                None => info.ceiling,
Packit f0b94e
                Some(max_block_start) => max(info.ceiling, max_block_start + self.offset.block),
Packit f0b94e
            },
Packit f0b94e
            max_inline_size: info.max_inline_size,
Packit f0b94e
            kind: info.kind
Packit f0b94e
        };
Packit f0b94e
Packit f0b94e
        debug!("add_float: added float with info {:?}", new_info);
Packit f0b94e
Packit f0b94e
        let new_float = Float {
Packit f0b94e
            bounds: LogicalRect::from_point_size(
Packit f0b94e
                self.writing_mode,
Packit f0b94e
                self.place_between_floats(&new_info).start - self.offset,
Packit f0b94e
                info.size,
Packit f0b94e
            ),
Packit f0b94e
            kind: info.kind
Packit f0b94e
        };
Packit f0b94e
Packit f0b94e
        self.list.floats = self.list.floats.prepend_elem(new_float);
Packit f0b94e
        self.list.max_block_start = match self.list.max_block_start {
Packit f0b94e
            None => Some(new_float.bounds.start.b),
Packit f0b94e
            Some(max_block_start) => Some(max(max_block_start, new_float.bounds.start.b)),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Given the three sides of the bounding rectangle in the block-start direction, finds the
Packit f0b94e
    /// largest block-size that will result in the rectangle not colliding with any floats. Returns
Packit f0b94e
    /// `None` if that block-size is infinite.
Packit f0b94e
    fn max_block_size_for_bounds(&self, inline_start: Au, block_start: Au, inline_size: Au)
Packit f0b94e
                                 -> Option<Au> {
Packit f0b94e
        let list = &self.list;
Packit f0b94e
Packit f0b94e
        let block_start = block_start - self.offset.block;
Packit f0b94e
        let inline_start = inline_start - self.offset.inline;
Packit f0b94e
        let mut max_block_size = None;
Packit f0b94e
Packit f0b94e
        for float in list.floats.iter() {
Packit f0b94e
            if float.bounds.start.b + float.bounds.size.block > block_start &&
Packit f0b94e
                   float.bounds.start.i + float.bounds.size.inline > inline_start &&
Packit f0b94e
                   float.bounds.start.i < inline_start + inline_size {
Packit f0b94e
               let new_y = float.bounds.start.b;
Packit f0b94e
               max_block_size = Some(min(max_block_size.unwrap_or(new_y), new_y));
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        max_block_size.map(|h| h + self.offset.block)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Given placement information, finds the closest place a fragment can be positioned without
Packit f0b94e
    /// colliding with any floats.
Packit f0b94e
    pub fn place_between_floats(&self, info: &PlacementInfo) -> LogicalRect<Au> {
Packit f0b94e
        debug!("place_between_floats: Placing object with {:?}", info.size);
Packit f0b94e
Packit f0b94e
        // If no floats, use this fast path.
Packit f0b94e
        if !self.list.is_present() {
Packit f0b94e
            match info.kind {
Packit f0b94e
                FloatKind::Left => {
Packit f0b94e
                    return LogicalRect::new(
Packit f0b94e
                        self.writing_mode,
Packit f0b94e
                        Au(0),
Packit f0b94e
                        info.ceiling,
Packit f0b94e
                        info.max_inline_size,
Packit f0b94e
                        MAX_AU)
Packit f0b94e
                }
Packit f0b94e
                FloatKind::Right => {
Packit f0b94e
                    return LogicalRect::new(
Packit f0b94e
                        self.writing_mode,
Packit f0b94e
                        info.max_inline_size - info.size.inline,
Packit f0b94e
                        info.ceiling,
Packit f0b94e
                        info.max_inline_size,
Packit f0b94e
                        MAX_AU)
Packit f0b94e
                }
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        // Can't go any higher than previous floats or previous elements in the document.
Packit f0b94e
        let mut float_b = info.ceiling;
Packit f0b94e
        loop {
Packit f0b94e
            let maybe_location = self.available_rect(float_b,
Packit f0b94e
                                                     info.size.block,
Packit f0b94e
                                                     info.max_inline_size);
Packit f0b94e
            debug!("place_float: got available rect: {:?} for block-pos: {:?}",
Packit f0b94e
                   maybe_location,
Packit f0b94e
                   float_b);
Packit f0b94e
            match maybe_location {
Packit f0b94e
                // If there are no floats blocking us, return the current location
Packit f0b94e
                // TODO(eatkinson): integrate with overflow
Packit f0b94e
                None => {
Packit f0b94e
                    return match info.kind {
Packit f0b94e
                        FloatKind::Left => {
Packit f0b94e
                            LogicalRect::new(
Packit f0b94e
                                self.writing_mode,
Packit f0b94e
                                Au(0),
Packit f0b94e
                                float_b,
Packit f0b94e
                                info.max_inline_size,
Packit f0b94e
                                MAX_AU)
Packit f0b94e
                        }
Packit f0b94e
                        FloatKind::Right => {
Packit f0b94e
                            LogicalRect::new(
Packit f0b94e
                                self.writing_mode,
Packit f0b94e
                                info.max_inline_size - info.size.inline,
Packit f0b94e
                                float_b,
Packit f0b94e
                                info.max_inline_size,
Packit f0b94e
                                MAX_AU)
Packit f0b94e
                        }
Packit f0b94e
                    }
Packit f0b94e
                }
Packit f0b94e
                Some(rect) => {
Packit f0b94e
                    assert_ne!(rect.start.b + rect.size.block, float_b,
Packit f0b94e
                               "Non-terminating float placement");
Packit f0b94e
Packit f0b94e
                    // Place here if there is enough room
Packit f0b94e
                    if rect.size.inline >= info.size.inline {
Packit f0b94e
                        let block_size = self.max_block_size_for_bounds(rect.start.i,
Packit f0b94e
                                                                        rect.start.b,
Packit f0b94e
                                                                        rect.size.inline);
Packit f0b94e
                        let block_size = block_size.unwrap_or(MAX_AU);
Packit f0b94e
                        return match info.kind {
Packit f0b94e
                            FloatKind::Left => {
Packit f0b94e
                                LogicalRect::new(
Packit f0b94e
                                    self.writing_mode,
Packit f0b94e
                                    rect.start.i,
Packit f0b94e
                                    float_b,
Packit f0b94e
                                    rect.size.inline,
Packit f0b94e
                                    block_size)
Packit f0b94e
                            }
Packit f0b94e
                            FloatKind::Right => {
Packit f0b94e
                                LogicalRect::new(
Packit f0b94e
                                    self.writing_mode,
Packit f0b94e
                                    rect.start.i + rect.size.inline - info.size.inline,
Packit f0b94e
                                    float_b,
Packit f0b94e
                                    rect.size.inline,
Packit f0b94e
                                    block_size)
Packit f0b94e
                            }
Packit f0b94e
                        }
Packit f0b94e
                    }
Packit f0b94e
Packit f0b94e
                    // Try to place at the next-lowest location.
Packit f0b94e
                    // Need to be careful of fencepost errors.
Packit f0b94e
                    float_b = rect.start.b + rect.size.block;
Packit f0b94e
                }
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    pub fn clearance(&self, clear: ClearType) -> Au {
Packit f0b94e
        let list = &self.list;
Packit f0b94e
        let mut clearance = Au(0);
Packit f0b94e
        for float in list.floats.iter() {
Packit f0b94e
            match (clear, float.kind) {
Packit f0b94e
                (ClearType::Left, FloatKind::Left) |
Packit f0b94e
                (ClearType::Right, FloatKind::Right) |
Packit f0b94e
                (ClearType::Both, _) => {
Packit f0b94e
                    let b = self.offset.block + float.bounds.start.b + float.bounds.size.block;
Packit f0b94e
                    clearance = max(clearance, b);
Packit f0b94e
                }
Packit f0b94e
                _ => {}
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
        clearance
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    pub fn is_present(&self) -> bool {
Packit f0b94e
        self.list.is_present()
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// The speculated inline sizes of floats flowing through or around a flow (depending on whether
Packit f0b94e
/// the flow is a block formatting context). These speculations are always *upper bounds*; the
Packit f0b94e
/// actual inline sizes might be less. Note that this implies that a speculated value of zero is a
Packit f0b94e
/// guarantee that there will be no floats on that side.
Packit f0b94e
///
Packit f0b94e
/// This is used for two purposes: (a) determining whether we can lay out blocks in parallel; (b)
Packit f0b94e
/// guessing the inline-sizes of block formatting contexts in an effort to lay them out in
Packit f0b94e
/// parallel.
Packit f0b94e
#[derive(Clone, Copy)]
Packit f0b94e
pub struct SpeculatedFloatPlacement {
Packit f0b94e
    /// The estimated inline size (an upper bound) of the left floats flowing through this flow.
Packit f0b94e
    pub left: Au,
Packit f0b94e
    /// The estimated inline size (an upper bound) of the right floats flowing through this flow.
Packit f0b94e
    pub right: Au,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl fmt::Debug for SpeculatedFloatPlacement {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        write!(f, "L {:?} R {:?}", self.left, self.right)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl SpeculatedFloatPlacement {
Packit f0b94e
    /// Returns a `SpeculatedFloatPlacement` objects with both left and right speculated inline
Packit f0b94e
    /// sizes initialized to zero.
Packit f0b94e
    pub fn zero() -> SpeculatedFloatPlacement {
Packit f0b94e
        SpeculatedFloatPlacement {
Packit f0b94e
            left: Au(0),
Packit f0b94e
            right: Au(0),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Given the speculated inline size of the floats out for the inorder predecessor of this
Packit f0b94e
    /// flow, computes the speculated inline size of the floats flowing in.
Packit f0b94e
    pub fn compute_floats_in(&mut self, flow: &mut Flow) {
Packit f0b94e
        let base_flow = flow.base();
Packit f0b94e
        if base_flow.flags.contains(FlowFlags::CLEARS_LEFT) {
Packit f0b94e
            self.left = Au(0)
Packit f0b94e
        }
Packit f0b94e
        if base_flow.flags.contains(FlowFlags::CLEARS_RIGHT) {
Packit f0b94e
            self.right = Au(0)
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Given the speculated inline size of the floats out for this flow's last child, computes the
Packit f0b94e
    /// speculated inline size of the floats out for this flow.
Packit f0b94e
    pub fn compute_floats_out(&mut self, flow: &mut Flow) {
Packit f0b94e
        if flow.is_block_like() {
Packit f0b94e
            let block_flow = flow.as_block();
Packit f0b94e
            if block_flow.formatting_context_type() != FormattingContextType::None {
Packit f0b94e
                *self = block_flow.base.speculated_float_placement_in;
Packit f0b94e
            } else {
Packit f0b94e
                if self.left > Au(0) || self.right > Au(0) {
Packit f0b94e
                    let speculated_inline_content_edge_offsets =
Packit f0b94e
                        block_flow.fragment.guess_inline_content_edge_offsets();
Packit f0b94e
                    if self.left > Au(0) && speculated_inline_content_edge_offsets.start > Au(0) {
Packit f0b94e
                        self.left = self.left + speculated_inline_content_edge_offsets.start
Packit f0b94e
                    }
Packit f0b94e
                    if self.right > Au(0) && speculated_inline_content_edge_offsets.end > Au(0) {
Packit f0b94e
                        self.right = self.right + speculated_inline_content_edge_offsets.end
Packit f0b94e
                    }
Packit f0b94e
                }
Packit f0b94e
Packit f0b94e
                self.left = max(self.left, block_flow.base.speculated_float_placement_in.left);
Packit f0b94e
                self.right = max(self.right, block_flow.base.speculated_float_placement_in.right);
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        let base_flow = flow.base();
Packit f0b94e
        if !base_flow.flags.is_float() {
Packit f0b94e
            return
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        let mut float_inline_size = base_flow.intrinsic_inline_sizes.preferred_inline_size;
Packit f0b94e
        if float_inline_size == Au(0) {
Packit f0b94e
            if flow.is_block_like() {
Packit f0b94e
                // Hack: If the size of the float is a percentage, then there's no way we can guess
Packit f0b94e
                // at its size now. So just pick an arbitrary nonzero value (in this case, 1px) so
Packit f0b94e
                // that the layout traversal logic will know that objects later in the document
Packit f0b94e
                // might flow around this float.
Packit f0b94e
                if let LengthOrPercentageOrAuto::Percentage(percentage) =
Packit f0b94e
                        flow.as_block().fragment.style.content_inline_size() {
Packit f0b94e
                    if percentage.0 > 0.0 {
Packit f0b94e
                        float_inline_size = Au::from_px(1)
Packit f0b94e
                    }
Packit f0b94e
                }
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        match base_flow.flags.float_kind() {
Packit f0b94e
            StyleFloat::None => {}
Packit f0b94e
            StyleFloat::Left => self.left = self.left + float_inline_size,
Packit f0b94e
            StyleFloat::Right => self.right = self.right + float_inline_size,
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Given a flow, computes the speculated inline size of the floats in of its first child.
Packit f0b94e
    pub fn compute_floats_in_for_first_child(parent_flow: &mut Flow) -> SpeculatedFloatPlacement {
Packit f0b94e
        if !parent_flow.is_block_like() {
Packit f0b94e
            return parent_flow.base().speculated_float_placement_in
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        let parent_block_flow = parent_flow.as_block();
Packit f0b94e
        if parent_block_flow.formatting_context_type() != FormattingContextType::None {
Packit f0b94e
            return SpeculatedFloatPlacement::zero()
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        let mut placement = parent_block_flow.base.speculated_float_placement_in;
Packit f0b94e
        let speculated_inline_content_edge_offsets =
Packit f0b94e
            parent_block_flow.fragment.guess_inline_content_edge_offsets();
Packit f0b94e
Packit f0b94e
        if speculated_inline_content_edge_offsets.start > Au(0) {
Packit f0b94e
            placement.left = if placement.left > speculated_inline_content_edge_offsets.start {
Packit f0b94e
                placement.left - speculated_inline_content_edge_offsets.start
Packit f0b94e
            } else {
Packit f0b94e
                Au(0)
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
        if speculated_inline_content_edge_offsets.end > Au(0) {
Packit f0b94e
            placement.right = if placement.right > speculated_inline_content_edge_offsets.end {
Packit f0b94e
                placement.right - speculated_inline_content_edge_offsets.end
Packit f0b94e
            } else {
Packit f0b94e
                Au(0)
Packit f0b94e
            }
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        placement
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e