diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index d35873f884..6484abb919 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -2187,7 +2187,7 @@ pub(crate) fn picture_notices_by<'a>( MarkerRefusal::TooManyPixels, MarkerRefusal::TooMuchToDraw, MarkerRefusal::SvgTooNested, - MarkerRefusal::SvgFontRadius, + MarkerRefusal::SvgUnknownRadius, MarkerRefusal::SvgTooSmall, MarkerRefusal::SvgNotUtf8, MarkerRefusal::SvgUndrawn, @@ -2234,7 +2234,7 @@ pub(crate) fn picture_notices_by<'a>( ], )); } - let [not_images, too_long, too_large, too_much, too_nested, font_radius, too_small, not_utf8, filtered, past_run] = &bad; + let [not_images, too_long, too_large, too_much, too_nested, unknown_radius, too_small, not_utf8, filtered, past_run] = &bad; if !not_images.is_empty() { notices.push(NoticeText::format( "Image markers in {names} aren't PNG, JPEG or SVG images and are drawn as circles", @@ -2266,8 +2266,8 @@ pub(crate) fn picture_notices_by<'a>( "Image markers in {names} are SVGs with more than 40,000 XML nodes or nested deeper than 64 levels and are drawn as circles", ), ( - font_radius, - "Image markers in {names} are SVGs with a radius in em or ex, whose size RustGIS can't know, and are drawn as circles", + unknown_radius, + "Image markers in {names} are SVGs with a radius whose size RustGIS can't know (in em or ex, or in % of a size in em, ex or %), and are drawn as circles", ), (too_small, "Image markers in {names} are SVGs too small to draw and are drawn as circles"), (not_utf8, "Image markers in {names} are SVGs that aren't UTF-8 text and are drawn as circles"), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a6684e6da3..b7762c7aa1 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2692,8 +2692,8 @@ fn svg_image_markers_that_dont_draw_are_said_with_why() { "Image markers in \"Cities\", \"Rivers\" are SVGs with more than 40,000 XML nodes or nested deeper than 64 levels and are drawn as circles", ), ( - MarkerRefusal::SvgFontRadius, - "Image markers in \"Cities\", \"Rivers\" are SVGs with a radius in em or ex, whose size RustGIS can't know, and are drawn as circles", + MarkerRefusal::SvgUnknownRadius, + "Image markers in \"Cities\", \"Rivers\" are SVGs with a radius whose size RustGIS can't know (in em or ex, or in % of a size in em, ex or %), and are drawn as circles", ), (MarkerRefusal::SvgTooSmall, "Image markers in \"Cities\", \"Rivers\" are SVGs too small to draw and are drawn as circles"), (MarkerRefusal::SvgNotUtf8, "Image markers in \"Cities\", \"Rivers\" are SVGs that aren't UTF-8 text and are drawn as circles"), diff --git a/crates/render/src/picture.rs b/crates/render/src/picture.rs index 9d1cf2fc18..4bced884fc 100644 --- a/crates/render/src/picture.rs +++ b/crates/render/src/picture.rs @@ -84,8 +84,9 @@ pub enum MarkerRefusal { /// It is an SVG with more XML nodes than `svg::XML_NODES` (40,000) or nested deeper than /// `svg::DEPTH` (64). SvgTooNested, - /// It is an SVG with a radius in em or ex, a font's size, which may be any. - SvgFontRadius, + /// It is an SVG with a radius that may be any size: in em or ex, a font's size, or in % of a + /// size in em, ex or % (other than the whole SVG's in %). + SvgUnknownRadius, /// It is an SVG too small to scale up to a pixmap (its size near 0). SvgTooSmall, /// It is an SVG that isn't UTF-8 text. @@ -135,7 +136,7 @@ refusal_phrases! { TooManyPixels => "has more pixels than 2048 × 2048", TooMuchToDraw => "is an SVG with more to draw than a 2048 × 2048 image", SvgTooNested => "is an SVG with more than 40,000 XML nodes or nested deeper than 64 levels", - SvgFontRadius => "is an SVG with a radius in em or ex, whose size RustGIS can't know", + SvgUnknownRadius => "is an SVG with a radius whose size RustGIS can't know (in em or ex, or in % of a size in em, ex or %)", SvgTooSmall => "is an SVG too small to draw", SvgNotUtf8 => "is an SVG that isn't UTF-8 text", SvgUndrawn => "is an SVG with filters, text, images, animation or links to other files, which RustGIS doesn't draw in marker images", @@ -347,7 +348,7 @@ impl From for MarkerRefusal { svg::Refusal::NotSvg => MarkerRefusal::NotAnImage, svg::Refusal::TooMuch => MarkerRefusal::TooMuchToDraw, svg::Refusal::TooNested => MarkerRefusal::SvgTooNested, - svg::Refusal::FontRadius => MarkerRefusal::SvgFontRadius, + svg::Refusal::UnknownRadius => MarkerRefusal::SvgUnknownRadius, svg::Refusal::Undrawn => MarkerRefusal::SvgUndrawn, } } @@ -747,7 +748,7 @@ mod tests { TooManyPixels, TooMuchToDraw, SvgTooNested, - SvgFontRadius, + SvgUnknownRadius, SvgTooSmall, SvgNotUtf8, SvgUndrawn, @@ -814,7 +815,7 @@ mod tests { } /// Each refusal says its own cause (#2713): a long image, an SVG nested deep or with many - /// nodes, a radius in em or ex, one too small to draw, one that isn't UTF-8. + /// nodes, a radius whose size can't be known, one too small to draw, one that isn't UTF-8. #[test] fn an_image_is_refused_for_its_own_cause() { use MarkerRefusal::*; @@ -831,15 +832,30 @@ mod tests { (long_png, TooManyBytes), (svg(&deep), SvgTooNested), (svg(&nodes), SvgTooNested), - (svg(r#""#), SvgFontRadius), - (svg(r#""#), SvgFontRadius), + (svg(r#""#), SvgUnknownRadius), + (svg(r#""#), SvgUnknownRadius), + // A radius in % of a size in em or ex, or of a nested one in %. + ( + b64( + r#""#, + ), + SvgUnknownRadius, + ), + (svg(r#""#), SvgUnknownRadius), (b64(r#""#), SvgTooSmall), (not_utf8, SvgNotUtf8), ] { assert_eq!(marker_in(&mut Store::default(), &image).err(), Some(why), "{why:?}"); } // A radius of 0 em is 0, drawn; so is one in em where the shape takes no such radius. - for drawn in [r#""#, r#""#, r#""#] { + for drawn in [ + r#""#, + r#""#, + r#""#, + // A % radius of a size it knows, and a nested size in % with no % radius. + r#""#, + r#""#, + ] { assert!(marker_in(&mut Store::default(), &svg(drawn)).is_ok(), "{drawn}"); } } diff --git a/crates/render/src/picture/svg.rs b/crates/render/src/picture/svg.rs index 8a2da57b04..291517a82e 100644 --- a/crates/render/src/picture/svg.rs +++ b/crates/render/src/picture/svg.rs @@ -34,9 +34,9 @@ pub(super) enum Refusal { TooMuch, /// It has more XML nodes than [`XML_NODES`], or nests deeper than [`DEPTH`]. TooNested, - /// A shape's radius is in em or ex: font sizes, which no font is loaded to give, so it may be - /// any size. - FontRadius, + /// A shape's radius may be any size: in em or ex, font sizes, which no font is loaded to give, + /// or in % of a size that may be any ([`viewport`]). + UnknownRadius, /// It has filters, text, images, animation or links to other files, which aren't drawn (the /// module says why). Undrawn, @@ -90,8 +90,9 @@ pub(super) fn read(text: &str) -> Result { if doc.descendants().any(undrawn) { return Err(Refusal::Undrawn); } - if doc.descendants().any(font_radius) { - return Err(Refusal::FontRadius); + let sized = viewport(&doc).is_some(); + if doc.descendants().any(|n| unknown_radius(n, sized)) { + return Err(Refusal::UnknownRadius); } with_sheet(&doc, |sheet| { elements(&doc, sheet)?; @@ -105,23 +106,27 @@ pub(super) fn read(text: &str) -> Result { }; usvg::Tree::from_xmltree(&doc, &opt).map_err(|e| match e { usvg::Error::ElementsLimitReached => Refusal::TooMuch, + // usvg checks the depth of every node, a comment or a line break too, which the count + // doesn't walk. + usvg::Error::ParsingFailed(roxmltree::Error::NodesLimitReached) => Refusal::TooNested, _ => Refusal::NotSvg, }) } -/// Whether shape `n` has a radius in em or ex other than 0 ([`Refusal::FontRadius`]), of those -/// usvg reads for it: a circle's `r`, an ellipse's or a rectangle's `rx` and `ry`. -fn font_radius(n: roxmltree::Node) -> bool { - use svgtypes::LengthUnit::{Em, Ex}; +/// Whether shape `n` has a radius whose size can't be known ([`Refusal::UnknownRadius`]), of those +/// usvg reads for it (a circle's `r`, an ellipse's or a rectangle's `rx` and `ry`): one in em or +/// ex other than 0, a font's size, or one in % other than 0 when the size it is a part of isn't +/// known (`sized`, from [`viewport`]). +fn unknown_radius(n: roxmltree::Node, sized: bool) -> bool { + use svgtypes::LengthUnit::{Em, Ex, Percent}; let radii: &[&str] = match n.tag_name().name() { "circle" => &["r"], "ellipse" | "rect" => &["rx", "ry"], _ => return false, }; - radii - .iter() - .filter_map(|a| n.attribute(*a)) - .any(|v| v.trim().parse::().is_ok_and(|l| matches!(l.unit, Em | Ex) && l.number != 0.0)) + radii.iter().filter_map(|a| n.attribute(*a)).any(|v| { + v.trim().parse::().is_ok_and(|l| l.number != 0.0 && (matches!(l.unit, Em | Ex) || (l.unit == Percent && !sized))) + }) } /// Whether `n` is something that isn't drawn: text, an image, animation, or what an SVG takes from @@ -339,7 +344,14 @@ fn elements(doc: &roxmltree::Document, sheet: &simplecss::StyleSheet) -> Result< ids.entry(id).or_insert(n); } } - let mut counter = Counter { ids, seen: std::collections::HashMap::new(), viewport: viewport(doc), sheet, matched: 0, deep: false }; + let mut counter = Counter { + ids, + seen: std::collections::HashMap::new(), + viewport: viewport(doc).unwrap_or(f64::from(f32::MAX)), + sheet, + matched: 0, + deep: false, + }; let refused = |c: &Counter| if c.deep { Refusal::TooNested } else { Refusal::TooMuch }; let all = counter.count(doc.root_element(), 0).ok_or_else(|| refused(&counter))?; // A marker sits on the vertices (`marker-mid`) and both ends of the shapes that set it, by @@ -442,9 +454,10 @@ impl<'a> Counter<'a, '_> { /// The largest a viewport of `doc` may be, which a length in % is a part of: its view boxes and the /// sizes of its ``s, ``s and ``s, as usvg takes them for one (100 a side -/// without). The root's size in % is of 100 a side; any other in %, em or ex may be any size, the -/// largest an f32 holds. -fn viewport(doc: &roxmltree::Document) -> f64 { +/// without). The root's size in % is of 100 a side. `None` when one may be any size: the root's in +/// em or ex, or another's in %, em or ex. +fn viewport(doc: &roxmltree::Document) -> Option { + use svgtypes::LengthUnit::{Em, Ex, Percent}; let root = doc.root_element(); let mut largest = 100.0f64; for n in doc.descendants().filter(|n| n.is_element()) { @@ -452,13 +465,19 @@ fn viewport(doc: &roxmltree::Document) -> f64 { largest = largest.max(b.w.abs()).max(b.h.abs()); } if matches!(n.tag_name().name(), "svg" | "symbol" | "use") { - let of = if n == root { 100.0 } else { f64::from(f32::MAX) }; for side in ["width", "height"].iter().filter_map(|&a| n.attribute(a)) { - largest = largest.max(length(side, of)); + let unknown = side + .trim() + .parse::() + .is_ok_and(|l| l.number != 0.0 && (matches!(l.unit, Em | Ex) || (l.unit == Percent && n != root))); + if unknown { + return None; + } + largest = largest.max(length(side, 100.0)); } } } - largest + Some(largest) } /// The points shape element `n` comes to once read, both ends counted (a marker may sit on each): @@ -888,6 +907,14 @@ mod tests { assert_eq!(read(&svg(&format!(r#"{rects}{marked}"#))).err(), Some(Refusal::TooMuch)); let deep = format!("{}{}", "".repeat(DEPTH as usize + 1), "".repeat(DEPTH as usize + 1)); assert_eq!(doc(&deep), None); + // usvg checks the depth of every node, a comment or a line break too, which the count + // doesn't walk: past it is too nested, never "not an SVG". One level less draws. + let at = |levels: usize, inner: &str| read(&svg(&format!("{}{inner}{}", "".repeat(levels), "".repeat(levels)))); + let deepest = (1..=DEPTH as usize).rev().find(|&l| at(l, "").is_ok()).unwrap_or_default(); + for inner in ["", "\n"] { + assert_eq!(at(deepest, inner).err(), Some(Refusal::TooNested), "{inner:?}"); + assert!(at(deepest - 1, inner).is_ok(), "{inner:?}"); + } // A path of 20,000 segments draws; used ten times its numbers are counted ten times, past // what reading takes. let long = format!(r#""#, " L1 1".repeat(20_000)); @@ -922,7 +949,7 @@ mod tests { let text = svg(body); let doc = roxmltree::Document::parse(&text).unwrap(); let shape = doc.descendants().find(|n| ["path", "circle", "ellipse", "rect"].contains(&n.tag_name().name())).unwrap(); - (points_of(shape, viewport(&doc)), read(&text).map(|t| drawn(t.root()))) + (points_of(shape, viewport(&doc).unwrap_or(f64::from(f32::MAX))), read(&text).map(|t| drawn(t.root()))) }; for body in [ r#""#, @@ -946,10 +973,12 @@ mod tests { let drawn = drawn.unwrap_or_else(|why| panic!("{body}: {why:?}")); assert!(points >= drawn && points < POINTS, "{body}: {points} counted, {drawn} drawn"); } - // A % radius in a viewport whose size isn't known (a nested one in %) counts as the - // largest it may come to, as one in em or ex does. + // A % radius in a viewport whose size isn't known (a nested one in %, a root in em) is + // refused for it, as one in em or ex is (#2713). let nested = svg(r#""#); - assert_eq!(read(&nested).err(), Some(Refusal::TooMuch)); + assert_eq!(read(&nested).err(), Some(Refusal::UnknownRadius)); + let em_root = r#""#; + assert_eq!(read(em_root).err(), Some(Refusal::UnknownRadius)); // Against a nested viewport of its own size. let large = svg(r#""#); assert_eq!(read(&large).err(), Some(Refusal::TooMuch)); @@ -967,7 +996,7 @@ mod tests { } // A radius in em or ex, a font's size, is refused as such (#2713). let em = r#""#; - assert_eq!((counted(em).0, read(&svg(em)).err()), (u64::MAX, Some(Refusal::FontRadius))); + assert_eq!((counted(em).0, read(&svg(em)).err()), (u64::MAX, Some(Refusal::UnknownRadius))); } /// What isn't an SVG, or reads past its bounds, is refused while it is read: an entity loop, @@ -1166,7 +1195,7 @@ mod tests { let points = |tag: &str, list: &str| { let text = svg(&format!(r#"<{tag} points="{list}"/>"#)); let doc = roxmltree::Document::parse(&text).unwrap(); - points_of(doc.descendants().find(|n| n.has_tag_name(tag)).unwrap(), viewport(&doc)) + points_of(doc.descendants().find(|n| n.has_tag_name(tag)).unwrap(), viewport(&doc).unwrap_or(f64::from(f32::MAX))) }; assert_eq!(points("polyline", "12.345,67.891 10.5,20.25 30.125,40.5"), 3 + 2); assert_eq!(points("polygon", "1e5,2 -3.5-4 .5.5"), 3 + 2); diff --git a/crates/ui-egui/src/markerpick.rs b/crates/ui-egui/src/markerpick.rs index 788cc900ff..17ef0d3fd0 100644 --- a/crates/ui-egui/src/markerpick.rs +++ b/crates/ui-egui/src/markerpick.rs @@ -100,8 +100,8 @@ impl MarkerImage { "This image can't be a marker: it is an SVG with more than 40,000 XML nodes or nested deeper than 64 levels" ) .to_string(), - MarkerRefusal::SvgFontRadius => { - tl!("This image can't be a marker: it is an SVG with a radius in em or ex, whose size RustGIS can't know").to_string() + MarkerRefusal::SvgUnknownRadius => { + tl!("This image can't be a marker: it is an SVG with a radius whose size RustGIS can't know (in em or ex, or in % of a size in em, ex or %)").to_string() } MarkerRefusal::SvgTooSmall => tl!("This image can't be a marker: it is an SVG too small to draw").to_string(), MarkerRefusal::SvgNotUtf8 => tl!("This image can't be a marker: it is an SVG that isn't UTF-8 text").to_string(), diff --git a/docs/user-guide/cartography.md b/docs/user-guide/cartography.md index 0060749504..a12a4f3a9b 100644 --- a/docs/user-guide/cartography.md +++ b/docs/user-guide/cartography.md @@ -111,12 +111,13 @@ repeats, nor one with a marker on a shape inside another marker (drawn on each o wherever it is). An arc is counted as the curves it is drawn with, more the larger its radius: one of more than 200 curves isn't taken, nor is a circle, an ellipse or a rounded corner whose radius is in em or ex, which may come to any size. A radius in % is counted as that share of the -largest size the SVG gives itself, unless its own width or height is in em or ex, or a size -inside it is in %, em or ex. Each image is decoded once until RustGIS restarts, and kept -decoded, at most 700 pixels a side: in all at most -256 megapixels of different marker images and 128 MiB once decoded (as many images as it keeps, -at 177 × 177 each), so that a crafted file can't hold it for minutes; past it a new image is -refused, or drawn as a circle, with a notice saying so, until RustGIS is restarted. A vector +largest size the SVG gives itself; such a radius isn't taken either when the SVG's own width or +height is in em or ex, or when the width or height of an ``, `` or `` inside it +is in %, em or ex. Each image is decoded once until RustGIS restarts, +and kept decoded, at most 700 pixels a side: in all at most 256 megapixels of different marker +images and 128 MiB once decoded (as many images as it keeps, at 177 × 177 each), so that a +crafted file can't hold it for minutes; past it a new image is refused, or drawn as a circle, +with a notice saying so, until RustGIS is restarted. A vector export keeps a compressed copy of each image it draws, at most about as much again (some 256 MiB in all), which counts too and leaves less for new images. Its size is its width, and it is as tall as its proportions make it, as QGIS draws a raster image