1 /* === S Y N F I G ========================================================= */
2 /*! \file loadcanvas.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 ======================================================= */
35 #include <ETL/stringf>
36 #include <libxml++/libxml++.h>
41 #include "loadcanvas.h"
42 #include "valuenode.h"
43 #include "valuenode_subtract.h"
44 #include "valuenode_animated.h"
45 #include "valuenode_composite.h"
46 #include "valuenode_const.h"
47 #include "valuenode_linear.h"
48 #include "valuenode_dynamiclist.h"
49 #include "valuenode_reference.h"
50 #include "valuenode_scale.h"
51 #include "valuenode_timedswap.h"
52 #include "valuenode_twotone.h"
53 #include "valuenode_stripes.h"
54 #include "valuenode_segcalctangent.h"
55 #include "valuenode_segcalcvertex.h"
56 #include "valuenode_bline.h"
61 #include "exception.h"
66 #include <sigc++/bind.h>
70 /* === U S I N G =========================================================== */
73 using namespace synfig;
80 test_class() { assert(!bleh); bleh++; synfig::info("test_class: initi: %d",bleh); }
81 ~test_class() { assert(bleh); synfig::info("test_class: uninit: %d",bleh); bleh--; }
83 int test_class::bleh(0);
85 test_class test_class_instance;
88 /* === M A C R O S ========================================================= */
90 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
92 /* === P R O C E D U R E S ================================================= */
94 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
96 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
99 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
100 return *open_canvas_map_;
103 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
105 static void _canvas_file_name_changed(Canvas *x)
107 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
109 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
112 assert(iter!=get_open_canvas_map().end());
113 if(iter==get_open_canvas_map().end())
115 get_open_canvas_map().erase(iter->first);
116 get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
121 synfig::open_canvas(const String &filename)
125 parser.set_allow_errors(true);
127 Canvas::Handle canvas=parser.parse_from_file(filename);
129 if(parser.error_count())
130 return Canvas::Handle();
136 synfig::open_canvas_as(const String &filename,const String &as)
140 parser.set_allow_errors(true);
142 Canvas::Handle canvas=parser.parse_from_file_as(filename,as);
144 if(parser.error_count())
145 return Canvas::Handle();
151 synfig::string_to_canvas(const String &data)
155 parser.set_allow_errors(true);
157 Canvas::Handle canvas=parser.parse_from_string(data);
159 if(parser.error_count())
160 return Canvas::Handle();
165 /* === M E T H O D S ======================================================= */
168 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
170 error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
174 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
176 error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
180 CanvasParser::warning(xmlpp::Node *element, const String &text)
182 string str=strprintf("%s:<%s>:%d: warning: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
183 //synfig::warning(str);
186 if(total_warnings_>=max_warnings_)
187 fatal_error(element, _("Too many warnings"));
191 CanvasParser::error(xmlpp::Node *element, const String &text)
193 string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
196 throw runtime_error(str);
198 // synfig::error(str);
202 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
204 string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
205 throw runtime_error(str);
211 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
213 assert(element->get_name()=="keyframe");
215 if(!element->get_attribute("time"))
217 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
221 Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
224 if(element->get_children().empty())
227 if(element->get_child_text()->get_content().empty())
230 ret.set_description(element->get_child_text()->get_content());
237 CanvasParser::parse_real(xmlpp::Element *element,Canvas::Handle canvas)
239 assert(element->get_name()=="real");
241 if(!element->get_children().empty())
242 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
244 if(!element->get_attribute("value"))
246 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
250 string val=element->get_attribute("value")->get_value();
252 return atof(val.c_str());
256 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
258 assert(element->get_name()=="time");
260 if(!element->get_children().empty())
261 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
263 if(!element->get_attribute("value"))
265 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
269 string val=element->get_attribute("value")->get_value();
271 return Time(val,canvas->rend_desc().get_frame_rate());
275 CanvasParser::parse_integer(xmlpp::Element *element,Canvas::Handle canvas)
277 assert(element->get_name()=="integer");
279 if(!element->get_children().empty())
280 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
282 if(!element->get_attribute("value"))
284 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
288 string val=element->get_attribute("value")->get_value();
290 return atoi(val.c_str());
294 CanvasParser::parse_vector(xmlpp::Element *element,Canvas::Handle canvas)
296 assert(element->get_name()=="vector");
298 if(element->get_children().empty())
300 error(element, "Undefined value in <vector>");
306 xmlpp::Element::NodeList list = element->get_children();
307 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
309 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
313 if(child->get_name()=="x")
315 if(child->get_children().empty())
317 error(element, "Undefined value in <x>");
320 vect[0]=atof(child->get_child_text()->get_content().c_str());
323 if(child->get_name()=="y")
325 if(child->get_children().empty())
327 error(element, "Undefined value in <y>");
330 vect[1]=atof(child->get_child_text()->get_content().c_str());
333 error_unexpected_element(child,child->get_name());
339 CanvasParser::parse_color(xmlpp::Element *element,Canvas::Handle canvas)
341 assert(element->get_name()=="color");
343 if(element->get_children().empty())
345 error(element, "Undefined value in <color>");
351 xmlpp::Element::NodeList list = element->get_children();
352 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
354 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
358 if(child->get_name()=="r")
360 if(child->get_children().empty())
362 error(element, "Undefined value in <r>");
365 color.set_r(atof(child->get_child_text()->get_content().c_str()));
368 if(child->get_name()=="g")
370 if(child->get_children().empty())
372 error(element, "Undefined value in <g>");
375 color.set_g(atof(child->get_child_text()->get_content().c_str()));
378 if(child->get_name()=="b")
380 if(child->get_children().empty())
382 error(element, "Undefined value in <b>");
385 color.set_b(atof(child->get_child_text()->get_content().c_str()));
388 if(child->get_name()=="a")
390 if(child->get_children().empty())
392 error(element, "Undefined value in <a>");
395 color.set_a(atof(child->get_child_text()->get_content().c_str()));
398 error_unexpected_element(child,child->get_name());
405 CanvasParser::parse_string(xmlpp::Element *element,Canvas::Handle canvas)
407 assert(element->get_name()=="string");
409 if(element->get_children().empty())
411 warning(element, "Undefined value in <string>");
412 return synfig::String();
415 if(element->get_child_text()->get_content().empty())
417 warning(element, "Content element of <string> appears to be empty");
418 return synfig::String();
421 return element->get_child_text()->get_content();
425 CanvasParser::parse_bool(xmlpp::Element *element,Canvas::Handle canvas)
427 assert(element->get_name()=="bool");
429 if(!element->get_children().empty())
430 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
432 if(!element->get_attribute("value"))
434 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
438 string val=element->get_attribute("value")->get_value();
440 if(val=="true" || val=="1")
442 if(val=="false" || val=="0")
445 error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
451 CanvasParser::parse_gradient(xmlpp::Element *node,Canvas::Handle canvas)
453 assert(node->get_name()=="gradient");
456 xmlpp::Element::NodeList list = node->get_children();
457 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
459 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
464 Gradient::CPoint cpoint;
465 cpoint.color=parse_color(child,canvas);
467 if(!child->get_attribute("pos"))
469 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
473 cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
475 ret.push_back(cpoint);
483 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
485 vector<ValueBase> value_list;
487 xmlpp::Element::NodeList list = element->get_children();
488 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
490 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
495 value_list.push_back(parse_value(child,canvas));
496 if(!value_list.back().is_valid())
498 value_list.pop_back();
499 error(child,"Bad ValueBase");
508 CanvasParser::parse_segment(xmlpp::Element *element,Canvas::Handle canvas)
510 assert(element->get_name()=="segment");
512 if(element->get_children().empty())
514 error(element, "Undefined value in <segment>");
520 xmlpp::Element::NodeList list = element->get_children();
521 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
523 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
527 if(child->get_name()=="p1")
529 xmlpp::Element::NodeList list = child->get_children();
530 xmlpp::Element::NodeList::iterator iter;
532 // Search for the first non-text XML element
533 for(iter = list.begin(); iter != list.end(); ++iter)
534 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
538 error(element, "Undefined value in <p1>");
542 if((*iter)->get_name()!="vector")
544 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
548 seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas);
551 if(child->get_name()=="t1")
553 xmlpp::Element::NodeList list = child->get_children();
554 xmlpp::Element::NodeList::iterator iter;
556 // Search for the first non-text XML element
557 for(iter = list.begin(); iter != list.end(); ++iter)
558 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
562 error(element, "Undefined value in <t1>");
566 if((*iter)->get_name()!="vector")
568 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
572 seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas);
575 if(child->get_name()=="p2")
577 xmlpp::Element::NodeList list = child->get_children();
578 xmlpp::Element::NodeList::iterator iter;
580 // Search for the first non-text XML element
581 for(iter = list.begin(); iter != list.end(); ++iter)
582 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
586 error(element, "Undefined value in <p2>");
590 if((*iter)->get_name()!="vector")
592 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
596 seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas);
599 if(child->get_name()=="t2")
601 xmlpp::Element::NodeList list = child->get_children();
602 xmlpp::Element::NodeList::iterator iter;
604 // Search for the first non-text XML element
605 for(iter = list.begin(); iter != list.end(); ++iter)
606 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
610 error(element, "Undefined value in <t2>");
614 if((*iter)->get_name()!="vector")
616 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
620 seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas);
623 error_unexpected_element(child,child->get_name());
629 CanvasParser::parse_bline_point(xmlpp::Element *element,Canvas::Handle canvas)
631 assert(element->get_name()=="bline_point");
632 if(element->get_children().empty())
634 error(element, "Undefined value in <bline_point>");
639 ret.set_split_tangent_flag(false);
641 xmlpp::Element::NodeList list = element->get_children();
642 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
644 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
649 if(child->get_name()[0]=='v' || child->get_name()=="p1")
651 xmlpp::Element::NodeList list = child->get_children();
652 xmlpp::Element::NodeList::iterator iter;
654 // Search for the first non-text XML element
655 for(iter = list.begin(); iter != list.end(); ++iter)
656 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
660 error(element, "Undefined value in <vertex>");
664 if((*iter)->get_name()!="vector")
666 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
670 ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas));
674 if(child->get_name()=="t1" || child->get_name()=="tangent")
676 xmlpp::Element::NodeList list = child->get_children();
677 xmlpp::Element::NodeList::iterator iter;
679 // Search for the first non-text XML element
680 for(iter = list.begin(); iter != list.end(); ++iter)
681 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
685 error(element, "Undefined value in <t1>");
689 if((*iter)->get_name()!="vector")
691 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
695 ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas));
699 if(child->get_name()=="t2")
701 xmlpp::Element::NodeList list = child->get_children();
702 xmlpp::Element::NodeList::iterator iter;
704 // Search for the first non-text XML element
705 for(iter = list.begin(); iter != list.end(); ++iter)
706 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
710 error(element, "Undefined value in <t2>");
714 if((*iter)->get_name()!="vector")
716 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
720 ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter),canvas));
721 ret.set_split_tangent_flag(true);
725 if(child->get_name()=="width")
727 xmlpp::Element::NodeList list = child->get_children();
728 xmlpp::Element::NodeList::iterator iter;
730 // Search for the first non-text XML element
731 for(iter = list.begin(); iter != list.end(); ++iter)
732 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
736 error(element, "Undefined value in <width>");
740 if((*iter)->get_name()!="real")
742 error_unexpected_element((*iter),(*iter)->get_name(),"real");
746 ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter),canvas));
750 if(child->get_name()=="origin")
752 xmlpp::Element::NodeList list = child->get_children();
753 xmlpp::Element::NodeList::iterator iter;
755 // Search for the first non-text XML element
756 for(iter = list.begin(); iter != list.end(); ++iter)
757 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
761 error(element, "Undefined value in <origin>");
765 if((*iter)->get_name()!="real")
767 error_unexpected_element((*iter),(*iter)->get_name(),"real");
771 ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter),canvas));
774 error_unexpected_element(child,child->get_name());
780 CanvasParser::parse_angle(xmlpp::Element *element,Canvas::Handle canvas)
782 assert(element->get_name()=="angle");
784 if(!element->get_children().empty())
785 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
787 if(!element->get_attribute("value"))
789 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
793 string val=element->get_attribute("value")->get_value();
795 return Angle::deg(atof(val.c_str()));
799 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
801 if(element->get_name()=="real")
802 return parse_real(element,canvas);
804 if(element->get_name()=="time")
805 return parse_time(element,canvas);
807 if(element->get_name()=="integer")
808 return parse_integer(element,canvas);
810 if(element->get_name()=="string")
811 return parse_string(element,canvas);
813 if(element->get_name()=="vector")
815 return parse_vector(element,canvas);
818 if(element->get_name()=="color")
819 return parse_color(element,canvas);
821 if(element->get_name()=="segment")
822 return parse_segment(element,canvas);
824 if(element->get_name()=="list")
825 return parse_list(element,canvas);
827 if(element->get_name()=="gradient")
828 return parse_gradient(element,canvas);
830 if(element->get_name()=="bool")
831 return parse_bool(element,canvas);
833 //if(element->get_name()=="canvas")
834 // return parse_canvas(element,canvas,true); // inline canvas
836 if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
837 return parse_angle(element,canvas);
839 if(element->get_name()=="bline_point")
840 return parse_bline_point(element,canvas);
842 if(element->get_name()=="canvas")
843 return ValueBase(parse_canvas(element,canvas,true));
847 error_unexpected_element(element,element->get_name());
856 ValueNode_Animated::Handle
857 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
859 assert(element->get_name()=="hermite" || element->get_name()=="animated");
861 if(!element->get_attribute("type"))
863 error(element,"Missing attribute \"type\" in <animated>");
864 return ValueNode_Animated::Handle();
867 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
871 error(element,"Bad type in <animated>");
872 return ValueNode_Animated::Handle();
875 ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
879 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_name(type).c_str()));
880 return ValueNode_Animated::Handle();
883 value_node->set_root_canvas(canvas->get_root());
885 xmlpp::Element::NodeList list = element->get_children();
886 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
888 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
892 if(child->get_name()=="waypoint")
894 if(!child->get_attribute("time"))
896 error(child,_("<waypoint> is missing attribute \"time\""));
900 Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
903 ValueNode::Handle waypoint_value_node;
905 if(child->get_attribute("use"))
907 waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
911 if(child->get_children().empty())
913 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
917 xmlpp::Element::NodeList list = child->get_children();
918 xmlpp::Element::NodeList::iterator iter;
920 // Search for the first non-text XML element
921 for(iter = list.begin(); iter != list.end(); ++iter)
922 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
926 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
930 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
933 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
937 error(child,_("Bad data for <waypoint>"));
941 if(!waypoint_value_node)
943 error(child,_("Bad data for <waypoint>"));
947 /*! HACK -- This is a temporary fix to help repair some
948 ** weirdness that is currently going on (10-21-2004).
949 ** This short circuits the linking of waypoints,
950 ** a feature which is so obscure that we can get
951 ** away with something like this pretty easily.
953 waypoint_value_node=waypoint_value_node->clone();
955 // Warn if there is trash after the param value
956 for(iter++; iter != list.end(); ++iter)
957 if(dynamic_cast<xmlpp::Element*>(*iter))
958 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
963 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
965 if(child->get_attribute("tension"))
967 synfig::String str(child->get_attribute("tension")->get_value());
968 waypoint->set_tension(atof(str.c_str()));
970 if(child->get_attribute("temporal-tension"))
972 synfig::String str(child->get_attribute("temporal-tension")->get_value());
973 waypoint->set_time_tension(atof(str.c_str()));
975 if(child->get_attribute("continuity"))
977 synfig::String str(child->get_attribute("continuity")->get_value());
978 waypoint->set_continuity(atof(str.c_str()));
980 if(child->get_attribute("bias"))
982 synfig::String str(child->get_attribute("bias")->get_value());
983 waypoint->set_bias(atof(str.c_str()));
986 if(child->get_attribute("before"))
988 string val=child->get_attribute("before")->get_value();
990 waypoint->set_before(INTERPOLATION_HALT);
991 else if(val=="constant")
992 waypoint->set_before(INTERPOLATION_CONSTANT);
993 else if(val=="linear")
994 waypoint->set_before(INTERPOLATION_LINEAR);
995 else if(val=="manual")
996 waypoint->set_before(INTERPOLATION_MANUAL);
998 waypoint->set_before(INTERPOLATION_TCB);
1000 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1003 if(child->get_attribute("after"))
1005 string val=child->get_attribute("after")->get_value();
1007 waypoint->set_after(INTERPOLATION_HALT);
1008 else if(val=="constant")
1009 waypoint->set_after(INTERPOLATION_CONSTANT);
1010 else if(val=="linear")
1011 waypoint->set_after(INTERPOLATION_LINEAR);
1012 else if(val=="manual")
1013 waypoint->set_after(INTERPOLATION_MANUAL);
1014 else if(val=="auto")
1015 waypoint->set_after(INTERPOLATION_TCB);
1017 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1020 catch(Exception::BadTime x)
1022 warning(child,x.what());
1028 error_unexpected_element(child,child->get_name());
1030 value_node->changed();
1034 // This function is a phase-out hack for the timed swap value node
1035 etl::handle<ValueNode_Animated>
1036 CanvasParser::parse_timedswap(xmlpp::Element *node,Canvas::Handle canvas)
1038 ValueNode_TimedSwap::Handle timed_swap(parse_linkable_value_node(node,canvas));
1042 ValueNode_Animated::Handle animated(ValueNode_Animated::create(timed_swap->get_type()));
1044 animated->set_root_canvas(canvas->get_root());
1048 Time swap_time, swap_length;
1049 (*timed_swap->get_swap_time())(0).put(&swap_time);
1050 (*timed_swap->get_swap_length())(0).put(&swap_length);
1052 animated->new_waypoint(swap_time-swap_length,timed_swap->get_before());
1053 animated->new_waypoint(swap_time,timed_swap->get_after());
1059 handle<ValueNode_Subtract>
1060 CanvasParser::parse_subtract(xmlpp::Element *element,Canvas::Handle canvas)
1062 assert(element->get_name()=="subtract");
1064 handle<ValueNode_Subtract> value_node;
1065 handle<ValueNode> lhs,rhs,scalar;
1067 if(element->get_attribute("type"))
1069 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1073 error(element,"Bad type in <subtract>");
1074 return ValueNode_Subtract::Handle();
1076 value_node=ValueNode_Subtract::create(type);
1079 value_node=ValueNode_Subtract::create();
1083 error(element,strprintf(_("Unable to create <subtract>")));
1084 return handle<ValueNode_Subtract>();
1087 //if(element->get_attribute("scalar"))
1089 // value_node->set_scalar(atof(element->get_attribute("scalar")->get_value().c_str()));
1094 if(element->get_attribute("scalar"))
1096 // This is for compatibility with older versions of the file format
1097 String value(element->get_attribute("scalar")->get_value());
1098 if((value[0]<='9' && value[0]>='0') || value[0]=='-')
1100 warning(element, _("Use of a real value where the ID should be is deprecated"));
1101 value_node->set_scalar(atof(value.c_str()));
1104 value_node->set_scalar(canvas->surefind_value_node(value));
1105 scalar=value_node->get_scalar();
1108 if(element->get_attribute("lhs"))
1110 lhs=canvas->surefind_value_node(element->get_attribute("lhs")->get_value());
1111 value_node->set_lhs(lhs);
1114 if(element->get_attribute("rhs"))
1116 rhs=canvas->surefind_value_node(element->get_attribute("rhs")->get_value());
1117 value_node->set_rhs(rhs);
1120 catch (Exception::IDNotFound)
1122 error(element,"attribute in <subtract> references unknown ID");
1124 catch (Exception::FileNotFound)
1126 error(element,"Unable to open external file referenced in ID");
1129 xmlpp::Element::NodeList list = element->get_children();
1130 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1132 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1136 if(child->get_name()=="lhs")
1140 error(child,"LHS component of <subtract> already defined!");
1144 xmlpp::Element::NodeList list = child->get_children();
1145 xmlpp::Element::NodeList::iterator iter;
1147 // Search for the first non-text XML element
1148 for(iter = list.begin(); iter != list.end(); ++iter)
1149 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1151 if(iter==list.end())
1153 error(child,_("<lhs> is missing its contents"));
1157 lhs=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1160 value_node->set_lhs(lhs);
1163 error((*iter),"Parse of LHS ValueNode failed");
1166 // \todo do a search for more elements and warn if they are found
1169 if(child->get_name()=="rhs")
1173 error(child,"RHS component of <subtract> already defined!");
1177 xmlpp::Element::NodeList list = child->get_children();
1178 xmlpp::Element::NodeList::iterator iter;
1180 // Search for the first non-text XML element
1181 for(iter = list.begin(); iter != list.end(); ++iter)
1182 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1184 if(iter==list.end())
1186 error(child,_("<rhs> is missing its contents"));
1190 rhs=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1193 value_node->set_rhs(rhs);
1196 error((*iter),"Parse of RHS ValueNode failed");
1199 // \todo do a search for more elements and warn if they are found
1202 if(child->get_name()=="scalar")
1206 error(child,"scalar component of <subtract> already defined!");
1210 xmlpp::Element::NodeList list = child->get_children();
1211 xmlpp::Element::NodeList::iterator iter;
1213 // Search for the first non-text XML element
1214 for(iter = list.begin(); iter != list.end(); ++iter)
1215 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1217 if(iter==list.end())
1219 error(child,_("<scalar> is missing its contents"));
1223 scalar=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1226 value_node->set_scalar(scalar);
1229 error((*iter),"Parse of scalar ValueNode failed");
1232 // \todo do a search for more elements and warn if they are found
1235 error_unexpected_element(child,child->get_name());
1238 if(!value_node->get_rhs() || !value_node->get_lhs() || !value_node->get_scalar())
1239 error(element,"<subtract> is missing LHS, RHS, or SCALAR");
1241 if(value_node->get_rhs() == value_node->get_lhs())
1242 warning(element,"LHS is equal to RHS in <subtract>, so this value_node will always be zero!");
1247 etl::handle<LinkableValueNode>
1248 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1250 handle<LinkableValueNode> value_node;
1251 ValueBase::Type type;
1253 // Determine the type
1254 if(element->get_attribute("type"))
1256 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1260 error(element,"Bad type in ValueNode");
1266 error(element,"Missing type in ValueNode");
1270 value_node=LinkableValueNode::create(element->get_name(),type);
1274 error(element,"Unknown ValueNode type "+element->get_name());
1278 if(value_node->get_type()!=type)
1280 error(element,"ValueNode did not accept type");
1284 value_node->set_root_canvas(canvas->get_root());
1287 for(i=0;i<value_node->link_count();i++)
1289 if(element->get_attribute(value_node->link_name(i)))
1291 String id(element->get_attribute(value_node->link_name(i))->get_value());
1293 if(!value_node->set_link(i,
1294 canvas->surefind_value_node(
1298 ) error(element,strprintf(_("Unable to set link \"%s\" to ValueNode \"%s\" (link #%d in \"%s\")"),value_node->link_name(i).c_str(),id.c_str(),i,value_node->get_name().c_str()));
1300 catch(Exception::IDNotFound)
1302 error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1304 catch(Exception::FileNotFound)
1306 error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1310 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1311 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
1318 xmlpp::Element::NodeList list = element->get_children();
1319 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1321 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1326 int index=value_node->get_link_index_from_name(child->get_name());
1328 xmlpp::Element::NodeList list = child->get_children();
1329 xmlpp::Element::NodeList::iterator iter;
1331 // Search for the first non-text XML element
1332 for(iter = list.begin(); iter != list.end(); ++iter)
1333 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1335 if(iter==list.end())
1337 error(child,_("element is missing its contents"));
1341 ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1345 error((*iter),"Parse of ValueNode failed");
1348 if(!value_node->set_link(index,link))
1350 //error(dynamic_cast<xmlpp::Element*>(*iter),strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1351 error(element,strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1354 // \todo do a search for more elements and warn if they are found
1357 catch(Exception::BadLinkName)
1359 error_unexpected_element(child,child->get_name());
1363 error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1371 handle<ValueNode_Composite>
1372 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1374 assert(element->get_name()=="composite");
1376 if(!element->get_attribute("type"))
1378 error(element,"Missing attribute \"type\" in <composite>");
1379 return handle<ValueNode_Composite>();
1382 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1386 error(element,"Bad type in <composite>");
1387 return handle<ValueNode_Composite>();
1390 handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1391 handle<ValueNode> c[6];
1395 error(element,strprintf(_("Unable to create <composite>")));
1396 return handle<ValueNode_Composite>();
1401 for(i=0;i<value_node->link_count();i++)
1403 string name=strprintf("c%d",i+1);
1406 error(element,name+" was already defined in <composite>");
1409 if(element->get_attribute(name))
1411 c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1414 if(!value_node->set_link(i,c[i]))
1416 error(element,'"'+name+"\" attribute in <composite> has bad type");
1420 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1424 xmlpp::Element::NodeList list = element->get_children();
1425 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1427 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1431 for(i=0;i<value_node->link_count();i++)
1433 string name=strprintf("c%d",i+1);
1434 if(child->get_name()==name)
1438 error(child,name+" was already defined in <composite>");
1442 xmlpp::Element::NodeList list = child->get_children();
1443 xmlpp::Element::NodeList::iterator iter;
1445 // Search for the first non-text XML element
1446 for(iter = list.begin(); iter != list.end(); ++iter)
1447 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1449 if(iter==list.end())
1451 error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1455 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1459 error((*iter),"Parse of "+name+" ValueNode failed");
1463 if(!value_node->set_link(i,c[i]))
1465 error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1469 // \todo do a search for more elements and warn if they are found
1473 // somewhat of a hack, but it works
1474 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1477 switch(value_node->link_count())
1480 if(!value_node->get_link(0))
1482 error(element,"<composite> is missing parameters");
1483 return handle<ValueNode_Composite>();
1487 if(!value_node->get_link(0) ||!value_node->get_link(1))
1489 error(element,"<composite> is missing parameters");
1490 return handle<ValueNode_Composite>();
1494 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1496 error(element,"<composite> is missing parameters");
1497 return handle<ValueNode_Composite>();
1501 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1503 error(element,"<composite> is missing parameters");
1504 return handle<ValueNode_Composite>();
1511 // This will also parse a bline
1512 handle<ValueNode_DynamicList>
1513 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1515 assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1517 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1519 if(!element->get_attribute("type"))
1521 error(element,"Missing attribute \"type\" in <dynamic_list>");
1522 return handle<ValueNode_DynamicList>();
1525 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1529 error(element,"Bad type in <dynamic_list>");
1530 return handle<ValueNode_DynamicList>();
1533 handle<ValueNode_DynamicList> value_node;
1534 handle<ValueNode_BLine> bline_value_node;
1536 if(element->get_name()=="bline")
1538 value_node=bline_value_node=ValueNode_BLine::create();
1539 if(element->get_attribute("loop"))
1541 String loop=element->get_attribute("loop")->get_value();
1542 if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1543 bline_value_node->set_loop(true);
1545 bline_value_node->set_loop(false);
1550 value_node=ValueNode_DynamicList::create(type);
1554 error(element,strprintf(_("Unable to create <dynamic_list>")));
1555 return handle<ValueNode_DynamicList>();
1558 value_node->set_root_canvas(canvas->get_root());
1560 xmlpp::Element::NodeList list = element->get_children();
1561 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1563 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1567 if(child->get_name()=="entry")
1569 ValueNode_DynamicList::ListEntry list_entry;
1571 // Parse begin/end waypoints
1573 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1574 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1576 String begin_sequence;
1577 String end_sequence;
1579 ActivepointList &timing_info(list_entry.timing_info);
1581 if(child->get_attribute("begin"))
1582 begin_sequence=child->get_attribute("begin")->get_value();
1584 if(child->get_attribute("on"))
1585 begin_sequence=child->get_attribute("on")->get_value();
1587 if(child->get_attribute("end"))
1588 end_sequence=child->get_attribute("end")->get_value();
1590 if(child->get_attribute("off"))
1591 end_sequence=child->get_attribute("off")->get_value();
1593 // clear out any auto-start
1594 if(!begin_sequence.empty())
1595 timing_info.clear();
1598 while(!begin_sequence.empty())
1600 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1601 String timecode(begin_sequence.begin(), iter);
1604 // skip whitespace before checking for a priority
1605 while (isspace(timecode[0]))
1606 timecode=timecode.substr(1);
1608 // If there is a priority, then grab it and remove
1609 // it from the timecode
1610 if(timecode[0]=='p')
1612 //priority=timecode[1]-'0';
1613 //timecode=String(timecode.begin()+3,timecode.end());
1614 int space=timecode.find_first_of(' ');
1615 priority=atoi(String(timecode,1,space-1).c_str());
1616 timecode=String(timecode.begin()+space+1,timecode.end());
1617 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1620 timing_info.push_back(
1626 true, // Mark as a "on" activepoint
1631 if(iter==begin_sequence.end())
1632 begin_sequence.clear();
1634 begin_sequence=String(iter+1,begin_sequence.end());
1638 while(!end_sequence.empty())
1640 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1641 String timecode(end_sequence.begin(), iter);
1644 // skip whitespace before checking for a priority
1645 while (isspace(timecode[0]))
1646 timecode=timecode.substr(1);
1648 // If there is a priority, then grab it and remove
1649 // it from the timecode
1650 if(timecode[0]=='p')
1652 //priority=timecode[1]-'0';
1653 //timecode=String(timecode.begin()+3,timecode.end());
1654 int space=timecode.find_first_of(' ');
1655 priority=atoi(String(timecode,1,space-1).c_str());
1656 timecode=String(timecode.begin()+space+1,timecode.end());
1657 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1660 timing_info.push_back(
1666 false, // Mark as a "off" activepoint
1670 if(iter==end_sequence.end())
1671 end_sequence.clear();
1673 end_sequence=String(iter+1,end_sequence.end());
1679 if(child->get_attribute("use"))
1681 string id=child->get_attribute("use")->get_value();
1684 list_entry.value_node=canvas->surefind_value_node(id);
1686 catch(Exception::IDNotFound)
1688 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1694 xmlpp::Element::NodeList list = child->get_children();
1695 xmlpp::Element::NodeList::iterator iter;
1697 // Search for the first non-text XML element
1698 for(iter = list.begin(); iter != list.end(); ++iter)
1699 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1701 if(iter==list.end())
1703 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1707 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1709 if(!list_entry.value_node)
1710 error((*iter),"Parse of ValueNode failed");
1712 // \todo do a search for more elements and warn if they are found
1716 value_node->add(list_entry);
1717 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1720 error_unexpected_element(child,child->get_name());
1726 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1728 handle<ValueNode> value_node;
1733 if(element->get_attribute("guid"))
1735 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1736 value_node=guid_cast<ValueNode>(guid);
1741 // If ValueBase::ident_type() recognises the name, then we know it's a ValueBase
1742 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1744 ValueBase data=parse_value(element,canvas);
1746 if(!data.is_valid())
1748 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1752 // We want to convert this ValueBase into a
1753 // ValueNode_Const. That way, we can treat the
1754 // ID like any other Datanode. Think of this
1755 // as a shorthand for creating constant ValueNodes.
1757 value_node=ValueNode_Const::create(data);
1760 if(element->get_name()=="hermite" || element->get_name()=="animated")
1761 value_node=parse_animated(element,canvas);
1763 if(element->get_name()=="subtract")
1764 value_node=parse_subtract(element,canvas);
1766 if(element->get_name()=="composite")
1767 value_node=parse_composite(element,canvas);
1769 if(element->get_name()=="dynamic_list")
1770 value_node=parse_dynamic_list(element,canvas);
1772 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1773 value_node=parse_dynamic_list(element,canvas);
1775 if(element->get_name()=="timed_swap")
1776 value_node=parse_timedswap(element,canvas);
1778 if(LinkableValueNode::book().count(element->get_name()))
1779 value_node=parse_linkable_value_node(element,canvas);
1781 if(element->get_name()=="canvas")
1782 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1785 error_unexpected_element(element,element->get_name());
1786 error(element, "Expected a ValueNode");
1790 value_node->set_root_canvas(canvas->get_root());
1793 // If we were successful, and our element has
1794 // an ID attribute, go ahead and add it to the
1796 if(value_node && element->get_attribute("id"))
1798 string id=element->get_attribute("id")->get_value();
1800 //value_node->set_id(id);
1802 // If there is already a value_node in the list
1803 // with the same ID, then that is an error
1804 try { canvas->add_value_node(value_node,id); }
1805 catch(Exception::BadLinkName)
1807 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1810 catch(Exception::IDAlreadyExists)
1812 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1817 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1821 value_node->set_guid(guid);
1826 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1828 assert(element->get_name()=="defs");
1829 xmlpp::Element::NodeList list = element->get_children();
1830 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1832 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1836 if(child->get_name()=="canvas")
1837 parse_canvas(child, canvas);
1839 parse_value_node(child,canvas);
1844 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1847 assert(element->get_name()=="layer");
1848 Layer::Handle layer;
1850 if(!element->get_attribute("type"))
1852 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1853 return Layer::Handle();
1856 layer=Layer::create(element->get_attribute("type")->get_value());
1857 layer->set_canvas(canvas);
1859 if(element->get_attribute("group"))
1861 layer->add_to_group(
1862 element->get_attribute("group")->get_value()
1866 // Handle the version attribute
1867 if(element->get_attribute("version"))
1869 String version(element->get_attribute("version")->get_value());
1870 if(version>layer->get_version())
1871 warning(element,_("Installed layer version is larger than layer version in file"));
1872 if(version!=layer->get_version())
1873 layer->set_version(version);
1876 // Handle the description
1877 if(element->get_attribute("desc"))
1878 layer->set_description(element->get_attribute("desc")->get_value());
1880 if(element->get_attribute("active"))
1881 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1883 xmlpp::Element::NodeList list = element->get_children();
1884 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1886 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1890 if(child->get_name()=="name")
1891 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1893 if(child->get_name()=="desc")
1894 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1896 if(child->get_name()=="param")
1898 xmlpp::Element::NodeList list = child->get_children();
1900 if(!child->get_attribute("name"))
1902 error(child,_("Missing \"name\" attribute for <param>."));
1906 String param_name=child->get_attribute("name")->get_value();
1908 if(child->get_attribute("use"))
1910 // If the "use" attribute is used, then the
1911 // element should be empty. Warn the user if
1912 // we find otherwise.
1914 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1916 String str= child->get_attribute("use")->get_value();
1918 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1920 if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1921 error((*iter),_("Layer rejected canvas link"));
1926 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1928 // Assign the value_node to the dynamic parameter list
1929 layer->connect_dynamic_param(param_name,value_node);
1931 catch(Exception::IDNotFound)
1933 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1939 xmlpp::Element::NodeList::iterator iter;
1941 // Search for the first non-text XML element
1942 for(iter = list.begin(); iter != list.end(); ++iter)
1943 if(dynamic_cast<xmlpp::Element*>(*iter))
1945 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1947 if(iter==list.end())
1949 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1953 // If we recognise the element name as a
1954 // ValueBase, then treat is at one
1955 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1957 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1959 if(!data.is_valid())
1961 error((*iter),_("Bad data for <param>"));
1965 // Set the layer's parameter, and make sure that
1966 // the layer liked it
1967 if(!layer->set_param(param_name,data))
1969 warning((*iter),_("Layer rejected value for <param>"));
1973 else // ... otherwise, we assume that it is a ValueNode
1975 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1979 error((*iter),_("Bad data for <param>"));
1983 // Assign the value_node to the dynamic parameter list
1984 layer->connect_dynamic_param(param_name,value_node);
1987 // Warn if there is trash after the param value
1988 for(iter++; iter != list.end(); ++iter)
1989 if(dynamic_cast<xmlpp::Element*>(*iter))
1990 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1994 error_unexpected_element(child,child->get_name());
1997 layer->reset_version();
2002 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
2005 if(element->get_name()!="canvas")
2007 error_unexpected_element(element,element->get_name(),"canvas");
2008 return Canvas::Handle();
2010 Canvas::Handle canvas;
2014 if(parent && (element->get_attribute("id") || inline_))
2018 canvas=Canvas::create_inline(parent);
2024 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
2028 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
2031 canvas->rend_desc().clear_flags();
2035 canvas=Canvas::create();
2036 if(filename=="/dev/stdin")
2037 canvas->set_file_name("./stdin.sif");
2039 canvas->set_file_name(filename);
2040 canvas->rend_desc().clear_flags();
2043 if(element->get_attribute("guid"))
2045 GUID guid(element->get_attribute("guid")->get_value());
2046 if(guid_cast<Canvas>(guid))
2047 return guid_cast<Canvas>(guid);
2049 canvas->set_guid(guid);
2052 if(element->get_attribute("width"))
2053 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
2055 if(element->get_attribute("height"))
2056 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
2058 if(element->get_attribute("xres"))
2059 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
2061 if(element->get_attribute("yres"))
2062 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
2065 if(element->get_attribute("fps"))
2066 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
2068 if(element->get_attribute("start-time"))
2069 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2071 if(element->get_attribute("begin-time"))
2072 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2074 if(element->get_attribute("end-time"))
2075 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2077 if(element->get_attribute("antialias"))
2078 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
2080 if(element->get_attribute("view-box"))
2082 string values=element->get_attribute("view-box")->get_value();
2086 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
2087 values=string(values.begin()+values.find(' ')+1,values.end());
2088 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
2089 values=string(values.begin()+values.find(' ')+1,values.end());
2090 br[0]=atof(string(values.data(),values.find(' ')).c_str());
2091 values=string(values.begin()+values.find(' ')+1,values.end());
2092 br[1]=atof(values.c_str());
2094 canvas->rend_desc().set_tl(tl);
2095 canvas->rend_desc().set_br(br);
2098 if(element->get_attribute("bgcolor"))
2100 string values=element->get_attribute("bgcolor")->get_value();
2103 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
2104 values=string(values.begin()+values.find(' ')+1,values.end());
2106 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
2107 values=string(values.begin()+values.find(' ')+1,values.end());
2109 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
2110 values=string(values.begin()+values.find(' ')+1,values.end());
2112 bg.set_a(atof(values.c_str()));
2114 canvas->rend_desc().set_bg_color(bg);
2117 if(element->get_attribute("focus"))
2119 string values=element->get_attribute("focus")->get_value();
2122 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
2123 values=string(values.begin()+values.find(' ')+1,values.end());
2124 focus[1]=atof(values.c_str());
2126 canvas->rend_desc().set_focus(focus);
2129 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2131 xmlpp::Element::NodeList list = element->get_children();
2132 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2134 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
2137 if(child->get_name()=="defs")
2139 if(canvas->is_inline())
2140 error(child,_("Inline canvas cannot have a <defs> section"));
2141 parse_canvas_defs(child, canvas);
2144 if(child->get_name()=="keyframe")
2146 if(canvas->is_inline())
2148 warning(child,_("Inline canvas cannot have keyframes"));
2152 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2153 canvas->keyframe_list().sync();
2156 if(child->get_name()=="meta")
2158 if(canvas->is_inline())
2160 warning(child,_("Inline canvases cannot have metadata"));
2164 String name,content;
2166 if(!child->get_attribute("name"))
2168 warning(child,_("<meta> must have a name"));
2172 if(!child->get_attribute("content"))
2174 warning(child,_("<meta> must have content"));
2178 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2180 else if(child->get_name()=="name")
2182 xmlpp::Element::NodeList list = child->get_children();
2184 // If we don't have any name, warn
2186 warning(child,_("blank \"name\" entitity"));
2189 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2190 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2191 canvas->set_name(tmp);
2194 if(child->get_name()=="desc")
2197 xmlpp::Element::NodeList list = child->get_children();
2199 // If we don't have any description, warn
2201 warning(child,_("blank \"desc\" entitity"));
2204 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2205 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2206 canvas->set_description(tmp);
2209 if(child->get_name()=="author")
2212 xmlpp::Element::NodeList list = child->get_children();
2214 // If we don't have any description, warn
2216 warning(child,_("blank \"author\" entitity"));
2219 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2220 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2221 canvas->set_author(tmp);
2224 if(child->get_name()=="layer")
2226 //if(canvas->is_inline())
2227 // canvas->push_front(parse_layer(child,canvas->parent()));
2229 canvas->push_front(parse_layer(child,canvas));
2232 error_unexpected_element(child,child->get_name());
2235 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2239 if(canvas->value_node_list().placeholder_count())
2241 error(element,strprintf(_("Canvas %s has undefined ValueNodes"),canvas->get_id().c_str()));
2248 CanvasParser::parse_from_file(const String &file)
2250 return parse_from_file_as(file,file);
2254 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2256 CHECK_EXPIRE_TIME();
2259 ChangeLocale change_locale(LC_NUMERIC, "C");
2260 String file(unix_to_local_path(file_));
2261 String as(unix_to_local_path(as_));
2263 if(get_open_canvas_map().count(etl::absolute_path(as)))
2264 return get_open_canvas_map()[etl::absolute_path(as)];
2268 xmlpp::DomParser parser(file);
2271 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2272 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2273 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2274 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2278 const ValueNodeList& value_node_list(canvas->value_node_list());
2281 ValueNodeList::const_iterator iter;
2282 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2284 ValueNode::Handle value_node(*iter);
2285 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2287 canvas->remove_value_node(value_node);
2295 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2296 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2297 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2298 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2299 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2300 catch(const std::exception& ex)
2302 synfig::error("Standard Exception: "+String(ex.what()));
2303 return Canvas::Handle();
2305 catch(const String& str)
2308 // synfig::error(str);
2309 return Canvas::Handle();
2311 return Canvas::Handle();
2315 CanvasParser::parse_from_string(const String &data)
2317 CHECK_EXPIRE_TIME();
2321 ChangeLocale change_locale(LC_NUMERIC, "C");
2322 filename=_("<INTERNAL>");
2324 xmlpp::DomParser parser;
2325 parser.parse_memory(data);
2326 xmlpp::Element *root=parser.get_document()->get_root_node();
2329 Canvas::Handle canvas(parse_canvas(root));
2330 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2331 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2333 const ValueNodeList& value_node_list(canvas->value_node_list());
2335 ValueNodeList::const_iterator iter;
2336 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2338 ValueNode::Handle value_node(*iter);
2339 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2341 canvas->remove_value_node(value_node);
2349 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2350 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2351 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2352 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2353 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2354 catch(const std::exception& ex)
2356 synfig::error("Standard Exception: "+String(ex.what()));
2357 return Canvas::Handle();
2359 catch(const String& str)
2362 // synfig::error(str);
2363 return Canvas::Handle();
2365 return Canvas::Handle();