Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / synfig / savecanvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file savecanvas.cpp
3 **      \brief Writeme
4 **
5 **      $Id: savecanvas.cpp,v 1.2 2005/01/07 03:29:12 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #ifdef HAVE_SYS_ERRNO_H
33 #       include <sys/errno.h>
34 #endif
35
36 #include "savecanvas.h"
37 #include "general.h"
38 #include "valuenode.h"
39 #include "valuenode_subtract.h"
40 #include "valuenode_animated.h"
41 #include "valuenode_composite.h"
42 #include "valuenode_const.h"
43 #include "valuenode_linear.h"
44 #include "valuenode_dynamiclist.h"
45 #include "valuenode_reference.h"
46 #include "valuenode_segcalctangent.h"
47 #include "valuenode_segcalcvertex.h"
48 #include "valuenode_bline.h"
49 #include "time.h"
50 #include "keyframe.h"
51 #include "layer.h"
52 #include "string.h"
53 #include "paramdesc.h"
54
55 #include <libxml++/libxml++.h>
56 #include <ETL/stringf>
57 #include "gradient.h"
58
59 #endif
60
61 /* === U S I N G =========================================================== */
62
63 using namespace std;
64 using namespace etl;
65 using namespace synfig;
66
67 /* === M A C R O S ========================================================= */
68
69 #define COLOR_VALUE_TYPE_FORMAT         "%f"
70 #define VECTOR_VALUE_TYPE_FORMAT        "%0.10f"
71 #define TIME_TYPE_FORMAT                        "%0.3f"
72 #define VIEW_BOX_FORMAT                         "%f %f %f %f"
73
74 /* === G L O B A L S ======================================================= */
75
76 /* === P R O C E D U R E S ================================================= */
77
78 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas);
79 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas);
80
81 xmlpp::Element* encode_keyframe(xmlpp::Element* root,const Keyframe &kf, float fps)
82 {
83         root->set_name("keyframe");
84         root->set_attribute("time",kf.get_time().get_string(fps));
85         if(!kf.get_description().empty())
86                 root->set_child_text(kf.get_description());
87         return root;
88 }
89
90
91 xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
92 {
93         root->set_name("real");
94         root->set_attribute("value",strprintf(VECTOR_VALUE_TYPE_FORMAT,v));
95         return root;
96 }
97
98 xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float fps=0)
99 {
100         root->set_name("time");
101         //root->set_attribute("value",t.get_string(fps));
102         root->set_attribute("value",t.get_string());
103         return root;
104 }
105
106 xmlpp::Element* encode_integer(xmlpp::Element* root,int i)
107 {
108         root->set_name("integer");
109         root->set_attribute("value",strprintf("%i",i));
110         return root;
111 }
112
113 xmlpp::Element* encode_bool(xmlpp::Element* root,bool b)
114 {
115         root->set_name("bool");
116         root->set_attribute("value",b?"true":"false");
117         return root;
118 }
119
120 xmlpp::Element* encode_string(xmlpp::Element* root,const String &str)
121 {
122         root->set_name("string");
123         root->set_child_text(str);
124         return root;
125 }
126
127 xmlpp::Element* encode_vector(xmlpp::Element* root,Vector vect)
128 {
129         root->set_name("vector");
130         root->add_child("x")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[0]));
131         root->add_child("y")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[1]));
132         return root;
133 }
134
135 xmlpp::Element* encode_color(xmlpp::Element* root,Color color)
136 {
137         root->set_name("color");
138         root->add_child("r")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_r()));
139         root->add_child("g")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_g()));
140         root->add_child("b")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_b()));
141         root->add_child("a")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_a()));
142         return root;
143 }
144
145 xmlpp::Element* encode_angle(xmlpp::Element* root,Angle theta)
146 {
147         root->set_name("angle");
148         root->set_attribute("value",strprintf("%f",(float)Angle::deg(theta).get()));
149         return root;
150 }
151
152 xmlpp::Element* encode_segment(xmlpp::Element* root,Segment seg)
153 {
154         root->set_name("segment");
155         encode_vector(root->add_child("p1")->add_child("vector"),seg.p1);
156         encode_vector(root->add_child("t1")->add_child("vector"),seg.t1);
157         encode_vector(root->add_child("p2")->add_child("vector"),seg.p2);
158         encode_vector(root->add_child("t2")->add_child("vector"),seg.t2);
159         return root;
160 }
161
162 xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
163 {
164         root->set_name(ValueBase::type_name(ValueBase::TYPE_BLINEPOINT));
165
166         encode_vector(root->add_child("vertex")->add_child("vector"),bline_point.get_vertex());
167         encode_vector(root->add_child("t1")->add_child("vector"),bline_point.get_tangent1());
168
169         if(bline_point.get_split_tangent_flag())
170                 encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
171         
172         encode_real(root->add_child("width")->add_child("real"),bline_point.get_width());
173         encode_real(root->add_child("origin")->add_child("real"),bline_point.get_origin());
174         return root;
175 }
176
177 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
178 {
179         root->set_name("gradient");
180         
181         Gradient::const_iterator iter;
182         x.sort();
183         for(iter=x.begin();iter!=x.end();iter++)
184         {
185                 xmlpp::Element *cpoint(encode_color(root->add_child("color"),iter->color));
186                 cpoint->set_attribute("pos",strprintf("%f",iter->pos));
187         }
188         return root;
189 }
190
191
192 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas=0);
193
194 xmlpp::Element* encode_list(xmlpp::Element* root,std::list<ValueBase> list, Canvas::ConstHandle canvas=0)
195 {
196         root->set_name("list");
197         
198         while(!list.empty())
199         {
200                 encode_value(root->add_child("value"),list.front(),canvas);
201                 list.pop_front();
202         }
203
204         return root;
205 }
206
207 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas)
208 {
209         switch(data.get_type())
210         {
211         case ValueBase::TYPE_REAL:
212                 return encode_real(root,data.get(Real()));
213         case ValueBase::TYPE_TIME:
214                 if(canvas)
215                         return encode_time(root,data.get(Time()),canvas->rend_desc().get_frame_rate());
216                 else
217                         return encode_time(root,data.get(Time()));
218         case ValueBase::TYPE_INTEGER:
219                 return encode_integer(root,data.get(int()));
220         case ValueBase::TYPE_COLOR:
221                 return encode_color(root,data.get(Color()));
222         case ValueBase::TYPE_VECTOR:
223                 return encode_vector(root,data.get(Vector()));
224         case ValueBase::TYPE_ANGLE:
225                 return encode_angle(root,data.get(Angle()));
226         case ValueBase::TYPE_BOOL:
227                 return encode_bool(root,data.get(bool()));
228         case ValueBase::TYPE_STRING:
229                 return encode_string(root,data.get(String()));
230         case ValueBase::TYPE_SEGMENT:
231                 return encode_segment(root,data.get(Segment()));
232         case ValueBase::TYPE_BLINEPOINT:
233                 return encode_bline_point(root,data.get(BLinePoint()));
234         case ValueBase::TYPE_GRADIENT:
235                 return encode_gradient(root,data.get(Gradient()));
236         case ValueBase::TYPE_LIST:
237                 return encode_list(root,data,canvas);
238         case ValueBase::TYPE_CANVAS:
239                 return encode_canvas(root,data.get(Canvas::Handle()).get());
240         case ValueBase::TYPE_NIL:
241                 synfig::error("Encountered NIL ValueBase");
242                 root->set_name("nil");
243                 return root;
244         default:
245                 synfig::error(strprintf("Unknown value(%s), cannot create XML representation!",ValueBase::type_name(data.get_type()).c_str()));
246                 root->set_name("nil");
247                 return root;
248         }
249 }
250
251 xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)
252 {
253         assert(value_node);
254         root->set_name("animated");
255
256         root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
257
258         const ValueNode_Animated::WaypointList &waypoint_list=value_node->waypoint_list();
259         ValueNode_Animated::WaypointList::const_iterator iter;
260
261         for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
262         {
263                 xmlpp::Element *waypoint_node=root->add_child("waypoint");
264                 //waypoint_node->set_attribute("time",iter->get_time().get_string(canvas->rend_desc().get_frame_rate()));
265                 waypoint_node->set_attribute("time",iter->get_time().get_string());
266
267                 //waypoint_node->add_child(encode_value(iter->get_value(),canvas));
268                 if(iter->get_value_node()->is_exported())
269                         waypoint_node->set_attribute("use",iter->get_value_node()->get_relative_id(canvas));
270                 else
271                         encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
272                                         
273                 switch(iter->get_before())
274                 {
275                 case INTERPOLATION_HALT:
276                         waypoint_node->set_attribute("before","halt");
277                         break;
278                 case INTERPOLATION_LINEAR:
279                         waypoint_node->set_attribute("before","linear");
280                         break;
281                 case INTERPOLATION_MANUAL:
282                         waypoint_node->set_attribute("before","manual");
283                         break;
284                 case INTERPOLATION_CONSTANT:
285                         waypoint_node->set_attribute("before","constant");
286                         break;
287                 case INTERPOLATION_TCB:
288                         // This is the default value, so don't add a new attribute
289                         break;
290                 default:
291                         error("Unknown waypoint type for \"before\" attribute");
292                 }
293
294                 switch(iter->get_after())
295                 {
296                 case INTERPOLATION_HALT:
297                         waypoint_node->set_attribute("after","halt");
298                         break;
299                 case INTERPOLATION_LINEAR:
300                         waypoint_node->set_attribute("after","linear");
301                         break;
302                 case INTERPOLATION_MANUAL:
303                         waypoint_node->set_attribute("after","manual");
304                         break;
305                 case INTERPOLATION_CONSTANT:
306                         waypoint_node->set_attribute("after","constant");
307                         break;
308                 case INTERPOLATION_TCB:
309                         // This is the default value, so don't add a new attribute
310                         break;
311                 default:
312                         error("Unknown waypoint type for \"before\" attribute");
313                 }
314
315                 if(iter->get_tension()!=0.0)
316                         waypoint_node->set_attribute("tension",strprintf("%f",iter->get_tension()));
317                 if(iter->get_time_tension()!=0.0)
318                         waypoint_node->set_attribute("temporal-tension",strprintf("%f",iter->get_time_tension()));
319                 if(iter->get_continuity()!=0.0)
320                         waypoint_node->set_attribute("continuity",strprintf("%f",iter->get_continuity()));
321                 if(iter->get_bias()!=0.0)
322                         waypoint_node->set_attribute("bias",strprintf("%f",iter->get_bias()));
323
324         }
325
326         return root;
327 }
328
329 xmlpp::Element* encode_composite(xmlpp::Element* root,ValueNode_Composite::ConstHandle value_node,Canvas::ConstHandle canvas=0)
330 {
331         assert(value_node);
332         root->set_name("composite");
333
334         root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
335
336         int i;
337         for(i=0;i<value_node->link_count();i++)
338         {
339                 string name(strprintf("c%d",i+1));
340                 assert(value_node->get_link(i));
341                 if(value_node->get_link(i)->is_exported())
342                         root->set_attribute(name,value_node->get_link(i)->get_relative_id(canvas));
343                 else
344                         encode_value_node(root->add_child(name)->add_child("value_node"),value_node->get_link(i).constant(),canvas);
345         }
346
347         return root;
348 }
349
350 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
351 {
352         assert(value_node);
353         root->set_name("subtract");
354
355         ValueNode::ConstHandle lhs=value_node->get_lhs();
356         ValueNode::ConstHandle rhs=value_node->get_rhs();
357         ValueNode::ConstHandle scalar=value_node->get_scalar();
358
359         assert(lhs);
360         assert(rhs);
361
362         root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
363
364         if(lhs==rhs)
365                 warning("LHS is equal to RHS, this <subtract> will always be zero!");
366
367         //if(value_node->get_scalar()!=1)
368         //      root->set_attribute("scalar",strprintf(VECTOR_VALUE_TYPE_FORMAT,value_node->get_scalar()));
369
370         if(!scalar->get_id().empty())
371                 root->set_attribute("scalar",scalar->get_relative_id(canvas));
372         else
373                 encode_value_node(root->add_child("scalar")->add_child("value_node"),scalar,canvas);
374
375         if(!lhs->get_id().empty())
376                 root->set_attribute("lhs",lhs->get_relative_id(canvas));
377         else
378                 encode_value_node(root->add_child("lhs")->add_child("value_node"),lhs,canvas);
379
380         if(!rhs->get_id().empty())
381                 root->set_attribute("rhs",rhs->get_relative_id(canvas));
382         else
383                 encode_value_node(root->add_child("rhs")->add_child("value_node"),rhs,canvas);
384
385         return root;
386 }
387
388 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
389 {
390         assert(value_node);
391 //      const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
392         const float fps(0);
393         
394         root->set_name(value_node->get_name());
395
396         root->set_attribute("type",ValueBase::type_name(value_node->get_contained_type()));
397
398         vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
399
400         ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
401         
402         if(bline_value_node)
403         {
404                 if(bline_value_node->get_loop())
405                         root->set_attribute("loop","true");
406                 else
407                         root->set_attribute("loop","false");
408         }
409         
410         for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
411         {
412                 xmlpp::Element  *entry_node=root->add_child("entry");
413                 assert(iter->value_node);
414                 if(!iter->value_node->get_id().empty())
415                         entry_node->set_attribute("use",iter->value_node->get_relative_id(canvas));
416                 else
417                         encode_value_node(entry_node->add_child("value_node"),iter->value_node,canvas);
418
419                 // process waypoints
420                 {
421                         typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
422                         typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
423                         String begin_sequence;
424                         String end_sequence;
425
426                         const ActivepointList& timing_info(iter->timing_info);
427                         ActivepointList::const_iterator entry_iter;
428                         
429                         for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
430                                 if(entry_iter->state==true)
431                                 {
432                                         if(entry_iter->priority)
433                                                 begin_sequence+=strprintf("p%d ",entry_iter->priority);
434                                         begin_sequence+=entry_iter->time.get_string(fps)+", ";
435                                 }       
436                                 else
437                                 {
438                                         if(entry_iter->priority)
439                                                 end_sequence+=strprintf("p%d ",entry_iter->priority);
440                                         end_sequence+=entry_iter->time.get_string(fps)+", ";
441                                 }
442                         
443                         // If this is just a plane-jane vanilla entry,
444                         // then don't bother with begins and ends
445                         if(end_sequence.empty() && begin_sequence=="SOT, ")
446                                 begin_sequence.clear();
447                         
448                         if(!begin_sequence.empty())
449                         {
450                                 // Remove the last ", " stuff
451                                 begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
452                                 // Add the attribute
453                                 entry_node->set_attribute("on",begin_sequence);                         
454                         }                               
455
456                         if(!end_sequence.empty())
457                         {
458                                 // Remove the last ", " stuff
459                                 end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
460                                 // Add the attribute
461                                 entry_node->set_attribute("off",end_sequence);                          
462                         }                               
463                 }
464         }
465
466         return root;
467 }
468
469 // Generic linkable data node entry
470 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
471 {
472         assert(value_node);
473         root->set_name(value_node->get_name());
474
475         root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
476
477         int i;
478         for(i=0;i<value_node->link_count();i++)
479         {
480                 ValueNode::ConstHandle link=value_node->get_link(i).constant();
481                 if(!link)
482                         throw runtime_error("Bad link");
483                 if(link->is_exported())
484                         root->set_attribute(value_node->link_name(i),link->get_relative_id(canvas));
485                 else
486                         encode_value_node(root->add_child(value_node->link_name(i))->add_child("value_node"),link,canvas);
487         }
488
489         return root;
490 }
491
492 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
493 {
494         assert(value_node);
495
496         if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
497                 encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
498         else
499         if(ValueNode_Composite::ConstHandle::cast_dynamic(value_node))
500                 encode_composite(root,ValueNode_Composite::ConstHandle::cast_dynamic(value_node),canvas);
501         else
502         if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
503                 encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
504         else
505         if(ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node))
506                 encode_dynamic_list(root,ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node),canvas);
507         else if(ValueNode_Const::ConstHandle::cast_dynamic(value_node))
508         {
509                 encode_value(root,ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value(),canvas);
510         }
511         else
512         if(LinkableValueNode::ConstHandle::cast_dynamic(value_node))
513                 encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
514         else
515         {
516                 error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
517                 root->set_name("nil");
518         }
519
520         assert(root);
521
522         if(!value_node->get_id().empty())
523                 root->set_attribute("id",value_node->get_id());
524
525         if(value_node->rcount()>1)
526                 root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
527
528         return root;
529 }
530
531 xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
532 {
533         root->set_name("layer");
534
535         root->set_attribute("type",layer->get_name());
536         root->set_attribute("active",layer->active()?"true":"false");
537
538         if(!layer->get_version().empty())
539                 root->set_attribute("version",layer->get_version());
540         if(!layer->get_description().empty())
541                 root->set_attribute("desc",layer->get_description());
542         if(!layer->get_group().empty())
543                 root->set_attribute("group",layer->get_group());
544
545         Layer::Vocab vocab(layer->get_param_vocab());
546         Layer::Vocab::const_iterator iter;
547
548         const Layer::DynamicParamList &dynamic_param_list=layer->dynamic_param_list();
549
550         for(iter=vocab.begin();iter!=vocab.end();++iter)
551         {
552                 // Handle dynamic parameters
553                 if(dynamic_param_list.count(iter->get_name()))
554                 {
555                         xmlpp::Element *node=root->add_child("param");
556                         node->set_attribute("name",iter->get_name());
557
558                         handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
559
560                         // If the valuenode.has no ID, then it must be defined in-place
561                         if(value_node->get_id().empty())
562                         {
563                                 encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
564                         }
565                         else
566                         {
567                                 node->set_attribute("use",value_node->get_relative_id(layer->get_canvas()));
568                         }
569                 }
570                 else  // Handle normal parameters
571                 if(iter->get_critical())
572                 {
573                         ValueBase value=layer->get_param(iter->get_name());
574                         if(!value.is_valid())
575                         {
576                                 error("Layer doesn't know it's own vocabulary -- "+iter->get_name());
577                                 continue;
578                         }
579
580                         if(value.get_type()==ValueBase::TYPE_CANVAS && !value.get(Canvas::LooseHandle())->is_inline())
581                         {
582                                 Canvas::Handle child(value.get(Canvas::LooseHandle()));
583                                 
584                                 if(!value.get(Canvas::Handle()))
585                                         continue;
586                                 xmlpp::Element *node=root->add_child("param");
587                                 node->set_attribute("name",iter->get_name());
588                                 node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
589                                 continue;
590                         }
591                         xmlpp::Element *node=root->add_child("param");
592                         node->set_attribute("name",iter->get_name());
593
594                         encode_value(node->add_child("value"),value,layer->get_canvas().constant());
595                 }               
596         }
597
598
599         return root;
600 }
601
602 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
603 {
604         assert(canvas);
605         const RendDesc &rend_desc=canvas->rend_desc();
606         root->set_name("canvas");
607         
608         if(canvas->is_root())
609                 root->set_attribute("version","0.1");
610
611         if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
612                 root->set_attribute("id",canvas->get_id());
613
614         if(!canvas->parent() || canvas->parent()->rend_desc().get_w()!=canvas->rend_desc().get_w())
615                 root->set_attribute("width",strprintf("%d",rend_desc.get_w()));
616
617         if(!canvas->parent() || canvas->parent()->rend_desc().get_h()!=canvas->rend_desc().get_h())
618                 root->set_attribute("height",strprintf("%d",rend_desc.get_h()));
619
620         if(!canvas->parent() || canvas->parent()->rend_desc().get_x_res()!=canvas->rend_desc().get_x_res())
621                 root->set_attribute("xres",strprintf("%f",rend_desc.get_x_res()));
622
623         if(!canvas->parent() || canvas->parent()->rend_desc().get_y_res()!=canvas->rend_desc().get_y_res())
624                 root->set_attribute("yres",strprintf("%f",rend_desc.get_y_res()));
625
626
627         if(!canvas->parent() ||
628                 canvas->parent()->rend_desc().get_tl()!=canvas->rend_desc().get_tl() ||
629                 canvas->parent()->rend_desc().get_br()!=canvas->rend_desc().get_br())
630         root->set_attribute("view-box",strprintf(VIEW_BOX_FORMAT,
631                 rend_desc.get_tl()[0],
632                 rend_desc.get_tl()[1],
633                 rend_desc.get_br()[0],
634                 rend_desc.get_br()[1])
635         );
636
637         if(!canvas->parent() || canvas->parent()->rend_desc().get_antialias()!=canvas->rend_desc().get_antialias())
638                 root->set_attribute("antialias",strprintf("%d",rend_desc.get_antialias()));
639
640         if(!canvas->parent())
641                 root->set_attribute("fps",strprintf(TIME_TYPE_FORMAT,rend_desc.get_frame_rate()));
642
643         if(!canvas->parent() || canvas->parent()->rend_desc().get_time_start()!=canvas->rend_desc().get_time_start())
644                 root->set_attribute("begin-time",rend_desc.get_time_start().get_string(rend_desc.get_frame_rate()));
645
646         if(!canvas->parent() || canvas->parent()->rend_desc().get_time_end()!=canvas->rend_desc().get_time_end())
647                 root->set_attribute("end-time",rend_desc.get_time_end().get_string(rend_desc.get_frame_rate()));
648         
649         if(!canvas->is_inline())
650         {
651                 root->set_attribute("bgcolor",strprintf(VIEW_BOX_FORMAT,
652                         rend_desc.get_bg_color().get_r(),
653                         rend_desc.get_bg_color().get_g(),
654                         rend_desc.get_bg_color().get_b(),
655                         rend_desc.get_bg_color().get_a())
656                 );
657
658                 if(!canvas->get_name().empty())
659                         root->add_child("name")->set_child_text(canvas->get_name());
660                 if(!canvas->get_description().empty())
661                         root->add_child("desc")->set_child_text(canvas->get_description());
662                 if(!canvas->get_author().empty())
663                         root->add_child("author")->set_child_text(canvas->get_description());
664
665                 std::list<String> meta_keys(canvas->get_meta_data_keys());
666                 while(!meta_keys.empty())
667                 {
668                         xmlpp::Element* meta_element(root->add_child("meta"));
669                         meta_element->set_attribute("name",meta_keys.front());
670                         meta_element->set_attribute("content",canvas->get_meta_data(meta_keys.front()));
671                         meta_keys.pop_front();
672                 }
673                 for(KeyframeList::const_iterator iter=canvas->keyframe_list().begin();iter!=canvas->keyframe_list().end();++iter)
674                         encode_keyframe(root->add_child("keyframe"),*iter,canvas->rend_desc().get_frame_rate());
675         }
676         
677         // Output the <defs> section
678         if(!canvas->is_inline() && !canvas->value_node_list().empty() || !canvas->children().empty())
679         {
680                 xmlpp::Element *node=root->add_child("defs");
681                 const ValueNodeList &value_node_list(canvas->value_node_list());
682
683                 for(ValueNodeList::const_iterator iter=value_node_list.begin();iter!=value_node_list.end();++iter)
684                 {
685                         // If the value_node is a constant, then use the shorthand
686                         if(handle<ValueNode_Const>::cast_dynamic(*iter))
687                         {
688                                 ValueNode_Const::Handle value_node(ValueNode_Const::Handle::cast_dynamic(*iter));
689                                 reinterpret_cast<xmlpp::Element*>(encode_value(node->add_child("value"),value_node->get_value()))->set_attribute("id",value_node->get_id());
690                                 continue;
691                         }
692                         encode_value_node(node->add_child("value_node"),*iter,canvas);
693                         // writeme
694                 }
695
696                 for(Canvas::Children::const_iterator iter=canvas->children().begin();iter!=canvas->children().end();++iter)
697                 {
698                         encode_canvas(node->add_child("canvas"),*iter);
699                 }
700         }       
701
702         Canvas::const_reverse_iterator iter;
703
704         for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
705                 encode_layer(root->add_child("layer"),*iter);   
706
707         return root;
708 }
709
710 bool
711 synfig::save_canvas(const String &filename, Canvas::ConstHandle canvas)
712 {
713     ChangeLocale change_locale(LC_NUMERIC, "C");
714
715         synfig::String tmp_filename(filename+".TMP");
716
717         try
718         {
719                 assert(canvas);
720                 xmlpp::Document document;
721         
722                 encode_canvas(document.create_root_node("canvas"),canvas);
723         
724                 document.write_to_file_formatted(tmp_filename);
725         }
726         catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; }
727         
728         
729 #ifdef _WIN32
730         // On Win32 platforms, rename() has bad behavior. work around it.
731         char old_file[80]="sif.XXXXXXXX";
732         mktemp(old_file);
733         rename(filename.c_str(),old_file);      
734         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
735         {
736                 rename(old_file,tmp_filename.c_str());
737                 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
738                 return false;
739         }
740         remove(old_file);
741 #else
742         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
743         {
744                 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
745                 return false;
746         }
747 #endif
748
749         return true;
750 }
751
752 String
753 synfig::canvas_to_string(Canvas::ConstHandle canvas)
754 {
755     ChangeLocale change_locale(LC_NUMERIC, "C");
756         assert(canvas);
757
758         xmlpp::Document document;
759
760         encode_canvas(document.create_root_node("canvas"),canvas);
761
762         return document.write_to_string_formatted();
763 }