Code

Redirected opening of AI files to the poppler-based PDF import extension
[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     gchar *docname = g_path_get_basename(uri);
73     gchar *dot = g_strrstr(docname, ".");
74     if (dot) {
75         *dot = 0;
76     }
77     SvgBuilder *builder = new SvgBuilder(doc, docname, pdf_doc->getXRef());
78     PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(),
79                                           page->getResourceDict(), page->getCropBox());
81     // Parse the document structure
82     Object obj;
83     page->getContents(&obj);
84     if (!obj.isNull()) {
85         pdf_parser->parse(&obj);
86     }
87     
88     // Cleanup
89     obj.free();
90     delete pdf_parser;
91     delete builder;
92     g_free(docname);
93     delete pdf_doc;
95     // Restore undo
96     sp_document_set_undo_sensitive(doc, saved);
98     return doc;
99 }
101 #include "../clear-n_.h"
103 void
104 PdfInput::init(void) {
105     Inkscape::Extension::Extension * ext;
107     /* PDF in */
108     ext = Inkscape::Extension::build_from_mem(
109         "<inkscape-extension>\n"
110             "<name>PDF Input</name>\n"
111             "<id>org.inkscape.input.pdf</id>\n"
112             "<input>\n"
113                 "<extension>.pdf</extension>\n"
114                 "<mimetype>application/pdf</mimetype>\n"
115                 "<filetypename>Adobe PDF (*.pdf) [via poppler]</filetypename>\n"
116                 "<filetypetooltip>Adobe Portable Document Format</filetypetooltip>\n"
117             "</input>\n"
118         "</inkscape-extension>", new PdfInput());
120     /* AI in */
121     ext = Inkscape::Extension::build_from_mem(
122         "<inkscape-extension>\n"
123             "<name>AI Input</name>\n"
124             "<id>org.inkscape.input.ai</id>\n"
125             "<input>\n"
126                 "<extension>.ai</extension>\n"
127                 "<mimetype>image/x-adobe-illustrator</mimetype>\n"
128                 "<filetypename>Adobe Illustrator (*.ai) [PDF-based]</filetypename>\n"
129                 "<filetypetooltip>Open files saved with recent versions of Adobe Illustrator</filetypetooltip>\n"
130             "</input>\n"
131         "</inkscape-extension>", new PdfInput());
132 } // init
134 } } }  /* namespace Inkscape, Extension, Implementation */
136 #endif /* HAVE_POPPLER */
138 /*
139   Local Variables:
140   mode:c++
141   c-file-style:"stroustrup"
142   c-file-offsets:((innamespace . 0)(inline-open . 0))
143   indent-tabs-mode:nil
144   fill-column:99
145   End:
146 */
147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :