Port default font resolve to SystemParametersInfoForDpi

We were getting font metrics for the primary screen and then adjusting
for the screen DPI get a screen-independent font size.

This could fail in edge cases where the screen DPI was changed after
app startup, but before the first window was shown. See QTBUG-105857.

Use SystemParametersInfoForDpi() to query for font info at 96 DPI instead,
which removes the need for tracking primary screen DPI.

LOGFONT_to_QFont() still has one usage (qwizard_win.cpp) which provides
a custom DPI for scaling, so we keep that function as-is.

Fixes: QTBUG-105857
Pick-to: 6.4 6.2 5.15
Change-Id: I1adf0ab3bf2c309e8fcb58093e86214fa11a2da8
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Morten Sørvig 2022-10-21 11:47:55 +02:00 committed by Morten Johan Sørvig
parent 18425c3329
commit edb00660e4
3 changed files with 5 additions and 16 deletions

View File

@ -566,16 +566,9 @@ void QWindowsFontDatabaseBase::createDirectWriteFactory(IDWriteFactory **factory
}
#endif // directwrite && direct2d
static int s_defaultVerticalDPI = 96; // Native Pixels
int QWindowsFontDatabaseBase::defaultVerticalDPI()
{
return s_defaultVerticalDPI;
}
void QWindowsFontDatabaseBase::setDefaultVerticalDPI(int d)
{
s_defaultVerticalDPI = d;
return 96;
}
LOGFONT QWindowsFontDatabaseBase::fontDefToLOGFONT(const QFontDef &request, const QString &faceName)
@ -690,9 +683,9 @@ HFONT QWindowsFontDatabaseBase::systemFont()
QFont QWindowsFontDatabaseBase::systemDefaultFont()
{
// Qt 6: Obtain default GUI font (typically "Segoe UI, 9pt", see QTBUG-58610)
NONCLIENTMETRICS ncm;
ncm.cbSize = FIELD_OFFSET(NONCLIENTMETRICS, lfMessageFont) + sizeof(LOGFONT);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize , &ncm, 0);
NONCLIENTMETRICS ncm = {};
ncm.cbSize = sizeof(ncm);
SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0, defaultVerticalDPI());
const QFont systemFont = QWindowsFontDatabase::LOGFONT_to_QFont(ncm.lfMessageFont);
qCDebug(lcQpaFonts) << __FUNCTION__ << systemFont;
return systemFont;

View File

@ -57,7 +57,6 @@ public:
QFontEngine *fontEngine(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference) override;
static int defaultVerticalDPI();
static void setDefaultVerticalDPI(int d);
static QSharedPointer<QWindowsFontEngineData> data();
#if QT_CONFIG(directwrite)

View File

@ -182,11 +182,8 @@ static bool monitorData(HMONITOR hMonitor, QWindowsScreenData *data)
// EnumDisplayMonitors (as opposed to EnumDisplayDevices) enumerates only
// virtual desktop screens.
data->flags |= QWindowsScreenData::VirtualDesktop;
if (info.dwFlags & MONITORINFOF_PRIMARY) {
if (info.dwFlags & MONITORINFOF_PRIMARY)
data->flags |= QWindowsScreenData::PrimaryScreen;
if ((data->flags & QWindowsScreenData::LockScreen) == 0)
QWindowsFontDatabase::setDefaultVerticalDPI(data->dpi.second);
}
return true;
}