use super::{hwpunit_to_px, stored_table_before_host_frame, StoredBlockCellFlow};
use crate::model::{control::Control, paragraph::Paragraph};
use crate::renderer::style_resolver::ResolvedStyleSet;
pub(super) fn terminal_cell_border_before_successor_hu(
table: &crate::model::table::Table,
successor: &Paragraph,
) -> Option<i32> {
use crate::model::paragraph::LineSeg;
let [cell] = table.cells.as_slice() else {
return None;
};
let tail = cell.paragraphs.last()?;
let line = tail.line_segs.last()?;
let [next] = successor.line_segs.as_slice() else {
return None;
};
if cell.dirty_flag
|| tail.stored_text_partition_is_dirty()
|| tail.cell_format_vpos_dirty
|| !tail.controls.is_empty()
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| line.line_height <= 0
|| table.outer_margin_top < 0
|| table.outer_margin_bottom < 0
{
return None;
}
let padding = cell.effective_padding(&table.padding);
if padding.top < 0 || padding.bottom < 0 {
return None;
}
let outer_bottom = i64::from(line.vertical_pos)
+ i64::from(line.line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
(outer_bottom == i64::from(next.vertical_pos))
.then(|| {
next.vertical_pos
.checked_sub(i32::from(table.outer_margin_bottom))
})
.flatten()
}
pub(super) fn closed_before_host_flow(
paragraphs: &[Paragraph],
styles: &ResolvedStyleSet,
heights: &[Vec<f64>],
dpi: f64,
) -> Option<StoredBlockCellFlow> {
let mut previous = None;
let mut has_frame = false;
let mut result = StoredBlockCellFlow {
paragraph_origins: Vec::with_capacity(paragraphs.len()),
origins: paragraphs
.iter()
.map(|p| vec![None; p.controls.len()])
.collect(),
bottom: 0.0,
closed_before_host: true,
};
for (pi, paragraph) in paragraphs.iter().enumerate() {
if paragraph.raw_break_type != 0
|| paragraph.column_type != crate::model::paragraph::ColumnBreakType::None
{
return None;
}
let first = paragraph.line_segs.first()?;
let source_host = hwpunit_to_px(first.vertical_pos, dpi);
result.paragraph_origins.push(source_host);
for line in ¶graph.line_segs {
if previous.is_some_and(|top| line.vertical_pos < top) {
return None;
}
previous = Some(line.vertical_pos);
let end = hwpunit_to_px(line.vertical_pos.checked_add(line.line_height)?, dpi);
result.bottom = result.bottom.max(end);
}
for (ci, control) in paragraph.controls.iter().enumerate() {
let Control::Table(table) = control else {
continue;
};
let body = *heights.get(pi)?.get(ci)?
- hwpunit_to_px(
i32::from(table.outer_margin_top) + i32::from(table.outer_margin_bottom),
dpi,
);
let before = styles
.para_styles
.get(paragraph.para_shape_id as usize)?
.spacing_before;
let frame = stored_table_before_host_frame(
paragraphs,
pi,
table,
ci,
source_host - before,
body,
styles,
dpi,
)?;
result.origins[pi][ci] = Some(frame.anchor_y);
result.bottom = result.bottom.max(frame.occupied_bottom);
has_frame = true;
}
}
has_frame.then_some(result)
}