use crate::styles::DocumentStyle;
pub struct PageFlow {
pub cursor_y_mm: f64,
pub page_height_mm: f64,
pub margin_top_mm: f64,
pub margin_bottom_mm: f64,
pub page_number: u32,
}
impl PageFlow {
pub fn new(style: &DocumentStyle) -> Self {
let (_, ph) = style.page_size.dimensions_mm();
Self {
cursor_y_mm: ph - style.margin_top_mm,
page_height_mm: ph,
margin_top_mm: style.margin_top_mm,
margin_bottom_mm: style.margin_bottom_mm,
page_number: 1,
}
}
pub fn would_overflow(&self, height_mm: f64) -> bool {
self.cursor_y_mm - height_mm < self.margin_bottom_mm
}
pub fn advance(&mut self, height_mm: f64) {
self.cursor_y_mm -= height_mm;
}
pub fn new_page(&mut self) {
self.cursor_y_mm = self.page_height_mm - self.margin_top_mm;
self.page_number += 1;
}
pub fn remaining_mm(&self) -> f64 {
self.cursor_y_mm - self.margin_bottom_mm
}
pub fn would_overflow_with_footnotes(
&self,
height_mm: f64,
reserved_footnotes_mm: f64,
) -> bool {
self.cursor_y_mm - height_mm < self.margin_bottom_mm + reserved_footnotes_mm
}
pub fn is_top_of_page(&self) -> bool {
(self.page_height_mm - self.margin_top_mm - self.cursor_y_mm).abs() < 0.5
}
}