]> git.lyx.org Git - lyx.git/blob - src/Bidi.cpp
Fix bugs #8546 and #9055, and introduce new separator inset.
[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         if (!lyxrc.rtl_support) {
61                 start_ = -1;
62                 return;
63         }
64
65         if (par.inInset().forceLTR()) {
66                 start_ = -1;
67                 return;
68         }
69
70         start_ = row.pos();
71         end_ = row.endpos() - 1;
72
73         if (start_ > end_) {
74                 start_ = -1;
75                 return;
76         }
77
78         if (end_ + 2 - start_ >
79             static_cast<pos_type>(log2vis_list_.size())) {
80                 pos_type new_size =
81                         (end_ + 2 - start_ < 500) ?
82                         500 : 2 * (end_ + 2 - start_);
83                 log2vis_list_.resize(new_size);
84                 vis2log_list_.resize(new_size);
85                 levels_.resize(new_size);
86         }
87
88         vis2log_list_[end_ + 1 - start_] = -1;
89         log2vis_list_[end_ + 1 - start_] = -1;
90
91         BufferParams const & bufparams = buf.params();
92         pos_type stack[2];
93         bool const rtl_par = par.isRTL(bufparams);
94         int lev = 0;
95         bool rtl = false;
96         bool rtl0 = false;
97         pos_type const body_pos = par.beginOfBody();
98
99         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
100                 bool is_space = false;
101                 // We do not handle spaces around an RTL segment in a special way anymore.
102                 // Neither do we do so when generating the LaTeX, so setting is_space
103                 // to false makes the view in the GUI consistent with the output of LaTeX 
104                 // later. The old setting was:
105                 //bool is_space = par.isLineSeparator(lpos);
106                 // FIXME: once we're sure that this is what we really want, we should just
107                 // get rid of this variable...
108                 pos_type const pos =
109                         (is_space && lpos + 1 <= end_ &&
110                          !par.isLineSeparator(lpos + 1) &&
111                          !par.isEnvSeparator(lpos + 1) &&
112                          !par.isNewline(lpos + 1))
113                         ? lpos + 1 : lpos;
114
115                 Font const * font = &(par.getFontSettings(bufparams, pos));
116                 if (pos != lpos && 0 < lpos && rtl0 && font->isRightToLeft() &&
117                     font->fontInfo().number() == FONT_ON &&
118                     par.getFontSettings(bufparams, lpos - 1).fontInfo().number()
119                     == FONT_ON) {
120                         font = &(par.getFontSettings(bufparams, lpos));
121                         is_space = false;
122                 }
123                 bool new_rtl = font->isVisibleRightToLeft();
124                 bool new_rtl0 = font->isRightToLeft();
125
126                 int new_level;
127
128                 if (lpos == body_pos - 1
129                     && row.pos() < body_pos - 1
130                     && is_space) {
131                         new_level = rtl_par ? 1 : 0;
132                         new_rtl0 = rtl_par;
133                         new_rtl = rtl_par;
134                 } else if (new_rtl0) {
135                         new_level = new_rtl ? 1 : 2;
136                 } else {
137                         new_level = rtl_par ? 2 : 0;
138                 }
139
140                 if (is_space && new_level >= lev) {
141                         new_level = lev;
142                         new_rtl = rtl;
143                         new_rtl0 = rtl0;
144                 }
145
146                 int new_level2 = new_level;
147
148                 if (lev == new_level && rtl0 != new_rtl0) {
149                         --new_level2;
150                         log2vis_list_[lpos - start_] = rtl ? 1 : -1;
151                 } else if (lev < new_level) {
152                         log2vis_list_[lpos - start_] = rtl ? -1 : 1;
153                         if (new_level > 0 && !rtl_par)
154                                 same_direction_ = false;
155                 } else {
156                         log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
157                 }
158                 rtl = new_rtl;
159                 rtl0 = new_rtl0;
160                 levels_[lpos - start_] = new_level;
161
162                 while (lev > new_level2) {
163                         pos_type old_lpos = stack[--lev];
164                         int delta = lpos - old_lpos - 1;
165                         if (lev % 2)
166                                 delta = -delta;
167                         log2vis_list_[lpos - start_] += delta;
168                         log2vis_list_[old_lpos - start_] += delta;
169                 }
170                 while (lev < new_level)
171                         stack[lev++] = lpos;
172         }
173
174         while (lev > 0) {
175                 pos_type const old_lpos = stack[--lev];
176                 int delta = end_ - old_lpos;
177                 if (lev % 2)
178                         delta = -delta;
179                 log2vis_list_[old_lpos - start_] += delta;
180         }
181
182         pos_type vpos = start_ - 1;
183         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
184                 vpos += log2vis_list_[lpos - start_];
185                 vis2log_list_[vpos - start_] = lpos;
186                 log2vis_list_[lpos - start_] = vpos;
187         }
188 }
189
190
191 // This method requires a previous call to computeTables()
192 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
193         pos_type pos) const
194 {
195         if (!lyxrc.rtl_support || pos == 0)
196                 return false;
197
198         if (!inRange(pos - 1)) {
199                 // This can happen if pos is the first char of a row.
200                 // Returning false in this case is incorrect!
201                 return false;
202         }
203
204         bool const rtl = level(pos - 1) % 2;
205         bool const rtl2 = inRange(pos)
206                 ? level(pos) % 2
207                 : par.isRTL(buf.params());
208         return rtl != rtl2;
209 }
210
211
212 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
213         pos_type pos, Font const & font) const
214 {
215         if (!lyxrc.rtl_support)
216                 return false;    // This is just for speedup
217
218         bool const rtl = font.isVisibleRightToLeft();
219         bool const rtl2 = inRange(pos)
220                 ? level(pos) % 2
221                 : par.isRTL(buf.params());
222         return rtl != rtl2;
223 }
224
225
226 bool reverseDirectionNeeded(Cursor const & cur)
227 {
228         /*
229          * We determine the directions based on the direction of the
230          * bottom() --- i.e., outermost --- paragraph, because that is
231          * the only way to achieve consistency of the arrow's movements
232          * within a paragraph, and thus avoid situations in which the
233          * cursor gets stuck.
234          */
235         return cur.bottom().paragraph().isRTL(cur.bv().buffer().params());
236 }
237
238
239 bool isWithinRtlParagraph(Cursor const & cur)
240 {
241         return cur.innerParagraph().isRTL(cur.bv().buffer().params());
242 }
243
244 } // namespace lyx