]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Replace 'KEITH PACKARD' with 'THE AUTHOR(S)' in license text in all files
[fontconfig.git] / src / fccfg.c
index 2f0d3111b58702b0adfe4a6344d66f4fef4fb6e5..da13f8c6b15a1b6ef14fff6c3314ae9047994fcb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
+ * fontconfig/src/fccfg.c
  *
  * Copyright © 2000 Keith Packard
  *
@@ -13,9 +13,9 @@
  * representations about the suitability of this software for any purpose.  It
  * is provided "as is" without express or implied warranty.
  *
- * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
@@ -90,10 +90,10 @@ FcConfigCreate (void)
     for (set = FcSetSystem; set <= FcSetApplication; set++)
        config->fonts[set] = 0;
 
-    config->caches = NULL;
-
     config->rescanTime = time(0);
     config->rescanInterval = 30;    
+
+    config->ref = 1;
     
     return config;
 
@@ -129,7 +129,7 @@ FcConfigNewestFile (FcStrSet *files)
     if (list)
     {
        while ((file = FcStrListNext (list)))
-           if (stat ((char *) file, &statb) == 0)
+           if (FcStat ((char *) file, &statb) == 0)
                if (!newest.set || statb.st_mtime - newest.time > 0)
                {
                    newest.set = FcTrue;
@@ -140,23 +140,10 @@ 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)
 {
-    FcFileTime config_time, font_time;
+    FcFileTime config_time, config_dir_time, font_time;
     time_t     now = time(0);
     if (!config)
     {
@@ -165,11 +152,24 @@ FcConfigUptoDate (FcConfig *config)
            return FcFalse;
     }
     config_time = FcConfigNewestFile (config->configFiles);
+    config_dir_time = FcConfigNewestFile (config->configDirs);
     font_time = FcConfigNewestFile (config->fontDirs);
     if ((config_time.set && config_time.time - config->rescanTime > 0) ||
+       (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
        (font_time.set && (font_time.time - config->rescanTime) > 0))
     {
-       return FcFalse;
+       /* We need to check for potential clock problems here (OLPC ticket #6046) */
+       if ((config_time.set && (config_time.time - now) > 0) ||
+       (config_dir_time.set && (config_dir_time.time - now) > 0) ||
+        (font_time.set && (font_time.time - now) > 0))
+       {
+           fprintf (stderr,
+                    "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
+           config->rescanTime = now;
+           return FcTrue;
+       }
+       else
+           return FcFalse;
     }
     config->rescanTime = now;
     return FcTrue;
@@ -193,11 +193,28 @@ FcSubstDestroy (FcSubst *s)
     }
 }
 
+FcConfig *
+FcConfigReference (FcConfig *config)
+{
+    if (!config)
+    {
+       config = FcConfigGetCurrent ();
+       if (!config)
+           return 0;
+    }
+
+    config->ref++;
+
+    return config;
+}
+
 void
 FcConfigDestroy (FcConfig *config)
 {
     FcSetName  set;
-    FcCacheList        *cl, *cl_next;
+
+    if (--config->ref > 0)
+       return;
 
     if (config == _fcConfig)
        _fcConfig = 0;
@@ -221,13 +238,6 @@ FcConfigDestroy (FcConfig *config)
        if (config->fonts[set])
            FcFontSetDestroy (config->fonts[set]);
 
-    for (cl = config->caches; cl; cl = cl_next)
-    {
-       cl_next = cl->next;
-       FcDirCacheUnload (cl->cache);
-       free (cl);
-    }
-
     free (config);
     FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
 }
@@ -237,28 +247,21 @@ FcConfigDestroy (FcConfig *config)
  */
 
 FcBool
-FcConfigAddCache (FcConfig *config, FcCache *cache)
+FcConfigAddCache (FcConfig *config, FcCache *cache, 
+                 FcSetName set, FcStrSet *dirSet)
 {
-    FcCacheList        *cl = malloc (sizeof (FcCacheList));
     FcFontSet  *fs;
     intptr_t   *dirs;
     int                i;
 
-    /*
-     * Add to cache list
-     */
-    if (!cl)
-       return FcFalse;
-    cl->cache = cache;
-    cl->next = config->caches;
-    config->caches = cl;
-
     /*
      * Add fonts
      */
     fs = FcCacheSet (cache);
     if (fs)
     {
+       int     nref = 0;
+       
        for (i = 0; i < fs->nfont; i++)
        {
            FcPattern   *font = FcFontSetFont (fs, i);
@@ -280,8 +283,10 @@ FcConfigAddCache (FcConfig *config, FcCache *cache)
            if (!FcConfigAcceptFont (config, font))
                continue;
                
-           FcFontSetAdd (config->fonts[FcSetSystem], font);
+           nref++;
+           FcFontSetAdd (config->fonts[set], font);
        }
+       FcDirCacheReference (cache, nref);
     }
 
     /*
@@ -294,12 +299,37 @@ FcConfigAddCache (FcConfig *config, FcCache *cache)
        {
            FcChar8     *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
            if (FcConfigAcceptFilename (config, dir))
-               FcConfigAddFontDir (config, dir);
+               FcStrSetAddFilename (dirSet, dir);
        }
     }
     return FcTrue;
 }
 
+static FcBool
+FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
+{
+    FcStrList      *dirlist;
+    FcChar8        *dir;
+    FcCache        *cache;
+    
+    dirlist = FcStrListCreate (dirSet);
+    if (!dirlist)
+        return FcFalse;
+       
+    while ((dir = FcStrListNext (dirlist)))
+    {
+       if (FcDebug () & FC_DBG_FONTSET)
+           printf ("adding fonts from%s\n", dir);
+       cache = FcDirCacheRead (dir, FcFalse, config);
+       if (!cache)
+           continue;
+       FcConfigAddCache (config, cache, set, dirSet);
+       FcDirCacheUnload (cache);
+    }
+    FcStrListDone (dirlist);
+    return FcTrue;
+}
+
 /*
  * Scan the current list of directories in the configuration
  * and build the set of available fonts.
@@ -309,9 +339,6 @@ FcBool
 FcConfigBuildFonts (FcConfig *config)
 {
     FcFontSet      *fonts;
-    FcStrList      *dirlist;
-    FcChar8        *dir;
-    FcCache        *cache;
 
     if (!config)
     {
@@ -322,37 +349,23 @@ FcConfigBuildFonts (FcConfig *config)
        
     fonts = FcFontSetCreate ();
     if (!fonts)
-       goto bail;
+       return FcFalse;
     
     FcConfigSetFonts (config, fonts, FcSetSystem);
     
-    dirlist = FcStrListCreate (config->fontDirs);
-    if (!dirlist)
-        goto bail;
-       
-    while ((dir = FcStrListNext (dirlist)))
-    {
-       if (FcDebug () & FC_DBG_FONTSET)
-           printf ("adding fonts from%s\n", dir);
-       cache = FcDirCacheRead (dir, FcFalse, config);
-       if (!cache)
-           continue;
-       FcConfigAddCache (config, cache);
-    }
-    
-    FcStrListDone (dirlist);
-    
+    if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
+       return FcFalse;
     if (FcDebug () & FC_DBG_FONTSET)
        FcFontSetPrint (fonts);
-
     return FcTrue;
-bail:
-    return FcFalse;
 }
 
 FcBool
 FcConfigSetCurrent (FcConfig *config)
 {
+    if (config == _fcConfig)
+       return FcTrue;
+
     if (!config->fonts)
        if (!FcConfigBuildFonts (config))
            return FcFalse;
@@ -529,7 +542,7 @@ FcConfigAddBlank (FcConfig  *config,
 }
 
 int
-FcConfigGetRescanInverval (FcConfig *config)
+FcConfigGetRescanInterval (FcConfig *config)
 {
     if (!config)
     {
@@ -541,7 +554,7 @@ FcConfigGetRescanInverval (FcConfig *config)
 }
 
 FcBool
-FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
+FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
 {
     if (!config)
     {
@@ -553,6 +566,22 @@ FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
     return FcTrue;
 }
 
+/*
+ * A couple of typos escaped into the library
+ */
+int
+FcConfigGetRescanInverval (FcConfig *config)
+{
+    return FcConfigGetRescanInterval (config);
+}
+
+FcBool
+FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
+{
+    return FcConfigSetRescanInterval (config, rescanInterval);
+}
+
+    
 FcBool
 FcConfigAddEdit (FcConfig      *config,
                 FcTest         *test,
@@ -699,7 +728,7 @@ FcConfigCompareValue (const FcValue *left_o,
                ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
                break;
            case FcOpNotContains:
-               ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
+               ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
                break;
            default:
                break;
@@ -1522,10 +1551,16 @@ FcConfigSubstitute (FcConfig    *config,
     return FcConfigSubstituteWithPat (config, p, 0, kind);
 }
 
-#if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
+#if defined (_WIN32)
+
+#  define WIN32_LEAN_AND_MEAN
+#  define WIN32_EXTRA_LEAN
+#  include <windows.h>
 
 static FcChar8 fontconfig_path[1000] = "";
 
+#  if (defined (PIC) || defined (DLL_EXPORT))
+
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,
         DWORD     fdwReason,
@@ -1563,12 +1598,12 @@ DllMain (HINSTANCE hinstDLL,
   return TRUE;
 }
 
+#  endif /* !PIC */
+
 #undef FONTCONFIG_PATH
 #define FONTCONFIG_PATH fontconfig_path
 
-#else /* !(_WIN32 && PIC) */
-
-#endif /* !(_WIN32 && PIC) */
+#endif /* !_WIN32 */
 
 #ifndef FONTCONFIG_FILE
 #define FONTCONFIG_FILE        "fonts.conf"
@@ -1653,6 +1688,17 @@ FcConfigGetPath (void)
        }
     }
     
+#ifdef _WIN32
+       if (fontconfig_path[0] == '\0')
+       {
+               char *p;
+               if(!GetModuleFileName(NULL, fontconfig_path, sizeof(fontconfig_path)))
+                       goto bail1;
+               p = strrchr (fontconfig_path, '\\');
+               if (p) *p = '\0';
+               strcat (fontconfig_path, "\\fonts");
+       }
+#endif
     dir = (FcChar8 *) FONTCONFIG_PATH;
     path[i] = malloc (strlen ((char *) dir) + 1);
     if (!path[i])
@@ -1709,7 +1755,7 @@ FcChar8 *
 FcConfigFilename (const FcChar8 *url)
 {
     FcChar8    *file, *dir, **path, **p;
-    
+
     if (!url || !*url)
     {
        url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
@@ -1814,9 +1860,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
                       const FcChar8   *dir)
 {
     FcFontSet  *set;
-    FcStrSet   *subdirs;
-    FcStrList  *sublist;
-    FcChar8    *subdir;
+    FcStrSet   *dirs;
     
     if (!config)
     {
@@ -1824,8 +1868,9 @@ FcConfigAppFontAddDir (FcConfig       *config,
        if (!config)
            return FcFalse;
     }
-    subdirs = FcStrSetCreate ();
-    if (!subdirs)
+
+    dirs = FcStrSetCreate ();
+    if (!dirs)
        return FcFalse;
     
     set = FcConfigGetFonts (config, FcSetApplication);
@@ -1834,26 +1879,20 @@ FcConfigAppFontAddDir (FcConfig     *config,
        set = FcFontSetCreate ();
        if (!set)
        {
-           FcStrSetDestroy (subdirs);
+           FcStrSetDestroy (dirs);
            return FcFalse;
        }
        FcConfigSetFonts (config, set, FcSetApplication);
     }
     
-    if (!FcDirScanConfig (set, subdirs, config->blanks, dir, FcFalse, config))
+    FcStrSetAddFilename (dirs, dir);
+    
+    if (!FcConfigAddDirList (config, FcSetApplication, dirs))
     {
-       FcStrSetDestroy (subdirs);
+       FcStrSetDestroy (dirs);
        return FcFalse;
     }
-    if ((sublist = FcStrListCreate (subdirs)))
-    {
-       while ((subdir = FcStrListNext (sublist)))
-       {
-           FcConfigAppFontAddDir (config, subdir);
-       }
-       FcStrListDone (sublist);
-    }
-    FcStrSetDestroy (subdirs);
+    FcStrSetDestroy (dirs);
     return FcTrue;
 }
 
@@ -1979,3 +2018,6 @@ FcConfigAcceptFont (FcConfig          *config,
        return FcFalse;
     return FcTrue;
 }
+#define __fccfg__
+#include "fcaliastail.h"
+#undef __fccfg__