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 return count(str.begin(), str.end(), ' ');
48 double Row::Element::pos2x(pos_type const i) const
50 // This can happen with inline completion when clicking on the
51 // row after the completion.
52 if (i < pos || i > endpos)
56 //handle first the two bounds of the element
57 if (i == endpos && type != VIRTUAL
58 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
59 w = isRTL() ? 0 : full_width();
60 else if (i == pos || type != STRING)
61 w = isRTL() ? full_width() : 0;
63 FontMetrics const & fm = theFontMetrics(font);
64 w = fm.pos2x(str, i - pos, isRTL(), extra);
71 pos_type Row::Element::x2pos(int &x) const
73 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
78 FontMetrics const & fm = theFontMetrics(font);
79 i = fm.x2pos(str, x, isRTL(), extra);
83 // those elements are actually empty (but they have a width)
85 x = isRTL() ? int(full_width()) : 0;
89 // those elements contain only one position. Round to
91 if (x > full_width()) {
92 x = int(full_width());
100 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
106 bool Row::Element::breakAt(int w, bool force)
108 if (type != STRING || dim.wid <= w)
111 FontMetrics const & fm = theFontMetrics(font);
113 if(fm.breakAt(str, x, isRTL(), force)) {
115 endpos = pos + str.length();
116 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
123 pos_type Row::Element::left_pos() const
125 return isRTL() ? endpos : pos;
129 pos_type Row::Element::right_pos() const
131 return isRTL() ? pos : endpos;
136 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
137 sel_beg(-1), sel_end(-1),
138 begin_margin_sel(false), end_margin_sel(false),
139 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
143 void Row::setCrc(size_type crc) const
145 changed_ = crc != crc_;
150 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
151 DocIterator const & end) const
153 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
154 pos_type const margin_pos = left_margin ? pos_ : end_;
156 // Is the chosen margin selected ?
157 if (sel_pos == margin_pos) {
158 if (beg.pos() == end.pos())
159 // This is a special case in which the space between after
160 // pos i-1 and before pos i is selected, i.e. the margins
161 // (see DocIterator::boundary_).
162 return beg.boundary() && !end.boundary();
163 else if (end.pos() == margin_pos)
164 // If the selection ends around the margin, it is only
165 // drawn if the cursor is after the margin.
166 return !end.boundary();
167 else if (beg.pos() == margin_pos)
168 // If the selection begins around the margin, it is
169 // only drawn if the cursor is before the margin.
170 return beg.boundary();
178 void Row::setSelectionAndMargins(DocIterator const & beg,
179 DocIterator const & end) const
181 setSelection(beg.pos(), end.pos());
184 end_margin_sel = isMarginSelected(false, beg, end);
185 begin_margin_sel = isMarginSelected(true, beg, end);
190 void Row::setSelection(pos_type beg, pos_type end) const
192 if (pos_ >= beg && pos_ <= end)
194 else if (beg > pos_ && beg <= end_)
199 if (end_ >= beg && end_ <= end)
201 else if (end < end_ && end >= pos_)
208 bool Row::selection() const
210 return sel_beg != -1 && sel_end != -1;
214 ostream & operator<<(ostream & os, Row::Element const & e)
217 os << e.endpos << "<<" << e.pos << " ";
219 os << e.pos << ">>" << e.endpos << " ";
223 os << "STRING: `" << to_utf8(e.str) << "' ("
224 << e.countSeparators() << " sep.), ";
227 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
230 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
236 os << "width=" << e.full_width();
241 ostream & operator<<(ostream & os, Row const & row)
243 os << " pos: " << row.pos_ << " end: " << row.end_
244 << " left_margin: " << row.left_margin
245 << " width: " << row.dim_.wid
246 << " right_margin: " << row.right_margin
247 << " ascent: " << row.dim_.asc
248 << " descent: " << row.dim_.des
249 << " separator: " << row.separator
250 << " label_hfill: " << row.label_hfill
251 << " row_boundary: " << row.right_boundary() << "\n";
252 double x = row.left_margin;
253 Row::Elements::const_iterator it = row.elements_.begin();
254 for ( ; it != row.elements_.end() ; ++it) {
255 os << "x=" << x << " => " << *it << endl;
256 x += it->full_width();
262 int Row::left_x() const
264 double x = left_margin;
265 const_iterator const end = elements_.end();
266 const_iterator cit = elements_.begin();
267 while (cit != end && cit->isVirtual()) {
268 x += cit->full_width();
275 int Row::right_x() const
278 const_iterator const begin = elements_.begin();
279 const_iterator cit = elements_.end();
280 while (cit != begin) {
282 if (cit->isVirtual())
283 x -= cit->full_width();
291 int Row::countSeparators() const
294 const_iterator const end = elements_.end();
295 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
296 n += cit->countSeparators();
301 void Row::setSeparatorExtraWidth(double w)
304 iterator const end = elements_.end();
305 for (iterator it = elements_.begin() ; it != end ; ++it)
306 if (it->type == Row::STRING)
311 bool Row::sameString(Font const & f, Change const & ch) const
313 if (elements_.empty())
315 Element const & elt = elements_.back();
316 return elt.type == STRING && !elt.final
317 && elt.font == f && elt.change == ch;
321 void Row::finalizeLast()
323 if (elements_.empty())
325 Element & elt = elements_.back();
330 if (elt.type == STRING) {
331 dim_.wid -= elt.dim.wid;
332 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
333 dim_.wid += elt.dim.wid;
338 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
339 Font const & f, Change const & ch)
342 Element e(INSET, pos, f, ch);
345 elements_.push_back(e);
350 void Row::add(pos_type const pos, char_type const c,
351 Font const & f, Change const & ch)
353 if (!sameString(f, ch)) {
355 Element e(STRING, pos, f, ch);
356 elements_.push_back(e);
358 if (back().str.length() % 30 == 0) {
359 dim_.wid -= back().dim.wid;
361 back().endpos = pos + 1;
362 back().dim.wid = theFontMetrics(back().font).width(back().str);
363 dim_.wid += back().dim.wid;
366 back().endpos = pos + 1;
371 void Row::addVirtual(pos_type const pos, docstring const & s,
372 Font const & f, Change const & ch)
375 Element e(VIRTUAL, pos, f, ch);
377 e.dim.wid = theFontMetrics(f).width(s);
378 dim_.wid += e.dim.wid;
380 elements_.push_back(e);
385 void Row::addSpace(pos_type const pos, int const width,
386 Font const & f, Change const & ch)
389 Element e(SPACE, pos, f, ch);
391 elements_.push_back(e);
392 dim_.wid += e.dim.wid;
398 dim_.wid -= elements_.back().dim.wid;
399 elements_.pop_back();
403 void Row::shortenIfNeeded(pos_type const keep, int const w)
405 if (empty() || width() <= w)
408 Elements::iterator const beg = elements_.begin();
409 Elements::iterator const end = elements_.end();
410 int wid = left_margin;
412 // Search for the first element that goes beyond right margin
413 Elements::iterator cit = beg;
414 for ( ; cit != end ; ++cit) {
415 if (wid + cit->dim.wid > w)
421 // This should not happen since the row is too long.
422 LYXERR0("Something is wrong cannot shorten row: " << *this);
426 // Iterate backwards over breakable elements and try to break them
427 Elements::iterator cit_brk = cit;
428 int wid_brk = wid + cit_brk->dim.wid;
430 while (cit_brk != beg) {
432 // make a copy of the element to work on it.
433 Element brk = *cit_brk;
434 wid_brk -= brk.dim.wid;
435 if (brk.countSeparators() == 0 || brk.pos < keep)
437 /* We have found a suitable separable element. This is the common case.
438 * Try to break it cleanly (at word boundary) at a length that is both
439 * - less than the available space on the row
440 * - shorter than the natural width of the element, in order to enforce
443 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
444 /* if this element originally did not cause a row overflow
445 * in itself, and the remainder of the row would still be
446 * too large after breaking, then we will have issues in
447 * next row. Thus breaking does not help.
449 if (wid_brk + cit_brk->dim.wid < w
450 && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
454 /* after breakAt, there may be spaces at the end of the
455 * string, but they are not counted in the string length
456 * (QTextLayout feature, actually). We remove them, but do
457 * not change the endo of the row, since the spaces at row
458 * break are invisible.
460 brk.str = rtrim(brk.str);
461 brk.endpos = brk.pos + brk.str.length();
463 dim_.wid = wid_brk + brk.dim.wid;
464 // If there are other elements, they should be removed.
465 elements_.erase(cit_brk + 1, end);
470 if (cit != beg && cit->type == VIRTUAL) {
471 // It is not possible to separate a virtual element from the
478 // There is no usable separator, but several elements have
479 // been added. We can cut right here.
482 elements_.erase(cit, end);
486 /* If we are here, it means that we have not found a separator to
487 * shorten the row. Let's try to break it again, but not at word
488 * boundary this time.
490 if (cit->breakAt(w - wid, true)) {
492 // See comment above.
493 cit->str = rtrim(cit->str);
494 cit->endpos = cit->pos + cit->str.length();
495 dim_.wid = wid + cit->dim.wid;
496 // If there are other elements, they should be removed.
497 elements_.erase(next(cit, 1), end);
502 void Row::reverseRTL(bool const rtl_par)
505 pos_type const end = elements_.size();
507 // gather a sequence of elements with the same direction
508 bool const rtl = elements_[i].isRTL();
510 while (j < end && elements_[j].isRTL() == rtl)
512 // if the direction is not the same as the paragraph
513 // direction, the sequence has to be reverted.
515 reverse(elements_.begin() + i, elements_.begin() + j);
518 // If the paragraph itself is RTL, reverse everything
520 reverse(elements_.begin(), elements_.end());