mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
* feat(ps): add craft-ps with an EPS reader (read feature) The reading half of craft-ps (#115): a clean-room PostScript interpreter that runs an EPS and returns what it paints as a one-page vector PDF of its bounding box, colours kept, so apps can draw and export EPS as a placed PDF. Moved from DesignCraft (storytold/designcraft, where it was written for placed EPS), with its app-specific names made neutral. Written from the PostScript Language Reference (3rd ed.), the EPS specification, the Type 1 Font Format and Adobe TN 5012/5176/5177. Covers the language, paths, clipping, strokepath, device/Separation/ DeviceN/Indexed/ICCBased/Lab colour, images with lazily decoded filters, shadings 1-7 and patterns, and Type 1/3/42/CFF/CID text through CMaps, horizontal and vertical. Bounded: each operator charges work proportional to its operands against the step limit as it goes, every byte read or decoded counts, copies count against the memory cap; defaults 50 M steps / 1 GiB, and 20 M / 256 MiB on wasm32. No panics (deny lints, fuzzed in debug builds). 34 tests, the Type 42 one on a TrueType font built in the test. No new external crates: pdf-writer, miniz_oxide, weezl, skrifa and kurbo are already locked at these versions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * clippy(ps): document stateful decoder complexity * fix(ps): bound huge arc angle normalization * fix(ps): preserve EffectCraft EPS text and limits parity * fix(ps): address interpreter review findings * fix(ps): preserve composite glyph paint order * fix(ps): address interpreter review findings * fix(ps): bound filter buffers and preserve file aliases * fix(ps): make predictor allocation fallible --------- Co-authored-by: markus.doebele <markus.doebele@top21.sh> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
4.5 KiB
Markdown
71 lines
4.5 KiB
Markdown
# craft-eps
|
|
|
|
Encapsulated PostScript files as a container: what a file is, where its PostScript and its
|
|
previews are, and what its header comments say, without running its program. It depends on
|
|
nothing.
|
|
|
|
| | |
|
|
|---|---|
|
|
| `sniff`, `is_eps` | EPS (binary header, or `%!PS-Adobe-… EPSF-…`), plain PostScript or PDF, and whether it is an Illustrator file: PostScript-based (`%AI…` comments, or Illustrator as `%%Creator`) or PDF-based (the `AIPrivateData` / `AIPDFPrivateData` keys near the start or end of the PDF, a hint only) |
|
|
| `sections` | The parts behind the binary header ("DOS EPS"): the PostScript, the TIFF preview, the Windows metafile preview. A header that points outside the file is an error |
|
|
| `postscript_section` | The PostScript program alone; a damaged preview does not prevent a reader from using a valid program section |
|
|
| `wrap` | PostScript and its previews behind a binary header, as `sections` reads them back |
|
|
| `Comments::read` | `%%BoundingBox` and `%%HiResBoundingBox` (the first in the header; `(atend)` taken from the trailer, outside embedded documents), `%%Pages`, `%%LanguageLevel`, `%%Creator`, `%%Title` |
|
|
| `Comments::read_with_header_limit` | The same comments with a caller-selected header scan limit capped at 256 KiB; `read` defaults to 64 KiB |
|
|
| `epsi` | The device-independent preview (`%%BeginPreview`, 1, 2, 4 or 8 bits) as an 8-bit grey bitmap |
|
|
| `ascii85::encode`, `ascii85::decode` | The ASCII85 data encoding; lines never start with `%`, decoding is bounded |
|
|
|
|
```rust
|
|
let parts = craft_eps::sections(bytes)?;
|
|
let comments = craft_eps::Comments::read(parts.postscript);
|
|
if let Some([x0, y0, x1, y1]) = comments.page_box() { /* points */ }
|
|
```
|
|
|
|
## What it is not
|
|
|
|
There is no PostScript interpreter here, and no writer of drawings. This crate is the layer every
|
|
EPS reader and writer in the Craft apps had its own copy of; what the program draws is read and
|
|
written elsewhere:
|
|
|
|
- VectorCraft's `crates/eps` runs the program into its own document and writes EPS, PostScript
|
|
print jobs and the legacy Illustrator format from it. That code speaks VectorCraft's document
|
|
model throughout and has not moved.
|
|
- A PostScript interpreter that returns what a file paints as a PDF is proposed in
|
|
craft-libs#159 (`craft-ps`, from DesignCraft).
|
|
- Decoding the TIFF or metafile preview is the caller's (`craft-metafile` reads the latter).
|
|
- The editing copy of the art inside a PDF-based Illustrator file is in the PDF's `PieceInfo`,
|
|
which a PDF reader finds.
|
|
|
|
## Where it came from
|
|
|
|
The Craft apps' own code (MIT OR Apache-2.0), merged so that no caller loses a case:
|
|
|
|
- the binary header reader and writer, the EPSI preview and ASCII85 from VectorCraft
|
|
(`crates/eps/src/lib.rs`, `import/preview.rs`, `ps.rs` at `6e873b80`);
|
|
- the header comments from DesignCraft (`crates/images/src/eps.rs` at `513f77b8`: bounded header
|
|
and trailer windows, `(atend)` boxes outside embedded documents, boxes of a finite size) and
|
|
VectorCraft (`import.rs`: `%%Pages`, the Illustrator markers);
|
|
- the same header and box reading also existed in EffectCraft (`crates/pdf/src/ps/eps.rs`) and,
|
|
as a signature check, in PhotoCraft (`crates/io/src/vector_preview.rs`).
|
|
|
|
## Clean room
|
|
|
|
Written from Adobe's published specifications only: the PostScript Language Document Structuring
|
|
Conventions 3.0 (Technical Note 5001), the Encapsulated PostScript File Format Specification 3.0
|
|
(Technical Note 5002, which defines the binary header and the EPSI preview), and the PostScript
|
|
Language Reference, third edition (ASCII85). The Illustrator markers are the header comments the
|
|
Adobe Illustrator File Format Specification, version 7.0 (1998), documents (listed by PRONOM at
|
|
<https://www.nationalarchives.gov.uk/PRONOM/fmt/423>), and the `PieceInfo` key names PDF-based
|
|
files carry in plain sight. No Adobe application was run or used as an oracle, no Adobe prolog or
|
|
other Adobe-authored PostScript is included, and no GPL, LGPL or AGPL code (Ghostscript, Inkscape,
|
|
Scribus…) was read. The test files are written by hand in the tests.
|
|
|
|
## Limits
|
|
|
|
The header is read from the first 64 KiB of the PostScript and a deferred bounding box from the
|
|
last 64 KiB (`MAX_HEADER`, `MAX_TRAILER`); a bounding box is four finite numbers that make a box
|
|
of a finite size; the binary header's offsets are checked against the file; an EPSI preview is
|
|
at most 8192 pixels a side and must have all its samples; `ascii85::decode` takes the most bytes
|
|
it may produce; a PDF is scanned for the Illustrator keys in its first and last mebibyte
|
|
(`MAX_PDF_SCAN`). Each limit has a test. No `unsafe`, no panics outside tests.
|