use crate::model::shape::CaptionDirection;
use crate::model::table::Table;
use crate::renderer::hwpunit_to_px;
use crate::renderer::style_resolver::ResolvedStyleSet;
pub(super) fn owned_body_height(
table: &Table,
body_height: f64,
line_height: f64,
styles: &ResolvedStyleSet,
dpi: f64,
tolerance: f64,
) -> Option<f64> {
let top = hwpunit_to_px(i32::from(table.outer_margin_top), dpi);
let bottom = hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi);
if top <= 0.0 && bottom <= 0.0 {
return None;
}
let caption_extra = table.caption.as_ref().map_or(0.0, |caption| {
if !matches!(
caption.direction,
CaptionDirection::Top | CaptionDirection::Bottom
) {
return 0.0;
}
let height = crate::renderer::composer::caption_height_px_in_context(
&table.caption,
Some(styles),
dpi,
);
if height > 0.0 {
height + hwpunit_to_px(i32::from(caption.spacing), dpi)
} else {
0.0
}
});
[body_height, body_height + caption_extra]
.into_iter()
.find(|height| (line_height - (height + top + bottom)).abs() <= tolerance)
}
pub(super) fn companion_table_top(
line_top: f64,
owner_h: f64,
owner_om_top: f64,
table_h: f64,
) -> f64 {
(line_top + owner_om_top + (owner_h - table_h) * 0.85).max(line_top)
}
pub(super) fn independent_stored_tail(
para: &crate::model::paragraph::Paragraph,
start: usize,
end: usize,
) -> bool {
use crate::model::{control::Control, paragraph::LineSeg};
let Some(lines) = para
.line_segs
.get(start..end)
.filter(|lines| !lines.is_empty())
else {
return false;
};
lines
.iter()
.all(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && line.line_height > 0)
&& para.controls.iter().enumerate().all(|(index, control)| {
if !matches!(control, Control::Table(table) if table.common.treat_as_char) {
return true;
}
super::control_line_seg_index(para, index)
.filter(|line| *line < start)
.and_then(|line| para.line_segs.get(line))
.is_some_and(|host| {
i64::from(lines[0].vertical_pos)
>= i64::from(host.vertical_pos)
+ i64::from(host.line_height)
+ i64::from(host.line_spacing)
})
})
}
pub(super) fn line_table_owner<'a>(
tables: impl IntoIterator<Item = &'a Table>,
line_height: f64,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(f64, f64)> {
tables.into_iter().find_map(|table| {
let height = hwpunit_to_px(table.common.height as i32, dpi);
owned_body_height(table, height, line_height, styles, dpi, 0.2)
.map(|owned| (owned, hwpunit_to_px(i32::from(table.outer_margin_top), dpi)))
})
}