use crate::model::{control::Control, paragraph::LineSeg, paragraph::Paragraph};
use crate::renderer::{hwpunit_to_px, px_to_hwpunit};
use super::InlineBoxPlacement;
use crate::renderer::{pagination::PageItem, style_resolver::ResolvedStyleSet};
use std::collections::HashMap;
pub(crate) fn restarted_tac_opening_before(
para: &Paragraph,
next: &Paragraph,
spacing_before: f64,
spacing_after: f64,
next_spacing_before: f64,
dpi: f64,
) -> Option<f64> {
if !dpi.is_finite()
|| dpi <= 0.0
|| !spacing_before.is_finite()
|| spacing_before <= 0.0
|| !spacing_after.is_finite()
|| !next_spacing_before.is_finite()
|| para.line_segs.len() != 1
|| para.cell_format_vpos_dirty
|| next.cell_format_vpos_dirty
|| next.stored_text_partition_is_dirty()
|| next.raw_break_type != 0
|| next.column_type != crate::model::paragraph::ColumnBreakType::None
|| !(next.controls.is_empty()
|| (matches!(next.controls.as_slice(), [Control::Table(_)])
&& crate::renderer::composer::stored_first_tac_line(next).is_some()))
|| !matches!(para.controls.as_slice(), [Control::Table(_)])
{
return None;
}
let line = crate::renderer::composer::stored_first_tac_line(para)?;
let after = next.line_segs.first()?;
if line.line_spacing < 0
|| after.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| after.vertical_pos < 0
|| after.vertical_pos >= line.vertical_pos
{
return None;
}
let before = px_to_hwpunit(spacing_before, dpi);
let end = before
.checked_add(line.line_height)?
.checked_add(line.line_spacing)?
.checked_add(px_to_hwpunit(spacing_after, dpi))?
.checked_add(px_to_hwpunit(next_spacing_before, dpi))?;
(before > 0 && end == after.vertical_pos).then(|| hwpunit_to_px(before, dpi))
}
pub(crate) fn restarted_tac_line_advance(
para: &Paragraph,
next: &Paragraph,
spacing_after: f64,
next_spacing_before: f64,
dpi: f64,
) -> Option<f64> {
if !dpi.is_finite()
|| dpi <= 0.0
|| !spacing_after.is_finite()
|| !next_spacing_before.is_finite()
|| para.line_segs.len() != 1
|| para.cell_format_vpos_dirty
|| next.cell_format_vpos_dirty
|| next.stored_text_partition_is_dirty()
|| !next.controls.is_empty()
|| next.raw_break_type != 0
|| next.column_type != crate::model::paragraph::ColumnBreakType::None
{
return None;
}
let [Control::Table(_)] = para.controls.as_slice() else {
return None;
};
let line = crate::renderer::composer::stored_first_tac_line(para)?;
let after = next.line_segs.first()?;
if line.line_spacing < 0
|| after.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| after.vertical_pos < 0
|| after.vertical_pos >= line.vertical_pos
{
return None;
}
let end = line
.line_height
.checked_add(line.line_spacing)?
.checked_add(px_to_hwpunit(spacing_after, dpi))?;
(end.checked_add(px_to_hwpunit(next_spacing_before, dpi))? == after.vertical_pos)
.then(|| hwpunit_to_px(end, dpi))
}
pub(crate) fn accepted_opening_origin(
para: &Paragraph,
next: &Paragraph,
spacing_before: f64,
spacing_after: f64,
next_spacing_before: f64,
placement: &InlineBoxPlacement,
dpi: f64,
) -> Option<i32> {
let opening_before = restarted_tac_opening_before(
para,
next,
spacing_before,
spacing_after,
next_spacing_before,
dpi,
);
let leading = opening_before.unwrap_or(0.0);
let end = if opening_before.is_some() {
let line = crate::renderer::composer::stored_first_tac_line(para)?;
leading
+ hwpunit_to_px(line.line_height.checked_add(line.line_spacing)?, dpi)
+ spacing_after
} else {
restarted_tac_line_advance(para, next, spacing_after, next_spacing_before, dpi)?
};
let tolerance = hwpunit_to_px(1, dpi);
((placement.y - leading).abs() <= tolerance
&& (placement.advance_end? - end).abs() <= tolerance)
.then_some(0)
}
pub(crate) fn accepted_column_origin(
items: &[PageItem],
placements: &HashMap<(usize, usize), InlineBoxPlacement>,
paragraphs: &[Paragraph],
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(usize, i32)> {
let PageItem::Table {
para_index,
control_index,
} = items.first()?
else {
return None;
};
let para = paragraphs.get(*para_index)?;
let next = paragraphs.get(para_index.checked_add(1)?)?;
let first_style = styles.para_styles.get(para.para_shape_id as usize)?;
let before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
let origin = accepted_opening_origin(
para,
next,
first_style.spacing_before,
first_style.spacing_after,
before,
placements.get(&(*para_index, *control_index))?,
dpi,
)?;
Some((*para_index, origin))
}