Skip to content

Truly independent web browser

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

//! The reads the paint side makes of a document.
//!
//! [`GeometryRead`] names what a box's geometry is computed from: its committed rows and the few
//! facts of its layout node that say how to read them. [`PaintRead`] adds the layout tree shape
//! and style reads painting makes. Paint order, node painting and the paintable geometry helpers
//! are written against these traits rather than against the live [`LayoutNodeArena`], so the
//! types say which reads painting depends on.

use crate::css::computed_value_views::ComputedValuesView;
use crate::css::css_pixels::CssPixelRect;
use crate::css::style::tree::StyleNodeID;
use crate::layout::LayoutNodeArena;
use crate::layout::fragment_tree::FragmentLink;
use crate::layout::node_data::{CompositorAnimationFrameKind, DomPaintFact, NodeFlag, NodeKind, NodeSlotId, PaintNode};
use crate::layout::node_facts::{self, NodeShape};
use crate::layout::text_chunker::GraphemeSegmenter;
use crate::layout::{RenderedText, RenderedTextBoundary, TextFragments};
use crate::painting::host::FfiLayerImageList;
use crate::painting::layer_image_paint_facts::{LayerImagePaintFacts, layer_image_paint_facts_in};
use crate::painting::paint_order_plan::PaintOrderInputs;
use crate::painting::paintable_data::{CommittedSideData, PaintableData};
use crate::painting::published_frame::{PublishedFrame, PublishedRows};
use crate::painting::record::change_ticks::PaintChangeStamps;
use crate::painting::record::recorder_state::AbsoluteRectMemo;
use crate::painting::replaced_paint_facts::ReplacedPaintFacts;
use crate::painting::stacking_context::entries::StackingContextEntries;
use crate::painting::svg_paint_resources::{
    PublishedSvgFilter, PublishedSvgPaintServer, SvgPaintResourceKind, published_filter_in, published_paint_server_in,
};
use crate::painting::visual_context::BoxVisualContextNodeHandles;
use std::cell::RefCell;
use std::ops::Deref;
use std::sync::Arc;

/// What a reader knows of a node of the layout tree that is live. Holding the row says so, so none
/// of its reads checks again.
pub(crate) trait PaintRow<'a>: NodeShape + Copy {
    fn generated_for(self) -> u8;
    fn dom_paint_facts(self) -> u8;
    fn compositor_animation_frame_kinds(self) -> u8;
    fn parent(self) -> NodeSlotId;
    fn first_child(self) -> NodeSlotId;
    fn next_sibling(self) -> NodeSlotId;
    fn style(self) -> Option<ComputedValuesView<'a>>;
    /// The node whose style the row carries.
    fn style_node(self) -> Option<StyleNodeID>;

    fn is_out_of_flow(self) -> bool {
        node_facts::node_is_out_of_flow(&self, self.style())
    }

    fn is_atomic_inline(self) -> bool {
        node_facts::node_is_atomic_inline(&self, self.style())
    }

    fn is_positioned(self) -> bool {
        node_facts::node_is_positioned(&self, self.style())
    }

    fn is_floating(self) -> bool {
        node_facts::node_is_floating(&self, self.style())
    }

    fn is_fragmented_inline(self) -> bool {
        node_facts::node_is_fragmented_inline(&self, self.style())
    }

    /// Whether the row was built for a DOM node: an element, a text node or the document.
    /// Anonymous boxes and generated content were not.
    fn is_dom_backed(self) -> bool {
        // A slot that has not been given a kind yet stands for nothing at all, and its flags do
        // not say so.
        self.kind() != NodeKind::Unset && !node_facts::has_flag(&self, NodeFlag::Anonymous)
    }
}

