use super::{CellUnit, LayoutEngine, ResolvedStyleSet, TablePageBreak};
use crate::model::shape::TextWrap;
use crate::renderer::hwpunit_to_px;
impl LayoutEngine {
pub(super) fn native_first_line_restart_cut_closes_frame(
&self,
table: &crate::model::table::Table,
cell: &crate::model::table::Cell,
units: &[CellUnit],
start_cut: usize,
end_cut: usize,
) -> bool {
let profile = self.profile.get();
let stored_span = cell.row_span > 1 && profile.hwpx_stored_layout();
if !(profile.hwp5_stored_pagination_layout() || stored_span)
|| profile.session_edited()
|| self
.render_normalization
.borrow()
.table_text_reflowed(table)
|| table.common.treat_as_char
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| !matches!(table.common.text_wrap, TextWrap::TopAndBottom)
|| start_cut != 0
|| end_cut != 1
|| end_cut >= units.len()
{
return false;
}
let closing = &units[0];
if closing.para_idx != 0 || closing.vis_start != 0 || closing.vis_end > 1 {
return false;
}
fn source_restart(cell: &crate::model::table::Cell) -> bool {
let mut lines = cell
.paragraphs
.iter()
.flat_map(|paragraph| paragraph.line_segs.iter().map(move |seg| (paragraph, seg)));
let (Some((first_para, first)), Some((next_para, next)), Some((after_para, after))) =
(lines.next(), lines.next(), lines.next())
else {
return false;
};
[first_para, next_para, after_para]
.iter()
.all(|para| para.controls.is_empty())
&& [first, next, after].iter().all(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
&& first.vertical_pos == 0
&& next.vertical_pos == 0
&& first.line_height > 0
&& next.line_height > 0
&& i64::from(after.vertical_pos)
== i64::from(next.line_height) + i64::from(next.line_spacing.max(0))
}
if !source_restart(cell) {
return false;
}
let native_shared_row = profile.hwp5_stored_pagination_layout();
let shared_restart = if cell.row_span > 1 && !native_shared_row {
let genuine_source = |owner: &crate::model::table::Cell| {
!owner.dirty_flag
&& owner.paragraphs.iter().all(|para| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.controls.is_empty()
})
&& source_restart(owner)
};
genuine_source(cell)
&& table.cells.iter().any(|other| {
other.row == cell.row
&& other.row_span == cell.row_span
&& other.col != cell.col
&& genuine_source(other)
})
} else {
let mut siblings = table
.cells
.iter()
.filter(|other| other.row == cell.row && other.col != cell.col)
.peekable();
let genuine_source = |owner: &crate::model::table::Cell| {
!owner.dirty_flag
&& owner.paragraphs.iter().all(|para| {
!para.stored_text_partition_is_dirty() && !para.cell_format_vpos_dirty
})
&& source_restart(owner)
};
genuine_source(cell) && siblings.peek().is_some() && siblings.all(genuine_source)
};
if !shared_restart {
return false;
}
let next = &units[end_cut];
if !(next.para_idx == 0 && next.vis_start == 1 || next.para_idx == 1 && next.vis_start == 0)
{
return false;
}
true
}
pub(super) fn native_first_line_restart_cut_trailing_trim(
&self,
table: &crate::model::table::Table,
cell: &crate::model::table::Cell,
units: &[CellUnit],
start_cut: usize,
end_cut: usize,
styles: &ResolvedStyleSet,
) -> f64 {
if !self.native_first_line_restart_cut_closes_frame(table, cell, units, start_cut, end_cut)
{
return 0.0;
}
let closing = &units[0];
let next = &units[end_cut];
let para = &cell.paragraphs[0];
let line_spacing = hwpunit_to_px(para.line_segs[0].line_spacing.max(0), self.dpi);
let after = if next.para_idx == closing.para_idx {
0.0
} else {
styles
.para_styles
.get(para.para_shape_id as usize)
.map(|style| style.spacing_after)
.unwrap_or(0.0)
.max(0.0)
};
(line_spacing + after).min(closing.height.max(0.0))
}
}