]> git.lyx.org Git - lyx.git/blob - src/Bidi.C
Use 'assign' as the name for the operation that opens/closes branch
[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 const & 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->lyxCode() == InsetOld::ERT_CODE) {
65                 start_ = -1;
66                 return;
67         }
68
69         start_ = row.pos();
70         end_ = row.endpos() - 1;
71
72         if (start_ > end_) {
73                 start_ = -1;
74                 return;
75         }
76
77         if (end_ + 2 - start_ >
78             static_cast<pos_type>(log2vis_list_.size())) {
79                 pos_type new_size =
80                         (end_ + 2 - start_ < 500) ?
81                         500 : 2 * (end_ + 2 - start_);
82                 log2vis_list_.resize(new_size);
83                 vis2log_list_.resize(new_size);
84                 levels_.resize(new_size);
85         }
86
87         vis2log_list_[end_ + 1 - start_] = -1;
88         log2vis_list_[end_ + 1 - start_] = -1;
89
90         BufferParams const & bufparams = buf.params();
91         pos_type stack[2];
92         bool const rtl_par = par.isRightToLeftPar(bufparams);
93         int lev = 0;
94         bool rtl = false;
95         bool rtl0 = false;
96         pos_type const body_pos = par.beginOfBody();
97
98         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
99                 bool is_space = par.isLineSeparator(lpos);
100                 pos_type const pos =
101                         (is_space && lpos + 1 <= end_ &&
102                          !par.isLineSeparator(lpos + 1) &&
103                          !par.isNewline(lpos + 1))
104                         ? lpos + 1 : lpos;
105                 LyXFont font = par.getFontSettings(bufparams, pos);
106                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
107                     font.number() == LyXFont::ON &&
108                     par.getFontSettings(bufparams, lpos - 1).number()
109                     == LyXFont::ON) {
110                         font = par.getFontSettings(bufparams, lpos);
111                         is_space = false;
112                 }
113
114                 bool new_rtl = font.isVisibleRightToLeft();
115                 bool new_rtl0 = font.isRightToLeft();
116                 int new_level;
117
118                 if (lpos == body_pos - 1
119                     && row.pos() < body_pos - 1
120                     && is_space) {
121                         new_level = rtl_par ? 1 : 0;
122                         new_rtl0 = rtl_par;
123                         new_rtl = rtl_par;
124                 } else if (new_rtl0)
125                         new_level = new_rtl ? 1 : 2;
126                 else
127                         new_level = rtl_par ? 2 : 0;
128
129                 if (is_space && new_level >= lev) {
130                         new_level = lev;
131                         new_rtl = rtl;
132                         new_rtl0 = rtl0;
133                 }
134
135                 int new_level2 = new_level;
136
137                 if (lev == new_level && rtl0 != new_rtl0) {
138                         --new_level2;
139                         log2vis_list_[lpos - start_] = rtl ? 1 : -1;
140                 } else if (lev < new_level) {
141                         log2vis_list_[lpos - start_] = rtl ? -1 : 1;
142                         if (new_level > rtl_par)
143                                 same_direction_ = false;
144                 } else
145                         log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
146                 rtl = new_rtl;
147                 rtl0 = new_rtl0;
148                 levels_[lpos - start_] = new_level;
149
150                 while (lev > new_level2) {
151                         pos_type old_lpos = stack[--lev];
152                         int delta = lpos - old_lpos - 1;
153                         if (lev % 2)
154                                 delta = -delta;
155                         log2vis_list_[lpos - start_] += delta;
156                         log2vis_list_[old_lpos - start_] += delta;
157                 }
158                 while (lev < new_level)
159                         stack[lev++] = lpos;
160         }
161
162         while (lev > 0) {
163                 pos_type const old_lpos = stack[--lev];
164                 int delta = end_ - old_lpos;
165                 if (lev % 2)
166                         delta = -delta;
167                 log2vis_list_[old_lpos - start_] += delta;
168         }
169
170         pos_type vpos = start_ - 1;
171         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
172                 vpos += log2vis_list_[lpos - start_];
173                 vis2log_list_[vpos - start_] = lpos;
174                 log2vis_list_[lpos - start_] = vpos;
175         }
176 }
177
178
179 // This method requires a previous call to computeTables()
180 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
181         pos_type pos) const
182 {
183         if (!lyxrc.rtl_support || pos == 0)
184                 return false;
185
186         if (!inRange(pos - 1)) {
187                 // This can happen if pos is the first char of a row.
188                 // Returning false in this case is incorrect!
189                 return false;
190         }
191
192         bool const rtl = level(pos - 1) % 2;
193         bool const rtl2 = inRange(pos)
194                 ? level(pos) % 2
195                 : par.isRightToLeftPar(buf.params());
196         return rtl != rtl2;
197 }
198
199
200 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
201         pos_type pos, LyXFont const & font) const
202 {
203         if (!lyxrc.rtl_support)
204                 return false;    // This is just for speedup
205
206         bool const rtl = font.isVisibleRightToLeft();
207         bool const rtl2 = inRange(pos)
208                 ? level(pos) % 2
209                 : par.isRightToLeftPar(buf.params());
210         return rtl != rtl2;
211 }