impl<'a> PaintRow<'a> for &'a PaintNode {
    fn generated_for(self) -> u8 {
        self.generated_for
    }

    fn dom_paint_facts(self) -> u8 {
        self.dom_paint_facts
    }

    fn compositor_animation_frame_kinds(self) -> u8 {
        self.compositor_animation_frame_kinds
    }

    fn parent(self) -> NodeSlotId {
        self.parent
    }

    fn first_child(self) -> NodeSlotId {
        self.first_child
    }

    fn next_sibling(self) -> NodeSlotId {
        self.next_sibling
    }

    fn style(self) -> Option<ComputedValuesView<'a>> {
        PaintNode::style(self)
    }

    fn style_node(self) -> Option<StyleNodeID> {
        self.style_node
    }
}

/// A node's link to another, which is none for an invalid slot.
fn link(link: NodeSlotId) -> Option<NodeSlotId> {
    (!link.is_invalid()).then_some(link)
}

pub(crate) trait GeometryRead: Sized {
    fn ink_overflow_stamp(&self) -> u64;
    fn hit_test_items_stamp(&self) -> u64;
    fn stored_ink_overflow(&self, id: NodeSlotId) -> crate::painting::ink_overflow::InkOverflow;

    fn absolute_ink_overflow_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        let ink = self.stored_ink_overflow(id);
        (!ink.unbounded).then(|| {
            ink.rect.translated_by(
                crate::painting::paintable_geometry::first_fragment_padding_box_rect(self, id).location(),
            )
        })
    }

    fn paintable_data(&self, id: NodeSlotId) -> &PaintableData;
    fn paintable_row_is_populated(&self, id: NodeSlotId) -> bool;
    /// Reads the fragment link a populated row committed.
    fn with_committed_fragment_link<R>(&self, id: NodeSlotId, read: impl FnOnce(Option<&FragmentLink>) -> R) -> R;
    /// The side data a populated row committed.
    fn committed_side_data(&self, id: NodeSlotId) -> impl Deref<Target = CommittedSideData> + '_;

    type Row<'a>: PaintRow<'a>
    where
        Self: 'a;
    /// The row of the node `id` names, or `None` once the node is gone.
    fn node(&self, id: NodeSlotId) -> Option<Self::Row<'_>>;

    fn node_kind_if_live(&self, id: NodeSlotId) -> Option<NodeKind> {
        Some(self.node(id)?.kind())
    }

    fn node_flags_if_live(&self, id: NodeSlotId) -> u32 {
        self.node(id).map_or(0, |node| node.flags())
    }

    fn node_parent_if_live(&self, id: NodeSlotId) -> Option<NodeSlotId> {
        link(self.node(id)?.parent())
    }

    /// The absolute rect memoized for a box, if any.
    fn memoized_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect>;
    fn memoize_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect);

    /// The line root whose side data holds an inline box's pieces.
    fn inline_pieces_root(&self, inline_paintable: NodeSlotId) -> Option<NodeSlotId> {
        if !self.paintable_row_is_populated(inline_paintable) {
            return None;
        }
        let root = self.paintable_data(inline_paintable).containing_block;
        (self.paintable_row_is_populated(root) && crate::painting::node_painting::has_lines(self, root)).then_some(root)
    }
}

/// The reads the display list recording makes of a document beyond a box's geometry: the shape of
/// the layout tree and the style of its nodes.
pub(crate) trait PaintRead: GeometryRead {
    fn slot_is_live(&self, id: NodeSlotId) -> bool {
        self.node(id).is_some()
    }

    fn node_first_child_if_live(&self, id: NodeSlotId) -> Option<NodeSlotId> {
        link(self.node(id)?.first_child())
    }

    fn node_next_sibling_if_live(&self, id: NodeSlotId) -> Option<NodeSlotId> {
        link(self.node(id)?.next_sibling())
    }

