diff --git a/Cargo.lock b/Cargo.lock index 6b0dd869b..9343b5a7b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1576,9 +1576,9 @@ dependencies = [ [[package]] name = "craft-svg" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c#08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" +source = "git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3#3dff4793f1a5bd1f99a4fe3280d48f515158dff3" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3)", "flate2", "kurbo", "quick-xml 0.41.0", @@ -1770,7 +1770,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c#08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" +source = "git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3#3dff4793f1a5bd1f99a4fe3280d48f515158dff3" dependencies = [ "quick-xml 0.41.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 62060c612..3ddaaf277 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3b8e426a260695af840a2cd79a064d40778f939e" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" } -craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "08da5e1486ee640de1d2cccd8fe8dd32b1d3b62c" } +craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "3dff4793f1a5bd1f99a4fe3280d48f515158dff3" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", default-features = false, features = ["stl", "obj", "gltf", "fbx"] } diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 3d131b004..7f7c61f72 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -1,1598 +1,1331 @@ -//! 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 ` 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 `` 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, - /// 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)>, -} - -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, - pub fill_opacity: f64, - pub fill_rule: FillRule, - pub stroke: Option, - pub opacity: f64, -} - -#[derive(Clone, Debug, PartialEq)] -pub struct Group { - pub name: String, - pub transform: Affine, - pub opacity: f64, - pub children: Vec, - /// 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>, - /// 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>, - 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, -} - -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, - fill_opacity: f64, - fill_rule: FillRule, - stroke: Option, - stroke_opacity: f64, - stroke_width: f64, - cap: Cap, - join: Join, - miter: f64, - dash: Option>, - 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>, - /// The parsed `