From dc82ff860e67a835912e9b09ceebbc1e1c708589 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 20:09:57 -0800 Subject: [PATCH] Metafile import: warn about images, clips and raster operations the reader leaves out craft-metafile now reports what it does not read or play as the file asks. The importer uses it: - A file, or a metafile image inside it, cut short by any of the reader's limits (items, bytes kept, saved states, clip paths) warns that what came after the limit was not read. The warning no longer depends on the count of objects, so a file with exactly 200,000 objects opens without it. - Images that are not read, clips that are not applied and raster operations other than a plain copy each add a warning with a count. --- crates/metafile/src/import/picture.rs | 60 ++++++++++++++++------ crates/metafile/src/tests/import.rs | 72 ++++++++++++++++++++++++++- docs/file-format-parity.md | 2 +- docs/target-app-parity.md | 1 + 4 files changed, 117 insertions(+), 18 deletions(-) diff --git a/crates/metafile/src/import/picture.rs b/crates/metafile/src/import/picture.rs index e114151f9d..0e07f644a8 100644 --- a/crates/metafile/src/import/picture.rs +++ b/crates/metafile/src/import/picture.rs @@ -46,12 +46,7 @@ use crate::{Imported, Kind}; const PATTERNS: &str = "hatched and pattern brushes are filled with their average color"; const ENDS_EARLY: &str = "the file ends early: what it held was imported"; const TOO_MANY: &str = "objects past the most one file may hold were left out"; -const AT_LIMIT: &str = "the file reached the most objects one file may hold: any after them were not read"; - -/// The most items `craft_metafile` keeps from one file (its `MAX_ITEMS`). It stops reading at the -/// limit and does not say whether more items followed, so a picture holding this many may have -/// been cut short. -const READER_ITEMS: usize = 200_000; +const AT_LIMIT: &str = "the file or a picture inside it reached a limit on what one file may hold: what came after the limit was not read"; /// Points a unit of a WMF without a placeable header (1440 units an inch). const WMF_UNIT: f64 = 72.0 / 1440.0; @@ -696,14 +691,33 @@ impl Mapper { } /// The document: what was drawn on one layer, clipped art in clipping groups. - fn finish(mut self, kind: Kind, skipped: u32) -> Imported { + fn finish(mut self, kind: Kind, lost: Lost) -> Imported { self.flush(); - if skipped > 0 { - let n = skipped; + let s = |n: u32| if n == 1 { "" } else { "s" }; + let was = |n: u32| if n == 1 { "was" } else { "were" }; + let n = lost.skipped; + if n > 0 { + self.warnings.push(format!("{n} record{} of kinds VectorCraft doesn't read {} skipped", s(n), was(n))); + } + let n = lost.images; + if n > 0 { + self.warnings.push(format!("{n} image{} that couldn't be read {} left out", s(n), was(n))); + } + let n = lost.clips; + if n > 0 { self.warnings.push(format!( - "{n} record{} of kinds VectorCraft doesn't read {} skipped", - if n == 1 { "" } else { "s" }, - if n == 1 { "was" } else { "were" } + "{n} clip{} couldn't be applied as the file sets {}: what is drawn after {} may show more or less than the file does", + s(n), + if n == 1 { "it" } else { "them" }, + if n == 1 { "it" } else { "them" } + )); + } + let n = lost.rops; + if n > 0 { + self.warnings.push(format!( + "{n} raster operation{} other than a plain copy (masks, inversions) {} drawn as a plain copy or left out", + s(n), + was(n) )); } let out = std::mem::take(&mut self.out); @@ -952,16 +966,30 @@ pub(crate) fn import(bytes: &[u8]) -> Result { craft_metafile::Error::BadHeader => format!("the {} header is damaged", kind.id().to_uppercase()), craft_metafile::Error::NoRecords => format!("the {} file holds no records", kind.id().to_uppercase()), })?; + let craft_metafile::Picture { width, height, items, skipped_records, truncated, dropped_images, ignored_clips, unplayed_raster_ops } = pic; let k = unit(bytes, kind); - let mut m = Mapper::new(f64::from(pic.width), f64::from(pic.height), k); + let mut m = Mapper::new(f64::from(width), f64::from(height), k); if ends_early(bytes, kind) { m.warn(ENDS_EARLY); } - if pic.items.len() >= READER_ITEMS { + if truncated { m.warn(AT_LIMIT); } - for item in pic.items { + for item in items { m.item(item); } - Ok(m.finish(kind, pic.skipped_records)) + let lost = Lost { skipped: skipped_records, images: dropped_images, clips: ignored_clips, rops: unplayed_raster_ops }; + Ok(m.finish(kind, lost)) +} + +/// What `craft_metafile` counted as not read or not played as the file asks. +struct Lost { + /// Records of kinds it doesn't read. + skipped: u32, + /// Images it couldn't read. + images: u32, + /// Clip changes it didn't apply as the file sets them. + clips: u32, + /// Raster operations drawn as a plain copy or left out. + rops: u32, } diff --git a/crates/metafile/src/tests/import.rs b/crates/metafile/src/tests/import.rs index 5f8ca735a0..146149ea39 100644 --- a/crates/metafile/src/tests/import.rs +++ b/crates/metafile/src/tests/import.rs @@ -527,11 +527,81 @@ fn a_file_with_more_objects_than_are_read_says_so() { let many = hand_emf(5000, 3000, &vec![rect.clone(); 200_010]); let back = import(&many).unwrap(); assert_eq!(paths(&back.document).len(), 200_000); - assert!(back.warnings.iter().any(|w| w.contains("reached the most objects")), "{:?}", back.warnings); + assert!(back.warnings.iter().any(|w| w.contains("reached a limit")), "{:?}", back.warnings); + // Exactly as many as the reader keeps: nothing was left unread. + let full = import(&hand_emf(5000, 3000, &vec![rect.clone(); 200_000])).unwrap(); + assert_eq!(paths(&full.document).len(), 200_000); + assert!(full.warnings.is_empty(), "{:?}", full.warnings); let few = import(&hand_emf(5000, 3000, &vec![rect; 3])).unwrap(); assert!(few.warnings.is_empty(), "{:?}", few.warnings); } +#[test] +fn a_file_cut_short_by_another_limit_says_so() { + // SAVEDC past the 1,024 levels the reader keeps stops the parse: the rectangle after them is + // not read. + let mut records = vec![(43, le(&[0, 0, 100, 100]))]; + records.extend(vec![(33, vec![]); 1100]); + records.push((43, le(&[200, 200, 300, 300]))); + let back = import(&hand_emf(5000, 3000, &records)).unwrap(); + assert_eq!(paths(&back.document).len(), 1); + assert_eq!( + back.warnings, + vec!["the file or a picture inside it reached a limit on what one file may hold: what came after the limit was not read".to_string()] + ); +} + +#[test] +fn images_the_reader_does_not_read_are_counted() { + let records = vec![(81, png_stretch_dibits(&png_claiming(1 << 13, 1 << 13))); 2]; + let back = import(&hand_emf(5000, 3000, &records)).unwrap(); + assert_eq!(back.warnings, vec!["2 images that couldn't be read were left out".to_string()]); + let back = import(&hand_emf(5000, 3000, &records[..1])).unwrap(); + assert_eq!(back.warnings, vec!["1 image that couldn't be read was left out".to_string()]); +} + +#[test] +fn clips_that_are_not_applied_are_counted() { + // BEGINPATH, a rectangle, ENDPATH, SELECTCLIPPATH with RGN_XOR (3), then a rectangle drawn after it. + let records = vec![(59, vec![]), (43, le(&[100, 100, 2000, 2000])), (60, vec![]), (67, le(&[3])), (43, le(&[500, 500, 600, 600]))]; + let back = import(&hand_emf(5000, 3000, &records)).unwrap(); + assert_eq!(paths(&back.document).len(), 1); + assert_eq!( + back.warnings, + vec!["1 clip couldn't be applied as the file sets it: what is drawn after it may show more or less than the file does".to_string()] + ); + // RGN_AND (1) is applied. + let mut records = records; + records[3] = (67, le(&[1])); + let back = import(&hand_emf(5000, 3000, &records)).unwrap(); + assert!(back.warnings.is_empty(), "{:?}", back.warnings); +} + +/// An EMR_BITBLT of the 100 × 100 box at the origin with `rop` and no source bitmap. +fn bitblt(rop: u32) -> Vec { + // Bounds; destination; the operation; the source origin, transform (identity) and background + // color; usage, then no BITMAPINFO and no bits. + let mut body = vec![0; 16]; + body.extend(le(&[0, 0, 100, 100, rop as i32, 0, 0])); + body.extend(le_f32(&[1.0, 0.0, 0.0, 1.0, 0.0, 0.0])); + body.extend(le(&[0, 0, 0, 0, 0, 0])); + body +} + +#[test] +fn raster_operations_that_are_not_played_are_counted() { + // DSTINVERT and PATINVERT take no source bitmap and are left out. + let records = vec![(76, bitblt(0x0055_0009)), (76, bitblt(0x005A_0049))]; + let back = import(&hand_emf(5000, 3000, &records)).unwrap(); + assert_eq!( + back.warnings, + vec!["2 raster operations other than a plain copy (masks, inversions) were drawn as a plain copy or left out".to_string()] + ); + // WHITENESS is played. + let back = import(&hand_emf(5000, 3000, &[(76, bitblt(0x00FF_0062))])).unwrap(); + assert!(back.warnings.is_empty(), "{:?}", back.warnings); +} + /// An EMR_STRETCHDIBITS copying `png` (a BI_PNG bitmap) to a 10 × 10 box. fn png_stretch_dibits(png: &[u8]) -> Vec { // Bounds; destination, source and its size; the BITMAPINFO and the bits (from the record start); diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 18587ae9a1..7df5c36c58 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -296,7 +296,7 @@ Names in the editing data that aren't UTF-8 (layers, objects, artboards, colors, | DXF | open, export | yes | yes (R12–2018) | R13–2018 closed SPLINE flags and control-point closure on import verified (#1299); circular cubic paths export as native CIRCLE/ARC where stroke semantics permit; R12 weighted strokes remain POLYLINE while non-circular curves keep the existing fallback (`crates/cad/src/tests.rs`); [Autodesk group 70](https://help.autodesk.com/cloudhelp/2024/ENU/AutoCAD-DXF/files/GUID-E1F884F8-AA90-4864-A215-3182D47A9C74.htm). DXF 2000–2018 LAYOUT objects write the paper image origin as 148/149 (was a stray 158), the shade plot codes (2004+) and the model type flag, and the plot style placeholder carries no subclass marker (#3085; structure test `r2000_and_later_files_have_the_structure_cad_apps_require`, checked with ezdxf's audit; not yet confirmed in Autodesk Fusion). DXF Options' Preset menu and `document.exportDxf`'s `dxfPreset: "cnc"` set the CNC / CAM options (#5500); VCarve QA remains open | | HP-GL/2 `.plt`, `.hpgl`, `.hgl`, `.hpg` | — | yes (pen moves, pens and widths, line types and attributes, circles, arcs, wedges and Bézier curves, filled and edged rectangles, wedges and polygons, encoded polylines, scaling and rotation; labels, symbol mode, raster images and hatching aren't drawn) | yes (`.plt`: each visible path once along its line, a pen per color or a pen table, type as outlines, clipping applied, HP-GL or HP-GL/2 with pen colors and encoded polylines) | recognized by its first instructions after any printer escapes, under its own extensions or one no other format reads; `crates/cad/src/hpgl/tests.rs`, `crates/cad/src/hpgl/write/tests.rs` (plots read back by the reader), `tests_hpgl.rs`, `import_fuzz.rs` | | DWG | open, export | no | no | no open spec; DXF instead | -| EMF / WMF | open, export | yes | yes | an oversized embedded PNG or JPEG is refused from its header, before any pixels are decoded (#1776) | +| EMF / WMF | open, export | yes | yes | an oversized embedded PNG or JPEG is refused from its header, before any pixels are decoded (#1776); a picture cut short by a reader limit, images that are not read, clips that are not applied and raster operations other than a plain copy come back as warnings; tests in `crates/metafile/src/tests/import.rs` | | PSD | open, place, export | yes (open: layers as layers, or the merged image with `layers: false`; place: merged image, or the layers embedded as a group of objects with `layers: true`; the merged image comes in clipped by the file's clipping path; every mode and depth; `.psd` and `.pdd`) | yes (layered, max editability, point type as editable type layers with `preserveText`; `tests_psd.rs`) | PSD Import Options on File › Open and on File › Place without Link (Convert Layers to Objects, Flatten Layers to a Single Image, Import Hidden Layers, Import Slices; `tests_psdlayers.rs`, `dialogs/psd_import.rs`); slices (image resource 1050, version 6 and the version 7 and 8 descriptor) open and embed as user slices with `importSlices` (`tests_psdslices.rs`); layer comps aren't read; the embedded ICC profile's tags are 4-byte aligned (#1584) | | PSB | open | yes (layers as layers, or the merged image; the merged image comes in clipped by the file's clipping path) | no | | | PNG / JPEG / GIF / BMP / TIFF / Targa | open, place, export | yes (PNG: `.png` and `.pns`; BMP: `.bmp`, `.dib` and `.rle`; Targa: `.tga`, `.vda`, `.icb` and `.vst`, raw or run-length encoded, with a 16-bit file's one-bit alpha) | yes; TIFF is also selectable in Export for Screens and Asset Export with its existing options | Targa: `tests_tiffbmp.rs`, `tests/links.rs`; TIFF: grayscale with an alpha channel or other extra channels reads (#1466 in part), and so do files that store each channel as its own plane (PlanarConfiguration 2), CMYK ones with their ink amounts, and 1-bit files compressed with CCITT Group 3 (Compression 2 and 3, one- and two-dimensional rows); the ICC profiles embedded in JPEG and TIFF have 4-byte aligned tags (#1584); PNG export marks its colours as sRGB (`embedIcc`, an `sRGB` chunk; no ICC profile is embedded, #2221) | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a4992647b2..1100952625 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2014,6 +2014,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - PICT pictures (`.pct`, `.pic`, and `.pict` as VectorCraft's own name) open and place, as [the help page](https://helpx.adobe.com/archive/illustrator/illustrator-cs5-5-troubleshooting.pdf) lists Macintosh PICT under both opening and placing: versions 1 and 2, with or without the file's 512 bytes of application data, read by the opcodes of Apple's [Opcodes in Pictures](https://dev.os9.ca/techpubs/mac/QuickDraw/QuickDraw-461.html). Lines, rectangles, rounded rectangles, ovals, arcs, polygons and regions become paths in their pen and pattern colors, text becomes point type in the font the picture names, bitmaps, pixel maps and QuickTime JPEG images become images, and the clip region clips what follows it; File › Place places the picture as one group clipped to its frame. Inverted shapes, other transfer modes and other QuickTime formats come back as warnings, and a picture cut short keeps what came before it (`crates/metafile/src/tests/pict.rs`, `crates/engine/src/cmd/fileio/tests_pict.rs`). - EMF and WMF files are read by the shared `craft-metafile` reader and open, place and paste as editable paths, clipping groups, images and point type: an EMF drawn with EMF+ records (on their own, or beside plain records) is read from them instead of opening empty, and hatched and pattern brushes fill with their average color (`crates/metafile/src/tests/import.rs`). - EMF, WMF and EMF+ text keeps the character advances the file gives (ExtTextOut's `lpDx`, DrawDriverString positions) as tracking, with pair kerning off; on a single left to right line, an advance that tracking can't give (wider than its range, or a tab) starts a new point type object where the advance puts it, with an underline or strikethrough filled across the space between and on to the end of the last character's advance; type on more than one line is placed a line at a time, each line aligned by its own advances and split as a single line is (`text_is_spaced_by_its_advances`, `multiline_text_with_advances_aligns_each_line`, `wmf_text_is_spaced_by_its_advances`, `emf_plus_driver_strings_keep_their_positions`, `split_type_keeps_its_underline_and_right_to_left_order`). + - EMF and WMF import warns about what the reader leaves out or does not play as the file asks, with counts: a file, or a metafile image inside it, cut short by one of the reader's limits (items, bytes kept, saved states, clip paths), images that are not read, clips that are not applied, and raster operations other than a plain copy (`a_file_cut_short_by_another_limit_says_so`, `images_the_reader_does_not_read_are_counted`, `clips_that_are_not_applied_are_counted`, `raster_operations_that_are_not_played_are_counted`). - Export As › HP-GL (`document.export {format: "hpgl"}`) writes a `.plt` plot of an artboard, each artboard or the art for pen plotters and cutters: each visible path once along its line in plotter units from the artboard's bottom-left corner or center, a pen per color or a pen table, curves flattened to a tolerance, type as outlines, clipping masks and the artboard's edges applied, and HP-GL or HP-GL/2 (pen colors and encoded polylines); plots read back by the HP-GL reader give the same lines (`crates/cad/src/hpgl/write/tests.rs`, `crates/engine/src/cmd/fileio/tests_hpgl.rs`). - HP-GL export's `insideFirst` cuts each pen's paths inside before outside: a path is drawn only after the pen's paths that lie inside it (a letter's counter, a part's holes, rings within rings), touching outlines counted as inside and crossing ones not, in paint order or nearest first with `sortPaths`, within the plot's work budget; these order rules are VectorCraft's own (`crates/cad/src/hpgl/write/nest.rs`, `crates/cad/src/hpgl/write/tests.rs`, `crates/engine/src/cmd/fileio/tests_hpgl.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); 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).