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"
31 #include <boost/next_prior.hpp>
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 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
51 w = rtl ? 0 : width();
52 else if (i == pos || type != STRING)
53 w = rtl ? width() : 0;
55 FontMetrics const & fm = theFontMetrics(font);
56 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft());
63 pos_type Row::Element::x2pos(double &x) const
65 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66 bool const rtl = font.isVisibleRightToLeft();
71 FontMetrics const & fm = theFontMetrics(font);
72 // FIXME: is it really necessary for x to be a double?
74 i = fm.x2pos(str, xx, rtl);
79 // those elements are actually empty (but they have a width)
81 x = rtl ? width() : 0;
86 // those elements contain only one position. Round to
97 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
103 bool Row::Element::breakAt(double w)
105 if (type != STRING || width() <= w)
108 bool const rtl = font.isVisibleRightToLeft();
111 pos_type new_pos = x2pos(w);
114 str = str.substr(0, new_pos - pos);
124 pos_type Row::Element::left_pos() const
126 return font.isVisibleRightToLeft() ? endpos : pos;
130 pos_type Row::Element::right_pos() const
132 return font.isVisibleRightToLeft() ? pos : endpos;
137 : separator(0), label_hfill(0), x(0), right_margin(0),
138 sel_beg(-1), sel_end(-1),
139 begin_margin_sel(false), end_margin_sel(false),
140 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
144 void Row::setCrc(size_type crc) const
146 changed_ = crc != crc_;
151 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
152 DocIterator const & end) const
154 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
155 pos_type const margin_pos = left_margin ? pos_ : end_;
157 // Is the chosen margin selected ?
158 if (sel_pos == margin_pos) {
159 if (beg.pos() == end.pos())
160 // This is a special case in which the space between after
161 // pos i-1 and before pos i is selected, i.e. the margins
162 // (see DocIterator::boundary_).
163 return beg.boundary() && !end.boundary();
164 else if (end.pos() == margin_pos)
165 // If the selection ends around the margin, it is only
166 // drawn if the cursor is after the margin.
167 return !end.boundary();
168 else if (beg.pos() == margin_pos)
169 // If the selection begins around the margin, it is
170 // only drawn if the cursor is before the margin.
171 return beg.boundary();
179 void Row::setSelectionAndMargins(DocIterator const & beg,
180 DocIterator const & end) const
182 setSelection(beg.pos(), end.pos());
185 end_margin_sel = isMarginSelected(false, beg, end);
186 begin_margin_sel = isMarginSelected(true, beg, end);
191 void Row::setSelection(pos_type beg, pos_type end) const
193 if (pos_ >= beg && pos_ <= end)
195 else if (beg > pos_ && beg <= end_)
200 if (end_ >= beg && end_ <= end)
202 else if (end < end_ && end >= pos_)
209 bool Row::selection() const
211 return sel_beg != -1 && sel_end != -1;
215 ostream & operator<<(ostream & os, Row::Element const & e)
217 if (e.font.isVisibleRightToLeft())
218 os << e.endpos << "<<" << e.pos << " ";
220 os << e.pos << ">>" << e.endpos << " ";
224 os << "STRING: `" << to_utf8(e.str) << "', ";
227 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
230 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
233 os << "SEPARATOR: extra=" << e.extra << ", ";
239 os << "width=" << e.width();
244 ostream & operator<<(ostream & os, Row const & row)
246 os << " pos: " << row.pos_ << " end: " << row.end_
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";
256 Row::Elements::const_iterator it = row.elements_.begin();
257 for ( ; it != row.elements_.end() ; ++it) {
258 os << "x=" << x << " => " << *it << endl;
265 bool Row::sameString(Font const & f, Change const & ch) const
267 if (elements_.empty())
269 Element const & elt = elements_.back();
270 return elt.type == STRING && !elt.final
271 && elt.font == f && elt.change == ch;
275 void Row::finalizeLast()
277 if (elements_.empty())
279 Element & elt = elements_.back();
284 if (elt.type == STRING) {
285 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
286 dim_.wid += elt.dim.wid;
291 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
292 Font const & f, Change const & ch)
295 Element e(INSET, pos, f, ch);
298 elements_.push_back(e);
303 void Row::add(pos_type const pos, char_type const c,
304 Font const & f, Change const & ch)
306 if (!sameString(f, ch)) {
308 Element e(STRING, pos, f, ch);
309 elements_.push_back(e);
312 back().endpos = pos + 1;
316 void Row::addVirtual(pos_type const pos, docstring const & s,
317 Font const & f, Change const & ch)
320 Element e(VIRTUAL, pos, f, ch);
322 e.dim.wid = theFontMetrics(f).width(s);
323 dim_.wid += e.dim.wid;
325 elements_.push_back(e);
330 void Row::addSeparator(pos_type const pos, char_type const c,
331 Font const & f, Change const & ch)
334 Element e(SEPARATOR, pos, f, ch);
336 e.dim.wid = theFontMetrics(f).width(c);
337 elements_.push_back(e);
338 dim_.wid += e.dim.wid;
342 void Row::addSpace(pos_type const pos, int const width,
343 Font const & f, Change const & ch)
346 Element e(SPACE, pos, f, ch);
348 elements_.push_back(e);
349 dim_.wid += e.dim.wid;
355 dim_.wid -= elements_.back().dim.wid;
356 elements_.pop_back();
360 void Row::shortenIfNeeded(pos_type const keep, int const w)
362 if (empty() || width() <= w)
365 Elements::iterator const beg = elements_.begin();
366 Elements::iterator const end = elements_.end();
367 Elements::iterator last_sep = elements_.end();
368 double last_width = 0;
371 Elements::iterator cit = beg;
372 for ( ; cit != end ; ++cit) {
373 if (cit->type == SEPARATOR && cit->pos >= keep) {
377 if (wid + cit->width() > w)
382 if (last_sep != end) {
383 // We have found a suitable separator. This is the
385 end_ = last_sep->endpos;
386 dim_.wid = int(last_width);
387 elements_.erase(last_sep, end);
392 // This should not happen since the row is too long.
393 LYXERR0("Something is wrong cannot shorten row: " << *this);
397 if (cit != beg && cit->type == VIRTUAL) {
398 // It is not possible to separate a virtual element from the
405 // There is no separator, but several elements (probably
406 // insets) have been added. We can cut at this place.
409 elements_.erase(cit, end);
413 /* If we are here, it means that we have not found a separator
414 * to shorten the row. There is one case where we can do
415 * something: when we have one big string, maybe with some
416 * other things after it.
418 if (cit->breakAt(w - x)) {
420 dim_.wid = int(x + cit->width());
421 // If there are other elements, they should be removed.
422 elements_.erase(boost::next(cit), end);
427 void Row::reverseRTL(bool const rtl_par)
430 pos_type const end = elements_.size();
432 // gather a sequence of elements with the same direction
433 bool const rtl = elements_[i].font.isVisibleRightToLeft();
435 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
437 // if the direction is not the same as the paragraph
438 // direction, the sequence has to be reverted.
440 reverse(elements_.begin() + i, elements_.begin() + j);
443 // If the paragraph itself is RTL, reverse everything
445 reverse(elements_.begin(), elements_.end());