mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
The bar height was clamped to [2, h/4], and f64::clamp panics when h/4 < 2, so any image shorter than 8 px panicked inside the command's run closure. Reject such images with a bad-params error before anything changes, like the existing bar-width check. Also keep the bar on the canvas when the label under it, or the left margin, would push it off a small image. Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0c2c268c49
commit
815b4ee0f9
2 files changed
+51
-2
No files matched your search
@@ -1021,6 +1021,9 @@ fn nice_length(d: &Document) -> f64 {
|
||||
[5.0, 2.0, 1.0].into_iter().map(|m| m * e).find(|v| *v <= target).unwrap_or(e)
|
||||
}
|
||||
|
||||
/// The shortest image a scale bar fits: four times its 2 px minimum height (#932).
|
||||
const MIN_SCALE_MARKER_HEIGHT: f64 = 8.0;
|
||||
|
||||
fn place_scale_marker(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "image.analysis.placeScaleMarker";
|
||||
let d = doc(s)?;
|
||||
@@ -1034,6 +1037,11 @@ fn place_scale_marker(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if px < 1.0 || px > w {
|
||||
return Err(bad(CMD, format!("a {length} {} bar is {px:.1} px; it must fit the {w} px wide image", sc.units)));
|
||||
}
|
||||
// The bar is at least 2 px tall and at most a quarter of the image, so a shorter image has no
|
||||
// room for it (and would invert the height clamp below, which panics) (#932).
|
||||
if h < MIN_SCALE_MARKER_HEIGHT {
|
||||
return Err(bad(CMD, format!("the image is {h} px tall; a scale marker needs at least {MIN_SCALE_MARKER_HEIGHT} px")));
|
||||
}
|
||||
let font_size = p.get("fontSize").and_then(Value::as_f64).unwrap_or(12.0).clamp(1.0, 1000.0);
|
||||
let font = p.get("font").and_then(Value::as_str).map(str::to_string);
|
||||
let show_text = p.get("displayText").and_then(Value::as_bool).unwrap_or(true);
|
||||
@@ -1051,8 +1059,11 @@ fn place_scale_marker(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let margin = (w.min(h) * 0.03).round().max(2.0);
|
||||
let bar_h = (font_size / 4.0).round().clamp(2.0, h / 4.0);
|
||||
let text_h = if show_text { font_size * 1.25 } else { 0.0 };
|
||||
let bar_y = if text_top { h - margin - bar_h } else { h - margin - bar_h - text_h };
|
||||
let bar = Rect::new(margin as i32, bar_y.round() as i32, (margin + px).round() as i32, (bar_y + bar_h).round() as i32);
|
||||
// Keep the bar on the canvas when the text under it doesn't fit; the label is clipped instead (#932).
|
||||
let bar_y = if text_top { h - margin - bar_h } else { h - margin - bar_h - text_h }.max(0.0);
|
||||
// Likewise pull a bar nearly as wide as the image in from the right edge (#932).
|
||||
let bar_x = margin.min(w - px);
|
||||
let bar = Rect::new(bar_x.round() as i32, bar_y.round() as i32, (bar_x + px).round() as i32, (bar_y + bar_h).round() as i32);
|
||||
let label = format!("{} {}", trim_num(length), sc.units);
|
||||
compound(s, "Place Scale Marker", |s| {
|
||||
let fmt = doc(s)?.pixel_format();
|
||||
|
||||
@@ -276,6 +276,44 @@ fn place_scale_marker_builds_one_group_in_one_step() {
|
||||
assert!(d(&s).layers.iter().all(|l| l.name != "Measurement Scale Marker"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn place_scale_marker_on_a_short_document_errors_without_panicking() {
|
||||
// A document under 8 px tall inverted the bar-height clamp and panicked (#932). Call `run`
|
||||
// directly too: dispatch would catch the panic and hide it behind a generic error.
|
||||
for (w, h, p) in [
|
||||
(100, 4, json!({})),
|
||||
(100, 1, json!({})),
|
||||
(100, 7, json!({"displayText": false, "textPosition": "top"})),
|
||||
(300_000, 3, json!({"fontSize": 1000})),
|
||||
(2, 7, json!({"length": 1})),
|
||||
(7, 7, json!({"length": 7, "fontSize": 1})),
|
||||
] {
|
||||
for h in [h, 1, 2, 3, 5, 6] {
|
||||
let mut s = session(w, h, 8);
|
||||
let before = s.active().unwrap().history.past_len();
|
||||
let e = place_scale_marker(&mut s, &p).unwrap_err().to_string();
|
||||
assert!(e.contains("tall"), "{w}x{h}: {e}");
|
||||
let e = s.execute("image.analysis.placeScaleMarker", p.clone()).unwrap_err().to_string();
|
||||
assert!(!e.contains("internal error"), "{w}x{h}: {e}");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), before, "{w}x{h}: nothing added");
|
||||
assert!(d(&s).layers.iter().all(|l| l.name != "Measurement Scale Marker"));
|
||||
}
|
||||
}
|
||||
// Control: 8 px is the shortest that fits, and tall, narrow or roomy documents still get a
|
||||
// marker that lies on the canvas.
|
||||
for (w, h, p) in [(100, 8, json!({})), (100, 100, json!({})), (20, 4000, json!({})), (2, 8, json!({"length": 2}))] {
|
||||
let mut s = session(w, h, 8);
|
||||
let r = place_scale_marker(&mut s, &p).unwrap();
|
||||
let b: Vec<i64> = r["barRect"].as_array().unwrap().iter().map(|v| v.as_i64().unwrap()).collect();
|
||||
assert!(0 <= b[0] && b[0] < b[2] && b[2] <= i64::from(w), "{w}x{h}: bar on the canvas: {r}");
|
||||
assert!(0 <= b[1] && b[1] < b[3] && b[3] <= i64::from(h), "{w}x{h}: bar on the canvas: {r}");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), 1);
|
||||
let doc = d(&s);
|
||||
let bar = doc.layers.last().unwrap().children().unwrap().iter().find(|l| l.name == "Scale Bar").unwrap();
|
||||
assert_eq!(f64::from(bar.surface().unwrap().content_bounds().width()), r["pixels"].as_f64().unwrap(), "{w}x{h}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_info_and_cmyk_document() {
|
||||
let mut s = Session::new();
|
||||
|
||||
Reference in new issue
Block a user