]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/ledger-line-spanner.cc
unsmob_pitch -> Pitch::unsmob and related
[lilypond.git] / lily / ledger-line-spanner.cc
index 01238222147c9afbf4f33a2e6b62ac2018a46c9f..f4bf36ef48cf8e64f574d5ad9d19fccb190a1fe1 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
   LilyPond is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -32,80 +32,23 @@ struct Ledger_line_spanner
 {
   DECLARE_SCHEME_CALLBACK (print, (SCM));
   DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
-  static Stencil brew_ledger_lines (Grob *me,
-                                    int pos,
-                                    Interval,
-                                    Real, Real,
-                                    Interval x_extent,
-                                    Real left_shorten);
-
   DECLARE_GROB_INTERFACE ();
 };
 
-Stencil
-Ledger_line_spanner::brew_ledger_lines (Grob *staff,
-                                        int pos,
-                                        Interval staff_extent,
-                                        Real halfspace,
-                                        Real ledgerlinethickness,
-                                        Interval x_extent,
-                                        Real left_shorten)
-{
-  int line_count = (staff_extent.contains (pos)
-                    ? 0
-                    : sign (pos) * int (rint (pos - staff_extent[Direction (sign (pos))])) / 2);
-  Stencil stencil;
-  if (line_count)
-    {
-      Real blotdiameter = ledgerlinethickness;
-      Interval y_extent
-        = Interval (-0.5 * (ledgerlinethickness),
-                    +0.5 * (ledgerlinethickness));
-      Stencil proto_ledger_line
-        = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
-
-      x_extent[LEFT] += left_shorten;
-      Stencil proto_first_line
-        = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
-
-      Direction dir = (Direction)sign (pos);
-      Real offs = (Staff_symbol_referencer::on_line (staff, pos))
-                  ? 0.0
-                  : -dir * halfspace;
-
-      offs += pos * halfspace;
-      for (int i = 0; i < line_count; i++)
-        {
-          Stencil ledger_line ((i == 0)
-                               ? proto_first_line
-                               : proto_ledger_line);
-          ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
-          stencil.add_stencil (ledger_line);
-        }
-    }
-
-  return stencil;
-}
-
 static void
 set_rods (Drul_array<Interval> const &current_extents,
           Drul_array<Interval> const &previous_extents,
           Item *current_column,
           Item *previous_column,
-          Real min_length_fraction)
+          Real min_length)
 {
-  Direction d = UP;
-  do
+  for (UP_and_DOWN (d))
     {
       if (!current_extents[d].is_empty ()
           && !previous_extents[d].is_empty ())
         {
-          Real total_head_length = previous_extents[d].length ()
-                                   + current_extents[d].length ();
-
           Rod rod;
-          rod.distance_ = total_head_length
-                          * (3 / 2 * min_length_fraction)
+          rod.distance_ = 2 * min_length
                           /*
                             we go from right to left.
                           */
@@ -117,14 +60,13 @@ set_rods (Drul_array<Interval> const &current_extents,
           rod.add_to_cols ();
         }
     }
-  while (flip (&d) != DOWN);
 }
 
 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
 SCM
 Ledger_line_spanner::set_spacing_rods (SCM smob)
 {
-  Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
+  Spanner *me = dynamic_cast<Spanner *> (Grob::unsmob (smob));
 
   // find size of note heads.
   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
@@ -139,6 +81,7 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
 
   Drul_array<Interval> current_extents;
   Drul_array<Interval> previous_extents;
+  Real current_head_width = 0.0;
   Item *previous_column = 0;
   Item *current_column = 0;
 
@@ -157,7 +100,12 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
       Item *h = heads[i];
 
       int pos = Staff_symbol_referencer::get_rounded_position (h);
-      if (staff_extent.contains (pos))
+      if  (Staff_symbol::ledger_positions (staff, pos).empty ())
+        continue;
+
+      /* Ambitus heads can appear out-of-order in heads[],
+       * but as part of prefatory matter, they need no rods */
+      if (h->internal_has_interface (ly_symbol2scm ("ambitus-interface")))
         continue;
 
       Item *column = h->get_column ();
@@ -165,7 +113,7 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
         {
           set_rods (current_extents, previous_extents,
                     current_column, previous_column,
-                    min_length_fraction);
+                    current_head_width * min_length_fraction);
 
           previous_column = current_column;
           current_column = column;
@@ -173,6 +121,7 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
 
           current_extents[DOWN].set_empty ();
           current_extents[UP].set_empty ();
+          current_head_width = 0.0;
         }
 
       Interval head_extent = h->extent (column, X_AXIS);
@@ -181,12 +130,13 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
         continue;
 
       current_extents[vdir].unite (head_extent);
+      current_head_width = max (current_head_width, head_extent.length ());
     }
 
   if (previous_column && current_column)
     set_rods (current_extents, previous_extents,
               current_column, previous_column,
-              min_length_fraction);
+              current_head_width * min_length_fraction);
 
   return SCM_UNSPECIFIED;
 }
@@ -196,7 +146,6 @@ struct Ledger_request
   Interval ledger_extent_;
   Interval head_extent_;
   int position_;
