]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Don't rescan when trying to normalize a non-declared font dir. Don't add
[fontconfig.git] / src / fccfg.c
index a5de075f2a9c7bc04bda6c20e1069bba7997f1ae..cae41b0ccd18ae6f7c3ad6babd22e54626c3d279 100644 (file)
@@ -22,6 +22,8 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <dirent.h>
+#include <sys/types.h>
 #include "fcint.h"
 
 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
@@ -139,11 +141,6 @@ bail0:
     return 0;
 }
 
-typedef struct _FcFileTime {
-    time_t  time;
-    FcBool  set;
-} FcFileTime;
-
 static FcFileTime
 FcConfigNewestFile (FcStrSet *files)
 {
@@ -166,6 +163,19 @@ FcConfigNewestFile (FcStrSet *files)
     return newest;
 }
 
+FcFileTime
+FcConfigModifiedTime (FcConfig *config)
+{
+    if (!config)
+    {
+       FcFileTime v = { 0, FcFalse };
+       config = FcConfigGetCurrent ();
+       if (!config)
+           return v;
+    }
+    return FcConfigNewestFile (config->configFiles);
+}
+
 FcBool
 FcConfigUptoDate (FcConfig *config)
 {
@@ -247,54 +257,85 @@ FcConfigDestroy (FcConfig *config)
 FcBool
 FcConfigBuildFonts (FcConfig *config)
 {
-    FcFontSet      *fonts;
+    FcFontSet      *fonts, *cached_fonts;
     FcGlobalCache   *cache;
     FcStrList      *list;
+    FcStrSet       *oldDirs;
     FcChar8        *dir;
 
-#if 0
-    if (config->cache)
-       FcGlobalCacheLoad (cache, config->cache);
-#endif
-    if (config->fonts[FcSetSystem])
-       return FcTrue;
-
     fonts = FcFontSetCreate ();
     if (!fonts)
        goto bail0;
-
-#if 0
+    
     cache = FcGlobalCacheCreate ();
     if (!cache)
        goto bail1;
-#endif
 
-    list = FcConfigGetFontDirs (config);
-    if (!list)
-       goto bail1;
+    oldDirs = FcStrSetCreate ();
+    if (!oldDirs)
+        goto bail2;
 
-    while ((dir = FcStrListNext (list)))
+    if (config->cache)
+       FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
+
+    cached_fonts = FcCacheRead(config, cache);
+    if (!cached_fonts)
+    {
+       list = FcConfigGetFontDirs (config);
+       if (!list)
+           goto bail2;
+       
+       while ((dir = FcStrListNext (list)))
+       {
+           if (FcDebug () & FC_DBG_FONTSET)
+               printf ("build scan dir %s\n", dir);
+           FcDirScanConfig (fonts, config->fontDirs, cache, 
+                            config->blanks, dir, FcFalse, config);
+       }
+       
+       FcStrListDone (list);
+    }
+    else
     {
-       if (FcDebug () & FC_DBG_FONTSET)
-           printf ("scan dir %s\n", dir);
-       FcDirScanConfig (fonts, config->fontDirs, cache, 
-                        config->blanks, dir, FcFalse, config);
+       int i;
+
+        for (i = 0; i < oldDirs->num; i++)
+        {
+           if (FcDebug () & FC_DBG_FONTSET)
+               printf ("scan dir %s\n", oldDirs->strs[i]);
+           FcDirScanConfig (fonts, config->fontDirs, cache, 
+                            config->blanks, oldDirs->strs[i], 
+                             FcFalse, config);
+       }
+
+       for (i = 0; i < cached_fonts->nfont; i++)
+       {
+           FcChar8     *cfn; 
+           FcPatternGetString (cached_fonts->fonts[i], FC_FILE, 0, &cfn);
+
+           if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
+                (cfn && FcConfigAcceptFilename (config, cfn)))
+               FcFontSetAdd (fonts, cached_fonts->fonts[i]);
+
+           cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
+       }
+       cached_fonts->nfont = 0;
+       FcFontSetDestroy (cached_fonts);
     }
     
-    FcStrListDone (list);
-    
     if (FcDebug () & FC_DBG_FONTSET)
        FcFontSetPrint (fonts);
 
-#if 0
     if (config->cache)
-       FcGlobalCacheSave (cache, config->cache);
+       FcGlobalCacheSave (cache, config->cache, config);
     FcGlobalCacheDestroy (cache);
-#endif
+    FcStrSetDestroy (oldDirs);
 
     FcConfigSetFonts (config, fonts, FcSetSystem);
     
     return FcTrue;
+bail2:
+    FcStrSetDestroy (oldDirs);
 bail1:
     FcFontSetDestroy (fonts);
 bail0:
@@ -342,13 +383,113 @@ FcConfigGetConfigDirs (FcConfig   *config)
     return FcStrListCreate (config->configDirs);
 }
 
