X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fgourlay-breaking.cc;h=df64d3e171f5a6a7ae02fc6f1898ed911270cc49;hb=75eebcb49e52d296b1da3e1074e0825d2c780db4;hp=93192d1a289ad5fc0927f108de501fac7e257e99;hpb=15551c56a17c10f2e1eae0766ddc87a62da119a8;p=lilypond.git diff --git a/lily/gourlay-breaking.cc b/lily/gourlay-breaking.cc index 93192d1a28..df64d3e171 100644 --- a/lily/gourlay-breaking.cc +++ b/lily/gourlay-breaking.cc @@ -3,18 +3,20 @@ source file of the GNU LilyPond music typesetter - (c) 1997--2004 Han-Wen Nienhuys + (c) 1997--2006 Han-Wen Nienhuys */ -#include // rint -#include #include "gourlay-breaking.hh" -#include "column-x-positions.hh" + +#include // rint +#include +using namespace std; + #include "warn.hh" #include "main.hh" #include "paper-column.hh" #include "paper-score.hh" -#include "paper-def.hh" +#include "output-def.hh" #include "simple-spacer.hh" #include "system.hh" @@ -22,23 +24,24 @@ const int HAPPY_DOTS_I = 3; /** - Helper to trace back an optimal path - */ -struct Break_node { + Helper to trace back an optimal path +*/ +struct Break_node +{ /** this was the previous. If negative, this break should not be - considered: this path has infinite energy - - */ + considered: this path has infinite energy + + */ int prev_break_; /** Which system number so far? - */ + */ int line_; Real demerits_; Column_x_positions line_config_; - - Break_node () + + Break_node () { prev_break_ = -1; line_ = 0; @@ -53,72 +56,75 @@ struct Break_node { }; void -print_break_nodes (Array const & arr) +print_break_nodes (Array const &arr) { - for (int i =0; i < arr.size (); i++) + for (int i = 0; i < arr.size (); i++) { - printf ( "node %d: ", i); + printf ("node %d: ", i); arr[i].print (); - } + } } /** - This algorithms is adapted from the OSU Tech report on breaking lines. + This algorithms is adapted from the OSU Tech report on breaking lines. + + this function is longish, but not very complicated. - this function is longish, but not very complicated. - */ + TODO: should rewrite. See the function in scm/page-layout.scm for + inspiration. +*/ Array Gourlay_breaking::do_solve () const { Array optimal_paths; - Link_array all = - pscore_->system_->columns (); - + Link_array all + = pscore_->root_system ()->columns (); + Array breaks = find_break_indices (); - - Break_node first_node ; + + Break_node first_node; optimal_paths.push (first_node); - bool ragged_right = to_boolean (pscore_->paper_->get_scmvar ("raggedright")); - bool ragged_last = to_boolean (pscore_->paper_->get_scmvar ("raggedlast")); + bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("raggedright")); + bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("raggedlast")); Real worst_force = 0.0; - for (int break_idx = 1; break_idx< breaks.size (); break_idx++) + for (int break_idx = 1; break_idx < breaks.size (); break_idx++) { /* - start with a short line, add measures. At some point - the line becomes infeasible. Then we don't try to add more - */ + start with a short line, add measures. At some point + the line becomes infeasible. Then we don't try to add more + */ int minimal_start_idx = -1; Column_x_positions minimal_sol; Column_x_positions backup_sol; - + Real minimal_demerits = infinity_f; for (int start_idx = break_idx; start_idx--;) { - Link_array line = all.slice (breaks[start_idx], breaks[break_idx]+1); - - line[0] = dynamic_cast (line[0])->find_prebroken_piece (RIGHT); - line.top () = dynamic_cast (line.top ())->find_prebroken_piece (LEFT); - + Link_array line = all.slice (breaks[start_idx], breaks[break_idx] + 1); + + line[0] = dynamic_cast (line[0])->find_prebroken_piece (RIGHT); + line.top () = dynamic_cast (line.top ())->find_prebroken_piece (LEFT); + Column_x_positions cp; cp.cols_ = line; Interval line_dims - = pscore_->paper_->line_dimensions_int (optimal_paths[start_idx].line_); - Simple_spacer * sp = generate_spacing_problem (line, line_dims); - bool last_line = break_idx == breaks.size ()-1; + = line_dimensions_int (pscore_->layout (), optimal_paths[start_idx].line_); + Simple_spacer_wrapper *sp = generate_spacing_problem (line, line_dims); + bool last_line = break_idx == breaks.size () - 1; bool ragged = ragged_right || (last_line && ragged_last); - + sp->solve (&cp, ragged); - + delete sp; if (ragged && last_line) cp.force_ = 0.0; - + if (fabs (cp.force_) > worst_force) worst_force = fabs (cp.force_); @@ -127,7 +133,7 @@ Gourlay_breaking::do_solve () const solution", in case everything fucks up. */ if (start_idx == break_idx - 1) backup_sol = cp; - + Real this_demerits; if (optimal_paths[start_idx].demerits_ >= infinity_f) @@ -136,7 +142,7 @@ Gourlay_breaking::do_solve () const this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp) + optimal_paths[start_idx].demerits_; - if (this_demerits < minimal_demerits) + if (this_demerits < minimal_demerits) { minimal_start_idx = start_idx; minimal_sol = cp; @@ -148,18 +154,17 @@ Gourlay_breaking::do_solve () const if we add more columns, so we get on with the next one */ if (!cp.satisfies_constraints_) - break ; + break; } - Break_node bnod; - if (minimal_start_idx < 0) + if (minimal_start_idx < 0) { bnod.demerits_ = infinity_f; bnod.line_config_ = backup_sol; - bnod.prev_break_ = break_idx - 1; + bnod.prev_break_ = break_idx - 1; } - else + else { bnod.prev_break_ = minimal_start_idx; bnod.demerits_ = minimal_demerits; @@ -167,14 +172,14 @@ Gourlay_breaking::do_solve () const } bnod.line_ = optimal_paths[bnod.prev_break_].line_ + 1; optimal_paths.push (bnod); - + if (! (break_idx % HAPPY_DOTS_I)) progress_indication (String ("[") + to_string (break_idx) + "]"); } /* do the last one */ if (breaks.size () % HAPPY_DOTS_I) - progress_indication (String ("[") + to_string (breaks.size ()) + "]"); + progress_indication (String ("[") + to_string (breaks.size ()) + "]"); progress_indication ("\n"); @@ -182,7 +187,7 @@ Gourlay_breaking::do_solve () const Array lines; /* skip 0-th element, since it is a "dummy" elt*/ - for (int i = optimal_paths.size ()-1; i> 0;) + for (int i = optimal_paths.size () - 1; i > 0;) { final_breaks.push (i); int prev = optimal_paths[i].prev_break_; @@ -190,53 +195,48 @@ Gourlay_breaking::do_solve () const i = prev; } - if (verbose_global_b) + if (be_verbose_global) { - progress_indication (_f ("Optimal demerits: %f", - optimal_paths.top ().demerits_) + "\n"); + message (_f ("Optimal demerits: %f", + optimal_paths.top ().demerits_) + "\n"); } - + if (optimal_paths.top ().demerits_ >= infinity_f) - warning (_ ("No feasible line breaking found")); - - for (int i= final_breaks.size (); i--;) + warning (_ ("no feasible line breaking found")); + + for (int i = final_breaks.size (); i--;) { Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_); - + lines.push (cp); if (!cp.satisfies_constraints_) - warning ("Could not find line breaking that satisfies constraints."); + warning (_ ("can't find line breaking that satisfies constraints")); } return lines; } - Gourlay_breaking::Gourlay_breaking () { } - - /* TODO: uniformity parameter to control rel. importance of spacing differences. TODO: mixing break penalties and constraint-failing solutions is confusing. - */ +*/ Real Gourlay_breaking::combine_demerits (Column_x_positions const &prev, Column_x_positions const &this_one) const { Real break_penalties = 0.0; - Grob * pc = this_one.cols_.top (); - if (pc->original_) + Grob *pc = this_one.cols_.top (); + if (pc->original ()) { SCM pen = pc->get_property ("penalty"); - if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000) - { - break_penalties += gh_scm2double (pen); - } + if (scm_is_number (pen) && fabs (scm_to_double (pen)) < 10000) + break_penalties += scm_to_double (pen); } /* @@ -248,25 +248,25 @@ Gourlay_breaking::combine_demerits (Column_x_positions const &prev, becoming denser, the uniformity requirement makes lines go from cramped to even more cramped (because going from cramped 3meas/line to relatively loose 2meas/line is such a big step. - - */ - Real demerit = abs (this_one.force_) + abs (prev.force_ - this_one.force_) + */ + + Real demerit = abs (this_one.force_) + abs (prev.force_ - this_one.force_) + break_penalties; - + if (!this_one.satisfies_constraints_) - { - /* - If it doesn't satisfy constraints, we make this one - really unattractive. - - add 20000 to the demerits, so that a break penalty - of -10000 won't change the result */ - demerit = (demerit + 20000) >? 2000; - - demerit *= 10; - } - - return demerit; + { + /* + If it doesn't satisfy constraints, we make this one + really unattractive. + + add 20000 to the demerits, so that a break penalty + of -10000 won't change the result */ + demerit = max ((demerit + 20000), 2000.0); + + demerit *= 10; + } + + return demerit; }