]> git.lyx.org Git - lyx.git/blob - src/Bidi.cpp
#5502 add binding for full screen toggle on mac
[lyx.git] / src / Bidi.cpp
1 /**
2  * \file Bidi.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Bidi.h"
14 #include "Buffer.h"
15 #include "BufferView.h"
16 #include "Cursor.h"
17 #include "Font.h"
18 #include "Row.h"
19 #include "LyXRC.h"
20 #include "Paragraph.h"
21
22
23 namespace lyx {
24
25
26 pos_type Bidi::log2vis(pos_type pos) const
27 {
28         return (start_ == -1) ? pos : log2vis_list_[pos - start_];
29 }
30
31
32 pos_type Bidi::vis2log(pos_type pos) const
33 {
34         return (start_ == -1) ? pos : vis2log_list_[pos - start_];
35 }
36
37
38 pos_type Bidi::level(pos_type pos) const
39 {
40         return (start_ == -1) ? 0 : levels_[pos - start_];
41 }
42
43
44 bool Bidi::inRange(pos_type pos) const
45 {
46         return start_ == -1 || (start_ <= pos && pos <= end_);
47 }
48
49
50 bool Bidi::same_direction() const
51 {
52         return same_direction_;
53 }
54
55
56 void Bidi::computeTables(Paragraph const & par,
57         Buffer const & buf, Row const & row)
58 {
59         same_direction_ = true;
60
61         if (par.inInset().forceLTR()) {
62                 start_ = -1;
63                 return;
64         }
65
66         start_ = row.pos();
67         end_ = row.endpos() - 1;
68
69         if (start_ > end_) {
70                 start_ = -1;
71                 return;
72         }
73
74         if (end_ + 2 - start_ >
75             static_cast<pos_type>(log2vis_list_.size())) {
76                 pos_type new_size =
77                         (end_ + 2 - start_ < 500) ?
78                         500 : 2 * (end_ + 2 - start_);
79                 log2vis_list_.resize(new_size);
80                 vis2log_list_.resize(new_size);
81                 levels_.resize(new_size);
82         }
83
84         vis2log_list_[end_ + 1 - start_] = -1;
85         log2vis_list_[end_ + 1 - start_] = -1;
86
87         BufferParams const & bufparams = buf.params();
88         pos_type stack[2];
89         bool const rtl_par = par.isRTL(bufparams);
90         int lev = 0;
91         bool rtl = false;
92         bool rtl0 = false;
93         pos_type const body_pos = par.beginOfBody();
94
95         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
96                 bool is_space = false;
97                 // We do not handle spaces around an RTL segment in a special way anymore.
98                 // Neither do we do so when generating the LaTeX, so setting is_space
99                 // to false makes the view in the GUI consistent with the output of LaTeX 
100                 // later. The old setting was:
101                 //bool is_space = par.isLineSeparator(lpos);
102                 // FIXME: once we're sure that this is what we really want, we should just
103                 // get rid of this variable...
104                 pos_type const pos =
105                         (is_space && lpos + 1 <= end_ &&
106                          !par.isLineSeparator(lpos + 1) &&
107                          !par.isEnvSeparator(lpos + 1) &&
108                          !par.isNewline(lpos + 1))
109                         ? lpos + 1 : lpos;
110
111                 Font const * font = &(par.getFontSettings(bufparams, pos));
112                 if (pos != lpos && 0 < lpos && rtl0 && font->isRightToLeft() &&
113                     font->fontInfo().number() == FONT_ON &&
114                     par.getFontSettings(bufparams, lpos - 1).fontInfo().number()
115                     == FONT_ON) {
116                         font = &(par.getFontSettings(bufparams, lpos));
117                         is_space = false;
118                 }
119                 bool new_rtl = font->isVisibleRightToLeft();
120                 bool new_rtl0 = font->isRightToLeft();
121
122                 int new_level;
123
124                 if (lpos == body_pos - 1
125                     && row.pos() < body_pos - 1
126                     && is_space) {
127                         new_level = rtl_par ? 1 : 0;
128                         new_rtl0 = rtl_par;
129                         new_rtl = rtl_par;
130                 } else if (new_rtl0) {
131                         new_level = new_rtl ? 1 : 2;
132                 } else {
133                         new_level = rtl_par ? 2 : 0;
134                 }
135
136                 if (is_space && new_level >= lev) {
137                         new_level = lev;
138                         new_rtl = rtl;
139                         new_rtl0 = rtl0;
140                 }
141
142                 int new_level2 = new_level;
143
144                 if (lev == new_level && rtl0 != new_rtl0) {
145                         --new_level2;
146                         log2vis_list_[lpos - start_] = rtl ? 1 : -1;
147                 } else if (lev < new_level) {
148                         log2vis_list_[lpos - start_] = rtl ? -1 : 1;
149                         if (new_level > 0 && !rtl_par)
150                                 same_direction_ = false;
151                 } else {
152                         log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
153                 }
154                 rtl = new_rtl;
155                 rtl0 = new_rtl0;
156                 levels_[lpos - start_] = new_level;
157
158                 while (lev > new_level2) {
159                         pos_type old_lpos = stack[--lev];
160                         int delta = lpos - old_lpos - 1;
161                         if (lev % 2)
162                                 delta = -delta;
163                         log2vis_list_[lpos - start_] += delta;
164                         log2vis_list_[old_lpos - start_] += delta;
165                 }
166                 while (lev < new_level)
167                         stack[lev++] = lpos;
168         }
169
170         while (lev > 0) {
171                 pos_type const old_lpos = stack[--lev];
172                 int delta = end_ - old_lpos;
173                 if (lev % 2)
174                         delta = -delta;
175                 log2vis_list_[old_lpos - start_] += delta;
176         }
177
178         pos_type vpos = start_ - 1;
179         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
180                 vpos += log2vis_list_[lpos - start_];
181                 vis2log_list_[vpos - start_] = lpos;
182                 log2vis_list_[lpos - start_] = vpos;
183         }
184 }
185
186
187 // This method requires a previous call to computeTables()
188 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
189         pos_type pos) const
190 {
191         if (pos == 0)
192                 return false;
193
194         if (!inRange(pos - 1)) {
195                 // This can happen if pos is the first char of a row.
196                 // Returning false in this case is incorrect!
197                 return false;
198         }
199
200         bool const rtl = level(pos - 1) % 2;
201         bool const rtl2 = inRange(pos)
202                 ? level(pos) % 2
203                 : par.isRTL(buf.params());
204         return rtl != rtl2;
205 }
206
207
208 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
209         pos_type pos, Font const & font) const
210 {
211         bool const rtl = font.isVisibleRightToLeft();
212         bool const rtl2 = inRange(pos)
213                 ? level(pos) % 2
214                 : par.isRTL(buf.params());
215         return rtl != rtl2;
216 }
217
218
219 bool reverseDirectionNeeded(Cursor const & cur)
220 {
221         /*
222          * We determine the directions based on the direction of the
223          * bottom() --- i.e., outermost --- paragraph, because that is
224          * the only way to achieve consistency of the arrow's movements
225          * within a paragraph, and thus avoid situations in which the
226          * cursor gets stuck.
227          */
228         return cur.bottom().paragraph().isRTL(cur.bv().buffer().params());
229 }
230
231
232 bool isWithinRtlParagraph(Cursor const & cur)
233 {
234         return cur.innerParagraph().isRTL(cur.bv().buffer().params());
235 }
236
237 } // namespace lyx