use crate::page::PdfPoints;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PdfPagePaperStandardSize {
USLetterAnsiA,
USHalfLetter,
USGovernmentLetter,
USLegal,
USJuniorLegal,
USGovernmentLegal,
USLedgerTabloidAnsiB,
A0x4,
A0x2,
A0,
A1,
A2,
A3,
A4,
A4R,
A5,
A6,
A7,
A8,
A9,
A10,
B0,
B1,
B2,
B3,
B4,
B5,
B6,
B7,
B8,
B9,
B10,
C0,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
C8,
C9,
C10,
AnsiBPlus,
AnsiC,
AnsiD,
AnsiE,
ArchA,
ArchB,
ArchC,
ArchD,
ArchE,
}
impl PdfPagePaperStandardSize {
pub fn from_mm_dimensions(width: u32, height: u32) -> Option<PdfPagePaperStandardSize> {
match (width, height) {
(216, 279) => Some(PdfPagePaperStandardSize::USLetterAnsiA),
(140, 216) => Some(PdfPagePaperStandardSize::USHalfLetter),
(203, 254) => Some(PdfPagePaperStandardSize::USGovernmentLetter),
(216, 356) => Some(PdfPagePaperStandardSize::USLegal),
(127, 203) => Some(PdfPagePaperStandardSize::USJuniorLegal),
(216, 330) => Some(PdfPagePaperStandardSize::USGovernmentLegal),
(279, 432) => Some(PdfPagePaperStandardSize::USLedgerTabloidAnsiB),
(1682, 2378) => Some(PdfPagePaperStandardSize::A0x4),
(1189, 1682) => Some(PdfPagePaperStandardSize::A0x2),
(841, 1189) => Some(PdfPagePaperStandardSize::A0),
(594, 841) => Some(PdfPagePaperStandardSize::A1),
(420, 594) => Some(PdfPagePaperStandardSize::A2),
(297, 420) => Some(PdfPagePaperStandardSize::A3),
(210, 297) => Some(PdfPagePaperStandardSize::A4),
(297, 210) => Some(PdfPagePaperStandardSize::A4R),
(148, 210) => Some(PdfPagePaperStandardSize::A5),
(105, 148) => Some(PdfPagePaperStandardSize::A6),
(74, 105) => Some(PdfPagePaperStandardSize::A7),
(52, 74) => Some(PdfPagePaperStandardSize::A8),
(37, 52) => Some(PdfPagePaperStandardSize::A9),
(26, 37) => Some(PdfPagePaperStandardSize::A10),
(1000, 1414) => Some(PdfPagePaperStandardSize::B0),
(707, 1000) => Some(PdfPagePaperStandardSize::B1),
(500, 707) => Some(PdfPagePaperStandardSize::B2),
(353, 500) => Some(PdfPagePaperStandardSize::B3),
(250, 353) => Some(PdfPagePaperStandardSize::B4),
(176, 250) => Some(PdfPagePaperStandardSize::B5),
(125, 176) => Some(PdfPagePaperStandardSize::B6),
(88, 125) => Some(PdfPagePaperStandardSize::B7),
(62, 88) => Some(PdfPagePaperStandardSize::B8),
(44, 62) => Some(PdfPagePaperStandardSize::B9),
(31, 44) => Some(PdfPagePaperStandardSize::B10),
(917, 1297) => Some(PdfPagePaperStandardSize::C0),
(648, 917) => Some(PdfPagePaperStandardSize::C1),
(458, 648) => Some(PdfPagePaperStandardSize::C2),
(324, 458) => Some(PdfPagePaperStandardSize::C3),
(229, 324) => Some(PdfPagePaperStandardSize::C4),
(162, 229) => Some(PdfPagePaperStandardSize::C5),
(114, 162) => Some(PdfPagePaperStandardSize::C6),
(81, 114) => Some(PdfPagePaperStandardSize::C7),
(57, 81) => Some(PdfPagePaperStandardSize::C8),
(40, 57) => Some(PdfPagePaperStandardSize::C9),
(28, 40) => Some(PdfPagePaperStandardSize::C10),
(330, 483) => Some(PdfPagePaperStandardSize::AnsiBPlus),
(432, 559) => Some(PdfPagePaperStandardSize::AnsiC),
(559, 864) => Some(PdfPagePaperStandardSize::AnsiD),
(864, 1118) => Some(PdfPagePaperStandardSize::AnsiE),
(229, 305) => Some(PdfPagePaperStandardSize::ArchA),
(305, 457) => Some(PdfPagePaperStandardSize::ArchB),
(457, 610) => Some(PdfPagePaperStandardSize::ArchC),
(610, 914) => Some(PdfPagePaperStandardSize::ArchD),
(762, 1067) => Some(PdfPagePaperStandardSize::ArchE),
_ => None,
}
}
pub fn width(&self) -> PdfPoints {
PdfPoints::from_mm(match self {
PdfPagePaperStandardSize::USLetterAnsiA => 216.0,
PdfPagePaperStandardSize::USHalfLetter => 140.0,
PdfPagePaperStandardSize::USGovernmentLetter => 203.0,
PdfPagePaperStandardSize::USLegal => 216.0,
PdfPagePaperStandardSize::USJuniorLegal => 127.0,
PdfPagePaperStandardSize::USGovernmentLegal => 216.0,
PdfPagePaperStandardSize::USLedgerTabloidAnsiB => 279.0,
PdfPagePaperStandardSize::A0x4 => 1682.0,
PdfPagePaperStandardSize::A0x2 => 1189.0,
PdfPagePaperStandardSize::A0 => 841.0,
PdfPagePaperStandardSize::A1 => 594.0,
PdfPagePaperStandardSize::A2 => 420.0,
PdfPagePaperStandardSize::A3 => 297.0,
PdfPagePaperStandardSize::A4 => 210.0,
PdfPagePaperStandardSize::A4R => 297.0,
PdfPagePaperStandardSize::A5 => 148.0,
PdfPagePaperStandardSize::A6 => 105.0,
PdfPagePaperStandardSize::A7 => 74.0,
PdfPagePaperStandardSize::A8 => 52.0,
PdfPagePaperStandardSize::A9 => 37.0,
PdfPagePaperStandardSize::A10 => 26.0,
PdfPagePaperStandardSize::B0 => 1000.0,
PdfPagePaperStandardSize::B1 => 707.0,
PdfPagePaperStandardSize::B2 => 500.0,
PdfPagePaperStandardSize::B3 => 353.0,
PdfPagePaperStandardSize::B4 => 250.0,
PdfPagePaperStandardSize::B5 => 176.0,
PdfPagePaperStandardSize::B6 => 125.0,
PdfPagePaperStandardSize::B7 => 88.0,
PdfPagePaperStandardSize::B8 => 62.0,
PdfPagePaperStandardSize::B9 => 44.0,
PdfPagePaperStandardSize::B10 => 31.0,
PdfPagePaperStandardSize::C0 => 917.0,
PdfPagePaperStandardSize::C1 => 648.0,
PdfPagePaperStandardSize::C2 => 458.0,
PdfPagePaperStandardSize::C3 => 324.0,
PdfPagePaperStandardSize::C4 => 229.0,
PdfPagePaperStandardSize::C5 => 162.0,
PdfPagePaperStandardSize::C6 => 114.0,
PdfPagePaperStandardSize::C7 => 81.0,
PdfPagePaperStandardSize::C8 => 57.0,
PdfPagePaperStandardSize::C9 => 40.0,
PdfPagePaperStandardSize::C10 => 28.0,
PdfPagePaperStandardSize::AnsiBPlus => 330.0,
PdfPagePaperStandardSize::AnsiC => 432.0,
PdfPagePaperStandardSize::AnsiD => 559.0,
PdfPagePaperStandardSize::AnsiE => 864.0,
PdfPagePaperStandardSize::ArchA => 229.0,
PdfPagePaperStandardSize::ArchB => 305.0,
PdfPagePaperStandardSize::ArchC => 457.0,
PdfPagePaperStandardSize::ArchD => 610.0,
PdfPagePaperStandardSize::ArchE => 762.0,
})
}
pub fn height(&self) -> PdfPoints {
PdfPoints::from_mm(match self {
PdfPagePaperStandardSize::USLetterAnsiA => 279.0,
PdfPagePaperStandardSize::USHalfLetter => 216.0,
PdfPagePaperStandardSize::USGovernmentLetter => 254.0,
PdfPagePaperStandardSize::USLegal => 356.0,
PdfPagePaperStandardSize::USJuniorLegal => 203.0,
PdfPagePaperStandardSize::USGovernmentLegal => 330.0,
PdfPagePaperStandardSize::USLedgerTabloidAnsiB => 432.0,
PdfPagePaperStandardSize::A0x4 => 2378.0,
PdfPagePaperStandardSize::A0x2 => 1682.0,
PdfPagePaperStandardSize::A0 => 1189.0,
PdfPagePaperStandardSize::A1 => 841.0,
PdfPagePaperStandardSize::A2 => 594.0,
PdfPagePaperStandardSize::A3 => 420.0,
PdfPagePaperStandardSize::A4 => 297.0,
PdfPagePaperStandardSize::A4R => 210.0,
PdfPagePaperStandardSize::A5 => 210.0,
PdfPagePaperStandardSize::A6 => 148.0,
PdfPagePaperStandardSize::A7 => 105.0,
PdfPagePaperStandardSize::A8 => 74.0,
PdfPagePaperStandardSize::A9 => 52.0,
PdfPagePaperStandardSize::A10 => 37.0,
PdfPagePaperStandardSize::B0 => 1414.0,
PdfPagePaperStandardSize::B1 => 1000.0,
PdfPagePaperStandardSize::B2 => 707.0,
PdfPagePaperStandardSize::B3 => 500.0,
PdfPagePaperStandardSize::B4 => 353.0,
PdfPagePaperStandardSize::B5 => 250.0,
PdfPagePaperStandardSize::B6 => 176.0,
PdfPagePaperStandardSize::B7 => 125.0,
PdfPagePaperStandardSize::B8 => 88.0,
PdfPagePaperStandardSize::B9 => 62.0,
PdfPagePaperStandardSize::B10 => 44.0,
PdfPagePaperStandardSize::C0 => 1297.0,
PdfPagePaperStandardSize::C1 => 917.0,
PdfPagePaperStandardSize::C2 => 648.0,
PdfPagePaperStandardSize::C3 => 458.0,
PdfPagePaperStandardSize::C4 => 324.0,
PdfPagePaperStandardSize::C5 => 229.0,
PdfPagePaperStandardSize::C6 => 162.0,
PdfPagePaperStandardSize::C7 => 114.0,
PdfPagePaperStandardSize::C8 => 81.0,
PdfPagePaperStandardSize::C9 => 57.0,
PdfPagePaperStandardSize::C10 => 40.0,
PdfPagePaperStandardSize::AnsiBPlus => 483.0,
PdfPagePaperStandardSize::AnsiC => 559.0,
PdfPagePaperStandardSize::AnsiD => 864.0,
PdfPagePaperStandardSize::AnsiE => 1118.0,
PdfPagePaperStandardSize::ArchA => 305.0,
PdfPagePaperStandardSize::ArchB => 457.0,
PdfPagePaperStandardSize::ArchC => 610.0,
PdfPagePaperStandardSize::ArchD => 914.0,
PdfPagePaperStandardSize::ArchE => 1067.0,
})
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PdfPagePaperSize {
Portrait(PdfPagePaperStandardSize),
Landscape(PdfPagePaperStandardSize),
Custom(PdfPoints, PdfPoints),
}
impl PdfPagePaperSize {
#[inline]
pub fn from_points(width: PdfPoints, height: PdfPoints) -> Self {
let width_mm = width.to_mm().trunc() as u32;
let height_mm = height.to_mm().trunc() as u32;
match PdfPagePaperStandardSize::from_mm_dimensions(width_mm, height_mm) {
Some(size) => PdfPagePaperSize::Portrait(size),
None => {
match PdfPagePaperStandardSize::from_mm_dimensions(height_mm, width_mm) {
Some(size) => PdfPagePaperSize::Landscape(size),
None => {
PdfPagePaperSize::Custom(width, height)
}
}
}
}
}
#[inline]
pub fn from_inches(width: f32, height: f32) -> Self {
Self::from_points(
PdfPoints::from_inches(width),
PdfPoints::from_inches(height),
)
}
#[inline]
pub fn from_cm(width: f32, height: f32) -> Self {
Self::from_points(PdfPoints::from_cm(width), PdfPoints::from_cm(height))
}
#[inline]
pub fn from_mm(width: f32, height: f32) -> Self {
Self::from_points(PdfPoints::from_mm(width), PdfPoints::from_mm(height))
}
#[inline]
pub fn new_portrait(size: PdfPagePaperStandardSize) -> Self {
PdfPagePaperSize::Portrait(size)
}
#[inline]
pub fn new_landscape(size: PdfPagePaperStandardSize) -> Self {
PdfPagePaperSize::Landscape(size)
}
#[inline]
pub fn new_custom(width: PdfPoints, height: PdfPoints) -> Self {
PdfPagePaperSize::Custom(width, height)
}
#[inline]
pub fn a4() -> Self {
Self::new_portrait(PdfPagePaperStandardSize::A4)
}
#[inline]
pub fn a4r() -> Self {
Self::new_portrait(PdfPagePaperStandardSize::A4R)
}
#[inline]
pub fn a3() -> Self {
Self::new_portrait(PdfPagePaperStandardSize::A3)
}
pub fn rotate(self) -> Self {
match self {
PdfPagePaperSize::Portrait(size) => PdfPagePaperSize::Landscape(size),
PdfPagePaperSize::Landscape(size) => PdfPagePaperSize::Portrait(size),
PdfPagePaperSize::Custom(width, height) => PdfPagePaperSize::Custom(height, width),
}
}
pub fn landscape(&self) -> Self {
match self {
PdfPagePaperSize::Portrait(size) => PdfPagePaperSize::Landscape(*size),
PdfPagePaperSize::Landscape(_) => *self,
PdfPagePaperSize::Custom(width, height) => {
if height > width {
PdfPagePaperSize::Custom(*height, *width)
} else {
*self
}
}
}
}
pub fn portrait(&self) -> Self {
match self {
PdfPagePaperSize::Portrait(_) => *self,
PdfPagePaperSize::Landscape(size) => PdfPagePaperSize::Portrait(*size),
PdfPagePaperSize::Custom(width, height) => {
if width > height {
PdfPagePaperSize::Custom(*height, *width)
} else {
*self
}
}
}
}
pub fn width(&self) -> PdfPoints {
match self {
PdfPagePaperSize::Portrait(size) => size.width(),
PdfPagePaperSize::Landscape(size) => size.height(),
PdfPagePaperSize::Custom(width, _) => *width,
}
}
pub fn height(&self) -> PdfPoints {
match self {
PdfPagePaperSize::Portrait(size) => size.height(),
PdfPagePaperSize::Landscape(size) => size.width(),
PdfPagePaperSize::Custom(_, height) => *height,
}
}
}