Skip to content

Truly independent web browser

32.0 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

use super::*;

/// A resolved px-or-auto inset produced by anchor resolution. It takes
/// precedence over every style read and reports
/// contains_anchor_function() == false.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResolvedInsetOverride {
    Auto,
    Pixels(CssPixels),
}

#[derive(Clone, Copy)]
pub(crate) enum InsetValue<'a> {
    FromStyle(&'a ComputedLengthPercentageOrAuto),
    BareAnchor(&'a crate::css::style_value::StyleValueData),
    Resolved(ResolvedInsetOverride),
}

impl InsetValue<'_> {
    pub(crate) fn auto_value() -> Self {
        Self::Resolved(ResolvedInsetOverride::Auto)
    }

    pub(crate) fn is_auto(self) -> bool {
        match self {
            Self::FromStyle(value) => value.is_auto(),
            Self::BareAnchor(_) => false,
            Self::Resolved(resolved) => matches!(resolved, ResolvedInsetOverride::Auto),
        }
    }

    pub(crate) fn to_px(self, reference: CssPixels) -> CssPixels {
        match self {
            Self::FromStyle(value) => value.to_px(reference),
            Self::BareAnchor(wrapper) => resolve_calc_to_px(std::ptr::from_ref(wrapper).cast(), reference),
            Self::Resolved(ResolvedInsetOverride::Auto) => CssPixels::default(),
            Self::Resolved(ResolvedInsetOverride::Pixels(px)) => px,
        }
    }

    pub(crate) fn contains_percentage(self) -> bool {
        match self {
            Self::FromStyle(value) => value.contains_percentage(),
            Self::BareAnchor(_) | Self::Resolved(_) => false,
        }
    }

    pub(crate) fn contains_anchor_function(self) -> bool {
        match self {
            Self::FromStyle(value) => value
                .length_percentage()
                .is_some_and(|length_percentage| length_percentage.contains_anchor_function()),
            Self::BareAnchor(_) => true,
            Self::Resolved(_) => false,
        }
    }

    /// The calculated style value carrying the anchor() function, for the
    /// abspos engine's anchor-aware resolution.
    pub(crate) fn anchor_bearing_calculated(self) -> *const c_void {
        match self {
            Self::FromStyle(value) => value
                .length_percentage()
                .expect("anchor-bearing inset must hold a style value")
                .calculated_pointer(),
            Self::BareAnchor(wrapper) => std::ptr::from_ref(wrapper).cast(),
            Self::Resolved(_) => unreachable!("resolved inset overrides never carry anchor functions"),
        }
    }
}

/// A sizing property whose computed value holds anchor-size() functions, as the abspos engine resolved it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResolvedSizeOverride {
    Length(CssPixels),
    Keyword(ComputedSizeKind),
}

/// The sizing and margin properties of one positioned box whose computed values hold anchor-size() functions, with
/// those functions resolved by the abspos engine. A property without one stays None and reads from style, as does a
/// sizing property whose anchor-size() functions all resolve to their fallback values. Margins also carry the zero that
/// auto resolves to in the axes a position area or anchor-center aligns the box in.
/// NB: The properties are those of the frame the box is laid out in, as style reads see them there.
/// https://drafts.csswg.org/css-anchor-position-1/#anchor-size-fn
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct ResolvedAnchorSizes {
    pub(crate) width: Option<ResolvedSizeOverride>,
    pub(crate) min_width: Option<ResolvedSizeOverride>,
    pub(crate) max_width: Option<ResolvedSizeOverride>,
    pub(crate) height: Option<ResolvedSizeOverride>,
    pub(crate) min_height: Option<ResolvedSizeOverride>,
    pub(crate) max_height: Option<ResolvedSizeOverride>,
    pub(crate) margin_top: Option<CssPixels>,
    pub(crate) margin_right: Option<CssPixels>,
    pub(crate) margin_bottom: Option<CssPixels>,
    pub(crate) margin_left: Option<CssPixels>,
}

impl ResolvedAnchorSizes {
    pub(crate) fn is_empty(&self) -> bool {
        *self == Self::default()
    }
}

/// The computed values `ResolvedAnchorSizes` stands for, which style reads borrow.
#[derive(Default)]
pub(crate) struct AnchorResolvedValues {
    width: Option<ComputedSize>,
    min_width: Option<ComputedSize>,
    max_width: Option<ComputedSize>,
    height: Option<ComputedSize>,
    min_height: Option<ComputedSize>,
    max_height: Option<ComputedSize>,
    margin_top: Option<ComputedLengthPercentageOrAuto>,
    margin_right: Option<ComputedLengthPercentageOrAuto>,
    margin_bottom: Option<ComputedLengthPercentageOrAuto>,
    margin_left: Option<ComputedLengthPercentageOrAuto>,
    /// The frame the box is laid out in, whose sides and axes the values are those of.
    frame: geometry::Frame,
}

impl std::fmt::Debug for AnchorResolvedValues {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_struct("AnchorResolvedValues").finish_non_exhaustive()
    }
}

impl AnchorResolvedValues {
    pub(crate) fn new(resolved: &ResolvedAnchorSizes, frame: geometry::Frame) -> Self {
        let size = |value: Option<ResolvedSizeOverride>| {
            value.map(|value| match value {
                ResolvedSizeOverride::Length(px) => ComputedSize::new(
                    ComputedSizeKind::Length,
                    ComputedStyleValueHandle::length(px.to_double()),
                ),
                ResolvedSizeOverride::Keyword(kind) => ComputedSize::keyword(kind),
            })
        };
        let margin = |value: Option<CssPixels>| {
            value.map(|px| ComputedLengthPercentageOrAuto::new(false, ComputedStyleValueHandle::length(px.to_double())))
        };
        Self {
            width: size(resolved.width),
            min_width: size(resolved.min_width),
            max_width: size(resolved.max_width),
            height: size(resolved.height),
            min_height: size(resolved.min_height),
            max_height: size(resolved.max_height),
            margin_top: margin(resolved.margin_top),
            margin_right: margin(resolved.margin_right),
            margin_bottom: margin(resolved.margin_bottom),
            margin_left: margin(resolved.margin_left),
            frame,
        }
    }
}

/// A padding read: the style's, or zero for a non-devolvable widget.
#[derive(Clone, Copy)]
pub(crate) enum PaddingValue<'a> {
    FromStyle(&'a ComputedLengthPercentageOrAuto),
    Zero,
}

impl PaddingValue<'_> {
    pub(crate) fn to_px(self, reference: CssPixels) -> CssPixels {
        match self {
            Self::FromStyle(value) => value.to_px(reference),
            Self::Zero => CssPixels::default(),
        }
    }

    pub(crate) fn contains_percentage(self) -> bool {
        match self {
            Self::FromStyle(value) => value.contains_percentage(),
            Self::Zero => false,
        }
    }
}

/// The layout pass view of a node's computed style: the pure payload view
/// plus the resolved anchor insets the abspos engine threads to the four
/// inset reads of the box it is placing, the line builder's vertical-align
/// keyword substitution, and the padding and border widths a non-devolvable
/// widget drops. Every other read derefs to ComputedValuesView.
#[derive(Clone, Copy)]
pub(crate) struct StyleValues<'a> {
    style: ComputedValuesView<'a>,
    resolved_anchor_insets: Option<&'a formatting_context::ResolvedAnchorInsets>,
    anchor_resolved_values: Option<&'a AnchorResolvedValues>,
    vertical_align_override: Option<u8>,
    is_non_devolvable_widget: bool,
    frame: geometry::Frame,
}

impl<'a> std::ops::Deref for StyleValues<'a> {
    type Target = ComputedValuesView<'a>;

