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