Code

Add Miklos's new Cairo PDF rendering
authorishmal <ishmal@users.sourceforge.net>
Fri, 28 Jul 2006 19:18:18 +0000 (19:18 +0000)
committerishmal <ishmal@users.sourceforge.net>
Fri, 28 Jul 2006 19:18:18 +0000 (19:18 +0000)
src/extension/extension.h
src/extension/init.cpp
src/extension/internal/Makefile_insert
src/extension/internal/cairo-pdf-out.cpp [new file with mode: 0644]
src/extension/internal/cairo-pdf-out.h [new file with mode: 0644]
src/extension/internal/pdf-cairo.cpp [new file with mode: 0644]
src/extension/internal/pdf-cairo.h [new file with mode: 0644]

index de3dda19b53689eeff28bff668eb5ac3800e2250..81629a2dcaa53b386c0579479c55907c9ef91536 100644 (file)
@@ -45,6 +45,7 @@
 #define SP_MODULE_KEY_PRINT_PS    "org.inkscape.print.ps"
 /** Defines the key for PDF printing */
 #define SP_MODULE_KEY_PRINT_PDF    "org.inkscape.print.pdf"
+#define SP_MODULE_KEY_PRINT_CAIRO_PDF  "org.inkscape.print.pdf.cairo"
 /** Defines the key for LaTeX printing */
 #define SP_MODULE_KEY_PRINT_LATEX    "org.inkscape.print.latex"
 /** Defines the key for printing with GNOME Print */
index a1833bcde6c40c9a913ffe65e7d619ca6f90cd7b..48ebeff789ce0e89aa1874eee4987c3f64b97b5c 100644 (file)
@@ -26,6 +26,7 @@
 #include "internal/svgz.h"
 #include "internal/ps.h"
 #include "internal/pdf.h"
+#include "internal/pdf-cairo.h"
 #ifdef WITH_GNOME_PRINT
 # include "internal/gnome.h"
 #endif
@@ -34,6 +35,7 @@
 #endif
 #include "internal/ps-out.h"
 #include "internal/pdf-out.h"
+#include "internal/cairo-pdf-out.h"
 #include "internal/pov-out.h"
 #include "internal/odf.h"
 #include "internal/latex-pstricks-out.h"
@@ -107,6 +109,8 @@ init()
     Internal::PrintPS::init();
     Internal::PdfOutput::init();
     Internal::PrintPDF::init();
+    Internal::CairoPdfOutput::init();
+    Internal::PrintCairoPDF::init();
 #ifdef WITH_GNOME_PRINT
     Internal::PrintGNOME::init();
 #endif
index 834bf0d317c5f283715ba6c9cf73d7f263c8ba59..57fd17b3670ce5a3e3eda77f97eab50e8211e2b0 100644 (file)
@@ -14,10 +14,9 @@ endif
 extension_internal_libinternal_a_SOURCES =     \
        extension/internal/bluredge.h   \
        extension/internal/bluredge.cpp \
-        extension/internal/clear-n_.h   \
+  extension/internal/clear-n_.h   \
        extension/internal/grid.h       \
        extension/internal/grid.cpp     \
-\
        extension/internal/gimpgrad.h   \
        extension/internal/gimpgrad.cpp \
        extension/internal/svg.h        \
@@ -33,6 +32,10 @@ extension_internal_libinternal_a_SOURCES =   \
        extension/internal/pdf-mini.h   \
        extension/internal/pdf-out.h    \
        extension/internal/pdf-out.cpp  \
+       extension/internal/pdf-cairo.cpp \
+       extension/internal/pdf-cairo.h \
+       extension/internal/cairo-pdf-out.h      \
+       extension/internal/cairo-pdf-out.cpp    \
        extension/internal/eps-out.h    \
        extension/internal/eps-out.cpp  \
        extension/internal/gdkpixbuf-input.h    \
@@ -45,6 +48,5 @@ extension_internal_libinternal_a_SOURCES =    \
        extension/internal/latex-pstricks.h     \
        extension/internal/latex-pstricks-out.cpp       \
        extension/internal/latex-pstricks-out.h \
-\
        $(extension_internal_gnome_print_sources)
 
