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