From d06b3282415b54b79fa29e829411c0ea08c16a37 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sat, 21 Nov 2026 19:13:32 -0800 Subject: [PATCH 1/4] svg: cover zero-size basic-shape eligibility --- crates/svg/src/paths.rs | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/crates/svg/src/paths.rs b/crates/svg/src/paths.rs index 9bd450594c..21c17af911 100644 --- a/crates/svg/src/paths.rs +++ b/crates/svg/src/paths.rs @@ -1984,6 +1984,102 @@ mod tests { fails("", "invalid XML"); } + #[test] + fn zero_sized_basic_shapes_are_not_imported() { + // Original examples from SVG 1.1 basic-shape zero-size rules, sections 9.2-9.4. + // These shapes are not drawable; a line/path with a zero-area bbox is different. + let mut retained = Vec::new(); + for (label, shape) in [ + ("rect width=0", r#""#), + ("rect height=0", r#""#), + ("circle r=0", r#""#), + ("ellipse rx=0", r#""#), + ("ellipse ry=0", r#""#), + ("default-zero dimensions", r#""#), + ] { + let got = read(&format!(r#"{shape}"#)); + assert!(got.warnings.is_empty()); + if !got.shapes.is_empty() { + retained.push(label); + } + } + + // Keep ordinary positive shapes and exact-zero (not epsilon) eligibility. + let positive = read( + r#""#, + ); + assert_eq!(positive.shapes.len(), 3); + for (i, expected) in + [kurbo::Rect::new(1.0, 2.0, 5.0, 8.0), kurbo::Rect::new(8.0, 8.0, 12.0, 12.0), kurbo::Rect::new(7.0, 8.0, 13.0, 12.0)].iter().enumerate() + { + let actual = bounds(&positive, i); + assert!(near(actual.x0, expected.x0) && near(actual.y0, expected.y0)); + assert!(near(actual.x1, expected.x1) && near(actual.y1, expected.y1)); + } + let near_positive = read(r#""#); + assert_eq!(near_positive.shapes.len(), 3); + for shape in &near_positive.shapes { + let bbox = shape.path.bounding_box(); + assert!(bbox.width() > 0.0 && bbox.height() > 0.0); + } + let square = read(r#""#); + assert_eq!(square.shapes.len(), 1); + assert_eq!(square.shapes[0].path.elements().len(), 5); + assert!(!square.shapes[0].path.elements().iter().any(|e| matches!(e, PathEl::CurveTo(..)))); + + // A vertical/horizontal/point line and a raw path keep their exact geometry. + let lines = read( + r#""#, + ); + assert_eq!(lines.shapes.len(), 4); + for (shape, (from, to)) in lines.shapes.iter().zip([ + (Point::new(2.0, 1.0), Point::new(2.0, 9.0)), + (Point::new(1.0, 2.0), Point::new(9.0, 2.0)), + (Point::new(3.0, 4.0), Point::new(3.0, 4.0)), + (Point::new(5.0, 6.0), Point::new(5.0, 10.0)), + ]) { + assert_eq!(shape.path.elements(), &[PathEl::MoveTo(from), PathEl::LineTo(to)]); + } + + // Referenced geometry goes through the same eligibility rule as direct geometry. + let uses = read( + r##""##, + ); + if uses.shapes.iter().any(|shape| shape.label == r#""#) { + retained.push("use zero rect"); + } + let positive_use = uses.shapes.iter().find(|shape| shape.label == r#""#).unwrap(); + assert_eq!(positive_use.path.bounding_box(), kurbo::Rect::new(2.0, 3.0, 5.0, 7.0)); + + // Invalid ordinary attributes still fail even if another dimension is zero. + for (shape, message) in [ + (r#""#, "width/height cannot be negative"), + (r#""#, "corner radii cannot be negative"), + (r#""#, "invalid ry"), + (r#""#, "radius cannot be negative"), + (r#""#, "invalid cx"), + (r#""#, "radii cannot be negative"), + (r#""#, "invalid cy"), + ] { + let error = import::<()>(&format!("{shape}"), "zero.svg").unwrap_err(); + assert!(error.0.contains(message), "{}", error.0); + } + + let clipped = read( + r##""##, + ); + assert_eq!(clipped.shapes.len(), 1); + assert_eq!(clipped.clips.len(), 1); + assert!(clipped.warnings.is_empty()); + if !clipped.clips[0].pieces.is_empty() { + // Record the baseline defect without sending degenerate pieces to boolean clipping. + retained.push("clip pieces"); + } else { + assert!(clipped.clipped_path(&clipped.shapes[0]).unwrap().elements().is_empty()); + } + assert!(retained.is_empty(), "zero-sized basic shapes retained: {retained:?}"); + } + #[test] fn reads_all_common_basic_shapes() { let got = read( From e144926bc88f602a26d611ea1690bf3958166705 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sat, 21 Nov 2026 19:23:06 -0800 Subject: [PATCH 2/4] svg: omit zero-size basic shapes from path import --- crates/svg/src/paths.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/svg/src/paths.rs b/crates/svg/src/paths.rs index 21c17af911..9b8d0c9662 100644 --- a/crates/svg/src/paths.rs +++ b/crates/svg/src/paths.rs @@ -942,7 +942,7 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result