]> git.donarmstrong.com Git - lilypond.git/blob - lily/page-turn-page-breaking.cc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / page-turn-page-breaking.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2009 Joe Neeman <joeneeman@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "page-turn-page-breaking.hh"
21
22 #include "international.hh"
23 #include "item.hh"
24 #include "output-def.hh"
25 #include "page-spacing.hh"
26 #include "paper-book.hh"
27 #include "paper-score.hh"
28 #include "paper-system.hh"
29 #include "system.hh"
30 #include "warn.hh"
31
32 static bool
33 is_break (Grob *g)
34 {
35   return scm_is_symbol (g->get_property ("page-turn-permission"));
36 }
37
38 Page_turn_page_breaking::Page_turn_page_breaking (Paper_book *pb)
39   : Page_breaking (pb, is_break)
40 {
41 }
42
43 Page_turn_page_breaking::~Page_turn_page_breaking ()
44 {
45 }
46
47 Page_turn_page_breaking::Break_node
48 Page_turn_page_breaking::put_systems_on_pages (vsize start,
49                                                vsize end,
50                                                vsize configuration,
51                                                vsize page_number)
52 {
53   vsize min_p_count = min_page_count (configuration, page_number);
54   bool auto_first = to_boolean (book_->paper_->c_variable ("auto-first-page-number"));
55
56   /* If [START, END] does not contain an intermediate
57      breakpoint, we may need to consider solutions that result in a bad turn.
58      In this case, we won't abort if the min_page_count is too big */
59   if (start < end - 1 && min_p_count + (auto_first ? 0 : (page_number % 2)) > 2)
60     return Break_node ();
61
62   /* if PAGE-NUMBER is odd, we are starting on a right hand page. That is, we
63      have the options
64      PAGE-NUMBER odd:
65        - even number of pages + a blank page
66        - odd number of pages
67      PAGE-NUMBER even:
68        - odd number of pages + a blank page
69        - even number of pages
70
71      No matter which case PAGE-NUMBER falls into, we take the second choice if
72      min_p_count has that evenness. (For example, if PAGE-NUMBER is even and
73      min_p_count is even, we don't even consider the blank page option). */
74
75   Page_spacing_result result;
76   if (start == 0 && auto_first)
77     {
78       if (min_p_count % 2)
79         result = space_systems_on_n_or_one_more_pages (configuration, min_p_count, page_number);
80       else
81         result = space_systems_on_n_pages (configuration, min_p_count, page_number);
82     }
83   else if (page_number % 2 == min_p_count % 2)
84     result = space_systems_on_n_pages (configuration, min_p_count, page_number);
85   else
86     result = space_systems_on_n_or_one_more_pages (configuration, min_p_count, page_number);
87
88   Break_node ret;
89   ret.prev_ = start - 1;
90   ret.break_pos_ = end;
91   ret.page_count_ = result.force_.size ();
92   ret.first_page_number_ = page_number;
93   if (auto_first && start == 0)
94     ret.first_page_number_ += 1 - (ret.page_count_ % 2);
95
96   ret.div_ = current_configuration (configuration);
97   ret.system_count_ = result.systems_per_page_;
98
99   ret.too_many_lines_ = all_lines_stretched (configuration);
100   ret.demerits_ = result.demerits_;
101   if (start > 0)
102     ret.demerits_ += state_[start-1].demerits_;
103
104   return ret;
105 }
106
107 /* The number of pages taken up by a Break_node, including
108    the blank page if there is one */
109 vsize
110 Page_turn_page_breaking::total_page_count (Break_node const &b)
111 {
112   vsize end = b.first_page_number_ + b.page_count_;
113   return end - 1 + (end % 2) - b.first_page_number_;
114 }
115
116 extern bool debug_page_breaking_scoring;
117
118 void
119 Page_turn_page_breaking::calc_subproblem (vsize ending_breakpoint)
120 {
121   vsize end = ending_breakpoint + 1;
122   Break_node best;
123   Break_node cur;
124   Break_node this_start_best;
125   vsize prev_best_system_count = 0;
126
127   for (vsize start = end; start--;)
128     {
129       if (start < end-1
130           && breakpoint_property (start+1, "page-turn-permission") == ly_symbol2scm ("force"))
131         break;
132
133       if (start > 0 && best.demerits_ < state_[start-1].demerits_)
134         continue;
135
136       int p_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
137       if (start > 0)
138         {
139           /* except possibly for the first page, enforce the fact that first_page_number_
140              should always be even (left hand page).
141              TODO: are there different conventions in right-to-left languages?
142           */
143           p_num = state_[start-1].first_page_number_ + state_[start-1].page_count_;
144           p_num += p_num % 2;
145         }
146
147       Line_division min_division;
148       Line_division max_division;
149
150       vsize min_sys_count = min_system_count (start, end);
151       vsize max_sys_count = max_system_count (start, end);
152       this_start_best.demerits_ = infinity_f;
153
154       bool ok_page = true;
155
156       if (debug_page_breaking_scoring)
157         message (_f ("page-turn-page-breaking: breaking from %d to %d", (int) start, (int) end));
158
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++)
163         {
164           set_current_breakpoints (start, end, sys_count, min_division, max_division);
165           bool found = false;
166
167           for (vsize i = 0; i < current_configuration_count (); i++)
168             {
169               cur = put_systems_on_pages (start, end, i, p_num);
170
171               if (isinf (cur.demerits_)
172                   || (cur.page_count_  + (p_num % 2) > 2
173                       && (!isinf (this_start_best.demerits_))
174                       && total_page_count (cur) > total_page_count (this_start_best)))
175                 {
176                   ok_page = false;
177                   break;
178                 }
179
180               if (cur.demerits_ < this_start_best.demerits_)
181                 {
182                   if (debug_page_breaking_scoring)
183                     print_break_node (cur);
184
185                   found = true;
186                   this_start_best = cur;
187                   prev_best_system_count = sys_count;
188
189                   /* heuristic: if we increase the number of systems, we can bound the
190                      division from below by our current best division */
191                   min_division = current_configuration (i);
192                 }
193             }
194           if (!found && this_start_best.too_many_lines_)
195             break;
196         }
197       if (isinf (this_start_best.demerits_))
198         {
199           assert (!isinf (best.demerits_) && start < end - 1);
200           break;
201         }
202
203       if (start == 0 && end == 1
204           && this_start_best.first_page_number_ == 1
205           && this_start_best.page_count_ > 1)
206         warning (_ ("cannot fit the first page turn onto a single page.  "
207                     "Consider setting first-page-number to an even number."));
208
209       if (this_start_best.demerits_ < best.demerits_)
210         best = this_start_best;
211     }
212   state_.push_back (best);
213 }
214
215 SCM
216 Page_turn_page_breaking::solve ()
217 {
218   state_.clear ();
219   message (_f ("Calculating page and line breaks (%d possible page breaks)...",
220                (int) last_break_position ()));
221   for (vsize i = 0; i < last_break_position (); i++)
222     {
223       calc_subproblem (i);
224       progress_indication (string ("[") + to_string (i + 1) + "]");
225     }
226   progress_indication ("\n");
227
228   vector<Break_node> breaking;
229   int i = state_.size () - 1;
230   while (i >= 0)
231     {
232       breaking.push_back (state_[i]);
233       i = state_[i].prev_;
234     }
235   reverse (breaking);
236
237   message (_ ("Drawing systems..."));
238   SCM systems = make_lines (&breaking);
239   return make_pages (breaking, systems);
240 }
241
242 /* do the line breaking in all the scores and return a big list of systems */
243 SCM
244 Page_turn_page_breaking::make_lines (vector<Break_node> *psoln)
245 {
246   vector<Break_node> &soln = *psoln;
247   for (vsize n = 0; n < soln.size (); n++)
248     {
249       vsize start = n > 0 ? soln[n-1].break_pos_ : 0;
250       vsize end = soln[n].break_pos_;
251
252       break_into_pieces (start, end, soln[n].div_);
253     }
254
255   return systems ();
256 }
257
258 SCM
259 Page_turn_page_breaking::make_pages (vector<Break_node> const &soln, SCM systems)
260 {
261   vector<vsize> lines_per_page;
262   for (vsize i = 0; i < soln.size (); i++)
263     {
264       for (vsize j = 0; j < soln[i].page_count_; j++)
265         lines_per_page.push_back (soln[i].system_count_[j]);
266
267       if (i + 1 < soln.size () && (soln[i].first_page_number_ + soln[i].page_count_) % 2)
268         /* add a blank page */
269         lines_per_page.push_back (0);
270     }
271
272   /* this should only actually modify first-page-number if
273      auto-first-page-number was true. */
274   book_->paper_->set_variable (ly_symbol2scm ("first-page-number"),
275                                scm_from_int (soln[0].first_page_number_));
276   return Page_breaking::make_pages (lines_per_page, systems);
277 }
278
279 void
280 Page_turn_page_breaking::print_break_node (Break_node const &node)
281 {
282   int system_count = 0;
283   for (vsize i = 0; i < node.system_count_.size (); i++)
284     system_count += node.system_count_[i];
285
286   message (_f ("break starting at page %d", (int) node.first_page_number_));
287   message (_f ("\tdemerits: %f", node.demerits_));
288   message (_f ("\tsystem count: %d", system_count));
289   message (_f ("\tpage count: %d", (int) node.page_count_));
290   message (_f ("\tprevious break: %d", (int) node.prev_));
291 }