]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/ledger-line-spanner.cc
Run `make grand-replace'.
[lilypond.git] / lily / ledger-line-spanner.cc
index bc8a68eeebb25c920e561784b85557c7b90bbf3d..59ae3e55c9b18290c7e043a7012dd8b95cf9bda5 100644 (file)
@@ -3,18 +3,18 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 2004--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include <map>
+using namespace std;
 
-#include "item.hh"
 #include "note-head.hh"
 #include "staff-symbol-referencer.hh"
 #include "staff-symbol.hh"
 #include "lookup.hh"
 #include "spanner.hh"
-#include "group-interface.hh"
+#include "pointer-group-interface.hh"
 #include "paper-column.hh"
 
 struct Ledger_line_spanner
@@ -23,26 +23,26 @@ struct Ledger_line_spanner
   DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
   static Stencil brew_ledger_lines (Grob *me,
                                    int pos,
-                                   int interspaces,
+                                   Interval,
                                    Real, Real,
                                    Interval x_extent,
                                    Real left_shorten);
 
-  static bool has_interface (Grob *);
+  DECLARE_GROB_INTERFACE ();
 };
 
 Stencil
 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
                                        int pos,
-                                       int interspaces,
+                                       Interval staff_extent,
                                        Real halfspace,
                                        Real ledgerlinethickness,
                                        Interval x_extent,
                                        Real left_shorten)
 {
-  int line_count = ((abs (pos) < interspaces)
+  int line_count = (staff_extent.contains (pos)
                    ? 0
-                   : (abs (pos) - interspaces) / 2);
+                   : sign (pos) * int (rint (pos -  staff_extent[Direction (sign (pos))])) / 2);
   Stencil stencil;
   if (line_count)
     {
@@ -58,7 +58,7 @@ Ledger_line_spanner::brew_ledger_lines (Grob *staff,
        = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
 
       Direction dir = (Direction)sign (pos);
-      Real offs = (Staff_symbol_referencer::on_staffline (staff, pos))
+      Real offs = (Staff_symbol_referencer::on_line (staff, pos))
        ? 0.0
        : -dir * halfspace;
 
@@ -118,9 +118,10 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
   // find size of note heads.
   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
   if (!staff)
-    return SCM_EOL;
-
-  SCM heads = me->get_property ("note-heads");
+    {
+      me->suicide ();
+      return SCM_EOL;
+    }
 
   Real min_length_fraction
     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
@@ -130,17 +131,22 @@ Ledger_line_spanner::set_spacing_rods (SCM smob)
   Item *previous_column = 0;
   Item *current_column = 0;
 
+  Real halfspace = Staff_symbol::staff_space (staff) / 2;
+
+  Interval staff_extent = staff->extent (staff, Y_AXIS);
+  staff_extent *= 1 / halfspace;
+    
   /*
-    Run through heads using a loop. Since Legder_line_spanner can
+    Run through heads using a loop. Since Ledger_line_spanner can
     contain a lot of noteheads, superlinear performance is too slow.
   */
-  int interspaces = Staff_symbol::line_count (staff) - 1;
-  for (SCM hp = heads; scm_is_pair (hp); hp = scm_cdr (hp))
+  extract_item_set (me, "note-heads", heads);
+  for (vsize i = heads.size (); i--;)
     {
-      Item *h = dynamic_cast<Item *> (unsmob_grob (scm_car (hp)));
+      Item *h = heads[i];
 
       int pos = Staff_symbol_referencer::get_rounded_position (h);
-      if (abs (pos) <= interspaces)
+      if (staff_extent.contains (pos))
        continue;
 
       Item *column = h->get_column ();
@@ -188,7 +194,7 @@ struct Ledger_request
   }
 };
 
-typedef std::map < int, Drul_array<Ledger_request> > Ledger_requests;
+typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
 
 /*
   TODO: ledger share a lot of info. Lots of room to optimize away
@@ -199,9 +205,10 @@ SCM
 Ledger_line_spanner::print (SCM smob)
 {
   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
-  Link_array<Grob> heads (extract_grob_array (me, ly_symbol2scm ("note-heads")));
 
-  if (heads.is_empty ())
+  extract_grob_set (me, "note-heads", heads);
+
+  if (heads.empty ())
     return SCM_EOL;
 
   // find size of note heads.
@@ -209,6 +216,11 @@ Ledger_line_spanner::print (SCM smob)
   if (!staff)
     return SCM_EOL;
 
+  Real halfspace = Staff_symbol::staff_space (staff) / 2;
+
+  Interval staff_extent = staff->extent (staff, Y_AXIS);
+  staff_extent *= 1 / halfspace;
+  
   Real length_fraction
     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
 
@@ -221,32 +233,30 @@ Ledger_line_spanner::print (SCM smob)
     {
       Axis a = Axis (i);
       common[a] = common_refpoint_of_array (heads, me, a);
-      for (int i = heads.size (); i--;)
-       if (Grob *g = unsmob_grob (me->get_property ("accidental-grob")))
+      for (vsize i = heads.size (); i--;)
+       if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
          common[a] = common[a]->common_refpoint (g, a);
     }
 
-  int interspaces = Staff_symbol::line_count (staff) - 1;
   Ledger_requests reqs;
-  for (int i = heads.size (); i--;)
+  for (vsize i = heads.size (); i--;)
     {
       Item *h = dynamic_cast<Item *> (heads[i]);
 
       int pos = Staff_symbol_referencer::get_rounded_position (h);
-      if (pos
-         && abs (pos) > interspaces)
+      if (pos && !staff_extent.contains (pos))
        {
          Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
          Interval ledger_extent = head_extent;
          ledger_extent.widen (length_fraction * head_extent.length ());
 
          Direction vdir = Direction (sign (pos));
-         int rank = Paper_column::get_rank (h->get_column ());
+         int rank = h->get_column ()->get_rank ();
 
          reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
          reqs[rank][vdir].head_extent_.unite (head_extent);
          reqs[rank][vdir].position_
-           = vdir * ((vdir * reqs[rank][vdir].position_) >? (vdir * pos));
+           = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
        }
     }
 
@@ -257,15 +267,13 @@ Ledger_line_spanner::print (SCM smob)
        i != reqs.end (); last = i++)
     {
       if (last == reqs.end ())
-       {
-         continue;
-       }
+       continue;
 
       Direction d = DOWN;
       do
        {
-         if (abs (last->second[d].position_) > interspaces
-             && abs (i->second[d].position_) > interspaces)
+         if (!staff_extent.contains (last->second[d].position_)
+             && !staff_extent.contains (i->second[d].position_))
            {
              Real center
                = (last->second[d].head_extent_[RIGHT]
@@ -277,13 +285,15 @@ Ledger_line_spanner::print (SCM smob)
                  Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
 
                  // due tilt of quarter note-heads
+                 /* FIXME */
                  bool both
-                   = (abs (last->second[d].position_) > interspaces + 1
-                      && abs (i->second[d].position_) > interspaces + 1);
-
+                   = (!staff_extent.contains (last->second[d].position_
+                                              - sign (last->second[d].position_))
+                      && !staff_extent.contains (i->second[d].position_
+                                                 - sign (i->second[d].position_)));
                  Real limit = (center + (both ? which * gap / 2 : 0));
-                 lr.ledger_extent_.elem_ref (-which)
-                   = which * (which * lr.ledger_extent_[-which] >? which * limit);
+                 lr.ledger_extent_.at (-which)
+                   = which * max (which * lr.ledger_extent_[-which], which * limit);
                }
              while (flip (&which) != LEFT);
            }
@@ -291,26 +301,26 @@ Ledger_line_spanner::print (SCM smob)
       while (flip (&d) != DOWN);
     }
 
-  // create  ledgers for note heads
+  // create ledgers for note heads
   Real ledgerlinethickness
     = Staff_symbol::get_ledger_line_thickness (staff);
-  Real halfspace = Staff_symbol::staff_space (staff) / 2;
-  for (int i = heads.size (); i--;)
+  for (vsize i = heads.size (); i--;)
     {
       Item *h = dynamic_cast<Item *> (heads[i]);
 
       int pos = Staff_symbol_referencer::get_rounded_position (h);
-      if (abs (pos) > interspaces + 1)
+      if (!staff_extent.contains (pos - sign (pos)))
        {
          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[Paper_column::get_rank (h->get_column ())][Direction (sign (pos))].ledger_extent_;
+         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_property ("accidental-grob")))
+         if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
            {
              Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
              Real d
@@ -318,7 +328,7 @@ Ledger_line_spanner::print (SCM smob)
                                                        head_size[LEFT]),
                                      0.0);
 
-             left_shorten = (-ledger_size[LEFT] + d) >? 0;
+             left_shorten = max (-ledger_size[LEFT] + d, 0.0);
 
              /*
                TODO: shorten 2 ledger lines for the case natural +
@@ -326,7 +336,7 @@ Ledger_line_spanner::print (SCM smob)
              */
            }
 
-         ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces,
+         ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
                                                  halfspace,
                                                  ledgerlinethickness,
                                                  ledger_size,
@@ -341,23 +351,27 @@ Ledger_line_spanner::print (SCM smob)
 }
 
 ADD_INTERFACE (Ledger_line_spanner,
-              "ledger-line-interface",
-              
-              "This spanner draws the ledger lines of a staff.\n"
-              "This is a separate grob to because it has to process\n"
-              "all potential collisions between all note heads.",
-              
-              "note-heads thickness minimum-length-fraction length-fraction gap");
+              "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.",
+
+              /* properties */
+              "gap "   
+              "length-fraction "       
+              "minimum-length-fraction "
+              "note-heads "
+              "thickness "
+              );
 
 struct Ledgered_interface
 {
-  static bool has_interface (Grob *);
+  DECLARE_GROB_INTERFACE ();
 };
 
 ADD_INTERFACE (Ledgered_interface,
-              "ledgered-interface",
-              
-              "Objects that need ledger lines, typically "
-              "note heads. See also @ref{ledger-line-interface}.",
-              
-              "no-ledgers");
+              "Objects that need ledger lines, typically note heads.  See"
+              " also @ref{ledger-line-spanner-interface}.",
+
+              /* properties */
+              "no-ledgers "
+              );