From 6faf6cf597ce120ebcc180965c509798a7e20a8a Mon Sep 17 00:00:00 2001
From: Joe Neeman <joeneeman@gmail.com>
Date: Sun, 17 Jun 2007 23:42:07 +1000
Subject: [PATCH] cleanups in note-spacing

---
 lily/note-spacing.cc      | 125 ++++++++------------------------------
 lily/spacing-interface.cc |   5 +-
 lily/spacing-spanner.cc   |   2 +-
 scm/define-grobs.scm      |   3 +-
 4 files changed, 32 insertions(+), 103 deletions(-)

diff --git a/lily/note-spacing.cc b/lily/note-spacing.cc
index 345b9209e7..a952455f7e 100644
--- a/lily/note-spacing.cc
+++ b/lily/note-spacing.cc
@@ -16,6 +16,7 @@
 #include "warn.hh"
 #include "stem.hh"
 #include "separation-item.hh"
+#include "spacing-interface.hh"
 #include "staff-spacing.hh"
 #include "accidental-placement.hh"
 #include "output-def.hh"
@@ -30,79 +31,30 @@ void
 Note_spacing::get_spacing (Grob *me, Item *right_col,
 			   Real base_space, Real increment, Real *space, Real *fixed)
 {
-  Drul_array<SCM> props (me->get_object ("left-items"),
-			 me->get_object ("right-items"));
-  Direction d = LEFT;
-  Direction col_dir = right_col->break_status_dir ();
-  Drul_array<Interval> extents;
+  vector<Item*> note_columns = Spacing_interface::left_note_columns (me);
+  Real left_head_end = 0;
 
-  Interval left_head_wid;
-  do
+  for (vsize i = 0; i < note_columns.size (); i++)
     {
-      vector<Grob*> const &items (ly_scm2link_array (props [d]));
-      for (vsize i = items.size (); i--;)
-	{
-	  Item *it = dynamic_cast<Item *> (items[i]);
-
-	  if (d == RIGHT && it->break_status_dir () != col_dir)
-	    it = it->find_prebroken_piece (col_dir);
-
-	  /*
-	    some kind of mismatch, eg. a note column, that is behind a
-	    linebreak.
-	  */
-	  if (!it)
-	    continue;
-
-	  Item *it_col = it->get_column ();
-	  if (d == RIGHT && right_col != it_col)
-	    continue;
-
-	  if (d == LEFT
-	      && Note_column::has_interface (it))
-	    {
-	      SCM r = it->get_object ("rest");
-	      Grob *g = unsmob_grob (r);
-	      if (!g)
-		g = Note_column::first_head (it);
-
-	      /*
-		Ugh. If Stem is switched off, we don't know what the
-		first note head will be.
-	      */
-	      if (g)
-		{
-		  if (g->common_refpoint (it_col, X_AXIS) != it_col)
-		    programming_error ("Note_spacing::get_spacing (): Common refpoint incorrect");
-		  else
-		    left_head_wid = g->extent (it_col, X_AXIS);
-		}
-	    }
-
-	  extents[d].unite (it->extent (it_col, X_AXIS));
-	  if (d == RIGHT)
-	    {
-	      Grob *accs = Note_column::accidentals (it);
-	      if (!accs)
-		accs = Note_column::accidentals (it->get_parent (X_AXIS));
-
-	      if (accs)
-		{
-		  Interval v
-		    = Accidental_placement::get_relevant_accidental_extent (accs, it_col, me);
-
-		  extents[d].unite (v);
-		}
-
-	      if (Grob *arpeggio = Note_column::arpeggio (it))
-		extents[d].unite (arpeggio->extent (it_col, X_AXIS));
-	    }
-	}
-
-      if (extents[d].is_empty ())
-	extents[d] = Interval (0, 0);
+       SCM r = note_columns[i]->get_object ("rest");
+       Grob *g = unsmob_grob (r);
+       Grob *col = note_columns[i]->get_column ();
+
+       if (!g)
+	 g = Note_column::first_head (note_columns[i]);
+
+       /*
+	 Ugh. If Stem is switched off, we don't know what the
+	 first note head will be.
+       */
+       if (g)
+	 {
+	   if (g->common_refpoint (col, X_AXIS) != col)
+	     programming_error ("Note_spacing::get_spacing (): Common refpoint incorrect");
+	   else
+	     left_head_end = g->extent (col, X_AXIS)[RIGHT];
+	 }
     }
-  while (flip (&d) != LEFT);
 
   /*
     We look at the width of the note head, since smaller heads get less space
@@ -111,32 +63,10 @@ Note_spacing::get_spacing (Grob *me, Item *right_col,
 
     What is sticking out of the note head (eg. a flag), doesn't get
     the full amount of space.
-
-    FIXED also includes the left part of the right object.
   */
-  *fixed
-    = (left_head_wid.is_empty () ? increment
-       :        /*
-		  Size of the head:
-		*/
-       (left_head_wid[RIGHT]+
-
-	/*
-	  What's sticking out of the head, eg. a flag:
-	*/
-	(extents[LEFT][RIGHT] - left_head_wid[RIGHT]) / 2))
-
-    /*
-      What is sticking out on the left side of the right note:
-    */
-    + (extents[RIGHT].is_empty ()
-       ? 0.0
-       : ((- extents[RIGHT][LEFT] / 2)
+  Real min_dist = Spacing_interface::minimum_distance (me);
 
-	  /*
-	    Add that which sticks out a lot.
-	  */
-	  + max (0.0, -extents[RIGHT][LEFT] - (base_space - 0.5 * increment))));
+  *fixed = max (min_dist, left_head_end + min_dist/2);
 
   /*
     We don't do complicated stuff: (base_space - increment) is the
@@ -291,12 +221,7 @@ Note_spacing::stem_dir_correction (Grob *me, Item *rcolumn,
 	      if (!head_extent.is_empty ())
 		note_head_width = head_extent[RIGHT];
 
-	      if (st)
-		{
-		  Real thick = Stem::thickness (st);
-
-		  note_head_width -= thick;
-		}
+	      note_head_width -= Stem::thickness (st);
 	    }
 
 	  correction = note_head_width * stem_dirs[LEFT];
diff --git a/lily/spacing-interface.cc b/lily/spacing-interface.cc
index b25fd65ad2..856eee1e73 100644
--- a/lily/spacing-interface.cc
+++ b/lily/spacing-interface.cc
@@ -35,7 +35,10 @@ Spacing_interface::minimum_distance (Grob *me)
 	  {
 	    SCM sky_scm = items[d][i]->get_property ("horizontal-skylines");
 	    Skyline_pair *sky = Skyline_pair::unsmob (sky_scm);
-	    skylines[d].merge ((*sky)[-d]);
+	    if (sky)
+	      skylines[d].merge ((*sky)[-d]);
+	    else
+	      programming_error ("separation item has no skyline");
 	    
 	    if (d == RIGHT && items[LEFT].size ())
 	      skylines[d].merge (Separation_item::conditional_skyline (items[d][i], items[LEFT][0]));
diff --git a/lily/spacing-spanner.cc b/lily/spacing-spanner.cc
index 721fcd30a4..69b0de980b 100644
--- a/lily/spacing-spanner.cc
+++ b/lily/spacing-spanner.cc
@@ -519,7 +519,7 @@ Spacing_spanner::breakable_column_spacing (Grob *me, Item *l, Item *r,
 
   Real inverse_strength = (compound_space - compound_fixed);
   Real distance = compound_space;
-  message (_f ("adding spring of length %f", distance));
+
   Spaceable_grob::add_spring (l, r, distance, inverse_strength);
 }
 
diff --git a/scm/define-grobs.scm b/scm/define-grobs.scm
index 9c8e83d311..7f9a068c2b 100644
--- a/scm/define-grobs.scm
+++ b/scm/define-grobs.scm
@@ -1143,9 +1143,10 @@
 	(axes . (0 1))
 	(X-extent . ,ly:axis-group-interface::width)
 	(Y-extent . ,ly:axis-group-interface::height)
+	(horizontal-skylines . ,ly:separation-item::calc-skylines)
 	(meta . ((class . Item)
 		 (interfaces . (axis-group-interface
-				separation-item-iterface
+				separation-item-interface
 				note-column-interface))))))
 
     (NoteHead
-- 
2.39.5