]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/score-element.cc
patch::: 1.3.96.jcn9
[lilypond.git] / lily / score-element.cc
index 992d2eee1d770802fd7a836a9e90a0d759176675..4a4c59d60da5972de8767809233786472562ac12 100644 (file)
@@ -3,13 +3,18 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
 
 #include <string.h>
+#include <math.h>
 
-#include "p-score.hh"
+#include "input-smob.hh"
+#include "libc-extension.hh"
+#include "group-interface.hh"
+#include "misc.hh"
+#include "paper-score.hh"
 #include "paper-def.hh"
 #include "lookup.hh"
 #include "molecule.hh"
 #include "spanner.hh"
 #include "line-of-score.hh"
 #include "item.hh"
-#include "p-col.hh"
+#include "paper-column.hh"
 #include "molecule.hh"
 #include "misc.hh"
 #include "paper-outputter.hh"
+#include "dimension-cache.hh"
+#include "side-position-interface.hh"
+#include "item.hh"
 
-Interval
-Score_element::dim_cache_callback (Dimension_cache*c)
-{
-  Score_element *  e =dynamic_cast<Score_element*>( c->element_l());
-  if(&e->dim_cache_[X_AXIS] == c)
-    return e->do_width ();
-  else
-    return e->do_height ();
-}
+#include "ly-smobs.icc"
+
+/*
+TODO:
+
+remove dynamic_cast<Spanner,Item> and put this code into respective
+  subclass.
+*/
 
-Score_element::Score_element()
+
+#define INFINITY_MSG "Infinity or NaN encountered"
+
+Score_element::Score_element(SCM basicprops)
 {
-  output_p_ =0;
-  dim_cache_[X_AXIS].set_callback (dim_cache_callback);
-  dim_cache_[Y_AXIS].set_callback (dim_cache_callback); 
-  used_b_ = false;
+  /*
+    fixme: default should be no callback.
+   */
+
   pscore_l_=0;
   lookup_l_ =0;
   status_i_ = 0;
   original_l_ = 0;
-  element_property_alist_ = SCM_EOL;
+  immutable_property_alist_ =  basicprops;
+  mutable_property_alist_ = SCM_EOL;
+
+  smobify_self ();
+
+  char const*onames[] = {"X-offset-callbacks", "Y-offset-callbacks"};
+  char const*enames[] = {"X-extent-callback", "Y-extent-callback"};
+  
+  for (int a = X_AXIS; a <= Y_AXIS; a++){
+    SCM l = get_elt_property (onames[a]);
+
+    if (scm_ilength (l) >=0)
+      {
+       dim_cache_[a].offset_callbacks_ = l;
+       dim_cache_[a].offsets_left_ = scm_ilength (l);
+      }
+    else
+      {
+       programming_error ("[XY]-offset-callbacks must be a list");
+      }
+
+    SCM cb = get_elt_property (enames[a]);
+
+    /*
+      Should change default to be empty? 
+     */
+    if (!gh_procedure_p (cb) && !gh_pair_p (cb))
+      cb = molecule_extent_proc;
+    
+    dim_cache_[a].dimension_ = cb;
+  }
+
+  SCM meta = get_elt_property ("meta");
+  SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
+  
+  set_elt_property ("interfaces",gh_cdr (ifs));
 }
 
+
 Score_element::Score_element (Score_element const&s)
-  : Graphical_element (s)
+   : dim_cache_ (s.dim_cache_)
 {
-  used_b_ = true;
   original_l_ =(Score_element*) &s;
-  element_property_alist_ = scm_list_copy (s.element_property_alist_);
-  dependency_arr_ = s.dependency_arr_;
-  output_p_ =0;
+  immutable_property_alist_ = s.immutable_property_alist_;
+  mutable_property_alist_ = SCM_EOL;
+  
   status_i_ = s.status_i_;
   lookup_l_ = s.lookup_l_;
   pscore_l_ = s.pscore_l_;
+
+  smobify_self ();
 }
 
 Score_element::~Score_element()
 {
-  element_property_alist_ = SCM_EOL; // try to be nice to GC.
-  delete output_p_; 
-  assert (status_i_ >=0);
-  status_i_  = -1;
+  /*
+    do nothing scm-ish and no unprotecting here.
+   */
 }
 
-Score_element*
-Score_element::dependency (int i) const
-{
-  return dependency_arr_ [i];
-}
 
-int
-Score_element::dependency_size () const
+SCM
+Score_element::get_elt_property (const char *nm) const
 {
-  return dependency_arr_.size ();
+  SCM sym = ly_symbol2scm (nm);
+  return get_elt_property (sym);
 }
 
-
-
 SCM
 Score_element::get_elt_property (SCM sym) const
 {
-  SCM s =  scm_assq(sym, element_property_alist_);
+  SCM s = scm_sloppy_assq(sym, mutable_property_alist_);
+  if (s != SCM_BOOL_F)
+    return gh_cdr (s);
 
-  // is this a good idea?
-  if (s == SCM_BOOL_F && pscore_l_ && pscore_l_->paper_l_)
-    s = pscore_l_->paper_l_->get_scm_var (sym);
-
-  return s;
+  s = scm_sloppy_assq (sym, immutable_property_alist_);
+  return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s); 
 }
 
+/*
+  Remove the value associated with KEY, and return it. The result is
+  that a next call will yield SCM_UNDEFINED (and not the underlying
+  `basic' property.
+*/
 SCM
-Score_element::remove_elt_property (SCM key)
+Score_element::remove_elt_property (const char* key)
 {
-  SCM s = get_elt_property (key); 
-  element_property_alist_ =  scm_assq_remove_x (element_property_alist_, key);
-  return s;
+  SCM val = get_elt_property (key);
+  if (val != SCM_EOL)
+    set_elt_property (key, SCM_EOL);
+  return val;
 }
 
 void
-Score_element::set_elt_property (SCM s, SCM v)
+Score_element::set_elt_property (const char* k, SCM v)
 {
-  element_property_alist_ =
-    scm_assoc_set_x (element_property_alist_, s, v);
+  SCM s = ly_symbol2scm (k);
+  set_elt_property (s, v);
 }
 
-Interval
-Score_element::do_width() const 
+/*
+  Puts the k, v in the immutable_property_alist_, which is convenient for
+  storing variables that are needed during the breaking process. (eg.
+  Line_of_score::rank : int )
+ */
+void
+Score_element::set_immutable_elt_property (const char*k, SCM v)
 {
-  Interval r;
-
-  Molecule*m = output_p_ ?  output_p_ : do_brew_molecule_p();
-  r = m->extent().x ();
+  SCM s = ly_symbol2scm (k);
+  set_immutable_elt_property (s, v);
+}
 
-  if (!output_p_)
-    delete m;
-  
-  return r;
+void
+Score_element::set_immutable_elt_property (SCM s, SCM v)
+{
+  immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
+  mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
+}
+void
+Score_element::set_elt_property (SCM s, SCM v)
+{
+  mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
 }
 
-Interval
-Score_element::do_height() const 
+
+MAKE_SCHEME_CALLBACK(Score_element,molecule_extent,2);
+SCM
+Score_element::molecule_extent (SCM element_smob, SCM scm_axis)
 {
-  Interval r;
-  Molecule*m = output_p_ ?  output_p_ : do_brew_molecule_p();
-  r = m->extent().y ();
-  if (!output_p_)
-    delete m;
+  Score_element *s = unsmob_element (element_smob);
+  Axis a = (Axis) gh_scm2int (scm_axis);
 
-  return r;
+  Molecule m = s->get_molecule ();
+  return ly_interval2scm ( m.extent(a));
 }
 
