]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur.cc
release: 1.3.101
[lilypond.git] / lily / slur.cc
index 4a86f7c96f17285f536cebe84ac19d6a97a4795c..b7b0c687695c594ba27a696fbb9dd4d5e77459b1 100644 (file)
@@ -10,8 +10,8 @@
 /*
   [TODO]
     * fix broken interstaff slurs
+    * should avoid stafflines with horizontal part.
     * 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)
@@ -48,7 +48,7 @@ Slur::add_column (Score_element*me, Score_element*n)
     warning (_ ("Putting slur over rest.  Ignoring."));
   else
     {
-      Pointer_group_interface (me, "note-columns").add_element (n);
+      Pointer_group_interface::add_element (me, "note-columns",n);
       me->add_dependency (n);
     }
 
@@ -122,11 +122,53 @@ Slur::after_line_breaking (SCM smob)
   return SCM_UNSPECIFIED;
 } 
 
+
 void
-Slur::set_extremities (Score_element*me)
+Slur::check_slope (Score_element *me)
+{
+  /*
+    Avoid too steep slurs.
+   */
+  SCM s = me->get_elt_property ("slope-limit");
+  if (gh_number_p (s))
+    {
+      Array<Offset> encompass = get_encompass_offset_arr (me);
+      Drul_array<Offset> attachment;
+      attachment[LEFT] = encompass[0];
+      attachment[RIGHT] = encompass.top ();
+
+      Real dx = attachment[RIGHT][X_AXIS] - attachment[LEFT][X_AXIS];
+      Real dy = attachment[RIGHT][Y_AXIS] - attachment[LEFT][Y_AXIS];
+      if (!dx)
+       return;
+      
+      Real slope = slope = abs (dy / dx);
+
+      Real limit = gh_scm2double (s);
+
+      if (slope > limit)
+       {
+         Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);
+         Direction dir = (Direction)gh_scm2int (me->get_elt_property ("direction"));
+         Direction d = (Direction)(- dir * (sign (dy)));
+         SCM a = me->get_elt_property ("attachment-offset");
+         Drul_array<Offset> o;
+         o[LEFT] = ly_scm2offset (index_cell (a, LEFT));
+         o[RIGHT] = ly_scm2offset (index_cell (a, RIGHT));
+         o[d][Y_AXIS] -= (limit - slope) * dx * dir / staff_space;
+         //o[d][Y_AXIS] = attachment[-d][Y_AXIS] + (dx * limit * dir / staff_space);
+         me->set_elt_property ("attachment-offset",
+                               gh_cons (ly_offset2scm (o[LEFT]),
+                                        ly_offset2scm (o[RIGHT])));
+       }
+    }
+}
+
+void
+Slur::set_extremities (Score_element *me)
 {
   if (!Directional_element_interface::get (me))
-    Directional_element_interface ::set (me,get_default_dir (me));
+    Directional_element_interface::set (me, get_default_dir (me));
 
   Direction dir = LEFT;
   do 
@@ -148,8 +190,11 @@ Slur::set_extremities (Score_element*me)
        }
     }
   while (flip (&dir) != LEFT);
+
+  check_slope (me);
 }
 
+
 Real
 Slur::get_first_notecolumn_y (Score_element *me, Direction dir)
 {
@@ -199,7 +244,10 @@ Slur::broken_trend_offset (Score_element *me, Direction dir)
                                             me->get_elt_property ("direction"));
              Real neighbour_y = get_first_notecolumn_y (neighbour, dir);
              Real y = get_first_notecolumn_y (me, -dir);
-             o = Offset (0, (y + neighbour_y) / 2);
+             int neighbour_cols = scm_ilength (neighbour->get_elt_property ("note-columns"));
+             int cols = scm_ilength (me->get_elt_property ("note-columns"));
+             o = Offset (0, (y*neighbour_cols + neighbour_y*cols) /
+                         (cols + neighbour_cols));
              break;
            }
        }
@@ -208,7 +256,7 @@ Slur::broken_trend_offset (Score_element *me, Direction dir)
 }
 
 Offset
-Slur::get_attachment (Score_element*me,Direction dir,
+Slur::get_attachment (Score_element *me, Direction dir,
                      Score_element **common) 
 {
   SCM s = me->get_elt_property ("attachment");
@@ -220,8 +268,8 @@ Slur::get_attachment (Score_element*me,Direction dir,
   SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
   Spanner*sp = dynamic_cast<Spanner*>(me);
   String str = ly_symbol2string (a);
-  Real ss = Staff_symbol_referencer::staff_space ((Score_element*)me);
-  Real hs = ss / 2.0;
+  Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);
+  Real hs = staff_space / 2.0;
   Offset o;
   
   Score_element *stem = 0;
@@ -238,8 +286,9 @@ Slur::get_attachment (Score_element*me,Direction dir,
              /*
                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::get (me));
+             o += Offset (0.5 * n->extent (n,X_AXIS).length (),
+                          0.5 * staff_space
+                          * Directional_element_interface::get (me));
            }
          else if (str == "alongside-stem")
            {
@@ -247,9 +296,10 @@ Slur::get_attachment (Score_element*me,Direction dir,
              /*
                Default position is on stem X, on outer side of head Y
               */
-             o += Offset (n->extent (X_AXIS).length ()
+             o += Offset (n->extent (n,X_AXIS).length ()
                           * (1 + Stem::get_direction (stem)),
-                          0.5 * ss * Directional_element_interface::get (me));
+                          0.5 * staff_space
+                          * Directional_element_interface::get (me));
            }
          else if (str == "stem")
            {
@@ -258,8 +308,8 @@ Slur::get_attachment (Score_element*me,Direction dir,
                Default position is on stem X, at stem end Y
               */
              o += Offset (0.5 *
-                          (n->extent (X_AXIS).length ()
-                           - stem->extent (X_AXIS).length ())
+                          (n->extent (n,X_AXIS).length ()
+                           - stem->extent (stem,X_AXIS).length ())
                            * (1 + Stem::get_direction (stem)),
                            0);
            }
@@ -295,7 +345,7 @@ int stemdir = stem ? Stem::get_direction (stem) : 1;
 
   if (l != SCM_BOOL_F)
     {
-      o += ly_scm2offset (gh_cdr (l)) * ss * dir;
+      o += ly_scm2offset (gh_cdr (l)) * staff_space * dir;
     }
 
   /*
@@ -308,6 +358,8 @@ int stemdir = stem ? Stem::get_direction (stem) : 1;
        - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
     }
 
+  o += ly_scm2offset (index_cell (me->get_elt_property ("attachment-offset"),
+                                 dir)) * staff_space;
   return o;
 }
 
@@ -335,35 +387,33 @@ Slur::encompass_offset (Score_element*me,
     Simply set x to middle of notehead
    */
 
-  o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
+  o[X_AXIS] -= 0.5 * stem_dir * col->extent (col,X_AXIS).length ();
 
   if ((stem_dir == dir)
-      && !stem_l->extent (Y_AXIS).empty_b ())
+      && !stem_l->extent (stem_l, Y_AXIS).empty_b ())
     {
-      o[Y_AXIS] = stem_l->relative_coordinate (common[Y_AXIS], Y_AXIS); // iuhg
-      o[Y_AXIS] += stem_l->extent (Y_AXIS)[dir];
+      o[Y_AXIS] = stem_l->extent (common[Y_AXIS], Y_AXIS)[dir];
     }
   else
     {
-      o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);   // ugh
-      o[Y_AXIS] += col->extent (Y_AXIS)[dir];
+      o[Y_AXIS] = col->extent (common[Y_AXIS], Y_AXIS)[dir];
     }
 
   /*
    leave a gap: slur mustn't touch head/stem
    */
   o[Y_AXIS] += dir * gh_scm2double (me->get_elt_property ("y-free")) *
-    me->paper_l ()->get_var ("staffspace");
+    1.0;
   return o;
 }
 
 Array<Offset>
