From 62011404767fe413717025013664f3a385d39c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 08:24:08 -0500 Subject: [PATCH 1/8] feat(layout): number a map frame's extent indicators (#3394) --- crates/engine/src/cmd/layout.rs | 2 + .../engine/src/cmd/layout/publication/mod.rs | 2 + crates/engine/src/cmd/layout/update.rs | 16 +- crates/engine/src/cmd/presentation.rs | 1 + crates/engine/src/project.rs | 1 + crates/engine/src/project_capabilities.rs | 20 ++ .../src/tests/archive_render_revision.rs | 1 + crates/engine/src/tests/layout_extents.rs | 85 ++++++++ crates/engine/src/tests/layout_files.rs | 1 + crates/map/src/layout/elements.rs | 9 + crates/map/src/layout/tests.rs | 3 + crates/render/src/layout.rs | 28 ++- crates/render/src/layout/extent_tests.rs | 191 ++++++++++++++++++ crates/render/src/layout/frame.rs | 16 ++ .../render/src/layout/legend/count_tests.rs | 1 + crates/render/src/layout/profile_tests.rs | 1 + crates/render/src/layout/tests.rs | 19 ++ crates/render/src/stream_tests.rs | 2 + crates/render/src/tiles.rs | 1 + crates/ui-egui/src/layoutview/format/frame.rs | 4 + crates/ui-egui/src/layoutview/tests.rs | 38 ++++ docs/maintaining.md | 7 + docs/user-guide/layouts.md | 5 +- schema/fixtures/layout_extent_numbers.rgis | 26 +++ schema/project.schema.json | 2 + 25 files changed, 478 insertions(+), 4 deletions(-) create mode 100644 crates/render/src/layout/extent_tests.rs create mode 100644 schema/fixtures/layout_extent_numbers.rgis diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index fcf3484f75..ea732dd62a 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -369,6 +369,7 @@ pub fn commands() -> Vec { r##""pitch"?: degrees of tilt (0 to 85), "##, r##""grid"?: {grid options as in layout.addElement} | false (map frame; a grid's "labelSize" goes in it, and is refused outside it unless null), "##, r##""extentIndicator"?: {"of"?: frame id, "color"?, "width"?: points, "remove"?: bool} | false (map frame: restyles one of its extent indicators, or removes it with "remove": true; "of" is the id of the frame that indicator outlines and may be omitted when there is only one; false removes them all), "##, + r##""extentNumbers"?: bool (map frame: numbers its extent indicators 1, 2, 3… in their order, each in its outline's colour at the outline's centre, none where that centre is off the frame; refused for other elements), "##, r##""border"?: {"color"?: "#RRGGBB"|"#RRGGBBAA", "width"?: points, 0 to 20, 0 for none} | null (map frame: the outline along its shape, default #303030 and 0.8, the keys not given keeping theirs; null gives the default back; refused for other elements), "##, r##""section"?: {"name": the section line it changes, "newName"?, "direction"?, "at"?, "color"?, "width"?, "dash"? ([] or null for solid), as in layout.addElement, the keys not given keeping theirs, "remove"?: true to remove it} (map frame; refused for other elements), "##, r##""layer"?: (colorbar: "" for the top visible raster with a colour scale, not drawn by classes), "##, @@ -602,6 +603,7 @@ fn frame_kind(s: &Session, map: &str, w: f64, shape: FrameShape) -> ElementKind extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing, diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 6a470ea0f2..e93fb2e73c 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1097,6 +1097,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, // A map that runs to the page's edges has no border. border_width: if edge { 0.0 } else { rustgis_map::layout::FRAME_BORDER_WIDTH }, @@ -1964,6 +1965,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index b875c84b28..14beeb99af 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -269,7 +269,7 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { // `true` adds the default one, an object changes the options it names, and null (not set) // leaves it as it is. let held; - let (restyled, new_grid, new_indicator, new_border, new_sections) = { + let (restyled, new_grid, new_indicator, new_numbers, new_border, new_sections) = { let l = s.project.layouts.get(li).ok_or("no layout")?; let kind = &l.element(id).ok_or_else(|| format!("no element {id}"))?.kind; // `"extentIndicator": false` takes the frame's indicators away; an object restyles or @@ -281,6 +281,13 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { (_, Some(_)) => return Err("only map frames have an extent indicator".into()), _ => None, }; + // Whether the frame numbers its extent indicators (#3394). + let numbers = match (kind, p.get("extentNumbers").filter(|v| !v.is_null())) { + (ElementKind::MapFrame { .. }, Some(Value::Bool(on))) => Some(*on), + (ElementKind::MapFrame { .. }, Some(_)) => return Err("extentNumbers is true or false".into()), + (_, Some(_)) => return Err("only map frames number their extent indicators".into()), + _ => None, + }; // `"border": null` gives the frame the default border back; an object changes the colour // and width it names, a width of 0 drawing none (#2557). let border = match (kind, p.get("border")) { @@ -326,7 +333,7 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { said.extend(held_width(v, "border width", (0.0, rustgis_map::layout::MAX_FRAME_BORDER_WIDTH))); } held = said; - (restyle(kind, p)?, grid, indicator, border, sections) + (restyle(kind, p)?, grid, indicator, numbers, border, sections) }; // A line turned to an "angle" or made a "length" long, about its centre: its new box, // direction and heads. @@ -628,6 +635,11 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some(indicators) = new_indicator { e.kind.set_extent_indicators(indicators); } + if let Some(on) = new_numbers + && let ElementKind::MapFrame { extent_numbers, .. } = &mut e.kind + { + *extent_numbers = on; + } match &mut e.kind { ElementKind::Text { text, diff --git a/crates/engine/src/cmd/presentation.rs b/crates/engine/src/cmd/presentation.rs index 66bb5fba5a..17ee75bbd5 100644 --- a/crates/engine/src/cmd/presentation.rs +++ b/crates/engine/src/cmd/presentation.rs @@ -414,6 +414,7 @@ pub fn page_layout(s: &Session, pres: &Presentation, page: &Page) -> Layout { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 71ab3346b2..32c6915aad 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -129,6 +129,7 @@ pub const CAPABILITIES: &[&str] = &[ "layout-legend-subtitle", "layout-legend-nested-sizes", "layout-extent-indicators", + "layout-extent-numbers", "layout-legend-order", "layout-picture-source", "layout-scale-bar-details", diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 23d1b6ec59..795528e6a1 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -183,6 +183,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[ ("layout-legend-subtitle", command_uses_legend_subtitle), ("layout-legend-nested-sizes", command_uses_legend_nested_sizes), ("layout-extent-indicators", command_uses_extent_indicators), + ("layout-extent-numbers", command_numbers_extents), ("layout-legend-order", command_uses_legend_order), ("layout-picture-source", command_uses_picture_source), ("layout-scale-bar-details", command_uses_scale_bar_details), @@ -694,6 +695,23 @@ fn extent_indicators_features(e: &rustgis_map::layout::Element) -> bool { e.kind.has_more_extents() } +/// A layout command that numbers a frame's extent indicators (#3394): an older reader ignores +/// `extentNumbers`, and a frame saved with it, drawing no numbers. `false` and `null` ask for what +/// it draws. +fn command_numbers_extents(id: &str, params: &Value) -> bool { + match id { + "layout.update" => params.get("extentNumbers") == Some(&Value::Bool(true)), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"extent_numbers\""], extent_numbers_features), + "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_numbers_extents)), + _ => false, + } +} + +/// Whether a layout element is a map frame that numbers its extent indicators. +fn extent_numbers_features(e: &rustgis_map::layout::Element) -> bool { + e.kind.numbers_extents() +} + /// A layout command that nests a legend's sizes (#3347): an older reader ignores `sizes`, and a /// legend saved with it, drawing rows. `"rows"` and `null` ask for what it draws. fn command_uses_legend_nested_sizes(id: &str, params: &Value) -> bool { @@ -1715,6 +1733,7 @@ fn layout_capabilities(layouts: &[rustgis_map::layout::Layout]) -> Vec<&'static (elements().any(legend_subtitle_features), "layout-legend-subtitle"), (elements().any(legend_nested_sizes_features), "layout-legend-nested-sizes"), (elements().any(extent_indicators_features), "layout-extent-indicators"), + (elements().any(extent_numbers_features), "layout-extent-numbers"), (elements().any(legend_orders_layers), "layout-legend-order"), (elements().any(pictures_with_files), "layout-picture-source"), (elements().any(bar_details), "layout-scale-bar-details"), @@ -2115,6 +2134,7 @@ mod tests { (&["layout-legend-counts"], "layout.update", json!({"element": 1, "showCounts": true})), (&["layout-legend-nested-sizes"], "layout.update", json!({"element": 1, "sizes": "nested"})), (&["layout-extent-indicators"], "layout.addElement", json!({"kind": "extentIndicator", "frame": 1, "of": 2})), + (&["layout-extent-numbers"], "layout.update", json!({"element": 1, "extentNumbers": true})), (&["layout-legend-subtitle"], "layout.update", json!({"element": 1, "subtitle": "a"})), (&["layout-legend-order"], "layout.update", json!({"element": 1, "layerOrder": ["a"]})), (&["layout-picture-source"], "layout.update", json!({"element": 1, "path": "logo.png"})), diff --git a/crates/engine/src/tests/archive_render_revision.rs b/crates/engine/src/tests/archive_render_revision.rs index 863b426864..05135615fc 100644 --- a/crates/engine/src/tests/archive_render_revision.rs +++ b/crates/engine/src/tests/archive_render_revision.rs @@ -149,6 +149,7 @@ fn page(map: &Map) -> map::layout::Layout { extent_color: map::Rgba::rgb(0, 0, 0), extent_width: 1.0, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/engine/src/tests/layout_extents.rs b/crates/engine/src/tests/layout_extents.rs index ec45d52111..547a20c1d9 100644 --- a/crates/engine/src/tests/layout_extents.rs +++ b/crates/engine/src/tests/layout_extents.rs @@ -264,3 +264,88 @@ fn a_files_further_width_past_the_limit_is_said() { frame["kind"]["MapFrame"]["more_extents"][0]["width"] = json!(99); assert_eq!(said(&layout), (1, rustgis_map::layout::MAX_INDICATOR_WIDTH)); } + +fn number(s: &mut Session, frame: u64, value: Value) -> Result { + s.run("layout.update", &json!({"element": frame, "extentNumbers": value})).map_err(|e| e.to_string()) +} + +/// What `frame` is saved with under `extent_numbers`. +fn saved_numbers(s: &Session, frame: u64) -> Value { + serde_json::to_value(&s.project.layouts[0].element(frame as u32).unwrap().kind).unwrap()["MapFrame"]["extent_numbers"].clone() +} + +fn requires_numbers(project: &Project) -> bool { + serde_json::to_value(project).unwrap()["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-extent-numbers"))) +} + +#[test] +fn numbering_is_turned_on_and_off_and_undone() { + let (mut s, overview, of) = laid_out(2); + add(&mut s, overview, of[0]); + let depth = s.undo_depth(); + number(&mut s, overview, json!(true)).unwrap(); + assert_eq!(saved_numbers(&s, overview), true); + assert_eq!(s.undo_depth(), depth + 1); + // Left out as not set: it stays as it is. + number(&mut s, overview, Value::Null).unwrap(); + assert_eq!(saved_numbers(&s, overview), true); + number(&mut s, overview, json!(false)).unwrap(); + assert_eq!(saved_numbers(&s, overview), Value::Null, "off is left out of the file"); + s.run("edit.undo", &Value::Null).unwrap(); + assert_eq!(saved_numbers(&s, overview), true); +} + +#[test] +fn numbering_refuses_what_is_not_a_map_frame_or_a_switch() { + let (mut s, overview, of) = laid_out(1); + add(&mut s, overview, of[0]); + let legend = s.run("layout.addElement", &json!({"kind": "legend"})).unwrap()["element"].as_u64().unwrap(); + for (element, value, said) in [ + (legend, json!(true), "only map frames number their extent indicators"), + (overview, json!("yes"), "extentNumbers is true or false"), + (overview, json!(1), "extentNumbers is true or false"), + ] { + let before = serde_json::to_value(&s.project).unwrap(); + let err = number(&mut s, element, value.clone()).expect_err(&value.to_string()); + assert!(err.contains(said), "{value}: {err}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "{value}"); + } +} + +#[test] +fn numbering_is_saved_with_its_capability_and_left_out_otherwise() { + let (mut s, overview, of) = laid_out(1); + add(&mut s, overview, of[0]); + assert!(!serde_json::to_string(&s.project).unwrap().contains("extent_numbers")); + assert!(!requires_numbers(&s.project)); + number(&mut s, overview, json!(true)).unwrap(); + assert!(requires_numbers(&s.project)); + let text = serde_json::to_string(&s.project).unwrap(); + let again: Project = serde_json::from_str(&text).unwrap(); + assert_eq!(serde_json::to_string(&again).unwrap(), text); + // With its indicators removed the frame keeps the setting, for the next one added. + update(&mut s, overview, json!(false)).unwrap(); + assert_eq!(saved_numbers(&s, overview), true); + assert!(requires_numbers(&s.project)); +} + +#[test] +fn saved_commands_that_number_require_the_capability() { + let (mut s, overview, of) = laid_out(1); + add(&mut s, overview, of[0]); + number(&mut s, overview, json!(true)).unwrap(); + let layout = serde_json::to_string(&s.project.layouts[0]).unwrap(); + let requires = |id: &str, params: Value| { + let mut p = Project::default(); + p.tasks.push( + serde_json::from_value(json!({"name": "t", "title": "t", "steps": [{"title": "s", "command": id, "params": params}]})).unwrap(), + ); + requires_numbers(&p) + }; + assert!(requires("layout.update", json!({"element": 1, "extentNumbers": true}))); + assert!(requires("layout.import", json!({"text": layout}))); + assert!(requires("script.run", json!({"script": "layout.update {\"element\": 1, \"extentNumbers\": true}"}))); + assert!(!requires("layout.update", json!({"element": 1, "extentNumbers": false}))); + assert!(!requires("layout.update", json!({"element": 1, "extentNumbers": null}))); + assert!(!requires("layout.addElement", json!({"kind": "northArrow"}))); +} diff --git a/crates/engine/src/tests/layout_files.rs b/crates/engine/src/tests/layout_files.rs index d809e2be41..6b7c2ab3f4 100644 --- a/crates/engine/src/tests/layout_files.rs +++ b/crates/engine/src/tests/layout_files.rs @@ -140,6 +140,7 @@ fn a_saved_layout_file_requires_what_its_elements_use_and_reads_back_the_same() ("legend-subtitle", include_str!("../../../../schema/fixtures/layout_legend_subtitle.rgis")), ("legend-nested-sizes", include_str!("../../../../schema/fixtures/layout_legend_nested_sizes.rgis")), ("extent-indicators", include_str!("../../../../schema/fixtures/layout_extent_indicators.rgis")), + ("extent-numbers", include_str!("../../../../schema/fixtures/layout_extent_numbers.rgis")), ("legend-order", include_str!("../../../../schema/fixtures/layout_legend_order.rgis")), ("picture-source", include_str!("../../../../schema/fixtures/layout_picture_source.rgis")), ("scale-grid-text", include_str!("../../../../schema/fixtures/layout_scale_grid_text.rgis")), diff --git a/crates/map/src/layout/elements.rs b/crates/map/src/layout/elements.rs index 7555dd05c6..677a702f5c 100644 --- a/crates/map/src/layout/elements.rs +++ b/crates/map/src/layout/elements.rs @@ -33,6 +33,10 @@ pub enum ElementKind { /// is left out when empty, so a frame with one indicator keeps its bytes. #[serde(default, skip_serializing_if = "Vec::is_empty")] more_extents: Vec, + /// Whether each outline carries its place in that list (1, 2, 3…) at its centre (#3394). + /// Left out when not, so layouts saved before keep their bytes. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + extent_numbers: bool, /// The outline drawn round the frame, along its shape: its colour and width (points), 0 /// for none (#2557). Dark grey and 0.8 unless set, and left out then, so layouts saved /// before keep their bytes. @@ -681,6 +685,11 @@ impl ElementKind { matches!(self, ElementKind::MapFrame { more_extents, .. } if !more_extents.is_empty()) } + /// Whether it is a map frame that numbers its extent indicators (#3394). + pub fn numbers_extents(&self) -> bool { + matches!(self, ElementKind::MapFrame { extent_numbers: true, .. }) + } + /// Whether it is a map frame with section lines (#2863). pub fn has_sections(&self) -> bool { matches!(self, ElementKind::MapFrame { sections, .. } if !sections.is_empty()) diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index af65e37b10..10c2c9e186 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -854,6 +854,7 @@ fn elements_add_hit_and_remove_with_surrounds() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: FRAME_BORDER_COLOR, border_width: FRAME_BORDER_WIDTH, bearing: 0.0, @@ -1352,6 +1353,7 @@ fn mgrs_labels_need_a_wgs84_utm_zone() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: FRAME_BORDER_COLOR, border_width: FRAME_BORDER_WIDTH, bearing: 0.0, @@ -2147,6 +2149,7 @@ fn a_profile_keeps_to_its_frames_side() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: FRAME_BORDER_COLOR, border_width: FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 6321e56f8d..8681fc53ed 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -337,6 +337,9 @@ fn element_bounds(layout: &Layout, id: u32, dpi: f64) -> Option 0 && (0.0..=cw).contains(&nx) && (0.0..=ch).contains(&ny) { + text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); } } } @@ -1245,3 +1267,7 @@ pub(crate) fn pin_mismatches(what: &str, got: &[(String, u64)], pinned: &[u64]) #[expect(clippy::disallowed_methods, reason = "a thread the system refuses fails the test, as it should")] #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "layout/extent_tests.rs"] +mod extent_tests; diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs new file mode 100644 index 0000000000..a75300fd6e --- /dev/null +++ b/crates/render/src/layout/extent_tests.rs @@ -0,0 +1,191 @@ +//! The numbers on a map frame's extent indicators (#3394). + +use serde_json::json; + +use super::*; +use crate::vector::Item; + +const COLORS: [[u8; 4]; 3] = [[255, 0, 255, 255], [0, 160, 160, 255], [200, 120, 0, 255]]; +/// The overview's box on the page (x, y, w, h in mm). +const OVERVIEW_MM: (f64, f64, f64, f64) = (10.0, 10.0, 80.0, 60.0); +const SCALE: f64 = 2e8; + +/// A page with an overview of the map "Over" outlining one frame of the map "m" per centre in +/// `centers` (longitude, latitude), numbered when `numbered`; only the overview is drawn. +fn page(centers: &[(f64, f64)], numbered: bool, colors: &[[u8; 4]]) -> Layout { + let mut l = Layout::new("Layout", 100.0, 100.0); + let of: Vec = centers + .iter() + .map(|&(x, y)| { + let kind = json!({"MapFrame": {"map": "m", "center": {"x": x, "y": y}, "scale": 100_000_000}}); + let id = l.add("Detail", (60.0, 75.0, 30.0, 20.0), serde_json::from_value(kind).unwrap()); + l.elements.iter_mut().filter(|e| e.id == id).for_each(|e| e.visible = false); + id + }) + .collect(); + let more: Vec<_> = of.iter().zip(colors).skip(1).map(|(of, c)| json!({"of": of, "color": c})).collect(); + let kind = json!({"MapFrame": {"map": "Over", "center": {"x": 0, "y": 0}, "scale": SCALE, + "extent_of": of.first(), "extent_color": colors[0], "more_extents": more, "extent_numbers": numbered}}); + l.add("Overview", OVERVIEW_MM, serde_json::from_value(kind).unwrap()); + l +} + +/// The text drawn in one of `colors` (not its halo), in the order drawn: what it says, its colour +/// and where its pen starts on the page, in pixels at 96 dpi (layout points). +fn written(l: &Layout, over: Map, colors: &[[u8; 4]]) -> Vec<(String, [u8; 4], (f32, f32))> { + written_from(l, [Map::new("m"), over], colors) +} + +/// [`written`] with the map the outlined frames show given too. +fn written_from(l: &Layout, mut maps: [Map; 2], colors: &[[u8; 4]]) -> Vec<(String, [u8; 4], (f32, f32))> { + fn walk(items: &[Item], at: Transform, colors: &[[u8; 4]], out: &mut Vec<(String, [u8; 4], (f32, f32))>) { + for item in items { + match item { + Item::Group(g) => walk(&g.items, at.pre_concat(g.transform), colors, out), + Item::Glyphs(run) if run.halo.is_none() => { + let c = run.color.to_color_u8(); + let rgba = [c.red(), c.green(), c.blue(), c.alpha()]; + if colors.contains(&rgba) { + let mut p = tiny_skia::Point::zero(); + at.pre_concat(run.transform).map_point(&mut p); + out.push((run.glyphs.iter().map(|g| g.ch).collect(), rgba, (p.x, p.y))); + } + } + _ => {} + } + } + } + let (drawing, pictured) = render_drawing(l, &mut maps, 96.0, &PageInfo::default()).unwrap(); + assert!(pictured.is_empty() && !drawing.overflowed()); + let (_, _, items) = drawing.finish(); + let mut out = Vec::new(); + walk(&items, Transform::identity(), colors, &mut out); + out +} + +fn said(drawn: &[(String, [u8; 4], (f32, f32))]) -> Vec<(&str, [u8; 4])> { + drawn.iter().map(|d| (d.0.as_str(), d.1)).collect() +} + +/// Where the flat overview shows (`lon`, `lat`), in pixels on the page. +fn on_page(lon: f64, lat: f64) -> (f32, f32) { + let map = Map::new("Over"); + let (x, y, w, h) = OVERVIEW_MM; + let camera = rustgis_map::layout::frame_camera(&map, Coord::new(0.0, 0.0), SCALE, 0.0, 0.0); + let at = camera.try_to_screen(map.to_map(Coord::new(lon, lat)).unwrap(), w * PT_PER_MM, h * PT_PER_MM).unwrap(); + ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32) +} + +/// A digit's pen starts half its width left of the centre, on a baseline below it. +fn centred(drawn: (f32, f32), centre: (f32, f32), within: f32) -> bool { + (drawn.0 - centre.0).abs() < within && (drawn.1 - centre.1).abs() < within +} + +const THREE: [(f64, f64); 3] = [(-35.0, 0.0), (0.0, 20.0), (35.0, -10.0)]; + +#[test] +fn each_outline_is_numbered_at_its_centre_in_list_order() { + let drawn = written(&page(&THREE, true, &COLORS), Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1]), ("3", COLORS[2])]); + for (d, (lon, lat)) in drawn.iter().zip(THREE) { + let at = on_page(lon, lat); + assert!(centred(d.2, at, 8.0), "{} starts at {:?}, its outline's centre is {at:?}", d.0, d.2); + } +} + +#[test] +fn a_single_outline_is_numbered_one() { + let drawn = written(&page(&THREE[..1], true, &COLORS), Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0])]); +} + +#[test] +fn outlines_are_not_numbered_unless_asked() { + assert!(written(&page(&THREE, false, &COLORS), Map::new("Over"), &COLORS).is_empty()); +} + +#[test] +fn removing_an_outline_renumbers_the_ones_after_it() { + let mut l = page(&THREE, true, &COLORS); + let overview = &mut l.elements.last_mut().unwrap().kind; + let mut list = overview.extent_indicators(); + list.remove(1); + overview.set_extent_indicators(list); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[2])]); + assert!(centred(drawn[1].2, on_page(35.0, -10.0), 8.0), "{drawn:?}"); +} + +#[test] +fn an_outline_centred_off_the_frame_has_no_number_and_the_others_keep_theirs() { + // The overview shows about 72° either side: 80° is off it, its outline's west side still on. + let drawn = written(&page(&[(-35.0, 0.0), (80.0, 0.0), (35.0, 0.0)], true, &COLORS), Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("3", COLORS[2])]); +} + +#[test] +fn an_outline_in_a_clear_colour_has_no_number() { + let colors = [[255, 0, 255, 0], COLORS[1]]; + let drawn = written(&page(&THREE[..2], true, &colors), Map::new("Over"), &colors); + assert_eq!(said(&drawn), [("2", COLORS[1])]); +} + +/// An outline cut at a world map's edge is in two pieces on opposite sides: its number goes with +/// the piece holding its centre, not in the middle of the map between them. +#[test] +fn an_outline_cut_at_the_maps_edge_is_numbered_on_the_side_of_its_centre() { + const WORLD_SCALE: f64 = 5e8; + let mut robinson = Map::new("Over"); + robinson.set_projection(rustgis_map::Projection::Custom("ESRI:54030".into())); + for lon in [175.0, -175.0] { + let mut l = page(&[(lon, 0.0)], true, &COLORS); + if let Some(ElementKind::MapFrame { scale, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + *scale = WORLD_SCALE; + } + let drawn = written(&l, robinson.clone(), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0])], "{lon}"); + let (x, y, w, h) = OVERVIEW_MM; + let camera = rustgis_map::layout::frame_camera(&robinson, Coord::new(0.0, 0.0), WORLD_SCALE, 0.0, 0.0); + let at = camera.try_to_screen(robinson.to_map(Coord::new(lon, 0.0)).unwrap(), w * PT_PER_MM, h * PT_PER_MM).unwrap(); + let centre = ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32); + assert!(centred(drawn[0].2, centre, 8.0), "{lon}: {drawn:?}, expected about {centre:?}"); + } +} + +/// A frame showing the whole globe has no outline, and so no number. +#[test] +fn a_frame_with_no_outline_has_no_number() { + let mut globe = Map::new("m"); + globe.set_projection(rustgis_map::Projection::Globe); + let mut l = page(&[(0.0, 0.0)], true, &COLORS); + if let Some(ElementKind::MapFrame { scale, .. }) = l.elements.first_mut().map(|e| &mut e.kind) { + *scale = 2e9; + } + assert!(written_from(&l, [globe, Map::new("Over")], &COLORS).is_empty()); +} + +/// A globe frame is drawn as pixels, so its number is found as what numbering changes. +#[test] +fn a_globe_overview_numbers_the_outline_where_the_globe_shows_it() { + let mut globe = Map::new("Over"); + globe.set_projection(rustgis_map::Projection::Globe); + let draw = |numbered| { + let mut maps = [Map::new("m"), globe.clone()]; + render(&page(&[(30.0, 25.0)], numbered, &COLORS), &mut maps, 96.0, &PageInfo::default()).unwrap() + }; + let (plain, numbered) = (draw(false), draw(true)); + let changed: Vec<(f32, f32)> = plain + .pixels() + .iter() + .zip(numbered.pixels()) + .enumerate() + .filter(|(_, (a, b))| a != b) + .map(|(at, _)| ((at as u32 % plain.width()) as f32, (at as u32 / plain.width()) as f32)) + .collect(); + assert!(!changed.is_empty(), "no number drawn"); + let (x, y, w, h) = OVERVIEW_MM; + let camera = rustgis_map::layout::frame_camera(&globe, Coord::new(0.0, 0.0), SCALE, 0.0, 0.0); + let at = rustgis_map::globe::Globe::new(&camera, w * PT_PER_MM, h * PT_PER_MM).forward(Coord::new(30.0, 25.0)).unwrap(); + let centre = ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32); + assert!(changed.iter().all(|&p| centred(p, centre, 14.0)), "{changed:?}, expected about {centre:?}"); +} diff --git a/crates/render/src/layout/frame.rs b/crates/render/src/layout/frame.rs index 54089e52af..464425c703 100644 --- a/crates/render/src/layout/frame.rs +++ b/crates/render/src/layout/frame.rs @@ -570,3 +570,19 @@ pub(super) fn outline_lonlat(pm: &mut Canvas, map: &Map, view: &View, ring: &[Op pm.stroke_path(&p, &paint(color, 1.0), &Stroke { width: width * ppp, ..Stroke::default() }, Transform::identity(), None); } } + +/// Where a frame's map shows `c` (longitude, latitude), the middle of another frame, in pixels of +/// its drawing: placed as [`outline_lonlat`] places that frame's outline, so on a map cut at its +/// edge it falls with the piece holding it. `None` where the map can't show it. +pub(super) fn place_lonlat(map: &Map, view: &View, c: Coord, from_globe: bool) -> Option<(f32, f32)> { + let (x, y) = if map.projection.is_globe() { + rustgis_map::globe::Globe::new(&view.camera, view.width, view.height).forward(c) + } else { + // As a ring of no length: the cut at a map's edge takes lines. + let placed = ring_on_flat(map, map.to_lonlat(view.camera.center), &[Some(c), Some(c)], from_globe); + let c = placed.into_iter().flatten().next()?; + map.to_map(c).and_then(|m| view.camera.try_to_screen(m, view.width, view.height)) + }?; + let ppp = view.pixels_per_point as f32; + Some((x as f32 * ppp, y as f32 * ppp)) +} diff --git a/crates/render/src/layout/legend/count_tests.rs b/crates/render/src/layout/legend/count_tests.rs index 4083fbf3c2..a3b400dfe6 100644 --- a/crates/render/src/layout/legend/count_tests.rs +++ b/crates/render/src/layout/legend/count_tests.rs @@ -372,6 +372,7 @@ fn counts_page(all_classes: bool) -> rustgis_map::layout::Layout { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, }, diff --git a/crates/render/src/layout/profile_tests.rs b/crates/render/src/layout/profile_tests.rs index 3e24111b9c..d5ae1957a2 100644 --- a/crates/render/src/layout/profile_tests.rs +++ b/crates/render/src/layout/profile_tests.rs @@ -32,6 +32,7 @@ fn frame_kind(sections: Vec
, bearing: f64, pitch: f64) -> ElementKind { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: FRAME_BORDER_COLOR, border_width: FRAME_BORDER_WIDTH, bearing, diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index b5ac7e0bd3..5b4afbc52b 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -3058,6 +3058,7 @@ fn colorbar_page(map: &Map, orientation: Orientation, reverse: bool, title: &str extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -3210,6 +3211,7 @@ fn page_settles_with_suns(suns: &[f64]) { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -3287,6 +3289,7 @@ fn a_reliefs_shadow_softness_changes_its_page_only_off_the_default() { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -3385,6 +3388,7 @@ fn a_reliefs_terrain_type_changes_its_frames_contrast_not_its_colour_bar() { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -3557,6 +3561,7 @@ fn indicator_page(color: Rgba, width: f64) -> Pixmap { extent_color: color, extent_width: width, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -4604,6 +4609,7 @@ fn a_map_frame_border_takes_its_colour_and_width_or_none() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color, border_width, bearing: 0.0, @@ -4668,6 +4674,7 @@ fn an_extent_indicator_on_a_globe_lands_on_the_area() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -4719,6 +4726,7 @@ fn a_frame_on_the_globe_is_outlined_by_what_it_shows() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -4775,6 +4783,7 @@ fn a_frame_partly_off_the_globe_is_outlined_without_a_chord() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -4852,6 +4861,7 @@ fn a_globe_frame_across_the_antimeridian_is_outlined_in_one_piece() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -4898,6 +4908,7 @@ fn outline_on(map: Map, source: (Coord, f64, f64, f64), locator: Map, view: (Coo extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5099,6 +5110,7 @@ fn a_flat_frame_wider_than_the_world_is_outlined_where_it_is() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5416,6 +5428,7 @@ fn colorbars_draw_the_raster_colour_scale() { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5540,6 +5553,7 @@ fn grid_page(grid: rustgis_map::layout::Grid) -> Pixmap { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5583,6 +5597,7 @@ fn winterthur_page(grid: rustgis_map::layout::Grid, projection: rustgis_map::Pro extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5841,6 +5856,7 @@ fn a_measured_grid_on_the_globe_seen_whole_is_drawn() { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -5877,6 +5893,7 @@ fn a_grid_on_the_globe_is_drawn_on_it() { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -6870,6 +6887,7 @@ fn colour_test_page() -> (Vec, Layout, u32) { extent_color: rustgis_map::layout::INDICATOR_COLOR, extent_width: rustgis_map::layout::INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -8694,6 +8712,7 @@ fn section_lines_are_drawn_over_their_frame() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: FRAME_BORDER_COLOR, border_width: FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/render/src/stream_tests.rs b/crates/render/src/stream_tests.rs index 1f0301123d..f0b3242d6d 100644 --- a/crates/render/src/stream_tests.rs +++ b/crates/render/src/stream_tests.rs @@ -565,6 +565,7 @@ fn layout_export_keeps_all_drawn_legend_classes() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, @@ -1378,6 +1379,7 @@ fn layout_export_rejects_unopened_local_shapefiles_and_required_masks() { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/render/src/tiles.rs b/crates/render/src/tiles.rs index 09b69a9229..c7935e06fc 100644 --- a/crates/render/src/tiles.rs +++ b/crates/render/src/tiles.rs @@ -2162,6 +2162,7 @@ pub(crate) mod tests { extent_color: INDICATOR_COLOR, extent_width: INDICATOR_WIDTH, more_extents: Vec::new(), + extent_numbers: false, border_color: rustgis_map::layout::FRAME_BORDER_COLOR, border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, bearing: 0.0, diff --git a/crates/ui-egui/src/layoutview/format/frame.rs b/crates/ui-egui/src/layoutview/format/frame.rs index 37baa3b462..586aafeebd 100644 --- a/crates/ui-egui/src/layoutview/format/frame.rs +++ b/crates/ui-egui/src/layoutview/format/frame.rs @@ -497,6 +497,10 @@ pub(super) fn frame_sections( } }); } + let mut numbered = e.kind.numbers_extents(); + if ui.checkbox(&mut numbered, tl!("Number them")).changed() { + update(app, json!({"extentNumbers": numbered})); + } }); } // Its border along its shape (#2557): its colour and width, 0 for none. diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index 9c36dbbd93..ef840c30b8 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -2151,6 +2151,44 @@ fn an_overview_frame_removes_one_of_several_extent_indicators() { assert_eq!(h.get_all_by_label("Remove Extent Indicator").count(), 1); } +/// A frame with an extent indicator has a "Number them" box (#3394), which numbers its outlines; +/// without one it has none. +#[test] +fn an_overview_frame_numbers_its_extent_indicators() { + use egui_kittest::kittest::Queryable; + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"name": "Overview"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let main = app.session.project.layouts[li].first_frame().unwrap(); + let inset = app.run("layout.addFrame", json!({"x": 200, "y": 150, "w": 60, "h": 40})).unwrap()["element"].as_u64().unwrap() as u32; + app.layout.selected = Some(inset); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 1100.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + with_format(app, ui); + }, + app, + ); + h.run_steps(3); + assert!(h.query_by_label("Number them").is_none(), "nothing to number yet"); + h.state_mut().run("layout.addElement", json!({"kind": "extentIndicator", "frame": inset, "of": main})).unwrap(); + h.run_steps(3); + let numbered = |app: &RustGisApp| { + serde_json::to_value(&app.session.project.layouts[li].element(inset).unwrap().kind).unwrap()["MapFrame"]["extent_numbers"] == true + }; + h.get_by_label("Number them").click(); + h.run_steps(3); + assert!(numbered(h.state())); + h.get_by_label("Number them").click(); + h.run_steps(3); + assert!(!numbered(h.state()), "and off again"); +} + /// A frame's Section Lines section (#2863) adds one each way, renames it, says one isn't drawn /// when its place is outside the frame, and removes it. #[test] diff --git a/docs/maintaining.md b/docs/maintaining.md index 948deca8a9..7d5fa49d46 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -376,6 +376,13 @@ replaces the frame's indicator instead of adding one, as do saved `layout.update `layout.import` or `report.existingTemplate` carrying such a frame requires it as well, and a layout file (`.rgislayout`) with one lists it in its header. +Projects with a map frame that numbers its extent indicators (`extent_numbers` on a map frame +element) require `layout-extent-numbers`, so an older reader refuses them instead of drawing the +outlines without their numbers and saving the frame again without the setting (#3394). Saved +`layout.update` commands whose `extentNumbers` is `true` require it too; `false` and `null` need +nothing. A saved `layout.import` or `report.existingTemplate` carrying such a frame requires it as +well, and a layout file (`.rgislayout`) with one lists it in its header. + Projects with a legend that shows feature counts (`show_counts` on a legend element) require `layout-legend-counts`, so an older reader refuses them instead of drawing no numbers and saving the legend again without the setting (#3237). Saved `layout.addElement` and `layout.update` diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index bfee8bbde1..b4cf6df2eb 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -324,7 +324,10 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu on `layout.addElement` (`kind` `extentIndicator`, with `of` the frame to outline), or `layout.update` with `"extentIndicator": {"color": "#555555", "width": 1}`. With several indicators, `of` says which: `{"of": 3, "width": 1}`, or `{"of": 3, "remove": true}`; `false` - removes them all. An outline breaks + removes them all. **Number them**, in the same section, draws 1, 2, 3… in their order at the + centre of each outline, in its colour, so a key map points to numbered detail maps; removing one + renumbers the rest, and an outline centred off the frame has no number (`layout.update` with + `"extentNumbers": true`). An outline breaks where the other frame's edge leaves the Earth (off a globe's disc, or past the edge of its projection's world) and runs up to it there, and where the overview's own projection can't show the place. An outline across the overview map's edge (on a world projection, the meridian diff --git a/schema/fixtures/layout_extent_numbers.rgis b/schema/fixtures/layout_extent_numbers.rgis new file mode 100644 index 0000000000..beb6b439d0 --- /dev/null +++ b/schema/fixtures/layout_extent_numbers.rgis @@ -0,0 +1,26 @@ +{ + "format": 2, + "name": "Extent numbers", + "requires": ["layout-extent-indicators", "layout-extent-numbers"], + "active_map": 0, + "maps": [ + { + "name": "Map", + "layers": [], + "basemap": "Paper", + "camera": {"center": {"x": 0, "y": 0}, "res": 30000} + } + ], + "layouts": [ + { + "name": "Key map", + "width": 200, + "height": 100, + "elements": [ + {"id": 1, "name": "Overview", "x": 5, "y": 5, "w": 90, "h": 90, "kind": {"MapFrame": {"map": "Map", "center": {"x": 0, "y": 0}, "scale": 100000000, "extent_of": 2, "more_extents": [{"of": 3, "color": [31, 78, 154, 255], "width": 1}], "extent_numbers": true}}}, + {"id": 2, "name": "North", "x": 100, "y": 5, "w": 95, "h": 40, "kind": {"MapFrame": {"map": "Map", "center": {"x": 0, "y": 20}, "scale": 10000000}}}, + {"id": 3, "name": "South", "x": 100, "y": 50, "w": 95, "h": 45, "kind": {"MapFrame": {"map": "Map", "center": {"x": 0, "y": -20}, "scale": 10000000}}} + ] + } + ] +} diff --git a/schema/project.schema.json b/schema/project.schema.json index 4f83cbbba0..b710ed0d49 100644 --- a/schema/project.schema.json +++ b/schema/project.schema.json @@ -1288,6 +1288,7 @@ "layout-legend-subtitle", "layout-legend-nested-sizes", "layout-extent-indicators", + "layout-extent-numbers", "layout-legend-order", "layout-picture-source", "layout-scale-bar-details", @@ -1427,6 +1428,7 @@ "layout-legend-subtitle", "layout-legend-nested-sizes", "layout-extent-indicators", + "layout-extent-numbers", "layout-legend-order", "layout-picture-source", "layout-scale-bar-details", From 8aa8d6ec6e93ff3ab1f6c606a3157643a36005e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 08:50:44 -0500 Subject: [PATCH 2/8] fix(layout): list extentNumbers in the command surface and place numbers only when asked (#3394) --- crates/engine/command-surface.txt | 2 +- crates/render/src/layout.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/engine/command-surface.txt b/crates/engine/command-surface.txt index fc45fd4648..1384000504 100644 --- a/crates/engine/command-surface.txt +++ b/crates/engine/command-surface.txt @@ -306,7 +306,7 @@ layout.save layout? path layout.series layout? layer nameField? frame? margin? off? layout.seriesPage layout? page layout.seriesRefresh layout? -layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? subtitle? allClasses? hideOther? showNoData? exclude? layerOrder? labels? classLabels? columns? textSize? showLayerNames? sizes? showCounts? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? map? scale? center? bearing? pitch? grid? extentIndicator? border? section? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? band? side? gap? thickness? unit? lineColor? lineWidth? min? guides? barOutline? hideLabels? rows? fields? header? image? path? background? +layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? valign? lineSpacing? case? font? fallback? title? subtitle? allClasses? hideOther? showNoData? exclude? layerOrder? labels? classLabels? columns? textSize? showLayerNames? sizes? showCounts? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? subdivisions? divisionsBeforeZero? labelPosition? unitLabel? unitLabelPosition? fit? map? scale? center? bearing? pitch? grid? extentIndicator? extentNumbers? border? section? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? band? side? gap? thickness? unit? lineColor? lineWidth? min? guides? barOutline? hideLabels? rows? fields? header? image? path? background? locator.find locator? text limit? zoom? map.activate name map.addTiles url kind? name? bounds? attribution? minZoom? maxZoom? tileSize? singleImage? tms? subdomains? layers? styleLayers? opacity? blend? resampling? overlay? top? replace? id? view? start? end? unit? interval? dates? legend? connection? anonymous? diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 8681fc53ed..8139765a32 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -483,7 +483,9 @@ fn draw_page( // Its number's place, for an outline there is (a frame showing the // whole globe has none): the middle of what that frame shows. let middle = om.to_lonlat(other.2.center).filter(|_| ring.iter().any(Option::is_some)); - if let Some(at) = middle.and_then(|c| place_lonlat(map, &view, c, from_globe)) { + if e.kind.numbers_extents() + && let Some(at) = middle.and_then(|c| place_lonlat(map, &view, c, from_globe)) + { numbers.push((n + 1, i.color, at)); } } @@ -492,13 +494,11 @@ fn draw_page( // Their numbers (#3394), over every outline so none crosses one: each in its // outline's colour, where its middle is on this frame. A clear colour // draws none, as it draws no outline, rather than a halo alone. - if e.kind.numbers_extents() { - let (cw, ch) = (frame_pm.width() as f32, frame_pm.height() as f32); - let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; - for (n, color, (nx, ny)) in numbers { - if color.a() > 0 && (0.0..=cw).contains(&nx) && (0.0..=ch).contains(&ny) { - text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); - } + let (cw, ch) = (frame_pm.width() as f32, frame_pm.height() as f32); + let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; + for (n, color, (nx, ny)) in numbers { + if color.a() > 0 && (0.0..=cw).contains(&nx) && (0.0..=ch).contains(&ny) { + text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); } } // Whole pixels, so the clipped image and its outline line up. From 87460709cc0b94478e74014d54c99d9864eb1cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 08:57:36 -0500 Subject: [PATCH 3/8] docs(layout): say in the help when an extent indicator has no number (#3394) --- crates/engine/src/cmd/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index ea732dd62a..ccea5c456d 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -369,7 +369,7 @@ pub fn commands() -> Vec { r##""pitch"?: degrees of tilt (0 to 85), "##, r##""grid"?: {grid options as in layout.addElement} | false (map frame; a grid's "labelSize" goes in it, and is refused outside it unless null), "##, r##""extentIndicator"?: {"of"?: frame id, "color"?, "width"?: points, "remove"?: bool} | false (map frame: restyles one of its extent indicators, or removes it with "remove": true; "of" is the id of the frame that indicator outlines and may be omitted when there is only one; false removes them all), "##, - r##""extentNumbers"?: bool (map frame: numbers its extent indicators 1, 2, 3… in their order, each in its outline's colour at the outline's centre, none where that centre is off the frame; refused for other elements), "##, + r##""extentNumbers"?: bool (map frame: numbers its extent indicators 1, 2, 3… in their order, each in its outline's colour at the outline's centre; none where that centre is off the frame or behind a globe's horizon, for an outline in a clear colour, or for a frame with no outline, one showing the whole globe; refused for other elements), "##, r##""border"?: {"color"?: "#RRGGBB"|"#RRGGBBAA", "width"?: points, 0 to 20, 0 for none} | null (map frame: the outline along its shape, default #303030 and 0.8, the keys not given keeping theirs; null gives the default back; refused for other elements), "##, r##""section"?: {"name": the section line it changes, "newName"?, "direction"?, "at"?, "color"?, "width"?, "dash"? ([] or null for solid), as in layout.addElement, the keys not given keeping theirs, "remove"?: true to remove it} (map frame; refused for other elements), "##, r##""layer"?: (colorbar: "" for the top visible raster with a colour scale, not drawn by classes), "##, From f1776b076da6ce238658a3cdc2cd0c7df5fb9d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 09:38:54 -0500 Subject: [PATCH 4/8] fix(layout): number only outlines centred inside a shaped overview (#3394) --- crates/render/src/layout.rs | 39 +++++++++++++++--------- crates/render/src/layout/extent_tests.rs | 16 ++++++++++ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 8139765a32..8aab87f229 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -491,30 +491,41 @@ fn draw_page( } } } - // Their numbers (#3394), over every outline so none crosses one: each in its - // outline's colour, where its middle is on this frame. A clear colour - // draws none, as it draws no outline, rather than a halo alone. - let (cw, ch) = (frame_pm.width() as f32, frame_pm.height() as f32); - let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; - for (n, color, (nx, ny)) in numbers { - if color.a() > 0 && (0.0..=cw).contains(&nx) && (0.0..=ch).contains(&ny) { - text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); - } - } // Whole pixels, so the clipped image and its outline line up. let (x0, y0) = (x.round(), y.round()); let r = Rect::from_xywh(x0, y0, ew.max(1.0), eh.max(1.0)).unwrap_or(r); let path = shape_path(shape, r); let up = (ppp / fppp) as f32; - // Shaped frames are clipped by a mask of the frame's own size. - if !matches!(shape, FrameShape::Rectangle) - && !frame_pm.is_recording() + // A shaped frame's mask, of the frame's own size: it cuts a frame drawn as pixels, + // and says which numbers are on the frame. + let mask = if !matches!(shape, FrameShape::Rectangle) + && (!frame_pm.is_recording() || !numbers.is_empty()) && let Some(p) = path.as_ref().and_then(|p| p.clone().transform(Transform::from_translate(-x0, -y0).post_scale(1.0 / up, 1.0 / up))) && let Some(mut m) = Mask::new(frame_pm.width(), frame_pm.height()) { m.fill_path(&p, FillRule::Winding, true, Transform::identity()); - frame_pm.pixels().apply_mask(&m); + Some(m) + } else { + None + }; + // Their numbers (#3394), over every outline so none crosses one: each in its + // outline's colour, where its middle is on this frame, inside its shape. A clear + // colour draws none, as it draws no outline, rather than a halo alone. + let (cw, ch) = (frame_pm.width(), frame_pm.height()); + let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; + for (n, color, (nx, ny)) in numbers { + let in_box = (0.0..cw as f32).contains(&nx) && (0.0..ch as f32).contains(&ny); + let in_shape = + mask.as_ref().is_none_or(|m| m.data().get(ny as usize * cw as usize + nx as usize).is_some_and(|&a| a >= 128)); + if color.a() > 0 && in_box && in_shape { + text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); + } + } + if !frame_pm.is_recording() + && let Some(m) = &mask + { + frame_pm.pixels().apply_mask(m); } let place = Transform::from_row(up, 0.0, 0.0, up, x0, y0); if frame_pm.is_recording() { diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index a75300fd6e..5cae49957f 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -164,6 +164,22 @@ fn a_frame_with_no_outline_has_no_number() { assert!(written_from(&l, [globe, Map::new("Over")], &COLORS).is_empty()); } +/// On a shaped overview a middle inside its box but outside its shape is off the overview: no +/// number, whole or cut. +#[test] +fn an_outline_centred_off_a_shaped_overview_has_no_number() { + // In the box's top right corner of the triangle and the ellipse, and just past the circle. + let triangle = FrameShape::Polygon(vec![(0.5, 0.0), (1.0, 1.0), (0.0, 1.0)]); + for (shape, off) in [(FrameShape::Circle, (55.0, 0.0)), (FrameShape::Ellipse, (55.0, 41.0)), (triangle, (55.0, 30.0))] { + let mut l = page(&[(0.0, 0.0), off], true, &COLORS); + assert_eq!(written(&l, Map::new("Over"), &COLORS).len(), 2, "{shape:?}: both are inside the box"); + if let Some(ElementKind::MapFrame { shape: of_frame, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + *of_frame = shape.clone(); + } + assert_eq!(said(&written(&l, Map::new("Over"), &COLORS)), [("1", COLORS[0])], "{shape:?}"); + } +} + /// A globe frame is drawn as pixels, so its number is found as what numbering changes. #[test] fn a_globe_overview_numbers_the_outline_where_the_globe_shows_it() { From 5da29fa29ced1f0bb22790b76540cb865dda81ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 09:46:00 -0500 Subject: [PATCH 5/8] fix(layout): read a shaped frame's mask only for a place on the frame (#3394) --- crates/render/src/layout.rs | 12 ++++++++---- crates/render/src/layout/extent_tests.rs | 13 +++++++++++++ docs/user-guide/layouts.md | 3 ++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 8aab87f229..987306e370 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -337,6 +337,13 @@ fn element_bounds(layout: &Layout, id: u32, dpi: f64) -> Option, (w, h): (u32, u32), (x, y): (f32, f32)) -> bool { + let in_box = (0.0..w as f32).contains(&x) && (0.0..h as f32).contains(&y); + // The box first: only a place in it has an index in the mask. + in_box && mask.is_none_or(|m| m.data().get(y as usize * w as usize + x as usize).is_some_and(|&a| a >= 128)) +} + /// The size of an extent indicator's number, in points (#3394). const EXTENT_NUMBER_SIZE: f32 = 10.0; @@ -515,10 +522,7 @@ fn draw_page( let (cw, ch) = (frame_pm.width(), frame_pm.height()); let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; for (n, color, (nx, ny)) in numbers { - let in_box = (0.0..cw as f32).contains(&nx) && (0.0..ch as f32).contains(&ny); - let in_shape = - mask.as_ref().is_none_or(|m| m.data().get(ny as usize * cw as usize + nx as usize).is_some_and(|&a| a >= 128)); - if color.a() > 0 && in_box && in_shape { + if color.a() > 0 && on_frame(mask.as_ref(), (cw, ch), (nx, ny)) { text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); } } diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index 5cae49957f..dfd80d68ed 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -180,6 +180,19 @@ fn an_outline_centred_off_a_shaped_overview_has_no_number() { } } +/// A centre far off the frame, as a hostile file can put it, is off it: the mask is not read +/// there, where its index would not fit. +#[test] +fn a_place_far_off_a_shaped_frame_is_off_it() { + let mut mask = Mask::new(8, 8).unwrap(); + mask.data_mut().fill(255); + assert!(on_frame(Some(&mask), (8, 8), (4.0, 4.0))); + for at in [(0.0, 1e30), (1e30, 0.0), (-1.0, 4.0), (4.0, 8.0), (f32::NAN, 4.0)] { + assert!(!on_frame(Some(&mask), (8, 8), at), "{at:?}"); + assert!(!on_frame(None, (8, 8), at), "{at:?}"); + } +} + /// A globe frame is drawn as pixels, so its number is found as what numbering changes. #[test] fn a_globe_overview_numbers_the_outline_where_the_globe_shows_it() { diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index b4cf6df2eb..443c7fc89b 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -327,7 +327,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu removes them all. **Number them**, in the same section, draws 1, 2, 3… in their order at the centre of each outline, in its colour, so a key map points to numbered detail maps; removing one renumbers the rest, and an outline centred off the frame has no number (`layout.update` with - `"extentNumbers": true`). An outline breaks + `"extentNumbers": true`). The frame keeps the setting when all its indicators are removed, so + the next one added is numbered. An outline breaks where the other frame's edge leaves the Earth (off a globe's disc, or past the edge of its projection's world) and runs up to it there, and where the overview's own projection can't show the place. An outline across the overview map's edge (on a world projection, the meridian From 5df2d2df7a97047aa733daeb4683adbf4d96a211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 10:01:05 -0500 Subject: [PATCH 6/8] fix(layout): move an extent number a turn of longitude with its outline (#3394) --- crates/render/src/layout.rs | 2 +- crates/render/src/layout/extent_tests.rs | 24 +++++++++++++++++ crates/render/src/layout/frame.rs | 34 +++++++++++++++--------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 987306e370..e2586c1ee9 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -491,7 +491,7 @@ fn draw_page( // whole globe has none): the middle of what that frame shows. let middle = om.to_lonlat(other.2.center).filter(|_| ring.iter().any(Option::is_some)); if e.kind.numbers_extents() - && let Some(at) = middle.and_then(|c| place_lonlat(map, &view, c, from_globe)) + && let Some(at) = middle.and_then(|c| place_lonlat(map, &view, c, &ring, from_globe)) { numbers.push((n + 1, i.color, at)); } diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index dfd80d68ed..74850469f1 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -152,6 +152,30 @@ fn an_outline_cut_at_the_maps_edge_is_numbered_on_the_side_of_its_centre() { } } +/// A globe frame's outline is moved a turn of longitude to sit by a flat overview's middle +/// (#2348): its number goes with it, not left a turn away. +#[test] +fn a_globe_frames_number_moves_a_turn_with_its_outline() { + const WIDE: f64 = 6e8; + let mut globe = Map::new("m"); + globe.set_projection(rustgis_map::Projection::Globe); + let mut l = page(&[(175.0, 0.0)], true, &COLORS); + if let Some(ElementKind::MapFrame { bearing, pitch, .. }) = l.elements.first_mut().map(|e| &mut e.kind) { + (*bearing, *pitch) = (90.0, 60.0); + } + if let Some(ElementKind::MapFrame { scale, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + *scale = WIDE; + } + let drawn = written_from(&l, [globe, Map::new("Over")], &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0])]); + let over = Map::new("Over"); + let (x, y, w, h) = OVERVIEW_MM; + let camera = rustgis_map::layout::frame_camera(&over, Coord::new(0.0, 0.0), WIDE, 0.0, 0.0); + let at = camera.try_to_screen(over.to_map(Coord::new(-185.0, 0.0)).unwrap(), w * PT_PER_MM, h * PT_PER_MM).unwrap(); + let centre = ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32); + assert!(centred(drawn[0].2, centre, 8.0), "{drawn:?}, expected about {centre:?}"); +} + /// A frame showing the whole globe has no outline, and so no number. #[test] fn a_frame_with_no_outline_has_no_number() { diff --git a/crates/render/src/layout/frame.rs b/crates/render/src/layout/frame.rs index 464425c703..6522a5567c 100644 --- a/crates/render/src/layout/frame.rs +++ b/crates/render/src/layout/frame.rs @@ -477,12 +477,7 @@ pub(super) fn frame_viewport( /// doesn't, a globe frame's around a pole, which runs a whole turn, at ±180°. pub(super) fn ring_on_flat(map: &Map, mid: Option, ring: &[Option], from_globe: bool) -> Vec> { use rustgis_data::Geometry; - let (west, east) = ring.iter().flatten().fold((f64::INFINITY, f64::NEG_INFINITY), |(w, e), c| (w.min(c.x), e.max(c.x))); - let around_pole = from_globe && east - west >= 360.0 - 1e-6; - let shift = match (from_globe && !around_pole && west <= east, mid) { - (true, Some(mid)) => 360.0 * ((mid.x - (west + east) / 2.0) / 360.0).round(), - _ => 0.0, - }; + let (shift, around_pole) = ring_shift(mid, ring, from_globe); let shifted = ring.iter().map(|c| c.map(|c| Coord::new(c.x + shift, c.y))); let flat = map.flat(); let edge = match &flat { @@ -503,6 +498,18 @@ pub(super) fn ring_on_flat(map: &Map, mid: Option, ring: &[Option] pieces.into_iter().flat_map(|p| p.into_iter().map(Some).chain([None])).collect() } +/// The turn of longitude [`ring_on_flat`] moves `ring` by to sit by the map's middle `mid`, and +/// whether it runs round a pole, which is not moved. +fn ring_shift(mid: Option, ring: &[Option], from_globe: bool) -> (f64, bool) { + let (west, east) = ring.iter().flatten().fold((f64::INFINITY, f64::NEG_INFINITY), |(w, e), c| (w.min(c.x), e.max(c.x))); + let around_pole = from_globe && east - west >= 360.0 - 1e-6; + let shift = match (from_globe && !around_pole && west <= east, mid) { + (true, Some(mid)) => 360.0 * ((mid.x - (west + east) / 2.0) / 360.0).round(), + _ => 0.0, + }; + (shift, around_pole) +} + /// Strokes the ring `ring` (longitude, latitude) on a frame's map `width` points wide: another /// frame's extent, as an extent indicator shows it, broken at each `None`. `from_globe`: the ring /// is a frame on the globe's, its longitudes run on across ±180° rather than kept in a world. @@ -571,15 +578,18 @@ pub(super) fn outline_lonlat(pm: &mut Canvas, map: &Map, view: &View, ring: &[Op } } -/// Where a frame's map shows `c` (longitude, latitude), the middle of another frame, in pixels of -/// its drawing: placed as [`outline_lonlat`] places that frame's outline, so on a map cut at its -/// edge it falls with the piece holding it. `None` where the map can't show it. -pub(super) fn place_lonlat(map: &Map, view: &View, c: Coord, from_globe: bool) -> Option<(f32, f32)> { +/// Where a frame's map shows `c` (longitude, latitude), the middle of the frame outlined by +/// `ring`, in pixels of its drawing: moved and cut as [`outline_lonlat`] moves and cuts that ring, +/// so it falls with the outline, on the piece holding it. `None` where the map can't show it. +pub(super) fn place_lonlat(map: &Map, view: &View, c: Coord, ring: &[Option], from_globe: bool) -> Option<(f32, f32)> { let (x, y) = if map.projection.is_globe() { rustgis_map::globe::Globe::new(&view.camera, view.width, view.height).forward(c) } else { - // As a ring of no length: the cut at a map's edge takes lines. - let placed = ring_on_flat(map, map.to_lonlat(view.camera.center), &[Some(c), Some(c)], from_globe); + let mid = map.to_lonlat(view.camera.center); + let c = Coord::new(c.x + ring_shift(mid, ring, from_globe).0, c.y); + // Moved already, so only cut at the map's edge; as a ring of no length, since the cut + // takes lines. + let placed = ring_on_flat(map, mid, &[Some(c), Some(c)], false); let c = placed.into_iter().flatten().next()?; map.to_map(c).and_then(|m| view.camera.try_to_screen(m, view.width, view.height)) }?; From 534c9ce0228e1cf67b880f7d5cf5e5e146db0428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 10:06:30 -0500 Subject: [PATCH 7/8] fix(layout): bring a globe frame's number to the turn of longitude its outline is drawn in (#3394) --- crates/render/src/layout/extent_tests.rs | 57 ++++++++++++++++++++++-- crates/render/src/layout/frame.rs | 28 ++++++++---- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index 74850469f1..5edf576c24 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -152,6 +152,31 @@ fn an_outline_cut_at_the_maps_edge_is_numbered_on_the_side_of_its_centre() { } } +/// The leftmost and rightmost pixel columns of what is stroked in `color`. +fn stroked_columns(l: &Layout, mut maps: [Map; 2], color: [u8; 4]) -> (f32, f32) { + fn walk(items: &[Item], at: Transform, color: [u8; 4], out: &mut (f32, f32)) { + for item in items { + match item { + Item::Group(g) => walk(&g.items, at.pre_concat(g.transform), color, out), + Item::Stroke { path, color: c, transform, .. } => { + let c = c.to_color_u8(); + if [c.red(), c.green(), c.blue(), c.alpha()] == color + && let Some(p) = path.clone().transform(at.pre_concat(*transform)) + { + *out = (out.0.min(p.bounds().left()), out.1.max(p.bounds().right())); + } + } + _ => {} + } + } + } + let (drawing, _) = render_drawing(l, &mut maps, 96.0, &PageInfo::default()).unwrap(); + let (_, _, items) = drawing.finish(); + let mut out = (f32::INFINITY, f32::NEG_INFINITY); + walk(&items, Transform::identity(), color, &mut out); + out +} + /// A globe frame's outline is moved a turn of longitude to sit by a flat overview's middle /// (#2348): its number goes with it, not left a turn away. #[test] @@ -159,19 +184,45 @@ fn a_globe_frames_number_moves_a_turn_with_its_outline() { const WIDE: f64 = 6e8; let mut globe = Map::new("m"); globe.set_projection(rustgis_map::Projection::Globe); - let mut l = page(&[(175.0, 0.0)], true, &COLORS); + // Tilted and turned east, the frame's middle is west of 180° and most of what it shows is + // east of it. + let mut l = page(&[(179.0, 0.0)], true, &COLORS); if let Some(ElementKind::MapFrame { bearing, pitch, .. }) = l.elements.first_mut().map(|e| &mut e.kind) { (*bearing, *pitch) = (90.0, 60.0); } if let Some(ElementKind::MapFrame { scale, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { *scale = WIDE; } + // The overview's middle by the frame, and far enough west that the outline moves a turn. + for over_lon in [0.0, -100.0] { + if let Some(ElementKind::MapFrame { center, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + *center = Coord::new(over_lon, 0.0); + } + let drawn = written_from(&l, [globe.clone(), Map::new("Over")], &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0])], "{over_lon}"); + let (left, right) = stroked_columns(&l, [globe.clone(), Map::new("Over")], COLORS[0]); + let x = drawn[0].2.0; + assert!(left - 8.0 < x && x < right + 8.0, "{over_lon}: the number at {x}, its outline from {left} to {right}"); + } +} + +/// An outline round a pole runs a whole turn and is not moved: nor is its number, whatever the +/// overview's middle. +#[test] +fn a_number_round_a_pole_stays_where_its_outline_is_cut() { + const WIDE: f64 = 6e8; + let mut globe = Map::new("m"); + globe.set_projection(rustgis_map::Projection::Globe); + let mut l = page(&[(-60.0, 82.0)], true, &COLORS); + if let Some(ElementKind::MapFrame { center, scale, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + (*center, *scale) = (Coord::new(150.0, 0.0), WIDE); + } let drawn = written_from(&l, [globe, Map::new("Over")], &COLORS); assert_eq!(said(&drawn), [("1", COLORS[0])]); let over = Map::new("Over"); let (x, y, w, h) = OVERVIEW_MM; - let camera = rustgis_map::layout::frame_camera(&over, Coord::new(0.0, 0.0), WIDE, 0.0, 0.0); - let at = camera.try_to_screen(over.to_map(Coord::new(-185.0, 0.0)).unwrap(), w * PT_PER_MM, h * PT_PER_MM).unwrap(); + let camera = rustgis_map::layout::frame_camera(&over, Coord::new(150.0, 0.0), WIDE, 0.0, 0.0); + let at = camera.try_to_screen(over.to_map(Coord::new(-60.0, 82.0)).unwrap(), w * PT_PER_MM, h * PT_PER_MM).unwrap(); let centre = ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32); assert!(centred(drawn[0].2, centre, 8.0), "{drawn:?}, expected about {centre:?}"); } diff --git a/crates/render/src/layout/frame.rs b/crates/render/src/layout/frame.rs index 6522a5567c..86f98c213e 100644 --- a/crates/render/src/layout/frame.rs +++ b/crates/render/src/layout/frame.rs @@ -477,7 +477,7 @@ pub(super) fn frame_viewport( /// doesn't, a globe frame's around a pole, which runs a whole turn, at ±180°. pub(super) fn ring_on_flat(map: &Map, mid: Option, ring: &[Option], from_globe: bool) -> Vec> { use rustgis_data::Geometry; - let (shift, around_pole) = ring_shift(mid, ring, from_globe); + let RingTurn { shift, around_pole, .. } = ring_turn(mid, ring, from_globe); let shifted = ring.iter().map(|c| c.map(|c| Coord::new(c.x + shift, c.y))); let flat = map.flat(); let edge = match &flat { @@ -498,16 +498,24 @@ pub(super) fn ring_on_flat(map: &Map, mid: Option, ring: &[Option] pieces.into_iter().flat_map(|p| p.into_iter().map(Some).chain([None])).collect() } -/// The turn of longitude [`ring_on_flat`] moves `ring` by to sit by the map's middle `mid`, and -/// whether it runs round a pole, which is not moved. -fn ring_shift(mid: Option, ring: &[Option], from_globe: bool) -> (f64, bool) { +/// How [`ring_on_flat`] takes a ring: the turn of longitude it moves it by to sit by the map's +/// middle, whether it runs round a pole (which is not moved), and the middle of its longitudes, +/// unless it does. +struct RingTurn { + shift: f64, + around_pole: bool, + middle: Option, +} + +fn ring_turn(mid: Option, ring: &[Option], from_globe: bool) -> RingTurn { let (west, east) = ring.iter().flatten().fold((f64::INFINITY, f64::NEG_INFINITY), |(w, e), c| (w.min(c.x), e.max(c.x))); let around_pole = from_globe && east - west >= 360.0 - 1e-6; - let shift = match (from_globe && !around_pole && west <= east, mid) { - (true, Some(mid)) => 360.0 * ((mid.x - (west + east) / 2.0) / 360.0).round(), + let middle = (!around_pole && west <= east).then_some((west + east) / 2.0); + let shift = match (middle.filter(|_| from_globe), mid) { + (Some(middle), Some(mid)) => 360.0 * ((mid.x - middle) / 360.0).round(), _ => 0.0, }; - (shift, around_pole) + RingTurn { shift, around_pole, middle } } /// Strokes the ring `ring` (longitude, latitude) on a frame's map `width` points wide: another @@ -586,7 +594,11 @@ pub(super) fn place_lonlat(map: &Map, view: &View, c: Coord, ring: &[Option Date: Mon, 30 Nov 2026 11:21:24 -0500 Subject: [PATCH 8/8] fix(layout): require layout-extent-numbers for a saved step that opens a numbered project (#3394) --- crates/engine/src/project_capabilities.rs | 30 +++++++++++++++++++++++ docs/maintaining.md | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index cc1ca27dde..a3382ba854 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -746,11 +746,21 @@ fn command_numbers_extents(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("extentNumbers") == Some(&Value::Bool(true)), "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"extent_numbers\""], extent_numbers_features), + // It opens its `text` only, whatever else the step holds. + "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_numbers_extents), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_numbers_extents)), _ => false, } } +/// Whether a project's saved text requires `layout-extent-numbers`, as one saved would: its +/// frames', its steps' and its notebooks'. Parsed only when the text names the key, either way it +/// is written, may hide it ([`may_hide_a_key`]), or holds a step's base64 `data` ([`names_data`]). +fn text_numbers_extents(text: &str) -> bool { + (["extent_numbers", "extentNumbers"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text)) + && Project::from_json(text).is_ok_and(|p| p.required_capabilities().contains(&"layout-extent-numbers")) +} + /// Whether a layout element is a map frame that numbers its extent indicators. fn extent_numbers_features(e: &rustgis_map::layout::Element) -> bool { e.kind.numbers_extents() @@ -2496,6 +2506,26 @@ mod tests { } } + /// A saved step that opens a project from its text requires `layout-extent-numbers` when that + /// project numbers a frame's outlines, header or not, in a script too; and nothing when it + /// doesn't. + #[test] + fn an_opened_project_that_numbers_outlines_requires_it() { + let text = include_str!("../../../schema/fixtures/layout_extent_numbers.rgis"); + let mut project: Value = serde_json::from_str(text).unwrap(); + project.as_object_mut().unwrap().remove("requires"); + let on = project.to_string(); + let off = on.replace("\"extent_numbers\":true", "\"extent_numbers\":false"); + assert_ne!(on, off); + let needs = + |id: &str, params: Value| model_capabilities(&[ModelStep { tool: id.into(), params }], None).contains(&"layout-extent-numbers"); + for (text, wanted) in [(&on, true), (&off, false), (&on.replace("extent_numbers", "extent\\u005fnumbers"), true)] { + assert_eq!(needs("project.openText", json!({"text": text})), wanted, "{text}"); + let line = format!("project.openText {}", json!({"text": text})); + assert_eq!(needs("script.run", json!({"script": line})), wanted, "in a script: {text}"); + } + } + /// `report.existingTemplate` imports a layout file as `layout.import` does, so a step of it /// requires what a `layout.import` of the same file does (#3226), by `text` or by `data`. Every /// `layout_*` fixture is walked, so a capability added with one and read for `layout.import` diff --git a/docs/maintaining.md b/docs/maintaining.md index d8de28fceb..dbbc3dd9ca 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -381,7 +381,8 @@ element) require `layout-extent-numbers`, so an older reader refuses them instea outlines without their numbers and saving the frame again without the setting (#3394). Saved `layout.update` commands whose `extentNumbers` is `true` require it too; `false` and `null` need nothing. A saved `layout.import` or `report.existingTemplate` carrying such a frame requires it as -well, and a layout file (`.rgislayout`) with one lists it in its header. +well, as does a saved `project.openText` whose project has one, and a layout file (`.rgislayout`) +with one lists it in its header. Projects with a legend that shows feature counts (`show_counts` on a legend element) require `layout-legend-counts`, so an older reader refuses them instead of drawing no numbers and saving