]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/new-slur.cc
* scm/define-grobs.scm: switch on new-slur by default.
[lilypond.git] / lily / new-slur.cc
index fdef450f4f9bf620221e4851493ecf8024197da1..fe20bcfde844a1630ba9ae24ed33b3557fe09f40 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <math.h>
 
+#include "main.hh"
 #include "font-interface.hh"
 #include "text-item.hh"
 #include "directional-element-interface.hh"
@@ -49,6 +50,28 @@ struct Encompass_info {
   }
 };
 
+struct Bound_info {
+
+  Box stem_extent_;
+  Direction stem_dir_;
+  Grob *bound_;
+  Grob *note_column_;
+  Grob *slur_head_;
+  Grob *staff_;
+  Grob *stem_;
+  Interval slur_head_extent_;
+  Real neighbor_y_;
+
+  Bound_info () {
+    stem_ = 0;
+    neighbor_y_ = 0;
+    staff_ = 0;
+    slur_head_ = 0;
+    stem_dir_ = CENTER;
+    note_column_ = 0;
+  }
+};
+
 /*
   TODO: put in details list.
   */
@@ -62,7 +85,7 @@ const Real SAME_SLOPE_PENALTY = 20;
 const Real STEEPER_SLOPE_FACTOR = 50;
 const Real NON_HORIZONTAL_PENALTY = 15;
 const Real HEAD_STRICT_FREE_SPACE = 0.2;
-const Real MAX_SLOPE = 1.4;
+const Real MAX_SLOPE = 1.1;
 const Real MAX_SLOPE_FACTOR = 10;
 
 
@@ -87,10 +110,13 @@ class New_slur
 public:
   static void add_column (Grob *me, Grob *col);
   DECLARE_SCHEME_CALLBACK (print, (SCM));
-  static void score_slopes (Grob * me,  Grob *common[], Drul_array<Offset> base_attach,
+  static void score_slopes (Grob * me,  Grob *common[],
+                           Drul_array<Bound_info>,
+                           Drul_array<Offset> base_attach,
                            Array<Slur_score> * scores);
   
   static  void score_encompass (Grob * me,  Grob *common[],
+                               Drul_array<Bound_info>,
                                Drul_array<Offset>, Array<Slur_score> * scores);
   static void set_interface (Grob*);
   static bool  has_interface (Grob*);
@@ -103,7 +129,7 @@ public:
 private:
   static void set_end_points (Grob*);
   static Real get_boundary_notecolumn_y (Grob *me, Direction dir);
-  static Offset broken_trend_offset (Grob *me, Direction dir);
+  static Real broken_trend_y (Grob *me, Grob**, Direction dir);
   static Offset get_attachment (Grob *me,Direction dir, Grob **common);
   static void de_uglyfy (Grob *me,Slur_bezier_bow* bb, Real default_height);
   static SCM set_extremities (Grob *me);
@@ -112,6 +138,59 @@ private:
   static Encompass_info get_encompass_info (Grob *me, Grob *col, Grob **common);
 };
 
