]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontMetrics.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
index 1c00372b38cf89521c4cf3cea1e4c54063c2ee36..c2d4b884e4eddc8018c8824c593f77969fdbb980 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "support/convert.h"
 #include "support/lassert.h"
+#include "support/lyxlib.h"
 
 #define DISABLE_PMPROF
 #include "support/pmprof.h"
@@ -180,26 +181,38 @@ int GuiFontMetrics::width(docstring const & s) const
        if (strwidth_cache_.contains(s))
                return strwidth_cache_[s];
        PROFILE_CACHE_MISS(width);
-       /* For some reason QMetrics::width returns a wrong value with Qt5
-        * with some arabic text. OTOH, QTextLayout is broken for single
-        * characters with null width (like \not in mathed). Also, as a
-        * safety measure, always use QMetrics::width with our math fonts.
+       /* Several problems have to be taken into account:
+        * * QFontMetrics::width does not returns a wrong value with Qt5 with
+        *   some arabic text, since the glyph-shaping operations are not
+        *   done (documented in Qt5).
+        * * QTextLayout is broken for single characters with null width
+        *   (like \not in mathed).
+        * * While QTextLine::horizontalAdvance is the right thing to use
+     *   for text strings, it does not give a good result with some
+     *   characters like the \int (gyph 4) of esint.
+
+        * Also, as a safety measure, always use QFontMetrics::width with
+        * our math fonts.
        */
        int w = 0;
-       if (s.length() == 1
+       // is the string a single character from a math font ?
 #if QT_VERSION >= 0x040800
-           || font_.styleName() == "LyX"
+       bool const math_char = s.length() == 1 || font_.styleName() == "LyX";
+#else
+       bool const math_char = s.length() == 1;
 #endif
-           )
-               w = metrics_.width(toqstr(s));
-       else {
+       // keep value 0 for math chars with width 0
+       if (!math_char || metrics_.width(toqstr(s)) != 0) {
                QTextLayout tl;
                tl.setText(toqstr(s));
                tl.setFont(font_);
                tl.beginLayout();
                QTextLine line = tl.createLine();
                tl.endLayout();
-               w = int(line.naturalTextWidth());
+               if (math_char)
+                       w = iround(line.naturalTextWidth());
+               else
+                       w = iround(line.horizontalAdvance());
        }
        strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
        return w;
@@ -386,7 +399,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
@@ -411,8 +425,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};
 }