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
39 changes: 26 additions & 13 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3550,13 +3550,13 @@ fn say_text_fit(s: &mut Session, li: usize, id: u32) {
};
let Some(loss) = text_fit_loss(s, li, id) else { return };
// A scale bar's labels (#2581): its box too small for them, or one off the page.
let said = if bar {
let notices = if bar {
rustgis_render::layout::scale_bar_fit_notices(&[loss])
} else {
rustgis_render::layout::lines_left_out_notice(&loss).into_iter().collect()
};
for said in said {
s.warn(said);
for notice in notices {
s.warn(notice);
}
}

Expand Down Expand Up @@ -5521,7 +5521,10 @@ fn update(s: &mut Session, p: &Value) -> CmdResult {
let refit = basis.filter(|basis| {
// A text size given back (`null`) changes the bar's labels too.
let restyled = id == basis.bar_id && (given(&["units", "divisions"]) || p.get("textSize").is_some());
(id == basis.frame && given(&["scale", "w"])) || (id == basis.text_id && given(&["text", "size", "bold"])) || restyled
// Its frame's centre and map fill the text's tokens and set the bar's ground too.
(id == basis.frame && given(&["scale", "w", "center", "map"]))
|| (id == basis.text_id && given(&["text", "size", "bold"]))
|| restyled
});
// A map frame drawn at another view: its scale bars' lengths and labels change (#2581).
let frame_view = given(&["scale", "center", "bearing", "pitch", "map"])
Expand All @@ -5531,8 +5534,14 @@ fn update(s: &mut Session, p: &Value) -> CmdResult {
} else if let Some(basis) = &refit {
refit_scale_box(s, li, basis);
}
// After the scale box is refitted: a text is measured in the box it is saved with.
if shown || TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) || BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some()) {
// After the scale box is refitted: a text is measured in the box it is saved with, and a scale
// bar moved, whose labels can then run off the page, too.
let bar = s.project.layouts.get(li).and_then(|l| l.element(id)).is_some_and(|e| matches!(e.kind, ElementKind::ScaleBar { .. }));
if shown
|| TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null()))
|| BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some())
|| (bar && given(&["x", "y"]))
{
say_text_fit(s, li, id);
}
say_held(s, "Element Properties", held);
Expand Down Expand Up @@ -5573,11 +5582,12 @@ fn scale_text_mm(text: &str, text_pt: f64, bold: bool) -> f64 {
/// What a publication places over its map, which a scale bar box measured again must stay off.
const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"];

/// What a publication's scale bar box is measured from: its frame (id, centre, scale, width), its
/// bar (id, units, divisions) and the text under the bar (id, text, size, boldness).
/// What a publication's scale bar box is measured from: its frame (id, map, centre, scale, width),
/// its bar (id, units, divisions, text size) and the text under the bar (id, text, size, boldness).
#[derive(Clone)]
struct BoxBasis {
frame: u32,
map: String,
center: Coord,
scale: f64,
frame_w: f64,
Expand Down Expand Up @@ -5629,9 +5639,10 @@ fn scale_box_basis(s: &Session, li: usize) -> Option<BoxBasis> {
return None;
};
let fr = l.element(*frame)?;
let ElementKind::MapFrame { center, scale, .. } = &fr.kind else { return None };
let ElementKind::MapFrame { map, center, scale, .. } = &fr.kind else { return None };
Some(BoxBasis {
frame: *frame,
map: map.clone(),
center: *center,
scale: *scale,
frame_w: fr.w,
Expand Down Expand Up @@ -5663,7 +5674,8 @@ fn frame_view_changed(s: &mut Session, li: usize, frame: u32, basis: Option<&Box
}
}

/// A publication's scale bar box measured again after its frame's scale or width changed (#1379):
/// A publication's scale bar box measured again after its frame's scale, width, centre or map
/// changed (#1379, #2581), each measured on its own map and centre:
/// the "Scale Box", the "Scale Bar" of `frame` and the "Scale" under it, as a publication lays them
/// out (one place, one width, none locked), take the width that bar and the text under it (its
/// tokens filled as the page draws them, measured as drawn) now need, kept on the side of the
Expand All @@ -5680,8 +5692,9 @@ fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) {
let Some(now) = scale_box_basis(s, li).filter(|b| b.frame == frame) else { return };
let Some(l) = s.project.layouts.get(li) else { return };
let Some(fr) = l.element(frame) else { return };
let ElementKind::MapFrame { map, .. } = &fr.kind else { return };
let Some(m) = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return };
// Each basis measured on its own map: a frame given another map fills the text from it.
let map_of = |b: &BoxBasis| s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&b.map));
let (Some(m), Some(was)) = (map_of(&now), map_of(before)) else { return };
let named = |name: &str| l.elements.iter().find(|e| e.name == name);
let (Some(boxed), Some(bar), Some(text)) = (named("Scale Box"), named("Scale Bar"), named("Scale")) else { return };
let (ElementKind::ScaleBar { frame: of, .. }, ElementKind::Text { text: written, letter_spacing, font, font_name, .. }) =
Expand All @@ -5708,7 +5721,7 @@ fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) {
return;
}
// The width the box had from its basis before the change: another one was set by hand.
if (boxed.w - basis_width(l, m, before)).abs() > 0.01 {
if (boxed.w - basis_width(l, was, before)).abs() > 0.01 {
return;
}
let w = basis_width(l, m, &now);
Expand Down
39 changes: 19 additions & 20 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool {
"layout.update" => {
params.get("textSize").is_some_and(|v| !v.is_null()) || params.get("grid").is_some_and(|g| sized(g, "labelSize", GRID_LABEL_PT))
}
"layout.import" => {
["\"text_size\"", "\"label_size\""].iter().any(|key| imported_layout_uses(params, key, |e| e.kind.has_custom_text_size()))
}
"layout.import" => imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_size)),
_ => false,
}
Expand All @@ -227,7 +225,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool {
fn command_uses_frame_border(id: &str, params: &Value) -> bool {
match id {
"layout.update" => params.get("border").is_some_and(|b| !b.is_null()),
"layout.import" => imported_layout_uses(params, "\"border_", |e| e.kind.has_custom_border()),
"layout.import" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_frame_border)),
_ => false,
}
Expand All @@ -252,7 +250,7 @@ fn command_uses_blank_paper(id: &str, params: &Value) -> bool {
fn command_uses_element_background(id: &str, params: &Value) -> bool {
match id {
"layout.addElement" | "layout.update" => params.get("background").is_some_and(|b| !b.is_null()),
"layout.import" => imported_layout_uses(params, "\"background\"", |e| e.background.is_some()),
"layout.import" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_background)),
_ => false,
}
Expand All @@ -263,7 +261,7 @@ fn command_uses_element_background(id: &str, params: &Value) -> bool {
/// and `layout.update`, so such a command fails there instead of losing the ink.
fn command_uses_element_ink(id: &str, params: &Value) -> bool {
match id {
"layout.import" => imported_layout_uses(params, "\"color\"", |e| e.kind.ink().is_some()),
"layout.import" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_ink)),
_ => false,
}
Expand All @@ -280,7 +278,7 @@ fn wraps(e: &rustgis_map::layout::Element) -> bool {
fn command_uses_element_wrap(id: &str, params: &Value) -> bool {
match id {
"layout.addElement" | "layout.update" => params.get("wrap").and_then(crate::cmd::bool_value) == Some(true),
"layout.import" => imported_layout_uses(params, "\"wrap\"", wraps),
"layout.import" => imported_layout_uses(params, &["\"wrap\""], wraps),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_wrap)),
_ => false,
}
Expand Down Expand Up @@ -310,9 +308,7 @@ fn command_uses_chart_data(id: &str, params: &Value) -> bool {
|| params.get("bars").and_then(Value::as_u64).is_some_and(|n| n != CHART_BARS as u64)
|| params.get("other").and_then(crate::cmd::bool_value) == Some(true)
}
"layout.import" => ["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""]
.iter()
.any(|key| imported_layout_uses(params, key, charts_data)),
"layout.import" => imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_data)),
_ => false,
}
Expand Down Expand Up @@ -343,9 +339,11 @@ fn command_uses_chart_look(id: &str, params: &Value) -> bool {
&& (only_chart() || text("orientation").as_deref() == Some("vertical") || params.get("title").is_some_and(|t| !t.is_null()))
}
"layout.update" => only_chart(),
"layout.import" => ["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""]
.iter()
.any(|key| imported_layout_uses(params, key, charts_look)),
"layout.import" => imported_layout_uses(
params,
&["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""],
charts_look,
),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_look)),
_ => false,
}
Expand All @@ -366,7 +364,7 @@ fn command_uses_chart_classes(id: &str, params: &Value) -> bool {
params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && classes()
}
"layout.update" => classes(),
"layout.import" => imported_layout_uses(params, "\"classes\"", charts_classes),
"layout.import" => imported_layout_uses(params, &["\"classes\""], charts_classes),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_classes)),
_ => false,
}
Expand All @@ -386,22 +384,23 @@ fn command_uses_chart_stack(id: &str, params: &Value) -> bool {
params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && stack()
}
"layout.update" => stack(),
"layout.import" => imported_layout_uses(params, "\"stack\"", charts_stack),
"layout.import" => imported_layout_uses(params, &["\"stack\""], charts_stack),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_stack)),
_ => false,
}
}

