-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathparser_aux.rs
408 lines (369 loc) · 14.6 KB
/
parser_aux.rs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#![cfg(feature = "nom_parser")]
use log::warn;
use crate::{
content::{Content, Operation},
document::Document,
encodings::Encoding,
error::ParseError,
object::Object::Name,
parser::ParserInput,
xref::{Xref, XrefEntry, XrefType},
Error, Result,
};
use crate::{parser, Dictionary, Object, ObjectId, Stream};
use std::{
collections::BTreeMap,
io::{Cursor, Read},
};
impl Content<Vec<Operation>> {
/// Decode content operations.
pub fn decode(data: &[u8]) -> Result<Self> {
parser::content(ParserInput::new_extra(data, "content operations"))
.ok_or(ParseError::InvalidContentStream.into())
}
}
impl Stream {
/// Decode content after decoding all stream filters.
pub fn decode_content(&self) -> Result<Content<Vec<Operation>>> {
Content::decode(&self.content)
}
}
impl Document {
/// Get decoded page content;
pub fn get_and_decode_page_content(&self, page_id: ObjectId) -> Result<Content<Vec<Operation>>> {
let content_data = self.get_page_content(page_id)?;
Content::decode(&content_data)
}
/// Add content to a page. All existing content will be unchanged.
pub fn add_to_page_content(&mut self, page_id: ObjectId, content: Content<Vec<Operation>>) -> Result<()> {
let content_data = Content::encode(&content)?;
self.add_page_contents(page_id, content_data)?;
Ok(())
}
pub fn extract_text(&self, page_numbers: &[u32]) -> Result<String> {
let text_fragments = self.extract_text_chunks(page_numbers);
let mut text = String::new();
for maybe_text_fragment in text_fragments.into_iter() {
let text_fragment = maybe_text_fragment?;
text.push_str(&text_fragment);
}
Ok(text)
}
pub fn extract_text_chunks(&self, page_numbers: &[u32]) -> Vec<Result<String>> {
let pages: BTreeMap<u32, (u32, u16)> = self.get_pages();
page_numbers
.iter()
.flat_map(|page_number| {
let result = self.extract_text_chunks_from_page(&pages, *page_number);
match result {
Ok(text_chunks) => text_chunks,
Err(err) => vec![Err(err)],
}
})
.collect()
}
fn extract_text_chunks_from_page(
&self, pages: &BTreeMap<u32, (u32, u16)>, page_number: u32,
) -> Result<Vec<Result<String>>> {
fn collect_text(text: &mut String, encoding: &Encoding, operands: &[Object]) -> Result<()> {
for operand in operands.iter() {
match operand {
Object::String(bytes, _) => {
text.push_str(&Document::decode_text(encoding, bytes)?);
}
Object::Array(arr) => {
collect_text(text, encoding, arr)?;
text.push(' ');
}
Object::Integer(i) => {
if *i < -100 {
text.push(' ');
}
}
_ => {}
}
}
Ok(())
}
let mut collected_chunks_and_errs: Vec<std::result::Result<String, Error>> = Vec::new();
let page_id = *pages.get(&page_number).ok_or(Error::PageNumberNotFound(page_number))?;
let fonts = self.get_page_fonts(page_id)?;
let encodings: BTreeMap<Vec<u8>, Encoding> = fonts
.into_iter()
.filter_map(|(name, font)| match font.get_font_encoding(self) {
Ok(it) => Some((name, it)),
Err(err) => {
collected_chunks_and_errs.push(Err(err));
None
}
})
.collect();
let content_data = self.get_page_content(page_id)?;
let content = Content::decode(&content_data)?;
// each text with different encoding is extracted as separate chunk
let mut current_encoding = None;
let mut current_text = String::new();
for operation in &content.operations {
match operation.operator.as_ref() {
"Tf" => {
let current_font = operation
.operands
.first()
.ok_or_else(|| Error::Syntax("missing font operand".to_string()))?
.as_name();
current_encoding = match current_font {
Ok(font) => encodings.get(font),
Err(err) => {
collected_chunks_and_errs.push(Err(err));
None
}
};
if !current_text.is_empty() {
collected_chunks_and_errs.push(Ok(current_text));
current_text = String::new();
}
}
"Tj" | "TJ" => match current_encoding {
Some(encoding) => {
let res = collect_text(&mut current_text, encoding, &operation.operands);
if let Err(err) = res {
collected_chunks_and_errs.push(Err(err));
}
}
None => warn!("Could not decode extracted text"),
},
"ET" => {
if !current_text.ends_with('\n') {
current_text.push('\n')
}
}
_ => {}
}
}
if !current_text.is_empty() {
collected_chunks_and_errs.push(Ok(current_text));
}
Ok(collected_chunks_and_errs)
}
pub fn replace_text(&mut self, page_number: u32, text: &str, other_text: &str) -> Result<()> {
let page = page_number.saturating_sub(1) as usize;
let page_id = self
.page_iter()
.nth(page)
.ok_or(Error::PageNumberNotFound(page_number))?;
let encodings: BTreeMap<Vec<u8>, Encoding> = self
.get_page_fonts(page_id)?
.into_iter()
.map(|(name, font)| font.get_font_encoding(self).map(|it| (name, it)))
.collect::<Result<BTreeMap<Vec<u8>, Encoding>>>()?;
let content_data = self.get_page_content(page_id)?;
let mut content = Content::decode(&content_data)?;
let mut current_encoding = None;
for operation in &mut content.operations {
match operation.operator.as_ref() {
"Tf" => {
let current_font = operation
.operands
.first()
.ok_or_else(|| Error::Syntax("missing font operand".to_string()))?
.as_name()?;
current_encoding = encodings.get(current_font);
}
"Tj" => match current_encoding {
Some(encoding) => try_to_replace_encoded_text(operation, encoding, text, other_text)?,
None => {
warn!("Could not decode extracted text, some of the occurances might not be properly replaced")
}
},
_ => {}
}
}
let modified_content = content.encode()?;
self.change_page_content(page_id, modified_content)
}
pub fn insert_image(
&mut self, page_id: ObjectId, img_object: Stream, position: (f32, f32), size: (f32, f32),
) -> Result<()> {
let img_id = self.add_object(img_object);
let img_name = format!("X{}", img_id.0);
self.add_xobject(page_id, img_name.as_bytes(), img_id)?;
let mut content = self.get_and_decode_page_content(page_id)?;
content.operations.push(Operation::new("q", vec![]));
content.operations.push(Operation::new(
"cm",
vec![
size.0.into(),
0.into(),
0.into(),
size.1.into(),
position.0.into(),
position.1.into(),
],
));
content
.operations
.push(Operation::new("Do", vec![Name(img_name.as_bytes().to_vec())]));
content.operations.push(Operation::new("Q", vec![]));
self.change_page_content(page_id, content.encode()?)
}
pub fn insert_form_object(&mut self, page_id: ObjectId, form_obj: Stream) -> Result<()> {
let form_id = self.add_object(form_obj);
let form_name = format!("X{}", form_id.0);
let mut content = self.get_and_decode_page_content(page_id)?;
content.operations.insert(0, Operation::new("q", vec![]));
content.operations.push(Operation::new("Q", vec![]));
content
.operations
.push(Operation::new("Do", vec![Name(form_name.as_bytes().to_vec())]));
let modified_content = content.encode()?;
self.add_xobject(page_id, form_name, form_id)?;
self.change_page_content(page_id, modified_content)
}
}
fn try_to_replace_encoded_text(
operation: &mut Operation, encoding: &Encoding, text_to_replace: &str, replacement: &str,
) -> Result<()> {
for bytes in operation.operands.iter_mut().flat_map(Object::as_str_mut) {
let decoded_text = Document::decode_text(encoding, bytes)?;
if decoded_text == text_to_replace {
let encoded_bytes = Document::encode_text(encoding, replacement);
*bytes = encoded_bytes;
}
}
Ok(())
}
/// Decode CrossReferenceStream
pub fn decode_xref_stream(mut stream: Stream) -> Result<(Xref, Dictionary)> {
if stream.is_compressed() {
stream.decompress()?;
}
let mut dict = stream.dict;
let mut reader = Cursor::new(stream.content);
let size = dict
.get(b"Size")
.and_then(Object::as_i64)
.map_err(|_| ParseError::InvalidXref)?;
let mut xref = Xref::new(size as u32, XrefType::CrossReferenceStream);
{
let section_indice = dict
.get(b"Index")
.and_then(parse_integer_array)
.unwrap_or_else(|_| vec![0, size]);
let field_widths = dict
.get(b"W")
.and_then(parse_integer_array)
.map_err(|_| ParseError::InvalidXref)?;
if field_widths.len() < 3
|| field_widths[0].is_negative()
|| field_widths[1].is_negative()
|| field_widths[2].is_negative()
{
return Err(ParseError::InvalidXref.into());
}
let mut bytes1 = vec![0_u8; field_widths[0] as usize];
let mut bytes2 = vec![0_u8; field_widths[1] as usize];
let mut bytes3 = vec![0_u8; field_widths[2] as usize];
for i in 0..section_indice.len() / 2 {
let start = section_indice[2 * i];
let count = section_indice[2 * i + 1];
for j in 0..count {
let entry_type = if !bytes1.is_empty() {
read_big_endian_integer(&mut reader, bytes1.as_mut_slice())?
} else {
1
};
match entry_type {
0 => {
// free object
read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
read_big_endian_integer(&mut reader, bytes3.as_mut_slice())?;
}
1 => {
// normal object
let offset = read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
let generation = if !bytes3.is_empty() {
read_big_endian_integer(&mut reader, bytes3.as_mut_slice())?
} else {
0
} as u16;
xref.insert((start + j) as u32, XrefEntry::Normal { offset, generation });
}
2 => {
// compressed object
let container = read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
let index = read_big_endian_integer(&mut reader, bytes3.as_mut_slice())? as u16;
xref.insert((start + j) as u32, XrefEntry::Compressed { container, index });
}
_ => {}
}
}
}
}
dict.remove(b"Length");
dict.remove(b"W");
dict.remove(b"Index");
Ok((xref, dict))
}
fn read_big_endian_integer(reader: &mut Cursor<Vec<u8>>, buffer: &mut [u8]) -> Result<u32> {
reader.read_exact(buffer)?;
let mut value = 0;
for &mut byte in buffer {
value = (value << 8) + u32::from(byte);
}
Ok(value)
}
fn parse_integer_array(array: &Object) -> Result<Vec<i64>> {
let array = array.as_array()?;
let mut out = Vec::with_capacity(array.len());
for n in array {
out.push(n.as_i64()?);
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::creator::tests::{create_document, create_document_with_texts, save_document};
#[cfg(not(feature = "async"))]
#[test]
fn load_and_save() {
// test load_from() and save_to()
use std::fs::File;
use std::io::Cursor;
// Create temporary folder to store file.
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("test_1_load_and_save.pdf");
let mut doc = create_document();
save_document(&file_path, &mut doc);
let in_file = File::open(file_path).unwrap();
let mut in_doc = Document::load_from(in_file).unwrap();
let out_buf = Vec::new();
let mut memory_cursor = Cursor::new(out_buf);
in_doc.save_to(&mut memory_cursor).unwrap();
// Check if saved file is not an empty bytes vector.
assert!(!memory_cursor.get_ref().is_empty());
}
#[test]
fn extract_text_chunks() {
let text1 = "Hello world!";
let text2 = "Ferris is the best!";
let doc = create_document_with_texts(&[text1, text2]);
let extracted_texts = doc.extract_text_chunks(&[1, 2]);
assert_eq!(extracted_texts.len(), 2);
assert_eq!(
[
extracted_texts[0].as_ref().unwrap().trim(),
extracted_texts[1].as_ref().unwrap().trim()
],
[text1, text2]
);
}
#[test]
fn extract_text_concatenates_text_from_multiple_pages() {
let text1 = "Hello world!";
let text2 = "Ferris is the best!";
let doc = create_document_with_texts(&[text1, text2]);
let extracted_text = doc.extract_text(&[1, 2]);
assert_eq!(extracted_text.unwrap(), format!("{text1}\n{text2}\n"));
}
}