]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/stem.cc
release: 1.3.0
[lilypond.git] / lily / stem.cc
index 9bdfd99f8029390698dcf58c833d425a63296a90..7c7797a385c9c43c4ec3256776b86fe1c9fc8309 100644 (file)
@@ -3,53 +3,61 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 
   TODO: This is way too hairy
 */
 
 #include "stem.hh"
-#include "dimen.hh"
 #include "debug.hh"
 #include "paper-def.hh"
 #include "note-head.hh"
 #include "lookup.hh"
 #include "molecule.hh"
-#include "p-col.hh"
+#include "paper-column.hh"
 #include "misc.hh"
 #include "beam.hh"
 #include "rest.hh"
 
-const int STEMLEN=7;
+void
+Stem::set_direction (Direction d)
+{
+  if  (!dir_)
+    warning (_ ("stem direction set already!"));
 
-IMPLEMENT_IS_TYPE_B1 (Stem,Item);
+  dir_ = d;
 
-Stem::Stem ()
-{
   /*
-    TODO: staff-size
-    */
-  abbrev_flag_i_ = 0;
-  beam_l_ = 0;
-  beams_left_i_ = 0;
-  beams_right_i_ = 0;
+    todo
+  */
+}
 
+Stem::Stem ()
+{
+  beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
   yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
   flag_i_ = 2;
   dir_ = CENTER;
-  stem_xdir_ = LEFT;
-  staff_size_i_ = 8;
-
-  beam_gap_i_ = 0;
+  beam_l_ = 0;
 }
 
 Interval_t<int>
 Stem::head_positions () const
 {
+  /* 
+    Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
+    trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
+
+   */
+  if (!head_l_arr_.size ())
+    {
+      return Interval_t<int> (100,-100);       
+    }
+
   Interval_t<int> r;
   for (int i =0; i < head_l_arr_.size (); i++)
     {
-      int p = head_l_arr_[i]->position_i_;
+      int p = (int)head_l_arr_[i]->position_f ();
       r[BIGGER] = r[BIGGER] >? p;
       r[SMALLER] = r[SMALLER] <? p;
     }
@@ -60,9 +68,9 @@ void
 Stem::do_print () const
 {
 #ifndef NPRINT
-  DOUT << "flag "<< flag_i_ << "abbrev_flag_i_" << abbrev_flag_i_;
+  DEBUG_OUT << "flag "<< flag_i_;
   if (beam_l_)
-    DOUT << "beamed";
+    DEBUG_OUT << "beamed";
 #endif
 }
 
@@ -73,11 +81,17 @@ Stem::stem_length_f () const
 }
 
 Real
-Stem::stem_start_f () const
+Stem::stem_begin_f () const
 {
   return yextent_drul_[Direction(-dir_)];
 }
 
+Real
+Stem::chord_start_f () const
+{
+  return head_positions()[dir_] * staff_line_leading_f ()/2.0;
+}
+
 Real
 Stem::stem_end_f () const
 {
@@ -89,7 +103,7 @@ Stem::set_stemend (Real se)
 {
   // todo: margins
   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
-    warning (_("Weird stem size; check for narrow beams"));
+    warning (_ ("Weird stem size; check for narrow beams"));
 
   
   yextent_drul_[dir_]  =  se;
@@ -103,16 +117,17 @@ Stem::type_i () const
 }
 
 void
-Stem::add (Rhythmic_head *n)
+Stem::add_head (Rhythmic_head *n)
 {
+  n->stem_l_ = this;
   n->add_dependency (this);    // ?
-  if (n->is_type_b (Note_head::static_name ()))
+  if (Note_head *nh = dynamic_cast<Note_head *> (n))
     {
-      head_l_arr_.push ((Note_head*)n);
+      head_l_arr_.push (nh);
     }
-  else if (n->is_type_b (Rest::static_name ()))
+  else if (Rest *r = dynamic_cast<Rest *> (n))
     {
-      rest_l_arr_.push ((Rest*)n);
+      rest_l_arr_.push (r);
     }
 }
 
@@ -124,7 +139,7 @@ Stem::invisible_b () const
 }
 
 int
-Stem::get_center_distance (Direction d)
+Stem::get_center_distance (Direction d) const
 {
   int staff_center = 0;
   int distance = d*(head_positions()[d] - staff_center);
@@ -132,76 +147,74 @@ Stem::get_center_distance (Direction d)
 }
 
 Direction
-Stem::get_default_dir ()
+Stem::get_default_dir () const
 {
-  return (get_center_distance (UP) >=
-         get_center_distance (DOWN)) 
-    ? DOWN 
-    : UP;
-}
+  int du = get_center_distance (UP);
+  int dd = get_center_distance (DOWN);
 
+  if (sign (dd - du))
+    return Direction (sign (dd -du));
 
-void
-Stem::set_default_dir ()
+  return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
+}
+
+Direction
+Stem::get_dir () const
 {
-  dir_ = get_default_dir ();
+  return dir_;
 }
 
+
 void
 Stem::set_default_stemlen ()
 {
-  Real len = STEMLEN;
-  Real dy = paper ()->interbeam_f ();
+  Real length_f = 0.;
+  SCM scm_len = get_elt_property(length_scm_sym);
+  if (scm_len != SCM_BOOL_F)
+    {
+      length_f = gh_scm2double (SCM_CDR(scm_len));
+    }
+  else
+    length_f = paper_l ()->get_var ("stem_length0");
 
-  // ugh, should get nice *rule* for this
-  if (abbrev_flag_i_ > 1)
-    len += (abbrev_flag_i_ - 1)* dy / 2;
+  bool grace_b = get_elt_property (grace_scm_sym) != SCM_BOOL_F;
+  String type_str = grace_b ? "grace_" : "";
 
+  Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
 
   if (!dir_)
-    set_default_dir ();
+    dir_ = get_default_dir ();
 
-  /* If the stem points in the "wrong" direction, it should be
-     shortened, according to [Roush & Gourlay].  Their suggestion to knock off
-     a whole staffspace is a bit drastical though.
-     */
-  else if (dir_ != get_default_dir ())
-    len  -= 1.0;
+  /* 
+    stems in unnatural (forced) direction should be shortened, 
+    according to [Roush & Gourlay]
+   */
+  if (((int)chord_start_f ())
+      && (dir_ != get_default_dir ()))
+    length_f -= shorten_f;
 
   if (flag_i_ >= 5)
-    len += 2.0;
+    length_f += 2.0;
   if (flag_i_ >= 6)
-    len += 1.0;
+    length_f += 1.0;
   
-  set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + len :
-              head_positions()[SMALLER] - len);
-
+  set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
+              head_positions()[SMALLER] - length_f);
 
-  if (dir_ * stem_end_f () < 0)
-    {
-      set_stemend (0);
-    }
+  bool no_extend_b = get_elt_property (no_stem_extend_scm_sym) != SCM_BOOL_F;
+  if (!grace_b && !no_extend_b && (dir_ * stem_end_f () < 0))
+    set_stemend (0);
 }
 
+//xxx
 void
 Stem::set_default_extents ()
 {
   if (!stem_length_f ())
     set_default_stemlen ();
 
-
-  if (dir_ == UP)
-    stem_xdir_ = RIGHT;
-  if (invisible_b ())
-    stem_xdir_ = CENTER;
 }
 
-/*
-  TODO
-
-  move into note_column.cc
-
-  */
 void
 Stem::set_noteheads ()
 {
@@ -211,23 +224,26 @@ Stem::set_noteheads ()
   if (dir_ < 0)
     head_l_arr_.reverse ();
 
-  head_l_arr_[0]->extremal_i_ = -1;
-  head_l_arr_.top ()->extremal_i_ = 1;
+  Note_head * beginhead =   head_l_arr_[0];
+  beginhead->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
+  if  (beginhead !=   head_l_arr_.top ())
+    head_l_arr_.top ()->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
+  
   int parity=1;
-  int lastpos = head_l_arr_[0]->position_i_;
+  int lastpos = int (beginhead->position_f ());
   for (int i=1; i < head_l_arr_.size (); i ++)
     {
-      int dy =abs (lastpos- head_l_arr_[i]->position_i_);
+      int dy =abs (lastpos- (int)head_l_arr_[i]->position_f ());
 
       if (dy <= 1)
        {
          if (parity)
-           head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
+           head_l_arr_[i]->flip_around_stem (dir_);
          parity = !parity;
        }
       else
        parity = 1;
-      lastpos = head_l_arr_[i]->position_i_;
+      lastpos = int (head_l_arr_[i]->position_f ());
     }
 }
 
@@ -237,26 +253,73 @@ Stem::do_pre_processing ()
   if (yextent_drul_[DOWN]== yextent_drul_[UP])
     set_default_extents ();
   set_noteheads ();
-  flag_i_ = flag_i_;
-  transparent_b_ = invisible_b ();
-  set_empty (invisible_b ());
+
+  if (invisible_b ())
+    {
+      set_elt_property (transparent_scm_sym, SCM_BOOL_T);
+    }
+  set_empty (invisible_b (), X_AXIS, Y_AXIS);
+  set_spacing_hints ();
 }
 
 
+
+/**
+   set stem directions for hinting the optical spacing correction.
+
+   Modifies DIR_LIST property of the Stem's Score_column
+
+   TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
+ */
+
+void
+Stem::set_spacing_hints () 
+{
+  if (!invisible_b ())
+    {
+      SCM scmdir  = gh_int2scm (dir_);
+      SCM dirlist = column_l ()->get_elt_property (dir_list_scm_sym);
+      if (dirlist == SCM_BOOL_F)
+       dirlist = SCM_EOL;
+      else
+       dirlist = SCM_CDR (dirlist);
+
+      if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
+       {
+         dirlist = gh_cons (scmdir, dirlist);
+         column_l ()->set_elt_property (dir_list_scm_sym, dirlist);
+       }
+    }
+}
+
+Molecule
+Stem::flag () const
+{
+  String style;
+  SCM st = get_elt_property (style_scm_sym);
+  if ( st != SCM_BOOL_F)
+    {
+      st = SCM_CDR(st);
+      style = ly_scm2string (st);
+    }
+
+  char c = (dir_ == UP) ? 'u' : 'd';
+  Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
+                                     to_str (flag_i_));
+  if (!style.empty_b ())
+    m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
+  return m;
+}
+
 Interval
 Stem::do_width () const
 {
   Interval r (0, 0);
-  if (abbrev_flag_i_)
-    {
-      r = abbrev_mol ().extent ().x ();
-    }
-  else if (beam_l_ || abs (flag_i_) <= 2)
+  if (beam_l_ || abs (flag_i_) <= 2)
     ;  // TODO!
   else
     {
-      Paper_def*p= paper ();
-      r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
+      r = flag ().dim_.x ();
       r += note_delta_f ();
     }
   return r;
@@ -264,78 +327,36 @@ Stem::do_width () const
 
 
 
-Molecule
-Stem::abbrev_mol () const
-{
-  Real dy = paper ()->interbeam_f ();
-  Real w = 1.5 * paper ()->lookup_l ()->ball (2).dim_.x ().length ();
-  Real beamdy = paper ()->interline_f () / 2;
-
-  int beams_i = 0;
-  Real slope_f = paper ()->internote_f () / 4;
-
-  if (beam_l_) {
-    // huh?
-      slope_f = 2 * beam_l_->slope_f;
-    // ugh, rather calc from Abbreviation_req
-      beams_i = beams_right_i_ >? beams_left_i_;
-  }
-  paper ()->lookup_l ()->beam (slope_f, 20 PT);
-
-  Molecule beams;
-  Atom a (paper ()->lookup_l ()->beam (slope_f, w));
-  a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope_f)));
-  
-  // ugh
-  if (!beams_i)
-    a.translate_axis (dy + beamdy - dir_ * dy, Y_AXIS);
-  else
-    a.translate_axis (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS);
-
-  for (int i = 0; i < abbrev_flag_i_; i++)
-    {
-      Atom b (a);
-      b.translate_axis (-dir_ * dy * (beams_i + i), Y_AXIS);
-      beams.add (b);
-    }
-
-  return beams;
-}
 
 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
 
 Molecule*
-Stem::brew_molecule_p () const
+Stem::do_brew_molecule_p () const
 {
   Molecule *mol_p =new Molecule;
-  Paper_def *p =paper ();
   Drul_array<Real> stem_y = yextent_drul_;
-  Real dy = p->internote_f ();
-  
+  Real dy = staff_line_leading_f ()/2.0;
 
   Real head_wid = 0;
   if (head_l_arr_.size ())
-    head_wid = head_l_arr_[0]->width ().length ();
+    head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
   
   if (!invisible_b ())
     {
-      Atom ss =p->lookup_l ()->stem (stem_y[DOWN]*dy,
-                                    stem_y[UP]*dy);
-      mol_p->add (Atom (ss));
+      Real stem_width = paper_l ()->get_var ("stemthickness");
+      Molecule ss =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);
     }
 
-  if (!beam_l_ &&abs (flag_i_) > 2)
+  if (!beam_l_ && abs (flag_i_) > 2)
     {
-      Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
+      Molecule fl = flag ();
       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
-      mol_p->add(fl);
-      assert (!abbrev_flag_i_);
+      mol_p->add_molecule (fl);
     }
 
-  if (abbrev_flag_i_)
-    mol_p->add (abbrev_mol ());
-
   if (head_l_arr_.size())
     {
       mol_p->translate_axis (note_delta_f (), X_AXIS);
@@ -347,29 +368,37 @@ Real
 Stem::note_delta_f () const
 {
   Real r=0;
-  if (head_l_arr_.size() && stem_xdir_ != CENTER)
+  if (head_l_arr_.size())
     {
-      Interval head_wid(0,  head_l_arr_[0]->width ().length ());
-      Real rule_thick(paper ()->rule_thickness ());
+      Interval head_wid(0,  head_l_arr_[0]->extent (X_AXIS).length ());
+         Real rule_thick = paper_l ()->get_var ("stemthickness");
+
       Interval stem_wid(-rule_thick/2, rule_thick/2);
-      r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
+      if (dir_ == CENTER)
+       r = head_wid.center ();
+      else
+       r = head_wid[dir_] - stem_wid[dir_];
     }
   return r;
 }
+
 Real
 Stem::hpos_f () const
 {
-  return note_delta_f () +Item::hpos_f ();
+  return note_delta_f () + Item::hpos_f ();
 }
 
-/*
-  TODO:  head_l_arr_/rest_l_arr_ in  do_substitute_dependent ()
- */
 void
- Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
+Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
 {
-  Item * o_l = o->item ();
-  Item * n_l = n? n->item () : 0;
-  head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
-  rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);
+  if (Note_head*h=dynamic_cast<Note_head*> (o))
+    head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
+  if (Rest *r=dynamic_cast<Rest*> (o))
+    rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
+  if (Beam* b = dynamic_cast<Beam*> (o))
+    {
+      if (b == beam_l_) 
+       beam_l_ = dynamic_cast<Beam*> (n);
+    }
+  Staff_symbol_referencer::do_substitute_element_pointer (o,n);
 }