]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/stem.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / stem.cc
index 8069a456c814e48cf24bddec96bf9d8fae2279ac..9d32e0b625d4a83ac7a306d37d39a9e9b477c425 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 1996--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 1996--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
 
   TODO: This is way too hairy
@@ -41,7 +41,6 @@
 #include "spanner.hh"
 
 #include <cmath>                // rint
-using namespace std;
 
 #include "beam.hh"
 #include "directional-element-interface.hh"
@@ -61,6 +60,9 @@ using namespace std;
 #include "stem-tremolo.hh"
 #include "warn.hh"
 
+using std::string;
+using std::vector;
+
 void
 Stem::set_beaming (Grob *me, int beam_count, Direction d)
 {
@@ -92,7 +94,7 @@ Stem::get_beaming (Grob *me, Direction d)
   SCM lst = index_get_cell (pair, d);
 
   int len = scm_ilength (lst);
-  return max (len, 0);
+  return std::max (len, 0);
 }
 
 Interval
@@ -110,10 +112,10 @@ Stem::head_positions (Grob *me)
 Real
 Stem::chord_start_y (Grob *me)
 {
-  Interval hp = head_positions (me);
-  if (!hp.is_empty ())
-    return hp[get_grob_direction (me)] * Staff_symbol_referencer::staff_space (me)
-           * 0.5;
+  if (head_count (me))
+    return Staff_symbol_referencer::get_position (last_head (me))
+      * Staff_symbol_referencer::staff_space (me) * 0.5;
+
   return 0;
 }
 
@@ -123,7 +125,7 @@ Stem::set_stem_positions (Grob *me, Real se)
   // todo: margins
   Direction d = get_grob_direction (me);
 
-  Grob *beam = unsmob_grob (me->get_object ("beam"));
+  Grob *beam = unsmob<Grob> (me->get_object ("beam"));
   if (d && d * head_positions (me)[get_grob_direction (me)] >= se * d)
     me->warning (_ ("weird stem size, check for narrow beams"));
 
@@ -152,7 +154,7 @@ Stem::set_stem_positions (Grob *me, Real se)
 
           height[-d] = (height[d] - d
                         * (0.5 * beam_thickness
-                           + beam_translation * max (0, (beam_count - 1))
+                           + beam_translation * std::max (0, (beam_count - 1))
                            + stemlet_length));
         }
       else if (!stemlet && beam)
@@ -257,7 +259,7 @@ Stem::note_head_positions (Grob *me, bool filter)
       ps.push_back (p);
     }
 
-  vector_sort (ps, less<int> ());
+  vector_sort (ps, std::less<int> ());
   return ps;
 }
 
@@ -266,18 +268,21 @@ Stem::add_head (Grob *me, Grob *n)
 {
   n->set_object ("stem", me->self_scm ());
 
-  if (Note_head::has_interface (n))
+  if (has_interface<Note_head> (n))
     Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), n);
-  else if (Rest::has_interface (n))
+  else if (has_interface<Rest> (n))
     Pointer_group_interface::add_grob (me, ly_symbol2scm ("rests"), 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
@@ -286,9 +291,7 @@ Stem::is_normal_stem (Grob *me)
   if (!head_count (me))
     return false;
 
-  extract_grob_set (me, "note-heads", heads);
-  SCM style = heads[0]->get_property ("style");
-  return style != ly_symbol2scm ("kievan") && scm_to_int (me->get_property ("duration-log")) >= 1;
+  return scm_to_int (me->get_property ("duration-log")) >= 1;
 }
 
 MAKE_SCHEME_CALLBACK (Stem, pure_height, 3)
@@ -297,7 +300,7 @@ Stem::pure_height (SCM smob,
                    SCM /* start */,
                    SCM /* end */)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return ly_interval2scm (internal_pure_height (me, true));
 }
 
@@ -307,7 +310,7 @@ Stem::internal_pure_height (Grob *me, bool calc_beam)
   if (!is_normal_stem (me))
     return Interval (0.0, 0.0);
 
-  Grob *beam = unsmob_grob (me->get_object ("beam"));
+  Grob *beam = unsmob<Grob> (me->get_object ("beam"));
 
   Interval iv = internal_height (me, false);
 
@@ -341,8 +344,8 @@ Stem::internal_pure_height (Grob *me, bool calc_beam)
       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]);
