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