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"
23 #include "frontends/FontMetrics.h"
25 #include "support/debug.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxalgo.h"
37 using frontend::FontMetrics;
40 // Maximum length that a space can be stretched when justifying text
41 static double const MAX_SPACE_STRETCH = 1.5; //em
44 int Row::Element::countSeparators() const
48 return count(str.begin(), str.end(), ' ');
52 int Row::Element::countExpanders() const
56 return theFontMetrics(font).countExpanders(str);
60 int Row::Element::expansionAmount() const
64 return countExpanders() * theFontMetrics(font).em();
68 void Row::Element::setExtra(double extra_per_em)
72 extra = extra_per_em * theFontMetrics(font).em();
76 double Row::Element::pos2x(pos_type const i) const
78 // This can happen with inline completion when clicking on the
79 // row after the completion.
80 if (i < pos || i > endpos)
84 //handle first the two bounds of the element
85 if (i == endpos && type != VIRTUAL)
86 w = isRTL() ? 0 : full_width();
87 else if (i == pos || type != STRING)
88 w = isRTL() ? full_width() : 0;
90 FontMetrics const & fm = theFontMetrics(font);
91 w = fm.pos2x(str, i - pos, isRTL(), extra);
98 pos_type Row::Element::x2pos(int &x) const
100 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
105 FontMetrics const & fm = theFontMetrics(font);
106 i = fm.x2pos(str, x, isRTL(), extra);
110 // those elements are actually empty (but they have a width)
112 x = isRTL() ? int(full_width()) : 0;
116 // those elements contain only one position. Round to
118 if (x > full_width()) {
119 x = int(full_width());
127 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
132 bool Row::Element::breakAt(int w, bool force)
134 if (type != STRING || dim.wid <= w)
137 FontMetrics const & fm = theFontMetrics(font);
139 if(fm.breakAt(str, x, isRTL(), force)) {
141 endpos = pos + str.length();
142 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
150 pos_type Row::Element::left_pos() const
152 return isRTL() ? endpos : pos;
156 pos_type Row::Element::right_pos() const
158 return isRTL() ? pos : endpos;
163 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
164 sel_beg(-1), sel_end(-1),
165 begin_margin_sel(false), end_margin_sel(false),
166 changed_(false), crc_(0),
167 pit_(0), pos_(0), end_(0),
168 right_boundary_(false), flushed_(false), rtl_(false)
172 void Row::setCrc(size_type crc) const
174 changed_ = crc != crc_;
179 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
180 DocIterator const & end) const
182 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
183 pos_type const margin_pos = left_margin ? pos_ : end_;
185 // Is the chosen margin selected ?
186 if (sel_pos == margin_pos) {
187 if (beg.pos() == end.pos())
188 // This is a special case in which the space between after
189 // pos i-1 and before pos i is selected, i.e. the margins
190 // (see DocIterator::boundary_).
191 return beg.boundary() && !end.boundary();
192 else if (end.pos() == margin_pos)
193 // If the selection ends around the margin, it is only
194 // drawn if the cursor is after the margin.
195 return !end.boundary();
196 else if (beg.pos() == margin_pos)
197 // If the selection begins around the margin, it is
198 // only drawn if the cursor is before the margin.
199 return beg.boundary();
207 void Row::setSelectionAndMargins(DocIterator const & beg,
208 DocIterator const & end) const
210 setSelection(beg.pos(), end.pos());
213 end_margin_sel = isMarginSelected(false, beg, end);
214 begin_margin_sel = isMarginSelected(true, beg, end);
219 void Row::setSelection(pos_type beg, pos_type end) const
221 if (pos_ >= beg && pos_ <= end)
223 else if (beg > pos_ && beg <= end_)
228 if (end_ >= beg && end_ <= end)
230 else if (end < end_ && end >= pos_)
237 bool Row::selection() const
239 return sel_beg != -1 && sel_end != -1;
243 ostream & operator<<(ostream & os, Row::Element const & e)
246 os << e.endpos << "<<" << e.pos << " ";
248 os << e.pos << ">>" << e.endpos << " ";
252 os << "STRING: `" << to_utf8(e.str) << "' ("
253 << e.countExpanders() << " expanders.), ";
256 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
259 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
265 os << "width=" << e.full_width();
270 ostream & operator<<(ostream & os, Row const & row)
272 os << " pos: " << row.pos_ << " end: " << row.end_
273 << " left_margin: " << row.left_margin
274 << " width: " << row.dim_.wid
275 << " right_margin: " << row.right_margin
276 << " ascent: " << row.dim_.asc
277 << " descent: " << row.dim_.des
278 << " separator: " << row.separator
279 << " label_hfill: " << row.label_hfill
280 << " row_boundary: " << row.right_boundary() << "\n";
281 double x = row.left_margin;
282 Row::Elements::const_iterator it = row.elements_.begin();
283 for ( ; it != row.elements_.end() ; ++it) {
284 os << "x=" << x << " => " << *it << endl;
285 x += it->full_width();
291 int Row::left_x() const
293 double x = left_margin;
294 const_iterator const end = elements_.end();
295 const_iterator cit = elements_.begin();
296 while (cit != end && cit->isVirtual()) {
297 x += cit->full_width();
304 int Row::right_x() const
307 const_iterator const begin = elements_.begin();
308 const_iterator cit = elements_.end();
309 while (cit != begin) {
311 if (cit->isVirtual())
312 x -= cit->full_width();
320 int Row::countSeparators() const
323 const_iterator const end = elements_.end();
324 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
325 n += cit->countSeparators();
330 bool Row::setExtraWidth(int w)
333 // this is not expected to happen (but it does)
335 // amount of expansion: number of expanders time the em value for each
338 for (Row::Element const & e : elements_)
339 exp_amount += e.expansionAmount();
342 // extra length per expander per em
343 double extra_per_em = double(w) / exp_amount;
344 if (extra_per_em > MAX_SPACE_STRETCH)
345 // do not stretch more than MAX_SPACE_STRETCH em per expander
347 // add extra length to each element proportionally to its em.
348 for (Row::Element & e : elements_)
349 if (e.type == Row::STRING)
350 e.setExtra(extra_per_em);
351 // update row dimension
357 bool Row::sameString(Font const & f, Change const & ch) const
359 if (elements_.empty())
361 Element const & elt = elements_.back();
362 return elt.type == STRING && !elt.final
363 && elt.font == f && elt.change == ch;
367 void Row::finalizeLast()
369 if (elements_.empty())
371 Element & elt = elements_.back();
376 if (elt.type == STRING) {
377 dim_.wid -= elt.dim.wid;
378 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
379 dim_.wid += elt.dim.wid;
384 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
385 Font const & f, Change const & ch)
388 Element e(INSET, pos, f, ch);
391 elements_.push_back(e);
396 void Row::add(pos_type const pos, char_type const c,
397 Font const & f, Change const & ch)
399 if (!sameString(f, ch)) {
401 Element e(STRING, pos, f, ch);
402 elements_.push_back(e);
404 if (back().str.length() % 30 == 0) {
405 dim_.wid -= back().dim.wid;
407 back().endpos = pos + 1;
408 back().dim.wid = theFontMetrics(back().font).width(back().str);
409 dim_.wid += back().dim.wid;
412 back().endpos = pos + 1;
417 void Row::addVirtual(pos_type const pos, docstring const & s,
418 Font const & f, Change const & ch)
421 Element e(VIRTUAL, pos, f, ch);
423 e.dim.wid = theFontMetrics(f).width(s);
424 dim_.wid += e.dim.wid;
426 elements_.push_back(e);
431 void Row::addSpace(pos_type const pos, int const width,
432 Font const & f, Change const & ch)
435 Element e(SPACE, pos, f, ch);
437 elements_.push_back(e);
438 dim_.wid += e.dim.wid;
444 dim_.wid -= elements_.back().dim.wid;
445 elements_.pop_back();
449 bool Row::shortenIfNeeded(pos_type const keep, int const w, int const next_width)
451 if (empty() || width() <= w)
454 Elements::iterator const beg = elements_.begin();
455 Elements::iterator const end = elements_.end();
456 int wid = left_margin;
458 // Search for the first element that goes beyond right margin
459 Elements::iterator cit = beg;
460 for ( ; cit != end ; ++cit) {
461 if (wid + cit->dim.wid > w)
467 // This should not happen since the row is too long.
468 LYXERR0("Something is wrong cannot shorten row: " << *this);
472 // Iterate backwards over breakable elements and try to break them
473 Elements::iterator cit_brk = cit;
474 int wid_brk = wid + cit_brk->dim.wid;
476 while (cit_brk != beg) {
478 // make a copy of the element to work on it.
479 Element brk = *cit_brk;
480 wid_brk -= brk.dim.wid;
481 if (brk.countSeparators() == 0 || brk.pos < keep)
483 /* We have found a suitable separable element. This is the common case.
484 * Try to break it cleanly (at word boundary) at a length that is both
485 * - less than the available space on the row
486 * - shorter than the natural width of the element, in order to enforce
489 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
490 /* if this element originally did not cause a row overflow
491 * in itself, and the remainder of the row would still be
492 * too large after breaking, then we will have issues in
493 * next row. Thus breaking does not help.
495 if (wid_brk + cit_brk->dim.wid < w
496 && dim_.wid - (wid_brk + brk.dim.wid) >= next_width) {
500 /* after breakAt, there may be spaces at the end of the
501 * string, but they are not counted in the string length
502 * (QTextLayout feature, actually). We remove them, but do
503 * not change the end of the row, since spaces at row
504 * break are invisible.
506 brk.str = rtrim(brk.str);
507 brk.endpos = brk.pos + brk.str.length();
509 dim_.wid = wid_brk + brk.dim.wid;
510 // If there are other elements, they should be removed.
511 elements_.erase(cit_brk + 1, end);
516 if (cit != beg && cit->type == VIRTUAL) {
517 // It is not possible to separate a virtual element from the
524 // There is no usable separator, but several elements have
525 // been added. We can cut right here.
528 elements_.erase(cit, end);
532 /* If we are here, it means that we have not found a separator to
533 * shorten the row. Let's try to break it again, but not at word
534 * boundary this time.
536 if (cit->breakAt(w - wid, true)) {
538 // See comment above.
539 cit->str = rtrim(cit->str);
540 cit->endpos = cit->pos + cit->str.length();
541 dim_.wid = wid + cit->dim.wid;
542 // If there are other elements, they should be removed.
543 elements_.erase(next(cit, 1), end);
550 void Row::reverseRTL(bool const rtl_par)
553 pos_type const end = elements_.size();
555 // gather a sequence of elements with the same direction
556 bool const rtl = elements_[i].isRTL();
558 while (j < end && elements_[j].isRTL() == rtl)
560 // if the direction is not the same as the paragraph
561 // direction, the sequence has to be reverted.
563 reverse(elements_.begin() + i, elements_.begin() + j);
566 // If the paragraph itself is RTL, reverse everything
568 reverse(elements_.begin(), elements_.end());
572 Row::const_iterator const
573 Row::findElement(pos_type const pos, bool const boundary, double & x) const
576 * When boundary is true, position i is in the row element (pos, endpos)
579 * whereas, when boundary is false, the test is
581 * The correction below allows to handle both cases.
583 int const boundary_corr = (boundary && pos) ? -1 : 0;
587 /** Early return in trivial cases
588 * 1) the row is empty
589 * 2) the position is the left-most position of the row; there
590 * is a quirk here however: if the first element is virtual
591 * (end-of-par marker for example), then we have to look
595 || (pos == begin()->left_pos() && !boundary
596 && !begin()->isVirtual()))
599 Row::const_iterator cit = begin();
600 for ( ; cit != end() ; ++cit) {
601 /** Look whether the cursor is inside the element's
602 * span. Note that it is necessary to take the
603 * boundary into account, and to accept virtual
604 * elements, which have pos == endpos.
606 if (pos + boundary_corr >= cit->pos
607 && (pos + boundary_corr < cit->endpos || cit->isVirtual())) {
608 x += cit->pos2x(pos);
611 x += cit->full_width();