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