    fn deref(&self) -> &ComputedValuesView<'a> {
        &self.style
    }
}

impl<'a> StyleValues<'a> {
    #[inline]
    pub(crate) fn new(payloads: ComputedValuesView<'a>) -> Self {
        Self {
            style: payloads,
            resolved_anchor_insets: None,
            anchor_resolved_values: None,
            vertical_align_override: None,
            is_non_devolvable_widget: false,
            frame: geometry::Frame::PHYSICAL,
        }
    }

    #[inline]
    pub(crate) fn for_node(pass: &LayoutPass<'a>, node: Node) -> Self {
        let (data, style) = pass.row_and_style(node);
        StyleValues::new(style.expect("styled node must publish its style container before layout"))
            .for_kind(data.kind.get())
            .in_frame(pass.frame)
    }

    /// The view as seen from `frame`, whose sides and axes the box's sizes and box model properties are read in.
    #[inline]
    pub(crate) fn in_frame(mut self, frame: geometry::Frame) -> Self {
        self.frame = frame;
        self
    }

    #[inline]
    fn physical(self, side: geometry::Side) -> geometry::Side {
        if self.frame.is_physical() {
            return side;
        }
        self.frame.physical_side(side)
    }

    /// `physical`, unless the frame is transposed and so reads the value of the other physical axis, `transposed`.
    #[inline]
    fn physical_or_transposed<T>(self, physical: T, transposed: T) -> T {
        if self.frame.is_transposed() {
            transposed
        } else {
            physical
        }
    }

    /// Whether the inline-start side is the frame's right side rather than its left side.
    #[inline]
    pub(crate) fn inline_axis_is_reverse(self) -> bool {
        if self.frame.is_physical() {
            return self.style.inline_axis_is_reverse();
        }
        // NB: The left side of a frame is the line-left side, which is inline-start in ltr text.
        self.style.direction() == direction::RTL
    }

    /// Whether the block-start side is the frame's bottom side rather than its top side.
    #[inline]
    pub(crate) fn block_axis_is_reverse(self) -> bool {
        if self.frame.is_physical() {
            return self.style.block_axis_is_reverse();
        }
        false
    }

