use kurbo::Affine;
pub use pdfrum_render::{ColorMode, ColorScheme, Pixmap, TextAa};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RenderOptions {
pub transform: Affine,
pub color_mode: ColorMode,
pub text_aa: TextAa,
pub smooth_paths: bool,
pub interpolate_images: bool,
pub background: Option<peniko::Color>,
pub annotations: bool,
}
impl Default for RenderOptions {
fn default() -> Self {
RenderOptions {
transform: Affine::IDENTITY,
color_mode: ColorMode::default(),
text_aa: TextAa::default(),
smooth_paths: true,
interpolate_images: true,
background: None,
annotations: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
#[must_use]
pub struct RenderOptionsBuilder(RenderOptions);
impl RenderOptionsBuilder {
pub fn transform(mut self, transform: Affine) -> Self {
self.0.transform = transform;
self
}
pub fn scale(self, scale: f64) -> Self {
self.transform(Affine::scale(scale))
}
pub fn color_mode(mut self, color_mode: ColorMode) -> Self {
self.0.color_mode = color_mode;
self
}
pub fn grayscale(self) -> Self {
self.color_mode(ColorMode::Gray)
}
pub fn text_aa(mut self, text_aa: TextAa) -> Self {
self.0.text_aa = text_aa;
self
}
pub fn smooth_paths(mut self, smooth: bool) -> Self {
self.0.smooth_paths = smooth;
self
}
pub fn interpolate_images(mut self, interpolate: bool) -> Self {
self.0.interpolate_images = interpolate;
self
}
pub fn background(mut self, background: peniko::Color) -> Self {
self.0.background = Some(background);
self
}
pub fn annotations(mut self, annotations: bool) -> Self {
self.0.annotations = annotations;
self
}
#[must_use]
pub fn build(self) -> RenderOptions {
self.0
}
}
impl RenderOptions {
pub fn builder() -> RenderOptionsBuilder {
RenderOptionsBuilder::default()
}
#[must_use]
pub fn scaled(scale: f64) -> RenderOptions {
RenderOptions {
transform: Affine::scale(scale),
..RenderOptions::default()
}
}
#[must_use]
pub fn fit(width: f64, height: f64, max_width: u32, max_height: u32) -> RenderOptions {
if width <= 0.0 || height <= 0.0 {
return RenderOptions::default();
}
let scale = (f64::from(max_width) / width).min(f64::from(max_height) / height);
RenderOptions::scaled(scale)
}
pub(crate) fn to_inner(&self) -> pdfrum_render::RenderOptions {
pdfrum_render::RenderOptions::from(self)
}
}
impl From<&RenderOptions> for pdfrum_render::RenderOptions {
fn from(options: &RenderOptions) -> Self {
Self {
transform: options.transform,
color_mode: options.color_mode,
text_aa: options.text_aa,
no_path_smooth: !options.smooth_paths,
no_image_smooth: !options.interpolate_images,
background: options.background,
..Self::default()
}
}
}