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, 2008 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 #define VALUENODE_COMPATIBILITY_URL "http://synfig.org/Convert#Compatibility"
93 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
95 /* === P R O C E D U R E S ================================================= */
97 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
99 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
101 if(!open_canvas_map_)
102 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
103 return *open_canvas_map_;
106 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
108 static void _canvas_file_name_changed(Canvas *x)
110 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
112 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
115 assert(iter!=get_open_canvas_map().end());
116 if(iter==get_open_canvas_map().end())
118 get_open_canvas_map().erase(iter->first);
119 get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
124 synfig::open_canvas(const String &filename)
128 parser.set_allow_errors(true);
130 Canvas::Handle canvas=parser.parse_from_file(filename);
132 if(parser.error_count())
133 return Canvas::Handle();
139 synfig::open_canvas_as(const String &filename,const String &as)
143 parser.set_allow_errors(true);
145 Canvas::Handle canvas=parser.parse_from_file_as(filename,as);
147 if(parser.error_count())
148 return Canvas::Handle();
154 synfig::string_to_canvas(const String &data)
158 parser.set_allow_errors(true);
160 Canvas::Handle canvas=parser.parse_from_string(data);
162 if(parser.error_count())
163 return Canvas::Handle();
168 /* === M E T H O D S ======================================================= */
171 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
173 error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
177 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
179 error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
183 CanvasParser::warning(xmlpp::Node *element, const String &text)
185 string str=strprintf("%s:<%s>:%d: warning: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
186 //synfig::warning(str);
189 if(total_warnings_>=max_warnings_)
190 fatal_error(element, _("Too many warnings"));
194 CanvasParser::error(xmlpp::Node *element, const String &text)
196 string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
199 throw runtime_error(str);
201 // synfig::error(str);
205 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
207 string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
208 throw runtime_error(str);
214 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
216 assert(element->get_name()=="keyframe");
218 if(!element->get_attribute("time"))
220 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
224 Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
227 if(element->get_children().empty())
230 if(element->get_child_text()->get_content().empty())
233 ret.set_description(element->get_child_text()->get_content());
240 CanvasParser::parse_real(xmlpp::Element *element)
242 assert(element->get_name()=="real");
244 if(!element->get_children().empty())
245 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
247 if(!element->get_attribute("value"))
249 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
253 string val=element->get_attribute("value")->get_value();
255 return atof(val.c_str());
259 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
261 assert(element->get_name()=="time");
263 if(!element->get_children().empty())
264 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
266 if(!element->get_attribute("value"))
268 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
272 string val=element->get_attribute("value")->get_value();
274 return Time(val,canvas->rend_desc().get_frame_rate());
278 CanvasParser::parse_integer(xmlpp::Element *element)
280 assert(element->get_name()=="integer");
282 if(!element->get_children().empty())
283 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
285 if(!element->get_attribute("value"))
287 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
291 string val=element->get_attribute("value")->get_value();
293 return atoi(val.c_str());
296 // see 'minor hack' at the end of parse_vector() below
297 // making this 'static' to give it file local scope
298 // stops it working (where working means working around
300 Vector &canvas_parser_vector_id(Vector &vector)
306 CanvasParser::parse_vector(xmlpp::Element *element)
308 assert(element->get_name()=="vector");
310 if(element->get_children().empty())
312 error(element, "Undefined value in <vector>");
318 xmlpp::Element::NodeList list = element->get_children();
319 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
321 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
325 if(child->get_name()=="x")
327 if(child->get_children().empty())
329 error(element, "Undefined value in <x>");
332 vect[0]=atof(child->get_child_text()->get_content().c_str());
335 if(child->get_name()=="y")
337 if(child->get_children().empty())
339 error(element, "Undefined value in <y>");
342 vect[1]=atof(child->get_child_text()->get_content().c_str());
345 error_unexpected_element(child,child->get_name());
347 // Minor hack - gcc 4.1.2 and earlier think that we're not using
348 // 'vect' and optimize it out at -O2 and higher. This convinces
349 // them that we are really using it.
350 return canvas_parser_vector_id(vect);
351 // When the bug is fixed, we can just do this instead:
356 CanvasParser::parse_color(xmlpp::Element *element)
358 assert(element->get_name()=="color");
360 if(element->get_children().empty())
362 error(element, "Undefined value in <color>");
368 xmlpp::Element::NodeList list = element->get_children();
369 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
371 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
375 if(child->get_name()=="r")
377 if(child->get_children().empty())
379 error(element, "Undefined value in <r>");
382 color.set_r(atof(child->get_child_text()->get_content().c_str()));
385 if(child->get_name()=="g")
387 if(child->get_children().empty())
389 error(element, "Undefined value in <g>");
392 color.set_g(atof(child->get_child_text()->get_content().c_str()));
395 if(child->get_name()=="b")
397 if(child->get_children().empty())
399 error(element, "Undefined value in <b>");
402 color.set_b(atof(child->get_child_text()->get_content().c_str()));
405 if(child->get_name()=="a")
407 if(child->get_children().empty())
409 error(element, "Undefined value in <a>");
412 color.set_a(atof(child->get_child_text()->get_content().c_str()));
415 error_unexpected_element(child,child->get_name());
422 CanvasParser::parse_string(xmlpp::Element *element)
424 assert(element->get_name()=="string");
426 if(element->get_children().empty())
428 warning(element, "Undefined value in <string>");
429 return synfig::String();
432 if(element->get_child_text()->get_content().empty())
434 warning(element, "Content element of <string> appears to be empty");
435 return synfig::String();
438 return element->get_child_text()->get_content();
442 CanvasParser::parse_bool(xmlpp::Element *element)
444 assert(element->get_name()=="bool");
446 if(!element->get_children().empty())
447 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
449 if(!element->get_attribute("value"))
451 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
455 string val=element->get_attribute("value")->get_value();
457 if(val=="true" || val=="1")
459 if(val=="false" || val=="0")
462 error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
468 CanvasParser::parse_gradient(xmlpp::Element *node)
470 assert(node->get_name()=="gradient");
473 xmlpp::Element::NodeList list = node->get_children();
474 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
476 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
481 Gradient::CPoint cpoint;
482 cpoint.color=parse_color(child);
484 if(!child->get_attribute("pos"))
486 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
490 cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
492 ret.push_back(cpoint);
500 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
502 vector<ValueBase> value_list;
504 xmlpp::Element::NodeList list = element->get_children();
505 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
507 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
512 value_list.push_back(parse_value(child,canvas));
513 if(!value_list.back().is_valid())
515 value_list.pop_back();
516 error(child,"Bad ValueBase");
525 CanvasParser::parse_segment(xmlpp::Element *element)
527 assert(element->get_name()=="segment");
529 if(element->get_children().empty())
531 error(element, "Undefined value in <segment>");
537 xmlpp::Element::NodeList list = element->get_children();
538 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
540 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
544 if(child->get_name()=="p1")
546 xmlpp::Element::NodeList list = child->get_children();
547 xmlpp::Element::NodeList::iterator iter;
549 // Search for the first non-text XML element
550 for(iter = list.begin(); iter != list.end(); ++iter)
551 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
555 error(element, "Undefined value in <p1>");
559 if((*iter)->get_name()!="vector")
561 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
565 seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
568 if(child->get_name()=="t1")
570 xmlpp::Element::NodeList list = child->get_children();
571 xmlpp::Element::NodeList::iterator iter;
573 // Search for the first non-text XML element
574 for(iter = list.begin(); iter != list.end(); ++iter)
575 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
579 error(element, "Undefined value in <t1>");
583 if((*iter)->get_name()!="vector")
585 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
589 seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
592 if(child->get_name()=="p2")
594 xmlpp::Element::NodeList list = child->get_children();
595 xmlpp::Element::NodeList::iterator iter;
597 // Search for the first non-text XML element
598 for(iter = list.begin(); iter != list.end(); ++iter)
599 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
603 error(element, "Undefined value in <p2>");
607 if((*iter)->get_name()!="vector")
609 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
613 seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
616 if(child->get_name()=="t2")
618 xmlpp::Element::NodeList list = child->get_children();
619 xmlpp::Element::NodeList::iterator iter;
621 // Search for the first non-text XML element
622 for(iter = list.begin(); iter != list.end(); ++iter)
623 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
627 error(element, "Undefined value in <t2>");
631 if((*iter)->get_name()!="vector")
633 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
637 seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
640 error_unexpected_element(child,child->get_name());
646 CanvasParser::parse_bline_point(xmlpp::Element *element)
648 assert(element->get_name()=="bline_point");
649 if(element->get_children().empty())
651 error(element, "Undefined value in <bline_point>");
656 ret.set_split_tangent_flag(false);
658 xmlpp::Element::NodeList list = element->get_children();
659 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
661 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
666 if(child->get_name()[0]=='v' || child->get_name()=="p1")
668 xmlpp::Element::NodeList list = child->get_children();
669 xmlpp::Element::NodeList::iterator iter;
671 // Search for the first non-text XML element
672 for(iter = list.begin(); iter != list.end(); ++iter)
673 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
677 error(element, "Undefined value in <vertex>");
681 if((*iter)->get_name()!="vector")
683 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
687 ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
691 if(child->get_name()=="t1" || child->get_name()=="tangent")
693 xmlpp::Element::NodeList list = child->get_children();
694 xmlpp::Element::NodeList::iterator iter;
696 // Search for the first non-text XML element
697 for(iter = list.begin(); iter != list.end(); ++iter)
698 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
702 error(element, "Undefined value in <t1>");
706 if((*iter)->get_name()!="vector")
708 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
712 ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
716 if(child->get_name()=="t2")
718 xmlpp::Element::NodeList list = child->get_children();
719 xmlpp::Element::NodeList::iterator iter;
721 // Search for the first non-text XML element
722 for(iter = list.begin(); iter != list.end(); ++iter)
723 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
727 error(element, "Undefined value in <t2>");
731 if((*iter)->get_name()!="vector")
733 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
737 ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
738 ret.set_split_tangent_flag(true);
742 if(child->get_name()=="width")
744 xmlpp::Element::NodeList list = child->get_children();
745 xmlpp::Element::NodeList::iterator iter;
747 // Search for the first non-text XML element
748 for(iter = list.begin(); iter != list.end(); ++iter)
749 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
753 error(element, "Undefined value in <width>");
757 if((*iter)->get_name()!="real")
759 error_unexpected_element((*iter),(*iter)->get_name(),"real");
763 ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
767 if(child->get_name()=="origin")
769 xmlpp::Element::NodeList list = child->get_children();
770 xmlpp::Element::NodeList::iterator iter;
772 // Search for the first non-text XML element
773 for(iter = list.begin(); iter != list.end(); ++iter)
774 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
778 error(element, "Undefined value in <origin>");
782 if((*iter)->get_name()!="real")
784 error_unexpected_element((*iter),(*iter)->get_name(),"real");
788 ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
791 error_unexpected_element(child,child->get_name());
797 CanvasParser::parse_angle(xmlpp::Element *element)
799 assert(element->get_name()=="angle");
801 if(!element->get_children().empty())
802 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
804 if(!element->get_attribute("value"))
806 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
810 string val=element->get_attribute("value")->get_value();
812 return Angle::deg(atof(val.c_str()));
816 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
818 if(element->get_name()=="real")
819 return parse_real(element);
821 if(element->get_name()=="time")
822 return parse_time(element,canvas);
824 if(element->get_name()=="integer")
825 return parse_integer(element);
827 if(element->get_name()=="string")
828 return parse_string(element);
830 if(element->get_name()=="vector")
831 return parse_vector(element);
833 if(element->get_name()=="color")
834 return parse_color(element);
836 if(element->get_name()=="segment")
837 return parse_segment(element);
839 if(element->get_name()=="list")
840 return parse_list(element,canvas);
842 if(element->get_name()=="gradient")
843 return parse_gradient(element);
845 if(element->get_name()=="bool")
846 return parse_bool(element);
848 //if(element->get_name()=="canvas")
849 // return parse_canvas(element,canvas,true); // inline canvas
851 if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
852 return parse_angle(element);
854 if(element->get_name()=="bline_point")
855 return parse_bline_point(element);
857 if(element->get_name()=="canvas")
858 return ValueBase(parse_canvas(element,canvas,true));
861 error_unexpected_element(element,element->get_name());
870 ValueNode_Animated::Handle
871 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
873 assert(element->get_name()=="hermite" || element->get_name()=="animated");
875 if(!element->get_attribute("type"))
877 error(element,"Missing attribute \"type\" in <animated>");
878 return ValueNode_Animated::Handle();
881 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
885 error(element,"Bad type in <animated>");
886 return ValueNode_Animated::Handle();
889 ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
893 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_local_name(type).c_str()));
894 return ValueNode_Animated::Handle();
897 value_node->set_root_canvas(canvas->get_root());
899 xmlpp::Element::NodeList list = element->get_children();
900 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
902 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
906 if(child->get_name()=="waypoint")
908 if(!child->get_attribute("time"))
910 error(child,_("<waypoint> is missing attribute \"time\""));
914 Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
917 ValueNode::Handle waypoint_value_node;
918 xmlpp::Element::NodeList list = child->get_children();
920 if(child->get_attribute("use"))
923 warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
925 // the waypoint might look like this, in which case we won't find "mycanvas" in the list of valuenodes, 'cos it's a canvas
927 // <animated type="canvas">
928 // <waypoint time="0s" use="mycanvas"/>
930 if (type==ValueBase::TYPE_CANVAS)
931 waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value()));
933 waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
937 if(child->get_children().empty())
939 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
943 xmlpp::Element::NodeList::iterator iter;
945 // Search for the first non-text XML element
946 for(iter = list.begin(); iter != list.end(); ++iter)
947 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
951 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
955 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
958 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
962 error(child,_("Bad data for <waypoint>"));
966 if(!waypoint_value_node)
968 error(child,_("Bad data for <waypoint>"));
972 /*! HACK -- This is a temporary fix to help repair some
973 ** weirdness that is currently going on (10-21-2004).
974 ** This short circuits the linking of waypoints,
975 ** a feature which is so obscure that we can get
976 ** away with something like this pretty easily.
978 waypoint_value_node=waypoint_value_node->clone();
980 // Warn if there is trash after the param value
981 for(iter++; iter != list.end(); ++iter)
982 if(dynamic_cast<xmlpp::Element*>(*iter))
983 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
988 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
990 if(child->get_attribute("tension"))
992 synfig::String str(child->get_attribute("tension")->get_value());
993 waypoint->set_tension(atof(str.c_str()));
995 if(child->get_attribute("temporal-tension"))
997 synfig::String str(child->get_attribute("temporal-tension")->get_value());
998 waypoint->set_time_tension(atof(str.c_str()));
1000 if(child->get_attribute("continuity"))
1002 synfig::String str(child->get_attribute("continuity")->get_value());
1003 waypoint->set_continuity(atof(str.c_str()));
1005 if(child->get_attribute("bias"))
1007 synfig::String str(child->get_attribute("bias")->get_value());
1008 waypoint->set_bias(atof(str.c_str()));
1011 if(child->get_attribute("before"))
1013 string val=child->get_attribute("before")->get_value();
1015 waypoint->set_before(INTERPOLATION_HALT);
1016 else if(val=="constant")
1017 waypoint->set_before(INTERPOLATION_CONSTANT);
1018 else if(val=="linear")
1019 waypoint->set_before(INTERPOLATION_LINEAR);
1020 else if(val=="manual")
1021 waypoint->set_before(INTERPOLATION_MANUAL);
1022 else if(val=="auto")
1023 waypoint->set_before(INTERPOLATION_TCB);
1025 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1028 if(child->get_attribute("after"))
1030 string val=child->get_attribute("after")->get_value();
1032 waypoint->set_after(INTERPOLATION_HALT);
1033 else if(val=="constant")
1034 waypoint->set_after(INTERPOLATION_CONSTANT);
1035 else if(val=="linear")
1036 waypoint->set_after(INTERPOLATION_LINEAR);
1037 else if(val=="manual")
1038 waypoint->set_after(INTERPOLATION_MANUAL);
1039 else if(val=="auto")
1040 waypoint->set_after(INTERPOLATION_TCB);
1042 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1045 catch(Exception::BadTime x)
1047 warning(child,x.what());
1053 error_unexpected_element(child,child->get_name());
1056 // in canvas version 0.1, angles used to wrap, so to get from -179
1057 // degrees to 180 degrees meant a 1 degree change
1058 // in canvas version 0.2 they don't, so that's a 359 degree change
1060 // when loading a version 0.1 canvas, modify constant angle
1061 // waypoints to that they are within 180 degrees of the previous
1063 if (type == ValueBase::TYPE_ANGLE)
1065 if (canvas->get_version() == "0.1")
1068 Real angle, prev = 0;
1069 WaypointList &wl = value_node->waypoint_list();
1070 for (WaypointList::iterator iter = wl.begin(); iter != wl.end(); iter++)
1072 angle = Angle::deg(iter->get_value(iter->get_time()).get(Angle())).get();
1075 else if (iter->get_value_node()->get_name() == "constant")
1077 if (angle - prev > 180)
1079 while (angle - prev > 180) angle -= 360;
1080 iter->set_value(Angle::deg(angle));
1082 else if (prev - angle > 180)
1084 while (prev - angle > 180) angle += 360;
1085 iter->set_value(Angle::deg(angle));
1093 value_node->changed();
1097 etl::handle<LinkableValueNode>
1098 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1100 // Determine the type
1101 if(!element->get_attribute("type"))
1103 error(element, strprintf(_("Missing attribute \"type\" in <%s>"), element->get_name().c_str()));
1107 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1111 error(element, strprintf(_("Bad type in <%s>"), element->get_name().c_str()));
1115 handle<LinkableValueNode> value_node=LinkableValueNode::create(element->get_name(),type);
1116 handle<ValueNode> c[value_node->link_count()];
1120 error(element, strprintf(_("Error creating ValueNode <%s> with type '%s'. Refer to '%s'"),
1121 element->get_name().c_str(),
1122 ValueBase::type_local_name(type).c_str(),
1123 VALUENODE_COMPATIBILITY_URL));
1127 if(value_node->get_type()!=type)
1129 error(element, strprintf(_("<%s> did not accept type '%s'"),
1130 element->get_name().c_str(),
1131 ValueBase::type_local_name(type).c_str()));
1135 value_node->set_root_canvas(canvas->get_root());
1137 // handle exported valuenodes
1141 xmlpp::Element::AttributeList attrib_list(element->get_attributes());
1142 for(xmlpp::Element::AttributeList::iterator iter = attrib_list.begin(); iter != attrib_list.end(); iter++)
1144 name = (*iter)->get_name();
1145 id = (*iter)->get_value();
1147 if (name == "guid" || name == "id" || name == "type")
1151 index = value_node->get_link_index_from_name(name);
1155 error(element,strprintf(_("'%s' was already defined in <%s>"),
1157 element->get_name().c_str()));
1161 c[index] = canvas->surefind_value_node(id);
1165 error(element, strprintf(_("'%s' attribute in <%s> references unknown ID '%s'"),
1167 element->get_name().c_str(),
1172 if(!value_node->set_link(index, c[index]))
1174 error(element, strprintf(_("Unable to set link '\"%s\" to ValueNode \"%s\" (link #%d in \"%s\")"),
1175 value_node->link_name(index).c_str(),
1178 element->get_name().c_str()));
1182 // printf(" <%s> set link %d (%s) using exported value\n", element->get_name().c_str(), index, name.c_str());
1184 catch (Exception::BadLinkName)
1186 warning(element, strprintf("Bad link name '%s'", name.c_str()));
1188 catch(Exception::IDNotFound)
1190 error(element,"Unable to resolve " + id);
1192 catch(Exception::FileNotFound)
1194 error(element,"Unable to open file referenced in " + id);
1198 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""), id.c_str()));
1204 // handle inline valuenodes
1208 xmlpp::Element::NodeList list = element->get_children();
1209 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1211 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1217 child_name = child->get_name();
1219 index = value_node->get_link_index_from_name(child_name);
1223 error(child, strprintf(_("'%s' was already defined in <%s>"),
1225 element->get_name().c_str()));
1229 xmlpp::Element::NodeList list = child->get_children();
1230 xmlpp::Element::NodeList::iterator iter;
1232 // Search for the first non-text XML element
1233 for(iter = list.begin(); iter != list.end(); ++iter)
1234 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1236 if(iter==list.end())
1238 error(child,strprintf(_("element <%s> is missing its contents"),
1239 child_name.c_str()));
1243 c[index]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1247 error((*iter),strprintf(_("Parse of '%s' failed"),
1248 child_name.c_str()));
1252 if(!value_node->set_link(index,c[index]))
1254 error(child,strprintf(_("Unable to connect value node ('%s' of type '%s') to link %d (%s)"),
1255 c[index]->get_name().c_str(),
1256 ValueBase::type_local_name(c[index]->get_type()).c_str(),
1258 value_node->link_name(index).c_str()));
1262 // \todo do a search for more elements and warn if they are found
1264 // printf(" <%s> set link %d (%s) using inline value\n", element->get_name().c_str(), index, child_name.c_str());
1266 catch(Exception::BadLinkName)
1268 warning(child, strprintf("Bad link name for <%s>", element->get_name().c_str()));
1272 error(child, strprintf(_("Unknown Exception thrown when working on element \"%s\""),child_name.c_str()));
1278 for (int i = 0; i < value_node->link_count(); i++)
1281 // the 'width' parameter of <stripes> wasn't always present, so it won't be in old .sif files
1282 !(element->get_name() == "stripes" && value_node->link_name(i) == "width"))
1284 error(element, strprintf(_("<%s> is missing link %d (%s)"),
1285 element->get_name().c_str(),
1287 value_node->link_name(i).c_str()));
1295 // This will also parse a bline
1296 handle<ValueNode_DynamicList>
1297 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1299 assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1301 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1303 if(!element->get_attribute("type"))
1305 error(element,"Missing attribute \"type\" in <dynamic_list>");
1306 return handle<ValueNode_DynamicList>();
1309 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1313 error(element,"Bad type in <dynamic_list>");
1314 return handle<ValueNode_DynamicList>();
1317 handle<ValueNode_DynamicList> value_node;
1318 handle<ValueNode_BLine> bline_value_node;
1320 if(element->get_name()=="bline")
1322 value_node=bline_value_node=ValueNode_BLine::create();
1323 if(element->get_attribute("loop"))
1325 String loop=element->get_attribute("loop")->get_value();
1326 if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1327 bline_value_node->set_loop(true);
1329 bline_value_node->set_loop(false);
1334 value_node=ValueNode_DynamicList::create(type);
1338 error(element,strprintf(_("Unable to create <dynamic_list>")));
1339 return handle<ValueNode_DynamicList>();
1342 value_node->set_root_canvas(canvas->get_root());
1344 xmlpp::Element::NodeList list = element->get_children();
1345 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1347 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1351 if(child->get_name()=="entry")
1353 ValueNode_DynamicList::ListEntry list_entry;
1355 // Parse begin/end waypoints
1357 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1358 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1360 String begin_sequence;
1361 String end_sequence;
1363 ActivepointList &timing_info(list_entry.timing_info);
1365 if(child->get_attribute("begin"))
1366 begin_sequence=child->get_attribute("begin")->get_value();
1368 if(child->get_attribute("on"))
1369 begin_sequence=child->get_attribute("on")->get_value();
1371 if(child->get_attribute("end"))
1372 end_sequence=child->get_attribute("end")->get_value();
1374 if(child->get_attribute("off"))
1375 end_sequence=child->get_attribute("off")->get_value();
1377 // clear out any auto-start
1378 if(!begin_sequence.empty())
1379 timing_info.clear();
1382 while(!begin_sequence.empty())
1384 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1385 String timecode(begin_sequence.begin(), iter);
1388 // skip whitespace before checking for a priority
1389 while (isspace(timecode[0]))
1390 timecode=timecode.substr(1);
1392 // If there is a priority, then grab it and remove
1393 // it from the timecode
1394 if(timecode[0]=='p')
1396 //priority=timecode[1]-'0';
1397 //timecode=String(timecode.begin()+3,timecode.end());
1398 int space=timecode.find_first_of(' ');
1399 priority=atoi(String(timecode,1,space-1).c_str());
1400 timecode=String(timecode.begin()+space+1,timecode.end());
1401 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1404 timing_info.push_back(
1410 true, // Mark as a "on" activepoint
1415 if(iter==begin_sequence.end())
1416 begin_sequence.clear();
1418 begin_sequence=String(iter+1,begin_sequence.end());
1422 while(!end_sequence.empty())
1424 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1425 String timecode(end_sequence.begin(), iter);
1428 // skip whitespace before checking for a priority
1429 while (isspace(timecode[0]))
1430 timecode=timecode.substr(1);
1432 // If there is a priority, then grab it and remove
1433 // it from the timecode
1434 if(timecode[0]=='p')
1436 //priority=timecode[1]-'0';
1437 //timecode=String(timecode.begin()+3,timecode.end());
1438 int space=timecode.find_first_of(' ');
1439 priority=atoi(String(timecode,1,space-1).c_str());
1440 timecode=String(timecode.begin()+space+1,timecode.end());
1441 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1444 timing_info.push_back(
1450 false, // Mark as a "off" activepoint
1454 if(iter==end_sequence.end())
1455 end_sequence.clear();
1457 end_sequence=String(iter+1,end_sequence.end());
1463 if(child->get_attribute("use"))
1465 // \todo does this need to be able to read 'use="canvas"', like waypoints can now? (see 'surefind_canvas' in this file)
1466 string id=child->get_attribute("use")->get_value();
1469 list_entry.value_node=canvas->surefind_value_node(id);
1471 catch(Exception::IDNotFound)
1473 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1479 xmlpp::Element::NodeList list = child->get_children();
1480 xmlpp::Element::NodeList::iterator iter;
1482 // Search for the first non-text XML element
1483 for(iter = list.begin(); iter != list.end(); ++iter)
1484 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1486 if(iter==list.end())
1488 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1492 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1494 if(!list_entry.value_node)
1495 error((*iter),"Parse of ValueNode failed");
1497 // \todo do a search for more elements and warn if they are found
1501 value_node->add(list_entry);
1502 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1505 error_unexpected_element(child,child->get_name());
1511 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1513 handle<ValueNode> value_node;
1518 if(element->get_attribute("guid"))
1520 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1521 value_node=guid_cast<ValueNode>(guid);
1526 // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1527 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1529 ValueBase data=parse_value(element,canvas);
1531 if(!data.is_valid())
1533 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1537 // We want to convert this ValueBase into a
1538 // ValueNode_Const. That way, we can treat the
1539 // ID like any other Datanode. Think of this
1540 // as a shorthand for creating constant ValueNodes.
1542 value_node=ValueNode_Const::create(data);
1545 if(element->get_name()=="hermite" || element->get_name()=="animated")
1546 value_node=parse_animated(element,canvas);
1548 if(element->get_name()=="dynamic_list")
1549 value_node=parse_dynamic_list(element,canvas);
1551 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1552 value_node=parse_dynamic_list(element,canvas);
1554 if(LinkableValueNode::book().count(element->get_name()))
1556 value_node=parse_linkable_value_node(element,canvas);
1557 if (!value_node) value_node = PlaceholderValueNode::create();
1560 if(element->get_name()=="canvas")
1561 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1564 error_unexpected_element(element,element->get_name());
1565 error(element, strprintf(_("Expected a ValueNode. Refer to '%s'"),
1566 VALUENODE_COMPATIBILITY_URL));
1567 value_node=PlaceholderValueNode::create();
1570 value_node->set_root_canvas(canvas->get_root());
1572 // If we were successful, and our element has
1573 // an ID attribute, go ahead and add it to the
1575 if(value_node && element->get_attribute("id"))
1577 string id=element->get_attribute("id")->get_value();
1579 //value_node->set_id(id);
1581 // If there is already a value_node in the list
1582 // with the same ID, then that is an error
1583 try { canvas->add_value_node(value_node,id); }
1584 catch(Exception::BadLinkName)
1586 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1589 catch(Exception::IDAlreadyExists)
1591 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1596 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1600 value_node->set_guid(guid);
1605 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1607 assert(element->get_name()=="defs");
1608 xmlpp::Element::NodeList list = element->get_children();
1609 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1611 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1615 if(child->get_name()=="canvas")
1616 parse_canvas(child, canvas);
1618 parse_value_node(child,canvas);
1623 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1626 assert(element->get_name()=="layer");
1627 Layer::Handle layer;
1629 if(!element->get_attribute("type"))
1631 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1632 return Layer::Handle();
1635 layer=Layer::create(element->get_attribute("type")->get_value());
1636 layer->set_canvas(canvas);
1638 if(element->get_attribute("group"))
1640 layer->add_to_group(
1641 element->get_attribute("group")->get_value()
1645 // Handle the version attribute
1646 if(element->get_attribute("version"))
1648 String version(element->get_attribute("version")->get_value());
1649 if(version>layer->get_version())
1650 warning(element,_("Installed layer version is smaller than layer version in file"));
1651 if(version!=layer->get_version())
1652 layer->set_version(version);
1655 // Handle the description
1656 if(element->get_attribute("desc"))
1657 layer->set_description(element->get_attribute("desc")->get_value());
1659 if(element->get_attribute("active"))
1660 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1662 xmlpp::Element::NodeList list = element->get_children();
1663 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1665 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1669 if(child->get_name()=="name")
1670 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1672 if(child->get_name()=="desc")
1673 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1675 if(child->get_name()=="param")
1677 xmlpp::Element::NodeList list = child->get_children();
1679 if(!child->get_attribute("name"))
1681 error(child,_("Missing \"name\" attribute for <param>."));
1685 String param_name=child->get_attribute("name")->get_value();
1687 if(child->get_attribute("use"))
1689 // If the "use" attribute is used, then the
1690 // element should be empty. Warn the user if
1691 // we find otherwise.
1693 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1695 String str= child->get_attribute("use")->get_value();
1698 error(child,_("Empty use=\"\" value in <param>"));
1699 else if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1701 if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1702 error((*iter),_("Layer rejected canvas link"));
1707 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1709 // Assign the value_node to the dynamic parameter list
1710 layer->connect_dynamic_param(param_name,value_node);
1712 catch(Exception::IDNotFound)
1714 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1720 xmlpp::Element::NodeList::iterator iter;
1722 // Search for the first non-text XML element
1723 for(iter = list.begin(); iter != list.end(); ++iter)
1724 if(dynamic_cast<xmlpp::Element*>(*iter))
1726 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1728 if(iter==list.end())
1730 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1734 // If we recognize the element name as a
1735 // ValueBase, then treat is at one
1736 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1738 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1740 if(!data.is_valid())
1742 error((*iter),_("Bad data for <param>"));
1746 // Set the layer's parameter, and make sure that
1747 // the layer liked it
1748 if(!layer->set_param(param_name,data))
1750 warning((*iter),_("Layer rejected value for <param>"));
1754 else // ... otherwise, we assume that it is a ValueNode
1756 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1760 error((*iter),_("Bad data for <param>"));
1764 // Assign the value_node to the dynamic parameter list
1765 layer->connect_dynamic_param(param_name,value_node);
1768 // Warn if there is trash after the param value
1769 for(iter++; iter != list.end(); ++iter)
1770 if(dynamic_cast<xmlpp::Element*>(*iter))
1771 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1775 error_unexpected_element(child,child->get_name());
1778 layer->reset_version();
1783 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1786 if(element->get_name()!="canvas")
1788 error_unexpected_element(element,element->get_name(),"canvas");
1789 return Canvas::Handle();
1791 Canvas::Handle canvas;
1795 if(parent && (element->get_attribute("id") || inline_))
1799 canvas=Canvas::create_inline(parent);
1805 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1809 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1812 canvas->rend_desc().clear_flags();
1816 canvas=Canvas::create();
1817 if(filename=="/dev/stdin")
1818 canvas->set_file_name("./stdin.sif");
1820 canvas->set_file_name(filename);
1821 canvas->rend_desc().clear_flags();
1824 if(element->get_attribute("guid"))
1826 GUID guid(element->get_attribute("guid")->get_value());
1827 if(guid_cast<Canvas>(guid))
1828 return guid_cast<Canvas>(guid);
1830 canvas->set_guid(guid);
1833 if(element->get_attribute("version"))
1834 canvas->set_version(element->get_attribute("version")->get_value());
1836 canvas->set_version(parent->get_version());
1838 if(element->get_attribute("width"))
1839 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1841 if(element->get_attribute("height"))
1842 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1844 if(element->get_attribute("xres"))
1845 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1847 if(element->get_attribute("yres"))
1848 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1851 if(element->get_attribute("fps"))
1852 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1854 if(element->get_attribute("start-time"))
1855 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1857 if(element->get_attribute("begin-time"))
1858 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1860 if(element->get_attribute("end-time"))
1861 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1863 if(element->get_attribute("antialias"))
1864 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1866 if(element->get_attribute("view-box"))
1868 string values=element->get_attribute("view-box")->get_value();
1872 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1873 values=string(values.begin()+values.find(' ')+1,values.end());
1874 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1875 values=string(values.begin()+values.find(' ')+1,values.end());
1876 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1877 values=string(values.begin()+values.find(' ')+1,values.end());
1878 br[1]=atof(values.c_str());
1880 canvas->rend_desc().set_tl(tl);
1881 canvas->rend_desc().set_br(br);
1884 if(element->get_attribute("bgcolor"))
1886 string values=element->get_attribute("bgcolor")->get_value();
1889 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1890 values=string(values.begin()+values.find(' ')+1,values.end());
1892 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1893 values=string(values.begin()+values.find(' ')+1,values.end());
1895 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1896 values=string(values.begin()+values.find(' ')+1,values.end());
1898 bg.set_a(atof(values.c_str()));
1900 canvas->rend_desc().set_bg_color(bg);
1903 if(element->get_attribute("focus"))
1905 string values=element->get_attribute("focus")->get_value();
1908 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1909 values=string(values.begin()+values.find(' ')+1,values.end());
1910 focus[1]=atof(values.c_str());
1912 canvas->rend_desc().set_focus(focus);
1915 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1917 xmlpp::Element::NodeList list = element->get_children();
1918 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1920 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1923 if(child->get_name()=="defs")
1925 if(canvas->is_inline())
1926 error(child,_("Inline canvas cannot have a <defs> section"));
1927 parse_canvas_defs(child, canvas);
1930 if(child->get_name()=="keyframe")
1932 if(canvas->is_inline())
1934 warning(child,_("Inline canvas cannot have keyframes"));
1938 canvas->keyframe_list().add(parse_keyframe(child,canvas));
1939 canvas->keyframe_list().sync();
1942 if(child->get_name()=="meta")
1944 if(canvas->is_inline())
1946 warning(child,_("Inline canvases cannot have metadata"));
1950 String name,content;
1952 if(!child->get_attribute("name"))
1954 warning(child,_("<meta> must have a name"));
1958 if(!child->get_attribute("content"))
1960 warning(child,_("<meta> must have content"));
1964 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
1966 else if(child->get_name()=="name")
1968 xmlpp::Element::NodeList list = child->get_children();
1970 // If we don't have any name, warn
1972 warning(child,_("blank \"name\" entity"));
1975 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1976 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
1977 canvas->set_name(tmp);
1980 if(child->get_name()=="desc")
1983 xmlpp::Element::NodeList list = child->get_children();
1985 // If we don't have any description, warn
1987 warning(child,_("blank \"desc\" entity"));
1990 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1991 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
1992 canvas->set_description(tmp);
1995 if(child->get_name()=="author")
1998 xmlpp::Element::NodeList list = child->get_children();
2000 // If we don't have any description, warn
2002 warning(child,_("blank \"author\" entity"));
2005 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2006 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2007 canvas->set_author(tmp);
2010 if(child->get_name()=="layer")
2012 //if(canvas->is_inline())
2013 // canvas->push_front(parse_layer(child,canvas->parent()));
2015 canvas->push_front(parse_layer(child,canvas));
2018 error_unexpected_element(child,child->get_name());
2021 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2025 if(canvas->value_node_list().placeholder_count())
2028 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2029 if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2031 if (nodes != "") nodes += ", ";
2032 nodes += "'" + (*iter)->get_id() + "'";
2034 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2035 canvas->get_id().c_str(),
2036 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2040 canvas->set_version(CURRENT_CANVAS_VERSION);
2045 CanvasParser::parse_from_file(const String &file)
2047 return parse_from_file_as(file,file);
2051 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2055 ChangeLocale change_locale(LC_NUMERIC, "C");
2056 String file(unix_to_local_path(file_));
2057 String as(unix_to_local_path(as_));
2059 if(get_open_canvas_map().count(etl::absolute_path(as)))
2060 return get_open_canvas_map()[etl::absolute_path(as)];
2064 xmlpp::DomParser parser(file);
2067 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2068 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2069 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2070 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2074 const ValueNodeList& value_node_list(canvas->value_node_list());
2077 ValueNodeList::const_iterator iter;
2078 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2080 ValueNode::Handle value_node(*iter);
2081 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2083 canvas->remove_value_node(value_node);
2091 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2092 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2093 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2094 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2095 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2096 catch(const std::exception& ex)
2098 synfig::error("Standard Exception: "+String(ex.what()));
2099 return Canvas::Handle();
2101 catch(const String& str)
2104 // synfig::error(str);
2105 return Canvas::Handle();
2107 return Canvas::Handle();
2111 CanvasParser::parse_from_string(const String &data)
2115 ChangeLocale change_locale(LC_NUMERIC, "C");
2116 filename=_("<INTERNAL>");
2118 xmlpp::DomParser parser;
2119 parser.parse_memory(data);
2120 xmlpp::Element *root=parser.get_document()->get_root_node();
2123 Canvas::Handle canvas(parse_canvas(root));
2124 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2125 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2127 const ValueNodeList& value_node_list(canvas->value_node_list());
2129 ValueNodeList::const_iterator iter;
2130 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2132 ValueNode::Handle value_node(*iter);
2133 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2135 canvas->remove_value_node(value_node);
2143 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2144 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2145 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2146 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2147 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2148 catch(const std::exception& ex)
2150 synfig::error("Standard Exception: "+String(ex.what()));
2151 return Canvas::Handle();
2153 catch(const String& str)
2156 // synfig::error(str);
2157 return Canvas::Handle();
2159 return Canvas::Handle();