]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
Merge commit 'origin/dev/jneeman' into systems-per-page
[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   Real force_;
19   Interval extent_;   /* Y-extent of the system */
20
21   Real padding_;  /* compulsory space after this system (if we're not
22                      last on a page) */
23   Real bottom_padding_;
24   Real space_;    /* spring length */
25   Real inverse_hooke_;
26
27   SCM break_permission_;
28   SCM page_permission_;
29   SCM turn_permission_;
30   Real break_penalty_;
31   Real page_penalty_;
32   Real turn_penalty_;
33
34   bool title_;
35
36   /* The page-breaker deals with forbidden page breaks by "compressing"
37      two Line_detailses into one. The following fields are used by the
38      page-breaker to keep track of this. If the number of fields needed
39      by the page-breaker grows, it might be a good idea to create a separate
40      class. */
41   int compressed_lines_count_;
42   int compressed_nontitle_lines_count_;
43
44   Line_details ()
45   {
46     force_ = infinity_f;
47     padding_ = 0;
48     bottom_padding_ = 0;
49     space_ = 0;
50     inverse_hooke_ = 1;
51     break_permission_ = ly_symbol2scm ("allow");
52     page_permission_ = ly_symbol2scm ("allow");
53     turn_permission_ = ly_symbol2scm ("allow");
54     break_penalty_ = 0;
55     page_penalty_ = 0;
56     turn_penalty_ = 0;
57     title_ = false;
58     compressed_lines_count_ = 1;
59     compressed_nontitle_lines_count_ = 1;
60   }
61
62   Line_details (Prob *pb)
63   {
64     force_ = 0;
65     extent_ = unsmob_stencil (pb->get_property ("stencil")) ->extent (Y_AXIS);
66     padding_ = robust_scm2double (pb->get_property ("next-padding"), 0);
67     bottom_padding_ = 0;
68     space_ = robust_scm2double (pb->get_property ("next-space"), 1.0);
69     inverse_hooke_ = 1.0;
70     break_permission_ = ly_symbol2scm ("allow");
71     page_permission_ = pb->get_property ("page-break-permission");
72     turn_permission_ = pb->get_property ("page-turn-permission");
73     break_penalty_ = 0;
74     page_penalty_ = robust_scm2double (pb->get_property ("page-break-penalty"), 0);
75     turn_penalty_ = robust_scm2double (pb->get_property ("page-turn-penalty"), 0);
76     title_ = to_boolean (pb->get_property ("is-title"));
77     compressed_lines_count_ = 1;
78     compressed_nontitle_lines_count_ = title_ ? 0 : 1;
79   }
80 };
81
82 /*
83    Helper to trace back an optimal path
84 */
85 struct Constrained_break_node
86 {
87   /* the number of bars in all the systems before this one
88   */
89   int prev_;
90
91   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
92    * and including, this line */
93   Real demerits_;
94   struct Line_details details_;
95
96   Constrained_break_node ()
97   {
98     prev_ = -1;
99     demerits_ = infinity_f;
100   }
101
102   void print () const
103   {
104     printf ("prev break %d, demerits %f\n", prev_, demerits_);
105   }
106 };
107
108 /*
109    A dynamic programming solution to breaking scores into lines
110 */
111 class Constrained_breaking
112 {
113 public:
114   vector<Column_x_positions> solve (vsize start, vsize end, vsize sys_count);
115   vector<Column_x_positions> best_solution (vsize start, vsize end);
116   vector<Line_details> line_details (vsize start, vsize end, vsize sys_count);
117
118   Constrained_breaking (Paper_score *ps);
119   Constrained_breaking (Paper_score *ps, vector<vsize> const &start_col_posns);
120
121   int max_system_count (vsize start, vsize end);
122   int min_system_count (vsize start, vsize end);
123
124 private:
125   Paper_score *pscore_;
126   vsize valid_systems_;
127   vsize systems_;
128   bool ragged_right_;
129   bool ragged_last_;
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 };
155 #endif /* CONSTRAINED_BREAKING_HH */