use crate::enums::{TextAlignment, VerticalAlignment};
use crate::style::{PdfColor, PdfFont};
#[derive(Debug, Clone)]
pub struct PdfText {
pub content: String,
pub alignment: TextAlignment,
pub font: PdfFont,
pub color: PdfColor,
}
impl PdfText {
#[must_use]
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
alignment: TextAlignment::default(),
font: PdfFont::default(),
color: PdfColor::default(),
}
}
#[must_use]
pub fn font(mut self, font: PdfFont) -> Self {
self.font = font;
self
}
#[must_use]
pub const fn alignment(mut self, alignment: TextAlignment) -> Self {
self.alignment = alignment;
self
}
#[must_use]
pub const fn color(mut self, color: PdfColor) -> Self {
self.color = color;
self
}
}
#[derive(Debug, Clone)]
pub struct PdfTable {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
pub column_widths: Vec<f64>,
pub width: f64,
}
impl PdfTable {
#[must_use]
pub fn new(headers: Vec<String>) -> Self {
Self {
headers,
rows: Vec::new(),
column_widths: Vec::new(),
width: 0.0,
}
}
#[must_use]
pub fn row(mut self, row: Vec<String>) -> Self {
self.rows.push(row);
self
}
#[must_use]
pub fn rows(mut self, rows: Vec<Vec<String>>) -> Self {
self.rows.extend(rows);
self
}
#[must_use]
pub const fn width(mut self, width: f64) -> Self {
self.width = width;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct PdfTableCell {
pub content: String,
pub h_alignment: TextAlignment,
pub v_alignment: VerticalAlignment,
pub font: PdfFont,
pub color: PdfColor,
}
#[derive(Debug, Clone)]
pub struct PdfImage {
pub data: Vec<u8>,
pub width: f64,
pub height: f64,
}
impl PdfImage {
#[must_use]
pub fn from_bytes(data: Vec<u8>) -> Self {
Self {
data,
width: 0.0,
height: 0.0,
}
}
pub fn from_path(path: impl AsRef<std::path::Path>) -> crate::error::Result<Self> {
let data = std::fs::read(path)?;
Ok(Self::from_bytes(data))
}
}
#[derive(Debug, Clone, Copy)]
pub struct PdfLine {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
pub width: f64,
pub color: PdfColor,
}
#[derive(Debug, Clone, Copy)]
pub struct PdfRect {
pub x: f64,
pub y: f64,
pub w: f64,
pub h: f64,
pub border_width: f64,
pub border_color: PdfColor,
pub fill_color: Option<PdfColor>,
}
#[cfg(test)]
#[allow(clippy::uninlined_format_args, clippy::float_cmp)]
mod tests {
use super::*;
#[test]
fn pdf_text_new() {
let t = PdfText::new("hello");
assert_eq!(t.content, "hello");
}
#[test]
fn pdf_text_font() {
let t = PdfText::new("x").font(PdfFont::default());
let _ = format!("{:?}", t.font);
}
#[test]
fn pdf_text_alignment() {
let t = PdfText::new("x").alignment(TextAlignment::Center);
assert_eq!(t.alignment, TextAlignment::Center);
}
#[test]
fn pdf_text_color() {
let t = PdfText::new("x").color(PdfColor::default());
assert_eq!(t.color, PdfColor::default());
}
#[test]
fn pdf_text_debug_clone() {
let t = PdfText::new("test");
let cloned = t.clone();
assert_eq!(t.content, cloned.content);
let _ = format!("{:?}", t);
}
#[test]
fn pdf_table_new() {
let t = PdfTable::new(vec!["A".into(), "B".into()]);
assert_eq!(t.headers.len(), 2);
assert!(t.rows.is_empty());
}
#[test]
fn pdf_table_row() {
let t = PdfTable::new(vec!["A".into()]).row(vec!["1".into()]);
assert_eq!(t.rows.len(), 1);
}
#[test]
fn pdf_table_rows() {
let t = PdfTable::new(vec!["A".into()]).rows(vec![vec!["1".into()], vec!["2".into()]]);
assert_eq!(t.rows.len(), 2);
}
#[test]
fn pdf_table_width() {
let t = PdfTable::new(vec!["A".into()]).width(200.0);
assert_eq!(t.width, 200.0);
}
#[test]
fn pdf_table_debug_clone() {
let t = PdfTable::new(vec!["A".into()]);
let cloned = t.clone();
assert_eq!(t.headers, cloned.headers);
let _ = format!("{:?}", t);
}
#[test]
fn pdf_image_from_bytes() {
let img = PdfImage::from_bytes(vec![1, 2, 3]);
assert_eq!(img.data, vec![1, 2, 3]);
assert_eq!(img.width, 0.0);
assert_eq!(img.height, 0.0);
}
#[test]
fn pdf_image_debug_clone() {
let img = PdfImage::from_bytes(vec![1]);
let cloned = img.clone();
assert_eq!(img.data, cloned.data);
let _ = format!("{:?}", img);
}
#[test]
fn pdf_table_cell_default() {
let cell = PdfTableCell::default();
assert!(cell.content.is_empty());
}
#[test]
fn pdf_table_cell_debug_clone() {
let cell = PdfTableCell {
content: "x".into(),
..Default::default()
};
let cloned = cell.clone();
assert_eq!(cell.content, cloned.content);
let _ = format!("{:?}", cell);
}
#[test]
fn pdf_line_debug_copy() {
let line = PdfLine {
x1: 0.0,
y1: 0.0,
x2: 100.0,
y2: 100.0,
width: 1.0,
color: PdfColor::default(),
};
let copied = line;
assert_eq!(line.x2, copied.x2);
let _ = format!("{:?}", line);
}
#[test]
fn pdf_rect_debug_copy() {
let rect = PdfRect {
x: 0.0,
y: 0.0,
w: 100.0,
h: 50.0,
border_width: 1.0,
border_color: PdfColor::default(),
fill_color: None,
};
let copied = rect;
assert_eq!(rect.w, copied.w);
let _ = format!("{:?}", rect);
}
}