    /// The children of the node `id` names, in tree order.
    fn children(&self, id: NodeSlotId) -> impl Iterator<Item = NodeSlotId> + '_ {
        std::iter::successors(self.node_first_child_if_live(id), |&child| {
            self.node_next_sibling_if_live(child)
        })
    }

    fn node_generated_for(&self, id: NodeSlotId) -> u8 {
        self.node(id).map_or(0, |node| node.generated_for())
    }

    fn node_is_generated_for_pseudo_element(&self, id: NodeSlotId) -> bool {
        self.node_generated_for(id) != 0
    }

    fn node_has_compositor_animation_frame(&self, id: NodeSlotId, kind: CompositorAnimationFrameKind) -> bool {
        self.node(id)
            .is_some_and(|node| node.compositor_animation_frame_kinds() & kind as u8 != 0)
    }

    fn node_style_if_live(&self, id: NodeSlotId) -> Option<ComputedValuesView<'_>> {
        self.node(id)?.style()
    }

    /// The rendered text of a text row.
    fn rendered_text(&self, id: NodeSlotId) -> Option<&RenderedText>;
    /// Reads a text row's grapheme boundaries, for a text row with rendered text.
    fn with_grapheme_segmenter<R>(&self, id: NodeSlotId, read: impl FnOnce(&GraphemeSegmenter) -> R) -> Option<R>;
    fn published_svg_filter(&self, slot: NodeSlotId, kind: SvgPaintResourceKind) -> Option<Arc<PublishedSvgFilter>>;
    fn published_svg_paint_server(
        &self,
        slot: NodeSlotId,
        kind: SvgPaintResourceKind,
    ) -> Option<Arc<PublishedSvgPaintServer>>;

    fn node_has_dom_paint_fact(&self, id: NodeSlotId, fact: DomPaintFact) -> bool {
        self.node(id)
            .is_some_and(|node| node.dom_paint_facts() & fact as u8 != 0)
    }

    /// The rows a text node is painted in: its first-letter row, if any, then its own.
    fn text_fragments(&self, primary: NodeSlotId) -> TextFragments;
    /// Whether a text row is the slice in front of a writing suggestion, whose end is where the caret is.
    fn text_precedes_writing_suggestion(&self, id: NodeSlotId) -> bool;
    fn replaced_paint_facts(&self, id: NodeSlotId) -> Option<ReplacedPaintFacts>;
    fn layer_image_paint_facts(
        &self,
        id: NodeSlotId,
        list: FfiLayerImageList,
        computed_index: u32,
    ) -> Option<LayerImagePaintFacts>;
    fn with_paintable_visual_context_node_handles<R>(
        &self,
        id: NodeSlotId,
        read: impl FnOnce(&BoxVisualContextNodeHandles) -> R,
    ) -> R;
    /// The paint-order inputs paint preparation gathered for the row, if it gathered them.
    fn prepared_paint_order_inputs(&self, row: NodeSlotId) -> Option<PaintOrderInputs> {
        self.committed_side_data(row).prepared_order_inputs()
    }
    fn stacking_context_entries(&self, root: NodeSlotId) -> Option<impl Deref<Target = StackingContextEntries> + '_>;

    /// The box whose content box supplies a node's layout coordinate space. An in-flow node uses the nearest ancestor
    /// that forms a containing block for its children. An absolutely positioned node uses its positioning containing
    /// block, or that inline's in-flow containing block when the positioning containing block is inline. A fixed-position
    /// node uses the nearest ancestor that establishes a fixed-position containing block, or the root if there isn't one.
    fn node_containing_block_if_live(&self, id: NodeSlotId) -> Option<NodeSlotId> {
        use crate::css::css_enums::positioning;
        let mut row = self.node(id)?;
        let position = if node_facts::kind_is_text(row.kind()) {
            positioning::STATIC
        } else {
            node_facts::node_position(row.style())
        };
        if position != positioning::ABSOLUTE && position != positioning::FIXED {
            let mut ancestor = link(row.parent());
            while let Some(candidate) = ancestor {
                let candidate_row = self.node(candidate)?;
                if node_facts::node_forms_containing_block_for_children(&candidate_row, candidate_row.style()) {
                    return Some(candidate);
                }
                ancestor = link(candidate_row.parent());
            }
            return None;
        }
        let is_fixed_position = position == positioning::FIXED;
        let establishes_containing_block = node_facts::containing_block_establishment_flag(is_fixed_position) as u32;
        let mut current = id;
        while let Some(ancestor) = link(row.parent()) {
            current = ancestor;
            let Some(ancestor_row) = self.node(ancestor) else {
                break;
            };
            if ancestor_row.flags() & establishes_containing_block != 0 {
                if node_facts::kind_is_box(ancestor_row.kind()) {
                    return Some(current);
                }
                if ancestor_row.kind() == NodeKind::InlineNode {
                    return self.node_containing_block_if_live(current);
                }
            }
            row = ancestor_row;
        }
        is_fixed_position.then_some(current)
    }

    fn node_is_dom_backed(&self, id: NodeSlotId) -> bool {
        self.node(id).is_some_and(PaintRow::is_dom_backed)
    }

    /// Whether the row was built for an element, as opposed to the document, a text node or
    /// nothing.
    fn node_is_element_backed(&self, id: NodeSlotId) -> bool {
        self.node(id).is_some_and(|row| {
            row.is_dom_backed() && row.kind() != NodeKind::Viewport && !node_facts::kind_is_text(row.kind())
        })
    }

    fn dom_offset_for_rendered_text_offset(
        &self,
        id: NodeSlotId,
        offset: usize,
        boundary: RenderedTextBoundary,
    ) -> usize {
        if !self.node_kind_if_live(id).is_some_and(node_facts::kind_is_text) {
            return offset;
        }
        self.rendered_text(id)
            .expect("text must be published before mapping rendered offsets")
            .dom_offset_for_rendered_text_offset(offset, boundary)
    }

    fn rendered_text_offset_for_dom_offset(
        &self,
        id: NodeSlotId,
        offset: usize,
        boundary: RenderedTextBoundary,
    ) -> usize {
        if !self.node_kind_if_live(id).is_some_and(node_facts::kind_is_text) {
            return offset;
        }
        self.rendered_text(id)
            .expect("text must be published before mapping DOM offsets")
            .rendered_text_offset_for_dom_offset(offset, boundary)
    }

    /// Visits the layout subtree under `root` in pre-order, descending into a node's children only
    /// when the visit asks to.
    fn for_each_node_in_layout_subtree_in_pre_order_with_pruning(
        &self,
        root: NodeSlotId,
        mut visit_node_and_report_whether_to_descend: impl FnMut(NodeSlotId) -> bool,
    ) {
        let mut current = root;
        'visit: loop {
            let descend_into_children = visit_node_and_report_whether_to_descend(current);
            let Some(mut row) = self.node(current) else {
                return;
            };
            if descend_into_children && let Some(first_child) = link(row.first_child()) {
                current = first_child;
                continue;
            }
            // Climb to the node after the subtree of `current`, staying under the root.
            while current != root {
                if let Some(next_sibling) = link(row.next_sibling()) {
                    current = next_sibling;
                    continue 'visit;
                }
                let Some(parent) = link(row.parent()).and_then(|parent| Some((parent, self.node(parent)?))) else {
                    debug_assert!(false, "a node under the walked root has no parent");
                    return;
                };
                (current, row) = parent;
            }
            return;
        }
    }
}

