use serde::{Deserialize, Serialize};
use super::{Element, RenderContext, RenderResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Orientation {
#[default]
Portrait,
Landscape,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SectionMargins {
pub top_mm: f64,
pub bottom_mm: f64,
pub left_mm: f64,
pub right_mm: f64,
}
impl SectionMargins {
pub fn uniform(mm: f64) -> Self {
Self { top_mm: mm, bottom_mm: mm, left_mm: mm, right_mm: mm }
}
pub fn symmetric(vertical_mm: f64, horizontal_mm: f64) -> Self {
Self {
top_mm: vertical_mm,
bottom_mm: vertical_mm,
left_mm: horizontal_mm,
right_mm: horizontal_mm,
}
}
}
impl Default for SectionMargins {
fn default() -> Self {
Self::symmetric(25.0, 25.0)
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SectionBreak {
pub orientation: Orientation,
#[serde(skip_serializing_if = "Option::is_none")]
pub margins: Option<SectionMargins>,
}
impl SectionBreak {
pub fn portrait() -> Self {
Self { orientation: Orientation::Portrait, margins: None }
}
pub fn landscape() -> Self {
Self { orientation: Orientation::Landscape, margins: None }
}
pub fn with_margins(mut self, margins: SectionMargins) -> Self {
self.margins = Some(margins);
self
}
}
impl Element for SectionBreak {
fn estimated_height_mm(&self) -> f64 {
0.0
}
fn render(&self, ctx: &mut RenderContext) -> crate::Result<RenderResult> {
ctx.force_page_break = true;
Ok(RenderResult::done())
}
}