mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
* ps: add a write feature for PostScript tokens Numbers, strings, matrices, path data and Flate, so apps can emit a PostScript or EPS program without owning the token serializer. Path and paint operators can use the Language Reference names or the one-letter names a bound prolog defines. Container concerns stay in craft-eps. Part of #778 and #790. * ps: tighten write-feature number trimming and Flate bounds - Require flate2 1.1.10 so inflate rejects an incomplete zlib stream. - Trim trailing zeros only when the token has a decimal point, so places=0 keeps integer zeros at the 1e15 boundary. - Use saturating_add when bounding inflate, so max = u64::MAX does not overflow.
59 lines
3.6 KiB
Markdown
59 lines
3.6 KiB
Markdown
# craft-ps
|
||
|
||
PostScript and EPS for the Craft apps. No feature is enabled by default.
|
||
|
||
| Feature | Provides |
|
||
| --- | --- |
|
||
| `write` | PostScript writing primitives: numbers, strings, matrices, path data and Flate |
|
||
| `read` | An EPS interpreter: runs the program and returns what it paints as a one-page PDF |
|
||
|
||
## Writing PostScript
|
||
|
||
`write` emits the tokens of a PostScript or EPS program. The caller writes the page program and
|
||
any short operator names a prolog binds; this module formats numbers (non-finite values as zero),
|
||
escapes string and byte literals, writes matrices, raises quadratic segments to cubics, and
|
||
compresses or inflates Flate (`FlateDecode`) payloads. Path and paint operators can use the
|
||
Language Reference names or the one-letter names a bound prolog defines. Container concerns (DSC
|
||
comments, a binary preview header, ASCII85) stay in `craft-eps`.
|
||
|
||
## Reading EPS
|
||
|
||
`read::eps_to_pdf(bytes, limits)` runs an EPS (plain, or DOS EPS with a binary header) and
|
||
returns a one-page PDF 1.4 the size of its bounding box: vectors stay vectors, CMYK and spot
|
||
colours stay as they are, so an app can draw and export the result as a placed PDF.
|
||
`read::cached_pdf` converts with the default limits and remembers the result by content.
|
||
`is_eps`, `bounding_box` and `postscript_section` read the file without running it.
|
||
EPS binary sections and DSC comments are read by `craft-eps`.
|
||
|
||
It is written from the PostScript Language Reference (third edition), the EPS specification,
|
||
the Type 1 Font Format and Adobe Technical Notes 5012 (Type 42), 5176 (CFF) and 5177 (Type 2
|
||
charstrings); no GPL code was used. It covers what EPS files use:
|
||
|
||
- the language: Level 3 operators, an explicit execution stack (no Rust recursion for
|
||
PostScript control flow), `stopped`, resources, `eexec`;
|
||
- graphics: paths, painting, clipping, `strokepath`, user paths and forms;
|
||
- colour: device spaces, Separation and DeviceN, Indexed, ICCBased, Lab;
|
||
- images, with filters decoded as they are read (ASCIIHex, ASCII85, RunLength, LZW, Flate with
|
||
predictors, SubFile, ReusableStream; JPEG is passed through);
|
||
- smooth shadings (types 1–7, meshes included), shading and tiling patterns;
|
||
- text in Type 1, Type 3, Type 42 and CFF fonts (`StartData`), CIDFonts and composite fonts
|
||
through CMaps, horizontal and vertical.
|
||
|
||
Not covered: Level 2 binary tokens, CIDFontType 0 with Type 1 charstrings, CIDFontType 1 and
|
||
the old composite FMapTypes other than 2 and 9.
|
||
|
||
## Limits
|
||
|
||
`Limits { steps, memory, stack_overflow_noop, skip_images, keep_unpainted, zero_division_zero, fallback_font }` bounds one conversion. The optional font callback supplies host stand-ins for missing fonts; booleans let a host retain prior operand-overflow, image, partial-page, and numeric-coercion behavior. Every operator charges the work it does in
|
||
proportion to its operands (elements copied or searched, path segments, glyphs, samples)
|
||
against `steps` as it goes, and every byte read or decoded at any filter level counts too, so
|
||
the step limit bounds run time. `memory` bounds everything a conversion makes, copies
|
||
included. The defaults are 50 million steps and 1 GiB, or 20 million and 256 MiB on wasm32;
|
||
real EPS files use at most a few million steps and tens of megabytes, and a program that runs
|
||
into the limits ends within seconds. By default a program that fails or exceeds them before
|
||
painting anything gives `Err`; setting `keep_unpainted` returns a blank page with the warning.
|
||
One that fails later keeps what it painted (with a warning).
|
||
|
||
`cargo run -p craft-ps --features read --example eps2pdf -- in.eps out.pdf` converts one file
|
||
and prints the steps and memory it used.
|