use super::{
stored_plain_text_paragraph_frame_reset, stored_text_paragraphs_share_vpos_frame, LayoutEngine,
ResolvedStyleSet,
};
use crate::model::paragraph::{LineSeg, Paragraph};
use crate::model::table::{Cell, Table, TablePageBreak};
impl LayoutEngine {
fn original_stored_table_frame_is_usable(&self, table: &Table, frame_reflowed: bool) -> bool {
let profile = self.profile.get();
(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
&& !profile.session_edited()
&& !table.common.treat_as_char
&& table.layout_page_break() == TablePageBreak::RowBreak
&& table.common.height > 0
&& !frame_reflowed
}
pub(crate) fn native_stored_table_has_fragment_frames(&self, table: &Table) -> bool {
self.original_stored_table_frame_is_usable(
table,
self.render_normalization
.borrow()
.table_text_reflowed(table),
) && self.indexed_stored_row_boxes_exceed_object_frame(table)
}
pub(crate) fn table_uses_stored_fragment_coordinates(&self, table: &Table) -> bool {
let profile = self.profile.get();
if !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| table.common.treat_as_char
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.common.height == 0
{
return false;
}
let source_geometry = |cell: &Cell| {
!cell.dirty_flag
&& !cell.paragraphs.is_empty()
&& cell.paragraphs.iter().all(|para| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& !crate::renderer::para_has_no_stored_line_segs(para)
&& para.line_segs.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
})
})
};
if Self::stored_row_boxes_exceed_object_frame(table)
&& table.cells.iter().all(source_geometry)
{
return true;
}
table.cells.iter().any(|cell| {
if cell.dirty_flag {
return false;
}
let mut previous = None;
for para in &cell.paragraphs {
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| crate::renderer::para_has_no_stored_line_segs(para)
{
previous = None;
continue;
}
for line in ¶.line_segs {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| line.line_height <= 0
{
previous = None;
continue;
}
if previous.is_some_and(|pos| line.vertical_pos < pos) {
return true;
}
previous = Some(line.vertical_pos);
}
}
false
})
}
pub(super) fn native_intra_paragraph_source_restart(
&self,
table: &Table,
cell: &Cell,
paragraph: usize,
line: usize,
) -> bool {
let Some(para) = cell.paragraphs.get(paragraph) else {
return false;
};
self.profile.get().hwp5_stored_pagination_layout()
&& self.original_stored_table_frame_is_usable(
table,
self.render_normalization
.borrow()
.cell_frame_reflowed(table, cell),
)
&& cell.row_span == 1
&& !cell.dirty_flag
&& source_paragraph(para)
&& line
.checked_sub(1)
.and_then(|index| para.line_segs.get(index))
.zip(para.line_segs.get(line))
.is_some_and(|(last, first)| source_line_restart(last, first))
}
pub(crate) fn native_stored_table_has_intra_paragraph_frames(
&self,
table: &Table,
styles: &ResolvedStyleSet,
) -> bool {
self.profile.get().hwp5_stored_pagination_layout()
&& self.original_stored_table_frame_is_usable(
table,
self.render_normalization
.borrow()
.table_text_reflowed(table),
)
&& table.cells.iter().any(|cell| {
if cell.row_span != 1 || cell.dirty_flag {
return false;
}
if !cell.paragraphs.iter().any(|paragraph| {
source_paragraph(paragraph)
&& paragraph
.line_segs
.windows(2)
.any(|lines| source_line_restart(&lines[0], &lines[1]))
}) {
return false;
}
let Some(prefix) = source_rows_before_hu(table, cell.row) else {
return false;
};
let padding = cell.effective_padding(&table.padding);
self.cell_units(cell, table, styles)
.windows(2)
.any(|units| {
let [before, after] = units else {
return false;
};
if !after.hard_break_before
|| !after.page_frame_reset_before
|| before.para_idx != after.para_idx
|| before.vis_end != after.vis_start
|| !self.native_intra_paragraph_source_restart(
table,
cell,
after.para_idx,
after.vis_start,
)
{
return false;
}
let last = &cell.paragraphs[before.para_idx].line_segs[before.vis_end - 1];
let end = prefix
+ i64::from(last.vertical_pos)
+ i64::from(last.line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom);
(end - i64::from(table.common.height)).abs() <= 2
})
})
}
pub(super) fn original_stored_text_frame_cell(
&self,
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
) -> bool {
cell.row_span == 1
&& !cell.dirty_flag
&& self.original_stored_table_frame_is_usable(
table,
self.render_normalization
.borrow()
.cell_frame_reflowed(table, cell),
)
&& stored_text_paragraphs_share_vpos_frame(cell, styles, self.dpi)
&& (self.indexed_stored_row_boxes_exceed_object_frame(table)
|| opening_text_frame_end_hu(cell, table, styles, self.dpi)
.is_some_and(|end| (end - i64::from(table.common.height)).abs() <= 2))
}
}
fn source_line_restart(previous: &LineSeg, current: &LineSeg) -> bool {
previous.vertical_pos > 0 && current.vertical_pos == 0
}
fn source_paragraph(paragraph: &Paragraph) -> bool {
paragraph.controls.is_empty()
&& !paragraph.stored_text_partition_is_dirty()
&& !paragraph.cell_format_vpos_dirty
&& !paragraph.line_segs.is_empty()
&& paragraph.line_segs.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
})
}
fn source_row_height_hu(cell: &Cell, table: &Table) -> Option<i64> {
if cell.dirty_flag || cell.height > i32::MAX as u32 {
return None;
}
let mut previous = None;
let mut bottom = 0_i64;
for paragraph in &cell.paragraphs {
if !source_paragraph(paragraph) {
return None;
}
for line in ¶graph.line_segs {
if previous.is_some_and(|vpos| line.vertical_pos < vpos) {
return None;
}
previous = Some(line.vertical_pos);
bottom = bottom.max(i64::from(line.vertical_pos) + i64::from(line.line_height));
}
}
previous?;
let padding = cell.effective_padding(&table.padding);
Some((bottom + i64::from(padding.top) + i64::from(padding.bottom)).max(i64::from(cell.height)))
}
fn source_rows_before_hu(table: &Table, end_row: u16) -> Option<i64> {
let mut prefix = 0_i64;
for row in 0..end_row {
if table.cells.iter().any(|peer| {
usize::from(peer.row) <= usize::from(row)
&& usize::from(row) < usize::from(peer.row) + usize::from(peer.row_span)
&& peer.row_span != 1
}) {
return None;
}
let mut row_height = None;
for peer in table.cells.iter().filter(|peer| peer.row == row) {
let height = source_row_height_hu(peer, table)?;
row_height = Some(row_height.unwrap_or(0_i64).max(height));
}
prefix += row_height? + i64::from(table.cell_spacing);
}
Some(prefix)
}
fn opening_text_frame_end_hu(
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<i64> {
if table.common.height > i32::MAX as u32 {
return None;
}
let mut previous: Option<&LineSeg> = None;
let mut closing = None;
'paragraphs: for (index, paragraph) in cell.paragraphs.iter().enumerate() {
if !source_paragraph(paragraph) {
return None;
}
for (line_index, line) in paragraph.line_segs.iter().enumerate() {
if let Some(before) = previous {
if line.vertical_pos < before.vertical_pos {
if line_index != 0
|| index == 0
|| !stored_plain_text_paragraph_frame_reset(
&cell.paragraphs[index - 1],
paragraph,
styles,
dpi,
)
{
return None;
}
closing = Some(before);
break 'paragraphs;
}
}
previous = Some(line);
}
}
let closing = closing?;
let prefix = source_rows_before_hu(table, cell.row)?;
let padding = cell.effective_padding(&table.padding);
Some(
prefix
+ i64::from(closing.vertical_pos)
+ i64::from(closing.line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom),
)
}