]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spacing-spanner.cc
patch::: 1.3.33.jcn3
[lilypond.git] / lily / spacing-spanner.cc
index 4979c7b5a3c0ebebd6a7c78e778b61ca48bb510f..52ff8c05e19da738def546c1c2606ec518ec6339 100644 (file)
@@ -3,34 +3,24 @@
   
   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 "spacing-spanner.hh"
-#include "score-column.hh"
+#include "paper-column.hh"
 #include "dimensions.hh"
 #include "paper-def.hh"
 #include "warn.hh"
-#include "p-score.hh"
+#include "paper-score.hh"
 #include "line-of-score.hh"
+#include "misc.hh"
 
 Spacing_spanner::Spacing_spanner ()
 {
-  set_elt_property (break_helper_only_scm_sym, SCM_BOOL_T);
-  set_elt_property (transparent_scm_sym, SCM_BOOL_T);
-}
-
-int
-Spacing_spanner::col_count () const
-{
-  return pscore_l_->line_l_->cols_.size ();
-}
-
-Score_column *
-Spacing_spanner::scol (int i)const
-{
-  return dynamic_cast<Score_column*> (pscore_l_->line_l_->cols_[i]);
+  set_empty (X_AXIS);
+  set_empty (Y_AXIS);  
+  set_elt_property ("transparent", SCM_BOOL_T);
 }
 
 /*
@@ -39,226 +29,313 @@ Spacing_spanner::scol (int i)const
   generate springs between columns.
 
 
-  TODO
-  
-  * Spacing should take optical effects into account
-  
   The algorithm is partly taken from :
 
   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.
+
+  TOO HAIRY.
   
  */
 Array<Spring>
