Skip to content

Commit

Permalink
Windows GUI: Use native font size from new API, not 120% of old API
Browse files Browse the repository at this point in the history
Code was copied from Qt library's dev branch's QWindowsFontDatabase,
which (unlike the 5.x branch) uses SPI_GETNONCLIENTMETRICS
to get the right font on Vista and up.
  • Loading branch information
nyanpasu64 committed Sep 11, 2019
1 parent 828dc0f commit 300d9c3
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions Source/Core/DolphinQt/Main.cpp
Expand Up @@ -80,6 +80,45 @@ static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no
return false; return false;
} }


#ifdef _WIN32
// Taken from https://github.com/qt/qtbase/blob/835c3e94f6089751421a19008d442faec9649ed8/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp#L2064
int defaultVerticalDPI()
{
static int vDPI = -1;
if (vDPI == -1)
{
if (HDC defaultDC = GetDC(0))
{
vDPI = GetDeviceCaps(defaultDC, LOGPIXELSY);
ReleaseDC(0, defaultDC);
}
else
{
// FIXME: Resolve now or return 96 and keep unresolved?
vDPI = 96;
}
}
return vDPI;
}

// Taken from https://github.com/qt/qtbase/blob/835c3e94f6089751421a19008d442faec9649ed8/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp#L2048
QFont LOGFONT_to_QFont(const LOGFONT& logFont, int verticalDPI_In = 0)
{
if (verticalDPI_In <= 0)
verticalDPI_In = defaultVerticalDPI();
QFont qFont(QString::fromWCharArray(logFont.lfFaceName));
qFont.setItalic(logFont.lfItalic);
//if (logFont.lfWeight != FW_DONTCARE)
// qFont.setWeight(QPlatformFontDatabase::weightFromInteger(logFont.lfWeight));
const qreal logFontHeight = qAbs(logFont.lfHeight);
qFont.setPointSizeF(logFontHeight * 72.0 / qreal(verticalDPI_In));
qFont.setUnderline(logFont.lfUnderline);
qFont.setOverline(false);
qFont.setStrikeOut(logFont.lfStrikeOut);
return qFont;
}
#endif

// N.B. On Windows, this should be called from WinMain. Link against qtmain and specify // N.B. On Windows, this should be called from WinMain. Link against qtmain and specify
// /SubSystem:Windows // /SubSystem:Windows
int main(int argc, char* argv[]) int main(int argc, char* argv[])
Expand Down Expand Up @@ -112,15 +151,7 @@ int main(int argc, char* argv[])
{ {
// Sadly Qt 5 doesn't support turning a native font handle into a QFont so this is the next best // Sadly Qt 5 doesn't support turning a native font handle into a QFont so this is the next best
// thing // thing
QFont font = QApplication::font(); QFont font = LOGFONT_to_QFont(logfont);
font.setFamily(QString::fromStdString(UTF16ToUTF8(logfont.lfFaceName)));

font.setItalic(logfont.lfItalic);
font.setStrikeOut(logfont.lfStrikeOut);
font.setUnderline(logfont.lfUnderline);

// The default font size is a bit too small
font.setPointSize(QFontInfo(font).pointSize() * 1.2);


QApplication::setFont(font); QApplication::setFont(font);
} }
Expand Down

0 comments on commit 300d9c3

Please sign in to comment.