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());
1054 value_node->changed();
1058 etl::handle<LinkableValueNode>
1059 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1061 handle<LinkableValueNode> value_node;
1062 ValueBase::Type type;
1064 // Determine the type
1065 if(element->get_attribute("type"))
1067 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1071 error(element,"Bad type in ValueNode");
1077 error(element,"Missing type in ValueNode");
1081 value_node=LinkableValueNode::create(element->get_name(),type);
1085 error(element,"Unknown ValueNode type "+element->get_name());
1089 if(value_node->get_type()!=type)
1091 error(element,"ValueNode did not accept type");
1095 value_node->set_root_canvas(canvas->get_root());
1098 for(i=0;i<value_node->link_count();i++)
1100 if(element->get_attribute(value_node->link_name(i)))
1102 String id(element->get_attribute(value_node->link_name(i))->get_value());
1104 if(!value_node->set_link(i,
1105 canvas->surefind_value_node(
1109 ) 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()));
1111 catch(Exception::IDNotFound)
1113 error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1115 catch(Exception::FileNotFound)
1117 error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1121 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1122 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
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));
1137 int index=value_node->get_link_index_from_name(child->get_name());
1139 xmlpp::Element::NodeList list = child->get_children();
1140 xmlpp::Element::NodeList::iterator iter;
1142 // Search for the first non-text XML element
1143 for(iter = list.begin(); iter != list.end(); ++iter)
1144 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1146 if(iter==list.end())
1148 error(child,_("element is missing its contents"));
1152 ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1156 error((*iter),"Parse of ValueNode failed");
1159 if(!value_node->set_link(index,link))
1161 //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));
1162 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));
1165 // \todo do a search for more elements and warn if they are found
1168 catch(Exception::BadLinkName)
1170 error_unexpected_element(child,child->get_name());
1174 error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1182 handle<ValueNode_Composite>
1183 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1185 assert(element->get_name()=="composite");
1187 if(!element->get_attribute("type"))
1189 error(element,"Missing attribute \"type\" in <composite>");
1190 return handle<ValueNode_Composite>();
1193 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1197 error(element,"Bad type in <composite>");
1198 return handle<ValueNode_Composite>();
1201 handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1202 handle<ValueNode> c[6];
1206 error(element,strprintf(_("Unable to create <composite>")));
1207 return handle<ValueNode_Composite>();
1212 for(i=0;i<value_node->link_count();i++)
1214 string name=strprintf("c%d",i+1);
1217 error(element,name+" was already defined in <composite>");
1220 if(element->get_attribute(name))
1222 c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1225 if(!value_node->set_link(i,c[i]))
1227 error(element,'"'+name+"\" attribute in <composite> has bad type");
1231 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1235 xmlpp::Element::NodeList list = element->get_children();
1236 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1238 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1242 for(i=0;i<value_node->link_count();i++)
1244 string name=strprintf("c%d",i+1);
1245 if(child->get_name()==name)
1249 error(child,name+" was already defined in <composite>");
1253 xmlpp::Element::NodeList list = child->get_children();
1254 xmlpp::Element::NodeList::iterator iter;
1256 // Search for the first non-text XML element
1257 for(iter = list.begin(); iter != list.end(); ++iter)
1258 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1260 if(iter==list.end())
1262 error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1266 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1270 error((*iter),"Parse of "+name+" ValueNode failed");
1274 if(!value_node->set_link(i,c[i]))
1276 error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1280 // \todo do a search for more elements and warn if they are found
1284 // somewhat of a hack, but it works
1285 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1288 switch(value_node->link_count())
1291 if(!value_node->get_link(0))
1293 error(element,"<composite> is missing parameters");
1294 return handle<ValueNode_Composite>();
1298 if(!value_node->get_link(0) ||!value_node->get_link(1))
1300 error(element,"<composite> is missing parameters");
1301 return handle<ValueNode_Composite>();
1305 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1307 error(element,"<composite> is missing parameters");
1308 return handle<ValueNode_Composite>();
1312 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1314 error(element,"<composite> is missing parameters");
1315 return handle<ValueNode_Composite>();
1322 // This will also parse a bline
1323 handle<ValueNode_DynamicList>
1324 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1326 assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1328 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1330 if(!element->get_attribute("type"))
1332 error(element,"Missing attribute \"type\" in <dynamic_list>");
1333 return handle<ValueNode_DynamicList>();
1336 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1340 error(element,"Bad type in <dynamic_list>");
1341 return handle<ValueNode_DynamicList>();
1344 handle<ValueNode_DynamicList> value_node;
1345 handle<ValueNode_BLine> bline_value_node;
1347 if(element->get_name()=="bline")
1349 value_node=bline_value_node=ValueNode_BLine::create();
1350 if(element->get_attribute("loop"))
1352 String loop=element->get_attribute("loop")->get_value();
1353 if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1354 bline_value_node->set_loop(true);
1356 bline_value_node->set_loop(false);
1361 value_node=ValueNode_DynamicList::create(type);
1365 error(element,strprintf(_("Unable to create <dynamic_list>")));
1366 return handle<ValueNode_DynamicList>();
1369 value_node->set_root_canvas(canvas->get_root());
1371 xmlpp::Element::NodeList list = element->get_children();
1372 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1374 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1378 if(child->get_name()=="entry")
1380 ValueNode_DynamicList::ListEntry list_entry;
1382 // Parse begin/end waypoints
1384 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1385 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1387 String begin_sequence;
1388 String end_sequence;
1390 ActivepointList &timing_info(list_entry.timing_info);
1392 if(child->get_attribute("begin"))
1393 begin_sequence=child->get_attribute("begin")->get_value();
1395 if(child->get_attribute("on"))
1396 begin_sequence=child->get_attribute("on")->get_value();
1398 if(child->get_attribute("end"))
1399 end_sequence=child->get_attribute("end")->get_value();
1401 if(child->get_attribute("off"))
1402 end_sequence=child->get_attribute("off")->get_value();
1404 // clear out any auto-start
1405 if(!begin_sequence.empty())
1406 timing_info.clear();
1409 while(!begin_sequence.empty())
1411 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1412 String timecode(begin_sequence.begin(), iter);
1415 // skip whitespace before checking for a priority
1416 while (isspace(timecode[0]))
1417 timecode=timecode.substr(1);
1419 // If there is a priority, then grab it and remove
1420 // it from the timecode
1421 if(timecode[0]=='p')
1423 //priority=timecode[1]-'0';
1424 //timecode=String(timecode.begin()+3,timecode.end());
1425 int space=timecode.find_first_of(' ');
1426 priority=atoi(String(timecode,1,space-1).c_str());
1427 timecode=String(timecode.begin()+space+1,timecode.end());
1428 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1431 timing_info.push_back(
1437 true, // Mark as a "on" activepoint
1442 if(iter==begin_sequence.end())
1443 begin_sequence.clear();
1445 begin_sequence=String(iter+1,begin_sequence.end());
1449 while(!end_sequence.empty())
1451 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1452 String timecode(end_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 false, // Mark as a "off" activepoint
1481 if(iter==end_sequence.end())
1482 end_sequence.clear();
1484 end_sequence=String(iter+1,end_sequence.end());
1490 if(child->get_attribute("use"))
1492 // \todo does this need to be able to read 'use="canvas"', like waypoints can now? (see 'surefind_canvas' in this file)
1493 string id=child->get_attribute("use")->get_value();
1496 list_entry.value_node=canvas->surefind_value_node(id);
1498 catch(Exception::IDNotFound)
1500 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1506 xmlpp::Element::NodeList list = child->get_children();
1507 xmlpp::Element::NodeList::iterator iter;
1509 // Search for the first non-text XML element
1510 for(iter = list.begin(); iter != list.end(); ++iter)
1511 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1513 if(iter==list.end())
1515 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1519 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1521 if(!list_entry.value_node)
1522 error((*iter),"Parse of ValueNode failed");
1524 // \todo do a search for more elements and warn if they are found
1528 value_node->add(list_entry);
1529 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1532 error_unexpected_element(child,child->get_name());
1538 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1540 handle<ValueNode> value_node;
1545 if(element->get_attribute("guid"))
1547 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1548 value_node=guid_cast<ValueNode>(guid);
1553 // If ValueBase::ident_type() recognises the name, then we know it's a ValueBase
1554 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1556 ValueBase data=parse_value(element,canvas);
1558 if(!data.is_valid())
1560 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1564 // We want to convert this ValueBase into a
1565 // ValueNode_Const. That way, we can treat the
1566 // ID like any other Datanode. Think of this
1567 // as a shorthand for creating constant ValueNodes.
1569 value_node=ValueNode_Const::create(data);
1572 if(element->get_name()=="hermite" || element->get_name()=="animated")
1573 value_node=parse_animated(element,canvas);
1575 if(element->get_name()=="composite")
1576 value_node=parse_composite(element,canvas);
1578 if(element->get_name()=="dynamic_list")
1579 value_node=parse_dynamic_list(element,canvas);
1581 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1582 value_node=parse_dynamic_list(element,canvas);
1584 if(LinkableValueNode::book().count(element->get_name()))
1585 value_node=parse_linkable_value_node(element,canvas);
1587 if(element->get_name()=="canvas")
1588 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1591 error_unexpected_element(element,element->get_name());
1592 error(element, "Expected a ValueNode");
1596 value_node->set_root_canvas(canvas->get_root());
1599 // If we were successful, and our element has
1600 // an ID attribute, go ahead and add it to the
1602 if(value_node && element->get_attribute("id"))
1604 string id=element->get_attribute("id")->get_value();
1606 //value_node->set_id(id);
1608 // If there is already a value_node in the list
1609 // with the same ID, then that is an error
1610 try { canvas->add_value_node(value_node,id); }
1611 catch(Exception::BadLinkName)
1613 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1616 catch(Exception::IDAlreadyExists)
1618 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1623 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1627 value_node->set_guid(guid);
1632 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1634 assert(element->get_name()=="defs");
1635 xmlpp::Element::NodeList list = element->get_children();
1636 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1638 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1642 if(child->get_name()=="canvas")
1643 parse_canvas(child, canvas);
1645 parse_value_node(child,canvas);
1650 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1653 assert(element->get_name()=="layer");
1654 Layer::Handle layer;
1656 if(!element->get_attribute("type"))
1658 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1659 return Layer::Handle();
1662 layer=Layer::create(element->get_attribute("type")->get_value());
1663 layer->set_canvas(canvas);
1665 if(element->get_attribute("group"))
1667 layer->add_to_group(
1668 element->get_attribute("group")->get_value()
1672 // Handle the version attribute
1673 if(element->get_attribute("version"))
1675 String version(element->get_attribute("version")->get_value());
1676 if(version>layer->get_version())
1677 warning(element,_("Installed layer version is larger than layer version in file"));
1678 if(version!=layer->get_version())
1679 layer->set_version(version);
1682 // Handle the description
1683 if(element->get_attribute("desc"))
1684 layer->set_description(element->get_attribute("desc")->get_value());
1686 if(element->get_attribute("active"))
1687 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1689 xmlpp::Element::NodeList list = element->get_children();
1690 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1692 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1696 if(child->get_name()=="name")
1697 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1699 if(child->get_name()=="desc")
1700 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1702 if(child->get_name()=="param")
1704 xmlpp::Element::NodeList list = child->get_children();
1706 if(!child->get_attribute("name"))
1708 error(child,_("Missing \"name\" attribute for <param>."));
1712 String param_name=child->get_attribute("name")->get_value();
1714 if(child->get_attribute("use"))
1716 // If the "use" attribute is used, then the
1717 // element should be empty. Warn the user if
1718 // we find otherwise.
1720 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1722 String str= child->get_attribute("use")->get_value();
1724 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1726 if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1727 error((*iter),_("Layer rejected canvas link"));
1732 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1734 // Assign the value_node to the dynamic parameter list
1735 layer->connect_dynamic_param(param_name,value_node);
1737 catch(Exception::IDNotFound)
1739 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1745 xmlpp::Element::NodeList::iterator iter;
1747 // Search for the first non-text XML element
1748 for(iter = list.begin(); iter != list.end(); ++iter)
1749 if(dynamic_cast<xmlpp::Element*>(*iter))
1751 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1753 if(iter==list.end())
1755 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1759 // If we recognise the element name as a
1760 // ValueBase, then treat is at one
1761 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1763 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1765 if(!data.is_valid())
1767 error((*iter),_("Bad data for <param>"));
1771 // Set the layer's parameter, and make sure that
1772 // the layer liked it
1773 if(!layer->set_param(param_name,data))
1775 warning((*iter),_("Layer rejected value for <param>"));
1779 else // ... otherwise, we assume that it is a ValueNode
1781 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1785 error((*iter),_("Bad data for <param>"));
1789 // Assign the value_node to the dynamic parameter list
1790 layer->connect_dynamic_param(param_name,value_node);
1793 // Warn if there is trash after the param value
1794 for(iter++; iter != list.end(); ++iter)
1795 if(dynamic_cast<xmlpp::Element*>(*iter))
1796 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1800 error_unexpected_element(child,child->get_name());
1803 layer->reset_version();
1808 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1811 if(element->get_name()!="canvas")
1813 error_unexpected_element(element,element->get_name(),"canvas");
1814 return Canvas::Handle();
1816 Canvas::Handle canvas;
1820 if(parent && (element->get_attribute("id") || inline_))
1824 canvas=Canvas::create_inline(parent);
1830 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1834 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1837 canvas->rend_desc().clear_flags();
1841 canvas=Canvas::create();
1842 if(filename=="/dev/stdin")
1843 canvas->set_file_name("./stdin.sif");
1845 canvas->set_file_name(filename);
1846 canvas->rend_desc().clear_flags();
1849 if(element->get_attribute("guid"))
1851 GUID guid(element->get_attribute("guid")->get_value());
1852 if(guid_cast<Canvas>(guid))
1853 return guid_cast<Canvas>(guid);
1855 canvas->set_guid(guid);
1858 if(element->get_attribute("width"))
1859 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1861 if(element->get_attribute("height"))
1862 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1864 if(element->get_attribute("xres"))
1865 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1867 if(element->get_attribute("yres"))
1868 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1871 if(element->get_attribute("fps"))
1872 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1874 if(element->get_attribute("start-time"))
1875 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1877 if(element->get_attribute("begin-time"))
1878 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1880 if(element->get_attribute("end-time"))
1881 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1883 if(element->get_attribute("antialias"))
1884 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1886 if(element->get_attribute("view-box"))
1888 string values=element->get_attribute("view-box")->get_value();
1892 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1893 values=string(values.begin()+values.find(' ')+1,values.end());
1894 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1895 values=string(values.begin()+values.find(' ')+1,values.end());
1896 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1897 values=string(values.begin()+values.find(' ')+1,values.end());
1898 br[1]=atof(values.c_str());
1900 canvas->rend_desc().set_tl(tl);
1901 canvas->rend_desc().set_br(br);
1904 if(element->get_attribute("bgcolor"))
1906 string values=element->get_attribute("bgcolor")->get_value();
1909 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1910 values=string(values.begin()+values.find(' ')+1,values.end());
1912 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1913 values=string(values.begin()+values.find(' ')+1,values.end());
1915 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1916 values=string(values.begin()+values.find(' ')+1,values.end());
1918 bg.set_a(atof(values.c_str()));
1920 canvas->rend_desc().set_bg_color(bg);
1923 if(element->get_attribute("focus"))
1925 string values=element->get_attribute("focus")->get_value();
1928 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1929 values=string(values.begin()+values.find(' ')+1,values.end());
1930 focus[1]=atof(values.c_str());
1932 canvas->rend_desc().set_focus(focus);
1935 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1937 xmlpp::Element::NodeList list = element->get_children();
1938 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1940 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1943 if(child->get_name()=="defs")
1945 if(canvas->is_inline())
1946 error(child,_("Inline canvas cannot have a <defs> section"));
1947 parse_canvas_defs(child, canvas);
1950 if(child->get_name()=="keyframe")
1952 if(canvas->is_inline())
1954 warning(child,_("Inline canvas cannot have keyframes"));
1958 canvas->keyframe_list().add(parse_keyframe(child,canvas));
1959 canvas->keyframe_list().sync();
1962 if(child->get_name()=="meta")
1964 if(canvas->is_inline())
1966 warning(child,_("Inline canvases cannot have metadata"));
1970 String name,content;
1972 if(!child->get_attribute("name"))
1974 warning(child,_("<meta> must have a name"));
1978 if(!child->get_attribute("content"))
1980 warning(child,_("<meta> must have content"));
1984 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
1986 else if(child->get_name()=="name")
1988 xmlpp::Element::NodeList list = child->get_children();
1990 // If we don't have any name, warn
1992 warning(child,_("blank \"name\" entitity"));
1995 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1996 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
1997 canvas->set_name(tmp);
2000 if(child->get_name()=="desc")
2003 xmlpp::Element::NodeList list = child->get_children();
2005 // If we don't have any description, warn
2007 warning(child,_("blank \"desc\" entitity"));
2010 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2011 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2012 canvas->set_description(tmp);
2015 if(child->get_name()=="author")
2018 xmlpp::Element::NodeList list = child->get_children();
2020 // If we don't have any description, warn
2022 warning(child,_("blank \"author\" entitity"));
2025 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2026 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2027 canvas->set_author(tmp);
2030 if(child->get_name()=="layer")
2032 //if(canvas->is_inline())
2033 // canvas->push_front(parse_layer(child,canvas->parent()));
2035 canvas->push_front(parse_layer(child,canvas));
2038 error_unexpected_element(child,child->get_name());
2041 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2045 if(canvas->value_node_list().placeholder_count())
2047 error(element,strprintf(_("Canvas %s has undefined ValueNodes"),canvas->get_id().c_str()));
2054 CanvasParser::parse_from_file(const String &file)
2056 return parse_from_file_as(file,file);
2060 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2062 CHECK_EXPIRE_TIME();
2065 ChangeLocale change_locale(LC_NUMERIC, "C");
2066 String file(unix_to_local_path(file_));
2067 String as(unix_to_local_path(as_));
2069 if(get_open_canvas_map().count(etl::absolute_path(as)))
2070 return get_open_canvas_map()[etl::absolute_path(as)];
2074 xmlpp::DomParser parser(file);
2077 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2078 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2079 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2080 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2084 const ValueNodeList& value_node_list(canvas->value_node_list());
2087 ValueNodeList::const_iterator iter;
2088 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2090 ValueNode::Handle value_node(*iter);
2091 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2093 canvas->remove_value_node(value_node);
2101 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2102 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2103 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2104 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2105 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2106 catch(const std::exception& ex)
2108 synfig::error("Standard Exception: "+String(ex.what()));
2109 return Canvas::Handle();
2111 catch(const String& str)
2114 // synfig::error(str);
2115 return Canvas::Handle();
2117 return Canvas::Handle();
2121 CanvasParser::parse_from_string(const String &data)
2123 CHECK_EXPIRE_TIME();
2127 ChangeLocale change_locale(LC_NUMERIC, "C");
2128 filename=_("<INTERNAL>");
2130 xmlpp::DomParser parser;
2131 parser.parse_memory(data);
2132 xmlpp::Element *root=parser.get_document()->get_root_node();
2135 Canvas::Handle canvas(parse_canvas(root));
2136 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2137 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2139 const ValueNodeList& value_node_list(canvas->value_node_list());
2141 ValueNodeList::const_iterator iter;
2142 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2144 ValueNode::Handle value_node(*iter);
2145 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2147 canvas->remove_value_node(value_node);
2155 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2156 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2157 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2158 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2159 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2160 catch(const std::exception& ex)
2162 synfig::error("Standard Exception: "+String(ex.what()));
2163 return Canvas::Handle();
2165 catch(const String& str)
2168 // synfig::error(str);
2169 return Canvas::Handle();
2171 return Canvas::Handle();