Skip to content

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

Merge PR #4317: M12: round XFA check buttons draw a round background and border

aiongg761353775bdd
diff --cc crates/engine/src/tests.rs
index bdc595b61,df0ed33a9..3c6776a13
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -4773,32 -4675,35 +4773,61 @@@ fn measurement_edits_are_transactional_
);
}
+ /// A round XFA check button opens drawn round: the appearance its laid-out widget gets draws a
+ /// circle, the on state with its mark in it, while a square one keeps its box.
+ #[test]
+ fn round_xfa_check_buttons_open_drawn_round() {
+ let xdp = r##"<?xml version="1.0" encoding="UTF-8"?>
+ <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/"><template xmlns="http://www.xfa.org/schema/xfa-template/3.3/"><subform name="form" layout="tb">
+ <field name="round" w="1in" h="0.3in"><ui><checkButton shape="round"/></ui><items><integer>1</integer><integer>0</integer></items></field>
+ <field name="square" w="1in" h="0.3in"><ui><checkButton/></ui><items><integer>1</integer><integer>0</integer></items></field>
+ </subform></template></xdp:xdp>"##;
+ let mut s = Session::new();
+ let id = s.open("form.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(xdp)), None).expect("opens");
+ let cos = s.get(id).unwrap().editor.as_ref().unwrap().cos.clone();
+ let all = pdfcraft_forms::fields(&cos);
+ // The decoded appearance of `name`'s widget in `state`.
+ let drawn = |name: &str, state: &[u8]| {
+ let w = all.iter().find(|f| f.name.ends_with(name)).unwrap_or_else(|| panic!("no field {name}")).widgets[0].obj;
+ let wd = cos.get(w).as_dict().cloned().unwrap();
+ let n = cos.resolve(wd.get(b"AP").unwrap().as_dict().unwrap().get(b"N").unwrap());
+ let r = n.as_dict().unwrap().reference(state).unwrap_or_else(|| panic!("{name} has no {} state", String::from_utf8_lossy(state)));
+ let pdfcraft_cos::Object::Stream(st) = &*cos.get(r) else { panic!("a stream") };
+ String::from_utf8_lossy(&st.decoded().unwrap()).into_owned()
+ };
+ for state in [&b"Off"[..], b"1"] {
+ let round = drawn("round", state);
+ assert!(round.contains(" c S") && !round.contains(" re S"), "the round one's border is a circle: {round}");
+ assert!(drawn("square", state).contains(" re S"), "the square one keeps its box");
+ }
+ }
+
+#[test]
+fn a_dynamic_xfa_form_is_saved_without_the_usage_rights_its_layout_voided() {
+ let xdp = pdfcraft_xfa::fixtures::template(3);
+ let original = Arc::new(pdfcraft_xfa::fixtures::usage_rights_shell(&xdp, "", false));
+ let mut s = Session::new().with_clock(|| 1_700_000_000);
+ let id = s.open("form.pdf", None, original.clone(), None).expect("opens");
+ let doc = s.get(id).unwrap();
+ let warnings = &doc.xfa.as_ref().expect("laid out from the template").warnings;
+ assert!(warnings.iter().any(|w| w.contains("usage rights")), "the user is told: {warnings:?}");
+ assert!(!doc.dirty, "laying out is still not an edit");
+ s.apply(id, Edit::SetFieldValue { name: "familyName".into(), value: FieldValue::Text("Singh".into()) }).unwrap();
+ let incremental = s.save_bytes(id).unwrap();
+ assert!(incremental.starts_with(&original[..]), "still an incremental save");
+ // Saved either way, the file no longer carries them, and keeps what was filled in.
+ for saved in [incremental, s.save_full_bytes(id).unwrap()] {
+ let cos = pdfcraft_cos::Document::open(saved.clone()).unwrap();
+ let catalog = cos.get(cos.root().unwrap()).as_dict().cloned().unwrap();
+ assert!(!catalog.contains(b"Perms"), "{catalog:?}");
+ let again = s.open("saved.pdf", None, saved, None).unwrap();
+ let d2 = s.get(again).unwrap();
+ assert_eq!(d2.form.iter().find(|f| f.name == "familyName").unwrap().value, vec!["Singh".to_string()]);
+ let warnings = &d2.xfa.as_ref().expect("the layout is remembered").warnings;
+ assert!(!warnings.iter().any(|w| w.contains("usage rights")), "nothing left to say: {warnings:?}");
+ }
+}
+
#[test]
fn dynamic_xfa_forms_are_laid_out_on_open_filled_and_saved_incrementally() {
let original = Arc::new(pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::template(3)));