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
38 changes: 22 additions & 16 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3194,6 +3194,7 @@ fn duplicate_element(s: &mut Session, p: &Value) -> CmdResult {
if dx.abs() > 5000.0 || dy.abs() > 5000.0 {
return Err("the copy's offset is up to 5000 mm".into());
}
let li = index(s, p)?;
let l = active(s, p)?;
let e = l.element(id).cloned().ok_or_else(|| format!("no element {id}"))?;
let name = numbered(&e.name, |n| l.elements.iter().any(|x| x.name == n));
Expand All @@ -3203,6 +3204,9 @@ fn duplicate_element(s: &mut Session, p: &Value) -> CmdResult {
c.background = e.background;
}
super::say_name_cut(s, "Duplicate Element", "the copy's name", &e.name, &name);
// Its offset can take a text's ink past the page's edge, as a move does (#2703); the lines it
// loses are the original's, said when it was set.
say_text_fit(s, li, copy, false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call also covers scale bars: say_text_fit takes the bar branch for a ScaleBar and ignores the lines flag there. So duplicating a scale bar whose box is too small for its labels now repeats the "box too small" notice for the copy, even when the copy sits inside the page. The comment above and the layout.duplicateElement line in docs/user-guide/layouts.md only describe text, and no test covers a copied scale bar. Either say in the comment that a copied scale bar repeats its own notice, as a move already does (#2581), or add a test that pins that behaviour.

Category: quality

Ok(json!({"element": copy, "name": name}))
}

Expand Down Expand Up @@ -3601,9 +3605,10 @@ fn say_legend_fit(s: &mut Session, li: usize, id: u32) {

/// Says how many lines of the text `id` of the layout `li` don't fit its box (#2346), as the
/// layout view and an export count them: on the series' current page, measured at 300 dpi as a
/// wrapped text is broken. For a scale bar `id`, whether its box is too short for its labels or
/// one runs off the page (#2581).
fn say_text_fit(s: &mut Session, li: usize, id: u32) {
/// wrapped text is broken, and, a notice of its own, that its ink runs off the page (#2703). A
/// `lines` of false (a move or a turn, which change no line) says only the edge. For a scale bar
/// `id`, whether its box is too short for its labels or one runs off the page (#2581).
fn say_text_fit(s: &mut Session, li: usize, id: u32, lines: bool) {
let bar = match s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind) {
Some(ElementKind::Text { .. }) => false,
Some(ElementKind::ScaleBar { .. }) => true,
Expand All @@ -3614,7 +3619,8 @@ fn say_text_fit(s: &mut Session, li: usize, id: u32) {
let notices = if bar {
rustgis_render::layout::scale_bar_fit_notices(&[loss])
} else {
rustgis_render::layout::lines_left_out_notice(&loss).into_iter().collect()
let left_out = rustgis_render::layout::lines_left_out_notice(&loss).filter(|_| lines);
left_out.into_iter().chain(rustgis_render::layout::off_page_notice(&loss)).collect()
};
for notice in notices {
s.warn(notice);
Expand All @@ -3636,8 +3642,9 @@ fn text_fit_loss(s: &Session, li: usize, id: u32) -> Option<rustgis_render::layo
}

/// What a text is given that changes the lines it breaks into or the room they have, or shows it
/// (#2346): a move or a turn doesn't, nor a colour (the Format pane sets one at each step of a
/// pick), except one that shows a text without ink, which `update` tells apart.
/// (#2346). A move or a turn changes no line, so `update` says only whether the text runs off the
/// page then (#2703); nor does a colour (the Format pane sets one at each step of a pick), except
/// one that shows a text without ink, which `update` tells apart.
const TEXT_FIT_KEYS: [&str; 12] =
["text", "size", "w", "h", "wrap", "font", "fallback", "bold", "italic", "letterSpacing", "align", "visible"];
/// What a scale bar is given that changes its labels (#2581): its text size (`null` included, which
Expand Down Expand Up @@ -4038,7 +4045,7 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult {
if kind == "chart" {
say_chart_losses(s, i, id);
}
say_text_fit(s, i, id);
say_text_fit(s, i, id, true);
if let Some(said) = empty_colorbar {
s.warn(said);
}
Expand Down Expand Up @@ -5648,15 +5655,14 @@ fn update(s: &mut Session, p: &Value) -> CmdResult {
} else if let Some(basis) = &refit {
refit_scale_box(s, li, basis);
}
// After the scale box is refitted: a text is measured in the box it is saved with, and a scale
// bar moved, whose labels can then run off the page, too.
let bar = s.project.layouts.get(li).and_then(|l| l.element(id)).is_some_and(|e| matches!(e.kind, ElementKind::ScaleBar { .. }));
if shown
|| TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null()))
|| BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some())
|| (bar && given(&["x", "y"]))
{
say_text_fit(s, li, id);
// After the scale box is refitted: a text is measured in the box it is saved with, and a text
// or scale bar moved, whose ink can then run off the page, too.
if shown || TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) || BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some()) {
say_text_fit(s, li, id, true);
} else if given(&["x", "y", "rotation"]) {
// A move or a turn changes no line a text breaks into, but can take its ink past the page's
// edge (#2703); a scale bar's labels move with it (#2581).
say_text_fit(s, li, id, false);
}
// A centre the frame's map can't place (a pole on Web Mercator) is said (#2684).
if new_center.is_some() || given(&["map"]) {
Expand Down
144 changes: 144 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43573,6 +43573,150 @@ fn text_elements_say_the_lines_left_out_of_their_box() {
assert!(said(&mut s, "layout.addElement", json!({"kind":"text", "text":"Source", "x":10, "y":60, "w":60, "h":10})).1.is_empty());
}

/// `layout.addElement` and `layout.update` say a text whose ink crosses the page's edge when they
/// add it, move it (`x`, `y`), turn it or change what it breaks into, as they say a scale bar's
/// labels off the page; one notice of each kind, so a text cut below its box and past the edge is
/// told both; a text inside the page, moved back inside or turned within it says nothing (#2703).
#[test]
fn text_elements_say_what_runs_off_the_page() {
let mut s = Session::new(Project::default());
s.run("layout.new", &json!({"template": "blank", "width": 120, "height": 90})).unwrap();
// The notices of the command: (past the edge, lines left out).
let said = |s: &mut Session, cmd: &str, p: Value| {
s.notices.clear();
let out = s.run(cmd, &p).unwrap();
let edge: Vec<String> = s.notices.iter().filter(|n| n.text.contains("runs off the page")).map(|n| n.text.clone()).collect();
let lines: Vec<String> = s.notices.iter().filter(|n| n.text.contains("fit its box")).map(|n| n.text.clone()).collect();
(out, edge, lines)
};
let off = |s: &Session, id: u64| {
let name = s.project.layouts[0].element(id as u32).map(|e| e.name.clone()).unwrap_or_default();
format!(
"The text \"{name}\" ({id}) runs off the page, so part of it isn't drawn; move it inside the page, resize it or reduce its size"
)
};
// Added inside the page: nothing. Added past an edge: told, once.
let (_, edge, lines) = said(&mut s, "layout.addElement", json!({"kind":"text", "text":"Note", "x":10, "y":10, "w":30, "h":10}));
assert!(edge.is_empty() && lines.is_empty(), "{edge:?} {lines:?}");
let (out, edge, _) = said(&mut s, "layout.addElement", json!({"kind":"text", "text":"Note", "x":118, "y":10, "w":30, "h":10}));
let past = out["element"].as_u64().unwrap();
assert_eq!(edge, vec![off(&s, past)]);
// A text moved past each side is told, once; moved back inside, it isn't.
let (out, ..) = said(&mut s, "layout.addElement", json!({"kind":"text", "text":"Note", "x":10, "y":40, "w":30, "h":10}));
let id = out["element"].as_u64().unwrap();
for (key, past_it) in [("x", 118), ("y", 89), ("x", -30), ("y", -8)] {
let (_, edge, lines) = said(&mut s, "layout.update", json!({"element": id, key: past_it}));
assert_eq!((edge, lines.len()), (vec![off(&s, id)], 0), "{key} {past_it}");
let back = if key == "x" { 10 } else { 40 };
let (_, edge, _) = said(&mut s, "layout.update", json!({"element": id, key: back}));
assert!(edge.is_empty(), "{key} back inside: {edge:?}");
}
// Moved on, still past the edge: told again, once a command; both places given: once.
assert_eq!(said(&mut s, "layout.update", json!({"element": id, "x": 118})).1.len(), 1);
assert_eq!(said(&mut s, "layout.update", json!({"element": id, "x": 119, "y": 41})).1, vec![off(&s, id)]);
assert!(said(&mut s, "layout.update", json!({"element": id, "x": 10, "y": 40})).1.is_empty());
// Turned about its box's centre: inside the page nothing, across its edge told.
assert!(said(&mut s, "layout.update", json!({"element": id, "rotation": 90})).1.is_empty());
assert!(said(&mut s, "layout.update", json!({"element": id, "rotation": null})).1.is_empty());
said(&mut s, "layout.update", json!({"element": id, "rotation": 0, "x": 106, "y": 40, "w": 20}));
assert!(said(&mut s, "layout.update", json!({"element": id, "x": 106})).1.is_empty(), "unturned, inside");
assert_eq!(said(&mut s, "layout.update", json!({"element": id, "rotation": 180})).1, vec![off(&s, id)]);
said(&mut s, "layout.update", json!({"element": id, "rotation": 0, "x": 10, "w": 30}));
// Cut below its box and past the edge: the move says the edge only (its lines were said), a resize
// or an edit says each, one notice of each kind.
let (out, edge, lines) =
said(&mut s, "layout.addElement", json!({"kind":"text", "text":"one\ntwo\nthree", "x":10, "y":10, "w":30, "h":2}));
let cut = out["element"].as_u64().unwrap();
assert!(edge.is_empty() && lines.len() == 1, "{edge:?} {lines:?}");
let (_, edge, lines) = said(&mut s, "layout.update", json!({"element": cut, "x": 118}));
assert!(edge == vec![off(&s, cut)] && lines.is_empty(), "{edge:?} {lines:?}");
let (_, edge, lines) = said(&mut s, "layout.update", json!({"element": cut, "x": 117, "w": 31}));
assert_eq!((edge.len(), lines.len()), (1, 1), "{edge:?} {lines:?}");
assert!(lines[0].starts_with("2 lines of the text \"") && lines[0].contains(&format!("({cut}) don't fit its box")), "{lines:?}");
// Hidden, without ink or empty: nothing to see off the page; shown or inked again, it is told.
let (_, edge, _) = said(&mut s, "layout.update", json!({"element": id, "x": 118, "visible": false}));
assert!(edge.is_empty(), "{edge:?}");
assert_eq!(said(&mut s, "layout.update", json!({"element": id, "visible": true})).1, vec![off(&s, id)]);
assert!(said(&mut s, "layout.update", json!({"element": id, "color": "#00000000"})).1.is_empty());
assert!(said(&mut s, "layout.update", json!({"element": id, "x": 119})).1.is_empty(), "no ink");
assert_eq!(said(&mut s, "layout.update", json!({"element": id, "color": "#1F2937"})).1, vec![off(&s, id)]);
assert!(said(&mut s, "layout.update", json!({"element": id, "text": ""})).1.is_empty(), "no letter");
// Another element moved past the edge isn't a text.
let rect =
said(&mut s, "layout.addElement", json!({"kind":"rectangle", "x":10, "y":10, "w":20, "h":20})).0["element"].as_u64().unwrap();
assert!(said(&mut s, "layout.update", json!({"element": rect, "x": 118})).1.is_empty());
// A place or size no page holds is held to its limits: told once, no panic.
let (_, edge, _) = said(&mut s, "layout.update", json!({"element": cut, "x": 1e300, "y": -1e300}));
assert_eq!(edge.len(), 1, "{edge:?}");
s.notices.clear();
let _ = s.run("layout.update", &json!({"element": cut, "size": 1e300}));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of s.run is discarded with let _ =, so this passes if the command refuses a size of 1e300, and the test then checks nothing about the notice. The following <= 1 check also can't fail, since one say_text_fit call emits at most one edge notice. The no-panic part is worth keeping, but it should unwrap the result (or assert on the refusal's text) and assert the expected count once the command has run.

Category: quality

assert!(s.notices.iter().filter(|n| n.text.contains("runs off the page")).count() <= 1);
}
/// A copy of a text that lands past the page's edge, by the default offset or a `dx`, `dy` of its
/// own, is told as a move is: the edge only, the lines it loses being the original's to say, once.
/// A copy inside the page says nothing, nor does a rectangle's (#2703).
#[test]
fn a_copied_text_that_lands_off_the_page_is_told() {
let mut s = Session::new(Project::default());
s.run("layout.new", &json!({"template": "blank", "width": 120, "height": 90})).unwrap();
let add = |s: &mut Session, p: Value| s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap();
// The copy's notices: (past the edge, lines left out).
let copied = |s: &mut Session, p: Value| {
s.notices.clear();
let out = s.run("layout.duplicateElement", &p).unwrap();
let edge: Vec<String> = s.notices.iter().filter(|n| n.text.contains("runs off the page")).map(|n| n.text.clone()).collect();
let lines = s.notices.iter().filter(|n| n.text.contains("fit its box")).count();
(out["element"].as_u64().unwrap(), edge, lines)
};
let off = |s: &Session, id: u64| {
let name = s.project.layouts[0].element(id as u32).map(|e| e.name.clone()).unwrap_or_default();
format!(
"The text \"{name}\" ({id}) runs off the page, so part of it isn't drawn; move it inside the page, resize it or reduce its size"
)
};
let note = add(&mut s, json!({"kind":"text", "text":"Note", "x":10, "y":10, "w":30, "h":10}));
let (_, edge, lines) = copied(&mut s, json!({"element": note}));
assert!(edge.is_empty() && lines == 0, "inside the page: {edge:?}");
let (copy, edge, _) = copied(&mut s, json!({"element": note, "dx": 108}));
assert_eq!(edge, vec![off(&s, copy)], "past the right edge");
let (copy, edge, _) = copied(&mut s, json!({"element": note, "dy": 79}));
assert_eq!(edge, vec![off(&s, copy)], "past the bottom");
let (copy, edge, _) = copied(&mut s, json!({"element": note, "dx": -50}));
assert_eq!(edge, vec![off(&s, copy)], "past the left edge");
// By the default offset: a text a few millimetres from the edge.
let near = add(&mut s, json!({"kind":"text", "text":"Note", "x":109, "y":10, "w":30, "h":10}));
let (copy, edge, _) = copied(&mut s, json!({"element": near}));
assert_eq!(edge, vec![off(&s, copy)], "the default offset");
// Cut below its box and past the edge: the copy says the edge only.
let cut = add(&mut s, json!({"kind":"text", "text":"one\ntwo\nthree", "x":10, "y":10, "w":30, "h":2}));
let (copy, edge, lines) = copied(&mut s, json!({"element": cut, "dx": 108}));
assert_eq!((edge, lines), (vec![off(&s, copy)], 0));
// A hidden text copied past the edge draws nothing; a rectangle isn't a text.
s.run("layout.update", &json!({"element": note, "visible": false})).unwrap();
assert!(copied(&mut s, json!({"element": note, "dx": 108})).1.is_empty());
let rect = add(&mut s, json!({"kind":"rectangle", "x":10, "y":10, "w":20, "h":20}));
assert!(copied(&mut s, json!({"element": rect, "dx": 108})).1.is_empty());
}

/// A text turned by a rotation that isn't a number (a layout file can hold one) draws as it
/// always has: measured without a panic, and told nothing off the page (#2703).
#[test]
fn a_text_with_a_rotation_that_is_not_a_number_says_nothing_off_the_page() {
let mut s = Session::new(Project::default());
s.run("layout.new", &json!({"template": "blank", "width": 120, "height": 90})).unwrap();
let id = s.run("layout.addElement", &json!({"kind":"text", "text":"Note", "x":10, "y":10, "w":30, "h":10})).unwrap()["element"]
.as_u64()
.unwrap();
if let Some(rustgis_map::layout::ElementKind::Text { rotation, .. }) = s.project.layouts[0].element_mut(id as u32).map(|e| &mut e.kind)
{
*rotation = f64::NAN;
}
s.notices.clear();
s.run("layout.update", &json!({"element": id, "x": 118})).unwrap();
s.run("layout.update", &json!({"element": id, "y": 10, "rotation": null})).unwrap();
assert!(s.notices.iter().all(|n| !n.text.contains("runs off the page")), "{:?}", s.notices.iter().map(|n| &n.text).collect::<Vec<_>>());
}

/// A layout chart groups, orders and cuts its bars by the options of `layout.addElement` and
/// `layout.update`, writes none of them at their defaults, refuses a bad one (changing nothing)
/// and refuses them for other kinds; it says what it leaves out: bars past its limit, features
Expand Down
Loading
Loading