1 /* === S Y N F I G ========================================================= */
2 /*! \file savecanvas.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #ifdef HAVE_SYS_ERRNO_H
33 # include <sys/errno.h>
36 #include "savecanvas.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"
53 #include "paramdesc.h"
55 #include <libxml++/libxml++.h>
56 #include <ETL/stringf>
62 /* === U S I N G =========================================================== */
66 using namespace synfig;
68 /* === M A C R O S ========================================================= */
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"
75 /* === G L O B A L S ======================================================= */
77 /* === P R O C E D U R E S ================================================= */
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);
82 xmlpp::Element* encode_keyframe(xmlpp::Element* root,const Keyframe &kf, float fps)
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());
92 xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
94 root->set_name("real");
95 root->set_attribute("value",strprintf(VECTOR_VALUE_TYPE_FORMAT,v));
99 xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float fps=0)
101 root->set_name("time");
102 //root->set_attribute("value",t.get_string(fps));
103 root->set_attribute("value",t.get_string());
107 xmlpp::Element* encode_integer(xmlpp::Element* root,int i)
109 root->set_name("integer");
110 root->set_attribute("value",strprintf("%i",i));
114 xmlpp::Element* encode_bool(xmlpp::Element* root,bool b)
116 root->set_name("bool");
117 root->set_attribute("value",b?"true":"false");
121 xmlpp::Element* encode_string(xmlpp::Element* root,const String &str)
123 root->set_name("string");
124 root->set_child_text(str);
128 xmlpp::Element* encode_vector(xmlpp::Element* root,Vector vect)
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]));
136 xmlpp::Element* encode_color(xmlpp::Element* root,Color color)
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()));
146 xmlpp::Element* encode_angle(xmlpp::Element* root,Angle theta)
148 root->set_name("angle");
149 root->set_attribute("value",strprintf("%f",(float)Angle::deg(theta).get()));
153 xmlpp::Element* encode_segment(xmlpp::Element* root,Segment seg)
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);
163 xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
165 root->set_name(ValueBase::type_name(ValueBase::TYPE_BLINEPOINT));
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());
170 if(bline_point.get_split_tangent_flag())
171 encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
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());
178 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
180 root->set_name("gradient");
182 Gradient::const_iterator iter;
184 for(iter=x.begin();iter!=x.end();iter++)
186 xmlpp::Element *cpoint(encode_color(root->add_child("color"),iter->color));
187 cpoint->set_attribute("pos",strprintf("%f",iter->pos));
193 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas=0);
195 xmlpp::Element* encode_list(xmlpp::Element* root,std::list<ValueBase> list, Canvas::ConstHandle canvas=0)
197 root->set_name("list");
201 encode_value(root->add_child("value"),list.front(),canvas);
208 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas)
210 switch(data.get_type())
212 case ValueBase::TYPE_REAL:
213 return encode_real(root,data.get(Real()));
214 case ValueBase::TYPE_TIME:
216 return encode_time(root,data.get(Time()),canvas->rend_desc().get_frame_rate());
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");
246 synfig::error(strprintf("Unknown value(%s), cannot create XML representation!",ValueBase::type_name(data.get_type()).c_str()));
247 root->set_name("nil");
252 xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)
255 root->set_name("animated");
257 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
259 const ValueNode_Animated::WaypointList &waypoint_list=value_node->waypoint_list();
260 ValueNode_Animated::WaypointList::const_iterator iter;
262 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
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());
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));
272 encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
274 switch(iter->get_before())
276 case INTERPOLATION_HALT:
277 waypoint_node->set_attribute("before","halt");
279 case INTERPOLATION_LINEAR:
280 waypoint_node->set_attribute("before","linear");
282 case INTERPOLATION_MANUAL:
283 waypoint_node->set_attribute("before","manual");
285 case INTERPOLATION_CONSTANT:
286 waypoint_node->set_attribute("before","constant");
288 case INTERPOLATION_TCB:
289 // This is the default value, so don't add a new attribute
292 error("Unknown waypoint type for \"before\" attribute");
295 switch(iter->get_after())
297 case INTERPOLATION_HALT:
298 waypoint_node->set_attribute("after","halt");
300 case INTERPOLATION_LINEAR:
301 waypoint_node->set_attribute("after","linear");
303 case INTERPOLATION_MANUAL:
304 waypoint_node->set_attribute("after","manual");
306 case INTERPOLATION_CONSTANT:
307 waypoint_node->set_attribute("after","constant");
309 case INTERPOLATION_TCB:
310 // This is the default value, so don't add a new attribute
313 error("Unknown waypoint type for \"before\" attribute");
316 if(iter->get_tension()!=0.0)
317 waypoint_node->set_attribute("tension",strprintf("%f",iter->get_tension()));
318 if(iter->get_time_tension()!=0.0)
319 waypoint_node->set_attribute("temporal-tension",strprintf("%f",iter->get_time_tension()));
320 if(iter->get_continuity()!=0.0)
321 waypoint_node->set_attribute("continuity",strprintf("%f",iter->get_continuity()));
322 if(iter->get_bias()!=0.0)
323 waypoint_node->set_attribute("bias",strprintf("%f",iter->get_bias()));
330 xmlpp::Element* encode_composite(xmlpp::Element* root,ValueNode_Composite::ConstHandle value_node,Canvas::ConstHandle canvas=0)
333 root->set_name("composite");
335 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
338 for(i=0;i<value_node->link_count();i++)
340 string name(strprintf("c%d",i+1));
341 assert(value_node->get_link(i));
342 if(value_node->get_link(i)->is_exported())
343 root->set_attribute(name,value_node->get_link(i)->get_relative_id(canvas));
345 encode_value_node(root->add_child(name)->add_child("value_node"),value_node->get_link(i).constant(),canvas);
351 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
354 root->set_name("subtract");
356 ValueNode::ConstHandle lhs=value_node->get_lhs();
357 ValueNode::ConstHandle rhs=value_node->get_rhs();
358 ValueNode::ConstHandle scalar=value_node->get_scalar();
363 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
366 warning("LHS is equal to RHS, this <subtract> will always be zero!");
368 //if(value_node->get_scalar()!=1)
369 // root->set_attribute("scalar",strprintf(VECTOR_VALUE_TYPE_FORMAT,value_node->get_scalar()));
371 if(!scalar->get_id().empty())
372 root->set_attribute("scalar",scalar->get_relative_id(canvas));
374 encode_value_node(root->add_child("scalar")->add_child("value_node"),scalar,canvas);
376 if(!lhs->get_id().empty())
377 root->set_attribute("lhs",lhs->get_relative_id(canvas));
379 encode_value_node(root->add_child("lhs")->add_child("value_node"),lhs,canvas);
381 if(!rhs->get_id().empty())
382 root->set_attribute("rhs",rhs->get_relative_id(canvas));
384 encode_value_node(root->add_child("rhs")->add_child("value_node"),rhs,canvas);
389 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
392 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
394 root->set_name(value_node->get_name());
396 root->set_attribute("type",ValueBase::type_name(value_node->get_contained_type()));
398 vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
400 ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
404 if(bline_value_node->get_loop())
405 root->set_attribute("loop","true");
407 root->set_attribute("loop","false");
410 for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
412 xmlpp::Element *entry_node=root->add_child("entry");
413 assert(iter->value_node);
414 if(!iter->value_node->get_id().empty())
415 entry_node->set_attribute("use",iter->value_node->get_relative_id(canvas));
417 encode_value_node(entry_node->add_child("value_node"),iter->value_node,canvas);
421 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
422 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
423 String begin_sequence;
426 const ActivepointList& timing_info(iter->timing_info);
427 ActivepointList::const_iterator entry_iter;
429 for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
430 if(entry_iter->state==true)
432 if(entry_iter->priority)
434 printf("begin priority is %d\n", entry_iter->priority);
435 begin_sequence+=strprintf("p%d ",entry_iter->priority);
437 begin_sequence+=entry_iter->time.get_string(fps)+", ";
441 if(entry_iter->priority)
443 printf("end priority is %d\n", entry_iter->priority);
444 end_sequence+=strprintf("p%d ",entry_iter->priority);
446 end_sequence+=entry_iter->time.get_string(fps)+", ";
449 // If this is just a plane-jane vanilla entry,
450 // then don't bother with begins and ends
451 if(end_sequence.empty() && begin_sequence=="SOT, ")
452 begin_sequence.clear();
454 if(!begin_sequence.empty())
456 // Remove the last ", " stuff
457 begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
459 entry_node->set_attribute("on",begin_sequence);
462 if(!end_sequence.empty())
464 // Remove the last ", " stuff
465 end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
467 entry_node->set_attribute("off",end_sequence);
475 // Generic linkable data node entry
476 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
479 root->set_name(value_node->get_name());
481 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
484 for(i=0;i<value_node->link_count();i++)
486 ValueNode::ConstHandle link=value_node->get_link(i).constant();
488 throw runtime_error("Bad link");
489 if(link->is_exported())
490 root->set_attribute(value_node->link_name(i),link->get_relative_id(canvas));
492 encode_value_node(root->add_child(value_node->link_name(i))->add_child("value_node"),link,canvas);
498 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
502 if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
503 encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
505 if(ValueNode_Composite::ConstHandle::cast_dynamic(value_node))
506 encode_composite(root,ValueNode_Composite::ConstHandle::cast_dynamic(value_node),canvas);
508 if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
509 encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
511 if(ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node))
512 encode_dynamic_list(root,ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node),canvas);
513 else if(ValueNode_Const::ConstHandle::cast_dynamic(value_node))
515 encode_value(root,ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value(),canvas);
518 if(LinkableValueNode::ConstHandle::cast_dynamic(value_node))
519 encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
522 error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
523 root->set_name("nil");
528 if(!value_node->get_id().empty())
529 root->set_attribute("id",value_node->get_id());
531 if(value_node->rcount()>1)
532 root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
537 xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
539 root->set_name("layer");
541 root->set_attribute("type",layer->get_name());
542 root->set_attribute("active",layer->active()?"true":"false");
544 if(!layer->get_version().empty())
545 root->set_attribute("version",layer->get_version());
546 if(!layer->get_description().empty())
547 root->set_attribute("desc",layer->get_description());
548 if(!layer->get_group().empty())
549 root->set_attribute("group",layer->get_group());
551 Layer::Vocab vocab(layer->get_param_vocab());
552 Layer::Vocab::const_iterator iter;
554 const Layer::DynamicParamList &dynamic_param_list=layer->dynamic_param_list();
556 for(iter=vocab.begin();iter!=vocab.end();++iter)
558 // Handle dynamic parameters
559 if(dynamic_param_list.count(iter->get_name()))
561 xmlpp::Element *node=root->add_child("param");
562 node->set_attribute("name",iter->get_name());
564 handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
566 // If the valuenode.has no ID, then it must be defined in-place
567 if(value_node->get_id().empty())
569 encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
573 node->set_attribute("use",value_node->get_relative_id(layer->get_canvas()));
576 else // Handle normal parameters
577 if(iter->get_critical())
579 ValueBase value=layer->get_param(iter->get_name());
580 if(!value.is_valid())
582 error("Layer doesn't know its own vocabulary -- "+iter->get_name());
586 if(value.get_type()==ValueBase::TYPE_CANVAS && !value.get(Canvas::LooseHandle())->is_inline())
588 Canvas::Handle child(value.get(Canvas::LooseHandle()));
590 if(!value.get(Canvas::Handle()))
592 xmlpp::Element *node=root->add_child("param");
593 node->set_attribute("name",iter->get_name());
594 node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
597 xmlpp::Element *node=root->add_child("param");
598 node->set_attribute("name",iter->get_name());
600 encode_value(node->add_child("value"),value,layer->get_canvas().constant());
608 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
611 const RendDesc &rend_desc=canvas->rend_desc();
612 root->set_name("canvas");
614 if(canvas->is_root())
615 root->set_attribute("version","0.1");
617 if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
618 root->set_attribute("id",canvas->get_id());
620 if(!canvas->parent() || canvas->parent()->rend_desc().get_w()!=canvas->rend_desc().get_w())
621 root->set_attribute("width",strprintf("%d",rend_desc.get_w()));
623 if(!canvas->parent() || canvas->parent()->rend_desc().get_h()!=canvas->rend_desc().get_h())
624 root->set_attribute("height",strprintf("%d",rend_desc.get_h()));
626 if(!canvas->parent() || canvas->parent()->rend_desc().get_x_res()!=canvas->rend_desc().get_x_res())
627 root->set_attribute("xres",strprintf("%f",rend_desc.get_x_res()));
629 if(!canvas->parent() || canvas->parent()->rend_desc().get_y_res()!=canvas->rend_desc().get_y_res())
630 root->set_attribute("yres",strprintf("%f",rend_desc.get_y_res()));
633 if(!canvas->parent() ||
634 canvas->parent()->rend_desc().get_tl()!=canvas->rend_desc().get_tl() ||
635 canvas->parent()->rend_desc().get_br()!=canvas->rend_desc().get_br())
636 root->set_attribute("view-box",strprintf(VIEW_BOX_FORMAT,
637 rend_desc.get_tl()[0],
638 rend_desc.get_tl()[1],
639 rend_desc.get_br()[0],
640 rend_desc.get_br()[1])
643 if(!canvas->parent() || canvas->parent()->rend_desc().get_antialias()!=canvas->rend_desc().get_antialias())
644 root->set_attribute("antialias",strprintf("%d",rend_desc.get_antialias()));
646 if(!canvas->parent())
647 root->set_attribute("fps",strprintf(TIME_TYPE_FORMAT,rend_desc.get_frame_rate()));
649 if(!canvas->parent() || canvas->parent()->rend_desc().get_time_start()!=canvas->rend_desc().get_time_start())
650 root->set_attribute("begin-time",rend_desc.get_time_start().get_string(rend_desc.get_frame_rate()));
652 if(!canvas->parent() || canvas->parent()->rend_desc().get_time_end()!=canvas->rend_desc().get_time_end())
653 root->set_attribute("end-time",rend_desc.get_time_end().get_string(rend_desc.get_frame_rate()));
655 if(!canvas->is_inline())
657 root->set_attribute("bgcolor",strprintf(VIEW_BOX_FORMAT,
658 rend_desc.get_bg_color().get_r(),
659 rend_desc.get_bg_color().get_g(),
660 rend_desc.get_bg_color().get_b(),
661 rend_desc.get_bg_color().get_a())
664 if(!canvas->get_name().empty())
665 root->add_child("name")->set_child_text(canvas->get_name());
666 if(!canvas->get_description().empty())
667 root->add_child("desc")->set_child_text(canvas->get_description());
668 if(!canvas->get_author().empty())
669 root->add_child("author")->set_child_text(canvas->get_description());
671 std::list<String> meta_keys(canvas->get_meta_data_keys());
672 while(!meta_keys.empty())
674 xmlpp::Element* meta_element(root->add_child("meta"));
675 meta_element->set_attribute("name",meta_keys.front());
676 meta_element->set_attribute("content",canvas->get_meta_data(meta_keys.front()));
677 meta_keys.pop_front();
679 for(KeyframeList::const_iterator iter=canvas->keyframe_list().begin();iter!=canvas->keyframe_list().end();++iter)
680 encode_keyframe(root->add_child("keyframe"),*iter,canvas->rend_desc().get_frame_rate());
683 // Output the <defs> section
684 if(!canvas->is_inline() && !canvas->value_node_list().empty() || !canvas->children().empty())
686 xmlpp::Element *node=root->add_child("defs");
687 const ValueNodeList &value_node_list(canvas->value_node_list());
689 for(ValueNodeList::const_iterator iter=value_node_list.begin();iter!=value_node_list.end();++iter)
691 // If the value_node is a constant, then use the shorthand
692 if(handle<ValueNode_Const>::cast_dynamic(*iter))
694 ValueNode_Const::Handle value_node(ValueNode_Const::Handle::cast_dynamic(*iter));
695 reinterpret_cast<xmlpp::Element*>(encode_value(node->add_child("value"),value_node->get_value()))->set_attribute("id",value_node->get_id());
698 encode_value_node(node->add_child("value_node"),*iter,canvas);
702 for(Canvas::Children::const_iterator iter=canvas->children().begin();iter!=canvas->children().end();++iter)
704 encode_canvas(node->add_child("canvas"),*iter);
708 Canvas::const_reverse_iterator iter;
710 for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
711 encode_layer(root->add_child("layer"),*iter);
717 synfig::save_canvas(const String &filename, Canvas::ConstHandle canvas)
719 ChangeLocale change_locale(LC_NUMERIC, "C");
721 synfig::String tmp_filename(filename+".TMP");
726 xmlpp::Document document;
728 encode_canvas(document.create_root_node("canvas"),canvas);
730 document.write_to_file_formatted(tmp_filename);
732 catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; }
736 // On Win32 platforms, rename() has bad behavior. work around it.
737 char old_file[80]="sif.XXXXXXXX";
739 rename(filename.c_str(),old_file);
740 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
742 rename(old_file,tmp_filename.c_str());
743 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
748 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
750 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
759 synfig::canvas_to_string(Canvas::ConstHandle canvas)
761 ChangeLocale change_locale(LC_NUMERIC, "C");
764 xmlpp::Document document;
766 encode_canvas(document.create_root_node("canvas"),canvas);
768 return document.write_to_string_formatted();