]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/constrained-breaking.cc
Web-ja: update introduction
[lilypond.git] / lily / constrained-breaking.cc
index 0db98ccaafb90d2efb779f4f10002656400f584f..df08d5433ce6cefd38c944448a80c20f0350c8cf 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 2006--2010 Joe Neeman <joeneeman@gmail.com>
+  Copyright (C) 2006--2015 Joe Neeman <joeneeman@gmail.com>
 
   LilyPond is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -87,22 +87,22 @@ Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk)
   vsize start_col = starting_breakpoints_[start];
   Matrix<Constrained_break_node> &st = state_[start];
   vsize max_index = brk - start_col;
-  for (vsize j=max_index; j-- > sys;)
+  for (vsize j = max_index; j-- > sys;)
     {
       if (0 == sys && j > 0)
         continue; /* the first line cannot have its first break after the beginning */
 
       Line_details const &cur = lines_.at (brk, j + start_col);
       if (isinf (cur.force_))
-       break;
+        break;
 
       Real prev_f = 0;
       Real prev_dem = 0;
 
       if (sys > 0)
         {
-          prev_f = st.at (j, sys-1).details_.force_;
-          prev_dem = st.at (j, sys-1).demerits_;
+          prev_f = st.at (j, sys - 1).details_.force_;
+          prev_dem = st.at (j, sys - 1).demerits_;
         }
       if (isinf (prev_dem))
         continue;
@@ -120,16 +120,14 @@ Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk)
   return found_something;
 }
 
