1 /* === S Y N F I G ========================================================= */
2 /*! \file savecanvas.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #ifdef HAVE_SYS_ERRNO_H
34 # include <sys/errno.h>
37 #include "savecanvas.h"
39 #include "valuenode.h"
40 #include "valuenode_subtract.h"
41 #include "valuenode_animated.h"
42 #include "valuenode_composite.h"
43 #include "valuenode_const.h"
44 #include "valuenode_linear.h"
45 #include "valuenode_dynamiclist.h"
46 #include "valuenode_reference.h"
47 #include "valuenode_segcalctangent.h"
48 #include "valuenode_segcalcvertex.h"
49 #include "valuenode_bline.h"
54 #include "paramdesc.h"
56 #include <libxml++/libxml++.h>
57 #include <ETL/stringf>
62 #include <libxml/tree.h>
67 /* === U S I N G =========================================================== */
71 using namespace synfig;
73 /* === M A C R O S ========================================================= */
75 #define COLOR_VALUE_TYPE_FORMAT "%f"
76 #define VECTOR_VALUE_TYPE_FORMAT "%0.10f"
77 #define TIME_TYPE_FORMAT "%0.3f"
78 #define VIEW_BOX_FORMAT "%f %f %f %f"
80 /* === G L O B A L S ======================================================= */
82 /* === P R O C E D U R E S ================================================= */
84 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas);
85 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas);
87 xmlpp::Element* encode_keyframe(xmlpp::Element* root,const Keyframe &kf, float fps)
89 root->set_name("keyframe");
90 root->set_attribute("time",kf.get_time().get_string(fps));
91 if(!kf.get_description().empty())
92 root->set_child_text(kf.get_description());
97 xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
99 root->set_name("real");
100 root->set_attribute("value",strprintf(VECTOR_VALUE_TYPE_FORMAT,v));
104 xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float /*fps*/=0)
106 root->set_name("time");
107 //root->set_attribute("value",t.get_string(fps));
108 root->set_attribute("value",t.get_string());
112 xmlpp::Element* encode_integer(xmlpp::Element* root,int i)
114 root->set_name("integer");
115 root->set_attribute("value",strprintf("%i",i));
119 xmlpp::Element* encode_bool(xmlpp::Element* root,bool b)
121 root->set_name("bool");
122 root->set_attribute("value",b?"true":"false");
126 xmlpp::Element* encode_string(xmlpp::Element* root,const String &str)
128 root->set_name("string");
129 root->set_child_text(str);
133 xmlpp::Element* encode_vector(xmlpp::Element* root,Vector vect)
135 root->set_name("vector");
136 root->add_child("x")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[0]));
137 root->add_child("y")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[1]));
141 xmlpp::Element* encode_color(xmlpp::Element* root,Color color)
143 root->set_name("color");
144 root->add_child("r")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_r()));
145 root->add_child("g")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_g()));
146 root->add_child("b")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_b()));
147 root->add_child("a")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_a()));
151 xmlpp::Element* encode_angle(xmlpp::Element* root,Angle theta)
153 root->set_name("angle");
154 root->set_attribute("value",strprintf("%f",(float)Angle::deg(theta).get()));
158 xmlpp::Element* encode_segment(xmlpp::Element* root,Segment seg)
160 root->set_name("segment");
161 encode_vector(root->add_child("p1")->add_child("vector"),seg.p1);
162 encode_vector(root->add_child("t1")->add_child("vector"),seg.t1);
163 encode_vector(root->add_child("p2")->add_child("vector"),seg.p2);
164 encode_vector(root->add_child("t2")->add_child("vector"),seg.t2);
168 xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
170 root->set_name(ValueBase::type_name(ValueBase::TYPE_BLINEPOINT));
172 encode_vector(root->add_child("vertex")->add_child("vector"),bline_point.get_vertex());
173 encode_vector(root->add_child("t1")->add_child("vector"),bline_point.get_tangent1());
175 if(bline_point.get_split_tangent_flag())
176 encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
178 encode_real(root->add_child("width")->add_child("real"),bline_point.get_width());
179 encode_real(root->add_child("origin")->add_child("real"),bline_point.get_origin());
183 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
185 root->set_name("gradient");
187 Gradient::const_iterator iter;
189 for(iter=x.begin();iter!=x.end();iter++)
191 xmlpp::Element *cpoint(encode_color(root->add_child("color"),iter->color));
192 cpoint->set_attribute("pos",strprintf("%f",iter->pos));
198 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas=0);
200 xmlpp::Element* encode_list(xmlpp::Element* root,std::list<ValueBase> list, Canvas::ConstHandle canvas=0)
202 root->set_name("list");
206 encode_value(root->add_child("value"),list.front(),canvas);
213 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas)
215 switch(data.get_type())
217 case ValueBase::TYPE_REAL:
218 return encode_real(root,data.get(Real()));
219 case ValueBase::TYPE_TIME:
221 return encode_time(root,data.get(Time()),canvas->rend_desc().get_frame_rate());
223 return encode_time(root,data.get(Time()));
224 case ValueBase::TYPE_INTEGER:
225 return encode_integer(root,data.get(int()));
226 case ValueBase::TYPE_COLOR:
227 return encode_color(root,data.get(Color()));
228 case ValueBase::TYPE_VECTOR:
229 return encode_vector(root,data.get(Vector()));
230 case ValueBase::TYPE_ANGLE:
231 return encode_angle(root,data.get(Angle()));
232 case ValueBase::TYPE_BOOL:
233 return encode_bool(root,data.get(bool()));
234 case ValueBase::TYPE_STRING:
235 return encode_string(root,data.get(String()));
236 case ValueBase::TYPE_SEGMENT:
237 return encode_segment(root,data.get(Segment()));
238 case ValueBase::TYPE_BLINEPOINT:
239 return encode_bline_point(root,data.get(BLinePoint()));
240 case ValueBase::TYPE_GRADIENT:
241 return encode_gradient(root,data.get(Gradient()));
242 case ValueBase::TYPE_LIST:
243 return encode_list(root,data,canvas);
244 case ValueBase::TYPE_CANVAS:
245 return encode_canvas(root,data.get(Canvas::Handle()).get());
246 case ValueBase::TYPE_NIL:
247 synfig::error("Encountered NIL ValueBase");
248 root->set_name("nil");
251 synfig::error(strprintf("Unknown value(%s), cannot create XML representation!",ValueBase::type_name(data.get_type()).c_str()));
252 root->set_name("nil");
257 xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)
260 root->set_name("animated");
262 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
264 const ValueNode_Animated::WaypointList &waypoint_list=value_node->waypoint_list();
265 ValueNode_Animated::WaypointList::const_iterator iter;
267 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
269 xmlpp::Element *waypoint_node=root->add_child("waypoint");
270 //waypoint_node->set_attribute("time",iter->get_time().get_string(canvas->rend_desc().get_frame_rate()));
271 waypoint_node->set_attribute("time",iter->get_time().get_string());
273 //waypoint_node->add_child(encode_value(iter->get_value(),canvas));
274 if(iter->get_value_node()->is_exported())
275 waypoint_node->set_attribute("use",iter->get_value_node()->get_relative_id(canvas));
277 ValueNode::ConstHandle value_node = iter->get_value_node();
278 if(ValueNode_Const::ConstHandle::cast_dynamic(value_node)) {
279 const ValueBase data = ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value();
280 if (data.get_type() == ValueBase::TYPE_CANVAS)
281 waypoint_node->set_attribute("use",data.get(Canvas::Handle()).get()->get_relative_id(canvas));
283 encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
286 encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
289 switch(iter->get_before())
291 case INTERPOLATION_HALT:
292 waypoint_node->set_attribute("before","halt");
294 case INTERPOLATION_LINEAR:
295 waypoint_node->set_attribute("before","linear");
297 case INTERPOLATION_MANUAL:
298 waypoint_node->set_attribute("before","manual");
300 case INTERPOLATION_CONSTANT:
301 waypoint_node->set_attribute("before","constant");
303 case INTERPOLATION_TCB:
304 waypoint_node->set_attribute("before","auto");
307 error("Unknown waypoint type for \"before\" attribute");
310 switch(iter->get_after())
312 case INTERPOLATION_HALT:
313 waypoint_node->set_attribute("after","halt");
315 case INTERPOLATION_LINEAR:
316 waypoint_node->set_attribute("after","linear");
318 case INTERPOLATION_MANUAL:
319 waypoint_node->set_attribute("after","manual");
321 case INTERPOLATION_CONSTANT:
322 waypoint_node->set_attribute("after","constant");
324 case INTERPOLATION_TCB:
325 waypoint_node->set_attribute("after","auto");
328 error("Unknown waypoint type for \"after\" attribute");
331 if(iter->get_tension()!=0.0)
332 waypoint_node->set_attribute("tension",strprintf("%f",iter->get_tension()));
333 if(iter->get_time_tension()!=0.0)
334 waypoint_node->set_attribute("temporal-tension",strprintf("%f",iter->get_time_tension()));
335 if(iter->get_continuity()!=0.0)
336 waypoint_node->set_attribute("continuity",strprintf("%f",iter->get_continuity()));
337 if(iter->get_bias()!=0.0)
338 waypoint_node->set_attribute("bias",strprintf("%f",iter->get_bias()));
345 xmlpp::Element* encode_composite(xmlpp::Element* root,ValueNode_Composite::ConstHandle value_node,Canvas::ConstHandle canvas=0)
348 root->set_name("composite");
350 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
353 for(i=0;i<value_node->link_count();i++)
355 string name(strprintf("c%d",i+1));
356 assert(value_node->get_link(i));
357 if(value_node->get_link(i)->is_exported())
358 root->set_attribute(name,value_node->get_link(i)->get_relative_id(canvas));
360 encode_value_node(root->add_child(name)->add_child("value_node"),value_node->get_link(i).constant(),canvas);
366 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
369 root->set_name("subtract");
371 ValueNode::ConstHandle lhs=value_node->get_lhs();
372 ValueNode::ConstHandle rhs=value_node->get_rhs();
373 ValueNode::ConstHandle scalar=value_node->get_scalar();
378 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
381 warning("LHS is equal to RHS, this <subtract> will always be zero!");
383 //if(value_node->get_scalar()!=1)
384 // root->set_attribute("scalar",strprintf(VECTOR_VALUE_TYPE_FORMAT,value_node->get_scalar()));
386 if(!scalar->get_id().empty())
387 root->set_attribute("scalar",scalar->get_relative_id(canvas));
389 encode_value_node(root->add_child("scalar")->add_child("value_node"),scalar,canvas);
391 if(!lhs->get_id().empty())
392 root->set_attribute("lhs",lhs->get_relative_id(canvas));
394 encode_value_node(root->add_child("lhs")->add_child("value_node"),lhs,canvas);
396 if(!rhs->get_id().empty())
397 root->set_attribute("rhs",rhs->get_relative_id(canvas));
399 encode_value_node(root->add_child("rhs")->add_child("value_node"),rhs,canvas);
404 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
407 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
409 root->set_name(value_node->get_name());
411 root->set_attribute("type",ValueBase::type_name(value_node->get_contained_type()));
413 vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
415 ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
419 if(bline_value_node->get_loop())
420 root->set_attribute("loop","true");
422 root->set_attribute("loop","false");
425 for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
427 xmlpp::Element *entry_node=root->add_child("entry");
428 assert(iter->value_node);
429 if(!iter->value_node->get_id().empty())
430 entry_node->set_attribute("use",iter->value_node->get_relative_id(canvas));
432 encode_value_node(entry_node->add_child("value_node"),iter->value_node,canvas);
436 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
437 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
438 String begin_sequence;
441 const ActivepointList& timing_info(iter->timing_info);
442 ActivepointList::const_iterator entry_iter;
444 for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
445 if(entry_iter->state==true)
447 if(entry_iter->priority)
449 printf("begin priority is %d\n", entry_iter->priority);
450 begin_sequence+=strprintf("p%d ",entry_iter->priority);
452 begin_sequence+=entry_iter->time.get_string(fps)+", ";
456 if(entry_iter->priority)
458 printf("end priority is %d\n", entry_iter->priority);
459 end_sequence+=strprintf("p%d ",entry_iter->priority);
461 end_sequence+=entry_iter->time.get_string(fps)+", ";
464 // If this is just a plane-jane vanilla entry,
465 // then don't bother with begins and ends
466 if(end_sequence.empty() && begin_sequence=="SOT, ")
467 begin_sequence.clear();
469 if(!begin_sequence.empty())
471 // Remove the last ", " stuff
472 begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
474 entry_node->set_attribute("on",begin_sequence);
477 if(!end_sequence.empty())
479 // Remove the last ", " stuff
480 end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
482 entry_node->set_attribute("off",end_sequence);
490 // Generic linkable data node entry
491 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
494 root->set_name(value_node->get_name());
496 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
499 for(i=0;i<value_node->link_count();i++)
501 ValueNode::ConstHandle link=value_node->get_link(i).constant();
503 throw runtime_error("Bad link");
504 if(link->is_exported())
505 root->set_attribute(value_node->link_name(i),link->get_relative_id(canvas));
507 encode_value_node(root->add_child(value_node->link_name(i))->add_child("value_node"),link,canvas);
513 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
517 if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
518 encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
520 if(ValueNode_Composite::ConstHandle::cast_dynamic(value_node))
521 encode_composite(root,ValueNode_Composite::ConstHandle::cast_dynamic(value_node),canvas);
523 if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
524 encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
526 if(ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node))
527 encode_dynamic_list(root,ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node),canvas);
528 else if(ValueNode_Const::ConstHandle::cast_dynamic(value_node))
530 encode_value(root,ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value(),canvas);
533 if(LinkableValueNode::ConstHandle::cast_dynamic(value_node))
534 encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
537 error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
538 root->set_name("nil");
543 if(!value_node->get_id().empty())
544 root->set_attribute("id",value_node->get_id());
546 if(value_node->rcount()>1)
547 root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
552 xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
554 root->set_name("layer");
556 root->set_attribute("type",layer->get_name());
557 root->set_attribute("active",layer->active()?"true":"false");
559 if(!layer->get_version().empty())
560 root->set_attribute("version",layer->get_version());
561 if(!layer->get_description().empty())
562 root->set_attribute("desc",layer->get_description());
563 if(!layer->get_group().empty())
564 root->set_attribute("group",layer->get_group());
566 Layer::Vocab vocab(layer->get_param_vocab());
567 Layer::Vocab::const_iterator iter;
569 const Layer::DynamicParamList &dynamic_param_list=layer->dynamic_param_list();
571 for(iter=vocab.begin();iter!=vocab.end();++iter)
573 // Handle dynamic parameters
574 if(dynamic_param_list.count(iter->get_name()))
576 xmlpp::Element *node=root->add_child("param");
577 node->set_attribute("name",iter->get_name());
579 handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
581 // If the valuenode has no ID, then it must be defined in-place
582 if(value_node->get_id().empty())
584 encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
588 node->set_attribute("use",value_node->get_relative_id(layer->get_canvas()));
591 else // Handle normal parameters
592 if(iter->get_critical())
594 ValueBase value=layer->get_param(iter->get_name());
595 if(!value.is_valid())
597 error("Layer doesn't know its own vocabulary -- "+iter->get_name());
601 if(value.get_type()==ValueBase::TYPE_CANVAS)
603 // the ->is_inline() below was crashing if the canvas
604 // contained a PasteCanvas with the default <No Image
605 // Selected> Canvas setting; this avoids the crash
606 if (!value.get(Canvas::LooseHandle()))
609 if (!value.get(Canvas::LooseHandle())->is_inline())
611 Canvas::Handle child(value.get(Canvas::LooseHandle()));
613 if(!value.get(Canvas::Handle()))
615 xmlpp::Element *node=root->add_child("param");
616 node->set_attribute("name",iter->get_name());
617 node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
621 xmlpp::Element *node=root->add_child("param");
622 node->set_attribute("name",iter->get_name());
624 encode_value(node->add_child("value"),value,layer->get_canvas().constant());
632 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
635 const RendDesc &rend_desc=canvas->rend_desc();
636 root->set_name("canvas");
638 if(canvas->is_root())
639 root->set_attribute("version",canvas->get_version());
641 if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
642 root->set_attribute("id",canvas->get_id());
644 if(!canvas->parent() || canvas->parent()->rend_desc().get_w()!=canvas->rend_desc().get_w())
645 root->set_attribute("width",strprintf("%d",rend_desc.get_w()));
647 if(!canvas->parent() || canvas->parent()->rend_desc().get_h()!=canvas->rend_desc().get_h())
648 root->set_attribute("height",strprintf("%d",rend_desc.get_h()));
650 if(!canvas->parent() || canvas->parent()->rend_desc().get_x_res()!=canvas->rend_desc().get_x_res())
651 root->set_attribute("xres",strprintf("%f",rend_desc.get_x_res()));
653 if(!canvas->parent() || canvas->parent()->rend_desc().get_y_res()!=canvas->rend_desc().get_y_res())
654 root->set_attribute("yres",strprintf("%f",rend_desc.get_y_res()));
657 if(!canvas->parent() ||
658 canvas->parent()->rend_desc().get_tl()!=canvas->rend_desc().get_tl() ||
659 canvas->parent()->rend_desc().get_br()!=canvas->rend_desc().get_br())
660 root->set_attribute("view-box",strprintf(VIEW_BOX_FORMAT,
661 rend_desc.get_tl()[0],
662 rend_desc.get_tl()[1],
663 rend_desc.get_br()[0],
664 rend_desc.get_br()[1])
667 if(!canvas->parent() || canvas->parent()->rend_desc().get_antialias()!=canvas->rend_desc().get_antialias())
668 root->set_attribute("antialias",strprintf("%d",rend_desc.get_antialias()));
670 if(!canvas->parent())
671 root->set_attribute("fps",strprintf(TIME_TYPE_FORMAT,rend_desc.get_frame_rate()));
673 if(!canvas->parent() || canvas->parent()->rend_desc().get_time_start()!=canvas->rend_desc().get_time_start())
674 root->set_attribute("begin-time",rend_desc.get_time_start().get_string(rend_desc.get_frame_rate()));
676 if(!canvas->parent() || canvas->parent()->rend_desc().get_time_end()!=canvas->rend_desc().get_time_end())
677 root->set_attribute("end-time",rend_desc.get_time_end().get_string(rend_desc.get_frame_rate()));
679 if(!canvas->is_inline())
681 root->set_attribute("bgcolor",strprintf(VIEW_BOX_FORMAT,
682 rend_desc.get_bg_color().get_r(),
683 rend_desc.get_bg_color().get_g(),
684 rend_desc.get_bg_color().get_b(),
685 rend_desc.get_bg_color().get_a())
688 if(!canvas->get_name().empty())
689 root->add_child("name")->set_child_text(canvas->get_name());
690 if(!canvas->get_description().empty())
691 root->add_child("desc")->set_child_text(canvas->get_description());
692 if(!canvas->get_author().empty())
693 root->add_child("author")->set_child_text(canvas->get_description());
695 std::list<String> meta_keys(canvas->get_meta_data_keys());
696 while(!meta_keys.empty())
698 xmlpp::Element* meta_element(root->add_child("meta"));
699 meta_element->set_attribute("name",meta_keys.front());
700 meta_element->set_attribute("content",canvas->get_meta_data(meta_keys.front()));
701 meta_keys.pop_front();
703 for(KeyframeList::const_iterator iter=canvas->keyframe_list().begin();iter!=canvas->keyframe_list().end();++iter)
704 encode_keyframe(root->add_child("keyframe"),*iter,canvas->rend_desc().get_frame_rate());
707 // Output the <defs> section
708 if(!canvas->is_inline() && !canvas->value_node_list().empty() || !canvas->children().empty())
710 xmlpp::Element *node=root->add_child("defs");
711 const ValueNodeList &value_node_list(canvas->value_node_list());
713 for(ValueNodeList::const_iterator iter=value_node_list.begin();iter!=value_node_list.end();++iter)
715 // If the value_node is a constant, then use the shorthand
716 if(handle<ValueNode_Const>::cast_dynamic(*iter))
718 ValueNode_Const::Handle value_node(ValueNode_Const::Handle::cast_dynamic(*iter));
719 reinterpret_cast<xmlpp::Element*>(encode_value(node->add_child("value"),value_node->get_value()))->set_attribute("id",value_node->get_id());
722 encode_value_node(node->add_child("value_node"),*iter,canvas);
726 for(Canvas::Children::const_iterator iter=canvas->children().begin();iter!=canvas->children().end();++iter)
728 encode_canvas(node->add_child("canvas"),*iter);
732 Canvas::const_reverse_iterator iter;
734 for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
735 encode_layer(root->add_child("layer"),*iter);
741 synfig::save_canvas(const String &filename, Canvas::ConstHandle canvas)
743 ChangeLocale change_locale(LC_NUMERIC, "C");
745 synfig::String tmp_filename(filename+".TMP");
747 if (filename_extension(filename) == ".sifz")
748 xmlSetCompressMode(9);
750 xmlSetCompressMode(0);
755 xmlpp::Document document;
757 encode_canvas(document.create_root_node("canvas"),canvas);
759 document.write_to_file_formatted(tmp_filename);
761 catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; }
765 // On Win32 platforms, rename() has bad behavior. work around it.
766 char old_file[80]="sif.XXXXXXXX";
768 rename(filename.c_str(),old_file);
769 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
771 rename(old_file,tmp_filename.c_str());
772 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
777 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
779 synfig::error("synfig::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
788 synfig::canvas_to_string(Canvas::ConstHandle canvas)
790 ChangeLocale change_locale(LC_NUMERIC, "C");
793 xmlpp::Document document;
795 encode_canvas(document.create_root_node("canvas"),canvas);
797 return document.write_to_string_formatted();