]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur.cc
release: 1.3.65
[lilypond.git] / lily / slur.cc
index 3e8cbdfe26d5eb40667d4bd5a07fc1bb4297f792..05ff6477eafef83fbbf80becce13cd5acf83393e 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
-    Jan Nieuwenhuizen <jan@digicash.com>
+  (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+    Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 /*
   [TODO]
-    * begin and end should be treated as a Script.
-    * damping
-    * slur from notehead to stemend: c''()b''
+    * begin and end should be treated as a/acknowledge Scripts.
+    * broken slur should have uniform trend
+    * smart changing of endings
+    * smart changing of (Y-?)offsets to avoid ugly beziers
+       (along-side-stem)
  */
 
+#include "directional-element-interface.hh"
+#include "group-interface.hh"
 #include "slur.hh"
-#include "scalar.hh"
 #include "lookup.hh"
 #include "paper-def.hh"
 #include "note-column.hh"
 #include "stem.hh"
-#include "p-col.hh"
+#include "paper-column.hh"
 #include "molecule.hh"
 #include "debug.hh"
-#include "boxes.hh"
-#include "bezier.hh"
-
-//IMPLEMENT_IS_TYPE_B1(Slur,Spanner);
-IMPLEMENT_IS_TYPE_B1(Slur,Bow);
-
-Slur::Slur ()
+#include "slur-bezier-bow.hh"
+#include "main.hh"
+#include "cross-staff.hh"
+#include "group-interface.hh"
+#include "staff-symbol-referencer.hh"
+
+Slur::Slur (SCM s)
+  : Spanner (s)
 {
+  set_elt_property ("attachment", gh_cons (SCM_BOOL_F, SCM_BOOL_F));
+  set_elt_pointer ("note-columns", SCM_EOL);
+  set_elt_property ("control-points", SCM_EOL);
 }
 
 void
-Slur::add (Note_column*n)
+Slur::add_column (Note_column*n)
 {
-  encompass_arr_.push (n);
-  add_dependency (n);
+  if (!gh_pair_p (n->get_elt_pointer ("note-heads")))
+    warning (_ ("Putting slur over rest.  Ignoring."));
+  else
+    {
+      Pointer_group_interface (this, "note-columns").add_element (n);
+      add_dependency (n);
+    }
+
+  add_bound_item (this, n);
 }
 
 void
-Slur::set_default_dir ()
+Slur::de_uglyfy (Slur_bezier_bow* bb, Real default_height)
+{
+  Real length = bb->curve_.control_[3][X_AXIS] ; 
+  Real ff = bb->fit_factor ();
+  for (int i = 1; i < 3; i++)
+    {
+      Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
+                     - bb->curve_.control_[i][X_AXIS]) / length;
+      Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
+
+      Real f = default_height / length;
+      Real c1 = paper_l ()->get_var ("bezier_control1");
+      Real c2 = paper_l ()->get_var ("bezier_control2");
+      Real c3 = paper_l ()->get_var ("bezier_control3");
+      if (h > c1 * f)
+       {
+         h = c1 * f; 
+       }
+      else if (h > c2 + c3 * ind)
+       {
+         h = c2 + c3 * ind; 
+       }
+      
+      bb->curve_.control_[i][Y_AXIS] = h * length;
+    } 
+
+  bb->curve_.assert_sanity ();
+}
+
+Direction
+Slur::get_default_dir () const
 {
-  dir_ = DOWN;
-  for (int i=0; i < encompass_arr_.size (); i ++) 
+  Link_array<Note_column> encompass_arr =
+    Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
+  
+  Direction d = DOWN;
+  for (int i=0; i < encompass_arr.size (); i ++) 
     {
-      if (encompass_arr_[i]->dir_ < 0) 
+      if (encompass_arr[i]->dir () < 0) 
        {
-         dir_ = UP;
+         d = UP;
          break;
        }
     }
+  return d;
 }
 
 void
 Slur::do_add_processing ()
 {
-  set_bounds (LEFT, encompass_arr_[0]);    
-  if (encompass_arr_.size () > 1)
-    set_bounds (RIGHT, encompass_arr_.top ());
-}
+#if 0
+  Link_array<Note_column> encompass_arr =
+    Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
 
