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 /** First, we try to remove elements one by one from the end
366 * until a separator is found. cit points to the first element
367 * we want to remove from the row.
369 Elements::iterator const beg = elements_.begin();
370 Elements::iterator const end = elements_.end();
371 Elements::iterator cit = end;
372 Elements::iterator first_below = end;
374 int new_wid = dim_.wid;
375 // if the row ends with a separator, skip it.
376 if (cit != beg && boost::prior(cit)->type == SEPARATOR && new_end > keep) {
379 new_wid -= cit->dim.wid;
382 // Search for a separator where the row can be broken.
383 while (cit != beg && boost::prior(cit)->type != SEPARATOR && new_end > keep) {
386 new_wid -= cit->dim.wid;
387 if (new_wid < w && first_below == end)
392 // We have found a suitable separator. This is the
396 elements_.erase(cit, end);
400 /* If we are here, it means that we have not found a separator
401 * to shorten the row. There is one case where we can do
402 * something: when we have one big string, maybe with some
403 * other things after it.
405 double max_w = w - x;
406 if (first_below->breakAt(max_w)) {
407 end_ = first_below->endpos;
408 dim_.wid = int(x + first_below->width());
409 // If there are other elements, they should be removed.
410 elements_.erase(boost::next(first_below), end);
411 } else if (first_below->pos > pos_) {
412 end_ = first_below->pos;
414 // Remove all elements from first_below.
415 elements_.erase(first_below, end);
420 void Row::reverseRTL(bool const rtl_par)
423 pos_type const end = elements_.size();
425 // gather a sequence of elements with the same direction
426 bool const rtl = elements_[i].font.isVisibleRightToLeft();
428 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
430 // if the direction is not the same as the paragraph
431 // direction, the sequence has to be reverted.
433 reverse(elements_.begin() + i, elements_.begin() + j);
436 // If the paragraph itself is RTL, reverse everything
438 reverse(elements_.begin(), elements_.end());