layout: use Au in BoxFragment (#31794)

* use au in BoxFragement

* review fix
This commit is contained in:
atbrakhi 2024-03-22 10:00:39 +05:30 committed by GitHub
parent 0b863de846
commit 95e69fe4ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 98 additions and 100 deletions

View file

@ -140,6 +140,18 @@ impl ToWebRender for PhysicalSides<Length> {
} }
} }
impl ToWebRender for PhysicalSides<Au> {
type Type = units::LayoutSideOffsets;
fn to_webrender(&self) -> Self::Type {
units::LayoutSideOffsets::new(
self.top.to_f32_px(),
self.right.to_f32_px(),
self.bottom.to_f32_px(),
self.left.to_f32_px(),
)
}
}
impl ToWebRender for ComputedTextDecorationStyle { impl ToWebRender for ComputedTextDecorationStyle {
type Type = LineStyle; type Type = LineStyle;
fn to_webrender(&self) -> Self::Type { fn to_webrender(&self) -> Self::Type {

View file

@ -809,16 +809,13 @@ impl<'a> BuilderForBoxFragment<'a> {
let border_widths = self let border_widths = self
.fragment .fragment
.border .border
.to_physical(self.fragment.style.writing_mode); .to_physical(self.fragment.style.writing_mode)
let widths = SideOffsets2D::new( .to_webrender();
border_widths.top.px(),
border_widths.right.px(), if border_widths == SideOffsets2D::zero() {
border_widths.bottom.px(),
border_widths.left.px(),
);
if widths == SideOffsets2D::zero() {
return; return;
} }
let common = builder.common_properties(self.border_rect, &self.fragment.style); let common = builder.common_properties(self.border_rect, &self.fragment.style);
let details = wr::BorderDetails::Normal(wr::NormalBorder { let details = wr::BorderDetails::Normal(wr::NormalBorder {
top: self.build_border_side(border.border_top_style, border.border_top_color.clone()), top: self.build_border_side(border.border_top_style, border.border_top_color.clone()),
@ -835,7 +832,7 @@ impl<'a> BuilderForBoxFragment<'a> {
}); });
builder builder
.wr() .wr()
.push_border(&common, self.border_rect, widths, details) .push_border(&common, self.border_rect, border_widths, details)
} }
fn build_outline(&mut self, builder: &mut DisplayListBuilder) { fn build_outline(&mut self, builder: &mut DisplayListBuilder) {

View file

@ -63,16 +63,6 @@ impl<T> FlexRelativeSides<T> {
cross: self.cross_start + self.cross_end, cross: self.cross_start + self.cross_end,
} }
} }
// TODO(#29819): Check if this function can be removed after we convert everything to Au.
pub fn map<U>(&self, f: impl Fn(&T) -> U) -> FlexRelativeSides<U> {
FlexRelativeSides {
main_start: f(&self.main_start),
main_end: f(&self.main_end),
cross_start: f(&self.cross_start),
cross_end: f(&self.cross_end),
}
}
} }
/// One of the two bits set by the `flex-direction` property /// One of the two bits set by the `flex-direction` property

View file

@ -856,9 +856,9 @@ impl FlexLine<'_> {
item.box_.style().clone(), item.box_.style().clone(),
item_result.fragments, item_result.fragments,
content_rect, content_rect,
flex_context.sides_to_flow_relative(item.padding.map(|t| (*t).into())), flex_context.sides_to_flow_relative(item.padding),
flex_context.sides_to_flow_relative(item.border.map(|t| (*t).into())), flex_context.sides_to_flow_relative(item.border),
margin, margin.into(),
None, /* clearance */ None, /* clearance */
collapsed_margin, collapsed_margin,
), ),

View file

