Truly independent web browser
LibWeb: Derive a flex container's baselines from its items
Aliaksandr Kalenik709a99e7a57b
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rsindex 3fc19fa576e..4f0a1012745 100644--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs@@ -74,6 +74,9 @@ struct FlexItem<'pass> {is_min_violation: bool,is_max_violation: bool,content_baselines: DerivedBaselines,+ /// For an item that establishes an orthogonal flow, its baselines along the horizontal axis of the container's+ /// frame, as its run handed them back.+ orthogonal_flow_baselines: Option<DerivedBaselines>,// For a table wrapper stretched in the block axis, its measured table box and content block sizes.stretched_table_wrapper_block_sizes: Option<sizing_context::TableWrapperBlockSizes>,}@@ -107,6 +110,7 @@ impl FlexItem<'_> {is_min_violation: false,is_max_violation: false,content_baselines: DerivedBaselines::default(),+ orthogonal_flow_baselines: None,stretched_table_wrapper_block_sizes: None,}}@@ -2837,18 +2841,15 @@ impl<'pass> FlexFormattingContext<'pass> {if self.run.callbacks.frame.is_transposed() && self.cross_axis_is_horizontal() {// NB: The cross axis is the horizontal axis of the frame, along which an item in an orthogonal flow// has its baselines.- let metrics = self.run.records.box_metrics_of(item.box_);+ let metrics = &item.cells;let collapsed = metrics.uses_collapsing_borders_model();let margin_box_left = metrics.margin_left() + metrics.border_box_left(collapsed);- return match metrics- .orthogonal_flow_baselines()- .and_then(|baselines| baselines.first)- {+ return match item.orthogonal_flow_baselines.and_then(|baselines| baselines.first) {Some(baseline) => margin_box_left + baseline,None => margin_box_left + metrics.content_inline_size() + metrics.border_box_right(collapsed),};}- let metrics = self.run.records.box_metrics_of(item.box_);+ let metrics = &item.cells;if self.item_baseline_is_synthesized(index) {let collapsed = metrics.uses_collapsing_borders_model();return metrics.margin_box_top(collapsed)@@ -2858,7 +2859,7 @@ impl<'pass> FlexFormattingContext<'pass> {formatting_context::box_baseline_with_content_baselines(&self.run.callbacks,item.box_,- &metrics,+ metrics,formatting_context::BaselineSet::First,item.content_baselines,)@@ -3100,7 +3101,13 @@ impl<'pass> FlexFormattingContext<'pass> {false,run.percentage_block_insets_of_root_children_have_definite_basis(),);- self.flex_items[index].cells = run.records.box_metrics_of(node).input_cells();+ self.flex_items[index].cells = match &laid_out_box {+ Some(laid_out_box) => laid_out_box.cells,+ // NB: The item was laid out by this flex container's run, in the record it keeps.+ None => run.records.box_metrics_of(node).input_cells(),+ };+ self.flex_items[index].orthogonal_flow_baselines =+ laid_out_box.and_then(|laid_out_box| laid_out_box.orthogonal_flow_baselines);outcome.report_percentage_block_size_dependency_of(run, node);self.flex_items[index].content_baselines = match outcome {ChildLayoutOutcome::Created(result) => result.baselines,@@ -3986,19 +3993,14 @@ impl<'pass> FlexFormattingContext<'pass> {.collect();self.resolve_baseline_aligned_items();for (index, (fragment_arrival, placed_item)) in laid_out_items.into_iter().enumerate() {- let item = &self.flex_items[index];- let offset = if self.main_axis_is_horizontal() {- FfiCssPixelPoint {- x: item.main_offset,- y: item.cross_offset,- }- } else {- FfiCssPixelPoint {- x: item.cross_offset,- y: item.main_offset,- }- };- formatting_context::place_laid_out_child(&self.run, item.box_, placed_item, offset, None, fragment_arrival);+ formatting_context::place_laid_out_child(+ &self.run,+ self.flex_items[index].box_,+ placed_item,+ self.item_content_offset(index),+ None,+ fragment_arrival,+ );}self.derived_baselines_of_root_box = DerivedBaselines {inline_alignment: self.flex_lines.len().checked_sub(1).and_then(|line_index| {@@ -4017,6 +4019,22 @@ impl<'pass> FlexFormattingContext<'pass> {};}+ /// The offset of the content box of the item at `index` in the container, where it is placed.+ fn item_content_offset(&self, index: usize) -> FfiCssPixelPoint {+ let item = &self.flex_items[index];+ if self.main_axis_is_horizontal() {+ FfiCssPixelPoint {+ x: item.main_offset,+ y: item.cross_offset,+ }+ } else {+ FfiCssPixelPoint {+ x: item.cross_offset,+ y: item.main_offset,+ }+ }+ }+// https://drafts.csswg.org/css-flexbox-1/#flex-baselinesfn baseline_of_line(&self, line_index: usize, baseline_set: formatting_context::BaselineSet) -> Option<CssPixels> {self.baseline_of_line_for_purpose(line_index, baseline_set, formatting_context::BaselinePurpose::Container)@@ -4054,10 +4072,15 @@ impl<'pass> FlexFormattingContext<'pass> {.max_by_key(|&index| self.flex_items[index].main_offset),})?;let source = self.baseline_source_for_item(item);- formatting_context::baseline_of_child_for_purpose(- self.run.records,+ let laid_out_item = &self.flex_items[item];+ formatting_context::baseline_of_laid_out_child_for_purpose(&self.run.callbacks,- self.flex_items[item].box_,+ formatting_context::LaidOutChild {+ node: laid_out_item.box_,+ metrics: &laid_out_item.cells,+ content_baselines: laid_out_item.content_baselines,+ content_offset: self.item_content_offset(item),+ },baseline_set,source,purpose,diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 4246e2a1d99..48e84b4fe67 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -1099,7 +1099,8 @@ pub(crate) fn baseline_of_child()}-pub(crate) fn baseline_of_child_for_purpose(+/// The baseline of `child`, a box `records` holds, relative to its container's margin edge.+fn baseline_of_child_for_purpose(records: &RunRecords,callbacks: &LayoutPass<'_>,child: Node,@@ -1108,7 +1109,52 @@ pub(crate) fn baseline_of_child_for_purpose(purpose: BaselinePurpose,) -> Option<CssPixels> {let child_metrics = records.box_metrics_of_if_owned(child)?;- let content_baselines = child_metrics.content_baselines();+ baseline_of_laid_out_child_for_purpose(+ callbacks,+ LaidOutChild {+ node: child,+ metrics: &child_metrics,+ content_baselines: child_metrics.content_baselines(),+ content_offset: records.content_offset_or_origin(child),+ },+ baseline_set,+ source,+ purpose,+ )+}++/// A child its parent laid out: its sizes and box sides, the baselines of its contents, and the offset of its content+/// box in its containing block, or the origin while it is not placed.+pub(crate) struct LaidOutChild<'metrics, Metrics: BoxMetricCells> {+ pub(crate) node: Node,+ pub(crate) metrics: &'metrics Metrics,+ pub(crate) content_baselines: DerivedBaselines,+ pub(crate) content_offset: FfiCssPixelPoint,+}++impl<Metrics: BoxMetricCells> Clone for LaidOutChild<'_, Metrics> {+ fn clone(&self) -> Self {+ *self+ }+}++impl<Metrics: BoxMetricCells> Copy for LaidOutChild<'_, Metrics> {}++/// The baseline of `child`, relative to its container's margin edge. A synthesized baseline comes from the child's+/// border box.+pub(crate) fn baseline_of_laid_out_child_for_purpose(+ callbacks: &LayoutPass<'_>,+ child: LaidOutChild<'_, impl BoxMetricCells>,+ baseline_set: BaselineSet,+ source: ChildBaselineSource,+ purpose: BaselinePurpose,+) -> Option<CssPixels> {+ let LaidOutChild {+ node,+ metrics: child_metrics,+ content_baselines,+ content_offset,+ } = child;let has_baseline = match baseline_set {BaselineSet::First => content_baselines.first.is_some(),BaselineSet::Last => content_baselines.last.is_some(),@@ -1121,23 +1167,13 @@ pub(crate) fn baseline_of_child_for_purpose(// https://drafts.csswg.org/css-align-3/#synthesize-baseline// The alphabetic baseline is synthesized from the line-under edge of the border box.return Some(- records.content_offset_or_origin(child).y- + child_metrics.content_block_size()- + child_metrics.border_box_bottom(collapsed),+ content_offset.y + child_metrics.content_block_size() + child_metrics.border_box_bottom(collapsed),);}- let child_offset_from_margin_edge =- records.content_offset_or_origin(child).y - child_metrics.margin_box_top(collapsed);+ let child_offset_from_margin_edge = content_offset.y - child_metrics.margin_box_top(collapsed);Some(child_offset_from_margin_edge- + box_baseline_for_purpose(- callbacks,- child,- &child_metrics,- baseline_set,- content_baselines,- purpose,- ),+ + box_baseline_for_purpose(callbacks, node, child_metrics, baseline_set, content_baselines, purpose),)}