From 5c69112684c1a8a70636b629d62b728d91b47b50 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 17:21:52 -0500 Subject: [PATCH] svg: cut over SVG import parsing to craft-svg (#2468) --- Cargo.lock | 255 ++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + crates/svg/Cargo.toml | 1 + crates/svg/src/import.rs | 26 ++-- 4 files changed, 266 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40f6ae7cc..f3f898177 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,6 +138,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloc-no-stdlib" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a" + +[[package]] +name = "alloc-stdlib" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1" +dependencies = [ + "alloc-no-stdlib", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -526,6 +541,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -611,6 +632,16 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "brotli-decompressor" +version = "6.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc031cd87c6529082b0b65c604e86af0ed7744841b17ccfe60b1e31fa606179e" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + [[package]] name = "bumpalo" version = "3.20.3" @@ -1030,6 +1061,14 @@ dependencies = [ "rustix 1.1.5", ] +[[package]] +name = "craft-dds" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "rayon", +] + [[package]] name = "craft-docpath" version = "0.1.0" @@ -1136,6 +1175,41 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "craft-dds", + "craft-jpeg", + "craft-metadata", + "craft-tiff", + "crc32fast", + "exr", + "flate2", + "half", + "hayro-jpeg2000", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-keymap" version = "0.1.0" @@ -1164,6 +1238,18 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-metadata" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "brotli-decompressor", + "chrono", + "craft-tiff", + "quick-xml 0.41.0", + "serde", +] + [[package]] name = "craft-penpot" version = "0.1.0" @@ -1209,6 +1295,33 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "craft-image-codecs", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "data-url", + "flate2", + "kurbo", + "quick-xml 0.41.0", + "roxmltree 0.21.2", + "thiserror 2.0.21", + "usvg", + "vectorcraft-geom", + "vectorcraft-pathops", +] + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1292,6 +1405,16 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1882,6 +2005,22 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fastrand" version = "2.5.0" @@ -2117,7 +2256,7 @@ version = "0.18.1" dependencies = [ "figcraft-doc", "figcraft-geom", - "linesweeper", + "linesweeper 0.4.0", ] [[package]] @@ -2199,7 +2338,8 @@ name = "figcraft-svg" version = "0.18.1" dependencies = [ "base64 0.22.1", - "craft-xml", + "craft-svg", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc)", "figcraft-color", "figcraft-doc", "figcraft-geom", @@ -2802,6 +2942,15 @@ dependencies = [ "foldhash 0.2.0", ] +[[package]] +name = "hayro-jpeg2000" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1" +dependencies = [ + "fearless_simd", +] + [[package]] name = "heck" version = "0.5.0" @@ -3080,6 +3229,7 @@ dependencies = [ "moxcms", "num-traits", "png", + "qoi", "tiff", "zune-core", "zune-jpeg", @@ -3293,6 +3443,12 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-encoder" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f" + [[package]] name = "js-sys" version = "0.3.106" @@ -3377,6 +3533,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + [[package]] name = "libc" version = "0.2.191" @@ -3430,6 +3592,19 @@ dependencies = [ "smallvec", ] +[[package]] +name = "linesweeper" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a25df1b0c5432893f41bba817150e323703ee5f6d974f3a6808ac7fd7f046d18" +dependencies = [ + "arrayvec", + "kurbo", + "polycool", + "rustc-hash 2.1.3", + "smallvec", +] + [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -3550,6 +3725,28 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + [[package]] name = "naga" version = "30.0.1" @@ -4403,6 +4600,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -5389,6 +5595,12 @@ dependencies = [ "libc", ] +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + [[package]] name = "tempfile" version = "3.27.0" @@ -5903,6 +6115,27 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vectorcraft-geom" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "vectorcraft-pathops" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo", + "linesweeper 0.5.0", + "thiserror 2.0.21", + "vectorcraft-geom", +] + [[package]] name = "vello_common" version = "0.2.0" @@ -7254,6 +7487,15 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.5.15" @@ -7263,6 +7505,15 @@ dependencies = [ "zune-core", ] +[[package]] +name = "zune-jpegxl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "5.15.1" diff --git a/Cargo.toml b/Cargo.toml index 481408604..76690bed1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ repository = "https://github.com/storytold/figcraft" [workspace.dependencies] craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" } +craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "010702ee9b030844c852939095b1da0d0656a890", default-features = false, features = ["tree"] } craft-vectordrawable = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5cd16f2b9ac2a84c4c0794e609914e2bbf848bd7" } # internal figcraft-geom = { path = "crates/geom" } diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 57f578249..0d8829e03 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -10,6 +10,7 @@ workspace = true [dependencies] craft-xml = { workspace = true } +craft-svg = { workspace = true } figcraft-geom = { workspace = true } figcraft-color = { workspace = true } figcraft-doc = { workspace = true } diff --git a/crates/svg/src/import.rs b/crates/svg/src/import.rs index fbdb9b2c1..f4f063b52 100644 --- a/crates/svg/src/import.rs +++ b/crates/svg/src/import.rs @@ -16,6 +16,8 @@ //! What can't be represented (filters other than shadows and blurs, rotated repeating patterns, //! nested clip paths) is left out and reported in the import notes. Linked files are never read: only data URIs. +use craft_svg::tree::{self, usvg}; + mod budget; mod css_color; mod fx; @@ -832,10 +834,10 @@ pub(crate) fn too_deep(data: &[u8]) -> bool { } } -fn options(data: &[u8], limits: budget::Limits) -> usvg::Options<'static> { +fn options(data: &[u8], limits: tree::Limits) -> usvg::Options<'static> { // Only data URIs, within budgets: an opened or pasted SVG never pulls other files off the disk // and never expands without limit. - let mut o = usvg::Options { image_href_resolver: budget::resolver(limits), ..Default::default() }; + let mut o = usvg::Options { image_href_resolver: tree::image_resolver(limits), ..Default::default() }; if data.windows(4).any(|w| w == b"text") { text::prepare(&mut o); } @@ -913,7 +915,7 @@ pub fn import_svg(doc: &mut Document, data: &[u8], parent: NodeId, x: f64, y: f6 /// [`import_svg`], with notes on what couldn't be represented. pub fn import_svg_with_notes(doc: &mut Document, data: &[u8], parent: NodeId, x: f64, y: f64, name: &str) -> Result { - import_within(doc, data, parent, x, y, name, budget::Limits::default()) + import_within(doc, data, parent, x, y, name, tree::Limits::default()) } fn import_within( @@ -923,22 +925,16 @@ fn import_within( x: f64, y: f64, name: &str, - limits: budget::Limits, + limits: tree::Limits, ) -> Result { // SVGZ is unpacked here, within a budget, so every check below sees the real document. - let data = budget::document(data, &limits).map_err(ImportError)?; - if too_wide(&data) { - return Err(ImportError(format!("an element has more than {MAX_ATTRIBUTES} attributes"))); - } - if too_deep(&data) { - return Err(ImportError(format!("elements nested more than {MAX_NESTING} deep"))); - } + let text = craft_svg::svgz::text(data, limits.bytes).map_err(|e| ImportError(e.to_string()))?; // usvg reads neither `color()` nor `color-mix()`: they are resolved first. - let (data, color_notes) = match css_color::resolve(&data, doc.color_profile) { - Some(resolved) => resolved, - None => (data, Vec::new()), + let (data, color_notes) = match css_color::resolve(text.as_bytes(), doc.color_profile) { + Some((resolved, notes)) => (resolved, notes), + None => (text.into_owned().into_bytes(), Vec::new()), }; - let tree = usvg::Tree::from_data(&data, &options(&data, limits)).map_err(|e| ImportError(e.to_string()))?; + let tree = tree::parse(&data, &limits, &options(&data, limits)).map_err(|e| ImportError(e.to_string()))?; let size = tree.size(); let frame = doc.create(parent, NodeKind::Frame, name, x, y, f64::from(size.width()), f64::from(size.height())).map_err(|e| ImportError(e.to_string()))?;