+Real
+New_slur::broken_trend_y (Grob *me, Grob**common, Direction hdir)
+{
+  /*
+    A broken slur should maintain the same vertical trend
+    the unbroken slur would have had.
+  */
+  Real by = 0.0;
+  if (Spanner *mother =  dynamic_cast<Spanner*> (me->original_))
+    {
+      int k = broken_spanner_index (dynamic_cast<Spanner*> (me));
+      int j = k + hdir;
+      if (j < 0 || j >= mother->broken_intos_.size ())
+       return by;
+      
+      Grob *neighbor = mother->broken_intos_[j];      
+      if (hdir == RIGHT)
+       neighbor->set_property ("direction",
+                                me->get_property ("direction"));
+
+      Spanner * common_mother = dynamic_cast<Spanner*> (common[Y_AXIS]->original_);
+      int common_k = broken_spanner_index (dynamic_cast<Spanner*> (common[Y_AXIS]));
+      int common_j = common_k + hdir;
+
+      if (common_j < 0 || common_j >= common_mother->broken_intos_.size())
+       return by;
+      
+
+      Grob *common_next_system = common_mother->broken_intos_[common_j];
+      Link_array<Grob> neighbor_cols = 
+       Pointer_group_interface__extract_grobs (neighbor, (Grob*)0, "note-columns");
+
+      Grob * neighbor_col = (hdir == RIGHT) ? neighbor_cols[0] : neighbor_cols.top ();
+      Grob * neighbor_common = common_next_system->common_refpoint (neighbor_col, Y_AXIS);
+
+      Direction vdir = get_grob_direction (me);
+      Real neighbor_y =
+       neighbor_col->extent (neighbor_common, Y_AXIS)
+       .linear_combination (int(neighbor_cols.size()==1 ? CENTER : vdir))
+       - common_next_system->relative_coordinate (neighbor_common, Y_AXIS);
+
+      Link_array<Grob> my_cols = 
+       Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
+      
+      Grob *extreme_col = (hdir == RIGHT) ? my_cols.top() : my_cols[0];
+      Real y = extreme_col->extent (common[Y_AXIS], Y_AXIS).linear_combination (vdir);
+      
+      by = (y*neighbor_cols.size() + neighbor_y*my_cols.size()) /
+       (my_cols.size() + neighbor_cols.size());
+    }
+  return by;
+}
+
 void
 New_slur::set_interface (Grob*me)
 {
@@ -144,7 +223,7 @@ New_slur::get_encompass_info (Grob *me,
     {
       programming_error ("No stem for note column?");
       ei.x_ = col->relative_coordinate (common[X_AXIS], X_AXIS);
-      ei.head_ = ei.stem_ = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
+      ei.head_ = ei.stem_ = col->extent (common[Y_AXIS], Y_AXIS)[get_grob_direction (me)];
       return ei;  
     }
   Direction stem_dir = get_grob_direction (stem);
@@ -157,7 +236,7 @@ New_slur::get_encompass_info (Grob *me,
   Grob * h = Stem::extremal_heads (stem)[Direction (dir)];
   if (!h)
     {
-      ei.head_ = ei.stem_ = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
+      ei.head_ = ei.stem_ = col->extent (common[Y_AXIS], Y_AXIS)[dir];
       return ei;  
     }
   
@@ -208,10 +287,6 @@ New_slur::after_line_breaking (SCM smob)
     set_grob_direction (me, get_default_dir (me));
 
   
-  if (!Note_column::has_interface (me->get_bound (LEFT))
-      || !Note_column::has_interface (me->get_bound (RIGHT)))
-    me->suicide ();            // fixme.
-  
   set_end_points (me);
 
   return SCM_UNSPECIFIED;
@@ -244,8 +319,8 @@ New_slur::set_end_points (Grob *me)
       return ; 
     }
   Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
-  Drul_array<Grob *> extremes (columns[0], columns.top ());
-  Direction dir = get_grob_direction (me);
+  Real minimum_length = staff_space * robust_scm2double (me->get_property ("minimum-length"),
+                                                        2.0);
   
   Drul_array<Offset> base_attachment;
 
@@ -258,72 +333,95 @@ New_slur::set_end_points (Grob *me)
   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
 
-  Direction d = LEFT;
-  Drul_array<Grob*> staves;
+
+  Drul_array<Bound_info> extremes;
+  Direction dir = get_grob_direction (me);
   
+  
+  Direction d = LEFT;
   do {
-    Grob *stem = Note_column::get_stem (extremes[d]);
-    Grob * h = Stem::extremal_heads (stem)[Direction (dir)];
-    staves[d] = Staff_symbol_referencer::get_staff_symbol (h);
-
-    common[Y_AXIS] = common[Y_AXIS]->common_refpoint (staves[d], Y_AXIS);
+    extremes[d].bound_ = dynamic_cast<Spanner*> (me)->get_bound (d);
+      
+    if (Note_column::has_interface (extremes[d].bound_))
+      {
+       extremes[d].note_column_ = extremes[d].bound_;
+       extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
+       extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
+       extremes[d].stem_extent_[X_AXIS] = extremes[d].stem_->extent (common[X_AXIS], X_AXIS);
+       extremes[d].stem_extent_[Y_AXIS] = extremes[d].stem_->extent (common[Y_AXIS], Y_AXIS);
+       extremes[d].slur_head_ = Stem::extremal_heads (extremes[d].stem_)[dir];
+       extremes[d].slur_head_extent_ = extremes[d].slur_head_->extent (common[X_AXIS], X_AXIS);
+       extremes[d].staff_ = Staff_symbol_referencer::get_staff_symbol (extremes[d].slur_head_);
+      }
+    else
+      {
+       extremes[d].neighbor_y_ = broken_trend_y (me, common, d);
+      }
   } while (flip (&d) != LEFT);
   
   do
     {
-      // c&p
-      Grob *stem = Note_column::get_stem (extremes[d]);
-      Grob * h = Stem::extremal_heads (stem)[dir];
-      Real y;
-      if (stem
-       && get_grob_direction (stem) == dir
-       && Stem::get_beam (stem))
+      Grob *stem = extremes[d].stem_;
+      Grob *head = extremes[d].slur_head_;
+      
+      Real x,y;
+      if (!extremes[d].note_column_)
        {
-         y = stem->extent (common[Y_AXIS], Y_AXIS)[dir];
+         y = extremes[d].neighbor_y_;
+         if (d== RIGHT)
+           x = extremes[d].bound_->extent (common[X_AXIS], X_AXIS)[d];
+         else
+           x = sp->get_broken_left_end_align ();
        }
       else
        {
-         y = h->extent (common[Y_AXIS], Y_AXIS)[dir];
-       }
-
-      y += dir * 0.5 * staff_space;
-
-      Grob * staff = Staff_symbol_referencer::get_staff_symbol (h);
-      Real pos = 2.0 * (y - staff->relative_coordinate (common[Y_AXIS], Y_AXIS))
-       / Staff_symbol::staff_space (staff);
-
-      
-      /*
-       start off staffline.
-      */
-      if (fabs (pos - round (pos)) < 0.2
-         && Staff_symbol_referencer::on_staffline (h, (int) rint (pos))
-         && Staff_symbol_referencer::line_count (h) -1 >= rint (pos)
-         )
+         if (stem
+             && extremes[d].stem_dir_ == dir
+             && Stem::get_beaming (stem, -d)
+             && columns.size () > 2
+             )
+           {
+             y = extremes[d].stem_extent_[Y_AXIS][dir];
+           }
+         else if (head) 
+           {
+             y = head->extent (common[Y_AXIS], Y_AXIS)[dir];
+           }
+         y += dir * 0.5 * staff_space;
+
+         Real pos = 2.0 * (y - extremes[d].staff_->relative_coordinate (common[Y_AXIS], Y_AXIS))
+           / Staff_symbol::staff_space (extremes[d].staff_);
+
+         /*
+           start off staffline.
+         */
+         if (fabs (pos - round (pos)) < 0.2
+             && Staff_symbol_referencer::on_staffline (head, (int) rint (pos))
+             && Staff_symbol_referencer::line_count (head) -1 >= rint (pos)
+             )
            y += staff_space * dir / 10 ;         
-
-      
       
-      Grob * fh = Note_column::first_head (extremes[d]);
-      Real x = fh->extent (common[X_AXIS],  X_AXIS).linear_combination (CENTER);
-
-      if (get_grob_direction (stem) == dir
-         && dir == -d)
-       {
-         x -=  d * fh->extent(fh, X_AXIS).length ();
+         Grob * fh = Note_column::first_head (extremes[d].note_column_);
+         x = fh->extent (common[X_AXIS],  X_AXIS).linear_combination (CENTER);
        }
-    
       base_attachment[d] = Offset (x, y);
 
     } while (flip (&d) != LEFT);
 
-  Drul_array<Real> staff_offsets;
   Interval end_ys;
-
   do {
-    staff_offsets[d] = staves[d]->relative_coordinate (common[Y_AXIS], Y_AXIS);
-    end_ys[d] =  dir * ((dir * (base_attachment[d][Y_AXIS] + 4.0 *dir)) >?
-                       (dir * (dir + extremes[d]->extent(common[Y_AXIS],Y_AXIS)[dir])));
+    if (extremes[d].note_column_)
+      {
+       end_ys[d] =  dir * ((dir * (base_attachment[d][Y_AXIS] + 4.0 *dir))
+                           >? (dir * (dir + extremes[d].note_column_->extent(common[Y_AXIS],Y_AXIS)[dir]))
+                           >? (dir * base_attachment[-d][Y_AXIS])
+                           );
+      }
+    else
+      {
+       end_ys[d] = extremes[d].neighbor_y_ + 4.0 * dir ;
+      }
   } while (flip (&d) != LEFT);
   
   Array<Slur_score> scores;
@@ -333,30 +431,51 @@ New_slur::set_end_points (Grob *me)
   /*ugh.   */
   os[LEFT] = base_attachment[LEFT];
     
-  for (int i = 0; dir * os[LEFT][Y_AXIS] < dir * end_ys[LEFT]; i++)
+  for (int i = 0; dir * os[LEFT][Y_AXIS] <= dir * end_ys[LEFT]; i++)
     {
       os[RIGHT] = base_attachment[RIGHT];
-      for (int j = 0; dir *os[RIGHT][Y_AXIS] < dir * end_ys[RIGHT]; j++)
+      for (int j = 0; dir *os[RIGHT][Y_AXIS] <= dir * end_ys[RIGHT]; j++)
        {
          Slur_score s;
-         s.attachment_ = os;
+         Direction d = LEFT;
 
+         
+         do {  
+           os[d][X_AXIS] = base_attachment[d][X_AXIS];
+           if (extremes[d].stem_
+               && !Stem::is_invisible (extremes[d].stem_)
+               && extremes[d].stem_dir_ == dir
+               && dir == -d)
+             {
+               if (extremes[d].stem_extent_[Y_AXIS].contains (os[d][Y_AXIS]))
+                 {
+                   os[d][X_AXIS] -=  d * extremes[d].slur_head_extent_.length ();
+                 }
+               else if (dir *extremes[d].stem_extent_[Y_AXIS][dir] < dir * os[d][Y_AXIS])
+                 {
+                   os[d][X_AXIS] = extremes[d].stem_extent_[X_AXIS].center();
+                 }
+             }
+         } while (flip (&d) != LEFT);
+
+         Offset dz = os[RIGHT] - os[LEFT];
+         if (dz[X_AXIS] < minimum_length
+             || fabs (dz[Y_AXIS] / dz[X_AXIS])  > MAX_SLOPE
+             )
+           {
+             do {
+               if (extremes[d].slur_head_)
+                 os[d][X_AXIS] = extremes[d].slur_head_extent_.center ();
+             } while (flip (&d) != LEFT);
+           }
+         
+         s.attachment_ = os;
          scores.push (s);
-
-         Real incr = dir * staff_space;
-         if  (Staff_symbol_referencer::staff_radius (staves[RIGHT])
-             < fabs ((os[RIGHT][Y_AXIS] - staff_offsets[RIGHT]) / staff_space))
-           incr /= 2;
-
-         os[RIGHT][Y_AXIS] += incr;      
+         
+         os[RIGHT][Y_AXIS] += dir * staff_space / 2;
        }
 
-      Real incr = dir * staff_space;
-      if  (Staff_symbol_referencer::staff_radius (staves[LEFT])
-          < fabs ((os[LEFT][Y_AXIS] - staff_offsets[LEFT]) / staff_space))
-       incr /= 2;
-
-      os[LEFT][Y_AXIS] += incr;          
+      os[LEFT][Y_AXIS] += dir * staff_space /2 ;
     }
   
   {
@@ -369,8 +488,8 @@ New_slur::set_end_points (Grob *me)
       }
   }
   
-  score_encompass (me, common, base_attachment, &scores);
-  score_slopes (me, common, base_attachment, &scores);
+  score_encompass (me, common, extremes, base_attachment, &scores);
+  score_slopes (me, common, extremes, base_attachment, &scores);
 
   Real opt = 1e6;
   int opt_idx = 0;
@@ -408,7 +527,9 @@ New_slur::set_end_points (Grob *me)
 }
 
 void
