Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / savecanvas.cpp
index 6bb025a..eb2bf75 100644 (file)
@@ -2,10 +2,11 @@
 /*!    \file savecanvas.cpp
 **     \brief Writeme
 **
-**     $Id: savecanvas.cpp,v 1.2 2005/01/07 03:29:12 darco Exp $
+**     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
 #include <libxml++/libxml++.h>
 #include <ETL/stringf>
 #include "gradient.h"
+#include <errno.h>
+
+extern "C" {
+#include <libxml/tree.h>
+}
 
 #endif
 
@@ -73,6 +79,9 @@ using namespace synfig;
 
 /* === G L O B A L S ======================================================= */
 
+ReleaseVersion save_canvas_version = ReleaseVersion(RELEASE_VERSION_END-1);
+int valuenode_too_new_count;
+
 /* === P R O C E D U R E S ================================================= */
 
 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas);
@@ -95,7 +104,7 @@ xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
        return root;
 }
 
-xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float fps=0)
+xmlpp::Element* encode_time(xmlpp::Element* root,Time t, float /*fps*/=0)
 {
        root->set_name("time");
        //root->set_attribute("value",t.get_string(fps));
@@ -168,7 +177,7 @@ xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
 
        if(bline_point.get_split_tangent_flag())
                encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
-       
+
        encode_real(root->add_child("width")->add_child("real"),bline_point.get_width());
        encode_real(root->add_child("origin")->add_child("real"),bline_point.get_origin());
        return root;
@@ -177,7 +186,7 @@ xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
 {
        root->set_name("gradient");
-       
+
        Gradient::const_iterator iter;
        x.sort();
        for(iter=x.begin();iter!=x.end();iter++)
@@ -194,7 +203,7 @@ xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::
 xmlpp::Element* encode_list(xmlpp::Element* root,std::list<ValueBase> list, Canvas::ConstHandle canvas=0)
 {
        root->set_name("list");
-       
+
        while(!list.empty())
        {
                encode_value(root->add_child("value"),list.front(),canvas);
@@ -242,7 +251,7 @@ xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::
                root->set_name("nil");
                return root;
        default:
-               synfig::error(strprintf("Unknown value(%s), cannot create XML representation!",ValueBase::type_name(data.get_type()).c_str()));
+               synfig::error(strprintf(_("Unknown value(%s), cannot create XML representation!"),ValueBase::type_local_name(data.get_type()).c_str()));
                root->set_name("nil");
                return root;
        }
@@ -267,9 +276,19 @@ xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHa
                //waypoint_node->add_child(encode_value(iter->get_value(),canvas));
                if(iter->get_value_node()->is_exported())
                        waypoint_node->set_attribute("use",iter->get_value_node()->get_relative_id(canvas));
-               else
-                       encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
-                                       
+               else {
+                       ValueNode::ConstHandle value_node = iter->get_value_node();
+                       if(ValueNode_Const::ConstHandle::cast_dynamic(value_node)) {
+                               const ValueBase data = ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value();
+                               if (data.get_type() == ValueBase::TYPE_CANVAS)
+                                       waypoint_node->set_attribute("use",data.get(Canvas::Handle()).get()->get_relative_id(canvas));
+                               else
+                                       encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
+                       }
+                       else
+                               encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
+               }
+
                switch(iter->get_before())
                {
                case INTERPOLATION_HALT:
@@ -285,7 +304,7 @@ xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHa
                        waypoint_node->set_attribute("before","constant");
                        break;
                case INTERPOLATION_TCB:
-                       // This is the default value, so don't add a new attribute
+                       waypoint_node->set_attribute("before","auto");
                        break;
                default:
                        error("Unknown waypoint type for \"before\" attribute");
@@ -306,10 +325,10 @@ xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHa
                        waypoint_node->set_attribute("after","constant");
                        break;
                case INTERPOLATION_TCB:
-                       // This is the default value, so don't add a new attribute
+                       waypoint_node->set_attribute("after","auto");
                        break;
                default:
-                       error("Unknown waypoint type for \"before\" attribute");
+                       error("Unknown waypoint type for \"after\" attribute");
                }
 
                if(iter->get_tension()!=0.0)
@@ -326,27 +345,6 @@ xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHa
        return root;
 }
 
-xmlpp::Element* encode_composite(xmlpp::Element* root,ValueNode_Composite::ConstHandle value_node,Canvas::ConstHandle canvas=0)
-{
-       assert(value_node);
-       root->set_name("composite");
-
-       root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
-
-       int i;
-       for(i=0;i<value_node->link_count();i++)
-       {
-               string name(strprintf("c%d",i+1));
-               assert(value_node->get_link(i));
-               if(value_node->get_link(i)->is_exported())
-                       root->set_attribute(name,value_node->get_link(i)->get_relative_id(canvas));
-               else
-                       encode_value_node(root->add_child(name)->add_child("value_node"),value_node->get_link(i).constant(),canvas);
-       }
-
-       return root;
-}
-
 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
 {
        assert(value_node);
@@ -388,9 +386,8 @@ xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHa
 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
 {
        assert(value_node);
-//     const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
-       const float fps(0);
-       
+       const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
+
        root->set_name(value_node->get_name());
 
        root->set_attribute("type",ValueBase::type_name(value_node->get_contained_type()));
@@ -398,7 +395,7 @@ xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::
        vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
 
        ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
-       
+
        if(bline_value_node)
        {
                if(bline_value_node->get_loop())
@@ -406,7 +403,7 @@ xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::
                else
                        root->set_attribute("loop","false");
        }
-       
+
        for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
        {
                xmlpp::Element  *entry_node=root->add_child("entry");
@@ -425,41 +422,47 @@ xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::
 
                        const ActivepointList& timing_info(iter->timing_info);
                        ActivepointList::const_iterator entry_iter;
-                       
+
                        for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
                                if(entry_iter->state==true)
                                {
                                        if(entry_iter->priority)
+                                       {
+                                               printf("begin priority is %d\n", entry_iter->priority);
                                                begin_sequence+=strprintf("p%d ",entry_iter->priority);
+                                       }
                                        begin_sequence+=entry_iter->time.get_string(fps)+", ";
-                               }       
+                               }
                                else
                                {
                                        if(entry_iter->priority)
+                                       {
+                                               printf("end priority is %d\n", entry_iter->priority);
                                                end_sequence+=strprintf("p%d ",entry_iter->priority);
+                                       }
                                        end_sequence+=entry_iter->time.get_string(fps)+", ";
                                }
-                       
+
                        // If this is just a plane-jane vanilla entry,
                        // then don't bother with begins and ends
                        if(end_sequence.empty() && begin_sequence=="SOT, ")
                                begin_sequence.clear();
-                       
+
                        if(!begin_sequence.empty())
                        {
                                // Remove the last ", " stuff
                                begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
                                // Add the attribute
-                               entry_node->set_attribute("on",begin_sequence);                         
-                       }                               
+                               entry_node->set_attribute("on",begin_sequence);
+                       }
 
                        if(!end_sequence.empty())
                        {
                                // Remove the last ", " stuff
                                end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
                                // Add the attribute
-                               entry_node->set_attribute("off",end_sequence);                          
-                       }                               
+                               entry_node->set_attribute("off",end_sequence);
+                       }
                }
        }
 
