Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / extension / internal / pov-out.cpp
index 7318fa78457873bbcaa21388f5cbeb33c333ab6d..a130b692354142786208ac3b1bd8fcfe97aa75bd 100644 (file)
@@ -9,9 +9,10 @@
  *      http://www.povray.org
  *
  * Authors:
- *   Bob Jamison <ishmalius@gmail.com>
+ *   Bob Jamison <ishmal@inkscape.org>
+ *   Abhishek Sharma
  *
- * Copyright (C) 2004-2007 Authors
+ * Copyright (C) 2004-2008 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 # include <config.h>
 #endif
 #include "pov-out.h"
-#include "inkscape.h"
-#include "sp-path.h"
+#include <inkscape.h>
+#include <inkscape-version.h>
+#include <sp-path.h>
 #include <style.h>
-#include "display/curve.h"
-#include "libnr/n-art-bpath.h"
-#include "extension/system.h"
-
-#include "io/sys.h"
+#include <display/curve.h>
+#include <extension/system.h>
+#include <2geom/pathvector.h>
+#include <2geom/rect.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
+#include "helper/geom.h"
+#include "helper/geom-curves.h"
+#include <io/sys.h>
 
 #include <string>
 #include <stdio.h>
@@ -43,33 +49,26 @@ namespace Internal
 {
 
 
-
-
 //########################################################################
-//# U T I L I T Y
+//# M E S S A G E S
 //########################################################################
 
-
-
-/**
- * This function searches the Repr tree recursively from the given node,
- * and adds refs to all nodes with the given name, to the result vector
- */
-static void
-findElementsByTagName(std::vector<Inkscape::XML::Node *> &results,
-                      Inkscape::XML::Node *node,
-                      char const *name)
+static void err(const char *fmt, ...)
 {
-    if ( !name || strcmp(node->name(), name) == 0 )
-        results.push_back(node);
+    va_list args;
+    g_log(NULL,  G_LOG_LEVEL_WARNING, "Pov-out err: ");
+    va_start(args, fmt);
+    g_logv(NULL, G_LOG_LEVEL_WARNING, fmt, args);
+    va_end(args);
+    g_log(NULL,  G_LOG_LEVEL_WARNING, "\n");
+}
 
-    for (Inkscape::XML::Node *child = node->firstChild() ; child ;
-              child = child->next())
-        findElementsByTagName( results, child, name );
 
-}
 
 
+//########################################################################
+//# U T I L I T Y
+//########################################################################
 
 
 
@@ -94,37 +93,30 @@ effective_opacity(SPItem const *item)
 //# OUTPUT FORMATTING
 //########################################################################
 
-static const char *formatDouble(gchar *sbuffer, double d)
-{
-    return (const char *)g_ascii_formatd(sbuffer,
-                G_ASCII_DTOSTR_BUF_SIZE, "%.8g", (gdouble)d);
-
-}
-
 
 /**
- * Not-threadsafe version
+ * We want to control floating output format
  */
-static char _dstr_buf[G_ASCII_DTOSTR_BUF_SIZE+1];
-
-static const char *dstr(double d)
+static PovOutput::String dstr(double d)
 {
-    return formatDouble(_dstr_buf, d);
+    char dbuf[G_ASCII_DTOSTR_BUF_SIZE+1];
+    g_ascii_formatd(dbuf, G_ASCII_DTOSTR_BUF_SIZE,
+                  "%.8f", (gdouble)d);
+    PovOutput::String s = dbuf;
+    return s;
 }
 
-
-
-
+#define DSTR(d) (dstr(d).c_str())
 
 
 /**
  *  Output data to the buffer, printf()-style
  */
