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) 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 (x2 - last_w > w - x2) {
93 // is element is rtl, flip values
102 //lyxerr << "=> p=" << i << " x=" << x << endl;
108 : separator(0), label_hfill(0), x(0), right_margin(0),
109 sel_beg(-1), sel_end(-1),
110 begin_margin_sel(false), end_margin_sel(false),
111 changed_(false), crc_(0), pos_(0), end_(0)
115 void Row::setCrc(size_type crc) const
117 changed_ = crc != crc_;
122 void Row::pos(pos_type p)
128 void Row::endpos(pos_type p)
134 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
135 DocIterator const & end) const
137 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
138 pos_type const margin_pos = left_margin ? pos_ : end_;
140 // Is the chosen margin selected ?
141 if (sel_pos == margin_pos) {
142 if (beg.pos() == end.pos())
143 // This is a special case in which the space between after
144 // pos i-1 and before pos i is selected, i.e. the margins
145 // (see DocIterator::boundary_).
146 return beg.boundary() && !end.boundary();
147 else if (end.pos() == margin_pos)
148 // If the selection ends around the margin, it is only
149 // drawn if the cursor is after the margin.
150 return !end.boundary();
151 else if (beg.pos() == margin_pos)
152 // If the selection begins around the margin, it is
153 // only drawn if the cursor is before the margin.
154 return beg.boundary();
162 void Row::setSelectionAndMargins(DocIterator const & beg,
163 DocIterator const & end) const
165 setSelection(beg.pos(), end.pos());
168 end_margin_sel = isMarginSelected(false, beg, end);
169 begin_margin_sel = isMarginSelected(true, beg, end);
174 void Row::setSelection(pos_type beg, pos_type end) const
176 if (pos_ >= beg && pos_ <= end)
178 else if (beg > pos_ && beg <= end_)
183 if (end_ >= beg && end_ <= end)
185 else if (end < end_ && end >= pos_)
192 bool Row::selection() const
194 return sel_beg != -1 && sel_end != -1;
198 ostream & operator<<(ostream & os, Row::Element const & e)
200 if (e.font.isVisibleRightToLeft())
201 os << e.endpos << "<<" << e.pos << " ";
203 os << e.pos << ">>" << e.endpos << " ";
207 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
210 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
213 os << "INSET: " << to_utf8(e.inset->layoutName());
216 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
219 os << "SPACE: " << e.dim.wid;
226 ostream & operator<<(ostream & os, Row const & row)
228 os << " pos: " << row.pos_ << " end: " << row.end_
230 << " width: " << row.dim_.wid
231 << " ascent: " << row.dim_.asc
232 << " descent: " << row.dim_.des
233 << " separator: " << row.separator
234 << " label_hfill : " << row.label_hfill << "\n";
235 Row::Elements::const_iterator it = row.elements_.begin();
236 for ( ; it != row.elements_.end() ; ++it) {
237 os << "** " << *it << endl;
243 bool Row::sameString(Font const & f, Change const & ch) const
245 if (elements_.empty())
247 Element const & elt = elements_.back();
248 return elt.type == STRING && !elt.final
249 && elt.font == f && elt.change == ch;
253 void Row::finalizeLast()
255 if (elements_.empty())
257 Element & elt = elements_.back();
262 if (elt.type == STRING) {
263 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
264 dim_.wid += elt.dim.wid;
269 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
270 Font const & f, Change const & ch)
273 Element e(INSET, pos, f, ch);
276 elements_.push_back(e);
281 void Row::add(pos_type const pos, char_type const c,
282 Font const & f, Change const & ch)
284 if (!sameString(f, ch)) {
286 Element e(STRING, pos, f, ch);
287 elements_.push_back(e);
289 //lyxerr << "FONT " <<back().font.language() << endl;
291 back().endpos = pos + 1;
295 void Row::addVirtual(pos_type const pos, docstring const & s,
296 Font const & f, Change const & ch)
299 Element e(VIRTUAL, pos, f, ch);
301 // A completion has no size
303 elements_.push_back(e);
308 void Row::addSeparator(pos_type const pos, char_type const c,
309 Font const & f, Change const & ch)
312 Element e(SEPARATOR, pos, f, ch);
314 e.dim.wid = theFontMetrics(f).width(c);
315 elements_.push_back(e);
316 dim_.wid += e.dim.wid;
320 void Row::addSpace(pos_type const pos, int const width,
321 Font const & f, Change const & ch)
324 Element e(SPACE, pos, f, ch);
326 elements_.push_back(e);
327 dim_.wid += e.dim.wid;
333 dim_.wid -= elements_.back().dim.wid;
334 elements_.pop_back();
338 void Row::separate_back(pos_type const keep)
342 int i = elements_.size();
344 int new_wid = dim_.wid;
345 if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
347 new_end = elements_[i].pos;
348 new_wid -= elements_[i].dim.wid;
351 while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
353 new_end = elements_[i].pos;
354 new_wid -= elements_[i].dim.wid;
360 elements_.erase(elements_.begin() + i, elements_.end());
364 void Row::reverseRtL()
367 pos_type const end = elements_.size();
370 while (i < end && !elements_[i].font.isRightToLeft())
375 // look for a RtL sequence
377 while (j < end && elements_[j].font.isRightToLeft())
379 reverse(elements_.begin() + i, elements_.begin() + j);