lilypond-0.1.56
[lilypond.git] / src / wordwrap.cc
blobf217a63b0812da110e012f18fa3615e7d6b23bd3
1 #include "break.hh"
2 #include "pscore.hh"
3 #include "debug.hh"
5 /** el stupido. This should be done more accurately:
7 It would be nice to have a Dynamic Programming type of algorithm
8 similar to TeX's
11 Array<Col_hpositions>
12 Word_wrap::solve()
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);
20 int break_idx_i=0;
21 while ( break_idx_i < breakpoints.size() -1) {
22 Col_hpositions minimum;
23 Col_hpositions current;
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++;
31 while (break_idx_i < breakpoints.size()) {
33 // add another measure.
34 while (breakpoints[break_idx_i] != curcol.ptr()){
35 current.add(curcol);
36 curcol++;
38 current.add(breakpoints[break_idx_i]->prebreak_p_ );
40 // try to solve
41 if (!feasible(current.cols)) {
42 if (!minimum.cols.size())
43 error("sorry, this measure is too long, breakpoint: "
44 + String(break_idx_i) );
45 current.energy = INFTY; // make sure we go back
46 } else {
47 current = solve_line(current.cols);
48 current.print();
51 // update minimum, or backup.
52 if (current.energy < minimum.energy) {
53 minimum = current;
54 } else { // we're one col too far.
55 break_idx_i--;
56 while (curcol.ptr() != breakpoints[break_idx_i])
57 curcol --;
58 break; // do the next line.
62 // add nobreak version of breakable column
63 current.cols.top()=breakpoints[break_idx_i];
64 curcol ++;
65 break_idx_i++;
68 *mlog << "[" <<break_idx_i<<"]"<<flush;
69 breaking.push(minimum);
72 return breaking;
75 Word_wrap::Word_wrap(PScore&ps)
76 : Break_algorithm(ps)