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