use super::gap_decorations::{
GridGapFragmentProjection, grid_fragment_source_range_from_bounds,
grid_gap_decoration_primitives_for_page, grid_used_track_extent,
};
use super::lanes::grid_lanes_stacking_axis_is_block;
use super::model::{GridItemLayout, GridLayout};
use super::*;
use crate::layout::assets::FragmentainerOrdinal;
use crate::layout::baseline::PhysicalTopBaselineOffset;
use crate::layout::block::{DefiniteBlockBreakContext, should_prebreak_definite_block};
use crate::units::content_box_to_margin_box_length;
fn grid_scrollbar_reservation(_: &ComputedStyle) -> ScrollbarGutterReservation {
ScrollbarGutterReservation::static_pdf_overlay()
}
fn grid_scrollport_content_width(
content_width: PhysicalContentWidth,
reservation: ScrollbarGutterReservation,
) -> PhysicalContentWidth {
PhysicalContentWidth::new(content_box_pt(
(content_width.points() - reservation.horizontal_extent().points()).max(0.0),
))
}
fn grid_scrollport_content_height_basis(
content_height: Option<PhysicalContentHeight>,
reservation: ScrollbarGutterReservation,
) -> Option<PhysicalContentHeight> {
content_height.map(|height| {
PhysicalContentHeight::new(content_box_pt(
(height.points() - reservation.vertical_extent().points()).max(0.0),
))
})
}
fn grid_total_content_height(
track_height: PhysicalContentHeight,
definite_content_height: Option<PhysicalContentHeight>,
reservation: ScrollbarGutterReservation,
) -> PhysicalContentHeight {
if let Some(height) = definite_content_height {
return height;
}
PhysicalContentHeight::new(content_box_pt(
track_height.points() + reservation.vertical_extent().points(),
))
}
#[allow(clippy::too_many_arguments)]
fn grid_size_contained_content_height(
layout: &mut LayoutBuilder<'_>,
style: &ComputedStyle,
contains_size: bool,
stylesheets: &Stylesheets<'_>,
grid_content_width: PhysicalContentWidth,
definite_content_height: Option<PhysicalContentHeight>,
scrollbar_reservation: ScrollbarGutterReservation,
available_block_size: f32,
) -> Option<PhysicalContentHeight> {
if !contains_size {
return None;
}
let empty_grid_height = layout
.compute_grid_layout(
style,
&[],
stylesheets,
grid_content_width,
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation),
GridLayoutPurpose::IntrinsicProbe,
)
.map(|layout| layout.height)
.unwrap_or_else(|| PhysicalContentHeight::new(content_box_pt(0.0)));
Some(
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation)
.unwrap_or_else(|| {
PhysicalContentHeight::new(constrain_content_height(
style,
empty_grid_height.content_box_length(),
PercentageBasis::definite(layout_pt(available_block_size)),
))
}),
)
}
impl<'a> LayoutBuilder<'a> {
pub(in crate::layout) fn measure_floated_grid_margin_box_height(
&mut self,
element: &Element,
placed_style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
content_width: PhysicalContentWidth,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
) -> Option<MarginBoxLength> {
#[cfg(feature = "layout-profile")]
let _profile = crate::layout::layout_profile::grid_float_block_size_measurement_scope();
let child_boxes = child_boxes?;
let mut replay_style = placed_style.clone();
suppress_replayed_item_margins(&mut replay_style);
let mut used_style =
GridUsedStyle::from_normalized(self.style_with_current_viewport_lengths(&replay_style));
if layout_containment_applies_to_element(element, &used_style)
|| paint_containment_applies_to_element(element, &used_style)
{
used_style.grid_template_rows.resolve_contained_subgrid();
used_style.grid_template_columns.resolve_contained_subgrid();
}
let authored_borders = used_border_widths(&used_style);
let containing_inline_size = content_box_to_border_box_length(
content_width.content_box_length(),
non_content_pt(
used_style.padding.left
+ used_style.padding.right
+ authored_borders.left
+ authored_borders.right,
),
)
.points();
let box_metrics = apply_used_box_metrics(
&mut used_style,
PercentageBasis::definite(layout_pt(containing_inline_size)),
);
let style = &used_style;
let scrollbar_reservation = grid_scrollbar_reservation(style);
let containment = used_property_containment(element, style);
let horizontal_extras = box_metrics.horizontal_non_content_length().points();
let vertical_extras = box_metrics.vertical_non_content_length().points();
let grid_content_width =
grid_scrollport_content_width(content_width, scrollbar_reservation);
let definite_content_height = non_replaced_aspect_ratio_content_height(
style,
content_width.points(),
horizontal_extras,
vertical_extras,
)
.map(|height| {
PhysicalContentHeight::new(constrain_content_height(
style,
content_box_pt(height),
PercentageBasis::definite(layout_pt(containing_inline_size)),
))
});
let (children, _) = grid_child_lists_from_boxes(child_boxes);
let children = self.prepare_grid_children(children);
let size_contained_content_height = grid_size_contained_content_height(
self,
style,
containment.size,
stylesheets,
grid_content_width,
definite_content_height,
scrollbar_reservation,
containing_inline_size,
);
let grid_layout = self.compute_grid_layout(
style,
&children,
stylesheets,
grid_content_width,
size_contained_content_height.or_else(|| {
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation)
}),
GridLayoutPurpose::FloatBlockSizeMeasurement,
)?;
let content_height = size_contained_content_height.unwrap_or_else(|| {
PhysicalContentHeight::new(constrain_content_height(
style,
grid_layout.height.content_box_length(),
PercentageBasis::definite(layout_pt(containing_inline_size)),
))
});
let content_height = grid_total_content_height(
content_height,
definite_content_height,
scrollbar_reservation,
);
Some(content_box_to_margin_box_length(
content_height.content_box_length(),
box_metrics.vertical_non_content_length(),
layout_pt(placed_style.margin.top + placed_style.margin.bottom),
))
}
pub(in crate::layout) fn layout_grid_with_descendant_percentage_height_basis(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
descendant_percentage_height_basis: Option<BlockSizePercentageBasis>,
principal_box_paint_mode: PrincipalBoxPaintMode,
) {
let source_style = style;
if style.position.is_out_of_flow_positioned() {
self.layout_positioned_block_with_static_source(
element,
style,
stylesheets,
child_boxes,
None,
);
return;
}
let defer_own_decoration_promotion = self.defer_next_block_decoration_promotion;
self.defer_next_block_decoration_promotion = false;
let fragmentainer_kind = self.active_fragmentainer_kind();
self.apply_forced_break_before_box_in(fragmentainer_kind, style);
let containing_inline_size = (self.content_right - self.content_left).max(0.0);
let mut used_style =
GridUsedStyle::from_normalized(self.style_with_current_viewport_lengths(style));
if layout_containment_applies_to_element(element, &used_style)
|| paint_containment_applies_to_element(element, &used_style)
{
used_style.grid_template_rows.resolve_contained_subgrid();
used_style.grid_template_columns.resolve_contained_subgrid();
}
let box_metrics = apply_used_box_metrics(
&mut used_style,
PercentageBasis::definite(layout_pt(containing_inline_size)),
);
let relative_offset = self.normal_flow_relative_position_offset(&used_style);
if matches!(used_style.position, Position::Relative | Position::Sticky) {
self.cursor_y += relative_offset.y();
}
let available_outer_width =
normal_flow_block_available_outer_width(&used_style, layout_pt(containing_inline_size));
let border_widths = box_metrics.border.to_css_edges();
let horizontal_extras = box_metrics.horizontal_non_content_length().points();
let vertical_extras = box_metrics.vertical_non_content_length().points();
let current_fragmentainer =
self.fragmentainer_from_page_cursor(PageTopBlockPosition::new(self.cursor_y));
let available_outer_height = current_fragmentainer
.available_block_size_after_reservation(layout_pt(
used_style.margin.top + used_style.margin.bottom,
))
.points();
let explicit_content_height = descendant_percentage_height_basis
.map(PercentageBasis::points)
.unwrap_or_else(|| {
used_content_box_height_or_auto(
&used_style,
layout_pt(available_outer_height),
non_content_pt(vertical_extras),
)
.map(SemanticLengthExt::points)
})
.map(|height| {
PhysicalContentHeight::new(constrain_content_height(
&used_style,
content_box_pt(height),
PercentageBasis::definite(layout_pt(available_outer_height)),
))
});
let requested_content_width = self.used_block_physical_content_width(
element,
&used_style,
stylesheets,
child_boxes,
BlockContentWidthInputs {
available_outer_width,
percentage_basis: PercentageBasis::definite(layout_pt(containing_inline_size)),
horizontal_non_content: non_content_pt(horizontal_extras),
definite_content_height: explicit_content_height,
auto_width_role: BlockAutoWidthRole::NormalFlow,
},
);
let requested_content_width = explicit_content_height
.and_then(|height| {
non_replaced_aspect_ratio_content_width(
&used_style,
height.points(),
horizontal_extras,
vertical_extras,
)
})
.map(|width| PhysicalContentWidth::new(content_box_pt(width)))
.unwrap_or(requested_content_width);
let resolve_auto_margins = used_style.float == Float::None;
let width = resolve_normal_flow_block_inline_geometry(
&mut used_style,
PageInlineSpan::from_edges(self.content_left, self.content_right),
requested_content_width,
non_content_pt(horizontal_extras),
self.containing_block_direction,
resolve_auto_margins,
);
let content_width = width.content_width.points();
let outer_width = width.border_box_width().points();
let style = &used_style;
let scrollbar_reservation = grid_scrollbar_reservation(style);
let containment = used_property_containment(element, style);
let mut outer_x = width.border_box_inline_span.left_x() + relative_offset.x();
let mut inner_x = outer_x + border_widths.left + style.padding.left;
let inner_width = grid_scrollport_content_width(
PhysicalContentWidth::new(content_box_pt(content_width)),
scrollbar_reservation,
)
.points();
let definite_content_height = explicit_content_height
.or_else(|| {
non_replaced_aspect_ratio_content_height(
style,
content_width,
horizontal_extras,
vertical_extras,
)
.map(|height| PhysicalContentHeight::new(content_box_pt(height)))
})
.map(|height| {
PhysicalContentHeight::new(constrain_content_height(
style,
height.content_box_length(),
PercentageBasis::definite(layout_pt(available_outer_height)),
))
});
let empty_destination_fragmentainer = match fragmentainer_kind {
FragmentainerKind::Page => {
let next_context = self.resolved_page_context(
self.destination_document_page_number(self.pages.len() + 2),
false,
);
Fragmentainer::new(
layout_pt(next_context.area_height()),
layout_pt(next_context.area_height()),
)
}
FragmentainerKind::Column => {
let next_capacity = self
.fragmentainer_override
.map(|override_| {
override_
.context_for_fragmentainer(self.pages.len() + 1)
.area_height()
})
.unwrap_or_else(|| self.page_area_height());
Fragmentainer::new(layout_pt(next_capacity), layout_pt(next_capacity))
}
};
if should_prebreak_definite_block(DefiniteBlockBreakContext {
definite_content_height: definite_content_height.map(PhysicalContentHeight::points),
vertical_non_content: box_metrics.vertical_non_content_length(),
style,
current_fragmentainer,
empty_destination_fragmentainer,
fragmentainer_has_occupied_flow: self.current_page_has_content()
|| self.cursor_y < self.page_top() - 0.01,
at_page_top: self.cursor_is_at_page_top(),
suppress_for_avoid_retry: self.avoid_inside_retry_depth > 0,
}) {
self.push_page();
self.layout_grid_with_descendant_percentage_height_basis(
element,
style,
stylesheets,
child_boxes,
descendant_percentage_height_basis,
principal_box_paint_mode,
);
return;
}
let built_child_boxes;
let child_boxes = if let Some(child_boxes) = child_boxes {
child_boxes
} else {
built_child_boxes = self.build_frozen_child_boxes_with_current_ancestors(
element,
stylesheets,
source_style,
);
&built_child_boxes
};
let (children, positioned_children) = grid_child_lists_from_boxes(child_boxes);
let children = self.prepare_grid_children(children);
let positioned_children = self.prepare_grid_children(positioned_children);
let size_contained_content_height = grid_size_contained_content_height(
self,
style,
containment.size,
stylesheets,
PhysicalContentWidth::new(content_box_pt(inner_width)),
definite_content_height,
scrollbar_reservation,
available_outer_height,
);
let grid_content_height_basis = size_contained_content_height.or(definite_content_height);
let grid_item_layout_height_basis =
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation);
self.cursor_y -= style.margin.top;
if style.float == Float::None {
let margin_box_width = style.margin.left + outer_width + style.margin.right;
let collision_height = grid_content_height_basis
.map(PhysicalContentHeight::points)
.unwrap_or(style.line_height)
+ vertical_extras
+ style.margin.top
+ style.margin.bottom;
let placement = self.place_float_avoiding_margin_box(
PageTopBlockPosition::new(self.cursor_y),
margin_box_size_pt(margin_box_width, collision_height),
style.clear,
self.containing_block_direction,
);
self.cursor_y = placement.origin.top_y();
outer_x = placement.origin.x() + style.margin.left + relative_offset.x();
inner_x = outer_x + border_widths.left + style.padding.left;
} else {
self.cursor_y = self
.resolve_block_clearance(BlockClearanceRequest::coincident_edges(
style.clear,
FloatPlacementAxes::new(
self.containing_block_writing_mode,
self.containing_block_direction,
),
PageTopBlockPosition::new(self.cursor_y),
))
.used_border_edge
.points();
}
let border_box_inline_span = PageInlineSpan::new(outer_x, outer_width);
let block_top = self.cursor_y;
let paint_page_index = self.pages.len();
let paint_checkpoint = self.current_page.paint_checkpoint();
self.cursor_y -= border_widths.top + style.padding.top;
let content_top = self.cursor_y;
let fragment_decoration_reservation = FragmentDecorationReservation::new(
FragmentDecoration::for_box_decoration_break(style.box_decoration_break, false, false),
non_content_pt(border_widths.top + style.padding.top),
non_content_pt(style.padding.bottom + border_widths.bottom),
);
let Some(mut grid_layout) = self.compute_grid_layout(
style,
&children,
stylesheets,
PhysicalContentWidth::new(content_box_pt(inner_width)),
grid_item_layout_height_basis,
GridLayoutPurpose::FinalLayout,
) else {
let mut flow_style = style.clone();
flow_style.display = Display::BLOCK;
suppress_replayed_item_margins(&mut flow_style);
self.layout_block(element, &flow_style, stylesheets, &[], Some(child_boxes));
return;
};
let content_fragmentainer =
self.fragmentainer_from_page_cursor(PageTopBlockPosition::new(content_top));
let initial_raw_fragmentainer_extent = content_fragmentainer.available_block_size();
let continuation_raw_fragmentainer_extent =
content_fragmentainer.fragmentainer_block_size();
let initial_grid_content_capacity = fragment_decoration_reservation
.remaining_content_extent(initial_raw_fragmentainer_extent);
let continuation_grid_content_capacity = fragment_decoration_reservation
.fresh_content_extent(continuation_raw_fragmentainer_extent);
for (item, child) in grid_layout.items.iter_mut().zip(&children) {
let decoration = FragmentDecoration::for_box_decoration_break(
child.style.box_decoration_break,
false,
false,
);
if !decoration.is_clone() {
continue;
}
let borders = used_border_widths(&child.style);
let reservation = FragmentDecorationReservation::new(
decoration,
non_content_pt(borders.top + child.style.padding.top),
non_content_pt(child.style.padding.bottom + borders.bottom),
);
let source_height = (item.height()
- reservation.block_start().points()
- reservation.block_end().points())
.max(0.0);
item.configure_cloned_fragment_source(source_height, reservation);
item.project_cloned_fragment_destinations(
initial_raw_fragmentainer_extent,
continuation_raw_fragmentainer_extent,
);
}
let total_content_height = size_contained_content_height
.or_else(|| {
Some(PhysicalContentHeight::new(constrain_content_height(
style,
grid_layout.height.content_box_length(),
PercentageBasis::definite(layout_pt(available_outer_height)),
)))
})
.map(|height| {
grid_total_content_height(height, definite_content_height, scrollbar_reservation)
.points()
})
.expect("the Grid layout always supplies a content height");
let fragmentation_content_height = grid_layout
.items
.iter()
.map(|item| item.y() + item.fragmentation_height())
.fold(total_content_height, f32::max);
let needs_contoured_overflow_clip = self.element_used_overflow_clips(element, style)
&& box_content_contour_is_non_rectangular(style);
let overflow_clip_active =
if self.element_used_overflow_clips(element, style) && !needs_contoured_overflow_clip {
self.push_padding_box_overflow_clip(
element,
style,
Some(scrollbar_reservation),
outer_x,
block_top,
border_widths,
content_width,
total_content_height,
)
} else {
false
};
let suppresses_descendant_fragmentation = used_property_containment(element, style).size
|| (overflow_clip_active && definite_content_height.is_some());
let grid_fragment_plan = if suppresses_descendant_fragmentation {
GridFragmentPlan::unfragmented(fragmentainer_kind, total_content_height)
} else {
GridFragmentPlan::from_grid_item_boundaries_with_content_capacity(
fragmentainer_kind,
GridFragmentContentCapacity::new(
GridFragmentBlockSize::new(initial_grid_content_capacity.points()),
GridFragmentBlockSize::new(continuation_grid_content_capacity.points()),
),
fragmentation_content_height,
&grid_layout.rows.line_offsets(),
&grid_layout.items,
&children,
)
};
let grid_fragment_plan_starts_after_break =
grid_fragment_plan.starts_after_fragmentainer_break();
debug_assert!(
!grid_fragment_plan_starts_after_break
|| grid_fragment_plan.requires_multiple_fragments()
);
debug_assert!(
!grid_fragment_plan.requires_multiple_fragments()
|| !grid_fragment_plan.slices().is_empty()
);
const FRAGMENT_COORDINATE_EPSILON: f32 = 0.01;
debug_assert!(
grid_fragment_plan
.fragment_records()
.iter()
.all(|fragment| {
fragment
.item_fragments(&grid_layout.items)
.iter()
.all(|item| {
item.item_index < grid_layout.items.len()
&& item.visible.height() >= -FRAGMENT_COORDINATE_EPSILON
&& item.original.height() + FRAGMENT_COORDINATE_EPSILON
>= item.visible.height()
&& item.content_slice.block_end.points()
+ FRAGMENT_COORDINATE_EPSILON
>= item.content_slice.block_start.points()
})
})
);
debug_assert!(
grid_fragment_plan
.fragment_records()
.iter()
.all(|fragment| {
fragment
.transition_before_fragment
.is_none_or(|transition| {
fragment.fragmentainer_offset > 0
&& transition.next_block_offset.points()
<= fragment.slice.source_block_start.points() + 0.01
&& matches!(
transition.reason,
GridFragmentTransitionReason::InitialOverflow
| GridFragmentTransitionReason::SliceContinuation
)
})
})
);
let grid_fragment_records = grid_fragment_plan.fragment_records();
let can_replay_committed_fragment_records =
grid_fragment_plan.requires_multiple_fragments();
let total_height = border_widths.top
+ style.padding.top
+ total_content_height
+ style.padding.bottom
+ border_widths.bottom;
let positioning_containing_block_mode =
PositionedContainingBlockMode::for_element(element, style);
let establishes_positioning_containing_block = positioning_containing_block_mode.is_some();
let positioned_containing_block_scope =
if let Some(mode) = positioning_containing_block_mode {
let containing_block = ContainingBlock::from_page_top_rect(PageTopRect::new(
outer_x + border_widths.left,
block_top - border_widths.top,
content_width + style.padding.left + style.padding.right,
total_content_height + style.padding.top + style.padding.bottom,
));
Some(self.push_positioned_containing_block(mode, containing_block))
} else {
None
};
let pushed_grid_positioning_scope = if let Some(containing_block) =
positioned_containing_block_scope
.is_some()
.then(|| self.containing_blocks.last().cloned())
.flatten()
{
self.grid_positioning_scopes.push(GridPositioningScope::new(
style,
GridPositioningGeometry {
inner_x,
inner_width: PhysicalContentWidth::new(content_box_pt(inner_width)),
content_top,
content_height: PhysicalContentHeight::new(content_box_pt(
total_content_height,
)),
columns: &grid_layout.columns,
rows: &grid_layout.rows,
},
containing_block.geometry,
));
true
} else {
false
};
let previous_left = self.content_left;
let previous_right = self.content_right;
let previous_containing_block_direction = self.containing_block_direction;
let previous_containing_block_writing_mode = self.containing_block_writing_mode;
self.containing_block_direction = style.used_direction();
self.containing_block_writing_mode = style.writing_mode;
if suppresses_descendant_fragmentation {
self.fragmentation_suppression_depth += 1;
}
for child in &positioned_children {
self.layout_positioned_grid_child(
child,
&children,
PositionedGridStaticContext {
container_style: style,
stylesheets,
inner_x,
inner_width: PhysicalContentWidth::new(content_box_pt(inner_width)),
content_top,
definite_content_height: grid_content_height_basis,
content_height: PhysicalContentHeight::new(content_box_pt(
total_content_height,
)),
columns: &grid_layout.columns,
rows: &grid_layout.rows,
column_line_offsets: &grid_layout.columns.line_offsets(),
row_line_offsets: &grid_layout.rows.line_offsets(),
positioning_containing_block: establishes_positioning_containing_block
.then(|| {
self.containing_blocks
.last()
.map(|context| context.geometry)
})
.flatten(),
},
);
}
let mut committed_gap_fragment_paint_bounds = Vec::new();
let committed_replay_end_cursor = if can_replay_committed_fragment_records {
let mut split_item_replay = std::iter::repeat_with(|| None)
.take(grid_layout.items.len())
.collect::<Vec<Option<ContinuousSourceReplay>>>();
let mut fragment_cursor = GridFragmentCursor::new(
PageTopBlockPosition::new(content_top),
GridFragmentBlockOffset::new(0.0),
);
self.push_float_context();
for fragment_record in &grid_fragment_records {
if let Some(transition) = fragment_record.transition_before_fragment
&& let Some(content_top) = self.materialize_fragmentainer_advance(
transition.fragmentainer_kind,
FragmentainerAdvance::Unforced,
)
{
fragment_cursor =
transition.cursor_after_fragmentainer_advance(PageTopBlockPosition::new(
content_top - fragment_decoration_reservation.block_start().points(),
));
}
self.mark_current_page_flow_content();
let paint_checkpoint = self.current_page.paint_checkpoint();
self.replay_grid_fragment_record_items(
*fragment_record,
style,
&grid_layout,
&children,
&grid_layout.items,
stylesheets,
inner_x,
PageInlineSpan::new(inner_x, inner_width),
fragment_cursor,
&mut split_item_replay,
);
if let Some(bounds) = self
.current_page
.paint_tree_fragment_since(&paint_checkpoint)
.bounds()
{
committed_gap_fragment_paint_bounds.push((*fragment_record, bounds));
}
}
self.pop_float_context();
Some(fragment_cursor)
} else {
self.push_float_context();
for (index, (child, item)) in children.iter().zip(&grid_layout.items).enumerate() {
self.replay_grid_item_with_resolved_axes(
style,
&grid_layout,
child,
item,
grid_layout.baseline_resolutions.get(index),
stylesheets,
inner_x,
PageTopBlockPosition::new(content_top),
);
}
self.pop_float_context();
None
};
if pushed_grid_positioning_scope {
self.grid_positioning_scopes.pop();
}
if suppresses_descendant_fragmentation {
self.fragmentation_suppression_depth -= 1;
}
self.pop_overflow_clip(overflow_clip_active);
self.content_left = previous_left;
self.content_right = previous_right;
self.containing_block_direction = previous_containing_block_direction;
self.containing_block_writing_mode = previous_containing_block_writing_mode;
self.cursor_y = committed_replay_end_cursor
.map(|cursor| {
cursor
.source_block_y(GridFragmentBlockOffset::new(total_content_height))
.points()
})
.unwrap_or(content_top - total_content_height);
self.cursor_y -= style.padding.bottom + border_widths.bottom;
let block_bottom = self.cursor_y;
let block_height = (block_top - block_bottom).max(total_height);
let contents_overflow_edge = (overflow_clip_active || needs_contoured_overflow_clip)
.then(|| {
resolve_overflow_clip_edge(
paint_space_rect(outer_x, block_bottom, outer_width, block_height),
style,
border_widths,
self.used_overflow_axes_for_element(element, style),
containment.clips_descendant_paint(),
None,
)
})
.flatten();
let contents_overflow_clip = contents_overflow_edge.as_ref().map(|edge| edge.clip.bounds);
let contoured_contents_overflow_clip = contents_overflow_edge
.as_ref()
.filter(|edge| edge.clips_x && edge.clips_y)
.map(|edge| edge.clip.clone())
.filter(|clip| !matches!(clip.contour, BoxContentContour::Rect));
if block_height > 0.0 {
self.mark_current_page_flow_content();
}
let grid_gap_items = grid_layout
.items
.iter()
.map(GridItemLayout::gap_decoration_item)
.collect::<Vec<_>>();
let mut own_background_primitives = Vec::new();
let mut own_outline_primitives = Vec::new();
if principal_box_paint_mode.root_paints()
&& style.visibility == Visibility::Visible
&& block_height > 0.0
{
own_background_primitives = self.box_background_primitives(
paint_space_rect(outer_x, block_bottom, outer_width, block_height),
style,
);
own_outline_primitives = self.box_outline_primitives(
paint_space_rect(outer_x, block_bottom, outer_width, block_height),
style,
);
}
let fragments = self.take_positioned_fragments_since(paint_page_index, paint_checkpoint);
let grid_spanned_pages = self.pages.len() != paint_page_index;
let mut decorated_grid_fragment_pages = Vec::new();
for (page_index, mut fragment) in fragments {
fragment.promote_background_border_to_in_flow_block();
fragment.promote_outline_to_in_flow_outline();
if page_index == paint_page_index {
if let Some(contour) = contoured_contents_overflow_clip.clone() {
fragment = fragment.with_contents_effect_scoped_to_box_content_contour(contour);
} else if let Some(clip) = contents_overflow_clip {
fragment = fragment.with_contents_effect_scoped_to_rect(clip);
}
}
if grid_spanned_pages {
let planned_fragment_record = grid_fragment_plan
.fragment_record_for_offset(page_index.saturating_sub(paint_page_index));
let fallback_fragment_bounds = || {
fragment.bounds().map(|bounds| {
PaintClip::from_paint_rect(paint_space_rect(
outer_x,
bounds.y(),
outer_width,
bounds.height(),
))
})
};
let committed_principal_fragment = planned_fragment_record.map(|fragment_record| {
let is_first = fragment_record.slice.source_block_start.points() <= 0.01;
let is_last = !matches!(
fragment_record.slice.break_after,
GridFragmentBreak::RowBoundary | GridFragmentBreak::ForcedRowBoundary
);
let decoration = FragmentDecoration::for_box_decoration_break(
style.box_decoration_break,
is_first,
is_last,
);
let cursor = fragment_record.cursor(PageTopBlockPosition::new(content_top));
let border_box = if decoration.is_clone() {
cursor.decorated_paint_clip(
fragment_record.slice,
border_box_inline_span,
fragment_decoration_reservation,
)
} else {
fragment_record.paint_clip(border_box_inline_span, cursor)
};
fragment_record.principal_box_fragment(
FragmentainerOrdinal::new(page_index),
border_box,
decoration,
)
});
if committed_principal_fragment.is_some() {
decorated_grid_fragment_pages.push(page_index);
}
if let Some(fragment_bounds) = committed_principal_fragment
.as_ref()
.and_then(|fragment| fragment.kind().principal_box())
.map(DecoratedBoxFragment::border_box)
.or_else(fallback_fragment_bounds)
{
let (source_block_start, source_block_end) = planned_fragment_record
.map(GridFragmentRecord::source_range)
.unwrap_or_else(|| {
grid_fragment_source_range_from_bounds(
fragment_bounds,
PageTopBlockPosition::new(content_top),
total_content_height,
)
});
if principal_box_paint_mode.root_paints()
&& style.visibility == Visibility::Visible
&& (style.background.background_color.is_potentially_visible()
|| style.background.background_image.is_image()
|| style.border_image.source.is_image()
|| used_border_width(style) > layout_pt(0.0))
{
let page_background_primitives = self.box_background_primitives(
paint_space_rect(
outer_x,
fragment_bounds.y(),
outer_width,
fragment_bounds.height(),
),
style,
);
fragment.prepend_primitives_in_band(
PaintBand::BackgroundBorder,
page_background_primitives,
);
}
if principal_box_paint_mode.root_paints()
&& style.visibility == Visibility::Visible
{
fragment.append_primitives_in_band(
PaintBand::BackgroundBorder,
grid_gap_decoration_primitives_for_page(GridGapFragmentProjection {
style,
content_origin: PageTopPoint::new(inner_x, content_top),
inner_width: PhysicalContentWidth::new(content_box_pt(inner_width)),
total_content_height,
items: &grid_gap_items,
gutters: &grid_layout.gap_decoration_gutters(style),
source_block_start,
source_block_end,
ends_at_fragment_break: planned_fragment_record.is_some_and(
|record| {
matches!(
record.slice.break_after,
GridFragmentBreak::RowBoundary
| GridFragmentBreak::ForcedRowBoundary
)
},
),
}),
);
}
if principal_box_paint_mode.root_paints()
&& style.visibility == Visibility::Visible
{
let page_outline_primitives = self.box_outline_primitives(
paint_space_rect(
outer_x,
fragment_bounds.y(),
outer_width,
fragment_bounds.height(),
),
style,
);
fragment
.append_primitives_in_band(PaintBand::Outline, page_outline_primitives);
}
}
} else if page_index == paint_page_index {
fragment.prepend_primitives_in_band(
PaintBand::BackgroundBorder,
own_background_primitives.clone(),
);
if principal_box_paint_mode.root_paints() && style.visibility == Visibility::Visible
{
if committed_gap_fragment_paint_bounds.is_empty() {
fragment.append_primitives_in_band(
PaintBand::BackgroundBorder,
grid_gap_decoration_primitives(
style,
GapDecorationContainer::new(
inner_x,
content_top,
grid_used_track_extent(
&grid_layout.columns.line_offsets(),
&grid_layout.items,
GridAxis::Column,
inner_width,
),
grid_used_track_extent(
&grid_layout.rows.line_offsets(),
&grid_layout.items,
GridAxis::Row,
total_content_height,
),
),
&grid_gap_items,
&grid_layout.gap_decoration_gutters(style),
),
);
} else {
for (fragment_record, bounds) in &committed_gap_fragment_paint_bounds {
let (source_block_start, source_block_end) =
fragment_record.source_range();
fragment.append_primitives_in_band(
PaintBand::BackgroundBorder,
grid_gap_decoration_primitives_for_page(
GridGapFragmentProjection {
style,
content_origin: PageTopPoint::new(bounds.x(), content_top),
inner_width: PhysicalContentWidth::new(content_box_pt(
inner_width,
)),
total_content_height,
items: &grid_gap_items,
gutters: &grid_layout.gap_decoration_gutters(style),
source_block_start,
source_block_end,
ends_at_fragment_break: matches!(
fragment_record.slice.break_after,
GridFragmentBreak::RowBoundary
| GridFragmentBreak::ForcedRowBoundary
),
},
),
);
}
}
}
fragment
.append_primitives_in_band(PaintBand::Outline, own_outline_primitives.clone());
}
if !defer_own_decoration_promotion {
fragment.promote_outline_to_in_flow_outline();
}
if !fragment.is_empty() {
let fragment = if grid_spanned_pages {
let context = PaintStackingContext::from_banded_fragment(fragment, Vec::new())
.with_source_order(self.next_paint_source_order());
PaintFragment::from_stacking_context_in_band(PaintBand::InFlowBlock, context)
} else {
fragment
};
if page_index < self.pages.len() {
self.pages[page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
}
}
}
if grid_spanned_pages
&& principal_box_paint_mode.root_paints()
&& style.visibility == Visibility::Visible
{
for fragment_record in &grid_fragment_records {
let page_index = paint_page_index + fragment_record.fragmentainer_offset;
if decorated_grid_fragment_pages.contains(&page_index) {
continue;
}
let is_first = fragment_record.slice.source_block_start.points() <= 0.01;
let is_last = !matches!(
fragment_record.slice.break_after,
GridFragmentBreak::RowBoundary | GridFragmentBreak::ForcedRowBoundary
);
let decoration = FragmentDecoration::for_box_decoration_break(
style.box_decoration_break,
is_first,
is_last,
);
let cursor = fragment_record.cursor(PageTopBlockPosition::new(content_top));
let border_box = if decoration.is_clone() {
cursor.decorated_paint_clip(
fragment_record.slice,
border_box_inline_span,
fragment_decoration_reservation,
)
} else {
fragment_record.paint_clip(border_box_inline_span, cursor)
};
let committed_fragment = fragment_record.principal_box_fragment(
FragmentainerOrdinal::new(page_index),
border_box,
decoration,
);
let principal = committed_fragment
.kind()
.principal_box()
.expect("committed grid source slice owns a principal box");
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
let border_rect = principal.border_box().paint_rect();
fragment.prepend_primitives_in_band(
PaintBand::BackgroundBorder,
self.box_background_primitives(border_rect, style),
);
fragment.append_primitives_in_band(
PaintBand::Outline,
self.box_outline_primitives(border_rect, style),
);
if !defer_own_decoration_promotion {
fragment.promote_outline_to_in_flow_outline();
}
let fragment = PaintFragment::from_stacking_context_in_band(
PaintBand::InFlowBlock,
PaintStackingContext::from_banded_fragment(fragment, Vec::new())
.with_source_order(self.next_paint_source_order()),
);
if page_index < self.pages.len() {
self.pages[page_index]
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else if page_index == self.pages.len() {
self.current_page
.append_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
self.pending_paint_fragments.push(PendingPaintFragment {
page_index,
fragment,
kind: PendingPaintFragmentKind::InFlowOverflow,
});
}
}
}
self.cursor_y -= style.margin.bottom;
if let Some(scope) = positioned_containing_block_scope {
self.pop_positioned_containing_block(scope);
}
if style.position.is_in_flow_positioned() {
self.cursor_y -= relative_offset.y();
}
self.apply_forced_break_after_box_in(fragmentainer_kind, style);
}
pub(in crate::layout) fn intrinsic_inline_grid_atom_for_element(
&mut self,
element: &Element,
style: &ComputedStyle,
child_boxes: &[box_tree::FormattingBox<'_>],
stylesheets: &Stylesheets<'_>,
baseline_shift: f32,
link_target: Option<String>,
) -> InlineAtom {
let mut used_style =
GridUsedStyle::from_normalized(self.style_with_current_viewport_lengths(style));
if layout_containment_applies_to_element(element, &used_style)
|| paint_containment_applies_to_element(element, &used_style)
{
used_style.grid_template_rows.resolve_contained_subgrid();
used_style.grid_template_columns.resolve_contained_subgrid();
}
let box_metrics = intrinsic_box_metrics(&used_style);
used_style.margin = box_metrics.margin.to_css_edges();
used_style.padding = box_metrics.padding.to_css_edges();
let available_width = (self.content_right
- self.content_left
- box_metrics.margin.left.points()
- box_metrics.margin.right.points())
.max(used_style.font_size);
let style = &used_style;
let scrollbar_reservation = grid_scrollbar_reservation(style);
let containment = used_property_containment(element, style);
let horizontal_extras = box_metrics.horizontal_non_content_length().points();
let vertical_extras = box_metrics.vertical_non_content_length().points();
let (children, _) = grid_child_lists_from_boxes(child_boxes);
let children = self.prepare_grid_children(children);
let definite_content_height = used_content_box_height_or_auto(
style,
layout_pt(style.line_height.max(1.0)),
non_content_pt(vertical_extras),
)
.map(SemanticLengthExt::points)
.map(|height| {
PhysicalContentHeight::new(constrain_content_height(
style,
content_box_pt(height),
PercentageBasis::definite(layout_pt(available_width)),
))
});
let (mut min_width, mut max_width) = if intrinsic_physical_width_is_contained(style) {
self.size_contained_grid_intrinsic_widths(style)
} else {
self.estimate_grid_intrinsic_widths(
element,
style,
stylesheets,
available_width,
Some(child_boxes),
)
};
if let Some(width) = self.grid_inline_intrinsic_width_with_resolved_rows(
style,
&children,
stylesheets,
definite_content_height,
) {
min_width = min_width.max(width);
max_width = max_width.max(width).max(min_width);
}
let requested_content_width = crate::layout::intrinsic::content_box_width_from_intrinsic(
style,
layout_pt(available_width),
non_content_pt(horizontal_extras),
content_box_pt(min_width),
content_box_pt(max_width),
crate::layout::intrinsic::IntrinsicAutoWidth::ShrinkToFit,
);
let content_width = constrain_content_width(
style,
requested_content_width,
PercentageBasis::definite(layout_pt(available_width.max(0.0))),
)
.points();
let grid_content_width = grid_scrollport_content_width(
PhysicalContentWidth::new(content_box_pt(content_width)),
scrollbar_reservation,
);
let size_contained_content_height = if containment.size {
let empty_grid_height = self
.compute_grid_layout(
style,
&[],
stylesheets,
grid_content_width,
grid_scrollport_content_height_basis(
definite_content_height,
scrollbar_reservation,
),
GridLayoutPurpose::IntrinsicProbe,
)
.map(|layout| layout.height)
.unwrap_or_else(|| PhysicalContentHeight::new(content_box_pt(0.0)));
Some(
grid_scrollport_content_height_basis(
definite_content_height,
scrollbar_reservation,
)
.unwrap_or_else(|| {
PhysicalContentHeight::new(constrain_content_height(
style,
empty_grid_height.content_box_length(),
PercentageBasis::definite(layout_pt(available_width)),
))
}),
)
} else {
None
};
let grid_layout = self.compute_grid_layout(
style,
&children,
stylesheets,
grid_content_width,
size_contained_content_height.or_else(|| {
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation)
}),
GridLayoutPurpose::IntrinsicProbe,
);
let content_height = size_contained_content_height.unwrap_or_else(|| {
let measured = grid_layout
.as_ref()
.map(|layout| layout.height.points())
.unwrap_or(style.line_height)
.max(style.line_height);
PhysicalContentHeight::new(constrain_content_height(
style,
content_box_pt(measured),
PercentageBasis::definite(layout_pt(available_width)),
))
});
let content_height = grid_total_content_height(
content_height,
definite_content_height,
scrollbar_reservation,
)
.points();
let border_box_height = content_height + vertical_extras;
let baseline_offset = grid_layout
.as_ref()
.and_then(|layout| layout.baselines.vertical.first)
.map(PhysicalTopBaselineOffset::points)
.map(|baseline| box_metrics.border.top.points() + style.padding.top + baseline)
.unwrap_or(border_box_height);
InlineAtom::new(
InlineAtomContent::Svg { asset: None },
style.clone_used_style(),
None,
InlineSize::new(
content_width + horizontal_extras + style.margin.left + style.margin.right,
border_box_height + style.margin.top + style.margin.bottom,
),
baseline_offset,
baseline_shift,
link_target,
None,
)
}
fn grid_inline_intrinsic_width_with_resolved_rows(
&mut self,
style: &ComputedStyle,
children: &[GridChild<'_>],
stylesheets: &Stylesheets<'_>,
block_size: Option<PhysicalContentHeight>,
) -> Option<f32> {
let block_size = block_size?;
let layout = self.compute_grid_layout(
style,
children,
stylesheets,
PhysicalContentWidth::new(content_box_pt(0.0)),
Some(block_size),
GridLayoutPurpose::IntrinsicProbe,
)?;
(!WritingModeAxes::new(style.writing_mode, style.used_direction()).swaps_physical_axes())
.then(|| {
layout
.columns
.line_offsets()
.last()
.copied()
.unwrap_or_default()
.max(0.0)
})
}
pub(in crate::layout) fn inline_grid_atom_for_element(
&mut self,
element: &Element,
style: &ComputedStyle,
child_boxes: &[box_tree::FormattingBox<'_>],
stylesheets: &Stylesheets<'_>,
baseline_shift: f32,
link_target: Option<String>,
) -> InlineAtom {
let mut used_style =
GridUsedStyle::from_normalized(self.style_with_current_viewport_lengths(style));
if layout_containment_applies_to_element(element, &used_style)
|| paint_containment_applies_to_element(element, &used_style)
{
used_style.grid_template_rows.resolve_contained_subgrid();
used_style.grid_template_columns.resolve_contained_subgrid();
}
let available_width = (self.content_right
- self.content_left
- used_style.margin.left
- used_style.margin.right)
.max(used_style.font_size);
let box_metrics = apply_used_box_metrics(
&mut used_style,
PercentageBasis::definite(layout_pt(available_width)),
);
let style = &used_style;
let scrollbar_reservation = grid_scrollbar_reservation(style);
let containment = used_property_containment(element, style);
let border_widths = box_metrics.border.to_css_edges();
let horizontal_extras = box_metrics.horizontal_non_content_length().points();
let vertical_extras = box_metrics.vertical_non_content_length().points();
let (children, positioned_children) = grid_child_lists_from_boxes(child_boxes);
let children = self.prepare_grid_children(children);
let positioned_children = self.prepare_grid_children(positioned_children);
let definite_content_height = used_content_box_height_or_auto(
style,
layout_pt(style.line_height.max(1.0)),
non_content_pt(vertical_extras),
)
.map(SemanticLengthExt::points)
.map(|height| {
PhysicalContentHeight::new(constrain_content_height(
style,
content_box_pt(height),
PercentageBasis::definite(layout_pt(available_width)),
))
});
let (mut min_width, mut max_width) = if intrinsic_physical_width_is_contained(style) {
self.size_contained_grid_intrinsic_widths(style)
} else {
self.estimate_grid_intrinsic_widths(
element,
style,
stylesheets,
available_width,
Some(child_boxes),
)
};
if let Some(width) = self.grid_inline_intrinsic_width_with_resolved_rows(
style,
&children,
stylesheets,
definite_content_height,
) {
min_width = min_width.max(width);
max_width = max_width.max(width).max(min_width);
}
let requested_content_width = crate::layout::intrinsic::content_box_width_from_intrinsic(
style,
layout_pt(available_width),
non_content_pt(horizontal_extras),
content_box_pt(min_width),
content_box_pt(max_width),
crate::layout::intrinsic::IntrinsicAutoWidth::ShrinkToFit,
);
let content_width = constrain_content_width(
style,
requested_content_width,
PercentageBasis::definite(layout_pt(available_width.max(0.0))),
)
.points();
let grid_content_width = grid_scrollport_content_width(
PhysicalContentWidth::new(content_box_pt(content_width)),
scrollbar_reservation,
);
let size_contained_content_height = if containment.size {
let empty_grid_height = self
.compute_grid_layout(
style,
&[],
stylesheets,
grid_content_width,
grid_scrollport_content_height_basis(
definite_content_height,
scrollbar_reservation,
),
GridLayoutPurpose::IntrinsicProbe,
)
.map(|layout| layout.height)
.unwrap_or_else(|| PhysicalContentHeight::new(content_box_pt(0.0)));
Some(
grid_scrollport_content_height_basis(
definite_content_height,
scrollbar_reservation,
)
.unwrap_or_else(|| {
PhysicalContentHeight::new(constrain_content_height(
style,
empty_grid_height.content_box_length(),
PercentageBasis::definite(layout_pt(available_width)),
))
}),
)
} else {
None
};
let grid_content_height_basis = size_contained_content_height.or_else(|| {
grid_scrollport_content_height_basis(definite_content_height, scrollbar_reservation)
});
let Some(grid_layout) = self.compute_grid_layout(
style,
&children,
stylesheets,
grid_content_width,
grid_content_height_basis,
GridLayoutPurpose::FinalLayout,
) else {
return self.inline_fragment_atom_for_children(
None,
style,
child_boxes,
stylesheets,
baseline_shift,
link_target,
);
};
let total_content_height = size_contained_content_height.unwrap_or_else(|| {
PhysicalContentHeight::new(constrain_content_height(
style,
grid_layout.height.content_box_length(),
PercentageBasis::definite(layout_pt(available_width)),
))
});
let total_content_height = grid_total_content_height(
total_content_height,
definite_content_height,
scrollbar_reservation,
)
.points();
let border_box_height = total_content_height + vertical_extras;
let pending_outside_marker_anchors = self.pending_outside_marker_anchors.suspend();
let snapshot = self.snapshot();
let positioned_layer_start = self.positioned_layers.len();
let fixed_layer_start = self.fixed_layers.len();
let top = 10_000.0;
let content_top = top - border_widths.top - style.padding.top;
let inner_x = border_widths.left + style.padding.left;
let inner_width = grid_content_width.points();
self.current_page = Page::new(content_width + horizontal_extras, top);
self.content_left = inner_x;
self.content_right = inner_x + inner_width;
self.cursor_y = content_top;
self.truncate_page_start_margins = false;
let positioning_containing_block_mode =
PositionedContainingBlockMode::for_element(element, style);
let establishes_positioning_containing_block = positioning_containing_block_mode.is_some();
let positioned_containing_block_scope =
if let Some(mode) = positioning_containing_block_mode {
let containing_block = ContainingBlock::from_page_top_rect(PageTopRect::new(
border_widths.left,
top - border_widths.top,
content_width + style.padding.left + style.padding.right,
total_content_height + style.padding.top + style.padding.bottom,
));
Some(self.push_positioned_containing_block(mode, containing_block))
} else {
None
};
let pushed_grid_positioning_scope = if let Some(containing_block) =
positioned_containing_block_scope
.is_some()
.then(|| self.containing_blocks.last().cloned())
.flatten()
{
self.grid_positioning_scopes.push(GridPositioningScope::new(
style,
GridPositioningGeometry {
inner_x,
inner_width: PhysicalContentWidth::new(content_box_pt(inner_width)),
content_top,
content_height: PhysicalContentHeight::new(content_box_pt(
total_content_height,
)),
columns: &grid_layout.columns,
rows: &grid_layout.rows,
},
containing_block.geometry,
));
true
} else {
false
};
self.push_page_name_scope_suppression();
let suppresses_descendant_fragmentation = containment.size;
if suppresses_descendant_fragmentation {
self.fragmentation_suppression_depth += 1;
}
self.push_float_context();
for (index, (child, item)) in children.iter().zip(&grid_layout.items).enumerate() {
self.replay_grid_item_with_resolved_axes(
style,
&grid_layout,
child,
item,
grid_layout.baseline_resolutions.get(index),
stylesheets,
inner_x,
PageTopBlockPosition::new(content_top),
);
}
self.pop_float_context();
for child in &positioned_children {
self.layout_positioned_grid_child(
child,
&children,
PositionedGridStaticContext {
container_style: style,
stylesheets,
inner_x,
inner_width: PhysicalContentWidth::new(content_box_pt(inner_width)),
content_top,
definite_content_height: grid_content_height_basis,
content_height: PhysicalContentHeight::new(content_box_pt(
total_content_height,
)),
columns: &grid_layout.columns,
rows: &grid_layout.rows,
column_line_offsets: &grid_layout.columns.line_offsets(),
row_line_offsets: &grid_layout.rows.line_offsets(),
positioning_containing_block: establishes_positioning_containing_block
.then(|| {
self.containing_blocks
.last()
.map(|context| context.geometry)
})
.flatten(),
},
);
}
if pushed_grid_positioning_scope {
self.grid_positioning_scopes.pop();
}
if suppresses_descendant_fragmentation {
self.fragmentation_suppression_depth -= 1;
}
self.pop_page_name_scope_suppression();
if let Some(scope) = positioned_containing_block_scope {
self.pop_positioned_containing_block(scope);
}
let border_bottom = top - border_box_height;
self.flush_positioned_layers_since(positioned_layer_start);
let mut fragment = self.current_page.paint_fragment();
if style.visibility == Visibility::Visible {
fragment.prepend_primitives_in_band(
PaintBand::BackgroundBorder,
grid_gap_decoration_primitives(
style,
GapDecorationContainer::new(
inner_x,
content_top,
grid_used_track_extent(
&grid_layout.columns.line_offsets(),
&grid_layout.items,
GridAxis::Column,
inner_width,
),
grid_used_track_extent(
&grid_layout.rows.line_offsets(),
&grid_layout.items,
GridAxis::Row,
total_content_height,
),
),
&grid_layout
.items
.iter()
.map(GridItemLayout::gap_decoration_item)
.collect::<Vec<_>>(),
&grid_layout.gap_decoration_gutters(style),
),
);
}
let fragment = fragment.translated(PaintTranslation::new(0.0, -border_bottom));
let descendant_baseline = (!containment.layout)
.then_some(grid_layout.baselines.vertical.first)
.flatten()
.map(PhysicalTopBaselineOffset::points)
.map(|baseline| border_widths.top + style.padding.top + baseline)
.or_else(|| {
(!containment.layout)
.then(|| fragment.first_line_y())
.flatten()
.map(|line_y| (border_box_height - line_y).max(0.0))
});
let baseline_offset = LayoutBuilder::inline_block_baseline_offset_with_containment(
style.used_style(),
containment.layout,
border_box_height,
descendant_baseline,
);
let inline_lanes_overflow_clearance =
grid_lanes_inline_overflow_clearance(style.used_style(), &grid_layout);
let fixed_layers = self.fixed_layers.split_off(fixed_layer_start);
self.restore(snapshot);
self.pending_outside_marker_anchors
.restore(pending_outside_marker_anchors);
self.fixed_layers.extend(fixed_layers);
let mut atom_style = style.clone_used_style();
atom_style.margin.bottom += inline_lanes_overflow_clearance;
InlineAtom::new(
InlineAtomContent::InlineFragment {
fragment: Box::new(fragment),
replay_coordinates: AtomicInlineFragmentReplayCoordinates::border_box_local(),
table_cell_context: None,
contents_overflow_clip_applied: false,
},
atom_style,
None,
InlineSize::new(
content_width + horizontal_extras + style.margin.left + style.margin.right,
border_box_height
+ inline_lanes_overflow_clearance
+ style.margin.top
+ style.margin.bottom,
),
baseline_offset,
baseline_shift,
link_target,
None,
)
}
}
fn grid_lanes_inline_overflow_clearance(style: &ComputedStyle, layout: &GridLayout) -> f32 {
if !style.display.is_grid_lanes()
|| !grid_lanes_stacking_axis_is_block(style)
|| style.vertical_align != VerticalAlign::BASELINE
|| !layout.items.iter().any(|item| {
item.final_percentage_axes().height && item.height() > layout.height.points() + 0.01
})
{
return 0.0;
}
layout
.items
.iter()
.filter(|item| {
item.final_percentage_axes().height && item.height() > layout.height.points() + 0.01
})
.map(GridItemLayout::x)
.filter(|offset| *offset > 0.01)
.min_by(|left, right| left.total_cmp(right))
.unwrap_or(0.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn static_pdf_grid_overlay_keeps_track_and_outer_sizes_identical() {
let mut style = ComputedStyle::initial();
style.overflow_x = css::Overflow::Scroll;
style.overflow_y = css::Overflow::Scroll;
let reservation = grid_scrollbar_reservation(&style);
assert_eq!(
grid_scrollport_content_width(
PhysicalContentWidth::new(content_box_pt(100.0)),
reservation,
)
.points(),
100.0,
);
assert_eq!(
grid_scrollport_content_height_basis(
Some(PhysicalContentHeight::new(content_box_pt(100.0))),
reservation,
)
.unwrap()
.points(),
100.0,
);
assert_eq!(
grid_total_content_height(
PhysicalContentHeight::new(content_box_pt(100.0)),
None,
reservation,
)
.points(),
100.0,
);
assert_eq!(
grid_total_content_height(
PhysicalContentHeight::new(content_box_pt(85.0)),
Some(PhysicalContentHeight::new(content_box_pt(100.0))),
reservation,
)
.points(),
100.0,
);
}
}