-void PovOutput::out(char *fmt, ...)
+void PovOutput::out(const char *fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    gchar * output = g_strdup_vprintf(fmt, args);
+    gchar *output = g_strdup_vprintf(fmt, args);
     va_end(args);
     outbuf.append(output);
     g_free(output);
@@ -134,18 +126,12 @@ void PovOutput::out(char *fmt, ...)
 
 
 
-
-
 /**
- *  Output a 3d vector
+ *  Output a 2d vector
  */
 void PovOutput::vec2(double a, double b)
 {
-    outbuf.append("<");
-    outbuf.append(dstr(a));
-    outbuf.append(", ");
-    outbuf.append(dstr(b));
-    outbuf.append(">");
+    out("<%s, %s>", DSTR(a), DSTR(b));
 }
 
 
@@ -155,13 +141,7 @@ void PovOutput::vec2(double a, double b)
  */
 void PovOutput::vec3(double a, double b, double c)
 {
-    outbuf.append("<");
-    outbuf.append(dstr(a));
-    outbuf.append(", ");
-    outbuf.append(dstr(b));
-    outbuf.append(", ");
-    outbuf.append(dstr(c));
-    outbuf.append(">");
+    out("<%s, %s, %s>", DSTR(a), DSTR(b), DSTR(c));
 }
 
 
@@ -171,25 +151,18 @@ void PovOutput::vec3(double a, double b, double c)
  */
 void PovOutput::vec4(double a, double b, double c, double d)
 {
-    outbuf.append("<");
-    outbuf.append(dstr(a));
-    outbuf.append(", ");
-    outbuf.append(dstr(b));
-    outbuf.append(", ");
-    outbuf.append(dstr(c));
-    outbuf.append(", ");
-    outbuf.append(dstr(d));
-    outbuf.append(">");
+    out("<%s, %s, %s, %s>", DSTR(a), DSTR(b), DSTR(c), DSTR(d));
 }
 
 
+
 /**
  *  Output an rgbf color vector
  */
 void PovOutput::rgbf(double r, double g, double b, double f)
 {
     //"rgbf < %1.3f, %1.3f, %1.3f %1.3f>"
-    outbuf.append("rgbf ");
+    out("rgbf ");
     vec4(r, g, b, f);
 }
 
@@ -198,22 +171,21 @@ void PovOutput::rgbf(double r, double g, double b, double f)
 /**
  *  Output one bezier's start, start-control, end-control, and end nodes
  */
-void PovOutput::segment(int segNr, double a0, double a1,
-                            double b0, double b1,
-                            double c0, double c1,
-                            double d0, double d1)
+void PovOutput::segment(int segNr,
+                        double startX,     double startY,
+                        double startCtrlX, double startCtrlY,
+                        double endCtrlX,   double endCtrlY,
+                        double endX,       double endY)
 {
     //"    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>"
-    char buf[32];
-    snprintf(buf, 31, "    /*%4d*/ ", segNr);
-    outbuf.append(buf);
-    vec2(a0, a1);
-    outbuf.append(", ");
-    vec2(b0, b1);
-    outbuf.append(", ");
-    vec2(c0, c1);
-    outbuf.append(", ");
-    vec2(d0, d1);
+    out("    /*%4d*/ ", segNr);
+    vec2(startX,     startY);
+    out(", ");
+    vec2(startCtrlX, startCtrlY);
+    out(", ");
+    vec2(endCtrlX,   endCtrlY);
+    out(", ");
+    vec2(endX,       endY);
 }
 
 
@@ -223,14 +195,14 @@ void PovOutput::segment(int segNr, double a0, double a1,
 /**
  * Output the file header
  */
-void PovOutput::doHeader()
+bool PovOutput::doHeader()
 {
     time_t tim = time(NULL);
     out("/*###################################################################\n");
     out("### This PovRay document was generated by Inkscape\n");
     out("### http://www.inkscape.org\n");
-    out("### Created: %s", ctime(&tim));
-    out("### Version: %s\n", VERSION);
+    out("### Created: %s",   ctime(&tim));
+    out("### Version: %s\n", Inkscape::version_string);
     out("#####################################################################\n");
     out("### NOTES:\n");
     out("### ============\n");
@@ -244,6 +216,11 @@ void PovOutput::doHeader()
     out("###\n");
     out("### For an example of how to use this file, look at\n");
     out("### share/examples/istest.pov\n");
+    out("###\n");
+    out("### If you have any problems with this output, please see the\n");
+    out("### Inkscape project at http://www.inkscape.org, or visit\n");
+    out("### the #inkscape channel on irc.freenode.net . \n");
+    out("###\n");
     out("###################################################################*/\n");
     out("\n\n");
     out("/*###################################################################\n");
@@ -254,6 +231,7 @@ void PovOutput::doHeader()
     out("##    Nodes    : %d\n", nrNodes);
     out("###################################################################*/\n");
     out("\n\n\n");
+    return true;
 }
 
 
@@ -261,13 +239,14 @@ void PovOutput::doHeader()
 /**
  *  Output the file footer
  */
-void PovOutput::doTail()
+bool PovOutput::doTail()
 {
     out("\n\n");
     out("/*###################################################################\n");
     out("### E N D    F I L E\n");
     out("###################################################################*/\n");
     out("\n\n");
+    return true;
 }
 
 
@@ -275,60 +254,33 @@ void PovOutput::doTail()
 /**
  *  Output the curve data to buffer
  */
-void PovOutput::doCurves(SPDocument *doc)
+bool PovOutput::doCurve(SPItem *item, const String &id)
 {
-    std::vector<Inkscape::XML::Node *>results;
-    //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path");
-    findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL);
-    if (results.size() == 0)
-        return;
+    using Geom::X;
+    using Geom::Y;
 
-    double bignum = 1000000.0;
-    double minx  =  bignum;
-    double maxx  = -bignum;
-    double miny  =  bignum;
-    double maxy  = -bignum;
+    //### Get the Shape
+    if (!SP_IS_SHAPE(item))//Bulia's suggestion.  Allow all shapes
+        return true;
 
-    for (unsigned int indx = 0; indx < results.size() ; indx++)
-        {
-        //### Fetch the object from the repr info
-        Inkscape::XML::Node *rpath = results[indx];
-        char *str  = (char *) rpath->attribute("id");
-        if (!str)
-            continue;
-
-        String id = str;
-        SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath);
-        if (!reprobj)
-            continue;
-
-        //### Get the transform of the item
-        if (!SP_IS_ITEM(reprobj))
-            continue;
-
-        SPItem *item = SP_ITEM(reprobj);
-        NR::Matrix tf = sp_item_i2d_affine(item);
-
-        //### Get the Shape
-        if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion.  Allow all shapes
-            continue;
-
-        SPShape *shape = SP_SHAPE(reprobj);
-        SPCurve *curve = shape->curve;
-        if (sp_curve_empty(curve))
-            continue;
-            
-        nrShapes++;
-
-        PovShapeInfo shapeInfo;
-        shapeInfo.id    = id;
-        shapeInfo.color = "";
-
-        //Try to get the fill color of the shape
-        SPStyle *style = SP_OBJECT_STYLE(shape);
-        /* fixme: Handle other fill types, even if this means translating gradients to a single
+    SPShape *shape = SP_SHAPE(item);
+    SPCurve *curve = shape->curve;
+    if (curve->is_empty())
+        return true;
+
+    nrShapes++;
+
+    PovShapeInfo shapeInfo;
+    shapeInfo.id    = id;
+    shapeInfo.color = "";
+
+    //Try to get the fill color of the shape
+    SPStyle *style = SP_OBJECT_STYLE(shape);
+    /* fixme: Handle other fill types, even if this means translating gradients to a single
            flat colour. */
-        if (style && (style->fill.type == SP_PAINT_TYPE_COLOR))
+    if (style)
+        {
+        if (style->fill.isColor())
             {
             // see color.h for how to parse SPColor
             float rgb[3];
@@ -344,146 +296,198 @@ void PovOutput::doCurves(SPDocument *doc)
             rgbf.append(dstr(1.0 - dopacity)); rgbf.append(">");
             shapeInfo.color += rgbf;
             }
+        }
 
-        povShapes.push_back(shapeInfo); //passed all tests.  save the info
+    povShapes.push_back(shapeInfo); //passed all tests.  save the info
+
+    // convert the path to only lineto's and cubic curveto's:
+    Geom::Matrix tf = item->i2d_affine();
+    Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf );
+
+    /*
+     * We need to know the number of segments (NR_CURVETOs/LINETOs, including
+     * closing line segment) before we write out segment data. Since we are
+     * going to skip degenerate (zero length) paths, we need to loop over all
+     * subpaths and segments first.
+     */
+    int segmentCount = 0;
+    /**
+     * For all Subpaths in the <path>
+     */
+    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit)
+    {
+        /**
+         * For all segments in the subpath, including extra closing segment defined by 2geom
+         */
+        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit)
+        {
 
-        int curveLength = SP_CURVE_LENGTH(curve);
+            // Skip zero length segments.
+            if( !cit->isDegenerate() ) ++segmentCount;
+        }
+    }
+
+    out("/*###################################################\n");
+    out("### PRISM:  %s\n", id.c_str());
+    out("###################################################*/\n");
+    out("#declare %s = prism {\n", id.c_str());
+    out("    linear_sweep\n");
+    out("    bezier_spline\n");
+    out("    1.0, //top\n");
+    out("    0.0, //bottom\n");
+    out("    %d //nr points\n", segmentCount * 4);
+    int segmentNr = 0;
+
+    nrSegments += segmentCount;
+
+    /**
+     *   at moment of writing, 2geom lacks proper initialization of empty intervals in rect...
+     */
+    Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() );
+
+
+    /**
+     * For all Subpaths in the <path>
+     */
+    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit)
+        {
 
-        //Count the NR_CURVETOs/LINETOs
-        int segmentCount=0;
-        NArtBpath *bp = SP_CURVE_BPATH(curve);
-        for (int curveNr=0 ; curveNr<curveLength ; curveNr++, bp++)
-            if (bp->code == NR_CURVETO || bp->code == NR_LINETO)
-                segmentCount++;
+        cminmax.expandTo(pit->initialPoint());
 
-        double cminx  =  bignum;
-        double cmaxx  = -bignum;
-        double cminy  =  bignum;
-        double cmaxy  = -bignum;
-        double lastx  = 0.0;
-        double lasty  = 0.0;
+        /**
+         * For all segments in the subpath, including extra closing segment defined by 2geom
+         */
+        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit)
+            {
 
-        out("/*###################################################\n");
-        out("### PRISM:  %s\n", id.c_str());
-        out("###################################################*/\n");
-        out("#declare %s = prism {\n", id.c_str());
-        out("    linear_sweep\n");
-        out("    bezier_spline\n");
-        out("    1.0, //top\n");
-        out("    0.0, //bottom\n");
-        out("    %d //nr points\n", segmentCount * 4);
-        int segmentNr = 0;
-        bp = SP_CURVE_BPATH(curve);
-        
-        nrSegments += curveLength;
-
-        for (int curveNr=0 ; curveNr < curveLength ; curveNr++)
+            // Skip zero length segments
+            if( cit->isDegenerate() )
+                continue;
+
+            if( is_straight_curve(*cit) )
+                {
+                Geom::Point p0 = cit->initialPoint();
+                Geom::Point p1 = cit->finalPoint();
+                segment(segmentNr++,
+                        p0[X], p0[Y], p0[X], p0[Y], p1[X], p1[Y], p1[X], p1[Y] );
+                nrNodes += 8;
+                }
+            else if(Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
             {
-            using NR::X;
-            using NR::Y;
-            NR::Point const p1(bp->c(1) * tf);
-            NR::Point const p2(bp->c(2) * tf);
-            NR::Point const p3(bp->c(3) * tf);
-            double const x1 = p1[X], y1 = p1[Y];
-            double const x2 = p2[X], y2 = p2[Y];
-            double const x3 = p3[X], y3 = p3[Y];
-
-            switch (bp->code)
+                std::vector<Geom::Point> points = cubic->points();
+                Geom::Point p0 = points[0];
+                Geom::Point p1 = points[1];
+                Geom::Point p2 = points[2];
+                Geom::Point p3 = points[3];
+                segment(segmentNr++,
+                            p0[X],p0[Y], p1[X],p1[Y], p2[X],p2[Y], p3[X],p3[Y]);
+                nrNodes += 8;
+                }
+            else
+            {
+                err("logical error, because pathv_to_linear_and_cubic_beziers was used");
+                return false;
+                }
+
+            if (segmentNr < segmentCount)
+                out(",\n");
+            else
+                out("\n");
+            if (segmentNr > segmentCount)
                 {
-                case NR_MOVETO:
-                case NR_MOVETO_OPEN:
-                    {
-                    //fprintf(f, "moveto: %f %f\n", bp->x3, bp->y3);
-                    break;
-                    }
-                case NR_CURVETO:
-                    {
-                    //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
-                    //        segmentNr++, lastx, lasty, x1, y1, x2, y2, x3, y3);
-                    segment(segmentNr++,
-                          lastx, lasty, x1, y1, x2, y2, x3, y3);
-                    nrNodes += 8;
-
-                    if (segmentNr < segmentCount)
-                        out(",\n");
-                    else
-                        out("\n");
-
-                    if (lastx < cminx)
-                        cminx = lastx;
-                    if (lastx > cmaxx)
-                        cmaxx = lastx;
-                    if (lasty < cminy)
-                        cminy = lasty;
-                    if (lasty > cmaxy)
-                        cmaxy = lasty;
-                    break;
-                    }
-                case NR_LINETO:
-                    {
-                    //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
-                    //        segmentNr++, lastx, lasty, lastx, lasty, x3, y3, x3, y3);
-                    segment(segmentNr++,
-                         lastx, lasty, lastx, lasty, x3, y3, x3, y3);
-                    nrNodes += 8;
-
-                    if (segmentNr < segmentCount)
-                        out(",\n");
-                    else
-                        out("\n");
-
-                    //fprintf(f, "lineto\n");
-                    if (lastx < cminx)
-                        cminx = lastx;
-                    if (lastx > cmaxx)
-                        cmaxx = lastx;
-                    if (lasty < cminy)
-                        cminy = lasty;
-                    if (lasty > cmaxy)
-                        cmaxy = lasty;
-                    break;
-                    }
-                case NR_END:
-                    {
-                    //fprintf(f, "end\n");
-                    break;
-                    }
+                err("Too many segments");
+                return false;
                 }
-            lastx = x3;
-            lasty = y3;
-            bp++;
+
+            cminmax.expandTo(cit->finalPoint());
+
             }
-        out("}\n");
+        }
 