-
 Column_x_positions
 Constrained_breaking::space_line (vsize i, vsize j)
 {
   bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
   bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
-  Column_x_positions col;
 
-  vector<Grob*> line (all_.begin () + breaks_[i],
-                     all_.begin () + breaks_[j] + 1);
+  vector<Grob *> line (all_.begin () + breaks_[i],
+                       all_.begin () + breaks_[j] + 1);
   Interval line_dims = line_dimensions_int (pscore_->layout (), i);
   bool last = j == breaks_.size () - 1;
   bool ragged = ragged_right || (last && ragged_last);
@@ -176,7 +174,7 @@ Constrained_breaking::solve (vsize start, vsize end, vsize sys_count)
   vector<Column_x_positions> ret;
 
   /* find the first solution that satisfies constraints */
-  for (vsize sys = sys_count-1; sys != VPOS; sys--)
+  for (vsize sys = sys_count - 1; sys != VPOS; sys--)
     {
       for (vsize brk = end_brk; brk != VPOS; brk--)
         {
@@ -184,8 +182,8 @@ Constrained_breaking::solve (vsize start, vsize end, vsize sys_count)
             {
               if (brk != end_brk)
                 {
-                 brk = st.at (brk, sys).prev_;
-                 sys--;
+                  brk = st.at (brk, sys).prev_;
+                  sys--;
                   warning (_ ("cannot find line breaking that satisfies constraints"));
                   ret.push_back (space_line (brk, end_brk));
                 }
@@ -193,7 +191,7 @@ Constrained_breaking::solve (vsize start, vsize end, vsize sys_count)
               /* build up the good part of the solution */
               for (vsize cur_sys = sys; cur_sys != VPOS; cur_sys--)
                 {
-                 vsize prev_brk = st.at (brk, cur_sys).prev_;
+                  vsize prev_brk = st.at (brk, cur_sys).prev_;
                   assert (brk != VPOS);
                   ret.push_back (space_line (prev_brk + start_brk, brk + start_brk));
                   brk = prev_brk;
@@ -204,8 +202,10 @@ Constrained_breaking::solve (vsize start, vsize end, vsize sys_count)
         }
     }
   /* if we get to here, just put everything on one line */
-  warning (_ ("cannot find line breaking that satisfies constraints"));
-  ret.push_back (space_line (0, end_brk));
+  if (sys_count > 0) {
+    warning (_ ("cannot find line breaking that satisfies constraints"));
+    ret.push_back (space_line (0, end_brk));
+  }
   return ret;
 }
 
@@ -220,27 +220,27 @@ Constrained_breaking::best_solution (vsize start, vsize end)
   for (vsize i = min_systems; i <= max_systems; i++)
     {
       vsize brk = prepare_solution (start, end, i);
-      Real dem = state_[start].at (brk, i-1).demerits_;
+      Real dem = state_[start].at (brk, i - 1).demerits_;
 
       if (dem < best_demerits)
-       {
-         best_demerits = dem;
-         best_so_far = solve (start, end, i);
-       }
+        {
+          best_demerits = dem;
+          best_so_far = solve (start, end, i);
+        }
       else
-       {
-         vector<Column_x_positions> cur = solve (start, end, i);
-         bool too_many_lines = true;
-         
-         for (vsize j = 0; j < cur.size (); j++)
-           if (cur[j].force_ < 0)
-             {
-               too_many_lines = false;
-               break;
-             }
-         if (too_many_lines)
-           return best_so_far;
-       }
+        {
+          vector<Column_x_positions> cur = solve (start, end, i);
+          bool too_many_lines = true;
+
+          for (vsize j = 0; j < cur.size (); j++)
+            if (cur[j].force_ < 0)
+              {
+                too_many_lines = false;
+                break;
+              }
+          if (too_many_lines)
+            return best_so_far;
+        }
     }
   if (best_so_far.size ())
     return best_so_far;
@@ -256,32 +256,32 @@ Constrained_breaking::line_details (vsize start, vsize end, vsize sys_count)
 
   /* This loop structure is C&Ped from solve(). */
   /* find the first solution that satisfies constraints */
-  for (vsize sys = sys_count-1; sys != VPOS; sys--)
+  for (vsize sys = sys_count - 1; sys != VPOS; sys--)
     {
       for (vsize brk = end_brk; brk != VPOS; brk--)
         {
           if (!isinf (st.at (brk, sys).details_.force_))
             {
               if (brk != end_brk)
-               {
-                 /*
-                   During initialize(), we only fill out a
-                   Line_details for lines that are valid (ie. not too
-                   long), otherwise line breaking becomes O(n^3).
-                   In case sys_count is such that no valid solution
-                   is found, we need to fill in the Line_details.
-                 */
-                 Line_details details;
-                 brk = st.at (brk, sys).prev_;
-                 sys--;
-                 fill_line_details (&details, brk, end_brk);
-                 ret.push_back (details);
-               }
+                {
+                  /*
+                    During initialize(), we only fill out a
+                    Line_details for lines that are valid (ie. not too
+                    long), otherwise line breaking becomes O(n^3).
+                    In case sys_count is such that no valid solution
+                    is found, we need to fill in the Line_details.
+                  */
+                  Line_details details;
+                  brk = st.at (brk, sys).prev_;
+                  sys--;
+                  fill_line_details (&details, brk, end_brk);
+                  ret.push_back (details);
+                }
 
               /* build up the good part of the solution */
               for (vsize cur_sys = sys; cur_sys != VPOS; cur_sys--)
                 {
-                 vsize prev_brk = st.at (brk, cur_sys).prev_;
+                  vsize prev_brk = st.at (brk, cur_sys).prev_;
                   assert (brk != VPOS);
                   ret.push_back (st.at (brk, cur_sys).details_);
                   brk = prev_brk;
@@ -293,9 +293,11 @@ Constrained_breaking::line_details (vsize start, vsize end, vsize sys_count)
     }
 
   /* if we get to here, just put everything on one line */
-  Line_details details;
-  fill_line_details (&details, 0, end_brk);
-  ret.push_back (details);
+  if (sys_count > 0) {
+    Line_details details;
+    fill_line_details (&details, 0, end_brk);
+    ret.push_back (details);
+  }
   return ret;
 }
 
@@ -363,10 +365,10 @@ Constrained_breaking::Constrained_breaking (Paper_score *ps, vector<vsize> const
 static SCM
 min_permission (SCM perm1, SCM perm2)
 {
-  if (perm1 == ly_symbol2scm ("force"))
+  if (scm_is_eq (perm1, ly_symbol2scm ("force")))
     return perm2;
-  if (perm1 == ly_symbol2scm ("allow")
-     && perm2 != ly_symbol2scm ("force"))
+  if (scm_is_eq (perm1, ly_symbol2scm ("allow"))
+      && !scm_is_eq (perm2, ly_symbol2scm ("force")))
     return perm2;
   return SCM_EOL;
 }
@@ -380,60 +382,84 @@ Constrained_breaking::initialize ()
 
   ragged_right_ = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
   ragged_last_ = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
-  /* NOTE: currently, we aren't using the space_ field of a
-     Line_details for anything.  That's because the approximations
-     used for scoring a page configuration don't actually space things
-     properly (for speed reasong) using springs anchored at the staff
-     refpoints.  Rather, the "space" is placed between the extent
-     boxes.  To get a good result, therefore, the "space" value for
-     page breaking needs to be much smaller than the "space" value for
-     page layout.  Currently, we just make it zero always.
-  */
-  between_system_space_ = 0;
-  between_system_padding_ = 0;
-  before_title_padding_ = 0;
+  system_system_space_ = 0;
+  system_markup_space_ = 0;
+  system_system_padding_ = 0;
+  system_system_min_distance_ = 0;
+  score_system_padding_ = 0;
+  score_system_min_distance_ = 0;
+  score_markup_padding_ = 0;
+  score_markup_min_distance_ = 0;
 
   Output_def *l = pscore_->layout ();
 
-  SCM spacing_spec = l->c_variable ("between-system-spacing");
-  SCM title_spec = l->c_variable ("before-title-spacing");
-  SCM page_breaking_spacing_spec = l->c_variable ("page-breaking-between-system-spacing");
+  SCM spacing_spec = l->c_variable ("system-system-spacing");
+  SCM between_scores_spec = l->c_variable ("score-system-spacing");
+  SCM title_spec = l->c_variable ("score-markup-spacing");
+  SCM page_breaking_spacing_spec = l->c_variable ("page-breaking-system-system-spacing");
+
   Page_layout_problem::read_spacing_spec (spacing_spec,
-                                         &between_system_padding_,
-                                         ly_symbol2scm ("padding"));
+                                          &system_system_space_,
+                                          ly_symbol2scm ("basic-distance"));
   Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
-                                         &between_system_padding_,
-                                         ly_symbol2scm ("padding"));
+                                          &system_system_space_,
+                                          ly_symbol2scm ("basic-distance"));
   Page_layout_problem::read_spacing_spec (title_spec,
-                                         &before_title_padding_,
-                                         ly_symbol2scm ("padding"));
+                                          &system_markup_space_,
+                                          ly_symbol2scm ("basic-distance"));
+
+  Page_layout_problem::read_spacing_spec (spacing_spec,
+                                          &system_system_padding_,
+                                          ly_symbol2scm ("padding"));
+  Page_layout_problem::read_spacing_spec (between_scores_spec,
+                                          &score_system_padding_,
+                                          ly_symbol2scm ("padding"));
+  Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+                                          &system_system_padding_,
+                                          ly_symbol2scm ("padding"));
+  Page_layout_problem::read_spacing_spec (title_spec,
+                                          &score_markup_padding_,
+                                          ly_symbol2scm ("padding"));
+
+  Page_layout_problem::read_spacing_spec (between_scores_spec,
+                                          &score_system_min_distance_,
+                                          ly_symbol2scm ("minimum-distance"));
+  Page_layout_problem::read_spacing_spec (spacing_spec,
+                                          &system_system_min_distance_,
+                                          ly_symbol2scm ("minimum-distance"));
+  Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+                                          &system_system_min_distance_,
+                                          ly_symbol2scm ("minimum-distance"));
+  Page_layout_problem::read_spacing_spec (title_spec,
+                                          &score_markup_min_distance_,
+                                          ly_symbol2scm ("minimum-distance"));
 
   Interval first_line = line_dimensions_int (pscore_->layout (), 0);
   Interval other_lines = line_dimensions_int (pscore_->layout (), 1);
   /* do all the rod/spring problems */