-Spacing_spanner::do_measure (int col1, int col2) const
+Spacing_spanner::do_measure (Link_array<Paper_column> cols) const
 {
-  for (int i =col1; i < col2; i++)
-    {
-      scol (i)->preprocess ();
-      scol (i)->print ();
-    }
-
   Moment shortest;
+  Moment mean_shortest;
   shortest.set_infinite (1);
-  for (int i =col1; i < col2; i++)
+
+  int n = 0;
+  for (int i =0 ; i < cols.size (); i++)  
     {
-      if (scol(i)->musical_b ())
+      if (cols[i]->musical_b ())
        {
-         shortest = shortest <? scol(i)->shortest_starter_mom_;
+         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;
 
-  /*
-    UGR GUR URG.  duplicate code for spacing generation.
-   */
-  for (int i= col1; i < col2; i++)
+  Real non_musical_space_strength = paper_l ()->get_var ("breakable_column_space_strength");
+  for (int i= 0; i < cols.size () - 1; i++)
     {
-      SCM hint = scol (i)->get_elt_property (extra_space_scm_sym);
-      if (hint != SCM_BOOL_F)
+      Item * l = cols[i];
+      Item * r = cols[i+1];
+      Item * lb = l->find_broken_piece (RIGHT);
+      Item * rb = r->find_broken_piece (LEFT);      
+
+      Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
+
+      for (int j=0; j < 4; j++)
        {
-         hint = SCM_CDR (hint);
+         Paper_column * lc = dynamic_cast<Paper_column*> (combinations[j][0]);
+         Paper_column *rc = dynamic_cast<Paper_column*> (combinations[j][1]);
+         if (!lc || !rc)
+           continue;
 
          Spring s;
-         s.item_l_drul_[LEFT] = scol (i);
-         s.item_l_drul_[RIGHT] = scol (i+1);
-         Real unbroken_dist =  gh_scm2double (SCM_CDR(hint));
-
-         s.distance_f_ = unbroken_dist;
-         s.strength_f_ = 2.0;
+         s.item_l_drul_[LEFT] = lc;
+         s.item_l_drul_[RIGHT] = rc;
          
+         SCM hint = lc->get_elt_property ("extra-space");
+         SCM next_hint = rc->get_elt_property ("extra-space");
+         SCM stretch_hint = lc->get_elt_property ("stretch-distance");
+         SCM next_stretch_hint = rc->get_elt_property ("stretch-distance");      
 
-         meas_springs.push (s);
-
-         
-         Item * l = scol(i)->find_prebroken_piece (RIGHT);
-         Item * r = scol(i+1)->find_prebroken_piece (LEFT);
-         if (l)
+         Real left_distance;
+         if (gh_pair_p (hint))
            {
-               Spring s;
-               s.item_l_drul_[LEFT] = l;
-               s.item_l_drul_[RIGHT] = scol (i+1);
-               hint = l->get_elt_property (extra_space_scm_sym);
-
-               if (hint == SCM_BOOL_F)
-                 {
-                   programming_error ("No postbreak breakable spacing hint set.");
-                   s.distance_f_= unbroken_dist;
-                 }
-               else
-                 s.distance_f_ =  gh_scm2double (SCM_CDDR(hint));
-
-               /*
-                 space around barlines should not stretch very much.
-                */
-               s.strength_f_ = 2.0;
-               meas_springs.push (s);
+             left_distance = gh_scm2double (gh_cdr (hint)); 
            }
-
-         if (r)
+          // 2nd condition should be (i+1 < col_count()), ie. not the last column in score.  FIXME
+         else if (!lc->musical_b() && i+1 < cols.size ()) 
            {
-             Spring s;
-             s.item_l_drul_[LEFT] = scol (i);
-             s.item_l_drul_[RIGHT] = r;
-             s.distance_f_ =  unbroken_dist;
-             
-             /*
-               space around barlines should not stretch very much.
-                */
-             s.strength_f_ = 2.0;
-             meas_springs.push (s);
+             left_distance= default_bar_spacing (lc,rc,shortest);
            }
-
-         if (l&&r)
+         else if (lc->musical_b())
            {
-             Spring s;
-             s.item_l_drul_[LEFT] = l;
-             s.item_l_drul_[RIGHT] = r;
-             
-             hint = l->get_elt_property (extra_space_scm_sym);
-             if (hint == SCM_BOOL_F)
-               {
-                 programming_error ("No postbreak breakable spacing hint set.");
-                 s.distance_f_= unbroken_dist;
-               }
-             else
-               s.distance_f_ =  gh_scm2double (SCM_CDDR(hint));
-             
-             /*
-               space around barlines should not stretch very much.
-             */
-             s.strength_f_ = 2.0;
-             meas_springs.push (s);
+             left_distance  = note_spacing (lc, rc, shortest);
            }
-       }
-      else if (!scol (i)->musical_b() && i+1 < col_count())
-       {
-         Real symbol_distance = scol (i)->extent (X_AXIS)[RIGHT] ;
-         Real durational_distance = 0;
-         Moment delta_t =  scol (i+1)->when_mom () - scol (i)->when_mom () ;
+
+         s.distance_f_ = left_distance;
+
          /*
-           ugh should use shortest_playing distance
+           Only do tight spaces *after* barlines (breakable columns),
+           not before.
+
+           We want the space before barline to be like the note
+           spacing in the measure.
          */
-         if (delta_t)
+         if (lc->breakable_b () || lc->original_l_)
+           s.strength_f_ = non_musical_space_strength;
+         else if (!lc->musical_b ())
+           left_distance *= paper_l ()->get_var ("decrease_nonmus_spacing_factor");
+
+         
+         Real right_dist = 0.0;
+         if (gh_pair_p (next_hint))
            {
-             Real k=  paper_l()->arithmetic_constant (shortest);
-             durational_distance =  paper_l()->length_mom_to_dist (delta_t,k);
+             right_dist += - gh_scm2double (gh_car (next_hint));
+           }
+         else
+           {
+             Interval ext (rc->extent (X_AXIS));
+             right_dist =  ext.empty_b() ? 0.0 : - ext [LEFT];
            }
-         symbol_distance += -scol (i+1)->extent(X_AXIS)[LEFT];
-
-         Spring s ;
-         s.item_l_drul_[LEFT] = scol (i);
-         s.item_l_drul_[RIGHT] = scol (i+1);
-         s.distance_f_ =  symbol_distance >? durational_distance;
-         meas_springs.push (s);
 
-         Item *l = s.item_l_drul_[LEFT]->find_prebroken_piece (RIGHT);
-         Item *r = s.item_l_drul_[RIGHT]->find_prebroken_piece (LEFT);
-         Spring sp_orig (s);
-         
-         if (l)
+         /*
+           don't want to create too much extra space for accidentals
+         */
+         if (lc->musical_b () && rc->musical_b ())
            {
-             s = sp_orig;
-             s.item_l_drul_[LEFT] =l ;
-             meas_springs.push (s);
+             if (!to_boolean (rc->get_elt_property ("contains-grace")))
+               right_dist *= paper_l ()->get_var ("musical_to_musical_left_spacing_factor");
            }
 
-         if (l && r)
+         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;
+           
+         Real stretch_dist = 0.;
+         if (gh_number_p (stretch_hint))
+           stretch_dist += gh_scm2double (stretch_hint);
+         else
+           stretch_dist += left_distance;
+         
+         if (gh_pair_p (next_stretch_hint))
+           // see regtest spacing-tight
+           stretch_dist += - gh_scm2double (gh_car  (next_stretch_hint));
+         else
+           stretch_dist += right_dist;
+
+         if (s.distance_f_ <0)
+           programming_error("negative dist");
+         
+         if (stretch_dist == 0.0)
            {
-             s = sp_orig;
-             s.item_l_drul_[RIGHT] = r;
-             s.item_l_drul_[LEFT] = l;
-             meas_springs.push (s);
+             /*
+               \bar "".  We give it 0 space, with high strength. 
+              */
+             s.strength_f_ = 20.0; 
            }
+         else
+           s.strength_f_ /= stretch_dist;
          
+         meas_springs.push (s);        
        }
     }
 
-  for (int i=col1; i < col2; i++)
+  return meas_springs;
+}
+
+/**
+   Do something if breakable column has no spacing hints set.
+ */
+Real
+Spacing_spanner::default_bar_spacing (Paper_column *lc, Paper_column *rc,
+                                     Moment shortest) const
+{
+  Real symbol_distance = lc->extent (X_AXIS)[RIGHT] ;
+  Real durational_distance = 0;
+  Moment delta_t =  rc->when_mom () - lc->when_mom () ;
+
+  /*
+               ugh should use shortest_playing distance
+  */
+  if (delta_t)
     {
-      if (scol (i)->musical_b())
-       {
-         Moment shortest_playing_len = scol(i)->shortest_playing_mom_;
-         if (! shortest_playing_len)
-           {
-             warning (_f ("can't find a ruling note at %s", 
-               scol (i)->when_mom ().str ()));
-             shortest_playing_len = 1;
-           }
-         if (! shortest)
-           {
-             warning (_f ("no minimum in measure at %s", 
-                     scol (i)->when_mom ().str ()));
-             shortest = 1;
-           }
-         Moment delta_t = scol (i+1)->when_mom () - scol (i)->when_mom ();
-         Real k=  paper_l()->arithmetic_constant(shortest);
-         Real dist = paper_l()->length_mom_to_dist (shortest_playing_len, k);
-         dist *= (double)(delta_t / shortest_playing_len);
+      durational_distance =  get_duration_space (delta_t, shortest);
+    }
 
+  return  symbol_distance >? durational_distance;
+}
 
-         Spring sp;
-         sp.distance_f_ =  dist;
-         sp.item_l_drul_[LEFT] = scol (i);
-         sp.item_l_drul_[RIGHT] = scol (i+1);
 
-         meas_springs.push (sp);
+/**
+  Get the measure wide constant for arithmetic spacing.
 
-         /*
-           UGH. TODO: more
-           advanced spacing here.
-          */
-         Spring sp_orig (sp);
+  @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.
 
-         Item *r =  sp.item_l_drul_[RIGHT]->find_prebroken_piece (LEFT);
-         
-         if (r)
-           {
-             sp = sp_orig;
-             sp.item_l_drul_[RIGHT] =r ;
-             meas_springs.push (sp);
-           }
-       }
+  */
+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-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 ());
+      shortest_playing_len = 1;
     }
-  return meas_springs;
+  
+  if (! shortest)
+    {
+      programming_error ("no minimum in measure at " + lc->when_mom ().str ());
+      shortest = 1;
+    }
+  Moment delta_t = rc->when_mom () - lc->when_mom ();
+  Real dist = get_duration_space (shortest_playing_len, shortest);
+  dist *= (double)(delta_t / shortest_playing_len);
+
+  dist += stem_dir_correction (lc,rc);
+  return dist;
 }
 
+
+/**
+   Correct for optical illusions. See [Wanske] p. 138. The combination
+   up-stem + down-stem should get extra space, the combination
+   down-stem + up-stem less.
+
+   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
+
+   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.
+*/
+Real
+Spacing_spanner::stem_dir_correction (Paper_column*l, Paper_column*r) const
+{
+  SCM dl = l->get_elt_property ("dir-list");
+  SCM dr = r->get_elt_property ("dir-list");
+  if (dl == SCM_UNDEFINED || dr == SCM_UNDEFINED)
+    return 0.0;
+
+
+  if (scm_ilength (dl) != 1 && scm_ilength (dr) != 1)
+    return 0.;
+
+  dl = gh_car (dl);
+  dr = gh_car (dr);
+
+  assert (gh_number_p (dl) && gh_number_p(dr));
+  int d1 = gh_scm2int (dl);
+  int d2 = gh_scm2int (dr);
+
+  if (d1 == d2)
+    return 0.0;
+
+  bool err = false;
+  Real correction = 0.0;
+  Real ssc = paper_l ()->get_var("stemSpacingCorrection");
+
+
+  if (d1 && d2)
+    {
+      if (d1 == 1 && d2 == -1)
+       correction = ssc;
+      else if (d1 == -1 && d2 == 1)
+       correction = -ssc;
+      else
+       err = true;
+    }
+  
+  else
+    err = true;
+
+  if (err)
+    programming_error ("Stem directions not set correctly for optical correction");
+  return correction;
+}
+  
+
 Array<Spring>
 Spacing_spanner::get_springs () const
 {
   Array<Spring> springs;
-  int last_break =0;
-  for (int i=1; i < col_count (); i++)
+  
+  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))
     {
-      if (scol (i)->breakable_b ())
-       {
-         springs.concat (do_measure (last_break, i));
-         last_break  = i;
-       }
+      Score_element * elt = unsmob_element (gh_car (s));
+      Paper_column* sc = dynamic_cast<Paper_column*> (elt);
+      measure.push (sc);
+      if (sc->breakable_b ())
+        {
+         measure.reverse ();
+          springs.concat (do_measure (measure));
+         measure.clear ();
+         measure.push (sc);
+        }
     }
   return springs;
 }
 
 
+
+
+