lilypond-1.0.8
[lilypond.git] / lily / break.cc
blobf6c374dce5c88f3cdb745131398adb0190422ba3
1 /*
2 break.cc -- implement Break_algorithm
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
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 "line-of-score.hh"
15 #include "p-score.hh"
16 #include "p-col.hh"
17 #include "cpu-timer.hh"
18 #include "command-request.hh"
20 String
21 Col_stats::str () const
23 String s;
24 if (!count_i_)
25 s = _ ("0 lines");
26 else if (count_i_ == 1)
27 s = _f ("1 line (of %.0f columns)", (Real)cols_i_/count_i_);
28 else
29 s = _f ("%d lines (with an average of %.1f columns)",
30 count_i_, (Real)cols_i_/count_i_);
31 return s;
34 void
35 Col_stats::add (Line_of_cols const& line)
37 count_i_++;
38 cols_i_ += line.size ();
42 Col_stats::Col_stats ()
44 count_i_ =0;
45 cols_i_ =0;
48 /* **************************************************************** */
50 Line_of_cols
51 Break_algorithm::all_cols () const
53 Line_of_cols retval;
54 for (PCursor<Paper_column*> c (pscore_l_->col_p_list_.top ());
55 c.ok (); c++)
58 retval.push (c);
60 return retval;
63 Array<int>
64 Break_algorithm::find_break_indices () const
66 Line_of_cols all (all_cols ());
67 Array<int> retval;
69 for (int i=0; i < all.size (); i++)
70 if (all[i]->breakable_b_)
71 retval.push (i);
73 if (linelength <=0)
74 while (retval.size () >2)
75 retval.del (1);
77 return retval;
80 /// return all breakable columns
81 Line_of_cols
82 Break_algorithm::find_breaks () const
84 Line_of_cols all (all_cols ());
85 Line_of_cols retval;
87 for (int i=0; i < all.size (); i++)
88 if (all[i]->breakable_b_)
89 retval.push (all[i]);
92 if (linelength <=0)
93 while (retval.size () >2)
94 retval.del (1);
96 return retval;
103 Line_spacer*
104 Break_algorithm::generate_spacing_problem (Line_of_cols curline, Interval line) const
106 Spring_spacer * sp= (Spring_spacer*) (*get_line_spacer) (); // ugh
108 sp->paper_l_ = pscore_l_->paper_l_;
109 sp->add_column (curline[0], true, line[LEFT]);
110 for (int i=1; i< curline.size ()-1; i++)
111 sp->add_column (curline[i]);
113 if (line.length () > 0)
115 sp->add_column (curline.top (), true, line[RIGHT]);
116 sp->energy_normalisation_f_ = sqr (line.length ());
118 else
119 sp->add_column (curline.top ());
121 sp->prepare ();
122 return sp;
125 Break_algorithm::Break_algorithm ()
127 pscore_l_ = 0;
128 get_line_spacer =0;
129 linelength = 0;
132 void
133 Break_algorithm::set_pscore (Paper_score*s)
135 pscore_l_ = s;
136 linelength = s->paper_l_->linewidth_f ();
137 do_set_pscore ();
140 bool
141 Break_algorithm::feasible (Line_of_cols curline) const
143 if (linelength <= 0)
144 return true;
146 for (int i=0; i < curline.size (); i++)
148 if (i && i < curline.size () -1
149 && (((Score_column*)curline[i])->break_penalty_i () >= Break_req::FORCE))
150 return false;
152 return true;
155 void
156 Break_algorithm::problem_OK () const
158 if (!pscore_l_->col_p_list_.size ())
159 error (_("score does not have any columns"));
160 OK ();
163 void
164 Break_algorithm::OK () const
166 #ifndef NDEBUG
167 iter_top (pscore_l_->col_p_list_,start);
168 PCursor<Paper_column *> end (pscore_l_->col_p_list_.bottom ());
170 assert (start->breakable_b_);
171 assert (end->breakable_b_);
172 #endif
175 Array<Column_x_positions>
176 Break_algorithm::solve () const
178 Cpu_timer timer;
180 Array<Column_x_positions> h= do_solve ();
182 if (approx_stats_.count_i_)
183 *mlog << '\n' << _f ("approximated: %s", approx_stats_.str ()) << endl;
184 if (exact_stats_.count_i_)
185 *mlog << _f ("calculated exactly: %s", exact_stats_.str ()) << endl;
186 *mlog << _f ("time: %.2f seconds", timer.read ()) << endl;
188 return h;
191 void
192 Break_algorithm::do_set_pscore ()