]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Fix "contains" op for strings and langsets.
[fontconfig.git] / src / fccfg.c
index 446004f23530c01cf2c3456c02f4c2ae12cfae71..cbb6ecbc8281a22d77df19e77615ce7eddb10f0c 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
+ * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
  *
- * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2000 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
 
 #include "fcint.h"
 
+#if defined (_WIN32) && defined (PIC)
+#define STRICT
+#include <windows.h>
+#undef STRICT
+#endif
+
 FcConfig    *_fcConfig;
 
 FcConfig *
@@ -49,10 +55,19 @@ FcConfigCreate (void)
     if (!config->fontDirs)
        goto bail3;
     
-    config->cache = 0;
-    if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
+    config->acceptGlobs = FcStrSetCreate ();
+    if (!config->acceptGlobs)
        goto bail4;
 
+    config->rejectGlobs = FcStrSetCreate ();
+    if (!config->rejectGlobs)
+       goto bail5;
+
+    config->cache = 0;
+    if (FcConfigHome())
+       if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
+           goto bail6;
+
     config->blanks = 0;
 
     config->substPattern = 0;
@@ -66,6 +81,10 @@ FcConfigCreate (void)
     
     return config;
 
+bail6:
+    FcStrSetDestroy (config->rejectGlobs);
+bail5:
+    FcStrSetDestroy (config->acceptGlobs);
 bail4:
     FcStrSetDestroy (config->fontDirs);
 bail3:
@@ -133,8 +152,10 @@ FcSubstDestroy (FcSubst *s)
     while (s)
     {
        n = s->next;
-       FcTestDestroy (s->test);
-       FcEditDestroy (s->edit);
+       if (s->test)
+           FcTestDestroy (s->test);
+       if (s->edit)
+           FcEditDestroy (s->edit);
        s = n;
     }
 }
@@ -150,6 +171,8 @@ FcConfigDestroy (FcConfig *config)
     FcStrSetDestroy (config->configDirs);
     FcStrSetDestroy (config->fontDirs);
     FcStrSetDestroy (config->configFiles);
+    FcStrSetDestroy (config->acceptGlobs);
+    FcStrSetDestroy (config->rejectGlobs);
 
     FcStrFree (config->cache);
 
@@ -194,7 +217,8 @@ FcConfigBuildFonts (FcConfig *config)
     {
        if (FcDebug () & FC_DBG_FONTSET)
            printf ("scan dir %s\n", dir);
-       FcDirScan (fonts, config->fontDirs, cache, config->blanks, dir, FcFalse);
+       FcDirScanConfig (fonts, config->fontDirs, cache, 
+                        config->blanks, dir, FcFalse, config);
     }
     
     FcStrListDone (list);
@@ -483,11 +507,13 @@ FcConfigPromote (FcValue v, FcValue u)
 }
 
 FcBool
-FcConfigCompareValue (FcValue  m,
-                     FcOp      op,
-                     FcValue   v)
+FcConfigCompareValue (const FcValue    m_o,
+                     FcOp              op,
+                     const FcValue     v_o)
 {
-    FcBool    ret = FcFalse;
+    FcValue    m = m_o;
+    FcValue    v = v_o;
+    FcBool     ret = FcFalse;
     
     m = FcConfigPromote (m, v);
     v = FcConfigPromote (v, m);
@@ -539,13 +565,17 @@ FcConfigCompareValue (FcValue     m,
        case FcTypeString:
            switch (op) {
            case FcOpEqual:    
-           case FcOpContains:
                ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) == 0;
                break;
+           case FcOpContains:
+               ret = FcStrStrIgnoreCase (m.u.s, v.u.s) != 0;
+               break;
            case FcOpNotEqual:
-           case FcOpNotContains:
                ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) != 0;
                break;
+           case FcOpNotContains:
+               ret = FcStrStrIgnoreCase (m.u.s, v.u.s) == 0;
+               break;
            default:
                break;
            }
