]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/note-column.cc
release: 1.3.10
[lilypond.git] / lily / note-column.cc
index 19cf429928ccd33c9aded260de7c238bf5a24851..21740f4513ccd9a02707d047e05ad84760a362bc 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
-
+#include "dot-column.hh"
 #include "note-column.hh"
-#include "script.hh"
+#include "beam.hh"
 #include "note-head.hh"
 #include "stem.hh"
+#include "rest.hh"
+#include "debug.hh"
+#include "paper-def.hh"
+#include "group-interface.hh"
 
+bool
+Note_column::rest_b () const
+{
+  SCM r = get_elt_property ("rests");
 
-IMPLEMENT_IS_TYPE_B1(Note_column,Head_column);
+  return gh_pair_p (r);
+}
 
-void
-Note_column::set (Stem*s)
+int
+Note_column::shift_compare (Note_column *const &p1, Note_column*const&p2)
 {
-  stem_l_ = s;
-  add_support (s);
+  SCM s1 = p1->get_elt_property ("horizontal-shift");
+  SCM s2 = p2->get_elt_property ("horizontal-shift");
+
+  int h1 = (s1 == SCM_UNDEFINED) ? 0 : gh_scm2int (s1);
+  int h2 = (s2 == SCM_UNDEFINED) ? 0 : gh_scm2int (s2);
+  return h1 - h2;
 }
 
 Note_column::Note_column()
 {
-  h_shift_b_ =false;
+  set_elt_property ("rests", SCM_EOL);
+  set_elt_property ("note-heads", SCM_EOL);  
+  set_axes (X_AXIS, Y_AXIS);
 }
 
-void
-Note_column::sort()
+Stem *
+Note_column::stem_l () const
 {
-  head_l_arr_.sort (Note_head::compare);
+  SCM s = get_elt_property ("stem");
+  return dynamic_cast<Stem*> (unsmob_element (s));
+
 }
+
   
-Interval_t<int>
-Note_column::head_positions_interval()const
+Slice
+Note_column::head_positions_interval() const
 {
-  ((Note_column*)this)->sort();
-  return Interval_t<int> ( head_l_arr_[0]->position_i_, 
-                            head_l_arr_.top()->position_i_);
+  Slice  iv;
 
+  iv.set_empty ();
+
+  SCM h = get_elt_property ("note-heads");
+  for (; gh_pair_p (h); h = gh_cdr (h))
+    {
+      Score_element *se = unsmob_element (gh_car (h));
+      
+      int j = int (dynamic_cast<Staff_symbol_referencer*> (se)->position_f ());
+      iv.unite (Slice (j,j));
+    }
+  return iv;
+}
+
+Direction
+Note_column::dir () const
+{
+  if (stem_l ())
+    return stem_l ()->get_direction ();
+  else if (gh_pair_p (get_elt_property ("note-heads")))
+    return (Direction)sign (head_positions_interval().center ());
+
+  programming_error ("Note column without heads and stem!");
+  return CENTER;
 }
 
 
 void
-Note_column::do_pre_processing()
+Note_column::set_stem (Stem * stem_l)
 {
-  if (!dir_i_&& !stem_l_)
+  set_elt_property ("stem", stem_l->self_scm_);
+
+  add_dependency (stem_l);
+  add_element (stem_l);
+}
+
+
+
+void
+Note_column::add_head (Rhythmic_head *h)
+{
+  if (Rest*r=dynamic_cast<Rest *> (h))
+    {
+      Group_interface gi (this, "rests");
+      gi.add_element (h);
+    }
+  if (Note_head *nh=dynamic_cast<Note_head *> (h))
+    {
+      Group_interface gi (this, "note-heads");
+      gi.add_element (nh);
+    }
+  add_element (h);
+}
+
+/**
+  translate the rest symbols vertically by amount DY_I.
+ */
+void
+Note_column::translate_rests (int dy_i)
+{
+  invalidate_cache (Y_AXIS);
+
+  SCM s = get_elt_property ("rests");
+  for (; gh_pair_p (s); s = gh_cdr (s))
     {
-       dir_i_ = (head_positions_interval().center () >=  5) ? -1 : 1;
+      Score_element * se = unsmob_element ( gh_car (s));
+      Staff_symbol_referencer *str = dynamic_cast<Staff_symbol_referencer*> (se);
+      se->translate_axis (dy_i * str->staff_line_leading_f ()/2.0, Y_AXIS);
     }
-  Head_column::do_pre_processing();
 }
 
+
+void
+Note_column::set_dotcol (Dot_column *d)
+{
+  add_element (d);
+}
+
+/*
+  [TODO]
+  handle rest under beam (do_post: beams are calculated now)
+  what about combination of collisions and rest under beam.
+
+  Should lookup
+    
+    rest -> stem -> beam -> interpolate_y_position ()
+    
+*/
+
+void
+Note_column::do_post_processing ()
+{
+  if (!stem_l () || !rest_b ())
+    return;
+
+  Beam * b = stem_l ()->beam_l ();
+  if (!b || !b->stem_count ())
+    return;
   
+  /* ugh. Should be done by beam. */
+  Direction d = stem_l ()->get_direction ();
+  Real beamy = (stem_l ()->hpos_f () - b->stem(0)->hpos_f ()) * b->slope_f_ + b->left_y_;
+
+  SCM s = get_elt_property ("rests");
+  Score_element * se = unsmob_element (gh_car (s));
+  Staff_symbol_referencer *str = dynamic_cast<Staff_symbol_referencer*> (se);
+
+  Real staff_space = str->staff_line_leading_f ();      
+  Real rest_dim = extent (Y_AXIS)[d]*2.0  /staff_space ;
+
+  Real minimum_dist
+    = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
+  Real dist =
+    minimum_dist +  -d  * (beamy - rest_dim) >? 0;
+
+  int stafflines = str->lines_i ();
+
+  // move discretely by half spaces.
+  int discrete_dist = int (ceil (dist ));
+
+  // move by whole spaces inside the staff.
+  if (discrete_dist < stafflines+1)
+    discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
+
+  translate_rests (-d *  discrete_dist);
+}
+
+
+Interval
+Note_column::rest_dim () const
+{
+  Interval restdim;
+  SCM s = get_elt_property ("rests");
+  for (; gh_pair_p (s); s = gh_cdr (s))
+    {
+      Score_element * sc = unsmob_element ( gh_car (s));
+      restdim.unite (sc->extent (Y_AXIS));
+    }
+  
+  return restdim;
+}
+
+Note_head*
+Note_column::first_head () const
+{
+  Stem * st = stem_l ();
+  return st?  st->first_head (): 0; 
+
+}