From d3baf2e9e7ffe35f61384bbcf2840b6e9b23be2d Mon Sep 17 00:00:00 2001 From: mo <131036780+mbolling@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:52:00 -0500 Subject: [PATCH] PDF text test: one glyph that shows sara aa in one word and sara am in another reads right in both MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After the typed-order change of issue #2899, the sara aa glyph shows ำ in น้ำ and า in นา. The PDF writer gives a glyph one ToUnicode entry, taken from its first use; krilla wraps a later use with other text in its own /ActualText span. The test plots นา and น้ำ in one drawing, in both orders, and reads the page glyph by glyph (ActualText where there is one, else ToUnicode), which must give the typed words. Co-Authored-By: Claude Opus 5.5 --- crates/io/tests/pdf_text.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/io/tests/pdf_text.rs b/crates/io/tests/pdf_text.rs index 4f0267c45..1d44482eb 100644 --- a/crates/io/tests/pdf_text.rs +++ b/crates/io/tests/pdf_text.rs @@ -413,3 +413,37 @@ fn thai_text_reads_in_typed_order() { assert_eq!(read, "น้ำ", "{map:?}"); assert_eq!(pdf_read::count(doc.content(), "Tj") + fills(&doc), 4, "no, nikhahit, mai tho and sara aa: {}", doc.content()); } + +/// The text the page reads as, glyph by glyph in content order: a glyph's `/ActualText` when it +/// has one, else its code through the font's `ToUnicode` map. +fn read_text(doc: &Doc) -> String { + let map = pdf_read::to_unicode_map(doc.pages[0].fonts[0].to_unicode.as_deref().unwrap()); + let mut out = String::new(); + for text in doc.content().split("BT").skip(1) { + if let Some((_, rest)) = text.split_once("/ActualText<") { + let hex = rest.split('>').next().unwrap(); + let units: Vec = hex.as_bytes().chunks(4).map(|u| u16::from_str_radix(std::str::from_utf8(u).unwrap(), 16).unwrap()).collect(); + out.extend(char::decode_utf16(units.into_iter().skip_while(|u| *u == 0xFEFF)).map(Result::unwrap)); + continue; + } + for s in pdf_read::shown(text.as_bytes()) { + for c in s.chunks(2).map(|c| u32::from(c[0]) << 8 | u32::from(c[1])) { + out.push_str(map.iter().find(|(k, _)| *k == c).map_or("?", |(_, s)| s.as_str())); + } + } + } + out +} + +/// One glyph shows sara am in น้ำ and sara aa in นา: whichever comes second gets its own +/// `/ActualText`, so both read right whichever comes first. +#[test] +fn a_glyph_shared_by_two_characters_reads_right_in_both() { + for words in [["นา", "น้ำ"], ["น้ำ", "นา"]] { + let mut d = drawing(); + text(&mut d, &Space::Model, "Thai", (0.0, 50.0), words[0], (0.0, 1.0, 0.0)); + text(&mut d, &Space::Model, "Thai", (0.0, 0.0), words[1], (0.0, 1.0, 0.0)); + let doc = pdf(&d, &Space::Model, json!({})); + assert_eq!(read_text(&doc), words.concat(), "{}", doc.content()); + } +}