mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
PDF text test: one glyph that shows sara aa in one word and sara am in another reads right in both
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<u16> = 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user