]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
c32be85ee7d0d60d0ae7c8b7177070c8c32ea429
[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         bool const rtl = font.isVisibleRightToLeft();
40
41         // handle first the two bounds of the element
42         if ((!rtl && pos >= i) || (rtl && endpos <= i))
43                 return 0;
44         if ((!rtl && endpos <= i) || (rtl && pos >= i))
45                 return width();
46
47         FontMetrics const & fm = theFontMetrics(font);
48         // FIXME Avoid caching of metrics there?
49         int const w = fm.width(str.substr(0, i - pos));
50         if (rtl)
51                 return width() - w;
52         else
53                 return w;
54 }
55
56
57 pos_type Row::Element::x2pos(double &x) const
58 {
59         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
60         // if element is rtl, flip x value
61         bool const rtl = font.isVisibleRightToLeft();
62         double x2 = rtl ? (width() - x) : x;
63
64         FontMetrics const & fm = theFontMetrics(font);
65         double last_w = 0;
66         double w = 0;
67         size_t i = 1;
68         // non-STRING element only contain one position
69         if (type != STRING) {
70                 i = 0;
71                 w = width();
72         } else {
73                 // FIXME: implement dichotomy search?
74                 for ( ; i <= str.size() ; ++i) {
75                         last_w = w;
76                         w = fm.width(str.substr(0,i));
77                         if (w > x2) {
78                                 --i;
79                                 break;
80                         }
81                 }
82                 // if (i == str.size())
83                 //      lyxerr << " NOT FOUND ";
84         }
85
86         // round to the closest side
87         if (x2 - last_w > w - x2) {
88                 x2 = w;
89                 ++i;
90         } else
91                 x2 = last_w;
92
93         // is element is rtl, flip values
94         if (rtl) {
95                 x = last_w - x2;
96                 i = endpos - i;
97         } else {
98                 x = x2;
99                 i = pos + i;
100         }
101
102         //lyxerr << "=> p=" << i << " x=" << x << endl;
103         return i;
104 }
105
106
107 Row::Row()
108         : separator(0), label_hfill(0), x(0), right_margin(0),
109         sel_beg(-1), sel_end(-1),
110         begin_margin_sel(false), end_margin_sel(false),
111         changed_(false), crc_(0), pos_(0), end_(0)
112 {}
113
114
115 void Row::setCrc(size_type crc) const
116 {
117         changed_ = crc != crc_;
118         crc_ = crc;
119 }
120
121
122 void Row::pos(pos_type p)
123 {
124         pos_ = p;
125 }
126
127
128 void Row::endpos(pos_type p)
129 {
130         end_ = p;
131 }
132
133
134 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
135                 DocIterator const & end) const
136 {
137         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
138         pos_type const margin_pos = left_margin ? pos_ : end_;
139
140         // Is the chosen margin selected ?
141         if (sel_pos == margin_pos) {
142                 if (beg.pos() == end.pos())
143                         // This is a special case in which the space between after
144                         // pos i-1 and before pos i is selected, i.e. the margins
145                         // (see DocIterator::boundary_).
146                         return beg.boundary() && !end.boundary();
147                 else if (end.pos() == margin_pos)
148                         // If the selection ends around the margin, it is only
149                         // drawn if the cursor is after the margin.
150                         return !end.boundary();
151                 else if (beg.pos() == margin_pos)
152                         // If the selection begins around the margin, it is
153                         // only drawn if the cursor is before the margin.
154                         return beg.boundary();
155                 else
156                         return true;
157         }
158         return false;
159 }
160
161
162 void Row::setSelectionAndMargins(DocIterator const & beg,
163                 DocIterator const & end) const
164 {
165         setSelection(beg.pos(), end.pos());
166
167         if (selection()) {
168                 end_margin_sel = isMarginSelected(false, beg, end);
169                 begin_margin_sel = isMarginSelected(true, beg, end);
170         }
171 }
172
173
174 void Row::setSelection(pos_type beg, pos_type end) const
175 {
176         if (pos_ >= beg && pos_ <= end)
177                 sel_beg = pos_;
178         else if (beg > pos_ && beg <= end_)
179                 sel_beg = beg;
180         else
181                 sel_beg = -1;
182
183         if (end_ >= beg && end_ <= end)
184                 sel_end = end_;
185         else if (end < end_ && end >= pos_)
186                 sel_end = end;
187         else
188                 sel_end = -1;
189 }
190
191
192 bool Row::selection() const
193 {
194         return sel_beg != -1 && sel_end != -1;
195 }
196
197
198 ostream & operator<<(ostream & os, Row::Element const & e)
199 {
200         if (e.font.isVisibleRightToLeft())
201                 os << e.endpos << "<<" << e.pos << " ";
202         else
203                 os << e.pos << ">>" << e.endpos << " ";
204
205         switch (e.type) {
206         case Row::STRING:
207                 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
208                 break;
209         case Row::VIRTUAL:
210                 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
211                 break;
212         case Row::INSET:
213                 os << "INSET: " << to_utf8(e.inset->layoutName());
214                 break;
215         case Row::SEPARATOR:
216                 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
217                 break;
218         case Row::SPACE:
219                 os << "SPACE: " << e.dim.wid;
220                 break;
221         }
222         return os;
223 }
224
225
226 ostream & operator<<(ostream & os, Row const & row)
227 {
228         os << " pos: " << row.pos_ << " end: " << row.end_
229            << " x: " << row.x
230            << " width: " << row.dim_.wid
231            << " ascent: " << row.dim_.asc
232            << " descent: " << row.dim_.des
233            << " separator: " << row.separator
234            << " label_hfill : " << row.label_hfill << "\n";
235         Row::Elements::const_iterator it = row.elements_.begin();
236         for ( ; it != row.elements_.end() ; ++it) {
237                 os << "** " << *it << endl;
238         }
239         return os;
240 }
241
242
243 bool Row::sameString(Font const & f, Change const & ch) const
244 {
245         if (elements_.empty())
246                 return false;
247         Element const & elt = elements_.back();
248         return elt.type == STRING && !elt.final
249                    && elt.font == f && elt.change == ch;
250 }
251
252
253 void Row::finalizeLast()
254 {
255         if (elements_.empty())
256                 return;
257         Element & elt = elements_.back();
258         if (elt.final)
259                 return;
260         elt.final = true;
261
262         if (elt.type == STRING) {
263                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
264                 dim_.wid += elt.dim.wid;
265         }
266 }
267
268
269 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
270               Font const & f, Change const & ch)
271 {
272         finalizeLast();
273         Element e(INSET, pos, f, ch);
274         e.inset = ins;
275         e.dim = dim;
276         elements_.push_back(e);
277         dim_.wid += dim.wid;
278 }
279
280
281 void Row::add(pos_type const pos, char_type const c,
282               Font const & f, Change const & ch)
283 {
284         if (!sameString(f, ch)) {
285                 finalizeLast();
286                 Element e(STRING, pos, f, ch);
287                 elements_.push_back(e);
288         }
289         //lyxerr << "FONT " <<back().font.language() << endl;
290         back().str += c;
291         back().endpos = pos + 1;
292 }
293
294
295 void Row::addVirtual(pos_type const pos, docstring const & s,
296                      Font const & f, Change const & ch)
297 {
298         finalizeLast();
299         Element e(VIRTUAL, pos, f, ch);
300         e.str = s;
301         // A completion has no size
302         e.endpos = pos;
303         elements_.push_back(e);
304         finalizeLast();
305 }
306
307
308 void Row::addSeparator(pos_type const pos, char_type const c,
309                        Font const & f, Change const & ch)
310 {
311         finalizeLast();
312         Element e(SEPARATOR, pos, f, ch);
313         e.str += c;
314         e.dim.wid = theFontMetrics(f).width(c);
315         elements_.push_back(e);
316         dim_.wid += e.dim.wid;
317 }
318
319
320 void Row::addSpace(pos_type const pos, int const width,
321                    Font const & f, Change const & ch)
322 {
323         finalizeLast();
324         Element e(SPACE, pos, f, ch);
325         e.dim.wid = width;
326         elements_.push_back(e);
327         dim_.wid += e.dim.wid;
328 }
329
330
331 void Row::pop_back()
332 {
333         dim_.wid -= elements_.back().dim.wid;
334         elements_.pop_back();
335 }
336
337
338 void Row::separate_back(pos_type const keep)
339 {
340         if (empty())
341                 return;
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                 return;
358         end_ = new_end;
359         dim_.wid = new_wid;
360         elements_.erase(elements_.begin() + i, elements_.end());
361 }
362
363
364 void Row::reverseRtL()
365 {
366         pos_type i = 0;
367         pos_type const end = elements_.size();
368         while (i < end) {
369                 // skip LtR elements
370                 while (i < end && !elements_[i].font.isRightToLeft())
371                         ++i;
372                 if (i >= end)
373                         break;
374
375                 // look for a RtL sequence
376                 pos_type j = i;
377                 while (j < end && elements_[j].font.isRightToLeft())
378                         ++j;
379                 reverse(elements_.begin() + i, elements_.begin() + j);
380                 i = j;
381         }
382 }
383
384 } // namespace lyx