]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/page-turn-page-breaking.cc
Run `make grand-replace'.
[lilypond.git] / lily / page-turn-page-breaking.cc
index 81cfab9cac70f8263e78036fe9d86d33d2d0d1b2..e239cc2f2854f3a5b416e554541f50e9813658d5 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2006 Joe Neeman <joeneeman@gmail.com>
+  (c) 2006--2008 Joe Neeman <joeneeman@gmail.com>
 */
 
 #include "page-turn-page-breaking.hh"
@@ -33,47 +33,19 @@ Page_turn_page_breaking::~Page_turn_page_breaking ()
 {
 }
 
-Real
-Page_turn_page_breaking::calc_demerits (const Break_node &me)
-{
-  Real prev_f = 0;
-  Real prev_dem = 0;
-  Real page_weighting = robust_scm2double (book_->paper_->c_variable ("page-spacing-weight"), 10);
-  if (me.prev_ != VPOS)
-    {
-      prev_f = state_[me.prev_].force_;
-      prev_dem = state_[me.prev_].demerits_;
-    }
-
-  Real dem = me.force_ * me.force_ * page_weighting
-           + me.line_force_ * me.line_force_
-           + fabs (me.force_ - prev_f);
-  if (isinf (me.line_force_) || isinf (me.force_) || me.page_count_ == 0)
-    dem = infinity_f;
-
-  return dem + prev_dem + me.penalty_ + me.line_penalty_;
-}
-
 Page_turn_page_breaking::Break_node
 Page_turn_page_breaking::put_systems_on_pages (vsize start,
                                               vsize end,
-                                              vector<Line_details> const &lines,
-                                              Line_division const &div,
-                                              int page_number)
+                                              vsize configuration,
+                                              vsize page_number)
 {
-  bool last = end == breaks_.size () - 1;
-  bool ragged_all = to_boolean (book_->paper_->c_variable ("ragged-bottom"));
-  bool ragged_last = last && to_boolean (book_->paper_->c_variable ("ragged-last-bottom"));
-  Real page_h = page_height (1, false); // FIXME
-  SCM force_sym = last ? ly_symbol2scm ("blank-last-page-force") : ly_symbol2scm ("blank-page-force");
-  Real blank_force = robust_scm2double (book_->paper_->lookup_variable (force_sym), 0);
-  int min_p_count = min_page_count (lines, page_h, ragged_all, ragged_last);
+  vsize min_p_count = min_page_count (configuration, page_number);
   bool auto_first = to_boolean (book_->paper_->c_variable ("auto-first-page-number"));
 
   /* If [START, END] does not contain an intermediate
      breakpoint, we may need to consider solutions that result in a bad turn.
      In this case, we won't abort if the min_page_count is too big */
-  if (start < end - 1 && min_p_count > 2)
+  if (start < end - 1 && min_p_count + (auto_first ? 0 : (page_number % 2)) > 2)
     return Break_node ();
 
   /* if PAGE-NUMBER is odd, we are starting on a right hand page. That is, we
@@ -89,18 +61,18 @@ Page_turn_page_breaking::put_systems_on_pages (vsize start,
      min_p_count has that evenness. (For example, if PAGE-NUMBER is even and
      min_p_count is even, we don't even consider the blank page option). */
 
-  Spacing_result result;
+  Page_spacing_result result;
   if (start == 0 && auto_first)
     {
       if (min_p_count % 2)
-       result = space_systems_on_n_or_one_more_pages (lines, min_p_count, page_h, 0, ragged_all, ragged_last);
+       result = space_systems_on_n_or_one_more_pages (configuration, min_p_count, page_number);
       else
-       result = space_systems_on_n_pages (lines, min_p_count, page_h, ragged_all, ragged_last);
+       result = space_systems_on_n_pages (configuration, min_p_count, page_number);
     }
   else if (page_number % 2 == min_p_count % 2)
-    result = space_systems_on_n_pages (lines, min_p_count, page_h, ragged_all, ragged_last);
+    result = space_systems_on_n_pages (configuration, min_p_count, page_number);
   else
-    result = space_systems_on_n_or_one_more_pages (lines, min_p_count, page_h, blank_force, ragged_all, ragged_last);
+    result = space_systems_on_n_or_one_more_pages (configuration, min_p_count, page_number);
 
   Break_node ret;
   ret.prev_ = start - 1;
@@ -109,36 +81,29 @@ Page_turn_page_breaking::put_systems_on_pages (vsize start,
   ret.first_page_number_ = page_number;
   if (auto_first && start == 0)
     ret.first_page_number_ += 1 - (ret.page_count_ % 2);
-  ret.force_ = 0;
-  for (vsize i = 0; i < result.force_.size (); i++)
-    ret.force_ += fabs (result.force_[i]);
 
-  ret.penalty_ = result.penalty_;
-  ret.div_ = div;
+  ret.div_ = current_configuration (configuration);
   ret.system_count_ = result.systems_per_page_;
 
-  ret.too_many_lines_ = true;
-  ret.line_force_ = 0;
-  ret.line_penalty_ = 0;
-  for (vsize i = 0; i < lines.size (); i++)
-    {
-      ret.line_force_ += fabs (lines[i].force_);
-      ret.line_penalty_ += lines[i].break_penalty_;
-      if (lines[i].force_ < 0)
-       ret.too_many_lines_ = false;
-    }
+  ret.too_many_lines_ = all_lines_stretched (configuration);
+  ret.demerits_ = result.demerits_;
+  if (start > 0)
+    ret.demerits_ += state_[start-1].demerits_;
+
   return ret;
 }
 
-/* "final page" meaning the number of the final right-hand page,
-   which always has an odd page number */
+/* The number of pages taken up by a Break_node, including
+   the blank page if there is one */
 vsize
-Page_turn_page_breaking::final_page_num (Break_node const &b)
+Page_turn_page_breaking::total_page_count (Break_node const &b)
 {
   vsize end = b.first_page_number_ + b.page_count_;
-  return end + 1 - (end % 2);
+  return end - 1 + (end % 2) - b.first_page_number_;
 }
 
+extern bool debug_page_breaking_scoring;
+
 void
 Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
 {
@@ -177,25 +142,25 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
 
       bool ok_page = true;
 
+      if (debug_page_breaking_scoring)
+       message (_f ("page-turn-page-breaking: breaking from %d to %d", (int) start, (int) end));
+
       /* heuristic: we've just added a breakpoint, we'll need at least as
          many systems as before */
       min_sys_count = max (min_sys_count, prev_best_system_count);
       for (vsize sys_count = min_sys_count; sys_count <= max_sys_count && ok_page; sys_count++)
         {
-         vector<Line_division> div = line_divisions (start, end, sys_count, min_division, max_division);
+         set_current_breakpoints (start, end, sys_count, min_division, max_division);
           bool found = false;
 
-          for (vsize d = 0; d < div.size (); d++)
+          for (vsize i = 0; i < current_configuration_count (); i++)
             {
-             vector<Line_details> line = line_details (start, end, div[d]);
-
-              cur = put_systems_on_pages (start, end, line, div[d], p_num);
-             cur.demerits_ = calc_demerits (cur);
+              cur = put_systems_on_pages (start, end, i, p_num);
 
               if (isinf (cur.demerits_)
-                 || (cur.page_count_ > 2
+                 || (cur.page_count_  + (p_num % 2) > 2
                      && (!isinf (this_start_best.demerits_))
-                     && final_page_num (cur) > final_page_num (this_start_best)))
+                     && total_page_count (cur) > total_page_count (this_start_best)))
                 {
                   ok_page = false;
                   break;
@@ -203,13 +168,16 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
 
               if (cur.demerits_ < this_start_best.demerits_)
                 {
+                 if (debug_page_breaking_scoring)
+                   print_break_node (cur);
+
                   found = true;
                   this_start_best = cur;
                   prev_best_system_count = sys_count;
 
                  /* heuristic: if we increase the number of systems, we can bound the
                     division from below by our current best division */
-                 min_division = div[d];
+                 min_division = current_configuration (i);
                 }
             }
           if (!found && this_start_best.too_many_lines_)
@@ -224,7 +192,7 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
       if (start == 0 && end == 1
          && this_start_best.first_page_number_ == 1
          && this_start_best.page_count_ > 1)
-       warning (_ ("couldn't fit the first page turn onto a single page. "
+       warning (_ ("cannot fit the first page turn onto a single page.  "
                    "Consider setting first-page-number to an even number."));
 
       if (this_start_best.demerits_ < best.demerits_)
@@ -238,8 +206,8 @@ Page_turn_page_breaking::solve ()
 {
   state_.clear ();
   message (_f ("Calculating page and line breaks (%d possible page breaks)...",
-               (int)breaks_.size () - 1) + " ");
-  for (vsize i = 0; i < breaks_.size () - 1; i++)
+               (int) last_break_position ()));
+  for (vsize i = 0; i < last_break_position (); i++)
     {
       calc_subproblem (i);
       progress_indication (string ("[") + to_string (i + 1) + "]");
@@ -285,7 +253,7 @@ Page_turn_page_breaking::make_pages (vector<Break_node> const &soln, SCM systems
       for (vsize j = 0; j < soln[i].page_count_; j++)
        lines_per_page.push_back (soln[i].system_count_[j]);
 
-      if (i < soln.size () - 1 && (soln[i].first_page_number_ + soln[i].page_count_) % 2)
+      if (i + 1 < soln.size () && (soln[i].first_page_number_ + soln[i].page_count_) % 2)
        /* add a blank page */
        lines_per_page.push_back (0);
     }
@@ -296,3 +264,17 @@ Page_turn_page_breaking::make_pages (vector<Break_node> const &soln, SCM systems
                               scm_from_int (soln[0].first_page_number_));
   return Page_breaking::make_pages (lines_per_page, systems);
 }
+
+void
+Page_turn_page_breaking::print_break_node (Break_node const &node)
+{
+  int system_count = 0;
+  for (vsize i = 0; i < node.system_count_.size (); i++)
+    system_count += node.system_count_[i];
+
+  message (_f ("break starting at page %d", (int) node.first_page_number_));
+  message (_f ("\tdemerits: %f", node.demerits_));
+  message (_f ("\tsystem count: %d", system_count));
+  message (_f ("\tpage count: %d", (int) node.page_count_));
+  message (_f ("\tprevious break: %d", (int) node.prev_));
+}