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;
39 double Row::Element::pos2x(pos_type const i) const
41 // This can happen with inline completion when clicking on the
42 // row after the completion.
43 if (i < pos || i > endpos)
46 bool const rtl = font.isVisibleRightToLeft();
49 //handle first the two bounds of the element
50 if (i == endpos && type != VIRTUAL
51 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
52 w = rtl ? 0 : full_width();
53 else if (i == pos || type != STRING)
54 w = rtl ? full_width() : 0;
56 FontMetrics const & fm = theFontMetrics(font);
57 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft());
64 pos_type Row::Element::x2pos(int &x) const
66 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
67 bool const rtl = font.isVisibleRightToLeft();
72 FontMetrics const & fm = theFontMetrics(font);
73 i = fm.x2pos(str, x, rtl);
77 // those elements are actually empty (but they have a width)
79 x = rtl ? int(full_width()) : 0;
84 // those elements contain only one position. Round to
86 if (x > full_width()) {
87 x = int(full_width());
95 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
101 bool Row::Element::breakAt(int w, bool force)
103 if (type != STRING || dim.wid <= w)
106 bool const rtl = font.isVisibleRightToLeft();
107 FontMetrics const & fm = theFontMetrics(font);
109 if(fm.breakAt(str, x, rtl, force)) {
111 endpos = pos + str.length();
112 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
119 pos_type Row::Element::left_pos() const
121 return font.isVisibleRightToLeft() ? endpos : pos;
125 pos_type Row::Element::right_pos() const
127 return font.isVisibleRightToLeft() ? pos : endpos;
132 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
133 sel_beg(-1), sel_end(-1),
134 begin_margin_sel(false), end_margin_sel(false),
135 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
139 void Row::setCrc(size_type crc) const
141 changed_ = crc != crc_;
146 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
147 DocIterator const & end) const
149 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
150 pos_type const margin_pos = left_margin ? pos_ : end_;
152 // Is the chosen margin selected ?
153 if (sel_pos == margin_pos) {
154 if (beg.pos() == end.pos())
155 // This is a special case in which the space between after
156 // pos i-1 and before pos i is selected, i.e. the margins
157 // (see DocIterator::boundary_).
158 return beg.boundary() && !end.boundary();
159 else if (end.pos() == margin_pos)
160 // If the selection ends around the margin, it is only
161 // drawn if the cursor is after the margin.
162 return !end.boundary();
163 else if (beg.pos() == margin_pos)
164 // If the selection begins around the margin, it is
165 // only drawn if the cursor is before the margin.
166 return beg.boundary();
174 void Row::setSelectionAndMargins(DocIterator const & beg,
175 DocIterator const & end) const
177 setSelection(beg.pos(), end.pos());
180 end_margin_sel = isMarginSelected(false, beg, end);
181 begin_margin_sel = isMarginSelected(true, beg, end);
186 void Row::setSelection(pos_type beg, pos_type end) const
188 if (pos_ >= beg && pos_ <= end)
190 else if (beg > pos_ && beg <= end_)
195 if (end_ >= beg && end_ <= end)
197 else if (end < end_ && end >= pos_)
204 bool Row::selection() const
206 return sel_beg != -1 && sel_end != -1;
210 ostream & operator<<(ostream & os, Row::Element const & e)
212 if (e.font.isVisibleRightToLeft())
213 os << e.endpos << "<<" << e.pos << " ";
215 os << e.pos << ">>" << e.endpos << " ";
219 os << "STRING: `" << to_utf8(e.str) << "', ";
222 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
225 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
228 os << "SEPARATOR: extra=" << e.extra << ", ";
234 os << "width=" << e.full_width();
239 ostream & operator<<(ostream & os, Row const & row)
241 os << " pos: " << row.pos_ << " end: " << row.end_
242 << " left_margin: " << row.left_margin
243 << " width: " << row.dim_.wid
244 << " right_margin: " << row.right_margin
245 << " ascent: " << row.dim_.asc
246 << " descent: " << row.dim_.des
247 << " separator: " << row.separator
248 << " label_hfill: " << row.label_hfill
249 << " row_boundary: " << row.right_boundary() << "\n";
250 double x = row.left_margin;
251 Row::Elements::const_iterator it = row.elements_.begin();
252 for ( ; it != row.elements_.end() ; ++it) {
253 os << "x=" << x << " => " << *it << endl;
254 x += it->full_width();
260 bool Row::sameString(Font const & f, Change const & ch) const
262 if (elements_.empty())
264 Element const & elt = elements_.back();
265 return elt.type == STRING && !elt.final
266 && elt.font == f && elt.change == ch;
270 void Row::finalizeLast()
272 if (elements_.empty())
274 Element & elt = elements_.back();
279 if (elt.type == STRING) {
280 dim_.wid -= elt.dim.wid;
281 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
282 dim_.wid += elt.dim.wid;
287 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
288 Font const & f, Change const & ch)
291 Element e(INSET, pos, f, ch);
294 elements_.push_back(e);
299 void Row::add(pos_type const pos, char_type const c,
300 Font const & f, Change const & ch)
302 if (!sameString(f, ch)) {
304 Element e(STRING, pos, f, ch);
305 elements_.push_back(e);
307 dim_.wid -= back().dim.wid;
309 back().endpos = pos + 1;
310 back().dim.wid = theFontMetrics(back().font).width(back().str);
311 dim_.wid += back().dim.wid;
315 void Row::addVirtual(pos_type const pos, docstring const & s,
316 Font const & f, Change const & ch)
319 Element e(VIRTUAL, pos, f, ch);
321 e.dim.wid = theFontMetrics(f).width(s);
322 dim_.wid += e.dim.wid;
324 elements_.push_back(e);
329 void Row::addSeparator(pos_type const pos, char_type const c,
330 Font const & f, Change const & ch)
333 Element e(SEPARATOR, pos, f, ch);
335 e.dim.wid = theFontMetrics(f).width(c);
336 elements_.push_back(e);
337 dim_.wid += e.dim.wid;
341 void Row::addSpace(pos_type const pos, int const width,
342 Font const & f, Change const & ch)
345 Element e(SPACE, pos, f, ch);
347 elements_.push_back(e);
348 dim_.wid += e.dim.wid;
354 dim_.wid -= elements_.back().dim.wid;
355 elements_.pop_back();
359 void Row::shortenIfNeeded(pos_type const keep, int const w)
361 if (empty() || width() <= w)
363 Elements::iterator const beg = elements_.begin();
364 Elements::iterator const end = elements_.end();
365 int wid = left_margin;
367 Elements::iterator cit = beg;
368 for ( ; cit != end ; ++cit) {
369 if (cit->endpos >= keep && wid + cit->dim.wid > w)
375 // This should not happen since the row is too long.
376 LYXERR0("Something is wrong cannot shorten row: " << *this);
380 if (cit != beg && cit->type == VIRTUAL) {
381 // It is not possible to separate a virtual element from the
387 // Try to break this row cleanly (at word boundary)
388 if (cit->breakAt(w - wid, false)) {
390 // after breakAt, there may be spaces at the end of the
391 // string, but they are not counted in the string length
392 // (qtextlayout feature, actually). We remove them, but do not
393 // change the endo of the row, since the spaces at row break
395 cit->str = rtrim(cit->str);
396 cit->endpos = cit->pos + cit->str.length();
397 dim_.wid = wid + cit->dim.wid;
398 // If there are other elements, they should be removed.
399 elements_.erase(next(cit, 1), end);
404 // There is no separator, but several elements have been
405 // added. We can cut right here.
408 elements_.erase(cit, end);
412 /* If we are here, it means that we have not found a separator to
413 * shorten the row. Let's try to break it again, but not at word
414 * boundary this time.
416 if (cit->breakAt(w - wid, true)) {
418 // See comment above.
419 cit->str = rtrim(cit->str);
420 cit->endpos = cit->pos + cit->str.length();
421 dim_.wid = wid + cit->dim.wid;
422 // If there are other elements, they should be removed.
423 elements_.erase(next(cit, 1), end);
428 void Row::reverseRTL(bool const rtl_par)
431 pos_type const end = elements_.size();
433 // gather a sequence of elements with the same direction
434 bool const rtl = elements_[i].font.isVisibleRightToLeft();
436 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
438 // if the direction is not the same as the paragraph
439 // direction, the sequence has to be reverted.
441 reverse(elements_.begin() + i, elements_.begin() + j);
444 // If the paragraph itself is RTL, reverse everything
446 reverse(elements_.begin(), elements_.end());