use super::control_line_seg_index;
use crate::model::{control::Control, paragraph::Paragraph, shape::TextWrap, table::Table};
pub(super) fn tac_has_only_in_front_decoration_shapes_before(
para: &Paragraph,
control_index: usize,
) -> bool {
let before = ¶.controls[..control_index.min(para.controls.len())];
!before.is_empty()
&& before.iter().all(|control| {
matches!(control, Control::Shape(shape)
if !shape.common().treat_as_char
&& matches!(
shape.common().text_wrap,
crate::model::shape::TextWrap::InFrontOfText
))
})
}
fn tac_has_only_non_flow_decorations_before(para: &Paragraph, control_index: usize) -> bool {
let before = ¶.controls[..control_index.min(para.controls.len())];
!before.is_empty()
&& before.iter().all(|control| {
let common = match control {
Control::Shape(shape) => shape.common(),
Control::Picture(picture) => &picture.common,
_ => return false,
};
!common.treat_as_char
&& matches!(
common.text_wrap,
crate::model::shape::TextWrap::InFrontOfText
| crate::model::shape::TextWrap::BehindText
)
})
}
pub(crate) fn tac_in_front_decoration_fixed_line_spacing_deduction_hu(
para: &Paragraph,
control_index: usize,
) -> Option<i32> {
let Control::Table(table) = para.controls.get(control_index)? else {
return None;
};
if !table.common.treat_as_char
|| !matches!(table.common.text_wrap, TextWrap::TopAndBottom)
|| !tac_has_only_in_front_decoration_shapes_before(para, control_index)
|| table.common.height >= 0x8000_0000
{
return None;
}
let seg = para
.line_segs
.get(control_line_seg_index(para, control_index)?)?;
let outer_box_height = i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
(seg.line_spacing < 0 && i64::from(seg.line_height) >= outer_box_height - 10)
.then_some(-seg.line_spacing)
}
pub(crate) fn stored_empty_full_band_tac_successor_vpos_hu(
para: &Paragraph,
control_index: usize,
table: &Table,
following: &Paragraph,
) -> Option<i32> {
if !para.text.trim().is_empty()
|| !table.common.treat_as_char
|| !matches!(table.common.text_wrap, TextWrap::TopAndBottom)
|| para.stored_text_partition_dirty
|| !tac_has_only_non_flow_decorations_before(para, control_index)
{
return None;
}
let synth = crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
let owner = para
.line_segs
.get(control_line_seg_index(para, control_index)?)
.filter(|line| line.tag & synth == 0)?;
let following = following
.line_segs
.iter()
.find(|line| line.tag & synth == 0)?;
let outer_box_height_hu = i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
if outer_box_height_hu <= 0 || (i64::from(owner.text_height) - outer_box_height_hu).abs() > 8 {
return None;
}
let expected_next_vpos = i64::from(owner.vertical_pos)
+ i64::from(owner.text_height)
+ i64::from(owner.line_spacing);
if following.vertical_pos <= owner.vertical_pos
|| (i64::from(following.vertical_pos) - expected_next_vpos).abs() > 8
{
return None;
}
Some(following.vertical_pos)
}