]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/stem.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / stem.cc
index 8a755afaa5aec63946003a19017be335f0c39b80..39a77a3858fea8ab3d6a7da92df1f26182830439 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 1996--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 1996--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
 
   TODO: This is way too hairy
@@ -127,10 +127,13 @@ Stem::set_stem_positions (Grob *me, Real se)
   if (d && d * head_positions (me)[get_grob_direction (me)] >= se * d)
     me->warning (_ ("weird stem size, check for narrow beams"));
 
-  Interval height = me->pure_height (me, 0, INT_MAX);
+  // trigger note collision mechanisms
+  Real stem_beg = internal_calc_stem_begin_position (me, false);
   Real staff_space = Staff_symbol_referencer::staff_space (me);
-  Real half_space =  staff_space * 0.5;
+  Real half_space = staff_space * 0.5;
 
+  Interval height;
+  height[-d] = stem_beg * half_space;
   height[d] = se * half_space + beam_end_corrective (me);
 
   Real stemlet_length = robust_scm2double (me->get_property ("stemlet-length"),
@@ -149,8 +152,8 @@ Stem::set_stem_positions (Grob *me, Real se)
 
           height[-d] = (height[d] - d
                         * (0.5 * beam_thickness
-                        + beam_translation * max (0, (beam_count - 1))
-                        + stemlet_length));
+                           + beam_translation * max (0, (beam_count - 1))
+                           + stemlet_length));
         }
       else if (!stemlet && beam)
         height[-d] = height[d];
@@ -160,7 +163,6 @@ Stem::set_stem_positions (Grob *me, Real se)
 
   me->set_property ("stem-begin-position", scm_from_double (height[-d] * 2 / staff_space));
   me->set_property ("length", scm_from_double (height.length () * 2 / staff_space));
-  (void) me->extent (me, Y_AXIS);
 }
 
 /* Note head that determines hshift for upstems
@@ -223,8 +225,7 @@ Stem::extremal_heads (Grob *me)
       Grob *n = heads[i];
       int p = Staff_symbol_referencer::get_rounded_position (n);
 
-      Direction d = LEFT;
-      do
+      for (LEFT_and_RIGHT (d))
         {
           if (d * p > d * extpos[d])
             {
@@ -232,23 +233,27 @@ Stem::extremal_heads (Grob *me)
               extpos[d] = p;
             }
         }
-      while (flip (&d) != DOWN);
     }
   return exthead;
 }
 
-/* The positions, in ascending order.  */
+/* The staff positions, in ascending order.
+ * If FILTER, include the main column of noteheads only */
 vector<int>
