speed up font enumeration in windows platform
authorjian liang <jianliang79@gmail.com>
Wed, 1 Feb 2012 15:19:46 +0000 (23:19 +0800)
committerQt by Nokia <qt-info@nokia.com>
Thu, 2 Feb 2012 10:10:04 +0000 (11:10 +0100)
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 <lars.knoll@nokia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp

index b32b8aa..e96305e 100644 (file)
@@ -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<FontKey> 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;
             }