use crate::model::{
control::Control,
paragraph::{LineSeg, Paragraph},
table::Table,
};
use crate::renderer::float_placement::ParagraphFloatPlacement;
use crate::renderer::hwpunit_to_px;
pub(crate) fn project_first_rowspan_frame(
table: &Table,
rows: &mut [f64],
content_floors: &[f64],
span_content_floors: &[(usize, f64)],
dpi: f64,
) -> bool {
if table.common.treat_as_char
|| table.page_break != crate::model::table::TablePageBreak::RowBreak
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| rows.len() < 2
|| rows.len() != usize::from(table.row_count)
|| rows.len() != content_floors.len()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !table.cells.iter().any(|cell| cell.row_span > 1)
{
return false;
}
let declared = hwpunit_to_px(table.common.height as i32, dpi);
let spacing = hwpunit_to_px(i32::from(table.cell_spacing), dpi);
let mut origin = spacing;
let Some((closing_row, remaining)) = rows.iter().enumerate().find_map(|(row, &height)| {
let end = origin + height + spacing;
let result =
(origin < declared && declared < end).then_some((row, declared - origin - spacing));
origin = end;
result
}) else {
return false;
};
if remaining < content_floors[closing_row] || remaining <= 0.0 || remaining >= rows[closing_row]
{
return false;
}
let mut witnessed_rewind = false;
let mut carry_row = rows.len();
for cell in &table.cells {
let start = usize::from(cell.row);
let end = start + usize::from(cell.row_span);
if start > closing_row || end <= closing_row + 1 || end > rows.len() {
continue;
}
carry_row = carry_row.min(end - 1);
let mut previous = None;
for segment in cell
.paragraphs
.iter()
.flat_map(|paragraph| ¶graph.line_segs)
{
if segment.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| segment.vertical_pos < 0
|| segment.line_height <= 0
{
return false;
}
witnessed_rewind |= previous.is_some_and(|position| segment.vertical_pos < position);
previous = Some(segment.vertical_pos);
}
}
if !witnessed_rewind || carry_row >= rows.len() {
return false;
}
let carried = rows[closing_row] - remaining;
if span_content_floors.iter().any(|&(index, floor)| {
let Some(cell) = table.cells.get(index) else {
return true;
};
let start = usize::from(cell.row);
let end = start + usize::from(cell.row_span);
start <= closing_row
&& end == closing_row + 1
&& rows[start..end].iter().sum::<f64>() - carried < floor
}) || table.cells.iter().any(|cell| {
cell.dirty_flag
|| cell.paragraphs.iter().any(|paragraph| {
paragraph.stored_text_partition_is_dirty() || paragraph.cell_format_vpos_dirty
})
}) {
return false;
}
rows[closing_row] = remaining;
rows[carry_row] += carried;
true
}
pub(crate) fn stored_rows_own_body(table: &Table, rows: &[f64], dpi: f64) -> bool {
if !dpi.is_finite()
|| dpi <= 0.0
|| table.common.treat_as_char
|| table.common.size_protect
|| rows.len() < 2
|| rows.len() != usize::from(table.row_count)
|| table.cells.is_empty()
|| table.cells.iter().any(|cell| {
cell.row_span != 1
|| cell.height == 0
|| cell.height > i32::MAX as u32
|| cell.dirty_flag
|| cell.paragraphs.is_empty()
|| cell.paragraphs.iter().any(|para| {
para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| crate::renderer::para_has_no_stored_line_segs(para)
})
})
{
return false;
}
let unit = dpi / 7200.0;
let mut declared_rows: Vec<Option<u32>> = vec![None; rows.len()];
for cell in &table.cells {
if let Some(declared) = declared_rows.get_mut(usize::from(cell.row)) {
*declared = Some(declared.map_or(cell.height, |height| height.max(cell.height)));
}
}
rows.iter().zip(&declared_rows).all(|(measured, declared)| {
let declared = declared.map(|height| hwpunit_to_px(height as i32, dpi));
declared.is_some_and(|height| {
measured.is_finite()
&& (measured - height).abs()
<= unit * 0.5 + (measured.abs() + height.abs()) * f64::EPSILON
})
})
}
pub(crate) fn normalized_empty_control_frame(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
rows: &[f64],
flow_origin: f64,
dpi: f64,
hwpx_object_frame: bool,
) -> Option<ParagraphFloatPlacement> {
if !stored_rows_own_body(table, rows, dpi)
|| table.caption.is_some()
|| !flow_origin.is_finite()
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
{
return None;
}
let anchor = host.line_segs.first()?;
let source_origin = host
.source_line_seg_vertical_pos
.as_ref()
.and_then(|positions| positions.first().copied())
.unwrap_or(anchor.vertical_pos);
let declared = hwpunit_to_px(table.common.height as i32, dpi);
let body = rows.iter().sum::<f64>()
+ hwpunit_to_px(i32::from(table.cell_spacing), dpi) * rows.len().saturating_sub(1) as f64;
if body >= declared {
return None;
}
let mut frame = crate::renderer::float_placement::stored_empty_control_table_frame(
host,
successor,
table,
declared,
0.0,
source_origin,
dpi,
hwpx_object_frame,
)?;
let change = body - declared;
frame.anchor_y += flow_origin;
frame.table_top += flow_origin;
frame.occupied_bottom += flow_origin + change;
frame.stored_host_origin = frame.stored_host_origin.map(|origin| origin + flow_origin);
frame.stored_successor_line_origin = frame
.stored_successor_line_origin
.map(|origin| origin + flow_origin + change);
Some(frame)
}
pub(crate) fn declared_tail_row_height(
table: &Table,
row_heights: &[f64],
cell_spacing: f64,
dpi: f64,
) -> Option<f64> {
let row_count = row_heights.len();
if row_count < 2 || table.row_count as usize != row_count || table.common.height == 0 {
return None;
}
let last_row = row_count - 1;
let has_stored_page_rewind = table.cells.iter().any(|cell| {
let mut previous: Option<i32> = None;
cell.paragraphs
.iter()
.flat_map(|paragraph| paragraph.line_segs.iter())
.filter(|seg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
.any(|seg| {
let rewinds = previous.is_some_and(|vpos| seg.vertical_pos < vpos);
previous = Some(seg.vertical_pos);
rewinds
})
});
if has_stored_page_rewind {
return None;
}
let declared_tail = table
.cells
.iter()
.filter(|cell| {
cell.row as usize == last_row && cell.row_span == 1 && cell.height < 0x8000_0000
})
.map(|cell| hwpunit_to_px(cell.height as i32, dpi))
.fold(0.0f64, f64::max);
if declared_tail <= 0.0 || (row_heights[last_row] - declared_tail).abs() > 0.5 {
return None;
}
let mut content_floor = 0.0f64;
for cell in &table.cells {
if cell.row as usize != last_row || cell.row_span != 1 {
continue;
}
if cell
.paragraphs
.iter()
.any(crate::renderer::para_has_no_stored_line_segs)
|| cell.paragraphs.iter().any(|paragraph| {
paragraph
.controls
.iter()
.any(|control| matches!(control, Control::Table(_)))
})
{
return None;
}
let content_hu = cell
.paragraphs
.iter()
.flat_map(|paragraph| paragraph.line_segs.iter())
.map(|seg| i64::from(seg.vertical_pos) + i64::from(seg.line_height))
.max()
.unwrap_or(0);
let pad = hwpunit_to_px(cell.effective_vertical_padding_hu(&table.padding), dpi);
let floor = hwpunit_to_px(content_hu as i32, dpi) + pad;
if floor > content_floor {
content_floor = floor;
}
}
if content_floor <= 0.0 {
return None;
}
let spacing_total = cell_spacing * row_count.saturating_sub(1) as f64;
let target_body_height = hwpunit_to_px(table.common.height as i32, dpi);
let target_row_sum = (target_body_height - spacing_total).max(0.0);
let current_row_sum = row_heights.iter().sum::<f64>();
let reduction = current_row_sum - target_row_sum;
if reduction <= 0.5 {
return None;
}
let target_tail = row_heights[last_row] - reduction;
if target_tail < content_floor - 0.5 {
return None;
}
Some(target_tail)
}