+    out("}\n");
+
+    double cminx = cminmax.min()[X];
+    double cmaxx = cminmax.max()[X];
+    double cminy = cminmax.min()[Y];
+    double cmaxy = cminmax.max()[Y];
+
+    out("#declare %s_MIN_X    = %s;\n", id.c_str(), DSTR(cminx));
+    out("#declare %s_CENTER_X = %s;\n", id.c_str(), DSTR((cmaxx+cminx)/2.0));
+    out("#declare %s_MAX_X    = %s;\n", id.c_str(), DSTR(cmaxx));
+    out("#declare %s_WIDTH    = %s;\n", id.c_str(), DSTR(cmaxx-cminx));
+    out("#declare %s_MIN_Y    = %s;\n", id.c_str(), DSTR(cminy));
+    out("#declare %s_CENTER_Y = %s;\n", id.c_str(), DSTR((cmaxy+cminy)/2.0));
+    out("#declare %s_MAX_Y    = %s;\n", id.c_str(), DSTR(cmaxy));
+    out("#declare %s_HEIGHT   = %s;\n", id.c_str(), DSTR(cmaxy-cminy));
+    if (shapeInfo.color.length()>0)
+        out("#declare %s_COLOR    = %s;\n",
+                id.c_str(), shapeInfo.color.c_str());
+    out("/*###################################################\n");
+    out("### end %s\n", id.c_str());
+    out("###################################################*/\n\n\n\n");
+
+    if (cminx < minx)
+        minx = cminx;
+    if (cmaxx > maxx)
+        maxx = cmaxx;
+    if (cminy < miny)
+        miny = cminy;
+    if (cmaxy > maxy)
+        maxy = cmaxy;
 
