]> git.donarmstrong.com Git - lilypond.git/blob - lily/break.cc
8d3779e4da1eb7bdf842b343b9b2e4164e30d657
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9
10 #include "break.hh"
11 #include "paper-def.hh"
12 #include "line-spacer.hh"
13 #include "debug.hh"
14 #include "scoreline.hh"
15 #include "p-score.hh"
16 #include "p-col.hh"
17
18 ///  return all breakable columns
19 Line_of_cols
20 Break_algorithm::find_breaks() const
21 {
22     Line_of_cols retval;
23     for (iter_top(pscore_l_->col_p_list_,c); c.ok(); c++) {
24         if (c->breakable_b())
25             retval.push(c);
26     }
27     if ( linelength <=0)
28         while ( retval.size() >2)
29             retval.del(1);
30
31     return retval;
32 }
33  
34 Line_spacer*
35 Break_algorithm::generate_spacing_problem(Line_of_cols curline)const
36 {
37     Line_spacer * sp= (*get_line_spacer)();
38     sp->paper_l_ = pscore_l_->paper_l_;
39     sp->add_column(curline[0], true, 0.0);
40     for (int i=1; i< curline.size()-1; i++)
41         sp->add_column(curline[i]);
42
43     if ( linelength > 0)
44         sp->add_column(curline.top(), true, linelength);
45     else
46         sp->add_column(curline.top());
47     return sp;
48 }
49
50 Col_hpositions
51 Break_algorithm::stupid_solution(Line_of_cols curline)const
52 {
53     Line_spacer *sp =generate_spacing_problem(curline);
54     Col_hpositions colhpos;
55     colhpos.cols = curline;
56     colhpos.energy = INFTY_f;
57     colhpos.ugh_b_ = true;
58     colhpos.config = sp->default_solution();
59     delete sp;
60     return colhpos;
61 }
62
63 /// construct an appropriate Spacing_problem and solve it. 
64 Col_hpositions
65 Break_algorithm::solve_line(Line_of_cols curline) const
66 {
67     Line_spacer *sp = generate_spacing_problem(curline);
68     sp->prepare();
69    
70     Array<Real> the_sol=sp->solve();
71     Col_hpositions col_hpos;
72     col_hpos.cols = curline;
73     col_hpos.energy = the_sol.pop();
74     col_hpos.config = the_sol;
75     col_hpos.error_col_l_arr_ = sp->error_pcol_l_arr();
76     col_hpos.OK();
77     delete sp;
78    
79     return col_hpos;
80 }
81
82 Break_algorithm::Break_algorithm(PScore&s)
83 {
84     pscore_l_ = &s;
85     get_line_spacer =0;
86     linelength = s.paper_l_->linewidth_f();
87 }
88
89 bool
90 Break_algorithm::feasible(Line_of_cols curline) const
91 {
92     if (linelength <=  0)
93         return true;
94     
95     Real l =0;
96     for (int i=0; i < curline.size(); i++)
97         l +=curline[i]->width().length();
98     return l < linelength;    
99 }
100
101 void
102 Break_algorithm::problem_OK() const
103 {
104     if (!pscore_l_->col_p_list_.size())
105         error("Score does not have any columns");
106     OK();
107 }
108
109 void
110 Break_algorithm::OK()const
111 {
112 #ifndef NDEBUG
113     iter_top(pscore_l_->col_p_list_,start);
114     PCursor<PCol *> end (pscore_l_->col_p_list_.bottom());
115     
116     assert(start->breakable_b());    
117     assert(end->breakable_b());
118 #endif
119 }
120
121 Array<Col_hpositions>
122 Break_algorithm::solve()const
123 {
124     return do_solve();
125 }
126