From 0c28eb92daf07d4562f7a0e5f1ed6165868429d9 Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Mon, 2 Oct 2006 08:17:49 +0000 Subject: [PATCH] * Documentation/user/page.itely (Page formatting): document auto-first-page-number * lily/page-breaking.cc (find_chunks_and_breaks): * lily/paper-score.cc (calc_breaking): Follow changes to the Constrained_breaking interface * lily/constrained-breaking.cc (calc_subproblem): run the main loop here backwards, as an optimisation (initialize): new function; move most of the code in resize () here (combine_demerits): cache ragged_right_ (Constrained_breaking): constructor now takes the Paper_score --- ChangeLog | 15 +++ Documentation/user/page.itely | 7 ++ lily/constrained-breaking.cc | 160 ++++++++++++++------------- lily/include/constrained-breaking.hh | 11 +- lily/page-breaking.cc | 5 +- lily/paper-score.cc | 7 +- 6 files changed, 119 insertions(+), 86 deletions(-) diff --git a/ChangeLog b/ChangeLog index c25ca8cd03..4bae291302 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2006-10-02 Joe Neeman + + * Documentation/user/page.itely (Page formatting): + document auto-first-page-number + + * lily/page-breaking.cc (find_chunks_and_breaks): + * lily/paper-score.cc (calc_breaking): Follow changes to the + Constrained_breaking interface + + * lily/constrained-breaking.cc (calc_subproblem): run the main loop + here backwards, as an optimisation + (initialize): new function; move most of the code in resize () here + (combine_demerits): cache ragged_right_ + (Constrained_breaking): constructor now takes the Paper_score + 2006-10-02 Erik Sandberg * lily/lexer.ll, lily/parser.yy: Add EXPECT_NO_MORE_ARGS token, to diff --git a/Documentation/user/page.itely b/Documentation/user/page.itely index 4aa6f228c2..c40a1da910 100644 --- a/Documentation/user/page.itely +++ b/Documentation/user/page.itely @@ -268,6 +268,13 @@ The relative importance of page (vertical) spacing and line (horizontal) spacing. High values will make page spacing more important. Default value is 1. +@funindex auto-first-page-number +@item auto-first-page-number +The page breaking algorithm is affected by the first page number being +odd or even. If this variable is set to #t, the page breaking algorithm +will decide whether to start with an odd or even number. This will +result in the first page number remaining as is or being increased by one. + @end table @end quotation diff --git a/lily/constrained-breaking.cc b/lily/constrained-breaking.cc index 4e5de38254..b8253ac4e9 100644 --- a/lily/constrained-breaking.cc +++ b/lily/constrained-breaking.cc @@ -76,12 +76,15 @@ Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk) vsize start_col = starting_breakpoints_[start]; Matrix &st = state_[start]; vsize max_index = brk - start_col; - for (vsize j=sys; j < max_index; j++) + for (vsize j=max_index; j-- > sys;) { if (0 == sys && j > 0) - break; /* the first line cannot have its first break after the beginning */ + continue; /* the first line cannot have its first break after the beginning */ Line_details const &cur = lines_.at (brk, j + start_col); + if (isinf (cur.force_)) + break; + Real prev_f = 0; Real prev_dem = 0; @@ -91,14 +94,11 @@ Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk) prev_dem = st.at (j, sys-1).demerits_; } if (isinf (prev_dem)) - break; - - Real dem = combine_demerits (cur.force_, prev_f) + prev_dem + cur.break_penalty_; - if (isinf (dem)) continue; + Real dem = combine_demerits (cur.force_, prev_f) + prev_dem + cur.break_penalty_; Constrained_break_node &n = st.at (max_index, sys); - if (isinf (n.demerits_) || dem < n.demerits_) + if (dem < n.demerits_) { found_something = true; n.demerits_ = dem; @@ -140,71 +140,6 @@ Constrained_breaking::resize (vsize systems) { systems_ = systems; - if (!breaks_.size () && pscore_) - { - Output_def *l = pscore_->layout (); - System *sys = pscore_->root_system (); - Real padding = robust_scm2double (l->c_variable ("between-system-padding"), 0); - Real space = robust_scm2double (l->c_variable ("ideal-system-space"), 0); - bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right")); - bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last")); - - Interval first_line = line_dimensions_int (pscore_->layout (), 0); - Interval other_lines = line_dimensions_int (pscore_->layout (), 1); - /* do all the rod/spring problems */ - breaks_ = pscore_->find_break_indices (); - all_ = pscore_->root_system ()->columns (); - lines_.resize (breaks_.size (), breaks_.size (), Line_details ()); - vector forces = get_line_forces (all_, - other_lines.length (), - other_lines.length () - first_line.length (), - ragged_right); - for (vsize i = 0; i < breaks_.size () - 1; i++) - { - Real max_ext = 0; - for (vsize j = i + 1; j < breaks_.size (); j++) - { - int start = Paper_column::get_rank (all_[breaks_[i]]); - int end = Paper_column::get_rank (all_[breaks_[j]]); - Interval extent = sys->pure_height (sys, start, end); - bool last = j == breaks_.size () - 1; - bool ragged = ragged_right || (last && ragged_last); - Line_details &line = lines_.at (j, i); - - line.force_ = forces[i*breaks_.size () + j]; - if (ragged && last && !isinf (line.force_)) - line.force_ = 0; - if (isinf (line.force_)) - break; - - Grob *c = all_[breaks_[j]]; - line.break_penalty_ = robust_scm2double (c->get_property ("line-break-penalty"), 0); - line.page_penalty_ = robust_scm2double (c->get_property ("page-break-penalty"), 0); - line.turn_penalty_ = robust_scm2double (c->get_property ("page-turn-penalty"), 0); - line.break_permission_ = c->get_property ("line-break-permission"); - line.page_permission_ = c->get_property ("page-break-permission"); - line.turn_permission_ = c->get_property ("page-turn-permission"); - - max_ext = max (max_ext, extent.length ()); - line.extent_ = extent; - line.padding_ = padding; - line.space_ = space; - line.inverse_hooke_ = 1; - } - } - - /* work out all the starting indices */ - for (vsize i = 0; i < start_.size (); i++) - { - vsize j; - for (j = 0; j < breaks_.size () - 1 && breaks_[j] < start_[i]; j++) - ; - starting_breakpoints_.push_back (j); - start_[i] = breaks_[j]; - } - state_.resize (start_.size ()); - } - if (pscore_ && systems_ > valid_systems_) { for (vsize i = 0; i < state_.size (); i++) @@ -359,22 +294,97 @@ Constrained_breaking::prepare_solution (vsize start, vsize end, vsize sys_count) return brk; } -Constrained_breaking::Constrained_breaking () +Constrained_breaking::Constrained_breaking (Paper_score *ps) { valid_systems_ = systems_ = 0; start_.push_back (0); + pscore_ = ps; + initialize (); } -Constrained_breaking::Constrained_breaking (vector const &start) +Constrained_breaking::Constrained_breaking (Paper_score *ps, vector const &start) : start_ (start) { valid_systems_ = systems_ = 0; + pscore_ = ps; + initialize (); +} + +/* find the forces for all possible lines and cache ragged_ and ragged_right_ */ +void +Constrained_breaking::initialize () +{ + if (!pscore_) + return; + + ragged_right_ = to_boolean (pscore_->layout ()->c_variable ("ragged-right")); + ragged_last_ = to_boolean (pscore_->layout ()->c_variable ("ragged-last")); + + Output_def *l = pscore_->layout (); + System *sys = pscore_->root_system (); + Real padding = robust_scm2double (l->c_variable ("between-system-padding"), 0); + Real space = robust_scm2double (l->c_variable ("ideal-system-space"), 0); + + Interval first_line = line_dimensions_int (pscore_->layout (), 0); + Interval other_lines = line_dimensions_int (pscore_->layout (), 1); + /* do all the rod/spring problems */ + breaks_ = pscore_->find_break_indices (); + all_ = pscore_->root_system ()->columns (); + lines_.resize (breaks_.size (), breaks_.size (), Line_details ()); + vector forces = get_line_forces (all_, + other_lines.length (), + other_lines.length () - first_line.length (), + ragged_right_); + for (vsize i = 0; i < breaks_.size () - 1; i++) + { + Real max_ext = 0; + for (vsize j = i + 1; j < breaks_.size (); j++) + { + int start = Paper_column::get_rank (all_[breaks_[i]]); + int end = Paper_column::get_rank (all_[breaks_[j]]); + Interval extent = sys->pure_height (sys, start, end); + bool last = j == breaks_.size () - 1; + bool ragged = ragged_right_ || (last && ragged_last_); + Line_details &line = lines_.at (j, i); + + line.force_ = forces[i*breaks_.size () + j]; + if (ragged && last && !isinf (line.force_)) + line.force_ = 0; + if (isinf (line.force_)) + break; + + Grob *c = all_[breaks_[j]]; + line.break_penalty_ = robust_scm2double (c->get_property ("line-break-penalty"), 0); + line.page_penalty_ = robust_scm2double (c->get_property ("page-break-penalty"), 0); + line.turn_penalty_ = robust_scm2double (c->get_property ("page-turn-penalty"), 0); + line.break_permission_ = c->get_property ("line-break-permission"); + line.page_permission_ = c->get_property ("page-break-permission"); + line.turn_permission_ = c->get_property ("page-turn-permission"); + + max_ext = max (max_ext, extent.length ()); + line.extent_ = extent; + line.padding_ = padding; + line.space_ = space; + line.inverse_hooke_ = 1; + } + } + + /* work out all the starting indices */ + for (vsize i = 0; i < start_.size (); i++) + { + vsize j; + for (j = 0; j < breaks_.size () - 1 && breaks_[j] < start_[i]; j++) + ; + starting_breakpoints_.push_back (j); + start_[i] = breaks_[j]; + } + state_.resize (start_.size ()); } Real Constrained_breaking::combine_demerits (Real force, Real prev_force) { - if (to_boolean (pscore_->layout ()->c_variable ("ragged-right"))) + if (ragged_right_) return force * force; return force * force + (prev_force - force) * (prev_force - force); diff --git a/lily/include/constrained-breaking.hh b/lily/include/constrained-breaking.hh index b0c1f7891d..9fe3223f98 100644 --- a/lily/include/constrained-breaking.hh +++ b/lily/include/constrained-breaking.hh @@ -90,12 +90,12 @@ struct Constrained_break_node /* A dynamic programming solution to breaking scores into lines */ -class Constrained_breaking : public Break_algorithm +class Constrained_breaking { public: vector solve (); - Constrained_breaking (); - Constrained_breaking (vector const &start_col_posns); + Constrained_breaking (Paper_score *ps); + Constrained_breaking (Paper_score *ps, vector const &start_col_posns); vector get_solution (vsize start, vsize end, vsize sys_count); vector get_best_solution (vsize start, vsize end); @@ -106,8 +106,11 @@ public: void resize (vsize systems); private: + Paper_score *pscore_; vsize valid_systems_; vsize systems_; + bool ragged_right_; + bool ragged_last_; /* the (i,j)th entry is the configuration for breaking between columns i and j */ @@ -123,6 +126,8 @@ private: vector all_; vector breaks_; + void initialize (); + Column_x_positions space_line (vsize start_col, vsize end_col); vsize prepare_solution (vsize start, vsize end, vsize sys_count); diff --git a/lily/page-breaking.cc b/lily/page-breaking.cc index c431c1ea25..b43c302367 100644 --- a/lily/page-breaking.cc +++ b/lily/page-breaking.cc @@ -277,8 +277,7 @@ Page_breaking::find_chunks_and_breaks (Break_predicate is_break) if ((break_point || chunk_end) && !last) line_breaker_columns.push_back (j); } - line_breaking_.push_back (Constrained_breaking (line_breaker_columns)); - line_breaking_.back ().set_pscore (all_[i].pscore_); + line_breaking_.push_back (Constrained_breaking (all_[i].pscore_, line_breaker_columns)); } else { @@ -287,7 +286,7 @@ Page_breaking::find_chunks_and_breaks (Break_predicate is_break) breaks_.push_back (Break_position (i)); chunks_.push_back (Break_position (i)); - line_breaking_.push_back (Constrained_breaking ()); + line_breaking_.push_back (Constrained_breaking (NULL)); } } } diff --git a/lily/paper-score.cc b/lily/paper-score.cc index e7a5f51247..028fb37a14 100644 --- a/lily/paper-score.cc +++ b/lily/paper-score.cc @@ -99,7 +99,7 @@ Paper_score::get_columns () const vector Paper_score::calc_breaking () { - Constrained_breaking algorithm; + Constrained_breaking algorithm (this); vector sol; message (_ ("Calculating line breaks...") + " "); @@ -107,11 +107,8 @@ Paper_score::calc_breaking () int system_count = robust_scm2int (layout ()->c_variable ("system-count"), 0); if (system_count) algorithm.resize (system_count); - - algorithm.set_pscore (this); - sol = algorithm.solve (); - return sol; + return algorithm.solve (); } void -- 2.39.5