commit 81465da5d599e071efebf067bb9e8ed252ac1456
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Sat Jan 21 00:30:20 2017 +0100

    Fix metrics of math characters with 0 width
    
    It seems that QTextLayout does not handle properly a single character
    with 0 width. This breaks drawing of things like \not.
    
    No status line needed as this amends 24648404.
---
 src/frontends/qt4/GuiFontMetrics.cpp |   30 +++++++++++++++++++++---------
 1 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/frontends/qt4/GuiFontMetrics.cpp 
b/src/frontends/qt4/GuiFontMetrics.cpp
index f17ac37..5bb99aa 100644
--- a/src/frontends/qt4/GuiFontMetrics.cpp
+++ b/src/frontends/qt4/GuiFontMetrics.cpp
@@ -178,16 +178,28 @@ int GuiFontMetrics::width(docstring const & s) const
        int * pw = strwidth_cache_[s];
        if (pw)
                return *pw;
-       // For some reason QMetrics::width returns a wrong value with Qt5
-       // int w = metrics_.width(toqstr(s));
 #endif
-       QTextLayout tl;
-       tl.setText(toqstr(s));
-       tl.setFont(font_);
-       tl.beginLayout();
-       QTextLine line = tl.createLine();
-       tl.endLayout();
-       int w = int(line.naturalTextWidth());
+       /* 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.
+       */
+       int w = 0;
+       if (s.length() == 1
+#if QT_VERSION >= 0x040800
+           || font_.styleName() == "LyX"
+#endif
+           )
+               w = metrics_.width(toqstr(s));
+       else {
+               QTextLayout tl;
+               tl.setText(toqstr(s));
+               tl.setFont(font_);
+               tl.beginLayout();
+               QTextLine line = tl.createLine();
+               tl.endLayout();
+               w = int(line.naturalTextWidth());
+       }
 #ifdef CACHE_METRICS_WIDTH
        strwidth_cache_.insert(s, new int(w), s.size() * sizeof(char_type));
 #endif

Reply via email to