2 page-turn-page-breaking.cc -- implement Page_turn_page_breaking
4 source file of the GNU LilyPond music typesetter
6 (c) 2006 Joe Neeman <joeneeman@gmail.com>
9 #include "page-turn-page-breaking.hh"
11 #include "international.hh"
13 #include "output-def.hh"
14 #include "page-spacing.hh"
15 #include "paper-book.hh"
16 #include "paper-score.hh"
17 #include "paper-system.hh"
24 return scm_is_symbol (g->get_property ("page-turn-permission"));
27 Page_turn_page_breaking::Page_turn_page_breaking (Paper_book *pb)
28 : Page_breaking (pb, is_break)
32 Page_turn_page_breaking::~Page_turn_page_breaking ()
36 Page_turn_page_breaking::Break_node
37 Page_turn_page_breaking::put_systems_on_pages (vsize start,
39 vector<Line_details> const &lines,
40 Line_division const &div,
43 bool last = end == breaks_.size () - 1;
44 bool ragged_all = to_boolean (book_->paper_->c_variable ("ragged-bottom"));
45 bool ragged_last = last && to_boolean (book_->paper_->c_variable ("ragged-last-bottom"));
46 Real page_h = page_height (1, false); // FIXME
47 SCM force_sym = last ? ly_symbol2scm ("blank-last-page-force") : ly_symbol2scm ("blank-page-force");
48 Real blank_force = robust_scm2double (book_->paper_->lookup_variable (force_sym), 0);
49 Real page_weighting = robust_scm2double (book_->paper_->c_variable ("page-spacing-weight"), 10);
50 int min_p_count = min_page_count (lines, page_h, ragged_all, ragged_last);
51 bool auto_first = to_boolean (book_->paper_->c_variable ("auto-first-page-number"));
53 /* If [START, END] does not contain an intermediate
54 breakpoint, we may need to consider solutions that result in a bad turn.
55 In this case, we won't abort if the min_page_count is too big */
56 if (start < end - 1 && min_p_count > 2)
59 /* if PAGE-NUMBER is odd, we are starting on a right hand page. That is, we
62 - even number of pages + a blank page
65 - odd number of pages + a blank page
66 - even number of pages
68 No matter which case PAGE-NUMBER falls into, we take the second choice if
69 min_p_count has that evenness. (For example, if PAGE-NUMBER is even and
70 min_p_count is even, we don't even consider the blank page option). */
72 Spacing_result result;
73 if (start == 0 && auto_first)
76 result = space_systems_on_n_or_one_more_pages (lines, min_p_count, page_h, 0, ragged_all, ragged_last);
78 result = space_systems_on_n_pages (lines, min_p_count, page_h, ragged_all, ragged_last);
80 else if (page_number % 2 == min_p_count % 2)
81 result = space_systems_on_n_pages (lines, min_p_count, page_h, ragged_all, ragged_last);
83 result = space_systems_on_n_or_one_more_pages (lines, min_p_count, page_h, blank_force, ragged_all, ragged_last);
86 ret.prev_ = start - 1;
88 ret.page_count_ = result.force_.size ();
89 ret.first_page_number_ = page_number;
90 if (auto_first && start == 0)
91 ret.first_page_number_ += 1 - (ret.page_count_ % 2);
94 ret.system_count_ = result.systems_per_page_;
96 ret.too_many_lines_ = true;
97 ret.demerits_ = result.penalty_;
99 ret.demerits_ += state_[start-1].demerits_;
100 for (vsize i = 0; i < lines.size (); i++)
102 ret.demerits_ += lines[i].force_ * lines[i].force_;
103 ret.demerits_ += lines[i].break_penalty_;
104 if (lines[i].force_ < 0)
105 ret.too_many_lines_ = false;
107 for (vsize i = 0; i < result.force_.size (); i++)
108 ret.demerits_ += result.force_[i] * result.force_[i] * page_weighting;
112 /* "final page" meaning the number of the final right-hand page,
113 which always has an odd page number */
115 Page_turn_page_breaking::final_page_num (Break_node const &b)
117 vsize end = b.first_page_number_ + b.page_count_;
118 return end + 1 - (end % 2);
122 Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
124 vsize end = ending_breakpoint + 1;
127 Break_node this_start_best;
128 vsize prev_best_system_count = 0;
130 for (vsize start = end; start--;)
133 && breakpoint_property (start+1, "page-turn-permission") == ly_symbol2scm ("force"))
136 if (start > 0 && best.demerits_ < state_[start-1].demerits_)
139 int p_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
142 /* except possibly for the first page, enforce the fact that first_page_number_
143 should always be even (left hand page).
144 TODO: are there different conventions in right-to-left languages?
146 p_num = state_[start-1].first_page_number_ + state_[start-1].page_count_;
150 Line_division min_division;
151 Line_division max_division;
153 vsize min_sys_count = min_system_count (start, end);
154 vsize max_sys_count = max_system_count (start, end);
155 this_start_best.demerits_ = infinity_f;
159 /* heuristic: we've just added a breakpoint, we'll need at least as
160 many systems as before */
161 min_sys_count = max (min_sys_count, prev_best_system_count);
162 for (vsize sys_count = min_sys_count; sys_count <= max_sys_count && ok_page; sys_count++)
164 vector<Line_division> div = line_divisions (start, end, sys_count, min_division, max_division);
167 for (vsize d = 0; d < div.size (); d++)
169 vector<Line_details> line = line_details (start, end, div[d]);
171 cur = put_systems_on_pages (start, end, line, div[d], p_num);
173 if (isinf (cur.demerits_)
174 || (cur.page_count_ > 2
175 && (!isinf (this_start_best.demerits_))
176 && final_page_num (cur) > final_page_num (this_start_best)))
182 if (cur.demerits_ < this_start_best.demerits_)
185 this_start_best = cur;
186 prev_best_system_count = sys_count;
188 /* heuristic: if we increase the number of systems, we can bound the
189 division from below by our current best division */
190 min_division = div[d];
193 if (!found && this_start_best.too_many_lines_)
196 if (isinf (this_start_best.demerits_))
198 assert (!isinf (best.demerits_) && start < end - 1);
202 if (start == 0 && end == 1
203 && this_start_best.first_page_number_ == 1
204 && this_start_best.page_count_ > 1)
205 warning (_ ("couldn't fit the first page turn onto a single page. "
206 "Consider setting first-page-number to an even number."));
208 if (this_start_best.demerits_ < best.demerits_)
209 best = this_start_best;
211 state_.push_back (best);
215 Page_turn_page_breaking::solve ()
218 message (_f ("Calculating page and line breaks (%d possible page breaks)...",
219 (int)breaks_.size () - 1) + " ");
220 for (vsize i = 0; i < breaks_.size () - 1; i++)
223 progress_indication (string ("[") + to_string (i + 1) + "]");
225 progress_indication ("\n");
227 vector<Break_node> breaking;
228 int i = state_.size () - 1;
231 breaking.push_back (state_[i]);
236 message (_ ("Drawing systems..."));
237 SCM systems = make_lines (&breaking);
238 return make_pages (breaking, systems);
241 /* do the line breaking in all the scores and return a big list of systems */
243 Page_turn_page_breaking::make_lines (vector<Break_node> *psoln)
245 vector<Break_node> &soln = *psoln;
246 for (vsize n = 0; n < soln.size (); n++)
248 vsize start = n > 0 ? soln[n-1].break_pos_ : 0;
249 vsize end = soln[n].break_pos_;
251 break_into_pieces (start, end, soln[n].div_);
258 Page_turn_page_breaking::make_pages (vector<Break_node> const &soln, SCM systems)
260 vector<vsize> lines_per_page;
261 for (vsize i = 0; i < soln.size (); i++)
263 for (vsize j = 0; j < soln[i].page_count_; j++)
264 lines_per_page.push_back (soln[i].system_count_[j]);
266 if (i < soln.size () - 1 && (soln[i].first_page_number_ + soln[i].page_count_) % 2)
267 /* add a blank page */
268 lines_per_page.push_back (0);
271 /* this should only actually modify first-page-number if
272 auto-first-page-number was true. */
273 book_->paper_->set_variable (ly_symbol2scm ("first-page-number"),
274 scm_from_int (soln[0].first_page_number_));
275 return Page_breaking::make_pages (lines_per_page, systems);