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