-           char *pfx = (char *)id.c_str();
+    return true;
+}
 
-        out("#declare %s_MIN_X    = %s;\n", pfx, dstr(cminx));
-        out("#declare %s_CENTER_X = %s;\n", pfx, dstr((cmaxx+cminx)/2.0));
-        out("#declare %s_MAX_X    = %s;\n", pfx, dstr(cmaxx));
-        out("#declare %s_WIDTH    = %s;\n", pfx, dstr(cmaxx-cminx));
-        out("#declare %s_MIN_Y    = %s;\n", pfx, dstr(cminy));
-        out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((cmaxy+cminy)/2.0));
-        out("#declare %s_MAX_Y    = %s;\n", pfx, dstr(cmaxy));
-        out("#declare %s_HEIGHT   = %s;\n", pfx, dstr(cmaxy-cminy));
-        if (shapeInfo.color.length()>0)
-            out("#declare %s_COLOR    = %s;\n",
-                    pfx, shapeInfo.color.c_str());
-        out("/*###################################################\n");
-        out("### end %s\n", id.c_str());
-        out("###################################################*/\n\n\n\n");
-        if (cminx < minx)
-            minx = cminx;
-        if (cmaxx > maxx)
-            maxx = cmaxx;
-        if (cminy < miny)
-            miny = cminy;
-        if (cmaxy > maxy)
-            maxy = cmaxy;
+/**
+ *  Descend the svg tree recursively, translating data
+ */
+bool PovOutput::doTreeRecursive(SPDocument *doc, SPObject *obj)
+{
 
-        }//for
+    String id;
+    if (!obj->getId())
+        {
+        char buf[16];
+        sprintf(buf, "id%d", idIndex++);
+        id = buf;
+        }
+    else
+        {
+            id = obj->getId();
+        }
 
