]> git.donarmstrong.com Git - lilypond.git/blob - src/wordwrap.cc
90d4d542dc7504ab23a8efa956d61c42cd210353
[lilypond.git] / src / wordwrap.cc
1 #include "break.hh"
2 #include "pscore.hh"
3 #include "debug.hh"
4
5 /** el stupido. This should be done more accurately:
6
7    It would be nice to have a Dynamic Programming type of algorithm
8    similar to TeX's
9    
10     */
11 Array<Col_hpositions>
12 Word_wrap::solve()
13 {
14     problem_OK();
15     iter_top(pscore_.cols,curcol);
16     Array<Col_hpositions> breaking;
17     Line_of_cols breakpoints(find_breaks());
18     assert(breakpoints.size()>=2);
19
20     int break_idx_i=0;                  
21     while ( break_idx_i < breakpoints.size() -1) {
22         Col_hpositions minimum;
23         Col_hpositions current;
24
25         // do  another line
26         PCol *post = breakpoints[break_idx_i]->postbreak_p_;
27         current.add( post);
28         curcol++;               // skip the breakable.
29         break_idx_i++;
30
31         while (break_idx_i < breakpoints.size()) {
32
33             // add another measure.
34             while (breakpoints[break_idx_i] != curcol.ptr()){
35                 current.add(curcol);
36                 curcol++;
37             }
38             current.add(breakpoints[break_idx_i]->prebreak_p_ );
39
40             // try to solve
41             if (!feasible(current.cols)) {
42                 if (!minimum.cols.size())
43                     error("sorry, this measure is too long");
44                 current.energy = INFTY; // make sure we go back
45             } else {
46                 current = solve_line(current.cols);
47                 current.print();
48             }
49
50             // update minimum, or backup.
51             if (current.energy < minimum.energy) {              
52                 minimum = current;         
53             } else {            // we're one col too far.
54                 break_idx_i--;
55                 while (curcol.ptr() != breakpoints[break_idx_i])
56                     curcol --;
57                 break;          // do the next line.
58             }
59
60
61             // add nobreak version of breakable column
62             current.cols.top()=breakpoints[break_idx_i];
63             curcol ++;
64             break_idx_i++;
65         }
66
67         *mlog << "[" <<break_idx_i<<"]"<<flush;
68         breaking.push(minimum);
69     }
70     
71     return breaking;
72 }
73
74 Word_wrap::Word_wrap(PScore&ps)
75     : Break_algorithm(ps)
76 {
77 }