3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
16 #include "lyxrow_funcs.h"
18 #include "paragraph.h"
20 #include "insets/updatableinset.h"
25 lyx::pos_type Bidi::log2vis(lyx::pos_type pos) const
27 return (start_ == -1) ? pos : log2vis_list_[pos - start_];
31 lyx::pos_type Bidi::vis2log(lyx::pos_type pos) const
33 return (start_ == -1) ? pos : vis2log_list_[pos - start_];
37 lyx::pos_type Bidi::level(lyx::pos_type pos) const
39 return (start_ == -1) ? 0 : levels_[pos - start_];
43 bool Bidi::inRange(lyx::pos_type pos) const
45 return start_ == -1 || (start_ <= pos && pos <= end_);
48 bool Bidi::same_direction() const
50 return same_direction_;
54 void Bidi::computeTables(Paragraph const & par,
55 Buffer const & buf, Row const & row)
57 same_direction_ = true;
58 if (!lyxrc.rtl_support) {
63 InsetOld * inset = par.inInset();
64 if (inset && inset->lyxCode() == InsetOld::ERT_CODE) {
70 end_ = row.endpos() - 1;
77 if (end_ + 2 - start_ >
78 static_cast<pos_type>(log2vis_list_.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();
92 bool const rtl_par = par.isRightToLeftPar(bufparams);
96 pos_type const body_pos = par.beginOfBody();
98 for (pos_type lpos = start_; lpos <= end_; ++lpos) {
99 bool is_space = par.isLineSeparator(lpos);
101 (is_space && lpos + 1 <= end_ &&
102 !par.isLineSeparator(lpos + 1) &&
103 !par.isNewline(lpos + 1))
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()
110 font = par.getFontSettings(bufparams, lpos);
114 bool new_rtl = font.isVisibleRightToLeft();
115 bool new_rtl0 = font.isRightToLeft();
118 if (lpos == body_pos - 1
119 && row.pos() < body_pos - 1
121 new_level = rtl_par ? 1 : 0;
125 new_level = new_rtl ? 1 : 2;
127 new_level = rtl_par ? 2 : 0;
129 if (is_space && new_level >= lev) {
135 int new_level2 = new_level;
137 if (lev == new_level && rtl0 != new_rtl0) {
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;
145 log2vis_list_[lpos - start_] = new_rtl ? -1 : 1;
148 levels_[lpos - start_] = new_level;
150 while (lev > new_level2) {
151 pos_type old_lpos = stack[--lev];
152 int delta = lpos - old_lpos - 1;
155 log2vis_list_[lpos - start_] += delta;
156 log2vis_list_[old_lpos - start_] += delta;
158 while (lev < new_level)
163 pos_type const old_lpos = stack[--lev];
164 int delta = end_ - old_lpos;
167 log2vis_list_[old_lpos - start_] += delta;
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;
179 // This method requires a previous call to computeTables()
180 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
183 if (!lyxrc.rtl_support || pos == 0)
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!
192 bool const rtl = level(pos - 1) % 2;
193 bool const rtl2 = inRange(pos)
195 : par.isRightToLeftPar(buf.params());
200 bool Bidi::isBoundary(Buffer const & buf, Paragraph const & par,
201 pos_type pos, LyXFont const & font) const
203 if (!lyxrc.rtl_support)
204 return false; // This is just for speedup
206 bool const rtl = font.isVisibleRightToLeft();
207 bool const rtl2 = inRange(pos)
209 : par.isRightToLeftPar(buf.params());