]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/constrained-breaking.hh
5759a12c05666f78877391c3c972a8f8464f6454
[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--2007 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   Line_details ()
37   {
38     force_ = infinity_f;
39     padding_ = 0;
40     bottom_padding_ = 0;
41     space_ = 0;
42     inverse_hooke_ = 1;
43     break_permission_ = ly_symbol2scm ("allow");
44     page_permission_ = ly_symbol2scm ("allow");
45     turn_permission_ = ly_symbol2scm ("allow");
46     break_penalty_ = 0;
47     page_penalty_ = 0;
48     turn_penalty_ = 0;
49     title_ = false;
50   }
51
52   Line_details (Prob *pb)
53   {
54     force_ = 0;
55     extent_ = unsmob_stencil (pb->get_property ("stencil")) ->extent (Y_AXIS);
56     padding_ = robust_scm2double (pb->get_property ("next-padding"), 0);
57     bottom_padding_ = 0;
58     space_ = robust_scm2double (pb->get_property ("next-space"), 1.0);
59     inverse_hooke_ = 1.0;
60     break_permission_ = ly_symbol2scm ("allow");
61     page_permission_ = pb->get_property ("page-break-permission");
62     turn_permission_ = pb->get_property ("page-turn-permission");
63     break_penalty_ = 0;
64     page_penalty_ = robust_scm2double (pb->get_property ("page-break-penalty"), 0);
65     turn_penalty_ = robust_scm2double (pb->get_property ("page-turn-penalty"), 0);
66     title_ = to_boolean (pb->get_property ("is-title"));
67   }
68 };
69
70 /*
71    Helper to trace back an optimal path
72 */
73 struct Constrained_break_node
74 {
75   /* the number of bars in all the systems before this one
76   */
77   int prev_;
78
79   /* unlike the Gourlay breaker, this is the sum of all demerits up to,
80    * and including, this line */
81   Real demerits_;
82   struct Line_details details_;
83
84   Constrained_break_node ()
85   {
86     prev_ = -1;
87     demerits_ = infinity_f;
88   }
89
90   void print () const
91   {
92     printf ("prev break %d, demerits %f\n", prev_, demerits_);
93   }
94 };
95
96 /*
97    A dynamic programming solution to breaking scores into lines
98 */
99 class Constrained_breaking
100 {
101 public:
102   vector<Column_x_positions> solve (vsize start, vsize end, vsize sys_count);
103   vector<Column_x_positions> best_solution (vsize start, vsize end);
104   vector<Line_details> line_details (vsize start, vsize end, vsize sys_count);
105
106   Constrained_breaking (Paper_score *ps);
107   Constrained_breaking (Paper_score *ps, vector<vsize> const &start_col_posns);
108
109   int max_system_count (vsize start, vsize end);
110   int min_system_count (vsize start, vsize end);
111
112 private:
113   Paper_score *pscore_;
114   vsize valid_systems_;
115   vsize systems_;
116   bool ragged_right_;
117   bool ragged_last_;
118
119   /* the (i,j)th entry is the configuration for breaking between
120     columns i and j */
121   Matrix<Line_details> lines_;
122
123   /* the [i](j,k)th entry is the score for fitting the first k bars onto the
124     first j systems, starting at the i'th allowed starting column */
125   vector<Matrix<Constrained_break_node> > state_;
126
127   vector<vsize> start_;         /* the columns at which we might be asked to start breaking */
128   vector<vsize> starting_breakpoints_; /* the corresponding index in breaks_ */
129
130   vector<Grob*> all_;
131   vector<vsize> breaks_;
132
133   void initialize ();
134   void resize (vsize systems);
135
136   Column_x_positions space_line (vsize start_col, vsize end_col);
137   vsize prepare_solution (vsize start, vsize end, vsize sys_count);
138
139   Real combine_demerits (Real force, Real prev_force);
140
141   bool calc_subproblem(vsize start, vsize systems, vsize max_break_index);
142 };
143 #endif /* CONSTRAINED_BREAKING_HH */