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"
38 using frontend::FontMetrics;
41 // Maximum length that a space can be stretched when justifying text
42 static double const MAX_SPACE_STRETCH = 1.5; //em
45 int Row::Element::countExpanders() const
49 return support::countExpanders(str);
53 int Row::Element::expansionAmount() const
57 return countExpanders() * theFontMetrics(font).em();
61 void Row::Element::setExtra(double extra_per_em)
65 extra = extra_per_em * theFontMetrics(font).em();
69 double Row::Element::pos2x(pos_type const i) const
71 // This can happen with inline completion when clicking on the
72 // row after the completion.
73 if (i < pos || i > endpos)
77 //handle first the two bounds of the element
78 if (i == endpos && type != VIRTUAL)
79 w = isRTL() ? 0 : full_width();
80 else if (i == pos || type != STRING)
81 w = isRTL() ? full_width() : 0;
83 FontMetrics const & fm = theFontMetrics(font);
84 w = fm.pos2x(str, i - pos, isRTL(), extra);
91 pos_type Row::Element::x2pos(int &x) const
93 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
98 FontMetrics const & fm = theFontMetrics(font);
99 i = fm.x2pos(str, x, isRTL(), extra);
103 // those elements are actually empty (but they have a width)
105 x = isRTL() ? int(full_width()) : 0;
110 // those elements contain only one position. Round to
112 if (x > (full_width() + 1) / 2) {
113 x = int(full_width());
120 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
125 bool Row::Element::splitAt(int const width, int next_width, bool force,
126 Row::Elements & tail)
128 // Not a string or already OK.
129 if (type != STRING || (dim.wid > 0 && dim.wid < width))
132 FontMetrics const & fm = theFontMetrics(font);
134 // A a string that is not breakable
135 if (!(row_flags & CanBreakInside)) {
136 // has width been computed yet?
138 dim.wid = fm.width(str);
142 bool const wrap_any = !font.language()->wordWrap();
143 FontMetrics::Breaks breaks = fm.breakString(str, width, next_width,
144 isRTL(), wrap_any | force);
146 // if breaking did not really work, give up
147 if (!force && breaks.front().wid > width) {
149 dim.wid = fm.width(str);
153 Element first_e(STRING, pos, font, change);
154 // should next element eventually replace *this?
156 docstring::size_type i = 0;
157 for (FontMetrics::Break const & brk : breaks) {
158 /* For some reason breakString can decide to break before the
159 * first character (normally we use a 0-width nbsp to prevent
160 * that). Skip leading empty elements, they are never wanted.
162 if (first && brk.len == 0 && breaks.size() > 1)
164 Element e(STRING, pos + i, font, change);
165 e.str = str.substr(i, brk.len);
166 e.endpos = e.pos + brk.len;
168 e.nspc_wid = brk.nspc_wid;
169 e.row_flags = CanBreakInside | BreakAfter;
171 // this element eventually goes to *this
172 e.row_flags |= row_flags & ~AfterFlags;
181 // Avoid having a last empty element. This happens when
182 // breaking at the trailing space of string
183 if (tail.back().str.empty())
186 // Copy the after flags of the original element to the last one.
187 tail.back().row_flags &= ~BreakAfter;
188 tail.back().row_flags |= row_flags & AfterFlags;
190 // first_e row should be broken after the original element
191 first_e.row_flags |= BreakAfter;
193 // Restore the after flags of the original element.
194 first_e.row_flags &= ~BreakAfter;
195 first_e.row_flags |= row_flags & AfterFlags;
199 swap(first_e, *this);
204 void Row::Element::rtrim()
208 /* This is intended for strings that have been created by splitAt.
209 * They may have trailing spaces, but they are not counted in the
210 * string length (QTextLayout feature, actually). We remove them,
211 * and decrease endpos, since spaces at row break are invisible.
213 str = support::rtrim(str);
214 endpos = pos + str.length();
219 bool Row::isMarginSelected(bool left, DocIterator const & beg,
220 DocIterator const & end) const
222 pos_type const sel_pos = left ? sel_beg : sel_end;
223 pos_type const margin_pos = left ? pos_ : end_;
225 // Is there a selection and is the chosen margin selected ?
226 if (!selection() || sel_pos != margin_pos)
228 else if (beg.pos() == end.pos())
229 // This is a special case in which the space between after
230 // pos i-1 and before pos i is selected, i.e. the margins
231 // (see DocIterator::boundary_).
232 return beg.boundary() && !end.boundary();
233 else if (end.pos() == margin_pos)
234 // If the selection ends around the margin, it is only
235 // drawn if the cursor is after the margin.
236 return !end.boundary();
237 else if (beg.pos() == margin_pos)
238 // If the selection begins around the margin, it is
239 // only drawn if the cursor is before the margin.
240 return beg.boundary();
246 void Row::setSelectionAndMargins(DocIterator const & beg,
247 DocIterator const & end) const
249 setSelection(beg.pos(), end.pos());
251 change(end_margin_sel, isMarginSelected(false, beg, end));
252 change(begin_margin_sel, isMarginSelected(true, beg, end));
256 void Row::clearSelectionAndMargins() const
260 change(end_margin_sel, false);
261 change(begin_margin_sel, false);
265 void Row::setSelection(pos_type beg, pos_type end) const
267 if (pos_ >= beg && pos_ <= end)
268 change(sel_beg, pos_);
269 else if (beg > pos_ && beg <= end_)
270 change(sel_beg, beg);
274 if (end_ >= beg && end_ <= end)
275 change(sel_end,end_);
276 else if (end < end_ && end >= pos_)
277 change(sel_end, end);
283 bool Row::selection() const
285 return sel_beg != -1 && sel_end != -1;
289 ostream & operator<<(ostream & os, Row::Element const & e)
292 os << e.endpos << "<<" << e.pos << " ";
294 os << e.pos << ">>" << e.endpos << " ";
298 os << "STRING: `" << to_utf8(e.str) << "' ("
299 << e.countExpanders() << " expanders.), ";
302 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
305 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
310 case Row::MARGINSPACE:
311 os << "MARGINSPACE: ";
313 os << "width=" << e.full_width() << ", row_flags=" << e.row_flags;
318 ostream & operator<<(ostream & os, Row::Elements const & elts)
321 for (Row::Element const & e : elts) {
322 os << "x=" << x << " => " << e << endl;
329 ostream & operator<<(ostream & os, Row const & row)
331 os << " pos: " << row.pos_ << " end: " << row.end_
332 << " left_margin: " << row.left_margin
333 << " width: " << row.dim_.wid
334 << " right_margin: " << row.right_margin
335 << " ascent: " << row.dim_.asc
336 << " descent: " << row.dim_.des
337 << " separator: " << row.separator
338 << " label_hfill: " << row.label_hfill
339 << " right_boundary: " << row.right_boundary()
340 << " flushed: " << row.flushed() << "\n";
341 // We cannot use the operator above, unfortunately
342 double x = row.left_margin;
343 for (Row::Element const & e : row.elements_) {
344 os << "x=" << x << " => " << e << endl;
351 int Row::left_x() const
353 double x = left_margin;
354 const_iterator const end = elements_.end();
355 const_iterator cit = elements_.begin();
356 while (cit != end && cit->isVirtual()) {
357 x += cit->full_width();
360 return support::iround(x);
364 int Row::right_x() const
367 const_iterator const begin = elements_.begin();
368 const_iterator cit = elements_.end();
369 while (cit != begin) {
371 if (cit->isVirtual())
372 x -= cit->full_width();
376 return support::iround(x);
380 bool Row::setExtraWidth(int w)
383 // this is not expected to happen (but it does)
385 // amount of expansion: number of expanders time the em value for each
388 for (Element const & e : elements_)
389 exp_amount += e.expansionAmount();
392 // extra length per expander per em
393 double extra_per_em = double(w) / exp_amount;
394 if (extra_per_em > MAX_SPACE_STRETCH)
395 // do not stretch more than MAX_SPACE_STRETCH em per expander
397 // add extra length to each element proportionally to its em.
398 for (Element & e : elements_)
399 if (e.type == STRING)
400 e.setExtra(extra_per_em);
401 // update row dimension
407 bool Row::sameString(Font const & f, Change const & ch) const
409 if (elements_.empty())
411 Element const & elt = elements_.back();
412 return elt.type == STRING && !elt.final
413 && elt.font == f && elt.change == ch;
417 void Row::finalizeLast()
419 if (elements_.empty())
421 Element & elt = elements_.back();
425 if (elt.change.changed())
430 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
431 Font const & f, Change const & ch)
434 Element e(INSET, pos, f, ch);
437 e.row_flags = ins->rowFlags();
438 elements_.push_back(e);
440 changebar_ |= ins->isChanged();
444 void Row::add(pos_type const pos, char_type const c,
445 Font const & f, Change const & ch)
447 if (!sameString(f, ch)) {
449 Element e(STRING, pos, f, ch);
450 e.row_flags = CanBreakInside;
451 elements_.push_back(e);
454 back().endpos = pos + 1;
458 void Row::addVirtual(pos_type const pos, docstring const & s,
459 Font const & f, Change const & ch)
462 Element e(VIRTUAL, pos, f, ch);
464 e.dim.wid = theFontMetrics(f).width(s);
465 dim_.wid += e.dim.wid;
467 // Copy after* flags from previous elements, forbid break before element
468 int const prev_row_flags = elements_.empty() ? Inline : elements_.back().row_flags;
469 int const can_inherit = AfterFlags & ~AlwaysBreakAfter;
470 e.row_flags = (prev_row_flags & can_inherit) | NoBreakBefore;
471 elements_.push_back(e);
476 void Row::addSpace(pos_type const pos, int const width,
477 Font const & f, Change const & ch)
480 Element e(SPACE, pos, f, ch);
482 elements_.push_back(e);
483 dim_.wid += e.dim.wid;
487 void Row::addMarginSpace(pos_type const pos, int const width,
488 Font const & f, Change const & ch)
491 Element e(MARGINSPACE, pos, f, ch);
493 e.row_flags = NoBreakBefore;
494 elements_.push_back(e);
495 dim_.wid += e.dim.wid;
499 void Row::push_back(Row::Element const & e)
501 dim_.wid += e.dim.wid;
502 elements_.push_back(e);
508 dim_.wid -= elements_.back().dim.wid;
509 elements_.pop_back();
515 // Move stuff after \c it from \c from and the end of \c to.
516 void moveElements(Row::Elements & from, Row::Elements::iterator const & it,
519 to.insert(to.end(), it, from.end());
520 from.erase(it, from.end());
522 from.back().row_flags = (from.back().row_flags & ~AfterFlags) | AlwaysBreakAfter;
528 Row::Elements Row::shortenIfNeeded(int const w, int const next_width)
530 // FIXME: performance: if the last element is a string, we would
531 // like to avoid computing its length.
533 if (empty() || width() <= w)
536 Elements::iterator const beg = elements_.begin();
537 Elements::iterator const end = elements_.end();
538 int wid = left_margin;
540 // Search for the first element that goes beyond right margin
541 Elements::iterator cit = beg;
542 for ( ; cit != end ; ++cit) {
543 if (wid + cit->dim.wid > w)
549 // This should not happen since the row is too long.
550 LYXERR0("Something is wrong, cannot shorten row: " << *this);
554 // Iterate backwards over breakable elements and try to break them
555 Elements::iterator cit_brk = cit;
556 int wid_brk = wid + cit_brk->dim.wid;
559 while (cit_brk != beg) {
561 // make a copy of the element to work on it.
562 Element brk = *cit_brk;
563 /* If the current element is an inset that allows breaking row
564 * after itself, and if the row is already short enough after
565 * this element, then cut right after it.
567 if (wid_brk <= w && brk.row_flags & CanBreakAfter) {
570 moveElements(elements_, cit_brk + 1, tail);
573 // assume now that the current element is not there
574 wid_brk -= brk.dim.wid;
575 /* If the current element is an inset that allows breaking row
576 * before itself, and if the row is already short enough before
577 * this element, then cut right before it.
579 if (wid_brk <= w && brk.row_flags & CanBreakBefore && cit_brk != beg) {
580 end_ = (cit_brk -1)->endpos;
582 moveElements(elements_, cit_brk, tail);
585 /* We have found a suitable separable element. This is the common case.
586 * Try to break it cleanly at a length that is both
587 * - less than the available space on the row
588 * - shorter than the natural width of the element, in order to enforce
591 if (brk.splitAt(min(w - wid_brk, brk.dim.wid - 2), next_width, false, tail)) {
592 /* if this element originally did not cause a row overflow
593 * in itself, and the remainder of the row would still be
594 * too large after breaking, then we will have issues in
595 * next row. Thus breaking does not help.
597 if (wid_brk + cit_brk->dim.wid < w
598 && dim_.wid - (wid_brk + brk.dim.wid) >= next_width) {
604 dim_.wid = wid_brk + brk.dim.wid;
605 // If there are other elements, they should be removed.
606 moveElements(elements_, cit_brk + 1, tail);
609 LATTEST(tail.empty());
612 if (cit != beg && cit->row_flags & NoBreakBefore) {
613 // It is not possible to separate this element from the
614 // previous one. (e.g. VIRTUAL)
620 // There is no usable separator, but several elements have
621 // been added. We can cut right here.
624 moveElements(elements_, cit, tail);
628 /* If we are here, it means that we have not found a separator to
629 * shorten the row. Let's try to break it again, but force
630 * splitting this time.
632 if (cit->splitAt(w - wid, next_width, true, tail)) {
634 dim_.wid = wid + cit->dim.wid;
635 // If there are other elements, they should be removed.
636 moveElements(elements_, cit + 1, tail);
640 // cit == beg; remove all elements after the first one.
641 moveElements(elements_, cit + 1, tail);
646 void Row::reverseRTL()
649 pos_type const end = elements_.size();
651 // gather a sequence of elements with the same direction
652 bool const rtl = elements_[i].isRTL();
654 while (j < end && elements_[j].isRTL() == rtl)
656 // if the direction is not the same as the paragraph
657 // direction, the sequence has to be reverted.
659 reverse(elements_.begin() + i, elements_.begin() + j);
662 // If the paragraph itself is RTL, reverse everything
664 reverse(elements_.begin(), elements_.end());
667 Row::const_iterator const
668 Row::findElement(pos_type const pos, bool const boundary, double & x) const
671 * When boundary is true, position i is in the row element (pos, endpos)
674 * whereas, when boundary is false, the test is
676 * The correction below allows to handle both cases.
678 int const boundary_corr = (boundary && pos) ? -1 : 0;
682 /** Early return in trivial cases
683 * 1) the row is empty
684 * 2) the position is the left-most position of the row; there
685 * is a quirk here however: if the first element is virtual
686 * (end-of-par marker for example), then we have to look
690 || (pos == begin()->left_pos() && !boundary
691 && !begin()->isVirtual()))
694 const_iterator cit = begin();
695 for ( ; cit != end() ; ++cit) {
696 /** Look whether the cursor is inside the element's span. Note
697 * that it is necessary to take the boundary into account, and
698 * to accept virtual elements, in which case the position
699 * will be before the virtual element.
701 if (cit->isVirtual() && pos + boundary_corr == cit->pos)
703 else if (pos + boundary_corr >= cit->pos
704 && pos + boundary_corr < cit->endpos) {
705 x += cit->pos2x(pos);
708 x += cit->full_width();