+
+ // Iterate backwards over breakable elements and try to break them
+ Elements::iterator cit_brk = cit;
+ int wid_brk = wid + cit_brk->dim.wid;
+ ++cit_brk;
+ Elements tail;
+ while (cit_brk != beg) {
+ --cit_brk;
+ // make a copy of the element to work on it.
+ Element brk = *cit_brk;
+ /* If the current element is an inset that allows breaking row
+ * after itself, and if the row is already short enough after
+ * this element, then cut right after it.
+ */
+ if (wid_brk <= w && brk.row_flags & CanBreakAfter) {
+ end_ = brk.endpos;
+ dim_.wid = wid_brk;
+ moveElements(elements_, cit_brk + 1, tail);
+ return tail;
+ }
+ // assume now that the current element is not there
+ wid_brk -= brk.dim.wid;
+ /* If the current element is an inset that allows breaking row
+ * before itself, and if the row is already short enough before
+ * this element, then cut right before it.
+ */
+ if (wid_brk <= w && brk.row_flags & CanBreakBefore && cit_brk != beg) {
+ end_ = (cit_brk -1)->endpos;
+ dim_.wid = wid_brk;
+ moveElements(elements_, cit_brk, tail);
+ return tail;
+ }
+ /* We have found a suitable separable element. This is the common case.
+ * Try to break it cleanly at a length that is both
+ * - less than the available space on the row
+ * - shorter than the natural width of the element, in order to enforce
+ * break-up.
+ */
+ if (brk.splitAt(min(w - wid_brk, brk.dim.wid - 2), next_width, false, tail)) {
+ /* if this element originally did not cause a row overflow
+ * in itself, and the remainder of the row would still be
+ * too large after breaking, then we will have issues in
+ * next row. Thus breaking does not help.
+ */
+ if (wid_brk + cit_brk->dim.wid < w
+ && dim_.wid - (wid_brk + brk.dim.wid) >= next_width) {
+ tail.clear();
+ break;
+ }
+ end_ = brk.endpos;
+ *cit_brk = brk;
+ dim_.wid = wid_brk + brk.dim.wid;
+ // If there are other elements, they should be removed.
+ moveElements(elements_, cit_brk + 1, tail);
+ return tail;
+ }
+ LATTEST(tail.empty());
+ }
+
+ if (cit != beg && cit->row_flags & NoBreakBefore) {
+ // It is not possible to separate this element from the
+ // previous one. (e.g. VIRTUAL)
+ --cit;
+ wid -= cit->dim.wid;
+ }
+
+ if (cit != beg) {
+ // There is no usable separator, but several elements have
+ // been added. We can cut right here.
+ end_ = cit->pos;
+ dim_.wid = wid;
+ moveElements(elements_, cit, tail);
+ return tail;
+ }
+
+ /* If we are here, it means that we have not found a separator to
+ * shorten the row. Let's try to break it again, but force
+ * splitting this time.
+ */
+ if (cit->splitAt(w - wid, next_width, true, tail)) {
+ end_ = cit->endpos;
+ dim_.wid = wid + cit->dim.wid;
+ // If there are other elements, they should be removed.
+ moveElements(elements_, cit + 1, tail);
+ return tail;
+ }
+
+ // cit == beg; remove all elements after the first one.
+ moveElements(elements_, cit + 1, tail);
+ return tail;
+}
+
+
+void Row::reverseRTL()
+{
+ pos_type i = 0;
+ pos_type const end = elements_.size();
+ while (i < end) {
+ // gather a sequence of elements with the same direction
+ bool const rtl = elements_[i].isRTL();
+ pos_type j = i;
+ while (j < end && elements_[j].isRTL() == rtl)
+ ++j;
+ // if the direction is not the same as the paragraph
+ // direction, the sequence has to be reverted.
+ if (rtl != rtl_)
+ reverse(elements_.begin() + i, elements_.begin() + j);
+ i = j;
+ }
+ // If the paragraph itself is RTL, reverse everything
+ if (rtl_)
+ reverse(elements_.begin(), elements_.end());
+}
+
+Row::const_iterator const
+Row::findElement(pos_type const pos, bool const boundary, double & x) const
+{
+ /**
+ * When boundary is true, position i is in the row element (pos, endpos)
+ * if
+ * pos < i <= endpos
+ * whereas, when boundary is false, the test is
+ * pos <= i < endpos
+ * The correction below allows to handle both cases.
+ */
+ int const boundary_corr = (boundary && pos) ? -1 : 0;
+
+ x = left_margin;
+
+ /** Early return in trivial cases
+ * 1) the row is empty
+ * 2) the position is the left-most position of the row; there
+ * is a quirk here however: if the first element is virtual
+ * (end-of-par marker for example), then we have to look
+ * closer
+ */
+ if (empty()
+ || (pos == begin()->left_pos() && !boundary
+ && !begin()->isVirtual()))
+ return begin();
+
+ const_iterator cit = begin();
+ for ( ; cit != end() ; ++cit) {
+ /** Look whether the cursor is inside the element's span. Note
+ * that it is necessary to take the boundary into account, and
+ * to accept virtual elements, in which case the position
+ * will be before the virtual element.
+ */
+ if (cit->isVirtual() && pos + boundary_corr == cit->pos)
+ break;
+ else if (pos + boundary_corr >= cit->pos
+ && pos + boundary_corr < cit->endpos) {
+ x += cit->pos2x(pos);
+ break;
+ }
+ x += cit->full_width();
+ }
+
+ if (cit == end())
+ --cit;
+
+ return cit;