]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/stem.cc
release: 1.3.83
[lilypond.git] / lily / stem.cc
index 232caf5aed7ac5caddd77103835aa5f983d725f4..b64f89bafacbf81b40311789b1855ab428700ef2 100644 (file)
@@ -3,17 +3,19 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
     Jan Nieuwenhuizen <janneke@gnu.org>
 
   TODO: This is way too hairy
 */
+#include <math.h>              // m_pi
 
-#include "dimension-cache.hh"
+#include "directional-element-interface.hh"
+#include "note-head.hh"
 #include "stem.hh"
 #include "debug.hh"
 #include "paper-def.hh"
-#include "note-head.hh"
+#include "rhythmic-head.hh"
 #include "lookup.hh"
 #include "molecule.hh"
 #include "paper-column.hh"
 #include "group-interface.hh"
 #include "cross-staff.hh"
 #include "staff-symbol-referencer.hh"
+#include "spanner.hh"
 
 
 void
-Stem::set_beaming (int i,  Direction d )
+Stem::set_beaming (Score_element*me ,int i,  Direction d )
 {
-  SCM pair = get_elt_property ("beaming");
+  SCM pair = me->get_elt_property ("beaming");
   
   if (!gh_pair_p (pair))
     {
       pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
-      set_elt_property ("beaming", pair);
+      me->      set_elt_property ("beaming", pair);
     }
   index_set_cell (pair, d, gh_int2scm (i));
 }
 
 int
-Stem::beam_count (Direction d) const
+Stem::beam_count (Score_element*me,Direction d)
 {
-  SCM p=get_elt_property ("beaming");
+  SCM p=me->get_elt_property ("beaming");
   if (gh_pair_p (p))
     return gh_scm2int (index_cell (p,d));
   else
     return 0;
 }
 
-Interval_t<int>
-Stem::head_positions () const
+Interval
+Stem::head_positions (Score_element*me) 
 {
-  /* 
-    Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
-    trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
-
-   */
-  if (!first_head ())
+  if (!heads_i (me))
     {
-      return Interval_t<int> (100,-100);       
+      Interval iv;
+      return iv;
     }
 
-  Link_array<Note_head> head_l_arr =
-    Group_interface__extract_elements (this, (Note_head*)0, "heads");
-  
-  Interval_t<int> r;
-  for (int i =0; i < head_l_arr.size (); i++)
-    {
-      Staff_symbol_referencer_interface si (head_l_arr[i]);
-      int p = (int)si.position_f ();
-      r[BIGGER] = r[BIGGER] >? p;
-      r[SMALLER] = r[SMALLER] <? p;
-    }
-  return r;
+  Drul_array<Score_element*> e (extremal_heads (me));
+
+  return Interval (Staff_symbol_referencer::position_f (e[DOWN]),
+                  Staff_symbol_referencer::position_f ( e[UP]));
 }
 
+
 Real
-Stem::stem_begin_f () const
+Stem::chord_start_f (Score_element*me) 
 {
-  return yextent_[Direction(-get_direction ())];
+  return head_positions(me)[get_direction (me)]
+    * Staff_symbol_referencer::staff_space (me)/2.0;
 }
 
 Real
-Stem::chord_start_f () const
+Stem::stem_end_position (Score_element*me) 
 {
-  return head_positions()[get_direction ()]
-    * Staff_symbol_referencer_interface (this).staff_line_leading_f ()/2.0;
+  SCM p =me->get_elt_property ("stem-end-position");
+  Real pos;
+  if (!gh_number_p (p))
+    {
+
+      pos = get_default_stem_end_position (me);
+      me->set_elt_property ("stem-end-position", gh_double2scm (pos));
+    }
+  else
+    pos = gh_scm2double (p);
+
+  return pos;
 }
 
-Real
-Stem::stem_end_f () const
+Direction
+Stem::get_direction (Score_element*me)
 {
-  return yextent_[get_direction ()];
+  Direction d = Directional_element_interface::get (me);
+
+  if (!d)
+    {
+       d = get_default_dir (me);
+       // urg, AAARGH!
+       Directional_element_interface::set (me, d);
+    }
+  return d ;
 }
 
+
 void
-Stem::set_stemend (Real se)
+Stem::set_stemend (Score_element*me, Real se)
 {
   // todo: margins
-  if (get_direction () && get_direction () * head_positions()[get_direction ()] >= se*get_direction ())
+  Direction d= get_direction (me);
+  
+  if (d && d * head_positions(me)[get_direction (me)] >= se*d)
     warning (_ ("Weird stem size; check for narrow beams"));
 
-  
-  yextent_[get_direction ()]  =  se;
-  yextent_[Direction(-get_direction ())] = head_positions()[-get_direction ()];
+  me->set_elt_property ("stem-end-position", gh_double2scm (se));
 }
 
 int
-Stem::type_i () const
+Stem::type_i (Score_element*me) 
 {
-  return first_head () ?  first_head ()->balltype_i () : 2;
+  return first_head (me) ?  Rhythmic_head::balltype_i (first_head (me)) : 2;
 }
 
-
-
 /*
   Note head that determines hshift for upstems
  */ 
 Score_element*
-Stem::support_head ()const
+Stem::support_head (Score_element*me)
 {
-  SCM h = get_elt_property ("support-head");
+  SCM h = me->get_elt_property ("support-head");
   Score_element * nh = unsmob_element (h);
   if (nh)
     return nh;
+  else if (heads_i (me) == 1)
+    {
+      /*
+       UGH.
+       */
+      
+      return unsmob_element (gh_car (me->get_elt_property ("heads")));
+    }
   else
-    return first_head ();
+    return first_head (me);
 }
 
 
+int
+Stem::heads_i (Score_element*me)
+{
+  Pointer_group_interface gi (me, "heads");
+  return gi.count ();
+}
+
 /*
   The note head which forms one end of the stem.  
  */
-Note_head*
-Stem::first_head () const
+Score_element*
+Stem::first_head (Score_element*me)
+{
+  return extremal_heads (me)[-get_direction (me)];
+}
+
+/*
+  START is part where stem reaches `last' head. 
+ */
+Drul_array<Score_element*>
+Stem::extremal_heads (Score_element*me) 
 {
   const int inf = 1000000;
-  int pos = -inf;              
-  Direction dir = get_direction ();
+  Drul_array<int> extpos;
+  extpos[DOWN] = inf;
+  extpos[UP] = -inf;  
+  
+  Drul_array<Score_element *> exthead;
+  exthead[LEFT] = exthead[RIGHT] =0;
+  
+  for (SCM s = me->get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
+    {
+      Score_element * n = unsmob_element (gh_car (s));
 
+      
+      int p = int(Staff_symbol_referencer::position_f (n));
 
-  Note_head *nh =0;
-  for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
-    {
-      Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
-      Staff_symbol_referencer_interface si (n);
-      int p = dir * int(si.position_f ());
-      if (p > pos)
+      Direction d = LEFT;
+      do {
+      if (d* p > d* extpos[d])
        {
-         nh = n;
+         exthead[d] = n;
+         extpos[d] = p;
        }
+      } while (flip (&d) != DOWN);
     }
 
-  return nh;
+  return exthead;
 }
 
 void
-Stem::add_head (Rhythmic_head *n)
+Stem::add_head (Score_element*me, Score_element *n)
 {
-  n->set_elt_property ("stem", this->self_scm_);
-  n->add_dependency (this);    // ?
-  
+  n->set_elt_property ("stem", me->self_scm ());
+  n->add_dependency (me);
 
-  Group_interface gi (this);
-  if (Note_head *nh = dynamic_cast<Note_head *> (n))
-    gi.name_ = "heads";
+  if (Note_head::has_interface (n))
+    {
+      Pointer_group_interface (me, "heads").add_element (n);
+    }
   else
-    gi.name_ = "rests";
-
-  gi.add_element (n);
-}
-
-Stem::Stem ()
-{
-  set_elt_property ("heads", SCM_EOL);
-  set_elt_property ("rests", SCM_EOL);
+    {
+      n->set_elt_property ("rest", n->self_scm ());
+    }
 }
 
 bool
-Stem::invisible_b () const
+Stem::invisible_b (Score_element*me)
 {
-  return !(first_head () && first_head()->balltype_i () >= 1);
+  return !(heads_i (me) && Rhythmic_head::balltype_i (support_head (me)) >= 1);
 }
 
 int
-Stem::get_center_distance (Direction d) const
+Stem::get_center_distance (Score_element*me, Direction d)
 {
   int staff_center = 0;
-  int distance = d*(head_positions()[d] - staff_center);
+  int distance = (int) (d*(head_positions(me)[d] - staff_center));
   return distance >? 0;
 }
 
 Direction
-Stem::get_default_dir () const
+Stem::get_default_dir (Score_element*me) 
 {
-  int du = get_center_distance (UP);
-  int dd = get_center_distance (DOWN);
+  int du = get_center_distance (me,UP);
+  int dd = get_center_distance (me,DOWN);
 
   if (sign (dd - du))
     return Direction (sign (dd -du));
 
-  return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
+  return to_dir (me->get_elt_property ("default-neutral-direction"));
 }
 
-void
-Stem::set_default_stemlen ()
+/*
+  ugh. A is used for different purposes. This functionality should be
+  moved into scheme at some point to get rid of the silly
+  conversions. (but lets wait till we have namespaces in SCM)
+ */
+Real
+Stem::get_default_stem_end_position (Score_element*me) 
 {
+  bool grace_b = to_boolean (me->get_elt_property ("grace"));
+  String type_str = grace_b ? "grace-" : "";
+  SCM s;
+  Array<Real> a;
+
   Real length_f = 0.;
-  SCM scm_len = get_elt_property("length");
-  if (scm_len != SCM_UNDEFINED)
+  SCM scm_len = me->get_elt_property("length");
+  if (gh_number_p (scm_len))
     {
       length_f = gh_scm2double (scm_len);
     }
   else
-    length_f = paper_l ()->get_var ("stem_length0");
+    {
+      s = scm_eval2 (ly_symbol2scm ((type_str + "stem-length").ch_C()),
+                    SCM_EOL);
+      for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+       a.push (gh_scm2double (gh_car (q)));
+               
+      // stem uses half-spaces
+      length_f = a[((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
+    }
+
 
-  bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
-  String type_str = grace_b ? "grace_" : "";
+  a.clear ();
+  s = scm_eval2 (ly_symbol2scm ((type_str + "stem-shorten").ch_C()),
+                SCM_EOL);
+  for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+    a.push (gh_scm2double (gh_car (q)));
 
-  Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
+
+  // stem uses half-spaces
+
+  // fixme: use gh_list_ref () iso. array[]
+  Real shorten_f = a[((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
 
   /* URGURGURG
      'set-default-stemlen' sets direction too
-   */ 
-  if (!get_direction ())
-    set_direction (get_default_dir ());
-
+   */
+  Direction dir = get_direction (me);
+  if (!dir)
+    {
+      dir = get_default_dir (me);
+      Directional_element_interface::set (me, dir);
+    }
+  
   /* 
     stems in unnatural (forced) direction should be shortened, 
     according to [Roush & Gourlay]
    */
-  if (((int)chord_start_f ())
-      && (get_direction () != get_default_dir ()))
+  if (((int)chord_start_f (me))
+      && (get_direction (me) != get_default_dir (me)))
     length_f -= shorten_f;
- if (flag_i () >= 5)
-    length_f += 2.0;
-  if (flag_i () >= 6)
-    length_f += 1.0;
+
+
+   Real st = head_positions(me)[dir] + dir * length_f;
   
-  set_stemend ((get_direction () > 0) ? head_positions()[BIGGER] + length_f:
-              head_positions()[SMALLER] - length_f);
+   bool no_extend_b = to_boolean (me->get_elt_property ("no-stem-extend"));
+    if (!grace_b && !no_extend_b && dir * st < 0)
+      st = 0.0;
 
-  bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
-  if (!grace_b && !no_extend_b && (get_direction () * stem_end_f () < 0))
-    set_stemend (0);
+  return st;
 }
 
+/*
+  FIXME: wrong name
+ */
 int
-Stem::flag_i () const
+Stem::flag_i (Score_element*me) 
 {
-  SCM s = get_elt_property ("duration-log");
+  SCM s = me->get_elt_property ("duration-log");
   return  (gh_number_p (s)) ? gh_scm2int (s) : 2;
 }
 
-//xxx
-void
-Stem::set_default_extents ()
-{
-  if (yextent_.empty_b ())
-    set_default_stemlen ();
-}
-
 void
-Stem::set_noteheads ()
+Stem::position_noteheads (Score_element*me)
 {
-  if (!first_head ())
+  if (!heads_i (me))
     return;
   
   Link_array<Score_element> heads =
-    Group_interface__extract_elements (this, (Score_element*)0, "heads");
+    Pointer_group_interface__extract_elements (me, (Score_element*)0, "heads");
 
   heads.sort (compare_position);
-  Direction dir =get_direction ();
+  Direction dir =get_direction (me);
   
   if (dir < 0)
     heads.reverse ();
 
 
-  Real w = support_head ()->extent (X_AXIS)[dir];
+  Real w = support_head (me)->extent (X_AXIS)[dir];
   for (int i=0; i < heads.size (); i++)
     {
       heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
     }
   
-  bool parity= true;
-  int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
+  bool parity= true;           // todo: make me settable.
+  int lastpos = int (Staff_symbol_referencer::position_f (heads[0]));
   for (int i=1; i < heads.size (); i ++)
     {
-      Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
+      Real p = Staff_symbol_referencer::position_f (heads[i]);
       int dy =abs (lastpos- (int)p);
 
       if (dy <= 1)
@@ -296,7 +354,7 @@ Stem::set_noteheads ()
          if (parity)
            {
              Real l  = heads[i]->extent (X_AXIS).length ();
-             heads[i]->translate_axis (l * get_direction (), X_AXIS);
+             heads[i]->translate_axis (l * get_direction (me), X_AXIS);
            }
          parity = !parity;
        }
@@ -307,21 +365,22 @@ Stem::set_noteheads ()
     }
 }
 
-void
-Stem::do_pre_processing ()
+MAKE_SCHEME_CALLBACK(Stem,before_line_breaking);
+SCM
+Stem::before_line_breaking (SCM smob)
 {
-  if (yextent_.empty_b ())
-    set_default_extents ();
-  set_noteheads ();
+  Score_element*me = unsmob_element (smob);
+  stem_end_position (me);      // ugh. Trigger direction calc.
+  position_noteheads (me);
 
-  if (invisible_b ())
+  if (invisible_b (me))
     {
-      set_elt_property ("transparent", SCM_BOOL_T);
-      set_empty (Y_AXIS);      
-      set_empty (X_AXIS);      
+      me->remove_elt_property ("molecule-callback");
+      // suicide();
     }
-
-  set_spacing_hints ();
+  
+  set_spacing_hints (me);
+  return SCM_UNSPECIFIED;
 }
 
 
@@ -329,181 +388,195 @@ Stem::do_pre_processing ()
 /**
    set stem directions for hinting the optical spacing correction.
 
-   Modifies DIR_LIST property of the Stem's Score_column
+   Modifies DIR_LIST property of the Stem's Paper_column
 
    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
  */
-
 void
-Stem::set_spacing_hints () 
+Stem::set_spacing_hints (Score_element*me
 {
-  if (!invisible_b ())
+  if (!invisible_b (me))
     {
-      SCM scmdir  = gh_int2scm (get_direction ());
-      SCM dirlist = column_l ()->get_elt_property ("dir-list");
+      SCM scmdir  = gh_int2scm (get_direction (me));
+
+      Item* item = dynamic_cast<Item*> (me);
+      Item * col =  item->column_l ();
+      SCM dirlist =col->get_elt_property ("dir-list");
       if (dirlist == SCM_UNDEFINED)
        dirlist = SCM_EOL;
 
       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
        {
          dirlist = gh_cons (scmdir, dirlist);
-         column_l ()->set_elt_property ("dir-list", dirlist);
+         col->set_elt_property ("dir-list", dirlist);
        }
     }
 }
 
 Molecule
-Stem::flag () const
+Stem::flag (Score_element*me)
 {
   String style;
-  SCM st = get_elt_property ("style");
-  if ( st != SCM_UNDEFINED)
+  SCM st = me->get_elt_property ("flag-style");
+  if ( gh_string_p (st))
     {
       style = ly_scm2string (st);
     }
 
-  char c = (get_direction () == UP) ? 'u' : 'd';
-  Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
-                                     to_str (flag_i ()));
+  char c = (get_direction (me) == UP) ? 'u' : 'd';
+  Molecule m = me->lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
+                                     to_str (flag_i (me)));
   if (!style.empty_b ())
-    m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
+    m.add_molecule(me->lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
   return m;
 }
 
 Interval
-Stem::dim_callback (Dimension_cache const* c
+Stem::dim_callback (Score_element *se, Axis 
 {
-  Stem * s = dynamic_cast<Stem*> (c->element_l ());
-  
   Interval r (0, 0);
-  if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
+  if (unsmob_element (se->get_elt_property ("beam")) || abs (flag_i (se)) <= 2)
     ;  // TODO!
   else
     {
-      r = s->flag ().dim_.x ();
-      r += s->note_delta_f ();
+      r = flag (se).extent (X_AXIS);
     }
   return r;
 }
 
 
-const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
+const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Should be settable.
 
-Molecule*
-Stem::do_brew_molecule_p () const
+
+MAKE_SCHEME_CALLBACK(Stem,brew_molecule);
+
+SCM
+Stem::brew_molecule (SCM smob) 
 {
-  Molecule *mol_p =new Molecule;
-  Interval stem_y = yextent_;
-  Real dy = staff_symbol_referencer_interface (this)
-    .staff_line_leading_f ()/2.0;
+  Score_element*me = unsmob_element (smob);
+  Molecule mol;
+  Direction d = get_direction (me);
+  
+  
+  Real y1 = Staff_symbol_referencer::position_f (first_head (me));
+  Real y2 = stem_end_position (me);
+  
+  Interval stem_y(y1,y2);
+  stem_y.unite (Interval (y2,y1));
 
+  Real dy = Staff_symbol_referencer::staff_space (me)/2.0;
   Real head_wid = 0;
-  if (support_head ())
-    head_wid = support_head ()->extent (X_AXIS).length ();
-  stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
+  if (support_head (me))
+    head_wid = support_head (me)->extent (X_AXIS).length ();
+  stem_y[Direction(-d)] += d * head_wid * tan(ANGLE)/(2*dy);
   
-  if (!invisible_b ())
+  if (!invisible_b (me))
     {
-      Real stem_width = paper_l ()->get_var ("stemthickness");
-      Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
+      Real stem_width = me->paper_l ()->get_var ("stemthickness");
+      Molecule ss =me->lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
                                                 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
-      mol_p->add_molecule (ss);
+      mol.add_molecule (ss);
     }
 
-  if (!beam_l () && abs (flag_i ()) > 2)
+  if (!beam_l (me) && abs (flag_i (me)) > 2)
     {
-      Molecule fl = flag ();
-      fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
-      mol_p->add_molecule (fl);
+      Molecule fl = flag (me);
+      fl.translate_axis(stem_y[d]*dy, Y_AXIS);
+      mol.add_molecule (fl);
     }
 
-  if (first_head ())
-    {
-      mol_p->translate_axis (note_delta_f (), X_AXIS);
-    }
-  return mol_p;
+  return mol.create_scheme();
 }
 
 Real
-Stem::note_delta_f () const
+Stem::off_callback (Score_element * me, Axis)
 {
   Real r=0;
-  if (first_head ())
+  if (Score_element * f = first_head (me))
     {
-      Interval head_wid(0,  first_head()->extent (X_AXIS).length ());
-         Real rule_thick = paper_l ()->get_var ("stemthickness");
+      Interval head_wid(0, f->extent (X_AXIS).length ());
 
-      Interval stem_wid(-rule_thick/2, rule_thick/2);
-      if (get_direction () == CENTER)
-       r = head_wid.center ();
-      else
-       r = head_wid[get_direction ()] - stem_wid[get_direction ()];
+      if (to_boolean (me->get_elt_property ("stem-centered")))
+       return head_wid.center ();
+      
+      Real rule_thick = me->paper_l ()->get_var ("stemthickness");
+      Direction d = get_direction (me);
+      r = head_wid[d] - d * rule_thick ;
     }
   return r;
 }
 
-Real
-Stem::hpos_f () const
-{
-  return note_delta_f () + Item::hpos_f ();
-}
 
 
-Beam*
-Stem::beam_l ()const
+Score_element*
+Stem::beam_l (Score_element*me)
 {
-  SCM b=  get_elt_property ("beam");
-  return dynamic_cast<Beam*> (unsmob_element (b));
+  SCM b=  me->get_elt_property ("beam");
+  return unsmob_element (b);
 }
 
 
 // ugh still very long.
 Stem_info
-Stem::calc_stem_info () const
+Stem::calc_stem_info (Score_element*me) 
 {
-  assert (beam_l ());
+  Score_element * beam = beam_l (me);
 
-  Direction beam_dir = beam_l ()->get_direction ();
+  Direction beam_dir = Directional_element_interface::get (beam);
   if (!beam_dir)
     {
       programming_error ("Beam dir not set.");
       beam_dir = UP;
     }
     
+
+  Real staff_space = Staff_symbol_referencer::staff_space (me);
+  Real half_space = staff_space / 2;
+  Real interbeam_f = me->paper_l ()->interbeam_f (Beam::get_multiplicity (beam));
+  Real thick = gh_scm2double (beam->get_elt_property ("beam-thickness"));
+  int multiplicity = Beam::get_multiplicity (beam);
+
   Stem_info info; 
-  Real internote_f
-     = staff_symbol_referencer_interface (this).staff_line_leading_f ()/2;
-   Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
-  Real beam_f = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
-         
-  info.idealy_f_ = chord_start_f ();
+  info.idealy_f_ = chord_start_f (me);
 
   // for simplicity, we calculate as if dir == UP
   info.idealy_f_ *= beam_dir;
+  SCM grace_prop = me->get_elt_property ("grace");
+
+  bool grace_b = to_boolean (grace_prop);
+  
+  Array<Real> a;
+  SCM s;
+  String type_str = grace_b ? "grace-" : "";
+  
+  s = scm_eval2 (ly_symbol2scm ((type_str + "beamed-stem-minimum-length").ch_C()), SCM_EOL);
+  a.clear ();
+  for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+    a.push (gh_scm2double (gh_car (q)));
 
-  bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
-  bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
 
-  int stem_max = (int)rint(paper_l ()->get_var ("stem_max"));
-  String type_str = grace_b ? "grace_" : "";
-  Real min_stem_f = paper_l ()->get_var (type_str + "minimum_stem_length"
-    + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
-  Real stem_f = paper_l ()->get_var (type_str + "stem_length"
-    + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
+  Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
+  s = scm_eval2 (ly_symbol2scm ((type_str + "beamed-stem-length").ch_C()),
+                SCM_EOL);
 
-  if (!beam_dir || (beam_dir == get_direction ()))
+  a.clear();
+  for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+    a.push (gh_scm2double (gh_car (q)));
+
+  Real stem_length =  a[multiplicity <? (a.size () - 1)] * staff_space;
+
+  if (!beam_dir || (beam_dir == Directional_element_interface::get (me)))
     /* normal beamed stem */
     {
-      if (beam_l ()->get_multiplicity ())
+      if (multiplicity)
        {
-         info.idealy_f_ += beam_f;
-         info.idealy_f_ += (beam_l ()->get_multiplicity () - 1) * interbeam_f;
+         info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
        }
       info.miny_f_ = info.idealy_f_;
       info.maxy_f_ = INT_MAX;
 
-      info.idealy_f_ += stem_f;
-      info.miny_f_ += min_stem_f;
+      info.idealy_f_ += stem_length;
+      info.miny_f_ += minimum_length;
 
       /*
        lowest beam of (UP) beam must never be lower than second staffline
@@ -515,39 +588,56 @@ Stem::calc_stem_info () const
        than middle staffline, just as normal stems.
        
       */
+      bool no_extend_b = to_boolean (me->get_elt_property ("no-stem-extend"));
       if (!grace_b && !no_extend_b)
        {
-         //highest beam of (UP) beam must never be lower than middle staffline
-         info.miny_f_ = info.miny_f_ >? 0;
-         //lowest beam of (UP) beam must never be lower than second staffline
-         info.miny_f_ = info.miny_f_ >? (- 2 * internote_f - beam_f
-                               + (beam_l ()->get_multiplicity () > 0) * beam_f + interbeam_f * (beam_l ()->get_multiplicity () - 1));
+         /* highest beam of (UP) beam must never be lower than middle
+            staffline
+            lowest beam of (UP) beam must never be lower than second staffline
+          */
+         info.miny_f_ =
+           info.miny_f_ >? 0
+           >? (- 2 * half_space - thick
+               + (multiplicity > 0) * thick
+               + interbeam_f * (multiplicity - 1));
        }
     }
   else
     /* knee */
     {
-      info.idealy_f_ -= beam_f;
+      info.idealy_f_ -= thick;
       info.maxy_f_ = info.idealy_f_;
       info.miny_f_ = -INT_MAX;
 
-      info.idealy_f_ -= stem_f;
-      info.maxy_f_ -= min_stem_f;
+      info.idealy_f_ -= stem_length;
+      info.maxy_f_ -= minimum_length;
     }
+  
+  info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
 
-  info.idealy_f_ = info.maxy_f_ <? info.idealy_f_;
-  info.idealy_f_ = info.miny_f_ >? info.idealy_f_;
-
-  Real interstaff_f = calc_interstaff_dist (this, beam_l ());
+  s = beam->get_elt_property ("shorten");
+  if (gh_number_p (s))
+    info.idealy_f_ -= gh_scm2double (s);
 
-  SCM s = beam_l ()->get_elt_property ("shorten");
-  if (s != SCM_UNDEFINED)
-    info.idealy_f_ -= gh_double2scm (s);
+  Real interstaff_f = -beam_dir* calc_interstaff_dist (dynamic_cast<Item*> (me), dynamic_cast<Spanner*> (beam));
 
-  info.idealy_f_ += interstaff_f * beam_dir;
-  info.miny_f_ += interstaff_f * beam_dir;
-  info.maxy_f_ += interstaff_f * beam_dir;
+  info.idealy_f_ += interstaff_f;
+  info.miny_f_ += interstaff_f;
+  info.maxy_f_ += interstaff_f ;
 
   return info;
 }
 
+bool
+Stem::has_interface (Score_element*m)
+{
+  return m && m->has_interface (ly_symbol2scm ("stem-interface"));
+}
+
+void
+Stem::set_interface (Score_element*me)
+{    
+  me->set_elt_property ("heads", SCM_EOL);
+  me->add_offset_callback ( &Stem::off_callback, X_AXIS);
+  me->set_interface (ly_symbol2scm ("stem-interface"));
+}