Files
craft-libs/crates/ps
dotCooCoo a6b7bdce3e ps: write numbers from their rounded integers instead of the float formatter
push_num_places now writes the digits of round(v * 10^places) directly
when that integer is below 2^52 in magnitude, placing the decimal point
and trimming fractional zeros itself. Below 2^52 the integer is exact
and dividing it by 10^places lands within half a decimal step of the
exact quotient, so the text is the same as the formatter's. NaN,
infinities, scaling overflow and larger rounded values take the
existing formatter path unchanged. Requests for more than nine places
are still clamped to nine first.

Tests compare the output with the formatter path for places 0 to 12
over fixed boundary values (negative zero, ties, 1e15, 2^52, f64::MAX,
non-finite values) and about 144,000 values in all (about 48,000
generated, plus the neighboring bit patterns of each). Widening the
limit to 2^54 makes the comparison fail, for example
-1709448349065955.5 at one place.

On 1,200,000 path coordinates at four places (release build, wall
clock on a Linux host running other builds at the same time, seven
alternating runs), the formatter path took 362 to 373 ms and the
integer path 38 to 44 ms.
2026-11-16 14:08:31 -08:00
..

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.