diff --git a/crates/core/src/anatomy/compose.rs b/crates/core/src/anatomy/compose.rs index e00df75a..453274fe 100644 --- a/crates/core/src/anatomy/compose.rs +++ b/crates/core/src/anatomy/compose.rs @@ -10,11 +10,15 @@ //! 2026-10-29), so a view never resolves into another view's drawing or the page, and the page //! never into a drawing. The reader already refused references to ids a drawing does not //! define. Stored bytes are never rewritten; the PDF view is rasterised as a document of its -//! own and keeps the drawing's ids. +//! own and keeps the drawing's ids, so it never holds one the composition writes +//! (`svg::refs::RESERVED_IDS`, the halo filter): the reader refuses such a drawing and both +//! composers refuse such a tree. use std::collections::HashSet; use super::layer::{LayerError, find_view, render_layer, view_box_of}; +pub use super::svg::refs::HALO_FILTER_ID; +use super::svg::refs::holds_reserved_id; use super::svg::sanitize::{local_reference_text, scope_url_targets, url_targets, urls_are_local}; use super::svg::serialize::is_xml_text; use super::svg::{Element, Node}; @@ -22,7 +26,6 @@ use crate::domain::sheet::AnatomicalSheetDocument; pub const HIGHLIGHT_FILL: &str = "#b85a3f"; pub const HIGHLIGHT_FILL_OPACITY: &str = "0.85"; -pub const HALO_FILTER_ID: &str = "osteo-halo"; const CANVAS_FALLBACK_VIEW_BOX: &str = "0 0 100 100"; /// Why a view cannot be composed. @@ -36,6 +39,11 @@ pub enum ComposeError { /// not read as a local reference (`sanitize::value_acceptable`), so only an element tree /// that did not come out of `read_svg` reaches it. UnscopedReference, + /// The drawing holds an id the composition writes (`svg::refs::RESERVED_IDS`): in the PDF + /// view its element would replace the halo filter or win its resolution; neither form + /// composes it. Fail-closed: the reader refuses it (`reserved_id`), so only a tree that did + /// not come out of `read_svg` reaches it, a composed view given back included. + ReservedId, } impl From for ComposeError { @@ -71,6 +79,9 @@ pub fn compose_canvas( asset: &Element, ) -> Result { let view = find_view(document, view_id).ok_or(ComposeError::UnknownView)?; + if holds_reserved_id(asset) { + return Err(ComposeError::ReservedId); + } let mut root = Element { qname: "svg".to_owned(), namespace: asset.namespace.clone(), @@ -147,7 +158,8 @@ fn scope_ids(element: &mut Element, scope: &str) { } /// The PDF view of `view_id`: `asset` (sanitised, consumed) framed on the view, the markers -/// of `annotation_ids` only, recoloured, with the halo filter when `halo`. +/// of `annotation_ids` only, recoloured, with the halo filter when `halo`. The only element +/// of the result holding a reserved id is the composition's own. pub fn compose_highlight( document: &AnatomicalSheetDocument, view_id: &str, @@ -156,13 +168,16 @@ pub fn compose_highlight( halo: bool, ) -> Result { let view = find_view(document, view_id).ok_or(ComposeError::UnknownView)?; + if holds_reserved_id(&asset) { + return Err(ComposeError::ReservedId); + } if let Some(view_box) = view_box_of(view) { asset.set_attr("viewBox", view_box); } asset.remove_attr("width"); asset.remove_attr("height"); if halo { - ensure_halo_filter(&mut asset); + add_halo_filter(&mut asset); } let layer = render_layer(document, view_id, &HashSet::new())?; let layer = highlight_layer(layer, annotation_ids, halo); @@ -198,24 +213,14 @@ fn highlight_element(mut element: Element, halo: bool) -> Element { element } -/// One halo filter per root: in the first `` of the drawing, else in a new `` -/// placed first; nothing when a `filter#osteo-halo` already exists. -fn ensure_halo_filter(root: &mut Element) { - if has_halo_filter(root) { - return; - } +/// The halo filter in the first `` of the drawing, else in a new `` placed first. +fn add_halo_filter(root: &mut Element) { if !append_to_first_defs(root) { let defs = Element::svg("defs", Vec::new(), vec![Node::Element(halo_filter())]); root.children.insert(0, Node::Element(defs)); } } -fn has_halo_filter(element: &Element) -> bool { - element.element_children().any(|c| { - (c.qname == "filter" && c.attr("id") == Some(HALO_FILTER_ID)) || has_halo_filter(c) - }) -} - /// Appends the halo to the first `` below `element` (document order). fn append_to_first_defs(element: &mut Element) -> bool { for child in &mut element.children { diff --git a/crates/core/src/anatomy/svg/clean.rs b/crates/core/src/anatomy/svg/clean.rs index d18fd35e..4d08ceb9 100644 --- a/crates/core/src/anatomy/svg/clean.rs +++ b/crates/core/src/anatomy/svg/clean.rs @@ -5,7 +5,8 @@ //! (`malformed`), or any removal of active or external content, or a processing //! instruction (`svg_active_content`, the names in `active`, a PI as `?`), or, once //! no active content is found, a reference to an id the drawing does not define, in the -//! file or after a removal (`dangling_reference`); no cleaning is offered; +//! file or after a removal (`dangling_reference`), then an id the composition writes +//! (`reserved_id`, `refs::RESERVED_IDS`); no cleaning is offered; //! - cleanable: only other removals (editor metadata and namespaces, `data-*`, `