]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-algorithm.cc
patch::: 1.2.8.jcn1
[lilypond.git] / lily / break-algorithm.cc
1 /*
2   break.cc -- implement Break_algorithm
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.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 "line-of-score.hh"
15 #include "paper-score.hh"
16 #include "paper-column.hh"
17 #include "cpu-timer.hh"
18 #include "command-request.hh"
19 #include "spring-spacer.hh"
20 #include "simple-spacer.hh"
21
22
23 String
24 Col_stats::str () const
25 {
26   String s;
27   if (!count_i_)
28     s = _ ("0 lines");
29   else if (count_i_ == 1)
30     s = _f ("1 line (of %.0f columns)", (Real)cols_i_/count_i_);
31   else
32     s = _f ("%d lines (with an average of %.1f columns)", 
33       count_i_, (Real)cols_i_/count_i_);
34   return s;
35 }
36
37 void
38 Col_stats::add (Line_of_cols const& line)
39 {
40   count_i_++;
41   cols_i_ += line.size ();
42 }
43
44
45 Col_stats::Col_stats ()
46 {
47   count_i_ =0;
48   cols_i_ =0;
49 }
50
51 /* **************************************************************** */
52
53
54 Array<int>
55 Break_algorithm::find_break_indices () const
56 {
57   Line_of_cols all (pscore_l_->col_l_arr_);
58   Array<int> retval;
59
60   for (int i=0; i < all.size (); i++)
61     if (all[i]->breakable_b ())
62       retval.push (i);
63
64   if (linelength <=0)
65     while (retval.size () >2)
66       retval.del (1);
67
68   return retval;
69 }
70
71
72 Line_of_cols
73 Break_algorithm::find_breaks () const
74 {
75   Line_of_cols all (pscore_l_->col_l_arr_);
76   Line_of_cols retval;
77
78   for (int i=0; i < all.size (); i++)
79     if (all[i]->breakable_b ())
80       retval.push (all[i]);
81
82   if (linelength <=0)
83     while (retval.size () >2)
84       retval.del (1);
85
86   return retval;
87 }
88
89
90 Line_spacer*
91 Break_algorithm::generate_spacing_problem (Line_of_cols curline, Interval line) const
92 {
93   Real r = pscore_l_->paper_l_->get_var ("simple_spacing_solver");
94     
95   Line_spacer * sp = 0;
96   if (r)
97     sp = new Simple_spacer;
98   else
99     sp = new Spring_spacer;
100   
101   sp->default_space_f_ = pscore_l_->paper_l_->get_var ("loose_column_distance");
102
103   sp->indent_f_ = line[LEFT];
104
105   /*
106     sort out how interfacing this should work;
107    */
108   if (line.empty_b())
109     {
110      sp->line_len_f_ = -1;
111     }
112   else
113     sp->line_len_f_ = line.length ();
114   
115   sp->add_columns (curline);
116   sp->prepare ();
117
118   return sp;
119 }
120
121 Break_algorithm::Break_algorithm ()
122 {
123   pscore_l_ = 0;
124   get_line_spacer =0;
125   linelength = 0;
126 }
127
128 void
129 Break_algorithm::set_pscore (Paper_score*s)
130 {
131   pscore_l_ = s;
132   linelength = s->paper_l_->linewidth_f ();
133   do_set_pscore ();
134 }
135
136 bool
137 Break_algorithm::feasible (Line_of_cols curline) const
138 {
139   if (linelength <=  0)
140     return true;
141
142   for (int i=0; i < curline.size (); i++)
143     {
144       if (i && i < curline.size () -1
145           && ((dynamic_cast<Score_column*>(curline[i]))->break_penalty_i () >= Break_req::FORCE))
146         return false;
147     }
148   return true;
149 }
150
151 void
152 Break_algorithm::problem_OK () const
153 {
154   if (pscore_l_->col_l_arr_.empty ())
155     error (_("Score does not have any columns"));
156   OK ();
157 }
158
159 void
160 Break_algorithm::OK () const
161 {
162 }
163
164 Array<Column_x_positions>
165 Break_algorithm::solve () const
166 {
167   Cpu_timer timer;
168
169   Array<Column_x_positions> h= do_solve ();
170
171   if (approx_stats_.count_i_)
172     *mlog << '\n' << _f ("approximated %s", approx_stats_.str ()) << endl;
173   if (exact_stats_.count_i_)
174     *mlog << _f ("calculated %s exactly", exact_stats_.str ()) << endl;
175   *mlog << _f ("elapsed time %.2f seconds",  timer.read ()) << endl;
176
177   return h;
178 }
179
180 void
181 Break_algorithm::do_set_pscore ()
182 {
183
184 }