]> git.donarmstrong.com Git - lilypond.git/blob - lily/break.cc
cc5c6f92a420487135a4c47b9c07496b01f34129
[lilypond.git] / lily / break.cc
1 /*
2   break.cc -- implement Break_algorithm
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "score-column.hh"
10 #include "break.hh"
11 #include "paper-def.hh"
12 #include "spring-spacer.hh"
13 #include "debug.hh"
14 #include "scoreline.hh"
15 #include "p-score.hh"
16 #include "p-col.hh"
17 #include "cpu-timer.hh"
18 #include "command-request.hh"
19
20 String
21 Col_stats::str () const
22 {
23   String s (count_i_);
24   s += _ (" lines");
25   if  (count_i_)
26     s += String (Real (cols_i_)/count_i_, _(", (with an average of %.1f columns)"));
27
28   return s;
29 }
30
31 void
32 Col_stats::add (Line_of_cols const& line)
33 {
34   count_i_++;
35   cols_i_ += line.size ();
36 }
37
38
39 Col_stats::Col_stats ()
40 {
41   count_i_ =0;
42   cols_i_ =0;
43 }
44
45 /* **************************************************************** */
46
47 Line_of_cols
48 Break_algorithm::all_cols () const
49 {
50   Line_of_cols retval;
51   for (PCursor<Paper_column*> c (pscore_l_->col_p_list_.top ());
52        c.ok (); c++)
53     {
54
55       retval.push (c);
56     }
57   return retval;
58 }
59
60 Array<int>
61 Break_algorithm::find_break_indices () const
62 {
63   Line_of_cols all (all_cols ());
64   Array<int> retval;
65
66   for (int i=0; i < all.size (); i++)
67     if (all[i]->breakable_b_)
68       retval.push (i);
69
70   if (linelength <=0)
71     while (retval.size () >2)
72       retval.del (1);
73
74   return retval;
75 }
76
77 ///  return all breakable columns
78 Line_of_cols
79 Break_algorithm::find_breaks () const
80 {
81   Line_of_cols all (all_cols ());
82   Line_of_cols retval;
83
84   for (int i=0; i < all.size (); i++)
85     if (all[i]->breakable_b_)
86       retval.push (all[i]);
87
88
89   if (linelength <=0)
90     while (retval.size () >2)
91       retval.del (1);
92
93   return retval;
94 }
95
96
97
98
99
100 Line_spacer*
101 Break_algorithm::generate_spacing_problem (Line_of_cols curline, Interval line) const
102 {
103   Spring_spacer * sp= (Spring_spacer*) (*get_line_spacer) (); // ugh
104
105   sp->paper_l_ = pscore_l_->paper_l_;
106   sp->add_column (curline[0], true, line.min ());
107   for (int i=1; i< curline.size ()-1; i++)
108     sp->add_column (curline[i]);
109
110   if (line.length () > 0)
111     {
112       sp->add_column (curline.top (), true, line[RIGHT]);
113       sp->energy_normalisation_f_  = sqr (line.length ());
114     }
115   else
116     sp->add_column (curline.top ());
117
118   sp->prepare ();
119   return sp;
120 }
121
122 Break_algorithm::Break_algorithm ()
123 {
124   pscore_l_ = 0;
125   get_line_spacer =0;
126   linelength = 0;
127 }
128
129 void
130 Break_algorithm::set_pscore (Paper_score*s)
131 {
132   pscore_l_ = s;
133   linelength = s->paper_l_->linewidth_f ();
134   do_set_pscore ();
135 }
136
137 bool
138 Break_algorithm::feasible (Line_of_cols curline) const
139 {
140   if (linelength <=  0)
141     return true;
142
143   for (int i=0; i < curline.size (); i++)
144     {
145       if (i && i < curline.size () -1
146           && (((Score_column*)curline[i])->break_penalty_i () >= Break_req::FORCE))
147         return false;
148     }
149   return true;
150 }
151
152 void
153 Break_algorithm::problem_OK () const
154 {
155   if (!pscore_l_->col_p_list_.size ())
156     error (_("Score does not have any columns"));
157   OK ();
158 }
159
160 void
161 Break_algorithm::OK () const
162 {
163 #ifndef NDEBUG
164   iter_top (pscore_l_->col_p_list_,start);
165   PCursor<Paper_column *> end (pscore_l_->col_p_list_.bottom ());
166
167   assert (start->breakable_b_);
168   assert (end->breakable_b_);
169 #endif
170 }
171
172 Array<Col_hpositions>
173 Break_algorithm::solve () const
174 {
175   Cpu_timer timer;
176
177   Array<Col_hpositions> h= do_solve ();
178
179   if (approx_stats_.count_i_)
180     *mlog << _ ("\n(Approximated: ") << approx_stats_.str () << ")\n";
181   if (exact_stats_.count_i_)
182     *mlog << _ ("(Calculated exactly: ") << exact_stats_.str () << ")\n";
183   *mlog << _ ("Time: ") << String (timer.read (), "%.2f") << _ (" seconds\n");
184
185   return h;
186 }
187
188 void
189 Break_algorithm::do_set_pscore ()
190 {
191
192 }