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 error(element
, strprintf(_("<%s> is missing link %d (%s)"),
1323 element
->get_name().c_str(),
1325 value_node
->link_name(i
).c_str()));
1330 // pre 0.4 canvases had *calctangent outputs scaled down by 0.5 for some reason
1331 if (element
->get_name() == "blinecalctangent" || element
->get_name() == "segcalctangent")
1333 if (version
== "0.1" || version
== "0.2" || version
== "0.3")
1335 handle
<LinkableValueNode
> scale_value_node
=LinkableValueNode::create("scale",type
);
1336 scale_value_node
->set_link(scale_value_node
->get_link_index_from_name("link"), value_node
);
1337 scale_value_node
->set_link(scale_value_node
->get_link_index_from_name("scalar"), ValueNode_Const::create(Real(0.5)));
1338 value_node
= scale_value_node
;
1345 // This will also parse a bline
1346 handle
<ValueNode_DynamicList
>
1347 CanvasParser::parse_dynamic_list(xmlpp::Element
*element
,Canvas::Handle canvas
)
1349 assert(element
->get_name()=="dynamic_list" || element
->get_name()=="bline");
1351 const float fps(canvas
?canvas
->rend_desc().get_frame_rate():0);
1353 if(!element
->get_attribute("type"))
1355 error(element
,"Missing attribute \"type\" in <dynamic_list>");
1356 return handle
<ValueNode_DynamicList
>();
1359 ValueBase::Type type
=ValueBase::ident_type(element
->get_attribute("type")->get_value());
1363 error(element
,"Bad type in <dynamic_list>");
1364 return handle
<ValueNode_DynamicList
>();
1367 handle
<ValueNode_DynamicList
> value_node
;
1368 handle
<ValueNode_BLine
> bline_value_node
;
1370 if(element
->get_name()=="bline")
1372 value_node
=bline_value_node
=ValueNode_BLine::create();
1373 if(element
->get_attribute("loop"))
1375 String loop
=element
->get_attribute("loop")->get_value();
1376 if(loop
=="true" || loop
=="1" || loop
=="TRUE" || loop
=="True")
1377 bline_value_node
->set_loop(true);
1379 bline_value_node
->set_loop(false);
1384 value_node
=ValueNode_DynamicList::create(type
);
1388 error(element
,strprintf(_("Unable to create <dynamic_list>")));
1389 return handle
<ValueNode_DynamicList
>();
1392 value_node
->set_root_canvas(canvas
->get_root());
1394 xmlpp::Element::NodeList list
= element
->get_children();
1395 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1397 xmlpp::Element
*child(dynamic_cast<xmlpp::Element
*>(*iter
));
1401 if(child
->get_name()=="entry")
1403 ValueNode_DynamicList::ListEntry list_entry
;
1405 // Parse begin/end waypoints
1407 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint
;
1408 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList
;
1410 String begin_sequence
;
1411 String end_sequence
;
1413 ActivepointList
&timing_info(list_entry
.timing_info
);
1415 if(child
->get_attribute("begin"))
1416 begin_sequence
=child
->get_attribute("begin")->get_value();
1418 if(child
->get_attribute("on"))
1419 begin_sequence
=child
->get_attribute("on")->get_value();
1421 if(child
->get_attribute("end"))
1422 end_sequence
=child
->get_attribute("end")->get_value();
1424 if(child
->get_attribute("off"))
1425 end_sequence
=child
->get_attribute("off")->get_value();
1427 // clear out any auto-start
1428 if(!begin_sequence
.empty())
1429 timing_info
.clear();
1432 while(!begin_sequence
.empty())
1434 String::iterator
iter(find(begin_sequence
.begin(),begin_sequence
.end(),','));
1435 String
timecode(begin_sequence
.begin(), iter
);
1438 // skip whitespace before checking for a priority
1439 while (isspace(timecode
[0]))
1440 timecode
=timecode
.substr(1);
1442 // If there is a priority, then grab it and remove
1443 // it from the timecode
1444 if(timecode
[0]=='p')
1446 //priority=timecode[1]-'0';
1447 //timecode=String(timecode.begin()+3,timecode.end());
1448 int space
=timecode
.find_first_of(' ');
1449 priority
=atoi(String(timecode
,1,space
-1).c_str());
1450 timecode
=String(timecode
.begin()+space
+1,timecode
.end());
1451 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1454 timing_info
.push_back(
1460 true, // Mark as a "on" activepoint
1465 if(iter
==begin_sequence
.end())
1466 begin_sequence
.clear();
1468 begin_sequence
=String(iter
+1,begin_sequence
.end());
1472 while(!end_sequence
.empty())
1474 String::iterator
iter(find(end_sequence
.begin(),end_sequence
.end(),','));
1475 String
timecode(end_sequence
.begin(), iter
);
1478 // skip whitespace before checking for a priority
1479 while (isspace(timecode
[0]))
1480 timecode
=timecode
.substr(1);
1482 // If there is a priority, then grab it and remove
1483 // it from the timecode
1484 if(timecode
[0]=='p')
1486 //priority=timecode[1]-'0';
1487 //timecode=String(timecode.begin()+3,timecode.end());
1488 int space
=timecode
.find_first_of(' ');
1489 priority
=atoi(String(timecode
,1,space
-1).c_str());
1490 timecode
=String(timecode
.begin()+space
+1,timecode
.end());
1491 //synfig::info("priority: %d timecode: %s",priority,timecode.c_str());
1494 timing_info
.push_back(
1500 false, // Mark as a "off" activepoint
1504 if(iter
==end_sequence
.end())
1505 end_sequence
.clear();
1507 end_sequence
=String(iter
+1,end_sequence
.end());
1513 if(child
->get_attribute("use"))
1515 // \todo does this need to be able to read 'use="canvas"', like waypoints can now? (see 'surefind_canvas' in this file)
1516 string id
=child
->get_attribute("use")->get_value();
1519 list_entry
.value_node
=canvas
->surefind_value_node(id
);
1521 catch(Exception::IDNotFound
)
1523 error(child
,"\"use\" attribute in <entry> references unknown ID -- "+id
);
1529 xmlpp::Element::NodeList list
= child
->get_children();
1530 xmlpp::Element::NodeList::iterator iter
;
1532 // Search for the first non-text XML element
1533 for(iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1534 if(dynamic_cast<xmlpp::Element
*>(*iter
)) break;
1536 if(iter
==list
.end())
1538 error(child
,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1542 list_entry
.value_node
=parse_value_node(dynamic_cast<xmlpp::Element
*>(*iter
),canvas
);
1544 if(!list_entry
.value_node
)
1545 error((*iter
),"Parse of ValueNode failed");
1547 // \todo do a search for more elements and warn if they are found
1551 value_node
->add(list_entry
);
1552 value_node
->set_link(value_node
->link_count()-1,list_entry
.value_node
);
1555 error_unexpected_element(child
,child
->get_name());
1561 CanvasParser::parse_value_node(xmlpp::Element
*element
,Canvas::Handle canvas
)
1563 handle
<ValueNode
> value_node
;
1568 if(element
->get_attribute("guid"))
1570 guid
=GUID(element
->get_attribute("guid")->get_value())^canvas
->get_root()->get_guid();
1571 value_node
=guid_cast
<ValueNode
>(guid
);
1576 // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1577 if(element
->get_name()!="canvas" && ValueBase::ident_type(element
->get_name()))
1579 ValueBase data
=parse_value(element
,canvas
);
1581 if(!data
.is_valid())
1583 error(element
,strprintf(_("Bad data in <%s>"),element
->get_name().c_str()));
1587 // We want to convert this ValueBase into a
1588 // ValueNode_Const. That way, we can treat the
1589 // ID like any other Datanode. Think of this
1590 // as a shorthand for creating constant ValueNodes.
1592 value_node
=ValueNode_Const::create(data
);
1595 if(element
->get_name()=="hermite" || element
->get_name()=="animated")
1596 value_node
=parse_animated(element
,canvas
);
1598 if(element
->get_name()=="dynamic_list")
1599 value_node
=parse_dynamic_list(element
,canvas
);
1601 if(element
->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1602 value_node
=parse_dynamic_list(element
,canvas
);
1604 if(LinkableValueNode::book().count(element
->get_name()))
1606 value_node
=parse_linkable_value_node(element
,canvas
);
1607 if (!value_node
) value_node
= PlaceholderValueNode::create();
1610 if(element
->get_name()=="canvas")
1611 value_node
=ValueNode_Const::create(parse_canvas(element
,canvas
,true));
1614 error_unexpected_element(element
,element
->get_name());
1615 error(element
, strprintf(_("Expected a ValueNode. Refer to '%s'"),
1616 VALUENODE_COMPATIBILITY_URL
));
1617 value_node
=PlaceholderValueNode::create();
1620 value_node
->set_root_canvas(canvas
->get_root());
1622 // If we were successful, and our element has
1623 // an ID attribute, go ahead and add it to the
1625 if(value_node
&& element
->get_attribute("id"))
1627 string id
=element
->get_attribute("id")->get_value();
1629 //value_node->set_id(id);
1631 // If there is already a value_node in the list
1632 // with the same ID, then that is an error
1633 try { canvas
->add_value_node(value_node
,id
); }
1634 catch(Exception::BadLinkName
)
1636 warning(element
,strprintf(_("Bad ID \"%s\""),id
.c_str()));
1639 catch(Exception::IDAlreadyExists
)
1641 error(element
,strprintf(_("Duplicate ID \"%s\""),id
.c_str()));
1646 error(element
,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id
.c_str()));
1650 value_node
->set_guid(guid
);
1655 CanvasParser::parse_canvas_defs(xmlpp::Element
*element
,Canvas::Handle canvas
)
1657 assert(element
->get_name()=="defs");
1658 xmlpp::Element::NodeList list
= element
->get_children();
1659 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1661 xmlpp::Element
*child(dynamic_cast<xmlpp::Element
*>(*iter
));
1665 if(child
->get_name()=="canvas")
1666 parse_canvas(child
, canvas
);
1668 parse_value_node(child
,canvas
);
1673 CanvasParser::parse_layer(xmlpp::Element
*element
,Canvas::Handle canvas
)
1676 assert(element
->get_name()=="layer");
1677 Layer::Handle layer
;
1679 if(!element
->get_attribute("type"))
1681 error(element
,_("Missing \"type\" attribute to \"layer\" element"));
1682 return Layer::Handle();
1685 layer
=Layer::create(element
->get_attribute("type")->get_value());
1686 layer
->set_canvas(canvas
);
1688 if(element
->get_attribute("group"))
1690 layer
->add_to_group(
1691 element
->get_attribute("group")->get_value()
1695 // Handle the version attribute
1696 if(element
->get_attribute("version"))
1698 String
version(element
->get_attribute("version")->get_value());
1699 if(version
>layer
->get_version())
1700 warning(element
,_("Installed layer version is smaller than layer version in file"));
1701 if(version
!=layer
->get_version())
1702 layer
->set_version(version
);
1705 // Handle the description
1706 if(element
->get_attribute("desc"))
1707 layer
->set_description(element
->get_attribute("desc")->get_value());
1709 if(element
->get_attribute("active"))
1710 layer
->set_active(element
->get_attribute("active")->get_value()=="false"?false:true);
1712 xmlpp::Element::NodeList list
= element
->get_children();
1713 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1715 xmlpp::Element
*child(dynamic_cast<xmlpp::Element
*>(*iter
));
1719 if(child
->get_name()=="name")
1720 warning(child
,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1722 if(child
->get_name()=="desc")
1723 warning(child
,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1725 if(child
->get_name()=="param")
1727 xmlpp::Element::NodeList list
= child
->get_children();
1729 if(!child
->get_attribute("name"))
1731 error(child
,_("Missing \"name\" attribute for <param>."));
1735 String param_name
=child
->get_attribute("name")->get_value();
1737 // SVN r2013 and r2014 renamed all 'pos' and 'offset' parameters to 'origin'
1738 // 'pos' and 'offset' will appear in old .sif files; handle them correctly
1739 if (param_name
== "pos" || param_name
== "offset")
1740 param_name
= "origin";
1742 if(child
->get_attribute("use"))
1744 // If the "use" attribute is used, then the
1745 // element should be empty. Warn the user if
1746 // we find otherwise.
1748 warning(child
,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1750 String str
= child
->get_attribute("use")->get_value();
1753 error(child
,_("Empty use=\"\" value in <param>"));
1754 else if(layer
->get_param(param_name
).get_type()==ValueBase::TYPE_CANVAS
)
1757 Canvas::Handle
c(canvas
->surefind_canvas(str
, warnings
));
1758 warnings_text
+= warnings
;
1759 if(!c
) error((*iter
),strprintf(_("Failed to load subcanvas '%s'"), str
.c_str()));
1760 if(!layer
->set_param(param_name
,c
))
1761 error((*iter
),_("Layer rejected canvas link"));
1766 handle
<ValueNode
> value_node
=canvas
->surefind_value_node(str
);
1768 // Assign the value_node to the dynamic parameter list
1769 if (param_name
== "segment_list" && (layer
->get_name() == "region" || layer
->get_name() == "outline"))
1771 synfig::warning("%s: Updated valuenode connection to use the \"bline\" parameter instead of \"segment_list\".",
1772 layer
->get_name().c_str());
1773 param_name
= "bline";
1775 layer
->connect_dynamic_param(param_name
,value_node
);
1777 catch(Exception::IDNotFound
)
1779 error(child
,strprintf(_("Unknown ID (%s) referenced in <param>"),str
.c_str()));
1785 xmlpp::Element::NodeList::iterator iter
;
1787 // Search for the first non-text XML element
1788 for(iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1789 if(dynamic_cast<xmlpp::Element
*>(*iter
))
1791 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment" )) break;
1793 if(iter
==list
.end())
1795 error(child
,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1799 // If we recognize the element name as a
1800 // ValueBase, then treat is at one
1801 if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter
)->get_name()) && !dynamic_cast<xmlpp::Element
*>(*iter
)->get_attribute("guid"))
1803 ValueBase data
=parse_value(dynamic_cast<xmlpp::Element
*>(*iter
),canvas
);
1805 if(!data
.is_valid())
1807 error((*iter
),_("Bad data for <param>"));
1811 // Set the layer's parameter, and make sure that
1812 // the layer liked it
1813 if(!layer
->set_param(param_name
,data
))
1815 warning((*iter
),strprintf(_("Layer '%s' rejected value for parameter '%s'"),
1816 element
->get_attribute("type")->get_value().c_str(),
1817 param_name
.c_str()));
1821 else // ... otherwise, we assume that it is a ValueNode
1823 handle
<ValueNode
> value_node
=parse_value_node(dynamic_cast<xmlpp::Element
*>(*iter
),canvas
);
1827 error((*iter
),_("Bad data for <param>"));
1831 // Assign the value_node to the dynamic parameter list
1832 layer
->connect_dynamic_param(param_name
,value_node
);
1835 // Warn if there is trash after the param value
1836 for(iter
++; iter
!= list
.end(); ++iter
)
1837 if(dynamic_cast<xmlpp::Element
*>(*iter
))
1838 warning((*iter
),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter
)->get_name().c_str()));
1842 error_unexpected_element(child
,child
->get_name());
1845 layer
->reset_version();
1850 CanvasParser::parse_canvas(xmlpp::Element
*element
,Canvas::Handle parent
,bool inline_
, String filename
)
1853 if(element
->get_name()!="canvas")
1855 error_unexpected_element(element
,element
->get_name(),"canvas");
1856 return Canvas::Handle();
1858 Canvas::Handle canvas
;
1862 if(parent
&& (element
->get_attribute("id") || inline_
))
1866 canvas
=Canvas::create_inline(parent
);
1873 canvas
=parent
->find_canvas(element
->get_attribute("id")->get_value(), warnings
);
1874 warnings_text
+= warnings
;
1878 canvas
=parent
->new_child_canvas(element
->get_attribute("id")->get_value());
1881 canvas
->rend_desc().clear_flags();
1885 canvas
=Canvas::create();
1886 if(filename
=="/dev/stdin")
1887 canvas
->set_file_name("./stdin.sif");
1889 canvas
->set_file_name(filename
);
1890 canvas
->rend_desc().clear_flags();
1893 if(element
->get_attribute("guid"))
1895 GUID
guid(element
->get_attribute("guid")->get_value());
1896 if(guid_cast
<Canvas
>(guid
))
1897 return guid_cast
<Canvas
>(guid
);
1899 canvas
->set_guid(guid
);
1902 if(element
->get_attribute("version"))
1903 canvas
->set_version(element
->get_attribute("version")->get_value());
1905 canvas
->set_version(parent
->get_version());
1907 if(element
->get_attribute("width"))
1909 int width
= atoi(element
->get_attribute("width")->get_value().c_str());
1911 fatal_error(element
, _("Canvas with width or height less than one is not allowed"));
1912 canvas
->rend_desc().set_w(width
);
1915 if(element
->get_attribute("height"))
1917 int height
= atoi(element
->get_attribute("height")->get_value().c_str());
1919 fatal_error(element
, _("Canvas with width or height less than one is not allowed"));
1920 canvas
->rend_desc().set_h(height
);
1923 if(element
->get_attribute("xres"))
1924 canvas
->rend_desc().set_x_res(atof(element
->get_attribute("xres")->get_value().c_str()));
1926 if(element
->get_attribute("yres"))
1927 canvas
->rend_desc().set_y_res(atof(element
->get_attribute("yres")->get_value().c_str()));
1930 if(element
->get_attribute("fps"))
1931 canvas
->rend_desc().set_frame_rate(atof(element
->get_attribute("fps")->get_value().c_str()));
1933 if(element
->get_attribute("start-time"))
1934 canvas
->rend_desc().set_time_start(Time(element
->get_attribute("start-time")->get_value(),canvas
->rend_desc().get_frame_rate()));
1936 if(element
->get_attribute("begin-time"))
1937 canvas
->rend_desc().set_time_start(Time(element
->get_attribute("begin-time")->get_value(),canvas
->rend_desc().get_frame_rate()));
1939 if(element
->get_attribute("end-time"))
1940 canvas
->rend_desc().set_time_end(Time(element
->get_attribute("end-time")->get_value(),canvas
->rend_desc().get_frame_rate()));
1942 if(element
->get_attribute("antialias"))
1943 canvas
->rend_desc().set_antialias(atoi(element
->get_attribute("antialias")->get_value().c_str()));
1945 if(element
->get_attribute("view-box"))
1947 string values
=element
->get_attribute("view-box")->get_value();
1951 tl
[0]=atof(string(values
.data(),values
.find(' ')).c_str());
1952 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1953 tl
[1]=atof(string(values
.data(),values
.find(' ')).c_str());
1954 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1955 br
[0]=atof(string(values
.data(),values
.find(' ')).c_str());
1956 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1957 br
[1]=atof(values
.c_str());
1959 canvas
->rend_desc().set_tl(tl
);
1960 canvas
->rend_desc().set_br(br
);
1963 if(element
->get_attribute("bgcolor"))
1965 string values
=element
->get_attribute("bgcolor")->get_value();
1968 bg
.set_r(atof(string(values
.data(),values
.find(' ')).c_str()));
1969 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1971 bg
.set_g(atof(string(values
.data(),values
.find(' ')).c_str()));
1972 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1974 bg
.set_b(atof(string(values
.data(),values
.find(' ')).c_str()));
1975 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1977 bg
.set_a(atof(values
.c_str()));
1979 canvas
->rend_desc().set_bg_color(bg
);
1982 if(element
->get_attribute("focus"))
1984 string values
=element
->get_attribute("focus")->get_value();
1987 focus
[0]=atof(string(values
.data(),values
.find(' ')).c_str());
1988 values
=string(values
.begin()+values
.find(' ')+1,values
.end());
1989 focus
[1]=atof(values
.c_str());
1991 canvas
->rend_desc().set_focus(focus
);
1994 canvas
->rend_desc().set_flags(RendDesc::PX_ASPECT
|RendDesc::IM_SPAN
);
1996 xmlpp::Element::NodeList list
= element
->get_children();
1997 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
1999 xmlpp::Element
*child(dynamic_cast<xmlpp::Element
*>(*iter
));
2002 if(child
->get_name()=="defs")
2004 if(canvas
->is_inline())
2005 error(child
,_("Inline canvas cannot have a <defs> section"));
2006 parse_canvas_defs(child
, canvas
);
2009 if(child
->get_name()=="keyframe")
2011 if(canvas
->is_inline())
2013 warning(child
,_("Inline canvas cannot have keyframes"));
2017 canvas
->keyframe_list().add(parse_keyframe(child
,canvas
));
2018 canvas
->keyframe_list().sync();
2021 if(child
->get_name()=="meta")
2023 if(canvas
->is_inline())
2025 warning(child
,_("Inline canvases cannot have metadata"));
2029 String name
,content
;
2031 if(!child
->get_attribute("name"))
2033 warning(child
,_("<meta> must have a name"));
2037 if(!child
->get_attribute("content"))
2039 warning(child
,_("<meta> must have content"));
2043 canvas
->set_meta_data(child
->get_attribute("name")->get_value(),child
->get_attribute("content")->get_value());
2045 else if(child
->get_name()=="name")
2047 xmlpp::Element::NodeList list
= child
->get_children();
2049 // If we don't have any name, warn
2051 warning(child
,_("blank \"name\" entity"));
2054 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
2055 if(dynamic_cast<xmlpp::TextNode
*>(*iter
))tmp
+=dynamic_cast<xmlpp::TextNode
*>(*iter
)->get_content();
2056 canvas
->set_name(tmp
);
2059 if(child
->get_name()=="desc")
2062 xmlpp::Element::NodeList list
= child
->get_children();
2064 // If we don't have any description, warn
2066 warning(child
,_("blank \"desc\" entity"));
2069 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
2070 if(dynamic_cast<xmlpp::TextNode
*>(*iter
))tmp
+=dynamic_cast<xmlpp::TextNode
*>(*iter
)->get_content();
2071 canvas
->set_description(tmp
);
2074 if(child
->get_name()=="author")
2077 xmlpp::Element::NodeList list
= child
->get_children();
2079 // If we don't have any description, warn
2081 warning(child
,_("blank \"author\" entity"));
2084 for(xmlpp::Element::NodeList::iterator iter
= list
.begin(); iter
!= list
.end(); ++iter
)
2085 if(dynamic_cast<xmlpp::TextNode
*>(*iter
))tmp
+=dynamic_cast<xmlpp::TextNode
*>(*iter
)->get_content();
2086 canvas
->set_author(tmp
);
2089 if(child
->get_name()=="layer")
2091 //if(canvas->is_inline())
2092 // canvas->push_front(parse_layer(child,canvas->parent()));
2094 canvas
->push_front(parse_layer(child
,canvas
));
2097 error_unexpected_element(child
,child
->get_name());
2100 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2104 if(canvas
->value_node_list().placeholder_count())
2107 for (ValueNodeList::const_iterator iter
= canvas
->value_node_list().begin(); iter
!= canvas
->value_node_list().end(); iter
++)
2108 if(PlaceholderValueNode::Handle::cast_dynamic(*iter
))
2110 if (nodes
!= "") nodes
+= ", ";
2111 nodes
+= "'" + (*iter
)->get_id() + "'";
2113 error(element
,strprintf(_("Canvas '%s' has undefined %s: %s"),
2114 canvas
->get_id().c_str(),
2115 canvas
->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2119 canvas
->set_version(CURRENT_CANVAS_VERSION
);
2124 CanvasParser::parse_from_file_as(const String
&file_
,const String
&as_
,String
&errors
)
2126 ChangeLocale
change_locale(LC_NUMERIC
, "C");
2127 String
file(unix_to_local_path(file_
));
2128 String
as(unix_to_local_path(as_
));
2132 if(get_open_canvas_map().count(etl::absolute_path(as
)))
2133 return get_open_canvas_map()[etl::absolute_path(as
)];
2137 xmlpp::DomParser
parser(file
);
2140 Canvas::Handle
canvas(parse_canvas(parser
.get_document()->get_root_node(),0,false,as
));
2141 if (!canvas
) return canvas
;
2142 get_open_canvas_map()[etl::absolute_path(as
)]=canvas
;
2143 canvas
->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map
),canvas
.get()));
2144 canvas
->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed
),canvas
.get()));
2146 const ValueNodeList
& value_node_list(canvas
->value_node_list());
2149 ValueNodeList::const_iterator iter
;
2150 for(iter
=value_node_list
.begin();iter
!=value_node_list
.end();++iter
)
2152 ValueNode::Handle
value_node(*iter
);
2153 if(value_node
->is_exported() && value_node
->get_id().find("Unnamed")==0)
2155 canvas
->remove_value_node(value_node
);
2163 catch(Exception::BadLinkName
) { synfig::error("BadLinkName Thrown"); }
2164 catch(Exception::BadType
) { synfig::error("BadType Thrown"); }
2165 catch(Exception::FileNotFound
) { synfig::error("FileNotFound Thrown"); }
2166 catch(Exception::IDNotFound
) { synfig::error("IDNotFound Thrown"); }
2167 catch(Exception::IDAlreadyExists
) { synfig::error("IDAlreadyExists Thrown"); }
2168 catch(xmlpp::internal_error x
)
2170 printf("caught internal error\n");
2171 if (!strcmp(x
.what(), "Couldn't create parsing context"))
2172 throw runtime_error(String(" * ") + _("Can't open file") + " \"" + file
+ "\"");
2175 catch(const std::exception
& ex
)
2177 synfig::error("Standard Exception: "+String(ex
.what()));
2179 return Canvas::Handle();
2181 catch(const String
& str
)
2184 // synfig::error(str);
2186 return Canvas::Handle();
2188 return Canvas::Handle();