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) << "', ";
230 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
233 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
239 os << "width=" << e.full_width();
244 ostream & operator<<(ostream & os, Row const & row)
246 os << " pos: " << row.pos_ << " end: " << row.end_
247 << " left_margin: " << row.left_margin
248 << " width: " << row.dim_.wid
249 << " right_margin: " << row.right_margin
250 << " ascent: " << row.dim_.asc
251 << " descent: " << row.dim_.des
252 << " separator: " << row.separator
253 << " label_hfill: " << row.label_hfill
254 << " row_boundary: " << row.right_boundary() << "\n";
255 double x = row.left_margin;
256 Row::Elements::const_iterator it = row.elements_.begin();
257 for ( ; it != row.elements_.end() ; ++it) {
258 os << "x=" << x << " => " << *it << endl;
259 x += it->full_width();
265 int Row::countSeparators() const
268 const_iterator const end = elements_.end();
269 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
270 n += cit->countSeparators();
275 void Row::setSeparatorExtraWidth(double w)
278 iterator const end = elements_.end();
279 for (iterator it = elements_.begin() ; it != end ; ++it)
280 if (it->type == Row::STRING)
285 bool Row::sameString(Font const & f, Change const & ch) const
287 if (elements_.empty())
289 Element const & elt = elements_.back();
290 return elt.type == STRING && !elt.final
291 && elt.font == f && elt.change == ch;
295 void Row::finalizeLast()
297 if (elements_.empty())
299 Element & elt = elements_.back();
304 if (elt.type == STRING) {
305 dim_.wid -= elt.dim.wid;
306 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
307 dim_.wid += elt.dim.wid;
312 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
313 Font const & f, Change const & ch)
316 Element e(INSET, pos, f, ch);
319 elements_.push_back(e);
324 void Row::add(pos_type const pos, char_type const c,
325 Font const & f, Change const & ch)
327 if (!sameString(f, ch)) {
329 Element e(STRING, pos, f, ch);
330 elements_.push_back(e);
332 if (back().str.length() % 30 == 0) {
333 dim_.wid -= back().dim.wid;
335 back().endpos = pos + 1;
336 back().dim.wid = theFontMetrics(back().font).width(back().str);
337 dim_.wid += back().dim.wid;
340 back().endpos = pos + 1;
345 void Row::addVirtual(pos_type const pos, docstring const & s,
346 Font const & f, Change const & ch)
349 Element e(VIRTUAL, pos, f, ch);
351 e.dim.wid = theFontMetrics(f).width(s);
352 dim_.wid += e.dim.wid;
354 elements_.push_back(e);
359 void Row::addSpace(pos_type const pos, int const width,
360 Font const & f, Change const & ch)
363 Element e(SPACE, pos, f, ch);
365 elements_.push_back(e);
366 dim_.wid += e.dim.wid;
372 dim_.wid -= elements_.back().dim.wid;
373 elements_.pop_back();
377 void Row::shortenIfNeeded(pos_type const keep, int const w)
379 if (empty() || width() <= w)
381 Elements::iterator const beg = elements_.begin();
382 Elements::iterator const end = elements_.end();
383 int wid = left_margin;
385 // The last breakable element that has been found and its x position.
386 Elements::iterator last_brk = elements_.end();
389 Elements::iterator cit = beg;
390 for ( ; cit != end ; ++cit) {
391 if (cit->countSeparators() && cit->pos >= keep) {
395 if (wid + cit->dim.wid > w)
400 /* We have found a suitable separable element. This is the common case.
401 * Try to break it cleanly (at word boundary) at a length that is both
402 * - less than the available space on the row
403 * - shorter than the natural width of the element, in order to enforce
407 && last_brk->breakAt(min(w - last_wid, last_brk->dim.wid - 2), false)) {
408 end_ = last_brk->endpos;
409 /* after breakAt, there may be spaces at the end of the
410 * string, but they are not counted in the string length
411 * (QTextLayout feature, actually). We remove them, but do not
412 * change the endo of the row, since the spaces at row break
415 last_brk->str = rtrim(last_brk->str);
416 last_brk->endpos = last_brk->pos + last_brk->str.length();
417 dim_.wid = last_wid + last_brk->dim.wid;
418 // If there are other elements, they should be removed.
419 elements_.erase(next(last_brk, 1), end);
424 // This should not happen since the row is too long.
425 LYXERR0("Something is wrong cannot shorten row: " << *this);
429 if (cit != beg && cit->type == VIRTUAL) {
430 // It is not possible to separate a virtual element from the
437 // There is no separator, but several elements have been
438 // added. We can cut right here.
441 elements_.erase(cit, end);
445 /* If we are here, it means that we have not found a separator to
446 * shorten the row. Let's try to break it again, but not at word
447 * boundary this time.
449 if (cit->breakAt(w - wid, true)) {
451 // See comment above.
452 cit->str = rtrim(cit->str);
453 cit->endpos = cit->pos + cit->str.length();
454 dim_.wid = wid + cit->dim.wid;
455 // If there are other elements, they should be removed.
456 elements_.erase(next(cit, 1), end);
461 void Row::reverseRTL(bool const rtl_par)
464 pos_type const end = elements_.size();
466 // gather a sequence of elements with the same direction
467 bool const rtl = elements_[i].font.isVisibleRightToLeft();
469 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
471 // if the direction is not the same as the paragraph
472 // direction, the sequence has to be reverted.
474 reverse(elements_.begin() + i, elements_.begin() + j);
477 // If the paragraph itself is RTL, reverse everything
479 reverse(elements_.begin(), elements_.end());