+          min_pos = std::min (min_pos, coords[i]);
+          max_pos = std::max (max_pos, coords[i]);
         }
       for (vsize i = 0; i < heights.size (); i++)
         {
@@ -377,7 +380,7 @@ MAKE_SCHEME_CALLBACK (Stem, calc_stem_end_position, 1)
 SCM
 Stem::calc_stem_end_position (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return scm_from_double (internal_calc_stem_end_position (me, true));
 }
 
@@ -387,7 +390,7 @@ Stem::pure_calc_stem_end_position (SCM smob,
                                    SCM, /* start */
                                    SCM /* end */)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return scm_from_double (internal_calc_stem_end_position (me, false));
 }
 
@@ -427,7 +430,7 @@ Stem::internal_calc_stem_end_position (Grob *me, bool calc_beam)
     {
       SCM sshorten = ly_assoc_get (ly_symbol2scm ("stem-shorten"), details, SCM_EOL);
       SCM scm_shorten = scm_is_pair (sshorten)
-                        ? robust_list_ref (max (duration_log (me) - 2, 0), sshorten) : SCM_EOL;
+                        ? robust_list_ref (std::max (duration_log (me) - 2, 0), sshorten) : SCM_EOL;
       Real shorten_property = 2 * robust_scm2double (scm_shorten, 0);
       /*  change in length between full-size and shortened stems is executed gradually.
           "transition area" = stems between full-sized and fully-shortened.
@@ -439,14 +442,14 @@ Stem::internal_calc_stem_end_position (Grob *me, bool calc_beam)
           (but not greater than 1/2 and not smaller than 1/4).
           value 6 is heuristic; it determines the suggested transition slope steepnesas.
           */
-      Real shortening_step = min (max (0.25, (shorten_property / 6)), 0.5);
+      Real shortening_step = std::min (std::max (0.25, (shorten_property / 6)), 0.5);
       /*  Shortening of unflagged stems should begin on the first stem that sticks
           more than 1 staffspace (2 units) out of the staff.
           Shortening of flagged stems begins in the same moment as unflagged ones,
           but not earlier than on the middle line note.
           */
-      Real which_step = (min (1.0, quarter_stem_length - (2 * staff_rad) - 2.0)) + abs (hp[dir]);
-      Real shorten = min (max (0.0, (shortening_step * which_step)), shorten_property);
+      Real which_step = (std::min (1.0, quarter_stem_length - (2 * staff_rad) - 2.0)) + abs (hp[dir]);
+      Real shorten = std::min (std::max (0.0, (shortening_step * which_step)), shorten_property);
 
       length -= shorten;
     }
@@ -454,8 +457,8 @@ Stem::internal_calc_stem_end_position (Grob *me, bool calc_beam)
   length *= robust_scm2double (me->get_property ("length-fraction"), 1.0);
 
   /* Tremolo stuff.  */
