]> git.lyx.org Git - lyx.git/blob - src/Bidi.cpp
Split pdf format as discussed on the list
[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.isNewline(lpos + 1))
112                         ? lpos + 1 : lpos;
113
114                 Font const * font = &(par.getFontSettings(bufparams, pos));
115                 if (pos != lpos && 0 < lpos && rtl0 && font->isRightToLeft() &&
116                     font->fontInfo().number() == FONT_ON &&
117                     par.getFontSettings(bufparams, lpos - 1).fontInfo().number()
118                     == FONT_ON) {
119                         font = &(par.getFontSettings(bufparams, lpos));
120                         is_space = false;
121                 }
122                 bool new_rtl = font->isVisibleRightToLeft();
123                 bool new_rtl0 = font->isRightToLeft();
124
125                 int new_level;
126
127                 if (lpos == body_pos - 1
128                     && row.pos() < body_pos - 1
129                     && is_space) {
130                         new_level = rtl_par ? 1 : 0;
131                         new_rtl0 = rtl_par;
132                         new_rtl = rtl_par;
133                 } else if (new_rtl0) {
134                         new_level = new_rtl ? 1 : 2;
135                 } else {
136                         new_level = rtl_par ? 2 : 0;
137                 }
138
139                 if (is_space && new_level >= lev) {
140                         new_level = lev;
141                         new_rtl = rtl;
142                         new_rtl0 = rtl0;
143                 }
144
145                 int new_level2 = new_level;
146
147                 if (lev == new_level && rtl0 != new_rtl0) {
148                         --new_level2;
149                         log2vis_list_[lpos - start_] = rtl ? 1 : -1;
150                 } else if (lev < new_level) {
151                         log2vis_list_[lpos - start_] = rtl ? -1 : 1;
152                         if (new_level > 0 && !rtl_par)
153                                 same_direction_ = false;
154                 } else {
155                         log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
156                 }
157                 rtl = new_rtl;
158                 rtl0 = new_rtl0;
159                 levels_[lpos - start_] = new_level;
160
161                 while (lev > new_level2) {
162                         pos_type old_lpos = stack[--lev];
163                         int delta = lpos - old_lpos - 1;
164                         if (lev % 2)
165                                 delta = -delta;
166                         log2vis_list_[lpos - start_] += delta;
167                         log2vis_list_[old_lpos - start_] += delta;
168                 }
169                 while (lev < new_level)
170                         stack[lev++] = lpos;
171         }
172
173         while (lev > 0) {
174                 pos_type const old_lpos = stack[--lev];
175                 int delta = end_ - old_lpos;
176                 if (lev % 2)
177                         delta = -delta;
178                 log2vis_list_[old_lpos - start_] += delta;
179         }
180
181         pos_type vpos = start_ - 1;
182         for (pos_type lpos = start_; lpos <= end_; ++lpos) {
183                 vpos += log2vis_list_[lpos - start_];
184                 vis2log_list_[vpos - start_] = lpos;
185                 log2vis_list_[lpos - start_] = vpos;
186         }
187 }
188
189
190 // This method requires a previous call to computeTables()
191 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
192         pos_type pos) const
193 {
194         if (!lyxrc.rtl_support || pos == 0)
195                 return false;
196
197         if (!inRange(pos - 1)) {
198                 // This can happen if pos is the first char of a row.
199                 // Returning false in this case is incorrect!
200                 return false;
201         }
202
203         bool const rtl = level(pos - 1) % 2;
204         bool const rtl2 = inRange(pos)
205                 ? level(pos) % 2
206                 : par.isRTL(buf.params());
207         return rtl != rtl2;
208 }
209
210
211 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
212         pos_type pos, Font const & font) const
213 {
214         if (!lyxrc.rtl_support)
215                 return false;    // This is just for speedup
216
217         bool const rtl = font.isVisibleRightToLeft();
218         bool const rtl2 = inRange(pos)
219                 ? level(pos) % 2
220                 : par.isRTL(buf.params());
221         return rtl != rtl2;
222 }
223
224
225 bool reverseDirectionNeeded(Cursor const & cur)
226 {
227         /*
228          * We determine the directions based on the direction of the
229          * bottom() --- i.e., outermost --- paragraph, because that is
230          * the only way to achieve consistency of the arrow's movements
231          * within a paragraph, and thus avoid situations in which the
232          * cursor gets stuck.
233          */
234         return cur.bottom().paragraph().isRTL(cur.bv().buffer().params());
235 }
236
237
238 bool isWithinRtlParagraph(Cursor const & cur)
239 {
240         return cur.innerParagraph().isRTL(cur.bv().buffer().params());
241 }
242
243 } // namespace lyx