]> git.lyx.org Git - features.git/commitdiff
Try to use the right width for math symbols
authorEnrico Forestieri <forenr@lyx.org>
Tue, 8 Sep 2020 20:30:02 +0000 (22:30 +0200)
committerEnrico Forestieri <forenr@lyx.org>
Tue, 8 Sep 2020 20:30:02 +0000 (22:30 +0200)
The rules for typesetting math differ from the rules for typesetting
text. For example, two italic 'f' chars have to be typeset more closely
than two 'o' chars in text mode, but not in math mode. Qt provides a
method that returns the distance appropriate for drawing a subsequent
character in text mode, but nothing for math mode. Typically, the
distance appropriate for drawing the next character in math mode is
the actual width span by the character, corrected by the rules of
an appendix in the TeXbook. Recently, those rules are followed more
closely in LyX but not exactly, and we have to find a way to adapt to them.
Some symbols may need more spacing around them than the width they span.
So, we use the distance suggested by Qt, unless it is less than the
width of the rectangle bounding the symbol. Before Qt 5.11 the used method
was QFontMetrics::width(), but since then it has been declared obsolete
in favor of QFontMetrics::horizontalAdvance(), whose name conveys better
its meaning.

src/frontends/qt/GuiFontMetrics.cpp

index 361125056ee82eac4f2a504d7719975ae9074f87..4a09d97dcf894c6d52d2850a05cc76cdc9c5e5c1 100644 (file)
@@ -246,9 +246,16 @@ int GuiFontMetrics::width(docstring const & s) const
        bool const math_char = s.length() == 1;
 #endif
        if (math_char) {
+               QString const qs = toqstr(s);
+               int br_width = metrics_.boundingRect(qs).width();
+#if QT_VERSION >= 0x050b00
+               int s_width = metrics_.horizontalAdvance(qs);
+#else
+               int s_width = metrics_.width(qs);
+#endif
                // keep value 0 for math chars with width 0
-               if (metrics_.width(toqstr(s)) != 0)
-                       w = metrics_.boundingRect(toqstr(s)).width();
+               if (s_width != 0)
+                       w = max(br_width, s_width);
        } else {
                QTextLayout tl;
                tl.setText(toqstr(s));