]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tie-formatting-problem.cc
Merge branch 'cvs-head' of ssh+git://hanwen@repo.or.cz/srv/git/lilypond into master...
[lilypond.git] / lily / tie-formatting-problem.cc
index aca4338730dae4cd105ce636a0b2e9eaf61c3a2d..d22d54bb6a28d43719a13ca5cc1b5111d6af6e88 100644 (file)
@@ -40,13 +40,18 @@ Tie_formatting_problem::print_ties_configuration (Ties_configuration const *ties
 }
 
 Interval
-Tie_formatting_problem::get_attachment (Real y) const
+Tie_formatting_problem::get_attachment (Real y, Drul_array<int> columns) const
 {
-  Interval attachments;
+  Interval attachments (0,0);
   Direction d = LEFT;
   do
     {
-      attachments[d] = skyline_height (chord_outlines_[d], y, -d);
+      Tuple2<int> key (columns[d], int (d));
+      Chord_outline_map::const_iterator i (chord_outlines_.find (key));
+      if (i == chord_outlines_.end ())
+       programming_error ("Can't find chord outline");
+      else
+       attachments[d] = skyline_height ((*i).second, y, -d);
     }
   while (flip (&d) != LEFT);
   
@@ -66,8 +71,9 @@ Tie_formatting_problem::~Tie_formatting_problem ()
 }
 
 void
-Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
-                                          Direction dir)
+Tie_formatting_problem::set_column_chord_outline (vector<Item*> bounds,
+                                                 Direction dir,
+                                                 int column_rank)
 {
   Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
 
@@ -80,7 +86,7 @@ Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
       Grob *head = bounds[i];
       if (!Note_head::has_interface (head))
        continue;
-      
+         
       if (!stem)
        stem = unsmob_grob (head->get_object ("stem"));
          
@@ -108,25 +114,28 @@ Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
        }
     }
 
-  chord_outlines_[dir] = empty_skyline (-dir);
+  Tuple2<int> key (column_rank, int (dir));
+  
+  chord_outlines_[key] = empty_skyline (-dir);
+      
   if (bounds[0]->break_status_dir ())
     {
       Real x = robust_relative_extent (bounds[0],  x_refpoint_, X_AXIS)[-dir];
-      chord_outlines_[dir].at (0).height_ = x; 
+      chord_outlines_[key].at (0).height_ = x; 
     }
   else
     {
-      Interval x;      
-      for (vsize i = 0; i < head_boxes.size (); i++)
+      Interval x;
+      for (vsize j = 0; j < head_boxes.size (); j++)
        {
-         x.unite (head_boxes[i][X_AXIS]);
+         x.unite (head_boxes[j][X_AXIS]);
        }
-
-      chord_outlines_[dir].at (0).height_ = x[dir];
+      
+      chord_outlines_[key].at (0).height_ = x[dir];
     }
-
+      
   for (vsize i = 0; i < boxes.size (); i++)
