mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(type): use an area path's bounds only when they are finite
PSD export, Release Type from Path and the type transform box all took the area path's control_bounds() as given. A path from a file can hold NaN, infinite or huge coordinates, and f64::min skips a NaN, so those bounds could be NaN, infinite, or finite while the path itself is not; they then became a NaN BoxBounds in the PSD or a NaN text box. photocraft_text::area::path_bounds returns the bounds only when every point is finite and within the area module's 1e7 coordinate limit (the paths AreaShape accepts). PSD export then saves point text, release returns an error, and the transform box falls back to the layout bounds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d3da314b5f
commit
7040fdc233
5 files changed
+68
-4
No files matched your search
@@ -128,6 +128,25 @@ fn release_turns_area_type_into_box_text_over_the_path() {
|
||||
assert!(matches!(text(&s, id).shape, TextShape::Area { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_with_a_path_that_is_not_finite_is_an_error() {
|
||||
let mut s = session(8);
|
||||
let id = text_layer(&mut s, WORDS);
|
||||
// A file can carry any coordinates: one NaN or infinite point must not become a NaN text box.
|
||||
for bad in [f64::NAN, f64::INFINITY] {
|
||||
let path = Path::new(vec![Subpath::polygon(&[(0.0, 0.0), (100.0, bad), (100.0, 100.0)])]);
|
||||
s.edit("area", |doc, _| {
|
||||
if let Some(LayerContent::Text(t)) = doc.layer_mut(id).map(|l| &mut l.content) {
|
||||
t.shape = TextShape::Area { path };
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(s.execute("type.detachFromPath", json!({})).is_err(), "{bad}");
|
||||
assert!(matches!(text(&s, id).shape, TextShape::Area { .. }), "unchanged");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn area_type_saves_to_psd_as_paragraph_text_with_a_warning() {
|
||||
let mut s = session(8);
|
||||
|
||||
@@ -410,7 +410,8 @@ fn detach_from_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
// Area type keeps wrapping, in a text box over the path's bounds.
|
||||
TextShape::Area { path } => {
|
||||
let (x0, y0, x1, y1) = path.control_bounds().ok_or_else(|| EngineError::Other("the area type's path is empty".into()))?;
|
||||
let (x0, y0, x1, y1) = photocraft_text::area::path_bounds(path)
|
||||
.ok_or_else(|| EngineError::Other("the area type's path is empty or has coordinates that aren't finite".into()))?;
|
||||
t.shape = TextShape::Box { x: x0 as f32, y: y0 as f32, width: ((x1 - x0) as f32).max(1.0), height: ((y1 - y0) as f32).max(1.0) };
|
||||
}
|
||||
_ => return Err(EngineError::Other("the type layer is not on a path".into())),
|
||||
|
||||
@@ -291,6 +291,18 @@ impl AreaShape {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounds `(x0, y0, x1, y1)` of an area type path's anchors and control points, when every point
|
||||
/// is finite and within the area coordinate limit (`1e7`), the same paths [`AreaShape::new`]
|
||||
/// accepts. `None` for an empty path or one with a NaN, infinite or huge coordinate, so callers
|
||||
/// that turn the bounds into a text box or a PSD rectangle never write NaN.
|
||||
pub fn path_bounds(path: &Path) -> Option<(f64, f64, f64, f64)> {
|
||||
let ok = |p: Point| p.x.is_finite() && p.y.is_finite() && p.x.abs() <= MAX_COORD && p.y.abs() <= MAX_COORD;
|
||||
if !path.subpaths.iter().flat_map(|s| &s.knots).all(|k| ok(k.anchor) && ok(k.in_ctrl) && ok(k.out_ctrl)) {
|
||||
return None;
|
||||
}
|
||||
path.control_bounds()
|
||||
}
|
||||
|
||||
/// Boolean combination of two sorted, disjoint interval lists, in one linear merge.
|
||||
fn boolean(a: &[(f32, f32)], b: &[(f32, f32)], f: impl Fn(bool, bool) -> bool) -> Vec<(f32, f32)> {
|
||||
let ends = |v: &[(f32, f32)]| -> Vec<f32> { v.iter().flat_map(|&(l, r)| [l, r]).collect() };
|
||||
@@ -489,6 +501,17 @@ mod tests {
|
||||
assert!(AreaShape::new(&Path::new(over), false).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_bounds_need_finite_small_points() {
|
||||
assert_eq!(path_bounds(&poly(&[(1.0, 2.0), (30.0, 2.0), (30.0, 40.0)])), Some((1.0, 2.0, 30.0, 40.0)));
|
||||
assert_eq!(path_bounds(&Path::default()), None);
|
||||
for bad in [f64::NAN, f64::INFINITY, -f64::INFINITY, 2e7] {
|
||||
// Anywhere in the path, not just the first point (`f64::min` skips a NaN).
|
||||
assert_eq!(path_bounds(&poly(&[(0.0, 0.0), (10.0, bad), (10.0, 10.0)])), None, "{bad}");
|
||||
assert_eq!(path_bounds(&poly(&[(bad, 0.0), (10.0, 0.0), (10.0, 10.0)])), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_row_skips_the_narrow_top() {
|
||||
let a = AreaShape::new(&poly(&[(50.0, 0.0), (100.0, 100.0), (0.0, 100.0)]), false).unwrap();
|
||||
|
||||
+23
-2
@@ -1194,7 +1194,7 @@ pub fn build_engine_data(layer: &TextLayer, template: Option<E>, dpi: f32) -> E
|
||||
TextShape::Box { x, y, width, height } => (1, Some([*x, *y, x + width, y + height])),
|
||||
// PSD area type is not written yet: the layer saves as paragraph text over the path's
|
||||
// bounds, so Photoshop opens it with the same text wrapping in a rectangle.
|
||||
TextShape::Area { path } => match path.control_bounds() {
|
||||
TextShape::Area { path } => match crate::area::path_bounds(path) {
|
||||
Some((x0, y0, x1, y1)) => (1, Some([x0 as f32, y0 as f32, x1 as f32, y1 as f32])),
|
||||
None => (0, None),
|
||||
},
|
||||
@@ -1310,7 +1310,7 @@ pub fn build_tysh(layer: &TextLayer, dpi: f32, ink: Option<[f32; 4]>) -> Vec<u8>
|
||||
),
|
||||
);
|
||||
let area = match &layer.shape {
|
||||
TextShape::Area { path } => path.control_bounds().map(|(x0, y0, x1, y1)| [x0 as f32, y0 as f32, x1 as f32, y1 as f32]),
|
||||
TextShape::Area { path } => crate::area::path_bounds(path).map(|(x0, y0, x1, y1)| [x0 as f32, y0 as f32, x1 as f32, y1 as f32]),
|
||||
_ => None,
|
||||
};
|
||||
let [l, t, r, b] = match (&layer.shape, area, ink) {
|
||||
@@ -1373,6 +1373,27 @@ mod nonfinite_tests {
|
||||
assert_eq!(char_style(None, &d, &[], 1.0).size_pt, 24.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn area_path_that_is_not_finite_saves_as_point_text() {
|
||||
let path = photocraft_doc::vector::Path::new(vec![photocraft_doc::vector::Subpath::polygon(&[(0.0, 0.0), (f64::NAN, 0.0), (10.0, f64::INFINITY)])]);
|
||||
let layer = TextLayer {
|
||||
text: "x".into(),
|
||||
runs: vec![TextRun { len: 1, style: CharStyle { size_pt: 12.0, ..Default::default() } }],
|
||||
shape: TextShape::Area { path },
|
||||
..Default::default()
|
||||
};
|
||||
let out = String::from_utf8_lossy(&crate::engine_data::write(&build_engine_data(&layer, None, 72.0))).into_owned();
|
||||
assert!(out.contains("/PointBase") && !out.contains("/BoxBounds"), "{out}");
|
||||
assert!(!out.to_ascii_lowercase().contains("nan") && !out.to_ascii_lowercase().contains("inf"), "{out}");
|
||||
let t = parse_tysh(&build_tysh(&layer, 72.0, None)).unwrap();
|
||||
for key in ["bounds", "boundingBox"] {
|
||||
let Some(D::Descriptor(d)) = t.text.get(key) else { panic!("no {key}") };
|
||||
for side in ["Left", "Top ", "Rght", "Btom"] {
|
||||
assert!(matches!(d.get(side), Some(D::UnitFloat { value, .. }) if value.is_finite()), "{key} {side}: {:?}", d.get(side));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infinite_dpi_does_not_write_a_zero_size() {
|
||||
let layer =
|
||||
|
||||
@@ -93,7 +93,7 @@ pub(crate) fn frame(app: &mut PhotocraftApp) -> Option<TransformSession> {
|
||||
return None;
|
||||
}
|
||||
let area = match &shape {
|
||||
TextShape::Area { path } => path.control_bounds(),
|
||||
TextShape::Area { path } => photocraft_text::area::path_bounds(path),
|
||||
_ => None,
|
||||
};
|
||||
let rect = match (shape, area) {
|
||||
|
||||
Reference in new issue
Block a user