]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spacing-determine-loose-columns.cc
Imported Upstream version 2.14.2
[lilypond.git] / lily / spacing-determine-loose-columns.cc
index 610b96b32081074d8c78ef1db6afd682b42dc2e8..5a2411b3226a04bdb8450eaee9d17c8e7b4acf00 100644 (file)
@@ -1,24 +1,36 @@
 /*
-  spacing-determine-loose-columns.cc -- implement Spacing_spanner
-  methods that decide which columns to turn loose.
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-  (c) 2005 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
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
 
-*/
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
+#include "staff-spacing.hh"
 
+#include "spacing-options.hh"
 #include "system.hh"
 #include "paper-column.hh"
 #include "column-x-positions.hh"
-#include "staff-spacing.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"
 
 /*
   Return whether COL is fixed to its neighbors by some kind of spacing
   (Otherwise, we might risk core dumps, and other weird stuff.)
 */
 static bool
-is_loose_column (Grob *l, Grob *c, Grob *r)
+is_loose_column (Grob *l, Grob *col, Grob *r, Spacing_options const *options)
 {
-  extract_grob_set (c, "right-neighbors", rns);
-  extract_grob_set (c, "left-neighbors", lns);
+  if (!to_boolean (col->get_property ("allow-loose-spacing")))
+    return false;
   
+
+  if ((options->float_nonmusical_columns_
+       || options->float_grace_columns_)
+      && Paper_column::when_mom (col).grace_part_)
+    {
+      return true;
+    }
+
+  
+  if (Paper_column::is_musical (col))
+    return false;
+
   /*
     If this column doesn't have a proper neighbor, we should really
     make it loose, but spacing it correctly is more than we can
@@ -57,57 +81,49 @@ is_loose_column (Grob *l, Grob *c, Grob *r)
     such a borderline case.)
 
   */
-  if (lns.is_empty () || rns.is_empty ())
-    return false;
 
-  Item *l_neighbor = dynamic_cast<Item *> (lns[0]);
-  Item *r_neighbor = dynamic_cast<Item *> (rns[0]);
+  Item *r_neighbor = unsmob_item (col->get_object ("right-neighbor"));
+  Item *l_neighbor = unsmob_item (col->get_object ("left-neighbor"));
 
   if (!l_neighbor || !r_neighbor)
     return false;
 
-  l_neighbor = l_neighbor->get_column ();
-  r_neighbor = dynamic_cast<Item *> (Note_spacing::right_column (r_neighbor));
-
-  if (l == l_neighbor && r == r_neighbor)
-    return false;
-
-  if (!l_neighbor || !r_neighbor)
-    return false;
+  /* If a non-empty column (ie. not \bar "") is placed nicely in series with
+     its neighbor (ie. no funny polyphonic stuff), don't make it loose.
+  */
+  if (l == l_neighbor && r == r_neighbor && col->extent (col, X_AXIS).length () > 0)
+     return false;
 
   /*
     Only declare loose if the bounds make a little sense.  This means
     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))))
-    {
-      return false;
-    }
+  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);
-  for (int i = elts.size (); i--; )
+  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 (int j = gelts.size (); j--; )
+         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;
+               {
+                 extract_grob_set (h, "elements", helts);
+                 for (vsize k = helts.size (); k--;)
+                   if ("" != robust_scm2string (helts[k]->get_property ("glyph-name"), ""))
+                     return false;
+               }
            }
        }
     }
@@ -115,109 +131,131 @@ is_loose_column (Grob *l, Grob *c, Grob *r)
   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, Link_array<Grob> *cols,
-                                     Spacing_options const *options)
+Spacing_spanner::prune_loose_columns (Grob *me,
+                                     vector<Grob*> *cols,
+                                     Spacing_options *options)
 {
-  Link_array<Grob> newcols;
-  Real increment = robust_scm2double (me->get_property ("spacing-increment"), 1.2);
-  for (int i = 0; i < cols->size (); i++)
+  vector<Grob*> newcols;
+
+  for (vsize i = 0; i < cols->size (); i++)
     {
-      if (Item::is_breakable (cols->elem (i))
-         || Paper_column::is_musical (cols->elem (i)))
+      Grob *c = cols->at (i);
+
+      bool loose = (i > 0 && i + 1 < cols->size ())
+       && is_loose_column (cols->at (i - 1), c, cols->at (i + 1), options);
+
+      /* Breakable columns never get pruned; even if they are loose,
+        their broken pieces are not.  However, we mark them so that
+        the spacing can take their mid-line looseness into account. */
+      if (loose && Paper_column::is_breakable (c))
        {
-         newcols.push (cols->elem (i));
-         continue;
+         loose = false;
+         c->set_property ("maybe-loose", SCM_BOOL_T);
+       }
+      /*
+       Unbreakable columns which only contain page-labels also
+       never get pruned, otherwise the labels are lost before they can
+       be collected by the System: so we mark these columns too.
+      */
+      if (!loose && !Paper_column::is_breakable (c)
+         && scm_is_pair (c->get_property ("labels")))
+       {
+         extract_grob_set (c, "elements", elts);
+         if (elts.empty ())
+           c->set_property ("maybe-loose", SCM_BOOL_T);
        }
 
-      Grob *c = cols->elem (i);
-      if (is_loose_column (cols->elem (i - 1), c, cols->elem (i + 1)))
+      if (loose)
        {
-         extract_grob_set (c, "right-neighbors", rns_arr);
-         extract_grob_set (c, "left-neighbors", lns_arr);
-         
-         SCM lns = lns_arr.size () ? lns_arr.top()->self_scm () : SCM_BOOL_F;
-         SCM rns = rns_arr.size () ? rns_arr.top()->self_scm () : SCM_BOOL_F;
-         
+         Grob *right_neighbor = unsmob_grob (c->get_object ("right-neighbor"));
+         Grob *left_neighbor = unsmob_grob (c->get_object ("left-neighbor"));
+
          /*
            Either object can be non existent, if the score ends
            prematurely.
          */
-
-         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->elem (i - 1);
-         next_door[RIGHT] = cols->elem (i + 1);
-         Direction d = LEFT;
-         Drul_array<Real> dists (0, 0);
-
-         do
+         if (!right_neighbor || !left_neighbor)
            {
-             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 (int k = wishes.size(); k--;)
-               {
-                 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 (d == LEFT)
-                   {
-                     /*
-                       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, increment, &space, &fixed);
-
-                     space -= increment;
-
-                     dists[d] = max (dists[d], space);
-                   }
-                 else
-                   {
-                     Real space, fixed_space;
-                     Staff_spacing::get_spacing_params (sp,
-                                                        &space, &fixed_space);
-
-                     dists[d] = max (dists[d], fixed_space);
-                   }
-               }
+             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 ()));
            }
-         while (flip (&d) != LEFT);
+         else
+           {
+             c->set_object ("between-cols", scm_cons (left_neighbor->self_scm (),
+                                                      right_neighbor->self_scm ()));
 
-         Rod r;
-         r.distance_ = dists[LEFT] + dists[RIGHT];
-         r.item_drul_[LEFT] = dynamic_cast<Item *> (cols->elem (i - 1));
-         r.item_drul_[RIGHT] = dynamic_cast<Item *> (cols->elem (i + 1));
 
-         r.add_to_cols ();
-       }
-      else
-       {
-         newcols.push (c);
+             /*
+               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)));
+
+             set_distances_for_loose_col (me, c, next_door, options);
+           }
        }
+
+      if (!loose)
+       newcols.push_back (c);
     }
 
   *cols = newcols;
@@ -227,108 +265,55 @@ Spacing_spanner::prune_loose_columns (Grob *me, Link_array<Grob> *cols,
   Set neighboring columns determined by the spacing-wishes grob property.
 */
 void
-Spacing_spanner::set_explicit_neighbor_columns (Link_array<Grob> const &cols)
+Spacing_spanner::set_explicit_neighbor_columns (vector<Grob*> const &cols)
 {
-  for (int i = 0; i < cols.size (); i++)
+  for (vsize i = 0; i < cols.size (); i++)
     {
-      SCM right_neighbors = Grob_array::make_array ();
-      Grob_array *rn_arr = unsmob_grob_array (right_neighbors);
-      int min_rank = 100000;   // inf.
-
       extract_grob_set (cols[i], "spacing-wishes", wishes);
-      for (int k = wishes.size(); k--;)
+      for (vsize j = wishes.size (); j--;)
        {
-         Item *wish = dynamic_cast<Item *> ( wishes[k]);
-
-         Item *lc = wish->get_column ();
-         Grob *right = Note_spacing::right_column (wish);
+         Item *wish = dynamic_cast<Item*> (wishes[j]);
+         Item *left_col = wish->get_column ();
+         int left_rank = Paper_column::get_rank (left_col);
+         int min_right_rank = INT_MAX;
 
-         if (!right)
-           continue;
-
-         Item *rc = dynamic_cast<Item *> (right);
-
-         int right_rank = Paper_column::get_rank (rc);
-         int left_rank = Paper_column::get_rank (lc);
-
-         /*
-           update the left column.
-         */
-         if (right_rank <= min_rank)
+         extract_grob_set (wish, "right-items", right_items);
+         for (vsize k = right_items.size (); k--;)
            {
-             if (right_rank < min_rank)
-               rn_arr->clear ();
+             Item *right_col = dynamic_cast<Item*> (right_items[k])->get_column ();
+             int right_rank = Paper_column::get_rank (right_col);
 
-             min_rank = right_rank;
-             rn_arr->add (wish);
-           }
-
-         /*
-           update the right column of the wish.
-         */
-         int maxrank = 0;
-
-         extract_grob_set (rc, "left-neighbors", lns_arr);
-         if (lns_arr.size ())
-           {
-             Item *it = dynamic_cast<Item *> (lns_arr.top());
-             maxrank = Paper_column::get_rank (it->get_column ());
-           }
-
-         if (left_rank >= maxrank)
-           {
-             
-             if (left_rank > maxrank)
+             if (right_rank < min_right_rank)
                {
-                 Grob_array *ga = unsmob_grob_array (rc->get_object ("left-neighbors"));
-                 if (ga)
-                   ga->clear ();
+                 left_col->set_object ("right-neighbor", right_col->self_scm ());
+                 min_right_rank = right_rank;
                }
 
-             Pointer_group_interface::add_grob (rc, ly_symbol2scm ("left-neighbors"), wish);
+             Grob *old_left_neighbor = unsmob_grob (right_col->get_object ("left-neighbor"));
+             if (!old_left_neighbor || left_rank > Paper_column::get_rank (old_left_neighbor))
+               right_col->set_object ("left-neighbor", left_col->self_scm ());
            }
        }
-
-      if (rn_arr->size ())
-       {
-         cols[i]->set_object ("right-neighbors", right_neighbors);
-       }
     }
 }
 
 /*
   Set neighboring columns that have no left/right-neighbor set
   yet. Only do breakable non-musical columns, and musical columns.
+  Why only these? --jneem
 */
 void
-Spacing_spanner::set_implicit_neighbor_columns (Link_array<Grob> const &cols)
+Spacing_spanner::set_implicit_neighbor_columns (vector<Grob*> const &cols)
 {
-  for (int i = 0; i < cols.size (); i++)
+  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.
-      */
-      extract_grob_set (cols[i], "left-neighbors", lns);
-      if (lns.is_empty () && i )
-       {
-         SCM ga_scm = Grob_array::make_array();
-         Grob_array *ga = unsmob_grob_array (ga_scm);
-         ga->add (cols[i-1]);
-         cols[i]->set_object ("left-neighbors", ga_scm);
-       }
-      extract_grob_set (cols[i], "right-neighbors", rns);
-      if (rns.is_empty () && i < cols.size () - 1)
-       {
-         SCM ga_scm = Grob_array::make_array();
-         Grob_array *ga = unsmob_grob_array (ga_scm);
-         ga->add (cols[i+1]);
-         cols[i]->set_object ("right-neighbors", ga_scm);
-       }
+      if (i && !unsmob_grob (cols[i]->get_object ("left-neighbor")))
+       cols[i]->set_object ("left-neighbor", cols[i-1]->self_scm ());
+      if (i + 1 < cols.size () && !unsmob_grob (cols[i]->get_object ("right-neighbor")))
+       cols[i]->set_object ("right-neighbor", cols[i+1]->self_scm ());
     }
 }