Skip to content

Truly independent web browser

LibWeb: Carry an interrupting block's geometry and baselines on its line

Aliaksandr Kalenikeb2629c5715f
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index c9cba7d4b18..b540e2c191c 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -44,10 +44,10 @@ struct BlockLevelBoxOutcome {
/// The offset of the content box in the containing block, or the origin while the box is not placed.
content_offset: FfiCssPixelPoint,
is_placed: bool,
- content_block_size: CssPixels,
- border_box_bottom: CssPixels,
- border_box_block_size: CssPixels,
- margin_bottom: CssPixels,
+ /// The sizes and box sides of the box, in the frame of its containing block.
+ cells: used_values::RootInputCells,
+ /// The baselines of the contents of the box, relative to its content box.
+ content_baselines: DerivedBaselines,
collapses_through: bool,
is_invisible_for_line_clamp: bool,
can_break_inside: bool,
@@ -57,8 +57,25 @@ struct BlockLevelBoxOutcome {
}
impl BlockLevelBoxOutcome {
+ fn content_block_size(&self) -> CssPixels {
+ self.cells.content_block_size
+ }
+
+ fn border_box_bottom(&self) -> CssPixels {
+ self.cells.border_box_bottom(self.cells.uses_collapsing_borders_model)
+ }
+
+ fn border_box_block_size(&self) -> CssPixels {
+ self.cells
+ .border_box_block_size(self.cells.uses_collapsing_borders_model)
+ }
+
+ fn margin_bottom(&self) -> CssPixels {
+ self.cells.margin_bottom
+ }
+
fn border_box_block_end(&self) -> CssPixels {
- self.content_offset.y + self.content_block_size + self.border_box_bottom
+ self.content_offset.y + self.content_block_size() + self.border_box_bottom()
}
}
@@ -123,8 +140,8 @@ impl RootFlowChild {
Self {
node,
border_box_block_end: outcome.border_box_block_end(),
- border_box_block_size: outcome.border_box_block_size,
- margin_bottom: outcome.margin_bottom,
+ border_box_block_size: outcome.border_box_block_size(),
+ margin_bottom: outcome.margin_bottom(),
collapses_through: outcome.collapses_through,
is_invisible_for_line_clamp: outcome.is_invisible_for_line_clamp,
}
@@ -820,7 +837,7 @@ impl<'pass> BlockFormattingContext<'pass> {
{
summary.last_in_flow_child = Some(LastInFlowChild {
border_box_block_end: outcome.border_box_block_end(),
- margin_bottom: outcome.margin_bottom,
+ margin_bottom: outcome.margin_bottom(),
});
}
}
@@ -2085,14 +2102,11 @@ impl<'pass> BlockFormattingContext<'pass> {
containing_line_box_fragment,
)?;
let metrics = self.run.records.box_metrics_of(node);
- let collapsed = metrics.uses_collapsing_borders_model();
Some(BlockLevelBoxOutcome {
content_offset: self.run.records.content_offset_or_origin(node),
is_placed: self.run.records.is_placed(node),
- content_block_size: metrics.content_block_size(),
- border_box_bottom: metrics.border_box_bottom(collapsed),
- border_box_block_size: metrics.border_box_block_size(collapsed),
- margin_bottom: metrics.margin_bottom(),
+ cells: metrics.input_cells(),
+ content_baselines: metrics.content_baselines(),
collapses_through: laid_out.collapses_through,
is_invisible_for_line_clamp: self.run.records.is_invisible_for_line_clamp(node),
can_break_inside: self.box_can_break_inside(node, laid_out.has_unbreakable_lines),
@@ -3215,18 +3229,18 @@ impl<'pass> BlockFormattingContext<'pass> {
return FragmentationStep::Stop;
}
let break_before_is_avoided = self.break_between_is_avoided(state, child);
- let border_box_block_size = outcome.border_box_block_size;
+ let border_box_block_size = outcome.border_box_block_size();
if self.allows_unforced_breaks() {
let content_block_end = state.container_content_box_block_start_in_root
+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins(block_container)
+ outcome.content_offset.y
- + outcome.content_block_size;
+ + outcome.content_block_size();
let is_breakable = outcome.can_break_inside
&& !fragmentation::avoids_column_break_inside(self.style(child).break_inside());
if !is_breakable {
fragmentainer.note_unbreakable_block_size(border_box_block_size);
}
- let block_end = content_block_end + outcome.border_box_bottom;
+ let block_end = content_block_end + outcome.border_box_bottom();
if fragmentainer.exceeds(block_end) {
fragmentainer.note_space_shortage(block_end);
if state.fragmentainer_had_content_before_child
@@ -4172,8 +4186,9 @@ impl<'pass> BlockFormattingContext<'pass> {
if let Some(index) = index_of_legend_in_root_flow_children
&& let Some(legend_outcome) = legend_outcome.as_ref()
{
- self.root_flow_children.borrow_mut()[index].border_box_block_end =
- legend_content_block_offset + legend_outcome.content_block_size + legend_outcome.border_box_bottom;
+ self.root_flow_children.borrow_mut()[index].border_box_block_end = legend_content_block_offset
+ + legend_outcome.content_block_size()
+ + legend_outcome.border_box_bottom();
}
self.translate_floats_in_subtree(
legend,
@@ -4342,13 +4357,29 @@ impl<'pass> BlockFormattingContext<'pass> {
.get()
.unwrap_or_else(|| line_builder.current_block_offset());
self.block_offset_of_current_block_container.set(saved);
+ let outcome = outcome.expect("the flow holds a block-level box that interrupts its lines");
+ assert!(
+ outcome.is_placed,
+ "a block-level box is placed before its line takes it"
+ );
line_builder.append_block_level_box(
node,
line_index,
block_bottom,
self.margin_state.borrow().current_collapsed_margin(),
+ line_box::BlockLevelBoxOnLine {
+ content_offset: outcome.content_offset,
+ cells: outcome.cells,
+ baselines: formatting_context::baselines_of_block_level_box_on_line(
+ &self.run.callbacks,
+ node,
+ &outcome.cells,
+ outcome.content_baselines,
+ outcome.content_offset.y,
+ ),
+ },
);
- outcome.is_none_or(|outcome| outcome.collapses_through)
+ outcome.collapses_through
}
// Run-prelude inline sizing for a block-level root: commits the result of the parent's winning
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 42f75ad2f19..dad7b722eaf 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -821,58 +821,59 @@ pub(crate) fn store_derived_baselines(used: &UsedValues, baselines: DerivedBasel
}
}
-/// The baselines of the lines laid out in `box_`, relative to its content box.
-pub(crate) fn derive_baselines_from_lines(
- records: &RunRecords,
+/// The first, last and inline-alignment baselines of a line, relative to the content box of its block container.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) struct LineBaselines {
+ pub(crate) first: CssPixels,
+ pub(crate) last: CssPixels,
+ pub(crate) inline_alignment: CssPixels,
+}
+
+/// The baselines a line gives its block container where it holds `box_`, a block-level box with `cells` and
+/// `content_baselines` whose content box sits `content_block_offset` into the container's content box.
+pub(crate) fn baselines_of_block_level_box_on_line(
callbacks: &LayoutPass<'_>,
box_: Node,
-) -> DerivedBaselines {
- let own_used = records.used_values(box_);
- let own_line_data = own_used.line_data_ref();
- let mut lines = own_line_data.iter().flat_map(|data| data.lines());
- let first = lines
- .find(|line| !line.is_empty)
- .or_else(|| own_line_data.as_ref()?.lines().next());
+ cells: &used_values::RootInputCells,
+ content_baselines: DerivedBaselines,
+ content_block_offset: CssPixels,
+) -> LineBaselines {
+ let offset_from_margin_edge = content_block_offset - cells.margin_box_top(cells.uses_collapsing_borders_model);
+ let baseline = |baseline_set, purpose| {
+ offset_from_margin_edge
+ + box_baseline_for_purpose(callbacks, box_, cells, baseline_set, content_baselines, purpose)
+ };
+ LineBaselines {
+ first: baseline(BaselineSet::First, BaselinePurpose::Container),
+ last: baseline(BaselineSet::Last, BaselinePurpose::Container),
+ inline_alignment: baseline(BaselineSet::Last, BaselinePurpose::InlineAlignment),
+ }
+}
+
+/// The baselines of `lines`, relative to the content box of the block container that holds them.
+pub(crate) fn derive_baselines_from_lines(lines: &[line_box::LineBoxData]) -> DerivedBaselines {
+ let mut remaining_lines = lines.iter();
+ let first = remaining_lines.find(|line| !line.is_empty()).or_else(|| lines.first());
if let Some(first) = first {
- let last = lines.rfind(|line| !line.is_empty).unwrap_or(first);
- let baseline_for_line_box =
- |line: inline_content::LineRecord, baseline_set: BaselineSet, purpose: BaselinePurpose| -> CssPixels {
- if !line.has_block_level_box {
- return line.physical_vertical_end() - line.block_length + line.baseline;
- }
- assert_eq!(line.layout_fragment_count, 1);
- let fragment_node = line
- .first_fragment_node
- .expect("block-level line must have one fragment");
- let block_child = records.box_metrics_of(fragment_node);
- let child_offset_from_margin_edge = records.content_offset_or_origin(fragment_node).y
- - block_child.margin_box_top(block_child.uses_collapsing_borders_model());
- child_offset_from_margin_edge
- + box_baseline_for_purpose(
- callbacks,
- fragment_node,
- &block_child,
- baseline_set,
- block_child.content_baselines(),
- purpose,
- )
- };
+ let last = remaining_lines.rfind(|line| !line.is_empty()).unwrap_or(first);
+ let baselines_of_line = |line: &line_box::LineBoxData| {
+ if !line.has_block_level_box {
+ let baseline = line.physical_vertical_end() - line.block_length + line.baseline;
+ return LineBaselines {
+ first: baseline,
+ last: baseline,
+ inline_alignment: baseline,
+ };
+ }
+ assert_eq!(line.fragments.len(), 1);
+ line.block_level_box
+ .expect("a line interrupted for a block-level box keeps the box")
+ .baselines
+ };
return DerivedBaselines {
- first: Some(baseline_for_line_box(
- first,
- BaselineSet::First,
- BaselinePurpose::Container,
- )),
- last: Some(baseline_for_line_box(
- last,
- BaselineSet::Last,
- BaselinePurpose::Container,
- )),
- inline_alignment: Some(baseline_for_line_box(
- last,
- BaselineSet::Last,
- BaselinePurpose::InlineAlignment,
- )),
+ first: Some(baselines_of_line(first).first),
+ last: Some(baselines_of_line(last).last),
+ inline_alignment: Some(baselines_of_line(last).inline_alignment),
};
}
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index 2cde5a349d4..3bb5f228ffb 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -46,8 +46,6 @@ impl PartialEq for GlyphRunRecord {
pub struct LineRecord {
pub rect: used_values::FfiCssPixelRect,
pub fragment_count: u32,
- pub(crate) layout_fragment_count: usize,
- pub(crate) first_fragment_node: Option<NodeSlotId>,
pub(crate) is_empty: bool,
pub(crate) horizontal_extent: CssPixels,
pub(crate) reusable_atomic_prefix: bool,
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 78765029358..761585ae9aa 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -529,8 +529,11 @@ pub(crate) fn compute(
}
let interrupting_block = if interrupting {
- let block = context.box_metrics(fragment.layout_node);
- let collapsed = block.uses_collapsing_borders_model();
+ let block = line
+ .block_level_box
+ .expect("a line interrupted for a block-level box keeps the box")
+ .cells;
+ let collapsed = block.uses_collapsing_borders_model;
let (border_box_inline_low, border_box_block_low, border_box_inline_size, border_box_block_size) = (
block.border_box_left(collapsed),
block.border_box_top(collapsed),
@@ -1448,6 +1451,7 @@ impl<'context> InlineFormattingContext<'context> {
static_position_markers: Vec::new(),
inline_box_baselines: Vec::new(),
text_overflow_elision: None,
+ block_level_box: None,
};
fragment_start = fragment_end;
let line_item_start = item_index;
@@ -2301,13 +2305,10 @@ impl<'context> InlineFormattingContext<'context> {
.line_boxes
.last()
.map(line_box::LineBoxData::retained_metrics);
+ let baselines = formatting_context::derive_baselines_from_lines(&self.line_data.get_mut().line_boxes);
containing_used.set_building_line_data(std::mem::take(self.line_data.get_mut()));
block_formatting_context::ChildrenSummary {
- baselines: formatting_context::derive_baselines_from_lines(
- self.run.records,
- &self.callbacks,
- self.containing_block,
- ),
+ baselines,
fragmentainer_stop_token: self.fragmentainer_stop_token.take(),
lines_are_unbreakable: self.lines_are_unbreakable.get(),
last_line,
diff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rs
index 9202711a013..bf4971e523b 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs
@@ -50,6 +50,17 @@ pub(crate) struct TextOverflowElision {
pub(crate) ellipsis_fragment_inline_length: CssPixels,
}
+/// What a line its block container interrupted for a block-level box keeps of the box, as the layout of the box left it.
+#[derive(Clone, Copy, PartialEq)]
+pub(crate) struct BlockLevelBoxOnLine {
+ /// The offset of the content box of the box in its containing block.
+ pub(crate) content_offset: FfiCssPixelPoint,
+ /// The sizes and box sides of the box.
+ pub(crate) cells: used_values::RootInputCells,
+ /// The baselines the box gives the line.
+ pub(crate) baselines: formatting_context::LineBaselines,
+}
+
#[derive(Clone, PartialEq)]
pub(crate) struct LineBoxData {
pub(crate) start: super::fragmentation::LinePosition,
@@ -57,6 +68,8 @@ pub(crate) struct LineBoxData {
pub(crate) static_position_markers: Vec<StaticPositionMarker>,
pub(crate) inline_box_baselines: Vec<InlineBoxBaseline>,
pub(crate) text_overflow_elision: Option<TextOverflowElision>,
+ /// The block-level box the line holds, where the line is interrupted for one.
+ pub(crate) block_level_box: Option<BlockLevelBoxOnLine>,
pub(crate) record: inline_content::LineRecord,
}
@@ -81,6 +94,7 @@ impl LineBoxData {
static_position_markers: Vec::new(),
inline_box_baselines: Vec::new(),
text_overflow_elision: None,
+ block_level_box: None,
record: inline_content::LineRecord {
direction,
writing_mode,
@@ -92,8 +106,6 @@ impl LineBoxData {
pub(crate) fn retained_metrics(&self) -> inline_content::LineRecord {
inline_content::LineRecord {
- layout_fragment_count: self.fragments.len(),
- first_fragment_node: self.fragments.first().map(|fragment| fragment.layout_node),
is_empty: self.is_empty(),
horizontal_extent: inline_formatting_context::line_physical_horizontal_extent(self),
..self.record
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 7506b3088fa..0536b9ab672 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -525,19 +525,13 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
line_index: usize,
block_end: CssPixels,
block_end_margin: CssPixels,
+ block_level_box: line_box::BlockLevelBoxOnLine,
) {
- let block = self.context().box_metrics(node);
- let content_offset = self
- .context()
- .run
- .records
- .placement_of(node)
- .expect("a block-level box is placed before its line takes it")
- .content_offset_in_containing_block;
- let inline_offset = content_offset.x;
- let block_offset = content_offset.y;
- let inline_length = block.content_inline_size();
- let block_length = block.content_block_size();
+ let block = block_level_box.cells;
+ let inline_offset = block_level_box.content_offset.x;
+ let block_offset = block_level_box.content_offset.y;
+ let inline_length = block.content_inline_size;
+ let block_length = block.content_block_size;
let border_start = block.border_box_top(false);
let line_inline_length = block.margin_box_inline_size(false);
let ensured_line_index = self.ensure_last_line_index();
@@ -567,6 +561,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
line.block_end = block_end;
line.baseline = CssPixels::default();
line.has_block_level_box = true;
+ line.block_level_box = Some(block_level_box);
line.block_level_box_block_end_margin = block_end_margin;
line.has_break = true;
line.has_forced_break = true;