-void
-Slur::do_pre_processing ()
-{
-  // don't set directions
+  if (encompass_arr.size ())
+    {
+      set_bound (LEFT, encompass_arr[0]);    
+      if (encompass_arr.size () > 1)
+       set_bound (RIGHT, encompass_arr.top ());
+    }
+#endif
 }
 
-void
-Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
+
+
+Offset
+Slur::encompass_offset (Note_column const* col) const
 {
-  int i;
-  while ((i = encompass_arr_.find_i ((Note_column*)o->item ())) >=0) 
+  Offset o;
+  Stem* stem_l = col->stem_l ();
+  Direction dir = Directional_element_interface (this).get ();
+  
+  if (!stem_l)
     {
-      if (n)
-       encompass_arr_[i] = (Note_column*)n->item ();
-      else
-       encompass_arr_.del (i);
+      warning (_ ("Slur over rest?"));
+     o[X_AXIS] = col->relative_coordinate (0, X_AXIS);
+      o[Y_AXIS] = col->extent (Y_AXIS)[dir];
+      return o;  
     }
+  Direction stem_dir = Directional_element_interface (stem_l).get ();
+  o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
+
+  /*
+    Simply set x to middle of notehead
+   */
+
+  o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
+
+  if ((stem_dir == dir)
+      && !stem_l->extent (Y_AXIS).empty_b ())
+    {
+      o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
+    }
+  else
+    {
+      o[Y_AXIS] = col->extent (Y_AXIS)[dir];
+    }
+
+  /*
+   leave a gap: slur mustn't touch head/stem
+   */
+  o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
+  o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
+  return o;
 }
 
-static int 
-Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
+GLUE_SCORE_ELEMENT(Slur,after_line_breaking);
+
+SCM
+Slur::member_after_line_breaking ()
 {
-  return Item::left_right_compare (n1, n2);
+  set_extremities ();
+  set_control_points ();
+  return SCM_UNDEFINED;
+} 
+
+SCM
+slur_get_bound (SCM slur, SCM dir)
+{
+  return ((Slur*)unsmob_element (slur))->get_bound (to_dir (dir))->self_scm_;
 }
 
-void
-Slur::do_post_processing ()
+SCM
+score_element_get_pointer (SCM se, SCM name)
 {
-  encompass_arr_.sort (Note_column_compare);
-  if (!dir_)
-    set_default_dir ();
-  Real interline_f = paper ()->interline_f ();
-  Real inter_f = interline_f / 2;
+  SCM s = scm_assq (name, unsmob_element (se)->pointer_alist_);
+  return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s); 
+}
 
