diff --git a/crates/xfa/src/layout/draw.rs b/crates/xfa/src/layout/draw.rs
index 6007005a3d..0000bf20dc 100644
--- a/crates/xfa/src/layout/draw.rs
+++ b/crates/xfa/src/layout/draw.rs
@@ -354,12 +354,25 @@ impl Layouter<'_> {
if !edge.visible() {
return Ok(());
}
- let (from, to) = if *slope_up {
+ // A line without a width is vertical and one without a height is horizontal,
+ // whatever its slope says; one too narrow or too flat for its stroke runs along its
+ // box's longer side (a zero-height line is horizontal).
+ let width = edge.thickness.clamp(0.1, 20.0);
+ let thin = inner.w <= width * 2.0 || inner.h <= width * 2.0;
+ let vertical = d.common.w.is_none() || (d.common.h.is_some() && thin && inner.h > inner.w);
+ let horizontal = !vertical && (d.common.h.is_none() || thin);
+ let (from, to) = if vertical {
+ let x = inner.x + inner.w / 2.0;
+ ((x, inner.y), (x, inner.bottom()))
+ } else if horizontal {
+ let y = inner.y + inner.h / 2.0;
+ ((inner.x, y), (inner.right(), y))
+ } else if *slope_up {
((inner.x, inner.bottom()), (inner.right(), inner.y))
} else {
((inner.x, inner.y), (inner.right(), inner.bottom()))
};
- self.push(Item::Line { from, to, width: edge.thickness.clamp(0.1, 20.0), color: edge.color.0, dashed: edge.stroke == Stroke::Dashed })
+ self.push(Item::Line { from, to, width, color: edge.color.0, dashed: edge.stroke == Stroke::Dashed })
}
Value::Rectangle(b) => self.draw_border(b, inner),
Value::Empty => Ok(()),
diff --git a/crates/xfa/src/layout/measure.rs b/crates/xfa/src/layout/measure.rs
index e8175553be..40c90f0197 100644
--- a/crates/xfa/src/layout/measure.rs
+++ b/crates/xfa/src/layout/measure.rs
@@ -70,7 +70,10 @@ impl Layouter<'_> {
let inner = (avail_w - c.margin.horizontal()).max(1.0);
(self.content_width(node, inner, ctx, depth) + c.margin.horizontal()).min(avail_w)
}
- Node::Draw(d) => self.natural_width(&d.value, &ctx.font, &ctx.para).map_or(avail_w, |t| (t + c.margin.horizontal()).min(avail_w)),
+ Node::Draw(d) => match &d.value {
+ Value::Line { edge, .. } => (edge.thickness.clamp(0.1, 20.0) + c.margin.horizontal()).min(avail_w),
+ _ => self.natural_width(&d.value, &ctx.font, &ctx.para).map_or(avail_w, |t| (t + c.margin.horizontal()).min(avail_w)),
+ },
Node::Field(f) if f.ui == Ui::Button => f
.caption
.as_ref()
diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs
index c1abcce190..5eb4e00e48 100644
--- a/crates/xfa/src/tests.rs
+++ b/crates/xfa/src/tests.rs
@@ -8006,3 +8006,44 @@ two"
assert_eq!(tree, before, "{structured}");
}
}
+
+/// A line draw without a width is vertical; one without a height is horizontal,
+/// even when the template declares a default slope="/".
+#[test]
+fn xfa_line_without_width_is_vertical_and_without_height_is_horizontal() {
+ let items = positioned(
+ r#"
+
+
+
+
+
+
+ "#,
+ );
+ let lines: Vec<&Item> = items.iter().filter(|i| matches!(i, Item::Line { .. })).collect();
+ assert_eq!(lines.len(), 6);
+ let is_vertical = |item: &Item| match item {
+ Item::Line { from, to, .. } => (from.0 - to.0).abs() < 1e-3,
+ _ => false,
+ };
+ let is_horizontal = |item: &Item| match item {
+ Item::Line { from, to, .. } => (from.1 - to.1).abs() < 1e-3,
+ _ => false,
+ };
+ assert!(is_vertical(lines[0]), "vert without width should be strictly vertical: {:?}", lines[0]);
+ // It is as wide as its stroke, so it sits at its own x (1in into a box at the content
+ // area's 0.5in), not in the middle of the row.
+ assert!(matches!(lines[0], Item::Line { from, .. } if (from.0 - (108.0 + 0.5)).abs() < 1e-3), "vert sits at its x: {:?}", lines[0]);
+ assert!(is_horizontal(lines[1]), "horiz without height should be strictly horizontal: {:?}", lines[1]);
+ assert!(is_horizontal(lines[2]), "a line 1pt high should be horizontal: {:?}", lines[2]);
+ assert!(is_vertical(lines[3]), "a line 1pt wide should be vertical: {:?}", lines[3]);
+ // A 25pt stroke is drawn 20pt wide, so a 48pt by 72pt box still follows its slope.
+ assert!(!is_vertical(lines[4]) && !is_horizontal(lines[4]), "a line wider than twice its drawn stroke follows its slope: {:?}", lines[4]);
+ // A 2pt by 0pt line is a 2pt horizontal stroke, not a zero-length vertical one.
+ assert!(
+ matches!(lines[5], Item::Line { from, to, .. } if (from.1 - to.1).abs() < 1e-3 && (to.0 - from.0).abs() > 1.9),
+ "a zero-height line is horizontal: {:?}",
+ lines[5]
+ );
+}