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
51 changes: 35 additions & 16 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,9 @@ const FIGURE_PT: f64 = 7.0;
/// The graticule steps a publication layout picks from, in degrees.
const GRID_STEPS: &[f64] = &[0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10.0, 15.0, 30.0];

/// The strips a frame's graticule labels take inside its edges, in mm: beside the left and right
/// edges (a latitude such as `10°S` across), and along the top and bottom ones.
/// The strips a frame's graticule labels take inside its edges on an A4 sheet, in mm: beside the
/// left and right edges (a latitude such as `10°S` across), and along the top and bottom ones. A
/// sheet scales them with its labels, by its short side (#2581).
const LABEL_ROOM_SIDE: f64 = 9.0;
const LABEL_ROOM_END: f64 = 6.0;

Expand Down Expand Up @@ -1202,7 +1203,7 @@ fn landscape_suits(aspect: f64, short: f64, long: f64, style: Style) -> bool {
let texts = if style == Style::Panel { 50.0 } else { 100.0 } * k;
let (iw, ih) = (w - 30.0 * k, h - texts);
let fw = if matches!(style, Style::Panel | Style::Editorial | Style::Modular) { 0.68 * iw } else { iw };
let (fw, fh) = (fw - 2.0 * LABEL_ROOM_SIDE, ih - 2.0 * LABEL_ROOM_END);
let (fw, fh) = (fw - 2.0 * LABEL_ROOM_SIDE * k, ih - 2.0 * LABEL_ROOM_END * k);
fw.min(fh / aspect.max(1e-6))
};
map_w(long, short) > 1.03 * map_w(short, long)
Expand Down Expand Up @@ -1357,6 +1358,11 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
// A sheet's sizes follow its short side (A4's 210 mm is 1); a figure keeps its texts at their
// printed size, whatever its width.
let k = if sheet { page_w.min(page_h) / 210.0 } else { 1.0 };
// So do its legend's rows, its scale bar's and graticule's labels and the strips those take
// inside the frame (#2581): A4's sizes times k, within the 4 to 100 points a text size takes
// on every sheet size (k from 0.70 on A5 to 4.0 on A0).
let (legend_pt, bar_pt, grid_pt) = (rustgis_map::layout::LEGEND_TEXT_PT * k, SCALE_BAR_TEXT_PT * k, GRID_LABEL_PT * k);
let (room_side, room_end) = (LABEL_ROOM_SIDE * k, LABEL_ROOM_END * k);
// The sources and the reference that have something in them, each its heading in `head_pt` and
// lines in `body_pt`, `tw` wide. Each one's elements keep their names (`Sources`, `Reference`)
// whatever its heading reads, the heading filled and wrapped like the rest.
Expand Down Expand Up @@ -1859,7 +1865,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
target = (sheet && matches!(style, Style::Panel | Style::Editorial))
.then(|| aspect(s, &extent))
.flatten()
.map(|a| (((lw - 2.0 * LABEL_ROOM_SIDE).max(1.0) * a + 2.0 * LABEL_ROOM_END) * 1.15).max(40.0 * k))
.map(|a| (((lw - 2.0 * room_side).max(1.0) * a + 2.0 * room_end) * 1.15).max(40.0 * k))
// Only a frame clearly too tall is cut.
.filter(|n| *n < 0.97 * lh);
}
Expand All @@ -1882,8 +1888,8 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
// The extent fits within the graticule's label strips (drawn inside the frame along its
// labelled edges), on both sides so it stays centred, and its labels cover none of it.
let edges = GridEdges { top: sheet, bottom: true, left: true, right: sheet };
let strip_w = if edges.left || edges.right { 2.0 * LABEL_ROOM_SIDE } else { 0.0 };
let strip_h = if edges.top || edges.bottom { 2.0 * LABEL_ROOM_END } else { 0.0 };
let strip_w = if edges.left || edges.right { 2.0 * room_side } else { 0.0 };
let strip_h = if edges.top || edges.bottom { 2.0 * room_end } else { 0.0 };
if fw - strip_w < fw / 2.0 || fh - strip_h < fh / 2.0 {
warnings.push("the frame is too small to keep its graticule's labels off the map's edges: they may cover them".into());
}
Expand All @@ -1907,7 +1913,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
fit_scale(s.map(), &extent, inner_w, inner_h).ok_or("the extent can't be drawn on the map's projection")?
};
let scale = round_scale(scale);
let grid = Grid { step, edges, ..Grid::default() };
let grid = Grid { step, edges, label_size: grid_pt, ..Grid::default() };
// A name given past the limit is refused, as layout.new refuses it, not cut (#2639).
let given = super::within("name", "a layout's name", ps(p, "name").unwrap_or("Publication").trim(), super::MAX_NAME)?;
let (name, name_cut) = unique_name(s, "New Publication Layout", "the name given", &given);
Expand Down Expand Up @@ -1950,9 +1956,9 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
e + if !labelled {
0.0
} else if side {
LABEL_ROOM_SIDE
room_side
} else {
LABEL_ROOM_END
room_end
}
};
let (left, right, top, bottom) = (room(edges.left, true), room(edges.right, true), room(edges.top, false), room(edges.bottom, false));
Expand Down Expand Up @@ -2127,7 +2133,14 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
if context_in_legend { Vec::new() } else { legend_context(s.map(), center, scale, (fw, fh)) }
};
let mut context = context_at(s, center, scale);
let mut sizes = crate::render::layout::legend_sizes_mm(s.map_mut(), render_scale, false, false, (&context, &Default::default()));
let mut sizes = crate::render::layout::legend_sizes_with_no_data_mm(
s.map_mut(),
render_scale,
false,
false,
(false, legend_pt),
(&context, &Default::default()),
);
let (mut cw, mut ch) = match sizes {
Some(sizes) => sizes[0],
None => {
Expand All @@ -2153,8 +2166,12 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
// would draw, which sets its length and divisions. The box is the narrowest that draws that same
// bar: the renderer gives a bar at most 85 % of its box, so the box is at least the bar's length
// over 0.85.
let bar_kind = match ElementKind::scale_bar(frame) {
ElementKind::ScaleBar { units, divisions, text_size, .. } => Some((units, divisions, text_size)),
let mut scale_bar = ElementKind::scale_bar(frame);
if let ElementKind::ScaleBar { text_size, .. } = &mut scale_bar {
*text_size = bar_pt;
}
let bar_kind = match &scale_bar {
ElementKind::ScaleBar { units, divisions, text_size, .. } => Some((*units, *divisions, *text_size)),
_ => None,
};
let scale_box = |ground_per_pt: f64, scale: f64| {
Expand Down Expand Up @@ -2379,11 +2396,12 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
} else {
// A legend that can't be measured there (a source that can't be read
// through) leaves that scale untried too, which is said.
let measured = crate::render::layout::legend_sizes_mm(
let measured = crate::render::layout::legend_sizes_with_no_data_mm(
s.map_mut(),
render_scale,
false,
false,
(false, legend_pt),
(&here_context, &Default::default()),
);
if measured.is_none() {
Expand Down Expand Up @@ -2471,11 +2489,12 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
*here
} else {
let at_there = crate::render::layout::frame_render_scale(s.map(), there, tried);
let measured = crate::render::layout::legend_sizes_mm(
let measured = crate::render::layout::legend_sizes_with_no_data_mm(
s.map_mut(),
at_there,
false,
false,
(false, legend_pt),
(&own_context, &Default::default()),
);
// A legend that can't be measured there leaves that move untried, which is
Expand Down Expand Up @@ -2881,7 +2900,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
columns: legend_cols,
labels: Default::default(),
color: None,
text_size: rustgis_map::layout::LEGEND_TEXT_PT,
text_size: legend_pt,
},
);
}
Expand All @@ -2905,7 +2924,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
if let Some((sx, sy, w, h)) = spot_of(Surround::Scale) {
placed.push(("scale bar", (sx, sy, w, h)));
l.add("Scale Box", (sx, sy, w, sh + nh), surround_box(paper));
l.add("Scale Bar", (sx, sy, w, sh), ElementKind::scale_bar(frame));
l.add("Scale Bar", (sx, sy, w, sh), scale_bar);
l.add("Scale", (sx, sy + sh, w, nh), pub_text("{scale}".into(), small_pt, ink, false, TextAlign::Center));
}
// What placing couldn't avoid: a surround over the map's drawing while a clearly freer corner
Expand Down
100 changes: 87 additions & 13 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11829,11 +11829,14 @@ fn piaui_publication(s: &mut Session, profile: &str) -> Result<Value, String> {
)
}

/// The scale Piauí fits `frame` at within the graticule's label strips (9 mm beside its sides, 6 mm
/// along its top and bottom).
/// The scale Piauí fits `frame` at within the graticule's label strips (on A4, 9 mm beside its
/// sides and 6 mm along its top and bottom; on a sheet, scaled with its short side, #2581).
fn piaui_fits(s: &Session, frame: &rustgis_map::layout::Element) -> f64 {
let extent = rustgis_geom::Extent::new(-46.8, -11.6, -38.5, -2.2);
rustgis_map::layout::fit_scale(s.map(), &extent, frame.w - 18.0, frame.h - 12.0).unwrap().1
let l = s.project.layouts.last().unwrap();
let sheet = crate::cmd::layout::PUBLICATION_PROFILES.iter().any(|p| p.sheet && (p.width, p.height) == (l.width, l.height));
let k = if sheet { l.width.min(l.height) / 210.0 } else { 1.0 };
rustgis_map::layout::fit_scale(s.map(), &extent, frame.w - 18.0 * k, frame.h - 12.0 * k).unwrap().1
}

/// `layout.publication` (#682): every profile builds a layout that fits its page, the map frame
Expand Down Expand Up @@ -11912,8 +11915,8 @@ fn publication_layouts_fit_their_pages() {
// A round scale, two significant figures, at which the extent still fits.
let unit = 10f64.powf(scale.log10().floor() - 1.0);
assert!(((scale / unit) - (scale / unit).round()).abs() < 1e-6, "{profile}: 1:{scale}");
// Fitted within the graticule's label strips (9 mm beside the frame's sides, 6 mm along its
// top and bottom), so the labels cover none of the extent.
// Fitted within the graticule's label strips (on A4, 9 mm beside the frame's sides and 6 mm
// along its top and bottom; scaled with the sheet), so the labels cover none of the extent.
let fits = piaui_fits(&s, frame);
assert!(fits <= *scale && *scale < fits * 1.1, "{profile}: 1:{scale} for 1:{fits}: {}", r["warnings"]);
assert_eq!(grid.step, 2.0, "{profile}: Piauí's 9.4° take a 2° graticule");
Expand Down Expand Up @@ -12417,11 +12420,74 @@ fn publication_layouts_take_a_style() {
let _ = std::fs::remove_dir_all(&dir);
}

/// On an A5 portrait sheet, Piauí's legend has no room beside the area at the scale that fits it:
/// the map is drawn at a smaller one, at most half, what runs on past the extent veiled for the
/// surrounds, and both are said; every element stays on the page (#2309).
/// A publication sheet's legend, scale bar and graticule labels follow its short side as its
/// title does (#2581, part 3): 9, 8 and 8 points on A4, four times that on A0; a figure keeps
/// them. A sheet off A4 is saved as needing `layout-text-size`, kept as it is when read again,
/// and its sized surrounds hold their text, also after the frame's scale changes.
#[test]
fn a_publication_sheets_surround_texts_follow_its_short_side() {
use rustgis_map::layout::{ElementKind, GRID_LABEL_PT, LEGEND_TEXT_PT, SCALE_BAR_TEXT_PT};
let mut s = world();
let cases: [(&str, Option<&str>, f64); 8] = [
("a4-portrait", None, 1.0),
("a4-landscape", None, 1.0),
("figure-1col", None, 1.0),
("thesis-half", None, 1.0),
("letter-portrait", None, 215.9 / 210.0),
("a5-portrait", None, 148.0 / 210.0),
("a0-portrait", None, 841.0 / 210.0),
("a0-portrait", Some("panel"), 841.0 / 210.0),
];
for (profile, style, k) in cases {
let mut p = json!({"profile": profile, "extent": [-46.8, -11.6, -38.5, -2.2], "author": ""});
if let Some(style) = style {
p["style"] = json!(style);
}
let r = s.run("layout.publication", &p).unwrap_or_else(|e| panic!("{profile}: {e}"));
let case = format!("{profile} {style:?}");
assert!(!r["warnings"].to_string().contains("more than fits"), "{case}: {r}");
let l = s.project.layouts.last().unwrap().clone();
let kind = |name: &str| &l.elements.iter().find(|e| e.name == name).unwrap_or_else(|| panic!("{case}: no {name}")).kind;
let near = |a: f64, b: f64| (a - b).abs() < 1e-9;
let ElementKind::Legend { text_size, .. } = kind("Legend") else { panic!("{case}: legend") };
assert!(near(*text_size, LEGEND_TEXT_PT * k), "{case}: a {text_size} pt legend");
let ElementKind::ScaleBar { text_size, .. } = kind("Scale Bar") else { panic!("{case}: scale bar") };
assert!(near(*text_size, SCALE_BAR_TEXT_PT * k), "{case}: a {text_size} pt scale bar");
let ElementKind::MapFrame { grid: Some(g), .. } = kind("Map Frame") else { panic!("{case}: grid") };
assert!(near(g.label_size, GRID_LABEL_PT * k), "{case}: {} pt grid labels", g.label_size);
// Saved: an A4 sheet or a figure needs nothing new; another sheet needs its text sizes
// read, and comes back with them.
let mut alone = Project::default();
alone.maps = s.project.maps.clone();
alone.layouts = vec![l.clone()];
let saved = serde_json::to_value(&alone).unwrap();
let sized = saved["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-text-size")));
assert_eq!(sized, k != 1.0, "{case}: {}", saved["requires"]);
assert_eq!(Project::from_json(&saved.to_string()).unwrap().layouts, alone.layouts, "{case}");
// Its legend's rows and its scale bar's labels fit their boxes, as measured for them.
let info = rustgis_render::layout::PageInfo { page: 1, pages: 1, page_name: String::new() };
let lost = rustgis_render::layout::text_losses(&l, &s.project.maps, 96.0, &info);
assert!(!lost.iter().any(|t| t.name == "Legend" || t.name == "Scale Bar"), "{case}: {lost:?}");
}
// The A0 sheet's frame zoomed out: its scale bar's box is measured again for its labels.
let frame = s.project.layouts.last().unwrap().elements.iter().find(|e| e.name == "Map Frame").unwrap().clone();
let ElementKind::MapFrame { scale, .. } = frame.kind else { panic!("no map frame") };
s.run("layout.update", &json!({"element": frame.id, "scale": scale * 3.0})).unwrap();
let info = rustgis_render::layout::PageInfo { page: 1, pages: 1, page_name: String::new() };
let l = s.project.layouts.last().unwrap();
let lost = rustgis_render::layout::text_losses(l, &s.project.maps, 96.0, &info);
assert!(!lost.iter().any(|t| t.name == "Scale Bar"), "{lost:?}");
let bar = l.elements.iter().find(|e| e.name == "Scale Bar").unwrap();
let ElementKind::ScaleBar { text_size, .. } = &bar.kind else { panic!("no scale bar") };
assert!((text_size - SCALE_BAR_TEXT_PT * 841.0 / 210.0).abs() < 1e-9, "the bar keeps its size: {text_size}");
}

/// On an A5 portrait sheet, Piauí's legend, set at the sheet's smaller size (#2581), finds room
/// beside the area at the scale that fits it: the map is moved within its frame rather than drawn
/// at a smaller scale and veiled, as it was with A4's sizes (#2309); every element stays on the
/// page.
#[test]
fn a_publication_on_a5_portrait_keeps_its_legend_at_a_smaller_scale() {
fn a_publication_on_a5_portrait_keeps_its_legend_at_the_scale_that_fits() {
let mut s = world();
let r = piaui_publication(&mut s, "a5-portrait").unwrap();
let l = s.project.layouts.last().unwrap();
Expand All @@ -12432,10 +12498,9 @@ fn a_publication_on_a5_portrait_keeps_its_legend_at_a_smaller_scale() {
let frame = l.elements.iter().find(|e| e.kind.code() == "mapFrame").unwrap();
let fits = piaui_fits(&s, frame);
let scale = r["scale"].as_f64().unwrap();
assert!(fits <= scale && scale <= fits * 2.0, "1:{scale} for 1:{fits}");
assert_eq!(r["veiled"], true, "{r}");
let said = r["warnings"].to_string();
assert!(said.contains("to leave the legend room") && said.contains("veiled"), "{said}");
assert!(fits <= scale && scale < fits * 1.1, "1:{scale} for 1:{fits}");
assert_eq!(r["veiled"], false, "{r}");
assert!(!r["warnings"].to_string().contains("veiled"), "{r}");
}

/// Every page size `layout.new` offers has a publication sheet, portrait and landscape, of its
Expand All @@ -12454,6 +12519,15 @@ fn publication_sheets_follow_the_page_sizes() {
assert_eq!(landscape.label, format!("{label} landscape sheet"));
}
assert_eq!(PUBLICATION_PROFILES.iter().filter(|p| p.sheet).count(), 2 * pages.len());
// Each sheet's surrounds, A4's sizes scaled by its short side, take a size a text size can
// have, so none is brought into range when the project is read again (#2581).
let range = rustgis_map::layout::LAYOUT_TEXT_SIZE;
for (id, _, w, h) in pages {
let k = w.min(*h) / 210.0;
for pt in [rustgis_map::layout::LEGEND_TEXT_PT, rustgis_map::layout::SCALE_BAR_TEXT_PT, rustgis_map::layout::GRID_LABEL_PT] {
assert!(range.contains(&(pt * k)), "{id}: {} pt", pt * k);
}
}
}

/// A publication layout's locator (#682, part 2): on a sheet, by default, a globe (a new map of
Expand Down
4 changes: 3 additions & 1 deletion docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
under them. When the legend sits at the right of the map (its right edge in the frame's
right tenth), starting between 45 % and 70 % of the frame's width, the right column starts
where the legend does, unless its texts would then need a taller box. Text sizes follow the
sheet's short side: an A3's are 1.41 times an A4's, an A0's four times. Given its size alone
sheet's short side: an A3's are 1.41 times an A4's, an A0's four times. So do the legend's
text size, the scale bar's and the graticule's (9, 8 and 8 points on A4) and the strips the
graticule's labels take inside the frame. Given its size alone
(`"a4"`, the default, or any other: `"letter"`, `"a0"`…), a sheet is turned to
portrait or landscape, whichever the map comes out larger on: the area's extent east to west
against north to south, on the map's projection, with the room the style leaves the map. The
Expand Down
Loading