- Element & front = elements_.front();
- if (!(front.type == STRING
- && (elements_.size() == 1
- || (elements_.size() == 2
- && back().type == VIRTUAL))))
- return;
-
- // If this is a string element, we can try to split it.
- if (front.type != STRING)
- return;
- double xstr = w - x;
- // If there is a paragraph marker, it should be taken in account
- if (elements_.size() == 2)
- xstr -= back().width();
- //FIXME: use FontMetrics::x2pos here?? handle rtl?
- pos_type new_pos = front.x2pos(xstr);
- front.str = front.str.substr(0, new_pos - pos_);
- front.dim.wid = xstr;
- front.endpos = new_pos;
- end_ = new_pos;
- dim_.wid = x + xstr;
- // If there is a paragraph marker, it should be removed.
- if (elements_.size() == 2)
- elements_.pop_back();
- return;
+ 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 end of the row, since 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 true;
+ }
+ }
+
+ if (cit != beg && cit->type == VIRTUAL) {
+ // It is not possible to separate a virtual element from the
+ // previous one.
+ --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;
+ elements_.erase(cit, end);
+ return true;