    #[inline]
    pub(crate) fn unresolved_width(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.width(), self.style.height())
    }

    #[inline]
    pub(crate) fn unresolved_height(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.height(), self.style.width())
    }

    #[inline]
    pub(crate) fn unresolved_min_width(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.min_width(), self.style.min_height())
    }

    #[inline]
    pub(crate) fn unresolved_min_height(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.min_height(), self.style.min_width())
    }

    #[inline]
    pub(crate) fn unresolved_max_width(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.max_width(), self.style.max_height())
    }

    #[inline]
    pub(crate) fn unresolved_max_height(self) -> &'a ComputedSize {
        self.physical_or_transposed(self.style.max_height(), self.style.max_width())
    }

    #[inline]
    pub(crate) fn contain_intrinsic_width_has_length(self) -> bool {
        self.physical_or_transposed(
            self.style.contain_intrinsic_width_has_length(),
            self.style.contain_intrinsic_height_has_length(),
        )
    }

    #[inline]
    pub(crate) fn contain_intrinsic_width_px(self) -> f64 {
        self.physical_or_transposed(
            self.style.contain_intrinsic_width_px(),
            self.style.contain_intrinsic_height_px(),
        )
    }

    #[inline]
    pub(crate) fn contain_intrinsic_height_has_length(self) -> bool {
        self.physical_or_transposed(
            self.style.contain_intrinsic_height_has_length(),
            self.style.contain_intrinsic_width_has_length(),
        )
    }

    #[inline]
    pub(crate) fn contain_intrinsic_height_px(self) -> f64 {
        self.physical_or_transposed(
            self.style.contain_intrinsic_height_px(),
            self.style.contain_intrinsic_width_px(),
        )
    }

    /// The preferred aspect ratio as a width to height fraction in the frame.
    #[inline]
    pub(crate) fn css_preferred_aspect_ratio(self) -> (CssPixels, CssPixels) {
        let (numerator, denominator) = self.style.css_preferred_aspect_ratio();
        if self.frame.is_transposed() {
            (denominator, numerator)
        } else {
            (numerator, denominator)
        }
    }

    #[inline]
    pub(crate) fn border_spacing_horizontal(self) -> CssPixels {
        self.physical_or_transposed(
            self.style.border_spacing_horizontal(),
            self.style.border_spacing_vertical(),
        )
    }

    #[inline]
    pub(crate) fn border_spacing_vertical(self) -> CssPixels {
        self.physical_or_transposed(
            self.style.border_spacing_vertical(),
            self.style.border_spacing_horizontal(),
        )
    }

    #[inline]
    fn physical_margin(self, side: geometry::Side) -> &'a ComputedLengthPercentageOrAuto {
        match side {
            geometry::Side::Top => self.style.margin_top(),
            geometry::Side::Right => self.style.margin_right(),
            geometry::Side::Bottom => self.style.margin_bottom(),
            geometry::Side::Left => self.style.margin_left(),
        }
    }

    #[inline]
    pub(crate) fn unresolved_margin_top(self) -> &'a ComputedLengthPercentageOrAuto {
        self.physical_margin(self.physical(geometry::Side::Top))
    }

    #[inline]
    pub(crate) fn unresolved_margin_right(self) -> &'a ComputedLengthPercentageOrAuto {
        self.physical_margin(self.physical(geometry::Side::Right))
    }

    #[inline]
    pub(crate) fn unresolved_margin_bottom(self) -> &'a ComputedLengthPercentageOrAuto {
        self.physical_margin(self.physical(geometry::Side::Bottom))
    }

    #[inline]
    pub(crate) fn unresolved_margin_left(self) -> &'a ComputedLengthPercentageOrAuto {
        self.physical_margin(self.physical(geometry::Side::Left))
    }

    #[inline]
    fn physical_border(self, side: geometry::Side) -> (CssPixels, u8, u32) {
        match side {
            geometry::Side::Top => (
                self.style.border_top_width(),
                self.style.border_top_style(),
                self.style.border_top_color(),
            ),
            geometry::Side::Right => (
                self.style.border_right_width(),
                self.style.border_right_style(),
                self.style.border_right_color(),
            ),
            geometry::Side::Bottom => (
                self.style.border_bottom_width(),
                self.style.border_bottom_style(),
                self.style.border_bottom_color(),
            ),
            geometry::Side::Left => (
                self.style.border_left_width(),
                self.style.border_left_style(),
                self.style.border_left_color(),
            ),
        }
    }

    #[inline]
    pub(crate) fn border_top_style(self) -> u8 {
        self.physical_border(self.physical(geometry::Side::Top)).1
    }

    #[inline]
    pub(crate) fn border_right_style(self) -> u8 {
        self.physical_border(self.physical(geometry::Side::Right)).1
    }

    #[inline]
    pub(crate) fn border_bottom_style(self) -> u8 {
        self.physical_border(self.physical(geometry::Side::Bottom)).1
    }

    #[inline]
    pub(crate) fn border_left_style(self) -> u8 {
        self.physical_border(self.physical(geometry::Side::Left)).1
    }

    #[inline]
    pub(crate) fn border_top_color(self) -> u32 {
        self.physical_border(self.physical(geometry::Side::Top)).2
    }

    #[inline]
    pub(crate) fn border_right_color(self) -> u32 {
        self.physical_border(self.physical(geometry::Side::Right)).2
    }

    #[inline]
    pub(crate) fn border_bottom_color(self) -> u32 {
        self.physical_border(self.physical(geometry::Side::Bottom)).2
    }

    #[inline]
    pub(crate) fn border_left_color(self) -> u32 {
        self.physical_border(self.physical(geometry::Side::Left)).2
    }

    /// The view for a box of `kind`. A non-devolvable widget is drawn natively to fill its box, so author padding and
    /// borders don't apply to it.
    #[inline]
    pub(crate) fn for_kind(mut self, kind: NodeKind) -> Self {
        self.is_non_devolvable_widget = node_facts::kind_is_non_devolvable_widget(kind);
        self
    }

    pub(crate) fn with_resolved_insets(
        mut self,
        resolved: Option<&'a formatting_context::ResolvedAnchorInsets>,
    ) -> Self {
        self.resolved_anchor_insets = resolved;
        self
    }

    pub(crate) fn with_anchor_resolved_values(mut self, resolved: Option<&'a AnchorResolvedValues>) -> Self {
        // FIXME: Map the resolved values into the frame of a box that lays out its contents in another writing mode
        //        than its containing block, rather than reading its computed values there.
        self.anchor_resolved_values = resolved.filter(|resolved| resolved.frame == self.frame);
        self
    }

    pub(crate) fn width(self) -> &'a ComputedSize {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.width.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_width(), auto_computed_size()))
    }

    pub(crate) fn min_width(self) -> &'a ComputedSize {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.min_width.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_min_width(), auto_computed_size()))
    }

    pub(crate) fn max_width(self) -> &'a ComputedSize {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.max_width.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_max_width(), none_computed_size()))
    }

    pub(crate) fn height(self) -> &'a ComputedSize {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.height.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_height(), auto_computed_size()))
    }

    /// Whether the box is a table row, row group or cell, whose 'min-height' and 'max-height' are ignored.
    // NB: "In CSS 2.1, the effect of 'min-height' and 'max-height' on tables, inline tables, table cells, table rows,
    //     and row groups is undefined." https://www.w3.org/TR/CSS22/visudet.html#min-max-heights
    // INTEROP: Other engines ignore them on table cells, rows and row groups.
    fn ignores_min_and_max_height(self) -> bool {
        let display = self.display();
        display.is_table_cell() || display.is_table_row() || display.is_table_row_group_kind()
    }

    pub(crate) fn min_height(self) -> &'a ComputedSize {
        if self.ignores_min_and_max_height() {
            return auto_computed_size();
        }
        self.anchor_resolved_values
            .and_then(|resolved| resolved.min_height.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_min_height(), auto_computed_size()))
    }

    pub(crate) fn max_height(self) -> &'a ComputedSize {
        if self.ignores_min_and_max_height() {
            return none_computed_size();
        }
        self.anchor_resolved_values
            .and_then(|resolved| resolved.max_height.as_ref())
            .unwrap_or_else(|| unresolvable_anchor_size_or(self.unresolved_max_height(), none_computed_size()))
    }

    pub(crate) fn margin_top(self) -> &'a ComputedLengthPercentageOrAuto {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.margin_top.as_ref())
            .unwrap_or_else(|| self.unresolved_margin_top())
    }

    pub(crate) fn margin_right(self) -> &'a ComputedLengthPercentageOrAuto {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.margin_right.as_ref())
            .unwrap_or_else(|| self.unresolved_margin_right())
    }

    pub(crate) fn margin_bottom(self) -> &'a ComputedLengthPercentageOrAuto {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.margin_bottom.as_ref())
            .unwrap_or_else(|| self.unresolved_margin_bottom())
    }

    pub(crate) fn margin_left(self) -> &'a ComputedLengthPercentageOrAuto {
        self.anchor_resolved_values
            .and_then(|resolved| resolved.margin_left.as_ref())
            .unwrap_or_else(|| self.unresolved_margin_left())
    }

    fn bare_anchor_wrapper(self, physical_side: geometry::Side) -> Option<&'a crate::css::style_value::StyleValueData> {
        let values = self.style.surround();
        let handle = match physical_side {
            geometry::Side::Top => &values.top_anchor_inset_wrapper,
            geometry::Side::Right => &values.right_anchor_inset_wrapper,
            geometry::Side::Bottom => &values.bottom_anchor_inset_wrapper,
            geometry::Side::Left => &values.left_anchor_inset_wrapper,
        };
        // SAFETY: The node's style group payload retains the wrapper for the
        // view's lifetime.
        unsafe {
            handle
                .pointer
                .cast::<crate::css::style_value::StyleValueData>()
                .as_ref()
        }
    }

    fn inset_value(self, side: geometry::Side) -> InsetValue<'a> {
        // NB: The abspos engine resolves anchor functions per side of the frame it lays the box out in.
        if let Some(resolved) = self.resolved_anchor_insets.and_then(|insets| insets.override_for(side)) {
            return InsetValue::Resolved(resolved);
        }
        let physical_side = self.physical(side);
        if let Some(wrapper) = self.bare_anchor_wrapper(physical_side) {
            return InsetValue::BareAnchor(wrapper);
        }
        let values = self.style.surround();
        InsetValue::FromStyle(match physical_side {
            geometry::Side::Top => &values.inset.top,
            geometry::Side::Right => &values.inset.right,
            geometry::Side::Bottom => &values.inset.bottom,
            geometry::Side::Left => &values.inset.left,
        })
    }

    pub(crate) fn inset_top(self) -> InsetValue<'a> {
        self.inset_value(geometry::Side::Top)
    }

    pub(crate) fn inset_right(self) -> InsetValue<'a> {
        self.inset_value(geometry::Side::Right)
    }

    pub(crate) fn inset_bottom(self) -> InsetValue<'a> {
        self.inset_value(geometry::Side::Bottom)
    }

    pub(crate) fn inset_left(self) -> InsetValue<'a> {
        self.inset_value(geometry::Side::Left)
    }

    // AD-HOC: CSS UI only says a non-devolvable widget keeps its native appearance whatever its border; it doesn't say
    //         the border takes no room, nor anything about padding. A native control fills its box, so its border
    //         widths and its padding read as zero.
    fn padding_unless_non_devolvable(self, padding: &'a ComputedLengthPercentageOrAuto) -> PaddingValue<'a> {
        if self.is_non_devolvable_widget {
            PaddingValue::Zero
        } else {
            PaddingValue::FromStyle(padding)
        }
    }

    fn border_width_unless_non_devolvable(self, width: CssPixels) -> CssPixels {
        if self.is_non_devolvable_widget {
            CssPixels::default()
        } else {
            width
        }
    }

    #[inline]
    fn physical_padding(self, side: geometry::Side) -> PaddingValue<'a> {
        self.padding_unless_non_devolvable(match side {
            geometry::Side::Top => self.style.padding_top(),
            geometry::Side::Right => self.style.padding_right(),
            geometry::Side::Bottom => self.style.padding_bottom(),
            geometry::Side::Left => self.style.padding_left(),
        })
    }

    #[inline]
    pub(crate) fn padding_top(self) -> PaddingValue<'a> {
        self.physical_padding(self.physical(geometry::Side::Top))
    }

    #[inline]
    pub(crate) fn padding_right(self) -> PaddingValue<'a> {
        self.physical_padding(self.physical(geometry::Side::Right))
    }

    #[inline]
    pub(crate) fn padding_bottom(self) -> PaddingValue<'a> {
        self.physical_padding(self.physical(geometry::Side::Bottom))
    }

    #[inline]
    pub(crate) fn padding_left(self) -> PaddingValue<'a> {
        self.physical_padding(self.physical(geometry::Side::Left))
    }

    #[inline]
    pub(crate) fn border_top_width(self) -> CssPixels {
        self.border_width_unless_non_devolvable(self.physical_border(self.physical(geometry::Side::Top)).0)
    }

    #[inline]
    pub(crate) fn border_right_width(self) -> CssPixels {
        self.border_width_unless_non_devolvable(self.physical_border(self.physical(geometry::Side::Right)).0)
    }

    #[inline]
    pub(crate) fn border_bottom_width(self) -> CssPixels {
        self.border_width_unless_non_devolvable(self.physical_border(self.physical(geometry::Side::Bottom)).0)
    }

    #[inline]
    pub(crate) fn border_left_width(self) -> CssPixels {
        self.border_width_unless_non_devolvable(self.physical_border(self.physical(geometry::Side::Left)).0)
    }

    pub(crate) fn with_vertical_align_keyword(mut self, keyword: u8) -> Self {
        self.vertical_align_override = Some(keyword);
        self
    }

    pub(crate) fn vertical_align_is_keyword(self) -> bool {
        self.vertical_align_override.is_some() || self.style.box_values().vertical_align.is_keyword
    }

    pub(crate) fn vertical_align_keyword(self) -> u8 {
        self.vertical_align_override
            .unwrap_or_else(|| self.style.box_values().vertical_align.keyword)
    }
}

