use super::{CellUnit, LayoutEngine, ResolvedStyleSet};
use crate::model::paragraph::LineSeg;
use crate::model::table::{Cell, Table, VerticalAlign};
use crate::renderer::float_placement::{stored_text_page_frame, StoredTextPageFrame};
use crate::renderer::hwpunit_to_px;
impl LayoutEngine {
pub(super) fn stored_measured_object_frame_row_count(&self, table: &Table) -> Option<usize> {
let profile = self.profile.get();
let normalization = self.render_normalization.borrow();
if !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| profile.session_edited()
|| table.common.treat_as_char
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| table.layout_page_break() != crate::model::table::TablePageBreak::RowBreak
|| !self.indexed_stored_row_boxes_exceed_object_frame(table)
|| normalization.table_frame_reflowed_without_cell_scope(table)
|| normalization.nested_table_has_reflowed_host(table)
{
return None;
}
drop(normalization);
let rows = self.stored_frame_row_plans.borrow();
let rows = rows.get(&(table as *const Table as usize))?;
let mut prefix = 0.0;
let declared = hwpunit_to_px(table.common.height as i32, self.dpi);
let spacing = hwpunit_to_px(table.cell_spacing as i32, self.dpi);
for (index, height) in rows.iter().enumerate().take(rows.len().saturating_sub(1)) {
prefix += *height + if index > 0 { spacing } else { 0.0 };
if prefix.to_bits() != declared.to_bits() {
continue;
}
let boundary = index + 1;
let raw_rows = table.get_raw_row_heights();
if raw_rows.get(..boundary).is_some_and(|prefix| {
prefix
.iter()
.all(|&height| height > 0 && height <= i32::MAX as u32)
&& prefix.iter().map(|&height| i64::from(height)).sum::<i64>()
+ i64::from(table.cell_spacing) * index as i64
>= i64::from(table.common.height)
}) {
return None;
}
let original = table
.cells
.iter()
.filter(|cell| usize::from(cell.row) < boundary)
.all(|cell| {
usize::from(cell.row) + usize::from(cell.row_span.max(1)) <= boundary
&& self.stored_plain_text_cell_frame(table, cell)
});
return original.then_some(boundary);
}
None
}
pub(super) fn preserve_stored_blank_opening_frame(
&self,
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
units: &mut Vec<CellUnit>,
) {
let Some((opening, remainder)) =
self.stored_blank_opening_frame_heights(cell, table, styles, units)
else {
return;
};
let mut tail = units[0].clone();
units[0].height = opening;
units[0].following_line_advance = 0.0;
tail.height = remainder;
tail.vis_start = tail.vis_end;
tail.empty_spacer = true;
tail.following_line_advance = 0.0;
tail.hard_break_before = true;
tail.stored_frame_break_before = true;
tail.page_frame_reset_before = true;
units.push(tail);
}
fn stored_blank_opening_frame_heights(
&self,
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
units: &[CellUnit],
) -> Option<(f64, f64)> {
let [unit] = units else { return None };
let [paragraph] = cell.paragraphs.as_slice() else {
return None;
};
let [line] = paragraph.line_segs.as_slice() else {
return None;
};
let row = usize::from(cell.row);
if !self.native_stored_table_has_fragment_frames(table)
|| table.common.height >= 0x8000_0000
|| cell.dirty_flag
|| cell.row_span != 1
|| cell.vertical_align != VerticalAlign::Top
|| cell.text_direction != 0
|| paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos != 0
|| line.line_height <= 0
|| line.line_spacing < 0
|| unit.nested_row.is_some()
|| unit.vis_start != 0
|| unit.vis_end != 1
{
return None;
}
let cached_rows = {
self.stored_frame_row_plans
.borrow()
.get(&(table as *const Table as usize))
.cloned()
};
let has_independent_object = || {
table.cells.iter().any(|other| {
other.paragraphs.iter().any(|paragraph| {
paragraph.controls.iter().any(|control| {
!crate::renderer::composer::control_is_width_neutral_marker(control)
})
})
})
};
if cached_rows.is_none() && has_independent_object() {
return None;
}
let rows = cached_rows.unwrap_or_else(|| {
self.resolve_row_heights(
table,
usize::from(table.col_count),
usize::from(table.row_count),
None,
styles,
true,
)
});
let height = *rows.get(row)?;
let before = rows[..row].iter().sum::<f64>()
+ hwpunit_to_px(i32::from(table.cell_spacing), self.dpi) * row as f64;
let opening = hwpunit_to_px(table.common.height as i32, self.dpi) - before;
let epsilon = hwpunit_to_px(2, self.dpi);
let (_, _, top, bottom) = self.resolve_stored_nested_cell_padding(cell, table, styles);
let content_opening = opening - top - bottom;
let remainder = height - opening;
if !height.is_finite()
|| content_opening <= unit.height + epsilon
|| remainder <= epsilon
|| self
.render_normalization
.borrow()
.row_frame_reflowed(table, row)
{
return None;
}
if has_independent_object()
|| table.cells.iter().any(|other| {
let first = usize::from(other.row);
other.row_span > 1 && first <= row && row < first + usize::from(other.row_span)
})
{
return None;
}
for peer in table.cells.iter().filter(|peer| peer.row == cell.row) {
let [p] = peer.paragraphs.as_slice() else {
return None;
};
let [s] = p.line_segs.as_slice() else {
return None;
};
let padding = peer.effective_padding(&table.padding);
let end = hwpunit_to_px(
s.line_height
.checked_add(i32::from(padding.top))?
.checked_add(i32::from(padding.bottom))?,
self.dpi,
);
if peer.dirty_flag
|| peer.text_direction != 0
|| peer.vertical_align != VerticalAlign::Top
|| p.stored_text_partition_is_dirty()
|| p.cell_format_vpos_dirty
|| s.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| s.vertical_pos != 0
|| s.line_height <= 0
|| end >= opening - epsilon
{
return None;
}
}
Some((content_opening, remainder))
}
pub(super) fn stored_page_frame_for_unit(
&self,
cell: &Cell,
table: &Table,
units: &[CellUnit],
start: usize,
padding: f64,
) -> Option<StoredTextPageFrame> {
if !self.stored_span_cut_preserves_line_slots(table, cell)
|| (hwpunit_to_px(cell.effective_vertical_padding_hu(&table.padding), self.dpi)
- padding)
.abs()
> hwpunit_to_px(1, self.dpi)
{
return None;
}
let first = units.get(start)?;
if first.vis_start != 0 || start > 0 && units[start - 1].para_idx == first.para_idx {
return None;
}
stored_text_page_frame(cell, table, first.para_idx)
}
pub(super) fn preserve_source_page_frame_extents(
&self,
cell: &Cell,
table: &Table,
styles: &ResolvedStyleSet,
units: &mut [CellUnit],
) {
if !self.stored_span_cut_preserves_line_slots(table, cell) {
return;
}
let (_, _, top, bottom) = self.resolve_stored_nested_cell_padding(cell, table, styles);
for start in 0..units.len() {
let Some(frame) =
self.stored_page_frame_for_unit(cell, table, units, start, top + bottom)
else {
continue;
};
let end = units[start..]
.iter()
.position(|unit| unit.para_idx >= frame.end_paragraph)
.map_or(units.len(), |relative| start + relative);
if end <= start || units[end - 1].para_idx + 1 != frame.end_paragraph {
continue;
}
let advance = units[start..end]
.iter()
.map(|unit| unit.height)
.sum::<f64>()
- self.stored_span_line_tail_spacing(&units[end - 1]);
let required = hwpunit_to_px(frame.content_height_hu, self.dpi);
if required > advance {
units[end - 1].height += required - advance;
}
}
}
}