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 "layer_pastecanvas.h"
43 #include "loadcanvas.h"
44 #include "valuenode.h"
45 #include "valuenode_subtract.h"
46 #include "valuenode_animated.h"
47 #include "valuenode_composite.h"
48 #include "valuenode_const.h"
49 #include "valuenode_linear.h"
50 #include "valuenode_dynamiclist.h"
51 #include "valuenode_reference.h"
52 #include "valuenode_scale.h"
53 #include "valuenode_timedswap.h"
54 #include "valuenode_twotone.h"
55 #include "valuenode_stripes.h"
56 #include "valuenode_segcalctangent.h"
57 #include "valuenode_segcalcvertex.h"
58 #include "valuenode_bline.h"
63 #include "exception.h"
68 #include <sigc++/bind.h>
72 /* === U S I N G =========================================================== */
75 using namespace synfig;
82 test_class() { assert(!bleh); bleh++; synfig::info("test_class: initi: %d",bleh); }
83 ~test_class() { assert(bleh); synfig::info("test_class: uninit: %d",bleh); bleh--; }
85 int test_class::bleh(0);
87 test_class test_class_instance;
90 /* === M A C R O S ========================================================= */
92 #define VALUENODE_COMPATIBILITY_URL "http://synfig.org/Convert#Compatibility"
94 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
96 std::set<String> CanvasParser::loading_;
98 /* === P R O C E D U R E S ================================================= */
100 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
102 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
104 if(!open_canvas_map_)
105 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
106 return *open_canvas_map_;
109 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
111 static void _canvas_file_name_changed(Canvas *x)
113 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
115 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
118 assert(iter!=get_open_canvas_map().end());
119 if(iter==get_open_canvas_map().end())
121 get_open_canvas_map().erase(iter->first);
122 get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
127 synfig::open_canvas(const String &filename,String &errors,String &warnings)
129 return open_canvas_as(filename, filename, errors, warnings);
133 synfig::open_canvas_as(const String &filename,const String &as,String &errors,String &warnings)
135 if (CanvasParser::loading_.count(filename))
137 String warning(strprintf(_("cannot load '%s' recursively"), filename.c_str()));
138 synfig::warning(warning);
139 warnings = " * " + warning + "\n";
140 Canvas::Handle canvas(Canvas::create());
141 canvas->set_file_name(filename);
142 Layer::Handle paste(Layer_PasteCanvas::create());
143 canvas->push_back(paste);
144 paste->set_description(warning);
148 Canvas::Handle canvas;
150 parser.set_allow_errors(true);
154 CanvasParser::loading_.insert(filename);
155 canvas=parser.parse_from_file_as(filename,as,errors);
159 CanvasParser::loading_.erase(filename);
162 CanvasParser::loading_.erase(filename);
164 warnings = parser.get_warnings_text();
166 if(parser.error_count())
168 errors = parser.get_errors_text();
169 return Canvas::Handle();
175 /* === M E T H O D S ======================================================= */
178 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
180 error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
184 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
186 error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
190 CanvasParser::warning(xmlpp::Node *element, const String &text)
192 string str=strprintf("%s:<%s>:%d: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
194 synfig::warning(str);
198 warnings_text += " * " + str + "\n";
199 if(total_warnings_>=max_warnings_)
200 fatal_error(element, _("Too many warnings"));
204 CanvasParser::error(xmlpp::Node *element, const String &text)
206 string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
208 errors_text += " * " + str + "\n";
210 throw runtime_error(str);
212 // synfig::error(str);
216 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
218 string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
219 throw runtime_error(str);
225 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
227 assert(element->get_name()=="keyframe");
229 if(!element->get_attribute("time"))
231 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
235 Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
238 if(element->get_children().empty())
241 if(element->get_child_text()->get_content().empty())
244 ret.set_description(element->get_child_text()->get_content());
251 CanvasParser::parse_real(xmlpp::Element *element)
253 assert(element->get_name()=="real");
255 if(!element->get_children().empty())
256 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
258 if(!element->get_attribute("value"))
260 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
264 string val=element->get_attribute("value")->get_value();
266 return atof(val.c_str());
270 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
272 assert(element->get_name()=="time");
274 if(!element->get_children().empty())
275 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
277 if(!element->get_attribute("value"))
279 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
283 string val=element->get_attribute("value")->get_value();
285 return Time(val,canvas->rend_desc().get_frame_rate());
289 CanvasParser::parse_integer(xmlpp::Element *element)
291 assert(element->get_name()=="integer");
293 if(!element->get_children().empty())
294 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
296 if(!element->get_attribute("value"))
298 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
302 string val=element->get_attribute("value")->get_value();
304 return atoi(val.c_str());
307 // see 'minor hack' at the end of parse_vector() below
308 // making this 'static' to give it file local scope
309 // stops it working (where working means working around
311 Vector &canvas_parser_vector_id(Vector &vector)
317 CanvasParser::parse_vector(xmlpp::Element *element)
319 assert(element->get_name()=="vector");
321 if(element->get_children().empty())
323 error(element, "Undefined value in <vector>");
329 xmlpp::Element::NodeList list = element->get_children();
330 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
332 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
336 if(child->get_name()=="x")
338 if(child->get_children().empty())
340 error(element, "Undefined value in <x>");
343 vect[0]=atof(child->get_child_text()->get_content().c_str());
346 if(child->get_name()=="y")
348 if(child->get_children().empty())
350 error(element, "Undefined value in <y>");
353 vect[1]=atof(child->get_child_text()->get_content().c_str());
356 error_unexpected_element(child,child->get_name());
358 // Minor hack - gcc 4.1.2 and earlier think that we're not using
359 // 'vect' and optimize it out at -O2 and higher. This convinces
360 // them that we are really using it.
361 return canvas_parser_vector_id(vect);
362 // When the bug is fixed, we can just do this instead:
367 CanvasParser::parse_color(xmlpp::Element *element)
369 assert(element->get_name()=="color");
371 if(element->get_children().empty())
373 error(element, "Undefined value in <color>");
379 xmlpp::Element::NodeList list = element->get_children();
380 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
382 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
386 if(child->get_name()=="r")
388 if(child->get_children().empty())
390 error(element, "Undefined value in <r>");
393 color.set_r(atof(child->get_child_text()->get_content().c_str()));
396 if(child->get_name()=="g")
398 if(child->get_children().empty())
400 error(element, "Undefined value in <g>");
403 color.set_g(atof(child->get_child_text()->get_content().c_str()));
406 if(child->get_name()=="b")
408 if(child->get_children().empty())
410 error(element, "Undefined value in <b>");
413 color.set_b(atof(child->get_child_text()->get_content().c_str()));
416 if(child->get_name()=="a")
418 if(child->get_children().empty())
420 error(element, "Undefined value in <a>");
423 color.set_a(atof(child->get_child_text()->get_content().c_str()));
426 error_unexpected_element(child,child->get_name());
433 CanvasParser::parse_string(xmlpp::Element *element)
435 assert(element->get_name()=="string");
437 if(element->get_children().empty())
439 warning(element, "Undefined value in <string>");
440 return synfig::String();
443 if(element->get_child_text()->get_content().empty())
445 warning(element, "Content element of <string> appears to be empty");
446 return synfig::String();
449 return element->get_child_text()->get_content();
453 CanvasParser::parse_bool(xmlpp::Element *element)
455 assert(element->get_name()=="bool");
457 if(!element->get_children().empty())
458 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
460 if(!element->get_attribute("value"))
462 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
466 string val=element->get_attribute("value")->get_value();
468 if(val=="true" || val=="1")
470 if(val=="false" || val=="0")
473 error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
479 CanvasParser::parse_gradient(xmlpp::Element *node)
481 assert(node->get_name()=="gradient");
484 xmlpp::Element::NodeList list = node->get_children();
485 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
487 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
492 Gradient::CPoint cpoint;
493 cpoint.color=parse_color(child);
495 if(!child->get_attribute("pos"))
497 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
501 cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
503 ret.push_back(cpoint);
511 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
513 vector<ValueBase> value_list;
515 xmlpp::Element::NodeList list = element->get_children();
516 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
518 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
523 value_list.push_back(parse_value(child,canvas));
524 if(!value_list.back().is_valid())
526 value_list.pop_back();
527 error(child,"Bad ValueBase");
536 CanvasParser::parse_segment(xmlpp::Element *element)
538 assert(element->get_name()=="segment");
540 if(element->get_children().empty())
542 error(element, "Undefined value in <segment>");
548 xmlpp::Element::NodeList list = element->get_children();
549 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
551 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
555 if(child->get_name()=="p1")
557 xmlpp::Element::NodeList list = child->get_children();
558 xmlpp::Element::NodeList::iterator iter;
560 // Search for the first non-text XML element
561 for(iter = list.begin(); iter != list.end(); ++iter)
562 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
566 error(element, "Undefined value in <p1>");
570 if((*iter)->get_name()!="vector")
572 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
576 seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
579 if(child->get_name()=="t1")
581 xmlpp::Element::NodeList list = child->get_children();
582 xmlpp::Element::NodeList::iterator iter;
584 // Search for the first non-text XML element
585 for(iter = list.begin(); iter != list.end(); ++iter)
586 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
590 error(element, "Undefined value in <t1>");
594 if((*iter)->get_name()!="vector")
596 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
600 seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
603 if(child->get_name()=="p2")
605 xmlpp::Element::NodeList list = child->get_children();
606 xmlpp::Element::NodeList::iterator iter;
608 // Search for the first non-text XML element
609 for(iter = list.begin(); iter != list.end(); ++iter)
610 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
614 error(element, "Undefined value in <p2>");
618 if((*iter)->get_name()!="vector")
620 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
624 seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
627 if(child->get_name()=="t2")
629 xmlpp::Element::NodeList list = child->get_children();
630 xmlpp::Element::NodeList::iterator iter;
632 // Search for the first non-text XML element
633 for(iter = list.begin(); iter != list.end(); ++iter)
634 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
638 error(element, "Undefined value in <t2>");
642 if((*iter)->get_name()!="vector")
644 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
648 seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
651 error_unexpected_element(child,child->get_name());
657 CanvasParser::parse_bline_point(xmlpp::Element *element)
659 assert(element->get_name()=="bline_point");
660 if(element->get_children().empty())
662 error(element, "Undefined value in <bline_point>");
667 ret.set_split_tangent_flag(false);
669 xmlpp::Element::NodeList list = element->get_children();
670 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
672 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
677 if(child->get_name()[0]=='v' || child->get_name()=="p1")
679 xmlpp::Element::NodeList list = child->get_children();
680 xmlpp::Element::NodeList::iterator iter;
682 // Search for the first non-text XML element
683 for(iter = list.begin(); iter != list.end(); ++iter)
684 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
688 error(element, "Undefined value in <vertex>");
692 if((*iter)->get_name()!="vector")
694 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
698 ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
702 if(child->get_name()=="t1" || child->get_name()=="tangent")
704 xmlpp::Element::NodeList list = child->get_children();
705 xmlpp::Element::NodeList::iterator iter;
707 // Search for the first non-text XML element
708 for(iter = list.begin(); iter != list.end(); ++iter)
709 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
713 error(element, "Undefined value in <t1>");
717 if((*iter)->get_name()!="vector")
719 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
723 ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
727 if(child->get_name()=="t2")
729 xmlpp::Element::NodeList list = child->get_children();
730 xmlpp::Element::NodeList::iterator iter;
732 // Search for the first non-text XML element
733 for(iter = list.begin(); iter != list.end(); ++iter)
734 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
738 error(element, "Undefined value in <t2>");
742 if((*iter)->get_name()!="vector")
744 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
748 ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
749 ret.set_split_tangent_flag(true);
753 if(child->get_name()=="width")
755 xmlpp::Element::NodeList list = child->get_children();
756 xmlpp::Element::NodeList::iterator iter;
758 // Search for the first non-text XML element
759 for(iter = list.begin(); iter != list.end(); ++iter)
760 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
764 error(element, "Undefined value in <width>");
768 if((*iter)->get_name()!="real")
770 error_unexpected_element((*iter),(*iter)->get_name(),"real");
774 ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
778 if(child->get_name()=="origin")
780 xmlpp::Element::NodeList list = child->get_children();
781 xmlpp::Element::NodeList::iterator iter;
783 // Search for the first non-text XML element
784 for(iter = list.begin(); iter != list.end(); ++iter)
785 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
789 error(element, "Undefined value in <origin>");
793 if((*iter)->get_name()!="real")
795 error_unexpected_element((*iter),(*iter)->get_name(),"real");
799 ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
802 error_unexpected_element(child,child->get_name());
808 CanvasParser::parse_angle(xmlpp::Element *element)
810 assert(element->get_name()=="angle");
812 if(!element->get_children().empty())
813 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
815 if(!element->get_attribute("value"))
817 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
821 string val=element->get_attribute("value")->get_value();
823 return Angle::deg(atof(val.c_str()));
827 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
829 if(element->get_name()=="real")
830 return parse_real(element);
832 if(element->get_name()=="time")
833 return parse_time(element,canvas);
835 if(element->get_name()=="integer")
836 return parse_integer(element);
838 if(element->get_name()=="string")
839 return parse_string(element);
841 if(element->get_name()=="vector")
842 return parse_vector(element);
844 if(element->get_name()=="color")
845 return parse_color(element);
847 if(element->get_name()=="segment")
848 return parse_segment(element);
850 if(element->get_name()=="list")
851 return parse_list(element,canvas);
853 if(element->get_name()=="gradient")
854 return parse_gradient(element);
856 if(element->get_name()=="bool")
857 return parse_bool(element);
859 //if(element->get_name()=="canvas")
860 // return parse_canvas(element,canvas,true); // inline canvas
862 if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
863 return parse_angle(element);
865 if(element->get_name()=="bline_point")
866 return parse_bline_point(element);
868 if(element->get_name()=="canvas")
869 return ValueBase(parse_canvas(element,canvas,true));
872 error_unexpected_element(element,element->get_name());
881 ValueNode_Animated::Handle
882 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
884 assert(element->get_name()=="hermite" || element->get_name()=="animated");
886 if(!element->get_attribute("type"))
888 error(element,"Missing attribute \"type\" in <animated>");
889 return ValueNode_Animated::Handle();
892 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
896 error(element,"Bad type in <animated>");
897 return ValueNode_Animated::Handle();
900 ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
904 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_local_name(type).c_str()));
905 return ValueNode_Animated::Handle();
908 value_node->set_root_canvas(canvas->get_root());
910 xmlpp::Element::NodeList list = element->get_children();
911 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
913 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
917 if(child->get_name()=="waypoint")
919 if(!child->get_attribute("time"))
921 error(child,_("<waypoint> is missing attribute \"time\""));
925 Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
928 ValueNode::Handle waypoint_value_node;
929 xmlpp::Element::NodeList list = child->get_children();
931 if(child->get_attribute("use"))
934 warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
936 // the waypoint might look like this, in which case we won't find "mycanvas" in the list of valuenodes, 'cos it's a canvas
938 // <animated type="canvas">
939 // <waypoint time="0s" use="mycanvas"/>
941 if (type==ValueBase::TYPE_CANVAS)
944 waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value(), warnings));
945 warnings_text += warnings;
948 waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
952 if(child->get_children().empty())
954 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
958 xmlpp::Element::NodeList::iterator iter;
960 // Search for the first non-text XML element
961 for(iter = list.begin(); iter != list.end(); ++iter)
962 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
966 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
970 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
973 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
977 error(child,_("Bad data for <waypoint>"));
981 if(!waypoint_value_node)
983 error(child,_("Bad data for <waypoint>"));
987 /*! HACK -- This is a temporary fix to help repair some
988 ** weirdness that is currently going on (10-21-2004).
989 ** This short circuits the linking of waypoints,
990 ** a feature which is so obscure that we can get
991 ** away with something like this pretty easily.
993 waypoint_value_node=waypoint_value_node->clone();
995 // Warn if there is trash after the param value
996 for(iter++; iter != list.end(); ++iter)
997 if(dynamic_cast<xmlpp::Element*>(*iter))
998 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
1003 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
1005 if(child->get_attribute("tension"))
1007 synfig::String str(child->get_attribute("tension")->get_value());
1008 waypoint->set_tension(atof(str.c_str()));
1010 if(child->get_attribute("temporal-tension"))
1012 synfig::String str(child->get_attribute("temporal-tension")->get_value());
1013 waypoint->set_temporal_tension(atof(str.c_str()));
1015 if(child->get_attribute("continuity"))
1017 synfig::String str(child->get_attribute("continuity")->get_value());
1018 waypoint->set_continuity(atof(str.c_str()));
1020 if(child->get_attribute("bias"))
1022 synfig::String str(child->get_attribute("bias")->get_value());
1023 waypoint->set_bias(atof(str.c_str()));
1026 if(child->get_attribute("before"))
1028 string val=child->get_attribute("before")->get_value();
1030 waypoint->set_before(INTERPOLATION_HALT);
1031 else if(val=="constant")
1032 waypoint->set_before(INTERPOLATION_CONSTANT);
1033 else if(val=="linear")
1034 waypoint->set_before(INTERPOLATION_LINEAR);
1035 else if(val=="manual")
1036 waypoint->set_before(INTERPOLATION_MANUAL);
1037 else if(val=="auto")
1038 waypoint->set_before(INTERPOLATION_TCB);
1040 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1043 if(child->get_attribute("after"))
1045 string val=child->get_attribute("after")->get_value();
1047 waypoint->set_after(INTERPOLATION_HALT);
1048 else if(val=="constant")
1049 waypoint->set_after(INTERPOLATION_CONSTANT);
1050 else if(val=="linear")
1051 waypoint->set_after(INTERPOLATION_LINEAR);
1052 else if(val=="manual")
1053 waypoint->set_after(INTERPOLATION_MANUAL);
1054 else if(val=="auto")
1055 waypoint->set_after(INTERPOLATION_TCB);
1057 error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1060 catch(Exception::BadTime x)
1062 warning(child,x.what());
1068 error_unexpected_element(child,child->get_name());
1071 // in canvas version 0.1, angles used to wrap, so to get from -179
1072 // degrees to 180 degrees meant a 1 degree change
1073 // in canvas version 0.2 they don't, so that's a 359 degree change
1075 // when loading a version 0.1 canvas, modify constant angle
1076 // waypoints to that they are within 180 degrees of the previous
1078 if (type == ValueBase::TYPE_ANGLE)
1080 if (canvas->get_version() == "0.1")
1083 Real angle, prev = 0;
1084 WaypointList &wl = value_node->waypoint_list();
1085 for (WaypointList::iterator iter = wl.begin(); iter != wl.end(); iter++)
1087 angle = Angle::deg(iter->get_value(iter->get_time()).get(Angle())).get();
1090 else if (iter->get_value_node()->get_name() == "constant")
1092 if (angle - prev > 180)
1094 while (angle - prev > 180) angle -= 360;
1095 iter->set_value(Angle::deg(angle));
1097 else if (prev - angle > 180)
1099 while (prev - angle > 180) angle += 360;
1100 iter->set_value(Angle::deg(angle));
1108 value_node->changed();
1112 etl::handle<LinkableValueNode>
1113 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1115 // Determine the type
1116 if(!element->get_attribute("type"))
1118 error(element, strprintf(_("Missing attribute \"type\" in <%s>"), element->get_name().c_str()));
1122 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1126 error(element, strprintf(_("Bad type in <%s>"), element->get_name().c_str()));
1130 handle<LinkableValueNode> value_node=LinkableValueNode::create(element->get_name(),type);
1131 handle<ValueNode> c[value_node->link_count()];
1135 error(element, strprintf(_("Error creating ValueNode <%s> with type '%s'. Refer to '%s'"),
1136 element->get_name().c_str(),
1137 ValueBase::type_local_name(type).c_str(),
1138 VALUENODE_COMPATIBILITY_URL));
1142 if(value_node->get_type()!=type)
1144 error(element, strprintf(_("<%s> did not accept type '%s'"),
1145 element->get_name().c_str(),
1146 ValueBase::type_local_name(type).c_str()));
1150 value_node->set_root_canvas(canvas->get_root());
1152 // handle exported valuenodes
1156 xmlpp::Element::AttributeList attrib_list(element->get_attributes());
1157 for(xmlpp::Element::AttributeList::iterator iter = attrib_list.begin(); iter != attrib_list.end(); iter++)
1159 name = (*iter)->get_name();
1160 id = (*iter)->get_value();
1162 if (name == "guid" || name == "id" || name == "type")
1166 index = value_node->get_link_index_from_name(name);
1170 error(element,strprintf(_("'%s' was already defined in <%s>"),
1172 element->get_name().c_str()));
1176 c[index] = canvas->surefind_value_node(id);
1180 error(element, strprintf(_("'%s' attribute in <%s> references unknown ID '%s'"),
1182 element->get_name().c_str(),
1187 if(!value_node->set_link(index, c[index]))
1189 error(element, strprintf(_("Unable to set link '\"%s\" to ValueNode \"%s\" (link #%d in \"%s\")"),
1190 value_node->link_name(index).c_str(),
1193 element->get_name().c_str()));
1197 // printf(" <%s> set link %d (%s) using exported value\n", element->get_name().c_str(), index, name.c_str());
1199 catch (Exception::BadLinkName)
1201 warning(element, strprintf("Bad link name '%s'", name.c_str()));
1203 catch(Exception::IDNotFound)
1205 error(element,"Unable to resolve " + id);
1207 catch(Exception::FileNotFound)
1209 error(element,"Unable to open file referenced in " + id);
1213 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""), id.c_str()));
1219 // handle inline valuenodes
1223 xmlpp::Element::NodeList list = element->get_children();
1224 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1226 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1232 child_name = child->get_name();
1234 index = value_node->get_link_index_from_name(child_name);
1238 error(child, strprintf(_("'%s' was already defined in <%s>"),
1240 element->get_name().c_str()));
1244 xmlpp::Element::NodeList list = child->get_children();
1245 xmlpp::Element::NodeList::iterator iter;
1247 // Search for the first non-text XML element
1248 for(iter = list.begin(); iter != list.end(); ++iter)
1249 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1251 if(iter==list.end())
1253 error(child,strprintf(_("element <%s> is missing its contents"),
1254 child_name.c_str()));
1258 c[index]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1262 error((*iter),strprintf(_("Parse of '%s' failed"),
1263 child_name.c_str()));
1267 if(!value_node->set_link(index,c[index]))
1269 error(child,strprintf(_("Unable to connect value node ('%s' of type '%s') to link %d (%s)"),
1270 c[index]->get_name().c_str(),
1271 ValueBase::type_local_name(c[index]->get_type()).c_str(),
1273 value_node->link_name(index).c_str()));
1277 // \todo do a search for more elements and warn if they are found
1279 // printf(" <%s> set link %d (%s) using inline value\n", element->get_name().c_str(), index, child_name.c_str());
1281 catch(Exception::BadLinkName)
1283 warning(child, strprintf("Bad link name for <%s>", element->get_name().c_str()));
1287 error(child, strprintf(_("Unknown Exception thrown when working on element \"%s\""),child_name.c_str()));
1293 String version(canvas->get_version());
1294 for (int i = 0; i < value_node->link_count(); i++)
1298 // the 'width' parameter of <stripes> wasn't always present in version 0.1 canvases
1299 if (version == "0.1" && element->get_name() == "stripes" && value_node->link_name(i) == "width")
1302 // these 3 blinecalctangent parameters didn't appear until canvas version 0.5
1303 if ((version == "0.1" || version == "0.2" || version == "0.3" || version == "0.4") &&
1304 element->get_name() == "blinecalctangent" &&
1305 (value_node->link_name(i) == "offset" ||
1306 value_node->link_name(i) == "scale" ||
1307 value_node->link_name(i) == "fixed_length"))
1310 // 'scale' was added while canvas version 0.5 was in use
1311 if ((version == "0.3" || version == "0.4" || version == "0.5") &&
1312 element->get_name() == "blinecalcwidth" &&
1313 value_node->link_name(i) == "scale")
1316 // 'loop' was added while canvas version 0.5 was in use, as was the 'gradientcolor' node type
1317 if (version == "0.5" &&
1318 element->get_name() == "gradientcolor" &&
1319 value_node->link_name(i) == "loop")
1322 // 'loop' was added while canvas version 0.6 was in use; the 'random' node was added back when 0.1 was in use
1323 if ((version == "0.1" || version == "0.2" || version == "0.3" || version == "0.4" || version == "0.5" || version == "0.6") &&
1324 element->get_name() == "random" &&
1325 value_node->link_name(i) == "loop")
1328 error(element, strprintf(_("<%s> is missing link %d (%s)"),
1329 element->get_name().c_str(),
1331 value_node->link_name(i).c_str()));
1336 // pre 0.4 canvases had *calctangent outputs scaled down by 0.5 for some reason
1337 if (element->get_name() == "blinecalctangent" || element->get_name() == "segcalctangent")
1339 if (version == "0.1" || version == "0.2" || version == "0.3")
1341 handle<LinkableValueNode> scale_value_node=LinkableValueNode::create("scale",type);
1342 scale_value_node->set_link(scale_value_node->get_link_index_from_name("link"), value_node);
1343 scale_value_node->set_link(scale_value_node->get_link_index_from_name("scalar"), ValueNode_Const::create(Real(0.5)));
1344 value_node = scale_value_node;
1351 // This will also parse a bline
1352 handle<ValueNode_DynamicList>
1353 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1355 assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1357 const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1359 if(!element->get_attribute("type"))
1361 error(element,"Missing attribute \"type\" in <dynamic_list>");
1362 return handle<ValueNode_DynamicList>();
1365 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1369 error(element,"Bad type in <dynamic_list>");
1370 return handle<ValueNode_DynamicList>();
1373 handle<ValueNode_DynamicList> value_node;
1374 handle<ValueNode_BLine> bline_value_node;
1376 if(element->get_name()=="bline")
1378 value_node=bline_value_node=ValueNode_BLine::create();
1379 if(element->get_attribute("loop"))
1381 String loop=element->get_attribute("loop")->get_value();
1382 if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1383 bline_value_node->set_loop(true);
1385 bline_value_node->set_loop(false);
1390 value_node=ValueNode_DynamicList::create(type);
1394 error(element,strprintf(_("Unable to create <dynamic_list>")));
1395 return handle<ValueNode_DynamicList>();
1398 value_node->set_root_canvas(canvas->get_root());
1400 xmlpp::Element::NodeList list = element->get_children();
1401 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1403 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1407 if(child->get_name()=="entry")
1409 ValueNode_DynamicList::ListEntry list_entry;
1411 // Parse begin/end waypoints
1413 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1414 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1416 String begin_sequence;
1417 String end_sequence;
1419 ActivepointList &timing_info(list_entry.timing_info);
1421 if(child->get_attribute("begin"))
1422 begin_sequence=child->get_attribute("begin")->get_value();
1424 if(child->get_attribute("on"))
1425 begin_sequence=child->get_attribute("on")->get_value();
1427 if(child->get_attribute("end"))
1428 end_sequence=child->get_attribute("end")->get_value();
1430 if(child->get_attribute("off"))
1431 end_sequence=child->get_attribute("off")->get_value();
1433 // clear out any auto-start
1434 if(!begin_sequence.empty())
1435 timing_info.clear();
1438 while(!begin_sequence.empty())
1440 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1441 String timecode(begin_sequence.begin(), iter);
1444 // skip whitespace before checking for a priority
1445 while (isspace(timecode[0]))
1446 timecode=timecode.substr(1);
1448 // If there is a priority, then grab it and remove
1449 // it from the timecode
1450 if(timecode[0]=='p')
1452 //priority=timecode[1]-'0';
1453 //timecode=String(timecode.begin()+3,timecode.end());
1454 int space=timecode.find_first_of(' ');
1455 priority=atoi(String(timecode,1,space-1).c_str());
1456 timecode=String(timecode.begin()+space+1,timecode.end());
1457 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1460 timing_info.push_back(
1466 true, // Mark as a "on" activepoint
1471 if(iter==begin_sequence.end())
1472 begin_sequence.clear();
1474 begin_sequence=String(iter+1,begin_sequence.end());
1478 while(!end_sequence.empty())
1480 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1481 String timecode(end_sequence.begin(), iter);
1484 // skip whitespace before checking for a priority
1485 while (isspace(timecode[0]))
1486 timecode=timecode.substr(1);
1488 // If there is a priority, then grab it and remove
1489 // it from the timecode
1490 if(timecode[0]=='p')
1492 //priority=timecode[1]-'0';
1493 //timecode=String(timecode.begin()+3,timecode.end());
1494 int space=timecode.find_first_of(' ');
1495 priority=atoi(String(timecode,1,space-1).c_str());
1496 timecode=String(timecode.begin()+space+1,timecode.end());
1497 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1500 timing_info.push_back(
1506 false, // Mark as a "off" activepoint
1510 if(iter==end_sequence.end())
1511 end_sequence.clear();
1513 end_sequence=String(iter+1,end_sequence.end());
1519 if(child->get_attribute("use"))
1521 // \todo does this need to be able to read 'use="canvas"', like waypoints can now? (see 'surefind_canvas' in this file)
1522 string id=child->get_attribute("use")->get_value();
1525 list_entry.value_node=canvas->surefind_value_node(id);
1527 catch(Exception::IDNotFound)
1529 error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1535 xmlpp::Element::NodeList list = child->get_children();
1536 xmlpp::Element::NodeList::iterator iter;
1538 // Search for the first non-text XML element
1539 for(iter = list.begin(); iter != list.end(); ++iter)
1540 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1542 if(iter==list.end())
1544 error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1548 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1550 if(!list_entry.value_node)
1551 error((*iter),"Parse of ValueNode failed");
1553 // \todo do a search for more elements and warn if they are found
1557 value_node->add(list_entry);
1558 value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1561 error_unexpected_element(child,child->get_name());
1567 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1569 handle<ValueNode> value_node;
1574 if(element->get_attribute("guid"))
1576 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1577 value_node=guid_cast<ValueNode>(guid);
1582 // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1583 if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1585 ValueBase data=parse_value(element,canvas);
1587 if(!data.is_valid())
1589 error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1593 // We want to convert this ValueBase into a
1594 // ValueNode_Const. That way, we can treat the
1595 // ID like any other Datanode. Think of this
1596 // as a shorthand for creating constant ValueNodes.
1598 value_node=ValueNode_Const::create(data);
1601 if(element->get_name()=="hermite" || element->get_name()=="animated")
1602 value_node=parse_animated(element,canvas);
1604 if(element->get_name()=="dynamic_list")
1605 value_node=parse_dynamic_list(element,canvas);
1607 if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1608 value_node=parse_dynamic_list(element,canvas);
1610 if(LinkableValueNode::book().count(element->get_name()))
1612 value_node=parse_linkable_value_node(element,canvas);
1613 if (!value_node) value_node = PlaceholderValueNode::create();
1616 if(element->get_name()=="canvas")
1617 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1620 error_unexpected_element(element,element->get_name());
1621 error(element, strprintf(_("Expected a ValueNode. Refer to '%s'"),
1622 VALUENODE_COMPATIBILITY_URL));
1623 value_node=PlaceholderValueNode::create();
1626 value_node->set_root_canvas(canvas->get_root());
1628 // If we were successful, and our element has
1629 // an ID attribute, go ahead and add it to the
1631 if(value_node && element->get_attribute("id"))
1633 string id=element->get_attribute("id")->get_value();
1635 //value_node->set_id(id);
1637 // If there is already a value_node in the list
1638 // with the same ID, then that is an error
1639 try { canvas->add_value_node(value_node,id); }
1640 catch(Exception::BadLinkName)
1642 warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1645 catch(Exception::IDAlreadyExists)
1647 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1652 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1656 value_node->set_guid(guid);
1661 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1663 assert(element->get_name()=="defs");
1664 xmlpp::Element::NodeList list = element->get_children();
1665 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1667 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1671 if(child->get_name()=="canvas")
1672 parse_canvas(child, canvas);
1674 parse_value_node(child,canvas);
1679 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1682 assert(element->get_name()=="layer");
1683 Layer::Handle layer;
1685 if(!element->get_attribute("type"))
1687 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1688 return Layer::Handle();
1691 layer=Layer::create(element->get_attribute("type")->get_value());
1692 layer->set_canvas(canvas);
1694 if(element->get_attribute("group"))
1696 layer->add_to_group(
1697 element->get_attribute("group")->get_value()
1701 // Handle the version attribute
1702 if(element->get_attribute("version"))
1704 String version(element->get_attribute("version")->get_value());
1705 if(version>layer->get_version())
1706 warning(element,_("Installed layer version is smaller than layer version in file"));
1707 if(version!=layer->get_version())
1708 layer->set_version(version);
1711 // Handle the description
1712 if(element->get_attribute("desc"))
1713 layer->set_description(element->get_attribute("desc")->get_value());
1715 if(element->get_attribute("active"))
1716 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1718 xmlpp::Element::NodeList list = element->get_children();
1719 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1721 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1725 if(child->get_name()=="name")
1726 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1728 if(child->get_name()=="desc")
1729 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1731 if(child->get_name()=="param")
1733 xmlpp::Element::NodeList list = child->get_children();
1735 if(!child->get_attribute("name"))
1737 error(child,_("Missing \"name\" attribute for <param>."));
1741 String param_name=child->get_attribute("name")->get_value();
1743 // SVN r2013 and r2014 renamed all 'pos' and 'offset' parameters to 'origin'
1744 // 'pos' and 'offset' will appear in old .sif files; handle them correctly
1745 if (param_name == "pos" || param_name == "offset")
1746 param_name = "origin";
1748 if(child->get_attribute("use"))
1750 // If the "use" attribute is used, then the
1751 // element should be empty. Warn the user if
1752 // we find otherwise.
1754 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1756 String str= child->get_attribute("use")->get_value();
1759 error(child,_("Empty use=\"\" value in <param>"));
1760 else if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1763 Canvas::Handle c(canvas->surefind_canvas(str, warnings));
1764 warnings_text += warnings;
1765 if(!c) error((*iter),strprintf(_("Failed to load subcanvas '%s'"), str.c_str()));
1766 if(!layer->set_param(param_name,c))
1767 error((*iter),_("Layer rejected canvas link"));
1772 handle<ValueNode> value_node=canvas->surefind_value_node(str);
1774 // Assign the value_node to the dynamic parameter list
1775 if (param_name == "segment_list" && (layer->get_name() == "region" || layer->get_name() == "outline"))
1777 synfig::warning("%s: Updated valuenode connection to use the \"bline\" parameter instead of \"segment_list\".",
1778 layer->get_name().c_str());
1779 param_name = "bline";
1781 layer->connect_dynamic_param(param_name,value_node);
1783 catch(Exception::IDNotFound)
1785 error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1791 xmlpp::Element::NodeList::iterator iter;
1793 // Search for the first non-text XML element
1794 for(iter = list.begin(); iter != list.end(); ++iter)
1795 if(dynamic_cast<xmlpp::Element*>(*iter))
1797 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1799 if(iter==list.end())
1801 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1805 // If we recognize the element name as a
1806 // ValueBase, then treat is at one
1807 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1809 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1811 if(!data.is_valid())
1813 error((*iter),_("Bad data for <param>"));
1817 // Set the layer's parameter, and make sure that
1818 // the layer liked it
1819 if(!layer->set_param(param_name,data))
1821 warning((*iter),strprintf(_("Layer '%s' rejected value for parameter '%s'"),
1822 element->get_attribute("type")->get_value().c_str(),
1823 param_name.c_str()));
1827 else // ... otherwise, we assume that it is a ValueNode
1829 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1833 error((*iter),_("Bad data for <param>"));
1837 // Assign the value_node to the dynamic parameter list
1838 layer->connect_dynamic_param(param_name,value_node);
1841 // Warn if there is trash after the param value
1842 for(iter++; iter != list.end(); ++iter)
1843 if(dynamic_cast<xmlpp::Element*>(*iter))
1844 warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1848 error_unexpected_element(child,child->get_name());
1851 layer->reset_version();
1856 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1859 if(element->get_name()!="canvas")
1861 error_unexpected_element(element,element->get_name(),"canvas");
1862 return Canvas::Handle();
1864 Canvas::Handle canvas;
1868 if(parent && (element->get_attribute("id") || inline_))
1872 canvas=Canvas::create_inline(parent);
1879 canvas=parent->find_canvas(element->get_attribute("id")->get_value(), warnings);
1880 warnings_text += warnings;
1884 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1887 canvas->rend_desc().clear_flags();
1891 canvas=Canvas::create();
1892 if(filename=="/dev/stdin")
1893 canvas->set_file_name("./stdin.sif");
1895 canvas->set_file_name(filename);
1896 canvas->rend_desc().clear_flags();
1899 if(element->get_attribute("guid"))
1901 GUID guid(element->get_attribute("guid")->get_value());
1902 if(guid_cast<Canvas>(guid))
1903 return guid_cast<Canvas>(guid);
1905 canvas->set_guid(guid);
1908 if(element->get_attribute("version"))
1909 canvas->set_version(element->get_attribute("version")->get_value());
1911 canvas->set_version(parent->get_version());
1913 if(element->get_attribute("width"))
1915 int width = atoi(element->get_attribute("width")->get_value().c_str());
1917 fatal_error(element, _("Canvas with width or height less than one is not allowed"));
1918 canvas->rend_desc().set_w(width);
1921 if(element->get_attribute("height"))
1923 int height = atoi(element->get_attribute("height")->get_value().c_str());
1925 fatal_error(element, _("Canvas with width or height less than one is not allowed"));
1926 canvas->rend_desc().set_h(height);
1929 if(element->get_attribute("xres"))
1930 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1932 if(element->get_attribute("yres"))
1933 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1936 if(element->get_attribute("fps"))
1937 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1939 if(element->get_attribute("start-time"))
1940 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1942 if(element->get_attribute("begin-time"))
1943 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1945 if(element->get_attribute("end-time"))
1946 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1948 if(element->get_attribute("antialias"))
1949 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1951 if(element->get_attribute("view-box"))
1953 string values=element->get_attribute("view-box")->get_value();
1957 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1958 values=string(values.begin()+values.find(' ')+1,values.end());
1959 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1960 values=string(values.begin()+values.find(' ')+1,values.end());
1961 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1962 values=string(values.begin()+values.find(' ')+1,values.end());
1963 br[1]=atof(values.c_str());
1965 canvas->rend_desc().set_tl(tl);
1966 canvas->rend_desc().set_br(br);
1969 if(element->get_attribute("bgcolor"))
1971 string values=element->get_attribute("bgcolor")->get_value();
1974 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1975 values=string(values.begin()+values.find(' ')+1,values.end());
1977 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1978 values=string(values.begin()+values.find(' ')+1,values.end());
1980 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1981 values=string(values.begin()+values.find(' ')+1,values.end());
1983 bg.set_a(atof(values.c_str()));
1985 canvas->rend_desc().set_bg_color(bg);
1988 if(element->get_attribute("focus"))
1990 string values=element->get_attribute("focus")->get_value();
1993 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1994 values=string(values.begin()+values.find(' ')+1,values.end());
1995 focus[1]=atof(values.c_str());
1997 canvas->rend_desc().set_focus(focus);
2000 canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2002 xmlpp::Element::NodeList list = element->get_children();
2003 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2005 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
2008 if(child->get_name()=="defs")
2010 if(canvas->is_inline())
2011 error(child,_("Inline canvas cannot have a <defs> section"));
2012 parse_canvas_defs(child, canvas);
2015 if(child->get_name()=="keyframe")
2017 if(canvas->is_inline())
2019 warning(child,_("Inline canvas cannot have keyframes"));
2023 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2024 canvas->keyframe_list().sync();
2027 if(child->get_name()=="meta")
2029 if(canvas->is_inline())
2031 warning(child,_("Inline canvases cannot have metadata"));
2035 String name,content;
2037 if(!child->get_attribute("name"))
2039 warning(child,_("<meta> must have a name"));
2043 if(!child->get_attribute("content"))
2045 warning(child,_("<meta> must have content"));
2049 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2051 else if(child->get_name()=="name")
2053 xmlpp::Element::NodeList list = child->get_children();
2055 // If we don't have any name, warn
2057 warning(child,_("blank \"name\" entity"));
2060 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2061 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2062 canvas->set_name(tmp);
2065 if(child->get_name()=="desc")
2068 xmlpp::Element::NodeList list = child->get_children();
2070 // If we don't have any description, warn
2072 warning(child,_("blank \"desc\" entity"));
2075 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2076 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2077 canvas->set_description(tmp);
2080 if(child->get_name()=="author")
2083 xmlpp::Element::NodeList list = child->get_children();
2085 // If we don't have any description, warn
2087 warning(child,_("blank \"author\" entity"));
2090 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2091 if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2092 canvas->set_author(tmp);
2095 if(child->get_name()=="layer")
2097 //if(canvas->is_inline())
2098 // canvas->push_front(parse_layer(child,canvas->parent()));
2100 canvas->push_front(parse_layer(child,canvas));
2103 error_unexpected_element(child,child->get_name());
2106 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2110 if(canvas->value_node_list().placeholder_count())
2113 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2114 if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2116 if (nodes != "") nodes += ", ";
2117 nodes += "'" + (*iter)->get_id() + "'";
2119 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2120 canvas->get_id().c_str(),
2121 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2125 canvas->set_version(CURRENT_CANVAS_VERSION);
2130 CanvasParser::register_canvas_in_map(Canvas::Handle canvas, String as)
2132 get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2133 canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2134 canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2139 CanvasParser::show_canvas_map(String file, int line, String text)
2142 printf(" .-----\n | %s:%d %s\n", file.c_str(), line, text.c_str());
2143 std::map<synfig::String, etl::loose_handle<Canvas> > canvas_map(synfig::get_open_canvas_map());
2144 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
2145 for (iter = canvas_map.begin(); iter != canvas_map.end(); iter++)
2147 synfig::String first(iter->first);
2148 etl::loose_handle<Canvas> second(iter->second);
2149 printf(" | %40s : %lx (%d)\n", first.c_str(), ulong(&*second), second->count());
2151 printf(" `-----\n\n");
2156 CanvasParser::parse_from_file_as(const String &file_,const String &as_,String &errors)
2158 ChangeLocale change_locale(LC_NUMERIC, "C");
2159 String file(unix_to_local_path(file_));
2160 String as(unix_to_local_path(as_));
2164 if(get_open_canvas_map().count(etl::absolute_path(as)))
2165 return get_open_canvas_map()[etl::absolute_path(as)];
2169 xmlpp::DomParser parser(file);
2172 Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2173 if (!canvas) return canvas;
2174 register_canvas_in_map(canvas, as);
2176 const ValueNodeList& value_node_list(canvas->value_node_list());
2179 ValueNodeList::const_iterator iter;
2180 for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2182 ValueNode::Handle value_node(*iter);
2183 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2185 canvas->remove_value_node(value_node);
2193 catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2194 catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2195 catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2196 catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2197 catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2198 catch(xmlpp::internal_error x)
2200 if (!strcmp(x.what(), "Couldn't create parsing context"))
2201 throw runtime_error(String(" * ") + _("Can't open file") + " \"" + file + "\"");
2204 catch(const std::exception& ex)
2206 synfig::error("Standard Exception: "+String(ex.what()));
2208 return Canvas::Handle();
2210 catch(const String& str)
2213 // synfig::error(str);
2215 return Canvas::Handle();
2217 return Canvas::Handle();