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 == pos || type != STRING)
51 w = rtl ? width() : 0;
53 w = rtl ? 0 : width();
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 << "\n";
255 Row::Elements::const_iterator it = row.elements_.begin();
256 for ( ; it != row.elements_.end() ; ++it) {
257 os << "x=" << x << " => " << *it << endl;
264 bool Row::sameString(Font const & f, Change const & ch) const
266 if (elements_.empty())
268 Element const & elt = elements_.back();
269 return elt.type == STRING && !elt.final
270 && elt.font == f && elt.change == ch;
274 void Row::finalizeLast()
276 if (elements_.empty())
278 Element & elt = elements_.back();
283 if (elt.type == STRING) {
284 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
285 dim_.wid += elt.dim.wid;
290 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
291 Font const & f, Change const & ch)
294 Element e(INSET, pos, f, ch);
297 elements_.push_back(e);
302 void Row::add(pos_type const pos, char_type const c,
303 Font const & f, Change const & ch)
305 if (!sameString(f, ch)) {
307 Element e(STRING, pos, f, ch);
308 elements_.push_back(e);
311 back().endpos = pos + 1;
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)
364 /** First, we try to remove elements one by one from the end
365 * until a separator is found. cit points to the first element
366 * we want to remove from the row.
368 Elements::iterator const beg = elements_.begin();
369 Elements::iterator const end = elements_.end();
370 Elements::iterator cit = end;
371 Elements::iterator first_below = end;
373 int new_wid = dim_.wid;
374 // if the row ends with a separator, skip it.
375 if (cit != beg && boost::prior(cit)->type == SEPARATOR && new_end > keep) {
378 new_wid -= cit->dim.wid;
381 // Search for a separator where the row can be broken.
382 while (cit != beg && boost::prior(cit)->type != SEPARATOR && new_end > keep) {
385 new_wid -= cit->dim.wid;
386 if (new_wid < w && first_below == end)
391 // We have found a suitable separator. This is the
395 elements_.erase(cit, end);
399 /* If we are here, it means that we have not found a separator
400 * to shorten the row. There is one case where we can do
401 * something: when we have one big string, maybe with some
402 * other things after it.
404 double max_w = w - x;
405 if (first_below->breakAt(max_w)) {
406 end_ = first_below->endpos;
407 dim_.wid = int(x + first_below->width());
408 // If there are other elements, they should be removed.
409 elements_.erase(boost::next(first_below), end);
410 } else if (first_below->pos > pos_) {
411 end_ = first_below->pos;
413 // Remove all elements from first_below.
414 elements_.erase(first_below, end);
419 void Row::reverseRTL(bool const rtl_par)
422 pos_type const end = elements_.size();
424 // gather a sequence of elements with the same direction
425 bool const rtl = elements_[i].font.isVisibleRightToLeft();
427 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
429 // if the direction is not the same as the paragraph
430 // direction, the sequence has to be reverted.
432 reverse(elements_.begin() + i, elements_.begin() + j);
435 // If the paragraph itself is RTL, reverse everything
437 reverse(elements_.begin(), elements_.end());