use crate::model::{paragraph::Paragraph, shape::TextWrap, table::Table};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct StoredLineLaneProbe {
pub(crate) vertical_pos: i32,
pub(crate) column_start: i32,
pub(crate) segment_width: i32,
pub(crate) full_width: i32,
}
pub(crate) fn stored_line_lane_probe(para: &Paragraph) -> Option<StoredLineLaneProbe> {
use crate::model::paragraph::LineSeg;
let original = |seg: &&LineSeg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0;
let first = para.line_segs.first().filter(original)?;
let full_width = para
.line_segs
.iter()
.filter(original)
.map(|seg| seg.segment_width as i32)
.max()?;
Some(StoredLineLaneProbe {
vertical_pos: first.vertical_pos,
column_start: first.column_start,
segment_width: first.segment_width as i32,
full_width,
})
}
pub(crate) fn square_successor_starts_beside_table(
host: &Paragraph,
next: Option<StoredLineLaneProbe>,
table: &Table,
) -> bool {
use crate::model::paragraph::LineSeg;
if std::env::var("RHWP_TABLE_DRIFT").is_ok() {
eprintln!(
"SQUARE_LANE_INPUT wrap={:?} tac={} first={:?} next={next:?}",
table.common.text_wrap,
table.common.treat_as_char,
host.line_segs.first()
);
}
if table.common.treat_as_char || !matches!(table.common.text_wrap, TextWrap::Square) {
return false;
}
let (Some(host_first), Some(next)) = (
host.line_segs
.first()
.filter(|seg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0),
next,
) else {
return false;
};
let contains_successor = |line: &LineSeg| {
line.segment_width > 0
&& next.vertical_pos >= line.vertical_pos
&& next.segment_width > 0
&& next.column_start >= line.column_start
&& i64::from(next.column_start) + i64::from(next.segment_width)
<= i64::from(line.column_start) + i64::from(line.segment_width)
};
if ((host_first.segment_width as i32) < next.full_width
|| host_line_clears_table_box(host_first, table))
&& contains_successor(host_first)
{
return true;
}
let Some(last) = host.line_segs.last() else {
return false;
};
let continuous_lane = !host.stored_text_partition_is_dirty()
&& host.line_segs.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && line.line_height > 0
})
&& host.line_segs.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(next.vertical_pos)
&& host
.line_segs
.iter()
.any(|line| i64::from(line.segment_width) > i64::from(last.segment_width))
&& contains_successor(last);
if std::env::var("RHWP_TABLE_DRIFT").is_ok() {
eprintln!(
"SQUARE_LANE last={last:?} next={next:?} dirty={} continuous={continuous_lane}",
host.stored_text_partition_is_dirty()
);
}
continuous_lane
}
fn host_line_clears_table_box(line: &crate::model::paragraph::LineSeg, table: &Table) -> bool {
use crate::model::shape::{HorzRelTo, VertRelTo};
use crate::renderer::float_placement::signed_hwpunit;
if table.common.vert_rel_to != VertRelTo::Para
|| !matches!(
table.common.horz_rel_to,
HorzRelTo::Para | HorzRelTo::Column
)
|| line.segment_width <= 0
|| line.line_height <= 0
{
return false;
}
let left = i64::from(signed_hwpunit(table.common.horizontal_offset));
let right = left
+ i64::from(table.outer_margin_left)
+ i64::from(table.common.width)
+ i64::from(table.outer_margin_right);
let line_left = i64::from(line.column_start);
let line_right = line_left + i64::from(line.segment_width);
let beside = line_right <= left || line_left >= right;
let top = i64::from(signed_hwpunit(table.common.vertical_offset));
let bottom = top
+ i64::from(table.outer_margin_top)
+ i64::from(table.common.height)
+ i64::from(table.outer_margin_bottom);
let overlaps_vertically = top < i64::from(line.line_height) && bottom > 0;
beside && overlaps_vertically
}