use super::ParagraphFloatPlacement;
use crate::model::{control::Control, paragraph::Paragraph, table::Table};
pub(crate) fn stored_nested_host_frame(
paragraph: &Paragraph,
table: &Table,
control: usize,
origin: f64,
body_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
if paragraph.cell_format_vpos_dirty
|| paragraph
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
{
return None;
}
ParagraphFloatPlacement::from_stored_host(paragraph, table, control, origin, body_height, dpi)
.or_else(|| {
ParagraphFloatPlacement::from_stored_head_host(
paragraph,
table,
control,
origin,
body_height,
dpi,
)
})
.or_else(|| {
ParagraphFloatPlacement::from_stored_inner_host(
paragraph,
table,
control,
origin,
body_height,
dpi,
)
})
.or_else(|| {
ParagraphFloatPlacement::from_empty_stored_offset_host(
paragraph,
table,
control,
origin,
body_height,
dpi,
)
})
}
pub(crate) fn stored_nested_host_content_bottom(
paragraphs: &[Paragraph],
host_index: usize,
frame: ParagraphFloatPlacement,
dpi: f64,
) -> f64 {
let origin = paragraphs
.get(host_index)
.and_then(|p| p.line_segs.first())
.map_or(0.0, |line| super::hwpunit_to_px(line.vertical_pos, dpi));
let object_bottom = origin + frame.occupied_bottom;
let mut previous = None;
let mut end = 0_i64;
for paragraph in paragraphs {
if paragraph.cell_format_vpos_dirty
|| paragraph.stored_text_partition_is_dirty()
|| paragraph.line_segs.is_empty()
{
return object_bottom;
}
for line in ¶graph.line_segs {
if line.vertical_pos < 0
|| line.line_height <= 0
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| previous.is_some_and(|top| line.vertical_pos < top)
|| (previous.is_some()
&& (line.is_first_line_of_page() || line.is_first_line_of_column()))
{
return object_bottom;
}
previous = Some(line.vertical_pos);
end = end.max(i64::from(line.vertical_pos) + i64::from(line.line_height));
}
}
i32::try_from(end).ok().map_or(object_bottom, |end| {
object_bottom.max(super::hwpunit_to_px(end, dpi))
})
}