]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
2006-08-04 Keith Packard (keithp@keithp.com) reviewed by: plam
[fontconfig.git] / src / fccfg.c
index 00510e5a66d0de5cba6c6cbd18ffdbc74d234a91..8eb82e5a3cac0f7611f4b5ca009ea0a9e834d00e 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:
@@ -224,6 +232,7 @@ FcConfigDestroy (FcConfig *config)
 
     FcStrSetDestroy (config->configDirs);
     FcStrSetDestroy (config->fontDirs);
+    FcStrSetDestroy (config->cacheDirs);
     FcStrSetDestroy (config->configFiles);
     FcStrSetDestroy (config->acceptGlobs);
     FcStrSetDestroy (config->rejectGlobs);
@@ -281,7 +290,7 @@ FcConfigBuildFonts (FcConfig *config)
     {
        list = FcConfigGetFontDirs (config);
        if (!list)
-           goto bail2;
+           goto bail3;
        
        while ((dir = FcStrListNext (list)))
        {
@@ -308,13 +317,11 @@ FcConfigBuildFonts (FcConfig *config)
 
        for (i = 0; i < cached_fonts->nfont; i++)
        {
-           FcChar8     *cfn, *cfd
+           FcChar8     *cfn; 
            FcPatternGetString (cached_fonts->fonts[i], FC_FILE, 0, &cfn);
-           cfd = (FcChar8 *)FcCacheFindBankDir (cached_fonts->fonts[i]->bank);
 
            if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
-                (cfn && FcConfigAcceptFilename (config, cfn)) &&
-               (cfd && FcConfigAcceptFilename (config, cfd)))
+                (cfn && FcConfigAcceptFilename (config, cfn)))
                FcFontSetAdd (fonts, cached_fonts->fonts[i]);
 
            cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
@@ -327,15 +334,17 @@ FcConfigBuildFonts (FcConfig *config)
        FcFontSetPrint (fonts);
 
     if (config->cache)
-       FcGlobalCacheSave (cache, config->cache);
+       FcGlobalCacheSave (cache, config->cache, config);
     FcGlobalCacheDestroy (cache);
     FcStrSetDestroy (oldDirs);
 
     FcConfigSetFonts (config, fonts, FcSetSystem);
     
     return FcTrue;
-bail2:
+bail3:
     FcStrSetDestroy (oldDirs);
+bail2:
+    FcGlobalCacheDestroy (cache);
 bail1:
     FcFontSetDestroy (fonts);
 bail0:
@@ -383,13 +392,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)
@@ -410,6 +519,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)
@@ -504,17 +632,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;
 }
@@ -792,6 +924,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
     FcValue    v, vl, vr;
     FcResult   r;
     FcMatrix   *m;
+    FcChar8     *str;
     
     switch (e->op) {
     case FcOpInteger:
@@ -805,7 +938,6 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
     case FcOpString:
        v.type = FcTypeString;
        v.u.s = FcStrStaticName(e->u.sval);
-       v = FcValueSave (v);
        break;
     case FcOpMatrix:
        v.type = FcTypeMatrix;
@@ -825,6 +957,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))
@@ -922,7 +1055,9 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                switch (e->op) {
                case FcOpPlus:
                    v.type = FcTypeString;
-                   v.u.s = FcStrStaticName (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;
@@ -1403,7 +1538,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
@@ -1458,6 +1594,7 @@ FcConfigSubstituteWithPat (FcConfig    *config,
                FcConfigPatternAdd (p, e->field, l, FcTrue);
                break;
            default:
+                FcValueListDestroy (FcValueListPtrCreateDynamic(l));
                break;
            }
        }
@@ -1775,6 +1912,7 @@ FcConfigAppFontAddFile (FcConfig    *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }
 
@@ -1822,6 +1960,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }