diff --git a/crates/core/src/pdf/write/layout/observation.rs b/crates/core/src/pdf/write/layout/observation.rs index 4fcff73e..ddfe0e77 100644 --- a/crates/core/src/pdf/write/layout/observation.rs +++ b/crates/core/src/pdf/write/layout/observation.rs @@ -1,6 +1,7 @@ //! `render/observationPage.ts`: one page per observation, continued on the next pages when its -//! text does not fit (A3): header, title block with the severity pill, the image grid of the observation's views, then the sections (zones, -//! findings by zone and side, free findings, severity, comment). +//! text does not fit (A3): header, title block with the severity pill, the image grid of the +//! observation's views, then the sections (zones, findings by zone and side, free findings, +//! severity, comment). use std::collections::HashMap; @@ -812,6 +813,16 @@ mod tests { .collect() } + #[test] + fn row_budget_flow_branch_gives_each_row_its_share_of_the_page() { + // 8 rows with 60 pt of room: the share is under the minimum, so the minimum. + assert!((row_budget(8, 687.89, 60.0) - IMAGE_MIN_CELL_H).abs() < 1e-9); + // 1 row with 60 pt of room: what the page leaves above the first section. + let page = 687.89 - IMAGE_BOTTOM_GAP - TEXT_TOP_GAP - FIRST_LINE - TEXT_FLOOR_Y; + let expected = page - IMAGE_LABEL_GAP; + assert!((row_budget(1, 687.89, 60.0) - expected).abs() < 1e-9); + } + fn row_embeds(rendered: &[Rendered]) -> Vec> { rows_of(rendered) .iter() diff --git a/crates/core/tests/pdf_proportions.rs b/crates/core/tests/pdf_proportions.rs index d5bb0217..b7e22f44 100644 --- a/crates/core/tests/pdf_proportions.rs +++ b/crates/core/tests/pdf_proportions.rs @@ -111,7 +111,7 @@ fn the_o4_inputs_where_views_differ_from_logos_are_listed() { // --------------------------------------------------------------------------- // The case builder (reused by the page-flow and the editor-preview tests) -use common::r4::{PngKeeper, repo_root}; +use common::r4::{PngKeeper, repo_root, sha256_hex}; use faunosteo_core::domain::archive::build_sheet_snapshot; use faunosteo_core::domain::js::parse_json; use faunosteo_core::domain::report::Report; @@ -335,6 +335,12 @@ fn demo_envelope() -> Value { /// The export input and the header-only rasters of a case. pub fn proportions_input(case: &Case) -> (CheckedInput, Rasters) { + let (input, rasters) = proportions_export_input(case); + (check_input(input).unwrap(), rasters) +} + +/// The same, before the input is checked (the review tool writes it as JSON). +fn proportions_export_input(case: &Case) -> (ExportInput, Rasters) { let sheet = case_sheet(case); let envelope = demo_envelope(); let zones = case_zones(case); @@ -378,7 +384,7 @@ pub fn proportions_input(case: &Case) -> (CheckedInput, Rasters) { locale: serde_json::from_value(envelope["locale"].clone()).unwrap(), exported_at: envelope["exportedAt"].as_str().unwrap().to_owned(), }; - (check_input(input).unwrap(), rasters) + (input, rasters) } // --------------------------------------------------------------------------- @@ -628,7 +634,6 @@ fn mixed_views_with_a_long_note_keep_order_ratios_and_the_sixty_points() { "{count} views" ); assert_clean(&drawn); - println!("{count} views: {} pages", drawn.pages.len()); } } @@ -650,12 +655,6 @@ fn eight_seven_to_one_views_paginate_before_going_under_sixty_points() { let drawn = draw(&case); assert!(observation_pages(&drawn).len() >= 2); assert_images_hold(&drawn, 8); - let heights: Vec = all_images(&drawn).iter().map(|i| i.height).collect(); - let lowest = heights.iter().copied().fold(f64::INFINITY, f64::min); - println!( - "8 x 7:1: {} pages, lowest image {lowest:.3} pt", - drawn.pages.len() - ); // Every row is before any text of the sections. let calls: Vec<(usize, &DrawOp)> = observation_pages(&drawn) .iter() @@ -699,6 +698,7 @@ fn the_footers_count_the_continuation_pages_and_the_conclusion_comes_last() { .iter() .all(|p| eyebrow_of(p) != last) ); + assert_clean(&drawn); } #[test] @@ -723,3 +723,138 @@ fn a_long_view_name_in_the_right_column_stays_inside_the_page() { assert!((label.x - 305.64).abs() < 1e-9); assert_clean(&drawn); } + +// --------------------------------------------------------------------------- +// The reference cases (Task 28): shown to the owner before they are frozen + +const CASE_NAMES: [&str; 5] = [ + "tall-1-3", + "wide-7-1", + "four-long", + "wide-and-two", + "flow-long", +]; + +const ARM: [f64; 4] = [625.0, 969.0, 220.0, 660.0]; +const BACK: [f64; 4] = [770.0, 458.0, 980.0, 140.0]; +const SHOULDER: [f64; 4] = [584.0, 757.0, 400.0, 300.0]; + +/// A reference case of the board, by name. +fn reference_case(name: &str) -> Case { + let (views, chars) = match name { + "tall-1-3" => (vec![main_view(), crop("Membre antérieur", ARM, 1)], 100), + "wide-7-1" => (vec![crop("Ligne du dos", BACK, 1)], 100), + "four-long" => ( + vec![ + main_view(), + head_view(), + crop("Membre antérieur", ARM, 1), + crop("Épaule", SHOULDER, 1), + ], + 1500, + ), + "wide-and-two" => ( + vec![main_view(), head_view(), crop("Ligne du dos", BACK, 1)], + 100, + ), + "flow-long" => (vec![main_view(), head_view()], 5000), + other => panic!("unknown case {other}"), + }; + Case { + views, + comment: french_comment(chars), + } +} + +#[test] +fn every_reference_case_passes_the_census() { + for name in CASE_NAMES { + let drawn = draw(&reference_case(name)); + assert_clean(&drawn); + } +} + +fn color(c: &faunosteo_core::pdf::write::draw_log::Rgb) -> Value { + json!({ "type": "RGB", "red": c.red, "green": c.green, "blue": c.blue }) +} + +/// One op in the oracle's shape (the form of `pdf_layout.rs`). +fn op_json(op: &DrawOp, pngs: &[Vec]) -> Value { + match op { + DrawOp::Text(t) => json!({ "op": "drawText", "text": t.text, "options": { + "x": t.x, "y": t.y, "size": t.size, "font": t.font.as_str(), "color": color(&t.color) } }), + DrawOp::Image(i) => json!({ "op": "drawImage", + "image": { "embedIndex": i.embed, "pngSha256": sha256_hex(&pngs[i.embed]) }, + "options": { "x": i.x, "y": i.y, "width": i.width, "height": i.height } }), + DrawOp::Line(l) => json!({ "op": "drawLine", "options": { + "start": { "x": l.start.x, "y": l.start.y }, "end": { "x": l.end.x, "y": l.end.y }, + "thickness": l.thickness, "color": color(&l.color) } }), + DrawOp::Rectangle(r) => json!({ "op": "drawRectangle", "options": { + "x": r.x, "y": r.y, "width": r.width, "height": r.height, "color": color(&r.color) } }), + } +} + +/// The whole draw log of a case at full precision: every page's calls in order. +fn draw_log_json(case: &Case) -> Value { + let (checked, rasters) = proportions_input(case); + let mut keeper = PngKeeper::default(); + let drawn = layout(&checked, &rasters, &mut keeper).unwrap(); + let pages: Vec = drawn + .log + .pages + .iter() + .map(|page| { + let ops: Vec = page.ops.iter().map(|o| op_json(o, &keeper.pngs)).collect(); + json!({ "ops": ops }) + }) + .collect(); + json!({ "pages": pages }) +} + +fn oracle_path(name: &str) -> std::path::PathBuf { + repo_root().join(format!( + "crates/core/tests/oracles/proportions/draw.{name}.json" + )) +} + +/// The references are compared once the owner has approved them; `MAKE_PROPORTION_ORACLES=1` +/// writes them (once, after the owner's go). +#[test] +#[ignore = "reference not yet approved by the owner"] +fn the_reference_cases_draw_what_the_owner_approved() { + let make = std::env::var("MAKE_PROPORTION_ORACLES").is_ok_and(|v| v == "1"); + for name in CASE_NAMES { + let actual = draw_log_json(&reference_case(name)); + let path = oracle_path(name); + if make { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, serde_json::to_string_pretty(&actual).unwrap()).unwrap(); + continue; + } + let text = std::fs::read_to_string(&path) + .unwrap_or_else(|_| panic!("{path:?} missing: MAKE_PROPORTION_ORACLES=1")); + let expected: Value = serde_json::from_str(&text).unwrap(); + assert!(expected == actual, "{name}: the draw log changed"); + } +} + +/// Writes every case's `ExportInput` JSON into `$PROPORTION_REVIEW_DIR`, for the owner's +/// renders (a scratch Node script draws the real drawings; nothing of it is committed). +#[test] +#[ignore = "writes files for the owner's review"] +fn write_review_inputs() { + let dir = std::env::var("PROPORTION_REVIEW_DIR").expect("PROPORTION_REVIEW_DIR"); + std::fs::create_dir_all(&dir).unwrap(); + for name in CASE_NAMES { + let (input, _) = proportions_export_input(&reference_case(name)); + let path = std::path::Path::new(&dir).join(format!("{name}.input.json")); + std::fs::write(path, serde_json::to_string(&input).unwrap()).unwrap(); + let drawn = draw(&reference_case(name)); + let images: Vec = all_images(&drawn).iter().map(|i| i.height).collect(); + let lowest = images.iter().copied().fold(f64::INFINITY, f64::min); + println!( + "{name}: {} pages, lowest image {lowest:.3} pt", + drawn.pages.len() + ); + } +}