]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Eliminate NormalizeDir. Eliminate gratuitous stat/access calls per dir.
[fontconfig.git] / src / fccfg.c
index 079e18c25514604c9b66784169606dd70289ef81..3c3681a100620e9c803e811aae0f55195121e340 100644 (file)
@@ -23,6 +23,8 @@
  */
 
 #include "fcint.h"
+#include <dirent.h>
+#include <sys/types.h>
 
 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
 #define STRICT
@@ -98,13 +100,17 @@ FcConfigCreate (void)
            if (!FcConfigSetCache (config, cache_dir))
            {
                FcStrFree (cache_dir);
-               goto bail6;
+               goto bail8;
            }
            FcStrFree (cache_dir);
        }
     }
 #endif
 
+    config->cacheDirs = FcStrSetCreate ();
+    if (!config->cacheDirs)
+       goto bail9;
+    
     config->blanks = 0;
 
     config->substPattern = 0;
@@ -118,6 +124,8 @@ FcConfigCreate (void)
     
     return config;
 
+bail9:
+    FcStrFree (config->cache);
 bail8:
     FcFontSetDestroy (config->rejectPatterns);
 bail7:
@@ -139,11 +147,6 @@ bail0:
     return 0;
 }
 
-typedef struct _FcFileTime {
-    time_t  time;
-    FcBool  set;
-} FcFileTime;
-
 static FcFileTime
 FcConfigNewestFile (FcStrSet *files)
 {
@@ -166,6 +169,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)
 {
@@ -216,6 +232,7 @@ FcConfigDestroy (FcConfig *config)
 
     FcStrSetDestroy (config->configDirs);
     FcStrSetDestroy (config->fontDirs);
+    FcStrSetDestroy (config->cacheDirs);
     FcStrSetDestroy (config->configFiles);
     FcStrSetDestroy (config->acceptGlobs);
     FcStrSetDestroy (config->rejectGlobs);
@@ -247,9 +264,10 @@ FcConfigDestroy (FcConfig *config)
 FcBool
 FcConfigBuildFonts (FcConfig *config)
 {
-    FcFontSet      *fonts;
+    FcFontSet      *fonts, *cached_fonts;
     FcGlobalCache   *cache;
     FcStrList      *list;
+    FcStrSet       *oldDirs;
     FcChar8        *dir;
 
     fonts = FcFontSetCreate ();
@@ -260,33 +278,73 @@ FcConfigBuildFonts (FcConfig *config)
     if (!cache)
        goto bail1;
 
-    if (config->cache)
-       FcGlobalCacheLoad (cache, config->cache);
+    oldDirs = FcStrSetCreate ();
+    if (!oldDirs)
+        goto bail2;
 
-    list = FcConfigGetFontDirs (config);
-    if (!list)
-       goto bail1;
+    if (config->cache)
+       FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
 
-    while ((dir = FcStrListNext (list)))
+    cached_fonts = FcCacheRead(config, cache);
+    if (!cached_fonts)
+    {
+       list = FcConfigGetFontDirs (config);
+       if (!list)
+           goto bail3;
+       
+       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 (config->cache)
-       FcGlobalCacheSave (cache, config->cache);
+       FcGlobalCacheSave (cache, config->cache, config);
     FcGlobalCacheDestroy (cache);
+    FcStrSetDestroy (oldDirs);
 
     FcConfigSetFonts (config, fonts, FcSetSystem);
     
     return FcTrue;
+bail3:
+    FcStrSetDestroy (oldDirs);
+bail2:
+    FcGlobalCacheDestroy (cache);
 bail1:
     FcFontSetDestroy (fonts);
 bail0:
@@ -361,6 +419,25 @@ FcConfigGetFontDirs (FcConfig      *config)
     return FcStrListCreate (config->fontDirs);
 }
 
+FcBool
+FcConfigAddCacheDir (FcConfig      *config,
+                    const FcChar8  *d)
+{
+    return FcStrSetAddFilename (config->cacheDirs, d);
+}
+
+FcStrList *
+FcConfigGetCacheDirs (FcConfig *config)
+{
+    if (!config)
+    {
+       config = FcConfigGetCurrent ();
+       if (!config)
+           return 0;
+    }
+    return FcStrListCreate (config->cacheDirs);
+}
+    
 FcBool
 FcConfigAddConfigFile (FcConfig            *config,
                       const FcChar8   *f)
@@ -455,17 +532,21 @@ FcBool
 FcConfigAddBlank (FcConfig     *config,
                  FcChar32      blank)
 {
-    FcBlanks   *b;
+    FcBlanks   *b, *freeme = 0;
     
     b = config->blanks;
     if (!b)
     {
-       b = FcBlanksCreate ();
+       freeme = b = FcBlanksCreate ();
        if (!b)
            return FcFalse;
     }
     if (!FcBlanksAdd (b, blank))
+    {
+        if (freeme)
+            FcBlanksDestroy (freeme);
        return FcFalse;
+    }
     config->blanks = b;
     return FcTrue;
 }
@@ -562,12 +643,12 @@ FcConfigPromote (FcValue v, FcValue u)
 }
 
 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);
