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)
55 bool const rtl = font.isVisibleRightToLeft();
58 //handle first the two bounds of the element
59 if (i == endpos && type != VIRTUAL
60 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
61 w = rtl ? 0 : full_width();
62 else if (i == pos || type != STRING)
63 w = rtl ? full_width() : 0;
65 FontMetrics const & fm = theFontMetrics(font);
66 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft(), extra);
73 pos_type Row::Element::x2pos(int &x) const
75 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
76 bool const rtl = font.isVisibleRightToLeft();
81 FontMetrics const & fm = theFontMetrics(font);
82 i = fm.x2pos(str, x, rtl, extra);
86 // those elements are actually empty (but they have a width)
88 x = rtl ? int(full_width()) : 0;
92 // those elements contain only one position. Round to
94 if (x > full_width()) {
95 x = int(full_width());
103 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
109 bool Row::Element::breakAt(int w, bool force)
111 if (type != STRING || dim.wid <= w)
114 bool const rtl = font.isVisibleRightToLeft();
115 FontMetrics const & fm = theFontMetrics(font);
117 if(fm.breakAt(str, x, rtl, force)) {
119 endpos = pos + str.length();
120 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
127 pos_type Row::Element::left_pos() const
129 return font.isVisibleRightToLeft() ? endpos : pos;
133 pos_type Row::Element::right_pos() const
135 return font.isVisibleRightToLeft() ? pos : endpos;
140 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
141 sel_beg(-1), sel_end(-1),
142 begin_margin_sel(false), end_margin_sel(false),
143 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
147 void Row::setCrc(size_type crc) const
149 changed_ = crc != crc_;
154 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
155 DocIterator const & end) const
157 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
158 pos_type const margin_pos = left_margin ? pos_ : end_;
160 // Is the chosen margin selected ?
161 if (sel_pos == margin_pos) {
162 if (beg.pos() == end.pos())
163 // This is a special case in which the space between after
164 // pos i-1 and before pos i is selected, i.e. the margins
165 // (see DocIterator::boundary_).
166 return beg.boundary() && !end.boundary();
167 else if (end.pos() == margin_pos)
168 // If the selection ends around the margin, it is only
169 // drawn if the cursor is after the margin.
170 return !end.boundary();
171 else if (beg.pos() == margin_pos)
172 // If the selection begins around the margin, it is
173 // only drawn if the cursor is before the margin.
174 return beg.boundary();
182 void Row::setSelectionAndMargins(DocIterator const & beg,
183 DocIterator const & end) const
185 setSelection(beg.pos(), end.pos());
188 end_margin_sel = isMarginSelected(false, beg, end);
189 begin_margin_sel = isMarginSelected(true, beg, end);
194 void Row::setSelection(pos_type beg, pos_type end) const
196 if (pos_ >= beg && pos_ <= end)
198 else if (beg > pos_ && beg <= end_)
203 if (end_ >= beg && end_ <= end)
205 else if (end < end_ && end >= pos_)
212 bool Row::selection() const
214 return sel_beg != -1 && sel_end != -1;
218 ostream & operator<<(ostream & os, Row::Element const & e)
220 if (e.font.isVisibleRightToLeft())
221 os << e.endpos << "<<" << e.pos << " ";
223 os << e.pos << ">>" << e.endpos << " ";
227 os << "STRING: `" << to_utf8(e.str) << "' ("
228 << e.countSeparators() << " sep.), ";
231 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
234 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
240 os << "width=" << e.full_width();
245 ostream & operator<<(ostream & os, Row const & row)
247 os << " pos: " << row.pos_ << " end: " << row.end_
248 << " left_margin: " << row.left_margin
249 << " width: " << row.dim_.wid
250 << " right_margin: " << row.right_margin
251 << " ascent: " << row.dim_.asc
252 << " descent: " << row.dim_.des
253 << " separator: " << row.separator
254 << " label_hfill: " << row.label_hfill
255 << " row_boundary: " << row.right_boundary() << "\n";
256 double x = row.left_margin;
257 Row::Elements::const_iterator it = row.elements_.begin();
258 for ( ; it != row.elements_.end() ; ++it) {
259 os << "x=" << x << " => " << *it << endl;
260 x += it->full_width();
266 int Row::countSeparators() const
269 const_iterator const end = elements_.end();
270 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
271 n += cit->countSeparators();
276 void Row::setSeparatorExtraWidth(double w)
279 iterator const end = elements_.end();
280 for (iterator it = elements_.begin() ; it != end ; ++it)
281 if (it->type == Row::STRING)
286 bool Row::sameString(Font const & f, Change const & ch) const
288 if (elements_.empty())
290 Element const & elt = elements_.back();
291 return elt.type == STRING && !elt.final
292 && elt.font == f && elt.change == ch;
296 void Row::finalizeLast()
298 if (elements_.empty())
300 Element & elt = elements_.back();
305 if (elt.type == STRING) {
306 dim_.wid -= elt.dim.wid;
307 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
308 dim_.wid += elt.dim.wid;
313 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
314 Font const & f, Change const & ch)
317 Element e(INSET, pos, f, ch);
320 elements_.push_back(e);
325 void Row::add(pos_type const pos, char_type const c,
326 Font const & f, Change const & ch)
328 if (!sameString(f, ch)) {
330 Element e(STRING, pos, f, ch);
331 elements_.push_back(e);
333 if (back().str.length() % 30 == 0) {
334 dim_.wid -= back().dim.wid;
336 back().endpos = pos + 1;
337 back().dim.wid = theFontMetrics(back().font).width(back().str);
338 dim_.wid += back().dim.wid;
341 back().endpos = pos + 1;
346 void Row::addVirtual(pos_type const pos, docstring const & s,
347 Font const & f, Change const & ch)
350 Element e(VIRTUAL, pos, f, ch);
352 e.dim.wid = theFontMetrics(f).width(s);
353 dim_.wid += e.dim.wid;
355 elements_.push_back(e);
360 void Row::addSpace(pos_type const pos, int const width,
361 Font const & f, Change const & ch)
364 Element e(SPACE, pos, f, ch);
366 elements_.push_back(e);
367 dim_.wid += e.dim.wid;
373 dim_.wid -= elements_.back().dim.wid;
374 elements_.pop_back();
378 void Row::shortenIfNeeded(pos_type const keep, int const w)
380 if (empty() || width() <= w)
383 Elements::iterator const beg = elements_.begin();
384 Elements::iterator const end = elements_.end();
385 int wid = left_margin;
387 // Search for the first element that goes beyond right margin
388 Elements::iterator cit = beg;
389 for ( ; cit != end ; ++cit) {
390 if (wid + cit->dim.wid > w)
396 // This should not happen since the row is too long.
397 LYXERR0("Something is wrong cannot shorten row: " << *this);
401 // Iterate backwards over breakable elements and try to break them
402 Elements::iterator cit_brk = cit;
403 int wid_brk = wid + cit_brk->dim.wid;
405 while (cit_brk != beg) {
407 Element & brk = *cit_brk;
408 wid_brk -= brk.dim.wid;
409 if (brk.countSeparators() == 0 || brk.pos < keep)
411 /* We have found a suitable separable element. This is the common case.
412 * Try to break it cleanly (at word boundary) at a length that is both
413 * - less than the available space on the row
414 * - shorter than the natural width of the element, in order to enforce
417 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
419 /* after breakAt, there may be spaces at the end of the
420 * string, but they are not counted in the string length
421 * (QTextLayout feature, actually). We remove them, but do
422 * not change the endo of the row, since the spaces at row
423 * break are invisible.
425 brk.str = rtrim(brk.str);
426 brk.endpos = brk.pos + brk.str.length();
427 dim_.wid = wid_brk + brk.dim.wid;
428 // If there are other elements, they should be removed.
429 elements_.erase(cit_brk + 1, end);
434 if (cit != beg && cit->type == VIRTUAL) {
435 // It is not possible to separate a virtual element from the
442 // There is no usable separator, but several elements have
443 // been added. We can cut right here.
446 elements_.erase(cit, end);
450 /* If we are here, it means that we have not found a separator to
451 * shorten the row. Let's try to break it again, but not at word
452 * boundary this time.
454 if (cit->breakAt(w - wid, true)) {
456 // See comment above.
457 cit->str = rtrim(cit->str);
458 cit->endpos = cit->pos + cit->str.length();
459 dim_.wid = wid + cit->dim.wid;
460 // If there are other elements, they should be removed.
461 elements_.erase(next(cit, 1), end);
466 void Row::reverseRTL(bool const rtl_par)
469 pos_type const end = elements_.size();
471 // gather a sequence of elements with the same direction
472 bool const rtl = elements_[i].font.isVisibleRightToLeft();
474 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
476 // if the direction is not the same as the paragraph
477 // direction, the sequence has to be reverted.
479 reverse(elements_.begin() + i, elements_.begin() + j);
482 // If the paragraph itself is RTL, reverse everything
484 reverse(elements_.begin(), elements_.end());