+    if (SP_IS_ITEM(obj))
+        {
+        SPItem *item = SP_ITEM(obj);
+        if (!doCurve(item, id))
+            return false;
+        }
 
+    /**
+     * Descend into children
+     */
+    for (SPObject *child = obj->firstChild() ; child ; child = child->next)
+        {
+            if (!doTreeRecursive(doc, child))
+                return false;
+        }
+
+    return true;
+}
+
+/**
+ *  Output the curve data to buffer
+ */
+bool PovOutput::doTree(SPDocument *doc)
+{
+    double bignum = 1000000.0;
+    minx  =  bignum;
+    maxx  = -bignum;
+    miny  =  bignum;
+    maxy  = -bignum;
+
+    if (!doTreeRecursive(doc, doc->root))
+        return false;
 
     //## Let's make a union of all of the Shapes
     if (povShapes.size()>0)
@@ -529,7 +533,7 @@ void PovOutput::doCurves(SPDocument *doc)
         out(" * Allow the user to redefine the Z-Increment\n");
         out(" */\n");
         out("#ifndef (AllShapes_Z_Increment)\n");
-        out("#declare AllShapes_Z_Increment = %s;\n", dstr(zinc));
+        out("#declare AllShapes_Z_Increment = %s;\n", DSTR(zinc));
         out("#end\n");
         out("\n");
         out("#declare AllShapes_Z_Scale = 1.0;\n");
@@ -554,25 +558,24 @@ void PovOutput::doCurves(SPDocument *doc)
 
         out("}\n");
 
-        out("#declare %s_MIN_X    = %s;\n", pfx, dstr(minx));
-        out("#declare %s_CENTER_X = %s;\n", pfx, dstr((maxx+minx)/2.0));
-        out("#declare %s_MAX_X    = %s;\n", pfx, dstr(maxx));
-        out("#declare %s_WIDTH    = %s;\n", pfx, dstr(maxx-minx));
-        out("#declare %s_MIN_Y    = %s;\n", pfx, dstr(miny));
-        out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((maxy+miny)/2.0));
-        out("#declare %s_MAX_Y    = %s;\n", pfx, dstr(maxy));
-        out("#declare %s_HEIGHT   = %s;\n", pfx, dstr(maxy-miny));
+        out("#declare %s_MIN_X    = %s;\n", pfx, DSTR(minx));
+        out("#declare %s_CENTER_X = %s;\n", pfx, DSTR((maxx+minx)/2.0));
+        out("#declare %s_MAX_X    = %s;\n", pfx, DSTR(maxx));
+        out("#declare %s_WIDTH    = %s;\n", pfx, DSTR(maxx-minx));
+        out("#declare %s_MIN_Y    = %s;\n", pfx, DSTR(miny));
+        out("#declare %s_CENTER_Y = %s;\n", pfx, DSTR((maxy+miny)/2.0));
+        out("#declare %s_MAX_Y    = %s;\n", pfx, DSTR(maxy));
+        out("#declare %s_HEIGHT   = %s;\n", pfx, DSTR(maxy-miny));
         out("/*##############################################\n");
         out("### end %s\n", id.c_str());
         out("##############################################*/\n");
         out("\n\n");
         }
 
