]> git.donarmstrong.com Git - lilypond.git/blob - break.cc
e705c546cfe4cd0227baac0a0a42b86bbe38f7ce
[lilypond.git] / break.cc
1 /*
2     do calculations for breaking problem
3     
4     */
5
6 #include "linespace.hh"
7 #include "debug.hh"
8 #include "line.hh"
9 #include "pscore.hh"
10
11 // construct an appropriate Spacing_problem and solve it. 
12 svec<Real>
13 PScore::solve_line(svec<const PCol *> curline) const
14 {
15    Spacing_problem sp;
16    mtor << "line of " << curline.sz() << " cols\n";
17    sp.add_column(curline[0], true, 0.0);
18    for (int i=1; i< curline.sz()-1; i++)
19        sp.add_column(curline[i]);
20    sp.add_column(curline.last(), true, linewidth);
21
22    // misschien  moeven uit Spacing_problem? 
23    for (PCursor<Idealspacing *> i(suz); i.ok(); i++) {
24        sp.add_ideal(i);
25    }
26    svec<Real> the_sol=sp.solve();
27    return the_sol;
28 }
29
30
31 void
32 PScore::problem_OK() const
33 {
34     if (!cols.size())
35         error("PScore::problem_OK(): Score does not have any columns");
36     PCursor<PCol *> start(cols);
37     PCursor<PCol *> end (((PScore*)this)->cols.bottom());
38     
39     assert(start->breakable);    
40     assert(end->breakable);
41 }
42
43 struct Col_configuration {
44     svec<const PCol*> line;
45     svec<Real> config;
46     Real energy;
47
48     Col_configuration() {
49         energy = INFTY;
50     }
51     void add(const PCol*c) { line.add(c);}
52     void setsol(svec<Real> sol) {
53         config = sol;
54         energy = config.last();
55         config.pop();
56     }
57 };
58
59 /// wordwrap type algorithm
60 /* el stupido. This should be optimised:
61
62    It would be nice to have a Dynamic Programming type of algorithm
63    similar to TeX's
64    
65     */
66
67 void
68 PScore::calc_breaking()
69 {
70     OK();
71     problem_OK();
72     PCursor<PCol *> curcol(cols);
73             
74     svec<const PCol *> breakpoints(find_breaks());
75     assert(breakpoints.sz()>=2);
76     for (int i=0 ; i < breakpoints.sz() -1; ) {
77         Col_configuration minimum;
78         Col_configuration current;
79
80         // do  another line 
81         current.add(breakpoints[i]->postbreak );
82         curcol++;               // skip the breakable.
83         i++;
84
85         while (i < breakpoints.sz()) {
86
87             // add another measure.
88             while(breakpoints[i] !=curcol){
89                 
90                 current.add(curcol);
91                 curcol++;
92             }
93             current.add(breakpoints[i]->prebreak );
94             current.setsol(solve_line(current.line));
95             mtor << "energy : " << current.energy << '\n';
96             
97             if (current.energy < minimum.energy) {
98                 minimum = current;
99             } else {
100                 break;
101             }
102         
103             current.line.last()=breakpoints[i];
104             curcol ++;
105             i++;
106         }
107         mtor << "Adding line, next breakpoint " << i << '\n';
108         add_line(minimum.line, minimum.config); 
109     }
110 }
111