]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
[xml] Allocate FcExpr's in a pool in FcConfig
[fontconfig.git] / src / fccfg.c
index 01d381c5f33ea118ad42adf3c8c4cc3b937065e8..0f89b57a415f61cca22418e8cdcc52fd82c7bccd 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
@@ -92,6 +92,10 @@ FcConfigCreate (void)
 
     config->rescanTime = time(0);
     config->rescanInterval = 30;    
+
+    config->expr_pool = NULL;
+
+    config->ref = 1;
     
     return config;
 
@@ -127,7 +131,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;
@@ -138,23 +142,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)
     {
@@ -163,11 +154,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;
@@ -191,10 +195,49 @@ FcSubstDestroy (FcSubst *s)
     }
 }
 
+FcExpr *
+FcConfigAllocExpr (FcConfig *config)
+{
+  if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
+  {
+    FcExprPage *new_page;
+
+    new_page = malloc (sizeof (FcExprPage));
+    if (!new_page)
+      return 0;
+    FcMemAlloc (FC_MEM_EXPR, sizeof (FcExprPage));
+
+    new_page->next_page = config->expr_pool;
+    new_page->next = new_page->exprs;
+    config->expr_pool = new_page;
+  }
+
+  return config->expr_pool->next++;
+}
+
+FcConfig *
+FcConfigReference (FcConfig *config)
+{
+    if (!config)
+    {
+       config = FcConfigGetCurrent ();
+       if (!config)
+           return 0;
+    }
+
+    config->ref++;
+
+    return config;
+}
+
 void
 FcConfigDestroy (FcConfig *config)
 {
     FcSetName  set;
+    FcExprPage *page;
+
+    if (--config->ref > 0)
+       return;
 
     if (config == _fcConfig)
        _fcConfig = 0;
@@ -218,6 +261,15 @@ FcConfigDestroy (FcConfig *config)
        if (config->fonts[set])
            FcFontSetDestroy (config->fonts[set]);
 
+    page = config->expr_pool;
+    while (page)
+    {
+      FcExprPage *next = page->next_page;
+      FcMemFree (FC_MEM_EXPR, sizeof (FcExprPage));
+      free (page);
+      page = next;
+    }
+
     free (config);
     FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
 }
@@ -227,7 +279,8 @@ FcConfigDestroy (FcConfig *config)
  */
 
 FcBool
-FcConfigAddCache (FcConfig *config, FcCache *cache)
+FcConfigAddCache (FcConfig *config, FcCache *cache, 
+                 FcSetName set, FcStrSet *dirSet)
 {
     FcFontSet  *fs;
     intptr_t   *dirs;
@@ -263,7 +316,7 @@ FcConfigAddCache (FcConfig *config, FcCache *cache)
                continue;
                
            nref++;
-           FcFontSetAdd (config->fonts[FcSetSystem], font);
+           FcFontSetAdd (config->fonts[set], font);
        }
        FcDirCacheReference (cache, nref);
     }
@@ -278,12 +331,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.
@@ -293,9 +371,6 @@ FcBool
 FcConfigBuildFonts (FcConfig *config)
 {
     FcFontSet      *fonts;
-    FcStrList      *dirlist;
-    FcChar8        *dir;
-    FcCache        *cache;
 
     if (!config)
     {
@@ -306,38 +381,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);
-       FcDirCacheUnload (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;
@@ -514,7 +574,7 @@ FcConfigAddBlank (FcConfig  *config,
 }
 
 int
-FcConfigGetRescanInverval (FcConfig *config)
+FcConfigGetRescanInterval (FcConfig *config)
 {
     if (!config)
     {
@@ -526,7 +586,7 @@ FcConfigGetRescanInverval (FcConfig *config)
 }
 
 FcBool
-FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
+FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
 {
     if (!config)
     {
@@ -538,6 +598,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,
@@ -684,7 +760,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;
@@ -808,7 +884,8 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        break;
     case FcOpString:
        v.type = FcTypeString;
-       v.u.s = FcStrStaticName(e->u.sval);
+       v.u.s = e->u.sval;
+       v = FcValueSave (v);
        break;
     case FcOpMatrix:
        v.type = FcTypeMatrix;
@@ -1507,10 +1584,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,
@@ -1548,12 +1631,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"
@@ -1638,6 +1721,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])
@@ -1694,7 +1788,7 @@ FcChar8 *
 FcConfigFilename (const FcChar8 *url)
 {
     FcChar8    *file, *dir, **path, **p;
-    
+
     if (!url || !*url)
     {
        url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
@@ -1799,9 +1893,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
                       const FcChar8   *dir)
 {
     FcFontSet  *set;
-    FcStrSet   *subdirs;
-    FcStrList  *sublist;
-    FcChar8    *subdir;
+    FcStrSet   *dirs;
     
     if (!config)
     {
@@ -1809,8 +1901,9 @@ FcConfigAppFontAddDir (FcConfig       *config,
        if (!config)
            return FcFalse;
     }
-    subdirs = FcStrSetCreate ();
-    if (!subdirs)
+
+    dirs = FcStrSetCreate ();
+    if (!dirs)
        return FcFalse;
     
     set = FcConfigGetFonts (config, FcSetApplication);
@@ -1819,26 +1912,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;
 }