Files
faunosteo/crates/core/tests/common/svg_tree.rs
T
bchanot 4f52f5e1c4 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.
2026-11-01 00:39:57 +01:00

178 lines
7.1 KiB
Rust

//! SVG trees as structure for the R3 oracles: namespace, local name, attributes by qualified
//! name (sorted), children with text, the shape the TypeScript generators freeze.
#![allow(dead_code, clippy::unwrap_used)]
use std::collections::BTreeMap;
use faunosteo_core::anatomy::svg::serialize::serialize;
use faunosteo_core::anatomy::svg::tree::{SVG_NS, parse_svg};
use faunosteo_core::anatomy::svg::{Element, Node};
use serde_json::{Value, json};
pub fn tree_value(element: &Element) -> Value {
let attrs: BTreeMap<&str, &str> = element
.attrs
.iter()
.map(|a| (a.qname.as_str(), a.value.as_str()))
.collect();
let children: Vec<Value> = element
.children
.iter()
.map(|n| match n {
Node::Element(e) => tree_value(e),
Node::Text(t) => json!({ "text": t }),
})
.collect();
json!({
"ns": element.namespace.as_deref().unwrap_or(SVG_NS),
"name": element.local_name(),
"attrs": attrs,
"children": children,
})
}
/// The element serialised and read again by the core, as a tree.
pub fn reread(element: &Element) -> Value {
tree_value(&parse_svg(&serialize(element)).unwrap())
}
/// Owner decision 2026-10-15 (R3): a trigger without a name of its own carries the opened
/// view's name in each language (the TypeScript freeze carries the French name twice). The
/// frozen trees stay as frozen; this declared change is applied to them before comparing:
/// `data-anno-name-en` takes the view's English name and the title reads "FR / EN".
pub fn with_trigger_names_per_language(tree: &mut Value, views: &[(String, String, String)]) {
if let Some(attrs) = tree.get_mut("attrs").and_then(Value::as_object_mut) {
let opened = attrs
.get("data-anno-opens-view")
.and_then(Value::as_str)
.map(str::to_owned);
let view = opened.and_then(|id| views.iter().find(|(v, _, _)| *v == id));
if let Some((_, fr, en)) = view
&& attrs.get("data-anno-name-en").and_then(Value::as_str) == Some(fr.as_str())
{
attrs.insert("data-anno-name-en".to_owned(), Value::String(en.clone()));
}
}
if let Some(text) = tree.get_mut("text") {
for (_, fr, en) in views {
if text.as_str() == Some(format!("{fr} / {fr}").as_str()) {
*text = Value::String(format!("{fr} / {en}"));
}
}
}
if let Some(children) = tree.get_mut("children").and_then(Value::as_array_mut) {
for child in children {
with_trigger_names_per_language(child, views);
}
}
}
/// Owner decision 2026-10-29 (V1-2.2): the canvas, inlined in the page, scopes the drawing's
/// ids and the references to them to its view (`fv.<view>.<id>`). Applied to a frozen canvas
/// tree before comparing, written apart from the core's rewrite: every `id`, every `#id`
/// href and every plain `url(#` (the only spelling of the product drawing).
pub fn with_ids_scoped_per_view(tree: &mut Value, view_id: &str) {
let scope = format!("fv.{view_id}.");
if let Some(attrs) = tree.get_mut("attrs").and_then(Value::as_object_mut) {
for (name, value) in attrs.iter_mut() {
let Some(text) = value.as_str() else { continue };
let scoped = match name.as_str() {
"id" => format!("{scope}{text}"),
"href" | "xlink:href" if text.starts_with('#') => format!("#{scope}{}", &text[1..]),
_ => text.replace("url(#", &format!("url(#{scope}")),
};
*value = Value::String(scoped);
}
}
if let Some(children) = tree.get_mut("children").and_then(Value::as_array_mut) {
for child in children {
with_ids_scoped_per_view(child, view_id);
}
}
}
/// (view id, FR name, EN name) of a document, for `with_trigger_names_per_language`.
pub fn view_names(
document: &faunosteo_core::domain::sheet::AnatomicalSheetDocument,
) -> Vec<(String, String, String)> {
document
.views
.iter()
.map(|v| (v.id().to_owned(), v.name().fr.clone(), v.name().en.clone()))
.collect()
}
/// Owner brief 2026-10-30 (V1-2.2, three MEDIUM): the composition root is the engine's. Applied
/// to a frozen view tree before comparing, written apart from the core: the root keeps its
/// namespaces and `viewBox`; its other attributes and every child before the layer go into a
/// `<g>`; the halo filter leaves the drawing for an engine `<defs>` placed before the layer;
/// each point hitbox (a circle capturing on `all` with no stroke of its own) declares
/// `stroke="none"` and `pointer-events="visibleFill"`.
pub fn with_isolated_layer(tree: &mut Value) {
let root = tree.as_object_mut().unwrap();
let mut children = root["children"].as_array().unwrap().clone();
let mut layer = children.pop().unwrap();
let halo = take_halo(&mut children);
let attrs = root["attrs"].as_object().unwrap().clone();
let (kept, moved): (Vec<_>, Vec<_>) = attrs
.into_iter()
.partition(|(name, _)| ["xmlns", "xmlns:xlink", "viewBox"].contains(&name.as_str()));
let group = json!({
"ns": root["ns"], "name": "g",
"attrs": moved.into_iter().collect::<serde_json::Map<_, _>>(),
"children": children,
});
let mut out = vec![group];
if let Some(halo) = halo {
out.push(json!({ "ns": SVG_NS, "name": "defs", "attrs": {}, "children": [halo] }));
}
for marker in layer["children"].as_array_mut().unwrap() {
let circle = marker["name"] == "circle";
let attrs = marker["attrs"].as_object_mut().unwrap();
let hitbox = circle
&& attrs.get("pointer-events") == Some(&json!("all"))
&& !attrs.contains_key("stroke");
if hitbox {
attrs.insert("stroke".to_owned(), json!("none"));
attrs.insert("pointer-events".to_owned(), json!("visibleFill"));
}
}
out.push(layer);
root.insert(
"attrs".to_owned(),
Value::Object(kept.into_iter().collect()),
);
root.insert("children".to_owned(), Value::Array(out));
}
/// Removes `filter#osteo-halo` from the frozen drawing (and the `<defs>` the engine created for
/// it alone, placed first), answering it.
fn take_halo(children: &mut Vec<Value>) -> Option<Value> {
let first_is_engine_defs = children.first().is_some_and(|c| {
c["name"] == "defs"
&& c["children"]
.as_array()
.is_some_and(|k| k.len() == 1 && k[0]["attrs"]["id"] == "osteo-halo")
});
if first_is_engine_defs {
return Some(children.remove(0)["children"][0].clone());
}
for child in children.iter_mut() {
if let Some(found) = take_from(child) {
return Some(found);
}
}
None
}
fn take_from(node: &mut Value) -> Option<Value> {
let kids = node.get_mut("children")?.as_array_mut()?;
if let Some(at) = kids
.iter()
.position(|k| k["name"] == "filter" && k["attrs"]["id"] == "osteo-halo")
{
return Some(kids.remove(at));
}
kids.iter_mut().find_map(take_from)
}