mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #2996 from yoriswind/codex/2994-svg-zero-shapes
svg: omit zero-size basic shapes from path import
This commit is contained in:
1 file changed
+165
-7
+165
-7
@@ -684,12 +684,12 @@ fn import_node<'a, 'input, K: Kind>(
|
||||
}
|
||||
if !state.display_none
|
||||
&& !state.visibility_hidden
|
||||
&& let Some(path) = geometry_for(node, context.path_name)?
|
||||
&& let Some((path, drawable)) = geometry_for(node, context.path_name)?
|
||||
{
|
||||
context.charge_segments(&path)?;
|
||||
let transformed = transform_path(&path, state.transform);
|
||||
validate_import_path(&transformed, context.path_name)?;
|
||||
context.shapes.push(Shape {
|
||||
context.shapes.extend(drawable.then(|| Shape {
|
||||
path: transformed,
|
||||
kind: state.kind.clone(),
|
||||
label: label(node),
|
||||
@@ -699,7 +699,7 @@ fn import_node<'a, 'input, K: Kind>(
|
||||
fill_rule: if style.get("fill-rule").is_some_and(|value| value == "evenodd") { FillRule::EvenOdd } else { FillRule::NonZero },
|
||||
clips: state.clips.clone(),
|
||||
layer: state.layer.as_deref().map(str::to_string),
|
||||
});
|
||||
}));
|
||||
}
|
||||
for child in node.children().filter(|n| n.is_element()) {
|
||||
import_node(child, state.clone(), depth + 1, context)?;
|
||||
@@ -891,7 +891,7 @@ fn clip_region<K>(
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some(path) = geometry_for(child, context.path_name)? else {
|
||||
let Some((path, drawable)) = geometry_for(child, context.path_name)? else {
|
||||
continue;
|
||||
};
|
||||
let mut child_space = space;
|
||||
@@ -908,13 +908,13 @@ fn clip_region<K>(
|
||||
if context.clip_pieces > MAX_SVG_CLIP_PIECES {
|
||||
return Err(svg_error(context.path_name, "SVG clip paths exceed the 100,000-piece limit"));
|
||||
}
|
||||
pieces.push((path, rule));
|
||||
pieces.extend(drawable.then_some((path, rule)));
|
||||
}
|
||||
context.clips.push(Clip { pieces });
|
||||
Ok(Some(context.clips.len() - 1))
|
||||
}
|
||||
|
||||
fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option<BezPath>, Error> {
|
||||
fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option<(BezPath, bool)>, Error> {
|
||||
let tag = node.tag_name().name();
|
||||
let number = |name: &str, default: f64| -> Result<f64, Error> {
|
||||
match node.attribute(name) {
|
||||
@@ -922,6 +922,8 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option
|
||||
None => Ok(default),
|
||||
}
|
||||
};
|
||||
// Non-rendering basic shapes still carry their original geometry through validation.
|
||||
let mut drawable = true;
|
||||
let path = match tag {
|
||||
"svg" | "g" | "defs" | "symbol" | "clipPath" | "mask" | "pattern" | "title" | "desc" | "metadata" | "style" | "stop" | "namedview"
|
||||
| "guide" | "perspective" => return Ok(None),
|
||||
@@ -942,6 +944,7 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option
|
||||
if rx < 0.0 || ry < 0.0 {
|
||||
return Err(svg_error(path_name, "<rect> corner radii cannot be negative"));
|
||||
}
|
||||
drawable = w != 0.0 && h != 0.0;
|
||||
rounded_rect(x, y, w, h, rx.min(w / 2.0), ry.min(h / 2.0))
|
||||
}
|
||||
"circle" => {
|
||||
@@ -951,6 +954,7 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option
|
||||
if r < 0.0 {
|
||||
return Err(svg_error(path_name, "<circle> radius cannot be negative"));
|
||||
}
|
||||
drawable = r != 0.0;
|
||||
ellipse_path(cx, cy, r, r)
|
||||
}
|
||||
"ellipse" => {
|
||||
@@ -961,6 +965,7 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option
|
||||
if rx < 0.0 || ry < 0.0 {
|
||||
return Err(svg_error(path_name, "<ellipse> radii cannot be negative"));
|
||||
}
|
||||
drawable = rx != 0.0 && ry != 0.0;
|
||||
ellipse_path(cx, cy, rx, ry)
|
||||
}
|
||||
"line" => {
|
||||
@@ -981,7 +986,7 @@ fn geometry_for(node: roxmltree::Node<'_, '_>, path_name: &str) -> Result<Option
|
||||
return Err(svg_error(path_name, &format!("unsupported SVG element <{tag}>; import stopped to avoid silently dropping art")));
|
||||
}
|
||||
};
|
||||
Ok(Some(path))
|
||||
Ok(Some((path, drawable)))
|
||||
}
|
||||
|
||||
/// Path data with every smooth segment (`S`, `T`) that has no control point to reflect made to
|
||||
@@ -1967,6 +1972,159 @@ mod tests {
|
||||
(a - b).abs() < 1e-9
|
||||
}
|
||||
|
||||
#[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 width="0" height="8"/>"#),
|
||||
("rect height=0", r#"<rect width="8" height="0"/>"#),
|
||||
("circle r=0", r#"<circle cx="2" cy="3" r="0"/>"#),
|
||||
("ellipse rx=0", r#"<ellipse cx="2" cy="3" rx="0" ry="4"/>"#),
|
||||
("ellipse ry=0", r#"<ellipse cx="2" cy="3" rx="4" ry="0"/>"#),
|
||||
("default-zero dimensions", r#"<rect/><circle/><ellipse rx="4"/>"#),
|
||||
] {
|
||||
let got = read(&format!(r#"<svg viewBox="0 0 20 20">{shape}</svg>"#));
|
||||
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#"<svg viewBox="0 0 20 20"><rect x="1" y="2" width="4" height="6"/><circle cx="10" cy="10" r="2"/><ellipse cx="10" cy="10" rx="3" ry="2"/></svg>"#,
|
||||
);
|
||||
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#"<svg><rect width="0.000001" height="8"/><circle r="0.000001"/><ellipse rx="0.000001" ry="2"/></svg>"#);
|
||||
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#"<svg><rect width="8" height="6" rx="0" ry="2"/></svg>"#);
|
||||
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#"<svg><line x1="2" y1="1" x2="2" y2="9" stroke="black"/><line x1="1" y1="2" x2="9" y2="2" stroke="black"/><line x1="3" y1="4" x2="3" y2="4" stroke="black"/><path d="M5 6L5 10"/></svg>"#,
|
||||
);
|
||||
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##"<svg viewBox="0 0 20 20"><defs><rect id="zero" width="0" height="8"/><rect id="positive" width="3" height="4"/></defs><use href="#zero"/><use href="#positive" x="2" y="3"/></svg>"##,
|
||||
);
|
||||
if uses.shapes.iter().any(|shape| shape.label == r#"<rect id="zero">"#) {
|
||||
retained.push("use zero rect");
|
||||
}
|
||||
let positive_use = uses.shapes.iter().find(|shape| shape.label == r#"<rect id="positive">"#).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#"<rect width="0" height="-1"/>"#, "width/height cannot be negative"),
|
||||
(r#"<rect width="0" height="8" rx="-1"/>"#, "corner radii cannot be negative"),
|
||||
(r#"<rect width="0" height="8" ry="bad"/>"#, "invalid ry"),
|
||||
(r#"<circle r="-1"/>"#, "radius cannot be negative"),
|
||||
(r#"<circle cx="bad" r="0"/>"#, "invalid cx"),
|
||||
(r#"<ellipse rx="0" ry="-1"/>"#, "radii cannot be negative"),
|
||||
(r#"<ellipse cy="bad" rx="0" ry="2"/>"#, "invalid cy"),
|
||||
] {
|
||||
let error = import::<()>(&format!("<svg>{shape}</svg>"), "zero.svg").unwrap_err();
|
||||
assert!(error.0.contains(message), "{}", error.0);
|
||||
}
|
||||
|
||||
let clipped = read(
|
||||
r##"<svg viewBox="0 0 20 20"><defs><clipPath id="zero"><rect width="0" height="8"/><rect width="8" height="0"/><circle r="0"/><ellipse rx="0" ry="4"/><ellipse rx="4" ry="0"/></clipPath></defs><rect width="8" height="8" clip-path="url(#zero)"/></svg>"##,
|
||||
);
|
||||
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_zero_sized_geometry_validation();
|
||||
assert!(retained.is_empty(), "zero-sized basic shapes retained: {retained:?}");
|
||||
}
|
||||
|
||||
fn assert_zero_sized_geometry_validation() {
|
||||
// Omission must not bypass validation performed on the old transformed geometry.
|
||||
fn rejects_as(svg: &str, message: &str) -> bool {
|
||||
import::<()>(svg, "zero.svg").is_err_and(|error| error.0.starts_with("zero.svg: ") && error.0.contains(message))
|
||||
}
|
||||
let mut validation_bypassed = Vec::new();
|
||||
let coordinates = "geometry coordinates must be finite and within ±1e9 mm";
|
||||
for (label, svg, message) in [
|
||||
(
|
||||
"clip transform",
|
||||
r##"<svg><defs><clipPath id="c"><rect width="0" height="8" transform="scale(nope)"/></clipPath></defs><rect width="8" height="8" clip-path="url(#c)"/></svg>"##,
|
||||
"<rect> transform: transform has invalid numeric arguments",
|
||||
),
|
||||
("transformed coordinates", r#"<svg><circle cx="1e10" r="0"/></svg>"#, coordinates),
|
||||
("rect coordinates", r#"<svg><rect x="1e10" width="0" height="8"/></svg>"#, coordinates),
|
||||
("ellipse coordinates", r#"<svg><ellipse cy="-1e10" rx="4" ry="0"/></svg>"#, coordinates),
|
||||
("inherited coordinates", r#"<svg><g transform="translate(10000000000 0)"><rect width="0" height="8"/></g></svg>"#, coordinates),
|
||||
(
|
||||
"use coordinates",
|
||||
r##"<svg><defs><rect id="zero" width="0" height="8"/></defs><use href="#zero" transform="translate(10000000000 0)"/></svg>"##,
|
||||
coordinates,
|
||||
),
|
||||
(
|
||||
"clip coordinates",
|
||||
r##"<svg><defs><clipPath id="c"><circle cy="1e10" r="0"/></clipPath></defs><rect width="8" height="8" clip-path="url(#c)"/></svg>"##,
|
||||
coordinates,
|
||||
),
|
||||
(
|
||||
"clip validation precedence",
|
||||
r##"<svg><defs><clipPath id="c"><rect width="0" height="-1" transform="scale(nope)"/></clipPath></defs><rect width="8" height="8" clip-path="url(#c)"/></svg>"##,
|
||||
"width/height cannot be negative",
|
||||
),
|
||||
] {
|
||||
if !rejects_as(svg, message) {
|
||||
validation_bypassed.push(label);
|
||||
}
|
||||
}
|
||||
|
||||
// Raw coordinates outside the bound may become valid in document millimetres.
|
||||
// Keep a positive rectangle in each control so the actual transform is observable.
|
||||
for (transform, width, height, expected) in [
|
||||
("translate(-10000000000 0)", 4, 6, kurbo::Rect::new(0.0, 0.0, 4.0, 6.0)),
|
||||
("scale(0.0000000001)", 100_000_000, 600_000_000, kurbo::Rect::new(1.0, 0.0, 1.01, 0.06)),
|
||||
] {
|
||||
let got = read(&format!(
|
||||
r#"<svg><g transform="{transform}"><rect id="positive" x="10000000000" width="{width}" height="{height}"/><rect x="10000000000" width="0" height="8"/><circle cx="10000000000" r="0"/><ellipse cx="10000000000" rx="0" ry="2"/></g></svg>"#
|
||||
));
|
||||
assert!(got.warnings.is_empty());
|
||||
let positive = got.shapes.iter().find(|shape| shape.label == r#"<rect id="positive">"#).unwrap();
|
||||
let actual = positive.path.bounding_box();
|
||||
assert!(near(actual.x0, expected.x0) && near(actual.y0, expected.y0));
|
||||
assert!(near(actual.x1, expected.x1) && near(actual.y1, expected.y1));
|
||||
}
|
||||
assert!(validation_bypassed.is_empty(), "zero-sized shape validation bypassed: {validation_bypassed:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_all_common_basic_shapes() {
|
||||
let got = read(
|
||||
|
||||
Reference in new issue
Block a user