From 4550b89051a65a4bde053e14552148adae50d717 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Fri, 9 Oct 2026 14:52:06 +0700 Subject: [PATCH] Part of #637: Illustrator EPS and .ai files open from their editing data An Illustrator EPS (version 9 on) carries the document as the app keeps it after its printed page (%AI9_PrivateDataBegin: Zstandard or zlib data in ASCII85 comment lines, or plain), and a .ai file in its first page's /PieceInfo /Illustrator /Private streams (%AI24_ZStandard_Data, %AI12_CompressedData or plain, after header comments). Both now open from it: layers and sublayers (name, order, visibility, lock, printing, preview, dimming, colour), groups as they were nested, compound paths, clipping groups and layer clipping, object names, hidden and locked objects, fills and strokes (grey, CMYK, RGB, spot colours as spot swatches, caps, joins, dashes, fill rule), linear and radial gradients, opacity, blend modes, isolation and knockout, embedded images with their alpha channel, guides and every artboard where it is. Objects with several fills or strokes, effects or brushes come in as their drawn look, named after them, with a warning. Type, symbols, pattern fills, placed files and operators the reader doesn't know are errors: the file opens as before (the EPS page, the .ai file's PDF content) with a first warning saying what was left out. A .ai opened with pages picked, and a .pdf, read the PDF as before. Reading is bounded (512 MB decompressed, nesting, objects, points, stack, image sizes checked against their data) and fuzzed in import_fuzz. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + ROADMAP.md | 10 +- crates/engine/src/cmd/fileio/load.rs | 15 +- crates/engine/src/cmd/fileio/mod.rs | 3 + .../engine/src/cmd/fileio/tests_aiimport.rs | 103 ++ crates/engine/tests/import_fuzz.rs | 179 ++- crates/eps/Cargo.toml | 2 + crates/eps/src/import/ai/lex.rs | 333 +++++ crates/eps/src/import/ai/mod.rs | 158 +++ crates/eps/src/import/ai/obj.rs | 208 +++ crates/eps/src/import/ai/paint.rs | 277 ++++ crates/eps/src/import/ai/read.rs | 1237 +++++++++++++++++ crates/eps/src/import/ai/tests.rs | 393 ++++++ crates/eps/src/import/mod.rs | 35 +- crates/eps/src/lib.rs | 2 +- crates/pdf/src/lib.rs | 2 +- crates/pdf/src/pages.rs | 27 +- crates/testkit/src/ai.rs | 122 ++ crates/testkit/src/lib.rs | 2 + crates/ui-egui/src/i18n/es.tsv | 20 + crates/ui-egui/src/i18n/fr.tsv | 20 + crates/ui-egui/src/i18n/it.tsv | 20 + crates/ui-egui/src/i18n/ru.tsv | 20 + docs/mcp.md | 13 +- 24 files changed, 3188 insertions(+), 14 deletions(-) create mode 100644 crates/engine/src/cmd/fileio/tests_aiimport.rs create mode 100644 crates/eps/src/import/ai/lex.rs create mode 100644 crates/eps/src/import/ai/mod.rs create mode 100644 crates/eps/src/import/ai/obj.rs create mode 100644 crates/eps/src/import/ai/paint.rs create mode 100644 crates/eps/src/import/ai/read.rs create mode 100644 crates/eps/src/import/ai/tests.rs create mode 100644 crates/testkit/src/ai.rs diff --git a/Cargo.lock b/Cargo.lock index b7bc08f4c..1fc75f19b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4476,6 +4476,7 @@ dependencies = [ "flate2", "image", "kurbo 0.13.1", + "ruzstd", "vectorcraft-brush", "vectorcraft-color", "vectorcraft-doc", diff --git a/ROADMAP.md b/ROADMAP.md index 810e07a9a..6bca93366 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-09 (after placed documents (File › Place of a `.vectorcraft` file, linked), 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), the macOS menu bar laid out as a Mac app's (the VectorCraft menu with Settings, Language and Appearance, the system's Hide, Services and Window items; owner's request), Object › Image Trace › Make tracing with the Default preset instead of asking for a preset's name (#543), the rulers' unit menu (#528), Live Corners on stars, polygons and pen paths (#511), the drawing tools snapping to Smart Guides as the Selection tool does, with Construction Guides (#506), the in-window menus' Select All, Cut, Copy and Paste acting on the text field they were opened from (#516), Windows rendering through DX12 before Vulkan (#545), Image Trace tracing colour layers in parallel and refusing traces too large for memory, with the canvas outlining huge selections by their bounds (#525), SVGs of chosen artboards holding only the art over each artboard (#550), the font menu scrolling only its list, under a search field and filters that stay put (#555), a click on an object of a selection making it the key object that Align aligns to and Distribute Spacing spaces from, drawn with a thicker outline (#541), Shape Builder merges taken from the planar map itself, so merged regions leave no inner edge, hairline gap or spur in their outline (#569), `.ai` written by the exports and the CLI (`--export`, `convert`) as Save As writes it (#526), SVG round trips keeping hidden objects and `data-*` attributes (#522), the gradient stop popover staying open after the click that opens it from the Gradient panel (#577), standard ligatures kept under small tracking and dropped only past about −8% / +22% of a word space or when the character asks for them (#583), the Color panel keeping its height as warnings come and go so a drag on its spectrum no longer flickers near white (#578), fonts that font services such as Adobe Fonts load on Windows, listed through DirectWrite (#579), `.ai` files writing their layers and sublayers as PDF layers, hidden ones with their art, and PDF layers nested in layers importing as sublayers (#372), file dialogs on Linux shown off the UI thread so the compositor no longer finds the window not responding, and parented to the window on Linux and Windows (#592), anchors and handle ends picked from 2 px past their drawn size before the segments through them (#593), an image copied in a browser pasting as the picture, not its address (#597), the Free Transform widget with Constrain, Free Transform, Perspective Distort and Free Distort (#598), touch gestures: a two-finger tap undoes and a three-finger tap redoes (#585), the font menus' live preview staying on the text instead of flickering on and off every frame (#563), Edit Artboards showing the active artboard in the Properties panel with New Artboard, Delete Artboard and Exit (#530), the same DirectWrite fonts in `vectorcraft-cli` and its MCP server (#579), SVG layers keeping their names when wrapped and sublayers round-tripping through SVG (#372), a Layers panel that lays out only the rows in view and keeps a document's open rows in its file (#638), a panel dropped on the dock lights up the icon column it goes back to and pops out of its icon (#649), Image Trace's View modes and an Advanced section that stays as it was left (part of #641), Image Trace's Palette: Limited, Full Tone, Automatic and Document Library (part of #641), Image Trace's Create Strokes: line art traced as stroked centre lines (part of #641), a dual-GPU Mac falling back to its other GPU when one fails to start (#651), two fingers dragged on a touch screen panning the canvas (part of #449), the Home screen's Recent Files and a double-clicked New Document preset creating the document (part of #663), quitting no longer crashing on Macs with a Touch Bar (#661), Animated Zoom, Large Tabs, Scale Cursor Proportional to UI and anti-aliased bitmaps in Pixel Preview acting (part of #394), Smart Guides › Spacing Guides (part of #394), Edit Artboards' Properties choosing an artboard's size preset and flipping it between portrait and landscape (#671), Preferences › Type › Additional Fonts Folder loading the fonts of a folder you choose (#683), Canvas Color's light, medium and dark grey (part of #663), a document tab's close box after its title on Windows and Linux, before it on macOS (part of #673), Rearrange All Artboards' layouts (a grid by row or by column, one row, one column) and right-to-left order, one click away in the Properties and Artboards panels (#681), User Interface › Show Tool Group Labels and Cmd+, opening Settings on the Mac (part of #663), the Windows taskbar and Alt+Tab showing VectorCraft's icon, a click with a selection tool making its artboard the active one, and Paste in Place, in Front and in Back pasting onto the active artboard (#693), Photoshop-style raster effects on a pixel-filter pipeline: Effect › Blur › Radial Blur and Smart Blur, Effect › Sharpen › Unsharp Mask (part of gap 2), the Properties panel's number fields all one width (#696), the rotate cursor drawn as a curved double arrow (#701), the Templates folder in the desktop's own documents folder, made when missing (#703), PDF and .ai glyphs whose ToUnicode names no character (U+FFFD) taking their character from the embedded font or keeping their outlines, with a warning (#708); see [Honest assessment](#honest-assessment-2026-10-05))._ +_Last updated: 2026-10-09 (after placed documents (File › Place of a `.vectorcraft` file, linked), 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), the macOS menu bar laid out as a Mac app's (the VectorCraft menu with Settings, Language and Appearance, the system's Hide, Services and Window items; owner's request), Object › Image Trace › Make tracing with the Default preset instead of asking for a preset's name (#543), the rulers' unit menu (#528), Live Corners on stars, polygons and pen paths (#511), the drawing tools snapping to Smart Guides as the Selection tool does, with Construction Guides (#506), the in-window menus' Select All, Cut, Copy and Paste acting on the text field they were opened from (#516), Windows rendering through DX12 before Vulkan (#545), Image Trace tracing colour layers in parallel and refusing traces too large for memory, with the canvas outlining huge selections by their bounds (#525), SVGs of chosen artboards holding only the art over each artboard (#550), the font menu scrolling only its list, under a search field and filters that stay put (#555), a click on an object of a selection making it the key object that Align aligns to and Distribute Spacing spaces from, drawn with a thicker outline (#541), Shape Builder merges taken from the planar map itself, so merged regions leave no inner edge, hairline gap or spur in their outline (#569), `.ai` written by the exports and the CLI (`--export`, `convert`) as Save As writes it (#526), SVG round trips keeping hidden objects and `data-*` attributes (#522), the gradient stop popover staying open after the click that opens it from the Gradient panel (#577), standard ligatures kept under small tracking and dropped only past about −8% / +22% of a word space or when the character asks for them (#583), the Color panel keeping its height as warnings come and go so a drag on its spectrum no longer flickers near white (#578), fonts that font services such as Adobe Fonts load on Windows, listed through DirectWrite (#579), `.ai` files writing their layers and sublayers as PDF layers, hidden ones with their art, and PDF layers nested in layers importing as sublayers (#372), file dialogs on Linux shown off the UI thread so the compositor no longer finds the window not responding, and parented to the window on Linux and Windows (#592), anchors and handle ends picked from 2 px past their drawn size before the segments through them (#593), an image copied in a browser pasting as the picture, not its address (#597), the Free Transform widget with Constrain, Free Transform, Perspective Distort and Free Distort (#598), touch gestures: a two-finger tap undoes and a three-finger tap redoes (#585), the font menus' live preview staying on the text instead of flickering on and off every frame (#563), Edit Artboards showing the active artboard in the Properties panel with New Artboard, Delete Artboard and Exit (#530), the same DirectWrite fonts in `vectorcraft-cli` and its MCP server (#579), SVG layers keeping their names when wrapped and sublayers round-tripping through SVG (#372), a Layers panel that lays out only the rows in view and keeps a document's open rows in its file (#638), a panel dropped on the dock lights up the icon column it goes back to and pops out of its icon (#649), Image Trace's View modes and an Advanced section that stays as it was left (part of #641), Image Trace's Palette: Limited, Full Tone, Automatic and Document Library (part of #641), Image Trace's Create Strokes: line art traced as stroked centre lines (part of #641), a dual-GPU Mac falling back to its other GPU when one fails to start (#651), two fingers dragged on a touch screen panning the canvas (part of #449), the Home screen's Recent Files and a double-clicked New Document preset creating the document (part of #663), quitting no longer crashing on Macs with a Touch Bar (#661), Animated Zoom, Large Tabs, Scale Cursor Proportional to UI and anti-aliased bitmaps in Pixel Preview acting (part of #394), Smart Guides › Spacing Guides (part of #394), Edit Artboards' Properties choosing an artboard's size preset and flipping it between portrait and landscape (#671), Preferences › Type › Additional Fonts Folder loading the fonts of a folder you choose (#683), Canvas Color's light, medium and dark grey (part of #663), a document tab's close box after its title on Windows and Linux, before it on macOS (part of #673), Rearrange All Artboards' layouts (a grid by row or by column, one row, one column) and right-to-left order, one click away in the Properties and Artboards panels (#681), User Interface › Show Tool Group Labels and Cmd+, opening Settings on the Mac (part of #663), the Windows taskbar and Alt+Tab showing VectorCraft's icon, a click with a selection tool making its artboard the active one, and Paste in Place, in Front and in Back pasting onto the active artboard (#693), Photoshop-style raster effects on a pixel-filter pipeline: Effect › Blur › Radial Blur and Smart Blur, Effect › Sharpen › Unsharp Mask (part of gap 2), the Properties panel's number fields all one width (#696), the rotate cursor drawn as a curved double arrow (#701), the Templates folder in the desktop's own documents folder, made when missing (#703), PDF and .ai glyphs whose ToUnicode names no character (U+FFFD) taking their character from the embedded font or keeping their outlines, with a warning (#708), Illustrator EPS and `.ai` files opening from the editing data they carry with their layers, sublayers, groups, names, hidden objects and artboards (part of #637); see [Honest assessment](#honest-assessment-2026-10-05))._ ## Where we are @@ -38,7 +38,7 @@ since 2026-10-01. | **Large missing subsystems** | 0–20% | 3D & Materials (0%), Photoshop-style raster effects and the Effect Gallery (~4 of ~56 filters), SVG Filters, CJK composition (vertical type with kinsoku, tate-chu-yoko and the font's vertical metrics; ruby, mojikumi and proportional vertical metrics open), Variables (data merge), scripting | | **Interaction fidelity:** modifiers, cursors, small behaviours | ~30–40% | The dedicated pass hasn't started, and there has been no side-by-side session with Illustrator yet. A power user notices this first | | **Look & feel** | ~75–80% | Measured against Illustrator 2026 screenshots (2026-10-02); the panels added since haven't been re-measured | -| **File interop** | ~85% | SVG/SVGZ, PDF and PDF-compatible `.ai`, EPS, DXF, EMF/WMF, raster formats, PSD export, Place and Links, Package, Print, clipboard flavours. Affinity documents (Affinity 1–3) open natively as editable art, checked against Affinity's own thumbnails of 22 pinned public files; Affinity export is missing and effects, adjustments and brushes don't import. Affinity isn't an Illustrator feature, so this doesn't raise the parity score. Native `.ai` private data is out of scope by design; DWG has no open spec | +| **File interop** | ~85% | SVG/SVGZ, PDF and PDF-compatible `.ai`, EPS, DXF, EMF/WMF, raster formats, PSD export, Place and Links, Package, Print, clipboard flavours. Affinity documents (Affinity 1–3) open natively as editable art, checked against Affinity's own thumbnails of 22 pinned public files; Affinity export is missing and effects, adjustments and brushes don't import. Affinity isn't an Illustrator feature, so this doesn't raise the parity score. Illustrator EPS and `.ai` files open from the editing data they carry (layers, groups, names, hidden objects, artboards); its type, appearances, symbols, pattern fills and placed files are still to come, and such files open as before (#637). DWG has no open spec | | **Bundled content:** brush, symbol, style and swatch libraries | ~30% | Ours are original and generated in code, and far fewer than Illustrator ships; brush and symbol libraries are still missing | | **Performance** | unverified | Multithreaded, off-thread rendering and caches are in place. The last budget run (2026-10-01, loaded machine) measured 290 ms for a 50k-path fit against a 16 ms budget. Re-run `vectorcraft-cli perf` on an idle machine | | **Robustness** | good, new | ~2,840 tests, property tests, no panics in shipped code (lints, the `guard` safety net, import fuzzing), Data Recovery. Missing: a corpus of real-world files, Windows/Linux/browser QA | @@ -164,7 +164,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; hidden objects written hidden on Save and `convert`, and `data-*` attributes kept as the objects' own data: #522; top-level groups open as layers named from `data-name`, else the id, also when one more group (a transform on all the art, a ``) wraps them, hidden ones hidden, and a file whose art sits loose under a root `` with an id (or `data-name`), as one-layer exports write it, opens with its one layer named from it; nested groups stay groups, except the sublayers VectorCraft writes, marked `data-vc-layer`, and Inkscape sublayers, which open as sublayers in their layer's colour: #372); 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). A masked CMYK image (`/SMask`, `/Mask` or a colour key) keeps its inks too, its alpha coming in as a greyscale opacity mask. - - 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). + - 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. Illustrator EPS files (version 9 on) and `.ai` files (PDF compatible or not) open from the editing data they carry after the page or in the PDF's private data (Zstandard or zlib, ASCII85 in EPS): layers and sublayers with their names, order, visibility, lock, printing, preview, dimming and colour, groups and nesting, compound paths, clipping groups and layer clipping, object names, hidden and locked objects, fills and strokes (grey, CMYK, RGB, spot colours as spot swatches, dashes, caps, joins), linear and radial gradients, opacity, blend modes, isolation and knockout, embedded images with their alpha channel, guides and every artboard where it is. Objects with several fills or strokes, effects or brushes come in as their drawn look, named after them. Files whose editing data has type, symbols, pattern fills, placed files or anything else the reader doesn't read yet open as before (the printed page, the PDF content), with a warning saying what (part of #637). DXF TEXT justified Fit or Aligned spans its two points (stretched, or scaled as a whole). - Affinity documents in (AF.1–AF.3): `.af` (Affinity 3) and `.afdesign`/`.afphoto`/`.afpub` (Affinity 1 and 2, container versions 8–12) open and place as editable art: layers and sublayers, groups (pass-through or isolated), artboards (with their names and backgrounds) and Publisher pages side by side, curves with live corners, parametric shapes (rectangles with corner radii, ellipses, polygons, stars, pies, triangles, trapezoids), compound shapes, solid/linear/elliptical gradient fills and strokes (weight, dashes, alignment, scale-with-object), clipping by nesting, vector and pixel masks, artistic and frame text with fonts, sizes, tracking, leading and alignment, placed images (their original JPEG/PNG) and pixel layers (8/16-bit RGB, CMYK), opacity, visibility, locking and blend modes, at the document's resolution in points. Effects, adjustments, brushes and pressure profiles, transparency gradients, master pages, symbol instances (imported as their cached picture), special shapes (cloud, heart, cog…, imported as ellipses) and Affinity-only blend modes are named in the import warning. A file whose native data can't be read opens as its embedded preview with the reason, and that preview can't be placed. Save never writes back over the Affinity file and there is no Affinity export. 22 public CC0/MIT/Apache-2.0 files, pinned and sha256-verified (`cargo xtask corpus --affinity`), must render within a few levels of the thumbnails Affinity saved in them; [scope, provenance and limits](crates/affinity/README.md). - Placed documents: File › Place of a `.vectorcraft` file links to it by default (`file.place`'s `link`): one locked object showing an artboard of the document (or its art's bounds), read the way placing it without Link reads it, its symbols, patterns and images kept apart from the document's; without Link (or from bytes, not a file) it is an editable copy as before. It stays vectors in every output (SVG, PDF, EPS, EMF, DXF), and so do placed documents inside it, read from their own files up to 8 levels deep (documents that place each other stop there). On screen they draw from bitmaps cached per file and zoom step, shared by every copy and made in the background (a placeholder until ready), so a sheet of them redraws in about a millisecond; exports and prints draw them exactly. Like linked images, a save keeps only a preview of each (JPEG when opaque) unless Include Linked Files; output, and zooming past the preview, read the file again, falling back to the preview with a warning when it is gone or changed. Saves for older versions write them as plain art. Links check, update, relink, Update Links on open, Link Info (the artboard's size), Placement Options, Break Link (also Embed, Object › Expand: the editable copy placing without Link gives, its resources joining the document), Edit Original (the document in a new tab) and the Links panel cover them; saving a document updates the open documents that place it. The native format (v4) keeps data of 8 KB or more after the JSON instead of base64 in it. - 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). @@ -225,7 +225,7 @@ about 1–1.5 agent-hours each, so the other rows (estimated on the older scale) | Layers, artboards, document setup | 5 | 82% | Layers panel: a confirmation before deleting layers that hold art, Collect for Export, row context menus, the Layer Options dialog for several rows with mixed values, Paste Remembers Layers for sublayers, drag-scrolling a long panel while dragging rows; artboard preset polish (Rearrange All Artboards, Document Setup, New Document and the rest of the Layers panel are done) | 4–7 | | View & navigation | 3 | 70% | New View/Edit Views, multiple windows/arrange, Snap to Pixel/Glyph (print tiling is done) | 9–14 | | Guides, grids, smart guides, snapping, rulers | 3 | 85% | global/video rulers, Spacing Guides' distance readouts and spacing for resized or drawn art, Smart Guides' angle and distance readouts while drawing, Alt swapping a ruler guide's orientation, ruler guides selected together with art (marquee, Shift-click), ruler guides as Layers rows with their own lock and hide (#451: Lock Guides locks them all; Make Guides paths are Layers rows already) (dragging existing guides is done, #414; new guides snapping to side midpoints and artboards, and artboard guides, #451; the drawing tools snapping, with Construction Guides, #506; Spacing Guides for moves, #394) | 2–4 | -| File formats | 6 | 85% | Affinity export, and the rest of Affinity import (effects, adjustments, brushes, master pages, live symbol instances), DWG (no open spec: DXF instead), PSD placement as layers, EPS checked against more real files users send (Inkscape files open as vectors; other generators' constructs are tested with programs written from the PostScript Language Reference only; CID-keyed CJK type and `CCITTFaxDecode` images not read), legacy `.ai` files saved without the prolog that defines their operators (their own operator set isn't read yet), the remaining fidelity polish; EPS, DXF, EMF/WMF, TIFF, BMP, Targa, PSD export, SVGZ, PDF security, PDF/X, PDF layers, presets and Affinity import are done | 6–10 (existing scope; Affinity export unestimated) | +| File formats | 6 | 85% | Affinity export, and the rest of Affinity import (effects, adjustments, brushes, master pages, live symbol instances), DWG (no open spec: DXF instead), PSD placement as layers, EPS checked against more real files users send (Inkscape files open as vectors; other generators' constructs are tested with programs written from the PostScript Language Reference only; CID-keyed CJK type and `CCITTFaxDecode` images not read), legacy `.ai` files saved without the prolog that defines their operators (their own operator set isn't read yet), the rest of Illustrator's editing data (type, appearances and live effects, symbols, pattern fills, brushes, placed files: #637), the remaining fidelity polish; EPS, DXF, EMF/WMF, TIFF, BMP, Targa, PSD export, SVGZ, PDF security, PDF/X, PDF layers, presets and Affinity import are done | 6–10 (existing scope; Affinity export unestimated) | | Export for Screens, Asset Export, slices, Save for Web | 3 | 85% | polish only (all four are done) | 2–4 | | Print, colour management, separations, flattener | 3 | 80% | print preview fidelity (flattener presets in PDF and print, overprint in PDF and composite print, Print dialog, presets, PostScript, marks, separations, Print Tiling, print overprint/flattener/bitmap options and printer profile are done) | 1–2 | | Automation | 3 | 60% | Variables (data merge), scripting surface, batch | 8–12 | @@ -249,7 +249,7 @@ lossless SVG/PDF export of live effects with SVG filters, a documented JSON form command, gesture and dialog drivable by agents (MCP, CLI, control channel). ## Out of scope (by design or by law) -- **Native `.ai` private data:** it's undocumented. We read the PDF-compatible part, so Illustrator-only live objects arrive as appearance, and plain groups arrive ungrouped: that part draws each object on its own and marks only layers, clipping groups and groups with opacity, blending or a mask (those come back). Art outside the artboards isn't in that part at all (Illustrator's PDF part leaves it out), so it doesn't open; the import says so in a note (#472). That part also has no paragraphs: area type is written line by line, and comes back as area type only where its font is installed and lays the lines out with the same breaks, else as a point type object per line (#508). Art off the page that a PDF does draw opens on the pasteboard. +- **Native `.ai` private data:** being read since #637 (see File interop). A file whose editing data has what VectorCraft doesn't read yet opens from the PDF-compatible part, where Illustrator-only live objects arrive as appearance, and plain groups arrive ungrouped: that part draws each object on its own and marks only layers, clipping groups and groups with opacity, blending or a mask (those come back). Art outside the artboards isn't in that part at all (Illustrator's PDF part leaves it out), so it doesn't open; the import says so in a note (#472). That part also has no paragraphs: area type is written line by line, and comes back as area type only where its font is installed and lays the lines out with the same breaks, else as a point type object per line (#508). Art off the page that a PDF does draw opens on the pasteboard. - **Adobe cloud services** (Libraries sync, Adobe Fonts, Firefly/generative): these are pluggable provider APIs, not built in. - **Adobe's bundled assets** (swatch/brush/symbol libraries, presets, icons): ours are original. diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index 810e8ebb4..56f8f365d 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -171,7 +171,20 @@ pub fn load_with(name: &str, bytes: &[u8], opts: &LoadOptions) -> Result .then(|| vectorcraft_pdf::editing_with(bytes, opts.password.as_deref())) .flatten() .map(|e| (e.intact, move || Some(e.data))); - restore_or_import(editing, || super::pdfimport::import(bytes, opts))? + restore_or_import(editing, || { + // An Illustrator file: the document its editing data holds, else its PDF. + let whole = opts.pages.is_none() && matches!(format.id, "ai" | "ait"); + let why = match whole.then(|| vectorcraft_pdf::ai_private_data(bytes, opts.password.as_deref())).flatten() { + None => return super::pdfimport::import(bytes, opts), + Some(raw) => match raw.and_then(|raw| vectorcraft_eps::import_ai(&raw)) { + Ok(i) => return Ok((i.document, i.warnings)), + Err(why) => why, + }, + }; + let (doc, mut warnings) = super::pdfimport::import(bytes, opts)?; + warnings.insert(0, vectorcraft_eps::editing_fallback(&why, vectorcraft_eps::PDF_CONTENT)); + Ok((doc, warnings)) + })? } "dxf" => { let (doc, warnings) = super::dxfimport::import(bytes, &opts.dxf)?; diff --git a/crates/engine/src/cmd/fileio/mod.rs b/crates/engine/src/cmd/fileio/mod.rs index 66b355672..8127110ab 100644 --- a/crates/engine/src/cmd/fileio/mod.rs +++ b/crates/engine/src/cmd/fileio/mod.rs @@ -936,6 +936,9 @@ mod tests_psd; #[cfg(test)] mod tests_epsimport; +#[cfg(test)] +mod tests_aiimport; + #[cfg(test)] mod tests_pdflayers; diff --git a/crates/engine/src/cmd/fileio/tests_aiimport.rs b/crates/engine/src/cmd/fileio/tests_aiimport.rs new file mode 100644 index 000000000..46e12f926 --- /dev/null +++ b/crates/engine/src/cmd/fileio/tests_aiimport.rs @@ -0,0 +1,103 @@ +//! Illustrator EPS and `.ai` files open from the editing data they carry: layers, groups, names, +//! hidden objects, artboards, colours and images as they were; what the reader doesn't read yet +//! opens as before, with a warning saying why. + +use std::io::Write as _; +use std::sync::Arc; + +use serde_json::{Value, json}; +use vectorcraft_doc::{Node, NodeKind}; +use vectorcraft_testkit::ai; + +use super::*; + +fn open(s: &mut Session, name: &str, bytes: &[u8], params: Value) -> Result { + let mut p = json!({"name": name, "dataBase64": vectorcraft_format::base64_encode(bytes)}); + if let (Some(p), Some(extra)) = (p.as_object_mut(), params.as_object()) { + p.extend(extra.clone()); + } + s.execute("document.open", &p) +} + +fn zlib(data: &[u8]) -> Vec { + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(data).unwrap(); + e.finish().unwrap() +} + +/// The `.ai` private data of `data`, compressed as such files compress it. +fn compressed(data: &[u8]) -> Vec { + [&b"%AI12_CompressedData"[..], &zlib(data)].concat() +} + +fn names(nodes: &[Arc]) -> Vec { + nodes.iter().map(|n| n.name.clone().unwrap_or_default()).collect() +} + +/// The sample's structure, as [`ai::sample`] builds it. +fn check_sample(s: &Session) { + let doc = &s.doc().unwrap().doc; + assert_eq!(names(&doc.layers), ["Art", "Hidden", "Images"]); + assert_eq!(doc.artboards.len(), 1); + assert_eq!(doc.artboards[0].rect, vectorcraft_geom::Rect::new(0.0, 0.0, 200.0, 100.0)); + let art = doc.layers[0].children().unwrap(); + assert_eq!(names(art), ["Pair", "Ring", "Window", "Shaded", ""]); + let pair = art[0].children().unwrap(); + assert_eq!(names(pair), ["Red", ""]); + assert!(pair[0].visible && !pair[1].visible, "the hidden member stays hidden"); + assert!(matches!(art[1].kind, NodeKind::Compound { .. })); + assert!(matches!(&art[2].kind, NodeKind::Group { clip: true, children } if matches!(children[0].kind, NodeKind::Path { clipping: true, .. }))); + assert!(matches!(art[3].appearance.fill().map(|f| &f.paint), Some(vectorcraft_color::Paint::Gradient(_)))); + assert_eq!((art[4].opacity, art[4].blend), (0.5, vectorcraft_color::BlendMode::Screen)); + let hidden = &doc.layers[1]; + assert!(!hidden.visible); + let sub = &hidden.children().unwrap()[0]; + assert!(sub.locked && matches!(sub.kind, NodeKind::Layer { .. })); + let image = &doc.layers[2].children().unwrap()[0]; + let NodeKind::Image(im) = &image.kind else { panic!("an image") }; + assert_eq!((im.width, im.height), (2, 1)); +} + +#[test] +fn an_illustrator_eps_opens_with_its_layers() { + let mut s = Session::new(); + let r = open(&mut s, "art.eps", &ai::eps(&ai::sample_data()), json!({})).unwrap(); + assert_eq!((&r["format"], &r["warnings"]), (&json!("eps"), &json!([])), "{r}"); + check_sample(&s); + // Saved as EPS and opened again, it comes back the same. + let eps = s.execute("document.exportEps", &json!({})).unwrap(); + let bytes = vectorcraft_format::base64_decode(eps["dataBase64"].as_str().unwrap()).unwrap(); + let r = open(&mut s, "again.eps", &bytes, json!({})).unwrap(); + assert_eq!(r["restored"], json!(true), "{r}"); + check_sample(&s); +} + +#[test] +fn an_illustrator_ai_file_opens_from_its_editing_data() { + let mut s = Session::new(); + let r = open(&mut s, "art.ai", &ai::ai(&compressed(&ai::sample_data())), json!({})).unwrap(); + assert_eq!((&r["format"], &r["warnings"]), (&json!("ai"), &json!([])), "{r}"); + check_sample(&s); + // Pages picked: the PDF's. + let r = open(&mut s, "art.ai", &ai::ai(&compressed(&ai::sample_data())), json!({"pages": "1"})).unwrap(); + assert_eq!(names(&s.doc().unwrap().doc.layers).len(), 1, "{r}"); + // A PDF that happens to carry the data opens as a PDF. + open(&mut s, "art.pdf", &ai::ai(&compressed(&ai::sample_data())), json!({})).unwrap(); + assert_ne!(names(&s.doc().unwrap().doc.layers), ["Art", "Hidden", "Images"]); +} + +#[test] +fn what_the_reader_doesnt_read_opens_as_before_with_a_warning() { + let text = ai::editing_data(200.0, 100.0, &ai::layer("Words", "/AI11Text :\n0 /FreeUndo ,\n0 /FrameIndex ,\n0 /StoryIndex ,\n;\n")); + let mut s = Session::new(); + let r = open(&mut s, "words.eps", &ai::eps(text.as_bytes()), json!({})).unwrap(); + let first = r["warnings"][0].as_str().unwrap(); + assert!(first.contains("editing data couldn't be read") && first.contains("type") && first.contains("printed page"), "{r}"); + assert!(s.doc().unwrap().doc.layers.iter().any(|l| l.children().is_some_and(|c| !c.is_empty())), "the page's line"); + let r = open(&mut s, "words.ai", &ai::ai(&compressed(text.as_bytes())), json!({})).unwrap(); + let first = r["warnings"][0].as_str().unwrap(); + assert!(first.contains("type") && first.contains("PDF content"), "{r}"); + // Damaged data says so. + let r = open(&mut s, "damaged.ai", &ai::ai(b"%AI12_CompressedData not zlib at all"), json!({})).unwrap(); + assert!(r["warnings"][0].as_str().unwrap().contains("zlib"), "{r}"); +} diff --git a/crates/engine/tests/import_fuzz.rs b/crates/engine/tests/import_fuzz.rs index 8b6396007..9818b03f4 100644 --- a/crates/engine/tests/import_fuzz.rs +++ b/crates/engine/tests/import_fuzz.rs @@ -4,7 +4,8 @@ //! must load as an error or as a document that then renders and exports, without a panic; nor may //! bitmaps, PDF and text pasted from other apps, nor EMF and WMF pictures (damaged files, records //! of every kind with random contents) opened, placed or pasted, nor EPS and PostScript files -//! (damaged ones, hostile programs) read by the PostScript interpreter, nor Affinity documents +//! (damaged ones, hostile programs) read by the PostScript interpreter, nor the editing data of +//! Illustrator EPS and `.ai` files (damaged, hostile operators and sections), nor Affinity documents //! (mutated object streams, archives and indexed PNG previews, hostile image dimensions). //! //! `PROPTEST_CASES=20000 cargo test -p vectorcraft-engine --test import_fuzz` runs a deeper search. @@ -1295,6 +1296,182 @@ proptest! { } } +// ---------- Illustrator editing data ---------- + +/// The operators, operands and section comments of hostile editing data. +fn arb_ai_token() -> impl Strategy { + prop_oneof![ + arb_num(), + prop::sample::select(vec![ + "m", + "l", + "L", + "c", + "C", + "v", + "V", + "y", + "Y", + "h", + "H", + "N", + "n", + "F", + "f", + "S", + "s", + "B", + "b", + "W", + "(b) *", + "u", + "U", + "*u", + "*U", + "q", + "Q", + "LB", + "Lb", + "Ln", + "(name) Ln", + "g", + "G", + "k", + "K", + "x", + "X", + "Xa", + "XA", + "Xx", + "XX", + "(Ink) 0.5 x", + "O", + "R", + "XR", + "w", + "J", + "j", + "M", + "d", + "[6 3] 0 d", + "[]0 d", + "Xy", + "2 0.5 1 1 1 Xy", + "99 -4 Xy", + "Xw", + "1 Xw", + "A", + "1 A", + "Ae", + "XW", + "1 (style) XW", + "9 () XW", + "Bd", + "(G) 1 3 Bd", + "Bs", + "0 0 0 0 1 0 0 2 1 6 50 0 Bs", + "BD", + "Bb", + "1 Bb", + "BB", + "2 BB", + "1 (G) 0 0 0 1 1 0 0 1 0 0 1 Bg", + "Bg", + "Bm", + "1e308 0 0 1e-308 0 0 Bm", + "Bh", + "XN", + "/DeviceCMYK XN", + "/DeviceGray XN", + "[ 1 0 0 1 0 0 ] 0 0 2 2 2 2 8 3 1 0 1 0", + "[ 1 0 0 1 0 0 ] 0 0 99999 99999 99999 99999 1 1 0 0 0 0", + "[", + "]", + "/Name", + "(text)", + "", + ":", + ";", + ",", + "/ArtDictionary :", + "/XMLUID : (A_x41_) ; (AI10_ArtUID) ,", + "(n) /String (AIArtName) ,", + "/Document :", + "/Array :", + "/Dictionary :", + "0 0 /RealPoint (PositionPoint1) ,", + "/AI11Text :", + "/SymbolInstance :", + "/Binary : /ASCII85Decode ,", + "~>", + "p", + "To", + "frobnicate", + "\n%AI5_BeginLayer\n", + "\n%AI5_EndLayer--\n", + "\n%_", + "\n%AI5_BeginRaster\n", + "\n%AI5_EndRaster\n", + "\n%%BeginData: 12\rXI\n", + "\n%%EndData\n", + "\n%AI5_BeginGradient: (G)\n", + "\n%AI14_BeginSymbol\n", + "\n%AI10_EndSymbol\n", + "\n%AI17_Begin_Content_if_version_gt:24 4\n", + "\n%AI17_Alternate_Content\n", + "\n%AI17_End_Versioned_Content\n", + "\n%AI3_Cropmarks: 0 0 1e308 -1e308\n", + "\n%AI5_ArtSize: 0 0\n", + "\n%AI9_ColorModel: 2\n", + "\n%AI5_BeginPlace\n", + "\n%AI26_BeginPlacedObjectPreview\n", + "\n%%PageTrailer\n", + ]) + .prop_map(str::to_string), + ] +} + +/// The sample editing data, compressed as `.ai` files compress it. +fn ai_sample_compressed() -> Vec { + use std::io::Write as _; + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(&vectorcraft_testkit::ai::sample_data()).unwrap(); + [&b"%AI12_CompressedData"[..], &e.finish().unwrap()].concat() +} + +proptest! { + #![proptest_config(config())] + + /// Hostile editing data in Illustrator EPS and `.ai` files: operators in any order with any + /// operands, sections and dictionaries left open, images of any size. + #[test] + fn ai_hostile_editing_data_never_panics(tokens in prop::collection::vec(arb_ai_token(), 0..80)) { + use vectorcraft_testkit::ai; + let data = ai::editing_data(200.0, 100.0, &format!("%AI5_BeginLayer\n1 1 1 1 0 0 1 0 79 128 255 0 50 0 Lb\n(L) Ln\n{}\nLB\n", tokens.join(" "))); + survive("hostile editing data", || vectorcraft_eps::import_ai(data.as_bytes()).ok().map(|r| r.document))?; + let eps = ai::eps(data.as_bytes()); + survive("hostile editing data in an EPS", || vectorcraft_engine::cmd::fileio::load("x.eps", &eps).ok().map(|l| l.doc))?; + } + + /// Damaged editing data, as it is and compressed in a `.ai` file. + #[test] + fn ai_mutated_editing_data_never_panics(cut in 0usize..4_000, edits in prop::collection::vec((0usize..4_000, any::()), 0..12)) { + static PLAIN: std::sync::OnceLock> = std::sync::OnceLock::new(); + static PACKED: std::sync::OnceLock> = std::sync::OnceLock::new(); + for (what, sample) in [("editing data", PLAIN.get_or_init(vectorcraft_testkit::ai::sample_data)), ("compressed editing data", PACKED.get_or_init(ai_sample_compressed))] { + let mut bytes = sample.clone(); + for &(at, b) in &edits { + let n = bytes.len(); + bytes[at % n] = b; + } + bytes.truncate(cut.max(4)); + survive(what, || vectorcraft_eps::import_ai(&bytes).ok().map(|r| r.document))?; + let file = vectorcraft_testkit::ai::ai(&bytes); + survive(what, || vectorcraft_engine::cmd::fileio::load("x.ai", &file).ok().map(|l| l.doc))?; + } + } +} + // ---------- DXF ---------- /// The rich document exported as DXF (2018: hatches, splines, text, images; R12: polylines). diff --git a/crates/eps/Cargo.toml b/crates/eps/Cargo.toml index 00132e662..0d835d03a 100644 --- a/crates/eps/Cargo.toml +++ b/crates/eps/Cargo.toml @@ -22,6 +22,8 @@ vectorcraft-render = { workspace = true } kurbo = { workspace = true } image = { workspace = true } flate2 = { workspace = true } +# Illustrator's editing data (Zstandard): pure Rust, builds for wasm. +ruzstd = "0.9" [dev-dependencies] vectorcraft-testkit = { workspace = true } diff --git a/crates/eps/src/import/ai/lex.rs b/crates/eps/src/import/ai/lex.rs new file mode 100644 index 000000000..25f757b7f --- /dev/null +++ b/crates/eps/src/import/ai/lex.rs @@ -0,0 +1,333 @@ +//! The scanner of the editing data: PostScript-like tokens, read line by line. +//! +//! - A line starting with `%_` holds tokens like any other line (they are marked [`Token::hidden`]: +//! readers of the printed format skip them as comments). +//! - Any other line starting with `%` is a [`Tok::Comment`] (the section markers). +//! - `%` later in a line starts a comment that is skipped. +//! - The binary image data after `%%BeginData:` is one [`Tok::Data`]; the binary previews of placed +//! files are skipped. + +use super::super::lex::{find, hex_decode}; + +/// A token. +#[derive(Clone, Debug, PartialEq)] +pub(super) enum Tok<'a> { + Num(f64), + /// A literal `(…)` string, escapes undone. + Str(Vec), + /// A hexadecimal `<…>` string. + Hex(Vec), + /// A literal `/name`. + Name(&'a [u8]), + /// An operator (any other word, `[` and `]` included). + Word(&'a [u8]), + /// A comment line, without its first `%`. + Comment(&'a [u8]), + /// An image's samples: what follows `XI` in a `%%BeginData:` section. + Data(&'a [u8]), +} + +#[derive(Clone, Debug, PartialEq)] +pub(super) struct Token<'a> { + pub tok: Tok<'a>, + /// Read from a `%_` line. + pub hidden: bool, +} + +/// Binary sections skipped whole: (begin, end) markers. +const SKIPPED: [(&[u8], &[u8]); 2] = [(b"%AI26_BeginPlacedObjectPreview", b"%AI26_EndPlacedObjectPreview"), (b"%%BeginDocument", b"%%EndDocument")]; + +fn is_space(b: u8) -> bool { + matches!(b, b' ' | b'\t' | b'\r' | b'\n' | b'\x0c' | b'\0') +} + +fn is_delim(b: u8) -> bool { + matches!(b, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%') +} + +fn is_eol(b: u8) -> bool { + matches!(b, b'\r' | b'\n') +} + +pub(super) struct Lexer<'a> { + src: &'a [u8], + pos: usize, + /// The current line started with `%_`. + hidden: bool, +} + +impl<'a> Lexer<'a> { + pub fn new(src: &'a [u8]) -> Self { + Self { src, pos: 0, hidden: false } + } + + fn peek(&self) -> Option { + self.src.get(self.pos).copied() + } + + fn rest(&self) -> &'a [u8] { + self.src.get(self.pos..).unwrap_or_default() + } + + fn at_line_start(&self) -> bool { + self.pos == 0 || self.src.get(self.pos - 1).is_some_and(|b| is_eol(*b)) + } + + /// The rest of the current line (without its end), the position moved past it. + fn line(&mut self) -> &'a [u8] { + let rest = self.rest(); + let n = rest.iter().position(|b| is_eol(*b)).unwrap_or(rest.len()); + self.pos += n; + rest.get(..n).unwrap_or_default() + } + + /// Skip past the next `needle` (the end of data that isn't tokens) → what was skipped. + pub fn skip_past(&mut self, needle: &[u8]) -> &'a [u8] { + let rest = self.rest(); + let n = find(rest, needle).map_or(rest.len(), |i| i + needle.len()); + self.pos += n; + rest.get(..n).unwrap_or_default() + } + + /// The next token; `None` at the end. + pub fn next_token(&mut self) -> Option> { + loop { + let b = self.peek()?; + if is_eol(b) { + self.pos += 1; + self.hidden = false; + continue; + } + if is_space(b) { + self.pos += 1; + continue; + } + if b == b'%' { + if self.at_line_start() { + if self.rest().starts_with(b"%_") { + self.pos += 2; + self.hidden = true; + continue; + } + let line = self.line(); + if let Some(t) = self.special(line) { + return Some(t); + } + return Some(Token { tok: Tok::Comment(line.get(1..).unwrap_or_default()), hidden: false }); + } + // A comment later in a line. + self.line(); + continue; + } + let hidden = self.hidden; + self.pos += 1; + let tok = match b { + b'(' => Tok::Str(self.string()), + b'<' => { + let rest = self.rest(); + let end = rest.iter().position(|b| *b == b'>').unwrap_or(rest.len()); + self.pos += (end + 1).min(rest.len()); + // Malformed digits read as an empty string. + Tok::Hex(rest.get(..end).and_then(hex_decode).unwrap_or_default()) + } + b'[' | b']' | b'{' | b'}' | b')' | b'>' => Tok::Word(self.src.get(self.pos - 1..self.pos).unwrap_or_default()), + b'/' => Tok::Name(self.word()), + _ => { + self.pos -= 1; + let w = self.word(); + match number(w) { + Some(v) => Tok::Num(v), + None => Tok::Word(w), + } + } + }; + return Some(Token { tok, hidden }); + } + } + + /// The token a comment line stands for when it starts a section read differently: image data + /// (`%%BeginData:` followed by `XI`) and skipped binary sections. + fn special(&mut self, line: &'a [u8]) -> Option> { + if let Some((_, end)) = SKIPPED.iter().find(|(b, _)| line.starts_with(b)) { + let after = find(self.rest(), end).map_or(self.src.len(), |i| self.pos + i); + self.pos = after; + self.line(); + return Some(Token { tok: Tok::Comment(line.get(1..).unwrap_or_default()), hidden: false }); + } + let n = line.strip_prefix(b"%%BeginData:")?; + let n: usize = std::str::from_utf8(n).ok()?.split_whitespace().next()?.parse().ok()?; + // The count starts after the comment's line end character (a following `\n` counts). + let from = self.pos + 1; + let region = self.src.get(from..)?; + let skip = region.iter().take_while(|b| is_space(**b)).count(); + if !region.get(skip..)?.starts_with(b"XI") { + return None; + } + let mut data = from + skip + 2; + match (self.src.get(data), self.src.get(data + 1)) { + (Some(b'\r'), Some(b'\n')) => data += 2, + (Some(b'\r' | b'\n'), _) => data += 1, + _ => {} + } + // The count leaves out an alpha channel: the data ends where `%%EndData` starts. + let declared = from.saturating_add(n).min(self.src.len()).max(data); + let window = + self.src.get(declared.saturating_sub(4).max(data)..declared.saturating_add(n.saturating_mul(2)).saturating_add(64).min(self.src.len())); + let end = match window.and_then(|w| find(w, b"%%EndData")) { + Some(i) => declared.saturating_sub(4).max(data) + i, + None => declared, + }; + self.pos = end; + Some(Token { tok: Tok::Data(self.src.get(data..end).unwrap_or_default()), hidden: false }) + } + + /// A regular token's characters. + fn word(&mut self) -> &'a [u8] { + let start = self.pos; + while let Some(b) = self.peek() { + if is_space(b) || is_delim(b) { + break; + } + self.pos += 1; + } + self.src.get(start..self.pos).unwrap_or_default() + } + + /// A literal string after its `(` (an unterminated one ends with the data). + fn string(&mut self) -> Vec { + let mut out = vec![]; + let mut depth = 0usize; + while let Some(b) = self.peek() { + self.pos += 1; + match b { + b'(' => { + depth += 1; + out.push(b); + } + b')' if depth == 0 => break, + b')' => { + depth -= 1; + out.push(b); + } + b'\\' => { + let Some(e) = self.peek() else { break }; + self.pos += 1; + match e { + b'n' => out.push(b'\n'), + b'r' => out.push(b'\r'), + b't' => out.push(b'\t'), + b'b' => out.push(8), + b'f' => out.push(12), + b'\r' => { + if self.peek() == Some(b'\n') { + self.pos += 1; + } + } + b'\n' => {} + b'0'..=b'7' => { + let mut v = u32::from(e - b'0'); + for _ in 0..2 { + match self.peek() { + Some(d @ b'0'..=b'7') => { + v = v * 8 + u32::from(d - b'0'); + self.pos += 1; + } + _ => break, + } + } + out.push((v & 0xff) as u8); + } + other => out.push(other), + } + } + _ => out.push(b), + } + } + out + } +} + +/// A finite number token (`12`, `-3.5`, `.5`, `1e-3`), or `None`. +fn number(w: &[u8]) -> Option { + let first = *w.first()?; + if !(first.is_ascii_digit() || matches!(first, b'+' | b'-' | b'.')) { + return None; + } + if w.iter().any(|b| b.is_ascii_alphabetic() && !matches!(b, b'e' | b'E')) { + return None; + } + let v: f64 = std::str::from_utf8(w).ok()?.parse().ok()?; + v.is_finite().then_some(v) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn all(src: &[u8]) -> Vec> { + let mut l = Lexer::new(src); + std::iter::from_fn(|| l.next_token()).collect() + } + + fn t(tok: Tok<'_>, hidden: bool) -> Token<'_> { + Token { tok, hidden } + } + + #[test] + fn hidden_lines_and_comments() { + let toks = all(b"%AI5_BeginLayer\r\n1 (a\\)b) Ln\r%_/X : ;\n0 %_BS\n<4142>\n"); + assert_eq!( + toks, + vec![ + t(Tok::Comment(b"AI5_BeginLayer"), false), + t(Tok::Num(1.0), false), + t(Tok::Str(b"a)b".to_vec()), false), + t(Tok::Word(b"Ln"), false), + t(Tok::Name(b"X"), true), + t(Tok::Word(b":"), true), + t(Tok::Word(b";"), true), + t(Tok::Num(0.0), false), + t(Tok::Hex(b"AB".to_vec()), false), + ] + ); + } + + #[test] + fn image_data_is_one_token() { + // The count starts after the comment's `\r`: `\nXI\n` and 3 samples; an alpha channel + // after them isn't counted. + let src = b"[1 0 0 1 0 0] XI\r\n%%BeginData: 7\r\nXI\n(%)A%%EndData\r\nXH\r\n"; + let toks = all(src); + assert!(toks.contains(&t(Tok::Data(b"(%)A"), false)), "{toks:?}"); + assert_eq!(toks.last(), Some(&t(Tok::Word(b"XH"), false))); + // A section of hex lines is comments. + let toks = all(b"%%BeginData: 9 Hex Bytes\r\n%00FF\r\n%%EndData\r\n1\r\n"); + assert_eq!(toks.last(), Some(&t(Tok::Num(1.0), false))); + } + + #[test] + fn binary_previews_are_skipped() { + let toks = all(b"%AI26_BeginPlacedObjectPreview\r\n2 1 (\x00\xff\r\n%AI26_EndPlacedObjectPreview\r\nN\r\n"); + assert_eq!(toks.last(), Some(&t(Tok::Word(b"N"), false))); + assert_eq!(toks.len(), 2); + } + + #[test] + fn skip_past_data() { + let mut l = Lexer::new(b"/Binary : /ASCII85Decode ,\r\n%_(abc[%\r\n%x~>\r\n%_; 1"); + for _ in 0..4 { + l.next_token(); + } + assert_eq!(l.skip_past(b"~>"), b"\r\n%_(abc[%\r\n%x~>"); + assert_eq!(l.next_token().map(|t| t.tok), Some(Tok::Word(b";"))); + assert_eq!(l.next_token().map(|t| t.tok), Some(Tok::Num(1.0))); + } + + #[test] + fn hostile_input_ends() { + for src in [&b"(unterminated \\"[..], b"<12", b"%%BeginData: 99999999999\r\nXI", b"%%BeginData: 5\rXI\r", b"/", b"1e999 nan"] { + let toks = all(src); + assert!(toks.len() < 10); + } + } +} diff --git a/crates/eps/src/import/ai/mod.rs b/crates/eps/src/import/ai/mod.rs new file mode 100644 index 000000000..ea4219a29 --- /dev/null +++ b/crates/eps/src/import/ai/mod.rs @@ -0,0 +1,158 @@ +//! Illustrator's editing data: the document as the app keeps it, which an Illustrator EPS carries +//! after its printed page and a `.ai` file (PDF compatible or not) in its first page's +//! `/PieceInfo /Illustrator /Private` streams. +//! +//! - In an EPS it starts at the `%AI9_PrivateDataBegin` comment: header comments, then +//! `%AI24_DataStream` (Zstandard) or `%AI9_DataStream` (zlib) and the compressed data as ASCII85 +//! on comment lines, up to `%AI9_PrivateDataEnd`. Data that isn't compressed follows the header +//! as it is. +//! - In a `.ai` file the streams joined start with `%AI24_ZStandard_Data` (Zstandard) or +//! `%AI12_CompressedData` (zlib), else are the data itself ([`decode_private`]). +//! +//! The data is a PostScript-like program of the legacy Illustrator format's operators (layers, +//! groups, paths, colours, gradients, images) with newer ones (transparency, hidden objects) and +//! dictionaries (names, artboards, type). `lex` scans it, `read` builds the document. +//! +//! Reading is bounded: the data decompresses to at most [`MAX_DATA`], and the reader caps nesting, +//! objects and points. Anything it doesn't read is an error: the caller opens the file the way it +//! did before (the printed page, or the PDF), with a warning saying why. + +mod lex; +mod obj; +mod paint; +mod read; + +#[cfg(test)] +mod tests; + +use std::io::Read as _; + +use super::lex::find; +use crate::ps; + +pub use read::read; + +/// Largest editing data read, decompressed (bytes). +pub const MAX_DATA: u64 = 512 << 20; +/// Largest Zstandard window accepted. +const MAX_WINDOW: u64 = 128 << 20; + +const EPS_BEGIN: &[u8] = b"%AI9_PrivateDataBegin"; +const EPS_END: &[u8] = b"%AI9_PrivateDataEnd"; + +/// The editing data an Illustrator EPS carries (`ps`: its PostScript), decoded: `None` without +/// one, an error when it can't be decoded. +pub fn eps_data(ps: &[u8]) -> Option, String>> { + let start = find(ps, EPS_BEGIN)? + EPS_BEGIN.len(); + let rest = ps.get(start..)?; + let section = rest.get(..find(rest, EPS_END).unwrap_or(rest.len())).unwrap_or_default(); + Some(eps_section(section)) +} + +/// The editing data between the EPS markers: header comments, then the data (encoded or not). +fn eps_section(section: &[u8]) -> Result, String> { + let mut header = Vec::new(); + let mut lines = section.split(|b| matches!(b, b'\n' | b'\r')).filter(|l| !l.is_empty()); + let mut codec = None; + for line in lines.by_ref() { + if line.starts_with(b"%AI24_DataStream") { + codec = Some(Codec::Zstd); + break; + } + if line.starts_with(b"%AI9_DataStream") { + codec = Some(Codec::Zlib); + break; + } + header.extend_from_slice(line); + header.extend_from_slice(b"\n"); + if header.len() as u64 > MAX_DATA { + return Err(too_large()); + } + } + let Some(codec) = codec else { + // Not compressed: the section is the data, as it is (images' samples are binary). + if section.len() as u64 > MAX_DATA { + return Err(too_large()); + } + return Ok(section.to_vec()); + }; + let mut text = String::new(); + for line in lines { + let body = line.strip_prefix(b"%").unwrap_or(line); + text.push_str(std::str::from_utf8(body).map_err(|_| damaged("its ASCII85 encoding"))?); + if text.len() as u64 > MAX_DATA { + return Err(too_large()); + } + } + if !text.contains("~>") { + text.push_str("~>"); + } + let packed = ps::ascii85_decode(&text).ok_or_else(|| damaged("its ASCII85 encoding"))?; + let mut data = header; + data.extend(codec.decode(&packed, MAX_DATA.saturating_sub(data.len() as u64))?); + Ok(data) +} + +/// The editing data of a `.ai` file: its `AIPrivateData` streams joined (`raw`), decompressed. The +/// compressed data may follow header comments that aren't. +pub fn decode_private(raw: &[u8]) -> Result, String> { + if raw.len() as u64 > MAX_DATA { + return Err(too_large()); + } + for (marker, codec) in [(&b"%AI24_ZStandard_Data"[..], Codec::Zstd), (b"%AI12_CompressedData", Codec::Zlib)] { + if let Some(at) = find(raw, marker) { + let (header, body) = raw.split_at(at); + let mut data = header.to_vec(); + data.extend(codec.decode(body.get(marker.len()..).unwrap_or_default(), MAX_DATA.saturating_sub(header.len() as u64))?); + return Ok(data); + } + } + Ok(raw.to_vec()) +} + +#[derive(Clone, Copy)] +enum Codec { + Zstd, + Zlib, +} + +impl Codec { + /// `data` decompressed, at most `max` bytes. + fn decode(self, data: &[u8], max: u64) -> Result, String> { + let mut out = Vec::new(); + match self { + Codec::Zlib => { + flate2::read::ZlibDecoder::new(data).take(max + 1).read_to_end(&mut out).map_err(|_| damaged("its zlib compression"))?; + } + Codec::Zstd => { + let mut src = data; + // The data may be several frames one after the other. + while src.iter().any(|b| *b != 0) { + let left = (max + 1).saturating_sub(out.len() as u64); + if left == 0 { + break; + } + let d = ruzstd::decoding::StreamingDecoder::new_with_max_window_size(&mut src, MAX_WINDOW) + .map_err(|_| damaged("its Zstandard compression"))?; + let before = out.len(); + d.take(left).read_to_end(&mut out).map_err(|_| damaged("its Zstandard compression"))?; + if out.len() == before { + break; + } + } + } + } + if out.len() as u64 > max { + return Err(too_large()); + } + Ok(out) + } +} + +fn damaged(what: &str) -> String { + format!("{what} is damaged") +} + +fn too_large() -> String { + format!("it decompresses to more than {} MB", MAX_DATA >> 20) +} diff --git a/crates/eps/src/import/ai/obj.rs b/crates/eps/src/import/ai/obj.rs new file mode 100644 index 000000000..a2ce586d8 --- /dev/null +++ b/crates/eps/src/import/ai/obj.rs @@ -0,0 +1,208 @@ +//! Operand values, and the dictionaries the editing data writes postfix: `/Type :` opens one, +//! `values key ,` adds an entry (a type name such as `/Real` may stand before the key; in an +//! `/Array` there is no key), `;` closes it (after a last `values /key` entry, or a bare value: its +//! content), leaving it as a value. + +use std::rc::Rc; + +#[derive(Clone, Debug, PartialEq)] +pub(super) enum V { + Num(f64), + Str(Vec), + Name(String), + Arr(Vec), + Obj(Rc), + /// `[`, until its `]`. + Mark, +} + +impl V { + pub fn num(&self) -> Option { + match self { + V::Num(v) => Some(*v), + _ => None, + } + } + pub fn obj(&self) -> Option<&Obj> { + match self { + V::Obj(o) => Some(o), + _ => None, + } + } + /// A string's or name's text. + pub fn text(&self) -> Option { + match self { + V::Str(s) => Some(text(s)), + V::Name(n) => Some(n.clone()), + _ => None, + } + } +} + +/// Text from the data's bytes: UTF-8, else Windows-1252 (Latin-1 for its gaps). +pub(super) fn text(bytes: &[u8]) -> String { + match std::str::from_utf8(bytes) { + Ok(s) => s.to_string(), + Err(_) => bytes.iter().map(|b| cp1252(*b)).collect(), + } +} + +fn cp1252(b: u8) -> char { + const HIGH: [char; 32] = [ + '€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', '—', + '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ', + ]; + match b { + 0x80..=0x9f => HIGH.get(usize::from(b - 0x80)).copied().unwrap_or('?'), + _ => char::from(b), + } +} + +/// A dictionary. +#[derive(Clone, Debug, Default, PartialEq)] +pub(super) struct Obj { + pub ty: String, + /// Entries in order: the key (`None` in an array) and the values before it. + pub entries: Vec<(Option, Vec)>, + /// Values closed without a key. + pub content: Vec, +} + +impl Obj { + /// The values of entry `key` (the first one). + pub fn get(&self, key: &str) -> Option<&[V]> { + self.entries.iter().find(|(k, _)| k.as_deref() == Some(key)).map(|(_, v)| v.as_slice()) + } + /// The numbers of entry `key`. + pub fn nums(&self, key: &str) -> Vec { + self.get(key).unwrap_or_default().iter().filter_map(V::num).collect() + } + /// The first text value of entry `key`. + pub fn text(&self, key: &str) -> Option { + self.get(key)?.iter().find(|v| matches!(v, V::Str(_))).and_then(V::text) + } + /// The dictionary value of entry `key`. + pub fn obj(&self, key: &str) -> Option<&Obj> { + self.get(key)?.iter().find_map(V::obj) + } + /// The dictionaries an array holds, in order. + pub fn items(&self) -> impl Iterator { + self.entries.iter().flat_map(|(_, v)| v.iter()).chain(self.content.iter()).filter_map(V::obj) + } + /// The first text of the content. + pub fn content_text(&self) -> Option { + self.content.iter().find_map(V::text) + } +} + +/// Add the values on `stack` above `base` to `o` as one entry (`,`): the last is the key, except +/// in an array. +pub(super) fn add_entry(o: &mut Obj, stack: &mut Vec, base: usize) { + let mut vals: Vec = stack.drain(base.min(stack.len())..).collect(); + if o.ty == "Array" { + o.entries.push((None, vals)); + return; + } + let key = match vals.last() { + Some(V::Str(_) | V::Name(_)) => vals.pop().and_then(|k| k.text()), + _ => None, + }; + o.entries.push((key, vals)); +} + +/// Close `o` (`;`): a last entry ending with a name is an entry, other values are its content. +pub(super) fn close(o: &mut Obj, stack: &mut Vec, base: usize) { + if stack.len() <= base { + return; + } + if o.ty != "Array" && matches!(stack.last(), Some(V::Name(_))) { + add_entry(o, stack, base); + } else { + let vals: Vec = stack.drain(base.min(stack.len())..).collect(); + o.content.extend(vals); + } +} + +/// An object name from its XML id (`AI10_ArtUID`): `_` stands for a space and `_xHH_` for a +/// character; ids made unique end with `_` and 20 or more digits. +pub(super) fn xml_name(id: &str) -> String { + let id = strip_unique(id); + let mut out = String::with_capacity(id.len()); + let mut rest = id; + while let Some(c) = rest.chars().next() { + if c == '_' { + if let Some((ch, n)) = escaped(rest) { + out.push(ch); + rest = rest.get(n..).unwrap_or_default(); + continue; + } + out.push(' '); + } else { + out.push(c); + } + rest = rest.get(c.len_utf8()..).unwrap_or_default(); + } + out +} + +/// `_xHHHH_` at the start of `s`: the character and the escape's length. +fn escaped(s: &str) -> Option<(char, usize)> { + let body = s.strip_prefix("_x")?; + let end = body.find('_')?; + let hex = body.get(..end)?; + if hex.is_empty() || hex.len() > 6 { + return None; + } + let c = char::from_u32(u32::from_str_radix(hex, 16).ok()?)?; + Some((c, end + 3)) +} + +/// `id` without the `_000…_` suffix that makes it unique. +fn strip_unique(id: &str) -> &str { + let Some(body) = id.strip_suffix('_') else { return id }; + let Some(at) = body.rfind('_') else { return id }; + let digits = body.get(at + 1..).unwrap_or_default(); + if digits.len() >= 20 && digits.bytes().all(|b| b.is_ascii_digit()) { body.get(..at).unwrap_or(id) } else { id } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_from_xml_ids() { + assert_eq!(xml_name("Layer_1"), "Layer 1"); + assert_eq!(xml_name("My_Rect__x23_2"), "My Rect #2"); + assert_eq!(xml_name("_x31_abc__x28_paren_x29_"), "1abc (paren)"); + assert_eq!(xml_name("Ünï_日本"), "Ünï 日本"); + assert_eq!(xml_name("S_00000092429200007574234970000005974767392385616558_"), "S"); + assert_eq!(xml_name("a_x_"), "a x "); + assert_eq!(xml_name("_xD800_"), " xD800 "); + } + + #[test] + fn entries_and_content() { + let mut o = Obj { ty: "XMLUID".into(), ..Obj::default() }; + let mut st = vec![V::Num(9.0), V::Str(b"Main".to_vec())]; + close(&mut o, &mut st, 1); + assert_eq!(o.content_text().as_deref(), Some("Main")); + assert_eq!(st, vec![V::Num(9.0)]); + let mut d = Obj { ty: "Dictionary".into(), ..Obj::default() }; + let mut st = vec![V::Num(1.0), V::Num(2.0), V::Name("RealPoint".into()), V::Str(b"P".to_vec())]; + add_entry(&mut d, &mut st, 0); + let mut st = vec![V::Name("Def".into())]; + close(&mut d, &mut st, 0); + assert_eq!(d.nums("P"), vec![1.0, 2.0]); + assert_eq!(d.get("Def"), Some(&[][..])); + let mut a = Obj { ty: "Array".into(), ..Obj::default() }; + let mut st = vec![V::Obj(Rc::new(d.clone()))]; + add_entry(&mut a, &mut st, 0); + assert_eq!(a.items().count(), 1); + } + + #[test] + fn latin_text() { + assert_eq!(text(b"caf\xe9 \x80"), "café €"); + assert_eq!(text("ü".as_bytes()), "ü"); + } +} diff --git a/crates/eps/src/import/ai/paint.rs b/crates/eps/src/import/ai/paint.rs new file mode 100644 index 000000000..2358b65f8 --- /dev/null +++ b/crates/eps/src/import/ai/paint.rs @@ -0,0 +1,277 @@ +//! Colours and gradients of the editing data. +//! +//! - Colours: `g`/`G` grey (1 is white), `k`/`K` CMYK, `x`/`X` a named CMYK colour at a tint +//! (`c m y k (name) tint`; tint 0 is the full colour), `Xa`/`XA` RGB (`c m y k r g b`, the CMYK +//! values its equivalent), `Xx`/`XX` a named colour of either model +//! (`c m y k [r g b] (name) tint type`). Lower case paints fills, upper case strokes. +//! - Gradient definitions: `(name) type stops Bd`, its stops as `colour style [opacity 6] +//! midpoint ramp Bs` (style 0 grey, 1 CMYK, 2 RGB with its CMYK, 3 and 4 named colours), `BD`. +//! - A gradient on an object: `[flag] Bb` (1: the stroke), `flag (name) x y angle length a b c d +//! tx ty Bg`, the gradient matrix `a b c d tx ty Bm`, the focal point `x y angle length Bh`, +//! `flag BB`. + +use std::collections::HashMap; + +use kurbo::{Affine, Point, Vec2}; +use vectorcraft_color::gradient::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop}; +use vectorcraft_color::{Color, Paint}; + +use super::obj::V; + +/// Largest number of stops read in one gradient. +const MAX_STOPS: usize = 256; + +/// A colour read with the swatch it names (spot and global colours). +#[derive(Clone, Debug, PartialEq)] +pub(super) struct Named { + pub name: String, + /// The full colour. + pub color: Color, + pub tint: f32, +} + +/// The paint of a colour operator's operands (`op`: its name, lower case or not), with the named +/// colour it uses. `None` when the operands don't fit. +pub(super) fn color_op(op: &str, vals: &[V]) -> Option<(Paint, Option)> { + let nums: Vec = vals.iter().filter_map(V::num).map(|v| v as f32).collect(); + let unit = |v: f32| v.clamp(0.0, 1.0); + let last = |n: usize| nums.get(nums.len().checked_sub(n)?..); + match op.to_ascii_lowercase().as_str() { + "g" => Some((Paint::solid(Color::gray(1.0 - unit(*nums.last()?))), None)), + "k" => { + let [c, m, y, k] = last(4)? else { return None }; + Some((Paint::solid(Color::cmyk(unit(*c), unit(*m), unit(*y), unit(*k))), None)) + } + "xa" => { + let [r, g, b] = last(3)? else { return None }; + Some((Paint::solid(Color::rgb(unit(*r), unit(*g), unit(*b))), None)) + } + "x" => named(vals, Some(false)), + "xx" => named(vals, None), + _ => None, + } +} + +/// `c m y k (name) tint x` or `c m y k [r g b] (name) tint type Xx` (type 1: RGB); `rgb` forces +/// the model. +fn named(vals: &[V], rgb: Option) -> Option<(Paint, Option)> { + let at = vals.iter().rposition(|v| matches!(v, V::Str(_)))?; + let name = vals.get(at)?.text()?; + let before: Vec = vals.get(..at)?.iter().filter_map(V::num).map(|v| v as f32).collect(); + let after: Vec = vals.get(at + 1..)?.iter().filter_map(V::num).map(|v| v as f32).collect(); + let unit = |v: f32| v.clamp(0.0, 1.0); + let tint = 1.0 - unit(*after.first()?); + let n = before.len(); + let color = if rgb.unwrap_or(after.get(1).is_some_and(|t| *t == 1.0)) { + let [r, g, b] = before.get(n.checked_sub(3)?..)? else { return None }; + Color::rgb(unit(*r), unit(*g), unit(*b)) + } else { + // The CMYK values come first (an RGB equivalent may follow them). + let from = if n >= 7 { n - 7 } else { n.checked_sub(4)? }; + let [c, m, y, k] = before.get(from..from + 4)? else { return None }; + Color::cmyk(unit(*c), unit(*m), unit(*y), unit(*k)) + }; + let paint = Paint::Solid { color: color.tinted(tint), swatch: Some(name.clone()), tint }; + Some((paint, Some(Named { name, color, tint }))) +} + +/// The colour of a gradient stop's colour values and style. +fn stop_color(style: f64, comps: &[V]) -> Option<(Color, Option)> { + let nums: Vec = comps.iter().filter_map(V::num).map(|v| v as f32).collect(); + let unit = |v: f32| v.clamp(0.0, 1.0); + let tail = |n: usize| nums.get(nums.len().checked_sub(n)?..); + Some(match style as i64 { + 0 => (Color::gray(1.0 - unit(*nums.last()?)), None), + 1 => { + let [c, m, y, k] = tail(4)? else { return None }; + (Color::cmyk(unit(*c), unit(*m), unit(*y), unit(*k)), None) + } + 2 => { + let [r, g, b] = tail(3)? else { return None }; + (Color::rgb(unit(*r), unit(*g), unit(*b)), None) + } + _ => { + let (paint, named) = named(comps, Some(style as i64 == 4))?; + (paint.color()?, named) + } + }) +} + +/// A gradient definition being read. +#[derive(Debug)] +pub(super) struct GradientDef { + pub name: String, + pub gradient: Gradient, +} + +impl GradientDef { + /// `(name) type stops Bd`. + pub fn begin(vals: &[V]) -> Option { + let name = vals.iter().find_map(|v| if let V::Str(_) = v { v.text() } else { None })?; + let nums: Vec = vals.iter().filter_map(V::num).collect(); + let kind = if nums.first().copied() == Some(1.0) { GradientKind::Radial } else { GradientKind::Linear }; + Some(Self { name, gradient: Gradient { kind, stops: vec![] } }) + } + + /// A stop (`Bs`). + pub fn stop(&mut self, vals: &[V], spots: &mut Vec) { + if self.gradient.stops.len() >= MAX_STOPS { + return; + } + let n = vals.len(); + let num = |i: usize| vals.get(i).and_then(V::num); + let (Some(ramp), Some(mid)) = (n.checked_sub(1).and_then(num), n.checked_sub(2).and_then(num)) else { return }; + // Newer stops have an opacity and the marker 6 before the midpoint. + let newer = n >= 5 && n.checked_sub(3).and_then(num) == Some(6.0); + let (style_at, opacity) = if newer { (n.checked_sub(5), n.checked_sub(4).and_then(num).unwrap_or(1.0)) } else { (n.checked_sub(3), 1.0) }; + let (Some(style_at), Some(style)) = (style_at, style_at.and_then(num)) else { return }; + let Some((color, named)) = stop_color(style, vals.get(..style_at).unwrap_or_default()) else { return }; + let mut stop = GradientStop::new((ramp / 100.0).clamp(0.0, 1.0) as f32, color); + stop.opacity = opacity.clamp(0.0, 1.0) as f32; + stop.midpoint = (mid / 100.0).clamp(0.13, 0.87) as f32; + if let Some(n) = named { + stop.set_color(n.color.tinted(n.tint), Some((n.name.clone(), n.tint))); + spots.push(n); + } + self.gradient.stops.push(stop); + } + + /// The finished gradient (`BD`): stops in order of their ramp points. + pub fn finish(mut self) -> Option<(String, Gradient)> { + if self.gradient.stops.is_empty() { + return None; + } + self.gradient.stops.sort_by(|a, b| a.offset.total_cmp(&b.offset)); + Some((self.name, self.gradient)) + } +} + +/// A gradient instance on an object, between `Bb` and `BB`. +#[derive(Clone, Debug, Default)] +pub(super) struct Instance { + pub stroke: bool, + pub name: Option, + origin: Point, + angle: f64, + length: f64, + /// `Bg`'s own matrix. + bg: Affine, + /// The gradient matrix (`Bm`), when there is one. + bm: Option, + /// The focal point's offset from the origin (`Bh`). + hilight: Vec2, +} + +fn affine(n: &[f64]) -> Option { + let [a, b, c, d, e, f] = n else { return None }; + let m = Affine::new([*a, *b, *c, *d, *e, *f]); + (m.as_coeffs().iter().all(|v| v.is_finite())).then_some(m) +} + +impl Instance { + pub fn begin(vals: &[V]) -> Self { + let stroke = vals.iter().filter_map(V::num).next_back() == Some(1.0); + Self { stroke, length: 1.0, ..Self::default() } + } + + /// `flag (name) x y angle length a b c d tx ty Bg`. + pub fn bg(&mut self, vals: &[V]) { + let Some(at) = vals.iter().rposition(|v| matches!(v, V::Str(_))) else { return }; + self.name = vals.get(at).and_then(V::text); + let n: Vec = vals.get(at + 1..).unwrap_or_default().iter().filter_map(V::num).collect(); + if let [x, y, angle, length, m @ ..] = n.as_slice() { + self.origin = Point::new(*x, *y); + self.angle = *angle; + self.length = *length; + self.bg = affine(m).unwrap_or(Affine::IDENTITY); + } + } + + /// `a b c d tx ty Bm`. + pub fn bm(&mut self, vals: &[V]) { + let n: Vec = vals.iter().filter_map(V::num).collect(); + self.bm = n.get(n.len().saturating_sub(6)..).and_then(affine).filter(|m| m.determinant().abs() > 1e-12); + } + + /// `x y angle length Bh`. + pub fn bh(&mut self, vals: &[V]) { + let n: Vec = vals.iter().filter_map(V::num).collect(); + if let [x, y, ..] = n.as_slice() { + self.hilight = Vec2::new(*x, *y); + } + } + + /// The paint, through `to_doc` (art space → the document), when the gradient is defined. + pub fn paint(&self, defs: &HashMap, to_doc: Affine) -> Option { + let name = self.name.as_ref()?; + let gradient = defs.get(name)?.clone(); + let m = to_doc * self.bm.unwrap_or(Affine::IDENTITY) * self.bg; + let a = self.angle.to_radians(); + let dir = Vec2::new(a.cos(), a.sin()) * self.length; + let start = m * self.origin; + let end = m * (self.origin + dir); + if !(start.is_finite() && end.is_finite()) || start.distance(end) < 1e-9 { + return None; + } + // Across the vector, for an elliptical radial gradient. + let across = m * (self.origin + Vec2::new(-dir.y, dir.x)); + let aspect = (across.distance(start) / start.distance(end)).clamp(0.01, 100.0); + let focal = (self.hilight.hypot() > 1e-9).then(|| m * (self.origin + self.hilight)).filter(|p| p.is_finite()); + let radial = gradient.kind == GradientKind::Radial; + let geom = GradientGeom { start, end, aspect: if radial { aspect } else { 1.0 }, focal: if radial { focal } else { None } }; + let angle = -(end - start).atan2().to_degrees(); + Some(Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), angle, swatch: Some(name.clone()), ..GradientPaint::new(gradient) }))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn nums(v: &[f64]) -> Vec { + v.iter().map(|n| V::Num(*n)).collect() + } + + #[test] + fn colours() { + let (p, _) = color_op("g", &nums(&[0.7])).unwrap(); + assert!(matches!(p.color(), Some(Color::Gray { k }) if (k - 0.3).abs() < 1e-6)); + let (p, _) = color_op("XA", &nums(&[0.0, 0.99, 1.0, 0.0, 1.0, 0.0, 0.0])).unwrap(); + assert_eq!(p.color(), Some(Color::rgb(1.0, 0.0, 0.0))); + let mut v = nums(&[0.0, 0.5, 1.0, 0.0]); + v.push(V::Str(b"MySpot".to_vec())); + v.push(V::Num(0.4)); + let (p, named) = color_op("x", &v).unwrap(); + let named = named.unwrap(); + assert_eq!(named.name, "MySpot"); + assert!((named.tint - 0.6).abs() < 1e-6); + assert!(matches!(p, Paint::Solid { swatch: Some(_), .. })); + assert!(color_op("k", &nums(&[1.0])).is_none()); + } + + #[test] + fn gradient_definition_and_instance() { + let mut d = GradientDef::begin(&[V::Str(b"G".to_vec()), V::Num(1.0), V::Num(2.0)]).unwrap(); + let mut spots = vec![]; + d.stop(&nums(&[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.5, 6.0, 30.0, 100.0]), &mut spots); + d.stop(&nums(&[0.81, 0.0, 1.0, 6.0, 50.0, 0.0]), &mut spots); + let (name, g) = d.finish().unwrap(); + assert_eq!(name, "G"); + assert_eq!(g.kind, GradientKind::Radial); + assert_eq!(g.stops.len(), 2); + assert_eq!(g.stops[1].opacity, 0.5); + assert!(matches!(g.stops[0].color, Color::Gray { k } if (k - 0.19).abs() < 1e-6)); + let defs = HashMap::from([(name, g)]); + let mut i = Instance::begin(&[]); + let mut bg = vec![V::Num(1.0), V::Str(b"G".to_vec())]; + bg.extend(nums(&[0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0])); + i.bg(&bg); + i.bm(&nums(&[50.0, 0.0, 0.0, -50.0, 100.0, 100.0])); + let Some(Paint::Gradient(gp)) = i.paint(&defs, Affine::IDENTITY) else { panic!() }; + let geom = gp.geom.unwrap(); + assert_eq!(geom.start, Point::new(100.0, 100.0)); + assert_eq!(geom.end, Point::new(150.0, 100.0)); + assert!((geom.aspect - 1.0).abs() < 1e-9); + assert!(i.paint(&HashMap::new(), Affine::IDENTITY).is_none()); + } +} diff --git a/crates/eps/src/import/ai/read.rs b/crates/eps/src/import/ai/read.rs new file mode 100644 index 000000000..f71a1cfc8 --- /dev/null +++ b/crates/eps/src/import/ai/read.rs @@ -0,0 +1,1237 @@ +//! The reader: runs the editing data's operators into a document. +//! +//! - Art space has y up; the first artboard's top left corner is the document's origin. +//! - Layers: `%AI5_BeginLayer`, `visible preview unlocked printing dimmed … colorIndex r g b … +//! dimPercent … Lb`, `(name) Ln`, its art, `LB`. +//! - Groups `u` … `U`, compound paths `*u` … `*U`, clipping groups `q` … `Q` (the path painted +//! with `W` clips); a group's transparency and name follow its end. +//! - Paths: `m`, `l`/`L`, `c`/`C`, `v`/`V`, `y`/`Y`, painted by `N n F f S s B b` (lower case +//! closes), `(op) *` for a guide. Stroke `w J j M d`, fill rule `XR`, overprint `O`/`R`. +//! - State: hidden `Xw`, locked `A`, transparency `mode opacity isolate knockout shape Xy`. +//! - Names: an `/ArtDictionary` after an object, its `AI10_ArtUID` the name's XML id. +//! - Artboards: the `ArtboardArray` of the `/Document` dictionary, else `%AI3_Cropmarks`. +//! - Images: `%AI5_BeginRaster`, the colour space `XN`, `[matrix] bounds w h bits type alpha …` +//! and the samples after `XI`. +//! - Art with an appearance the format can't write as plain art (several fills or strokes, +//! effects, brushes) comes first as that look, drawn (a group), then the object itself on `%_` +//! lines and `1 (style) XW`: the drawn look is kept, named after the object. +//! +//! Sections the reader doesn't need (patterns, brushes, symbols, styles, swatches, filters) are +//! skipped; type, symbols, pattern fills, placed files, legacy type and operators it doesn't know +//! are errors. + +use std::collections::{BTreeSet, HashMap, HashSet}; +use std::rc::Rc; +use std::sync::Arc; + +use kurbo::{Affine, BezPath, PathEl, Point, Rect}; +use vectorcraft_color::gradient::Gradient; +use vectorcraft_color::swatch::Swatch; +use vectorcraft_color::{BlendMode, Paint}; +use vectorcraft_doc::{ + Appearance, AppearanceItem, Artboard, ColorMode, Dash, Document, FillLayer, ImageBlob, ImageObject, Knockout, LayerColor, LineCap, LineJoin, + Node, NodeId, NodeKind, StrokeLayer, +}; +use vectorcraft_geom::{FillRule, PathData}; + +use super::lex::{Lexer, Tok}; +use super::obj::{self, Obj, V}; +use super::paint::{self, GradientDef, Instance, Named}; +use crate::import::Imported; + +/// Deepest nesting of layers, groups and dictionaries. +const MAX_DEPTH: usize = 256; +/// Most operands on the stack. +const MAX_STACK: usize = 1 << 20; +/// Most objects read. +const MAX_NODES: usize = 4_000_000; +/// Most points in one path. +const MAX_POINTS: usize = 4_000_000; +/// Largest image read (pixels). +const MAX_PIXELS: u64 = 1 << 28; +/// Largest side of the document and its artboards (points). +const MAX_SIDE: f64 = 1e6; + +/// Read the editing data `data` into a document. +pub fn read(data: &[u8]) -> Result { + let mut r = Reader::new(data); + r.run()?; + r.finish() +} + +#[derive(Clone, Debug)] +struct GState { + fill: Paint, + stroke: Paint, + width: f64, + cap: LineCap, + join: LineJoin, + miter: f64, + dash: Option, + rule: FillRule, + opacity: f32, + blend: BlendMode, + isolate: bool, + knockout: Knockout, + knockout_shape: bool, + overprint_fill: bool, + overprint_stroke: bool, + locked: bool, + hidden: bool, +} + +impl Default for GState { + fn default() -> Self { + let black = Paint::solid(vectorcraft_color::Color::BLACK); + Self { + fill: black.clone(), + stroke: black, + width: 1.0, + cap: LineCap::Butt, + join: LineJoin::Miter, + miter: 10.0, + dash: None, + rule: FillRule::NonZero, + opacity: 1.0, + blend: BlendMode::Normal, + isolate: false, + knockout: Knockout::Off, + knockout_shape: false, + overprint_fill: false, + overprint_stroke: false, + locked: false, + hidden: false, + } + } +} + +/// The blend modes by their number in `Xy`. +const BLENDS: [BlendMode; 16] = [ + BlendMode::Normal, + BlendMode::Multiply, + BlendMode::Screen, + BlendMode::Overlay, + BlendMode::SoftLight, + BlendMode::HardLight, + BlendMode::ColorDodge, + BlendMode::ColorBurn, + BlendMode::Darken, + BlendMode::Lighten, + BlendMode::Difference, + BlendMode::Exclusion, + BlendMode::Hue, + BlendMode::Saturation, + BlendMode::Color, + BlendMode::Luminosity, +]; + +#[derive(Debug, Default)] +struct LayerAttrs { + name: String, + visible: bool, + preview: bool, + locked: bool, + printable: bool, + dim: Option, + color: Option, +} + +#[derive(Debug)] +enum Kind { + Layer(Box), + Group, + Compound, + Clip, + Obj(Box), +} + +#[derive(Debug)] +struct Frame { + kind: Kind, + children: Vec, + /// Opened on a `%_` line. + hidden: bool, + /// [`Reader::after_object`] when it opened. + after: bool, + /// The state when it opened (a container's lock and visibility; a dictionary's state to go + /// back to). + gs: GState, + /// The operand stack's height when it opened. + base: usize, + /// The clipping paths' indexes among the children. + clips: Vec, +} + +/// An image's operands, from `XN` and before `XI`. +#[derive(Debug, Default)] +struct Raster { + space: Option, +} + +struct Reader<'a> { + lex: Lexer<'a>, + stack: Vec, + gs: GState, + frames: Vec