Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ 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;
c.background = e.background;
}
// A profile's copy keeps beside the same frame: its box is the frame's side.
l.fit_profiles();
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
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.
Expand Down
11 changes: 11 additions & 0 deletions crates/engine/src/cmd/layout/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +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 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();
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
}
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}));
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub const CAPABILITIES: &[&str] = &[
"blank-paper",
"layout-frame-border",
"layout-section",
"layout-profile",
NATIVE_COORDINATES,
"layout-text-size",
"layout-legend-fit",
Expand Down
21 changes: 21 additions & 0 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1300,6 +1312,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"),
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
(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"),
Expand Down Expand Up @@ -1612,6 +1625,13 @@ mod tests {
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 8 4"/>"#)
};
// 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"})),
Expand All @@ -1636,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"}}})),
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/tests/layout_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
48 changes: 48 additions & 0 deletions crates/engine/src/tests/layout_profiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! 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<String> = 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<String> = 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");
}
49 changes: 45 additions & 4 deletions crates/map/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod grid;
mod legend;
mod lines;
mod north;
mod profile;
mod scale_bar;
mod section;
mod stats;
Expand All @@ -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::*;
Expand Down Expand Up @@ -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<u32, (f64, f64, f64, f64)> {
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.
Expand Down Expand Up @@ -530,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 {
Expand Down Expand Up @@ -664,6 +694,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
Expand All @@ -682,6 +713,9 @@ impl Layout {
// References must point at map frames that exist.
let frames: std::collections::HashSet<u32> =
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) {
Expand All @@ -694,6 +728,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);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
}
adjusted = adjusted.saturating_add(usize::from(changed));
cut_count = cut_count.saturating_add(usize::from(cut));
self.elements.push(e);
Expand Down
32 changes: 26 additions & 6 deletions crates/map/src/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ pub enum ElementKind {
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "crate::symbology::de_dash")]
dash: Option<Dash>,
},
/// 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 {
Expand Down Expand Up @@ -501,6 +503,7 @@ impl ElementKind {
ElementKind::Rectangle { .. } => "rectangle",
ElementKind::Ellipse { .. } => "ellipse",
ElementKind::Line { .. } => "line",
ElementKind::Profile(_) => "profile",
}
}
pub fn label(&self) -> &'static str {
Expand All @@ -517,6 +520,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
Expand All @@ -525,10 +529,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(_)
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
)
}

/// Whether `key` given as null changes an element of this kind, giving it back its default:
Expand All @@ -548,7 +560,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,
}
}
Expand All @@ -559,7 +572,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,
}
}
Expand All @@ -586,6 +600,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<u32> {
match self {
Expand All @@ -594,7 +613,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,
}
}
Expand Down
Loading
Loading