PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M2: the Identity-H ToUnicode map leaves out DEL and C1 controls
Lcoding15978c77c583c6
diff --git a/crates/fonts/src/pdf.rs b/crates/fonts/src/pdf.rsindex 9dab6d2d7..0762eaaa1 100644--- a/crates/fonts/src/pdf.rs+++ b/crates/fonts/src/pdf.rs@@ -1225,17 +1225,17 @@ fn unicode_map(doc: &Document, font: &Dict, composite: bool) -> Unicode {}/// A ToUnicode CMap mapping each two-byte code to the same UTF-16 code unit, leaving out the-/// surrogates (which are not characters on their own) and the C0 controls. One `bfrange` per-/// high byte, so no range crosses a last-byte boundary (§9.10.3).+/// surrogates (which are not characters on their own) and the controls (C0, DEL and C1, as the+/// renderer's text layer does). No range crosses a last-byte boundary (§9.10.3).fn identity_to_unicode() -> Vec<u8> {- let highs: Vec<u32> = (0u32..=0xFF).filter(|h| !(0xD8..=0xDF).contains(h)).collect();+ let mut ranges = vec![(0x20u32, 0x7Eu32), (0xA0, 0xFF)];+ ranges.extend((0x01u32..=0xFF).filter(|h| !(0xD8..=0xDF).contains(h)).map(|h| (h << 8, (h << 8) | 0xFF)));let mut cmap = String::from("1 begincodespacerange <0000> <FFFF> endcodespacerange\n");// At most 100 entries per section (§9.7.5.2 limits for CMap files).- for chunk in highs.chunks(100) {+ for chunk in ranges.chunks(100) {cmap.push_str(&format!("{} beginbfrange\n", chunk.len()));- for h in chunk {- let low = if *h == 0 { 0x20 } else { 0 };- cmap.push_str(&format!("<{h:02X}{low:02X}> <{h:02X}FF> <{h:02X}{low:02X}>\n"));+ for (lo, hi) in chunk {+ cmap.push_str(&format!("<{lo:04X}> <{hi:04X}> <{lo:04X}>\n"));}cmap.push_str("endbfrange\n");}@@ -1537,6 +1537,10 @@ mod tests {let m = Metrics::from_dict(&doc, &f);assert_eq!(m.decode(&[0, b'Z', 0, 0xFC, 0, b'r', 0x20, 0xAC, 0x4E, 0x2D]), "Zür€中", "{name}");assert_eq!(m.text_of(0x0001), None);+ assert_eq!(m.text_of(0x007F), None, "DEL is a control");+ assert_eq!(m.text_of(0x0085), None, "a C1 control");+ assert_eq!(m.text_of(0x00A0).as_deref(), Some("\u{A0}"), "the range resumes after C1");+ assert_eq!(m.text_of(0x007E).as_deref(), Some("~"));assert_eq!(m.text_of(0xD800), None);assert_eq!(m.text_of(0xFFFD).as_deref(), Some("\u{FFFD}"));}diff --git a/crates/render/src/text.rs b/crates/render/src/text.rsindex 14ca4f15a..91328fed7 100644--- a/crates/render/src/text.rs+++ b/crates/render/src/text.rs@@ -1705,7 +1705,8 @@ mod tests {/// WSCAD writes `/ToUnicode /Identity-H`, a name rather than a CMap stream, on Type 0 fonts/// whose two-byte codes are UTF-16 code units. The codes read as that Unicode text (as in- /// Acrobat), so the page's text can be selected and found; before, it had none.+ /// Acrobat), so the page's text can be selected and found; before, it had none. Controls (here a+ /// DEL and a C1 control at the line ends) are not text.#[test]fn identity_to_unicode_names_give_text() {use lopdf::{Document, Object, Stream, dictionary};@@ -1717,8 +1718,7 @@ mod tests {"DW" => 600, "CIDToGIDMap" => "Identity"});let font = doc.add_object(dictionary! { "Type" => "Font", "Subtype" => "Type0", "BaseFont" => "RobotoMono-Regular", "Encoding" => "Identity-H", "DescendantFonts" => vec![Object::Reference(cid)], "ToUnicode" => name });- let operators =- "BT /F1 12 Tf 1 0 0 1 20 250 Tm <004400650063006B0062006C006100740074> Tj 1 0 0 1 20 200 Tm <005A00FC0072006900630068002020AC> Tj ET";+ let operators = "BT /F1 12 Tf 1 0 0 1 20 250 Tm <004400650063006B0062006C006100740074007F> Tj 1 0 0 1 20 200 Tm <005A00FC0072006900630068002020AC0085> Tj ET";let content = doc.add_object(Stream::new(dictionary! {}, operators.as_bytes().to_vec()));let pages = doc.new_object_id();let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages, "MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(300), Object::Integer(300)], "Resources" => dictionary! {"Font" => dictionary! {"F1" => font}}, "Contents" => content });