use super::*;
use crate::layout::block::float::FLOAT_EPSILON;
pub(in crate::layout) type DefinitePhysicalContentHeight = Definite<PhysicalContentHeight>;
pub(in crate::layout) fn writing_modes_are_orthogonal(a: WritingMode, b: WritingMode) -> bool {
WritingModeAxes::new(a, Direction::Ltr).swaps_physical_axes()
!= WritingModeAxes::new(b, Direction::Ltr).swaps_physical_axes()
}
pub(in crate::layout) fn child_available_space_for_formatting_context(
style: &ComputedStyle,
content_width: PhysicalContentWidth,
definite_content_height: Option<DefinitePhysicalContentHeight>,
inherited_orthogonal_available_height: OrthogonalAvailableHeight,
initial_fallback_height: PhysicalContentHeight,
) -> ChildAvailableSpace {
let local_orthogonal_constraint = orthogonal_fallback_physical_content_height(
style,
PercentageBasis::definite(content_width.content_box_length()),
);
let direct_orthogonal_constraint = direct_orthogonal_available_height(
style,
PercentageBasis::definite(content_width.content_box_length()),
);
let is_scroll_container = style_clips_overflow(style);
let mut space = ChildAvailableSpace::new(
style.writing_mode,
content_width,
!style.writing_mode.has_vertical_lines() || !style.box_values.width.is_auto(),
definite_content_height.map(DefinitePhysicalContentHeight::value),
inherited_orthogonal_available_height.value(),
)
.with_orthogonal_available_height(inherited_orthogonal_available_height)
.with_direct_orthogonal_available_height(
(!is_scroll_container)
.then_some(direct_orthogonal_constraint)
.flatten()
.and_then(|height| height.capped_by_initial_containing_block(initial_fallback_height)),
);
if is_scroll_container {
let initial = initial_fallback_height.points().max(0.0);
let has_max_height = used_max_height(
style,
PercentageBasis::definite(content_width.content_box_length()),
)
.is_some();
let constrained_height = definite_content_height
.map(DefinitePhysicalContentHeight::value)
.or(has_max_height
.then_some(local_orthogonal_constraint)
.flatten())
.map(PhysicalContentHeight::points);
let fallback = constrained_height.map_or_else(
|| OrthogonalAvailableHeight::initial_containing_block(initial_fallback_height),
|height| {
OrthogonalAvailableHeight::nearest_scroll_container(PhysicalContentHeight::new(
content_box_pt(height.min(initial).max(0.0)),
))
},
);
space = space.with_orthogonal_available_height(fallback);
}
space
}
pub(in crate::layout) fn child_available_space_for_block(
style: &ComputedStyle,
content_width: PhysicalContentWidth,
definite_content_height: Option<DefinitePhysicalContentHeight>,
inherited_orthogonal_available_height: OrthogonalAvailableHeight,
initial_fallback_height: PhysicalContentHeight,
) -> ChildAvailableSpace {
child_available_space_for_formatting_context(
style,
content_width,
definite_content_height,
inherited_orthogonal_available_height,
initial_fallback_height,
)
}
pub(in crate::layout) fn orthogonal_fallback_physical_content_height(
style: &ComputedStyle,
percentage_basis: PercentageBasis<ContentBoxLength>,
) -> Option<PhysicalContentHeight> {
let min_height = used_min_height(style, percentage_basis).map(SemanticLengthExt::points);
let max_height = used_max_height(style, percentage_basis).map(SemanticLengthExt::points);
let height = match (min_height, max_height) {
(Some(min_height), Some(max_height)) => max_height.max(min_height),
(Some(min_height), None) => min_height,
(None, Some(max_height)) => max_height,
(None, None) => return None,
};
Some(PhysicalContentHeight::new(content_box_pt(height)))
}
fn direct_orthogonal_available_height(
style: &ComputedStyle,
percentage_basis: PercentageBasis<ContentBoxLength>,
) -> Option<DirectOrthogonalAvailableHeight> {
if let Some(height) = style.box_values.height.length_if_no_percent() {
return Some(DirectOrthogonalAvailableHeight::Definite(
PhysicalContentHeight::new(content_box_pt(height.max(0.0))),
));
}
let min_height = used_min_height(style, percentage_basis).map(SemanticLengthExt::points);
let max_height = used_max_height(style, percentage_basis).map(SemanticLengthExt::points);
let height = orthogonal_fallback_physical_content_height(style, percentage_basis)?;
if min_height.is_some_and(|minimum| max_height.is_none_or(|maximum| minimum > maximum)) {
Some(DirectOrthogonalAvailableHeight::MinimumFloor(height))
} else {
Some(DirectOrthogonalAvailableHeight::Maximum(height))
}
}
pub(in crate::layout) fn vertical_block_align_content_needs_fragment_bounds(
style: &ComputedStyle,
) -> bool {
WritingModeAxes::new(style.writing_mode, style.direction).swaps_physical_axes()
&& style.align_content.keyword != ContentAlignmentKeyword::Normal
}
pub(in crate::layout) fn vertical_block_align_content_x_offset(
style: &ComputedStyle,
content_inline_span: PageInlineSpan,
subject_bounds: Option<PaintClip>,
) -> f32 {
if !vertical_block_align_content_needs_fragment_bounds(style) {
return 0.0;
}
let Some(subject_bounds) = subject_bounds else {
return 0.0;
};
let subject_width = subject_bounds.width().max(0.0);
let content_width = content_inline_span.width().max(0.0);
let free_space = content_width - subject_width;
let toward_block_end = content_alignment_offset_toward_end(
style.align_content,
free_space,
block_align_content_defaults_to_safe_overflow(style),
);
match block_start_side(style.writing_mode) {
PhysicalSide::Left => content_inline_span.left_x() + toward_block_end - subject_bounds.x(),
PhysicalSide::Right => {
content_inline_span.right_x() - toward_block_end - (subject_bounds.x() + subject_width)
}
PhysicalSide::Top | PhysicalSide::Bottom => 0.0,
}
}
pub(in crate::layout) fn plain_inline_content_needs_inline_items(
text: &str,
style: &ComputedStyle,
) -> bool {
style.writing_mode != WritingMode::HorizontalTb
|| contains_bidi_text(text)
|| style.used_word_spacing() != layout_pt(0.0)
|| style.used_letter_spacing() != layout_pt(0.0)
|| style.text_transform != css::TextTransform::NONE
|| style.text_decoration.clone().has_visible_line()
|| style.text_decoration_origins.has_effective_layers()
|| !matches!(style.text_emphasis_style, css::TextEmphasisStyle::None)
}
pub(in crate::layout) fn block_end_margin_collapse_survives_height_constraints(
style: &ComputedStyle,
content_width: PhysicalContentWidth,
vertical_non_content: NonContentLength,
content_height_without_child_margin: PhysicalContentHeight,
) -> bool {
let requested_content_height = used_content_box_height_or_auto(
style,
layout_pt(content_height_without_child_margin.points()),
vertical_non_content,
)
.map(SemanticLengthExt::points)
.unwrap_or_else(|| content_height_without_child_margin.points());
let height_depends_on_intrinsic_content =
needs_intrinsic_height_contribution(style.box_values.height.value().clone())
|| needs_intrinsic_height_contribution(style.box_values.min_height.clone())
|| needs_intrinsic_height_contribution(style.box_values.max_height.clone());
let constrained_height = if height_depends_on_intrinsic_content {
constrain_height_with_intrinsic(
style,
content_box_pt(requested_content_height),
content_height_without_child_margin.content_box_length(),
content_height_without_child_margin.content_box_length(),
PercentageBasis::definite(content_width.content_box_length()),
vertical_non_content,
)
.points()
} else {
constrain_content_height(
style,
content_box_pt(requested_content_height),
PercentageBasis::definite(content_width.content_box_length()),
)
.points()
};
(constrained_height - content_height_without_child_margin.points()).abs() <= 0.01
}
pub(in crate::layout) struct BlockLayoutGeometry {
pub(in crate::layout) style: css::ZoomedLayoutStyle,
pub(in crate::layout) relative_offset: RelativeOffset,
pub(in crate::layout) border_edges: UsedEdges,
pub(in crate::layout) vertical_non_content: NonContentLength,
pub(in crate::layout) containing_block_content_height: BlockSizePercentageBasis,
pub(in crate::layout) containing_block_percentage_context: DescendantBlockPercentageContext,
pub(in crate::layout) definite_content_height: Option<DefinitePhysicalContentHeight>,
pub(in crate::layout) content_logical_inline_size: LogicalInlineContentSize,
pub(in crate::layout) selected_orthogonal_inline_layout: Option<SelectedOrthogonalInlineLayout>,
pub(in crate::layout) outer_inline: BlockBorderBoxInlineBounds,
pub(in crate::layout) content_inline: BlockContentBoxInlineBounds,
}
#[derive(Clone)]
pub(in crate::layout) struct SelectedOrthogonalInlineLayout {
pub(in crate::layout) logical_inline_measure: LogicalInlineContentSize,
pub(in crate::layout) line_sequence: inline_layout::InlineLineSequence,
pub(in crate::layout) logical_block_contribution: LogicalBlockContentSize,
pub(in crate::layout) frozen_replay_input: inline_collect::FrozenInlineReplayInput,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct BlockLayoutInlineConstraint {
pub(in crate::layout) containing_inline_span: PageInlineSpan,
pub(in crate::layout) percentage_basis: LogicalInlinePercentageBasis,
pub(in crate::layout) physical_width_percentage_basis: PhysicalContentWidth,
pub(in crate::layout) auto_border_box_width: Option<BorderBoxLength>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct DirectBlockLayoutConstraint {
element: ElementId,
inline: BlockLayoutInlineConstraint,
}
impl DirectBlockLayoutConstraint {
pub(in crate::layout) fn new(element: ElementId, inline: BlockLayoutInlineConstraint) -> Self {
Self { element, inline }
}
pub(in crate::layout) fn for_element(
self,
element: &Element,
) -> Option<BlockLayoutInlineConstraint> {
(self.element == element.id).then_some(self.inline)
}
}
impl BlockLayoutGeometry {
pub(in crate::layout) fn outer_inline(&self) -> BlockBorderBoxInlineBounds {
self.outer_inline
}
pub(in crate::layout) fn content_inline(&self) -> BlockContentBoxInlineBounds {
self.content_inline
}
pub(in crate::layout) fn content_logical_inline_size(&self) -> LogicalInlineContentSize {
self.content_logical_inline_size
}
pub(in crate::layout) fn reanchor_float_avoiding_border_box(
&mut self,
normal_flow_border_box_span: PageInlineSpan,
) {
let outer_span = PageInlineSpan::new(
normal_flow_border_box_span.left_x() + self.relative_offset.x(),
normal_flow_border_box_span.width(),
);
self.outer_inline = BlockBorderBoxInlineBounds::new(outer_span);
self.content_inline = BlockContentBoxInlineBounds::new(PageInlineSpan::new(
outer_span.left_x() + self.border_edges.left.points() + self.style.padding.left,
self.content_inline.width().points(),
));
}
pub(in crate::layout) fn float_avoidance_candidate(
&self,
border_box_block_size: BorderBoxLength,
) -> FloatAvoidanceCandidate {
let (inline_start_containment, inline_end_containment) = match self.style.direction {
Direction::Ltr => (
(self.style.margin.left < -FLOAT_EPSILON)
.then_some(FloatAvoidanceInlineContainment::PermittedNegativeMarginOverflow),
(self.style.margin.right < -FLOAT_EPSILON)
.then_some(FloatAvoidanceInlineContainment::PermittedNegativeMarginOverflow),
),
Direction::Rtl => (
(self.style.margin.right < -FLOAT_EPSILON)
.then_some(FloatAvoidanceInlineContainment::PermittedNegativeMarginOverflow),
(self.style.margin.left < -FLOAT_EPSILON)
.then_some(FloatAvoidanceInlineContainment::PermittedNegativeMarginOverflow),
),
};
FloatAvoidanceCandidate {
normal_flow_border_box_inline_span: PageInlineSpan::new(
self.outer_inline.span().left_x() - self.relative_offset.x(),
self.outer_inline.span().width(),
),
normal_flow_border_box_block_size: border_box_block_size,
inline_start_containment: inline_start_containment
.unwrap_or(FloatAvoidanceInlineContainment::Required),
inline_end_containment: inline_end_containment
.unwrap_or(FloatAvoidanceInlineContainment::Required),
}
}
pub(in crate::layout) fn border_box_top_rect(
&self,
block_top: f32,
block_height: f32,
) -> BlockBorderBox {
BlockBorderBox::from_rect(BlockRect::new(
BlockPoint::new(self.outer_inline.span().left_x(), block_top),
BlockSize::new(self.outer_inline.width().points(), block_height.max(0.0)),
))
}
pub(in crate::layout) fn padding_box_top_rect(
&self,
block_top: f32,
content_height: f32,
) -> PageTopRect {
PageTopRect::new(
self.outer_inline.span().left_x() + self.border_edges.left.points(),
block_top - self.border_edges.top.points(),
self.content_inline.width().points()
+ self.style.padding.left
+ self.style.padding.right,
content_height + self.style.padding.top + self.style.padding.bottom,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct BlockBorderBoxInlineBounds {
span: PageInlineSpan,
}
impl BlockBorderBoxInlineBounds {
pub(in crate::layout) fn new(span: PageInlineSpan) -> Self {
Self { span }
}
pub(in crate::layout) fn span(self) -> PageInlineSpan {
self.span
}
pub(in crate::layout) fn width(self) -> BorderBoxLength {
border_box_pt(self.span.width())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct BlockContentBoxInlineBounds {
span: PageInlineSpan,
}
impl BlockContentBoxInlineBounds {
pub(in crate::layout) fn new(span: PageInlineSpan) -> Self {
Self { span }
}
pub(in crate::layout) fn span(self) -> PageInlineSpan {
self.span
}
pub(in crate::layout) fn width(self) -> ContentBoxLength {
content_box_pt(self.span.width())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct BlockBorderBox {
pub(in crate::layout) rect: BlockRect,
}
impl BlockBorderBox {
pub(in crate::layout) fn from_rect(rect: BlockRect) -> Self {
Self { rect }
}
pub(in crate::layout) fn x(self) -> f32 {
self.rect.origin.x
}
pub(in crate::layout) fn top_y(self) -> f32 {
self.rect.origin.y
}
pub(in crate::layout) fn width(self) -> f32 {
self.rect.size.width
}
pub(in crate::layout) fn height(self) -> f32 {
self.rect.size.height
}
pub(in crate::layout) fn page_top_rect(self) -> PageTopRect {
PageTopRect::new(self.x(), self.top_y(), self.width(), self.height())
}
}