+ return dynamic_cast<System *> (system_grob);
+}
+
+Grob *
+System::get_vertical_alignment ()
+{
+ extract_grob_set (this, "elements", elts);
+ Grob *ret = 0;
+ for (vsize i = 0; i < elts.size (); i++)
+ if (Align_interface::has_interface (elts[i]))
+ {
+ if (ret)
+ programming_error ("found multiple vertical alignments in this system");
+ ret = elts[i];
+ }
+
+ if (!ret)
+ programming_error ("didn't find a vertical alignment in this system");
+ return ret;
+}
+
+// Finds the furthest staff in the given direction whose x-extent
+// overlaps with the given interval.
+Grob *
+System::get_extremal_staff (Direction dir, Interval const &iv)
+{
+ Grob *align = get_vertical_alignment ();
+ if (!align)
+ return 0;
+
+ extract_grob_set (align, "elements", elts);
+ vsize start = (dir == UP) ? 0 : elts.size () - 1;
+ vsize end = (dir == UP) ? elts.size () : VPOS;
+ for (vsize i = start; i != end; i += dir)
+ {
+ if (Hara_kiri_group_spanner::has_interface (elts[i]))
+ Hara_kiri_group_spanner::consider_suicide (elts[i]);
+
+ Interval intersection = elts[i]->extent (this, X_AXIS);
+ intersection.intersect (iv);
+ if (elts[i]->is_live () && !intersection.is_empty ())
+ return elts[i];
+ }
+ return 0;
+}
+
+Interval
+System::pure_refpoint_extent (vsize start, vsize end)
+{
+ Interval ret;
+ Grob *alignment = get_vertical_alignment ();
+ if (!alignment)
+ return Interval ();
+
+ extract_grob_set (alignment, "elements", staves);
+ vector<Real> offsets = Align_interface::get_pure_minimum_translations (alignment, staves, Y_AXIS, start, end);
+
+ for (vsize i = 0; i < offsets.size (); ++i)
+ if (Page_layout_problem::is_spaceable (staves[i]))
+ {
+ ret[UP] = offsets[i];
+ break;
+ }
+
+ for (vsize i = offsets.size (); i--;)
+ if (Page_layout_problem::is_spaceable (staves[i]))
+ {
+ ret[DOWN] = offsets[i];
+ break;
+ }
+
+ return ret;
+}
+
+Interval
+System::part_of_line_pure_height (vsize start, vsize end, bool begin)
+{
+ Grob *alignment = get_vertical_alignment ();
+ if (!alignment)
+ return Interval ();
+
+ extract_grob_set (alignment, "elements", staves);
+ vector<Real> offsets = Align_interface::get_pure_minimum_translations (alignment, staves, Y_AXIS, start, end);
+
+ Interval ret;
+ for (vsize i = 0; i < staves.size (); ++i)
+ {
+ Interval iv = begin
+ ? Axis_group_interface::begin_of_line_pure_height (staves[i], start)
+ : Axis_group_interface::rest_of_line_pure_height (staves[i], start, end);
+ if (i < offsets.size ())
+ iv.translate (offsets[i]);
+ ret.unite (iv);
+ }
+
+ Interval other_elements = begin
+ ? Axis_group_interface::begin_of_line_pure_height (this, start)
+ : Axis_group_interface::rest_of_line_pure_height (this, start, end);
+
+ ret.unite (other_elements);
+
+ return ret;
+}
+
+Interval
+System::begin_of_line_pure_height (vsize start, vsize end)
+{
+ return part_of_line_pure_height (start, end, true);
+}
+
+Interval
+System::rest_of_line_pure_height (vsize start, vsize end)
+{
+ return part_of_line_pure_height (start, end, false);
+}
+
+// This differs from Axis_group_interface::calc_pure_relevant_grobs
+// because here, we are only interested in those few elements that aren't
+// descended from VerticalAlignment (ie. things like RehearsalMark, BarLine).
+MAKE_SCHEME_CALLBACK (System, calc_pure_relevant_grobs, 1);
+SCM
+System::calc_pure_relevant_grobs (SCM smob)
+{
+ Grob *me = unsmob_grob (smob);
+
+ extract_grob_set (me, "elements", elts);
+ vector<Grob *> relevant_grobs;
+ SCM pure_relevant_p = ly_lily_module_constant ("pure-relevant?");
+
+ for (vsize i = 0; i < elts.size (); ++i)
+ {
+ if (!Axis_group_interface::has_interface (elts[i]))
+ {
+ if (to_boolean (scm_apply_1 (pure_relevant_p, elts[i]->self_scm (), SCM_EOL)))
+ relevant_grobs.push_back (elts[i]);
+
+ if (Item *it = dynamic_cast<Item*> (elts[i]))
+ {
+ Direction d = LEFT;
+ do
+ {
+ Item *piece = it->find_prebroken_piece (d);
+ if (piece && to_boolean (scm_apply_1 (pure_relevant_p, piece->self_scm (), SCM_EOL)))
+ relevant_grobs.push_back (piece);
+ }
+ while (flip (&d) != LEFT);
+ }
+ }
+ }
+
+ SCM grobs_scm = Grob_array::make_array ();
+
+ unsmob_grob_array (grobs_scm)->set_array (relevant_grobs);
+ return grobs_scm;
+}
+
+MAKE_SCHEME_CALLBACK (System, height, 1);
+SCM
+System::height (SCM smob)
+{
+ return Axis_group_interface::height (smob);
+}
+
+MAKE_SCHEME_CALLBACK (System, calc_pure_height, 3);
+SCM
+System::calc_pure_height (SCM smob, SCM start_scm, SCM end_scm)
+{
+ System *me = dynamic_cast<System *> (unsmob_grob (smob));
+ int start = scm_to_int (start_scm);
+ int end = scm_to_int (end_scm);
+
+ Interval begin = me->begin_of_line_pure_height (start, end);
+ Interval rest = me->rest_of_line_pure_height (start, end);
+ begin.unite (rest);
+
+ return ly_interval2scm (begin);
+}
+
+Grob*
+System::get_pure_bound (Direction d, int start, int end)
+{
+ vector<vsize> ranks = pscore_->get_break_ranks ();
+ vector<vsize> indices = pscore_->get_break_indices ();
+ vector<Grob*> cols = pscore_->get_columns ();
+
+ vsize target_rank = (d == LEFT ? start : end);
+ vector<vsize>::const_iterator i =
+ lower_bound (ranks.begin (), ranks.end (), target_rank, std::less<vsize> ());
+
+ if (i != ranks.end () && (*i) == target_rank)
+ return cols[indices[i - ranks.begin ()]];
+ else
+ return 0;
+}
+
+Grob*
+System::get_maybe_pure_bound (Direction d, bool pure, int start, int end)
+{
+ return pure ? get_pure_bound (d, start, end) : get_bound (d);