diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index c996faa5f4..faf2e3ceda 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -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)); @@ -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); Ok(json!({"element": copy, "name": name})) } @@ -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, @@ -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); @@ -3636,8 +3642,9 @@ fn text_fit_loss(s: &Session, li: usize, id: u32) -> Option 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); } @@ -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"]) { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 30bdc16be1..1f5b0ea68b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -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 = s.notices.iter().filter(|n| n.text.contains("runs off the page")).map(|n| n.text.clone()).collect(); + let lines: Vec = 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})); + 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 = 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::>()); +} + /// 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 diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 09708dda5a..f335c6db4a 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -564,6 +564,18 @@ pub fn lines_left_out_notice(loss: &TextLoss) -> Option { Some(format!("{count} of the text \"{}\" ({}) {said}", loss.name, loss.id)) } +/// What a text whose ink crosses the page's edge is told (#2703), from its [`TextLoss`]: `None` +/// when none does. Its lines left out below its box are [`lines_left_out_notice`]'s: a text can +/// have both, and each is said. +pub fn off_page_notice(loss: &TextLoss) -> Option { + loss.off_page.then(|| { + format!( + "The text \"{}\" ({}) runs off the page, so part of it isn't drawn; move it inside the page, resize it or reduce its size", + loss.name, loss.id + ) + }) +} + /// One element on transparency, allocating only its on-page pixel bounds. /// Outer `None` means rendering failed; inner `None` means the element is off-page. /// A fatal source error is retained for the composed SVG's revision check and error report. @@ -6925,6 +6937,91 @@ mod tests { assert_eq!(lines_left_out_notice(&loss[0]), None); } + /// A text whose ink crosses the page's edge is told apart from its lines left out, with the + /// advice that fixes an edge: moved past each side it is, back inside or turned within the + /// page it isn't, and one cut below its box and past the edge has both (#2703). + #[test] + fn a_text_past_the_pages_edge_is_told_with_its_advice() { + let info = PageInfo::default(); + let mut l = note_page("Note", false, TextAlign::Left, 0.0); + let id = l.elements[0].id; + let put = |l: &mut Layout, (x, y): (f64, f64)| { + if let Some(e) = l.element_mut(id) { + (e.x, e.y) = (x, y); + } + }; + let turn = |l: &mut Layout, degrees: f64| { + if let Some(ElementKind::Text { rotation, .. }) = l.element_mut(id).map(|e| &mut e.kind) { + *rotation = degrees; + } + }; + let told = |l: &Layout| text_loss(l, &[], 96.0, &info, id).and_then(|loss| off_page_notice(&loss)); + let said = format!( + "The text \"Note\" ({id}) runs off the page, so part of it isn't drawn; move it inside the page, resize it or reduce its size" + ); + assert_eq!(told(&l), None, "inside the page"); + // Past each edge. + for at in [(98.0, 10.0), (10.0, 99.0), (-30.0, 10.0), (10.0, -5.0)] { + put(&mut l, at); + let loss = text_loss(&l, &[], 96.0, &info, id).unwrap_or_else(|| panic!("{at:?} is past an edge")); + assert!(loss.off_page && loss.left_out == 0 && !loss.below_box, "{at:?} {loss:?}"); + assert_eq!(off_page_notice(&loss), Some(said.clone()), "{at:?}"); + assert_eq!(lines_left_out_notice(&loss), None, "{at:?}: no line is left out"); + put(&mut l, (10.0, 10.0)); + assert_eq!(told(&l), None, "{at:?} moved back inside"); + } + // A text that doesn't cross says nothing, whatever else is lost. + let inside = + TextLoss { id, name: "Note".into(), left_out: 1, at_least: false, below_box: true, past_limit: false, off_page: false }; + assert_eq!(off_page_notice(&inside), None); + // Turned within the page: nothing. Turned about its box's centre past the edge: told. + put(&mut l, (30.0, 10.0)); + turn(&mut l, 90.0); + assert_eq!(told(&l), None, "turned within the page"); + turn(&mut l, 180.0); + assert_eq!(told(&l), None, "turned within the page"); + put(&mut l, (80.0, 10.0)); + turn(&mut l, 0.0); + assert_eq!(told(&l), None, "unturned, it is inside"); + turn(&mut l, 180.0); + assert_eq!(told(&l), Some(said.clone()), "turned about its box's centre, past the edge"); + // Cut below its box and past the edge: each is told, apart. + let mut l = note_page("one\ntwo\nthree", false, TextAlign::Left, 0.0); + let id = l.elements[0].id; + if let Some(e) = l.element_mut(id) { + (e.x, e.h) = (98.0, 2.0); + } + let loss = text_loss(&l, &[], 96.0, &info, id).unwrap_or_else(|| panic!("cut")); + assert!(loss.below_box && loss.off_page && loss.left_out == 2, "{loss:?}"); + let (lines, edge) = (lines_left_out_notice(&loss).unwrap_or_default(), off_page_notice(&loss).unwrap_or_default()); + assert!(lines.contains("don't fit its box") && !lines.contains("page"), "{lines}"); + assert!(edge.contains("runs off the page") && !edge.contains("lines"), "{edge}"); + // No ink, nothing to cut: an empty or blank text, past the edge. + for blank in ["", " \n "] { + let mut l = note_page(blank, false, TextAlign::Left, 0.0); + let id = l.elements[0].id; + if let Some(e) = l.element_mut(id) { + e.x = 98.0; + } + assert_eq!(text_loss(&l, &[], 96.0, &info, id), None, "{blank:?} has no ink"); + } + // A file can hold any place and size: measured without a panic, and told at most once. + for (x, y, size) in + [(1e300, -1e300, 12.0), (f64::NAN, f64::INFINITY, 12.0), (10.0, 10.0, f64::NAN), (10.0, 10.0, 1e300), (98.0, 10.0, 1e300)] + { + let mut l = note_page("Note", false, TextAlign::Left, 0.0); + let id = l.elements[0].id; + if let Some(e) = l.element_mut(id) { + (e.x, e.y) = (x, y); + if let ElementKind::Text { size: s, .. } = &mut e.kind { + *s = size; + } + } + let losses = text_losses(&l, &[], 96.0, &info); + assert!(losses.len() <= 1 && losses.iter().all(|loss| off_page_notice(loss).iter().count() <= 1), "{x} {y} {size}"); + } + } + /// A long text counts every line left out below its box, each only looked at for a letter /// past the box: none is shaped there (#2346). #[test] diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 52f5230145..910237710e 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -437,7 +437,15 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu spacing, wrap or box, or show it, and the Format pane shows the count under **Wrap to the box** (a text wrapped past the 200 lines a text draws counts them as "or more"). A text draws 200 lines at most, whatever its box: lines past them are said apart, with the advice to split - the text into more than one, since a taller box doesn't draw them. + the text into more than one, since a taller box doesn't draw them. A text whose ink crosses the + page's edge is said in a notice of its own when one of these commands puts it there, so a text + both cut below its box and past the edge is told each: `layout.addElement` and `layout.update` add a text whose ink + crosses it, set what its lines or box are, show it, move it (`x`, `y`) or turn it (`rotation`), + and `layout.duplicateElement` copies it past the edge, and each advises moving it inside the + page, resizing it or reducing its size. A move, a turn or a copy alone says only that, not the + lines again; a text turned within the page, or with no letter or ink to show, says nothing. + Resizing the page (`layout.page`) measures no text, so a text a smaller page cuts is told only + by the export and the layout view. - **Text** and **Dynamic Text**: `{date}`, `{time}`, `{scale}`, `{map}`, `{crs}`, `{page}`, `{pages}`, `{pageName}` and `{layout}` fill in when the page is drawn; the Format pane's **Insert Dynamic Text** menu, under the text, lists each with what it shows. The text, its