namespace frontend {
+/*
+ * Limit (strwidth|breakat)_cache_ size to 512kB of string data.
+ * Limit qtextlayout_cache_ size to 500 elements (we do not know the
+ * size of the QTextLayout objects anyway).
+ * Note that all these numbers are arbitrary.
+ * Also, setting size to 0 is tantamount to disabling the cache.
+ */
int cache_metrics_width_size = 1 << 19;
int cache_metrics_breakat_size = 1 << 19;
// Qt 5.x already has its own caching of QTextLayout objects
-#if (QT_VERSION < 0x050000)
+// but it does not seem to work well on MacOS X.
+#if (QT_VERSION < 0x050000) || defined(Q_OS_MAC)
int cache_metrics_qtextlayout_size = 500;
#else
int cache_metrics_qtextlayout_size = 0;
} // anon namespace
-/*
- * Limit (strwidth|breakat)_cache_ size to 512kB of string data.
- * Limit qtextlayout_cache_ size to 500 elements (we do not know the
- * size of the QTextLayout objects anyway).
- * Note that all these numbers are arbitrary.
- */
GuiFontMetrics::GuiFontMetrics(QFont const & font)
: font_(font), metrics_(font, 0),
strwidth_cache_(cache_metrics_width_size),
double const wordspacing) const
{
shared_ptr<QTextLayout const> 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<int>(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<int>(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<int>(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<int>(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).