Skip to content

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

M12: a draw without text or a width keeps the width it has room for

Eric Kadenec14687c6d62
diff --git a/crates/xfa/src/layout.rs b/crates/xfa/src/layout.rs
index 91e79de14..a5e515594 100644
--- a/crates/xfa/src/layout.rs
+++ b/crates/xfa/src/layout.rs
@@ -1015,14 +1015,17 @@ impl Layouter<'_> {
}
/// How wide text `v` is unwrapped, in `font` and `para`, with a hair to spare so that laying
- /// it out at that width keeps it on its lines; `None` for what isn't text.
+ /// it out at that width keeps it on its lines; `None` for what isn't text, or text that draws
+ /// nothing.
fn natural_width(&self, v: &Value, font: &Font, para: &Para) -> Option<f64> {
let rich = match v {
Value::Text(t) => text::plain(t),
Value::Rich(r) => r.clone(),
_ => return None,
};
- Some(text::natural_width(&rich, para, &Face::of(font), &|id| self.embed(id)) + 0.01)
+ // No text (a bordered spacer or rule): nothing to size to, so it keeps what it has room for.
+ let w = text::natural_width(&rich, para, &Face::of(font), &|id| self.embed(id));
+ (w > 0.0).then_some(w + 0.01)
}
fn value_height(&self, v: &Value, w: f64, ctx: &Ctx) -> f64 {
diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs
index 70ffba338..48068e7bd 100644
--- a/crates/xfa/src/tests.rs
+++ b/crates/xfa/src/tests.rs
@@ -1232,6 +1232,16 @@ fn a_label_without_a_width_is_as_wide_as_its_text() {
assert!(lines.len() > 1, "wraps: {} line(s)", lines.len());
}
+/// A draw without a width and without text (a bordered spacer or rule) keeps the width it has
+/// room for: with no text to size it to, it doesn't shrink to its margins.
+#[test]
+fn an_empty_draw_without_a_width_keeps_the_width_it_has_room_for() {
+ let items = positioned(r#"<draw name="rule" x="0" y="0" h="12pt"><value><text></text></value><border><edge thickness="1pt"/></border></draw>"#);
+ let widest =
+ items.iter().filter_map(|i| if let Item::Line { from, to, .. } = i { Some((to.0 - from.0).abs()) } else { None }).fold(0.0_f64, f64::max);
+ assert!((widest - 540.0).abs() < 0.5, "the border spans the 7.5in box: {widest}");
+}
+
/// A button without a width is as wide as its caption, not the subform it sits in, unless its
/// minimum width is wider.
#[test]