3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
10 * \author Jean-Marc Lasgouttes
12 * Full author contact details are available in file CREDITS.
14 * Metrics for an on-screen text row.
21 #include "DocIterator.h"
24 #include "frontends/FontMetrics.h"
26 #include "support/debug.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
39 using frontend::FontMetrics;
42 // Maximum length that a space can be stretched when justifying text
43 static double const MAX_SPACE_STRETCH = 1.5; //em
46 int Row::Element::countSeparators() const
50 return count(str.begin(), str.end(), ' ');
54 int Row::Element::countExpanders() const
58 return theFontMetrics(font).countExpanders(str);
62 int Row::Element::expansionAmount() const
66 return countExpanders() * theFontMetrics(font).em();
70 void Row::Element::setExtra(double extra_per_em)
74 extra = extra_per_em * theFontMetrics(font).em();
78 double Row::Element::pos2x(pos_type const i) const
80 // This can happen with inline completion when clicking on the
81 // row after the completion.
82 if (i < pos || i > endpos)
86 //handle first the two bounds of the element
87 if (i == endpos && type != VIRTUAL)
88 w = isRTL() ? 0 : full_width();
89 else if (i == pos || type != STRING)
90 w = isRTL() ? full_width() : 0;
92 FontMetrics const & fm = theFontMetrics(font);
93 w = fm.pos2x(str, i - pos, isRTL(), extra);
100 pos_type Row::Element::x2pos(int &x) const
102 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
107 FontMetrics const & fm = theFontMetrics(font);
108 i = fm.x2pos(str, x, isRTL(), extra);
112 // those elements are actually empty (but they have a width)
114 x = isRTL() ? int(full_width()) : 0;
118 // those elements contain only one position. Round to
120 if (x > (full_width() + 1) / 2) {
121 x = int(full_width());
128 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
133 bool Row::Element::breakAt(int w, bool force)
135 if (type != STRING || dim.wid <= w)
138 FontMetrics const & fm = theFontMetrics(font);
140 if(fm.breakAt(str, x, isRTL(), force)) {
142 endpos = pos + str.length();
143 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
151 pos_type Row::Element::left_pos() const
153 return isRTL() ? endpos : pos;
157 pos_type Row::Element::right_pos() const
159 return isRTL() ? pos : endpos;
164 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
165 sel_beg(-1), sel_end(-1),
166 begin_margin_sel(false), end_margin_sel(false),
168 pit_(0), pos_(0), end_(0),
169 right_boundary_(false), flushed_(false), rtl_(false),
174 bool Row::isMarginSelected(bool left, DocIterator const & beg,
175 DocIterator const & end) const
177 pos_type const sel_pos = left ? sel_beg : sel_end;
178 pos_type const margin_pos = left ? pos_ : end_;
180 // Is there a selection and is the chosen margin selected ?
181 if (!selection() || sel_pos != margin_pos)
183 else if (beg.pos() == end.pos())
184 // This is a special case in which the space between after
185 // pos i-1 and before pos i is selected, i.e. the margins
186 // (see DocIterator::boundary_).
187 return beg.boundary() && !end.boundary();
188 else if (end.pos() == margin_pos)
189 // If the selection ends around the margin, it is only
190 // drawn if the cursor is after the margin.
191 return !end.boundary();
192 else if (beg.pos() == margin_pos)
193 // If the selection begins around the margin, it is
194 // only drawn if the cursor is before the margin.
195 return beg.boundary();
201 void Row::setSelectionAndMargins(DocIterator const & beg,
202 DocIterator const & end) const
204 setSelection(beg.pos(), end.pos());
206 change(end_margin_sel, isMarginSelected(false, beg, end));
207 change(begin_margin_sel, isMarginSelected(true, beg, end));
211 void Row::clearSelectionAndMargins() const
215 change(end_margin_sel, false);
216 change(begin_margin_sel, false);
220 void Row::setSelection(pos_type beg, pos_type end) const
222 if (pos_ >= beg && pos_ <= end)
223 change(sel_beg, pos_);
224 else if (beg > pos_ && beg <= end_)
225 change(sel_beg, beg);
229 if (end_ >= beg && end_ <= end)
230 change(sel_end,end_);
231 else if (end < end_ && end >= pos_)
232 change(sel_end, end);
238 bool Row::selection() const
240 return sel_beg != -1 && sel_end != -1;
244 ostream & operator<<(ostream & os, Row::Element const & e)
247 os << e.endpos << "<<" << e.pos << " ";
249 os << e.pos << ">>" << e.endpos << " ";
253 os << "STRING: `" << to_utf8(e.str) << "' ("
254 << e.countExpanders() << " expanders.), ";
257 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
260 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
266 os << "width=" << e.full_width();
271 ostream & operator<<(ostream & os, Row const & row)
273 os << " pos: " << row.pos_ << " end: " << row.end_
274 << " left_margin: " << row.left_margin
275 << " width: " << row.dim_.wid
276 << " right_margin: " << row.right_margin
277 << " ascent: " << row.dim_.asc
278 << " descent: " << row.dim_.des
279 << " separator: " << row.separator
280 << " label_hfill: " << row.label_hfill
281 << " row_boundary: " << row.right_boundary() << "\n";
282 double x = row.left_margin;
283 Row::Elements::const_iterator it = row.elements_.begin();
284 for ( ; it != row.elements_.end() ; ++it) {
285 os << "x=" << x << " => " << *it << endl;
286 x += it->full_width();
292 int Row::left_x() const
294 double x = left_margin;
295 const_iterator const end = elements_.end();
296 const_iterator cit = elements_.begin();
297 while (cit != end && cit->isVirtual()) {
298 x += cit->full_width();
301 return support::iround(x);
305 int Row::right_x() const
308 const_iterator const begin = elements_.begin();
309 const_iterator cit = elements_.end();
310 while (cit != begin) {
312 if (cit->isVirtual())
313 x -= cit->full_width();
317 return support::iround(x);
321 int Row::countSeparators() const
324 const_iterator const end = elements_.end();
325 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
326 n += cit->countSeparators();
331 bool Row::setExtraWidth(int w)
334 // this is not expected to happen (but it does)
336 // amount of expansion: number of expanders time the em value for each
339 for (Element const & e : elements_)
340 exp_amount += e.expansionAmount();
343 // extra length per expander per em
344 double extra_per_em = double(w) / exp_amount;
345 if (extra_per_em > MAX_SPACE_STRETCH)
346 // do not stretch more than MAX_SPACE_STRETCH em per expander
348 // add extra length to each element proportionally to its em.
349 for (Element & e : elements_)
350 if (e.type == STRING)
351 e.setExtra(extra_per_em);
352 // update row dimension
358 bool Row::sameString(Font const & f, Change const & ch) const
360 if (elements_.empty())
362 Element const & elt = elements_.back();
363 return elt.type == STRING && !elt.final
364 && elt.font == f && elt.change == ch;
368 void Row::finalizeLast()
370 if (elements_.empty())
372 Element & elt = elements_.back();
376 if (elt.change.changed())
379 if (elt.type == STRING) {
380 dim_.wid -= elt.dim.wid;
381 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
382 dim_.wid += elt.dim.wid;
387 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
388 Font const & f, Change const & ch)
391 Element e(INSET, pos, f, ch);
394 elements_.push_back(e);
396 changebar_ |= ins->isChanged();
400 void Row::add(pos_type const pos, char_type const c,
401 Font const & f, Change const & ch)
403 if (!sameString(f, ch)) {
405 Element e(STRING, pos, f, ch);
406 elements_.push_back(e);
408 if (back().str.length() % 30 == 0) {
409 dim_.wid -= back().dim.wid;
411 back().endpos = pos + 1;
412 back().dim.wid = theFontMetrics(back().font).width(back().str);
413 dim_.wid += back().dim.wid;
416 back().endpos = pos + 1;
421 void Row::addVirtual(pos_type const pos, docstring const & s,
422 Font const & f, Change const & ch)
425 Element e(VIRTUAL, pos, f, ch);
427 e.dim.wid = theFontMetrics(f).width(s);
428 dim_.wid += e.dim.wid;
430 elements_.push_back(e);
435 void Row::addSpace(pos_type const pos, int const width,
436 Font const & f, Change const & ch)
439 Element e(SPACE, pos, f, ch);
441 elements_.push_back(e);
442 dim_.wid += e.dim.wid;
448 dim_.wid -= elements_.back().dim.wid;
449 elements_.pop_back();
453 bool Row::shortenIfNeeded(pos_type const keep, int const w, int const next_width)
455 if (empty() || width() <= w)
458 Elements::iterator const beg = elements_.begin();
459 Elements::iterator const end = elements_.end();
460 int wid = left_margin;
462 // Search for the first element that goes beyond right margin
463 Elements::iterator cit = beg;
464 for ( ; cit != end ; ++cit) {
465 if (wid + cit->dim.wid > w)
471 // This should not happen since the row is too long.
472 LYXERR0("Something is wrong cannot shorten row: " << *this);
476 // Iterate backwards over breakable elements and try to break them
477 Elements::iterator cit_brk = cit;
478 int wid_brk = wid + cit_brk->dim.wid;
480 while (cit_brk != beg) {
482 // make a copy of the element to work on it.
483 Element brk = *cit_brk;
484 /* If the current element is an inset that allows breaking row
485 * after itself, and it the row is already short enough after
486 * this inset, then cut right after this element.
488 if (wid_brk <= w && brk.type == INSET
489 && brk.inset->rowFlags() & Inset::CanBreakAfter) {
492 elements_.erase(cit_brk + 1, end);
495 // assume now that the current element is not there
496 wid_brk -= brk.dim.wid;
498 * Some Asian languages split lines anywhere (no notion of
499 * word). It seems that QTextLayout is not aware of this fact.
501 * https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
503 * FIXME: Something shall be done about characters which are
504 * not allowed at the beginning or end of line.
506 bool const word_wrap = brk.font.language()->wordWrap();
507 // When there is text before the body part (think description
508 // environment), do not try to break.
511 /* We have found a suitable separable element. This is the common case.
512 * Try to break it cleanly (at word boundary) at a length that is both
513 * - less than the available space on the row
514 * - shorter than the natural width of the element, in order to enforce
517 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), !word_wrap)) {
518 /* if this element originally did not cause a row overflow
519 * in itself, and the remainder of the row would still be
520 * too large after breaking, then we will have issues in
521 * next row. Thus breaking does not help.
523 if (wid_brk + cit_brk->dim.wid < w
524 && dim_.wid - (wid_brk + brk.dim.wid) >= next_width) {
528 /* after breakAt, there may be spaces at the end of the
529 * string, but they are not counted in the string length
530 * (QTextLayout feature, actually). We remove them, but do
531 * not change the end of the row, since spaces at row
532 * break are invisible.
534 brk.str = rtrim(brk.str);
535 brk.endpos = brk.pos + brk.str.length();
537 dim_.wid = wid_brk + brk.dim.wid;
538 // If there are other elements, they should be removed.
539 elements_.erase(cit_brk + 1, end);
544 if (cit != beg && cit->type == VIRTUAL) {
545 // It is not possible to separate a virtual element from the
552 // There is no usable separator, but several elements have
553 // been added. We can cut right here.
556 elements_.erase(cit, end);
560 /* If we are here, it means that we have not found a separator to
561 * shorten the row. Let's try to break it again, but not at word
562 * boundary this time.
564 if (cit->breakAt(w - wid, true)) {
566 // See comment above.
567 cit->str = rtrim(cit->str);
568 cit->endpos = cit->pos + cit->str.length();
569 dim_.wid = wid + cit->dim.wid;
570 // If there are other elements, they should be removed.
571 elements_.erase(next(cit, 1), end);
578 void Row::reverseRTL(bool const rtl_par)
581 pos_type const end = elements_.size();
583 // gather a sequence of elements with the same direction
584 bool const rtl = elements_[i].isRTL();
586 while (j < end && elements_[j].isRTL() == rtl)
588 // if the direction is not the same as the paragraph
589 // direction, the sequence has to be reverted.
591 reverse(elements_.begin() + i, elements_.begin() + j);
594 // If the paragraph itself is RTL, reverse everything
596 reverse(elements_.begin(), elements_.end());
600 Row::const_iterator const
601 Row::findElement(pos_type const pos, bool const boundary, double & x) const
604 * When boundary is true, position i is in the row element (pos, endpos)
607 * whereas, when boundary is false, the test is
609 * The correction below allows to handle both cases.
611 int const boundary_corr = (boundary && pos) ? -1 : 0;
615 /** Early return in trivial cases
616 * 1) the row is empty
617 * 2) the position is the left-most position of the row; there
618 * is a quirk here however: if the first element is virtual
619 * (end-of-par marker for example), then we have to look
623 || (pos == begin()->left_pos() && !boundary
624 && !begin()->isVirtual()))
627 const_iterator cit = begin();
628 for ( ; cit != end() ; ++cit) {
629 /** Look whether the cursor is inside the element's span. Note
630 * that it is necessary to take the boundary into account, and
631 * to accept virtual elements, in which case the position
632 * will be before the virtual element.
634 if (cit->isVirtual() && pos + boundary_corr == cit->pos)
636 else if (pos + boundary_corr >= cit->pos
637 && pos + boundary_corr < cit->endpos) {
638 x += cit->pos2x(pos);
641 x += cit->full_width();