/// `size`, or `initial` when `size` holds an anchor function that is unresolvable and has no fallback value.
/// https://drafts.csswg.org/css-anchor-position-1/#anchor-size-resolution
/// If no fallback value is specified, it makes the declaration referencing it invalid at computed-value time.
/// NB: Outside of the resolutions the abspos engine installs, an anchor function is unresolvable.
fn unresolvable_anchor_size_or<'a>(size: &'a ComputedSize, initial: &'static ComputedSize) -> &'a ComputedSize {
    if !size.contains_anchor_function() {
        return size;
    }
    let mut percentage_basis = CssPixels::default();
    // SAFETY: The callback context is the percentage basis, live for this synchronous resolution.
    let resolved = unsafe {
        resolve_calc_with_external_resolutions(
            size.length_percentage().calculated_pointer(),
            percentage_basis,
            (&raw mut percentage_basis).cast(),
            Some(resolve_unresolvable_anchor_function),
        )
    };
    if resolved.resolved { size } else { initial }
}

/// A px calc node, holding one reference for the external-resolution snapshot that releases it.
pub(crate) fn calc_node_create_px_dimension(value: f64) -> *const c_void {
    crate::css::calc::calc_node_create_numeric_dimension(
        formatting_context::CALC_NUMERIC_KIND_LENGTH,
        value,
        crate::css::style_compute::px_length_unit(),
    )
    .cast()
}

