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 ";
91 // round to the closest side
92 else if (!low && (x2 - last_w > w - x2)) {
98 // is element is rtl, flip values
105 //lyxerr << "=> p=" << i << " x=" << x << endl;
111 : separator(0), label_hfill(0), x(0), right_margin(0),
112 sel_beg(-1), sel_end(-1),
113 begin_margin_sel(false), end_margin_sel(false),
114 changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
118 void Row::setCrc(size_type crc) const
120 changed_ = crc != crc_;
125 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
126 DocIterator const & end) const
128 pos_type const sel_pos = left_margin ? sel_beg : sel_end;
129 pos_type const margin_pos = left_margin ? pos_ : end_;
131 // Is the chosen margin selected ?
132 if (sel_pos == margin_pos) {
133 if (beg.pos() == end.pos())
134 // This is a special case in which the space between after
135 // pos i-1 and before pos i is selected, i.e. the margins
136 // (see DocIterator::boundary_).
137 return beg.boundary() && !end.boundary();
138 else if (end.pos() == margin_pos)
139 // If the selection ends around the margin, it is only
140 // drawn if the cursor is after the margin.
141 return !end.boundary();
142 else if (beg.pos() == margin_pos)
143 // If the selection begins around the margin, it is
144 // only drawn if the cursor is before the margin.
145 return beg.boundary();
153 void Row::setSelectionAndMargins(DocIterator const & beg,
154 DocIterator const & end) const
156 setSelection(beg.pos(), end.pos());
159 end_margin_sel = isMarginSelected(false, beg, end);
160 begin_margin_sel = isMarginSelected(true, beg, end);
165 void Row::setSelection(pos_type beg, pos_type end) const
167 if (pos_ >= beg && pos_ <= end)
169 else if (beg > pos_ && beg <= end_)
174 if (end_ >= beg && end_ <= end)
176 else if (end < end_ && end >= pos_)
183 bool Row::selection() const
185 return sel_beg != -1 && sel_end != -1;
189 ostream & operator<<(ostream & os, Row::Element const & e)
191 if (e.font.isVisibleRightToLeft())
192 os << e.endpos << "<<" << e.pos << " ";
194 os << e.pos << ">>" << e.endpos << " ";
198 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
201 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
204 os << "INSET: " << to_utf8(e.inset->layoutName());
207 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
210 os << "SPACE: " << e.dim.wid;
217 ostream & operator<<(ostream & os, Row const & row)
219 os << " pos: " << row.pos_ << " end: " << row.end_
221 << " width: " << row.dim_.wid
222 << " right_margin: " << row.right_margin
223 << " ascent: " << row.dim_.asc
224 << " descent: " << row.dim_.des
225 << " separator: " << row.separator
226 << " label_hfill : " << row.label_hfill << "\n";
228 Row::Elements::const_iterator it = row.elements_.begin();
229 for ( ; it != row.elements_.end() ; ++it) {
230 os << "x=" << x << " => " << *it << endl;
237 bool Row::sameString(Font const & f, Change const & ch) const
239 if (elements_.empty())
241 Element const & elt = elements_.back();
242 return elt.type == STRING && !elt.final
243 && elt.font == f && elt.change == ch;
247 void Row::finalizeLast()
249 if (elements_.empty())
251 Element & elt = elements_.back();
256 if (elt.type == STRING) {
257 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
258 dim_.wid += elt.dim.wid;
263 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
264 Font const & f, Change const & ch)
267 Element e(INSET, pos, f, ch);
270 elements_.push_back(e);
275 void Row::add(pos_type const pos, char_type const c,
276 Font const & f, Change const & ch)
278 if (!sameString(f, ch)) {
280 Element e(STRING, pos, f, ch);
281 elements_.push_back(e);
283 //lyxerr << "FONT " <<back().font.language() << endl;
285 back().endpos = pos + 1;
289 void Row::addVirtual(pos_type const pos, docstring const & s,
290 Font const & f, Change const & ch)
293 Element e(VIRTUAL, pos, f, ch);
295 // A completion has no size
297 elements_.push_back(e);
302 void Row::addSeparator(pos_type const pos, char_type const c,
303 Font const & f, Change const & ch)
306 Element e(SEPARATOR, pos, f, ch);
308 e.dim.wid = theFontMetrics(f).width(c);
309 elements_.push_back(e);
310 dim_.wid += e.dim.wid;
314 void Row::addSpace(pos_type const pos, int const width,
315 Font const & f, Change const & ch)
318 Element e(SPACE, pos, f, ch);
320 elements_.push_back(e);
321 dim_.wid += e.dim.wid;
327 dim_.wid -= elements_.back().dim.wid;
328 elements_.pop_back();
332 void Row::shorten_if_needed(pos_type const keep, int const w)
334 if (empty() || width() < w)
337 /** First, we try to remove elements one by one from the end
338 * until a separator is found.
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 we are here, it means that we have not found a
356 * separator to shorten the row. There is one case
357 * where we can do something: when we have one big
358 * string, maybe with a paragraph marker after it.
360 Element & front = elements_.front();
361 if (!(front.type == STRING
362 && (elements_.size() == 1
363 || (elements_.size() == 2
364 && back().type == VIRTUAL))))
367 // If this is a string element, we can try to split it.
368 if (front.type != STRING)
371 // If there is a paragraph marker, it should be taken in account
372 if (elements_.size() == 2)
373 xstr -= back().width();
374 pos_type new_pos = front.x2pos(xstr, true);
375 front.str = front.str.substr(0, new_pos - pos_);
376 front.dim.wid = xstr;
377 front.endpos = new_pos;
380 // If there is a paragraph marker, it should be removed.
381 if (elements_.size() == 2)
382 elements_.pop_back();
387 elements_.erase(elements_.begin() + i, elements_.end());
391 void Row::reverseRTL(bool const rtl_par)
394 pos_type const end = elements_.size();
396 // gather a sequence of elements with the same direction
397 bool const rtl = elements_[i].font.isVisibleRightToLeft();
399 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
401 // if the direction is not the same as the paragraph
402 // direction, the sequence has to be reverted.
404 reverse(elements_.begin() + i, elements_.begin() + j);
407 // If the paragraph itself is RTL, reverse everything
409 reverse(elements_.begin(), elements_.end());