]> git.lyx.org Git - features.git/blob - src/Row.cpp
New helper method Row::Element::isRTL()
[features.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::countSeparators() const
263 {
264         int n = 0;
265         const_iterator const end = elements_.end();
266         for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
267                 n += cit->countSeparators();
268         return n;
269 }
270
271
272 void Row::setSeparatorExtraWidth(double w)
273 {
274         separator = w;
275         iterator const end = elements_.end();
276         for (iterator it = elements_.begin() ; it != end ; ++it)
277                 if (it->type == Row::STRING)
278                         it->extra = w;
279 }
280
281
282 bool Row::sameString(Font const & f, Change const & ch) const
283 {
284         if (elements_.empty())
285                 return false;
286         Element const & elt = elements_.back();
287         return elt.type == STRING && !elt.final
288                    && elt.font == f && elt.change == ch;
289 }
290
291
292 void Row::finalizeLast()
293 {
294         if (elements_.empty())
295                 return;
296         Element & elt = elements_.back();
297         if (elt.final)
298                 return;
299         elt.final = true;
300
301         if (elt.type == STRING) {
302                 dim_.wid -= elt.dim.wid;
303                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
304                 dim_.wid += elt.dim.wid;
305         }
306 }
307
308
309 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
310               Font const & f, Change const & ch)
311 {
312         finalizeLast();
313         Element e(INSET, pos, f, ch);
314         e.inset = ins;
315         e.dim = dim;
316         elements_.push_back(e);
317         dim_.wid += dim.wid;
318 }
319
320
321 void Row::add(pos_type const pos, char_type const c,
322               Font const & f, Change const & ch)
323 {
324         if (!sameString(f, ch)) {
325                 finalizeLast();
326                 Element e(STRING, pos, f, ch);
327                 elements_.push_back(e);
328         }
329         if (back().str.length() % 30 == 0) {
330                 dim_.wid -= back().dim.wid;
331                 back().str += c;
332                 back().endpos = pos + 1;
333                 back().dim.wid = theFontMetrics(back().font).width(back().str);
334                 dim_.wid += back().dim.wid;
335         } else {
336                 back().str += c;
337                 back().endpos = pos + 1;
338         }
339 }
340
341
342 void Row::addVirtual(pos_type const pos, docstring const & s,
343                      Font const & f, Change const & ch)
344 {
345         finalizeLast();
346         Element e(VIRTUAL, pos, f, ch);
347         e.str = s;
348         e.dim.wid = theFontMetrics(f).width(s);
349         dim_.wid += e.dim.wid;
350         e.endpos = pos;
351         elements_.push_back(e);
352         finalizeLast();
353 }
354
355
356 void Row::addSpace(pos_type const pos, int const width,
357                    Font const & f, Change const & ch)
358 {
359         finalizeLast();
360         Element e(SPACE, pos, f, ch);
361         e.dim.wid = width;
362         elements_.push_back(e);
363         dim_.wid += e.dim.wid;
364 }
365
366
367 void Row::pop_back()
368 {
369         dim_.wid -= elements_.back().dim.wid;
370         elements_.pop_back();
371 }
372
373
374 void Row::shortenIfNeeded(pos_type const keep, int const w)
375 {
376         if (empty() || width() <= w)
377                 return;
378
379         Elements::iterator const beg = elements_.begin();
380         Elements::iterator const end = elements_.end();
381         int wid = left_margin;
382
383         // Search for the first element that goes beyond right margin
384         Elements::iterator cit = beg;
385         for ( ; cit != end ; ++cit) {
386                 if (wid + cit->dim.wid > w)
387                         break;
388                 wid += cit->dim.wid;
389         }
390
391         if (cit == end) {
392                 // This should not happen since the row is too long.
393                 LYXERR0("Something is wrong cannot shorten row: " << *this);
394                 return;
395         }
396
397         // Iterate backwards over breakable elements and try to break them
398         Elements::iterator cit_brk = cit;
399         int wid_brk = wid + cit_brk->dim.wid;
400         ++cit_brk;
401         while (cit_brk != beg) {
402                 --cit_brk;
403                 // make a copy of the element to work on it.
404                 Element brk = *cit_brk;
405                 wid_brk -= brk.dim.wid;
406                 if (brk.countSeparators() == 0 || brk.pos < keep)
407                         continue;
408                 /* We have found a suitable separable element. This is the common case.
409                  * Try to break it cleanly (at word boundary) at a length that is both
410                  * - less than the available space on the row
411                  * - shorter than the natural width of the element, in order to enforce
412                  *   break-up.
413                  */
414                 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
415                         /* if this element originally did not cause a row overflow
416                          * in itself, and the remainder of the row would still be
417                          * too large after breaking, then we will have issues in
418                          * next row. Thus breaking does not help.
419                          */
420                         if (wid_brk + cit_brk->dim.wid < w
421                             && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
422                                 break;
423                         }
424                         end_ = brk.endpos;
425                         /* after breakAt, there may be spaces at the end of the
426                          * string, but they are not counted in the string length
427                          * (QTextLayout feature, actually). We remove them, but do
428                          * not change the endo of the row, since the spaces at row
429                          * break are invisible.
430                          */
431                         brk.str = rtrim(brk.str);
432                         brk.endpos = brk.pos + brk.str.length();
433                         *cit_brk = brk;
434                         dim_.wid = wid_brk + brk.dim.wid;
435                         // If there are other elements, they should be removed.
436                         elements_.erase(cit_brk + 1, end);
437                         return;
438                 }
439         }
440
441         if (cit != beg && cit->type == VIRTUAL) {
442                 // It is not possible to separate a virtual element from the
443                 // previous one.
444                 --cit;
445                 wid -= cit->dim.wid;
446         }
447
448         if (cit != beg) {
449                 // There is no usable separator, but several elements have
450                 // been added. We can cut right here.
451                 end_ = cit->pos;
452                 dim_.wid = wid;
453                 elements_.erase(cit, end);
454                 return;
455         }
456
457         /* If we are here, it means that we have not found a separator to
458          * shorten the row. Let's try to break it again, but not at word
459          * boundary this time.
460          */
461         if (cit->breakAt(w - wid, true)) {
462                 end_ = cit->endpos;
463                 // See comment above.
464                 cit->str = rtrim(cit->str);
465                 cit->endpos = cit->pos + cit->str.length();
466                 dim_.wid = wid + cit->dim.wid;
467                 // If there are other elements, they should be removed.
468                 elements_.erase(next(cit, 1), end);
469         }
470 }
471
472
473 void Row::reverseRTL(bool const rtl_par)
474 {
475         pos_type i = 0;
476         pos_type const end = elements_.size();
477         while (i < end) {
478                 // gather a sequence of elements with the same direction
479                 bool const rtl = elements_[i].isRTL();
480                 pos_type j = i;
481                 while (j < end && elements_[j].isRTL() == rtl)
482                         ++j;
483                 // if the direction is not the same as the paragraph
484                 // direction, the sequence has to be reverted.
485                 if (rtl != rtl_par)
486                         reverse(elements_.begin() + i, elements_.begin() + j);
487                 i = j;
488         }
489         // If the paragraph itself is RTL, reverse everything
490         if (rtl_par)
491                 reverse(elements_.begin(), elements_.end());
492 }
493
494 } // namespace lyx