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