use super::pagination::{EndnoteParaSource, PageItem};
pub(crate) fn starts_note_after_complete_tail(
previous_source: Option<&EndnoteParaSource>,
current_source: &EndnoteParaSource,
current_item: &PageItem,
previous_items: &[PageItem],
previous_para_index: usize,
previous_line_count: usize,
) -> bool {
let Some(previous_source) = previous_source else {
return false;
};
if current_source.note_para_index != 0
|| previous_line_count == 0
|| (
previous_source.section_index,
previous_source.para_index,
previous_source.control_index,
) == (
current_source.section_index,
current_source.para_index,
current_source.control_index,
)
|| !matches!(
current_item,
PageItem::FullParagraph { .. } | PageItem::PartialParagraph { start_line: 0, .. }
)
{
return false;
}
previous_items.iter().any(|item| match item {
PageItem::FullParagraph { para_index } => *para_index == previous_para_index,
PageItem::PartialParagraph {
para_index,
end_line,
..
} => *para_index == previous_para_index && *end_line == previous_line_count,
_ => false,
})
}
pub(crate) fn occupied_end(content_end: Option<f64>, line_box_end: Option<f64>) -> Option<f64> {
content_end
.into_iter()
.chain(line_box_end)
.filter(|end| end.is_finite())
.reduce(f64::max)
}
pub(crate) fn origin_after_gap(flow_origin: f64, occupied_end: Option<f64>, gap: f64) -> f64 {
occupied_end.map_or(flow_origin + gap, |end| (end + gap).max(flow_origin))
}
pub(crate) fn gap_is_reserved(flow_origin: f64, occupied_end: f64, gap: f64) -> bool {
let end_with_gap = occupied_end + gap;
let roundoff = (flow_origin.abs() + occupied_end.abs() + gap.abs()) * f64::EPSILON;
end_with_gap <= flow_origin + roundoff
}
#[derive(Default)]
pub(crate) struct ParagraphLineBoxEnd {
owner: Option<usize>,
bottom: Option<f64>,
}
impl ParagraphLineBoxEnd {
pub(crate) fn after_item(
&mut self,
item: &PageItem,
is_inline_control: bool,
line_bottom: f64,
) -> Option<f64> {
let measured = line_bottom.is_finite().then_some(line_bottom);
match item {
PageItem::FullParagraph { para_index }
| PageItem::PartialParagraph { para_index, .. } => {
self.owner = Some(*para_index);
self.bottom = measured;
}
PageItem::Shape { para_index, .. }
if is_inline_control && self.owner == Some(*para_index) =>
{
self.bottom = occupied_end(self.bottom, measured);
}
_ => {
self.owner = None;
self.bottom = measured;
}
}
self.bottom
}
}