]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur.cc
release: 1.1.38
[lilypond.git] / lily / slur.cc
index 69b49cde0add34b8360f15db61214583ee725714..ff5c72076282cc3e07f99f3654a327b821189a3c 100644 (file)
@@ -3,12 +3,13 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
-    Jan Nieuwenhuizen <jan@digicash.com>
+  (c) 1996,  1997--1999, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+    Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 /*
   [TODO]
+    * URG: share code with tie
     * begin and end should be treated as a Script.
     * damping
     * slur from notehead to stemend: c''()b''
 #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"
 
-//IMPLEMENT_IS_TYPE_B1(Slur,Spanner);
-IMPLEMENT_IS_TYPE_B1(Slur,Bow);
 
 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);
+  //  n->stem_l_->slur_l_ = this;
   add_dependency (n);
 }
 
@@ -46,7 +50,7 @@ Slur::set_default_dir ()
   dir_ = DOWN;
   for (int i=0; i < encompass_arr_.size (); i ++) 
     {
-      if (encompass_arr_[i]->dir_ < 0) 
+      if (encompass_arr_[i]->dir () < 0) 
        {
          dir_ = UP;
          break;
@@ -69,13 +73,13 @@ Slur::do_pre_processing ()
 }
 
 void
-Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
+Slur::do_substitute_element_pointer (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);
     }
@@ -87,14 +91,33 @@ Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
   return Item::left_right_compare (n1, n2);
 }
 
+static bool
+broken_edge_b (Slur*s, Drul_array<Note_column*>& extrema, Direction dir)
+{
+  return extrema[dir] != s->spanned_drul_[dir];
+}
+
+static bool
+normal_edge_b (Slur*s, Drul_array<Note_column*>& extrema, Direction dir)
+{
+  Note_column *n = extrema[dir];
+  return !broken_edge_b (s, extrema, dir)
+    && n->stem_l_
+    && n->stem_l_->get_elt_property (transparent_scm_sym) == SCM_BOOL_F
+    && n->head_l_arr_.size ();
+}
+
 void
 Slur::do_post_processing ()
 {
   encompass_arr_.sort (Note_column_compare);
   if (!dir_)
     set_default_dir ();
-  Real interline_f = paper ()->interline_f ();
-  Real inter_f = interline_f / 2;
+
+  Real interline_f = paper_l ()->get_realvar (interline_scm_sym);
+  Real internote_f = interline_f / 2;
+  // URG
+  Real notewidth_f = paper_l ()->note_width () * 0.8;
 
   /* 
    [OSU]: slur and tie placement
@@ -105,140 +128,216 @@ Slur::do_post_processing ()
    * 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...
-
-   * suggested gap = ss / 5;
    */
-  // jcn: 1/5 seems so small?
-  Real gap_f = interline_f / 2; // 5;
   
+  Real gap_f = paper_l ()->get_var ("slur_x_gap");
+
   Drul_array<Note_column*> extrema;
   extrema[LEFT] = encompass_arr_[0];
   extrema[RIGHT] = encompass_arr_.top ();
 
   Direction d=LEFT;
-  Real nw_f = paper ()->note_width ();
  
   do 
     {
-      if (extrema[d] != spanned_drul_[d]) 
+      if (broken_edge_b (this, extrema, d))
        {
+         // ugh -- check if needed
          dx_f_drul_[d] = -d 
-           *(spanned_drul_[d]->width ().length () -0.5*nw_f);
-         Direction u = d;
-         flip(&u);
-         if ((extrema[u] == spanned_drul_[u]) && extrema[u]->stem_l_)
+           *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);
+
+         // prebreak
+         if (d == RIGHT)
            {
-             dy_f_drul_[d] = extrema[u]->stem_l_->height ()[dir_];
-             dy_f_drul_[u] = extrema[u]->stem_l_->height ()[dir_];
+             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;
            }
        }
-      else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
+      /*
+        normal slur
+       */
+      else if (normal_edge_b (this, extrema, d))
         {
-         dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
-         dx_f_drul_[d] += 0.5 * nw_f - d * gap_f;
+         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 * nw_f;
+               dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
              else
-               dx_f_drul_[d] += 0.25 * (dir_ * d) * d * nw_f;
+               dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
            }
        }
-      else 
+       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 
+    {
+      if (broken_edge_b (this, extrema, d))
         {
-         dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
-           [dir_])* inter_f;
+         Direction u = d;
+         flip(&u);
+
+         // postbreak
+         if (d == LEFT)
+           dy_f_drul_[u] += dir_ * internote_f;
+
+         dy_f_drul_[d] = dy_f_drul_[u];
        }
-      dy_f_drul_[d] += dir_ * interline_f;
-    }
+     }
   while (flip(&d) != LEFT);
