]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
1a3bb247f036c7e829f60c4d39ce2983aedf00e8
[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, bool const low) 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 (!low && (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 = width() - x2;
96         } else {
97                 x = x2;
98         }
99
100         //lyxerr << "=> p=" << i << " x=" << x << endl;
101         return pos + i;
102 }
103
104
105 Row::Row()
106         : separator(0), label_hfill(0), x(0), right_margin(0),
107           sel_beg(-1), sel_end(-1),
108           begin_margin_sel(false), end_margin_sel(false),
109           changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
110 {}
111
112
113 void Row::setCrc(size_type crc) const
114 {
115         changed_ = crc != crc_;
116         crc_ = crc;
117 }
118
119
120 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
121                 DocIterator const & end) const
122 {
123         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
124         pos_type const margin_pos = left_margin ? pos_ : end_;
125
126         // Is the chosen margin selected ?
127         if (sel_pos == margin_pos) {
128                 if (beg.pos() == end.pos())
129                         // This is a special case in which the space between after
130                         // pos i-1 and before pos i is selected, i.e. the margins
131                         // (see DocIterator::boundary_).
132                         return beg.boundary() && !end.boundary();
133                 else if (end.pos() == margin_pos)
134                         // If the selection ends around the margin, it is only
135                         // drawn if the cursor is after the margin.
136                         return !end.boundary();
137                 else if (beg.pos() == margin_pos)
138                         // If the selection begins around the margin, it is
139                         // only drawn if the cursor is before the margin.
140                         return beg.boundary();
141                 else
142                         return true;
143         }
144         return false;
145 }
146
147
148 void Row::setSelectionAndMargins(DocIterator const & beg,
149                 DocIterator const & end) const
150 {
151         setSelection(beg.pos(), end.pos());
152
153         if (selection()) {
154                 end_margin_sel = isMarginSelected(false, beg, end);
155                 begin_margin_sel = isMarginSelected(true, beg, end);
156         }
157 }
158
159
160 void Row::setSelection(pos_type beg, pos_type end) const
161 {
162         if (pos_ >= beg && pos_ <= end)
163                 sel_beg = pos_;
164         else if (beg > pos_ && beg <= end_)
165                 sel_beg = beg;
166         else
167                 sel_beg = -1;
168
169         if (end_ >= beg && end_ <= end)
170                 sel_end = end_;
171         else if (end < end_ && end >= pos_)
172                 sel_end = end;
173         else
174                 sel_end = -1;
175 }
176
177
178 bool Row::selection() const
179 {
180         return sel_beg != -1 && sel_end != -1;
181 }
182
183
184 ostream & operator<<(ostream & os, Row::Element const & e)
185 {
186         if (e.font.isVisibleRightToLeft())
187                 os << e.endpos << "<<" << e.pos << " ";
188         else
189                 os << e.pos << ">>" << e.endpos << " ";
190
191         switch (e.type) {
192         case Row::STRING:
193                 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
194                 break;
195         case Row::VIRTUAL:
196                 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
197                 break;
198         case Row::INSET:
199                 os << "INSET: " << to_utf8(e.inset->layoutName());
200                 break;
201         case Row::SEPARATOR:
202                 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
203                 break;
204         case Row::SPACE:
205                 os << "SPACE: " << e.dim.wid;
206                 break;
207         }
208         return os;
209 }
210
211
212 ostream & operator<<(ostream & os, Row const & row)
213 {
214         os << " pos: " << row.pos_ << " end: " << row.end_
215            << " x: " << row.x
216            << " width: " << row.dim_.wid
217            << " ascent: " << row.dim_.asc
218            << " descent: " << row.dim_.des
219            << " separator: " << row.separator
220            << " label_hfill : " << row.label_hfill << "\n";
221         Row::Elements::const_iterator it = row.elements_.begin();
222         for ( ; it != row.elements_.end() ; ++it) {
223                 os << "** " << *it << endl;
224         }
225         return os;
226 }
227
228
229 bool Row::sameString(Font const & f, Change const & ch) const
230 {
231         if (elements_.empty())
232                 return false;
233         Element const & elt = elements_.back();
234         return elt.type == STRING && !elt.final
235                    && elt.font == f && elt.change == ch;
236 }
237
238
239 void Row::finalizeLast()
240 {
241         if (elements_.empty())
242                 return;
243         Element & elt = elements_.back();
244         if (elt.final)
245                 return;
246         elt.final = true;
247
248         if (elt.type == STRING) {
249                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
250                 dim_.wid += elt.dim.wid;
251         }
252 }
253
254
255 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
256               Font const & f, Change const & ch)
257 {
258         finalizeLast();
259         Element e(INSET, pos, f, ch);
260         e.inset = ins;
261         e.dim = dim;
262         elements_.push_back(e);
263         dim_.wid += dim.wid;
264 }
265
266
267 void Row::add(pos_type const pos, char_type const c,
268               Font const & f, Change const & ch)
269 {
270         if (!sameString(f, ch)) {
271                 finalizeLast();
272                 Element e(STRING, pos, f, ch);
273                 elements_.push_back(e);
274         }
275         //lyxerr << "FONT " <<back().font.language() << endl;
276         back().str += c;
277         back().endpos = pos + 1;
278 }
279
280
281 void Row::addVirtual(pos_type const pos, docstring const & s,
282                      Font const & f, Change const & ch)
283 {
284         finalizeLast();
285         Element e(VIRTUAL, pos, f, ch);
286         e.str = s;
287         // A completion has no size
288         e.endpos = pos;
289         elements_.push_back(e);
290         finalizeLast();
291 }
292
293
294 void Row::addSeparator(pos_type const pos, char_type const c,
295                        Font const & f, Change const & ch)
296 {
297         finalizeLast();
298         Element e(SEPARATOR, pos, f, ch);
299         e.str += c;
300         e.dim.wid = theFontMetrics(f).width(c);
301         elements_.push_back(e);
302         dim_.wid += e.dim.wid;
303 }
304
305
306 void Row::addSpace(pos_type const pos, int const width,
307                    Font const & f, Change const & ch)
308 {
309         finalizeLast();
310         Element e(SPACE, pos, f, ch);
311         e.dim.wid = width;
312         elements_.push_back(e);
313         dim_.wid += e.dim.wid;
314 }
315
316
317 void Row::pop_back()
318 {
319         dim_.wid -= elements_.back().dim.wid;
320         elements_.pop_back();
321 }
322
323
324 void Row::shorten_if_needed(pos_type const keep, int const w)
325 {
326         if (empty() || width() < w)
327                 return;
328
329         /** First, we try to remove elements one by one from the end
330          * until a separator is found.
331          */
332         int i = elements_.size();
333         int new_end = end_;
334         int new_wid = dim_.wid;
335         if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
336                 --i;
337                 new_end = elements_[i].pos;
338                 new_wid -= elements_[i].dim.wid;
339         }
340
341         while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
342                 --i;
343                 new_end = elements_[i].pos;
344                 new_wid -= elements_[i].dim.wid;
345         }
346         if (i == 0) {
347                 /* If we are here, it means that we have not found a
348                  * separator to shorten the row. There is one case
349                  * where we can do something: when we have one big
350                  * string, maybe with a paragraph marker after it.
351                  */
352                 Element & front = elements_.front();
353                 if (!(front.type == STRING
354                       && (elements_.size() == 1
355                           || (elements_.size() == 2
356                               && back().type == VIRTUAL))))
357                         return;
358
359                 // If this is a string element, we can try to split it.
360                 if (front.type != STRING)
361                         return;
362                 double xstr = w - x;
363                 // If there is a paragraph marker, it should be taken in account
364                 if (elements_.size() == 2)
365                         xstr -= back().width();
366                 pos_type new_pos = front.x2pos(xstr, true);
367                 front.str = front.str.substr(0, new_pos - pos_);
368                 front.dim.wid = xstr;
369                 front.endpos = new_pos;
370                 end_ = new_pos;
371                 dim_.wid = x + xstr;
372                 // If there is a paragraph marker, it should be removed.
373                 if (elements_.size() == 2)
374                         elements_.pop_back();
375                 return;
376         }
377         end_ = new_end;
378         dim_.wid = new_wid;
379         elements_.erase(elements_.begin() + i, elements_.end());
380 }
381
382
383 void Row::reverseRTL()
384 {
385         pos_type i = 0;
386         pos_type const end = elements_.size();
387         while (i < end) {
388                 // skip LtR elements
389                 while (i < end && !elements_[i].font.isRightToLeft())
390                         ++i;
391                 if (i >= end)
392                         break;
393
394                 // look for a RTL sequence
395                 pos_type j = i;
396                 while (j < end && elements_[j].font.isRightToLeft())
397                         ++j;
398                 reverse(elements_.begin() + i, elements_.begin() + j);
399                 i = j;
400         }
401 }
402
403 } // namespace lyx