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
use std::fs::File;
use std::io::{Result, Seek, Write, SeekFrom};
use std::path::Path;

use super::{Document, Object, Dictionary, Stream, StringFormat};
use super::Object::*;

impl Document {
	/// Save PDF document to specified file path.
	pub fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<File> {
		let mut file = File::create(path)?;

		file.write_all(format!("%PDF-{}\n", self.version).as_bytes())?;
		self.reference_table.clear();

		for (id, object) in &self.objects {
			let offset = file.seek(SeekFrom::Current(0)).unwrap();
			self.reference_table.insert(id.0, (id.1, offset));

			file.write_all(format!("{} {} obj{}", id.0, id.1, if Document::need_separator(object) {" "} else {""}).as_bytes())?;
			Document::write_object(&mut file, object)?;
			file.write_all(format!("{}endobj\n", if Document::need_end_separator(object) {" "} else {""}).as_bytes())?;
		}

		let xref_start = file.seek(SeekFrom::Current(0)).unwrap();
		self.write_xref(&mut file)?;
		self.write_trailer(&mut file)?;
		file.write_all(format!("\nstartxref\n{}\n%%EOF", xref_start).as_bytes())?;

		Ok(file)
	}

	fn need_separator(object: &Object) -> bool {
		match *object {
			Null => true,
			Boolean(_) => true,
			Integer(_) => true,
			Real(_) => true,
			Reference(_) => true,
			_ => false,
		}
	}

	fn need_end_separator(object: &Object) -> bool {
		match *object {
			Null => true,
			Boolean(_) => true,
			Integer(_) => true,
			Real(_) => true,
			Name(_) => true,
			Reference(_) => true,
			Object::Stream(_) => true,
			_ => false,
		}
	}