-    insert_extent_into_skyline (&chord_outlines_[dir]  ,
+    insert_extent_into_skyline (&chord_outlines_[key]  ,
                                boxes[i], Y_AXIS, -dir);
 
   if (stem
@@ -142,18 +151,36 @@ Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
       y.add_point (Stem::head_positions (stem)[-stemdir]
                   * staff_space * .5);
          
-      insert_extent_into_skyline (&chord_outlines_[dir], Box (x,y), Y_AXIS, -dir);
+      insert_extent_into_skyline (&chord_outlines_[key], Box (x,y), Y_AXIS, -dir);
 
-      stem_extents_[dir].unite (Box (x,y));
+      stem_extents_[key].unite (Box (x,y));
 
       if (dir == LEFT)
        {
          Box flag_box = Stem::get_translated_flag (stem).extent_box ();
          flag_box.translate( Offset (x[RIGHT], X_AXIS));
-         insert_extent_into_skyline (&chord_outlines_[dir], flag_box,
+         insert_extent_into_skyline (&chord_outlines_[key], flag_box,
                                      Y_AXIS, -dir);
        }
     }
+  else if (stem)
+    {
+      Grob *head = Stem::support_head (stem);
+
+      /*
+       In case of invisible stem, don't pass x-center of heads.
+       */
+      Real x_center = head->extent (x_refpoint_, X_AXIS).center ();
+      Interval x_ext;
+      x_ext[-dir] = x_center;
+      Interval y_ext;
+      for (vsize j = 0; j < head_boxes.size (); j++)
+       y_ext.unite (head_boxes[j][Y_AXIS]);
+         
+      insert_extent_into_skyline (&chord_outlines_[key],
+                                 Box (x_ext, y_ext),
+                                 Y_AXIS, -dir);
+    }
   
   Direction updowndir = DOWN;
   do
@@ -170,18 +197,44 @@ Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
        }
 
       if (!x.is_empty ())
-       insert_extent_into_skyline (&chord_outlines_[dir],
+       insert_extent_into_skyline (&chord_outlines_[key],
                                    Box (x,y),
                                    Y_AXIS, -dir);
     }
   while (flip (&updowndir) != DOWN);
   
-  head_extents_[dir].set_empty ();
+  head_extents_[key].set_empty ();
   for (vsize i = 0; i < head_boxes.size (); i++)
     {
-      head_extents_[dir].unite (head_boxes[i]);
+      head_extents_[key].unite (head_boxes[i]);
+    }
+}
+
+void
+Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
+                                          Direction dir)
+
+{
+  vector<int> ranks;
+  for (vsize i = 0; i < bounds.size (); i++)
+    ranks.push_back (bounds[i]->get_column ()->get_rank ());
+
+  vector_sort (ranks, less<int> ());
+  uniq (ranks);
+
+  for (vsize i = 0; i < ranks.size (); i++)
+    {
+      vector<Item*> col_items;
+      for (vsize j = 0; j < bounds.size (); j ++)
+       {
+         if (bounds[j]->get_column ()->get_rank () == ranks[i])
+           col_items.push_back (bounds[j]);
+       }
+
+      set_column_chord_outline (col_items, dir, ranks[i]);
     }
 }
+  
 
 
 void
@@ -235,20 +288,10 @@ Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
   for (vsize i = 0; i < ties.size (); i++)
     {
       Tie_specification spec;
+
+      spec.get_tie_manual_settings (ties[i]);
       
-      if (scm_is_number (ties[i]->get_property_data (ly_symbol2scm ("direction"))))
-       {
-         spec.manual_dir_ = to_dir (ties[i]->get_property ("direction"));
-         spec.has_manual_dir_ = true;
-       }
-         
-      spec.position_ = Tie::get_position (ties[i]);
-      if (scm_is_number (ties[i]->get_property ("staff-position")))
-       {
-         spec.manual_position_ = scm_to_double (ties[i]->get_property ("staff-position"));
-         spec.has_manual_position_ = true;
-         spec.position_ = int (my_round (spec.manual_position_));
-       }
+
       
       do
        {
@@ -262,18 +305,19 @@ Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
 }
 
 void
-Tie_formatting_problem::from_semi_ties (vector<Grob*> const &lv_ties, Direction head_dir)
+Tie_formatting_problem::from_semi_ties (vector<Grob*> const &semi_ties, Direction head_dir)
 {
-  if (lv_ties.empty ())
+  if (semi_ties.empty ())
     return;
   
-  details_.from_grob (lv_ties[0]);
+  details_.from_grob (semi_ties[0]);
   vector<Item*> heads;
-  
-  for (vsize i = 0; i < lv_ties.size (); i++)
+
+  int column_rank = -1;
+  for (vsize i = 0; i < semi_ties.size (); i++)
     {
       Tie_specification spec;
-      Item *head = unsmob_item (lv_ties[i]->get_object ("note-head"));
+      Item *head = unsmob_item (semi_ties[i]->get_object ("note-head"));
        
       if (!head)
        programming_error ("LV tie without head?!");
@@ -283,14 +327,18 @@ Tie_formatting_problem::from_semi_ties (vector<Grob*> const &lv_ties, Direction
          spec.position_ = int (Staff_symbol_referencer::get_position (head));
        }
 
+      spec.get_tie_manual_settings (semi_ties[i]);
+      
       spec.note_head_drul_[head_dir] = head;
+      column_rank = dynamic_cast<Item*> (head)->get_column ()->get_rank ();
+      spec.column_ranks_ = Drul_array<int> (column_rank, column_rank);
       heads.push_back (head);
       specifications_.push_back (spec);
     }
 
-  x_refpoint_ = lv_ties [0];
-  for (vsize i = 0; i < lv_ties.size (); i++)
-    x_refpoint_ = lv_ties[i]->common_refpoint (x_refpoint_, X_AXIS); 
+  x_refpoint_ = semi_ties [0];
+  for (vsize i = 0; i < semi_ties.size (); i++)
+    x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS); 
   for (vsize i = 0; i < heads.size (); i++)
     x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS); 
 
