1 /* === S I N F G =========================================================== */
2 /*! \file savecanvas.cpp
5 ** $Id: savecanvas.cpp,v 1.2 2005/01/07 03:29:12 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
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.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "savecanvas.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"
48 #include "paramdesc.h"
50 #include <libxml++/libxml++.h>
51 #include <ETL/stringf>
56 /* === U S I N G =========================================================== */
60 using namespace sinfg;
62 /* === M A C R O S ========================================================= */
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"
69 /* === G L O B A L S ======================================================= */
71 /* === P R O C E D U R E S ================================================= */
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);
76 xmlpp::Element* encode_keyframe(xmlpp::Element* root,const Keyframe &kf, float fps)
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());
86 xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
88 root->set_name("real");
89 root->set_attribute("value",strprintf(VECTOR_VALUE_TYPE_FORMAT,v));
93 xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float fps=0)
95 root->set_name("time");
96 //root->set_attribute("value",t.get_string(fps));
97 root->set_attribute("value",t.get_string());
101 xmlpp::Element* encode_integer(xmlpp::Element* root,int i)
103 root->set_name("integer");
104 root->set_attribute("value",strprintf("%i",i));
108 xmlpp::Element* encode_bool(xmlpp::Element* root,bool b)
110 root->set_name("bool");
111 root->set_attribute("value",b?"true":"false");
115 xmlpp::Element* encode_string(xmlpp::Element* root,const String &str)
117 root->set_name("string");
118 root->set_child_text(str);
122 xmlpp::Element* encode_vector(xmlpp::Element* root,Vector vect)
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]));
130 xmlpp::Element* encode_color(xmlpp::Element* root,Color color)
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()));
140 xmlpp::Element* encode_angle(xmlpp::Element* root,Angle theta)
142 root->set_name("angle");
143 root->set_attribute("value",strprintf("%f",(float)Angle::deg(theta).get()));
147 xmlpp::Element* encode_segment(xmlpp::Element* root,Segment seg)
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);
157 xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
159 root->set_name(ValueBase::type_name(ValueBase::TYPE_BLINEPOINT));
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());
164 if(bline_point.get_split_tangent_flag())
165 encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
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());
172 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
174 root->set_name("gradient");
176 Gradient::const_iterator iter;
178 for(iter=x.begin();iter!=x.end();iter++)
180 xmlpp::Element *cpoint(encode_color(root->add_child("color"),iter->color));
181 cpoint->set_attribute("pos",strprintf("%f",iter->pos));
187 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas=0);
189 xmlpp::Element* encode_list(xmlpp::Element* root,std::list<ValueBase> list, Canvas::ConstHandle canvas=0)
191 root->set_name("list");
195 encode_value(root->add_child("value"),list.front(),canvas);
202 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas)
204 switch(data.get_type())
206 case ValueBase::TYPE_REAL:
207 return encode_real(root,data.get(Real()));
208 case ValueBase::TYPE_TIME:
210 return encode_time(root,data.get(Time()),canvas->rend_desc().get_frame_rate());
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 sinfg::error("Encountered NIL ValueBase");
237 root->set_name("nil");
240 sinfg::error(strprintf("Unknown value(%s), cannot create XML representation!",ValueBase::type_name(data.get_type()).c_str()));
241 root->set_name("nil");
246 xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)
249 root->set_name("animated");
251 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
253 const ValueNode_Animated::WaypointList &waypoint_list=value_node->waypoint_list();
254 ValueNode_Animated::WaypointList::const_iterator iter;
256 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
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());
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));
266 encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
268 switch(iter->get_before())
270 case INTERPOLATION_HALT:
271 waypoint_node->set_attribute("before","halt");
273 case INTERPOLATION_LINEAR:
274 waypoint_node->set_attribute("before","linear");
276 case INTERPOLATION_MANUAL:
277 waypoint_node->set_attribute("before","manual");
279 case INTERPOLATION_CONSTANT:
280 waypoint_node->set_attribute("before","constant");
282 case INTERPOLATION_TCB:
283 // This is the default value, so don't add a new attribute
286 error("Unknown waypoint type for \"before\" attribute");
289 switch(iter->get_after())
291 case INTERPOLATION_HALT:
292 waypoint_node->set_attribute("after","halt");
294 case INTERPOLATION_LINEAR:
295 waypoint_node->set_attribute("after","linear");
297 case INTERPOLATION_MANUAL:
298 waypoint_node->set_attribute("after","manual");
300 case INTERPOLATION_CONSTANT:
301 waypoint_node->set_attribute("after","constant");
303 case INTERPOLATION_TCB:
304 // This is the default value, so don't add a new attribute
307 error("Unknown waypoint type for \"before\" attribute");
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()));
324 xmlpp::Element* encode_composite(xmlpp::Element* root,ValueNode_Composite::ConstHandle value_node,Canvas::ConstHandle canvas=0)
327 root->set_name("composite");
329 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
332 for(i=0;i<value_node->link_count();i++)
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));
339 encode_value_node(root->add_child(name)->add_child("value_node"),value_node->get_link(i).constant(),canvas);
345 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
348 root->set_name("subtract");
350 ValueNode::ConstHandle lhs=value_node->get_lhs();
351 ValueNode::ConstHandle rhs=value_node->get_rhs();
352 ValueNode::ConstHandle scalar=value_node->get_scalar();
357 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
360 warning("LHS is equal to RHS, this <subtract> will always be zero!");
362 //if(value_node->get_scalar()!=1)
363 // root->set_attribute("scalar",strprintf(VECTOR_VALUE_TYPE_FORMAT,value_node->get_scalar()));
365 if(!scalar->get_id().empty())
366 root->set_attribute("scalar",scalar->get_relative_id(canvas));
368 encode_value_node(root->add_child("scalar")->add_child("value_node"),scalar,canvas);
370 if(!lhs->get_id().empty())
371 root->set_attribute("lhs",lhs->get_relative_id(canvas));
373 encode_value_node(root->add_child("lhs")->add_child("value_node"),lhs,canvas);
375 if(!rhs->get_id().empty())
376 root->set_attribute("rhs",rhs->get_relative_id(canvas));
378 encode_value_node(root->add_child("rhs")->add_child("value_node"),rhs,canvas);
383 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
386 // const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
389 root->set_name(value_node->get_name());
391 root->set_attribute("type",ValueBase::type_name(value_node->get_contained_type()));
393 vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
395 ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
399 if(bline_value_node->get_loop())
400 root->set_attribute("loop","true");
402 root->set_attribute("loop","false");
405 for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
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));
412 encode_value_node(entry_node->add_child("value_node"),iter->value_node,canvas);
416 typedef sinfg::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
417 typedef sinfg::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
418 String begin_sequence;
421 const ActivepointList& timing_info(iter->timing_info);
422 ActivepointList::const_iterator entry_iter;
424 for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
425 if(entry_iter->state==true)
427 if(entry_iter->priority)
428 begin_sequence+=strprintf("p%d ",entry_iter->priority);
429 begin_sequence+=entry_iter->time.get_string(fps)+", ";
433 if(entry_iter->priority)
434 end_sequence+=strprintf("p%d ",entry_iter->priority);
435 end_sequence+=entry_iter->time.get_string(fps)+", ";
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();
443 if(!begin_sequence.empty())
445 // Remove the last ", " stuff
446 begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
448 entry_node->set_attribute("on",begin_sequence);
451 if(!end_sequence.empty())
453 // Remove the last ", " stuff
454 end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
456 entry_node->set_attribute("off",end_sequence);
464 // Generic linkable data node entry
465 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
468 root->set_name(value_node->get_name());
470 root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
473 for(i=0;i<value_node->link_count();i++)
475 ValueNode::ConstHandle link=value_node->get_link(i).constant();
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));
481 encode_value_node(root->add_child(value_node->link_name(i))->add_child("value_node"),link,canvas);
487 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
491 if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
492 encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
494 if(ValueNode_Composite::ConstHandle::cast_dynamic(value_node))
495 encode_composite(root,ValueNode_Composite::ConstHandle::cast_dynamic(value_node),canvas);
497 if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
498 encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
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))
504 encode_value(root,ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value(),canvas);
507 if(LinkableValueNode::ConstHandle::cast_dynamic(value_node))
508 encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
511 error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
512 root->set_name("nil");
517 if(!value_node->get_id().empty())
518 root->set_attribute("id",value_node->get_id());
520 if(value_node->rcount()>1)
521 root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
526 xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
528 root->set_name("layer");
530 root->set_attribute("type",layer->get_name());
531 root->set_attribute("active",layer->active()?"true":"false");
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());
540 Layer::Vocab vocab(layer->get_param_vocab());
541 Layer::Vocab::const_iterator iter;
543 const Layer::DynamicParamList &dynamic_param_list=layer->dynamic_param_list();
545 for(iter=vocab.begin();iter!=vocab.end();++iter)
547 // Handle dynamic parameters
548 if(dynamic_param_list.count(iter->get_name()))
550 xmlpp::Element *node=root->add_child("param");
551 node->set_attribute("name",iter->get_name());
553 handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
555 // If the valuenode.has no ID, then it must be defined in-place
556 if(value_node->get_id().empty())
558 encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
562 node->set_attribute("use",value_node->get_relative_id(layer->get_canvas()));
565 else // Handle normal parameters
566 if(iter->get_critical())
568 ValueBase value=layer->get_param(iter->get_name());
569 if(!value.is_valid())
571 error("Layer doesn't know it's own vocabulary -- "+iter->get_name());
575 if(value.get_type()==ValueBase::TYPE_CANVAS && !value.get(Canvas::LooseHandle())->is_inline())
577 Canvas::Handle child(value.get(Canvas::LooseHandle()));
579 if(!value.get(Canvas::Handle()))
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()));
586 xmlpp::Element *node=root->add_child("param");
587 node->set_attribute("name",iter->get_name());
589 encode_value(node->add_child("value"),value,layer->get_canvas().constant());
597 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
600 const RendDesc &rend_desc=canvas->rend_desc();
601 root->set_name("canvas");
603 if(canvas->is_root())
604 root->set_attribute("version","0.1");
606 if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
607 root->set_attribute("id",canvas->get_id());
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()));
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()));
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()));
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()));
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])
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()));
635 if(!canvas->parent())
636 root->set_attribute("fps",strprintf(TIME_TYPE_FORMAT,rend_desc.get_frame_rate()));
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()));
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()));
644 if(!canvas->is_inline())
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())
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());
660 std::list<String> meta_keys(canvas->get_meta_data_keys());
661 while(!meta_keys.empty())
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();
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());
672 // Output the <defs> section
673 if(!canvas->is_inline() && !canvas->value_node_list().empty() || !canvas->children().empty())
675 xmlpp::Element *node=root->add_child("defs");
676 const ValueNodeList &value_node_list(canvas->value_node_list());
678 for(ValueNodeList::const_iterator iter=value_node_list.begin();iter!=value_node_list.end();++iter)
680 // If the value_node is a constant, then use the shorthand
681 if(handle<ValueNode_Const>::cast_dynamic(*iter))
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());
687 encode_value_node(node->add_child("value_node"),*iter,canvas);
691 for(Canvas::Children::const_iterator iter=canvas->children().begin();iter!=canvas->children().end();++iter)
693 encode_canvas(node->add_child("canvas"),*iter);
697 Canvas::const_reverse_iterator iter;
699 for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
700 encode_layer(root->add_child("layer"),*iter);
706 sinfg::save_canvas(const String &filename, Canvas::ConstHandle canvas)
708 sinfg::String tmp_filename(filename+".TMP");
713 xmlpp::Document document;
715 encode_canvas(document.create_root_node("canvas"),canvas);
717 document.write_to_file_formatted(tmp_filename);
719 catch(...) { sinfg::error("sinfg::save_canvas(): Caught unknown exception"); return false; }
723 // On Win32 platforms, rename() has bad behavior. work around it.
724 char old_file[80]="sif.XXXXXXXX";
726 rename(filename.c_str(),old_file);
727 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
729 rename(old_file,tmp_filename.c_str());
730 sinfg::error("sinfg::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
735 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
737 sinfg::error("sinfg::save_canvas(): Unable to rename file to correct filename, errno=%d",errno);
746 sinfg::canvas_to_string(Canvas::ConstHandle canvas)
750 xmlpp::Document document;
752 encode_canvas(document.create_root_node("canvas"),canvas);
754 return document.write_to_string_formatted();