Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
34 changes: 25 additions & 9 deletions crates/render/src/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 %)",
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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",
Expand Down Expand Up @@ -347,7 +348,7 @@ impl From<svg::Refusal> 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,
}
}
Expand Down Expand Up @@ -747,7 +748,7 @@ mod tests {
TooManyPixels,
TooMuchToDraw,
SvgTooNested,
SvgFontRadius,
SvgUnknownRadius,
SvgTooSmall,
SvgNotUtf8,
SvgUndrawn,
Expand Down Expand Up @@ -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::*;
Expand All @@ -831,15 +832,30 @@ mod tests {
(long_png, TooManyBytes),
(svg(&deep), SvgTooNested),
(svg(&nodes), SvgTooNested),
(svg(r#"<circle r="1em"/>"#), SvgFontRadius),
(svg(r#"<rect width="9" height="9" rx="2ex"/>"#), SvgFontRadius),
(svg(r#"<circle r="1em"/>"#), SvgUnknownRadius),
(svg(r#"<rect width="9" height="9" rx="2ex"/>"#), SvgUnknownRadius),
// A radius in % of a size in em or ex, or of a nested one in %.
(
b64(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="2em" height="2em" viewBox="0 0 100 100"><circle cx="50" cy="50" r="10%"/></svg>"#,
),
SvgUnknownRadius,
),
(svg(r#"<svg width="50%" height="50%"><circle r="50%"/></svg>"#), SvgUnknownRadius),
(b64(r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1e-40 1e-40"/>"#), 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#"<circle r="0em"/>"#, r#"<circle r="5" rx="1em"/>"#, r#"<rect width="9" height="9" r="1em"/>"#] {
for drawn in [
r#"<circle r="0em"/>"#,
r#"<circle r="5" rx="1em"/>"#,
r#"<rect width="9" height="9" r="1em"/>"#,
// A % radius of a size it knows, and a nested size in % with no % radius.
r#"<circle r="10%"/>"#,
r#"<svg width="50%" height="50%"><circle r="5"/></svg>"#,
] {
assert!(marker_in(&mut Store::default(), &svg(drawn)).is_ok(), "{drawn}");
}
}
Expand Down
81 changes: 55 additions & 26 deletions crates/render/src/picture/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -90,8 +90,9 @@ pub(super) fn read(text: &str) -> Result<usvg::Tree, Refusal> {
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)?;
Expand All @@ -105,23 +106,27 @@ pub(super) fn read(text: &str) -> Result<usvg::Tree, Refusal> {
};
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::<svgtypes::Length>().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::<svgtypes::Length>().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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -442,23 +454,30 @@ 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 `<svg>`s, `<symbol>`s and `<use>`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<f64> {
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()) {
if let Some(b) = n.attribute("viewBox").and_then(|v| v.parse::<svgtypes::ViewBox>().ok()) {
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
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
.trim()
.parse::<svgtypes::Length>()
.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):
Expand Down Expand Up @@ -888,6 +907,14 @@ mod tests {
assert_eq!(read(&svg(&format!(r#"<marker id="m">{rects}</marker>{marked}"#))).err(), Some(Refusal::TooMuch));
let deep = format!("{}{}", "<g>".repeat(DEPTH as usize + 1), "</g>".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}{}", "<g>".repeat(levels), "</g>".repeat(levels))));
let deepest = (1..=DEPTH as usize).rev().find(|&l| at(l, "").is_ok()).unwrap_or_default();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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#"<defs><path id="p" d="M0 0{}"/></defs>"#, " L1 1".repeat(20_000));
Expand Down Expand Up @@ -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#"<path d="M0 0A1 1 0 1 1 1 1"/>"#,
Expand All @@ -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#"<svg width="50%" height="50%"><circle r="50%"/></svg>"#);
assert_eq!(read(&nested).err(), Some(Refusal::TooMuch));
assert_eq!(read(&nested).err(), Some(Refusal::UnknownRadius));
let em_root = r#"<svg xmlns="http://www.w3.org/2000/svg" width="2em" height="2em"><rect width="9" height="9" rx="10%"/></svg>"#;
assert_eq!(read(em_root).err(), Some(Refusal::UnknownRadius));
// Against a nested viewport of its own size.
let large = svg(r#"<svg viewBox="0 0 1e30 1e30"><circle r="50%"/></svg>"#);
assert_eq!(read(&large).err(), Some(Refusal::TooMuch));
Expand All @@ -967,7 +996,7 @@ mod tests {
}
// A radius in em or ex, a font's size, is refused as such (#2713).
let em = r#"<rect width="9" height="9" rx="2em"/>"#;
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,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/ui-egui/src/markerpick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
13 changes: 7 additions & 6 deletions docs/user-guide/cartography.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<svg>`, `<symbol>` or `<use>` 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
Expand Down
Loading