Skip to content

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

M12: a flowed table a page break cuts between its rows keeps its border on each page

Eric Kaden35716c628213
diff --git a/crates/xfa/src/layout.rs b/crates/xfa/src/layout.rs
index 60c2e41ba..a574c2bb3 100644
--- a/crates/xfa/src/layout.rs
+++ b/crates/xfa/src/layout.rs
@@ -646,16 +646,6 @@ impl Layouter<'_> {
self.pages.get(page).is_some_and(|p| p.items.len() > at)
}
- /// Before placing a child `h` tall inside flowed subforms: when it doesn't fit what is left
- /// of the content area but would fit a fresh one, break there ([`Self::break_frames`]).
- fn break_before(&mut self, h: f64) -> Result<(), XfaError> {
- let (rect, y, placed) = self.cursor();
- if self.frames.is_empty() || !placed || y + h <= rect.bottom() + 0.01 || h > rect.h + 0.01 {
- return Ok(());
- }
- self.break_frames(h)
- }
-
/// Break the flowed subforms being laid out ([`Self::frames`]) across content areas, as
/// another viewer breaks such a section on a synthetic form: each one's part here ends after
/// its content and its bottom margin (the innermost first, each inside the next), the flow
@@ -757,13 +747,19 @@ impl Layouter<'_> {
}
/// Make room for `h`: stay, or move to the next content area / page. Returns whether a
- /// break happened.
+ /// break happened. Something that fits a fresh area breaks the flowed subforms it is in with
+ /// it ([`Self::break_frames`]), a table's rows included; one taller than an area just moves
+ /// on, and those subforms are drawn without their border ([`Self::finish_frame`]).
fn make_room(&mut self, h: f64) -> Result<bool, XfaError> {
let (rect, y, placed) = self.cursor();
if y + h <= rect.bottom() + 0.01 || !placed {
return Ok(false);
}
- self.next_area()?;
+ if h <= rect.h + 0.01 {
+ self.break_frames(h)?;
+ } else {
+ self.next_area()?;
+ }
Ok(true)
}
@@ -966,7 +962,6 @@ impl Layouter<'_> {
continue;
}
let (cw, ch) = self.size_of(child, inner_w, &ctx, depth + 1);
- self.break_before(ch)?;
self.flow_block(child, inner_x, (cw, ch), &ctx, depth + 1, csib)?;
}
}
diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs
index 110b89629..ac6366dd2 100644
--- a/crates/xfa/src/tests.rs
+++ b/crates/xfa/src/tests.rs
@@ -2131,6 +2131,40 @@ fn a_flowed_subform_is_drawn_with_its_border_under_its_content() {
assert!(form.warnings.is_empty(), "{:?}", form.warnings);
}
+/// A flowed table a page break cuts between its rows gets a closed border round its part on
+/// each page, as a flowed subform does: its margins repeat, and its overflow leader starts the
+/// second part after the top margin.
+#[test]
+fn a_flowed_table_cut_between_its_rows_keeps_its_border_on_each_page() {
+ let row = |name: &str, text: &str| {
+ format!(
+ r#"<subform name="{name}" layout="row"><draw name="{name}a" h="0.3in"><value><text>{text}</text></value></draw><draw name="{name}b" h="0.3in"><value><text>-</text></value></draw></subform>"#
+ )
+ };
+ let rows: String = (0..40).map(|i| row(&format!("r{i}"), &format!("Row {i}"))).collect();
+ let table = format!(
+ r#"<subform name="grid" layout="table" columnWidths="2in 2in" w="4in"><border><edge/><fill><color value="230,230,230"/></fill></border><margin topInset="0.1in" bottomInset="0.2in"/><overflow leader="head"/>{}{rows}</subform>"#,
+ row("head", "Head")
+ );
+ let body = format!(r#"<subform name="body" layout="tb" w="7.5in">{table}</subform>"#);
+ let form = layout_xml(&one_area_form(r#"layout="tb""#, &body)).unwrap();
+ assert_eq!(form.pages.len(), 2);
+ let fills = |page: usize| -> Vec<Rect> {
+ form.pages[page].items.iter().filter_map(|i| if let Item::Fill { rect, .. } = i { Some(*rect) } else { None }).collect()
+ };
+ let lines = |page: usize| form.pages[page].items.iter().filter(|i| matches!(i, Item::Line { .. })).count();
+ let (first, second) = (fills(0), fills(1));
+ assert_eq!((first.len(), second.len()), (1, 1), "a part on each page");
+ assert_eq!((lines(0), lines(1)), (4, 4), "each part's border is closed");
+ // Page 1: 0.1in of margin, the leader and 32 rows of 0.3in fill the area to its bottom.
+ assert!((first[0].y - 36.0).abs() < 0.01 && (first[0].bottom() - 756.0).abs() < 0.01, "{:?}", first[0]);
+ // Page 2: the top margin again, the leader, the other 8 rows, the bottom margin.
+ assert!((second[0].y - 36.0).abs() < 0.01 && (second[0].bottom() - (36.0 + 7.2 + 9.0 * 21.6 + 14.4)).abs() < 0.01, "{:?}", second[0]);
+ let head = form.pages[1].items.iter().find_map(|i| if let Item::Text(t) = i { (t.text == "Head").then_some(t.baseline) } else { None });
+ assert!(head.is_some_and(|b| b > 43.2 && b < 64.8), "the leader starts the second part after its top margin: {head:?}");
+ assert!(form.warnings.is_empty(), "{:?}", form.warnings);
+}
+
#[test]
fn a_page_break_keeps_a_fields_border_with_the_field() {
// 6in of heading, then a bordered 5in field: it doesn't fit the 10in area and moves to