summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/pdfinput/pdf-input.cpp
diff options
context:
space:
mode:
authormiklosh <miklosh@users.sourceforge.net>2007-07-12 17:05:34 +0000
committermiklosh <miklosh@users.sourceforge.net>2007-07-12 17:05:34 +0000
commit4424fcfb6d10006f2e73a988184462794856d31f (patch)
treeff5adfc3d92e3ebf5641d60fb3de130b5261c926 /src/extension/internal/pdfinput/pdf-input.cpp
parentfix for panning and zooming into area in 3D box tool (root_handler of event_c... (diff)
downloadinkscape-4424fcfb6d10006f2e73a988184462794856d31f.tar.gz
inkscape-4424fcfb6d10006f2e73a988184462794856d31f.zip
Initial commit of native poppler-based PDF import.
(bzr r3230)
Diffstat (limited to 'src/extension/internal/pdfinput/pdf-input.cpp')
-rw-r--r--src/extension/internal/pdfinput/pdf-input.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/extension/internal/pdfinput/pdf-input.cpp b/src/extension/internal/pdfinput/pdf-input.cpp
new file mode 100644
index 000000000..b9e616025
--- /dev/null
+++ b/src/extension/internal/pdfinput/pdf-input.cpp
@@ -0,0 +1,139 @@
+ /** \file
+ * Native PDF import using libpoppler.
+ *
+ * Authors:
+ * miklos erdelyi
+ *
+ * Copyright (C) 2007 Authors
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef HAVE_POPPLER
+
+#include "goo/GooString.h"
+#include "ErrorCodes.h"
+#include "GlobalParams.h"
+#include "PDFDoc.h"
+#include "Page.h"
+#include "Catalog.h"
+
+#include "pdf-input.h"
+#include "extension/system.h"
+#include "extension/input.h"
+#include "svg-builder.h"
+#include "pdf-parser.h"
+
+#include "document-private.h"
+
+namespace Inkscape {
+namespace Extension {
+namespace Internal {
+
+/**
+ * Parses the first page of the given PDF document using PdfParser.
+ */
+SPDocument *
+PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) {
+
+ // Initialize the globalParams variable for poppler
+ if (!globalParams) {
+ globalParams = new GlobalParams();
+ g_message("Created globalParams");
+ }
+ GooString *filename_goo = new GooString(uri);
+ PDFDoc *pdf_doc = new PDFDoc(filename_goo, NULL, NULL, NULL); // TODO: Could ask for password
+ if (!pdf_doc->isOk()) {
+ int error = pdf_doc->getErrorCode();
+ delete pdf_doc;
+ if (error == errEncrypted) {
+ g_message("Document is encrypted.");
+ } else {
+ g_message("Failed to load document from data (error %d)", error);
+ }
+
+ return NULL;
+ }
+
+ // Get needed page
+ int page_num = 1;
+ Catalog *catalog = pdf_doc->getCatalog();
+ Page *page = catalog->getPage(page_num);
+
+ double width, height;
+ int rotate = page->getRotate();
+ if (rotate == 90 || rotate == 270) {
+ width = page->getCropHeight();
+ height = page->getCropWidth();
+ } else {
+ width = page->getCropWidth();
+ height = page->getCropHeight();
+ }
+
+ SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
+ bool saved = sp_document_get_undo_sensitive(doc);
+ sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document
+
+ // Create builder and parser
+ SvgBuilder *builder = new SvgBuilder(doc);
+ PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1,
+ page->getResourceDict(), page->getCropBox());
+
+ // Parse the document structure
+ Object obj;
+ page->getContents(&obj);
+ if (!obj.isNull()) {
+ pdf_parser->saveState();
+ pdf_parser->parse(&obj);
+ pdf_parser->restoreState();
+ }
+
+ // Cleanup
+ obj.free();
+ delete pdf_parser;
+ delete builder;
+
+ // Restore undo
+ sp_document_set_undo_sensitive(doc, saved);
+
+ return doc;
+}
+
+#include "../clear-n_.h"
+
+void
+PdfInput::init(void) {
+ Inkscape::Extension::Extension * ext;
+
+ ext = Inkscape::Extension::build_from_mem(
+ "<inkscape-extension>\n"
+ "<name>PDF Input</name>\n"
+ "<id>org.inkscape.input.pdf</id>\n"
+ "<input>\n"
+ "<extension>.pdf</extension>\n"
+ "<mimetype>application/pdf</mimetype>\n"
+ "<filetypename>Adobe PDF (*.pdf) [native]</filetypename>\n"
+ "<filetypetooltip>PDF Document</filetypetooltip>\n"
+ "</input>\n"
+ "</inkscape-extension>", new PdfInput());
+} // init
+
+} } } /* namespace Inkscape, Extension, Implementation */
+
+#endif /* HAVE_POPPLER */
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :