]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
Run grand-replace (issue 3765)
[lilypond.git] / lily / include / constrained-breaking.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2014 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 CONSTRAINED_BREAKING_HH
21 #define CONSTRAINED_BREAKING_HH
22
23 #include "lily-guile.hh"
24 #include "matrix.hh"
25 #include "prob.hh"
26
27 /*
28  * Begin/rest-of-line hack.  This geometrical shape is a crude approximation
29  * of Skyline, but it is better than a rectangle.
30  */
31 struct Line_shape
32 {
33   Interval begin_;
34   Interval rest_;
35
36   Line_shape ()
37   {
38   }
39   Line_shape (Interval begin, Interval rest);
40   Line_shape piggyback (Line_shape mount, Real padding) const;
41 };
42
43 struct Line_details
44 {
45   Grob *last_column_;
46   Real force_;
47   Line_shape shape_;
48   vector<Real> footnote_heights_; /* The footnotes at the bottom of the
49                                    page, where each stencil represents
50                                    a different footnote. */
51   vector<Real> in_note_heights_; /* The in-notes under a system,
52                                    where each stencil represents
53                                    a different in-note. */
54   Interval refpoint_extent_; /* The refpoints of the first and last
55                                 spaceable staff in this line.  min-distance
56                                 should be measured from the bottom
57                                 refpoint_extent of one line to the
58                                 top refpoint_extent of the next. */
59   Real tallness_; /* Y-extent, adjusted according to begin/rest-of-line*/
60
61   Real padding_;  /* compulsory space after this system (if we're not
62                      last on a page) */
63   Real title_padding_;
64   Real min_distance_;
65   Real title_min_distance_;
66   Real bottom_padding_;
67   Real space_;    /* spring length */
68   Real title_space_;
69   Real inverse_hooke_;
70
71   SCM break_permission_;
72   SCM page_permission_;
73   SCM turn_permission_;
74   Real break_penalty_;
75   Real page_penalty_;
76   Real turn_penalty_;
77
78   bool title_;
79
80   /* The page-breaker deals with forbidden page breaks by "compressing"
81      two Line_detailses into one. The following fields are used by the
82      page-breaker to keep track of this. If the number of fields needed
83      by the page-breaker grows, it might be a good idea to create a separate
84      class. */
85   int compressed_lines_count_;
86   int compressed_nontitle_lines_count_;
87   bool last_markup_line_;
88   bool first_markup_line_;
89   bool tight_spacing_;
90
91   Line_details ()
92   {
93     last_column_ = 0;
94     force_ = infinity_f;
95     padding_ = 0;
96     title_padding_ = 0;
97     bottom_padding_ = 0;
98     min_distance_ = 0;
99     title_min_distance_ = 0;
100     space_ = 0;
101     title_space_ = 0;
102     inverse_hooke_ = 1;
103     tight_spacing_ = false;
104     break_permission_ = ly_symbol2scm ("allow");
105     page_permission_ = ly_symbol2scm ("allow");
106     turn_permission_ = ly_symbol2scm ("allow");
107     break_penalty_ = 0;
108     page_penalty_ = 0;
109     turn_penalty_ = 0;
110     title_ = false;
111     compressed_lines_count_ = 1;
112     compressed_nontitle_lines_count_ = 1;
113     last_markup_line_ = false;
114     first_markup_line_ = false;
115     tallness_ = 0;
116     refpoint_extent_ = Interval (0, 0);
117   }
118
119   Line_details (Prob *pb, Output_def *paper);
120   Real full_height () const;
121   Real tallness () const;
122   Real spring_length (Line_details const &next_line) const;
123 };
124
125 /*
126    Helper to trace back an optimal path
127 */
128 struct Constrained_break_node
129 {
130   /* the number of bars in all the systems before this one
131   */
132   int prev_;
133
134   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
135    * and including, this line */
136   Real demerits_;
137   struct Line_details details_;
138
139   Constrained_break_node ()
140   {
141     prev_ = -1;
142     demerits_ = infinity_f;
143   }
144
145   void print () const
146   {
147     printf ("prev break %d, demerits %f\n", prev_, demerits_);
148   }
149 };
150
151 /*
152    A dynamic programming solution to breaking scores into lines
153 */
154 class Constrained_breaking
155 {
156 public:
157   vector<Column_x_positions> solve (vsize start, vsize end, vsize sys_count);
158   vector<Column_x_positions> best_solution (vsize start, vsize end);
159   vector<Line_details> line_details (vsize start, vsize end, vsize sys_count);
160
161   Constrained_breaking (Paper_score *ps);
162   Constrained_breaking (Paper_score *ps, vector<vsize> const &start_col_posns);
163
164   int max_system_count (vsize start, vsize end);
165   int min_system_count (vsize start, vsize end);
166
167 private:
168   Paper_score *pscore_;
169   vsize valid_systems_;
170   vsize systems_;
171   bool ragged_right_;
172   bool ragged_last_;
173
174   Real system_system_min_distance_;
175   Real system_system_padding_;
176   Real system_system_space_;
177   Real system_markup_space_;
178   Real score_system_min_distance_;
179   Real score_system_padding_;
180   Real score_markup_min_distance_;
181   Real score_markup_padding_;
182
183   /* the (i,j)th entry is the configuration for breaking between
184     columns i and j */
185   Matrix<Line_details> lines_;
186
187   /* the [i](j,k)th entry is the score for fitting the first k bars onto the
188     first j systems, starting at the i'th allowed starting column */
189   vector<Matrix<Constrained_break_node> > state_;
190
191   vector<vsize> start_;         /* the columns at which we might be asked to start breaking */
192   vector<vsize> starting_breakpoints_; /* the corresponding index in breaks_ */
193
194   vector<Grob *> all_;
195   vector<vsize> breaks_;
196
197   void initialize ();
198   void resize (vsize systems);
199
200   Column_x_positions space_line (vsize start_col, vsize end_col);
201   vsize prepare_solution (vsize start, vsize end, vsize sys_count);
202
203   Real combine_demerits (Real force, Real prev_force);
204
205   bool calc_subproblem (vsize start, vsize systems, vsize max_break_index);
206   void fill_line_details (Line_details *, vsize, vsize);
207 };
208 #endif /* CONSTRAINED_BREAKING_HH */