From b852b019586cd53c2b49848ff2ae703fb0815308 Mon Sep 17 00:00:00 2001 From: jian liang Date: Wed, 1 Feb 2012 23:19:46 +0800 Subject: [PATCH] speed up font enumeration in windows platform Currently, font enumeration is very slow in windows platform, it will take several seconds to enumerate all fonts for the first time. This patch cache the font information queried from font registry, it can significantly speed up font enumeration. Change-Id: Ic783877b7f3db3facf24965b0c5d669b22d40c61 Reviewed-by: Lars Knoll Reviewed-by: Friedemann Kleint --- .../windows/qwindowsfontdatabase_ft.cpp | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp b/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp index b32b8aa59d..e96305e4b7 100644 --- a/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp +++ b/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp @@ -172,17 +172,40 @@ static bool addFontToDatabase(QString familyName, const QString &scriptName, const QSettings fontRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", QSettings::NativeFormat); + struct FontKey + { + FontKey() {} + FontKey(const QString &k) : key(k) {} + + QString key; + QStringList fonts; + }; + static QVector allFonts; + if (allFonts.isEmpty()) { + const QStringList allKeys = fontRegistry.allKeys(); + allFonts.reserve(allKeys.size()); + const QString trueType = QStringLiteral("(TrueType)"); + foreach (const QString &key, allKeys) { + FontKey fontKey(key); + + QString realKey = key; + realKey = realKey.remove(trueType); + const QStringList fonts = realKey.trimmed().split(QLatin1Char('&')); + foreach (const QString &font, fonts) + fontKey.fonts.push_back(font.trimmed()); + + allFonts.push_back(fontKey); + } + } + QByteArray value; int index = 0; - foreach (const QString &key, fontRegistry.allKeys()) { - QString realKey = key; - realKey = realKey.replace("(TrueType)", ""); - realKey = realKey.trimmed(); - QStringList fonts = realKey.split('&'); - for (int i = 0; i < fonts.length(); ++i) { - const QString font = fonts[i].trimmed(); + for (int k = 0; k < allFonts.size(); ++k) { + const FontKey &fontKey = allFonts.at(k); + for (int i = 0; i < fontKey.fonts.length(); ++i) { + const QString font = fontKey.fonts[i]; if (font == faceName || (faceName != fullName && fullName == font)) { - value = fontRegistry.value(key).toByteArray(); + value = fontRegistry.value(fontKey.key).toByteArray(); index = i; break; }