use super::measurements::{cm, inch, mm};
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum PageSize {
#[default]
A4,
A3,
A5,
Letter,
Legal,
Tabloid,
Custom(f64, f64),
}
impl PageSize {
pub fn dimensions(&self, layout: PageLayout) -> (f64, f64) {
let (w, h) = match self {
PageSize::A4 => (595.28, 841.89),
PageSize::A3 => (841.89, 1190.55),
PageSize::A5 => (419.53, 595.28),
PageSize::Letter => (612.0, 792.0),
PageSize::Legal => (612.0, 1008.0),
PageSize::Tabloid => (792.0, 1224.0),
PageSize::Custom(w, h) => (*w, *h),
};
match layout {
PageLayout::Portrait => (w, h),
PageLayout::Landscape => (h, w),
}
}
pub fn from_mm(width: f64, height: f64) -> Self {
PageSize::Custom(mm(width), mm(height))
}
pub fn from_cm(width: f64, height: f64) -> Self {
PageSize::Custom(cm(width), cm(height))
}
pub fn from_inches(width: f64, height: f64) -> Self {
PageSize::Custom(inch(width), inch(height))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PageLayout {
#[default]
Portrait,
Landscape,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_page_size_dimensions() {
let (w, h) = PageSize::A4.dimensions(PageLayout::Portrait);
assert!((w - 595.28).abs() < 0.01);
assert!((h - 841.89).abs() < 0.01);
}
#[test]
fn test_page_size_landscape() {
let (w, h) = PageSize::A4.dimensions(PageLayout::Landscape);
assert!((w - 841.89).abs() < 0.01);
assert!((h - 595.28).abs() < 0.01);
}
#[test]
fn test_page_size_from_mm() {
let size = PageSize::from_mm(210.0, 297.0);
let (w, h) = size.dimensions(PageLayout::Portrait);
assert!((w - 595.28).abs() < 1.0);
assert!((h - 841.89).abs() < 1.0);
}
#[test]
fn test_page_size_from_inches() {
let size = PageSize::from_inches(8.5, 11.0);
let (w, h) = size.dimensions(PageLayout::Portrait);
assert_eq!(w, 612.0);
assert_eq!(h, 792.0);
}
}