impl AsRef<LayoutNodeArena> for LayoutNodeArena {
    fn as_ref(&self) -> &LayoutNodeArena {
        self
    }
}

/// Where a live read keeps the absolute rects it computed. A view of the rows keeps its own, each valid for the
/// geometry epoch of the arena it was computed at; the bare arena keeps none, as a shared borrow of it writes nothing.
pub(crate) trait LiveAbsoluteRects {
    fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect>;
    fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect);
}

impl LiveAbsoluteRects for LayoutNodeArena {
    fn memoized_live_absolute_rect(&self, _: NodeSlotId) -> Option<CssPixelRect> {
        None
    }

    fn memoize_live_absolute_rect(&self, _: NodeSlotId, _: CssPixelRect) {}
}

impl<Arena: Deref<Target = LayoutNodeArena>> LiveAbsoluteRects
    for crate::painting::paintable_rows::PaintableRows<Arena>
{
    fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        self.memoized_absolute_rect_of_view(id)
    }

    fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
        self.memoize_absolute_rect_of_view(id, rect);
    }
}

impl<Reader: LiveAbsoluteRects + ?Sized> LiveAbsoluteRects for &Reader {
    fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        (**self).memoized_live_absolute_rect(id)
    }

    fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
        (**self).memoize_live_absolute_rect(id, rect);
    }
}

