2 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
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.
27 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config = malloc (sizeof (FcConfig));
50 FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig));
52 config->configDirs = FcStrSetCreate ();
53 if (!config->configDirs)
56 config->configFiles = FcStrSetCreate ();
57 if (!config->configFiles)
60 config->fontDirs = FcStrSetCreate ();
61 if (!config->fontDirs)
64 config->acceptGlobs = FcStrSetCreate ();
65 if (!config->acceptGlobs)
68 config->rejectGlobs = FcStrSetCreate ();
69 if (!config->rejectGlobs)
72 config->acceptPatterns = FcFontSetCreate ();
73 if (!config->acceptPatterns)
76 config->rejectPatterns = FcFontSetCreate ();
77 if (!config->rejectPatterns)
82 if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
86 if (config->cache == 0)
88 /* If no home, use the temp folder. */
90 int templen = GetTempPath (1, dummy);
91 FcChar8 *temp = malloc (templen + 1);
97 GetTempPath (templen + 1, temp);
98 cache_dir = FcStrPlus (temp, FC_USER_CACHE_FILE);
100 if (!FcConfigSetCache (config, cache_dir))
102 FcStrFree (cache_dir);
105 FcStrFree (cache_dir);
112 config->substPattern = 0;
113 config->substFont = 0;
114 config->maxObjects = 0;
115 for (set = FcSetSystem; set <= FcSetApplication; set++)
116 config->fonts[set] = 0;
118 config->rescanTime = time(0);
119 config->rescanInterval = 30;
124 FcFontSetDestroy (config->rejectPatterns);
126 FcFontSetDestroy (config->acceptPatterns);
128 FcStrSetDestroy (config->rejectGlobs);
130 FcStrSetDestroy (config->acceptGlobs);
132 FcStrSetDestroy (config->fontDirs);
134 FcStrSetDestroy (config->configFiles);
136 FcStrSetDestroy (config->configDirs);
139 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
145 FcConfigNewestFile (FcStrSet *files)
147 FcStrList *list = FcStrListCreate (files);
148 FcFileTime newest = { 0, FcFalse };
154 while ((file = FcStrListNext (list)))
155 if (stat ((char *) file, &statb) == 0)
156 if (!newest.set || statb.st_mtime - newest.time > 0)
159 newest.time = statb.st_mtime;
161 FcStrListDone (list);
167 FcConfigModifiedTime (FcConfig *config)
171 FcFileTime v = { 0, FcFalse };
172 config = FcConfigGetCurrent ();
176 return FcConfigNewestFile (config->configFiles);
180 FcConfigUptoDate (FcConfig *config)
182 FcFileTime config_time, font_time;
183 time_t now = time(0);
186 config = FcConfigGetCurrent ();
190 config_time = FcConfigNewestFile (config->configFiles);
191 font_time = FcConfigNewestFile (config->fontDirs);
192 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
193 (font_time.set && (font_time.time - config->rescanTime) > 0))
197 config->rescanTime = now;
202 FcSubstDestroy (FcSubst *s)
210 FcTestDestroy (s->test);
212 FcEditDestroy (s->edit);
214 FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
220 FcConfigDestroy (FcConfig *config)
224 if (config == _fcConfig)
227 FcStrSetDestroy (config->configDirs);
228 FcStrSetDestroy (config->fontDirs);
229 FcStrSetDestroy (config->configFiles);
230 FcStrSetDestroy (config->acceptGlobs);
231 FcStrSetDestroy (config->rejectGlobs);
232 FcFontSetDestroy (config->acceptPatterns);
233 FcFontSetDestroy (config->rejectPatterns);
236 FcBlanksDestroy (config->blanks);
239 FcStrFree (config->cache);
241 FcSubstDestroy (config->substPattern);
242 FcSubstDestroy (config->substFont);
243 for (set = FcSetSystem; set <= FcSetApplication; set++)
244 if (config->fonts[set])
245 FcFontSetDestroy (config->fonts[set]);
248 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
252 * Scan the current list of directories in the configuration
253 * and build the set of available fonts. Update the
254 * per-user cache file to reflect the new configuration
258 FcConfigBuildFonts (FcConfig *config)
260 FcFontSet *fonts, *cached_fonts;
261 FcGlobalCache *cache;
266 fonts = FcFontSetCreate ();
270 cache = FcGlobalCacheCreate ();
274 oldDirs = FcStrSetCreate ();
279 FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
281 cached_fonts = FcCacheRead(config, cache);
284 list = FcConfigGetFontDirs (config);
288 while ((dir = FcStrListNext (list)))
290 if (FcDebug () & FC_DBG_FONTSET)
291 printf ("build scan dir %s\n", dir);
292 FcDirScanConfig (fonts, config->fontDirs, cache,
293 config->blanks, dir, FcFalse, config);
296 FcStrListDone (list);
302 for (i = 0; i < oldDirs->num; i++)
304 if (FcDebug () & FC_DBG_FONTSET)
305 printf ("scan dir %s\n", oldDirs->strs[i]);
306 FcDirScanConfig (fonts, config->fontDirs, cache,
307 config->blanks, oldDirs->strs[i],
311 for (i = 0; i < cached_fonts->nfont; i++)
314 FcPatternGetString (cached_fonts->fonts[i], FC_FILE, 0, &cfn);
316 if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
317 (cfn && FcConfigAcceptFilename (config, cfn)))
318 FcFontSetAdd (fonts, cached_fonts->fonts[i]);
320 cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
322 cached_fonts->nfont = 0;
323 FcFontSetDestroy (cached_fonts);
326 if (FcDebug () & FC_DBG_FONTSET)
327 FcFontSetPrint (fonts);
330 FcGlobalCacheSave (cache, config->cache, config);
331 FcGlobalCacheDestroy (cache);
332 FcStrSetDestroy (oldDirs);
334 FcConfigSetFonts (config, fonts, FcSetSystem);
338 FcStrSetDestroy (oldDirs);
340 FcGlobalCacheDestroy (cache);
342 FcFontSetDestroy (fonts);
348 FcConfigSetCurrent (FcConfig *config)
351 if (!FcConfigBuildFonts (config))
355 FcConfigDestroy (_fcConfig);
361 FcConfigGetCurrent (void)
370 FcConfigAddConfigDir (FcConfig *config,
373 return FcStrSetAddFilename (config->configDirs, d);
377 FcConfigGetConfigDirs (FcConfig *config)
381 config = FcConfigGetCurrent ();
385 return FcStrListCreate (config->configDirs);
389 FcConfigInodeMatchFontDir (FcConfig *config, const FcChar8 *d)
396 /* first we do string matches rather than file accesses */
397 /* FcStrSetMember doesn't tell us the index so that we can return
398 * the config-owned copy. */
399 for (n = 0; n < config->fontDirs->num; n++)
401 if (!FcStrCmp (config->fontDirs->strs[n], d))
402 return config->fontDirs->strs[n];
405 /* If this is a bottleneck, we can cache the fontDir inodes. */
406 if (stat ((char *)d, &s) == -1)
408 di = s.st_ino; dd = s.st_dev;
410 for (n = 0; n < config->fontDirs->num; n++)
412 if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
414 if (di == s.st_ino && dd == s.st_dev)
415 return config->fontDirs->strs[n];
421 FcConfigAddFontDir (FcConfig *config,
424 /* Avoid adding d if it's an alias of something else, too. */
425 if (FcConfigInodeMatchFontDir(config, d))
427 return FcStrSetAddFilename (config->fontDirs, d);
431 FcConfigAddFontDirSubdirs (FcConfig *config,
437 FcBool added = FcFalse;
439 if (!(dir = opendir ((char *) d)))
441 if (!(subdir = (FcChar8 *) malloc (strlen ((char *) d) + FC_MAX_FILE_LEN + 2)))
443 fprintf (stderr, "out of memory");
446 while ((e = readdir (dir)))
448 if (strcmp (e->d_name, ".") && strcmp (e->d_name, "..") &&
449 strlen (e->d_name) < FC_MAX_FILE_LEN)
451 strcpy ((char *)subdir, (char *)d);
452 strcat ((char *)subdir, "/");
453 strcat ((char *)subdir, e->d_name);
454 if (FcFileIsDir (subdir))
456 if (FcConfigInodeMatchFontDir(config, subdir))
457 continue; /* already added */
458 FcStrSetAddFilename (config->fontDirs, subdir);
459 FcConfigAddFontDirSubdirs (config, subdir);
470 FcConfigNormalizeFontDir (FcConfig *config,
475 FcBool added = FcFalse;
477 d0 = FcConfigInodeMatchFontDir(config, d);
481 /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
482 for (n = 0, n0 = config->fontDirs->num; n < n0; n++)
484 if (FcConfigAddFontDirSubdirs (config, config->fontDirs->strs[n]))
488 /* ... and try again. */
490 return FcConfigInodeMatchFontDir(config, d);
496 FcConfigAddDir (FcConfig *config,
499 return (FcConfigAddConfigDir (config, d) &&
500 FcConfigAddFontDir (config, d));
504 FcConfigGetFontDirs (FcConfig *config)
508 config = FcConfigGetCurrent ();
512 return FcStrListCreate (config->fontDirs);
516 FcConfigAddConfigFile (FcConfig *config,
520 FcChar8 *file = FcConfigFilename (f);
525 ret = FcStrSetAdd (config->configFiles, file);
531 FcConfigGetConfigFiles (FcConfig *config)
535 config = FcConfigGetCurrent ();
539 return FcStrListCreate (config->configFiles);
543 FcConfigSetCache (FcConfig *config,
546 FcChar8 *new = FcStrCopyFilename (c);
551 FcStrFree (config->cache);
557 FcConfigGetCache (FcConfig *config)
561 config = FcConfigGetCurrent ();
565 return config->cache;
569 FcConfigGetFonts (FcConfig *config,
574 config = FcConfigGetCurrent ();
578 return config->fonts[set];
582 FcConfigSetFonts (FcConfig *config,
586 if (config->fonts[set])
587 FcFontSetDestroy (config->fonts[set]);
588 config->fonts[set] = fonts;
594 FcConfigGetBlanks (FcConfig *config)
598 config = FcConfigGetCurrent ();
602 return config->blanks;
606 FcConfigAddBlank (FcConfig *config,
609 FcBlanks *b, *freeme = 0;
614 freeme = b = FcBlanksCreate ();
618 if (!FcBlanksAdd (b, blank))
621 FcBlanksDestroy (freeme);
629 FcConfigGetRescanInverval (FcConfig *config)
633 config = FcConfigGetCurrent ();
637 return config->rescanInterval;
641 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
645 config = FcConfigGetCurrent ();
649 config->rescanInterval = rescanInterval;
654 FcConfigAddEdit (FcConfig *config,
659 FcSubst *subst, **prev;
663 subst = (FcSubst *) malloc (sizeof (FcSubst));
666 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
667 if (kind == FcMatchPattern)
668 prev = &config->substPattern;
670 prev = &config->substFont;
671 for (; *prev; prev = &(*prev)->next);
677 for (t = test; t; t = t->next)
679 if (t->kind == FcMatchDefault)
683 if (config->maxObjects < num)
684 config->maxObjects = num;
685 if (FcDebug () & FC_DBG_EDIT)
687 printf ("Add Subst ");
688 FcSubstPrint (subst);
693 typedef struct _FcSubState {
699 FcConfigPromote (FcValue v, FcValue u)
701 if (v.type == FcTypeInteger)
703 v.type = FcTypeDouble;
704 v.u.d = (double) v.u.i;
706 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
708 v.u.m = &FcIdentityMatrix;
709 v.type = FcTypeMatrix;
711 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
713 v.u.l = FcLangSetPromote (v.u.s);
714 v.type = FcTypeLangSet;
720 FcConfigCompareValue (const FcValue *left_o,
722 const FcValue *right_o)
724 FcValue left = FcValueCanonicalize(left_o);
725 FcValue right = FcValueCanonicalize(right_o);
726 FcBool ret = FcFalse;
728 left = FcConfigPromote (left, right);
729 right = FcConfigPromote (right, left);
730 if (left.type == right.type)
734 break; /* FcConfigPromote prevents this from happening */
740 ret = left.u.d == right.u.d;
743 case FcOpNotContains:
744 ret = left.u.d != right.u.d;
747 ret = left.u.d < right.u.d;
750 ret = left.u.d <= right.u.d;
753 ret = left.u.d > right.u.d;
756 ret = left.u.d >= right.u.d;
767 ret = left.u.b == right.u.b;
770 case FcOpNotContains:
771 ret = left.u.b != right.u.b;
781 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
784 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
787 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
789 case FcOpNotContains:
790 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
801 ret = FcMatrixEqual (left.u.m, right.u.m);
804 case FcOpNotContains:
805 ret = !FcMatrixEqual (left.u.m, right.u.m);
815 /* left contains right if right is a subset of left */
816 ret = FcCharSetIsSubset (right.u.c, left.u.c);
818 case FcOpNotContains:
819 /* left contains right if right is a subset of left */
820 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
823 ret = FcCharSetEqual (left.u.c, right.u.c);
826 ret = !FcCharSetEqual (left.u.c, right.u.c);
836 ret = FcLangSetContains (left.u.l, right.u.l);
838 case FcOpNotContains:
839 ret = !FcLangSetContains (left.u.l, right.u.l);
842 ret = FcLangSetEqual (left.u.l, right.u.l);
845 ret = !FcLangSetEqual (left.u.l, right.u.l);
867 ret = left.u.f == right.u.f;
870 case FcOpNotContains:
871 ret = left.u.f != right.u.f;
881 if (op == FcOpNotEqual || op == FcOpNotContains)
888 #define _FcDoubleFloor(d) ((int) (d))
889 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
890 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
891 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
892 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
893 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
896 FcConfigEvaluate (FcPattern *p, FcExpr *e)
905 v.type = FcTypeInteger;
909 v.type = FcTypeDouble;
913 v.type = FcTypeString;
914 v.u.s = FcStrStaticName(e->u.sval);
917 v.type = FcTypeMatrix;
922 v.type = FcTypeCharSet;
931 r = FcPatternGet (p, e->u.field, 0, &v);
932 if (r != FcResultMatch)
937 if (FcNameConstant (e->u.constant, &v.u.i))
938 v.type = FcTypeInteger;
943 vl = FcConfigEvaluate (p, e->u.tree.left);
944 if (vl.type == FcTypeBool)
947 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
949 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
962 case FcOpNotContains:
964 vl = FcConfigEvaluate (p, e->u.tree.left);
965 vr = FcConfigEvaluate (p, e->u.tree.right);
967 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
977 vl = FcConfigEvaluate (p, e->u.tree.left);
978 vr = FcConfigEvaluate (p, e->u.tree.right);
979 vl = FcConfigPromote (vl, vr);
980 vr = FcConfigPromote (vr, vl);
981 if (vl.type == vr.type)
987 v.type = FcTypeDouble;
988 v.u.d = vl.u.d + vr.u.d;
991 v.type = FcTypeDouble;
992 v.u.d = vl.u.d - vr.u.d;
995 v.type = FcTypeDouble;
996 v.u.d = vl.u.d * vr.u.d;
999 v.type = FcTypeDouble;
1000 v.u.d = vl.u.d / vr.u.d;
1003 v.type = FcTypeVoid;
1006 if (v.type == FcTypeDouble &&
1007 v.u.d == (double) (int) v.u.d)
1009 v.type = FcTypeInteger;
1010 v.u.i = (int) v.u.d;
1016 v.type = FcTypeBool;
1017 v.u.b = vl.u.b || vr.u.b;
1020 v.type = FcTypeBool;
1021 v.u.b = vl.u.b && vr.u.b;
1024 v.type = FcTypeVoid;
1031 v.type = FcTypeString;
1032 str = FcStrPlus (vl.u.s, vr.u.s);
1033 v.u.s = FcStrStaticName (str);
1037 v.type = FcTypeVoid;
1040 v.type = FcTypeVoid;
1047 v.type = FcTypeMatrix;
1048 m = malloc (sizeof (FcMatrix));
1051 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
1052 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1057 v.type = FcTypeVoid;
1061 v.type = FcTypeVoid;
1066 v.type = FcTypeVoid;
1071 v.type = FcTypeVoid;
1072 FcValueDestroy (vl);
1073 FcValueDestroy (vr);
1076 vl = FcConfigEvaluate (p, e->u.tree.left);
1079 v.type = FcTypeBool;
1083 v.type = FcTypeVoid;
1086 FcValueDestroy (vl);
1089 vl = FcConfigEvaluate (p, e->u.tree.left);
1095 v.type = FcTypeInteger;
1096 v.u.i = FcDoubleFloor (vl.u.d);
1099 v.type = FcTypeVoid;
1102 FcValueDestroy (vl);
1105 vl = FcConfigEvaluate (p, e->u.tree.left);
1111 v.type = FcTypeInteger;
1112 v.u.i = FcDoubleCeil (vl.u.d);
1115 v.type = FcTypeVoid;
1118 FcValueDestroy (vl);
1121 vl = FcConfigEvaluate (p, e->u.tree.left);
1127 v.type = FcTypeInteger;
1128 v.u.i = FcDoubleRound (vl.u.d);
1131 v.type = FcTypeVoid;
1134 FcValueDestroy (vl);
1137 vl = FcConfigEvaluate (p, e->u.tree.left);
1143 v.type = FcTypeInteger;
1144 v.u.i = FcDoubleTrunc (vl.u.d);
1147 v.type = FcTypeVoid;
1150 FcValueDestroy (vl);
1153 v.type = FcTypeVoid;
1159 static FcValueList *
1160 FcConfigMatchValueList (FcPattern *p,
1162 FcValueList *values)
1164 FcValueList *ret = 0;
1165 FcExpr *e = t->expr;
1171 /* Compute the value of the match expression */
1172 if (e->op == FcOpComma)
1174 value = FcConfigEvaluate (p, e->u.tree.left);
1175 e = e->u.tree.right;
1179 value = FcConfigEvaluate (p, e);
1183 for (v = values; v; v = FcValueListPtrU(v->next))
1185 /* Compare the pattern value to the match expression value */
1186 if (FcConfigCompareValue (&v->value, t->op, &value))
1193 if (t->qual == FcQualAll)
1200 FcValueDestroy (value);
1205 static FcValueList *
1206 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1212 l = (FcValueList *) malloc (sizeof (FcValueList));
1215 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1216 if (e->op == FcOpComma)
1218 l->value = FcConfigEvaluate (p, e->u.tree.left);
1219 l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
1223 l->value = FcConfigEvaluate (p, e);
1224 l->next = FcValueListPtrCreateDynamic(0);
1226 l->binding = binding;
1227 if (l->value.type == FcTypeVoid)
1229 FcValueList *next = FcValueListPtrU(l->next);
1231 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1240 FcConfigAdd (FcValueListPtr *head,
1241 FcValueList *position,
1245 FcValueListPtr *prev, last, v;
1246 FcValueBinding sameBinding;
1249 sameBinding = position->binding;
1251 sameBinding = FcValueBindingWeak;
1252 for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v);
1253 v = FcValueListPtrU(v)->next)
1254 if (FcValueListPtrU(v)->binding == FcValueBindingSame)
1255 FcValueListPtrU(v)->binding = sameBinding;
1259 prev = &position->next;
1261 for (prev = head; FcValueListPtrU(*prev);
1262 prev = &(FcValueListPtrU(*prev)->next))
1269 for (prev = head; FcValueListPtrU(*prev);
1270 prev = &(FcValueListPtrU(*prev)->next))
1272 if (FcValueListPtrU(*prev) == position)
1279 if (FcDebug () & FC_DBG_EDIT)
1281 if (!FcValueListPtrU(*prev))
1282 printf ("position not on list\n");
1286 if (FcDebug () & FC_DBG_EDIT)
1288 printf ("%s list before ", append ? "Append" : "Prepend");
1289 FcValueListPrint (*head);
1295 last = FcValueListPtrCreateDynamic(new);
1296 while (FcValueListPtrU(FcValueListPtrU(last)->next))
1297 last = FcValueListPtrU(last)->next;
1299 FcValueListPtrU(last)->next = *prev;
1300 *prev = FcValueListPtrCreateDynamic(new);
1303 if (FcDebug () & FC_DBG_EDIT)
1305 printf ("%s list after ", append ? "Append" : "Prepend");
1306 FcValueListPrint (*head);
1314 FcConfigDel (FcValueListPtr *head,
1315 FcValueList *position)
1317 FcValueListPtr *prev;
1319 for (prev = head; FcValueListPtrU(*prev);
1320 prev = &(FcValueListPtrU(*prev)->next))
1322 if (FcValueListPtrU(*prev) == position)
1324 *prev = position->next;
1325 position->next = FcValueListPtrCreateDynamic(0);
1326 FcValueListDestroy (FcValueListPtrCreateDynamic(position));
1333 FcConfigPatternAdd (FcPattern *p,
1340 FcPatternElt *e = FcPatternInsertElt (p, object);
1344 FcConfigAdd (&e->values, 0, append, list);
1349 * Delete all values associated with a field
1352 FcConfigPatternDel (FcPattern *p,
1355 FcPatternElt *e = FcPatternFindElt (p, object);
1358 while (FcValueListPtrU(e->values))
1359 FcConfigDel (&e->values, FcValueListPtrU(e->values));
1363 FcConfigPatternCanon (FcPattern *p,
1366 FcPatternElt *e = FcPatternFindElt (p, object);
1369 if (!FcValueListPtrU(e->values))
1370 FcPatternDel (p, object);
1374 FcConfigSubstituteWithPat (FcConfig *config,
1389 config = FcConfigGetCurrent ();
1394 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1395 if (!st && config->maxObjects)
1397 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1399 if (FcDebug () & FC_DBG_EDIT)
1401 printf ("FcConfigSubstitute ");
1404 if (kind == FcMatchPattern)
1405 s = config->substPattern;
1407 s = config->substFont;
1408 for (; s; s = s->next)
1411 * Check the tests to see if
1412 * they all match the pattern
1414 for (t = s->test, i = 0; t; t = t->next, i++)
1416 if (FcDebug () & FC_DBG_EDIT)
1418 printf ("FcConfigSubstitute test ");
1422 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1427 st[i].elt = FcPatternFindElt (m, t->field);
1431 * If there's no such field in the font,
1432 * then FcQualAll matches while FcQualAny does not
1436 if (t->qual == FcQualAll)
1445 * Check to see if there is a match, mark the location
1446 * to apply match-relative edits
1448 st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
1451 if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
1453 if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
1458 if (FcDebug () & FC_DBG_EDIT)
1459 printf ("No match\n");
1462 if (FcDebug () & FC_DBG_EDIT)
1464 printf ("Substitute ");
1467 for (e = s->edit; e; e = e->next)
1470 * Evaluate the list of expressions
1472 l = FcConfigValues (p, e->expr, e->binding);
1474 * Locate any test associated with this field, skipping
1475 * tests associated with the pattern when substituting in
1478 for (t = s->test, i = 0; t; t = t->next, i++)
1480 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1481 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1482 (FcChar8 *) e->field))
1485 * KLUDGE - the pattern may have been reallocated or
1486 * things may have been inserted or deleted above
1487 * this element by other edits. Go back and find
1490 if (e != s->edit && st[i].elt)
1491 st[i].elt = FcPatternFindElt (p, t->field);
1500 * If there was a test, then replace the matched
1501 * value with the new list of values
1505 FcValueList *thisValue = st[i].value;
1506 FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
1509 * Append the new list of values after the current value
1511 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1513 * Delete the marked value
1516 FcConfigDel (&st[i].elt->values, thisValue);
1518 * Adjust any pointers into the value list to ensure
1519 * future edits occur at the same place
1521 for (t = s->test, i = 0; t; t = t->next, i++)
1523 if (st[i].value == thisValue)
1524 st[i].value = nextValue;
1528 /* fall through ... */
1529 case FcOpAssignReplace:
1531 * Delete all of the values and insert
1534 FcConfigPatternDel (p, e->field);
1535 FcConfigPatternAdd (p, e->field, l, FcTrue);
1537 * Adjust any pointers into the value list as they no
1538 * longer point to anything valid
1542 FcPatternElt *thisElt = st[i].elt;
1543 for (t = s->test, i = 0; t; t = t->next, i++)
1545 if (st[i].elt == thisElt)
1553 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1556 /* fall through ... */
1557 case FcOpPrependFirst:
1558 FcConfigPatternAdd (p, e->field, l, FcFalse);
1563 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1566 /* fall through ... */
1567 case FcOpAppendLast:
1568 FcConfigPatternAdd (p, e->field, l, FcTrue);
1571 FcValueListDestroy (FcValueListPtrCreateDynamic(l));
1576 * Now go through the pattern and eliminate
1577 * any properties without data
1579 for (e = s->edit; e; e = e->next)
1580 FcConfigPatternCanon (p, e->field);
1582 if (FcDebug () & FC_DBG_EDIT)
1584 printf ("FcConfigSubstitute edit");
1588 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1590 if (FcDebug () & FC_DBG_EDIT)
1592 printf ("FcConfigSubstitute done");
1599 FcConfigSubstitute (FcConfig *config,
1603 return FcConfigSubstituteWithPat (config, p, 0, kind);
1606 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1608 static FcChar8 fontconfig_path[1000] = "";
1611 DllMain (HINSTANCE hinstDLL,
1617 switch (fdwReason) {
1618 case DLL_PROCESS_ATTACH:
1619 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1620 sizeof (fontconfig_path)))
1623 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1624 * assume it's a Unix-style installation tree, and use
1625 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1626 * folder where the DLL is as FONTCONFIG_PATH.
1628 p = strrchr (fontconfig_path, '\\');
1632 p = strrchr (fontconfig_path, '\\');
1633 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1634 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1636 strcat (fontconfig_path, "\\etc\\fonts");
1639 fontconfig_path[0] = '\0';
1647 #undef FONTCONFIG_PATH
1648 #define FONTCONFIG_PATH fontconfig_path
1650 #else /* !(_WIN32 && PIC) */
1652 #endif /* !(_WIN32 && PIC) */
1654 #ifndef FONTCONFIG_FILE
1655 #define FONTCONFIG_FILE "fonts.conf"
1659 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1664 dir = (FcChar8 *) "";
1665 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1669 strcpy ((char *) path, (const char *) dir);
1670 /* make sure there's a single separator */
1672 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1673 path[strlen((char *) path)-1] != '\\')) &&
1676 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1677 strcat ((char *) path, "\\");
1679 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1680 strcat ((char *) path, "/");
1682 strcat ((char *) path, (char *) file);
1684 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1685 if (access ((char *) path, R_OK) == 0)
1693 FcConfigGetPath (void)
1696 FcChar8 *env, *e, *colon;
1701 npath = 2; /* default dir + null */
1702 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1708 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1711 path = calloc (npath, sizeof (FcChar8 *));
1721 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1723 colon = e + strlen ((char *) e);
1724 path[i] = malloc (colon - e + 1);
1727 strncpy ((char *) path[i], (const char *) e, colon - e);
1728 path[i][colon - e] = '\0';
1737 dir = (FcChar8 *) FONTCONFIG_PATH;
1738 path[i] = malloc (strlen ((char *) dir) + 1);
1741 strcpy ((char *) path[i], (const char *) dir);
1745 for (i = 0; path[i]; i++)
1753 FcConfigFreePath (FcChar8 **path)
1757 for (p = path; *p; p++)
1762 static FcBool _FcConfigHomeEnabled = FcTrue;
1767 if (_FcConfigHomeEnabled)
1769 char *home = getenv ("HOME");
1773 home = getenv ("USERPROFILE");
1776 return (FcChar8 *) home;
1782 FcConfigEnableHome (FcBool enable)
1784 FcBool prev = _FcConfigHomeEnabled;
1785 _FcConfigHomeEnabled = enable;
1790 FcConfigFilename (const FcChar8 *url)
1792 FcChar8 *file, *dir, **path, **p;
1796 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1798 url = (FcChar8 *) FONTCONFIG_FILE;
1803 if (isalpha (*url) &&
1805 (url[2] == '/' || url[2] == '\\'))
1811 dir = FcConfigHome ();
1813 file = FcConfigFileExists (dir, url + 1);
1822 file = FcConfigFileExists (0, url);
1825 path = FcConfigGetPath ();
1828 for (p = path; *p; p++)
1830 file = FcConfigFileExists (*p, url);
1834 FcConfigFreePath (path);
1841 * Manage the application-specific fonts
1845 FcConfigAppFontAddFile (FcConfig *config,
1846 const FcChar8 *file)
1855 config = FcConfigGetCurrent ();
1860 subdirs = FcStrSetCreate ();
1864 set = FcConfigGetFonts (config, FcSetApplication);
1867 set = FcFontSetCreate ();
1870 FcStrSetDestroy (subdirs);
1873 FcConfigSetFonts (config, set, FcSetApplication);
1876 if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
1878 FcStrSetDestroy (subdirs);
1881 if ((sublist = FcStrListCreate (subdirs)))
1883 while ((subdir = FcStrListNext (sublist)))
1885 FcConfigAppFontAddDir (config, subdir);
1887 FcStrListDone (sublist);
1889 FcStrSetDestroy (subdirs);
1894 FcConfigAppFontAddDir (FcConfig *config,
1904 config = FcConfigGetCurrent ();
1908 subdirs = FcStrSetCreate ();
1912 set = FcConfigGetFonts (config, FcSetApplication);
1915 set = FcFontSetCreate ();
1918 FcStrSetDestroy (subdirs);
1921 FcConfigSetFonts (config, set, FcSetApplication);
1924 if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
1926 FcStrSetDestroy (subdirs);
1929 if ((sublist = FcStrListCreate (subdirs)))
1931 while ((subdir = FcStrListNext (sublist)))
1933 FcConfigAppFontAddDir (config, subdir);
1935 FcStrListDone (sublist);
1937 FcStrSetDestroy (subdirs);
1942 FcConfigAppFontClear (FcConfig *config)
1946 config = FcConfigGetCurrent ();
1951 FcConfigSetFonts (config, 0, FcSetApplication);
1955 * Manage filename-based font source selectors
1959 FcConfigGlobAdd (FcConfig *config,
1960 const FcChar8 *glob,
1963 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1965 return FcStrSetAdd (set, glob);
1969 FcConfigGlobMatch (const FcChar8 *glob,
1970 const FcChar8 *string)
1974 while ((c = *glob++))
1978 /* short circuit common case */
1981 /* short circuit another common case */
1982 if (strchr ((char *) glob, '*') == 0)
1983 string += strlen ((char *) string) - strlen ((char *) glob);
1986 if (FcConfigGlobMatch (glob, string))
1992 if (*string++ == '\0')
2001 return *string == '\0';
2005 FcConfigGlobsMatch (const FcStrSet *globs,
2006 const FcChar8 *string)
2010 for (i = 0; i < globs->num; i++)
2011 if (FcConfigGlobMatch (globs->strs[i], string))
2017 FcConfigAcceptFilename (FcConfig *config,
2018 const FcChar8 *filename)
2020 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2022 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2028 * Manage font-pattern based font source selectors
2032 FcConfigPatternsAdd (FcConfig *config,
2036 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2038 return FcFontSetAdd (set, pattern);
2042 FcConfigPatternsMatch (const FcFontSet *patterns,
2043 const FcPattern *font)
2047 for (i = 0; i < patterns->nfont; i++)
2048 if (FcListPatternMatchAny (patterns->fonts[i], font))
2054 FcConfigAcceptFont (FcConfig *config,
2055 const FcPattern *font)
2057 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2059 if (FcConfigPatternsMatch (config->rejectPatterns, font))