Skip to content

Truly independent web browser

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

use crate::layout::svg_formatting_context::FfiSvgNumberPercentage;
use crate::layout::used_values;
use crate::painting::display_list::commands::{OptionalAffineTransform, OptionalColor};
use crate::stage::MainThread;
use libcompositing_rust::scrolling::scroll_snap_selection::SnapSelectionStrategyType;
use libgfx_rust::{Color, IntRect, InterpolationColorSpace};
use std::ffi::c_void;

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiRecordingInputs {
    pub device_viewport_rect: IntRect,
    pub css_viewport_rect: used_values::FfiCssPixelRect,
    pub should_show_line_box_borders: bool,
    pub force_dark_enabled: bool,
    pub force_dark_foreground_threshold: i32,
    pub force_dark_background_threshold: i32,
    pub should_paint_overlay: bool,
    pub records_in_finest_runs: bool,
    pub records_whole_document: bool,
    pub notes_record_origins: bool,
    pub has_blocking_wheel_event_region_covering_viewport: bool,
    pub has_navigable_container_with_composited_context: bool,
    pub wheel_event_listener_state_generation: u64,
    pub chrome_metrics: crate::painting::ffi::FfiChromeMetrics,
    pub paint_viewport_scrollbars: bool,
    pub middle_button_scroll_active: bool,
    pub middle_button_scroll_origin: used_values::FfiCssPixelPoint,
    pub canvas_fill_rect: used_values::OptionalIntRect,
    pub canvas_color: Color,
    pub opaque_canvas: bool,
    pub bitmap_rect: IntRect,
    pub background_color: Color,
    pub publishes_recording: bool,
    pub window_is_focused: bool,
    pub outline_auto_color: Color,
    pub selection_background_from_palette: Color,
    pub selection_background_light: Color,
    pub selection_background_dark: Color,
    pub inactive_selection_background_from_palette: Color,
    pub inactive_selection_background_light: Color,
    pub inactive_selection_background_dark: Color,
    pub palette_is_dark: bool,
    pub document_supports_light_or_dark: bool,
    pub image_fallback_color_scheme: u8,
    pub records_without_visual_contexts: bool,
    pub accessibility_focus_target: crate::layout::node_data::NodeSlotId,
    pub has_inspector_highlight: bool,
    pub inspector_highlight_paintable: crate::layout::node_data::NodeSlotId,
    pub tooltip_color: Color,
    pub tooltip_text_color: Color,
    pub tooltip_border_color: Color,
    pub grid_overlays: *const FfiGridOverlayInput,
    pub grid_overlay_count: usize,
    pub flex_overlays: *const FfiFlexOverlayInput,
    pub flex_overlay_count: usize,
    pub caret_debug_rect: used_values::OptionalCssPixelRect,
    // Per-document facts the host resolves once per recording.
    pub caret: FfiCaretPaint,
    pub focused_text_control: FfiFocusedTextControlSelection,
    pub focused_area_outline: FfiFocusedAreaOutline,
    pub inspector_highlight_label: FfiInspectorHighlightLabel,
    pub grid_label_fonts: FfiOverlayLabelFonts,
    pub text_fragment_indications: *const FfiTextFragmentIndicationRange,
    pub text_fragment_indication_count: usize,
    pub mark_color: Color,
    pub mark_text_color: Color,
    /// What the compositor clears the surface with below the recording, for a top-level traversable.
    pub surface_clear_color: OptionalColor,
    pub has_async_scrolling_metadata: bool,
    pub async_scrolling_metadata: FfiAsyncScrollingMetadata,
}

/// What a recording's display list is stamped with for async scrolling, as the host knows it where the recording begins:
/// `Compositing::AsyncScrollingMetadata` but for the keyboard scroll state, which each frame sets.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiAsyncScrollingMetadata {
    pub has_document_id: bool,
    pub document_id: i64,
    pub viewport_rect: IntRect,
    pub wheel_event_listener_state_generation: u64,
    pub has_blocking_wheel_event_listeners: bool,
    pub has_blocking_wheel_event_region_covering_viewport: bool,
    pub viewport_keeps_horizontal_scrolls: bool,
    pub viewport_keeps_vertical_scrolls: bool,
    pub device_pixels_per_css_pixel: f64,
}

