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;
93 // those elements contain only one position. Round to
95 if (x > full_width()) {
96 x = int(full_width());
104 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
110 bool Row::Element::breakAt(int w, bool force)
112 if (type != STRING || dim.wid <= w)
115 bool const rtl = font.isVisibleRightToLeft();
116 FontMetrics const & fm = theFontMetrics(font);
118 if(fm.breakAt(str, x, rtl, force)) {
120 endpos = pos + str.length();
121 //lyxerr << "breakAt(" << w << ") Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
128 pos_type Row::Element::left_pos() const
130 return font.isVisibleRightToLeft() ? endpos : pos;
134 pos_type Row::Element::right_pos() const
136 return font.isVisibleRightToLeft() ? pos : endpos;
141 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
142 sel_beg(-1), sel_end(-1),
143 begin_margin_sel(false), end_margin_sel(false),
144 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
148 void Row::setCrc(size_type crc) const
150 changed_ = crc != crc_;
155 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
156 DocIterator const & end) const
158 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
159 pos_type const margin_pos = left_margin ? pos_ : end_;
161 // Is the chosen margin selected ?
162 if (sel_pos == margin_pos) {
163 if (beg.pos() == end.pos())
164 // This is a special case in which the space between after
165 // pos i-1 and before pos i is selected, i.e. the margins
166 // (see DocIterator::boundary_).
167 return beg.boundary() && !end.boundary();
168 else if (end.pos() == margin_pos)
169 // If the selection ends around the margin, it is only
170 // drawn if the cursor is after the margin.
171 return !end.boundary();
172 else if (beg.pos() == margin_pos)
173 // If the selection begins around the margin, it is
174 // only drawn if the cursor is before the margin.
175 return beg.boundary();
183 void Row::setSelectionAndMargins(DocIterator const & beg,
184 DocIterator const & end) const
186 setSelection(beg.pos(), end.pos());
189 end_margin_sel = isMarginSelected(false, beg, end);
190 begin_margin_sel = isMarginSelected(true, beg, end);
195 void Row::setSelection(pos_type beg, pos_type end) const
197 if (pos_ >= beg && pos_ <= end)
199 else if (beg > pos_ && beg <= end_)
204 if (end_ >= beg && end_ <= end)
206 else if (end < end_ && end >= pos_)
213 bool Row::selection() const
215 return sel_beg != -1 && sel_end != -1;
219 ostream & operator<<(ostream & os, Row::Element const & e)
221 if (e.font.isVisibleRightToLeft())
222 os << e.endpos << "<<" << e.pos << " ";
224 os << e.pos << ">>" << e.endpos << " ";
228 os << "STRING: `" << to_utf8(e.str) << "', ";
231 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
234 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
237 os << "SEPARATOR: extra=" << e.extra << ", ";
243 os << "width=" << e.full_width();
248 ostream & operator<<(ostream & os, Row const & row)
250 os << " pos: " << row.pos_ << " end: " << row.end_
251 << " left_margin: " << row.left_margin
252 << " width: " << row.dim_.wid
253 << " right_margin: " << row.right_margin
254 << " ascent: " << row.dim_.asc
255 << " descent: " << row.dim_.des
256 << " separator: " << row.separator
257 << " label_hfill: " << row.label_hfill
258 << " row_boundary: " << row.right_boundary() << "\n";
259 double x = row.left_margin;
260 Row::Elements::const_iterator it = row.elements_.begin();
261 for ( ; it != row.elements_.end() ; ++it) {
262 os << "x=" << x << " => " << *it << endl;
263 x += it->full_width();
269 int Row::countSeparators() const
272 const_iterator const end = elements_.end();
273 for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
274 n += cit->countSeparators();
279 void Row::setSeparatorExtraWidth(double w)
282 iterator const end = elements_.end();
283 for (iterator it = elements_.begin() ; it != end ; ++it)
284 if (it->type == Row::STRING)
289 bool Row::sameString(Font const & f, Change const & ch) const
291 if (elements_.empty())
293 Element const & elt = elements_.back();
294 return elt.type == STRING && !elt.final
295 && elt.font == f && elt.change == ch;
299 void Row::finalizeLast()
301 if (elements_.empty())
303 Element & elt = elements_.back();
308 if (elt.type == STRING) {
309 dim_.wid -= elt.dim.wid;
310 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
311 dim_.wid += elt.dim.wid;
316 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
317 Font const & f, Change const & ch)
320 Element e(INSET, pos, f, ch);
323 elements_.push_back(e);
328 void Row::add(pos_type const pos, char_type const c,
329 Font const & f, Change const & ch)
331 if (!sameString(f, ch)) {
333 Element e(STRING, pos, f, ch);
334 elements_.push_back(e);
336 dim_.wid -= back().dim.wid;
338 back().endpos = pos + 1;
339 back().dim.wid = theFontMetrics(back().font).width(back().str);
340 dim_.wid += back().dim.wid;
344 void Row::addVirtual(pos_type const pos, docstring const & s,
345 Font const & f, Change const & ch)
348 Element e(VIRTUAL, pos, f, ch);
350 e.dim.wid = theFontMetrics(f).width(s);
351 dim_.wid += e.dim.wid;
353 elements_.push_back(e);
358 void Row::addSeparator(pos_type const pos, char_type const c,
359 Font const & f, Change const & ch)
362 Element e(SEPARATOR, pos, f, ch);
364 e.dim.wid = theFontMetrics(f).width(c);
365 elements_.push_back(e);
366 dim_.wid += e.dim.wid;
370 void Row::addSpace(pos_type const pos, int const width,
371 Font const & f, Change const & ch)
374 Element e(SPACE, pos, f, ch);
376 elements_.push_back(e);
377 dim_.wid += e.dim.wid;
383 dim_.wid -= elements_.back().dim.wid;
384 elements_.pop_back();
388 void Row::shortenIfNeeded(pos_type const keep, int const w)
390 if (empty() || width() <= w)
392 Elements::iterator const beg = elements_.begin();
393 Elements::iterator const end = elements_.end();
394 int wid = left_margin;
396 Elements::iterator cit = beg;
397 for ( ; cit != end ; ++cit) {
398 if (cit->endpos >= keep && wid + cit->dim.wid > w)
404 // This should not happen since the row is too long.
405 LYXERR0("Something is wrong cannot shorten row: " << *this);
409 if (cit != beg && cit->type == VIRTUAL) {
410 // It is not possible to separate a virtual element from the
416 // Try to break this row cleanly (at word boundary)
417 if (cit->breakAt(w - wid, 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 not
422 // change the endo of the row, since the spaces at row break
424 cit->str = rtrim(cit->str);
425 cit->endpos = cit->pos + cit->str.length();
426 dim_.wid = wid + cit->dim.wid;
427 // If there are other elements, they should be removed.
428 elements_.erase(next(cit, 1), end);
433 // There is no separator, but several elements have been
434 // added. We can cut right here.
437 elements_.erase(cit, end);
441 /* If we are here, it means that we have not found a separator to
442 * shorten the row. Let's try to break it again, but not at word
443 * boundary this time.
445 if (cit->breakAt(w - wid, true)) {
447 // See comment above.
448 cit->str = rtrim(cit->str);
449 cit->endpos = cit->pos + cit->str.length();
450 dim_.wid = wid + cit->dim.wid;
451 // If there are other elements, they should be removed.
452 elements_.erase(next(cit, 1), end);
457 void Row::reverseRTL(bool const rtl_par)
460 pos_type const end = elements_.size();
462 // gather a sequence of elements with the same direction
463 bool const rtl = elements_[i].font.isVisibleRightToLeft();
465 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
467 // if the direction is not the same as the paragraph
468 // direction, the sequence has to be reverted.
470 reverse(elements_.begin() + i, elements_.begin() + j);
473 // If the paragraph itself is RTL, reverse everything
475 reverse(elements_.begin(), elements_.end());