mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
a14e94617b
commit
9b69ba89aa
3 files changed
+14
-5
No files matched your search
@@ -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<Self> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -346,7 +346,8 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"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)")));
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user