mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-16 23:24:22 +00:00
Fill layers: reject non-finite gradient angles and pattern angle/phase (#5752)
layer.newFillLayer.gradient and gradient.presets.apply cast `angle` to f32 unchecked, so 1e39 stored an infinite angle (NaN render, .pcraft save refused) and 450 stayed 450. They now share gradient.fill.set's parsing: non-finite is BadParams, the angle is normalised to (-180, 180]. Pattern placement (Pattern Fill layer, Edit > Fill, Paint Bucket) does the same for its angle and rejects a phase outside +/-1e7 like pattern.fill.set. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8d840eab59
commit
bb7c84f997
8 files changed
+75
-22
No files matched your search
@@ -229,9 +229,6 @@ pub(crate) fn u32_id_param(cmd: &str, p: &Value, key: &str) -> Result<Option<u32
|
||||
p.get(key).map(|v| u32_id(cmd, key, v)).transpose()
|
||||
}
|
||||
|
||||
fn f32_or(p: &Value, key: &str, default: f32) -> f32 {
|
||||
p.get(key).and_then(Value::as_f64).map(|v| v as f32).unwrap_or(default)
|
||||
}
|
||||
pub(crate) fn color_param(p: &Value, key: &str, default: [f32; 4]) -> [f32; 4] {
|
||||
match p.get(key) {
|
||||
Some(Value::Array(a)) if a.len() >= 3 => {
|
||||
@@ -909,7 +906,7 @@ fn build() -> Vec<CommandSpec> {
|
||||
|s, p| {
|
||||
let a = color_param(p, "from", s.tools.foreground);
|
||||
let b = color_param(p, "to", s.tools.background);
|
||||
let angle = f32_or(p, "angle", 90.0);
|
||||
let angle = crate::gradient_fill_cmds::angle_param("layer.newFillLayer.gradient", p, 90.0)?;
|
||||
let style = crate::layer_style::gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear"));
|
||||
let reverse = p.get("reverse").and_then(Value::as_bool).unwrap_or(false);
|
||||
let id = s.edit("New Gradient Fill Layer", |doc, active| {
|
||||
|
||||
@@ -112,7 +112,7 @@ pub fn fill(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"white" => Source::Color([1.0; 4]),
|
||||
"pattern" => {
|
||||
let pat = crate::pattern_cmds::resolve_param(s, CMD, p)?;
|
||||
let (scale, angle, _, phase) = crate::pattern_cmds::placement(p);
|
||||
let (scale, angle, _, phase) = crate::pattern_cmds::placement(CMD, p)?;
|
||||
let tile = photocraft_compose::pattern::Tile::new(&pat).ok_or_else(|| bad("the pattern is empty or too large to paint"))?;
|
||||
// Photoshop's Fill uses the canvas origin.
|
||||
let place = photocraft_compose::pattern::Placement::new(Rect::EMPTY, false, phase, scale, angle);
|
||||
|
||||
@@ -36,6 +36,14 @@ fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }
|
||||
}
|
||||
|
||||
/// The `angle` param (degrees) normalised to (-180, 180], or `default` when absent. Rejects
|
||||
/// a non-number or one beyond f32 range: an infinite angle renders NaN and can't be saved.
|
||||
pub(crate) fn angle_param(cmd: &str, p: &Value, default: f32) -> Result<f32> {
|
||||
let Some(a) = p.get("angle") else { return Ok(default) };
|
||||
let a = a.as_f64().map(|v| v as f32).filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, "`angle` is a number (degrees)"))?.rem_euclid(360.0);
|
||||
Ok(if a > 180.0 { a - 360.0 } else { a })
|
||||
}
|
||||
|
||||
fn num(p: &Value, k: &str) -> Option<f32> {
|
||||
p.get(k).and_then(Value::as_f64).map(|v| v as f32).filter(|v| v.is_finite())
|
||||
}
|
||||
@@ -176,12 +184,7 @@ pub fn apply_set(layer: &Layer, f: &Fill, canvas: Rect, p: &Value, fg: [f32; 4],
|
||||
}
|
||||
let new_style = style_param(CMD, p)?;
|
||||
let explicit_geometry = ["angle", "scale", "offset"].iter().any(|k| p.get(*k).is_some());
|
||||
if let Some(a) = p.get("angle") {
|
||||
*angle = a.as_f64().map(|v| v as f32).filter(|v| v.is_finite()).ok_or_else(|| bad(CMD, "`angle` is a number (degrees)"))?.rem_euclid(360.0);
|
||||
if *angle > 180.0 {
|
||||
*angle -= 360.0;
|
||||
}
|
||||
}
|
||||
*angle = angle_param(CMD, p, *angle)?;
|
||||
if p.get("scale").is_some() {
|
||||
let v = num(p, "scale").filter(|v| *v > 0.0).ok_or_else(|| bad(CMD, "`scale` is a positive percentage"))?;
|
||||
*scale = clamp_scale(v / 100.0);
|
||||
|
||||
@@ -63,7 +63,7 @@ fn bucket(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let pat = crate::pattern_cmds::resolve_param(s, "paint.bucket", p)?;
|
||||
let tile = photocraft_compose::pattern::Tile::new(&pat)
|
||||
.ok_or_else(|| EngineError::BadParams { cmd: "paint.bucket".into(), msg: "the pattern is empty or too large to paint".into() })?;
|
||||
let (scale, angle, _, phase) = crate::pattern_cmds::placement(p);
|
||||
let (scale, angle, _, phase) = crate::pattern_cmds::placement("paint.bucket", p)?;
|
||||
Some((tile, scale, angle, phase))
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -300,19 +300,25 @@ fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"path": path, "count": pats.len(), "bytes": bytes.len()}))
|
||||
}
|
||||
|
||||
pub(crate) fn placement(p: &Value) -> (f32, f32, bool, (f32, f32)) {
|
||||
let num = |k: &str, d: f64| p.get(k).and_then(Value::as_f64).unwrap_or(d) as f32;
|
||||
let phase = p
|
||||
.get("phase")
|
||||
.and_then(Value::as_array)
|
||||
.map_or((0.0, 0.0), |a| (a.first().and_then(Value::as_f64).unwrap_or(0.0) as f32, a.get(1).and_then(Value::as_f64).unwrap_or(0.0) as f32));
|
||||
((num("scale", 100.0) / 100.0).clamp(0.01, 10.0), num("angle", 0.0), p.get("link").and_then(Value::as_bool).unwrap_or(true), phase)
|
||||
/// A pattern's scale, angle, link and phase from `p`. The angle is normalised to (-180, 180];
|
||||
/// a non-finite angle or a phase beyond ±`MAX_PHASE` is an error (it would render NaN).
|
||||
pub(crate) fn placement(cmd: &str, p: &Value) -> Result<(f32, f32, bool, (f32, f32))> {
|
||||
let scale = p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32;
|
||||
let max = crate::pattern_fill_cmds::MAX_PHASE;
|
||||
let a = p.get("phase").and_then(Value::as_array);
|
||||
let c = |i: usize| {
|
||||
let v = a.and_then(|a| a.get(i)).and_then(Value::as_f64).unwrap_or(0.0);
|
||||
(v.is_finite() && v.abs() <= max).then_some(v as f32).ok_or_else(|| bad(cmd, format!("`phase` needs finite coordinates within ±{max}")))
|
||||
};
|
||||
let phase = (c(0)?, c(1)?);
|
||||
let angle = crate::gradient_fill_cmds::angle_param(cmd, p, 0.0)?;
|
||||
Ok(((scale / 100.0).clamp(0.01, 10.0), angle, p.get("link").and_then(Value::as_bool).unwrap_or(true), phase))
|
||||
}
|
||||
|
||||
/// The edit of `layer.newFillLayer.pattern`: a Pattern Fill layer of `pat` placed as `p` asks,
|
||||
/// above `active`. The Pattern Fill dialog previews it on a copy of the document.
|
||||
pub(crate) fn insert_fill_layer(doc: &mut Document, active: &mut Option<photocraft_doc::LayerId>, pat: &Pattern, p: &Value) -> Result<photocraft_doc::LayerId> {
|
||||
let (scale, angle, link, phase) = placement(p);
|
||||
let (scale, angle, link, phase) = placement("layer.newFillLayer.pattern", p)?;
|
||||
ensure_in_doc(doc, pat);
|
||||
let fill = Fill::Pattern { name: pat.name.clone(), scale, id: pat.id.clone(), angle, link, phase };
|
||||
let mut l = Layer::new(doc.next_layer_name("Pattern Fill"), LayerContent::Fill(fill));
|
||||
@@ -535,6 +541,26 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_placement_rejects_non_finite_angle_and_phase() {
|
||||
// 1e39 overflowed f32 to infinity: a NaN render, and a fill layer .pcraft refused to save.
|
||||
let mut s = session(8);
|
||||
let n = s.active().unwrap().doc.layers.len();
|
||||
for cmd in ["layer.newFillLayer.pattern", "edit.fill", "paint.bucket"] {
|
||||
for bad in [json!({"angle": 1e39}), json!({"angle": "x"}), json!({"phase": [1e39, 0]}), json!({"phase": [0, -1e300]})] {
|
||||
let mut p = json!({"pattern": "Checkerboard", "contents": "pattern", "x": 1, "y": 1});
|
||||
p.as_object_mut().unwrap().extend(bad.as_object().unwrap().clone());
|
||||
assert!(s.execute(cmd, p).is_err(), "{cmd} {bad}");
|
||||
}
|
||||
}
|
||||
assert_eq!(s.active().unwrap().doc.layers.len(), n);
|
||||
// The angle is normalised to (-180, 180] like the Pattern Fill dialog's.
|
||||
let r = s.execute("layer.newFillLayer.pattern", json!({"pattern": "Checkerboard", "angle": 270, "phase": [3, -4]})).unwrap();
|
||||
let id = LayerId(r["layer"].as_u64().unwrap());
|
||||
let Some(LayerContent::Fill(Fill::Pattern { angle, phase, .. })) = s.active().unwrap().doc.layer(id).map(|l| &l.content) else { panic!() };
|
||||
assert_eq!((*angle, *phase), (-90.0, (3.0, -4.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fill_with_pattern_respects_selection() {
|
||||
for depth in [8u64, 16, 32] {
|
||||
|
||||
@@ -14,7 +14,7 @@ pub const CREATE: &str = "layer.newFillLayer.pattern";
|
||||
/// Photoshop's Scale range in the Pattern Fill dialog (percent).
|
||||
pub const SCALE_RANGE: (f64, f64) = (1.0, 1000.0);
|
||||
/// Largest phase (origin offset) accepted, in pixels.
|
||||
const MAX_PHASE: f64 = 1.0e7;
|
||||
pub(crate) const MAX_PHASE: f64 = 1.0e7;
|
||||
|
||||
fn bad(msg: impl Into<String>) -> EngineError {
|
||||
EngineError::BadParams { cmd: SET.into(), msg: msg.into() }
|
||||
|
||||
@@ -603,7 +603,7 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "gradient.presets.apply";
|
||||
let g = preset_param(s, p, CMD)?;
|
||||
let stops = g.doc_stops(s.tools.foreground, s.tools.background);
|
||||
let angle = p.get("angle").and_then(Value::as_f64).unwrap_or(90.0) as f32;
|
||||
let angle = crate::gradient_fill_cmds::angle_param(CMD, p, 90.0)?;
|
||||
let scale = (p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32 / 100.0).clamp(0.1, 1.5);
|
||||
let style = crate::layer_style::gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear"));
|
||||
let reverse = p.get("reverse").and_then(Value::as_bool).unwrap_or(false);
|
||||
|
||||
@@ -635,6 +635,33 @@ fn fill_layers_and_colors() {
|
||||
assert!(l < r);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gradient_fill_layer_angles_are_finite_and_normalised() {
|
||||
// `angle` 1e39 overflowed f32 to infinity: the render went NaN and .pcraft refused to save.
|
||||
let mut s = session_with_doc();
|
||||
let angle = |s: &Session| {
|
||||
let st = s.active().unwrap();
|
||||
match &st.doc.layer(st.active_layer.unwrap()).unwrap().content {
|
||||
photocraft_doc::LayerContent::Fill(photocraft_doc::Fill::Gradient { angle, .. }) => *angle,
|
||||
_ => panic!("not a gradient fill"),
|
||||
}
|
||||
};
|
||||
for cmd in ["layer.newFillLayer.gradient", "gradient.presets.apply"] {
|
||||
let layers = s.active().unwrap().doc.layers.len();
|
||||
for bad in [json!(1e39), json!(-1e300), json!("90")] {
|
||||
assert!(s.execute(cmd, json!({"angle": bad})).is_err(), "{cmd} {bad}");
|
||||
}
|
||||
assert_eq!(s.active().unwrap().doc.layers.len(), layers, "{cmd}: nothing added");
|
||||
// Normalised to (-180, 180] like gradient.fill.set.
|
||||
for (a, want) in [(450.0, 90.0), (-90.0, -90.0), (-180.0, 180.0)] {
|
||||
s.execute(cmd, json!({"angle": a})).unwrap();
|
||||
assert_eq!(angle(&s), want, "{cmd} {a}");
|
||||
}
|
||||
s.execute(cmd, json!({})).unwrap();
|
||||
assert_eq!(angle(&s), 90.0, "{cmd}: default");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_colors_with_mask_targeted_resets_to_mask_colors() {
|
||||
let mut s = Session::new();
|
||||
|
||||
Reference in new issue
Block a user