From e63606826a8a7e3f4f343edbdb6ca8aa56813c18 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Thu, 8 Oct 2026 22:19:45 +0700 Subject: [PATCH] Fix #508: PDF/.ai import keeps type strokes, wrapped lines and layer order - Type left unfinished when a layer's marked content ends, and a clip that closes in a later layer, stay in their own layer (no stray "Page N" layer, no art moved to the next layer). - Top-level layers stack as the pages mark them, so a layer whose art starts on a later page keeps its place; layers marked with no art come in empty. - A stroke written as each glyph's outline stroked, right after (or before) the type, becomes the type object's stroke above (or below) its characters: the type stays one editable object. - Spaces read from gaps wider than a space get their own tracking where even tracking would put the glyphs after them more than a tenth of an em off (barcode digit groups); lines are measured to their last visible glyph, so a wrapped line's trailing space no longer spreads into the tracking. - Lines wrapped in a frame (each wrapped line ending in a space, one left edge, font, size and leading, with an optional paragraph spacing) come back as one area type object, left aligned or justified, only where laying it out breaks the lines exactly where the file does. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 6 +- crates/engine/src/cmd/fileio/mod.rs | 2 +- crates/engine/tests/import_fuzz.rs | 4 + crates/pdf/src/import.rs | 79 +++++++- crates/pdf/src/import_lines.rs | 254 ++++++++++++++++++++++++ crates/pdf/src/import_text.rs | 128 ++++++++++-- crates/pdf/src/lib.rs | 3 + crates/pdf/src/tests_import_fidelity.rs | 8 +- crates/pdf/src/tests_import_lines.rs | 203 +++++++++++++++++++ crates/pdf/src/tests_live_text.rs | 6 +- docs/mcp.md | 9 +- 11 files changed, 672 insertions(+), 30 deletions(-) create mode 100644 crates/pdf/src/import_lines.rs create mode 100644 crates/pdf/src/tests_import_lines.rs diff --git a/ROADMAP.md b/ROADMAP.md index df38638f..d6434cac 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ VectorCraft is a clean-room, open-source, pure-Rust reimplementation of the Adob This file tracks **how far we are and what's left**. Time estimates are wall-clock hours of continuous Claude Opus 5.5 agent work (including builds and the CI gate), given both for **one agent** and for **4–6 parallel agents** on disjoint crates. They are counted from the remaining work (see [Parity estimate](#parity-estimate)), calibrated against measured throughput, and updated as work lands. -_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS files from Illustrator 2026, Inkscape/cairo, matplotlib, CorelDRAW, Affinity, LibreOffice and Ghostscript opening as vectors, 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); see [Honest assessment](#honest-assessment-2026-10-05))._ +_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS files from Illustrator 2026, Inkscape/cairo, matplotlib, CorelDRAW, Affinity, LibreOffice and Ghostscript opening as vectors, 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); see [Honest assessment](#honest-assessment-2026-10-05))._ ## Where we are @@ -154,7 +154,7 @@ grade by behaviour against `plan/illustrator/`, not by whether a menu item exist - **Fonts:** a font is found by any of its names (family and style in every language of its name table, legacy family/style pairs, PostScript name), so documents from other apps open with the font they name; Find Font and `text.fonts` say for each font whether it was found exactly, by another name, substituted or missing, and how many glyphs are missing. PDF and `.ai` import brings Japanese text back as live type: CID fonts without a ToUnicode map read through the standard CMaps, vertical columns (WMode 1) as vertical type, letter spacing as tracking; a glyph is live type only when the installed font (in the closest style of its family) draws the same glyph, and text in a font that isn't installed comes in as live type shown in the fallback font until the font is installed (exports that draw it — PDF, EPS, EMF/WMF, raster, SVG outlines or embedded fonts — warn that they wrote the fallback font; `textAs: outlines` keeps the PDF's embedded glyphs). - **Files (M4):** - `.vectorcraft` (lossless JSON, compressed, atomic saves, save down to v1), Save As with a format chooser, Save a Copy, Revert, templates, Data Recovery after a crash (browser tabs hold their copies with Web Locks too, so a background tab's copies are never taken for a crash's, and a tab repairs copies another removed when it resumes: #367), background save and export. The desktop app writes its warnings and errors (graphics adapter, a lost graphics device, panics the guard recovered from) to standard error and to `logs/vectorcraft.log` next to the preferences, the two previous runs kept and each file capped at 16 MiB, so a launch without a terminal still leaves something to attach to a bug report (#465). - - SVG/SVGZ in and out with SVG Options, Preserve Editing, symbols, filters, rich text and physical units (SVG type under a transform or `viewBox` scale opens at the size it draws at, the scale moved into its size, leading, baseline shift and strokes, a stretch, skew or rotation kept as its transform: #396; a top-level group with a clip path, such as a clipped Inkscape layer, opens as a layer with a clipping mask, its name kept: #420); PDF and PDF-compatible `.ai` in and out (layers, with hidden ones kept hidden with their art and nested ones as sublayers, on pages of up to 64 million operators, art whose layer can't be told going to a hidden, non-printing layer rather than showing or printing: #419; strokes kept live, a fill and a stroke of one outline as one object; 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). + - SVG/SVGZ in and out with SVG Options, Preserve Editing, symbols, filters, rich text and physical units (SVG type under a transform or `viewBox` scale opens at the size it draws at, the scale moved into its size, leading, baseline shift and strokes, a stretch, skew or rotation kept as its transform: #396; a top-level group with a clip path, such as a clipped Inkscape layer, opens as a layer with a clipping mask, its name kept: #420); PDF and PDF-compatible `.ai` in and out (layers, with hidden ones kept hidden with their art and nested ones as sublayers, on pages of up to 64 million operators, art whose layer can't be told going to a hidden, non-printing layer rather than showing or printing: #419; strokes kept live, a fill and a stroke of one outline as one object; a type object's stroke written as each glyph's outline stroked back on the type, so it stays one editable object; lines wrapped in a frame (each wrapped line ending in a space, one left edge, leading and font) back as one area type object, left aligned or justified with its paragraph spacing, where laying it out breaks the lines where the file does; glyphs set apart by gaps wider than a space (a barcode's digit groups) keeping their places; layers stacked as the pages mark them even when one first paints on a later page, art ending a layer's marked content or a clip closing in a later layer kept in its own layer, and layers with no art kept empty: #508; masks, editable text with type set along a curve coming back as type on a path, each artboard's art on its own page only, an `.ai`'s white page under its layers left out rather than coming in as art, spot colours, presets, security, marks and bleed, ICC output intents, raster effects, subset fonts, PDF/X, PDF layers, overprint, page thumbnails, fast web view, PDF 1.3). CMYK images keep their ink amounts from PDF import (and from placed CMYK TIFFs) to PDF export: a CMYK JPEG passes through unchanged, others stay CMYK (DeviceCMYK, or ICC-based CMYK in PDF/X-3/X-4) unless the Output settings convert them; the canvas shows them through the working CMYK profile (#320). - EPS and DXF in and out, EMF/WMF in and out; PNG (with PNG-8), JPEG, WebP, GIF, TIFF, BMP, Targa and layered PSD export. EPS import runs Illustrator's AGM/CoolType prologs (resource categories, `resourceforall`, `clipsave`/`cliprestore`, subarrays and substrings that share storage, the standard dictionaries in `systemdict`, `dictstack`, `internaldict`, executable filters, `cshow`, metadata skipped with `flushfile`), so Illustrator 30 EPS files open as vectors. It also runs what other generators write (Discord feedback, #466, #469): Illustrator 2026's procsets (`gcheck`, `rootfont`, `currentcolorrendering`: every Illustrator 2026 EPS fell back to its preview before), cairo/Inkscape (`status`/`flushfile`, images with a mask, `ImageType 3`, reusable streams), matplotlib's Type 3 fonts, Illustrator 8 EPS (stock art's usual format: executable strings such as its gradients' `ed`, `load` by integer keys, #474), Ghostscript's `//name` immediate names, `pdfmark`, `token`, `execform` and user paths. 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. When a file's PostScript still can't be read, it opens as its TIFF preview, its Windows metafile preview (as vectors) or its EPSI preview, and the warning (in the status bar, `document.open`/`app.open`/MCP `open_file` replies and `vectorcraft-cli info`) names the error, the operator that raised it and the procedures it ran in. Legacy PostScript `.ai` files (Illustrator 3–8, written with the prolog that defines their operators) keep their groups and nested groups (`u` … `U`), between and inside clipping groups; EPS and EMF/WMF import nest clips (and groups) at most 128 deep, so a hostile file can't exhaust the stack. DXF TEXT justified Fit or Aligned spans its two points (stretched, or scaled as a whole). - Place and the Links panel, Package, File Info, Print with print presets, PostScript output and print tiling, slices and Save for Web, Export for Screens and Asset Export, CSS Properties, PNG/PDF/SVG/text clipboard flavours; files copied in a file manager paste as art (SVG, PDF, EMF/WMF, bitmaps) on every desktop platform, and Linux/Wayland reads the compositor's clipboard (#398); on Linux the Paste menu checks the clipboard off the UI thread, so an X11 owner that never answers can't freeze the window (#510); screenshots and other copied pictures paste as embedded images at the view centre everywhere, the web included (#457). - **Performance:** 20k shapes + 1k texts render in 27 ms per full-retina frame (7.8 ms zoomed), 7× faster than the first version. The UI thread never blocks. The web build is 7.1 MB gzipped. @@ -238,7 +238,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). Art off the page that a PDF does draw opens on the pasteboard. +- **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. - **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/mod.rs b/crates/engine/src/cmd/fileio/mod.rs index 031113e6..e2ae1d06 100644 --- a/crates/engine/src/cmd/fileio/mod.rs +++ b/crates/engine/src/cmd/fileio/mod.rs @@ -78,7 +78,7 @@ pub fn specs() -> Vec { "Open Document", [], None, - "{path} or {name, dataBase64}, PDF/.ai: pages?: \"2-3, 5\" (1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page) | false (one layer per page, without the art that is off; Save then asks for a name), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), DXF: dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its pixel size), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again). Linked images are read from their files (looked for at their path, then relative to the document): missing ones show their saved preview (links.relink), modified ones are read again only with Preferences › Update Links: Automatically (else links.update). An SVG's files (relative links from the SVG's folder) stay linked, SVG files become art, missing ones a placeholder in their box (a warning and missingLinks)", + "{path} or {name, dataBase64}, PDF/.ai: pages?: \"2-3, 5\" (1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, lines wrapped in a frame as area type where they lay out the same, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page) | false (one layer per page, without the art that is off; Save then asks for a name), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), DXF: dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its pixel size), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again). Linked images are read from their files (looked for at their path, then relative to the document): missing ones show their saved preview (links.relink), modified ones are read again only with Preferences › Update Links: Automatically (else links.update). An SVG's files (relative links from the SVG's folder) stay linked, SVG files become art, missing ones a placeholder in their box (a warning and missingLinks)", always, load::open ), diff --git a/crates/engine/tests/import_fuzz.rs b/crates/engine/tests/import_fuzz.rs index 4223955e..ec93b755 100644 --- a/crates/engine/tests/import_fuzz.rs +++ b/crates/engine/tests/import_fuzz.rs @@ -488,6 +488,10 @@ fn arb_rich_op() -> impl Strategy { "/Sh0 sh", "/Sh1 sh", "BT /F1 12 Tf 10 10 Td (Hi there) Tj [(a) -900 (b)] TJ ET", + // Lines wrapped in a frame, digits set apart by gaps, glyph outlines stroked (#508). + "BT /F1 10 Tf 10 80 Td (Wrapped line ) Tj 0 -12 Td (and its next ) Tj 0 -12 Td (end.) Tj ET", + "BT /F1 10 Tf 10 40 Td (3) Tj 20 0 Td (5331) Tj 40 0 Td (0106) Tj ET", + "0.5 w 10 10 m 14 18 l 18 10 l h S", "7 Tr", "q 10 10 50 50 re W n", "Q", diff --git a/crates/pdf/src/import.rs b/crates/pdf/src/import.rs index 88833c88..f9f6db07 100644 --- a/crates/pdf/src/import.rs +++ b/crates/pdf/src/import.rs @@ -23,7 +23,7 @@ use crate::import_color::{Colors, Native}; use crate::import_mask::{MaskSpec, contains, is_rectangle, luminance, mask_spec, white_cover}; use crate::import_scan::{MAX_NESTING, Ocgs, Scan, all_on, hides_forms, scan_page, tag_key}; use crate::import_shading::{clipped, extend_clip, fold_stop_opacity, mesh_shading, shading_gradient}; -use crate::import_text::{Families, Look, Placement, TextLine, Upright}; +use crate::import_text::{Families, LineFacts, Look, Placement, TextLine, Upright}; use crate::{CropTo, ImportOptions, ImportReport, PdfError, TextAs}; /// The name of the paths text imports as. @@ -76,6 +76,33 @@ fn place_group(g: usize, ocgs: &Ocgs, slots: &mut Vec, nesting: &mut Nesti slots.push(Slot::Group(child)); } +/// The top-level group that group `g` is listed under (`g` itself when it isn't a sublayer). +fn top_group(g: usize, ocgs: &Ocgs) -> usize { + let mut top = g; + for _ in 0..=MAX_NESTING { + match ocgs.list.get(top).and_then(|o| o.parent) { + Some(parent) => top = parent, + None => break, + } + } + top +} + +/// Merge the top-level groups a page marks (`page`, in the order it first marks each, its empty +/// ones too) into the stacking order of the pages before (`order`, bottom first; `known`: its +/// groups). A group new to it goes right below the first group the page marks after it that it +/// has: a layer whose art starts on a later page keeps its place among the others (#508). +fn merge_order(order: &mut Vec, known: &mut HashSet, page: &[usize]) { + let mut above = None; + for &g in page.iter().rev() { + if known.insert(g) { + let at = above.and_then(|a| order.iter().position(|&x| x == a)).unwrap_or(order.len()); + order.insert(at, g); + } + above = Some(g); + } +} + /// The layer of group `g`, in `color`: its art and sublayers. `None` for a group that isn't read. fn group_layer( b: &mut Builder<'_>, @@ -166,6 +193,8 @@ pub fn import_with_report(bytes: &[u8], opts: &ImportOptions) -> Result = vec![]; let mut group_art: HashMap>> = HashMap::new(); let mut nesting = Nesting::new(); + // The top-level groups in the order the pages mark them, bottom first. + let (mut order, mut known) = (vec![], HashSet::new()); for (i, &number) in picked.iter().enumerate() { let Some(page) = pages.get(number) else { continue }; // The chosen box sits at (x, 0); the page draws round it. @@ -175,6 +204,11 @@ pub fn import_with_report(bytes: &[u8], opts: &ImportOptions) -> Result = scan.tags.iter().filter_map(|(_, g)| g.map(|g| top_group(g, &ocgs))).filter(|g| seen.insert(*g)).collect(); + merge_order(&mut order, &mut known, &marked); + } b.begin_page(scan); interpret_page(page, &mut ctx, &mut b); let mut parts = b.end_page(); @@ -239,8 +273,25 @@ pub fn import_with_report(bytes: &[u8], opts: &ImportOptions) -> Result = order.iter().enumerate().map(|(i, &g)| (g, i)).collect(); + let at: Vec = slots.iter().enumerate().filter(|(_, s)| matches!(s, Slot::Group(_))).map(|(i, _)| i).collect(); + let mut stacked: Vec = slots.iter().filter_map(|s| if let Slot::Group(g) = s { Some(*g) } else { None }).collect(); + stacked.sort_by_key(|g| rank.get(g).copied().unwrap_or(usize::MAX)); + for (i, g) in at.into_iter().zip(stacked) { + if let Some(s) = slots.get_mut(i) { + *s = Slot::Group(g); + } + } for (n, slot) in slots.into_iter().enumerate() { - let layer = match slot { + let mut layer = match slot { Slot::Page(l) => *l, // Sublayers take their top-level layer's colour. Slot::Group(g) => match group_layer(&mut b, g, &ocgs, &nesting, &mut group_art, LayerColor::Preset((n % 27) as u8)) { @@ -248,6 +299,9 @@ pub fn import_with_report(bytes: &[u8], opts: &ImportOptions) -> Result continue, }, }; + if let Some(art) = layer.children_mut() { + crate::import_lines::rebuild(art, &b.lines); + } doc.layers.push(Arc::new(layer)); } for (k, blob) in b.images.drain() { @@ -567,6 +621,8 @@ struct Builder<'p> { nested: u32, /// The last filled path: its node and its outline (a stroke of the same outline joins it). last_fill: Option<(NodeId, BezPath)>, + /// What each line of type made tells beyond its text object ([`crate::import_lines`]). + lines: HashMap, } fn blend(b: hayro_interpret::BlendMode) -> BlendMode { @@ -701,6 +757,7 @@ impl<'p> Builder<'p> { pending: false, nested: 0, last_fill: None, + lines: HashMap::new(), } } @@ -786,10 +843,11 @@ impl<'p> Builder<'p> { let current = self.current_group(); let Some(f) = self.stack.last_mut() else { return }; f.children.push(n); + // A frame's art goes where its first art was drawn (a clip can end in a later group). if root { self.root_groups.push(group.or(current)); } else if f.group.is_none() { - f.group = group; + f.group = group.or(current); } } @@ -955,9 +1013,10 @@ impl<'p> Builder<'p> { } fn flush_text(&mut self) { - let Some((t, opacity)) = self.text.take().and_then(TextLine::finish) else { return }; + let Some((t, opacity, facts)) = self.text.take().and_then(TextLine::finish) else { return }; let mut n = Node::new(self.id(), NodeKind::Text(Box::new(t))); n.opacity = opacity; + self.lines.insert(n.id, facts); self.push_node(n); } @@ -1227,6 +1286,14 @@ impl<'p> Builder<'p> { place(&mut line); self.text = Some(line); } + // Its ink: a stroke the file draws as outlines of the glyphs is told by it. + let mut ink = o.outline(); + if let Some(line) = &mut self.text + && !ink.elements().is_empty() + { + ink.apply_affine(m); + line.ink.push(ink.bounding_box()); + } true } } @@ -1570,8 +1637,10 @@ impl<'a> Device<'a> for Builder<'_> { if self.nested > 0 { return; } - if self.marked.pop().flatten().is_some() { + // The runs gathered within a layer's sequence are its art: finish them before it ends. + if self.marked.last().copied().flatten().is_some() { self.flush(); } + self.marked.pop(); } } diff --git a/crates/pdf/src/import_lines.rs b/crates/pdf/src/import_lines.rs new file mode 100644 index 00000000..2009355c --- /dev/null +++ b/crates/pdf/src/import_lines.rs @@ -0,0 +1,254 @@ +//! What a file's lines of type were, beyond what each line says on its own (#508). An editor +//! writes what PDF text can't say as more art next to the text, and the type it wrapped in a +//! frame as lines: +//! - a stroke added to a type object (above or below its characters) comes as the outline of each +//! glyph stroked, one path per glyph, right after the type (or right before it): it is the +//! type's stroke again, so the type stays one object; +//! - area type comes as a line of point type per line, each wrapped line ending in a space. Lines +//! in a row sharing a left edge, font, size and leading (and paragraph spacing) are one area +//! type object again, aligned left or justified, where laying its text out in the frame breaks +//! it where the file does: else, as where its font isn't installed, they stay lines. + +use std::collections::HashMap; +use std::sync::Arc; + +use kurbo::{Point, Rect, Shape}; +use vectorcraft_color::BlendMode; +use vectorcraft_doc::{AppearanceItem, Justify, Node, NodeId, NodeKind, TextKind, TextObject, TextRun}; +use vectorcraft_geom::shapes; + +use crate::import_text::LineFacts; + +/// Rebuild the type among `nodes` (and in their groups) from what its lines tell (`lines`). +pub(crate) fn rebuild(nodes: &mut Vec>, lines: &HashMap) { + if lines.is_empty() { + return; + } + for n in nodes.iter_mut() { + if n.children().is_some_and(|c| !c.is_empty()) + && let Some(children) = Arc::make_mut(n).children_mut() + { + rebuild(children, lines); + } + } + join_strokes(nodes, lines); + join_paragraphs(nodes, lines); +} + +/// The stroke paths `nodes` draw when they are the outlines of the glyphs whose ink boxes are +/// `ink`, stroked one by one alike (no fill, mask or blending, fully opaque). +fn outline_stroke(nodes: &[Arc], ink: &[Rect]) -> Option { + let first = nodes.first()?; + let [stroke @ AppearanceItem::Stroke(_)] = first.appearance.items.as_slice() else { return None }; + let on_glyph = |n: &Node, r: &Rect| { + let tol = 0.01 * (r.width() + r.height()) + 0.01; + let NodeKind::Path { path, clipping: false, guide: false, live: None, .. } = &n.kind else { return false }; + let b = path.to_bezpath().bounding_box(); + n.appearance == first.appearance + && n.mask.is_none() + && n.opacity >= 0.999 + && n.blend == BlendMode::Normal + && [(b.x0, r.x0), (b.y0, r.y0), (b.x1, r.x1), (b.y1, r.y1)].iter().all(|(a, b)| (a - b).abs() <= tol) + }; + (nodes.len() == ink.len() && nodes.iter().zip(ink).all(|(n, r)| on_glyph(n, r))).then(|| stroke.clone()) +} + +/// Each type object among `nodes` whose glyphs' outlines are stroked by the paths right after it +/// (or right before it) takes that stroke above (or below) its characters, and the paths go. +fn join_strokes(nodes: &mut Vec>, lines: &HashMap) { + let mut i = 0; + while i < nodes.len() { + let ink = nodes + .get(i) + .filter(|n| matches!(n.kind, NodeKind::Text(_)) && n.appearance.items.is_empty()) + .and_then(|n| lines.get(&n.id)) + .map_or(&[][..], |f| f.ink.as_slice()); + let k = ink.len(); + if k > 0 { + if let Some(stroke) = nodes.get(i + 1..i + 1 + k).and_then(|after| outline_stroke(after, ink)) { + nodes.drain(i + 1..i + 1 + k); + if let Some(t) = nodes.get_mut(i) { + Arc::make_mut(t).appearance.items.push(stroke); + } + } else if let Some(stroke) = i.checked_sub(k).and_then(|from| nodes.get(from..i)).and_then(|before| outline_stroke(before, ink)) { + nodes.drain(i - k..i); + i -= k; + if let Some(t) = nodes.get_mut(i) { + let ap = &mut Arc::make_mut(t).appearance; + ap.items.insert(0, stroke); + ap.set_contents_at(1); + } + } + } + i += 1; + } +} + +/// `n` as a line that can be part of area type: horizontal point type, with what its line told. +fn line<'a>(n: &'a Node, lines: &'a HashMap) -> Option<(&'a TextObject, &'a LineFacts)> { + let NodeKind::Text(t) = &n.kind else { return None }; + let point = matches!(t.kind, TextKind::Point) && !t.vertical && n.mask.is_none() && !t.runs.is_empty(); + point.then_some(())?; + Some((t, lines.get(&n.id)?)) +} + +/// Lines of type in a row that may be one area type object. +struct Block { + /// The step from a wrapped line's baseline to the next. + leading: f64, + /// The extra step after a line ending a paragraph. + space: f64, + /// Each line's baseline below the first (text space). + ys: Vec, +} + +/// The lines from `nodes`' first on that share its left edge, angle, font and size, its look +/// (opacity, blending, appearance) and steps down by one leading after a wrapped line and by +/// another (the leading and paragraph spacing) after a line ending a paragraph: how many, and +/// the block they make unless none of them was wrapped (0 and `None`: the first isn't a line). +fn block(nodes: &[Arc], lines: &HashMap) -> (usize, Option) { + let Some((head, (t0, f0))) = nodes.first().and_then(|n| Some((n, line(n, lines)?))) else { return (0, None) }; + let st = t0.first_style(); + let size = st.size; + let inv = t0.xf.inverse(); + let lin = |t: &TextObject| { + let [a, b, c, d, ..] = t.xf.as_coeffs(); + [a, b, c, d] + }; + let (mut ys, mut wraps) = (vec![0.0], f0.wraps); + let (mut leading, mut para): (Option, Option) = (None, None); + for n in nodes.iter().skip(1) { + let Some((t, f)) = line(n, lines) else { break }; + let s = t.first_style(); + let alike = n.opacity == head.opacity + && n.blend == head.blend + && n.appearance == head.appearance + && s.font_family == st.font_family + && s.font_style == st.font_style + && (s.size - size).abs() <= size * 0.01 + && t.para == t0.para + && lin(t).iter().zip(lin(t0)).all(|(a, b)| (a - b).abs() < 1e-6); + let at = inv * (t.xf * Point::ZERO); + let step = at.y - ys.last().copied().unwrap_or_default(); + if !alike || at.x.abs() > size * 0.02 || !(step > size * 0.5 && step < size * 4.0) { + break; + } + let slot = if wraps { &mut leading } else { &mut para }; + match *slot { + Some(s) if (s - step).abs() > size * 0.01 => break, + Some(_) => {} + None => *slot = Some(step), + } + ys.push(at.y); + wraps = f.wraps; + } + let count = ys.len(); + let Some(leading) = leading else { return (count, None) }; + let space = para.map_or(0.0, |p| p - leading); + (count, (space > -size * 0.01).then_some(Block { leading, space: space.max(0.0), ys })) +} + +/// Lines `nodes` of block `b` as one area type object, aligned left or justified, when laying it +/// out breaks its lines where the file does. +fn area_type(nodes: &[Arc], lines: &HashMap, b: &Block) -> Option { + let texts: Vec<(&TextObject, &LineFacts)> = nodes.iter().map(|n| line(n, lines)).collect::>()?; + let (first, _) = texts.first()?; + let size = first.first_style().size; + // Each line's tracking (a line with spaces tracked apart was set by hand). + let trackings: Vec = texts + .iter() + .map(|(t, _)| { + let mut all = t.runs.iter().map(|r| r.style.tracking); + let one = all.next()?; + all.all(|x| x == one).then_some(one) + }) + .collect::>()?; + let median = |mut v: Vec| { + v.sort_by(f64::total_cmp); + v.get(v.len() / 2).copied() + }; + let wrapped: Vec = texts.iter().filter(|(_, f)| f.wraps).map(|(_, f)| f.length).collect(); + let longest = |v: &[f64]| v.iter().copied().fold(0.0, f64::max); + let mut tries = vec![]; + // Wrapped lines all as long: justified (a paragraph's last line aligned left), tracked as the + // lines that end paragraphs are (the others are spaced out). + let full = longest(&wrapped); + if wrapped.len() >= 2 && wrapped.iter().all(|l| (full - l).abs() <= size * 0.02) { + let ends: Vec = texts.iter().zip(&trackings).filter(|((_, f), _)| !f.wraps).map(|(_, t)| *t).collect(); + let tracking = median(ends).or_else(|| trackings.iter().copied().reduce(f64::min))?; + tries.push((Justify::JustifyLeft, full, tracking)); + } + let all: Vec = texts.iter().map(|(_, f)| f.length).collect(); + tries.push((Justify::Left, longest(&all) + size * 0.02, median(trackings)?)); + tries.into_iter().find_map(|(justify, width, tracking)| laid_out(&texts, b, justify, width, tracking)) +} + +/// The area type of lines `texts` (block `b`): `width` wide, aligned `justify`, its characters +/// tracked `tracking`; `None` when it doesn't lay out as the lines did. +fn laid_out(texts: &[(&TextObject, &LineFacts)], b: &Block, justify: Justify, width: f64, tracking: f64) -> Option { + let (first, _) = texts.first()?; + let size = first.first_style().size; + // Leading as set, unless it is Auto's (120% of the size). + let explicit = + (b.leading - size * 1.2).abs() > size * 1e-3 || texts.iter().any(|(t, _)| t.runs.iter().any(|r| (r.style.size - size).abs() > 1e-9)); + let mut runs: Vec = vec![]; + for (k, (t, f)) in texts.iter().enumerate() { + for r in &t.runs { + let mut style = r.style.clone(); + style.tracking = tracking; + style.leading = explicit.then_some((b.leading * 1000.0).round() / 1000.0); + match runs.last_mut() { + Some(last) if last.style == style => last.text.push_str(&r.text), + _ => runs.push(TextRun { text: r.text.clone(), style }), + } + } + // A wrapped line's space, or the end of a paragraph. + if k + 1 < texts.len() + && let Some(last) = runs.last_mut() + { + last.text.push(if f.wraps { ' ' } else { '\n' }); + } + } + let mut t = (*first).clone(); + t.runs = runs; + t.para.justify = justify; + t.para.space_before = b.space; + let last = b.ys.last().copied()?; + let db = vectorcraft_text::FontDb::global(); + // The frame's top puts the first baseline where the file has it. + t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, width, last + b.leading.max(size) + size)) }; + let top = -vectorcraft_text::layout(db, &t).lines.first()?.baseline; + t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, top, width, last + b.leading.max(size))) }; + let laid = vectorcraft_text::layout(db, &t); + let plain = t.plain_text(); + let same = !laid.overflow + && laid.lines.len() == texts.len() + && laid.lines.iter().zip(texts).zip(&b.ys).all(|((l, (line, f)), y)| { + let text = plain.get(l.start..l.end).unwrap_or_default(); + text.trim() == line.plain_text().trim() + && (l.baseline - y).abs() <= size * 0.05 + && (justify != Justify::JustifyLeft || !f.wraps || (l.x1 - f.length).abs() <= size * 0.05) + }); + t.cached_bounds = Some(laid.bounds); + same.then_some(t) +} + +/// Lines of point type in a row among `nodes` that were wrapped in a frame become one area type +/// object again (see [`area_type`]). +fn join_paragraphs(nodes: &mut Vec>, lines: &HashMap) { + let mut i = 0; + while i < nodes.len() { + let (count, b) = nodes.get(i..).map_or((0, None), |rest| block(rest, lines)); + let area = b.filter(|_| count > 1).and_then(|b| area_type(nodes.get(i..i + count)?, lines, &b)); + if let Some(area) = area + && let Some(head) = nodes.get_mut(i) + { + Arc::make_mut(head).kind = NodeKind::Text(Box::new(area)); + nodes.drain(i + 1..i + count); + i += 1; + } else { + // Lines that don't make area type: none of them starts it either. + i += count.max(1); + } + } +} diff --git a/crates/pdf/src/import_text.rs b/crates/pdf/src/import_text.rs index 3c9b440e..de2a2e47 100644 --- a/crates/pdf/src/import_text.rs +++ b/crates/pdf/src/import_text.rs @@ -9,9 +9,10 @@ use std::collections::HashMap; -use kurbo::{Affine, BezPath, Point, Vec2}; +use kurbo::{Affine, BezPath, Point, Rect, Vec2}; use vectorcraft_color::Paint; use vectorcraft_doc::{CharStyle, ParaDirection, TextKind, TextObject, TextRun}; +use vectorcraft_text::TextLayout; /// One glyph's placement: its baseline origin, advance direction, size and horizontal scale. #[derive(Clone, Copy, Debug)] @@ -103,6 +104,8 @@ pub(crate) struct TextLine { next: Point, /// The last glyph's origin. last: Point, + /// Where the last glyph that isn't a space ends. + visible: Point, /// The last glyph's baseline direction. last_dir: Vec2, /// Each glyph's baseline origin, then where the last one ends. @@ -112,6 +115,24 @@ pub(crate) struct TextLine { upright: Option, /// The glyphs make a column (vertical type), each below the last. column: bool, + /// Each glyph's ink box as drawn (document space), in drawing order; glyphs without ink are + /// left out. + pub ink: Vec, + /// The spaces read from gaps between glyphs: their byte offset in the text, and the gap from + /// the end of the glyph before to the start of the next (points). + gaps: Vec<(usize, f64)>, +} + +/// What a finished line of type tells beyond its text object (see [`crate::import_lines`]). +#[derive(Clone, Debug, Default)] +pub(crate) struct LineFacts { + /// [`TextLine::ink`]. + pub ink: Vec, + /// Horizontal point type: it ended in a space (as a line an editor wrapped in a frame does). + pub wraps: bool, + /// Horizontal point type: how long the file set it, from its first glyph's origin to the end + /// of its last glyph that isn't a space (points, along the baseline). + pub length: f64, } /// The most a glyph on a curve turns from the one before it (about 20°). @@ -119,7 +140,20 @@ const CURVE_TURN_COS: f64 = 0.94; impl TextLine { pub fn new(at: Placement, opacity: f32) -> Self { - Self { at, opacity, next: at.origin, last: at.origin, last_dir: at.dir, baseline: vec![], runs: vec![], upright: None, column: false } + Self { + at, + opacity, + next: at.origin, + last: at.origin, + visible: at.origin, + last_dir: at.dir, + baseline: vec![], + runs: vec![], + upright: None, + column: false, + ink: vec![], + gaps: vec![], + } } /// Add glyph `text` set vertically (`upright`) if it continues this column (or makes this @@ -171,6 +205,7 @@ impl TextLine { if self.column || self.upright.is_some() { return false; } + let bytes: usize = self.runs.iter().map(|(_, t)| t.len()).sum(); if let Some((last, run)) = self.runs.last_mut() { let size = self.at.size.max(at.size); let gap = (at.origin - self.next).dot(self.last_dir); @@ -192,6 +227,9 @@ impl TextLine { // A gap wider than a fifth of an em reads as a space. if gap > size * 0.2 && !run.ends_with(' ') && !text.starts_with(' ') { run.push(' '); + if on_line { + self.gaps.push((bytes, gap)); + } } if last.takes(look) { run.push_str(text); @@ -202,6 +240,9 @@ impl TextLine { self.runs.push((look.clone(), text.to_string())); } self.next = at.origin + at.dir * advance; + if !text.trim().is_empty() { + self.visible = self.next; + } self.last = at.origin; self.last_dir = at.dir; self.baseline.push(at.origin); @@ -247,10 +288,12 @@ impl TextLine { true } - /// The point type object and its opacity. - pub fn finish(mut self) -> Option<(TextObject, f32)> { + /// The point type object, its opacity and what else the line tells. + pub fn finish(mut self) -> Option<(TextObject, f32, LineFacts)> { + let mut facts = LineFacts { ink: std::mem::take(&mut self.ink), ..LineFacts::default() }; // Turned along a curve: type on a path through the glyphs. let path = if self.curved() { self.baseline_path() } else { None }; + facts.wraps = path.is_none() && self.upright.is_none() && self.runs.last().is_some_and(|(_, t)| t.ends_with(' ')); if let Some((_, last)) = self.runs.last_mut() { last.truncate(last.trim_end().len()); } @@ -272,7 +315,7 @@ impl TextLine { t.kind = TextKind::OnPath { path: vectorcraft_geom::PathData::from_bezpath(&path), start: 0.0, end: None }; t.xf = Affine::IDENTITY; t.cached_bounds = Some(vectorcraft_text::layout(db, &t).bounds); - return Some((t, self.opacity)); + return Some((t, self.opacity, facts)); } let (start, along) = match self.upright { // Vertical point type (a column, or a single upright glyph) is anchored at the top @@ -286,26 +329,87 @@ impl TextLine { }; // Tracking that makes the line as long as in the file: the PDF placed each glyph, the // layout sets them by their advances. Spread over the gaps between characters (tracking - // follows each character, the last one's past the end of the line). - let length = (self.next - start).dot(along); + // follows each character, the last one's past the end of the line). A horizontal line is + // measured to its last glyph that isn't a space: the spaces after it were dropped. + let length = (if t.vertical { self.next } else { self.visible } - start).dot(along); + if !t.vertical { + facts.length = length; + } let laid = vectorcraft_text::layout(db, &t); let natural: f64 = laid.glyphs.iter().map(|g| g.advance).sum(); let chars: usize = t.runs.iter().map(|r| r.text.chars().count()).sum(); let size = self.at.size; let mut bounds = laid.bounds; if chars > 1 && length.is_finite() && (length - natural).abs() > size * 0.01 { - let tracking = ((length - natural) / (chars - 1) as f64 / size * 1000.0).clamp(-1000.0, 1000.0).round(); - for r in &mut t.runs { - r.style.tracking = tracking; - } + // The spaces read from gaps keep their own place only in text left in the file's order. + let total: usize = t.runs.iter().map(|r| r.text.len()).sum(); + let gaps: Vec<(usize, f64)> = + if t.para.direction.is_none() { self.gaps.into_iter().filter(|(b, _)| *b < total).collect() } else { vec![] }; + let (tracking, spaces) = spacing(&laid, &gaps, length - natural, chars, size); + t.runs = tracked(std::mem::take(&mut t.runs), tracking, &spaces); // Tracked: laid out again for its bounds. bounds = vectorcraft_text::layout(db, &t).bounds; } t.cached_bounds = Some(bounds); - Some((t, self.opacity)) + Some((t, self.opacity, facts)) } } +/// The tracking (thousandths of an em) that sets a line of `chars` characters, laid out untracked +/// as `laid`, `extra` points longer, as the file set it: the same after every character, except +/// the spaces read from gaps (`gaps`: byte offset, gap in points) where that would put the glyphs +/// after them more than a tenth of an em off (a barcode's digits, set in groups apart). Those take +/// their own tracking, given with their byte offsets. +fn spacing(laid: &TextLayout, gaps: &[(usize, f64)], extra: f64, chars: usize, size: f64) -> (f64, Vec<(usize, f64)>) { + let pairs = chars.saturating_sub(1).max(1) as f64; + // The space and the character before it take `need` (the gap less the space) between them; + // tracking gives them twice its share. + let space = |b: usize| laid.glyphs.iter().find(|g| g.byte == b).map(|g| g.advance); + let needs: Vec<(usize, f64)> = gaps.iter().filter_map(|&(b, gap)| Some((b, gap - space(b)?))).collect(); + let mut own: Vec<(usize, f64)> = vec![]; + let mut tracking = extra / pairs; + // Each space taken apart changes the others' share: until no more are. + loop { + let more: Vec<(usize, f64)> = + needs.iter().filter(|(b, need)| !own.iter().any(|(o, _)| o == b) && (need - 2.0 * tracking).abs() > size * 0.1).copied().collect(); + if more.is_empty() { + break; + } + own.extend(more); + let rest = pairs - 2.0 * own.len() as f64; + tracking = if rest >= 1.0 { (extra - own.iter().map(|(_, need)| need).sum::()) / rest } else { 0.0 }; + } + own.sort_by_key(|(b, _)| *b); + let em = |pts: f64| (pts / size * 1000.0).clamp(-1000.0, 1000.0).round(); + (em(tracking), own.into_iter().map(|(b, need)| (b, em(need - tracking))).collect()) +} + +/// `runs` with tracking `tracking`, and each space at a byte offset of `spaces` (in order) a run +/// of its own with the tracking given with it. +fn tracked(runs: Vec, tracking: f64, spaces: &[(usize, f64)]) -> Vec { + let mut out = Vec::with_capacity(runs.len() + 2 * spaces.len()); + let mut off = 0; + for r in runs { + let style = CharStyle { tracking, ..r.style }; + let end = off + r.text.len(); + let mut from = 0; + for &(b, own) in spaces.iter().filter(|(b, _)| (off..end).contains(b)) { + let at = b - off; + let (Some(before), Some(space)) = (r.text.get(from..at), r.text.get(at..at + 1)) else { continue }; + if !before.is_empty() { + out.push(TextRun { text: before.to_string(), style: style.clone() }); + } + out.push(TextRun { text: space.to_string(), style: CharStyle { tracking: own, ..style.clone() } }); + from = at + 1; + } + if let Some(rest) = r.text.get(from..).filter(|t| !t.is_empty()) { + out.push(TextRun { text: rest.to_string(), style }); + } + off = end; + } + out +} + fn round(v: f64) -> f64 { (v * 1000.0).round() / 1000.0 } diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 61ce4b35..2cbc4176 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -48,6 +48,7 @@ mod images; mod import; mod import_color; mod import_image; +mod import_lines; mod import_mask; mod import_scan; mod import_shading; @@ -231,6 +232,8 @@ mod tests_import_fidelity; #[cfg(test)] mod tests_import_layers; #[cfg(test)] +mod tests_import_lines; +#[cfg(test)] mod tests_import_options; #[cfg(test)] mod tests_layers; diff --git a/crates/pdf/src/tests_import_fidelity.rs b/crates/pdf/src/tests_import_fidelity.rs index 8c52ded8..b6098eed 100644 --- a/crates/pdf/src/tests_import_fidelity.rs +++ b/crates/pdf/src/tests_import_fidelity.rs @@ -242,11 +242,13 @@ fn text_becomes_point_type() { // The baseline origin, in y-down page space. let o = t[0].xf * kurbo::Point::ORIGIN; assert!((o.x - 10.0).abs() < 0.01 && (o.y - 70.0).abs() < 0.01, "{o:?}"); - // A gap of an em between words reads as a space; another font on the line is another run. + // A gap of an em between words reads as a space, tracked on its own to keep the gap's width + // (#508); another font on the line is another run. assert_eq!(text(&t[1]), "Next line in bold"); let runs: Vec<(&str, &str)> = t[1].runs.iter().map(|r| (r.text.as_str(), r.style.font_style.as_str())).collect(); - assert_eq!(runs, [("Next line", "Regular"), (" in bold", "Bold")]); - assert_eq!(t[1].runs[1].style.fill.color(), Some(Color::rgb(0.0, 0.0, 1.0))); + assert_eq!(runs, [("Next", "Regular"), (" ", "Regular"), ("line", "Regular"), (" in bold", "Bold")]); + assert!(t[1].runs[1].style.tracking > t[1].runs[0].style.tracking + 100.0, "{:?}", t[1].runs); + assert_eq!(t[1].runs[3].style.fill.color(), Some(Color::rgb(0.0, 0.0, 1.0))); assert!(!r.warnings.iter().any(|w| w.contains("outlines")), "{:?}", r.warnings); // As outlines: paths, no type. let r = open_with(&bytes, |o| o.text_as = TextAs::Outlines); diff --git a/crates/pdf/src/tests_import_lines.rs b/crates/pdf/src/tests_import_lines.rs new file mode 100644 index 00000000..6005e7f0 --- /dev/null +++ b/crates/pdf/src/tests_import_lines.rs @@ -0,0 +1,203 @@ +//! What a file's lines of type were (#508): the layer art ending a group's marked content belongs +//! to, layers ordered as the pages mark them, a stroke written as stroked glyph outlines, spaces +//! read from wide gaps, and lines wrapped in a frame rebuilt as area type. + +use kurbo::{BezPath, PathEl, Point, Shape}; +use serde_json::json; +use vectorcraft_color::{Color, Paint}; +use vectorcraft_doc::{AppearanceItem, CharStyle, Document, Justify, Node, NodeKind, TextKind, TextObject}; +use vectorcraft_geom::{Affine, Rect, shapes}; +use vectorcraft_testkit::pdf::{PdfPage, first_extra, pdf_with_catalog}; + +use crate::*; + +const HELVETICA: &str = "/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >>"; + +/// Groups "A" and "B" (A painted first) for a file of `pages` pages, and the catalog entries. +fn groups(pages: usize) -> ([String; 2], String, String) { + let (a, b) = (first_extra(pages), first_extra(pages) + 1); + let objs = ["<< /Type /OCG /Name (A) >>".to_string(), "<< /Type /OCG /Name (B) >>".to_string()]; + let catalog = format!("/OCProperties << /OCGs [{a} 0 R {b} 0 R] /D << /Order [{b} 0 R {a} 0 R] >> >> "); + let resources = format!("/Properties << /MC0 {a} 0 R /MC1 {b} 0 R >> {HELVETICA}"); + (objs, catalog, resources) +} + +/// A file of 100 × 100 pt pages drawing `contents`, with groups A (`/MC0`) and B (`/MC1`). +fn layered(contents: &[&str]) -> Document { + let (objs, catalog, resources) = groups(contents.len()); + let pages: Vec = contents.iter().map(|c| PdfPage { resources: resources.clone(), ..PdfPage::new(100.0, 100.0, c) }).collect(); + import(&pdf_with_catalog(&pages, &[&objs[0], &objs[1]], &catalog, None)).unwrap() +} + +/// The layers' names, bottom first, and the kinds of their art. +fn layers(d: &Document) -> Vec<(String, Vec<&'static str>)> { + let kind = |n: &std::sync::Arc| match n.kind { + NodeKind::Text(_) => "text", + NodeKind::Group { .. } => "group", + _ => "path", + }; + d.layers.iter().map(|l| (l.name.clone().unwrap_or_default(), l.children().map_or(vec![], |c| c.iter().map(kind).collect()))).collect() +} + +fn texts(d: &Document) -> Vec { + let mut v = vec![]; + d.walk(|n| { + if let NodeKind::Text(t) = &n.kind { + v.push((**t).clone()); + } + }); + v +} + +fn named(layers: &[(&str, &[&'static str])]) -> Vec<(String, Vec<&'static str>)> { + layers.iter().map(|(n, k)| (n.to_string(), k.to_vec())).collect() +} + +#[test] +fn type_ending_a_layers_marked_content_stays_in_its_layer() { + // The line is still being gathered when the group's sequence ends. + let d = layered(&["/OC /MC0 BDC 0 0 1 rg 0 0 10 10 re f BT /F1 12 Tf 10 50 Td (Lead) Tj ET EMC /OC /MC1 BDC EMC"]); + assert_eq!(layers(&d), named(&[("A", &["path", "text"]), ("B", &[])])); +} + +#[test] +fn a_clip_ending_in_a_later_group_stays_with_its_art() { + // The clip opens in A, its art is drawn in A, and it closes after B's (empty) sequence. + let d = layered(&["/OC /MC0 BDC q 0 0 50 50 re W n 1 0 0 rg 40 40 20 20 re f EMC /OC /MC1 BDC EMC Q"]); + assert_eq!(layers(&d), named(&[("A", &["group"]), ("B", &[])])); +} + +#[test] +fn layers_stack_as_the_pages_mark_them_when_one_first_paints_on_a_later_page() { + // A, the bottom layer, is empty on page 1 and has art on page 2 only. + let d = layered(&["/OC /MC0 BDC EMC /OC /MC1 BDC 1 0 0 rg 0 0 10 10 re f EMC", "/OC /MC0 BDC 0 0 1 rg 0 0 10 10 re f EMC /OC /MC1 BDC EMC"]); + assert_eq!(layers(&d), named(&[("A", &["path"]), ("B", &["path"])])); + // A group marked with no art on any page is an empty layer in its place. + let d = layered(&["/OC /MC0 BDC EMC /OC /MC1 BDC 1 0 0 rg 0 0 10 10 re f EMC"]); + assert_eq!(layers(&d), named(&[("A", &[]), ("B", &["path"])])); +} + +/// A 100 × 100 pt page drawing `content` in Helvetica. +fn page(content: &str) -> Vec { + vectorcraft_testkit::pdf::pdf(&[PdfPage { resources: HELVETICA.into(), ..PdfPage::new(100.0, 100.0, content) }], None) +} + +/// The outline of glyph `c` drawn at 24 pt from (x, 50), in PDF operators (y up), stroked. +fn outline(c: char, x: f64) -> String { + let opts = ImportOptions { text_as: TextAs::Outlines, ..Default::default() }; + let d = import_with_report(&page(&format!("BT /F1 24 Tf {x} 50 Td ({c}) Tj ET")), &opts).unwrap().document; + let mut p: Option = None; + d.walk(|n| { + if let NodeKind::Path { path, .. } = &n.kind { + p = Some(path.to_bezpath()); + } + }); + let pt = |q: Point| format!("{:.3} {:.3}", q.x, 100.0 - q.y); + let ops: String = p + .unwrap() + .elements() + .iter() + .map(|e| match e { + PathEl::MoveTo(a) => format!("{} m ", pt(*a)), + PathEl::LineTo(a) => format!("{} l ", pt(*a)), + PathEl::QuadTo(a, b) => format!("{} {} {} c ", pt(*a), pt(*b), pt(*b)), + PathEl::CurveTo(a, b, c) => format!("{} {} {} c ", pt(*a), pt(*b), pt(*c)), + PathEl::ClosePath => "h ".into(), + }) + .collect(); + ops + "S " +} + +#[test] +fn a_stroke_written_as_stroked_glyph_outlines_is_the_types_stroke() { + // "Hi": the H is 722 units wide in Helvetica. + let strokes = format!("0 0 1 RG 0.5 w {}{}", outline('H', 10.0), outline('i', 10.0 + 0.722 * 24.0)); + let text = "BT 1 0 0 rg /F1 24 Tf 10 50 Td (Hi) Tj ET "; + for (content, below) in [(format!("{text}{strokes}"), false), (format!("{strokes}{text}"), true)] { + let d = import(&page(&content)).unwrap(); + let art = d.layers[0].children().unwrap(); + assert_eq!(art.len(), 1, "one object: {art:?}"); + let NodeKind::Text(t) = &art[0].kind else { panic!("{:?}", art[0].kind) }; + assert_eq!(t.plain_text(), "Hi"); + assert_eq!(t.first_style().fill.color(), Some(Color::rgb(1.0, 0.0, 0.0))); + let ap = &art[0].appearance; + let [AppearanceItem::Stroke(st)] = ap.items.as_slice() else { panic!("{ap:?}") }; + assert_eq!(st.paint.color(), Some(Color::rgb(0.0, 0.0, 1.0))); + assert!((st.width - 0.5).abs() < 1e-6); + assert_eq!(ap.contents_at(), usize::from(below), "a stroke drawn first paints below the characters"); + } + // A stroke that isn't on the glyphs stays a path. + let d = import(&page(&format!("{text}0 0 1 RG 0.5 w {}", outline('H', 40.0)))).unwrap(); + assert_eq!(d.layers[0].children().unwrap().len(), 2); +} + +/// A 300 × 200 pt page with `lines` of (x, y from the top, size, text) in Source Sans 3. +fn source_sans(lines: &[(f32, f32, f32, &str)]) -> Vec { + crate::tests_live_text::text_pdf(lines, false) +} + +#[test] +fn spaces_read_from_wide_gaps_keep_the_glyphs_after_them_in_place() { + // A barcode's digits in three groups, set apart by more than a space. + let (x1, x2) = (29.0, 69.0); + let bytes = source_sans(&[(20.0, 50.0, 10.0, "3"), (x1, 50.0, 10.0, "533121"), (x2, 50.0, 10.0, "010629")]); + let t = texts(&import(&bytes).unwrap()); + assert_eq!(t.len(), 1); + assert_eq!(t[0].plain_text(), "3 533121 010629"); + let laid = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t[0]); + let at = |byte: usize| (t[0].xf * laid.glyphs.iter().find(|g| g.byte == byte).unwrap().origin).x; + assert!((at(2) - f64::from(x1)).abs() < 0.05, "the second group starts where the file has it: {} vs {x1}", at(2)); + assert!((at(9) - f64::from(x2)).abs() < 0.05, "the third group starts where the file has it: {} vs {x2}", at(9)); +} + +/// A document of area type `text` (Source Sans 3, 10 pt) `width` wide at (20, 30). +fn area_doc(text: &str, justify: Justify, width: f64, space_before: f64) -> Document { + let style = CharStyle { size: 10.0, fill: Paint::solid(Color::BLACK), ..Default::default() }; + let mut t = TextObject::point(Point::ORIGIN, text, style); + t.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, width, 200.0)) }; + t.xf = Affine::translate((20.0, 30.0)); + t.para.justify = justify; + t.para.space_before = space_before; + let mut d = Document::new(300.0, 300.0); + let l = d.layers[0].id; + let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t))); + d.insert(Some(l), 0, n).unwrap(); + d +} + +/// `d` exported (real text) and imported again. +fn round_trip(d: &Document) -> Document { + let settings: PdfSettings = serde_json::from_value(json!({"compression": {"compressText": false}, "advanced": {"outlineText": false}})).unwrap(); + let bytes = export_with_report(d, &PdfOptions { settings, ..Default::default() }).unwrap().bytes; + import(&bytes).unwrap() +} + +const PROSE: &str = "Stop losing your sinkers on the cast: the clip holds the lead until a snag pulls it free, \ + so the fish swims on without the weight.\nIt works on every running rig and leaves the line clear."; + +#[test] +fn lines_wrapped_in_a_frame_come_back_as_area_type() { + for (justify, space) in [(Justify::Left, 0.0), (Justify::JustifyLeft, 4.0)] { + let d = round_trip(&area_doc(PROSE, justify, 120.0, space)); + let t = texts(&d); + assert_eq!(t.len(), 1, "{justify:?}: one object, not a line each: {:?}", t.iter().map(TextObject::plain_text).collect::>()); + assert_eq!(t[0].plain_text(), PROSE); + assert_eq!(t[0].para.justify, justify); + assert!((t[0].para.space_before - space).abs() < 0.01, "{}", t[0].para.space_before); + let TextKind::Area { frame } = &t[0].kind else { panic!("{:?}", t[0].kind) }; + let w = frame.to_bezpath().bounding_box().width(); + assert!(w <= 120.5 && w > 100.0, "{w}"); + let origin = t[0].xf * Point::ZERO; + assert!((origin.x - 20.0).abs() < 0.05, "{origin:?}"); + } +} + +#[test] +fn lines_that_were_not_wrapped_stay_point_type() { + // A list: each line its own, none ending in a space. + let bytes = source_sans(&[(20.0, 50.0, 10.0, "Hooks"), (20.0, 62.0, 10.0, "Swivels"), (20.0, 74.0, 10.0, "Leads")]); + assert_eq!(texts(&import(&bytes).unwrap()).len(), 3); + // Wrapped lines whose breaks a frame doesn't give again (a line far short of the others). + let bytes = source_sans(&[(20.0, 50.0, 10.0, "One two three four five six "), (20.0, 62.0, 10.0, "a "), (20.0, 74.0, 10.0, "seven")]); + assert_eq!(texts(&import(&bytes).unwrap()).len(), 3); +} diff --git a/crates/pdf/src/tests_live_text.rs b/crates/pdf/src/tests_live_text.rs index 32ebc975..cdbe985c 100644 --- a/crates/pdf/src/tests_live_text.rs +++ b/crates/pdf/src/tests_live_text.rs @@ -13,7 +13,7 @@ const SOURCE_SERIF: &[u8] = include_bytes!("../../../assets/fonts/SourceSerif4-R /// A 300 × 200 pt page with `lines` of (x, y from the top, size, text) in Source Sans 3, and the /// same rotated 90° when `rotated`. -fn text_pdf(lines: &[(f32, f32, f32, &str)], rotated: bool) -> Vec { +pub(crate) fn text_pdf(lines: &[(f32, f32, f32, &str)], rotated: bool) -> Vec { let mut pdf = krilla::Document::new(); let mut page = pdf.start_page_with(PageSettings::from_wh(300.0, 200.0).unwrap()); let mut s = page.surface(); @@ -269,7 +269,7 @@ fn a_single_upright_glyph_comes_in_as_vertical_type_and_takes_no_horizontal_glyp let mut line = TextLine::new(at(100.0, 100.0), 1.0); assert!(line.push_upright(&look, at(100.0, 100.0), 1.0, Upright { top: Point::new(110.0, 82.0) }, "§")); assert!(!line.push(&look, at(130.0, 100.0), 1.0, 10.0, "a"), "a horizontal glyph starts another line"); - let (t, _) = line.finish().unwrap(); + let (t, ..) = line.finish().unwrap(); assert!(t.vertical, "one upright glyph is vertical type"); assert_eq!(t.plain_text(), "§"); } @@ -294,7 +294,7 @@ fn letter_spaced_vertical_type_keeps_its_characters_together() { let y = 100.0 + 30.0 * i as f64; assert!(line.push_upright(&look, at(y), 1.0, Upright { top: Point::new(110.0, y - 18.0) }, c)); } - let (t, _) = line.finish().unwrap(); + let (t, ..) = line.finish().unwrap(); assert!(t.vertical); assert_eq!(t.plain_text(), "§§§", "letter spacing isn't a space"); } diff --git a/docs/mcp.md b/docs/mcp.md index 998c5a23..e681bcb1 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -402,11 +402,14 @@ their stop opacity and stop where the shading doesn't extend. Text becomes point the file's font (by name; fonts that aren't available are listed in `warnings` and show in the fallback font, and every export that draws that type — PDF, EPS, EMF/WMF, raster images, SVG with outlined or embedded fonts — says in its `warnings` that it wrote the fallback font) — `textAs: "outlines"` keeps the glyph outlines the file draws instead -(its embedded fonts, installed or not). Strokes stay live strokes (width, cap, join, miter limit, dash and paint), and +(its embedded fonts, installed or not). Lines wrapped in a frame (each wrapped line ending in a space, sharing a left +edge, font and leading) come back as one area type object, left aligned or justified, where laying it out breaks the +lines where the file does; a stroke written as each glyph's outline stroked is the type object's stroke; glyphs set +apart by gaps wider than a space keep their places. Strokes stay live strokes (width, cap, join, miter limit, dash and paint), and an object written as a fill and then a stroke of the same outline is one path with both. Optional content groups (the layers of PDF and PDF-compatible `.ai` files) become layers with their name, visibility (the default configuration's, -or a view state that is off), print state and lock, nested as sublayers the way the file's layer order nests them; art -that is off comes in as a hidden layer. Art outside them goes to a layer per page (except the opaque white page a `.ai` +or a view state that is off), print state and lock, nested as sublayers the way the file's layer order nests them, stacked as the pages mark them +(layers with no art come in empty); art that is off comes in as a hidden layer. Art outside them goes to a layer per page (except the opaque white page a `.ai` paints under its layers, which isn't art). `layers: false` gives one layer per page of only what shows: ```json