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