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 : full_width();
52 else if (i == pos || type != STRING)
53 w = rtl ? full_width() : 0;
55 FontMetrics const & fm = theFontMetrics(font);
56 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft());
63 pos_type Row::Element::x2pos(int &x) const
65 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66 bool const rtl = font.isVisibleRightToLeft();
71 FontMetrics const & fm = theFontMetrics(font);
72 i = fm.x2pos(str, x, rtl);
76 // those elements are actually empty (but they have a width)
78 x = rtl ? int(full_width()) : 0;
83 // those elements contain only one position. Round to
85 if (x > full_width()) {
86 x = int(full_width());
94 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
100 bool Row::Element::breakAt(int w)
102 if (type != STRING || dim.wid <= w)
105 bool const rtl = font.isVisibleRightToLeft();
108 pos_type new_pos = x2pos(w);
111 str = str.substr(0, new_pos - pos);
121 pos_type Row::Element::left_pos() const
123 return font.isVisibleRightToLeft() ? endpos : pos;
127 pos_type Row::Element::right_pos() const
129 return font.isVisibleRightToLeft() ? pos : endpos;
134 : separator(0), label_hfill(0), left_margin(0), right_margin(0),
135 sel_beg(-1), sel_end(-1),
136 begin_margin_sel(false), end_margin_sel(false),
137 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
141 void Row::setCrc(size_type crc) const
143 changed_ = crc != crc_;
148 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
149 DocIterator const & end) const
151 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
152 pos_type const margin_pos = left_margin ? pos_ : end_;
154 // Is the chosen margin selected ?
155 if (sel_pos == margin_pos) {
156 if (beg.pos() == end.pos())
157 // This is a special case in which the space between after
158 // pos i-1 and before pos i is selected, i.e. the margins
159 // (see DocIterator::boundary_).
160 return beg.boundary() && !end.boundary();
161 else if (end.pos() == margin_pos)
162 // If the selection ends around the margin, it is only
163 // drawn if the cursor is after the margin.
164 return !end.boundary();
165 else if (beg.pos() == margin_pos)
166 // If the selection begins around the margin, it is
167 // only drawn if the cursor is before the margin.
168 return beg.boundary();
176 void Row::setSelectionAndMargins(DocIterator const & beg,
177 DocIterator const & end) const
179 setSelection(beg.pos(), end.pos());
182 end_margin_sel = isMarginSelected(false, beg, end);
183 begin_margin_sel = isMarginSelected(true, beg, end);
188 void Row::setSelection(pos_type beg, pos_type end) const
190 if (pos_ >= beg && pos_ <= end)
192 else if (beg > pos_ && beg <= end_)
197 if (end_ >= beg && end_ <= end)
199 else if (end < end_ && end >= pos_)
206 bool Row::selection() const
208 return sel_beg != -1 && sel_end != -1;
212 ostream & operator<<(ostream & os, Row::Element const & e)
214 if (e.font.isVisibleRightToLeft())
215 os << e.endpos << "<<" << e.pos << " ";
217 os << e.pos << ">>" << e.endpos << " ";
221 os << "STRING: `" << to_utf8(e.str) << "', ";
224 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
227 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
230 os << "SEPARATOR: extra=" << e.extra << ", ";
236 os << "width=" << e.full_width();
241 ostream & operator<<(ostream & os, Row const & row)
243 os << " pos: " << row.pos_ << " end: " << row.end_
244 << " left_margin: " << row.left_margin
245 << " width: " << row.dim_.wid
246 << " right_margin: " << row.right_margin
247 << " ascent: " << row.dim_.asc
248 << " descent: " << row.dim_.des
249 << " separator: " << row.separator
250 << " label_hfill: " << row.label_hfill
251 << " row_boundary: " << row.right_boundary() << "\n";
252 double x = row.left_margin;
253 Row::Elements::const_iterator it = row.elements_.begin();
254 for ( ; it != row.elements_.end() ; ++it) {
255 os << "x=" << x << " => " << *it << endl;
256 x += it->full_width();
262 bool Row::sameString(Font const & f, Change const & ch) const
264 if (elements_.empty())
266 Element const & elt = elements_.back();
267 return elt.type == STRING && !elt.final
268 && elt.font == f && elt.change == ch;
272 void Row::finalizeLast()
274 if (elements_.empty())
276 Element & elt = elements_.back();
281 if (elt.type == STRING) {
282 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
283 dim_.wid += elt.dim.wid;
288 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
289 Font const & f, Change const & ch)
292 Element e(INSET, pos, f, ch);
295 elements_.push_back(e);
300 void Row::add(pos_type const pos, char_type const c,
301 Font const & f, Change const & ch)
303 if (!sameString(f, ch)) {
305 Element e(STRING, pos, f, ch);
306 elements_.push_back(e);
309 back().endpos = pos + 1;
313 void Row::addVirtual(pos_type const pos, docstring const & s,
314 Font const & f, Change const & ch)
317 Element e(VIRTUAL, pos, f, ch);
319 e.dim.wid = theFontMetrics(f).width(s);
320 dim_.wid += e.dim.wid;
322 elements_.push_back(e);
327 void Row::addSeparator(pos_type const pos, char_type const c,
328 Font const & f, Change const & ch)
331 Element e(SEPARATOR, pos, f, ch);
333 e.dim.wid = theFontMetrics(f).width(c);
334 elements_.push_back(e);
335 dim_.wid += e.dim.wid;
339 void Row::addSpace(pos_type const pos, int const width,
340 Font const & f, Change const & ch)
343 Element e(SPACE, pos, f, ch);
345 elements_.push_back(e);
346 dim_.wid += e.dim.wid;
352 dim_.wid -= elements_.back().dim.wid;
353 elements_.pop_back();
357 void Row::shortenIfNeeded(pos_type const keep, int const w)
359 if (empty() || width() <= w)
362 Elements::iterator const beg = elements_.begin();
363 Elements::iterator const end = elements_.end();
364 Elements::iterator last_sep = elements_.end();
366 int wid = left_margin;
368 Elements::iterator cit = beg;
369 for ( ; cit != end ; ++cit) {
370 if (cit->type == SEPARATOR && cit->pos >= keep) {
374 if (wid + cit->dim.wid > w)
379 if (last_sep != end) {
380 // We have found a suitable separator. This is the
382 end_ = last_sep->endpos;
383 dim_.wid = last_width;
384 elements_.erase(last_sep, end);
389 // This should not happen since the row is too long.
390 LYXERR0("Something is wrong cannot shorten row: " << *this);
394 if (cit != beg && cit->type == VIRTUAL) {
395 // It is not possible to separate a virtual element from the
402 // There is no separator, but several elements (probably
403 // insets) have been added. We can cut at this place.
406 elements_.erase(cit, end);
410 /* If we are here, it means that we have not found a separator
411 * to shorten the row. There is one case where we can do
412 * something: when we have one big string, maybe with some
413 * other things after it.
415 if (cit->breakAt(w - left_margin)) {
417 dim_.wid = left_margin + cit->dim.wid;
418 // If there are other elements, they should be removed.
419 elements_.erase(boost::next(cit), end);
424 void Row::reverseRTL(bool const rtl_par)
427 pos_type const end = elements_.size();
429 // gather a sequence of elements with the same direction
430 bool const rtl = elements_[i].font.isVisibleRightToLeft();
432 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
434 // if the direction is not the same as the paragraph
435 // direction, the sequence has to be reverted.
437 reverse(elements_.begin() + i, elements_.begin() + j);
440 // If the paragraph itself is RTL, reverse everything
442 reverse(elements_.begin(), elements_.end());