From 04b683098030a55c4015f65a884579623c1e5b84 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 20:29:18 -0300 Subject: [PATCH] fix(engine): a saved relief step requires its capability only when the command would draw it `command_sets_aerial` and `command_sets_shadow_softness` now read a saved `raster.symbology` as the command does: only a Shaded Relief (its type matched as the command matches it, a stretch when left out) reads either key, and a numeric string is its number. The default softness, 5 or "5", which the command keeps as none, requires nothing, like `null`. Follow-up promised on #3260. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/raster.rs | 15 ++++++++-- crates/engine/src/project_capabilities.rs | 36 ++++++++++++++++++----- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index c74f53b6bb..914794013d 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -975,7 +975,7 @@ pub(crate) fn add_named_bytes_read( fn symbology(s: &mut Session, p: &Value) -> CmdResult { let (id, r) = get(s, p)?; - let kind = ps(p, "type").unwrap_or("stretch").to_string(); + let kind = symbology_kind(p); // A custom ramp deleted from the project is still found on the raster that uses it. let ramp = ps(p, "ramp") .map(|n| { @@ -994,7 +994,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { let cur_band = rl.style.renderer.bands().first().copied().unwrap_or(0); let bi = bnd.unwrap_or(cur_band).min(r.band_count().saturating_sub(1)); let cur_ramp = rl.style.renderer.ramp(); - rl.style.renderer = match kind.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() { + rl.style.renderer = match kind.as_str() { "stretch" => { let ramp = ramp.or(cur_ramp.filter(|r| !r.is_qualitative())).unwrap_or_else(Ramp::gray); // Left out, a stretch keeps its own inversion (a band change); a new one isn't inverted. @@ -1087,6 +1087,17 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { }) } +/// `raster.symbology`'s `"type"` as the command matches it: lower case, without spaces, `_` or `-`; +/// left out, a stretch. +pub(crate) fn symbology_kind(p: &Value) -> String { + ps(p, "type").unwrap_or("stretch").to_ascii_lowercase().replace([' ', '_', '-'], "") +} + +/// Whether a `raster.symbology` draws a Shaded Relief, the only renderer that reads its relief keys. +pub(crate) fn symbology_is_relief(p: &Value) -> bool { + matches!(symbology_kind(p).as_str(), "shadedrelief" | "hillshade") +} + /// A Shaded Relief's `"shadowSoftness"` (see `raster.symbology`), `v` being the number given, if /// it is one: refused by name outside the range, never brought into it. The default's own value /// is kept as none, so a project that sets nothing new saves as it did. diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 1440cc949a..fee9521b8d 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -219,10 +219,14 @@ fn command_sets_publication_legend(id: &str, params: &Value) -> bool { /// A saved `raster.symbology` that sets a Shaded Relief's shadow softness (#3144): an older reader /// runs it without the key, saying nothing, so the shadows would be traced at the default disc. -/// `null` asks for that default. +/// Read as the command reads it (its type, and a numeric string is its number): `null` and the +/// default, which the command keeps as none, ask for that disc; another renderer reads no softness. fn command_sets_shadow_softness(id: &str, params: &Value) -> bool { match id { - "raster.symbology" => params.get("shadowSoftness").is_some_and(|v| !v.is_null()), + "raster.symbology" => { + crate::cmd::raster::symbology_is_relief(params) + && crate::cmd::f(params, "shadowSoftness").is_some_and(|v| v != rustgis_map::shadow::SOFT_EDGE) + } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_shadow_softness)), _ => false, } @@ -230,11 +234,11 @@ fn command_sets_shadow_softness(id: &str, params: &Value) -> bool { /// A saved `raster.symbology` that sets a Shaded Relief's aerial perspective (#3141): an older reader /// runs it without the key, saying nothing, so the low ground would be drawn without its haze. -/// Read as the command reads it (a numeric string is its number): `null` and 0 ask for none, -/// which an older reader draws. +/// Read as the command reads it (its type, and a numeric string is its number): `null` and 0 ask +/// for none, which an older reader draws; another renderer reads no aerial perspective. fn command_sets_aerial(id: &str, params: &Value) -> bool { match id { - "raster.symbology" => crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0), + "raster.symbology" => crate::cmd::raster::symbology_is_relief(params) && crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_aerial)), _ => false, } @@ -1682,6 +1686,17 @@ mod tests { // Read as the command reads it: a numeric string is its number, so "0" is none too. (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0"})), (&["shaded-relief-aerial"], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0.5"})), + // Only a Shaded Relief draws them, its type read as the command reads it: left out, the + // command draws a stretch, which an older reader draws the same. + (&[], "raster.symbology", json!({"type": "stretch", "aerial": 0.6, "shadowSoftness": 1.5})), + (&[], "raster.symbology", json!({"aerial": 0.6, "shadowSoftness": 1.5})), + (&["shaded-relief-aerial"], "raster.symbology", json!({"type": "hillshade", "aerial": 0.6})), + (&["shaded-relief-shadow-softness"], "raster.symbology", json!({"type": "Shaded Relief", "shadowSoftness": 1.5})), + // The default softness, 5, is kept as none, which an older reader draws; a numeric + // string is its number. + (&[], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": 5})), + (&[], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": "5"})), + (&["shaded-relief-shadow-softness"], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": "1.5"})), // An SVG image is a picture marker as well, in a node as in a saved command. (&["picture-markers", "svg-markers"], "layer.marker", json!({"image": svg})), (&[], "buffer", json!({"input": "Cities", "distance": 1})), @@ -1702,9 +1717,14 @@ mod tests { let missing: Vec<&str> = COMMAND_CAPABILITIES.iter().map(|&(name, _)| name).filter(|name| !covered.contains(name)).collect(); assert!(missing.is_empty(), "add a sample for each command capability, missing: {missing:?}"); // A saved script's step is read as the command reads it too. - for (aerial, needs) in [("0", false), ("0.5", true)] { - let script = format!(r#"raster.symbology {{"type": "shadedRelief", "aerial": "{aerial}"}}"#); - assert_eq!(command_sets_aerial("script.run", &json!({ "script": script })), needs, "{aerial}"); + for (key, value, needs) in + [("aerial", "0", false), ("aerial", "0.5", true), ("shadowSoftness", "5", false), ("shadowSoftness", "1.5", true)] + { + for kind in ["shadedRelief", "stretch"] { + let script = format!(r#"raster.symbology {{"type": "{kind}", "{key}": "{value}"}}"#); + let uses = if key == "aerial" { command_sets_aerial } else { command_sets_shadow_softness }; + assert_eq!(uses("script.run", &json!({ "script": script })), needs && kind == "shadedRelief", "{kind} {key} {value}"); + } } }