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 int Row::Element::countSeparators() const
44 // Consecutive spaces count as only one separator.
45 bool wasspace = false;
47 for (size_t i = 0 ; i < str.size() ; ++i) {
60 double Row::Element::pos2x(pos_type const i) const
62 // This can happen with inline completion when clicking on the
63 // row after the completion.
64 if (i < pos || i > endpos)
68 //handle first the two bounds of the element
69 if (i == endpos && type != VIRTUAL
70 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
71 w = isRTL() ? 0 : full_width();
72 else if (i == pos || type != STRING)
73 w = isRTL() ? full_width() : 0;
75 FontMetrics const & fm = theFontMetrics(font);
76 w = fm.pos2x(str, i - pos, isRTL(), extra);
83 pos_type Row::Element::x2pos(int &x) const
85 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
90 FontMetrics const & fm = theFontMetrics(font);
91 i = fm.x2pos(str, x, isRTL(), extra);
95 // those elements are actually empty (but they have a width)
97 x = isRTL() ? int(full_width()) : 0;
101 // those elements contain only one position. Round to
103 if (x > full_width()) {
104 x = int(full_width());
112 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
118 bool Row::Element::breakAt(int w, bool force)
120 if (type != STRING || dim.wid <= w)
123 FontMetrics const & fm = theFontMetrics(font);
125 if(fm.breakAt(str, x, isRTL(), force)) {
127 endpos = pos + str.length();
128 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
132 // Qt will not break at a leading space, and we need that sometimes, see
133 // http://www.lyx.org/trac/ticket/9921.
134 // It would be nice to fix this properly, but for now do it by hand.
135 // FIXME: figure out what to do for RtL text.
136 if (!isRTL() && !str.empty() && str[0] == ' ') {
147 pos_type Row::Element::left_pos() const
149 return isRTL() ? endpos : pos;
153 pos_type Row::Element::right_pos() const
155 return isRTL() ? pos : endpos;
160 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
161 sel_beg(-1), sel_end(-1),
162 begin_margin_sel(false), end_margin_sel(false),
163 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
167 void Row::setCrc(size_type crc) const
169 changed_ = crc != crc_;
174 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
175 DocIterator const & end) const
177 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
178 pos_type const margin_pos = left_margin ? pos_ : end_;
180 // Is the chosen margin selected ?
181 if (sel_pos == margin_pos) {
182 if (beg.pos() == end.pos())
183 // This is a special case in which the space between after
184 // pos i-1 and before pos i is selected, i.e. the margins
185 // (see DocIterator::boundary_).
186 return beg.boundary() && !end.boundary();
187 else if (end.pos() == margin_pos)
188 // If the selection ends around the margin, it is only
189 // drawn if the cursor is after the margin.
190 return !end.boundary();
191 else if (beg.pos() == margin_pos)
192 // If the selection begins around the margin, it is
193 // only drawn if the cursor is before the margin.
194 return beg.boundary();
202 void Row::setSelectionAndMargins(DocIterator const & beg,
203 DocIterator const & end) const
205 setSelection(beg.pos(), end.pos());
208 end_margin_sel = isMarginSelected(false, beg, end);
209 begin_margin_sel = isMarginSelected(true, beg, end);
214 void Row::setSelection(pos_type beg, pos_type end) const
216 if (pos_ >= beg && pos_ <= end)
218 else if (beg > pos_ && beg <= end_)
223 if (end_ >= beg && end_ <= end)
225 else if (end < end_ && end >= pos_)
232 bool Row::selection() const
234 return sel_beg != -1 && sel_end != -1;
238 ostream & operator<<(ostream & os, Row::Element const & e)
241 os << e.endpos << "<<" << e.pos << " ";
243 os << e.pos << ">>" << e.endpos << " ";
247 os << "STRING: `" << to_utf8(e.str) << "' ("
248 << e.countSeparators() << " sep.), ";
251 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
254 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
260 os << "width=" << e.full_width();
265 ostream & operator<<(ostream & os, Row const & row)
267 os << " pos: " << row.pos_ << " end: " << row.end_
268 << " left_margin: " << row.left_margin
269 << " width: " << row.dim_.wid
270 << " right_margin: " << row.right_margin
271 << " ascent: " << row.dim_.asc
272 << " descent: " << row.dim_.des
273 << " separator: " << row.separator
274 << " label_hfill: " << row.label_hfill
275 << " row_boundary: " << row.right_boundary() << "\n";
276 double x = row.left_margin;
277 Row::Elements::const_iterator it = row.elements_.begin();
278 for ( ; it != row.elements_.end() ; ++it) {
279 os << "x=" << x << " => " << *it << endl;
280 x += it->full_width();
286 int Row::left_x() const
288 double x = left_margin;
289 const_iterator const end = elements_.end();
290 const_iterator cit = elements_.begin();
291 while (cit != end && cit->isVirtual()) {
292 x += cit->full_width();
299 int Row::right_x() const
302 const_iterator const begin = elements_.begin();
303 const_iterator cit = elements_.end();
304 while (cit != begin) {
306 if (cit->isVirtual())
307 x -= cit->full_width();
315 int Row::countSeparators() const
318 const_iterator const end = elements_.end();
319 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
320 n += cit->countSeparators();
325 void Row::setSeparatorExtraWidth(double w)
328 iterator const end = elements_.end();
329 for (iterator it = elements_.begin() ; it != end ; ++it)
330 if (it->type == Row::STRING)
335 bool Row::sameString(Font const & f, Change const & ch) const
337 if (elements_.empty())
339 Element const & elt = elements_.back();
340 return elt.type == STRING && !elt.final
341 && elt.font == f && elt.change == ch;
345 void Row::finalizeLast()
347 if (elements_.empty())
349 Element & elt = elements_.back();
354 if (elt.type == STRING) {
355 dim_.wid -= elt.dim.wid;
356 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
357 dim_.wid += elt.dim.wid;
362 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
363 Font const & f, Change const & ch)
366 Element e(INSET, pos, f, ch);
369 elements_.push_back(e);
374 void Row::add(pos_type const pos, char_type const c,
375 Font const & f, Change const & ch)
377 if (!sameString(f, ch)) {
379 Element e(STRING, pos, f, ch);
380 elements_.push_back(e);
382 if (back().str.length() % 30 == 0) {
383 dim_.wid -= back().dim.wid;
385 back().endpos = pos + 1;
386 back().dim.wid = theFontMetrics(back().font).width(back().str);
387 dim_.wid += back().dim.wid;
390 back().endpos = pos + 1;
395 void Row::addVirtual(pos_type const pos, docstring const & s,
396 Font const & f, Change const & ch)
399 Element e(VIRTUAL, pos, f, ch);
401 e.dim.wid = theFontMetrics(f).width(s);
402 dim_.wid += e.dim.wid;
404 elements_.push_back(e);
409 void Row::addSpace(pos_type const pos, int const width,
410 Font const & f, Change const & ch)
413 Element e(SPACE, pos, f, ch);
415 elements_.push_back(e);
416 dim_.wid += e.dim.wid;
422 dim_.wid -= elements_.back().dim.wid;
423 elements_.pop_back();
427 void Row::shortenIfNeeded(pos_type const keep, int const w)
429 if (empty() || width() <= w)
432 Elements::iterator const beg = elements_.begin();
433 Elements::iterator const end = elements_.end();
434 int wid = left_margin;
436 // Search for the first element that goes beyond right margin
437 Elements::iterator cit = beg;
438 for ( ; cit != end ; ++cit) {
439 if (wid + cit->dim.wid > w)
445 // This should not happen since the row is too long.
446 LYXERR0("Something is wrong cannot shorten row: " << *this);
450 // Iterate backwards over breakable elements and try to break them
451 Elements::iterator cit_brk = cit;
452 int wid_brk = wid + cit_brk->dim.wid;
454 while (cit_brk != beg) {
456 // make a copy of the element to work on it.
457 Element brk = *cit_brk;
458 wid_brk -= brk.dim.wid;
459 if (brk.countSeparators() == 0 || brk.pos < keep)
461 /* We have found a suitable separable element. This is the common case.
462 * Try to break it cleanly (at word boundary) at a length that is both
463 * - less than the available space on the row
464 * - shorter than the natural width of the element, in order to enforce
467 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
468 /* if this element originally did not cause a row overflow
469 * in itself, and the remainder of the row would still be
470 * too large after breaking, then we will have issues in
471 * next row. Thus breaking does not help.
473 if (wid_brk + cit_brk->dim.wid < w
474 && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
478 /* after breakAt, there may be spaces at the end of the
479 * string, but they are not counted in the string length
480 * (QTextLayout feature, actually). We remove them, but do
481 * not change the end of the row, since spaces at row
482 * break are invisible.
484 brk.str = rtrim(brk.str);
485 brk.endpos = brk.pos + brk.str.length();
487 dim_.wid = wid_brk + brk.dim.wid;
488 // If there are other elements, they should be removed.
489 elements_.erase(cit_brk + 1, end);
494 if (cit != beg && cit->type == VIRTUAL) {
495 // It is not possible to separate a virtual element from the
502 // There is no usable separator, but several elements have
503 // been added. We can cut right here.
506 elements_.erase(cit, end);
510 /* If we are here, it means that we have not found a separator to
511 * shorten the row. Let's try to break it again, but not at word
512 * boundary this time.
514 if (cit->breakAt(w - wid, true)) {
516 // See comment above.
517 cit->str = rtrim(cit->str);
518 cit->endpos = cit->pos + cit->str.length();
519 dim_.wid = wid + cit->dim.wid;
520 // If there are other elements, they should be removed.
521 elements_.erase(next(cit, 1), end);
526 void Row::reverseRTL(bool const rtl_par)
529 pos_type const end = elements_.size();
531 // gather a sequence of elements with the same direction
532 bool const rtl = elements_[i].isRTL();
534 while (j < end && elements_[j].isRTL() == rtl)
536 // if the direction is not the same as the paragraph
537 // direction, the sequence has to be reverted.
539 reverse(elements_.begin() + i, elements_.begin() + j);
542 // If the paragraph itself is RTL, reverse everything
544 reverse(elements_.begin(), elements_.end());