@@ -298,17 +346,20 @@ Tie_formatting_problem::from_semi_ties (vector<Grob*> const &lv_ties, Direction
 
   Real extremal = head_dir * infinity_f;   
 
-  for (vsize i = 0; i < chord_outlines_[head_dir].size (); i++)
+  Tuple2<int> head_key (column_rank, head_dir);
+  Tuple2<int> open_key (column_rank, -head_dir);
+  
+  for (vsize i = 0; i < chord_outlines_[head_key].size (); i++)
     {
       extremal = head_dir * min (head_dir * extremal,
-                                  head_dir * chord_outlines_[head_dir][i].height_);
+                                head_dir * chord_outlines_[head_key][i].height_);
     }
 
   Skyline_entry right_entry;
   right_entry.width_.set_full ();
   right_entry.height_ = extremal - head_dir * 1.5;
   
-  chord_outlines_[-head_dir].push_back (right_entry);
+  chord_outlines_[open_key].push_back (right_entry);
 }
 
 
@@ -360,22 +411,20 @@ Tie_formatting_problem::generate_configuration (int pos, Direction dir,
       conf->delta_y_ += dir * 0.25 * details_.staff_space_;
       y_tune = false;
     }
-               
-          
   
   if (y_tune
-      && max (fabs (head_extents_[LEFT][Y_AXIS][dir] - y),
-             fabs (head_extents_[RIGHT][Y_AXIS][dir] - y)) < 0.25
+      && max (fabs (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y),
+             fabs (get_head_extent (columns[RIGHT], RIGHT, Y_AXIS)[dir] - y)) < 0.25
       && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
     {
       conf->delta_y_ =
-       (head_extents_[LEFT][Y_AXIS][dir] - y)
+       (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y)
        + dir * details_.outer_tie_vertical_gap_;
     }
 
   if (y_tune)
     {
-      conf->attachment_x_ = get_attachment (y + conf->delta_y_);
+      conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
       Real h =  conf->height (details_);
       
       /*
@@ -418,7 +467,7 @@ Tie_formatting_problem::generate_configuration (int pos, Direction dir,
        }
     }
   
-  conf->attachment_x_ = get_attachment (y + conf->delta_y_);
+  conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
   if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
     {
       /*
@@ -428,29 +477,52 @@ Tie_formatting_problem::generate_configuration (int pos, Direction dir,
       Interval close_by = get_attachment (y
                                          + conf->delta_y_
                                          + (dir * details_.intra_space_threshold_ * 0.25
-                                            * details_.staff_space_));
+                                            * details_.staff_space_),
+                                         conf->column_ranks_);
       
       conf->attachment_x_.intersect (close_by);
     }
 
   conf->attachment_x_.widen ( - details_.x_gap_);
 
-  Direction d = LEFT;
-  do
+  if (conf->column_span_length ())
     {
-      Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
-      if (stem_extents_[d][X_AXIS].is_empty ()
-         || !stem_extents_[d][Y_AXIS].contains (y))
-       continue;
+      /*
+       avoid the stems that we attach to as well. We don't do this
+       for semities (span length = 0)
 
-      conf->attachment_x_[d] =
-       d* min (d * conf->attachment_x_[d],
-               d * (stem_extents_[d][X_AXIS][-d] - d * details_.stem_gap_));
-    }
-  while (flip (&d) != LEFT);
+       It would be better to check D against HEAD-DIRECTION if
+       applicable.
+      */
+      Direction d = LEFT;
+      do
+       {
+         Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
+         if (get_stem_extent (conf->column_ranks_[d], d, X_AXIS).is_empty ()
+             || !get_stem_extent (conf->column_ranks_[d], d, Y_AXIS).contains (y))
+           continue;
+
+         conf->attachment_x_[d] =
+           d * min (d * conf->attachment_x_[d],
+                    d * (get_stem_extent (conf->column_ranks_[d], d, X_AXIS)[-d] - d * details_.stem_gap_));
+       }
+      while (flip (&d) != LEFT);
+    }  
   return conf;
 }
 
+Interval
+Tie_formatting_problem::get_head_extent (int col, Direction d, Axis a) const
+{
+  return (*head_extents_.find (Tuple2<int> (col, int (d)))).second[a];
+}
+
+Interval
+Tie_formatting_problem::get_stem_extent (int col, Direction d, Axis a) const
+{
+  return (*stem_extents_.find (Tuple2<int> (col, int (d)))).second[a];
+}
+
 /**
    TIE_IDX and TIES_CONF are optional.
  */
@@ -612,7 +684,12 @@ Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const
        }
     }
 
-  Tie_configuration best = *confs[best_idx];
+  if (best_idx < 0)
+    programming_error ("No best tie configuration found.");
+
+  Tie_configuration best
+    = (best_idx >= 0) ?  *confs[best_idx] : *confs[0];
+  
   for (vsize i = 0; i < confs.size (); i++)
     delete confs[i];
 
@@ -632,6 +709,25 @@ Tie_specification::Tie_specification ()
     column_ranks_[LEFT] = 0;
 }
 
+
+void
+Tie_specification::get_tie_manual_settings (Grob *tie)
+{
+  if (scm_is_number (tie->get_property_data ("direction")))
+    {
+      manual_dir_ = to_dir (tie->get_property ("direction"));
+      has_manual_dir_ = true;
+    }
+  
+  position_ = Tie::get_position (tie);
+  if (scm_is_number (tie->get_property ("staff-position")))
+    {
+      manual_position_ = scm_to_double (tie->get_property ("staff-position"));
+      has_manual_position_ = true;
+      position_ = int (my_round (manual_position_));
+    }
+}
+
 int
 Tie_specification::column_span () const
 {
@@ -761,6 +857,8 @@ Tie_formatting_problem::generate_base_chord_configuration ()
        {
          conf.position_ = specifications_[i].position_;
        }
+
+      conf.column_ranks_ = specifications_[i].column_ranks_;
       ties_config.push_back (conf);
     }
 
@@ -806,7 +904,6 @@ Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
 Ties_configuration
 Tie_formatting_problem::generate_optimal_chord_configuration ()
 {
-  
   Ties_configuration base = generate_base_chord_configuration ();
   vector<Tie_configuration_variation> vars = generate_collision_variations (base);
   
@@ -823,9 +920,16 @@ Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration
 {
   if (tie_configs->empty ())
     return ;
-  
+
   if (!tie_configs->at (0).dir_)
-    tie_configs->at (0).dir_ = DOWN;
+    {
+      if (tie_configs->size () == 1)
+       tie_configs->at (0).dir_ = Direction (sign (tie_configs->at (0).position_));
+
+      if (!tie_configs->at (0).dir_)
+       tie_configs->at (0).dir_ = DOWN;
+    }
+  
   if (!tie_configs->back ().dir_)
     tie_configs->back ().dir_ = UP;