]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/page-breaking.hh
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / include / page-breaking.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2010 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 in system_spec_index_, if VPOS start of book. 
56    */
57   vsize system_spec_index_;
58
59   /* if system_spec_index_ is a score, then we start at the score_brk_'th possible
60      page-break in the score */
61   vsize score_break_; 
62
63   /* if system_spec_index_ is a score, this points to the broken column */
64   Grob *col_;  
65   bool score_ender_;
66
67   /* if non-zero, this is the (fixed, uncompressed) number of lines between
68      this Break_position and the previous. */
69   int forced_line_count_;
70
71   Break_position (vsize s=VPOS, vsize brk=VPOS, Grob *g=NULL, bool end=false)
72   {
73     system_spec_index_ = s;
74     score_break_ = brk;
75     col_ = g;
76     score_ender_ = end;
77     forced_line_count_ = 0;
78   }
79
80   /*
81     lexicographic in (system_spec_index_, score_break_)
82    */
83   bool operator< (const Break_position &other)
84   {
85     return (system_spec_index_ == VPOS && other.system_spec_index_ != VPOS)
86       || (system_spec_index_ < other.system_spec_index_)
87       || (system_spec_index_ == other.system_spec_index_ && score_break_ < other.score_break_);
88   }
89
90   bool operator<= (const Break_position &other)
91   {
92     return (system_spec_index_ == VPOS)
93       || (system_spec_index_ < other.system_spec_index_ && other.system_spec_index_ != VPOS)
94       || (system_spec_index_ == other.system_spec_index_ && score_break_ <= other.score_break_);
95   }
96 };
97
98 class Page_breaking
99 {
100 public:
101   typedef bool (*Break_predicate) (Grob *);
102   typedef vector<vsize> Line_division;
103   
104   /*
105     TODO: naming.
106
107     determine the page breaking, and break scores into lines
108     appropriately.
109    */
110   virtual SCM solve () = 0;
111
112   Page_breaking (Paper_book *pb, Break_predicate);
113   virtual ~Page_breaking ();
114
115   bool ragged () const;
116   bool ragged_last () const;
117   bool is_last () const;
118   bool ends_score () const;
119   int systems_per_page () const;
120   int max_systems_per_page () const;
121   int min_systems_per_page () const;
122   Real page_height (int page_number, bool last) const;
123   vsize system_count () const;
124   Real line_count_penalty (int line_count) const;
125   int line_count_status (int line_count) const;
126   bool too_many_lines (int line_count) const;
127   bool too_few_lines (int line_count) const;
128   Real min_whitespace_at_top_of_page (Line_details const&) const;
129   Real min_whitespace_at_bottom_of_page (Line_details const&) const;
130   int orphan_penalty () const;
131
132 protected:
133   Paper_book *book_;
134
135   vsize next_system (Break_position const &break_pos) const;
136
137   SCM make_pages (vector<vsize> lines_per_page, SCM lines);
138
139   vsize min_system_count (vsize start, vsize end);
140   vsize max_system_count (vsize start, vsize end);
141
142
143   void break_into_pieces (vsize start, vsize end, Line_division const &div);
144   SCM systems ();
145
146   void set_current_breakpoints (vsize start,
147                                 vsize end,
148                                 vsize system_count,
149                                 Line_division lower_bound = Line_division (),
150                                 Line_division upper_bound = Line_division ());
151   void set_to_ideal_line_configuration (vsize start, vsize end);
152
153   vsize current_configuration_count () const;
154   Line_division current_configuration (vsize configuration_index) const;
155   Page_spacing_result space_systems_on_n_pages (vsize configuration_index,
156                                                 vsize n, vsize first_page_num);
157   Page_spacing_result space_systems_on_n_or_one_more_pages (vsize configuration_index, vsize n,
158                                                             vsize first_page_num,
159                                                             Real penalty_for_fewer_pages);
160   Page_spacing_result space_systems_on_best_pages (vsize configuration_index,
161                                                    vsize first_page_num);
162   Page_spacing_result space_systems_with_fixed_number_per_page (vsize configuration_index,
163                                                                 vsize first_page_num);
164   Page_spacing_result pack_systems_on_least_pages (vsize configuration_index,
165                                                    vsize first_page_num);
166   vsize min_page_count (vsize configuration_index, vsize first_page_num);
167   bool all_lines_stretched (vsize configuration_index);
168   Real blank_page_penalty () const;
169
170   SCM breakpoint_property (vsize breakpoint, char const *str);
171
172   vsize last_break_position () const;
173 private:
174   vector<Break_position> breaks_;
175   vector<Break_position> chunks_;
176   vector<System_spec> system_specs_;
177   vector<Constrained_breaking> line_breaking_;
178   bool ragged_;
179   bool ragged_last_;
180   int systems_per_page_;
181   int max_systems_per_page_;
182   int min_systems_per_page_;
183   vsize system_count_;
184   int orphan_penalty_;
185
186   vector<Line_division> current_configurations_;
187   vector<Break_position> current_chunks_;
188   vsize current_start_breakpoint_;
189   vsize current_end_breakpoint_;
190
191   void cache_line_details (vsize configuration_index);
192   void clear_line_details_cache ();
193   vsize cached_configuration_index_;
194   vector<Line_details> cached_line_details_;
195   vector<Line_details> uncompressed_line_details_;
196
197   vector<Break_position> chunk_list (vsize start, vsize end);
198   Line_division system_count_bounds (vector<Break_position> const &chunks, bool min);
199   void line_breaker_args (vsize i,
200                           Break_position const &start,
201                           Break_position const &end,
202                           vsize *line_breaker_start,
203                           vsize *line_breaker_end);
204
205   void line_divisions_rec (vsize system_count,
206                            Line_division const &min,
207                            Line_division const &max,
208                            Line_division *cur);
209
210   vector<Line_details> line_details (vsize start, vsize end, Line_division const &div);
211   Page_spacing_result space_systems_on_1_page (vector<Line_details> const &lines, Real page_height, bool ragged);
212   Page_spacing_result space_systems_on_2_pages (vsize configuration_index, vsize first_page_num);
213   Page_spacing_result finalize_spacing_result (vsize configuration_index, Page_spacing_result);
214   void create_system_list ();
215   void find_chunks_and_breaks (Break_predicate);
216   SCM make_page (int page_num, bool last) const;
217   SCM get_page_configuration (SCM systems, int page_num, bool ragged, bool last);
218   SCM draw_page (SCM systems, SCM config, int page_num, bool last);
219 };
220 #endif /* PAGE_BREAKING_HH */