diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 7cfc7d8a..8d50d98b 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -109,6 +109,10 @@ required-features = ["pdf-read", "pdf-write"] name = "pdf_serialize" required-features = ["pdf-read", "pdf-write"] +[[test]] +name = "pdf_proportions" +required-features = ["pdf-write"] + [[test]] name = "pdf_session" required-features = ["pdf-read", "pdf-write"] diff --git a/crates/core/src/pdf/write/views.rs b/crates/core/src/pdf/write/views.rs index 2307c311..35fac32b 100644 --- a/crates/core/src/pdf/write/views.rs +++ b/crates/core/src/pdf/write/views.rs @@ -84,6 +84,40 @@ pub fn raster_size_of(view_box: Option<&str>) -> (u32, u32) { (RASTER_WIDTH_PX, height as u32) } +/// The raster size of a view (owner 2026-11-22, A5): the frame's ratio to half a pixel for +/// every ratio, so `meet` shows nothing outside the frame (R2). The short side is rounded first +/// and the long side follows from it; never more pixels than 1 200 x 2 400. From 1:1 to 1:2 +/// that is `raster_size_of`, whose rounding already holds the bound. Logos keep +/// `raster_size_of`. +pub fn view_raster_size(view_box: Option<&str>) -> (u32, u32) { + let aspect = aspect_of(view_box); + if aspect > ASPECT_MAX { + return tall_size(aspect); + } + if aspect < 1.0 { + return wide_size(aspect); + } + raster_size_of(view_box) +} + +/// More than twice as high as wide: 2 400 px high at most, the width rounded, then the height +/// recomputed from that width. +fn tall_size(aspect: f64) -> (u32, u32) { + let max_h = f64::from(RASTER_WIDTH_PX) * ASPECT_MAX; + let width = (max_h / aspect + 0.5).floor().max(1.0); + let height = (width * aspect + 0.5).floor().clamp(1.0, max_h); + (width as u32, height as u32) +} + +/// Wider than high: 1 200 px wide at most, the height rounded, then the width recomputed from +/// that height. +fn wide_size(aspect: f64) -> (u32, u32) { + let max_w = f64::from(RASTER_WIDTH_PX); + let height = (max_w * aspect + 0.5).floor().max(1.0); + let width = (height / aspect + 0.5).floor().clamp(1.0, max_w); + (width as u32, height as u32) +} + pub fn logo_key(slot: LogoSlot) -> String { format!("logo:{}", slot.as_str()) } @@ -92,8 +126,8 @@ pub fn view_key(observation_id: &str, view_id: &str) -> String { format!("view:{observation_id}:{view_id}") } -fn request(key: String, root: &Element) -> RasterRequest { - let (width_px, height_px) = raster_size_of(root.attr("viewBox")); +fn request(key: String, root: &Element, size_of: fn(Option<&str>) -> (u32, u32)) -> RasterRequest { + let (width_px, height_px) = size_of(root.attr("viewBox")); RasterRequest { key, svg: serialize(root), @@ -110,7 +144,7 @@ pub fn logo_request(slot: LogoSlot, asset: Option<&ImageAsset>) -> Option None, ImageAsset::Svg(svg) => { let root = crate::anatomy::svg::read_svg(&svg.svg).ok()?.root; - Some(request(logo_key(slot), &root)) + Some(request(logo_key(slot), &root, raster_size_of)) } } } @@ -153,5 +187,9 @@ pub fn view_request( view.is_main(), ) .ok()?; - Some(request(view_key(&observation.id, view_id), &composed)) + Some(request( + view_key(&observation.id, view_id), + &composed, + view_raster_size, + )) } diff --git a/crates/core/tests/pdf_proportions.rs b/crates/core/tests/pdf_proportions.rs new file mode 100644 index 00000000..b0b75e71 --- /dev/null +++ b/crates/core/tests/pdf_proportions.rs @@ -0,0 +1,94 @@ +//! The PDF keeps every view's proportions and shows nothing outside its frame (owner +//! 2026-11-22, README « Conséquences » 2, R2, A5). +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use faunosteo_core::pdf::write::views::{raster_size_of, view_raster_size}; + +/// What `meet` leaves of the drawing beyond a frame of width 1 and height `aspect`, centred in +/// a raster of `width` × `height` px: the largest band, in raster px. +fn beyond_frame_px(width: u32, height: u32, aspect: f64) -> f64 { + let (w, h) = (f64::from(width), f64::from(height)); + let scale = w.min(h / aspect); + ((w - scale) / 2.0).max((h - scale * aspect) / 2.0) +} + +fn box_of(aspect: f64) -> String { + format!("0 0 1000 {}", 1000.0 * aspect) +} + +#[test] +fn every_view_has_its_frame_ratio() { + let mut aspects = vec![ + 100.0, + 50.5, + 49.5, + 7.1, + 3.0, + 2.0001, + 2.0, + 1.0, + 0.7859, + 0.76, + 0.2195, + 0.2, + 1.0 / 7.0, + 0.01, + ]; + for k in 0..=2000 { + aspects.push(0.01 * 10_000f64.powf(f64::from(k) / 2000.0)); + } + for aspect in aspects { + let (w, h) = view_raster_size(Some(&box_of(aspect))); + assert!( + beyond_frame_px(w, h, aspect) <= 0.25 + 1e-9, + "h/w {aspect}: {w} x {h}" + ); + assert!( + w <= 1200 && h <= 2400 && w >= 1 && h >= 1, + "h/w {aspect}: {w} x {h}" + ); + } +} + +#[test] +fn views_higher_than_wide_up_to_1_2_keep_todays_size() { + for step in 0..=100 { + let view_box = box_of(1.0 + f64::from(step) * 0.01); + assert_eq!( + view_raster_size(Some(&view_box)), + raster_size_of(Some(&view_box)), + "{view_box}" + ); + } +} + +#[test] +fn the_historical_views_keep_their_sizes() { + assert_eq!(view_raster_size(Some("0 0 2073.4 1629.4")), (1200, 943)); // main view + assert_eq!(view_raster_size(Some("60 100 500 380")), (1200, 912)); // head + assert_eq!(view_raster_size(None), raster_size_of(None)); +} + +#[test] +fn the_board_frames_get_their_sizes() { + assert_eq!(view_raster_size(Some("625 969 220 660")), (800, 2400)); // « Membre antérieur », 1:3 + assert_eq!(view_raster_size(Some("770 458 980 140")), (1197, 171)); // « Ligne du dos », 7:1 + assert_eq!(view_raster_size(Some(&box_of(100.0))), (24, 2400)); + assert_eq!(view_raster_size(Some(&box_of(0.01))), (1200, 12)); +} + +/// For the owner's report (A5: « contrôler les éventuelles différences »): the O4 inputs whose +/// view size differs from the logo rule, printed; none of them is a historical view. +#[test] +fn the_o4_inputs_where_views_differ_from_logos_are_listed() { + let (_, oracle) = common::oracle_file("r4/text.json"); + for case in oracle["rasterSizeOf"]["cases"].as_array().unwrap() { + let input = case[0].as_str(); + let (view, logo) = (view_raster_size(input), raster_size_of(input)); + if view != logo { + println!("O4 {input:?}: logo {logo:?}, view {view:?}"); + } + } +} diff --git a/crates/core/tests/pdf_session.rs b/crates/core/tests/pdf_session.rs index 7ae27fa8..52c954cf 100644 --- a/crates/core/tests/pdf_session.rs +++ b/crates/core/tests/pdf_session.rs @@ -24,7 +24,9 @@ use faunosteo_core::pdf::write::input::{ use faunosteo_core::pdf::write::session::{ ExportSession, ExportStep, SessionError, SessionRefusal, keyed, }; -use faunosteo_core::pdf::write::views::{RASTER_WIDTH_PX, RasterRequest, raster_size_of}; +use faunosteo_core::pdf::write::views::{ + RASTER_WIDTH_PX, RasterRequest, raster_size_of, view_raster_size, +}; use serde_json::{Value, json}; const TWO_BY_ONE_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC"; @@ -168,7 +170,7 @@ fn the_requests_are_the_svg_logos_then_each_observations_views_main_first() { assert_eq!(request.svg, serialize(&composed), "{}", request.key); assert_eq!( (request.width_px, request.height_px), - raster_size_of(composed.attr("viewBox")), + view_raster_size(composed.attr("viewBox")), "{}", request.key );