use crate::model::paragraph::{LineSeg, Paragraph};
use crate::model::table::Table;
use crate::renderer::hwpunit_to_px;
fn stored_seg(para: &Paragraph) -> Option<&LineSeg> {
para.line_segs
.iter()
.find(|seg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
}
fn stored_vpos(para: &Paragraph) -> Option<i64> {
stored_seg(para).map(|seg| i64::from(seg.vertical_pos))
}
pub(crate) fn stored_anchor_successor_rewinds(para: &Paragraph, next: &Paragraph) -> bool {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& !next.stored_text_partition_is_dirty()
&& !next.cell_format_vpos_dirty
&& para.line_segs.len() == 1
&& next.line_segs.len() == 1
&& [¶.line_segs[0], &next.line_segs[0]].iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
})
&& next.line_segs[0].vertical_pos < para.line_segs[0].vertical_pos
}
pub(crate) fn stored_topbottom_object_span(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
hwpx_object_frame: bool,
) -> (i64, i64) {
if let Some(outer_box_height) = stored_topbottom_flow_advance_hu(para, next_para, table) {
let top = stored_outer_box_table_top_hu(table);
let offset = if offset_float_follows_inline_line(para, table) {
i64::from(table.common.vertical_offset)
} else {
0
};
(top + offset, outer_box_height)
} else {
let top = -i64::from(table.outer_margin_top);
(
top,
top + i64::from(table.common.height) + i64::from(table.outer_margin_bottom),
)
}
}
pub(crate) fn stored_outer_box_table_top_hu(table: &Table) -> i64 {
i64::from(table.outer_margin_top)
}
pub(crate) fn stored_topbottom_flow_advance_hu(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
) -> Option<i64> {
let outer_box_height = i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
let offset = if offset_float_follows_inline_line(para, table) {
i64::from(table.common.vertical_offset)
} else {
0
};
let occupied_height = outer_box_height + offset;
let stored_outer_box = stored_vpos(para)
.zip(next_para.and_then(stored_vpos))
.is_some_and(|(current, next)| next - current == occupied_height);
stored_outer_box.then_some(occupied_height)
}
fn offset_float_follows_inline_line(para: &Paragraph, table: &Table) -> bool {
use crate::model::control::Control;
let offset = table.common.vertical_offset;
offset > 0
&& offset <= i32::MAX as u32
&& para.controls.len() > 1
&& para.controls.iter().enumerate().all(|(ci, control)| {
let Control::Table(sibling) = control else {
return false;
};
if std::ptr::eq(sibling.as_ref(), table) {
return true;
}
sibling.common.treat_as_char
&& sibling.caption.is_none()
&& crate::renderer::layout::control_line_seg_index(para, ci)
.and_then(|owner| para.line_segs.get(owner))
.is_some_and(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& i64::from(line.line_height)
== i64::from(sibling.common.height)
+ i64::from(sibling.outer_margin_top)
+ i64::from(sibling.outer_margin_bottom)
&& line.line_height > 0
&& line.line_height as u32 <= offset
})
})
}
pub(crate) fn stored_empty_topbottom_outer_box_is_valid(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
measured_height: Option<f64>,
dpi: f64,
) -> bool {
use crate::model::control::Control;
!para
.text
.chars()
.any(|c| !c.is_whitespace() && c > '\u{001F}' && c != '\u{FFFC}')
&& !para.stored_text_partition_is_dirty()
&& next_para.is_some_and(|next| !next.stored_text_partition_is_dirty())
&& crate::renderer::float_placement::is_para_topbottom_float(&table.common)
&& (table.common.vertical_offset == 0 || offset_float_follows_inline_line(para, table))
&& table.common.height > 0
&& table.common.height <= i32::MAX as u32
&& (para
.controls
.iter()
.filter(|c| matches!(c, Control::Table(_)))
.count()
== 1
|| offset_float_follows_inline_line(para, table))
&& stored_topbottom_flow_advance_hu(para, next_para, table).is_some()
&& measured_height
.is_some_and(|height| height <= hwpunit_to_px(table.common.height as i32, dpi) + 0.5)
}
pub(crate) fn stored_ladder_leaves_object_room(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
) -> bool {
let (_, occupied_bottom) = stored_topbottom_object_span(para, next_para, table, false);
let need = occupied_bottom.max(0);
match (stored_vpos(para), next_para.and_then(stored_vpos)) {
(Some(current), Some(next)) => next - current >= need,
_ => false,
}
}
pub(crate) fn stored_single_topbottom_top_px(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
available_px: f64,
dpi: f64,
hwpx_object_frame: bool,
) -> Option<f64> {
if table.common.vertical_offset != 0 {
return None;
}
let current = stored_vpos(para)?;
let next = next_para.and_then(stored_vpos)?;
if next <= current {
return None;
}
if !stored_ladder_leaves_object_room(para, next_para, table) {
return None;
}
let (top_offset, _) = stored_topbottom_object_span(para, next_para, table, hwpx_object_frame);
let top = hwpunit_to_px((current + top_offset) as i32, dpi);
let bottom = top + hwpunit_to_px(table.common.height as i32, dpi);
(top >= -0.5 && bottom <= available_px + 0.5).then_some(top)
}
pub(crate) fn stored_single_cell_first_frame_height_hu(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
) -> Option<i32> {
use crate::model::paragraph::LineSeg;
use crate::model::table::TablePageBreak;
if para.stored_text_partition_is_dirty()
|| !para.text.trim().is_empty()
|| para.controls.len() != 1
|| para.line_segs.len() != 1
|| para.line_segs[0].segment_width != 0
|| table.common.treat_as_char
|| !crate::renderer::float_placement::is_para_topbottom_float(&table.common)
|| table.common.vertical_offset != 0
|| table.caption.is_some()
|| table.row_count != 1
|| table.col_count != 1
|| table.cells.len() != 1
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
{
return None;
}
let cell = &table.cells[0];
let next = next_para?;
let host_y = stored_vpos(para)?;
let next_y = stored_vpos(next)?;
if next.stored_text_partition_is_dirty()
|| cell.row != 0
|| cell.col != 0
|| cell.row_span != 1
|| cell.col_span != 1
|| cell.height <= table.common.height
|| host_y <= 0
|| next_y <= 0
|| next_y >= host_y
|| cell.paragraphs.is_empty()
{
return None;
}
let tail = i64::from(cell.height) - i64::from(table.common.height);
let continuation_end =
tail + i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
if continuation_end != next_y {
return None;
}
let padding = cell.effective_padding(&table.padding);
for paragraph in &cell.paragraphs {
if paragraph.stored_text_partition_is_dirty()
|| !paragraph.controls.is_empty()
|| paragraph.line_segs.is_empty()
|| paragraph.line_segs.iter().any(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| i64::from(line.vertical_pos)
+ i64::from(line.line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom)
> i64::from(table.common.height)
})
{
return None;
}
}
Some(table.common.height as i32)
}