]> git.lyx.org Git - lyx.git/blob - src/Bidi.C
more cursor dispatch
[lyx.git] / src / Bidi.C
1 /**
2  * \file Bidi.C
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
12 #include "Bidi.h"
13 #include "buffer.h"
14 #include "lyxfont.h"
15 #include "lyxrow.h"
16 #include "lyxrow_funcs.h"
17 #include "lyxrc.h"
18 #include "paragraph.h"
19
20 #include "insets/updatableinset.h"
21
22 using lyx::pos_type;
23
24
25 lyx::pos_type Bidi::log2vis(lyx::pos_type pos) const
26 {
27         return (start_ == -1) ? pos : log2vis_list_[pos - start_];
28 }
29
30
31 lyx::pos_type Bidi::vis2log(lyx::pos_type pos) const
32 {
33         return (start_ == -1) ? pos : vis2log_list_[pos - start_];
34 }
35
36
37 lyx::pos_type Bidi::level(lyx::pos_type pos) const
38 {
39         return (start_ == -1) ? 0 : levels_[pos - start_];
40 }
41
42
43 bool Bidi::inRange(lyx::pos_type pos) const
44 {
45         return start_ == -1 || (start_ <= pos && pos <= end_);
46 }
47
48 bool Bidi::same_direction() const
49 {
50         return same_direction_;
51 }
52
53
54 void Bidi::computeTables(Paragraph const & par,
55         Buffer const & buf, Row & row)
56 {
57         same_direction_ = true;
58         if (!lyxrc.rtl_support) {
59                 start_ = -1;
60                 return;
61         }
62
63         InsetOld * inset = par.inInset();
64         if (inset && inset->owner() &&
65             inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
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.isRightToLeftPar(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 = par.isLineSeparator(lpos);
101                 pos_type const pos =
102                         (is_space && lpos + 1 <= end_ &&
103                          !par.isLineSeparator(lpos + 1) &&
104                          !par.isNewline(lpos + 1))
105                         ? lpos + 1 : lpos;
106                 LyXFont font = par.getFontSettings(bufparams, pos);
107                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
108                     font.number() == LyXFont::ON &&
109                     par.getFontSettings(bufparams, lpos - 1).number()
110                     == LyXFont::ON) {
111                         font = par.getFontSettings(bufparams, lpos);
112                         is_space = false;
113                 }
114
115                 bool new_rtl = font.isVisibleRightToLeft();
116                 bool new_rtl0 = font.isRightToLeft();
117                 int new_level;
118
119                 if (lpos == body_pos - 1
120                     && row.pos() < body_pos - 1
121                     && is_space) {
122                         new_level = rtl_par ? 1 : 0;
123                         new_rtl0 = rtl_par;
124                         new_rtl = rtl_par;
125                 } else if (new_rtl0)
126                         new_level = new_rtl ? 1 : 2;
127                 else
128                         new_level = rtl_par ? 2 : 0;
129
130                 if (is_space && new_level >= lev) {
131                         new_level = lev;
132                         new_rtl = rtl;
133                         new_rtl0 = rtl0;
134                 }
135
136                 int new_level2 = new_level;
137
138                 if (lev == new_level && rtl0 != new_rtl0) {
139                         --new_level2;
140                         log2vis_list_[lpos - start_] = rtl ? 1 : -1;
141                 } else if (lev < new_level) {
142                         log2vis_list_[lpos - start_] = rtl ? -1 : 1;
143                         if (new_level > rtl_par)
144                                 same_direction_ = false;
145                 } else
146                         log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
147                 rtl = new_rtl;
148                 rtl0 = new_rtl0;
149                 levels_[lpos - start_] = new_level;
150
151                 while (lev > new_level2) {
152                         pos_type old_lpos = stack[--lev];
153                         int delta = lpos - old_lpos - 1;
154                         if (lev % 2)
155                                 delta = -delta;
156                         log2vis_list_[lpos - start_] += delta;
157                         log2vis_list_[old_lpos - start_] += delta;
158                 }
159                 while (lev < new_level)
160                         stack[lev++] = lpos;
161         }
162
163         while (lev > 0) {
164                 pos_type const old_lpos = stack[--lev];
165                 int delta = end_ - old_lpos;
166                 if (lev % 2)
167                         delta = -delta;
168                 log2vis_list_[old_lpos - start_] += delta;
169         }
170
171         pos_type vpos = start_ - 1;
172         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
173                 vpos += log2vis_list_[lpos - start_];
174                 vis2log_list_[vpos - start_] = lpos;
175                 log2vis_list_[lpos - start_] = vpos;
176         }
177 }
178
179
180 // This method requires a previous call to computeTables()
181 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
182         pos_type pos) const
183 {
184         if (!lyxrc.rtl_support || pos == 0)
185                 return false;
186
187         if (!inRange(pos - 1)) {
188                 // This can happen if pos is the first char of a row.
189                 // Returning false in this case is incorrect!
190                 return false;
191         }
192
193         bool const rtl = level(pos - 1) % 2;
194         bool const rtl2 = inRange(pos)
195                 ? level(pos) % 2
196                 : par.isRightToLeftPar(buf.params());
197         return rtl != rtl2;
198 }
199
200
201 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
202         pos_type pos, LyXFont const & font) const
203 {
204         if (!lyxrc.rtl_support)
205                 return false;    // This is just for speedup
206
207         bool const rtl = font.isVisibleRightToLeft();
208         bool const rtl2 = inRange(pos)
209                 ? level(pos) % 2
210                 : par.isRightToLeftPar(buf.params());
211         return rtl != rtl2;
212 }