2 * $XFree86: xc/lib/fontconfig/src/fccfg.c,v 1.20 2002/08/19 19:32:05 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)
175 FcGlobalCache *cache;
179 fonts = FcFontSetCreate ();
183 cache = FcGlobalCacheCreate ();
187 FcGlobalCacheLoad (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 FcGlobalCacheSave (cache, config->cache);
206 FcGlobalCacheDestroy (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);
442 for (t = test; t; t = t->next)
444 if (t->kind == FcMatchDefault)
448 if (config->maxObjects < num)
449 config->maxObjects = num;
450 if (FcDebug () & FC_DBG_EDIT)
452 printf ("Add Subst ");
453 FcSubstPrint (subst);
458 typedef struct _FcSubState {
464 FcConfigPromote (FcValue v, FcValue u)
466 if (v.type == FcTypeInteger)
468 v.type = FcTypeDouble;
469 v.u.d = (double) v.u.i;
471 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
473 v.u.m = &FcIdentityMatrix;
474 v.type = FcTypeMatrix;
476 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
478 v.u.l = FcLangSetPromote (v.u.s);
479 v.type = FcTypeLangSet;
485 FcConfigCompareValue (FcValue m,
489 FcBool ret = FcFalse;
491 m = FcConfigPromote (m, v);
492 v = FcConfigPromote (v, m);
493 if (m.type == v.type)
497 break; /* FcConfigPromote prevents this from happening */
502 ret = m.u.d == v.u.d;
505 ret = m.u.d != v.u.d;
511 ret = m.u.d <= v.u.d;
517 ret = m.u.d >= v.u.d;
527 ret = m.u.b == v.u.b;
530 ret = m.u.b != v.u.b;
540 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) == 0;
543 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) != 0;
553 ret = FcMatrixEqual (m.u.m, v.u.m);
556 ret = !FcMatrixEqual (m.u.m, v.u.m);
565 /* m contains v if v is a subset of m */
566 ret = FcCharSetIsSubset (v.u.c, m.u.c);
569 ret = FcCharSetEqual (m.u.c, v.u.c);
572 ret = !FcCharSetEqual (m.u.c, v.u.c);
581 ret = FcLangSetCompare (v.u.l, m.u.l) != FcLangDifferentLang;
584 ret = FcLangSetEqual (v.u.l, m.u.l);
587 ret = !FcLangSetEqual (v.u.l, m.u.l);
606 ret = m.u.f == v.u.f;
609 ret = m.u.f != v.u.f;
619 if (op == FcOpNotEqual)
627 FcConfigEvaluate (FcPattern *p, FcExpr *e)
635 v.type = FcTypeInteger;
639 v.type = FcTypeDouble;
643 v.type = FcTypeString;
648 v.type = FcTypeMatrix;
653 v.type = FcTypeCharSet;
662 r = FcPatternGet (p, e->u.field, 0, &v);
663 if (r != FcResultMatch)
667 if (FcNameConstant (e->u.constant, &v.u.i))
668 v.type = FcTypeInteger;
673 vl = FcConfigEvaluate (p, e->u.tree.left);
674 if (vl.type == FcTypeBool)
677 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
679 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
691 vl = FcConfigEvaluate (p, e->u.tree.left);
692 vr = FcConfigEvaluate (p, e->u.tree.right);
694 v.u.b = FcConfigCompareValue (vl, e->op, vr);
705 vl = FcConfigEvaluate (p, e->u.tree.left);
706 vr = FcConfigEvaluate (p, e->u.tree.right);
707 vl = FcConfigPromote (vl, vr);
708 vr = FcConfigPromote (vr, vl);
709 if (vl.type == vr.type)
715 v.type = FcTypeDouble;
716 v.u.d = vl.u.d + vr.u.d;
719 v.type = FcTypeDouble;
720 v.u.d = vl.u.d - vr.u.d;
723 v.type = FcTypeDouble;
724 v.u.d = vl.u.d * vr.u.d;
727 v.type = FcTypeDouble;
728 v.u.d = vl.u.d / vr.u.d;
734 if (v.type == FcTypeDouble &&
735 v.u.d == (double) (int) v.u.d)
737 v.type = FcTypeInteger;
745 v.u.b = vl.u.b || vr.u.b;
749 v.u.b = vl.u.b && vr.u.b;
759 v.type = FcTypeString;
760 v.u.s = FcStrPlus (vl.u.s, vr.u.s);
772 v.type = FcTypeMatrix;
773 m = malloc (sizeof (FcMatrix));
776 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
777 FcMatrixMultiply (m, vl.u.m, vr.u.m);
801 vl = FcConfigEvaluate (p, e->u.tree.left);
821 FcConfigMatchValueList (FcPattern *p,
825 FcValueList *ret = 0;
832 if (e->op == FcOpComma)
834 value = FcConfigEvaluate (p, e->u.tree.left);
839 value = FcConfigEvaluate (p, e);
843 for (v = values; v; v = v->next)
845 if (FcConfigCompareValue (v->value, t->op, value))
852 if (t->qual == FcQualAll)
859 FcValueDestroy (value);
865 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
871 l = (FcValueList *) malloc (sizeof (FcValueList));
874 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
875 if (e->op == FcOpComma)
877 l->value = FcConfigEvaluate (p, e->u.tree.left);
878 l->next = FcConfigValues (p, e->u.tree.right, binding);
882 l->value = FcConfigEvaluate (p, e);
885 l->binding = binding;
886 while (l && l->value.type == FcTypeVoid)
888 FcValueList *next = l->next;
890 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
898 FcConfigAdd (FcValueList **head,
899 FcValueList *position,
903 FcValueList **prev, *last;
908 prev = &position->next;
910 for (prev = head; *prev; prev = &(*prev)->next)
917 for (prev = head; *prev; prev = &(*prev)->next)
919 if (*prev == position)
926 if (FcDebug () & FC_DBG_EDIT)
929 printf ("position not on list\n");
933 if (FcDebug () & FC_DBG_EDIT)
935 printf ("%s list before ", append ? "Append" : "Prepend");
936 FcValueListPrint (*head);
950 if (FcDebug () & FC_DBG_EDIT)
952 printf ("%s list after ", append ? "Append" : "Prepend");
953 FcValueListPrint (*head);
961 FcConfigDel (FcValueList **head,
962 FcValueList *position)
966 for (prev = head; *prev; prev = &(*prev)->next)
968 if (*prev == position)
970 *prev = position->next;
972 FcValueListDestroy (position);
979 FcConfigPatternAdd (FcPattern *p,
986 FcPatternElt *e = FcPatternInsertElt (p, object);
990 FcConfigAdd (&e->values, 0, append, list);
995 * Delete all values associated with a field
998 FcConfigPatternDel (FcPattern *p,
1001 FcPatternElt *e = FcPatternFindElt (p, object);
1005 FcConfigDel (&e->values, e->values);
1009 FcConfigPatternCanon (FcPattern *p,
1012 FcPatternElt *e = FcPatternFindElt (p, object);
1016 FcPatternDel (p, object);
1020 FcConfigSubstituteWithPat (FcConfig *config,
1035 config = FcConfigGetCurrent ();
1040 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1041 if (!st && config->maxObjects)
1043 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1045 if (FcDebug () & FC_DBG_EDIT)
1047 printf ("FcConfigSubstitute ");
1050 if (kind == FcMatchPattern)
1051 s = config->substPattern;
1053 s = config->substFont;
1054 for (; s; s = s->next)
1057 * Check the tests to see if
1058 * they all match the pattern
1060 for (t = s->test, i = 0; t; t = t->next, i++)
1062 if (FcDebug () & FC_DBG_EDIT)
1064 printf ("FcConfigSubstitute test ");
1068 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1073 st[i].elt = FcPatternFindElt (m, t->field);
1077 * If there's no such field in the font,
1078 * then FcQualAll matches while FcQualAny does not
1082 if (t->qual == FcQualAll)
1091 * Check to see if there is a match, mark the location
1092 * to apply match-relative edits
1094 st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
1097 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1099 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1104 if (FcDebug () & FC_DBG_EDIT)
1105 printf ("No match\n");
1108 if (FcDebug () & FC_DBG_EDIT)
1110 printf ("Substitute ");
1113 for (e = s->edit; e; e = e->next)
1116 * Evaluate the list of expressions
1118 l = FcConfigValues (p, e->expr, e->binding);
1120 * Locate any test associated with this field, skipping
1121 * tests associated with the pattern when substituting in
1124 for (t = s->test, i = 0; t; t = t->next, i++)
1126 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1127 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1128 (FcChar8 *) e->field))
1134 * If there was a test, then replace the matched
1135 * value with the new list of values
1139 FcValueList *thisValue = st[i].value;
1140 FcValueList *nextValue = thisValue ? thisValue->next : 0;
1143 * Append the new list of values after the current value
1145 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1147 * Delete the marked value
1149 FcConfigDel (&st[i].elt->values, thisValue);
1151 * Adjust any pointers into the value list to ensure
1152 * future edits occur at the same place
1154 for (t = s->test, i = 0; t; t = t->next, i++)
1156 if (st[i].value == thisValue)
1157 st[i].value = nextValue;
1161 /* fall through ... */
1162 case FcOpAssignReplace:
1164 * Delete all of the values and insert
1167 FcConfigPatternDel (p, e->field);
1168 FcConfigPatternAdd (p, e->field, l, FcTrue);
1170 * Adjust any pointers into the value list as they no
1171 * longer point to anything valid
1175 FcPatternElt *thisElt = st[i].elt;
1176 for (t = s->test, i = 0; t; t = t->next, i++)
1178 if (st[i].elt == thisElt)
1186 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1189 /* fall through ... */
1190 case FcOpPrependFirst:
1191 FcConfigPatternAdd (p, e->field, l, FcFalse);
1196 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1199 /* fall through ... */
1200 case FcOpAppendLast:
1201 FcConfigPatternAdd (p, e->field, l, FcTrue);
1208 * Now go through the pattern and eliminate
1209 * any properties without data
1211 for (e = s->edit; e; e = e->next)
1212 FcConfigPatternCanon (p, e->field);
1214 if (FcDebug () & FC_DBG_EDIT)
1216 printf ("FcConfigSubstitute edit");
1220 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1222 if (FcDebug () & FC_DBG_EDIT)
1224 printf ("FcConfigSubstitute done");
1231 FcConfigSubstitute (FcConfig *config,
1235 return FcConfigSubstituteWithPat (config, p, 0, kind);
1238 #ifndef FONTCONFIG_PATH
1239 #define FONTCONFIG_PATH "/etc/fonts"
1242 #ifndef FONTCONFIG_FILE
1243 #define FONTCONFIG_FILE "fonts.conf"
1247 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1252 dir = (FcChar8 *) "";
1253 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1257 strcpy ((char *) path, (const char *) dir);
1258 /* make sure there's a single separating / */
1259 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1260 strcat ((char *) path, "/");
1261 strcat ((char *) path, (char *) file);
1263 if (access ((char *) path, R_OK) == 0)
1271 FcConfigGetPath (void)
1274 FcChar8 *env, *e, *colon;
1279 npath = 2; /* default dir + null */
1280 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1289 path = calloc (npath, sizeof (FcChar8 *));
1299 colon = (FcChar8 *) strchr ((char *) e, ':');
1301 colon = e + strlen ((char *) e);
1302 path[i] = malloc (colon - e + 1);
1305 strncpy ((char *) path[i], (const char *) e, colon - e);
1306 path[i][colon - e] = '\0';
1315 dir = (FcChar8 *) FONTCONFIG_PATH;
1316 path[i] = malloc (strlen ((char *) dir) + 1);
1319 strcpy ((char *) path[i], (const char *) dir);
1323 for (i = 0; path[i]; i++)
1331 FcConfigFreePath (FcChar8 **path)
1335 for (p = path; *p; p++)
1341 FcConfigFilename (const FcChar8 *url)
1343 FcChar8 *file, *dir, **path, **p;
1347 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1349 url = (FcChar8 *) FONTCONFIG_FILE;
1354 dir = (FcChar8 *) getenv ("HOME");
1356 file = FcConfigFileExists (dir, url + 1);
1361 file = FcConfigFileExists (0, url);
1364 path = FcConfigGetPath ();
1367 for (p = path; *p; p++)
1369 file = FcConfigFileExists (*p, url);
1373 FcConfigFreePath (path);
1380 * Manage the application-specific fonts
1384 FcConfigAppFontAddFile (FcConfig *config,
1385 const FcChar8 *file)
1394 config = FcConfigGetCurrent ();
1399 subdirs = FcStrSetCreate ();
1403 set = FcConfigGetFonts (config, FcSetApplication);
1406 set = FcFontSetCreate ();
1409 FcStrSetDestroy (subdirs);
1412 FcConfigSetFonts (config, set, FcSetApplication);
1415 if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
1417 FcStrSetDestroy (subdirs);
1420 if ((sublist = FcStrListCreate (subdirs)))
1422 while ((subdir = FcStrListNext (sublist)))
1424 FcConfigAppFontAddDir (config, subdir);
1426 FcStrListDone (sublist);
1432 FcConfigAppFontAddDir (FcConfig *config,
1442 config = FcConfigGetCurrent ();
1446 subdirs = FcStrSetCreate ();
1450 set = FcConfigGetFonts (config, FcSetApplication);
1453 set = FcFontSetCreate ();
1456 FcStrSetDestroy (subdirs);
1459 FcConfigSetFonts (config, set, FcSetApplication);
1462 if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
1464 FcStrSetDestroy (subdirs);
1467 if ((sublist = FcStrListCreate (subdirs)))
1469 while ((subdir = FcStrListNext (sublist)))
1471 FcConfigAppFontAddDir (config, subdir);
1473 FcStrListDone (sublist);
1479 FcConfigAppFontClear (FcConfig *config)
1481 FcConfigSetFonts (config, 0, FcSetApplication);