Skip to content
3 changes: 1 addition & 2 deletions crates/engine/src/cmd/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ pub fn map_png(s: &mut Session, width: Option<f64>, height: Option<f64>, 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<f64>, height: Option<f64>, dpi: Option<f64>) -> Result<Vec<u8>, String> {
let view_size = s.view_size;
png_of(s.map_mut(), view_size, width, height, dpi, &Limit::none())
Expand Down
44 changes: 24 additions & 20 deletions crates/engine/src/cmd/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,32 +656,36 @@ fn animation_zip(s: &mut Session, p: &Value) -> Result<Vec<u8>, 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?;
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions crates/engine/src/tests/layout_credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -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();
Expand Down
166 changes: 166 additions & 0 deletions crates/engine/src/tests/map_credits.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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<String> {
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<String> {
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::<String>::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::<String>::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::<String>::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::<String>::new());
}
2 changes: 1 addition & 1 deletion crates/render/src/layout/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
42 changes: 42 additions & 0 deletions crates/render/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
Loading