impl<Reader: LiveAbsoluteRects + ?Sized> LiveAbsoluteRects for &mut Reader {
    fn memoized_live_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        (**self).memoized_live_absolute_rect(id)
    }

    fn memoize_live_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
        (**self).memoize_live_absolute_rect(id, rect);
    }
}

// The live arena, and every view that borrows it, answers each read with the arena's own method of
// the same name.
impl<Live: AsRef<LayoutNodeArena> + LiveAbsoluteRects> GeometryRead for Live {
    type Row<'a>
        = crate::layout::LiveRow<'a>
    where
        Self: 'a;

    fn ink_overflow_stamp(&self) -> u64 {
        self.as_ref().paintable_rows.ink_overflow_stamp
    }

    fn hit_test_items_stamp(&self) -> u64 {
        self.as_ref().paintable_rows.hit_test_items_stamp
    }

    fn stored_ink_overflow(&self, id: NodeSlotId) -> crate::painting::ink_overflow::InkOverflow {
        self.as_ref()
            .paintable_rows
            .ink_overflow
            .get(id.slot_index() as usize)
            .copied()
            .unwrap_or_default()
    }

    fn node(&self, id: NodeSlotId) -> Option<Self::Row<'_>> {
        self.as_ref().live_row(id)
    }

    fn paintable_data(&self, id: NodeSlotId) -> &PaintableData {
        self.as_ref().live_paintable_data(id)
    }

    fn paintable_row_is_populated(&self, id: NodeSlotId) -> bool {
        self.as_ref().paintable_row_is_populated(id)
    }

    fn with_committed_fragment_link<R>(&self, id: NodeSlotId, read: impl FnOnce(Option<&FragmentLink>) -> R) -> R {
        self.as_ref().with_committed_fragment_link(id, read)
    }

    fn committed_side_data(&self, id: NodeSlotId) -> impl Deref<Target = CommittedSideData> + '_ {
        self.as_ref().committed_side_data(id)
    }

    fn memoized_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        self.memoized_live_absolute_rect(id)
    }

    fn memoize_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
        self.memoize_live_absolute_rect(id, rect);
    }
}

impl<Live: AsRef<LayoutNodeArena> + LiveAbsoluteRects> PaintRead for Live {
    fn rendered_text(&self, id: NodeSlotId) -> Option<&RenderedText> {
        self.as_ref().text_content(id).map(|content| &**content)
    }

    fn with_grapheme_segmenter<R>(&self, id: NodeSlotId, read: impl FnOnce(&GraphemeSegmenter) -> R) -> Option<R> {
        self.as_ref()
            .text_content(id)
            .map(|content| read(&GraphemeSegmenter::new(&content.text)))
    }

    fn text_fragments(&self, primary: NodeSlotId) -> TextFragments {
        self.as_ref().text_fragments(primary)
    }

    fn text_precedes_writing_suggestion(&self, id: NodeSlotId) -> bool {
        self.as_ref().text_precedes_writing_suggestion(id)
    }

    fn published_svg_filter(&self, slot: NodeSlotId, kind: SvgPaintResourceKind) -> Option<Arc<PublishedSvgFilter>> {
        self.as_ref().svg_paint_resources().published_filter(slot, kind)
    }

