remove unnecessary short lived header check

This commit is contained in:
PgBiel 2025-04-20 13:26:42 -03:00
parent 6e21eae3eb
commit 9a01b9bfe8

View File

@ -1462,63 +1462,59 @@ impl<'a> GridLayouter<'a> {
self.current.repeated_header_rows.min(self.lrows.len()); self.current.repeated_header_rows.min(self.lrows.len());
} }
let footer_would_be_widow = if let Some(last_header_row) = self let footer_would_be_widow =
.current if !self.lrows.is_empty() && self.current.repeated_header_rows > 0 {
.repeated_header_rows // If headers are repeating, then we already know they are not
.checked_sub(1) // short-lived as that is checked, so they have orphan prevention.
.and_then(|last_header_index| self.lrows.get(last_header_index)) if self.lrows.len() == self.current.repeated_header_rows
{ && may_progress_with_offset(
let last_header_end = last_header_row.index(); self.regions,
if self.grid.rows.len() > last_header_end // Since we're trying to find a region where to place all
&& self // repeating + pending headers, it makes sense to use
.grid // 'header_height' and include even non-repeating pending
.footer // headers for this check.
.as_ref() self.current.header_height + self.current.footer_height,
.and_then(Repeatable::as_repeated) )
.is_none_or(|footer| footer.start != last_header_end) {
&& self.lrows.len() == self.current.repeated_header_rows // Header and footer would be alone in this region, but
&& may_progress_with_offset( // there are more rows beyond the headers and the footer.
self.regions, // Push an empty region.
// Since we're trying to find a region where to place all self.lrows.clear();
// repeating + pending headers, it makes sense to use self.current.last_repeated_header_end = 0;
// 'header_height' and include even non-repeating pending self.current.repeated_header_rows = 0;
// headers for this check. true
self.current.header_height + self.current.footer_height, } else {
) false
{ }
// Header and footer would be alone in this region, but there are more } else if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
// rows beyond the header and the footer. Push an empty region. // If no rows other than the footer have been laid out so far,
self.lrows.clear(); // and there are rows beside the footer, then don't lay it out
self.current.last_repeated_header_end = 0; // at all. (Similar check from above, but for the case without
self.current.repeated_header_rows = 0; // headers.)
true // TODO: widow prevention for non-repeated footers with a
// similar mechanism / when implementing multiple footers.
self.lrows.is_empty()
&& may_progress_with_offset(
self.regions,
// This header height isn't doing much as we just
// confirmed that there are no headers in this region,
// but let's keep it here for correctness. It will add
// zero anyway.
self.current.header_height + self.current.footer_height,
)
&& footer.start != 0
} else { } else {
false false
} };
} else if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
// If no rows other than the footer have been laid out so far, and
// there are rows beside the footer, then don't lay it out at all.
// (Similar check from above, but for the case without headers.)
// TODO: widow prevention for non-repeated footers with a similar
// mechanism / when implementing multiple footers.
self.lrows.is_empty()
&& may_progress_with_offset(
self.regions,
// This header height isn't doing much as we just confirmed
// that there are no headers in this region, but let's keep
// it here for correctness. It will add zero anyway.
self.current.header_height + self.current.footer_height,
)
&& footer.start != 0
} else {
false
};
let mut laid_out_footer_start = None; let mut laid_out_footer_start = None;
if !footer_would_be_widow { if !footer_would_be_widow {
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer { if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
// Don't layout the footer if it would be alone with the header in // Don't layout the footer if it would be alone with the header
// the page (hence the widow check), and don't layout it twice. // in the page (hence the widow check), and don't layout it
// twice.
// TODO: this check can be replaced by a vector of repeating
// footers in the future.
if self.lrows.iter().all(|row| row.index() < footer.start) { if self.lrows.iter().all(|row| row.index() < footer.start) {
laid_out_footer_start = Some(footer.start); laid_out_footer_start = Some(footer.start);
self.layout_footer(footer, engine, self.finished.len())?; self.layout_footer(footer, engine, self.finished.len())?;