Skip to content

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

M12: an XFA button's box is its whole field

Eric Kaden37f43e947d6f
diff --git a/crates/xfa/src/layout.rs b/crates/xfa/src/layout.rs
index 72b515137..d1943d68f 100644
--- a/crates/xfa/src/layout.rs
+++ b/crates/xfa/src/layout.rs
@@ -1662,13 +1662,13 @@ impl Layouter<'_> {
}
Ui::Button => {
let caption = f.caption.as_ref().and_then(|c| c.value.plain()).unwrap_or_default().trim().to_string();
- // A button's box is its field border; without one it is invisible (a link
- // over text, say).
+ // A button's box is its field border, around the whole field (its margins pad
+ // the caption, not the box); without one it is invisible (a link over text, say).
let border = match &f.common.border {
Some(b) => widget_border(Some(b), false),
None => WidgetBorder { color: None, fill: None, shape: BorderShape::None, width: 0.0 },
};
- (WidgetKind::Button { caption }, ui, border, None, f.tooltip.clone())
+ (WidgetKind::Button { caption }, rect, border, None, f.tooltip.clone())
}
Ui::DateTimeEdit => {
let pattern = date_pattern(f.picture.as_deref());
diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs
index e14917b7e..70ec183c3 100644
--- a/crates/xfa/src/tests.rs
+++ b/crates/xfa/src/tests.rs
@@ -1266,6 +1266,17 @@ fn a_button_without_a_height_is_as_tall_as_its_caption() {
assert!((button("10.5pt") - 10.5).abs() < 0.05, "{}", button("10.5pt"));
}
+#[test]
+fn a_buttons_box_is_the_whole_field_whatever_its_margins() {
+ // The button's box is its field's border: its margins pad the caption, not the box.
+ let items = positioned(
+ r#"<field name="help" x="1in" y="10pt" w="100pt" h="20pt"><ui><button/></ui><margin topInset="5pt" leftInset="3pt"/><border><edge/></border><caption><value><text>Help</text></value><font typeface="Arial" size="8pt"/></caption></field>"#,
+ );
+ let button = items.iter().find_map(|i| if let Item::Widget(w) = i { Some(w.rect) } else { None }).expect("the button");
+ assert!((button.w - 100.0).abs() < 0.05 && (button.h - 20.0).abs() < 0.05, "{button:?}");
+ assert!((button.x - (36.0 + 72.0)).abs() < 0.05, "{button:?}");
+}
+
#[test]
fn page_area_without_occur_serves_every_body_page() {
let section = |n: u32| {