    fn published_svg_paint_server(
        &self,
        slot: NodeSlotId,
        kind: SvgPaintResourceKind,
    ) -> Option<Arc<PublishedSvgPaintServer>> {
        self.as_ref().svg_paint_resources().published_paint_server(slot, kind)
    }

    fn replaced_paint_facts(&self, id: NodeSlotId) -> Option<ReplacedPaintFacts> {
        self.as_ref().replaced_paint_facts(id)
    }

    fn layer_image_paint_facts(
        &self,
        id: NodeSlotId,
        list: FfiLayerImageList,
        computed_index: u32,
    ) -> Option<LayerImagePaintFacts> {
        self.as_ref().layer_image_paint_facts(id, list, computed_index)
    }

    fn with_paintable_visual_context_node_handles<R>(
        &self,
        id: NodeSlotId,
        read: impl FnOnce(&BoxVisualContextNodeHandles) -> R,
    ) -> R {
        self.as_ref().with_paintable_visual_context_node_handles(id, read)
    }

    fn stacking_context_entries(&self, root: NodeSlotId) -> Option<impl Deref<Target = StackingContextEntries> + '_> {
        self.as_ref().stacking_context_entries(root)
    }
}

/// What the display list recording reads a document through: [`PaintRead`] and nothing else. It
/// has no `Deref` to the arena, so a read the trait does not name does not compile, and it reads
/// everything from the frame the document published when the recording started. The absolute rects
/// it computes go to the recorder's own memo, stamped with the geometry epoch of the frame. The
/// host reads the rows its document's render state published through one too, without a frame.
#[derive(Clone, Copy)]
pub(crate) struct PaintSource<'a> {
    rows: &'a PublishedRows,
    /// The frame a recording reads, or none for the host reading published rows.
    frame: Option<&'a PublishedFrame>,
    absolute_rects: &'a RefCell<AbsoluteRectMemo>,
}

impl<'a> PaintSource<'a> {
    pub(crate) fn new(frame: &'a PublishedFrame, absolute_rects: &'a RefCell<AbsoluteRectMemo>) -> Self {
        Self {
            rows: &frame.rows,
            frame: Some(frame),
            absolute_rects,
        }
    }

    /// Reads `rows` alone, as the host reads the rows its document's render state published.
    pub(crate) fn over_rows(rows: &'a PublishedRows, absolute_rects: &'a RefCell<AbsoluteRectMemo>) -> Self {
        Self {
            rows,
            frame: None,
            absolute_rects,
        }
    }

    /// The frame the recording reads.
    pub(crate) fn frame(&self) -> &'a PublishedFrame {
        self.frame.expect("only a recording reads a frame")
    }

    /// The shape of an `<area>` of the map of the image whose paintable row is `image` (see
    /// [`crate::painting::image_map_areas::area_shape_path`]).
    pub(crate) fn image_map_area_shape_path(
        &self,
        image: NodeSlotId,
        area: u32,
        width: f32,
        height: f32,
    ) -> Option<libgfx_rust::path::Path> {
        crate::painting::image_map_areas::area_shape_path(
            &self.rows().paint_facts.image_maps,
            image,
            area,
            width,
            height,
        )
    }

    fn rows(&self) -> &'a PublishedRows {
        self.rows
    }

    pub(crate) fn paint_change_stamps(&self, row: NodeSlotId) -> PaintChangeStamps {
        self.rows.paint_change_stamps(row)
    }
}

