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