use super::CellUnit;
use crate::model::table::Cell;
pub(super) fn for_next_unit(
cell: &Cell,
unit: &CellUnit,
unit_count: usize,
next_height: f64,
available: f64,
stored_tolerance: f64,
) -> f64 {
let composed_text = unit.nested_row.is_none()
&& !unit.mixed_nested_fragment
&& !unit.top_and_bottom_flow
&& cell.paragraphs.get(unit.para_idx).is_some_and(|para| {
para.controls.is_empty() && crate::renderer::para_has_no_stored_line_segs(para)
});
let operations = unit_count.saturating_add(1) as f64;
let tolerance = if composed_text {
(next_height.abs() + available.abs()) * f64::EPSILON * operations
} else {
stored_tolerance
};
#[cfg(not(target_arch = "wasm32"))]
{
static TRACE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if *TRACE.get_or_init(|| std::env::var_os("RHWP_DIAG_REFLOW_CAPACITY").is_some())
&& (next_height - available).abs() <= unit.height.abs()
{
let paragraph = cell.paragraphs.get(unit.para_idx);
eprintln!(
"DIAG_REFLOW_CAPACITY row={} col={} para={} no_ls={} controls={:?} nested={:?} mixed={} topbottom={} next={:.12} available={:.12} tolerance={:.12}",
cell.row,
cell.col,
unit.para_idx,
paragraph.is_some_and(crate::renderer::para_has_no_stored_line_segs),
paragraph.map(|para| para.controls.len()),
unit.nested_row,
unit.mixed_nested_fragment,
unit.top_and_bottom_flow,
next_height,
available,
tolerance,
);
}
}
tolerance
}