]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontMetrics.cpp
Use the command-alternatives sequence as defined in menus.bind
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
index 31c45481ae6af56eac56b86e7b46ce1d89eb1d15..b9eab8531fec62276a33fccdd04ab3437733fb1a 100644 (file)
 #include "qt_helpers.h"
 
 #include "Dimension.h"
-#include "Language.h"
-#include "LyXRC.h"
-
-#include "insets/Inset.h"
 
 #include "support/convert.h"
 #include "support/lassert.h"
+#include "support/lyxlib.h"
 
 #define DISABLE_PMPROF
 #include "support/pmprof.h"
@@ -45,16 +42,24 @@ uint qHash(lyx::docstring const & s)
                                s.size() * sizeof(lyx::docstring::value_type)));
 }
 
-}
+} // namespace std
 
 namespace lyx {
 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;
@@ -80,15 +85,9 @@ inline QChar const ucs4_to_qchar(char_type const ucs4)
        LATTEST(is_utf16(ucs4));
        return QChar(static_cast<unsigned short>(ucs4));
 }
-} // anon namespace
+} // 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),
@@ -201,7 +200,7 @@ int GuiFontMetrics::width(docstring const & s) const
                tl.beginLayout();
                QTextLine line = tl.createLine();
                tl.endLayout();
-               w = int(line.naturalTextWidth());
+               w = iround(line.horizontalAdvance());
        }
        strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
        return w;
@@ -271,9 +270,34 @@ int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
                           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).
@@ -363,7 +387,8 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
        line.setLineWidth(x);
        tl.createLine();
        tl.endLayout();
-       if ((force && line.textLength() == offset) || int(line.naturalTextWidth()) > x)
+       int const line_wid = iround(line.horizontalAdvance());
+       if ((force && line.textLength() == offset) || line_wid > x)
                return {-1, -1};
        /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
         * not be the same when there are high-plan unicode characters
@@ -388,8 +413,7 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
                --len;
        LASSERT(len > 0 || qlen == 0, /**/);
 #endif
-       // The -1 is here to account for the leading zerow_nbsp.
-       return {len, int(line.naturalTextWidth())};
+       return {len, line_wid};
 }
 
 
@@ -421,19 +445,20 @@ bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const
 void GuiFontMetrics::rectText(docstring const & str,
        int & w, int & ascent, int & descent) const
 {
-       static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
+       // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
+       int const offset = 4;
 
-       w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
-       ascent = metrics_.ascent() + d;
-       descent = metrics_.descent() + d;
+       w = width(str) + offset;
+       ascent = metrics_.ascent() + offset / 2;
+       descent = metrics_.descent() + offset / 2;
 }
 
 
-void GuiFontMetrics::buttonText(docstring const & str,
+void GuiFontMetrics::buttonText(docstring const & str, const int offset,
        int & w, int & ascent, int & descent) const
 {
        rectText(str, w, ascent, descent);
-       w += Inset::TEXT_TO_INSET_OFFSET;
+       w += offset;
 }