diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index d0aa917e5e..ba83abcf3a 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -28,6 +28,7 @@ use chart::*; mod elements; pub(crate) use elements::*; mod export; +pub(in crate::cmd) use export::chart_notice_lines_in; pub use export::*; mod files; pub(crate) use files::*; diff --git a/crates/engine/src/cmd/layout/export.rs b/crates/engine/src/cmd/layout/export.rs index e9c8f97e12..9c721f49df 100644 --- a/crates/engine/src/cmd/layout/export.rs +++ b/crates/engine/src/cmd/layout/export.rs @@ -93,19 +93,32 @@ pub fn layout_png(s: &mut Session, li: usize, dpi: f64, region: Option<[f64; 4]> img.encode_png().map_err(|e| e.to_string()) } -/// [`chart_notices`] of the charts of `layout` a drawing of it drew, one notice for each text, -/// in the order first met, naming the charts it is true of by name and id, each once whatever the -/// pages: the first ten, and how many more. +/// [`chart_notices`] of the charts of `layout` a drawing of it drew, as [`chart_notice_lines_in`] +/// words them. fn chart_notice_lines(layout: &Layout, drawn: &[rustgis_render::layout::DrawnChart]) -> Vec { - let mut said: Vec<(String, Vec<(u32, String)>)> = Vec::new(); - for (id, data) in drawn { + chart_notice_lines_in(drawn.iter().map(|chart| (None, layout, chart))) +} + +/// [`chart_notices`] of each chart of `drawn` in its layout, one notice for each text, in the +/// order first met, naming the charts it is true of by name and id, each once whatever the pages: +/// the first ten, and how many more. A chart given its layout's index, as a report's pages give +/// it, is named with that layout, and is another chart than one of the same id in another layout. +pub(in crate::cmd) fn chart_notice_lines_in<'a>( + drawn: impl IntoIterator, &'a Layout, &'a rustgis_render::layout::DrawnChart)>, +) -> Vec { + let mut said: Vec<(String, Vec<((Option, u32), String)>)> = Vec::new(); + for (li, layout, (id, data)) in drawn { let Some(e) = layout.element(*id) else { continue }; - let named = (*id, format!("\"{}\" ({id})", crate::project::short_name(&e.name))); + let mut named = format!("\"{}\" ({id})", crate::project::short_name(&e.name)); + if li.is_some() { + named.push_str(&format!(" in \"{}\"", crate::project::short_name(&layout.name))); + } + let chart = (li, *id); for note in chart_notices(e, data, false) { match said.iter_mut().find(|(n, _)| *n == note) { - Some((_, charts)) if charts.iter().any(|(at, _)| at == id) => {} - Some((_, charts)) => charts.push(named.clone()), - None => said.push((note, vec![named.clone()])), + Some((_, charts)) if charts.iter().any(|(at, _)| *at == chart) => {} + Some((_, charts)) => charts.push((chart, named.clone())), + None => said.push((note, vec![(chart, named.clone())])), } } } diff --git a/crates/engine/src/cmd/report.rs b/crates/engine/src/cmd/report.rs index 1f5f869d84..8e90a58b1d 100644 --- a/crates/engine/src/cmd/report.rs +++ b/crates/engine/src/cmd/report.rs @@ -1167,8 +1167,9 @@ fn report_pages_bytes( let mut out = Vec::new(); let mut held = 0usize; let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); - // The legends its layouts' pages drew under a fit still cut, by layout ([`keep_unfitted`]). - let mut unfitted = Vec::new(); + // The legends its layouts' pages drew under a fit still cut, and the charts they drew, by + // layout ([`keep_drawn`]). + let (mut unfitted, mut charts) = (Vec::new(), Vec::new()); // Counted, and stopped on Cancel, a page at a time. rustgis_data::progress::begin(n); for k in 0..n { @@ -1178,28 +1179,29 @@ fn report_pages_bytes( _ => None, }; if !pdf { - let (drawn, listed) = rustgis_render::layout::unfitted_legends(|| { - rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps) - }); + let (drawn, listed, shown) = + listing(|| rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (img, ..) = drawn?; - keep_unfitted(&mut unfitted, page_layout, listed); + keep_drawn(&mut unfitted, page_layout, listed); + keep_drawn(&mut charts, page_layout, shown); say_report_unfitted(s, &unfitted); + say_report_charts(s, &charts); say_sections_not_drawn(s, pages, 1); return Ok((img.encode_png().map_err(|e| e.to_string())?, pages.len())); } - let (drawn, listed) = rustgis_render::layout::unfitted_legends(|| { - rustgis_render::report::pdf_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps) - }); + let (drawn, listed, shown) = + listing(|| rustgis_render::report::pdf_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (page, pictured) = drawn?; - keep_unfitted(&mut unfitted, page_layout, listed); + keep_drawn(&mut unfitted, page_layout, listed); + keep_drawn(&mut charts, page_layout, shown); if page.overflowed() { // Too large for vectors all the same: the page as one picture. pages_pictured.push(k + 1); - let (drawn, listed) = rustgis_render::layout::unfitted_legends(|| { - rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps) - }); + let (drawn, listed, shown) = + listing(|| rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (img, w, h) = drawn?; - keep_unfitted(&mut unfitted, page_layout, listed); + keep_drawn(&mut unfitted, page_layout, listed); + keep_drawn(&mut charts, page_layout, shown); out.push(rustgis_render::pdf::Page::new(&img, w, h)); } else { frames_pictured.extend(pictured); @@ -1211,6 +1213,7 @@ fn report_pages_bytes( } } say_report_unfitted(s, &unfitted); + say_report_charts(s, &charts); super::layout::pictured_notices(s, &frames_pictured, &pages_pictured); say_sections_not_drawn(s, pages, n); Ok((rustgis_render::pdf::write(&out, &doc.title), pages.len())) @@ -1239,16 +1242,19 @@ fn say_sections_not_drawn(s: &mut Session, pages: &[rustgis_render::report::Page } } -/// Keeps the legends `listed` as cut on a page that draws the layout `layout` (by its index), each -/// with that layout: the ids are a layout's own, so two layouts' legends sharing one are two. Only -/// an exact repeat (the same legend of the same layout, cut the same way) is dropped: the page -/// drawn again as a picture, or the layout on another page of the report. A page that draws no -/// layout draws no legend. -fn keep_unfitted( - kept: &mut Vec<(usize, rustgis_render::layout::UnfittedLegend)>, - layout: Option, - listed: Vec, -) { +/// Runs `f`, and returns its result with the legends it drew under a fit still cut and the charts +/// it drew. +fn listing(f: impl FnOnce() -> T) -> (T, Vec, Vec) { + let ((drawn, legends), charts) = rustgis_render::layout::drawn_charts(|| rustgis_render::layout::unfitted_legends(f)); + (drawn, legends, charts) +} + +/// Keeps what a page that draws the layout `layout` (by its index) `listed`, a legend cut under +/// its fit or a chart drawn, each with that layout: the ids are a layout's own, so two layouts' +/// elements sharing one are two. Only an exact repeat (the same element of the same layout, cut +/// the same way or drawn with the same data) is dropped: the page drawn again as a picture, or +/// the layout on another page of the report. A page that draws no layout lists nothing. +fn keep_drawn(kept: &mut Vec<(usize, T)>, layout: Option, listed: Vec) { let Some(li) = layout else { return }; for l in listed { let item = (li, l); @@ -1258,7 +1264,7 @@ fn keep_unfitted( } } -/// Says the legends [`keep_unfitted`] kept, each named with its layout, as a layout's export +/// Says the legends [`keep_drawn`] kept, each named with its layout, as a layout's export /// says its own ([`super::layout::say_unfitted_legends_in`]). fn say_report_unfitted(s: &mut Session, kept: &[(usize, rustgis_render::layout::UnfittedLegend)]) { let named: Vec<_> = @@ -1266,6 +1272,15 @@ fn say_report_unfitted(s: &mut Session, kept: &[(usize, rustgis_render::layout:: super::layout::say_unfitted_legends_in(s, &named); } +/// Says what the charts [`keep_drawn`] kept leave out, each named with its layout, as a layout's +/// export says its own ([`super::layout::chart_notice_lines_in`]). +fn say_report_charts(s: &mut Session, kept: &[(usize, rustgis_render::layout::DrawnChart)]) { + let drawn = kept.iter().filter_map(|(li, chart)| Some((Some(*li), s.project.layouts.get(*li)?, chart))); + for note in super::layout::chart_notice_lines_in(drawn) { + s.warn(note); + } +} + /// Resolves the report creator's string-name or active/default layer convention. fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Layer, String> { let context = input.input_context(); @@ -1519,12 +1534,12 @@ mod tests { too_tall: true, }; let mut kept = Vec::new(); - super::keep_unfitted(&mut kept, Some(0), vec![cut(1)]); + super::keep_drawn(&mut kept, Some(0), vec![cut(1)]); // The vector page, then the same page drawn as a picture. - super::keep_unfitted(&mut kept, Some(0), vec![cut(1)]); - super::keep_unfitted(&mut kept, Some(1), vec![cut(1)]); - super::keep_unfitted(&mut kept, Some(0), vec![cut(2)]); - super::keep_unfitted(&mut kept, None, vec![cut(3)]); + super::keep_drawn(&mut kept, Some(0), vec![cut(1)]); + super::keep_drawn(&mut kept, Some(1), vec![cut(1)]); + super::keep_drawn(&mut kept, Some(0), vec![cut(2)]); + super::keep_drawn(&mut kept, None, vec![cut(3)]); assert_eq!(kept, [(0, cut(1)), (1, cut(1)), (0, cut(2))]); } } diff --git a/crates/engine/src/tests/layout_chart_notices.rs b/crates/engine/src/tests/layout_chart_notices.rs index caaf94e92e..cd46249b78 100644 --- a/crates/engine/src/tests/layout_chart_notices.rs +++ b/crates/engine/src/tests/layout_chart_notices.rs @@ -179,3 +179,102 @@ fn a_chart_with_bars_under_a_point_says_so() { assert_eq!(exported(&mut s, "svg"), [want], "{key}"); } } + +/// A report "R" over Shops that draws each of `layouts` as a page after its own pages. +fn report(s: &mut Session, layouts: &[&str]) { + s.run("report.new", &json!({"layer": "Shops", "name": "R"})).unwrap(); + for layout in layouts { + s.run("report.existingTemplate", &json!({"report": "R", "layout": layout, "end": true})).unwrap(); + } +} + +/// Removed however the test ends. +struct Dir(std::path::PathBuf); + +impl Drop for Dir { + fn drop(&mut self) { + std::fs::remove_dir_all(&self.0).ok(); + } +} + +/// What exporting the report "R" to a file of `ext` says of charts. +fn reported(s: &mut Session, ext: &str) -> Vec { + let dir = std::env::temp_dir().join(format!("rustgis-3166-{}-{:?}", std::process::id(), std::thread::current().id())); + std::fs::create_dir_all(&dir).unwrap(); + let dir = Dir(dir); + let path = dir.0.join(format!("report.{ext}")); + let mut all = said(s, "report.export", json!({"report": "R", "path": path, "dpi": 36})); + all.retain(|t| t.contains("chart")); + all +} + +/// What a report says of the chart `id` of the layout `li`'s `note`. +fn of_page(s: &Session, li: usize, id: u64, note: &str) -> String { + let layout = &s.project.layouts[li]; + let name = &layout.element(id as u32).unwrap().name; + format!("Chart \"{name}\" ({id}) in \"{}\": {note}", layout.name) +} + +const TWO_OF_THREE: &str = + "the chart draws 2 of 3 bars: 1 left out (\"bars\" draws up to 100, \"other\": true gathers the rest in an Other bar)"; + +#[test] +fn a_report_says_what_the_charts_of_its_layout_pages_leave_out() { + let mut s = shops(); + let id = chart(&mut s, json!({"bars": 2})); + // The same layout twice: said once. + report(&mut s, &["Charts", "Charts"]); + let want = of_page(&s, 0, id, TWO_OF_THREE); + assert_eq!(reported(&mut s, "pdf"), std::slice::from_ref(&want)); + // A project opened again says it too. + let mut again = Session::new(serde_json::from_value(serde_json::to_value(&s.project).unwrap()).unwrap()); + assert_eq!(reported(&mut again, "pdf"), std::slice::from_ref(&want)); + // The layout's own export names no layout. + assert_eq!(exported(&mut s, "pdf"), [of(&s, id, TWO_OF_THREE)]); +} + +#[test] +fn a_report_image_says_the_charts_of_its_first_page_alone() { + let mut s = shops(); + let id = chart(&mut s, json!({"bars": 2})); + report(&mut s, &["Charts"]); + assert!(reported(&mut s, "png").is_empty(), "the layout is not on the first page"); + s.run("report.existingTemplate", &json!({"report": "R", "layout": "Charts"})).unwrap(); + assert_eq!(reported(&mut s, "png"), [of_page(&s, 0, id, TWO_OF_THREE)]); +} + +#[test] +fn a_report_names_charts_of_two_layouts_with_the_same_notice_in_one() { + let mut s = shops(); + let first = chart(&mut s, json!({"bars": 2})); + s.run("layout.new", &json!({"name": "More"})).unwrap(); + let second = chart(&mut s, json!({"layout": 1, "bars": 2})); + assert_eq!(first, second, "each layout numbers its own elements"); + report(&mut s, &["Charts", "More"]); + let named = |li: usize| { + let name = &s.project.layouts[li].element(first as u32).unwrap().name; + format!("\"{name}\" ({first}) in \"{}\"", s.project.layouts[li].name) + }; + let want = format!("2 charts ({}, {}): {TWO_OF_THREE}", named(0), named(1)); + assert_eq!(reported(&mut s, "pdf"), [want]); +} + +#[test] +fn a_report_says_a_chart_box_too_small_for_it() { + let mut s = shops(); + let id = chart(&mut s, json!({"orientation": "vertical", "values": true, "h": 10})); + report(&mut s, &["Charts"]); + assert_eq!(reported(&mut s, "pdf"), [of_page(&s, 0, id, COLUMNS)]); +} + +#[test] +fn a_report_says_nothing_of_charts_with_no_losses_or_with_no_layout_page() { + let mut s = shops(); + let id = chart(&mut s, json!({})); + report(&mut s, &[]); + assert!(reported(&mut s, "pdf").is_empty(), "no layout page"); + s.run("report.existingTemplate", &json!({"report": "R", "layout": "Charts"})).unwrap(); + assert!(reported(&mut s, "pdf").is_empty(), "nothing lost"); + s.run("layout.update", &json!({"element": id, "bars": 2, "visible": false})).unwrap(); + assert!(reported(&mut s, "pdf").is_empty(), "hidden"); +} diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 27bfeea6d2..a704104693 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -784,10 +784,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu - **What a chart says**: exporting a layout (or the Assistant viewing it) says again what each chart it shows leaves out, and whether its box is too small for it, read as the exported page draws it, on every page of a map series it exports: charts with the same notice are named together in one, by name and element - number, the first ten and how many more there are. A box is too small when columns are left no height by their labels - and values (the chart shows its placeholder), or when bars, or the room they run in, come under - a point. The chart also says that when it is added, resized, retitled, turned, or given or - denied values or labels. + number, the first ten and how many more there are. Exporting a report that draws a layout as a + page says the same of that page's charts, naming each with its layout. A box is too small when + columns are left no height by their labels and values (the chart shows its placeholder), or when + bars, or the room they run in, come under a point. The chart also says that when it is added, + resized, retitled, turned, or given or denied values or labels. - **Facing charts**: two charts side by side compare two fields per category back to back, such as the share of businesses against the share of people in each district. **Mirror** draws a chart as its mirror image, its text reading as ever: bars running across start from the right