impl GeometryRead for PaintSource<'_> {
    type Row<'a>
        = &'a PaintNode
    where
        Self: 'a;

    fn ink_overflow_stamp(&self) -> u64 {
        self.rows().ink_overflow_stamp
    }

    fn hit_test_items_stamp(&self) -> u64 {
        self.rows().hit_test_items_stamp
    }

    fn stored_ink_overflow(&self, id: NodeSlotId) -> crate::painting::ink_overflow::InkOverflow {
        self.rows()
            .ink_overflow
            .get(id.slot_index() as usize)
            .copied()
            .unwrap_or_default()
    }

    fn node(&self, id: NodeSlotId) -> Option<&PaintNode> {
        self.rows().node(id)
    }

    fn paintable_data(&self, id: NodeSlotId) -> &PaintableData {
        self.rows().paintable_data(id)
    }

    fn paintable_row_is_populated(&self, id: NodeSlotId) -> bool {
        self.rows().paintable_row_is_populated(id)
    }

    fn with_committed_fragment_link<R>(&self, id: NodeSlotId, read: impl FnOnce(Option<&FragmentLink>) -> R) -> R {
        self.rows().with_committed_fragment_link(id, read)
    }

    fn committed_side_data(&self, id: NodeSlotId) -> impl Deref<Target = CommittedSideData> + '_ {
        self.rows().committed_side_data(id)
    }

    fn memoized_absolute_rect(&self, id: NodeSlotId) -> Option<CssPixelRect> {
        self.absolute_rects.borrow().get(id, self.rows().geometry_epoch)
    }

    fn memoize_absolute_rect(&self, id: NodeSlotId, rect: CssPixelRect) {
        self.absolute_rects
            .borrow_mut()
            .set(id, self.rows().geometry_epoch, rect);
    }
}

impl PaintRead for PaintSource<'_> {
    fn with_paintable_visual_context_node_handles<R>(
        &self,
        id: NodeSlotId,
        read: impl FnOnce(&BoxVisualContextNodeHandles) -> R,
    ) -> R {
        read(self.rows().visual_context_node_handles(id))
    }

    fn stacking_context_entries(&self, root: NodeSlotId) -> Option<impl Deref<Target = StackingContextEntries> + '_> {
        self.rows().stacking_context_entries(root)
    }

    fn rendered_text(&self, id: NodeSlotId) -> Option<&RenderedText> {
        self.rows().text(id)?.rendered.as_deref()
    }

    fn with_grapheme_segmenter<R>(&self, id: NodeSlotId, read: impl FnOnce(&GraphemeSegmenter) -> R) -> Option<R> {
        self.rendered_text(id)
            .map(|rendered| read(&GraphemeSegmenter::new(&rendered.text)))
    }

    fn text_fragments(&self, primary: NodeSlotId) -> TextFragments {
        let mut fragments = TextFragments {
            nodes: [NodeSlotId::INVALID; 2],
            length: 0,
        };
        if !self.node_kind_if_live(primary).is_some_and(node_facts::kind_is_text) {
            return fragments;
        }
        if let Some(text) = self.rows().text(primary)
            && self.slot_is_live(text.first_letter)
        {
            fragments.nodes[0] = text.first_letter;
            fragments.length = 1;
        }
        fragments.nodes[fragments.length] = primary;
        fragments.length += 1;
        fragments
    }

    fn text_precedes_writing_suggestion(&self, id: NodeSlotId) -> bool {
        self.rows()
            .text(id)
            .is_some_and(|text| text.precedes_writing_suggestion)
    }

    fn published_svg_filter(&self, slot: NodeSlotId, kind: SvgPaintResourceKind) -> Option<Arc<PublishedSvgFilter>> {
        published_filter_in(&self.rows().paint_facts.svg_paint_resources, slot, kind)
    }

    fn published_svg_paint_server(
        &self,
        slot: NodeSlotId,
        kind: SvgPaintResourceKind,
    ) -> Option<Arc<PublishedSvgPaintServer>> {
        published_paint_server_in(&self.rows().paint_facts.svg_paint_resources, slot, kind)
    }

    fn replaced_paint_facts(&self, id: NodeSlotId) -> Option<ReplacedPaintFacts> {
        self.rows().paint_facts.replaced.get(&id).cloned()
    }

    fn layer_image_paint_facts(
        &self,
        id: NodeSlotId,
        list: FfiLayerImageList,
        computed_index: u32,
    ) -> Option<LayerImagePaintFacts> {
        layer_image_paint_facts_in(&self.rows().paint_facts.layer_images, id, list, computed_index)
    }
}