-  Grob *t_flag = unsmob_grob (me->get_object ("tremolo-flag"));
-  if (t_flag && (!unsmob_grob (me->get_object ("beam")) || !calc_beam))
+  Grob *t_flag = unsmob<Grob> (me->get_object ("tremolo-flag"));
+  if (t_flag && (!unsmob<Grob> (me->get_object ("beam")) || !calc_beam))
     {
       /* Crude hack: add extra space if tremolo flag is there.
 
@@ -480,7 +483,7 @@ Stem::internal_calc_stem_end_position (Grob *me, bool calc_beam)
           if (dir == UP)
             minlen += beam_trans;
         }
-      length = max (length, minlen + 1.0);
+      length = std::max (length, minlen + 1.0);
     }
 
   Real stem_end = dir ? hp[dir] + dir * length : 0;
@@ -505,7 +508,7 @@ MAKE_SCHEME_CALLBACK (Stem, calc_positioning_done, 1);
 SCM
 Stem::calc_positioning_done (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   if (!head_count (me))
     return SCM_BOOL_T;
 
@@ -532,7 +535,8 @@ Stem::calc_positioning_done (SCM smob)
   bool is_harmonic_centered = false;
   for (vsize i = 0; i < heads.size (); i++)
     is_harmonic_centered = is_harmonic_centered
-                           || heads[i]->get_property ("style") == ly_symbol2scm ("harmonic");
+                           || scm_is_eq (heads[i]->get_property ("style"),
+                                         ly_symbol2scm ("harmonic"));
   is_harmonic_centered = is_harmonic_centered && is_invisible (me);
 
   Real w = hed->extent (hed, X_AXIS)[dir];
@@ -545,7 +549,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]));
@@ -573,14 +578,32 @@ Stem::calc_positioning_done (SCM smob)
                 stem 100% whereas reversed heads only overlaps the
                 stem 50%
               */
-
               Real reverse_overlap = 0.5;
-              heads[i]->translate_axis ((ell - thick * reverse_overlap) * d,
-                                        X_AXIS);
+
+              /*
+                However, the first reverse head has to be shifted even
+                more than the full reverse overlap if it is the same
+                height as the first head or there will be a gap
+                because of the head slant (issue 346).
+              */
+
+              if (i == 1 && dy < 0.1)
+                reverse_overlap = 1.1;
 
               if (is_invisible (me))
-                heads[i]->translate_axis (-thick * (2 - reverse_overlap) * d,
-                                          X_AXIS);
+                {
+                  // Semibreves and longer are tucked in considerably
+                  // to be recognizable as chorded rather than
+                  // parallel voices.  During the course of issue 346
+                  // there was a discussion to change this for unisons
+                  // (dy < 0.1) to reduce overlap but without reaching
+                  // agreement and with Gould being rather on the
+                  // overlapping front.
+                  reverse_overlap = 2;
+                }
+
+              heads[i]->translate_axis ((ell - thick * reverse_overlap) * d,
+                                        X_AXIS);
 
               /* TODO:
 
@@ -611,9 +634,9 @@ MAKE_SCHEME_CALLBACK (Stem, calc_direction, 1);
 SCM
 Stem::calc_direction (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   Direction dir = CENTER;
-  if (Grob *beam = unsmob_grob (me->get_object ("beam")))
+  if (Grob *beam = unsmob<Grob> (me->get_object ("beam")))
     {
       SCM ignore_me = beam->get_property ("direction");
       (void) ignore_me;
@@ -634,13 +657,13 @@ MAKE_SCHEME_CALLBACK (Stem, calc_default_direction, 1);
 SCM
 Stem::calc_default_direction (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
 
   Direction dir = CENTER;
   int staff_center = 0;
-  Interval hp = head_positions (me);
-  if (!hp.is_empty ())
+  if (head_count (me))
     {
+      Interval hp = head_positions (me);
       int udistance = (int) (UP * hp[UP] - staff_center);
       int ddistance = (int) (DOWN * hp[DOWN] - staff_center);
 
@@ -656,7 +679,7 @@ MAKE_SCHEME_CALLBACK (Stem, height, 1);
 SCM
 Stem::height (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return ly_interval2scm (internal_height (me, true));
 }
 
@@ -671,7 +694,7 @@ Stem::get_reference_head (Grob *me)
 Real
 Stem::beam_end_corrective (Grob *me)
 {
-  Grob *beam = unsmob_grob (me->get_object ("beam"));
+  Grob *beam = unsmob<Grob> (me->get_object ("beam"));
   Direction dir = get_grob_direction (me);
   if (beam)
     {
@@ -704,7 +727,7 @@ Stem::internal_height (Grob *me, bool calc_beam)
     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")))
+  if (calc_beam && !beam && !unsmob<Stencil> (me->get_property ("stencil")))
     return Interval ();
 
   Real y1 = robust_scm2double ((calc_beam
@@ -720,7 +743,7 @@ Stem::internal_height (Grob *me, bool calc_beam)
 
   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 (std::min (y1, y2), std::max (y2, y1)) * half_space;
 
   return stem_y;
 }
@@ -729,7 +752,7 @@ MAKE_SCHEME_CALLBACK (Stem, width, 1);
 SCM
 Stem::width (SCM e)
 {
-  Grob *me = unsmob_grob (e);
+  Grob *me = unsmob<Grob> (e);
 
   Interval r;
 
@@ -755,7 +778,7 @@ MAKE_SCHEME_CALLBACK (Stem, calc_stem_begin_position, 1);
 SCM
 Stem::calc_stem_begin_position (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return scm_from_double (internal_calc_stem_begin_position (me, true));
 }
 
@@ -765,7 +788,7 @@ Stem::pure_calc_stem_begin_position (SCM smob,
                                      SCM, /* start */
                                      SCM /* end */)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return scm_from_double (internal_calc_stem_begin_position (me, false));
 }
 
