use super::{ColumnContent, PageContent, PageItem, WrapAroundPara};
use crate::renderer::page_layout::PageLayoutInfo;
use std::collections::HashMap;
const DEFENSE_MAX_PER_PAGE: u32 = 100;
pub(super) struct PaginationState {
pub pages: Vec<PageContent>,
pub current_items: Vec<PageItem>,
pub current_height: f64,
pub current_column: u16,
pub current_footnote_height: f64,
pub footnote_placement: crate::model::footnote::FootnotePlacement,
pub footnote_frames: Vec<crate::renderer::footnote_frames::Reservation>,
pub is_first_footnote_on_page: bool,
pub col_count: u16,
pub layout: PageLayoutInfo,
pub current_zone_y_offset: f64,
pub current_zone_layout: Option<PageLayoutInfo>,
pub on_first_multicolumn_page: bool,
pub section_index: usize,
pub footnote_separator_overhead: f64,
pub footnote_between_notes_margin: f64,
pub footnote_safety_margin: f64,
pub current_column_wrap_around_paras: Vec<WrapAroundPara>,
pub current_column_wrap_anchors: std::collections::HashMap<usize, super::WrapAnchorRef>,
pub page_vpos_base: Option<i32>,
pub page_has_block_table: bool,
pub defense_counts: HashMap<usize, u32>,
pub overflow_carry: Option<PageItem>,
pub layer1_advancing: bool,
}
impl PaginationState {
pub fn new(
layout: PageLayoutInfo,
col_count: u16,
section_index: usize,
footnote_separator_overhead: f64,
footnote_between_notes_margin: f64,
footnote_safety_margin: f64,
) -> Self {
Self {
pages: Vec::new(),
current_items: Vec::new(),
current_height: 0.0,
current_column: 0,
current_footnote_height: 0.0,
footnote_placement: Default::default(),
footnote_frames: Vec::new(),
is_first_footnote_on_page: true,
col_count,
layout,
current_zone_y_offset: 0.0,
current_zone_layout: None,
on_first_multicolumn_page: false,
section_index,
footnote_separator_overhead,
footnote_between_notes_margin,
footnote_safety_margin,
current_column_wrap_around_paras: Vec::new(),
current_column_wrap_anchors: std::collections::HashMap::new(),
page_vpos_base: None,
page_has_block_table: false,
defense_counts: HashMap::new(),
overflow_carry: None,
layer1_advancing: false,
}
}
pub fn flush_column_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
if self.current_items.is_empty() {
return Ok(());
}
let col_content = ColumnContent {
paragraph_origins: Default::default(),
computed_paragraphs: Default::default(),
column_index: self.current_column,
start_height: 0.0,
endnote_flow: false,
items: std::mem::take(&mut self.current_items),
zone_layout: crate::storage::try_clone(&self.current_zone_layout, storage)?,
zone_y_offset: self.current_zone_y_offset,
wrap_around_paras: std::mem::take(&mut self.current_column_wrap_around_paras),
used_height: self.current_height,
wrap_anchors: std::mem::take(&mut self.current_column_wrap_anchors),
overlay_continuations: Vec::new(),
overlay_cuts: Vec::new(),
inline_placements: Default::default(),
inline_flow_plans: Default::default(),
stored_paragraph_flow_ends: Default::default(),
host_compositions: Default::default(),
stored_column_top_spacings: Default::default(),
paragraph_float_placements: Default::default(),
float_pushed_line_spacings: Default::default(),
page_band_flow_starts: Default::default(),
};
if let Some(page) = self.pages.last_mut() {
storage.push(&mut page.column_contents, col_content)?;
} else {
let page =
self.new_page_content_with_storage(storage.collect([col_content])?, storage)?;
storage.push(&mut self.pages, page)?;
}
Ok(())
}
pub fn flush_column_always_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
let col_content = ColumnContent {
paragraph_origins: Default::default(),
computed_paragraphs: Default::default(),
column_index: self.current_column,
start_height: 0.0,
endnote_flow: false,
items: std::mem::take(&mut self.current_items),
zone_layout: crate::storage::try_clone(&self.current_zone_layout, storage)?,
zone_y_offset: self.current_zone_y_offset,
wrap_around_paras: std::mem::take(&mut self.current_column_wrap_around_paras),
used_height: self.current_height,
wrap_anchors: std::mem::take(&mut self.current_column_wrap_anchors),
overlay_continuations: Vec::new(),
overlay_cuts: Vec::new(),
inline_placements: Default::default(),
inline_flow_plans: Default::default(),
stored_paragraph_flow_ends: Default::default(),
host_compositions: Default::default(),
stored_column_top_spacings: Default::default(),
paragraph_float_placements: Default::default(),
float_pushed_line_spacings: Default::default(),
page_band_flow_starts: Default::default(),
};
if let Some(page) = self.pages.last_mut() {
storage.push(&mut page.column_contents, col_content)?;
} else {
let page =
self.new_page_content_with_storage(storage.collect([col_content])?, storage)?;
storage.push(&mut self.pages, page)?;
}
Ok(())
}
pub fn advance_column_or_new_page_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
self.flush_column_with_storage(storage)?;
if self.current_column + 1 < self.col_count {
self.current_column += 1;
self.select_footnote_frame();
self.current_height = 0.0;
} else {
self.push_new_page_with_storage(storage)?;
}
Ok(())
}
fn check_last_item_overflow_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
if self.overflow_carry.is_some() {
return Ok(());
}
let is_para_item = self.current_items.last().map_or(false, |item| {
matches!(
item,
PageItem::FullParagraph { .. } | PageItem::PartialParagraph { .. }
)
});
if !is_para_item {
return Ok(());
}
let available = self.available_height();
if self.current_height <= available + 0.5 {
return Ok(());
}
let page_idx = self.pages.len();
if !self.defense_counts.contains_key(&page_idx) {
storage.reserve_map(&mut self.defense_counts, 1)?;
}
let count = self.defense_counts.entry(page_idx).or_insert(0);
*count += 1;
if *count > DEFENSE_MAX_PER_PAGE {
return Ok(());
}
self.overflow_carry = self.current_items.pop();
Ok(())
}
pub fn reinsert_carry_with_height_with_storage<S: crate::storage::Storage>(
&mut self,
carry_height: f64,
storage: &S,
) -> Result<(), S::Error> {
if let Some(carry) = self.overflow_carry.take() {
storage.push(&mut self.current_items, carry)?;
self.current_height += carry_height;
}
Ok(())
}
pub fn force_new_page_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
self.flush_column_with_storage(storage)?;
self.push_new_page_with_storage(storage)?;
Ok(())
}
pub fn ensure_page_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
if self.pages.is_empty() {
let page = self.new_page_content_with_storage(Vec::new(), storage)?;
storage.push(&mut self.pages, page)?;
}
Ok(())
}
pub fn available_height(&self) -> f64 {
let base = self.layout.available_body_height();
let fn_margin = if self.current_footnote_height > 0.0 {
self.footnote_safety_margin
} else {
0.0
};
(base - self.current_footnote_height - fn_margin - self.current_zone_y_offset).max(0.0)
}
pub fn base_available_height(&self) -> f64 {
self.layout.available_body_height()
}
fn footnote_frame_policy(&self) -> crate::renderer::footnote_frames::FramePolicy {
crate::renderer::footnote_frames::FramePolicy {
placement: self.footnote_placement,
columns: self.col_count,
}
}
fn select_footnote_frame(&mut self) {
let policy = self.footnote_frame_policy();
if policy.spans_page() {
return;
}
let frame = if policy.charges_column(self.current_column) {
self.footnote_frames
.get(usize::from(policy.target(self.current_column)))
.copied()
.unwrap_or_default()
} else {
Default::default()
};
self.current_footnote_height = frame.height;
self.is_first_footnote_on_page = frame.count == 0;
}
pub fn add_footnote_height(&mut self, height: f64) {
let policy = self.footnote_frame_policy();
if !policy.spans_page() {
self.footnote_frames
.resize(usize::from(self.col_count), Default::default());
self.footnote_frames[usize::from(policy.target(self.current_column))].add(
height,
true,
self.footnote_separator_overhead,
self.footnote_between_notes_margin,
);
let maximum = self
.footnote_frames
.iter()
.map(|frame| frame.height)
.fold(0.0, f64::max);
if let Some(page) = self.pages.last_mut() {
page.layout.update_footnote_area(maximum);
}
self.select_footnote_frame();
return;
}
if self.is_first_footnote_on_page {
self.current_footnote_height += self.footnote_separator_overhead;
self.is_first_footnote_on_page = false;
} else {
self.current_footnote_height += self.footnote_between_notes_margin;
}
self.current_footnote_height += height;
self.sync_current_page_footnote_area();
}
pub fn projected_footnote_height(&self, note_content_height: f64, note_count: usize) -> f64 {
let policy = self.footnote_frame_policy();
if !policy.spans_page() {
let frame = self
.footnote_frames
.get(usize::from(policy.target(self.current_column)))
.copied()
.unwrap_or_default();
let projected = frame.projected(
note_content_height,
note_count,
true,
self.footnote_separator_overhead,
self.footnote_between_notes_margin,
);
return if policy.charges_column(self.current_column)
|| projected > self.base_available_height()
{
projected
} else {
0.0
};
}
if note_count == 0 {
return self.current_footnote_height;
}
let separator = if self.is_first_footnote_on_page {
self.footnote_separator_overhead
} else {
0.0
};
let between_count = if self.is_first_footnote_on_page {
note_count.saturating_sub(1)
} else {
note_count
};
self.current_footnote_height
+ separator
+ self.footnote_between_notes_margin * between_count as f64
+ note_content_height
}
fn sync_current_page_footnote_area(&mut self) {
if self.current_footnote_height <= 0.0 {
return;
}
if let Some(page) = self.pages.last_mut() {
page.layout
.update_footnote_area(self.current_footnote_height);
}
}
fn push_new_page_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
let page = self.new_page_content_with_storage(Vec::new(), storage)?;
storage.push(&mut self.pages, page)?;
self.reset_for_new_page();
Ok(())
}
fn reset_for_new_page(&mut self) {
self.current_column = 0;
self.current_height = 0.0;
self.current_footnote_height = 0.0;
self.footnote_frames.clear();
self.is_first_footnote_on_page = true;
self.page_vpos_base = None;
self.page_has_block_table = false;
self.current_zone_y_offset = 0.0;
self.current_zone_layout = None;
self.on_first_multicolumn_page = false;
self.current_column_wrap_around_paras.clear();
self.current_column_wrap_anchors.clear();
}
fn new_page_content_with_storage<S: crate::storage::Storage>(
&self,
column_contents: Vec<ColumnContent>,
storage: &S,
) -> Result<PageContent, S::Error> {
Ok(PageContent {
page_index: self.pages.len() as u32,
page_number: 0,
page_number_restarted: false,
section_index: self.section_index,
layout: crate::storage::try_clone(&self.layout, storage)?,
column_contents,
active_header: None,
active_footer: None,
page_number_pos: None,
page_hide: None,
footnotes: Vec::new(),
active_master_page: None,
extra_master_pages: Vec::new(),
ladder_band_tables: Vec::new(),
})
}
}