/// Whether a `layout.import` carries its layout as `"text"` with an element `uses`: an older
/// reader reads it without the keys it doesn't know. One read from a `"path"` is a layout file,
/// which carries no format header yet (#2277). Every save asks, so a text without the feature's
/// `key` (a saved layout writes it only when set) isn't read through, unless it has a backslash:
/// JSON may spell the key with escapes (`"\u0063olor"`).
fn imported_layout_uses(params: &Value, key: &str, uses: fn(&rustgis_map::layout::Element) -> bool) -> bool {
/// which carries no format header yet (#2277). Every save asks, so a text with none of the
/// feature's `keys` (a saved layout writes them only when set) isn't read through, unless it has a
/// backslash: JSON may spell a key with escapes (`"\u0063olor"`). It is read once, whatever its
/// keys.
fn imported_layout_uses(params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool {
params
.get("text")
.and_then(Value::as_str)
.filter(|t| t.contains(key) || t.contains('\\'))
.filter(|t| keys.iter().any(|key| t.contains(key)) || t.contains('\\'))
.and_then(|t| serde_json::from_str::<rustgis_map::layout::Layout>(t).ok())
.is_some_and(|l| l.elements.iter().any(uses))
}
Expand Down
27 changes: 27 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12062,6 +12062,25 @@ fn publication_layouts_fit_their_pages() {
assert!(widths.iter().any(|w| (w - first).abs() > 0.5), "the box changed: {first} mm, then {widths:?}");
// Its bar's labels at 40 points need a wider box for the last one, and 8 gives the width back.
assert!(widths[2] > widths[1] + 0.5 && (widths[3] - widths[1]).abs() < 1e-9, "{widths:?}");
// A text naming the frame's centre and map follows them: a new centre or map, each measured
// from its own, leaves the box as wide as it is automatically then.
let scale_text = id_in(&s, "Scale");
s.run("map.new", &json!({"name": "The second map, longer"})).unwrap();
let mut followed = Vec::new();
for (text, change) in [
("{scale} {center}", json!({"element": frame, "center": [-120.5, -45.25]})),
("{scale} {map}", json!({"element": frame, "map": "The second map, longer"})),
] {
s.run("layout.update", &json!({"layout": li, "element": scale_text, "text": text})).unwrap();
let mut change = change;
change["layout"] = json!(li);
let before = width(&s);
s.run("layout.update", &change).unwrap();
let auto = crate::cmd::layout::scale_box_auto(&s, li).unwrap();
assert!((width(&s) - auto).abs() < 1e-9, "{change}: {} mm, {auto} mm automatic", width(&s));
followed.push(width(&s) - before);
}
assert!(followed.iter().all(|d| d.abs() > 0.1), "each changed the box: {followed:?}");
// A long title wraps within the frame, and pushes the map down; the result names every
// element for layout.update.
let r = s.run("layout.publication", &json!({"extent": piaui, "title": long})).unwrap();
Expand Down Expand Up @@ -14236,6 +14255,14 @@ fn scale_bars_and_grids_take_a_label_size() {
s.run(change.0, &change.1).unwrap();
assert!(said(&s, mark).contains("its box is too small"), "{change:?}: {}", said(&s, mark));
}
// Moved off the page's edge, its labels are told too; a locked bar takes no size, null too.
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": bar, "x": 250})).unwrap();
assert!(said(&s, mark).contains("runs off the page"), "{}", said(&s, mark));
s.run("layout.update", &json!({"element": bar, "x": 10, "locked": true})).unwrap();
assert!(s.run("layout.update", &json!({"element": bar, "textSize": null})).is_err());
assert_eq!(bar_size(&s, bar), 40.0, "the locked bar keeps its size");
s.run("layout.update", &json!({"element": bar, "locked": false})).unwrap();
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": bar, "textSize": null})).unwrap();
assert_eq!(said(&s, mark), "", "the default fits");
Expand Down
4 changes: 2 additions & 2 deletions crates/map/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,11 +1486,11 @@ impl ElementKind {

/// Whether `key` given as null changes an element of this kind, giving it back its default:
/// an ink's `color`, a chart's `title` (the title it is given) and `barColor`, and a legend's
/// `textSize` (#2581).
/// or scale bar's `textSize` (#2581).
pub fn null_resets(&self, key: &str) -> bool {
(self.takes_ink() && key == "color")
|| (matches!(self, ElementKind::Chart { .. }) && matches!(key, "title" | "barColor"))
|| (matches!(self, ElementKind::Legend { .. }) && key == "textSize")
|| (matches!(self, ElementKind::Legend { .. } | ElementKind::ScaleBar { .. }) && key == "textSize")
}

/// Its ink, when it [takes one](ElementKind::takes_ink) and has one; `None` draws in [`INK`].
Expand Down
Loading
Loading