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)
113 void Row::setCrc(size_type crc) const
115 changed_ = crc != crc_;
120 void Row::pos(pos_type p)
126 void Row::endpos(pos_type p)
132 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
133 DocIterator const & end) const
135 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
136 pos_type const margin_pos = left_margin ? pos_ : end_;
138 // Is the chosen margin selected ?
139 if (sel_pos == margin_pos) {
140 if (beg.pos() == end.pos())
141 // This is a special case in which the space between after
142 // pos i-1 and before pos i is selected, i.e. the margins
143 // (see DocIterator::boundary_).
144 return beg.boundary() && !end.boundary();
145 else if (end.pos() == margin_pos)
146 // If the selection ends around the margin, it is only
147 // drawn if the cursor is after the margin.
148 return !end.boundary();
149 else if (beg.pos() == margin_pos)
150 // If the selection begins around the margin, it is
151 // only drawn if the cursor is before the margin.
152 return beg.boundary();
160 void Row::setSelectionAndMargins(DocIterator const & beg,
161 DocIterator const & end) const
163 setSelection(beg.pos(), end.pos());
166 end_margin_sel = isMarginSelected(false, beg, end);
167 begin_margin_sel = isMarginSelected(true, beg, end);
172 void Row::setSelection(pos_type beg, pos_type end) const
174 if (pos_ >= beg && pos_ <= end)
176 else if (beg > pos_ && beg <= end_)
181 if (end_ >= beg && end_ <= end)
183 else if (end < end_ && end >= pos_)
190 bool Row::selection() const
192 return sel_beg != -1 && sel_end != -1;
196 ostream & operator<<(ostream & os, Row::Element const & e)
198 if (e.font.isVisibleRightToLeft())
199 os << e.endpos << "<<" << e.pos << " ";
201 os << e.pos << ">>" << e.endpos << " ";
205 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
208 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
211 os << "INSET: " << to_utf8(e.inset->layoutName());
214 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
217 os << "SPACE: " << e.dim.wid;
224 ostream & operator<<(ostream & os, Row const & row)
226 os << " pos: " << row.pos_ << " end: " << row.end_
228 << " width: " << row.dim_.wid
229 << " ascent: " << row.dim_.asc
230 << " descent: " << row.dim_.des
231 << " separator: " << row.separator
232 << " label_hfill : " << row.label_hfill << "\n";
233 Row::Elements::const_iterator it = row.elements_.begin();
234 for ( ; it != row.elements_.end() ; ++it) {
235 os << "** " << *it << endl;
241 bool Row::sameString(Font const & f, Change const & ch) const
243 if (elements_.empty())
245 Element const & elt = elements_.back();
246 return elt.type == STRING && !elt.final
247 && elt.font == f && elt.change == ch;
251 void Row::finalizeLast()
253 if (elements_.empty())
255 Element & elt = elements_.back();
260 if (elt.type == STRING) {
261 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
262 dim_.wid += elt.dim.wid;
267 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
268 Font const & f, Change const & ch)
271 Element e(INSET, pos, f, ch);
274 elements_.push_back(e);
279 void Row::add(pos_type const pos, char_type const c,
280 Font const & f, Change const & ch)
282 if (!sameString(f, ch)) {
284 Element e(STRING, pos, f, ch);
285 elements_.push_back(e);
287 //lyxerr << "FONT " <<back().font.language() << endl;
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 // A completion has no size
301 elements_.push_back(e);
306 void Row::addSeparator(pos_type const pos, char_type const c,
307 Font const & f, Change const & ch)
310 Element e(SEPARATOR, pos, f, ch);
312 e.dim.wid = theFontMetrics(f).width(c);
313 elements_.push_back(e);
314 dim_.wid += e.dim.wid;
318 void Row::addSpace(pos_type const pos, int const width,
319 Font const & f, Change const & ch)
322 Element e(SPACE, pos, f, ch);
324 elements_.push_back(e);
325 dim_.wid += e.dim.wid;
331 dim_.wid -= elements_.back().dim.wid;
332 elements_.pop_back();
336 void Row::shorten_if_needed(pos_type const keep, int const w)
338 if (empty() || width() < w)
340 int i = elements_.size();
342 int new_wid = dim_.wid;
343 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
345 new_end = elements_[i].pos;
346 new_wid -= elements_[i].dim.wid;
349 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
351 new_end = elements_[i].pos;
352 new_wid -= elements_[i].dim.wid;
355 if (elements_.size() != 1) {
356 LYXERR0("Row is too large but has more than one element. " << *this);
363 if (back().type != STRING)
366 pos_type new_pos = back().x2pos(xstr, true);
367 back().str = back().str.substr(0, new_pos);
368 back().endpos = new_pos;
375 elements_.erase(elements_.begin() + i, elements_.end());
379 void Row::reverseRTL()
382 pos_type const end = elements_.size();
385 while (i < end && !elements_[i].font.isRightToLeft())
390 // look for a RTL sequence
392 while (j < end && elements_[j].font.isRightToLeft())
394 reverse(elements_.begin() + i, elements_.begin() + j);