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,21 +1462,11 @@ 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
{
let last_header_end = last_header_row.index();
if self.grid.rows.len() > last_header_end
&& self
.grid
.footer
.as_ref()
.and_then(Repeatable::as_repeated)
.is_none_or(|footer| footer.start != last_header_end)
&& self.lrows.len() == self.current.repeated_header_rows
&& may_progress_with_offset( && may_progress_with_offset(
self.regions, self.regions,
// Since we're trying to find a region where to place all // Since we're trying to find a region where to place all
@ -1486,8 +1476,9 @@ impl<'a> GridLayouter<'a> {
self.current.header_height + self.current.footer_height, self.current.header_height + self.current.footer_height,
) )
{ {
// Header and footer would be alone in this region, but there are more // Header and footer would be alone in this region, but
// rows beyond the header and the footer. Push an empty region. // there are more rows beyond the headers and the footer.
// Push an empty region.
self.lrows.clear(); self.lrows.clear();
self.current.last_repeated_header_end = 0; self.current.last_repeated_header_end = 0;
self.current.repeated_header_rows = 0; self.current.repeated_header_rows = 0;
@ -1496,17 +1487,19 @@ impl<'a> GridLayouter<'a> {
false false
} }
} else if let Some(Repeatable::Repeated(footer)) = &self.grid.footer { } else if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
// If no rows other than the footer have been laid out so far, and // If no rows other than the footer have been laid out so far,
// there are rows beside the footer, then don't lay it out at all. // and there are rows beside the footer, then don't lay it out
// (Similar check from above, but for the case without headers.) // at all. (Similar check from above, but for the case without
// TODO: widow prevention for non-repeated footers with a similar // headers.)
// mechanism / when implementing multiple footers. // TODO: widow prevention for non-repeated footers with a
// similar mechanism / when implementing multiple footers.
self.lrows.is_empty() self.lrows.is_empty()
&& may_progress_with_offset( && may_progress_with_offset(
self.regions, self.regions,
// This header height isn't doing much as we just confirmed // This header height isn't doing much as we just
// that there are no headers in this region, but let's keep // confirmed that there are no headers in this region,
// it here for correctness. It will add zero anyway. // but let's keep it here for correctness. It will add
// zero anyway.
self.current.header_height + self.current.footer_height, self.current.header_height + self.current.footer_height,
) )
&& footer.start != 0 && footer.start != 0
@ -1517,8 +1510,11 @@ impl<'a> GridLayouter<'a> {
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())?;