* de.po: sync with branch.
[lyx.git] / src / Bidi.cpp
blobb9d1222931d83f0459b0fc527fb90bf1270e7a6f
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.
6 * \author Dekel Tsur
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
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"
23 namespace lyx {
26 pos_type Bidi::log2vis(pos_type pos) const
28 return (start_ == -1) ? pos : log2vis_list_[pos - start_];
32 pos_type Bidi::vis2log(pos_type pos) const
34 return (start_ == -1) ? pos : vis2log_list_[pos - start_];
38 pos_type Bidi::level(pos_type pos) const
40 return (start_ == -1) ? 0 : levels_[pos - start_];
44 bool Bidi::inRange(pos_type pos) const
46 return start_ == -1 || (start_ <= pos && pos <= end_);
49 bool Bidi::same_direction() const
51 return same_direction_;
55 void Bidi::computeTables(Paragraph const & par,
56 Buffer const & buf, Row const & row)
58 same_direction_ = true;
59 if (!lyxrc.rtl_support) {
60 start_ = -1;
61 return;
64 if (par.inInset().forceLTR()) {
65 start_ = -1;
66 return;
69 start_ = row.pos();
70 end_ = row.endpos() - 1;
72 if (start_ > end_) {
73 start_ = -1;
74 return;
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);
87 vis2log_list_[end_ + 1 - start_] = -1;
88 log2vis_list_[end_ + 1 - start_] = -1;
90 BufferParams const & bufparams = buf.params();
91 pos_type stack[2];
92 bool const rtl_par = par.isRTL(bufparams);
93 int lev = 0;
94 bool rtl = false;
95 bool rtl0 = false;
96 pos_type const body_pos = par.beginOfBody();
98 for (pos_type lpos = start_; lpos <= end_; ++lpos) {
99 bool is_space = false;
100 // We do not handle spaces around an RTL segment in a special way anymore.
101 // Neither do we do so when generating the LaTeX, so setting is_space
102 // to false makes the view in the GUI consistent with the output of LaTeX
103 // later. The old setting was:
104 //bool is_space = par.isLineSeparator(lpos);
105 // FIXME: once we're sure that this is what we really want, we should just
106 // get rid of this variable...
107 pos_type const pos =
108 (is_space && lpos + 1 <= end_ &&
109 !par.isLineSeparator(lpos + 1) &&
110 !par.isNewline(lpos + 1))
111 ? lpos + 1 : lpos;
113 Font const * font = &(par.getFontSettings(bufparams, pos));
114 if (pos != lpos && 0 < lpos && rtl0 && font->isRightToLeft() &&
115 font->fontInfo().number() == FONT_ON &&
116 par.getFontSettings(bufparams, lpos - 1).fontInfo().number()
117 == FONT_ON) {
118 font = &(par.getFontSettings(bufparams, lpos));
119 is_space = false;
121 bool new_rtl = font->isVisibleRightToLeft();
122 bool new_rtl0 = font->isRightToLeft();
124 int new_level;
126 if (lpos == body_pos - 1
127 && row.pos() < body_pos - 1
128 && is_space) {
129 new_level = rtl_par ? 1 : 0;
130 new_rtl0 = rtl_par;
131 new_rtl = rtl_par;
132 } else if (new_rtl0) {
133 new_level = new_rtl ? 1 : 2;
134 } else {
135 new_level = rtl_par ? 2 : 0;
138 if (is_space && new_level >= lev) {
139 new_level = lev;
140 new_rtl = rtl;
141 new_rtl0 = rtl0;
144 int new_level2 = new_level;
146 if (lev == new_level && rtl0 != new_rtl0) {
147 --new_level2;
148 log2vis_list_[lpos - start_] = rtl ? 1 : -1;
149 } else if (lev < new_level) {
150 log2vis_list_[lpos - start_] = rtl ? -1 : 1;
151 if (new_level > 0 && !rtl_par)
152 same_direction_ = false;
153 } else {
154 log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
156 rtl = new_rtl;
157 rtl0 = new_rtl0;
158 levels_[lpos - start_] = new_level;
160 while (lev > new_level2) {
161 pos_type old_lpos = stack[--lev];
162 int delta = lpos - old_lpos - 1;
163 if (lev % 2)
164 delta = -delta;
165 log2vis_list_[lpos - start_] += delta;
166 log2vis_list_[old_lpos - start_] += delta;
168 while (lev < new_level)
169 stack[lev++] = lpos;
172 while (lev > 0) {
173 pos_type const old_lpos = stack[--lev];
174 int delta = end_ - old_lpos;
175 if (lev % 2)
176 delta = -delta;
177 log2vis_list_[old_lpos - start_] += delta;
180 pos_type vpos = start_ - 1;
181 for (pos_type lpos = start_; lpos <= end_; ++lpos) {
182 vpos += log2vis_list_[lpos - start_];
183 vis2log_list_[vpos - start_] = lpos;
184 log2vis_list_[lpos - start_] = vpos;
189 // This method requires a previous call to computeTables()
190 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
191 pos_type pos) const
193 if (!lyxrc.rtl_support || pos == 0)
194 return false;
196 if (!inRange(pos - 1)) {
197 // This can happen if pos is the first char of a row.
198 // Returning false in this case is incorrect!
199 return false;
202 bool const rtl = level(pos - 1) % 2;
203 bool const rtl2 = inRange(pos)
204 ? level(pos) % 2
205 : par.isRTL(buf.params());
206 return rtl != rtl2;
210 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
211 pos_type pos, Font const & font) const
213 if (!lyxrc.rtl_support)
214 return false; // This is just for speedup
216 bool const rtl = font.isVisibleRightToLeft();
217 bool const rtl2 = inRange(pos)
218 ? level(pos) % 2
219 : par.isRTL(buf.params());
220 return rtl != rtl2;
224 bool reverseDirectionNeeded(Cursor const & cur)
227 * We determine the directions based on the direction of the
228 * bottom() --- i.e., outermost --- paragraph, because that is
229 * the only way to achieve consistency of the arrow's movements
230 * within a paragraph, and thus avoid situations in which the
231 * cursor gets stuck.
233 return cur.bottom().paragraph().isRTL(cur.bv().buffer().params());
237 bool isWithinRtlParagraph(Cursor const & cur)
239 return cur.innerParagraph().isRTL(cur.bv().buffer().params());
242 } // namespace lyx