1 /* === S Y N F I G ========================================================= */
2 /*! \file loadcanvas.cpp
5 ** $Id: loadcanvas.cpp,v 1.3 2005/01/04 23:40:44 darco Exp $
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 // If there is a priority, then grab it and remove
1605 // it from the timecode
1606 if(timecode[0]=='p')
1608 //priority=timecode[1]-'0';
1609 //timecode=String(timecode.begin()+3,timecode.end());
1610 int space=timecode.find_first_of(' ');
1611 priority=atoi(String(timecode,1,space-1).c_str());
1612 timecode=String(timecode.begin()+space+1,timecode.end());
1613 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1616 timing_info.push_back(
1622 true, // Mark as a "on" activepoint
1627 if(iter==begin_sequence.end())
1628 begin_sequence.clear();
1630 begin_sequence=String(iter+1,begin_sequence.end());
1634 while(!end_sequence.empty())
1636 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1637 String timecode(end_sequence.begin(), iter);
1640 // If there is a priority, then grab it and remove
1641 // it from the timecode
1642 if(timecode[0]=='p')
1644 //priority=timecode[1]-'0';
1645 //timecode=String(timecode.begin()+3,timecode.end());
1646 int space=timecode.find_first_of(' ');
1647 priority=atoi(String(timecode,1,space-1).c_str());
1648 timecode=String(timecode.begin()+space+1,timecode.end());
1649 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1652 timing_info.push_back(
1658 false, // Mark as a "off" activepoint
1662 if(iter==end_sequence.end())
1663 end_sequence.clear();
1665 end_sequence=String(iter+1,end_sequence.end());
1671 if(child->get_attribute("use"))
1673 string id=child->get_attribute("use")->get_value();
1676 list_entry.value_node=canvas->surefind_value_node(id);
1678 catch(Exception::IDNotFound)
1680 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1686 xmlpp::Element::NodeList list = child->get_children();
1687 xmlpp::Element::NodeList::iterator iter;
1689 // Search for the first non-text XML element
1690 for(iter = list.begin(); iter != list.end(); ++iter)
1691 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1693 if(iter==list.end())
1695 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1699 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1701 if(!list_entry.value_node)
1702 error((*iter),"Parse of ValueNode failed");
1704 // \todo do a search for more elements and warn if they are found
1708 value_node->add(list_entry);
1709 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1712 error_unexpected_element(child,child->get_name());
1718 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1720 handle<ValueNode> value_node;
1725 if(element->get_attribute("guid"))
1727 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1728 value_node=guid_cast<ValueNode>(guid);
1733 // If ValueBase::ident_type() recognises the name, then we know it's a ValueBase
1734 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1736 ValueBase data=parse_value(element,canvas);
1738 if(!data.is_valid())
1740 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1744 // We want to convert this ValueBase into a
1745 // ValueNode_Const. That way, we can treat the
1746 // ID like any other Datanode. Think of this
1747 // as a shorthand for creating constant ValueNodes.
1749 value_node=ValueNode_Const::create(data);
1752 if(element->get_name()=="hermite" || element->get_name()=="animated")
1753 value_node=parse_animated(element,canvas);
1755 if(element->get_name()=="subtract")
1756 value_node=parse_subtract(element,canvas);
1758 if(element->get_name()=="composite")
1759 value_node=parse_composite(element,canvas);
1761 if(element->get_name()=="dynamic_list")
1762 value_node=parse_dynamic_list(element,canvas);
1764 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1765 value_node=parse_dynamic_list(element,canvas);
1767 if(element->get_name()=="timed_swap")
1768 value_node=parse_timedswap(element,canvas);
1770 if(LinkableValueNode::book().count(element->get_name()))
1771 value_node=parse_linkable_value_node(element,canvas);
1773 if(element->get_name()=="canvas")
1774 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1777 error_unexpected_element(element,element->get_name());
1778 error(element, "Expected a ValueNode");
1782 value_node->set_root_canvas(canvas->get_root());
1785 // If we were successful, and our element has
1786 // an ID attribute, go ahead and add it to the
1788 if(value_node && element->get_attribute("id"))
1790 string id=element->get_attribute("id")->get_value();
1792 //value_node->set_id(id);
1794 // If there is already a value_node in the list
1795 // with the same ID, then that is an error
1796 try { canvas->add_value_node(value_node,id); }
1797 catch(Exception::BadLinkName)
1799 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1802 catch(Exception::IDAlreadyExists)
1804 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1809 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1813 value_node->set_guid(guid);
1818 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1820 assert(element->get_name()=="defs");
1821 xmlpp::Element::NodeList list = element->get_children();
1822 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1824 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1828 if(child->get_name()=="canvas")
1829 parse_canvas(child, canvas);
1831 parse_value_node(child,canvas);
1836 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1839 assert(element->get_name()=="layer");
1840 Layer::Handle layer;
1842 if(!element->get_attribute("type"))
1844 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1845 return Layer::Handle();
1848 layer=Layer::create(element->get_attribute("type")->get_value());
1849 layer->set_canvas(canvas);
1851 if(element->get_attribute("group"))
1853 layer->add_to_group(
1854 element->get_attribute("group")->get_value()
1858 // Handle the version attribute
1859 if(element->get_attribute("version"))
1861 String version(element->get_attribute("version")->get_value());
1862 if(version>layer->get_version())
1863 warning(element,_("Installed layer version is larger than layer version in file"));
1864 if(version!=layer->get_version())
1865 layer->set_version(version);
1868 // Handle the description
1869 if(element->get_attribute("desc"))
1870 layer->set_description(element->get_attribute("desc")->get_value());
1872 if(element->get_attribute("active"))
1873 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1875 xmlpp::Element::NodeList list = element->get_children();
1876 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1878 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1882 if(child->get_name()=="name")
1883 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1885 if(child->get_name()=="desc")
1886 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1888 if(child->get_name()=="param")
1890 xmlpp::Element::NodeList list = child->get_children();
1892 if(!child->get_attribute("name"))
1894 error(child,_("Missing \"name\" attribute for <param>."));
1898 String param_name=child->get_attribute("name")->get_value();
1900 if(child->get_attribute("use"))
1902 // If the "use" attribute is used, then the
1903 // element should be empty. Warn the user if
1904 // we find otherwise.
1906 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1908 String str= child->get_attribute("use")->get_value();
1910 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1912 if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1913 error((*iter),_("Layer rejected canvas link"));
1918 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1920 // Assign the value_node to the dynamic parameter list
1921 layer->connect_dynamic_param(param_name,value_node);
1923 catch(Exception::IDNotFound)
1925 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1931 xmlpp::Element::NodeList::iterator iter;
1933 // Search for the first non-text XML element
1934 for(iter = list.begin(); iter != list.end(); ++iter)
1935 if(dynamic_cast<xmlpp::Element*>(*iter))
1937 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1939 if(iter==list.end())
1941 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1945 // If we recognise the element name as a
1946 // ValueBase, then treat is at one
1947 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1949 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1951 if(!data.is_valid())
1953 error((*iter),_("Bad data for <param>"));
1957 // Set the layer's parameter, and make sure that
1958 // the layer liked it
1959 if(!layer->set_param(param_name,data))
1961 warning((*iter),_("Layer rejected value for <param>"));
1965 else // ... otherwise, we assume that it is a ValueNode
1967 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1971 error((*iter),_("Bad data for <param>"));
1975 // Assign the value_node to the dynamic parameter list
1976 layer->connect_dynamic_param(param_name,value_node);
1979 // Warn if there is trash after the param value
1980 for(iter++; iter != list.end(); ++iter)
1981 if(dynamic_cast<xmlpp::Element*>(*iter))
1982 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1986 error_unexpected_element(child,child->get_name());
1989 layer->reset_version();
1994 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1997 if(element->get_name()!="canvas")
1999 error_unexpected_element(element,element->get_name(),"canvas");
2000 return Canvas::Handle();
2002 Canvas::Handle canvas;
2006 if(parent && (element->get_attribute("id") || inline_))
2010 canvas=Canvas::create_inline(parent);
2016 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
2020 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
2023 canvas->rend_desc().clear_flags();
2027 canvas=Canvas::create();
2028 if(filename=="/dev/stdin")
2029 canvas->set_file_name("./stdin.sif");
2031 canvas->set_file_name(filename);
2032 canvas->rend_desc().clear_flags();
2035 if(element->get_attribute("guid"))
2037 GUID guid(element->get_attribute("guid")->get_value());
2038 if(guid_cast<Canvas>(guid))
2039 return guid_cast<Canvas>(guid);
2041 canvas->set_guid(guid);
2044 if(element->get_attribute("width"))
2045 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
2047 if(element->get_attribute("height"))
2048 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
2050 if(element->get_attribute("xres"))
2051 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
2053 if(element->get_attribute("yres"))
2054 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
2057 if(element->get_attribute("fps"))
2058 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
2060 if(element->get_attribute("start-time"))
2061 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2063 if(element->get_attribute("begin-time"))
2064 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2066 if(element->get_attribute("end-time"))
2067 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
2069 if(element->get_attribute("antialias"))
2070 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
2072 if(element->get_attribute("view-box"))
2074 string values=element->get_attribute("view-box")->get_value();
2078 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
2079 values=string(values.begin()+values.find(' ')+1,values.end());
2080 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
2081 values=string(values.begin()+values.find(' ')+1,values.end());
2082 br[0]=atof(string(values.data(),values.find(' ')).c_str());
2083 values=string(values.begin()+values.find(' ')+1,values.end());
2084 br[1]=atof(values.c_str());
2086 canvas->rend_desc().set_tl(tl);
2087 canvas->rend_desc().set_br(br);
2090 if(element->get_attribute("bgcolor"))
2092 string values=element->get_attribute("bgcolor")->get_value();
2095 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
2096 values=string(values.begin()+values.find(' ')+1,values.end());
2098 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
2099 values=string(values.begin()+values.find(' ')+1,values.end());
2101 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
2102 values=string(values.begin()+values.find(' ')+1,values.end());
2104 bg.set_a(atof(values.c_str()));
2106 canvas->rend_desc().set_bg_color(bg);
2109 if(element->get_attribute("focus"))
2111 string values=element->get_attribute("focus")->get_value();
2114 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
2115 values=string(values.begin()+values.find(' ')+1,values.end());
2116 focus[1]=atof(values.c_str());
2118 canvas->rend_desc().set_focus(focus);
2121 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2123 xmlpp::Element::NodeList list = element->get_children();
2124 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2126 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
2129 if(child->get_name()=="defs")
2131 if(canvas->is_inline())
2132 error(child,_("Inline canvas cannot have a <defs> section"));
2133 parse_canvas_defs(child, canvas);
2136 if(child->get_name()=="keyframe")
2138 if(canvas->is_inline())
2140 warning(child,_("Inline canvas cannot have keyframes"));
2144 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2145 canvas->keyframe_list().sync();
2148 if(child->get_name()=="meta")
2150 if(canvas->is_inline())
2152 warning(child,_("Inline canvases cannot have metadata"));
2156 String name,content;
2158 if(!child->get_attribute("name"))
2160 warning(child,_("<meta> must have a name"));
2164 if(!child->get_attribute("content"))
2166 warning(child,_("<meta> must have content"));
2170 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2172 else if(child->get_name()=="name")
2174 xmlpp::Element::NodeList list = child->get_children();
2176 // If we don't have any name, warn
2178 warning(child,_("blank \"name\" entitity"));
2181 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2182 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2183 canvas->set_name(tmp);
2186 if(child->get_name()=="desc")
2189 xmlpp::Element::NodeList list = child->get_children();
2191 // If we don't have any description, warn
2193 warning(child,_("blank \"desc\" entitity"));
2196 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2197 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2198 canvas->set_description(tmp);
2201 if(child->get_name()=="author")
2204 xmlpp::Element::NodeList list = child->get_children();
2206 // If we don't have any description, warn
2208 warning(child,_("blank \"author\" entitity"));
2211 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2212 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2213 canvas->set_author(tmp);
2216 if(child->get_name()=="layer")
2218 //if(canvas->is_inline())
2219 // canvas->push_front(parse_layer(child,canvas->parent()));
2221 canvas->push_front(parse_layer(child,canvas));
2224 error_unexpected_element(child,child->get_name());
2227 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2231 if(canvas->value_node_list().placeholder_count())
2233 error(element,strprintf(_("Canvas %s has undefined ValueNodes"),canvas->get_id().c_str()));
2240 CanvasParser::parse_from_file(const String &file)
2242 return parse_from_file_as(file,file);
2246 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2248 CHECK_EXPIRE_TIME();
2251 ChangeLocale change_locale(LC_NUMERIC, "C");
2252 String file(unix_to_local_path(file_));
2253 String as(unix_to_local_path(as_));
2255 if(get_open_canvas_map().count(etl::absolute_path(as)))
2256 return get_open_canvas_map()[etl::absolute_path(as)];
2260 xmlpp::DomParser parser(file);
2263 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2264 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2265 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2266 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2270 const ValueNodeList& value_node_list(canvas->value_node_list());
2273 ValueNodeList::const_iterator iter;
2274 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2276 ValueNode::Handle value_node(*iter);
2277 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2279 canvas->remove_value_node(value_node);
2287 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2288 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2289 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2290 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2291 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2292 catch(const std::exception& ex)
2294 synfig::error("Standard Exception: "+String(ex.what()));
2295 return Canvas::Handle();
2297 catch(const String& str)
2300 // synfig::error(str);
2301 return Canvas::Handle();
2303 return Canvas::Handle();
2307 CanvasParser::parse_from_string(const String &data)
2309 CHECK_EXPIRE_TIME();
2313 ChangeLocale change_locale(LC_NUMERIC, "C");
2314 filename=_("<INTERNAL>");
2316 xmlpp::DomParser parser;
2317 parser.parse_memory(data);
2318 xmlpp::Element *root=parser.get_document()->get_root_node();
2321 Canvas::Handle canvas(parse_canvas(root));
2322 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2323 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2325 const ValueNodeList& value_node_list(canvas->value_node_list());
2327 ValueNodeList::const_iterator iter;
2328 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2330 ValueNode::Handle value_node(*iter);
2331 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2333 canvas->remove_value_node(value_node);
2341 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2342 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2343 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2344 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2345 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2346 catch(const std::exception& ex)
2348 synfig::error("Standard Exception: "+String(ex.what()));
2349 return Canvas::Handle();
2351 catch(const String& str)
2354 // synfig::error(str);
2355 return Canvas::Handle();
2357 return Canvas::Handle();