-Stem::note_head_positions (Grob *me)
+Stem::note_head_positions (Grob *me, bool filter)
 {
   vector<int> ps;
   extract_grob_set (me, "note-heads", heads);
+  Grob *xref = common_refpoint_of_array (heads, me, X_AXIS);
 
   for (vsize i = heads.size (); i--;)
     {
       Grob *n = heads[i];
-      int p = Staff_symbol_referencer::get_rounded_position (n);
+      if (filter
+          && n->relative_coordinate (xref, X_AXIS) != 0.0)
+        continue;
 
+      int p = Staff_symbol_referencer::get_rounded_position (n);
       ps.push_back (p);
     }
 
@@ -270,15 +275,21 @@ Stem::add_head (Grob *me, Grob *n)
 bool
 Stem::is_invisible (Grob *me)
 {
-  return !is_normal_stem (me)
-         && (robust_scm2double (me->get_property ("stemlet-length"),
-                                0.0) == 0.0);
+  if (is_normal_stem (me))
+    return false;
+  else if (head_count (me))
+    return true;
+  else // if there are no note-heads, we might want stemlets
+    return 0.0 == robust_scm2double (me->get_property ("stemlet-length"), 0.0);
 }
 
 bool
 Stem::is_normal_stem (Grob *me)
 {
-  return head_count (me) && scm_to_int (me->get_property ("duration-log")) >= 1;
+  if (!head_count (me))
+    return false;
+
+  return scm_to_int (me->get_property ("duration-log")) >= 1;
 }
 
 MAKE_SCHEME_CALLBACK (Stem, pure_height, 3)
@@ -303,25 +314,46 @@ Stem::internal_pure_height (Grob *me, bool calc_beam)
 
   if (!beam)
     return iv;
-  if (!to_boolean (me->get_property ("cross-staff")) && calc_beam)
+  if (calc_beam)
     {
       Interval overshoot;
       Direction dir = get_grob_direction (me);
-      Direction d = DOWN;
-      do
+      for (DOWN_and_UP (d))
         overshoot[d] = d == dir ? dir * infinity_f : iv[d];
-      while (flip (&d) != DOWN);
 
       vector<Interval> heights;
       vector<Grob *> my_stems;
       extract_grob_set (beam, "normal-stems", normal_stems);
       for (vsize i = 0; i < normal_stems.size (); i++)
-        if (normal_stems[i] != me && get_grob_direction (normal_stems[i]) == dir)
+        if (get_grob_direction (normal_stems[i]) == dir)
           {
-            heights.push_back (Stem::internal_pure_height (normal_stems[i], false));
+            if (normal_stems[i] != me)
+              heights.push_back (Stem::internal_pure_height (normal_stems[i], false));
+            else
+              heights.push_back (iv);
             my_stems.push_back (normal_stems[i]);
-            iv.unite (heights.back ());
           }
+      //iv.unite (heights.back ());
+      // look for cross staff effects
+      vector<Real> coords;
+      Grob *common = common_refpoint_of_array (my_stems, me, Y_AXIS);
+      Real min_pos = infinity_f;
+      Real max_pos = -infinity_f;
+      for (vsize i = 0; i < my_stems.size (); i++)
+        {
+          coords.push_back (my_stems[i]->pure_relative_y_coordinate (common, 0, INT_MAX));
+          min_pos = min (min_pos, coords[i]);
+          max_pos = max (max_pos, coords[i]);
+        }
+      for (vsize i = 0; i < heights.size (); i++)
+        {
+          heights[i][dir] += dir == DOWN
+                             ? coords[i] - max_pos
+                             : coords[i] - min_pos;
+        }
+
+      for (vsize i = 0; i < heights.size (); i++) iv.unite (heights[i]);
+
       for (vsize i = 0; i < my_stems.size (); i++)
         cache_pure_height (my_stems[i], iv, heights[i]);
       iv.intersect (overshoot);
@@ -335,10 +367,8 @@ Stem::cache_pure_height (Grob *me, Interval iv, Interval my_iv)
 {
   Interval overshoot;
   Direction dir = get_grob_direction (me);
-  Direction d = DOWN;
-  do
+  for (DOWN_and_UP (d))
     overshoot[d] = d == dir ? dir * infinity_f : my_iv[d];
-  while (flip (&d) != DOWN);
 
   iv.intersect (overshoot);
   dynamic_cast<Item *> (me)->cache_pure_height (iv);
@@ -391,7 +421,6 @@ Stem::internal_calc_stem_end_position (Grob *me, bool calc_beam)
   if (scm_is_pair (s))
     length = 2 * scm_to_double (robust_list_ref (durlog - 2, s));
 
-
   /* Stems in unnatural (forced) direction should be shortened,
      according to [Roush & Gourlay] */
   Interval hp = head_positions (me);
@@ -517,7 +546,8 @@ Stem::calc_positioning_done (SCM smob)
           = hed->extent (hed, X_AXIS).linear_combination (CENTER)
             - heads[i]->extent (heads[i], X_AXIS).linear_combination (CENTER);
 
-      heads[i]->translate_axis (amount, X_AXIS);
+      if (!isnan (amount)) // empty heads can produce NaN
+        heads[i]->translate_axis (amount, X_AXIS);
     }
   bool parity = true;
   Real lastpos = Real (Staff_symbol_referencer::get_position (heads[0]));
@@ -632,7 +662,7 @@ Stem::height (SCM smob)
   return ly_interval2scm (internal_height (me, true));
 }
 
-Grob*
+Grob *
 Stem::get_reference_head (Grob *me)
 {
   return to_boolean (me->get_property ("avoid-note-head"))
@@ -661,7 +691,7 @@ Interval
 Stem::internal_height (Grob *me, bool calc_beam)
 {
   Grob *beam = get_beam (me);
-  if (!is_valid_stem (me) && ! beam)
+  if (!is_valid_stem (me) && !beam)
     return Interval ();
 
   Direction dir = get_grob_direction (me);
@@ -672,20 +702,27 @@ Stem::internal_height (Grob *me, bool calc_beam)
       (void) beam->get_property ("quantized-positions");
     }
 
+  /*
+    If there is a beam but no stem, slope calculations depend on this
+    routine to return where the stem end /would/ be.
+  */
+  if (calc_beam && !beam && !unsmob_stencil (me->get_property ("stencil")))
+    return Interval ();
+
   Real y1 = robust_scm2double ((calc_beam
                                 ? me->get_property ("stem-begin-position")
                                 : me->get_pure_property ("stem-begin-position", 0, INT_MAX)),
                                0.0);
 
   Real y2 = dir * robust_scm2double ((calc_beam
-                                     ? me->get_property ("length")
-                                     : me->get_pure_property ("length", 0, INT_MAX)),
-                                      0.0)
-                + y1;
+                                      ? me->get_property ("length")
+                                      : me->get_pure_property ("length", 0, INT_MAX)),
+                                     0.0)
+            + y1;
 
   Real half_space = Staff_symbol_referencer::staff_space (me) * 0.5;
 
