[LyX/2.3.x] Try to use the right width for math symbols

Enrico Forestieri forenr at lyx.org
Tue Sep 8 22:59:52 UTC 2020


commit 34f33b27a36ec3e7298546588768747802f99fcc
Author: Enrico Forestieri <forenr at lyx.org>
Date:   Wed Sep 9 01:25:12 2020 +0200

    Try to use the right width for math symbols
    
    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.
    
    No status entry is needed as this amends 79998fdc.
---
 src/frontends/qt4/GuiFontMetrics.cpp |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/frontends/qt4/GuiFontMetrics.cpp b/src/frontends/qt4/GuiFontMetrics.cpp
index 9ad6c17..33fcf30 100644
--- a/src/frontends/qt4/GuiFontMetrics.cpp
+++ b/src/frontends/qt4/GuiFontMetrics.cpp
@@ -212,8 +212,8 @@ int GuiFontMetrics::width(docstring const & s) const
 	/* 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::boundingRect().width()
-	 * with our math fonts.
+	 * safety measure, with our math fonts we always use the maximum
+	 * between QMetrics::boundingRect().width() and QMetrics::width().
 	*/
 	int w = 0;
 	if (s.length() == 1
@@ -221,9 +221,16 @@ int GuiFontMetrics::width(docstring const & s) const
 	    && font_.styleName() == "LyX"
 #endif
 	    ) {
+		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 null width
-		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));


More information about the lyx-cvs mailing list