diff --git a/Cargo.lock b/Cargo.lock index f6b5e507e6..c793b70f9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1048,6 +1048,11 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7258e42d8#7258e42d8dd909f2176fb08f09aae048611f3eaf" + [[package]] name = "craft-eps" version = "0.1.0" @@ -1248,10 +1253,15 @@ dependencies = [ [[package]] name = "craft-ps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d7c9632bfb3040cf45db394e7500cc93c81c49e0#d7c9632bfb3040cf45db394e7500cc93c81c49e0" +source = "git+https://github.com/storytold/craft-libs?rev=7258e42d8#7258e42d8dd909f2176fb08f09aae048611f3eaf" dependencies = [ + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7258e42d8)", "flate2", "kurbo 0.13.1", + "miniz_oxide 0.8.9", + "pdf-writer", + "skrifa 0.47.0", + "weezl 0.1.12", ] [[package]] @@ -6866,7 +6876,7 @@ dependencies = [ name = "vectorcraft-eps" version = "0.24.1" dependencies = [ - "craft-eps", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d7c9632bfb3040cf45db394e7500cc93c81c49e0)", "craft-image-codecs", "craft-pdf", "craft-ps", diff --git a/Cargo.toml b/Cargo.toml index 1a33442d15..d7d90bb1a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } -craft-ps = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0", default-features = false, features = ["write"] } +craft-ps = { git = "https://github.com/storytold/craft-libs", rev = "7258e42d8", default-features = false, features = ["read", "write"] } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } diff --git a/crates/eps/src/import/lex.rs b/crates/eps/src/import/lex.rs index 0ce1707f61..4e7c2b2a19 100644 --- a/crates/eps/src/import/lex.rs +++ b/crates/eps/src/import/lex.rs @@ -1,293 +1,76 @@ //! The scanner: PostScript source into objects (numbers, names, strings, procedures), with the //! position the program's own data (`currentfile`) is read from. +//! +//! Backed by `craft_ps::read::lex`. use std::rc::Rc; -use super::obj::{Obj, PsError, Res}; -use crate::ps; +use craft_ps::read::lex::{LexError, Number, Token}; -/// Deepest procedure nesting the scanner reads. -const MAX_NESTING: usize = 256; +use super::obj::{Obj, PsError, Res}; + +pub(crate) use craft_ps::read::lex::{IMMEDIATE, find, hex_decode}; pub(crate) struct Lexer<'a> { - src: &'a [u8], - pos: usize, - /// The whitespace character that ended the last token is still unread (data the program - /// reads from itself starts after it). - fresh: bool, + inner: craft_ps::read::lex::Lexer<'a>, /// The last object read has immediately evaluated names (`//name`) in it, read as executable /// names starting with [`IMMEDIATE`] for the interpreter to replace by their values. pub immediate: bool, } -/// How an immediately evaluated name (`//name`) is read, until the interpreter replaces it. -pub(crate) const IMMEDIATE: &str = "//"; - -fn is_space(b: u8) -> bool { - matches!(b, b' ' | b'\t' | b'\r' | b'\n' | b'\x0c' | b'\0') -} - -fn is_delim(b: u8) -> bool { - matches!(b, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%') -} - -fn syntax(what: &str) -> Res { - Err(PsError::Ps("syntaxerror", what.to_string())) -} - impl<'a> Lexer<'a> { pub fn new(src: &'a [u8]) -> Self { - Self { src, pos: 0, fresh: false, immediate: false } + Self { inner: craft_ps::read::lex::Lexer::new(src), immediate: false } } /// What is left to read. pub fn rest(&self) -> &'a [u8] { - self.src.get(self.pos..).unwrap_or_default() + self.inner.rest() } /// What is left to read as data: after the whitespace that ended the last token (a CR LF /// pair counts as one). pub fn data(&mut self) -> &'a [u8] { - if std::mem::take(&mut self.fresh) { - match self.peek() { - Some(b'\r') => { - self.pos += 1; - if self.peek() == Some(b'\n') { - self.pos += 1; - } - } - Some(b) if is_space(b) => self.pos += 1, - _ => {} - } - } - self.rest() + self.inner.data() } /// Skip `n` bytes (the data a filter or `readstring` took). pub fn advance(&mut self, n: usize) { - self.fresh = false; - self.pos = self.pos.saturating_add(n).min(self.src.len()); - } - - fn peek(&self) -> Option { - self.src.get(self.pos).copied() - } - - fn skip_space(&mut self) { - while let Some(b) = self.peek() { - if is_space(b) { - self.pos += 1; - } else if b == b'%' { - while let Some(c) = self.peek() { - if c == b'\n' || c == b'\r' { - break; - } - self.pos += 1; - } - } else { - break; - } - } + self.inner.advance(n); } /// The next object at the top level (`None` at the end of the file). pub fn next(&mut self) -> Res> { - self.immediate = false; - let t = self.token(0); - self.fresh = true; - t - } - - fn token(&mut self, depth: usize) -> Res> { - self.skip_space(); - let Some(b) = self.peek() else { return Ok(None) }; - self.pos += 1; - Ok(Some(match b { - b'(' => Obj::string(self.string()?), - b'<' => match self.peek() { - Some(b'<') => { - self.pos += 1; - Obj::Exec(Rc::from("<<")) - } - Some(b'~') => { - self.pos += 1; - Obj::string(self.ascii85()?) - } - _ => Obj::string(self.hex()?), - }, - b'>' => { - if self.peek() != Some(b'>') { - return syntax(">"); - } - self.pos += 1; - Obj::Exec(Rc::from(">>")) - } - b'[' => Obj::Exec(Rc::from("[")), - b']' => Obj::Exec(Rc::from("]")), - b'{' => { - if depth >= MAX_NESTING { - return Err(PsError::Limit("procedures nested too deeply")); - } - let mut items = vec![]; - loop { - self.skip_space(); - match self.peek() { - None => return syntax("{"), - Some(b'}') => { - self.pos += 1; - break; - } - _ => match self.token(depth + 1)? { - Some(o) => items.push(o), - None => return syntax("{"), - }, - } - } - Obj::proc(items) - } - b'}' | b')' => return syntax(&char::from(b).to_string()), - b'/' => { - // `//name` stands for the name's value when it is read. - let immediate = self.peek() == Some(b'/'); - if immediate { - self.pos += 1; - self.immediate = true; - } - let name = self.word(); - if immediate { Obj::Exec(Rc::from(format!("{IMMEDIATE}{name}").as_str())) } else { Obj::Name(Rc::from(name.as_str())) } - } - _ => { - self.pos -= 1; - let w = self.word(); - number(&w).unwrap_or_else(|| Obj::Exec(Rc::from(w.as_str()))) - } - })) - } - - /// A regular token's characters. - fn word(&mut self) -> String { - let start = self.pos; - while let Some(b) = self.peek() { - if is_space(b) || is_delim(b) { - break; - } - self.pos += 1; - } - String::from_utf8_lossy(self.src.get(start..self.pos).unwrap_or_default()).into_owned() - } - - /// A literal string after its `(`. - fn string(&mut self) -> Res> { - let mut out = vec![]; - let mut depth = 0usize; - loop { - let Some(b) = self.peek() else { return syntax("(") }; - self.pos += 1; - match b { - b'(' => { - depth += 1; - out.push(b); - } - b')' if depth == 0 => return Ok(out), - b')' => { - depth -= 1; - out.push(b); - } - b'\\' => { - let Some(e) = self.peek() else { return syntax("(") }; - self.pos += 1; - match e { - b'n' => out.push(b'\n'), - b'r' => out.push(b'\r'), - b't' => out.push(b'\t'), - b'b' => out.push(8), - b'f' => out.push(12), - b'\r' => { - if self.peek() == Some(b'\n') { - self.pos += 1; - } - } - b'\n' => {} - b'0'..=b'7' => { - let mut v = u32::from(e - b'0'); - for _ in 0..2 { - match self.peek() { - Some(d @ b'0'..=b'7') => { - v = v * 8 + u32::from(d - b'0'); - self.pos += 1; - } - _ => break, - } - } - out.push((v & 0xff) as u8); - } - other => out.push(other), - } - } - _ => out.push(b), - } - } - } - - /// A hexadecimal string after its `<`. - fn hex(&mut self) -> Res> { - let end = self.rest().iter().position(|b| *b == b'>').ok_or(PsError::Ps("syntaxerror", "<".into()))?; - let body = self.rest().get(..end).unwrap_or_default(); - let out = hex_decode(body).ok_or(PsError::Ps("syntaxerror", "<".into()))?; - self.advance(end + 1); - Ok(out) - } - - /// An ASCII85 string after its `<~`. - fn ascii85(&mut self) -> Res> { - let end = find(self.rest(), b"~>").ok_or(PsError::Ps("syntaxerror", "<~".into()))?; - let text = String::from_utf8_lossy(self.rest().get(..end + 2).unwrap_or_default()).into_owned(); - self.advance(end + 2); - ps::ascii85_decode(&text).ok_or(PsError::Ps("syntaxerror", "<~".into())) + let res = self.inner.next().map(|opt| opt.map(into_obj)).map_err(into_err); + self.immediate = self.inner.immediate; + res } } -/// Where `needle` first starts in `hay`. -pub(crate) fn find(hay: &[u8], needle: &[u8]) -> Option { - if needle.is_empty() { - return Some(0); +fn into_err(err: LexError) -> PsError { + match err { + LexError::Syntax(what) => PsError::Ps("syntaxerror", what), + LexError::Limit(msg) => PsError::Limit(msg), } - memchr::memmem::find(hay, needle) } -/// Hexadecimal digits (whitespace ignored, an odd last digit padded with 0); `None` on others. -pub(crate) fn hex_decode(body: &[u8]) -> Option> { - // Callers pass an already-delimited body. Do not let the filter decoder's EOD - // handling silently accept a stray terminator or discard bytes after it. - if body.contains(&b'>') { - return None; +fn into_obj(tok: Token) -> Obj { + match tok { + Token::Int(i) => Obj::Int(i), + Token::Real(r) => Obj::Real(r), + Token::Name(n) => Obj::Name(Rc::from(n.as_str())), + Token::Exec(n) => Obj::Exec(Rc::from(n.as_str())), + Token::Str(s) => Obj::string(s), + Token::Proc(items) => Obj::proc(items.into_iter().map(into_obj).collect()), } - craft_stream_codecs::asciihex::decode(body, body.len().div_ceil(2)).ok() } /// A number token (`12`, `-3.5`, `.5`, `1e-3`, `16#ff`), or `None`. pub(crate) fn number(w: &str) -> Option { - let first = w.bytes().next()?; - if !(first.is_ascii_digit() || matches!(first, b'+' | b'-' | b'.')) { - return None; + match craft_ps::read::lex::number(w)? { + Number::Int(i) => Some(Obj::Int(i)), + Number::Real(r) => Some(Obj::Real(r)), } - if let Ok(i) = w.parse::() { - return Some(Obj::Int(i)); - } - if let Some((base, digits)) = w.split_once('#') { - let base: u32 = base.parse().ok().filter(|b| (2..=36).contains(b))?; - // `from_str_radix` accepts a leading sign; a PostScript radix number has digits only. - if digits.starts_with(['+', '-']) { - return None; - } - return i64::from_str_radix(digits, base).ok().map(Obj::Int); - } - // Rust reads `inf` and `nan`; PostScript doesn't. - if w.bytes().any(|b| b.is_ascii_alphabetic() && !matches!(b, b'e' | b'E')) { - return None; - } - let v: f64 = w.parse().ok()?; - v.is_finite().then_some(Obj::Real(v)) } #[cfg(test)] @@ -300,33 +83,4 @@ mod tests { assert!(number("16#-ff").is_none()); assert!(matches!(number("16#ff"), Some(Obj::Int(255)))); } - #[test] - fn shared_hex_decode_keeps_body_boundaries_and_odd_nibbles() { - assert_eq!(hex_decode(b" 4\t1\n4\r2\x0c4\0"), Some(vec![0x41, 0x42, 0x40])); - assert_eq!(hex_decode(b""), Some(vec![])); - for invalid in [b"41>42".as_slice(), b">", b"4g", b"4\x0b1", b"\xff"] { - assert_eq!(hex_decode(invalid), None); - } - // Every byte pairs with each possible nibble, including all whitespace - // and invalid-byte combinations; this is the former body-only contract. - for first in 0..=255u8 { - for second in 0..=255u8 { - let input = [first, second]; - let mut digits = Vec::new(); - let mut valid = true; - for b in input { - if is_space(b) { - continue; - } - match char::from(b).to_digit(16) { - Some(d) => digits.push(d as u8), - None => valid = false, - } - } - let expected = - if !valid { None } else { Some(digits.chunks(2).map(|pair| (pair[0] << 4) | pair.get(1).copied().unwrap_or(0)).collect()) }; - assert_eq!(hex_decode(&input), expected, "{input:?}"); - } - } - } }