~ubuntu-branches/ubuntu/natty/poppler/natty-proposed

« back to all changes in this revision

Viewing changes to cpp/tests/poppler-dump.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-01-13 21:32:30 UTC
  • mfrom: (1.5.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110113213230-0rmd29fcww6e28sx
Tags: 0.16.0-0ubuntu1
* New upstream release
* Drop Qt 3 build-depend and packages, no longer used, remove --enable-poppler-qt
* libpoppler7 renamed to libpoppler12
* libpoppler-glib5 renamed to libpoppler-glib6

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <poppler-toc.h>
24
24
 
25
25
#include <cstdlib>
 
26
#include <cstring>
26
27
#include <ctime>
27
28
#include <iomanip>
 
29
#include <ios>
28
30
#include <iostream>
29
31
#include <memory>
30
32
#include <sstream>
44
46
bool show_embedded_files = false;
45
47
bool show_pages = false;
46
48
bool show_help = false;
 
49
char show_text[32];
 
50
poppler::page::text_layout_enum show_text_layout = poppler::page::physical_layout;
47
51
 
48
52
static const ArgDesc the_args[] = {
49
53
    { "--show-all",            argFlag,  &show_all,            0,
62
66
      "show the document-level embedded files" },
63
67
    { "--show-pages",          argFlag,  &show_pages,          0,
64
68
      "show pages information" },
 
69
    { "--show-text",           argString, &show_text,          sizeof(show_text),
 
70
      "show text (physical|raw) extracted from all pages" },
65
71
    { "-h",                    argFlag,  &show_help,           0,
66
72
      "print usage information" },
67
73
    { "--help",                argFlag,  &show_help,           0,
76
82
    exit(1);
77
83
}
78
84
 
79
 
static std::string out_ustring(const poppler::ustring &str)
 
85
std::ostream& operator<<(std::ostream& stream, const poppler::ustring &str)
80
86
{
81
 
    return str.to_latin1();
 
87
    const poppler::byte_array ba = str.to_utf8();
 
88
    for (unsigned int i = 0; i < ba.size(); ++i) {
 
89
        stream << (char)(ba[i]);
 
90
    }
 
91
    return stream;
82
92
}
83
93
 
84
94
static std::string out_date(std::time_t date)
166
176
    int major = 0, minor = 0;
167
177
    doc->get_pdf_version(&major, &minor);
168
178
    std::cout << std::setw(out_width) << "PDF version" << ": " << major << "." << minor << std::endl;
 
179
    std::string permanent_id, update_id;
 
180
    if (doc->get_pdf_id(&permanent_id, &update_id)) {
 
181
        std::cout << std::setw(out_width) << "PDF IDs" << ": P: " << permanent_id << " - U: " << update_id << std::endl;
 
182
    } else {
 
183
        std::cout << std::setw(out_width) << "PDF IDs" << ": <none>" << std::endl;
 
184
    }
169
185
    const std::vector<std::string> keys = doc->info_keys();
170
186
    std::vector<std::string>::const_iterator key_it = keys.begin(), key_end = keys.end();
171
187
    for (; key_it != key_end; ++key_it) {
172
 
        std::cout << std::setw(out_width) << *key_it << ": " << out_ustring(doc->info_key(*key_it)) << std::endl;
 
188
        std::cout << std::setw(out_width) << *key_it << ": " << doc->info_key(*key_it) << std::endl;
173
189
    }
174
190
    std::cout << std::setw(out_width) << "Date (creation)" << ": " << out_date(doc->info_date("CreationDate")) << std::endl;
175
191
    std::cout << std::setw(out_width) << "Date (modification)" << ": " << out_date(doc->info_date("ModDate")) << std::endl;
200
216
static void print_metadata(poppler::document *doc)
201
217
{
202
218
    std::cout << std::setw(out_width) << "Metadata" << ":" << std::endl
203
 
              << out_ustring(doc->metadata()) << std::endl;
 
219
              << doc->metadata() << std::endl;
204
220
    std::cout << std::endl;
205
221
}
206
222
 
207
223
static void print_toc_item(poppler::toc_item *item, int indent)
208
224
{
209
225
    std::cout << std::setw(indent * 2) << " "
210
 
              << "+ " << out_ustring(item->title()) << " (" << item->is_open() << ")"
 
226
              << "+ " << item->title() << " (" << item->is_open() << ")"
211
227
              << std::endl;
212
228
    poppler::toc_item::iterator it = item->children_begin(), it_end = item->children_end();
213
229
    for (; it != it_end; ++it) {
266
282
                << " " << std::setw(20) << out_date(f->creation_date())
267
283
                << " " << std::setw(20) << out_date(f->modification_date())
268
284
                << std::endl
269
 
                << "     " << (f->description().empty() ? std::string("<no description>") : out_ustring(f->description()))
 
285
                << "     ";
 
286
            if (f->description().empty()) {
 
287
                std::cout << "<no description>";
 
288
            } else {
 
289
                std::cout << f->description();
 
290
            }
 
291
            std::cout
270
292
                << std::endl
271
293
                << "     " << std::setw(35) << (f->checksum().empty() ? std::string("<no checksum>") : out_hex_string(f->checksum()))
272
294
                << " " << (f->mime_type().empty() ? std::string("<no mime type>") : f->mime_type())
282
304
static void print_page(poppler::page *p)
283
305
{
284
306
    std::cout << std::setw(out_width) << "Rect" << ": " << p->page_rect() << std::endl;
285
 
    std::cout << std::setw(out_width) << "Label" << ": " << out_ustring(p->label()) << std::endl;
 
307
    std::cout << std::setw(out_width) << "Label" << ": " << p->label() << std::endl;
286
308
    std::cout << std::setw(out_width) << "Duration" << ": " << p->duration() << std::endl;
287
309
    std::cout << std::setw(out_width) << "Orientation" << ": " << out_page_orientation(p->orientation()) << std::endl;
288
310
    std::cout << std::endl;
289
311
}
290
312
 
 
313
static void print_page_text(poppler::page *p)
 
314
{
 
315
    std::cout << p->text(p->page_rect(), show_text_layout) << std::endl;
 
316
    std::cout << std::endl;
 
317
}
 
318
 
291
319
int main(int argc, char *argv[])
292
320
{
293
321
    if (!parseArgs(the_args, &argc, argv)
296
324
        exit(1);
297
325
    }
298
326
 
 
327
    if (show_text[0]) {
 
328
        if (!memcmp(show_text, "physical", 9)) {
 
329
            show_text_layout = poppler::page::physical_layout;
 
330
        } else if (!memcmp(show_text, "raw", 4)) {
 
331
            show_text_layout = poppler::page::raw_order_layout;
 
332
        } else {
 
333
            error(std::string("unrecognized text mode: '") + show_text + "'");
 
334
        }
 
335
    }
 
336
 
299
337
    std::string file_name(argv[1]);
300
338
 
301
339
    std::auto_ptr<poppler::document> doc(poppler::document::load_from_file(file_name));
345
383
            print_page(p.get());
346
384
        }
347
385
    }
 
386
    if (show_text[0]) {
 
387
        const int pages = doc->pages();
 
388
        for (int i = 0; i < pages; ++i) {
 
389
            std::cout << "Page " << (i + 1) << "/" << pages << ":" << std::endl;
 
390
            std::auto_ptr<poppler::page> p(doc->create_page(i));
 
391
            print_page_text(p.get());
 
392
        }
 
393
    }
348
394
 
349
395
    return 0;
350
396
}