Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / canvas.cpp
index 739cf7e..c50b901 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \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
@@ -68,8 +69,9 @@ int _CanvasCounter::counter(0);
 
 /* === M E T H O D S ======================================================= */
 
-Canvas::Canvas(const string &id):
+Canvas::Canvas(const String &id):
        id_                     (id),
+       version_        (CURRENT_CANVAS_VERSION),
        cur_time_       (0),
        is_inline_      (false),
        is_dirty_       (true),
@@ -88,9 +90,28 @@ Canvas::on_changed()
 
 Canvas::~Canvas()
 {
+       // we were having a crash where pastecanvas layers were still
+       // refering to a canvas after it had been destroyed;  this code
+       // will stop the pastecanvas layers from refering to the canvas
+       // before the canvas is destroyed
+
+       // the set_sub_canvas(0) ends up deleting the parent-child link,
+       // which deletes the current element from the set we're iterating
+       // through, so we have to make sure we've incremented the iterator
+       // before we mess with the pastecanvas
+       std::set<Node*>::iterator iter = parent_set.begin();
+       while (iter != parent_set.end())
+       {
+               Layer_PasteCanvas* paste_canvas = dynamic_cast<Layer_PasteCanvas*>(*iter);
+               iter++;
+               if(paste_canvas)
+                       paste_canvas->set_sub_canvas(0);
+               else
+                       warning("destroyed canvas has a parent that is not a pastecanvas - please report if repeatable");
+       }
+
        //if(is_inline() && parent_) assert(0);
        _CanvasCounter::counter--;
-       //DEBUGPOINT();
        clear();
        begin_delete();
 }
@@ -411,7 +432,6 @@ Canvas::add_value_node(ValueNode::Handle x, const String &id)
                return parent_->add_value_node(x,id);
 //             throw runtime_error("You cannot add a ValueNode to an inline Canvas");
 
-       //DEBUGPOINT();
        if(x->is_exported())
                throw runtime_error("ValueNode is already exported");
 
@@ -423,16 +443,13 @@ Canvas::add_value_node(ValueNode::Handle x, const String &id)
 
        try
        {
-               //DEBUGPOINT();
                if(PlaceholderValueNode::Handle::cast_dynamic(value_node_list_.find(id)))
                        throw Exception::IDNotFound("add_value_node()");
 
-               //DEBUGPOINT();
                throw Exception::IDAlreadyExists(id);
        }
        catch(Exception::IDNotFound)
        {
-               //DEBUGPOINT();
                x->set_id(id);
 
                x->set_parent_canvas(this);
@@ -442,7 +459,6 @@ Canvas::add_value_node(ValueNode::Handle x, const String &id)
                        synfig::error("Unable to add ValueNode");
                        throw std::runtime_error("Unable to add ValueNode");
                }
-               //DEBUGPOINT();
 
                return;
        }
@@ -662,7 +678,6 @@ Canvas::create()
 void
 Canvas::push_back(etl::handle<Layer> x)
 {
-//     DEBUGPOINT();
 //     int i(x->count());
        insert(end(),x);
        //if(x->count()!=i+1)synfig::info("push_back before %d, after %d",i,x->count());
@@ -671,7 +686,6 @@ Canvas::push_back(etl::handle<Layer> x)
 void
 Canvas::push_front(etl::handle<Layer> x)
 {
-//     DEBUGPOINT();
 //     int i(x->count());
        insert(begin(),x);
        //if(x->count()!=i+1)synfig::error("push_front before %d, after %d",i,x->count());
@@ -733,7 +747,7 @@ Canvas::push_back_simple(etl::handle<Layer> x)
 }
 
 void
-Canvas::erase(Canvas::iterator iter)
+Canvas::erase(iterator iter)
 {
        if(!(*iter)->get_group().empty())
                remove_group_pair((*iter)->get_group(),(*iter));
@@ -768,7 +782,7 @@ Canvas::clone(const GUID& deriv_guid)const
        {
                name=get_id()+"_CLONE";
 
-               throw runtime_error("Cloning of non-inline canvases is not yet suported");
+               throw runtime_error("Cloning of non-inline canvases is not yet supported");
        }
 
        Handle canvas(new Canvas(name));
@@ -776,7 +790,11 @@ Canvas::clone(const GUID& deriv_guid)const
        if(is_inline())
        {
                canvas->is_inline_=true;
-               canvas->parent_=0;
+               // \todo this was setting parent_=0 - is there a reason for that?
+               // this was causing bug 1838132, where cloning an inline canvas that contains an imported image fails
+               // it was failing to ascertain the absolute pathname of the imported image, since it needs the pathname
+               // of the canvas to get that, which is stored in the parent canvas
+               canvas->parent_=parent();
                //canvas->set_inline(parent());
        }
 
@@ -1012,16 +1030,21 @@ Canvas::get_meta_data_keys()const
        return ret;
 }
 
+/* note - the "Motion Blur" and "Duplicate" layers need the dynamic
+                 parameters of any PasteCanvas layers they loop over to be
+                 maintained.  When the variables in the following function
+                 refer to "motion blur", they mean either of these two
+                 layers. */
 void
-synfig::optimize_layers(Context context, Canvas::Handle op_canvas, bool seen_motion_blur_in_parent)
+synfig::optimize_layers(Time time, Context context, Canvas::Handle op_canvas, bool seen_motion_blur_in_parent)
 {
        Context iter;
 
        std::vector< std::pair<float,Layer::Handle> > sort_list;
-       int i, motion_blur_i;           // motion_blur_i is for resolving which layer comes first in the event of a z_depth tie
-       float motion_blur_z_depth;      // the z_depth of the least deep motion blur layer in this context
+       int i, motion_blur_i=0; // motion_blur_i is for resolving which layer comes first in the event of a z_depth tie
+       float motion_blur_z_depth=0; // the z_depth of the least deep motion blur layer in this context
        bool seen_motion_blur_locally = false;
-       bool motion_blurred;            // the final result - is this layer blurred or not?
+       bool motion_blurred; // the final result - is this layer blurred or not?
 
        // If the parent didn't cause us to already be motion blurred,
        // check whether there's a motion blur in this context,
@@ -1040,7 +1063,7 @@ synfig::optimize_layers(Context context, Canvas::Handle op_canvas, bool seen_mot
                        if(value.get_type()==ValueBase::TYPE_REAL && value.get(Real())==0)
                                continue;
 
-                       if(layer->get_name()=="MotionBlur")
+                       if(layer->get_name()=="MotionBlur" || layer->get_name()=="duplicate")
                        {
                                float z_depth(layer->get_z_depth()*1.0001+i);
 
@@ -1079,9 +1102,12 @@ synfig::optimize_layers(Context context, Canvas::Handle op_canvas, bool seen_mot
                if(value.get_type()==ValueBase::TYPE_REAL && value.get(Real())==0)
                        continue;
 
-               Layer_PasteCanvas* paste_canvas(static_cast<Layer_PasteCanvas*>(layer.get()));
-               if(layer->get_name()=="PasteCanvas" && paste_canvas->get_time_offset()==0)
+               // note: this used to include "&& paste_canvas->get_time_offset()==0", but then
+               //               time-shifted layers weren't being sorted by z-depth (bug #1806852)
+               if(layer->get_name()=="PasteCanvas")
                {
+                       Layer_PasteCanvas* paste_canvas(static_cast<Layer_PasteCanvas*>(layer.get()));
+
                        // we need to blur the sub canvas if:
                        // our parent is blurred,
                        // or the child is lower than a local blur,
@@ -1106,41 +1132,49 @@ synfig::optimize_layers(Context context, Canvas::Handle op_canvas, bool seen_mot
                        Canvas::Handle sub_canvas(Canvas::create_inline(op_canvas));
                        Canvas::Handle paste_sub_canvas = paste_canvas->get_sub_canvas();
                        if(paste_sub_canvas)
-                               optimize_layers(paste_sub_canvas->get_context(),sub_canvas,motion_blurred);
-//#define SYNFIG_OPTIMIZE_PASTE_CANVAS 1
+                               optimize_layers(time, paste_sub_canvas->get_context(),sub_canvas,motion_blurred);
 
+// \todo: uncommenting the following breaks the rendering of at least examples/backdrop.sifz quite severely
+// #define SYNFIG_OPTIMIZE_PASTE_CANVAS
 #ifdef SYNFIG_OPTIMIZE_PASTE_CANVAS
                        Canvas::iterator sub_iter;
-                       // Determine if we can just remove the paste canvas
-                       // altogether
-                       if(paste_canvas->get_blend_method()==Color::BLEND_COMPOSITE && paste_canvas->get_amount()==1.0f && paste_canvas->get_zoom()==0 && paste_canvas->get_time_offset()==0 && paste_canvas->get_origin()==Point(0,0))
-                       try{
-                               for(sub_iter=sub_canvas->begin();sub_iter!=sub_canvas->end();++sub_iter)
-                               {
-                                       Layer* layer=sub_iter->get();
 
-                                       // any layers that deform end up breaking things
-                                       // so do things the old way if we run into anything like this
-                                       if(!dynamic_cast<Layer_NoDeform*>(layer))
-                                               throw int();
+                       // Determine if we can just remove the paste canvas altogether
+                       if (paste_canvas->get_blend_method()    == Color::BLEND_COMPOSITE       &&
+                               paste_canvas->get_amount()                      == 1.0f                                         &&
+                               paste_canvas->get_zoom()                        == 0                                            &&
+                               paste_canvas->get_time_offset()         == 0                                            &&
+                               paste_canvas->get_origin()                      == Point(0,0)                           )
+                               try {
+                                       for(sub_iter=sub_canvas->begin();sub_iter!=sub_canvas->end();++sub_iter)
+                                       {
+                                               Layer* layer=sub_iter->get();
 
-                                       ValueBase value(layer->get_param("blend_method"));
-                                       if(value.get_type()!=ValueBase::TYPE_INTEGER || value.get(int())!=(int)Color::BLEND_COMPOSITE)
-                                               throw int();
-                               }
+                                               // any layers that deform end up breaking things
+                                               // so do things the old way if we run into anything like this
+                                               if(!dynamic_cast<Layer_NoDeform*>(layer))
+                                                       throw int();
 
-                               // It has turned out that we don't need a paste canvas
-                               // layer, so just go ahead and add all the layers onto
-                               // the current stack and be done with it
-                               while(sub_canvas->size())
-                               {
-                                       sort_list.push_back(std::pair<float,Layer::Handle>(z_depth,sub_canvas->front()));
-                                       //op_canvas->push_back_simple(sub_canvas->front());
-                                       sub_canvas->pop_front();
+                                               ValueBase value(layer->get_param("blend_method"));
+                                               if(value.get_type()!=ValueBase::TYPE_INTEGER || value.get(int())!=(int)Color::BLEND_COMPOSITE)
+                                                       throw int();
+                                       }
+
+                                       // It has turned out that we don't need a paste canvas
+                                       // layer, so just go ahead and add all the layers onto
+                                       // the current stack and be done with it
+                                       while(sub_canvas->size())
+                                       {
+                                               sort_list.push_back(std::pair<float,Layer::Handle>(z_depth,sub_canvas->front()));
+                                               //op_canvas->push_back_simple(sub_canvas->front());
+                                               sub_canvas->pop_front();
+                                       }
+                                       continue;
                                }
-                               continue;
-                       }catch(int) { }
-#endif
+                               catch(int)
+                               { }
+#endif // SYNFIG_OPTIMIZE_PASTE_CANVAS
+
                        Layer::Handle new_layer(Layer::create("PasteCanvas"));
                        dynamic_cast<Layer_PasteCanvas*>(new_layer.get())->set_muck_with_time(false);
                        if (motion_blurred)
@@ -1156,6 +1190,45 @@ synfig::optimize_layers(Context context, Canvas::Handle op_canvas, bool seen_mot
                        dynamic_cast<Layer_PasteCanvas*>(new_layer.get())->set_muck_with_time(true);
                        layer=new_layer;
                }
+               else                                    // not a PasteCanvas - does it use blend method 'Straight'?
+               {
+                       /* when we use the 'straight' blend method, every pixel on the layer affects the layers underneath,
+                        * not just the non-transparent pixels; the following workarea wraps non-pastecanvas layers in a
+                        * new pastecanvas to ensure that the straight blend affects the full plane, not just the area
+                        * within the layer's bounding box
+                        */
+
+                       // \todo: this code probably needs modification to work properly with motionblur and duplicate
+                       etl::handle<Layer_Composite> composite = etl::handle<Layer_Composite>::cast_dynamic(layer);
+
+                       /* some layers (such as circle) don't touch pixels that aren't
+                        * part of the circle, so they don't get blended correctly when
+                        * using a straight blend.  so we encapsulate the circle, and the
+                        * encapsulation layer takes care of the transparent pixels
+                        * for us.  if we do that for all layers, however, then the
+                        * distortion layers no longer work, since they have no
+                        * context to work on.  the Layer::reads_context() method
+                        * returns true for layers which need to be able to see
+                        * their context.  we can't encapsulate those.
+                        */
+                       if (composite &&
+                               Color::is_straight(composite->get_blend_method()) &&
+                               !composite->reads_context())
+                       {
+                               Canvas::Handle sub_canvas(Canvas::create_inline(op_canvas));
+                               sub_canvas->push_back(composite = composite->clone());
+                               layer = Layer::create("PasteCanvas");
+                               composite->set_description(strprintf("Wrapped clone of '%s'", composite->get_non_empty_description().c_str()));
+                               layer->set_description(strprintf("PasteCanvas wrapper for '%s'", composite->get_non_empty_description().c_str()));
+                               Layer_PasteCanvas* paste_canvas(static_cast<Layer_PasteCanvas*>(layer.get()));
+                               paste_canvas->set_blend_method(composite->get_blend_method());
+                               paste_canvas->set_amount(composite->get_amount());
+                               sub_canvas->set_time(time); // region and outline don't calculate their bounding rects until their time is set
+                               composite->set_blend_method(Color::BLEND_STRAIGHT); // do this before calling set_sub_canvas(), but after set_time()
+                               composite->set_amount(1.0f); // after set_time()
+                               paste_canvas->set_sub_canvas(sub_canvas);
+                       }
+               }
 
                sort_list.push_back(std::pair<float,Layer::Handle>(z_depth,layer));
                //op_canvas->push_back_simple(layer);
@@ -1250,13 +1323,13 @@ Canvas::remove_group_pair(String group, etl::handle<Layer> layer)
 }
 
 void
-Canvas::add_connection(Layer::LooseHandle layer, sigc::connection connection)
+Canvas::add_connection(etl::loose_handle<Layer> layer, sigc::connection connection)
 {
        connections_[layer].push_back(connection);
 }
 
 void
-Canvas::disconnect_connections(Layer::LooseHandle layer)
+Canvas::disconnect_connections(etl::loose_handle<Layer> layer)
 {
        std::vector<sigc::connection>::iterator iter;
        for(iter=connections_[layer].begin();iter!=connections_[layer].end();++iter)