+    return true;
 }
 
 
-
-
 //########################################################################
 //# M A I N    O U T P U T
 //########################################################################
@@ -587,6 +590,7 @@ void PovOutput::reset()
     nrNodes    = 0;
     nrSegments = 0;
     nrShapes   = 0;
+    idIndex    = 0;
     outbuf.clear();
     povShapes.clear();
 }
@@ -594,30 +598,43 @@ void PovOutput::reset()
 
 
 /**
- * Saves the <paths> of an Inkscape SVG file as PovRay spline definitions
+ * Saves the Shapes of an Inkscape SVG file as PovRay spline definitions
  */
-void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
+void PovOutput::saveDocument(SPDocument *doc, gchar const *filename_utf8)
 {
     reset();
 
     //###### SAVE IN POV FORMAT TO BUFFER
     //# Lets do the curves first, to get the stats
-    doCurves(doc);
+    if (!doTree(doc))
+        {
+        err("Could not output curves for %s", filename_utf8);
+        return;
+        }
+
     String curveBuf = outbuf;
     outbuf.clear();
 
-    doHeader();
-    
+    if (!doHeader())
+        {
+        err("Could not write header for %s", filename_utf8);
+        return;
+        }
+
     outbuf.append(curveBuf);
-    
-    doTail();
+
+    if (!doTail())
+        {
+        err("Could not write footer for %s", filename_utf8);
+        return;
+        }
 
 
 
 
     //###### WRITE TO FILE
-    Inkscape::IO::dump_fopen_call(uri, "L");
-    FILE *f = Inkscape::IO::fopen_utf8name(uri, "w");
+    Inkscape::IO::dump_fopen_call(filename_utf8, "L");
+    FILE *f = Inkscape::IO::fopen_utf8name(filename_utf8, "w");
     if (!f)
         return;
 
@@ -626,7 +643,7 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
         int ch = *iter;
         fputc(ch, f);
         }
