diff --git a/crates/engine/src/cmd/presentation.rs b/crates/engine/src/cmd/presentation.rs index a4106f4f2a..219cbf2763 100644 --- a/crates/engine/src/cmd/presentation.rs +++ b/crates/engine/src/cmd/presentation.rs @@ -68,6 +68,19 @@ pub fn commands() -> Vec { if let Some(cut) = cut { s.notify(cut, false); } + // Pages often share a title's figure: each warning once, with its count of pages. + let mut said: Vec<(String, usize)> = Vec::new(); + for k in 0..s.project.presentations[i].pages.len() { + for w in title_statistic_notices(s, i, k) { + match said.iter_mut().find(|(text, _)| *text == w) { + Some((_, pages)) => *pages += 1, + None => said.push((w, 1)), + } + } + } + for (w, pages) in said { + s.warn(if pages > 1 { format!("{w} (in {pages} map pages)") } else { w }); + } Ok(json!({"presentation": i, "name": name})) }).roles(&[crate::ParamRole { key: "text", role: crate::Role::Document(crate::Document::Presentation) }]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .params(r#"{"path"?: "*.rgispres", "text"?: presentation JSON, "data"?: base64 file}"#), @@ -95,10 +108,14 @@ pub fn commands() -> Vec { let center = Coord::new(f(p, "lon").unwrap_or(c.x).clamp(-180.0, 180.0), f(p, "lat").unwrap_or(c.y).clamp(-85.0, 85.0)); let width_m = f(p, "widthM").unwrap_or(s.view_size.0 * map.ground_res()).clamp(10.0, 4e7); let title = page_title(ps(p, "title").unwrap_or(&name))?; - add_page(s, p, title, PageKind::Map { map: name, center, width_m }) + let added = add_page(s, p, title, PageKind::Map { map: name, center, width_m })?; + if let (Some(i), Some(k)) = (added["presentation"].as_u64(), added["page"].as_u64()) { + say_title_statistics(s, i as usize, k as usize); + } + Ok(added) }).inputs(map_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_presentation) - .params(r#"{"presentation"?, "map"?: name (default: the active map, at its current view), "lon"?, "lat"?, "widthM"?: ground width shown (m), "title"? (a layer's figure may be written in it: {count:Layer}, {sum:Layer:field}, {mean:…}, {min:…}, {max:…}, filled in when the page is drawn), "at"?: page index}"#), + .params(r#"{"presentation"?, "map"?: name (default: the active map, at its current view), "lon"?, "lat"?, "widthM"?: ground width shown (m), "title"? (a layer's figure may be written in it: {count:Layer}, {sum:Layer:field}, {mean:…}, {min:…}, {max:…}, filled in when the page is drawn; one with no figure is drawn as written, with a warning), "at"?: page index}"#), C::new("presentation.blankPage", "Blank Page", "Publish › Page", |s, p| { let background = super::layout::color(p, "background", Rgba::rgb(0xFF, 0xFF, 0xFF))?; let text = super::within("text", "a page's text", ps(p, "text").unwrap_or(""), MAX_TEXT)?; @@ -280,6 +297,7 @@ fn update_page(s: &mut Session, p: &Value) -> CmdResult { let new_notes = checked("notes", "a page's notes", MAX_TEXT)?; let new_text = checked("text", "a page's text", MAX_TEXT)?; let new_caption = checked("caption", "a page's caption", MAX_CAPTION)?; + let titled = new_title.is_some(); let pres = s.project.presentations.get_mut(i).ok_or("no presentation")?; let n = pres.pages.len(); let page = pres.pages.get_mut(k).ok_or("no page")?; @@ -314,9 +332,29 @@ fn update_page(s: &mut Session, p: &Value) -> CmdResult { let page = pres.pages.remove(k); pres.pages.insert(at, page); } + if titled { + say_title_statistics(s, i, at); + } Ok(json!({"page": at})) } +/// Which layer statistics (#2853) in the title of the page `k` of the presentation `i` are drawn +/// as written, and what each misses: only a map page fills them in, from the map [`page_layout`] +/// draws it over. One pass over each layer the title names, as a layout text's notices make, so an +/// import makes one per map page that holds a figure. +fn title_statistic_notices(s: &Session, i: usize, k: usize) -> Vec { + let Some(page) = s.project.presentations.get(i).and_then(|p| p.pages.get(k)) else { return Vec::new() }; + let PageKind::Map { map, .. } = &page.kind else { return Vec::new() }; + rustgis_render::layout::map_statistic_notices(page_map(s, map), "the page's map", &page.title) +} + +/// Warns with each of [`title_statistic_notices`] for one page. +fn say_title_statistics(s: &mut Session, i: usize, k: usize) { + for w in title_statistic_notices(s, i, k) { + s.warn(w); + } +} + /// A presentation file. pub fn presentation_file(s: &mut Session, i: usize) -> Result, String> { let pres = s.project.presentations.get_mut(i).ok_or("no presentation")?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a0e3bd7fe2..72bb994b32 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -40422,6 +40422,67 @@ fn renaming_a_layer_renames_it_in_a_presentation_map_page_title() { assert!(said.iter().any(|n| n.contains("1 presentation title(s) keep the old layer name") && n.contains("500")), "{said:?}"); } +/// A presentation map page's title fills its layer statistics (#2853) from the page's map, so a +/// title set or imported with one that has no figure says what it misses (#2999); a page with no +/// map frame, which never fills one in, says nothing. +#[test] +fn a_map_page_title_statistic_without_a_figure_is_said() { + let mut s = world(); + let home = s.map().name.clone(); + s.run("presentation.new", &json!({"name": "P"})).unwrap(); + let run = |s: &mut Session, id: &str, p: Value| { + let mark = s.notice_mark(); + s.run(id, &p).unwrap(); + s.notices_since(mark).iter().map(|n| n.text.as_str().to_string()).filter(|t| t.contains("shows as written")).collect::>() + }; + let nowhere = "{count:Nowhere} shows as written: the page's map has no layer named 'Nowhere'"; + for title in ["{count:Countries} countries", "{scale} {foo}"] { + assert_eq!(run(&mut s, "presentation.mapPage", json!({"title": title})), Vec::::new(), "{title}"); + } + assert_eq!(run(&mut s, "presentation.mapPage", Value::Null), Vec::::new(), "the map's name as the title"); + assert_eq!(run(&mut s, "presentation.mapPage", json!({"title": "{count:Nowhere} and {count:Nowhere}", "at": 0})), [nowhere]); + assert_eq!( + run(&mut s, "presentation.mapPage", json!({"title": "{max:Countries:nosuch} {sum:Countries}"})), + [ + "{max:Countries:nosuch} shows as written: 'Countries' has no field named 'nosuch'", + "{sum:Countries} shows as written: a sum takes a field: write {sum:Countries:field}", + ] + ); + // The page's own map is read, not the active one. + s.run("map.new", &json!({"name": "Other"})).unwrap(); + s.run("map.activate", &json!({"name": home})).unwrap(); + assert_eq!( + run(&mut s, "presentation.mapPage", json!({"map": "Other", "title": "{count:Countries}"})), + ["{count:Countries} shows as written: the page's map has no layer named 'Countries'"] + ); + // A new title on a map page says it; another change to the page doesn't. + assert_eq!(run(&mut s, "presentation.updatePage", json!({"page": 0, "title": "{count:Nowhere}"})), [nowhere]); + for change in [json!({"page": 0, "notes": "n"}), json!({"page": 0, "seconds": 9}), json!({"page": 0, "moveTo": 1})] { + assert_eq!(run(&mut s, "presentation.updatePage", change.clone()), Vec::::new(), "{change}"); + } + // A page moved in the same change is still the one read. + assert_eq!(run(&mut s, "presentation.updatePage", json!({"page": 1, "title": "{count:Nowhere}", "moveTo": 0})), [nowhere]); + assert_eq!(run(&mut s, "edit.undo", Value::Null), Vec::::new(), "undo says nothing"); + // A blank page draws its title with no map frame. + let blank = s.project.presentations[0].pages.len(); + s.run("presentation.blankPage", &Value::Null).unwrap(); + assert_eq!(run(&mut s, "presentation.updatePage", json!({"page": blank, "title": "{count:Nowhere}"})), Vec::::new()); + // An imported presentation says each warning once, with how many map pages it is for when + // more than one. + s.run("presentation.new", &json!({"name": "Q", "title": "{count:Nowhere}"})).unwrap(); + for title in ["{count:Nowhere}", "{count:Nowhere} {count:Elsewhere}"] { + s.run("presentation.mapPage", &json!({"title": title})).unwrap(); + } + let text = serde_json::to_string(&s.project.presentations[1]).unwrap(); + assert_eq!( + run(&mut s, "presentation.import", json!({"text": text})), + [ + format!("{nowhere} (in 2 map pages)"), + "{count:Elsewhere} shows as written: the page's map has no layer named 'Elsewhere'".to_string(), + ] + ); +} + /// The map of a layout's first frame decides its texts' figures (#2853), so a map deleted that a /// layout's first frame shows says what its texts lose, as it does for `{map}`; a map no first /// frame shows says nothing. diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index c8444187fc..035f0e4264 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -1252,17 +1252,32 @@ pub fn expand(t: &str, layout: &Layout, info: &PageInfo, frame: Option<(&Map, ru /// a figure over a layer read in place, which covers only the features loaded, has a line for that /// as well. pub fn statistic_notices(layout: &Layout, maps: &[Map], text: &str) -> Vec { - use rustgis_map::layout::StatMissing; let map = layout.first_frame().and_then(|f| match &layout.element(f)?.kind { ElementKind::MapFrame { map, .. } => maps.get(map_index(maps, map)?), _ => None, }); + let no_map = match layout.first_frame() { + Some(_) => "the map of the layout's first frame is not in the project", + None => "the layout has no map frame whose map it would read", + }; + notices_over(map.ok_or(no_map), "the map of the layout's first frame", text) +} + +/// [`statistic_notices`] for a text whose figures are read from `map`, which the notices call +/// `whose`: a presentation map page's title, drawn over that page's own map. +pub fn map_statistic_notices(map: &Map, whose: &str, text: &str) -> Vec { + notices_over(Ok(map), whose, text) +} + +/// The notices of `text`'s figures over `map`, or why there is none to read. +fn notices_over(map: Result<&Map, &str>, whose: &str, text: &str) -> Vec { + use rustgis_map::layout::StatMissing; let mut said: Vec = Vec::new(); let tokens = distinct_stat_tokens(text, true); // Each layer's figures in one pass, whatever their number. let figures: Vec)>>> = match map { - Some(m) => figures_of(m, &tokens).into_iter().map(Some).collect(), - None => vec![None; tokens.len()], + Ok(m) => figures_of(m, &tokens).into_iter().map(Some).collect(), + Err(_) => vec![None; tokens.len()], }; for (t, found) in tokens.iter().zip(figures) { let (layer, field) = (t.layer, t.field.unwrap_or_default()); @@ -1272,9 +1287,8 @@ pub fn statistic_notices(layout: &Layout, maps: &[Map], text: &str) -> Vec "the map of the layout's first frame is not in the project".to_string(), - None => "the layout has no map frame whose map it would read".to_string(), - Some(None) => format!("the map of the layout's first frame has no layer named '{layer}'"), + None => map.err().unwrap_or_default().to_string(), + Some(None) => format!("{whose} has no layer named '{layer}'"), Some(Some(Ok(_))) => continue, Some(Some(Err(StatMissing::Raster))) => format!("'{layer}' is a raster layer, which has no features"), Some(Some(Err(StatMissing::Field))) if t.field.is_none() => { @@ -11281,8 +11295,14 @@ Elaboração: RustGIS", let kind = serde_json::from_value(serde_json::json!({"MapFrame": {"map": "m", "center": {"x": 0, "y": 0}, "scale": 6300000}})).unwrap(); layout.add("Frame", (0.0, 0.0, 50.0, 50.0), kind); + let maps = [map]; + assert_eq!( + statistic_notices(&layout, &maps, "{count:Elevation}"), + vec!["{count:Elevation} shows as written: 'Elevation' is a raster layer, which has no features".to_string()] + ); + // The same figures over a map given directly, as a presentation map page's title is. assert_eq!( - statistic_notices(&layout, &[map], "{count:Elevation}"), + map_statistic_notices(&maps[0], "the page's map", "{count:Elevation}"), vec!["{count:Elevation} shows as written: 'Elevation' is a raster layer, which has no features".to_string()] ); } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index c4814329f5..a4e495aea9 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -509,7 +509,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu its field) stays as written, and a warning names what is missing. Both warnings come when the text is added or changed, or its layout is made or imported, or when another map frame becomes the first (it is given another map, the one before it is deleted or drawn over it, or its map is - deleted), not when the page is exported. The text, its size (2 to 400 points, as text graphics on + deleted), not when the page is exported. A presentation map page's title is checked against the + page's map when the page is added with it, when it is changed or when the presentation is + imported. The text, its size (2 to 400 points, as text graphics on the map), colour, bold, **Italic** (slanted, as water and ocean names are; the built-in fonts have no italic face, so the letters are slanted 12°), **Alignment** (left, center or right within the element) and **Letter spacing** in points (spaced-out names such as O C E A N O, or a