-void
-Score_element::print() const
-{
-#ifndef NPRINT
-  DOUT << classname(this) << "{\n";
-  DOUT << "dependencies: " << dependency_size();
-  if (original_l_)
-    DOUT << "Copy ";
-  Graphical_element::do_print ();
-  do_print();
+MAKE_SCHEME_CALLBACK(Score_element,preset_extent,2);
+
+SCM
+Score_element::preset_extent (SCM element_smob, SCM scm_axis)
+{
+  Score_element *s = unsmob_element (element_smob);
+  Axis a = (Axis) gh_scm2int (scm_axis);
+
+  SCM ext = s->get_elt_property ((a == X_AXIS)
+                                ? "extent-X"
+                                : "extent-Y");
+  
+  if (gh_pair_p (ext))
+    {
+      Real l = gh_scm2double (gh_car (ext));
+      Real r = gh_scm2double (gh_cdr (ext));
+      l *= s->paper_l ()->get_var ("staffspace");
+      r *= s->paper_l ()->get_var ("staffspace");
+      return ly_interval2scm (Interval (l, r));
+    }
   
-  DOUT <<  "}\n";
-#endif
+  return ly_interval2scm ( Interval ());
 }
 
 
+
 Paper_def*
 Score_element::paper_l ()  const
 {
- return pscore_l_->paper_l_;
+ return pscore_l_ ? pscore_l_->paper_l_ : 0;
 }
 
-
 Lookup const *
 Score_element::lookup_l () const
 {
+  /*
+    URG junkthis, caching is clumsy.
+   */
   if (!lookup_l_)
     {
-      Score_element * me = (Score_element*)this;
-      SCM sz = me->remove_elt_property (fontsize_scm_sym);
-      int i = (sz != SCM_BOOL_F)
-       ? gh_scm2int (SCM_CDR (sz))
+      Score_element * urg = (Score_element*)this;
+      SCM sz = urg->remove_elt_property ("font-size");
+      int i = (gh_number_p (sz))
+       ? gh_scm2int  (sz)
        : 0;
 
-      me->lookup_l_ =  pscore_l_->paper_l_->lookup_l (i);
+      urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
     }
   return lookup_l_;
 }
 
 void
-Score_element::add_processing()
-{
-  assert (status_i_ >=0);
-  if (status_i_)
-    return;
-  status_i_ ++;
-  do_add_processing();
-}
-
-
-void
-Score_element::calculate_dependencies (int final, int busy,
-                                   Score_element_method_pointer funcptr)
+Score_element::calculate_dependencies (int final, int busy, SCM funcname)
 {
   assert (status_i_ >=0);
 
   if (status_i_ >= final)
     return;
 
-  assert (status_i_!= busy);
+  if (status_i_== busy)
+    {
+      programming_error ("Element is busy, come back later");
+      return;
+    }
+  
   status_i_= busy;
 
-  for (int i=0; i < dependency_arr_.size(); i++)
-    dependency_arr_[i]->calculate_dependencies (final, busy, funcptr);
+  for (SCM d=  get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
+    {
+      unsmob_element (gh_car (d))
+       ->calculate_dependencies (final, busy, funcname);
+    }
 
-  Link_array<Score_element> extra (get_extra_dependencies());
-  for (int i=0; i < extra.size(); i++)
-    extra[i]->calculate_dependencies (final, busy, funcptr);
+  // ughugh.
+  String s = ly_symbol2string (funcname);
+  SCM proc = get_elt_property (s.ch_C());
+  if (gh_procedure_p (proc))
+    gh_call1 (proc, this->self_scm ());
   
-  invalidate_cache (X_AXIS);
-  invalidate_cache (Y_AXIS);
-  (this->*funcptr)();
   status_i_= final;
+
 }
 
-void
-Score_element::output_processing () 
+Molecule
+Score_element::get_molecule ()  const
 {
-  if (get_elt_property (transparent_scm_sym) != SCM_BOOL_F)
-    return;
+  SCM proc = get_elt_property ("molecule-callback");
+
+  SCM mol = SCM_EOL;
+  if (gh_procedure_p (proc)) 
+    mol = gh_apply (proc, gh_list (this->self_scm (), SCM_UNDEFINED));
 
-  // we're being silly here. 
-  if (output_p_)
-    delete output_p_;
+    
+  SCM origin =get_elt_property ("origin");
+  if (!unsmob_input (origin))
+    origin =ly_symbol2scm ("no-origin");
   
-  output_p_ = do_brew_molecule_p ();
-  pscore_l_->outputter_l_->output_molecule (output_p_,
-                                           absolute_offset (),
-                                           classname(this));
+  if (gh_pair_p (mol))
+    {
+      // ugr.
+       mol = gh_cons (gh_list (origin, gh_car (mol), SCM_UNDEFINED), gh_cdr (mol));
+    }
 
-  pscore_l_->schedule_for_delete (this);
-}
 
+  Molecule m (create_molecule (mol));
+
+  /*
+    This is almost the same as setting molecule-callback to #f, but
+    this retains the dimensions of this element, which means that you
+    can erase elements individually.  */
+  if (to_boolean (get_elt_property ("transparent")))
+    m = Molecule (m.extent_box (), SCM_EOL);
+
+  return m;
+}
 
 
 /*
@@ -234,205 +311,608 @@ Score_element::output_processing ()
   VIRTUAL STUBS
 
  */
-
 void
 Score_element::do_break_processing()
 {
-  handle_broken_dependencies();
 }
 
-void
-Score_element::do_post_processing()
+
+
+
+
+MAKE_SCHEME_CALLBACK(Score_element,brew_molecule,1)
+
+/*
+  ugh.
+ */  
+SCM
+Score_element::brew_molecule (SCM smob) 
 {
+  Score_element * sc = unsmob_element (smob);
+  SCM glyph = sc->get_elt_property ("glyph");
+  if (gh_string_p (glyph))
+    {
+      return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
+    }
+  else
+    {
+      return SCM_EOL;
+    }
 }
 
-void
-Score_element::do_breakable_col_processing()
+
+Line_of_score *
+Score_element::line_l() const
 {
-  handle_prebroken_dependencies();
+  return 0;
 }
 
 void
-Score_element::do_pre_processing()
+Score_element::add_dependency (Score_element*e)
 {
+  if (e)
+    {
+      Pointer_group_interface ::add_element (this, "dependencies",e);
+
+    }
+  else
+    programming_error ("Null dependency added");
+}
+
+
+
+
+/**
+      Do break substitution in S, using CRITERION. Return new value.
+      CRITERION is either a SMOB pointer to the desired line, or a number
+      representing the break direction. Do not modify SRC.
+*/
+SCM
+Score_element::handle_broken_smobs (SCM src, SCM criterion)
+{
+ again:
+  Score_element *sc = unsmob_element (src);
+  if (sc)
+    {
+      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_prebroken_piece (d);
+             return  (br) ? br->self_scm () : SCM_UNDEFINED;
+           }
+       }
+      else
+       {
+         Line_of_score * line
+           = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
+         if (sc->line_l () != line)
+           {
+             sc = sc->find_broken_piece (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 newcar = handle_broken_smobs (oldcar, criterion);
+      SCM oldcdr = gh_cdr (src);
+      
+      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.  Without
+           tail-recursion, this easily crashes with a stack overflow.  */
+         src =  oldcdr;
+         goto again;
+       }
+
+      SCM newcdr = handle_broken_smobs (oldcdr, criterion);
+      return gh_cons (newcar, newcdr);
+    }
+  else
+    return src;
+
+  return src;
 }
 
 void
-Score_element::do_space_processing ()
+Score_element::handle_broken_dependencies()
 {
+  Spanner * s= dynamic_cast<Spanner*> (this);
+  if (original_l_ && s)
+    return;
+
+  if (s)
+    {
+      for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
+       {
+         Score_element * sc = s->broken_into_l_arr_[i];
+         Line_of_score * l = sc->line_l ();
+         sc->mutable_property_alist_ =
+           handle_broken_smobs (mutable_property_alist_,
+                                l ? l->self_scm () : SCM_UNDEFINED);
+       }
+    }
+
+
+  Line_of_score *line = line_l();
+
+  if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
+    {
+      mutable_property_alist_
+       = handle_broken_smobs (mutable_property_alist_,
+                              line ? line->self_scm () : SCM_UNDEFINED);
+    }
+  else if (dynamic_cast <Line_of_score*> (this))
+    {
+      mutable_property_alist_ = handle_broken_smobs (mutable_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();
+    }
 }
 
+/*
+ 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::do_add_processing()
+Score_element::suicide ()
 {
+  mutable_property_alist_ = SCM_EOL;
+  immutable_property_alist_ = SCM_EOL;
+
+  set_extent_callback (SCM_EOL, Y_AXIS);
+  set_extent_callback (SCM_EOL, X_AXIS);
+
+  for (int a= X_AXIS; a <= Y_AXIS; a++)
+    {
+      dim_cache_[a].offset_callbacks_ = SCM_EOL;
+      dim_cache_[a].offsets_left_ = 0;
+    }
 }
 
 void
-Score_element::do_substitute_element_pointer (Score_element*,Score_element*)
+Score_element::handle_prebroken_dependencies()
+{
+}
+
+Score_element*
+Score_element::find_broken_piece (Line_of_score*) const
 {
+  return 0;
 }
 
+void
+Score_element::translate_axis (Real y, Axis a)
+{
+  if (isinf (y) || isnan (y))
+    programming_error (_(INFINITY_MSG));
+  else
+    {
+      dim_cache_[a].offset_ += y;
+    }
+}  
 
-Molecule*
-Score_element::do_brew_molecule_p() const
+Real
+Score_element::relative_coordinate (Score_element const*refp, Axis a) const
 {
-  Molecule a (lookup_l ()->fill (Box (Interval (0,0), Interval (0,0))));
-  return new Molecule (a);
+  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);
 }
 
+Real
+Score_element::get_offset (Axis a) const
+{
+  Score_element *me = (Score_element*) this;
+  while (dim_cache_[a].offsets_left_)
+    {
+      int l = --me->dim_cache_[a].offsets_left_;
+      SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_,  gh_int2scm (l));
+      SCM retval = gh_call2 (cb, self_scm (), gh_int2scm (a));
 
-Line_of_score *
-Score_element::line_l() const
+      Real r =  gh_scm2double (retval);
+      if (isinf (r) || isnan (r))
+       {
+         programming_error (INFINITY_MSG);
+         r = 0.0;
+       }
+      me->dim_cache_[a].offset_ +=r;
+    }
+  return dim_cache_[a].offset_;
+}
+
+
+MAKE_SCHEME_CALLBACK(Score_element,point_dimension_callback,2);
+SCM
+Score_element::point_dimension_callback (SCM , SCM )
 {
-  return 0;
+  return ly_interval2scm ( Interval (0,0));
+}
+
+bool
+Score_element::empty_b (Axis a)const
+{
+  return ! (gh_pair_p (dim_cache_[a].dimension_ ) ||
+           gh_procedure_p (dim_cache_[a].dimension_ ));
 }
 
 /*
+  TODO: add
+
+    Score_element *refpoint
+
+  to arguments?
+ */
+Interval
+Score_element::extent (Score_element * refp, Axis a) const
+{
+  Real x = relative_coordinate (refp, a);
+
+  
+  Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
+  Interval ext ;   
+  if (gh_pair_p (d->dimension_))
+    ;
+  else if (gh_procedure_p (d->dimension_))
+    {
+      /*
+       FIXME: add doco on types, and should typecheck maybe? 
+       */
+      d->dimension_= gh_call2 (d->dimension_, self_scm(), gh_int2scm (a));
+    }
+  else
+    return ext;
+
+  if (!gh_pair_p (d->dimension_))
+    return ext;
   
-  DEPENDENCIES
+  ext = ly_scm2interval (d->dimension_);
 
-  */
+  SCM extra = get_elt_property (a == X_AXIS
+                               ? "extra-extent-X"
+                               : "extra-extent-Y");
 
-void
-Score_element::remove_dependency (Score_element*e)
+  /*
+    signs ?
+   */
+  Real s = paper_l ()->get_var ("staffspace");
+  if (gh_pair_p (extra))
+    {
+      ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
+      ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
+    }
+  
+  extra = get_elt_property (a == X_AXIS
+                               ? "minimum-extent-X"
+                               : "minimum-extent-Y");
+  if (gh_pair_p (extra))
+    {
+      ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
+                          s * gh_scm2double (gh_cdr (extra))));
+    }
+
+  ext.translate (x);
+  
+  return ext;
+}
+
+
+Score_element*
+Score_element::parent_l (Axis a) const
 {
-  int i;
-  while ((i = dependency_arr_.find_i (e)) >=0 )
-    dependency_arr_.unordered_del (i);
+  return  dim_cache_[a].parent_l_;
+}
 
