Fix vertical alignment at the root of an IFC (#31636)

At the root of an inline formatting context, we used its vertical-align
in order to compute the strut. That was wrong, since vertical-align
on a block container shouldn't affect the contents, it should only
affect the alignment of the block container (if it's inline-level)
within the parent IFC.

This was only working well if the block container was block-level, since
effective_vertical_align_for_inline_layout returned `baseline` for
block-level boxes.

Instead of the outer display type, this patch changes the logic to check
whether we are at the root of the IFC.
This commit is contained in:
Oriol Brufau 2024-03-13 11:39:01 +01:00 committed by GitHub
parent 63527f56ca
commit 0860deba05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 223 additions and 58 deletions

View file

@ -10,7 +10,7 @@ use gfx::font::FontMetrics;
use gfx::text::glyph::GlyphStore;
use servo_arc::Arc;
use style::properties::ComputedValues;
use style::values::computed::{Length, LengthPercentage};
use style::values::computed::Length;
use style::values::generics::box_::{GenericVerticalAlign, VerticalAlignKeyword};
use style::values::generics::text::LineHeight;
use style::values::specified::box_::DisplayOutside;
@ -28,7 +28,7 @@ use crate::geom::{LogicalRect, LogicalVec2};
use crate::positioned::{
relative_adjustement, AbsolutelyPositionedBox, PositioningContext, PositioningContextLength,
};
use crate::style_ext::{ComputedValuesExt, PaddingBorderMargin};
use crate::style_ext::PaddingBorderMargin;
use crate::ContainingBlock;
pub(super) struct LineMetrics {
@ -226,16 +226,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 mut start_corner = &LogicalVec2 {
let start_corner = &LogicalVec2 {
inline: state.inline_position,
block: (state.baseline_offset - self.font_metrics.ascent).into(),
} - &state.parent_offset;
if !is_baseline_relative(
self.parent_style
.effective_vertical_align_for_inline_layout(),
) {
start_corner.block = Length::zero();
}
let rect = LogicalRect {
start_corner,
@ -428,12 +422,11 @@ impl InlineBoxLineItem {
/// Given the state for a line item layout and the space above the baseline for this inline
/// box, find the block start position relative to the line block start position.
fn calculate_block_start(&self, state: &LineItemLayoutState, space_above_baseline: Au) -> Au {
let vertical_align = self.style.effective_vertical_align_for_inline_layout();
let line_gap = self.font_metrics.line_gap;
// The baseline offset that we have in `Self::baseline_offset` is relative to the line
// baseline, so we need to make it relative to the line block start.
match vertical_align {
match self.style.clone_vertical_align() {
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Top) => {
let line_height: Au = line_height(&self.style, &self.font_metrics).into();
(line_height - line_gap).scale_by(0.5)
@ -586,14 +579,6 @@ impl FloatLineItem {
}
}
fn is_baseline_relative(vertical_align: GenericVerticalAlign<LengthPercentage>) -> bool {
!matches!(
vertical_align,
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Top) |
GenericVerticalAlign::Keyword(VerticalAlignKeyword::Bottom)
)
}
fn line_height(parent_style: &ComputedValues, font_metrics: &FontMetrics) -> Length {
let font_size = parent_style.get_font().font_size.computed_size();
match parent_style.get_inherited_text().line_height {