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