+ pscore_ = ps;
+ initialize ();
+}
+
+static SCM
+min_permission (SCM perm1, SCM perm2)
+{
+ if (perm1 == ly_symbol2scm ("force"))
+ return perm2;
+ if (perm1 == ly_symbol2scm ("allow")
+ && perm2 != ly_symbol2scm ("force"))
+ return perm2;
+ return SCM_EOL;
+}
+
+/* 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"));
+ /* NOTE: currently, we aren't using the space_ field of a
+ Line_details for anything. That's because the approximations
+ used for scoring a page configuration don't actually space things
+ properly (for speed reasong) using springs anchored at the staff
+ refpoints. Rather, the "space" is placed between the extent
+ boxes. To get a good result, therefore, the "space" value for
+ page breaking needs to be much smaller than the "space" value for
+ page layout. Currently, we just make it zero always.
+ */
+ between_system_space_ = 0;
+ between_system_padding_ = 0;
+ before_title_padding_ = 0;
+
+ Output_def *l = pscore_->layout ();
+
+ SCM spacing_spec = l->c_variable ("between-system-spacing");
+ SCM title_spec = l->c_variable ("before-title-spacing");
+ SCM page_breaking_spacing_spec = l->c_variable ("page-breaking-between-system-spacing");
+ Page_layout_problem::read_spacing_spec (spacing_spec,
+ &between_system_padding_,
+ ly_symbol2scm ("padding"));
+ Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+ &between_system_padding_,
+ ly_symbol2scm ("padding"));
+ Page_layout_problem::read_spacing_spec (title_spec,
+ &before_title_padding_,
+ ly_symbol2scm ("padding"));
+
+ 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 ()->used_columns ();
+ lines_.resize (breaks_.size (), breaks_.size (), Line_details ());
+ vector<Real> forces = get_line_forces (all_,
+ other_lines.length (),
+ other_lines.length () - first_line.length (),
+ ragged_right_);
+ for (vsize i = 0; i + 1 < breaks_.size (); i++)
+ {
+ for (vsize j = i + 1; j < breaks_.size (); j++)
+ {
+ 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_ = (line.force_ < 0 && j > i + 1) ? infinity_f : 0;
+ if (isinf (line.force_))
+ break;
+
+ fill_line_details (&line, i, j);
+ }
+ }
+
+ /* work out all the starting indices */
+ for (vsize i = 0; i < start_.size (); i++)
+ {
+ vsize j;
+ for (j = 0; j + 1 < breaks_.size () && breaks_[j] < start_[i]; j++)
+ ;
+ starting_breakpoints_.push_back (j);
+ start_[i] = breaks_[j];
+ }
+ state_.resize (start_.size ());
+}
+
+/*
+ Fills out all of the information contained in a Line_details,
+ except for information about horizontal spacing.
+*/
+void
+Constrained_breaking::fill_line_details (Line_details *const out, vsize start, vsize end)
+{
+ int start_rank = Paper_column::get_rank (all_[breaks_[start]]);
+ int end_rank = Paper_column::get_rank (all_[breaks_[end]]);
+ System *sys = pscore_->root_system ();
+ Interval extent = sys->pure_height (sys, start_rank, end_rank);
+
+ Grob *c = all_[breaks_[end]];
+ out->last_column_ = c;
+ out->break_penalty_ = robust_scm2double (c->get_property ("line-break-penalty"), 0);
+ out->page_penalty_ = robust_scm2double (c->get_property ("page-break-penalty"), 0);
+ out->turn_penalty_ = robust_scm2double (c->get_property ("page-turn-penalty"), 0);
+ out->break_permission_ = c->get_property ("line-break-permission");
+ out->page_permission_ = c->get_property ("page-break-permission");
+ out->turn_permission_ = c->get_property ("page-turn-permission");
+
+ /* turn permission should always be stricter than page permission
+ and page permission should always be stricter than line permission */
+ out->page_permission_ = min_permission (out->break_permission_,
+ out->page_permission_);
+ out->turn_permission_ = min_permission (out->page_permission_,
+ out->turn_permission_);
+
+ // TODO: see the hack regarding begin_of_line and
+ // rest_of_line extents in align-interface. Perhaps we
+ // should do the same thing here so that the effect extends
+ // between systems as well as within systems. It isn't as
+ // crucial here, however, because the effect is largest when
+ // dealing with large systems.
+ out->extent_ = (extent.is_empty ()
+ || isnan (extent[LEFT])
+ || isnan (extent[RIGHT]))
+ ? Interval (0, 0) : extent;
+ out->padding_ = between_system_padding_;
+ out->title_padding_ = before_title_padding_;
+ out->space_ = between_system_space_;
+ out->inverse_hooke_ = extent.length () + between_system_space_;