pdf: decode PDFDocEncoding text strings and bound logical text labels

text_string decoded strings without a byte-order mark as Latin-1, so an
/ActualText with byte 0x80 became U+0080 instead of a bullet. It now maps
PDFDocEncoding's 0x18-0x1F and 0x80-0xA0. Each paint inside an annotation
carries its own copy of the label, so begin_text keeps at most 256 characters
of it.
This commit is contained in:
FredApps committed 2026-11-16 08:30:38 +01:00
1 parent 790b065d53
commit 843deea4d6
2 files changed
+45 -2

No files matched your search

+20
View File
@@ -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");
}
}
+25 -2
View File
@@ -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<u16> = 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.