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