mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-20 22:40:59 +01:00
Merge main into feat/image-craft-texture
This commit is contained in:
commit
caf79e955f
4 files changed
+356
-22
No files matched your search
Generated
+230
-10
@@ -132,6 +132,21 @@ dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[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"
|
||||
@@ -225,6 +240,9 @@ name = "arrayvec"
|
||||
version = "0.7.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "as-raw-xcb-connection"
|
||||
@@ -521,6 +539,16 @@ dependencies = [
|
||||
"piper",
|
||||
]
|
||||
|
||||
[[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"
|
||||
@@ -867,6 +895,40 @@ dependencies = [
|
||||
"writeable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"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=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-keymap"
|
||||
version = "0.1.0"
|
||||
@@ -901,6 +963,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=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"brotli-decompressor",
|
||||
"chrono",
|
||||
"craft-tiff",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-mime"
|
||||
version = "0.1.0"
|
||||
@@ -922,6 +996,25 @@ dependencies = [
|
||||
"objc2-app-kit 0.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=57288edafdc45552e30481cc649021de446abfa0)",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"kurbo",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg",
|
||||
"roxmltree 0.21.2",
|
||||
"thiserror 2.0.21",
|
||||
"usvg",
|
||||
"vectorcraft-geom",
|
||||
"vectorcraft-pathops",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-texture"
|
||||
version = "0.1.0"
|
||||
@@ -941,6 +1034,15 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
@@ -1000,6 +1102,16 @@ dependencies = [
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=57288edafdc45552e30481cc649021de446abfa0#57288edafdc45552e30481cc649021de446abfa0"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
@@ -1561,6 +1673,7 @@ dependencies = [
|
||||
"miniz_oxide 0.8.9",
|
||||
"num-complex",
|
||||
"pulp",
|
||||
"rayon-core",
|
||||
"smallvec",
|
||||
"zune-inflate",
|
||||
]
|
||||
@@ -1664,6 +1777,17 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontdb"
|
||||
version = "0.24.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f"
|
||||
dependencies = [
|
||||
"log",
|
||||
"slotmap",
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foreign-types"
|
||||
version = "0.5.0"
|
||||
@@ -2120,6 +2244,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 = "hermit-abi"
|
||||
version = "0.5.3"
|
||||
@@ -2370,6 +2503,7 @@ dependencies = [
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png",
|
||||
"qoi",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
@@ -2387,9 +2521,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "imagesize"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c"
|
||||
checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
@@ -2539,6 +2673,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.107"
|
||||
@@ -2596,6 +2736,7 @@ dependencies = [
|
||||
"arrayvec",
|
||||
"euclid",
|
||||
"polycool",
|
||||
"serde",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
@@ -2651,6 +2792,19 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4a5ff6bcca6c4867b1c4fd4ef63e4db7436ef363e0ad7531d1558856bae64f4"
|
||||
|
||||
[[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"
|
||||
@@ -2789,9 +2943,10 @@ dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-mcmat",
|
||||
"craft-mime",
|
||||
"craft-svg",
|
||||
"craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=726ad0f8290bedb6ced60fa06200a6b574f9990d)",
|
||||
"craft-texture 0.1.0 (git+https://github.com/storytold/craft-libs?rev=92fb3fa8ac2981c4c31961cfce7f7fdfbb03540d)",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b6d9feed23ceae249b40593ed84a668343091627)",
|
||||
"dds",
|
||||
"flate2",
|
||||
"gltf",
|
||||
@@ -2799,7 +2954,6 @@ dependencies = [
|
||||
"materialcraft-core",
|
||||
"materialcraft-geom",
|
||||
"materialcraft-nodes",
|
||||
"resvg",
|
||||
"roxmltree 0.20.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -3836,6 +3990,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 = "2.0.1"
|
||||
@@ -4015,16 +4178,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "resvg"
|
||||
version = "0.47.0"
|
||||
version = "0.48.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be183ad6a216aa96f33e4c8033b0988b8b3ea6fd2359d19af5bac4643fd8e81"
|
||||
checksum = "67e3803f97b999e80cbf7c6ecdd07a8102204d92e1633cf48783720c521196bd"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"log",
|
||||
"pico-args",
|
||||
"rgb",
|
||||
"svgtypes",
|
||||
"tiny-skia",
|
||||
"usvg",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4846,6 +5013,12 @@ dependencies = [
|
||||
"zerovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee"
|
||||
|
||||
[[package]]
|
||||
name = "tobj"
|
||||
version = "4.0.5"
|
||||
@@ -5058,12 +5231,24 @@ version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-script"
|
||||
version = "0.5.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-vo"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.2.3"
|
||||
@@ -5113,13 +5298,14 @@ checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "usvg"
|
||||
version = "0.47.0"
|
||||
version = "0.48.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d46cf96c5f498d36b7a9693bc6a7075c0bb9303189d61b2249b0dc3d309c07de"
|
||||
checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"base64 0.23.1",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"fontdb",
|
||||
"harfrust",
|
||||
"imagesize",
|
||||
"kurbo",
|
||||
"log",
|
||||
@@ -5127,9 +5313,13 @@ dependencies = [
|
||||
"roxmltree 0.21.2",
|
||||
"simplecss",
|
||||
"siphasher",
|
||||
"skrifa",
|
||||
"strict-num",
|
||||
"svgtypes",
|
||||
"tiny-skia-path",
|
||||
"unicode-bidi",
|
||||
"unicode-script",
|
||||
"unicode-vo",
|
||||
"xmlwriter",
|
||||
]
|
||||
|
||||
@@ -5175,6 +5365,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",
|
||||
"thiserror 2.0.21",
|
||||
"vectorcraft-geom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vello_common"
|
||||
version = "0.2.0"
|
||||
@@ -6439,6 +6650,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"
|
||||
|
||||
+2
-1
@@ -45,7 +45,8 @@ craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae
|
||||
craft-texture-dds = { package = "craft-texture", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "726ad0f8290bedb6ced60fa06200a6b574f9990d", features = ["dds"] }
|
||||
# Shared XML escaping; SBS model and serialization policy stay app-local.
|
||||
craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b6d9feed23ceae249b40593ed84a668343091627" }
|
||||
resvg = { version = "0.47", default-features = false }
|
||||
# SVG: bounded parsing (usvg re-exported) and drawing (resvg) behind the shared checks.
|
||||
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "57288edafdc45552e30481cc649021de446abfa0", features = ["tree-render"] }
|
||||
# internal
|
||||
materialcraft-core = { path = "crates/core" }
|
||||
materialcraft-geom = { path = "crates/geom" }
|
||||
|
||||
@@ -27,7 +27,7 @@ zip = { workspace = true }
|
||||
roxmltree = { workspace = true }
|
||||
flate2 = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
resvg = { workspace = true }
|
||||
craft-svg = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
flate2 = { workspace = true }
|
||||
|
||||
+123
-10
@@ -1,23 +1,53 @@
|
||||
//! SVG resources, drawn to a linear-light image. Parsing and drawing go through the shared
|
||||
//! `craft-svg` (`tree` and `tree-render`): its preflight bounds the bytes (SVGZ included), entity
|
||||
//! expansion, attributes and nesting before usvg sees the document, its image resolver never
|
||||
//! reads linked files and bounds embedded images, and its draw check refuses a drawing whose
|
||||
//! layers, filters or patterns would allocate past the budget instead of aborting.
|
||||
|
||||
use crate::{FormatError, Result};
|
||||
use craft_svg::tree::{self, ImageResolver, Limits, draw, usvg};
|
||||
use craft_texture::{Image, Kind, MAX_SIDE, srgb_to_linear};
|
||||
use resvg::{tiny_skia, usvg};
|
||||
|
||||
/// Largest SVG file (unpacked when SVGZ).
|
||||
const MAX_BYTES: usize = 16 << 20;
|
||||
/// Nesting usvg may build for the document itself, references followed.
|
||||
const MAX_TREE_DEPTH: usize = 32;
|
||||
/// Nesting usvg may build in all, the document and the SVG images embedded in it (and in those)
|
||||
/// together. usvg recurses about 15 kB of stack per level, so 64 levels (about 1 MB) stay inside
|
||||
/// a 2 MB worker stack whatever the embedding.
|
||||
const MAX_TOTAL_DEPTH: usize = 64;
|
||||
/// Bytes the RGBA buffers of one drawing may hold at once: the 8192² target plus its layers.
|
||||
const DRAW_BUDGET: u64 = 1 << 30;
|
||||
|
||||
/// Limits for the document: its own nesting gets [`MAX_TREE_DEPTH`].
|
||||
fn limits() -> Limits {
|
||||
Limits { bytes: MAX_BYTES, depth: MAX_TREE_DEPTH, tree_depth: MAX_TREE_DEPTH, ..Limits::default() }
|
||||
}
|
||||
|
||||
/// The embedded-image resolver. It counts the levels of every embedded SVG on top of the
|
||||
/// document's, seeded with the most the document may use, so the total stays within
|
||||
/// [`MAX_TOTAL_DEPTH`] however deep the document or the embedding chain is.
|
||||
fn image_resolver() -> ImageResolver {
|
||||
let resolver = ImageResolver::new(Limits { tree_depth: MAX_TOTAL_DEPTH, depth: MAX_TOTAL_DEPTH, ..limits() });
|
||||
resolver.set_document_depth(MAX_TREE_DEPTH);
|
||||
resolver
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8]) -> Result<Image> {
|
||||
if bytes.len() > 16 << 20 {
|
||||
return Err(FormatError::Image("SVG exceeds the 16 MiB limit".into()));
|
||||
}
|
||||
|
||||
let mut options = usvg::Options::default();
|
||||
options.image_href_resolver.resolve_string = Box::new(|_, _| None);
|
||||
let tree = usvg::Tree::from_data(bytes, &options).map_err(|error| FormatError::Image(error.to_string()))?;
|
||||
let limits = limits();
|
||||
let resolver = image_resolver();
|
||||
let options = usvg::Options { image_href_resolver: resolver.resolver(), ..usvg::Options::default() };
|
||||
let svg = tree::text(bytes, &limits).map_err(svg_error)?;
|
||||
resolver.check_document(&svg).map_err(svg_error)?;
|
||||
let tree = tree::parse_text(&svg, &limits, &options).map_err(svg_error)?;
|
||||
let size = tree.size().to_int_size();
|
||||
let (width, height) = (size.width(), size.height());
|
||||
if width > MAX_SIDE || height > MAX_SIDE {
|
||||
return Err(FormatError::Image(format!("SVG exceeds {MAX_SIDE} pixels")));
|
||||
}
|
||||
|
||||
let mut pixels = tiny_skia::Pixmap::new(width, height).ok_or_else(|| FormatError::Image("could not allocate SVG image".into()))?;
|
||||
resvg::render(&tree, tiny_skia::Transform::identity(), &mut pixels.as_mut());
|
||||
let pixels = draw::render(&tree, usvg::Transform::identity(), width, height, 0, DRAW_BUDGET)
|
||||
.map_err(|refusal| FormatError::Image(format!("SVG not drawn: {}", refusal.message)))?;
|
||||
Ok(Image::generate(width, height, Kind::Color, |x, y| {
|
||||
let pixel = pixels.pixel(x, y).map(|pixel| pixel.demultiply());
|
||||
pixel.map_or([0.0; 4], |pixel| {
|
||||
@@ -30,3 +60,86 @@ pub(crate) fn decode(bytes: &[u8]) -> Result<Image> {
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn svg_error(error: tree::Error) -> FormatError {
|
||||
FormatError::Image(error.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use super::decode;
|
||||
|
||||
fn svg(body: &str) -> Vec<u8> {
|
||||
format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="4" height="2">{body}</svg>"#).into_bytes()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draws_a_shape_in_linear_light() {
|
||||
let image = decode(&svg(r##"<rect width="2" height="2" fill="#ff0000"/>"##)).unwrap();
|
||||
assert_eq!((image.width(), image.height()), (4, 2));
|
||||
let left = image.get(0, 0, false);
|
||||
assert!((left[0] - 1.0).abs() < 1e-3 && left[1].abs() < 1e-3 && (left[3] - 1.0).abs() < 1e-3, "{left:?}");
|
||||
assert_eq!(image.get(3, 1, false)[3], 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_documents_are_errors_not_crashes() {
|
||||
// Entities that expand past the shared bound (a "billion laughs").
|
||||
let mut laughs = String::from(r#"<?xml version="1.0"?><!DOCTYPE svg [<!ENTITY a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa">"#);
|
||||
for level in 1..10 {
|
||||
let prev = char::from(b'a' + level - 1);
|
||||
let name = char::from(b'a' + level);
|
||||
laughs.push_str(&format!("<!ENTITY {name} \"{}\">", format!("&{prev};").repeat(10)));
|
||||
}
|
||||
laughs.push_str(r#"]><svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"><text>&j;</text></svg>"#);
|
||||
assert!(decode(laughs.as_bytes()).is_err());
|
||||
// Nesting deeper than the tree depth.
|
||||
let deep = format!("{}{}", "<g>".repeat(200), "</g>".repeat(200));
|
||||
assert!(decode(&svg(&deep)).is_err());
|
||||
// Larger than the image side limit.
|
||||
let huge = br#"<svg xmlns="http://www.w3.org/2000/svg" width="100000" height="1"/>"#;
|
||||
assert!(decode(huge).is_err());
|
||||
assert!(decode(b"<svg").is_err());
|
||||
}
|
||||
|
||||
fn nested_groups(levels: usize, inner: &str) -> String {
|
||||
format!("{}{inner}{}", "<g>".repeat(levels), "</g>".repeat(levels))
|
||||
}
|
||||
|
||||
fn embedded(levels: usize) -> Vec<u8> {
|
||||
use base64::Engine as _;
|
||||
let inner = svg(&nested_groups(levels, r##"<rect width="4" height="2" fill="#ff0000"/>"##));
|
||||
let data = base64::engine::general_purpose::STANDARD.encode(inner);
|
||||
svg(&nested_groups(30, &format!(r#"<image href="data:image/svg+xml;base64,{data}" width="4" height="2"/>"#)))
|
||||
}
|
||||
|
||||
/// The document and the SVG images it embeds share one nesting budget, so a deep document
|
||||
/// cannot embed another deep one and double the stack usvg needs.
|
||||
#[test]
|
||||
fn embedded_svgs_share_the_nesting_budget() {
|
||||
let shallow = decode(&embedded(2)).unwrap();
|
||||
assert!(shallow.get(1, 1, false)[0] > 0.9, "{:?}", shallow.get(1, 1, false));
|
||||
let deep = decode(&embedded(40)).unwrap();
|
||||
assert_eq!(deep.get(1, 1, false)[3], 0.0);
|
||||
// The document alone past its own budget is refused.
|
||||
assert!(decode(&svg(&nested_groups(40, ""))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_files_are_not_read() {
|
||||
let linked = svg(r#"<image href="file:///etc/hostname" width="4" height="2"/>"#);
|
||||
let image = decode(&linked).unwrap();
|
||||
assert_eq!(image.get(0, 0, false)[3], 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svgz_is_unpacked() {
|
||||
use std::io::Write;
|
||||
let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
gz.write_all(&svg(r#"<rect width="4" height="2" fill="blue"/>"#)).unwrap();
|
||||
let image = decode(&gz.finish().unwrap()).unwrap();
|
||||
assert!(image.get(1, 1, false)[2] > 0.9);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user