use super::{row_physical_band::CutHeight, CellUnit, LayoutEngine, ResolvedStyleSet};
use crate::model::{
control::Control,
table::{Cell, Table},
};
use crate::renderer::hwpunit_to_px;
pub(in crate::renderer::layout) struct ReflowHostCutOwnership {
pub(in crate::renderer::layout) owns_start: bool,
pub(in crate::renderer::layout) owns_end: bool,
}
impl ReflowHostCutOwnership {
pub(super) fn new(first: usize, last: usize, start: usize, end: usize) -> Option<Self> {
(start < last && end > first).then_some(Self {
owns_start: start <= first,
owns_end: end >= last,
})
}
}
impl LayoutEngine {
pub(in crate::renderer::layout) fn reflow_cut_opens_outer_frame(
cut: &super::NestedTableCut,
) -> bool {
!cut.is_block_split
&& !cut.start_cut_is_block
&& (cut.start_row > 0
|| cut
.start_cut
.iter()
.chain(&cut.end_cut)
.any(|&unit| unit > 0))
}
pub(in crate::renderer::layout) fn reflow_units_open_outer_frame(
units: &[CellUnit],
cut: Option<&super::NestedTableCut>,
) -> bool {
cut.is_some_and(Self::reflow_cut_opens_outer_frame)
|| units.first().into_iter().chain(units.last()).any(|unit| {
unit.nested_table_fragment.as_ref().is_some_and(|fragment| {
fragment
.start_cut
.iter()
.chain(&fragment.end_cut)
.any(|&consumed| consumed > 0)
})
})
}
pub(in crate::renderer::layout) fn reflow_host_cut_ownership(
units: &[CellUnit],
paragraph_index: usize,
start: usize,
end: usize,
) -> Option<ReflowHostCutOwnership> {
let first = units
.iter()
.position(|unit| unit.para_idx == paragraph_index)?;
let last = units
.iter()
.rposition(|unit| unit.para_idx == paragraph_index)?
+ 1;
ReflowHostCutOwnership::new(first, last, start, end)
}
pub(in crate::renderer::layout) fn reflow_partial_cell_content_height(
&self,
cell: &Cell,
table: &Table,
start: usize,
end: usize,
styles: &ResolvedStyleSet,
) -> Option<f64> {
if cell.row_span != 1 {
return None;
}
if self
.reflow_unrestricted_table_flow(table, styles)
.is_some_and(|flow| {
self.current_body_area.get().3 > 0.0
&& flow.parent_frame_height <= self.current_body_area.get().3
})
{
return Some(if start == 0 {
self.reflow_block_cell_flow_in_cell(cell, table, styles)?
.bottom
} else {
0.0
});
}
let units = self.cell_units(cell, table, styles);
let start = start.min(units.len());
let end = end.clamp(start, units.len());
self.reflow_cut_content_height(cell, table, &units, start, end, styles, CutHeight::Occupied)
}
pub(in crate::renderer::layout) fn reflow_partial_row_frame_height(
&self,
table: &Table,
row: usize,
start: &[usize],
end: &[usize],
styles: &ResolvedStyleSet,
) -> Option<f64> {
let cells = self.indexed_row_cut_cell_order(table, row);
if cells.is_empty() {
return None;
}
let mut height = 0.0_f64;
for (slot, index) in cells.into_iter().enumerate() {
let cell = &table.cells[index];
let content = self.reflow_partial_cell_content_height(
cell,
table,
start.get(slot).copied().unwrap_or(0),
end.get(slot).copied().unwrap_or(usize::MAX),
styles,
)?;
let (_, _, top, bottom) = self.resolve_stored_nested_cell_padding(cell, table, styles);
height = height.max(content + top + bottom);
}
Some(height)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn reflow_cut_content_height(
&self,
cell: &Cell,
table: &Table,
units: &[CellUnit],
start: usize,
end: usize,
styles: &ResolvedStyleSet,
purpose: CutHeight,
) -> Option<f64> {
if cell.text_direction != 0 {
return None;
}
if units
.get(start..end)?
.iter()
.any(|unit| unit.mixed_nested_fragment)
{
return None;
}
let flow = self.reflow_block_cell_flow_in_cell(cell, table, styles)?;
if flow
.origins
.iter()
.flatten()
.flatten()
.any(|o| o.host_relative)
{
return None;
}
if start == 0 && end == units.len() {
return Some(flow.bottom);
}
let selected = units.get(start..end)?;
if selected.is_empty() {
return Some(0.0);
}
let mut bounds = vec![None; cell.paragraphs.len()];
for (index, unit) in units.iter().enumerate() {
let slot = bounds.get_mut(unit.para_idx)?;
let (first, _) = slot.unwrap_or((index, index));
*slot = Some((first, index + 1));
}
let mut height = 0.0;
for (pi, para) in cell.paragraphs.iter().enumerate() {
let source = bounds[pi];
let own = source.map_or(&units[0..0], |(first, last)| {
let lo = first.max(start).min(last);
let hi = last.min(end).max(lo);
&units[lo..hi]
});
if own.is_empty() {
continue;
}
let children: Vec<_> = para
.controls
.iter()
.filter_map(|c| {
if let Control::Table(t) = c {
Some(t.as_ref())
} else {
None
}
})
.collect();
if children.is_empty() {
height += own.iter().map(|u| u.height).sum::<f64>();
continue;
}
let [child] = children.as_slice() else {
return None;
};
let (first, last) = source?;
let ReflowHostCutOwnership {
owns_start,
owns_end,
} = ReflowHostCutOwnership::new(first, last, start, end)?;
let recursive_cut = self
.nested_table_split_from_cut_units(cell, table, styles, start, end, pi)
.and_then(|s| s.recursive_cut);
let repeats_outer_frame =
Self::reflow_units_open_outer_frame(own, recursive_cut.as_ref());
let body = if let Some(cut) = recursive_cut.as_ref() {
self.nested_cut_height(child, cut, styles, purpose)
} else if let Some((start_row, end_row)) =
self.nested_row_range_from_cut_units(cell, table, styles, start, end, pi)
{
self.nested_cut_height(
child,
&super::NestedTableCut {
start_row,
end_row,
start_cut: Vec::new(),
end_cut: Vec::new(),
is_block_split: false,
start_cut_is_block: false,
},
styles,
purpose,
)
} else {
self.nested_table_body_height(child, styles)
};
let offset = if owns_start {
hwpunit_to_px(child.common.vertical_offset as i32, self.dpi)
} else {
0.0
};
let object = offset
+ body
+ hwpunit_to_px(
if owns_start || repeats_outer_frame {
i32::from(child.outer_margin_top)
} else {
0
} + if owns_end || repeats_outer_frame {
i32::from(child.outer_margin_bottom)
} else {
0
},
self.dpi,
);
let last_para = pi + 1 == cell.paragraphs.len();
let line = crate::renderer::float_placement::reflow_host_line_height(
para, styles, self.dpi, last_para,
)?;
let style = styles.para_styles.get(para.para_shape_id as usize);
let before = if owns_start {
crate::renderer::cell_paragraph_spacing::paragraph_before(
para,
pi,
style.map_or(0.0, |s| s.spacing_before),
self.dpi,
)
.occupied_before()
} else {
0.0
};
let after = if owns_end && !last_para {
style.map_or(0.0, |s| s.spacing_after)
} else {
0.0
};
height += before + line.max(object) + after;
}
Some(height)
}
}