@@ -470,7 +473,26 @@ xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::
 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
 {
        assert(value_node);
-       root->set_name(value_node->get_name());
+
+       String name(value_node->get_name());
+       ReleaseVersion saving_version(get_file_version());
+       ReleaseVersion feature_version(LinkableValueNode::book()[name].release_version);
+
+       if (saving_version < feature_version)
+       {
+               valuenode_too_new_count++;
+               warning("can't save <%s> valuenodes in this old file format version", name.c_str());
+
+               ValueBase value((*value_node)(0));
+               encode_value(root,value,canvas);
+
+               // ValueNode_Const::ConstHandle const_value(ValueNode_Const::create((*value_node)(0)));
+               // encode_value_node(root,const_value,canvas);
+
+               return root;
+       }
+
+       root->set_name(name);
 
        root->set_attribute("type",ValueBase::type_name(value_node->get_type()));
 
@@ -496,9 +518,6 @@ xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle va
        if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
                encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
        else
-       if(ValueNode_Composite::ConstHandle::cast_dynamic(value_node))
-               encode_composite(root,ValueNode_Composite::ConstHandle::cast_dynamic(value_node),canvas);
-       else
        if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
                encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
        else
@@ -557,7 +576,7 @@ xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
 
                        handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
 
-                       // If the valuenode.has no ID, then it must be defined in-place
+                       // If the valuenode has no ID, then it must be defined in-place
                        if(value_node->get_id().empty())
                        {
                                encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
@@ -573,26 +592,35 @@ xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
                        ValueBase value=layer->get_param(iter->get_name());
                        if(!value.is_valid())
                        {
-                               error("Layer doesn't know it's own vocabulary -- "+iter->get_name());
+                               error("Layer doesn't know its own vocabulary -- "+iter->get_name());
                                continue;
                        }
 
-                       if(value.get_type()==ValueBase::TYPE_CANVAS && !value.get(Canvas::LooseHandle())->is_inline())
+                       if(value.get_type()==ValueBase::TYPE_CANVAS)
                        {
-                               Canvas::Handle child(value.get(Canvas::LooseHandle()));
-                               
-                               if(!value.get(Canvas::Handle()))
+                               // the ->is_inline() below was crashing if the canvas
+                               // contained a PasteCanvas with the default <No Image
+                               // Selected> Canvas setting;  this avoids the crash
+                               if (!value.get(Canvas::LooseHandle()))
                                        continue;
-                               xmlpp::Element *node=root->add_child("param");
-                               node->set_attribute("name",iter->get_name());
-                               node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
-                               continue;
+
+                               if (!value.get(Canvas::LooseHandle())->is_inline())
+                               {
+                                       Canvas::Handle child(value.get(Canvas::LooseHandle()));
+
+                                       if(!value.get(Canvas::Handle()))
+                                               continue;
+                                       xmlpp::Element *node=root->add_child("param");
+                                       node->set_attribute("name",iter->get_name());
+                                       node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
+                                       continue;
+                               }
                        }
                        xmlpp::Element *node=root->add_child("param");
                        node->set_attribute("name",iter->get_name());
 
                        encode_value(node->add_child("value"),value,layer->get_canvas().constant());
-               }               
+               }
        }
 
 
@@ -604,9 +632,9 @@ xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
        assert(canvas);
        const RendDesc &rend_desc=canvas->rend_desc();
        root->set_name("canvas");
-       
+
        if(canvas->is_root())
-               root->set_attribute("version","0.1");
+               root->set_attribute("version",canvas->get_version());
 
        if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
                root->set_attribute("id",canvas->get_id());
@@ -645,7 +673,7 @@ xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
 
        if(!canvas->parent() || canvas->parent()->rend_desc().get_time_end()!=canvas->rend_desc().get_time_end())
                root->set_attribute("end-time",rend_desc.get_time_end().get_string(rend_desc.get_frame_rate()));
-       
+
        if(!canvas->is_inline())
        {
                root->set_attribute("bgcolor",strprintf(VIEW_BOX_FORMAT,
@@ -673,9 +701,10 @@ xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
                for(KeyframeList::const_iterator iter=canvas->keyframe_list().begin();iter!=canvas->keyframe_list().end();++iter)
                        encode_keyframe(root->add_child("keyframe"),*iter,canvas->rend_desc().get_frame_rate());
        }
-       
+
        // Output the <defs> section
-       if(!canvas->is_inline() && !canvas->value_node_list().empty() || !canvas->children().empty())
+       //! \todo check where the parentheses should really go - around the && or the ||?
+       if((!canvas->is_inline() && !canvas->value_node_list().empty()) || !canvas->children().empty())
        {
                xmlpp::Element *node=root->add_child("defs");
                const ValueNodeList &value_node_list(canvas->value_node_list());
@@ -697,16 +726,28 @@ xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
                {
                        encode_canvas(node->add_child("canvas"),*iter);
                }
-       }       
+       }
 
        Canvas::const_reverse_iterator iter;
 
        for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
-               encode_layer(root->add_child("layer"),*iter);   
+               encode_layer(root->add_child("layer"),*iter);
 
        return root;
 }
 
+xmlpp::Element* encode_canvas_toplevel(xmlpp::Element* root,Canvas::ConstHandle canvas)
+{
+       valuenode_too_new_count = 0;
+
+       xmlpp::Element* ret = encode_canvas(root, canvas);
+
+       if (valuenode_too_new_count)
+               warning("saved %d valuenodes as constant values in old file format\n", valuenode_too_new_count);
+
+       return ret;
+}
+
 bool
 synfig::save_canvas(const String &filename, Canvas::ConstHandle canvas)
 {
@@ -714,23 +755,28 @@ synfig::save_canvas(const String &filename, Canvas::ConstHandle canvas)
 
        synfig::String tmp_filename(filename+".TMP");
 
+       if (filename_extension(filename) == ".sifz")
+               xmlSetCompressMode(9);
+       else
+               xmlSetCompressMode(0);
+
        try
        {
                assert(canvas);
                xmlpp::Document document;
-       
-               encode_canvas(document.create_root_node("canvas"),canvas);
-       
+
+               encode_canvas_toplevel(document.create_root_node("canvas"),canvas);
+
                document.write_to_file_formatted(tmp_filename);
        }
        catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; }
-       
-       
+
+
 #ifdef _WIN32
        // On Win32 platforms, rename() has bad behavior. work around it.
        char old_file[80]="sif.XXXXXXXX";
        mktemp(old_file);
-       rename(filename.c_str(),old_file);      
+       rename(filename.c_str(),old_file);
        if(rename(tmp_filename.c_str(),filename.c_str())!=0)
        {
                rename(old_file,tmp_filename.c_str());
@@ -757,7 +803,19 @@ synfig::canvas_to_string(Canvas::ConstHandle canvas)
 
        xmlpp::Document document;
 
-       encode_canvas(document.create_root_node("canvas"),canvas);
+       encode_canvas_toplevel(document.create_root_node("canvas"),canvas);
 
        return document.write_to_string_formatted();
 }
+
+void
+synfig::set_file_version(ReleaseVersion version)
+{
+       save_canvas_version = version;
+}
+
+ReleaseVersion
+synfig::get_file_version()
+{
+       return save_canvas_version;
+}