]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/page-breaking.hh
Grand fixcc.py run on all .hh .cc files.
[lilypond.git] / lily / include / page-breaking.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2011 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 #ifndef PAGE_BREAKING_HH
21 #define PAGE_BREAKING_HH
22
23 #include "constrained-breaking.hh"
24 #include "page-spacing.hh"
25
26 /* Either a paper-score, markup or header.
27  */
28 struct System_spec
29 {
30   System_spec (Paper_score *ps)
31   {
32     pscore_ = ps;
33     prob_ = NULL;
34   }
35
36   System_spec (Prob *pb)
37   {
38     prob_ = pb;
39     pscore_ = NULL;
40   }
41
42   System_spec ()
43   {
44     pscore_ = NULL;
45     prob_ = NULL;
46   }
47
48   Paper_score *pscore_;
49   Prob *prob_;
50 };
51
52 struct Break_position
53 {
54   /*
55     index into system_specs_, if this is VPOS, the Break_position represents the
56     start of the book.
57    */
58   vsize system_spec_index_;
59
60   /* if system_spec_index_ indexes a score, then we start at the score_brk_'th
61      possible page-break in the score */
62   vsize score_break_;
63
64   /* if system_spec_index_ indexes a score, this points to the broken column */
65   Grob *col_;
66   bool score_ender_;
67
68   /* if non-zero, this is the (fixed, uncompressed) number of lines between
69      this Break_position and the previous. */
70   int forced_line_count_;
71
72   Break_position (vsize s = VPOS, vsize brk = VPOS, Grob *g = NULL, bool end = false)
73   {
74     system_spec_index_ = s;
75     score_break_ = brk;
76     col_ = g;
77     score_ender_ = end;
78     forced_line_count_ = 0;
79   }
80
81   /*
82     lexicographic in (system_spec_index_, score_break_)
83    */
84   bool operator < (const Break_position &other)
85   {
86     return (system_spec_index_ == VPOS && other.system_spec_index_ != VPOS)
87            || (system_spec_index_ < other.system_spec_index_)
88            || (system_spec_index_ == other.system_spec_index_ && score_break_ < other.score_break_);
89   }
90
91   bool operator <= (const Break_position &other)
92   {
93     return (system_spec_index_ == VPOS)
94            || (system_spec_index_ < other.system_spec_index_ && other.system_spec_index_ != VPOS)
95            || (system_spec_index_ == other.system_spec_index_ && score_break_ <= other.score_break_);
96   }
97 };
98
99 class Page_breaking
100 {
101 public:
102   typedef bool (*Break_predicate) (Grob *);
103   typedef bool (*Prob_break_predicate) (Prob *);
104   typedef vector<vsize> Line_division;
105
106   /*
107     TODO: naming.
108
109     determine the page breaking, and break scores into lines
110     appropriately.
111    */
112   virtual SCM solve () = 0;
113
114   Page_breaking (Paper_book *pb, Break_predicate, Prob_break_predicate);
115   virtual ~Page_breaking ();
116
117   bool ragged () const;
118   bool ragged_last () const;
119   bool is_last () const;
120   bool ends_score () const;
121   int systems_per_page () const;
122   int max_systems_per_page () const;
123   int min_systems_per_page () const;
124   Real page_height (int page_number, bool last) const;
125   Real paper_height () const;
126   vsize system_count () const;
127   Real footnote_separator_stencil_height () const;
128   Real footnote_padding () const;
129   Real footnote_number_raise () const;
130   Real footnote_footer_padding () const;
131   Real line_count_penalty (int line_count) const;
132   int line_count_status (int line_count) const;
133   bool too_many_lines (int line_count) const;
134   bool too_few_lines (int line_count) const;
135   Real min_whitespace_at_top_of_page (Line_details const &) const;
136   Real min_whitespace_at_bottom_of_page (Line_details const &) const;
137   int orphan_penalty () const;
138
139 protected:
140   Paper_book *book_;
141
142   vsize next_system (Break_position const &break_pos) const;
143
144   SCM make_pages (vector<vsize> lines_per_page, SCM lines);
145
146   vsize min_system_count (vsize start, vsize end);
147   vsize max_system_count (vsize start, vsize end);
148
149   void break_into_pieces (vsize start, vsize end, Line_division const &div);
150   SCM systems ();
151   SCM footnotes ();
152
153   void set_current_breakpoints (vsize start,
154                                 vsize end,
155                                 vsize system_count,
156                                 Line_division lower_bound = Line_division (),
157                                 Line_division upper_bound = Line_division ());
158   void set_to_ideal_line_configuration (vsize start, vsize end);
159
160   vsize current_configuration_count () const;
161   Line_division current_configuration (vsize configuration_index) const;
162   Page_spacing_result space_systems_on_n_pages (vsize configuration_index,
163                                                 vsize n, vsize first_page_num);
164   Page_spacing_result space_systems_on_n_or_one_more_pages (vsize configuration_index, vsize n,
165                                                             vsize first_page_num,
166                                                             Real penalty_for_fewer_pages);
167   Page_spacing_result space_systems_on_best_pages (vsize configuration_index,
168                                                    vsize first_page_num);
169   Page_spacing_result space_systems_with_fixed_number_per_page (vsize configuration_index,
170       vsize first_page_num);
171   Page_spacing_result pack_systems_on_least_pages (vsize configuration_index,
172                                                    vsize first_page_num);
173   vsize min_page_count (vsize configuration_index, vsize first_page_num);
174   bool all_lines_stretched (vsize configuration_index);
175   Real blank_page_penalty () const;
176
177   SCM breakpoint_property (vsize breakpoint, char const *str);
178
179   vsize last_break_position () const;
180 private:
181   vector<Break_position> breaks_;
182   vector<Break_position> chunks_;
183   vector<System_spec> system_specs_;
184   vector<Constrained_breaking> line_breaking_;
185   bool ragged_;
186   bool ragged_last_;
187   int systems_per_page_;
188   int max_systems_per_page_;
189   int min_systems_per_page_;
190   vsize system_count_;
191   Real footnote_separator_stencil_height_;
192   Real footnote_padding_;
193   Real footnote_number_raise_;
194   Real footnote_footer_padding_;
195   int orphan_penalty_;
196
197   vector<Line_division> current_configurations_;
198   vector<Break_position> current_chunks_;
199   vsize current_start_breakpoint_;
200   vsize current_end_breakpoint_;
201
202   void cache_line_details (vsize configuration_index);
203   void compute_line_heights ();
204   void clear_line_details_cache ();
205   vsize cached_configuration_index_;
206   vector<Line_details> cached_line_details_;
207   vector<Line_details> uncompressed_line_details_;
208
209   Real paper_height_;
210   mutable vector<Real> page_height_cache_;
211   mutable vector<Real> last_page_height_cache_;
212
213   vector<Break_position> chunk_list (vsize start, vsize end);
214   Line_division system_count_bounds (vector<Break_position> const &chunks, bool min);
215   void line_breaker_args (vsize i,
216                           Break_position const &start,
217                           Break_position const &end,
218                           vsize *line_breaker_start,
219                           vsize *line_breaker_end);
220
221   void line_divisions_rec (vsize system_count,
222                            Line_division const &min,
223                            Line_division const &max,
224                            Line_division *cur);
225
226   vector<Line_details> line_details (vsize start, vsize end, Line_division const &div);
227   Page_spacing_result space_systems_on_1_page (vector<Line_details> const &lines, Real page_height, bool ragged);
228   Page_spacing_result space_systems_on_2_pages (vsize configuration_index, vsize first_page_num);
229   Page_spacing_result finalize_spacing_result (vsize configuration_index, Page_spacing_result);
230   void create_system_list ();
231   void find_chunks_and_breaks (Break_predicate, Prob_break_predicate);
232   SCM make_page (int page_num, bool last) const;
233   SCM get_page_configuration (SCM systems, int page_num, int footnote_count, bool ragged, bool last);
234   SCM draw_page (SCM systems, SCM config, int page_num, int footnote_num, bool last);
235 };
236 #endif /* PAGE_BREAKING_HH */