Truly independent web browser
LibWeb: Align vertical text by its central baseline across the parent
sideshowbarkerd71e6fad84bb
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 51b81fcc09c..c2bbab422c5 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -2590,7 +2590,7 @@ fn lay_out_run_root_and_contents<'pass>(own_root_after_body.content_inline_size,own_frame.writing_mode(),);- result = result_in_parent_frame(result, own_root_after_body, parent_frame, own_frame);+ result = result_in_parent_frame(&pass, run.box_, result, own_root_after_body, parent_frame, own_frame);}root.set(own_root_after_body.in_frame(own_frame, parent_frame));@@ -2686,6 +2686,8 @@ fn lay_out_run_root_and_contents<'pass>(result.orthogonal_flow_baselines = orthogonal_flow_baselines(shifted_baselines,root_in_own_frame_left_by_run.content_block_size,+ &pass,+ run.box_,parent_frame,own_frame,);@@ -2927,6 +2929,8 @@ 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(+ pass: &LayoutPass<'_>,+ root: Node,mut result: ChildLayoutResult,root_in_own_frame: used_values::RootInputCells,parent_frame: geometry::Frame,@@ -2942,6 +2946,8 @@ fn result_in_parent_frame(result.orthogonal_flow_baselines = orthogonal_flow_baselines(result.baselines,root_in_own_frame.content_block_size,+ pass,+ root,parent_frame,own_frame,);@@ -2966,6 +2972,8 @@ fn result_in_parent_frame(fn orthogonal_flow_baselines(baselines: DerivedBaselines,content_block_size: CssPixels,+ pass: &LayoutPass<'_>,+ root: Node,parent_frame: geometry::Frame,own_frame: geometry::Frame,) -> Option<DerivedBaselines> {@@ -2974,8 +2982,16 @@ fn orthogonal_flow_baselines(own_frame.side_in(geometry::Side::Top, parent_frame),geometry::Side::Left);+ // NB: Only lines in a vertical typographic mode have a dominant baseline other than the alphabetic one, so only+ // then is the block whose lines give the baseline looked for.+ let distance_to_dominant_baseline = if dominant_baseline_is_central(StyleValues::for_node(pass, root)) {+ distance_from_alphabetic_to_dominant_baseline(style_of_first_baseline_line_root(pass, root))+ } else {+ CssPixels::default()+ };let along_parent_horizontal_axis = |baseline: Option<CssPixels>| {baseline.map(|baseline| {+ let baseline = baseline - distance_to_dominant_baseline;if block_start_is_left {baseline} else {@@ -2991,6 +3007,64 @@ fn orthogonal_flow_baselines((orthogonal_flow_baselines != DerivedBaselines::default()).then_some(orthogonal_flow_baselines)}+/// The style of the block container whose lines give `node` its first baseline: `node` itself, where its children are+/// inline, or else the first in-flow descendant in document order that gives it baselines and has a line of content. So,+/// the dominant baseline of text in a nested block is found from that block's font, and an empty block before it is+/// passed over, as it gives no baseline.+fn style_of_first_baseline_line_root<'pass>(pass: &LayoutPass<'pass>, node: Node) -> StyleValues<'pass> {+ // NB: An explicit stack rather than recursion, as the boxes can nest arbitrarily deep.+ let mut candidates = vec![node];+ while let Some(current) = candidates.pop() {+ let facts = NodeFacts::new(pass, current);+ if !facts.is_block_container() {+ // NB: A box that establishes another kind of formatting context gives the baselines of its own contents.+ return StyleValues::for_node(pass, current);+ }+ if facts.children_are_inline() {+ if pass+ .children(current)+ .any(|child| !pass.can_skip_is_anonymous_text_run(child))+ {+ return StyleValues::for_node(pass, current);+ }+ continue;+ }+ let first_candidate = candidates.len();+ candidates.extend(pass.children(current).filter(|&child| {+ NodeFacts::new(pass, child).is_box()+ && !pass.can_skip_is_anonymous_text_run(child)+ && child_gives_container_baselines(pass, current, child)+ }));+ candidates[first_candidate..].reverse();+ }+ StyleValues::for_node(pass, node)+}++/// How far a box's dominant baseline lies from the alphabetic baseline its lines record, toward the top of its frame.+/// https://drafts.csswg.org/css-inline-3/#valdef-dominant-baseline-auto+/// If in horizontal typographic mode, the dominant baseline is alphabetic. If in vertical typographic mode and+/// text-orientation is mixed or upright, the dominant baseline is central.+/// https://drafts.csswg.org/css-inline-3/#central+/// central: Use the central baseline (halfway between the ascent and descent).+fn distance_from_alphabetic_to_dominant_baseline(style: StyleValues<'_>) -> CssPixels {+ if !dominant_baseline_is_central(style) {+ return CssPixels::default();+ }+ // NB: A line puts the ascent of its text toward the top of its frame, and counts the font's metrics in whole+ // pixels.+ let ascent = line_builder::font_metric_in_whole_pixels(style.font_ascent());+ let descent = line_builder::font_metric_in_whole_pixels(style.font_descent());+ (ascent - descent) / 2+}++/// Whether the lines of a box with `style` have the central baseline as their dominant one, rather than the alphabetic.+fn dominant_baseline_is_central(style: StyleValues<'_>) -> bool {+ matches!(+ style.writing_mode(),+ writing_mode::VERTICAL_RL | writing_mode::VERTICAL_LR+ ) && style.text_orientation() != text_orientation::SIDEWAYS+}+fn finalize_atomic_root_block_size(run: &FormattingContextRun,sized_root: sizing_context::SizedCells<'_>,diff --git a/Tests/LibWeb/Text/expected/Layout/flex-column-baseline-alignment-across-horizontal-axis.txt b/Tests/LibWeb/Text/expected/Layout/flex-column-baseline-alignment-across-horizontal-axis.txtindex ff1814525cc..0b0a7f8bca3 100644--- a/Tests/LibWeb/Text/expected/Layout/flex-column-baseline-alignment-across-horizontal-axis.txt+++ b/Tests/LibWeb/Text/expected/Layout/flex-column-baseline-alignment-across-horizontal-axis.txt@@ -3,6 +3,13 @@ different-margins: 15, 15horizontal-text: 0, 0vertical-lr: 10, 10, 10vertical-rl: 150, 150, 0+vertical-lr-text: 0, 5+vertical-rl-text: 170, 175+vertical-lr-text-two-lines: 0, 5+vertical-lr-text-in-nested-block: 0, 0+vertical-lr-text-after-empty-block: 0, 0+sideways-text: 170, 172+upright-text: 0, 5align-self: 85, 0auto-margin: 170, 15rtl: 135, 135diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/align-items-baseline-column-horz.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/align-items-baseline-column-horz.txtindex 7d22d95a628..7e63c9e7d77 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/align-items-baseline-column-horz.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/align-items-baseline-column-horz.txt@@ -2,10 +2,9 @@ Harness status: OKFound 5 tests-2 Pass-3 Fail+5 PassPass #target > div 1-Fail #target > div 2-Fail #target > div 3+Pass #target > div 2+Pass #target > div 3Pass #target > div 4-Fail #target > div 5\ No newline at end of file+Pass #target > div 5\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/Layout/flex-column-baseline-alignment-across-horizontal-axis.html b/Tests/LibWeb/Text/input/Layout/flex-column-baseline-alignment-across-horizontal-axis.htmlindex 484ce8d3d12..4d5e1d8c07c 100644--- a/Tests/LibWeb/Text/input/Layout/flex-column-baseline-alignment-across-horizontal-axis.html+++ b/Tests/LibWeb/Text/input/Layout/flex-column-baseline-alignment-across-horizontal-axis.html@@ -6,15 +6,23 @@.f { display: flex; flex-direction: column; align-items: baseline; width: 200px; margin-bottom: 10px; }.f > div { background: silver; }</style>-<!-- In a column flex container, baseline alignment runs across the horizontal axis. An item with no baseline there- synthesizes one from its left border edge. A vertical-lr item aligns with the others at the left edge, a- vertical-rl item with the other vertical-rl items at the right edge, and any other item at the cross-start edge.- An item with an auto cross-axis margin doesn't take part. Each line gives each item's offset from the left. -->+<!-- In a column flex container, baseline alignment runs across the horizontal axis. An item in a vertical writing mode+ aligns by its dominant baseline: central, unless its text is sideways. Any other item synthesizes a baseline from+ its left border edge. A vertical-lr item aligns at the left edge, a vertical-rl item at the right edge, and any+ other item at the cross-start edge. An item with an auto cross-axis margin doesn't take part. Each line gives+ each item's offset from the left. --><div class="f" id="different-heights"><div style="width: 30px; height: 20px"></div><div style="width: 30px; height: 50px"></div></div><div class="f" id="different-margins"><div style="width: 30px; height: 20px; margin-left: 15px"></div><div style="width: 30px; height: 50px"></div></div><div class="f" id="horizontal-text"><div>ab</div><div style="font-size: 40px">ab</div></div><div class="f" id="vertical-lr"><div style="writing-mode: vertical-lr; width: 20px; height: 20px; margin-left: 10px"></div><div style="writing-mode: vertical-lr; width: 40px; height: 20px"></div><div style="width: 30px; height: 20px"></div></div><div class="f" id="vertical-rl"><div style="writing-mode: vertical-rl; width: 20px; height: 20px"></div><div style="writing-mode: vertical-rl; width: 40px; height: 20px; margin-right: 10px"></div><div style="width: 30px; height: 20px"></div></div>+<div class="f" id="vertical-lr-text"><div style="writing-mode: vertical-lr; font-size: 30px">ab</div><div style="writing-mode: vertical-lr">ab</div></div>+<div class="f" id="vertical-rl-text"><div style="writing-mode: vertical-rl; font-size: 30px">ab</div><div style="writing-mode: vertical-rl">ab</div></div>+<div class="f" id="vertical-lr-text-two-lines"><div style="writing-mode: vertical-lr; font-size: 30px">ab<br>cd</div><div style="writing-mode: vertical-lr">ab</div></div>+<div class="f" id="vertical-lr-text-in-nested-block"><div style="writing-mode: vertical-lr; font-size: 10px"><div style="font-size: 30px">ab</div></div><div style="writing-mode: vertical-lr; font-size: 30px">ab</div></div>+<div class="f" id="vertical-lr-text-after-empty-block"><div style="writing-mode: vertical-lr; font-size: 10px"><div></div><div style="font-size: 30px">ab</div></div><div style="writing-mode: vertical-lr; font-size: 30px">ab</div></div>+<div class="f" id="sideways-text"><div style="writing-mode: vertical-rl; text-orientation: sideways; font-size: 30px">ab</div><div style="writing-mode: vertical-rl; text-orientation: sideways">ab</div></div>+<div class="f" id="upright-text"><div style="writing-mode: vertical-lr; text-orientation: upright; font-size: 30px">ab</div><div style="writing-mode: vertical-lr; text-orientation: upright">ab</div></div><div class="f" id="align-self"><div style="width: 30px; height: 20px; align-self: center"></div><div style="width: 30px; height: 50px"></div></div><div class="f" id="auto-margin"><div style="width: 30px; height: 20px; margin-left: auto"></div><div style="width: 30px; height: 50px; margin-left: 15px"></div></div><div class="f" id="rtl" style="direction: rtl"><div style="width: 30px; height: 20px"></div><div style="width: 50px; height: 50px; margin-right: 15px"></div></div>