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 // 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 == pos || type != STRING)
49 w = rtl ? width() : 0;
51 w = rtl ? 0 : width();
53 FontMetrics const & fm = theFontMetrics(font);
54 w = fm.pos2x(str, i - pos, font.isVisibleRightToLeft());
61 pos_type Row::Element::x2pos(double &x) const
63 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
64 bool const rtl = font.isVisibleRightToLeft();
69 FontMetrics const & fm = theFontMetrics(font);
70 // FIXME: is it really necessary for x to be a double?
72 i = fm.x2pos(str, xx, rtl);
77 // those elements are actually empty (but they have a width)
79 x = rtl ? width() : 0;
84 // those elements contain only one position. Round to
95 //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
101 pos_type Row::Element::left_pos() const
103 return font.isVisibleRightToLeft() ? endpos : pos;
107 pos_type Row::Element::right_pos() const
109 return font.isVisibleRightToLeft() ? pos : endpos;
115 : separator(0), label_hfill(0), x(0), right_margin(0),
116 sel_beg(-1), sel_end(-1),
117 begin_margin_sel(false), end_margin_sel(false),
118 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
122 void Row::setCrc(size_type crc) const
124 changed_ = crc != crc_;
129 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
130 DocIterator const & end) const
132 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
133 pos_type const margin_pos = left_margin ? pos_ : end_;
135 // Is the chosen margin selected ?
136 if (sel_pos == margin_pos) {
137 if (beg.pos() == end.pos())
138 // This is a special case in which the space between after
139 // pos i-1 and before pos i is selected, i.e. the margins
140 // (see DocIterator::boundary_).
141 return beg.boundary() && !end.boundary();
142 else if (end.pos() == margin_pos)
143 // If the selection ends around the margin, it is only
144 // drawn if the cursor is after the margin.
145 return !end.boundary();
146 else if (beg.pos() == margin_pos)
147 // If the selection begins around the margin, it is
148 // only drawn if the cursor is before the margin.
149 return beg.boundary();
157 void Row::setSelectionAndMargins(DocIterator const & beg,
158 DocIterator const & end) const
160 setSelection(beg.pos(), end.pos());
163 end_margin_sel = isMarginSelected(false, beg, end);
164 begin_margin_sel = isMarginSelected(true, beg, end);
169 void Row::setSelection(pos_type beg, pos_type end) const
171 if (pos_ >= beg && pos_ <= end)
173 else if (beg > pos_ && beg <= end_)
178 if (end_ >= beg && end_ <= end)
180 else if (end < end_ && end >= pos_)
187 bool Row::selection() const
189 return sel_beg != -1 && sel_end != -1;
193 ostream & operator<<(ostream & os, Row::Element const & e)
195 if (e.font.isVisibleRightToLeft())
196 os << e.endpos << "<<" << e.pos << " ";
198 os << e.pos << ">>" << e.endpos << " ";
202 os << "STRING: `" << to_utf8(e.str) << "', ";
205 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
208 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
211 os << "SEPARATOR: extra=" << e.extra << ", ";
217 os << "width=" << e.width();
222 ostream & operator<<(ostream & os, Row const & row)
224 os << " pos: " << row.pos_ << " end: " << row.end_
226 << " width: " << row.dim_.wid
227 << " right_margin: " << row.right_margin
228 << " ascent: " << row.dim_.asc
229 << " descent: " << row.dim_.des
230 << " separator: " << row.separator
231 << " label_hfill : " << row.label_hfill << "\n";
233 Row::Elements::const_iterator it = row.elements_.begin();
234 for ( ; it != row.elements_.end() ; ++it) {
235 os << "x=" << x << " => " << *it << endl;
242 bool Row::sameString(Font const & f, Change const & ch) const
244 if (elements_.empty())
246 Element const & elt = elements_.back();
247 return elt.type == STRING && !elt.final
248 && elt.font == f && elt.change == ch;
252 void Row::finalizeLast()
254 if (elements_.empty())
256 Element & elt = elements_.back();
261 if (elt.type == STRING) {
262 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
263 dim_.wid += elt.dim.wid;
268 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
269 Font const & f, Change const & ch)
272 Element e(INSET, pos, f, ch);
275 elements_.push_back(e);
280 void Row::add(pos_type const pos, char_type const c,
281 Font const & f, Change const & ch)
283 if (!sameString(f, ch)) {
285 Element e(STRING, pos, f, ch);
286 elements_.push_back(e);
289 back().endpos = pos + 1;
293 void Row::addVirtual(pos_type const pos, docstring const & s,
294 Font const & f, Change const & ch)
297 Element e(VIRTUAL, pos, f, ch);
299 e.dim.wid = theFontMetrics(f).width(s);
300 dim_.wid += e.dim.wid;
302 elements_.push_back(e);
307 void Row::addSeparator(pos_type const pos, char_type const c,
308 Font const & f, Change const & ch)
311 Element e(SEPARATOR, pos, f, ch);
313 e.dim.wid = theFontMetrics(f).width(c);
314 elements_.push_back(e);
315 dim_.wid += e.dim.wid;
319 void Row::addSpace(pos_type const pos, int const width,
320 Font const & f, Change const & ch)
323 Element e(SPACE, pos, f, ch);
325 elements_.push_back(e);
326 dim_.wid += e.dim.wid;
332 dim_.wid -= elements_.back().dim.wid;
333 elements_.pop_back();
337 void Row::shorten_if_needed(pos_type const keep, int const w)
339 if (empty() || width() < w)
342 /** First, we try to remove elements one by one from the end
343 * until a separator is found.
345 int i = elements_.size();
347 int new_wid = dim_.wid;
348 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
350 new_end = elements_[i].pos;
351 new_wid -= elements_[i].dim.wid;
354 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
356 new_end = elements_[i].pos;
357 new_wid -= elements_[i].dim.wid;
360 /* If we are here, it means that we have not found a
361 * separator to shorten the row. There is one case
362 * where we can do something: when we have one big
363 * string, maybe with a paragraph marker after it.
365 Element & front = elements_.front();
366 if (!(front.type == STRING
367 && (elements_.size() == 1
368 || (elements_.size() == 2
369 && back().type == VIRTUAL))))
372 // If this is a string element, we can try to split it.
373 if (front.type != STRING)
376 // If there is a paragraph marker, it should be taken in account
377 if (elements_.size() == 2)
378 xstr -= back().width();
379 //FIXME: use FontMetrics::x2pos here?? handle rtl?
380 pos_type new_pos = front.x2pos(xstr);
381 front.str = front.str.substr(0, new_pos - pos_);
382 front.dim.wid = xstr;
383 front.endpos = new_pos;
386 // If there is a paragraph marker, it should be removed.
387 if (elements_.size() == 2)
388 elements_.pop_back();
393 elements_.erase(elements_.begin() + i, elements_.end());
397 void Row::reverseRTL(bool const rtl_par)
400 pos_type const end = elements_.size();
402 // gather a sequence of elements with the same direction
403 bool const rtl = elements_[i].font.isVisibleRightToLeft();
405 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
407 // if the direction is not the same as the paragraph
408 // direction, the sequence has to be reverted.
410 reverse(elements_.begin() + i, elements_.begin() + j);
413 // If the paragraph itself is RTL, reverse everything
415 reverse(elements_.begin(), elements_.end());