lilypond-1.5.10
[lilypond.git] / lily / wordwrap.cc
blob7d25bb9e94cb3025f9e3a5b3d6cc633082b82a31
1 /*
2 wordwrap.cc -- implement Word_wrap
4 source file of the LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "word-wrap.hh"
10 #include "p-score.hh"
11 #include "debug.hh"
12 #include "p-col.hh"
13 #include "spring-spacer.hh"
16 /** el stupido. This should be done more accurately:
18 It would be nice to have a Dynamic Programming type of algorithm
19 similar to TeX's
22 Array<Col_hpositions>
23 Word_wrap::do_solve()const
25 problem_OK();
26 iter_top(pscore_l_->col_p_list_,curcol);
27 Array<Col_hpositions> breaking;
28 Line_of_cols breakpoints(find_breaks());
29 assert(breakpoints.size()>=2);
31 int break_idx_i=0;
32 while ( break_idx_i < breakpoints.size() -1) {
33 Col_hpositions minimum;
34 Col_hpositions current;
36 // do another line
37 PCol *post = breakpoints[break_idx_i]->postbreak_p_;
38 current.add( post);
39 curcol++; // skip the breakable.
40 break_idx_i++;
42 while (break_idx_i < breakpoints.size()) {
44 // add another measure.
45 while (breakpoints[break_idx_i] != curcol.ptr()){
46 current.add(curcol);
47 curcol++;
49 current.add(breakpoints[break_idx_i]->prebreak_p_ );
51 // try to solve
52 if (!feasible(current.cols)) {
53 if (!minimum.cols.size()) {
54 warning("Ugh, this measure is too long, breakpoint: "
55 + String(break_idx_i) +
56 " (generating stupido solution)");
57 current = stupid_solution(current.cols);
58 current.energy = - 1; // make sure we break out.
59 } else
60 current.energy = INFTY_f; // make sure we go back
61 } else {
62 current = solve_line(current.cols);
63 current.print();
66 // update minimum, or backup.
67 if (current.energy < minimum.energy || current.energy < 0) {
68 minimum = current;
69 } else { // we're one col too far.
70 break_idx_i--;
71 while (curcol.ptr() != breakpoints[break_idx_i])
72 curcol --;
73 break; // do the next line.
77 // add nobreak version of breakable column
78 current.cols.top()=breakpoints[break_idx_i];
79 curcol ++;
80 break_idx_i++;
83 *mlog << "[" <<break_idx_i<<"]"<<flush;
84 breaking.push(minimum);
87 return breaking;
90 Word_wrap::Word_wrap(PScore&ps)
91 : Break_algorithm(ps)
93 get_line_spacer = Spring_spacer::constructor;