diff --git a/apps/app/tests/unit/editor-min-point-radius.test.ts b/apps/app/tests/unit/editor-min-point-radius.test.ts new file mode 100644 index 00000000..8a4ce17f --- /dev/null +++ b/apps/app/tests/unit/editor-min-point-radius.test.ts @@ -0,0 +1,36 @@ +/** + * The core states, per view, the smallest radius a point may have (the sheet editor's size + * slider marks it): the bundled draft's main view, from its frame and the preview size. + */ +import { beforeAll, describe, expect, it } from 'vitest' +import { CataloguePreview } from '@/catalogue/preview' +import { coreRuntime, setCoreRuntime } from '@/core/runtime' +import { bundledSheet } from '../helpers/sheet' +import { testCoreRuntime } from '../helpers/core' + +beforeAll(async () => { + setCoreRuntime(await testCoreRuntime()) +}) + +describe('min_point_radius of the preview state', () => { + it('gives the bundled draft its main-view radius, none for an unknown view', async () => { + const bundle = await bundledSheet() + const [assetId] = Object.keys(bundle.assets) + const preview = new CataloguePreview( + coreRuntime(), + { status: 'draft', document: JSON.stringify(bundle.document) }, + [ + { + assetId, + sha256: bundle.assetHashes[assetId], + bytes: new TextEncoder().encode(bundle.assets[assetId]), + }, + ], + ) + const state = preview.state() + if (!state.ok) throw new Error(state.kind) + const radii = state.value.min_point_radius + expect(radii['main']).toBeCloseTo(26.5, 1) + expect(radii['nowhere'] ?? null).toBeNull() + }) +}) diff --git a/crates/core/src/anatomy/frame.rs b/crates/core/src/anatomy/frame.rs index a2c676b1..2026a985 100644 --- a/crates/core/src/anatomy/frame.rs +++ b/crates/core/src/anatomy/frame.rs @@ -20,8 +20,10 @@ //! Owner brief 2026-11-01 (the last MEDIUM, zone without interior): a polygon or a path must //! also fill a disc of `MIN_VISIBLE_PX` on that preview (`interior`), its stroke left out. +use std::collections::BTreeMap; + use super::interior::{Budget, LAYER_FILL_RULE, Search, geometry_outline, widest_disc}; -use super::layer::{find_view, twin_radius}; +use super::layer::{TWIN_INSET, TWIN_MIN_RADIUS, find_view, twin_radius}; use super::path::{Bounds, number_list, path_bounds}; use super::svg::Element; use crate::domain::sheet::{AnatomicalSheetDocument, Annotation, Geometry, SheetView}; @@ -227,6 +229,31 @@ pub fn annotation_placement( } } +/// The smallest hitbox radius of a point (a circle that is not a trigger) that passes +/// `TooSmall` in a view with this frame, rounded up to 0.01; `None` when every positive radius +/// passes (preview scale of 1 or more: the twin's 2-unit floor already shows 4 px). +pub fn min_point_radius(frame: &Frame, main_view: bool) -> Option { + let half_disc = MIN_VISIBLE_PX / 2.0 / frame.preview_scale(main_view); + // `twin_radius(r) = max(2, r - 12)`: below the floor no radius is too small. + (half_disc > TWIN_MIN_RADIUS).then(|| ((TWIN_INSET + half_disc) * 100.0).ceil() / 100.0) +} + +/// The minimum point radius of each view whose frame is usable and which has one, by view id. +pub fn min_point_radii<'a>( + document: &AnatomicalSheetDocument, + drawing_of: impl Fn(&str) -> Option<&'a Element>, +) -> BTreeMap { + document + .views + .iter() + .filter_map(|view| { + let drawing = first_view_drawing(document, view, &drawing_of)?; + let radius = min_point_radius(&view_frame(view, drawing)?, view.is_main())?; + Some((view.id().to_owned(), radius)) + }) + .collect() +} + /// Whether the fill of a polygon or a path holds a disc of `MIN_VISIBLE_PX` at `scale` CSS px /// per unit; a circle's disc is its own. A path without an outline is refused (the bounds and /// the outline share one reader, so it does not happen). @@ -274,12 +301,7 @@ pub fn frame_faults<'a>( let mut faults = Vec::new(); let mut budget = Budget::default(); for (view_index, view) in document.views.iter().enumerate() { - // A repeated view id draws the first view of that id, as the composers find it. - if !find_view(document, view.id()).is_some_and(|first| std::ptr::eq(first, view)) { - continue; - } - let drawing = super::compose::asset_id_of_view(document, view.id()).and_then(&drawing_of); - let Some(drawing) = drawing else { + let Some(drawing) = first_view_drawing(document, view, &drawing_of) else { continue; }; let Some(frame) = view_frame(view, drawing) else { @@ -291,6 +313,19 @@ pub fn frame_faults<'a>( faults } +/// The drawing `view` is drawn on, when it is the first view of its id (a repeated view id draws +/// the first view of that id, as the composers find it) and its drawing is given. +fn first_view_drawing<'a>( + document: &AnatomicalSheetDocument, + view: &SheetView, + drawing_of: &impl Fn(&str) -> Option<&'a Element>, +) -> Option<&'a Element> { + if !find_view(document, view.id()).is_some_and(|first| std::ptr::eq(first, view)) { + return None; + } + super::compose::asset_id_of_view(document, view.id()).and_then(drawing_of) +} + fn annotation_faults( document: &AnatomicalSheetDocument, view: &SheetView, diff --git a/crates/core/src/anatomy/layer.rs b/crates/core/src/anatomy/layer.rs index 7e593832..0aa395aa 100644 --- a/crates/core/src/anatomy/layer.rs +++ b/crates/core/src/anatomy/layer.rs @@ -256,9 +256,14 @@ impl Context<'_> { } } +/// The smallest radius of a point's visible twin, in view units. +pub const TWIN_MIN_RADIUS: f64 = 2.0; +/// How much smaller than its hitbox a point's visible twin is drawn, in view units. +pub const TWIN_INSET: f64 = 12.0; + /// The radius of a point's visible twin, drawn on its hitbox of radius `r`. pub fn twin_radius(r: f64) -> f64 { - 2.0_f64.max(r - 12.0) + TWIN_MIN_RADIUS.max(r - TWIN_INSET) } /// A trigger without a name of its own takes the opened view's name in each language diff --git a/crates/core/src/domain/sheet/preview.rs b/crates/core/src/domain/sheet/preview.rs index 6e45b4d8..0555c545 100644 --- a/crates/core/src/domain/sheet/preview.rs +++ b/crates/core/src/domain/sheet/preview.rs @@ -25,6 +25,7 @@ use super::report::{SheetReport, frame_issues, referenced_asset_ids, sheet_repor use super::review::DigestError; use super::schema::{AnatomicalSheetDocument, LocalizedText}; use super::verdict::{read_document, sha256_hex}; +use crate::anatomy::frame::min_point_radii; use crate::anatomy::svg::processing_instructions; use crate::anatomy::svg::serialize::stored_form; use crate::domain::archive::SnapshotCaps; @@ -284,6 +285,9 @@ impl PreviewSheet { fingerprint: self.fingerprint.clone(), drawings: self.drawing_states(), index: preview_index(&self.document), + min_point_radius: min_point_radii(&self.document, |id| { + self.drawing(id).and_then(|d| d.as_ref().ok()) + }), report, document: self.document.clone(), }) @@ -365,6 +369,10 @@ pub struct PreviewState { pub report: SheetReport, pub drawings: Vec, pub index: Option, + /// The smallest hitbox radius of a point in each view, by view id: the radius whose visible + /// disc just passes the minimum size on the smallest preview. Absent for a view without a + /// usable frame or drawing, and for one where every radius passes. + pub min_point_radius: BTreeMap, /// The document as read (defaults filled in). pub document: AnatomicalSheetDocument, } diff --git a/crates/core/tests/sheet_preview.rs b/crates/core/tests/sheet_preview.rs index 0f37707d..f357421f 100644 --- a/crates/core/tests/sheet_preview.rs +++ b/crates/core/tests/sheet_preview.rs @@ -7,12 +7,16 @@ mod common; +use faunosteo_core::anatomy::frame::{ + Frame, MIN_VISIBLE_PX, Placement, annotation_placement, min_point_radius, view_frame, +}; +use faunosteo_core::anatomy::interior::Budget; use faunosteo_core::domain::js::parse_json; use faunosteo_core::domain::sheet::preview::{ PreviewDrawing, PreviewDrawingStatus, PreviewFailure, PreviewInput, PreviewSheet, PreviewState, PreviewStatus, }; -use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet, SheetIndexData}; +use faunosteo_core::domain::sheet::{Annotation, BundledSheetData, LoadedSheet, SheetIndexData}; use faunosteo_core::facade::{call_with_preview, call_with_sheet}; use serde_json::{Value, json}; @@ -466,3 +470,93 @@ fn a_drawing_outside_the_stored_form_shows_rejected() { PreviewDrawingStatus::Ok ); } + +/// The minimum radius the formula gives on a frame, rounded up to 0.01, written apart from the +/// function under test. +fn formula_radius(frame: &Frame, main: bool) -> Option { + let (w, h) = if main { (286.0, 310.0) } else { (254.0, 191.0) }; + let scale = (w / frame.width).min(h / frame.height); + let half_disc = MIN_VISIBLE_PX / 2.0 / scale; + (half_disc > 2.0).then(|| ((12.0 + half_disc) * 100.0).ceil() / 100.0) +} + +/// The frames of the equine draft's views, read from the fixture's document and drawing. +fn equine_frames(sheet: &PreviewSheet) -> Vec<(String, bool, Frame)> { + let drawing = sheet.drawing(HORSE_ID).unwrap().as_ref().unwrap(); + sheet + .document() + .views + .iter() + .map(|v| { + ( + v.id().to_owned(), + v.is_main(), + view_frame(v, drawing).unwrap(), + ) + }) + .collect() +} + +#[test] +fn each_view_states_the_smallest_radius_a_point_may_have() { + let sheet = published(); + let radii = state(&sheet).min_point_radius; + let frames = equine_frames(&sheet); + assert_eq!(radii.len(), frames.len()); + for (id, main, frame) in &frames { + assert_eq!(radii.get(id).copied(), formula_radius(frame, *main), "{id}"); + } + assert!((radii["main"] - 26.50).abs() < 0.01, "{radii:?}"); + assert!((radii["head"] - 15.98).abs() < 0.01, "{radii:?}"); +} + +#[test] +fn a_view_that_shows_every_radius_or_has_no_frame_states_none() { + let main = |w: f64, h: f64| Frame { + min_x: 0.0, + min_y: 0.0, + width: w, + height: h, + }; + // Width 100: the main preview scale is 2.86, the twin's floor of 2 units shows 11 px. + assert_eq!(min_point_radius(&main(100.0, 100.0), true), None); + // Scale of exactly 1: the floor shows exactly 4 px, no radius fails. + assert_eq!(min_point_radius(&main(286.0, 310.0), true), None); + assert!(min_point_radius(&main(286.1, 310.0), true).is_some()); + let mut doc = parse_json(&document_text()).unwrap(); + doc["views"][1]["view_box"] = json!({"min_x": 0, "min_y": 0, "width": 0.5, "height": 0.5}); + let draft = input(PreviewStatus::Draft, doc.to_string(), None); + let sheet = PreviewSheet::load(draft, vec![horse_drawing()]).unwrap(); + assert_eq!( + state(&sheet).min_point_radius.keys().collect::>(), + ["main"] + ); +} + +/// A point of the draft with the radius `r`, centred in the middle of its view's frame. +fn point_with(frame: &Frame, view: &str, r: f64) -> Annotation { + let mut point = parse_json(&document_text()).unwrap()["annotations"][0].clone(); + assert_eq!(point["geometry"]["type"], json!("circle")); + point["view_id"] = json!(view); + point["geometry"] = json!({ + "type": "circle", + "cx": frame.min_x + frame.width / 2.0, + "cy": frame.min_y + frame.height / 2.0, + "r": r + }); + serde_json::from_value(point).unwrap() +} + +#[test] +fn the_stated_radius_passes_and_a_hundredth_less_is_too_small() { + let sheet = published(); + let radii = state(&sheet).min_point_radius; + for (id, main, frame) in equine_frames(&sheet) { + let placement = |r: f64| { + let point = point_with(&frame, &id, r); + annotation_placement(&point, &frame, main, &mut Budget::default()) + }; + assert_eq!(placement(radii[&id]), Placement::InFrame, "{id}"); + assert_eq!(placement(radii[&id] - 0.01), Placement::TooSmall, "{id}"); + } +} diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 94e3a434..325f37aa 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3953,6 +3953,7 @@ trapping test stub (`apps/app/tests/helpers/core.ts`) lists the two new glue mem | Measure | Value | |---|---| | shipped module | 1 513 522 B raw, 559 360 B gzip -9 (script), 569 181 B by Node's gzip in `size.test.ts` (final, finalisation §12.8; 568 696 B at f1ef6a4, the debug `name` section dropped since §12.7); budget 600 000 (D-R13); 596 671 B at ca31b78, 531 715 B before V1-2.2 (RUST.md §11.22) | +| shipped module, V1-2.6 U2 (`PreviewState.min_point_radius`) | 1 580 788 B raw, 585 951 B gzip -9 (script), 595 331 B by Node's gzip in `size.test.ts`; before: 1 569 864 B, 583 126 B, 592 143 B (+3 188 B by the test's measure, +2 825 B by the script); margin under the 600 000 budget 4 669 B | | verdict at every cap, native | 107 ms release, 493 ms debug (document 1 165 850 bytes) | | `clean_svg`, shipped module | 45 hostile inputs and floods, slowest 0.4 ms | diff --git a/packages/core-wasm/tests/contracts.types.test.ts b/packages/core-wasm/tests/contracts.types.test.ts index 2365b808..c6b2c2f6 100644 --- a/packages/core-wasm/tests/contracts.types.test.ts +++ b/packages/core-wasm/tests/contracts.types.test.ts @@ -22,6 +22,7 @@ import type { Finding, Observation, PdfLabelSet, + PreviewState, Report, ReportArchiveEnvelope, ReportZone, @@ -60,4 +61,11 @@ describe('ts-rs contract types', () => { expectTypeOf().toMatchTypeOf() expectTypeOf().toMatchTypeOf() }) + + // The preview state's per-view minimum point radius: view id to a number, none when absent. + it('give the preview state a radius per view id', () => { + expectTypeOf().toEqualTypeOf< + Record + >() + }) }) diff --git a/packages/core-wasm/types/PreviewState.ts b/packages/core-wasm/types/PreviewState.ts index 27048036..eeafb893 100644 --- a/packages/core-wasm/types/PreviewState.ts +++ b/packages/core-wasm/types/PreviewState.ts @@ -13,6 +13,12 @@ export type PreviewState = { status: PreviewStatus, * Recomputed; absent when the document has an integrity error. */ fingerprint: string | null, report: SheetReport, drawings: Array, index: PreviewIndex | null, +/** + * The smallest hitbox radius of a point in each view, by view id: the radius whose visible + * disc just passes the minimum size on the smallest preview. Absent for a view without a + * usable frame or drawing, and for one where every radius passes. + */ +min_point_radius: { [key in string]: number }, /** * The document as read (defaults filled in). */