@@ -567,12 +597,12 @@ FcConfigCompareValue (FcValue     m,
        case FcTypeCharSet:
            switch (op) {
            case FcOpContains:
-               /* m contains v if v is a subset of m */
-               ret = FcCharSetIsSubset (v.u.c, m.u.c);
+               /* v contains m if m is a subset of v */
+               ret = FcCharSetIsSubset (m.u.c, v.u.c);
                break;
            case FcOpNotContains:
-               /* m contains v if v is a subset of m */
-               ret = !FcCharSetIsSubset (v.u.c, m.u.c);
+               /* v contains m if m is a subset of v */
+               ret = !FcCharSetIsSubset (m.u.c, v.u.c);
                break;
            case FcOpEqual:
                ret = FcCharSetEqual (m.u.c, v.u.c);
@@ -587,16 +617,16 @@ FcConfigCompareValue (FcValue     m,
        case FcTypeLangSet:
            switch (op) {
            case FcOpContains:
-               ret = FcLangSetCompare (v.u.l, m.u.l) != FcLangDifferentLang;
+               ret = FcLangSetContains (m.u.l, v.u.l);
                break;
            case FcOpNotContains:
-               ret = FcLangSetCompare (v.u.l, m.u.l) == FcLangDifferentLang;
+               ret = FcLangSetContains (m.u.l, v.u.l);
                break;
            case FcOpEqual:
-               ret = FcLangSetEqual (v.u.l, m.u.l);
+               ret = FcLangSetEqual (m.u.l, v.u.l);
                break;
            case FcOpNotEqual:
-               ret = !FcLangSetEqual (v.u.l, m.u.l);
+               ret = !FcLangSetEqual (m.u.l, v.u.l);
                break;
            default:
                break;
@@ -637,6 +667,13 @@ FcConfigCompareValue (FcValue      m,
 }
 
 
+#define _FcDoubleFloor(d)      ((int) (d))
+#define _FcDoubleCeil(d)       ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
+#define FcDoubleFloor(d)       ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
+#define FcDoubleCeil(d)                ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
+#define FcDoubleRound(d)       FcDoubleFloor ((d) + 0.5)
+#define FcDoubleTrunc(d)       ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
+
 static FcValue
 FcConfigEvaluate (FcPattern *p, FcExpr *e)
 {
@@ -825,6 +862,70 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        }
        FcValueDestroy (vl);
        break;
+    case FcOpFloor:
+       vl = FcConfigEvaluate (p, e->u.tree.left);
+       switch (vl.type) {
+       case FcTypeInteger:
+           v = vl;
+           break;
+       case FcTypeDouble:
+           v.type = FcTypeInteger;
+           v.u.i = FcDoubleFloor (vl.u.d);
+           break;
+       default:
+           v.type = FcTypeVoid;
+           break;
+       }
+       FcValueDestroy (vl);
+       break;
+    case FcOpCeil:
+       vl = FcConfigEvaluate (p, e->u.tree.left);
+       switch (vl.type) {
+       case FcTypeInteger:
+           v = vl;
+           break;
+       case FcTypeDouble:
+           v.type = FcTypeInteger;
+           v.u.i = FcDoubleCeil (vl.u.d);
+           break;
+       default:
+           v.type = FcTypeVoid;
+           break;
+       }
+       FcValueDestroy (vl);
+       break;
+    case FcOpRound:
+       vl = FcConfigEvaluate (p, e->u.tree.left);
+       switch (vl.type) {
+       case FcTypeInteger:
+           v = vl;
+           break;
+       case FcTypeDouble:
+           v.type = FcTypeInteger;
+           v.u.i = FcDoubleRound (vl.u.d);
+           break;
+       default:
+           v.type = FcTypeVoid;
+           break;
+       }
+       FcValueDestroy (vl);
+       break;
+    case FcOpTrunc:
+       vl = FcConfigEvaluate (p, e->u.tree.left);
+       switch (vl.type) {
+       case FcTypeInteger:
+           v = vl;
+           break;
+       case FcTypeDouble:
+           v.type = FcTypeInteger;
+           v.u.i = FcDoubleTrunc (vl.u.d);
+           break;
+       default:
+           v.type = FcTypeVoid;
+           break;
+       }
+       FcValueDestroy (vl);
+       break;
     default:
        v.type = FcTypeVoid;
        break;
@@ -915,8 +1016,16 @@ FcConfigAdd (FcValueList    **head,
             FcBool         append,
             FcValueList    *new)
 {
-    FcValueList    **prev, *last;
+    FcValueList            **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;
     if (append)
     {
        if (position)
@@ -1142,6 +1251,14 @@ FcConfigSubstituteWithPat (FcConfig    *config,
                    !FcStrCmpIgnoreCase ((FcChar8 *) t->field, 
                                         (FcChar8 *) e->field))
                {
+                   /* 
+                    * KLUDGE - the pattern may have been reallocated or
+                    * things may have been inserted or deleted above
+                    * this element by other edits.  Go back and find
+                    * the element again
+                    */
+                   if (e != s->edit && st[i].elt)
+                       st[i].elt = FcPatternFindElt (p, t->field);
                    if (!st[i].elt)
                        t = 0;
                    break;
@@ -1254,9 +1371,53 @@ FcConfigSubstitute (FcConfig     *config,
     return FcConfigSubstituteWithPat (config, p, 0, kind);
 }
 
-#ifndef FONTCONFIG_PATH
-#define FONTCONFIG_PATH        "/etc/fonts"
-#endif
+#if defined (_WIN32) && defined (PIC)
+
+static FcChar8 fontconfig_path[1000] = "";
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+        DWORD     fdwReason,
+        LPVOID    lpvReserved)
+{
+  FcChar8 *p;
+
+  switch (fdwReason) {
+  case DLL_PROCESS_ATTACH:
+      if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
+                             sizeof (fontconfig_path)))
+         break;
+
+      /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
+       * assume it's a Unix-style installation tree, and use
+       * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
+       * folder where the DLL is as FONTCONFIG_PATH.
+       */
+      p = strrchr (fontconfig_path, '\\');
+      if (p)
+      {
+         *p = '\0';
+         p = strrchr (fontconfig_path, '\\');
+         if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
+                   FcStrCmpIgnoreCase (p + 1, "lib") == 0))
+             *p = '\0';
+         strcat (fontconfig_path, "\\etc\\fonts");
+      }
+      else
+          fontconfig_path[0] = '\0';
+      
+      break;
+  }
+
+  return TRUE;
+}
+
+#undef FONTCONFIG_PATH
+#define FONTCONFIG_PATH fontconfig_path
+
+#else /* !(_WIN32 && PIC) */
+
+#endif /* !(_WIN32 && PIC) */
 
 #ifndef FONTCONFIG_FILE
 #define FONTCONFIG_FILE        "fonts.conf"
@@ -1274,9 +1435,16 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
        return 0;
 
     strcpy ((char *) path, (const char *) dir);
-    /* make sure there's a single separating / */
+    /* make sure there's a single separator */
+#ifdef _WIN32
+    if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
+                     path[strlen((char *) path)-1] != '\\')) &&
+        (file[0] != '/' && file[0] != '\\'))
+       strcat ((char *) path, "\\");
+#else
     if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
        strcat ((char *) path, "/");
