1 /*
2 * Simple PDF import extension using libpoppler and Cairo's SVG surface.
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_GLIB
19 #include "pdf-input-cairo.h"
20 #include "extension/system.h"
21 #include "extension/input.h"
22 #include "document.h"
24 #include <cairo-svg.h>
25 #include <poppler/glib/poppler.h>
26 #include <poppler/glib/poppler-document.h>
27 #include <poppler/glib/poppler-page.h>
29 namespace Inkscape {
30 namespace Extension {
31 namespace Internal {
33 static cairo_status_t _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length);
35 SPDocument *
36 PdfInputCairo::open(Inkscape::Extension::Input * mod, const gchar * uri) {
38 gchar* filename_uri = g_filename_to_uri(uri, NULL, NULL);
40 PopplerDocument* document = poppler_document_new_from_file(filename_uri, NULL, NULL);
41 if (document == NULL)
42 return NULL;
44 double width, height;
45 PopplerPage* page = poppler_document_get_page(document, 0);
46 poppler_page_get_size(page, &width, &height);
48 Glib::ustring* output = new Glib::ustring("");
49 cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
50 output, width, height);
51 cairo_t* cr = cairo_create(surface);
53 poppler_page_render(page, cr);
54 cairo_show_page(cr);
56 cairo_destroy(cr);
57 cairo_surface_destroy(surface);
59 SPDocument * doc = sp_document_new_from_mem(output->c_str(), output->length(), TRUE);
61 delete output;
62 g_object_unref(page);
63 g_object_unref(document);
65 return doc;
66 }
68 static cairo_status_t
69 _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length)
70 {
71 Glib::ustring* stream = (Glib::ustring*)closure;
72 stream->append((const char*)data, length);
74 return CAIRO_STATUS_SUCCESS;
75 }
78 #include "clear-n_.h"
80 void
81 PdfInputCairo::init(void) {
82 Inkscape::Extension::Extension * ext;
84 ext = Inkscape::Extension::build_from_mem(
85 "<inkscape-extension>\n"
86 "<name>PDF Input</name>\n"
87 "<id>org.inkscape.input.pdf</id>\n"
88 "<input>\n"
89 "<extension>.pdf</extension>\n"
90 "<mimetype>application/pdf</mimetype>\n"
91 "<filetypename>Adobe PDF (*.pdf)</filetypename>\n"
92 "<filetypetooltip>PDF Document</filetypetooltip>\n"
93 "</input>\n"
94 "</inkscape-extension>", new PdfInputCairo());
95 } // init
97 } } } /* namespace Inkscape, Extension, Implementation */
99 #endif /* HAVE_POPPLER_GLIB */
101 /*
102 Local Variables:
103 mode:c++
104 c-file-style:"stroustrup"
105 c-file-offsets:((innamespace . 0)(inline-open . 0))
106 indent-tabs-mode:nil
107 fill-column:99
108 End:
109 */
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :