]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/score-element.cc
release: 1.3.54
[lilypond.git] / lily / score-element.cc
index adede15101459055bd2fb31ff965e027a0e1eb97..9aeac9c546040b844e283812424ea553b348be1c 100644 (file)
@@ -8,7 +8,9 @@
 
 
 #include <string.h>
+#include <math.h>
 
+#include "libc-extension.hh"
 #include "group-interface.hh"
 #include "misc.hh"
 #include "paper-score.hh"
@@ -27,6 +29,7 @@
 #include "dimension-cache.hh"
 #include "side-position-interface.hh"
 #include "item.hh"
+
 /*
 TODO:
 
@@ -36,15 +39,11 @@ remove dynamic_cast<Spanner,Item> and put this code into respective
 
 Score_element::Score_element()
 {
-  dim_cache_[X_AXIS] = new Dimension_cache;
-  dim_cache_[Y_AXIS] = new Dimension_cache;
-  dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
-
   // junkme.
   used_b_ = false;
+  set_extent_callback (molecule_extent, X_AXIS);
+  set_extent_callback (molecule_extent, Y_AXIS);    
 
-  dim_cache_[X_AXIS]->set_extent_callback (molecule_extent);
-  dim_cache_[Y_AXIS]->set_extent_callback (molecule_extent); 
   used_b_ = false;
   pscore_l_=0;
   lookup_l_ =0;
@@ -54,19 +53,14 @@ Score_element::Score_element()
   element_property_alist_ = SCM_EOL;
 
   smobify_self ();
-
-
   set_elt_property ("dependencies", SCM_EOL);
   set_elt_property ("interfaces", SCM_EOL);
 }
 
 
 Score_element::Score_element (Score_element const&s)
+   : dim_cache_ (s.dim_cache_)
 {
-  dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
-  dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
-  dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
-  
   self_scm_ = SCM_EOL;
   used_b_ = true;
   original_l_ =(Score_element*) &s;
@@ -81,11 +75,6 @@ Score_element::Score_element (Score_element const&s)
 
 Score_element::~Score_element()
 {
-  assert (status_i_ >=0);
-  status_i_  = -1;
-
-  delete dim_cache_[X_AXIS];
-  delete dim_cache_[Y_AXIS];  
 }
 
 // should also have one that takes SCM arg. 
@@ -134,18 +123,16 @@ Score_element::set_elt_property (String k, SCM v)
 }
 
 Interval
-Score_element::molecule_extent (Dimension_cache const *c)
+Score_element::molecule_extent (Score_element const *s, Axis a )
 {
-  Score_element *s = dynamic_cast<Score_element*>(c->element_l());
   Molecule m = s->do_brew_molecule();
-  return m.extent()[c->axis ()];
+  return m.extent(a);
 }
 
 Interval
-Score_element::preset_extent (Dimension_cache const *c)
+Score_element::preset_extent (Score_element const *s , Axis a )
 {
-  Score_element *s = dynamic_cast<Score_element*>(c->element_l());
-  SCM ext = s->get_elt_property ((c->axis () == X_AXIS)
+  SCM ext = s->get_elt_property ((a == X_AXIS)
                                 ? "extent-X"
                                 : "extent-Y");
   
@@ -162,22 +149,6 @@ Score_element::preset_extent (Dimension_cache const *c)
 }
 
 
-void
-Score_element::print() const
-{
-#ifndef NPRINT
-  DEBUG_OUT << classname(this) << "{\n";
-    
-  if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
-    ly_display_scm (element_property_alist_);
-
-  if (original_l_)
-    DEBUG_OUT << "Copy ";
-  do_print();
-  
-  DEBUG_OUT <<  "}\n";
-#endif
-}
 
 Paper_def*
 Score_element::paper_l ()  const
@@ -209,24 +180,6 @@ Score_element::add_processing()
     return;
   status_i_ ++;
 
-#if 0
-    /*
-    UGH. UGH. UGH.
-   */
-  if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
-      && !dim_cache_[X_AXIS]->off_callback_l_)
-    {
-      add_offset_callback (Side_position_interface::aligned_on_self,X_AXIS);
-    }
-  
-  if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
-      && !dim_cache_[X_AXIS]->off_callback_l_)
-      
-    {
-      add_offset_callback (Side_position_interface::aligned_on_self, Y_AXIS);
-    }
-#endif
-  
   do_add_processing();
 }
 
@@ -247,7 +200,7 @@ Score_element::calculate_dependencies (int final, int busy,
   
   status_i_= busy;
 
-  for (SCM d=  get_elt_property ("dependencies"); d != SCM_EOL; d = gh_cdr (d))
+  for (SCM d=  get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
     {
       unsmob_element (gh_car (d))
        ->calculate_dependencies (final, busy, funcptr);
@@ -257,26 +210,16 @@ Score_element::calculate_dependencies (int final, int busy,
   status_i_= final;
 }
 
-void
-Score_element::output_processing () 
+Molecule
+Score_element::get_molecule ()  const
 {
   if (to_boolean (get_elt_property ("transparent")))
-    return;
-
-  Molecule m (do_brew_molecule ());
-  Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
+    return Molecule ();
 
-  SCM s = get_elt_property ("extra-offset");
-  if (gh_pair_p (s))
-    {
-      Real il = paper_l ()->get_var ("interline");
-      o[X_AXIS] += il * gh_scm2double (gh_car (s));
-      o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
-    }
-  
-  line_l ()->output_molecule (m.expr_, o);
+  return do_brew_molecule ();
 }
 
+
 /*
   
   VIRTUAL STUBS
@@ -309,7 +252,9 @@ Score_element::do_add_processing()
 }
 
 
-
+/*
+  ugh.
+ */  
 Molecule 
 Score_element::do_brew_molecule() const
 {
@@ -358,60 +303,68 @@ SCM
 Score_element::handle_broken_smobs (SCM src, SCM criterion)
 {
  again:
-
-  
   Score_element *sc = unsmob_element (src);
   if (sc)
     {
-      if (criterion == SCM_UNDEFINED)
-       return SCM_UNDEFINED;
-      else if (gh_number_p (criterion))
+      if (gh_number_p (criterion))
        {
          Item * i = dynamic_cast<Item*> (sc);
          Direction d = to_dir (criterion);
          if (i && i->break_status_dir () != d)
            {
-             Item *br = i->find_broken_piece (d);
+             Item *br = i->find_prebroken_piece (d);
              return  (br) ? br->self_scm_ : SCM_UNDEFINED;
            }
        }
       else
        {
-         Score_element * ln = unsmob_element ( criterion);
-         Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
-         Score_element * br =0;
-         Line_of_score * dep_line = sc->line_l ();
-         if (dep_line != line)
+         Line_of_score * line
+           = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
+         if (sc->line_l () != line)
            {
-             br = sc->find_broken_piece (line);
-             return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
+             sc = sc->find_broken_piece (line);
+
            }
-         if (!dep_line)
+
+         /* now: !sc || (sc && sc->line_l () == line) */
+         if (!sc)
            return SCM_UNDEFINED;
+
+         /* now: sc && sc->line_l () == line */
+         if (!line
+             || (sc->common_refpoint (line, X_AXIS)
+                 && sc->common_refpoint (line, Y_AXIS)))
+           {
+             return sc->self_scm_;
+           }
+         return SCM_UNDEFINED;
        }
     }
   else if (gh_pair_p (src))
     {
+      SCM oldcar =gh_car (src);
       /*
        UGH! breaks on circular lists.
       */
-      SCM car = handle_broken_smobs (gh_car (src), criterion);
-      SCM cdr = gh_cdr (src);
+      SCM newcar = handle_broken_smobs (oldcar, criterion);
+      SCM oldcdr = gh_cdr (src);
       
-      if (car == SCM_UNDEFINED
-         && (gh_pair_p (cdr) || cdr == SCM_EOL))
+      if (newcar == SCM_UNDEFINED
+         && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
        {
          /*
            This is tail-recursion, ie. 
            
            return handle_broken_smobs (cdr, criterion);
 
-           We don't want to rely on the compiler to do this.  */
-         src =  cdr;   
+           We don't want to rely on the compiler to do this.  Without
+           tail-recursion, this easily crashes with a stack overflow.  */
+         src =  oldcdr;
          goto again;
        }
 
-      return gh_cons (car, handle_broken_smobs (cdr, criterion));
+      SCM newcdr = handle_broken_smobs (oldcdr, criterion);
+      return gh_cons (newcar, newcdr);
     }
   else
     return src;
@@ -438,41 +391,60 @@ Score_element::handle_broken_dependencies()
        }
     }
 
+
   Line_of_score *line = line_l();
-  element_property_alist_
-    = handle_broken_smobs (element_property_alist_,
-                          line ? line->self_scm_ : SCM_UNDEFINED);
-}
 
+  if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
+    {
+      element_property_alist_
+       = handle_broken_smobs (element_property_alist_,
+                              line ? line->self_scm_ : SCM_UNDEFINED);
+    }
+  else if (dynamic_cast <Line_of_score*> (this))
+    {
+      element_property_alist_ = handle_broken_smobs (element_property_alist_,
+                                                    SCM_UNDEFINED);
+    }
+  else
+    {
+      /*
+       This element is `invalid'; it has been removed from all
+       dependencies, so let's junk the element itself.
+
+       do not do this for Line_of_score, since that would remove
+       references to the originals of score-elts, which get then GC'd
+       (a bad thing.)
+      */
+      suicide();
+    }
+}
 
 /*
-  TODO: cleanify.
- */
+ Note that we still want references to this element to be
+ rearranged, and not silently thrown away, so we keep pointers
+ like {broken_into_{drul,array}, original}
+*/
+void
+Score_element::suicide ()
+{
+  element_property_alist_ = SCM_EOL;
+  set_extent_callback (0, Y_AXIS);
+  set_extent_callback (0, X_AXIS);
+}
+
+
 void
 Score_element::handle_prebroken_dependencies()
 {
-  if (Item*i =dynamic_cast<Item*> (this))
-    {
-      if (original_l_)
-       {
-         element_property_alist_
-           = handle_broken_smobs (original_l_->element_property_alist_,
-                              gh_int2scm (i->break_status_dir ()));
-       }
-    }
 }
 
+
 bool
 Score_element::linked_b() const
 {
   return used_b_;
 }
 
-void
-Score_element::do_print () const
-{
-}
-
 Score_element*
 Score_element::find_broken_piece (Line_of_score*) const
 {
@@ -482,38 +454,79 @@ Score_element::find_broken_piece (Line_of_score*) const
 void
 Score_element::translate_axis (Real y, Axis a)
 {
-  dim_cache_[a]->translate (y);
+  dim_cache_[a].offset_ += y;
 }  
 
 Real
-Score_element::relative_coordinate (Score_element const*e, Axis a) const
+Score_element::relative_coordinate (Score_element const*refp, Axis a) const
 {
-  return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
+  if (refp == this)
+    return 0.0;
+
+  /*
+    We catch PARENT_L_ == nil case with this, but we crash if we did
+    not ask for the absolute coordinate (ie. REFP == nil.)
+    
+   */
+  if (refp == dim_cache_[a].parent_l_)
+    return get_offset (a);
+  else
+    return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
 }
 
-Score_element * 
-Score_element::common_refpoint (Score_element const* s, Axis a) const
+Real
+Score_element::get_offset (Axis a) const
 {
-  Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
-  return  dim ? dim->element_l () : 0;
+  Score_element *me = (Score_element*) this;
+  while (dim_cache_[a].off_callbacks_.size ())
+    {
+      Offset_callback c = dim_cache_[a].off_callbacks_[0];
+      me->dim_cache_[a].off_callbacks_.del (0);
+      Real r =  (*c) (me,a );
+      if (isinf (r) || isnan (r))
+       {
+         r = 0.0;
+         programming_error ("Infinity or NaN encountered");
+       }
+      me->dim_cache_[a].offset_ +=r;
+    }
+  return dim_cache_[a].offset_;
+}
+
+
+Interval
+Score_element::point_dimension_callback (Score_element const* , Axis)
+{
+  return Interval (0,0);
 }
 
 bool
 Score_element::empty_b (Axis a)const
 {
-  return !dim_cache_[a]->extent_callback_l_;
+  return !dim_cache_[a].extent_callback_l_;
 }
 
 Interval
 Score_element::extent (Axis a) const
 {
-  Dimension_cache const * d = dim_cache_[a];
-  Interval ext = d->get_dim ();
+  Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
+  if (!d->extent_callback_l_)
+    {
+      d->dim_.set_empty ();
+    }
+  else if (!d->valid_b_)
+    {
+      d->dim_= (*d->extent_callback_l_ ) (this, a);
+      d->valid_b_ = true;
+    }
 
+  Interval ext = d->dim_;
+  
   if (empty_b (a)) 
     return ext;
 
-  SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
+  SCM extra = get_elt_property (a == X_AXIS
+                               ? "extra-extent-X"
                                : "extra-extent-Y");
 
   /*
@@ -542,20 +555,37 @@ Score_element::extent (Axis a) const
 Score_element*
 Score_element::parent_l (Axis a) const
 {
-  Dimension_cache*d= dim_cache_[a]->parent_l_;
-  return d ? d->elt_l_ : 0;
+  return  dim_cache_[a].parent_l_;
+}
+
+Score_element * 
+Score_element::common_refpoint (Score_element const* s, Axis a) const
+{
+  /*
+    I don't like the quadratic aspect of this code. Maybe this should
+    be rewritten some time, but the largest chain of parents might be
+    10 high or so, so it shouldn't be a real issue. */
+  for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
+    for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
+      if (d == c)
+       return (Score_element*)d;
+
+  return 0;
 }
 
+
 Score_element *
-Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
+Score_element::common_refpoint (SCM elist, Axis a) const
 {
-  Dimension_cache * common = dim_cache_[a];
-  for (int i=0; i < gs.size (); i++)
+  Score_element * common = (Score_element*) this;
+  for (; gh_pair_p (elist); elist = gh_cdr (elist))
     {
-      common = common->common_refpoint (gs[i]->dim_cache_[a]);
+      Score_element * s = unsmob_element (gh_car (elist));
+      if (s)
+       common = common->common_refpoint (s, a);
     }
 
-  return common->element_l ();
+  return common;
 }
 
 char const *
@@ -565,17 +595,23 @@ Score_element::name () const
 }
 
 void
-Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
+Score_element::add_offset_callback (Offset_callback cb, Axis a)
 {
-  dim_cache_[a]->off_callbacks_.push (cb);
+  dim_cache_[a].off_callbacks_.push (cb);
 }
 
 bool
-Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
+Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
 {
-  for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
+  return cb == dim_cache_[a].extent_callback_l_;
+}
+
+bool
+Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
+{
+  for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
     {
-      if (dim_cache_[a]->off_callbacks_[i] == cb)
+      if (dim_cache_[a].off_callbacks_[i] == cb)
        return true;
     }
   return false;
@@ -584,14 +620,14 @@ Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
 void
 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
 {
-  dim_cache_[a]->extent_callback_l_ = dc ;
+  dim_cache_[a].extent_callback_l_ = dc ;
 }
 
                                    
 void
 Score_element::set_parent (Score_element *g, Axis a)
 {
-  dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
+  dim_cache_[a].parent_l_ = g;
 }
 
 void
@@ -609,10 +645,6 @@ Score_element::fixup_refpoint ()
        {
          Score_element * newparent = parent->find_broken_piece (line_l ());
          set_parent (newparent, ax);
-         if (!newparent)
-           {
-             programming_error ("Orphaned score-element.");
-           }
        }
 
       if (Item * i  = dynamic_cast<Item*> (this))
@@ -624,7 +656,7 @@ Score_element::fixup_refpoint ()
              Direction  my_dir = i->break_status_dir () ;
              if (my_dir!= parenti->break_status_dir())
                {
-                 Item *newparent =  parenti->find_broken_piece (my_dir);
+                 Item *newparent =  parenti->find_prebroken_piece (my_dir);
                  set_parent (newparent, ax);
                }
            }
@@ -638,11 +670,11 @@ Score_element::fixup_refpoint ()
   SMOB funcs
  ****************************************************/
 
-
 #include "ly-smobs.icc"
 
 IMPLEMENT_UNSMOB(Score_element, element);
 IMPLEMENT_SMOBS(Score_element);
+
 SCM
 Score_element::mark_smob (SCM ses)
 {
@@ -741,6 +773,6 @@ init_functions ()
 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
 
 void
-Score_element::do_breakable_col_processing ()
+Score_element::discretionary_processing()
 {
 }