PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge remote-tracking branch 'upstream/main' into fix/xfa-edit-picture-alternatives
Eric Kadencf2f4ec76679
diff --cc crates/engine/src/tests.rsindex 11963bdd8,afe6fd06c..d86125685--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@@ -7284,42 -7284,48 +7284,88 @@@ fn exporting_office_keeps_dotted_leaderassert!(positions.windows(2).all(|pair| pair[0] < pair[1]), "reading order: {html}");}+/// A date typed in another of an XFA date field's edit patterns is kept in its first: yymmdd and+/// yyyymd are rewritten as yyyymmdd, a date none of them reads is refused, and the data holds the date.+#[test]+fn a_date_typed_by_another_of_an_xfa_fields_edit_patterns_is_kept_in_its_first() {+ let mut s = Session::new();+ let id = s+ .open("dates.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::date_alternatives_template())), None)+ .expect("opens");+ let value = |s: &Session| s.get(id).unwrap().form.iter().find(|f| f.name == "leave").unwrap().value.clone();+ s.apply(id, Edit::SetFieldValue { name: "leave".into(), value: FieldValue::Text("261018".into()) }).expect("yymmdd is one of its patterns");+ assert_eq!(value(&s), ["20261018"]);+ s.apply(id, Edit::SetFieldValue { name: "leave".into(), value: FieldValue::Text("2026118".into()) }).expect("so is yyyymd");+ assert_eq!(value(&s), ["20261108"]);+ let refused = s.apply(id, Edit::SetFieldValue { name: "leave".into(), value: FieldValue::Text("2618".into()) });+ assert!(refused.is_err_and(|e| e.to_string().contains("yyyymmdd")), "none of its patterns reads it");+ assert_eq!(value(&s), ["20261108"]);+ // The data holds the date.+ let cos = pdfcraft_cos::Document::open(s.save_bytes(id).unwrap()).unwrap();+ let data = pdfcraft_xfa::parse_datasets(&pdfcraft_xfa::read_packets(&cos).unwrap().unwrap().xdp).expect("a datasets packet");+ assert_eq!(data.text_at(&pdfcraft_xfa::som_to_path("form[0].leave[0]")), Some("2026-11-08"));+}++/// A date the field's own scripts refuse is not tried again in another of an XFA date field's+/// patterns: only a date its format refuses is, which happens before any script runs, so the+/// scripts run once. This validate script counts its runs in `name` and accepts on its second.+#[test]+fn a_date_its_validate_script_refuses_is_not_tried_again() {+ let mut s = Session::new();+ let id = s+ .open("dates.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::date_alternatives_template())), None)+ .expect("opens");+ let script = Some("var n = Number(getField('name').value || 0) + 1; getField('name').value = String(n); event.rc = n >= 2;".to_string());+ s.apply(id, Edit::SetFieldScript { name: "leave".into(), event: "validate".into(), script }).unwrap();+ let value = |s: &Session, n: &str| s.get(id).unwrap().form.iter().find(|f| f.name == n).unwrap().value.clone();+ // Its format reads 18OCT26, and so does its DDMMMYY pattern.+ let refused = s.apply(id, Edit::SetFieldValue { name: "leave".into(), value: FieldValue::Text("18OCT26".into()) });+ assert!(refused.is_err(), "the validate script refused it, once");+ assert!(value(&s, "name").is_empty(), "nothing the refused run changed is kept: {:?}", value(&s, "name"));+}+++ /// A page whose forms go past the reading limit and whose picture goes past the image budget is+ /// listed once among the truncated pages.+ #[test]+ fn a_page_truncated_for_forms_and_pictures_is_listed_once() {+ let depth = compare::MAX_FORM_DEPTH + 1;+ let mut objs: Vec<String> = vec![+ "<< /Type /Catalog /Pages 2 0 R >>".into(),+ "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 200] >>".into(),+ String::new(),+ "<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 4 >>\nstream\n\0\x20\x40\x7f\nendstream".into(),+ "<< /Length 34 >>\nstream\nq 10 0 0 10 0 0 cm /Im Do Q /D1 Do\nendstream".into(),+ ];+ // Forms D1..D{depth}, objects 6 onwards, each drawing the next.+ let xobjects: String = (1..=depth).map(|i| format!("/D{i} {} 0 R ", 5 + i)).collect();+ let resources = format!("<< /XObject << /Im 4 0 R {xobjects}>> >>");+ for i in 1..=depth {+ let body = if i == depth { "q Q".to_string() } else { format!("/D{} Do", i + 1) };+ objs.push(format!(+ "<< /Type /XObject /Subtype /Form /BBox [0 0 200 200] /Resources {resources} /Length {} >>\nstream\n{body}\nendstream",+ body.len()+ ));+ }+ objs[2] = format!("<< /Type /Page /Parent 2 0 R /Contents 5 0 R /Resources {resources} >>");+ let mut bytes = b"%PDF-1.7\n".to_vec();+ let mut offsets = Vec::new();+ for (i, o) in objs.iter().enumerate() {+ offsets.push(bytes.len());+ bytes.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());+ }+ let xref = bytes.len();+ bytes.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());+ for o in offsets {+ bytes.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());+ }+ bytes.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());+ let mut s = Session::new();+ let id = s.open("both.pdf", None, Arc::new(bytes), None).unwrap();+ let doc = s.get(id).unwrap();+ assert_eq!(doc.export_pages_checked(false, 0).1, [0], "the forms alone truncate the page");+ let (exported, _) = doc.export_pages_checked(true, 1 << 20);+ assert_eq!(exported[0].images.len(), 1, "the picture exports within a sufficient budget");+ let (pages, truncated) = doc.export_pages_checked(true, 0);+ assert!(pages[0].images.is_empty(), "the picture is past the budget");+ assert_eq!(truncated, [0]);+ }