]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
5bdcbe004bbb46f7443a30e1b05c6e3e7160bf31
[lyx.git] / src / Row.cpp
1 /**
2  * \file Row.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  * \author Jean-Marc Lasgouttes
11  *
12  * Full author contact details are available in file CREDITS.
13  *
14  * Metrics for an on-screen text row.
15  */
16
17 #include <config.h>
18
19 #include "Row.h"
20
21 #include "DocIterator.h"
22
23 #include "frontends/FontMetrics.h"
24
25 #include "support/debug.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxalgo.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36 using support::rtrim;
37 using frontend::FontMetrics;
38
39
40 int Row::Element::countSeparators() const
41 {
42         if (type != STRING)
43                 return 0;
44         return count(str.begin(), str.end(), ' ');
45 }
46
47
48 double Row::Element::pos2x(pos_type const i) const
49 {
50         // This can happen with inline completion when clicking on the
51         // row after the completion.
52         if (i < pos || i > endpos)
53                 return 0;
54
55         double w = 0;
56         //handle first the two bounds of the element
57         if (i == endpos && type != VIRTUAL
58                 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
59                 w = isRTL() ? 0 : full_width();
60         else if (i == pos || type != STRING)
61                 w = isRTL() ? full_width() : 0;
62         else {
63                 FontMetrics const & fm = theFontMetrics(font);
64                 w = fm.pos2x(str, i - pos, isRTL(), extra);
65         }
66
67         return w;
68 }
69
70
71 pos_type Row::Element::x2pos(int &x) const
72 {
73         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
74         size_t i = 0;
75
76         switch (type) {
77         case STRING: {
78                 FontMetrics const & fm = theFontMetrics(font);
79                 i = fm.x2pos(str, x, isRTL(), extra);
80                 break;
81         }
82         case VIRTUAL:
83                 // those elements are actually empty (but they have a width)
84                 i = 0;
85                 x = isRTL() ? int(full_width()) : 0;
86                 break;
87         case INSET:
88         case SPACE:
89                 // those elements contain only one position. Round to
90                 // the closest side.
91                 if (x > full_width()) {
92                         x = int(full_width());
93                         i = !isRTL();
94                 } else {
95                         x = 0;
96                         i = isRTL();
97                 }
98
99         }
100         //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
101         return pos + i;
102
103 }
104
105
106 bool Row::Element::breakAt(int w, bool force)
107 {
108         if (type != STRING || dim.wid <= w)
109                 return false;
110
111         FontMetrics const & fm = theFontMetrics(font);
112         int x = w;
113         if(fm.breakAt(str, x, isRTL(), force)) {
114                 dim.wid = x;
115                 endpos = pos + str.length();
116                 //lyxerr << "breakAt(" << w << ")  Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
117                 return true;
118         }
119         return false;
120 }
121
122
123 pos_type Row::Element::left_pos() const
124 {
125         return isRTL() ? endpos : pos;
126 }
127
128
129 pos_type Row::Element::right_pos() const
130 {
131         return isRTL() ? pos : endpos;
132 }
133
134
135 Row::Row()
136         : separator(0), label_hfill(0), left_margin(0), right_margin(0),
137           sel_beg(-1), sel_end(-1),
138           begin_margin_sel(false), end_margin_sel(false),
139           changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
140 {}
141
142
143 void Row::setCrc(size_type crc) const
144 {
145         changed_ = crc != crc_;
146         crc_ = crc;
147 }
148
149
150 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
151                 DocIterator const & end) const
152 {
153         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
154         pos_type const margin_pos = left_margin ? pos_ : end_;
155
156         // Is the chosen margin selected ?
157         if (sel_pos == margin_pos) {
158                 if (beg.pos() == end.pos())
159                         // This is a special case in which the space between after
160                         // pos i-1 and before pos i is selected, i.e. the margins
161                         // (see DocIterator::boundary_).
162                         return beg.boundary() && !end.boundary();
163                 else if (end.pos() == margin_pos)
164                         // If the selection ends around the margin, it is only
165                         // drawn if the cursor is after the margin.
166                         return !end.boundary();
167                 else if (beg.pos() == margin_pos)
168                         // If the selection begins around the margin, it is
169                         // only drawn if the cursor is before the margin.
170                         return beg.boundary();
171                 else
172                         return true;
173         }
174         return false;
175 }
176
177
178 void Row::setSelectionAndMargins(DocIterator const & beg,
179                 DocIterator const & end) const
180 {
181         setSelection(beg.pos(), end.pos());
182
183         if (selection()) {
184                 end_margin_sel = isMarginSelected(false, beg, end);
185                 begin_margin_sel = isMarginSelected(true, beg, end);
186         }
187 }
188
189
190 void Row::setSelection(pos_type beg, pos_type end) const
191 {
192         if (pos_ >= beg && pos_ <= end)
193                 sel_beg = pos_;
194         else if (beg > pos_ && beg <= end_)
195                 sel_beg = beg;
196         else
197                 sel_beg = -1;
198
199         if (end_ >= beg && end_ <= end)
200                 sel_end = end_;
201         else if (end < end_ && end >= pos_)
202                 sel_end = end;
203         else
204                 sel_end = -1;
205 }
206
207
208 bool Row::selection() const
209 {
210         return sel_beg != -1 && sel_end != -1;
211 }
212
213
214 ostream & operator<<(ostream & os, Row::Element const & e)
215 {
216         if (e.isRTL())
217                 os << e.endpos << "<<" << e.pos << " ";
218         else
219                 os << e.pos << ">>" << e.endpos << " ";
220
221         switch (e.type) {
222         case Row::STRING:
223                 os << "STRING: `" << to_utf8(e.str) << "' ("
224                    << e.countSeparators() << " sep.), ";
225                 break;
226         case Row::VIRTUAL:
227                 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
228                 break;
229         case Row::INSET:
230                 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
231                 break;
232         case Row::SPACE:
233                 os << "SPACE: ";
234                 break;
235         }
236         os << "width=" << e.full_width();
237         return os;
238 }
239
240
241 ostream & operator<<(ostream & os, Row const & row)
242 {
243         os << " pos: " << row.pos_ << " end: " << row.end_
244            << " left_margin: " << row.left_margin
245            << " width: " << row.dim_.wid
246            << " right_margin: " << row.right_margin
247            << " ascent: " << row.dim_.asc
248            << " descent: " << row.dim_.des
249            << " separator: " << row.separator
250            << " label_hfill: " << row.label_hfill 
251            << " row_boundary: " << row.right_boundary() << "\n";
252         double x = row.left_margin;
253         Row::Elements::const_iterator it = row.elements_.begin();
254         for ( ; it != row.elements_.end() ; ++it) {
255                 os << "x=" << x << " => " << *it << endl;
256                 x += it->full_width();
257         }
258         return os;
259 }
260
261
262 int Row::left_x() const
263 {
264         double x = left_margin;
265         const_iterator const end = elements_.end();
266         const_iterator cit = elements_.begin();
267         while (cit != end && cit->isVirtual()) {
268                 x += cit->full_width();
269                 ++cit;
270         }
271         return int(x + 0.5);
272 }
273
274
275 int Row::right_x() const
276 {
277         double x = dim_.wid;
278         const_iterator const begin = elements_.begin();
279         const_iterator cit = elements_.end();
280         while (cit != begin) {
281                 --cit;
282                 if (cit->isVirtual())
283                         x -= cit->full_width();
284                 else
285                         break;
286         }
287         return int(x + 0.5);
288 }
289
290
291 int Row::countSeparators() const
292 {
293         int n = 0;
294         const_iterator const end = elements_.end();
295         for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
296                 n += cit->countSeparators();
297         return n;
298 }
299
300
301 void Row::setSeparatorExtraWidth(double w)
302 {
303         separator = w;
304         iterator const end = elements_.end();
305         for (iterator it = elements_.begin() ; it != end ; ++it)
306                 if (it->type == Row::STRING)
307                         it->extra = w;
308 }
309
310
311 bool Row::sameString(Font const & f, Change const & ch) const
312 {
313         if (elements_.empty())
314                 return false;
315         Element const & elt = elements_.back();
316         return elt.type == STRING && !elt.final
317                    && elt.font == f && elt.change == ch;
318 }
319
320
321 void Row::finalizeLast()
322 {
323         if (elements_.empty())
324                 return;
325         Element & elt = elements_.back();
326         if (elt.final)
327                 return;
328         elt.final = true;
329
330         if (elt.type == STRING) {
331                 dim_.wid -= elt.dim.wid;
332                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
333                 dim_.wid += elt.dim.wid;
334         }
335 }
336
337
338 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
339               Font const & f, Change const & ch)
340 {
341         finalizeLast();
342         Element e(INSET, pos, f, ch);
343         e.inset = ins;
344         e.dim = dim;
345         elements_.push_back(e);
346         dim_.wid += dim.wid;
347 }
348
349
350 void Row::add(pos_type const pos, char_type const c,
351               Font const & f, Change const & ch)
352 {
353         if (!sameString(f, ch)) {
354                 finalizeLast();
355                 Element e(STRING, pos, f, ch);
356                 elements_.push_back(e);
357         }
358         if (back().str.length() % 30 == 0) {
359                 dim_.wid -= back().dim.wid;
360                 back().str += c;
361                 back().endpos = pos + 1;
362                 back().dim.wid = theFontMetrics(back().font).width(back().str);
363                 dim_.wid += back().dim.wid;
364         } else {
365                 back().str += c;
366                 back().endpos = pos + 1;
367         }
368 }
369
370
371 void Row::addVirtual(pos_type const pos, docstring const & s,
372                      Font const & f, Change const & ch)
373 {
374         finalizeLast();
375         Element e(VIRTUAL, pos, f, ch);
376         e.str = s;
377         e.dim.wid = theFontMetrics(f).width(s);
378         dim_.wid += e.dim.wid;
379         e.endpos = pos;
380         elements_.push_back(e);
381         finalizeLast();
382 }
383
384
385 void Row::addSpace(pos_type const pos, int const width,
386                    Font const & f, Change const & ch)
387 {
388         finalizeLast();
389         Element e(SPACE, pos, f, ch);
390         e.dim.wid = width;
391         elements_.push_back(e);
392         dim_.wid += e.dim.wid;
393 }
394
395
396 void Row::pop_back()
397 {
398         dim_.wid -= elements_.back().dim.wid;
399         elements_.pop_back();
400 }
401
402
403 void Row::shortenIfNeeded(pos_type const keep, int const w)
404 {
405         if (empty() || width() <= w)
406                 return;
407
408         Elements::iterator const beg = elements_.begin();
409         Elements::iterator const end = elements_.end();
410         int wid = left_margin;
411
412         // Search for the first element that goes beyond right margin
413         Elements::iterator cit = beg;
414         for ( ; cit != end ; ++cit) {
415                 if (wid + cit->dim.wid > w)
416                         break;
417                 wid += cit->dim.wid;
418         }
419
420         if (cit == end) {
421                 // This should not happen since the row is too long.
422                 LYXERR0("Something is wrong cannot shorten row: " << *this);
423                 return;
424         }
425
426         // Iterate backwards over breakable elements and try to break them
427         Elements::iterator cit_brk = cit;
428         int wid_brk = wid + cit_brk->dim.wid;
429         ++cit_brk;
430         while (cit_brk != beg) {
431                 --cit_brk;
432                 // make a copy of the element to work on it.
433                 Element brk = *cit_brk;
434                 wid_brk -= brk.dim.wid;
435                 if (brk.countSeparators() == 0 || brk.pos < keep)
436                         continue;
437                 /* We have found a suitable separable element. This is the common case.
438                  * Try to break it cleanly (at word boundary) at a length that is both
439                  * - less than the available space on the row
440                  * - shorter than the natural width of the element, in order to enforce
441                  *   break-up.
442                  */
443                 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
444                         /* if this element originally did not cause a row overflow
445                          * in itself, and the remainder of the row would still be
446                          * too large after breaking, then we will have issues in
447                          * next row. Thus breaking does not help.
448                          */
449                         if (wid_brk + cit_brk->dim.wid < w
450                             && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
451                                 break;
452                         }
453                         end_ = brk.endpos;
454                         /* after breakAt, there may be spaces at the end of the
455                          * string, but they are not counted in the string length
456                          * (QTextLayout feature, actually). We remove them, but do
457                          * not change the endo of the row, since the spaces at row
458                          * break are invisible.
459                          */
460                         brk.str = rtrim(brk.str);
461                         brk.endpos = brk.pos + brk.str.length();
462                         *cit_brk = brk;
463                         dim_.wid = wid_brk + brk.dim.wid;
464                         // If there are other elements, they should be removed.
465                         elements_.erase(cit_brk + 1, end);
466                         return;
467                 }
468         }
469
470         if (cit != beg && cit->type == VIRTUAL) {
471                 // It is not possible to separate a virtual element from the
472                 // previous one.
473                 --cit;
474                 wid -= cit->dim.wid;
475         }
476
477         if (cit != beg) {
478                 // There is no usable separator, but several elements have
479                 // been added. We can cut right here.
480                 end_ = cit->pos;
481                 dim_.wid = wid;
482                 elements_.erase(cit, end);
483                 return;
484         }
485
486         /* If we are here, it means that we have not found a separator to
487          * shorten the row. Let's try to break it again, but not at word
488          * boundary this time.
489          */
490         if (cit->breakAt(w - wid, true)) {
491                 end_ = cit->endpos;
492                 // See comment above.
493                 cit->str = rtrim(cit->str);
494                 cit->endpos = cit->pos + cit->str.length();
495                 dim_.wid = wid + cit->dim.wid;
496                 // If there are other elements, they should be removed.
497                 elements_.erase(next(cit, 1), end);
498         }
499 }
500
501
502 void Row::reverseRTL(bool const rtl_par)
503 {
504         pos_type i = 0;
505         pos_type const end = elements_.size();
506         while (i < end) {
507                 // gather a sequence of elements with the same direction
508                 bool const rtl = elements_[i].isRTL();
509                 pos_type j = i;
510                 while (j < end && elements_[j].isRTL() == rtl)
511                         ++j;
512                 // if the direction is not the same as the paragraph
513                 // direction, the sequence has to be reverted.
514                 if (rtl != rtl_par)
515                         reverse(elements_.begin() + i, elements_.begin() + j);
516                 i = j;
517         }
518         // If the paragraph itself is RTL, reverse everything
519         if (rtl_par)
520                 reverse(elements_.begin(), elements_.end());
521 }
522
523 } // namespace lyx