layout: Make geom.rs logical geoemetry types more ergonomic (#32633)

Make using the logical geometry types more ergonomic by having them all
implement `Copy` (at most 4 64-bit numbers), similar to what `euclid`
does. In addition add an implementation of `Neg` for `LogicalVec` and
`LogicalSides` as it will be used in upcoming table implementation code.
This commit is contained in:
Martin Robinson 2024-06-28 10:20:50 +02:00 committed by GitHub
parent e9cf4d4971
commit adc0fc984d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 80 additions and 102 deletions

View file

@ -617,7 +617,7 @@ impl<'a> BuilderForBoxFragment<'a> {
let radii = inner_radii(
self.border_radius,
(&self.fragment.border + &self.fragment.padding)
(self.fragment.border + self.fragment.padding)
.to_physical(self.fragment.style.writing_mode)
.to_webrender(),
);

View file

@ -266,6 +266,6 @@ where
inline: flow_relative_base_rect_size.inline - flow_relative_offsets.inline_end,
block: flow_relative_base_rect_size.block - flow_relative_offsets.block_end,
};
let size = &end_corner_position - &start_corner;
let size = end_corner_position - start_corner;
LogicalRect { start_corner, size }
}

View file

@ -428,7 +428,7 @@ impl FlexContainer {
},
};
for (fragment, _) in &mut line.item_fragments {
fragment.content_rect.start_corner += &flow_relative_line_position
fragment.content_rect.start_corner += flow_relative_line_position
}
line.item_fragments
});

View file