-  breaks_ = pscore_->find_break_indices ();
+  breaks_ = pscore_->get_break_indices ();
   all_ = pscore_->root_system ()->used_columns ();
   lines_.resize (breaks_.size (), breaks_.size (), Line_details ());
   vector<Real> forces = get_line_forces (all_,
-                                        other_lines.length (),
-                                        other_lines.length () - first_line.length (),
-                                        ragged_right_);
+                                         other_lines.length (),
+                                         other_lines.length () - first_line.length (),
+                                         ragged_right_);
   for (vsize i = 0; i + 1 < breaks_.size (); i++)
     {
       for (vsize j = i + 1; j < breaks_.size (); j++)
-       {
-         bool last = j == breaks_.size () - 1;
-         bool ragged = ragged_right_ || (last && ragged_last_);
-         Line_details &line = lines_.at (j, i);
-
-         line.force_ = forces[i*breaks_.size () + j];
-         if (ragged && last && !isinf (line.force_))
-           line.force_ = (line.force_ < 0 && j > i + 1) ? infinity_f : 0;
-         if (isinf (line.force_))
-           break;
-
-         fill_line_details (&line, i, j);
-       }
+        {
+          bool last = j == breaks_.size () - 1;
+          bool ragged = ragged_right_ || (last && ragged_last_);
+          Line_details &line = lines_.at (j, i);
+
+          line.force_ = forces[i * breaks_.size () + j];
+          if (ragged && last && !isinf (line.force_))
+            line.force_ = (line.force_ < 0 && j > i + 1) ? infinity_f : 0;
+          if (isinf (line.force_))
+            break;
+
+          fill_line_details (&line, i, j);
+        }
     }
 
   /* work out all the starting indices */
@@ -441,7 +467,7 @@ Constrained_breaking::initialize ()
     {
       vsize j;
       for (j = 0; j + 1 < breaks_.size () && breaks_[j] < start_[i]; j++)
-       ;
+        ;
       starting_breakpoints_.push_back (j);
       start_[i] = breaks_[j];
     }
@@ -458,7 +484,9 @@ Constrained_breaking::fill_line_details (Line_details *const out, vsize start, v
   int start_rank = Paper_column::get_rank (all_[breaks_[start]]);
   int end_rank = Paper_column::get_rank (all_[breaks_[end]]);
   System *sys = pscore_->root_system ();
-  Interval extent = sys->pure_height (sys, start_rank, end_rank);
+  Interval begin_of_line_extent = sys->begin_of_line_pure_height (start_rank, end_rank);
+  Interval rest_of_line_extent = sys->rest_of_line_pure_height (start_rank, end_rank);
+  bool last = (end == breaks_.size () - 1);
 
   Grob *c = all_[breaks_[end]];
   out->last_column_ = c;
@@ -472,24 +500,33 @@ Constrained_breaking::fill_line_details (Line_details *const out, vsize start, v
   /* turn permission should always be stricter than page permission
      and page permission should always be stricter than line permission */
   out->page_permission_ = min_permission (out->break_permission_,
-                                         out->page_permission_);
+                                          out->page_permission_);
   out->turn_permission_ = min_permission (out->page_permission_,
-                                         out->turn_permission_);
-
-  // TODO: see the hack regarding begin_of_line and
-  // rest_of_line extents in align-interface.  Perhaps we
-  // should do the same thing here so that the effect extends
-  // between systems as well as within systems.  It isn't as
-  // crucial here, however, because the effect is largest when
-  // dealing with large systems.
-  out->extent_ = (extent.is_empty ()
-                 || isnan (extent[LEFT])
-                 || isnan (extent[RIGHT]))
-    ? Interval (0, 0) : extent;
-  out->padding_ = between_system_padding_;
-  out->title_padding_ = before_title_padding_;
-  out->space_ = between_system_space_;
-  out->inverse_hooke_ = extent.length () + between_system_space_;
+                                          out->turn_permission_);
+
+  begin_of_line_extent = (begin_of_line_extent.is_empty ()
+                          || isnan (begin_of_line_extent[LEFT])
+                          || isnan (begin_of_line_extent[RIGHT]))
+                         ? Interval (0, 0) : begin_of_line_extent;
+  rest_of_line_extent = (rest_of_line_extent.is_empty ()
+                         || isnan (rest_of_line_extent[LEFT])
+                         || isnan (rest_of_line_extent[RIGHT]))
+                        ? Interval (0, 0) : rest_of_line_extent;
+  out->shape_ = Line_shape (begin_of_line_extent, rest_of_line_extent);
+  out->padding_ = last ? score_system_padding_ : system_system_padding_;
+  out->title_padding_ = score_markup_padding_;
+  out->min_distance_ = last ? score_system_min_distance_ : system_system_min_distance_;
+  out->title_min_distance_ = score_markup_min_distance_;
+  out->space_ = system_system_space_;
+  out->title_space_ = system_markup_space_;
+  out->inverse_hooke_ = out->full_height () + system_system_space_;
+
+  out->footnote_heights_ = sys->get_footnote_heights_in_range (start_rank, end_rank);
+  out->in_note_heights_ = sys->get_in_note_heights_in_range (start_rank, end_rank);
+
+  out->refpoint_extent_ = sys->pure_refpoint_extent (start_rank, end_rank);
+  if (out->refpoint_extent_.is_empty ())
+    out->refpoint_extent_ = Interval (0, 0);
 }
 
 Real
@@ -503,18 +540,43 @@ Constrained_breaking::combine_demerits (Real force, Real prev_force)
 
 Line_details::Line_details (Prob *pb, Output_def *paper)
 {
-  SCM spec = paper->c_variable ("after-title-spacing");
-  SCM title_spec = paper->c_variable ("between-title-spacing");
+  SCM spec = paper->c_variable ("markup-system-spacing");
+  SCM title_spec = paper->c_variable ("markup-markup-spacing");
   padding_ = 0;
   title_padding_ = 0;
+  min_distance_ = 0;
+  title_min_distance_ = 0;
+  space_ = 0;
+  title_space_ = 0;
+  Page_layout_problem::read_spacing_spec (spec, &space_, ly_symbol2scm ("basic-distance"));
+  Page_layout_problem::read_spacing_spec (title_spec, &title_space_, ly_symbol2scm ("basic-distance"));
   Page_layout_problem::read_spacing_spec (spec, &padding_, ly_symbol2scm ("padding"));
   Page_layout_problem::read_spacing_spec (title_spec, &title_padding_, ly_symbol2scm ("padding"));
+  Page_layout_problem::read_spacing_spec (spec, &min_distance_, ly_symbol2scm ("minimum-distance"));
+  Page_layout_problem::read_spacing_spec (title_spec, &title_min_distance_, ly_symbol2scm ("minimum-distance"));
+
+  SCM footnotes = pb->get_property ("footnotes");
+
+  if (scm_is_pair (footnotes))
+    for (SCM s = footnotes; scm_is_pair (s); s = scm_cdr (s))
+      {
+        Stencil *sten = unsmob<Stencil> (scm_caddar (s));
+        if (!sten)
+          {
+            programming_error ("expecting stencil, got empty pointer");
+            continue;
+          }
+        footnote_heights_.push_back (sten->extent (Y_AXIS).length ());
+      }
 
   last_column_ = 0;
   force_ = 0;
-  extent_ = unsmob_stencil (pb->get_property ("stencil")) ->extent (Y_AXIS);
+  Stencil *st = unsmob<Stencil> (pb->get_property ("stencil"));
+  Interval stencil_extent = st->is_empty (Y_AXIS) ? Interval (0, 0)
+    : st->extent (Y_AXIS);
+  shape_ = Line_shape (stencil_extent, stencil_extent); // pretend it goes all the way across
+  tallness_ = 0;
   bottom_padding_ = 0;
-  space_ = 0.0;
   inverse_hooke_ = 1.0;
   break_permission_ = ly_symbol2scm ("allow");
   page_permission_ = pb->get_property ("page-break-permission");
@@ -525,8 +587,52 @@ Line_details::Line_details (Prob *pb, Output_def *paper)
   title_ = to_boolean (pb->get_property ("is-title"));
   compressed_lines_count_ = 1;
   compressed_nontitle_lines_count_ = title_ ? 0 : 1;
-  SCM last_scm  = pb->get_property ("last-markup-line");
-  last_markup_line_  = to_boolean (last_scm);
+  SCM last_scm = pb->get_property ("last-markup-line");
+  last_markup_line_ = to_boolean (last_scm);
   SCM first_scm = pb->get_property ("first-markup-line");
   first_markup_line_ = to_boolean (first_scm);
+  tight_spacing_ = to_boolean (pb->get_property ("tight-spacing"));
+  refpoint_extent_ = Interval (0, 0);
+}
+
+Real
+Line_details::full_height () const
+{
+  Interval ret;
+  ret.unite (shape_.begin_);
+  ret.unite (shape_.rest_);
+  return ret.length ();
+}
+
+Real
+Line_details::tallness () const
+{
+  return tallness_;
+}
+
+Real
+Line_details::spring_length (Line_details const &next_line) const
+{
+  // space_ measures the spring which goes from the bottom refpoint
+  // of this to the top refpoint of next_line. We want to return
+  // the stretchable space between the bottom of this's extent to
+  // the top of next_line's extent.
+  Real refpoint_dist = tallness_ + refpoint_extent_[DOWN] - next_line.refpoint_extent_[UP];
+  Real space = next_line.title_ ? title_space_ : space_;
+  return max (0.0, space - refpoint_dist);
+}
+
+Line_shape::Line_shape (Interval begin, Interval rest)
+{
+  begin_ = begin;
+  rest_ = rest;
+}
+
+Line_shape
+Line_shape::piggyback (Line_shape mount, Real padding) const
+{
+  Real elevation = max (begin_[UP] - mount.begin_[DOWN], rest_[UP] - mount.rest_[DOWN]);
+  Interval begin = Interval (begin_[DOWN], elevation + mount.begin_[UP] + padding);
+  Interval rest = Interval (rest_[DOWN], elevation + mount.rest_[UP] + padding);
+  return Line_shape (begin, rest);
 }