use std::fmt;
pub use printpdf::{xobject::XObjectTransform, units::Mm};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(usize)]
pub enum FontVariant
{
Regular = 0,
Bold = 1,
Italic = 2,
BoldItalic = 3
}
pub const FONTVARIANT_VARIANTS: usize = 4;
impl fmt::Display for FontVariant
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
match self
{
Self::Regular => write!(f, "Regular"),
Self::Bold => write!(f, "Bold"),
Self::Italic => write!(f, "Italic"),
Self::BoldItalic => write!(f, "Bold Italic")
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FontPaths
{
pub regular: String,
pub bold: String,
pub italic: String,
pub bold_italic: String
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FontSizes
{
title_font_size: f32,
header_font_size: f32,
body_font_size: f32,
table_title_font_size: f32,
table_body_font_size: f32
}
impl FontSizes
{
pub fn new
(
title_font_size: f32,
header_font_size: f32,
body_font_size: f32,
table_title_font_size: f32,
table_body_font_size: f32
)
-> Result<Self, String>
{
if title_font_size < 0.0 { Err(String::from("Invalid title_font_size.")) }
else if header_font_size < 0.0 { Err(String::from("Invalid header_font_size.")) }
else if body_font_size < 0.0 { Err(String::from("Invalid body_font_size.")) }
else if table_title_font_size < 0.0 { Err(String::from("Invalid table_title_font_size.")) }
else if table_body_font_size < 0.0 { Err(String::from("Invalid table_body_font_size.")) }
else
{
Ok(Self
{
title_font_size: title_font_size,
header_font_size: header_font_size,
body_font_size: body_font_size,
table_title_font_size: table_title_font_size,
table_body_font_size: table_body_font_size
})
}
}
pub fn title_font_size(&self) -> f32 { self.title_font_size }
pub fn header_font_size(&self) -> f32 { self.header_font_size }
pub fn body_font_size(&self) -> f32 { self.body_font_size }
pub fn table_title_font_size(&self) -> f32 { self.table_title_font_size }
pub fn table_body_font_size(&self) -> f32 { self.table_body_font_size }
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FontScalars
{
regular: f32,
bold: f32,
italic: f32,
bold_italic: f32
}
impl FontScalars
{
pub fn new(regular: f32, bold: f32, italic: f32, bold_italic: f32) -> Result<Self, String>
{
if regular < 0.0 { Err(String::from("Invalid regular scalar.")) }
else if bold < 0.0 { Err(String::from("Invalid bold scalar.")) }
else if italic < 0.0 { Err(String::from("Invalid italic scalar.")) }
else if bold_italic < 0.0 { Err(String::from("Invalid bold_italic scalar.")) }
else
{
Ok(Self
{
regular: regular,
bold: bold,
italic: italic,
bold_italic: bold_italic
})
}
}
pub fn regular_scalar(&self) -> f32 { self.regular }
pub fn bold_scalar(&self) -> f32 { self.bold }
pub fn italic_scalar(&self) -> f32 { self.italic }
pub fn bold_italic_scalar(&self) -> f32 { self.bold_italic }
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct SpacingOptions
{
tab_amount: f32,
title_newline_amount: f32,
header_newline_amount: f32,
body_newline_amount: f32,
table_title_newline_amount: f32,
table_body_newline_amount: f32
}
impl SpacingOptions
{
pub fn new
(
tab_amount: f32,
title_newline_amount: f32,
header_newline_amount: f32,
body_newline_amount: f32,
table_title_newline_amount: f32,
table_body_newline_amount: f32
)
-> Result<Self, String>
{
if tab_amount < 0.0 { Err(String::from("Invalid tab_amount.")) }
else if title_newline_amount < 0.0 { Err(String::from("Invalid title_newline_amount.")) }
else if header_newline_amount < 0.0 { Err(String::from("Invalid header_newline_amount.")) }
else if body_newline_amount < 0.0 { Err(String::from("Invalid body_newline_amount.")) }
else if table_title_newline_amount < 0.0 { Err(String::from("Invalid table_title_newline_amount.")) }
else if table_body_newline_amount < 0.0 { Err(String::from("Invalid table_body_newline_amount.")) }
else
{
Ok(Self
{
tab_amount: tab_amount,
title_newline_amount: title_newline_amount,
header_newline_amount: header_newline_amount,
body_newline_amount: body_newline_amount,
table_title_newline_amount: table_title_newline_amount,
table_body_newline_amount: table_body_newline_amount
})
}
}
pub fn tab_amount(&self) -> f32 { self.tab_amount }
pub fn title_newline_amount(&self) -> f32 { self.title_newline_amount }
pub fn header_newline_amount(&self) -> f32 { self.header_newline_amount }
pub fn body_newline_amount(&self) -> f32 { self.body_newline_amount }
pub fn table_title_newline_amount(&self) -> f32 { self.table_title_newline_amount }
pub fn table_body_newline_amount(&self) -> f32 { self.table_body_newline_amount }
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TextColorOptions
{
pub title_color: (u8, u8, u8),
pub header_color: (u8, u8, u8),
pub body_color: (u8, u8, u8),
pub table_title_color: (u8, u8, u8),
pub table_body_color: (u8, u8, u8)
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct PageSizeOptions
{
width: f32,
height: f32,
left_margin: f32,
right_margin: f32,
top_margin: f32,
bottom_margin: f32
}
impl PageSizeOptions
{
pub fn new
(
width: f32,
height: f32,
left_margin: f32,
right_margin: f32,
top_margin: f32,
bottom_margin: f32
)
-> Result<Self, String>
{
if width <= 0.0
{
Err(String::from("Invalid page width."))
}
else if height <= 0.0
{
Err(String::from("Invalid page height."))
}
else if left_margin <= 0.0 || right_margin <= 0.0 || left_margin + right_margin >= width
{
Err(String::from("Invalid horizontal page margin."))
}
else if top_margin <= 0.0 || bottom_margin <= 0.0 || top_margin + bottom_margin >= height
{
Err(String::from("Invalid vertical page margin."))
}
else
{
Ok(Self
{
width: width,
height: height,
left_margin: left_margin,
right_margin: right_margin,
top_margin: top_margin,
bottom_margin: bottom_margin
})
}
}
pub fn width(&self) -> f32 { self.width }
pub fn height(&self) -> f32 { self.height }
pub fn left_margin(&self) -> f32 { self.left_margin }
pub fn right_margin(&self) -> f32 { self.right_margin }
pub fn top_margin(&self) -> f32 { self.top_margin }
pub fn bottom_margin(&self) -> f32 { self.bottom_margin }
pub fn has_same_margins(&self) -> bool
{
return self.left_margin == self.right_margin && self.left_margin == self.top_margin &&
self.left_margin == self.bottom_margin
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum HSide
{
Left,
Right
}
impl std::ops::Not for HSide
{
type Output = Self;
fn not(self) -> Self::Output
{
match self
{
Self::Left => Self::Right,
Self::Right => Self::Left
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PageNumberOptions
{
starting_side: HSide,
flips_sides: bool,
starting_num: i64,
font_variant: FontVariant,
font_size: f32,
newline_amount: f32,
color: (u8, u8, u8),
side_margin: f32,
bottom_margin: f32
}
impl PageNumberOptions
{
pub fn new
(
starting_side: HSide,
flips_sides: bool,
starting_num: i64,
font_variant: FontVariant,
font_size: f32,
newline_amount: f32,
color: (u8, u8, u8),
side_margin: f32,
bottom_margin: f32
)
-> Result<Self, String>
{
if font_size < 0.0
{
Err(String::from("Invalid font size."))
}
else if newline_amount < 0.0
{
Err(String::from("Invalid newline amount."))
}
else if side_margin < 0.0
{
Err(String::from("Invalid side margin."))
}
else if bottom_margin < 0.0
{
Err(String::from("Invalid bottom margin."))
}
else
{
Ok(Self
{
starting_side: starting_side,
flips_sides: flips_sides,
starting_num: starting_num,
font_variant: font_variant,
font_size: font_size,
newline_amount: newline_amount,
color: color,
side_margin: side_margin,
bottom_margin: bottom_margin
})
}
}
pub fn starting_side(&self) -> HSide { self.starting_side }
pub fn flips_sides(&self) -> bool { self.flips_sides }
pub fn starting_num(&self) -> i64 { self.starting_num }
pub fn font_variant(self) -> FontVariant { self.font_variant }
pub fn font_size(&self) -> f32 { self.font_size }
pub fn newline_amount(&self) -> f32 { self.newline_amount }
pub fn color(&self) -> (u8, u8, u8) { self.color }
pub fn side_margin(&self) -> f32 { self.side_margin }
pub fn bottom_margin(&self) -> f32 { self.bottom_margin }
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct TableOptions
{
horizontal_cell_margin: f32,
vertical_cell_margin: f32,
outer_horizontal_margin: f32,
outer_vertical_margin: f32,
off_row_color_lines_y_adjust_scalar: f32,
off_row_color_lines_height_scalar: f32,
off_row_color: (u8, u8, u8)
}
impl TableOptions
{
pub fn new
(
horizontal_cell_margin: f32,
vertical_cell_margin: f32,
outer_horizontal_margin: f32,
outer_vertical_margin: f32,
off_row_color_lines_y_adjust_scalar: f32,
off_row_color_lines_height_scalar: f32,
off_row_color: (u8, u8, u8)
)
-> Result<Self, String>
{
if horizontal_cell_margin < 0.0 { Err(String::from("Invalid horizontal_cell_margin.")) }
else if vertical_cell_margin < 0.0 { Err(String::from("Invalid vertical_cell_margin.")) }
else if outer_horizontal_margin < 0.0 { Err(String::from("Invalid outer_horizontal_margin.")) }
else if outer_vertical_margin < 0.0 { Err(String::from("Invalid outer_vertical_margin.")) }
else if off_row_color_lines_y_adjust_scalar < 0.0
{ Err(String::from("Invalid off_row_color_lines_y_adjust_scalar.")) }
else if off_row_color_lines_height_scalar < 0.0
{ Err(String::from("Invalid off_row_color_lines_height_scalar.")) }
else
{
Ok(Self
{
horizontal_cell_margin: horizontal_cell_margin,
vertical_cell_margin: vertical_cell_margin,
outer_horizontal_margin: outer_horizontal_margin,
outer_vertical_margin: outer_vertical_margin,
off_row_color_lines_y_adjust_scalar: off_row_color_lines_y_adjust_scalar,
off_row_color_lines_height_scalar: off_row_color_lines_height_scalar,
off_row_color: off_row_color
})
}
}
pub fn horizontal_cell_margin(&self) -> f32 { self.horizontal_cell_margin }
pub fn vertical_cell_margin(&self) -> f32 { self.vertical_cell_margin }
pub fn outer_horizontal_margin(&self) -> f32 { self.outer_horizontal_margin }
pub fn outer_vertical_margin(&self) -> f32 { self.outer_vertical_margin }
pub fn off_row_color_lines_y_adjust_scalar(&self) -> f32 { self.off_row_color_lines_y_adjust_scalar }
pub fn off_row_color_lines_height_scalar(&self) -> f32 { self.off_row_color_lines_height_scalar }
pub fn off_row_color(&self) -> (u8, u8, u8) { self.off_row_color }
}