Delete svn tags. We don't need them in git
[synfig.git] / synfig-core / trunk / src / synfig / loadcanvas.cpp
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
10 **
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.
15 **
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
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
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>
41
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"
59
60 #include "layer.h"
61 #include "string.h"
62
63 #include "exception.h"
64
65 #include "gradient.h"
66
67 #include <map>
68 #include <sigc++/bind.h>
69
70 #endif
71
72 /* === U S I N G =========================================================== */
73
74 using namespace std;
75 using namespace synfig;
76 using namespace etl;
77
78 /*
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--; }
84 };
85 int test_class::bleh(0);
86
87 test_class test_class_instance;
88 */
89
90 /* === M A C R O S ========================================================= */
91
92 #define VALUENODE_COMPATIBILITY_URL "http://synfig.org/Convert#Compatibility"
93
94 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
95
96 std::set<String> CanvasParser::loading_;
97
98 /* === P R O C E D U R E S ================================================= */
99
100 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
101
102 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
103 {
104         if(!open_canvas_map_)
105                 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
106         return *open_canvas_map_;
107 }
108
109 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
110
111 static void _canvas_file_name_changed(Canvas *x)
112 {
113         std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
114
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;
123
124 }
125
126 Canvas::Handle
127 synfig::open_canvas(const String &filename,String &errors,String &warnings)
128 {
129         return open_canvas_as(filename, filename, errors, warnings);
130 }
131
132 Canvas::Handle
133 synfig::open_canvas_as(const String &filename,const String &as,String &errors,String &warnings)
134 {
135         if (CanvasParser::loading_.count(filename))
136         {
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;
146         }
147
148         Canvas::Handle canvas;
149         CanvasParser parser;
150         parser.set_allow_errors(true);
151
152         try
153         {
154                 CanvasParser::loading_.insert(filename);
155                 canvas=parser.parse_from_file_as(filename,as,errors);
156         }
157         catch (...)
158         {
159                 CanvasParser::loading_.erase(filename);
160                 throw;
161         }
162         CanvasParser::loading_.erase(filename);
163
164         warnings = parser.get_warnings_text();
165
166         if(parser.error_count())
167         {
168                 errors = parser.get_errors_text();
169                 return Canvas::Handle();
170         }
171
172         return canvas;
173 }
174
175 /* === M E T H O D S ======================================================= */
176
177 void
178 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
179 {
180         error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
181 }
182
183 void
184 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
185 {
186         error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
187 }
188
189 void
190 CanvasParser::warning(xmlpp::Node *element, const String &text)
191 {
192         string str=strprintf("%s:<%s>:%d: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
193
194         synfig::warning(str);
195         // cerr<<str<<endl;
196
197         total_warnings_++;
198         warnings_text += "  * " + str + "\n";
199         if(total_warnings_>=max_warnings_)
200                 fatal_error(element, _("Too many warnings"));
201 }
202
203 void
204 CanvasParser::error(xmlpp::Node *element, const String &text)
205 {
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);
213 }
214
215 void
216 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
217 {
218         string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
219         throw runtime_error(str);
220 }
221
222
223
224 Keyframe
225 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
226 {
227         assert(element->get_name()=="keyframe");
228
229         if(!element->get_attribute("time"))
230         {
231                 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
232                 return Keyframe();
233         }
234
235         Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
236
237
238         if(element->get_children().empty())
239                 return ret;
240
241         if(element->get_child_text()->get_content().empty())
242                 return ret;
243
244         ret.set_description(element->get_child_text()->get_content());
245
246         return ret;
247 }
248
249
250 Real
251 CanvasParser::parse_real(xmlpp::Element *element)
252 {
253         assert(element->get_name()=="real");
254
255         if(!element->get_children().empty())
256                 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
257
258         if(!element->get_attribute("value"))
259         {
260                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
261                 return false;
262         }
263
264         string val=element->get_attribute("value")->get_value();
265
266         return atof(val.c_str());
267 }
268
269 Time
270 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
271 {
272         assert(element->get_name()=="time");
273
274         if(!element->get_children().empty())
275                 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
276
277         if(!element->get_attribute("value"))
278         {
279                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
280                 return false;
281         }
282
283         string val=element->get_attribute("value")->get_value();
284
285         return Time(val,canvas->rend_desc().get_frame_rate());
286 }
287
288 int
289 CanvasParser::parse_integer(xmlpp::Element *element)
290 {
291         assert(element->get_name()=="integer");
292
293         if(!element->get_children().empty())
294                 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
295
296         if(!element->get_attribute("value"))
297         {
298                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
299                 return false;
300         }
301
302         string val=element->get_attribute("value")->get_value();
303
304         return atoi(val.c_str());
305 }
306
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)
312 {
313         return vector;
314 }
315
316 Vector
317 CanvasParser::parse_vector(xmlpp::Element *element)
318 {
319         assert(element->get_name()=="vector");
320
321         if(element->get_children().empty())
322         {
323                 error(element, "Undefined value in <vector>");
324                 return Vector();
325         }
326
327         Vector vect;
328
329         xmlpp::Element::NodeList list = element->get_children();
330         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
331         {
332                 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
333                 if(!child)
334                         continue;
335                 else
336                 if(child->get_name()=="x")
337                 {
338                         if(child->get_children().empty())
339                         {
340                                 error(element, "Undefined value in <x>");
341                                 return Vector();
342                         }
343                         vect[0]=atof(child->get_child_text()->get_content().c_str());
344                 }
345                 else
346                 if(child->get_name()=="y")
347                 {
348                         if(child->get_children().empty())
349                         {
350                                 error(element, "Undefined value in <y>");
351                                 return Vector();
352                         }
353                         vect[1]=atof(child->get_child_text()->get_content().c_str());
354                 }
355                 else
356                         error_unexpected_element(child,child->get_name());
357         }
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;
364 }
365
366 Color
367 CanvasParser::parse_color(xmlpp::Element *element)
368 {
369         assert(element->get_name()=="color");
370
371         if(element->get_children().empty())
372         {
373                 error(element, "Undefined value in <color>");
374                 return Color();
375         }
376
377         Color color(0);
378
379         xmlpp::Element::NodeList list = element->get_children();
380         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
381         {
382                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
383                 if(!child)
384                         continue;
385                 else
386                 if(child->get_name()=="r")
387                 {
388                         if(child->get_children().empty())
389                         {
390                                 error(element, "Undefined value in <r>");
391                                 return Color();
392                         }
393                         color.set_r(atof(child->get_child_text()->get_content().c_str()));
394                 }
395                 else
396                 if(child->get_name()=="g")
397                 {
398                         if(child->get_children().empty())
399                         {
400                                 error(element, "Undefined value in <g>");
401                                 return Color();
402                         }
403                         color.set_g(atof(child->get_child_text()->get_content().c_str()));
404                 }
405                 else
406                 if(child->get_name()=="b")
407                 {
408                         if(child->get_children().empty())
409                         {
410                                 error(element, "Undefined value in <b>");
411                                 return Color();
412                         }
413                         color.set_b(atof(child->get_child_text()->get_content().c_str()));
414                 }
415                 else
416                 if(child->get_name()=="a")
417                 {
418                         if(child->get_children().empty())
419                         {
420                                 error(element, "Undefined value in <a>");
421                                 return Color();
422                         }
423                         color.set_a(atof(child->get_child_text()->get_content().c_str()));
424                 }
425                 else
426                         error_unexpected_element(child,child->get_name());
427         }
428
429         return color;
430 }
431
432 synfig::String
433 CanvasParser::parse_string(xmlpp::Element *element)
434 {
435         assert(element->get_name()=="string");
436
437         if(element->get_children().empty())
438         {
439                 warning(element, "Undefined value in <string>");
440                 return synfig::String();
441         }
442
443         if(element->get_child_text()->get_content().empty())
444         {
445                 warning(element, "Content element of <string> appears to be empty");
446                 return synfig::String();
447         }
448
449         return element->get_child_text()->get_content();
450 }
451
452 bool
453 CanvasParser::parse_bool(xmlpp::Element *element)
454 {
455         assert(element->get_name()=="bool");
456
457         if(!element->get_children().empty())
458                 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
459
460         if(!element->get_attribute("value"))
461         {
462                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
463                 return false;
464         }
465
466         string val=element->get_attribute("value")->get_value();
467
468         if(val=="true" || val=="1")
469                 return true;
470         if(val=="false" || val=="0")
471                 return false;
472
473         error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
474
475         return false;
476 }
477
478 Gradient
479 CanvasParser::parse_gradient(xmlpp::Element *node)
480 {
481         assert(node->get_name()=="gradient");
482         Gradient ret;
483
484         xmlpp::Element::NodeList list = node->get_children();
485         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
486         {
487                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
488                 if(!child)
489                         continue;
490                 else
491                 {
492                         Gradient::CPoint cpoint;
493                         cpoint.color=parse_color(child);
494
495                         if(!child->get_attribute("pos"))
496                         {
497                                 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
498                                 return Gradient();
499                         }
500
501                         cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
502
503                         ret.push_back(cpoint);
504                 }
505         }
506         ret.sort();
507         return ret;
508 }
509
510 ValueBase
511 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
512 {
513         vector<ValueBase> value_list;
514
515         xmlpp::Element::NodeList list = element->get_children();
516         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
517         {
518                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
519                 if(!child)
520                         continue;
521                 else
522                 {
523                         value_list.push_back(parse_value(child,canvas));
524                         if(!value_list.back().is_valid())
525                         {
526                                 value_list.pop_back();
527                                 error(child,"Bad ValueBase");
528                                 continue;
529                         }
530                 }
531         }
532         return value_list;
533 }
534
535 Segment
536 CanvasParser::parse_segment(xmlpp::Element *element)
537 {
538         assert(element->get_name()=="segment");
539
540         if(element->get_children().empty())
541         {
542                 error(element, "Undefined value in <segment>");
543                 return Segment();
544         }
545
546         Segment seg;
547
548         xmlpp::Element::NodeList list = element->get_children();
549         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
550         {
551                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
552                 if(!child)
553                         continue;
554                 else
555                 if(child->get_name()=="p1")
556                 {
557                         xmlpp::Element::NodeList list = child->get_children();
558                         xmlpp::Element::NodeList::iterator iter;
559
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;
563
564                         if(iter==list.end())
565                         {
566                                 error(element, "Undefined value in <p1>");
567                                 continue;
568                         }
569
570                         if((*iter)->get_name()!="vector")
571                         {
572                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
573                                 continue;
574                         }
575
576                         seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
577                 }
578                 else
579                 if(child->get_name()=="t1")
580                 {
581                         xmlpp::Element::NodeList list = child->get_children();
582                         xmlpp::Element::NodeList::iterator iter;
583
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;
587
588                         if(iter==list.end())
589                         {
590                                 error(element, "Undefined value in <t1>");
591                                 continue;
592                         }
593
594                         if((*iter)->get_name()!="vector")
595                         {
596                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
597                                 continue;
598                         }
599
600                         seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
601                 }
602                 else
603                 if(child->get_name()=="p2")
604                 {
605                         xmlpp::Element::NodeList list = child->get_children();
606                         xmlpp::Element::NodeList::iterator iter;
607
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;
611
612                         if(iter==list.end())
613                         {
614                                 error(element, "Undefined value in <p2>");
615                                 continue;
616                         }
617
618                         if((*iter)->get_name()!="vector")
619                         {
620                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
621                                 continue;
622                         }
623
624                         seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
625                 }
626                 else
627                 if(child->get_name()=="t2")
628                 {
629                         xmlpp::Element::NodeList list = child->get_children();
630                         xmlpp::Element::NodeList::iterator iter;
631
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;
635
636                         if(iter==list.end())
637                         {
638                                 error(element, "Undefined value in <t2>");
639                                 continue;
640                         }
641
642                         if((*iter)->get_name()!="vector")
643                         {
644                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
645                                 continue;
646                         }
647
648                         seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
649                 }
650                 else
651                         error_unexpected_element(child,child->get_name());
652         }
653         return seg;
654 }
655
656 BLinePoint
657 CanvasParser::parse_bline_point(xmlpp::Element *element)
658 {
659         assert(element->get_name()=="bline_point");
660         if(element->get_children().empty())
661         {
662                 error(element, "Undefined value in <bline_point>");
663                 return BLinePoint();
664         }
665
666         BLinePoint ret;
667         ret.set_split_tangent_flag(false);
668
669         xmlpp::Element::NodeList list = element->get_children();
670         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
671         {
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")
678                 {
679                         xmlpp::Element::NodeList list = child->get_children();
680                         xmlpp::Element::NodeList::iterator iter;
681
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;
685
686                         if(iter==list.end())
687                         {
688                                 error(element, "Undefined value in <vertex>");
689                                 continue;
690                         }
691
692                         if((*iter)->get_name()!="vector")
693                         {
694                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
695                                 continue;
696                         }
697
698                         ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
699                 }
700                 else
701                 // Tangent 1
702                 if(child->get_name()=="t1" || child->get_name()=="tangent")
703                 {
704                         xmlpp::Element::NodeList list = child->get_children();
705                         xmlpp::Element::NodeList::iterator iter;
706
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;
710
711                         if(iter==list.end())
712                         {
713                                 error(element, "Undefined value in <t1>");
714                                 continue;
715                         }
716
717                         if((*iter)->get_name()!="vector")
718                         {
719                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
720                                 continue;
721                         }
722
723                         ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
724                 }
725                 else
726                 // Tangent 2
727                 if(child->get_name()=="t2")
728                 {
729                         xmlpp::Element::NodeList list = child->get_children();
730                         xmlpp::Element::NodeList::iterator iter;
731
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;
735
736                         if(iter==list.end())
737                         {
738                                 error(element, "Undefined value in <t2>");
739                                 continue;
740                         }
741
742                         if((*iter)->get_name()!="vector")
743                         {
744                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
745                                 continue;
746                         }
747
748                         ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
749                         ret.set_split_tangent_flag(true);
750                 }
751                 else
752                 // width
753                 if(child->get_name()=="width")
754                 {
755                         xmlpp::Element::NodeList list = child->get_children();
756                         xmlpp::Element::NodeList::iterator iter;
757
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;
761
762                         if(iter==list.end())
763                         {
764                                 error(element, "Undefined value in <width>");
765                                 continue;
766                         }
767
768                         if((*iter)->get_name()!="real")
769                         {
770                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
771                                 continue;
772                         }
773
774                         ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
775                 }
776                 else
777                 // origin
778                 if(child->get_name()=="origin")
779                 {
780                         xmlpp::Element::NodeList list = child->get_children();
781                         xmlpp::Element::NodeList::iterator iter;
782
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;
786
787                         if(iter==list.end())
788                         {
789                                 error(element, "Undefined value in <origin>");
790                                 continue;
791                         }
792
793                         if((*iter)->get_name()!="real")
794                         {
795                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
796                                 continue;
797                         }
798
799                         ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
800                 }
801                 else
802                         error_unexpected_element(child,child->get_name());
803         }
804         return ret;
805 }
806
807 Angle
808 CanvasParser::parse_angle(xmlpp::Element *element)
809 {
810         assert(element->get_name()=="angle");
811
812         if(!element->get_children().empty())
813                 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
814
815         if(!element->get_attribute("value"))
816         {
817                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
818                 return Angle();
819         }
820
821         string val=element->get_attribute("value")->get_value();
822
823         return Angle::deg(atof(val.c_str()));
824 }
825
826 ValueBase
827 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
828 {
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
871         {
872                 error_unexpected_element(element,element->get_name());
873         }
874
875         return ValueBase();
876 }
877
878
879
880
881 ValueNode_Animated::Handle
882 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
883 {
884         assert(element->get_name()=="hermite" || element->get_name()=="animated");
885
886         if(!element->get_attribute("type"))
887         {
888                 error(element,"Missing attribute \"type\" in <animated>");
889                 return ValueNode_Animated::Handle();
890         }
891
892         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
893
894         if(!type)
895         {
896                 error(element,"Bad type in <animated>");
897                 return ValueNode_Animated::Handle();
898         }
899
900         ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
901
902         if(!value_node)
903         {
904                 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_local_name(type).c_str()));
905                 return ValueNode_Animated::Handle();
906         }
907
908         value_node->set_root_canvas(canvas->get_root());
909
910         xmlpp::Element::NodeList list = element->get_children();
911         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
912         {
913                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
914                 if(!child)
915                         continue;
916                 else
917                 if(child->get_name()=="waypoint")
918                 {
919                         if(!child->get_attribute("time"))
920                         {
921                                 error(child,_("<waypoint> is missing attribute \"time\""));
922                                 continue;
923                         }
924
925                         Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
926
927
928                         ValueNode::Handle waypoint_value_node;
929                         xmlpp::Element::NodeList list = child->get_children();
930
931                         if(child->get_attribute("use"))
932                         {
933                                 if(!list.empty())
934                                         warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
935
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
937                                 //
938                                 //      <animated type="canvas">
939                                 //        <waypoint time="0s" use="mycanvas"/>
940                                 //      </animated>
941                                 if (type==ValueBase::TYPE_CANVAS)
942                                 {
943                                         String warnings;
944                                         waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value(), warnings));
945                                         warnings_text += warnings;
946                                 }
947                                 else
948                                         waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
949                         }
950                         else
951                         {
952                                 if(child->get_children().empty())
953                                 {
954                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
955                                         continue;
956                                 }
957
958                                 xmlpp::Element::NodeList::iterator iter;
959
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;
963
964                                 if(iter==list.end())
965                                 {
966                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
967                                         continue;
968                                 }
969
970                                 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
971
972                                 /*
973                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
974
975                                 if(!data.is_valid())
976                                 {
977                                         error(child,_("Bad data for <waypoint>"));
978                                         continue;
979                                 }
980                                 */
981                                 if(!waypoint_value_node)
982                                 {
983                                         error(child,_("Bad data for <waypoint>"));
984                                         continue;
985                                 }
986
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.
992                                 */
993                                 waypoint_value_node=waypoint_value_node->clone();
994
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()));
999                         }
1000
1001
1002                         try {
1003                                 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
1004
1005                         if(child->get_attribute("tension"))
1006                         {
1007                                 synfig::String str(child->get_attribute("tension")->get_value());
1008                                 waypoint->set_tension(atof(str.c_str()));
1009                         }
1010                         if(child->get_attribute("temporal-tension"))
1011                         {
1012                                 synfig::String str(child->get_attribute("temporal-tension")->get_value());
1013                                 waypoint->set_temporal_tension(atof(str.c_str()));
1014                         }
1015                         if(child->get_attribute("continuity"))
1016                         {
1017                                 synfig::String str(child->get_attribute("continuity")->get_value());
1018                                 waypoint->set_continuity(atof(str.c_str()));
1019                         }
1020                         if(child->get_attribute("bias"))
1021                         {
1022                                 synfig::String str(child->get_attribute("bias")->get_value());
1023                                 waypoint->set_bias(atof(str.c_str()));
1024                         }
1025
1026                         if(child->get_attribute("before"))
1027                         {
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"));
1041                         }
1042
1043                         if(child->get_attribute("after"))
1044                         {
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"));
1058                         }
1059                         }
1060                         catch(Exception::BadTime x)
1061                         {
1062                                 warning(child,x.what());
1063                         }
1064                         continue;
1065
1066                 }
1067                 else
1068                         error_unexpected_element(child,child->get_name());
1069         }
1070
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
1074
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)
1079         {
1080                 if (canvas->get_version() == "0.1")
1081                 {
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++)
1086                         {
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")
1091                                 {
1092                                         if (angle - prev > 180)
1093                                         {
1094                                                 while (angle - prev > 180) angle -= 360;
1095                                                 iter->set_value(Angle::deg(angle));
1096                                         }
1097                                         else if (prev - angle > 180)
1098                                         {
1099                                                 while (prev - angle > 180) angle += 360;
1100                                                 iter->set_value(Angle::deg(angle));
1101                                         }
1102                                 }
1103                                 prev = angle;
1104                         }
1105                 }
1106         }
1107
1108         value_node->changed();
1109         return value_node;
1110 }
1111
1112 etl::handle<LinkableValueNode>
1113 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1114 {
1115         // Determine the type
1116         if(!element->get_attribute("type"))
1117         {
1118                 error(element, strprintf(_("Missing attribute \"type\" in <%s>"), element->get_name().c_str()));
1119                 return 0;
1120         }
1121
1122         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1123
1124         if(!type)
1125         {
1126                 error(element, strprintf(_("Bad type in <%s>"), element->get_name().c_str()));
1127                 return 0;
1128         }
1129
1130         handle<LinkableValueNode> value_node=LinkableValueNode::create(element->get_name(),type);
1131         handle<ValueNode> c[value_node->link_count()];
1132
1133         if(!value_node)
1134         {
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;
1140         }
1141
1142         if(value_node->get_type()!=type)
1143         {
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;
1148         }
1149
1150         value_node->set_root_canvas(canvas->get_root());
1151
1152         // handle exported valuenodes
1153         {
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++)
1158                 {
1159                         name = (*iter)->get_name();
1160                         id = (*iter)->get_value();
1161
1162                         if (name == "guid" || name == "id" || name == "type")
1163                                 continue;
1164
1165                         try {
1166                                 index = value_node->get_link_index_from_name(name);
1167
1168                                 if(c[index])
1169                                 {
1170                                         error(element,strprintf(_("'%s' was already defined in <%s>"),
1171                                                                                         name.c_str(),
1172                                                                                         element->get_name().c_str()));
1173                                         continue;
1174                                 }
1175
1176                                 c[index] = canvas->surefind_value_node(id);
1177
1178                                 if (!c[index])
1179                                 {
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;
1185                                 }
1186
1187                                 if(!value_node->set_link(index, c[index]))
1188                                 {
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;
1195                                 }
1196
1197                                 // printf("  <%s> set link %d (%s) using exported value\n", element->get_name().c_str(), index, name.c_str());
1198                         }
1199                         catch (Exception::BadLinkName)
1200                         {
1201                                 warning(element, strprintf("Bad link name '%s'", name.c_str()));
1202                         }
1203                         catch(Exception::IDNotFound)
1204                         {
1205                                 error(element,"Unable to resolve " + id);
1206                         }
1207                         catch(Exception::FileNotFound)
1208                         {
1209                                 error(element,"Unable to open file referenced in " + id);
1210                         }
1211                         catch(...)
1212                         {
1213                                 error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""), id.c_str()));
1214                                 throw;
1215                         }
1216                 }
1217         }
1218
1219         // handle inline valuenodes
1220         {
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)
1225                 {
1226                         xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1227                         try
1228                         {
1229                                 if(!child)
1230                                         continue;
1231
1232                                 child_name = child->get_name();
1233
1234                                 index = value_node->get_link_index_from_name(child_name);
1235
1236                                 if(c[index])
1237                                 {
1238                                         error(child, strprintf(_("'%s' was already defined in <%s>"),
1239                                                                                    child_name.c_str(),
1240                                                                                    element->get_name().c_str()));
1241                                         break;
1242                                 }
1243
1244                                 xmlpp::Element::NodeList list = child->get_children();
1245                                 xmlpp::Element::NodeList::iterator iter;
1246
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;
1250
1251                                 if(iter==list.end())
1252                                 {
1253                                         error(child,strprintf(_("element <%s> is missing its contents"),
1254                                                                                   child_name.c_str()));
1255                                         continue;
1256                                 }
1257
1258                                 c[index]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1259
1260                                 if(!c[index])
1261                                 {
1262                                         error((*iter),strprintf(_("Parse of '%s' failed"),
1263                                                                                         child_name.c_str()));
1264                                         continue;
1265                                 }
1266
1267                                 if(!value_node->set_link(index,c[index]))
1268                                 {
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;
1275                                 }
1276
1277                                 // \todo do a search for more elements and warn if they are found
1278
1279                                 // printf("  <%s> set link %d (%s) using inline value\n", element->get_name().c_str(), index, child_name.c_str());
1280                         }
1281                         catch(Exception::BadLinkName)
1282                         {
1283                                 warning(child, strprintf("Bad link name for <%s>", element->get_name().c_str()));
1284                         }
1285                         catch(...)
1286                         {
1287                                 error(child, strprintf(_("Unknown Exception thrown when working on element \"%s\""),child_name.c_str()));
1288                                 throw;
1289                         }
1290                 }
1291         }
1292
1293         String version(canvas->get_version());
1294         for (int i = 0; i < value_node->link_count(); i++)
1295         {
1296                 if (!c[i])
1297                 {
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;
1301
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;
1309
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;
1315
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;
1321
1322                         // 'loop' was added while canvas version 0.6 was in use; the 'random' node was added back when 0.1 was in use
1323                         if ((version == "0.1" || version == "0.2" || version == "0.3" || version == "0.4" || version == "0.5" || version == "0.6") &&
1324                                 element->get_name() == "random" &&
1325                                 value_node->link_name(i) == "loop")
1326                                 continue;
1327
1328                         error(element, strprintf(_("<%s> is missing link %d (%s)"),
1329                                                                          element->get_name().c_str(),
1330                                                                          i,
1331                                                                          value_node->link_name(i).c_str()));
1332                         return 0;
1333                 }
1334         }
1335
1336         // pre 0.4 canvases had *calctangent outputs scaled down by 0.5 for some reason
1337         if (element->get_name() == "blinecalctangent" || element->get_name() == "segcalctangent")
1338         {
1339                 if (version == "0.1" || version == "0.2" || version == "0.3")
1340                 {
1341                         handle<LinkableValueNode> scale_value_node=LinkableValueNode::create("scale",type);
1342                         scale_value_node->set_link(scale_value_node->get_link_index_from_name("link"), value_node);
1343                         scale_value_node->set_link(scale_value_node->get_link_index_from_name("scalar"), ValueNode_Const::create(Real(0.5)));
1344                         value_node = scale_value_node;
1345                 }
1346         }
1347
1348         return value_node;
1349 }
1350
1351 // This will also parse a bline
1352 handle<ValueNode_DynamicList>
1353 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1354 {
1355         assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1356
1357         const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1358
1359         if(!element->get_attribute("type"))
1360         {
1361                 error(element,"Missing attribute \"type\" in <dynamic_list>");
1362                 return handle<ValueNode_DynamicList>();
1363         }
1364
1365         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1366
1367         if(!type)
1368         {
1369                 error(element,"Bad type in <dynamic_list>");
1370                 return handle<ValueNode_DynamicList>();
1371         }
1372
1373         handle<ValueNode_DynamicList> value_node;
1374         handle<ValueNode_BLine> bline_value_node;
1375
1376         if(element->get_name()=="bline")
1377         {
1378                 value_node=bline_value_node=ValueNode_BLine::create();
1379                 if(element->get_attribute("loop"))
1380                 {
1381                         String loop=element->get_attribute("loop")->get_value();
1382                         if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1383                                 bline_value_node->set_loop(true);
1384                         else
1385                                 bline_value_node->set_loop(false);
1386                 }
1387
1388         }
1389         else
1390                 value_node=ValueNode_DynamicList::create(type);
1391
1392         if(!value_node)
1393         {
1394                 error(element,strprintf(_("Unable to create <dynamic_list>")));
1395                 return handle<ValueNode_DynamicList>();
1396         }
1397
1398         value_node->set_root_canvas(canvas->get_root());
1399
1400         xmlpp::Element::NodeList list = element->get_children();
1401         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1402         {
1403                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1404                 if(!child)
1405                         continue;
1406                 else
1407                 if(child->get_name()=="entry")
1408                 {
1409                         ValueNode_DynamicList::ListEntry list_entry;
1410
1411                         // Parse begin/end waypoints
1412                         {
1413                                 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1414                                 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1415
1416                                 String begin_sequence;
1417                                 String end_sequence;
1418
1419                                 ActivepointList &timing_info(list_entry.timing_info);
1420
1421                                 if(child->get_attribute("begin"))
1422                                         begin_sequence=child->get_attribute("begin")->get_value();
1423
1424                                 if(child->get_attribute("on"))
1425                                         begin_sequence=child->get_attribute("on")->get_value();
1426
1427                                 if(child->get_attribute("end"))
1428                                         end_sequence=child->get_attribute("end")->get_value();
1429
1430                                 if(child->get_attribute("off"))
1431                                         end_sequence=child->get_attribute("off")->get_value();
1432
1433                                 // clear out any auto-start
1434                                 if(!begin_sequence.empty())
1435                                         timing_info.clear();
1436
1437                                 //! \optimize
1438                                 while(!begin_sequence.empty())
1439                                 {
1440                                         String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1441                                         String timecode(begin_sequence.begin(), iter);
1442                                         int priority=0;
1443
1444                                         // skip whitespace before checking for a priority
1445                                         while (isspace(timecode[0]))
1446                                                 timecode=timecode.substr(1);
1447
1448                                         // If there is a priority, then grab it and remove
1449                                         // it from the timecode
1450                                         if(timecode[0]=='p')
1451                                         {
1452                                                 //priority=timecode[1]-'0';
1453                                                 //timecode=String(timecode.begin()+3,timecode.end());
1454                                                 int space=timecode.find_first_of(' ');
1455                                                 priority=atoi(String(timecode,1,space-1).c_str());
1456                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1457                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1458                                         }
1459
1460                                         timing_info.push_back(
1461                                                 Activepoint(
1462                                                         Time(
1463                                                                 timecode,
1464                                                                 fps
1465                                                         ),
1466                                                         true,   // Mark as a "on" activepoint
1467                                                         priority
1468                                                 )
1469                                         );
1470
1471                                         if(iter==begin_sequence.end())
1472                                                 begin_sequence.clear();
1473                                         else
1474                                                 begin_sequence=String(iter+1,begin_sequence.end());
1475                                 }
1476
1477                                 //! \optimize
1478                                 while(!end_sequence.empty())
1479                                 {
1480                                         String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1481                                         String timecode(end_sequence.begin(),   iter);
1482                                         int priority=0;
1483
1484                                         // skip whitespace before checking for a priority
1485                                         while (isspace(timecode[0]))
1486                                                 timecode=timecode.substr(1);
1487
1488                                         // If there is a priority, then grab it and remove
1489                                         // it from the timecode
1490                                         if(timecode[0]=='p')
1491                                         {
1492                                                 //priority=timecode[1]-'0';
1493                                                 //timecode=String(timecode.begin()+3,timecode.end());
1494                                                 int space=timecode.find_first_of(' ');
1495                                                 priority=atoi(String(timecode,1,space-1).c_str());
1496                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1497                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1498                                         }
1499
1500                                         timing_info.push_back(
1501                                                 Activepoint(
1502                                                         Time(
1503                                                                 timecode,
1504                                                                 fps
1505                                                         ),
1506                                                         false,  // Mark as a "off" activepoint
1507                                                         priority
1508                                                 )
1509                                         );
1510                                         if(iter==end_sequence.end())
1511                                                 end_sequence.clear();
1512                                         else
1513                                                 end_sequence=String(iter+1,end_sequence.end());
1514                                 }
1515
1516                                 timing_info.sort();
1517                         }
1518
1519                         if(child->get_attribute("use"))
1520                         {
1521                                 // \todo does this need to be able to read 'use="canvas"', like waypoints can now?  (see 'surefind_canvas' in this file)
1522                                 string id=child->get_attribute("use")->get_value();
1523                                 try
1524                                 {
1525                                         list_entry.value_node=canvas->surefind_value_node(id);
1526                                 }
1527                                 catch(Exception::IDNotFound)
1528                                 {
1529                                         error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1530                                         continue;
1531                                 }
1532                         }
1533                         else
1534                         {
1535                                 xmlpp::Element::NodeList list = child->get_children();
1536                                 xmlpp::Element::NodeList::iterator iter;
1537
1538                                 // Search for the first non-text XML element
1539                                 for(iter = list.begin(); iter != list.end(); ++iter)
1540                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1541
1542                                 if(iter==list.end())
1543                                 {
1544                                         error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1545                                         continue;
1546                                 }
1547
1548                                 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1549
1550                                 if(!list_entry.value_node)
1551                                         error((*iter),"Parse of ValueNode failed");
1552
1553                                 // \todo do a search for more elements and warn if they are found
1554
1555                         }
1556
1557                         value_node->add(list_entry);
1558                         value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1559                 }
1560                 else
1561                         error_unexpected_element(child,child->get_name());
1562         }
1563         return value_node;
1564 }
1565
1566 handle<ValueNode>
1567 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1568 {
1569         handle<ValueNode> value_node;
1570         assert(element);
1571
1572         GUID guid;
1573
1574         if(element->get_attribute("guid"))
1575         {
1576                 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1577                 value_node=guid_cast<ValueNode>(guid);
1578                 if(value_node)
1579                         return value_node;
1580         }
1581
1582         // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1583         if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1584         {
1585                 ValueBase data=parse_value(element,canvas);
1586
1587                 if(!data.is_valid())
1588                 {
1589                         error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1590                         return value_node;
1591                 }
1592
1593                 // We want to convert this ValueBase into a
1594                 // ValueNode_Const. That way, we can treat the
1595                 // ID like any other Datanode. Think of this
1596                 // as a shorthand for creating constant ValueNodes.
1597
1598                 value_node=ValueNode_Const::create(data);
1599         }
1600         else
1601         if(element->get_name()=="hermite" || element->get_name()=="animated")
1602                 value_node=parse_animated(element,canvas);
1603         else
1604         if(element->get_name()=="dynamic_list")
1605                 value_node=parse_dynamic_list(element,canvas);
1606         else
1607         if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1608                 value_node=parse_dynamic_list(element,canvas);
1609         else
1610         if(LinkableValueNode::book().count(element->get_name()))
1611         {
1612                 value_node=parse_linkable_value_node(element,canvas);
1613                 if (!value_node) value_node = PlaceholderValueNode::create();
1614         }
1615         else
1616         if(element->get_name()=="canvas")
1617                 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1618         else
1619         {
1620                 error_unexpected_element(element,element->get_name());
1621                 error(element, strprintf(_("Expected a ValueNode.  Refer to '%s'"),
1622                                                                  VALUENODE_COMPATIBILITY_URL));
1623                 value_node=PlaceholderValueNode::create();
1624         }
1625
1626         value_node->set_root_canvas(canvas->get_root());
1627
1628         // If we were successful, and our element has
1629         // an ID attribute, go ahead and add it to the
1630         // value_node list
1631         if(value_node && element->get_attribute("id"))
1632         {
1633                 string id=element->get_attribute("id")->get_value();
1634
1635                 //value_node->set_id(id);
1636
1637                 // If there is already a value_node in the list
1638                 // with the same ID, then that is an error
1639                 try { canvas->add_value_node(value_node,id); }
1640                 catch(Exception::BadLinkName)
1641                 {
1642                         warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1643                         return value_node;
1644                 }
1645                 catch(Exception::IDAlreadyExists)
1646                 {
1647                         error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1648                         return value_node;
1649                 }
1650                 catch(...)
1651                 {
1652                         error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1653                         throw;
1654                 }
1655         }
1656         value_node->set_guid(guid);
1657         return value_node;
1658 }
1659
1660 void
1661 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1662 {
1663         assert(element->get_name()=="defs");
1664         xmlpp::Element::NodeList list = element->get_children();
1665         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1666         {
1667                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1668                 if(!child)
1669                         continue;
1670                 else
1671                 if(child->get_name()=="canvas")
1672                         parse_canvas(child, canvas);
1673                 else
1674                         parse_value_node(child,canvas);
1675         }
1676 }
1677
1678 Layer::Handle
1679 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1680 {
1681
1682         assert(element->get_name()=="layer");
1683         Layer::Handle layer;
1684
1685         if(!element->get_attribute("type"))
1686         {
1687                 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1688                 return Layer::Handle();
1689         }
1690
1691         layer=Layer::create(element->get_attribute("type")->get_value());
1692         layer->set_canvas(canvas);
1693
1694         if(element->get_attribute("group"))
1695         {
1696                 layer->add_to_group(
1697                         element->get_attribute("group")->get_value()
1698                 );
1699         }
1700
1701         // Handle the version attribute
1702         if(element->get_attribute("version"))
1703         {
1704                 String version(element->get_attribute("version")->get_value());
1705                 if(version>layer->get_version())
1706                         warning(element,_("Installed layer version is smaller than layer version in file"));
1707                 if(version!=layer->get_version())
1708                         layer->set_version(version);
1709         }
1710
1711         // Handle the description
1712         if(element->get_attribute("desc"))
1713                 layer->set_description(element->get_attribute("desc")->get_value());
1714
1715         if(element->get_attribute("active"))
1716                 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1717
1718         xmlpp::Element::NodeList list = element->get_children();
1719         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1720         {
1721                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1722                 if(!child)
1723                         continue;
1724                 else
1725                 if(child->get_name()=="name")
1726                         warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1727                 else
1728                 if(child->get_name()=="desc")
1729                         warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1730                 else
1731                 if(child->get_name()=="param")
1732                 {
1733                         xmlpp::Element::NodeList list = child->get_children();
1734
1735                         if(!child->get_attribute("name"))
1736                         {
1737                                 error(child,_("Missing \"name\" attribute for <param>."));
1738                                 continue;
1739                         }
1740
1741                         String param_name=child->get_attribute("name")->get_value();
1742
1743                         // SVN r2013 and r2014 renamed all 'pos' and 'offset' parameters to 'origin'
1744                         // 'pos' and 'offset' will appear in old .sif files; handle them correctly
1745                         if (param_name == "pos" || param_name == "offset")
1746                                 param_name = "origin";
1747
1748                         if(child->get_attribute("use"))
1749                         {
1750                                 // If the "use" attribute is used, then the
1751                                 // element should be empty. Warn the user if
1752                                 // we find otherwise.
1753                                 if(!list.empty())
1754                                         warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1755
1756                                 String str=     child->get_attribute("use")->get_value();
1757
1758                                 if (str.empty())
1759                                         error(child,_("Empty use=\"\" value in <param>"));
1760                                 else if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1761                                 {
1762                                         String warnings;
1763                                         Canvas::Handle c(canvas->surefind_canvas(str, warnings));
1764                                         warnings_text += warnings;
1765                                         if(!c) error((*iter),strprintf(_("Failed to load subcanvas '%s'"), str.c_str()));
1766                                         if(!layer->set_param(param_name,c))
1767                                                 error((*iter),_("Layer rejected canvas link"));
1768                                 }
1769                                 else
1770                                 try
1771                                 {
1772                                         handle<ValueNode> value_node=canvas->surefind_value_node(str);
1773
1774                                         // Assign the value_node to the dynamic parameter list
1775                                         if (param_name == "segment_list" && (layer->get_name() == "region" || layer->get_name() == "outline"))
1776                                         {
1777                                                 synfig::warning("%s: Updated valuenode connection to use the \"bline\" parameter instead of \"segment_list\".",
1778                                                                                 layer->get_name().c_str());
1779                                                 param_name = "bline";
1780                                         }
1781                                         layer->connect_dynamic_param(param_name,value_node);
1782                         }
1783                                 catch(Exception::IDNotFound)
1784                                 {
1785                                         error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1786                                 }
1787
1788                                 continue;
1789                         }
1790
1791                         xmlpp::Element::NodeList::iterator iter;
1792
1793                         // Search for the first non-text XML element
1794                         for(iter = list.begin(); iter != list.end(); ++iter)
1795                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1796                                         break;
1797                                 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment"   )) break;
1798
1799                         if(iter==list.end())
1800                         {
1801                                 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1802                                 continue;
1803                         }
1804
1805                         // If we recognize the element name as a
1806                         // ValueBase, then treat is at one
1807                         if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1808                         {
1809                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1810
1811                                 if(!data.is_valid())
1812                                 {
1813                                         error((*iter),_("Bad data for <param>"));
1814                                         continue;
1815                                 }
1816
1817                                 // Set the layer's parameter, and make sure that
1818                                 // the layer liked it
1819                                 if(!layer->set_param(param_name,data))
1820                                 {
1821                                         warning((*iter),strprintf(_("Layer '%s' rejected value for parameter '%s'"),
1822                                                                                           element->get_attribute("type")->get_value().c_str(),
1823                                                                                           param_name.c_str()));
1824                                         continue;
1825                                 }
1826                         }
1827                         else    // ... otherwise, we assume that it is a ValueNode
1828                         {
1829                                 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1830
1831                                 if(!value_node)
1832                                 {
1833                                         error((*iter),_("Bad data for <param>"));
1834                                         continue;
1835                                 }
1836
1837                                 // Assign the value_node to the dynamic parameter list
1838                                 layer->connect_dynamic_param(param_name,value_node);
1839                         }
1840
1841                         // Warn if there is trash after the param value
1842                         for(iter++; iter != list.end(); ++iter)
1843                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1844                                         warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1845                         continue;
1846                 }
1847                 else
1848                         error_unexpected_element(child,child->get_name());
1849         }
1850
1851         layer->reset_version();
1852         return layer;
1853 }
1854
1855 Canvas::Handle
1856 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1857 {
1858
1859         if(element->get_name()!="canvas")
1860         {
1861                 error_unexpected_element(element,element->get_name(),"canvas");
1862                 return Canvas::Handle();
1863         }
1864         Canvas::Handle canvas;
1865
1866
1867
1868         if(parent && (element->get_attribute("id") || inline_))
1869         {
1870                 if(inline_)
1871                 {
1872                         canvas=Canvas::create_inline(parent);
1873                 }
1874                 else
1875                 {
1876                         try
1877                         {
1878                                 String warnings;
1879                                 canvas=parent->find_canvas(element->get_attribute("id")->get_value(), warnings);
1880                                 warnings_text += warnings;
1881                         }
1882                         catch(...)
1883                         {
1884                                 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1885                         }
1886                 }
1887                 canvas->rend_desc().clear_flags();
1888         }
1889         else
1890         {
1891                 canvas=Canvas::create();
1892                 if(filename=="/dev/stdin")
1893                         canvas->set_file_name("./stdin.sif");
1894                 else
1895                         canvas->set_file_name(filename);
1896                 canvas->rend_desc().clear_flags();
1897         }
1898
1899         if(element->get_attribute("guid"))
1900         {
1901                 GUID guid(element->get_attribute("guid")->get_value());
1902                 if(guid_cast<Canvas>(guid))
1903                         return guid_cast<Canvas>(guid);
1904                 else
1905                         canvas->set_guid(guid);
1906         }
1907
1908         if(element->get_attribute("version"))
1909                 canvas->set_version(element->get_attribute("version")->get_value());
1910         else if(parent)
1911                 canvas->set_version(parent->get_version());
1912
1913         if(element->get_attribute("width"))
1914         {
1915                 int width = atoi(element->get_attribute("width")->get_value().c_str());
1916                 if (width < 1)
1917                         fatal_error(element, _("Canvas with width or height less than one is not allowed"));
1918                 canvas->rend_desc().set_w(width);
1919         }
1920
1921         if(element->get_attribute("height"))
1922         {
1923                 int height = atoi(element->get_attribute("height")->get_value().c_str());
1924                 if (height < 1)
1925                         fatal_error(element, _("Canvas with width or height less than one is not allowed"));
1926                 canvas->rend_desc().set_h(height);
1927         }
1928
1929         if(element->get_attribute("xres"))
1930                 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1931
1932         if(element->get_attribute("yres"))
1933                 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1934
1935
1936         if(element->get_attribute("fps"))
1937                 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1938
1939         if(element->get_attribute("start-time"))
1940                 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1941
1942         if(element->get_attribute("begin-time"))
1943                 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1944
1945         if(element->get_attribute("end-time"))
1946                 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1947
1948         if(element->get_attribute("antialias"))
1949                 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1950
1951         if(element->get_attribute("view-box"))
1952         {
1953                 string values=element->get_attribute("view-box")->get_value();
1954                 Vector
1955                         tl,
1956                         br;
1957                 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1958                 values=string(values.begin()+values.find(' ')+1,values.end());
1959                 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1960                 values=string(values.begin()+values.find(' ')+1,values.end());
1961                 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1962                 values=string(values.begin()+values.find(' ')+1,values.end());
1963                 br[1]=atof(values.c_str());
1964
1965                 canvas->rend_desc().set_tl(tl);
1966                 canvas->rend_desc().set_br(br);
1967         }
1968
1969         if(element->get_attribute("bgcolor"))
1970         {
1971                 string values=element->get_attribute("bgcolor")->get_value();
1972                 Color bg;
1973
1974                 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1975                 values=string(values.begin()+values.find(' ')+1,values.end());
1976
1977                 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1978                 values=string(values.begin()+values.find(' ')+1,values.end());
1979
1980                 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1981                 values=string(values.begin()+values.find(' ')+1,values.end());
1982
1983                 bg.set_a(atof(values.c_str()));
1984
1985                 canvas->rend_desc().set_bg_color(bg);
1986         }
1987
1988         if(element->get_attribute("focus"))
1989         {
1990                 string values=element->get_attribute("focus")->get_value();
1991                 Vector focus;
1992
1993                 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1994                 values=string(values.begin()+values.find(' ')+1,values.end());
1995                 focus[1]=atof(values.c_str());
1996
1997                 canvas->rend_desc().set_focus(focus);
1998         }
1999
2000         canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2001
2002         xmlpp::Element::NodeList list = element->get_children();
2003         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2004         {
2005                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
2006                 if(child)
2007                 {
2008                         if(child->get_name()=="defs")
2009                         {
2010                                 if(canvas->is_inline())
2011                                         error(child,_("Inline canvas cannot have a <defs> section"));
2012                                 parse_canvas_defs(child, canvas);
2013                         }
2014                         else
2015                         if(child->get_name()=="keyframe")
2016                         {
2017                                 if(canvas->is_inline())
2018                                 {
2019                                         warning(child,_("Inline canvas cannot have keyframes"));
2020                                         continue;
2021                                 }
2022
2023                                 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2024                                 canvas->keyframe_list().sync();
2025                         }
2026                         else
2027                         if(child->get_name()=="meta")
2028                         {
2029                                 if(canvas->is_inline())
2030                                 {
2031                                         warning(child,_("Inline canvases cannot have metadata"));
2032                                         continue;
2033                                 }
2034
2035                                 String name,content;
2036
2037                                 if(!child->get_attribute("name"))
2038                                 {
2039                                         warning(child,_("<meta> must have a name"));
2040                                         continue;
2041                                 }
2042
2043                                 if(!child->get_attribute("content"))
2044                                 {
2045                                         warning(child,_("<meta> must have content"));
2046                                         continue;
2047                                 }
2048
2049                                 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2050                         }
2051                         else if(child->get_name()=="name")
2052                         {
2053                                 xmlpp::Element::NodeList list = child->get_children();
2054
2055                                 // If we don't have any name, warn
2056                                 if(list.empty())
2057                                         warning(child,_("blank \"name\" entity"));
2058
2059                                 string tmp;
2060                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2061                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2062                                 canvas->set_name(tmp);
2063                         }
2064                         else
2065                         if(child->get_name()=="desc")
2066                         {
2067
2068                                 xmlpp::Element::NodeList list = child->get_children();
2069
2070                                 // If we don't have any description, warn
2071                                 if(list.empty())
2072                                         warning(child,_("blank \"desc\" entity"));
2073
2074                                 string tmp;
2075                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2076                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2077                                 canvas->set_description(tmp);
2078                         }
2079                         else
2080                         if(child->get_name()=="author")
2081                         {
2082
2083                                 xmlpp::Element::NodeList list = child->get_children();
2084
2085                                 // If we don't have any description, warn
2086                                 if(list.empty())
2087                                         warning(child,_("blank \"author\" entity"));
2088
2089                                 string tmp;
2090                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2091                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2092                                 canvas->set_author(tmp);
2093                         }
2094                         else
2095                         if(child->get_name()=="layer")
2096                         {
2097                                 //if(canvas->is_inline())
2098                                 //      canvas->push_front(parse_layer(child,canvas->parent()));
2099                                 //else
2100                                         canvas->push_front(parse_layer(child,canvas));
2101                         }
2102                         else
2103                                 error_unexpected_element(child,child->get_name());
2104                 }
2105 //              else
2106 //              if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2107 //                      continue;
2108         }
2109
2110         if(canvas->value_node_list().placeholder_count())
2111         {
2112                 String nodes;
2113                 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2114                         if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2115                         {
2116                                 if (nodes != "") nodes += ", ";
2117                                 nodes += "'" + (*iter)->get_id() + "'";
2118                         }
2119                 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2120                                                                 canvas->get_id().c_str(),
2121                                                                 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2122                                                                 nodes.c_str()));
2123         }
2124
2125         canvas->set_version(CURRENT_CANVAS_VERSION);
2126         return canvas;
2127 }
2128
2129 void
2130 CanvasParser::register_canvas_in_map(Canvas::Handle canvas, String as)
2131 {
2132         get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2133         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2134         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2135 }
2136
2137 #ifdef _DEBUG
2138 void
2139 CanvasParser::show_canvas_map(String file, int line, String text)
2140 {
2141         return;
2142         printf("  .-----\n  |  %s:%d %s\n", file.c_str(), line, text.c_str());
2143         std::map<synfig::String, etl::loose_handle<Canvas> > canvas_map(synfig::get_open_canvas_map());
2144         std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
2145         for (iter = canvas_map.begin(); iter != canvas_map.end(); iter++)
2146         {
2147                 synfig::String first(iter->first);
2148                 etl::loose_handle<Canvas> second(iter->second);
2149                 printf("  |    %40s : %lx (%d)\n", first.c_str(), ulong(&*second), second->count());
2150         }
2151         printf("  `-----\n\n");
2152 }
2153 #endif  // _DEBUG
2154
2155 Canvas::Handle
2156 CanvasParser::parse_from_file_as(const String &file_,const String &as_,String &errors)
2157 {
2158         ChangeLocale change_locale(LC_NUMERIC, "C");
2159         String file(unix_to_local_path(file_));
2160         String as(unix_to_local_path(as_));
2161
2162         try
2163         {
2164                 if(get_open_canvas_map().count(etl::absolute_path(as)))
2165                         return get_open_canvas_map()[etl::absolute_path(as)];
2166
2167                 filename=as;
2168                 total_warnings_=0;
2169                 xmlpp::DomParser parser(file);
2170                 if(parser)
2171                 {
2172                         Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2173                         if (!canvas) return canvas;
2174                         register_canvas_in_map(canvas, as);
2175
2176                         const ValueNodeList& value_node_list(canvas->value_node_list());
2177
2178                         again:
2179                         ValueNodeList::const_iterator iter;
2180                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2181                         {
2182                                 ValueNode::Handle value_node(*iter);
2183                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2184                                 {
2185                                         canvas->remove_value_node(value_node);
2186                                         goto again;
2187                                 }
2188                         }
2189
2190                         return canvas;
2191                 }
2192         }
2193         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2194         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2195         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2196         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2197         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2198         catch(xmlpp::internal_error x)
2199         {
2200                 if (!strcmp(x.what(), "Couldn't create parsing context"))
2201                         throw runtime_error(String("  * ") + _("Can't open file") + " \"" + file + "\"");
2202                 throw;
2203         }
2204         catch(const std::exception& ex)
2205         {
2206                 synfig::error("Standard Exception: "+String(ex.what()));
2207                 errors = ex.what();
2208                 return Canvas::Handle();
2209         }
2210         catch(const String& str)
2211         {
2212                 cerr<<str<<endl;
2213                 //      synfig::error(str);
2214                 errors = str;
2215                 return Canvas::Handle();
2216         }
2217         return Canvas::Handle();
2218 }