diff --git a/ROADMAP.md b/ROADMAP.md index 9e298c30..1e477316 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ VectorCraft is a clean-room, open-source, pure-Rust reimplementation of the Adob This file tracks **how far we are and what's left**. Time estimates are wall-clock hours of continuous Claude Opus 5.5 agent work (including builds and the CI gate), given both for **one agent** and for **4–6 parallel agents** on disjoint crates. They are counted from the remaining work (see [Parity estimate](#parity-estimate)), calibrated against measured throughput, and updated as work lands. -_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS import running more of PostScript Level 3 (patterns, Type 3 fonts, mesh shadings, masked images, VM and resource probes), and a preview that stands in saying why (Discord feedback, #466, #469, #474), chords with modifiers recorded in Edit › Keyboard Shortcuts (#487), Type Object Selection by Path Only (#394), Anti-aliased Artwork (#394), Show Pixel Grid (#394), pen and touch taps pressed and lifted within one frame clicking where they tapped and the Linux/Wayland pen-tablet workaround (#491), bounding-box resizes landing on the other objects' anchors and centres with labels, and the Rotate, Scale, Shear and Reflect tools snapping their drags (#482), Shift-drag marquees that toggle the objects or anchors they reach and a Lasso whose Alt subtraction keeps the rest of the selection (#483), the mouse wheel stepping the focused numeric field, the dialogs' and preferences' drag values made such fields (#485), Show The Home Screen When No Documents Are Open (#394), Numbers Without Units Are Points (#394), Auto-Collapse Iconic Panels (#394), the Contextual Task Bar moved by its handle and its More Options menu pinning and resetting its position, a designed macOS DMG window (#493), panels, panel groups and the Tools panel undocking into floating panels that move, stack and dock again (#495), the 0.5.0 startup crash on dual-GPU Linux desktops under Wayland fixed: the desktop's own GPU by default and a restart on the next GPU when one can't show the window (#502), path editing while drawing: Cmd/Ctrl with the Pen borrowing Direct Selection and drawing on afterwards, Pen+Alt reshaping segments, handles of a whole selected path dragging, and handle drags no longer re-gathering snap targets every move (#494), the Control bar's and Properties panel's Convert buttons for a path selected as a whole with Direct Selection, the Pen and the anchor tools (#504), the Pen closing a path dragged out of its first anchor every time, without flicker (#501), the Paste menu checking the system clipboard on a background thread on Linux so an X11 clipboard owner that never answers no longer freezes the window (#510), PDF/`.ai` import keeping a type object's outline stroke on the type, wrapped lines as area type, barcode digits in place and layers in the file's stacking order (#508), the Italian interface (#514); see [Honest assessment](#honest-assessment-2026-10-05))._ +_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS import running more of PostScript Level 3 (patterns, Type 3 fonts, mesh shadings, masked images, VM and resource probes), and a preview that stands in saying why (Discord feedback, #466, #469, #474), chords with modifiers recorded in Edit › Keyboard Shortcuts (#487), Type Object Selection by Path Only (#394), Anti-aliased Artwork (#394), Show Pixel Grid (#394), pen and touch taps pressed and lifted within one frame clicking where they tapped and the Linux/Wayland pen-tablet workaround (#491), bounding-box resizes landing on the other objects' anchors and centres with labels, and the Rotate, Scale, Shear and Reflect tools snapping their drags (#482), Shift-drag marquees that toggle the objects or anchors they reach and a Lasso whose Alt subtraction keeps the rest of the selection (#483), the mouse wheel stepping the focused numeric field, the dialogs' and preferences' drag values made such fields (#485), Show The Home Screen When No Documents Are Open (#394), Numbers Without Units Are Points (#394), Auto-Collapse Iconic Panels (#394), the Contextual Task Bar moved by its handle and its More Options menu pinning and resetting its position, a designed macOS DMG window (#493), panels, panel groups and the Tools panel undocking into floating panels that move, stack and dock again (#495), the 0.5.0 startup crash on dual-GPU Linux desktops under Wayland fixed: the desktop's own GPU by default and a restart on the next GPU when one can't show the window (#502), path editing while drawing: Cmd/Ctrl with the Pen borrowing Direct Selection and drawing on afterwards, Pen+Alt reshaping segments, handles of a whole selected path dragging, and handle drags no longer re-gathering snap targets every move (#494), the Control bar's and Properties panel's Convert buttons for a path selected as a whole with Direct Selection, the Pen and the anchor tools (#504), the Pen closing a path dragged out of its first anchor every time, without flicker (#501), the Paste menu checking the system clipboard on a background thread on Linux so an X11 clipboard owner that never answers no longer freezes the window (#510), PDF/`.ai` import keeping a type object's outline stroke on the type, wrapped lines as area type, barcode digits in place and layers in the file's stacking order (#508), the Italian interface (#514), EPS images with several data sources read in turn (#498), `restore` undoing dictionary changes in EPS import (part of #505); see [Honest assessment](#honest-assessment-2026-10-05))._ ## Where we are @@ -155,7 +155,7 @@ grade by behaviour against `plan/illustrator/`, not by whether a menu item exist - **Files (M4):** - `.vectorcraft` (lossless JSON, compressed, atomic saves, save down to v1), Save As with a format chooser, Save a Copy, Revert, templates, Data Recovery after a crash (browser tabs hold their copies with Web Locks too, so a background tab's copies are never taken for a crash's, and a tab repairs copies another removed when it resumes: #367), background save and export. The desktop app writes its warnings and errors (graphics adapter, a lost graphics device, panics the guard recovered from) to standard error and to `logs/vectorcraft.log` next to the preferences, the two previous runs kept and each file capped at 16 MiB, so a launch without a terminal still leaves something to attach to a bug report (#465). - SVG/SVGZ in and out with SVG Options, Preserve Editing, symbols, filters, rich text and physical units (SVG type under a transform or `viewBox` scale opens at the size it draws at, the scale moved into its size, leading, baseline shift and strokes, a stretch, skew or rotation kept as its transform: #396; a top-level group with a clip path, such as a clipped Inkscape layer, opens as a layer with a clipping mask, its name kept: #420); PDF and PDF-compatible `.ai` in and out (layers, with hidden ones kept hidden with their art and nested ones as sublayers, on pages of up to 64 million operators, art whose layer can't be told going to a hidden, non-printing layer rather than showing or printing: #419; strokes kept live, a fill and a stroke of one outline as one object; a type object's stroke written as each glyph's outline stroked back on the type, so it stays one editable object; lines wrapped in a frame (each wrapped line ending in a space, one left edge, leading and font) back as one area type object, left aligned or justified with its paragraph spacing, where laying it out breaks the lines where the file does; glyphs set apart by gaps wider than a space (a barcode's digit groups) keeping their places; layers stacked as the pages mark them even when one first paints on a later page, art ending a layer's marked content or a clip closing in a later layer kept in its own layer, and layers with no art kept empty: #508; masks, editable text with type set along a curve coming back as type on a path, each artboard's art on its own page only, an `.ai`'s white page under its layers left out rather than coming in as art, spot colours, presets, security, marks and bleed, ICC output intents, raster effects, subset fonts, PDF/X, PDF layers, overprint, page thumbnails, fast web view, PDF 1.3). CMYK images keep their ink amounts from PDF import (and from placed CMYK TIFFs) to PDF export: a CMYK JPEG passes through unchanged, others stay CMYK (DeviceCMYK, or ICC-based CMYK in PDF/X-3/X-4) unless the Output settings convert them; the canvas shows them through the working CMYK profile (#320). - - 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. 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. 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). + - 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. 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). - Place and the Links panel, Package, File Info, Print with print presets, PostScript output and print tiling, slices and Save for Web, Export for Screens and Asset Export, CSS Properties, PNG/PDF/SVG/text clipboard flavours; files copied in a file manager paste as art (SVG, PDF, EMF/WMF, bitmaps) on every desktop platform, and Linux/Wayland reads the compositor's clipboard (#398); on Linux the Paste menu checks the clipboard off the UI thread, so an X11 owner that never answers can't freeze the window (#510); screenshots and other copied pictures paste as embedded images at the view centre everywhere, the web included (#457). - **Performance:** 20k shapes + 1k texts render in 27 ms per full-retina frame (7.8 ms zoomed), 7× faster than the first version. The UI thread never blocks. The web build is 7.1 MB gzipped. - **Graphics processor:** the canvas is rasterized on the CPU and the GPU only composites it, so any GPU that can show the window will do. Preferences › Performance › Graphics Processor (`gpuPreference`) is Automatic by default: the power-saving GPU on Windows and macOS (the integrated one on hybrid-graphics laptops, where presenting from the discrete GPU made the window flicker, #306), and the GPU the desktop runs on under Linux, where KWin on an NVIDIA GPU refused frames from a Ryzen's integrated GPU and 0.5.0 crashed at startup (#502). Power Saving and High Performance pick a kind of GPU; `WGPU_POWER_PREF`, `WGPU_ADAPTER_NAME` and `WGPU_BACKEND` override them. The app picks among the adapters that can present to the window (native backends before OpenGL, hardware before software), and if the window fails on one while starting up it starts again without it instead of crashing; Help › About, `ui.inspect` and the log name the adapter in use. When the browser drops the page's WebGPU device or WebGL context, the web build writes recovery copies, starts again on new graphics with the same app and uploads its textures again; if it can't, it removes the stale canvas and says to reload, which offers the copies back. The desktop app writes the copies and says to restart (#369). On Windows and Linux the tools' cursors are OS cursors (bitmaps rasterized once per display scale), which the system moves itself: painted into the window, they trailed the pointer by the frames waiting to be shown, and the window now queues one frame instead of two (#444). macOS and the web still paint them (macOS sizes cursor bitmaps in points, eframe has no bitmap cursors on the web). Pens and touch drive the tools as the mouse does, and a tap pressed and lifted within one frame clicks where it tapped (#491); pen pressure reaches the Liquify tools' Use Pressure Pen on Windows, while winit 0.30 reports none on macOS or Linux and egui none on the web. On Linux, see `docs/development.md` for pens under Wayland. diff --git a/crates/engine/tests/import_fuzz.rs b/crates/engine/tests/import_fuzz.rs index aa2f8c7f..83450a86 100644 --- a/crates/engine/tests/import_fuzz.rs +++ b/crates/engine/tests/import_fuzz.rs @@ -953,6 +953,12 @@ fn arb_ps_token() -> impl Strategy { "/T3 << /FontType 3 /FontMatrix [0.1 0 0 0.1 0 0] /FontBBox [0 0 9 9] /Encoding [/a /b] /BuildChar { pop pop 9 0 setcharwidth 0 0 5 5 rectfill } >> definefont setfont", "/T4 << /FontType 3 /FontMatrix [1 0 0 1 0 0] /BuildGlyph { pop pop (x) show } /Encoding [/a] >> definefont setfont", "glyphshow", "xshow", "xyshow", "awidthshow", "kshow", "/a", "[1 2 3]", + // Several data sources reading one file in turn, and sources of uneven lengths. + "/f currentfile /ASCIIHexDecode filter def", "{f 3 string readstring pop}", "{f 0 string readstring pop}", "true 3 colorimage", + "3 2 8 [3 0 0 2 0 0] {f 5 string readstring pop} {(ab)} {f 1 string readstring pop} true 3 colorimage", + "<< /ImageType 1 /Width 3 /Height 2 /BitsPerComponent 4 /Decode [0 1 0 1 0 1 0 1] /ImageMatrix [3 0 0 2 0 0] /MultipleDataSources true /DataSource [(a) (bc) {f 2 string readstring pop} (d)] >>", + // Local VM restored: dictionary changes undone, saves restored twice. + "/a 1 def save /a 2 def", "restore", "save dup restore", "1 dict save exch /k 1 put", "cachestatus", "/n 0 def save /n n 1 add store", // Executable strings and keys other than names (PLRM 3rd ed., `cvx`, `load`). "(>>) cvx", "(1 2 add) cvx exec", "(x) cvx dup exec", "0 load", "1 { } def", "(mark) cvx cvlit" ]) diff --git a/crates/eps/src/import/data.rs b/crates/eps/src/import/data.rs index 4a8d56d0..c91e658d 100644 --- a/crates/eps/src/import/data.rs +++ b/crates/eps/src/import/data.rs @@ -635,33 +635,51 @@ impl Interp<'_> { /// The bytes a data procedure gives, called until it gives an empty string or `n` bytes. fn gather(&mut self, p: &Obj, n: usize) -> Res> { - let mut out = vec![]; - while out.len() < n { - self.call(p.clone())?; - let s = self.pop_str()?; - let s = s.borrow(); - if s.is_empty() { - break; - } - self.alloc(s.len())?; - out.extend_from_slice(&s); - } - out.truncate(n); - Ok(out) + Ok(self.read_sources(&[Source::Proc(p.clone())], n, n)?.pop().unwrap_or_default()) } /// `n` bytes of image data from `src` (zeros past its end). fn source_bytes(&mut self, src: &Source, n: usize) -> Res> { - let mut v = match src { - Source::File(f) => self.read(f, n)?, - Source::Str(s) if !s.is_empty() => s.iter().copied().cycle().take(n).collect(), - Source::Str(_) => vec![], - Source::Proc(p) => self.gather(p, n)?, - }; + let mut v = self.read_sources(std::slice::from_ref(src), n, n)?.pop().unwrap_or_default(); v.resize(n, 0); Ok(v) } + /// Up to `n` bytes from each of `sources`, read in turn as an image reads several data sources + /// (PLRM 3rd ed., §4.10 "Images" and `image`/`colorimage` in chapter 8: the sources are read + /// in turn, the same amount from each): each procedure called once a round (its string, + /// whatever its length), each file and string giving `step` bytes a round, until each has + /// given `n` bytes or ended. Procedures that read one file get their own rows that way. + fn read_sources(&mut self, sources: &[Source], n: usize, step: usize) -> Res>> { + let mut out: Vec> = vec![vec![]; sources.len()]; + let mut ended = vec![false; sources.len()]; + while out.iter().zip(&ended).any(|(o, e)| o.len() < n && !e) { + for ((src, o), e) in sources.iter().zip(out.iter_mut()).zip(ended.iter_mut()) { + if *e || o.len() >= n { + continue; + } + let want = step.min(n - o.len()); + let chunk = match src { + Source::File(f) => self.read(f, want)?, + // A string gives its bytes over and over. + Source::Str(s) => { + let at = o.len() % s.len().max(1); + s.get(at..).unwrap_or_default().iter().chain(s.iter().cycle()).take(want).copied().collect() + } + Source::Proc(p) => { + self.call(p.clone())?; + let s = self.pop_str()?.to_vec(); + self.alloc(s.len())?; + s + } + }; + *e = chunk.is_empty(); + o.extend_from_slice(chunk.get(..chunk.len().min(n - o.len())).unwrap_or_default()); + } + } + Ok(out) + } + /// `eexec`: an encrypted font program follows. It is skipped (type is drawn in the app's /// fonts, by name): the font dictionary it would complete is defined as it is. fn eexec(&mut self) -> Res { @@ -849,16 +867,12 @@ impl Interp<'_> { let multi = s.sources.len() > 1; let row_bits = if multi { s.w * bpc } else { s.w * n * bpc }; let row = row_bits.div_ceil(8); - let planes: Vec> = if multi { - let mut v = vec![]; - for src in &s.sources { - v.push(self.source_bytes(src, row * s.h)?); - } - v - } else { - let src = s.sources.first().ok_or(PsError::Ps("undefined", "DataSource".into()))?; - vec![self.source_bytes(src, row * s.h)?] - }; + // Several sources are read in turn, a row at a time each (zeros past their ends). + let sources = if multi { &s.sources[..] } else { s.sources.get(..1).ok_or(PsError::Ps("undefined", "DataSource".into()))? }; + let mut planes = self.read_sources(sources, row * s.h, if multi { row } else { row * s.h })?; + for p in &mut planes { + p.resize(row * s.h, 0); + } let max = ((1u32 << bpc.min(16)) - 1) as f64; let sample = |plane: &[u8], y: usize, i: usize| -> u32 { let bit = y * row * 8 + i * bpc; diff --git a/crates/eps/src/import/interp.rs b/crates/eps/src/import/interp.rs index 1a1d8005..6104d1b5 100644 --- a/crates/eps/src/import/interp.rs +++ b/crates/eps/src/import/interp.rs @@ -49,6 +49,12 @@ pub(crate) struct Interp<'a> { /// Bytes asked for so far (see [`MAX_MEMORY`]). allocated: usize, seed: u32, + /// The `save`s not restored yet, oldest first. + saves: Vec, + /// What dictionary entries were before they changed since the oldest `save` (dictionary, + /// key, old value): `restore` puts them back, as restoring local VM does (PLRM 3rd ed., + /// §3.7.3 "Save and Restore": composite objects get the values they had at the `save`). + journal: Vec<(DictRef, Key, Option)>, /// Where the error that is unwinding was raised (see [`Fault`]). pub fault: Option, /// The width a Type 3 glyph procedure gave (`setcachedevice`, `setcharwidth`). @@ -72,6 +78,13 @@ pub(crate) struct Fault { /// Most procedure names a [`Fault`] keeps. const FAULT_PROCS: usize = 3; +/// Where a `save` was made: the graphics states saved and the journal's length then. +#[derive(Clone, Copy, Debug)] +struct SaveMark { + gstates: usize, + journal: usize, +} + fn new_dict() -> DictRef { Rc::new(RefCell::new(Dict::new())) } @@ -143,6 +156,8 @@ impl<'a> Interp<'a> { ops: 0, allocated: 0, seed: 1, + saves: vec![], + journal: vec![], fault: None, glyph_width: None, g, @@ -190,6 +205,7 @@ impl<'a> Interp<'a> { } self.dicts.clear(); self.stack.clear(); + self.journal.clear(); } // ---------- the operand stack ---------- @@ -879,7 +895,7 @@ impl<'a> Interp<'a> { } Obj::Dict(d) => { let key = k.key().ok_or(PsError::Ps("typecheck", String::new()))?; - insert(&d, key, v)?; + self.insert(&d, key, Some(v))?; true } _ => return ps_err("typecheck", ""), @@ -968,7 +984,7 @@ impl<'a> Interp<'a> { let v = self.pop()?; let k = self.pop()?.key().ok_or(PsError::Ps("typecheck", String::new()))?; let d = self.dicts.last().cloned().ok_or(PsError::Ps("dictstackunderflow", String::new()))?; - insert(&d, k, v)?; + self.insert(&d, k, Some(v))?; } Load => { let k = self.pop()?; @@ -981,7 +997,7 @@ impl<'a> Interp<'a> { let k = self.pop()?.key().ok_or(PsError::Ps("typecheck", String::new()))?; let d = self.dicts.iter().rev().find(|d| d.borrow().contains_key(&k)).or(self.dicts.last()).cloned(); let d = d.ok_or(PsError::Ps("dictstackunderflow", String::new()))?; - insert(&d, k, v)?; + self.insert(&d, k, Some(v))?; } Known => { let k = self.pop()?.key().ok_or(PsError::Ps("typecheck", String::new()))?; @@ -1001,7 +1017,8 @@ impl<'a> Interp<'a> { } Undef => { let k = self.pop()?.key().ok_or(PsError::Ps("typecheck", String::new()))?; - self.pop_dict()?.borrow_mut().remove(&k); + let d = self.pop_dict()?; + self.insert(&d, k, None)?; } CurrentDict => { let d = self.dicts.last().cloned().ok_or(PsError::Ps("dictstackunderflow", String::new()))?; @@ -1034,11 +1051,24 @@ impl<'a> Interp<'a> { } Save => { self.gsave()?; - self.push(Obj::Save(self.saved.len()))?; + self.push(Obj::Save(self.saves.len()))?; + self.saves.push(SaveMark { gstates: self.saved.len(), journal: self.journal.len() }); } Restore => { - let Obj::Save(level) = self.pop()? else { return ps_err("typecheck", "") }; - while self.saved.len() >= level && !self.saved.is_empty() { + let Obj::Save(i) = self.pop()? else { return ps_err("typecheck", "") }; + // A save restored already (or through an outer one) is no longer valid: §3.7.3, + // `invalidrestore`. + let mark = self.saves.get(i).copied().ok_or(PsError::Ps("invalidrestore", String::new()))?; + self.saves.truncate(i); + // The dictionaries as they were at the save, newest change undone first. + for (d, k, old) in self.journal.drain(mark.journal..).rev() { + let mut d = d.borrow_mut(); + match old { + Some(v) => d.insert(k, v), + None => d.remove(&k), + }; + } + while self.saved.len() >= mark.gstates && !self.saved.is_empty() { self.grestore(); } } @@ -1083,9 +1113,12 @@ impl<'a> Interp<'a> { self.push(Obj::Int(1 << 20))?; } } + // An empty font cache of a device's sizes, in `cachestatus`'s order (PLRM 3rd ed., + // chapter 8): bytes used and most, fonts used and most, glyphs used and most, and the + // most bytes one cached glyph may take (positive on a real device: programs divide by it). CacheStatus => { - for _ in 0..7 { - self.push(Obj::Int(0))?; + for v in [0, 1 << 20, 0, 1000, 0, 10_000, 25_000] { + self.push(Obj::Int(v))?; } } StartJob => { @@ -1143,7 +1176,9 @@ impl<'a> Interp<'a> { } Obj::Dict(d) => { let from = self.pop_dict()?.borrow().clone(); - d.borrow_mut().extend(from); + for (k, v) in from { + self.insert(&d, k, Some(v))?; + } self.push(Obj::Dict(d))?; } _ => return ps_err("typecheck", "copy"), @@ -1219,6 +1254,26 @@ impl<'a> Interp<'a> { } } + /// Put `v` under `k` in `d` (`None`: remove it), within [`MAX_ALLOC`] entries; while a `save` + /// is open the old value is kept for `restore`. + fn insert(&mut self, d: &DictRef, k: Key, v: Option) -> Res { + let old = { + let mut dm = d.borrow_mut(); + if v.is_some() && dm.len() >= MAX_ALLOC && !dm.contains_key(&k) { + return Err(PsError::Limit("a dictionary grew too large")); + } + match v { + Some(v) => dm.insert(k.clone(), v), + None => dm.remove(&k), + } + }; + if !self.saves.is_empty() { + self.alloc(2 * std::mem::size_of::())?; + self.journal.push((d.clone(), k, old)); + } + Ok(()) + } + // ---------- resources ---------- /// The resources of `category`. @@ -1293,16 +1348,6 @@ impl<'a> Interp<'a> { } } -/// Put `v` under `k` in `d`, within [`MAX_ALLOC`] entries. -fn insert(d: &DictRef, k: Key, v: Obj) -> Res { - let mut d = d.borrow_mut(); - if d.len() >= MAX_ALLOC && !d.contains_key(&k) { - return Err(PsError::Limit("a dictionary grew too large")); - } - d.insert(k, v); - Ok(()) -} - /// An array or string index. fn index(k: &Obj) -> Res { match *k { diff --git a/crates/eps/src/import/mod.rs b/crates/eps/src/import/mod.rs index bf03d95c..dbcc84ca 100644 --- a/crates/eps/src/import/mod.rs +++ b/crates/eps/src/import/mod.rs @@ -41,6 +41,8 @@ mod tests_fontnames; mod tests_generators; #[cfg(test)] mod tests_illustrator; +#[cfg(test)] +mod tests_images; use std::sync::Arc; diff --git a/crates/eps/src/import/tests_generators.rs b/crates/eps/src/import/tests_generators.rs index 6bc08d5e..e5ad7c4d 100644 --- a/crates/eps/src/import/tests_generators.rs +++ b/crates/eps/src/import/tests_generators.rs @@ -42,7 +42,7 @@ fn a85_flate(data: &[u8]) -> String { } /// Pixel `(x, y)` (straight RGBA) of the first image in `d`. -fn pixel(d: &Document, x: u32, y: u32) -> [u8; 4] { +pub(super) fn pixel(d: &Document, x: u32, y: u32) -> [u8; 4] { let im = all(d) .into_iter() .find_map(|n| match &n.kind { @@ -932,3 +932,30 @@ showpage"##; assert!(all(d).iter().any(|n| fill(n).and_then(Paint::color) == Some(Color::rgb(0.0, 1.0, 0.0)))); assert_eq!(objects(d).len(), 3); } + +/// `restore` puts local VM back as it was at the `save` (PLRM 3rd ed., §3.7.3 "Save and +/// Restore"): dictionaries get back the entries they had, whatever changed them. A program that +/// keeps a count of the saves it has open in a dictionary, and refuses to go past a depth, reads +/// to its end only if each `restore` puts the count back (part of #505: files with many images, +/// each drawn between `save` and `restore`). +#[test] +fn restore_puts_dictionaries_back() { + let r = read( + "/Depth 2 dict def Depth /open 0 put \ + /enter { save Depth /open get 1 add dup 8 gt { rangecheck } if Depth exch /open exch put } def \ + 1 1 100 { pop enter 0 0 1 1 rectfill restore } for Depth /open get 0 eq { 10 10 5 5 rectfill } if", + ); + clean(&r); + assert_eq!(objects(&r.document).len(), 101); + // `def`, `store`, `put`, `undef` and dictionary `copy` are undone, newest first. + super::tests::check( + "/a 1 def /d 2 dict def d /k 1 put save /a 2 def /b 3 def d /k 2 put d /k undef 1 dict dup /z 9 put d copy pop \ + /a 4 store restore a 1 eq /b where not and d /k get 1 eq and d /z known not and", + ); + // A save restored (itself, or through an outer one) is no longer valid: `invalidrestore`. + super::tests::check("save dup restore { restore } stopped"); + super::tests::check("save save exch restore { restore } stopped"); + // `cachestatus` gives a device's cache sizes; its last, the most bytes one cached glyph may + // take, is positive (chapter 8), and programs divide by it. + super::tests::check("cachestatus 7 1 roll 6 { pop } repeat 0 gt"); +} diff --git a/crates/eps/src/import/tests_images.rs b/crates/eps/src/import/tests_images.rs new file mode 100644 index 00000000..641cd1ac --- /dev/null +++ b/crates/eps/src/import/tests_images.rs @@ -0,0 +1,130 @@ +//! Image data read as the PostScript Language Reference (3rd ed.) defines it: several data sources +//! read in turn (§4.10 "Images", `image` and `colorimage` in chapter 8), rows padded to a whole +//! byte at every bit depth and odd widths (§4.10), and Flate predictors undone row by row (§3.13, +//! `FlateDecode` parameters) (#498). + +use super::tests::read; +use super::tests_generators::pixel; + +/// Each pixel of the first image, row by row. +fn pixels(r: &crate::Imported, w: u32, h: u32) -> Vec<[u8; 4]> { + assert!(!r.preview && r.warnings.is_empty(), "{:?}", r.warnings); + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| pixel(&r.document, x, y)).collect() +} + +/// A CMYK image whose four data sources are procedures reading one file, a row of one ink each: +/// the image operator calls them in turn, so each gets its own rows. Read one after the other, +/// the cyan source took the first four rows of all four inks and the image came out in +/// horizontal bands (#498). +#[test] +fn procedures_reading_one_file_are_read_in_turn() { + // 3 × 2 pixels; rows: C M Y K of row 0, then of row 1. + let rows = ["ff0000", "00ff00", "000000", "000000", "000000", "000000", "ffff00", "0000ff"]; + let r = read(&format!( + "/DeviceCMYK setcolorspace 30 20 scale /f currentfile /ASCIIHexDecode filter def \ + << /ImageType 1 /Width 3 /Height 2 /BitsPerComponent 8 /Decode [0 1 0 1 0 1 0 1] /ImageMatrix [3 0 0 -2 0 2] \ + /MultipleDataSources true /DataSource [ {{f 3 string readstring pop}} {{f 3 string readstring pop}} \ + {{f 3 string readstring pop}} {{f 3 string readstring pop}} ] >> image\n{}>", + rows.concat() + )); + let cyan = [0, 174, 239, 255]; + let magenta = [236, 0, 140, 255]; + let yellow = [255, 242, 0, 255]; + let black = [35, 31, 32, 255]; + let white = [255, 255, 255, 255]; + let px = pixels(&r, 3, 2); + let near = |a: [u8; 4], b: [u8; 4]| a.iter().zip(b).all(|(x, y)| x.abs_diff(y) <= 40); + let want = [cyan, magenta, white, yellow, yellow, black]; + assert!(px.iter().zip(want).all(|(a, b)| near(*a, b)), "{px:?}"); + + // The operand form: `{r} {g} {b} true 3 colorimage`, procedures reading one file. + let r = read( + "30 20 scale /f currentfile /ASCIIHexDecode filter def \ + 3 2 8 [3 0 0 -2 0 2] {f 3 string readstring pop} {f 3 string readstring pop} {f 3 string readstring pop} true 3 colorimage\n\ + ff0000 00ff00 0000ff 102030 405060 708090>", + ); + assert_eq!(pixels(&r, 3, 2), [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [16, 64, 112, 255], [32, 80, 128, 255], [48, 96, 144, 255]]); +} + +/// Separate strings and files as the sources: each is read for its own plane, whatever the row. +#[test] +fn separate_sources_keep_their_planes() { + let r = read("30 20 scale 3 2 8 [3 0 0 -2 0 2] <00ff00405060> <0000ff708090> true 3 colorimage"); + assert_eq!(pixels(&r, 3, 2), [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [16, 64, 112, 255], [32, 80, 128, 255], [48, 96, 144, 255]]); +} + +/// Rows of an odd width at 1, 2, 4, 12 and 16 bits a sample start at a byte (their last byte +/// padded), in one source and in several. +#[test] +fn rows_of_every_depth_start_at_a_byte() { + // 3 pixels a row, two rows: 0, max, mid / max, 0, 0 (grey), each row padded. + let cases = [ + (1, "a0 80"), // 101(00000) 100(00000) + (2, "cc c0"), // 11 00 11 (00), 11 00 00 (00) + (4, "f0 80 f0 00"), // f 0 8 (0), f 0 0 (0) + (12, "fff0008000 fff0000000"), // fff 000 800 (0), fff 000 000 (0) + (16, "ffff00008000 ffff00000000"), + ]; + for (bpc, data) in cases { + let data = data.replace(' ', ""); + let r = read(&format!("30 20 scale 3 2 {bpc} [3 0 0 -2 0 2] <{data}> image")); + let px: Vec = pixels(&r, 3, 2).iter().map(|p| p[0]).collect(); + let want: &[u8] = match bpc { + 1 => &[255, 0, 255, 255, 0, 0], + 2 => &[255, 0, 255, 255, 0, 0], + 4 => &[255, 0, 136, 255, 0, 0], + 12 => &[255, 0, 128, 255, 0, 0], + _ => &[255, 0, 128, 255, 0, 0], + }; + assert_eq!(px, want, "{bpc} bits"); + } + // Several 4-bit sources, odd width: each plane's rows padded on their own. + let r = read("30 20 scale 3 2 4 [3 0 0 -2 0 2] <0f00f000> <00f00ff0> true 3 colorimage"); + let (on, off) = (255, 0); + let want = [[on, off, off], [off, on, off], [on, off, on], [off, on, off], [off, off, on], [on, off, on]]; + assert_eq!(pixels(&r, 3, 2), want.map(|[r, g, b]| [r, g, b, 255])); +} + +/// A PNG-predicted Flate image of an odd width: every row filter (None, Sub, Up, Average, Paeth) +/// undone on its own row. +#[test] +fn predicted_rows_of_an_odd_width() { + // 3 RGB pixels a row, five rows; each row predicted with its own filter. + let raw: Vec<[u8; 9]> = (0..5u8).map(|y| std::array::from_fn(|i| (i as u8).wrapping_mul(29).wrapping_add(y.wrapping_mul(53)))).collect(); + let mut encoded = vec![]; + let mut prev = [0u8; 9]; + for (kind, row) in raw.iter().enumerate() { + encoded.push(kind as u8); + for i in 0..9 { + let a = if i >= 3 { row[i - 3] } else { 0 }; + let b = prev[i]; + let c = if i >= 3 { prev[i - 3] } else { 0 }; + let pred = match kind { + 1 => a, + 2 => b, + 3 => ((u16::from(a) + u16::from(b)) / 2) as u8, + 4 => { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } + } + _ => 0, + }; + encoded.push(row[i].wrapping_sub(pred)); + } + prev = *row; + } + let r = read(&format!( + "/DeviceRGB setcolorspace 30 50 scale << /ImageType 1 /Width 3 /Height 5 /BitsPerComponent 8 /Decode [0 1 0 1 0 1] /ImageMatrix [3 0 0 -5 0 5] \ + /DataSource currentfile /ASCII85Decode filter << /Predictor 15 /Colors 3 /Columns 3 >> /FlateDecode filter >> image\n{}", + crate::ps::ascii85(&crate::ps::deflate(&encoded)) + )); + let want: Vec<[u8; 4]> = raw.iter().flat_map(|row| (0..3).map(move |x| [row[3 * x], row[3 * x + 1], row[3 * x + 2], 255])).collect(); + assert_eq!(pixels(&r, 3, 5), want); +}