use crate::model::table::{Cell, Table};
use crate::renderer::hwpunit_to_px;
use super::{CellUnit, LayoutEngine, ResolvedStyleSet};
impl LayoutEngine {
pub(super) fn stored_nested_row_has_in_paragraph_cut(
&self,
table: &Table,
cells: &[&Cell],
units: &[std::sync::Arc<Vec<CellUnit>>],
) -> bool {
let profile = self.profile.get();
if profile.session_edited()
|| !(profile.hwpx_stored_layout() || profile.hwp5_stored_pagination_layout())
{
return false;
}
cells.iter().zip(units).any(|(cell, units)| {
!self
.render_normalization
.borrow()
.cell_frame_reflowed(table, cell)
&& units.iter().any(|unit| {
self.stored_in_paragraph_unit_is_original_restart(table, cell, unit)
})
})
}
pub(super) fn stored_in_paragraph_unit_is_original_restart(
&self,
table: &Table,
cell: &Cell,
unit: &CellUnit,
) -> bool {
if cell.dirty_flag
|| !unit.page_frame_reset_before
|| unit.vis_start == 0
|| unit.vis_end <= unit.vis_start
{
return false;
}
let Some(paragraph) = cell.paragraphs.get(unit.para_idx) else {
return false;
};
if paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
|| !crate::renderer::cell_paragraph_has_only_text_metadata(paragraph)
{
return false;
}
let Some(previous) = paragraph.line_segs.get(unit.vis_start - 1) else {
return false;
};
let Some(current) = paragraph.line_segs.get(unit.vis_start) else {
return false;
};
let synthetic = crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
previous.tag & synthetic == 0
&& current.tag & synthetic == 0
&& (previous.vertical_pos > 0
|| (previous.vertical_pos == 0
&& unit.stored_frame_break_before
&& matches!(
self.stored_zero_origin_text_frame_break_witness(
table, cell, previous, current,
),
Some(super::stored_zero_origin::SavedZeroOriginWitness::OpeningFrame)
)))
&& current.vertical_pos == 0
&& previous.line_height > 0
&& current.line_height > 0
&& current.text_start > previous.text_start
}
pub(super) fn stored_first_cell_frame_height(&self, table: &Table, cell: &Cell) -> Option<f64> {
(cell.row_span == 1)
.then(|| self.source_first_cell_frame_height(table, cell))
.flatten()
}
fn source_first_cell_frame_height(&self, table: &Table, cell: &Cell) -> Option<f64> {
if !self.stored_plain_text_cell_frame(table, cell)
|| self
.render_normalization
.borrow()
.nested_table_has_reflowed_host(table)
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
{
return None;
}
let rows = table.get_raw_row_heights();
let row = usize::from(cell.row);
let prefix = rows.get(..row)?;
let span_end = row.checked_add(usize::from(cell.row_span))?;
let source_span = rows.get(row..span_end)?;
if prefix
.iter()
.chain(source_span)
.any(|&height| height == 0 || height > i32::MAX as u32)
{
return None;
}
let (origin, current) = self
.stored_frame_row_plans
.borrow()
.get(&(table as *const Table as usize))
.and_then(|rows| {
Some((
rows.get(..row)?.iter().sum::<f64>(),
rows.get(row..span_end)?.iter().sum::<f64>(),
))
})
.unwrap_or_else(|| {
(
prefix
.iter()
.map(|&height| hwpunit_to_px(height as i32, self.dpi))
.sum(),
source_span
.iter()
.map(|&height| hwpunit_to_px(height as i32, self.dpi))
.sum(),
)
});
let origin = origin + hwpunit_to_px(table.cell_spacing as i32, self.dpi) * row as f64;
let current = current
+ hwpunit_to_px(table.cell_spacing as i32, self.dpi)
* usize::from(cell.row_span).saturating_sub(1) as f64;
let end = hwpunit_to_px(table.common.height as i32, self.dpi);
if std::env::var_os("RHWP_DIAG_SCAN").is_some() {
eprintln!(
"DIAG_SCAN STORED_CELL_FRAME table={:p} row={} span={} origin={:.6} current={:.6} end={:.6} cached={}",
table,
row,
cell.row_span,
origin,
current,
end,
self.stored_frame_row_plans
.borrow()
.contains_key(&(table as *const Table as usize)),
);
}
if !origin.is_finite() || !current.is_finite() || end <= origin || end >= origin + current {
return None;
}
Some(end - origin)
}
pub(super) fn first_source_frame_owner(
&self,
table: &Table,
cell: &Cell,
styles: &ResolvedStyleSet,
) -> Option<(usize, f64)> {
let opening = self.source_first_cell_frame_height(table, cell)?;
let units = self.cell_units(cell, table, styles);
let (_, _, top, bottom) = self.resolve_cell_padding(cell, table);
let padding = top + bottom;
let epsilon = hwpunit_to_px(2, self.dpi);
let source_padding = cell.effective_padding(&table.padding);
let source_padding = hwpunit_to_px(
i32::from(source_padding.top) + i32::from(source_padding.bottom),
self.dpi,
);
if (source_padding - padding).abs() > epsilon {
return None;
}
let mut previous: Option<&crate::model::paragraph::LineSeg> = None;
let mut advance = 0.0;
for (index, unit) in units.iter().enumerate() {
let paragraph = cell.paragraphs.get(unit.para_idx)?;
let source_only_empty_line = unit.empty_spacer
&& unit.vis_start == 0
&& unit.vis_end == 0
&& paragraph.text.trim().is_empty()
&& paragraph.line_segs.len() == 1;
if unit.vis_end != unit.vis_start + 1 && !source_only_empty_line {
return None;
}
let line = paragraph.line_segs.get(unit.vis_start)?;
if let Some(before) = previous {
if line.vertical_pos == 0
&& (before.vertical_pos > 0
|| (unit.stored_frame_break_before
&& units[index - 1].para_idx == unit.para_idx
&& matches!(
self.stored_zero_origin_text_frame_break_witness(
table, cell, before, line,
),
Some(
super::stored_zero_origin::SavedZeroOriginWitness::OpeningFrame
)
)))
{
let ink = hwpunit_to_px(
before.vertical_pos.checked_add(before.line_height)?,
self.dpi,
) + padding;
let independently_closed_span = cell.row_span > 1
&& unit.stored_frame_break_before
&& unit.page_frame_reset_before
&& opening <= hwpunit_to_px(cell.height as i32, self.dpi)
&& table.cells.iter().any(|peer| {
peer.row_span == 1
&& peer.row >= cell.row
&& usize::from(peer.row)
< usize::from(cell.row) + usize::from(cell.row_span)
&& peer.col != cell.col
&& self.first_source_frame_owner(table, peer, styles).is_some()
});
let ordinary_source_restart = cell.row_span == 1
&& unit.page_frame_reset_before
&& units[index - 1].para_idx == unit.para_idx;
let accepted = opening + epsilon >= ink
&& (opening <= advance + padding + epsilon
|| independently_closed_span
|| ordinary_source_restart);
return accepted.then_some((index, opening));
}
if i64::from(line.vertical_pos)
!= i64::from(before.vertical_pos)
+ i64::from(before.line_height)
+ i64::from(before.line_spacing)
{
return None;
}
} else {
let before = styles
.para_styles
.get(cell.paragraphs.get(unit.para_idx)?.para_shape_id as usize)?
.spacing_before;
let origin = hwpunit_to_px(line.vertical_pos, self.dpi);
if origin < 0.0 || (origin != 0.0 && (origin - before).abs() > epsilon) {
return None;
}
}
let source_advance =
hwpunit_to_px(line.line_height.checked_add(line.line_spacing)?, self.dpi)
+ if previous.is_none() {
hwpunit_to_px(line.vertical_pos, self.dpi)
} else {
0.0
};
if (unit.height - source_advance).abs() > epsilon {
return None;
}
advance += unit.height;
previous = Some(line);
}
None
}
pub(crate) fn row_cut_first_source_frame_height(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> Option<f64> {
if !start_cut.is_empty() || end_cut.is_empty() {
return None;
}
let cells = Self::row_cut_cell_order(table, row);
if cells.len() != end_cut.len() {
return None;
}
let epsilon = hwpunit_to_px(2, self.dpi);
let mut frame: Option<f64> = None;
let mut completed_height = 0.0f64;
for (&index, &end) in cells.iter().zip(end_cut) {
let cell = &table.cells[index];
if !self.stored_plain_text_cell_frame(table, cell) {
return None;
}
let units = self.cell_units(cell, table, styles);
if units.is_empty() || end == 0 || end > units.len() {
return None;
}
if end < units.len() {
let (owner, opening) = self.first_source_frame_owner(table, cell, styles)?;
if owner != end || frame.is_some_and(|height| (height - opening).abs() > epsilon) {
return None;
}
frame = Some(opening);
} else {
let (_, _, top, bottom) = self.resolve_cell_padding(cell, table);
let height = units.iter().map(|unit| unit.height).sum::<f64>() + top + bottom;
completed_height = completed_height.max(height);
}
}
frame.filter(|height| completed_height <= *height)
}
pub(crate) fn completed_row_closes_opening_source_frame(
&self,
table: &Table,
row: usize,
end_cut: &[usize],
natural_height: f64,
available: f64,
styles: &ResolvedStyleSet,
) -> Option<f64> {
let order = Self::row_cut_cell_order(table, row);
if order.is_empty() || order.len() != end_cut.len() || !available.is_finite() {
return None;
}
let epsilon = hwpunit_to_px(2, self.dpi);
let mut frame: Option<f64> = None;
for (&index, &end) in order.iter().zip(end_cut) {
let cell = &table.cells[index];
let units = self.cell_units(cell, table, styles);
if units.is_empty() || end != units.len() {
return None;
}
let height = self.source_first_cell_frame_height(table, cell)?;
if (height - available).abs() > epsilon
|| frame.is_some_and(|before| (height - before).abs() > epsilon)
{
return None;
}
frame = Some(height);
}
let height = frame?;
let required =
self.stored_closing_row_occupancy(table, row, natural_height, height, styles)?;
(required <= height).then_some(height)
}
pub(super) fn stored_span_partial_first_frame_owner(
&self,
table: &Table,
cell: &Cell,
row: usize,
cut: &[usize],
measured_rows: &[f64],
styles: &ResolvedStyleSet,
) -> Option<usize> {
if cell.row_span <= 1 || usize::from(cell.row) != row || cut.is_empty() {
return None;
}
let (owner, opening) = self.first_source_frame_owner(table, cell, styles)?;
let epsilon = hwpunit_to_px(2, self.dpi);
let origin = measured_rows.get(..row)?.iter().sum::<f64>()
+ hwpunit_to_px(table.cell_spacing as i32, self.dpi) * row as f64;
if (origin + opening - hwpunit_to_px(table.common.height as i32, self.dpi)).abs() > epsilon
|| (self.row_cut_content_height(table, row, &[], cut, styles) - opening).abs() > epsilon
{
return None;
}
table
.cells
.iter()
.any(|peer| {
peer.row == cell.row
&& peer.col != cell.col
&& self
.first_source_frame_owner(table, peer, styles)
.is_some_and(|(_, height)| (height - opening).abs() <= epsilon)
})
.then_some(owner)
}
pub(crate) fn stored_first_frame_row_remainder(
&self,
table: &Table,
row: usize,
cut: &[usize],
measured_rows: &[f64],
styles: &ResolvedStyleSet,
) -> Option<f64> {
let mut cells: Vec<_> = table
.cells
.iter()
.filter(|cell| usize::from(cell.row) == row && cell.row_span == 1)
.collect();
cells.sort_by_key(|cell| cell.col);
if cells.len() != cut.len() || row >= measured_rows.len() {
return None;
}
let epsilon = hwpunit_to_px(2, self.dpi);
let mut opening: Option<f64> = None;
for (cell, &end) in cells.into_iter().zip(cut) {
let units = self.cell_units(cell, table, styles);
if end >= units.len() {
continue;
}
if end == 0 {
return None;
}
let height = self.stored_first_cell_frame_height(table, cell)?;
let before = units[..end - 1].iter().map(|unit| unit.height).sum();
if self.native_intra_para_saved_reset_trailing_trim(
table,
cell,
&units,
end,
before,
units[end - 1].height,
styles,
) <= 0.0
|| opening.is_some_and(|other| (other - height).abs() > epsilon)
{
return None;
}
opening = Some(height);
}
let opening = opening?;
let origin = measured_rows[..row].iter().sum::<f64>()
+ hwpunit_to_px(table.cell_spacing as i32, self.dpi) * row as f64;
let source_end = hwpunit_to_px(table.common.height as i32, self.dpi);
let accepted = self.row_cut_content_height(table, row, &[], cut, styles);
if (origin + opening - source_end).abs() > epsilon || (accepted - opening).abs() > epsilon {
return None;
}
let declared = table
.get_raw_row_heights()
.get(row)
.copied()
.filter(|&height| height > 0 && height <= i32::MAX as u32)
.map(|height| hwpunit_to_px(height as i32, self.dpi));
let physical = declared.map_or(measured_rows[row], |declared| {
declared.min(measured_rows[row])
});
let remaining = physical - opening;
let content = self.row_cut_content_height(table, row, cut, &[], styles);
(remaining > content + epsilon).then_some(remaining)
}
}