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
30 changes: 30 additions & 0 deletions crates/engine/src/cmd/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ fn report_pages_bytes(
// 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());
// The texts cut off on its layouts' pages, by layout and element.
let mut clipped = ClippedTexts::new();
// Counted, and stopped on Cancel, a page at a time.
rustgis_data::progress::begin(n);
for k in 0..n {
Expand All @@ -1190,6 +1192,17 @@ fn report_pages_bytes(
Some(rustgis_render::report::Page::Layout(li)) => Some(*li),
_ => None,
};
if let Some(li) = page_layout
&& let Some(layout) = s.project.layouts.get(li)
{
// The page's own number, as the page draws it: a text that shows it may only be cut
// off on some pages.
let info = rustgis_render::layout::PageInfo { page: k + 1, pages: pages.len(), page_name: String::new() };
let cut = rustgis_render::layout::clipped_text(layout, &s.project.maps, dpi, &info);
if !cut.is_empty() {
clipped.entry(li).or_default().extend(cut);
}
}
if !pdf {
let (drawn, listed, shown) =
listing(|| rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps));
Expand All @@ -1198,6 +1211,7 @@ fn report_pages_bytes(
keep_drawn(&mut charts, page_layout, shown);
say_report_unfitted(s, &unfitted);
say_report_charts(s, &charts);
say_report_clipped(s, &clipped);
say_sections_not_drawn(s, pages, 1);
return Ok((img.encode_png().map_err(|e| e.to_string())?, pages.len()));
}
Expand Down Expand Up @@ -1232,6 +1246,7 @@ fn report_pages_bytes(
}
say_report_unfitted(s, &unfitted);
say_report_charts(s, &charts);
say_report_clipped(s, &clipped);
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()))
Expand Down Expand Up @@ -1299,6 +1314,21 @@ fn say_report_charts(s: &mut Session, kept: &[(usize, rustgis_render::layout::Dr
}
}

/// The texts cut off on a report's layout pages: each layout's (by its index) elements, by id.
type ClippedTexts = std::collections::BTreeMap<usize, std::collections::BTreeMap<u32, String>>;

/// Says the texts cut off on the report's layout pages as each layout's export says them, one
/// notice a layout, which it names: the advice depends on that layout's elements.
fn say_report_clipped(s: &mut Session, clipped: &ClippedTexts) {
for (li, texts) in clipped {
let Some(layout) = s.project.layouts.get(*li) else { continue };
let texts: Vec<_> = texts.clone().into_iter().collect();
let Some(note) = rustgis_render::layout::clipped_text_notice(layout, &texts) else { continue };
let name = crate::project::short_name(&layout.name);
s.warn(format!("The report, layout \"{name}\": {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();
Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ mod layout_publication_parts;
#[path = "tests/layout_typed_values.rs"]
mod layout_typed_values;

#[path = "tests/layout_report_text.rs"]
mod layout_report_text;

#[path = "tests/sde_connections.rs"]
mod sde_connections;

Expand Down
115 changes: 115 additions & 0 deletions crates/engine/src/tests/layout_report_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! A report says the texts cut off on the layouts it draws as pages (#3195).

use super::*;
use rustgis_data::{Dataset, Field, FieldType, Geometry, GeometryType, Value as Data};

/// A map with the points layer "Shops" and the layout "Sheet".
fn sheet() -> Session {
let mut s = Session::new(sample::world_project());
let mut ds = Dataset::new("Shops", GeometryType::Point, vec![Field::new("n", FieldType::Double)]);
ds.push(Some(Geometry::Point(rustgis_data::Coord::new(0.0, 0.0))), vec![Data::Float(1.0)]);
s.map_mut().add_dataset(ds);
s.run("layout.new", &json!({"name": "Sheet"})).unwrap();
s
}

/// Adds the text `text` to the layout `li`, in a box `w` by `h` millimetres; its element id.
fn text(s: &mut Session, li: usize, text: &str, w: f64, h: f64) -> u64 {
let p = json!({"layout": li, "kind": "text", "text": text, "x": 20, "y": 20, "w": w, "h": h});
s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap()
}

/// Adds a text of three lines to the layout `li`, in a box that holds one.
fn cut(s: &mut Session, li: usize) -> u64 {
text(s, li, "one\ntwo\nthree", 60.0, 5.0)
}

/// 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 cut-off text.
fn reported(s: &mut Session, ext: &str) -> Vec<String> {
let dir = std::env::temp_dir().join(format!("rustgis-3195-{}-{:?}", 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 mark = s.notice_mark();
s.run("report.export", &json!({"report": "R", "path": path, "dpi": 72})).unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("cut off")).collect()
}

/// What a report says of the one text `id` cut off on the layout `li`.
fn of_page(s: &Session, li: usize, id: u64) -> String {
let layout = &s.project.layouts[li];
let note = rustgis_render::layout::clipped_text_notice(layout, &[(id as u32, layout.element(id as u32).unwrap().name.clone())]);
format!("The report, layout \"{}\": {}", layout.name, note.unwrap())
}

#[test]
fn a_report_says_the_text_cut_off_on_its_layout_pages() {
let mut s = sheet();
let id = cut(&mut s, 0);
// The same layout twice: said once, the text counted once.
report(&mut s, &["Sheet", "Sheet"]);
let want = of_page(&s, 0, id);
assert!(want.contains("Text is cut off in 1 layout element(s)"), "{want}");
assert_eq!(reported(&mut s, "pdf"), [want]);
}

#[test]
fn a_report_names_each_layout_with_cut_off_text() {
let mut s = sheet();
let first = cut(&mut s, 0);
s.run("layout.new", &json!({"name": "More"})).unwrap();
let second = cut(&mut s, 1);
assert_eq!(first, second, "each layout numbers its own elements");
report(&mut s, &["Sheet", "More"]);
let want = [of_page(&s, 0, first), of_page(&s, 1, second)];
assert_eq!(reported(&mut s, "pdf"), want);
}

#[test]
fn a_report_says_nothing_of_text_that_fits() {
let mut s = sheet();
text(&mut s, 0, "one", 60.0, 20.0);
report(&mut s, &["Sheet"]);
assert!(reported(&mut s, "pdf").is_empty());
}

#[test]
fn a_report_image_says_the_text_of_its_first_page_alone() {
let mut s = sheet();
let id = cut(&mut s, 0);
report(&mut s, &["Sheet"]);
assert!(reported(&mut s, "png").is_empty(), "the layout is not on the first page");
s.run("report.existingTemplate", &json!({"report": "R", "layout": "Sheet"})).unwrap();
assert_eq!(reported(&mut s, "png"), [of_page(&s, 0, id)]);
}

#[test]
fn a_report_measures_each_layout_page_with_its_own_page_number() {
let mut s = sheet();
// One line while the page number has one digit; two from page 10.
let id = text(&mut s, 0, &["{page}"; 8].join(" "), 18.0, 6.0);
s.run("layout.update", &json!({"element": id, "wrap": true, "size": 10})).unwrap();
report(&mut s, &["Sheet"]);
assert!(reported(&mut s, "pdf").is_empty(), "a one-digit page fits");
for _ in 0..9 {
s.run("report.existingTemplate", &json!({"report": "R", "layout": "Sheet", "end": true})).unwrap();
}
assert_eq!(reported(&mut s, "pdf"), [of_page(&s, 0, id)]);
}
8 changes: 5 additions & 3 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
`layout.update={"element":7,"angle":-90}` to turn one.
- **Text clipping**: the layout view and PNG, PDF and SVG exports warn when a text element or
legend title runs off the page, or a text element has more lines than its height allows.
The notice counts and names the affected elements with their IDs. A line may extend beyond
its element's width while remaining on the page; that alone is not clipped. Reduce the size,
add line breaks, turn on **Wrap to the box**, or resize or move the element to keep all its text;
The notice counts and names the affected elements with their IDs. Exporting a report that draws
a layout as a page says the same of that page's texts, in a notice for each such layout that
names it. A line may extend beyond its element's width while remaining on the page; that alone
is not clipped. Reduce the size, add line breaks, turn on **Wrap to the box**, or resize or move
the element to keep all its text;
when every text it names is already wrapped, the notice suggests only the size and the box, and
a legend's title, which has no wrap, is told to add line breaks. Lines left out below a text's
box are said as soon as they are: `layout.addElement` and `layout.update` warn how many lines
Expand Down
Loading