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