	fn write_object<'a>(file: &mut File, object: &'a Object) -> Result<()> {
		match *object {
			Null => file.write_all(b"null"),
			Boolean(ref value) => file.write_all(format!("{}", value).as_bytes()),
			Integer(ref value) => file.write_all(format!("{}", value).as_bytes()),
			Real(ref value) => file.write_all(format!("{}", value).as_bytes()),
			Name(ref name) => Document::write_name(file, name),
			String(ref text, ref format) => Document::write_string(file, text, format),
			Array(ref array) => Document::write_array(file, array),
			Object::Dictionary(ref dict) => Document::write_dictionary(file, dict),
			Object::Stream(ref stream) => Document::write_stream(file, stream),
			Reference(ref id) => file.write_all(format!("{} {} R", id.0, id.1).as_bytes()),
		}
	}

	fn write_name<'a>(file: &mut File, name: &'a str) -> Result<()> {
		file.write_all(b"/")?;
		for &byte in name.as_bytes() {
			// white-space and delimiter chars are encoded to # sequences
			if b" \t\n\r\x0C()<>[]{}/%#".contains(&byte) {
				file.write_all(format!("#{:02X}", byte).as_bytes())?;
			} else {
				file.write_all(&[byte])?;
			}
		}
		Ok(())
	}

	fn write_string<'a>(file: &mut File, text: &'a [u8], format: &'a StringFormat) -> Result<()> {
		match *format {
			// Within a Literal string, backslash (\) and unbalanced parentheses should be escaped.
			// This rule apply to each individual byte in a string object,
			// whether the string is interpreted as single-byte or multiple-byte character codes.
			// If an end-of-line marker appears within a literal string without a preceding backslash, the result is equivalent to \n.
			// So \r also need be escaped.
			StringFormat::Literal => {
				let mut escape_indice = Vec::new();
				let mut parentheses = Vec::new();
				for (index, &byte) in text.into_iter().enumerate() {
					match byte {
						b'(' => parentheses.push(index),
						b')' => {
							if parentheses.len() > 0 {
								parentheses.pop();
							} else {
								escape_indice.push(index);
							}
						}
						b'\\' | b'\r' => escape_indice.push(index),
						_ => continue,
					}
				}
				escape_indice.append(&mut parentheses);

				file.write_all(b"(")?;
				if escape_indice.len() > 0 {
					for (index, &byte) in text.into_iter().enumerate() {
						if escape_indice.contains(&index) {
							file.write_all(b"\\")?;
							file.write_all(&[if byte == b'\r' { b'r' } else { byte }])?;
						} else {
							file.write_all(&[byte])?;
						}
					}
				} else {
					file.write_all(text)?;
				}
				file.write_all(b")")?;
			}
			StringFormat::Hexadecimal => {
				file.write_all(b"<")?;
				for &byte in text {
					file.write_all(format!("{:02X}", byte).as_bytes())?;
				}
				file.write_all(b">")?;
			}
		}
		Ok(())
	}

	fn write_array<'a>(file: &mut File, array: &'a Vec<Object>) -> Result<()> {
		file.write_all(b"[")?;
		let mut first = true;
		for object in array {
			if first {
				first = false;
			} else if Document::need_separator(object) {
				file.write_all(b" ")?;
			}
			Document::write_object(file, object)?;
		}
		file.write_all(b"]")?;
		Ok(())
	}

	fn write_dictionary<'a>(file: &mut File, dictionary: &'a Dictionary) -> Result<()> {
		file.write_all(b"<<")?;
		for (key, value) in dictionary {
			Document::write_name(file, key)?;
			if Document::need_separator(value) {
				file.write_all(b" ")?;
			}
			Document::write_object(file, value)?;
		}
		file.write_all(b">>")?;
		Ok(())
	}

	fn write_stream<'a>(file: &mut File, stream: &'a Stream) -> Result<()> {
		Document::write_dictionary(file, &stream.dict)?;
		file.write_all(b"stream\n")?;
		file.write_all(&stream.content)?;
		file.write_all(b"endstream")?;
		Ok(())
	}

	fn write_xref(&self, file: &mut File) -> Result<()> {
		file.write_all(b"xref\n")?;
		file.write_all(format!("0 {}\n", self.max_id + 1).as_bytes())?;

		let mut write_xref_entry = |offset: u64, generation: u16, kind: char| {
			file.write_all(format!("{:>010} {:>05} {} \n", offset, generation, kind).as_bytes())
		};
		write_xref_entry(0, 65535, 'f')?;

		let mut obj_id = 1;
		while obj_id <= self.max_id {
			if let Some(&(generation, offset)) = self.reference_table.get(&obj_id) {
				write_xref_entry(offset, generation, 'n')?;
			} else {
				write_xref_entry(0, 65535, 'f')?;
			}
			obj_id += 1;
		}
		Ok(())
	}

	fn write_trailer(&mut self, file: &mut File) -> Result<()> {
		self.trailer.set("Size", (self.max_id + 1) as i64);
		file.write_all(b"trailer\n")?;
		Document::write_dictionary(file, &self.trailer)?;
		Ok(())
	}
}

#[test]
fn save_document() {
	let mut doc = Document::new();
	doc.version = "1.5".to_string();
	doc.objects.insert((1,0), Null);
	doc.objects.insert((2,0), Boolean(true));
	doc.objects.insert((3,0), Integer(3));
	doc.objects.insert((4,0), Real(0.5));
	doc.objects.insert((5,0), String("text((\r)".as_bytes().to_vec(), StringFormat::Literal));
	doc.objects.insert((6,0), String("text((\r)".as_bytes().to_vec(), StringFormat::Hexadecimal));
	doc.objects.insert((7,0), Name("name \t".to_string()));
	doc.objects.insert((8,0), Reference((1,0)));
	doc.objects.insert((9,2), Array(vec![Integer(1), Integer(2), Integer(3)]));
	doc.objects.insert((11,0), Stream(Stream::new(Dictionary::new(), vec![0x41, 0x42, 0x43])));
	let mut dict = Dictionary::new();
	dict.set("A", Null);
	dict.set("B", false);
	dict.set("C", Name("name".to_string()));
	doc.objects.insert((12,0), Object::Dictionary(dict));
	doc.max_id = 12;
	doc.save("test_0_save.pdf").unwrap();
}