use super::{CellUnit, LayoutEngine, RowCut, RowCutResult};
use crate::renderer::style_resolver::ResolvedStyleSet;
pub(super) struct CellCutFrame<'a> {
engine: &'a LayoutEngine,
cell: &'a crate::model::table::Cell,
table: &'a crate::model::table::Table,
styles: &'a ResolvedStyleSet,
units: &'a [CellUnit],
start: usize,
padding: f64,
preserves_line_slots: bool,
source_page_frame: Option<crate::renderer::float_placement::StoredTextPageFrame>,
}
impl<'a> CellCutFrame<'a> {
pub(super) fn new(
engine: &'a LayoutEngine,
cell: &'a crate::model::table::Cell,
table: &'a crate::model::table::Table,
styles: &'a ResolvedStyleSet,
units: &'a [CellUnit],
start: usize,
) -> Self {
let (_, _, top, bottom) = engine.resolve_stored_nested_cell_padding(cell, table, styles);
let preserves_line_slots = engine.stored_span_cut_preserves_line_slots(table, cell);
let source_page_frame =
engine.stored_page_frame_for_unit(cell, table, units, start, top + bottom);
Self {
engine,
cell,
table,
styles,
units,
start,
padding: top + bottom,
preserves_line_slots,
source_page_frame,
}
}
pub(super) fn content_budget(&self, physical_budget: f64) -> f64 {
(physical_budget - self.padding - self.header_height(self.start + 1)).max(0.0)
}
fn header_height(&self, end: usize) -> f64 {
if end > self.start {
self.engine.nested_continuation_header_height(
self.cell,
self.units,
self.start,
self.styles,
)
} else {
0.0
}
}
pub(super) fn content_height(&self, end: usize, content_advance: f64) -> f64 {
let trim = self.engine.physical_cut_trailing_trim(
self.table,
self.cell,
self.units,
self.start,
end,
self.styles,
);
let following_advance = if end > self.start
&& end < self.units.len()
&& self
.engine
.stored_plain_text_span_frame(self.table, self.cell)
{
self.engine.stored_span_cut_closing_advance(
self.table,
self.cell,
self.units,
self.start,
end,
self.styles,
)
} else {
0.0
};
(content_advance - trim.max(following_advance)).max(0.0) + self.header_height(end)
}
pub(super) fn stored_content_height(&self, end: usize, content_advance: f64) -> Option<f64> {
let frame = self.source_page_frame?;
let last = self.units.get(end.checked_sub(1)?)?;
if end <= self.start
|| last.para_idx + 1 != frame.end_paragraph
|| self
.units
.get(end)
.is_some_and(|next| next.para_idx == last.para_idx)
{
return None;
}
Some(
self.content_height(end, content_advance)
.max(crate::renderer::hwpunit_to_px(
frame.content_height_hu,
self.engine.dpi,
)),
)
}
pub(super) fn height(&self, end: usize, content_advance: f64) -> f64 {
let physical = self.height_without_mixed_opening(end, content_advance);
self.engine
.stored_mixed_opening_frame(self.table, self.cell, self.styles, self.start, end)
.map_or(physical, |frame| frame.physical_height)
}
pub(super) fn height_without_mixed_opening(&self, end: usize, content_advance: f64) -> f64 {
let content = self
.stored_content_height(end, content_advance)
.unwrap_or_else(|| self.content_height(end, content_advance));
let physical = if content > 0.0 {
content + self.padding
} else {
0.0
};
if self.start == 0 {
if let Some((owner, opening)) =
self.engine
.first_source_frame_owner(self.table, self.cell, self.styles)
{
if end == owner {
return physical.max(opening);
}
}
}
physical
}
pub(super) fn fitting_height(&self, end: usize, content_advance: f64) -> f64 {
let physical = self.height(end, content_advance).max(
self.engine
.stored_complete_cell_slot_capacity(
self.cell, self.table, self.units, self.start, end,
)
.unwrap_or(0.0),
);
if self.preserves_line_slots && end > self.start && end < self.units.len() {
let following_advance = self.engine.stored_span_cut_closing_advance(
self.table,
self.cell,
self.units,
self.start,
end,
self.styles,
);
physical
.max(content_advance - following_advance + self.padding + self.header_height(end))
} else {
physical
}
}
}
impl LayoutEngine {
pub(in crate::renderer::layout) fn stored_cell_cut_alignment_height(
&self,
cell: &crate::model::table::Cell,
table: &crate::model::table::Table,
styles: &ResolvedStyleSet,
start: usize,
end: usize,
) -> Option<f64> {
let units = self.cell_units(cell, table, styles);
let start = start.min(units.len());
let end = end.clamp(start, units.len());
let advance = units[start..end].iter().map(|unit| unit.height).sum();
CellCutFrame::new(self, cell, table, styles, &units, start)
.stored_content_height(end, advance)
}
pub(crate) fn advance_row_block_cut_with_row_offsets(
&self,
table: &crate::model::table::Table,
b_start: usize,
b_end: usize,
start_cut: &[usize],
avail_height: f64,
row_offsets: &[f64],
styles: &ResolvedStyleSet,
) -> RowCutResult {
let mut cells = Self::row_block_cells(table, b_start, b_end);
cells.sort_by_key(|c| (c.row, c.col));
let mut end_cut: RowCut = Vec::with_capacity(cells.len());
let mut hit_hard_break = false;
let mut fully_consumed = true;
let mut consumed_height = 0.0f64;
let mut physical_height = 0.0f64;
const HARD_BREAK_REMAINING_TOLERANCE_PX: f64 = 32.0;
let block_has_top_and_bottom_flow = cells
.iter()
.any(|cell| self.cell_has_top_and_bottom_non_inline_flow(cell));
let relaxed_hard_break = matches!(
table.layout_page_break(),
crate::model::table::TablePageBreak::RowBreak
) && (table.col_count <= 2 || table.row_count > 5)
&& !block_has_top_and_bottom_flow;
for (i, cell) in cells.iter().enumerate() {
let units = self.cell_units(cell, table, styles);
let start = start_cut.get(i).copied().unwrap_or(0).min(units.len());
let cell_row = cell.row as usize;
let row_offset = cell_row
.checked_sub(b_start)
.and_then(|idx| row_offsets.get(idx))
.copied()
.unwrap_or(0.0);
let cell_budget = (avail_height - row_offset).max(0.0);
let frame = CellCutFrame::new(self, cell, table, styles, &units, start);
let content_budget = frame.content_budget(cell_budget);
let allow_force_progress = row_offset <= 0.5;
let mut j = start;
let mut h = 0.0f64;
const REWIND_ORPHAN_CONT_PX: f64 = 48.0;
const GIANT_CELL_RELAXED_MIN_UNITS: usize = 24;
let cell_relaxed = relaxed_hard_break && units.len() >= GIANT_CELL_RELAXED_MIN_UNITS;
while j < units.len() {
let u = &units[j];
let next_physical_height = frame.fitting_height(j + 1, h + u.height);
let roundoff = f64::EPSILON
* (avail_height.abs()
+ row_offset.abs()
+ next_physical_height.abs()
+ (h + u.height).abs())
.max(1.0)
* (j - start + cell_row.saturating_sub(b_start) + 2) as f64;
let exceeds_budget = next_physical_height > cell_budget
&& !(next_physical_height.is_finite()
&& cell_budget.is_finite()
&& next_physical_height - cell_budget <= roundoff);
if j > start
&& u.hard_break_before
&& (!cell_relaxed
|| (!u.empty_spacer
&& (exceeds_budget
|| cell_budget - frame.height(j, h)
<= HARD_BREAK_REMAINING_TOLERANCE_PX)))
{
if start > 0 && j == start + 1 && h <= REWIND_ORPHAN_CONT_PX {
h += u.height;
j += 1;
continue;
}
if !self.stored_paragraph_allows_orphan_split(table, cell, &units, j, styles) {
Self::rewind_rowbreak_orphan_before_hard_break(
table,
&units,
start,
content_budget,
true,
&mut j,
&mut h,
);
}
hit_hard_break = true;
break;
}
if j > start && exceeds_budget {
if Self::rewind_rowbreak_mixed_nested_table_tail_for_fresh_page(
table,
&units,
start,
self.current_body_area.get().3.max(content_budget),
&mut j,
&mut h,
) {
break;
}
if let Some((absorbed_h, absorbed_j)) =
Self::absorb_tail_before_stored_hard_break(&units, j, h, content_budget)
{
h = absorbed_h;
j = absorbed_j;
hit_hard_break = true;
break;
}
break;
}
if j == start && !allow_force_progress && exceeds_budget {
break;
}
if self.should_defer_overflowing_top_and_bottom_entry(
u,
j,
start,
h,
content_budget,
) {
break;
}
h += u.height;
j += 1;
}
let cell_physical_height = frame.height(j, h);
let minimum_cut_height = if self
.native_first_line_restart_cut_trailing_trim(table, cell, &units, start, j, styles)
> 0.0
{
cell_physical_height
} else {
h + frame.header_height(j)
};
if j < units.len() {
fully_consumed = false;
}
if minimum_cut_height > 0.0 {
consumed_height = consumed_height.max(row_offset + minimum_cut_height);
}
if cell_physical_height > 0.0 {
physical_height = physical_height.max(row_offset + cell_physical_height);
}
if std::env::var("RHWP_DIAG_BLKCUT").is_ok() {
let stop = if j >= units.len() {
"end"
} else if units[j].hard_break_before {
"hard"
} else {
"budget"
};
eprintln!(
"DIAG_BLKCUT(ofs) cell[{}] r={} c={} units={} start={} j={} h={:.1} physical_h={:.1} row_off={:.1} cell_budget={:.1} stop={} next_h={:.1}",
i,
cell.row,
cell.col,
units.len(),
start,
j,
h,
cell_physical_height,
row_offset,
cell_budget,
stop,
units.get(j).map(|u| u.height).unwrap_or(0.0)
);
}
end_cut.push(j);
}
RowCutResult {
end_cut,
hit_hard_break,
fully_consumed,
consumed_height,
physical_height: Some(physical_height),
}
}
}