]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur-scoring.cc
*** empty log message ***
[lilypond.git] / lily / slur-scoring.cc
index 4cc4490aafa4934b6c9ef4edd3b2c4fbf2d3a16c..7230c975e9e573a3334e27c23b688f3b8eba105c 100644 (file)
@@ -3,24 +3,23 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
+#include "slur-scoring.hh"
+
 #include <math.h>
 
 #include "libc-extension.hh"
 #include "slur-configuration.hh"
-#include "slur-scoring.hh"
 #include "beam.hh"
 #include "directional-element-interface.hh"
 #include "group-interface.hh"
-#include "lily-guile.hh"
 #include "slur.hh"
 #include "note-column.hh"
 #include "output-def.hh"
 #include "pitch.hh"
-#include "bezier.hh"
 #include "spanner.hh"
 #include "staff-symbol-referencer.hh"
 #include "staff-symbol.hh"
@@ -287,7 +286,7 @@ Slur_score_state::fill (Grob *me)
     }
 
   staff_space_ = Staff_symbol_referencer::staff_space (me);
-  Real lt = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
+  Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
   thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) *  lt;
   
   dir_ = get_grob_direction (me);
@@ -305,7 +304,12 @@ Slur_score_state::fill (Grob *me)
 
       Direction d = LEFT;
       do {
-       common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
+       /*
+         If bound is not in note-columns, we don't want to know about
+         its Y-position
+        */
+       if (a != Y_AXIS) 
+         common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
       }
       while (flip (&d) != LEFT);
     }
@@ -314,6 +318,9 @@ Slur_score_state::fill (Grob *me)
   is_broken_ = (!extremes_[LEFT].note_column_
                || !extremes_[RIGHT].note_column_); 
 
