use crate::model::{
control::Control,
paragraph::{ColumnBreakType, LineSeg, Paragraph},
table::Table,
};
use crate::renderer::float_placement::ParagraphFloatPlacement;
use crate::renderer::hwpunit_to_px;
pub(crate) fn stored_rows_close_at_successor(
rows: &[crate::model::HwpUnit],
cell_spacing: i16,
first_fragment: u32,
successor_top: i32,
) -> bool {
rows.len() >= 2
&& first_fragment > 0
&& first_fragment <= i32::MAX as u32
&& successor_top > 0
&& rows
.iter()
.all(|&height| height > 0 && height <= i32::MAX as u32)
&& rows.iter().map(|&height| i64::from(height)).sum::<i64>()
+ i64::from(cell_spacing) * (rows.len() - 1) as i64
== i64::from(first_fragment) + i64::from(successor_top)
}
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
})
})
{
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)
}
fn clean_single_saved_line(para: &Paragraph) -> Option<&LineSeg> {
let [line] = para.line_segs.as_slice() else {
return None;
};
(!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.column_type == ColumnBreakType::None
&& line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.text_start == 0
&& line.vertical_pos >= 0
&& line.text_height > 0
&& line.text_height <= line.line_height
&& line.segment_width > 0
&& para
.source_line_seg_vertical_pos
.as_ref()
.is_none_or(|source| source.as_slice() == [line.vertical_pos]))
.then_some(line)
}
fn saved_single_line_ladder_end(paragraphs: &[Paragraph]) -> Option<i64> {
let mut end = None;
for paragraph in paragraphs {
let line = clean_single_saved_line(paragraph)?;
let top = i64::from(line.vertical_pos);
if end.is_some_and(|previous| top < previous) {
return None;
}
end = Some(top + i64::from(line.line_height));
}
end
}
fn stored_inline_tail_table_fits_line(para: &Paragraph) -> bool {
let [Control::Table(child)] = para.controls.as_slice() else {
return false;
};
let Some(line) = clean_single_saved_line(para) else {
return false;
};
let height = i64::from(child.common.height);
if !para.text.is_empty()
|| !child.common.treat_as_char
|| !child.common.flow_with_text
|| child.caption.is_some()
|| child.row_count != 1
|| child.cells.is_empty()
|| child.cells.len() != usize::from(child.col_count)
|| child.cell_spacing < 0
|| child.common.width > i32::MAX as u32
|| height == 0
|| [
child.outer_margin_left,
child.outer_margin_right,
child.outer_margin_top,
child.outer_margin_bottom,
]
.iter()
.any(|&margin| margin < 0)
|| i64::from(child.common.width)
+ i64::from(child.outer_margin_left)
+ i64::from(child.outer_margin_right)
> i64::from(line.segment_width)
|| height + i64::from(child.outer_margin_top) + i64::from(child.outer_margin_bottom)
!= i64::from(line.line_height)
{
return false;
}
let mut width = u64::from(child.cell_spacing as u16) * (child.cells.len() - 1) as u64;
for (col, cell) in child.cells.iter().enumerate() {
let Some(content_end) = saved_single_line_ladder_end(&cell.paragraphs) else {
return false;
};
let padding = cell.effective_padding(&child.padding);
let inside_width =
i64::from(cell.width) - i64::from(padding.left) - i64::from(padding.right);
if cell.dirty_flag
|| cell.row != 0
|| usize::from(cell.col) != col
|| cell.row_span != 1
|| cell.col_span != 1
|| cell.width == 0
|| cell.height > child.common.height
|| [padding.left, padding.right, padding.top, padding.bottom]
.iter()
.any(|&value| value < 0)
|| cell.paragraphs.iter().any(|paragraph| {
let line = ¶graph.line_segs[0];
!paragraph.controls.is_empty()
|| line.column_start < 0
|| i64::from(line.column_start) + i64::from(line.segment_width) > inside_width
})
|| content_end + i64::from(padding.top) + i64::from(padding.bottom) > height
{
return false;
}
width += u64::from(cell.width);
}
width == u64::from(child.common.width)
}
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;
let mut has_inline_tail_table = false;
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)
{
return None;
}
let mut cell_has_inline_tail_table = false;
for paragraph in &cell.paragraphs {
if !paragraph
.controls
.iter()
.any(|control| matches!(control, Control::Table(_)))
{
continue;
}
if cell.dirty_flag || !stored_inline_tail_table_fits_line(paragraph) {
return None;
}
has_inline_tail_table = true;
cell_has_inline_tail_table = true;
}
let content_hu = if cell_has_inline_tail_table {
saved_single_line_ladder_end(&cell.paragraphs)?
} else {
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(i32::try_from(content_hu).ok()?, dpi) + pad;
if floor > content_floor {
content_floor = floor;
}
}
if content_floor <= 0.0
|| (has_inline_tail_table
&& table.cells.iter().any(|cell| {
cell.row_span > 1 && usize::from(cell.row) + usize::from(cell.row_span) > last_row
}))
{
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)
}