Skip to content

Truly independent web browser

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

use crate::css::css_pixels::CssPixels;
use crate::layout::node_data::NodeSlotId;
use crate::layout::used_values;
use crate::painting::display_list::commands::{ContextRef, SpatialNodeIndex};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum PaintableFlag {
    HasNonInvertibleCssTransform = 1 << 0,
    HorizontalScrollbarHidden = 1 << 1,
    VerticalScrollbarHidden = 1 << 2,
    InLaterContainingBlockFragment = 1 << 3,
    HasLaterBoxFragments = 1 << 4,
    /// An SVG mask or clip path of the box covers no area, which leaves nothing of the box.
    HiddenByEmptySvgMaskOrClip = 1 << 5,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiPixelBox {
    pub top: CssPixels,
    pub right: CssPixels,
    pub bottom: CssPixels,
    pub left: CssPixels,
}

impl From<crate::layout::geometry::Sides> for FfiPixelBox {
    fn from(sides: crate::layout::geometry::Sides) -> Self {
        Self {
            top: sides.top,
            right: sides.right,
            bottom: sides.bottom,
            left: sides.left,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiOverflowData {
    pub rect: used_values::FfiCssPixelRect,
    pub has_scrollable_overflow: bool,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct PaintableData {
    pub containing_block: NodeSlotId,
    pub selection_state: u8,
    pub slot_generation: u8,
    pub flags: u32,

    pub establishes_stacking_context: bool,

    pub own_scroll_node_index: SpatialNodeIndex,
    pub node_identity: i64,
    pub has_accumulated_visual_context: bool,
    pub accumulated_visual_context: ContextRef,
    pub accumulated_visual_context_for_descendants: ContextRef,
}

impl Default for PaintableData {
    fn default() -> Self {
        Self {
            containing_block: NodeSlotId::INVALID,
            selection_state: 0,
            slot_generation: 0,
            flags: 0,
            establishes_stacking_context: false,
            own_scroll_node_index: SpatialNodeIndex::default(),
            node_identity: 0,
            has_accumulated_visual_context: false,
            accumulated_visual_context: ContextRef::default(),
            accumulated_visual_context_for_descendants: ContextRef::default(),
        }
    }
}

impl PaintableData {
    pub fn has_flag(&self, flag: PaintableFlag) -> bool {
        self.flags & flag as u32 != 0
    }

    pub fn set_flag(&mut self, flag: PaintableFlag, value: bool) {
        if value {
            self.flags |= flag as u32;
        } else {
            self.flags &= !(flag as u32);
        }
    }
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiSelectionEntry {
    pub is_text_node_entry: bool,
    // Text entries name the DOM text's primary layout node; Rust expands its slices.
    pub layout_node: NodeSlotId,
    pub state: u8,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiSearchTextRange {
    pub first_entry: usize,
    pub entry_count: usize,
    pub start_offset: usize,
    pub end_offset: usize,
    pub is_current: bool,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiSpellingError {
    /// The DOM text's primary layout node; Rust expands its slices.
    pub layout_node: NodeSlotId,
    pub start_offset: usize,
    pub end_offset: usize,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiCustomHighlightTextEntry {
    pub layer: u32,
    pub layout_node: NodeSlotId,
    pub start_offset: usize,
    pub end_offset: usize,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiCustomHighlightReplacedEntry {
    pub layer: u32,
    pub layout_node: NodeSlotId,
}

pub const SELECTION_STATE_NONE: u8 = 0;
pub const SELECTION_STATE_START: u8 = 1;
pub const SELECTION_STATE_END: u8 = 2;
pub const SELECTION_STATE_START_AND_END: u8 = 3;
pub const SELECTION_STATE_FULL: u8 = 4;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum PaintableRowResetKind {
    Recommitted = 0,
    Cleared = 1,
    Freed = 2,
    /// The viewport's row, which the document is bound to, was recommitted.
    ViewportRecommitted = 3,
}

pub use crate::layout::inline_content::{FragmentRecord, GlyphRunRecord, InlineBoxPieceRecord};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BorderEdge {
    Top,
    Right,
    Bottom,
    Left,
}

impl BorderEdge {
    pub const ALL: [BorderEdge; 4] = [BorderEdge::Top, BorderEdge::Right, BorderEdge::Bottom, BorderEdge::Left];
}

pub const PIECE_EDGE_TOP: u8 = 1 << 0;
pub const PIECE_EDGE_RIGHT: u8 = 1 << 1;
pub const PIECE_EDGE_BOTTOM: u8 = 1 << 2;
pub const PIECE_EDGE_LEFT: u8 = 1 << 3;

impl InlineBoxPieceRecord {
    pub fn shrunken_by_present_edges(
        &self,
        rect: crate::css::css_pixels::CssPixelRect,
        widths: FfiPixelBox,
    ) -> crate::css::css_pixels::CssPixelRect {
        let zero = CssPixels::from_raw(0);
        let has = |edge: u8| self.present_edges & edge != 0;
        rect.shrunken(
            if has(PIECE_EDGE_TOP) { widths.top } else { zero },
            if has(PIECE_EDGE_RIGHT) { widths.right } else { zero },
            if has(PIECE_EDGE_BOTTOM) { widths.bottom } else { zero },
            if has(PIECE_EDGE_LEFT) { widths.left } else { zero },
        )
    }
}

#[derive(Clone, Default)]
pub struct PaintableSideData {
    // Invalidation also runs while paint geometry is borrowed. Keep this
    // mutable cache state out of the plain-data row shared with C++.
    pub(crate) overflow_style: Option<crate::painting::scrollable_overflow::OverflowStyle>,
    // The filter a recommit cleared, kept so the next assignment can tell whether the box
    // paints a different selection of fragments than before.
    pub(crate) fragment_ownership_before_recommit:
        Option<std::sync::Arc<crate::painting::fragment_ownership::FragmentOwnershipFilter>>,
}

#[derive(Clone)]
pub(crate) struct CommittedBoxFragment {
    pub(crate) link: crate::layout::fragment_tree::FragmentLink,
    pub(crate) containing_block_fragment: u32,
}

/// What a row committed beside its geometry that painting reads with it: its inline content and what paint
/// preparation adds for the recording. Shared records are held by `Arc`, so copying a row
/// copies reference counts.
#[derive(Clone, Default)]
pub(crate) struct CommittedSideData {
    pub(crate) inline_content: Option<std::sync::Arc<crate::layout::inline_content::InlineContent>>,
    /// The bounds of the SVG filter the box references, as the visual context update resolved
    /// them.
    pub(crate) svg_filter_bounds: Option<used_values::FfiCssPixelRect>,
    // Only meaningful while is_self_painting(); assigned by the containing block's
    // assign_fragment_ownership().
    pub(crate) fragment_ownership: Option<std::sync::Arc<crate::painting::fragment_ownership::FragmentOwnershipFilter>>,
    /// The paint-order decisions paint preparation gathered for the row, if it gathered them.
    pub(crate) order_inputs: crate::painting::paint_order_plan::PaintOrderInputs,
    pub(crate) containing_block_fragment: u32,
    pub(crate) box_fragments: Option<std::sync::Arc<Vec<CommittedBoxFragment>>>,
    pub(crate) hit_test_bounds_of_lines: Option<std::sync::Arc<[crate::painting::ink_overflow::HitTestBoundOfLine]>>,
}

/// Rows are the same when they hold equal records; a shared record is compared only when the rows
/// hold different allocations of it.
impl PartialEq for CommittedSideData {
    fn eq(&self, other: &Self) -> bool {
        use crate::cow_column::same_payload;
        self.svg_filter_bounds == other.svg_filter_bounds
            && self.order_inputs == other.order_inputs
            && same_payload(
                self.fragment_ownership.as_ref(),
                other.fragment_ownership.as_ref(),
                |a, b| a == b,
            )
            && same_payload(self.inline_content.as_ref(), other.inline_content.as_ref(), |a, b| {
                a == b
            })
            && self.containing_block_fragment == other.containing_block_fragment
            && same_payload(
                self.hit_test_bounds_of_lines.as_ref(),
                other.hit_test_bounds_of_lines.as_ref(),
                |a, b| a == b,
            )
            && same_payload(self.box_fragments.as_ref(), other.box_fragments.as_ref(), |a, b| {
                a.len() == b.len()
                    && a.iter().zip(b.iter()).all(|(left, right)| {
                        left.containing_block_fragment == right.containing_block_fragment
                            && left.link.places_same_fragment_identically_to(&right.link)
                    })
            })
    }
}

impl CommittedSideData {
    pub(crate) fn prepared_order_inputs(&self) -> Option<crate::painting::paint_order_plan::PaintOrderInputs> {
        self.order_inputs.is_initialized().then_some(self.order_inputs)
    }

    pub(crate) fn box_fragments(&self) -> &[CommittedBoxFragment] {
        self.box_fragments.as_deref().map_or(&[], Vec::as_slice)
    }

    pub(crate) fn lines(&self) -> &[crate::layout::inline_content::LineRecord] {
        self.inline_content.as_ref().map_or(&[], |content| &content.lines)
    }
    pub(crate) fn fragments(&self) -> &[FragmentRecord] {
        self.inline_content.as_ref().map_or(&[], |content| &content.fragments)
    }
    pub(crate) fn glyph_ink_of_lines(&self) -> &[crate::css::css_pixels::CssPixelRect] {
        self.inline_content
            .as_ref()
            .map_or(&[], |content| content.glyph_ink_of_lines())
    }
    /// For a line root, the indices of the fragments of `node` among its fragments, in fragment order.
    pub(crate) fn fragment_indices_of(&self, node: NodeSlotId) -> &[u32] {
        self.inline_content
            .as_ref()
            .map_or(&[], |content| content.fragment_indices_of(node))
    }
    pub(crate) fn fragmentless_break_lines_of(&self, node: NodeSlotId) -> &[u32] {
        self.inline_content
            .as_ref()
            .map_or(&[], |content| content.fragmentless_break_lines_of(node))
    }
    pub(crate) fn inline_box_pieces(&self) -> &[InlineBoxPieceRecord] {
        self.inline_content
            .as_ref()
            .map_or(&[], |content| &content.inline_box_pieces)
    }
    /// For a line root, the indices of the pieces of the inline box `node` among its inline box pieces, in line order.
    pub(crate) fn inline_box_piece_indices(&self, node: NodeSlotId) -> &[u32] {
        self.inline_content
            .as_ref()
            .map_or(&[], |content| content.inline_boxes.piece_indices_of(node))
    }
}