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