]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur.cc
release: 1.1.39
[lilypond.git] / lily / slur.cc
index 6bc5c1b24b98082dc3f1e3911dba480842b277da..789f0aac8c765140280feef29f726a0d6f29315f 100644 (file)
 /*
+  slur.cc -- implement  Slur
 
-  TODO:
-  
-  think about crossing stems.
-  Begin and end should be treated as a Script.
-  
+  source file of the GNU LilyPond music typesetter
+
+  (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 "slur.hh"
 #include "scalar.hh"
 #include "lookup.hh"
 #include "paper-def.hh"
-#include "notehead.hh"
+#include "note-column.hh"
+#include "stem.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()
+Slur::Slur ()
 {
-    open_right=open_left=false;
 }
 
-Offset
-Slur::center() const
+void
+Slur::add_column (Note_column*n)
 {
-    int pos1 = encompass.top()->position_i_;
-    int pos2 = encompass[0]->position_i_;
-
-    int dy =  pos1-pos2;
+  if (!n->head_l_arr_.size ())
+    warning (_ ("Putting slur over rest."));
+  encompass_arr_.push (n);
+  //  n->stem_l_->slur_l_ = this;
+  add_dependency (n);
+}
 
-    Real w = width().length();
+Direction
+Slur::get_default_dir () const
+{
+  Direction d = DOWN;
+  for (int i=0; i < encompass_arr_.size (); i ++) 
+    {
+      if (encompass_arr_[i]->dir () < 0) 
+       {
+         d = UP;
+         break;
+       }
+    }
+  return d;
+}
 
-    return Offset(w/2,dy * paper()->internote());
+void
+Slur::do_add_processing ()
+{
+  set_bounds (LEFT, encompass_arr_[0]);    
+  if (encompass_arr_.size () > 1)
+    set_bounds (RIGHT, encompass_arr_.top ());
 }
 
 void
-Slur::add(Notehead*n)
+Slur::do_pre_processing ()
 {
-    encompass.push(n);
-    add_dependency(n);
+  // don't set directions
 }
 
 void
-Slur::set_default_dir()
+Slur::do_substitute_element_pointer (Score_element*o, Score_element*n)
 {
-    int sumpos=0;
-    for (int i=0; i < encompass.size(); i ++) {
-       sumpos += encompass[i]->position_i_;
+  int i;
+  while ((i = encompass_arr_.find_i (dynamic_cast<Note_column *> (o))) >=0) 
+    {
+      if (n)
+       encompass_arr_[i] = dynamic_cast<Note_column *> (n);
+      else
+       encompass_arr_.del (i);
     }
+}
 
-    /* should consult stems */
-    Real meanpos = sumpos/Real(encompass.size());
-    if (meanpos < 5)           // todo
-       dir_i_ = -1;
-    else
-       dir_i_ = 1;    
+static int 
+Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
+{
+  return Item::left_right_compare (n1, n2);
 }
 
-void
-Slur::do_pre_processing()
+bool
+Slur::broken_edge_b ( Direction dir) const
+{
+  return extrema ()[dir] != spanned_drul_[dir];
+}
+
+bool
+Slur::normal_edge_b ( Direction dir) const
 {
-    right_col_l_  = encompass.top()->pcol_l_;
-    left_col_l_ = encompass[0]->pcol_l_;    
+  Note_column *n = extrema ()[dir];
+  return !broken_edge_b ( dir)
+    && n->stem_l_
+    && n->stem_l_->get_elt_property (transparent_scm_sym) == SCM_BOOL_F
+    && n->head_l_arr_.size ();
 }
 