+  has_same_beam_ =
+    (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
+     && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
   
   base_attachments_ = get_base_attachments ();
 
@@ -342,9 +349,6 @@ Slur_score_state::fill (Grob *me)
     = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
     || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
 
-  has_same_beam_ =
-    (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
-     && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
 
   
   set_next_direction ();
@@ -363,8 +367,18 @@ set_slur_control_points (Grob *me)
     return;
   
   state.generate_curves ();
-  Bezier best (state.get_best_curve());
 
+  SCM end_ys = me->get_property ("positions");
+  Bezier best;
+
+  if (is_number_pair (end_ys))
+    {
+      best = state.configurations_[state.get_closest_index (end_ys)]->curve_;
+    }
+  else
+    {
+      best = state.get_best_curve();
+    }
 
   SCM controls = SCM_EOL;
   for (int i = 4; i--;)
@@ -399,38 +413,45 @@ Slur_score_state::get_best_curve ()
 
 #if DEBUG_SLUR_SCORING
   SCM inspect_quants = slur_->get_property ("inspect-quants");
-  if (to_boolean (slur_->get_paper ()
+  if (to_boolean (slur_->get_layout ()
                  ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
       && scm_is_pair (inspect_quants))
-    {
-      Drul_array<Real> ins = ly_scm2interval (inspect_quants);
-      Real mindist = 1e6;
-      for (int i = 0; i < configurations_.size (); i ++)
-       {
-         Real d =fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
-           + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
-         if (d < mindist)
-           {
-             opt_idx = i;
-             mindist= d;
-           }
-       }
-      if (mindist > 1e5)
-       programming_error ("Could not find quant.");
-    }
+    opt_idx = get_closest_index (inspect_quants);
 
   configurations_[opt_idx]->score_card_ += to_string ("=%.2f", opt);  
   configurations_[opt_idx]->score_card_ += to_string ("i%d", opt_idx);
 
   // debug quanting
   slur_->set_property ("quant-score",
-                   scm_makfrom0str (configurations_[opt_idx]->score_card_.to_str0 ()));
+                      scm_makfrom0str (configurations_[opt_idx]->score_card_.to_str0 ()));
 
 #endif
 
   return configurations_[opt_idx]->curve_;
 }
 
+int
+Slur_score_state::get_closest_index (SCM inspect_quants) const
+{
+  Drul_array<Real> ins = ly_scm2interval (inspect_quants);
+
+  int opt_idx = -1;
+  Real mindist = 1e6;
+  for (int i = 0; i < configurations_.size (); i ++)
+    {
+      Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
+       + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
+      if (d < mindist)
+       {
+         opt_idx = i;
+         mindist = d;
+       }
+    }
+  if (mindist > 1e5)
+    programming_error ("Could not find quant.");
+  return opt_idx;
+}
+
 /*
   TODO: should analyse encompasses to determine sensible region, and
   should limit slopes available.
@@ -503,18 +524,8 @@ Slur_score_state::get_base_attachments () const
            y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
          y += dir_ * 0.5 * staff_space_;
 
-         Real pos
-           = (y - extremes_[d].staff_->relative_coordinate (common_[Y_AXIS],
-                                                           Y_AXIS))
-           * 2.0 / staff_space_;
 
-         /* start off staffline. */
-         if (fabs (pos - my_round (pos)) < 0.2
-             && Staff_symbol_referencer::on_staffline (head, (int) rint (pos))
-             && Staff_symbol_referencer::line_count (head) - 1 >= rint (pos)
-             )
-           // TODO: calc from slur thick & line thick, parameter.      
-           y += 1.5 * staff_space_ * dir_ / 10;
+         y = move_away_from_staffline (y, head); 
 
          Grob * fh = Note_column::first_head (extremes_[d].note_column_);
          x =
@@ -544,12 +555,19 @@ Slur_score_state::get_base_attachments () const
          if (extremes_[-d].bound_ != col)
            {
              y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
-             if (get_grob_direction (col) == dir_)
-               y -= dir_ ;
+             y += dir_ * 0.5 * staff_space_;
+             
+             if (get_grob_direction (col) == dir_
+                 && Note_column::get_stem (col)
+                 && !Stem::is_invisible (Note_column::get_stem (col)))
+               y -= dir_ * 1.5 * staff_space_;
            }
          else
            y = base_attachment[-d][Y_AXIS];
 
+         
+         y = move_away_from_staffline (y, col);
+           
          base_attachment[d] = Offset (x, y);  
        }
     }
@@ -558,6 +576,24 @@ Slur_score_state::get_base_attachments () const
   return base_attachment;
 }
 
+Real
+Slur_score_state::move_away_from_staffline (Real y,
+                                           Grob *on_staff) const
+{
+  Real pos
+    = (y - Staff_symbol_referencer::get_staff_symbol (on_staff)->relative_coordinate (common_[Y_AXIS],
+                                                                              Y_AXIS))
+    * 2.0 / staff_space_;
+  
+  if (fabs (pos - my_round (pos)) < 0.2
+      && Staff_symbol_referencer::on_staffline (on_staff, (int) rint (pos))
+      && Staff_symbol_referencer::line_count (on_staff) - 1 >= rint (pos)
+      )
+    y += 1.5 * staff_space_ * dir_ / 10;
+
+  return y;
+}
+
 void
 Slur_score_state::generate_curves () const
 {
@@ -599,15 +635,14 @@ Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
                {
                  Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
                  stem_y.widen (0.25 * staff_space_);
-                 if (dir_ == -d
-                     && stem_y.contains (os[d][Y_AXIS]))
+                 if (stem_y.contains (os[d][Y_AXIS]))
                    {
-                     os[d][X_AXIS] =  extremes_[d].slur_head_extent_[-d]
+                     os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
                        - d * 0.3;
                      attach_to_stem[d] = true;
                    }
-                 else if (dir_ *extremes_[d].stem_extent_[Y_AXIS][dir_]
-                            < dir_ * os[d][Y_AXIS]
+                 else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
+                          < dir_ * os[d][Y_AXIS]
                           && !extremes_[d].stem_extent_[X_AXIS].is_empty ()
                           )