]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / include / constrained-breaking.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2009 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 struct Line_details {
28   Grob *last_column_;
29   Real force_;
30   Interval extent_;   /* Y-extent of the system */
31
32   Real padding_;  /* compulsory space after this system (if we're not
33                      last on a page) */
34   Real title_padding_;
35   Real bottom_padding_;
36   Real space_;    /* spring length */
37   Real inverse_hooke_;
38
39   SCM break_permission_;
40   SCM page_permission_;
41   SCM turn_permission_;
42   Real break_penalty_;
43   Real page_penalty_;
44   Real turn_penalty_;
45
46   bool title_;
47
48   /* The page-breaker deals with forbidden page breaks by "compressing"
49      two Line_detailses into one. The following fields are used by the
50      page-breaker to keep track of this. If the number of fields needed
51      by the page-breaker grows, it might be a good idea to create a separate
52      class. */
53   int compressed_lines_count_;
54   int compressed_nontitle_lines_count_;
55
56   Line_details ()
57   {
58     last_column_ = 0;
59     force_ = infinity_f;
60     padding_ = 0;
61     title_padding_ = 0;
62     bottom_padding_ = 0;
63     space_ = 0;
64     inverse_hooke_ = 1;
65     break_permission_ = ly_symbol2scm ("allow");
66     page_permission_ = ly_symbol2scm ("allow");
67     turn_permission_ = ly_symbol2scm ("allow");
68     break_penalty_ = 0;
69     page_penalty_ = 0;
70     turn_penalty_ = 0;
71     title_ = false;
72     compressed_lines_count_ = 1;
73     compressed_nontitle_lines_count_ = 1;
74   }
75
76   Line_details (Prob *pb, Output_def *paper);
77 };
78
79 /*
80    Helper to trace back an optimal path
81 */
82 struct Constrained_break_node
83 {
84   /* the number of bars in all the systems before this one
85   */
86   int prev_;
87
88   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
89    * and including, this line */
90   Real demerits_;
91   struct Line_details details_;
92
93   Constrained_break_node ()
94   {
95     prev_ = -1;
96     demerits_ = infinity_f;
97   }
98
99   void print () const
100   {
101     printf ("prev break %d, demerits %f\n", prev_, demerits_);
102   }
103 };
104
105 /*
106    A dynamic programming solution to breaking scores into lines
107 */
108 class Constrained_breaking
109 {
110 public:
111   vector<Column_x_positions> solve (vsize start, vsize end, vsize sys_count);
112   vector<Column_x_positions> best_solution (vsize start, vsize end);
113   vector<Line_details> line_details (vsize start, vsize end, vsize sys_count);
114
115   Constrained_breaking (Paper_score *ps);
116   Constrained_breaking (Paper_score *ps, vector<vsize> const &start_col_posns);
117
118   int max_system_count (vsize start, vsize end);
119   int min_system_count (vsize start, vsize end);
120
121 private:
122   Paper_score *pscore_;
123   vsize valid_systems_;
124   vsize systems_;
125   bool ragged_right_;
126   bool ragged_last_;
127   Real between_system_space_;
128   Real before_title_padding_;
129   Real between_system_padding_;
130
131   /* the (i,j)th entry is the configuration for breaking between
132     columns i and j */
133   Matrix<Line_details> lines_;
134
135   /* the [i](j,k)th entry is the score for fitting the first k bars onto the
136     first j systems, starting at the i'th allowed starting column */
137   vector<Matrix<Constrained_break_node> > state_;
138
139   vector<vsize> start_;         /* the columns at which we might be asked to start breaking */
140   vector<vsize> starting_breakpoints_; /* the corresponding index in breaks_ */
141
142   vector<Grob*> all_;
143   vector<vsize> breaks_;
144
145   void initialize ();
146   void resize (vsize systems);
147
148   Column_x_positions space_line (vsize start_col, vsize end_col);
149   vsize prepare_solution (vsize start, vsize end, vsize sys_count);
150
151   Real combine_demerits (Real force, Real prev_force);
152
153   bool calc_subproblem (vsize start, vsize systems, vsize max_break_index);
154   void fill_line_details (Line_details *const, vsize, vsize);
155 };
156 #endif /* CONSTRAINED_BREAKING_HH */