1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#![deny(missing_docs)]
mod buf;
mod content;
mod font;
mod object;
mod stream;
mod structure;
pub mod writers {
use super::*;
pub use content::{ImageStream, Path, Text};
pub use font::{CidFont, CmapStream, FontDescriptor, Type0Font, Type1Font, Widths};
pub use structure::{Catalog, Page, Pages, Resources};
}
pub use content::{ColorSpace, Content, LineCapStyle};
pub use font::{CidFontType, FontFlags, SystemInfo, UnicodeCmap};
pub use object::*;
pub use stream::*;
use std::fmt::{self, Debug, Formatter};
use std::io::Write;
use buf::BufExt;
use writers::*;
pub struct PdfWriter {
buf: Vec<u8>,
offsets: Vec<(Ref, usize)>,
depth: usize,
indent: usize,
}
impl PdfWriter {
pub fn new(major: i32, minor: i32) -> Self {
Self::with_capacity(8 * 1024, major, minor)
}
pub fn with_capacity(capacity: usize, major: i32, minor: i32) -> Self {
let mut buf = Vec::with_capacity(capacity);
buf.push_bytes(b"%PDF-");
buf.push_int(major);
buf.push(b'.');
buf.push_int(minor);
buf.push_bytes(b"\n\n");
Self {
buf,
offsets: vec![],
depth: 0,
indent: 0,
}
}
pub fn set_indent(&mut self, indent: usize) {
self.indent = indent;
}
pub fn finish(mut self, catalog_id: Ref) -> Vec<u8> {
assert_eq!(self.depth, 0, "unfinished object");
let (xref_len, xref_offset) = self.xref_table();
self.trailer(catalog_id, xref_len, xref_offset);
self.buf
}
pub fn len(&self) -> usize {
self.buf.len()
}
fn xref_table(&mut self) -> (i32, usize) {
self.offsets.sort();
let xref_len = 1 + self.offsets.last().map_or(0, |p| p.0.get());
let xref_offset = self.buf.len();
self.buf.push_bytes(b"xref\n0 ");
self.buf.push_int(xref_len);
self.buf.push(b'\n');
let mut idx = 0;
let mut free = 0;
loop {
let start = idx;
let mut link = free + 1;
while self.offsets.get(idx).map_or(false, |(id, _)| link == id.get()) {
idx += 1;
link += 1;
}
let gen = if free == 0 { "65535" } else { "00000" };
write!(self.buf, "{:010} {} f\r\n", link % xref_len, gen).unwrap();
free = link;
for &(_, offset) in &self.offsets[start .. idx] {
write!(self.buf, "{:010} 00000 n\r\n", offset).unwrap();
}
if idx >= self.offsets.len() {
break;
}
}
(xref_len, xref_offset)
}
fn trailer(&mut self, catalog_id: Ref, xref_len: i32, xref_offset: usize) {
self.buf.push_bytes(b"trailer\n");
Dict::start(self, ())
.pair(Name(b"Size"), xref_len)
.pair(Name(b"Root"), catalog_id);
self.buf.push_bytes(b"\nstartxref\n");
write!(self.buf, "{}", xref_offset).unwrap();
self.buf.push_bytes(b"\n%%EOF");
}
fn push_indent(&mut self) {
let width = self.indent * self.depth;
for _ in 0 .. width {
self.buf.push(b' ');
}
}
}
impl PdfWriter {
pub fn indirect(&mut self, id: Ref) -> Obj<'_, IndirectGuard> {
let indirect = IndirectGuard::start(self, id);
Obj::new(self, indirect)
}
pub fn catalog(&mut self, id: Ref) -> Catalog<'_> {
Catalog::start(self.indirect(id))
}
pub fn pages(&mut self, id: Ref) -> Pages<'_> {
Pages::start(self.indirect(id))
}
pub fn page(&mut self, id: Ref) -> Page<'_> {
Page::start(self.indirect(id))
}
pub fn type1_font(&mut self, id: Ref) -> Type1Font<'_> {
Type1Font::start(self.indirect(id))
}
pub fn type0_font(&mut self, id: Ref) -> Type0Font<'_> {
Type0Font::start(self.indirect(id))
}
pub fn cid_font(&mut self, id: Ref, subtype: CidFontType) -> CidFont<'_> {
CidFont::start(self.indirect(id), subtype)
}
pub fn font_descriptor(&mut self, id: Ref) -> FontDescriptor<'_> {
FontDescriptor::start(self.indirect(id))
}
}
impl PdfWriter {
pub fn stream<'a>(&'a mut self, id: Ref, data: &'a [u8]) -> Stream<'a> {
let indirect = IndirectGuard::start(self, id);
Stream::start(self, data, indirect)
}
pub fn cmap<'a>(&'a mut self, id: Ref, cmap: &'a [u8]) -> CmapStream<'a> {
CmapStream::start(self.stream(id, cmap))
}
pub fn image<'a>(&'a mut self, id: Ref, samples: &'a [u8]) -> ImageStream<'a> {
ImageStream::start(self.stream(id, samples))
}
}
impl Debug for PdfWriter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.pad("PdfWriter(..)")
}
}
pub trait Guard {
fn finish(&self, writer: &mut PdfWriter);
}
impl Guard for () {
fn finish(&self, _: &mut PdfWriter) {}
}
pub struct IndirectGuard;
impl IndirectGuard {
pub(crate) fn start(w: &mut PdfWriter, id: Ref) -> Self {
assert_eq!(w.depth, 0);
w.depth += 1;
w.offsets.push((id, w.buf.len()));
w.buf.push_int(id.get());
w.buf.push_bytes(b" 0 obj\n");
w.push_indent();
Self
}
}
impl Guard for IndirectGuard {
fn finish(&self, w: &mut PdfWriter) {
w.depth -= 1;
w.buf.push_bytes(b"\nendobj\n\n");
}
}