use crate::model::paragraph::LineSeg;
use crate::model::table::{Cell, Table, TablePageBreak};
use crate::renderer::hwpunit_to_px;
use super::{unit_diagnostics, CellUnit, LayoutEngine, ResolvedStyleSet};
impl LayoutEngine {
#[allow(clippy::too_many_arguments)]
pub(crate) fn reserved_straddle_end_band(
&self,
table: &Table,
resolved_row_heights: &[f64],
start_row: usize,
end_row: usize,
start_cut: &[usize],
is_continuation: bool,
start_row_height_override: Option<f64>,
end_row_height_override: Option<f64>,
straddle_row_relief: &[(usize, f64)],
carried: &[crate::renderer::pagination::StraddleCellCarry],
styles: &ResolvedStyleSet,
) -> f64 {
let Some(accepted) = end_row_height_override else {
return 0.0;
};
let Some(last) = end_row.checked_sub(1).filter(|last| *last >= start_row) else {
return 0.0;
};
if !table.cells.iter().any(|cell| {
usize::from(cell.row) < end_row
&& end_row < usize::from(cell.row) + usize::from(cell.row_span)
}) {
return 0.0;
}
let natural = self.ordinary_rowbreak_fragment_heights(
table,
resolved_row_heights,
start_row,
end_row,
start_cut,
&[],
is_continuation,
start_row_height_override,
None,
straddle_row_relief,
carried,
true,
styles,
);
natural
.get(last)
.map_or(0.0, |height| (accepted - height).max(0.0))
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn rowbreak_straddle_cut_units(
&self,
table: &crate::model::table::Table,
cell: &crate::model::table::Cell,
start_row: usize,
end_row: usize,
start_cut: &[usize],
start_cut_is_block: bool,
start_row_height_override: Option<f64>,
end_cut: &[usize],
end_cut_is_block: bool,
end_row_height_override: Option<f64>,
cell_height: f64,
resolved_row_heights: &[f64],
straddle_page_bands: &[crate::renderer::pagination::StraddleCellCarry],
styles: &ResolvedStyleSet,
) -> (usize, usize) {
let cell_row = cell.row as usize;
let cell_end = cell_row + cell.row_span as usize;
let straddles_start = (cell_row < start_row && cell_end > start_row)
|| crate::renderer::layout::table_partial::resumes_inside_own_start_row(
table,
cell,
start_row,
start_cut,
start_cut_is_block,
start_row_height_override,
straddle_page_bands,
);
let straddles_end = crate::renderer::layout::table_partial::cell_crosses_fragment_end(
cell, end_row, end_cut,
) || (cell_row < end_row
&& cell_row + usize::from(cell.row_span) == end_row
&& end_row_height_override.is_some()
&& self.cell_uses_plain_reflow_units(cell, table));
let stored_opening_owner = self
.stored_span_opening_frame(table, cell_row)
.is_some_and(|frame| frame.restart_columns.contains(&cell.col));
if stored_opening_owner
&& straddles_end
&& end_row == cell_row + 1
&& end_cut.is_empty()
&& (start_row < cell_row
|| (start_row == cell_row
&& start_cut.is_empty()
&& start_row_height_override.is_none()))
{
return (0, 1);
}
let resumes_stored_opening = stored_opening_owner
&& straddles_start
&& start_row == cell_row + 1
&& start_cut.is_empty()
&& start_row_height_override.is_none();
let cell_spacing = hwpunit_to_px(table.cell_spacing as i32, self.dpi);
let padding = cell.effective_padding(&table.padding);
let pad_top = hwpunit_to_px(padding.top as i32, self.dpi);
let mut prior_h = 0.0;
if straddles_start {
let stored_physical_span = self.stored_plain_text_span_frame(table, cell);
for r in cell_row..start_row {
prior_h += self.rowbreak_prior_row_height(
table,
r,
resolved_row_heights,
styles,
stored_physical_span,
);
prior_h += cell_spacing;
}
if let Some(remaining_band) = start_row_height_override {
let physical_prefix = (resolved_row_heights.get(start_row).copied().unwrap_or(0.0)
- remaining_band)
.max(0.0);
let accepted_cut_prefix = if !start_cut_is_block && !start_cut.is_empty() {
self.row_cut_content_height(table, start_row, &[], start_cut, styles)
} else {
0.0
};
prior_h += physical_prefix.max(accepted_cut_prefix);
} else if !start_cut_is_block && !start_cut.is_empty() {
prior_h += self.row_cut_content_height(table, start_row, &[], start_cut, styles);
}
}
let whole_source_rows =
start_cut.is_empty() && start_row_height_override.is_none() && end_cut.is_empty();
let saved_start_owner = whole_source_rows
.then(|| self.stored_span_row_boundary_owner(table, cell, start_row, styles))
.flatten()
.or_else(|| {
(!start_cut_is_block)
.then(|| {
self.stored_span_partial_first_frame_owner(
table,
cell,
start_row,
start_cut,
resolved_row_heights,
styles,
)
})
.flatten()
})
.or_else(|| {
(!start_cut_is_block)
.then(|| {
self.stored_shared_span_cut_owner(table, cell, start_row, start_cut, styles)
})
.flatten()
});
if straddles_start {
if let Some(index) = table
.cells
.iter()
.position(|owner| std::ptr::eq(owner, cell))
{
if let Some(carry) = straddle_page_bands
.iter()
.find(|carry| carry.cell_index == index)
{
prior_h = carry
.accepted_physical_height
.filter(|_| carry.fragment_cut.is_none())
.unwrap_or(prior_h + carry.physical_height);
}
}
}
let accepted_start = table
.cells
.iter()
.position(|owner| std::ptr::eq(owner, cell))
.and_then(|index| {
straddle_page_bands
.iter()
.find(|carry| carry.cell_index == index)
})
.and_then(|carry| carry.next_unit);
let su = accepted_start.unwrap_or_else(|| {
if resumes_stored_opening {
1
} else if let Some(owner) = saved_start_owner {
owner
} else if prior_h > 0.0 {
let fit = self.cell_units_fitting_height(cell, table, styles, prior_h - pad_top);
self.snap_units_to_stored_cell_reset(cell, table, styles, 0, fit)
} else {
0
}
});
let eu = if straddles_end {
let owner = whole_source_rows
.then(|| self.stored_span_row_boundary_owner(table, cell, end_row, styles))
.flatten()
.or_else(|| {
(!end_cut_is_block)
.then(|| {
self.stored_span_partial_first_frame_owner(
table,
cell,
end_row.saturating_sub(1),
end_cut,
resolved_row_heights,
styles,
)
})
.flatten()
})
.or_else(|| {
(!end_cut_is_block)
.then(|| {
self.stored_shared_span_cut_owner(
table,
cell,
end_row.saturating_sub(1),
end_cut,
styles,
)
})
.flatten()
});
owner
.unwrap_or_else(|| {
let accepted_height = cell_height.max(0.0);
let units = self.cell_units(cell, table, styles);
let mut fit = su.min(units.len());
let frame = super::row_block_cut::CellCutFrame::new(
self, cell, table, styles, &units, fit,
);
let mut advance = 0.0;
while fit < units.len() {
let next = advance + units[fit].height;
if frame.fitting_height(fit + 1, next) > accepted_height + 0.1 {
break;
}
advance = next;
fit += 1;
}
fit
})
.max(su)
} else {
usize::MAX
};
unit_diagnostics::trace_span_cut(cell, start_row, end_row, prior_h, cell_height, su, eu);
(su, eu)
}
pub(in crate::renderer::layout) fn rowbreak_prior_row_height(
&self,
table: &Table,
row: usize,
resolved_row_heights: &[f64],
styles: &ResolvedStyleSet,
stored_physical_span: bool,
) -> f64 {
let physical = resolved_row_heights.get(row).copied().unwrap_or(0.0);
if stored_physical_span {
return physical;
}
let has_single_row_cells = table
.cells
.iter()
.any(|cell| cell.row as usize == row && cell.row_span == 1);
let content = if has_single_row_cells {
self.row_cut_content_height(table, row, &[], &[], styles)
} else {
0.0
};
if content > 0.0 {
content
} else {
physical
}
}
pub(in crate::renderer::layout) fn stored_plain_text_span_frame(
&self,
table: &Table,
cell: &Cell,
) -> bool {
cell.row_span > 1 && self.stored_plain_text_cell_frame(table, cell)
}
pub(in crate::renderer::layout) fn stored_span_cut_preserves_line_slots(
&self,
table: &Table,
cell: &Cell,
) -> bool {
cell.vertical_align != crate::model::table::VerticalAlign::Top
&& self.stored_plain_text_span_frame(table, cell)
}
pub(in crate::renderer::layout) fn current_program_span_owns_alignment(
&self,
table: &Table,
cell: &Cell,
) -> bool {
let profile = self.profile.get();
cell.row_span > 1
&& cell.vertical_align != crate::model::table::VerticalAlign::Top
&& (profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
&& !profile.session_edited()
&& !cell.dirty_flag
&& self
.render_normalization
.borrow()
.cell_has_current_render_partition(table, cell)
&& cell.paragraphs.iter().all(|para| {
para.controls.is_empty()
&& !para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
})
}
pub(in crate::renderer::layout) fn stored_span_cut_owns_alignment(
&self,
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
start: usize,
end: usize,
) -> bool {
if !self.stored_span_cut_preserves_line_slots(table, cell) {
return false;
}
let units = self.cell_units(cell, table, styles);
let Some(last) = end.checked_sub(1).and_then(|index| units.get(index)) else {
return false;
};
if start >= end {
return false;
}
for (index, unit) in units.iter().enumerate().skip(end) {
if unit.para_idx != last.para_idx {
break;
}
if self.stored_in_paragraph_unit_is_original_restart(table, cell, unit) {
return index == end;
}
}
true
}
pub(super) fn stored_plain_text_cell_frame(&self, table: &Table, cell: &Cell) -> bool {
self.stored_body_text_cell_frame(table, cell)
&& cell
.paragraphs
.iter()
.all(crate::renderer::cell_paragraph_has_only_text_metadata)
}
pub(super) fn stored_body_text_cell_frame(&self, table: &Table, cell: &Cell) -> bool {
self.stored_cell_content_frame(table, cell, false)
}
pub(super) fn stored_inline_content_cell_frame(&self, table: &Table, cell: &Cell) -> bool {
self.stored_cell_content_frame(table, cell, true)
}
fn stored_cell_content_frame(&self, table: &Table, cell: &Cell, inline_objects: bool) -> bool {
let profile = self.profile.get();
(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
&& !profile.session_edited()
&& !self
.render_normalization
.borrow()
.cell_frame_reflowed(table, cell)
&& !table.common.treat_as_char
&& table.layout_page_break() == TablePageBreak::RowBreak
&& !cell.dirty_flag
&& !cell.paragraphs.is_empty()
&& cell.paragraphs.iter().all(|para| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.controls.iter().all(|control| {
matches!(
control,
crate::model::control::Control::Header(_)
| crate::model::control::Control::Footer(_)
) || crate::renderer::cell_control_is_text_metadata(control)
|| (inline_objects
&& control.is_treat_as_char_object()
&& !matches!(control, crate::model::control::Control::Table(_)))
})
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
&& line.line_spacing >= 0
})
})
}
pub(super) fn stored_span_cut_closing_advance(
&self,
table: &Table,
cell: &Cell,
units: &[CellUnit],
start: usize,
end: usize,
styles: &ResolvedStyleSet,
) -> f64 {
let Some(unit) = end.checked_sub(1).and_then(|index| units.get(index)) else {
return 0.0;
};
if start >= end || end >= units.len() {
return 0.0;
}
if self.stored_span_cut_preserves_line_slots(table, cell) && !unit.reflowed_line_slot {
if unit.para_idx == units[end].para_idx
|| units[start + 1..end]
.iter()
.any(|owned| owned.hard_break_before && !owned.reflowed_line_slot)
{
0.0
} else {
self.row_cut_capacity_trailing_trim(table, cell, units, start, end, styles)
}
} else {
self.stored_span_line_tail_spacing(unit)
}
}
pub(super) fn stored_span_line_tail_spacing(&self, unit: &CellUnit) -> f64 {
if unit.vis_start >= unit.vis_end
|| unit.nested_row.is_some()
|| unit.mixed_nested_fragment
|| unit.non_inline_control_range.is_some()
{
return 0.0;
}
unit.following_line_advance.min(unit.height.max(0.0))
}
pub(super) fn stored_spanning_cut_closing_advance(
&self,
cell: &crate::model::table::Cell,
table: &crate::model::table::Table,
units: &[CellUnit],
end: usize,
) -> f64 {
let profile = self.profile.get();
if !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| profile.session_edited()
|| cell.row_span <= 1
|| cell.dirty_flag
|| self
.render_normalization
.borrow()
.table_text_reflowed(table)
|| end == 0
|| end >= units.len()
{
return 0.0;
}
let unit = &units[end - 1];
let Some(para) = cell.paragraphs.get(unit.para_idx) else {
return 0.0;
};
if !para.controls.is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| unit.vis_end != unit.vis_start + 1
{
return 0.0;
}
let Some(line) = para.line_segs.get(unit.vis_start) else {
return 0.0;
};
if line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
{
return 0.0;
}
hwpunit_to_px(line.line_spacing, self.dpi)
.min((unit.height - hwpunit_to_px(line.line_height, self.dpi)).max(0.0))
}
}