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
73 // FIXME: implement dichotomy search?
74 for ( ; i <= str.size() ; ++i) {
76 w = fm.width(str.substr(0,i));
82 // if (i == str.size())
83 // lyxerr << " NOT FOUND ";
86 // round to the closest side
87 if (!low && (x2 - last_w > w - x2)) {
93 // is element is rtl, flip values
100 //lyxerr << "=> p=" << i << " x=" << x << endl;
106 : separator(0), label_hfill(0), x(0), right_margin(0),
107 sel_beg(-1), sel_end(-1),
108 begin_margin_sel(false), end_margin_sel(false),
109 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
113 void Row::setCrc(size_type crc) const
115 changed_ = crc != crc_;
120 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
121 DocIterator const & end) const
123 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
124 pos_type const margin_pos = left_margin ? pos_ : end_;
126 // Is the chosen margin selected ?
127 if (sel_pos == margin_pos) {
128 if (beg.pos() == end.pos())
129 // This is a special case in which the space between after
130 // pos i-1 and before pos i is selected, i.e. the margins
131 // (see DocIterator::boundary_).
132 return beg.boundary() && !end.boundary();
133 else if (end.pos() == margin_pos)
134 // If the selection ends around the margin, it is only
135 // drawn if the cursor is after the margin.
136 return !end.boundary();
137 else if (beg.pos() == margin_pos)
138 // If the selection begins around the margin, it is
139 // only drawn if the cursor is before the margin.
140 return beg.boundary();
148 void Row::setSelectionAndMargins(DocIterator const & beg,
149 DocIterator const & end) const
151 setSelection(beg.pos(), end.pos());
154 end_margin_sel = isMarginSelected(false, beg, end);
155 begin_margin_sel = isMarginSelected(true, beg, end);
160 void Row::setSelection(pos_type beg, pos_type end) const
162 if (pos_ >= beg && pos_ <= end)
164 else if (beg > pos_ && beg <= end_)
169 if (end_ >= beg && end_ <= end)
171 else if (end < end_ && end >= pos_)
178 bool Row::selection() const
180 return sel_beg != -1 && sel_end != -1;
184 ostream & operator<<(ostream & os, Row::Element const & e)
186 if (e.font.isVisibleRightToLeft())
187 os << e.endpos << "<<" << e.pos << " ";
189 os << e.pos << ">>" << e.endpos << " ";
193 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
196 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
199 os << "INSET: " << to_utf8(e.inset->layoutName());
202 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
205 os << "SPACE: " << e.dim.wid;
212 ostream & operator<<(ostream & os, Row const & row)
214 os << " pos: " << row.pos_ << " end: " << row.end_
216 << " width: " << row.dim_.wid
217 << " ascent: " << row.dim_.asc
218 << " descent: " << row.dim_.des
219 << " separator: " << row.separator
220 << " label_hfill : " << row.label_hfill << "\n";
221 Row::Elements::const_iterator it = row.elements_.begin();
222 for ( ; it != row.elements_.end() ; ++it) {
223 os << "** " << *it << endl;
229 bool Row::sameString(Font const & f, Change const & ch) const
231 if (elements_.empty())
233 Element const & elt = elements_.back();
234 return elt.type == STRING && !elt.final
235 && elt.font == f && elt.change == ch;
239 void Row::finalizeLast()
241 if (elements_.empty())
243 Element & elt = elements_.back();
248 if (elt.type == STRING) {
249 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
250 dim_.wid += elt.dim.wid;
255 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
256 Font const & f, Change const & ch)
259 Element e(INSET, pos, f, ch);
262 elements_.push_back(e);
267 void Row::add(pos_type const pos, char_type const c,
268 Font const & f, Change const & ch)
270 if (!sameString(f, ch)) {
272 Element e(STRING, pos, f, ch);
273 elements_.push_back(e);
275 //lyxerr << "FONT " <<back().font.language() << endl;
277 back().endpos = pos + 1;
281 void Row::addVirtual(pos_type const pos, docstring const & s,
282 Font const & f, Change const & ch)
285 Element e(VIRTUAL, pos, f, ch);
287 // A completion has no size
289 elements_.push_back(e);
294 void Row::addSeparator(pos_type const pos, char_type const c,
295 Font const & f, Change const & ch)
298 Element e(SEPARATOR, pos, f, ch);
300 e.dim.wid = theFontMetrics(f).width(c);
301 elements_.push_back(e);
302 dim_.wid += e.dim.wid;
306 void Row::addSpace(pos_type const pos, int const width,
307 Font const & f, Change const & ch)
310 Element e(SPACE, pos, f, ch);
312 elements_.push_back(e);
313 dim_.wid += e.dim.wid;
319 dim_.wid -= elements_.back().dim.wid;
320 elements_.pop_back();
324 void Row::shorten_if_needed(pos_type const keep, int const w)
326 if (empty() || width() < w)
329 /** First, we try to remove elements one by one from the end
330 * until a separator is found.
332 int i = elements_.size();
334 int new_wid = dim_.wid;
335 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
337 new_end = elements_[i].pos;
338 new_wid -= elements_[i].dim.wid;
341 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
343 new_end = elements_[i].pos;
344 new_wid -= elements_[i].dim.wid;
347 /* If we are here, it means that we have not found a
348 * separator to shorten the row. There is one case
349 * where we can do something: when we have one big
350 * string, maybe with a paragraph marker after it.
352 Element & front = elements_.front();
353 if (!(front.type == STRING
354 && (elements_.size() == 1
355 || (elements_.size() == 2
356 && back().type == VIRTUAL))))
359 // If this is a string element, we can try to split it.
360 if (front.type != STRING)
363 // If there is a paragraph marker, it should be taken in account
364 if (elements_.size() == 2)
365 xstr -= back().width();
366 pos_type new_pos = front.x2pos(xstr, true);
367 front.str = front.str.substr(0, new_pos - pos_);
368 front.dim.wid = xstr;
369 front.endpos = new_pos;
372 // If there is a paragraph marker, it should be removed.
373 if (elements_.size() == 2)
374 elements_.pop_back();
379 elements_.erase(elements_.begin() + i, elements_.end());
383 void Row::reverseRTL()
386 pos_type const end = elements_.size();
389 while (i < end && !elements_[i].font.isRightToLeft())
394 // look for a RTL sequence
396 while (j < end && elements_[j].font.isRightToLeft())
398 reverse(elements_.begin() + i, elements_.begin() + j);