]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/page-spacing.cc
When working out page breaks, take into account the fact that titles
[lilypond.git] / lily / page-spacing.cc
index 028a67a9fde4d73e383f1f5fc09f32d5b7d53146..384ce440222a95cc1f5901dd0cdd54113e779eda 100644 (file)
 #include "page-breaking.hh"
 #include "warn.hh"
 
-/* In order to prevent possible division by zero, we require every line
-   to have a spring of non-zero length. */
-Real
-line_space (const Line_details &line)
-{
-  return max (0.1, line.space_);
-}
-
 void
 Page_spacing::calc_force ()
 {
-  if (rod_height_ + last_line_.bottom_padding_ >= page_height_ || !inverse_spring_k_)
+  /* If the first system is a title, we add back in the page-top-space. */
+  Real height = first_line_.title_ ? page_height_ + page_top_space_ : page_height_;
+
+  if (rod_height_ + last_line_.bottom_padding_ >= height)
     force_ = infinity_f;
   else
-    force_ = (page_height_ - rod_height_ - last_line_.bottom_padding_ - spring_len_) / inverse_spring_k_;
+    force_ = (height - rod_height_ - last_line_.bottom_padding_ - spring_len_)
+      / max (0.1, inverse_spring_k_);
+}
+
+void
+Page_spacing::resize (Real new_height)
+{
+  page_height_ = new_height;
+  calc_force ();
 }
 
 void
 Page_spacing::append_system (const Line_details &line)
 {
+  if (!rod_height_)
+    first_line_ = line;
+
   rod_height_ += last_line_.padding_;
 
   rod_height_ += line.extent_.length ();
-  spring_len_ += line_space (line);
-  inverse_spring_k_ += max (0.1, line.inverse_hooke_);
+  spring_len_ += line.space_;
+  inverse_spring_k_ += line.inverse_hooke_;
 
   last_line_ = line;
 
@@ -53,8 +59,10 @@ Page_spacing::prepend_system (const Line_details &line)
     last_line_ = line;
 
   rod_height_ += line.extent_.length ();
-  spring_len_ += line_space (line);
-  inverse_spring_k_ += max (0.1, line.inverse_hooke_);
+  spring_len_ += line.space_;
+  inverse_spring_k_ += line.inverse_hooke_;
+
+  first_line_ = line;
 
   calc_force ();
 }
@@ -74,16 +82,16 @@ Page_spacer::Page_spacer (vector<Line_details> const &lines, vsize first_page_nu
   breaker_ = breaker;
   max_page_count_ = 0;
   ragged_ = breaker->ragged ();
-  ragged_last_ = breaker->last () && breaker->ragged_last ();
+  ragged_last_ = breaker->is_last () && breaker->ragged_last ();
 }
 
-Spacing_result
+Page_spacing_result
 Page_spacer::solve (vsize page_count)
 {
   if (page_count > max_page_count_)
     resize (page_count);
 
-  Spacing_result ret;
+  Page_spacing_result ret;
   ret.force_.resize (page_count);
   ret.systems_per_page_.resize (page_count);
 
@@ -99,7 +107,7 @@ Page_spacer::solve (vsize page_count)
         All the systems that don't fit get tacked onto the last page.
       */
       vsize i;
-      for (i = system; isinf (state_.at (i, page_count-1).demerits_) && i--; )
+      for (i = system; isinf (state_.at (i, page_count-1).demerits_) && i; i--)
        ;
 
       if (i)
@@ -108,7 +116,7 @@ Page_spacer::solve (vsize page_count)
          system = i;
        }
       else
-       return Spacing_result (); /* couldn't salvage it -- probably going to crash */
+       return Page_spacing_result (); /* couldn't salvage it -- probably going to crash */
     }
 
   ret.penalty_ = state_.at (system, page_count-1).penalty_
@@ -153,7 +161,8 @@ bool
 Page_spacer::calc_subproblem (vsize page, vsize line)
 {
   bool last = line == lines_.size () - 1;
-  Page_spacing space (breaker_->page_height (page + first_page_num_, last));
+  Page_spacing space (breaker_->page_height (page + first_page_num_, last),
+                     breaker_->page_top_space ());
   Page_spacing_node &cur = state_.at (line, page);
   bool ragged = ragged_ || (ragged_last_ && last);