- vector<Line_details> lines = line_details (0, breaks_.size () - 1, line_count);
- Real page_h = page_height (1, false); // FIXME
- SCM force_sym = ly_symbol2scm ("blank-last-page-force");
- Real blank_force = robust_scm2double (book_->paper_->lookup_variable (force_sym), 0);
- bool ragged_all = book_->paper_->c_variable ("ragged-bottom");
- bool ragged_last = book_->paper_->c_variable ("ragged-last-bottom");
- Spacing_result ret = space_systems_on_best_pages (lines,
- page_h,
- blank_force,
- ragged_all,
- ragged_last);
-
- /* add in the line penalties */
- Real line_force = 0;
- Real line_penalty = 0;
- Real page_weighting = robust_scm2double (book_->paper_->c_variable ("page-spacing-weight"), 1);
-
- for (vsize i = 0; i < lines.size (); i++)
+ vsize end = last_break_position ();
+ vsize max_sys_count = max_system_count (0, end);
+ vsize first_page_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
+
+ /* find out the ideal number of pages */
+ message (_ ("Finding the ideal number of pages..."));
+ set_to_ideal_line_configuration (0, end);
+
+ Page_spacing_result best = space_systems_on_best_pages (0, first_page_num);
+ vsize page_count = best.systems_per_page_.size ();
+ Line_division ideal_line_division = current_configuration (0);
+ Line_division best_division = ideal_line_division;
+
+ vsize ideal_sys_count = best.system_count ();
+ vsize min_sys_count = ideal_sys_count - best.systems_per_page_.back ();
+
+ if (page_count > 1 && best.systems_per_page_[page_count - 2] > 1)
+ min_sys_count -= best.systems_per_page_[page_count - 2];
+
+ if (page_count == 1)
+ message (_ ("Fitting music on 1 page..."));
+ else
+ message (_f ("Fitting music on %d or %d pages...", (int)page_count-1, (int)page_count));
+
+ /* try a smaller number of systems than the ideal number for line breaking */
+ Line_division bound = ideal_line_division;
+ for (vsize sys_count = ideal_sys_count; --sys_count >= min_sys_count;)