@@ -794,26 +817,53 @@ 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)
 {
   /* TODO: make the stem start a direction ?
      This is required to avoid stems passing in tablature chords.  */
+  if (!me)
+    return false;
   Grob *lh = get_reference_head (me);
-  Grob *beam = unsmob_grob (me->get_object ("beam"));
+  Grob *beam = unsmob<Grob> (me->get_object ("beam"));
 
   if (!lh && !beam)
     return false;
 
-  if (lh && robust_scm2int (lh->get_property ("duration-log"), 0) < 1)
-    return false;
-
   if (is_invisible (me))
     return false;
 
@@ -824,7 +874,7 @@ MAKE_SCHEME_CALLBACK (Stem, print, 1);
 SCM
 Stem::print (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   if (!is_valid_stem (me))
     return SCM_EOL;
 
@@ -834,7 +884,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 (std::min (y1, y2), std::max (y2, y1)) * half_space;
 
   stem_y[dir] -= beam_end_corrective (me);
 
@@ -860,13 +910,13 @@ MAKE_SCHEME_CALLBACK (Stem, offset_callback, 1);
 SCM
 Stem::offset_callback (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
 
   extract_grob_set (me, "rests", rests);
   if (rests.size ())
     {
       Grob *rest = rests.back ();
-      Real r = rest->extent (rest, X_AXIS).center ();
+      Real r = robust_relative_extent (rest, rest, X_AXIS).center ();
       return scm_from_double (r);
     }
 
@@ -882,7 +932,7 @@ 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.  */
       string style = robust_symbol2string (f->get_property ("style"), "default");
@@ -904,7 +954,7 @@ Spanner *
 Stem::get_beam (Grob *me)
 {
   SCM b = me->get_object ("beam");
-  return dynamic_cast<Spanner *> (unsmob_grob (b));
+  return unsmob<Spanner> (b);
 }
 
 Stem_info
@@ -923,7 +973,7 @@ MAKE_SCHEME_CALLBACK (Stem, calc_stem_info, 1);
 SCM
 Stem::calc_stem_info (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   Direction my_dir = get_grob_direction (me);
 
   if (!my_dir)
@@ -973,7 +1023,7 @@ Stem::calc_stem_info (SCM smob)
        : 0.0);
 
   Real height_of_my_trem = 0.0;
-  Grob *trem = unsmob_grob (me->get_object ("tremolo-flag"));
+  Grob *trem = unsmob<Grob> (me->get_object ("tremolo-flag"));
   if (trem)
     {
       height_of_my_trem
@@ -998,7 +1048,7 @@ Stem::calc_stem_info (SCM smob)
                               /* stem only extends to center of beam */
                               - 0.5 * beam_thickness;
 
-  ideal_length = max (ideal_length, ideal_minimum_length);
+  ideal_length = std::max (ideal_length, ideal_minimum_length);
 
   /* Convert to Y position, calculate for dir == UP */
   Real note_start
@@ -1028,9 +1078,9 @@ Stem::calc_stem_info (SCM smob)
     {
       /* Highest beam of (UP) beam must never be lower than middle
          staffline */
-      ideal_y = max (ideal_y, 0.0);
+      ideal_y = std::max (ideal_y, 0.0);
       /* Lowest beam of (UP) beam must never be lower than second staffline */
-      ideal_y = max (ideal_y, (-staff_space
+      ideal_y = std::max (ideal_y, (-staff_space
                                - beam_thickness + height_of_my_beams));
     }
 
@@ -1046,7 +1096,7 @@ Stem::calc_stem_info (SCM smob)
           * length_fraction)
        : 0.0);
 
-  Real minimum_length = max (minimum_free, height_of_my_trem)
+  Real minimum_length = std::max (minimum_free, height_of_my_trem)
                         + height_of_my_beams
                         /* stem only extends to center of beam */
                         - 0.5 * beam_thickness;
@@ -1072,7 +1122,7 @@ Stem::beam_multiplicity (Grob *stem)
 bool
 Stem::is_cross_staff (Grob *stem)
 {
-  Grob *beam = unsmob_grob (stem->get_object ("beam"));
+  Grob *beam = unsmob<Grob> (stem->get_object ("beam"));
   return beam && Beam::is_cross_staff (beam);
 }
 
@@ -1080,13 +1130,13 @@ MAKE_SCHEME_CALLBACK (Stem, calc_cross_staff, 1)
 SCM
 Stem::calc_cross_staff (SCM smob)
 {
-  return scm_from_bool (is_cross_staff (unsmob_grob (smob)));
+  return scm_from_bool (is_cross_staff (unsmob<Grob> (smob)));
 }
 
 Grob *
 Stem::flag (Grob *me)
 {
-  return unsmob_grob (me->get_object ("flag"));
+  return unsmob<Grob> (me->get_object ("flag"));
 }
 
 /* FIXME:  Too many properties  */
@@ -1125,6 +1175,7 @@ ADD_INTERFACE (Stem,
                "default-direction "
                "details "
                "direction "
+               "double-stem-separation "
                "duration-log "
                "flag "
                "french-beaming "