PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(edit): read marked content that spans content streams in one pass (#3081)
skylar taylor-barrick9b4af15b21c6
diff --git a/crates/audit/src/tests.rs b/crates/audit/src/tests.rsindex 64ea05bde..63d9220cc 100644--- a/crates/audit/src/tests.rs+++ b/crates/audit/src/tests.rs@@ -518,4 +518,14 @@ fn reading_blocks_respect_stream_comments_and_split_operands() {let doc = open(two_streams(b"BT /F1 24 Tf 72 700 Td (Split operand)", b"Tj ET"));let text: String = pdfcraft_edit::reading_blocks(&doc, 0).unwrap().into_iter().map(|block| block.editor_text).collect();assert_eq!(text, "Split operand");+ // Marked content opened in one stream applies to text in the next: its /ActualText replaces+ // the glyphs, as when the streams are read joined.+ for (s1, s2) in [+ (b"BT /F1 24 Tf 72 700 Td /Span <</ActualText (Replacement text)".as_slice(), b">> BDC (Glyphs) Tj EMC ET".as_slice()),+ (b"BT /F1 24 Tf 72 700 Td /Span <</ActualText (Replacement text)>> BDC", b"(Glyphs) Tj EMC ET"),+ ] {+ let doc = open(two_streams(s1, s2));+ let text: String = pdfcraft_edit::reading_blocks(&doc, 0).unwrap().into_iter().map(|block| block.editor_text).collect();+ assert_eq!(text, "Replacement text", "{}", String::from_utf8_lossy(s1));+ }}diff --git a/crates/edit/src/text.rs b/crates/edit/src/text.rsindex 93d1b54fc..916aaeadd 100644--- a/crates/edit/src/text.rs+++ b/crates/edit/src/text.rs@@ -1237,12 +1237,14 @@ fn reading_lines(doc: &Document, page: usize) -> Result<Vec<TextLine>, EditErrorpath: Vec<pdfcraft_cos::ObjRef>,visits: usize,}- fn walk(w: &mut Walk, ops: &[Op], resources: &Dict, carry: &mut Carry, stream: usize) {+ /// `stream` gives the stream number of the operator at an index of `ops`.+ fn walk(w: &mut Walk, ops: &[Op], resources: &Dict, carry: &mut Carry, stream: &dyn Fn(usize) -> usize) {let fonts_res = resources.get(b"Font").map(|f| w.doc.resolve(f)).and_then(|f| f.as_dict().cloned()).unwrap_or_default();let mut cache = HashMap::new();let mut invoked = Vec::new();let shown = interpret(w.doc, ops, &fonts_res, &mut cache, carry, Some(&mut invoked), true);let mut last = None;+ let mut last_stream = None;let mut forms = invoked.into_iter().peekable();for s in shown {while let Some((_, name, ts)) = forms.next_if(|f| f.0 < s.op) {@@ -1250,7 +1252,12 @@ fn reading_lines(doc: &Document, page: usize) -> Result<Vec<TextLine>, EditError// Text after a form starts a new line.last = None;}- add_shown(&mut w.lines, &mut last, s, stream);+ let si = stream(s.op);+ // Text from another stream starts a new line, as when each stream was read alone.+ if last_stream.replace(si).is_some_and(|prev| prev != si) {+ last = None;+ }+ add_shown(&mut w.lines, &mut last, s, si);}for (_, name, ts) in forms {enter(w, resources, &name, ts);@@ -1266,7 +1273,7 @@ fn reading_lines(doc: &Document, page: usize) -> Result<Vec<TextLine>, EditErrorif let Some(r) = form.obj {w.path.push(r);}- walk(w, &ops, &form.resources, &mut carry, stream);+ walk(w, &ops, &form.resources, &mut carry, &|_| stream);if form.obj.is_some() {w.path.pop();}@@ -1276,19 +1283,14 @@ fn reading_lines(doc: &Document, page: usize) -> Result<Vec<TextLine>, EditErrorlet streams = content_streams(doc, &p.dict);let mut w = Walk { doc, lines: Vec::new(), next_stream: streams.len(), path: Vec::new(), visits: 0 };let mut carry = Carry::new();- // Read the page's streams as the one stream they are, as rendering and editing do: a- // comment or an operator's operands can run on from one stream into the next. Each operator- // still counts as its first stream's, which keeps lines from different streams apart.+ // Read the page's streams as the one stream they are, in one pass, as rendering and editing+ // do: a comment, an operator's operands or a marked-content sequence (its /ActualText too)+ // can run on from one stream into the next. Text still counts as the stream its operator+ // starts in, which keeps lines from different streams apart.let pieces = Pieces::join(&streams.into_iter().map(|(_, data)| data).collect::<Vec<_>>());- let mut grouped: Vec<Vec<Op>> = (0..pieces.len()).map(|_| Vec::new()).collect();- for op in pieces.parse().ops {- if let Some(group) = grouped.get_mut(pieces.pieces_of(&op).0) {- group.push(op);- }- }- for (si, ops) in grouped.iter().enumerate() {- walk(&mut w, ops, &res, &mut carry, si);- }+ let ops = pieces.parse().ops;+ let stream_of = |i: usize| ops.get(i).map_or(0, |op| pieces.pieces_of(op).0);+ walk(&mut w, &ops, &res, &mut carry, &stream_of);let mut lines = w.lines;// A visual-order Indic font draws pre-base vowel signs before their consonant.for l in lines.iter_mut().filter(|l| l.visual_order && !l.actual_text) {