-  substitute_dependency (e, 0);
+Score_element * 
+Score_element::common_refpoint (Score_element const* s, Axis a) const
+{
+  /*
+    I don't like the quadratic aspect of this code, but I see no other
+    way. 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;
 }
 
-void
-Score_element::add_dependency (Score_element*e)
+
+Score_element *
+Score_element::common_refpoint (SCM elist, Axis a) const
 {
-  if (e)
+  Score_element * common = (Score_element*) this;
+  for (; gh_pair_p (elist); elist = gh_cdr (elist))
     {
-      dependency_arr_.push (e);
-      e->used_b_ = true;
+      Score_element * s = unsmob_element (gh_car (elist));
+      if (s)
+       common = common->common_refpoint (s, a);
     }
-  else
-    warning("Null dependency added");
-      
+
+  return common;
+}
+
+String
+Score_element::name () const
+{
+  SCM meta = get_elt_property ("meta");
+  SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
+  nm =  (gh_pair_p (nm)) ? gh_cdr (nm) : SCM_EOL;
+  return  gh_string_p (nm) ?ly_scm2string (nm) :  classname (this);  
+}
+
+void
+Score_element::add_offset_callback (SCM cb, Axis a)
+{
+  if (!has_offset_callback_b (cb, a))
+  {
+    dim_cache_[a].offset_callbacks_ = gh_cons (cb, dim_cache_[a].offset_callbacks_ );
+    dim_cache_[a].offsets_left_ ++;
+  }
+}
+
+bool
+Score_element::has_extent_callback_b (SCM cb, Axis a)const
+{
+  return scm_equal_p (cb, dim_cache_[a].dimension_);
+}
+
+
+bool
+Score_element::has_extent_callback_b (Axis a) const
+{
+  return gh_procedure_p (dim_cache_[a].dimension_);
+}
+
+bool
+Score_element::has_offset_callback_b (SCM cb, Axis a)const
+{
+  return scm_memq (cb, dim_cache_[a].offset_callbacks_) != SCM_BOOL_F;
 }
+
 void
-Score_element::substitute_dependency (Score_element* old, Score_element* new_l)
+Score_element::set_extent_callback (SCM dc, Axis a)
 {
-  do_substitute_element_pointer (old,new_l);
-  old->do_substitute_element_pointer (this, 0);
+  dim_cache_[a].dimension_ =dc;
 }
 
 void
-Score_element::handle_broken_dependencies()
+Score_element::set_parent (Score_element *g, Axis a)
 {
-  Line_of_score *line  = line_l();
-  if (!line)
-    return;
+  dim_cache_[a].parent_l_ = g;
+}
 
-  Link_array<Score_element> remove_us_arr;
-  for (int i=0; i < dependency_size(); i++) 
+MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint,1);
+SCM
+Score_element::fixup_refpoint (SCM smob)
+{
+  Score_element *me = unsmob_element (smob);
+  for (int a = X_AXIS; a < NO_AXES; a ++)
     {
-      Score_element * elt = dependency (i);
-      if (elt->line_l() != line)
+      Axis ax = (Axis)a;
+      Score_element * parent = me->parent_l (ax);
+
+      if (!parent)
+       continue;
+      
+      if (parent->line_l () != me->line_l () && me->line_l ())
        {
-         if (Spanner *sp = dynamic_cast<Spanner *> (elt)) 
-           {
-             Spanner * broken = sp->find_broken_piece (line);
-             substitute_dependency (sp, broken);
+         Score_element * newparent = parent->find_broken_piece (me->line_l ());
+         me->set_parent (newparent, ax);
+       }
 
-             if (broken)
-               add_dependency (broken);
-           }
-         else if (Item *original = dynamic_cast <Item *> (elt))
+      if (Item * i  = dynamic_cast<Item*> (me))
+       {
+         Item *parenti = dynamic_cast<Item*> (parent);
+
+         if (parenti && i)
            {
-             Item * my_item = original->find_prebroken_piece (line);
-               
-             substitute_dependency (elt, my_item);
-             if (my_item)
-               add_dependency (my_item);
+             Direction  my_dir = i->break_status_dir () ;
+             if (my_dir!= parenti->break_status_dir())
+               {
+                 Item *newparent =  parenti->find_prebroken_piece (my_dir);
+                 me->set_parent (newparent, ax);
+               }
            }
-         remove_us_arr.push (elt);
        }
     }
-
-  remove_us_arr.default_sort();
-  remove_us_arr.uniq();
-  for (int i=0;  i <remove_us_arr.size(); i++)
-    remove_dependency (remove_us_arr[i]);
+  return smob;
 }
 
-/*
-  This sux.
 
-  unlike with spanners, the number of items can increase
 
-  span: item1
+/****************************************************
+  SMOB funcs
+ ****************************************************/
 
-  becomes
 
-  span: item1 item2 item3
+IMPLEMENT_UNSMOB(Score_element, element);
+IMPLEMENT_SMOBS(Score_element);
+IMPLEMENT_DEFAULT_EQUAL_P(Score_element);
 
-  How to let span (a derived class) know that this happened?
- */
-void
-Score_element::handle_prebroken_dependencies()
+SCM
+Score_element::mark_smob (SCM ses)
 {
-  /*  dynamic_cast<Item*> (this) && 
-  if (!break_status_dir ())
-    return;
-  */
-  Link_array<Score_element> old_arr, new_arr;
+  Score_element * s = (Score_element*) SCM_CELL_WORD_1(ses);
+  scm_gc_mark (s->immutable_property_alist_);
+  scm_gc_mark (s->mutable_property_alist_);
+
+  for (int a =0 ; a < 2; a++)
+    {
+      scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
+      scm_gc_mark (s->dim_cache_[a].dimension_);
+    }
   
-  for (int i=0; i < dependency_size(); i++) 
+  if (s->parent_l (Y_AXIS))
+    scm_gc_mark (s->parent_l (Y_AXIS)->self_scm ());
+  if (s->parent_l (X_AXIS))
+    scm_gc_mark (s->parent_l (X_AXIS)->self_scm ());
+
+  if (s->original_l_)
+    scm_gc_mark (s->original_l_->self_scm ());
+  return s->do_derived_mark ();
+}
+
+int
+Score_element::print_smob (SCM s, SCM port, scm_print_state *)
+{
+  Score_element *sc = (Score_element *) gh_cdr (s);
+     
+  scm_puts ("#<Score_element ", port);
+  scm_puts ((char *)sc->name ().ch_C(), port);
+
+  /*
+    don't try to print properties, that is too much hassle.
+   */
+  scm_puts (" >", port);
+  return 1;
+}
+
+SCM
+Score_element::do_derived_mark ()
+{
+  return SCM_EOL;
+}
+
+
+SCM
+ly_set_elt_property (SCM elt, SCM sym, SCM val)
+{
+  Score_element * sc = unsmob_element (elt);
+
+  if (!gh_symbol_p (sym))
+    {
+      error ("Not a symbol");
+      ly_display_scm (sym);
+      return SCM_UNSPECIFIED;
+    }
+
+  if (sc)
     {
-      Score_element * elt = dependency (i);
-      Item *it_l = dynamic_cast <Item *> (elt);
-      if (it_l && it_l->broken_original_b ())
-       if (Item *me = dynamic_cast<Item*> (this) )
-         {
-           Score_element *new_l = it_l->find_prebroken_piece (me->break_status_dir ());
-           if (new_l != elt) 
-             {
-               new_arr.push (new_l);
-               old_arr.push (elt);
-             }
-         }
-       else 
-         {
-           Direction d = LEFT;
-           do {
-             old_arr.push (0);
-             new_arr.push (it_l->find_prebroken_piece (d));
-           } while (flip(&d)!= LEFT);
-         }
+      sc->set_elt_property (sym, val);
     }
+  else
+    {
+      error ("Not a score element");
+      ly_display_scm (elt);
+    }
+
+  return SCM_UNSPECIFIED;
+}
+
+
+SCM
+ly_get_elt_property (SCM elt, SCM sym)
+{
+  Score_element * sc = unsmob_element (elt);
   
-  for (int i=0;  i < old_arr.size(); i++)
-    if (old_arr[i])
-      substitute_dependency (old_arr[i], new_arr[i]);
+  if (sc)
+    {
+      return sc->get_elt_property (sym);
+    }
+  else
+    {
+      error ("Not a score element");
+      ly_display_scm (elt);
+    }
+  return SCM_UNSPECIFIED;
 }
 
+
 void
-Score_element::handle_prebroken_dependents()
+Score_element::discretionary_processing()
 {
 }
 
 
 
-Link_array<Score_element>
-Score_element::get_extra_dependencies() const
+SCM
+spanner_get_bound (SCM slur, SCM dir)
 {
-  Link_array<Score_element> empty;
-  return empty;
+  return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm ();
+}
+
+
+
+static SCM interfaces_sym;
+static void
+init_functions ()
+{
+  interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
+
+  scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (Scheme_function_unknown)ly_get_elt_property);
+  scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (Scheme_function_unknown)ly_set_elt_property);
+  scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (Scheme_function_unknown) spanner_get_bound);
 }
 
 bool
-Score_element::linked_b() const
+Score_element::has_interface (SCM k)
 {
-  return used_b_;
+  SCM ifs = get_elt_property (interfaces_sym);
+
+  return scm_memq (k, ifs) != SCM_BOOL_F;
 }
 
 void
-Score_element::do_print () const
+Score_element::set_interface (SCM k)
 {
+  if (has_interface (k))
+    return ;
+  else
+    {
+      set_elt_property (interfaces_sym,
+                       gh_cons  (k, get_elt_property (interfaces_sym)));
+    }
 }
+
+
+ADD_SCM_INIT_FUNC(scoreelt, init_functions);
+IMPLEMENT_TYPE_P(Score_element, "ly-element?");