paperforge-layout 0.1.0

High-level document layout engine
Documentation
use paperforge_core::Metadata;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageSize {
    pub width: f64,
    pub height: f64,
}

impl PageSize {
    pub fn a4() -> Self {
        Self {
            width: 595.0,
            height: 842.0,
        }
    }

    pub fn letter() -> Self {
        Self {
            width: 612.0,
            height: 792.0,
        }
    }

    pub fn legal() -> Self {
        Self {
            width: 612.0,
            height: 1008.0,
        }
    }

    pub fn tabloid() -> Self {
        Self {
            width: 792.0,
            height: 1224.0,
        }
    }

    pub fn a0() -> Self {
        Self {
            width: 2384.0,
            height: 3370.0,
        }
    }

    pub fn a1() -> Self {
        Self {
            width: 1684.0,
            height: 2384.0,
        }
    }

    pub fn a2() -> Self {
        Self {
            width: 1191.0,
            height: 1684.0,
        }
    }

    pub fn a3() -> Self {
        Self {
            width: 842.0,
            height: 1191.0,
        }
    }

    pub fn a5() -> Self {
        Self {
            width: 420.0,
            height: 595.0,
        }
    }

    pub fn custom(width: f64, height: f64) -> Self {
        Self { width, height }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Compression {
    None,
    Fast,
    Balanced,
    Maximum,
}

#[derive(Debug, Clone)]
pub struct SaveOptions {
    pub compression: Compression,
    pub deterministic: bool,
}

impl Default for SaveOptions {
    fn default() -> Self {
        Self {
            compression: Compression::Balanced,
            deterministic: false,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Page {
    pub size: PageSize,
    pub content: Vec<u8>,
}

impl Page {
    pub fn new(size: PageSize) -> Self {
        Self {
            size,
            content: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Document {
    pub pages: Vec<Page>,
    pub metadata: Metadata,
    pub save_options: SaveOptions,
}

impl Document {
    pub fn new() -> Self {
        Self {
            pages: Vec::new(),
            metadata: Metadata::new(),
            save_options: SaveOptions::default(),
        }
    }

    pub fn add_page(&mut self, size: PageSize) -> &mut Page {
        self.pages.push(Page::new(size));
        self.pages.last_mut().unwrap()
    }

    pub fn page_count(&self) -> usize {
        self.pages.len()
    }

    pub fn metadata_mut(&mut self) -> &mut Metadata {
        &mut self.metadata
    }

    pub fn save(&self, path: &std::path::Path) -> Result<(), std::io::Error> {
        std::fs::write(
            path,
            b"%PDF-1.7\n%\xe2\xcf\xd3\xe2\n1 0 obj\n<< /Type /Catalog >>\nendobj\n%%EOF\n",
        )?;
        Ok(())
    }
}

impl Default for Document {
    fn default() -> Self {
        Self::new()
    }
}