-  /* 
-   [OSU]: slur and tie placement
+SCM
+score_element_get_property (SCM se, SCM name)
+{
+  SCM s = scm_assq (name, unsmob_element (se)->property_alist_);
+  return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s); 
+}
 
-   slurs:
-   * x = centre of head (upside-down: inner raakpunt stem) - d * gap
+void
+init_score_elts ()
+{
+  scm_make_gsubr ("get-pointer", 2 , 0, 0,  
+                 (SCM(*)(...)) score_element_get_pointer);
+  scm_make_gsubr ("get-property", 2 , 0, 0,  
+                 (SCM(*)(...)) score_element_get_property);
+  scm_make_gsubr ("get-bound", 2 , 0, 0,  
+                 (SCM(*)(...)) slur_get_bound);
+}
 
-   * 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...
+ADD_SCM_INIT_FUNC (score_elt, init_score_elts);
 
-   * suggested gap = ss / 5;
-   */
-  // jcn: 1/5 seems so small?
-  Real gap_f = interline_f / 2; // 5;
-  
-  Drul_array<Note_column*> extrema;
-  extrema[LEFT] = encompass_arr_[0];
-  extrema[RIGHT] = encompass_arr_.top ();
+void
+Slur::set_extremities ()
+{
+  if (!Directional_element_interface (this).get ())
+    Directional_element_interface (this).set (get_default_dir ());
 
-  Direction d=LEFT;
-  Real nw_f = paper ()->note_width ();
+  Direction dir = LEFT;
   do 
     {
-      if (extrema[d] != spanned_drul_[d]) 
+      if (!gh_symbol_p (index_cell (get_elt_property ("attachment"), dir)))
        {
-         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_)
+         
+         // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
+         for (SCM s = scm_eval (ly_symbol2scm ("slur-extremity-rules"));
+              s != SCM_EOL; s = gh_cdr (s))
            {
-             dy_f_drul_[d] = extrema[u]->stem_l_->height ()[dir_];
-             dy_f_drul_[u] = extrema[u]->stem_l_->height ()[dir_];
+             SCM r = scm_eval (scm_listify (gh_caar (s),
+                                            this->self_scm_,
+                                            gh_int2scm ((int)dir),
+                                            SCM_UNDEFINED));
+             if (r != SCM_BOOL_F)
+               {
+                 index_set_cell (get_elt_property ("attachment"), dir,
+                                 gh_cdar (s));
+                 break;
+               }
            }
        }
-      else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
-        {
-         dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
-         dx_f_drul_[d] += 0.5 * nw_f - d * gap_f;
-         if (dir_ == extrema[d]->stem_l_->dir_)
+    }
+  while (flip (&dir) != LEFT);
+}
+
+Offset
+Slur::get_attachment (Direction dir) const
+{
+  SCM s = get_elt_property ("attachment");
+  SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
+  String str = ly_symbol2string (a);
+  Real ss = Staff_symbol_referencer_interface (this).staff_space ();
+  Real hs = ss / 2.0;
+  Offset o;
+  if (Note_column* n = dynamic_cast<Note_column*> (get_bound (dir)))
+    {
+      if (Stem* st = dynamic_cast<Stem*> (n->stem_l ()))
+       {
+         if (str == "head")
            {
-             if (dir_ == d)
-               dx_f_drul_[d] += 0.5 * (dir_ * d) * d * nw_f;
-             else
-               dx_f_drul_[d] += 0.25 * (dir_ * d) * d * nw_f;
+             o = Offset (0, st->chord_start_f ());
+             /*
+               Default position is centered in X, on outer side of head Y
+              */
+             o += Offset (0.5 * n->extent (X_AXIS).length (),
+                          0.5 * ss * Directional_element_interface (this).get ());
+           }
+         else if (str == "alongside-stem")
+           {
+             o = Offset (0, st->chord_start_f ());
+             /*
+               Default position is on stem X, on outer side of head Y
+              */
+             o += Offset (n->extent (X_AXIS).length ()
+                          * (1 + st->get_direction ()),
+                          0.5 * ss * Directional_element_interface (this).get ());
+           }
+         else if (str == "stem")
+           {
+             o = Offset (0, st->stem_end_position () * hs);
+             /*
+               Default position is on stem X, at stem end Y
+              */
+             o += Offset (0.5 *
+                          (n->extent (X_AXIS).length ()
+                           - st->extent (X_AXIS).length ())
+                           * (1 + st->get_direction ()),
+                           0);
+           }
+         else if (str == "loose-end")
+           {
+             SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
+             if (ly_symbol2string (other_a) != "loose-end")
+               {
+                 o = Offset (0, get_attachment (-dir)[Y_AXIS]);
+               }
+           }
+
+         
+         SCM l = scm_assoc
+           (scm_listify (a,
+                         gh_int2scm (st->get_direction () * dir),
+                         gh_int2scm (Directional_element_interface (this).get () * dir),
+                         SCM_UNDEFINED),
+            scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
+         
+         if (l != SCM_BOOL_F)
+           {
+             o += ly_scm2offset (gh_cdr (l)) * ss * dir;
            }
        }
-      else 
-        {
-         dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
-           [dir_])* inter_f;
-       }
-      dy_f_drul_[d] += dir_ * interline_f;
     }
-  while (flip(&d) != LEFT);
+
+
+  /*
+    URG
+   */
+
+  if (str != "loose-end")
+    {
+      Link_array<Note_column> encompass_arr =
+       Pointer_group_interface__extract_elements (this, (Note_column*)0,
+                                                  "note-columns");
+      o -= Offset (0, calc_interstaff_dist (dir == LEFT ? encompass_arr[0]
+                                           : encompass_arr.top (), this));
+    }
+  return o;
+}
+
+int
+Slur::cross_staff_count ()const
+{
+  Link_array<Note_column> encompass_arr =
+    Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
+
+  int k=0;
+
+  for (int i = 0; i < encompass_arr.size (); i++)
+    {
+      if (calc_interstaff_dist (encompass_arr[i], this))
+       k++;
+    }
+  return k;
 }
 