+
+  /*
+    Slur should follow line of music
+   */
+  if (normal_edge_b (this, extrema, LEFT)
+      && normal_edge_b (this, extrema, RIGHT)
+      && (extrema[LEFT]->stem_l_ != extrema[RIGHT]->stem_l_))
+    {
+      Real note_dy = extrema[RIGHT]->stem_l_->head_positions ()[dir_]
+       - extrema[LEFT]->stem_l_->head_positions ()[dir_];
+      Real dy = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
+      /*
+       Should we always follow note-heads, (like a tie)?
+       For now, only if the note_dy != slur_dy, we'll do
+       slur_dy := note_dy * factor.
+      */
+      if (sign (dy) != sign (note_dy))
+       {
+         Real damp_f = paper_l ()->get_var ("slur_slope_follow_music_factor");
+         Real realdy = note_dy * damp_f;
+         Direction adjust_dir = (Direction)(- dir_ * sign (realdy));
+         if (!adjust_dir)
+           adjust_dir = -dir_;
+         /*
+           adjust only if no beam gets in the way
+          */
+         if (!extrema[adjust_dir]->stem_l_->beam_l_
+             || (adjust_dir == extrema[adjust_dir]->stem_l_->dir_)
+             || (extrema[adjust_dir]->stem_l_->beams_i_drul_[-adjust_dir] < 1))
+           {
+             dy_f_drul_[adjust_dir] = dy_f_drul_[-adjust_dir]
+               + 2 * adjust_dir * realdy;
+             Real dx = notewidth_f / 2;
+             if (adjust_dir != extrema[adjust_dir]->stem_l_->dir_)
+               dx /= 2;
+             dx_f_drul_[adjust_dir] -= adjust_dir * dx;
+           }
+       }
+    }
+
+  /*
+    Avoid too steep slurs.
+   */
+  Real damp_f = paper_l ()->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_AXIS] += extent (X_AXIS).length ();
+
+  Real ratio_f = abs (d_off[Y_AXIS] / d_off[X_AXIS]);
+  if (ratio_f > damp_f)
+    dy_f_drul_[(Direction)(- dir_ * sign (d_off[Y_AXIS]))] +=
+      dir_ * (ratio_f - damp_f) * d_off[X_AXIS];
 }
 
 Array<Offset>
 Slur::get_encompass_offset_arr () const
 {
-  Real interline = paper ()->interline_f ();
-  Real notewidth = paper ()->note_width ();
-  Real internote = interline / 2;
+  Real notewidth = paper_l ()->note_width () * 0.8;
+  Real gap = paper_l ()->get_var ("slur_x_gap");
 
-  Stem* left_stem = encompass_arr_[0]->stem_l_;
-  Real left_x = left_stem->hpos_f ();
-  left_x += dx_f_drul_[LEFT];
+  Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
+  left[X_AXIS] += encompass_arr_[0]->stem_l_->hpos_f ();
+  Real internote = encompass_arr_[0]->stem_l_->staff_line_leading_f ()/2.0;
+
+  /*
+    <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??
+
+   */
 
-  Real left_y = dy_f_drul_[LEFT];
+  if (dir_ != encompass_arr_[0]->stem_l_->dir_)
+    left[X_AXIS] += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
+      + gap;
+  else if (encompass_arr_[0]->stem_l_->dir_ == UP)
+    left[X_AXIS] -= notewidth;
 
-  Real dx = width ().length ();
-  dx += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
-  dx = dx <? 1000;
-  dx = dx >? 2 * interline;
+  if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
+    && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
+    left[Y_AXIS] -= internote * encompass_arr_[0]->stem_l_->dir_;
+  /* </URG> */
 
-  Real dy = (dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
-  if (abs (dy) > 1000)
-    dy = sign (dy) * 1000;
+  Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
+    dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
+  d[X_AXIS] += extent (X_AXIS).length ();
 
   int first = 1;
   int last = encompass_arr_.size () - 1;
-  if (encompass_arr_[0] != spanned_drul_[LEFT])
-    {
-      first = 0;
-      left_x = spanned_drul_[LEFT]->width ().length ();
-      left_x -= 2 * notewidth;
-      left_y = encompass_arr_[last]->stem_l_->height ()[dir_];
-      dy = 0;
-    }
+
+  // prebreak
   if (encompass_arr_.top () != spanned_drul_[RIGHT])
-    {
-      last += 1;
-      dy = 0;
-    }
+    last++;
+
+  // postbreak
+  if (encompass_arr_[0] != spanned_drul_[LEFT])
+    first--;
 
-#define RESIZE_ICE
-#ifndef RESIZE_ICE
   Array<Offset> notes;
   notes.push (Offset (0,0));
-#else
-  int n = last - first + 2;
-  Array<Offset> notes (n);
-  notes[0] = Offset (0,0);
-#endif
+
   for (int i = first; i < last; i++)
     {
-      Stem* stem = encompass_arr_[i]->stem_l_;
-      /* 
-       set x to middle of notehead or on exact x position of stem,
-       according to slur direction
-          */
-      Real x = stem->hpos_f ();
-
-      if (stem->dir_ != dir_)
-       x += 0.5 * notewidth;
-      else if (stem->dir_ == UP)
-       x += 1.0 * notewidth;
-
-      x -= left_x;
+      Encompass_info info (encompass_arr_[i], dir_, this);
+      notes.push (info.o_ - left);
+    }
+  Encompass_info info (encompass_arr_.top (), dir_, this);
+  Real inter_staff = info.interstaff_f_;
+  
+  d[Y_AXIS] += inter_staff;
 
-      Real y = stem->height ()[dir_];
+  // prebreak
+  if (inter_staff && (encompass_arr_.top () != spanned_drul_[RIGHT]))
+    {
+      Encompass_info info (encompass_arr_[encompass_arr_.size () - 1], dir_, this);
+      d[Y_AXIS] -= info.o_[Y_AXIS] - inter_staff;
+    }
 
-      /*
-       leave a gap: slur mustn't touch head/stem
-       */
-      y += 2.5 * internote * dir_;
+  notes.push (d);
 
-      if (dir_ == DOWN)
-       y += 1.5 * internote * dir_;
+  return notes;
+}
 
-      y -= left_y;
 
-#ifndef RESIZE_ICE
-      notes.push (Offset (x, y));
-    }
-  notes.push (Offset (dx, dy));
-#else
-      notes[i - first + 1] = Offset (x, y);
-    }
-  notes[n - 1] = Offset (dx, dy);
-#endif
+Array<Rod>
+Slur::get_rods () const
+{
+  Array<Rod> a;
+  Rod r;
+  r.item_l_drul_ = spanned_drul_;
+  r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
 
-  return notes;
+  a.push (r);
+  return a;
 }