Code

PDF export patch by Ulf Erikson
authorbuliabyak <buliabyak@users.sourceforge.net>
Fri, 19 May 2006 02:36:23 +0000 (02:36 +0000)
committerbuliabyak <buliabyak@users.sourceforge.net>
Fri, 19 May 2006 02:36:23 +0000 (02:36 +0000)
src/extension/extension.h
src/extension/init.cpp
src/extension/internal/Makefile_insert
src/extension/internal/pdf-mini.h [new file with mode: 0644]
src/extension/internal/pdf-out.cpp [new file with mode: 0644]
src/extension/internal/pdf-out.h [new file with mode: 0644]
src/extension/internal/pdf.cpp [new file with mode: 0644]
src/extension/internal/pdf.h [new file with mode: 0644]
src/svg/stringstream.cpp
src/svg/stringstream.h

index 1e3d8ead0dc846a82b1a0133e1139dd1fa2e60e4..de3dda19b53689eeff28bff668eb5ac3800e2250 100644 (file)
@@ -43,6 +43,8 @@
 
 /** Defines the key for Postscript printing */
 #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"
 /** 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 66c6c89861854346fcb148ce83df0c6e38e1a631..a1833bcde6c40c9a913ffe65e7d619ca6f90cd7b 100644 (file)
@@ -25,6 +25,7 @@
 #include "db.h"
 #include "internal/svgz.h"
 #include "internal/ps.h"
+#include "internal/pdf.h"
 #ifdef WITH_GNOME_PRINT
 # include "internal/gnome.h"
 #endif
@@ -32,6 +33,7 @@
 # include "internal/win32.h"
 #endif
 #include "internal/ps-out.h"
+#include "internal/pdf-out.h"
 #include "internal/pov-out.h"
 #include "internal/odf.h"
 #include "internal/latex-pstricks-out.h"
@@ -103,6 +105,8 @@ init()
     Internal::PsOutput::init();
     Internal::EpsOutput::init();
     Internal::PrintPS::init();
+    Internal::PdfOutput::init();
+    Internal::PrintPDF::init();
 #ifdef WITH_GNOME_PRINT
     Internal::PrintGNOME::init();
 #endif
index f29b37227be60429f9f4399eceb0f19ea2ecfff1..8bf6638527196b09f428d0b910b29cf1921a1219 100644 (file)
@@ -27,6 +27,11 @@ extension_internal_libinternal_a_SOURCES =   \
        extension/internal/ps.cpp       \
        extension/internal/ps-out.h     \
        extension/internal/ps-out.cpp   \
+       extension/internal/pdf.h        \
+       extension/internal/pdf.cpp      \
+       extension/internal/pdf-mini.h   \
+       extension/internal/pdf-out.h    \
+       extension/internal/pdf-out.cpp  \
        extension/internal/eps-out.h    \
        extension/internal/eps-out.cpp  \
        extension/internal/gdkpixbuf-input.h    \
diff --git a/src/extension/internal/pdf-mini.h b/src/extension/internal/pdf-mini.h
new file mode 100644 (file)
index 0000000..ca57744
--- /dev/null
@@ -0,0 +1,343 @@
+/** \file\r
+ * PDF Mini library.\r
+ */\r
+/*\r
+ * Authors:\r
+ *   Ulf Erikson <ulferikson@users.sf.net>\r
+ *\r
+ * Minimalistic library to support generation of PDF files.\r
+ *\r
+ * Copyright (C) 2006 Authors\r
+ *\r
+ * Released under GNU GPL, read the file 'COPYING' for more information\r
+ */\r
+\r
+#ifndef PDF_MINI_H_SEEN\r
+#define PDF_MINI_H_SEEN\r
+\r
+\r
+class PdfFile;\r
+class PdfXref;\r
+class PdfObject;\r
+\r
+enum pdf_resource_type { pdf_none, pdf_extgstate, pdf_colorspace, pdf_pattern, pdf_shading, pdf_xobject, pdf_font, pdf_properties };\r
+\r
+char pdf_res_short[][5] = { "", "GS", "CS", "Pa", "Sh", "XObj", "F", "Prop" };\r
+char pdf_res_long[][20] = { "", "ExtGState", "ColorSpace", "Pattern",\r
+                      "Shading", "XObject", "Font", "Properties" };\r
+\r
+class PdfFile {\r
+  public:\r
+    PdfFile(FILE *_stream);\r
+    ~PdfFile();\r
+\r
+    long puts(const char *str);\r
+    long puts(std::string ss) { return puts(ss.c_str()); }\r
+    long puts(Inkscape::SVGOStringStream &os) { return puts(os.str().c_str()); }\r
+    long puts(PdfXref *table);\r
+    long puts(PdfObject *object);\r
+    \r
+    long tell();\r
+    \r
+    PdfObject *begin_document(double version = 1.2);\r
+    void end_document(PdfObject *doc_info);\r
+    PdfObject *begin_page(int x0, int y0, int x1, int y1);\r
+    PdfObject *begin_page(int w, int h) { return begin_page(0,0, w, h); }\r
+    void end_page(PdfObject *page);\r
+    PdfObject *begin_object();\r
+    void end_object(PdfObject *object);\r
+    PdfObject *begin_resource(enum pdf_resource_type);\r
+    void end_resource(PdfObject *resource);\r
+    \r
+  protected:\r
+    FILE *fp;\r
+    long length;\r
+    long stream_pos;\r
+\r
+    PdfXref *xref;\r
+\r
+    PdfXref *pages;\r
+    PdfXref *resources;\r
+\r
+    PdfObject *obj_root;\r
+    PdfObject *obj_catalog;\r
+    PdfObject *obj_info;\r
+    PdfObject *obj_resources;\r
+    PdfObject *obj_contents;\r
+    PdfObject *obj_length;\r
+};\r
+\r
+\r
+class PdfXref {\r
+  public:\r
+    PdfXref() { size = 10; table = (long*) malloc(size*sizeof(long)); entries = 0; }\r
+    ~PdfXref() { free(table); }\r
+\r
+    long get_size() { return entries; }\r
+    long get_entry(long id) { return table[id]; }\r
+    \r
+    long alloc_id()  {\r
+        entries++;\r
+        if (entries >= size) {\r
+            size = size*2;\r
+            table = (long *) realloc(table, size*sizeof(long));\r
+        }\r
+        return entries;\r
+    }\r
+    \r
+    void update_entry(long id, long offset) {\r
+        table[id] = offset;\r
+    }\r
+    \r
+    long add_entry(long offset = 0) {\r
+        long id = alloc_id();\r
+        update_entry(id, offset);\r
+        return id;\r
+    }\r
+\r
+  protected:\r
+    long *table;\r
+    long entries;\r
+    long size;\r
+};\r
+\r
+\r
+class PdfObject : public Inkscape::SVGOStringStream {\r
+  public:\r
+    PdfObject(long i) : next(NULL) { id = i; setf(std::ios::fixed); }\r
+    ~PdfObject() {}\r
+\r
+    long get_id() { return id; }\r
+    const char *get_name() { return name.str().c_str(); }\r
+    long get_length() { return str().length(); }\r
+    void set_type(enum pdf_resource_type type) { name << "/" << pdf_res_short[type] << id; }\r
+    PdfObject *next;\r
+\r
+  protected:\r
+    long id;\r
+    Inkscape::SVGOStringStream name;\r
+};\r
+\r
+PdfFile::PdfFile(FILE *_stream) {\r
+    fp = _stream;\r
+    xref = new PdfXref();\r
+}\r
+\r
+PdfFile::~PdfFile() {\r
+    delete xref;\r
+}\r
+\r
+long PdfFile::puts(const char *str) {\r
+    long res = fprintf(fp, "%s", str);\r
+    if (res >= 0) {\r
+        length += res;\r
+        res = length;\r
+    }\r
+    return res;\r
+}\r
+\r
+long PdfFile::puts(PdfObject *object) {\r
+    xref->update_entry( object->get_id(), tell() );\r
+    return puts(object->str());\r
+}\r
+\r
+long PdfFile::puts(PdfXref *table) {\r
+    Inkscape::SVGOStringStream os;\r
+    char buffer[32];\r
+    int i;\r
+\r
+    int size = table->get_size() + 1;\r
+\r
+    os << "xref\n";\r
+    os << "0" << " " << size << "\n";\r
+\r
+    sprintf(buffer, "%010d %05d %c \n", 0, 65535, 'f');\r
+    os << buffer;\r
+\r
+    for (i = 1; i < size; i++) {\r
+        sprintf(buffer, "%010d %05d %c \n", table->get_entry(i), 0, 'n');\r
+        os << buffer;\r
+    }\r
+\r
+    return puts(os);\r
+}\r
+\r
+long PdfFile::tell() {\r
+    return length;\r
+}\r
+\r
+PdfObject *PdfFile::begin_document(double version) {\r
+    Inkscape::SVGOStringStream os;\r
+\r
+    length = 0;\r
+    pages = new PdfXref();;\r
+\r
+    os << "%PDF-" << version << "\n";\r
+\r
+    obj_info = begin_object();\r
+    *obj_info << "<<\n";\r
+\r
+    obj_catalog = begin_object();\r
+\r
+    obj_root = begin_object();\r
+    *obj_root << "<<\n";\r
+    *obj_root << "  /Type /Catalog\n";\r
+    *obj_root << "  /Pages " << obj_catalog->get_id() << " 0 R\n";\r
+    *obj_root << ">>\n";\r
+    end_object(obj_root);\r
+\r
+    puts(os);\r
+    puts(obj_root);\r
+\r
+    return obj_info;\r
+}\r
+\r
+void PdfFile::end_document(PdfObject *doc_info) {\r
+    Inkscape::SVGOStringStream os;\r
+    long startxref;\r
+    int i;\r
+\r
+    *doc_info << ">>\n";\r
+    end_object(doc_info);\r
+    puts(doc_info);\r
+    \r
+    *obj_catalog << "<<\n"\r
+                 << "  /Type /Pages\n"\r
+                 << "  /Count " << pages->get_size() << "\n"\r
+                 << "  /Kids [\n";\r
+    for (i=1; i<=pages->get_size(); i++) {\r
+        *obj_catalog << "    " << pages->get_entry(i) << " 0 R\n";\r
+    }\r
+    *obj_catalog << "  ]\n"\r
+                 << ">>\n";\r
+    end_object(obj_catalog);\r
+    puts(obj_catalog);\r
+    \r
+    startxref = tell();\r
+\r
+    puts(xref);\r
+    \r
+    os << "trailer\n"\r
+       << "<<\n"\r
+       << "  /Size " << xref->get_size() << "\n"\r
+       << "  /Root " << obj_root->get_id() << " 0 R\n"\r
+       << "  /Info " << obj_info->get_id() << " 0 R\n"\r
+       << ">>\n";\r
+    os << "startxref\n"\r
+       << startxref << "\n";\r
+    os << "%%EOF\n";\r
+\r
+    puts(os);\r
+\r
+    delete pages;\r
+    delete obj_root;\r
+    delete obj_catalog;\r
+    delete obj_info;\r
+}\r
+\r
+PdfObject *PdfFile::begin_page(int x0, int y0, int x1, int y1) {\r
+    Inkscape::SVGOStringStream os;\r
+\r
+    resources = new PdfXref[11]();\r
+    PdfObject *obj_page = begin_object();\r
+   \r
+    obj_resources = begin_object();\r
+    obj_contents  = begin_object();\r
+    obj_length = begin_object();\r
+    \r
+    *obj_page << "<<\n"\r
+              << "  /Type /Page\n"\r
+              << "  /Parent " << obj_catalog->get_id() << " 0 R\n"\r
+              << "  /MediaBox [ " << x0 << " " << y0 << " " << x1 << " " << y1 << " ]\n"\r
+              << "  /Resources " << obj_resources->get_id() << " 0 R\n"\r
+              << "  /Contents " << obj_contents->get_id() << " 0 R\n"\r
+              << ">>\n";\r
+    end_object(obj_page);\r
+    puts(obj_page);\r
+\r
+    pages->add_entry( obj_page->get_id() );\r
+\r
+    *obj_contents << "<<\n"\r
+                  << "  /Length " << obj_length->get_id() << " 0 R\n"\r
+                  << ">>\n"\r
+                  << "stream\n";\r
+    stream_pos = obj_contents->get_length();\r
+\r
+    return obj_contents;\r
+}\r
+\r
+void PdfFile::end_page(PdfObject *page) {\r
+    long stream_length = page->get_length() - stream_pos;\r
+    \r
+    *page << "endstream\n";\r
+    end_object(page);\r
+    puts(page);\r
+    \r
+    *obj_length << stream_length << "\n";\r
+    end_object(obj_length);\r
+    puts(obj_length);\r
+\r
+    *obj_resources << "<<\n";\r
+    *obj_resources << "  /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]\n";\r
+    int res;\r
+    for (res=pdf_extgstate; res<=pdf_properties; res++) {\r
+        if (resources[res].get_size()) {\r
+            *obj_resources << "  /" << pdf_res_long[res] << "\n";\r
+            *obj_resources << "  <<\n";\r
+            int i;\r
+            for (i=1; i<=resources[res].get_size(); i++) {\r
+                int id = resources[res].get_entry(i);\r
+                *obj_resources << "    /"\r
+                               << pdf_res_short[res] << id\r
+                               << " " << id << " 0 R\n";\r
+            }\r
+            *obj_resources << "  >>\n";\r
+        }\r
+    }\r
+    *obj_resources << ">>\n";\r
+    end_object(obj_resources);\r
+    puts(obj_resources);\r
+\r
+    PdfObject *o = obj_resources->next;\r
+    while (o) {\r
+        PdfObject *t = o->next;\r
+        puts(o);\r
+        delete o;\r
+        o = t;\r
+    }\r
+    \r
+    delete obj_resources;\r
+    delete obj_length;\r
+    delete obj_contents;\r
+    \r
+    delete resources;\r
+}\r
+\r
+PdfObject *PdfFile::begin_object() {\r
+    long id = xref->add_entry();\r
+    PdfObject *obj = new PdfObject(id);\r
+    *obj << id << " 0 obj\n";\r
+    return obj;\r
+}\r
+\r
+void PdfFile::end_object(PdfObject *object) {\r
+    *object << "endobj\n";\r
+}\r
+\r
+PdfObject *PdfFile::begin_resource(enum pdf_resource_type res) {\r
+    PdfObject *obj = begin_object();\r
+    if (res != pdf_none) {\r
+        resources[res].add_entry(obj->get_id());\r
+        obj->set_type(res);\r
+    }\r
+    obj->next = obj_resources->next;\r
+    obj_resources->next = obj;\r
+    return obj;\r
+}\r
+\r
+void PdfFile::end_resource(PdfObject *resource) {\r
+    end_object(resource);\r
+}\r
+\r
+\r
+#endif /* !PDF_MINI_H_SEEN */\r
diff --git a/src/extension/internal/pdf-out.cpp b/src/extension/internal/pdf-out.cpp
new file mode 100644 (file)
index 0000000..370855f
--- /dev/null
@@ -0,0 +1,144 @@
+/*\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 "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
+PdfOutput::check (Inkscape::Extension::Extension * module)\r
+{\r
+       if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_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_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
+PdfOutput::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_PDF);\r
+    if (ext == NULL)\r
+        return;\r
+\r
+    bool old_textToPath  = ext->get_param_bool("textToPath");\r
+    bool new_val         = mod->get_param_bool("textToPath");\r
+    ext->set_param_bool("textToPath", new_val);\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
+    ext->set_param_bool("textToPath", old_textToPath);\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
+PdfOutput::init (void)\r
+{\r
+       Inkscape::Extension::build_from_mem(\r
+               "<inkscape-extension>\n"\r
+                       "<name>PDF Output</name>\n"\r
+                       "<id>org.inkscape.output.pdf</id>\n"\r
+                       "<param name=\"textToPath\" gui-text=\"Text to Path\" type=\"boolean\">true</param>\n"\r
+                       "<output>\n"\r
+                               "<extension>.pdf</extension>\n"\r
+                               "<mimetype>application/pdf</mimetype>\n"\r
+                               "<filetypename>PDF (*.pdf)</filetypename>\n"\r
+                               "<filetypetooltip>PDF File</filetypetooltip>\n"\r
+                       "</output>\n"\r
+               "</inkscape-extension>", new PdfOutput());\r
+\r
+       return;\r
+}\r
+\r
+} } }  /* namespace Inkscape, Extension, Implementation */\r
diff --git a/src/extension/internal/pdf-out.h b/src/extension/internal/pdf-out.h
new file mode 100644 (file)
index 0000000..00966d3
--- /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_PDF_OUT_H\r
+#define EXTENSION_INTERNAL_PDF_OUT_H\r
+\r
+#include "extension/implementation/implementation.h"\r
+\r
+namespace Inkscape {\r
+namespace Extension {\r
+namespace Internal {\r
+\r
+class PdfOutput : 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_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.cpp b/src/extension/internal/pdf.cpp
new file mode 100644 (file)
index 0000000..0a4969a
--- /dev/null
@@ -0,0 +1,1551 @@
+#define __SP_PDF_C__\r
+\r
+/** \file\r
+ * PDF printing.\r
+ */\r
+/*\r
+ * Authors:\r
+ *   Lauris Kaplinski <lauris@kaplinski.com>\r
+ *   bulia byak <buliabyak@users.sf.net>\r
+ *   Ulf Erikson <ulferikson@users.sf.net>\r
+ *\r
+ * Basic printing code, EXCEPT image and\r
+ * ascii85 filter is in public domain\r
+ *\r
+ * Image printing and Ascii85 filter:\r
+ *\r
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis\r
+ * Copyright (C) 1997-98 Peter Kirchgessner\r
+ * George White <aa056@chebucto.ns.ca>\r
+ * Austin Donnelly <austin@gimp.org>\r
+ *\r
+ * Licensed under GNU GPL\r
+ */\r
+\r
+/* Plain Print */\r
+\r
+#ifdef HAVE_CONFIG_H\r
+# include "config.h"\r
+#endif\r
+\r
+#include <signal.h>\r
+#include <errno.h>\r
+\r
+#include <libnr/n-art-bpath.h>\r
+#include <libnr/nr-point-matrix-ops.h>\r
+\r
+#include <gtk/gtkstock.h>\r
+#include <gtk/gtkvbox.h>\r
+#include <gtk/gtkframe.h>\r
+#include <gtk/gtkradiobutton.h>\r
+#include <gtk/gtkcombo.h>\r
+#include <gtk/gtklabel.h>\r
+#include <gtk/gtkentry.h>\r
+#include <gtk/gtktooltips.h>\r
+\r
+#include <glibmm/i18n.h>\r
+#include "display/nr-arena-item.h"\r
+#include "display/canvas-bpath.h"\r
+#include "sp-item.h"\r
+#include "style.h"\r
+#include "sp-linear-gradient.h"\r
+#include "sp-radial-gradient.h"\r
+\r
+#include "libnrtype/font-instance.h"\r
+#include "libnrtype/font-style-to-pos.h"\r
+\r
+#include <unit-constants.h>\r
+\r
+#include "pdf.h"\r
+#include "extension/system.h"\r
+#include "extension/print.h"\r
+\r
+#include "io/sys.h"\r
+\r
+#include "pdf-mini.h"\r
+\r
+namespace Inkscape {\r
+namespace Extension {\r
+namespace Internal {\r
+\r
+PrintPDF::PrintPDF() :\r
+    _stream(NULL),\r
+    _dpi(72),\r
+    _bitmap(false)\r
+{\r
+}\r
+\r
+PrintPDF::~PrintPDF(void)\r
+{\r
+    /* fixme: should really use pclose for popen'd streams */\r
+    if (_stream) fclose(_stream);\r
+\r
+    /* restore default signal handling for SIGPIPE */\r
+#if !defined(_WIN32) && !defined(__WIN32__)\r
+    (void) signal(SIGPIPE, SIG_DFL);\r
+#endif\r
+\r
+    return;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::setup(Inkscape::Extension::Print * mod)\r
+{\r
+    static gchar const *const pdr[] = {"72", "75", "100", "144", "150", "200", "300", "360", "600", "1200", "2400", NULL};\r
+\r
+#ifdef TED\r
+    Inkscape::XML::Node *repr = ((SPModule *) mod)->repr;\r
+#endif\r
+\r
+    unsigned int ret = FALSE;\r
+\r
+    /* Create dialog */\r
+    GtkTooltips *tt = gtk_tooltips_new();\r
+    g_object_ref((GObject *) tt);\r
+    gtk_object_sink((GtkObject *) tt);\r
+\r
+    GtkWidget *dlg = gtk_dialog_new_with_buttons(_("Print Destination"),\r
+//            SP_DT_WIDGET(SP_ACTIVE_DESKTOP)->window,\r
+            NULL,\r
+            (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT),\r
+            GTK_STOCK_CANCEL,\r
+            GTK_RESPONSE_CANCEL,\r
+            GTK_STOCK_PRINT,\r
+            GTK_RESPONSE_OK,\r
+            NULL);\r
+\r
+    gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);\r
+\r
+    GtkWidget *vbox = GTK_DIALOG(dlg)->vbox;\r
+    gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);\r
+    /* Print properties frame */\r
+    GtkWidget *f = gtk_frame_new(_("Print properties"));\r
+    gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);\r
+    GtkWidget *vb = gtk_vbox_new(FALSE, 4);\r
+    gtk_container_add(GTK_CONTAINER(f), vb);\r
+    gtk_container_set_border_width(GTK_CONTAINER(vb), 4);\r
+    /* Print type */\r
+    bool const p2bm = mod->get_param_bool("bitmap");\r
+    GtkWidget *rb = gtk_radio_button_new_with_label(NULL, _("Print using PDF operators"));\r
+    gtk_tooltips_set_tip((GtkTooltips *) tt, rb,\r
+                         _("Use PDF vector operators. The resulting image is usually smaller "\r
+                           "in file size and can be arbitrarily scaled, but "\r
+                           "patterns will be lost."), NULL);\r
+    if (!p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);\r
+    gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);\r
+    rb = gtk_radio_button_new_with_label(gtk_radio_button_get_group((GtkRadioButton *) rb), _("Print as bitmap"));\r
+    gtk_tooltips_set_tip((GtkTooltips *) tt, rb,\r
+                         _("Print everything as bitmap. The resulting image is usually larger "\r
+                           "in file size and cannot be arbitrarily scaled without quality loss, "\r
+                           "but all objects will be rendered exactly as displayed."), NULL);\r
+    if (p2bm) gtk_toggle_button_set_active((GtkToggleButton *) rb, TRUE);\r
+    gtk_box_pack_start(GTK_BOX(vb), rb, FALSE, FALSE, 0);\r
+    /* Resolution */\r
+    GtkWidget *hb = gtk_hbox_new(FALSE, 4);\r
+    gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);\r
+    GtkWidget *combo = gtk_combo_new();\r
+    gtk_combo_set_value_in_list(GTK_COMBO(combo), FALSE, FALSE);\r
+    gtk_combo_set_use_arrows(GTK_COMBO(combo), TRUE);\r
+    gtk_combo_set_use_arrows_always(GTK_COMBO(combo), TRUE);\r
+    gtk_widget_set_size_request(combo, 64, -1);\r
+    gtk_tooltips_set_tip((GtkTooltips *) tt, GTK_COMBO(combo)->entry,\r
+                         _("Preferred resolution (dots per inch) of bitmap"), NULL);\r
+    /* Setup strings */\r
+    GList *sl = NULL;\r
+    for (unsigned i = 0; pdr[i] != NULL; i++) {\r
+        sl = g_list_prepend(sl, (gpointer) pdr[i]);\r
+    }\r
+    sl = g_list_reverse(sl);\r
+    gtk_combo_set_popdown_strings(GTK_COMBO(combo), sl);\r
+    g_list_free(sl);\r
+    if (1) {\r
+        gchar const *val = mod->get_param_string("resolution");\r
+        gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), val);\r
+    }\r
+    gtk_box_pack_end(GTK_BOX(hb), combo, FALSE, FALSE, 0);\r
+    GtkWidget *l = gtk_label_new(_("Resolution:"));\r
+    gtk_box_pack_end(GTK_BOX(hb), l, FALSE, FALSE, 0);\r
+\r
+    /* Print destination frame */\r
+    f = gtk_frame_new(_("Print destination"));\r
+    gtk_box_pack_start(GTK_BOX(vbox), f, FALSE, FALSE, 4);\r
+    vb = gtk_vbox_new(FALSE, 4);\r
+    gtk_container_add(GTK_CONTAINER(f), vb);\r
+    gtk_container_set_border_width(GTK_CONTAINER(vb), 4);\r
+\r
+    l = gtk_label_new(_("Printer name (as given by lpstat -p);\n"\r
+                        "leave empty to use the system default printer.\n"\r
+                        "Use '> filename' to print to file.\n"\r
+                        "Use '| prog arg...' to pipe to a program."));\r
+    gtk_box_pack_start(GTK_BOX(vb), l, FALSE, FALSE, 0);\r
+\r
+    GtkWidget *e = gtk_entry_new();\r
+    if (1) {\r
+        gchar const *val = mod->get_param_string("destination");\r
+        gtk_entry_set_text(GTK_ENTRY(e), ( val != NULL\r
+                                           ? val\r
+                                           : "" ));\r
+    }\r
+    gtk_box_pack_start(GTK_BOX(vb), e, FALSE, FALSE, 0);\r
+\r
+    // pressing enter in the destination field is the same as clicking Print:\r
+    gtk_entry_set_activates_default(GTK_ENTRY(e), TRUE);\r
+\r
+    gtk_widget_show_all(vbox);\r
+\r
+    int const response = gtk_dialog_run(GTK_DIALOG(dlg));\r
+\r
+    g_object_unref((GObject *) tt);\r
+\r
+    if (response == GTK_RESPONSE_OK) {\r
+        gchar const *fn;\r
+        char const *sstr;\r
+\r
+        _bitmap = gtk_toggle_button_get_active((GtkToggleButton *) rb);\r
+        sstr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));\r
+        _dpi = (unsigned int) MAX((int)(atof(sstr)), 1);\r
+        /* Arrgh, have to do something */\r
+        fn = gtk_entry_get_text(GTK_ENTRY(e));\r
+        /* skip leading whitespace, bug #1068483 */\r
+        while (fn && *fn==' ') { fn++; }\r
+        /* g_print("Printing to %s\n", fn); */\r
+\r
+        mod->set_param_bool("bitmap", _bitmap);\r
+        mod->set_param_string("resolution", (gchar *)sstr);\r
+        mod->set_param_string("destination", (gchar *)fn);\r
+        ret = TRUE;\r
+    }\r
+\r
+    gtk_widget_destroy(dlg);\r
+\r
+    return ret;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::begin(Inkscape::Extension::Print *mod, SPDocument *doc)\r
+{\r
+    gboolean epsexport = false;\r
+\r
+    _latin1_encoded_fonts.clear();\r
+    _newlatin1font_proc_defined = false;\r
+\r
+    FILE *osf = NULL;\r
+    FILE *osp = NULL;\r
+\r
+    gsize bytesRead = 0;\r
+    gsize bytesWritten = 0;\r
+    GError *error = NULL;\r
+    gchar const *utf8_fn = mod->get_param_string("destination");\r
+    gchar *local_fn = g_filename_from_utf8( utf8_fn,\r
+                                            -1,  &bytesRead,  &bytesWritten, &error);\r
+    gchar const *fn = local_fn;\r
+\r
+    /* TODO: Replace the below fprintf's with something that does the right thing whether in\r
+     * gui or batch mode (e.g. --print=blah).  Consider throwing an exception: currently one of\r
+     * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the\r
+     * return code.\r
+     */\r
+    if (fn != NULL) {\r
+        if (*fn == '|') {\r
+            fn += 1;\r
+            while (isspace(*fn)) fn += 1;\r
+#ifndef WIN32\r
+            osp = popen(fn, "wb");\r
+#else\r
+            osp = _popen(fn, "wb");\r
+#endif\r
+            if (!osp) {\r
+                fprintf(stderr, "inkscape: popen(%s): %s\n",\r
+                        fn, strerror(errno));\r
+                return 0;\r
+            }\r
+            _stream = osp;\r
+        } else if (*fn == '>') {\r
+            fn += 1;\r
+            epsexport = g_str_has_suffix(fn,".eps");\r
+            while (isspace(*fn)) fn += 1;\r
+            Inkscape::IO::dump_fopen_call(fn, "K");\r
+            osf = Inkscape::IO::fopen_utf8name(fn, "wb+");\r
+            if (!osf) {\r
+                fprintf(stderr, "inkscape: fopen(%s): %s\n",\r
+                        fn, strerror(errno));\r
+                return 0;\r
+            }\r
+            _stream = osf;\r
+        } else {\r
+            /* put cwd stuff in here */\r
+            gchar *qn = ( *fn\r
+                          ? g_strdup_printf("lpr -P %s", fn)  /* FIXME: quote fn */\r
+                          : g_strdup("lpr") );\r
+#ifndef WIN32\r
+            osp = popen(qn, "wb");\r
+#else\r
+            osp = _popen(qn, "wb");\r
+#endif\r
+            if (!osp) {\r
+                fprintf(stderr, "inkscape: popen(%s): %s\n",\r
+                        qn, strerror(errno));\r
+                return 0;\r
+            }\r
+            g_free(qn);\r
+            _stream = osp;\r
+        }\r
+    }\r
+\r
+    g_free(local_fn);\r
+\r
+    if (_stream) {\r
+        /* fixme: this is kinda icky */\r
+#if !defined(_WIN32) && !defined(__WIN32__)\r
+        (void) signal(SIGPIPE, SIG_IGN);\r
+#endif\r
+    }\r
+\r
+    pdf_file = new PdfFile(_stream);\r
+    doc_info = pdf_file->begin_document(1.4);\r
+//    *doc_info << "  /Title(" << "?" << ")\n";\r
+//    *doc_info << "  /Author(" << "?" << ")\n";\r
+//    *doc_info << "  /Subject(" << "?" << ")\n";\r
+//    *doc_info << "  /Keywords(" << "?" << ")\n";\r
+//    *doc_info << "  /Creator(" << "?" << ")\n";\r
+    *doc_info << "  /Producer(" << PACKAGE_STRING << ")\n";\r
+//    *doc_info << "  /CreationDate(D:YYYYMMDDHHmmSSOHH'mm')\n";\r
+\r
+    /* flush this to test output stream as early as possible */\r
+    if (fflush(_stream)) {\r
+        /*g_print("caught error in sp_module_print_plain_begin\n");*/\r
+        if (ferror(_stream)) {\r
+            g_print("Error %d on output stream: %s\n", errno,\r
+                    g_strerror(errno));\r
+        }\r
+        g_print("Printing failed\n");\r
+        /* fixme: should use pclose() for pipes */\r
+        fclose(_stream);\r
+        _stream = NULL;\r
+        fflush(stdout);\r
+        return 0;\r
+    }\r
+\r
+    // width and height in pt\r
+    _width = sp_document_width(doc) * PT_PER_PX;\r
+    _height = sp_document_height(doc) * PT_PER_PX;\r
+\r
+    NRRect d;\r
+    bool   pageBoundingBox;\r
+    pageBoundingBox = mod->get_param_bool("pageBoundingBox");\r
+    // printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");\r
+    if (pageBoundingBox) {\r
+        d.x0 = d.y0 = 0;\r
+        d.x1 = ceil(_width);\r
+        d.y1 = ceil(_height);\r
+    } else {\r
+        SPItem* doc_item = SP_ITEM(sp_document_root(doc));\r
+        sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);\r
+        // convert from px to pt\r
+        d.x0 *= PT_PER_PX;\r
+        d.x1 *= PT_PER_PX;\r
+        d.y0 *= PT_PER_PX;\r
+        d.y1 *= PT_PER_PX;\r
+    }\r
+\r
+    page_stream = pdf_file->begin_page( (int) d.x0, (int) d.y0,\r
+                                        (int) d.x1, (int) d.y1 );\r
+\r
+    if (!_bitmap) {\r
+        *page_stream << PT_PER_PX << " 0 0 "\r
+                     << -PT_PER_PX << " 0 " << (int) ceil(_height)\r
+                     << " cm\n";\r
+        // from now on we can output px, but they will be treated as pt\r
+    }\r
+    \r
+    return 1;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::finish(Inkscape::Extension::Print *mod)\r
+{\r
+    if (!_stream) return 0;\r
+\r
+    if (_bitmap) {\r
+        double const dots_per_pt = _dpi / PT_PER_IN;\r
+\r
+        double const x0 = 0.0;\r
+        double const y0 = 0.0;\r
+        double const x1 = x0 + _width;\r
+        double const y1 = y0 + _height;\r
+\r
+        /* Bitmap width/height in bitmap dots. */\r
+        int const width = (int) (_width * dots_per_pt + 0.5);\r
+        int const height = (int) (_height * dots_per_pt + 0.5);\r
+\r
+        NRMatrix affine;\r
+        affine.c[0] = width / ((x1 - x0) * PX_PER_PT);\r
+        affine.c[1] = 0.0;\r
+        affine.c[2] = 0.0;\r
+        affine.c[3] = height / ((y1 - y0) * PX_PER_PT);\r
+        affine.c[4] = -affine.c[0] * x0;\r
+        affine.c[5] = -affine.c[3] * y0;\r
+\r
+        nr_arena_item_set_transform(mod->root, &affine);\r
+\r
+        guchar *const px = nr_new(guchar, 4 * width * 64);\r
+\r
+        for (int y = 0; y < height; y += 64) {\r
+            /* Set area of interest. */\r
+            NRRectL bbox;\r
+            bbox.x0 = 0;\r
+            bbox.y0 = y;\r
+            bbox.x1 = width;\r
+            bbox.y1 = MIN(height, y + 64);\r
+\r
+            /* Update to renderable state. */\r
+            NRGC gc(NULL);\r
+            nr_matrix_set_identity(&gc.transform);\r
+            nr_arena_item_invoke_update(mod->root, &bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);\r
+            /* Render */\r
+            /* This should take guchar* instead of unsigned char*) */\r
+            NRPixBlock pb;\r
+            nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8A8N,\r
+                                     bbox.x0, bbox.y0, bbox.x1, bbox.y1,\r
+                                     (guchar*)px, 4 * width, FALSE, FALSE);\r
+            memset(px, 0xff, 4 * width * 64);\r
+            nr_arena_item_invoke_render(mod->root, &bbox, &pb, 0);\r
+            /* Blitter goes here */\r
+            NRMatrix imgt;\r
+            imgt.c[0] = (bbox.x1 - bbox.x0) / dots_per_pt;\r
+            imgt.c[1] = 0.0;\r
+            imgt.c[2] = 0.0;\r
+            imgt.c[3] = (bbox.y1 - bbox.y0) / dots_per_pt;\r
+            imgt.c[4] = 0.0;\r
+            imgt.c[5] = _height - y / dots_per_pt - (bbox.y1 - bbox.y0) / dots_per_pt;\r
+\r
+            print_image(_stream, px, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0, 4 * width, &imgt);\r
+        }\r
+\r
+        nr_free(px);\r
+    }\r
+\r
+    pdf_file->end_page(page_stream);\r
+    pdf_file->end_document(doc_info);\r
+\r
+    delete pdf_file;\r
+\r
+    /* Flush stream to be sure. */\r
+    (void) fflush(_stream);\r
+\r
+    /* fixme: should really use pclose for popen'd streams */\r
+    fclose(_stream);\r
+    _stream = 0;\r
+    _latin1_encoded_fonts.clear();\r
+\r
+    return 0;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::bind(Inkscape::Extension::Print *mod, NRMatrix const *transform, float opacity)\r
+{\r
+    if (!_stream) return 0;  // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    *page_stream << "q\n";\r
+    *page_stream << transform->c[0] << " "\r
+                 << transform->c[1] << " "\r
+                 << transform->c[2] << " "\r
+                 << transform->c[3] << " "\r
+                 << transform->c[4] << " "\r
+                 << transform->c[5] << " cm\n";\r
+\r
+    return 1;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::release(Inkscape::Extension::Print *mod)\r
+{\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    *page_stream << "Q\n";\r
+\r
+    return 1;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::comment(Inkscape::Extension::Print *mod, char const *comment)\r
+{\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    *page_stream << "% " << comment;\r
\r
+    return 1;\r
+}\r
+\r
+void\r
+PrintPDF::print_fill_alpha(SVGOStringStream &/*os*/, SPStyle const *const style, NRRect const *pbox)\r
+{\r
+    g_return_if_fail( style->fill.type == SP_PAINT_TYPE_COLOR\r
+                      || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                           && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) );\r
+    \r
+    if (style->fill.type == SP_PAINT_TYPE_COLOR) {\r
+        float alpha = 1.0;\r
+        \r
+        if (style->opacity.set)\r
+            alpha *= SP_SCALE24_TO_FLOAT(style->opacity.value);\r
+        \r
+        if (style->fill_opacity.set)\r
+            alpha *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);\r
+\r
+        if (alpha != 1.0) {\r
+            PdfObject *pdf_alpha = pdf_file->begin_resource(pdf_extgstate);\r
+            *pdf_alpha << "<< /Type /ExtGState\n";\r
+            *pdf_alpha << "   /ca " << alpha << "\n";\r
+            *pdf_alpha << "   /AIS false\n";\r
+            *pdf_alpha << ">>\n";\r
+            \r
+            *page_stream << pdf_alpha->get_name()\r
+                         << " gs\n";\r
+            \r
+            pdf_file->end_resource(pdf_alpha);\r
+        }\r
+    } else {\r
+        g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                  && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );\r
+        \r
+        if (SP_IS_LINEARGRADIENT (SP_STYLE_FILL_SERVER (style))) {\r
+            \r
+            SPLinearGradient *lg=SP_LINEARGRADIENT(SP_STYLE_FILL_SERVER (style));\r
+            NR::Point p1 (lg->x1.computed, lg->y1.computed);\r
+            NR::Point p2 (lg->x2.computed, lg->y2.computed);\r
+            if (pbox && SP_GRADIENT(lg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {\r
+                // convert to userspace\r
+                NR::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);\r
+                p1 *= bbox2user;\r
+                p2 *= bbox2user;\r
+            }\r
+            \r
+            double alpha = 1.0;\r
+            for (gint i = 0; unsigned(i) < lg->vector.stops.size(); i++) {\r
+                alpha *= lg->vector.stops[i].opacity;\r
+            }\r
+            \r
+            if (alpha != 1.0 || style->opacity.set) {\r
+                PdfObject *pdf_gstate = pdf_file->begin_resource(pdf_extgstate);\r
+                *pdf_gstate << "<< /Type /ExtGState\n";\r
+                \r
+                if (style->opacity.set) {\r
+                    *pdf_gstate << "   /ca " << SP_SCALE24_TO_FLOAT(style->opacity.value) << "\n";\r
+                    *pdf_gstate << "   /AIS false\n";\r
+                }\r
+                \r
+                if (alpha != 1.0) {\r
+                    PdfObject *pdf_alpha = pdf_file->begin_resource(pdf_shading);\r
+                    PdfObject *pdf_smask = pdf_file->begin_resource(pdf_none);\r
+                    PdfObject *pdf_xobj = pdf_file->begin_resource(pdf_none);\r
+\r
+\r
+                    *pdf_gstate << "   /SMask " << pdf_smask->get_id() << " 0 R\n";\r
+\r
+            \r
+                    *pdf_alpha << "<<\n/ShadingType 2\n/ColorSpace /DeviceGray\n";\r
+                    *pdf_alpha << "/Coords [" << p1[NR::X] << " " << p1[NR::Y] << " " << p2[NR::X] << " " << p2[NR::Y] <<"]\n";\r
+                    *pdf_alpha << "/Extend [true true]\n";\r
+                    *pdf_alpha << "/Domain [0 1]\n";\r
+                    *pdf_alpha << "/Function <<\n/FunctionType 3\n/Functions\n[\n";\r
+\r
+                    sp_gradient_ensure_vector(SP_GRADIENT(lg)); // when exporting from commandline, vector is not built\r
+                    for (gint i = 0; unsigned(i) < lg->vector.stops.size() - 1; i++) {\r
+                        *pdf_alpha << "<<\n/FunctionType 2\n/Domain [0 1]\n";\r
+                        *pdf_alpha << "/C0 [" << lg->vector.stops[i].opacity << "]\n";\r
+                        *pdf_alpha << "/C1 [" << lg->vector.stops[i+1].opacity << "]\n";\r
+                        *pdf_alpha << "/N 1\n>>\n";\r
+                    }\r
+                    *pdf_alpha << "]\n/Domain [0 1]\n";\r
+                    *pdf_alpha << "/Bounds [ ";\r
+                    for (gint i=0;unsigned(i)<lg->vector.stops.size()-2;i++) {\r
+                        *pdf_alpha << lg->vector.stops[i+1].offset <<" ";\r
+                    }\r
+                    *pdf_alpha << "]\n";\r
+                    *pdf_alpha << "/Encode [ ";\r
+                    for (gint i=0;unsigned(i)<lg->vector.stops.size()-1;i++) {\r
+                        *pdf_alpha << "0 1 ";\r
+                    }\r
+                    *pdf_alpha << "]\n";\r
+                    *pdf_alpha << ">>\n>>\n";\r
+\r
+                    \r
+                    *pdf_smask << "<< /Type /Mask\n";\r
+                    *pdf_smask << "   /S /Alpha\n";\r
+                    *pdf_smask << "   /BC [0.0]\n";\r
+                    *pdf_smask << "   /G " << pdf_xobj->get_id() << " 0 R\n";\r
+                    *pdf_smask << ">>\n";\r
+\r
+                    \r
+                    *pdf_xobj << "<< /Type /XObject\n";\r
+                    *pdf_xobj << "   /Subtype /Form\n";\r
+                    *pdf_xobj << "   /FormType 1\n";\r
+                    *pdf_xobj << "   /BBox ["\r
+                              << pbox->x0 << " "\r
+                              << pbox->y0 << " "\r
+                              << pbox->x1 << " "\r
+                              << pbox->y1 << "]\n";\r
+                    *pdf_xobj << "   /Group\n";\r
+                    *pdf_xobj << "   << /Type /Group\n";\r
+                    *pdf_xobj << "      /S /Transparency\n";\r
+                    *pdf_xobj << "      /CS /DeviceGray \n";\r
+                    *pdf_xobj << "   >>\n";\r
+                    \r
+                    Inkscape::SVGOStringStream os;\r
+                    os.setf(std::ios::fixed);\r
+                    \r
+                    os << "q\n"\r
+                       << pbox->x0 << " " << pbox->y0 << " m\n"\r
+                       << pbox->x1 << " " << pbox->y0 << " l\n"\r
+                       << pbox->x1 << " " << pbox->y1 << " l\n"\r
+                       << pbox->x0 << " " << pbox->y1 << " l\n"\r
+                       << pbox->x0 << " " << pbox->y0 << " l\n"\r
+                       << "h\n"\r
+                       << "W* n\n";\r
+                    \r
+                    SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));\r
+                    if (g->gradientTransform_set) {\r
+                        os << g->gradientTransform[0] << " "\r
+                           << g->gradientTransform[1] << " "\r
+                           << g->gradientTransform[2] << " "\r
+                           << g->gradientTransform[3] << " "\r
+                           << g->gradientTransform[4] << " "\r
+                           << g->gradientTransform[5] << " cm\n"; \r
+                    }\r
+                    \r
+                    os << pdf_alpha->get_name() << " sh\n";\r
+                    os << "Q\n";\r
+                    *pdf_xobj << "   /Length " << strlen(os.str().c_str()) << "\n";\r
+                    *pdf_xobj << ">>\n";\r
+                    *pdf_xobj << "stream\n";\r
+                    *pdf_xobj << os.str().c_str();\r
+                    *pdf_xobj << "endstream\n";\r
+\r
+\r
+                    pdf_file->end_resource(pdf_alpha);\r
+                    pdf_file->end_resource(pdf_smask);\r
+                    pdf_file->end_resource(pdf_xobj);\r
+                }\r
+                \r
+                *pdf_gstate << ">>\n";\r
+\r
+                *page_stream << pdf_gstate->get_name() << " gs\n";\r
+                    \r
+                pdf_file->end_resource(pdf_gstate);\r
+            }\r
+        } else if (SP_IS_RADIALGRADIENT (SP_STYLE_FILL_SERVER (style))) {\r
+\r
+            SPRadialGradient *rg=SP_RADIALGRADIENT(SP_STYLE_FILL_SERVER (style));\r
+            NR::Point c (rg->cx.computed, rg->cy.computed);\r
+            NR::Point f (rg->fx.computed, rg->fy.computed);\r
+            double r = rg->r.computed;\r
+            if (pbox && SP_GRADIENT(rg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {\r
+                // convert to userspace\r
+                NR::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);\r
+                c *= bbox2user;\r
+                f *= bbox2user;\r
+                r *= bbox2user.expansion();\r
+            }\r
+            \r
+            double alpha = 1.0;\r
+            for (gint i = 0; unsigned(i) < rg->vector.stops.size(); i++) {\r
+                alpha *= rg->vector.stops[i].opacity;\r
+            }\r
+\r
+            if (alpha != 1.0 || style->opacity.set) {\r
+                PdfObject *pdf_gstate = pdf_file->begin_resource(pdf_extgstate);\r
+                *pdf_gstate << "<< /Type /ExtGState\n";\r
+                \r
+                if (style->opacity.set) {\r
+                    *pdf_gstate << "   /ca " << SP_SCALE24_TO_FLOAT(style->opacity.value) << "\n";\r
+                    *pdf_gstate << "   /AIS false\n";\r
+                }\r
+                \r
+                if (alpha != 1.0) {\r
+                    PdfObject *pdf_alpha = pdf_file->begin_resource(pdf_shading);\r
+                    PdfObject *pdf_smask = pdf_file->begin_resource(pdf_none);\r
+                    PdfObject *pdf_xobj = pdf_file->begin_resource(pdf_none);\r
+\r
+\r
+                    *pdf_gstate << "   /SMask " << pdf_smask->get_id() << " 0 R\n";\r
+\r
+\r
+                    *pdf_alpha << "<<\n/ShadingType 3\n/ColorSpace /DeviceGray\n";\r
+                    *pdf_alpha << "/Coords ["<< f[NR::X] <<" "<< f[NR::Y] <<" 0 "<< c[NR::X] <<" "<< c[NR::Y] <<" "<< r <<"]\n";\r
+                    *pdf_alpha << "/Extend [true true]\n";\r
+                    *pdf_alpha << "/Domain [0 1]\n";\r
+                    *pdf_alpha << "/Function <<\n/FunctionType 3\n/Functions\n[\n";\r
+\r
+                    sp_gradient_ensure_vector(SP_GRADIENT(rg)); // when exporting from commandline, vector is not built\r
+                    for (gint i = 0; unsigned(i) < rg->vector.stops.size() - 1; i++) {\r
+                        *pdf_alpha << "<<\n/FunctionType 2\n/Domain [0 1]\n";\r
+                        *pdf_alpha << "/C0 [" << rg->vector.stops[i].opacity << "]\n";\r
+                        *pdf_alpha << "/C1 [" << rg->vector.stops[i+1].opacity << "]\n";\r
+                        *pdf_alpha << "/N 1\n>>\n";\r
+                    }\r
+                    *pdf_alpha << "]\n/Domain [0 1]\n";\r
+                    *pdf_alpha << "/Bounds [ ";\r
+\r
+                    for (gint i=0;unsigned(i)<rg->vector.stops.size()-2;i++) {\r
+                        *pdf_alpha << rg->vector.stops[i+1].offset <<" ";\r
+                    }\r
+                    *pdf_alpha << "]\n";\r
+                    *pdf_alpha << "/Encode [ ";\r
+                    for (gint i=0;unsigned(i)<rg->vector.stops.size()-1;i++) {\r
+                        *pdf_alpha << "0 1 ";\r
+                    }\r
+                    *pdf_alpha << "]\n";\r
+                    *pdf_alpha << ">>\n>>\n";\r
+                    \r
+                    \r
+                    *pdf_smask << "<< /Type /Mask\n";\r
+                    *pdf_smask << "   /S /Alpha\n";\r
+                    *pdf_smask << "   /BC [0.0]\n";\r
+                    *pdf_smask << "   /G " << pdf_xobj->get_id() << " 0 R\n";\r
+                    *pdf_smask << ">>\n";\r
+\r
+                    \r
+                    *pdf_xobj << "<< /Type /XObject\n";\r
+                    *pdf_xobj << "   /Subtype /Form\n";\r
+                    *pdf_xobj << "   /FormType 1\n";\r
+                    *pdf_xobj << "   /BBox ["\r
+                              << pbox->x0 << " "\r
+                              << pbox->y0 << " "\r
+                              << pbox->x1 << " "\r
+                              << pbox->y1 << "]\n";\r
+                    *pdf_xobj << "   /Group\n";\r
+                    *pdf_xobj << "   << /Type /Group\n";\r
+                    *pdf_xobj << "      /S /Transparency\n";\r
+                    *pdf_xobj << "      /CS /DeviceGray \n";\r
+                    *pdf_xobj << "   >>\n";\r
+                    \r
+                    Inkscape::SVGOStringStream os;\r
+                    os.setf(std::ios::fixed);\r
+                    \r
+                    os << "q\n"\r
+                       << pbox->x0 << " " << pbox->y0 << " m\n"\r
+                       << pbox->x1 << " " << pbox->y0 << " l\n"\r
+                       << pbox->x1 << " " << pbox->y1 << " l\n"\r
+                       << pbox->x0 << " " << pbox->y1 << " l\n"\r
+                       << pbox->x0 << " " << pbox->y0 << " l\n"\r
+                       << "h\n"\r
+                       << "W* n\n";\r
+                    \r
+                    SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));\r
+                    if (g->gradientTransform_set) {\r
+                        os << g->gradientTransform[0] << " "\r
+                           << g->gradientTransform[1] << " "\r
+                           << g->gradientTransform[2] << " "\r
+                           << g->gradientTransform[3] << " "\r
+                           << g->gradientTransform[4] << " "\r
+                           << g->gradientTransform[5] << " cm\n"; \r
+                    }\r
+                    \r
+                    os << pdf_alpha->get_name() << " sh\n";\r
+                    os << "Q\n";\r
+                    *pdf_xobj << "   /Length " << strlen(os.str().c_str()) << "\n";\r
+                    *pdf_xobj << ">>\n";\r
+                    *pdf_xobj << "stream\n";\r
+                    *pdf_xobj << os.str().c_str();\r
+                    *pdf_xobj << "endstream\n";\r
+\r
+\r
+                    pdf_file->end_resource(pdf_alpha);\r
+                    pdf_file->end_resource(pdf_smask);\r
+                    pdf_file->end_resource(pdf_xobj);\r
+                }\r
+                \r
+                *pdf_gstate << ">>\n";\r
+                \r
+                *page_stream << pdf_gstate->get_name() << " gs\n";\r
+                \r
+                pdf_file->end_resource(pdf_gstate);\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
+void\r
+PrintPDF::print_fill_style(SVGOStringStream &os, SPStyle const *const style, NRRect const *pbox)\r
+{\r
+    g_return_if_fail( style->fill.type == SP_PAINT_TYPE_COLOR\r
+                      || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                           && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) );\r
+    \r
+    if (style->fill.type == SP_PAINT_TYPE_COLOR) {\r
+        float rgb[3];\r
+        sp_color_get_rgb_floatv(&style->fill.value.color, rgb);\r
+\r
+        *page_stream << rgb[0] << " " << rgb[1] << " " << rgb[2] << " rg\n";\r
+    } else {\r
+        g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                  && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );\r
+\r
+        if (SP_IS_LINEARGRADIENT (SP_STYLE_FILL_SERVER (style))) {\r
+\r
+            SPLinearGradient *lg=SP_LINEARGRADIENT(SP_STYLE_FILL_SERVER (style));\r
+            NR::Point p1 (lg->x1.computed, lg->y1.computed);\r
+            NR::Point p2 (lg->x2.computed, lg->y2.computed);\r
+            if (pbox && SP_GRADIENT(lg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {\r
+                // convert to userspace\r
+                NR::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);\r
+                p1 *= bbox2user;\r
+                p2 *= bbox2user;\r
+            }\r
+\r
+            PdfObject *pdf_shade = pdf_file->begin_resource(pdf_shading);\r
+\r
+            *pdf_shade << "<<\n/ShadingType 2\n/ColorSpace /DeviceRGB\n";\r
+            *pdf_shade << "/Coords [" << p1[NR::X] << " " << p1[NR::Y] << " " << p2[NR::X] << " " << p2[NR::Y] <<"]\n";\r
+            *pdf_shade << "/Extend [true true]\n";\r
+            *pdf_shade << "/Domain [0 1]\n";\r
+            *pdf_shade << "/Function <<\n/FunctionType 3\n/Functions\n[\n";\r
+\r
+            sp_gradient_ensure_vector(SP_GRADIENT(lg)); // when exporting from commandline, vector is not built\r
+            for (gint i = 0; unsigned(i) < lg->vector.stops.size() - 1; i++) {\r
+                float rgb[3];\r
+                sp_color_get_rgb_floatv(&lg->vector.stops[i].color, rgb);\r
+                *pdf_shade << "<<\n/FunctionType 2\n/Domain [0 1]\n";\r
+                *pdf_shade << "/C0 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";\r
+                sp_color_get_rgb_floatv(&lg->vector.stops[i+1].color, rgb);\r
+                *pdf_shade << "/C1 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";\r
+                *pdf_shade << "/N 1\n>>\n";\r
+            }\r
+            *pdf_shade << "]\n/Domain [0 1]\n";\r
+            *pdf_shade << "/Bounds [ ";\r
+            for (gint i=0;unsigned(i)<lg->vector.stops.size()-2;i++) {\r
+                *pdf_shade << lg->vector.stops[i+1].offset <<" ";\r
+            }\r
+            *pdf_shade << "]\n";\r
+            *pdf_shade << "/Encode [ ";\r
+            for (gint i=0;unsigned(i)<lg->vector.stops.size()-1;i++) {\r
+                *pdf_shade << "0 1 ";\r
+            }\r
+            *pdf_shade << "]\n";\r
+            *pdf_shade << ">>\n>>\n";\r
+            *page_stream << pdf_shade->get_name() << " ";\r
+\r
+            pdf_file->end_resource(pdf_shade);\r
+        } else if (SP_IS_RADIALGRADIENT (SP_STYLE_FILL_SERVER (style))) {\r
+\r
+            SPRadialGradient *rg=SP_RADIALGRADIENT(SP_STYLE_FILL_SERVER (style));\r
+            NR::Point c (rg->cx.computed, rg->cy.computed);\r
+            NR::Point f (rg->fx.computed, rg->fy.computed);\r
+            double r = rg->r.computed;\r
+            if (pbox && SP_GRADIENT(rg)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {\r
+                // convert to userspace\r
+                NR::Matrix bbox2user(pbox->x1 - pbox->x0, 0, 0, pbox->y1 - pbox->y0, pbox->x0, pbox->y0);\r
+                c *= bbox2user;\r
+                f *= bbox2user;\r
+                r *= bbox2user.expansion();\r
+            }\r
+\r
+            PdfObject *pdf_shade = pdf_file->begin_resource(pdf_shading);\r
+\r
+            *pdf_shade << "<<\n/ShadingType 3\n/ColorSpace /DeviceRGB\n";\r
+            *pdf_shade << "/Coords ["<< f[NR::X] <<" "<< f[NR::Y] <<" 0 "<< c[NR::X] <<" "<< c[NR::Y] <<" "<< r <<"]\n";\r
+            *pdf_shade << "/Extend [true true]\n";\r
+            *pdf_shade << "/Domain [0 1]\n";\r
+            *pdf_shade << "/Function <<\n/FunctionType 3\n/Functions\n[\n";\r
+\r
+            sp_gradient_ensure_vector(SP_GRADIENT(rg)); // when exporting from commandline, vector is not built\r
+            for (gint i = 0; unsigned(i) < rg->vector.stops.size() - 1; i++) {\r
+                float rgb[3];\r
+                sp_color_get_rgb_floatv(&rg->vector.stops[i].color, rgb);\r
+                *pdf_shade << "<<\n/FunctionType 2\n/Domain [0 1]\n";\r
+                *pdf_shade << "/C0 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";\r
+                sp_color_get_rgb_floatv(&rg->vector.stops[i+1].color, rgb);\r
+                *pdf_shade << "/C1 [" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "]\n";\r
+                *pdf_shade << "/N 1\n>>\n";\r
+            }\r
+            *pdf_shade << "]\n/Domain [0 1]\n";\r
+            *pdf_shade << "/Bounds [ ";\r
+\r
+            for (gint i=0;unsigned(i)<rg->vector.stops.size()-2;i++) {\r
+                *pdf_shade << rg->vector.stops[i+1].offset <<" ";\r
+            }\r
+            *pdf_shade << "]\n";\r
+            *pdf_shade << "/Encode [ ";\r
+            for (gint i=0;unsigned(i)<rg->vector.stops.size()-1;i++) {\r
+                *pdf_shade << "0 1 ";\r
+            }\r
+            *pdf_shade << "]\n";\r
+            *pdf_shade << ">>\n>>\n";\r
+\r
+            *page_stream << pdf_shade->get_name() << " ";\r
+\r
+            pdf_file->end_resource(pdf_shade);\r
+        }\r
+    }\r
+}\r
+\r
+void\r
+PrintPDF::print_stroke_style(SVGOStringStream &os, SPStyle const *style)\r
+{\r
+    float rgb[3];\r
+    \r
+    sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);\r
+    *page_stream << rgb[0] << " " << rgb[1] << " " << rgb[2] << " RG\n";\r
+        \r
+    float alpha = 1.0;\r
+\r
+    if (style->opacity.set)\r
+        alpha *= SP_SCALE24_TO_FLOAT(style->opacity.value);\r
+        \r
+    if (style->stroke_opacity.set)\r
+        alpha *= SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);\r
+\r
+    if (alpha != 1.0) {\r
+        PdfObject *pdf_alpha = pdf_file->begin_resource(pdf_extgstate);\r
+        *pdf_alpha << "<< /Type /ExtGState\n";\r
+        *pdf_alpha << "   /CA " << alpha << "\n";\r
+        *pdf_alpha << "   /AIS false\n";\r
+        *pdf_alpha << ">>\n";\r
+        \r
+        *page_stream << pdf_alpha->get_name() << " gs\n";\r
+        \r
+        pdf_file->end_resource(pdf_alpha);\r
+    }\r
+\r
+    // There are rare cases in which for a solid line stroke_dasharray_set is true. To avoid\r
+    // invalid PS-lines such as "[0.0000000 0.0000000] 0.0000000 setdash", which should be "[] 0 setdash",\r
+    // we first check if all components of stroke_dash.dash are 0.\r
+    bool LineSolid = true;\r
+    if (style->stroke_dasharray_set &&\r
+        style->stroke_dash.n_dash   &&\r
+        style->stroke_dash.dash       )\r
+    {\r
+        int i = 0;\r
+        while (LineSolid && (i < style->stroke_dash.n_dash)) {\r
+                if (style->stroke_dash.dash[i] > 0.00000001)\r
+                    LineSolid = false;\r
+                i++;\r
+        }\r
+        if (!LineSolid) {\r
+            *page_stream << "[";\r
+            for (i = 0; i < style->stroke_dash.n_dash; i++) {\r
+                if (i > 0) {\r
+                    *page_stream << " ";\r
+                }\r
+                *page_stream << style->stroke_dash.dash[i];\r
+            }\r
+            *page_stream << "] " << style->stroke_dash.offset << " d\n";\r
+        } else {\r
+            *page_stream << "[] 0 d\n";\r
+        }\r
+    } else {\r
+        *page_stream << "[] 0 d\n";\r
+    }\r
+\r
+    *page_stream << style->stroke_width.computed << " w\n";\r
+    *page_stream << style->stroke_linejoin.computed << " j\n";\r
+    *page_stream << style->stroke_linecap.computed << " J\n";\r
+    if (style->stroke_miterlimit.set) {\r
+        *page_stream << style->stroke_miterlimit.value << " M\n";\r
+    }\r
+}\r
+\r
+\r
+unsigned int\r
+PrintPDF::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *const style,\r
+              NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)\r
+{\r
+    Inkscape::SVGOStringStream os;\r
+    os.setf(std::ios::fixed);\r
+\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    if ( style->fill.type == SP_PAINT_TYPE_COLOR ) {\r
+        *page_stream << "q\n";\r
+        print_fill_style(os, style, pbox);\r
+        print_fill_alpha(os, style, pbox);\r
+        print_bpath(os, bpath->path);\r
+        if (style->fill_rule.value == SP_WIND_RULE_EVENODD) {\r
+            *page_stream << "f*\n";\r
+        } else {\r
+            *page_stream << "f\n";\r
+        }\r
+        *page_stream << "Q\n";\r
+    }\r
+    else if ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+              && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) )\r
+    {\r
+        *page_stream << "q\n";\r
+        print_bpath(os, bpath->path);\r
+\r
+        if (style->fill_rule.value == SP_WIND_RULE_EVENODD) {\r
+            g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                      && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );\r
+            SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));\r
+            *page_stream << "W* n\n";\r
+            print_fill_alpha(os, style, pbox);\r
+            if (g->gradientTransform_set) {\r
+                *page_stream << "q\n";\r
+                *page_stream << g->gradientTransform[0] << " "\r
+                             << g->gradientTransform[1] << " "\r
+                             << g->gradientTransform[2] << " "\r
+                             << g->gradientTransform[3] << " "\r
+                             << g->gradientTransform[4] << " "\r
+                             << g->gradientTransform[5] << " cm\n";\r
+            }\r
+            print_fill_style(os, style, pbox);\r
+            *page_stream << "sh\n";\r
+            if (g->gradientTransform_set) {\r
+                *page_stream << "Q\n";\r
+            }\r
+        } else {\r
+            g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+                      && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );\r
+            SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));\r
+            *page_stream << "W n\n";\r
+            print_fill_alpha(os, style, pbox);\r
+            if (g->gradientTransform_set) {\r
+                *page_stream << "q\n";\r
+                *page_stream << g->gradientTransform[0] << " "\r
+                             << g->gradientTransform[1] << " "\r
+                             << g->gradientTransform[2] << " "\r
+                             << g->gradientTransform[3] << " "\r
+                             << g->gradientTransform[4] << " "\r
+                             << g->gradientTransform[5] << " cm\n"; \r
+            }\r
+            print_fill_style(os, style, pbox);\r
+            *page_stream << "sh\n";\r
+            if (g->gradientTransform_set) {\r
+                *page_stream << "Q\n";\r
+            }\r
+        }\r
+\r
+        *page_stream << "Q\n";\r
+    }        \r
+\r
+    return 0;\r
+}\r
+\r
+\r
+unsigned int\r
+PrintPDF::stroke(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,\r
+                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)\r
+{\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    if (style->stroke.type == SP_PAINT_TYPE_COLOR) {\r
+        Inkscape::SVGOStringStream os;\r
+        os.setf(std::ios::fixed);\r
+\r
+        *page_stream << "q\n";\r
+\r
+        print_stroke_style(os, style);\r
+        print_bpath(os, bpath->path);\r
+        *page_stream << "S\n";\r
+\r
+        *page_stream << "Q\n";\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+unsigned int\r
+PrintPDF::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsigned int h, unsigned int rs,\r
+               NRMatrix const *transform, SPStyle const *style)\r
+{\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    return 0;\r
+    \r
+    return print_image(_stream, px, w, h, rs, transform);\r
+#if 0\r
+    fprintf(_stream, "gsave\n");\r
+    fprintf(_stream, "/rowdata %d string def\n", 3 * w);\r
+    fprintf(_stream, "[%g %g %g %g %g %g] concat\n",\r
+            transform->c[0],\r
+            transform->c[1],\r
+            transform->c[2],\r
+            transform->c[3],\r
+            transform->c[4],\r
+            transform->c[5]);\r
+    fprintf(_stream, "%d %d 8 [%d 0 0 -%d 0 %d]\n", w, h, w, h, h);\r
+    fprintf(_stream, "{currentfile rowdata readhexstring pop}\n");\r
+    fprintf(_stream, "false 3 colorimage\n");\r
+\r
+    for (unsigned int r = 0; r < h; r++) {\r
+        guchar *s;\r
+        unsigned int c0, c1, c;\r
+        s = px + r * rs;\r
+        for (c0 = 0; c0 < w; c0 += 24) {\r
+            c1 = MIN(w, c0 + 24);\r
+            for (c = c0; c < c1; c++) {\r
+                static char const xtab[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};\r
+                fputc(xtab[s[0] >> 4], _stream);\r
+                fputc(xtab[s[0] & 0xf], _stream);\r
+                fputc(xtab[s[1] >> 4], _stream);\r
+                fputc(xtab[s[1] & 0xf], _stream);\r
+                fputc(xtab[s[2] >> 4], _stream);\r
+                fputc(xtab[s[2] & 0xf], _stream);\r
+                s += 4;\r
+            }\r
+            fputs("\n", _stream);\r
+        }\r
+    }\r
+\r
+    fprintf(_stream, "grestore\n");\r
+\r
+    return 0;\r
+#endif\r
+}\r
+\r
+char const *\r
+PrintPDF::PSFontName(SPStyle const *style)\r
+{\r
+    font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));\r
+\r
+    char const *n;\r
+    char name_buf[256];\r
+\r
+    if (tf) {\r
+        tf->PSName(name_buf, sizeof(name_buf));\r
+        n = name_buf;\r
+        tf->Unref();\r
+    } else {\r
+        // this system does not have this font, so just use the name from SVG in the hope that PS interpreter will make sense of it\r
+        bool i = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);\r
+        bool o = (style->font_style.value == SP_CSS_FONT_STYLE_OBLIQUE);\r
+        bool b = (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) ||\r
+            (style->font_weight.value >= SP_CSS_FONT_WEIGHT_500 && style->font_weight.value <= SP_CSS_FONT_WEIGHT_900);\r
+\r
+        n = g_strdup_printf("%s%s%s%s",\r
+                            g_strdelimit(style->text->font_family.value, " ", '-'),\r
+                            (b || i || o) ? "-" : "",\r
+                            (b) ? "Bold" : "",\r
+                            (i) ? "Italic" : ((o) ? "Oblique" : "") );\r
+    }\r
+\r
+    return g_strdup(n);\r
+}\r
+\r
+\r
+unsigned int\r
+PrintPDF::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,\r
+              SPStyle const *const style)\r
+{\r
+    if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.\r
+    if (_bitmap) return 0;\r
+\r
+    Inkscape::SVGOStringStream os;\r
+    os.setf(std::ios::fixed);\r
+\r
+    return 0;\r
+    \r
+    // Escape chars\r
+    Inkscape::SVGOStringStream escaped_text;\r
+    escaped_text.setf(std::ios::fixed);\r
+    \r
+    escaped_text << std::oct;\r
+    for (gchar const *p_text = text ; *p_text ; p_text = g_utf8_next_char(p_text)) {\r
+        gunichar const c = g_utf8_get_char(p_text);\r
+        if (c == '\\' || c == ')' || c == '(')\r
+            escaped_text << '\\' << static_cast<char>(c);\r
+        else if (c >= 0x80)\r
+            escaped_text << '\\' << c;\r
+        else\r
+            escaped_text << static_cast<char>(c);\r
+    }\r
+\r
+    os << "gsave\n";\r
+\r
+    // set font\r
+    char const *fn = PSFontName(style);\r
+    if (_latin1_encoded_fonts.find(fn) == _latin1_encoded_fonts.end()) {\r
+        if (!_newlatin1font_proc_defined) {\r
+            // input: newfontname, existingfontname\r
+            // output: new font object, also defined to newfontname\r
+            os << "/newlatin1font "         // name of the proc\r
+                  "{findfont dup length dict copy "     // load the font and create a copy of it\r
+                  "dup /Encoding ISOLatin1Encoding put "     // change the encoding in the copy\r
+                  "definefont} def\n";      // create the new font and leave it on the stack, define the proc\r
+            _newlatin1font_proc_defined = true;\r
+        }\r
+        os << "/" << fn << "-ISOLatin1 /" << fn << " newlatin1font\n";\r
+        _latin1_encoded_fonts.insert(fn);\r
+    } else\r
+        os << "/" << fn << "-ISOLatin1 findfont\n";\r
+    os << style->font_size.computed << " scalefont\n";\r
+    os << "setfont\n";\r
+    g_free((void *) fn);\r
+\r
+    if ( style->fill.type == SP_PAINT_TYPE_COLOR\r
+         || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER\r
+              && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )\r
+    {\r
+        // set fill style\r
+        print_fill_style(os, style, NULL);\r
+        // FIXME: we don't know the pbox of text, so have to pass NULL. This means gradients with\r
+        // bbox units won't work with text. However userspace gradients don't work with text either\r
+        // (text is black) for some reason.\r
+\r
+        os << "newpath\n";\r
+        os << p[NR::X] << " " << p[NR::Y] << " moveto\n";\r
+        os << "(" << escaped_text.str() << ") show\n";\r
+    }\r
+\r
+    if (style->stroke.type == SP_PAINT_TYPE_COLOR) {\r
+\r
+        // set stroke style\r
+        print_stroke_style(os, style);\r
+\r
+        // paint stroke\r
+        os << "newpath\n";\r
+        os << p[NR::X] << " " << p[NR::Y] << " moveto\n";\r
+        os << "(" << escaped_text.str() << ") false charpath stroke\n";\r
+    }\r
+\r
+    os << "grestore\n";\r
+\r
+    fprintf(_stream, "%s", os.str().c_str());\r
+\r
+    return 0;\r
+}\r
+\r
+\r
+\r
+/* PDF helpers */\r
+\r
+void\r
+PrintPDF::print_bpath(SVGOStringStream &os, NArtBpath const *bp)\r
+{\r
+    bool closed = false;\r
+    while (bp->code != NR_END) {\r
+        switch (bp->code) {\r
+            case NR_MOVETO:\r
+                if (closed) {\r
+                    *page_stream << "h\n";\r
+                }\r
+                closed = true;\r
+                *page_stream << bp->x3 << " " << bp->y3 << " m\n";\r
+                break;\r
+            case NR_MOVETO_OPEN:\r
+                if (closed) {\r
+                    *page_stream << "h\n";\r
+                }\r
+                closed = false;\r
+                *page_stream << bp->x3 << " " << bp->y3 << " m\n";\r
+                break;\r
+            case NR_LINETO:\r
+                *page_stream << bp->x3 << " " << bp->y3 << " l\n";\r
+                break;\r
+            case NR_CURVETO:\r
+                *page_stream << bp->x1 << " " << bp->y1 << " "\r
+                   << bp->x2 << " " << bp->y2 << " "\r
+                   << bp->x3 << " " << bp->y3 << " c\n";\r
+                break;\r
+            default:\r
+                break;\r
+        }\r
+        bp += 1;\r
+    }\r
+    if (closed) {\r
+        *page_stream << "h\n";\r
+    }\r
+}\r
+\r
+/* The following code is licensed under GNU GPL.\r
+** The packbits, ascii85 and imaging printing code\r
+** is from the gimp's postscript.c.\r
+*/\r
+\r
+/**\r
+* \param nin Number of bytes of source data.\r
+* \param src Source data.\r
+* \param nout Number of output bytes.\r
+* \param dst Buffer for output.\r
+*/\r
+void\r
+PrintPDF::compress_packbits(int nin,\r
+                           guchar *src,\r
+                           int *nout,\r
+                           guchar *dst)\r
+\r
+{\r
+    register guchar c;\r
+    int nrepeat, nliteral;\r
+    guchar *run_start;\r
+    guchar *start_dst = dst;\r
+    guchar *last_literal = NULL;\r
+\r
+    for (;;) {\r
+        if (nin <= 0) break;\r
+\r
+        run_start = src;\r
+        c = *run_start;\r
+\r
+        /* Search repeat bytes */\r
+        if ((nin > 1) && (c == src[1])) {\r
+            nrepeat = 1;\r
+            nin -= 2;\r
+            src += 2;\r
+            while ((nin > 0) && (c == *src)) {\r
+                nrepeat++;\r
+                src++;\r
+                nin--;\r
+                if (nrepeat == 127) break; /* Maximum repeat */\r
+            }\r
+\r
+            /* Add two-byte repeat to last literal run ? */\r
+            if ( (nrepeat == 1)\r
+                 && (last_literal != NULL) && (((*last_literal)+1)+2 <= 128) )\r
+            {\r
+                *last_literal += 2;\r
+                *(dst++) = c;\r
+                *(dst++) = c;\r
+                continue;\r
+            }\r
+\r
+            /* Add repeat run */\r
+            *(dst++) = (guchar)((-nrepeat) & 0xff);\r
+            *(dst++) = c;\r
+            last_literal = NULL;\r
+            continue;\r
+        }\r
+        /* Search literal bytes */\r
+        nliteral = 1;\r
+        nin--;\r
+        src++;\r
+\r
+        for (;;) {\r
+            if (nin <= 0) break;\r
+\r
+            if ((nin >= 2) && (src[0] == src[1])) /* A two byte repeat ? */\r
+                break;\r
+\r
+            nliteral++;\r
+            nin--;\r
+            src++;\r
+            if (nliteral == 128) break; /* Maximum literal run */\r
+        }\r
+\r
+        /* Could be added to last literal run ? */\r
+        if ((last_literal != NULL) && (((*last_literal)+1)+nliteral <= 128)) {\r
+            *last_literal += nliteral;\r
+        } else {\r
+            last_literal = dst;\r
+            *(dst++) = (guchar)(nliteral-1);\r
+        }\r
+        while (nliteral-- > 0) *(dst++) = *(run_start++);\r
+    }\r
+    *nout = dst - start_dst;\r
+}\r
+\r
+void\r
+PrintPDF::ascii85_init(void)\r
+{\r
+    ascii85_len = 0;\r
+    ascii85_linewidth = 0;\r
+}\r
+\r
+void\r
+PrintPDF::ascii85_flush(SVGOStringStream &os)\r
+{\r
+    char c[5];\r
+    bool const zero_case = (ascii85_buf == 0);\r
+    static int const max_linewidth = 75;\r
+\r
+    for (int i = 4; i >= 0; i--) {\r
+        c[i] = (ascii85_buf % 85) + '!';\r
+        ascii85_buf /= 85;\r
+    }\r
+    /* check for special case: "!!!!!" becomes "z", but only if not\r
+     * at end of data. */\r
+    if (zero_case && (ascii85_len == 4)) {\r
+        if (ascii85_linewidth >= max_linewidth) {\r
+            os << '\n';\r
+            ascii85_linewidth = 0;\r
+        }\r
+        os << 'z';\r
+        ascii85_linewidth++;\r
+    } else {\r
+        for (int i = 0; i < ascii85_len+1; i++) {\r
+            if ((ascii85_linewidth >= max_linewidth) && (c[i] != '%')) {\r
+                os << '\n';\r
+                ascii85_linewidth = 0;\r
+            }\r
+            os << c[i];\r
+            ascii85_linewidth++;\r
+        }\r
+    }\r
+\r
+    ascii85_len = 0;\r
+    ascii85_buf = 0;\r
+}\r
+\r
+inline void\r
+PrintPDF::ascii85_out(guchar byte, SVGOStringStream &os)\r
+{\r
+    if (ascii85_len == 4)\r
+        ascii85_flush(os);\r
+\r
+    ascii85_buf <<= 8;\r
+    ascii85_buf |= byte;\r
+    ascii85_len++;\r
+}\r
+\r
+void\r
+PrintPDF::ascii85_nout(int n, guchar *uptr, SVGOStringStream &os)\r
+{\r
+    while (n-- > 0) {\r
+        ascii85_out(*uptr, os);\r
+        uptr++;\r
+    }\r
+}\r
+\r
+void\r
+PrintPDF::ascii85_done(SVGOStringStream &os)\r
+{\r
+    if (ascii85_len) {\r
+        /* zero any unfilled buffer portion, then flush */\r
+        ascii85_buf <<= (8 * (4-ascii85_len));\r
+        ascii85_flush(os);\r
+    }\r
+\r
+    os << "~>\n";\r
+}\r
+\r
+unsigned int\r
+PrintPDF::print_image(FILE *ofp, guchar *px, unsigned int width, unsigned int height, unsigned int rs,\r
+                     NRMatrix const *transform)\r
+{\r
+    Inkscape::SVGOStringStream os;\r
+    os.setf(std::ios::fixed);\r
+\r
+    return 0;\r
+    \r
+    os << "gsave\n";\r
+    os << "[" << transform->c[0] << " "\r
+       << transform->c[1] << " "\r
+       << transform->c[2] << " "\r
+       << transform->c[3] << " "\r
+       << transform->c[4] << " "\r
+       << transform->c[5] << "] concat\n";\r
+    os << width << " " << height << " 8 ["\r
+       << width << " 0 0 -" << height << " 0 " << height << "]\n";\r
+\r
+\r
+    /* Write read image procedure */\r
+    os << "% Strings to hold RGB-samples per scanline\n";\r
+    os << "/rstr " << width << " string def\n";\r
+    os << "/gstr " << width << " string def\n";\r
+    os << "/bstr " << width << " string def\n";\r
+    os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter rstr readstring pop}\n";\r
+    os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter gstr readstring pop}\n";\r
+    os << "{currentfile /ASCII85Decode filter /RunLengthDecode filter bstr readstring pop}\n";\r
+    os << "true 3\n";\r
+\r
+    /* Allocate buffer for packbits data. Worst case: Less than 1% increase */\r
+    guchar *const packb = (guchar *)g_malloc((width * 105)/100+2);\r
+    guchar *const plane = (guchar *)g_malloc(width);\r
+\r
+    /* ps_begin_data(ofp); */\r
+    os << "colorimage\n";\r
+\r
+/*#define GET_RGB_TILE(begin)                   \\r
+ *  {int scan_lines;                                                    \\r
+ *    scan_lines = (i+tile_height-1 < height) ? tile_height : (height-i); \\r
+ *    gimp_pixel_rgn_get_rect(&pixel_rgn, begin, 0, i, width, scan_lines); \\r
+ *    src = begin; }\r
+ */\r
+\r
+    for (unsigned i = 0; i < height; i++) {\r
+        /* if ((i % tile_height) == 0) GET_RGB_TILE(data); */ /* Get more data */\r
+        guchar const *const src = px + i * rs;\r
+\r
+        /* Iterate over RGB */\r
+        for (int rgb = 0; rgb < 3; rgb++) {\r
+            guchar const *src_ptr = src + rgb;\r
+            guchar *plane_ptr = plane;\r
+            for (unsigned j = 0; j < width; j++) {\r
+                *(plane_ptr++) = *src_ptr;\r
+                src_ptr += 4;\r
+            }\r
+\r
+            int nout;\r
+            compress_packbits(width, plane, &nout, packb);\r
+\r
+            ascii85_init();\r
+            ascii85_nout(nout, packb, os);\r
+            ascii85_out(128, os); /* Write EOD of RunLengthDecode filter */\r
+            ascii85_done(os);\r
+        }\r
+    }\r
+    /* ps_end_data(ofp); */\r
+\r
+#if 0\r
+    fprintf(ofp, "showpage\n");\r
+    g_free(data);\r
+#endif\r
+\r
+    g_free(packb);\r
+    g_free(plane);\r
+\r
+#if 0\r
+    if (ferror(ofp)) {\r
+        g_message(_("write error occurred"));\r
+        return (FALSE);\r
+    }\r
+#endif\r
+\r
+    os << "grestore\n";\r
+\r
+    fprintf(ofp, "%s", os.str().c_str());\r
+\r
+    return 0;\r
+//#undef GET_RGB_TILE\r
+}\r
+\r
+bool\r
+PrintPDF::textToPath(Inkscape::Extension::Print * ext)\r
+{\r
+    return ext->get_param_bool("textToPath");\r
+}\r
+\r
+#include "clear-n_.h"\r
+\r
+void\r
+PrintPDF::init(void)\r
+{\r
+    /* SVG in */\r
+    (void) Inkscape::Extension::build_from_mem(\r
+        "<inkscape-extension>\n"\r
+        "<name>" N_("PDF Print") "</name>\n"\r
+        "<id>" SP_MODULE_KEY_PRINT_PDF "</id>\n"\r
+        "<param name=\"bitmap\" type=\"boolean\">FALSE</param>\n"\r
+        "<param name=\"resolution\" type=\"string\">72</param>\n"\r
+        "<param name=\"destination\" type=\"string\">| lp</param>\n"\r
+        "<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"\r
+        "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"\r
+        "<print/>\n"\r
+        "</inkscape-extension>", new PrintPDF());\r
+}\r
+\r
+\r
+}  /* namespace Internal */\r
+}  /* namespace Extension */\r
+}  /* namespace Inkscape */\r
+\r
+/* End of GNU GPL code */\r
+\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.h b/src/extension/internal/pdf.h
new file mode 100644 (file)
index 0000000..0273f98
--- /dev/null
@@ -0,0 +1,113 @@
+#ifndef EXTENSION_INTERNAL_PDF_H_SEEN\r
+#define EXTENSION_INTERNAL_PDF_H_SEEN\r
+\r
+/*\r
+ * Authors:\r
+ *     Lauris Kaplinski <lauris@kaplinski.com>\r
+ *     Ted Gould <ted@gould.cx>\r
+ *     Ulf Erikson <ulferikson@users.sf.net>\r
+ *\r
+ * Lauris' original code is in the public domain.\r
+ * Ted's changes are licensed under the GNU GPL.\r
+ */\r
+\r
+#include <config.h>\r
+#include "extension/extension.h"\r
+#include "extension/implementation/implementation.h"\r
+#include <set>\r
+#include <string>\r
+\r
+#include "libnr/nr-path.h"\r
+\r
+#include "svg/stringstream.h"\r
+\r
+class PdfFile;\r
+class PdfObject;\r
+\r
+namespace Inkscape {\r
+namespace Extension {\r
+namespace Internal {\r
+\r
+class PrintPDF : public Inkscape::Extension::Implementation::Implementation {\r
+    float _width;\r
+    float _height;\r
+    FILE *_stream;\r
+    PdfFile   *pdf_file;\r
+    PdfObject *doc_info;\r
+    PdfObject *page_stream;\r
+    unsigned short _dpi;\r
+    bool _bitmap;\r
+    std::set<std::string> _latin1_encoded_fonts;\r
+    bool _newlatin1font_proc_defined;\r
+\r
+    void print_bpath(SVGOStringStream &os, NArtBpath const *bp);\r
+\r
+    void print_fill_style(SVGOStringStream &os, SPStyle const *style, NRRect const *pbox);\r
+    void print_fill_alpha(SVGOStringStream &os, SPStyle const *style, NRRect const *pbox);\r
+    void print_stroke_style(SVGOStringStream &os, SPStyle const *style);\r
+\r
+    char const *PSFontName(SPStyle const *style);\r
+\r
+    unsigned int print_image(FILE *ofp, guchar *px, unsigned int width, unsigned int height, unsigned int rs,\r
+                             NRMatrix const *transform);\r
+    void compress_packbits(int nin, guchar *src, int *nout, guchar *dst);\r
+\r
+    /* ASCII 85 variables */\r
+    guint32 ascii85_buf;\r
+    int ascii85_len;\r
+    int ascii85_linewidth;\r
+    /* ASCII 85 Functions */\r
+    void ascii85_init(void);\r
+    void ascii85_flush(SVGOStringStream &os);\r
+    inline void ascii85_out(guchar byte, SVGOStringStream &os);\r
+    void ascii85_nout(int n, guchar *uptr, SVGOStringStream &os);\r
+    void ascii85_done(SVGOStringStream &os);\r
+\r
+\r
+public:\r
+    PrintPDF(void);\r
+    virtual ~PrintPDF(void);\r
+\r
+    /* Print functions */\r
+    virtual unsigned int setup(Inkscape::Extension::Print *module);\r
+    /*\r
+      virtual unsigned int set_preview(Inkscape::Extension::Print *module);\r
+    */\r
+\r
+    virtual unsigned int begin(Inkscape::Extension::Print *module, SPDocument *doc);\r
+    virtual unsigned int finish(Inkscape::Extension::Print *module);\r
+\r
+    /* Rendering methods */\r
+    virtual unsigned int bind(Inkscape::Extension::Print *module, NRMatrix const *transform, float opacity);\r
+    virtual unsigned int release(Inkscape::Extension::Print *module);\r
+    virtual unsigned int comment(Inkscape::Extension::Print *module, char const *comment);\r
+    virtual unsigned int fill(Inkscape::Extension::Print *module, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,\r
+                              NRRect const *pbox, NRRect const *dbox, NRRect const *bbox);\r
+    virtual unsigned int stroke(Inkscape::Extension::Print *module, NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,\r
+                                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox);\r
+    virtual unsigned int image(Inkscape::Extension::Print *module, unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,\r
+                               NRMatrix const *transform, SPStyle const *style);\r
+    virtual unsigned int text(Inkscape::Extension::Print *module, char const *text,\r
+                              NR::Point p, SPStyle const *style);\r
+\r
+    bool textToPath(Inkscape::Extension::Print *ext);\r
+    static void init(void);\r
+};\r
+\r
+}  /* namespace Internal */\r
+}  /* namespace Extension */\r
+}  /* namespace Inkscape */\r
+\r
+\r
+#endif /* !EXTENSION_INTERNAL_PDF_H_SEEN */\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
index 3cbfcd86b38f7ff64901b3097f5cfeb1d532f9e5..5ef5c2ec06ddce94f6d2d1282b49920183f49107 100644 (file)
@@ -28,7 +28,7 @@ operator<<(Inkscape::SVGOStringStream &os, float d)
 
     std::ostringstream s;
     s.imbue(std::locale::classic());
