Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7502,12 +7502,25 @@ fn follow_removal(s: &mut Session, names: &[String]) {
terrain_follows_removal(s, names);
square_labels_follow_removal(s, names);
layouts_follow_removal(s, names);
pages_follow_removal(s, names);
// A legend that left it out forgets it.
for n in names {
legends_follow_layer(s, n, None);
}
}

/// The notice that `count` items name the removed layers `named`, in the first of `keys` for one
/// name and the second for more. `None` when no item does.
fn removed_notice(keys: [&'static str; 2], count: usize, named: &[&str]) -> Option<crate::notice_text::NoticeText> {
if count == 0 {
return None;
}
let [one, more] = keys;
let key = if named.len() == 1 { one } else { more };
let names = named.iter().map(|n| format!("'{n}'")).collect::<Vec<_>>().join(", ");
Some(crate::notice_text::NoticeText::format(key, [("count", count.to_string().into()), ("names", names.into())]))
}

/// After layers called `names` are removed: a table's, chart's or colorbar's layer, and a text's
/// layer statistics (#2853), keep the name, so a layer of that name brings them back, and are drawn
/// without it meanwhile: how many elements, said once. Which elements read the active map is as for
Expand All @@ -7534,26 +7547,62 @@ fn layouts_follow_removal(s: &mut Session, names: &[String]) {
}
_ => continue,
};
let mut lost = false;
for name in gone.iter().filter(|n| reads.iter().any(|r| r.eq_ignore_ascii_case(n))) {
lost = true;
if !named.iter().any(|n| n.eq_ignore_ascii_case(name)) {
named.push(name.as_str());
}
}
count += usize::from(lost);
count += usize::from(reads_removed(&reads, &gone, &mut named));
}
}
if count == 0 {
return;
let keys = [
"{count} layout element(s) name the removed layer {names}: they are drawn without it until a layer of that name comes back",
"{count} layout element(s) name the removed layers {names}: they are drawn without it until a layer of that name comes back",
];
if let Some(notice) = removed_notice(keys, count, &named) {
s.notify_text(notice, false);
}
}

/// Whether `reads`, the layer names one item reads, hold a removed one: each of `gone` it reads
/// joins `named`, once.
fn reads_removed<'a>(reads: &[&str], gone: &[&'a String], named: &mut Vec<&'a str>) -> bool {
let mut lost = false;
for name in gone.iter().filter(|n| reads.iter().any(|r| r.eq_ignore_ascii_case(n))) {
lost = true;
if !named.iter().any(|n| n.eq_ignore_ascii_case(name)) {
named.push(name.as_str());
}
}
lost
}

/// After layers called `names` are removed: a map series' index layer, and the layer statistics
/// (#2853) in a presentation map page's title, keep the name, so a layer of that name brings them
/// back: how many series and titles, said once (#3075). Which of them read the active map is as for
/// a rename (`references_follow_layer`).
fn pages_follow_removal(s: &mut Session, names: &[String]) {
use rustgis_map::presentation::PageKind;

let gone: Vec<&String> = names.iter().filter(|n| named_layers(s, n) == 0).collect();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let map_name = &s.map().name;
let mut named: Vec<&str> = Vec::new();
let mut count = 0usize;
for layout in &s.project.layouts {
if let Some(series) = &layout.series
&& frames_showing(layout, map_name).contains(&series.frame)
{
count += usize::from(reads_removed(&[&series.layer], &gone, &mut named));
}
}
for page in s.project.presentations.iter().flat_map(|p| &p.pages) {
if matches!(&page.kind, PageKind::Map { map, .. } if map.eq_ignore_ascii_case(map_name)) {
let reads: Vec<&str> = rustgis_map::layout::stat_tokens(&page.title).iter().map(|t| t.layer).collect();
count += usize::from(reads_removed(&reads, &gone, &mut named));
}
}
let keys = [
"{count} map series or presentation title(s) name the removed layer {names}: a series exports only its current page, and a title shows the figure as written, until a layer of that name comes back",
"{count} map series or presentation title(s) name the removed layers {names}: a series exports only its current page, and a title shows the figure as written, until a layer of that name comes back",
];
if let Some(notice) = removed_notice(keys, count, &named) {
s.notify_text(notice, false);
}
let key = if named.len() == 1 {
"{count} layout element(s) name the removed layer {names}: they are drawn without it until a layer of that name comes back"
} else {
"{count} layout element(s) name the removed layers {names}: they are drawn without it until a layer of that name comes back"
};
let names = named.iter().map(|n| format!("'{n}'")).collect::<Vec<_>>().join(", ");
s.notify_text(crate::notice_text::NoticeText::format(key, [("count", count.to_string().into()), ("names", names.into())]), false);
}

