Code

r19076@shi: ted | 2008-04-21 15:42:45 -0700
[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  *
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     printf("Attempting to open using PdfInputCairo\n");
40     gchar* filename_uri = g_filename_to_uri(uri, NULL, NULL);
42     PopplerDocument* document = poppler_document_new_from_file(filename_uri, NULL, NULL);
43     if (document == NULL)
44         return NULL;
46     double width, height;
47     PopplerPage* page = poppler_document_get_page(document, 0);
48     poppler_page_get_size(page, &width, &height);
50     Glib::ustring* output = new Glib::ustring("");
51     cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
52                                                                    output, width, height);
53     cairo_t* cr = cairo_create(surface);
55     poppler_page_render(page, cr);
56     cairo_show_page(cr);
58     cairo_destroy(cr);
59     cairo_surface_destroy(surface);
61     SPDocument * doc = sp_document_new_from_mem(output->c_str(), output->length(), TRUE);
63     delete output;
64     g_object_unref(page);
65     g_object_unref(document);
67     return doc;
68 }
70 static cairo_status_t
71         _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length)
72 {
73     Glib::ustring* stream = (Glib::ustring*)closure;
74     stream->append((const char*)data, length);
76     return CAIRO_STATUS_SUCCESS;
77 }
80 #include "clear-n_.h"
82 void
83 PdfInputCairo::init(void) {
84     Inkscape::Extension::Extension * ext;
86     ext = Inkscape::Extension::build_from_mem(
87         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
88             "<name>PDF Input</name>\n"
89             "<id>org.inkscape.input.pdf</id>\n"
90             "<input>\n"
91                 "<extension>.pdf</extension>\n"
92                 "<mimetype>application/pdf</mimetype>\n"
93                 "<filetypename>Adobe PDF (*.pdf)</filetypename>\n"
94                 "<filetypetooltip>PDF Document</filetypetooltip>\n"
95             "</input>\n"
96         "</inkscape-extension>", new PdfInputCairo());
97 } // init
99 } } }  /* namespace Inkscape, Extension, Implementation */
101 #endif /* HAVE_POPPLER_GLIB */
103 /*
104   Local Variables:
105   mode:c++
106   c-file-style:"stroustrup"
107   c-file-offsets:((innamespace . 0)(inline-open . 0))
108   indent-tabs-mode:nil
109   fill-column:99
110   End:
111 */
112 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :