OSDN Git Service

optimize and make font tables lookups consistent
authorIvailo Monev <xakepa10@laimg.moc>
Thu, 26 Dec 2019 18:58:00 +0000 (18:58 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Thu, 26 Dec 2019 18:58:00 +0000 (18:58 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/gui/text/qfont.cpp
src/gui/text/qfontdatabase_x11_p.h

index d2d6a2a..7a6fbee 100644 (file)
@@ -1680,22 +1680,20 @@ QFont QFont::resolve(const QFont &other) const
 typedef QHash<QString, QStringList> QFontSubst;
 Q_GLOBAL_STATIC(QFontSubst, globalFontSubst)
 
+static const struct FontSubstitutesTblData {
+    const QLatin1String original;
+    const QLatin1String substitute;
+} FontSubstitutesTbl[] = {
+    { QLatin1String("arial"), QLatin1String("helvetica") },
+    { QLatin1String("times new roman"), QLatin1String("times") },
+    { QLatin1String("courier new"), QLatin1String("courier") },
+    { QLatin1String("sans serif"), QLatin1String("helvetica") },
+};
+static const qint16 FontSubstitutesTblSize = sizeof(FontSubstitutesTbl) / sizeof(FontSubstitutesTblData);
+
 // create substitution dict
 static void initFontSubst()
 {
-    // default substitutions
-    static const char * const initTbl[] = {
-
-#if defined(Q_WS_X11)
-        "arial",        "helvetica",
-        "times new roman", "times",
-        "courier new",  "courier",
-        "sans serif",   "helvetica",
-#endif
-
-        0,              0
-    };
-
     QFontSubst *fontSubst = globalFontSubst();
     Q_ASSERT(fontSubst != 0);
     if (!fontSubst->isEmpty())
@@ -1705,9 +1703,9 @@ static void initFontSubst()
         return;
 #endif
 
-    for (int i=0; initTbl[i] != 0; i += 2) {
-        QStringList &list = (*fontSubst)[QString::fromLatin1(initTbl[i])];
-        list.append(QString::fromLatin1(initTbl[i+1]));
+    for (qint16 i = 0; i < FontSubstitutesTblSize; i++) {
+        QStringList &list = (*fontSubst)[FontSubstitutesTbl[i].original];
+        list.append(FontSubstitutesTbl[i].substitute);
     }
 }
 
index 586fba1..ac63910 100644 (file)
@@ -545,6 +545,25 @@ static const uint specialChars[] = {
 };
 enum { SpecialCharCount = sizeof(specialChars) / sizeof(uint) };
 
+static const struct DefaultFontTblData {
+    const QLatin1String name;
+    const bool fixedpitch;
+} DefaultFontTbl[] = {
+    { QLatin1String("Serif"), false },
+    { QLatin1String("Sans Serif"), false },
+    { QLatin1String("Monospace"), true },
+};
+static const qint16 DefaultFontTblSize = sizeof(DefaultFontTbl) / sizeof(DefaultFontTblData);
+
+static const char* PatternPropertiesTbl[] = {
+    FC_FAMILY, FC_STYLE, FC_WEIGHT, FC_SLANT,
+    FC_SPACING, FC_FILE, FC_INDEX,
+    FC_LANG, FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, FC_WEIGHT,
+    FC_WIDTH,
+    FC_CAPABILITY,
+};
+static const qint16 PatternPropertiesTblSize = 15;
+
 static void loadFontConfig()
 {
     Q_ASSERT_X(qt_x11Data, "QFontDatabase",
@@ -581,18 +600,8 @@ static void loadFontConfig()
     {
         FcObjectSet *os = FcObjectSetCreate();
         FcPattern *pattern = FcPatternCreate();
-        const char *properties [] = {
-            FC_FAMILY, FC_STYLE, FC_WEIGHT, FC_SLANT,
-            FC_SPACING, FC_FILE, FC_INDEX,
-            FC_LANG, FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, FC_WEIGHT,
-            FC_WIDTH,
-            FC_CAPABILITY,
-            (const char *)0
-        };
-        const char **p = properties;
-        while (*p) {
-            FcObjectSetAdd(os, *p);
-            ++p;
+        for (qint16 i = 0; i < PatternPropertiesTblSize; i++) {
+            FcObjectSetAdd(os, PatternPropertiesTbl[i]);
         }
         fonts = FcFontList(0, pattern, os);
         FcObjectSetDestroy(os);
@@ -670,34 +679,22 @@ static void loadFontConfig()
 
     FcFontSetDestroy (fonts);
 
-    struct FcDefaultFont {
-        const char *qtname;
-        bool fixed;
-    };
-    const FcDefaultFont defaults[] = {
-        { "Serif", false },
-        { "Sans Serif", false },
-        { "Monospace", true },
-        { 0, false }
-    };
-    const FcDefaultFont *f = defaults;
-    while (f->qtname) {
-        QtFontFamily *family = db->family(QLatin1String(f->qtname), true);
-        family->fixedPitch = f->fixed;
+    for (qint16 i = 0; i < DefaultFontTblSize; i++) {
+        QtFontFamily *family = db->family(DefaultFontTbl[i].name, true);
+        family->fixedPitch = DefaultFontTbl[i].fixedpitch;
         family->synthetic = true;
         QtFontFoundry *foundry = family->foundry(QString(), true);
 
         QtFontStyle::Key styleKey;
-        for (int i = 0; i < 4; ++i) {
-            styleKey.style = (i%2) ? QFont::StyleNormal : QFont::StyleItalic;
-            styleKey.weight = (i > 1) ? QFont::Bold : QFont::Normal;
+        for (int j = 0; j < 4; j++) {
+            styleKey.style = (j%2) ? QFont::StyleNormal : QFont::StyleItalic;
+            styleKey.weight = (j > 1) ? QFont::Bold : QFont::Normal;
             QtFontStyle *style = foundry->style(styleKey, QString(), true);
             style->smoothScalable = true;
             QtFontSize *size = style->pixelSize(SMOOTH_SCALABLE, true);
             QtFontEncoding *enc = size->encodingID(-1, 0, 0, 0, 0, true);
-            enc->pitch = (f->fixed ? 'm' : 'p');
+            enc->pitch = (DefaultFontTbl[i].fixedpitch ? 'm' : 'p');
         }
-        ++f;
     }
 }
 #endif // QT_NO_FONTCONFIG