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