@@ -629,9 +710,11 @@ FcConfigCompareValue (const FcValue        left_o,
                ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
                break;
            case FcOpNotEqual:
-           case FcOpNotContains:
                ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
                break;
+           case FcOpNotContains:
+               ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
+               break;
            default:
                break;
            }
@@ -741,6 +824,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
     FcValue    v, vl, vr;
     FcResult   r;
     FcMatrix   *m;
+    FcChar8     *str;
     
     switch (e->op) {
     case FcOpInteger:
@@ -753,8 +837,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        break;
     case FcOpString:
        v.type = FcTypeString;
-       v.u.s = e->u.sval;
-       v = FcValueSave (v);
+       v.u.s = FcStrStaticName(e->u.sval);
        break;
     case FcOpMatrix:
        v.type = FcTypeMatrix;
@@ -774,6 +857,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))
@@ -806,7 +890,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;  
@@ -871,7 +955,10 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                switch (e->op) {
                case FcOpPlus:
                    v.type = FcTypeString;
-                   v.u.s = FcStrPlus (vl.u.s, vr.u.s);
+                   str = FcStrPlus (vl.u.s, vr.u.s);
+                   v.u.s = FcStrStaticName (str);
+                   FcStrFree (str);
+                        
                    if (!v.u.s)
                        v.type = FcTypeVoid;
                    break;
@@ -1019,10 +1106,10 @@ FcConfigMatchValueList (FcPattern       *p,
            e = 0;
        }
 
-       for (v = values; v; v = v->next)
+       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;
@@ -1055,56 +1142,60 @@ FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
     if (e->op == FcOpComma)
     {
        l->value = FcConfigEvaluate (p, e->u.tree.left);
-       l->next  = FcConfigValues (p, e->u.tree.right, binding);
+       l->next  = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
     }
     else
     {
        l->value = FcConfigEvaluate (p, e);
-       l->next  = 0;
+       l->next  = FcValueListPtrCreateDynamic(0);
     }
     l->binding = binding;
-    while (l && l->value.type == FcTypeVoid)
+    if (l->value.type == FcTypeVoid)
     {
-       FcValueList     *next = l->next;
-       
+       FcValueList  *next = FcValueListPtrU(l->next);
+
        FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
        free (l);
        l = next;
     }
+
     return l;
 }
 
 static FcBool
