PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Integrate KenM76 PR #4906
FredAppsd9ddd8a8b05f
diff --cc crates/edit/src/text/interp.rsindex e4248aede,3fa2d8e0d..16b04b406--- a/crates/edit/src/text/interp.rs+++ b/crates/edit/src/text/interp.rs@@@ -761,25 -490,74 +764,25 @@@ pub(super) fn interpretlet mut out: Vec<Shown> = Vec::new();let mut ts = carry.ts.clone();let mut at_bt = (0usize, ts.clone());- let mut stack: Vec<Ts> = std::mem::take(&mut carry.stack);+ let mut stack: Vec<(Ts, Matrix, Matrix)> = std::mem::take(&mut carry.stack);let (mut tm, mut tlm) = (Matrix::IDENTITY, Matrix::IDENTITY);let mut bt = 0usize;- let mut marked = std::mem::take(&mut carry.marked);- let mut layers = std::mem::take(&mut carry.layers);- let mut actual: Vec<Option<(usize, usize, String)>> = Vec::new();+ let mut marked =+ MarkedContent { regions: std::mem::take(&mut carry.marked), layers: std::mem::take(&mut carry.layers), ..MarkedContent::default() };for (i, op) in ops.iter().enumerate() {match op.op.as_slice() {// No depth cap: every `EMC` pops, so a capped push would pair it with the enclosing// sequence (#3573). Both stacks stay bounded by the number of operators.b"BMC" | b"BDC" => {- // Marked text paragraphs/headings are authoritative boundaries. Ignore- // artifacts and inner spans, which inherit their enclosing region.- let region = matches!(op.name(0), Some(b"P" | b"H" | b"H1" | b"H2" | b"H3" | b"H4" | b"H5" | b"H6" | b"LI" | b"TD" | b"TH"));- marked.push(if region { Some(i) } else { marked.last().copied().flatten() });- // A layer named in the resources is told apart by its name, an inline one by where it opens.- let layer = op.is("BDC") && op.name(0) == Some(b"OC".as_slice());- layers.push(layer.then(|| op.operands.get(1).and_then(Object::as_name).map_or_else(|| format!("@{i}").into_bytes(), <[u8]>::to_vec)));- // The property list is inline or named in the resources' /Properties (§14.6.2).- let list = logical_text.and_then(|properties| match op.operands.get(1) {- Some(Object::Name(n)) => properties.get(n).and_then(|o| doc.dict(o)),- other => other.and_then(Object::as_dict).cloned(),- });- let logical = list.as_ref().and_then(|d| d.get(b"ActualText")).and_then(|s| match s {- Object::String(s) if s.bytes.len() <= 4_000_000 => Some(s.to_text()),- _ => None,- });- actual.push(logical.map(|s| (out.len(), i, s)));+ marked.begin(doc, op, i, logical_text, out.len());}b"EMC" => {- marked.pop();- layers.pop();- if let Some(Some((start, open, logical))) = actual.pop() {- let tail = out.split_off(start.min(out.len()));- let mut items = tail.into_iter();- if let Some(mut first) = items.next() {- for shown in items {- first.glyph_count = first.glyph_count.saturating_add(shown.glyph_count);- first.extra_ops.push(shown.op);- first.extra_ops.extend(shown.extra_ops);- first.rect = [- first.rect[0].min(shown.rect[0]),- first.rect[1].min(shown.rect[1]),- first.rect[2].max(shown.rect[2]),- first.rect[3].max(shown.rect[3]),- ];- first.frame = union(first.frame, shown.frame);- first.ink.extend(shown.ink);- }- first.extra_ops.extend([open, i]);- // Only right-to-left ActualText is already in typing order and must stay- // out of the line's bidi recovery. Word and Chrome wrap Latin ligatures- // (`fi`, `fl`) in ActualText spans mid-word; those keep joining their line.- first.logical_order = logical.chars().any(crate::bidi::is_rtl);- first.actual_text = true;- first.text = logical;- first.start_x = first.rect[0];- first.end_x = first.rect[2];- first.end_y = first.baseline;- first.advance = (first.end_x - first.start_x) / first.tm.then(&first.state.ctm).0[0].abs().max(1e-6);- out.push(first);- }- }+ marked.end(i, &mut out);}- b"q" => stack.push(ts.clone()),+ b"q" => stack.push((ts.clone(), tm, tlm)),b"Q" => {- if let Some(s) = stack.pop() {- ts = s;+ if let Some(saved) = stack.pop() {+ (ts, tm, tlm) = saved;}}b"cm" => {