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