/// After layers called `names` are removed: point labels turned square to one of them (#2263),
Expand Down
8 changes: 8 additions & 0 deletions crates/engine/src/cmd/layout/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ pub(in crate::cmd::layout) fn layout_bytes_inner(
// Only a whole-series PDF names every page.
let info = series_info(s, li, current);
let names = if all && pdf { series_pages(s, li) } else { None };
// A series whose index layer is gone has no pages to step through (#3075).
if all
&& pdf
&& info.is_none()
&& let Some(series) = &saved.series
{
s.warn(format!("The map series' index layer '{}' isn't in its frame's map: the PDF holds the current page only", series.layer));
}
let total = info.as_ref().map_or(1, |i| i.0);
// Every page of a map series (PDF), or just the layout as it is now.
let pages: Vec<usize> = match (&names, all && pdf) {
Expand Down
124 changes: 123 additions & 1 deletion crates/engine/src/tests/layout_removed_layer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Deleting a layer says how many layout elements name it: a chart's, table's or colorbar's
//! layer, and a text's layer statistics (#3020). The elements keep the name.
//! layer, and a text's layer statistics (#3020). So for map series and presentation titles
//! (#3075). All keep the name.

use super::*;
use rustgis_map::layout::ElementKind;
Expand Down Expand Up @@ -117,3 +118,124 @@ fn removing_a_group_says_it_too() {
s.run("group.add", &json!({"name": "Places", "layers": ["Cities"]})).unwrap();
assert_eq!(lost(&mut s, "group.remove", json!({"group": "Places"})), [said(1, "'Cities'")]);
}

/// The notices `command` gives about map series and presentation titles that lost a layer.
fn lost_pages(s: &mut Session, command: &str, params: Value) -> Vec<String> {
let mark = s.notice_mark();
s.run(command, &params).unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("map series or presentation title(s)")).collect()
}

fn said_pages(count: usize, names: &str) -> String {
let layers = if names.contains(", ") { "layers" } else { "layer" };
format!(
"{count} map series or presentation title(s) name the removed {layers} {names}: a series exports only its current page, and a title shows the figure as written, until a layer of that name comes back"
)
}

fn series(s: &mut Session, layer: &str) {
s.run("layout.series", &json!({"layer": layer, "nameField": "name"})).unwrap();
}

fn titled(s: &mut Session, title: &str) {
if s.project.presentations.is_empty() {
s.run("presentation.new", &json!({"name": "Talk"})).unwrap();
}
s.run("presentation.mapPage", &json!({"title": title})).unwrap();
}

#[test]
fn removing_a_series_index_layer_says_it() {
let mut s = with_layout();
series(&mut s, "Countries");
assert_eq!(lost_pages(&mut s, "layer.remove", json!({"layer": "Countries"})), [said_pages(1, "'Countries'")]);
assert_eq!(s.project.layouts[0].series.as_ref().map(|r| r.layer.as_str()), Some("Countries"), "the series keeps the name");
assert_eq!(lost_pages(&mut s, "edit.undo", Value::Null), Vec::<String>::new());
}

#[test]
fn removing_a_layer_a_presentation_title_counts_says_it() {
let mut s = world();
// Two statistics of the layer in one title count it once; each page counts.
titled(&mut s, "{count:countries} countries, {sum:Countries:pop_est} people");
titled(&mut s, "{count:Countries}");
let pages = s.project.presentations[0].pages.len();
assert_eq!(lost_pages(&mut s, "layer.remove", json!({"layer": "Countries"})), [said_pages(2, "'Countries'")]);
assert_eq!(s.project.presentations[0].pages.len(), pages);
assert_eq!(s.project.presentations[0].pages.last().map(|p| p.title.as_str()), Some("{count:Countries}"));
}

#[test]
fn a_series_and_titles_of_several_layers_are_said_once() {
let mut s = with_layout();
series(&mut s, "Countries");
titled(&mut s, "{count:Countries} and {count:Cities}");
s.run("group.add", &json!({"name": "Places", "layers": ["Countries", "Cities"]})).unwrap();
let said_now = lost_pages(&mut s, "group.remove", json!({"group": "Places"}));
assert_eq!(said_now.len(), 1, "{said_now:?}");
assert!(said_now[0].starts_with("2 map series or presentation title(s) name the removed layers "), "{said_now:?}");
for name in ["'Countries'", "'Cities'"] {
assert_eq!(said_now[0].matches(name).count(), 1, "{said_now:?}");
}
}

#[test]
fn a_series_or_title_no_removed_layer_is_in_says_nothing() {
let mut s = with_layout();
series(&mut s, "Countries");
titled(&mut s, "{count:Countries} in {map}");
assert_eq!(lost_pages(&mut s, "layer.remove", json!({"layer": "Cities"})), Vec::<String>::new());
// Another layer of the name stays: nothing is lost until the last goes.
s.run("layer.rename", &json!({"layer": "Lakes", "name": "Countries"})).unwrap();
let first = s.map().layers.iter().find(|l| l.name == "Countries").unwrap().id;
assert_eq!(lost_pages(&mut s, "layer.remove", json!({"layer": first})), Vec::<String>::new());
}

#[test]
fn a_series_and_titles_of_another_map_are_not_counted() {
let mut s = with_layout();
series(&mut s, "Countries");
titled(&mut s, "{count:Countries}");
s.run("map.new", &json!({"name": "Other"})).unwrap();
s.run("layer.addSample", &json!({"name": "countries"})).unwrap();
assert_eq!(lost_pages(&mut s, "layer.remove", json!({"layer": "Countries"})), Vec::<String>::new());
}

/// What exporting the first layout to a file named `file` warns about its map series' index layer.
fn export_warnings(s: &mut Session, file: &str, all: bool) -> Vec<String> {
let dir = std::env::temp_dir().join(format!("rustgis-3075-{}-{:?}", std::process::id(), std::thread::current().id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(file);
let mark = s.notice_mark();
let run = s.run("layout.export", &json!({"path": path, "dpi": 36, "allPages": all}));
std::fs::remove_dir_all(&dir).unwrap();
run.unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("index layer")).collect()
}

const ONE_PAGE: &str = "The map series' index layer 'Countries' isn't in its frame's map: the PDF holds the current page only";

#[test]
fn exporting_a_series_without_its_index_layer_says_the_pdf_holds_one_page() {
let mut s = with_layout();
series(&mut s, "Countries");
s.run("layer.remove", &json!({"layer": "Countries"})).unwrap();
assert_eq!(export_warnings(&mut s, "series.pdf", true), [ONE_PAGE]);
}

#[test]
fn an_export_that_never_wrote_the_series_says_nothing_of_its_index_layer() {
let mut s = with_layout();
// No series.
assert_eq!(export_warnings(&mut s, "plain.pdf", true), Vec::<String>::new());
series(&mut s, "Countries");
s.run("layer.remove", &json!({"layer": "Countries"})).unwrap();
// One page asked for, or a format that only ever writes one.
assert_eq!(export_warnings(&mut s, "page.pdf", false), Vec::<String>::new());
assert_eq!(export_warnings(&mut s, "page.png", true), Vec::<String>::new());
assert_eq!(export_warnings(&mut s, "page.svg", true), Vec::<String>::new());
// The layer back, the series is whole again.
s.run("edit.undo", &Value::Null).unwrap();
assert!(s.map().layer_by_name("Countries").is_some());
assert_eq!(export_warnings(&mut s, "series.pdf", true), Vec::<String>::new());
}
13 changes: 8 additions & 5 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,12 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
in a presentation's map pages of it); a text that a longer name would take past the characters a
text holds, or that a name with a brace (which a token can't hold) would break, keeps the old
name, and the rename says so; deleting it leaves them as written, and when layout elements still
name it (a text's tokens, or a chart's, table's or colorbar's layer), says how many. A layer
read in place covers what is loaded, as a chart does, and a warning says so. A token with no
figure (no such layer or field, a field with no numbers, a raster layer, or a sum, mean, least
or greatest written without its field) stays as written, and a warning names what is missing.
name it (a text's tokens, or a chart's, table's or colorbar's layer), says how many. It also says
how many map series (by their index layer) and presentation map page titles (by their tokens)
name it. A layer read in place covers what is loaded, as a chart does, and a warning says so. A
token with no figure (no such layer or field, a field with no numbers, a raster layer, or a sum,
mean, least or greatest written without 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. A presentation map page's
Expand Down Expand Up @@ -808,7 +810,8 @@ The locator outlines the main frame's changing extent as the series turns its pa
**Export Layout** writes all pages to PDF by default, but only the **first 500** when there are
more, with a warning. Split longer books' index data into layers of at most 500 features.
`layout.export` with `"allPages":false` writes only the current PDF page; PNG and SVG always
write the current page.
write the current page. When the index layer is no longer in the series frame's map (it was
removed, say), the PDF holds only the current page, and the export says so.

### Mark the page's own feature

Expand Down
Loading