-New_slur::score_encompass (Grob * me,  Grob *common[], Drul_array<Offset> base_attach,
+New_slur::score_encompass (Grob * me,  Grob *common[],
+                          Drul_array<Bound_info> extremes,
+                          Drul_array<Offset> base_attach,
                           Array<Slur_score> * scores)
 {
   Link_array<Grob> encompasses =
@@ -416,23 +537,10 @@ New_slur::score_encompass (Grob * me,  Grob *common[], Drul_array<Offset> base_a
   Direction dir = get_grob_direction (me);
  
   Array<Encompass_info> infos;
-  Drul_array<Grob *> extremes (encompasses[0], encompasses.top ());
-
-  int first = 1;
-  int last = encompasses.size () - 2;
 
-  for (int i = first; i <= last; i++)
+  for (int i = 0; i < encompasses.size(); i++)
     infos.push (get_encompass_info (me, encompasses[i], common));
 
-  Drul_array<Grob*> stems;
-  Direction d = LEFT;
-  do {
-    Grob *stem = Note_column::get_stem (extremes [d]);
-    stems[d] = stem;
-  } while (flip (&d) != LEFT);
-
-
   for (int i =0 ; i < scores->size (); i++)
     {
       Bezier const &bez (scores->elem (i).curve_);
@@ -452,7 +560,7 @@ New_slur::score_encompass (Grob * me,  Grob *common[], Drul_array<Offset> base_a
          
          if (dir * (y - infos[j].stem_) < 0)
            demerit += STEM_ENCOMPASS_PENALTY;
-         else
+         else if (j && j < encompasses.size () - 1)  
            {
              Interval ext;
              ext.add_point (infos[j].stem_);
@@ -465,11 +573,10 @@ New_slur::score_encompass (Grob * me,  Grob *common[], Drul_array<Offset> base_a
 
       Direction d = LEFT;
       do {
-
        Real attr =
          EDGE_ATTRACTION_FACTOR
          * fabs (scores->elem (i).attachment_[d][Y_AXIS] - base_attach[d][Y_AXIS]);
-       if (get_grob_direction (stems[d]) == dir)
+       if (extremes[d].stem_ && extremes[d].stem_dir_ == dir)
          attr /= 5;
        
        demerit += attr;
@@ -485,57 +592,52 @@ New_slur::score_encompass (Grob * me,  Grob *common[], Drul_array<Offset> base_a
 
 
 void
-New_slur::score_slopes (Grob * me,  Grob *common[], Drul_array<Offset> base_attach,
+New_slur::score_slopes (Grob * me,  Grob *common[],
+                       Drul_array<Bound_info> extremes,
+                       Drul_array<Offset> base_attach,
                        Array<Slur_score> * scores)
 {
- Link_array<Grob> columns =
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
-
-  Drul_array<Grob *> extremes (columns[0], columns.top ());
   Direction dir = get_grob_direction (me);
   Drul_array<Real> ys;
 
   Direction d = LEFT;
-  Drul_array<Direction> stem_dirs;
-  Drul_array<bool> beams;
   do {
-    Grob *stem = Note_column::get_stem (extremes [d]);
-    ys[d] = Stem::extremal_heads (stem)[Direction (dir)]
-      ->relative_coordinate (common[Y_AXIS], Y_AXIS);
 
-    stem_dirs[d] = get_grob_direction (stem);
-    beams[d] = Stem::get_beam (stem);
+    if (extremes[d].slur_head_)
+      ys[d] = extremes[d].slur_head_ ->relative_coordinate (common[Y_AXIS], Y_AXIS);
+    else
+      ys[d] = extremes[d].neighbor_y_;
   } while (flip (&d) != LEFT);
 
-  Real dx = extremes[RIGHT]->relative_coordinate (common[X_AXIS],X_AXIS)
-    - extremes[LEFT]->relative_coordinate (common[X_AXIS],X_AXIS);
+  bool has_beams =
+    (extremes[LEFT].stem_ && Stem::get_beam (extremes[LEFT].stem_))
+    || (extremes[RIGHT].stem_ && Stem::get_beam (extremes[RIGHT].stem_));
 
   Real dy = ys[RIGHT] - ys[LEFT];
   for (int i =0 ; i < scores->size (); i++)
     {
-      Real slur_dy = (*scores)[i].attachment_[RIGHT][Y_AXIS]
-       -  (*scores)[i].attachment_[LEFT][Y_AXIS];
+      Offset slur_dz = (*scores)[i].attachment_[RIGHT]
+       -  (*scores)[i].attachment_[LEFT];
 
-      Real demerit = 0.0;
+      Real slur_dy = slur_dz[Y_AXIS]; 
 
 
-      if(! (beams[LEFT] || beams[RIGHT]))
+      Real demerit = 0.0;
+
+      if (!has_beams)
        demerit += STEEPER_SLOPE_FACTOR *  (dir * (fabs (slur_dy) - fabs (dy)) >? 0);
 
-      demerit += ((fabs (slur_dy/dx) - MAX_SLOPE)>?0)  * MAX_SLOPE_FACTOR;
+      demerit += ((fabs (slur_dy/slur_dz[X_AXIS]) - MAX_SLOPE)>?0)  * MAX_SLOPE_FACTOR;
       
       if (sign (dy) == 0 &&
          sign (slur_dy) != 0)
        demerit += NON_HORIZONTAL_PENALTY;
 
-       
-
       if (sign (dy)
          && sign (slur_dy)
          && sign (slur_dy) != sign (dy))
        demerit +=
-         (beams[LEFT] || beams[RIGHT])
-         ? SAME_SLOPE_PENALTY/10 : SAME_SLOPE_PENALTY;
+         has_beams ? SAME_SLOPE_PENALTY/10 : SAME_SLOPE_PENALTY;
       
 #if DEBUG_SLUR_QUANTING
       (*scores)[i].score_card_ += to_string ("S%.2f",d);
@@ -559,13 +661,6 @@ New_slur::get_curve (Grob*me)
 }
 
 
-
-
-
-
-/*
-  ugh ?
- */
 MAKE_SCHEME_CALLBACK (New_slur, height, 2);
 SCM
 New_slur::height (SCM smob, SCM ax)
@@ -619,7 +714,8 @@ New_slur::print (SCM smob)
 
 #if DEBUG_SLUR_QUANTING
   SCM quant_score = me->get_property ("quant-score");
-  if (// debug_beam_quanting_flag      &&
+  
+  if (debug_beam_quanting_flag      &&
       ly_c_string_p (quant_score))
     {
       String str;