fix(anatomy): V1-2.2 refuse annotations outside their view's frame

A view without view_box was framed by the drawing's viewBox: moving it and
translating the drawing back left the horse identical and its 19 main-view
markers out of frame, accepted. One frame definition (view_box, else the
drawing viewBox read with the SVG grammar) now frames both composers and is
measured by the verdict (verification.validation.out_of_frame) and the
preview; curves and arcs on their shape. The document is never rewritten.
This commit is contained in:
bchanot committed 2026-10-30 20:54:46 +01:00
1 parent 958228a9de
commit 0c87c7f6c3
8 files changed
+1406 -25

No files matched your search

+5 -2
View File
@@ -21,7 +21,7 @@ use super::fingerprint::document_fingerprint;
use super::index::SheetIndex;
use super::index_data::{names, node_keys, sorted, spine_ids};
use super::loaded::{Drawing, read_drawing};
use super::report::{SheetReport, referenced_asset_ids, sheet_report};
use super::report::{SheetReport, frame_issues, referenced_asset_ids, sheet_report};
use super::review::DigestError;
use super::schema::{AnatomicalSheetDocument, LocalizedText};
use super::verdict::{read_document, sha256_hex};
@@ -270,12 +270,15 @@ impl PreviewSheet {
}
pub fn state(&self) -> Result<PreviewState, DigestError> {
let report = sheet_report(
let mut report = sheet_report(
&self.document,
self.document_bytes,
&self.drawing_sizes(),
None,
)?;
report.issues.extend(frame_issues(&self.document, |id| {
self.drawing(id).and_then(|d| d.as_ref().ok())
}));
Ok(PreviewState {
status: self.status,
fingerprint: self.fingerprint.clone(),
+60
View File
@@ -12,11 +12,19 @@ use super::caps::{CapsReport, caps_report};
use super::review::{Completeness, DigestError, ReviewGroup, completeness, review_groups};
use super::schema::{AnatomicalSheetDocument, Annotation};
use super::validate::{SheetIssue, SheetIssueSeverity, validate_sheet_document};
use crate::anatomy::frame::{FrameFault, Placement, frame_faults};
use crate::anatomy::svg::Element;
/// The submission rule of §6.7 the integrity tier does not hold: a draft may keep an
/// annotation without geometry, a submitted revision may not.
pub const GEOMETRY_MISSING: &str = "annotation.geometry.missing";
/// An annotation not entirely inside the frame of its view, or whose path data a browser would
/// not read whole (V1-2.2 owner brief 2026-10-30, `anatomy::frame`).
pub const OUT_OF_FRAME: &str = "annotation.geometry.out_of_frame";
/// A view drawn on a drawing with neither a `view_box` nor a usable drawing `viewBox`.
pub const NO_FRAME: &str = "view.frame.unknown";
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
pub struct SheetReport {
@@ -79,3 +87,55 @@ pub fn referenced_asset_ids(document: &AnatomicalSheetDocument) -> Vec<&str> {
.filter(|id| seen.insert(*id))
.collect()
}
/// The frame issues of `document` on the drawings `drawing_of` gives by asset id (read, with no
/// removal): the same faults for the verdict and the preview. A view whose drawing is not given
/// is not measured.
pub fn frame_issues<'a>(
document: &AnatomicalSheetDocument,
drawing_of: impl Fn(&str) -> Option<&'a Element>,
) -> Vec<SheetIssue> {
frame_faults(document, drawing_of)
.into_iter()
.map(|fault| frame_issue(document, &fault))
.collect()
}
fn frame_issue(document: &AnatomicalSheetDocument, fault: &FrameFault) -> SheetIssue {
let (code, path, message) = match *fault {
FrameFault::NoFrame { view_index } => (
NO_FRAME,
format!("views[{view_index}]"),
format!(
"View \"{}\" has neither a view_box nor a usable drawing viewBox",
document.views[view_index].id()
),
),
FrameFault::Annotation {
annotation_index,
placement,
} => {
let annotation = &document.annotations[annotation_index];
let what = if placement == Placement::Unreadable {
"has path data that cannot be read whole"
} else {
"is not entirely inside the frame"
};
(
OUT_OF_FRAME,
format!("annotations[{annotation_index}].geometry"),
format!(
"Annotation \"{}\" {what} of view \"{}\"",
annotation.id(),
annotation.view_id()
),
)
}
};
SheetIssue {
code: code.to_owned(),
severity: SheetIssueSeverity::Error,
path,
message,
}
}
+54 -16
View File
@@ -20,13 +20,15 @@ use ts_rs::TS;
use super::caps::CapsReport;
use super::fingerprint::{FingerprintError, document_fingerprint};
use super::report::{SheetReport, referenced_asset_ids, sheet_report};
use super::report::{
NO_FRAME, OUT_OF_FRAME, SheetReport, frame_issues, referenced_asset_ids, sheet_report,
};
use super::review::{Completeness, ElementChange, ReviewGroup, change_list};
use super::schema::AnatomicalSheetDocument;
use super::size::document_text_bytes;
use super::validate::{SheetIssue, SheetIssueSeverity};
use crate::anatomy::svg::serialize::stored_form;
use crate::anatomy::svg::{processing_instructions, read_svg};
use crate::anatomy::svg::{Element, processing_instructions, read_svg};
use crate::domain::js::parse_json;
/// The format of the verdict object, bumped with any change of its meaning.
@@ -49,6 +51,9 @@ pub mod codes {
pub const SVG_REJECTED: &str = "verification.integrity.svg_rejected";
pub const SVG_REMOVAL: &str = "verification.integrity.svg_removal";
pub const SVG_NOT_CANONICAL: &str = "verification.integrity.svg_not_canonical";
/// An annotation outside the frame of its view, or a view without a frame (V1-2.2):
/// measured on the views whose drawing reads clean.
pub const OUT_OF_FRAME: &str = "verification.validation.out_of_frame";
pub const FINGERPRINT: &str = "verification.integrity.fingerprint";
}
@@ -173,11 +178,19 @@ fn verify(input: &VerdictInput<'_>, verdict: &mut Verdict) -> Result<(), &'stati
let drawings = check_drawings(&document.typed, &input.drawings);
let sizes: Vec<u64> = drawings.iter().map(|d| d.size).collect();
let base_typed = base.as_ref().map(|b| &b.typed);
let report = sheet_report(&document.typed, document.bytes, &sizes, base_typed)
let mut report = sheet_report(&document.typed, document.bytes, &sizes, base_typed)
.map_err(|_| codes::FINGERPRINT)?;
let roots: HashMap<&str, &Element> = drawings
.iter()
.filter_map(|d| Some((d.check.asset_id.as_str(), d.root.as_ref()?)))
.collect();
report
.issues
.extend(frame_issues(&document.typed, |id| roots.get(id).copied()));
for code in rule_failures(&report)
.into_iter()
.chain(drawing_failures(&drawings))
.chain(frame_failures(&report))
{
fail(verdict, code);
}
@@ -262,7 +275,9 @@ fn rule_failures(report: &SheetReport) -> Vec<&'static str> {
.filter(|i| i.severity == SheetIssueSeverity::Error)
};
let mut out = Vec::new();
if errors().any(|i| i.code != super::report::GEOMETRY_MISSING) {
let rule =
|code: &str| ![super::report::GEOMETRY_MISSING, OUT_OF_FRAME, NO_FRAME].contains(&code);
if errors().any(|i| rule(&i.code)) {
out.push(codes::RULES);
}
if errors().any(|i| i.code == super::report::GEOMETRY_MISSING) {
@@ -274,10 +289,22 @@ fn rule_failures(report: &SheetReport) -> Vec<&'static str> {
out
}
/// The frame code when a frame issue was found (after the drawing faults: a view is measured
/// only on a drawing that reads clean).
fn frame_failures(report: &SheetReport) -> Option<&'static str> {
report
.issues
.iter()
.any(|i| i.code == OUT_OF_FRAME || i.code == NO_FRAME)
.then_some(codes::OUT_OF_FRAME)
}
struct CheckedDrawing {
check: DrawingCheck,
/// The size the caps count: the object's, else the row's.
size: u64,
/// The drawing as read, when it is `Ok` (the frame of its views).
root: Option<Element>,
}
/// Every drawing the views reference, in view order, against its row and its bytes.
@@ -292,12 +319,14 @@ fn check_drawings(
None => CheckedDrawing {
check: drawing_check(asset_id, None, DrawingResult::Missing, None),
size: 0,
root: None,
},
Some(row) => {
let (result, detail) = check_bytes(row);
let (result, detail, root) = check_bytes(row);
CheckedDrawing {
check: drawing_check(asset_id, Some(row), result, detail),
size: row.bytes.map_or(row.byte_size, |b| b.len() as u64),
root,
}
}
})
@@ -319,34 +348,43 @@ fn drawing_check(
}
}
/// Size, then the hash of the bytes against the row (the object key), then the reader.
fn check_bytes(row: &VerdictDrawing<'_>) -> (DrawingResult, Option<String>) {
/// Size, then the hash of the bytes against the row (the object key), then the reader; the
/// root read when the drawing is `Ok`.
fn check_bytes(row: &VerdictDrawing<'_>) -> (DrawingResult, Option<String>, Option<Element>) {
let Some(bytes) = row.bytes else {
return (DrawingResult::Missing, None);
return (DrawingResult::Missing, None, None);
};
if bytes.len() as u64 != row.byte_size {
return (DrawingResult::SizeMismatch, None);
return (DrawingResult::SizeMismatch, None, None);
}
if sha256_hex(bytes) != row.sha256 {
return (DrawingResult::HashMismatch, None);
return (DrawingResult::HashMismatch, None, None);
}
let Ok(text) = std::str::from_utf8(bytes) else {
return (DrawingResult::SvgRejected, Some("malformed".to_owned()));
return (
DrawingResult::SvgRejected,
Some("malformed".to_owned()),
None,
);
};
match read_svg(text) {
Err(rejection) => (
DrawingResult::SvgRejected,
Some(rejection.reason().to_owned()),
None,
),
Ok(read) if !read.removed.is_empty() => (
DrawingResult::SvgRemoval,
Some(read.removed.join(", ")),
None,
),
Ok(read) if !read.removed.is_empty() => {
(DrawingResult::SvgRemoval, Some(read.removed.join(", ")))
}
Ok(_) if !processing_instructions(text).is_empty() => (
DrawingResult::SvgRejected,
Some("processing_instruction".to_owned()),
None,
),
Ok(read) if stored_form(&read.root) != text => (DrawingResult::SvgNotCanonical, None),
Ok(_) => (DrawingResult::Ok, None),
Ok(read) if stored_form(&read.root) != text => (DrawingResult::SvgNotCanonical, None, None),
Ok(read) => (DrawingResult::Ok, None, Some(read.root)),
}
}