diff --git a/crates/engine/command-surface.txt b/crates/engine/command-surface.txt index ef0d908570..01c6a4e3ea 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? columnBreaks? 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? columnBreaks? 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/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index c1af5d4944..efee760540 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -373,6 +373,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 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), "##, @@ -606,6 +607,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 dac24e14b6..7917cd906b 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 1cad80bd8e..19b920c95c 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. @@ -644,6 +651,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 0df7028870..c607cebde6 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-legend-column-breaks", "layout-picture-source", diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 9d05d50dae..a3382ba854 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-legend-column-breaks", command_uses_legend_column_breaks), ("layout-picture-source", command_uses_picture_source), @@ -738,6 +739,33 @@ 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), + // 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() +} + /// 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 { @@ -1764,6 +1792,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(legend_breaks_columns), "layout-legend-column-breaks"), (elements().any(pictures_with_files), "layout-picture-source"), @@ -2165,6 +2194,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-legend-column-breaks"], "layout.update", json!({"element": 1, "columnBreaks": ["a"]})), @@ -2476,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/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 69b157272c..b14d91527d 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")), ("legend-column-breaks", include_str!("../../../../schema/fixtures/layout_legend_column_breaks.rgis")), ("picture-source", include_str!("../../../../schema/fixtures/layout_picture_source.rgis")), diff --git a/crates/map/src/layout/elements.rs b/crates/map/src/layout/elements.rs index 617faef6d7..5ab7bf7fd5 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. @@ -686,6 +690,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 63d14b4c21..c61944e3c5 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, @@ -2170,6 +2172,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 94e1c6a322..e0673a8041 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -337,6 +337,16 @@ 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; + /// Draws the selected region with the full layout's frame and dynamic-text context, recorded /// when `record` holds a list (see [`render_drawing`]), which gets the names of the frames /// drawn as pictures. `pending` records the frames and legends drawn without content still on @@ -468,7 +478,8 @@ fn draw_page( } } // The extent indicators, in order, a later one over an earlier one. - for i in e.kind.extent_indicators() { + let mut numbers = Vec::new(); + for (n, i) in e.kind.extent_indicators().into_iter().enumerate() { if let Some(other) = frame(i.of) && let Some(om) = maps.get(other.1) { @@ -476,6 +487,14 @@ fn draw_page( if let Some(map) = maps.get(mi) { let from_globe = om.projection.is_globe(); outline_lonlat(&mut frame_pm, map, &view, &ring, from_globe, i.color, i.width as f32); + // 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 e.kind.numbers_extents() + && let Some(at) = middle.and_then(|c| place_lonlat(map, &view, c, &ring, from_globe)) + { + numbers.push((n + 1, i.color, at)); + } } } } @@ -484,15 +503,33 @@ fn draw_page( 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 { + 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); + } + } + 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() { @@ -1248,3 +1285,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..5edf576c24 --- /dev/null +++ b/crates/render/src/layout/extent_tests.rs @@ -0,0 +1,295 @@ +//! 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:?}"); + } +} + +/// 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] +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); + // 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(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:?}"); +} + +/// 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()); +} + +/// 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 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() { + 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..86f98c213e 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 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 { @@ -503,6 +498,26 @@ 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() } +/// 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 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, + }; + RingTurn { shift, around_pole, middle } +} + /// 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. @@ -570,3 +585,26 @@ 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 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 { + let mid = map.to_lonlat(view.camera.center); + let RingTurn { shift, middle, .. } = ring_turn(mid, ring, from_globe); + // A globe frame's ring runs on from its first point, which can leave it a turn from its + // middle's longitude: the middle is brought to the ring's turn, then moved with it. + let own = middle.filter(|_| from_globe).map_or(0.0, |m| 360.0 * ((m - c.x) / 360.0).round()); + let c = Coord::new(c.x + own + shift, 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)) + }?; + 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 2c7c50f255..48c51edf0d 100644 --- a/crates/render/src/layout/legend/count_tests.rs +++ b/crates/render/src/layout/legend/count_tests.rs @@ -374,6 +374,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 8a312e4c46..2cb07d7bf8 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -3082,6 +3082,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, @@ -3234,6 +3235,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, @@ -3311,6 +3313,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, @@ -3409,6 +3412,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, @@ -3583,6 +3587,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, @@ -4630,6 +4635,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, @@ -4694,6 +4700,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, @@ -4745,6 +4752,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, @@ -4801,6 +4809,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, @@ -4878,6 +4887,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, @@ -4924,6 +4934,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, @@ -5125,6 +5136,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, @@ -5442,6 +5454,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, @@ -5566,6 +5579,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, @@ -5609,6 +5623,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, @@ -5867,6 +5882,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, @@ -5903,6 +5919,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, @@ -6896,6 +6913,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, @@ -8727,6 +8745,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 d3e7551d4d..de17284088 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, @@ -1379,6 +1380,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 61e707956a..dbbc3dd9ca 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -376,6 +376,14 @@ 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, 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 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 0496603d97..404f30b680 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -324,7 +324,11 @@ 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`). 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 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 77925530d0..9669e35f7b 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-legend-column-breaks", "layout-picture-source", @@ -1428,6 +1429,7 @@ "layout-legend-subtitle", "layout-legend-nested-sizes", "layout-extent-indicators", + "layout-extent-numbers", "layout-legend-order", "layout-legend-column-breaks", "layout-picture-source",