pdf-input-cairo.cpp revision 368f41429bd8478e4d22f20c54523c6a28ee5bce
df8bdeb362277e8d95a74d6c097341fe97409948johnz /*
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Simple PDF import extension using libpoppler and Cairo's SVG surface.
df8bdeb362277e8d95a74d6c097341fe97409948johnz *
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Authors:
df8bdeb362277e8d95a74d6c097341fe97409948johnz * miklos erdelyi
df8bdeb362277e8d95a74d6c097341fe97409948johnz *
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Copyright (C) 2007 Authors
df8bdeb362277e8d95a74d6c097341fe97409948johnz *
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Released under GNU GPL, read the file 'COPYING' for more information
df8bdeb362277e8d95a74d6c097341fe97409948johnz *
df8bdeb362277e8d95a74d6c097341fe97409948johnz */
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#ifdef HAVE_CONFIG_H
df8bdeb362277e8d95a74d6c097341fe97409948johnz# include <config.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#endif
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#ifdef HAVE_POPPLER_GLIB
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "pdf-input-cairo.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "extension/system.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "extension/input.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "document.h"
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <cairo-svg.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <poppler/glib/poppler.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <poppler/glib/poppler-document.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <poppler/glib/poppler-page.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnznamespace Inkscape {
df8bdeb362277e8d95a74d6c097341fe97409948johnznamespace Extension {
df8bdeb362277e8d95a74d6c097341fe97409948johnznamespace Internal {
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic cairo_status_t _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzSPDocument *
df8bdeb362277e8d95a74d6c097341fe97409948johnzPdfInputCairo::open(Inkscape::Extension::Input * mod, const gchar * uri) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz gchar* filename_uri = g_filename_to_uri(uri, NULL, NULL);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz PopplerDocument* document = poppler_document_new_from_file(filename_uri, NULL, NULL);
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (document == NULL)
df8bdeb362277e8d95a74d6c097341fe97409948johnz return NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz double width, height;
df8bdeb362277e8d95a74d6c097341fe97409948johnz PopplerPage* page = poppler_document_get_page(document, 0);
df8bdeb362277e8d95a74d6c097341fe97409948johnz poppler_page_get_size(page, &width, &height);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz Glib::ustring* output = new Glib::ustring("");
df8bdeb362277e8d95a74d6c097341fe97409948johnz cairo_surface_t* surface = cairo_svg_surface_create_for_stream(Inkscape::Extension::Internal::_write_ustring_cb,
df8bdeb362277e8d95a74d6c097341fe97409948johnz output, width, height);
df8bdeb362277e8d95a74d6c097341fe97409948johnz cairo_t* cr = cairo_create(surface);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz poppler_page_render(page, cr);
df8bdeb362277e8d95a74d6c097341fe97409948johnz cairo_show_page(cr);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
735564919188238196dbd0d320770dda59b38369Anthony Scarpino cairo_destroy(cr);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino cairo_surface_destroy(surface);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
df8bdeb362277e8d95a74d6c097341fe97409948johnz SPDocument * doc = sp_document_new_from_mem(output->c_str(), output->length(), TRUE);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz delete output;
735564919188238196dbd0d320770dda59b38369Anthony Scarpino g_object_unref(page);
df8bdeb362277e8d95a74d6c097341fe97409948johnz g_object_unref(document);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
735564919188238196dbd0d320770dda59b38369Anthony Scarpino return doc;
df8bdeb362277e8d95a74d6c097341fe97409948johnz}
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic cairo_status_t
df8bdeb362277e8d95a74d6c097341fe97409948johnz _write_ustring_cb(void *closure, const unsigned char *data, unsigned int length)
df8bdeb362277e8d95a74d6c097341fe97409948johnz{
df8bdeb362277e8d95a74d6c097341fe97409948johnz Glib::ustring* stream = (Glib::ustring*)closure;
df8bdeb362277e8d95a74d6c097341fe97409948johnz stream->append((const char*)data, length);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz return CAIRO_STATUS_SUCCESS;
df8bdeb362277e8d95a74d6c097341fe97409948johnz}
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "clear-n_.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzvoid
df8bdeb362277e8d95a74d6c097341fe97409948johnzPdfInputCairo::init(void) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz Inkscape::Extension::Extension * ext;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz ext = Inkscape::Extension::build_from_mem(
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<inkscape-extension>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<name>PDF Input</name>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<id>org.inkscape.input.pdf</id>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<input>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<extension>.pdf</extension>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "<mimetype>application/pdf</mimetype>\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM "<filetypename>Adobe PDF (*.pdf)</filetypename>\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM "<filetypetooltip>PDF Document</filetypetooltip>\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM "</input>\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "</inkscape-extension>", new PdfInputCairo());
df8bdeb362277e8d95a74d6c097341fe97409948johnz} // init
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz} } } /* namespace Inkscape, Extension, Implementation */
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#endif /* HAVE_POPPLER_GLIB */
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz/*
df8bdeb362277e8d95a74d6c097341fe97409948johnz Local Variables:
df8bdeb362277e8d95a74d6c097341fe97409948johnz mode:c++
735564919188238196dbd0d320770dda59b38369Anthony Scarpino c-file-style:"stroustrup"
df8bdeb362277e8d95a74d6c097341fe97409948johnz c-file-offsets:((innamespace . 0)(inline-open . 0))
df8bdeb362277e8d95a74d6c097341fe97409948johnz indent-tabs-mode:nil
df8bdeb362277e8d95a74d6c097341fe97409948johnz fill-column:99
df8bdeb362277e8d95a74d6c097341fe97409948johnz End:
735564919188238196dbd0d320770dda59b38369Anthony Scarpino*/
735564919188238196dbd0d320770dda59b38369Anthony Scarpino// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
735564919188238196dbd0d320770dda59b38369Anthony Scarpino