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 bool const rtl = font.isVisibleRightToLeft();
41 // handle first the two bounds of the element
42 if ((!rtl && pos >= i) || (rtl && endpos <= i))
44 if ((!rtl && endpos <= i) || (rtl && pos >= i))
47 FontMetrics const & fm = theFontMetrics(font);
48 // FIXME Avoid caching of metrics there?
49 int const w = fm.width(str.substr(0, i - pos));
57 pos_type Row::Element::x2pos(double &x, bool const low) const
59 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
60 // if element is rtl, flip x value
61 bool const rtl = font.isVisibleRightToLeft();
62 double x2 = rtl ? (width() - x) : x;
64 FontMetrics const & fm = theFontMetrics(font);
68 // non-STRING element only contain one position
72 // FIXME: implement dichotomy search?
73 for ( ; i < str.size() ; ++i) {
75 w = fm.width(str.substr(0, i + 1));
79 // if (i == str.size())
80 // lyxerr << " NOT FOUND ";
85 // round to the closest side
86 else if (!low && (x2 - last_w > w - x2)) {
92 // is element is rtl, flip values
99 //lyxerr << "=> p=" << i << " x=" << x << endl;
105 : separator(0), label_hfill(0), x(0), right_margin(0),
106 sel_beg(-1), sel_end(-1),
107 begin_margin_sel(false), end_margin_sel(false),
108 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
112 void Row::setCrc(size_type crc) const
114 changed_ = crc != crc_;
119 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
120 DocIterator const & end) const
122 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
123 pos_type const margin_pos = left_margin ? pos_ : end_;
125 // Is the chosen margin selected ?
126 if (sel_pos == margin_pos) {
127 if (beg.pos() == end.pos())
128 // This is a special case in which the space between after
129 // pos i-1 and before pos i is selected, i.e. the margins
130 // (see DocIterator::boundary_).
131 return beg.boundary() && !end.boundary();
132 else if (end.pos() == margin_pos)
133 // If the selection ends around the margin, it is only
134 // drawn if the cursor is after the margin.
135 return !end.boundary();
136 else if (beg.pos() == margin_pos)
137 // If the selection begins around the margin, it is
138 // only drawn if the cursor is before the margin.
139 return beg.boundary();
147 void Row::setSelectionAndMargins(DocIterator const & beg,
148 DocIterator const & end) const
150 setSelection(beg.pos(), end.pos());
153 end_margin_sel = isMarginSelected(false, beg, end);
154 begin_margin_sel = isMarginSelected(true, beg, end);
159 void Row::setSelection(pos_type beg, pos_type end) const
161 if (pos_ >= beg && pos_ <= end)
163 else if (beg > pos_ && beg <= end_)
168 if (end_ >= beg && end_ <= end)
170 else if (end < end_ && end >= pos_)
177 bool Row::selection() const
179 return sel_beg != -1 && sel_end != -1;
183 ostream & operator<<(ostream & os, Row::Element const & e)
185 if (e.font.isVisibleRightToLeft())
186 os << e.endpos << "<<" << e.pos << " ";
188 os << e.pos << ">>" << e.endpos << " ";
192 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
195 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
198 os << "INSET: " << to_utf8(e.inset->layoutName());
201 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
204 os << "SPACE: " << e.dim.wid;
211 ostream & operator<<(ostream & os, Row const & row)
213 os << " pos: " << row.pos_ << " end: " << row.end_
215 << " width: " << row.dim_.wid
216 << " right_margin: " << row.right_margin
217 << " ascent: " << row.dim_.asc
218 << " descent: " << row.dim_.des
219 << " separator: " << row.separator
220 << " label_hfill : " << row.label_hfill << "\n";
222 Row::Elements::const_iterator it = row.elements_.begin();
223 for ( ; it != row.elements_.end() ; ++it) {
224 os << "x=" << x << " => " << *it << endl;
231 bool Row::sameString(Font const & f, Change const & ch) const
233 if (elements_.empty())
235 Element const & elt = elements_.back();
236 return elt.type == STRING && !elt.final
237 && elt.font == f && elt.change == ch;
241 void Row::finalizeLast()
243 if (elements_.empty())
245 Element & elt = elements_.back();
250 if (elt.type == STRING) {
251 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
252 dim_.wid += elt.dim.wid;
257 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
258 Font const & f, Change const & ch)
261 Element e(INSET, pos, f, ch);
264 elements_.push_back(e);
269 void Row::add(pos_type const pos, char_type const c,
270 Font const & f, Change const & ch)
272 if (!sameString(f, ch)) {
274 Element e(STRING, pos, f, ch);
275 elements_.push_back(e);
277 //lyxerr << "FONT " <<back().font.language() << endl;
279 back().endpos = pos + 1;
283 void Row::addVirtual(pos_type const pos, docstring const & s,
284 Font const & f, Change const & ch)
287 Element e(VIRTUAL, pos, f, ch);
289 // A completion has no size
291 elements_.push_back(e);
296 void Row::addSeparator(pos_type const pos, char_type const c,
297 Font const & f, Change const & ch)
300 Element e(SEPARATOR, pos, f, ch);
302 e.dim.wid = theFontMetrics(f).width(c);
303 elements_.push_back(e);
304 dim_.wid += e.dim.wid;
308 void Row::addSpace(pos_type const pos, int const width,
309 Font const & f, Change const & ch)
312 Element e(SPACE, pos, f, ch);
314 elements_.push_back(e);
315 dim_.wid += e.dim.wid;
321 dim_.wid -= elements_.back().dim.wid;
322 elements_.pop_back();
326 void Row::shorten_if_needed(pos_type const keep, int const w)
328 if (empty() || width() < w)
331 /** First, we try to remove elements one by one from the end
332 * until a separator is found.
334 int i = elements_.size();
336 int new_wid = dim_.wid;
337 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
339 new_end = elements_[i].pos;
340 new_wid -= elements_[i].dim.wid;
343 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
345 new_end = elements_[i].pos;
346 new_wid -= elements_[i].dim.wid;
349 /* If we are here, it means that we have not found a
350 * separator to shorten the row. There is one case
351 * where we can do something: when we have one big
352 * string, maybe with a paragraph marker after it.
354 Element & front = elements_.front();
355 if (!(front.type == STRING
356 && (elements_.size() == 1
357 || (elements_.size() == 2
358 && back().type == VIRTUAL))))
361 // If this is a string element, we can try to split it.
362 if (front.type != STRING)
365 // If there is a paragraph marker, it should be taken in account
366 if (elements_.size() == 2)
367 xstr -= back().width();
368 pos_type new_pos = front.x2pos(xstr, true);
369 front.str = front.str.substr(0, new_pos - pos_);
370 front.dim.wid = xstr;
371 front.endpos = new_pos;
374 // If there is a paragraph marker, it should be removed.
375 if (elements_.size() == 2)
376 elements_.pop_back();
381 elements_.erase(elements_.begin() + i, elements_.end());
385 void Row::reverseRTL(bool const rtl_par)
388 pos_type const end = elements_.size();
390 // gather a sequence of elements with the same direction
391 bool const rtl = elements_[i].font.isVisibleRightToLeft();
393 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
395 // if the direction is not the same as the paragraph
396 // direction, the sequence has to be reverted.
398 reverse(elements_.begin() + i, elements_.begin() + j);
401 // If the paragraph itself is RTL, reverse everything
403 reverse(elements_.begin(), elements_.end());