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() + 1) / 2) {
119 x = int(full_width());
126 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
131 bool Row::Element::breakAt(int w, bool force)
133 if (type != STRING || dim.wid <= w)
136 FontMetrics const & fm = theFontMetrics(font);
138 if(fm.breakAt(str, x, isRTL(), force)) {
140 endpos = pos + str.length();
141 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
149 pos_type Row::Element::left_pos() const
151 return isRTL() ? endpos : pos;
155 pos_type Row::Element::right_pos() const
157 return isRTL() ? pos : endpos;
162 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
163 sel_beg(-1), sel_end(-1),
164 begin_margin_sel(false), end_margin_sel(false),
165 changed_(false), crc_(0),
166 pit_(0), pos_(0), end_(0),
167 right_boundary_(false), flushed_(false), rtl_(false)
171 void Row::setCrc(size_type crc) const
173 changed_ = crc != crc_;
178 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
179 DocIterator const & end) const
181 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
182 pos_type const margin_pos = left_margin ? pos_ : end_;
184 // Is the chosen margin selected ?
185 if (sel_pos == margin_pos) {
186 if (beg.pos() == end.pos())
187 // This is a special case in which the space between after
188 // pos i-1 and before pos i is selected, i.e. the margins
189 // (see DocIterator::boundary_).
190 return beg.boundary() && !end.boundary();
191 else if (end.pos() == margin_pos)
192 // If the selection ends around the margin, it is only
193 // drawn if the cursor is after the margin.
194 return !end.boundary();
195 else if (beg.pos() == margin_pos)
196 // If the selection begins around the margin, it is
197 // only drawn if the cursor is before the margin.
198 return beg.boundary();
206 void Row::setSelectionAndMargins(DocIterator const & beg,
207 DocIterator const & end) const
209 setSelection(beg.pos(), end.pos());
212 end_margin_sel = isMarginSelected(false, beg, end);
213 begin_margin_sel = isMarginSelected(true, beg, end);
218 void Row::setSelection(pos_type beg, pos_type end) const
220 if (pos_ >= beg && pos_ <= end)
222 else if (beg > pos_ && beg <= end_)
227 if (end_ >= beg && end_ <= end)
229 else if (end < end_ && end >= pos_)
236 bool Row::selection() const
238 return sel_beg != -1 && sel_end != -1;
242 ostream & operator<<(ostream & os, Row::Element const & e)
245 os << e.endpos << "<<" << e.pos << " ";
247 os << e.pos << ">>" << e.endpos << " ";
251 os << "STRING: `" << to_utf8(e.str) << "' ("
252 << e.countExpanders() << " expanders.), ";
255 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
258 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
264 os << "width=" << e.full_width();
269 ostream & operator<<(ostream & os, Row const & row)
271 os << " pos: " << row.pos_ << " end: " << row.end_
272 << " left_margin: " << row.left_margin
273 << " width: " << row.dim_.wid
274 << " right_margin: " << row.right_margin
275 << " ascent: " << row.dim_.asc
276 << " descent: " << row.dim_.des
277 << " separator: " << row.separator
278 << " label_hfill: " << row.label_hfill
279 << " row_boundary: " << row.right_boundary() << "\n";
280 double x = row.left_margin;
281 Row::Elements::const_iterator it = row.elements_.begin();
282 for ( ; it != row.elements_.end() ; ++it) {
283 os << "x=" << x << " => " << *it << endl;
284 x += it->full_width();
290 int Row::left_x() const
292 double x = left_margin;
293 const_iterator const end = elements_.end();
294 const_iterator cit = elements_.begin();
295 while (cit != end && cit->isVirtual()) {
296 x += cit->full_width();
303 int Row::right_x() const
306 const_iterator const begin = elements_.begin();
307 const_iterator cit = elements_.end();
308 while (cit != begin) {
310 if (cit->isVirtual())
311 x -= cit->full_width();
319 int Row::countSeparators() const
322 const_iterator const end = elements_.end();
323 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
324 n += cit->countSeparators();
329 bool Row::setExtraWidth(int w)
332 // this is not expected to happen (but it does)
334 // amount of expansion: number of expanders time the em value for each
337 for (Row::Element const & e : elements_)
338 exp_amount += e.expansionAmount();
341 // extra length per expander per em
342 double extra_per_em = double(w) / exp_amount;
343 if (extra_per_em > MAX_SPACE_STRETCH)
344 // do not stretch more than MAX_SPACE_STRETCH em per expander
346 // add extra length to each element proportionally to its em.
347 for (Row::Element & e : elements_)
348 if (e.type == Row::STRING)
349 e.setExtra(extra_per_em);
350 // update row dimension
356 bool Row::sameString(Font const & f, Change const & ch) const
358 if (elements_.empty())
360 Element const & elt = elements_.back();
361 return elt.type == STRING && !elt.final
362 && elt.font == f && elt.change == ch;
366 void Row::finalizeLast()
368 if (elements_.empty())
370 Element & elt = elements_.back();
375 if (elt.type == STRING) {
376 dim_.wid -= elt.dim.wid;
377 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
378 dim_.wid += elt.dim.wid;
383 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
384 Font const & f, Change const & ch)
387 Element e(INSET, pos, f, ch);
390 elements_.push_back(e);
395 void Row::add(pos_type const pos, char_type const c,
396 Font const & f, Change const & ch)
398 if (!sameString(f, ch)) {
400 Element e(STRING, pos, f, ch);
401 elements_.push_back(e);
403 if (back().str.length() % 30 == 0) {
404 dim_.wid -= back().dim.wid;
406 back().endpos = pos + 1;
407 back().dim.wid = theFontMetrics(back().font).width(back().str);
408 dim_.wid += back().dim.wid;
411 back().endpos = pos + 1;
416 void Row::addVirtual(pos_type const pos, docstring const & s,
417 Font const & f, Change const & ch)
420 Element e(VIRTUAL, pos, f, ch);
422 e.dim.wid = theFontMetrics(f).width(s);
423 dim_.wid += e.dim.wid;
425 elements_.push_back(e);
430 void Row::addSpace(pos_type const pos, int const width,
431 Font const & f, Change const & ch)
434 Element e(SPACE, pos, f, ch);
436 elements_.push_back(e);
437 dim_.wid += e.dim.wid;
443 dim_.wid -= elements_.back().dim.wid;
444 elements_.pop_back();
448 bool Row::shortenIfNeeded(pos_type const keep, int const w, int const next_width)
450 if (empty() || width() <= w)
453 Elements::iterator const beg = elements_.begin();
454 Elements::iterator const end = elements_.end();
455 int wid = left_margin;
457 // Search for the first element that goes beyond right margin
458 Elements::iterator cit = beg;
459 for ( ; cit != end ; ++cit) {
460 if (wid + cit->dim.wid > w)
466 // This should not happen since the row is too long.
467 LYXERR0("Something is wrong cannot shorten row: " << *this);
471 // Iterate backwards over breakable elements and try to break them
472 Elements::iterator cit_brk = cit;
473 int wid_brk = wid + cit_brk->dim.wid;
475 while (cit_brk != beg) {
477 // make a copy of the element to work on it.
478 Element brk = *cit_brk;
479 wid_brk -= brk.dim.wid;
480 if (brk.countSeparators() == 0 || brk.pos < keep)
482 /* We have found a suitable separable element. This is the common case.
483 * Try to break it cleanly (at word boundary) at a length that is both
484 * - less than the available space on the row
485 * - shorter than the natural width of the element, in order to enforce
488 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
489 /* if this element originally did not cause a row overflow
490 * in itself, and the remainder of the row would still be
491 * too large after breaking, then we will have issues in
492 * next row. Thus breaking does not help.
494 if (wid_brk + cit_brk->dim.wid < w
495 && dim_.wid - (wid_brk + brk.dim.wid) >= next_width) {
499 /* after breakAt, there may be spaces at the end of the
500 * string, but they are not counted in the string length
501 * (QTextLayout feature, actually). We remove them, but do
502 * not change the end of the row, since spaces at row
503 * break are invisible.
505 brk.str = rtrim(brk.str);
506 brk.endpos = brk.pos + brk.str.length();
508 dim_.wid = wid_brk + brk.dim.wid;
509 // If there are other elements, they should be removed.
510 elements_.erase(cit_brk + 1, end);
515 if (cit != beg && cit->type == VIRTUAL) {
516 // It is not possible to separate a virtual element from the
523 // There is no usable separator, but several elements have
524 // been added. We can cut right here.
527 elements_.erase(cit, end);
531 /* If we are here, it means that we have not found a separator to
532 * shorten the row. Let's try to break it again, but not at word
533 * boundary this time.
535 if (cit->breakAt(w - wid, true)) {
537 // See comment above.
538 cit->str = rtrim(cit->str);
539 cit->endpos = cit->pos + cit->str.length();
540 dim_.wid = wid + cit->dim.wid;
541 // If there are other elements, they should be removed.
542 elements_.erase(next(cit, 1), end);
549 void Row::reverseRTL(bool const rtl_par)
552 pos_type const end = elements_.size();
554 // gather a sequence of elements with the same direction
555 bool const rtl = elements_[i].isRTL();
557 while (j < end && elements_[j].isRTL() == rtl)
559 // if the direction is not the same as the paragraph
560 // direction, the sequence has to be reverted.
562 reverse(elements_.begin() + i, elements_.begin() + j);
565 // If the paragraph itself is RTL, reverse everything
567 reverse(elements_.begin(), elements_.end());
571 Row::const_iterator const
572 Row::findElement(pos_type const pos, bool const boundary, double & x) const
575 * When boundary is true, position i is in the row element (pos, endpos)
578 * whereas, when boundary is false, the test is
580 * The correction below allows to handle both cases.
582 int const boundary_corr = (boundary && pos) ? -1 : 0;
586 /** Early return in trivial cases
587 * 1) the row is empty
588 * 2) the position is the left-most position of the row; there
589 * is a quirk here however: if the first element is virtual
590 * (end-of-par marker for example), then we have to look
594 || (pos == begin()->left_pos() && !boundary
595 && !begin()->isVirtual()))
598 Row::const_iterator cit = begin();
599 for ( ; cit != end() ; ++cit) {
600 /** Look whether the cursor is inside the element's
601 * span. Note that it is necessary to take the
602 * boundary into account, and to accept virtual
603 * elements, which have pos == endpos.
605 if (pos + boundary_corr >= cit->pos
606 && (pos + boundary_corr < cit->endpos || cit->isVirtual())) {
607 x += cit->pos2x(pos);
610 x += cit->full_width();