mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-19 08:51:41 +01:00
Merge pull request #15374 from Terra854/codex/svg-embedded-image-validation
Validate embedded SVG images and reuse checked XML
This commit is contained in:
22 files changed
+1752
-372
No files matched your search
Generated
+128
-35
@@ -1532,8 +1532,8 @@ name = "craft-crs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-metadata",
|
||||
"craft-xml",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -1553,6 +1553,14 @@ dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-dds"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-dialogs"
|
||||
version = "0.1.0"
|
||||
@@ -1615,12 +1623,12 @@ source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e75
|
||||
dependencies = [
|
||||
"craft-eps",
|
||||
"craft-idml",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-kra",
|
||||
"craft-ora",
|
||||
"craft-pcraft-bundle",
|
||||
"craft-raw",
|
||||
"craft-svg",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-zip",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1751,7 +1759,7 @@ name = "craft-idml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-zip",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1761,11 +1769,38 @@ name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-dds",
|
||||
"craft-dds 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-heif",
|
||||
"craft-jpeg",
|
||||
"craft-metadata",
|
||||
"craft-tiff",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jpeg-encoder",
|
||||
"png 0.18.1",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"craft-dds 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
@@ -1815,6 +1850,14 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-keymap"
|
||||
version = "0.1.0"
|
||||
@@ -1822,7 +1865,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e75
|
||||
dependencies = [
|
||||
"craft-host",
|
||||
"craft-i18n",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"egui",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1833,7 +1876,7 @@ name = "craft-kra"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-zip",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1877,7 +1920,19 @@ source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e75
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.2",
|
||||
"chrono",
|
||||
"craft-tiff",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.2",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
@@ -1887,7 +1942,7 @@ name = "craft-ora"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-zip",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -2035,8 +2090,8 @@ source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e75
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-metadata",
|
||||
"craft-tiff",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"jxl-oxide",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
@@ -2082,10 +2137,29 @@ name = "craft-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb#18ffc07519b15f5ef39e752aa0f405261fec8ebb"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"flate2",
|
||||
"kurbo 0.13.1",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg 0.48.1",
|
||||
"roxmltree 0.21.2",
|
||||
"thiserror 2.0.21",
|
||||
"usvg 0.48.1",
|
||||
"vectorcraft-geom",
|
||||
"vectorcraft-pathops",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"kurbo 0.13.1",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg 0.48.1",
|
||||
@@ -2179,6 +2253,15 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-time"
|
||||
version = "0.1.0"
|
||||
@@ -2248,7 +2331,7 @@ dependencies = [
|
||||
"craft-platform",
|
||||
"craft-quit",
|
||||
"craft-settings",
|
||||
"craft-svg",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-theme",
|
||||
"craft-ui-macros",
|
||||
"craft-window",
|
||||
@@ -2326,7 +2409,7 @@ dependencies = [
|
||||
"craft-host",
|
||||
"craft-i18n",
|
||||
"craft-platform",
|
||||
"craft-svg",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-theme",
|
||||
"craft-ui-macros",
|
||||
"craft-ui-panels",
|
||||
@@ -2439,6 +2522,16 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be#7b8c1eccea0c695a495ab279c9523e6efcc9b8be"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
@@ -6278,10 +6371,10 @@ dependencies = [
|
||||
"core-graphics2",
|
||||
"craft",
|
||||
"craft-cms",
|
||||
"craft-image-codecs",
|
||||
"craft-svg",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-update",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"eframe",
|
||||
"egui",
|
||||
"egui_kittest",
|
||||
@@ -6325,7 +6418,7 @@ name = "photocraft-algo"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"craft",
|
||||
"craft-metadata",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"criterion",
|
||||
"photocraft-color",
|
||||
"photocraft-geom",
|
||||
@@ -6345,7 +6438,7 @@ dependencies = [
|
||||
"cap-std",
|
||||
"clap",
|
||||
"craft",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-psd",
|
||||
"getrandom 0.3.4",
|
||||
"photocraft-color",
|
||||
@@ -6378,7 +6471,7 @@ dependencies = [
|
||||
"clap_mangen",
|
||||
"craft",
|
||||
"craft-cms",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-psd",
|
||||
"photocraft-automation",
|
||||
"photocraft-doc",
|
||||
@@ -6442,11 +6535,11 @@ dependencies = [
|
||||
"craft-affinity",
|
||||
"craft-cms",
|
||||
"craft-file-formats",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-pcraft-format",
|
||||
"craft-print",
|
||||
"craft-psd",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"exr",
|
||||
"flate2",
|
||||
"photocraft-algo",
|
||||
@@ -6482,7 +6575,7 @@ name = "photocraft-format"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-pcraft-bundle",
|
||||
"craft-pcraft-format",
|
||||
"craft-zip",
|
||||
@@ -6542,16 +6635,16 @@ dependencies = [
|
||||
"craft-crs",
|
||||
"craft-docpath",
|
||||
"craft-file-formats",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-kra",
|
||||
"craft-ora",
|
||||
"craft-pdf",
|
||||
"craft-pdn",
|
||||
"craft-psd",
|
||||
"craft-svg",
|
||||
"craft-tiff",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7b8c1eccea0c695a495ab279c9523e6efcc9b8be)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-xcf",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"kurbo 0.13.1",
|
||||
@@ -6642,7 +6735,7 @@ dependencies = [
|
||||
name = "photocraft-raw"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-raw",
|
||||
"flate2",
|
||||
"image",
|
||||
@@ -6704,7 +6797,7 @@ dependencies = [
|
||||
"craft",
|
||||
"craft-file-formats",
|
||||
"craft-i18n",
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-print",
|
||||
"craft-psd",
|
||||
"eframe",
|
||||
@@ -6753,7 +6846,7 @@ dependencies = [
|
||||
name = "photocraft-web"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=18ffc07519b15f5ef39e752aa0f405261fec8ebb)",
|
||||
"craft-psd",
|
||||
"eframe",
|
||||
"egui",
|
||||
|
||||
+2
-1
@@ -58,7 +58,8 @@ craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519
|
||||
craft-kra = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb" }
|
||||
craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb" }
|
||||
craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb" }
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb", features = ["tree-render", "tree-render-system-fonts"] }
|
||||
# Keep the shared embedded-image validation API until the framework revision includes it.
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "7b8c1eccea0c695a495ab279c9523e6efcc9b8be", features = ["tree-render", "tree-render-system-fonts"] }
|
||||
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb" }
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "18ffc07519b15f5ef39e752aa0f405261fec8ebb" }
|
||||
# Camera-field EXIF reader/writer, shared prerequisite craft-libs#1563.
|
||||
|
||||
@@ -460,6 +460,10 @@ pub fn custom_shapes(s: &Session) -> &[CustomShape] {
|
||||
&s.edit_state.custom_shapes
|
||||
}
|
||||
|
||||
/// Serialize tests that intentionally purge or seed the global smart caches.
|
||||
#[cfg(test)]
|
||||
pub(crate) static PURGE_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "edit_menu_cmds/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use super::*;
|
||||
use photocraft_doc::vector::Subpath;
|
||||
|
||||
static PURGE_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
fn session(depth: u32) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth, "background": "white"})).unwrap();
|
||||
|
||||
@@ -70,7 +70,12 @@ pub fn encode_source(doc: &Document) -> Result<Vec<u8>> {
|
||||
|
||||
/// Decodes smart-object contents (a `.pcraft` bundle, PSD/PSB, or any flat image format).
|
||||
pub fn decode_source(file_name: &str, bytes: &[u8]) -> Result<Document> {
|
||||
if photocraft_io::svg::is_svg(bytes) {
|
||||
decode_source_classified(file_name, bytes, photocraft_io::svg::is_svg(bytes))
|
||||
}
|
||||
|
||||
/// Reuse the central format decision made by this render operation.
|
||||
fn decode_source_classified(file_name: &str, bytes: &[u8], svg: bool) -> Result<Document> {
|
||||
if svg {
|
||||
// This API has no warning channel. Stack/source caches and contents edits must not
|
||||
// turn a partial SVG import into silently complete smart-object contents.
|
||||
return photocraft_io::svg::import_svg_bounded_images_strict(file_name, bytes, 24_000_000)
|
||||
@@ -197,13 +202,13 @@ pub(crate) use placing::detach_psd;
|
||||
pub(crate) use placing::{affine_homography, set_placement, shift_smart, transform_placement};
|
||||
use placing::{convert, reset_transform, shift_layer, via_copy};
|
||||
pub use placing::{layer_to_smart, layers_to_smart, placement};
|
||||
#[cfg(test)]
|
||||
pub(crate) use rendering::DISPLAY_NATIVE_HASH_BYTES;
|
||||
use rendering::refresh_or_fail;
|
||||
#[cfg(test)]
|
||||
pub(crate) use rendering::{DISPLAY_NATIVE_HASH_BYTES, WARNING_SOURCE_DECODES, WARNING_SOURCE_LIMIT};
|
||||
#[cfg(test)]
|
||||
use rendering::{NativeDigests, blend_surfaces, place, render_in};
|
||||
pub(crate) use rendering::{WarningRenderSources, render_with_warnings, render_with_warnings_using, source_box};
|
||||
pub use rendering::{apply_smart_filters, purge_stack_cache, refresh, refresh_layer, render, render_below_filter, stack_cache_bytes, stack_image};
|
||||
pub(crate) use rendering::{render_with_warnings, source_box};
|
||||
#[cfg(test)]
|
||||
use source::svg_source_image_in;
|
||||
pub use source::{SourceImage, purge_source_cache, source_bytes, source_cache_bytes, source_decode_count, source_image, svg_source_image};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::rendering::{refresh_or_fail, unapplied_filters};
|
||||
use super::source::{SourceImage, buffer_to_surface, cache_key, cache_put, source_bytes};
|
||||
use super::source::{SourceImage, buffer_to_surface, cache_put, source_bytes, source_cache_key};
|
||||
use super::{
|
||||
Affine, Arc, BlendMode, Document, EngineError, Homography, Layer, LayerContent, LayerId, PLACED_KEYS, Rect, Result, Session, Size, SmartContentsId,
|
||||
SmartObject, SmartSource, Surface, Value, encode_source, json, layer_param, other, source_box, translate_surface,
|
||||
@@ -258,8 +258,11 @@ pub fn layers_to_smart(doc: &Document, name: &str, layers: Vec<Layer>) -> Result
|
||||
sub.layers.iter_mut().for_each(|l| shift_layer(l, -b.x0, -b.y0));
|
||||
sub.size = Size::new(b.width(), b.height());
|
||||
let bytes = encode_source(&sub)?;
|
||||
// Seed the decode cache with the composite we already have.
|
||||
cache_put(cache_key(&bytes, fmt), SourceImage { surface: Arc::new(translate_surface(&cache, -b.x0, -b.y0)), bounds: sub.bounds() });
|
||||
// The encoded native document is non-SVG; match the classified render/bounds path.
|
||||
cache_put(
|
||||
source_cache_key(&sub.name, &bytes, fmt, Some(false)),
|
||||
SourceImage { surface: Arc::new(translate_surface(&cache, -b.x0, -b.y0)), bounds: sub.bounds() },
|
||||
);
|
||||
|
||||
let source = SmartSource::Embedded { file_name: sub.name.clone(), bytes: Arc::new(bytes) };
|
||||
Ok(Layer::new(name, LayerContent::Smart(SmartObject::new(source, Affine::translate(b.x0 as f64, b.y0 as f64), Some(cache)))))
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
use super::filters::smart;
|
||||
use super::source::{
|
||||
SourceImage, cache_get, cache_key, cache_put, convert_svg_source, source_bytes, source_image, source_surface_from_buffer, svg_source_image_in, vector_scale,
|
||||
SourceImage, cache_get, cache_key, cache_put, convert_svg_source, source_bytes, source_surface_from_buffer, svg_source_image_in, vector_scale,
|
||||
};
|
||||
use super::{
|
||||
Affine, Arc, BlendMode, Document, EngineError, Homography, JobCtx, Layer, LayerContent, LayerId, PixelFormat, Rect, Result, SmartFilter, SmartObject,
|
||||
SmartSource, Stage, StageKey, Surface, Value, affine_homography, decode_source, other, placement, stack_cache,
|
||||
SmartSource, Stage, StageKey, Surface, Value, affine_homography, other, placement, stack_cache,
|
||||
};
|
||||
use std::sync::Mutex;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::{Mutex, Weak};
|
||||
|
||||
/// Layer › Smart Objects › Stack Mode: the source's layers (or, when it holds a single group, the
|
||||
/// group's layers) combined per pixel with `mode`. Cached like [`source_image`], keyed by mode.
|
||||
/// group's layers) combined per pixel with `mode`. Cached like [`super::source_image`], keyed by mode.
|
||||
pub fn stack_image(file_name: &str, bytes: &[u8], fmt: PixelFormat, mode: photocraft_doc::StackMode) -> Result<SourceImage> {
|
||||
stack_image_classified(file_name, bytes, fmt, mode, None)
|
||||
}
|
||||
|
||||
fn stack_image_classified(file_name: &str, bytes: &[u8], fmt: PixelFormat, mode: photocraft_doc::StackMode, svg: Option<bool>) -> Result<SourceImage> {
|
||||
use photocraft_algo::stack::Stat;
|
||||
let mut key = cache_key(bytes, fmt);
|
||||
key.0 = *blake3::hash(&[&key.0[..], b"stack:", mode.id().as_bytes()].concat()).as_bytes();
|
||||
// Keep the filename in the key because fallback import uses it to choose a format.
|
||||
// Use a separate domain from entries that predate plain SVG completeness checks.
|
||||
key.0 = *blake3::hash(&[&key.0[..], b"stack-source-v3:", blake3::hash(file_name.as_bytes()).as_bytes(), mode.id().as_bytes()].concat()).as_bytes();
|
||||
if let Some(img) = cache_get(&key) {
|
||||
return Ok(img);
|
||||
}
|
||||
let doc = decode_source(file_name, bytes)?;
|
||||
let svg = svg.unwrap_or_else(|| photocraft_io::svg::is_svg(bytes));
|
||||
let doc = super::decode_source_classified(file_name, bytes, svg)?;
|
||||
let layers: &[Layer] = match doc.layers.as_slice() {
|
||||
[only] if only.is_group() => only.children().unwrap_or_default(),
|
||||
all => all,
|
||||
@@ -25,7 +33,7 @@ pub fn stack_image(file_name: &str, bytes: &[u8], fmt: PixelFormat, mode: photoc
|
||||
let bounds = doc.bounds();
|
||||
// An SVG opens with one layer per shape, and every visible one gets its own float frame:
|
||||
// they may take 1 GiB together, the SVG import's own budget, before any is rendered.
|
||||
if photocraft_io::svg::is_svg(bytes) {
|
||||
if svg {
|
||||
let visible = u64::try_from(layers.iter().filter(|l| l.visible).count()).unwrap_or(u64::MAX);
|
||||
let frame_bytes = doc.size.area().saturating_mul(16);
|
||||
if visible.saturating_mul(frame_bytes) > 1 << 30 {
|
||||
@@ -173,16 +181,24 @@ fn apply_smart_filter(f: &SmartFilter, sm: &SmartObject, cur: &Surface, canvas:
|
||||
if f.blend == BlendMode::Normal && f.opacity >= 1.0 { Ok(out) } else { Ok(blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0))) }
|
||||
}
|
||||
|
||||
/// The source placed in document pixels, before any smart filter.
|
||||
/// The source placed in document pixels, before any smart filter (uncached test oracle).
|
||||
#[cfg(test)]
|
||||
pub(super) fn place(name: &str, bytes: &[u8], doc: &Document, sm: &SmartObject) -> Result<Surface> {
|
||||
place_with_svg(name, bytes, doc, sm, &mut |bytes, scale| svg_source_image_in(bytes, doc, scale))
|
||||
place_with_svg(name, bytes, doc, sm, photocraft_io::svg::is_svg(bytes), &mut |bytes, scale| svg_source_image_in(bytes, doc, scale))
|
||||
}
|
||||
|
||||
fn place_with_svg(name: &str, bytes: &[u8], doc: &Document, sm: &SmartObject, svg: &mut impl FnMut(&[u8], f64) -> Result<SourceImage>) -> Result<Surface> {
|
||||
fn place_with_svg(
|
||||
name: &str,
|
||||
bytes: &[u8],
|
||||
doc: &Document,
|
||||
sm: &SmartObject,
|
||||
is_svg: bool,
|
||||
svg: &mut impl FnMut(&[u8], f64) -> Result<SourceImage>,
|
||||
) -> Result<Surface> {
|
||||
let fmt = doc.pixel_format();
|
||||
// A vector source is rendered at the placement's scale, and the placement is divided by
|
||||
// that scale so the rendered pixels land where the source units would.
|
||||
if sm.stack_mode.is_none() && sm.warp.is_none() && photocraft_io::svg::is_svg(bytes) {
|
||||
if sm.stack_mode.is_none() && sm.warp.is_none() && is_svg {
|
||||
let k = vector_scale(sm);
|
||||
let img = svg(bytes, k)?;
|
||||
return Ok(match &sm.perspective {
|
||||
@@ -201,10 +217,10 @@ fn place_with_svg(name: &str, bytes: &[u8], doc: &Document, sm: &SmartObject, sv
|
||||
});
|
||||
}
|
||||
let img = match sm.stack_mode {
|
||||
Some(mode) if photocraft_io::svg::is_svg(bytes) => convert_svg_source(stack_image(name, bytes, PixelFormat::RGBA32F, mode)?, doc)?,
|
||||
None if photocraft_io::svg::is_svg(bytes) => svg(bytes, 1.0)?,
|
||||
Some(mode) => stack_image(name, bytes, fmt, mode)?,
|
||||
None => source_image(name, bytes, fmt)?,
|
||||
Some(mode) if is_svg => convert_svg_source(stack_image_classified(name, bytes, PixelFormat::RGBA32F, mode, Some(is_svg))?, doc)?,
|
||||
None if is_svg => svg(bytes, 1.0)?,
|
||||
Some(mode) => stack_image_classified(name, bytes, fmt, mode, Some(is_svg))?,
|
||||
None => super::source::source_image_classified(name, bytes, fmt, is_svg)?,
|
||||
};
|
||||
// The placed stage is unclipped: a large enough placement (an image scaled to fill a huge
|
||||
// frame) would otherwise fill memory before any allocation could fail.
|
||||
@@ -312,7 +328,7 @@ pub(crate) fn source_box(doc: &Document, sm: &SmartObject) -> Option<[f64; 4]> {
|
||||
let r = match sm.stack_mode {
|
||||
None if photocraft_io::svg::is_svg(&bytes) => photocraft_io::svg::bounded_canvas_bounds(&bytes, 24_000_000).ok()?,
|
||||
Some(mode) => stack_image(&name, &bytes, fmt, mode).ok()?.bounds,
|
||||
None => source_image(&name, &bytes, fmt).ok()?.bounds,
|
||||
None => super::source::source_image(&name, &bytes, fmt).ok()?.bounds,
|
||||
};
|
||||
Some([f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)])
|
||||
}
|
||||
@@ -324,28 +340,143 @@ pub fn render(doc: &Document, sm: &SmartObject) -> Result<Option<Surface>> {
|
||||
}
|
||||
|
||||
/// Command-only rendering that retains usable SVG artwork and reports omitted embedded images.
|
||||
/// It deliberately bypasses both source and filter-stage caches: a later warningless render
|
||||
/// must not accidentally reuse an incomplete surface. Native previews remain strict.
|
||||
/// Its local source cache never enters the strict process-wide source/filter caches.
|
||||
pub(crate) fn render_with_warnings(doc: &Document, sm: &SmartObject) -> Result<(Option<Surface>, Vec<String>)> {
|
||||
let Some((name, bytes)) = source_bytes(&doc.metadata, &sm.source) else { return Ok((None, Vec::new())) };
|
||||
if !photocraft_io::svg::is_svg(&bytes) || sm.vector_preview.is_some() || sm.stack_mode.is_some() {
|
||||
render_with_warnings_using(sm, &mut WarningRenderSources::new(doc))
|
||||
}
|
||||
|
||||
struct WarningSource {
|
||||
source: Weak<Vec<u8>>,
|
||||
name: [u8; 32],
|
||||
scale: u64,
|
||||
image: SourceImage,
|
||||
warnings: Vec<String>,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
/// One command's bounded source-space renders. Placement, warp, filters and masks
|
||||
/// are instance-specific and must never be cached here. Weak identity pins only the
|
||||
/// allocation header; the borrowed document owns encoded bytes throughout the command.
|
||||
pub(crate) struct WarningRenderSources<'a> {
|
||||
document: &'a Document,
|
||||
srgb: Option<bool>,
|
||||
entries: VecDeque<WarningSource>,
|
||||
budget: usize,
|
||||
limit: usize,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(crate) static WARNING_SOURCE_DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
pub(crate) static WARNING_SOURCE_LIMIT: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
impl<'a> WarningRenderSources<'a> {
|
||||
pub(crate) fn new(document: &'a Document) -> Self {
|
||||
#[cfg(test)]
|
||||
let limit = WARNING_SOURCE_LIMIT.with(|limit| limit.get().unwrap_or(32));
|
||||
#[cfg(not(test))]
|
||||
let limit = 32;
|
||||
Self { document, srgb: None, entries: VecDeque::new(), budget: if cfg!(target_arch = "wasm32") { 128 << 20 } else { 384 << 20 }, limit, bytes: 0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn with_limits(document: &'a Document, budget: usize, limit: usize) -> Self {
|
||||
Self { budget, limit, ..Self::new(document) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn profile_initialized(&self) -> bool {
|
||||
self.srgb.is_some()
|
||||
}
|
||||
|
||||
fn image(&mut self, bytes: &Arc<Vec<u8>>, scale: f64, name: [u8; 32]) -> Result<(SourceImage, Vec<String>)> {
|
||||
let doc = self.document;
|
||||
// Non-SVG and unavailable sources keep their old fast path without
|
||||
// materializing ICC bytes. This immutable document needs the context once.
|
||||
let key = scale.to_bits();
|
||||
if let Some(index) =
|
||||
self.entries.iter().position(|entry| entry.scale == key && entry.name == name && std::ptr::eq(entry.source.as_ptr(), Arc::as_ptr(bytes)))
|
||||
&& let Some(entry) = self.entries.remove(index)
|
||||
{
|
||||
let result = (entry.image.clone(), entry.warnings.clone());
|
||||
self.entries.push_back(entry);
|
||||
return Ok(result);
|
||||
}
|
||||
// Only the exact sRGB fast path needs this lazy, immutable document context.
|
||||
// Pixel format/profile do not belong in each entry's key: the document is borrowed.
|
||||
let srgb = *self.srgb.get_or_insert_with(|| {
|
||||
doc.mode == photocraft_color::ColorMode::Rgb
|
||||
&& crate::color_cmds::document_profile(doc).to_bytes().as_ref() == craft_cms::Builtin::Srgb.profile().to_bytes().as_ref()
|
||||
});
|
||||
#[cfg(test)]
|
||||
WARNING_SOURCE_DECODES.with(|count| count.set(count.get().saturating_add(1)));
|
||||
let (buf, warnings) = photocraft_io::svg::rasterize_bounded_with_warnings(bytes, scale as f32, 24_000_000)
|
||||
.map_err(|e| other(format!("can't render the SVG smart object: {e}")))?;
|
||||
// Charge owned native tiles, entry and warning storage, not borrowed encoded bytes.
|
||||
// This is a soft memo budget: oversize entries retain the existing uncached path.
|
||||
let cost = usize::try_from(photocraft_raster::tile_storage_bytes(buf.rect, &doc.pixel_format()))
|
||||
.unwrap_or(usize::MAX)
|
||||
.saturating_add(std::mem::size_of::<WarningSource>())
|
||||
.saturating_add(warnings.capacity().saturating_mul(std::mem::size_of::<String>()))
|
||||
.saturating_add(warnings.iter().fold(0usize, |total, warning| total.saturating_add(warning.capacity())));
|
||||
let admitted = self.limit != 0 && cost <= self.budget;
|
||||
if admitted {
|
||||
while self.entries.len() >= self.limit || self.bytes > self.budget - cost {
|
||||
let Some(entry) = self.entries.pop_front() else {
|
||||
// Recover conservatively if accounting ever outlives an empty queue.
|
||||
self.bytes = 0;
|
||||
break;
|
||||
};
|
||||
self.bytes = self.bytes.saturating_sub(entry.bytes);
|
||||
}
|
||||
}
|
||||
// Preserve the normal renderer's exact built-in sRGB fast path.
|
||||
// A full memo is optional. Let the placement proceed uncached if the
|
||||
// browser cannot afford retaining these tiles along with prior renders.
|
||||
let admitted = admitted && crate::memory_gate::ensure("retain the SVG source for this update", cost as u64).is_ok();
|
||||
let image = if srgb {
|
||||
SourceImage { surface: Arc::new(super::source::buffer_to_surface(&buf, doc.pixel_format())), bounds: buf.rect }
|
||||
} else {
|
||||
let source = SourceImage { surface: Arc::new(super::source::buffer_to_surface(&buf, PixelFormat::RGBA32F)), bounds: buf.rect };
|
||||
convert_svg_source(source, doc)?
|
||||
};
|
||||
if admitted && self.entries.try_reserve(1).is_ok() {
|
||||
self.bytes += cost; // cost was admitted against budget - bytes above.
|
||||
self.entries.push_back(WarningSource {
|
||||
source: Arc::downgrade(bytes),
|
||||
name,
|
||||
scale: key,
|
||||
image: image.clone(),
|
||||
warnings: warnings.clone(),
|
||||
bytes: cost,
|
||||
});
|
||||
}
|
||||
Ok((image, warnings))
|
||||
}
|
||||
}
|
||||
|
||||
/// Reuse only warning-aware source pixels during one shared-content update.
|
||||
pub(crate) fn render_with_warnings_using(sm: &SmartObject, sources: &mut WarningRenderSources<'_>) -> Result<(Option<Surface>, Vec<String>)> {
|
||||
let doc = sources.document;
|
||||
if sm.vector_preview.is_some() || sm.stack_mode.is_some() {
|
||||
return render(doc, sm).map(|surface| (surface, Vec::new()));
|
||||
}
|
||||
let Some((name, bytes)) = source_bytes(&doc.metadata, &sm.source) else { return Ok((None, Vec::new())) };
|
||||
if !photocraft_io::svg::is_svg(&bytes) {
|
||||
// Keep the same source snapshot for fallback, including authorized linked reads.
|
||||
return render_in_with_source(doc, sm, &JobCtx::new(), Some((name, bytes))).map(|surface| (surface, Vec::new()));
|
||||
}
|
||||
let name_key = *blake3::hash(name.as_bytes()).as_bytes();
|
||||
if !unapplied_filters(sm).is_empty() {
|
||||
return Err(other("the smart object has unsupported visible filters"));
|
||||
}
|
||||
let mut warnings = Vec::new();
|
||||
let placed = place_with_svg(&name, &bytes, doc, sm, &mut |bytes, scale| {
|
||||
let (buf, omitted) = photocraft_io::svg::rasterize_bounded_with_warnings(bytes, scale as f32, 24_000_000)
|
||||
.map_err(|e| other(format!("can't render the SVG smart object: {e}")))?;
|
||||
let placed = place_with_svg(&name, &bytes, doc, sm, true, &mut |_, scale| {
|
||||
let (image, omitted) = sources.image(&bytes, scale, name_key)?;
|
||||
warnings.extend(omitted);
|
||||
// Preserve the normal renderer's exact built-in sRGB fast path.
|
||||
let profile = crate::color_cmds::document_profile(doc);
|
||||
if doc.mode == photocraft_color::ColorMode::Rgb && profile.to_bytes().as_ref() == craft_cms::Builtin::Srgb.profile().to_bytes().as_ref() {
|
||||
return Ok(SourceImage { surface: Arc::new(super::source::buffer_to_surface(&buf, doc.pixel_format())), bounds: buf.rect });
|
||||
}
|
||||
let source = SourceImage { surface: Arc::new(super::source::buffer_to_surface(&buf, PixelFormat::RGBA32F)), bounds: buf.rect };
|
||||
convert_svg_source(source, doc)
|
||||
Ok(image)
|
||||
})?;
|
||||
Ok((Some(apply_smart_filters(&placed, sm, doc.bounds())?), warnings))
|
||||
}
|
||||
@@ -354,27 +485,33 @@ pub(crate) fn render_with_warnings(doc: &Document, sm: &SmartObject) -> Result<(
|
||||
/// is cancelled. Gives the same pixels as [`apply_smart_filters`] on the placed source, but
|
||||
/// starts from the deepest stage of the stack already rendered (see [`stack_cache`]).
|
||||
pub(super) fn render_in(doc: &Document, sm: &SmartObject, ctx: &JobCtx) -> Result<Option<Surface>> {
|
||||
// Stored filters can come from a file or another layer. Check before looking up cached
|
||||
// stages too: a trusted UI render may have populated this process-wide cache earlier.
|
||||
render_in_with_source(doc, sm, ctx, None)
|
||||
}
|
||||
|
||||
fn render_in_with_source(doc: &Document, sm: &SmartObject, ctx: &JobCtx, snapshot: Option<(String, Arc<Vec<u8>>)>) -> Result<Option<Surface>> {
|
||||
// A trusted UI render may have populated these stages. Check authority even on a hit.
|
||||
for filter in sm.smart_filters.iter().filter(|f| sm.filters_enabled && f.visible) {
|
||||
crate::jobs::authorize_render_command(&filter.command, &filter.params)?;
|
||||
}
|
||||
let Some((name, bytes)) = source_bytes(&doc.metadata, &sm.source) else { return Ok(None) };
|
||||
let bytes = display_source(sm, &bytes);
|
||||
let Some((name, original)) = snapshot.or_else(|| source_bytes(&doc.metadata, &sm.source)) else { return Ok(None) };
|
||||
let bytes = display_source(sm, &original);
|
||||
let (fmt, canvas) = (doc.pixel_format(), doc.bounds());
|
||||
let mut placed_key = stack_cache::placed_key(&bytes, fmt, sm, canvas);
|
||||
if photocraft_io::svg::is_svg(&bytes) {
|
||||
let mut h = blake3::Hasher::new();
|
||||
h.update(&placed_key);
|
||||
// Plain SVG uses the original strict vector path. Gzip sources use ordinary
|
||||
// file import and native-size pixels, as existing saved objects expect.
|
||||
let is_svg = photocraft_io::svg::is_svg(&bytes);
|
||||
let mut h = blake3::Hasher::new();
|
||||
h.update(b"smart-placement-v3:");
|
||||
h.update(&stack_cache::placed_key(&bytes, fmt, sm, canvas));
|
||||
h.update(blake3::hash(name.as_bytes()).as_bytes());
|
||||
if is_svg {
|
||||
h.update(&crate::color_cmds::document_profile(doc).to_bytes());
|
||||
placed_key = *h.finalize().as_bytes();
|
||||
}
|
||||
let placed_key: StageKey = *h.finalize().as_bytes();
|
||||
let placed = || -> Result<Stage> {
|
||||
if let Some(stage) = stack_cache::get(&placed_key) {
|
||||
return Ok(stage);
|
||||
}
|
||||
let surface = place(&name, &bytes, doc, sm)?;
|
||||
// Edge pixels repeat at the canvas ∪ placed-content edge, as for layer filters.
|
||||
let surface = place_with_svg(&name, &bytes, doc, sm, is_svg, &mut |bytes, scale| svg_source_image_in(bytes, doc, scale))?;
|
||||
let stage = Stage { extent: canvas.union(&surface.content_bounds()), surface };
|
||||
stack_cache::put(placed_key, &stage);
|
||||
Ok(stage)
|
||||
@@ -497,3 +634,7 @@ pub(super) fn refresh_or_fail_in(doc: &mut Document, id: LayerId, ctx: &JobCtx)
|
||||
}
|
||||
Err(other("the smart object's contents are unavailable (missing linked file?), so it can't be re-rendered"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests/source_work.rs"]
|
||||
mod source_work_tests;
|
||||
@@ -70,7 +70,7 @@ pub struct SourceImage {
|
||||
pub bounds: Rect,
|
||||
}
|
||||
|
||||
type CacheKey = ([u8; 32], PixelFormat);
|
||||
pub(super) type CacheKey = ([u8; 32], PixelFormat, Option<[u8; 32]>);
|
||||
|
||||
struct CacheEntry {
|
||||
key: CacheKey,
|
||||
@@ -100,7 +100,23 @@ fn cache() -> std::sync::MutexGuard<'static, Vec<CacheEntry>> {
|
||||
}
|
||||
|
||||
pub(super) fn cache_key(bytes: &[u8], fmt: PixelFormat) -> CacheKey {
|
||||
(*blake3::hash(bytes).as_bytes(), fmt)
|
||||
(*blake3::hash(bytes).as_bytes(), fmt, None)
|
||||
}
|
||||
|
||||
/// Keep automatic recognition and caller-supplied classifications separate. The filename
|
||||
/// also selects extension-sensitive importers. Neither decision may be bypassed by a hit.
|
||||
pub(super) fn source_cache_key(file_name: &str, bytes: &[u8], fmt: PixelFormat, svg: Option<bool>) -> CacheKey {
|
||||
let mut key = cache_key(bytes, fmt);
|
||||
let mut context = blake3::Hasher::new();
|
||||
context.update(b"source-decode-context-v1\0");
|
||||
context.update(&[match svg {
|
||||
None => 0,
|
||||
Some(false) => 1,
|
||||
Some(true) => 2,
|
||||
}]);
|
||||
context.update(file_name.as_bytes());
|
||||
key.2 = Some(*context.finalize().as_bytes());
|
||||
key
|
||||
}
|
||||
|
||||
pub(super) fn cache_get(key: &CacheKey) -> Option<SourceImage> {
|
||||
@@ -136,7 +152,7 @@ fn cache_put_within(key: CacheKey, image: SourceImage, limit: usize) {
|
||||
/// How many times these contents were decoded at this pixel format (0 = never / evicted).
|
||||
pub fn source_decode_count(bytes: &[u8], fmt: PixelFormat) -> u32 {
|
||||
let key = cache_key(bytes, fmt);
|
||||
cache().iter().find(|e| e.key == key).map(|e| e.decodes).unwrap_or(0)
|
||||
cache().iter().filter(|e| e.key.0 == key.0 && e.key.1 == fmt).fold(0u32, |total, e| total.saturating_add(e.decodes))
|
||||
}
|
||||
|
||||
/// Bytes held by cached source composites.
|
||||
@@ -291,7 +307,7 @@ pub fn svg_source_image(bytes: &[u8], fmt: PixelFormat, scale: f64) -> Result<So
|
||||
let mut h = blake3::Hasher::new();
|
||||
h.update(bytes);
|
||||
h.update(&scale.to_bits().to_le_bytes());
|
||||
let key = (*h.finalize().as_bytes(), fmt);
|
||||
let key = (*h.finalize().as_bytes(), fmt, None);
|
||||
if let Some(img) = cache_get(&key) {
|
||||
return Ok(img);
|
||||
}
|
||||
@@ -318,7 +334,7 @@ pub(super) fn svg_source_image_in(bytes: &[u8], doc: &Document, scale: f64) -> R
|
||||
h.update(bytes);
|
||||
h.update(&scale.to_bits().to_le_bytes());
|
||||
h.update(&profile.to_bytes());
|
||||
let key = (*h.finalize().as_bytes(), fmt);
|
||||
let key = (*h.finalize().as_bytes(), fmt, None);
|
||||
if let Some(image) = cache_get(&key) {
|
||||
return Ok(image);
|
||||
}
|
||||
@@ -353,12 +369,23 @@ pub(crate) fn vector_scale(sm: &SmartObject) -> f64 {
|
||||
|
||||
/// The composited source image of `bytes` in pixel format `fmt` (decoded once, then cached).
|
||||
pub fn source_image(file_name: &str, bytes: &[u8], fmt: PixelFormat) -> Result<SourceImage> {
|
||||
let key = cache_key(bytes, fmt);
|
||||
source_image_inner(file_name, bytes, fmt, None)
|
||||
}
|
||||
|
||||
pub(super) fn source_image_classified(file_name: &str, bytes: &[u8], fmt: PixelFormat, svg: bool) -> Result<SourceImage> {
|
||||
source_image_inner(file_name, bytes, fmt, Some(svg))
|
||||
}
|
||||
|
||||
fn source_image_inner(file_name: &str, bytes: &[u8], fmt: PixelFormat, svg: Option<bool>) -> Result<SourceImage> {
|
||||
let key = source_cache_key(file_name, bytes, fmt, svg);
|
||||
if let Some(img) = cache_get(&key) {
|
||||
return Ok(img);
|
||||
}
|
||||
crate::memory_gate::ensure(&format!("render the smart object \u{201c}{file_name}\u{201d}"), crate::memory_gate::smart_render_need(bytes))?;
|
||||
let doc = decode_source(file_name, bytes)?;
|
||||
let doc = match svg {
|
||||
Some(svg) => super::decode_source_classified(file_name, bytes, svg)?,
|
||||
None => decode_source(file_name, bytes)?,
|
||||
};
|
||||
let img = SourceImage { surface: Arc::new(flattened_source(&doc, fmt)?), bounds: doc.bounds() };
|
||||
cache_put(key, img.clone());
|
||||
Ok(img)
|
||||
@@ -368,6 +395,36 @@ pub fn source_image(file_name: &str, bytes: &[u8], fmt: PixelFormat) -> Result<S
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn source_cache_keys_preserve_filename_and_classification() {
|
||||
let bytes = b"source context regression";
|
||||
let fmt = PixelFormat::RGBA8;
|
||||
let keys = [
|
||||
source_cache_key("source.png", bytes, fmt, None),
|
||||
source_cache_key("source.dat", bytes, fmt, None),
|
||||
source_cache_key("source.png", bytes, fmt, Some(false)),
|
||||
source_cache_key("source.png", bytes, fmt, Some(true)),
|
||||
cache_key(bytes, fmt),
|
||||
];
|
||||
for (i, key) in keys.iter().enumerate() {
|
||||
for other in &keys[i + 1..] {
|
||||
assert_ne!(key, other);
|
||||
}
|
||||
}
|
||||
assert_eq!(keys[0], source_cache_key("source.png", bytes, fmt, None));
|
||||
assert_ne!(keys[0], source_cache_key("source.png", bytes, PixelFormat::RGBA32F, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warm_raster_source_cannot_bypass_svg_classification() {
|
||||
let raster = photocraft_codecs::Image::from_u8(1, 1, photocraft_codecs::ChannelLayout::Rgb, vec![13, 47, 91]).unwrap();
|
||||
let png = photocraft_codecs::encode(&raster, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let name = "source-classification-regression.png";
|
||||
let first = source_image_classified(name, &png, PixelFormat::RGBA8, false).unwrap();
|
||||
assert_eq!(first.bounds, Rect::new(0, 0, 1, 1));
|
||||
assert!(source_image_classified(name, &png, PixelFormat::RGBA8, true).is_err(), "a warm raster hit must not skip strict SVG decoding");
|
||||
assert!(source_image(name, &png, PixelFormat::RGBA8).is_ok());
|
||||
}
|
||||
fn image(side: i32) -> SourceImage {
|
||||
let mut s = Surface::new(PixelFormat::RGBA8);
|
||||
s.fill_rect(Rect::new(0, 0, side, side), &[0.2, 0.4, 0.6, 1.0]);
|
||||
|
||||
@@ -5,6 +5,7 @@ mod selection;
|
||||
mod source_flatten;
|
||||
mod stack_cache;
|
||||
mod svg_profile;
|
||||
mod svg_stack_completeness;
|
||||
|
||||
const DEPTHS: [u64; 3] = [8, 16, 32];
|
||||
const W: i32 = 64;
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
use super::*;
|
||||
use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_geom::Size;
|
||||
use std::io::Write;
|
||||
|
||||
fn drawing(label: &str, omitted: bool) -> Vec<u8> {
|
||||
let image = if omitted { r#"<image width="1" height="1" href="data:image/png;base64,A"/>"# } else { "" };
|
||||
format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><!--{label}--><rect width="8" height="8" fill="red"/>{image}</svg>"#).into_bytes()
|
||||
}
|
||||
|
||||
fn gzip(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(bytes).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
fn document() -> Document {
|
||||
Document::new("Source work", Size::new(8, 8), ColorMode::Rgb, SampleType::U8)
|
||||
}
|
||||
fn object(name: &str, bytes: Arc<Vec<u8>>) -> SmartObject {
|
||||
SmartObject::new(SmartSource::Embedded { file_name: name.into(), bytes }, Affine::IDENTITY, None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_svgz_refresh_keeps_native_size_pixels_and_place_reports_omissions() {
|
||||
// The fractional edge distinguishes native-size import and resampling from
|
||||
// rasterizing the vector again at the enlarged placement resolution.
|
||||
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect x=".35" width="5.4" height="8" fill="red"/><image width="1" height="1" href="data:image/png;base64,A"/></svg>"#;
|
||||
let encoded = gzip(svg);
|
||||
let name = "saved-refresh.svgz";
|
||||
let mut session = crate::Session::new();
|
||||
session.execute("file.new", serde_json::json!({"width":32,"height":32})).unwrap();
|
||||
let placed = crate::file_cmds::place_bytes(&mut session, name, encoded.clone(), None, &serde_json::json!({"smartObject":true,"scale":175})).unwrap();
|
||||
let warnings = placed["warnings"].as_array().unwrap();
|
||||
assert!(warnings.iter().any(|warning| warning.as_str().is_some_and(|warning| warning.contains("embedded image was omitted"))));
|
||||
let state = session.active().unwrap();
|
||||
let layer = state.active_layer.unwrap();
|
||||
let saved = photocraft_format::save_to_bytes(&state.doc, &Default::default()).unwrap();
|
||||
let mut doc = photocraft_format::load_from_bytes(&saved).unwrap();
|
||||
let format = doc.pixel_format();
|
||||
let mut changed = doc.layer(layer).unwrap().clone();
|
||||
let LayerContent::Smart(sm) = &mut changed.content else { panic!("saved SVGZ smart object") };
|
||||
sm.transform.m[4] += 1.;
|
||||
sm.cache = None;
|
||||
|
||||
// Independent oracle for the previous route: ordinary file import at source
|
||||
// size, then the same image placement used for other raster sources.
|
||||
let source = crate::file_cmds::import(name, &encoded).unwrap();
|
||||
let bounds = source.bounds();
|
||||
let source = super::super::source::flattened_source(&source, format).unwrap();
|
||||
let expected = photocraft_algo::warp::place_source(&source, bounds, &sm.transform, None).unwrap();
|
||||
assert_eq!(source_box(&doc, sm), Some([0., 0., 8., 8.]));
|
||||
assert!(refresh_layer(&doc, &mut changed).unwrap());
|
||||
let LayerContent::Smart(sm) = &mut changed.content else { panic!("refreshed SVGZ smart object") };
|
||||
assert_eq!(sm.cache.as_ref(), Some(&expected));
|
||||
|
||||
sm.smart_filters.push(SmartFilter {
|
||||
command: "image.adjustments.invert".into(),
|
||||
params: serde_json::json!({}),
|
||||
blend: BlendMode::Normal,
|
||||
opacity: 1.,
|
||||
visible: true,
|
||||
});
|
||||
let filtered = apply_smart_filters(&expected, sm, doc.bounds()).unwrap();
|
||||
*doc.layer_mut(layer).unwrap() = changed;
|
||||
assert!(refresh(&mut doc, layer).unwrap());
|
||||
let LayerContent::Smart(sm) = &doc.layer(layer).unwrap().content else { panic!("filtered SVGZ smart object") };
|
||||
assert_eq!(sm.cache.as_ref(), Some(&filtered));
|
||||
|
||||
let plain = object("saved-refresh.svg", Arc::new(svg.to_vec()));
|
||||
assert!(render(&doc, &plain).is_err(), "plain SVG refresh still requires complete pixels");
|
||||
let (pixels, warnings) = render_with_warnings(&doc, &plain).unwrap();
|
||||
assert!(pixels.is_some());
|
||||
assert_eq!(warnings.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memo_prices_owned_output_and_does_not_retain_encoded_capacity() {
|
||||
let doc = document();
|
||||
let data = drawing("reserved encoded capacity", false);
|
||||
let mut reserved = Vec::with_capacity(1 << 20);
|
||||
reserved.extend_from_slice(&data);
|
||||
let bytes = Arc::new(reserved);
|
||||
let weak = Arc::downgrade(&bytes);
|
||||
let sm = object("reserved.svg", bytes);
|
||||
let tile = photocraft_raster::tile_storage_bytes(doc.bounds(), &doc.pixel_format()) as usize;
|
||||
let mut memo = WarningRenderSources::with_limits(&doc, tile + 4096, 32);
|
||||
WARNING_SOURCE_DECODES.with(|value| value.set(0));
|
||||
let first = render_with_warnings_using(&sm, &mut memo).unwrap();
|
||||
assert!(first.1.is_empty());
|
||||
assert_eq!(memo.entries.len(), 1, "encoded spare capacity is borrowed, not retained memo storage");
|
||||
assert!(memo.bytes <= tile + 4096);
|
||||
assert_eq!(render_with_warnings_using(&sm, &mut memo).unwrap(), first);
|
||||
assert_eq!(WARNING_SOURCE_DECODES.with(std::cell::Cell::get), 1);
|
||||
assert_eq!(weak.strong_count(), 1, "only the caller owns its source allocation");
|
||||
drop(sm);
|
||||
assert!(weak.upgrade().is_none(), "the memo must not pin an encoded body after its owner drops it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_memo_with_stale_accounting_recovers_without_panicking() {
|
||||
let doc = document();
|
||||
let data = Arc::new(drawing("empty accounting", false));
|
||||
let mut memo = WarningRenderSources::new(&doc);
|
||||
memo.bytes = memo.budget;
|
||||
let (image, warnings) = memo.image(&data, 1., *blake3::hash(b"empty.svg").as_bytes()).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
assert_eq!(image.surface.rgba(3, 3), [1., 0., 0., 1.]);
|
||||
assert_eq!(memo.entries.len(), 1);
|
||||
assert!(memo.bytes <= memo.budget);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_named_svg_keeps_the_ordinary_import_path() {
|
||||
let image = photocraft_codecs::Image::from_u8(1, 1, photocraft_codecs::ChannelLayout::Rgb, vec![0, 255, 0]).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let doc = document();
|
||||
let sm = object("renamed.svg", Arc::new(png));
|
||||
let rendered = render(&doc, &sm).unwrap().unwrap();
|
||||
assert_eq!(rendered.rgba(0, 0), [0., 1., 0., 1.]);
|
||||
let (reported, warnings) = render_with_warnings_using(&sm, &mut WarningRenderSources::new(&doc)).unwrap();
|
||||
assert_eq!(reported.unwrap(), rendered);
|
||||
assert!(warnings.is_empty());
|
||||
}
|
||||
@@ -92,16 +92,23 @@ fn convert_to_layers_unpacks_full_canvas_shapes_in_a_4k_document() {
|
||||
/// and renders the rest of the drawing, as it did before the bounded render.
|
||||
#[test]
|
||||
fn svg_with_an_unreadable_embedded_image_still_places() {
|
||||
let svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"8\" height=\"8\"><image width=\"4\" height=\"4\" href=\"data:image/x-foo,abc\"/><rect x=\"4\" width=\"4\" height=\"8\" fill=\"#ff0000\"/></svg>";
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
let placed = crate::file_cmds::place_bytes(&mut s, "Logo.svg", svg.as_bytes().to_vec(), None, &json!({"smartObject": true})).unwrap();
|
||||
assert!(placed["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().contains("embedded image was omitted")));
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let LayerContent::Smart(sm) = &doc.layer(LayerId(placed["layer"].as_u64().unwrap())).unwrap().content else { panic!("smart") };
|
||||
assert_eq!(sm.cache.as_ref().unwrap().rgba(6, 4), [1.0, 0.0, 0.0, 1.0]);
|
||||
// A warning-bearing command must not poison the strict source/filter caches.
|
||||
assert!(render(doc, sm).is_err());
|
||||
for href in ["data:image/x-foo,abc", "data:image/png;base64,A"] {
|
||||
let svg = format!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"8\" height=\"8\"><image width=\"4\" height=\"4\" href=\"{href}\"/><rect x=\"4\" width=\"4\" height=\"8\" fill=\"#ff0000\"/></svg>"
|
||||
);
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
let placed = crate::file_cmds::place_bytes(&mut s, "Logo.svg", svg.as_bytes().to_vec(), None, &json!({"smartObject": true})).unwrap();
|
||||
let warnings = placed["warnings"].as_array().unwrap();
|
||||
assert_eq!(warnings.len(), 1, "{href}: {warnings:?}");
|
||||
assert!(warnings[0].as_str().unwrap().contains("embedded image was omitted"));
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let LayerContent::Smart(sm) = &doc.layer(LayerId(placed["layer"].as_u64().unwrap())).unwrap().content else { panic!("smart") };
|
||||
assert_eq!(sm.cache.as_ref().unwrap().rgba(6, 4), [1.0, 0.0, 0.0, 1.0]);
|
||||
assert!(matches!(&sm.source, SmartSource::Embedded { file_name, bytes } if file_name == "Logo.svg" && bytes.as_slice() == svg.as_bytes()));
|
||||
// The warning render must neither repair the original bytes nor populate strict caches.
|
||||
assert!(render(doc, sm).is_err(), "{href}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Warp setup needs only the source frame, found with the bounded parse and the exact canvas
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
use super::super::{source, stack_cache};
|
||||
use super::*;
|
||||
|
||||
fn incomplete_svg() -> Vec<u8> {
|
||||
let image = photocraft_codecs::Image::from_u8(1, 1, photocraft_codecs::ChannelLayout::Rgba, vec![0, 255, 0, 255]).unwrap();
|
||||
let mut png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let idat = png.windows(4).position(|bytes| bytes == b"IDAT").unwrap();
|
||||
// Keep the complete IHDR and dimensions, but damage actual compressed pixels.
|
||||
*png.get_mut(idat + 4).unwrap() ^= 0xff;
|
||||
assert_eq!(photocraft_codecs::probe_allocation_size(&png).unwrap(), (1, 1));
|
||||
let data = png.iter().map(|byte| format!("%{byte:02X}")).collect::<String>();
|
||||
format!("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"2\" height=\"2\"><!--stack-completeness-review--><rect width=\"2\" height=\"2\" fill=\"red\"/><image width=\"2\" height=\"2\" href=\"data:image/png,{data}\"/></svg>").into_bytes()
|
||||
}
|
||||
|
||||
fn smart_svg(bytes: Vec<u8>) -> SmartObject {
|
||||
SmartObject::new(SmartSource::Embedded { file_name: "Stack completeness.svg".into(), bytes: Arc::new(bytes) }, Affine::IDENTITY, None)
|
||||
}
|
||||
|
||||
fn old_stack_key(bytes: &[u8], fmt: PixelFormat, mode: photocraft_doc::StackMode) -> source::CacheKey {
|
||||
let mut key = source::cache_key(bytes, fmt);
|
||||
key.0 = *blake3::hash(&[&key.0[..], b"stack:", mode.id().as_bytes()].concat()).as_bytes();
|
||||
key
|
||||
}
|
||||
|
||||
fn old_placed_key(bytes: &[u8], doc: &Document, smart: &SmartObject) -> StageKey {
|
||||
let key = stack_cache::placed_key(bytes, doc.pixel_format(), smart, doc.bounds());
|
||||
let mut hash = blake3::Hasher::new();
|
||||
hash.update(&key);
|
||||
hash.update(&crate::color_cmds::document_profile(doc).to_bytes());
|
||||
*hash.finalize().as_bytes()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gzipped_svg_stack_retains_ordinary_import_for_misnamed_sources() {
|
||||
use std::io::Write;
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(&incomplete_svg()).unwrap();
|
||||
let bytes = Arc::new(encoder.finish().unwrap());
|
||||
let session = session(8);
|
||||
let doc = &session.active().unwrap().doc;
|
||||
for name in ["logo.gz", "without-extension"] {
|
||||
let mut smart = SmartObject::new(SmartSource::Embedded { file_name: name.into(), bytes: bytes.clone() }, Affine::IDENTITY, None);
|
||||
assert!(decode_source(name, &bytes).is_ok());
|
||||
smart.stack_mode = Some(photocraft_doc::StackMode::Mean);
|
||||
let rendered = render(doc, &smart).unwrap().unwrap();
|
||||
assert_eq!(rendered.rgba(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(source_box(doc, &smart), Some([0., 0., 2., 2.]));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_stack_rejects_incomplete_pixels_repeatedly_but_ordinary_placement_keeps_warnings() {
|
||||
let session = session(8);
|
||||
let doc = &session.active().unwrap().doc;
|
||||
let bytes = incomplete_svg();
|
||||
let mut smart = smart_svg(bytes.clone());
|
||||
let (ordinary, warnings) = render_with_warnings(doc, &smart).unwrap();
|
||||
assert_eq!(ordinary.unwrap().rgba(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
assert!(warnings[0].contains("embedded image was omitted"));
|
||||
smart.stack_mode = Some(photocraft_doc::StackMode::Mean);
|
||||
for _ in 0..2 {
|
||||
assert!(stack_image("Stack completeness.svg", &bytes, PixelFormat::RGBA32F, photocraft_doc::StackMode::Mean).is_err());
|
||||
assert!(render(doc, &smart).is_err());
|
||||
assert!(render_with_warnings(doc, &smart).is_err());
|
||||
assert_eq!(source_box(doc, &smart), None);
|
||||
assert!(smart.cache.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_stack_does_not_reuse_incomplete_legacy_source_or_filter_stages() {
|
||||
let _cache = crate::edit_menu_cmds::PURGE_TEST_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let session = session(8);
|
||||
let doc = &session.active().unwrap().doc;
|
||||
let bytes = incomplete_svg();
|
||||
let mut smart = smart_svg(bytes.clone());
|
||||
smart.stack_mode = Some(photocraft_doc::StackMode::Mean);
|
||||
let mut poisoned = Surface::new(PixelFormat::RGBA32F);
|
||||
poisoned.fill_rect(Rect::new(0, 0, 2, 2), &[0., 1., 0., 1.]);
|
||||
let key = old_stack_key(&bytes, PixelFormat::RGBA32F, photocraft_doc::StackMode::Mean);
|
||||
source::cache_put(key, source::SourceImage { surface: Arc::new(poisoned), bounds: Rect::new(0, 0, 2, 2) });
|
||||
assert!(stack_image("Stack completeness.svg", &bytes, PixelFormat::RGBA32F, photocraft_doc::StackMode::Mean).is_err());
|
||||
|
||||
let mut poisoned = Surface::new(doc.pixel_format());
|
||||
poisoned.fill_rect(Rect::new(0, 0, 2, 2), &[0., 1., 0., 1.]);
|
||||
let stage = Stage { surface: poisoned, extent: doc.bounds() };
|
||||
let placed = old_placed_key(&bytes, doc, &smart);
|
||||
stack_cache::put(placed, &stage);
|
||||
assert!(render(doc, &smart).is_err(), "old placed pixels must not bypass strict decoding");
|
||||
smart.smart_filters.push(SmartFilter {
|
||||
command: "filter.blur.gaussianBlur".into(),
|
||||
params: json!({"radius": 1}),
|
||||
blend: BlendMode::Normal,
|
||||
opacity: 1.,
|
||||
visible: true,
|
||||
});
|
||||
let filtered = stack_cache::filter_key(&placed, &smart.smart_filters[0], None);
|
||||
stack_cache::put(filtered, &stage);
|
||||
assert!(render(doc, &smart).is_err(), "old deepest filter stages must not bypass strict decoding");
|
||||
assert!(render_with_warnings(doc, &smart).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_svg_stack_keeps_mean_pixels_across_source_stage_and_warning_render_caches() {
|
||||
let bytes = br#"<svg xmlns="http://www.w3.org/2000/svg" width="2" height="2"><!--complete-stack-review--><rect width="2" height="2" fill="red"/><rect width="2" height="2" fill="blue"/></svg>"#;
|
||||
let session = session(32);
|
||||
let mut doc = (*session.active().unwrap().doc).clone();
|
||||
doc.icc_profile = Some(craft_cms::Builtin::Srgb.profile().to_bytes());
|
||||
let mut smart = smart_svg(bytes.to_vec());
|
||||
smart.stack_mode = Some(photocraft_doc::StackMode::Mean);
|
||||
// The red/blue Mean is 0.5 in the SVG's U8 encoding. Existing source
|
||||
// conversion rounds it to 128/255 before widening and decoding the sRGB
|
||||
// transfer for F32. The explicit sRGB destination keeps the rendered result
|
||||
// within ICC roundoff of that value (the matrix path is not bit-exact).
|
||||
let expected = photocraft_color::convert::srgb_to_linear(128.0 / 255.0);
|
||||
let check_mean = |pixel: [f32; 4]| {
|
||||
assert!((pixel[0] - expected).abs() < 3e-5, "red Mean: {pixel:?}");
|
||||
assert!(pixel[1].abs() < 1e-6, "green Mean: {pixel:?}");
|
||||
assert!((pixel[2] - expected).abs() < 3e-5, "blue Mean: {pixel:?}");
|
||||
assert_eq!(pixel[3], 1.0);
|
||||
};
|
||||
for _ in 0..2 {
|
||||
let source = stack_image("Complete.svg", bytes, PixelFormat::RGBA32F, photocraft_doc::StackMode::Mean).unwrap();
|
||||
check_mean(source.surface.rgba(1, 1));
|
||||
let strict = render(&doc, &smart).unwrap().unwrap();
|
||||
check_mean(strict.rgba(1, 1));
|
||||
let (reported, warnings) = render_with_warnings(&doc, &smart).unwrap();
|
||||
assert_eq!(reported.unwrap(), strict);
|
||||
assert!(warnings.is_empty());
|
||||
assert_eq!(source_box(&doc, &smart), Some([0., 0., 2., 2.]));
|
||||
}
|
||||
}
|
||||
@@ -385,6 +385,7 @@ fn update_contents_with_frame_budget(s: &mut Session, p: &Value, native: bool, f
|
||||
replacements.push(replacement);
|
||||
}
|
||||
// Finish every fallible render before committing any document state.
|
||||
let mut sources = crate::smart_cmds::WarningRenderSources::new(doc);
|
||||
for replacement in &mut replacements {
|
||||
if let Some(preview) = vector_preview.as_ref() {
|
||||
// This exact SVG was fully validated above. Render only a disposable display
|
||||
@@ -402,7 +403,7 @@ fn update_contents_with_frame_budget(s: &mut Session, p: &Value, native: bool, f
|
||||
sm.cache = rendered.cache;
|
||||
} else {
|
||||
let LayerContent::Smart(sm) = &mut replacement.content else { return Err(EngineError::NoLayer(replacement.id)) };
|
||||
let (surface, omitted) = crate::smart_cmds::render_with_warnings(doc, sm)?;
|
||||
let (surface, omitted) = crate::smart_cmds::render_with_warnings_using(sm, &mut sources)?;
|
||||
sm.cache = Some(surface.ok_or_else(|| bad(UPDATE, "SVG contents could not be rendered"))?);
|
||||
for warning in omitted {
|
||||
if !warnings.contains(&warning) {
|
||||
@@ -412,6 +413,7 @@ fn update_contents_with_frame_budget(s: &mut Session, p: &Value, native: bool, f
|
||||
}
|
||||
crate::smart_cmds::detach_psd(replacement);
|
||||
}
|
||||
drop(sources);
|
||||
s.edit(if native { "Update Vector Contents" } else { "Update SVG Contents" }, |doc, _| {
|
||||
for replacement in replacements {
|
||||
let id = replacement.id;
|
||||
@@ -459,5 +461,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod source_cache_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,306 @@
|
||||
use super::*;
|
||||
use crate::smart_cmds::{WARNING_SOURCE_DECODES, WARNING_SOURCE_LIMIT, WarningRenderSources, render_with_warnings_using};
|
||||
use photocraft_color::{ColorMode, PixelFormat, SampleType};
|
||||
use photocraft_doc::SmartFilter;
|
||||
use photocraft_geom::Affine;
|
||||
|
||||
fn artwork(width: u32, height: u32) -> String {
|
||||
let image = photocraft_codecs::Image::from_u8(width, height, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat((width * height) as usize)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let encoded = base64::engine::general_purpose::STANDARD.encode(png);
|
||||
format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><image width="8" height="16" preserveAspectRatio="none" href="data:image/png;base64,{encoded}"/><rect x="8" width="8" height="16" fill="#808080"/><image width="1" height="1" href="data:image/png;base64,A"/></svg>"##
|
||||
)
|
||||
}
|
||||
|
||||
fn object(svg: &str) -> SmartObject {
|
||||
SmartObject::new(SmartSource::Embedded { file_name: "Memo.svg".into(), bytes: Arc::new(svg.as_bytes().to_vec()) }, Affine::IDENTITY, None)
|
||||
}
|
||||
|
||||
fn count() -> usize {
|
||||
WARNING_SOURCE_DECODES.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
fn reset() {
|
||||
WARNING_SOURCE_DECODES.with(|count| count.set(0));
|
||||
}
|
||||
|
||||
fn assert_update_warnings(result: &Value) {
|
||||
let warnings = result["warnings"].as_array().unwrap();
|
||||
let omitted = "SVG: 1 embedded image was omitted";
|
||||
assert_eq!(warnings.iter().filter(|warning| warning.as_str().unwrap().starts_with(omitted)).count(), 1);
|
||||
assert!(warnings.len() <= 2, "at most one omission and one image-layer information notice: {warnings:?}");
|
||||
assert!(warnings.iter().all(|warning| {
|
||||
let warning = warning.as_str().unwrap();
|
||||
warning.starts_with(omitted) || warning == "SVG: embedded images are pixel layers"
|
||||
}));
|
||||
}
|
||||
|
||||
fn draw(sources: &mut WarningRenderSources<'_>, sm: &SmartObject) -> photocraft_raster::Surface {
|
||||
let (surface, warnings) = render_with_warnings_using(sm, sources).unwrap();
|
||||
assert_eq!(warnings.len(), 1);
|
||||
surface.unwrap()
|
||||
}
|
||||
|
||||
fn shared_session(instances: usize) -> (Session, LayerId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width":32,"height":32})).unwrap();
|
||||
let original = br#"<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><rect width="16" height="16" fill="red"/></svg>"#;
|
||||
let placed = crate::file_cmds::place_bytes(&mut s, "Shared.svg", original.to_vec(), None, &json!({"smartObject":true})).unwrap();
|
||||
let id = LayerId(placed["layer"].as_u64().unwrap());
|
||||
let first = s.active().unwrap().doc.layer(id).unwrap().clone();
|
||||
let doc = Arc::make_mut(&mut s.active_mut().unwrap().doc);
|
||||
for index in 1..instances {
|
||||
let mut layer = first.duplicate();
|
||||
let LayerContent::Smart(sm) = &mut layer.content else { panic!("smart") };
|
||||
sm.transform = Affine::translate((index % 4) as f64, (index % 3) as f64);
|
||||
layer.opacity = 0.5;
|
||||
doc.layers.push(layer);
|
||||
}
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn request(s: &mut Session, id: LayerId, svg: &str) -> Value {
|
||||
let handoff = s.execute(EXPORT, json!({"layer":id.0})).unwrap();
|
||||
json!({"document":handoff["document"],"layer":id.0,"contentsId":handoff["contentsId"],"fingerprint":handoff["fingerprint"],"svg":svg})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warning_source_profile_context_stays_lazy_for_non_svg_and_missing_sources() {
|
||||
let doc = Document::new("Raster", Size::new(8, 8), ColorMode::Rgb, SampleType::U8);
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let sm = SmartObject::new(SmartSource::Embedded { file_name: "Raster.png".into(), bytes: Arc::new(png) }, Affine::IDENTITY, None);
|
||||
let expected = crate::smart_cmds::render(&doc, &sm).unwrap();
|
||||
let mut sources = WarningRenderSources::new(&doc);
|
||||
assert!(!sources.profile_initialized());
|
||||
reset();
|
||||
let (surface, warnings) = render_with_warnings_using(&sm, &mut sources).unwrap();
|
||||
assert_eq!(surface, expected);
|
||||
assert!(warnings.is_empty());
|
||||
assert!(!sources.profile_initialized(), "a strict non-SVG cache hit needs no SVG color context");
|
||||
let missing = SmartObject::new(SmartSource::Linked { path: String::new() }, Affine::IDENTITY, None);
|
||||
let (surface, warnings) = render_with_warnings_using(&missing, &mut sources).unwrap();
|
||||
assert!(surface.is_none());
|
||||
assert!(warnings.is_empty());
|
||||
assert!(!sources.profile_initialized());
|
||||
assert_eq!(count(), 0);
|
||||
draw(&mut sources, &object(&artwork(4, 4)));
|
||||
assert!(sources.profile_initialized());
|
||||
assert_eq!(count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_svg_update_decodes_one_source_for_equal_scale_and_keeps_warning_caches_private() {
|
||||
let (mut s, id) = shared_session(100);
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let svg = artwork(8, 8);
|
||||
let params = request(&mut s, id, &svg);
|
||||
reset();
|
||||
let result = s.execute(UPDATE, params).unwrap();
|
||||
assert_eq!(count(), 1, "each placement must reuse the update's source-space render");
|
||||
assert_update_warnings(&result);
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let instances: Vec<_> = doc.layers.iter().filter(|layer| matches!(&layer.content, LayerContent::Smart(_))).collect();
|
||||
assert_eq!(instances.len(), 100);
|
||||
for layer in instances {
|
||||
let sm = smart(doc, layer.id).unwrap();
|
||||
let previous = before.layer(layer.id).unwrap();
|
||||
assert_eq!(layer.opacity, previous.opacity);
|
||||
assert_eq!(sm.transform, smart(&before, layer.id).unwrap().transform);
|
||||
assert_eq!(embedded(doc, sm).unwrap().1, svg.as_bytes());
|
||||
let x = sm.transform.m[4] as i32;
|
||||
let y = sm.transform.m[5] as i32;
|
||||
assert_eq!(sm.cache.as_ref().unwrap().rgba(x + 3, y + 3), [0., 1., 0., 1.]);
|
||||
}
|
||||
let sm = smart(doc, id).unwrap();
|
||||
assert!(crate::smart_cmds::render(doc, sm).is_err(), "strict global cache must remain unpoisoned");
|
||||
let mut stack = sm.clone();
|
||||
stack.stack_mode = Some(photocraft_doc::StackMode::Mean);
|
||||
assert!(crate::smart_cmds::render(doc, &stack).is_err(), "Stack Mode still requires complete artwork");
|
||||
assert!(s.undo());
|
||||
assert_eq!(s.active().unwrap().doc, before);
|
||||
assert!(s.redo());
|
||||
assert_eq!(embedded(&s.active().unwrap().doc, smart(&s.active().unwrap().doc, id).unwrap()).unwrap().1, svg.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_svg_sources_separate_scale_and_profile_but_keep_instance_filters_and_placement() {
|
||||
let mut doc = Document::new("Profile", Size::new(64, 64), ColorMode::Rgb, SampleType::F32);
|
||||
doc.icc_profile = Some(craft_cms::Builtin::Srgb.profile().to_bytes());
|
||||
let mut linear = doc.clone();
|
||||
linear.icc_profile = Some(craft_cms::Builtin::LinearSrgb.profile().to_bytes());
|
||||
let first = object(&artwork(4, 4));
|
||||
let mut moved = first.clone();
|
||||
moved.transform = Affine::translate(20., 0.);
|
||||
moved.smart_filters.push(SmartFilter {
|
||||
command: "image.adjustments.invert".into(),
|
||||
params: json!({}),
|
||||
blend: photocraft_color::BlendMode::Normal,
|
||||
opacity: 0.5,
|
||||
visible: true,
|
||||
});
|
||||
let mut sources = WarningRenderSources::new(&doc);
|
||||
reset();
|
||||
let a = draw(&mut sources, &first);
|
||||
let b = draw(&mut sources, &moved);
|
||||
assert_eq!(count(), 1);
|
||||
assert_eq!(a.rgba(3, 3), [0., 1., 0., 1.]);
|
||||
assert_eq!(b.rgba(3, 3), [0.; 4]);
|
||||
assert!(b.rgba(23, 3).iter().take(3).all(|value| (*value - 0.5).abs() < 0.01));
|
||||
let mut scaled = first.clone();
|
||||
scaled.transform = Affine { m: [2., 0., 0., 2., 0., 0.] };
|
||||
draw(&mut sources, &scaled);
|
||||
assert_eq!(count(), 2, "different raster scales require different source renders");
|
||||
let corrected = draw(&mut WarningRenderSources::new(&linear), &first);
|
||||
assert_eq!(count(), 3, "a different document interpretation cannot reuse the earlier source");
|
||||
assert!((a.rgba(12, 3)[0] - 0.502).abs() < 0.01);
|
||||
assert!((corrected.rgba(12, 3)[0] - 0.216).abs() < 0.01);
|
||||
assert_eq!(draw(&mut sources, &first), a);
|
||||
assert_eq!(count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_svg_source_memo_evicts_and_oversize_entries_render_without_retention() {
|
||||
let mut doc = Document::new("Budget", Size::new(64, 64), ColorMode::Rgb, SampleType::U8);
|
||||
doc.icc_profile = Some(craft_cms::Builtin::Srgb.profile().to_bytes());
|
||||
let first = object(&artwork(4, 4));
|
||||
let mut scaled = first.clone();
|
||||
scaled.transform = Affine { m: [2., 0., 0., 2., 0., 0.] };
|
||||
let tile = photocraft_raster::tile_storage_bytes(Rect::new(0, 0, 32, 32), &PixelFormat::RGBA8) as usize;
|
||||
for (budget, limit) in [(tile + 4096, 32), (usize::MAX, 1)] {
|
||||
let mut sources = WarningRenderSources::with_limits(&doc, budget, limit);
|
||||
reset();
|
||||
let expected = draw(&mut sources, &first);
|
||||
draw(&mut sources, &scaled);
|
||||
assert_eq!(draw(&mut sources, &first), expected);
|
||||
assert_eq!(count(), 3, "byte and entry limits both evict the old scale");
|
||||
}
|
||||
let huge_source = artwork(4, 4).replace("</svg>", &format!("<!--{}--></svg>", " ".repeat(tile + 8192)));
|
||||
let encoded = object(&huge_source);
|
||||
let mut sources = WarningRenderSources::with_limits(&doc, tile + 4096, 32);
|
||||
reset();
|
||||
let expected = draw(&mut sources, &encoded);
|
||||
assert_eq!(draw(&mut sources, &encoded), expected);
|
||||
assert_eq!(count(), 1, "the immutable document owns encoded bytes; only rendered output belongs to the memo budget");
|
||||
let mut output_too_large = WarningRenderSources::with_limits(&doc, tile - 1, 32);
|
||||
reset();
|
||||
let expected = draw(&mut output_too_large, &first);
|
||||
assert_eq!(draw(&mut output_too_large, &first), expected);
|
||||
assert_eq!(count(), 2, "oversize rendered output remains an uncached success");
|
||||
let mut disabled = WarningRenderSources::with_limits(&doc, 0, 0);
|
||||
assert_eq!(draw(&mut disabled, &first), draw(&mut WarningRenderSources::new(&doc), &first));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_svg_update_render_failure_and_cancelled_authorization_preserve_document_history() {
|
||||
for cancel in [false, true] {
|
||||
let (mut s, id) = shared_session(2);
|
||||
let doc = Arc::make_mut(&mut s.active_mut().unwrap().doc);
|
||||
let LayerContent::Smart(sm) = &mut doc.layers.last_mut().unwrap().content else { panic!("smart") };
|
||||
sm.smart_filters.push(SmartFilter {
|
||||
command: if cancel { "image.adjustments.invert" } else { "unsupported.persisted.filter" }.into(),
|
||||
params: json!({}),
|
||||
blend: photocraft_color::BlendMode::Normal,
|
||||
opacity: 1.,
|
||||
visible: true,
|
||||
});
|
||||
if cancel {
|
||||
s.authorize = Some(|command, _| if command == "image.adjustments.invert" { Err(EngineError::Cancelled) } else { Ok(()) });
|
||||
}
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let history = s.active().unwrap().history.entries();
|
||||
let revision = s.active().unwrap().revision;
|
||||
let params = request(&mut s, id, &artwork(4, 4));
|
||||
reset();
|
||||
let error = s.execute(UPDATE, params).unwrap_err();
|
||||
if cancel {
|
||||
assert!(matches!(error, EngineError::Cancelled), "expected the authority cancellation, got {error}");
|
||||
} else {
|
||||
assert!(error.to_string().contains("unsupported visible filters"), "{error}");
|
||||
}
|
||||
assert_eq!(s.active().unwrap().doc, before);
|
||||
assert_eq!(s.active().unwrap().history.entries(), history);
|
||||
assert_eq!(s.active().unwrap().revision, revision);
|
||||
assert_eq!(count(), if cancel { 0 } else { 1 });
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_svg_update_keeps_the_existing_placed_pixel_budget_atomic() {
|
||||
let (mut s, id) = shared_session(2);
|
||||
let LayerContent::Smart(sm) = &mut Arc::make_mut(&mut s.active_mut().unwrap().doc).layers.last_mut().unwrap().content else { panic!("smart") };
|
||||
sm.transform = Affine { m: [1000., 0., 0., 1000., 0., 0.] };
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let history = s.active().unwrap().history.entries();
|
||||
let revision = s.active().unwrap().revision;
|
||||
let params = request(&mut s, id, &artwork(4, 4));
|
||||
reset();
|
||||
assert!(s.execute(UPDATE, params).is_err());
|
||||
assert_eq!(count(), 0, "placed-frame admission precedes source rendering");
|
||||
assert_eq!(s.active().unwrap().doc, before);
|
||||
assert_eq!(s.active().unwrap().history.entries(), history);
|
||||
assert_eq!(s.active().unwrap().revision, revision);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "24MP embedded PNG / 100 shared instances; run once in release for performance qualification"]
|
||||
fn shared_svg_update_24mp_source_timing() {
|
||||
struct MemoLimit(Option<usize>);
|
||||
impl MemoLimit {
|
||||
fn set(limit: usize) -> Self {
|
||||
Self(WARNING_SOURCE_LIMIT.with(|current| current.replace(Some(limit))))
|
||||
}
|
||||
}
|
||||
impl Drop for MemoLimit {
|
||||
fn drop(&mut self) {
|
||||
WARNING_SOURCE_LIMIT.with(|current| current.set(self.0));
|
||||
}
|
||||
}
|
||||
let previous_limit = WARNING_SOURCE_LIMIT.with(std::cell::Cell::get);
|
||||
// Encode once. Each pass builds the same initial document and handoff outside
|
||||
// the clock; UPDATE includes its normal import and all instance rendering.
|
||||
let svg = artwork(6000, 4000);
|
||||
let mut expected = None;
|
||||
let mut timings = Vec::new();
|
||||
for (label, limit, decodes) in [("optimized-before", 32, 1), ("memo-off", 0, 100), ("optimized-after", 32, 1)] {
|
||||
let (mut s, id) = shared_session(100);
|
||||
let params = request(&mut s, id, &svg);
|
||||
let _restore = MemoLimit::set(limit);
|
||||
reset();
|
||||
let start = std::time::Instant::now();
|
||||
let result = s.execute(UPDATE, params).unwrap();
|
||||
let elapsed = start.elapsed().as_secs_f64() * 1000.;
|
||||
assert_eq!(count(), decodes, "{label}");
|
||||
assert_update_warnings(&result);
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let mut pixels = blake3::Hasher::new();
|
||||
let mut sources = blake3::Hasher::new();
|
||||
let mut samples = Vec::new();
|
||||
for layer in doc.layers.iter().filter(|layer| matches!(&layer.content, LayerContent::Smart(_))) {
|
||||
let sm = smart(doc, layer.id).unwrap();
|
||||
let source = embedded(doc, sm).unwrap().1;
|
||||
assert_eq!(source, svg.as_bytes());
|
||||
sources.update(blake3::hash(source).as_bytes());
|
||||
let cache = sm.cache.as_ref().unwrap();
|
||||
for value in cache.read_region(doc.bounds()) {
|
||||
pixels.update(&value.to_bits().to_le_bytes());
|
||||
}
|
||||
let (x, y) = (sm.transform.m[4] as i32, sm.transform.m[5] as i32);
|
||||
let sample = cache.rgba(x + 3, y + 3);
|
||||
assert_eq!(sample, [0., 1., 0., 1.]);
|
||||
samples.push(sample);
|
||||
}
|
||||
assert_eq!(samples.len(), 100);
|
||||
let signature = (*pixels.finalize().as_bytes(), *sources.finalize().as_bytes(), samples, result["warnings"].clone());
|
||||
if let Some(expected) = &expected {
|
||||
assert_eq!(&signature, expected, "pixels, samples, encoded sources and warnings must match: {label}");
|
||||
} else {
|
||||
expected = Some(signature);
|
||||
}
|
||||
eprintln!("24MP PNG / 16x16 SVG / 100 shared instances: {label}: {elapsed:.3} ms, {decodes} source renders");
|
||||
timings.push(elapsed);
|
||||
}
|
||||
eprintln!("optimized median of two: {:.3} ms; memo-off: {:.3} ms", (timings[0] + timings[2]) / 2., timings[1]);
|
||||
assert_eq!(WARNING_SOURCE_LIMIT.with(std::cell::Cell::get), previous_limit, "test override restored");
|
||||
}
|
||||
@@ -419,13 +419,24 @@ fn original_vector_formats_save_atomically_and_survive_undo_and_persistence() {
|
||||
/// is accepted with that image left out, as placing the same SVG is.
|
||||
#[test]
|
||||
fn saved_svg_with_an_unreadable_embedded_image_still_updates() {
|
||||
let (mut s, id) = session(8);
|
||||
let edited = r#"<svg xmlns="http://www.w3.org/2000/svg" width="8" height="6"><image width="4" height="6" href="data:image/bmp,BM"/><rect x="4" width="4" height="6" fill="blue"/></svg>"#;
|
||||
let params = request(&mut s, id, edited);
|
||||
let result = s.execute(UPDATE, params).unwrap();
|
||||
assert_eq!(result["updated"], true);
|
||||
assert!(result["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().contains("embedded image was omitted")));
|
||||
assert_eq!(source(&s, id), edited.as_bytes());
|
||||
for href in ["data:image/bmp,BM", "data:image/png;base64,A"] {
|
||||
let (mut s, id) = session(8);
|
||||
let edited = format!(
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg" width="8" height="6"><image width="4" height="6" href="{href}"/><rect x="4" width="4" height="6" fill="blue"/></svg>"#
|
||||
);
|
||||
let params = request(&mut s, id, &edited);
|
||||
let result = s.execute(UPDATE, params).unwrap();
|
||||
assert_eq!(result["updated"], true);
|
||||
let warnings = result["warnings"].as_array().unwrap();
|
||||
assert_eq!(warnings.len(), 1, "{href}: {warnings:?}");
|
||||
assert!(warnings[0].as_str().unwrap().contains("embedded image was omitted"));
|
||||
assert_eq!(source(&s, id), edited.as_bytes());
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let sm = smart(doc, id).unwrap();
|
||||
assert_eq!(sm.cache.as_ref().unwrap().rgba(18, 11), [0.0, 0.0, 1.0, 1.0]);
|
||||
assert!(crate::smart_cmds::render(doc, sm).is_err(), "warning-bearing save must not populate strict caches: {href}");
|
||||
assert_eq!(source(&s, id), edited.as_bytes(), "strict refusal must leave the unrepaired source intact");
|
||||
}
|
||||
}
|
||||
|
||||
/// updateSvg replaces SVG contents only: it never turns an AI, PDF or EPS source into SVG.
|
||||
|
||||
+67
-193
@@ -10,7 +10,6 @@
|
||||
//! stops and direction in the layer's own gradient fill, with a warning that the exact geometry
|
||||
//! is not kept.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex, OnceLock, PoisonError};
|
||||
|
||||
use craft_svg::tree::draw::{self, tiny_skia};
|
||||
@@ -25,6 +24,9 @@ use photocraft_raster::Surface;
|
||||
|
||||
use crate::{ImportResult, IoError};
|
||||
|
||||
#[cfg(test)]
|
||||
mod xml_reuse_tests;
|
||||
|
||||
/// Hard caps on the memory resvg needs to draw a subtree, checked before it draws: craft-svg's
|
||||
/// `tree::draw`, within this crate's peak pixel budget.
|
||||
#[cfg(test)]
|
||||
@@ -300,18 +302,25 @@ fn fallback_face(c: char, used: &[usvg::fontdb::ID], db: &usvg::fontdb::Database
|
||||
.map(|f| f.id)
|
||||
}
|
||||
|
||||
/// usvg's font choice, with the chosen faces read from memory ([`in_memory`]).
|
||||
fn font_resolver() -> usvg::FontResolver<'static> {
|
||||
/// Load the same fonts for top-level and embedded text. usvg requests them only
|
||||
/// when it shapes text, including text hidden inside an encoded image URL.
|
||||
fn font_resolver(loaded: &OnceLock<Arc<usvg::fontdb::Database>>, load: fn() -> Arc<usvg::fontdb::Database>) -> usvg::FontResolver<'_> {
|
||||
let mut resolver = usvg::FontResolver::default();
|
||||
let select = resolver.select_font;
|
||||
resolver.select_font = Box::new(move |font, db| {
|
||||
if db.faces().next().is_none() {
|
||||
*db = Arc::clone(loaded.get_or_init(load));
|
||||
}
|
||||
let id = select(font, db)?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let id = in_memory(id, db);
|
||||
Some(id)
|
||||
});
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let select = usvg::FontResolver::default_font_selector();
|
||||
usvg::FontResolver {
|
||||
select_font: Box::new(move |font, db| select(font, db).map(|id| in_memory(id, db))),
|
||||
select_fallback: Box::new(|c, used, db| fallback_face(c, used, db).map(|id| in_memory(id, db))),
|
||||
}
|
||||
resolver.select_fallback = Box::new(|c, used, db| fallback_face(c, used, db).map(|id| in_memory(id, db)));
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
usvg::FontResolver::default()
|
||||
resolver
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -361,17 +370,17 @@ impl From<ParseFailure> for IoError {
|
||||
|
||||
/// Native parsing always reuses the complete-operation worker, including private probes.
|
||||
/// WASM has no worker stack, so shared outer/embedded XML depth is guarded inline.
|
||||
fn parse_at_depth<T: Send>(depth: usize, isolated: bool, parse: impl FnOnce(bool) -> Result<T, IoError> + Send) -> Result<T, ParseFailure> {
|
||||
fn parse_at_depth<T: Send>(depth: usize, parse: impl FnOnce() -> Result<T, IoError> + Send) -> Result<T, ParseFailure> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let _ = (depth, isolated);
|
||||
let _ = depth;
|
||||
// The nested Result distinguishes a worker failure from a normal parser error.
|
||||
on_svg_stack(|| Ok(parse(true))).map_err(ParseFailure::Isolation)?.map_err(ParseFailure::Decode)
|
||||
on_svg_stack(|| Ok(parse())).map_err(ParseFailure::Isolation)?.map_err(ParseFailure::Decode)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
if isolated || depth <= INLINE_XML_DEPTH {
|
||||
return parse(isolated).map_err(ParseFailure::Decode);
|
||||
if depth <= INLINE_XML_DEPTH {
|
||||
return parse().map_err(ParseFailure::Decode);
|
||||
}
|
||||
Err(ParseFailure::Isolation(IoError::Svg(format!(
|
||||
"SVG parsing deeper than {INLINE_XML_DEPTH} levels requires the native app's isolated parser; simplify the drawing or open it in PhotoCraft desktop"
|
||||
@@ -379,24 +388,19 @@ fn parse_at_depth<T: Send>(depth: usize, isolated: bool, parse: impl FnOnce(bool
|
||||
}
|
||||
}
|
||||
|
||||
/// Supplies bounded parsing options to direct resolver tests without changing their existing interface.
|
||||
#[cfg(test)]
|
||||
fn options(fontdb: Arc<usvg::fontdb::Database>, depth_left: usize) -> usvg::Options<'static> {
|
||||
options_with_isolation(fontdb, depth_left, false, Arc::new(craft_svg::tree::ImageResolver::new(tree_limits(depth_left, MAX_RASTER_PIXELS))))
|
||||
}
|
||||
|
||||
/// Parse options for an untrusted drawing. `<image href>` resolves only `data:` URLs: a file path
|
||||
/// or relative href is never read, so opening an SVG cannot pull local files (`/etc/...`,
|
||||
/// `~/.ssh/...`) into the document. Embedded SVG images get no `<image>` resolution at all
|
||||
/// (`usvg` already blanks it for sub-SVGs). An embedded SVG may nest `depth_left` levels: it is
|
||||
/// parsed on top of the drawing's own recursion, so the two depths share [`MAX_XML_DEPTH`].
|
||||
fn options_with_isolation(
|
||||
fontdb: Arc<usvg::fontdb::Database>,
|
||||
_depth_left: usize,
|
||||
_isolated: bool,
|
||||
image_resolver: Arc<craft_svg::tree::ImageResolver>,
|
||||
) -> usvg::Options<'static> {
|
||||
usvg::Options { resources_dir: None, image_href_resolver: image_resolver.resolver(), fontdb, font_resolver: font_resolver(), ..usvg::Options::default() }
|
||||
/// Font and resource options for untrusted drawings. The shared validated context
|
||||
/// installs its own bounded data-only image resolver during conversion.
|
||||
fn parse_checked_svg(
|
||||
text: &str,
|
||||
resolver: &craft_svg::tree::ValidatedImageResolver,
|
||||
load_fonts: fn() -> Arc<usvg::fontdb::Database>,
|
||||
) -> Result<(usvg::Tree, bool), IoError> {
|
||||
let checked = craft_svg::tree::preflight(text, &resolver.limits()).map_err(svg_parse_error)?;
|
||||
let loaded = OnceLock::new();
|
||||
let options = usvg::Options { resources_dir: None, font_resolver: font_resolver(&loaded, load_fonts), ..usvg::Options::default() };
|
||||
let tree = resolver.parse_preflight(checked, options).map_err(svg_parse_error)?;
|
||||
let fontless_text = loaded.get().is_some_and(|db| db.faces().next().is_none());
|
||||
Ok((tree, fontless_text))
|
||||
}
|
||||
|
||||
/// A parsed drawing.
|
||||
@@ -414,8 +418,8 @@ struct Parsed {
|
||||
|
||||
/// Parses a drawing, `.svgz` included. The XML is inflated and checked here, before the parser
|
||||
/// sees it: deeper nesting than [`MAX_XML_DEPTH`] is an error, not a stack overflow. The font
|
||||
/// database (and its one-off scan of the system fonts) is only loaded when the drawing may
|
||||
/// contain text.
|
||||
/// database (and its one-off scan of the system fonts) is loaded only when the drawing requests
|
||||
/// a text font.
|
||||
fn parse(bytes: &[u8]) -> Result<Parsed, IoError> {
|
||||
parse_with(bytes, fonts)
|
||||
}
|
||||
@@ -426,10 +430,14 @@ fn tree_limits(depth: usize, raster_pixels: u64) -> craft_svg::tree::Limits {
|
||||
// PhotoCraft's stack guard counts open tags; the shared SVG preflight counts every
|
||||
// element, including a self-closing leaf. Allow that leaf at the same guarded depth.
|
||||
depth: depth.saturating_add(1),
|
||||
// The shared default is intentionally conservative for general consumers. PhotoCraft
|
||||
// already guards XML nesting and reference expansion against its 512-level parser
|
||||
// budget, so pass that same explicit limit through instead of inheriting 256.
|
||||
tree_depth: depth.saturating_add(1),
|
||||
// Expanded references consume parser stack even when the source XML is
|
||||
// shallow. Native parsing owns the larger worker; WASM must keep both
|
||||
// reference expansion and lexical nesting within its inline allowance.
|
||||
tree_depth: {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let depth = depth.min(INLINE_XML_DEPTH);
|
||||
depth.saturating_add(1)
|
||||
},
|
||||
embedded_bytes: usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap_or(usize::MAX),
|
||||
embedded_depth: MAX_EMBEDDED_SVG_DEPTH,
|
||||
embedded_raster_pixels: raster_pixels.min(MAX_RASTER_PIXELS),
|
||||
@@ -448,23 +456,16 @@ fn parse_with_image_limit(bytes: &[u8], fonts: fn() -> Arc<usvg::fontdb::Databas
|
||||
if depth > MAX_XML_DEPTH {
|
||||
return Err(IoError::Svg(format!("elements are nested more than {MAX_XML_DEPTH} levels deep")));
|
||||
}
|
||||
let has_text = xml.windows(4).any(|w| w == b"text");
|
||||
let fontdb = if has_text { fonts() } else { Arc::new(usvg::fontdb::Database::new()) };
|
||||
let fontless_text = has_text && fontdb.faces().next().is_none();
|
||||
// Parse the already-bounded-and-decoded text with from_str, not from_data: from_data
|
||||
// Convert the already-bounded-and-decoded text through checked XML, never from_data:
|
||||
// re-checks the gzip magic and would decompress a second time without our size limit if
|
||||
// the inflated content happened to start with a nested gzip stream.
|
||||
let text = std::str::from_utf8(&xml).map_err(|_| IoError::Svg("the SVG is not valid UTF-8".into()))?;
|
||||
let image_resolver = Arc::new(craft_svg::tree::ImageResolver::new(tree_limits(MAX_XML_DEPTH, max_image_pixels)));
|
||||
image_resolver.set_document_depth(depth);
|
||||
parse_at_depth(depth, false, |isolated| {
|
||||
image_resolver.check_document(text).map_err(svg_parse_error)?;
|
||||
let opt = options_with_isolation(fontdb, MAX_XML_DEPTH.saturating_sub(depth), isolated, Arc::clone(&image_resolver));
|
||||
let tree = guarded_parse(|| craft_svg::tree::parse_text(text, &tree_limits(MAX_XML_DEPTH, max_image_pixels), &opt))?;
|
||||
let tree = tree.map_err(svg_parse_error)?;
|
||||
// The resolver already rejects each over-budget optional image before decode.
|
||||
// Ordinary Open keeps the usable outer drawing, as before the shared-parser
|
||||
// migration; strict stored previews enforce completeness in their separate path.
|
||||
let limits = tree_limits(MAX_XML_DEPTH, max_image_pixels);
|
||||
let image_resolver = craft_svg::tree::ValidatedImageResolver::new(limits);
|
||||
parse_at_depth(depth, || {
|
||||
let (tree, fontless_text) = guarded_parse(|| parse_checked_svg(text, &image_resolver, fonts))??;
|
||||
// Ordinary Open retains usable artwork and discloses optional omissions.
|
||||
// Strict stored previews enforce completeness in their separate path.
|
||||
Ok(Parsed { tree, fontless_text, image_usage: image_resolver.usage() })
|
||||
})
|
||||
.map_err(IoError::from)
|
||||
@@ -513,7 +514,7 @@ struct ImageBudget {
|
||||
/// stored display preview must draw completely, or it isn't used.
|
||||
strict: bool,
|
||||
error: Option<String>,
|
||||
resolver: Arc<craft_svg::tree::ImageResolver>,
|
||||
resolver: Arc<craft_svg::tree::ValidatedImageResolver>,
|
||||
}
|
||||
|
||||
/// Record the first budget or decoding failure seen while usvg resolves images. Resolver
|
||||
@@ -526,13 +527,7 @@ fn image_error(budget: &Mutex<ImageBudget>, error: impl Into<String>) {
|
||||
}
|
||||
|
||||
/// Parse one bounded SVG with the shared tree parser and its format-neutral image resolver.
|
||||
fn parse_image_bounded_with(
|
||||
bytes: &[u8],
|
||||
budget: Arc<Mutex<ImageBudget>>,
|
||||
xml_left: usize,
|
||||
fonts: fn() -> Arc<usvg::fontdb::Database>,
|
||||
isolated: bool,
|
||||
) -> Result<usvg::Tree, IoError> {
|
||||
fn parse_image_bounded_with(bytes: &[u8], budget: Arc<Mutex<ImageBudget>>, fonts: fn() -> Arc<usvg::fontdb::Database>) -> Result<usvg::Tree, IoError> {
|
||||
let overrun = |error: IoError| {
|
||||
image_error(&budget, error.to_string());
|
||||
error
|
||||
@@ -541,28 +536,17 @@ fn parse_image_bounded_with(
|
||||
return Err(overrun(IoError::Svg("embedded SVG source exceeds the 16 MiB byte limit".into())));
|
||||
}
|
||||
let xml = inflate_with_limit(bytes, MAX_BOUNDED_SVG_BYTES).map_err(overrun)?;
|
||||
let nesting = nesting_depth(&xml, xml_left)?;
|
||||
if nesting > xml_left {
|
||||
return Err(IoError::Svg(format!("elements are nested more than {xml_left} levels deep")));
|
||||
let depth = nesting_depth(&xml, MAX_XML_DEPTH)?;
|
||||
if depth > MAX_XML_DEPTH {
|
||||
return Err(IoError::Svg(format!("elements are nested more than {MAX_XML_DEPTH} levels deep")));
|
||||
}
|
||||
let shared_depth = MAX_XML_DEPTH.saturating_sub(xml_left).saturating_add(nesting);
|
||||
budget.lock().unwrap_or_else(PoisonError::into_inner).resolver.set_document_depth(nesting);
|
||||
parse_at_depth(shared_depth, isolated, |isolated| {
|
||||
let has_text = xml.windows(4).any(|w| w == b"text");
|
||||
let fontdb = if has_text { fonts() } else { Arc::new(usvg::fontdb::Database::new()) };
|
||||
if has_text && fontdb.faces().next().is_none() {
|
||||
budget.lock().unwrap_or_else(PoisonError::into_inner).fontless_text = true;
|
||||
}
|
||||
parse_at_depth(depth, || {
|
||||
let resolver = Arc::clone(&budget.lock().unwrap_or_else(PoisonError::into_inner).resolver);
|
||||
let text = std::str::from_utf8(&xml).map_err(|_| IoError::Svg("the SVG is not valid UTF-8".into()))?;
|
||||
resolver.check_document(text).map_err(svg_parse_error)?;
|
||||
let refusals = Arc::new(SvgRefusals::default());
|
||||
let mut opt = options_with_isolation(fontdb, xml_left, isolated, Arc::clone(&resolver));
|
||||
opt.image_href_resolver = noting_refusals(opt.image_href_resolver, resolver, Arc::clone(&refusals));
|
||||
let limits = tree_limits(xml_left, MAX_RASTER_PIXELS);
|
||||
let tree = guarded_parse(|| craft_svg::tree::parse_text(text, &limits, &opt))?;
|
||||
let tree = tree.map_err(svg_parse_error)?;
|
||||
let (tree, fontless_text) =
|
||||
guarded_parse(|| parse_checked_svg(text, &resolver, fonts)).inspect_err(|error| image_error(&budget, error.to_string()))??;
|
||||
let mut counter = budget.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
counter.fontless_text |= fontless_text;
|
||||
let usage = counter.resolver.usage();
|
||||
counter.pixels = counter.pixels.saturating_sub(usage.raster_pixels);
|
||||
counter.xml_bytes = counter.xml_bytes.saturating_sub(u64::try_from(usage.embedded_svg_bytes).unwrap_or(u64::MAX));
|
||||
@@ -571,8 +555,8 @@ fn parse_image_bounded_with(
|
||||
// Resource overruns fail bounded renders; non-strict renders may omit a child
|
||||
// whose XML depth exceeds the allowance left by the outer drawing.
|
||||
if usage.raster_limit_images > 0 {
|
||||
counter.error.get_or_insert_with(|| "embedded SVG raster images exceed the decoded pixel budget".into());
|
||||
} else if usage.svg_limit_images > 0 && (counter.strict || refusals.over_budget.load(Ordering::Relaxed)) {
|
||||
counter.error.get_or_insert_with(|| "embedded SVG raster images exceed the encoded byte or decoded pixel budget".into());
|
||||
} else if usage.svg_resource_limit_images > 0 || (counter.strict && usage.svg_limit_images > 0) {
|
||||
counter.error.get_or_insert_with(|| "embedded SVG images exceed the byte or nesting budget".into());
|
||||
} else if counter.strict && (usage.invalid_raster_images > 0 || usage.invalid_svg_images > 0) {
|
||||
counter.error.get_or_insert_with(|| "can't parse an embedded SVG image".into());
|
||||
@@ -598,116 +582,6 @@ fn parse_image_bounded_with(
|
||||
})
|
||||
}
|
||||
|
||||
/// Why craft-svg left embedded SVG images out of one bounded parse. Its usage counts every
|
||||
/// refused SVG image together, but an image past [`MAX_EMBEDDED_SVG_DEPTH`] or the byte budget
|
||||
/// fails even a non-strict parse, as the bounded path did before craft-svg, while one nested
|
||||
/// too deeply in XML or that can't be unpacked (gzip inside gzip, a damaged stream, an unknown
|
||||
/// encoding) only leaves itself out.
|
||||
#[derive(Default)]
|
||||
struct SvgRefusals {
|
||||
/// Embedded SVG images being resolved around the current one.
|
||||
depth: AtomicUsize,
|
||||
/// An image was refused for the chain depth or the byte budget.
|
||||
over_budget: AtomicBool,
|
||||
/// Bytes craft-svg charged images that couldn't be unpacked: it charges such an image
|
||||
/// everything the byte budget had left, and so does a readable image that fits the budget
|
||||
/// only without those charges. Neither is the drawing's content.
|
||||
unread_charges: AtomicUsize,
|
||||
/// Bytes unpacked again to tell those images apart, at most one byte budget per drawing.
|
||||
rechecked: AtomicUsize,
|
||||
/// Source bytes decoded during refusal rechecks, at most one byte budget per drawing.
|
||||
text_rechecked: AtomicUsize,
|
||||
}
|
||||
|
||||
/// Note in `refusals` what craft-svg will do with an embedded SVG's `data` when its images have
|
||||
/// `used` bytes of the budget: it unpacks the data within what is left, accepts it and charges
|
||||
/// its text (decoded Latin-1 can be longer), or refuses it and charges everything left. Refused
|
||||
/// for size after unreadable images were charged, the data is unpacked once more within the
|
||||
/// budget less only the drawing's content; past that, or past what may be unpacked again, the
|
||||
/// drawing is over budget.
|
||||
fn note_byte_refusal(data: &[u8], used: usize, refusals: &SvgRefusals) {
|
||||
let budget = usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap_or(usize::MAX);
|
||||
let left = budget.saturating_sub(used);
|
||||
let unread = refusals.unread_charges.load(Ordering::Relaxed);
|
||||
match craft_svg::svgz::text(data, left) {
|
||||
Ok(text) if text.len() > left => refusals.over_budget.store(true, Ordering::Relaxed),
|
||||
Ok(_) => {}
|
||||
Err(craft_svg::svgz::Error::TooLarge(_)) if unread == 0 => refusals.over_budget.store(true, Ordering::Relaxed),
|
||||
Err(craft_svg::svgz::Error::TooLarge(_)) => {
|
||||
let content_left = budget.saturating_sub(used.saturating_sub(unread));
|
||||
// Rechecks may inflate or allocate text for a legacy encoding.
|
||||
let compressed = data.starts_with(&[0x1f, 0x8b]);
|
||||
let limit = if compressed { content_left.min(budget.saturating_sub(refusals.rechecked.load(Ordering::Relaxed))) } else { content_left };
|
||||
// Bound inflation and text-conversion work independently from content size.
|
||||
let (unpacked, too_large) = match craft_svg::svgz::inflate(data, limit) {
|
||||
Ok(xml) => {
|
||||
// Inflation and text conversion have separate work allowances: a fitting
|
||||
// Latin-1 document must not spend its UTF-8 size against inflation bytes.
|
||||
// Charge source bytes before decoding, including invalid encodings. The
|
||||
// supported encodings expand by at most 2x, so conversion work stays bounded
|
||||
// even for plain children that do not require inflation.
|
||||
let text_left = budget.saturating_sub(refusals.text_rechecked.load(Ordering::Relaxed));
|
||||
let too_large = if xml.len() > text_left {
|
||||
true
|
||||
} else {
|
||||
refusals.text_rechecked.fetch_add(xml.len(), Ordering::Relaxed);
|
||||
craft_svg::svgz::decode(&xml).is_ok_and(|text| text.len() > content_left)
|
||||
};
|
||||
(if compressed { xml.len() } else { 0 }, too_large)
|
||||
}
|
||||
Err(craft_svg::svgz::Error::Nested(len)) => (len, false),
|
||||
Err(craft_svg::svgz::Error::TooLarge(_)) => (if compressed { limit } else { 0 }, true),
|
||||
Err(_) => (limit, false),
|
||||
};
|
||||
refusals.rechecked.fetch_add(unpacked, Ordering::Relaxed);
|
||||
if too_large {
|
||||
refusals.over_budget.store(true, Ordering::Relaxed);
|
||||
} else {
|
||||
refusals.unread_charges.fetch_add(left, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
refusals.unread_charges.fetch_add(left, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `resolve`, also noting in `refusals` why craft-svg is about to refuse each embedded SVG. It
|
||||
/// repeats craft-svg's first two checks in their order: the chain depth, then unpacking within
|
||||
/// what the byte budget has left (`text/plain` data is an SVG unless it is a raster craft-svg
|
||||
/// draws).
|
||||
fn noting_refusals(
|
||||
resolve: usvg::ImageHrefResolver<'static>,
|
||||
resolver: Arc<craft_svg::tree::ImageResolver>,
|
||||
refusals: Arc<SvgRefusals>,
|
||||
) -> usvg::ImageHrefResolver<'static> {
|
||||
let usvg::ImageHrefResolver { resolve_data, resolve_string } = resolve;
|
||||
usvg::ImageHrefResolver {
|
||||
resolve_data: Box::new(move |mime, data, options| {
|
||||
let raster = matches!(
|
||||
photocraft_codecs::detect(&data),
|
||||
Some(photocraft_codecs::Format::Png | photocraft_codecs::Format::Jpeg | photocraft_codecs::Format::Gif | photocraft_codecs::Format::WebP)
|
||||
);
|
||||
if !(mime == "image/svg+xml" || (mime == "text/plain" && !raster)) {
|
||||
return resolve_data(mime, data, options);
|
||||
}
|
||||
// Once over budget the drawing fails whatever follows, so nothing more is unpacked.
|
||||
if !refusals.over_budget.load(Ordering::Relaxed) {
|
||||
if refusals.depth.load(Ordering::Relaxed) >= MAX_EMBEDDED_SVG_DEPTH {
|
||||
refusals.over_budget.store(true, Ordering::Relaxed);
|
||||
} else {
|
||||
note_byte_refusal(&data, resolver.usage().embedded_svg_bytes, &refusals);
|
||||
}
|
||||
}
|
||||
refusals.depth.fetch_add(1, Ordering::Relaxed);
|
||||
let image = resolve_data(mime, data, options);
|
||||
refusals.depth.fetch_sub(1, Ordering::Relaxed);
|
||||
image
|
||||
}),
|
||||
resolve_string,
|
||||
}
|
||||
}
|
||||
|
||||
/// Used paint/effect resources can decode an image once per use. Ordinary paths, gradients,
|
||||
/// clipping paths and blur filters without image references do not multiply unrelated photos.
|
||||
fn repeatable_image_resources(tree: &usvg::Tree) -> (bool, bool) {
|
||||
@@ -809,9 +683,9 @@ fn parse_bounded_work_with(bytes: &[u8], max_pixels: u64, strict: bool, fonts: f
|
||||
fontless_text: false,
|
||||
strict,
|
||||
error: None,
|
||||
resolver: Arc::new(craft_svg::tree::ImageResolver::new(tree_limits(MAX_XML_DEPTH, max_pixels.min(MAX_RASTER_PIXELS)))),
|
||||
resolver: Arc::new(craft_svg::tree::ValidatedImageResolver::new(tree_limits(MAX_XML_DEPTH, max_pixels.min(MAX_RASTER_PIXELS)))),
|
||||
}));
|
||||
let tree = parse_image_bounded_with(bytes, budget.clone(), MAX_XML_DEPTH, fonts, false)?;
|
||||
let tree = parse_image_bounded_with(bytes, budget.clone(), fonts)?;
|
||||
let counter = budget.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let decoded = max_pixels.min(MAX_RASTER_PIXELS).saturating_sub(counter.pixels);
|
||||
Ok(ParsedWork {
|
||||
|
||||
@@ -1,6 +1,254 @@
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
fn assert_incomplete_image_is_disclosed(text: &str) {
|
||||
for bytes in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
let imported = crate::import("damaged.svg", &bytes).unwrap();
|
||||
assert_omission_warning(&imported, 1);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [1., 0., 0., 1.]);
|
||||
let bounded = import_svg_bounded_images("damaged.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
assert_omission_warning(&bounded, 1);
|
||||
assert!(import_svg_preview_with_work("damaged.svg", &bytes, MAX_RASTER_PIXELS).is_err());
|
||||
assert!(import_svg_bounded_images_strict("damaged.svg", &bytes, MAX_RASTER_PIXELS).is_err());
|
||||
assert!(rasterize(&bytes, 1.).is_err());
|
||||
assert!(rasterize_bounded(&bytes, 1., MAX_RASTER_PIXELS).is_err());
|
||||
let (pixels, warnings) = rasterize_bounded_with_warnings(&bytes, 1., MAX_RASTER_PIXELS).unwrap();
|
||||
assert_eq!(pixels.get(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
assert!(warnings[0].starts_with("SVG: 1 embedded "));
|
||||
assert!(warnings[0].len() < 256);
|
||||
assert!(!warnings[0].contains("data:"));
|
||||
// A warning-aware parse cannot make a later strict render accept an omission.
|
||||
assert!(rasterize_bounded(&bytes, 1., MAX_RASTER_PIXELS).is_err());
|
||||
assert!(rasterize_rgba8(&bytes).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_twelve_bit_jpeg_rejected_by_the_svg_renderer_is_disclosed() {
|
||||
// ISO/IEC 10918-1 marker syntax: one 8x8, 12-bit gray DCT block.
|
||||
// Unit quantization, zero DC difference and EOB yield a constant mid-gray.
|
||||
// This is generated data, not a camera or third-party binary fixture.
|
||||
fn segment(out: &mut Vec<u8>, marker: u8, payload: &[u8]) {
|
||||
out.extend_from_slice(&[0xff, marker]);
|
||||
out.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes());
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
let mut jpeg = vec![0xff, 0xd8];
|
||||
let mut quantization = vec![0];
|
||||
quantization.extend_from_slice(&[1; 64]);
|
||||
segment(&mut jpeg, 0xdb, &quantization);
|
||||
segment(&mut jpeg, 0xc1, &[12, 0, 8, 0, 8, 1, 1, 0x11, 0]);
|
||||
let mut tables = Vec::new();
|
||||
for class in [0x00, 0x10] {
|
||||
tables.extend_from_slice(&[class, 1]);
|
||||
tables.extend_from_slice(&[0; 15]);
|
||||
tables.push(0); // One one-bit code: DC category zero / AC end-of-block.
|
||||
}
|
||||
segment(&mut jpeg, 0xc4, &tables);
|
||||
segment(&mut jpeg, 0xda, &[1, 1, 0, 0, 63, 0]);
|
||||
jpeg.extend_from_slice(&[0x3f, 0xff, 0xd9]); // DC=0, EOB=0, then six pad bits.
|
||||
let decoded = photocraft_codecs::decode(&jpeg).expect("valid extended sequential JPEG");
|
||||
assert_eq!(decoded.dimensions(), (8, 8));
|
||||
// Match resvg's actual JPEG backend/output policy, rather than the broader
|
||||
// shared codec which also supports valid 12-bit and arithmetic JPEG files.
|
||||
let kind = usvg::ImageKind::JPEG(Arc::new(jpeg.clone()));
|
||||
let mut pixels_left = MAX_RASTER_PIXELS;
|
||||
assert_eq!(
|
||||
draw::validate_raster(&kind, MAX_RASTER_PIXELS, &mut pixels_left),
|
||||
Err(draw::RasterValidationFailure::Invalid),
|
||||
"fixture must exercise the shared renderer capability gap"
|
||||
);
|
||||
for mime in ["image/jpeg", "text/plain"] {
|
||||
let direct = drawing(&url(mime, &jpeg));
|
||||
assert_incomplete_image_is_disclosed(&direct);
|
||||
for child in [direct.as_bytes().to_vec(), gzip(direct.as_bytes())] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_png_pixels_with_valid_dimensions_are_disclosed_before_rasterization() {
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let mut png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let mut at = 8;
|
||||
loop {
|
||||
let length = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize;
|
||||
if &png[at + 4..at + 8] == b"IDAT" {
|
||||
assert!(length >= 2);
|
||||
png[at + 8..at + 10].fill(0); // Invalid zlib header, with an honest chunk CRC.
|
||||
let checksum = crc32fast::hash(&png[at + 4..at + 8 + length]);
|
||||
png[at + 8 + length..at + 12 + length].copy_from_slice(&checksum.to_be_bytes());
|
||||
break;
|
||||
}
|
||||
at += length + 12;
|
||||
}
|
||||
assert_eq!(photocraft_codecs::probe_allocation_size(&png).unwrap(), (2, 2));
|
||||
assert!(tiny_skia::Pixmap::decode_png(&png).is_err());
|
||||
let direct = drawing(&url("image/png", &png));
|
||||
assert_incomplete_image_is_disclosed(&direct);
|
||||
for child in [direct.as_bytes().to_vec(), gzip(direct.as_bytes())] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_data_image_hrefs_are_disclosed_before_the_parser_discards_them() {
|
||||
let direct = drawing("data:image/png;base64,A");
|
||||
assert_incomplete_image_is_disclosed(&direct);
|
||||
assert_incomplete_image_is_disclosed(&direct.replace("href=", "xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:href="));
|
||||
assert_incomplete_image_is_disclosed(&direct.replace("base64,A", "base64,A"));
|
||||
for child in [direct.as_bytes().to_vec(), gzip(direct.as_bytes())] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_no_omission_or_unexpected_import_warning(imported: &ImportResult) {
|
||||
assert!(omission_warnings(imported).is_empty(), "{:?}", imported.warnings);
|
||||
assert!(
|
||||
imported.warnings.iter().all(|warning| warning == "SVG: embedded images are pixel layers"),
|
||||
"only the existing pixel-layer information is expected: {:?}",
|
||||
imported.warnings
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_ignored_data_image_stays_quiet(text: &str) {
|
||||
for bytes in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
let imported = crate::import("ignored.svg", &bytes).unwrap();
|
||||
assert_no_omission_or_unexpected_import_warning(&imported);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [1., 0., 0., 1.]);
|
||||
let strict = import_svg_bounded_images_strict("ignored.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
assert_no_omission_or_unexpected_import_warning(&strict);
|
||||
import_svg_preview_with_work("ignored.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
let (pixels, warnings) = rasterize_bounded_with_warnings(&bytes, 1., MAX_RASTER_PIXELS).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
assert_eq!(pixels.get(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(rasterize_bounded(&bytes, 1., MAX_RASTER_PIXELS).unwrap().get(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(rasterize(&bytes, 1.).unwrap().get(1, 1), [1., 0., 0., 1.]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_non_image_data_hrefs_stay_ignored_in_open_and_strict_rendering() {
|
||||
for href in [
|
||||
"data:application/octet-stream;base64,A",
|
||||
"data:application/octet-stream;base64",
|
||||
"data:APPLICATION/OCTET-STREAM;BASE64,A",
|
||||
"data:text/css;base64,A",
|
||||
"data:application/octet-stream;base64#fragment,not-the-delimiter",
|
||||
] {
|
||||
let text = drawing(href);
|
||||
assert_ignored_data_image_stays_quiet(&text);
|
||||
assert_ignored_data_image_stays_quiet(&text.replace("href=", "xmlns:x='http://www.w3.org/1999/xlink' x:href="));
|
||||
for child in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
assert_ignored_data_image_stays_quiet(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_and_explicit_plain_data_failures_keep_image_omission_policy() {
|
||||
for href in ["data:text/plain;base64,A", "data:;base64,A", "data:;base64", "data:image/png;base64"] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(href));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignored_non_image_repair_preserves_hidden_foreign_and_valid_image_controls() {
|
||||
for content in [
|
||||
"<image display='none' width='2' height='2' href='data:application/octet-stream;base64'/>",
|
||||
"<foreign:image xmlns:foreign='urn:unrelated' width='2' height='2' href='data:application/octet-stream;base64,A'/>",
|
||||
"<image width='2' height='2' xmlns:x='http://www.w3.org/1999/xlink' x:href='data:image/png;base64,A' href='data:application/octet-stream;base64,A'/>",
|
||||
] {
|
||||
let text = format!("<svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><rect width='2' height='2' fill='red'/>{content}</svg>");
|
||||
assert_ignored_data_image_stays_quiet(&text);
|
||||
}
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
let text = format!(
|
||||
"<svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><image width='2' height='2' href='{}'/><image width='2' height='2' href='data:application/octet-stream;base64,A'/></svg>",
|
||||
url("image/png", &png)
|
||||
);
|
||||
let imported = crate::import("mixed.svg", text.as_bytes()).unwrap();
|
||||
assert_eq!(imported.warnings, vec!["SVG: embedded images are pixel layers"]);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [0., 1., 0., 1.]);
|
||||
let strict = import_svg_bounded_images_strict("mixed.svg", text.as_bytes(), MAX_RASTER_PIXELS).unwrap();
|
||||
assert_eq!(strict.warnings, vec!["SVG: embedded images are pixel layers"]);
|
||||
assert_eq!(rasterize_bounded(text.as_bytes(), 1., MAX_RASTER_PIXELS).unwrap().get(1, 1), [0., 1., 0., 1.]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_nested_declarations_are_not_removed_into_valid_images() {
|
||||
let child = b"<?xml bogus?><svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><rect width='2' height='2' fill='green'/></svg>";
|
||||
for bytes in [child.to_vec(), gzip(child)] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url("image/svg+xml", &bytes)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn used_data_images_with_mismatched_image_mime_are_disclosed() {
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (2, 2));
|
||||
for mime in ["image/jpeg", "image/jpg"] {
|
||||
let direct = drawing(&url(mime, &png));
|
||||
assert_incomplete_image_is_disclosed(&direct);
|
||||
for child in [direct.as_bytes().to_vec(), gzip(direct.as_bytes())] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_renderer_raster_formats_and_plain_text_sniffing_stay_usable() {
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
for (format, mime) in [
|
||||
(photocraft_codecs::Format::Png, "image/png"),
|
||||
(photocraft_codecs::Format::Jpeg, "image/jpeg"),
|
||||
(photocraft_codecs::Format::Gif, "image/gif"),
|
||||
(photocraft_codecs::Format::WebP, "image/webp"),
|
||||
] {
|
||||
let encoded = photocraft_codecs::encode(&image, format, &Default::default()).unwrap();
|
||||
for mime in [mime, "text/plain"] {
|
||||
let text = drawing(&url(mime, &encoded));
|
||||
let imported = crate::import("complete.svg", text.as_bytes()).unwrap();
|
||||
assert!(omission_warnings(&imported).is_empty(), "{format:?}: {:?}", imported.warnings);
|
||||
let pixel = rasterize_bounded(text.as_bytes(), 1., MAX_RASTER_PIXELS).unwrap().get(1, 1);
|
||||
assert!(pixel[1] > 0.8 && pixel[0] < 0.2 && pixel[2] < 0.2 && pixel[3] == 1., "{format:?}: {pixel:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unused_hidden_and_unselected_href_failures_do_not_acquire_warnings() {
|
||||
for content in [
|
||||
"<defs><image id='unused' width='2' height='2' href='data:image/png;base64,A'/></defs>",
|
||||
"<image display='none' width='2' height='2' href='data:image/png;base64,A'/>",
|
||||
"<image width='2' height='2' xmlns:x='http://www.w3.org/1999/xlink' x:href='data:image/png;base64,A' href='application-ignored'/>",
|
||||
"<foreign:image xmlns:foreign='urn:unrelated' width='2' height='2' href='data:image/png;base64,A'/>",
|
||||
] {
|
||||
let text = format!("<svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><rect width='2' height='2' fill='red'/>{content}</svg>");
|
||||
for source_text in [text.clone(), drawing(&url("image/svg+xml", text.as_bytes())), drawing(&url("image/svg+xml", &gzip(text.as_bytes())))] {
|
||||
for bytes in [source_text.as_bytes().to_vec(), gzip(source_text.as_bytes())] {
|
||||
let imported = crate::import("unused.svg", &bytes).unwrap();
|
||||
assert!(omission_warnings(&imported).is_empty(), "{content}: {:?}", imported.warnings);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [1., 0., 0., 1.]);
|
||||
let imported = import_svg_bounded_images("unused.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
assert!(omission_warnings(&imported).is_empty());
|
||||
let (pixels, warnings) = rasterize_bounded_with_warnings(&bytes, 1., MAX_RASTER_PIXELS).unwrap();
|
||||
assert!(warnings.is_empty(), "{content}: {warnings:?}");
|
||||
assert_eq!(pixels.get(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(rasterize_bounded(&bytes, 1., MAX_RASTER_PIXELS).unwrap().get(1, 1), [1., 0., 0., 1.]);
|
||||
assert_eq!(rasterize(&bytes, 1.).unwrap().get(1, 1), [1., 0., 0., 1.]);
|
||||
rasterize_rgba8(&bytes).unwrap();
|
||||
import_svg_preview_with_work("unused.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
import_svg_bounded_images_strict("unused.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn url(mime: &str, bytes: &[u8]) -> String {
|
||||
let mut value = format!("data:{mime},");
|
||||
for byte in bytes {
|
||||
@@ -9,6 +257,23 @@ fn url(mime: &str, bytes: &[u8]) -> String {
|
||||
value
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svgz_content_detection_and_parser_agree_on_later_gzip_members() {
|
||||
let xml = b"<svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><rect width='2' height='2'/></svg>";
|
||||
// The first member alone looks like SVG. Detection must validate the
|
||||
// following member as well, even when there is no filename extension.
|
||||
let split = xml.len() - b"</svg>".len();
|
||||
let mut compressed = gzip(&xml[..split]);
|
||||
compressed.extend(gzip(&xml[split..]));
|
||||
assert!(crate::is_svg_input("untitled", &compressed));
|
||||
assert!(rasterize_bounded(&compressed, 1., 16).is_ok());
|
||||
|
||||
let crc = compressed.len() - 8;
|
||||
compressed[crc] ^= 1;
|
||||
assert!(!crate::is_svg_input("untitled", &compressed));
|
||||
assert!(rasterize_bounded(&compressed, 1., 16).is_err());
|
||||
}
|
||||
|
||||
fn gzip(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut writer = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
writer.write_all(bytes).unwrap();
|
||||
@@ -215,3 +480,70 @@ fn malformed_data_urls_and_broken_pixel_payloads_are_disclosed_before_rendering(
|
||||
assert!(import_svg_preview_with_work("strict.svg", text.as_bytes(), MAX_RASTER_PIXELS).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn excessive_mime_parameters_preserve_valid_images_and_disclose_only_real_failures() {
|
||||
fn assert_complete_green_image(text: &str) {
|
||||
for bytes in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
let imported = crate::import("valid.svg", &bytes).unwrap();
|
||||
assert_no_omission_or_unexpected_import_warning(&imported);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [0., 1., 0., 1.]);
|
||||
let strict = import_svg_bounded_images_strict("valid.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
assert_no_omission_or_unexpected_import_warning(&strict);
|
||||
assert_eq!(photocraft_compose::flatten(&strict.document).get(1, 1), [0., 1., 0., 1.]);
|
||||
import_svg_preview_with_work("valid.svg", &bytes, MAX_RASTER_PIXELS).unwrap();
|
||||
let (pixels, warnings) = rasterize_bounded_with_warnings(&bytes, 1., MAX_RASTER_PIXELS).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
assert_eq!(pixels.get(1, 1), [0., 1., 0., 1.]);
|
||||
assert_eq!(rasterize_bounded(&bytes, 1., MAX_RASTER_PIXELS).unwrap().get(1, 1), [0., 1., 0., 1.]);
|
||||
assert_eq!(rasterize(&bytes, 1.).unwrap().get(1, 1), [0., 1., 0., 1.]);
|
||||
let (width, height, pixels) = rasterize_rgba8(&bytes).unwrap();
|
||||
assert_eq!((width, height), (2, 2));
|
||||
assert_eq!(pixels, [0, 255, 0, 255].repeat(4));
|
||||
}
|
||||
}
|
||||
let parameters = (0..65).map(|index| format!(";p{index}=x")).collect::<String>();
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
for essence in ["image/png", "text/plain", ""] {
|
||||
let href = url(&format!("{essence}{parameters}"), &png);
|
||||
let text = drawing(&href);
|
||||
assert_complete_green_image(&text);
|
||||
for child in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
assert_complete_green_image(&drawing(&url("image/svg+xml", &child)));
|
||||
}
|
||||
// Header compaction cannot turn a malformed base64 body or a missing
|
||||
// delimiter into a valid image, including default text/plain MIME.
|
||||
for href in [format!("data:{essence}{parameters};base64,A"), format!("data:{essence}{parameters};base64")] {
|
||||
assert_incomplete_image_is_disclosed(&drawing(&href));
|
||||
}
|
||||
}
|
||||
assert_incomplete_image_is_disclosed(&drawing(&url(&format!("image/unsupported{parameters}"), &png)));
|
||||
let href = format!("data:application/octet-stream{parameters};base64,AA==");
|
||||
assert_ignored_data_image_stays_quiet(&drawing(&href));
|
||||
let image = url(&format!("image/png{parameters}"), &png);
|
||||
for content in [
|
||||
format!("<defs><image id='unused' href='{image}'/></defs>"),
|
||||
format!("<image display='none' width='2' height='2' href='{image}'/>"),
|
||||
format!("<foreign:image xmlns:foreign='urn:unrelated' width='2' height='2' href='{image}'/>"),
|
||||
format!("<image width='2' height='2' xmlns:x='http://www.w3.org/1999/xlink' x:href='{image}' href='{href}'/>"),
|
||||
] {
|
||||
let text = format!("<svg xmlns='http://www.w3.org/2000/svg' width='2' height='2'><rect width='2' height='2' fill='red'/>{content}</svg>");
|
||||
assert_ignored_data_image_stays_quiet(&text);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_image_at_the_mime_parameter_boundary_keeps_its_pixels() {
|
||||
let image = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, [0, 255, 0].repeat(4)).unwrap();
|
||||
let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
// This helper percent-encodes the payload; 64 parameters hit the separator boundary.
|
||||
let parameters = (0..64).map(|index| format!(";p{index}=x")).collect::<String>();
|
||||
let text = drawing(&url(&format!("image/png{parameters}"), &png));
|
||||
for bytes in [text.as_bytes().to_vec(), gzip(text.as_bytes())] {
|
||||
let imported = crate::import("valid.svg", &bytes).unwrap();
|
||||
assert_eq!(imported.warnings, vec!["SVG: embedded images are pixel layers"]);
|
||||
assert_eq!(photocraft_compose::flatten(&imported.document).get(1, 1), [0., 1., 0., 1.]);
|
||||
assert_eq!(rasterize(&bytes, 1.).unwrap().get(1, 1), [0., 1., 0., 1.]);
|
||||
}
|
||||
}
|
||||
+155
-64
@@ -5,13 +5,11 @@ use super::*;
|
||||
/// Require shallow, deep and nested private SVG probes to reuse the single native parser worker.
|
||||
fn private_svg_parsing_uses_the_same_worker_at_every_depth() {
|
||||
for depth in [2, 200] {
|
||||
parse_at_depth(depth, false, |isolated| {
|
||||
assert!(isolated);
|
||||
parse_at_depth(depth, || {
|
||||
assert!(SVG_WORKER.with(std::cell::Cell::get));
|
||||
assert_eq!(std::thread::current().name(), Some("photocraft-svg"));
|
||||
let worker = std::thread::current().id();
|
||||
parse_at_depth(200, isolated, |nested| {
|
||||
assert!(nested);
|
||||
parse_at_depth(200, || {
|
||||
assert_eq!(std::thread::current().id(), worker);
|
||||
Ok(())
|
||||
})
|
||||
@@ -40,17 +38,48 @@ fn non_strict_embedded_svg_records_isolation_failure_but_omits_invalid_images()
|
||||
fontless_text: false,
|
||||
strict: false,
|
||||
error: None,
|
||||
resolver: Arc::new(craft_svg::tree::ImageResolver::new(tree_limits(MAX_XML_DEPTH, MAX_RASTER_PIXELS))),
|
||||
resolver: Arc::new(craft_svg::tree::ValidatedImageResolver::new(tree_limits(MAX_XML_DEPTH, MAX_RASTER_PIXELS))),
|
||||
}));
|
||||
let invalid = image_svg(&embedded_image("image/svg+xml", b"<not-svg/>"));
|
||||
parse_image_bounded_with(invalid.as_bytes(), budget.clone(), MAX_XML_DEPTH, inter_only, false).unwrap();
|
||||
parse_image_bounded_with(invalid.as_bytes(), budget.clone(), inter_only).unwrap();
|
||||
assert!(budget.lock().unwrap().error.is_none(), "ordinary invalid images are still omitted");
|
||||
let svg = image_svg("<text>x</text>");
|
||||
let error = parse_image_bounded_with(svg.as_bytes(), budget.clone(), 2, panic_fonts, false).unwrap_err();
|
||||
let error = parse_image_bounded_with(svg.as_bytes(), budget.clone(), panic_fonts).unwrap_err();
|
||||
assert!(error.to_string().contains("bounded SVG worker failed"));
|
||||
assert_eq!(budget.lock().unwrap().error.as_deref(), Some(error.to_string().as_str()));
|
||||
}
|
||||
|
||||
/// Shallow declarations can expand to a much deeper parser walk through references.
|
||||
fn reference_chain_svg(links: usize) -> String {
|
||||
let mut source = String::from("<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><defs>");
|
||||
for index in 0..links {
|
||||
source.push_str(&format!("<g id='n{index}'><use href='#n{}'/></g>", index + 1));
|
||||
}
|
||||
source.push_str(&format!("<g id='n{links}'><rect width='1' height='1'/></g></defs><use href='#n0'/></svg>"));
|
||||
source
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounded_render_rejects_expanded_references_that_exceed_the_app_limit() {
|
||||
let depth = tree_limits(MAX_XML_DEPTH, MAX_RASTER_PIXELS).tree_depth;
|
||||
let source = reference_chain_svg(depth + 2);
|
||||
assert!(nesting_depth(source.as_bytes(), depth).unwrap() <= depth, "the lexical guard alone admits this source");
|
||||
let error = rasterize_bounded(source.as_bytes(), 1., 16).unwrap_err();
|
||||
assert!(error.to_string().contains("layers are nested too deeply"), "{error}");
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[test]
|
||||
fn wasm_inline_allowance_bounds_shallow_expanded_references_before_conversion() {
|
||||
let limits = tree_limits(MAX_XML_DEPTH, MAX_RASTER_PIXELS);
|
||||
assert_eq!(limits.tree_depth, INLINE_XML_DEPTH + 1);
|
||||
let shallow = reference_chain_svg(INLINE_XML_DEPTH / 4);
|
||||
assert!(craft_svg::tree::preflight(&shallow, &limits).is_ok());
|
||||
let expanded = reference_chain_svg(INLINE_XML_DEPTH + 2);
|
||||
assert!(nesting_depth(expanded.as_bytes(), INLINE_XML_DEPTH).unwrap() <= INLINE_XML_DEPTH);
|
||||
assert!(matches!(craft_svg::tree::preflight(&expanded, &limits), Err(craft_svg::tree::Error::TooDeep(_))));
|
||||
}
|
||||
|
||||
fn embedded_image(mime: &str, bytes: &[u8]) -> String {
|
||||
let data = bytes.iter().map(|byte| format!("%{byte:02X}")).collect::<String>();
|
||||
format!("<image width=\"1\" height=\"1\" href=\"data:{mime},{data}\"/>")
|
||||
@@ -60,7 +89,7 @@ fn image_svg(images: &str) -> String {
|
||||
format!("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\">{images}</svg>")
|
||||
}
|
||||
|
||||
fn base64(bytes: &[u8]) -> String {
|
||||
pub(super) fn base64(bytes: &[u8]) -> String {
|
||||
const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut out = String::new();
|
||||
for chunk in bytes.chunks(3) {
|
||||
@@ -427,6 +456,46 @@ fn inter_only() -> Arc<usvg::fontdb::Database> {
|
||||
Arc::new(db)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn percent_encoded_svg_child_text_renders_without_outer_text() {
|
||||
let child = br#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40"><text x="5" y="32" font-family="Inter" font-size="30">HI</text></svg>"#;
|
||||
let data = child.iter().map(|byte| format!("%{byte:02X}")).collect::<String>();
|
||||
let outer =
|
||||
format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40"><image width="80" height="40" href="data:image/svg+xml,{data}"/></svg>"#);
|
||||
assert!(!outer.contains("text"), "the encoded child is the only text source");
|
||||
let has_glyph_ink = |pixels: &photocraft_compose::Buffer| pixels.px.iter().any(|pixel| pixel[3] > 0.5);
|
||||
|
||||
let ink = on_svg_stack(|| {
|
||||
let parsed = parse_with(outer.as_bytes(), inter_only)?;
|
||||
let imported = import_parsed("child.svg", parsed, MAX_GROUP_DEPTH, None, false)?;
|
||||
let (parsed, chain) = parse_bounded_inner_with(outer.as_bytes(), MAX_RASTER_PIXELS, inter_only)?;
|
||||
assert_eq!(chain, 1);
|
||||
let pixels = rasterize_tree(&complete_raster_tree(parsed)?, 1.0)?;
|
||||
Ok([has_glyph_ink(&photocraft_compose::flatten(&imported.document)), has_glyph_ink(&pixels)])
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(ink, [true, true], "glyph ink through ordinary import and bounded rendering");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_without_text_does_not_load_fonts() {
|
||||
fn unexpected_fonts() -> Arc<usvg::fontdb::Database> {
|
||||
panic!("a text-free drawing should never load fonts");
|
||||
}
|
||||
let child = image_svg(r#"<rect width="1" height="1" fill="red"/>"#);
|
||||
let outer = image_svg(&embedded_image("image/svg+xml", child.as_bytes()));
|
||||
on_svg_stack(|| {
|
||||
let ordinary = parse_with(outer.as_bytes(), unexpected_fonts)?;
|
||||
let (bounded, _) = parse_bounded_inner_with(outer.as_bytes(), MAX_RASTER_PIXELS, unexpected_fonts)?;
|
||||
for parsed in [ordinary, bounded] {
|
||||
assert!(!parsed.fontless_text);
|
||||
assert_eq!(rasterize_tree(&parsed.tree, 1.0)?.get(0, 0), [1., 0., 0., 1.]);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Text asking for fonts that aren't installed (`Arial, Helvetica, sans-serif` on a Linux
|
||||
/// without them) is set in an installed font, as a browser does, instead of being dropped.
|
||||
#[test]
|
||||
@@ -851,24 +920,29 @@ fn missing_svg_font_uses_an_available_generic_instead_of_dropping_text() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_data_rejects_oversized_and_malformed_embedded_png_and_webp() {
|
||||
let opt = options(Arc::new(usvg::fontdb::Database::new()), MAX_XML_DEPTH);
|
||||
let resolve = &opt.image_href_resolver.resolve_data;
|
||||
fn embedded_png_and_webp_validation_preserves_public_warning_and_strict_contracts() {
|
||||
let img = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, vec![255; 12]).unwrap();
|
||||
let valid_png = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
assert!(resolve("image/png", Arc::new(valid_png.clone()), &opt).is_some());
|
||||
assert!(resolve("text/plain", Arc::new(valid_png.clone()), &opt).is_some());
|
||||
|
||||
let mut huge_png = valid_png;
|
||||
huge_png[16..20].copy_from_slice(&40_000u32.to_be_bytes());
|
||||
huge_png[20..24].copy_from_slice(&40_000u32.to_be_bytes());
|
||||
assert!(resolve("image/png", Arc::new(huge_png.clone()), &opt).is_none(), "oversized PNG rejected before decode");
|
||||
assert!(resolve("text/plain", Arc::new(huge_png), &opt).is_none(), "oversized mime-less PNG rejected before decode");
|
||||
|
||||
// Malformed WebP header under image/webp or text/plain must be rejected at the probe step.
|
||||
let bad_webp = b"RIFF\x10\x00\x00\x00WEBPVP8 ".to_vec();
|
||||
assert!(resolve("image/webp", Arc::new(bad_webp.clone()), &opt).is_none());
|
||||
assert!(resolve("text/plain", Arc::new(bad_webp), &opt).is_none());
|
||||
for mime in ["image/png", "text/plain"] {
|
||||
let svg = image_svg(&embedded_image(mime, &valid_png));
|
||||
let output = rasterize_bounded(svg.as_bytes(), 1.0, 64).unwrap();
|
||||
assert_eq!((output.rect.width(), output.rect.height()), (1, 1));
|
||||
assert_eq!(output.get(0, 0), [1.; 4]);
|
||||
let mut huge = valid_png.clone();
|
||||
huge[16..20].copy_from_slice(&40_000u32.to_be_bytes());
|
||||
huge[20..24].copy_from_slice(&40_000u32.to_be_bytes());
|
||||
let svg = image_svg(&embedded_image(mime, &huge));
|
||||
assert!(parse_bounded(svg.as_bytes(), 64).is_err(), "oversized PNG refused before decode: {mime}");
|
||||
assert!(rasterize_bounded(svg.as_bytes(), 1.0, 64).is_err());
|
||||
}
|
||||
for mime in ["image/webp", "text/plain"] {
|
||||
let svg = image_svg(&embedded_image(mime, b"RIFF\x10\x00\x00\x00WEBPVP8 "));
|
||||
let parsed = parse_bounded(svg.as_bytes(), 64).unwrap();
|
||||
assert_eq!(omitted_image_count(parsed.image_usage), 1);
|
||||
assert!(rasterize_bounded(svg.as_bytes(), 1.0, 64).is_err());
|
||||
let imported = import_svg_bounded("invalid.webp.svg", svg.as_bytes(), 64).unwrap();
|
||||
assert!(imported.warnings.iter().any(|warning| warning.contains("omitted")));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -884,11 +958,12 @@ fn jpeg_allocation_probe_uses_the_frame_header_and_oversized_jpegs_are_refused()
|
||||
assert_eq!(photocraft_codecs::probe_allocation_size(&jpeg(400, 300)).unwrap(), (300, 400));
|
||||
assert!(photocraft_codecs::probe_allocation_size(&[0xFF, 0xD8, 0xFF, 0xDA, 0, 2]).is_err());
|
||||
assert!(photocraft_codecs::probe_allocation_size(&[0xFF, 0xD8]).is_err());
|
||||
let opt = options(Arc::new(usvg::fontdb::Database::new()), MAX_XML_DEPTH);
|
||||
let resolve = &opt.image_href_resolver.resolve_data;
|
||||
// 1.2 billion pixels would be 4.8 GB of RGBA before resvg scaled anything down.
|
||||
assert!(resolve("image/jpeg", Arc::new(jpeg(40_000, 30_000)), &opt).is_none());
|
||||
assert!(resolve("text/plain", Arc::new(jpeg(40_000, 30_000)), &opt).is_none());
|
||||
for mime in ["image/jpeg", "text/plain"] {
|
||||
// 1.2 billion pixels would require 4.8 GB before scaling.
|
||||
let svg = image_svg(&embedded_image(mime, &jpeg(40_000, 30_000)));
|
||||
assert!(parse_bounded(svg.as_bytes(), 64).is_err());
|
||||
assert!(rasterize_bounded(svg.as_bytes(), 1.0, 64).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -899,17 +974,21 @@ fn resolve_data_rejects_nested_gzip_and_handles_text_plain_svgz() {
|
||||
e.write_all(bytes).unwrap();
|
||||
e.finish().unwrap()
|
||||
}
|
||||
let opt = options(Arc::new(usvg::fontdb::Database::new()), MAX_XML_DEPTH);
|
||||
let resolve = &opt.image_href_resolver.resolve_data;
|
||||
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"4\"><rect width=\"4\" height=\"4\"/></svg>";
|
||||
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"4\"><rect width=\"4\" height=\"4\" fill=\"#ff0000\"/></svg>";
|
||||
let once = gz(svg);
|
||||
assert!(resolve("image/svg+xml", Arc::new(once.clone()), &opt).is_some());
|
||||
assert!(resolve("text/plain", Arc::new(once.clone()), &opt).is_some());
|
||||
|
||||
// Doubly gzip-compressed payload must be rejected rather than decompressed a second time by usvg.
|
||||
let twice = gz(&once);
|
||||
assert!(resolve("image/svg+xml", Arc::new(twice.clone()), &opt).is_none());
|
||||
assert!(resolve("text/plain", Arc::new(twice), &opt).is_none());
|
||||
for mime in ["image/svg+xml", "text/plain"] {
|
||||
let single = image_svg(&embedded_image(mime, &once));
|
||||
let output = rasterize_bounded(single.as_bytes(), 1.0, 64).unwrap();
|
||||
assert_eq!((output.rect.width(), output.rect.height()), (1, 1));
|
||||
assert_eq!(output.get(0, 0), [1., 0., 0., 1.]);
|
||||
let nested = image_svg(&embedded_image(mime, &twice));
|
||||
let parsed = parse_bounded(nested.as_bytes(), 64).unwrap();
|
||||
assert_eq!(omitted_image_count(parsed.image_usage), 1);
|
||||
assert!(rasterize_bounded(nested.as_bytes(), 1.0, 64).is_err());
|
||||
let imported = import_svg_bounded("nested.svg", nested.as_bytes(), 64).unwrap();
|
||||
assert!(imported.warnings.iter().any(|warning| warning.contains("omitted")));
|
||||
}
|
||||
}
|
||||
|
||||
/// The outer gzip is size-limited by PhotoCraft. Its result must be XML, never another
|
||||
@@ -1191,10 +1270,24 @@ fn fitting_latin1_retry_keeps_inflation_and_content_budgets_separate() {
|
||||
let decoded = craft_svg::svgz::decode(&latin1).unwrap();
|
||||
assert!(latin1.len() < budget / 2);
|
||||
assert!(decoded.len() > budget / 2 && decoded.len() < budget);
|
||||
let refusals = SvgRefusals { unread_charges: AtomicUsize::new(budget), rechecked: AtomicUsize::new(budget / 2), ..SvgRefusals::default() };
|
||||
note_byte_refusal(&gzip(&latin1), budget, &refusals);
|
||||
assert!(!refusals.over_budget.load(Ordering::Relaxed));
|
||||
assert_eq!(refusals.rechecked.load(Ordering::Relaxed), budget / 2 + latin1.len());
|
||||
let valid = gzip(b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"/>");
|
||||
let damaged = embedded_image("image/svg+xml", &valid[..valid.len() / 2]);
|
||||
let mut primer = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"><!--".to_vec();
|
||||
let close = b"--></svg>";
|
||||
primer.resize(budget / 2 - close.len(), b' ');
|
||||
primer.extend_from_slice(close);
|
||||
assert_eq!(primer.len(), budget / 2);
|
||||
let primer = embedded_image("image/svg+xml", &gzip(&primer));
|
||||
let fitting = embedded_image("image/svg+xml", &gzip(&latin1));
|
||||
let source = image_svg(&format!("{damaged}{primer}{fitting}"));
|
||||
let parsed = parse_bounded(source.as_bytes(), MAX_RASTER_PIXELS).unwrap();
|
||||
assert_eq!(parsed.image_usage.svg_resource_limit_images, 0, "decoded content fits its independent original allowance");
|
||||
assert_eq!(parsed.image_usage.svg_limit_images, 2, "both fitting retries remain omitted");
|
||||
assert_eq!(parsed.image_usage.invalid_svg_images, 1, "the original corrupt source stays unreadable");
|
||||
assert_eq!(parsed.image_usage.embedded_svg_bytes, budget, "retry classification never refunds failed admission work");
|
||||
assert!(rasterize_bounded(source.as_bytes(), 1.0, MAX_RASTER_PIXELS).is_err(), "warningless pixels must remain strict");
|
||||
let imported = import_svg_bounded("legacy-retry.svg", source.as_bytes(), MAX_RASTER_PIXELS).unwrap();
|
||||
assert!(imported.warnings.iter().any(|warning| warning.contains("omitted")));
|
||||
}
|
||||
/// After a damaged image spent the budget, images refused for bytes are unpacked again to tell
|
||||
/// whether they are too large themselves, but at most one budget's worth in all: past that the
|
||||
@@ -1213,8 +1306,9 @@ fn images_after_a_damaged_svgz_are_unpacked_again_within_one_budget() {
|
||||
assert!(error.to_string().contains("byte or nesting budget"), "{error}");
|
||||
}
|
||||
|
||||
/// Each image unpacked again is charged at least what was unpacked, refused ones included, so
|
||||
/// however many follow a damaged image the unpacking adds up to one budget, not one each.
|
||||
/// Classification after damaged data has one separate allowance for the entire drawing.
|
||||
/// Exercise the real wrapped resolver: failed work stays charged and later references cannot
|
||||
/// acquire another classification allowance after the first oversized source exhausts it.
|
||||
#[test]
|
||||
fn unpacking_images_again_adds_up_to_one_budget_however_many_there_are() {
|
||||
let budget = usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap();
|
||||
@@ -1222,12 +1316,23 @@ fn unpacking_images_again_adds_up_to_one_budget_however_many_there_are() {
|
||||
huge.resize(budget * 2, b' ');
|
||||
huge.extend_from_slice(b"</svg>");
|
||||
let huge = gzip(&huge);
|
||||
// A damaged image was charged the whole budget; every later image is refused for size.
|
||||
let refusals = SvgRefusals { unread_charges: AtomicUsize::new(budget), ..SvgRefusals::default() };
|
||||
for _ in 0..64 {
|
||||
note_byte_refusal(&huge, budget, &refusals);
|
||||
assert!(refusals.over_budget.load(Ordering::Relaxed), "too large without the charges");
|
||||
assert_eq!(refusals.rechecked.load(Ordering::Relaxed), budget);
|
||||
let valid = gzip(b"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"/>");
|
||||
let damaged = embedded_image("image/svg+xml", &valid[..valid.len() / 2]);
|
||||
let parsed = parse_bounded(image_svg(&damaged).as_bytes(), MAX_RASTER_PIXELS).unwrap();
|
||||
assert_eq!(parsed.image_usage.invalid_svg_images, 1);
|
||||
assert_eq!(parsed.image_usage.embedded_svg_bytes, budget, "failed work retains its original charge");
|
||||
assert_eq!(parsed.image_usage.svg_resource_limit_images, 0, "corruption alone is not a resource overrun");
|
||||
for count in [1, 64] {
|
||||
let mut children = damaged.clone();
|
||||
for index in 0..count {
|
||||
let mut source = huge.clone();
|
||||
source.push(index);
|
||||
children.push_str(&embedded_image("image/svg+xml", &source));
|
||||
}
|
||||
let source = image_svg(&children);
|
||||
let error = parse_bounded(source.as_bytes(), MAX_RASTER_PIXELS).unwrap_err();
|
||||
assert!(error.to_string().contains("byte or nesting budget"), "{error}");
|
||||
assert!(rasterize_bounded(source.as_bytes(), 1.0, MAX_RASTER_PIXELS).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1609,17 +1714,3 @@ fn every_raster_refusal_reason_is_reported_once() {
|
||||
assert_eq!(dropped("rasterising limit"), 1, "{:?}", result.warnings);
|
||||
assert_eq!(dropped("too much memory"), 1, "{:?}", result.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_legacy_encoding_rechecks_have_a_separate_bounded_work_allowance() {
|
||||
let budget = usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap();
|
||||
let xml = b"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><svg><!--\xe9--></svg>";
|
||||
let refusals = SvgRefusals { unread_charges: AtomicUsize::new(budget), text_rechecked: AtomicUsize::new(budget - xml.len()), ..SvgRefusals::default() };
|
||||
note_byte_refusal(xml, budget, &refusals);
|
||||
assert!(!refusals.over_budget.load(Ordering::Relaxed));
|
||||
assert_eq!(refusals.text_rechecked.load(Ordering::Relaxed), budget);
|
||||
assert_eq!(refusals.rechecked.load(Ordering::Relaxed), 0);
|
||||
note_byte_refusal(xml, budget, &refusals);
|
||||
assert!(refusals.over_budget.load(Ordering::Relaxed));
|
||||
assert_eq!(refusals.text_rechecked.load(Ordering::Relaxed), budget);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
//! App-visible XML validation, rendering and omission contracts. Detailed repair
|
||||
//! source projection and parse-count tests live with the shared SVG implementation.
|
||||
use super as svg;
|
||||
use super::tests::base64;
|
||||
use super::tiny_skia;
|
||||
use crate::IoError;
|
||||
use std::sync::Arc;
|
||||
|
||||
const SVG_NS: &str = "http://www.w3.org/2000/svg";
|
||||
const XLINK_NS: &str = "http://www.w3.org/1999/xlink";
|
||||
|
||||
fn empty_fonts() -> Arc<svg::usvg::fontdb::Database> {
|
||||
Arc::new(svg::usvg::fontdb::Database::new())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_xml_keeps_ordinary_and_bounded_rendering() {
|
||||
let text = "<?xml version='1.0'?><svg xmlns='http://www.w3.org/2000/svg' width='4' height='4'><a href='#shape'/><rect id='shape' width='4' height='4' fill='red'/></svg>";
|
||||
let parsed = svg::parse_with(text.as_bytes(), empty_fonts).unwrap();
|
||||
assert_eq!(svg::omitted_image_count(parsed.image_usage), 0);
|
||||
let pixels = svg::render(&parsed.tree, 1.).unwrap();
|
||||
assert_eq!(pixels.pixel(1, 1).unwrap().red(), 255);
|
||||
svg::parse_bounded_inner_with(text.as_bytes(), 16, empty_fonts).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repaired_xml_keeps_warning_and_strict_contracts() {
|
||||
for text in [
|
||||
"<?xml version='1.0'?><svg xmlns='http://www.w3.org/2000/svg' width='4' height='4'><rect width='4' height='4' fill='red'/><image width='1' height='1' href='data:image/png;base64,A'/></svg>",
|
||||
"<!DOCTYPE svg [<!ENTITY a 'data:image/png;base64,A'>]><svg xmlns='http://www.w3.org/2000/svg' width='4' height='4'><rect width='4' height='4' fill='red'/><image width='1' height='1' href='&a;'/></svg>",
|
||||
] {
|
||||
let parsed = svg::parse_with(text.as_bytes(), empty_fonts).unwrap();
|
||||
assert_eq!(parsed.image_usage.invalid_raster_images, 1);
|
||||
assert_eq!(svg::render(&parsed.tree, 1.).unwrap().pixel(2, 2).unwrap().red(), 255);
|
||||
let (pixels, warnings) = svg::rasterize_bounded_with_warnings(text.as_bytes(), 1., 16).unwrap();
|
||||
assert_eq!(pixels.get(2, 2), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
assert!(svg::rasterize_bounded(text.as_bytes(), 1., 16).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn original_xml_guards_run_before_repair_or_conversion() {
|
||||
let opening = "<svg xmlns='http://www.w3.org/2000/svg' width='4' height='4'>";
|
||||
let image = "<image width='1' height='1' href='data:image/png;base64,A'/>";
|
||||
let mut attributes = String::new();
|
||||
for index in 0..=svg::tree_limits(svg::MAX_XML_DEPTH, 16).attributes {
|
||||
use std::fmt::Write;
|
||||
write!(attributes, " a{index}='x'").unwrap();
|
||||
}
|
||||
let rejected = format!("{opening}<g{attributes}>{image}</g></svg>");
|
||||
let Err(error) = svg::parse_with(rejected.as_bytes(), empty_fonts) else {
|
||||
panic!("original attribute limits must apply before URL repair");
|
||||
};
|
||||
assert!(error.to_string().contains("attribute"), "{error}");
|
||||
|
||||
let accepted = format!(
|
||||
"<!DOCTYPE svg [<!ENTITY a 'data:image/png;base64,A'>]>{opening}<image width='1' height='1' href='&a;'/><rect width='4' height='4' fill='red'/></svg>"
|
||||
);
|
||||
let (pixels, warnings) = svg::rasterize_bounded_with_warnings(accepted.as_bytes(), 1., 16).unwrap();
|
||||
assert_eq!(pixels.get(2, 2), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checked_xml_conversion_panic_is_an_isolation_failure() {
|
||||
let result: Result<(), IoError> = svg::guarded_parse(|| panic!("injected XML conversion panic"));
|
||||
assert!(matches!(result, Err(IoError::Svg(message)) if message.contains("bounded SVG worker failed")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_attribute_spacing_preserves_artwork_and_discloses_only_used_images() {
|
||||
for spacing in [" ".repeat(300), " \t\n\r".repeat(300)] {
|
||||
for attribute in ["href", "x:href"] {
|
||||
let text = format!(
|
||||
"<svg xmlns='{SVG_NS}' xmlns:x='{XLINK_NS}' width='4' height='4'><rect width='4' height='4' fill='red'/><image width='1' height='1' {attribute}{spacing}={spacing}'data:image/png;base64,A'/></svg>"
|
||||
);
|
||||
let (pixels, warnings) = svg::rasterize_bounded_with_warnings(text.as_bytes(), 1., 16).unwrap();
|
||||
assert_eq!(pixels.get(2, 2), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
assert!(svg::rasterize_bounded(text.as_bytes(), 1., 16).is_err());
|
||||
let hidden = text.replace("<image ", "<image display='none' ");
|
||||
let (_, warnings) = svg::rasterize_bounded_with_warnings(hidden.as_bytes(), 1., 16).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_mime_parameters_preserve_valid_pixels_and_invalid_image_omissions() {
|
||||
let mut image = tiny_skia::Pixmap::new(1, 1).unwrap();
|
||||
image.fill(tiny_skia::Color::from_rgba8(0, 255, 0, 255));
|
||||
let body = base64(&image.encode_png().unwrap());
|
||||
let parameters = ";p=x".repeat(100);
|
||||
let drawing = |body: &str| {
|
||||
format!(
|
||||
"<svg xmlns='{SVG_NS}' width='4' height='4'><rect width='4' height='4' fill='red'/><image width='1' height='1' href='data:image/png{parameters};base64,{body}'/></svg>"
|
||||
)
|
||||
};
|
||||
let text = drawing(&body);
|
||||
let (pixels, warnings) = svg::rasterize_bounded_with_warnings(text.as_bytes(), 1., 16).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
assert_eq!(pixels.get(0, 0), [0., 1., 0., 1.]);
|
||||
assert_eq!(pixels.get(2, 2), [1., 0., 0., 1.]);
|
||||
assert!(svg::rasterize_bounded(text.as_bytes(), 1., 16).is_ok());
|
||||
let text = drawing("A");
|
||||
let (pixels, warnings) = svg::rasterize_bounded_with_warnings(text.as_bytes(), 1., 16).unwrap();
|
||||
assert_eq!(pixels.get(2, 2), [1., 0., 0., 1.]);
|
||||
assert_eq!(warnings.len(), 1);
|
||||
assert!(svg::rasterize_bounded(text.as_bytes(), 1., 16).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_use_reports_encoded_byte_exhaustion_at_small_pixel_counts() {
|
||||
let mut png = tiny_skia::Pixmap::new(1, 1).unwrap();
|
||||
png.fill(tiny_skia::Color::from_rgba8(0, 255, 0, 255));
|
||||
let png = png.encode_png().unwrap();
|
||||
let references = "<use href='#i'/>".repeat(64);
|
||||
let body = base64(&png);
|
||||
let source = format!(
|
||||
"<svg xmlns='{SVG_NS}' width='4' height='4'><defs><image id='i' width='1' height='1' href='data:image/png;base64,{body}'/></defs>{references}</svg>"
|
||||
);
|
||||
// Keep the test small while using the real resolver and PhotoCraft error
|
||||
// adapter. The repeated callbacks exhaust encoded bytes before raster pixels.
|
||||
let mut limits = svg::tree_limits(svg::MAX_XML_DEPTH, 1024);
|
||||
limits.bytes = source.len();
|
||||
limits.embedded_bytes = 0;
|
||||
assert!(64 * png.len() > limits.bytes);
|
||||
let resolver = Arc::new(craft_svg::tree::ValidatedImageResolver::new(limits));
|
||||
let budget = Arc::new(std::sync::Mutex::new(svg::ImageBudget {
|
||||
pixels: 1024,
|
||||
xml_bytes: svg::MAX_BOUNDED_SVG_BYTES,
|
||||
svg_images: 0,
|
||||
svg_chain: 0,
|
||||
draw_nodes: 0,
|
||||
repeatable_raster: false,
|
||||
repeatable_svg: false,
|
||||
fontless_text: false,
|
||||
strict: false,
|
||||
error: None,
|
||||
resolver: resolver.clone(),
|
||||
}));
|
||||
let error = svg::parse_image_bounded_with(source.as_bytes(), budget, empty_fonts).unwrap_err();
|
||||
assert!(error.to_string().contains("encoded byte or decoded pixel budget"), "{error}");
|
||||
let usage = resolver.usage();
|
||||
assert!(usage.raster_limit_images > 0);
|
||||
assert!(usage.raster_pixels > 0 && usage.raster_pixels < 64, "{usage:?}");
|
||||
}
|
||||
@@ -403,6 +403,12 @@ fn deeply_nested_embedded_svg_is_dropped_not_a_stack_overflow() {
|
||||
// drawing still renders (its rect is black).
|
||||
let r = open(&embed(&base64_data_url("image/svg+xml", nested(svg::MAX_XML_DEPTH - 2).as_bytes())));
|
||||
assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 4, 4));
|
||||
// One more child level fits in isolation, but exceeds the shared
|
||||
// allowance once the outer drawing's ancestor is included.
|
||||
let beyond = nested(svg::MAX_XML_DEPTH - 1);
|
||||
let r = open(&embed(&base64_data_url("image/svg+xml", beyond.as_bytes())));
|
||||
assert!(close(px(&r.document, 4, 4), [0.0; 4]), "over-limit child must remain transparent: {:?}", px(&r.document, 4, 4));
|
||||
assert!(r.warnings.iter().any(|warning| warning.contains("omitted")), "over-limit child must be disclosed: {:?}", r.warnings);
|
||||
})
|
||||
.unwrap()
|
||||
.join()
|
||||
|
||||
@@ -479,7 +479,7 @@ pub(crate) fn poll(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
Ok(result) => {
|
||||
if let Some(fingerprint) = result.get("fingerprint").filter(|v| !v.is_null()) {
|
||||
edit.target.fingerprint = fingerprint.clone();
|
||||
crate::notices::io_result_warnings(app, &result, "SVG");
|
||||
crate::notices::io_result_warnings(app, &result, if native { "Vector" } else { "SVG" });
|
||||
} else {
|
||||
report(app, "The SVG update did not return its fingerprint; syncing stopped".into());
|
||||
continue;
|
||||
|
||||
@@ -20,13 +20,13 @@ impl ExternalSvgSession for TestSession {
|
||||
}
|
||||
}
|
||||
|
||||
struct NativeTestSession(Saves);
|
||||
struct NativeTestSession(Saves, &'static str);
|
||||
impl ExternalSvgSession for NativeTestSession {
|
||||
fn poll(&mut self) -> Option<Result<Vec<u8>, String>> {
|
||||
self.0.borrow_mut().pop_front()
|
||||
}
|
||||
fn poll_vector(&mut self) -> Option<Result<crate::ExternalVectorSave, String>> {
|
||||
self.poll().map(|r| r.map(|bytes| crate::ExternalVectorSave { bytes, preview_svg: Some(SVG.as_bytes().to_vec()) }))
|
||||
self.poll().map(|r| r.map(|bytes| crate::ExternalVectorSave { bytes, preview_svg: Some(self.1.as_bytes().to_vec()) }))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -571,14 +571,23 @@ fn embedded_ai_link_table_handoff_preserves_source_until_saved_and_undo_restores
|
||||
assert_eq!(editor, ExternalSvgEditor::VectorCraft);
|
||||
assert_eq!(*source.bytes, original);
|
||||
assert_eq!(source.extension, "ai");
|
||||
Ok(Box::new(NativeTestSession(queue.clone())))
|
||||
// An empty, same-size preview is admissible and yields a real
|
||||
// "nothing to draw" warning from the native update command.
|
||||
let empty_preview = r#"<svg xmlns="http://www.w3.org/2000/svg" width="12" height="8"/>"#;
|
||||
Ok(Box::new(NativeTestSession(queue.clone(), empty_preview)))
|
||||
}));
|
||||
open(&mut app);
|
||||
assert!(calls.borrow().is_empty());
|
||||
confirm(&mut app).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().doc, before);
|
||||
app.ui.notices.clear();
|
||||
saves.borrow_mut().push_back(Ok(b"%PDF-1.6\nedited native AI".to_vec()));
|
||||
poll(&mut app, &egui::Context::default());
|
||||
assert_eq!(app.ui.notices.len(), 1);
|
||||
let notice = &app.ui.notices[0];
|
||||
assert!(notice.title.contains("Vector"), "{notice:?}");
|
||||
assert_eq!(notice.tone, craft::theme::variant::Tone::Neutral);
|
||||
assert!(notice.lines.iter().any(|line| line.contains("nothing to draw")));
|
||||
let layer = app.session.active().unwrap().doc.layer(id).unwrap();
|
||||
assert!(
|
||||
matches!(&layer.content,LayerContent::Smart(sm) if matches!(&sm.source,SmartSource::Embedded {file_name,bytes} if file_name=="Logo.ai" && bytes.as_slice()==b"%PDF-1.6\nedited native AI"))
|
||||
@@ -653,6 +662,38 @@ fn finished_session_retains_final_save_until_target_is_safe() {
|
||||
assert!(app.smart_svg.editing.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_svg_save_back_reports_omissions_once_and_keeps_silent_updates_quiet() {
|
||||
for omitted in [false, true] {
|
||||
let (mut app, saves, _) = fixture();
|
||||
open(&mut app);
|
||||
app.smart_svg.pending.as_mut().unwrap().editor = Editor::Illustrator;
|
||||
confirm(&mut app).unwrap();
|
||||
app.ui.notices.clear();
|
||||
let before = app.session.active().unwrap().doc.clone();
|
||||
let child = if omitted { "<image width='2' height='2' href='data:image/png,%00'/>" } else { "" };
|
||||
let svg = SVG.replace("red", "blue").replace("</svg>", &format!("{child}</svg>"));
|
||||
saves.borrow_mut().push_back(Ok(svg.into_bytes()));
|
||||
let ctx = egui::Context::default();
|
||||
poll(&mut app, &ctx);
|
||||
assert_ne!(app.session.active().unwrap().doc, before);
|
||||
assert_eq!(app.smart_svg.editing.len(), 1, "the session keeps the successful new fingerprint");
|
||||
if omitted {
|
||||
assert_eq!(app.ui.notices.len(), 1);
|
||||
assert!(app.ui.notices[0].title.contains("SVG"));
|
||||
assert!(app.ui.notices[0].lines.iter().any(|line| line.contains("embedded") && line.contains("omitted")));
|
||||
assert_eq!(app.ui.notices[0].tone, craft::theme::variant::Tone::Neutral);
|
||||
} else {
|
||||
assert!(app.ui.notices.is_empty(), "{:?}", app.ui.notices);
|
||||
}
|
||||
let notices = app.ui.notices.clone();
|
||||
poll(&mut app, &ctx);
|
||||
assert_eq!(app.ui.notices, notices, "polling the same completed save cannot duplicate a warning");
|
||||
app.session.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().doc, before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_save_back_displays_omitted_image_warning() {
|
||||
let (mut app, saves, _) = fixture();
|
||||
|
||||
Reference in new issue
Block a user