2 optimal-page-breaking.cc -- implement a page-breaker that
3 will break pages in such a way that both horizontal and
4 vertical spacing will be acceptable
6 source file of the GNU LilyPond music typesetter
8 (c) 2006--2009 Joe Neeman <joeneeman@gmail.com>
11 #include "international.hh"
12 #include "optimal-page-breaking.hh"
13 #include "output-def.hh"
14 #include "page-spacing.hh"
15 #include "paper-book.hh"
16 #include "paper-score.hh"
26 Optimal_page_breaking::Optimal_page_breaking (Paper_book *pb)
27 : Page_breaking (pb, is_break)
31 Optimal_page_breaking::~Optimal_page_breaking ()
36 Optimal_page_breaking::solve ()
38 vsize end = last_break_position ();
39 vsize max_sys_count = max_system_count (0, end);
40 vsize first_page_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
41 SCM forced_page_count = book_->paper_->c_variable ("page-count");
42 int systems_per_page = robust_scm2int (book_->paper_->c_variable ("systems-per-page"), 0);
44 set_to_ideal_line_configuration (0, end);
46 Page_spacing_result best;
47 vsize page_count = robust_scm2int (forced_page_count, 1);
48 Line_division ideal_line_division = current_configuration (0);
49 Line_division best_division = ideal_line_division;
50 vsize min_sys_count = 1;
51 vsize ideal_sys_count = system_count ();
53 if (!scm_is_integer (forced_page_count))
55 /* find out the ideal number of pages */
56 message (_ ("Finding the ideal number of pages..."));
58 if (systems_per_page > 0)
59 best = space_systems_with_fixed_number_per_page (0, first_page_num, systems_per_page);
61 best = space_systems_on_best_pages (0, first_page_num);
63 page_count = best.systems_per_page_.size ();
64 ideal_sys_count = best.system_count ();
65 min_sys_count = ideal_sys_count - best.systems_per_page_.back ();
67 if (page_count > 1 && best.systems_per_page_[page_count - 2] > 1)
68 min_sys_count -= best.systems_per_page_[page_count - 2];
70 min_sys_count = max (min_sys_count, (vsize)1);
74 /* TODO: the following line will spit out programming errors if the
75 ideal line spacing doesn't fit on PAGE_COUNT pages */
76 /* TODO: the interaction between systems_per_page and page_count needs to
78 best = space_systems_on_n_pages (0, page_count, first_page_num, systems_per_page);
79 min_sys_count = page_count;
83 message (_ ("Fitting music on 1 page..."));
84 else if (scm_is_integer (forced_page_count))
85 message (_f ("Fitting music on %d pages...", (int)page_count));
87 message (_f ("Fitting music on %d or %d pages...", (int)page_count-1, (int)page_count));
89 /* try a smaller number of systems than the ideal number for line breaking */
90 Line_division bound = ideal_line_division;
91 for (vsize sys_count = ideal_sys_count; --sys_count >= min_sys_count;)
93 Page_spacing_result best_for_this_sys_count;
94 set_current_breakpoints (0, end, sys_count, Line_division (), bound);
96 for (vsize i = 0; i < current_configuration_count (); i++)
98 vsize min_p_count = min_page_count (i, first_page_num);
99 Page_spacing_result cur;
101 if (min_p_count == page_count || scm_is_integer (forced_page_count))
102 cur = space_systems_on_n_pages (i, page_count, first_page_num, systems_per_page);
104 cur = space_systems_on_n_or_one_more_pages (i, page_count-1, first_page_num, systems_per_page);
106 if (cur.demerits_ < best_for_this_sys_count.demerits_ || isinf (best_for_this_sys_count.demerits_))
108 best_for_this_sys_count = cur;
109 bound = current_configuration (i);
113 if (best_for_this_sys_count.demerits_ < best.demerits_ || isinf (best.demerits_))
115 best = best_for_this_sys_count;
116 best_division = bound;
119 /* if the pages are stretched on average, stop trying to reduce sys_count */
120 if (best_for_this_sys_count.page_count () < page_count
121 && best_for_this_sys_count.average_force () > 0)
125 if (isinf (best_for_this_sys_count.demerits_))
129 /* try a larger number of systems than the ideal line breaking number. This
130 is more or less C&P, but the loop bounds make it difficult to try something
131 like do {...} while (flip(&d) != UP). */
132 bound = ideal_line_division;
133 for (vsize sys_count = ideal_sys_count+1; sys_count <= max_sys_count; sys_count++)
135 Real best_demerits_for_this_sys_count = infinity_f;
136 set_current_breakpoints (0, end, sys_count, bound);
138 for (vsize i = 0; i < current_configuration_count (); i++)
140 vsize min_p_count = min_page_count (i, first_page_num);
141 Page_spacing_result cur;
143 if (min_p_count > page_count)
146 cur = space_systems_on_n_pages (i, page_count, first_page_num, systems_per_page);
148 if (cur.demerits_ < best.demerits_ || isinf (best.demerits_))
151 best_division = current_configuration (i);
154 if (cur.demerits_ < best_demerits_for_this_sys_count || isinf (best_demerits_for_this_sys_count))
156 best_demerits_for_this_sys_count = cur.demerits_;
157 bound = current_configuration (i);
160 if (isinf (best_demerits_for_this_sys_count))
164 message (_ ("Drawing systems..."));
165 break_into_pieces (0, end, best_division);
166 SCM lines = systems ();
167 return make_pages (best.systems_per_page_, lines);