+ 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"));
+ system_system_space_ = 0;
+ system_markup_space_ = 0;
+ system_system_padding_ = 0;
+ system_system_min_distance_ = 0;
+ score_system_padding_ = 0;
+ score_system_min_distance_ = 0;
+ score_markup_padding_ = 0;
+ score_markup_min_distance_ = 0;
+
+ Output_def *l = pscore_->layout ();
+
+ SCM spacing_spec = l->c_variable ("system-system-spacing");
+ SCM between_scores_spec = l->c_variable ("score-system-spacing");
+ SCM title_spec = l->c_variable ("score-markup-spacing");
+ SCM page_breaking_spacing_spec = l->c_variable ("page-breaking-system-system-spacing");
+
+ Page_layout_problem::read_spacing_spec (spacing_spec,
+ &system_system_space_,
+ ly_symbol2scm ("basic-distance"));
+ Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+ &system_system_space_,
+ ly_symbol2scm ("basic-distance"));
+ Page_layout_problem::read_spacing_spec (title_spec,
+ &system_markup_space_,
+ ly_symbol2scm ("basic-distance"));
+
+ Page_layout_problem::read_spacing_spec (spacing_spec,
+ &system_system_padding_,
+ ly_symbol2scm ("padding"));
+ Page_layout_problem::read_spacing_spec (between_scores_spec,
+ &score_system_padding_,
+ ly_symbol2scm ("padding"));
+ Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+ &system_system_padding_,
+ ly_symbol2scm ("padding"));
+ Page_layout_problem::read_spacing_spec (title_spec,
+ &score_markup_padding_,
+ ly_symbol2scm ("padding"));
+
+ Page_layout_problem::read_spacing_spec (between_scores_spec,
+ &score_system_min_distance_,
+ ly_symbol2scm ("minimum-distance"));
+ Page_layout_problem::read_spacing_spec (spacing_spec,
+ &system_system_min_distance_,
+ ly_symbol2scm ("minimum-distance"));
+ Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
+ &system_system_min_distance_,
+ ly_symbol2scm ("minimum-distance"));
+ Page_layout_problem::read_spacing_spec (title_spec,
+ &score_markup_min_distance_,
+ ly_symbol2scm ("minimum-distance"));
+
+ 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_->get_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 begin_of_line_extent = sys->begin_of_line_pure_height (start_rank, end_rank);
+ Interval rest_of_line_extent = sys->rest_of_line_pure_height (start_rank, end_rank);
+ bool last = (end == breaks_.size () - 1);
+
+ 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_);
+
+ begin_of_line_extent = (begin_of_line_extent.is_empty ()
+ || isnan (begin_of_line_extent[LEFT])
+ || isnan (begin_of_line_extent[RIGHT]))
+ ? Interval (0, 0) : begin_of_line_extent;
+ rest_of_line_extent = (rest_of_line_extent.is_empty ()
+ || isnan (rest_of_line_extent[LEFT])
+ || isnan (rest_of_line_extent[RIGHT]))
+ ? Interval (0, 0) : rest_of_line_extent;
+ out->shape_ = Line_shape (begin_of_line_extent, rest_of_line_extent);
+ out->padding_ = last ? score_system_padding_ : system_system_padding_;
+ out->title_padding_ = score_markup_padding_;
+ out->min_distance_ = last ? score_system_min_distance_ : system_system_min_distance_;
+ out->title_min_distance_ = score_markup_min_distance_;
+ out->space_ = system_system_space_;
+ out->title_space_ = system_markup_space_;
+ out->inverse_hooke_ = out->full_height () + system_system_space_;
+
+ out->footnotes_ = sys->get_footnotes_in_range (start_rank, end_rank);
+
+ out->refpoint_extent_ = sys->pure_refpoint_extent (start_rank, end_rank);
+ if (out->refpoint_extent_.is_empty ())
+ out->refpoint_extent_ = Interval (0, 0);