]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
Fix 1112.
[lilypond.git] / lily / include / constrained-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 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   Grob *last_column_;
45   Real force_;
46   Line_shape shape_;
47   Real tallness_; /* Y-extent, adjusted according to begin/rest-of-line*/
48
49   Real padding_;  /* compulsory space after this system (if we're not
50                      last on a page) */
51   Real title_padding_;
52   Real min_distance_;
53   Real title_min_distance_;
54   Real bottom_padding_;
55   Real space_;    /* spring length */
56   Real inverse_hooke_;
57
58   SCM break_permission_;
59   SCM page_permission_;
60   SCM turn_permission_;
61   Real break_penalty_;
62   Real page_penalty_;
63   Real turn_penalty_;
64
65   bool title_;
66
67   /* The page-breaker deals with forbidden page breaks by "compressing"
68      two Line_detailses into one. The following fields are used by the
69      page-breaker to keep track of this. If the number of fields needed
70      by the page-breaker grows, it might be a good idea to create a separate
71      class. */
72   int compressed_lines_count_;
73   int compressed_nontitle_lines_count_;
74   bool last_markup_line_;
75   bool first_markup_line_;
76   bool tight_spacing_;
77   Real first_refpoint_offset_;
78
79   Line_details ()
80   {
81     last_column_ = 0;
82     force_ = infinity_f;
83     padding_ = 0;
84     title_padding_ = 0;
85     bottom_padding_ = 0;
86     min_distance_ = 0;
87     title_min_distance_ = 0;
88     space_ = 0;
89     inverse_hooke_ = 1;
90     tight_spacing_ = false;
91     break_permission_ = ly_symbol2scm ("allow");
92     page_permission_ = ly_symbol2scm ("allow");
93     turn_permission_ = ly_symbol2scm ("allow");
94     break_penalty_ = 0;
95     page_penalty_ = 0;
96     turn_penalty_ = 0;
97     title_ = false;
98     compressed_lines_count_ = 1;
99     compressed_nontitle_lines_count_ = 1;
100     last_markup_line_ = false;
101     first_markup_line_ = false;
102     tallness_ = 0;
103     first_refpoint_offset_ = 0;
104   }
105
106   Line_details (Prob *pb, Output_def *paper);
107   Real full_height () const;
108   Real tallness () const;
109 };
110
111 /*
112    Helper to trace back an optimal path
113 */
114 struct Constrained_break_node
115 {
116   /* the number of bars in all the systems before this one
117   */
118   int prev_;
119
120   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
121    * and including, this line */
122   Real demerits_;
123   struct Line_details details_;
124
125   Constrained_break_node ()
126   {
127     prev_ = -1;
128     demerits_ = infinity_f;
129   }
130
131   void print () const
132   {
133     printf ("prev break %d, demerits %f\n", prev_, demerits_);
134   }
135 };
136
137 /*
138    A dynamic programming solution to breaking scores into lines
139 */
140 class Constrained_breaking
141 {
142 public:
143   vector<Column_x_positions> solve (vsize start, vsize end, vsize sys_count);
144   vector<Column_x_positions> best_solution (vsize start, vsize end);
145   vector<Line_details> line_details (vsize start, vsize end, vsize sys_count);
146
147   Constrained_breaking (Paper_score *ps);
148   Constrained_breaking (Paper_score *ps, vector<vsize> const &start_col_posns);
149
150   int max_system_count (vsize start, vsize end);
151   int min_system_count (vsize start, vsize end);
152
153 private:
154   Paper_score *pscore_;
155   vsize valid_systems_;
156   vsize systems_;
157   bool ragged_right_;
158   bool ragged_last_;
159
160   Real between_system_min_distance_;
161   Real between_system_padding_;
162   Real between_system_space_;
163   Real between_scores_system_min_distance_;
164   Real between_scores_system_padding_;
165   Real before_title_min_distance_;
166   Real before_title_padding_;
167
168   /* the (i,j)th entry is the configuration for breaking between
169     columns i and j */
170   Matrix<Line_details> lines_;
171
172   /* the [i](j,k)th entry is the score for fitting the first k bars onto the
173     first j systems, starting at the i'th allowed starting column */
174   vector<Matrix<Constrained_break_node> > state_;
175
176   vector<vsize> start_;         /* the columns at which we might be asked to start breaking */
177   vector<vsize> starting_breakpoints_; /* the corresponding index in breaks_ */
178
179   vector<Grob*> all_;
180   vector<vsize> breaks_;
181
182   void initialize ();
183   void resize (vsize systems);
184
185   Column_x_positions space_line (vsize start_col, vsize end_col);
186   vsize prepare_solution (vsize start, vsize end, vsize sys_count);
187
188   Real combine_demerits (Real force, Real prev_force);
189
190   bool calc_subproblem (vsize start, vsize systems, vsize max_break_index);
191   void fill_line_details (Line_details *const, vsize, vsize);
192 };
193 #endif /* CONSTRAINED_BREAKING_HH */