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