use super::{CellUnit, LayoutEngine};
use crate::model::control::Control;
use crate::model::shape::{TextWrap, VertRelTo};
use crate::renderer::hwpunit_to_px;
impl LayoutEngine {
pub(super) fn delay_empty_anchor_topandbottom_flow_units_before_hard_break(
&self,
units: Vec<CellUnit>,
cell: &crate::model::table::Cell,
table: &crate::model::table::Table,
) -> Vec<CellUnit> {
if !matches!(
table.layout_page_break(),
crate::model::table::TablePageBreak::RowBreak
) || table.common.treat_as_char
{
return units;
}
let source_boundaries: Vec<bool> = units
.iter()
.map(|unit| same_cell_stored_frame_boundary(cell, unit))
.collect();
let mut has_future_stored_frame = vec![false; units.len()];
let mut seen_stored_frame = false;
for idx in (0..units.len()).rev() {
has_future_stored_frame[idx] = seen_stored_frame;
if source_boundaries[idx] {
seen_stored_frame = true;
}
}
let stored_profile = (self.profile.get().hwp5_stored_pagination_layout()
|| self.profile.get().hwpx_stored_layout())
&& !self.profile.get().session_edited();
let mut charged_host_line_h = std::collections::HashMap::<usize, f64>::new();
for unit in units.iter().filter(|unit| unit.vis_start < unit.vis_end) {
*charged_host_line_h.entry(unit.para_idx).or_default() += unit.height;
}
let stored_band_owns_host_line = |para_idx: usize, band: f64| -> bool {
stored_profile
&& cell
.paragraphs
.get(para_idx)
.and_then(|para| para.line_segs.first())
.zip(
cell.paragraphs
.get(para_idx + 1)
.and_then(|next| next.line_segs.first()),
)
.is_some_and(|(line, next)| {
let synthetic =
crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
line.tag & synthetic == 0
&& next.tag & synthetic == 0
&& (hwpunit_to_px(
next.vertical_pos.saturating_sub(line.vertical_pos),
self.dpi,
) - band)
.abs()
<= 1.0
})
};
let mut reordered = Vec::with_capacity(units.len());
let mut pending = Vec::new();
for (idx, mut unit) in units.into_iter().enumerate() {
if has_future_stored_frame[idx]
&& Self::is_delayable_empty_anchor_topandbottom_flow_unit(cell, &unit)
{
if let Some(para) = cell.paragraphs.get(unit.para_idx) {
if let Some(height) =
crate::renderer::float_placement::deferred_picture_band_height(
para, self.dpi,
)
{
let height = if stored_band_owns_host_line(unit.para_idx, height) {
(height
- charged_host_line_h
.get(&unit.para_idx)
.copied()
.unwrap_or(0.0))
.max(0.0)
} else {
height
};
unit.height = height;
unit.non_inline_control_range = Some((0, para.controls.len() - 1));
}
}
pending.push(unit);
continue;
}
if source_boundaries[idx] && !pending.is_empty() {
reordered.append(&mut pending);
}
reordered.push(unit);
}
reordered.append(&mut pending);
reordered
}
fn is_delayable_empty_anchor_topandbottom_flow_unit(
cell: &crate::model::table::Cell,
unit: &CellUnit,
) -> bool {
if !Self::is_non_inline_control_flow_unit(unit) {
return false;
}
let Some(para) = cell.paragraphs.get(unit.para_idx) else {
return false;
};
para.text.trim().is_empty()
&& para.controls.iter().any(|control| match control {
Control::Picture(pic) => {
!pic.common.treat_as_char
&& pic.common.flow_with_text
&& matches!(pic.common.text_wrap, TextWrap::TopAndBottom)
&& matches!(pic.common.vert_rel_to, VertRelTo::Para)
}
Control::Shape(shape) => {
let common = shape.common();
!common.treat_as_char
&& common.flow_with_text
&& matches!(common.text_wrap, TextWrap::TopAndBottom)
&& matches!(common.vert_rel_to, VertRelTo::Para)
}
_ => false,
})
}
}
fn same_cell_stored_frame_boundary(cell: &crate::model::table::Cell, unit: &CellUnit) -> bool {
use crate::model::paragraph::LineSeg;
if !unit.stored_frame_break_before && !unit.hard_break_before {
return false;
}
let Some(para) = cell.paragraphs.get(unit.para_idx) else {
return false;
};
let Some(current) = para.line_segs.get(unit.vis_start) else {
return false;
};
let previous = if unit.vis_start > 0 {
para.line_segs.get(unit.vis_start - 1)
} else {
cell.paragraphs[..unit.para_idx]
.iter()
.rev()
.find_map(|paragraph| paragraph.line_segs.last())
};
previous.is_some_and(|previous| {
(previous.tag | current.tag) & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& current.vertical_pos >= 0
&& current.vertical_pos < previous.vertical_pos
})
}