use crate::error::Result;
use crate::graphics::Image;
use crate::layout::{FlowLayout, PageConfig, RichText};
use crate::text::{Font, Table};
use crate::Document;
use std::sync::Arc;
pub struct DocumentBuilder {
layout: FlowLayout,
}
impl DocumentBuilder {
pub fn a4() -> Self {
Self {
layout: FlowLayout::new(PageConfig::a4()),
}
}
pub fn new(config: PageConfig) -> Self {
Self {
layout: FlowLayout::new(config),
}
}
pub fn add_text(mut self, text: &str, font: Font, font_size: f64) -> Self {
self.layout.add_text(text, font, font_size);
self
}
pub fn add_text_with_line_height(
mut self,
text: &str,
font: Font,
font_size: f64,
line_height: f64,
) -> Self {
self.layout
.add_text_with_line_height(text, font, font_size, line_height);
self
}
pub fn add_spacer(mut self, points: f64) -> Self {
self.layout.add_spacer(points);
self
}
pub fn add_table(mut self, table: Table) -> Self {
self.layout.add_table(table);
self
}
pub fn add_image(
mut self,
name: &str,
image: Arc<Image>,
max_width: f64,
max_height: f64,
) -> Self {
self.layout.add_image(name, image, max_width, max_height);
self
}
pub fn add_image_centered(
mut self,
name: &str,
image: Arc<Image>,
max_width: f64,
max_height: f64,
) -> Self {
self.layout
.add_image_centered(name, image, max_width, max_height);
self
}
pub fn add_rich_text(mut self, rich: RichText) -> Self {
self.layout.add_rich_text(rich);
self
}
pub fn build(self) -> Result<Document> {
let mut doc = Document::new();
self.layout.build_into(&mut doc)?;
Ok(doc)
}
}