@ -897,7 +897,7 @@ impl FloatBox {
// or non-replaced.
let pbm = style.padding_border_margin(containing_block);
let margin = pbm.margin.auto_is(Au::zero);
let pbm_sums = &(&pbm.padding + &pbm.border) + &margin.clone();
let pbm_sums = pbm.padding + pbm.border + margin;
let (content_size, children);
match self.contents {
@ -1208,10 +1208,10 @@ impl SequentialLayoutState {
block_start_of_containing_block_in_bfc + block_offset_from_containing_block_top,
);
let pbm_sums = &(&box_fragment.padding + &box_fragment.border) + &box_fragment.margin;
let content_rect = box_fragment.content_rect.clone();
let pbm_sums = box_fragment.padding + box_fragment.border + box_fragment.margin;
let content_rect = &box_fragment.content_rect;
let margin_box_start_corner = self.floats.add_float(&PlacementInfo {
size: &content_rect.size + &pbm_sums.sum(),
size: content_rect.size + pbm_sums.sum(),
side: FloatSide::from_style(&box_fragment.style).expect("Float box wasn't floated!"),
clear: box_fragment.style.get_box().clear,
});
@ -1219,7 +1219,7 @@ impl SequentialLayoutState {
// 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.
let new_position_in_bfc =
&(&margin_box_start_corner + &pbm_sums.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.
let new_position_in_containing_block = LogicalVec2 {

View file

@ -223,10 +223,10 @@ impl TextRunLineItem {
// The block start of the TextRun is often zero (meaning it has the same font metrics as the
// inline box's strut), but for children of the inline formatting context root or for
// fallback fonts that use baseline relatve alignment, it might be different.
let start_corner = &LogicalVec2 {
let start_corner = LogicalVec2 {
inline: state.inline_position,
block: (state.baseline_offset - self.font_metrics.ascent).into(),
} - &state.parent_offset;
} - state.parent_offset;
let rect = LogicalRect {
start_corner,
@ -283,8 +283,8 @@ impl InlineBoxLineItem {
state: &mut LineItemLayoutState,
) -> Option<BoxFragment> {
let style = self.style.clone();
let mut padding = self.pbm.padding.clone();
let mut border = self.pbm.border.clone();
let mut padding = self.pbm.padding;
let mut border = self.pbm.border;
let mut margin = self.pbm.margin.auto_is(Au::zero);
if !self.is_first_fragment {
@ -297,7 +297,7 @@ impl InlineBoxLineItem {
border.inline_end = Au::zero();
margin.inline_end = Au::zero();
}
let pbm_sums = &(&padding + &border) + &margin;
let pbm_sums = padding + border + margin;
state.inline_position += pbm_sums.inline_start.into();
let space_above_baseline = self.calculate_space_above_baseline();
@ -335,7 +335,7 @@ impl InlineBoxLineItem {
border.inline_end = Au::zero();
margin.inline_end = Au::zero();
}
let pbm_sums = &(&padding + &border) + &margin.clone();
let pbm_sums = padding + border + margin;
// If the inline box didn't have any content at all, don't add a Fragment for it.
let box_has_padding_border_or_margin = pbm_sums.inline_sum() > Au::zero();
@ -361,13 +361,13 @@ impl InlineBoxLineItem {
};
// Make `content_rect` relative to the parent Fragment.
content_rect.start_corner = &content_rect.start_corner - &state.parent_offset;
content_rect.start_corner -= state.parent_offset;
// Relative adjustment should not affect the rest of line layout, so we can
// do it right before creating the Fragment.
if style.clone_position().is_relative() {
content_rect.start_corner +=
&relative_adjustement(&style, state.ifc_containing_block).into();
relative_adjustement(&style, state.ifc_containing_block).into();
}
let mut fragment = BoxFragment::new(
@ -466,12 +466,11 @@ impl AtomicLineItem {
self.calculate_block_start(state.line_metrics).into();
// Make the final result relative to the parent box.
self.fragment.content_rect.start_corner =
&self.fragment.content_rect.start_corner - &state.parent_offset.into();
self.fragment.content_rect.start_corner -= state.parent_offset.into();
if self.fragment.style.clone_position().is_relative() {
self.fragment.content_rect.start_corner +=
&relative_adjustement(&self.fragment.style, state.ifc_containing_block);
relative_adjustement(&self.fragment.style, state.ifc_containing_block);
}
state.inline_position += self.size.inline.into();
@ -571,8 +570,7 @@ impl FloatLineItem {
inline: state.parent_offset.inline,
block: state.line_metrics.block_offset + state.parent_offset.block,
};
self.fragment.content_rect.start_corner =
&self.fragment.content_rect.start_corner - &distance_from_parent_to_ifc.into();
self.fragment.content_rect.start_corner -= distance_from_parent_to_ifc.into();
self.fragment
}
}

View file

@ -1206,7 +1206,7 @@ impl<'a, 'b> InlineFormattingContextState<'a, 'b> {
);
let mut placement_rect = placement.place();
placement_rect.start_corner = &placement_rect.start_corner - &ifc_offset_in_float_container;
placement_rect.start_corner -= ifc_offset_in_float_container;
placement_rect.into()
}
@ -2026,7 +2026,7 @@ impl IndependentFormattingContext {
let style = self.style();
let pbm = style.padding_border_margin(ifc.containing_block);
let margin = pbm.margin.auto_is(Au::zero);
let pbm_sums = &(&pbm.padding + &pbm.border) + &margin.clone();
let pbm_sums = pbm.padding + pbm.border + margin;
let mut child_positioning_context = None;
// We need to know the inline size of the atomic before deciding whether to do the line break.
@ -2156,7 +2156,7 @@ impl IndependentFormattingContext {
ifc.process_soft_wrap_opportunity();
}
let size = &pbm_sums.sum() + &fragment.content_rect.size;
let size = pbm_sums.sum() + fragment.content_rect.size;
let baseline_offset = self
.pick_baseline(&fragment.baselines)
.map(|baseline| pbm_sums.block_start + baseline)
@ -2171,7 +2171,7 @@ impl IndependentFormattingContext {
);
ifc.push_line_item_to_unbreakable_segment(LineItem::Atomic(AtomicLineItem {
fragment,
size: size.into(),
size,
positioning_context: child_positioning_context,
baseline_offset_in_parent,
baseline_offset_in_item: baseline_offset,
@ -2402,7 +2402,7 @@ impl<'a> ContentSizesComputation<'a> {
.percentages_relative_to(zero)
.auto_is(Length::zero);
let pbm = &(&margin + &padding) + &border;
let pbm = margin + padding + border;
if inline_box.is_first_fragment {
self.add_length(pbm.inline_start);
}

View file

@ -1086,7 +1086,7 @@ impl NonReplacedFormattingContext {
&collapsed_margin_block_start,
containing_block,
&pbm,
&content_size + &pbm.padding_border_sums.into(),
content_size + pbm.padding_border_sums.into(),
&self.style,
);
} else {
@ -1102,12 +1102,10 @@ impl NonReplacedFormattingContext {
});
// Create a PlacementAmongFloats using the minimum size in all dimensions as the object size.
let minimum_size_of_block = &LogicalVec2 {
inline: min_box_size.inline,
block: block_size.auto_is(|| min_box_size.block),
}
.into() +
&pbm.padding_border_sums;
let minimum_size_of_block = LogicalVec2 {
inline: min_box_size.inline.into(),
block: block_size.auto_is(|| min_box_size.block).into(),
} + pbm.padding_border_sums;
let mut placement = PlacementAmongFloats::new(
&sequential_layout_state.floats,
ceiling,
@ -1291,7 +1289,7 @@ fn layout_in_flow_replaced_block_level(
// than defined by section 10.3.3. CSS 2 does not define when a UA may put said
// element next to the float or by how much said element may become narrower."
let collapsed_margin_block_start = CollapsedMargin::new(margin_block_start);
let size = &content_size + &pbm.padding_border_sums.clone();
let size = content_size + pbm.padding_border_sums;
(
clearance,
(margin_inline_start, margin_inline_end),

View file

@ -4,7 +4,7 @@
use std::convert::From;
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};
use app_units::Au;
use serde::Serialize;
@ -32,13 +32,13 @@ pub struct LogicalVec2<T> {
pub block: T,
}
#[derive(Clone, Serialize)]
#[derive(Clone, Copy, Serialize)]
pub struct LogicalRect<T> {
pub start_corner: LogicalVec2<T>,
pub size: LogicalVec2<T>,
}
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Copy, Debug, Serialize)]
pub struct LogicalSides<T> {
pub inline_start: T,
pub inline_end: T,
@ -79,13 +79,9 @@ impl<T: Clone> LogicalVec2<T> {
}
}
impl<T> Add<&'_ LogicalVec2<T>> for &'_ LogicalVec2<T>
where
T: Add<Output = T> + Copy,
{
impl<T: Add<Output = T> + Copy> Add<LogicalVec2<T>> for LogicalVec2<T> {
type Output = LogicalVec2<T>;
fn add(self, other: &'_ LogicalVec2<T>) -> Self::Output {
fn add(self, other: Self) -> Self::Output {
LogicalVec2 {
inline: self.inline + other.inline,
block: self.block + other.block,
@ -93,13 +89,9 @@ where
}
}
impl<T> Sub<&'_ LogicalVec2<T>> for &'_ LogicalVec2<T>
where
T: Sub<Output = T> + Copy,
{
impl<T: Sub<Output = T> + Copy> Sub<LogicalVec2<T>> for LogicalVec2<T> {
type Output = LogicalVec2<T>;
fn sub(self, other: &'_ LogicalVec2<T>) -> Self::Output {
fn sub(self, other: Self) -> Self::Output {
LogicalVec2 {
inline: self.inline - other.inline,
block: self.block - other.block,
@ -107,41 +99,27 @@ where
}
}
impl<T> AddAssign<&'_ LogicalVec2<T>> for LogicalVec2<T>
where
T: AddAssign<T> + Copy,
{
fn add_assign(&mut self, other: &'_ LogicalVec2<T>) {
impl<T: AddAssign<T> + Copy> AddAssign<LogicalVec2<T>> for LogicalVec2<T> {
fn add_assign(&mut self, other: LogicalVec2<T>) {
self.inline += other.inline;
self.block += other.block;
}
}
impl<T> AddAssign<LogicalVec2<T>> for LogicalVec2<T>
where
T: AddAssign<T> + Copy,
{
fn add_assign(&mut self, other: LogicalVec2<T>) {
self.add_assign(&other);
}
}
impl<T> SubAssign<&'_ LogicalVec2<T>> for LogicalVec2<T>
where
T: SubAssign<T> + Copy,
{
fn sub_assign(&mut self, other: &'_ LogicalVec2<T>) {
impl<T: SubAssign<T> + Copy> SubAssign<LogicalVec2<T>> for LogicalVec2<T> {
fn sub_assign(&mut self, other: LogicalVec2<T>) {
self.inline -= other.inline;
self.block -= other.block;
}
}
impl<T> SubAssign<LogicalVec2<T>> for LogicalVec2<T>
where
T: SubAssign<T> + Copy,
{
fn sub_assign(&mut self, other: LogicalVec2<T>) {
self.sub_assign(&other);
impl<T: Neg<Output = T> + Copy> Neg for LogicalVec2<T> {
type Output = LogicalVec2<T>;
fn neg(self) -> Self::Output {
Self {
inline: -self.inline,
block: -self.block,
}
}
}
@ -352,10 +330,7 @@ impl<T> LogicalSides<T> {
}
}
impl<T> LogicalSides<T>
where
T: Copy,
{
impl<T: Copy> LogicalSides<T> {
pub fn start_offset(&self) -> LogicalVec2<T> {
LogicalVec2 {
inline: self.inline_start,
@ -382,13 +357,10 @@ impl<T: Clone> LogicalSides<AutoOr<T>> {
}
}
impl<T> Add<&'_ LogicalSides<T>> for &'_ LogicalSides<T>
where
T: Add<Output = T> + Copy,
{
impl<T: Add<Output = T> + Copy> Add<LogicalSides<T>> for LogicalSides<T> {
type Output = LogicalSides<T>;
fn add(self, other: &'_ LogicalSides<T>) -> Self::Output {
fn add(self, other: Self) -> Self::Output {
LogicalSides {
inline_start: self.inline_start + other.inline_start,
inline_end: self.inline_end + other.inline_end,
@ -398,6 +370,18 @@ where
}
}
impl<T: Neg<Output = T> + Copy> Neg for LogicalSides<T> {
type Output = LogicalSides<T>;
fn neg(self) -> Self::Output {
Self {
inline_start: -self.inline_start,
inline_end: -self.inline_end,
block_start: -self.block_start,
block_end: -self.block_end,
}
}
}
impl<T: Zero> LogicalSides<T> {
pub(crate) fn zero() -> LogicalSides<T> {
LogicalSides {

View file

@ -240,8 +240,7 @@ impl PositioningContext {
self.append(new_context);
if style.clone_position() == Position::Relative {
new_fragment.content_rect.start_corner +=
&relative_adjustement(style, containing_block);
new_fragment.content_rect.start_corner += relative_adjustement(style, containing_block);
}
new_fragment
@ -690,7 +689,7 @@ impl HoistedAbsolutelyPositionedBox {
block_end: block_axis.margin_end,
};
let pb = &pbm.padding + &pbm.border;
let pb = pbm.padding + pbm.border;
let inline_start = match inline_axis.anchor {
Anchor::Start(start) => start + pb.inline_start + margin.inline_start,
Anchor::End(end) => {

View file

@ -1079,8 +1079,8 @@ impl<'a> TableLayout<'a> {
Some(CellLayout {
layout,
padding: padding.clone(),
border: border.clone(),
padding,
border,
positioning_context,
})
})
@ -1666,8 +1666,7 @@ impl<'a> TableLayout<'a> {
cell.rowspan,
cell.colspan,
);
row_relative_cell_rect.start_corner =
&row_relative_cell_rect.start_corner - &row_rect.start_corner;
row_relative_cell_rect.start_corner -= row_rect.start_corner;
let mut fragment = cell.create_fragment(
layout,
row_relative_cell_rect,
@ -1710,7 +1709,7 @@ impl<'a> TableLayout<'a> {
})
}
if let Some(row) = row {
let mut rect = row_rect.clone();
let mut rect = *row_rect;
rect.start_corner = LogicalVec2::zero();
fragment.add_extra_background(ExtraBackground {
style: row.style.clone(),
@ -1989,12 +1988,12 @@ impl TableAndTrackDimensions {
inline: column_dimensions.first().map_or_else(Au::zero, |v| v.0),
block: row_dimensions.first().map_or_else(Au::zero, |v| v.0),
};
let table_size = &LogicalVec2 {
let table_size = LogicalVec2 {
inline: column_dimensions
.last()
.map_or(fallback_inline_size, |v| v.1),
block: row_dimensions.last().map_or(fallback_block_size, |v| v.1),
} - &table_start_corner;
} - table_start_corner;
let table_cells_rect = LogicalRect {
start_corner: table_start_corner,
size: table_size,
@ -2017,7 +2016,7 @@ impl TableAndTrackDimensions {
}
fn get_row_rect(&self, row_index: usize) -> LogicalRect<Au> {
let mut row_rect = self.table_cells_rect.clone();
let mut row_rect = self.table_cells_rect;
let row_dimensions = self.row_dimensions[row_index];
row_rect.start_corner.block = row_dimensions.0;
row_rect.size.block = row_dimensions.1 - row_dimensions.0;
@ -2025,7 +2024,7 @@ impl TableAndTrackDimensions {
}
fn get_column_rect(&self, column_index: usize) -> LogicalRect<Au> {
let mut row_rect = self.table_cells_rect.clone();
let mut row_rect = self.table_cells_rect;
let column_dimensions = self.column_dimensions[column_index];
row_rect.start_corner.inline = column_dimensions.0;
row_rect.size.inline = column_dimensions.1 - column_dimensions.0;
@ -2037,7 +2036,7 @@ impl TableAndTrackDimensions {
return LogicalRect::zero();
}
let mut row_group_rect = self.table_cells_rect.clone();
let mut row_group_rect = self.table_cells_rect;
let block_start = self.row_dimensions[row_group.track_range.start].0;
let block_end = self.row_dimensions[row_group.track_range.end - 1].1;
row_group_rect.start_corner.block = block_start;
@ -2050,7 +2049,7 @@ impl TableAndTrackDimensions {
return LogicalRect::zero();
}
let mut column_group_rect = self.table_cells_rect.clone();
let mut column_group_rect = self.table_cells_rect;
let inline_start = self.column_dimensions[column_group.track_range.start].0;
let inline_end = self.column_dimensions[column_group.track_range.end - 1].1;
column_group_rect.start_corner.inline = inline_start;
@ -2068,10 +2067,10 @@ impl TableAndTrackDimensions {
inline: self.column_dimensions[coordinates.x].0,
block: self.row_dimensions[coordinates.y].0,
};
let size = &LogicalVec2 {
let size = LogicalVec2 {
inline: self.column_dimensions[coordinates.x + colspan - 1].1,
block: self.row_dimensions[coordinates.y + rowspan - 1].1,
} - &start_corner;
} - start_corner;
LogicalRect { start_corner, size }
}
}
@ -2213,7 +2212,7 @@ impl TableSlotCell {
// This must be scoped to this function because it conflicts with euclid's Zero.
use style::Zero as StyleZero;
let cell_content_rect = cell_rect.deflate(&(&layout.padding + &layout.border));
let cell_content_rect = cell_rect.deflate(&(layout.padding + layout.border));
let content_block_size = layout.layout.content_block_size;
let vertical_align_offset = match self.effective_vertical_align() {
VerticalAlignKeyword::Top => Au::zero(),
@ -2237,7 +2236,7 @@ impl TableSlotCell {
}
// Create an `AnonymousFragment` to move the cell contents to the cell baseline.
let mut vertical_align_fragment_rect = cell_content_rect.clone();
let mut vertical_align_fragment_rect = cell_content_rect;
vertical_align_fragment_rect.start_corner = LogicalVec2 {
inline: Au::zero(),
block: vertical_align_offset,
@ -2317,7 +2316,7 @@ fn get_outer_sizes_from_style(
) -> (LogicalVec2<Au>, LogicalVec2<Au>, LogicalVec2<Au>) {
let box_sizing = style.get_position().box_sizing;
let outer_size = |size: LogicalVec2<Au>| match box_sizing {
BoxSizing::ContentBox => &size + padding_border_sums,
BoxSizing::ContentBox => size + *padding_border_sums,
BoxSizing::BorderBox => LogicalVec2 {
inline: size.inline.max(padding_border_sums.inline),
block: size.block.max(padding_border_sums.block),