/// The calc node the fallback value of an unresolvable anchor() or anchor-size() function resolves to, or null when
/// it has none. Nested anchor functions resolve through `resolve_non_math_function`.
/// https://drafts.csswg.org/css-anchor-position-1/#anchor-resolution
///
/// # Safety
/// `callback_context` must be what `resolve_non_math_function` expects, and stay live for the resolution.
pub(crate) unsafe fn anchor_function_fallback_node(
    fallback_value: &crate::css::style_value::RetainedStyleValueData,
    percentage_basis: CssPixels,
    callback_context: *mut c_void,
    resolve_non_math_function: unsafe extern "C" fn(*mut c_void, *const c_void) -> *const c_void,
) -> *const c_void {
    use crate::css::style_value::StyleValueData;
    match fallback_value.optional_data() {
        None => std::ptr::null(),
        Some(StyleValueData::Length { value, unit }) => {
            // Computed anchor() fallbacks hold absolute lengths only; relative
            // units carry NaN canonical ratios.
            let ratio = crate::css::style_compute::LENGTH_UNIT_CANONICAL_PX_RATIOS[*unit as usize];
            assert!(ratio.is_finite());
            calc_node_create_px_dimension(CssPixels::nearest_value_for(*value * ratio).to_double())
        }
        Some(StyleValueData::Percentage { value }) => {
            calc_node_create_px_dimension(percentage_basis.to_double() * (value / 100.0))
        }
        Some(StyleValueData::Calculated { .. }) => {
            // SAFETY: The anchor function shell retains the fallback calculation for
            // this synchronous resolution.
            let resolved = unsafe {
                resolve_calc_with_external_resolutions(
                    fallback_value.pointer().cast(),
                    percentage_basis,
                    callback_context,
                    Some(resolve_non_math_function),
                )
            };
            if !resolved.resolved {
                return std::ptr::null();
            }
            calc_node_create_px_dimension(resolved.value)
        }
        // SAFETY: The outer anchor function shell retains the nested one for this synchronous resolution.
        Some(StyleValueData::Anchor { .. } | StyleValueData::AnchorSize { .. }) => unsafe {
            resolve_non_math_function(callback_context, fallback_value.pointer().cast())
        },
        Some(_) => {
            unreachable!("anchor function fallback must be a length, percentage, calculation or anchor function")
        }
    }
}

