paperforge_layout/
document.rs1use paperforge_core::Metadata;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct PageSize {
5 pub width: f64,
6 pub height: f64,
7}
8
9impl PageSize {
10 pub fn a4() -> Self {
11 Self {
12 width: 595.0,
13 height: 842.0,
14 }
15 }
16
17 pub fn letter() -> Self {
18 Self {
19 width: 612.0,
20 height: 792.0,
21 }
22 }
23
24 pub fn legal() -> Self {
25 Self {
26 width: 612.0,
27 height: 1008.0,
28 }
29 }
30
31 pub fn tabloid() -> Self {
32 Self {
33 width: 792.0,
34 height: 1224.0,
35 }
36 }
37
38 pub fn a0() -> Self {
39 Self {
40 width: 2384.0,
41 height: 3370.0,
42 }
43 }
44
45 pub fn a1() -> Self {
46 Self {
47 width: 1684.0,
48 height: 2384.0,
49 }
50 }
51
52 pub fn a2() -> Self {
53 Self {
54 width: 1191.0,
55 height: 1684.0,
56 }
57 }
58
59 pub fn a3() -> Self {
60 Self {
61 width: 842.0,
62 height: 1191.0,
63 }
64 }
65
66 pub fn a5() -> Self {
67 Self {
68 width: 420.0,
69 height: 595.0,
70 }
71 }
72
73 pub fn custom(width: f64, height: f64) -> Self {
74 Self { width, height }
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq)]
79pub enum Compression {
80 None,
81 Fast,
82 Balanced,
83 Maximum,
84}
85
86#[derive(Debug, Clone)]
87pub struct SaveOptions {
88 pub compression: Compression,
89 pub deterministic: bool,
90}
91
92impl Default for SaveOptions {
93 fn default() -> Self {
94 Self {
95 compression: Compression::Balanced,
96 deterministic: false,
97 }
98 }
99}
100
101#[derive(Debug, Clone)]
102pub struct Page {
103 pub size: PageSize,
104 pub content: Vec<u8>,
105}
106
107impl Page {
108 pub fn new(size: PageSize) -> Self {
109 Self {
110 size,
111 content: Vec::new(),
112 }
113 }
114}
115
116#[derive(Debug, Clone)]
117pub struct Document {
118 pub pages: Vec<Page>,
119 pub metadata: Metadata,
120 pub save_options: SaveOptions,
121}
122
123impl Document {
124 pub fn new() -> Self {
125 Self {
126 pages: Vec::new(),
127 metadata: Metadata::new(),
128 save_options: SaveOptions::default(),
129 }
130 }
131
132 pub fn add_page(&mut self, size: PageSize) -> &mut Page {
133 self.pages.push(Page::new(size));
134 self.pages.last_mut().unwrap()
135 }
136
137 pub fn page_count(&self) -> usize {
138 self.pages.len()
139 }
140
141 pub fn metadata_mut(&mut self) -> &mut Metadata {
142 &mut self.metadata
143 }
144
145 pub fn save(&self, path: &std::path::Path) -> Result<(), std::io::Error> {
146 std::fs::write(
147 path,
148 b"%PDF-1.7\n%\xe2\xcf\xd3\xe2\n1 0 obj\n<< /Type /Catalog >>\nendobj\n%%EOF\n",
149 )?;
150 Ok(())
151 }
152}
153
154impl Default for Document {
155 fn default() -> Self {
156 Self::new()
157 }
158}