]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Revert ill-advised addition of FC_RENDER. Add strategy for handling objects
[fontconfig.git] / src / fccfg.c
index f393c79bf73fd6bdbabf51b11ed278e2195ae990..5d6339f65af9102b828000ece797127dffa5bb2d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
  *
- * Copyright © 2000 Keith Packard
+ * 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
@@ -67,10 +67,43 @@ FcConfigCreate (void)
     if (!config->rejectGlobs)
        goto bail5;
 
+    config->acceptPatterns = FcFontSetCreate ();
+    if (!config->acceptPatterns)
+       goto bail6;
+    
+    config->rejectPatterns = FcFontSetCreate ();
+    if (!config->rejectPatterns)
+       goto bail7;
+
     config->cache = 0;
     if (FcConfigHome())
        if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
-           goto bail6;
+           goto bail8;
+
+#ifdef _WIN32
+    if (config->cache == 0)
+    {
+       /* If no home, use the temp folder. */
+       FcChar8     dummy[1];
+       int         templen = GetTempPath (1, dummy);
+       FcChar8     *temp = malloc (templen + 1);
+
+       if (temp)
+       {
+           FcChar8 *cache_dir;
+
+           GetTempPath (templen + 1, temp);
+           cache_dir = FcStrPlus (temp, FC_USER_CACHE_FILE);
+           free (temp);
+           if (!FcConfigSetCache (config, cache_dir))
+           {
+               FcStrFree (cache_dir);
+               goto bail6;
+           }
+           FcStrFree (cache_dir);
+       }
+    }
+#endif
 
     config->blanks = 0;
 
@@ -85,6 +118,10 @@ FcConfigCreate (void)
     
     return config;
 
+bail8:
+    FcFontSetDestroy (config->rejectPatterns);
+bail7:
+    FcFontSetDestroy (config->acceptPatterns);
 bail6:
     FcStrSetDestroy (config->rejectGlobs);
 bail5:
@@ -120,7 +157,10 @@ FcConfigNewestFile (FcStrSet *files)
        while ((file = FcStrListNext (list)))
            if (stat ((char *) file, &statb) == 0)
                if (!newest.set || statb.st_mtime - newest.time > 0)
+               {
+                   newest.set = FcTrue;
                    newest.time = statb.st_mtime;
+               }
        FcStrListDone (list);
     }
     return newest;
@@ -138,9 +178,9 @@ FcConfigUptoDate (FcConfig *config)
            return FcFalse;
     }
     config_time = FcConfigNewestFile (config->configFiles);
-    font_time = FcConfigNewestFile (config->configDirs);
+    font_time = FcConfigNewestFile (config->fontDirs);
     if ((config_time.set && config_time.time - config->rescanTime > 0) ||
-       (font_time.set && font_time.time - config->rescanTime) > 0)
+       (font_time.set && (font_time.time - config->rescanTime) > 0))
     {
        return FcFalse;
     }
@@ -179,6 +219,8 @@ FcConfigDestroy (FcConfig *config)
     FcStrSetDestroy (config->configFiles);
     FcStrSetDestroy (config->acceptGlobs);
     FcStrSetDestroy (config->rejectGlobs);
+    FcFontSetDestroy (config->acceptPatterns);
+    FcFontSetDestroy (config->rejectPatterns);
 
     if (config->blanks)
        FcBlanksDestroy (config->blanks);
@@ -205,9 +247,10 @@ FcConfigDestroy (FcConfig *config)
 FcBool
 FcConfigBuildFonts (FcConfig *config)
 {
-    FcFontSet      *fonts;
+    FcFontSet      *fonts, *cached_fonts;
     FcGlobalCache   *cache;
     FcStrList      *list;
+    FcStrSet       *oldDirs;
     FcChar8        *dir;
 
     fonts = FcFontSetCreate ();
@@ -218,22 +261,53 @@ 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);
 
-    while ((dir = FcStrListNext (list)))
+    cached_fonts = FcCacheRead(config, cache);
+    if (!cached_fonts)
     {
-       if (FcDebug () & FC_DBG_FONTSET)
-           printf ("scan dir %s\n", dir);
-       FcDirScanConfig (fonts, config->fontDirs, cache, 
-                        config->blanks, dir, FcFalse, config);
+       list = FcConfigGetFontDirs (config);
+       if (!list)
+           goto bail2;
+       
+       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
+    {
+       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++)
+       {
+           if (FcConfigAcceptFont (config, cached_fonts->fonts[i]))
+               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);
@@ -241,10 +315,13 @@ FcConfigBuildFonts (FcConfig *config)
     if (config->cache)
        FcGlobalCacheSave (cache, config->cache);
     FcGlobalCacheDestroy (cache);
+    FcStrSetDestroy (oldDirs);
 
     FcConfigSetFonts (config, fonts, FcSetSystem);
     
     return FcTrue;
+bail2:
+    FcStrSetDestroy (oldDirs);
 bail1:
     FcFontSetDestroy (fonts);
 bail0:
@@ -520,12 +597,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);
@@ -587,9 +664,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;
            }
