Truly independent web browser
LibWeb: Hand the lines of inline content back from their layout
Aliaksandr Kalenike36373b496d0
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex 4b84356f390..fac1ff517be 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -230,6 +230,8 @@ impl<'sized> ChildSizedHere<'sized> {#[derive(Default)]pub(crate) struct ChildrenSummary {pub(crate) baselines: DerivedBaselines,+ /// The lines laid out in the container, where an inline formatting context laid them out.+ pub(crate) lines: Option<used_values::LineData>,/// The break token of the container, where the fragmentainer stopped while its children were laid out.pub(crate) fragmentainer_stop_token: Option<std::rc::Rc<fragmentation::BlockBreakToken>>,/// Whether the lines of the container are kept together in one fragmentainer.@@ -555,6 +557,8 @@ pub(crate) struct BlockFormattingContext<'pass> {column_inline_items: OnceCell<std::rc::Rc<inline_level_iterator::InlineItemCache>>,/// The children of the run's root that its flow laid out, in tree order.root_flow_children: RefCell<Vec<RootFlowChild>>,+ /// The lines laid out in the run's root, which the run finishes once it has sized the root.+ root_lines: RefCell<used_values::LineDataState>,column_early_breaks: RefCell<ColumnEarlyBreaks>,multicol_automatic_content_block_size: Cell<Option<CssPixels>>,discard_block_start_margin_of_next_child: Cell<bool>,@@ -681,6 +685,7 @@ impl<'pass> BlockFormattingContext<'pass> {break_mode: Cell::new(BreakMode::AllBreaks),column_inline_items: OnceCell::new(),root_flow_children: RefCell::new(Vec::new()),+ root_lines: RefCell::new(used_values::LineDataState::Empty),column_early_breaks: RefCell::new(ColumnEarlyBreaks::default()),multicol_automatic_content_block_size: Cell::new(None),discard_block_start_margin_of_next_child: Cell::new(false),@@ -945,6 +950,12 @@ impl<'pass> BlockFormattingContext<'pass> {self.derived_baselines_of_root_box.get()}+ /// Takes the lines laid out in the run's root, which the run finishes. Call this after the last read of+ /// `automatic_content_block_size`, which reads the last of these lines.+ pub(crate) fn take_root_lines(&self) -> used_values::LineDataState {+ std::mem::take(&mut *self.root_lines.borrow_mut())+ }+/// Resolves the insets of `node`, an in-flow child of `block_container`, keeps them for the box and returns them.fn compute_inset(&self,@@ -3015,8 +3026,17 @@ impl<'pass> BlockFormattingContext<'pass> {let insets = self.compute_inset(run, node, block_container);+ let lines_laid_out_in_this_flow = summary_of_children_laid_out_in_this_flow+ .as_mut()+ .and_then(|summary| summary.lines.take());let placed_child = child_handed_back_by_its_run- .unwrap_or_else(|| formatting_context::PlacedChild::of_record(self.record_of(sized_child), &self.run))+ .unwrap_or_else(|| {+ formatting_context::PlacedChild::of_record(+ self.record_of(sized_child),+ lines_laid_out_in_this_flow,+ &self.run,+ )+ }).with_in_flow_box_insets(insets).with_fragment_facts(fragment_facts).with_offset_from_relatively_positioned_inline_ancestors(@@ -4026,9 +4046,9 @@ impl<'pass> BlockFormattingContext<'pass> {}self.margin_state.borrow_mut().reset();if !root_line_data_of_columns.is_empty() {- root_used.set_finished_line_data(inline_content::InlineContent::concatenate_translated(- &root_line_data_of_columns,- ));+ *self.root_lines.borrow_mut() = used_values::LineDataState::Finished(+ inline_content::InlineContent::concatenate_translated(&root_line_data_of_columns),+ );}let multicol_layout_data = std::sync::Arc::new(fragmentation::MulticolLayoutData { geometry, rows });self.run.update_fragment_payloads_of(self.run.box_, |payloads| {@@ -4547,7 +4567,7 @@ impl<'pass> BlockFormattingContext<'pass> {self.record_derived_baselines_of_root_box(baselines);return;}- let children_summary = if root_facts.is_table_wrapper() {+ let mut children_summary = if root_facts.is_table_wrapper() {self.layout_table_wrapper_children(run, root_input, available_space)} else if root_facts.children_are_inline() {self.layout_inline_children(@@ -4566,6 +4586,9 @@ impl<'pass> BlockFormattingContext<'pass> {available_space,)};+ if let Some(lines) = children_summary.lines.take() {+ *self.root_lines.borrow_mut() = used_values::LineDataState::Building(Box::new(lines));+ }let baselines = children_summary.baselines;self.record_derived_baselines_of_root_box(baselines);@@ -5114,7 +5137,7 @@ impl<'pass> BlockFormattingContext<'pass> {}let line_clamp_reached = self.line_clamp_reached();let block_size = automatic_block_size_for_bfc_root(- self.run.records,+ self.root_lines.borrow().last_line(),self.run.callbacks,self.run.box_,&self.root_flow_children.borrow(),@@ -5314,7 +5337,7 @@ pub(crate) fn margins_can_collapse_through_box_itself(// https://www.w3.org/TR/CSS22/visudet.html#root-heightpub(crate) fn automatic_block_size_for_bfc_root(- records: &RunRecords,+ root_last_line: Option<inline_content::LineRecord>,callbacks: LayoutPass<'_>,root: Node,flow_children: &[RootFlowChild],@@ -5332,9 +5355,7 @@ pub(crate) fn automatic_block_size_for_bfc_root(if facts.children_are_inline() {// If it only has inline-level children, the block size is the distance between// the top content edge and the bottom of the bottommost line box.- let root_used = records.used_values(root);- let line_data = root_used.line_data_ref();- if let Some(last_line) = line_data.as_ref().and_then(|data| data.last_line()) {+ if let Some(last_line) = root_last_line {bottom = Some(last_line.physical_vertical_end());// A trailing interrupting block's bottom margin cannot collapse out of a BFC root,// so it contributes to the root's automatic block size. The line box bottom excludes it.diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 76be334e26f..824d59744db 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -378,8 +378,12 @@ impl PlacedChild {})}- /// A box that `run` lays out in `record`, before its parent decided anything about it.- pub(crate) fn of_record(record: &UsedValues, run: &FormattingContextRun) -> Self {+ /// A box that `run` lays out in `record` with `lines`, before its parent decided anything about it.+ pub(crate) fn of_record(+ record: &UsedValues,+ lines: Option<used_values::LineData>,+ run: &FormattingContextRun,+ ) -> Self {let callbacks = &run.callbacks;Self::new(fragment_tree::FragmentGeometry {cells: used_values::RecordInFrame::new(record, callbacks.frame).cells(),@@ -387,7 +391,16 @@ impl PlacedChild {line_data: run.fragments.is_some()- .then(|| record.finish_line_data(callbacks))+ .then(|| {+ lines.map(|lines| {+ std::sync::Arc::new(inline_content::InlineContent::finish(+ lines,+ callbacks.arena(),+ record.content_inline_size.get(),+ record.frame.get().writing_mode(),+ ))+ })+ }).flatten(),})}@@ -1386,13 +1399,13 @@ impl<'pass> FormattingContextRun<'pass> {&self,result: ChildLayoutResult,root: Option<fragment_tree::UnplacedRootFragment>,+ root_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,inputs_never_observed: fc_run_cache::RunInputsTheStoredRunNeverObserved,parent_owns_root_input_cells: bool,) -> RunOutputs {- let record = self.root_record();let root_outcome = RunRootOutcome {- input_cells_left_by_run: (!parent_owns_root_input_cells).then(|| record.cells()),- line_data: record.record().finish_line_data(&self.callbacks),+ input_cells_left_by_run: (!parent_owns_root_input_cells).then(|| self.root_record().cells()),+ line_data: root_line_data,};RunOutputs {result,@@ -2375,6 +2388,7 @@ fn lay_out_run_root_and_contents<'pass>(};let mut implementation = None;+ let mut root_lines = used_values::LineDataState::Empty;let mut result = if !changes_frame&& run.fragments.is_some()&& matches!(@@ -2412,6 +2426,7 @@ fn lay_out_run_root_and_contents<'pass>(if NodeFacts::new(&body_run.callbacks, box_).display().is_math_inside() {math_formatting_context::layout_operator(body_run, &input, &mut result);}+ root_lines = context.take_root_lines();result}FormattingContextImplementation::Flex(context) => {@@ -2490,7 +2505,15 @@ fn lay_out_run_root_and_contents<'pass>(// NB: Intrinsic size overrides use the box's own writing mode, before its sizes are projected into the// parent's frame.if changes_frame {- result = result_in_parent_frame(run, body_run, result, parent_frame, own_frame);+ // NB: The lines are finished at the inline size the contents were laid out at, before the root's own box is+ // sized in its parent's frame.+ let record = records.used_values(box_);+ root_lines.finish(+ callbacks.arena(),+ record.content_inline_size.get(),+ record.frame.get().writing_mode(),+ );+ result = result_in_parent_frame(run, result, parent_frame, own_frame);}if containment_facts.has_preferred_aspect_ratio() {@@ -2551,6 +2574,12 @@ fn lay_out_run_root_and_contents<'pass>(result.content_block_size_for_aspect_ratio_minimum,);}+ let record = records.used_values(box_);+ let root_line_data = root_lines.finish(+ callbacks.arena(),+ record.content_inline_size.get(),+ record.frame.get().writing_mode(),+ );result.depends_on_percentage_block_size = run.sizing().resolve_percentage_block_size_dependency(run.box_);let item_run_observations_are_known = !run_is_handed_table_inline_layout_outside_its_key;let inputs_never_observed = fc_run_cache::RunInputsTheStoredRunNeverObserved {@@ -2578,6 +2607,7 @@ fn lay_out_run_root_and_contents<'pass>(return run.outputs(result,take_run_fragments(),+ root_line_data,inputs_never_observed,parent_owns_root_input_cells,);@@ -2596,6 +2626,7 @@ fn lay_out_run_root_and_contents<'pass>(return run.outputs(result,take_run_fragments(),+ root_line_data,inputs_never_observed,parent_owns_root_input_cells,);@@ -2606,6 +2637,7 @@ fn lay_out_run_root_and_contents<'pass>(run.outputs(result,take_run_fragments(),+ root_line_data,inputs_never_observed,parent_owns_root_input_cells,)@@ -2688,13 +2720,11 @@ fn body_input_in_own_frame(/// Maps what the layout of a run root's contents in its own frame found into its parent's frame.fn result_in_parent_frame(run: &FormattingContextRun,- body_run: &FormattingContextRun,mut result: ChildLayoutResult,parent_frame: geometry::Frame,own_frame: geometry::Frame,) -> ChildLayoutResult {let used = run.records.used_values(run.box_);- used.finish_line_data(&body_run.callbacks);if parent_frame.is_orthogonal_to(own_frame) {// https://drafts.csswg.org/css-writing-modes-4/#orthogonal-shrink-to-fit// Note that this section requires that when a child box auto-sized in its block axis establishes an@@ -2889,7 +2919,11 @@ pub(crate) fn layout_inside_child_with_input_cells(.filter(|fragment| {run.fragments.is_some()&& fragment.is_plain_box()- && record_of_this_layout.shows_box_of_fragment(fragment)+ && fragment_tree::FragmentGeometry {+ cells: used_values::RootInputCells::capture(&record_of_this_layout),+ line_data: None,+ }+ .shows_box_of_fragment(fragment)&& !facts.has_anchor_names()}).map(|fragment| fragment_tree::ChildFragmentArrival::CommittedFragment {diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 3f91c1524c4..dd9de10abfa 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -975,13 +975,13 @@ impl FragmentainerLineLimit {pub(crate) struct InlineFormattingContext<'context> {pub(crate) run: &'context FormattingContextRun<'context>,pub(crate) containing_block: Node,- /// The containing block, with the record it lays out its lines in.+ /// The containing block, with the record that holds the metrics its lines are laid out against.block_container: block_formatting_context::BlockContainer<'context>,pub(crate) input: LayoutInput,containing_block_position_in_bfc_root: FfiCssPixelPoint,pub(crate) callbacks: LayoutPass<'context>,pub(crate) parent: &'context block_formatting_context::BlockFormattingContext<'context>,- /// The lines this context lays out, which its containing block's record keeps once they are laid out.+ /// The lines this context lays out, which it hands back once they are laid out.line_data: RefCell<used_values::LineData>,pub(crate) fragmented_inlines_in_pre_order: Vec<Node>,pub(crate) current_line_start: Cell<fragmentation::LinePosition>,@@ -1022,7 +1022,6 @@ impl<'context> InlineFormattingContext<'context> {callbacks: LayoutPass<'context>,parent: &'context block_formatting_context::BlockFormattingContext<'context>,) -> Self {- block_container.used.ensure_line_data();Self {run,containing_block: block_container.node,@@ -2421,8 +2420,6 @@ impl<'context> InlineFormattingContext<'context> {pub(crate) fn run(&mut self) -> block_formatting_context::ChildrenSummary {assert!(self.facts(self.containing_block).children_are_inline());let run = self.run;- let containing_used = self.block_container.used;- *self.line_data.get_mut() = containing_used.take_building_line_data();let iterator = match run.fragmentainer.as_deref() {Some(fragmentainer) => {inline_level_iterator::InlineLevelIterator::new_sharing_items(self, &fragmentainer.inline_items)@@ -2443,9 +2440,9 @@ impl<'context> InlineFormattingContext<'context> {.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,+ lines: Some(std::mem::take(self.line_data.get_mut())),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/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex a4e29844133..6f952f05849 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -301,7 +301,7 @@ pub(crate) struct LaidOutChildBox {impl LaidOutChildBox {/// A child as the run of its parent left it in `record`, where that run sizes the child itself instead of handing- /// it to a run of its own. The record keeps any lines the parent's run laid out in it.+ /// it to a run of its own.pub(crate) fn sized_in_parent_run(record: &UsedValues) -> Self {Self {cells: used_values::RootInputCells::capture(record),diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex e03a05a25bf..3edf607cc12 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1879,7 +1879,7 @@ impl<'pass> SvgFormattingContext<'pass> {/// Places `node`, a box this context lays out in `record`, whose metrics are final once the box is placed, and/// returns what the parent of the box reads of it.fn place_child(&self, node: Node, record: &UsedValues, x: CssPixels, y: CssPixels) -> LaidOutSvgElement {- let placed_child = formatting_context::PlacedChild::of_record(record, &self.run);+ let placed_child = formatting_context::PlacedChild::of_record(record, None, &self.run);self.place_laid_out_child(node, placed_child, x, y, None);record.seal_own_metrics();LaidOutSvgElement {@@ -2245,7 +2245,7 @@ impl<'pass> SvgFormattingContext<'pass> {used.has_definite_block_size.set(true);self.place_laid_out_child(shape,- formatting_context::PlacedChild::of_record(used, &self.run),+ formatting_context::PlacedChild::of_record(used, None, &self.run),committed_offset.x,committed_offset.y,Some(fragment_tree::ChildFragmentArrival::CommittedFragment {diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rsindex 4b28ed0c2a8..715a7593a23 100644--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs@@ -1271,7 +1271,7 @@ impl<'pass> TableFormattingContext<'pass> {formatting_context::place_child(&self.run,node,- formatting_context::PlacedChild::of_record(record, &self.run).with_fragment_facts(fragment_facts),+ formatting_context::PlacedChild::of_record(record, None, &self.run).with_fragment_facts(fragment_facts),content_offset,None,None,diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex bb195f2e33d..3bacca8497a 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -113,23 +113,27 @@ pub(crate) enum LineDataState {Finished(std::sync::Arc<inline_content::InlineContent>),}-impl std::fmt::Debug for LineDataState {- fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- formatter.write_str(match self {- Self::Empty => "Empty",- Self::Building(_) => "Building",- Self::Finished(_) => "Finished",- })- }-}-impl LineDataState {- pub(crate) fn building_mut(&mut self) -> &mut LineData {- match self {- Self::Building(data) => data,- Self::Empty => panic!("line building mutated line data before it began"),- Self::Finished(_) => panic!("line building mutated finalized inline content"),- }+ /// The lines as inline content laid out at `content_inline_size` in `writing_mode`. Finishes lines still being+ /// built and keeps the result, so later calls return the same content.+ pub(crate) fn finish(+ &mut self,+ arena: &LayoutNodeArena,+ content_inline_size: CssPixels,+ writing_mode: u8,+ ) -> Option<std::sync::Arc<inline_content::InlineContent>> {+ let content = match self {+ Self::Empty => return None,+ Self::Finished(content) => return Some(content.clone()),+ Self::Building(data) => std::sync::Arc::new(inline_content::InlineContent::finish(+ std::mem::take(data.as_mut()),+ arena,+ content_inline_size,+ writing_mode,+ )),+ };+ *self = Self::Finished(content.clone());+ Some(content)}pub(crate) fn lines(&self) -> impl DoubleEndedIterator<Item = inline_content::LineRecord> + '_ {@@ -202,7 +206,6 @@ pub(crate) struct UsedValues {pub last_baseline: Cell<CssPixels>,pub inline_alignment_baseline: Cell<Option<CssPixels>>,- pub(crate) line_data: RefCell<LineDataState>,/// The frame the box's sizes, size constraints and box sides are held in.pub(crate) frame: Cell<geometry::Frame>,}@@ -235,59 +238,12 @@ impl Default for UsedValues {has_last_baseline: Cell::new(false),last_baseline: Cell::new(zero),inline_alignment_baseline: Cell::new(None),- line_data: RefCell::new(LineDataState::Empty),frame: Cell::new(geometry::Frame::PHYSICAL),}}}impl UsedValues {- pub(crate) fn line_data_ref(&self) -> Option<Ref<'_, LineDataState>> {- let state = self.line_data.borrow();- (!matches!(*state, LineDataState::Empty)).then_some(state)- }-- pub(crate) fn ensure_line_data(&self) {- let mut state = self.line_data.borrow_mut();- if matches!(*state, LineDataState::Empty) {- *state = LineDataState::Building(Box::default());- }- }-- /// Takes the lines built in this record so far, for an inline formatting context to build on.- pub(crate) fn take_building_line_data(&self) -> LineData {- let mut data = RefMut::map(self.line_data.borrow_mut(), LineDataState::building_mut);- std::mem::take(&mut *data)- }-- /// Keeps the lines an inline formatting context built in this record.- pub(crate) fn set_building_line_data(&self, data: LineData) {- *RefMut::map(self.line_data.borrow_mut(), LineDataState::building_mut) = data;- }-- pub(crate) fn set_finished_line_data(&self, content: std::sync::Arc<inline_content::InlineContent>) {- *self.line_data.borrow_mut() = LineDataState::Finished(content);- }-- pub(crate) fn finish_line_data(- &self,- callbacks: &LayoutPass<'_>,- ) -> Option<std::sync::Arc<inline_content::InlineContent>> {- let mut state = self.line_data.borrow_mut();- let content = match &mut *state {- LineDataState::Empty => return None,- LineDataState::Finished(content) => return Some(content.clone()),- LineDataState::Building(data) => std::sync::Arc::new(inline_content::InlineContent::finish(- std::mem::take(data.as_mut()),- callbacks.arena(),- self.content_inline_size.get(),- self.frame.get().writing_mode(),- )),- };- *state = LineDataState::Finished(content.clone());- Some(content)- }-pub(crate) fn content_baselines_from_cells(&self) -> DerivedBaselines {DerivedBaselines {inline_alignment: self.inline_alignment_baseline.get(),@@ -509,31 +465,6 @@ impl UsedValues {pub(crate) fn set_box_metrics_from_fragment(&self, fragment: &fragment_tree::Fragment) {RecordInFrame::in_its_own_frame(self).set_box_metrics_from_fragment(fragment);}-- /// Whether `fragment` shows this record's box as it stands: its metrics, and the very line data it holds.- pub(crate) fn shows_box_of_fragment(&self, fragment: &fragment_tree::Fragment) -> bool {- let same_line_data = match (&fragment.line_data, &*self.line_data.borrow()) {- (None, LineDataState::Empty) => true,- (Some(left), LineDataState::Finished(right)) => std::sync::Arc::ptr_eq(left, right),- _ => false,- };- same_line_data- && fragment.content_inline_size == self.content_inline_size.get()- && fragment.content_block_size == self.content_block_size.get()- && fragment.margin_left == self.margin_left.get()- && fragment.margin_right == self.margin_right.get()- && fragment.margin_top == self.margin_top.get()- && fragment.margin_bottom == self.margin_bottom.get()- && fragment.border_left == self.border_left.get()- && fragment.border_right == self.border_right.get()- && fragment.border_top == self.border_top.get()- && fragment.border_bottom == self.border_bottom.get()- && fragment.padding_left == self.padding_left.get()- && fragment.padding_right == self.padding_right.get()- && fragment.padding_top == self.padding_top.get()- && fragment.padding_bottom == self.padding_bottom.get()- && fragment.uses_collapsing_borders_model == self.uses_collapsing_borders_model.get()- }}/// The sizes, size constraints and box sides of a box, with what derives from them, whether a run holds them in the box's@@ -729,12 +660,6 @@ impl<'record> RecordInFrame<'record> {}}- /// The record, for what it holds besides the metrics this view projects.- #[inline]- pub(crate) fn record(self) -> &'record UsedValues {- self.record- }-/// Writes the record's metrics, which `write` sees and changes as the reader's frame has them.pub(crate) fn update(self, write: impl FnOnce(&mut RootInputCells)) {let mut cells = self.cells();