]> git.donarmstrong.com Git - lilypond.git/blob - lily/optimal-page-breaking.cc
Run `make grand-replace'.
[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--2008 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   SCM forced_page_count = book_->paper_->c_variable ("page-count");
43
44   set_to_ideal_line_configuration (0, end);
45
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 ();
52   
53   if (!scm_is_integer (forced_page_count))
54     {
55       /* find out the ideal number of pages */
56       message (_ ("Finding the ideal number of pages..."));
57   
58       best = space_systems_on_best_pages (0, first_page_num);
59       page_count = best.systems_per_page_.size ();
60
61       ideal_sys_count = best.system_count ();
62       min_sys_count = ideal_sys_count - best.systems_per_page_.back ();
63   
64       if (page_count > 1 && best.systems_per_page_[page_count - 2] > 1)
65         min_sys_count -= best.systems_per_page_[page_count - 2];
66
67       min_sys_count = max (min_sys_count, (vsize)1);
68     }
69   else
70     {
71       /* todo: the following line will spit out programming errors if the
72          ideal line spacing doesn't fit on PAGE_COUNT pages */
73       best = space_systems_on_n_pages (0, page_count, first_page_num);
74       min_sys_count = page_count;
75     }
76
77   if (page_count == 1)
78     message (_ ("Fitting music on 1 page..."));
79   else if (scm_is_integer (forced_page_count))
80     message (_f ("Fitting music on %d pages...", (int)page_count));
81   else
82     message (_f ("Fitting music on %d or %d pages...", (int)page_count-1, (int)page_count));
83
84   /* try a smaller number of systems than the ideal number for line breaking */
85   Line_division bound = ideal_line_division;
86   for (vsize sys_count = ideal_sys_count; --sys_count >= min_sys_count;)
87     {
88       Page_spacing_result best_for_this_sys_count;
89       set_current_breakpoints (0, end, sys_count, Line_division (), bound);
90
91       for (vsize i = 0; i < current_configuration_count (); i++)
92         {
93           vsize min_p_count = min_page_count (i, first_page_num);
94           Page_spacing_result cur;
95
96           if (min_p_count == page_count || scm_is_integer (forced_page_count))
97             cur = space_systems_on_n_pages (i, page_count, first_page_num);
98           else
99             cur = space_systems_on_n_or_one_more_pages (i, page_count-1, first_page_num);
100
101           if (cur.demerits_ < best_for_this_sys_count.demerits_ || isinf (best_for_this_sys_count.demerits_))
102             {
103               best_for_this_sys_count = cur;
104               bound = current_configuration (i);
105             }
106         }
107
108       if (best_for_this_sys_count.demerits_ < best.demerits_ || isinf (best.demerits_))
109         {
110           best = best_for_this_sys_count;
111           best_division = bound;
112         }
113
114       /* if the pages are stretched on average, stop trying to reduce sys_count */
115       if (best_for_this_sys_count.page_count () < page_count
116           && best_for_this_sys_count.average_force () > 0)
117         break;
118         
119
120       if (isinf (best_for_this_sys_count.demerits_))
121         break;
122     }
123
124   /* try a larger number of systems than the ideal line breaking number. This
125      is more or less C&P, but the loop bounds make it difficult to try something
126      like do {...} while (flip(&d) != UP). */
127   bound = ideal_line_division;
128   for (vsize sys_count = ideal_sys_count+1; sys_count <= max_sys_count; sys_count++)
129     {
130       Real best_demerits_for_this_sys_count = infinity_f;
131       set_current_breakpoints (0, end, sys_count, bound);
132
133       for (vsize i = 0; i < current_configuration_count (); i++)
134         {
135           vsize min_p_count = min_page_count (i, first_page_num);
136           Page_spacing_result cur;
137
138           if (min_p_count > page_count)
139             continue;
140           else
141             cur = space_systems_on_n_pages (i, page_count, first_page_num);
142
143           if (cur.demerits_ < best.demerits_ || isinf (best.demerits_))
144             {
145               best = cur;
146               best_division = current_configuration (i);
147             }
148
149           if (cur.demerits_ < best_demerits_for_this_sys_count || isinf (best_demerits_for_this_sys_count))
150             {
151               best_demerits_for_this_sys_count = cur.demerits_;
152               bound = current_configuration (i);
153             }
154         }
155       if (isinf (best_demerits_for_this_sys_count))
156         break;
157     }
158
159   message (_ ("Drawing systems..."));
160   break_into_pieces (0, end, best_division);
161   SCM lines = systems ();
162   return make_pages (best.systems_per_page_, lines);
163 }
164