+ // 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;
+ while (cit_brk != beg) {
+ --cit_brk;
+ // make a copy of the element to work on it.
+ Element brk = *cit_brk;
+ wid_brk -= brk.dim.wid;
+ if (brk.countSeparators() == 0 || brk.pos < keep)
+ continue;
+ /* We have found a suitable separable element. This is the common case.
+ * Try to break it cleanly (at word boundary) 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.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
+ /* 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) >= w) {
+ break;
+ }
+ end_ = brk.endpos;
+ /* after breakAt, there may be spaces at the end of the
+ * string, but they are not counted in the string length
+ * (QTextLayout feature, actually). We remove them, but do
+ * not change the endo of the row, since the spaces at row
+ * break are invisible.
+ */
+ brk.str = rtrim(brk.str);
+ brk.endpos = brk.pos + brk.str.length();
+ *cit_brk = brk;
+ dim_.wid = wid_brk + brk.dim.wid;
+ // If there are other elements, they should be removed.
+ elements_.erase(cit_brk + 1, end);
+ return;
+ }
+ }
+