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"
35 using frontend::FontMetrics;
37 double Row::Element::pos2x(pos_type const i) const
39 LASSERT(i >= pos && i <= endpos, return 0);
41 bool const rtl = font.isVisibleRightToLeft();
44 //handle first the two bounds of the element
50 LASSERT(type == STRING, return 0);
51 FontMetrics const & fm = theFontMetrics(font);
52 // FIXME Avoid caching of metrics there?
53 w = fm.width(str.substr(0, i - pos));
63 pos_type Row::Element::x2pos(double &x, bool const low) const
65 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66 // If element is rtl, flip x value
67 bool const rtl = font.isVisibleRightToLeft();
68 double x2 = rtl ? (width() - x) : x;
75 // those elements are actually empty (but they have a width)
78 FontMetrics const & fm = theFontMetrics(font);
79 // FIXME: implement dichotomy search?
80 for ( ; i < str.size() ; ++i) {
82 w = fm.width(str.substr(0, i + 1));
91 // those elements contain only one position
95 if (type == STRING && i == str.size())
97 // round to the closest side. The !rtl is here to obtain the
98 // same rounding as with the old code (this is cosmetic and
99 // can be eventually removed).
100 else if (type != VIRTUAL && !low && (x2 - last_w + !rtl > w - x2)) {
106 // is element is rtl, flip values back
107 x = rtl ? width() - x2 : x2;
109 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
114 pos_type Row::Element::left_pos() const
116 return font.isVisibleRightToLeft() ? endpos : pos;
120 pos_type Row::Element::right_pos() const
122 return font.isVisibleRightToLeft() ? pos : endpos;
128 : separator(0), label_hfill(0), x(0), right_margin(0),
129 sel_beg(-1), sel_end(-1),
130 begin_margin_sel(false), end_margin_sel(false),
131 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
135 void Row::setCrc(size_type crc) const
137 changed_ = crc != crc_;
142 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
143 DocIterator const & end) const
145 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
146 pos_type const margin_pos = left_margin ? pos_ : end_;
148 // Is the chosen margin selected ?
149 if (sel_pos == margin_pos) {
150 if (beg.pos() == end.pos())
151 // This is a special case in which the space between after
152 // pos i-1 and before pos i is selected, i.e. the margins
153 // (see DocIterator::boundary_).
154 return beg.boundary() && !end.boundary();
155 else if (end.pos() == margin_pos)
156 // If the selection ends around the margin, it is only
157 // drawn if the cursor is after the margin.
158 return !end.boundary();
159 else if (beg.pos() == margin_pos)
160 // If the selection begins around the margin, it is
161 // only drawn if the cursor is before the margin.
162 return beg.boundary();
170 void Row::setSelectionAndMargins(DocIterator const & beg,
171 DocIterator const & end) const
173 setSelection(beg.pos(), end.pos());
176 end_margin_sel = isMarginSelected(false, beg, end);
177 begin_margin_sel = isMarginSelected(true, beg, end);
182 void Row::setSelection(pos_type beg, pos_type end) const
184 if (pos_ >= beg && pos_ <= end)
186 else if (beg > pos_ && beg <= end_)
191 if (end_ >= beg && end_ <= end)
193 else if (end < end_ && end >= pos_)
200 bool Row::selection() const
202 return sel_beg != -1 && sel_end != -1;
206 ostream & operator<<(ostream & os, Row::Element const & e)
208 if (e.font.isVisibleRightToLeft())
209 os << e.endpos << "<<" << e.pos << " ";
211 os << e.pos << ">>" << e.endpos << " ";
215 os << "STRING: `" << to_utf8(e.str) << "', ";
218 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
221 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
224 os << "SEPARATOR: extra=" << e.extra << ", ";
230 os << "width=" << e.width();
235 ostream & operator<<(ostream & os, Row const & row)
237 os << " pos: " << row.pos_ << " end: " << row.end_
239 << " width: " << row.dim_.wid
240 << " right_margin: " << row.right_margin
241 << " ascent: " << row.dim_.asc
242 << " descent: " << row.dim_.des
243 << " separator: " << row.separator
244 << " label_hfill : " << row.label_hfill << "\n";
246 Row::Elements::const_iterator it = row.elements_.begin();
247 for ( ; it != row.elements_.end() ; ++it) {
248 os << "x=" << x << " => " << *it << endl;
255 bool Row::sameString(Font const & f, Change const & ch) const
257 if (elements_.empty())
259 Element const & elt = elements_.back();
260 return elt.type == STRING && !elt.final
261 && elt.font == f && elt.change == ch;
265 void Row::finalizeLast()
267 if (elements_.empty())
269 Element & elt = elements_.back();
274 if (elt.type == STRING) {
275 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
276 dim_.wid += elt.dim.wid;
281 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
282 Font const & f, Change const & ch)
285 Element e(INSET, pos, f, ch);
288 elements_.push_back(e);
293 void Row::add(pos_type const pos, char_type const c,
294 Font const & f, Change const & ch)
296 if (!sameString(f, ch)) {
298 Element e(STRING, pos, f, ch);
299 elements_.push_back(e);
302 back().endpos = pos + 1;
306 void Row::addVirtual(pos_type const pos, docstring const & s,
307 Font const & f, Change const & ch)
310 Element e(VIRTUAL, pos, f, ch);
312 e.dim.wid = theFontMetrics(f).width(s);
313 dim_.wid += e.dim.wid;
315 elements_.push_back(e);
320 void Row::addSeparator(pos_type const pos, char_type const c,
321 Font const & f, Change const & ch)
324 Element e(SEPARATOR, pos, f, ch);
326 e.dim.wid = theFontMetrics(f).width(c);
327 elements_.push_back(e);
328 dim_.wid += e.dim.wid;
332 void Row::addSpace(pos_type const pos, int const width,
333 Font const & f, Change const & ch)
336 Element e(SPACE, pos, f, ch);
338 elements_.push_back(e);
339 dim_.wid += e.dim.wid;
345 dim_.wid -= elements_.back().dim.wid;
346 elements_.pop_back();
350 void Row::shorten_if_needed(pos_type const keep, int const w)
352 if (empty() || width() < w)
355 /** First, we try to remove elements one by one from the end
356 * until a separator is found.
358 int i = elements_.size();
360 int new_wid = dim_.wid;
361 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
363 new_end = elements_[i].pos;
364 new_wid -= elements_[i].dim.wid;
367 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
369 new_end = elements_[i].pos;
370 new_wid -= elements_[i].dim.wid;
373 /* If we are here, it means that we have not found a
374 * separator to shorten the row. There is one case
375 * where we can do something: when we have one big
376 * string, maybe with a paragraph marker after it.
378 Element & front = elements_.front();
379 if (!(front.type == STRING
380 && (elements_.size() == 1
381 || (elements_.size() == 2
382 && back().type == VIRTUAL))))
385 // If this is a string element, we can try to split it.
386 if (front.type != STRING)
389 // If there is a paragraph marker, it should be taken in account
390 if (elements_.size() == 2)
391 xstr -= back().width();
392 pos_type new_pos = front.x2pos(xstr, true);
393 front.str = front.str.substr(0, new_pos - pos_);
394 front.dim.wid = xstr;
395 front.endpos = new_pos;
398 // If there is a paragraph marker, it should be removed.
399 if (elements_.size() == 2)
400 elements_.pop_back();
405 elements_.erase(elements_.begin() + i, elements_.end());
409 void Row::reverseRTL(bool const rtl_par)
412 pos_type const end = elements_.size();
414 // gather a sequence of elements with the same direction
415 bool const rtl = elements_[i].font.isVisibleRightToLeft();
417 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
419 // if the direction is not the same as the paragraph
420 // direction, the sequence has to be reverted.
422 reverse(elements_.begin() + i, elements_.begin() + j);
425 // If the paragraph itself is RTL, reverse everything
427 reverse(elements_.begin(), elements_.end());