]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
6e60e97cf9b191fe2a03c723efb2e93bd65be058
[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
28 #include <algorithm>
29 #include <ostream>
30
31 using namespace std;
32
33 namespace lyx {
34
35 using frontend::FontMetrics;
36
37 double Row::Element::pos2x(pos_type const i) const
38 {
39         LASSERT(i >= pos && i <= endpos, return 0);
40
41         bool const rtl = font.isVisibleRightToLeft();
42
43         int w = 0;
44         //handle first the two bounds of the element
45         if (i == pos)
46                 w = 0;
47         else if (i == endpos)
48                 w = width();
49         else {
50                 LASSERT(type == STRING, return 0);
51                 FontMetrics const & fm = theFontMetrics(font);
52                 // FIXME Avoid caching of metrics there?
53                 w = fm.width(str.substr(0, i - pos));
54         }
55
56         if (rtl)
57                 return width() - w;
58         else
59                 return w;
60 }
61
62
63 pos_type Row::Element::x2pos(double &x, bool const low) const
64 {
65         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66         // if element is rtl, flip x value
67         bool const rtl = font.isVisibleRightToLeft();
68         double x2 = rtl ? (width() - x) : x;
69
70         FontMetrics const & fm = theFontMetrics(font);
71         double last_w = 0;
72         double w = 0;
73         size_t i = 0;
74         // non-STRING element only contain one position
75         if (type != STRING) {
76                 w = width();
77         } else {
78                 // FIXME: implement dichotomy search?
79                 for ( ; i < str.size() ; ++i) {
80                         last_w = w;
81                         w = fm.width(str.substr(0, i + 1));
82                         if (w > x2)
83                                 break;
84                 }
85                 // if (i == str.size())
86                 //      lyxerr << " NOT FOUND ";
87         }
88
89         if (type == STRING && i == str.size())
90                 x2 = w;
91         // round to the closest side. The !rtl is here to obtain the
92         // same rounding as with the old code (this is cosmetic and
93         // can be eventually removed).
94         else if (!low && (x2 - last_w + !rtl > w - x2)) {
95                 x2 = w;
96                 ++i;
97         } else
98                 x2 = last_w;
99
100         // is element is rtl, flip values
101         if (rtl) {
102                 x = width() - x2;
103         } else {
104                 x = x2;
105         }
106
107         //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
108         return pos + i;
109 }
110
111
112 Row::Row()
113         : separator(0), label_hfill(0), x(0), right_margin(0),
114           sel_beg(-1), sel_end(-1),
115           begin_margin_sel(false), end_margin_sel(false),
116           changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
117 {}
118
119
120 void Row::setCrc(size_type crc) const
121 {
122         changed_ = crc != crc_;
123         crc_ = crc;
124 }
125
126
127 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
128                 DocIterator const & end) const
129 {
130         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
131         pos_type const margin_pos = left_margin ? pos_ : end_;
132
133         // Is the chosen margin selected ?
134         if (sel_pos == margin_pos) {
135                 if (beg.pos() == end.pos())
136                         // This is a special case in which the space between after
137                         // pos i-1 and before pos i is selected, i.e. the margins
138                         // (see DocIterator::boundary_).
139                         return beg.boundary() && !end.boundary();
140                 else if (end.pos() == margin_pos)
141                         // If the selection ends around the margin, it is only
142                         // drawn if the cursor is after the margin.
143                         return !end.boundary();
144                 else if (beg.pos() == margin_pos)
145                         // If the selection begins around the margin, it is
146                         // only drawn if the cursor is before the margin.
147                         return beg.boundary();
148                 else
149                         return true;
150         }
151         return false;
152 }
153
154
155 void Row::setSelectionAndMargins(DocIterator const & beg,
156                 DocIterator const & end) const
157 {
158         setSelection(beg.pos(), end.pos());
159
160         if (selection()) {
161                 end_margin_sel = isMarginSelected(false, beg, end);
162                 begin_margin_sel = isMarginSelected(true, beg, end);
163         }
164 }
165
166
167 void Row::setSelection(pos_type beg, pos_type end) const
168 {
169         if (pos_ >= beg && pos_ <= end)
170                 sel_beg = pos_;
171         else if (beg > pos_ && beg <= end_)
172                 sel_beg = beg;
173         else
174                 sel_beg = -1;
175
176         if (end_ >= beg && end_ <= end)
177                 sel_end = end_;
178         else if (end < end_ && end >= pos_)
179                 sel_end = end;
180         else
181                 sel_end = -1;
182 }
183
184
185 bool Row::selection() const
186 {
187         return sel_beg != -1 && sel_end != -1;
188 }
189
190
191 ostream & operator<<(ostream & os, Row::Element const & e)
192 {
193         if (e.font.isVisibleRightToLeft())
194                 os << e.endpos << "<<" << e.pos << " ";
195         else
196                 os << e.pos << ">>" << e.endpos << " ";
197
198         switch (e.type) {
199         case Row::STRING:
200                 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
201                 break;
202         case Row::VIRTUAL:
203                 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
204                 break;
205         case Row::INSET:
206                 os << "INSET: " << to_utf8(e.inset->layoutName());
207                 break;
208         case Row::SEPARATOR:
209                 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
210                 break;
211         case Row::SPACE:
212                 os << "SPACE: " << e.dim.wid;
213                 break;
214         }
215         return os;
216 }
217
218
219 ostream & operator<<(ostream & os, Row const & row)
220 {
221         os << " pos: " << row.pos_ << " end: " << row.end_
222            << " x: " << row.x
223            << " width: " << row.dim_.wid
224            << " right_margin: " << row.right_margin
225            << " ascent: " << row.dim_.asc
226            << " descent: " << row.dim_.des
227            << " separator: " << row.separator
228            << " label_hfill : " << row.label_hfill << "\n";
229         double x = row.x;
230         Row::Elements::const_iterator it = row.elements_.begin();
231         for ( ; it != row.elements_.end() ; ++it) {
232                 os << "x=" << x << " => " << *it << endl;
233                 x += it->width();
234         }
235         return os;
236 }
237
238
239 bool Row::sameString(Font const & f, Change const & ch) const
240 {
241         if (elements_.empty())
242                 return false;
243         Element const & elt = elements_.back();
244         return elt.type == STRING && !elt.final
245                    && elt.font == f && elt.change == ch;
246 }
247
248
249 void Row::finalizeLast()
250 {
251         if (elements_.empty())
252                 return;
253         Element & elt = elements_.back();
254         if (elt.final)
255                 return;
256         elt.final = true;
257
258         if (elt.type == STRING) {
259                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
260                 dim_.wid += elt.dim.wid;
261         }
262 }
263
264
265 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
266               Font const & f, Change const & ch)
267 {
268         finalizeLast();
269         Element e(INSET, pos, f, ch);
270         e.inset = ins;
271         e.dim = dim;
272         elements_.push_back(e);
273         dim_.wid += dim.wid;
274 }
275
276
277 void Row::add(pos_type const pos, char_type const c,
278               Font const & f, Change const & ch)
279 {
280         if (!sameString(f, ch)) {
281                 finalizeLast();
282                 Element e(STRING, pos, f, ch);
283                 elements_.push_back(e);
284         }
285         back().str += c;
286         back().endpos = pos + 1;
287 }
288
289
290 void Row::addVirtual(pos_type const pos, docstring const & s,
291                      Font const & f, Change const & ch)
292 {
293         finalizeLast();
294         Element e(VIRTUAL, pos, f, ch);
295         e.str = s;
296         e.dim.wid = theFontMetrics(f).width(s);
297         dim_.wid += e.dim.wid;
298         e.endpos = pos;
299         elements_.push_back(e);
300         finalizeLast();
301 }
302
303
304 void Row::addSeparator(pos_type const pos, char_type const c,
305                        Font const & f, Change const & ch)
306 {
307         finalizeLast();
308         Element e(SEPARATOR, pos, f, ch);
309         e.str += c;
310         e.dim.wid = theFontMetrics(f).width(c);
311         elements_.push_back(e);
312         dim_.wid += e.dim.wid;
313 }
314
315
316 void Row::addSpace(pos_type const pos, int const width,
317                    Font const & f, Change const & ch)
318 {
319         finalizeLast();
320         Element e(SPACE, pos, f, ch);
321         e.dim.wid = width;
322         elements_.push_back(e);
323         dim_.wid += e.dim.wid;
324 }
325
326
327 void Row::pop_back()
328 {
329         dim_.wid -= elements_.back().dim.wid;
330         elements_.pop_back();
331 }
332
333
334 void Row::shorten_if_needed(pos_type const keep, int const w)
335 {
336         if (empty() || width() < w)
337                 return;
338
339         /** First, we try to remove elements one by one from the end
340          * until a separator is found.
341          */
342         int i = elements_.size();
343         int new_end = end_;
344         int new_wid = dim_.wid;
345         if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
346                 --i;
347                 new_end = elements_[i].pos;
348                 new_wid -= elements_[i].dim.wid;
349         }
350
351         while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
352                 --i;
353                 new_end = elements_[i].pos;
354                 new_wid -= elements_[i].dim.wid;
355         }
356         if (i == 0) {
357                 /* If we are here, it means that we have not found a
358                  * separator to shorten the row. There is one case
359                  * where we can do something: when we have one big
360                  * string, maybe with a paragraph marker after it.
361                  */
362                 Element & front = elements_.front();
363                 if (!(front.type == STRING
364                       && (elements_.size() == 1
365                           || (elements_.size() == 2
366                               && back().type == VIRTUAL))))
367                         return;
368
369                 // If this is a string element, we can try to split it.
370                 if (front.type != STRING)
371                         return;
372                 double xstr = w - x;
373                 // If there is a paragraph marker, it should be taken in account
374                 if (elements_.size() == 2)
375                         xstr -= back().width();
376                 pos_type new_pos = front.x2pos(xstr, true);
377                 front.str = front.str.substr(0, new_pos - pos_);
378                 front.dim.wid = xstr;
379                 front.endpos = new_pos;
380                 end_ = new_pos;
381                 dim_.wid = x + xstr;
382                 // If there is a paragraph marker, it should be removed.
383                 if (elements_.size() == 2)
384                         elements_.pop_back();
385                 return;
386         }
387         end_ = new_end;
388         dim_.wid = new_wid;
389         elements_.erase(elements_.begin() + i, elements_.end());
390 }
391
392
393 void Row::reverseRTL(bool const rtl_par)
394 {
395         pos_type i = 0;
396         pos_type const end = elements_.size();
397         while (i < end) {
398                 // gather a sequence of elements with the same direction
399                 bool const rtl = elements_[i].font.isVisibleRightToLeft();
400                 pos_type j = i;
401                 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
402                         ++j;
403                 // if the direction is not the same as the paragraph
404                 // direction, the sequence has to be reverted.
405                 if (rtl != rtl_par)
406                         reverse(elements_.begin() + i, elements_.begin() + j);
407                 i = j;
408         }
409         // If the paragraph itself is RTL, reverse everything
410         if (rtl_par)
411                 reverse(elements_.begin(), elements_.end());
412 }
413
414 } // namespace lyx