diff --git a/src/extension/internal/cairo-pdf-out.cpp b/src/extension/internal/cairo-pdf-out.cpp
new file mode 100644 (file)
index 0000000..52eb668
--- /dev/null
@@ -0,0 +1,137 @@
+/*\r
+ * A quick hack to use the print output to write out a file.  This\r
+ * then makes 'save as...' PDF.\r
+ *\r
+ * Authors:\r
+ *   Ted Gould <ted@gould.cx>\r
+ *   Ulf Erikson <ulferikson@users.sf.net>\r
+ *\r
+ * Copyright (C) 2004-2006 Authors\r
+ *\r
+ * Released under GNU GPL, read the file 'COPYING' for more information\r
+ */\r
+\r
+#ifdef HAVE_CONFIG_H\r
+# include <config.h>\r
+#endif\r
+#include "cairo-pdf-out.h"\r
+#include <print.h>\r
+#include "extension/system.h"\r
+#include "extension/print.h"\r
+#include "extension/db.h"\r
+#include "extension/output.h"\r
+#include "display/nr-arena.h"\r
+#include "display/nr-arena-item.h"\r
+#include "sp-path.h"\r
+\r
+namespace Inkscape {\r
+namespace Extension {\r
+namespace Internal {\r
+\r
+bool\r
+CairoPdfOutput::check (Inkscape::Extension::Extension * module)\r
+{\r
+       if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF))\r
+               return FALSE;\r
+\r
+       return TRUE;\r
+}\r
+\r
+\r
+static void\r
+pdf_print_document_to_file(SPDocument *doc, gchar const *filename)\r
+{\r
+    Inkscape::Extension::Print *mod;\r
+    SPPrintContext context;\r
+    gchar const *oldconst;\r
+    gchar *oldoutput;\r
+    unsigned int ret;\r
+\r
+    sp_document_ensure_up_to_date(doc);\r
+\r
+    mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_CAIRO_PDF);\r
+    oldconst = mod->get_param_string("destination");\r
+    oldoutput = g_strdup(oldconst);\r
+    mod->set_param_string("destination", (gchar *)filename);\r
+\r
+/* Start */\r
+    context.module = mod;\r
+    /* fixme: This has to go into module constructor somehow */\r
+    /* Create new arena */\r
+    mod->base = SP_ITEM(sp_document_root(doc));\r
+    mod->arena = NRArena::create();\r
+    mod->dkey = sp_item_display_key_new(1);\r
+    mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);\r
+    /* Print document */\r
+    ret = mod->begin(doc);\r
+    sp_item_invoke_print(mod->base, &context);\r
+    ret = mod->finish();\r
+    /* Release arena */\r
+    sp_item_invoke_hide(mod->base, mod->dkey);\r
+    mod->base = NULL;\r
+    nr_arena_item_unref(mod->root);\r
+    mod->root = NULL;\r
+    nr_object_unref((NRObject *) mod->arena);\r
+    mod->arena = NULL;\r
+/* end */\r
+\r
+    mod->set_param_string("destination", oldoutput);\r
+    g_free(oldoutput);\r
+\r
+    return;\r
+}\r
+\r
+\r
+/**\r
+    \brief  This function calls the print system with the filename\r
+       \param  mod   unused\r
+       \param  doc   Document to be saved\r
+    \param  uri   Filename to save to (probably will end in .pdf)\r
+\r
+       The most interesting thing that this function does is just attach\r
+       an '>' on the front of the filename.  This is the syntax used to\r
+       tell the printing system to save to file.\r
+*/\r
+void\r
+CairoPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)\r
+{\r
+    Inkscape::Extension::Extension * ext;\r
+\r
+    ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF);\r
+    if (ext == NULL)\r
+        return;\r
+\r
+       gchar * final_name;\r
+       final_name = g_strdup_printf("> %s", uri);\r
+       pdf_print_document_to_file(doc, final_name);\r
+       g_free(final_name);\r
+\r
+       return;\r
+}\r
+\r
+/**\r
+       \brief   A function allocate a copy of this function.\r
+\r
+       This is the definition of PDF out.  This function just\r
+       calls the extension system with the memory allocated XML that\r
+       describes the data.\r
+*/\r
+void\r
+CairoPdfOutput::init (void)\r
+{\r
+       Inkscape::Extension::build_from_mem(\r
+               "<inkscape-extension>\n"\r
+                       "<name>Cairo PDF Output</name>\n"\r
+                       "<id>org.inkscape.output.pdf.cairo</id>\n"\r
+                       "<output>\n"\r
+                               "<extension>.pdf</extension>\n"\r
+                               "<mimetype>application/pdf</mimetype>\n"\r
+                               "<filetypename>Cairo PDF (*.pdf)</filetypename>\n"\r
+                               "<filetypetooltip>PDF File</filetypetooltip>\n"\r
+                       "</output>\n"\r
+               "</inkscape-extension>", new CairoPdfOutput());\r
+\r
+       return;\r
+}\r
+\r
+} } }  /* namespace Inkscape, Extension, Implementation */\r
diff --git a/src/extension/internal/cairo-pdf-out.h b/src/extension/internal/cairo-pdf-out.h
new file mode 100644 (file)
index 0000000..394c074
--- /dev/null
@@ -0,0 +1,46 @@
+/*\r
+ * A quick hack to use the print output to write out a file.  This\r
+ * then makes 'save as...' PDF.\r
+ *\r
+ * Authors:\r
+ *   Ted Gould <ted@gould.cx>\r
+ *   Ulf Erikson <ulferikson@users.sf.net>\r
+ *\r
+ * Copyright (C) 2004-2006 Authors\r
+ *\r
+ * Released under GNU GPL, read the file 'COPYING' for more information\r
+ */\r
+\r
+#ifndef EXTENSION_INTERNAL_CAIRO_PDF_OUT_H\r
+#define EXTENSION_INTERNAL_CAIRO_PDF_OUT_H\r
+\r
+#include "extension/implementation/implementation.h"\r
+\r
+namespace Inkscape {\r
+namespace Extension {\r
+namespace Internal {\r
+\r
+class CairoPdfOutput : Inkscape::Extension::Implementation::Implementation {\r
+\r
+public:\r
+    bool check(Inkscape::Extension::Extension *module);\r
+    void save(Inkscape::Extension::Output *mod,\r
+              SPDocument *doc,\r
+              gchar const *uri);\r
+    static void init();\r
+};\r
+\r
+} } }  /* namespace Inkscape, Extension, Implementation */\r
+\r
+#endif /* !EXTENSION_INTERNAL_CAIRO_PDF_OUT_H */\r
+\r
+/*\r
+  Local Variables:\r
+  mode:c++\r
+  c-file-style:"stroustrup"\r
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
+  indent-tabs-mode:nil\r
+  fill-column:99\r
+  End:\r
+*/\r
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :\r
diff --git a/src/extension/internal/pdf-cairo.cpp b/src/extension/internal/pdf-cairo.cpp
new file mode 100644 (file)
index 0000000..c35fbe8
--- /dev/null
@@ -0,0 +1,1030 @@
+#define __SP_PDF_CAIRO_C__
+
+/** \file
+ * PDF printing with Cairo.
+ */
+/*
+ * Authors:
+ *   Miklós Erdélyi <erdelyim@gmail.com>
+ * 
+ * Based on pdf.cpp
+ *
+ * Licensed under GNU GPL
+ */
+
+/* Plain Print */
+
+#define PANGO_ENABLE_BACKEND
+#define PANGO_ENABLE_ENGINE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <signal.h>
+#include <errno.h>
+
+#include <libnr/n-art-bpath.h>
+
+#include <glib/gmem.h>
+#include <gtk/gtkstock.h>
+#include <gtk/gtkvbox.h>
+#include <gtk/gtkframe.h>
+#include <gtk/gtkradiobutton.h>
+#include <gtk/gtkcombo.h>
+#include <gtk/gtklabel.h>
+#include <gtk/gtkentry.h>
+#include <gtk/gtktooltips.h>
+
+#include <glibmm/i18n.h>
+#include "display/nr-arena-item.h"
+#include "display/canvas-bpath.h"
+#include "sp-item.h"
+#include "style.h"
+#include "sp-linear-gradient.h"
+#include "sp-radial-gradient.h"
+
+#include "libnrtype/font-instance.h"
+#include "libnrtype/font-style-to-pos.h"
+
+#include <unit-constants.h>
+
+#include "pdf-cairo.h"
+#include "extension/system.h"
+#include "extension/print.h"
+
+#include "io/sys.h"
+
+#include "cairo.h"
+#include "cairo-pdf.h"
+#include "cairo-ft.h"
+
+#include <pango/pangofc-fontmap.h>
+
+#ifdef USE_PANGO_CAIRO
+#include <pango/pangocairo.h>
+#endif
+
+namespace Inkscape {
+namespace Extension {
+namespace Internal {
+
+static cairo_status_t _write_callback(void *closure, const unsigned char *data, unsigned int length);
+static void _concat_transform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
+
+PrintCairoPDF::PrintCairoPDF() :
+    cr(NULL),
+    pdf_surface(NULL),
+    _layout(NULL),
+    _dpi(72),
+    _bitmap(false)
+{
+    _num_alphas = 8;
+    _alpha_stack = (float*)malloc(sizeof(float) * _num_alphas);
+}
+
+PrintCairoPDF::~PrintCairoPDF(void)
+{
+    if (cr) cairo_destroy(cr);
+    if (pdf_surface) cairo_surface_destroy(pdf_surface);
+    if (_layout) g_object_unref(_layout);
+    
+    free(_alpha_stack);
+
+    /* restore default signal handling for SIGPIPE */
+#if !defined(_WIN32) && !defined(__WIN32__)
+    (void) signal(SIGPIPE, SIG_DFL);
+#endif
+
+    return;
+}
+
+unsigned int
+PrintCairoPDF::setup(Inkscape::Extension::Print * mod)
+{
+    static gchar const *const pdr[] = {"72", "75", "100", "144", "150", "200", "300", "360", "600", "1200", "2400", NULL};
+
+#ifdef TED
+    Inkscape::XML::Node *repr = ((SPModule *) mod)->repr;
+#endif
+
+    unsigned int ret = FALSE;
+
+    /* Create dialog */
+    GtkTooltips *tt = gtk_tooltips_new();
+    g_object_ref((GObject *) tt);
+    gtk_object_sink((GtkObject *) tt);
+
+    GtkWidget *dlg = gtk_dialog_new_with_buttons(_("Print Destination"),
+//            SP_DT_WIDGET(SP_ACTIVE_DESKTOP)->window,
+            NULL,
+            (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT),
+            GTK_STOCK_CANCEL,
+            GTK_RESPONSE_CANCEL,
+            GTK_STOCK_PRINT,
+            GTK_RESPONSE_OK,
+            NULL);
+
+    gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
+
+    GtkWidget *vbox = GTK_DIALOG(dlg)->vbox;
+    gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
+    /* Print properties frame */
+    GtkWidget *f = gtk_frame_new(_("Print properties"));
+    gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
+    GtkWidget *vb = gtk_vbox_new(FALSE, 4);
+    gtk_container_add(GTK_CONTAINER(f), vb);
+    gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
+    /* Print type */
+    bool const p2bm = mod->get_param_bool("bitmap");
+    GtkWidget *rb = gtk_radio_button_new_with_label(NULL, _("Print using PDF operators"));
+    gtk_tooltips_set_tip((GtkTooltips *) tt, rb,
+                         _("Use PDF vector operators. The resulting image is usually smaller "
+                           "in file size and can be arbitrarily scaled, but "
+                           "patterns will be lost."), NULL);
+    if (!p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);
+    gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);
+    rb = gtk_radio_button_new_with_label(gtk_radio_button_get_group((GtkRadioButton *) rb), _("Print as bitmap"));
+    gtk_tooltips_set_tip((GtkTooltips *) tt, rb,
+                         _("Print everything as bitmap. The resulting image is usually larger "
+                           "in file size and cannot be arbitrarily scaled without quality loss, "
+                           "but all objects will be rendered exactly as displayed."), NULL);
+    if (p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);
+    gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);
+    /* Resolution */
+    GtkWidget *hb = gtk_hbox_new(FALSE, 4);
+    gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
+    GtkWidget *combo = gtk_combo_new();
+    gtk_combo_set_value_in_list(GTK_COMBO(combo), FALSE, FALSE);
+    gtk_combo_set_use_arrows(GTK_COMBO(combo), TRUE);
+    gtk_combo_set_use_arrows_always(GTK_COMBO(combo), TRUE);
+    gtk_widget_set_size_request(combo, 64, -1);
+    gtk_tooltips_set_tip((GtkTooltips *) tt, GTK_COMBO(combo)->entry,
+                         _("Preferred resolution (dots per inch) of bitmap"), NULL);
+    /* Setup strings */
+    GList *sl = NULL;
+    for (unsigned i = 0; pdr[i] != NULL; i++) {
+        sl = g_list_prepend(sl, (gpointer) pdr[i]);
+    }
+    sl = g_list_reverse(sl);
+    gtk_combo_set_popdown_strings(GTK_COMBO(combo), sl);
+    g_list_free(sl);
+    if (1) {
+        gchar const *val = mod->get_param_string("resolution");
+        gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), val);
+    }
+    gtk_box_pack_end(GTK_BOX(hb), combo, FALSE, FALSE, 0);
+    GtkWidget *l = gtk_label_new(_("Resolution:"));
+    gtk_box_pack_end(GTK_BOX(hb), l, FALSE, FALSE, 0);
+
+    /* Print destination frame */
+    f = gtk_frame_new(_("Print destination"));
+    gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);
+    vb = gtk_vbox_new(FALSE, 4);
+    gtk_container_add(GTK_CONTAINER(f), vb);
+    gtk_container_set_border_width(GTK_CONTAINER(vb), 4);
+
+    l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"
+                        "leave empty to use the system default printer.\n"
+                        "Use '> filename' to print to file.\n"
+                        "Use '| prog arg...' to pipe to a program."));
+    gtk_box_pack_start(GTK_BOX(vb), l, FALSE, FALSE, 0);
+
+    GtkWidget *e = gtk_entry_new();
+    if (1) {
+        gchar const *val = mod->get_param_string("destination");
+        gtk_entry_set_text(GTK_ENTRY(e), ( val != NULL
+                                           ? val
+                                           : "" ));
+    }
+    gtk_box_pack_start(GTK_BOX(vb), e, FALSE, FALSE, 0);
+
+    // pressing enter in the destination field is the same as clicking Print:
+    gtk_entry_set_activates_default(GTK_ENTRY(e), TRUE);
+
+    gtk_widget_show_all(vbox);
+
+    int const response = gtk_dialog_run(GTK_DIALOG(dlg));
+
+    g_object_unref((GObject *) tt);
+
+    if (response == GTK_RESPONSE_OK) {
+        gchar const *fn;
+        char const *sstr;
+
+        _bitmap = gtk_toggle_button_get_active((GtkToggleButton *) rb);
+        sstr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
+        _dpi = (unsigned int) MAX((int)(atof(sstr)), 1);
+        /* Arrgh, have to do something */
+        fn = gtk_entry_get_text(GTK_ENTRY(e));
+        /* skip leading whitespace, bug #1068483 */
+        while (fn && *fn==' ') { fn++; }
+        /* g_print("Printing to %s\n", fn); */
+
+        mod->set_param_bool("bitmap", _bitmap);
+        mod->set_param_string("resolution", (gchar *)sstr);
+        mod->set_param_string("destination", (gchar *)fn);
+        ret = TRUE;
+    }
+
+    gtk_widget_destroy(dlg);
+
+    return ret;
+}
+
+unsigned int
+PrintCairoPDF::begin(Inkscape::Extension::Print *mod, SPDocument *doc)
+{
+    FILE *osf = NULL;
+    FILE *osp = NULL;
+
+    _alpha_ptr = 0;
+    _alpha_stack[_alpha_ptr] = 1.0;
+
+    gsize bytesRead = 0;
+    gsize bytesWritten = 0;
+    GError *error = NULL;
+    gchar const *utf8_fn = mod->get_param_string("destination");
+    gchar *local_fn = g_filename_from_utf8( utf8_fn,
+                                            -1,  &bytesRead,  &bytesWritten, &error);
+    gchar const *fn = local_fn;
+
+    /* TODO: Replace the below fprintf's with something that does the right thing whether in
+     * gui or batch mode (e.g. --print=blah).  Consider throwing an exception: currently one of
+     * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
+     * return code.
+     */
+    if (fn != NULL) {
+        if (*fn == '|') {
+            fn += 1;
+            while (isspace(*fn)) fn += 1;
+#ifndef WIN32
+            osp = popen(fn, "w");
+#else
+            osp = _popen(fn, "w");
+#endif
+            if (!osp) {
+                fprintf(stderr, "inkscape: popen(%s): %s\n",
+                        fn, strerror(errno));
+                return 0;
+            }
+            _stream = osp;
+        } else if (*fn == '>') {
+            fn += 1;
+            while (isspace(*fn)) fn += 1;
+            Inkscape::IO::dump_fopen_call(fn, "K");
+            osf = Inkscape::IO::fopen_utf8name(fn, "w+");
+            if (!osf) {
+                fprintf(stderr, "inkscape: fopen(%s): %s\n",
+                        fn, strerror(errno));
+                return 0;
+            }
+            _stream = osf;
+        } else {
+            /* put cwd stuff in here */
+            gchar *qn = ( *fn
+                          ? g_strdup_printf("lpr -P %s", fn)  /* FIXME: quote fn */
+                          : g_strdup("lpr") );
+#ifndef WIN32
+            osp = popen(qn, "w");
+#else
+            osp = _popen(qn, "w");
+#endif
+            if (!osp) {
+                fprintf(stderr, "inkscape: popen(%s): %s\n",
+                        qn, strerror(errno));
+                return 0;
+            }
+            g_free(qn);
+            _stream = osp;
+        }
+    }
+
+    g_free(local_fn);
+
+    if (_stream) {
+        /* fixme: this is kinda icky */
+#if !defined(_WIN32) && !defined(__WIN32__)
+        (void) signal(SIGPIPE, SIG_IGN);
+#endif
+    }
+    
+    // test output stream?
+
+    // width and height in pt
+    _width = sp_document_width(doc) * PT_PER_PX;
+    _height = sp_document_height(doc) * PT_PER_PX;
+
+    NRRect d;
+    bool   pageBoundingBox;
+    pageBoundingBox = mod->get_param_bool("pageBoundingBox");
+    // printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
+    if (pageBoundingBox) {
+        d.x0 = d.y0 = 0;
+        d.x1 = ceil(_width);
+        d.y1 = ceil(_height);
+    } else {
+        SPItem* doc_item = SP_ITEM(sp_document_root(doc));
+        sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
+        // convert from px to pt
+        d.x0 *= PT_PER_PX;
+        d.x1 *= PT_PER_PX;
+        d.y0 *= PT_PER_PX;
+        d.y1 *= PT_PER_PX;
+    }
+    printf("%f %f\n", _width, _height);
+
+    pdf_surface = cairo_pdf_surface_create_for_stream(Inkscape::Extension::Internal::_write_callback, _stream, d.x1-d.x0, d.y1-d.y0);
+    cr = cairo_create(pdf_surface);
+    
+    if (!_bitmap) {
+       cairo_scale(cr, PT_PER_PX, PT_PER_PX);
+       
+        // from now on we can output px, but they will be treated as pt
+        // note that the we do not have to flip the y axis
+        // because Cairo's coordinate system is identical to Inkscape's
+    }   
+
+    return 1;
+}
+
+unsigned int
+PrintCairoPDF::finish(Inkscape::Extension::Print *mod)
+{
+    if (!_stream) return 0;
+    if (_bitmap) return 0;
+
+    cairo_show_page(cr);       
+
+    cairo_destroy(cr);
+    cairo_surface_destroy(pdf_surface);
+    cr = NULL;
+    pdf_surface = NULL;
+
+    /* Flush stream to be sure. */
+    (void) fflush(_stream);
+
+    /* fixme: should really use pclose for popen'd streams */
+    fclose(_stream);
+    _stream = 0;
+
+    return 0;
+}
+
+unsigned int
+PrintCairoPDF::bind(Inkscape::Extension::Print *mod, NRMatrix const *transform, float opacity)
+{
+    if (!_stream) return 0;  // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+    
+    cairo_save(cr);
+    _concat_transform(cr, transform->c[0], transform->c[1], transform->c[2], transform->c[3], transform->c[4], transform->c[5]);
+//    g_printf("bind: (%f) %f %f %f %f %f %f\n", opacity, transform->c[0], transform->c[1], transform->c[2], transform->c[3], transform->c[4], transform->c[5]);
+    // remember these to be able to undo them when outputting text
+    _last_tx = transform->c[4];
+    _last_ty = transform->c[5];
+    
+    if (_num_alphas <= ++_alpha_ptr) {
+        _num_alphas *= 2;
+        _alpha_stack = (float*)realloc((void*)_alpha_stack, sizeof(float) * _num_alphas);
+    }
+    _alpha_stack[_alpha_ptr] = _alpha_stack[_alpha_ptr-1] * opacity;
+    
+    return 1;
+}
+
+unsigned int
+PrintCairoPDF::release(Inkscape::Extension::Print *mod)
+{
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+    
+    cairo_restore(cr);
+//    g_printf("release\n");
+    if (_alpha_ptr > 0)
+        _alpha_ptr--;
+
+    return 1;
+}
+
+unsigned int
+PrintCairoPDF::comment(Inkscape::Extension::Print *mod, char const *comment)
+{
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+
+    return 1;
+}
+
+cairo_pattern_t*
+PrintCairoPDF::create_pattern_for_paint(SPPaintServer const *const paintserver, NRRect const *pbox, float alpha)
+{
+       cairo_pattern_t *pattern = NULL;
+    bool apply_bbox2user = false;
+    
+    if (SP_IS_LINEARGRADIENT (paintserver)) {
+
+            SPLinearGradient *lg=SP_LINEARGRADIENT(paintserver);
+            
+            sp_gradient_ensure_vector(SP_GRADIENT(lg)); // when exporting from commandline, vector is not built
+
+            NR::Point p1 (lg->x1.computed, lg->y1.computed);
+            NR::Point p2 (lg->x2.computed, lg->y2.computed);
+            if (pbox && SP_GRADIENT(lg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
+                // convert to userspace
+                NR::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);
+                p1 *= bbox2user;
+                p2 *= bbox2user;
+            }
+            
+            // create linear gradient pattern
+            pattern = cairo_pattern_create_linear(p1[NR::X], p1[NR::Y], p2[NR::X], p2[NR::Y]);
+            
+            // add stops
+            for (gint i = 0; unsigned(i) < lg->vector.stops.size(); i++) {
+                float rgb[3];
+                sp_color_get_rgb_floatv(&lg->vector.stops[i].color, rgb);
+                cairo_pattern_add_color_stop_rgba(pattern, lg->vector.stops[i].offset, rgb[0], rgb[1], rgb[2], lg->vector.stops[i].opacity * alpha);
+            }
+    } else if (SP_IS_RADIALGRADIENT (paintserver)) {
+
+        SPRadialGradient *rg=SP_RADIALGRADIENT(paintserver);
+
+        sp_gradient_ensure_vector(SP_GRADIENT(rg)); // when exporting from commandline, vector is not built
+
+        NR::Point c (rg->cx.computed, rg->cy.computed);
+        NR::Point f (rg->fx.computed, rg->fy.computed);
+        double r = rg->r.computed;
+        if (pbox && SP_GRADIENT(rg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX)
+            apply_bbox2user = true;
+        
+        // create radial gradient pattern
+        pattern = cairo_pattern_create_radial(f[NR::X], f[NR::Y], 0, c[NR::X], c[NR::Y], r);
+
+        // add stops
+        for (gint i = 0; unsigned(i) < rg->vector.stops.size(); i++) {
+            float rgb[3];
+            sp_color_get_rgb_floatv(&rg->vector.stops[i].color, rgb);
+            cairo_pattern_add_color_stop_rgba(pattern, rg->vector.stops[i].offset, rgb[0], rgb[1], rgb[2], rg->vector.stops[i].opacity * alpha);
+        }
+    }
+        
+    if (pattern) {
+        SPGradient *g = SP_GRADIENT(paintserver);
+
+        // set extend type
+        SPGradientSpread spread = sp_gradient_get_spread(g);
+       switch (spread) {
+                       case SP_GRADIENT_SPREAD_REPEAT:
+                               cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
+                               break;
+                       case SP_GRADIENT_SPREAD_REFLECT:        // not supported by cairo-pdf yet
+                               //cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REFLECT);
+                               //break;
+                       case SP_GRADIENT_SPREAD_PAD:
+            default:
+                               cairo_pattern_set_extend(pattern, CAIRO_EXTEND_NONE);   // PAD not supported by cairo-pdf yet
+                               break;
+               }
+
+        cairo_matrix_t pattern_matrix;         
+        if (g->gradientTransform_set) {
+               // apply gradient transformation
+               cairo_matrix_init(&pattern_matrix,
+                   g->gradientTransform[0], g->gradientTransform[1],
+                   g->gradientTransform[2], g->gradientTransform[3],
+                   g->gradientTransform[4], g->gradientTransform[5]);
+        } else {
+            cairo_matrix_init_identity (&pattern_matrix);
+        }
+
+        if (apply_bbox2user) {
+            // convert to userspace
+            cairo_matrix_t bbox2user;
+            cairo_matrix_init (&bbox2user, pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);
+            cairo_matrix_multiply (&pattern_matrix, &bbox2user, &pattern_matrix);
+        }
+        cairo_matrix_invert(&pattern_matrix);   // because Cairo expects a userspace->patternspace matrix
+        cairo_pattern_set_matrix(pattern, &pattern_matrix);
+    }
+        
+    return pattern;
+}
+
+void
+PrintCairoPDF::print_fill_style(cairo_t *cr, SPStyle const *const style, NRRect const *pbox)
+{
+    g_return_if_fail( style->fill.type == SP_PAINT_TYPE_COLOR
+                      || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+                           && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) );
+    
+    if (style->fill.type == SP_PAINT_TYPE_COLOR) {
+        float rgb[3];
+        sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
+
+        float alpha = 1.0;
+        alpha *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
+        alpha *= _alpha_stack[_alpha_ptr];
+
+        cairo_set_source_rgba(cr, rgb[0], rgb[1], rgb[2], alpha);
+    } else {
+        g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+                  && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
+
+        cairo_pattern_t *pattern = create_pattern_for_paint(SP_STYLE_FILL_SERVER(style), pbox, 1.0);
+        
+        if (pattern) {
+               cairo_set_source(cr, pattern);
+            cairo_pattern_destroy(pattern);
+        }
+    }
+}
+
+unsigned int
+PrintCairoPDF::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *const style,
+              NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
+{
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+
+    if ( style->fill.type == SP_PAINT_TYPE_COLOR
+        || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+             && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) ) {
+        
+        float alpha = 1.0;
+        alpha *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
+if (alpha != 1.0)
+g_printf( "fa: %f a:%f fa*a = %f\n", alpha, _alpha_stack[_alpha_ptr], alpha*_alpha_stack[_alpha_ptr]);
+        alpha *= _alpha_stack[_alpha_ptr];
+               
+        cairo_save(cr);
+        
+        print_fill_style(cr, style, pbox);
+        print_bpath(cr, bpath->path);
+        if (style->fill_rule.value == SP_WIND_RULE_EVENODD) {
+            cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
+        } else {
+            cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
+        }
+        if (alpha != 1.0 &&
+            style->fill.type != SP_PAINT_TYPE_COLOR) {
+
+            cairo_clip (cr);
+            cairo_paint_with_alpha (cr, alpha);
+        } else {
+            cairo_fill(cr);
+        }
+        
+        cairo_restore(cr);
+    }
+
+    return 0;
+}
+
+void
+PrintCairoPDF::print_stroke_style(cairo_t *cr, SPStyle const *style, NRRect const *pbox)
+{
+    float alpha = 1.0;
+    alpha *= SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
+    alpha *= _alpha_stack[_alpha_ptr];
+
+    if ( style->stroke.type == SP_PAINT_TYPE_COLOR ) {
+        float rgb[3];
+        sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
+      
+        cairo_set_source_rgba(cr, rgb[0], rgb[1], rgb[2], alpha);
+    } else if ( style->stroke.type == SP_PAINT_TYPE_PAINTSERVER
+                  && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style)) ) {
+
+        cairo_pattern_t *pattern = create_pattern_for_paint(SP_STYLE_STROKE_SERVER(style), pbox, alpha);
+        
+        if (pattern) {
+               cairo_set_source(cr, pattern);
+            cairo_pattern_destroy(pattern);
+        }
+    }
+
+    if (style->stroke_dash.n_dash   &&
+        style->stroke_dash.dash       )
+    {
+        cairo_set_dash(cr, style->stroke_dash.dash, style->stroke_dash.n_dash, style->stroke_dash.offset);
+    } else {
+       cairo_set_dash(cr, NULL, 0, 0.0);       // disable dashing
+    }
+    
+    cairo_set_line_width(cr, style->stroke_width.computed);
+    
+    // set line join type
+    cairo_line_join_t join = CAIRO_LINE_JOIN_MITER;
+    switch (style->stroke_linejoin.computed) {
+       case SP_STROKE_LINEJOIN_MITER:
+           join = CAIRO_LINE_JOIN_MITER;
+           break;
+       case SP_STROKE_LINEJOIN_ROUND:
+           join = CAIRO_LINE_JOIN_ROUND;
+           break;
+       case SP_STROKE_LINEJOIN_BEVEL:
+           join = CAIRO_LINE_JOIN_BEVEL;
+           break;
+    }          
+    cairo_set_line_join(cr, join);
+    
+    // set line cap type
+    cairo_line_cap_t cap = CAIRO_LINE_CAP_BUTT;
+    switch (style->stroke_linecap.computed) {
+       case SP_STROKE_LINECAP_BUTT:
+           cap = CAIRO_LINE_CAP_BUTT;
+           break;
+       case SP_STROKE_LINECAP_ROUND:
+           cap = CAIRO_LINE_CAP_ROUND;
+           break;
+       case SP_STROKE_LINECAP_SQUARE:
+           cap = CAIRO_LINE_CAP_SQUARE;
+           break;
+    }          
+    cairo_set_line_cap(cr, cap);
+    cairo_set_miter_limit(cr, MAX(1, style->stroke_miterlimit.value));
+}
+
+unsigned int
+PrintCairoPDF::stroke(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
+                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
+{
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+
+    if ( style->stroke.type == SP_PAINT_TYPE_COLOR ||
+        ( style->stroke.type == SP_PAINT_TYPE_PAINTSERVER
+              && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style)) ) ) {
+
+       cairo_save(cr);
+       
+       print_stroke_style(cr, style, pbox);
+        print_bpath(cr, bpath->path);
+        cairo_stroke(cr);
+               
+        cairo_restore(cr);
+    }
+
+    return 0;
+}
+
+unsigned int
+PrintCairoPDF::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsigned int h, unsigned int rs,
+               NRMatrix const *transform, SPStyle const *style)
+{
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+    
+    guchar* px_rgba = (guchar*)g_malloc(4 * w * h);
+    if (!px_rgba) return 0;
+    
+    float alpha = _alpha_stack[_alpha_ptr];
+
+    // make a copy of the original pixbuf with premultiplied alpha
+    // if we pass the original pixbuf it will get messed up
+    for (unsigned i = 0; i < h; i++) {
+       for (unsigned j = 0; j < w; j++) {
+            guchar const *src = px + i * rs + j * 4;
+            guint32 *dst = (guint32 *)(px_rgba + i * rs + j * 4);
+            guchar r, g, b, alpha_dst;
+            
+            // calculate opacity-modified alpha
+            alpha_dst = src[3];
+            if (alpha != 1.0)
+                alpha_dst = (guchar)ceil((float)alpha_dst * alpha);
+
+            // premul alpha (needed because this will be undone by cairo-pdf)
+            r = src[0]*alpha_dst/255;
+            g = src[1]*alpha_dst/255;
+            b = src[2]*alpha_dst/255;
+            
+            *dst = (((alpha_dst) << 24) | (((r)) << 16) | (((g)) << 8) | (b));
+       }
+    }
+    
+    cairo_surface_t *image_surface = cairo_image_surface_create_for_data(px_rgba, CAIRO_FORMAT_ARGB32, w, h, w * 4);
+    if (cairo_surface_status(image_surface)) {
+       g_printf("%s\n", cairo_status_to_string(cairo_surface_status(image_surface)));
+       return 0;
+    }
+    
+    cairo_save(cr);
+    
+       // scaling by width & height is not needed because it will be done by cairo-pdf
+       // we have to translate down by height also in order to eliminate the last translation done by sp_image_print
+    cairo_matrix_t matrix;
+    cairo_matrix_init(&matrix,
+                   transform->c[0]/w, transform->c[1],
+                   transform->c[2], -transform->c[3]/h,
+                   transform->c[4], transform->c[5] + transform->c[3]);
+    cairo_transform(cr, &matrix);
+
+    cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image_surface);
+    cairo_pattern_set_extend(pattern, CAIRO_EXTEND_NONE);
+
+    cairo_set_source(cr, pattern);
+    cairo_pattern_destroy(pattern);
+    
+    // set clip region so that the pattern will not be repeated (bug in Cairo prior 1.2.1)
+    cairo_new_path(cr);
+    cairo_rectangle(cr, 0, 0, w, h);
+    cairo_clip(cr);
+
+    cairo_paint(cr);
+    
+    cairo_restore(cr);
+    
+    cairo_surface_destroy(image_surface);
+    g_free(px_rgba);
+    
+    return 0;
+}
+
+#define GLYPH_ARRAY_SIZE 64
+
+NR::Point
+PrintCairoPDF::draw_glyphs(cairo_t *cr, NR::Point p, PangoFont *font, PangoGlyphString *glyph_string,
+               bool vertical, bool stroke)
+{
+    cairo_glyph_t glyph_array[GLYPH_ARRAY_SIZE];
+    cairo_glyph_t *glyphs = glyph_array;
+    if (glyph_string->num_glyphs > GLYPH_ARRAY_SIZE)
+        glyphs = (cairo_glyph_t*)g_malloc(sizeof(cairo_glyph_t) * glyph_string->num_glyphs);
+
+    PangoGlyphUnit x_offset = 0, y_offset = 0;
+    PangoGlyphInfo *info;
+    int num_invalid_glyphs = 0;
+    for (gint i = 0; i < glyph_string->num_glyphs; i++) {
+        info = &glyph_string->glyphs[i];
+        // skip empty or unknown glyphs
+        if (info->glyph == PANGO_GLYPH_EMPTY || info->glyph & PANGO_GLYPH_UNKNOWN_FLAG) {
+            num_invalid_glyphs++;
+            continue;
+        }
+        glyphs[i - num_invalid_glyphs].index = info->glyph;
+        glyphs[i - num_invalid_glyphs].x = p[NR::X] + (x_offset + info->geometry.x_offset)/PANGO_SCALE;
+        glyphs[i - num_invalid_glyphs].y = p[NR::Y] + (y_offset + info->geometry.y_offset)/PANGO_SCALE;
+
+        if (vertical) {
+            cairo_text_extents_t extents;
+            cairo_glyph_extents(cr, &glyphs[i - num_invalid_glyphs], 1, &extents);
+            y_offset += (PangoGlyphUnit)(extents.y_advance * PANGO_SCALE);
+        }
+        else
+            x_offset += info->geometry.width;
+    }
+
+    if (stroke)
+        cairo_glyph_path(cr, glyphs, glyph_string->num_glyphs - num_invalid_glyphs);
+    else
+        cairo_show_glyphs(cr, glyphs, glyph_string->num_glyphs - num_invalid_glyphs);
+
+    if (glyph_string->num_glyphs > GLYPH_ARRAY_SIZE)
+        g_free(glyphs);
+
+    return NR::Point(x_offset, y_offset);
+}
+
+unsigned int
+PrintCairoPDF::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
+              SPStyle const *const style)
+{
+    bool dirty_pattern = false;
+
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
+    if (_bitmap) return 0;
+
+    cairo_save(cr);
+
+    // this needs to be there to encounter Cairo's userspace locking semantics for set_source
+    // otherwise gradients won't be shown correctly
+    cairo_translate(cr, -_last_tx, -_last_ty);
+
+    // create pango layout and context if needed
+    if (_layout == NULL) {
+#ifdef USE_PANGO_CAIRO
+        //_context = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(pango_cairo_font_map_get_default()));
+        _layout = pango_cairo_create_layout(cr);
+#else
+        _layout = pango_layout_new((font_factory::Default())->fontContext);
+#endif
+    }
+
+    // create font instance from style and use it
+    font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
+    if (tf == NULL) {   // do something
+        g_printf("Warning: trouble getting font_instance\n");
+        tf = (font_factory::Default())->Face("sans", font_style_to_pos(*style));
+    }        
+    PangoFontDescription *adjusted = pango_font_description_copy(tf->descr);
+    
+    pango_font_description_set_absolute_size(adjusted, (gint)(style->font_size.computed * PANGO_SCALE));
+    pango_layout_set_font_description(_layout, adjusted);
+    pango_layout_set_text(_layout, text, -1);
+
+    pango_font_description_free(adjusted);
+    tf->Unref();
+
+#ifdef USE_PANGO_CAIRO
+    pango_cairo_update_layout(cr, _layout);
+#else
+    pango_layout_context_changed(_layout);   // is this needed?
+#endif
+
+    PangoLayoutLine *line = pango_layout_get_line(_layout, 0);
+    if (line == NULL)
+        return 0;
+
+#ifndef USE_PANGO_CAIRO
+    // apply the selected font
+    double size;
+    PangoLayoutRun *first_run = (PangoLayoutRun *)line->runs->data;
+    PangoFcFont *fc_font = PANGO_FC_FONT(first_run->item->analysis.font);
+    FcPattern *fc_pattern = fc_font->font_pattern;
+
+    if ( style->writing_mode.set &&
+       (style->writing_mode.computed == SP_CSS_WRITING_MODE_TB_RL ||
+        style->writing_mode.computed == SP_CSS_WRITING_MODE_TB_LR) )
+    {
+        FcPatternDel(fc_pattern, FC_VERTICAL_LAYOUT);
+        FcPatternAddBool(fc_pattern, FC_VERTICAL_LAYOUT, FcTrue);
+        dirty_pattern = true;
+    }
+
+    cairo_font_face_t *font_face = cairo_ft_font_face_create_for_pattern(fc_pattern);
+    cairo_set_font_face(cr, font_face);
+
+    if (FcPatternGetDouble(fc_pattern, FC_PIXEL_SIZE, 0, &size) != FcResultMatch)
+        size = 12.0;
+
+    // adjust size and horizontal flip
+    cairo_matrix_t matrix;
+    cairo_get_font_matrix(cr, &matrix);
+    matrix.xx = size;
+    matrix.yy = -matrix.xx;
+    cairo_set_font_matrix(cr, &matrix);
+#endif
+    
+    if ( style->fill.type == SP_PAINT_TYPE_COLOR
+         || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+              && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )
+    {
+        // set fill style
+        print_fill_style(cr, style, NULL);
+
+#ifndef USE_PANGO_CAIRO
+        NR::Point cursor(_last_tx, _last_ty);
+        for (GSList *tmpList = line->runs; tmpList && tmpList->data; tmpList = tmpList->next) {
+            PangoLayoutRun *run = (PangoLayoutRun *)tmpList->data;
+            cursor += draw_glyphs(cr, cursor, run->item->analysis.font, run->glyphs, dirty_pattern, false) / PANGO_SCALE;
+        }
+#else
+        cairo_new_path(cr);
+        cairo_move_to(cr, _last_tx, _last_ty);
+        pango_cairo_show_layout_line(cr, line);
+#endif
+    }
+
+    if (style->stroke.type == SP_PAINT_TYPE_COLOR
+         || ( style->stroke.type == SP_PAINT_TYPE_PAINTSERVER
+              && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style)) ) )
+    {
+        // set stroke style
+        print_stroke_style(cr, style, NULL);
+
+        // paint stroke
+#ifndef USE_PANGO_CAIRO
+        NR::Point cursor(_last_tx, _last_ty);
+        for (GSList *tmpList = line->runs; tmpList && tmpList->data; tmpList = tmpList->next) {
+            PangoLayoutRun *run = (PangoLayoutRun *)tmpList->data;
+            cursor += draw_glyphs(cr, cursor, run->item->analysis.font, run->glyphs, dirty_pattern, true) / PANGO_SCALE;
+        }
+#else
+        cairo_new_path(cr);
+        cairo_move_to(cr, _last_tx, _last_ty);
+        pango_cairo_layout_line_path(cr, line);
+#endif
+        cairo_stroke(cr);
+    }
+
+    cairo_font_face_destroy(font_face);
+
+    cairo_restore(cr);
+
+#ifndef USE_PANGO_CAIRO
+    if (dirty_pattern) {
+        FcPatternDel(fc_pattern, FC_VERTICAL_LAYOUT);
+        FcPatternAddBool(fc_pattern, FC_VERTICAL_LAYOUT, FcFalse);
+    }
+#endif
+    tf->Unref();
+
+return 0;
+}
+
+/* Helper functions */
+
+void
+PrintCairoPDF::print_bpath(cairo_t *cr, NArtBpath const *bp)
+{
+    cairo_new_path(cr);
+    bool closed = false;
+    while (bp->code != NR_END) {
+        switch (bp->code) {
+            case NR_MOVETO:
+                if (closed) {
+                    cairo_close_path(cr);
+                }
+                closed = true;
+                cairo_move_to(cr, bp->x3, bp->y3);
+                break;
+            case NR_MOVETO_OPEN:
+                if (closed) {
+                    cairo_close_path(cr);
+                }
+                closed = false;
+                cairo_move_to(cr, bp->x3, bp->y3);
+                break;
+            case NR_LINETO:
+                cairo_line_to(cr, bp->x3, bp->y3);
+                break;
+            case NR_CURVETO:
+                cairo_curve_to(cr, bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
+                break;
+            default:
+                break;
+        }
+        bp += 1;
+    }
+    if (closed) {
+        cairo_close_path(cr);
+    }
+}
+
+static void
+_concat_transform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0)
+{
+    cairo_matrix_t matrix;
+
+    cairo_matrix_init(&matrix, xx, yx, xy, yy, x0, y0); 
+    cairo_transform(cr, &matrix);
+}
+
+static cairo_status_t
+_write_callback(void *closure, const unsigned char *data, unsigned int length)
+{
+    size_t written;
+    FILE *file = (FILE*)closure;
+
+    written = fwrite (data, 1, length, file);
+
+    if (written == length)
+       return CAIRO_STATUS_SUCCESS;
+    else
+       return CAIRO_STATUS_WRITE_ERROR;
+}
+
+bool
+PrintCairoPDF::textToPath(Inkscape::Extension::Print * ext)
+{
+    return ext->get_param_bool("textToPath");
+}
+
+#include "clear-n_.h"
+
+void
+PrintCairoPDF::init(void)
+{
+    /* PDF out */
+    (void) Inkscape::Extension::build_from_mem(
+        "<inkscape-extension>\n"
+        "<name>" N_("PDF Print") "</name>\n"
+        "<id>" SP_MODULE_KEY_PRINT_CAIRO_PDF "</id>\n"
+        "<param name=\"bitmap\" type=\"boolean\">FALSE</param>\n"
+        "<param name=\"resolution\" type=\"string\">72</param>\n"
+        "<param name=\"destination\" type=\"string\">| lp</param>\n"
+        "<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"
+        "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
+        "<print/>\n"
+        "</inkscape-extension>", new PrintCairoPDF());
+}
+
+
+}  /* namespace Internal */
+}  /* namespace Extension */
+}  /* namespace Inkscape */
+
+/* End of GNU GPL code */
+
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
diff --git a/src/extension/internal/pdf-cairo.h b/src/extension/internal/pdf-cairo.h
new file mode 100644 (file)
index 0000000..63f537a
--- /dev/null
@@ -0,0 +1,107 @@
+#ifndef EXTENSION_INTERNAL_PDF_CAIRO_H_SEEN
+#define EXTENSION_INTERNAL_PDF_CAIRO_H_SEEN
+
+/** \file
+ * Declaration of PrintCairoPDF, the internal module used to do PDF printing with Cairo.
+ */
+/*
+ * Authors:
+ *        Lauris Kaplinski <lauris@kaplinski.com>
+ *        Ted Gould <ted@gould.cx>
+ *
+ * Lauris' original code is in the public domain.
+ * Ted's changes are licensed under the GNU GPL.
+ */
+
+#include <config.h>
+#include "extension/extension.h"
+#include "extension/implementation/implementation.h"
+#include <set>
+#include <string>
+
+#include "libnr/nr-path.h"
+#include "libnrtype/font-instance.h"
+
+#include "svg/stringstream.h"
+#include "sp-gradient.h"
+
+#include "cairo.h"
+#include <pango/pangocairo.h>
+
+namespace Inkscape {
+namespace Extension {
+namespace Internal {
+
+class PrintCairoPDF : public Inkscape::Extension::Implementation::Implementation {
+    float _width;
+    float _height;
+    FILE *_stream;
+    cairo_t *cr;
+    cairo_surface_t *pdf_surface;
+    PangoLayout *_layout;
+//    PangoContext *_context;
+    float *_alpha_stack;
+    int        _num_alphas, _alpha_ptr;
+    double _last_tx, _last_ty;
+    
+    unsigned short _dpi;
+    bool _bitmap;
+
+    void print_bpath(cairo_t *cr, NArtBpath const *bp);
+    cairo_pattern_t *create_pattern_for_paint(SPPaintServer const *const paintserver, NRRect const *pbox, float alpha);
+    
+    void print_fill_style(cairo_t *cr, SPStyle const *const style, NRRect const *pbox);
+    void print_stroke_style(cairo_t *cr, SPStyle const *style, NRRect const *pbox);
+
+#ifndef USE_PANGO_CAIRO
+    NR::Point draw_glyphs(cairo_t *cr, NR::Point p, PangoFont *font, PangoGlyphString *glyph_string,
+                          bool vertical, bool stroke);
+#endif
+
+public:
+    PrintCairoPDF(void);
+    virtual ~PrintCairoPDF(void);
+
+    /* Print functions */
+    virtual unsigned int setup(Inkscape::Extension::Print *module);
+    /*
+      virtual unsigned int set_preview(Inkscape::Extension::Print *module);
+    */
+
+    virtual unsigned int begin(Inkscape::Extension::Print *module, SPDocument *doc);
+    virtual unsigned int finish(Inkscape::Extension::Print *module);
+
+    /* Rendering methods */
+    virtual unsigned int bind(Inkscape::Extension::Print *module, NRMatrix const *transform, float opacity);
+    virtual unsigned int release(Inkscape::Extension::Print *module);
+    virtual unsigned int comment(Inkscape::Extension::Print *module, char const *comment);
+    virtual unsigned int fill(Inkscape::Extension::Print *module, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
+                              NRRect const *pbox, NRRect const *dbox, NRRect const *bbox);
+    virtual unsigned int stroke(Inkscape::Extension::Print *module, NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,
+                                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox);
+    virtual unsigned int image(Inkscape::Extension::Print *module, unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
+                               NRMatrix const *transform, SPStyle const *style);
+    virtual unsigned int text(Inkscape::Extension::Print *module, char const *text,
+                              NR::Point p, SPStyle const *style);
+
+    bool textToPath(Inkscape::Extension::Print *ext);
+    static void init(void);
+};
+
+}  /* namespace Internal */
+}  /* namespace Extension */
+}  /* namespace Inkscape */
+
+
+#endif /* !EXTENSION_INTERNAL_PDF_CAIRO_H_SEEN */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :