]> git.donarmstrong.com Git - lilypond.git/commitdiff
* lily/page-turn-page-breaking.cc (calc_demerits): now that we allow
authorJoe Neeman <joeneeman@gmail.com>
Sat, 30 Sep 2006 10:30:46 +0000 (10:30 +0000)
committerJoe Neeman <joeneeman@gmail.com>
Sat, 30 Sep 2006 10:30:46 +0000 (10:30 +0000)
put_systems_on_pages to return an empty result, we need to assign
demerits properly in that case.
(put_systems_on_pages): Make the number of pages depend on the
evenness of page_number. Add auto-first-page-number.
(calc_subproblem): ensure that the page-number is even for the
left-hand page. Warn if the first page-turn doesn't fit onto the
first (right-hand) page.

* lily/page-spacing.cc (solve): bug: demerits_ would always be inf
(min_page_count): make this publicly accessible
(min_page_count): fix a bug when there are forced page breaks that
was introduced when I made this loop run backwards
(space_systems_on_n_pages, space_systems_on_n_or_one_more_pages):
replace space_systems_on_min_pages with these two. The logic in
space_systems_on_min_pages was getting too convoluted and is better
contained in page-turn-page-breaking.

ChangeLog
input/regression/page-turn-page-breaking-badturns.ly
input/regression/page-turn-page-breaking.ly
lily/include/page-spacing.hh
lily/include/page-turn-page-breaking.hh
lily/page-spacing.cc
lily/page-turn-page-breaking.cc

index 839b0a5f633c46463d27603b0fcc03afbdf16f8d..a235bd2d785401e80d57927eadb969e3110806d6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2006-09-30  Joe Neeman  <joeneeman@gmail.com>
 
+       * lily/page-turn-page-breaking.cc (calc_demerits): now that we allow
+       put_systems_on_pages to return an empty result, we need to assign
+       demerits properly in that case.
+       (put_systems_on_pages): Make the number of pages depend on the
+       evenness of page_number. Add auto-first-page-number.
+       (calc_subproblem): ensure that the page-number is even for the
+       left-hand page. Warn if the first page-turn doesn't fit onto the
+       first (right-hand) page.
+
+       * lily/page-spacing.cc (solve): bug: demerits_ would always be inf
+       (min_page_count): make this publicly accessible
+       (min_page_count): fix a bug when there are forced page breaks that
+       was introduced when I made this loop run backwards
+       (space_systems_on_n_pages, space_systems_on_n_or_one_more_pages):
+       replace space_systems_on_min_pages with these two. The logic in
+       space_systems_on_min_pages was getting too convoluted and is better
+       contained in page-turn-page-breaking.
+
        * lily/page-turn-engraver.cc (breakable_column): remove an always-true
        conditional
        (breakable_column): typo
index cae98946a70c352f615057c3a5b51bf11c622cf5..ff872ae2123ab487c305d4fe1793b309a82c45bd 100644 (file)
@@ -10,6 +10,7 @@ should appear on 3 pages.
 \paper {
   #(define page-breaking ly:page-turn-breaking)
   paper-height = #70
+  print-page-number = ##t
 }
 
 \relative c' {
index 88aea25c01023ce726a8dc84f8af21ca29e0ddaf..fee9a83a93107c917171c09fe958dfaf6cbd7b1c 100644 (file)
@@ -10,6 +10,9 @@ in which case the turn will go after the special barline.
 \paper {
   #(define page-breaking ly:page-turn-breaking)
   paper-height = #70
+  auto-first-page-number = ##t
+  print-page-number = ##t
+  print-first-page-number = ##t
 }
 
 \layout {
index b91540a03f7937622ea5eadafbd443f403939744..f64df2c8a2ae7a4751ace94d67d7f2ea9ca8e35c 100644 (file)
@@ -65,12 +65,24 @@ private:
   bool calc_subproblem (vsize page, vsize lines);
 };
 
+vsize
+min_page_count (vector<Line_details> const &lines,
+               Real page_height, bool ragged, bool ragged_last);
+
+Spacing_result
+space_systems_on_n_pages (vector<Line_details> const&,
+                         vsize n,
+                         Real page_height,
+                         bool ragged,
+                         bool ragged_last);
+
 Spacing_result
-space_systems_on_min_pages (vector<Line_details> const&,
-                           Real page_height,
-                           Real odd_pages_penalty,
-                           bool ragged,
-                           bool ragged_last);
+space_systems_on_n_or_one_more_pages (vector<Line_details> const&,
+                                     vsize n,
+                                     Real page_height,
+                                     Real odd_pages_penalty,
+                                     bool ragged,
+                                     bool ragged_last);
 Spacing_result
 space_systems_on_best_pages (vector<Line_details> const&,
                             Real page_height,
index dc52bcb03a2a93313a5cfeb3c373b387effd65a4..1706437908be0a93a7adda746911dbea2a80f578 100644 (file)
@@ -55,12 +55,14 @@ protected:
       line_penalty_ = line_force_ = 0;
       demerits_ = infinity_f;
       first_page_number_ = 0;
+      page_count_ = 0;
       too_many_lines_ = false;
     }
   };
 
   vector<Break_node> state_;
 
+  vsize final_page_num (Break_node const &b);
   Break_node put_systems_on_pages (vsize start,
                                   vsize end,
                                   vector<Line_details> const &lines,
index e7816009cd2a6c0450310d3a3337af5bd1fc3d14..dd468e7ea9f337ecaff30d366f8aa176a8377b42 100644 (file)
@@ -259,6 +259,7 @@ Page_spacer::solve (vsize page_count)
   ret.penalty_ = state_.at (system, page_count-1).penalty_
     + lines_.back ().page_penalty_ + lines_.back ().turn_penalty_;
 
+  ret.demerits_ = 0;
   for (vsize p = page_count; p--;)
     {
       assert (system != VPOS);
@@ -340,11 +341,13 @@ Page_spacer::calc_subproblem (vsize page, vsize line)
   return !isinf (cur.demerits_);
 }
 
-static vsize
-min_page_count (vector<Line_details> const &lines, Real page_height, bool ragged, bool ragged_last)
+vsize
+min_page_count (vector<Line_details> const &uncompressed_lines,
+               Real page_height, bool ragged, bool ragged_last)
 {
   vsize ret = 1;
   Real cur_rod_height = 0;
+  vector<Line_details> lines = compress_lines (uncompressed_lines);
 
   assert (lines.size ());
   for (vsize i = lines.size (); i--;)
@@ -356,7 +359,7 @@ min_page_count (vector<Line_details> const &lines, Real page_height, bool ragged
        + ((cur_rod_height > 0) ? lines[i].padding_: 0);
 
       if ((next_height > page_height && cur_rod_height > 0)
-         || (i > 0 && lines[i-1].page_permission_ == ly_symbol2scm ("force")))
+         || (i < lines.size () - 1 && lines[i].page_permission_ == ly_symbol2scm ("force")))
        {
          ret++;
          cur_rod_height = ext_len + (rag ? lines[i].space_ : 0);
@@ -369,57 +372,48 @@ min_page_count (vector<Line_details> const &lines, Real page_height, bool ragged
 }
 
 Spacing_result
-space_systems_on_min_pages (vector<Line_details> const &lines,
-                           Real page_height,
-                           Real odd_pages_penalty,
-                           bool ragged,
-                           bool ragged_last)
+space_systems_on_n_pages (vector<Line_details> const &lines,
+                         vsize n,
+                         Real page_height,
+                         bool ragged,
+                         bool ragged_last)
 {
   vector<Line_details> compressed_lines = compress_lines (lines);
-  vsize min_p_count = min_page_count (compressed_lines, page_height, ragged, ragged_last);
   Spacing_result ret;
+  assert (n >= min_page_count (lines, page_height, ragged, ragged_last));
 
-  if (min_p_count == 1)
-    {
-      Spacing_result candidate1 = space_systems_on_1_page (compressed_lines, page_height, ragged || ragged_last);
-      candidate1.force_.back () += odd_pages_penalty;
-      candidate1.demerits_ += odd_pages_penalty;
-      if (compressed_lines.size () == 1)
-       ret = candidate1;
-      else
-       {
-         Spacing_result candidate2 = space_systems_on_2_pages (compressed_lines, page_height, ragged, ragged_last);
-         ret = (candidate1.demerits_ < candidate2.demerits_) ?
-           candidate1 : candidate2;
-       }
-    }
-  else if (min_p_count == 2)
+  if (n > compressed_lines.size ())
+    return Spacing_result ();
+  if (n == 1)
+    ret = space_systems_on_1_page (compressed_lines, page_height, ragged || ragged_last);
+  else if (n == 2)
     ret = space_systems_on_2_pages (compressed_lines, page_height, ragged, ragged_last);
-  else
-    {
-      Page_spacer ps (compressed_lines, page_height, ragged, ragged_last);
-      Spacing_result candidate1 = ps.solve (min_p_count);
-      if (min_p_count % 2 == 0)
-       ret = candidate1;
-      else
-       {
-         candidate1.force_.back () += odd_pages_penalty;
-         candidate1.demerits_ += odd_pages_penalty;
 
-         if (min_p_count == compressed_lines.size ())
-           ret = candidate1;
-         else
-           {
-             Spacing_result candidate2 = ps.solve (min_p_count + 1);
-             ret = (candidate1.demerits_ < candidate2.demerits_) ?
-               candidate1 : candidate2;
-           }
-       }
-    }
+  Page_spacer ps (compressed_lines, page_height, ragged, ragged_last);
+  ret = ps.solve (n);
+
   ret.systems_per_page_ = uncompress_solution (ret.systems_per_page_, compressed_lines);
   return ret;
 }
 
+Spacing_result
+space_systems_on_n_or_one_more_pages (vector<Line_details> const &lines,
+                                     vsize n,
+                                     Real page_height,
+                                     Real odd_pages_penalty,
+                                     bool ragged,
+                                     bool ragged_last)
+{
+  Spacing_result n_res = space_systems_on_n_pages (lines, n, page_height, ragged, ragged_last);
+  Spacing_result m_res = space_systems_on_n_pages (lines, n+1, page_height, ragged, ragged_last);
+  n_res.demerits_ += odd_pages_penalty;
+  n_res.force_.back () += odd_pages_penalty;
+
+  if (n_res.demerits_ < m_res.demerits_)
+    return n_res;
+  return m_res;
+}
+
 Spacing_result
 space_systems_on_best_pages (vector<Line_details> const &lines,
                             Real page_height,
index 7db8831905237469fe51ec9d259addc3896affa2..e60ccfe03396e246026675b90fc23c3b19e20a16 100644 (file)
@@ -48,7 +48,7 @@ Page_turn_page_breaking::calc_demerits (const Break_node &me)
   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_))
+  if (isinf (me.line_force_) || isinf (me.force_) || me.page_count_ == 0)
     dem = infinity_f;
 
   return dem + prev_dem + me.penalty_ + me.line_penalty_;
@@ -67,13 +67,48 @@ Page_turn_page_breaking::put_systems_on_pages (vsize start,
   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);
-  Spacing_result result = space_systems_on_min_pages (lines, page_h, blank_force, ragged_all, ragged_last);
+  int min_p_count = min_page_count (lines, page_h, ragged_all, ragged_last);
+  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)
+    return Break_node ();
+
+  /* if PAGE-NUMBER is odd, we are starting on a right hand page. That is, we
+     have the options
+     PAGE-NUMBER odd:
+       - even number of pages + a blank page
+       - odd number of pages
+     PAGE-NUMBER even:
+       - odd number of pages + a blank page
+       - even number of pages
+
+     No matter which case PAGE-NUMBER falls into, we take the second choice if
+     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;
+  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);
+      else
+       result = space_systems_on_n_pages (lines, min_p_count, page_h, ragged_all, ragged_last);
+    }
+  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);
+  else
+    result = space_systems_on_n_or_one_more_pages (lines, min_p_count, page_h, blank_force, ragged_all, ragged_last);
 
   Break_node ret;
   ret.prev_ = start - 1;
   ret.break_pos_ = end;
-  ret.first_page_number_ = page_number;
   ret.page_count_ = result.force_.size ();
+  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]);
@@ -95,6 +130,15 @@ Page_turn_page_breaking::put_systems_on_pages (vsize start,
   return ret;
 }
 
+/* "final page" meaning the number of the final right-hand page,
+   which always has an odd page number */
+vsize
+Page_turn_page_breaking::final_page_num (Break_node const &b)
+{
+  vsize end = b.first_page_number_ + b.page_count_;
+  return end + 1 - (end % 2);
+}
+
 void
 Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
 {
@@ -113,14 +157,15 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
       if (start > 0 && best.demerits_ < state_[start-1].demerits_)
         continue;
 
-      int p_num = 1;
+      int p_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
       if (start > 0)
         {
-          /* if the last node has an odd number of pages and is not the first page,
-             add a blank page */
-          int p_count = state_[start-1].page_count_;
-          int f_p_num = state_[start-1].first_page_number_;
-          p_num = f_p_num + p_count + ((f_p_num > 1) ? p_count % 2 : 0);
+         /* except possibly for the first page, enforce the fact that first_page_number_
+            should always be even (left hand page).
+            TODO: are there different conventions in right-to-left languages?
+         */
+         p_num = state_[start-1].first_page_number_ + state_[start-1].page_count_;
+         p_num += p_num % 2;
         }
 
       Line_division min_division;
@@ -145,16 +190,16 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
              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);
 
-              if (cur.page_count_ > 2 &&
-                 (start < end - 1 || (!isinf (this_start_best.demerits_)
-                                      && cur.page_count_ + cur.page_count_ % 2
-                                      > this_start_best.page_count_ + this_start_best.page_count_ % 2)))
+              if (isinf (cur.demerits_)
+                 || (cur.page_count_ > 2
+                     && (!isinf (this_start_best.demerits_))
+                     && final_page_num (cur) > final_page_num (this_start_best)))
                 {
                   ok_page = false;
                   break;
                 }
-             cur.demerits_ = calc_demerits (cur);
 
               if (cur.demerits_ < this_start_best.demerits_)
                 {
@@ -176,6 +221,12 @@ Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
           break;
         }
 
+      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. "
+                   "Consider setting first-page-number to an even number."));
+
       if (this_start_best.demerits_ < best.demerits_)
        best = this_start_best;
     }
@@ -234,9 +285,14 @@ 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 > 0 && i < soln.size () - 1 && soln[i].page_count_ % 2)
+      if (i < soln.size () - 1 && (soln[i].first_page_number_ + soln[i].page_count_) % 2)
        /* add a blank page */
        lines_per_page.push_back (0);
     }
+
+  /* this should only actually modify first-page-number if
+     auto-first-page-number was true. */
+  book_->paper_->set_variable (ly_symbol2scm ("first-page-number"),
+                              scm_from_int (soln[0].first_page_number_));
   return Page_breaking::make_pages (lines_per_page, systems);
 }