Code

Fix image transform when there is no rotate or scale
[inkscape.git] / src / extension / internal / pov-out.cpp
1 /*
2  * A simple utility for exporting Inkscape svg Shapes as PovRay bezier
3  * prisms.  Note that this is output-only, and would thus seem to be
4  * better placed as an 'export' rather than 'output'.  However, Export
5  * handles all or partial documents, while this outputs ALL shapes in
6  * the current SVG document.
7  *
8  *  For information on the PovRay file format, see:
9  *      http://www.povray.org
10  *
11  * Authors:
12  *   Bob Jamison <rjamison@titan.com>
13  *
14  * Copyright (C) 2004 Authors
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #include "pov-out.h"
24 #include "inkscape.h"
25 #include "sp-path.h"
26 #include <style.h>
27 #include "display/curve.h"
28 #include "libnr/n-art-bpath.h"
29 #include "extension/system.h"
33 #include "io/sys.h"
35 namespace Inkscape
36 {
37 namespace Extension
38 {
39 namespace Internal
40 {
44 static const char *
45 dstr(gchar *sbuffer, double d)
46 {
47     return (const char *)g_ascii_formatd(sbuffer,
48                  G_ASCII_DTOSTR_BUF_SIZE, "%.8g", (gdouble)d);
50 }
53 /**
54  * Make sure that we are in the database
55  */
56 bool
57 PovOutput::check (Inkscape::Extension::Extension *module)
58 {
59     /* We don't need a Key
60     if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
61         return FALSE;
62     */
64     return TRUE;
65 }
70 /**
71  * This function searches the Repr tree recursively from the given node,
72  * and adds refs to all nodes with the given name, to the result vector
73  */
74 static void
75 findElementsByTagName(std::vector<Inkscape::XML::Node *> &results,
76                       Inkscape::XML::Node *node,
77                       char const *name)
78 {
79     if ( !name
80          || strcmp(node->name(), name) == 0 ) {
81         results.push_back(node);
82     }
84     for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
85         findElementsByTagName( results, child, name );
87 }
90 /**
91  * used for saving information about shapes
92  */
93 class PovShapeInfo
94 {
95 public:
96     PovShapeInfo()
97     {}
98     virtual ~PovShapeInfo()
99     {}
100     std::string id;
101     std::string color;
102 };
106 static double
107 effective_opacity(SPItem const *item)
109     double ret = 1.0;
110     for (SPObject const *obj = item; obj; obj = obj->parent) {
111         SPStyle const *const style = SP_OBJECT_STYLE(obj);
112         g_return_val_if_fail(style, ret);
113         ret *= SP_SCALE24_TO_FLOAT(style->opacity.value);
114     }
115     return ret;
119 /**
120  * Saves the <paths> of an Inkscape SVG file as PovRay spline definitions
121 */
122 void
123 PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri)
125     std::vector<Inkscape::XML::Node *>results;
126     //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path");
127     findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL);//Check all nodes
128     if (results.size() == 0)
129         return;
130     Inkscape::IO::dump_fopen_call(uri, "L");
131     FILE *f = Inkscape::IO::fopen_utf8name(uri, "w");
132     if (!f)
133         return;
135     time_t tim = time(NULL);
136     fprintf(f, "/*#################################################\n");
137     fprintf(f, "### This PovRay document was generated by Inkscape\n");
138     fprintf(f, "### http://www.inkscape.org\n");
139     fprintf(f, "### Created: %s", ctime(&tim));
140     fprintf(f, "##################################################*/\n\n\n");
142     std::vector<PovShapeInfo>povShapes; //A list for saving information about the shapes
144     //we only need 8 for printf() calls that call dstr() 8 times
145     gchar s1[G_ASCII_DTOSTR_BUF_SIZE + 1];
146     gchar s2[G_ASCII_DTOSTR_BUF_SIZE + 1];
147     gchar s3[G_ASCII_DTOSTR_BUF_SIZE + 1];
148     gchar s4[G_ASCII_DTOSTR_BUF_SIZE + 1];
149     gchar s5[G_ASCII_DTOSTR_BUF_SIZE + 1];
150     gchar s6[G_ASCII_DTOSTR_BUF_SIZE + 1];
151     gchar s7[G_ASCII_DTOSTR_BUF_SIZE + 1];
152     gchar s8[G_ASCII_DTOSTR_BUF_SIZE + 1];
154     double bignum = 1000000.0;
155     double minx  =  bignum;
156     double maxx  = -bignum;
157     double miny  =  bignum;
158     double maxy  = -bignum;
162     unsigned indx;
163     for (indx = 0; indx < results.size() ; indx++)
164     {
165         //### Fetch the object from the repr info
166         Inkscape::XML::Node *rpath = results[indx];
167         gchar *id  = (gchar *)rpath->attribute("id");
168         SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath);
169         if (!reprobj)
170             continue;
172         //### Get the transform of the item
173         if (!SP_IS_ITEM(reprobj))
174         {
175             continue;
176         }
177         SPItem *item = SP_ITEM(reprobj);
178         NR::Matrix tf = sp_item_i2d_affine(item);
180         //### Get the Shape
181         if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion.  Allow all shapes
182         {
183             continue;
184         }
185         SPShape *shape = SP_SHAPE(reprobj);
186         SPCurve *curve = shape->curve;
187         if (sp_curve_empty(curve))
188             continue;
190         PovShapeInfo shapeInfo;
192         shapeInfo.id           = id;
193         shapeInfo.color        = "";
195         //Try to get the fill color of the shape
196         SPStyle *style = SP_OBJECT_STYLE(shape);
197         /* fixme: Handle other fill types, even if this means translating gradients to a single
198            flat colour. */
199         if (style && (style->fill.type == SP_PAINT_TYPE_COLOR)) {
200             // see color.h for how to parse SPColor
201             float rgb[3];
202             sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
203             double const dopacity = ( SP_SCALE24_TO_FLOAT(style->fill_opacity.value)
204                                       * effective_opacity(shape) );
205             //gchar *str = g_strdup_printf("rgbf < %1.3f, %1.3f, %1.3f %1.3f>",
206             //                             rgb[0], rgb[1], rgb[2], 1.0 - dopacity);
207             gchar *str = g_strdup_printf("rgbf < %s, %s, %s %s>",
208                    dstr(s1, rgb[0]), dstr(s2, rgb[1]), dstr(s3, rgb[2]), dstr(s4, 1.0 - dopacity));
210             shapeInfo.color += str;
211             g_free(str);
212         }
214         povShapes.push_back(shapeInfo); //passed all tests.  save the info
216         int curveNr;
218         //Count the NR_CURVETOs/LINETOs
219         int segmentCount=0;
220         NArtBpath *bp = SP_CURVE_BPATH(curve);
221         for (curveNr=0 ; curveNr<curve->length ; curveNr++, bp++)
222             if (bp->code == NR_CURVETO || bp->code == NR_LINETO)
223                 segmentCount++;
225         bp = SP_CURVE_BPATH(curve);
226         double cminx  =  bignum;
227         double cmaxx  = -bignum;
228         double cminy  =  bignum;
229         double cmaxy  = -bignum;
230         double lastx  = 0.0;
231         double lasty  = 0.0;
233         fprintf(f, "/*##############################################\n");
234         fprintf(f, "### PRISM:  %s\n", id);
235         fprintf(f, "##############################################*/\n");
236         fprintf(f, "#declare %s = prism {\n", id);
237         fprintf(f, "    linear_sweep\n");
238         fprintf(f, "    bezier_spline\n");
239         fprintf(f, "    1.0, //top\n");
240         fprintf(f, "    0.0, //bottom\n");
241         fprintf(f, "    %d, //nr points\n", segmentCount * 4);
242         int segmentNr = 0;
243         for (bp = SP_CURVE_BPATH(curve), curveNr=0 ; curveNr<curve->length ; curveNr++, bp++) {
244             using NR::X;
245             using NR::Y;
246             NR::Point const p1(bp->c(1) * tf);
247             NR::Point const p2(bp->c(2) * tf);
248             NR::Point const p3(bp->c(3) * tf);
249             double const x1 = p1[X], y1 = p1[Y];
250             double const x2 = p2[X], y2 = p2[Y];
251             double const x3 = p3[X], y3 = p3[Y];
253             switch (bp->code) {
254                 case NR_MOVETO:
255                 case NR_MOVETO_OPEN:
256                     //fprintf(f, "moveto: %f %f\n", bp->x3, bp->y3);
257                     break;
258                 case NR_CURVETO:
260                     //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
261                     //        segmentNr++, lastx, lasty, x1, y1, x2, y2, x3, y3);
262                     fprintf(f, "    /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>",
263                             segmentNr++,
264                             dstr(s1, lastx), dstr(s2, lasty),
265                             dstr(s3, x1),    dstr(s4, y1),
266                             dstr(s5, x2),    dstr(s6, y2),
267                             dstr(s7, x3),    dstr(s8, y3));
269                     if (segmentNr < segmentCount)
270                         fprintf(f, ",\n");
271                     else
272                         fprintf(f, "\n");
274                     if (lastx < cminx)
275                         cminx = lastx;
276                     if (lastx > cmaxx)
277                         cmaxx = lastx;
278                     if (lasty < cminy)
279                         cminy = lasty;
280                     if (lasty > cmaxy)
281                         cmaxy = lasty;
282                     break;
283                 case NR_LINETO:
285                     //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
286                     //        segmentNr++, lastx, lasty, lastx, lasty, x3, y3, x3, y3);
287                     fprintf(f, "    /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>",
288                             segmentNr++,
289                             dstr(s1, lastx),  dstr(s2, lasty),
290                             dstr(s3, lastx),  dstr(s4, lasty),
291                             dstr(s5, x3),     dstr(s6, y3),
292                             dstr(s7, x3),     dstr(s8, y3));
294                     if (segmentNr < segmentCount)
295                         fprintf(f, ",\n");
296                     else
297                         fprintf(f, "\n");
299                     //fprintf(f, "lineto\n");
300                     if (lastx < cminx)
301                         cminx = lastx;
302                     if (lastx > cmaxx)
303                         cmaxx = lastx;
304                     if (lasty < cminy)
305                         cminy = lasty;
306                     if (lasty > cmaxy)
307                         cmaxy = lasty;
308                     break;
309                 case NR_END:
310                     //fprintf(f, "end\n");
311                     break;
312             }
313             lastx = x3;
314             lasty = y3;
315         }
316         fprintf(f, "}\n");
317         /*
318         fprintf(f, "#declare %s_MIN_X    = %4.3f;\n", id, cminx);
319         fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (cmaxx+cminx)/2.0);
320         fprintf(f, "#declare %s_MAX_X    = %4.3f;\n", id, cmaxx);
321         fprintf(f, "#declare %s_WIDTH    = %4.3f;\n", id, cmaxx-cminx);
322         fprintf(f, "#declare %s_MIN_Y    = %4.3f;\n", id, cminy);
323         fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (cmaxy+cminy)/2.0);
324         fprintf(f, "#declare %s_MAX_Y    = %4.3f;\n", id, cmaxy);
325         fprintf(f, "#declare %s_HEIGHT   = %4.3f;\n", id, cmaxy-cminy);
326         */
327         fprintf(f, "#declare %s_MIN_X    = %s;\n", id, dstr(s1, cminx));
328         fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (cmaxx+cminx)/2.0));
329         fprintf(f, "#declare %s_MAX_X    = %s;\n", id, dstr(s1, cmaxx));
330         fprintf(f, "#declare %s_WIDTH    = %s;\n", id, dstr(s1, cmaxx-cminx));
331         fprintf(f, "#declare %s_MIN_Y    = %s;\n", id, dstr(s1, cminy));
332         fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (cmaxy+cminy)/2.0));
333         fprintf(f, "#declare %s_MAX_Y    = %s;\n", id, dstr(s1, cmaxy));
334         fprintf(f, "#declare %s_HEIGHT   = %s;\n", id, dstr(s1, cmaxy-cminy));
335         if (shapeInfo.color.length()>0)
336             fprintf(f, "#declare %s_COLOR    = %s;\n",
337                     id, shapeInfo.color.c_str());
338         fprintf(f, "/*##############################################\n");
339         fprintf(f, "### end %s\n", id);
340         fprintf(f, "##############################################*/\n\n\n\n");
341         if (cminx < minx)
342             minx = cminx;
343         if (cmaxx > maxx)
344             maxx = cmaxx;
345         if (cminy < miny)
346             miny = cminy;
347         if (cmaxy > maxy)
348             maxy = cmaxy;
351     }//for
355     //## Let's make a union of all of the Shapes
356     if (!povShapes.empty()) {
357         char const *id = "AllShapes";
358         fprintf(f, "/*##############################################\n");
359         fprintf(f, "### UNION OF ALL SHAPES IN DOCUMENT\n");
360         fprintf(f, "##############################################*/\n");
361         fprintf(f, "\n\n");
362         fprintf(f, "/**\n");
363         fprintf(f, " * Allow the user to redefine the finish{}\n");
364         fprintf(f, " * by declaring it before #including this file\n");
365         fprintf(f, " */\n");
366         fprintf(f, "#ifndef (%s_Finish)\n", id);
367         fprintf(f, "#declare %s_Finish = finish {\n", id);
368         fprintf(f, "    phong 0.5\n");
369         fprintf(f, "    reflection 0.3\n");
370         fprintf(f, "    specular 0.5\n");
371         fprintf(f, "}\n");
372         fprintf(f, "#end\n");
373         fprintf(f, "\n\n");
374         fprintf(f, "#declare %s = union {\n", id);
375         for (unsigned i = 0 ; i < povShapes.size() ; i++) {
376             fprintf(f, "    object { %s\n", povShapes[i].id.c_str());
377             fprintf(f, "        texture { \n");
378             if (povShapes[i].color.length()>0)
379                 fprintf(f, "            pigment { %s }\n", povShapes[i].color.c_str());
380             else
381                 fprintf(f, "            pigment { rgb <0,0,0> }\n");
382             fprintf(f, "            finish { %s_Finish }\n", id);
383             fprintf(f, "            } \n");
384             fprintf(f, "        } \n");
385         }
386         fprintf(f, "}\n\n\n\n");
389         double zinc   = 0.2 / (double)povShapes.size();
390         fprintf(f, "/*#### Same union, but with Z-diffs (actually Y in pov) ####*/\n");
391         fprintf(f, "\n\n");
392         fprintf(f, "/**\n");
393         fprintf(f, " * Allow the user to redefine the Z-Increment\n");
394         fprintf(f, " */\n");
395         fprintf(f, "#ifndef (AllShapes_Z_Increment)\n");
396         fprintf(f, "#declare AllShapes_Z_Increment = %s;\n", dstr(s1, zinc));
397         fprintf(f, "#end\n");
398         fprintf(f, "\n");
399         fprintf(f, "#declare AllShapes_Z_Scale = 1.0;\n");
400         fprintf(f, "\n\n");
401         fprintf(f, "#declare %s_Z = union {\n", id);
402         for (unsigned i = 0 ; i < povShapes.size() ; i++) {
403             fprintf(f, "    object { %s\n", povShapes[i].id.c_str());
404             fprintf(f, "        texture { \n");
405             if (povShapes[i].color.length()>0)
406                 fprintf(f, "            pigment { %s }\n", povShapes[i].color.c_str());
407             else
408                 fprintf(f, "            pigment { rgb <0,0,0> }\n");
409             fprintf(f, "            finish { %s_Finish }\n", id);
410             fprintf(f, "            } \n");
411             fprintf(f, "        scale <1, %s_Z_Scale, 1>\n", id);
412             fprintf(f, "        } \n");
413             fprintf(f, "#declare %s_Z_Scale = %s_Z_Scale + %s_Z_Increment;\n\n",
414                     id, id, id);
415         }
417         fprintf(f, "}\n");
418         /*
419         fprintf(f, "#declare %s_MIN_X    = %4.3f;\n", id, minx);
420         fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (maxx+minx)/2.0);
421         fprintf(f, "#declare %s_MAX_X    = %4.3f;\n", id, maxx);
422         fprintf(f, "#declare %s_WIDTH    = %4.3f;\n", id, maxx-minx);
423         fprintf(f, "#declare %s_MIN_Y    = %4.3f;\n", id, miny);
424         fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (maxy+miny)/2.0);
425         fprintf(f, "#declare %s_MAX_Y    = %4.3f;\n", id, maxy);
426         fprintf(f, "#declare %s_HEIGHT   = %4.3f;\n", id, maxy-miny);
427         */
428         fprintf(f, "#declare %s_MIN_X    = %s;\n", id, dstr(s1, minx));
429         fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (maxx+minx)/2.0));
430         fprintf(f, "#declare %s_MAX_X    = %s;\n", id, dstr(s1, maxx));
431         fprintf(f, "#declare %s_WIDTH    = %s;\n", id, dstr(s1, maxx-minx));
432         fprintf(f, "#declare %s_MIN_Y    = %s;\n", id, dstr(s1, miny));
433         fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (maxy+miny)/2.0));
434         fprintf(f, "#declare %s_MAX_Y    = %s;\n", id, dstr(s1, maxy));
435         fprintf(f, "#declare %s_HEIGHT   = %s;\n", id, dstr(s1, maxy-miny));
436         fprintf(f, "/*##############################################\n");
437         fprintf(f, "### end %s\n", id);
438         fprintf(f, "##############################################*/\n\n\n\n");
439     }
441     //All done
442     fclose(f);
445 #include "clear-n_.h"
447 /**
448  * This is the definition of PovRay output.  This function just
449  * calls the extension system with the memory allocated XML that
450  * describes the data.
451 */
452 void
453 PovOutput::init()
455     Inkscape::Extension::build_from_mem(
456         "<inkscape-extension>\n"
457             "<name>" N_("PovRay Output") "</name>\n"
458             "<id>org.inkscape.output.pov</id>\n"
459             "<output>\n"
460                 "<extension>.pov</extension>\n"
461                 "<mimetype>text/x-povray-script</mimetype>\n"
462                 "<filetypename>" N_("PovRay (*.pov) (export splines)") "</filetypename>\n"
463                 "<filetypetooltip>" N_("PovRay Raytracer File") "</filetypetooltip>\n"
464             "</output>\n"
465         "</inkscape-extension>",
466         new PovOutput());
473 }  //namespace Internal
474 }  //namespace Extension
475 }  //namespace Inkscape
478 /*
479   Local Variables:
480   mode:c++
481   c-file-style:"stroustrup"
482   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
483   indent-tabs-mode:nil
484   fill-column:99
485   End:
486 */
487 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :