Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
45 changes: 37 additions & 8 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,17 @@ pub(crate) fn veil_panel(
/// its labels.
const COLORBAR_MM: f64 = 15.0;

/// The height a publication gives its colorbar in a legend's box `ih` mm high inside, in mm,
/// when its title needs `need` (#2493). Over the map it leaves the rows the room they were sized
/// to (`rows`, with the gap above the colorbar), so a title past the box's cap is cut short
/// rather than taking them; but never below a colorbar's own height while the box has it, so
/// the bar isn't lost to rows that fill the box: those are cut and said instead (#2638). In a
/// side column it takes what it needs of the box.
pub(crate) fn colorbar_fit(need: f64, ih: f64, rows: f64, over_map: bool) -> f64 {
let ih = ih.max(0.0);
if over_map { need.min((ih - rows).max(COLORBAR_MM.min(ih))) } else { need.min(ih) }
}

/// A publication layout's colorbar's height, in mm, `width` mm wide: [`COLORBAR_MM`] and a line's
/// pitch more for each line its title wraps to past the first (#2493).
fn colorbar_mm(title: &str, width: f64) -> f64 {
Expand Down Expand Up @@ -2215,8 +2226,17 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
};
let base_w = if sheet { 50.0 * k } else { 40.0 };
let bar_w = base_w.max(bar.as_deref().map_or(0.0, crate::render::layout::colorbar_title_width_mm).min(title_room));
let bar_h = bar.as_deref().map_or(COLORBAR_MM, |title| colorbar_mm(title, bar_w.min(title_room)));
let bar_part = if bar.is_some() { bar_h + if ch > 0.0 { 2.0 } else { 0.0 } } else { 0.0 };
let bar_measured = bar.as_deref().map_or(COLORBAR_MM, |title| colorbar_mm(title, bar_w.min(title_room)));
let bar_gap = if ch > 0.0 { 2.0 } else { 0.0 };
// Over the map the box is never placed taller than 60% of the frame, so the title's lines
Comment thread
giswqs marked this conversation as resolved.
Resolved
// count only up to that, less the rows of the legend beside them (a colorbar's own height, as
// before, at the least): the map isn't moved, veiled or scaled for room the box can't have, and
// each candidate's rows keep their height (the fit test and the placement below). The lines past
// it are said as cut off.
let over_map = column.is_none();
let cap = |rows_h: f64| (fh * 0.6 - 2.0 * pad - rows_h - bar_gap).max(COLORBAR_MM);
let bar_reserve = |rows_h: f64| if over_map { bar_measured.min(cap(rows_h)) } else { bar_measured };
let bar_part = if bar.is_some() { bar_reserve(ch) + bar_gap } else { 0.0 };
let (content_w, content_h) = (if bar.is_some() { cw.max(bar_w) } else { cw }, ch + bar_part);
let legend_in_column = column.is_some();
if content_h > 0.0 && !legend_in_column {
Expand Down Expand Up @@ -2250,7 +2270,11 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
// the frame's area in the background (which the export reads anyway); web tiles aren't fetched.
let coverage = crate::render::layout::frame_coverage(s.map_mut(), center, scale, fw, fh, 384);
// The legend's whole box, around its rows and the colorbar under them.
let whole = |(w, h): (f64, f64)| ((if bar.is_some() { w.max(bar_w) } else { w }) + 2.0 * pad, h + bar_part + 2.0 * pad);
// A candidate's colorbar reservation follows its own rows, which its column count sets.
let whole = |(w, h): (f64, f64)| {
let part = if bar.is_some() { bar_reserve(h) + bar_gap } else { 0.0 };
((if bar.is_some() { w.max(bar_w) } else { w }) + 2.0 * pad, h + part + 2.0 * pad)
};
// The largest rectangle a coverage leaves free inside the graticule's margins, in mm.
let free_room = |(gw, gh, cells): &(u32, u32, Vec<bool>)| {
let (gw, gh) = (*gw as usize, *gh as usize);
Expand Down Expand Up @@ -2859,13 +2883,18 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
// the colorbar asked for (the rows in more columns): fewer lines leave the rows more room.
let (ix, iy, iw, ih) = (r.0 + pad, r.1 + pad, lw - 2.0 * pad, lh - 2.0 * pad);
let bar_need = bar.as_deref().map_or(0.0, |title| colorbar_mm(title, iw));
let bar_fit = bar_need.min(ih);
let rows_h = (ih - bar_fit - if bar.is_some() { bar_part - bar_h } else { 0.0 }).max(0.0);
let bar_fit = colorbar_fit(bar_need, ih, ch + bar_gap, over_map);
let rows_h = (ih - bar_fit - if bar.is_some() { bar_gap } else { 0.0 }).max(0.0);
warnings.extend(legend_fit_warnings(legend_cols, (cw, ch), (iw, rows_h)).into_iter().map(String::from));
let short = bar_need > bar_fit + LEGEND_FIT_MM;
// In a column the colorbar takes the column's width, as its rows do.
if let Some(layer) = bar.as_deref().filter(|_| (base_w > iw + LEGEND_FIT_MM && !legend_in_column) || bar_need > ih + LEGEND_FIT_MM)
{
warnings.push(format!("the legend's box is smaller than the colorbar of '{layer}': it is drawn narrower or shorter"));
if let Some(layer) = bar.as_deref().filter(|_| (base_w > iw + LEGEND_FIT_MM && !legend_in_column) || short) {
// With no height left in the box the bar is not drawn at all, so the notice says so.
if bar_fit > 0.0 {
warnings.push(format!("the legend's box is smaller than the colorbar of '{layer}': it is drawn narrower or shorter"));
} else {
warnings.push(format!("the legend's box has no room for the colorbar of '{layer}': it is left out"));
}
}
if ch > 0.0 && rows_h >= 1.0 {
l.add(
Expand Down
19 changes: 19 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14784,6 +14784,25 @@ fn a_publications_colorbar_title_stays_in_the_legends_box() {
let _ = std::fs::remove_dir_all(&dir);
}

/// A publication's colorbar takes what its title needs, leaving the legend's rows the room they
/// were sized to over the map, but never less than a colorbar's own height while the box has it:
/// rows that fill the box are cut and said, not the bar (#2638).
#[test]
fn a_publications_colorbar_keeps_its_own_height_beside_rows() {
use crate::cmd::layout::colorbar_fit;
// Room to spare: what the title needs.
assert_eq!(colorbar_fit(30.0, 56.0, 22.0, true), 30.0);
// A title past the cap: what the rows leave it.
assert_eq!(colorbar_fit(75.0, 56.0, 22.0, true), 34.0);
// Rows that fill the box (one 69.85 mm row and the gap): the colorbar keeps its 15 mm.
assert_eq!(colorbar_fit(20.0, 56.0, 71.85, true), 15.0);
// A box shorter than a colorbar: all of it, and none of a box with no room.
assert_eq!(colorbar_fit(20.0, 10.0, 9.0, true), 10.0);
assert_eq!(colorbar_fit(20.0, -1.0, 9.0, true), 0.0);
// In a side column: what it needs of the box.
assert_eq!(colorbar_fit(75.0, 56.0, 22.0, false), 56.0);
}

/// A layout frame centred on a pole keeps that centre through `layout.import` and
/// `layout.update`, and says nothing on a polar map, where it's drawn there; on Web Mercator,
/// which can't show a pole, it says the frame is drawn at 85° (#2684).
Expand Down
Loading
Loading