]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spacing-determine-loose-columns.cc
Run `make grand-replace'.
[lilypond.git] / lily / spacing-determine-loose-columns.cc
index 951cc8b12ef23ae72d3fe6750ad6ea3ca0afbc22..ed706a6575460447c16d6eb2d2866d28dda3d3c2 100644 (file)
@@ -4,18 +4,21 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 2005--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include "staff-spacing.hh"
 
+#include "spacing-options.hh"
 #include "system.hh"
 #include "paper-column.hh"
 #include "column-x-positions.hh"
 #include "pointer-group-interface.hh"
+#include "spacing-interface.hh"
 #include "spacing-spanner.hh"
 #include "note-spacing.hh"
 #include "moment.hh"
+#include "grob-array.hh"
 #include "break-align-interface.hh"
 #include "warn.hh"
 
   (Otherwise, we might risk core dumps, and other weird stuff.)
 */
 static bool
-is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
+is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
 {
-  if (options->float_nonmusical_columns_
-      && Paper_column::when_mom (c).grace_part_)
-    return true;
-
-  if (Paper_column::is_musical (c)
-      || Item::is_breakable (c))
+  if (!to_boolean (col->get_property ("allow-loose-spacing")))
     return false;
+  
 
-  extract_grob_set (c, "right-neighbors", rns);
-  extract_grob_set (c, "left-neighbors", lns);
+  if ((options->float_nonmusical_columns_
+       || options->float_grace_columns_)
+      && Paper_column::when_mom (col).grace_part_)
+    {
+      return true;
+    }
+
+  
+  if (Paper_column::is_musical (col)
+      || Paper_column::is_breakable (col))
+    return false;
 
   /*
     If this column doesn't have a proper neighbor, we should really
@@ -64,9 +72,14 @@ is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
     such a borderline case.)
 
   */
+
+  extract_grob_set (col, "right-neighbors", rns);
+  extract_grob_set (col, "left-neighbors", lns);
+
   if (lns.empty () || rns.empty ())
     return false;
 
+  
   Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
   Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
 
@@ -74,7 +87,7 @@ is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
     return false;
 
   l_neighbor = l_neighbor->get_column ();
-  r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
+  r_neighbor = dynamic_cast<Item *> (Spacing_interface::right_column (r_neighbor));
 
   if (l == l_neighbor && r == r_neighbor)
     return false;
@@ -87,30 +100,24 @@ is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
     some cases (two isolated, consecutive clef changes) won't be
     nicely folded, but hey, then don't do that.
   */
-  if (! ((Paper_column::is_musical (l_neighbor) || Item::is_breakable (l_neighbor))
-        && (Paper_column::is_musical (r_neighbor) || Item::is_breakable (r_neighbor))))
+  if (! ((Paper_column::is_musical (l_neighbor) || Paper_column::is_breakable (l_neighbor))
+        && (Paper_column::is_musical (r_neighbor) || Paper_column::is_breakable (r_neighbor))))
     return false;
 
   /*
-    A rather hairy check, but we really only want to move around
-    clefs. (anything else?)
-
     in any case, we don't want to move bar lines.
   */
-  extract_grob_set (c, "elements", elts);
+  extract_grob_set (col, "elements", elts);
   for (vsize i = elts.size (); i--;)
     {
       Grob *g = elts[i];
-      if (g && Break_align_interface::has_interface (g))
+      if (g && Break_alignment_interface::has_interface (g))
        {
          extract_grob_set (g, "elements", gelts);
          for (vsize j = gelts.size (); j--;)
            {
              Grob *h = gelts[j];
 
-             /*
-               ugh. -- fix staff-bar name?
-             */
              if (h && h->get_property ("break-align-symbol") == ly_symbol2scm ("staff-bar"))
                return false;
            }
@@ -120,14 +127,67 @@ is_loose_column (Grob *l, Grob *c, Grob *r, Spacing_options const *options)
   return true;
 }
 
+void
+Spacing_spanner::set_distances_for_loose_col (Grob *me, Grob *c,
+                                             Drul_array<Item *> next_door,
+                                             Spacing_options const *options)
+{
+  Direction d = LEFT;
+  Drul_array<Real> dists (0, 0);
+
+  do
+    {
+      Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
+      Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
+
+      extract_grob_set (lc, "spacing-wishes", wishes);
+      for (vsize k = wishes.size (); k--;)
+       {
+         Grob *sp = wishes[k];
+         if (Spacing_interface::left_column (sp) != lc
+             || Spacing_interface::right_column (sp) != rc)
+           continue;
+
+         if (Note_spacing::has_interface (sp))
+           {
+             /*
+               The note spacing should be taken from the musical
+               columns.
+             */
+             Real base = note_spacing (me, lc, rc, options);
+             Spring spring = Note_spacing::get_spacing (sp, rc, base, options->increment_);
+
+             dists[d] = max (dists[d], spring.min_distance ());
+           }
+         else if (Staff_spacing::has_interface (sp))
+           {
+             Spring spring = Staff_spacing::get_spacing (sp, rc);
+
+             dists[d] = max (dists[d], spring.min_distance ());
+           }
+         else
+           programming_error ("Subversive spacing wish");
+       }
+    }
+  while (flip (&d) != LEFT);
+
+  Rod r;
+  r.distance_ = dists[LEFT] + dists[RIGHT];
+  r.item_drul_ = next_door;
+  r.add_to_cols (); 
+}
+
+
 /*
   Remove columns that are not tightly fitting from COLS. In the
   removed columns, set 'between-cols to the columns where it is in
   between.
 */
 void
-Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
-                                     Spacing_options const *options)
+Spacing_spanner::prune_loose_columns (Grob *me,
+                                     vector<Grob*> *cols,
+                                     Spacing_options *options)
 {
   vector<Grob*> newcols;
 
@@ -135,7 +195,7 @@ Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
     {
       Grob *c = cols->at (i);
 
-      bool loose = (i > 0 && i < cols->size () - 1)
+      bool loose = (i > 0 && i + 1 < cols->size ())
        && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
 
       if (loose)
@@ -152,72 +212,40 @@ Spacing_spanner::prune_loose_columns (Grob *me, vector<Grob*> *cols,
          */
 
          extract_grob_set (unsmob_grob (rns), "right-items", right_items);
-         c->set_object ("between-cols", scm_cons (lns,
-                                                  right_items[0]->self_scm ()));
-
-         /*
-           Set distance constraints for loose columns
-         */
-         Drul_array<Grob *> next_door;
-         next_door[LEFT] = cols->at (i - 1);
-         next_door[RIGHT] = cols->at (i + 1);
-         Direction d = LEFT;
-         Drul_array<Real> dists (0, 0);
-
-         do
+         if (right_items.size () == 0 || !unsmob_grob (lns))
            {
-             dists[d] = 0.0;
-             Item *lc = dynamic_cast<Item *> ((d == LEFT) ? next_door[LEFT] : c);
-             Item *rc = dynamic_cast<Item *> (d == LEFT ? c : next_door[RIGHT]);
-
-             extract_grob_set (lc, "spacing-wishes", wishes);
-             for (vsize k = wishes.size (); k--;)
+             c->programming_error ("Cannot determine neighbors for floating column. ");
+             c->set_object ("between-cols", scm_cons (cols->at (i-1)->self_scm (),
+                                                      cols->at (i+1)->self_scm ()));
+           }
+         else
+           {
+             Grob *min_item = 0;
+             int min_rank = INT_MAX;
+             for (vsize j = 0; j < right_items.size (); j ++)
                {
-                 Grob *sp = wishes[k];
-                 if (Note_spacing::left_column (sp) != lc
-                     || Note_spacing::right_column (sp) != rc)
-                   continue;
-
-                 Real space, fixed;
-                 fixed = 0.0;
-                 bool dummy;
-
-                 if (Note_spacing::has_interface (sp))
+                 int rank = dynamic_cast<Item*> (right_items[j])->get_column ()->get_rank ();
+                 if (rank < min_rank)
                    {
-                     /*
-                       The note spacing should be taken from the musical
-                       columns.
-
-                     */
-                     Real base = note_spacing (me, lc, rc, options, &dummy);
-                     Note_spacing::get_spacing (sp, rc, base, options->increment_, &space, &fixed);
-
-                     space -= options->increment_;
-
-                     dists[d] = max (dists[d], space);
-                   }
-                 else if (Staff_spacing::has_interface (sp))
-                   {
-                     Real space, fixed_space;
-                     Staff_spacing::get_spacing_params (sp,
-                                                        &space, &fixed_space);
-
-                     dists[d] = max (dists[d], fixed_space);
+                     min_item = right_items[j];
+                     min_rank = rank;
                    }
-                 else
-                   programming_error ("Subversive spacing wish");
                }
-           }
-         while (flip (&d) != LEFT);
+             
+             c->set_object ("between-cols", scm_cons (lns,
+                                                      min_item->self_scm ()));
 
-         Rod r;
-         r.distance_ = dists[LEFT] + dists[RIGHT];
-         r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->at (i - 1));
-         r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->at (i + 1));
+             /*
+               Set distance constraints for loose columns
+             */
+             Drul_array<Item *> next_door (dynamic_cast<Item*> (cols->at (i - 1)),
+                                           dynamic_cast<Item*> (cols->at (i + 1)));
 
-         r.add_to_cols ();
+             set_distances_for_loose_col (me, c, next_door, options);
+           }
        }
-      else
+
+      if (!loose)
        newcols.push_back (c);
     }
 
@@ -234,7 +262,7 @@ Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
     {
       SCM right_neighbors = Grob_array::make_array ();
       Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
-      int min_rank = 100000;   // inf.
+      int min_rank = INT_MAX;
 
       extract_grob_set (cols[i], "spacing-wishes", wishes);
       for (vsize k = wishes.size (); k--;)
@@ -242,7 +270,7 @@ Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
          Item *wish = dynamic_cast<Item *> (wishes[k]);
 
          Item *lc = wish->get_column ();
-         Grob *right = Note_spacing::right_column (wish);
+         Grob *right = Spacing_interface::right_column (wish);
 
          if (!right)
            continue;
@@ -305,11 +333,9 @@ Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
   for (vsize i = 0; i < cols.size (); i++)
     {
       Item *it = dynamic_cast<Item *> (cols[i]);
-      if (!Item::is_breakable (it) && !Paper_column::is_musical (it))
+      if (!Paper_column::is_breakable (it) && !Paper_column::is_musical (it))
        continue;
 
-      // it->breakable || it->musical
-
       /*
        sloppy with typing left/right-neighbors should take list, but paper-column found instead.
       */
@@ -322,7 +348,7 @@ Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
          cols[i]->set_object ("left-neighbors", ga_scm);
        }
       extract_grob_set (cols[i], "right-neighbors", rns);
-      if (rns.empty () && i < cols.size () - 1)
+      if (rns.empty () && i + 1 < cols.size ())
        {
          SCM ga_scm = Grob_array::make_array ();
          Grob_array *ga = unsmob_grob_array (ga_scm);