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;
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), pos_(0), end_(0), right_boundary_(false)
155 void Row::setCrc(size_type crc) const
157 changed_ = crc != crc_;
162 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
163 DocIterator const & end) const
165 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
166 pos_type const margin_pos = left_margin ? pos_ : end_;
168 // Is the chosen margin selected ?
169 if (sel_pos == margin_pos) {
170 if (beg.pos() == end.pos())
171 // This is a special case in which the space between after
172 // pos i-1 and before pos i is selected, i.e. the margins
173 // (see DocIterator::boundary_).
174 return beg.boundary() && !end.boundary();
175 else if (end.pos() == margin_pos)
176 // If the selection ends around the margin, it is only
177 // drawn if the cursor is after the margin.
178 return !end.boundary();
179 else if (beg.pos() == margin_pos)
180 // If the selection begins around the margin, it is
181 // only drawn if the cursor is before the margin.
182 return beg.boundary();
190 void Row::setSelectionAndMargins(DocIterator const & beg,
191 DocIterator const & end) const
193 setSelection(beg.pos(), end.pos());
196 end_margin_sel = isMarginSelected(false, beg, end);
197 begin_margin_sel = isMarginSelected(true, beg, end);
202 void Row::setSelection(pos_type beg, pos_type end) const
204 if (pos_ >= beg && pos_ <= end)
206 else if (beg > pos_ && beg <= end_)
211 if (end_ >= beg && end_ <= end)
213 else if (end < end_ && end >= pos_)
220 bool Row::selection() const
222 return sel_beg != -1 && sel_end != -1;
226 ostream & operator<<(ostream & os, Row::Element const & e)
229 os << e.endpos << "<<" << e.pos << " ";
231 os << e.pos << ">>" << e.endpos << " ";
235 os << "STRING: `" << to_utf8(e.str) << "' ("
236 << e.countSeparators() << " sep.), ";
239 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
242 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
248 os << "width=" << e.full_width();
253 ostream & operator<<(ostream & os, Row const & row)
255 os << " pos: " << row.pos_ << " end: " << row.end_
256 << " left_margin: " << row.left_margin
257 << " width: " << row.dim_.wid
258 << " right_margin: " << row.right_margin
259 << " ascent: " << row.dim_.asc
260 << " descent: " << row.dim_.des
261 << " separator: " << row.separator
262 << " label_hfill: " << row.label_hfill
263 << " row_boundary: " << row.right_boundary() << "\n";
264 double x = row.left_margin;
265 Row::Elements::const_iterator it = row.elements_.begin();
266 for ( ; it != row.elements_.end() ; ++it) {
267 os << "x=" << x << " => " << *it << endl;
268 x += it->full_width();
274 int Row::left_x() const
276 double x = left_margin;
277 const_iterator const end = elements_.end();
278 const_iterator cit = elements_.begin();
279 while (cit != end && cit->isVirtual()) {
280 x += cit->full_width();
287 int Row::right_x() const
290 const_iterator const begin = elements_.begin();
291 const_iterator cit = elements_.end();
292 while (cit != begin) {
294 if (cit->isVirtual())
295 x -= cit->full_width();
303 int Row::countSeparators() const
306 const_iterator const end = elements_.end();
307 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
308 n += cit->countSeparators();
313 void Row::setSeparatorExtraWidth(double w)
316 iterator const end = elements_.end();
317 for (iterator it = elements_.begin() ; it != end ; ++it)
318 if (it->type == Row::STRING)
323 bool Row::sameString(Font const & f, Change const & ch) const
325 if (elements_.empty())
327 Element const & elt = elements_.back();
328 return elt.type == STRING && !elt.final
329 && elt.font == f && elt.change == ch;
333 void Row::finalizeLast()
335 if (elements_.empty())
337 Element & elt = elements_.back();
342 if (elt.type == STRING) {
343 dim_.wid -= elt.dim.wid;
344 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
345 dim_.wid += elt.dim.wid;
350 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
351 Font const & f, Change const & ch)
354 Element e(INSET, pos, f, ch);
357 elements_.push_back(e);
362 void Row::add(pos_type const pos, char_type const c,
363 Font const & f, Change const & ch)
365 if (!sameString(f, ch)) {
367 Element e(STRING, pos, f, ch);
368 elements_.push_back(e);
370 if (back().str.length() % 30 == 0) {
371 dim_.wid -= back().dim.wid;
373 back().endpos = pos + 1;
374 back().dim.wid = theFontMetrics(back().font).width(back().str);
375 dim_.wid += back().dim.wid;
378 back().endpos = pos + 1;
383 void Row::addVirtual(pos_type const pos, docstring const & s,
384 Font const & f, Change const & ch)
387 Element e(VIRTUAL, pos, f, ch);
389 e.dim.wid = theFontMetrics(f).width(s);
390 dim_.wid += e.dim.wid;
392 elements_.push_back(e);
397 void Row::addSpace(pos_type const pos, int const width,
398 Font const & f, Change const & ch)
401 Element e(SPACE, pos, f, ch);
403 elements_.push_back(e);
404 dim_.wid += e.dim.wid;
410 dim_.wid -= elements_.back().dim.wid;
411 elements_.pop_back();
415 void Row::shortenIfNeeded(pos_type const keep, int const w)
417 if (empty() || width() <= w)
420 Elements::iterator const beg = elements_.begin();
421 Elements::iterator const end = elements_.end();
422 int wid = left_margin;
424 // Search for the first element that goes beyond right margin
425 Elements::iterator cit = beg;
426 for ( ; cit != end ; ++cit) {
427 if (wid + cit->dim.wid > w)
433 // This should not happen since the row is too long.
434 LYXERR0("Something is wrong cannot shorten row: " << *this);
438 // Iterate backwards over breakable elements and try to break them
439 Elements::iterator cit_brk = cit;
440 int wid_brk = wid + cit_brk->dim.wid;
442 while (cit_brk != beg) {
444 // make a copy of the element to work on it.
445 Element brk = *cit_brk;
446 wid_brk -= brk.dim.wid;
447 if (brk.countSeparators() == 0 || brk.pos < keep)
449 /* We have found a suitable separable element. This is the common case.
450 * Try to break it cleanly (at word boundary) at a length that is both
451 * - less than the available space on the row
452 * - shorter than the natural width of the element, in order to enforce
455 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
456 /* if this element originally did not cause a row overflow
457 * in itself, and the remainder of the row would still be
458 * too large after breaking, then we will have issues in
459 * next row. Thus breaking does not help.
461 if (wid_brk + cit_brk->dim.wid < w
462 && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
466 /* after breakAt, there may be spaces at the end of the
467 * string, but they are not counted in the string length
468 * (QTextLayout feature, actually). We remove them, but do
469 * not change the endo of the row, since the spaces at row
470 * break are invisible.
472 brk.str = rtrim(brk.str);
473 brk.endpos = brk.pos + brk.str.length();
475 dim_.wid = wid_brk + brk.dim.wid;
476 // If there are other elements, they should be removed.
477 elements_.erase(cit_brk + 1, end);
482 if (cit != beg && cit->type == VIRTUAL) {
483 // It is not possible to separate a virtual element from the
490 // There is no usable separator, but several elements have
491 // been added. We can cut right here.
494 elements_.erase(cit, end);
498 /* If we are here, it means that we have not found a separator to
499 * shorten the row. Let's try to break it again, but not at word
500 * boundary this time.
502 if (cit->breakAt(w - wid, true)) {
504 // See comment above.
505 cit->str = rtrim(cit->str);
506 cit->endpos = cit->pos + cit->str.length();
507 dim_.wid = wid + cit->dim.wid;
508 // If there are other elements, they should be removed.
509 elements_.erase(next(cit, 1), end);
514 void Row::reverseRTL(bool const rtl_par)
517 pos_type const end = elements_.size();
519 // gather a sequence of elements with the same direction
520 bool const rtl = elements_[i].isRTL();
522 while (j < end && elements_[j].isRTL() == rtl)
524 // if the direction is not the same as the paragraph
525 // direction, the sequence has to be reverted.
527 reverse(elements_.begin() + i, elements_.begin() + j);
530 // If the paragraph itself is RTL, reverse everything
532 reverse(elements_.begin(), elements_.end());