diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index 36fd72fbef..27c845d70d 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -248,8 +248,7 @@ pub fn map_png(s: &mut Session, width: Option, height: Option, dpi: Op } /// [`map_png`] for a caller that draws several frames inside one [`saying_shadows`], which says -/// what they all said of their cast shadows once. Only the desktop's animation exports draw so. -#[cfg(not(target_arch = "wasm32"))] +/// what they all said of their cast shadows once. The animation and time exports draw so. pub(super) fn map_png_unsaid(s: &mut Session, width: Option, height: Option, dpi: Option) -> Result, String> { let view_size = s.view_size; png_of(s.map_mut(), view_size, width, height, dpi, &Limit::none()) diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index 8a8754dd70..4f42f0fd6c 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -656,32 +656,36 @@ fn animation_zip(s: &mut Session, p: &Value) -> Result, String> { m.camera.center = mc; } m.set_scale(scale); - let png = super::share::map_png(s, Some(w), Some(h), Some(96.0))?; + let png = super::share::map_png_unsaid(s, Some(w), Some(h), Some(96.0))?; n += 1; frames.push((format!("frame_{n:04}.png"), png)); Ok(()) }; - let mut result = Ok(()); - 'frames: for pair in keys.windows(2) { - let [a, b2] = pair else { continue }; - let steps = (b2.seconds * fps).round().max(1.0) as usize; - for i in 0..steps { - let t = i as f64 / steps as f64; - // Smooth in and out; scale changes geometrically. - let e = t * t * (3.0 - 2.0 * t); - let c = Coord::new(a.center.x + (b2.center.x - a.center.x) * e, a.center.y + (b2.center.y - a.center.y) * e); - let sc = rustgis_map::camera::scale_between(a.scale, b2.scale, e); - if let Err(e) = render(s, c, sc) { - result = Err(e); - break 'frames; + // What the frames say of their cast shadows and credits is said once, about the animation. + let result = super::share::saying_shadows(s, "The animation", |s| { + let mut result = Ok(()); + 'frames: for pair in keys.windows(2) { + let [a, b2] = pair else { continue }; + let steps = (b2.seconds * fps).round().max(1.0) as usize; + for i in 0..steps { + let t = i as f64 / steps as f64; + // Smooth in and out; scale changes geometrically. + let e = t * t * (3.0 - 2.0 * t); + let c = Coord::new(a.center.x + (b2.center.x - a.center.x) * e, a.center.y + (b2.center.y - a.center.y) * e); + let sc = rustgis_map::camera::scale_between(a.scale, b2.scale, e); + if let Err(e) = render(s, c, sc) { + result = Err(e); + break 'frames; + } } } - } - if result.is_ok() - && let Some(last) = keys.last() - { - result = render(s, last.center, last.scale); - } + if result.is_ok() + && let Some(last) = keys.last() + { + result = render(s, last.center, last.scale); + } + result + }); s.map_mut().camera = saved; s.view_size = saved_size; result?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 40e499f3f5..6b2bab3a6a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -151,6 +151,8 @@ mod layout_chart_notices; #[path = "tests/layout_credits.rs"] mod layout_credits; +#[path = "tests/map_credits.rs"] +mod map_credits; #[path = "tests/layout_publication_parts.rs"] mod layout_publication_parts; diff --git a/crates/engine/src/tests/layout_credits.rs b/crates/engine/src/tests/layout_credits.rs index 6059e94647..853f245f01 100644 --- a/crates/engine/src/tests/layout_credits.rs +++ b/crates/engine/src/tests/layout_credits.rs @@ -6,10 +6,10 @@ const CREDIT: &str = "© A tile provider whose credit is longer than a small map const ADVICE: &str = "its tile credits don't fit in it, so their ends are cut off; make it larger, or show fewer tile layers in it"; /// A temporary directory of this thread's, removed however the test ends. -struct Dir(std::path::PathBuf); +pub(super) struct Dir(pub(super) std::path::PathBuf); impl Dir { - fn new(tag: &str) -> Self { + pub(super) fn new(tag: &str) -> Self { let dir = std::env::temp_dir().join(format!("rustgis-3474-{tag}-{}-{:?}", std::process::id(), std::thread::current().id())); std::fs::create_dir_all(&dir).unwrap(); Dir(dir) @@ -23,7 +23,12 @@ impl Drop for Dir { } /// The world with a one-tile MBTiles layer crediting `CREDIT`, and a blank layout "Credits". -fn credited(dir: &Dir) -> Session { +pub(super) fn credited(dir: &Dir) -> Session { + credited_with(dir, CREDIT) +} + +/// [`credited`] with a credit of its own. +pub(super) fn credited_with(dir: &Dir, credit: &str) -> Session { let path = dir.0.join("tiles.mbtiles"); let mut tile = tiny_skia::Pixmap::new(256, 256).unwrap(); tile.fill(tiny_skia::Color::from_rgba8(200, 220, 240, 255)); @@ -33,7 +38,7 @@ fn credited(dir: &Dir) -> Session { INSERT INTO metadata VALUES ('name', 'Blue'), ('format', 'png'), ('minzoom', '0'), ('maxzoom', '0');", ) .unwrap(); - db.execute("INSERT INTO metadata VALUES ('attribution', ?1)", [CREDIT]).unwrap(); + db.execute("INSERT INTO metadata VALUES ('attribution', ?1)", [credit]).unwrap(); db.execute("INSERT INTO tiles VALUES (0, 0, 0, ?1)", [tile.encode_png().unwrap()]).unwrap(); drop(db); let mut s = world(); diff --git a/crates/engine/src/tests/map_credits.rs b/crates/engine/src/tests/map_credits.rs new file mode 100644 index 0000000000..46ee81e5c4 --- /dev/null +++ b/crates/engine/src/tests/map_credits.rs @@ -0,0 +1,166 @@ +//! A map export whose view is narrower than its tile credits says so (#3581). + +use super::layout_credits::{Dir, credited, credited_with}; +use super::*; + +/// The notice, with the points the credit overruns by as `N` ([`credits_said`] writes it so). +const SAID: &str = "the tile credits are N points wider than the picture, so their ends are cut off; fewer tile layers, or a wider picture, would show them whole"; + +/// What the notices since `mark` say of the tile credits, each with its overrun written as `N`. +fn credits_said(s: &Session, mark: u64) -> Vec { + s.notices_since(mark).iter().filter_map(|n| overrun(&n.text).map(|(said, _)| said)).collect() +} + +/// A tile-credit notice with its overrun in points written as `N`, and that overrun. +fn overrun(text: &str) -> Option<(String, u32)> { + let at = text.find("the tile credits are ")? + "the tile credits are ".len(); + let (points, tail) = text.get(at..)?.split_once(' ')?; + let tail = tail.replacen("point wider", "points wider", 1); + Some((format!("{}N {tail}", &text[..at]), points.parse().ok()?)) +} + +/// What `share.exportMap` to `name` at a view `width` points wide and `dpi` says of the tile credits. +fn exported_at(s: &mut Session, dir: &Dir, name: &str, width: f64, dpi: f64) -> Vec { + let mark = s.notice_mark(); + s.run("share.exportMap", &json!({"path": dir.0.join(name), "width": width, "height": 100, "dpi": dpi})).unwrap(); + credits_said(s, mark) +} + +fn exported(s: &mut Session, dir: &Dir, name: &str, width: f64) -> Vec { + exported_at(s, dir, name, width, 72.0) +} + +#[test] +fn an_export_narrower_than_its_credit_says_so() { + let dir = Dir::new("map-narrow"); + let mut s = credited(&dir); + let said = exported(&mut s, &dir, "m.png", 100.0); + assert_eq!(said, [format!("{}: {SAID}", dir.0.join("m.png").display())]); +} + +#[test] +fn an_export_that_holds_its_credit_says_nothing() { + let dir = Dir::new("map-whole"); + let mut s = credited(&dir); + assert_eq!(exported(&mut s, &dir, "m.png", 800.0), Vec::::new()); +} + +/// The canvas is measured in points, however many pixels a point has: the same view says the same +/// overrun at 72 and at 144 DPI, and one that fits in points doesn't say it at a DPI whose pixels +/// alone would be more than the credit. +#[test] +fn the_credit_is_measured_in_points_at_any_dpi() { + let dir = Dir::new("map-dpi"); + let mut s = credited(&dir); + let points = |s: &mut Session, name: &str, width: f64, dpi: f64| { + let mark = s.notice_mark(); + s.run("share.exportMap", &json!({"path": dir.0.join(name), "width": width, "height": 100, "dpi": dpi})).unwrap(); + s.notices_since(mark).iter().find_map(|n| overrun(&n.text).map(|(_, p)| p)) + }; + let low = points(&mut s, "low.png", 100.0, 72.0); + assert!(low.is_some()); + assert_eq!(points(&mut s, "high.png", 100.0, 144.0), low); + assert_eq!(points(&mut s, "wide.png", 800.0, 300.0), None); +} + +#[test] +fn the_map_pdf_that_holds_its_credit_says_nothing() { + let dir = Dir::new("map-pdf-whole"); + let mut s = credited(&dir); + s.view_size = (800.0, 100.0); + let mark = s.notice_mark(); + crate::cmd::share::map_pdf(&mut s, 96.0).unwrap(); + assert_eq!(credits_said(&s, mark), Vec::::new()); +} + +#[test] +fn an_export_without_tile_layers_says_nothing() { + let dir = Dir::new("map-plain"); + let mut s = world(); + assert_eq!(exported(&mut s, &dir, "m.png", 100.0), Vec::::new()); +} + +#[test] +fn the_map_pdf_narrower_than_its_credit_says_so() { + let dir = Dir::new("map-pdf"); + let mut s = credited(&dir); + s.view_size = (100.0, 100.0); + let mark = s.notice_mark(); + crate::cmd::share::map_pdf(&mut s, 96.0).unwrap(); + assert_eq!(credits_said(&s, mark), [format!("The map PDF: {SAID}")]); +} + +#[test] +fn the_map_image_narrower_than_its_credit_says_so() { + let dir = Dir::new("map-image"); + let mut s = credited(&dir); + let mark = s.notice_mark(); + crate::cmd::share::map_png(&mut s, Some(100.0), Some(100.0), None).unwrap(); + assert_eq!(credits_said(&s, mark), [format!("The map image: {SAID}")]); +} + +#[test] +fn many_frames_say_it_once() { + let dir = Dir::new("map-frames"); + let mut s = credited(&dir); + let view = rustgis_render::View { width: 100.0, height: 100.0, pixels_per_point: 1.0, camera: s.map().camera, sky: false }; + let (_, said) = rustgis_render::shadows_said(|| { + for _ in 0..3 { + rustgis_render::render_png(s.map_mut(), &view).unwrap(); + } + }); + assert_eq!(said.notices.iter().filter(|n| n.contains("tile credits")).count(), 1, "{:?}", said.notices); +} + +/// An animation draws a frame for each step of each move, and says it once, not once a frame. +#[test] +fn an_animation_says_it_once() { + let dir = Dir::new("map-animation"); + let mut s = credited(&dir); + s.run("animation.add", &Value::Null).unwrap(); + s.run("map.goToXY", &json!({"lon": 2.35, "lat": 48.85, "scale": 4_000_000})).unwrap(); + s.run("animation.add", &json!({"seconds": 1})).unwrap(); + let mark = s.notice_mark(); + s.run("animation.export", &json!({"path": dir.0.join("a.zip"), "fps": 5, "width": 100, "height": 100})).unwrap(); + assert_eq!(credits_said(&s, mark), [format!("The animation: {SAID}")]); +} + +/// The project thumbnail is a fixed 240 × 160 picture the credit can't fit, and says so. +#[test] +fn the_project_thumbnail_says_it_too() { + let dir = Dir::new("map-thumbnail"); + let mut s = credited(&dir); + let mark = s.notice_mark(); + s.run("project.thumbnail", &json!({"create": true})).unwrap(); + assert_eq!(credits_said(&s, mark), [format!("The map image: {SAID}")]); +} + +/// The notice says by how many points the credit is wider than the picture: 50 points less width, +/// 50 points more overrun (each rounded to a point). +#[test] +fn the_notice_says_how_far_the_credit_overruns() { + let dir = Dir::new("map-overrun"); + let mut s = credited(&dir); + let points = |s: &mut Session, name: &str, width: f64| { + let mark = s.notice_mark(); + s.run("share.exportMap", &json!({"path": dir.0.join(name), "width": width, "height": 100, "dpi": 72})).unwrap(); + s.notices_since(mark).iter().find_map(|n| overrun(&n.text).map(|(_, p)| p)).unwrap() + }; + let (narrow, wide) = (points(&mut s, "narrow.png", 100.0), points(&mut s, "wide.png", 150.0)); + assert!(narrow > 50, "{narrow}"); + assert!((i64::from(narrow) - i64::from(wide) - 50).abs() <= 1, "{narrow} and {wide}"); +} + +/// A tile source's credit with line breaks is drawn as one line: tile sources collapse every run of +/// white space to one space (`plain_text`), so its width is that of the whole credit, and the notice +/// says so when that whole doesn't fit. (One typed on a layer keeps its breaks: see the render +/// test `a_credit_with_line_breaks_is_as_wide_as_its_widest_line`.) +#[test] +fn a_tile_sources_credit_with_line_breaks_is_one_line() { + let line = "© A provider whose credit has lines"; + let dir = Dir::new("map-lines"); + let mut s = credited_with(&dir, &[line; 8].join("\n")); + // Each line fits 300 points; the eight of them, drawn as one line, don't. + assert_eq!(exported(&mut s, &dir, "cut.png", 300.0).len(), 1); + assert_eq!(exported(&mut s, &dir, "whole.png", 2000.0), Vec::::new()); +} diff --git a/crates/render/src/layout/frame.rs b/crates/render/src/layout/frame.rs index f11f03c1b8..0bd4b10b00 100644 --- a/crates/render/src/layout/frame.rs +++ b/crates/render/src/layout/frame.rs @@ -114,7 +114,7 @@ pub(super) fn credit_leaves_frame((w, h): (f32, f32), tw: f32, (bottom, right): if tw <= 0.0 { return false; } - if tw > w { + if crate::credit_cut_off(w, tw) { return true; } let Some(shape) = shape.filter(|s| !matches!(s, FrameShape::Rectangle)) else { return false }; diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 4a25e48c62..1d047805fe 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -6577,6 +6577,48 @@ fn a_credit_within_a_margin_of_the_frames_width_is_drawn_whole() { assert!((crate::attribution_center(100.0, 140.0, 0.0) - 50.0).abs() < 1e-3); } +/// A credit is cut off only when wider than the canvas: one exactly as wide is drawn whole (#3581). +#[test] +fn a_credit_is_cut_off_only_when_wider_than_the_canvas() { + assert!(!crate::credit_cut_off(100.0, 100.0)); + assert!(!crate::credit_cut_off(100.0, 60.0)); + assert!(crate::credit_cut_off(100.0, 100.5)); +} + +/// A credit with line breaks is as wide as its widest line, as `text::draw` lays it out (a layer +/// added with `tiles.add` keeps its credit as typed), not as one long run (#3581). +#[test] +fn a_credit_with_line_breaks_is_as_wide_as_its_widest_line() { + let wide = "a much longer first line of a credit"; + let width = |t: &str| crate::attribution_text(&[t.to_string()]).unwrap().1; + assert!((width(&format!("{wide}\nshort")) - width(wide)).abs() < 1e-3); + assert!(width(&format!("{wide}\n{wide}")) < width(&format!("{wide} {wide}"))); +} + +/// A layout frame judges a credit typed with line breaks by its widest line too (#3581): a frame +/// that holds each line isn't said to cut it, though its lines side by side wouldn't fit. +#[test] +fn a_frame_holding_each_line_of_a_credit_does_not_cut_it() { + let line = "© A provider whose credit has lines"; + let credit = [line; 8].join("\n"); + let (_, tw) = crate::attribution_text(std::slice::from_ref(&credit)).unwrap(); + let one_run = crate::text::measure(&credit.replace('\n', " "), crate::ATTRIBUTION_SIZE, false); + let w = tw + 40.0; + assert!(one_run > w, "the eight lines in one run ({one_run}) must not fit {w}"); + assert!(!credit_leaves_frame((w, 100.0), tw, (0.0, 0.0), None)); + assert!(credit_leaves_frame((tw - 1.0, 100.0), tw, (0.0, 0.0), None)); +} + +/// The notice names the overrun in whole points, rounded up, in the singular for one (#3581). +#[test] +fn the_cut_credit_notice_counts_its_points() { + let said = |over: f32| crate::credit_overrun_notice(over); + assert!(said(214.2).starts_with("the tile credits are 215 points wider than the picture"), "{}", said(214.2)); + assert!(said(0.3).starts_with("the tile credits are 1 point wider than the picture"), "{}", said(0.3)); + assert!(said(1.0).starts_with("the tile credits are 1 point wider than the picture"), "{}", said(1.0)); + assert!(said(1.01).starts_with("the tile credits are 2 points wider than the picture"), "{}", said(1.01)); +} + /// `cut_credits` lists each frame once, in the order first drawn; a call inside another lists /// nothing and leaves its frames to the outer one; nothing is kept outside one. #[test] diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 662a99c32f..c405463070 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -9956,7 +9956,7 @@ fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option Result { text::draw(&mut pm, l, view.pixels_per_point as f32); } text::scale_bar(&mut pm, view, map.ground_res_at(&view.camera)); - draw_attribution(&mut pm, &r.attribution, view.pixels_per_point as f32, (0.0, 0.0)); + draw_export_attribution(&mut pm, &r.attribution, view.pixels_per_point as f32); Ok(pm.into_pixmap()) } @@ -10114,14 +10114,15 @@ pub(crate) const ATTRIBUTION_SIZE: f32 = 9.0; /// How far in from the right edge they end, in points. pub(crate) const ATTRIBUTION_MARGIN: f32 = 4.0; -/// The tile layers' credits as [`draw_attribution`] writes them, and their whole width in points; -/// none when there are none. +/// The tile layers' credits as [`draw_attribution`] writes them, and their width in points: that of +/// the widest of their lines, as `text::draw` lays them out (a credit typed with line breaks is not +/// one long run); none when there are none. pub(crate) fn attribution_text(attribution: &[String]) -> Option<(String, f32)> { if attribution.is_empty() { return None; } let text = attribution.join(" | "); - let tw = text::measure(&text, ATTRIBUTION_SIZE, false); + let tw = text::lines(&text).map(|line| text::measure(line, ATTRIBUTION_SIZE, false)).fold(0.0, f32::max); Some((text, tw)) } @@ -10134,6 +10135,35 @@ pub(crate) fn attribution_center(w: f32, tw: f32, right: f32) -> f32 { (w - right - half - ATTRIBUTION_MARGIN).max(half) } +/// Whether a credit `tw` points wide runs past the edges of a canvas `w` points wide, so that its +/// ends are cut off ([`attribution_center`] keeps one that fits inside). +pub(crate) fn credit_cut_off(w: f32, tw: f32) -> bool { + tw > w +} + +/// What an export says of a credit `over` points wider than the picture: by how many whole points +/// (rounded up, so a small overrun never reads 0), and what would show it whole. +pub(crate) fn credit_overrun_notice(over: f32) -> String { + let points = over.ceil().max(1.0) as u32; + let unit = if points == 1 { "point" } else { "points" }; + format!( + "the tile credits are {points} {unit} wider than the picture, so their ends are cut off; fewer tile layers, or a wider picture, would show them whole" + ) +} + +/// [`draw_attribution`] on a map drawn for an export, which says when the credit is cut off, and by +/// how many points it is wider than the picture (#3581). Said here, not in [`draw_attribution`], +/// which a layout's frame draws with a notice of its own. +fn draw_export_attribution(pm: &mut Canvas, attribution: &[String], ppp: f32) { + let w = pm.width() as f32 / ppp; + if let Some((_, tw)) = attribution_text(attribution) + && credit_cut_off(w, tw) + { + approximations::note("\0credits cut".to_string(), credit_overrun_notice(tw - w)); + } + draw_attribution(pm, attribution, ppp, (0.0, 0.0)); +} + /// The tile layers' credits, small, in the bottom-right corner: `inset` points further in (up /// from the bottom, in from the right), to keep clear of what sits along those edges. pub(crate) fn draw_attribution(pm: &mut Canvas, attribution: &[String], ppp: f32, (bottom, right): (f32, f32)) {