-Slur::get_encompass_offset_arr (Score_element*me) 
+Slur::get_encompass_offset_arr (Score_element *me)
 {
   Spanner*sp = dynamic_cast<Spanner*>(me);
   SCM eltlist = me->get_elt_property ("note-columns");
-  Score_element *common[] = {me->common_refpoint (eltlist,X_AXIS),
-                            me->common_refpoint (eltlist,Y_AXIS)};
+  Score_element *common[] = {me->common_refpoint (eltlist, X_AXIS),
+                            me->common_refpoint (eltlist, Y_AXIS)};
 
 
   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
@@ -440,7 +490,7 @@ Slur::set_spacing_rods (SCM smob)
   r.item_l_drul_[RIGHT] = sp->get_bound (RIGHT);
   r.distance_f_ =
     gh_scm2double (me->get_elt_property ("minimum-length"))
-    * me->paper_l ()->get_var ("staffspace");
+    * 1.0;
 
   r.add_to_cols ();
   return SCM_UNSPECIFIED;
@@ -465,12 +515,16 @@ Slur::brew_molecule (SCM smob)
     gh_scm2double (me->get_elt_property ("thickness"));
   Bezier one = get_curve (me);
 
+  // get_curve may suicide
+  if (!scm_ilength (me->get_elt_property ("note-columns")))
+    return SCM_EOL;
+
   Molecule a;
   SCM d =  me->get_elt_property ("dashed");
   if (gh_number_p (d))
-    a = me->lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
+    a = Lookup::dashed_slur (one, thick, thick * gh_scm2double (d));
   else
-    a = me->lookup_l ()->slur (one, Directional_element_interface::get (me) * thick, thick);
+    a = Lookup::slur (one, Directional_element_interface::get (me) * thick, thick);
 
   return a.create_scheme();
 }
@@ -496,10 +550,12 @@ Slur::set_control_points (Score_element*me)
       Real length = bb.curve_.control_[3][X_AXIS]; 
       Real default_height = slur_height (length, h_inf, r_0);
 
-      SCM ssb = scm_assq (ly_symbol2scm ("beautiful"), details);
-      Real sb =gh_scm2double (gh_cdr (ssb));
+      SCM ssb = me->get_elt_property ("beautiful");
+      Real sb = 0;
+      if (gh_number_p (ssb))
+       sb = gh_scm2double (ssb);
 
-      bb.minimise_enclosed_area (me->paper_l(), sb);
+      bb.minimise_enclosed_area ( sb, details);
       SCM sbf = scm_assq (ly_symbol2scm ("force-blowfit"), details);
       Real bff = 1.0;
       if (gh_pair_p (sbf) && gh_number_p (gh_cdr (sbf)))
@@ -532,7 +588,10 @@ Slur::set_control_points (Score_element*me)
        All these null control-points, where do they all come from?
       */
       if (i && b.control_[i][X_AXIS] == 0)
-       me->suicide ();
+       {
+         me->suicide ();
+         return;
+       }
     }
 
   me->set_elt_property ("control-points", controls);
@@ -545,12 +604,17 @@ Slur::get_curve (Score_element*me)
   int i = 0;
 
   if (!Directional_element_interface::get (me)
-      || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT)))
+      || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT))
+      || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), RIGHT)))
     set_extremities (me);
   
   if (!gh_pair_p (me->get_elt_property ("control-points")))
     set_control_points (me);
 
+  // set_control_points may suicide
+  if (!scm_ilength (me->get_elt_property ("note-columns")))
+    return b;
+
   for (SCM s= me->get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
     {
       b.control_[i] = ly_scm2offset (gh_car (s));