PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #3697: Edit text: keep a line together across subsets of the same font (#3661)
aionggfa1095350a5b
diff --cc crates/edit/src/text.rsindex 953736e86,5f8866bf4..f8cd15fc6--- a/crates/edit/src/text.rs+++ b/crates/edit/src/text.rs@@@ -2851,55 -2806,20 +2868,69 @@@ pub(crate) fn translate_blocks(doc: &mumod tests {use super::*;+ fn block(text: &str, rect: [f64; 4]) -> TextBlock {+ TextBlock {+ text: text.into(),+ rect,+ base_font: String::new(),+ size: 12.0,+ bold: false,+ italic: false,+ color: [0.0; 3],+ rotation: 0.0,+ lines: Vec::new(),+ rows: Vec::new(),+ editor_text: text.into(),+ spans: Vec::new(),+ invisible: false,+ }+ }++ #[test]+ fn a_block_key_finds_its_paragraph_after_others_move_and_not_a_neighbour_in_its_place() {+ let (a, b, c) = (block("Alpha", [0.0, 80.0, 50.0, 100.0]), block("Bravo", [0.0, 50.0, 50.0, 60.0]), block("Charlie", [0.0, 0.0, 50.0, 20.0]));+ let key = BlockKey::of(&b);+ assert_eq!(key.find(&[a.clone(), b.clone(), c.clone()], 1), Some(1));+ // Alpha and Bravo became one paragraph: Bravo's number now belongs to Charlie.+ let merged = block("Alpha Bravo", [0.0, 50.0, 50.0, 100.0]);+ assert_eq!(key.find(&[merged, c.clone()], 1), None);+ // A paragraph that split in two shifts the others down.+ assert_eq!(key.find(&[a.clone(), block("x", [0.0; 4]), b.clone(), c], 1), Some(2));+ // The same words somewhere else, or other words in the same place, are not the paragraph.+ assert_eq!(key.find(&[block("Bravo", [0.0, 40.0, 50.0, 50.0])], 0), None);+ assert_eq!(key.find(&[block("Bravo!", b.rect)], 0), None);+ assert_eq!(key.find(&[], 0), None);+ // Two paragraphs drawn on top of one another are told apart by the hint.+ assert_eq!(key.find(&[b.clone(), b], 1), Some(1));+ // A box that is not a number matches nothing, and cannot panic.+ let nan = BlockKey::of(&block("Bravo", [f64::NAN; 4]));+ assert_eq!(nan.find(&[block("Bravo", [f64::NAN; 4])], 0), None);+ }++ #[test]+ fn the_block_that_overlaps_most_is_the_edited_paragraph() {+ let blocks = [block("a", [0.0, 80.0, 50.0, 100.0]), block("b", [0.0, 40.0, 50.0, 70.0])];+ assert_eq!(block_overlapping(&blocks, [0.0, 60.0, 50.0, 95.0]), Some(0));+ assert_eq!(block_overlapping(&blocks, [0.0, 35.0, 50.0, 75.0]), Some(1));+ assert_eq!(block_overlapping(&blocks, [200.0, 0.0, 250.0, 10.0]), None, "a box that overlaps nothing");+ assert_eq!(block_overlapping(&[], [0.0, 0.0, 1.0, 1.0]), None);+ assert_eq!(block_overlapping(&blocks, [f64::NAN; 4]), None);+ }++ #[test]+ fn a_subset_tag_is_the_only_difference_between_subsets_of_one_font() {+ assert!(same_base_font("ABCDEF+Arial", "GHIJKL+Arial"));+ assert!(same_base_font("Arial", "ABCDEF+Arial"));+ assert!(same_base_font("Arial", "Arial"));+ assert!(!same_base_font("ABCDEF+Arial", "GHIJKL+Helvetica"));+ // Five letters are part of the name, not a subset tag.+ assert!(!same_base_font("ABCDE+Helvetica-Bold", "Helvetica-Bold"));+ // A tag with no name after it, or no name at all, names no font.+ assert!(!same_base_font("ABCDEF+", "GHIJKL+"));+ assert!(!same_base_font("", ""));+ assert!(!same_base_font("Arial", ""));+ }+#[test]fn paragraph_wrapping_keeps_ideographic_spaces() {let width = |s: &str| s.chars().count() as f64;