Code

a couple of trivial %d -> %u changes.
[inkscape.git] / src / extension / internal / pov-out.cpp
index c915de75bfb3dbf34dcb8c65acb24c0070238d03..6dd62206c20402ce101af71d7315ff91637ced89 100644 (file)
@@ -9,9 +9,9 @@
  *      http://www.povray.org
  *
  * Authors:
- *   Bob Jamison <ishmalius@gmail.com>
+ *   Bob Jamison <ishmal@inkscape.org>
  *
- * Copyright (C) 2004-2007 Authors
+ * Copyright (C) 2004-2008 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -94,37 +94,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;
 }
 
 
 
 
-
-
 /**
  *  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 +127,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).c_str(), dstr(b).c_str());
 }
 
 
@@ -155,13 +142,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).c_str(), dstr(b).c_str(), dstr(c).c_str());
 }
 
 
@@ -171,25 +152,19 @@ 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).c_str(), dstr(b).c_str(),
+                dstr(c).c_str(), dstr(d).c_str());
 }
 
 
+
 /**
  *  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 +173,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);
 }
 
 
@@ -244,6 +218,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");
@@ -315,7 +294,7 @@ void PovOutput::doCurves(SPDocument *doc)
 
         SPShape *shape = SP_SHAPE(reprobj);
         SPCurve *curve = shape->curve;
-        if (sp_curve_empty(curve))
+        if (curve->is_empty())
             continue;
             
         nrShapes++;
@@ -351,7 +330,7 @@ void PovOutput::doCurves(SPDocument *doc)
 
         //Count the NR_CURVETOs/LINETOs
         int segmentCount=0;
-        NArtBpath *bp = SP_CURVE_BPATH(curve);
+        NArtBpath const *bp = SP_CURVE_BPATH(curve);
         for (int curveNr=0 ; curveNr<curveLength ; curveNr++, bp++)
             if (bp->code == NR_CURVETO || bp->code == NR_LINETO)
                 segmentCount++;
@@ -381,6 +360,7 @@ void PovOutput::doCurves(SPDocument *doc)
             {
             using NR::X;
             using NR::Y;
+            //transform points.  note overloaded '*'
             NR::Point const p1(bp->c(1) * tf);
             NR::Point const p2(bp->c(2) * tf);
             NR::Point const p3(bp->c(3) * tf);
@@ -401,7 +381,7 @@ void PovOutput::doCurves(SPDocument *doc)
                     //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);
+                            lastx, lasty, x1, y1, x2, y2, x3, y3);
                     nrNodes += 8;
 
                     if (segmentNr < segmentCount)
@@ -421,10 +401,12 @@ void PovOutput::doCurves(SPDocument *doc)
                     }
                 case NR_LINETO:
                     {
+                    //NOTE: we need to carefully handle line->curve and curve->line
+                    //    transitions.
                     //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);
+                            lastx, lasty, lastx, lasty, x3, y3, x3, y3);
                     nrNodes += 8;
 
                     if (segmentNr < segmentCount)
@@ -456,16 +438,17 @@ void PovOutput::doCurves(SPDocument *doc)
         out("}\n");
 
 
+        //# prefix for following declarations
            char *pfx = (char *)id.c_str();
 
-        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));
+        out("#declare %s_MIN_X    = %s;\n", pfx, dstr(cminx).c_str());
+        out("#declare %s_CENTER_X = %s;\n", pfx, dstr((cmaxx+cminx)/2.0).c_str());
+        out("#declare %s_MAX_X    = %s;\n", pfx, dstr(cmaxx).c_str());
+        out("#declare %s_WIDTH    = %s;\n", pfx, dstr(cmaxx-cminx).c_str());
+        out("#declare %s_MIN_Y    = %s;\n", pfx, dstr(cminy).c_str());
+        out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((cmaxy+cminy)/2.0).c_str());
+        out("#declare %s_MAX_Y    = %s;\n", pfx, dstr(cmaxy).c_str());
+        out("#declare %s_HEIGHT   = %s;\n", pfx, dstr(cmaxy-cminy).c_str());
         if (shapeInfo.color.length()>0)
             out("#declare %s_COLOR    = %s;\n",
                     pfx, shapeInfo.color.c_str());
@@ -529,7 +512,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).c_str());
         out("#end\n");
         out("\n");
         out("#declare AllShapes_Z_Scale = 1.0;\n");
@@ -554,14 +537,14 @@ 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).c_str());
+        out("#declare %s_CENTER_X = %s;\n", pfx, dstr((maxx+minx)/2.0).c_str());
+        out("#declare %s_MAX_X    = %s;\n", pfx, dstr(maxx).c_str());
+        out("#declare %s_WIDTH    = %s;\n", pfx, dstr(maxx-minx).c_str());
+        out("#declare %s_MIN_Y    = %s;\n", pfx, dstr(miny).c_str());
+        out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((maxy+miny)/2.0).c_str());
+        out("#declare %s_MAX_Y    = %s;\n", pfx, dstr(maxy).c_str());
+        out("#declare %s_HEIGHT   = %s;\n", pfx, dstr(maxy-miny).c_str());
         out("/*##############################################\n");
         out("### end %s\n", id.c_str());
         out("##############################################*/\n");
@@ -679,7 +662,7 @@ 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"