]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spacing-spanner.cc
release: 1.3.65
[lilypond.git] / lily / spacing-spanner.cc
index e54f3f971b6e92dc228141b22794490d71209626..827213d9fd089b1046a4a5d2f614c6576a89fa6a 100644 (file)
@@ -3,7 +3,7 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   
  */
 
 #include "warn.hh"
 #include "paper-score.hh"
 #include "line-of-score.hh"
+#include "misc.hh"
 
-Spacing_spanner::Spacing_spanner ()
+Spacing_spanner::Spacing_spanner (SCM s)
+  : Spanner (s)
 {
-  set_empty (X_AXIS);
-  set_empty (Y_AXIS);  
-  set_elt_property ("transparent", SCM_BOOL_T);
+  set_extent_callback (0, X_AXIS);
+  set_extent_callback (0, Y_AXIS);  
 }
 
 /*
-  cut 'n paste from spring-spacer.cc
-
-  generate springs between columns.
-
 
   The algorithm is partly taken from :
 
@@ -35,22 +32,33 @@ Spacing_spanner::Spacing_spanner ()
   Science, The Ohio State University, 1987.
 
   TOO HAIRY.
+
+  TODO: write comments 
   
  */
 Array<Spring>
 Spacing_spanner::do_measure (Link_array<Paper_column> cols) const
 {
   Moment shortest;
+  Moment mean_shortest;
   shortest.set_infinite (1);
+
+  int n = 0;
   for (int i =0 ; i < cols.size (); i++)  
     {
       if (cols[i]->musical_b ())
        {
-         SCM  st = cols[i]->get_elt_property ("shortest-starter");
-         
-         shortest = shortest <? (*SMOB_TO_TYPE(Moment, st));
+         SCM  st = cols[i]->get_elt_property ("shortest-starter-duration");
+         Moment this_shortest = (*SMOB_TO_TYPE(Moment, st));
+         shortest = shortest <? this_shortest;
+         if (!mean_shortest.infty_b ())
+           {
+             n++;
+             mean_shortest += this_shortest;
+           }
        }
     }
+  mean_shortest /= n;
 
   Array<Spring> meas_springs;
 
@@ -59,8 +67,8 @@ Spacing_spanner::do_measure (Link_array<Paper_column> cols) const
     {
       Item * l = cols[i];
       Item * r = cols[i+1];
-      Item * lb = l->find_broken_piece (RIGHT);
-      Item * rb = r->find_broken_piece (LEFT);      
+      Item * lb = l->find_prebroken_piece (RIGHT);
+      Item * rb = r->find_prebroken_piece (LEFT);      
 
       Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
 
@@ -126,15 +134,14 @@ Spacing_spanner::do_measure (Link_array<Paper_column> cols) const
          */
          if (lc->musical_b () && rc->musical_b ())
            {
-             if (rc->get_elt_property ("contains-grace") == SCM_UNDEFINED)
+             if (!to_boolean (rc->get_elt_property ("contains-grace")))
                right_dist *= paper_l ()->get_var ("musical_to_musical_left_spacing_factor");
            }
 
-         if (rc->musical_b () && rc->get_elt_property ("contains-grace") != SCM_UNDEFINED)
+         if (rc->musical_b () && to_boolean (rc->get_elt_property ("contains-grace")))
            right_dist *= paper_l ()->get_var ("before_grace_spacing_factor");
  
-         s.distance_f_ = left_distance + right_dist;
+         s.distance_f_ = left_distance + right_dist;
            
          Real stretch_dist = 0.;
          if (gh_number_p (stretch_hint))
@@ -149,8 +156,10 @@ Spacing_spanner::do_measure (Link_array<Paper_column> cols) const
            stretch_dist += right_dist;
 
          if (s.distance_f_ <0)
-           programming_error("negative dist");
-         
+           {
+             programming_error("Negative dist, setting to 1.0 PT");
+             s.distance_f_ = 1.0;
+           }
          if (stretch_dist == 0.0)
            {
              /*
@@ -184,26 +193,46 @@ Spacing_spanner::default_bar_spacing (Paper_column *lc, Paper_column *rc,
   */
   if (delta_t)
     {
-      Real k=  paper_l()->arithmetic_constant (shortest);
-      durational_distance =  paper_l()->length_mom_to_dist (delta_t,k);
+      durational_distance =  get_duration_space (delta_t, shortest);
     }
 
   return  symbol_distance >? durational_distance;
 }
 
 
+/**
+  Get the measure wide constant for arithmetic spacing.
+
+  @see
+  John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
+  OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
+  The Ohio State University, 1987.
+
+  */
+Real
+Spacing_spanner::get_duration_space (Moment d, Moment shortest) const
+{
+  Real log = log_2 (Moment (1,8) <? shortest);
+  Real k=   paper_l ()->get_var ("arithmetic_basicspace")
+    - log;
+  
+  return (log_2 (d) + k) * paper_l ()->get_var ("arithmetic_multiplier");
+}
+
+
 Real
 Spacing_spanner::note_spacing (Paper_column *lc, Paper_column *rc, Moment shortest) const
 {
   Moment shortest_playing_len = 0;
-  SCM s = lc->get_elt_property ("shortest-playing");
+  SCM s = lc->get_elt_property ("shortest-playing-duration");
+  //  SCM s = lc->get_elt_property ("mean-playing-duration");  
   if (SMOB_IS_TYPE_B(Moment, s))
     shortest_playing_len = *SMOB_TO_TYPE (Moment, s);
 
   
   if (! shortest_playing_len)
     {
-      programming_error ("Can't find a ruling note at " + lc->when_mom ().str ());
+      programming_error ("can't find a ruling note at " + lc->when_mom ().str ());
       shortest_playing_len = 1;
     }
   
@@ -213,8 +242,7 @@ Spacing_spanner::note_spacing (Paper_column *lc, Paper_column *rc, Moment shorte
       shortest = 1;
     }
   Moment delta_t = rc->when_mom () - lc->when_mom ();
-  Real k=  paper_l()->arithmetic_constant(shortest);
-  Real dist = paper_l()->length_mom_to_dist (shortest_playing_len, k);
+  Real dist = get_duration_space (shortest_playing_len, shortest);
   dist *= (double)(delta_t / shortest_playing_len);
 
   dist += stem_dir_correction (lc,rc);
@@ -230,13 +258,13 @@ Spacing_spanner::note_spacing (Paper_column *lc, Paper_column *rc, Moment shorte
    This should be more advanced, since relative heights of the note
    heads also influence required correction.
 
-   Also might not work correctly ico. multi voices or staff changing voices
+   Also might not work correctly in case of multi voices or staff
+   changing voices
 
    TODO: lookup correction distances?  More advanced correction?
    Possibly turn this off?
 
-   This routine reads the DIR_LIST property of both its L and R arguments.
-*/
+   This routine reads the DIR-LIST property of both its L and R arguments.  */
 Real
 Spacing_spanner::stem_dir_correction (Paper_column*l, Paper_column*r) const
 {
@@ -287,22 +315,27 @@ Array<Spring>
 Spacing_spanner::get_springs () const
 {
   Array<Spring> springs;
-  
-  SCM last_col = pscore_l_->line_l_->get_elt_property ("columns");
-  Link_array<Paper_column> measure;
-  for (SCM s = last_col; gh_pair_p (s); s = gh_cdr (s))
+
+  Link_array<Paper_column> all (pscore_l_->line_l_->column_l_arr ()) ;
+
+  int j = 0;
+
+  for (int i = 1; i < all.size (); i++)
     {
-      Score_element * elt = unsmob_element (gh_car (s));
-      Paper_column* sc = dynamic_cast<Paper_column*> (elt);
-      measure.push (sc);
+      Paper_column* sc = dynamic_cast<Paper_column*> (all[i]);
       if (sc->breakable_b ())
         {
-         measure.reverse ();
+         Link_array<Paper_column> measure (all.slice (j, i+1));          
           springs.concat (do_measure (measure));
-         measure.clear ();
-         measure.push (sc);
+         j = i;
         }
     }
+
+  /*
+    farewell, cruel world
+   */
+  ((Spacing_spanner*)this)->suicide ();
+  
   return springs;
 }