Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #3861: M7: keep a word on its line when its spacing differs by less than a thousandth of an em

aionggf4f9e009adb3
diff --cc crates/edit/src/tests.rs
index 3c66aeea6,20fc7b23e..e24dd9fdf
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@@ -5219,13 -5219,26 +5219,37 @@@ fn a_clipping_copy_on_the_same_baseline
assert_eq!(text::text_lines(&doc, 0).unwrap().len(), 2, "refused means untouched");
}
+/// A `TJ` with a surplus array operand (`[(FIRSTARR)] [(LASTARR)] TJ`) is malformed, but the
+/// operator still takes the one immediately before it, as the renderer, MuPDF and pdftotext do
+/// (#3569) -- not the first, which this reader took before, showing a word the page never draws.
+#[test]
+fn a_tj_with_a_surplus_array_reads_the_last_one() {
+ let doc = text_page("BT /F1 24 Tf 20 100 Td [(FIRSTARR)] [(LASTARR)] TJ ET");
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!(lines.len(), 1);
+ assert_eq!(lines[0].text, "LASTARR", "the operand immediately before TJ, as the page draws: {lines:?}");
+}
++
+ /// A producer wrote `0.0001 Tc` on one word of a line (Word through macOS Quartz). That spacing
+ /// can't be seen, so the word stays on its line; spacing that can be seen still keeps runs apart.
+ #[test]
+ fn a_word_with_invisible_extra_spacing_stays_on_its_line() {
+ let page = |spacing: &str| {
+ text_page(&format!(
+ "BT /F1 12 Tf 1 0 0 1 72 700 Tm (for company ) Tj ET \
+ BT /F1 12 Tf 1 0 0 1 141.36 700 Tm (A) Tj ET \
+ BT {spacing} Tc /F1 12 Tf 1 0 0 1 149.364 700 Tm (EROSOL) Tj ET"
+ ))
+ };
+ let blocks = text::text_blocks(&page("0.0001"), 0).unwrap();
+ assert_eq!(blocks.iter().map(|b| b.text.as_str()).collect::<Vec<_>>(), ["for company AEROSOL"]);
+
+ let mut doc = page("0.0001");
+ text::rewrite_block(&mut doc, 0, 0, Some("for company AEROSOL LLC"), &BlockStyle::default()).unwrap();
+ let lines = text::text_lines(&reopen(&doc), 0).unwrap();
+ assert_eq!(lines.iter().map(|l| l.text.as_str()).collect::<Vec<_>>(), ["for company AEROSOL LLC"]);
+ assert!((lines[0].rect[0] - 72.0).abs() < 0.01, "{:?}", lines[0].rect);
+
+ let blocks = text::text_blocks(&page("0.5"), 0).unwrap();
+ assert_eq!(blocks.iter().map(|b| b.text.as_str()).collect::<Vec<_>>(), ["for company A", "EROSOL"]);
+ }