@@ -637,7 +716,7 @@ FcConfigCompareValue (const FcValue left_o,
                ret = FcLangSetContains (left.u.l, right.u.l);
                break;
            case FcOpNotContains:
-               ret = FcLangSetContains (left.u.l, right.u.l);
+               ret = !FcLangSetContains (left.u.l, right.u.l);
                break;
            case FcOpEqual:
                ret = FcLangSetEqual (left.u.l, right.u.l);
@@ -711,7 +790,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        break;
     case FcOpString:
        v.type = FcTypeString;
-       v.u.s = e->u.sval;
+       v.u.s = FcStrStaticName(e->u.sval);
        v = FcValueSave (v);
        break;
     case FcOpMatrix:
@@ -764,7 +843,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;  
@@ -829,7 +908,8 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                switch (e->op) {
                case FcOpPlus:
                    v.type = FcTypeString;
-                   v.u.s = FcStrPlus (vl.u.s, vr.u.s);
+                   v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
+                        
                    if (!v.u.s)
                        v.type = FcTypeVoid;
                    break;
@@ -977,10 +1057,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;
@@ -1003,6 +1083,7 @@ static FcValueList *
 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
 {
     FcValueList        *l;
+    FcValueListPtr lp;
     
     if (!e)
        return 0;
@@ -1013,56 +1094,63 @@ 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)
+    lp = FcValueListPtrCreateDynamic(l);
+    while (FcValueListPtrU(lp) && FcValueListPtrU(lp)->value.type == FcTypeVoid)
     {
-       FcValueList     *next = l->next;
-       
-       FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
-       free (l);
-       l = next;
+       FcValueListPtr  next = FcValueListPtrU(lp)->next;
+
+       if (lp.bank == FC_BANK_DYNAMIC)
+       {
+           FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
+           free (l);
+       }
+       lp = 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;
            }
        }
@@ -1071,7 +1159,7 @@ FcConfigAdd (FcValueList    **head,
 
        if (FcDebug () & FC_DBG_EDIT)
        {
-           if (!*prev)
+           if (!FcValueListPtrU(*prev))
                printf ("position not on list\n");
        }
     }
@@ -1085,12 +1173,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)
@@ -1104,18 +1192,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;
        }
     }
@@ -1147,8 +1236,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
@@ -1158,7 +1247,7 @@ FcConfigPatternCanon (FcPattern       *p,
     FcPatternElt    *e = FcPatternFindElt (p, object);
     if (!e)
        return;
-    if (!e->values)
+    if (!FcValueListPtrU(e->values))
        FcPatternDel (p, object);
 }
 
@@ -1237,12 +1326,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)
@@ -1295,7 +1384,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
@@ -1461,7 +1550,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] != '/')
@@ -1561,7 +1652,7 @@ FcConfigHome (void)
            home = getenv ("USERPROFILE");
 #endif
 
-       return home;
+       return (FcChar8 *) home;
     }
     return 0;
 }
@@ -1809,3 +1900,40 @@ FcConfigAcceptFilename (FcConfig *config,
        return FcFalse;
     return FcTrue;
 }
+
+/*
+ * Manage font-pattern based font source selectors
+ */
+
+FcBool
+FcConfigPatternsAdd (FcConfig  *config,
+                    FcPattern  *pattern,
+                    FcBool     accept)
+{
+    FcFontSet  *set = accept ? config->acceptPatterns : config->rejectPatterns;
+
+    return FcFontSetAdd (set, pattern);
+}
+
+static FcBool
+FcConfigPatternsMatch (const FcFontSet *patterns,
+                      const FcPattern  *font)
+{
+    int i;
+    
+    for (i = 0; i < patterns->nfont; i++)
+       if (FcListPatternMatchAny (patterns->fonts[i], font))
+           return FcTrue;
+    return FcFalse;
+}
+
+FcBool
+FcConfigAcceptFont (FcConfig       *config,
+                   const FcPattern *font)
+{
+    if (FcConfigPatternsMatch (config->acceptPatterns, font))
+       return FcTrue;
+    if (FcConfigPatternsMatch (config->rejectPatterns, font))
+       return FcFalse;
+    return FcTrue;
+}