From 99951c7ce4ef32b76e03849dd176a290bbbfebae Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 15:20:19 -0300 Subject: [PATCH 1/4] feat(layout): a profile element beside a map frame (#2863 part 2a) A layout keeps a Profile element: a raster's profile along a section line of one of its map frames, on the side the line runs along. Its box is not its own: it is the frame's side along its length, with a gap and a thickness, and it is put back there whenever the frame is moved or resized (layout.update, a copy, a layout read). It goes with its frame. This part is the element itself, as layout files hold it: what it reads and draws comes in the next parts, its placeholder meanwhile. A hand- edited file is brought into range and counted. A layout with one requires layout-profile. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout/elements.rs | 2 + crates/engine/src/cmd/layout/update.rs | 4 + crates/engine/src/project.rs | 1 + crates/engine/src/project_capabilities.rs | 1 + crates/engine/src/tests/layout_files.rs | 1 + crates/map/src/layout.rs | 38 ++++ crates/map/src/layout/elements.rs | 32 +++- crates/map/src/layout/profile.rs | 203 ++++++++++++++++++++++ crates/map/src/layout/tests.rs | 76 ++++++++ crates/render/src/layout.rs | 2 + docs/maintaining.md | 4 + schema/fixtures/layout_profile.rgis | 27 +++ schema/project.schema.json | 2 + 13 files changed, 387 insertions(+), 6 deletions(-) create mode 100644 crates/map/src/layout/profile.rs create mode 100644 schema/fixtures/layout_profile.rgis diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index 91e695b957..c66bb3a032 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -18,6 +18,8 @@ pub(in crate::cmd::layout) fn duplicate_element(s: &mut Session, p: &Value) -> C c.visible = e.visible; c.background = e.background; } + // A profile's copy keeps beside the same frame: its box is the frame's side. + l.fit_profiles(); crate::cmd::say_name_cut(s, "Duplicate Element", "the copy's name", &e.name, &name); // Its offset can take a text's ink past the page's edge, as a move does (#2703); the lines it // loses are the original's, said when it was set. diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index 3574cff500..8d76fcbaa6 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -831,6 +831,10 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some((_, Some(name))) = &new_sections { say_section_not_drawn(s, li, id, name); } + // A frame moved or resized takes its profiles with it (#2863). + if let Some(l) = s.project.layouts.get_mut(li) { + l.fit_profiles(); + } say_held(s, "Element Properties", held); if let Some(w) = font_told { return Ok(json!({"warning": w})); diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 9c32ea295f..4746b856a1 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -114,6 +114,7 @@ pub const CAPABILITIES: &[&str] = &[ "blank-paper", "layout-frame-border", "layout-section", + "layout-profile", NATIVE_COORDINATES, "layout-text-size", "layout-legend-fit", diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 201f64f145..1a4ce53037 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -1271,6 +1271,7 @@ fn layout_capabilities(layouts: &[rustgis_map::layout::Layout]) -> Vec<&'static (elements().any(charts_outlined), "layout-chart-outline"), (elements().any(|e| e.kind.has_custom_border()), "layout-frame-border"), (elements().any(|e| e.kind.has_sections()), "layout-section"), + (elements().any(|e| e.kind.is_profile()), "layout-profile"), (elements().any(|e| e.kind.has_custom_text_size()), "layout-text-size"), (elements().any(legends_fitting), "layout-legend-fit"), (elements().any(legend_labels_classes), "layout-legend-class-labels"), diff --git a/crates/engine/src/tests/layout_files.rs b/crates/engine/src/tests/layout_files.rs index f1eed5eda6..0c18dc242d 100644 --- a/crates/engine/src/tests/layout_files.rs +++ b/crates/engine/src/tests/layout_files.rs @@ -125,6 +125,7 @@ fn a_saved_layout_file_requires_what_its_elements_use_and_reads_back_the_same() ("outline", include_str!("../../../../schema/fixtures/layout_chart_outline.rgis")), ("border", include_str!("../../../../schema/fixtures/layout_frame_border.rgis")), ("section", include_str!("../../../../schema/fixtures/layout_section.rgis")), + ("profile", include_str!("../../../../schema/fixtures/layout_profile.rgis")), ("legend-text", include_str!("../../../../schema/fixtures/layout_legend_text.rgis")), ("legend-fit", include_str!("../../../../schema/fixtures/layout_legend_fit.rgis")), ("legend-class-labels", include_str!("../../../../schema/fixtures/layout_legend_class_labels.rgis")), diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index 708b4a516d..871d9f7b51 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -22,6 +22,7 @@ mod grid; mod legend; mod lines; mod north; +mod profile; mod scale_bar; mod section; mod stats; @@ -35,6 +36,7 @@ pub use grid::*; pub use legend::*; pub use lines::*; pub use north::*; +pub use profile::*; pub use scale_bar::*; pub use section::*; pub use stats::*; @@ -465,6 +467,31 @@ impl Layout { self.elements.len() != before } + /// Each map frame's box (x, y, w, h in mm), by its id. + fn frame_boxes(&self) -> BTreeMap { + let frames = self.elements.iter().filter(|e| matches!(e.kind, ElementKind::MapFrame { .. })); + frames.map(|e| (e.id, (e.x, e.y, e.w, e.h))).collect() + } + + /// Puts each profile beside its frame (#2863), as [`Profile::rect`] lays it, after the frame + /// was moved or resized, or the profile's side, gap or thickness changed. How many moved. + pub fn fit_profiles(&mut self) -> usize { + let boxes = self.frame_boxes(); + let mut moved = 0; + for e in &mut self.elements { + if let ElementKind::Profile(p) = &e.kind + && let Some(frame) = boxes.get(&p.frame) + { + let (x, y, w, h) = p.rect(*frame); + if (e.x, e.y, e.w, e.h) != (x, y, w, h) { + (e.x, e.y, e.w, e.h) = (x, y, w, h); + moved += 1; + } + } + } + moved + } + /// Keeps the page and elements sane after loading or editing (layout files are untrusted): /// sizes in range, unique element ids, and no references to missing frames. Returns what it /// changed, so a project reader can say so. @@ -663,6 +690,7 @@ impl Layout { *width = finite_or(*width, 1.0).clamp(0.0, 50.0) } ElementKind::Line { width, .. } => *width = finite_or(*width, 1.0).clamp(0.1, 50.0), + ElementKind::Profile(p) => cut |= p.clamp(), _ => {} } // A map frame draws its map and its paper itself: a background on one (a hand-edited @@ -681,6 +709,9 @@ impl Layout { // References must point at map frames that exist. let frames: std::collections::HashSet = self.elements.iter().filter(|e| matches!(e.kind, ElementKind::MapFrame { .. })).map(|e| e.id).collect(); + // A profile's box is its frame's side (#2863): one saved elsewhere (a hand-edited file) is + // put back beside it. + let boxes = self.frame_boxes(); let (mut adjusted, mut cut_count) = (0usize, 0usize); let elements = std::mem::take(&mut self.elements); for (mut e, (mut changed, cut)) in elements.into_iter().zip(changed) { @@ -693,6 +724,13 @@ impl Layout { *extent_of = None; changed = true; } + if let ElementKind::Profile(p) = &e.kind + && let Some(frame) = boxes.get(&p.frame) + { + let (x, y, w, h) = p.rect(*frame); + changed |= (e.x, e.y, e.w, e.h) != (x, y, w, h); + (e.x, e.y, e.w, e.h) = (x, y, w, h); + } adjusted = adjusted.saturating_add(usize::from(changed)); cut_count = cut_count.saturating_add(usize::from(cut)); self.elements.push(e); diff --git a/crates/map/src/layout/elements.rs b/crates/map/src/layout/elements.rs index e873b950b4..3480db6b59 100644 --- a/crates/map/src/layout/elements.rs +++ b/crates/map/src/layout/elements.rs @@ -361,6 +361,8 @@ pub enum ElementKind { #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "crate::symbology::de_dash")] dash: Option, }, + /// A raster's profile along a section line of a map frame, beside the frame (#2863 part 2). + Profile(Profile), } pub(super) fn rows() -> usize { @@ -491,6 +493,7 @@ impl ElementKind { ElementKind::Rectangle { .. } => "rectangle", ElementKind::Ellipse { .. } => "ellipse", ElementKind::Line { .. } => "line", + ElementKind::Profile(_) => "profile", } } pub fn label(&self) -> &'static str { @@ -507,6 +510,7 @@ impl ElementKind { ElementKind::Rectangle { .. } => "Rectangle", ElementKind::Ellipse { .. } => "Ellipse", ElementKind::Line { .. } => "Line", + ElementKind::Profile(_) => "Profile", } } /// Whether an element of this kind takes a background (#2257): all but a map frame, which @@ -515,10 +519,18 @@ impl ElementKind { !matches!(self, ElementKind::MapFrame { .. }) } - /// Whether an element of this kind takes an ink (#2257): a legend, colorbar, table or chart, - /// whose text and lines are otherwise dark grey. The other kinds' colours are their own options. + /// Whether an element of this kind takes an ink (#2257): a legend, colorbar, table, chart or + /// profile, whose text and lines are otherwise dark grey. The other kinds' colours are their own + /// options. pub fn takes_ink(&self) -> bool { - matches!(self, ElementKind::Legend { .. } | ElementKind::Colorbar { .. } | ElementKind::Table { .. } | ElementKind::Chart { .. }) + matches!( + self, + ElementKind::Legend { .. } + | ElementKind::Colorbar { .. } + | ElementKind::Table { .. } + | ElementKind::Chart { .. } + | ElementKind::Profile(_) + ) } /// Whether `key` given as null changes an element of this kind, giving it back its default: @@ -538,7 +550,8 @@ impl ElementKind { ElementKind::Legend { color, .. } | ElementKind::Colorbar { color, .. } | ElementKind::Table { color, .. } - | ElementKind::Chart { color, .. } => *color, + | ElementKind::Chart { color, .. } + | ElementKind::Profile(Profile { color, .. }) => *color, _ => None, } } @@ -549,7 +562,8 @@ impl ElementKind { ElementKind::Legend { color, .. } | ElementKind::Colorbar { color, .. } | ElementKind::Table { color, .. } - | ElementKind::Chart { color, .. } => Some(color), + | ElementKind::Chart { color, .. } + | ElementKind::Profile(Profile { color, .. }) => Some(color), _ => None, } } @@ -576,6 +590,11 @@ impl ElementKind { matches!(self, ElementKind::MapFrame { sections, .. } if !sections.is_empty()) } + /// Whether it is a profile (#2863 part 2). + pub fn is_profile(&self) -> bool { + matches!(self, ElementKind::Profile(_)) + } + /// The map frame this element belongs to. pub fn frame(&self) -> Option { match self { @@ -584,7 +603,8 @@ impl ElementKind { | ElementKind::Legend { frame, .. } | ElementKind::Colorbar { frame, .. } | ElementKind::Table { frame, .. } - | ElementKind::Chart { frame, .. } => Some(*frame), + | ElementKind::Chart { frame, .. } + | ElementKind::Profile(Profile { frame, .. }) => Some(*frame), _ => None, } } diff --git a/crates/map/src/layout/profile.rs b/crates/map/src/layout/profile.rs new file mode 100644 index 0000000000..cc7679152b --- /dev/null +++ b/crates/map/src/layout/profile.rs @@ -0,0 +1,203 @@ +//! A raster's profile beside its map frame (#2863 part 2): along one of the frame's section lines, +//! on the side the line runs along, lined up with the map so that each point of the curve is +//! straight across from the place it comes from. + +use super::*; + +/// The side of its map frame a profile lies along: under or over it for a horizontal section line, +/// left or right of it for a vertical one. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub enum ProfileSide { + #[default] + Bottom, + Top, + Left, + Right, +} + +impl ProfileSide { + pub const ALL: [ProfileSide; 4] = [ProfileSide::Bottom, ProfileSide::Top, ProfileSide::Left, ProfileSide::Right]; + + pub fn code(self) -> &'static str { + match self { + ProfileSide::Bottom => "bottom", + ProfileSide::Top => "top", + ProfileSide::Left => "left", + ProfileSide::Right => "right", + } + } + + pub fn from_name(s: &str) -> Option { + ProfileSide::ALL.into_iter().find(|v| v.code().eq_ignore_ascii_case(s.trim())) + } + + /// Whether it runs across the page: under or over the frame. + pub fn is_horizontal(self) -> bool { + matches!(self, ProfileSide::Bottom | ProfileSide::Top) + } + + /// Whether a profile of `line` can lie on this side: one that runs the same way as the line. + pub fn suits(self, line: SectionLine) -> bool { + self.is_horizontal() == matches!(line, SectionLine::Horizontal(_)) + } + + /// The side a profile of `line` takes unless given one: under a horizontal line's frame, right + /// of a vertical one's. + pub fn default_for(line: SectionLine) -> ProfileSide { + match line { + SectionLine::Horizontal(_) => ProfileSide::Bottom, + SectionLine::Vertical(_) => ProfileSide::Right, + } + } +} + +/// A profile element: the values of a raster's band along a section line of its map frame, drawn +/// beside the frame as a line, filled under it or not, with a value axis, a distance axis and the +/// section's names at its ends. Its box isn't its own: it takes the frame's side along its length +/// ([`Profile::rect`]), so it follows the frame when the frame is moved or resized. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Profile { + /// The map frame, and its section line (by name, any case). + pub frame: u32, + pub section: String, + /// The raster layer of the frame's map (by name, any case), and its band from 1. + pub layer: String, + #[serde(default = "first_band", skip_serializing_if = "is_first_band")] + pub band: u32, + #[serde(default)] + pub side: ProfileSide, + /// Millimetres between the frame and the profile, and across the profile. + #[serde(default = "profile_gap")] + pub gap: f64, + #[serde(default = "profile_thickness")] + pub thickness: f64, + /// Written over the curve ("Elevation"), and after the value axis' numbers ("m"). Left out + /// when empty. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub title: String, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub unit: String, + /// The curve's colour and width (points), and the colour under it; none, no fill. + #[serde(default = "profile_line")] + pub line_color: Rgba, + #[serde(default = "profile_line_width")] + pub line_width: f64, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub fill: Option, + /// The value axis' ends; none, the samples' own, out to round numbers. Two profiles given the + /// same share a scale. Read only when below the other end. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub min: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub max: Option, + /// What the distance axis counts in, as a scale bar does. + #[serde(default, skip_serializing_if = "is_default")] + pub units: ScaleUnits, + /// Lines across the curve at the value axis' numbers. + #[serde(default = "yes", skip_serializing_if = "is_true")] + pub guides: bool, + /// The colour of its text and axes (#2257); none, the dark grey of [`INK`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub color: Option, +} + +/// A new profile's gap and thickness (mm), and the least and most each may be. +pub const PROFILE_GAP: f64 = 3.0; +pub const PROFILE_GAPS: (f64, f64) = (0.0, 100.0); +pub const PROFILE_THICKNESS: f64 = 30.0; +pub const PROFILE_THICKNESSES: (f64, f64) = (10.0, 300.0); +/// A new profile's curve: its colour, width (points) and fill; and the least and most the width may be. +pub const PROFILE_LINE: Rgba = Rgba::rgb(0x3C, 0x32, 0x28); +pub const PROFILE_LINE_WIDTH: f64 = 1.0; +pub const PROFILE_LINE_WIDTHS: (f64, f64) = (0.1, 10.0); +pub const PROFILE_FILL: Rgba = Rgba::rgb(0xD8, 0xC8, 0xA8); +/// The longest title and unit (characters), and the highest band a profile names. +pub const MAX_PROFILE_TEXT: usize = 100; +pub const MAX_PROFILE_BAND: u32 = 65_535; +/// The most samples a profile reads along its section, as the `profile.*` commands. +pub const MAX_PROFILE_SAMPLES: usize = 10_000; + +fn first_band() -> u32 { + 1 +} + +fn is_first_band(b: &u32) -> bool { + *b == 1 +} + +fn is_true(v: &bool) -> bool { + *v +} + +fn profile_gap() -> f64 { + PROFILE_GAP +} + +fn profile_thickness() -> f64 { + PROFILE_THICKNESS +} + +fn profile_line() -> Rgba { + PROFILE_LINE +} + +fn profile_line_width() -> f64 { + PROFILE_LINE_WIDTH +} + +impl Profile { + /// A profile of `layer`'s first band along `section` of `frame`, on `side`, in the default look. + pub fn new(frame: u32, section: String, layer: String, side: ProfileSide) -> Profile { + Profile { + frame, + section, + layer, + band: 1, + side, + gap: PROFILE_GAP, + thickness: PROFILE_THICKNESS, + title: String::new(), + unit: String::new(), + line_color: PROFILE_LINE, + line_width: PROFILE_LINE_WIDTH, + fill: Some(PROFILE_FILL), + min: None, + max: None, + units: ScaleUnits::default(), + guides: true, + color: None, + } + } + + /// Its box (x, y, w, h in mm) beside a frame whose box is `frame`: the frame's length along its + /// side, `gap` from it and `thickness` across. + pub fn rect(&self, (fx, fy, fw, fh): (f64, f64, f64, f64)) -> (f64, f64, f64, f64) { + let (g, t) = (self.gap, self.thickness); + match self.side { + ProfileSide::Bottom => (fx, fy + fh + g, fw, t), + ProfileSide::Top => (fx, fy - g - t, fw, t), + ProfileSide::Right => (fx + fw + g, fy, t, fh), + ProfileSide::Left => (fx - g - t, fy, t, fh), + } + } + + /// Its numbers and texts brought into range (a layout file is hostile input). Whether a text was + /// cut. + pub(super) fn clamp(&mut self) -> bool { + self.gap = finite_or(self.gap, PROFILE_GAP).clamp(PROFILE_GAPS.0, PROFILE_GAPS.1); + self.thickness = finite_or(self.thickness, PROFILE_THICKNESS).clamp(PROFILE_THICKNESSES.0, PROFILE_THICKNESSES.1); + self.line_width = finite_or(self.line_width, PROFILE_LINE_WIDTH).clamp(PROFILE_LINE_WIDTHS.0, PROFILE_LINE_WIDTHS.1); + self.band = self.band.clamp(1, MAX_PROFILE_BAND); + self.min = self.min.filter(|v| v.is_finite()); + self.max = self.max.filter(|v| v.is_finite()); + let mut cut = false; + for (text, most) in [(&mut self.title, MAX_PROFILE_TEXT), (&mut self.unit, MAX_PROFILE_TEXT), (&mut self.section, MAX_SECTION_NAME)] + { + // One line: a break in a hand-edited file is a space. + let one = text.replace("\r\n", " ").replace(['\n', '\r'], " "); + cut |= one.trim().chars().nth(most).is_some(); + *text = one.trim().chars().take(most).collect::().trim_end().to_string(); + } + cut + } +} diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index 0e9e307513..b15239b6c7 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -2012,3 +2012,79 @@ fn legend_class_labels_save_only_when_set_and_load_into_range() { let (saved, _, cut) = load(&json!({"a": many})); assert_eq!((saved["a"].as_object().unwrap().len(), cut), (MAX_LEGEND_LABELS, 1)); } + +/// A profile beside its frame (#2863 part 2): its box is the frame's side, which it takes back after +/// the frame moves or is resized; saved with only what isn't its default, read back as saved; a hostile file's brought into range and counted, +/// and one saved elsewhere than its frame's side put back there. +#[test] +fn a_profile_keeps_to_its_frames_side() { + let frame = (30.0, 34.0, 186.0, 112.0); + for (side, rect) in [ + (ProfileSide::Bottom, (30.0, 149.0, 186.0, 30.0)), + (ProfileSide::Top, (30.0, 1.0, 186.0, 30.0)), + (ProfileSide::Right, (219.0, 34.0, 30.0, 112.0)), + (ProfileSide::Left, (-3.0, 34.0, 30.0, 112.0)), + ] { + let p = Profile::new(1, "A".into(), "DEM".into(), side); + assert_eq!(p.rect(frame), rect, "{side:?}"); + } + assert!(ProfileSide::Bottom.suits(SectionLine::Horizontal(Coord::new(0.0, 0.0)))); + assert!(!ProfileSide::Left.suits(SectionLine::Horizontal(Coord::new(0.0, 0.0)))); + assert_eq!(ProfileSide::default_for(SectionLine::Vertical(Coord::new(0.0, 0.0))), ProfileSide::Right); + // On the page, it follows its frame. + let mut l = Layout::new("L", 297.0, 210.0); + let f = l.add( + "Map", + frame, + ElementKind::MapFrame { + map: "M".into(), + center: Coord::new(0.0, 0.0), + scale: 1e6, + shape: FrameShape::Rectangle, + grid: None, + sections: vec![Section::new("A".into(), SectionLine::Horizontal(Coord::new(0.0, 0.0)))], + extent_of: None, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + border_color: FRAME_BORDER_COLOR, + border_width: FRAME_BORDER_WIDTH, + bearing: 0.0, + pitch: 0.0, + }, + ); + let p = Profile::new(f, "A".into(), "DEM".into(), ProfileSide::Bottom); + let id = l.add("Profile", p.rect(frame), ElementKind::Profile(p)); + if let Some(e) = l.element_mut(f) { + (e.x, e.y, e.w) = (10.0, 20.0, 150.0); + } + assert_eq!(l.fit_profiles(), 1); + let e = l.element(id).unwrap(); + assert_eq!((e.x, e.y, e.w, e.h), (10.0, 135.0, 150.0, 30.0)); + assert_eq!(l.fit_profiles(), 0, "already beside it"); + // Saved with only what isn't its default; read back as saved; a repaired one needs nothing more. + let text = serde_json::to_string(&l).unwrap(); + let saved = text.split("\"Profile\":").nth(1).unwrap(); + for left_out in ["\"band\"", "\"min\"", "\"max\"", "\"units\"", "\"guides\"", "\"color\"", "\"title\"", "\"unit\""] { + assert!(!saved.contains(left_out), "{left_out} in {saved}"); + } + let back: Layout = serde_json::from_str(&text).unwrap(); + assert_eq!(back, l); + assert!(l.clamp().is_empty()); + // A hostile file: out of range, a text too long, saved away from its frame. + let hostile = text + .replace("\"gap\":3.0", "\"gap\":-1e9") + .replace("\"line_width\":1.0", "\"line_width\":1e9,\"band\":0,\"title\":\"x\\ny\",\"unit\":\"%UNIT%\"") + .replace("\"y\":135.0", "\"y\":0.0") + .replace("%UNIT%", &"m".repeat(MAX_PROFILE_TEXT + 5)); + let mut l: Layout = serde_json::from_str(&hostile).unwrap_or_else(|e| panic!("{e}: {hostile}")); + let repairs = l.clamp(); + assert_eq!((repairs.elements_adjusted, repairs.elements_cut), (1, 1), "{repairs:?}"); + let e = l.element(id).unwrap(); + let ElementKind::Profile(p) = &e.kind else { panic!("a profile") }; + assert_eq!((p.gap, p.line_width, p.band), (PROFILE_GAPS.0, PROFILE_LINE_WIDTHS.1, 1)); + assert_eq!((p.title.as_str(), p.unit.chars().count()), ("x y", MAX_PROFILE_TEXT)); + assert_eq!((e.x, e.y), (10.0, 132.0), "beside its frame again"); + assert!(l.clamp().is_empty(), "a repaired profile needs nothing more"); + // Its frame removed, it goes with it. + assert!(l.remove(f) && l.element(id).is_none()); +} diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 2a77925213..a6b70bde95 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -672,6 +672,8 @@ fn draw_page( None => placeholder(&mut pm, r, "Chart", ppp), } } + // What it reads and draws comes in its own steps (#2863 parts 2b and 2c). + ElementKind::Profile(_) => placeholder(&mut pm, r, "Profile", ppp), ElementKind::Rectangle { fill, outline, width, radius, dash } => { // Square corners filled as before, so an existing layout draws to the same pixels. let rounded = rounded_rect(r, (*radius * PT_PER_MM * ppp) as f32); diff --git a/docs/maintaining.md b/docs/maintaining.md index f8e490dde3..5e6e7b3de5 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -288,6 +288,10 @@ saved `layout.addElement` of a section requires it too, and so does a `layout.up `section` object, which an older reader warns that it ignores. A saved `layout.import` whose `text` has such a frame requires it as well. +Projects with a profile beside a map frame (an element of kind `Profile`) require `layout-profile`, +so an older reader refuses them instead of dropping the element (#2863 part 2). A saved +`layout.import` whose `text` has one requires it as well. + Projects with a legend, a scale bar or a grid whose text isn't its default size (`text_size` on a legend, 9 points by default, or on a scale bar, 8; `label_size` on a frame's grid, 8) require `layout-text-size`, so an older reader refuses them instead of drawing that text at the default diff --git a/schema/fixtures/layout_profile.rgis b/schema/fixtures/layout_profile.rgis new file mode 100644 index 0000000000..6defac7e10 --- /dev/null +++ b/schema/fixtures/layout_profile.rgis @@ -0,0 +1,27 @@ +{ + "format": 2, + "name": "Profile", + "requires": ["layout-section", "layout-profile"], + "active_map": 0, + "maps": [ + { + "name": "Map", + "layers": [], + "basemap": "Paper", + "camera": {"center": {"x": 0, "y": 0}, "res": 30000} + } + ], + "layouts": [ + { + "name": "Profiles", + "width": 200, + "height": 150, + "elements": [ + {"id": 1, "name": "Map", "x": 10, "y": 10, "w": 120, "h": 90, "kind": {"MapFrame": {"map": "Map", "center": {"x": 0, "y": 0}, "scale": 1000000, "sections": [ + {"name": "A", "line": {"Horizontal": {"x": 0.05, "y": 0.1}}, "color": [168, 67, 31, 255], "width": 1.5} + ]}}}, + {"id": 2, "name": "Profile", "x": 10, "y": 103, "w": 120, "h": 30, "kind": {"Profile": {"frame": 1, "section": "A", "layer": "DEM", "side": "Bottom", "gap": 3, "thickness": 30, "title": "Elevation", "unit": "m", "line_color": [60, 50, 40, 255], "line_width": 1, "fill": [216, 200, 168, 255]}}} + ] + } + ] +} diff --git a/schema/project.schema.json b/schema/project.schema.json index 480dd51cb4..b4a7fe8dc9 100644 --- a/schema/project.schema.json +++ b/schema/project.schema.json @@ -1276,6 +1276,7 @@ "blank-paper", "layout-frame-border", "layout-section", + "layout-profile", "native-coordinates", "layout-text-size", "layout-legend-fit", @@ -1398,6 +1399,7 @@ "blank-paper", "layout-frame-border", "layout-section", + "layout-profile", "native-coordinates", "layout-text-size", "layout-legend-fit", From b2e8670de29d847a07c8e209be2954ea514c7a69 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 18:36:05 -0300 Subject: [PATCH 2/4] fix(layout): a profile's crossed value axis is dropped when read, counted (#2863 part 2a) A hand-edited file with a profile's min at or above its max loaded unchanged and the value axis would quietly fall back to the samples' range. Profile::clamp now drops the pair, which Layout::clamp counts as an adjusted element. Co-Authored-By: Claude Opus 5.5 --- crates/map/src/layout/profile.rs | 6 +++++- crates/map/src/layout/tests.rs | 6 ++++++ crates/render/src/layout.rs | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/map/src/layout/profile.rs b/crates/map/src/layout/profile.rs index cc7679152b..ae7310adcd 100644 --- a/crates/map/src/layout/profile.rs +++ b/crates/map/src/layout/profile.rs @@ -85,7 +85,7 @@ pub struct Profile { #[serde(default, skip_serializing_if = "Option::is_none")] pub fill: Option, /// The value axis' ends; none, the samples' own, out to round numbers. Two profiles given the - /// same share a scale. Read only when below the other end. + /// same share a scale. A pair whose ends cross is dropped when read, counted as a repair. #[serde(default, skip_serializing_if = "Option::is_none")] pub min: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -190,6 +190,10 @@ impl Profile { self.band = self.band.clamp(1, MAX_PROFILE_BAND); self.min = self.min.filter(|v| v.is_finite()); self.max = self.max.filter(|v| v.is_finite()); + // Ends that cross can't be an axis: the samples' own range, counted as a repair. + if matches!((self.min, self.max), (Some(lo), Some(hi)) if lo >= hi) { + (self.min, self.max) = (None, None); + } let mut cut = false; for (text, most) in [(&mut self.title, MAX_PROFILE_TEXT), (&mut self.unit, MAX_PROFILE_TEXT), (&mut self.section, MAX_SECTION_NAME)] { diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index b15239b6c7..bf5403c3f8 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -2085,6 +2085,12 @@ fn a_profile_keeps_to_its_frames_side() { assert_eq!((p.title.as_str(), p.unit.chars().count()), ("x y", MAX_PROFILE_TEXT)); assert_eq!((e.x, e.y), (10.0, 132.0), "beside its frame again"); assert!(l.clamp().is_empty(), "a repaired profile needs nothing more"); + // A value axis whose ends cross is the samples' own again, counted. + let crossed = text.replace("\"line_width\":1.0", "\"line_width\":1.0,\"min\":900.0,\"max\":900.0"); + let mut l: Layout = serde_json::from_str(&crossed).unwrap_or_else(|e| panic!("{e}: {crossed}")); + assert_eq!(l.clamp().elements_adjusted, 1); + let ElementKind::Profile(p) = &l.element(id).unwrap().kind else { panic!("a profile") }; + assert_eq!((p.min, p.max), (None, None)); // Its frame removed, it goes with it. assert!(l.remove(f) && l.element(id).is_none()); } diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 159dc04c98..ea59fae9d7 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -676,7 +676,7 @@ fn draw_page( None => placeholder(&mut pm, r, "Chart", ppp), } } - // What it reads and draws comes in its own steps (#2863 parts 2b and 2c). + // What it reads and draws comes in its own steps (#2863 parts 2b to 2d). ElementKind::Profile(_) => placeholder(&mut pm, r, "Profile", ppp), ElementKind::Rectangle { fill, outline, width, radius, dash } => { // Square corners filled as before, so an existing layout draws to the same pixels. From 715f60366efe975bd535388df87c185d69031d15 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 20:01:29 -0300 Subject: [PATCH 3/4] fix(layout): a profile's import requires layout-profile; its copy and far boxes are said or kept (#2863 part 2a) - A saved layout.import or report.existingTemplate whose layout has a profile requires layout-profile, script.run included, even with no layout in the project. - Layout::clamp leaves a profile's box to its frame's side, so one past the page's limits beside a frame near them is no repair on every read. - layout.duplicateElement of a profile says its copy is put beside its frame, without the offset. Each has a test written first that failed. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout/elements.rs | 5 ++++ crates/engine/src/project_capabilities.rs | 20 +++++++++++++++ crates/engine/src/tests.rs | 2 ++ crates/engine/src/tests/layout_profiles.rs | 29 ++++++++++++++++++++++ crates/map/src/layout.rs | 11 +++++--- crates/map/src/layout/tests.rs | 6 +++++ 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 crates/engine/src/tests/layout_profiles.rs diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index c7072d9ce3..6bc42c2c0e 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -13,6 +13,7 @@ pub(in crate::cmd::layout) fn duplicate_element(s: &mut Session, p: &Value) -> C let l = active(s, p)?; let e = l.element(id).cloned().ok_or_else(|| format!("no element {id}"))?; let name = numbered(&e.name, |n| l.elements.iter().any(|x| x.name == n)); + let profile = e.kind.is_profile(); let copy = l.add(name.clone(), (e.x + dx, e.y + dy, e.w, e.h), e.kind); if let Some(c) = l.element_mut(copy) { c.visible = e.visible; @@ -20,6 +21,10 @@ pub(in crate::cmd::layout) fn duplicate_element(s: &mut Session, p: &Value) -> C } // A profile's copy keeps beside the same frame: its box is the frame's side. l.fit_profiles(); + if profile { + let why = "Duplicate Element: a profile's copy is put beside its frame, over the original"; + s.notify(format!("{why}: its box is the frame's side, so it takes no offset"), false); + } crate::cmd::say_name_cut(s, "Duplicate Element", "the copy's name", &e.name, &name); // Its offset can take a text's ink past the page's edge, as a move does (#2703); the lines it // loses are the original's, said when it was set. diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 7f3007a3fa..9d1235e530 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -169,6 +169,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[ ("blank-paper", command_uses_blank_paper), ("layout-frame-border", command_uses_frame_border), ("layout-section", command_uses_section), + ("layout-profile", command_uses_profile), ("layout-text-size", command_uses_text_size), ("layout-legend-fit", command_uses_legend_fit), ("layout-legend-class-labels", command_uses_legend_class_labels), @@ -509,6 +510,17 @@ fn command_uses_section(id: &str, params: &Value) -> bool { } } +/// A layout command that brings in a profile beside a map frame (#2863 part 2): an older reader +/// refuses the kind `Profile` of a layout file. +fn command_uses_profile(id: &str, params: &Value) -> bool { + match id { + // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"Profile\""], |e| e.kind.is_profile()), + "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_profile)), + _ => false, + } +} + /// A `layout.page` command that gives the page a colour other than white (#2855), read as the /// command reads it: an older reader ignores `color` and opens the page white. An imported layout /// file that has one needs it too. @@ -1613,6 +1625,13 @@ mod tests { use base64::Engine; base64::engine::general_purpose::STANDARD.encode(r#""#) }; + // A profile beside a frame with no section line of its own, so it needs only its own. + let profile_layout = { + let fixture = Project::from_json(include_str!("../../../schema/fixtures/layout_profile.rgis")).unwrap(); + let mut layout = serde_json::to_value(&fixture).unwrap()["layouts"][0].clone(); + layout["elements"][0]["kind"]["MapFrame"]["sections"] = json!([]); + layout.to_string() + }; let chart = json!({"kind": "chart"}); let samples: Vec<(&[&str], &str, Value)> = vec![ (&["slope-percent"], "raster.function", json!({"function": "slope", "units": "percent"})), @@ -1637,6 +1656,7 @@ mod tests { (&["blank-paper"], "map.basemap", json!({"name": "blank", "color": "#112233"})), (&["layout-frame-border"], "layout.update", json!({"element": 1, "border": {}})), (&["layout-section"], "layout.update", json!({"element": 1, "section": {"name": "A", "y": 0}})), + (&["layout-profile"], "layout.import", json!({"text": profile_layout})), (&["layout-text-size"], "layout.update", json!({"element": 1, "textSize": 12})), (&["layout-legend-fit"], "layout.update", json!({"element": 1, "fit": "adjustSize"})), (&["layout-legend-class-labels"], "layout.update", json!({"element": 1, "classLabels": {"a": {"x": "X"}}})), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 0cf477dd2f..4acdcfe27e 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -111,6 +111,8 @@ mod elevation_tiles; #[path = "tests/layout_files.rs"] mod layout_files; +#[path = "tests/layout_profiles.rs"] +mod layout_profiles; #[path = "tests/layout_sections.rs"] mod layout_sections; diff --git a/crates/engine/src/tests/layout_profiles.rs b/crates/engine/src/tests/layout_profiles.rs new file mode 100644 index 0000000000..211ed48872 --- /dev/null +++ b/crates/engine/src/tests/layout_profiles.rs @@ -0,0 +1,29 @@ +//! Profiles beside a layout's map frame (#2863 part 2): the commands and what they say. + +use crate::{Session, sample}; +use rustgis_map::layout::{ElementKind, Profile, ProfileSide}; +use serde_json::json; + +/// A profile put beside the first frame of the session's first layout, and its id. +fn put_profile(s: &mut Session, frame: u32, side: ProfileSide) -> u32 { + let l = &mut s.project.layouts[0]; + let e = l.element(frame).expect("the frame"); + let at = (e.x, e.y, e.w, e.h); + let p = Profile::new(frame, "A".into(), "DEM".into(), side); + l.add("Profile", p.rect(at), ElementKind::Profile(p)) +} + +/// A profile's copy stays beside its frame, over the original, and says it took no offset. +#[test] +fn a_profiles_copy_keeps_beside_its_frame_and_says_so() { + let mut s = Session::new(sample::world_project()); + s.run("layout.new", &json!({"page": "a4", "template": "map"})).unwrap(); + let frame = s.project.layouts[0].first_frame().unwrap(); + let id = put_profile(&mut s, frame, ProfileSide::Bottom); + let mark = s.notice_mark(); + let copy = s.run("layout.duplicateElement", &json!({"element": id, "dx": 20})).unwrap()["element"].as_u64().unwrap(); + let box_of = |id: u64| s.project.layouts[0].element(id as u32).map(|e| (e.x, e.y, e.w, e.h)); + assert_eq!(box_of(copy), box_of(u64::from(id))); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|t| t.contains("beside its frame") && t.contains("offset")), "{said:?}"); +} diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index 779131068a..3f8efd731b 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -557,10 +557,13 @@ impl Layout { seen.insert(next); next = next.saturating_add(1); } - e.w = finite_or(e.w, 20.0).clamp(MIN_ELEMENT_MM, 5000.0); - e.h = finite_or(e.h, 20.0).clamp(MIN_ELEMENT_MM, 5000.0); - e.x = finite_or(e.x, 0.0).clamp(-5000.0, 5000.0); - e.y = finite_or(e.y, 0.0).clamp(-5000.0, 5000.0); + // A profile's box is its frame's side, laid below: beside a frame at the limit, past it. + if !e.kind.is_profile() { + e.w = finite_or(e.w, 20.0).clamp(MIN_ELEMENT_MM, 5000.0); + e.h = finite_or(e.h, 20.0).clamp(MIN_ELEMENT_MM, 5000.0); + e.x = finite_or(e.x, 0.0).clamp(-5000.0, 5000.0); + e.y = finite_or(e.y, 0.0).clamp(-5000.0, 5000.0); + } let mut cut = e.name.chars().nth(200).is_some(); e.name = e.name.chars().take(200).collect(); match &mut e.kind { diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index bf5403c3f8..c12449a4ab 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -2091,6 +2091,12 @@ fn a_profile_keeps_to_its_frames_side() { assert_eq!(l.clamp().elements_adjusted, 1); let ElementKind::Profile(p) = &l.element(id).unwrap().kind else { panic!("a profile") }; assert_eq!((p.min, p.max), (None, None)); + // Beside a frame at the page's limit it lies past it, and that is no repair, however often read. + if let Some(e) = l.element_mut(f) { + (e.y, e.h) = (5000.0, 100.0); + } + assert_eq!(l.fit_profiles(), 1); + assert!(l.clamp().is_empty() && l.element(id).is_some_and(|e| e.y == 5103.0)); // Its frame removed, it goes with it. assert!(l.remove(f) && l.element(id).is_none()); } From 23821aa149a1de13bcc771485a6365ea33ecf52d Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 20:17:38 -0300 Subject: [PATCH 4/4] fix(layout): a box given to a profile by layout.update is said (#2863 part 2a) A profile's x, y, w or h given to layout.update were put back beside its frame by fit_profiles with no word, so a drag of the profile alone snapped back silently. It is said once now; the frame's own move and resize carry the profile as before. Test written first, failing with no notice. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout/update.rs | 9 ++++++++- crates/engine/src/tests/layout_profiles.rs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index b06f87375c..165a57cf1b 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -839,10 +839,17 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some((_, Some(name))) = &new_sections { say_section_not_drawn(s, li, id, name); } - // A frame moved or resized takes its profiles with it (#2863). + // A frame moved or resized takes its profiles with it (#2863); a profile's own box is its frame's. + let profile = s.project.layouts.get(li).and_then(|l| l.element(id)).filter(|e| e.kind.is_profile()).map(|e| e.name.clone()); if let Some(l) = s.project.layouts.get_mut(li) { l.fit_profiles(); } + if let Some(name) = profile.filter(|_| given(&["x", "y", "w", "h"])) { + s.notify( + format!("Element Properties: the profile '{name}' keeps beside its frame, whose side is its box: x, y, w and h are not used"), + false, + ); + } say_held(s, "Element Properties", held); if let Some(w) = font_told { return Ok(json!({"warning": w})); diff --git a/crates/engine/src/tests/layout_profiles.rs b/crates/engine/src/tests/layout_profiles.rs index 211ed48872..d4945a81f6 100644 --- a/crates/engine/src/tests/layout_profiles.rs +++ b/crates/engine/src/tests/layout_profiles.rs @@ -27,3 +27,22 @@ fn a_profiles_copy_keeps_beside_its_frame_and_says_so() { let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert!(said.iter().any(|t| t.contains("beside its frame") && t.contains("offset")), "{said:?}"); } + +/// A box given to a profile is its frame's side again, and said; the frame's own move is not. +#[test] +fn a_box_given_to_a_profile_is_said_not_kept() { + let mut s = Session::new(sample::world_project()); + s.run("layout.new", &json!({"page": "a4", "template": "map"})).unwrap(); + let frame = s.project.layouts[0].first_frame().unwrap(); + let id = put_profile(&mut s, frame, ProfileSide::Bottom); + let box_of = |s: &Session| s.project.layouts[0].element(id).map(|e| (e.x, e.y, e.w, e.h)); + let at = box_of(&s); + let mark = s.notice_mark(); + s.run("layout.update", &json!({"element": id, "x": 5, "h": 50})).unwrap(); + assert_eq!(box_of(&s), at); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|t| t.contains("Profile") && t.contains("beside its frame")), "{said:?}"); + let mark = s.notice_mark(); + s.run("layout.update", &json!({"element": frame, "x": 20})).unwrap(); + assert!(s.notices_since(mark).is_empty(), "the frame takes its profile along"); +}