@ -25,7 +25,7 @@ use crate::dom::NodeExt;
use crate::dom_traversal::{Contents, NodeAndStyleInfo}; use crate::dom_traversal::{Contents, NodeAndStyleInfo};
use crate::formatting_contexts::IndependentFormattingContext; use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::{BoxFragment, CollapsedBlockMargins, CollapsedMargin, FloatFragment}; use crate::fragment_tree::{BoxFragment, CollapsedBlockMargins, CollapsedMargin, FloatFragment};
use crate::geom::{LogicalRect, LogicalSides, LogicalVec2}; use crate::geom::{LogicalRect, LogicalVec2};
use crate::positioned::PositioningContext; use crate::positioned::PositioningContext;
use crate::style_ext::{ComputedValuesExt, DisplayInside, PaddingBorderMargin}; use crate::style_ext::{ComputedValuesExt, DisplayInside, PaddingBorderMargin};
use crate::ContainingBlock; use crate::ContainingBlock;
@ -989,9 +989,9 @@ impl FloatBox {
style.clone(), style.clone(),
children, children,
content_rect, content_rect,
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin.into(), margin,
// Clearance is handled internally by the float placement logic, so there's no need // Clearance is handled internally by the float placement logic, so there's no need
// to store it explicitly in the fragment. // to store it explicitly in the fragment.
None, // clearance None, // clearance
@ -1215,9 +1215,8 @@ impl SequentialLayoutState {
let pbm_sums = &(&box_fragment.padding + &box_fragment.border) + &box_fragment.margin; let pbm_sums = &(&box_fragment.padding + &box_fragment.border) + &box_fragment.margin;
let content_rect: LogicalRect<Au> = box_fragment.content_rect.clone().into(); let content_rect: LogicalRect<Au> = box_fragment.content_rect.clone().into();
let pbm_sums_all: LogicalSides<Au> = pbm_sums.map(|length| (*length).into());
let margin_box_start_corner = self.floats.add_float(&PlacementInfo { let margin_box_start_corner = self.floats.add_float(&PlacementInfo {
size: &content_rect.size + &pbm_sums_all.sum(), size: &content_rect.size + &pbm_sums.sum(),
side: FloatSide::from_style(&box_fragment.style).expect("Float box wasn't floated!"), side: FloatSide::from_style(&box_fragment.style).expect("Float box wasn't floated!"),
clear: box_fragment.style.get_box().clear, clear: box_fragment.style.get_box().clear,
}); });
@ -1225,7 +1224,7 @@ impl SequentialLayoutState {
// This is the position of the float in the float-containing block formatting context. We add the // This is the position of the float in the float-containing block formatting context. We add the
// existing start corner here because we may have already gotten some relative positioning offset. // existing start corner here because we may have already gotten some relative positioning offset.
let new_position_in_bfc = let new_position_in_bfc =
&(&margin_box_start_corner + &pbm_sums_all.start_offset()) + &content_rect.start_corner; &(&margin_box_start_corner + &pbm_sums.start_offset()) + &content_rect.start_corner;
// This is the position of the float relative to the containing block start. // This is the position of the float relative to the containing block start.
let new_position_in_containing_block = LogicalVec2 { let new_position_in_containing_block = LogicalVec2 {

View file

@ -1036,7 +1036,7 @@ impl<'a, 'b> InlineFormattingContextState<'a, 'b> {
let margin_box = float_item let margin_box = float_item
.fragment .fragment
.border_rect() .border_rect()
.inflate(&float_item.fragment.margin); .inflate(&float_item.fragment.margin.map(|t| (*t).into()));
let inline_size = margin_box.size.inline.max(Length::zero()); let inline_size = margin_box.size.inline.max(Length::zero());
let available_inline_size = match self.current_line.placement_among_floats.get() { let available_inline_size = match self.current_line.placement_among_floats.get() {
@ -2006,9 +2006,9 @@ impl IndependentFormattingContext {
replaced.style.clone(), replaced.style.clone(),
fragments, fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin.into(), margin,
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(), CollapsedBlockMargins::zero(),
) )
@ -2095,9 +2095,9 @@ impl IndependentFormattingContext {
non_replaced.style.clone(), non_replaced.style.clone(),
independent_layout.fragments, independent_layout.fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin.into(), margin,
None, None,
CollapsedBlockMargins::zero(), CollapsedBlockMargins::zero(),
) )

View file

@ -378,9 +378,9 @@ impl InlineBoxLineItem {
self.style.clone(), self.style.clone(),
fragments, fragments,
content_rect, content_rect,
padding.into(), padding,
border.into(), border,
margin.into(), margin,
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(), CollapsedBlockMargins::zero(),
); );

View file

@ -814,10 +814,10 @@ fn layout_in_flow_non_replaced_block_level_same_formatting_context(
style.clone(), style.clone(),
flow_layout.fragments, flow_layout.fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin, margin.into(),
clearance.map(|t| t.into()), clearance,
block_margins_collapsed_with_children, block_margins_collapsed_with_children,
) )
.with_baselines(flow_layout.baselines) .with_baselines(flow_layout.baselines)
@ -900,9 +900,9 @@ impl NonReplacedFormattingContext {
self.style.clone(), self.style.clone(),
layout.fragments, layout.fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin, margin.into(),
None, /* clearance */ None, /* clearance */
block_margins_collapsed_with_children, block_margins_collapsed_with_children,
) )
@ -1091,10 +1091,9 @@ impl NonReplacedFormattingContext {
// prevent margin collapse. // prevent margin collapse.
clearance = if clear_position.is_some() || placement_rect.start_corner.block > ceiling { clearance = if clear_position.is_some() || placement_rect.start_corner.block > ceiling {
Some( Some(
(placement_rect.start_corner.block - placement_rect.start_corner.block -
sequential_layout_state sequential_layout_state
.position_with_zero_clearance(&collapsed_margin_block_start)) .position_with_zero_clearance(&collapsed_margin_block_start),
.into(),
) )
} else { } else {
None None
@ -1130,7 +1129,8 @@ impl NonReplacedFormattingContext {
sequential_layout_state.collapse_margins(); sequential_layout_state.collapse_margins();
sequential_layout_state.advance_block_position( sequential_layout_state.advance_block_position(
pbm.padding_border_sums.block + pbm.padding_border_sums.block +
(content_size.block + clearance.unwrap_or_else(Length::zero)).into(), Au::from(content_size.block) +
clearance.unwrap_or_else(Au::zero),
); );
sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin.block_end)); sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin.block_end));
@ -1138,7 +1138,7 @@ impl NonReplacedFormattingContext {
start_corner: LogicalVec2 { start_corner: LogicalVec2 {
block: pbm.padding.block_start + block: pbm.padding.block_start +
pbm.border.block_start + pbm.border.block_start +
clearance.unwrap_or_else(Length::zero).into(), clearance.unwrap_or_else(Au::zero),
inline: pbm.padding.inline_start + inline: pbm.padding.inline_start +
pbm.border.inline_start + pbm.border.inline_start +
effective_margin_inline_start, effective_margin_inline_start,
@ -1152,9 +1152,9 @@ impl NonReplacedFormattingContext {
self.style.clone(), self.style.clone(),
layout.fragments, layout.fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin, margin.into(),
clearance, clearance,
block_margins_collapsed_with_children, block_margins_collapsed_with_children,
) )
@ -1218,7 +1218,7 @@ fn layout_in_flow_replaced_block_level(
// Margins can never collapse into replaced elements. // Margins can never collapse into replaced elements.
sequential_layout_state.collapse_margins(); sequential_layout_state.collapse_margins();
sequential_layout_state sequential_layout_state
.advance_block_position(size.block + clearance.unwrap_or_else(Length::zero).into()); .advance_block_position(size.block + clearance.unwrap_or_else(Au::zero));
sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin_block_end.into())); sequential_layout_state.adjoin_assign(&CollapsedMargin::new(margin_block_end.into()));
} else { } else {
clearance = None; clearance = None;
@ -1242,7 +1242,7 @@ fn layout_in_flow_replaced_block_level(
let start_corner = LogicalVec2 { let start_corner = LogicalVec2 {
block: pbm.padding.block_start + block: pbm.padding.block_start +
pbm.border.block_start + pbm.border.block_start +
clearance.unwrap_or_else(Length::zero).into(), clearance.unwrap_or_else(Au::zero),
inline: pbm.padding.inline_start + pbm.border.inline_start + effective_margin_inline_start, inline: pbm.padding.inline_start + pbm.border.inline_start + effective_margin_inline_start,
}; };
@ -1257,9 +1257,9 @@ fn layout_in_flow_replaced_block_level(
style.clone(), style.clone(),
fragments, fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin, margin.into(),
clearance, clearance,
block_margins_collapsed_with_children, block_margins_collapsed_with_children,
) )
@ -1492,7 +1492,7 @@ fn solve_clearance_and_inline_margins_avoiding_floats(
pbm: &PaddingBorderMargin, pbm: &PaddingBorderMargin,
size: LogicalVec2<Length>, size: LogicalVec2<Length>,
style: &Arc<ComputedValues>, style: &Arc<ComputedValues>,
) -> (Option<Length>, (Au, Au), Au) { ) -> (Option<Au>, (Au, Au), Au) {
let (clearance, placement_rect) = sequential_layout_state let (clearance, placement_rect) = sequential_layout_state
.calculate_clearance_and_inline_adjustment( .calculate_clearance_and_inline_adjustment(
style.get_box().clear, style.get_box().clear,
@ -1507,11 +1507,7 @@ fn solve_clearance_and_inline_margins_avoiding_floats(
size.inline, size.inline,
placement_rect.into(), placement_rect.into(),
); );
( (clearance, inline_margins, effective_margin_inline_start)
clearance.map(|t| t.into()),
inline_margins,
effective_margin_inline_start,
)
} }
/// State that we maintain when placing blocks. /// State that we maintain when placing blocks.
@ -1590,7 +1586,7 @@ impl PlacementState {
let fragment_block_margins = &fragment.block_margins_collapsed_with_children; let fragment_block_margins = &fragment.block_margins_collapsed_with_children;
let mut fragment_block_size = fragment.padding.block_sum() + let mut fragment_block_size = fragment.padding.block_sum() +
fragment.border.block_sum() + fragment.border.block_sum() +
fragment.content_rect.size.block; fragment.content_rect.size.block.into();
// We use `last_in_flow_margin_collapses_with_parent_end_margin` to implement // We use `last_in_flow_margin_collapses_with_parent_end_margin` to implement
// this quote from https://drafts.csswg.org/css2/#collapsing-margins // this quote from https://drafts.csswg.org/css2/#collapsing-margins
// > If the top and bottom margins of an element with clearance are adjoining, // > If the top and bottom margins of an element with clearance are adjoining,
@ -1632,12 +1628,12 @@ impl PlacementState {
if fragment_block_margins.collapsed_through { if fragment_block_margins.collapsed_through {
// `fragment_block_size` is typically zero when collapsing through, // `fragment_block_size` is typically zero when collapsing through,
// but we still need to consider it in case there is clearance. // but we still need to consider it in case there is clearance.
self.current_block_direction_position += fragment_block_size; self.current_block_direction_position += fragment_block_size.into();
self.current_margin self.current_margin
.adjoin_assign(&fragment_block_margins.end); .adjoin_assign(&fragment_block_margins.end);
} else { } else {
self.current_block_direction_position += self.current_block_direction_position +=
self.current_margin.solve() + fragment_block_size; self.current_margin.solve() + fragment_block_size.into();
self.current_margin = fragment_block_margins.end; self.current_margin = fragment_block_margins.end;
} }
}, },

View file

@ -52,9 +52,9 @@ pub(crate) struct BoxFragment {
/// <https://drafts.csswg.org/css-writing-modes/#orthogonal-flows> /// <https://drafts.csswg.org/css-writing-modes/#orthogonal-flows>
pub content_rect: LogicalRect<Length>, pub content_rect: LogicalRect<Length>,
pub padding: LogicalSides<Length>, pub padding: LogicalSides<Au>,
pub border: LogicalSides<Length>, pub border: LogicalSides<Au>,
pub margin: LogicalSides<Length>, pub margin: LogicalSides<Au>,
/// When the `clear` property is not set to `none`, it may introduce clearance. /// When the `clear` property is not set to `none`, it may introduce clearance.
/// Clearance is some extra spacing that is added above the top margin, /// Clearance is some extra spacing that is added above the top margin,
@ -62,7 +62,7 @@ pub(crate) struct BoxFragment {
/// The presence of clearance prevents the top margin from collapsing with /// The presence of clearance prevents the top margin from collapsing with
/// earlier margins or with the bottom margin of the parent block. /// earlier margins or with the bottom margin of the parent block.
/// <https://drafts.csswg.org/css2/#clearance> /// <https://drafts.csswg.org/css2/#clearance>
pub clearance: Option<Length>, pub clearance: Option<Au>,
/// When this [`BoxFragment`] is for content that has a baseline, this tracks /// When this [`BoxFragment`] is for content that has a baseline, this tracks
/// the first and last baselines of that content. This is used to propagate baselines /// the first and last baselines of that content. This is used to propagate baselines
@ -93,10 +93,10 @@ impl BoxFragment {
style: ServoArc<ComputedValues>, style: ServoArc<ComputedValues>,
children: Vec<Fragment>, children: Vec<Fragment>,
content_rect: LogicalRect<Length>, content_rect: LogicalRect<Length>,
padding: LogicalSides<Length>, padding: LogicalSides<Au>,
border: LogicalSides<Length>, border: LogicalSides<Au>,
margin: LogicalSides<Length>, margin: LogicalSides<Au>,
clearance: Option<Length>, clearance: Option<Au>,
block_margins_collapsed_with_children: CollapsedBlockMargins, block_margins_collapsed_with_children: CollapsedBlockMargins,
) -> BoxFragment { ) -> BoxFragment {
let position = style.get_box().position; let position = style.get_box().position;
@ -128,10 +128,10 @@ impl BoxFragment {
style: ServoArc<ComputedValues>, style: ServoArc<ComputedValues>,
children: Vec<Fragment>, children: Vec<Fragment>,
content_rect: LogicalRect<Length>, content_rect: LogicalRect<Length>,
padding: LogicalSides<Length>, padding: LogicalSides<Au>,
border: LogicalSides<Length>, border: LogicalSides<Au>,
margin: LogicalSides<Length>, margin: LogicalSides<Au>,
clearance: Option<Length>, clearance: Option<Au>,
block_margins_collapsed_with_children: CollapsedBlockMargins, block_margins_collapsed_with_children: CollapsedBlockMargins,
overconstrained: PhysicalSize<bool>, overconstrained: PhysicalSize<bool>,
) -> BoxFragment { ) -> BoxFragment {
@ -154,8 +154,10 @@ impl BoxFragment {
let mut baselines = Baselines::default(); let mut baselines = Baselines::default();
if style.establishes_scroll_container() { if style.establishes_scroll_container() {
baselines.last = Some( baselines.last = Some(
(content_rect.size.block + padding.block_end + border.block_end + margin.block_end) Au::from(content_rect.size.block) +
.into(), padding.block_end +
border.block_end +
margin.block_end,
) )
} }
@ -221,11 +223,13 @@ impl BoxFragment {
} }
pub fn padding_rect(&self) -> LogicalRect<Length> { pub fn padding_rect(&self) -> LogicalRect<Length> {
self.content_rect.inflate(&self.padding) self.content_rect
.inflate(&self.padding.map(|t| (*t).into()))
} }
pub fn border_rect(&self) -> LogicalRect<Length> { pub fn border_rect(&self) -> LogicalRect<Length> {
self.padding_rect().inflate(&self.border) self.padding_rect()
.inflate(&self.border.map(|t| (*t).into()))
} }
pub fn print(&self, tree: &mut PrintTree) { pub fn print(&self, tree: &mut PrintTree) {

View file

@ -254,7 +254,7 @@ impl PositioningContext {
// Ignore the content rects position in its own containing block: // Ignore the content rects position in its own containing block:
start_corner: LogicalVec2::zero(), start_corner: LogicalVec2::zero(),
} }
.inflate(&new_fragment.padding); .inflate(&new_fragment.padding.map(|t| (*t).into()));
let containing_block = DefiniteContainingBlock { let containing_block = DefiniteContainingBlock {
size: padding_rect.size.into(), size: padding_rect.size.into(),
style: &new_fragment.style, style: &new_fragment.style,
@ -703,9 +703,9 @@ impl HoistedAbsolutelyPositionedBox {
absolutely_positioned_box.context.style().clone(), absolutely_positioned_box.context.style().clone(),
fragments, fragments,
content_rect.into(), content_rect.into(),
pbm.padding.into(), pbm.padding,
pbm.border.into(), pbm.border,
margin.into(), margin,
None, /* clearance */ None, /* clearance */
// We do not set the baseline offset, because absolutely positioned // We do not set the baseline offset, because absolutely positioned
// elements are not inflow. // elements are not inflow.

View file

@ -370,14 +370,14 @@ pub fn process_resolved_style_request<'dom>(
match longhand_id { match longhand_id {
LonghandId::Width => Some(content_rect.size.width), LonghandId::Width => Some(content_rect.size.width),
LonghandId::Height => Some(content_rect.size.height), LonghandId::Height => Some(content_rect.size.height),
LonghandId::MarginBottom => Some(margins.bottom), LonghandId::MarginBottom => Some(margins.bottom.into()),
LonghandId::MarginTop => Some(margins.top), LonghandId::MarginTop => Some(margins.top.into()),
LonghandId::MarginLeft => Some(margins.left), LonghandId::MarginLeft => Some(margins.left.into()),
LonghandId::MarginRight => Some(margins.right), LonghandId::MarginRight => Some(margins.right.into()),
LonghandId::PaddingBottom => Some(padding.bottom), LonghandId::PaddingBottom => Some(padding.bottom.into()),
LonghandId::PaddingTop => Some(padding.top), LonghandId::PaddingTop => Some(padding.top.into()),
LonghandId::PaddingLeft => Some(padding.left), LonghandId::PaddingLeft => Some(padding.left.into()),
LonghandId::PaddingRight => Some(padding.right), LonghandId::PaddingRight => Some(padding.right.into()),
_ => None, _ => None,
} }
.map(|value| value.to_css_string()) .map(|value| value.to_css_string())

View file

@ -2077,8 +2077,8 @@ impl TableSlotCell {
self.style.clone(), self.style.clone(),
vec![Fragment::Positioning(vertical_align_fragment)], vec![Fragment::Positioning(vertical_align_fragment)],
cell_content_rect, cell_content_rect,
layout.padding, layout.padding.into(),
layout.border, layout.border.into(),
LogicalSides::zero(), /* margin */ LogicalSides::zero(), /* margin */
None, /* clearance */ None, /* clearance */
CollapsedBlockMargins::zero(), CollapsedBlockMargins::zero(),

View file

@ -470,8 +470,8 @@ impl Drop for FloatPlacement {
impl PlacedFloat { impl PlacedFloat {
fn rect(&self) -> LogicalRect<Au> { fn rect(&self) -> LogicalRect<Au> {
LogicalRect { LogicalRect {
start_corner: self.origin.clone(), start_corner: self.origin,
size: self.info.size.clone(), size: self.info.size,
} }
} }
} }