-        
+
     fclose(f);
 }
 
@@ -647,10 +664,11 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
  * API call to save document
 */
 void
-PovOutput::save(Inkscape::Extension::Output *mod,
-                        SPDocument *doc, gchar const *uri)
+PovOutput::save(Inkscape::Extension::Output */*mod*/,
+                        SPDocument *doc, gchar const *filename_utf8)
 {
-    saveDocument(doc, uri);
+    /* See comments in JavaFSOutput::save re the name `filename_utf8'. */
+    saveDocument(doc, filename_utf8);
 }
 
 
@@ -658,7 +676,7 @@ PovOutput::save(Inkscape::Extension::Output *mod,
 /**
  * Make sure that we are in the database
  */
-bool PovOutput::check (Inkscape::Extension::Extension *module)
+bool PovOutput::check (Inkscape::Extension::Extension */*module*/)
 {
     /* We don't need a Key
     if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
@@ -679,13 +697,13 @@ void
 PovOutput::init()
 {
     Inkscape::Extension::build_from_mem(
-        "<inkscape-extension>\n"
+        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
             "<name>" N_("PovRay Output") "</name>\n"
             "<id>org.inkscape.output.pov</id>\n"
             "<output>\n"
                 "<extension>.pov</extension>\n"
                 "<mimetype>text/x-povray-script</mimetype>\n"
-                "<filetypename>" N_("PovRay (*.pov) (export splines)") "</filetypename>\n"
+                "<filetypename>" N_("PovRay (*.pov) (paths and shapes only)") "</filetypename>\n"
                 "<filetypetooltip>" N_("PovRay Raytracer File") "</filetypetooltip>\n"
             "</output>\n"
         "</inkscape-extension>",
@@ -710,4 +728,4 @@ PovOutput::init()
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :