]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/note-column.cc
release: 1.1.41
[lilypond.git] / lily / note-column.cc
index dbeeba4a91c3f52723601e2d8c60fe12adccb41f..26a33eb5cd28c76dab6d2c329f270371eb7d2bcb 100644 (file)
 /*
   note-column.cc -- implement Note_column
 
-  source file of the LilyPond music typesetter
+  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 "debug.hh"
-#include "script.hh"
-#include "notehead.hh"
+#include "beam.hh"
+#include "note-head.hh"
 #include "stem.hh"
+#include "rest.hh"
+#include "debug.hh"
+#include "paper-def.hh"
 
-IMPLEMENT_STATIC_NAME(Note_column);
-
-
-void
-Note_column::add(Stem*stem_l)
+bool
+Note_column::rest_b () const
 {
-    assert(!stem_l_);
-    stem_l_ = stem_l;
-    add_dependency(stem_l);
+  return rest_l_arr_.size ();
 }
 
-void
-Note_column::add(Notehead* n_l)
+Note_column::Note_column()
 {
-    if (head_l_arr_.size())
-       assert(n_l->rest_b_ == rest_b_);
-    else
-       rest_b_ = n_l->rest_b_;
-    
-    head_l_arr_.push(n_l);
-    add_dependency(n_l);
+  set_axes (X_AXIS,X_AXIS);
+  stem_l_ = 0;
 }
 
 void
-Note_column::add(Script*s_l)
+Note_column::sort()
 {
-    script_l_arr_.push(s_l);
-    add_dependency(s_l);
+  head_l_arr_.sort (Note_head::compare);
 }
-
-void
-Note_column::translate(Offset o)
+  
+Interval_t<int>
+Note_column::head_positions_interval() const
 {
-    for (int i=0; i < head_l_arr_.size(); i++)
-       head_l_arr_[i]->translate(o);
-    for (int i=0; i < script_l_arr_.size(); i++) 
-       script_l_arr_[i]->translate(o);
-    if (stem_l_)
-       stem_l_->translate(o);
-}
+  ((Note_column*)this)->sort();
+  Interval_t<int>  iv;
 
+  iv.set_empty ();
 
-void
-Note_column::do_print()const
-{
-    mtor << "heads: " << head_l_arr_.size() << '\n'; 
-    mtor << "scripts: " << script_l_arr_.size() << '\n'; 
+  if (head_l_arr_.size ())
+    iv = Interval_t<int>(head_l_arr_[0]->position_i_, 
+                        head_l_arr_.top()->position_i_);
+  
+  return iv;
 }
 
-Interval
-Note_column::do_height()const return r
+Direction
+Note_column::dir () const
 {
-    if (stem_l_)
-        r.unite(stem_l_->height());
-    for (int i=0; i < head_l_arr_.size(); i++)
-       r.unite(head_l_arr_[i]->height());
-    for (int i=0; i < script_l_arr_.size(); i++) 
-       r.unite(script_l_arr_[i]->height());
+  if (stem_l_)
+    return stem_l_->dir_;
+  else if (head_l_arr_.size ())
+    return sign (head_positions_interval().center ());
+
+  assert (false);
+  return CENTER;
 }
 
-Interval
-Note_column::do_width()const return r;
+
+void
+Note_column::set_stem (Stem * stem_l)
 {
-    if (stem_l_)
-        r.unite(stem_l_->width());
-    for (int i=0; i < head_l_arr_.size(); i++)
-       r.unite(head_l_arr_[i]->width());
-    for (int i=0; i < script_l_arr_.size(); i++) 
-       r.unite(script_l_arr_[i]->width());
+  stem_l_ = stem_l;
+  add_dependency (stem_l);
+  add_element (stem_l);
 }
 
+
 void
-Note_column::do_pre_processing()
+Note_column::do_substitute_element_pointer (Score_element*o, Score_element*n)
 {
-    if (stem_l_ && !dir_i_)
-       dir_i_ = stem_l_->dir_i_;
-    
-    if (!script_l_arr_.size()) 
-       return;
-
-    Array<Script*> placed_l_arr_a[4];
-    for (int i=0; i < script_l_arr_.size(); i++) {
-       Script*s_l = script_l_arr_[i];
-       int j = (s_l->dir_i_ >0) ? 0 : 2;
-       if (!s_l->inside_staff_b_) 
-           j ++;
-       
-       placed_l_arr_a[j].push(s_l);
+  if (stem_l_ == o) 
+    {
+      stem_l_ = n ? dynamic_cast<Stem *> (n):0;
     }
-    for (int j =0; j <4; j++) {
-       placed_l_arr_a[j].sort( Script::compare);
+  if (dynamic_cast<Note_head *> (o))
+    {
+      head_l_arr_.substitute (dynamic_cast<Note_head *> (o), 
+                             (n)? dynamic_cast<Note_head *> (n) : 0);
     }
-    
-    Notehead *top_head_l=0;
-    Notehead *bot_head_l=0;
-    for (int i=0; i< head_l_arr_.size(); i++) {
-       if (head_l_arr_[i]->extremal == -1)
-           bot_head_l = head_l_arr_[i];
-       else if (head_l_arr_[i]->extremal == 1)
-           top_head_l = head_l_arr_[i];
+
+  if (dynamic_cast<Rest *> (o)) 
+    {
+      rest_l_arr_.substitute (dynamic_cast<Rest *> (o), 
+                             (n)? dynamic_cast<Rest *> (n) : 0);
     }
-    /* argh. This sux. */
-    if (!top_head_l) 
-       top_head_l = bot_head_l;
-    if (!bot_head_l) 
-       bot_head_l = top_head_l;
-    assert(bot_head_l && top_head_l);
-    Item *support_l=top_head_l;
-    int j;
-    for (j = 0; j < 2; j++ ) {
-       for (int i=0; i < placed_l_arr_a[j].size(); i++) {
-           placed_l_arr_a[j][i]->add_support(support_l);
-           support_l = placed_l_arr_a[j][i];
-       }
+}
+
+void
+Note_column::add_head (Rhythmic_head *h)
+{
+  if (Rest*r=dynamic_cast<Rest *> (h))
+    {
+      rest_l_arr_.push (r);
     }
-    
-    support_l=bot_head_l;
-    for (; j < 4; j++ ) {
-       for (int i=0; i < placed_l_arr_a[j].size(); i++) {
-           placed_l_arr_a[j][i]->add_support(support_l);
-           support_l = placed_l_arr_a[j][i];
-       }
+  if (Note_head *nh=dynamic_cast<Note_head *> (h))
+    {
+      head_l_arr_.push (nh);
     }
+  add_element (h);
 }
 
-Note_column::Note_column()
+/**
+  translate the rest symbols
+ */
+void
+Note_column::translate_rests (int dy_i)
 {
-    h_shift_b_ =false;
-    stem_l_ =0;
-    rest_b_ = false;
-    dir_i_ =0;
+  invalidate_cache (Y_AXIS);
+  for (int i=0; i < rest_l_arr_.size(); i++)
+    rest_l_arr_[i]->position_i_ += dy_i;
 }
+
 void
-Note_column::sort()
+Note_column::do_print() const
 {
-    head_l_arr_.sort( Notehead::compare);
+#ifndef NPRINT
+  DOUT << "rests: " << rest_l_arr_.size() << ", ";
+  DOUT << "heads: " << head_l_arr_.size();
+#endif
 }
-    
-Interval_t<int>
-Note_column::head_positions_interval()const
+
+void
+Note_column::set_dotcol (Dot_column *d)
 {
-    (    (Note_column*)this)->sort();
-    return Interval_t<int> ( head_l_arr_[0]->position, 
-                            head_l_arr_.top()->position);
+  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)
+    return;
+      
+      /* ugh. Should be done by beam. */
+  Real x = stem_l_->hpos_f ();
+  Direction d = stem_l_->get_dir ();
+  Real beamy = x * b->slope_f_ + b->left_y_;
+  Interval restdim = extent (Y_AXIS);
+
+  Real staff_space = rest_l_arr_[0]->staff_line_leading_f ();      
+  Real internote_f = staff_space/2;
+  Real minimum_dist
+    = paper_l ()->get_var ("restcollision_minimum_beamdist") * internote_f;
+  Real dist =
+    minimum_dist +  -d  * (beamy - restdim[d]) >? 0;
+
+  int stafflines = rest_l_arr_[0]->lines_i ();
+      
+  // move discretely by half spaces.
+  int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
+
+  // 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);
 }