diff --git a/crates/assistant/skills/print-layout/SKILL.md b/crates/assistant/skills/print-layout/SKILL.md index d8f1b39333..7bc304379b 100644 --- a/crates/assistant/skills/print-layout/SKILL.md +++ b/crates/assistant/skills/print-layout/SKILL.md @@ -8,7 +8,7 @@ description: Make a printable page layout — map frame, title, legend, north ar 1. Get the map right first: symbology, labels and extent. A layout's map frame shows the map's current view. 2. For a finished sheet or a figure for a paper in one step, use `layout.publication` with a `"profile"` (a page size alone, `"a4"`, `"letter"`, `"legal"`, `"tabloid"`, `"a5"` to `"a0"`, for a sheet turned to the area's shape, or with its orientation, `"a4-portrait"`, `"letter-landscape"`, … to choose; `"figure-1col"`, `"thesis-full"`, … for a figure), the `"extent"` - to fit, and the texts: `"title"`, `"subtitle"`, `"sources"`, `"note"`, a `"locator"` (`"globe"`: the visible polygon layers held in the project, not streamed ones, over the basemap; `"inset"` or `"none"`; when omitted, `"globe"` on a sheet and `"none"` on a figure), and `"decimal": ","` for a map in a language with a decimal comma. A sheet's `"style"` arranges it: `"classic"` (the default), `"banner"`, `"panel"`, `"editorial"`, `"modular"`, `"poster"` (for a poster-like sheet) or `"edge"` (the map from side to side of the page; it also takes `"paper"`, the page colour as `"#RRGGBB"`, `"kicker"`, a short line over the title, and `"figures"`, up to three `{"value", "label"}` under the map, whose texts may hold layer statistics such as `{count:Layer}`), with an `"accent"` colour as `"#RRGGBB"`. Its + to fit, and the texts: `"title"`, `"subtitle"`, `"sources"`, `"note"`, a `"locator"` (`"globe"`: the visible polygon layers held in the project, not streamed ones, over the basemap; `"inset"`; `["inset", "globe"]`, both, the globe outlining the inset; or `"none"`; when omitted, `"globe"` on a sheet and `"none"` on a figure), and `"decimal": ","` for a map in a language with a decimal comma. A sheet's `"style"` arranges it: `"classic"` (the default), `"banner"`, `"panel"`, `"editorial"`, `"modular"`, `"poster"` (for a poster-like sheet) or `"edge"` (the map from side to side of the page; it also takes `"paper"`, the page colour as `"#RRGGBB"`, `"kicker"`, a short line over the title, and `"figures"`, up to three `{"value", "label"}` under the map, whose texts may hold layer statistics such as `{count:Layer}`), with an `"accent"` colour as `"#RRGGBB"`. Its surrounds (locator, north arrow, legend, scale bar) start from an `"arrangement"` (`"classic"`, `"mirrored"`, `"legendRight"`, `"legendTop"`, or a corner for each; `"best"`, the default, tries the user's favourite, set with `layout.arrangement`, and the others), and one over the data moves to the freest place in the frame, the largest first (the legend, as a rule); read its `"arrangement"`, `"moved"` and `"warnings"`. It places the frame at a round scale, the graticule, legend (a long one in up to three columns: its own choice, though a legend takes up to six), scale bar and north arrow, and wraps the footer. `"grid"`, `"scaleBar"` and `"northArrow"` take `"none"` to leave that part out, or an object of its options (the grid's `"step"` or `"type"`, the bar's `"units"` or `"style"`, the arrow's `"style"`, `"fill"`, `"outline"` and `"show"`). Its result lists every element's id and name (`"elements"`), so skip to step 5 to adjust them. Otherwise, diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index ce11f715d0..cbcb4abcc0 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -78,7 +78,7 @@ pub fn commands() -> Vec { r#""note"?: text, "#, r#""author"?: text (default: "Made with RustGIS · {date}"; "" or [] for none), "#, r#""headings"?: [the sources' heading, the reference's heading, an editorial column's heading], "#, - r#""locator"?: "globe" (a globe with the area outlined; the default on sheets, but for a map on the globe with no extent, the globe seen whole, which by default has no locator (one asked for by name is drawn) and no scale bar, the globe filling its frame) | "inset" (the surrounding region, the area outlined) | "none" (the default on figures), "#, + r#""locator"?: "globe" (a globe with the area outlined; the default on sheets, but for a map on the globe with no extent, the globe seen whole, which by default has no locator (one asked for by name is drawn) and no scale bar, the globe filling its frame) | "inset" (the surrounding region, the area outlined) | ["inset", "globe"] (both side by side, the globe outlining the inset) | "none" (the default on figures), "#, r#""decimal"?: "." (the default) | "," (graticule, scale bar and colorbar numbers and {scale} with a decimal comma: 0,05 and 1:6.300.000), "#, r#""colorbar"?: "auto" (the default: the top visible raster with a colour scale, not drawn by classes, gets one, under the legend in its box; a raster drawn by classes has them in the legend's rows instead) | "none" (no colorbar, and the legend's box has no room for one), "#, r#""veil"?: true (the default: when no shape of the legend fits, a map that runs on past the extent, with a long legend, has what is outside the extent veiled and its surrounds grouped there, at up to half the scale on a sheet) | false (never veiled, and drawn at its scale; the map may still move aside), "#, diff --git a/crates/engine/src/cmd/layout/frame.rs b/crates/engine/src/cmd/layout/frame.rs index 7309d321ce..7df4a0b4e1 100644 --- a/crates/engine/src/cmd/layout/frame.rs +++ b/crates/engine/src/cmd/layout/frame.rs @@ -202,7 +202,7 @@ pub(in crate::cmd::layout) fn scale_text_mm(text: &str, text_pt: f64, bold: bool } /// What a publication places over its map, which a scale bar box measured again must stay off. -pub(in crate::cmd::layout) const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"]; +pub(in crate::cmd::layout) const PLACED_OVER_MAP: [&str; 5] = ["Legend Box", "Locator", "Locator Inset", "Locator Globe", "North Arrow"]; /// What a publication's scale bar box is measured from: its frame (id, map, centre, scale, width), /// its bar (id, and the bar as it is set: every setting changes its labels or their room) and the diff --git a/crates/engine/src/cmd/layout/inputs.rs b/crates/engine/src/cmd/layout/inputs.rs index 2305481ddc..afd218638c 100644 --- a/crates/engine/src/cmd/layout/inputs.rs +++ b/crates/engine/src/cmd/layout/inputs.rs @@ -168,11 +168,11 @@ pub(in crate::cmd::layout) fn locator_orphan(input: &impl crate::cmd::InputSourc pub(in crate::cmd::layout) fn publication_inputs(input: &crate::cmd::InputContext<'_>, p: &Value) -> Result { let s = input; let (_, profile) = publication_profile(p)?; - let locator = publication_locator(p, profile.sheet, shows_whole_globe(s.map(), p))?; + let locators = publication_locators(p, profile.sheet, shows_whole_globe(s.map(), p))?; let map = s.map(); let mut inputs = crate::cmd::Inputs::layers(map.layers.iter().filter(|l| map.shows(l)).map(|l| (map, l))); inputs.maps.push(map.instance_id()); - if matches!(locator, Locator::Globe) { + if locators.contains(&Locator::Globe) { if let Some(old) = locator_orphan(input).and_then(|i| s.project.maps.get(i)) { inputs.maps.push(old.instance_id()); inputs.layers.extend(old.layers.iter().map(|l| (old.instance_id(), l.id))); diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 33a121ab95..c03d6e0e41 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -27,12 +27,11 @@ pub(in crate::cmd::layout) fn decimal_mark(p: &Value) -> Result, St } } -/// What a publication layout locates its area on. +/// What a publication layout locates its area on, in the order they nest. #[derive(Clone, Copy, PartialEq)] pub(in crate::cmd::layout) enum Locator { - Globe, Inset, - None, + Globe, } /// The scale at which `map` on the globe, centred on `center`, draws a globe `d_mm` across in a @@ -121,7 +120,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu }; let headings = texts_of(p, "headings")?.unwrap_or_default(); let comma = decimal_mark(p)?.unwrap_or(false); - let locator = publication_locator(p, sheet, whole_globe)?; + let locators = publication_locators(p, sheet, whole_globe)?; let colorbar = publication_colorbar(p)?; let may_veil = publication_veil(p)?; let context_in_legend = publication_flag(p, "contextInLegend")?.unwrap_or(false); @@ -972,17 +971,20 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // The locator: a globe (a copy of the map's polygon layers on the globe, its diameter nearly // the frame's) or the surrounding region on the map itself, the area outlined on either by // the map frame's extent indicator. + // Two locators sit side by side, each `side` square. + let (locator_count, locator_gap) = (locators.len().max(1) as f64, 3.0 * k); let side = match column { - // In a column, its width up to an A4 portrait column's (a landscape sheet's wider - // column doesn't make it larger), leaving the legend most of the height under it. - Some(c) => c.w.min(52.0 * k).min(0.45 * c.h).max(0.0), + // In a column, its width (shared by two) up to an A4 portrait column's (a landscape + // sheet's wider column doesn't make it larger), leaving the legend most of the height + // under it. + Some(c) => ((c.w - (locator_count - 1.0) * locator_gap) / locator_count).min(52.0 * k).min(0.45 * c.h).max(0.0), None if sheet => (0.24 * fw.min(fh)).clamp(25.0 * k, 70.0 * k), None => (0.3 * fw.min(fh)).clamp(18.0, 45.0), }; let mut locator_cut = None; - let locator_map = match locator { - Locator::None => None, - Locator::Globe => { + let globe_locator = match locators.contains(&Locator::Globe) { + false => None, + true => { let src = s.map(); let base = format!("{} Locator", src.name); // A locator an earlier sheet made that no layout shows any more (its layout deleted) @@ -1115,8 +1117,16 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu } Some((name, globe, FrameShape::Circle, at)) } - Locator::Inset => Some((s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center)), }; + // The inset first: it outlines the area, and the globe after it outlines the inset. + let inset_locator = locators + .contains(&Locator::Inset) + .then(|| (s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center)); + let locator_maps: Vec<_> = [(Locator::Inset, inset_locator), (Locator::Globe, globe_locator)] + .into_iter() + .filter_map(|(kind, frame)| frame.map(|frame| (kind, frame))) + .collect(); + let locators_w = locator_count * side + (locator_count - 1.0) * locator_gap; // The legend lists what the frame draws: its map synced to the frame's view first, as an // export does, so a streamed layer has its features there. (This changes the session's map, // so it comes after the last refusal: nothing below fails.) While one is still reading, the @@ -1226,8 +1236,8 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let pad = 2.0; // The surrounds to place: what each is and its size (the arrangement gives its corner). let mut surrounds: Vec<(Surround, f64, f64)> = Vec::new(); - if locator_map.is_some() && column.is_none() { - surrounds.push((Surround::Locator, side, side)); + if !locator_maps.is_empty() && column.is_none() { + surrounds.push((Surround::Locator, locators_w, side)); } // At a continental span the graticule's lines give north, and one arrow can't point north // across the whole frame: none, unless "northArrow": "always" (#1379). Left out so, it is said; @@ -1360,14 +1370,14 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu bands }; let size_of = |kind: Surround| surrounds.iter().find(|x| x.0 == kind).map(|x| (x.1, x.2)); - let (locator_side, arrow_size) = (size_of(Surround::Locator).map(|x| x.0), size_of(Surround::Arrow)); + let (locator_size, arrow_size) = (size_of(Surround::Locator), size_of(Surround::Arrow)); // The fewest columns from `from` up in which the surrounds group in a band around `clip`, with // their places; the scale bar's box `scale_bar` is the one at the scale tried. let grouped = |clip: Rect, from: usize, known: &[(f64, f64); PUBLICATION_LEGEND_COLUMNS], scale_bar: (f64, f64)| { (from..known.len()).find_map(|i| { bands_around(clip) .iter() - .find_map(|band| veil_panel(*band, whole(known[i]), locator_side, arrow_size, scale_bar, GROUP_GAP)) + .find_map(|band| veil_panel(*band, whole(known[i]), locator_size, arrow_size, scale_bar, GROUP_GAP)) .map(|places| (i, places)) }) }; @@ -1852,11 +1862,11 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let spot_of = |what: Surround| spots.iter().find(|(kind, _)| *kind == what).map(|(_, r)| *r); // In a column, at its top, centred across it; otherwise in its corner over the map. let locator_rect = match column { - Some(c) => Some((c.x + (c.w - side) / 2.0, c.y, side, side)), + Some(c) => Some((c.x + (c.w - locators_w) / 2.0, c.y, locators_w, side)), None => spot_of(Surround::Locator).inspect(|&r| placed.push(("locator", r))), }; let surround_box = |fill: Rgba| if style.cards() { pub_card(fill, clear, 0.0, 2.0 * k) } else { pub_box(fill, clear, 0.0) }; - if let (Some((map, locator_scale, shape, locator_center)), Some(r)) = (&locator_map, locator_rect) { + if let Some(r) = locator_rect.filter(|_| !locator_maps.is_empty()) { if let (true, Some(c)) = (card, column) { l.add( "Locator Card", @@ -1864,25 +1874,38 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu pub_card(Rgba::rgb(0xFF, 0xFF, 0xFF), Rgba::new(0x2B, 0x26, 0x20, 60), 0.4, card_r), ); } - l.add( - "Locator", - r, - ElementKind::MapFrame { - map: map.clone(), - center: *locator_center, - scale: *locator_scale, - shape: shape.clone(), - grid: None, - sections: Vec::new(), - extent_of: Some(frame), - extent_color: rustgis_map::layout::INDICATOR_COLOR, - extent_width: rustgis_map::layout::INDICATOR_WIDTH, - border_color: rustgis_map::layout::FRAME_BORDER_COLOR, - border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, - bearing: 0.0, - pitch: 0.0, - }, - ); + // Inset first from the left. A pair over the map's right half is mirrored, so the inset + // is the one against the frame's edge there too. + let from_right = column.is_none() && r.0 + r.2 / 2.0 > fx + fw / 2.0; + // What the next locator outlines: the map frame, then the locator before it. + let mut target = frame; + for (i, (kind, (map, locator_scale, shape, locator_center))) in locator_maps.iter().enumerate() { + let slot = if from_right { locator_maps.len() - 1 - i } else { i }; + let name = match kind { + _ if locator_maps.len() == 1 => "Locator", + Locator::Inset => "Locator Inset", + Locator::Globe => "Locator Globe", + }; + target = l.add( + name, + (r.0 + slot as f64 * (side + locator_gap), r.1, side, r.3), + ElementKind::MapFrame { + map: map.clone(), + center: *locator_center, + scale: *locator_scale, + shape: shape.clone(), + grid: None, + sections: Vec::new(), + extent_of: Some(target), + extent_color: rustgis_map::layout::INDICATOR_COLOR, + extent_width: rustgis_map::layout::INDICATOR_WIDTH, + border_color: rustgis_map::layout::FRAME_BORDER_COLOR, + border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, + bearing: 0.0, + pitch: 0.0, + }, + ); + } } if let Some(r) = spot_of(Surround::Arrow) { placed.push(("north arrow", r)); @@ -1894,7 +1917,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // In a panel, the legend under the locator, as tall as it needs within what is left. let legend_rect = match column { Some(c) if content_h > 0.0 => { - let ly = c.y + if locator_map.is_some() { side + 3.0 * k + if card { 2.0 * card_pad } else { 0.0 } } else { 0.0 }; + let ly = c.y + if !locator_maps.is_empty() { side + 3.0 * k + if card { 2.0 * card_pad } else { 0.0 } } else { 0.0 }; let room_h = c.y + c.h - ly; if room_h < MIN_LEGEND_MM { warnings.push("the side column leaves the legend no room under the locator, so it is left out: shorten its texts".into()); @@ -2084,7 +2107,9 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu if let Some(crs) = p.get("grid").and_then(|g| g.get("crs")).and_then(Value::as_str) { crate::cmd::say_crs_note(s, crs); } - let locator = locator_map.map(|(map, ..)| map); + // One locator's map by name; two, both names, the inset's first. + let mut named_maps: Vec = locator_maps.into_iter().map(|(_, (map, ..))| map).collect(); + let locator = if named_maps.len() > 1 { json!(named_maps) } else { json!(named_maps.pop()) }; Ok( json!({"layout": i, "name": name, "profile": profile.id, "page": [w, h], "scale": scale, "grid": step, "locator": locator, "style": style.code(), "arrangement": arrangement_used, "legendColumns": legend.as_ref().map(|_| legend_cols), "mapShift": [map_shift.0, map_shift.1], "veiled": veiled.is_some(), "legendBox": legend_rect.is_some() && !legend_bare, "elements": elements, "moved": moved, "coverage": coverage_written, "warnings": warnings}), ) @@ -2252,23 +2277,31 @@ pub(crate) fn globe_locator_center(map: &Map, area: Coord, scale: f64, side_mm: if globe(inside).forward(area).is_some() { (inside, true) } else { (area, false) } } -/// Parses the publication's locator choice with its default: a globe on a sheet, none on a figure -/// or where the map is itself the globe seen whole (it would only show it again, smaller). -pub(in crate::cmd::layout) fn publication_locator(p: &Value, sheet: bool, whole_globe: bool) -> Result { +/// Parses the publication's locator choice, a name or a list of "inset" and "globe", with its +/// default: a globe on a sheet, none on a figure or where the map is itself the globe seen whole (it +/// would only show it again, smaller). A list is a set: whatever its order, the inset comes first. +pub(in crate::cmd::layout) fn publication_locators(p: &Value, sheet: bool, whole_globe: bool) -> Result, String> { + let one = |v: &Value| match v.as_str().map(str::trim) { + Some("globe") => Ok(Some(Locator::Globe)), + Some("inset") => Ok(Some(Locator::Inset)), + Some("none") => Ok(None), + _ => Err("locator is \"globe\", \"inset\", \"none\" or a list of \"inset\" and \"globe\"".to_string()), + }; Ok(match p.get("locator").filter(|v| !v.is_null()) { - None => { - if sheet && !whole_globe { - Locator::Globe - } else { - Locator::None + None => (sheet && !whole_globe).then_some(Locator::Globe).into_iter().collect(), + Some(Value::Array(list)) => { + let mut chosen = Vec::new(); + for v in list { + let locator = one(v)?.ok_or("locator: \"none\" goes alone, not in a list")?; + if chosen.contains(&locator) { + return Err(format!("locator: {v} is given twice")); + } + chosen.push(locator); } + chosen.sort_by_key(|&l| l == Locator::Globe); + chosen } - Some(v) => match v.as_str().map(str::trim) { - Some("globe") => Locator::Globe, - Some("inset") => Locator::Inset, - Some("none") => Locator::None, - _ => return Err("locator is \"globe\", \"inset\" or \"none\"".into()), - }, + Some(v) => one(v)?.into_iter().collect(), }) } diff --git a/crates/engine/src/cmd/layout/publication/placement.rs b/crates/engine/src/cmd/layout/publication/placement.rs index 9992999438..4b8c4c90d2 100644 --- a/crates/engine/src/cmd/layout/publication/placement.rs +++ b/crates/engine/src/cmd/layout/publication/placement.rs @@ -104,7 +104,7 @@ pub(crate) fn crop_cells( pub(crate) fn veil_panel( band: Rect, legend: (f64, f64), - locator: Option, + locator: Option<(f64, f64)>, arrow: Option<(f64, f64)>, scale: (f64, f64), gap: f64, @@ -113,7 +113,8 @@ pub(crate) fn veil_panel( let (aw, ah) = arrow.unwrap_or((0.0, 0.0)); // The locator and the arrow side by side, with a gap between them only when both are there. let between = if locator.is_some() && arrow.is_some() { gap } else { 0.0 }; - let top = (locator.map_or(0.0, |side| side + between) + aw, locator.unwrap_or(0.0).max(ah)); + let (lw, lh) = locator.unwrap_or((0.0, 0.0)); + let top = (lw + between + aw, lh.max(ah)); // The gap over the scale bar only when there is one and something sits above it. let lead = if top.1 > 0.0 && scale.1 > 0.0 { gap } else { 0.0 }; let group = (top.0.max(scale.0), top.1 + lead + scale.1); @@ -133,7 +134,7 @@ pub(crate) fn veil_panel( }; Some(( (legend_at.0, legend_at.1, legend.0, legend.1), - locator.map(|side| (group_at.0, group_at.1, side, side)), + locator.map(|(lw, lh)| (group_at.0, group_at.1, lw, lh)), arrow.map(|(aw, ah)| (group_at.0 + group.0 - aw, group_at.1, aw, ah)), (group_at.0, group_at.1 + top.1 + lead, scale.0, scale.1), )) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 228a95f994..f235ae5ff4 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -141,6 +141,9 @@ mod layout_legend_class_labels; #[path = "tests/layout_report_text.rs"] mod layout_report_text; +#[path = "tests/layout_publication_locators.rs"] +mod layout_publication_locators; + #[path = "tests/sde_connections.rs"] mod sde_connections; @@ -16324,21 +16327,21 @@ fn a_publication_reads_streamed_layers_for_the_move_searchs_wide_drawing() { fn a_veil_panel_groups_the_surrounds_by_the_bands_shape() { use crate::cmd::layout::veil_panel; let (legend, arrow, scale) = ((80.0, 50.0), Some((8.0, 12.0)), (60.0, 10.0)); - let (l, locator, a, b) = veil_panel((0.0, 0.0, 160.0, 70.0), legend, Some(40.0), arrow, scale, 3.0).unwrap(); + let (l, locator, a, b) = veil_panel((0.0, 0.0, 160.0, 70.0), legend, Some((40.0, 40.0)), arrow, scale, 3.0).unwrap(); let a = a.unwrap(); assert!(l.0 + l.2 <= locator.unwrap().0, "beside it: {l:?} {locator:?}"); assert!(a.0 + a.2 <= 160.0 && b.1 >= locator.unwrap().1 + 40.0, "{a:?} {b:?}"); let apart = |p: (f64, f64, f64, f64), q: (f64, f64, f64, f64)| p.0 + p.2 <= q.0 || q.0 + q.2 <= p.0 || p.1 + p.3 <= q.1 || q.1 + q.3 <= p.1; assert!(apart(a, l) && apart(a, (locator.unwrap().0, locator.unwrap().1, 40.0, 40.0)), "the arrow clear: {a:?} {l:?} {locator:?}"); - let (l, locator, _, b) = veil_panel((0.0, 0.0, 90.0, 130.0), legend, Some(40.0), arrow, scale, 3.0).unwrap(); + let (l, locator, _, b) = veil_panel((0.0, 0.0, 90.0, 130.0), legend, Some((40.0, 40.0)), arrow, scale, 3.0).unwrap(); assert!(l.1 >= b.1 + b.3 && locator.unwrap().0 == b.0, "under it: {l:?} {b:?}"); - assert!(veil_panel((0.0, 0.0, 60.0, 60.0), legend, Some(40.0), arrow, scale, 3.0).is_none()); + assert!(veil_panel((0.0, 0.0, 60.0, 60.0), legend, Some((40.0, 40.0)), arrow, scale, 3.0).is_none()); // With nothing to group beside it, a legend the size of the band fits it: no gap is kept. assert!(veil_panel((0.0, 0.0, legend.0, legend.1), legend, None, None, (0.0, 0.0), 3.0).is_some()); assert!(veil_panel((0.0, 0.0, 160.0, 70.0), legend, None, arrow, scale, 3.0).unwrap().1.is_none()); // With no north arrow (a continental extent, #1379), the locator stands alone over the scale bar. - let (_, locator, a, b) = veil_panel((0.0, 0.0, 160.0, 70.0), legend, Some(40.0), None, scale, 3.0).unwrap(); + let (_, locator, a, b) = veil_panel((0.0, 0.0, 160.0, 70.0), legend, Some((40.0, 40.0)), None, scale, 3.0).unwrap(); assert!(a.is_none(), "no place for an arrow there isn't: {a:?}"); assert_eq!(locator.unwrap().0, b.0, "the locator over the scale bar: {locator:?} {b:?}"); // With neither, the scale bar heads its group, with no gap over it: a band just as tall as the diff --git a/crates/engine/src/tests/layout_publication_locators.rs b/crates/engine/src/tests/layout_publication_locators.rs new file mode 100644 index 0000000000..4a264f1497 --- /dev/null +++ b/crates/engine/src/tests/layout_publication_locators.rs @@ -0,0 +1,169 @@ +//! A publication's inset and globe locators together (#3240): the inset outlines the map frame and +//! the globe outlines the inset. + +use super::*; +use rustgis_map::layout::{Element, ElementKind, FrameShape}; + +fn publish(s: &mut Session, parts: Value) -> Result { + let mut p = json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]}); + p.as_object_mut().unwrap().extend(parts.as_object().unwrap().clone()); + s.run("layout.publication", &p) +} + +fn sheet(parts: Value) -> (Session, Value) { + let mut s = world(); + let r = publish(&mut s, parts).unwrap(); + (s, r) +} + +/// Open sea: with nothing to cover, each surround stays in its home corner. +const SEA: [f64; 4] = [-30.0, -30.0, -20.0, -22.0]; + +fn find<'a>(s: &'a Session, name: &str) -> &'a Element { + let elements = &s.project.layouts.last().unwrap().elements; + elements.iter().find(|e| e.name == name).unwrap_or_else(|| panic!("no {name}")) +} + +fn has(s: &Session, name: &str) -> bool { + s.project.layouts.last().unwrap().elements.iter().any(|e| e.name == name) +} + +/// A frame element's map, shape and the frame it outlines. +fn frame_of(e: &Element) -> (&str, &FrameShape, Option) { + match &e.kind { + ElementKind::MapFrame { map, shape, extent_of, .. } => (map, shape, *extent_of), + k => panic!("{k:?}"), + } +} + +fn apart(a: &Element, b: &Element) -> bool { + a.x + a.w <= b.x || b.x + b.w <= a.x || a.y + a.h <= b.y || b.y + b.h <= a.y +} + +fn elements(s: &Session) -> Value { + serde_json::to_value(&s.project.layouts.last().unwrap().elements).unwrap() +} + +#[test] +fn an_inset_and_a_globe_nest_the_globe_outlining_the_inset() { + let (s, r) = sheet(json!({"extent": SEA, "locator": ["inset", "globe"]})); + let main = s.map().name.clone(); + let globe_map = format!("{main} Locator"); + assert_eq!(r["locator"], json!([main, globe_map]), "{r}"); + let (map, inset, globe) = (find(&s, "Map Frame"), find(&s, "Locator Inset"), find(&s, "Locator Globe")); + assert_eq!(frame_of(inset), (main.as_str(), &FrameShape::Rectangle, Some(map.id))); + assert_eq!(frame_of(globe), (globe_map.as_str(), &FrameShape::Circle, Some(inset.id))); + assert!(!has(&s, "Locator")); + // Side by side in the locator's corner, the inset against the frame's edge. + assert!(apart(inset, globe) && (inset.y - globe.y).abs() < 1e-6 && (inset.w - globe.w).abs() < 1e-6); + assert!(inset.x < globe.x && inset.x > map.x && inset.x - map.x < inset.w, "{inset:?} {globe:?}"); + assert!(apart(find(&s, "Legend Box"), inset) && apart(find(&s, "Legend Box"), globe)); +} + +#[test] +fn the_order_of_two_locators_changes_nothing() { + let (a, ra) = sheet(json!({"locator": ["inset", "globe"]})); + let (b, rb) = sheet(json!({"locator": ["globe", "inset"]})); + assert_eq!((elements(&a), &ra["locator"]), (elements(&b), &rb["locator"])); +} + +#[test] +fn a_list_of_one_locator_is_its_name() { + for one in ["globe", "inset"] { + let (a, ra) = sheet(json!({"locator": [one]})); + let (b, rb) = sheet(json!({"locator": one})); + assert_eq!((elements(&a), &ra["locator"]), (elements(&b), &rb["locator"]), "{one}"); + assert!(has(&a, "Locator") && ra["locator"].is_string(), "{one}: {ra}"); + } + let (a, ra) = sheet(json!({"locator": []})); + let (b, _) = sheet(json!({"locator": "none"})); + assert_eq!(elements(&a), elements(&b)); + assert!(ra["locator"].is_null() && !has(&a, "Locator"), "{ra}"); +} + +#[test] +fn a_locator_list_with_a_wrong_entry_is_refused_and_adds_nothing() { + let mut s = world(); + let before = (s.project.maps.len(), s.project.layouts.len()); + for (list, why) in [ + (json!(["globe", "globe"]), "\"globe\" is given twice"), + (json!(["inset", "globe", "inset"]), "\"inset\" is given twice"), + (json!(["inset", "none"]), "\"none\" goes alone"), + (json!(["none"]), "\"none\" goes alone"), + (json!(["mosaic"]), "locator is"), + (json!([3]), "locator is"), + ] { + let e = publish(&mut s, json!({"locator": list})).unwrap_err(); + assert!(e.contains(why), "{list}: {e}"); + } + assert_eq!((s.project.maps.len(), s.project.layouts.len()), before); +} + +#[test] +fn a_figure_and_a_whole_globe_draw_both_locators_asked_for() { + let (s, _) = sheet(json!({"profile": "figure-2col", "locator": ["inset", "globe"]})); + assert!(has(&s, "Locator Inset") && has(&s, "Locator Globe")); + let mut s = world(); + s.map_mut().set_projection(rustgis_map::Projection::Globe); + s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": ["inset", "globe"]})).unwrap(); + assert!(has(&s, "Locator Inset") && has(&s, "Locator Globe")); +} + +#[test] +fn a_column_style_holds_both_locators_at_its_top() { + for style in ["panel", "modular"] { + let (s, _) = sheet(json!({"profile": "a4-landscape", "style": style, "locator": ["inset", "globe"]})); + let (map, inset, globe) = (find(&s, "Map Frame"), find(&s, "Locator Inset"), find(&s, "Locator Globe")); + assert!(inset.x >= map.x + map.w && globe.x >= map.x + map.w, "{style}: in the column"); + assert!(apart(inset, globe) && (inset.y - globe.y).abs() < 1e-6 && inset.x < globe.x, "{style}"); + let legend = find(&s, "Legend Box"); + assert!(legend.y >= inset.y + inset.h, "{style}: the legend under them"); + } +} + +#[test] +fn both_locators_take_the_arranged_corner() { + let corners = json!({"locator": "bottom-right", "northArrow": "top-left", "legend": "top-right", "scaleBar": "bottom-left"}); + let (s, _) = sheet(json!({"extent": SEA, "locator": ["inset", "globe"], "arrangement": corners})); + let (map, inset, globe) = (find(&s, "Map Frame"), find(&s, "Locator Inset"), find(&s, "Locator Globe")); + let my = map.y + map.h / 2.0; + assert!(inset.y > my && globe.y > my && map.x + map.w - (inset.x + inset.w) < inset.w, "{inset:?} {globe:?}"); + assert!(inset.x > globe.x, "the inset stays against the frame's edge"); +} + +#[test] +fn two_locators_reuse_an_orphan_globe_and_undo_together() { + let mut s = world(); + let maps = s.project.maps.len(); + let first = publish(&mut s, json!({"locator": ["inset", "globe"], "name": "Again"})).unwrap()["locator"][1].clone(); + s.run("layout.remove", &json!({"name": "Again"})).unwrap(); + let r = publish(&mut s, json!({"locator": ["inset", "globe"], "name": "Again"})).unwrap(); + assert_eq!((s.project.maps.len(), &r["locator"][1]), (maps + 1, &first), "{r}"); + let layouts = s.project.layouts.len(); + publish(&mut s, json!({"locator": ["inset", "globe"]})).unwrap(); + assert_eq!(s.project.maps.len(), maps + 2); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!((s.project.maps.len(), s.project.layouts.len()), (maps + 1, layouts)); +} + +#[test] +fn a_scale_box_measured_again_stays_off_either_locator() { + for name in ["Locator Inset", "Locator Globe"] { + let (mut s, _) = sheet(json!({"locator": ["inset", "globe"]})); + let id = |s: &Session, n: &str| find(s, n).id; + let (frame, text, boxed, locator) = (id(&s, "Map Frame"), id(&s, "Scale"), id(&s, "Scale Box"), id(&s, name)); + s.run("layout.update", &json!({"element": text, "text": "{scale}"})).unwrap(); + s.run("layout.update", &json!({"element": frame, "scale": 1_500_000.0})).unwrap(); + let (narrow, map) = (find(&s, "Scale Box").clone(), find(&s, "Map Frame").clone()); + // Beside the box on the side it grows to: the left one when it keeps its right edge. + let x = if narrow.x + narrow.w / 2.0 > map.x + map.w / 2.0 { narrow.x - 5.5 } else { narrow.x + narrow.w + 0.5 }; + s.run("layout.update", &json!({"element": locator, "x": x, "y": narrow.y, "w": 5.0, "h": 5.0})).unwrap(); + let mark = s.notice_mark(); + s.run("layout.update", &json!({"element": text, "text": "Escala aproximada da folha {scale}"})).unwrap(); + s.run("layout.update", &json!({"element": frame, "scale": 30_000_000.0})).unwrap(); + let after = find(&s, "Scale Box"); + assert_eq!((after.id, after.x, after.w), (boxed, narrow.x, narrow.w), "{name}: the box stays"); + let said = format!("would now cover the {}", name.to_lowercase()); + assert!(s.notices_since(mark).iter().any(|n| n.text.contains(&said)), "{name}: said"); + } +} diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index dd0de935ab..e97a6a1f47 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -216,8 +216,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu It uses the map's basemap, not its web tiles, in a round frame, the globe nearly filling it and the area outlined in red; `"inset"` shows the surrounding region (about four times the area across) on the map itself, outlined the same way; `"none"` (a figure's - default) leaves it out. The legend stops short of it; a frame with no room for a legend beside - it leaves the legend out, with a warning. Undo takes the globe's map away with the layout; a + default) leaves it out. `["inset", "globe"]` draws both side by side in the locator's corner + (or at the top of a style's column, sharing its width): the inset outlines the area, and the + globe outlines the inset. Their order in the list changes nothing, and the answer's `locator` + names both maps, the inset's first. The legend stops short of the locator; a frame with no room + for a legend beside it leaves the legend out, with a warning. Undo takes the globe's map away with the layout; a globe map no layout shows any more (its layout deleted) is replaced by the next sheet's rather than left beside it, which a warning says. - **The style** (`"style"`, on any sheet, portrait or landscape): `"classic"` (the default) sets