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/lyxalgo.h"
35 using frontend::FontMetrics;
37 double Row::Element::pos2x(pos_type const i) const
39 // This can happen with inline completion when clicking on the
40 // row after the completion.
41 if (i < pos || i > endpos)
44 bool const rtl = font.isVisibleRightToLeft();
47 //handle first the two bounds of the element
48 if (i == endpos && type != VIRTUAL
49 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
50 w = rtl ? 0 : full_width();
51 else if (i == pos || type != STRING)
52 w = rtl ? full_width() : 0;
54 FontMetrics const & fm = theFontMetrics(font);
55 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft());
62 pos_type Row::Element::x2pos(int &x) const
64 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
65 bool const rtl = font.isVisibleRightToLeft();
70 FontMetrics const & fm = theFontMetrics(font);
71 i = fm.x2pos(str, x, rtl);
75 // those elements are actually empty (but they have a width)
77 x = rtl ? int(full_width()) : 0;
82 // those elements contain only one position. Round to
84 if (x > full_width()) {
85 x = int(full_width());
93 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
99 bool Row::Element::breakAt(int w)
101 if (type != STRING || dim.wid <= w)
104 bool const rtl = font.isVisibleRightToLeft();
107 pos_type new_pos = x2pos(w);
110 str = str.substr(0, new_pos - pos);
120 pos_type Row::Element::left_pos() const
122 return font.isVisibleRightToLeft() ? endpos : pos;
126 pos_type Row::Element::right_pos() const
128 return font.isVisibleRightToLeft() ? pos : endpos;
133 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
134 sel_beg(-1), sel_end(-1),
135 begin_margin_sel(false), end_margin_sel(false),
136 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
140 void Row::setCrc(size_type crc) const
142 changed_ = crc != crc_;
147 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
148 DocIterator const & end) const
150 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
151 pos_type const margin_pos = left_margin ? pos_ : end_;
153 // Is the chosen margin selected ?
154 if (sel_pos == margin_pos) {
155 if (beg.pos() == end.pos())
156 // This is a special case in which the space between after
157 // pos i-1 and before pos i is selected, i.e. the margins
158 // (see DocIterator::boundary_).
159 return beg.boundary() && !end.boundary();
160 else if (end.pos() == margin_pos)
161 // If the selection ends around the margin, it is only
162 // drawn if the cursor is after the margin.
163 return !end.boundary();
164 else if (beg.pos() == margin_pos)
165 // If the selection begins around the margin, it is
166 // only drawn if the cursor is before the margin.
167 return beg.boundary();
175 void Row::setSelectionAndMargins(DocIterator const & beg,
176 DocIterator const & end) const
178 setSelection(beg.pos(), end.pos());
181 end_margin_sel = isMarginSelected(false, beg, end);
182 begin_margin_sel = isMarginSelected(true, beg, end);
187 void Row::setSelection(pos_type beg, pos_type end) const
189 if (pos_ >= beg && pos_ <= end)
191 else if (beg > pos_ && beg <= end_)
196 if (end_ >= beg && end_ <= end)
198 else if (end < end_ && end >= pos_)
205 bool Row::selection() const
207 return sel_beg != -1 && sel_end != -1;
211 ostream & operator<<(ostream & os, Row::Element const & e)
213 if (e.font.isVisibleRightToLeft())
214 os << e.endpos << "<<" << e.pos << " ";
216 os << e.pos << ">>" << e.endpos << " ";
220 os << "STRING: `" << to_utf8(e.str) << "', ";
223 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
226 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
229 os << "SEPARATOR: extra=" << e.extra << ", ";
235 os << "width=" << e.full_width();
240 ostream & operator<<(ostream & os, Row const & row)
242 os << " pos: " << row.pos_ << " end: " << row.end_
243 << " left_margin: " << row.left_margin
244 << " width: " << row.dim_.wid
245 << " right_margin: " << row.right_margin
246 << " ascent: " << row.dim_.asc
247 << " descent: " << row.dim_.des
248 << " separator: " << row.separator
249 << " label_hfill: " << row.label_hfill
250 << " row_boundary: " << row.right_boundary() << "\n";
251 double x = row.left_margin;
252 Row::Elements::const_iterator it = row.elements_.begin();
253 for ( ; it != row.elements_.end() ; ++it) {
254 os << "x=" << x << " => " << *it << endl;
255 x += it->full_width();
261 bool Row::sameString(Font const & f, Change const & ch) const
263 if (elements_.empty())
265 Element const & elt = elements_.back();
266 return elt.type == STRING && !elt.final
267 && elt.font == f && elt.change == ch;
271 void Row::finalizeLast()
273 if (elements_.empty())
275 Element & elt = elements_.back();
280 if (elt.type == STRING) {
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);
308 back().endpos = pos + 1;
312 void Row::addVirtual(pos_type const pos, docstring const & s,
313 Font const & f, Change const & ch)
316 Element e(VIRTUAL, pos, f, ch);
318 e.dim.wid = theFontMetrics(f).width(s);
319 dim_.wid += e.dim.wid;
321 elements_.push_back(e);
326 void Row::addSeparator(pos_type const pos, char_type const c,
327 Font const & f, Change const & ch)
330 Element e(SEPARATOR, pos, f, ch);
332 e.dim.wid = theFontMetrics(f).width(c);
333 elements_.push_back(e);
334 dim_.wid += e.dim.wid;
338 void Row::addSpace(pos_type const pos, int const width,
339 Font const & f, Change const & ch)
342 Element e(SPACE, pos, f, ch);
344 elements_.push_back(e);
345 dim_.wid += e.dim.wid;
351 dim_.wid -= elements_.back().dim.wid;
352 elements_.pop_back();
356 void Row::shortenIfNeeded(pos_type const keep, int const w)
358 if (empty() || width() <= w)
361 Elements::iterator const beg = elements_.begin();
362 Elements::iterator const end = elements_.end();
363 Elements::iterator last_sep = elements_.end();
365 int wid = left_margin;
367 Elements::iterator cit = beg;
368 for ( ; cit != end ; ++cit) {
369 if (cit->type == SEPARATOR && cit->pos >= keep) {
373 if (wid + cit->dim.wid > w)
378 if (last_sep != end) {
379 // We have found a suitable separator. This is the
381 end_ = last_sep->endpos;
382 dim_.wid = last_width;
383 elements_.erase(last_sep, end);
388 // This should not happen since the row is too long.
389 LYXERR0("Something is wrong cannot shorten row: " << *this);
393 if (cit != beg && cit->type == VIRTUAL) {
394 // It is not possible to separate a virtual element from the
401 // There is no separator, but several elements (probably
402 // insets) have been added. We can cut at this place.
405 elements_.erase(cit, end);
409 /* If we are here, it means that we have not found a separator
410 * to shorten the row. There is one case where we can do
411 * something: when we have one big string, maybe with some
412 * other things after it.
414 if (cit->breakAt(w - left_margin)) {
416 dim_.wid = left_margin + cit->dim.wid;
417 // If there are other elements, they should be removed.
418 elements_.erase(next(cit, 1), end);
423 void Row::reverseRTL(bool const rtl_par)
426 pos_type const end = elements_.size();
428 // gather a sequence of elements with the same direction
429 bool const rtl = elements_[i].font.isVisibleRightToLeft();
431 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
433 // if the direction is not the same as the paragraph
434 // direction, the sequence has to be reverted.
436 reverse(elements_.begin() + i, elements_.begin() + j);
439 // If the paragraph itself is RTL, reverse everything
441 reverse(elements_.begin(), elements_.end());