- LASSERT(sl.text() == text_, return 0);
- pit_type const pit = sl.pit();
- Paragraph const & par = text_->paragraphs()[pit];
- ParagraphMetrics const & pm = par_metrics_[pit];
- if (pm.rows().empty())
- return 0;
-
- pos_type ppos = sl.pos();
- // Correct position in front of big insets
- bool const boundary_correction = ppos != 0 && boundary;
- if (boundary_correction)
- --ppos;
-
- Row const & row = pm.getRow(sl.pos(), boundary);
-
- 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);
+ /**
+ * 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;
+
+ x = row.left_margin;
+
+ /** Early return in trivial cases
+ * 1) the row is empty
+ * 2) the position is the left-most position of the row; there
+ * is a quirk here however: 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() && !boundary
+ && !row.begin()->isVirtual()))
+ return row.begin();
+
+ Row::const_iterator cit = row.begin();
+ for ( ; cit != row.end() ; ++cit) {
+ /** Look whether the cursor is inside the element's
+ * span. Note that it is necessary to take the
+ * boundary into account, and to accept virtual
+ * elements, which have pos == endpos.
+ */
+ if (pos + boundary_corr >= cit->pos
+ && (pos + boundary_corr < cit->endpos || cit->isVirtual())) {
+ x += cit->pos2x(pos);
+ break;