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::countSeparators() const
265 const_iterator const end = elements_.end();
266 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
267 n += cit->countSeparators();
272 void Row::setSeparatorExtraWidth(double w)
275 iterator const end = elements_.end();
276 for (iterator it = elements_.begin() ; it != end ; ++it)
277 if (it->type == Row::STRING)
282 bool Row::sameString(Font const & f, Change const & ch) const
284 if (elements_.empty())
286 Element const & elt = elements_.back();
287 return elt.type == STRING && !elt.final
288 && elt.font == f && elt.change == ch;
292 void Row::finalizeLast()
294 if (elements_.empty())
296 Element & elt = elements_.back();
301 if (elt.type == STRING) {
302 dim_.wid -= elt.dim.wid;
303 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
304 dim_.wid += elt.dim.wid;
309 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
310 Font const & f, Change const & ch)
313 Element e(INSET, pos, f, ch);
316 elements_.push_back(e);
321 void Row::add(pos_type const pos, char_type const c,
322 Font const & f, Change const & ch)
324 if (!sameString(f, ch)) {
326 Element e(STRING, pos, f, ch);
327 elements_.push_back(e);
329 if (back().str.length() % 30 == 0) {
330 dim_.wid -= back().dim.wid;
332 back().endpos = pos + 1;
333 back().dim.wid = theFontMetrics(back().font).width(back().str);
334 dim_.wid += back().dim.wid;
337 back().endpos = pos + 1;
342 void Row::addVirtual(pos_type const pos, docstring const & s,
343 Font const & f, Change const & ch)
346 Element e(VIRTUAL, pos, f, ch);
348 e.dim.wid = theFontMetrics(f).width(s);
349 dim_.wid += e.dim.wid;
351 elements_.push_back(e);
356 void Row::addSpace(pos_type const pos, int const width,
357 Font const & f, Change const & ch)
360 Element e(SPACE, pos, f, ch);
362 elements_.push_back(e);
363 dim_.wid += e.dim.wid;
369 dim_.wid -= elements_.back().dim.wid;
370 elements_.pop_back();
374 void Row::shortenIfNeeded(pos_type const keep, int const w)
376 if (empty() || width() <= w)
379 Elements::iterator const beg = elements_.begin();
380 Elements::iterator const end = elements_.end();
381 int wid = left_margin;
383 // Search for the first element that goes beyond right margin
384 Elements::iterator cit = beg;
385 for ( ; cit != end ; ++cit) {
386 if (wid + cit->dim.wid > w)
392 // This should not happen since the row is too long.
393 LYXERR0("Something is wrong cannot shorten row: " << *this);
397 // Iterate backwards over breakable elements and try to break them
398 Elements::iterator cit_brk = cit;
399 int wid_brk = wid + cit_brk->dim.wid;
401 while (cit_brk != beg) {
403 // make a copy of the element to work on it.
404 Element brk = *cit_brk;
405 wid_brk -= brk.dim.wid;
406 if (brk.countSeparators() == 0 || brk.pos < keep)
408 /* We have found a suitable separable element. This is the common case.
409 * Try to break it cleanly (at word boundary) at a length that is both
410 * - less than the available space on the row
411 * - shorter than the natural width of the element, in order to enforce
414 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
415 /* if this element originally did not cause a row overflow
416 * in itself, and the remainder of the row would still be
417 * too large after breaking, then we will have issues in
418 * next row. Thus breaking does not help.
420 if (wid_brk + cit_brk->dim.wid < w
421 && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
425 /* after breakAt, there may be spaces at the end of the
426 * string, but they are not counted in the string length
427 * (QTextLayout feature, actually). We remove them, but do
428 * not change the endo of the row, since the spaces at row
429 * break are invisible.
431 brk.str = rtrim(brk.str);
432 brk.endpos = brk.pos + brk.str.length();
434 dim_.wid = wid_brk + brk.dim.wid;
435 // If there are other elements, they should be removed.
436 elements_.erase(cit_brk + 1, end);
441 if (cit != beg && cit->type == VIRTUAL) {
442 // It is not possible to separate a virtual element from the
449 // There is no usable separator, but several elements have
450 // been added. We can cut right here.
453 elements_.erase(cit, end);
457 /* If we are here, it means that we have not found a separator to
458 * shorten the row. Let's try to break it again, but not at word
459 * boundary this time.
461 if (cit->breakAt(w - wid, true)) {
463 // See comment above.
464 cit->str = rtrim(cit->str);
465 cit->endpos = cit->pos + cit->str.length();
466 dim_.wid = wid + cit->dim.wid;
467 // If there are other elements, they should be removed.
468 elements_.erase(next(cit, 1), end);
473 void Row::reverseRTL(bool const rtl_par)
476 pos_type const end = elements_.size();
478 // gather a sequence of elements with the same direction
479 bool const rtl = elements_[i].isRTL();
481 while (j < end && elements_[j].isRTL() == rtl)
483 // if the direction is not the same as the paragraph
484 // direction, the sequence has to be reverted.
486 reverse(elements_.begin() + i, elements_.begin() + j);
489 // If the paragraph itself is RTL, reverse everything
491 reverse(elements_.begin(), elements_.end());