mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +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>
94 lines
4.4 KiB
Rust
94 lines
4.4 KiB
Rust
//! The binary header of an EPS file with a preview ("DOS EPS binary file header", EPSF 3.0
|
||
//! section 5.2): 30 bytes, little-endian.
|
||
//!
|
||
//! | bytes | |
|
||
//! |---|---|
|
||
//! | 0–3 | `C5 D0 D3 C6` |
|
||
//! | 4–7, 8–11 | offset and length of the PostScript |
|
||
//! | 12–15, 16–19 | offset and length of the Windows metafile preview (0 for none) |
|
||
//! | 20–23, 24–27 | offset and length of the TIFF preview (0 for none) |
|
||
//! | 28–29 | checksum of bytes 0–27 (XOR of the 16-bit words), or `FFFF` for none |
|
||
|
||
use crate::Error;
|
||
|
||
/// The first bytes of an EPS file with a binary header.
|
||
pub const DOS_MAGIC: [u8; 4] = [0xC5, 0xD0, 0xD3, 0xC6];
|
||
/// The binary header's size.
|
||
pub const DOS_HEADER: usize = 30;
|
||
|
||
/// The parts of an EPS file.
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||
pub struct Sections<'a> {
|
||
/// The PostScript program: the whole file when it has no binary header.
|
||
pub postscript: &'a [u8],
|
||
/// The TIFF preview.
|
||
pub tiff: Option<&'a [u8]>,
|
||
/// The Windows metafile preview.
|
||
pub metafile: Option<&'a [u8]>,
|
||
}
|
||
|
||
fn word(bytes: &[u8], at: usize) -> Result<usize, Error> {
|
||
let end = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||
let b = bytes.get(at..end).and_then(|b| <[u8; 4]>::try_from(b).ok()).ok_or(Error::OutsideFile)?;
|
||
usize::try_from(u32::from_le_bytes(b)).map_err(|_| Error::OutsideFile)
|
||
}
|
||
|
||
fn part(bytes: &[u8], at: usize) -> Result<&[u8], Error> {
|
||
let len_at = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||
let (start, len) = (word(bytes, at)?, word(bytes, len_at)?);
|
||
start.checked_add(len).and_then(|end| bytes.get(start..end)).ok_or(Error::OutsideFile)
|
||
}
|
||
|
||
/// The PostScript program in an EPS, ignoring preview ranges in its binary header.
|
||
///
|
||
/// This is useful to readers that execute only PostScript and historically accepted a valid
|
||
/// program section even when a separate preview range was damaged. Use [`sections`] when every
|
||
/// declared section must be validated.
|
||
pub fn postscript_section(bytes: &[u8]) -> Result<&[u8], Error> {
|
||
if bytes.starts_with(&DOS_MAGIC) { part(bytes, 4) } else { Ok(bytes) }
|
||
}
|
||
|
||
/// The sections of an EPS file: its PostScript and previews behind a binary header, or the whole
|
||
/// file as PostScript. A header that points outside the file is an error, whichever section it
|
||
/// is: such a file is cut short or not an EPS.
|
||
pub fn sections(bytes: &[u8]) -> Result<Sections<'_>, Error> {
|
||
if !bytes.starts_with(&DOS_MAGIC) {
|
||
return Ok(Sections { postscript: bytes, tiff: None, metafile: None });
|
||
}
|
||
// A preview with no offset or no length is absent.
|
||
let preview = |at: usize| -> Result<Option<&[u8]>, Error> {
|
||
let len_at = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||
if word(bytes, at)? == 0 || word(bytes, len_at)? == 0 { Ok(None) } else { part(bytes, at).map(Some) }
|
||
};
|
||
Ok(Sections { postscript: postscript_section(bytes)?, metafile: preview(12)?, tiff: preview(20)? })
|
||
}
|
||
|
||
/// `postscript` behind a binary header, followed by its previews. With neither preview the
|
||
/// PostScript is returned as it is: a header would only hide it from readers of plain EPS.
|
||
pub fn wrap(postscript: &[u8], tiff: Option<&[u8]>, metafile: Option<&[u8]>) -> Result<Vec<u8>, Error> {
|
||
let (tiff, metafile) = (tiff.filter(|t| !t.is_empty()), metafile.filter(|m| !m.is_empty()));
|
||
if tiff.is_none() && metafile.is_none() {
|
||
return Ok(postscript.to_vec());
|
||
}
|
||
let size = |part: Option<&[u8]>| u32::try_from(part.map_or(0, <[u8]>::len)).map_err(|_| Error::TooLarge);
|
||
let ps_len = u32::try_from(postscript.len()).map_err(|_| Error::TooLarge)?;
|
||
let (metafile_len, tiff_len) = (size(metafile)?, size(tiff)?);
|
||
let metafile_at = (DOS_HEADER as u32).checked_add(ps_len).ok_or(Error::TooLarge)?;
|
||
let tiff_at = metafile_at.checked_add(metafile_len).ok_or(Error::TooLarge)?;
|
||
let total = tiff_at.checked_add(tiff_len).ok_or(Error::TooLarge)?;
|
||
let mut out = Vec::with_capacity(usize::try_from(total).map_err(|_| Error::TooLarge)?);
|
||
out.extend(DOS_MAGIC);
|
||
out.extend((DOS_HEADER as u32).to_le_bytes());
|
||
out.extend(ps_len.to_le_bytes());
|
||
for (at, len) in [(metafile_at, metafile_len), (tiff_at, tiff_len)] {
|
||
out.extend((if len == 0 { 0 } else { at }).to_le_bytes());
|
||
out.extend(len.to_le_bytes());
|
||
}
|
||
// No checksum.
|
||
out.extend(0xFFFFu16.to_le_bytes());
|
||
out.extend(postscript);
|
||
out.extend(metafile.unwrap_or_default());
|
||
out.extend(tiff.unwrap_or_default());
|
||
Ok(out)
|
||
}
|