-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpdf_document_parser.cpp
More file actions
272 lines (213 loc) · 8.31 KB
/
Copy pathpdf_document_parser.cpp
File metadata and controls
272 lines (213 loc) · 8.31 KB
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
#include <odr/internal/pdf/pdf_document_parser.hpp>
#include <odr/internal/crypto/crypto_util.hpp>
#include <odr/internal/pdf/pdf_cmap_parser.hpp>
#include <odr/internal/pdf/pdf_document.hpp>
#include <odr/internal/pdf/pdf_document_element.hpp>
#include <odr/internal/pdf/pdf_file_parser.hpp>
#include <ranges>
#include <sstream>
namespace odr::internal::pdf {
namespace {
Element *parse_page_or_pages(DocumentParser &parser,
const ObjectReference &reference,
Document &document, Element *parent);
Font *parse_font(DocumentParser &parser, const ObjectReference &reference,
Document &document) {
Font *font = document.create_element<Font>();
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
font->type = Type::font;
font->object_reference = reference;
font->object = Object(dictionary);
if (dictionary.has_key("ToUnicode")) {
IndirectObject to_unicode_obj =
parser.read_object(dictionary["ToUnicode"].as_reference());
std::string stream = parser.read_object_stream(to_unicode_obj);
std::string inflate = crypto::util::zlib_inflate(stream);
std::istringstream ss(inflate);
CMapParser cmap_parser(ss);
font->cmap = cmap_parser.parse_cmap();
}
return font;
}
Resources *parse_resources(DocumentParser &parser, const Object &object,
Document &document) {
auto *resources = document.create_element<Resources>();
Dictionary dictionary = parser.resolve_object_copy(object).as_dictionary();
resources->type = Type::resources;
resources->object = Object(dictionary);
if (!dictionary["Font"].is_null()) {
Dictionary font_table =
parser.resolve_object_copy(dictionary["Font"]).as_dictionary();
for (const auto &[key, value] : font_table) {
resources->font[key] = parse_font(parser, value.as_reference(), document);
}
}
return resources;
}
Annotation *parse_annotation(DocumentParser &parser,
const ObjectReference &reference,
Document &document) {
auto *annotation = document.create_element<Annotation>();
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
annotation->type = Type::annotation;
annotation->object_reference = reference;
annotation->object = Object(dictionary);
return annotation;
}
Page *parse_page(DocumentParser &parser, const ObjectReference &reference,
Document &document, Element *parent) {
Page *page = document.create_element<Page>();
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
page->type = Type::page;
page->object_reference = reference;
page->object = Object(dictionary);
page->parent = dynamic_cast<Pages *>(parent);
page->resources = parse_resources(parser, dictionary["Resources"], document);
if (dictionary["Contents"].is_reference()) {
page->contents_reference = {dictionary["Contents"].as_reference()};
} else {
for (const Object &e : dictionary["Contents"].as_array()) {
page->contents_reference.push_back(e.as_reference());
}
}
if (dictionary.has_key("Annots")) {
// TODO why rvalue not working?
Array annotations =
parser.resolve_object_copy(dictionary["Annots"]).as_array();
for (const Object &annotation : annotations) {
page->annotations.push_back(
parse_annotation(parser, annotation.as_reference(), document));
}
}
return page;
}
Pages *parse_pages(DocumentParser &parser, const ObjectReference &reference,
Document &document) {
auto *pages = document.create_element<Pages>();
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
pages->type = Type::pages;
pages->object_reference = reference;
pages->object = Object(dictionary);
pages->count = dictionary["Count"].as_integer();
for (const Object &kid : dictionary["Kids"].as_array()) {
pages->kids.push_back(
parse_page_or_pages(parser, kid.as_reference(), document, pages));
}
return pages;
}
Element *parse_page_or_pages(DocumentParser &parser,
const ObjectReference &reference,
Document &document, Element *parent) {
// TODO we are parsing twice
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
const std::string &type = dictionary["Type"].as_string();
if (type == "Pages") {
return parse_pages(parser, reference, document);
}
if (type == "Page") {
return parse_page(parser, reference, document, parent);
}
throw std::runtime_error("unknown element");
}
Catalog *parse_catalog(DocumentParser &parser, const ObjectReference &reference,
Document &document) {
auto *catalog = document.create_element<Catalog>();
IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
const ObjectReference &pages_reference = dictionary["Pages"].as_reference();
catalog->type = Type::catalog;
catalog->object_reference = reference;
catalog->object = Object(dictionary);
catalog->pages = parse_pages(parser, pages_reference, document);
return catalog;
}
} // namespace
DocumentParser::DocumentParser(std::istream &in) : m_parser(in) {}
std::istream &DocumentParser::in() { return m_parser.in(); }
FileParser &DocumentParser::parser() { return m_parser; }
const Xref &DocumentParser::xref() const { return m_xref; }
const IndirectObject &
DocumentParser::read_object(const ObjectReference &reference) {
if (const auto it = m_objects.find(reference); it != std::end(m_objects)) {
return it->second;
}
const std::uint32_t position = m_xref.table.at(reference).position;
in().seekg(position);
IndirectObject object = parser().read_indirect_object();
return m_objects.emplace(reference, std::move(object)).first->second;
}
std::string
DocumentParser::read_object_stream(const ObjectReference &reference) {
return read_object_stream(read_object(reference));
}
std::string DocumentParser::read_object_stream(const IndirectObject &object) {
const Object length = object.object.as_dictionary()["Length"];
std::uint32_t size = 0;
if (length.is_integer()) {
size = length.as_integer();
} else if (length.is_reference()) {
size = read_object(length.as_reference()).object.as_integer();
} else {
throw std::runtime_error("unknown length property");
}
in().seekg(object.stream_position.value());
return m_parser.read_stream(static_cast<std::int32_t>(size));
}
std::unique_ptr<Document> DocumentParser::parse_document() {
parser().seek_start_xref();
const StartXref start_xref = parser().read_start_xref();
std::uint32_t xref_position = start_xref.start;
std::optional<Trailer> trailer;
while (true) {
in().seekg(xref_position);
m_xref.append(parser().read_xref());
parser().parser().skip_whitespace();
Trailer new_trailer = parser().read_trailer();
if (!trailer) {
trailer = new_trailer;
}
if (new_trailer.dictionary.has_key("Prev")) {
xref_position = new_trailer.dictionary["Prev"].as_integer();
continue;
}
break;
}
auto document = std::make_unique<Document>();
document->catalog =
parse_catalog(*this, trailer->root_reference(), *document);
return document;
}
void DocumentParser::resolve_object(Object &object) {
if (object.is_reference()) {
object = read_object(object.as_reference()).object;
}
}
void DocumentParser::deep_resolve_object(Object &object) {
if (object.is_reference()) {
object = read_object(object.as_reference()).object;
} else if (object.is_array()) {
for (Object &e : object.as_array()) {
deep_resolve_object(e);
}
} else if (object.is_dictionary()) {
for (Object &v : object.as_dictionary() | std::views::values) {
deep_resolve_object(v);
}
}
}
Object DocumentParser::resolve_object_copy(const Object &object) {
Object result = object;
resolve_object(result);
return result;
}
Object DocumentParser::deep_resolve_object_copy(const Object &object) {
Object result = object;
deep_resolve_object(result);
return result;
}
} // namespace odr::internal::pdf