[poppler] Branch 'cpp-frontend' - 5 commits - configure.ac cpp/CMakeLists.txt cpp/Makefile.am cpp/poppler-page.cpp cpp/poppler-toc.cpp cpp/tests

Pino Toscano pino at kemper.freedesktop.org
Thu Dec 17 15:22:24 PST 2009


 configure.ac               |    1 
 cpp/CMakeLists.txt         |    2 
 cpp/Makefile.am            |    1 
 cpp/poppler-page.cpp       |    2 
 cpp/poppler-toc.cpp        |    8 +
 cpp/tests/.gitignore       |    1 
 cpp/tests/CMakeLists.txt   |   24 +++
 cpp/tests/Makefile.am      |   17 ++
 cpp/tests/poppler-dump.cpp |  293 +++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 347 insertions(+), 2 deletions(-)

New commits:
commit 752b14857cfb0669fd6d7dfef2ea73c13a2369fc
Author: Pino Toscano <pino at kde.org>
Date:   Thu Dec 17 17:16:01 2009 +0100

    simplify

diff --git a/cpp/poppler-toc.cpp b/cpp/poppler-toc.cpp
index 5d58d7b..1fc0f4d 100644
--- a/cpp/poppler-toc.cpp
+++ b/cpp/poppler-toc.cpp
@@ -47,7 +47,7 @@ toc* toc_private::load_from_outline(Outline *outline)
 
     toc *newtoc = new toc();
     newtoc->d->root.d->is_open = true;
-    newtoc->d->root.d->load_children(outline->getItems());
+    newtoc->d->root.d->load_children(items);
 
     return newtoc;
 }
commit a26e4b7903fabc667d7d16c23ca2c7a535dd05fa
Author: Pino Toscano <pino at kde.org>
Date:   Thu Dec 17 17:09:35 2009 +0100

    [cpp/tests] add a simple poppler-dump test
    
    this small test executable can dump various features of the document,
    like the general info, permissions, metadata, toc, fonts,
    embedded files, pages, etc
    
    also add the necessary autotools+cmake machinery to compile it

diff --git a/configure.ac b/configure.ac
index 70bac6c..a51eede 100644
--- a/configure.ac
+++ b/configure.ac
@@ -568,6 +568,7 @@ qt4/tests/Makefile
 qt4/demos/Makefile
 cpp/Makefile
 cpp/poppler-version.h
+cpp/tests/Makefile
 poppler.pc
 poppler-cairo.pc
 poppler-splash.pc
diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index 0a0c6f0..b5880de 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -5,6 +5,8 @@ include_directories(
 
 configure_file(poppler-version.h.in ${CMAKE_CURRENT_BINARY_DIR}/poppler-version.h @ONLY)
 
+add_subdirectory(tests)
+
 set(poppler_cpp_SRCS
   poppler-document.cpp
   poppler-embedded-file.cpp
diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index 6259770..8890f83 100644
--- a/cpp/Makefile.am
+++ b/cpp/Makefile.am
@@ -3,6 +3,7 @@ INCLUDES =					\
 	-I$(top_srcdir)/goo			\
 	-I$(top_srcdir)/poppler
 
+SUBDIRS = . tests
 
 poppler_includedir = $(includedir)/poppler/cpp
 poppler_include_HEADERS =			\
diff --git a/cpp/tests/.gitignore b/cpp/tests/.gitignore
new file mode 100644
index 0000000..737f30a
--- /dev/null
+++ b/cpp/tests/.gitignore
@@ -0,0 +1 @@
+poppler-dump
diff --git a/cpp/tests/CMakeLists.txt b/cpp/tests/CMakeLists.txt
new file mode 100644
index 0000000..a083afd
--- /dev/null
+++ b/cpp/tests/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories(
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}/..
+  ${CMAKE_CURRENT_BINARY_DIR}/..
+  ${CMAKE_SOURCE_DIR}/utils
+)
+
+# ### temporary
+set(BUILD_CPP_TESTS TRUE)
+
+macro(CPP_ADD_SIMPLETEST exe)
+  string(REPLACE "-" "" test_name ${exe})
+  set(${test_name}_SOURCES
+    ${ARGN}
+  )
+  poppler_add_test(${exe} BUILD_CPP_TESTS ${${test_name}_SOURCES})
+  target_link_libraries(${exe} poppler-cpp)
+  if(MSVC)
+    target_link_libraries(${exe} poppler ${poppler_LIBS})
+  endif(MSVC)
+endmacro(CPP_ADD_SIMPLETEST)
+
+
+cpp_add_simpletest(poppler-dump poppler-dump.cpp ${CMAKE_SOURCE_DIR}/utils/parseargs.c)
diff --git a/cpp/tests/Makefile.am b/cpp/tests/Makefile.am
new file mode 100644
index 0000000..b01d3b9
--- /dev/null
+++ b/cpp/tests/Makefile.am
@@ -0,0 +1,17 @@
+INCLUDES =					\
+	-I$(top_srcdir)				\
+	-I$(top_srcdir)/utils			\
+	-I$(top_srcdir)/cpp
+
+LDADDS =					\
+	$(top_builddir)/cpp/libpoppler-cpp.la
+
+
+noinst_PROGRAMS =				\
+	poppler-dump
+
+poppler_dump_SOURCES = \
+	$(top_srcdir)/utils/parseargs.c		\
+	poppler-dump.cpp
+poppler_dump_LDADD = $(LDADDS)
+
diff --git a/cpp/tests/poppler-dump.cpp b/cpp/tests/poppler-dump.cpp
new file mode 100644
index 0000000..de50244
--- /dev/null
+++ b/cpp/tests/poppler-dump.cpp
@@ -0,0 +1,293 @@
+/*
+ * Copyright (C) 2009, Pino Toscano <pino at kde.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <poppler-document.h>
+#include <poppler-embedded-file.h>
+#include <poppler-font.h>
+#include <poppler-page.h>
+#include <poppler-toc.h>
+
+#include <cstdlib>
+#include <ctime>
+#include <iomanip>
+#include <iostream>
+#include <memory>
+#include <sstream>
+
+#include "parseargs.h"
+
+static const int out_width = 30;
+
+bool show_info = false;
+bool show_perm = false;
+bool show_metadata = false;
+bool show_toc = false;
+bool show_fonts = false;
+bool show_embedded_files = false;
+bool show_pages = false;
+bool show_help = false;
+
+static const ArgDesc the_args[] = {
+    { "--show-info",           argFlag,  &show_info,           0,
+      "show general document information" },
+    { "--show-perm",           argFlag,  &show_perm,           0,
+      "show document permissions" },
+    { "--show-metadata",       argFlag,  &show_metadata,       0,
+      "show document metadata" },
+    { "--show-toc",            argFlag,  &show_toc,            0,
+      "show the TOC" },
+    { "--show-fonts",          argFlag,  &show_fonts,          0,
+      "show the document fonts" },
+    { "--show-embedded-files", argFlag,  &show_embedded_files, 0,
+      "show the document-level embedded files" },
+    { "--show-pages",          argFlag,  &show_pages,          0,
+      "show pages information" },
+    { "-h",                    argFlag,  &show_help,           0,
+      "print usage information" },
+    { "--help",                argFlag,  &show_help,           0,
+      "print usage information" },
+    { NULL, argFlag, 0, 0, NULL }
+};
+
+static void error(const std::string &msg)
+{
+    std::cerr << "Error: " << msg << std::endl;
+    std::cerr << "Exiting..." << std::endl;
+    exit(1);
+}
+
+static std::string out_ustring(const poppler::ustring &str)
+{
+    return str.to_latin1();
+}
+
+static std::string out_date(std::time_t date)
+{
+    if (date != std::time_t(-1)) {
+        struct tm time;
+        gmtime_r(&date, &time);
+        char buf[32];
+        strftime(buf, sizeof(buf) - 1, "%d/%m/%Y %H:%M:%S", &time);
+        return std::string(buf);
+    }
+    return std::string("n/a");
+}
+
+static std::string out_size(int size)
+{
+    if (size >= 0) {
+        std::ostringstream ss;
+        ss << size;
+        return ss.str();
+    }
+    return std::string("n/a");
+}
+
+static char charToHex(int x)
+{
+    return x < 10 ? x + '0' : x - 10 + 'a';
+}
+
+static std::string out_hex_string(const std::string &str)
+{
+    std::string ret(str.size() * 2, '\0');
+    const char *str_p = str.data();
+    for (unsigned int i = 0; i < str.size(); ++i, ++str_p) {
+        ret[i * 2] = charToHex((*str_p & 0xf0) >> 4);
+        ret[i * 2 + 1] = charToHex(*str_p & 0xf);
+    }
+    return ret;
+}
+
+static void print_info(poppler::document *doc)
+{
+    std::cout << "Document information:" << std::endl;
+    int major = 0, minor = 0;
+    doc->get_pdf_version(&major, &minor);
+    std::cout << std::setw(out_width) << "PDF version" << ": " << major << "." << minor << std::endl;
+    const std::vector<std::string> keys = doc->info_keys();
+    std::vector<std::string>::const_iterator key_it = keys.begin(), key_end = keys.end();
+    for (; key_it != key_end; ++key_it) {
+        std::cout << std::setw(out_width) << *key_it << ": " << out_ustring(doc->info_key(*key_it)) << std::endl;
+    }
+    std::cout << std::setw(out_width) << "Date (creation)" << ": " << out_date(doc->info_date("CreationDate")) << std::endl;
+    std::cout << std::setw(out_width) << "Date (modification)" << ": " << out_date(doc->info_date("ModDate")) << std::endl;
+    std::cout << std::setw(out_width) << "Number of pages" << ": " << doc->pages() << std::endl;
+    std::cout << std::setw(out_width) << "Linearized" << ": " << doc->is_linearized() << std::endl;
+    std::cout << std::setw(out_width) << "Encrypted" << ": " << doc->is_encrypted() << std::endl;
+    std::cout << std::endl;
+}
+
+static void print_perm(poppler::document *doc)
+{
+    std::cout << "Document permissions:" << std::endl;
+#define OUT_PERM(theperm) \
+    std::cout << std::setw(out_width) << "Perm (" #theperm ")" << ": " \
+              << doc->has_permission(poppler::perm_##theperm) << std::endl
+    OUT_PERM(print);
+    OUT_PERM(change);
+    OUT_PERM(copy);
+    OUT_PERM(add_notes);
+    OUT_PERM(fill_forms);
+    OUT_PERM(accessibility);
+    OUT_PERM(assemble);
+    OUT_PERM(print_high_resolution);
+    std::cout << std::endl;
+#undef OUT_PERM
+}
+
+static void print_metadata(poppler::document *doc)
+{
+    std::cout << std::setw(out_width) << "Metadata" << ":" << std::endl
+              << out_ustring(doc->metadata()) << std::endl;
+    std::cout << std::endl;
+}
+
+static void print_toc_item(poppler::toc_item *item, int indent)
+{
+    std::cout << std::setw(indent * 2) << " "
+              << "+ " << out_ustring(item->title()) << " (" << item->is_open() << ")"
+              << std::endl;
+    poppler::toc_item::iterator it = item->children_begin(), it_end = item->children_end();
+    for (; it != it_end; ++it) {
+        print_toc_item(*it, indent + 1);
+    }
+}
+
+static void print_toc(poppler::toc *doctoc)
+{
+    std::cout << "Document TOC:" << std::endl;
+    if (doctoc) {
+        print_toc_item(doctoc->root(), 0);
+    } else {
+        std::cout << "<no TOC>" << std::endl;
+    }
+    std::cout << std::endl;
+}
+
+static void print_fonts(poppler::document *doc)
+{
+    std::cout << "Document fonts:" << std::endl;
+    std::vector<poppler::font_info> fl = doc->fonts();
+    if (!fl.empty()) {
+        std::vector<poppler::font_info>::const_iterator it = fl.begin(), it_end = fl.end();
+        const std::ios_base::fmtflags f = std::cout.flags();
+        std::left(std::cout);
+        for (; it != it_end; ++it) {
+            std::cout
+                << " " << std::setw(out_width + 10) << it->name()
+                << " " << std::setw(15) << it->type()
+                << " " << std::setw(5) << it->is_embedded()
+                << " " << std::setw(5) << it->is_subset()
+                << " " << it->file()
+                << std::endl;
+        }
+        std::cout.flags(f);
+    } else {
+        std::cout << "<no fonts>" << std::endl;
+    }
+    std::cout << std::endl;
+}
+
+static void print_embedded_files(poppler::document *doc)
+{
+    std::cout << "Document embedded files:" << std::endl;
+    std::vector<poppler::embedded_file *> ef = doc->embedded_files();
+    if (!ef.empty()) {
+        std::vector<poppler::embedded_file *>::const_iterator it = ef.begin(), it_end = ef.end();
+        const std::ios_base::fmtflags f = std::cout.flags();
+        std::left(std::cout);
+        for (; it != it_end; ++it) {
+            poppler::embedded_file *f = *it;
+            std::cout
+                << " " << std::setw(out_width + 10) << f->name()
+                << " " << std::setw(10) << out_size(f->size())
+                << " " << std::setw(20) << out_date(f->creation_date())
+                << " " << std::setw(20) << out_date(f->modification_date())
+                << std::endl
+                << "     " << (f->description().empty() ? std::string("<no description>") : out_ustring(f->description()))
+                << std::endl
+                << "     " << 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;
+        }
+        std::cout.flags(f);
+    } else {
+        std::cout << "<no embedded files>" << std::endl;
+    }
+    std::cout << std::endl;
+}
+
+static void print_page(poppler::page *p)
+{
+    std::cout << std::setw(out_width) << "Rect" << ": " << p->page_rect() << std::endl;
+    std::cout << std::setw(out_width) << "Label" << ": " << out_ustring(p->label()) << std::endl;
+    std::cout << std::setw(out_width) << "Duration" << ": " << p->duration() << std::endl;
+    std::cout << std::endl;
+}
+
+int main(int argc, char *argv[])
+{
+    if (!parseArgs(the_args, &argc, argv)
+        || argc < 2 || show_help) {
+        printUsage(argv[0], "DOCUMENT", the_args);
+        exit(1);
+    }
+
+    std::string file_name(argv[1]);
+
+    std::auto_ptr<poppler::document> doc(poppler::document::load_from_file(file_name));
+    if (!doc.get()) {
+        error("loading error");
+    }
+    if (doc->is_locked()) {
+        error("encrypted document");
+    }
+
+    std::cout.setf(std::ios_base::boolalpha);
+
+    if (show_info) {
+        print_info(doc.get());
+    }
+    if (show_perm) {
+        print_perm(doc.get());
+    }
+    if (show_metadata) {
+        print_metadata(doc.get());
+    }
+    if (show_toc) {
+        std::auto_ptr<poppler::toc> doctoc(doc->create_toc());
+        print_toc(doctoc.get());
+    }
+    if (show_fonts) {
+        print_fonts(doc.get());
+    }
+    if (show_embedded_files) {
+        print_embedded_files(doc.get());
+    }
+    if (show_pages) {
+        const int pages = doc->pages();
+        for (int i = 0; i < pages; ++i) {
+            std::cout << "Page " << (i + 1) << "/" << pages << ":" << std::endl;
+            std::auto_ptr<poppler::page> p(doc->create_page(i));
+            print_page(p.get());
+        }
+    }
+
+    return 0;
+}
commit ef7954b86f9b1a762c4f77a48d5f42f8db4bbed7
Author: Pino Toscano <pino at kde.org>
Date:   Tue Dec 15 02:20:18 2009 +0100

    [cpp] use the correct index (instead of an uninitialized variable)

diff --git a/cpp/poppler-page.cpp b/cpp/poppler-page.cpp
index b10cc5b..c8ce706 100644
--- a/cpp/poppler-page.cpp
+++ b/cpp/poppler-page.cpp
@@ -27,7 +27,7 @@ using namespace poppler;
 
 page_private::page_private(document_private *_doc, int _index)
     : doc(_doc)
-    , page(doc->doc->getCatalog()->getPage(index + 1))
+    , page(doc->doc->getCatalog()->getPage(_index + 1))
     , index(_index)
     , transition(0)
 {
commit 2547b5e53e7799cf7299838ae9bd882c21a228ae
Author: Pino Toscano <pino at kde.org>
Date:   Tue Dec 15 02:14:57 2009 +0100

    [cpp] properly delete the children of a toc item

diff --git a/cpp/poppler-toc.cpp b/cpp/poppler-toc.cpp
index 73a97dc..5d58d7b 100644
--- a/cpp/poppler-toc.cpp
+++ b/cpp/poppler-toc.cpp
@@ -59,6 +59,7 @@ toc_item_private::toc_item_private()
 
 toc_item_private::~toc_item_private()
 {
+    delete_all(children);
 }
 
 void toc_item_private::load(OutlineItem *item)
commit 771bd3594740fe16bd030dbe73928a8b1d4d113f
Author: Pino Toscano <pino at kde.org>
Date:   Tue Dec 15 02:04:24 2009 +0100

    [cpp] actually implement toc::root()

diff --git a/cpp/poppler-toc.cpp b/cpp/poppler-toc.cpp
index 6f05f21..73a97dc 100644
--- a/cpp/poppler-toc.cpp
+++ b/cpp/poppler-toc.cpp
@@ -99,6 +99,11 @@ toc::~toc()
     delete d;
 }
 
+toc_item* toc::root() const
+{
+    return &d->root;
+}
+
 
 toc_item::toc_item()
     : d(new toc_item_private())


More information about the poppler mailing list