-  Interval stem_y  = Interval (min (y1, y2), max (y2, y1)) * half_space;
+  Interval stem_y = Interval (min (y1, y2), max (y2, y1)) * half_space;
 
   return stem_y;
 }
@@ -759,12 +796,40 @@ Stem::internal_calc_stem_begin_position (Grob *me, bool calc_beam)
       Real y_attach = Note_head::stem_attachment_coordinate (head, Y_AXIS);
 
       y_attach = head_height.linear_combination (y_attach);
-      pos += d * y_attach * 2 / ss;
+      if (!isinf (y_attach) && !isnan (y_attach)) // empty heads
+        pos += d * y_attach * 2 / ss;
     }
 
   return pos;
 }
 
+
+MAKE_SCHEME_CALLBACK (Stem, pure_calc_length, 3);
+SCM
+Stem::pure_calc_length (SCM smob, SCM /*start*/, SCM /*end*/)
+{
+  Grob *me = unsmob_grob (smob);
+  Real beg = robust_scm2double (me->get_pure_property ("stem-begin-position", 0, INT_MAX), 0.0);
+  Real res = fabs (internal_calc_stem_end_position (me, false) - beg);
+  return scm_from_double (res);
+}
+
+MAKE_SCHEME_CALLBACK (Stem, calc_length, 1);
+SCM
+Stem::calc_length (SCM smob)
+{
+  Grob *me = unsmob_grob (smob);
+  if (unsmob_grob (me->get_object ("beam")))
+    {
+      me->programming_error ("ly:stem::calc-length called but will not be used for beamed stem.");
+      return scm_from_double (0.0);
+    }
+
+  Real beg = robust_scm2double (me->get_property ("stem-begin-position"), 0.0);
+  Real res = fabs (internal_calc_stem_end_position (me, true) - beg);
+  return scm_from_double (res);
+}
+
 bool
 Stem::is_valid_stem (Grob *me)
 {
@@ -776,9 +841,6 @@ Stem::is_valid_stem (Grob *me)
   if (!lh && !beam)
     return false;
 
-  if (lh && robust_scm2int (lh->get_property ("duration-log"), 0) < 1)
-    return false;
-
   if (is_invisible (me))
     return false;
 
@@ -799,7 +861,7 @@ Stem::print (SCM smob)
 
   Real half_space = Staff_symbol_referencer::staff_space (me) * 0.5;
 
-  Interval stem_y  = Interval (min (y1, y2), max (y2, y1)) * half_space;
+  Interval stem_y = Interval (min (y1, y2), max (y2, y1)) * half_space;
 
   stem_y[dir] -= beam_end_corrective (me);
 
@@ -847,10 +909,13 @@ Stem::offset_callback (SCM smob)
 
       Direction d = get_grob_direction (me);
       Real real_attach = head_wid.linear_combination (d * attach);
-      Real r = real_attach;
+      Real r = isnan(real_attach)? 0.0: real_attach;
 
       /* If not centered: correct for stem thickness.  */
-      if (attach)
+      string style = robust_symbol2string (f->get_property ("style"), "default");
+      if (attach && style != "mensural"
+          && style != "neomensural"
+          && style != "petrucci")
         {
           Real rule_thick = thickness (me);
           r += -d * rule_thick * 0.5;
@@ -985,7 +1050,7 @@ Stem::calc_stem_info (SCM smob)
 
   Also, not for knees.  Seems to be a good thing. */
   bool no_extend = to_boolean (me->get_property ("no-stem-extend"));
-  bool is_knee = to_boolean (beam->get_property ("knee"));
+  bool is_knee = Beam::is_knee (beam);
   if (!no_extend && !is_knee)
     {
       /* Highest beam of (UP) beam must never be lower than middle
@@ -1045,7 +1110,7 @@ Stem::calc_cross_staff (SCM smob)
   return scm_from_bool (is_cross_staff (unsmob_grob (smob)));
 }
 
-Grob*
+Grob *
 Stem::flag (Grob *me)
 {
   return unsmob_grob (me->get_object ("flag"));
@@ -1093,6 +1158,7 @@ ADD_INTERFACE (Stem,
                "length "
                "length-fraction "
                "max-beam-connect "
+               "melody-spanner "
                "neutral-direction "
                "no-stem-extend "
                "note-heads "
@@ -1103,6 +1169,7 @@ ADD_INTERFACE (Stem,
                "stemlet-length "
                "thickness "
                "tremolo-flag "
+               "tuplet-start "
               );
 
 /****************************************************************/