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