impl FfiAsyncScrollingMetadata {
    fn to_metadata(self) -> libcompositing_rust::ipc_types::Compositing_AsyncScrollingMetadata {
        libcompositing_rust::ipc_types::Compositing_AsyncScrollingMetadata {
            document_id: self
                .has_document_id
                .then_some(crate::painting::display_list::commands::UniqueNodeId(self.document_id)),
            viewport_rect: self.viewport_rect,
            wheel_event_listener_state_generation: self.wheel_event_listener_state_generation,
            has_blocking_wheel_event_listeners: self.has_blocking_wheel_event_listeners,
            has_blocking_wheel_event_region_covering_viewport: self.has_blocking_wheel_event_region_covering_viewport,
            viewport_keeps_horizontal_scrolls: self.viewport_keeps_horizontal_scrolls,
            viewport_keeps_vertical_scrolls: self.viewport_keeps_vertical_scrolls,
            device_pixels_per_css_pixel: self.device_pixels_per_css_pixel,
            keyboard_scroll_state: crate::painting::presentation::empty_keyboard_scroll_state(),
        }
    }
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiTextFragmentIndicationRange {
    pub text_node: crate::layout::node_data::NodeSlotId,
    pub start: usize,
    pub end: usize,
}

impl FfiRecordingInputs {
    /// The inputs of a recording, but for what the frame it records decides, which copy what they read of the arrays
    /// and byte buffers.
    ///
    /// # Safety
    ///
    /// Nonempty arrays and byte buffers must be aligned, valid and immutable for this call. Fonts
    /// for enabled overlays must point to live `Gfx::Font`s.
    pub(crate) unsafe fn recording_inputs(&self) -> crate::painting::record::inputs::UnframedRecordingInputs {
        use crate::painting::force_dark::ForceDarkSettings;
        use crate::painting::record::inputs::{
            CaretPaint, CaretTarget, FocusedAreaOutline, FocusedTextControlSelection, GridOverlays, InspectorHighlight,
            RecordingInputs, UnframedRecordingInputs,
        };

        // SAFETY: The caller lends these arrays and buffers for this call.
        let (grid_overlays, flex_overlays, text_fragment_indications) = unsafe {
            (
                ak::slice_from_raw_parts(self.grid_overlays, self.grid_overlay_count),
                ak::slice_from_raw_parts(self.flex_overlays, self.flex_overlay_count),
                ak::slice_from_raw_parts(self.text_fragment_indications, self.text_fragment_indication_count),
            )
        };
        let caret = self.caret;
        let caret_target = match caret.kind {
            FfiCaretPaintKind::None => None,
            FfiCaretPaintKind::InBlock => Some(CaretTarget::InBlock {
                block: caret.block,
                owner: (!caret.owner.is_invalid()).then_some(caret.owner),
            }),
            FfiCaretPaintKind::EmptyInline => Some(CaretTarget::EmptyInline(caret.block)),
        };
        let control = self.focused_text_control;
        UnframedRecordingInputs(RecordingInputs {
            device_pixels_per_css_pixel: 0.0,
            uncaptured: crate::painting::record::inputs::UncapturedContentInputs {
                root_background_source: Default::default(),
                device_viewport_size: libgfx_rust::IntSize {
                    width: self.device_viewport_rect.width,
                    height: self.device_viewport_rect.height,
                },
                has_blocking_wheel_event_region_covering_viewport: self
                    .has_blocking_wheel_event_region_covering_viewport,
                has_navigable_container_with_composited_context: self.has_navigable_container_with_composited_context,
                chrome_metrics: self.chrome_metrics,
                paint_viewport_scrollbars: self.paint_viewport_scrollbars,
                middle_button_scroll_origin: self
                    .middle_button_scroll_active
                    .then(|| self.middle_button_scroll_origin.into()),
                canvas_color: self.canvas_color,
                background_color: self.background_color,
            },
            wheel_event_listener_state_generation: self.wheel_event_listener_state_generation,
            css_viewport_rect: self.css_viewport_rect.into(),
            should_show_line_box_borders: self.should_show_line_box_borders,
            force_dark_enabled: self.force_dark_enabled,
            force_dark_settings: ForceDarkSettings {
                foreground_brightness_threshold: self.force_dark_foreground_threshold,
                background_brightness_threshold: self.force_dark_background_threshold,
            },
            should_paint_overlay: self.should_paint_overlay,
            records_in_finest_runs: self.records_in_finest_runs,
            records_whole_document: self.records_whole_document,
            notes_record_origins: self.notes_record_origins,
            canvas_fill_rect: self.canvas_fill_rect.has_value.then_some(self.canvas_fill_rect.value),
            opaque_canvas: self.opaque_canvas,
            bitmap_rect: self.bitmap_rect,
            publishes_recording: self.publishes_recording,
            surface_clear_color: self.surface_clear_color.get(),
            async_scrolling_metadata: self
                .has_async_scrolling_metadata
                .then(|| self.async_scrolling_metadata.to_metadata()),
            window_is_focused: self.window_is_focused,
            outline_auto_color: self.outline_auto_color,
            selection_background_from_palette: self.selection_background_from_palette,
            selection_background_light: self.selection_background_light,
            selection_background_dark: self.selection_background_dark,
            inactive_selection_background_from_palette: self.inactive_selection_background_from_palette,
            inactive_selection_background_light: self.inactive_selection_background_light,
            inactive_selection_background_dark: self.inactive_selection_background_dark,
            palette_is_dark: self.palette_is_dark,
            document_supports_light_or_dark: self.document_supports_light_or_dark,
            image_fallback_color_scheme: self.image_fallback_color_scheme,
            records_without_visual_contexts: self.records_without_visual_contexts,
            accessibility_focus_target: (!self.accessibility_focus_target.is_invalid())
                .then_some(self.accessibility_focus_target),
            inspector_highlight: self.has_inspector_highlight.then(|| {
                // SAFETY: The caller lends the label bytes and supplies live fonts for this overlay.
                let (text, fonts) = unsafe {
                    (
                        ak::slice_from_raw_parts(
                            self.inspector_highlight_label.text,
                            self.inspector_highlight_label.text_byte_count,
                        ),
                        self.inspector_highlight_label.fonts.retain(),
                    )
                };
                InspectorHighlight {
                    paintable: self.inspector_highlight_paintable,
                    label: String::from_utf8_lossy(text).into_owned(),
                    fonts,
                }
            }),
            tooltip_color: self.tooltip_color,
            tooltip_text_color: self.tooltip_text_color,
            tooltip_border_color: self.tooltip_border_color,
            grid_overlays: (!grid_overlays.is_empty()).then(|| GridOverlays {
                inputs: grid_overlays.into(),
                // SAFETY: The caller supplies live label fonts when grid overlays are enabled.
                fonts: unsafe { self.grid_label_fonts.retain() },
            }),
            flex_overlays: flex_overlays.into(),
            caret_debug_rect: self
                .caret_debug_rect
                .has_value
                .then(|| self.caret_debug_rect.value.into()),
            caret: caret_target.map(|target| CaretPaint {
                target,
                rect: caret.rect.into(),
                color: caret.color,
                blink_cycle_start_time_ns: caret.blink_cycle_start_time_ns,
                should_blink: caret.should_blink,
            }),
            focused_text_control: (control.start != control.end).then_some(FocusedTextControlSelection {
                text_node: control.text_node,
                start: control.start,
                end: control.end,
            }),
            focused_area_outline: (self.focused_area_outline.area != 0).then_some(FocusedAreaOutline {
                image: self.focused_area_outline.image,
                area: self.focused_area_outline.area,
            }),
            text_fragment_indications: text_fragment_indications.to_vec().into(),
            mark_color: self.mark_color,
            mark_text_color: self.mark_text_color,
        })
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum FfiCaretPaintKind {
    None,
    /// `block` paints the caret, in the fragment run owned by the self-painting inline `owner`
    /// (`INVALID` when the block itself owns it).
    InBlock,
    /// The empty editable inline `block` paints the caret at its own position.
    EmptyInline,
}

/// Where the document's caret paints, resolved once per recording by the host.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiCaretPaint {
    pub kind: FfiCaretPaintKind,
    pub block: crate::layout::node_data::NodeSlotId,
    pub owner: crate::layout::node_data::NodeSlotId,
    pub rect: used_values::FfiCssPixelRect,
    pub color: Color,
    pub blink_cycle_start_time_ns: i64,
    pub should_blink: bool,
}

/// The focused text control's selection, keyed by its primary layout text node. Equal start
/// and end offsets mean no selection.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiFocusedTextControlSelection {
    pub text_node: crate::layout::node_data::NodeSlotId,
    pub start: usize,
    pub end: usize,
}

/// The focus ring of a focused image-map area, painted by the image whose rendering makes the
/// area's shape focusable, along the shape the image's map gives the area its style-tree identity
/// names. An identity of zero means no focus ring.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiFocusedAreaOutline {
    pub image: crate::layout::node_data::NodeSlotId,
    pub area: u32,
}

