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 w = isRTL() ? 0 : full_width();
71 else if (i == pos || type != STRING)
72 w = isRTL() ? full_width() : 0;
74 FontMetrics const & fm = theFontMetrics(font);
75 w = fm.pos2x(str, i - pos, isRTL(), extra);
82 pos_type Row::Element::x2pos(int &x) const
84 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
89 FontMetrics const & fm = theFontMetrics(font);
90 i = fm.x2pos(str, x, isRTL(), extra);
94 // those elements are actually empty (but they have a width)
96 x = isRTL() ? int(full_width()) : 0;
100 // those elements contain only one position. Round to
102 if (x > full_width()) {
103 x = int(full_width());
111 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
117 bool Row::Element::breakAt(int w, bool force)
119 if (type != STRING || dim.wid <= w)
122 FontMetrics const & fm = theFontMetrics(font);
124 if(fm.breakAt(str, x, isRTL(), force)) {
126 endpos = pos + str.length();
127 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
135 pos_type Row::Element::left_pos() const
137 return isRTL() ? endpos : pos;
141 pos_type Row::Element::right_pos() const
143 return isRTL() ? pos : endpos;
148 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
149 sel_beg(-1), sel_end(-1),
150 begin_margin_sel(false), end_margin_sel(false),
151 changed_(false), crc_(0),
152 pit_(0), pos_(0), end_(0), right_boundary_(false)
156 void Row::setCrc(size_type crc) const
158 changed_ = crc != crc_;
163 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
164 DocIterator const & end) const
166 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
167 pos_type const margin_pos = left_margin ? pos_ : end_;
169 // Is the chosen margin selected ?
170 if (sel_pos == margin_pos) {
171 if (beg.pos() == end.pos())
172 // This is a special case in which the space between after
173 // pos i-1 and before pos i is selected, i.e. the margins
174 // (see DocIterator::boundary_).
175 return beg.boundary() && !end.boundary();
176 else if (end.pos() == margin_pos)
177 // If the selection ends around the margin, it is only
178 // drawn if the cursor is after the margin.
179 return !end.boundary();
180 else if (beg.pos() == margin_pos)
181 // If the selection begins around the margin, it is
182 // only drawn if the cursor is before the margin.
183 return beg.boundary();
191 void Row::setSelectionAndMargins(DocIterator const & beg,
192 DocIterator const & end) const
194 setSelection(beg.pos(), end.pos());
197 end_margin_sel = isMarginSelected(false, beg, end);
198 begin_margin_sel = isMarginSelected(true, beg, end);
203 void Row::setSelection(pos_type beg, pos_type end) const
205 if (pos_ >= beg && pos_ <= end)
207 else if (beg > pos_ && beg <= end_)
212 if (end_ >= beg && end_ <= end)
214 else if (end < end_ && end >= pos_)
221 bool Row::selection() const
223 return sel_beg != -1 && sel_end != -1;
227 ostream & operator<<(ostream & os, Row::Element const & e)
230 os << e.endpos << "<<" << e.pos << " ";
232 os << e.pos << ">>" << e.endpos << " ";
236 os << "STRING: `" << to_utf8(e.str) << "' ("
237 << e.countSeparators() << " sep.), ";
240 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
243 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
249 os << "width=" << e.full_width();
254 ostream & operator<<(ostream & os, Row const & row)
256 os << " pos: " << row.pos_ << " end: " << row.end_
257 << " left_margin: " << row.left_margin
258 << " width: " << row.dim_.wid
259 << " right_margin: " << row.right_margin
260 << " ascent: " << row.dim_.asc
261 << " descent: " << row.dim_.des
262 << " separator: " << row.separator
263 << " label_hfill: " << row.label_hfill
264 << " row_boundary: " << row.right_boundary() << "\n";
265 double x = row.left_margin;
266 Row::Elements::const_iterator it = row.elements_.begin();
267 for ( ; it != row.elements_.end() ; ++it) {
268 os << "x=" << x << " => " << *it << endl;
269 x += it->full_width();
275 int Row::left_x() const
277 double x = left_margin;
278 const_iterator const end = elements_.end();
279 const_iterator cit = elements_.begin();
280 while (cit != end && cit->isVirtual()) {
281 x += cit->full_width();
288 int Row::right_x() const
291 const_iterator const begin = elements_.begin();
292 const_iterator cit = elements_.end();
293 while (cit != begin) {
295 if (cit->isVirtual())
296 x -= cit->full_width();
304 int Row::countSeparators() const
307 const_iterator const end = elements_.end();
308 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
309 n += cit->countSeparators();
314 void Row::setSeparatorExtraWidth(double w)
317 iterator const end = elements_.end();
318 for (iterator it = elements_.begin() ; it != end ; ++it)
319 if (it->type == Row::STRING)
324 bool Row::sameString(Font const & f, Change const & ch) const
326 if (elements_.empty())
328 Element const & elt = elements_.back();
329 return elt.type == STRING && !elt.final
330 && elt.font == f && elt.change == ch;
334 void Row::finalizeLast()
336 if (elements_.empty())
338 Element & elt = elements_.back();
343 if (elt.type == STRING) {
344 dim_.wid -= elt.dim.wid;
345 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
346 dim_.wid += elt.dim.wid;
351 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
352 Font const & f, Change const & ch)
355 Element e(INSET, pos, f, ch);
358 elements_.push_back(e);
363 void Row::add(pos_type const pos, char_type const c,
364 Font const & f, Change const & ch)
366 if (!sameString(f, ch)) {
368 Element e(STRING, pos, f, ch);
369 elements_.push_back(e);
371 if (back().str.length() % 30 == 0) {
372 dim_.wid -= back().dim.wid;
374 back().endpos = pos + 1;
375 back().dim.wid = theFontMetrics(back().font).width(back().str);
376 dim_.wid += back().dim.wid;
379 back().endpos = pos + 1;
384 void Row::addVirtual(pos_type const pos, docstring const & s,
385 Font const & f, Change const & ch)
388 Element e(VIRTUAL, pos, f, ch);
390 e.dim.wid = theFontMetrics(f).width(s);
391 dim_.wid += e.dim.wid;
393 elements_.push_back(e);
398 void Row::addSpace(pos_type const pos, int const width,
399 Font const & f, Change const & ch)
402 Element e(SPACE, pos, f, ch);
404 elements_.push_back(e);
405 dim_.wid += e.dim.wid;
411 dim_.wid -= elements_.back().dim.wid;
412 elements_.pop_back();
416 bool Row::shortenIfNeeded(pos_type const keep, int const w)
418 if (empty() || width() <= w)
421 Elements::iterator const beg = elements_.begin();
422 Elements::iterator const end = elements_.end();
423 int wid = left_margin;
425 // Search for the first element that goes beyond right margin
426 Elements::iterator cit = beg;
427 for ( ; cit != end ; ++cit) {
428 if (wid + cit->dim.wid > w)
434 // This should not happen since the row is too long.
435 LYXERR0("Something is wrong cannot shorten row: " << *this);
439 // Iterate backwards over breakable elements and try to break them
440 Elements::iterator cit_brk = cit;
441 int wid_brk = wid + cit_brk->dim.wid;
443 while (cit_brk != beg) {
445 // make a copy of the element to work on it.
446 Element brk = *cit_brk;
447 wid_brk -= brk.dim.wid;
448 if (brk.countSeparators() == 0 || brk.pos < keep)
450 /* We have found a suitable separable element. This is the common case.
451 * Try to break it cleanly (at word boundary) at a length that is both
452 * - less than the available space on the row
453 * - shorter than the natural width of the element, in order to enforce
456 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
457 /* if this element originally did not cause a row overflow
458 * in itself, and the remainder of the row would still be
459 * too large after breaking, then we will have issues in
460 * next row. Thus breaking does not help.
462 if (wid_brk + cit_brk->dim.wid < w
463 && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
467 /* after breakAt, there may be spaces at the end of the
468 * string, but they are not counted in the string length
469 * (QTextLayout feature, actually). We remove them, but do
470 * not change the end of the row, since spaces at row
471 * break are invisible.
473 brk.str = rtrim(brk.str);
474 brk.endpos = brk.pos + brk.str.length();
476 dim_.wid = wid_brk + brk.dim.wid;
477 // If there are other elements, they should be removed.
478 elements_.erase(cit_brk + 1, end);
483 if (cit != beg && cit->type == VIRTUAL) {
484 // It is not possible to separate a virtual element from the
491 // There is no usable separator, but several elements have
492 // been added. We can cut right here.
495 elements_.erase(cit, end);
499 /* If we are here, it means that we have not found a separator to
500 * shorten the row. Let's try to break it again, but not at word
501 * boundary this time.
503 if (cit->breakAt(w - wid, true)) {
505 // See comment above.
506 cit->str = rtrim(cit->str);
507 cit->endpos = cit->pos + cit->str.length();
508 dim_.wid = wid + cit->dim.wid;
509 // If there are other elements, they should be removed.
510 elements_.erase(next(cit, 1), end);
517 void Row::reverseRTL(bool const rtl_par)
520 pos_type const end = elements_.size();
522 // gather a sequence of elements with the same direction
523 bool const rtl = elements_[i].isRTL();
525 while (j < end && elements_[j].isRTL() == rtl)
527 // if the direction is not the same as the paragraph
528 // direction, the sequence has to be reverted.
530 reverse(elements_.begin() + i, elements_.begin() + j);
533 // If the paragraph itself is RTL, reverse everything
535 reverse(elements_.begin(), elements_.end());