Code

add pdf import filter via poppler-cairo
[inkscape.git] / src / extension / internal / pdf-input-cairo.cpp
1  /*
2  * Simple PDF import extension using libpoppler and Cairo's SVG surface.
3  *
4  * Authors:
5  *   miklos erdelyi
6  *   Abhishek Sharma
7  *
8  * Copyright (C) 2007 Authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  *
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #ifdef HAVE_POPPLER_GLIB
20 #include "pdf-input-cairo.h"
21 #include "extension/system.h"
22 #include "extension/input.h"
23 #include "document.h"
25 #include <cairo-svg.h>
26 #include <poppler/glib/poppler.h>
27 #include <poppler/glib/poppler-document.h>
28 #include <poppler/glib/poppler-page.h>
30 namespace Inkscape {
31 namespace Extension {
32 namespace Internal {
34 static cairo_status_t _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length);
36 SPDocument *
37 PdfInputCairo::open(Inkscape::Extension::Input * /*mod*/, const gchar * uri) {
39     printf("Attempting to open using PdfInputCairo\n");
41     gchar* filename_uri = g_filename_to_uri(uri, NULL, NULL);
43     PopplerDocument* document = poppler_document_new_from_file(filename_uri, NULL, NULL);
44     if (document == NULL)
45         return NULL;
47     double width, height;
48     PopplerPage* page = poppler_document_get_page(document, 0);
49     poppler_page_get_size(page, &width, &height);
51     Glib::ustring* output = new Glib::ustring("");
52     cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
53                                                                    output, width, height);
54     cairo_t* cr = cairo_create(surface);
56     poppler_page_render(page, cr);
57     cairo_show_page(cr);
59     cairo_destroy(cr);
60     cairo_surface_destroy(surface);
62     SPDocument * doc = SPDocument::createNewDocFromMem(output->c_str(), output->length(), TRUE);
64     delete output;
65     g_object_unref(page);
66     g_object_unref(document);
68     return doc;
69 }
71 static cairo_status_t
72         _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length)
73 {
74     Glib::ustring* stream = (Glib::ustring*)closure;
75     stream->append((const char*)data, length);
77     return CAIRO_STATUS_SUCCESS;
78 }
81 #include "clear-n_.h"
83 void
84 PdfInputCairo::init(void) {
85     Inkscape::Extension::Extension * ext;
87     ext = Inkscape::Extension::build_from_mem(
88         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
89             "<name>PDF Input</name>\n"
90             "<id>org.inkscape.input.cairo-pdf</id>\n"
91             "<input>\n"
92                 "<extension>.pdf</extension>\n"
93                 "<mimetype>application/pdf</mimetype>\n"
94                 "<filetypename>Adobe PDF via poppler-cairo (*.pdf)</filetypename>\n"
95                 "<filetypetooltip>PDF Document</filetypetooltip>\n"
96             "</input>\n"
97         "</inkscape-extension>", new PdfInputCairo());
98 } // init
100 } } }  /* namespace Inkscape, Extension, Implementation */
102 #endif /* HAVE_POPPLER_GLIB */
104 /*
105   Local Variables:
106   mode:c++
107   c-file-style:"stroustrup"
108   c-file-offsets:((innamespace . 0)(inline-open . 0))
109   indent-tabs-mode:nil
110   fill-column:99
111   End:
112 */
113 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :