]> git.lyx.org Git - features.git/blob - src/Row.cpp
Merge branch 'rowpainter2'
[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         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                 break;
229         case Row::VIRTUAL:
230                 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
231                 break;
232         case Row::INSET:
233                 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
234                 break;
235         case Row::SPACE:
236                 os << "SPACE: ";
237                 break;
238         }
239         os << "width=" << e.full_width();
240         return os;
241 }
242
243
244 ostream & operator<<(ostream & os, Row const & row)
245 {
246         os << " pos: " << row.pos_ << " end: " << row.end_
247            << " left_margin: " << row.left_margin
248            << " width: " << row.dim_.wid
249            << " right_margin: " << row.right_margin
250            << " ascent: " << row.dim_.asc
251            << " descent: " << row.dim_.des
252            << " separator: " << row.separator
253            << " label_hfill: " << row.label_hfill 
254            << " row_boundary: " << row.right_boundary() << "\n";
255         double x = row.left_margin;
256         Row::Elements::const_iterator it = row.elements_.begin();
257         for ( ; it != row.elements_.end() ; ++it) {
258                 os << "x=" << x << " => " << *it << endl;
259                 x += it->full_width();
260         }
261         return os;
262 }
263
264
265 int Row::countSeparators() const
266 {
267         int n = 0;
268         const_iterator const end = elements_.end();
269         for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
270                 n += cit->countSeparators();
271         return n;
272 }
273
274
275 void Row::setSeparatorExtraWidth(double w)
276 {
277         separator = w;
278         iterator const end = elements_.end();
279         for (iterator it = elements_.begin() ; it != end ; ++it)
280                 if (it->type == Row::STRING)
281                         it->extra = w;
282 }
283
284
285 bool Row::sameString(Font const & f, Change const & ch) const
286 {
287         if (elements_.empty())
288                 return false;
289         Element const & elt = elements_.back();
290         return elt.type == STRING && !elt.final
291                    && elt.font == f && elt.change == ch;
292 }
293
294
295 void Row::finalizeLast()
296 {
297         if (elements_.empty())
298                 return;
299         Element & elt = elements_.back();
300         if (elt.final)
301                 return;
302         elt.final = true;
303
304         if (elt.type == STRING) {
305                 dim_.wid -= elt.dim.wid;
306                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
307                 dim_.wid += elt.dim.wid;
308         }
309 }
310
311
312 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
313               Font const & f, Change const & ch)
314 {
315         finalizeLast();
316         Element e(INSET, pos, f, ch);
317         e.inset = ins;
318         e.dim = dim;
319         elements_.push_back(e);
320         dim_.wid += dim.wid;
321 }
322
323
324 void Row::add(pos_type const pos, char_type const c,
325               Font const & f, Change const & ch)
326 {
327         if (!sameString(f, ch)) {
328                 finalizeLast();
329                 Element e(STRING, pos, f, ch);
330                 elements_.push_back(e);
331         }
332         if (back().str.length() % 30 == 0) {
333                 dim_.wid -= back().dim.wid;
334                 back().str += c;
335                 back().endpos = pos + 1;
336                 back().dim.wid = theFontMetrics(back().font).width(back().str);
337                 dim_.wid += back().dim.wid;
338         } else {
339                 back().str += c;
340                 back().endpos = pos + 1;
341         }
342 }
343
344
345 void Row::addVirtual(pos_type const pos, docstring const & s,
346                      Font const & f, Change const & ch)
347 {
348         finalizeLast();
349         Element e(VIRTUAL, pos, f, ch);
350         e.str = s;
351         e.dim.wid = theFontMetrics(f).width(s);
352         dim_.wid += e.dim.wid;
353         e.endpos = pos;
354         elements_.push_back(e);
355         finalizeLast();
356 }
357
358
359 void Row::addSpace(pos_type const pos, int const width,
360                    Font const & f, Change const & ch)
361 {
362         finalizeLast();
363         Element e(SPACE, pos, f, ch);
364         e.dim.wid = width;
365         elements_.push_back(e);
366         dim_.wid += e.dim.wid;
367 }
368
369
370 void Row::pop_back()
371 {
372         dim_.wid -= elements_.back().dim.wid;
373         elements_.pop_back();
374 }
375
376
377 void Row::shortenIfNeeded(pos_type const keep, int const w)
378 {
379         if (empty() || width() <= w)
380                 return;
381         Elements::iterator const beg = elements_.begin();
382         Elements::iterator const end = elements_.end();
383         int wid = left_margin;
384
385         Elements::iterator cit = beg;
386         for ( ; cit != end ; ++cit) {
387                 if (cit->endpos >= keep && wid + cit->dim.wid > w)
388                         break;
389                 wid += cit->dim.wid;
390         }
391
392         if (cit == end) {
393                 // This should not happen since the row is too long.
394                 LYXERR0("Something is wrong cannot shorten row: " << *this);
395                 return;
396         }
397
398         if (cit != beg && cit->type == VIRTUAL) {
399                 // It is not possible to separate a virtual element from the
400                 // previous one.
401                 --cit;
402                 wid -= cit->dim.wid;
403         }
404
405         // Try to break this row cleanly (at word boundary)
406         if (cit->breakAt(w - wid, false)) {
407                 end_ = cit->endpos;
408                 // after breakAt, there may be spaces at the end of the
409                 // string, but they are not counted in the string length
410                 // (qtextlayout feature, actually). We remove them, but do not
411                 // change the endo of the row, since the spaces at row break
412                 // are invisible.
413                 cit->str = rtrim(cit->str);
414                 cit->endpos = cit->pos + cit->str.length();
415                 dim_.wid = wid + cit->dim.wid;
416                 // If there are other elements, they should be removed.
417                 elements_.erase(next(cit, 1), end);
418                 return;
419         }
420
421         if (cit != beg) {
422                 // There is no separator, but several elements have been
423                 // added. We can cut right here.
424                 end_ = cit->pos;
425                 dim_.wid = wid;
426                 elements_.erase(cit, end);
427                 return;
428         }
429
430         /* If we are here, it means that we have not found a separator to
431          * shorten the row. Let's try to break it again, but not at word
432          * boundary this time.
433          */
434         if (cit->breakAt(w - wid, true)) {
435                 end_ = cit->endpos;
436                 // See comment above.
437                 cit->str = rtrim(cit->str);
438                 cit->endpos = cit->pos + cit->str.length();
439                 dim_.wid = wid + cit->dim.wid;
440                 // If there are other elements, they should be removed.
441                 elements_.erase(next(cit, 1), end);
442         }
443 }
444
445
446 void Row::reverseRTL(bool const rtl_par)
447 {
448         pos_type i = 0;
449         pos_type const end = elements_.size();
450         while (i < end) {
451                 // gather a sequence of elements with the same direction
452                 bool const rtl = elements_[i].font.isVisibleRightToLeft();
453                 pos_type j = i;
454                 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
455                         ++j;
456                 // if the direction is not the same as the paragraph
457                 // direction, the sequence has to be reverted.
458                 if (rtl != rtl_par)
459                         reverse(elements_.begin() + i, elements_.begin() + j);
460                 i = j;
461         }
462         // If the paragraph itself is RTL, reverse everything
463         if (rtl_par)
464                 reverse(elements_.begin(), elements_.end());
465 }
466
467 } // namespace lyx