-    s.setf(std::ios::showpoint);
+    s.flags(os.setf(std::ios::showpoint));
     s.precision(os.precision());
     s << d;
     os << strip_trailing_zeros(s.str());
@@ -49,7 +49,7 @@ operator<<(Inkscape::SVGOStringStream &os, double d)
 
     std::ostringstream s;
     s.imbue(std::locale::classic());
-    s.setf(std::ios::showpoint);
+    s.flags(os.setf(std::ios::showpoint));
     s.precision(os.precision());
     s << d;
     os << strip_trailing_zeros(s.str());
index 34cbdc2bd81ecaa301cd85db694b3cb2d4eab3ea..b08a89e03af968302535628cb35bffdbe473185d 100644 (file)
@@ -53,6 +53,18 @@ public:
     std::streamsize precision(std::streamsize p) {
         return ostr.precision(p);
     }
+
+    std::ios::fmtflags setf(std::ios::fmtflags fmtfl) {
+        return ostr.setf(fmtfl);
+    }
+
+    std::ios::fmtflags setf(std::ios::fmtflags fmtfl, std::ios::fmtflags mask) {
+        return ostr.setf(fmtfl, mask);
+    }
+
+    void unsetf(std::ios::fmtflags mask) {
+        ostr.unsetf(mask);
+    }
 };
 
 }