diff --git a/crates/pdf/src/build.rs b/crates/pdf/src/build.rs index ad0f68216..3310bd9e0 100644 --- a/crates/pdf/src/build.rs +++ b/crates/pdf/src/build.rs @@ -10,6 +10,9 @@ pub(crate) const MAX_PATH: usize = 1 << 20; /// The deepest the open groups nest (the render tree and its consumers recurse through them). const MAX_DEPTH: usize = 256; +/// The most characters of a logical text label kept in a group name. +pub(crate) const MAX_LABEL: usize = 256; + #[derive(Clone, Copy, PartialEq, Eq)] enum Kind { Plain, @@ -124,6 +127,9 @@ impl Builder { self.skip("logical text nesting limit"); return false; } + // Every paint wrapped by the annotation carries its own copy of the label, so a long + // /ActualText is cut to MAX_LABEL characters rather than copied whole into each. + let text: String = text.chars().take(MAX_LABEL).collect(); self.text_labels.push(format!("Text: {text}")); true } @@ -209,3 +215,17 @@ impl Builder { (root, flags, self.skipped) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_long_logical_label_is_cut_once_at_begin_text() { + let mut b = Builder::new(); + assert!(b.begin_text(&"é".repeat(MAX_LABEL * 4))); + assert_eq!(b.text_labels[0].chars().count(), "Text: ".len() + MAX_LABEL); + assert!(b.begin_text("short")); + assert_eq!(b.text_labels[1], "Text: short"); + } +} diff --git a/crates/pdf/src/color.rs b/crates/pdf/src/color.rs index 5a7e9af86..8b150f907 100644 --- a/crates/pdf/src/color.rs +++ b/crates/pdf/src/color.rs @@ -779,7 +779,24 @@ impl Func { } } -/// PDF text string → Rust string (UTF-16BE with a byte-order mark, else PDFDocEncoding ≈ Latin-1). +/// PDFDocEncoding (ISO 32000-2 Annex D) where it differs from Latin-1: bytes 0x18–0x1F and +/// 0x80–0xA0 (0x9F is undefined). +const PDFDOC_LOW: [char; 8] = ['˘', 'ˇ', 'ˆ', '˙', '˝', '˛', '˚', '˜']; +const PDFDOC_HIGH: [char; 33] = [ + '•', '†', '‡', '…', '—', '–', 'ƒ', '⁄', '‹', '›', '−', '‰', '„', '“', '”', '‘', '’', '‚', '™', 'fi', 'fl', 'Ł', 'Œ', 'Š', 'Ÿ', 'Ž', 'ı', 'ł', 'œ', 'š', 'ž', + '\u{FFFD}', '€', +]; + +fn pdfdoc_char(c: u8) -> char { + match c { + 0x18..=0x1F => PDFDOC_LOW[usize::from(c - 0x18)], + 0x80..=0xA0 => PDFDOC_HIGH[usize::from(c - 0x80)], + _ => char::from(c), + } +} + +/// PDF text string → Rust string (UTF-16BE with a byte-order mark, UTF-8 with one, else +/// PDFDocEncoding). pub fn text_string(b: &[u8]) -> String { if b.starts_with(&[0xFE, 0xFF]) { let u: Vec = b[2..].as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); @@ -788,13 +805,19 @@ pub fn text_string(b: &[u8]) -> String { if let Some(rest) = b.strip_prefix(&[0xEF, 0xBB, 0xBF]) { return String::from_utf8_lossy(rest).into_owned(); } - b.iter().map(|&c| c as char).collect() + b.iter().map(|&c| pdfdoc_char(c)).collect() } #[cfg(test)] mod tests { use super::*; + #[test] + fn text_strings_without_a_bom_are_pdfdocencoding() { + assert_eq!(text_string(b"a\x80b\x92\xa0\x18\xe9"), "a•b™€˘é"); + assert_eq!(text_string(b"\xfe\xff\x00A\x20\x22"), "A•"); + } + #[test] fn sample_count_at_and_over_the_cap() { // 4 Mi entries × 4 outputs fill the cap exactly; a fifth output passes it.