mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-pdf: content reads a lone minus between operands as the number 0 (#1958)
This commit is contained in:
1 parent
ba3a5e71a0
commit
1bb51887fd
3 files changed
+87
-3
No files matched your search
@@ -136,7 +136,11 @@ let bytes = write_full(&doc, &SaveOptions::default())?; // rewrite what t
|
||||
|
||||
With `content`, `cos::content` reads and writes page content streams. `parse` keeps each
|
||||
operator's byte span and skips (and counts) what it cannot read instead of failing; inline images
|
||||
stay whole, so `parse` → `serialize_ops` round-trips them. `Matrix` holds the arithmetic content
|
||||
stay whole, so `parse` → `serialize_ops` round-trips them. Between operands, a token of one or two
|
||||
signs with a minus among them (`-`, `--`, `-+`, `+-`), followed by whitespace, a delimiter or the
|
||||
end, is read as the number 0, as hayro-syntax reads it. Inside an array or dictionary, or among an
|
||||
inline image's parameters, such a token still makes the whole operand unreadable (such an image is
|
||||
left out of `ops` and counted in `ambiguous_images`). `Matrix` holds the arithmetic content
|
||||
interpretation needs.
|
||||
|
||||
- Reads every cross-reference form (tables, streams, hybrid `/XRefStm`) and object streams. A
|
||||
|
||||
@@ -137,6 +137,10 @@ pub struct Ops<'a> {
|
||||
ends: &'a [usize],
|
||||
boundary: usize,
|
||||
comment_edges: Vec<(bool, bool)>,
|
||||
/// Arrays, dictionaries and hex strings whose read failed, until as many `]` or `>` (of either
|
||||
/// kind) have passed: what follows the opener is re-read token by token, and a sign there is
|
||||
/// left to the lexer, not read as 0.
|
||||
broken: usize,
|
||||
}
|
||||
|
||||
impl<'a> Ops<'a> {
|
||||
@@ -151,6 +155,7 @@ impl<'a> Ops<'a> {
|
||||
ends,
|
||||
boundary: 0,
|
||||
comment_edges: vec![(false, false); ends.len()],
|
||||
broken: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +175,7 @@ impl Iterator for Ops<'_> {
|
||||
type Item = Op;
|
||||
|
||||
fn next(&mut self) -> Option<Op> {
|
||||
let Ops { data, lx, operands, start, skipped, ambiguous_images, ends, boundary, comment_edges: edges } = self;
|
||||
let Ops { data, lx, operands, start, skipped, ambiguous_images, ends, boundary, comment_edges: edges, broken } = self;
|
||||
let (data, ends): (&[u8], &[usize]) = (data, ends);
|
||||
loop {
|
||||
let trivia = lx.pos;
|
||||
@@ -187,6 +192,14 @@ impl Iterator for Ops<'_> {
|
||||
return None;
|
||||
};
|
||||
let at = lx.pos;
|
||||
if *broken == 0
|
||||
&& let Some(run) = lone_minus(data, at)
|
||||
{
|
||||
lx.pos = at + run;
|
||||
start.get_or_insert(at);
|
||||
operands.push(Object::Int(0));
|
||||
continue;
|
||||
}
|
||||
let operand_start = matches!(b, b'/' | b'(' | b'<' | b'[' | b'+' | b'-' | b'.') || b.is_ascii_digit();
|
||||
if operand_start {
|
||||
match lx.object() {
|
||||
@@ -200,6 +213,9 @@ impl Iterator for Ops<'_> {
|
||||
lx.pos = data.len();
|
||||
}
|
||||
Err(_) => {
|
||||
if matches!(b, b'[' | b'<') {
|
||||
*broken += 1;
|
||||
}
|
||||
let read = lx.pos.saturating_sub(at);
|
||||
#[cfg(test)]
|
||||
tests::RESCANNED.with(|c| c.set(c.get() + read));
|
||||
@@ -219,6 +235,9 @@ impl Iterator for Ops<'_> {
|
||||
}
|
||||
if is_delim(b) {
|
||||
// A stray `)`, `>`, `]`, `{`, `}`.
|
||||
if matches!(b, b']' | b'>') {
|
||||
*broken = broken.saturating_sub(1);
|
||||
}
|
||||
lx.pos += 1;
|
||||
*skipped += 1;
|
||||
continue;
|
||||
@@ -272,6 +291,23 @@ impl Iterator for Ops<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The length of the token at `at` when it is one or two signs with a minus among them, followed by
|
||||
/// whitespace, a delimiter or the end (`-`, `--`, `-+`, `+-`). That is no number to the lexer, but
|
||||
/// the number 0 to hayro-syntax, both the copy vendored here and PdfCraft's. A lone `-` occurs in
|
||||
/// real files: PDF.js bug 1753983 has one among the operands of `c`. The two copies disagree on `+`
|
||||
/// and `++` and on runs such as `---`, both read `+++` as an unknown operator, and signs after a
|
||||
/// digit belong to that number, so the lexer's reading of those stands. No more than four bytes
|
||||
/// are looked at: the one before `at` and three from it.
|
||||
fn lone_minus(data: &[u8], at: usize) -> Option<usize> {
|
||||
if !at.checked_sub(1).and_then(|i| data.get(i)).is_none_or(|c| is_ws(*c) || is_delim(*c)) {
|
||||
return None;
|
||||
}
|
||||
let token = data.get(at..)?;
|
||||
let run = token.iter().take(2).take_while(|c| matches!(c, b'+' | b'-')).count();
|
||||
let ends = token.get(run).is_none_or(|c| is_ws(*c) || is_delim(*c));
|
||||
(run > 0 && ends && token.get(..run)?.contains(&b'-')).then_some(run)
|
||||
}
|
||||
|
||||
/// Record the piece boundaries crossed by the comments in `trivia` (skipped whitespace and
|
||||
/// comments). `boundary` is the first boundary not yet passed; boundaries only move forward.
|
||||
fn comment_edges(data: &[u8], trivia: std::ops::Range<usize>, ends: &[usize], boundary: &mut usize, edges: &mut [(bool, bool)]) {
|
||||
|
||||
@@ -42,13 +42,14 @@ fn splicing_visits_each_piece_a_few_times() {
|
||||
|
||||
#[test]
|
||||
fn a_long_unreadable_number_is_read_once() {
|
||||
for junk in ["+", "5"] {
|
||||
for junk in ["+", "5", "-", "-+", "+-"] {
|
||||
let n = 20_000;
|
||||
let src = format!("{}{} 1 0 0 1 0 0 cm", junk.repeat(n), if junk == "5" { "x" } else { "" });
|
||||
RESCANNED.with(|c| c.set(0));
|
||||
let p = parse(src.as_bytes());
|
||||
let rescanned = RESCANNED.with(|c| c.get());
|
||||
assert!(rescanned <= 2 * n, "{rescanned} bytes read for a {n}-byte token");
|
||||
assert!(junk == "5" || junk == "+" || rescanned > 0, "the run of {junk:?} is counted");
|
||||
assert_eq!(p.ops.last().and_then(|o| o.nums::<6>()), Some([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]));
|
||||
}
|
||||
// A short damaged number still recovers byte by byte.
|
||||
@@ -57,6 +58,49 @@ fn a_long_unreadable_number_is_read_once() {
|
||||
assert_eq!(p.ops[0].nums::<2>(), Some([5.0, 0.0]));
|
||||
}
|
||||
|
||||
/// A lone `-`, or two signs with a minus among them, between operands, is the number 0 to
|
||||
/// hayro-syntax, as a lone `-` is in real files (PDF.js bug 1753983, among the operands of `c`):
|
||||
/// the second `Tr` of `3 Tr - Tr` gets 0, and `0 - (a) "` three operands. The token takes no
|
||||
/// bytes from what follows and starts the operator's span. Other sign tokens are read as before: a
|
||||
/// `+` alone, three signs, signs before a letter or after a digit, and signs inside an array or
|
||||
/// dictionary the lexer can't read.
|
||||
#[test]
|
||||
fn a_lone_minus_is_read_as_zero() {
|
||||
let ops = |data: &[u8]| -> Vec<(Vec<u8>, Vec<Object>)> { parse(data).ops.into_iter().map(|o| (o.op, o.operands)).collect() };
|
||||
let zero = || Object::Int(0);
|
||||
assert_eq!(ops(b"3 Tr - Tr"), [(b"Tr".to_vec(), vec![Object::Int(3)]), (b"Tr".to_vec(), vec![zero()])]);
|
||||
assert_eq!(ops(b"0 - (a) \""), [(b"\"".to_vec(), vec![zero(), zero(), Object::String(PdfString::literal(b"a".to_vec()))])]);
|
||||
for data in [&b"- w"[..], b"-- w", b"+- w", b"-+ w", b"-"] {
|
||||
let want: &[(Vec<u8>, Vec<Object>)] = if data == b"-" { &[] } else { &[(b"w".to_vec(), vec![zero()])] };
|
||||
assert_eq!(ops(data), want, "{}", String::from_utf8_lossy(data));
|
||||
}
|
||||
assert_eq!(ops(b"(a)-/N gs"), [(b"gs".to_vec(), vec![Object::String(PdfString::literal(b"a".to_vec())), zero(), Object::name("N")])]);
|
||||
for data in [&b"- w"[..], b"-- w", b"-+ w", b"+- w"] {
|
||||
let parsed = parse(data);
|
||||
assert_eq!((parsed.skipped, parsed.ops.first().map(|o| o.span.clone())), (0, Some(0..data.len())), "{}", String::from_utf8_lossy(data));
|
||||
}
|
||||
// As before.
|
||||
assert_eq!(ops(b"5 + Tc"), [(b"Tc".to_vec(), vec![Object::Int(5)])]);
|
||||
assert_eq!(ops(b"/Span << /A - >> BDC"), [(b"BDC".to_vec(), vec![Object::name("Span"), Object::name("A")])]);
|
||||
assert_eq!(ops(b"[(a) - ] TJ"), [(b"TJ".to_vec(), vec![Object::String(PdfString::literal(b"a".to_vec()))])]);
|
||||
assert_eq!(ops(b"[1 - 2] 0 d"), [(b"d".to_vec(), vec![Object::Int(1), Object::Int(2), Object::Int(0)])]);
|
||||
assert_eq!(ops(b"<- w"), [(b"w".to_vec(), vec![])]);
|
||||
// Past a broken array's or dictionary's end, a lone minus is 0 again; not before the outer one's.
|
||||
assert_eq!(ops(b"[(a) - ] TJ - Tc")[1], (b"Tc".to_vec(), vec![zero()]));
|
||||
assert_eq!(ops(b"/Span << /A - >> BDC - w")[1], (b"w".to_vec(), vec![zero()]));
|
||||
assert_eq!(
|
||||
ops(b"/P << /A << /B - >> /C - >> BDC - w"),
|
||||
[(b"BDC".to_vec(), vec![Object::name("P"), Object::name("A"), Object::name("B"), Object::name("C")]), (b"w".to_vec(), vec![zero()])]
|
||||
);
|
||||
assert_eq!(ops(b"5 ++ Tc"), [(b"Tc".to_vec(), vec![Object::Int(5)])]);
|
||||
assert_eq!(ops(b"--- w"), [(b"w".to_vec(), vec![])]);
|
||||
assert_eq!(ops(b"-x w"), [(b"x".to_vec(), vec![]), (b"w".to_vec(), vec![])]);
|
||||
assert_eq!(ops(b"5-- Tc"), [(b"Tc".to_vec(), vec![])]);
|
||||
for (data, n) in [(&b"-5 w"[..], -5.0), (b"+5 w", 5.0), (b"-.5 w", -0.5), (b"--5 w", -5.0)] {
|
||||
assert_eq!(parse(data).ops.first().and_then(|o| o.num_back(0)), Some(n), "{}", String::from_utf8_lossy(data));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operators_keep_operands_and_spans() {
|
||||
let src = b"q 1 0 0 1 72 720 cm BT /F1 12 Tf (Hello \\(world\\)) Tj [(A) -120 (B)] TJ ET Q";
|
||||
|
||||
Reference in new issue
Block a user