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 LASSERT(i >= pos && i <= endpos, return 0);
41 bool const rtl = font.isVisibleRightToLeft();
44 //handle first the two bounds of the element
50 LASSERT(type == STRING, return 0);
51 FontMetrics const & fm = theFontMetrics(font);
52 // FIXME Avoid caching of metrics there?
53 w = fm.width(str.substr(0, i - pos));
63 pos_type Row::Element::x2pos(double &x, bool const low) const
65 //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66 // if element is rtl, flip x value
67 bool const rtl = font.isVisibleRightToLeft();
68 double x2 = rtl ? (width() - x) : x;
70 FontMetrics const & fm = theFontMetrics(font);
74 // non-STRING element only contain one position
78 // FIXME: implement dichotomy search?
79 for ( ; i < str.size() ; ++i) {
81 w = fm.width(str.substr(0, i + 1));
85 // if (i == str.size())
86 // lyxerr << " NOT FOUND ";
89 if (type == STRING && i == str.size())
91 // round to the closest side. The !rtl is here to obtain the
92 // same rounding as with the old code (this is cosmetic and
93 // can be eventually removed).
94 else if (!low && (x2 - last_w + !rtl > w - x2)) {
100 // is element is rtl, flip values
107 //lyxerr << "=> p=" << i << " x=" << x << endl;
113 : separator(0), label_hfill(0), x(0), right_margin(0),
114 sel_beg(-1), sel_end(-1),
115 begin_margin_sel(false), end_margin_sel(false),
116 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
120 void Row::setCrc(size_type crc) const
122 changed_ = crc != crc_;
127 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
128 DocIterator const & end) const
130 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
131 pos_type const margin_pos = left_margin ? pos_ : end_;
133 // Is the chosen margin selected ?
134 if (sel_pos == margin_pos) {
135 if (beg.pos() == end.pos())
136 // This is a special case in which the space between after
137 // pos i-1 and before pos i is selected, i.e. the margins
138 // (see DocIterator::boundary_).
139 return beg.boundary() && !end.boundary();
140 else if (end.pos() == margin_pos)
141 // If the selection ends around the margin, it is only
142 // drawn if the cursor is after the margin.
143 return !end.boundary();
144 else if (beg.pos() == margin_pos)
145 // If the selection begins around the margin, it is
146 // only drawn if the cursor is before the margin.
147 return beg.boundary();
155 void Row::setSelectionAndMargins(DocIterator const & beg,
156 DocIterator const & end) const
158 setSelection(beg.pos(), end.pos());
161 end_margin_sel = isMarginSelected(false, beg, end);
162 begin_margin_sel = isMarginSelected(true, beg, end);
167 void Row::setSelection(pos_type beg, pos_type end) const
169 if (pos_ >= beg && pos_ <= end)
171 else if (beg > pos_ && beg <= end_)
176 if (end_ >= beg && end_ <= end)
178 else if (end < end_ && end >= pos_)
185 bool Row::selection() const
187 return sel_beg != -1 && sel_end != -1;
191 ostream & operator<<(ostream & os, Row::Element const & e)
193 if (e.font.isVisibleRightToLeft())
194 os << e.endpos << "<<" << e.pos << " ";
196 os << e.pos << ">>" << e.endpos << " ";
200 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
203 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
206 os << "INSET: " << to_utf8(e.inset->layoutName());
209 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
212 os << "SPACE: " << e.dim.wid;
219 ostream & operator<<(ostream & os, Row const & row)
221 os << " pos: " << row.pos_ << " end: " << row.end_
223 << " width: " << row.dim_.wid
224 << " right_margin: " << row.right_margin
225 << " ascent: " << row.dim_.asc
226 << " descent: " << row.dim_.des
227 << " separator: " << row.separator
228 << " label_hfill : " << row.label_hfill << "\n";
230 Row::Elements::const_iterator it = row.elements_.begin();
231 for ( ; it != row.elements_.end() ; ++it) {
232 os << "x=" << x << " => " << *it << endl;
239 bool Row::sameString(Font const & f, Change const & ch) const
241 if (elements_.empty())
243 Element const & elt = elements_.back();
244 return elt.type == STRING && !elt.final
245 && elt.font == f && elt.change == ch;
249 void Row::finalizeLast()
251 if (elements_.empty())
253 Element & elt = elements_.back();
258 if (elt.type == STRING) {
259 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
260 dim_.wid += elt.dim.wid;
265 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
266 Font const & f, Change const & ch)
269 Element e(INSET, pos, f, ch);
272 elements_.push_back(e);
277 void Row::add(pos_type const pos, char_type const c,
278 Font const & f, Change const & ch)
280 if (!sameString(f, ch)) {
282 Element e(STRING, pos, f, ch);
283 elements_.push_back(e);
285 //lyxerr << "FONT " <<back().font.language() << endl;
287 back().endpos = pos + 1;
291 void Row::addVirtual(pos_type const pos, docstring const & s,
292 Font const & f, Change const & ch)
295 Element e(VIRTUAL, pos, f, ch);
297 // A completion has no size
299 elements_.push_back(e);
304 void Row::addSeparator(pos_type const pos, char_type const c,
305 Font const & f, Change const & ch)
308 Element e(SEPARATOR, pos, f, ch);
310 e.dim.wid = theFontMetrics(f).width(c);
311 elements_.push_back(e);
312 dim_.wid += e.dim.wid;
316 void Row::addSpace(pos_type const pos, int const width,
317 Font const & f, Change const & ch)
320 Element e(SPACE, pos, f, ch);
322 elements_.push_back(e);
323 dim_.wid += e.dim.wid;
329 dim_.wid -= elements_.back().dim.wid;
330 elements_.pop_back();
334 void Row::shorten_if_needed(pos_type const keep, int const w)
336 if (empty() || width() < w)
339 /** First, we try to remove elements one by one from the end
340 * until a separator is found.
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;
357 /* If we are here, it means that we have not found a
358 * separator to shorten the row. There is one case
359 * where we can do something: when we have one big
360 * string, maybe with a paragraph marker after it.
362 Element & front = elements_.front();
363 if (!(front.type == STRING
364 && (elements_.size() == 1
365 || (elements_.size() == 2
366 && back().type == VIRTUAL))))
369 // If this is a string element, we can try to split it.
370 if (front.type != STRING)
373 // If there is a paragraph marker, it should be taken in account
374 if (elements_.size() == 2)
375 xstr -= back().width();
376 pos_type new_pos = front.x2pos(xstr, true);
377 front.str = front.str.substr(0, new_pos - pos_);
378 front.dim.wid = xstr;
379 front.endpos = new_pos;
382 // If there is a paragraph marker, it should be removed.
383 if (elements_.size() == 2)
384 elements_.pop_back();
389 elements_.erase(elements_.begin() + i, elements_.end());
393 void Row::reverseRTL(bool const rtl_par)
396 pos_type const end = elements_.size();
398 // gather a sequence of elements with the same direction
399 bool const rtl = elements_[i].font.isVisibleRightToLeft();
401 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
403 // if the direction is not the same as the paragraph
404 // direction, the sequence has to be reverted.
406 reverse(elements_.begin() + i, elements_.begin() + j);
409 // If the paragraph itself is RTL, reverse everything
411 reverse(elements_.begin(), elements_.end());