+static FcChar8 *
+FcConfigInodeMatchFontDir (FcConfig *config, const FcChar8 *d)
+{
+    int                n;
+    ino_t      di;
+    dev_t      dd;
+    struct stat s;
+
+    /* first we do string matches rather than file accesses */
+    /* FcStrSetMember doesn't tell us the index so that we can return
+     * the config-owned copy. */
+    for (n = 0; n < config->fontDirs->num; n++)
+    {
+       if (!FcStrCmp (config->fontDirs->strs[n], d))
+           return config->fontDirs->strs[n];
+    }
+
+    /* If this is a bottleneck, we can cache the fontDir inodes. */
+    if (stat ((char *)d, &s) == -1)
+       return 0;
+    di = s.st_ino; dd = s.st_dev;
+
+    for (n = 0; n < config->fontDirs->num; n++)
+    {
+       if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
+           continue;
+       if (di == s.st_ino && dd == s.st_dev)
+           return config->fontDirs->strs[n];
+    }
+    return 0;
+}
+
 FcBool
 FcConfigAddFontDir (FcConfig       *config,
                    const FcChar8   *d)
 {
+    /* Avoid adding d if it's an alias of something else, too. */
+    if (FcConfigInodeMatchFontDir(config, d))
+       return FcTrue;
     return FcStrSetAddFilename (config->fontDirs, d);
 }
 
+static FcBool
+FcConfigAddFontDirSubdirs (FcConfig        *config,
+                          const FcChar8   *d)
+{
+    DIR *dir;
+    struct dirent *e;
+    FcChar8 *subdir;
+    FcBool added = FcFalse;
+    
+    if (!(dir = opendir ((char *) d)))
+       return FcFalse;
+    if (!(subdir = (FcChar8 *) malloc (strlen ((char *) d) + FC_MAX_FILE_LEN + 2)))
+    {
+       fprintf (stderr, "out of memory");
+       return FcFalse;
+    }
+    while ((e = readdir (dir)))
+    {
+       if (strcmp (e->d_name, ".") && strcmp (e->d_name, "..") &&
+           strlen (e->d_name) < FC_MAX_FILE_LEN)
+       {
+           strcpy ((char *)subdir, (char *)d);
+           strcat ((char *)subdir, "/");
+           strcat ((char *)subdir, e->d_name);
+           if (FcFileIsDir (subdir))
+           {
+               if (FcConfigInodeMatchFontDir(config, subdir))
+                   continue; /* already added */
+               FcStrSetAddFilename (config->fontDirs, subdir);
+               FcConfigAddFontDirSubdirs (config, subdir);
+               added = FcTrue;
+           }
+       }
+    }
+    free (subdir);
+    closedir (dir);
+    return added;
+}
+
+const FcChar8 *
+FcConfigNormalizeFontDir (FcConfig     *config, 
+                         const FcChar8 *d)
+{
+    FcChar8    *d0;
+    int                n, n0;
+    FcBool     added = FcFalse;
+
+    d0 = FcConfigInodeMatchFontDir(config, d);
+    if (d0)
+       return d0;
+
+    /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
+    for (n = 0, n0 = config->fontDirs->num; n < n0; n++) 
+    {
+       if (FcConfigAddFontDirSubdirs (config, config->fontDirs->strs[n]))
+           added = FcTrue;
+    }
+
+    /* ... and try again. */
+    if (added)
+       return FcConfigInodeMatchFontDir(config, d);
+
+    return 0;
+}
+
 FcBool
 FcConfigAddDir (FcConfig           *config,
                const FcChar8       *d)
@@ -558,25 +699,24 @@ FcConfigPromote (FcValue v, FcValue u)
     }
     else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
     {
-       v.u.mi = FcIdentityMatrix;
+       v.u.m = &FcIdentityMatrix;
        v.type = FcTypeMatrix;
     }
     else if (v.type == FcTypeString && u.type == FcTypeLangSet)
     {
-       v.u.li = FcLangSetPtrCreateDynamic(FcLangSetPromote 
-                                          (FcObjectPtrU(v.u.si)));
+       v.u.l = FcLangSetPromote (v.u.s);
        v.type = FcTypeLangSet;
     }
     return v;
 }
 
 FcBool