+#endif
     strcat ((char *) path, (char *) file);
 
     FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
@@ -1303,7 +1471,7 @@ FcConfigGetPath (void)
        e = env;
        npath++;
        while (*e)
-           if (*e++ == ':')
+           if (*e++ == FC_SEARCH_PATH_SEPARATOR)
                npath++;
     }
     path = calloc (npath, sizeof (FcChar8 *));
@@ -1316,7 +1484,7 @@ FcConfigGetPath (void)
        e = env;
        while (*e) 
        {
-           colon = (FcChar8 *) strchr ((char *) e, ':');
+           colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
            if (!colon)
                colon = e + strlen ((char *) e);
            path[i] = malloc (colon - e + 1);
@@ -1357,6 +1525,33 @@ FcConfigFreePath (FcChar8 **path)
     free (path);
 }
 
+static FcBool  _FcConfigHomeEnabled = FcTrue;
+
+FcChar8 *
+FcConfigHome (void)
+{
+    if (_FcConfigHomeEnabled)
+    {
+        char *home = getenv ("HOME");
+
+#ifdef _WIN32
+       if (home == NULL)
+           home = getenv ("USERPROFILE");
+#endif
+
+       return home;
+    }
+    return 0;
+}
+
+FcBool
+FcConfigEnableHome (FcBool enable)
+{
+    FcBool  prev = _FcConfigHomeEnabled;
+    _FcConfigHomeEnabled = enable;
+    return prev;
+}
+
 FcChar8 *
 FcConfigFilename (const FcChar8 *url)
 {
@@ -1369,14 +1564,26 @@ FcConfigFilename (const FcChar8 *url)
            url = (FcChar8 *) FONTCONFIG_FILE;
     }
     file = 0;
+
+#ifdef _WIN32
+    if (isalpha (*url) &&
+       url[1] == ':' &&
+       (url[2] == '/' || url[2] == '\\'))
+       goto absolute_path;
+#endif
+
     switch (*url) {
     case '~':
-       dir = (FcChar8 *) getenv ("HOME");
+       dir = FcConfigHome ();
        if (dir)
            file = FcConfigFileExists (dir, url + 1);
        else
            file = 0;
        break;
+#ifdef _WIN32
+    case '\\':
+    absolute_path:
+#endif
     case '/':
        file = FcConfigFileExists (0, url);
        break;
@@ -1432,7 +1639,7 @@ FcConfigAppFontAddFile (FcConfig    *config,
        FcConfigSetFonts (config, set, FcSetApplication);
     }
        
-    if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
+    if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
     {
        FcStrSetDestroy (subdirs);
        return FcFalse;
@@ -1479,7 +1686,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
        FcConfigSetFonts (config, set, FcSetApplication);
     }
     
-    if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
+    if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
     {
        FcStrSetDestroy (subdirs);
        return FcFalse;
@@ -1500,3 +1707,76 @@ FcConfigAppFontClear (FcConfig       *config)
 {
     FcConfigSetFonts (config, 0, FcSetApplication);
 }
+
+/*
+ * Manage filename-based font source selectors
+ */
+
+FcBool
+FcConfigGlobAdd (FcConfig      *config,
+                const FcChar8  *glob,
+                FcBool         accept)
+{
+    FcStrSet   *set = accept ? config->acceptGlobs : config->rejectGlobs;
+
+    return FcStrSetAdd (set, glob);
+}
+
+static FcBool
+FcConfigGlobMatch (const FcChar8    *glob,
+                  const FcChar8    *string)
+{
+    FcChar8    c;
+
+    while ((c = *glob++)) 
+    {
+       switch (c) {
+       case '*':
+           /* short circuit common case */
+           if (!*glob)
+               return FcTrue;
+           /* short circuit another common case */
+           if (strchr ((char *) glob, '*') == 0)
+               string += strlen ((char *) string) - strlen ((char *) glob);
+           while (*string)
+           {
+               if (FcConfigGlobMatch (glob, string))
+                   return FcTrue;
+               string++;
+           }
+           return FcFalse;
+       case '?':
+           if (*string++ == '\0')
+               return FcFalse;
+           break;
+       default:
+           if (*string++ != c)
+               return FcFalse;
+           break;
+       }
+    }
+    return *string == '\0';
+}
+
+static FcBool
+FcConfigGlobsMatch (const FcStrSet     *globs,
+                   const FcChar8       *string)
+{
+    int        i;
+
+    for (i = 0; i < globs->num; i++)
+       if (FcConfigGlobMatch (globs->strs[i], string))
+           return FcTrue;
+    return FcFalse;
+}
+
+FcBool
+FcConfigAcceptFilename (FcConfig       *config,
+                       const FcChar8   *filename)
+{
+    if (FcConfigGlobsMatch (config->acceptGlobs, filename))
+       return FcTrue;
+    if (FcConfigGlobsMatch (config->rejectGlobs, filename))
+       return FcFalse;
+    return FcTrue;
+}