use super::*;
pub(crate) fn stored_square_top_band(
para: &Paragraph,
control: &Control,
body: &LayoutRect,
dpi: f64,
) -> Option<f64> {
let Control::Table(table) = control else {
return None;
};
let common = &table.common;
if common.treat_as_char
|| common.text_wrap != TextWrap::Square
|| !matches!(common.vert_rel_to, VertRelTo::Page | VertRelTo::Paper)
|| common.vert_align != VertAlign::Top
|| common.horz_rel_to != HorzRelTo::Page
|| table.caption.is_some()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| body.width <= 0.0
|| !dpi.is_finite()
|| dpi <= 0.0
{
return None;
}
let line = para.line_segs.first()?;
if line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.vertical_pos < 0
{
return None;
}
let ref_y = if common.vert_rel_to == VertRelTo::Page {
body.y
} else {
0.0
};
let height = hwpunit_to_px(table_reserved_height_hu(table) as i32, dpi);
let (top, bottom) = absolute_table_vertical_geometry(table, ref_y, body.height, height, dpi);
let hu = |px| super::super::px_to_hwpunit(px, dpi);
if hu(top) > hu(body.y) || hu(bottom) <= hu(body.y) || line.vertical_pos < hu(bottom - body.y) {
return None;
}
let context = FloatPlacementContext::new(*body).with_body_area(*body);
let width = hwpunit_to_px(signed_hwpunit(common.width), dpi);
let (left, right) = horizontal_range(common, width, context, dpi);
let exclusion = FrameExclusion {
horizontal: hu(left - hwpunit_to_px(i32::from(table.outer_margin_left), dpi))
..hu(right + hwpunit_to_px(i32::from(table.outer_margin_right), dpi)),
vertical: hu(top)..hu(bottom),
policy: frame_exclusion_policy(common.text_flow),
};
let body_top = hu(body.y);
let mut frame = LayoutFrame::new(
hu(body.x)..hu(body.x + body.width),
body_top,
vec![exclusion],
);
frame.carve(line.line_height);
(frame.top > body_top).then_some(bottom)
}
pub(crate) fn table_reserved_height_hu(table: &Table) -> u32 {
let row_count = table.row_count as usize;
if row_count == 0 {
return table.common.height;
}
let mut row_heights = vec![0u32; row_count];
for cell in &table.cells {
if cell.row_span == 1 && (cell.row as usize) < row_count {
let r = cell.row as usize;
if cell.height < 0x80000000 && cell.height > row_heights[r] {
row_heights[r] = cell.height;
}
}
}
for cell in &table.cells {
if cell.row_span == 1 && (cell.row as usize) < row_count {
let r = cell.row as usize;
let (pad_top, pad_bottom) = if cell.apply_inner_margin {
(cell.padding.top as u32, cell.padding.bottom as u32)
} else {
(table.padding.top as u32, table.padding.bottom as u32)
};
let content_h: i32 = cell
.paragraphs
.iter()
.flat_map(|p| p.line_segs.last())
.map(|s| s.vertical_pos.saturating_add(s.line_height))
.max()
.unwrap_or(0);
let required = content_h as u32 + pad_top + pad_bottom;
if required > row_heights[r] {
row_heights[r] = required;
}
}
}
let total: u32 = row_heights.iter().sum();
total.max(table.common.height)
}
pub(crate) fn stored_empty_line_has_closed_source_box(
paragraphs: &[Paragraph],
index: usize,
) -> bool {
let Some(current) = paragraphs.get(index) else {
return false;
};
let Some(lines) = stored_empty_flow_lines(current) else {
return false;
};
let first = &lines[0];
let last = &lines[lines.len() - 1];
let Some(previous) = index.checked_sub(1).and_then(|i| paragraphs.get(i)) else {
return false;
};
let Some(next) = paragraphs.get(index + 1) else {
return false;
};
if [previous, current, next]
.iter()
.any(|para| para.stored_text_partition_is_dirty() || para.cell_format_vpos_dirty)
|| current.column_type != crate::model::paragraph::ColumnBreakType::None
|| next.column_type != crate::model::paragraph::ColumnBreakType::None
{
return false;
}
let Some(before) = previous.line_segs.last() else {
return false;
};
let Some(after) = next.line_segs.first() else {
return false;
};
let original = |line: &crate::model::paragraph::LineSeg| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.line_height > 0
&& line.vertical_pos >= 0
&& line.line_spacing >= 0
};
original(before)
&& original(after)
&& before
.vertical_pos
.checked_add(before.line_height)
.and_then(|end| end.checked_add(before.line_spacing))
== Some(first.vertical_pos)
&& lines.windows(2).all(|pair| {
pair[0]
.vertical_pos
.checked_add(pair[0].line_height)
.and_then(|end| end.checked_add(pair[0].line_spacing))
== Some(pair[1].vertical_pos)
})
&& last
.vertical_pos
.checked_add(last.line_height)
.and_then(|end| end.checked_add(last.line_spacing))
== Some(after.vertical_pos)
}