GitCorner

Code

ossartcrafteffectcraft🌐
665bfac0b339
//! SVG import (see README.md for the supported subset and the specifications used).
//!
//! [`parse`] resolves styles, `use` references and gradients into a small render tree
//! ([`Doc`]): groups with transforms and opacity, and shapes with fills and strokes in their own
//! user space. [`rasterize`] draws it at any scale; the engine converts it to shape layers.

#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]

mod bounds;
mod color;
mod pathdata;
mod raster;

use std::{collections::HashMap, sync::Arc};

pub use color::parse_color;
pub use kurbo::{Affine, BezPath};
pub use pathdata::parse_path_data;
pub use raster::{RasterError, blend_pixel, rasterize, rasterize_with, try_rasterize, try_rasterize_with};

#[derive(Debug, Clone, PartialEq)]
pub enum Error {
    Xml(String),
    NotSvg,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Xml(e) => write!(f, "invalid SVG: {e}"),
            Error::NotSvg => write!(f, "not an SVG document"),
        }
    }
}

impl std::error::Error for Error {}

/// Quick sniff: the first element is `<svg` (`craft_svg::sniff::is_svg`).
pub fn looks_like_svg(bytes: &[u8]) -> bool {
    craft_svg::sniff::is_svg(bytes)
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FillRule {
    #[default]
    NonZero,
    EvenOdd,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Cap {
    #[default]
    Butt,
    Round,
    Square,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Join {
    #[default]
    Miter,
    Round,
    Bevel,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Spread {
    #[default]
    Pad,
    Reflect,
    Repeat,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// How pattern tile coordinates are interpreted.
pub enum PatternUnits {
    /// Coordinates are fractions of the painted shape's bounding box.
    ObjectBoundingBox,
    /// Coordinates are expressed in the current user space.
    UserSpaceOnUse,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GradientKind {
    Linear { x1: f64, y1: f64, x2: f64, y2: f64 },
    Radial { cx: f64, cy: f64, r: f64, fx: f64, fy: f64 },
}

#[derive(Clone, Debug, PartialEq)]
pub struct Gradient {
    pub kind: GradientKind,
    /// (offset 0..1, straight RGBA 0..1), sorted.
    pub stops: Arc<[(f64, [f64; 4])]>,
    /// `gradientUnits="objectBoundingBox"` (the default): coordinates are fractions of the
    /// shape's bounding box.
    pub bbox_units: bool,
    pub transform: Affine,
    pub spread: Spread,
}

#[derive(Clone, Debug, PartialEq)]
/// A resolved `<pattern>` paint: tile geometry, coordinate units and shared content.
pub struct Pattern {
    /// Tile origin and size in `pattern_units` coordinates.
    pub x: f64,
    /// Tile origin y in `pattern_units` coordinates.
    pub y: f64,
    /// Tile width in `pattern_units` coordinates.
    pub width: f64,
    /// Tile height in `pattern_units` coordinates.
    pub height: f64,
    /// Coordinate system used for the tile origin and size.
    pub pattern_units: PatternUnits,
    /// Coordinate system used by the tile's child shapes.
    pub content_units: PatternUnits,
    /// Transform applied to the repeating tile.
    pub transform: Affine,
    /// Immutable tile content shared by every use of the resolved pattern.
    pub content: Arc<Group>,
    /// Conservative shape/path traversal cost per tile pixel, charged during rasterisation.
    pub render_complexity: usize,
    /// The fallback from `url(#id) fallback`, used if the target bbox or tile is invalid.
    pub fallback: Option<[f64; 4]>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Paint {
    Color([f64; 3]),
    Gradient(Gradient),
    Pattern(Pattern),
}

#[derive(Clone, Debug, PartialEq)]
pub struct Stroke {
    pub paint: Paint,
    pub opacity: f64,
    pub width: f64,
    pub cap: Cap,
    pub join: Join,
    pub miter: f64,
    /// Dash array and offset.
    pub dash: Option<(Vec<f64>, f64)>,
}

impl Stroke {
    fn style(&self) -> effectcraft_path::StrokeStyle {
        effectcraft_path::StrokeStyle {
            width: self.width,
            cap: match self.cap {
                Cap::Butt => effectcraft_path::Cap::Butt,
                Cap::Round => effectcraft_path::Cap::Round,
                Cap::Square => effectcraft_path::Cap::Square,
            },
            join: match self.join {
                Join::Miter => effectcraft_path::Join::Miter,
                Join::Round => effectcraft_path::Join::Round,
                Join::Bevel => effectcraft_path::Join::Bevel,
            },
            miter: self.miter,
            dash: self.dash.clone(),
            taper: None,
            wave: None,
        }
    }
}

/// Shape geometry in its user space (basic shapes are kept so they can become parametric shape
/// layer items).
#[derive(Clone, Debug, PartialEq)]
pub enum Geom {
    Rect { x: f64, y: f64, w: f64, h: f64, rx: f64, ry: f64 },
    Ellipse { cx: f64, cy: f64, rx: f64, ry: f64 },
    Path(BezPath),
}

impl Geom {
    pub fn to_path(&self) -> BezPath {
        use kurbo::Shape as _;
        match self {
            Geom::Rect { x, y, w, h, rx, ry } => {
                let r = kurbo::Rect::new(*x, *y, x + w, y + h);
                if *rx > 0.0 || *ry > 0.0 {
                    if (rx - ry).abs() < 1e-9 { kurbo::RoundedRect::from_rect(r, *rx).to_path(0.01) } else { elliptic_round_rect(*x, *y, *w, *h, *rx, *ry) }
                } else {
                    r.to_path(0.01)
                }
            }
            Geom::Ellipse { cx, cy, rx, ry } => kurbo::Ellipse::new((*cx, *cy), (*rx, *ry), 0.0).to_path(0.01),
            Geom::Path(p) => p.clone(),
        }
    }
}

fn elliptic_round_rect(x: f64, y: f64, w: f64, h: f64, rx: f64, ry: f64) -> BezPath {
    const K: f64 = 0.552_284_749_830_793_6;
    let (kx, ky) = (rx * K, ry * K);
    let mut p = BezPath::new();
    p.move_to((x + rx, y));
    p.line_to((x + w - rx, y));
    p.curve_to((x + w - rx + kx, y), (x + w, y + ry - ky), (x + w, y + ry));
    p.line_to((x + w, y + h - ry));
    p.curve_to((x + w, y + h - ry + ky), (x + w - rx + kx, y + h), (x + w - rx, y + h));
    p.line_to((x + rx, y + h));
    p.curve_to((x + rx - kx, y + h), (x, y + h - ry + ky), (x, y + h - ry));
    p.line_to((x, y + ry));
    p.curve_to((x, y + ry - ky), (x + rx - kx, y), (x + rx, y));
    p.close_path();
    p
}

#[derive(Clone, Debug, PartialEq)]
pub struct Shape {
    /// Element id, or the element name (`rect`, `path`…).
    pub name: String,
    pub geom: Geom,
    /// User space → parent space.
    pub transform: Affine,
    pub fill: Option<Paint>,
    pub fill_opacity: f64,
    pub fill_rule: FillRule,
    pub stroke: Option<Stroke>,
    pub opacity: f64,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Group {
    pub name: String,
    pub transform: Affine,
    pub opacity: f64,
    pub children: Vec<Node>,
    /// Clipping paths in the group's space (after `transform`); the children show only where
    /// every one of them covers (PDF / EPS clipping; SVG `clipPath` is not read yet).
    pub clip: Vec<(BezPath, FillRule)>,
    /// How the group composites onto what is below it (PDF blend modes; SVG `mix-blend-mode`
    /// is not read yet).
    pub blend: BlendMode,
    /// A soft mask (PDF `SMask` in the graphics state): the group shows where the mask's
    /// luminosity (or alpha) is high.
    pub mask: Option<Box<SoftMask>>,
    /// Composited on its own (a transparent backdrop, as SVG groups and PDF isolated
    /// transparency groups are) rather than over what is below it (PDF non-isolated groups and
    /// clipping groups: blend modes inside reach the backdrop). Matters only when the group is
    /// drawn offscreen (clip, mask, blend mode, opacity or knockout).
    pub isolated: bool,
    /// A PDF knockout group: each child composites with the group's initial backdrop instead
    /// of with the children below it.
    pub knockout: bool,
}

impl Group {
    /// An empty, opaque, unclipped group.
    pub fn new(name: &str) -> Group {
        Group {
            name: name.to_string(),
            transform: Affine::IDENTITY,
            opacity: 1.0,
            children: vec![],
            clip: vec![],
            blend: BlendMode::Normal,
            mask: None,
            isolated: true,
            knockout: false,
        }
    }
}

/// Separable and non-separable blend modes (ISO 32000-1 §11.3.5; the same formulas as the
/// W3C Compositing and Blending Level 1 specification).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BlendMode {
    #[default]
    Normal,
    Multiply,
    Screen,
    Overlay,
    Darken,
    Lighten,
    ColorDodge,
    ColorBurn,
    HardLight,
    SoftLight,
    Difference,
    Exclusion,
    Hue,
    Saturation,
    Color,
    Luminosity,
}

impl BlendMode {
    /// A PDF blend mode name (`/Multiply` …); unknown names are Normal.
    pub fn from_pdf_name(n: &str) -> BlendMode {
        use BlendMode::*;
        match n {
            "Multiply" => Multiply,
            "Screen" => Screen,
            "Overlay" => Overlay,
            "Darken" => Darken,
            "Lighten" => Lighten,
            "ColorDodge" => ColorDodge,
            "ColorBurn" => ColorBurn,
            "HardLight" => HardLight,
            "SoftLight" => SoftLight,
            "Difference" => Difference,
            "Exclusion" => Exclusion,
            "Hue" => Hue,
            "Saturation" => Saturation,
            "Color" => Color,
            "Luminosity" => Luminosity,
            _ => Normal,
        }
    }
}

/// A soft mask: `content` is drawn in the masked group's space; its luminosity (composited
/// over `backdrop`) or its alpha is the mask.
#[derive(Clone, Debug, PartialEq)]
pub struct SoftMask {
    pub content: Group,
    pub luminosity: bool,
    pub backdrop: [f64; 3],
}

/// A raster image: `width`×`height` straight RGBA8 pixels; `transform` maps pixel space (y
/// down, pixel `(0, 0)` at the top-left corner) to the parent's space.
#[derive(Clone, Debug, PartialEq)]
pub struct Image {
    pub name: String,
    pub transform: Affine,
    pub width: u32,
    pub height: u32,
    pub rgba: std::sync::Arc<Vec<u8>>,
    pub opacity: f64,
}

#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum Node {
    Group(Group),
    Shape(Shape),
    Image(Image),
}

/// A parsed SVG document. `root.transform` maps the viewBox to `width`×`height` pixels.
#[derive(Clone, Debug, PartialEq)]
pub struct Doc {
    pub width: f64,
    pub height: f64,
    pub root: Group,
    /// Elements that were skipped (text, image, filter…), by name.
    pub skipped: Vec<String>,
}

impl Doc {
    /// Pixel size (rounded up, at least 1×1).
    pub fn pixel_size(&self) -> (u32, u32) {
        (self.width.ceil().clamp(1.0, 30000.0) as u32, self.height.ceil().clamp(1.0, 30000.0) as u32)
    }
    /// Every shape with its accumulated transform (to document pixels) and opacity, in paint
    /// order.
    pub fn flatten(&self) -> Vec<(Affine, f64, &Shape)> {
        fn walk<'a>(g: &'a Group, m: Affine, o: f64, out: &mut Vec<(Affine, f64, &'a Shape)>) {
            let m = m * g.transform;
            let o = o * g.opacity;
            for c in &g.children {
                match c {
                    Node::Group(sub) => walk(sub, m, o, out),
                    Node::Shape(s) => out.push((m * s.transform, o * s.opacity, s)),
                    Node::Image(_) => {}
                }
            }
        }
        let mut out = vec![];
        walk(&self.root, Affine::IDENTITY, 1.0, &mut out);
        out
    }
}

// ---------------------------------------------------------------- styles

/// Inherited style state.
#[derive(Clone, Debug)]
struct Style {
    fill: Option<PaintRef>,
    fill_opacity: f64,
    fill_rule: FillRule,
    stroke: Option<PaintRef>,
    stroke_opacity: f64,
    stroke_width: f64,
    cap: Cap,
    join: Join,
    miter: f64,
    dash: Option<Vec<f64>>,
    dash_offset: f64,
    color: [f64; 4],
    visible: bool,
    font_size: f64,
}

impl Default for Style {
    fn default() -> Self {
        Style {
            fill: Some(PaintRef::Color([0.0, 0.0, 0.0, 1.0])),
            fill_opacity: 1.0,
            fill_rule: FillRule::NonZero,
            stroke: None,
            stroke_opacity: 1.0,
            stroke_width: 1.0,
            cap: Cap::Butt,
            join: Join::Miter,
            miter: 4.0,
            dash: None,
            dash_offset: 0.0,
            color: [0.0, 0.0, 0.0, 1.0],
            visible: true,
            font_size: 16.0,
        }
    }
}

#[derive(Clone, Debug)]
enum PaintRef {
    Color([f64; 4]),
    Url(String, Option<[f64; 4]>),
    Current,
}

#[cfg(test)]
use craft_svg::simple_css::{MAX_CSS_DECLARATIONS, MAX_CSS_SELECTORS};
use craft_svg::simple_css::{MAX_CSS_WORK, StyleSheet};

fn parse_css(text: &str, sheet: &mut StyleSheet) -> Result<(), Error> {
    craft_svg::simple_css::parse(text, sheet).map_err(|error| Error::Xml(error.to_string()))
}

const PROPS: &[&str] = &[
    "fill",
    "fill-opacity",
    "fill-rule",
    "stroke",
    "stroke-width",
    "stroke-opacity",
    "stroke-linecap",
    "stroke-linejoin",
    "stroke-miterlimit",
    "stroke-dasharray",
    "stroke-dashoffset",
    "opacity",
    "display",
    "visibility",
    "color",
    "font-size",
    "transform",
    "stop-color",
    "stop-opacity",
];

struct Parser<'a, 'i> {
    ids: HashMap<String, roxmltree::Node<'a, 'i>>,
    /// The parsed `<style>` sheets.
    css: StyleSheet,
    /// CSS matching work done so far; past [`MAX_CSS_WORK`] rules stop applying and the import errs.
    css_work: std::cell::Cell<usize>,
    vw: f64,
    vh: f64,
    skipped: Vec<String>,
    /// Elements converted so far, `<use>` copies included, and how many may be (#1236).
    work: usize,
    budget: usize,
    /// The `<use>` targets being expanded, outermost first: a reference back into one is a
    /// cycle and is skipped.
    using: Vec<roxmltree::NodeId>,
    /// Pattern contents may use only solid paints; nested paint servers invalidate that pattern.
    in_pattern_content: bool,
    /// Set once pattern content uses a paint server, invalidating that pattern.
    invalid_pattern_content: bool,
    /// Work revisited while resolving patterns, bounded across all references.
    pattern_work: usize,
    /// Maximum pattern resolution work allowed for this import.
    pattern_work_budget: usize,
    /// Set when pattern resolution work passed its budget.
    pattern_work_exceeded: bool,
    /// Path data bytes (weighted) parsed so far.
    path_data_work: usize,
    /// Path elements produced so far across all shapes.
    path_element_work: usize,
    /// Maximum weighted path data work allowed for this import.
    path_work_budget: usize,
    /// Set when path data or element work passed its budget.
    path_work_exceeded: bool,
    /// Parsed pattern content is viewport-dependent because lengths may be percentages; the cache is bounded.
    pattern_cache: HashMap<(roxmltree::NodeId, u64, u64), Option<Pattern>>,
    /// Resolved gradients share their stop arrays across repeated paint references.
    gradient_cache: HashMap<(roxmltree::NodeId, u64, u64), Gradient>,
    /// Gradient resolution work done so far.
    gradient_work: usize,
    /// Set when gradient work passed [`MAX_GRADIENT_WORK`].
    gradient_work_exceeded: bool,
    /// Path elements and gradient stops kept so far, `<use>` copies and repeated gradient
    /// references included; past [`MAX_RETAINED`] the import errs.
    retained: std::cell::Cell<usize>,
}

/// The most path elements and gradient stops an import keeps: a complex path reused many times,
/// or a gradient with many stops referenced by many shapes, is copied for each use.
const MAX_RETAINED: usize = 8_000_000;

fn is_svg_ns(n: &roxmltree::Node) -> bool {
    n.is_element()
}

impl<'a, 'i> Parser<'a, 'i> {
    /// The declared properties of an element: presentation attributes, then CSS rules (by
    /// specificity), then the `style` attribute.
    fn declared(&self, n: roxmltree::Node) -> HashMap<String, String> {
        let mut out = HashMap::new();
        for a in n.attributes() {
            if PROPS.contains(&a.name()) && a.name() != "transform" {
                out.insert(a.name().to_string(), a.value().to_string());
            }
        }
        self.apply_rules(n, &mut out);
        if let Some(st) = n.attribute("style") {
            for (k, v) in craft_svg::simple_css::declarations(st) {
                out.insert(k, v);
            }
        }
        out
    }

    /// Charge `n` kept path elements or gradient stops; false once [`MAX_RETAINED`] is passed.
    fn charge_retained(&self, n: usize) -> bool {
        let r = self.retained.get().saturating_add(n);
        self.retained.set(r);
        r <= MAX_RETAINED
    }

    /// Charge `n` units of CSS work; false once the budget is spent.
    fn charge_css(&self, n: usize) -> bool {
        let w = self.css_work.get().saturating_add(n);
        self.css_work.set(w);
        w <= MAX_CSS_WORK
    }

    /// Charge work while resolving pattern references. Attribute bytes and each traversed child
    /// both count so a cache miss cannot repeatedly scan an enormous descriptive-only pattern.
    fn charge_pattern_work(&mut self, work: usize) -> bool {
        self.pattern_work = self.pattern_work.saturating_add(work);
        if self.pattern_work > self.pattern_work_budget {
            self.pattern_work_exceeded = true;
            return false;
        }
        true
    }

    /// Charge gradient resolution work; false once the budget is exceeded.
    fn charge_gradient_work(&mut self, work: usize) -> bool {
        self.gradient_work = self.gradient_work.saturating_add(work);
        if self.gradient_work > MAX_GRADIENT_WORK {
            self.gradient_work_exceeded = true;
            return false;
        }
        true
    }

    /// Charge path data bytes against the path work budget; false once exceeded.
    fn charge_path_data(&mut self, bytes: usize) -> bool {
        self.path_data_work = self.path_data_work.saturating_add(bytes.saturating_mul(PATH_DATA_WORK_FACTOR));
        if self.path_data_work > self.path_work_budget {
            self.path_work_exceeded = true;
            return false;
        }
        true
    }

    /// Charge produced path elements against the per-import cap; false once exceeded.
    fn charge_path_elements(&mut self, elements: usize) -> bool {
        self.path_element_work = self.path_element_work.saturating_add(elements);
        if self.path_element_work > MAX_PATH_ELEMENTS_PER_IMPORT {
            self.path_work_exceeded = true;
            return false;
        }
        true
    }

    /// Charge a pattern node's attributes and traversal while resolving a pattern reference.
    fn charge_pattern_attributes(&mut self, n: roxmltree::Node) -> bool {
        let bytes = n.attributes().map(|attribute| attribute.name().len().saturating_add(attribute.value().len())).fold(0usize, usize::saturating_add);
        self.charge_pattern_work(bytes.saturating_add(1))
    }

    /// Apply the shared compatibility stylesheet rules under the document work budget.
    fn apply_rules(&self, n: roxmltree::Node, out: &mut HashMap<String, String>) {
        self.css.apply(n.tag_name().name(), n.attribute("id"), n.attribute("class").unwrap_or_default(), out, |work| self.charge_css(work));
    }

    fn length(&self, s: &str, axis: char, font: f64) -> Option<f64> {
        let s = s.trim();
        let num_end = s.find(|c: char| !(c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E'))).unwrap_or(s.len());
        // "e" also starts "em"/"ex": back off if the number does not parse.
        let (mut num, mut unit) = (&s[..num_end], &s[num_end..]);
        if num.parse::<f64>().is_err() && (num.ends_with('e') || num.ends_with('E')) {
            num = &s[..num_end - 1];
            unit = &s[num_end - 1..];
        }
        let v: f64 = num.parse().ok()?;
        let value = match unit.trim() {
            "" | "px" => v,
            "pt" => v * 4.0 / 3.0,
            "pc" => v * 16.0,
            "mm" => v * 96.0 / 25.4,
            "cm" => v * 96.0 / 2.54,
            "in" => v * 96.0,
            "em" => v * font,
            "ex" => v * font / 2.0,
            "%" => {
                let base = match axis {
                    'x' => self.vw,
                    'y' => self.vh,
                    _ => ((self.vw * self.vw + self.vh * self.vh) / 2.0).sqrt(),
                };
                v / 100.0 * base
            }
            _ => v,
        };
        value.is_finite().then_some(value)
    }

    fn attr_len(&self, n: roxmltree::Node, name: &str, axis: char, st: &Style) -> f64 {
        n.attribute(name).and_then(|v| self.length(v, axis, st.font_size)).unwrap_or(0.0)
    }

    fn paint_ref(&self, v: &str) -> Option<Option<PaintRef>> {
        let v = v.trim();
        if v == "none" {
            return Some(None);
        }
        if v == "currentColor" || v == "currentcolor" {
            return Some(Some(PaintRef::Current));
        }
        if let Some(rest) = v.strip_prefix("url(") {
            let (url, fallback) = rest.split_once(')').unwrap_or((rest, ""));
            let url = url.trim().trim_matches(['"', '\'']);
            let id = url.strip_prefix('#').filter(|id| !id.is_empty()).unwrap_or_default().to_string();
            let fb = fallback.trim();
            let fb = if fb.is_empty() || fb == "none" { None } else { parse_color(fb) };
            return Some(Some(PaintRef::Url(id, fb)));
        }
        if v == "inherit" {
            return None;
        }
        parse_color(v).map(|c| Some(PaintRef::Color(c)))
    }

    /// Apply an element's declared inherited properties to `st`. Returns (opacity, display).
    fn cascade(&self, n: roxmltree::Node, st: &mut Style) -> (f64, bool) {
        let d = self.declared(n);
        let num = |k: &str| {
            d.get(k)
                .and_then(|v| v.trim().trim_end_matches('%').parse::<f64>().ok().map(|x| if v.trim().ends_with('%') { x / 100.0 } else { x }))
                .filter(|x| x.is_finite())
        };
        if let Some(c) = d.get("color").and_then(|v| parse_color(v)) {
            st.color = c;
        }
        let font_size = d.get("font-size").and_then(|v| match v.trim().strip_suffix('%') {
            Some(p) => p.trim().parse::<f64>().ok().map(|p| p / 100.0 * st.font_size),
            None => self.length(v, 'o', st.font_size),
        });
        if let Some(v) = font_size.filter(|v| v.is_finite()) {
            st.font_size = v;
        }
        if let Some(p) = d.get("fill").and_then(|v| self.paint_ref(v)) {
            st.fill = p;
        }
        if let Some(p) = d.get("stroke").and_then(|v| self.paint_ref(v)) {
            st.stroke = p;
        }
        if let Some(v) = num("fill-opacity") {
            st.fill_opacity = v.clamp(0.0, 1.0);
        }
        if let Some(v) = num("stroke-opacity") {
            st.stroke_opacity = v.clamp(0.0, 1.0);
        }
        if let Some(v) = d.get("stroke-width").and_then(|v| self.length(v, 'o', st.font_size)) {
            st.stroke_width = v.max(0.0);
        }
        if let Some(v) = num("stroke-miterlimit") {
            st.miter = v.max(1.0);
        }
        if let Some(v) = d.get("fill-rule") {
            st.fill_rule = if v.trim() == "evenodd" { FillRule::EvenOdd } else { FillRule::NonZero };
        }
        if let Some(v) = d.get("stroke-linecap") {
            st.cap = match v.trim() {
                "round" => Cap::Round,
                "square" => Cap::Square,
                _ => Cap::Butt,
            };
        }
        if let Some(v) = d.get("stroke-linejoin") {
            st.join = match v.trim() {
                "round" => Join::Round,
                "bevel" => Join::Bevel,
                _ => Join::Miter,
            };
        }
        if let Some(v) = d.get("stroke-dasharray") {
            st.dash = if v.trim() == "none" {
                None
            } else {
                let a: Vec<f64> =
                    v.split(|c: char| c == ',' || c.is_whitespace()).filter(|t| !t.is_empty()).filter_map(|t| self.length(t, 'o', st.font_size)).collect();
                if a.iter().all(|x| *x >= 0.0) && a.iter().any(|x| *x > 0.0) { Some(a) } else { None }
            };
        }
        if let Some(v) = d.get("stroke-dashoffset").and_then(|v| self.length(v, 'o', st.font_size)) {
            st.dash_offset = v;
        }
        if let Some(v) = d.get("visibility") {
            st.visible = !matches!(v.trim(), "hidden" | "collapse");
        }
        let display = d.get("display").is_none_or(|v| v.trim() != "none");
        (num("opacity").unwrap_or(1.0).clamp(0.0, 1.0), display)
    }

    /// Resolve a paint reference, retaining a per-reference fallback for patterns.
    fn resolve_paint(&mut self, p: &PaintRef, st: &Style) -> Option<(Paint, f64)> {
        if self.in_pattern_content && matches!(p, PaintRef::Url(..)) {
            self.invalid_pattern_content = true;
            return None;
        }
        match p {
            PaintRef::Color(c) => Some((Paint::Color([c[0], c[1], c[2]]), c[3])),
            PaintRef::Current => Some((Paint::Color([st.color[0], st.color[1], st.color[2]]), st.color[3])),
            PaintRef::Url(id, fallback) => match self.gradient(id, 0) {
                Some(g) if g.stops.len() == 1 => {
                    let (_, c) = g.stops.first()?;
                    Some((Paint::Color([c[0], c[1], c[2]]), c[3]))
                }
                Some(g) if !g.stops.is_empty() => Some((Paint::Gradient(g), 1.0)),
                Some(_) => None,
                None => match self.pattern(id) {
                    Some(mut pattern) => {
                        pattern.fallback = *fallback;
                        Some((Paint::Pattern(pattern), 1.0))
                    }
                    None => fallback.map(|c| (Paint::Color([c[0], c[1], c[2]]), c[3])),
                },
            },
        }
    }

    /// Read a local `href` or namespaced `xlink:href` value from an SVG element.
    fn href_value(n: roxmltree::Node<'a, 'i>) -> Option<String> {
        /// XML namespace of `xlink:href`.
        const XLINK: &str = "http://www.w3.org/1999/xlink";
        let href = n
            .attributes()
            .find(|a| a.name() == "href" && a.namespace().is_none())
            .or_else(|| n.attributes().find(|a| a.name() == "href" && a.namespace() == Some(XLINK)))?;
        Some(href.value().to_string())
    }

    fn href(&self, n: roxmltree::Node<'a, 'i>) -> Option<roxmltree::Node<'a, 'i>> {
        let h = Self::href_value(n)?;
        let id = h.trim().strip_prefix('#')?;
        if id.is_empty() {
            return None;
        }
        self.ids.get(id).copied()
    }

    /// A gradient by id, with a viewport-specific cache for its resolved attributes and stops.
    fn gradient(&mut self, id: &str, depth: usize) -> Option<Gradient> {
        let node = *self.ids.get(id)?;
        if !matches!(node.tag_name().name(), "linearGradient" | "radialGradient") || depth > 16 {
            return None;
        }
        let key = (node.id(), self.vw.to_bits(), self.vh.to_bits());
        if let Some(gradient) = self.gradient_cache.get(&key) {
            return Some(gradient.clone());
        }
        let gradient = self.parse_gradient(id, depth)?;
        if self.gradient_cache.len() < MAX_GRADIENT_CACHE_ENTRIES {
            self.gradient_cache.insert(key, gradient.clone());
        }
        Some(gradient)
    }

    /// Parse a gradient by id, charging reference-chain and stop work before retaining the result.
    fn parse_gradient(&mut self, id: &str, depth: usize) -> Option<Gradient> {
        let n = *self.ids.get(id)?;
        let tag = n.tag_name().name();
        if !matches!(tag, "linearGradient" | "radialGradient") || depth > 16 {
            return None;
        }
        // Chain: this element, then what it references.
        let mut chain = vec![n];
        let mut cur = n;
        if !self.charge_gradient_work(cur.attributes().map(|a| a.name().len().saturating_add(a.value().len())).fold(1usize, usize::saturating_add)) {
            return None;
        }
        while let Some(next) = self.href(cur) {
            if chain.len() > 16 || chain.contains(&next) {
                break;
            }
            chain.push(next);
            cur = next;
            if !self.charge_gradient_work(cur.attributes().map(|a| a.name().len().saturating_add(a.value().len())).fold(1usize, usize::saturating_add)) {
                return None;
            }
        }
        let attr = |name: &str| chain.iter().find_map(|e| e.attribute(name));
        let bbox_units = attr("gradientUnits") != Some("userSpaceOnUse");
        let st = Style::default();
        let len = |name: &str, axis: char, def: &str| -> f64 {
            let v = attr(name).unwrap_or(def);
            let value = if bbox_units {
                let v = v.trim();
                if let Some(p) = v.strip_suffix('%') { p.parse::<f64>().unwrap_or(0.0) / 100.0 } else { v.parse::<f64>().unwrap_or(0.0) }
            } else {
                self.length(v, axis, st.font_size).unwrap_or(0.0)
            };
            if value.is_finite() { value } else { 0.0 }
        };
        let kind = if tag == "linearGradient" {
            GradientKind::Linear { x1: len("x1", 'x', "0%"), y1: len("y1", 'y', "0%"), x2: len("x2", 'x', "100%"), y2: len("y2", 'y', "0%") }
        } else {
            let cx = len("cx", 'x', "50%");
            let cy = len("cy", 'y', "50%");
            let r = len("r", 'o', "50%");
            let fx = if attr("fx").is_some() { len("fx", 'x', "50%") } else { cx };
            let fy = if attr("fy").is_some() { len("fy", 'y', "50%") } else { cy };
            GradientKind::Radial { cx, cy, r, fx, fy }
        };
        let transform = attr("gradientTransform").map(parse_transform).unwrap_or(Affine::IDENTITY);
        let spread = match attr("spreadMethod") {
            Some("reflect") => Spread::Reflect,
            Some("repeat") => Spread::Repeat,
            _ => Spread::Pad,
        };
        // Stops: from the first element in the chain that has any.
        let mut stops = vec![];
        let mut source = None;
        'source: for candidate in &chain {
            for child in candidate.children().filter(|child| child.is_element()) {
                if !self.charge_gradient_work(1) {
                    return None;
                }
                if child.tag_name().name() == "stop" {
                    source = Some(*candidate);
                    break 'source;
                }
            }
        }
        if let Some(src) = source {
            // Charged before the stops are read: every reference reads and keeps them again.
            if !self.charge_retained(src.children().filter(|c| c.is_element()).count()) {
                return None;
            }
            let mut last = 0.0f64;
            for s in src.children().filter(|c| c.is_element() && c.tag_name().name() == "stop") {
                let stop_work = s.attributes().map(|a| a.name().len().saturating_add(a.value().len())).fold(1usize, usize::saturating_add);
                if !self.charge_gradient_work(stop_work) {
                    return None;
                }
                let d = self.declared(s);
                let off = s
                    .attribute("offset")
                    .map(|o| {
                        let o = o.trim();
                        if let Some(p) = o.strip_suffix('%') { p.parse::<f64>().unwrap_or(0.0) / 100.0 } else { o.parse::<f64>().unwrap_or(0.0) }
                    })
                    .unwrap_or(0.0)
                    .clamp(0.0, 1.0)
                    .max(last);
                last = off;
                let c = d
                    .get("stop-color")
                    .and_then(|c| if c.trim() == "currentColor" { d.get("color").and_then(|x| parse_color(x)) } else { parse_color(c) })
                    .unwrap_or([0.0, 0.0, 0.0, 1.0]);
                let o = d.get("stop-opacity").and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite()).unwrap_or(1.0).clamp(0.0, 1.0);
                stops.push((off, [c[0], c[1], c[2], c[3] * o]));
            }
        }
        Some(Gradient { kind, stops: Arc::from(stops.into_boxed_slice()), bbox_units, transform, spread })
    }

    /// Parse a pattern length using the coordinate system selected by its units attribute.
    fn pattern_length(&self, value: &str, axis: char, font: f64, units: PatternUnits) -> Option<f64> {
        let value = value.trim();
        let length = match units {
            PatternUnits::ObjectBoundingBox => match value.strip_suffix('%') {
                Some(percent) => percent.trim().parse::<f64>().ok()? / 100.0,
                None => value.parse::<f64>().ok()?,
            },
            PatternUnits::UserSpaceOnUse => {
                let number_end = value.find(|c: char| !(c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E'))).unwrap_or(value.len());
                let (mut number, mut unit) = (&value[..number_end], &value[number_end..]);
                // The length scanner admits `e` for exponents; back it off when it starts `em` or
                // `ex`, or follows a complete exponent (for example `1e2em`).
                if number.parse::<f64>().is_err() && (number.ends_with('e') || number.ends_with('E')) {
                    number = &number[..number.len().saturating_sub(1)];
                    unit = &value[number_end.saturating_sub(1)..];
                }
                number.parse::<f64>().ok()?;
                let unit = unit.trim();
                if !matches!(unit, "" | "px" | "pt" | "pc" | "mm" | "cm" | "in" | "em" | "ex" | "%") {
                    return None;
                }
                self.length(value, axis, font)?
            }
        };
        length.is_finite().then_some(length)
    }

    /// Resolve a local static pattern. The nearest chain member supplies each attribute;
    /// child content comes from the first member with non-descriptive element children.
    fn pattern(&mut self, id: &str) -> Option<Pattern> {
        let target = *self.ids.get(id)?;
        if target.tag_name().name() != "pattern" {
            return None;
        }
        let key = (target.id(), self.vw.to_bits(), self.vh.to_bits());
        if let Some(pattern) = self.pattern_cache.get(&key) {
            return pattern.clone();
        }
        let pattern = self.parse_pattern(id);
        if pattern.is_some() && self.pattern_cache.len() < MAX_PATTERN_CACHE_ENTRIES {
            self.pattern_cache.insert(key, pattern.clone());
        }
        pattern
    }

    /// Parse one pattern and account for all work done while resolving its inheritance chain.
    fn parse_pattern(&mut self, id: &str) -> Option<Pattern> {
        /// Longest `href` inheritance chain followed when resolving a pattern.
        const MAX_PATTERN_HREF_DEPTH: usize = 16;
        let first = *self.ids.get(id)?;
        if first.tag_name().name() != "pattern" {
            return None;
        }
        let mut chain = vec![first];
        loop {
            let current = *chain.last()?;
            if !self.charge_pattern_attributes(current) {
                return None;
            }
            let Some(href) = Self::href_value(current) else { break };
            if chain.len() >= MAX_PATTERN_HREF_DEPTH {
                return None;
            }
            let target_id = href.trim().strip_prefix('#')?;
            if target_id.is_empty() {
                return None;
            }
            let target = *self.ids.get(target_id)?;
            if target.tag_name().name() != "pattern" || chain.iter().any(|seen| seen.id() == target.id()) {
                return None;
            }
            chain.push(target);
        }
        // This implementation has no viewBox mapper for pattern contents; don't silently paint
        // them in the wrong coordinate system.
        if chain.iter().any(|node| node.attribute("viewBox").is_some()) {
            return None;
        }

        let attr = |name: &str| chain.iter().find_map(|node| node.attribute(name).map(str::to_string));
        let mut content_style = Style::default();
        let mut content_opacity = 1.0;
        let ancestors: Vec<_> = first.ancestors().filter(|node| node.is_element()).collect();
        for ancestor in ancestors.into_iter().rev() {
            if ancestor.id() != first.id() && !self.charge_pattern_attributes(ancestor) {
                return None;
            }
            let (opacity, _) = self.cascade(ancestor, &mut content_style);
            if ancestor.id() == first.id() {
                content_opacity = opacity;
            }
        }
        let font_size = content_style.font_size;
        let units = match attr("patternUnits").as_deref() {
            None | Some("objectBoundingBox") => PatternUnits::ObjectBoundingBox,
            Some("userSpaceOnUse") => PatternUnits::UserSpaceOnUse,
            Some(_) => return None,
        };
        let content_units = match attr("patternContentUnits").as_deref() {
            None | Some("userSpaceOnUse") => PatternUnits::UserSpaceOnUse,
            Some("objectBoundingBox") => PatternUnits::ObjectBoundingBox,
            Some(_) => return None,
        };
        let x = match attr("x").as_deref() {
            Some(value) => self.pattern_length(value, 'x', font_size, units)?,
            None => 0.0,
        };
        let y = match attr("y").as_deref() {
            Some(value) => self.pattern_length(value, 'y', font_size, units)?,
            None => 0.0,
        };
        let width = self.pattern_length(attr("width")?.as_str(), 'x', font_size, units)?;
        let height = self.pattern_length(attr("height")?.as_str(), 'y', font_size, units)?;
        if width <= 0.0 || height <= 0.0 {
            return None;
        }
        let transform = attr("patternTransform").as_deref().map(parse_transform).unwrap_or(Affine::IDENTITY);
        let coeffs = transform.as_coeffs();
        let determinant = coeffs[0] * coeffs[3] - coeffs[1] * coeffs[2];
        if !coeffs.iter().all(|v| v.is_finite())
            || !determinant.is_finite()
            || determinant == 0.0
            || !transform.inverse().as_coeffs().iter().all(|v| v.is_finite())
        {
            return None;
        }
        let (old_pattern_content, old_invalid) = (self.in_pattern_content, self.invalid_pattern_content);
        let (old_vw, old_vh) = (self.vw, self.vh);
        self.in_pattern_content = true;
        self.invalid_pattern_content = false;
        if content_units == PatternUnits::ObjectBoundingBox {
            self.vw = 1.0;
            self.vh = 1.0;
        }
        let source = chain.iter().copied().find(|node| {
            node.children().any(|child| {
                if !self.charge_pattern_work(1) {
                    // Stop the scan immediately; the exceeded flag below rejects this pattern.
                    return true;
                }
                child.is_element() && !matches!(child.tag_name().name(), "title" | "desc" | "metadata")
            })
        });
        if self.pattern_work_exceeded {
            return None;
        }
        // Content inherits from the element that actually owns it, which differs from `first`
        // when the content comes through an `href` chain.
        if let Some(source) = source.filter(|s| s.id() != first.id()) {
            let mut inherited = Style::default();
            let owners: Vec<_> = source.ancestors().filter(|node| node.is_element()).collect();
            for owner in owners.into_iter().rev() {
                if !self.charge_pattern_attributes(owner) {
                    self.in_pattern_content = old_pattern_content;
                    self.invalid_pattern_content = old_invalid;
                    self.vw = old_vw;
                    self.vh = old_vh;
                    return None;
                }
                self.cascade(owner, &mut inherited);
            }
            content_style = inherited;
        }
        let mut children = Vec::new();
        if let Some(source) = source {
            for child in source.children() {
                self.node(child, &content_style, &mut children, 1);
                if self.pattern_work_exceeded {
                    break;
                }
            }
        }
        let invalid = self.invalid_pattern_content;
        self.in_pattern_content = old_pattern_content;
        self.invalid_pattern_content = old_invalid;
        self.vw = old_vw;
        self.vh = old_vh;
        if invalid || self.pattern_work_exceeded {
            return None;
        }
        let render_complexity = pattern_render_complexity(&children);
        let content = Group {
            name: source.map_or_else(|| Self::element_name(first), Self::element_name),
            transform: Affine::IDENTITY,
            opacity: content_opacity,
            children,
            clip: vec![],
            blend: BlendMode::Normal,
            mask: None,
            isolated: true,
            knockout: false,
        };
        Some(Pattern { x, y, width, height, pattern_units: units, content_units, transform, content: Arc::new(content), render_complexity, fallback: None })
    }

    fn element_name(n: roxmltree::Node) -> String {
        n.attribute("id").map(str::to_string).unwrap_or_else(|| n.tag_name().name().to_string())
    }

    /// Convert an element (and its subtree) into nodes appended to `out`.
    fn node(&mut self, n: roxmltree::Node<'a, 'i>, parent: &Style, out: &mut Vec<Node>, depth: usize) {
        if depth > 64 || !is_svg_ns(&n) || self.work > self.budget {
            return;
        }
        if self.in_pattern_content && !self.charge_pattern_attributes(n) {
            return;
        }
        // Every element converted counts, each `<use>` copy again: a few references to groups
        // that reference each other several times can multiply into billions (#1236).
        self.work += 1;
        let tag = n.tag_name().name();
        if self.in_pattern_content
            && !matches!(tag, "g" | "rect" | "circle" | "ellipse" | "path" | "line" | "polyline" | "polygon" | "defs" | "title" | "desc" | "metadata" | "style")
        {
            self.invalid_pattern_content = true;
            return;
        }
        match tag {
            "defs" | "title" | "desc" | "metadata" | "style" | "linearGradient" | "radialGradient" | "symbol" | "clipPath" | "mask" | "pattern" | "marker"
            | "script" => return,
            "text" | "image" | "filter" | "foreignObject" | "switch" => {
                self.skipped.push(tag.to_string());
                return;
            }
            _ => {}
        }
        let mut st = parent.clone();
        let (opacity, display) = self.cascade(n, &mut st);
        if !display {
            return;
        }
        let mut transform = n.attribute("transform").map(parse_transform).unwrap_or(Affine::IDENTITY);
        match tag {
            "g" | "a" => {
                let mut children = vec![];
                for c in n.children() {
                    self.node(c, &st, &mut children, depth + 1);
                }
                out.push(Node::Group(Group {
                    name: Self::element_name(n),
                    transform,
                    opacity,
                    children,
                    clip: vec![],
                    blend: BlendMode::Normal,
                    mask: None,
                    isolated: true,
                    knockout: false,
                }));
            }
            "svg" => {
                // Nested viewport.
                let x = self.attr_len(n, "x", 'x', &st);
                let y = self.attr_len(n, "y", 'y', &st);
                let w = n.attribute("width").and_then(|v| self.length(v, 'x', st.font_size)).filter(|v| v.is_finite()).unwrap_or(self.vw);
                let h = n.attribute("height").and_then(|v| self.length(v, 'y', st.font_size)).filter(|v| v.is_finite()).unwrap_or(self.vh);
                let vb = viewbox_transform(n, w, h);
                let mut children = vec![];
                let saved = (self.vw, self.vh);
                // Percentages use this viewport's user space, even without a viewBox.
                let (vw, vh) = viewbox(n).map_or((w, h), |(_, vw, vh)| (vw, vh));
                self.vw = vw;
                self.vh = vh;
                for c in n.children() {
                    self.node(c, &st, &mut children, depth + 1);
                }
                (self.vw, self.vh) = saved;
                out.push(Node::Group(Group {
                    name: Self::element_name(n),
                    transform: Affine::translate((x, y)) * vb,
                    opacity,
                    children,
                    clip: vec![],
                    blend: BlendMode::Normal,
                    mask: None,
                    isolated: true,
                    knockout: false,
                }));
            }
            "use" => {
                let Some(target) = self.href(n) else { return };
                if target == n || n.ancestors().any(|a| a == target) || self.using.contains(&target.id()) {
                    return;
                }
                self.using.push(target.id());
                let x = self.attr_len(n, "x", 'x', &st);
                let y = self.attr_len(n, "y", 'y', &st);
                transform *= Affine::translate((x, y));
                let mut children = vec![];
                if target.tag_name().name() == "symbol" {
                    let w = n.attribute("width").and_then(|v| self.length(v, 'x', st.font_size)).unwrap_or(self.vw);
                    let h = n.attribute("height").and_then(|v| self.length(v, 'y', st.font_size)).unwrap_or(self.vh);
                    let vb = viewbox_transform(target, w, h);
                    let mut inner = vec![];
                    let mut sst = st.clone();
                    let (o, _) = self.cascade(target, &mut sst);
                    let saved_viewport = (self.vw, self.vh);
                    (self.vw, self.vh) = viewbox(target).map_or((w, h), |(_, vw, vh)| (vw, vh));
                    for c in target.children() {
                        self.node(c, &sst, &mut inner, depth + 1);
                    }
                    (self.vw, self.vh) = saved_viewport;
                    children.push(Node::Group(Group {
                        name: Self::element_name(target),
                        transform: vb,
                        opacity: o,
                        children: inner,
                        clip: vec![],
                        blend: BlendMode::Normal,
                        mask: None,
                        isolated: true,
                        knockout: false,
                    }));
                } else {
                    self.node(target, &st, &mut children, depth + 1);
                }
                self.using.pop();
                out.push(Node::Group(Group {
                    name: Self::element_name(n),
                    transform,
                    opacity,
                    children,
                    clip: vec![],
                    blend: BlendMode::Normal,
                    mask: None,
                    isolated: true,
                    knockout: false,
                }));
            }
            "path" | "rect" | "circle" | "ellipse" | "line" | "polyline" | "polygon" => {
                if !st.visible {
                    return;
                }
                let (path_data, points) = match tag {
                    "path" => {
                        let Some(data) = n.attribute("d") else { return };
                        if !self.charge_path_data(data.len()) {
                            return;
                        }
                        let (path, exceeded) = pathdata::parse_path_data_bounded(data, MAX_PATH_ELEMENTS_PER_IMPORT_ELEMENT);
                        if exceeded || !self.charge_path_elements(path.elements().len()) {
                            self.path_work_exceeded = true;
                            return;
                        }
                        (Some(path), None)
                    }
                    "polyline" | "polygon" => {
                        let Some(data) = n.attribute("points") else { return };
                        if !self.charge_path_data(data.len()) {
                            return;
                        }
                        let max_values = MAX_PATH_ELEMENTS_PER_IMPORT_ELEMENT.saturating_sub(1).saturating_mul(2);
                        let (points, exceeded) = pathdata::numbers_bounded(data, max_values);
                        if exceeded {
                            self.path_work_exceeded = true;
                            return;
                        }
                        let point_count = points.len() / 2;
                        let elements = point_count.saturating_add(usize::from(tag == "polygon"));
                        if points.len() >= 4 && !self.charge_path_elements(elements) {
                            return;
                        }
                        (None, Some(points))
                    }
                    _ => (None, None),
                };
                let Some(geom) = self.geometry(n, &st, path_data, points) else { return };
                if let Geom::Path(p) = &geom
                    && !self.charge_retained(p.elements().len())
                {
                    return;
                }
                let fill = if matches!(tag, "line") { None } else { st.fill.as_ref().and_then(|p| self.resolve_paint(p, &st)) };
                let (fill, fill_alpha) = match fill {
                    Some((p, a)) => (Some(p), a),
                    None => (None, 1.0),
                };
                let stroke = st.stroke.as_ref().and_then(|p| self.resolve_paint(p, &st)).filter(|_| st.stroke_width > 0.0).map(|(paint, a)| Stroke {
                    paint,
                    opacity: st.stroke_opacity * a,
                    width: st.stroke_width,
                    cap: st.cap,
                    join: st.join,
                    miter: st.miter,
                    dash: st.dash.clone().map(|d| (d, st.dash_offset)),
                });
                out.push(Node::Shape(Shape {
                    name: Self::element_name(n),
                    geom,
                    transform,
                    fill,
                    fill_opacity: st.fill_opacity * fill_alpha,
                    fill_rule: st.fill_rule,
                    stroke,
                    opacity,
                }));
            }
            _ => {
                // Unknown container-ish elements: render their children.
                if n.has_children() {
                    let mut children = vec![];
                    for c in n.children() {
                        self.node(c, &st, &mut children, depth + 1);
                    }
                    if !children.is_empty() {
                        out.push(Node::Group(Group {
                            name: Self::element_name(n),
                            transform,
                            opacity,
                            children,
                            clip: vec![],
                            blend: BlendMode::Normal,
                            mask: None,
                            isolated: true,
                            knockout: false,
                        }));
                    }
                }
            }
        }
    }

    /// Resolve an element's geometry from its attributes, or from pre-parsed path data / points.
    fn geometry(&self, n: roxmltree::Node, st: &Style, path_data: Option<BezPath>, points: Option<Vec<f64>>) -> Option<Geom> {
        let l = |name: &str, axis: char| self.attr_len(n, name, axis, st);
        Some(match n.tag_name().name() {
            "path" => {
                let p = path_data?;
                if p.elements().is_empty() {
                    return None;
                }
                Geom::Path(p)
            }
            "rect" => {
                let (w, h) = (l("width", 'x'), l("height", 'y'));
                if !(w > 0.0 && h > 0.0) {
                    return None;
                }
                let (x, y) = (l("x", 'x'), l("y", 'y'));
                if !(x + w).is_finite() || !(y + h).is_finite() {
                    return None;
                }
                let rx_a = n.attribute("rx").and_then(|v| self.length(v, 'x', st.font_size));
                let ry_a = n.attribute("ry").and_then(|v| self.length(v, 'y', st.font_size));
                let (rx, ry) = match (rx_a, ry_a) {
                    (Some(x), Some(y)) => (x, y),
                    (Some(x), None) => (x, x),
                    (None, Some(y)) => (y, y),
                    (None, None) => (0.0, 0.0),
                };
                Geom::Rect { x, y, w, h, rx: rx.clamp(0.0, w / 2.0), ry: ry.clamp(0.0, h / 2.0) }
            }
            "circle" => {
                let r = l("r", 'o');
                if !(r > 0.0 && r <= MAX_ELLIPSE_RADIUS) {
                    return None;
                }
                let (cx, cy) = (l("cx", 'x'), l("cy", 'y'));
                if ![cx - r, cx + r, cy - r, cy + r].iter().all(|value| value.is_finite()) {
                    return None;
                }
                Geom::Ellipse { cx, cy, rx: r, ry: r }
            }
            "ellipse" => {
                let (rx, ry) = (l("rx", 'x'), l("ry", 'y'));
                if !(rx > 0.0 && rx <= MAX_ELLIPSE_RADIUS && ry > 0.0 && ry <= MAX_ELLIPSE_RADIUS) {
                    return None;
                }
                let (cx, cy) = (l("cx", 'x'), l("cy", 'y'));
                if ![cx - rx, cx + rx, cy - ry, cy + ry].iter().all(|value| value.is_finite()) {
                    return None;
                }
                Geom::Ellipse { cx, cy, rx, ry }
            }
            "line" => {
                let mut p = BezPath::new();
                p.move_to((l("x1", 'x'), l("y1", 'y')));
                p.line_to((l("x2", 'x'), l("y2", 'y')));
                Geom::Path(p)
            }
            "polyline" | "polygon" => {
                let nums = points?;
                if nums.len() < 4 {
                    return None;
                }
                let mut p = BezPath::new();
                let first_pair = nums.get(..2)?;
                let (Some(&first_x), Some(&first_y)) = (first_pair.first(), first_pair.get(1)) else { return None };
                p.move_to((first_x, first_y));
                for [x, y] in nums.get(2..).unwrap_or_default().as_chunks::<2>().0 {
                    p.line_to((*x, *y));
                }
                if n.tag_name().name() == "polygon" {
                    p.close_path();
                }
                Geom::Path(p)
            }
            _ => return None,
        })
    }
}

/// Estimate tile raster work from group traversal and the shape/path operations it contains.
fn pattern_render_complexity(nodes: &[Node]) -> usize {
    nodes.iter().fold(0usize, |total, node| {
        let cost = match node {
            Node::Group(group) => 4usize.saturating_add(pattern_render_complexity(&group.children)),
            Node::Shape(shape) => {
                let segments = match &shape.geom {
                    Geom::Path(path) => path.elements().len().max(1),
                    Geom::Rect { .. } | Geom::Ellipse { .. } => 8,
                };
                let paints = usize::from(shape.fill.is_some()).saturating_add(usize::from(shape.stroke.is_some()));
                1usize.saturating_add(segments.saturating_mul(paints))
            }
            Node::Image(_) => 1,
        };
        total.saturating_add(cost)
    })
}

fn viewbox(n: roxmltree::Node) -> Option<([f64; 2], f64, f64)> {
    let (v, exceeded) = pathdata::numbers_bounded(n.attribute("viewBox")?, 5);
    if exceeded || !v.iter().all(|x| x.is_finite()) {
        return None;
    }
    let [x, y, width, height] = v.as_slice() else { return None };
    (*width > 0.0 && *height > 0.0).then_some(([*x, *y], *width, *height))
}

/// The viewBox → viewport transform (`preserveAspectRatio`, default `xMidYMid meet`).
fn viewbox_transform(n: roxmltree::Node, w: f64, h: f64) -> Affine {
    let Some(([x, y], vw, vh)) = viewbox(n) else { return Affine::IDENTITY };
    let par = n.attribute("preserveAspectRatio").unwrap_or("xMidYMid meet");
    let mut parts = par.split_whitespace();
    let mut align = parts.next().unwrap_or("xMidYMid");
    if align == "defer" {
        align = parts.next().unwrap_or("xMidYMid");
    }
    let slice = parts.next() == Some("slice");
    let (sx, sy) = (w / vw, h / vh);
    if align == "none" {
        return Affine::scale_non_uniform(sx, sy) * Affine::translate((-x, -y));
    }
    let s = if slice { sx.max(sy) } else { sx.min(sy) };
    let fx = if align.contains("xMid") {
        0.5
    } else if align.contains("xMax") {
        1.0
    } else {
        0.0
    };
    let fy = if align.contains("YMid") {
        0.5
    } else if align.contains("YMax") {
        1.0
    } else {
        0.0
    };
    let tx = (w - vw * s) * fx;
    let ty = (h - vh * s) * fy;
    Affine::translate((tx, ty)) * Affine::scale(s) * Affine::translate((-x, -y))
}

/// Parse a `transform` list.
pub use craft_svg::transform_compat::parse_transform;

/// The deepest element nesting accepted. The XML parser recurses per level and overflowed a
/// 2 MiB thread's stack (aborting the app) at a few thousand levels; real documents stay far
/// below this.
const MAX_XML_DEPTH: usize = 256;
/// The most elements an import converts, `<use>` copies included, unless the file itself has
/// more than half as many (#1236).
const MAX_EXPANDED_ELEMENTS: usize = 500_000;
/// Bound path geometry expanded from a single path or point-list element.
const MAX_PATH_ELEMENTS_PER_IMPORT_ELEMENT: usize = pathdata::MAX_PATH_ELEMENTS;
/// Bound the path geometry retained across all path and point-list elements, including `<use>` copies.
const MAX_PATH_ELEMENTS_PER_IMPORT: usize = 1_000_000;
/// Bound repeated gradient-chain and stop scanning across the import.
const MAX_GRADIENT_WORK: usize = 64 * 1024 * 1024;
/// Bound viewport-specific resolved gradients; stop data is shared between clones.
const MAX_GRADIENT_CACHE_ENTRIES: usize = 256;
/// Bound ellipse expansion before kurbo approximates it with tolerance-dependent path segments.
const MAX_ELLIPSE_RADIUS: f64 = 1.0e9;
/// Weight applied to each path data byte when charging path work.
const PATH_DATA_WORK_FACTOR: usize = 8;
/// Aggregate work that pattern references may revisit, including attributes and scanned children,
/// even when many shapes share one large pattern definition.
const MAX_PATTERN_WORK_BYTES: usize = 256 * 1024 * 1024;
/// Lower bound of the pattern work budget regardless of document size.
const MIN_PATTERN_WORK_BYTES: usize = 8 * 1024 * 1024;
/// Bound viewport-specific pattern results; uncached resolutions remain covered by the work budget.
const MAX_PATTERN_CACHE_ENTRIES: usize = 256;

/// Nodes a document may expand to: a few kilobytes of nested internal entities
/// (`<!ENTITY b "&a;&a;…">`) otherwise expand to millions and exhaust memory.
const MAX_XML_NODES: u32 = 4_000_000;

/// Parse an SVG document.
pub fn parse(bytes: &[u8]) -> Result<Doc, Error> {
    let text = String::from_utf8_lossy(bytes);
    let text = text.trim_start_matches('\u{feff}');
    if craft_svg::guard::deeper_than(text, MAX_XML_DEPTH) {
        return Err(Error::Xml(format!("elements nested more than {MAX_XML_DEPTH} levels deep")));
    }
    // DTDs stay allowed (Illustrator's SVGs declare entities), but expansion is bounded (#2129).
    let opts = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: MAX_XML_NODES, ..Default::default() };
    let xml = roxmltree::Document::parse_with_options(text, opts).map_err(|e| Error::Xml(e.to_string()))?;
    let root = xml.root_element();
    if root.tag_name().name() != "svg" {
        return Err(Error::NotSvg);
    }
    let mut ids = HashMap::new();
    let mut css = StyleSheet::default();
    for n in root.descendants().filter(|n| n.is_element()) {
        if let Some(id) = n.attribute("id") {
            ids.entry(id.to_string()).or_insert(n);
        }
        if n.tag_name().name() == "style" {
            let text: String = n.children().filter_map(|c| c.text()).collect();
            parse_css(&text, &mut css)?;
        }
    }
    css.index();
    let vb = viewbox(root);
    // Room for generous reuse, and always for every element of the file itself.
    let budget = MAX_EXPANDED_ELEMENTS.max(root.descendants().filter(|n| n.is_element()).count().saturating_mul(2));
    let pattern_work_budget = bytes.len().saturating_mul(8).clamp(MIN_PATTERN_WORK_BYTES, MAX_PATTERN_WORK_BYTES);
    let path_work_budget = pattern_work_budget;
    let mut p = Parser {
        ids,
        css,
        css_work: std::cell::Cell::new(0),
        vw: vb.map_or(300.0, |v| v.1),
        vh: vb.map_or(150.0, |v| v.2),
        skipped: vec![],
        work: 0,
        budget,
        using: vec![],
        in_pattern_content: false,
        invalid_pattern_content: false,
        pattern_work: 0,
        pattern_work_budget,
        pattern_work_exceeded: false,
        path_data_work: 0,
        path_element_work: 0,
        path_work_budget,
        path_work_exceeded: false,
        pattern_cache: HashMap::new(),
        gradient_cache: HashMap::new(),
        gradient_work: 0,
        gradient_work_exceeded: false,
        retained: std::cell::Cell::new(0),
    };
    let st = Style::default();
    let pct_or = |name: &str, axis: char, def: f64| -> f64 {
        match root.attribute(name) {
            Some(v) if v.trim().ends_with('%') => {
                let f = v.trim().trim_end_matches('%').parse::<f64>().unwrap_or(100.0) / 100.0;
                def * f
            }
            Some(v) => p.length(v, axis, st.font_size).filter(|x| *x > 0.0).unwrap_or(def),
            None => def,
        }
    };
    // Without width/height the viewBox size is used (or 300×150).
    let (dw, dh) = match vb {
        Some((_, w, h)) => {
            let w2 = pct_or("width", 'x', w);
            let h2 = match (root.attribute("width"), root.attribute("height")) {
                // Only width given: keep the aspect ratio.
                (Some(_), None) => w2 * h / w,
                _ => pct_or("height", 'y', h),
            };
            let w2 = if root.attribute("width").is_none() && root.attribute("height").is_some() { h2 * w / h } else { w2 };
            (w2, h2)
        }
        None => (pct_or("width", 'x', 300.0), pct_or("height", 'y', 150.0)),
    };
    // Percentages use the root viewport's user space, even without a viewBox. A size that is not
    // a finite positive number keeps the 300x150 default, so it cannot produce NaN geometry.
    let usable = |v: f64, d: f64| if v.is_finite() && v > 0.0 { v } else { d };
    (p.vw, p.vh) = vb.map_or((usable(dw, 300.0), usable(dh, 150.0)), |(_, w, h)| (w, h));
    let mut rst = st.clone();
    let (opacity, _) = p.cascade(root, &mut rst);
    let mut children = vec![];
    for c in root.children() {
        p.node(c, &rst, &mut children, 1);
    }
    if p.work > p.budget {
        // Never a silently partial drawing.
        return Err(Error::Xml(format!("its <use> references expand to more than {} elements", p.budget)));
    }
    if p.pattern_work_exceeded {
        return Err(Error::Xml(format!("resolving its <pattern> references scans more than {} work units", p.pattern_work_budget)));
    }
    if p.path_work_exceeded {
        return Err(Error::Xml("its SVG paths expand beyond the bounded path-data work budget".to_string()));
    }
    if p.gradient_work_exceeded {
        return Err(Error::Xml(format!("resolving its gradients exceeds the {MAX_GRADIENT_WORK}-unit work budget")));
    }
    if p.retained.get() > MAX_RETAINED {
        return Err(Error::Xml(format!("its paths and gradients, with their copies, hold more than {MAX_RETAINED} elements")));
    }
    if p.css_work.get() > MAX_CSS_WORK {
        // Never a silently unstyled drawing.
        return Err(Error::Xml(format!("matching its <style> rules takes more than {MAX_CSS_WORK} steps")));
    }
    let transform = viewbox_transform(root, dw, dh);
    Ok(Doc {
        width: dw,
        height: dh,
        root: Group { name: "svg".into(), transform, opacity, children, clip: vec![], blend: BlendMode::Normal, mask: None, isolated: true, knockout: false },
        skipped: std::mem::take(&mut p.skipped),
    })
}

#[cfg(test)]
mod tests;