/// Resolves an anchor() or anchor-size() function outside the abspos engine, where it is unresolvable and resolves
/// to its fallback value. The callback context is the percentage basis.
unsafe extern "C" fn resolve_unresolvable_anchor_function(context: *mut c_void, shell: *const c_void) -> *const c_void {
    use crate::css::style_value::StyleValueData;
    // SAFETY: The context is the percentage basis resolve_calc_with_unresolvable_anchor_functions passes.
    let percentage_basis = unsafe { *context.cast::<CssPixels>() };
    // SAFETY: `shell` is the live style value the calc core's non-math-function node retains.
    match unsafe { &*shell.cast::<StyleValueData>() }.anchor_name_and_fallback() {
        Some((_, _, fallback_value)) => unsafe {
            anchor_function_fallback_node(
                fallback_value,
                percentage_basis,
                context,
                resolve_unresolvable_anchor_function,
            )
        },
        None => std::ptr::null(),
    }
}

/// Resolves `calculated`, whose anchor functions are unresolvable, to px. A function without a fallback value makes
/// the declaration invalid at computed-value time, which this approximates as zero.
pub(crate) fn resolve_calc_with_unresolvable_anchor_functions(
    calculated: *const c_void,
    mut percentage_basis: CssPixels,
) -> f64 {
    // SAFETY: The callback context is the percentage basis, live for this synchronous resolution.
    let resolved = unsafe {
        resolve_calc_with_external_resolutions(
            calculated,
            percentage_basis,
            (&raw mut percentage_basis).cast(),
            Some(resolve_unresolvable_anchor_function),
        )
    };
    if resolved.resolved { resolved.value } else { 0.0 }
}

