]> git.donarmstrong.com Git - lilypond.git/blob - lily/break.cc
release: 0.0.75
[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 "linespace.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_.cols,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 void
35 Break_algorithm::generate_spacing_problem(Line_of_cols curline, Spacing_problem & sp)const
36 {
37     sp.add_column(curline[0], true, 0.0);
38     for (int i=1; i< curline.size()-1; i++)
39        sp.add_column(curline[i]);
40
41     if ( linelength > 0)
42         sp.add_column(curline.top(), true, linelength);
43     else
44         sp.add_column(curline.top());
45 }
46
47 Col_hpositions
48 Break_algorithm::stupid_solution(Line_of_cols curline)const
49 {
50     Spacing_problem sp;
51     generate_spacing_problem(curline, sp);
52    Col_hpositions colhpos;
53    colhpos.cols = curline;
54    colhpos.energy = INFTY;
55    colhpos.ugh_b_ = true;
56    colhpos.config = sp.try_initial_solution();
57    return colhpos;
58 }
59
60 /// construct an appropriate Spacing_problem and solve it. 
61 Col_hpositions
62 Break_algorithm::solve_line(Line_of_cols curline) const
63 {
64    Spacing_problem sp;
65    generate_spacing_problem(curline, sp);
66
67    // misschien  moeven uit Spacing_problem? 
68    for (iter_top(pscore_.suz,i); i.ok(); i++) {
69        sp.add_ideal(i);
70    }
71    sp.prepare();
72    
73    Array<Real> the_sol=sp.solve();
74    Col_hpositions col_hpos;
75    col_hpos.cols = curline;
76    col_hpos.energy = the_sol.pop();
77    col_hpos.config = the_sol;
78    col_hpos.error_col_l_arr_ = sp.error_pcol_l_arr();
79    col_hpos.OK();
80    return col_hpos;
81 }
82
83 Break_algorithm::Break_algorithm(PScore&s)
84     :pscore_(s)
85 {
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_.cols.size())
105         error("Score does not have any columns");
106 #ifndef NDEBUG
107     iter_top(pscore_.cols,start);
108     PCursor<PCol *> end (pscore_.cols.bottom());
109     
110     assert(start->breakable_b());    
111     assert(end->breakable_b());
112 #endif
113 }
114
115 Array<Col_hpositions>
116 Break_algorithm::solve()const
117 {
118
119     return do_solve();
120 }
121