optimize QFont::lastResortFont()

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-12-26 21:19:00 +00:00
parent 47a2cf587e
commit edb9f7c9c9

View file

@ -245,14 +245,7 @@ QString QFont::defaultFamily() const
} }
} }
/* static const char* LastResortFontsTbl[] = {
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[] = {
"-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*", "-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*",
"-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*", "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*",
"-*-times-medium-r-*-*-*-120-*-*-*-*-*-*", "-*-times-medium-r-*-*-*-120-*-*-*-*-*-*",
@ -271,37 +264,35 @@ static const char * const tryFonts[] = {
"8x13", "8x13",
"9x15", "9x15",
"fixed", "fixed",
0
}; };
static const qint16 LastResortFontsTblSize = 18;
// Returns true if the font exists, false otherwise /*
static bool fontExists(const QString &fontName) 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
int count; returns \e something, almost no matter what. The current
char **fontNames = XListFonts(QX11Info::display(), (char*)fontName.toLatin1().constData(), 32768, &count); implementation tries a wide variety of common fonts, returning the
if (fontNames) XFreeFontNames(fontNames); first one it finds. The implementation may change at any time.
*/
return count != 0;
}
QString QFont::lastResortFont() const QString QFont::lastResortFont() const
{ {
static QString last; static QString last;
// already found // already found
if (! last.isNull()) if (!last.isNull())
return last; return last;
int i = 0; for (qint16 i = 0; i < LastResortFontsTblSize; i++) {
const char* f; int count;
char **fontNames = XListFonts(QX11Info::display(), LastResortFontsTbl[i], SHRT_MAX, &count);
if (fontNames) {
XFreeFontNames(fontNames);
}
while ((f = tryFonts[i])) { if (count != 0) {
last = QString::fromLatin1(f); last = QString::fromLatin1(LastResortFontsTbl[i]);
if (fontExists(last))
return last; return last;
}
i++;
} }
#if defined(CHECK_NULL) #if defined(CHECK_NULL)