From edb9f7c9c93220c0ffb53db1099a5dcc3d1fc5e7 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 26 Dec 2019 21:19:00 +0000 Subject: [PATCH] optimize QFont::lastResortFont() Signed-off-by: Ivailo Monev --- src/gui/text/qfont_x11.cpp | 47 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/gui/text/qfont_x11.cpp b/src/gui/text/qfont_x11.cpp index 210128480..b838af952 100644 --- a/src/gui/text/qfont_x11.cpp +++ b/src/gui/text/qfont_x11.cpp @@ -245,14 +245,7 @@ QString QFont::defaultFamily() const } } -/* - Returns a last resort raw font name for the font matching algorithm. - This is used if even the last resort family is not available. It - returns \e something, almost no matter what. The current - implementation tries a wide variety of common fonts, returning the - first one it finds. The implementation may change at any time. -*/ -static const char * const tryFonts[] = { +static const char* LastResortFontsTbl[] = { "-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*", "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*", "-*-times-medium-r-*-*-*-120-*-*-*-*-*-*", @@ -271,37 +264,35 @@ static const char * const tryFonts[] = { "8x13", "9x15", "fixed", - 0 }; +static const qint16 LastResortFontsTblSize = 18; -// Returns true if the font exists, false otherwise -static bool fontExists(const QString &fontName) -{ - int count; - char **fontNames = XListFonts(QX11Info::display(), (char*)fontName.toLatin1().constData(), 32768, &count); - if (fontNames) XFreeFontNames(fontNames); - - return count != 0; -} - +/* + Returns a last resort raw font name for the font matching algorithm. + This is used if even the last resort family is not available. It + returns \e something, almost no matter what. The current + implementation tries a wide variety of common fonts, returning the + first one it finds. The implementation may change at any time. +*/ QString QFont::lastResortFont() const { static QString last; // already found - if (! last.isNull()) + if (!last.isNull()) return last; - int i = 0; - const char* f; + for (qint16 i = 0; i < LastResortFontsTblSize; i++) { + int count; + char **fontNames = XListFonts(QX11Info::display(), LastResortFontsTbl[i], SHRT_MAX, &count); + if (fontNames) { + XFreeFontNames(fontNames); + } - while ((f = tryFonts[i])) { - last = QString::fromLatin1(f); - - if (fontExists(last)) + if (count != 0) { + last = QString::fromLatin1(LastResortFontsTbl[i]); return last; - - i++; + } } #if defined(CHECK_NULL) -- 2.11.0