When loading .sif files, show any warnings in the GUI. When loading .sif files with...
[synfig.git] / synfig-core / trunk / src / synfig / loadcanvas.cpp
blob102b74484c7946a2e1ec351f78d9f43c1fce8ce5
1 /* === S Y N F I G ========================================================= */
2 /*! \file loadcanvas.cpp
3 ** \brief writeme
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 #ifdef USING_PCH
27 # include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
33 #include <cstdlib>
34 #include <cstdio>
35 #include <cstring>
36 #include <ETL/stringf>
37 #include <libxml++/libxml++.h>
38 #include <vector>
39 #include <stdexcept>
40 #include <iostream>
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"
60 #include "layer.h"
61 #include "string.h"
63 #include "exception.h"
65 #include "gradient.h"
67 #include <map>
68 #include <sigc++/bind.h>
70 #endif
72 /* === U S I N G =========================================================== */
74 using namespace std;
75 using namespace synfig;
76 using namespace etl;
79 class test_class {
80 static int bleh;
81 public:
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)
116 if(iter->second==x)
117 break;
118 assert(iter!=get_open_canvas_map().end());
119 if(iter==get_open_canvas_map().end())
120 return;
121 get_open_canvas_map().erase(iter->first);
122 get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
126 Canvas::Handle
127 synfig::open_canvas(const String &filename,String &errors,String &warnings)
129 return open_canvas_as(filename, filename, errors, warnings);
132 Canvas::Handle
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);
145 return canvas;
148 Canvas::Handle canvas;
149 CanvasParser parser;
150 parser.set_allow_errors(true);
154 CanvasParser::loading_.insert(filename);
155 canvas=parser.parse_from_file_as(filename,as,errors);
157 catch (...)
159 CanvasParser::loading_.erase(filename);
160 throw;
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();
172 return canvas;
175 /* === M E T H O D S ======================================================= */
177 void
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()));
183 void
184 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
186 error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
189 void
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);
195 // cerr<<str<<endl;
197 total_warnings_++;
198 warnings_text += " * " + str + "\n";
199 if(total_warnings_>=max_warnings_)
200 fatal_error(element, _("Too many warnings"));
203 void
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;
207 total_errors_++;
208 errors_text += " * " + str + "\n";
209 if(!allow_errors_)
210 throw runtime_error(str);
211 cerr<<str<<endl;
212 // synfig::error(str);
215 void
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);
224 Keyframe
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"));
232 return Keyframe();
235 Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
238 if(element->get_children().empty())
239 return ret;
241 if(element->get_child_text()->get_content().empty())
242 return ret;
244 ret.set_description(element->get_child_text()->get_content());
246 return ret;
250 Real
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"));
261 return false;
264 string val=element->get_attribute("value")->get_value();
266 return atof(val.c_str());
269 Time
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"));
280 return false;
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"));
299 return false;
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
310 // bug #1509627)
311 Vector &canvas_parser_vector_id(Vector &vector)
313 return vector;
316 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>");
324 return Vector();
327 Vector vect;
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);
333 if(!child)
334 continue;
335 else
336 if(child->get_name()=="x")
338 if(child->get_children().empty())
340 error(element, "Undefined value in <x>");
341 return Vector();
343 vect[0]=atof(child->get_child_text()->get_content().c_str());
345 else
346 if(child->get_name()=="y")
348 if(child->get_children().empty())
350 error(element, "Undefined value in <y>");
351 return Vector();
353 vect[1]=atof(child->get_child_text()->get_content().c_str());
355 else
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:
363 // return vect;
366 Color
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>");
374 return Color();
377 Color color(0);
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));
383 if(!child)
384 continue;
385 else
386 if(child->get_name()=="r")
388 if(child->get_children().empty())
390 error(element, "Undefined value in <r>");
391 return Color();
393 color.set_r(atof(child->get_child_text()->get_content().c_str()));
395 else
396 if(child->get_name()=="g")
398 if(child->get_children().empty())
400 error(element, "Undefined value in <g>");
401 return Color();
403 color.set_g(atof(child->get_child_text()->get_content().c_str()));
405 else
406 if(child->get_name()=="b")
408 if(child->get_children().empty())
410 error(element, "Undefined value in <b>");
411 return Color();
413 color.set_b(atof(child->get_child_text()->get_content().c_str()));
415 else
416 if(child->get_name()=="a")
418 if(child->get_children().empty())
420 error(element, "Undefined value in <a>");
421 return Color();
423 color.set_a(atof(child->get_child_text()->get_content().c_str()));
425 else
426 error_unexpected_element(child,child->get_name());
429 return color;
432 synfig::String
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();
452 bool
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"));
463 return false;
466 string val=element->get_attribute("value")->get_value();
468 if(val=="true" || val=="1")
469 return true;
470 if(val=="false" || val=="0")
471 return false;
473 error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
475 return false;
478 Gradient
479 CanvasParser::parse_gradient(xmlpp::Element *node)
481 assert(node->get_name()=="gradient");
482 Gradient ret;
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));
488 if(!child)
489 continue;
490 else
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"));
498 return Gradient();
501 cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
503 ret.push_back(cpoint);
506 ret.sort();
507 return ret;
510 ValueBase
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));
519 if(!child)
520 continue;
521 else
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");
528 continue;
532 return value_list;
535 Segment
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>");
543 return Segment();
546 Segment seg;
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));
552 if(!child)
553 continue;
554 else
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;
564 if(iter==list.end())
566 error(element, "Undefined value in <p1>");
567 continue;
570 if((*iter)->get_name()!="vector")
572 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
573 continue;
576 seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
578 else
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;
588 if(iter==list.end())
590 error(element, "Undefined value in <t1>");
591 continue;
594 if((*iter)->get_name()!="vector")
596 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
597 continue;
600 seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
602 else
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;
612 if(iter==list.end())
614 error(element, "Undefined value in <p2>");
615 continue;
618 if((*iter)->get_name()!="vector")
620 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
621 continue;
624 seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
626 else
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;
636 if(iter==list.end())
638 error(element, "Undefined value in <t2>");
639 continue;
642 if((*iter)->get_name()!="vector")
644 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
645 continue;
648 seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
650 else
651 error_unexpected_element(child,child->get_name());
653 return seg;
656 BLinePoint
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>");
663 return BLinePoint();
666 BLinePoint ret;
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));
673 if(!child)
674 continue;
675 else
676 // Vertex
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;
686 if(iter==list.end())
688 error(element, "Undefined value in <vertex>");
689 continue;
692 if((*iter)->get_name()!="vector")
694 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
695 continue;
698 ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
700 else
701 // Tangent 1
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;
711 if(iter==list.end())
713 error(element, "Undefined value in <t1>");
714 continue;
717 if((*iter)->get_name()!="vector")
719 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
720 continue;
723 ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
725 else
726 // Tangent 2
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;
736 if(iter==list.end())
738 error(element, "Undefined value in <t2>");
739 continue;
742 if((*iter)->get_name()!="vector")
744 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
745 continue;
748 ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
749 ret.set_split_tangent_flag(true);
751 else
752 // width
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;
762 if(iter==list.end())
764 error(element, "Undefined value in <width>");
765 continue;
768 if((*iter)->get_name()!="real")
770 error_unexpected_element((*iter),(*iter)->get_name(),"real");
771 continue;
774 ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
776 else
777 // origin
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;
787 if(iter==list.end())
789 error(element, "Undefined value in <origin>");
790 continue;
793 if((*iter)->get_name()!="real")
795 error_unexpected_element((*iter),(*iter)->get_name(),"real");
796 continue;
799 ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
801 else
802 error_unexpected_element(child,child->get_name());
804 return ret;
807 Angle
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"));
818 return Angle();
821 string val=element->get_attribute("value")->get_value();
823 return Angle::deg(atof(val.c_str()));
826 ValueBase
827 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
829 if(element->get_name()=="real")
830 return parse_real(element);
831 else
832 if(element->get_name()=="time")
833 return parse_time(element,canvas);
834 else
835 if(element->get_name()=="integer")
836 return parse_integer(element);
837 else
838 if(element->get_name()=="string")
839 return parse_string(element);
840 else
841 if(element->get_name()=="vector")
842 return parse_vector(element);
843 else
844 if(element->get_name()=="color")
845 return parse_color(element);
846 else
847 if(element->get_name()=="segment")
848 return parse_segment(element);
849 else
850 if(element->get_name()=="list")
851 return parse_list(element,canvas);
852 else
853 if(element->get_name()=="gradient")
854 return parse_gradient(element);
855 else
856 if(element->get_name()=="bool")
857 return parse_bool(element);
858 else
859 //if(element->get_name()=="canvas")
860 // return parse_canvas(element,canvas,true); // inline canvas
861 //else
862 if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
863 return parse_angle(element);
864 else
865 if(element->get_name()=="bline_point")
866 return parse_bline_point(element);
867 else
868 if(element->get_name()=="canvas")
869 return ValueBase(parse_canvas(element,canvas,true));
870 else
872 error_unexpected_element(element,element->get_name());
875 return ValueBase();
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());
894 if(!type)
896 error(element,"Bad type in <animated>");
897 return ValueNode_Animated::Handle();
900 ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
902 if(!value_node)
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));
914 if(!child)
915 continue;
916 else
917 if(child->get_name()=="waypoint")
919 if(!child->get_attribute("time"))
921 error(child,_("<waypoint> is missing attribute \"time\""));
922 continue;
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"))
933 if(!list.empty())
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"/>
940 // </animated>
941 if (type==ValueBase::TYPE_CANVAS)
943 String warnings;
944 waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value(), warnings));
945 warnings_text += warnings;
947 else
948 waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
950 else
952 if(child->get_children().empty())
954 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
955 continue;
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;
964 if(iter==list.end())
966 error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
967 continue;
970 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
973 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
975 if(!data.is_valid())
977 error(child,_("Bad data for <waypoint>"));
978 continue;
981 if(!waypoint_value_node)
983 error(child,_("Bad data for <waypoint>"));
984 continue;
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()));
1002 try {
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();
1029 if(val=="halt")
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);
1039 else
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();
1046 if(val=="halt")
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);
1056 else
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());
1064 continue;
1067 else
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
1077 // waypoint's value
1078 if (type == ValueBase::TYPE_ANGLE)
1080 if (canvas->get_version() == "0.1")
1082 bool first = true;
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();
1088 if (first)
1089 first = false;
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));
1103 prev = angle;
1108 value_node->changed();
1109 return value_node;
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()));
1119 return 0;
1122 ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1124 if(!type)
1126 error(element, strprintf(_("Bad type in <%s>"), element->get_name().c_str()));
1127 return 0;
1130 handle<LinkableValueNode> value_node=LinkableValueNode::create(element->get_name(),type);
1131 handle<ValueNode> c[value_node->link_count()];
1133 if(!value_node)
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));
1139 return 0;
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()));
1147 return 0;
1150 value_node->set_root_canvas(canvas->get_root());
1152 // handle exported valuenodes
1154 int index;
1155 String id, name;
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")
1163 continue;
1165 try {
1166 index = value_node->get_link_index_from_name(name);
1168 if(c[index])
1170 error(element,strprintf(_("'%s' was already defined in <%s>"),
1171 name.c_str(),
1172 element->get_name().c_str()));
1173 continue;
1176 c[index] = canvas->surefind_value_node(id);
1178 if (!c[index])
1180 error(element, strprintf(_("'%s' attribute in <%s> references unknown ID '%s'"),
1181 name.c_str(),
1182 element->get_name().c_str(),
1183 id.c_str()));
1184 continue;
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(),
1191 id.c_str(),
1192 index,
1193 element->get_name().c_str()));
1194 continue;
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);
1211 catch(...)
1213 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""), id.c_str()));
1214 throw;
1219 // handle inline valuenodes
1221 int index;
1222 String child_name;
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));
1229 if(!child)
1230 continue;
1232 child_name = child->get_name();
1234 index = value_node->get_link_index_from_name(child_name);
1236 if(c[index])
1238 error(child, strprintf(_("'%s' was already defined in <%s>"),
1239 child_name.c_str(),
1240 element->get_name().c_str()));
1241 break;
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()));
1255 continue;
1258 c[index]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1260 if(!c[index])
1262 error((*iter),strprintf(_("Parse of '%s' failed"),
1263 child_name.c_str()));
1264 continue;
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(),
1272 index,
1273 value_node->link_name(index).c_str()));
1274 continue;
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()));
1285 catch(...)
1287 error(child, strprintf(_("Unknown Exception thrown when working on element \"%s\""),child_name.c_str()));
1288 throw;
1293 String version(canvas->get_version());
1294 for (int i = 0; i < value_node->link_count(); i++)
1296 if (!c[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")
1300 continue;
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"))
1308 continue;
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")
1314 continue;
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")
1320 continue;
1322 error(element, strprintf(_("<%s> is missing link %d (%s)"),
1323 element->get_name().c_str(),
1325 value_node->link_name(i).c_str()));
1326 return 0;
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;
1342 return 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());
1361 if(!type)
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);
1378 else
1379 bline_value_node->set_loop(false);
1383 else
1384 value_node=ValueNode_DynamicList::create(type);
1386 if(!value_node)
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));
1398 if(!child)
1399 continue;
1400 else
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();
1431 //! \optimize
1432 while(!begin_sequence.empty())
1434 String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1435 String timecode(begin_sequence.begin(), iter);
1436 int priority=0;
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(
1455 Activepoint(
1456 Time(
1457 timecode,
1460 true, // Mark as a "on" activepoint
1461 priority
1465 if(iter==begin_sequence.end())
1466 begin_sequence.clear();
1467 else
1468 begin_sequence=String(iter+1,begin_sequence.end());
1471 //! \optimize
1472 while(!end_sequence.empty())
1474 String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1475 String timecode(end_sequence.begin(), iter);
1476 int priority=0;
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(
1495 Activepoint(
1496 Time(
1497 timecode,
1500 false, // Mark as a "off" activepoint
1501 priority
1504 if(iter==end_sequence.end())
1505 end_sequence.clear();
1506 else
1507 end_sequence=String(iter+1,end_sequence.end());
1510 timing_info.sort();
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);
1524 continue;
1527 else
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")));
1539 continue;
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);
1554 else
1555 error_unexpected_element(child,child->get_name());
1557 return value_node;
1560 handle<ValueNode>
1561 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1563 handle<ValueNode> value_node;
1564 assert(element);
1566 GUID guid;
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);
1572 if(value_node)
1573 return value_node;
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()));
1584 return value_node;
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);
1594 else
1595 if(element->get_name()=="hermite" || element->get_name()=="animated")
1596 value_node=parse_animated(element,canvas);
1597 else
1598 if(element->get_name()=="dynamic_list")
1599 value_node=parse_dynamic_list(element,canvas);
1600 else
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);
1603 else
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();
1609 else
1610 if(element->get_name()=="canvas")
1611 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1612 else
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
1624 // value_node list
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()));
1637 return value_node;
1639 catch(Exception::IDAlreadyExists)
1641 error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1642 return value_node;
1644 catch(...)
1646 error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1647 throw;
1650 value_node->set_guid(guid);
1651 return value_node;
1654 void
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));
1662 if(!child)
1663 continue;
1664 else
1665 if(child->get_name()=="canvas")
1666 parse_canvas(child, canvas);
1667 else
1668 parse_value_node(child,canvas);
1672 Layer::Handle
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));
1716 if(!child)
1717 continue;
1718 else
1719 if(child->get_name()=="name")
1720 warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1721 else
1722 if(child->get_name()=="desc")
1723 warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1724 else
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>."));
1732 continue;
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.
1747 if(!list.empty())
1748 warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1750 String str= child->get_attribute("use")->get_value();
1752 if (str.empty())
1753 error(child,_("Empty use=\"\" value in <param>"));
1754 else if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1756 String warnings;
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"));
1763 else
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()));
1782 continue;
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))
1790 break;
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."));
1796 continue;
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>"));
1808 continue;
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()));
1818 continue;
1821 else // ... otherwise, we assume that it is a ValueNode
1823 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1825 if(!value_node)
1827 error((*iter),_("Bad data for <param>"));
1828 continue;
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()));
1839 continue;
1841 else
1842 error_unexpected_element(child,child->get_name());
1845 layer->reset_version();
1846 return layer;
1849 Canvas::Handle
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_))
1864 if(inline_)
1866 canvas=Canvas::create_inline(parent);
1868 else
1872 String warnings;
1873 canvas=parent->find_canvas(element->get_attribute("id")->get_value(), warnings);
1874 warnings_text += warnings;
1876 catch(...)
1878 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1881 canvas->rend_desc().clear_flags();
1883 else
1885 canvas=Canvas::create();
1886 if(filename=="/dev/stdin")
1887 canvas->set_file_name("./stdin.sif");
1888 else
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);
1898 else
1899 canvas->set_guid(guid);
1902 if(element->get_attribute("version"))
1903 canvas->set_version(element->get_attribute("version")->get_value());
1904 else if(parent)
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());
1910 if (width < 1)
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());
1918 if (height < 1)
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();
1948 Vector
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();
1966 Color bg;
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();
1985 Vector focus;
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));
2000 if(child)
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);
2008 else
2009 if(child->get_name()=="keyframe")
2011 if(canvas->is_inline())
2013 warning(child,_("Inline canvas cannot have keyframes"));
2014 continue;
2017 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2018 canvas->keyframe_list().sync();
2020 else
2021 if(child->get_name()=="meta")
2023 if(canvas->is_inline())
2025 warning(child,_("Inline canvases cannot have metadata"));
2026 continue;
2029 String name,content;
2031 if(!child->get_attribute("name"))
2033 warning(child,_("<meta> must have a name"));
2034 continue;
2037 if(!child->get_attribute("content"))
2039 warning(child,_("<meta> must have content"));
2040 continue;
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
2050 if(list.empty())
2051 warning(child,_("blank \"name\" entity"));
2053 string tmp;
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);
2058 else
2059 if(child->get_name()=="desc")
2062 xmlpp::Element::NodeList list = child->get_children();
2064 // If we don't have any description, warn
2065 if(list.empty())
2066 warning(child,_("blank \"desc\" entity"));
2068 string tmp;
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);
2073 else
2074 if(child->get_name()=="author")
2077 xmlpp::Element::NodeList list = child->get_children();
2079 // If we don't have any description, warn
2080 if(list.empty())
2081 warning(child,_("blank \"author\" entity"));
2083 string tmp;
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);
2088 else
2089 if(child->get_name()=="layer")
2091 //if(canvas->is_inline())
2092 // canvas->push_front(parse_layer(child,canvas->parent()));
2093 //else
2094 canvas->push_front(parse_layer(child,canvas));
2096 else
2097 error_unexpected_element(child,child->get_name());
2099 // else
2100 // if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2101 // continue;
2104 if(canvas->value_node_list().placeholder_count())
2106 String nodes;
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"),
2116 nodes.c_str()));
2119 canvas->set_version(CURRENT_CANVAS_VERSION);
2120 return canvas;
2123 Canvas::Handle
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)];
2135 filename=as;
2136 total_warnings_=0;
2137 xmlpp::DomParser parser(file);
2138 if(parser)
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());
2148 again:
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);
2156 goto again;
2160 return canvas;
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 + "\"");
2173 throw;
2175 catch(const std::exception& ex)
2177 synfig::error("Standard Exception: "+String(ex.what()));
2178 errors = ex.what();
2179 return Canvas::Handle();
2181 catch(const String& str)
2183 cerr<<str<<endl;
2184 // synfig::error(str);
2185 errors = str;
2186 return Canvas::Handle();
2188 return Canvas::Handle();