/// The platform default font at an overlay label's CSS size and at that size in device pixels.
/// Both are null unless the recording paints the overlay they belong to.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiOverlayLabelFonts {
    pub css_font: *const c_void,
    pub device_font: *const c_void,
}

impl FfiOverlayLabelFonts {
    /// # Safety
    ///
    /// Both pointers must refer to live `Gfx::Font`s.
    unsafe fn retain(self) -> crate::painting::record::inputs::OverlayLabelFonts {
        // SAFETY: The caller supplies live fonts; the handles retain them for recording.
        unsafe {
            crate::painting::record::inputs::OverlayLabelFonts {
                css_font: libgfx_rust::font::FontHandle::intern(self.css_font),
                device_font: libgfx_rust::font::FontHandle::intern(self.device_font),
            }
        }
    }
}

/// The inspector's box-model label for the highlighted node: UTF-8 text live for the recording call.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiInspectorHighlightLabel {
    pub fonts: FfiOverlayLabelFonts,
    pub text: *const u8,
    pub text_byte_count: usize,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiGridOverlayInput {
    pub paintable: crate::layout::node_data::NodeSlotId,
    pub color: Color,
    pub label_foreground_color: Color,
    pub label_css_pixel_size: f32,
    pub show_area_names: bool,
    pub show_line_numbers: bool,
    pub show_track_sizes: bool,
    pub show_infinite_lines: bool,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiFlexOverlayInput {
    pub paintable: crate::layout::node_data::NodeSlotId,
    pub color: Color,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
#[expect(dead_code, reason = "C++ constructs the variants")]
pub enum FfiImageContentKind {
    #[default]
    None,
    Raster,
    Vector,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiNaturalSize {
    pub width: used_values::OptionalCssPixels,
    pub height: used_values::OptionalCssPixels,
    pub has_aspect_ratio: bool,
    pub aspect_ratio_numerator: crate::css::css_pixels::CssPixels,
    pub aspect_ratio_denominator: crate::css::css_pixels::CssPixels,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiImageContent {
    pub kind: FfiImageContentKind,
    pub vector_content_identity: u64,
    pub vector_has_active_view_box: bool,
    pub frame: *const c_void,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiLayerImagePaintFacts {
    pub is_paintable: bool,
    pub natural: FfiNaturalSize,
    pub has_image_set_selected_option: bool,
    pub image_set_selected_option_index: u32,
    pub content: FfiImageContent,
    pub single_pixel_color: OptionalColor,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiLayerImagePaintFactsEntry {
    pub list: FfiLayerImageList,
    pub computed_index: u32,
    pub facts: FfiLayerImagePaintFacts,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
#[expect(dead_code, reason = "C++ constructs the variants")]
pub enum FfiVideoRepresentation {
    #[default]
    VideoFrame,
    PosterFrame,
    TransparentBlack,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiReplacedImagePaintFacts {
    pub natural: FfiNaturalSize,
    pub content: FfiImageContent,
    pub preserve_aspect_ratio_align: u8,
    pub preserve_aspect_ratio_meet_or_slice: u8,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiVideoPaintFacts {
    pub representation: FfiVideoRepresentation,
    pub has_video_frame: bool,
    pub video_src_width: i32,
    pub video_src_height: i32,
    pub video_sink_handle: u64,
    pub poster_frame: *const c_void,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
#[expect(dead_code, reason = "C++ constructs the variants")]
pub enum FfiSvgGradientSpreadMethod {
    #[default]
    Pad,
    Repeat,
    Reflect,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
#[expect(dead_code, reason = "C++ constructs the variants")]
pub enum FfiSvgGradientKind {
    #[default]
    Linear,
    Radial,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct FfiSvgGradientDescription {
    pub kind: FfiSvgGradientKind,
    pub units_are_object_bounding_box: bool,
    pub spread_method: FfiSvgGradientSpreadMethod,
    pub color_space: InterpolationColorSpace,
    pub gradient_transform: OptionalAffineTransform,
    pub x1: FfiSvgNumberPercentage,
    pub y1: FfiSvgNumberPercentage,
    pub x2: FfiSvgNumberPercentage,
    pub y2: FfiSvgNumberPercentage,
    pub cx: FfiSvgNumberPercentage,
    pub cy: FfiSvgNumberPercentage,
    pub r: FfiSvgNumberPercentage,
    pub fx: FfiSvgNumberPercentage,
    pub fy: FfiSvgNumberPercentage,
    pub fr: FfiSvgNumberPercentage,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct FfiSvgPatternDescription {
    pub pattern_box: crate::layout::node_data::NodeSlotId,
    pub units_are_object_bounding_box: bool,
    pub content_units_are_object_bounding_box: bool,
    pub has_view_box: bool,
    pub x: FfiSvgNumberPercentage,
    pub y: FfiSvgNumberPercentage,
    pub width: FfiSvgNumberPercentage,
    pub height: FfiSvgNumberPercentage,
    pub pattern_transform_attribute: OptionalAffineTransform,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct FfiSelectionStyleFacts {
    pub colors_authored: bool,
    pub background_color: Color,
    pub background_color_is_current_color: bool,
    pub text_color: OptionalColor,
    pub wash_color: Color,
    pub has_text_shadow: bool,
    pub has_text_decoration: bool,
    pub text_decoration_lines: [u8; 8],
    pub text_decoration_line_count: u32,
    pub text_decoration_style: u8,
    pub text_decoration_color: Color,
    pub text_decoration_color_is_current_color: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum FfiLayerImageList {
    Background,
    Mask,
    BorderImageSource,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct FfiVectorImageRenderRequest {
    pub owner: crate::layout::node_data::NodeSlotId,
    pub is_replaced_content: bool,
    pub list: FfiLayerImageList,
    pub computed_index: u32,
    pub css_width: crate::css::css_pixels::CssPixels,
    pub css_height: crate::css::css_pixels::CssPixels,
    pub raster_scale: f32,
    pub color_scheme: u8,
    pub force_dark_enabled: bool,
    pub force_dark_foreground_threshold: i32,
    pub force_dark_background_threshold: i32,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiFormControlPaintFacts {
    pub enabled: bool,
    pub checked: bool,
    pub indeterminate: bool,
    pub being_activated: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiCanvasPaintFacts {
    pub has_content: bool,
    pub content_width: i32,
    pub content_height: i32,
    pub canvas_id: u64,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiNavigableContainerPaintFacts {
    pub has_composited_context: bool,
    pub composited_context_id: u64,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiSnapAxes {
    pub x: bool,
    pub y: bool,
}

/// The geometry snap position selection runs over for a snap container, in CSS pixels so that the
/// selection is exact.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiSnapContainerGeometry {
    pub snapport: crate::layout::used_values::FfiCssPixelRect,
    pub min_scroll_offset: crate::layout::used_values::FfiCssPixelPoint,
    pub max_scroll_offset: crate::layout::used_values::FfiCssPixelPoint,
    pub strictness: u8,
    pub axes: FfiSnapAxes,
    pub horizontal_writing_mode: bool,
}

/// A snap area's geometry in its snap container's coordinate space: the transformed border box with
/// the scroll margin added, and the alignment along each physical axis.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiSnapAreaGeometry {
    /// The unique ID of the element the area's box belongs to.
    pub node_id: i64,
    /// 0 for the element's own box; otherwise the pseudo-element the box is generated for, plus one.
    pub pseudo_element_type: u8,
    pub rect: crate::layout::used_values::FfiCssPixelRect,
    pub align_x: u8,
    pub align_y: u8,
    pub always_stop: bool,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiSnapAreaIdentity {
    pub node_id: i64,
    pub pseudo_element_type: u8,
}

#[repr(C)]
pub struct FfiSnapSelectionStrategy {
    pub r#type: SnapSelectionStrategyType,
    pub has_start_offset: bool,
    pub start_offset: used_values::FfiCssPixelPoint,
    pub displacement: used_values::FfiCssPixelPoint,
    pub has_starting_positions_boundary: bool,
    pub starting_positions_boundary: used_values::FfiCssPixelPoint,
}

#[repr(C)]
pub struct FfiSnapDestination<'a> {
    pub position: used_values::FfiCssPixelPoint,
    pub snapped_x: bool,
    pub snapped_y: bool,
    pub evaluated_x: bool,
    pub evaluated_y: bool,
    pub snapped_areas_x: ak::Span<'a, FfiSnapAreaIdentity>,
    pub snapped_areas_y: ak::Span<'a, FfiSnapAreaIdentity>,
}

#[repr(C)]
pub struct FfiSnapAreaDomRelations {
    pub context: *mut c_void,
    pub area_contains_focused_node: unsafe extern "C" fn(*mut c_void, FfiSnapAreaIdentity) -> bool,
    pub area_contains_targeted_element: unsafe extern "C" fn(*mut c_void, FfiSnapAreaIdentity) -> bool,
    pub element_of_area_is_ancestor_of_element_of:
        unsafe extern "C" fn(*mut c_void, FfiSnapAreaIdentity, FfiSnapAreaIdentity) -> bool,
}

/// How a recording that is published has the host render its SVG images. The fields are private: the callback is
/// reached only through the method below, which takes the main thread token.
///
/// NB: The callback is typed as a safe function because the table only comes from the host through an export whose
///     safety contract keeps it callable, and the private fields let nothing else call it.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct FfiRecordingPublishCallbacks {
    context: *mut c_void,
    render_vector_image:
        extern "C" fn(*mut c_void, *const FfiVectorImageRenderRequest) -> *const crate::render_state::DocumentHost,
}

impl FfiRecordingPublishCallbacks {
    /// Has the host render the SVG image `request` names, and answers the host of the image's document, which keeps
    /// the image's display list for the request, or none where the image has nothing to show.
    pub(crate) fn render_vector_image<'a>(
        &self,
        _: &'a MainThread,
        request: &FfiVectorImageRenderRequest,
    ) -> Option<&'a crate::render_state::DocumentHost> {
        // The C++ host renders the image synchronously and reads the request only for the duration of the call. The
        // image's document, and its host, outlive the recording that draws it.
        let host = (self.render_vector_image)(self.context, request);
        // SAFETY: The host answers a live document host or null.
        unsafe { host.as_ref() }
    }
}