]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur.cc
release: 1.1.18
[lilypond.git] / lily / slur.cc
index aadb9d44b9f393fe25944df636ad854be537e7bd..465102e6af68084b38c3da7c9fe4e32bdd6e65d9 100644 (file)
@@ -3,15 +3,18 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c) 1996,  1997--1998, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+    Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 /*
-  TODO:
-  
-  think about crossing stems.
-  Begin and end should be treated as a Script.
+  [TODO]
+    * URG: share code with tie
+    * begin and end should be treated as a Script.
+    * damping
+    * slur from notehead to stemend: c''()b''
  */
+
 #include "slur.hh"
 #include "scalar.hh"
 #include "lookup.hh"
 #include "p-col.hh"
 #include "molecule.hh"
 #include "debug.hh"
-#include "boxes.hh"
+#include "box.hh"
+#include "bezier.hh"
+#include "encompass-info.hh"
+#include "main.hh"
+
 
 
+Slur::Slur ()
+{
+}
 
 void
-Slur::add (Note_column*n)
+Slur::add_column (Note_column*n)
 {
+  if (!n->head_l_arr_.size ())
+    warning (_ ("Putting slur over rest."));
   encompass_arr_.push (n);
   add_dependency (n);
 }
 
 void
-Slur::set_default_dir()
+Slur::set_default_dir ()
 {
   dir_ = DOWN;
-  for (int i=0; i < encompass_arr_.size(); i ++) 
+  for (int i=0; i < encompass_arr_.size (); i ++) 
     {
       if (encompass_arr_[i]->dir_ < 0) 
        {
-         dir_ =UP;
+         dir_ = UP;
          break;
        }
     }
 }
 
 void
-Slur::do_pre_processing()
+Slur::do_add_processing ()
 {
-  set_bounds(LEFT, encompass_arr_[0]);    
-  set_bounds(RIGHT, encompass_arr_.top());
+  set_bounds (LEFT, encompass_arr_[0]);    
+  if (encompass_arr_.size () > 1)
+    set_bounds (RIGHT, encompass_arr_.top ());
 }
 
+void
+Slur::do_pre_processing ()
+{
+  // don't set directions
+}
 
 void
-Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
+Slur::do_substitute_dependency (Score_element*o, Score_element*n)
 {
   int i;
-  while ((i = encompass_arr_.find_i ((Note_column*)o->item())) >=0) 
+  while ((i = encompass_arr_.find_i (dynamic_cast<Note_column *> (o))) >=0) 
     {
       if (n)
-       encompass_arr_[i] = (Note_column*)n->item();
+       encompass_arr_[i] = dynamic_cast<Note_column *> (n);
       else
        encompass_arr_.del (i);
     }
 }
 
-
 static int 
 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
 {
-  return Item::left_right_compare(n1, n2);
+  return Item::left_right_compare (n1, n2);
 }
 
 void