-Spanner*
-Slur::do_break_at(PCol*l, PCol*r) const
+Drul_array<Note_column*>
+Slur::extrema ()const
 {
-    assert(l->line_l_ == r->line_l_);
-    Slur*ret = new Slur(*this);
+  Drul_array<Note_column*> extrema;
+  extrema[LEFT] = encompass_arr_[0];
+  extrema[RIGHT] = encompass_arr_.top ();
+  return extrema;
+}
+
+/*
+  TODO.
+
+  Unhair this.
+ */
+void
+Slur::do_post_processing ()
+{
+  encompass_arr_.sort (Note_column_compare);
+  if (!dir_)
+    dir_ = get_default_dir ();
+
+  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
+
+   slurs:
+   * x = centre of head (upside-down: inner raakpunt stem) - d * gap
 
-    ret->encompass.set_size(0);
-    for (int i =0; i < encompass.size(); i++) {
-       if (encompass[i]->pcol_l_->line_l_==l->line_l_)
-           ret->encompass.push(encompass[i]);
+   * 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_l ()->get_var ("slur_x_gap");
+
+
+  Direction d=LEFT;
+  do 
+    {
+      if (broken_edge_b (d))
+       {
+         // ugh -- check if needed
+         dx_f_drul_[d] = -d 
+           *(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 (normal_edge_b (d))
+        {
+         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_ * notewidth_f;
+             else
+               dx_f_drul_[d] += 0.25 * dir_ * 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 
+    {
+      if (broken_edge_b (d))
+        {
+         Direction u = d;
+         flip(&u);
+
+         // postbreak
+         if (d == LEFT)
+           dy_f_drul_[u] += dir_ * internote_f;
+
+         dy_f_drul_[d] = dy_f_drul_[u];
+       }
+     }
+  while (flip(&d) != LEFT);
+
+  /*
+    Slur should follow line of music
+   */
+  if (normal_edge_b (LEFT)
+      && normal_edge_b (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;
+           }
+       }
     }
-    if (right_col_l_ != r)
-       ret->open_right = true;
-    if (left_col_l_ != l)
-       ret->open_left = true;
 
+  /*
+    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 ();
 
-    return ret;
+  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];
 }
 
-void
-Slur::do_post_processing()
+Array<Offset>
+Slur::get_encompass_offset_arr () const
 {
-    if (!dir_i_)
-       set_default_dir();
+  Real notewidth = paper_l ()->note_width () * 0.8;
+  Real gap = paper_l ()->get_var ("slur_x_gap");
+
+  /*
+  urg.  Calcs done wrt the leftmost note.  Fixme.
+
+  Calcs ignore possibility of pre/postbreak.
+
+
+  */
+
+  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??
+
+   */
+
+  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;
+
+  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> */
+
+  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;
+
+  // prebreak
+  if (broken_edge_b (RIGHT))
+    last++;
+
+  // postbreak
+  if (broken_edge_b (LEFT))
+    first--;
+
+  Array<Offset> notes;
+  notes.push (Offset (0,0));
+
+  Real dy =0.0;
+  for (int i = 0; i < last; i++)
+    {
+      Encompass_info info (encompass_arr_[i], dir_, this);
+      if (i >= first)
+       notes.push (info.o_ - left);
+      else
+       dy = info.interstaff_f_;
+    }
+
+  notes[0][Y_AXIS] += dy;
+  notes.push (d);
+  
+  return notes;
 }
 
-Molecule*
-Slur::brew_molecule_p() const
+
+Array<Rod>
+Slur::get_rods () const
 {
-    Molecule*output = new Molecule;
+  Array<Rod> a;
+  Rod r;
+  r.item_l_drul_ = spanned_drul_;
+  r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
 
-    int minp=1000, maxp=-1000; // todo    
-    for (int i=0; i<encompass.size(); i++) {
-       minp = encompass[i]->position_i_ <? minp;
-       maxp = encompass[i]->position_i_ >? maxp;
-    }
-    assert(encompass.size()>0);        // todo
-    
-    Notehead *lnote_p =encompass[0];
-    Notehead *rnote_p =encompass.top();
-    int lpos_i = lnote_p->position_i_;
-    int rpos_i = rnote_p->position_i_;
-    Offset  left_off(lnote_p->x_dir_i_, lpos_i + 2*dir_i_);
-    Offset right_off(lnote_p->x_dir_i_, rpos_i + 2*dir_i_);
-    if (!lnote_p->extremal_i_)
-       left_off += Offset(0.5, -dir_i_);
-    if (!rnote_p->extremal_i_)
-       right_off+= Offset(-0.5, -dir_i_);
-    
-    int dy = int(right_off.y - left_off.y);
-    
-    Real nw_f = paper()->note_width();
-    Real nh_f = paper()->internote();
-    Real w = width().length();
-    
-    w+= (right_off.x - left_off.x) * nw_f ;
-    Real round_w = w;          // slur lookup rounds the slurwidth .
-    
-    Symbol sl = paper()->lookup_l()->slur(dy , round_w, dir_i_);
-
-    Real error = w-round_w;
-    
-    Atom a(sl);
-    a.translate(Offset((left_off.x + 0.5 )*nw_f + error/2,
-                      left_off.y * nh_f));
-    output->add(a);
-    return output;
+  a.push (r);
+  return a;
 }
 
-IMPLEMENT_STATIC_NAME(Slur);