2 * $XFree86: xc/lib/fontconfig/src/fccfg.c,v 1.15 2002/06/21 06:14:45 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
35 config = malloc (sizeof (FcConfig));
38 FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig));
40 config->configDirs = FcStrSetCreate ();
41 if (!config->configDirs)
44 config->configFiles = FcStrSetCreate ();
45 if (!config->configFiles)
48 config->fontDirs = FcStrSetCreate ();
49 if (!config->fontDirs)
53 if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
58 config->substPattern = 0;
59 config->substFont = 0;
60 config->maxObjects = 0;
61 for (set = FcSetSystem; set <= FcSetApplication; set++)
62 config->fonts[set] = 0;
64 config->rescanTime = time(0);
65 config->rescanInterval = 30;
70 FcStrSetDestroy (config->fontDirs);
72 FcStrSetDestroy (config->configFiles);
74 FcStrSetDestroy (config->configDirs);
77 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
82 typedef struct _FcFileTime {
88 FcConfigNewestFile (FcStrSet *files)
90 FcStrList *list = FcStrListCreate (files);
91 FcFileTime newest = { 0, FcFalse };
97 while ((file = FcStrListNext (list)))
98 if (stat ((char *) file, &statb) == 0)
99 if (!newest.set || statb.st_mtime - newest.time > 0)
100 newest.time = statb.st_mtime;
101 FcStrListDone (list);
107 FcConfigUptoDate (FcConfig *config)
109 FcFileTime config_time, font_time;
110 time_t now = time(0);
113 config = FcConfigGetCurrent ();
117 config_time = FcConfigNewestFile (config->configFiles);
118 font_time = FcConfigNewestFile (config->configDirs);
119 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
120 (font_time.set && font_time.time - config->rescanTime) > 0)
124 config->rescanTime = now;
129 FcSubstDestroy (FcSubst *s)
136 FcTestDestroy (s->test);
137 FcEditDestroy (s->edit);
143 FcConfigDestroy (FcConfig *config)
147 if (config == _fcConfig)
150 FcStrSetDestroy (config->configDirs);
151 FcStrSetDestroy (config->fontDirs);
152 FcStrSetDestroy (config->configFiles);
154 FcStrFree (config->cache);
156 FcSubstDestroy (config->substPattern);
157 FcSubstDestroy (config->substFont);
158 for (set = FcSetSystem; set <= FcSetApplication; set++)
159 if (config->fonts[set])
160 FcFontSetDestroy (config->fonts[set]);
162 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
166 * Scan the current list of directories in the configuration
167 * and build the set of available fonts. Update the
168 * per-user cache file to reflect the new configuration
172 FcConfigBuildFonts (FcConfig *config)
179 fonts = FcFontSetCreate ();
183 cache = FcFileCacheCreate ();
187 FcFileCacheLoad (cache, config->cache);
189 list = FcConfigGetFontDirs (config);
193 while ((dir = FcStrListNext (list)))
195 if (FcDebug () & FC_DBG_FONTSET)
196 printf ("scan dir %s\n", dir);
197 FcDirScan (fonts, config->fontDirs, cache, config->blanks, dir, FcFalse);
200 FcStrListDone (list);
202 if (FcDebug () & FC_DBG_FONTSET)
203 FcFontSetPrint (fonts);
205 FcFileCacheSave (cache, config->cache);
206 FcFileCacheDestroy (cache);
208 FcConfigSetFonts (config, fonts, FcSetSystem);
212 FcFontSetDestroy (fonts);
218 FcConfigSetCurrent (FcConfig *config)
221 if (!FcConfigBuildFonts (config))
225 FcConfigDestroy (_fcConfig);
231 FcConfigGetCurrent (void)
240 FcConfigAddConfigDir (FcConfig *config,
243 return FcStrSetAddFilename (config->configDirs, d);
247 FcConfigGetConfigDirs (FcConfig *config)
251 config = FcConfigGetCurrent ();
255 return FcStrListCreate (config->configDirs);
259 FcConfigAddFontDir (FcConfig *config,
262 return FcStrSetAddFilename (config->fontDirs, d);
266 FcConfigAddDir (FcConfig *config,
269 return (FcConfigAddConfigDir (config, d) &&
270 FcConfigAddFontDir (config, d));
274 FcConfigGetFontDirs (FcConfig *config)
278 config = FcConfigGetCurrent ();
282 return FcStrListCreate (config->fontDirs);
286 FcConfigAddConfigFile (FcConfig *config,
290 FcChar8 *file = FcConfigFilename (f);
295 ret = FcStrSetAdd (config->configFiles, file);
301 FcConfigGetConfigFiles (FcConfig *config)
305 config = FcConfigGetCurrent ();
309 return FcStrListCreate (config->configFiles);
313 FcConfigSetCache (FcConfig *config,
316 FcChar8 *new = FcStrCopyFilename (c);
321 FcStrFree (config->cache);
327 FcConfigGetCache (FcConfig *config)
331 config = FcConfigGetCurrent ();
335 return config->cache;
339 FcConfigGetFonts (FcConfig *config,
344 config = FcConfigGetCurrent ();
348 return config->fonts[set];
352 FcConfigSetFonts (FcConfig *config,
356 if (config->fonts[set])
357 FcFontSetDestroy (config->fonts[set]);
358 config->fonts[set] = fonts;
364 FcConfigGetBlanks (FcConfig *config)
368 config = FcConfigGetCurrent ();
372 return config->blanks;
376 FcConfigAddBlank (FcConfig *config,
384 b = FcBlanksCreate ();
388 if (!FcBlanksAdd (b, blank))
395 FcConfigGetRescanInverval (FcConfig *config)
399 config = FcConfigGetCurrent ();
403 return config->rescanInterval;
407 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
411 config = FcConfigGetCurrent ();
415 config->rescanInterval = rescanInterval;
420 FcConfigAddEdit (FcConfig *config,
425 FcSubst *subst, **prev;
429 subst = (FcSubst *) malloc (sizeof (FcSubst));
432 if (kind == FcMatchPattern)
433 prev = &config->substPattern;
435 prev = &config->substFont;
436 for (; *prev; prev = &(*prev)->next);
441 if (FcDebug () & FC_DBG_EDIT)
443 printf ("Add Subst ");
444 FcSubstPrint (subst);
447 for (t = test; t; t = t->next)
449 if (config->maxObjects < num)
450 config->maxObjects = num;
454 typedef struct _FcSubState {
459 static const FcMatrix FcIdentityMatrix = { 1, 0, 0, 1 };
462 FcConfigPromote (FcValue v, FcValue u)
464 if (v.type == FcTypeInteger)
466 v.type = FcTypeDouble;
467 v.u.d = (double) v.u.i;
469 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
471 v.u.m = FcMatrixCopy (&FcIdentityMatrix);
473 v.type = FcTypeMatrix;
479 FcConfigCompareValue (FcValue m,
483 FcBool ret = FcFalse;
485 m = FcConfigPromote (m, v);
486 v = FcConfigPromote (v, m);
487 if (m.type == v.type)
492 break; /* FcConfigPromote prevents this from happening */
497 ret = m.u.d == v.u.d;
500 ret = m.u.d != v.u.d;
506 ret = m.u.d <= v.u.d;
512 ret = m.u.d >= v.u.d;
522 ret = m.u.b == v.u.b;
525 ret = m.u.b != v.u.b;
535 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) == 0;
538 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) != 0;
548 ret = FcMatrixEqual (m.u.m, v.u.m);
551 ret = !FcMatrixEqual (m.u.m, v.u.m);
560 /* m contains v if v is a subset of m */
561 ret = FcCharSetIsSubset (v.u.c, m.u.c);
564 ret = FcCharSetEqual (m.u.c, v.u.c);
567 ret = !FcCharSetEqual (m.u.c, v.u.c);
586 ret = m.u.f == v.u.f;
589 ret = m.u.f != v.u.f;
598 if (op == FcOpNotEqual)
606 FcConfigEvaluate (FcPattern *p, FcExpr *e)
614 v.type = FcTypeInteger;
618 v.type = FcTypeDouble;
622 v.type = FcTypeString;
627 v.type = FcTypeMatrix;
632 v.type = FcTypeCharSet;
641 r = FcPatternGet (p, e->u.field, 0, &v);
642 if (r != FcResultMatch)
646 if (FcNameConstant (e->u.constant, &v.u.i))
647 v.type = FcTypeInteger;
652 vl = FcConfigEvaluate (p, e->u.tree.left);
653 if (vl.type == FcTypeBool)
656 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
658 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
677 vl = FcConfigEvaluate (p, e->u.tree.left);
678 vr = FcConfigEvaluate (p, e->u.tree.right);
679 vl = FcConfigPromote (vl, vr);
680 vr = FcConfigPromote (vr, vl);
681 if (vl.type == vr.type)
687 v.type = FcTypeDouble;
688 v.u.d = vl.u.d + vr.u.d;
691 v.type = FcTypeDouble;
692 v.u.d = vl.u.d - vr.u.d;
695 v.type = FcTypeDouble;
696 v.u.d = vl.u.d * vr.u.d;
699 v.type = FcTypeDouble;
700 v.u.d = vl.u.d / vr.u.d;
705 v.u.b = vl.u.d == vr.u.d;
709 v.u.b = vl.u.d != vr.u.d;
713 v.u.b = vl.u.d < vr.u.d;
717 v.u.b = vl.u.d <= vr.u.d;
721 v.u.b = vl.u.d > vr.u.d;
725 v.u.b = vl.u.d >= vr.u.d;
731 if (v.type == FcTypeDouble &&
732 v.u.d == (double) (int) v.u.d)
734 v.type = FcTypeInteger;
742 v.u.b = vl.u.b || vr.u.b;
746 v.u.b = vl.u.b && vr.u.b;
751 v.u.b = vl.u.b == vr.u.b;
755 v.u.b = vl.u.b != vr.u.b;
767 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) == 0;
771 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) != 0;
774 v.type = FcTypeString;
775 v.u.s = FcStrPlus (vl.u.s, vr.u.s);
789 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
793 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
796 v.type = FcTypeMatrix;
797 m = malloc (sizeof (FcMatrix));
800 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
801 FcMatrixMultiply (m, vl.u.m, vr.u.m);
817 /* vl contains vr if vr is a subset of vl */
819 v.u.b = FcCharSetIsSubset (vr.u.c, vl.u.c);
823 v.u.b = FcCharSetEqual (vl.u.c, vr.u.c);
827 v.u.b = !FcCharSetEqual (vl.u.c, vr.u.c);
845 vl = FcConfigEvaluate (p, e->u.tree.left);
865 FcConfigMatchValueList (FcPattern *p,
869 FcValueList *ret = 0;
876 if (e->op == FcOpComma)
878 value = FcConfigEvaluate (p, e->u.tree.left);
883 value = FcConfigEvaluate (p, e);
887 for (v = values; v; v = v->next)
889 if (FcConfigCompareValue (v->value, t->op, value))
896 if (t->qual == FcQualAll)
903 FcValueDestroy (value);
909 FcConfigValues (FcPattern *p, FcExpr *e)
915 l = (FcValueList *) malloc (sizeof (FcValueList));
918 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
919 if (e->op == FcOpComma)
921 l->value = FcConfigEvaluate (p, e->u.tree.left);
922 l->next = FcConfigValues (p, e->u.tree.right);
926 l->value = FcConfigEvaluate (p, e);
929 l->binding = FcValueBindingWeak;
930 while (l && l->value.type == FcTypeVoid)
932 FcValueList *next = l->next;
934 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
942 FcConfigAdd (FcValueList **head,
943 FcValueList *position,
947 FcValueList **prev, *last;
952 prev = &position->next;
954 for (prev = head; *prev; prev = &(*prev)->next)
961 for (prev = head; *prev; prev = &(*prev)->next)
963 if (*prev == position)
970 if (FcDebug () & FC_DBG_EDIT)
973 printf ("position not on list\n");
977 if (FcDebug () & FC_DBG_EDIT)
979 printf ("%s list before ", append ? "Append" : "Prepend");
980 FcValueListPrint (*head);
994 if (FcDebug () & FC_DBG_EDIT)
996 printf ("%s list after ", append ? "Append" : "Prepend");
997 FcValueListPrint (*head);
1005 FcConfigDel (FcValueList **head,
1006 FcValueList *position)
1010 for (prev = head; *prev; prev = &(*prev)->next)
1012 if (*prev == position)
1014 *prev = position->next;
1016 FcValueListDestroy (position);
1023 FcConfigPatternAdd (FcPattern *p,
1030 FcPatternElt *e = FcPatternInsertElt (p, object);
1034 FcConfigAdd (&e->values, 0, append, list);
1039 * Delete all values associated with a field
1042 FcConfigPatternDel (FcPattern *p,
1045 FcPatternElt *e = FcPatternFindElt (p, object);
1049 FcConfigDel (&e->values, e->values);
1053 FcConfigPatternCanon (FcPattern *p,
1056 FcPatternElt *e = FcPatternFindElt (p, object);
1060 FcPatternDel (p, object);
1064 FcConfigSubstitute (FcConfig *config,
1077 config = FcConfigGetCurrent ();
1082 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1083 if (!st && config->maxObjects)
1085 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1087 if (FcDebug () & FC_DBG_EDIT)
1089 printf ("FcConfigSubstitute ");
1092 if (kind == FcMatchPattern)
1093 s = config->substPattern;
1095 s = config->substFont;
1096 for (; s; s = s->next)
1099 * Check the tests to see if
1100 * they all match the pattern
1102 for (t = s->test, i = 0; t; t = t->next, i++)
1104 if (FcDebug () & FC_DBG_EDIT)
1106 printf ("FcConfigSubstitute test ");
1109 st[i].elt = FcPatternFindElt (p, t->field);
1111 * If there's no such field in the font,
1112 * then FcQualAll matches while FcQualAny does not
1116 if (t->qual == FcQualAll)
1125 * Check to see if there is a match, mark the location
1126 * to apply match-relative edits
1128 st[i].value = FcConfigMatchValueList (p, t, st[i].elt->values);
1131 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1133 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1138 if (FcDebug () & FC_DBG_EDIT)
1139 printf ("No match\n");
1142 if (FcDebug () & FC_DBG_EDIT)
1144 printf ("Substitute ");
1147 for (e = s->edit; e; e = e->next)
1150 * Evaluate the list of expressions
1152 l = FcConfigValues (p, e->expr);
1154 * Locate any test associated with this field
1156 for (t = s->test, i = 0; t; t = t->next, i++)
1157 if (!FcStrCmpIgnoreCase ((FcChar8 *) t->field, (FcChar8 *) e->field))
1162 * If there was a test, then replace the matched
1163 * value with the new list of values
1167 FcValueList *thisValue = st[i].value;
1168 FcValueList *nextValue = thisValue ? thisValue->next : 0;
1171 * Append the new list of values after the current value
1173 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1175 * Delete the marked value
1177 FcConfigDel (&st[i].elt->values, thisValue);
1179 * Adjust any pointers into the value list to ensure
1180 * future edits occur at the same place
1182 for (t = s->test, i = 0; t; t = t->next, i++)
1184 if (st[i].value == thisValue)
1185 st[i].value = nextValue;
1189 /* fall through ... */
1190 case FcOpAssignReplace:
1192 * Delete all of the values and insert
1195 FcConfigPatternDel (p, e->field);
1196 FcConfigPatternAdd (p, e->field, l, FcTrue);
1198 * Adjust any pointers into the value list as they no
1199 * longer point to anything valid
1203 FcPatternElt *thisElt = st[i].elt;
1204 for (t = s->test, i = 0; t; t = t->next, i++)
1206 if (st[i].elt == thisElt)
1214 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1217 /* fall through ... */
1218 case FcOpPrependFirst:
1219 FcConfigPatternAdd (p, e->field, l, FcFalse);
1224 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1227 /* fall through ... */
1228 case FcOpAppendLast:
1229 FcConfigPatternAdd (p, e->field, l, FcTrue);
1236 * Now go through the pattern and eliminate
1237 * any properties without data
1239 for (e = s->edit; e; e = e->next)
1240 FcConfigPatternCanon (p, e->field);
1242 if (FcDebug () & FC_DBG_EDIT)
1244 printf ("FcConfigSubstitute edit");
1248 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1250 if (FcDebug () & FC_DBG_EDIT)
1252 printf ("FcConfigSubstitute done");
1258 #ifndef FONTCONFIG_PATH
1259 #define FONTCONFIG_PATH "/etc/fonts"
1262 #ifndef FONTCONFIG_FILE
1263 #define FONTCONFIG_FILE "fonts.conf"
1267 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1272 dir = (FcChar8 *) "";
1273 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1277 strcpy ((char *) path, (const char *) dir);
1278 /* make sure there's a single separating / */
1279 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1280 strcat ((char *) path, "/");
1281 strcat ((char *) path, (char *) file);
1283 if (access ((char *) path, R_OK) == 0)
1291 FcConfigGetPath (void)
1294 FcChar8 *env, *e, *colon;
1299 npath = 2; /* default dir + null */
1300 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1309 path = calloc (npath, sizeof (FcChar8 *));
1319 colon = (FcChar8 *) strchr ((char *) e, ':');
1321 colon = e + strlen ((char *) e);
1322 path[i] = malloc (colon - e + 1);
1325 strncpy ((char *) path[i], (const char *) e, colon - e);
1326 path[i][colon - e] = '\0';
1335 dir = (FcChar8 *) FONTCONFIG_PATH;
1336 path[i] = malloc (strlen ((char *) dir) + 1);
1339 strcpy ((char *) path[i], (const char *) dir);
1343 for (i = 0; path[i]; i++)
1351 FcConfigFreePath (FcChar8 **path)
1355 for (p = path; *p; p++)
1361 FcConfigFilename (const FcChar8 *url)
1363 FcChar8 *file, *dir, **path, **p;
1367 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1369 url = (FcChar8 *) FONTCONFIG_FILE;
1374 dir = (FcChar8 *) getenv ("HOME");
1376 file = FcConfigFileExists (dir, url + 1);
1381 file = FcConfigFileExists (0, url);
1384 path = FcConfigGetPath ();
1387 for (p = path; *p; p++)
1389 file = FcConfigFileExists (*p, url);
1393 FcConfigFreePath (path);
1400 * Manage the application-specific fonts
1404 FcConfigAppFontAddFile (FcConfig *config,
1405 const FcChar8 *file)
1414 config = FcConfigGetCurrent ();
1419 subdirs = FcStrSetCreate ();
1423 set = FcConfigGetFonts (config, FcSetApplication);
1426 set = FcFontSetCreate ();
1429 FcStrSetDestroy (subdirs);
1432 FcConfigSetFonts (config, set, FcSetApplication);
1435 if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
1437 FcStrSetDestroy (subdirs);
1440 if ((sublist = FcStrListCreate (subdirs)))
1442 while ((subdir = FcStrListNext (sublist)))
1444 FcConfigAppFontAddDir (config, subdir);
1446 FcStrListDone (sublist);
1452 FcConfigAppFontAddDir (FcConfig *config,
1462 config = FcConfigGetCurrent ();
1466 subdirs = FcStrSetCreate ();
1470 set = FcConfigGetFonts (config, FcSetApplication);
1473 set = FcFontSetCreate ();
1476 FcStrSetDestroy (subdirs);
1479 FcConfigSetFonts (config, set, FcSetApplication);
1482 if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
1484 FcStrSetDestroy (subdirs);
1487 if ((sublist = FcStrListCreate (subdirs)))
1489 while ((subdir = FcStrListNext (sublist)))
1491 FcConfigAppFontAddDir (config, subdir);
1493 FcStrListDone (sublist);
1499 FcConfigAppFontClear (FcConfig *config)
1501 FcConfigSetFonts (config, 0, FcSetApplication);