fix(anatomy): V1-2.2 marker layer and halo outside the drawing's group

Owner-approved lot B (brief 2026-11-01). The composition root is the
engine's (namespaces and frame); the drawing's root becomes a group and
the marker layer and halo are its siblings, so no root property reaches
a marker. Point hitboxes declare stroke none and visibleFill. Census over
the reader's whole allowlist; R3 references compared under the declared
transform; click proofs in three engines.
This commit is contained in:
bchanot committed 2026-11-01 00:39:57 +01:00
1 parent f1bb19b30b
commit 4f52f5e1c4
23 files changed
+933 -112

No files matched your search

+71 -65
View File
@@ -1,9 +1,9 @@
//! Composition of one view (`docs/ANNOTATION-RENDERING.md`): the sanitised drawing framed on
//! the view, with its annotation layer last. Two forms, as the TypeScript has them: the
//! editor canvas (`apps/app/src/anatomy/drawing.ts`: a fresh root, `0 0 100 100` when no box
//! is known, the interactive layer) and the PDF highlight (`packages/anatomy/src/render/
//! compose.ts`: the asset's root framed in place, the selected markers only, recoloured, the
//! halo filter on request). Both take a sanitised tree, never text.
//! editor canvas (`apps/app/src/anatomy/drawing.ts`: `0 0 100 100` when no frame is known, the
//! interactive layer) and the PDF highlight (`packages/anatomy/src/render/compose.ts`: no
//! `viewBox` when no frame is known, the selected markers only, recoloured, the halo filter on
//! request). Both take a sanitised tree, never text.
//!
//! The canvas is inlined into the page, where `#id` resolves against the whole document: its
//! ids and the references to them are scoped to the view (`fv.<view id>.<id>`, owner decision
@@ -13,19 +13,23 @@
//! 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.
//! Both forms append the marker layer as the root's last child, so the root never hides it: a
//! root `visibility` other than `visible` is refused by the reader and by both composers.
//! Both frame the view on `frame::view_frame` (the view's box, else the drawing's `viewBox` read
//! as a browser reads it), the frame the verdict and the preview measure the annotations against
//! (V1-2.2 owner brief 2026-10-30, defect 3); the canvas falls back to `0 0 100 100`, the PDF
//! view to no `viewBox`. The drawing's `preserveAspectRatio` is dropped: the default
//! `xMidYMid meet` shows the whole frame whatever the box (a `slice` would crop it where the
//! raster's aspect is clamped or the canvas box is not the frame's).
//!
//! The composition root is the engine's (V1-2.2 owner brief 2026-10-30): the namespaces and the
//! frame only. The frame is `frame::view_frame` (the view's box, else the drawing's `viewBox` read
//! as a browser reads it), the one the verdict and the preview measure the annotations against;
//! with no `preserveAspectRatio` the default `xMidYMid meet` shows the whole frame whatever the
//! box. The drawing's root becomes a
//! `<g>` keeping its other attributes and its children, and the marker layer (and the PDF's halo
//! filter, in a `<defs>` of the engine) are appended as that group's siblings: no property the
//! drawing sets, inherited (`stroke-width`, `stroke-opacity`, `stroke-dasharray`, `fill-rule`,
//! `visibility`…) or acting on a whole group (`opacity`, `display`), reaches a marker or the
//! halo. A root `visibility` other than `visible` stays refused by the reader and by both
//! composers (accepted fix of 2026-10-30).
use std::collections::HashSet;
use super::frame::view_frame;
use super::layer::{LayerError, find_view, render_layer};
use super::layer::{LayerError, find_view, render_composed_layer};
pub use super::svg::refs::HALO_FILTER_ID;
use super::svg::refs::holds_reserved_id;
use super::svg::sanitize::check_root_visibility;
@@ -54,8 +58,9 @@ pub enum ComposeError {
/// 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,
/// The drawing's root carries a `visibility` other than `visible`, which the marker layer
/// would inherit. Fail-closed: the reader refuses it (`root_visibility`).
/// The drawing's root carries a `visibility` other than `visible` (the marker layer inherited
/// it before the root became the engine's). Fail-closed: the reader refuses it
/// (`root_visibility`).
RootVisibility,
}
@@ -98,32 +103,62 @@ pub fn compose_canvas(
if check_root_visibility(asset).is_err() {
return Err(ComposeError::RootVisibility);
}
let mut root = Element {
qname: "svg".to_owned(),
namespace: asset.namespace.clone(),
attrs: asset
.attrs
.iter()
.filter(|a| !matches!(a.qname.as_str(), "width" | "height" | "preserveAspectRatio"))
.cloned()
.collect(),
children: asset.children.clone(),
};
let view_box = view_frame(view, asset).map_or_else(
|| CANVAS_FALLBACK_VIEW_BOX.to_owned(),
|frame| frame.view_box(),
);
root.set_attr("viewBox", view_box);
let mut root = isolated(asset.clone(), Some(view_box));
let scope = id_scope(view_id);
scope_ids(&mut root, &scope);
if !references_scoped(&root, &scope) {
return Err(ComposeError::UnscopedReference);
}
let layer = render_layer(document, view_id, &HashSet::new())?;
let layer = render_composed_layer(document, view_id, &HashSet::new())?;
root.children.push(Node::Element(checked(layer)?));
Ok(root)
}
/// Root attributes with no meaning on a `<g>`: the frame is the engine's, the size is the
/// page's or the raster's.
const ROOT_ONLY: [&str; 7] = [
"viewBox",
"preserveAspectRatio",
"version",
"width",
"height",
"x",
"y",
];
/// The composition root framed on `view_box`, holding the drawing as its one child: a `<g>`
/// with the drawing root's attributes (namespace declarations and `ROOT_ONLY` aside) and its
/// children. The caller appends the engine's elements after it.
fn isolated(asset: Element, view_box: Option<String>) -> Element {
let (namespaces, attrs): (Vec<_>, Vec<_>) = asset
.attrs
.into_iter()
.partition(|a| a.qname == "xmlns" || a.qname == "xmlns:xlink");
let drawing = Element {
qname: "g".to_owned(),
namespace: asset.namespace.clone(),
attrs: attrs
.into_iter()
.filter(|a| !ROOT_ONLY.contains(&a.qname.as_str()))
.collect(),
children: asset.children,
};
let mut root = Element {
qname: "svg".to_owned(),
namespace: asset.namespace,
attrs: namespaces,
children: vec![Node::Element(drawing)],
};
if let Some(view_box) = view_box {
root.set_attr("viewBox", view_box);
}
root
}
/// The prefix of a view's ids in its canvas. Stable ids hold no `.`, so the scope of one view
/// is never the start of another's, and no page id starts with `fv.`.
pub fn id_scope(view_id: &str) -> String {
@@ -180,7 +215,7 @@ fn scope_ids(element: &mut Element, scope: &str) {
pub fn compose_highlight(
document: &AnatomicalSheetDocument,
view_id: &str,
mut asset: Element,
asset: Element,
annotation_ids: &HashSet<&str>,
halo: bool,
) -> Result<Element, ComposeError> {
@@ -191,20 +226,16 @@ pub fn compose_highlight(
if check_root_visibility(&asset).is_err() {
return Err(ComposeError::RootVisibility);
}
match view_frame(view, &asset) {
Some(frame) => asset.set_attr("viewBox", frame.view_box()),
None => asset.remove_attr("viewBox"),
}
asset.remove_attr("width");
asset.remove_attr("height");
asset.remove_attr("preserveAspectRatio");
let view_box = view_frame(view, &asset).map(|frame| frame.view_box());
let mut root = isolated(asset, view_box);
if halo {
add_halo_filter(&mut asset);
let defs = Element::svg("defs", Vec::new(), vec![Node::Element(halo_filter())]);
root.children.push(Node::Element(defs));
}
let layer = render_layer(document, view_id, &HashSet::new())?;
let layer = render_composed_layer(document, view_id, &HashSet::new())?;
let layer = highlight_layer(layer, annotation_ids, halo);
asset.children.push(Node::Element(checked(layer)?));
Ok(asset)
root.children.push(Node::Element(checked(layer)?));
Ok(root)
}
/// The layer reduced to the selected annotations, recoloured, every `data-*` dropped.
@@ -235,31 +266,6 @@ fn highlight_element(mut element: Element, halo: bool) -> Element {
element
}
/// The halo filter in the first `<defs>` of the drawing, else in a new `<defs>` 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));
}
}
/// Appends the halo to the first `<defs>` below `element` (document order).
fn append_to_first_defs(element: &mut Element) -> bool {
for child in &mut element.children {
let Node::Element(child) = child else {
continue;
};
if child.qname == "defs" {
child.children.push(Node::Element(halo_filter()));
return true;
}
if append_to_first_defs(child) {
return true;
}
}
false
}
fn halo_filter() -> Element {
let blur = |std: &str, result: &str| {
Node::Element(Element::svg(
+34 -3
View File
@@ -27,14 +27,38 @@ pub fn find_view<'a>(
}
/// `renderAnnotationLayer({ document, viewId, states })`: `states` holds the ids marked
/// `data-state="current"`.
/// `data-state="current"`. The R3 tree, kept for the seed review drawings
/// (`curation::draft::app_review`); the composers draw `render_composed_layer`.
pub fn render_layer(
document: &AnatomicalSheetDocument,
view_id: &str,
states: &HashSet<&str>,
) -> Result<Element, LayerError> {
layer(document, view_id, states, false)
}
/// The layer the composers draw (V1-2.2 owner brief 2026-10-30): the R3 tree with each point
/// hitbox declaring its paint and its capture rule, `stroke="none"` and
/// `pointer-events="visibleFill"`, so its clickable area is its circle whatever stroke the page
/// or a drawing would give it (under `pointer-events="all"` a stroke widens the hit area even
/// when nothing paints it). The zones and the trigger already declare theirs.
pub fn render_composed_layer(
document: &AnatomicalSheetDocument,
view_id: &str,
states: &HashSet<&str>,
) -> Result<Element, LayerError> {
layer(document, view_id, states, true)
}
fn layer(
document: &AnatomicalSheetDocument,
view_id: &str,
states: &HashSet<&str>,
declared_hitbox: bool,
) -> Result<Element, LayerError> {
let view = find_view(document, view_id).ok_or(LayerError::UnknownView)?;
let context = Context {
declared_hitbox,
twin_opacity: if view.is_main() { "0.65" } else { "0.5" },
categories: document
.categories
@@ -59,6 +83,8 @@ pub fn render_layer(
}
struct Context<'a> {
/// The hitbox declares `stroke="none"` and captures on its circle only (composed form).
declared_hitbox: bool,
twin_opacity: &'static str,
/// Last duplicate wins, as a JavaScript `Map` built from the list.
categories: HashMap<&'a str, Option<&'a str>>,
@@ -154,9 +180,14 @@ impl Context<'_> {
("cy", js_number(cy)),
("r", js_number(r)),
("fill", "transparent".to_owned()),
("pointer-events", "all".to_owned()),
("style", "cursor:pointer".to_owned()),
];
if self.declared_hitbox {
hitbox.push(("stroke", "none".to_owned()));
hitbox.push(("pointer-events", "visibleFill".to_owned()));
} else {
hitbox.push(("pointer-events", "all".to_owned()));
}
hitbox.push(("style", "cursor:pointer".to_owned()));
hitbox.extend(data.iter().cloned());
let mut visible: Attrs = vec![
("cx", js_number(cx)),
+1 -1
View File
@@ -14,7 +14,7 @@ pub use tree::{Element, Node, SvgRejection};
/// Bounds, parses, sanitises and checks the references of an SVG text: no cycle, within the
/// expansion budget, each naming an id the drawing defines; no id the composition writes and
/// no root `visibility` the marker layer would inherit.
/// no root `visibility` other than `visible`.
pub fn read_svg(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize(root)?;
+2 -2
View File
@@ -18,8 +18,8 @@ use super::tree::{Element, Node, SvgRejection};
/// Expanded elements a drawing may instantiate (the product's drawing: a few thousand).
pub const MAX_EXPANDED_ELEMENTS: u64 = 50_000;
/// The PDF highlight's filter (`compose::compose_highlight`), written into the drawing's own
/// document: the PDF view keeps the drawing's ids.
/// The PDF highlight's filter (`compose::compose_highlight`), written in an engine `<defs>` of
/// the composed document next to the drawing: the PDF view keeps the drawing's ids.
pub const HALO_FILTER_ID: &str = "osteo-halo";
/// Every id the composition writes next to the drawing's ids. A drawing defining one would
+6 -5
View File
@@ -44,7 +44,8 @@ const ALLOWED_ELEMENTS: [&str; 28] = [
"use",
];
const ALLOWED_ATTRIBUTES: [&str; 77] = [
/// Public for the marker isolation census (`tests/svg_marker_isolation.rs`).
pub const ALLOWED_ATTRIBUTES: [&str; 77] = [
"id",
"class",
"d",
@@ -435,10 +436,10 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
}
/// The root lets the engine's marker layer show: no `visibility`, or `visible` (CSS whitespace
/// and letter case aside). Both composers append the layer as the root's last child, so it
/// inherits the root's `visibility`, which the drawing's own children can set back to
/// `visible`: any other value, `hidden`, `collapse`, a CSS-wide keyword or an unreadable one,
/// is refused (V1-2.2 security review, owner brief 2026-10-30). The root's `style` and `class`
/// and letter case aside). Until the composition root became the engine's, the layer inherited
/// the root's `visibility`, which the drawing's own children can set back to `visible`: any
/// other value, `hidden`, `collapse`, a CSS-wide keyword or an unreadable one, stays refused
/// (V1-2.2 security review, owner brief 2026-10-30), the composers checking it too. The root's `style` and `class`
/// are removed by `sanitize`, so this attribute is the one way left to set it.
pub fn check_root_visibility(root: &Element) -> Result<(), SvgRejection> {
let visible = root.attr("visibility").is_none_or(|value| {
+3 -3
View File
@@ -111,9 +111,9 @@ pub enum SvgRejection {
/// An id the composition writes into the drawing's own document (`refs::RESERVED_IDS`):
/// the drawing's element would stand in for the engine's (V1-2.2, owner brief 2026-10-30).
ReservedId,
/// A `visibility` on the root other than `visible`: the engine's marker layer, appended as
/// the root's last child, inherits it, while the drawing's own children can set `visible`
/// back (V1-2.2 security review, owner brief 2026-10-30).
/// A `visibility` on the root other than `visible`: until the composition root became the
/// engine's, the marker layer inherited it while the drawing's own children set `visible`
/// back (V1-2.2 security review, owner brief 2026-10-30); kept as a reader rule.
RootVisibility,
}
@@ -4,6 +4,11 @@
//! uses. Written under `draft/review/app-<view>.svg` so the markers and the framing can be
//! compared outside the browser.
//!
//! Declared difference (V1-2.2, owner brief 2026-10-30): the application draws the drawing in a
//! group of its own under an engine root and declares each point hitbox's stroke and capture
//! (`compose`, `layer::render_composed_layer`); this review drawing keeps the R5 form, a seed
//! resource never regenerated without the owner.
//!
//! Declared difference: the R3 owner decision (2026-10-15) gives a trigger without a name of
//! its own the opened view's name in each language; the TypeScript renderer wrote the French
//! name twice (`svg_compare::TriggerNames`).