-Slur::do_post_processing()
+Slur::do_post_processing ()
 {
   encompass_arr_.sort (Note_column_compare);
   if (!dir_)
-    set_default_dir();
-  Real inter_f = paper()->internote_f ();
+    set_default_dir ();
+
+  Real interline_f = paper ()->interline_f ();
+  Real internote_f = interline_f / 2;
+  // URG
+  Real notewidth_f = paper ()->note_width () * 0.8;
+  Real slur_min = paper ()->get_var ("slur_x_minimum");
+
+  /* 
+   [OSU]: slur and tie placement
+
+   slurs:
+   * x = centre of head (upside-down: inner raakpunt stem) - d * gap
+
+   * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
+     y = length >= 5ss : y next interline - d * 0.25 ss
+     --> height <= 5 length ?? we use <= 3 length, now...
+   */
   
+  Real gap_f = paper ()->get_var ("slur_x_gap");
+
   Drul_array<Note_column*> extrema;
   extrema[LEFT] = encompass_arr_[0];
-  extrema[RIGHT] = encompass_arr_.top();
+  extrema[RIGHT] = encompass_arr_.top ();
 
   Direction d=LEFT;
-  Real nw_f = paper()->note_width ();
  
   do 
     {
-      if  (extrema[d] != spanned_drul_[d]) 
+      /*
+        broken slur
+       */
+      if (extrema[d] != spanned_drul_[d]) 
        {
+         // ugh -- check if needed
          dx_f_drul_[d] = -d 
-           *(spanned_drul_[d]->width ().length ()/nw_f -0.5);
+           *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);
+
+         // prebreak
+         if (d == RIGHT)
+           {
+             dx_f_drul_[LEFT] = spanned_drul_[LEFT]->extent (X_AXIS).length ();
+
+             // urg -- check if needed
+             if (encompass_arr_.size () > 1)
+               dx_f_drul_[RIGHT] += notewidth_f;
+           }
+       }
+      /*
+        normal slur
+       */
+      else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_ 
+              && extrema[d]->head_l_arr_.size ()) 
+        {
+         Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
+         dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_-> extent (Y_AXIS)[dir_]);
+         dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
+         if (dir_ == extrema[d]->stem_l_->dir_)
+           {
+             if (dir_ == d)
+               dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
+             else
+               dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
+           }
+       }
+       else 
+         {
+           Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
+           dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
+                                      [dir_]) * internote_f;
+           dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
+       }
+       dy_f_drul_[d] += dir_ * interline_f;
+       if (extrema[d]->stem_l_ && (dir_ == extrema[d]->stem_l_->dir_))
+         dy_f_drul_[d] -= dir_ * internote_f;
+      }
+  while (flip(&d) != LEFT);
+
+  // now that both are set, do dependent
+  do 
+    {
+      /*
+        broken slur
+       */
+      if (extrema[d] != spanned_drul_[d]) 
+        {
+         Direction u = d;
+         flip(&u);
+
+         // postbreak
+         if (d == LEFT)
+           dy_f_drul_[u] += dir_ * internote_f;
+
+         dy_f_drul_[d] = dy_f_drul_[(Direction)-d];
+
+         // pre and post
+         if (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT] < slur_min)
+           {
+             dx_f_drul_[d] -= d * slur_min 
+               - (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
+             dx_f_drul_[d] = dx_f_drul_[(Direction)-d] + d * slur_min;
+           }
        }
-      else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
-       pos_i_drul_[d] = (int)rint (extrema[d]->stem_l_->height()[dir_]/inter_f);
-      else 
-       pos_i_drul_[d] = (int)rint (extrema[d]->head_positions_interval()[dir_]);
-      pos_i_drul_[d] += dir_;
+     }
+  while (flip(&d) != LEFT);
+
+  /*
+    Avoid too steep slurs.
+      * slur from notehead to stemend: c''()b''
+   */
+  Real damp_f = paper ()->get_var ("slur_slope_damping");
+  Offset d_off = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
+    dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
+  d_off.x () += extent (X_AXIS).length ();
+
+  Real ratio_f = abs (d_off.y () / d_off.x ());
+  if (ratio_f > damp_f)
+    dy_f_drul_[(Direction)(- dir_ * sign (d_off.y ()))] -=
+      dir_ * (damp_f - ratio_f) * d_off.x ();
+}
+
+Array<Offset>
+Slur::get_encompass_offset_arr () const
+{
+  Real notewidth = paper ()->note_width () * 0.8;
+  Real gap = paper ()->get_var ("slur_x_gap");
+  Real internote = paper ()->internote_f ();
+
+  Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
+  left.x () += encompass_arr_[0]->stem_l_->hpos_f ();
+
+  /*
+    <URG>
+    i don't understand these two, but *must* for symmetry 
+    look at encompass array: 
+       lilypond -D input/test/slur-symmetry*.ly
+       lilypond -D input/test/sleur.ly
+
+    do_post_processing should have calculated these into
+    dx_f_drul_[], no??
+
+   */
+
+  if (dir_ != encompass_arr_[0]->stem_l_->dir_)
+    left.x () += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
+      + gap;
+  else if (encompass_arr_[0]->stem_l_->dir_ == UP)
+    left.x () -= notewidth;
+
+  if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
+    && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
+    left.y () -= internote * encompass_arr_[0]->stem_l_->dir_;
+  /* </URG> */
+
+  Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
+    dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
+  d.x () += extent (X_AXIS).length ();
+
+  int first = 1;
+  int last = encompass_arr_.size () - 1;
+
+  // prebreak
+  if (encompass_arr_[0] != spanned_drul_[LEFT])
+    first--;
+
+  // postbreak
+  if (encompass_arr_.top () != spanned_drul_[RIGHT])
+    last++;
+
+  Array<Offset> notes;
+  notes.push (Offset (0,0));
+
+  for (int i = first; i < last; i++)
+    {
+      Encompass_info info (encompass_arr_[i], dir_);
+      notes.push (info.o_ - left);
     }
-  while ((d *= -1) != LEFT);
+  notes.push (d);
+
+  return notes;
 }
 
-IMPLEMENT_IS_TYPE_B1(Slur,Spanner);