Fix issue with mispositioned family name i QFontComboBox
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
Tue, 2 Oct 2012 07:09:26 +0000 (09:09 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 3 Oct 2012 15:17:29 +0000 (17:17 +0200)
Mac OS X 10.7 comes with the family of Stix fonts, some
of which exposed an ugly layout bug in the QFontComboBox
because the ascent/descent ratio is very large due to
a very high ascent, so centering the text vertically might
cause most of the text to be clipped away.

The solution is to detect when the ascent is larger than the
height of the destination rectangle (hence a large part of the
characters will be clipped) and use the actual bounding rect
for centralizing instead. Since this only happens for a
very few of the fonts, the overhead of getting the bounding
rect should be tolerable.

This is a port of 4679c6901fc7c388fdf6c022d3499708222ef1f1 from
Qt 4.8.

Task-number: QTBUG-26691

Change-Id: Ia2014775e5baf0568df3290f2dc4ad64fb5a74bd
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
src/widgets/widgets/qfontcombobox.cpp

index 806f72e..ab878cc 100644 (file)
@@ -164,7 +164,18 @@ void QFontFamilyDelegate::paint(QPainter *painter,
 
     QFont old = painter->font();
     painter->setFont(font);
-    painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, text);
+
+    // If the ascent of the font is larger than the height of the rect,
+    // we will clip the text, so it's better to align the tight bounding rect in this case
+    // This is specifically for fonts where the ascent is very large compared to
+    // the descent, like certain of the Stix family.
+    QFontMetricsF fontMetrics(font);
+    if (fontMetrics.ascent() > r.height()) {
+        QRectF tbr = fontMetrics.tightBoundingRect(text);
+        painter->drawText(r.x(), r.y() + (r.height() + tbr.height()) / 2.0, text);
+    } else {
+        painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeading|Qt::TextSingleLine, text);
+    }
 
     if (writingSystem != QFontDatabase::Any)
         system = writingSystem;