-FcConfigCompareValue (const FcValue    left_o,
+FcConfigCompareValue (const FcValue    *left_o,
                      FcOp              op,
-                     const FcValue     right_o)
+                     const FcValue     *right_o)
 {
-    FcValue    left = left_o;
-    FcValue    right = right_o;
+    FcValue    left = FcValueCanonicalize(left_o);
+    FcValue    right = FcValueCanonicalize(right_o);
     FcBool     ret = FcFalse;
     
     left = FcConfigPromote (left, right);
@@ -632,20 +772,16 @@ FcConfigCompareValue (const FcValue       left_o,
            switch (op) {
            case FcOpEqual:    
            case FcOpListing:
-               ret = FcStrCmpIgnoreCase (FcObjectPtrU(left.u.si), 
-                                         FcObjectPtrU(right.u.si)) == 0;
+               ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
                break;
            case FcOpContains:
-               ret = FcStrStrIgnoreCase (FcObjectPtrU(left.u.si), 
-                                         FcObjectPtrU(right.u.si)) != 0;
+               ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
                break;
            case FcOpNotEqual:
-               ret = FcStrCmpIgnoreCase (FcObjectPtrU(left.u.si), 
-                                         FcObjectPtrU(right.u.si)) != 0;
+               ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
                break;
            case FcOpNotContains:
-               ret = FcStrCmpIgnoreCase (FcObjectPtrU(left.u.si), 
-                                         FcObjectPtrU(right.u.si)) == 0;
+               ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
                break;
            default:
                break;
@@ -656,11 +792,11 @@ FcConfigCompareValue (const FcValue       left_o,
            case FcOpEqual:
            case FcOpContains:
            case FcOpListing:
-               ret = FcMatrixEqual (FcMatrixPtrU(left.u.mi), FcMatrixPtrU(right.u.mi));
+               ret = FcMatrixEqual (left.u.m, right.u.m);
                break;
            case FcOpNotEqual:
            case FcOpNotContains:
-               ret = !FcMatrixEqual (FcMatrixPtrU(left.u.mi), FcMatrixPtrU(right.u.mi));
+               ret = !FcMatrixEqual (left.u.m, right.u.m);
                break;
            default:
                break;
@@ -671,17 +807,17 @@ FcConfigCompareValue (const FcValue       left_o,
            case FcOpContains:
            case FcOpListing:
                /* left contains right if right is a subset of left */
-               ret = FcCharSetIsSubset (FcCharSetPtrU(right.u.ci), FcCharSetPtrU(left.u.ci));
+               ret = FcCharSetIsSubset (right.u.c, left.u.c);
                break;
            case FcOpNotContains:
                /* left contains right if right is a subset of left */
-               ret = !FcCharSetIsSubset (FcCharSetPtrU(right.u.ci), FcCharSetPtrU(left.u.ci));
+               ret = !FcCharSetIsSubset (right.u.c, left.u.c);
                break;
            case FcOpEqual:
-               ret = FcCharSetEqual (FcCharSetPtrU(left.u.ci), FcCharSetPtrU(right.u.ci));
+               ret = FcCharSetEqual (left.u.c, right.u.c);
                break;
            case FcOpNotEqual:
-               ret = !FcCharSetEqual (FcCharSetPtrU(left.u.ci), FcCharSetPtrU(right.u.ci));
+               ret = !FcCharSetEqual (left.u.c, right.u.c);
                break;
            default:
                break;
@@ -691,16 +827,16 @@ FcConfigCompareValue (const FcValue       left_o,
            switch (op) {
            case FcOpContains:
            case FcOpListing:
-               ret = FcLangSetContains (FcLangSetPtrU(left.u.li), FcLangSetPtrU(right.u.li));
+               ret = FcLangSetContains (left.u.l, right.u.l);
                break;
            case FcOpNotContains:
-               ret = !FcLangSetContains (FcLangSetPtrU(left.u.li), FcLangSetPtrU(right.u.li));
+               ret = !FcLangSetContains (left.u.l, right.u.l);
                break;
            case FcOpEqual:
-               ret = FcLangSetEqual (FcLangSetPtrU(left.u.li), FcLangSetPtrU(right.u.li));
+               ret = FcLangSetEqual (left.u.l, right.u.l);
                break;
            case FcOpNotEqual:
-               ret = !FcLangSetEqual (FcLangSetPtrU(left.u.li), FcLangSetPtrU(right.u.li));
+               ret = !FcLangSetEqual (left.u.l, right.u.l);
                break;
            default:
                break;
@@ -768,17 +904,16 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        break;
     case FcOpString:
        v.type = FcTypeString;
-       v.u.si = FcObjectStaticName(e->u.sval);
-       v = FcValueSave (v);
+       v.u.s = FcStrStaticName(e->u.sval);
        break;
     case FcOpMatrix:
        v.type = FcTypeMatrix;
-       v.u.mi = FcMatrixPtrCreateDynamic(e->u.mval);
+       v.u.m = e->u.mval;
        v = FcValueSave (v);
        break;
     case FcOpCharSet:
        v.type = FcTypeCharSet;
-       v.u.ci = FcCharSetPtrCreateDynamic(e->u.cval);
+       v.u.c = e->u.cval;
        v = FcValueSave (v);
        break;
     case FcOpBool:
@@ -789,6 +924,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        r = FcPatternGet (p, e->u.field, 0, &v);
        if (r != FcResultMatch)
            v.type = FcTypeVoid;
+       v = FcValueSave (v);
        break;
     case FcOpConst:
        if (FcNameConstant (e->u.constant, &v.u.i))
@@ -821,7 +957,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        vl = FcConfigEvaluate (p, e->u.tree.left);
        vr = FcConfigEvaluate (p, e->u.tree.right);
        v.type = FcTypeBool;
-       v.u.b = FcConfigCompareValue (vl, e->op, vr);
+       v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
        FcValueDestroy (vl);
        FcValueDestroy (vr);
        break;  
@@ -886,11 +1022,9 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                switch (e->op) {
                case FcOpPlus:
                    v.type = FcTypeString;
-                   v.u.si = FcObjectStaticName
-                       (FcStrPlus (FcObjectPtrU(vl.u.si), 
-                                   FcObjectPtrU(vr.u.si)));
+                   v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
                         
-                   if (!FcObjectPtrU(v.u.si))
+                   if (!v.u.s)
                        v.type = FcTypeVoid;
                    break;
                default:
@@ -906,9 +1040,8 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                    if (m)
                    {
                        FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
-                       FcMatrixMultiply (m, FcMatrixPtrU(vl.u.mi), 
-                                         FcMatrixPtrU(vr.u.mi));
-                       v.u.mi = FcMatrixPtrCreateDynamic(m);
+                       FcMatrixMultiply (m, vl.u.m, vr.u.m);
+                       v.u.m = m;
                    }
                    else
                    {
@@ -1041,7 +1174,7 @@ FcConfigMatchValueList (FcPattern *p,
        for (v = values; v; v = FcValueListPtrU(v->next))
        {
            /* Compare the pattern value to the match expression value */
-           if (FcConfigCompareValue (v->value, t->op, value))
+           if (FcConfigCompareValue (&v->value, t->op, &value))
            {
                if (!ret)
                    ret = v;
@@ -1064,7 +1197,6 @@ static FcValueList *
 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
 {
     FcValueList        *l;
-    FcValueListPtr lp;
     
     if (!e)
        return 0;
@@ -1083,18 +1215,15 @@ FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
        l->next  = FcValueListPtrCreateDynamic(0);
     }
     l->binding = binding;
-    lp = FcValueListPtrCreateDynamic(l);
-    while (FcValueListPtrU(lp) && FcValueListPtrU(lp)->value.type == FcTypeVoid)
+    if (l->value.type == FcTypeVoid)
     {
-       FcValueListPtr  next = FcValueListPtrU(lp)->next;
+       FcValueList  *next = FcValueListPtrU(l->next);
 
-       if (lp.storage == FcStorageDynamic)
-       {
-           FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
-           free (l);
-       }
-       lp = next;
+       FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
+       free (l);
+       l = next;
     }
+
     return l;
 }
 
@@ -1633,7 +1762,7 @@ FcConfigHome (void)
            home = getenv ("USERPROFILE");
 #endif
 
-       return home;
+       return (FcChar8 *) home;
     }
     return 0;
 }
@@ -1746,6 +1875,7 @@ FcConfigAppFontAddFile (FcConfig    *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }
 
@@ -1793,6 +1923,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }
 
@@ -1866,7 +1997,7 @@ FcConfigGlobsMatch (const FcStrSet        *globs,
     int        i;
 
     for (i = 0; i < globs->num; i++)
-       if (FcConfigGlobMatch (FcStrSetGet(globs, i), string))
+       if (FcConfigGlobMatch (globs->strs[i], string))
            return FcTrue;
     return FcFalse;
 }