-  bool excentric_;
   Ledger_request ()
   {
     ledger_extent_.set_empty ();
@@ -215,7 +164,7 @@ MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
 SCM
 Ledger_line_spanner::print (SCM smob)
 {
-  Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
+  Spanner *me = dynamic_cast<Spanner *> (Grob::unsmob (smob));
 
   extract_grob_set (me, "note-heads", heads);
 
@@ -236,7 +185,6 @@ Ledger_line_spanner::print (SCM smob)
     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
 
   Stencil ledgers;
-  Stencil default_ledger;
 
   Grob *common[NO_AXES];
 
@@ -245,7 +193,7 @@ Ledger_line_spanner::print (SCM smob)
       Axis a = Axis (i);
       common[a] = common_refpoint_of_array (heads, me, a);
       for (vsize i = heads.size (); i--;)
-        if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
+        if (Grob *g = Grob::unsmob (me->get_object ("accidental-grob")))
           common[a] = common[a]->common_refpoint (g, a);
     }
 
@@ -280,8 +228,7 @@ Ledger_line_spanner::print (SCM smob)
       if (last == reqs.end ())
         continue;
 
-      Direction d = DOWN;
-      do
+      for (DOWN_and_UP (d))
         {
           if (!staff_extent.contains (last->second[d].position_)
               && !staff_extent.contains (i->second[d].position_))
@@ -290,8 +237,7 @@ Ledger_line_spanner::print (SCM smob)
                 = (last->second[d].head_extent_[RIGHT]
                    + i->second[d].head_extent_[LEFT]) / 2;
 
-              Direction which = LEFT;
-              do
+              for (LEFT_and_RIGHT (which))
                 {
                   Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
 
@@ -306,10 +252,8 @@ Ledger_line_spanner::print (SCM smob)
                   lr.ledger_extent_.at (-which)
                     = which * max (which * lr.ledger_extent_[-which], which * limit);
                 }
-              while (flip (&which) != LEFT);
             }
         }
-      while (flip (&d) != DOWN);
     }
 
   // create ledgers for note heads
@@ -320,38 +264,56 @@ Ledger_line_spanner::print (SCM smob)
       Item *h = dynamic_cast<Item *> (heads[i]);
 
       int pos = Staff_symbol_referencer::get_rounded_position (h);
-      if (!staff_extent.contains (pos - sign (pos)) && !staff_extent.is_empty ())
+      vector<Real> ledger_positions = Staff_symbol::ledger_positions (staff, pos);
+      if (!ledger_positions.empty ())
         {
+          int ledger_count = ledger_positions.size ();
           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
           Interval ledger_size = head_size;
           ledger_size.widen (ledger_size.length () * length_fraction);
 
-          Interval max_size = reqs[h->get_column ()->get_rank ()]
-                              [Direction (sign (pos))].ledger_extent_;
-
-          ledger_size.intersect (max_size);
-          Real left_shorten = 0.0;
-          if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
+          if (pos && !staff_extent.contains (pos))
             {
-              Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
-              Real d
-                = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
-                                                        head_size[LEFT]),
-                                      0.0);
-
-              left_shorten = max (-ledger_size[LEFT] + d, 0.0);
-
-              /*
-                TODO: shorten 2 ledger lines for the case natural +
-                downstem.
-              */
+              Interval max_size = reqs[h->get_column ()->get_rank ()]
+                                  [Direction (sign (pos))].ledger_extent_;
+
+              if (!max_size.is_empty ())
+                ledger_size.intersect (max_size);
             }
 
-          ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
-                                                  halfspace,
-                                                  ledgerlinethickness,
-                                                  ledger_size,
-                                                  left_shorten));
+          for (int i = 0; i < ledger_count; i++)
+            {
+              Real lpos = ledger_positions[i];
+              Interval x_extent = ledger_size;
+
+              if (i == 0)
+                if (Grob *g = Grob::unsmob (h->get_object ("accidental-grob")))
+                  {
+                    Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
+                    Real d
+                      = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
+                                                              head_size[LEFT]),
+                                            0.0);
+
+                    Real left_shorten = max (-ledger_size[LEFT] + d, 0.0);
+
+                    x_extent[LEFT] += left_shorten;
+                    /*
+                      TODO: shorten 2 ledger lines for the case natural +
+                      downstem.
+                    */
+                  }
+
+              Real blotdiameter = ledgerlinethickness;
+              Interval y_extent
+                = Interval (-0.5 * (ledgerlinethickness),
+                            +0.5 * (ledgerlinethickness));
+              Stencil ledger_line
+                = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
+
+              ledger_line.translate_axis ( lpos * halfspace, Y_AXIS);
+              ledgers.add_stencil (ledger_line);
+            }
         }
     }
 
@@ -364,14 +326,15 @@ Ledger_line_spanner::print (SCM smob)
 ADD_INTERFACE (Ledger_line_spanner,
                "This spanner draws the ledger lines of a staff.  This is a"
                " separate grob because it has to process all potential"
-               " collisions between all note heads.",
+               " collisions between all note heads.  The thickness of ledger"
+               " lines is controlled by the @code{ledger-line-thickness}"
+               " property of the @ref{StaffSymbol} grob.",
 
                /* properties */
                "gap "
                "length-fraction "
                "minimum-length-fraction "
                "note-heads "
-               "thickness "
               );
 
 struct Ledgered_interface