Code

Further development of the PDF import module: continued pruning of PdfParser.cpp...
[inkscape.git] / src / extension / internal / pdfinput / pdf-input.cpp
1  /** \file
2  * Native PDF import using libpoppler.
3  * 
4  * Authors:
5  *   miklos erdelyi
6  *
7  * Copyright (C) 2007 Authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  *
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #ifdef HAVE_POPPLER
19 #include "goo/GooString.h"
20 #include "ErrorCodes.h"
21 #include "GlobalParams.h"
22 #include "PDFDoc.h"
23 #include "Page.h"
24 #include "Catalog.h"
26 #include "pdf-input.h"
27 #include "extension/system.h"
28 #include "extension/input.h"
29 #include "svg-builder.h"
30 #include "pdf-parser.h"
32 #include "document-private.h"
34 namespace Inkscape {
35 namespace Extension {
36 namespace Internal {
38 /**
39  * Parses the first page of the given PDF document using PdfParser.
40  */
41 SPDocument *
42 PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) {
44     // Initialize the globalParams variable for poppler
45     if (!globalParams) {
46         globalParams = new GlobalParams();
47     }
48     GooString *filename_goo = new GooString(uri);
49     PDFDoc *pdf_doc = new PDFDoc(filename_goo, NULL, NULL, NULL);   // TODO: Could ask for password
50     if (!pdf_doc->isOk()) {
51         int error = pdf_doc->getErrorCode();
52         delete pdf_doc;
53         if (error == errEncrypted) {
54             g_message("Document is encrypted.");
55         } else {
56             g_message("Failed to load document from data (error %d)", error);
57         }
58  
59         return NULL;
60     }
62     // Get needed page
63     int page_num = 1;
64     Catalog *catalog = pdf_doc->getCatalog();
65     Page *page = catalog->getPage(page_num);
67     SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
68     bool saved = sp_document_get_undo_sensitive(doc);
69     sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document
71     // Create builder and parser
72     SvgBuilder *builder = new SvgBuilder(doc);
73     PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(),
74                                           page->getResourceDict(), page->getCropBox());
76     // Parse the document structure
77     Object obj;
78     page->getContents(&obj);
79     if (!obj.isNull()) {
80         pdf_parser->saveState();
81         pdf_parser->parse(&obj);
82         pdf_parser->restoreState();
83     }
84     
85     // Cleanup
86     obj.free();
87     delete pdf_parser;
88     delete builder;
89     delete pdf_doc;
91     // Restore undo
92     sp_document_set_undo_sensitive(doc, saved);
94     return doc;
95 }
97 #include "../clear-n_.h"
99 void
100 PdfInput::init(void) {
101     Inkscape::Extension::Extension * ext;
103     ext = Inkscape::Extension::build_from_mem(
104         "<inkscape-extension>\n"
105             "<name>PDF Input</name>\n"
106             "<id>org.inkscape.input.pdf</id>\n"
107             "<input>\n"
108                 "<extension>.pdf</extension>\n"
109                 "<mimetype>application/pdf</mimetype>\n"
110                 "<filetypename>Adobe PDF (*.pdf) [native]</filetypename>\n"
111                 "<filetypetooltip>PDF Document</filetypetooltip>\n"
112             "</input>\n"
113         "</inkscape-extension>", new PdfInput());
114 } // init
116 } } }  /* namespace Inkscape, Extension, Implementation */
118 #endif /* HAVE_POPPLER */
120 /*
121   Local Variables:
122   mode:c++
123   c-file-style:"stroustrup"
124   c-file-offsets:((innamespace . 0)(inline-open . 0))
125   indent-tabs-mode:nil
126   fill-column:99
127   End:
128 */
129 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :