]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/rest-collision.cc
release: 1.3.50
[lilypond.git] / lily / rest-collision.cc
index 0b065747bb058f301a988e5fc00354e0ddead3f7..df842f8f33cbdf5c273f687f9df7fd14adbd2617 100644 (file)
@@ -3,8 +3,10 @@
 
   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 <math.h>              // ceil.
+
 #include "beam.hh"
 #include "debug.hh"
 #include "rest-collision.hh"
 #include "collision.hh"
 #include "paper-def.hh"
 #include "rest.hh"
+#include "group-interface.hh"
+#include "staff-symbol-referencer.hh"
+#include "duration.hh"
 
 void
 Rest_collision::add_column (Note_column *nc_l)
 {
   add_dependency (nc_l);
+  Group_interface gi (this);  
   if (nc_l->rest_b ())
-    rest_l_arr_.push (nc_l);
+    gi.name_ = "rests";
   else
-    ncol_l_arr_.push (nc_l);
+    gi.name_ = "notes";
+  
+  gi.add_element (nc_l);
+}
+
+static Duration
+to_duration (int type, int dots)
+{
+  Duration d;
+  d.durlog_i_ = type;
+  d.dots_i_ = dots;
+  return d;
+}
+
+static Moment
+rhythmic_head2mom (Rhythmic_head* r)
+{
+  return to_duration (r->balltype_i (), r->dot_count ()).length_mom ();
+}
+
+static Rhythmic_head*
+col2rhythmic_head (Note_column* c)
+{
+  SCM s = c->get_elt_property ("rests");
+  assert (gh_pair_p (s));
+  Score_element* e = unsmob_element (gh_car (s));
+  return dynamic_cast<Rhythmic_head*> (e);
 }
 
 void
-Rest_collision::do_pre_processing()
+Rest_collision::before_line_breaking ()
 {
+  Link_array<Note_column> rest_l_arr =
+    Group_interface__extract_elements (this, (Note_column*) 0, "rests");
+  Link_array<Note_column> ncol_l_arr =
+    Group_interface__extract_elements (this, (Note_column*) 0, "notes");
+                                     
+  
   /* 
      handle rest-rest and rest-note collisions
 
@@ -38,60 +76,91 @@ Rest_collision::do_pre_processing()
    */
 
   // no rests to collide
-  if (!rest_l_arr_.size())
+  if (!rest_l_arr.size())
     return;
 
   // no partners to collide with
-  if (rest_l_arr_.size() + ncol_l_arr_.size () < 2)
+  if (rest_l_arr.size() + ncol_l_arr.size () < 2)
     return;
 
   // meisjes met meisjes
-  if (!ncol_l_arr_.size()) 
+  if (!ncol_l_arr.size()) 
     {
+      Moment m = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[0]));
+      int i = 1;
+      for (; i < rest_l_arr.size (); i++)
+       {
+         Moment me = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[i]));
+         if (me != m)
+           break;
+       }
+
+      /*
+       If all durations are the same, we'll check if there are more
+       rests than maximum-rest-count.
+       Otherwise (different durations), we'll try to display them all
+       (urg: all 3 of them, currently).
+       */
+      int display_count;
+      SCM s = get_elt_property ("maximum-rest-count");
+      if (i == rest_l_arr.size ()
+         && gh_number_p (s) && gh_scm2int (s) < rest_l_arr.size ())
+       {
+         display_count = gh_scm2int (s);
+         for (; i > display_count; i--)
+           col2rhythmic_head (rest_l_arr[i-1])
+             ->set_elt_property ("transparent", SCM_BOOL_T);
+       }
+      else
+       display_count = rest_l_arr.size ();
+      
       /*
        UGH.  Should get dims from table.  Should have minimum dist.
        */
-      int dy = rest_l_arr_.size() > 2 ? 6 : 4;
-       
-      rest_l_arr_[0]->translate_rests (rest_l_arr_[0]->dir () *dy);    
-      rest_l_arr_.top()->translate_rests (rest_l_arr_.top ()->dir ()* dy);
+      int dy = display_count > 2 ? 6 : 4;
+      if (display_count > 1)
+       {
+         rest_l_arr[0]->translate_rests (dy);  
+         rest_l_arr[1]->translate_rests (-dy);
+       }
     }
   // meisjes met jongetjes
   else 
     {
-      if (rest_l_arr_.size () > 1)
+      if (rest_l_arr.size () > 1)
        {
          warning (_("too many colliding rests"));
        }
-      if (ncol_l_arr_.size () > 1)
+      if (ncol_l_arr.size () > 1)
        {
          warning (_("too many notes for rest collision"));
        }
-      Note_column * rcol = rest_l_arr_[0];
+      Note_column * rcol = rest_l_arr[0];
 
       // try to be opposite of noteheads. 
-      Direction dir = - ncol_l_arr_[0]->dir();
-
-      Interval restdim;
-      for (int i=0; i < rcol->rest_l_arr_.size(); i++)
-       restdim.unite (rcol->rest_l_arr_[i]->extent (Y_AXIS));
+      Direction dir = - ncol_l_arr[0]->dir();
 
+      Interval restdim = rcol->rest_dim ();
       if (restdim.empty_b ())
        return;
       
       // staff ref'd?
-      Real staff_space = rcol->rest_l_arr_[0]->staff_line_leading_f ();      
-      Real internote_f = staff_space/2;
+      Real staff_space = paper_l()->get_var ("interline");
+
+       /* FIXME
+         staff_space =  rcol->rest_l_arr[0]->staff_space ();
+       */
+      Real half_staff_space_f = staff_space/2;
       Real minimum_dist = paper_l ()->get_var ("restcollision_minimum_dist")
-       * internote_f;
+       * half_staff_space_f;
       
       /*
        assumption: ref points are the same. 
        */
       Interval notedim;
-      for (int i = 0; i < ncol_l_arr_.size(); i++) 
+      for (int i = 0; i < ncol_l_arr.size(); i++) 
        {
-         notedim.unite (ncol_l_arr_[i]->extent (Y_AXIS));
+         notedim.unite (ncol_l_arr[i]->extent (Y_AXIS));
        }
 
       Interval inter (notedim);
@@ -101,8 +170,11 @@ Rest_collision::do_pre_processing()
        minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
 
 
-      int stafflines = rcol->rest_l_arr_[0]->lines_i ();
-
+      // FIXME
+      //int stafflines = 5; // rcol->rest_l_arr[0]->line_count;
+      int stafflines = Staff_symbol_referencer_interface (this).line_count ();
+      // hurg?
+      stafflines = stafflines != 0 ? stafflines : 5;
       
       // move discretely by half spaces.
       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
@@ -115,30 +187,13 @@ Rest_collision::do_pre_processing()
     }
 }
 
-void
-Rest_collision::do_print() const
-{
-#ifndef NPRINT
-  DEBUG_OUT << "rests: " << rest_l_arr_.size() << ", ";
-  DEBUG_OUT << "cols: " << ncol_l_arr_.size();
-#endif
-}
-
-void
-Rest_collision::do_substitute_element_pointer (Score_element*o,Score_element*n)
-{
-  if (Note_column *onl = dynamic_cast<Note_column *> (o))
-    {
-      Note_column *n_l = n?dynamic_cast<Note_column *> (n):0;
-      rest_l_arr_.substitute (onl, n_l);
-      ncol_l_arr_.substitute (onl, n_l);
-    }
-}
 
 Rest_collision::Rest_collision()
 {
+  set_elt_property ("rests", SCM_EOL);
+  set_elt_property ("notes", SCM_EOL);
   set_elt_property ("transparent", SCM_BOOL_T);
-  set_empty (X_AXIS);
-  set_empty (Y_AXIS);
+  set_extent_callback (0, X_AXIS);
+  set_extent_callback (0, Y_AXIS);
 }