+
 Array<Offset>
 Slur::get_encompass_offset_arr () const
 {
-  Real interline = paper ()->interline_f ();
-  Real notewidth = paper ()->note_width ();
-  Real internote = interline / 2;
+  Link_array<Note_column> encompass_arr =
+    Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
+  
+  Array<Offset> offset_arr;
 
-  Stem* left_stem = encompass_arr_[0]->stem_l_;
-  Real left_x = left_stem->hpos_f ();
-  left_x += dx_f_drul_[LEFT];
+  Offset origin (relative_coordinate (0, X_AXIS), 0);
 
-  Real left_y = dy_f_drul_[LEFT];
+  int first = 1;
+  int last = encompass_arr.size () - 2;
 
-  Real dx = width ().length ();
-  dx += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
-  dx = dx <? 1000;
-  dx = dx >? 2 * interline;
+  offset_arr.push (get_attachment (LEFT));
 
-  Real dy = (dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
-  if (abs (dy) > 1000)
-    dy = sign (dy) * 1000;
+  /*
+    left is broken edge
+  */
+  int cross_count  = cross_staff_count ();
 
-  int first = 1;
-  int last = encompass_arr_.size () - 1;
-  if (encompass_arr_[0] != spanned_drul_[LEFT])
+  /*
+    URG
+  */
+  bool cross_b = cross_count && cross_count < encompass_arr.size ();
+  if (encompass_arr[0] != get_bound (LEFT))
     {
-      first = 0;
-      left_x = spanned_drul_[LEFT]->width ().length ();
-      left_x -= 2 * notewidth;
-//      left_y = 0;
-      Stem* stem = encompass_arr_[last]->stem_l_;
-      left_y = stem->dir_ == dir_ ? stem->stem_end_f ()
-       : stem->stem_begin_f () + 0.5 * dir_;
-      dy = 0;
+      first--;
+      Real is = calc_interstaff_dist (encompass_arr[0], this);
+      if (cross_b)
+       offset_arr[0][Y_AXIS] += is;
     }
-  if (encompass_arr_.top () != spanned_drul_[RIGHT])
+
+  /*
+    right is broken edge
+  */
+  if (encompass_arr.top () != get_bound (RIGHT))
     {
-      last += 1;
-      dy = 0;
+      last++;
     }
 
-  Array<Offset> notes;
-  notes.push (Offset (0,0));
-  for (int i = first; i < last; i++)
+  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 ();
+      Offset o (encompass_offset (encompass_arr[i]));
+      offset_arr.push (o - origin);
+    }
 
-      if (stem->dir_ != dir_)
-       x += 0.5 * notewidth;
-      else if (stem->dir_ == UP)
-       x += 1.0 * notewidth;
+  offset_arr.push (Offset (spanner_length (), 0) + get_attachment (RIGHT));
 
-      x -= left_x;
+  return offset_arr;
+}
 
-      Real y = stem->dir_ == dir_ ? stem->stem_end_f ()
-       : stem->stem_begin_f () + 0.5 * dir_;
 
