]> git.donarmstrong.com Git - lilypond.git/blob - lily/optimal-page-breaking.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / optimal-page-breaking.cc
1 /*
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
5
6   source file of the GNU LilyPond music typesetter
7
8   (c) 2006--2007 Joe Neeman <joeneeman@gmail.com>
9 */
10
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"
17 #include "prob.hh"
18 #include "system.hh"
19
20 static bool
21 is_break (Grob *g)
22 {
23   (void) g; /* shutup warning */
24   return false;
25 }
26
27 Optimal_page_breaking::Optimal_page_breaking (Paper_book *pb)
28   : Page_breaking (pb, is_break)
29 {
30 }
31
32 Optimal_page_breaking::~Optimal_page_breaking ()
33 {
34 }
35
36 SCM
37 Optimal_page_breaking::solve ()
38 {
39   vsize end = last_break_position ();
40   vsize max_sys_count = max_system_count (0, end);
41   vsize first_page_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
42
43   /* find out the ideal number of pages */
44   message (_ ("Finding the ideal number of pages..."));
45   set_to_ideal_line_configuration (0, end);
46   
47   Line_division ideal_line_division = current_configuration (0);
48   Page_spacing_result best = space_systems_on_best_pages (0, first_page_num);
49   vsize page_count = best.systems_per_page_.size ();
50   Line_division best_division = ideal_line_division;
51
52   vsize ideal_sys_count = best.system_count ();
53   vsize min_sys_count = ideal_sys_count - best.systems_per_page_.back ();
54   
55   if (page_count > 1)
56     min_sys_count -= best.systems_per_page_[page_count - 2];
57
58   if (page_count == 1)
59     message (_ ("Fitting music on 1 page..."));
60   else
61     message (_f ("Fitting music on %d or %d pages...", (int)page_count-1, (int)page_count));
62
63   /* try a smaller number of systems than the ideal number for line breaking */
64   Line_division bound = ideal_line_division;
65   for (vsize sys_count = ideal_sys_count; --sys_count >= min_sys_count;)
66     {
67       Page_spacing_result best_for_this_sys_count;
68       set_current_breakpoints (0, end, sys_count, Line_division (), bound);
69
70       for (vsize i = 0; i < current_configuration_count (); i++)
71         {
72           vsize min_p_count = min_page_count (i, first_page_num);
73           Page_spacing_result cur;
74
75           if (min_p_count > page_count)
76             continue;
77           else if (min_p_count == page_count)
78             cur = space_systems_on_n_pages (i, page_count, first_page_num);
79           else
80             cur = space_systems_on_n_or_one_more_pages (i, page_count-1, first_page_num);
81
82           if (cur.demerits_ < best_for_this_sys_count.demerits_ || isinf (best_for_this_sys_count.demerits_))
83             {
84               best_for_this_sys_count = cur;
85               bound = current_configuration (i);
86             }
87         }
88
89       if (best_for_this_sys_count.demerits_ < best.demerits_ || isinf (best.demerits_))
90         {
91           best = best_for_this_sys_count;
92           best_division = bound;
93         }
94
95       if (best_for_this_sys_count.systems_per_page_.size () < page_count)
96         {
97           /* if the pages are stretched on average, stop trying to reduce sys_count */
98           Real avg_f = 0;
99           for (vsize i = 0; i < best_for_this_sys_count.systems_per_page_.size (); i++)
100             avg_f += best_for_this_sys_count.systems_per_page_[i];
101           if (avg_f > 0)
102             break;
103         }
104
105       if (isinf (best_for_this_sys_count.demerits_))
106         break;
107     }
108
109   /* try a larger number of systems than the ideal line breaking number. This
110      is more or less C&P, but the loop bounds make it difficult to try something
111      like do {...} while (flip(&d) != UP). */
112   bound = ideal_line_division;
113   for (vsize sys_count = ideal_sys_count+1; sys_count <= max_sys_count; sys_count++)
114     {
115       Real best_demerits_for_this_sys_count = infinity_f;
116       set_current_breakpoints (0, end, sys_count, bound);
117
118       for (vsize i = 0; i < current_configuration_count (); i++)
119         {
120           vsize min_p_count = min_page_count (i, first_page_num);
121           Page_spacing_result cur;
122
123           if (min_p_count > page_count)
124             continue;
125           else
126             cur = space_systems_on_n_pages (i, page_count, first_page_num);
127
128           if (cur.demerits_ < best.demerits_ || isinf (best.demerits_))
129             {
130               best = cur;
131               best_division = current_configuration (i);
132             }
133
134           if (cur.demerits_ < best_demerits_for_this_sys_count || isinf (best_demerits_for_this_sys_count))
135             {
136               best_demerits_for_this_sys_count = cur.demerits_;
137               bound = current_configuration (i);
138             }
139         }
140       if (isinf (best_demerits_for_this_sys_count))
141         break;
142     }
143
144   message ("Drawing systems...");
145   break_into_pieces (0, end, best_division);
146   SCM lines = systems ();
147   return make_pages (best.systems_per_page_, lines);
148 }
149