1 /* === S Y N F I G ========================================================= */
2 /*! \file loadcanvas.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
37 #include <libxml++/libxml++.h>
42 #include "loadcanvas.h"
43 #include "valuenode.h"
44 #include "valuenode_subtract.h"
45 #include "valuenode_animated.h"
46 #include "valuenode_composite.h"
47 #include "valuenode_const.h"
48 #include "valuenode_linear.h"
49 #include "valuenode_dynamiclist.h"
50 #include "valuenode_reference.h"
51 #include "valuenode_scale.h"
52 #include "valuenode_timedswap.h"
53 #include "valuenode_twotone.h"
54 #include "valuenode_stripes.h"
55 #include "valuenode_segcalctangent.h"
56 #include "valuenode_segcalcvertex.h"
57 #include "valuenode_bline.h"
62 #include "exception.h"
67 #include <sigc++/bind.h>
71 /* === U S I N G =========================================================== */
74 using namespace synfig;
81 test_class() { assert(!bleh); bleh++; synfig::info("test_class: initi: %d",bleh); }
82 ~test_class() { assert(bleh); synfig::info("test_class: uninit: %d",bleh); bleh--; }
84 int test_class::bleh(0);
86 test_class test_class_instance;
89 /* === M A C R O S ========================================================= */
91 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
93 /* === P R O C E D U R E S ================================================= */
95 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
97 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
100 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
101 return *open_canvas_map_;
104 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
106 static void _canvas_file_name_changed(Canvas *x)
108 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
110 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
113 assert(iter!=get_open_canvas_map().end());
114 if(iter==get_open_canvas_map().end())
116 get_open_canvas_map().erase(iter->first);
117 get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
122 synfig::open_canvas(const String &filename)
126 parser.set_allow_errors(true);
128 Canvas::Handle canvas=parser.parse_from_file(filename);
130 if(parser.error_count())
131 return Canvas::Handle();
137 synfig::open_canvas_as(const String &filename,const String &as)
141 parser.set_allow_errors(true);
143 Canvas::Handle canvas=parser.parse_from_file_as(filename,as);
145 if(parser.error_count())
146 return Canvas::Handle();
152 synfig::string_to_canvas(const String &data)
156 parser.set_allow_errors(true);
158 Canvas::Handle canvas=parser.parse_from_string(data);
160 if(parser.error_count())
161 return Canvas::Handle();
166 /* === M E T H O D S ======================================================= */
169 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
171 error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
175 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
177 error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
181 CanvasParser::warning(xmlpp::Node *element, const String &text)
183 string str=strprintf("%s:<%s>:%d: warning: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
184 //synfig::warning(str);
187 if(total_warnings_>=max_warnings_)
188 fatal_error(element, _("Too many warnings"));
192 CanvasParser::error(xmlpp::Node *element, const String &text)
194 string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
197 throw runtime_error(str);
199 // synfig::error(str);
203 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
205 string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
206 throw runtime_error(str);
212 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
214 assert(element->get_name()=="keyframe");
216 if(!element->get_attribute("time"))
218 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
222 Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
225 if(element->get_children().empty())
228 if(element->get_child_text()->get_content().empty())
231 ret.set_description(element->get_child_text()->get_content());
238 CanvasParser::parse_real(xmlpp::Element *element)
240 assert(element->get_name()=="real");
242 if(!element->get_children().empty())
243 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
245 if(!element->get_attribute("value"))
247 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
251 string val=element->get_attribute("value")->get_value();
253 return atof(val.c_str());
257 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
259 assert(element->get_name()=="time");
261 if(!element->get_children().empty())
262 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
264 if(!element->get_attribute("value"))
266 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
270 string val=element->get_attribute("value")->get_value();
272 return Time(val,canvas->rend_desc().get_frame_rate());
276 CanvasParser::parse_integer(xmlpp::Element *element)
278 assert(element->get_name()=="integer");
280 if(!element->get_children().empty())
281 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
283 if(!element->get_attribute("value"))
285 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
289 string val=element->get_attribute("value")->get_value();
291 return atoi(val.c_str());
294 // see 'minor hack' at the end of parse_vector() below
295 // making this 'static' to give it file local scope
296 // stops it working (where working means working around
298 Vector &canvas_parser_vector_id(Vector &vector)
304 CanvasParser::parse_vector(xmlpp::Element *element)
306 assert(element->get_name()=="vector");
308 if(element->get_children().empty())
310 error(element, "Undefined value in <vector>");
316 xmlpp::Element::NodeList list = element->get_children();
317 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
319 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
323 if(child->get_name()=="x")
325 if(child->get_children().empty())
327 error(element, "Undefined value in <x>");
330 vect[0]=atof(child->get_child_text()->get_content().c_str());
333 if(child->get_name()=="y")
335 if(child->get_children().empty())
337 error(element, "Undefined value in <y>");
340 vect[1]=atof(child->get_child_text()->get_content().c_str());
343 error_unexpected_element(child,child->get_name());
345 // Minor hack - gcc 4.1.2 and earlier think that we're not using
346 // 'vect' and optimize it out at -O2 and higher. This convinces
347 // them that we are really using it.
348 return canvas_parser_vector_id(vect);
349 // When the bug is fixed, we can just do this instead:
354 CanvasParser::parse_color(xmlpp::Element *element)
356 assert(element->get_name()=="color");
358 if(element->get_children().empty())
360 error(element, "Undefined value in <color>");
366 xmlpp::Element::NodeList list = element->get_children();
367 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
369 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
373 if(child->get_name()=="r")
375 if(child->get_children().empty())
377 error(element, "Undefined value in <r>");
380 color.set_r(atof(child->get_child_text()->get_content().c_str()));
383 if(child->get_name()=="g")
385 if(child->get_children().empty())
387 error(element, "Undefined value in <g>");
390 color.set_g(atof(child->get_child_text()->get_content().c_str()));
393 if(child->get_name()=="b")
395 if(child->get_children().empty())
397 error(element, "Undefined value in <b>");
400 color.set_b(atof(child->get_child_text()->get_content().c_str()));
403 if(child->get_name()=="a")
405 if(child->get_children().empty())
407 error(element, "Undefined value in <a>");
410 color.set_a(atof(child->get_child_text()->get_content().c_str()));
413 error_unexpected_element(child,child->get_name());
420 CanvasParser::parse_string(xmlpp::Element *element)
422 assert(element->get_name()=="string");
424 if(element->get_children().empty())
426 warning(element, "Undefined value in <string>");
427 return synfig::String();
430 if(element->get_child_text()->get_content().empty())
432 warning(element, "Content element of <string> appears to be empty");
433 return synfig::String();
436 return element->get_child_text()->get_content();
440 CanvasParser::parse_bool(xmlpp::Element *element)
442 assert(element->get_name()=="bool");
444 if(!element->get_children().empty())
445 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
447 if(!element->get_attribute("value"))
449 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
453 string val=element->get_attribute("value")->get_value();
455 if(val=="true" || val=="1")
457 if(val=="false" || val=="0")
460 error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
466 CanvasParser::parse_gradient(xmlpp::Element *node)
468 assert(node->get_name()=="gradient");
471 xmlpp::Element::NodeList list = node->get_children();
472 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
474 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
479 Gradient::CPoint cpoint;
480 cpoint.color=parse_color(child);
482 if(!child->get_attribute("pos"))
484 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
488 cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
490 ret.push_back(cpoint);
498 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
500 vector<ValueBase> value_list;
502 xmlpp::Element::NodeList list = element->get_children();
503 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
505 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
510 value_list.push_back(parse_value(child,canvas));
511 if(!value_list.back().is_valid())
513 value_list.pop_back();
514 error(child,"Bad ValueBase");
523 CanvasParser::parse_segment(xmlpp::Element *element)
525 assert(element->get_name()=="segment");
527 if(element->get_children().empty())
529 error(element, "Undefined value in <segment>");
535 xmlpp::Element::NodeList list = element->get_children();
536 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
538 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
542 if(child->get_name()=="p1")
544 xmlpp::Element::NodeList list = child->get_children();
545 xmlpp::Element::NodeList::iterator iter;
547 // Search for the first non-text XML element
548 for(iter = list.begin(); iter != list.end(); ++iter)
549 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
553 error(element, "Undefined value in <p1>");
557 if((*iter)->get_name()!="vector")
559 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
563 seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
566 if(child->get_name()=="t1")
568 xmlpp::Element::NodeList list = child->get_children();
569 xmlpp::Element::NodeList::iterator iter;
571 // Search for the first non-text XML element
572 for(iter = list.begin(); iter != list.end(); ++iter)
573 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
577 error(element, "Undefined value in <t1>");
581 if((*iter)->get_name()!="vector")
583 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
587 seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
590 if(child->get_name()=="p2")
592 xmlpp::Element::NodeList list = child->get_children();
593 xmlpp::Element::NodeList::iterator iter;
595 // Search for the first non-text XML element
596 for(iter = list.begin(); iter != list.end(); ++iter)
597 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
601 error(element, "Undefined value in <p2>");
605 if((*iter)->get_name()!="vector")
607 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
611 seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
614 if(child->get_name()=="t2")
616 xmlpp::Element::NodeList list = child->get_children();
617 xmlpp::Element::NodeList::iterator iter;
619 // Search for the first non-text XML element
620 for(iter = list.begin(); iter != list.end(); ++iter)
621 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
625 error(element, "Undefined value in <t2>");
629 if((*iter)->get_name()!="vector")
631 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
635 seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
638 error_unexpected_element(child,child->get_name());
644 CanvasParser::parse_bline_point(xmlpp::Element *element)
646 assert(element->get_name()=="bline_point");
647 if(element->get_children().empty())
649 error(element, "Undefined value in <bline_point>");
654 ret.set_split_tangent_flag(false);
656 xmlpp::Element::NodeList list = element->get_children();
657 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
659 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
664 if(child->get_name()[0]=='v' || child->get_name()=="p1")
666 xmlpp::Element::NodeList list = child->get_children();
667 xmlpp::Element::NodeList::iterator iter;
669 // Search for the first non-text XML element
670 for(iter = list.begin(); iter != list.end(); ++iter)
671 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
675 error(element, "Undefined value in <vertex>");
679 if((*iter)->get_name()!="vector")
681 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
685 ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
689 if(child->get_name()=="t1" || child->get_name()=="tangent")
691 xmlpp::Element::NodeList list = child->get_children();
692 xmlpp::Element::NodeList::iterator iter;
694 // Search for the first non-text XML element
695 for(iter = list.begin(); iter != list.end(); ++iter)
696 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
700 error(element, "Undefined value in <t1>");
704 if((*iter)->get_name()!="vector")
706 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
710 ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
714 if(child->get_name()=="t2")
716 xmlpp::Element::NodeList list = child->get_children();
717 xmlpp::Element::NodeList::iterator iter;
719 // Search for the first non-text XML element
720 for(iter = list.begin(); iter != list.end(); ++iter)
721 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
725 error(element, "Undefined value in <t2>");
729 if((*iter)->get_name()!="vector")
731 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
735 ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
736 ret.set_split_tangent_flag(true);
740 if(child->get_name()=="width")
742 xmlpp::Element::NodeList list = child->get_children();
743 xmlpp::Element::NodeList::iterator iter;
745 // Search for the first non-text XML element
746 for(iter = list.begin(); iter != list.end(); ++iter)
747 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
751 error(element, "Undefined value in <width>");
755 if((*iter)->get_name()!="real")
757 error_unexpected_element((*iter),(*iter)->get_name(),"real");
761 ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
765 if(child->get_name()=="origin")
767 xmlpp::Element::NodeList list = child->get_children();
768 xmlpp::Element::NodeList::iterator iter;
770 // Search for the first non-text XML element
771 for(iter = list.begin(); iter != list.end(); ++iter)
772 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
776 error(element, "Undefined value in <origin>");
780 if((*iter)->get_name()!="real")
782 error_unexpected_element((*iter),(*iter)->get_name(),"real");
786 ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
789 error_unexpected_element(child,child->get_name());
795 CanvasParser::parse_angle(xmlpp::Element *element)
797 assert(element->get_name()=="angle");
799 if(!element->get_children().empty())
800 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
802 if(!element->get_attribute("value"))
804 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
808 string val=element->get_attribute("value")->get_value();
810 return Angle::deg(atof(val.c_str()));
814 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
816 if(element->get_name()=="real")
817 return parse_real(element);
819 if(element->get_name()=="time")
820 return parse_time(element,canvas);
822 if(element->get_name()=="integer")
823 return parse_integer(element);
825 if(element->get_name()=="string")
826 return parse_string(element);
828 if(element->get_name()=="vector")
829 return parse_vector(element);
831 if(element->get_name()=="color")
832 return parse_color(element);
834 if(element->get_name()=="segment")
835 return parse_segment(element);
837 if(element->get_name()=="list")
838 return parse_list(element,canvas);
840 if(element->get_name()=="gradient")
841 return parse_gradient(element);
843 if(element->get_name()=="bool")
844 return parse_bool(element);
846 //if(element->get_name()=="canvas")
847 // return parse_canvas(element,canvas,true); // inline canvas
849 if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
850 return parse_angle(element);
852 if(element->get_name()=="bline_point")
853 return parse_bline_point(element);
855 if(element->get_name()=="canvas")
856 return ValueBase(parse_canvas(element,canvas,true));
860 error_unexpected_element(element,element->get_name());
869 ValueNode_Animated::Handle
870 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
872 assert(element->get_name()=="hermite" || element->get_name()=="animated");
874 if(!element->get_attribute("type"))
876 error(element,"Missing attribute \"type\" in <animated>");
877 return ValueNode_Animated::Handle();
880 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
884 error(element,"Bad type in <animated>");
885 return ValueNode_Animated::Handle();
888 ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
892 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_name(type).c_str()));
893 return ValueNode_Animated::Handle();
896 value_node->set_root_canvas(canvas->get_root());
898 xmlpp::Element::NodeList list = element->get_children();
899 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
901 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
905 if(child->get_name()=="waypoint")
907 if(!child->get_attribute("time"))
909 error(child,_("<waypoint> is missing attribute \"time\""));
913 Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
916 ValueNode::Handle waypoint_value_node;
917 xmlpp::Element::NodeList list = child->get_children();
919 if(child->get_attribute("use"))
922 warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
924 // the waypoint might look like this, in which case we won't find "mycanvas" in the list of valuenodes, 'cos it's a canvas
926 // <animated type="canvas">
927 // <waypoint time="0s" use="mycanvas"/>
929 if (type==ValueBase::TYPE_CANVAS)
930 waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value()));
932 waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
936 if(child->get_children().empty())
938 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
942 xmlpp::Element::NodeList::iterator iter;
944 // Search for the first non-text XML element
945 for(iter = list.begin(); iter != list.end(); ++iter)
946 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
950 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
954 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
957 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
961 error(child,_("Bad data for <waypoint>"));
965 if(!waypoint_value_node)
967 error(child,_("Bad data for <waypoint>"));
971 /*! HACK -- This is a temporary fix to help repair some
972 ** weirdness that is currently going on (10-21-2004).
973 ** This short circuits the linking of waypoints,
974 ** a feature which is so obscure that we can get
975 ** away with something like this pretty easily.
977 waypoint_value_node=waypoint_value_node->clone();
979 // Warn if there is trash after the param value
980 for(iter++; iter != list.end(); ++iter)
981 if(dynamic_cast<xmlpp::Element*>(*iter))
982 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
987 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
989 if(child->get_attribute("tension"))
991 synfig::String str(child->get_attribute("tension")->get_value());
992 waypoint->set_tension(atof(str.c_str()));
994 if(child->get_attribute("temporal-tension"))
996 synfig::String str(child->get_attribute("temporal-tension")->get_value());
997 waypoint->set_time_tension(atof(str.c_str()));
999 if(child->get_attribute("continuity"))
1001 synfig::String str(child->get_attribute("continuity")->get_value());
1002 waypoint->set_continuity(atof(str.c_str()));
1004 if(child->get_attribute("bias"))
1006 synfig::String str(child->get_attribute("bias")->get_value());
1007 waypoint->set_bias(atof(str.c_str()));
1010 if(child->get_attribute("before"))
1012 string val=child->get_attribute("before")->get_value();
1014 waypoint->set_before(INTERPOLATION_HALT);
1015 else if(val=="constant")
1016 waypoint->set_before(INTERPOLATION_CONSTANT);
1017 else if(val=="linear")
1018 waypoint->set_before(INTERPOLATION_LINEAR);
1019 else if(val=="manual")
1020 waypoint->set_before(INTERPOLATION_MANUAL);
1021 else if(val=="auto")
1022 waypoint->set_before(INTERPOLATION_TCB);
1024 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1027 if(child->get_attribute("after"))
1029 string val=child->get_attribute("after")->get_value();
1031 waypoint->set_after(INTERPOLATION_HALT);
1032 else if(val=="constant")
1033 waypoint->set_after(INTERPOLATION_CONSTANT);
1034 else if(val=="linear")
1035 waypoint->set_after(INTERPOLATION_LINEAR);
1036 else if(val=="manual")
1037 waypoint->set_after(INTERPOLATION_MANUAL);
1038 else if(val=="auto")
1039 waypoint->set_after(INTERPOLATION_TCB);
1041 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1044 catch(Exception::BadTime x)
1046 warning(child,x.what());
1052 error_unexpected_element(child,child->get_name());
1055 // in canvas version 0.1, angles used to wrap, so to get from -179
1056 // degrees to 180 degrees meant a 1 degree change
1057 // in canvas version 0.2 they don't, so that's a 359 degree change
1059 // when loading a version 0.1 canvas, modify constant angle
1060 // waypoints to that they are within 180 degrees of the previous
1062 if (type == ValueBase::TYPE_ANGLE)
1064 Canvas::Handle parent = canvas;
1065 while (!parent->is_root())
1066 parent = parent->parent();
1068 if (parent->get_version() == "0.1")
1071 Real angle, prev = 0;
1072 WaypointList &wl = value_node->waypoint_list();
1073 for (WaypointList::iterator iter = wl.begin(); iter != wl.end(); iter++)
1075 angle = Angle::deg(iter->get_value(iter->get_time()).get(Angle())).get();
1078 else if (iter->get_value_node()->get_name() == "constant")
1079 if (angle - prev > 180)
1081 while (angle - prev > 180) angle -= 360;
1082 iter->set_value(Angle::deg(angle));
1084 else if (prev - angle > 180)
1086 while (prev - angle > 180) angle += 360;
1087 iter->set_value(Angle::deg(angle));
1094 value_node->changed();
1098 etl::handle<LinkableValueNode>
1099 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1101 handle<LinkableValueNode> value_node;
1102 ValueBase::Type type;
1104 // Determine the type
1105 if(element->get_attribute("type"))
1107 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1111 error(element,"Bad type in ValueNode");
1117 error(element,"Missing type in ValueNode");
1121 value_node=LinkableValueNode::create(element->get_name(),type);
1125 error(element,"Unknown ValueNode type "+element->get_name());
1129 if(value_node->get_type()!=type)
1131 error(element,"ValueNode did not accept type");
1135 value_node->set_root_canvas(canvas->get_root());
1138 for(i=0;i<value_node->link_count();i++)
1140 if(element->get_attribute(value_node->link_name(i)))
1142 String id(element->get_attribute(value_node->link_name(i))->get_value());
1144 if(!value_node->set_link(i,
1145 canvas->surefind_value_node(
1149 ) 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()));
1151 catch(Exception::IDNotFound)
1153 error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1155 catch(Exception::FileNotFound)
1157 error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1161 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1162 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
1169 xmlpp::Element::NodeList list = element->get_children();
1170 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1172 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1177 int index=value_node->get_link_index_from_name(child->get_name());
1179 xmlpp::Element::NodeList list = child->get_children();
1180 xmlpp::Element::NodeList::iterator iter;
1182 // Search for the first non-text XML element
1183 for(iter = list.begin(); iter != list.end(); ++iter)
1184 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1186 if(iter==list.end())
1188 error(child,_("element is missing its contents"));
1192 ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1196 error((*iter),"Parse of ValueNode failed");
1199 if(!value_node->set_link(index,link))
1201 //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));
1202 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));
1205 // \todo do a search for more elements and warn if they are found
1208 catch(Exception::BadLinkName)
1210 error_unexpected_element(child,child->get_name());
1214 error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1222 handle<ValueNode_Composite>
1223 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1225 assert(element->get_name()=="composite");
1227 if(!element->get_attribute("type"))
1229 error(element,"Missing attribute \"type\" in <composite>");
1230 return handle<ValueNode_Composite>();
1233 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1237 error(element,"Bad type in <composite>");
1238 return handle<ValueNode_Composite>();
1241 handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1242 handle<ValueNode> c[6];
1246 error(element,strprintf(_("Unable to create <composite>")));
1247 return handle<ValueNode_Composite>();
1252 for(i=0;i<value_node->link_count();i++)
1254 string name=strprintf("c%d",i+1);
1257 error(element,name+" was already defined in <composite>");
1260 if(element->get_attribute(name))
1262 c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1265 if(!value_node->set_link(i,c[i]))
1267 error(element,'"'+name+"\" attribute in <composite> has bad type");
1271 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1275 xmlpp::Element::NodeList list = element->get_children();
1276 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1278 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1282 for(i=0;i<value_node->link_count();i++)
1284 string name=strprintf("c%d",i+1);
1285 if(child->get_name()==name)
1289 error(child,name+" was already defined in <composite>");
1293 xmlpp::Element::NodeList list = child->get_children();
1294 xmlpp::Element::NodeList::iterator iter;
1296 // Search for the first non-text XML element
1297 for(iter = list.begin(); iter != list.end(); ++iter)
1298 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1300 if(iter==list.end())
1302 error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1306 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1310 error((*iter),"Parse of "+name+" ValueNode failed");
1314 if(!value_node->set_link(i,c[i]))
1316 error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1320 // \todo do a search for more elements and warn if they are found
1324 // somewhat of a hack, but it works
1325 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1328 switch(value_node->link_count())
1331 if(!value_node->get_link(0))
1333 error(element,"<composite> is missing parameters");
1334 return handle<ValueNode_Composite>();
1338 if(!value_node->get_link(0) ||!value_node->get_link(1))
1340 error(element,"<composite> is missing parameters");
1341 return handle<ValueNode_Composite>();
1345 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1347 error(element,"<composite> is missing parameters");
1348 return handle<ValueNode_Composite>();
1352 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1354 error(element,"<composite> is missing parameters");
1355 return handle<ValueNode_Composite>();
1362 // This will also parse a bline
1363 handle<ValueNode_DynamicList>
1364 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1366 assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1368 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1370 if(!element->get_attribute("type"))
1372 error(element,"Missing attribute \"type\" in <dynamic_list>");
1373 return handle<ValueNode_DynamicList>();
1376 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1380 error(element,"Bad type in <dynamic_list>");
1381 return handle<ValueNode_DynamicList>();
1384 handle<ValueNode_DynamicList> value_node;
1385 handle<ValueNode_BLine> bline_value_node;
1387 if(element->get_name()=="bline")
1389 value_node=bline_value_node=ValueNode_BLine::create();
1390 if(element->get_attribute("loop"))
1392 String loop=element->get_attribute("loop")->get_value();
1393 if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1394 bline_value_node->set_loop(true);
1396 bline_value_node->set_loop(false);
1401 value_node=ValueNode_DynamicList::create(type);
1405 error(element,strprintf(_("Unable to create <dynamic_list>")));
1406 return handle<ValueNode_DynamicList>();
1409 value_node->set_root_canvas(canvas->get_root());
1411 xmlpp::Element::NodeList list = element->get_children();
1412 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1414 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1418 if(child->get_name()=="entry")
1420 ValueNode_DynamicList::ListEntry list_entry;
1422 // Parse begin/end waypoints
1424 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1425 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1427 String begin_sequence;
1428 String end_sequence;
1430 ActivepointList &timing_info(list_entry.timing_info);
1432 if(child->get_attribute("begin"))
1433 begin_sequence=child->get_attribute("begin")->get_value();
1435 if(child->get_attribute("on"))
1436 begin_sequence=child->get_attribute("on")->get_value();
1438 if(child->get_attribute("end"))
1439 end_sequence=child->get_attribute("end")->get_value();
1441 if(child->get_attribute("off"))
1442 end_sequence=child->get_attribute("off")->get_value();
1444 // clear out any auto-start
1445 if(!begin_sequence.empty())
1446 timing_info.clear();
1449 while(!begin_sequence.empty())
1451 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1452 String timecode(begin_sequence.begin(), iter);
1455 // skip whitespace before checking for a priority
1456 while (isspace(timecode[0]))
1457 timecode=timecode.substr(1);
1459 // If there is a priority, then grab it and remove
1460 // it from the timecode
1461 if(timecode[0]=='p')
1463 //priority=timecode[1]-'0';
1464 //timecode=String(timecode.begin()+3,timecode.end());
1465 int space=timecode.find_first_of(' ');
1466 priority=atoi(String(timecode,1,space-1).c_str());
1467 timecode=String(timecode.begin()+space+1,timecode.end());
1468 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1471 timing_info.push_back(
1477 true, // Mark as a "on" activepoint
1482 if(iter==begin_sequence.end())
1483 begin_sequence.clear();
1485 begin_sequence=String(iter+1,begin_sequence.end());
1489 while(!end_sequence.empty())
1491 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1492 String timecode(end_sequence.begin(), iter);
1495 // skip whitespace before checking for a priority
1496 while (isspace(timecode[0]))
1497 timecode=timecode.substr(1);
1499 // If there is a priority, then grab it and remove
1500 // it from the timecode
1501 if(timecode[0]=='p')
1503 //priority=timecode[1]-'0';
1504 //timecode=String(timecode.begin()+3,timecode.end());
1505 int space=timecode.find_first_of(' ');
1506 priority=atoi(String(timecode,1,space-1).c_str());
1507 timecode=String(timecode.begin()+space+1,timecode.end());
1508 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1511 timing_info.push_back(
1517 false, // Mark as a "off" activepoint
1521 if(iter==end_sequence.end())
1522 end_sequence.clear();
1524 end_sequence=String(iter+1,end_sequence.end());
1530 if(child->get_attribute("use"))
1532 // \todo does this need to be able to read 'use="canvas"', like waypoints can now? (see 'surefind_canvas' in this file)
1533 string id=child->get_attribute("use")->get_value();
1536 list_entry.value_node=canvas->surefind_value_node(id);
1538 catch(Exception::IDNotFound)
1540 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1546 xmlpp::Element::NodeList list = child->get_children();
1547 xmlpp::Element::NodeList::iterator iter;
1549 // Search for the first non-text XML element
1550 for(iter = list.begin(); iter != list.end(); ++iter)
1551 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1553 if(iter==list.end())
1555 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1559 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1561 if(!list_entry.value_node)
1562 error((*iter),"Parse of ValueNode failed");
1564 // \todo do a search for more elements and warn if they are found
1568 value_node->add(list_entry);
1569 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1572 error_unexpected_element(child,child->get_name());
1578 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1580 handle<ValueNode> value_node;
1585 if(element->get_attribute("guid"))
1587 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1588 value_node=guid_cast<ValueNode>(guid);
1593 // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1594 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1596 ValueBase data=parse_value(element,canvas);
1598 if(!data.is_valid())
1600 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1604 // We want to convert this ValueBase into a
1605 // ValueNode_Const. That way, we can treat the
1606 // ID like any other Datanode. Think of this
1607 // as a shorthand for creating constant ValueNodes.
1609 value_node=ValueNode_Const::create(data);
1612 if(element->get_name()=="hermite" || element->get_name()=="animated")
1613 value_node=parse_animated(element,canvas);
1615 if(element->get_name()=="composite")
1616 value_node=parse_composite(element,canvas);
1618 if(element->get_name()=="dynamic_list")
1619 value_node=parse_dynamic_list(element,canvas);
1621 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1622 value_node=parse_dynamic_list(element,canvas);
1624 if(LinkableValueNode::book().count(element->get_name()))
1625 value_node=parse_linkable_value_node(element,canvas);
1627 if(element->get_name()=="canvas")
1628 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1631 error_unexpected_element(element,element->get_name());
1632 error(element, "Expected a ValueNode");
1636 value_node->set_root_canvas(canvas->get_root());
1639 // If we were successful, and our element has
1640 // an ID attribute, go ahead and add it to the
1642 if(value_node && element->get_attribute("id"))
1644 string id=element->get_attribute("id")->get_value();
1646 //value_node->set_id(id);
1648 // If there is already a value_node in the list
1649 // with the same ID, then that is an error
1650 try { canvas->add_value_node(value_node,id); }
1651 catch(Exception::BadLinkName)
1653 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1656 catch(Exception::IDAlreadyExists)
1658 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1663 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1667 value_node->set_guid(guid);
1672 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1674 assert(element->get_name()=="defs");
1675 xmlpp::Element::NodeList list = element->get_children();
1676 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1678 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1682 if(child->get_name()=="canvas")
1683 parse_canvas(child, canvas);
1685 parse_value_node(child,canvas);
1690 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1693 assert(element->get_name()=="layer");
1694 Layer::Handle layer;
1696 if(!element->get_attribute("type"))
1698 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1699 return Layer::Handle();
1702 layer=Layer::create(element->get_attribute("type")->get_value());
1703 layer->set_canvas(canvas);
1705 if(element->get_attribute("group"))
1707 layer->add_to_group(
1708 element->get_attribute("group")->get_value()
1712 // Handle the version attribute
1713 if(element->get_attribute("version"))
1715 String version(element->get_attribute("version")->get_value());
1716 if(version>layer->get_version())
1717 warning(element,_("Installed layer version is smaller than layer version in file"));
1718 if(version!=layer->get_version())
1719 layer->set_version(version);
1722 // Handle the description
1723 if(element->get_attribute("desc"))
1724 layer->set_description(element->get_attribute("desc")->get_value());
1726 if(element->get_attribute("active"))
1727 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1729 xmlpp::Element::NodeList list = element->get_children();
1730 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1732 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1736 if(child->get_name()=="name")
1737 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1739 if(child->get_name()=="desc")
1740 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1742 if(child->get_name()=="param")
1744 xmlpp::Element::NodeList list = child->get_children();
1746 if(!child->get_attribute("name"))
1748 error(child,_("Missing \"name\" attribute for <param>."));
1752 String param_name=child->get_attribute("name")->get_value();
1754 if(child->get_attribute("use"))
1756 // If the "use" attribute is used, then the
1757 // element should be empty. Warn the user if
1758 // we find otherwise.
1760 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1762 String str= child->get_attribute("use")->get_value();
1764 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1766 if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1767 error((*iter),_("Layer rejected canvas link"));
1772 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1774 // Assign the value_node to the dynamic parameter list
1775 layer->connect_dynamic_param(param_name,value_node);
1777 catch(Exception::IDNotFound)
1779 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1785 xmlpp::Element::NodeList::iterator iter;
1787 // Search for the first non-text XML element
1788 for(iter = list.begin(); iter != list.end(); ++iter)
1789 if(dynamic_cast<xmlpp::Element*>(*iter))
1791 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1793 if(iter==list.end())
1795 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1799 // If we recognize the element name as a
1800 // ValueBase, then treat is at one
1801 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1803 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1805 if(!data.is_valid())
1807 error((*iter),_("Bad data for <param>"));
1811 // Set the layer's parameter, and make sure that
1812 // the layer liked it
1813 if(!layer->set_param(param_name,data))
1815 warning((*iter),_("Layer rejected value for <param>"));
1819 else // ... otherwise, we assume that it is a ValueNode
1821 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1825 error((*iter),_("Bad data for <param>"));
1829 // Assign the value_node to the dynamic parameter list
1830 layer->connect_dynamic_param(param_name,value_node);
1833 // Warn if there is trash after the param value
1834 for(iter++; iter != list.end(); ++iter)
1835 if(dynamic_cast<xmlpp::Element*>(*iter))
1836 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1840 error_unexpected_element(child,child->get_name());
1843 layer->reset_version();
1848 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1851 if(element->get_name()!="canvas")
1853 error_unexpected_element(element,element->get_name(),"canvas");
1854 return Canvas::Handle();
1856 Canvas::Handle canvas;
1860 if(parent && (element->get_attribute("id") || inline_))
1864 canvas=Canvas::create_inline(parent);
1870 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1874 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1877 canvas->rend_desc().clear_flags();
1881 canvas=Canvas::create();
1882 if(filename=="/dev/stdin")
1883 canvas->set_file_name("./stdin.sif");
1885 canvas->set_file_name(filename);
1886 canvas->rend_desc().clear_flags();
1889 if(element->get_attribute("guid"))
1891 GUID guid(element->get_attribute("guid")->get_value());
1892 if(guid_cast<Canvas>(guid))
1893 return guid_cast<Canvas>(guid);
1895 canvas->set_guid(guid);
1898 if(element->get_attribute("version"))
1899 canvas->set_version(element->get_attribute("version")->get_value());
1901 if(element->get_attribute("width"))
1902 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1904 if(element->get_attribute("height"))
1905 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1907 if(element->get_attribute("xres"))
1908 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1910 if(element->get_attribute("yres"))
1911 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1914 if(element->get_attribute("fps"))
1915 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1917 if(element->get_attribute("start-time"))
1918 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1920 if(element->get_attribute("begin-time"))
1921 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1923 if(element->get_attribute("end-time"))
1924 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1926 if(element->get_attribute("antialias"))
1927 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1929 if(element->get_attribute("view-box"))
1931 string values=element->get_attribute("view-box")->get_value();
1935 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1936 values=string(values.begin()+values.find(' ')+1,values.end());
1937 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1938 values=string(values.begin()+values.find(' ')+1,values.end());
1939 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1940 values=string(values.begin()+values.find(' ')+1,values.end());
1941 br[1]=atof(values.c_str());
1943 canvas->rend_desc().set_tl(tl);
1944 canvas->rend_desc().set_br(br);
1947 if(element->get_attribute("bgcolor"))
1949 string values=element->get_attribute("bgcolor")->get_value();
1952 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1953 values=string(values.begin()+values.find(' ')+1,values.end());
1955 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1956 values=string(values.begin()+values.find(' ')+1,values.end());
1958 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1959 values=string(values.begin()+values.find(' ')+1,values.end());
1961 bg.set_a(atof(values.c_str()));
1963 canvas->rend_desc().set_bg_color(bg);
1966 if(element->get_attribute("focus"))
1968 string values=element->get_attribute("focus")->get_value();
1971 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1972 values=string(values.begin()+values.find(' ')+1,values.end());
1973 focus[1]=atof(values.c_str());
1975 canvas->rend_desc().set_focus(focus);
1978 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1980 xmlpp::Element::NodeList list = element->get_children();
1981 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1983 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1986 if(child->get_name()=="defs")
1988 if(canvas->is_inline())
1989 error(child,_("Inline canvas cannot have a <defs> section"));
1990 parse_canvas_defs(child, canvas);
1993 if(child->get_name()=="keyframe")
1995 if(canvas->is_inline())
1997 warning(child,_("Inline canvas cannot have keyframes"));
2001 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2002 canvas->keyframe_list().sync();
2005 if(child->get_name()=="meta")
2007 if(canvas->is_inline())
2009 warning(child,_("Inline canvases cannot have metadata"));
2013 String name,content;
2015 if(!child->get_attribute("name"))
2017 warning(child,_("<meta> must have a name"));
2021 if(!child->get_attribute("content"))
2023 warning(child,_("<meta> must have content"));
2027 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2029 else if(child->get_name()=="name")
2031 xmlpp::Element::NodeList list = child->get_children();
2033 // If we don't have any name, warn
2035 warning(child,_("blank \"name\" entity"));
2038 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2039 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2040 canvas->set_name(tmp);
2043 if(child->get_name()=="desc")
2046 xmlpp::Element::NodeList list = child->get_children();
2048 // If we don't have any description, warn
2050 warning(child,_("blank \"desc\" entity"));
2053 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2054 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2055 canvas->set_description(tmp);
2058 if(child->get_name()=="author")
2061 xmlpp::Element::NodeList list = child->get_children();
2063 // If we don't have any description, warn
2065 warning(child,_("blank \"author\" entity"));
2068 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2069 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2070 canvas->set_author(tmp);
2073 if(child->get_name()=="layer")
2075 //if(canvas->is_inline())
2076 // canvas->push_front(parse_layer(child,canvas->parent()));
2078 canvas->push_front(parse_layer(child,canvas));
2081 error_unexpected_element(child,child->get_name());
2084 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2088 if(canvas->value_node_list().placeholder_count())
2091 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2092 if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2094 if (nodes != "") nodes += ", ";
2095 nodes += "'" + (*iter)->get_id() + "'";
2097 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2098 canvas->get_id().c_str(),
2099 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2103 canvas->set_version(CURRENT_CANVAS_VERSION);
2108 CanvasParser::parse_from_file(const String &file)
2110 return parse_from_file_as(file,file);
2114 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2118 ChangeLocale change_locale(LC_NUMERIC, "C");
2119 String file(unix_to_local_path(file_));
2120 String as(unix_to_local_path(as_));
2122 if(get_open_canvas_map().count(etl::absolute_path(as)))
2123 return get_open_canvas_map()[etl::absolute_path(as)];
2127 xmlpp::DomParser parser(file);
2130 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2131 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2132 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2133 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2137 const ValueNodeList& value_node_list(canvas->value_node_list());
2140 ValueNodeList::const_iterator iter;
2141 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2143 ValueNode::Handle value_node(*iter);
2144 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2146 canvas->remove_value_node(value_node);
2154 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2155 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2156 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2157 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2158 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2159 catch(const std::exception& ex)
2161 synfig::error("Standard Exception: "+String(ex.what()));
2162 return Canvas::Handle();
2164 catch(const String& str)
2167 // synfig::error(str);
2168 return Canvas::Handle();
2170 return Canvas::Handle();
2174 CanvasParser::parse_from_string(const String &data)
2178 ChangeLocale change_locale(LC_NUMERIC, "C");
2179 filename=_("<INTERNAL>");
2181 xmlpp::DomParser parser;
2182 parser.parse_memory(data);
2183 xmlpp::Element *root=parser.get_document()->get_root_node();
2186 Canvas::Handle canvas(parse_canvas(root));
2187 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2188 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2190 const ValueNodeList& value_node_list(canvas->value_node_list());
2192 ValueNodeList::const_iterator iter;
2193 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2195 ValueNode::Handle value_node(*iter);
2196 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2198 canvas->remove_value_node(value_node);
2206 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2207 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2208 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2209 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2210 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2211 catch(const std::exception& ex)
2213 synfig::error("Standard Exception: "+String(ex.what()));
2214 return Canvas::Handle();
2216 catch(const String& str)
2219 // synfig::error(str);
2220 return Canvas::Handle();
2222 return Canvas::Handle();