Debian Codegraph @

cpp/tests/poppler-dump.cpp

raw
1 /*
2 * Copyright (C) 2009-2010, Pino Toscano <[email protected]>
3 * Copyright (C) 2017-2019, 2022, Albert Astals Cid <[email protected]>
4 * Copyright (C) 2017, Jason Alan Palmer <[email protected]>
5 * Copyright (C) 2018, 2020, Suzuki Toshiya <[email protected]>
6 * Copyright (C) 2019, Masamichi Hosoda <[email protected]>
7 * Copyright (C) 2020, Jiri Jakes <[email protected]>
8 * Copyright (C) 2025, Zsombor Hollay-Horvath <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 #include <goo/glibc.h>
26 #include <poppler-destination.h>
27 #include <poppler-document.h>
28 #include <poppler-embedded-file.h>
29 #include <poppler-font.h>
30 #include <poppler-page.h>
31 #include <poppler-toc.h>
32 #include <poppler-version.h>
33
34 #include <cstdlib>
35 #include <cstring>
36 #include <ctime>
37 #include <algorithm>
38 #include <iomanip>
39 #include <ios>
40 #include <iostream>
41 #include <map>
42 #include <memory>
43 #include <sstream>
44
45 #include "parseargs.h"
46
47 #include "config.h"
48
49 static const int out_width = 30;
50
51 bool show_all = false;
52 bool show_info = false;
53 bool show_perm = false;
54 bool show_metadata = false;
55 bool show_toc = false;
56 bool show_fonts = false;
57 bool show_embedded_files = false;
58 bool show_pages = false;
59 bool show_destinations = false;
60 bool show_help = false;
61 bool show_version = false;
62 char show_text[32];
63 bool show_text_list = false;
64 bool show_text_list_with_font = false;
65 poppler::page::text_layout_enum show_text_layout = poppler::page::physical_layout;
66
67 static const ArgDesc the_args[] = { { "--show-all", argFlag, &show_all, 0, "show all the available information" },
68 { "--show-info", argFlag, &show_info, 0, "show general document information" },
69 { "--show-perm", argFlag, &show_perm, 0, "show document permissions" },
70 { "--show-metadata", argFlag, &show_metadata, 0, "show document metadata" },
71 { "--show-toc", argFlag, &show_toc, 0, "show the TOC" },
72 { "--show-fonts", argFlag, &show_fonts, 0, "show the document fonts" },
73 { "--show-embedded-files", argFlag, &show_embedded_files, 0, "show the document-level embedded files" },
74 { "--show-pages", argFlag, &show_pages, 0, "show pages information" },
75 { "--show-destinations", argFlag, &show_destinations, 0, "show named destinations" },
76 { "--show-text", argString, &show_text, sizeof(show_text), "show text (physical|raw|none) extracted from all pages" },
77 { "--show-text-list", argFlag, &show_text_list, 0, "show text list (experimental)" },
78 { "--show-text-list-with-font", argFlag, &show_text_list_with_font, 0, "show text list with font info (experimental)" },
79 { "-h", argFlag, &show_help, 0, "print usage information" },
80 { "--help", argFlag, &show_help, 0, "print usage information" },
81 { "--version", argFlag, &show_version, 0, "print poppler version" },
82 { nullptr, argFlag, nullptr, 0, nullptr } };
83
84 static void error(const std::string &msg)
85 {
86 std::cerr << "Error: " << msg << std::endl;
87 std::cerr << "Exiting..." << std::endl;
88 exit(1);
89 }
90
91 static std::ostream &operator<<(std::ostream &stream, const poppler::ustring &str)
92 {
93 const poppler::byte_array ba = str.to_utf8();
94 for (const char c : ba) {
95 stream << c;
96 }
97 return stream;
98 }
99
100 static std::string out_date(std::time_t date)
101 {
102 if (date != std::time_t(-1)) {
103 struct tm time;
104 gmtime_r(&date, &time);
105 struct tm *t = &time;
106 char buf[32];
107 strftime(buf, sizeof(buf) - 1, "%d/%m/%Y %H:%M:%S", t);
108 return std::string(buf);
109 }
110 return std::string("n/a");
111 }
112
113 static std::string out_size(int size)
114 {
115 if (size >= 0) {
116 std::ostringstream ss;
117 ss << size;
118 return ss.str();
119 }
120 return std::string("n/a");
121 }
122
123 static char charToHex(int x)
124 {
125 return x < 10 ? x + '0' : x - 10 + 'a';
126 }
127
128 static std::string out_hex_string(const poppler::byte_array &str)
129 {
130 std::string ret(str.size() * 2, '\0');
131 const char *str_p = &str[0];
132 for (unsigned int i = 0; i < str.size(); ++i, ++str_p) {
133 ret[i * 2] = charToHex((*str_p & 0xf0) >> 4);
134 ret[i * 2 + 1] = charToHex(*str_p & 0xf);
135 }
136 return ret;
137 }
138
139 static std::string out_page_orientation(poppler::page::orientation_enum o)
140 {
141 switch (o) {
142 case poppler::page::landscape:
143 return "landscape (90)";
144 case poppler::page::portrait:
145 return "portrait (0)";
146 case poppler::page::seascape:
147 return "seascape (270)";
148 case poppler::page::upside_down:
149 return "upside_downs (180)";
150 };
151 return "<unknown orientation>";
152 }
153
154 static std::string out_font_info_type(poppler::font_info::type_enum t)
155 {
156 #define OUT_FONT_TYPE(thetype) \
157 case poppler::font_info::thetype: \
158 return #thetype
159 switch (t) {
160 OUT_FONT_TYPE(unknown);
161 OUT_FONT_TYPE(type1);
162 OUT_FONT_TYPE(type1c);
163 OUT_FONT_TYPE(type1c_ot);
164 OUT_FONT_TYPE(type3);
165 OUT_FONT_TYPE(truetype);
166 OUT_FONT_TYPE(truetype_ot);
167 OUT_FONT_TYPE(cid_type0);
168 OUT_FONT_TYPE(cid_type0c);
169 OUT_FONT_TYPE(cid_type0c_ot);
170 OUT_FONT_TYPE(cid_truetype);
171 OUT_FONT_TYPE(cid_truetype_ot);
172 }
173 return "<unknown font type>";
174 #undef OUT_FONT_TYPE
175 }
176
177 static void print_info(poppler::document *doc)
178 {
179 std::cout << "Document information:" << std::endl;
180 int major = 0, minor = 0;
181 doc->get_pdf_version(&major, &minor);
182 std::cout << std::setw(out_width) << "PDF version"
183 << ": " << major << "." << minor << std::endl;
184 std::string permanent_id, update_id;
185 if (doc->get_pdf_id(&permanent_id, &update_id)) {
186 std::cout << std::setw(out_width) << "PDF IDs"
187 << ": P: " << permanent_id << " - U: " << update_id << std::endl;
188 } else {
189 std::cout << std::setw(out_width) << "PDF IDs"
190 << ": <none>" << std::endl;
191 }
192 const std::vector<std::string> keys = doc->info_keys();
193 std::vector<std::string>::const_iterator key_it = keys.begin(), key_end = keys.end();
194 for (; key_it != key_end; ++key_it) {
195 std::cout << std::setw(out_width) << *key_it << ": " << doc->info_key(*key_it) << std::endl;
196 }
197 std::cout << std::setw(out_width) << "Date (creation)"
198 << ": " << out_date(doc->info_date_t("CreationDate")) << std::endl;
199 std::cout << std::setw(out_width) << "Date (modification)"
200 << ": " << out_date(doc->info_date_t("ModDate")) << std::endl;
201 std::cout << std::setw(out_width) << "Number of pages"
202 << ": " << doc->pages() << std::endl;
203 std::cout << std::setw(out_width) << "Linearized"
204 << ": " << doc->is_linearized() << std::endl;
205 std::cout << std::setw(out_width) << "Encrypted"
206 << ": " << doc->is_encrypted() << std::endl;
207 std::cout << std::endl;
208 }
209
210 static void print_perm(poppler::document *doc)
211 {
212 std::cout << "Document permissions:" << std::endl;
213 #define OUT_PERM(theperm) std::cout << std::setw(out_width) << #theperm << ": " << doc->has_permission(poppler::perm_##theperm) << std::endl
214 OUT_PERM(print);
215 OUT_PERM(change);
216 OUT_PERM(copy);
217 OUT_PERM(add_notes);
218 OUT_PERM(fill_forms);
219 OUT_PERM(accessibility);
220 OUT_PERM(assemble);
221 OUT_PERM(print_high_resolution);
222 std::cout << std::endl;
223 #undef OUT_PERM
224 }
225
226 static void print_metadata(poppler::document *doc)
227 {
228 std::cout << std::setw(out_width) << "Metadata"
229 << ":" << std::endl
230 << doc->metadata() << std::endl;
231 std::cout << std::endl;
232 }
233
234 static void print_toc_item(poppler::toc_item *item, int indent)
235 {
236 std::cout << std::setw(indent * 2) << " "
237 << "+ " << item->title() << " (" << item->is_open() << ")" << std::endl;
238 poppler::toc_item::iterator it = item->children_begin(), it_end = item->children_end();
239 for (; it != it_end; ++it) {
240 print_toc_item(*it, indent + 1);
241 }
242 }
243
244 static void print_toc(poppler::toc *doctoc)
245 {
246 std::cout << "Document TOC:" << std::endl;
247 if (doctoc) {
248 print_toc_item(doctoc->root(), 0);
249 } else {
250 std::cout << "<no TOC>" << std::endl;
251 }
252 std::cout << std::endl;
253 }
254
255 static void print_fonts(poppler::document *doc)
256 {
257 std::cout << "Document fonts:" << std::endl;
258 std::vector<poppler::font_info> fl = doc->fonts();
259 if (!fl.empty()) {
260 std::vector<poppler::font_info>::const_iterator it = fl.begin(), it_end = fl.end();
261 const std::ios_base::fmtflags f = std::cout.flags();
262 std::left(std::cout);
263 for (; it != it_end; ++it) {
264 std::cout << " " << std::setw(out_width + 10) << it->name() << " " << std::setw(15) << out_font_info_type(it->type()) << " " << std::setw(5) << it->is_embedded() << " " << std::setw(5) << it->is_subset() << " " << it->file()
265 << std::endl;
266 }
267 std::cout.flags(f);
268 } else {
269 std::cout << "<no fonts>" << std::endl;
270 }
271 std::cout << std::endl;
272 }
273
274 static void print_embedded_files(poppler::document *doc)
275 {
276 std::cout << "Document embedded files:" << std::endl;
277 std::vector<poppler::embedded_file *> ef = doc->embedded_files();
278 if (!ef.empty()) {
279 std::vector<poppler::embedded_file *>::const_iterator it = ef.begin(), it_end = ef.end();
280 const std::ios_base::fmtflags flags = std::cout.flags();
281 std::left(std::cout);
282 for (; it != it_end; ++it) {
283 poppler::embedded_file *f = *it;
284 std::cout << " " << std::setw(out_width + 10) << f->unicodeName() << " " << std::setw(10) << out_size(f->size()) << " " << std::setw(20) << out_date(f->creation_date_t()) << " " << std::setw(20)
285 << out_date(f->modification_date_t()) << std::endl
286 << " ";
287 if (f->description().empty()) {
288 std::cout << "<no description>";
289 } else {
290 std::cout << f->description();
291 }
292 std::cout << std::endl
293 << " " << std::setw(35) << (f->checksum().empty() ? std::string("<no checksum>") : out_hex_string(f->checksum())) << " " << (f->mime_type().empty() ? std::string("<no mime type>") : f->mime_type()) << std::endl;
294 }
295 std::cout.flags(flags);
296 } else {
297 std::cout << "<no embedded files>" << std::endl;
298 }
299 std::cout << std::endl;
300 }
301
302 static void print_page(poppler::page *p)
303 {
304 if (p) {
305 std::cout << std::setw(out_width) << "Rect"
306 << ": " << p->page_rect() << std::endl;
307 std::cout << std::setw(out_width) << "Label"
308 << ": " << p->label() << std::endl;
309 std::cout << std::setw(out_width) << "Duration"
310 << ": " << p->duration() << std::endl;
311 std::cout << std::setw(out_width) << "Orientation"
312 << ": " << out_page_orientation(p->orientation()) << std::endl;
313 } else {
314 std::cout << std::setw(out_width) << "Broken Page. Could not be parsed" << std::endl;
315 }
316 std::cout << std::endl;
317 }
318
319 static void print_destination(const poppler::destination *d)
320 {
321 if (d) {
322 std::cout << std::setw(out_width) << "Type"
323 << ": ";
324 switch (d->type()) {
325 case poppler::destination::unknown:
326 std::cout << "unknown" << std::endl;
327 break;
328 case poppler::destination::xyz:
329 std::cout << "xyz" << std::endl
330 << std::setw(out_width) << "Page"
331 << ": " << d->page_number() << std::endl
332 << std::setw(out_width) << "Left"
333 << ": " << d->left() << std::endl
334 << std::setw(out_width) << "Top"
335 << ": " << d->top() << std::endl
336 << std::setw(out_width) << "Zoom"
337 << ": " << d->zoom() << std::endl;
338 break;
339 case poppler::destination::fit:
340 std::cout << "fit" << std::endl
341 << std::setw(out_width) << "Page"
342 << ": " << d->page_number() << std::endl;
343 break;
344 case poppler::destination::fit_h:
345 std::cout << "fit_h" << std::endl
346 << std::setw(out_width) << "Page"
347 << ": " << d->page_number() << std::endl
348 << std::setw(out_width) << "Top"
349 << ": " << d->top() << std::endl;
350 break;
351 case poppler::destination::fit_v:
352 std::cout << "fit_v" << std::endl
353 << std::setw(out_width) << "Page"
354 << ": " << d->page_number() << std::endl
355 << std::setw(out_width) << "Left"
356 << ": " << d->left() << std::endl;
357 break;
358 case poppler::destination::fit_r:
359 std::cout << "fit_r" << std::endl
360 << std::setw(out_width) << "Page"
361 << ": " << d->page_number() << std::endl
362 << std::setw(out_width) << "Left"
363 << ": " << d->left() << std::endl
364 << std::setw(out_width) << "Bottom"
365 << ": " << d->bottom() << std::endl
366 << std::setw(out_width) << "Right"
367 << ": " << d->right() << std::endl
368 << std::setw(out_width) << "Top"
369 << ": " << d->top() << std::endl;
370 break;
371 case poppler::destination::fit_b:
372 std::cout << "fit_b" << std::endl
373 << std::setw(out_width) << "Page"
374 << ": " << d->page_number() << std::endl;
375 break;
376 case poppler::destination::fit_b_h:
377 std::cout << "fit_b_h" << std::endl
378 << std::setw(out_width) << "Page"
379 << ": " << d->page_number() << std::endl
380 << std::setw(out_width) << "Top"
381 << ": " << d->top() << std::endl;
382 break;
383 case poppler::destination::fit_b_v:
384 std::cout << "fit_b_v" << std::endl
385 << std::setw(out_width) << "Page"
386 << ": " << d->page_number() << std::endl
387 << std::setw(out_width) << "Left"
388 << ": " << d->left() << std::endl;
389 break;
390 default:
391 std::cout << "error" << std::endl;
392 break;
393 }
394 }
395 std::cout << std::endl;
396 }
397
398 static void print_page_text(poppler::page *p)
399 {
400 if (p) {
401 std::cout << p->text(poppler::rectf(), show_text_layout) << std::endl;
402 } else {
403 std::cout << std::setw(out_width) << "Broken Page. Could not be parsed" << std::endl;
404 }
405 std::cout << std::endl;
406 }
407
408 static void print_page_text_list(poppler::page *p, int opt_flag = 0)
409 {
410 if (!p) {
411 std::cout << std::setw(out_width) << "Broken Page. Could not be parsed" << std::endl;
412 std::cout << std::endl;
413 return;
414 }
415 auto text_list = p->text_list(opt_flag);
416
417 std::cout << "---" << std::endl;
418 for (const poppler::text_box &text : text_list) {
419 poppler::rectf bbox = text.bbox();
420 poppler::ustring ustr = text.text();
421 int wmode = text.get_wmode();
422 double font_size = text.get_font_size();
423 std::string font_name = text.get_font_name();
424 std::cout << "[" << ustr << "] @ ";
425 std::cout << "( x=" << bbox.x() << " y=" << bbox.y() << " w=" << bbox.width() << " h=" << bbox.height() << " )";
426 if (text.has_font_info()) {
427 std::cout << "( fontname=" << font_name << " fontsize=" << font_size << " wmode=" << wmode << " )";
428 }
429 std::cout << std::endl;
430 }
431 std::cout << "---" << std::endl;
432 }
433
434 int main(int argc, char *argv[])
435 {
436 if (!parseArgs(the_args, &argc, argv) || argc < 2 || show_help) {
437 printUsage(argv[0], "DOCUMENT", the_args);
438 exit(1);
439 }
440
441 if (show_text[0]) {
442 if (!memcmp(show_text, "physical", 9)) {
443 show_text_layout = poppler::page::physical_layout;
444 } else if (!memcmp(show_text, "raw", 4)) {
445 show_text_layout = poppler::page::raw_order_layout;
446 } else if (!memcmp(show_text, "none", 5)) {
447 show_text_layout = poppler::page::non_raw_non_physical_layout;
448 } else {
449 error(std::string("unrecognized text mode: '") + show_text + "'");
450 }
451 }
452
453 std::string file_name(argv[1]);
454
455 std::unique_ptr<poppler::document> doc(poppler::document::load_from_file(file_name));
456 if (!doc) {
457 error("loading error");
458 }
459 if (doc->is_locked()) {
460 error("encrypted document");
461 }
462
463 std::cout.setf(std::ios_base::boolalpha);
464
465 if (show_all) {
466 show_info = true;
467 show_perm = true;
468 show_metadata = true;
469 show_toc = true;
470 show_fonts = true;
471 show_embedded_files = true;
472 show_pages = true;
473 }
474
475 if (show_version) {
476 std::cout << std::setw(out_width) << "Compiled"
477 << ": poppler-cpp " << POPPLER_VERSION << std::endl
478 << std::setw(out_width) << "Running"
479 << ": poppler-cpp " << poppler::version_string() << std::endl;
480 }
481 if (show_info) {
482 print_info(doc.get());
483 }
484 if (show_perm) {
485 print_perm(doc.get());
486 }
487 if (show_metadata) {
488 print_metadata(doc.get());
489 }
490 if (show_toc) {
491 std::unique_ptr<poppler::toc> doctoc(doc->create_toc());
492 print_toc(doctoc.get());
493 }
494 if (show_fonts) {
495 print_fonts(doc.get());
496 }
497 if (show_embedded_files) {
498 print_embedded_files(doc.get());
499 }
500 if (show_pages) {
501 const int pages = doc->pages();
502 for (int i = 0; i < pages; ++i) {
503 std::cout << "Page " << (i + 1) << "/" << pages << ":" << std::endl;
504 std::unique_ptr<poppler::page> p(doc->create_page(i));
505 print_page(p.get());
506 }
507 }
508 if (show_destinations) {
509 auto map = doc->create_destination_map();
510 for (const auto &pair : map) {
511 std::string s = pair.first;
512 for (auto &c : s) {
513 if (c < 0x20 || c > 0x7e) {
514 c = '.';
515 }
516 }
517 std::cout << "Named destination \"" << s << "\":" << std::endl;
518 print_destination(&pair.second);
519 }
520 }
521 if (show_text[0]) {
522 const int pages = doc->pages();
523 for (int i = 0; i < pages; ++i) {
524 std::cout << "Page " << (i + 1) << "/" << pages << ":" << std::endl;
525 std::unique_ptr<poppler::page> p(doc->create_page(i));
526 print_page_text(p.get());
527 }
528 }
529 if (show_text_list || show_text_list_with_font) {
530 const int pages = doc->pages();
531 for (int i = 0; i < pages; ++i) {
532 std::cout << "Page " << (i + 1) << "/" << pages << ":" << std::endl;
533 std::unique_ptr<poppler::page> p(doc->create_page(i));
534 if (show_text_list_with_font) {
535 print_page_text_list(p.get(), poppler::page::text_list_include_font);
536 } else {
537 print_page_text_list(p.get(), 0);
538 }
539 }
540 }
541
542 return 0;
543 }