From da590925cd6749587b9f500ecdaa457e34bc0f4a Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Fri, 31 Mar 2017 16:56:06 +0200 Subject: [PATCH] Work around bug in QTextLine::xToCursor With RtL text, the method can be non-monotonic wrt x value (which is a Qt bug). To work around this, we check whether positions adjacent to those returned by xToCursor look better. Depending on whether the new x position is too small or too large, we look backward or forward for a better solution. The performance is probably not great, but this is only needed for user interactions, so the performance penalty should not be a problem. Fixes #10569. --- src/frontends/qt4/GuiFontMetrics.cpp | 29 ++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/frontends/qt4/GuiFontMetrics.cpp b/src/frontends/qt4/GuiFontMetrics.cpp index 33df3cf126..585933a328 100644 --- a/src/frontends/qt4/GuiFontMetrics.cpp +++ b/src/frontends/qt4/GuiFontMetrics.cpp @@ -272,9 +272,34 @@ int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl, double const wordspacing) const { shared_ptr tl = getTextLayout(s, rtl, wordspacing); - int const qpos = tl->lineForTextPosition(0).xToCursor(x); + QTextLine const & tline = tl->lineForTextPosition(0); + int qpos = tline.xToCursor(x); + int newx = static_cast(tline.cursorToX(qpos)); + // The value of qpos may be wrong in rtl text (see ticket #10569). + // To work around this, let's have a look at adjacent positions to + // see whether we find closer matches. + if (rtl && newx < x) { + while (qpos > 0) { + int const xm = static_cast(tline.cursorToX(qpos - 1)); + if (abs(xm - x) < abs(newx - x)) { + --qpos; + newx = xm; + } else + break; + } + } else if (rtl && newx > x) { + while (qpos < tline.textLength()) { + int const xp = static_cast(tline.cursorToX(qpos + 1)); + if (abs(xp - x) < abs(newx - x)) { + ++qpos; + newx = xp; + } else + break; + } + } // correct x value to the actual cursor position. - x = static_cast(tl->lineForTextPosition(0).cursorToX(qpos)); + x = newx; + /* Since QString is UTF-16 and docstring is UCS-4, the offsets may * not be the same when there are high-plan unicode characters * (bug #10443). -- 2.39.2