From 9b69ba89aa301f821bd1e2777e06bfa2e83af752 Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Sat, 10 Oct 2026 00:02:04 +0800 Subject: [PATCH] image.mode.bitmap: an unknown halftone shape is a bad-params error (#1842) HalftoneShape::from_id returns None for an unlisted id instead of silently falling back to Round, and the command fails naming the valid shapes (part of #507). Co-Authored-By: Claude Opus 5.5 --- crates/algo/src/quantize.rs | 9 +++++---- crates/engine/src/mode_cmds.rs | 3 ++- crates/engine/src/mode_cmds/tests.rs | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/algo/src/quantize.rs b/crates/algo/src/quantize.rs index 3b6feb20f6..589c9f196d 100644 --- a/crates/algo/src/quantize.rs +++ b/crates/algo/src/quantize.rs @@ -483,15 +483,16 @@ pub enum HalftoneShape { } impl HalftoneShape { - pub fn from_id(s: &str) -> Self { - match s { + pub fn from_id(s: &str) -> Option { + Some(match s { + "round" => HalftoneShape::Round, "ellipse" => HalftoneShape::Ellipse, "line" => HalftoneShape::Line, "square" => HalftoneShape::Square, "diamond" => HalftoneShape::Diamond, "cross" => HalftoneShape::Cross, - _ => HalftoneShape::Round, - } + _ => return None, + }) } } diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs index f9ee2154ed..ad8ca01795 100644 --- a/crates/engine/src/mode_cmds.rs +++ b/crates/engine/src/mode_cmds.rs @@ -346,7 +346,8 @@ fn bitmap(s: &mut Session, p: &Value) -> Result { "diffusion" => BitmapMethod::Diffusion, "halftone" => { let freq = num(p, "frequency", 53.0).clamp(1.0, 999.0); - BitmapMethod::Halftone { cell: (dpi / freq).max(2.0), angle: num(p, "angle", 45.0), shape: HalftoneShape::from_id(str_or(p, "shape", "round")) } + let shape = HalftoneShape::from_id(str_or(p, "shape", "round")).ok_or_else(|| bad(CMD, "shape: round|ellipse|line|square|diamond|cross"))?; + BitmapMethod::Halftone { cell: (dpi / freq).max(2.0), angle: num(p, "angle", 45.0), shape } } m => { return Err(bad(CMD, format!("method `{m}` (threshold|pattern|diffusion|halftone)"))); diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs index af9eb7989d..850da98f89 100644 --- a/crates/engine/src/mode_cmds/tests.rs +++ b/crates/engine/src/mode_cmds/tests.rs @@ -211,6 +211,13 @@ fn bitmap_methods_need_grayscale() { s.execute("edit.undo", json!({})).unwrap(); } assert!(s.execute("image.mode.bitmap", json!({"method": "nope"})).is_err()); + // An unlisted halftone shape is a bad-params error, not a silent Round (#507). + assert!(s.execute("image.mode.bitmap", json!({"method": "halftone", "shape": "hexagon"})).is_err()); + assert_eq!(doc(&s).mode, ColorMode::Grayscale); + for shape in ["round", "ellipse", "line", "square", "diamond", "cross"] { + s.execute("image.mode.bitmap", json!({"method": "halftone", "shape": shape})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + } } #[test]