+#include "pointer-group-interface.hh"
+#include "libc-extension.hh"
+#include "paper-column.hh"
+#include "paper-column.hh"
+#include "paper-score.hh"
+#include "stencil.hh"
+#include "system.hh"
+#include "warn.hh"
+
+Grob *
+Spanner::clone (int count) const
+{
+ return new Spanner (*this, count);
+}
+
+void
+Spanner::do_break_processing ()
+{
+ //break_into_pieces
+ Item *left = spanned_drul_[LEFT];
+ Item *right = spanned_drul_[RIGHT];
+
+ if (!left || !right)
+ return;
+
+ /*
+ Check if our parent in X-direction spans equally wide
+ or wider than we do.
+ */
+ for (int a = X_AXIS; a < NO_AXES; a++)
+ {
+ if (Spanner *parent = dynamic_cast<Spanner *> (get_parent ((Axis)a)))
+ {
+ if (!parent->spanned_rank_iv ().superset (this->spanned_rank_iv ()))
+ {
+ programming_error (to_string ("Spanner `%s' is not fully contained in parent spanner `%s'.",
+ name ().c_str (),
+ parent->name ().c_str ()));
+ }
+ }
+ }
+
+ if (get_system () || is_broken ())
+ return;
+
+ if (left == right)
+ {
+ /*
+ If we have a spanner spanning one column, we must break it
+ anyway because it might provide a parent for another item. */
+ Direction d = LEFT;
+ do
+ {
+ Item *bound = left->find_prebroken_piece (d);
+ if (!bound)
+ programming_error ("no broken bound");
+ else if (bound->get_system ())
+ {
+ Spanner *span = dynamic_cast<Spanner *> (clone (broken_intos_.size ()));
+ span->set_bound (LEFT, bound);
+ span->set_bound (RIGHT, bound);
+
+ assert (span->get_system ());
+ span->get_system ()->typeset_grob (span);
+ broken_intos_.push_back (span);
+ }
+ }
+ while ((flip (&d)) != LEFT);
+ }
+ else
+ {
+ System *root = get_root_system (this);
+ vector<Item*> break_points = root->broken_col_range (left, right);
+
+ break_points.insert (break_points.begin () + 0, left);
+ break_points.push_back (right);
+
+ for (vsize i = 1; i < break_points.size (); i++)
+ {
+ Drul_array<Item *> bounds;
+ bounds[LEFT] = break_points[i - 1];
+ bounds[RIGHT] = break_points[i];
+ Direction d = LEFT;
+ do
+ {
+ if (!bounds[d]->get_system ())
+ bounds[d] = bounds[d]->find_prebroken_piece (- d);
+ }
+ while ((flip (&d)) != LEFT);
+
+ if (!bounds[LEFT] || ! bounds[RIGHT])
+ {
+ programming_error ("bounds of this piece aren't breakable. ");
+ continue;
+ }
+
+ Spanner *span = dynamic_cast<Spanner *> (clone (broken_intos_.size ()));
+ span->set_bound (LEFT, bounds[LEFT]);
+ span->set_bound (RIGHT, bounds[RIGHT]);
+
+ if (!bounds[LEFT]->get_system ()
+
+ || !bounds[RIGHT]->get_system ()
+ || bounds[LEFT]->get_system () != bounds[RIGHT]->get_system ())
+ {
+ programming_error ("bounds of spanner are invalid");
+ span->suicide ();
+ }
+ else
+ {
+ bounds[LEFT]->get_system ()->typeset_grob (span);
+ broken_intos_.push_back (span);
+ }
+ }
+ }
+ vector_sort (broken_intos_, Spanner::compare);
+ for (vsize i = broken_intos_.size (); i--;)
+ broken_intos_[i]->break_index_ = i;
+}
+
+vsize
+Spanner::get_break_index () const
+{
+ return break_index_;
+}
+
+void
+Spanner::set_my_columns ()
+{
+ Direction i = (Direction) LEFT;
+ do
+ {
+ if (!spanned_drul_[i]->get_system ())
+ set_bound (i, spanned_drul_[i]->find_prebroken_piece ((Direction) -i));
+ }
+ while (flip (&i) != LEFT);
+}
+
+Interval_t<int>
+Spanner::spanned_rank_iv ()
+{
+ Interval_t<int> iv (0, 0);
+
+ if (spanned_drul_[LEFT] && spanned_drul_[LEFT]->get_column ())
+ iv[LEFT] = spanned_drul_[LEFT]->get_column ()->get_rank ();
+ if (spanned_drul_[RIGHT] && spanned_drul_[RIGHT]->get_column ())
+ iv[RIGHT] = spanned_drul_[RIGHT]->get_column ()->get_rank ();
+ return iv;
+}
+
+Item *
+Spanner::get_bound (Direction d) const
+{
+ return spanned_drul_ [d];
+}