-
- pos_type cursor_vpos = 0;
-
- Buffer const & buffer = bv_->buffer();
- double x = row.x;
- Bidi bidi;
- bidi.computeTables(par, buffer, row);
-
- pos_type const row_pos = row.pos();
- pos_type const end = row.endpos();
- // Spaces at logical line breaks in bidi text must be skipped during
- // cursor positioning. However, they may appear visually in the middle
- // of a row; they must be skipped, wherever they are...
- // * logically "abc_[HEBREW_\nHEBREW]"
- // * visually "abc_[_WERBEH\nWERBEH]"
- pos_type skipped_sep_vpos = -1;
-
- if (end <= row_pos)
- cursor_vpos = row_pos;
- else if (ppos >= end)
- cursor_vpos = text_->isRTL(par) ? row_pos : end;
- else if (ppos > row_pos && ppos >= end)
- //FIXME: this code is never reached!
- // (see http://www.lyx.org/trac/changeset/8251)
- // Place cursor after char at (logical) position pos - 1
- cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
- ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
- else
- // Place cursor before char at (logical) position ppos
- cursor_vpos = (bidi.level(ppos) % 2 == 0)
- ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
-
- pos_type body_pos = par.beginOfBody();
- if (body_pos > 0 &&
- (body_pos > end || !par.isLineSeparator(body_pos - 1)))
- body_pos = 0;
-
- // check for possible inline completion in this row
- DocIterator const & inlineCompletionPos = bv_->inlineCompletionPos();
- pos_type inlineCompletionVPos = -1;
- if (inlineCompletionPos.inTexted()
- && inlineCompletionPos.text() == text_
- && inlineCompletionPos.pit() == pit
- && inlineCompletionPos.pos() - 1 >= row_pos
- && inlineCompletionPos.pos() - 1 < end) {
- // draw logically behind the previous character
- inlineCompletionVPos = bidi.log2vis(inlineCompletionPos.pos() - 1);
- }
-
- // Use font span to speed things up, see below
- FontSpan font_span;
- Font font;
-
- // If the last logical character is a separator, skip it, unless
- // it's in the last row of a paragraph; see skipped_sep_vpos declaration
- if (end > 0 && end < par.size() && par.isSeparator(end - 1))
- skipped_sep_vpos = bidi.log2vis(end - 1);
-
- if (lyxrc.paragraph_markers && text_->isRTL(par)) {
- ParagraphList const & pars_ = text_->paragraphs();
- if (size_type(pit + 1) < pars_.size()) {
- FontInfo f;
- docstring const s = docstring(1, char_type(0x00B6));
- x += theFontMetrics(f).width(s);
- }
- }
-
- // Inline completion RTL special case row_pos == cursor_pos:
- // "__|b" => cursor_pos is right of __
- if (row_pos == inlineCompletionVPos && row_pos == cursor_vpos) {
- font = displayFont(pit, row_pos + 1);
- docstring const & completion = bv_->inlineCompletion();
- if (font.isRightToLeft() && completion.length() > 0)
- x += theFontMetrics(font.fontInfo()).width(completion);
- }
-
- for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
- // Skip the separator which is at the logical end of the row
- if (vpos == skipped_sep_vpos)
- continue;
- pos_type pos = bidi.vis2log(vpos);
- if (body_pos > 0 && pos == body_pos - 1) {
- FontMetrics const & labelfm = theFontMetrics(
- text_->labelFont(par));
- x += row.label_hfill + labelfm.width(par.layout().labelsep);
- if (par.isLineSeparator(body_pos - 1))
- x -= singleWidth(pit, body_pos - 1);
- }
-
- // Use font span to speed things up, see above
- if (pos < font_span.first || pos > font_span.last) {
- font_span = par.fontSpan(pos);
- font = displayFont(pit, pos);
- }
-
- x += pm.singleWidth(pos, font);
-
- // Inline completion RTL case:
- // "a__|b", __ of b => non-boundary a-pos is right of __
- if (vpos + 1 == inlineCompletionVPos
- && (vpos + 1 < cursor_vpos || !boundary_correction)) {
- font = displayFont(pit, vpos + 1);
- docstring const & completion = bv_->inlineCompletion();
- if (font.isRightToLeft() && completion.length() > 0)
- x += theFontMetrics(font.fontInfo()).width(completion);
- }
-
- // Inline completion LTR case:
- // "b|__a", __ of b => non-boundary a-pos is in front of __
- if (vpos == inlineCompletionVPos
- && (vpos + 1 < cursor_vpos || boundary_correction)) {
- font = displayFont(pit, vpos);
- docstring const & completion = bv_->inlineCompletion();
- if (!font.isRightToLeft() && completion.length() > 0)
- x += theFontMetrics(font.fontInfo()).width(completion);
+ pos_type const pos = sl.pos();
+
+ /**
+ * When boundary is true, position i is in the row element (pos, endpos)
+ * if
+ * pos < i <= endpos
+ * whereas, when boundary is false, the test is
+ * pos <= i < endpos
+ * The correction below allows to handle both cases.
+ */
+ int const boundary_corr = (boundary && pos) ? -1 : 0;
+
+ /** Early return in trivial cases
+ * 1) the row is empty
+ * 2) the position is the left-most position of the row; there
+ * is a quirck herehowever: if the first element is virtual
+ * (end-of-par marker for example), then we have to look
+ * closer
+ */
+ if (row.empty()
+ || (pos == row.begin()->left_pos()
+ && pos != row.begin()->right_pos()))
+ return row.left_margin;
+
+ Row::const_iterator cit = row.begin();
+ double x = row.left_margin;
+ for ( ; cit != row.end() ; ++cit) {
+ /** Look whether the cursor is inside the element's
+ * span. Note that it is necessary to take the
+ * boundary in account, and to accept virtual
+ * elements, which have pos == endpos.
+ */
+ if (pos + boundary_corr >= cit->pos
+ && (pos + boundary_corr < cit->endpos
+ || cit->pos == cit->endpos)) {
+ x += cit->pos2x(pos);
+ break;