use super::{LayoutEngine, ResolvedStyleSet};
use crate::renderer::hwpunit_to_px;
#[derive(Debug, Clone, Copy)]
pub(crate) struct StraddleContinuationDemand {
pub(crate) content: f64,
pub(crate) physical: f64,
}
impl StraddleContinuationDemand {
pub(crate) fn required(self) -> f64 {
self.content.max(self.physical)
}
}
impl LayoutEngine {
#[allow(clippy::too_many_arguments)]
pub(crate) fn straddle_continuation_demand(
&self,
table: &crate::model::table::Table,
row: usize,
start_row: usize,
start_cut: &[usize],
start_cut_is_block: bool,
start_row_height_override: Option<f64>,
resolved_row_heights: &[f64],
straddle_page_bands: &[crate::renderer::pagination::StraddleCellCarry],
styles: &ResolvedStyleSet,
fragment_end: (usize, bool),
) -> Option<StraddleContinuationDemand> {
if !matches!(
table.layout_page_break(),
crate::model::table::TablePageBreak::RowBreak
| crate::model::table::TablePageBreak::CellBreak
) {
return None;
}
let (end_row, end_cut_is_empty) = fragment_end;
self.spanning_cells(table)
.filter(|cell| {
let cell_row = cell.row as usize;
let cell_end = cell_row + cell.row_span as usize;
let resumes_own_row = || {
super::super::table_partial::resumes_inside_own_start_row(
table,
cell,
start_row,
start_cut,
start_cut_is_block,
start_row_height_override,
straddle_page_bands,
)
};
cell.row_span > 1
&& cell_end > start_row
&& cell_end == row + 1
&& (cell_end < end_row || (cell_end == end_row && end_cut_is_empty))
&& (cell_row < start_row || resumes_own_row())
})
.map(|cell| {
let (su, eu) = self.rowbreak_straddle_cut_units(
table,
cell,
start_row,
end_row,
start_cut,
start_cut_is_block,
start_row_height_override,
&[],
false,
None,
0.0,
resolved_row_heights,
straddle_page_bands,
styles,
);
let content = self.cell_cut_visible_height(cell, table, styles, su, eu);
let carry = straddle_page_bands.iter().find(|carry| {
table
.cells
.get(carry.cell_index)
.is_some_and(|owner| std::ptr::eq(owner, cell))
});
let prefix = carry.and_then(|carry| carry.accepted_prefix_physical_height());
let physical = prefix.map_or(0.0, |before| {
let declared = if cell.height < 0x8000_0000 {
hwpunit_to_px(cell.height as i32, self.dpi)
} else {
0.0
};
let whole = self.cell_cut_visible_height(
cell,
table,
styles,
0,
self.cell_units(cell, table, styles).len(),
);
(declared.max(whole) - before).max(0.0)
});
super::unit_diagnostics::trace_continuation_demand(
cell,
(content, physical),
(
prefix,
carry.and_then(|carry| carry.accepted_physical_height),
),
carry.is_some_and(|carry| carry.fragment_cut.is_some()),
);
StraddleContinuationDemand { content, physical }
})
.reduce(|a, b| StraddleContinuationDemand {
content: a.content.max(b.content),
physical: a.physical.max(b.physical),
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn accepted_straddle_after_block(
&self,
table: &crate::model::table::Table,
resolved_row_heights: &[f64],
trailing_start: Option<usize>,
end_row: usize,
end_cut: &[usize],
is_continuation: bool,
end_row_height_override: Option<f64>,
straddle_row_relief: &[(usize, f64)],
carried: &[crate::renderer::pagination::StraddleCellCarry],
styles: &ResolvedStyleSet,
) -> Vec<crate::renderer::pagination::StraddleCellCarry> {
let mut accepted: Vec<_> = carried
.iter()
.cloned()
.map(|mut carry| {
carry.accepted_physical_height = None;
carry.owns_blank_band = false;
carry
})
.collect();
if let Some(start) = trailing_start {
let ordinary = self.accepted_straddle_fragment_units(
table,
resolved_row_heights,
start,
end_row,
&[],
end_cut,
is_continuation,
None,
end_row_height_override,
straddle_row_relief,
carried,
styles,
);
for owner in ordinary.into_iter().filter(|carry| {
table
.cells
.get(carry.cell_index)
.is_some_and(|cell| usize::from(cell.row) >= start)
}) {
accepted.retain(|carry| carry.cell_index != owner.cell_index);
accepted.push(owner);
}
}
accepted
}
pub(crate) fn straddle_page_bands_after_fragment(
table: &crate::model::table::Table,
carried_bands: &[crate::renderer::pagination::StraddleCellCarry],
next_row: usize,
band: f64,
owns_blank_band: bool,
) -> Vec<crate::renderer::pagination::StraddleCellCarry> {
table
.cells
.iter()
.enumerate()
.filter(|(index, cell)| {
let started_on_own_row = cell.row as usize == next_row
&& carried_bands.iter().any(|carry| {
carry.cell_index == *index
&& (carry.next_unit.is_some_and(|unit| unit > 0)
|| carry.fragment_cut.is_some_and(|(start, end)| end > start)
|| carry
.accepted_physical_height
.is_some_and(|height| height.is_finite() && height > 0.0))
});
cell.row_span > 1
&& ((cell.row as usize) < next_row || started_on_own_row)
&& next_row < cell.row as usize + cell.row_span as usize
})
.map(|(index, _)| {
let before = carried_bands
.iter()
.find(|carry| carry.cell_index == index)
.map_or(0.0, |carry| carry.physical_height);
let accepted = carried_bands.iter().find(|carry| carry.cell_index == index);
crate::renderer::pagination::StraddleCellCarry {
cell_index: index,
physical_height: before + band.max(0.0),
owns_blank_band: owns_blank_band
|| accepted.is_some_and(|carry| carry.owns_blank_band),
accepted_physical_height: accepted
.and_then(|carry| carry.accepted_physical_height),
accepted_before_fragment: None,
next_unit: accepted.and_then(|carry| {
carry.fragment_cut.map(|(_, end)| end).or(carry.next_unit)
}),
fragment_cut: None,
}
})
.collect()
}
}