pub(crate) unsafe fn resolve_calc_with_external_resolutions(
    calculated: *const c_void,
    percentage_basis: CssPixels,
    callback_context: *mut c_void,
    resolve_non_math_function: Option<unsafe extern "C" fn(*mut c_void, *const c_void) -> *const c_void>,
) -> crate::css::calc::FfiResolvedCalc {
    use crate::css::calc::{
        CalcExternalResolutionKind, CalcNode, calc_root_from_calculated, collect_external_resolutions,
        resolve_calculated,
    };

    let context = px_calc_resolution_context(percentage_basis);
    let root = unsafe { &*calc_root_from_calculated(calculated) };
    let mut externals = Vec::new();
    collect_external_resolutions(root, &mut externals);
    if let Some(resolve_non_math_function) = resolve_non_math_function {
        for resolution in &mut externals {
            if resolution.kind == CalcExternalResolutionKind::NonMathFunction {
                let node = unsafe { resolve_non_math_function(callback_context, resolution.source.pointer().cast()) }
                    .cast::<CalcNode>();
                // SAFETY: The resolver hands back one reference to each node it resolves.
                resolution.resolved_node = (!node.is_null()).then(|| unsafe { std::sync::Arc::from_raw(node) });
            }
        }
    }
    resolve_calculated(
        unsafe { &*calculated.cast::<crate::css::style_value::StyleValueData>() },
        &context,
        &externals,
        true,
    )
}