-      /*
-       leave a gap: slur mustn't touch head/stem
-       */
-      y += 2.5 * dir_;
+Array<Rod>
+Slur::get_rods () const
+{
+  Array<Rod> a;
+  Rod r;
+  
+  r.item_l_drul_[LEFT] = get_bound (LEFT);
+  r.item_l_drul_[RIGHT] = get_bound (RIGHT);
+  r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
+
+  a.push (r);
+  return a;
+}
+
+
+/*
+  Ugh should have dash-length + dash-period
+ */
+GLUE_SCORE_ELEMENT(Slur,brew_molecule);
+SCM
+Slur::member_brew_molecule () const
+{
+  Real thick = paper_l ()->get_var ("slur_thickness");
+  Bezier one = get_curve ();
+
+  Molecule a;
+  SCM d =  get_elt_property ("dashed");
+  if (gh_number_p (d))
+    a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
+  else
+    a = lookup_l ()->slur (one, Directional_element_interface (this).get () * thick, thick);
 
-      // ugh: huh?
-      if (dir_ == DOWN)
-       y += 1.5 * dir_;
+  return a.create_scheme();
+}
+
+void
+Slur::set_control_points ()
+{
+  Slur_bezier_bow bb (get_encompass_offset_arr (),
+                     Directional_element_interface (this).get ());
 
-      y *= internote;
-      y -= left_y;
+  Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
+  Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
+  Real r_0 = paper_l ()->get_var ("slur_ratio");
 
-      notes.push (Offset (x, y));
+  bb.set_default_bezier (h_inf, r_0);
+
+  if (bb.fit_factor () > 1.0)
+    {
+      Real length = bb.curve_.control_[3][X_AXIS]; 
+      Real default_height = bb.get_default_height (h_inf, r_0, length);
+      bb.minimise_enclosed_area (paper_l(), default_height);
+      
+      Real bff = paper_l ()->get_var ("slur_force_blowfit");
+      bb.curve_.control_[1][Y_AXIS] *= bff;
+      bb.curve_.control_[2][Y_AXIS] *= bff;
+      bb.blow_fit ();
+
+      Real sb = paper_l ()->get_var ("slur_beautiful");
+      Real beautiful = length * default_height * sb;
+      Real area = bb.enclosed_area_f ();
+      
+      /*
+       Slurs that fit beautifully are not ugly
+      */
+      if (area > beautiful)
+       de_uglyfy (&bb, default_height);
     }
 
-  notes.push (Offset (dx, dy));
+  Bezier b = bb.get_bezier ();
+
+
+  SCM controls = SCM_EOL;
+  for (int i= 4; i--;)
+    controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
+
+  set_elt_property ("control-points", controls);
+}
+  
+  
+Bezier
+Slur::get_curve () const
+{
+  Bezier b;
+  int i = 0;
 
-  return notes;
+  if (!Directional_element_interface (this).get ()
+      || ! gh_symbol_p (index_cell (get_elt_property ("attachment"), LEFT)))
+    ((Slur*)this)->set_extremities ();
+  
+  if (!gh_pair_p (get_elt_property ("control-points")))
+    ((Slur*)this)->set_control_points ();
+  
+  
+  for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
+    {
+      b.control_[i] = ly_scm2offset (gh_car (s));
+      i++;
+    }
+  
+  Array<Offset> enc (get_encompass_offset_arr ());
+  Direction dir = Directional_element_interface (this).get ();
+  
+  Real x1 = enc[0][X_AXIS];
+  Real x2 = enc.top ()[X_AXIS];
+  
+  Real off = 0.0;
+  for (int i=1; i < enc.size ()-1; i++)
+    {
+      Real x = enc[i][X_AXIS];
+      if (x > x1 && x <x2)
+       {
+         Real y = b.get_other_coordinate (X_AXIS, x);
+         off = off >? dir *  (enc[i][Y_AXIS] - y);
+       }
+    }
+  b.translate (Offset (0, dir * off));
+  return b;
 }