diff --git a/crates/pdf/src/import/device.rs b/crates/pdf/src/import/device.rs index 2a2df28e37..45ca7f7081 100644 --- a/crates/pdf/src/import/device.rs +++ b/crates/pdf/src/import/device.rs @@ -312,6 +312,8 @@ impl<'a> Device<'a> for Builder<'_> { let px = match (img, alpha) { // An opaque image's RGB samples are encoded as they are, with no copy. (ImageData::Rgb(d), None) if !white => (d.data, false), + // Gray samples are stored as RGB: as a gray PNG, the image would become a + // grayscale image that a fill color colorizes. (ImageData::Luma(d), None) if !white => (d.data.iter().flat_map(|g| [*g; 3]).collect(), false), (img, alpha) => { let a = alpha.map(|a| resize_alpha(&a, w, h)).unwrap_or_else(|| vec![255; (w * h) as usize]); diff --git a/crates/pdf/src/pages.rs b/crates/pdf/src/pages.rs index 12a7405d6f..470432aa8f 100644 --- a/crates/pdf/src/pages.rs +++ b/crates/pdf/src/pages.rs @@ -323,9 +323,9 @@ pub struct TrimPages { pub bleed: [f64; 4], } -/// Preserve the application's existing accepted file sizes. Ordinary stream decoding still -/// uses the shared reader's immutable per-document limits, and its input copy is fallible. One -/// image may decode to the bytes and pixels of [`crate::streams::image_limits`] for this build. +/// The shared reader's options: files of any size are accepted. Ordinary stream decoding uses the shared reader's immutable per-document limits, and its input +/// copy is fallible. One image may decode to the bytes and pixels of +/// [`crate::streams::image_limits`] for this build, past the shared reader's defaults. pub(crate) fn open_options() -> craft_pdf::OpenOptions { let (bytes, pixels) = crate::streams::image_limits(crate::streams::LARGE_IMAGES); let decode_limits = craft_pdf::syntax::DecodeLimits::default().with_max_decoded_image(bytes).with_max_image_pixels(pixels); diff --git a/crates/pdf/src/streams.rs b/crates/pdf/src/streams.rs index 0b20eb7866..1cb23b2768 100644 --- a/crates/pdf/src/streams.rs +++ b/crates/pdf/src/streams.rs @@ -20,9 +20,9 @@ pub(crate) const MAX_DECODED: usize = 256 << 20; pub(crate) const LARGE_IMAGES: bool = cfg!(all(target_pointer_width = "64", not(target_family = "wasm"))); /// The most bytes one image XObject may decode to and the most pixels it may have, for a build -/// that reads `large` images: 4 GB and 2^30 pixels (31404 × 31403 pixels are 2.96 GB in RGB and -/// 3.94 GB in CMYK), otherwise 1 GB and 2^28 pixels (an A0 sheet scanned at 300 ppi is 418 MB in -/// RGB and 557 MB in CMYK). The shared reader takes the same two figures ([`crate::pages::open_options`]). +/// that reads `large` images: 4 GiB and 2^30 pixels (31404 × 31403 pixels are 2.76 GiB in RGB and +/// 3.67 GiB in CMYK), otherwise 1 GiB and 2^28 pixels (an A0 sheet scanned at 300 ppi is 399 MiB +/// in RGB and 532 MiB in CMYK). The shared reader takes the same two figures ([`crate::pages::open_options`]). pub(crate) const fn image_limits(large: bool) -> (u64, u64) { if large { (1 << 32, 1 << 30) } else { (1 << 30, 1 << 28) } } @@ -791,31 +791,42 @@ mod tests { /// A one-page PDF drawing one flat 8-bit DeviceRGB image of `w` × `h` pixels, Flate-compressed. fn one_image_pdf(w: usize, h: usize) -> Vec { + image_pdf(&format!("/Width {w} /Height {h} /ColorSpace /DeviceRGB"), &flat(&[200, 180, 150].repeat(w), h), None) + } + + /// `row` repeated `h` times, Flate-compressed as it is written. + fn flat(row: &[u8], h: usize) -> Vec { let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); - let row: Vec = [200, 180, 150].repeat(w); for _ in 0..h { - std::io::Write::write_all(&mut e, &row).unwrap(); + std::io::Write::write_all(&mut e, row).unwrap(); } - let image = e.finish().unwrap(); + e.finish().unwrap() + } + + /// A one-page PDF drawing one 8-bit image with the size and color space of `keys` and the + /// Flate-compressed samples `data`; with `smask`, a soft mask with those keys and data. + fn image_pdf(keys: &str, data: &[u8], smask: Option<(&str, &[u8])>) -> Vec { + let stream = |keys: &str, data: &[u8]| { + let dict = + format!("<< /Type /XObject /Subtype /Image {keys} /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", data.len()); + [dict.as_bytes(), data, b"\nendstream"].concat() + }; let content = b"q 595 0 0 842 0 0 cm /Im0 Do Q"; let mut out = b"%PDF-1.6\n".to_vec(); let mut offsets = vec![]; - let objects: Vec> = vec![ + let mut objects: Vec> = vec![ b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R /Resources << /XObject << /Im0 5 0 R >> >> >>".to_vec(), [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), - [ - format!( - "<< /Type /XObject /Subtype /Image /Width {w} /Height {h} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", - image.len() - ) - .as_bytes(), - &image, - b"\nendstream", - ] - .concat(), ]; + match smask { + Some((mask_keys, mask_data)) => { + objects.push(stream(&format!("{keys} /SMask 6 0 R"), data)); + objects.push(stream(mask_keys, mask_data)); + } + None => objects.push(stream(keys, data)), + } for (i, o) in objects.iter().enumerate() { offsets.push(out.len()); out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes()); @@ -844,18 +855,27 @@ mod tests { fn a_64_bit_desktop_build_reads_larger_images_and_others_keep_their_limits() { assert_eq!(image_limits(false), (1 << 30, 1 << 28)); assert_eq!(image_limits(true), (1 << 32, 1 << 30)); - assert_eq!(LARGE_IMAGES, cfg!(all(target_pointer_width = "64", not(target_family = "wasm")))); let (bytes, pixels) = image_limits(LARGE_IMAGES); assert_eq!(MAX_IMAGE_DECODED as u64, bytes); let limits = crate::pages::open_options().decode_limits; assert_eq!((limits.max_decoded_image(), limits.max_image_pixels()), (bytes, pixels)); - // An image past the limit is refused with the same message, naming this build's limit. + // An image declaring more is held to this build's limit, which a refusal of it names. let d = dict("<< /Subtype /Image /Width 70000 /Height 70000 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode >>"); assert_eq!(budget(&d), MAX_IMAGE_DECODED); let most = if LARGE_IMAGES { "4 GB" } else { "1 GB" }; assert_eq!(crate::pages::decoded_size(MAX_IMAGE_DECODED), most); } + /// The only image of `doc`, as stored: its width, height and channel layout. + fn stored_image(doc: &vectorcraft_doc::Document) -> (u32, u32, image::ExtendedColorType) { + use image::ImageDecoder as _; + let blobs: Vec<_> = doc.images.values().collect(); + let [blob] = blobs[..] else { panic!("one image expected, found {}", blobs.len()) }; + let decoder = image::ImageReader::new(std::io::Cursor::new(blob.bytes.as_slice())).with_guessed_format().unwrap().into_decoder().unwrap(); + let (w, h) = decoder.dimensions(); + (w, h, decoder.original_color_type()) + } + #[test] fn an_image_past_1_gb_and_2_to_the_28_pixels_opens_on_a_64_bit_desktop_build() { if !LARGE_IMAGES { @@ -864,17 +884,30 @@ mod tests { // 18919 × 18919 RGB pixels: 358 million pixels, 1.07 GB decoded (#4366). let side = 18919; let doc = crate::import(&one_image_pdf(side, side)).unwrap(); - let blobs: Vec<_> = doc.images.values().collect(); - let [blob] = blobs[..] else { panic!("one image expected, found {}", blobs.len()) }; // Opaque: stored as RGB, without an alpha channel. - let (w, h, layout) = { - use image::ImageDecoder as _; - let decoder = image::ImageReader::new(std::io::Cursor::new(blob.bytes.as_slice())).with_guessed_format().unwrap().into_decoder().unwrap(); - let (w, h) = decoder.dimensions(); - (w, h, decoder.original_color_type()) - }; - assert_eq!((w, h), (side as u32, side as u32)); - assert_eq!(layout, image::ExtendedColorType::Rgb8); + assert_eq!(stored_image(&doc), (side as u32, side as u32, image::ExtendedColorType::Rgb8)); + } + + #[test] + fn opaque_images_are_stored_without_an_alpha_channel_and_masked_ones_with_one() { + let rgb = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceRGB", &flat(&[200, 180, 150].repeat(4), 2), None); + assert_eq!(stored_image(&crate::import(&rgb).unwrap()), (4, 2, image::ExtendedColorType::Rgb8)); + let gray = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceGray", &flat(&[120; 4], 2), None); + assert_eq!(stored_image(&crate::import(&gray).unwrap()), (4, 2, image::ExtendedColorType::Rgb8)); + let mask = ("/Width 4 /Height 2 /ColorSpace /DeviceGray", &flat(&[0, 85, 170, 255], 2)[..]); + let masked = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceRGB", &flat(&[200, 180, 150].repeat(4), 2), Some(mask)); + assert_eq!(stored_image(&crate::import(&masked).unwrap()), (4, 2, image::ExtendedColorType::Rgba8)); + } + + #[test] + fn an_image_of_more_pixels_than_the_limit_is_left_out_with_a_warning() { + let (_, pixels) = image_limits(LARGE_IMAGES); + // One more row than a square of the limit's pixels; its data isn't looked at. + let side = 1u64 << (pixels.ilog2() / 2); + let pdf = image_pdf(&format!("/Width {side} /Height {} /ColorSpace /DeviceGray", side + 1), &flat(&[0; 16], 1), None); + let report = crate::import_with_report(&pdf, &crate::ImportOptions::default()).unwrap(); + assert!(report.document.images.is_empty()); + assert!(report.warnings.iter().any(|w| w.contains("an image could not be decoded and was skipped")), "{:?}", report.warnings); } #[test] diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e683c240f4..72abae6f61 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1962,7 +1962,6 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur When the editing data of a `.ai` or EPS is for a page of another size than the file's, the warning that its layers weren't read gives both sizes in points, and the scale of the editing data's page to the file's page (1:10 for a tenth of its size) when their sides are in proportion (#2034; `crates/eps/src/import/tests_native.rs`). A `.ai` or EPS opened from its editing data keeps its layers when it embeds an image of more than 2^25 pixels (part of #1479). The image comes in at full size up to a target size (2^26 to 2^28 pixels on the desktop, by installed memory; 2^27 on the web), a 1-bit gray image as a 1-bit PNG. A larger image is averaged down by the smallest whole step that fits, a 1-bit one into shades of gray, with a note giving both sizes. An image of more than 2^31 pixels or 512 MiB of samples is an object the reader can't read, so its box comes in as the page's art; without page art, it is left out with a note (`crates/eps/src/import/tests_native.rs`). A `.ai`, or a PDF saved with Illustrator's editing data, whose editing data can't be decompressed opens from its PDF part with a warning that its layers weren't read and why, and an export or Save As to its own path asks first (`acknowledgeLoss`), as for a file whose layers the reader can't read (`crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). - A PDF, or a `.ai` read through its PDF part, opens an image that decodes to up to 4 GB with up to 2^30 pixels in a 64-bit desktop build (31404 × 31403 RGB pixels are 2.96 GB), and up to 1 GB with 2^28 pixels in 32-bit and web builds. A file with an image that would decode to more is refused with the size it would decode past, and an image with more pixels is not drawn (#4366). An opaque image is kept as RGB, without an alpha channel. Measured on Linux x86_64: a synthetic 31404 × 31403 RGB image opened in 15 s, peaking at 5.9 GiB (`crates/pdf/src/streams.rs`). A `.ai` or EPS opened from its editing data with one object on a shown layer that the reader can't read (an unknown operator inside a group, a pattern fill whose pattern it doesn't have, a symbol instance, a placed file without a usable preview) keeps its layers and groups: in that object's place, its layer gets a group of the page's objects inside the object's box, named after it, with a warning that lists what came in as the page's art and an export or Save As to its own path asking first (`acknowledgeLoss`). The file still opens from its page or PDF part when no box can be found, when live art or another unread object shares the page art inside it, when the object spans two artboards or lies on an artboard that has no page, when no page art lies inside its box, when that page art is inside a page group with a clip, mask, transparency or appearance, when the object holds a frame of threaded type, when the page art uses a pattern, image or swatch that the page lacks or that differs from the editing data's, or when the page art doesn't carry the object's transparency (#4806; `crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). A `.ai` or EPS opened from its editing data with one object on a shown layer that the reader can't read (an unknown operator inside a group, a pattern fill whose pattern it doesn't have, a symbol instance, a placed file without a usable preview) keeps its layers and groups: in that object's place, its layer gets a group of the page's objects inside the object's box, named after it, with a warning that lists what came in as the page's art and an export or Save As to its own path asking first (`acknowledgeLoss`). The file still opens from its page or PDF part when no box can be found, when live art or another unread object shares the page art inside it, when the object spans two artboards, or when the page art doesn't carry the object's transparency (#4806; `crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). In a `.ai` opened from its editing data, an envelope made with a warp, a mesh or a top object opens as a live envelope of its contents, with its warp style, bend and distortions, its mesh points and handles or its top object's outline, its Envelope Options, its Fidelity and whether its contents are being edited. When Distort Appearance is on and the size the file saves for the contents matches their bounds with strokes, the contents map from that stroked box, until an edit in Edit Contents changes how far the strokes reach; VectorCraft's new envelopes map from the geometric box, as do contents with no width or no height, such as a single straight line. An envelope that can't be read, or that VectorCraft draws differently from the file's drawn art, keeps that drawn art, with a note. An envelope of type opens live, its type laid out from the text document as its contents, where VectorCraft's own envelope of that type draws as the drawn art the file saves (compared as for the other envelopes, seen on white and on black, so white type counts, and only where the layers with it are compared with the page and draw as it, the envelope weighed as the art it draws; where they don't, even with the drawn art of every live envelope put back, and are used all the same, trusted, with the objects that differ coming in as the page's art, or opened alone, it stays live where, in the pixels it and its drawn art ink (drawn together without the rest of the layers), the layers with it differ from the page in no more than its own check allows and the envelopes that stay differ there in no more than half of what the page check allows, and nothing above it clips, masks or moves it); elsewhere, and where the type's font isn't available, a text or visibility variable binds the type, or the envelope is in a symbol's or a pattern's art, it keeps its drawn art, with its type hidden and a note that says the type isn't editable (#3179; `crates/eps/src/import/ai/tests.rs`, `crates/eps/src/import/tests_native.rs`). A warp of type whose own warp, VectorCraft's, doesn't draw as its drawn art is tried again as a mesh envelope on the grid the file saves for it: its type stays editable and keeps the file's shape, but the warp's settings (style, bend) are lost, as the document has no place for them on a mesh. Type with strokes in an envelope with Distort Appearance on is also tried mapped from its glyphs grown by as far as its strokes paint along a side (half the weight of a centred stroke), without what miter joins add at corners, where the size the file saves for it doesn't already give that box; after those tries (and before a grid moved onto its drawn art), it is also tried mapped from the box of what it paints, where that is another box, as users' files map some such type: its glyph outlines with their fills and strokes baked as the envelope bakes them, so geometry effects on them count (a stroke a Transform effect moves paints where it is moved to), each measured by its visual bounds (exact for miter joins, a tip counting where the miter limit keeps it, and for round joins; an upper bound for bevel joins, miters the limit cuts, width profiles and square-capped dashes), and a contour of zero length, which paints nothing, left out; effects on the type object itself are left out, as VectorCraft applies them after the envelope bends the type. The drawn-art check decides which one opens. An envelope's grid reads its way round from all its rows and columns, so a mesh bent far round reads the way it runs. While an envelope's contents are edited, its mesh is kept in its dictionary, and in some files that mesh lies away from the drawn art by a move, the contents with the drawn art (as if the mesh hadn't followed the envelope when it moved): a mesh or top object saved in Edit Contents whose mesh draws the contents as large as the drawn art but elsewhere is tried again, after every other way, with its mesh moved onto that art, and opens there where it then draws as it. The drawn art of these files strokes type in an envelope along its bent outlines at the stroke's own weight, Distort Appearance on or off, and VectorCraft draws it so (#6644): an envelope of stroked type whose grid squeezes or stretches it opens live where it draws as that art. @@ -1991,6 +1990,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur A `.ai` saved without PDF compatibility (its PDF part only a placeholder page) is placed and pasted from its editing data, as it opens: File › Place places the art of the layers that show on the first artboard (`file.place`'s `page` places another one), and Paste of it as PDF data (`clipboard.importPdf`) takes every object. `document.open` with `pages` naming every page of such a file opens it the same way (`crates/engine/src/cmd/fileio/tests_aiimport.rs`). Explicitly requested editing data keeps the full single artboard when its PDF page is capped at 14,400 pt (#6865; `crates/eps/src/import/tests_native.rs`). Scores and estimates unchanged. A `.ai`, or a PDF saved with Illustrator's editing data, whose PDF part can't be read (a stream the import refuses) opens from its editing data, with the PDF part's error as its first warning; File › Place places its first artboard the same way, and Paste of it as PDF data with `editingData: true` takes every object (part of #4366; `crates/engine/src/cmd/fileio/tests_aiimport.rs`). + A PDF, or a `.ai` read through its PDF part, opens an image that decodes to up to 4 GiB with up to 2^30 pixels in a 64-bit desktop build (31404 × 31403 RGB pixels are 2.76 GiB), and up to 1 GiB with 2^28 pixels in 32-bit and web builds. A file with an image that would decode to more is refused with the size it would decode past, and an image with more pixels is left out with a warning (#4366). An opaque image is kept as RGB, without an alpha channel (`crates/pdf/src/streams.rs`). - EPS and DXF in and out, EMF/WMF in and out; PNG (with PNG-8), JPEG, WebP, GIF, TIFF, BMP, Targa and layered PSD export; grayscale TIFFs with an alpha channel or other extra channels place, relink and open, the alpha as the image's transparency (#1466 in part; CCITT Group 3 and planar files with extra channels are not read); PSD and PSB files open and place, linked or embedded, as their merged image in every colour mode and depth (#918). EPS import runs a PostScript Level 3 interpreter written from the PostScript Language Reference: resource categories and `resourceforall`, local and global VM (`currentglobal`/`setglobal`/`gcheck`), `clipsave`/`cliprestore`, subarrays and substrings that share storage, the standard dictionaries in `systemdict`, `dictstack`, `internaldict`, executable filters and strings, `load` by any key, `//name` immediate names, `token`, `execform`, user paths, `cshow`, `rootfont`, `cachestatus`, colour rendering and halftone queries, file `status` and data read with `flushfile`, and `pdfmark` marks dropped (Discord feedback, #466, #469, #474). Masked images (`ImageType 3` in its three interleavings) and reusable streams are read, and an image's several data sources are read in turn, as the reference defines (procedures that read one file get their own rows: such images came in banded, #498). `restore` puts dictionaries back as they were at the `save`, so programs that count their saves in a dictionary read to their end (part of #505). Tiling patterns come in as pattern swatches, uncoloured ones too; Type 3 fonts' glyphs as their outlines, spaced by `ashow`/`widthshow`/`xshow`/`kshow`; mesh and function-based shadings (types 1, 4–7) as gradient meshes; sampled functions (type 0) and Flate/LZW predictors are read. It is tested with programs written from the specification in the ways common generators use these features; Inkscape/cairo EPS files open as vectors. When a file's PostScript still can't be read, it opens as its TIFF preview, its Windows metafile preview (as vectors) or its EPSI preview, and the warning (in the status bar, `document.open`/`app.open`/MCP `open_file` replies and `vectorcraft-cli info`) names the error, the operator that raised it and the procedures it ran in. Legacy PostScript `.ai` files (Illustrator 3–8, written with the prolog that defines their operators) keep their groups and nested groups (`u` … `U`), between and inside clipping groups; EPS and EMF/WMF import nest clips (and groups) at most 128 deep, so a hostile file can't exhaust the stack. DXF TEXT justified Fit or Aligned spans its two points (stretched, or scaled as a whole). PDF export writes type as real, searchable text in embedded subset fonts by default, as Illustrator's PDF presets do (Convert Text to Outlines is an option; printer's marks stay shapes), so text-heavy PDFs stay small (#733). DXF export writes each fill as its outlines followed by its hatch, so laser and cutter software, which skips hatches, gets filled art and type too (#546). - JPEG XL (`.jxl`) images open and place, linked or embedded, as their first frame, decoded with jxl-oxide and stored as PNG: 16-bit and floating-point samples become 8 bits, alpha is kept, and an image past the pixel allowance is refused from its header (part of #2306; `crates/engine/src/cmd/fileio/tests_jxlread.rs`). - The Open and Place dialogs list PostScript `.ps` files and `.epsf` files. A `.ps` file opens and places through the EPS reader (its first page), also when its header is only `%!`, as the `ps` format, which exports don't write (Print to PostScript does); an `.epsf` file opens and places as EPS. Copied in a file manager, these files paste as EPS, and `clipboard.importEps` also reads PostScript whose header is only `%!` (`crates/engine/src/cmd/fileio/tests_epsimport.rs`).