-FcConfigAdd (FcValueList    **head,
+FcConfigAdd (FcValueListPtr *head,
             FcValueList    *position,
             FcBool         append,
             FcValueList    *new)
 {
-    FcValueList            **prev, *last, *v;
+    FcValueListPtr  *prev, last, v;
     FcValueBinding  sameBinding;
     
     if (position)
        sameBinding = position->binding;
     else
        sameBinding = FcValueBindingWeak;
-    for (v = new; v; v = v->next)
-       if (v->binding == FcValueBindingSame)
-           v->binding = sameBinding;
+    for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v); 
+        v = FcValueListPtrU(v)->next)
+       if (FcValueListPtrU(v)->binding == FcValueBindingSame)
+           FcValueListPtrU(v)->binding = sameBinding;
     if (append)
     {
        if (position)
            prev = &position->next;
        else
-           for (prev = head; *prev; prev = &(*prev)->next)
+           for (prev = head; FcValueListPtrU(*prev); 
+                prev = &(FcValueListPtrU(*prev)->next))
                ;
     }
     else
     {
        if (position)
        {
-           for (prev = head; *prev; prev = &(*prev)->next)
+           for (prev = head; FcValueListPtrU(*prev); 
+                prev = &(FcValueListPtrU(*prev)->next))
            {
-               if (*prev == position)
+               if (FcValueListPtrU(*prev) == position)
                    break;
            }
        }
@@ -1113,7 +1204,7 @@ FcConfigAdd (FcValueList    **head,
 
        if (FcDebug () & FC_DBG_EDIT)
        {
-           if (!*prev)
+           if (!FcValueListPtrU(*prev))
                printf ("position not on list\n");
        }
     }
@@ -1127,12 +1218,12 @@ FcConfigAdd (FcValueList    **head,
     
     if (new)
     {
-       last = new;
-       while (last->next)
-           last = last->next;
+       last = FcValueListPtrCreateDynamic(new);
+       while (FcValueListPtrU(FcValueListPtrU(last)->next))
+           last = FcValueListPtrU(last)->next;
     
-       last->next = *prev;
-       *prev = new;
+       FcValueListPtrU(last)->next = *prev;
+       *prev = FcValueListPtrCreateDynamic(new);
     }
     
     if (FcDebug () & FC_DBG_EDIT)
@@ -1146,18 +1237,19 @@ FcConfigAdd (FcValueList    **head,
 }
 
 static void
-FcConfigDel (FcValueList    **head,
+FcConfigDel (FcValueListPtr *head,
             FcValueList    *position)
 {
-    FcValueList    **prev;
+    FcValueListPtr *prev;
 
-    for (prev = head; *prev; prev = &(*prev)->next)
+    for (prev = head; FcValueListPtrU(*prev); 
+        prev = &(FcValueListPtrU(*prev)->next))
     {
-       if (*prev == position)
+       if (FcValueListPtrU(*prev) == position)
        {
            *prev = position->next;
-           position->next = 0;
-           FcValueListDestroy (position);
+           position->next = FcValueListPtrCreateDynamic(0);
+           FcValueListDestroy (FcValueListPtrCreateDynamic(position));
            break;
        }
     }
@@ -1189,8 +1281,8 @@ FcConfigPatternDel (FcPattern     *p,
     FcPatternElt    *e = FcPatternFindElt (p, object);
     if (!e)
        return;
-    while (e->values)
-       FcConfigDel (&e->values, e->values);
+    while (FcValueListPtrU(e->values))
+       FcConfigDel (&e->values, FcValueListPtrU(e->values));
 }
 
 static void
@@ -1200,7 +1292,7 @@ FcConfigPatternCanon (FcPattern       *p,
     FcPatternElt    *e = FcPatternFindElt (p, object);
     if (!e)
        return;
-    if (!e->values)
+    if (!FcValueListPtrU(e->values))
        FcPatternDel (p, object);
 }
 
@@ -1279,12 +1371,12 @@ FcConfigSubstituteWithPat (FcConfig    *config,
             * Check to see if there is a match, mark the location
             * to apply match-relative edits
             */
-           st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
+           st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
            if (!st[i].value)
                break;
-           if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
+           if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
                break;
-           if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
+           if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
                break;
        }
        if (t)
@@ -1337,7 +1429,7 @@ FcConfigSubstituteWithPat (FcConfig    *config,
                if (t)
                {
                    FcValueList *thisValue = st[i].value;
-                   FcValueList *nextValue = thisValue ? thisValue->next : 0;
+                   FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
                    
                    /*
                     * Append the new list of values after the current value
@@ -1346,7 +1438,8 @@ FcConfigSubstituteWithPat (FcConfig    *config,
                    /*
                     * Delete the marked value
                     */
-                   FcConfigDel (&st[i].elt->values, thisValue);
+                    if (thisValue)
+                       FcConfigDel (&st[i].elt->values, thisValue);
                    /*
                     * Adjust any pointers into the value list to ensure
                     * future edits occur at the same place
@@ -1401,6 +1494,7 @@ FcConfigSubstituteWithPat (FcConfig    *config,
                FcConfigPatternAdd (p, e->field, l, FcTrue);
                break;
            default:
+                FcValueListDestroy (FcValueListPtrCreateDynamic(l));
                break;
            }
        }
@@ -1503,7 +1597,9 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
 #ifdef _WIN32
     if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
                      path[strlen((char *) path)-1] != '\\')) &&
-        (file[0] != '/' && file[0] != '\\'))
+       !(file[0] == '/' ||
+         file[0] == '\\' ||
+         (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
        strcat ((char *) path, "\\");
 #else
     if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
@@ -1603,7 +1699,7 @@ FcConfigHome (void)
            home = getenv ("USERPROFILE");
 #endif
 
-       return home;
+       return (FcChar8 *) home;
     }
     return 0;
 }
@@ -1716,6 +1812,7 @@ FcConfigAppFontAddFile (FcConfig    *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }
 
@@ -1763,6 +1860,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }