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