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)
80 config->cacheDirs = FcStrSetCreate ();
81 if (!config->cacheDirs)
86 config->substPattern = 0;
87 config->substFont = 0;
88 config->substScan = 0;
89 config->maxObjects = 0;
90 for (set = FcSetSystem; set <= FcSetApplication; set++)
91 config->fonts[set] = 0;
93 config->rescanTime = time(0);
94 config->rescanInterval = 30;
99 FcFontSetDestroy (config->rejectPatterns);
101 FcFontSetDestroy (config->acceptPatterns);
103 FcStrSetDestroy (config->rejectGlobs);
105 FcStrSetDestroy (config->acceptGlobs);
107 FcStrSetDestroy (config->fontDirs);
109 FcStrSetDestroy (config->configFiles);
111 FcStrSetDestroy (config->configDirs);
114 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
120 FcConfigNewestFile (FcStrSet *files)
122 FcStrList *list = FcStrListCreate (files);
123 FcFileTime newest = { 0, FcFalse };
129 while ((file = FcStrListNext (list)))
130 if (stat ((char *) file, &statb) == 0)
131 if (!newest.set || statb.st_mtime - newest.time > 0)
134 newest.time = statb.st_mtime;
136 FcStrListDone (list);
142 FcConfigModifiedTime (FcConfig *config)
146 FcFileTime v = { 0, FcFalse };
147 config = FcConfigGetCurrent ();
151 return FcConfigNewestFile (config->configFiles);
155 FcConfigUptoDate (FcConfig *config)
157 FcFileTime config_time, font_time;
158 time_t now = time(0);
161 config = FcConfigGetCurrent ();
165 config_time = FcConfigNewestFile (config->configFiles);
166 font_time = FcConfigNewestFile (config->fontDirs);
167 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
168 (font_time.set && (font_time.time - config->rescanTime) > 0))
172 config->rescanTime = now;
177 FcSubstDestroy (FcSubst *s)
185 FcTestDestroy (s->test);
187 FcEditDestroy (s->edit);
189 FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
195 FcConfigDestroy (FcConfig *config)
199 if (config == _fcConfig)
202 FcStrSetDestroy (config->configDirs);
203 FcStrSetDestroy (config->fontDirs);
204 FcStrSetDestroy (config->cacheDirs);
205 FcStrSetDestroy (config->configFiles);
206 FcStrSetDestroy (config->acceptGlobs);
207 FcStrSetDestroy (config->rejectGlobs);
208 FcFontSetDestroy (config->acceptPatterns);
209 FcFontSetDestroy (config->rejectPatterns);
212 FcBlanksDestroy (config->blanks);
214 FcSubstDestroy (config->substPattern);
215 FcSubstDestroy (config->substFont);
216 FcSubstDestroy (config->substScan);
217 for (set = FcSetSystem; set <= FcSetApplication; set++)
218 if (config->fonts[set])
219 FcFontSetDestroy (config->fonts[set]);
222 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
226 * Add cache to configuration, adding fonts and directories
230 FcConfigAddCache (FcConfig *config, FcCache *cache,
231 FcSetName set, FcStrSet *dirSet)
240 fs = FcCacheSet (cache);
245 for (i = 0; i < fs->nfont; i++)
247 FcPattern *font = FcFontSetFont (fs, i);
251 * Check to see if font is banned by filename
253 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
254 0, &font_file) == FcResultMatch &&
255 !FcConfigAcceptFilename (config, font_file))
261 * Check to see if font is banned by pattern
263 if (!FcConfigAcceptFont (config, font))
267 FcFontSetAdd (config->fonts[set], font);
269 FcDirCacheReference (cache, nref);
275 dirs = FcCacheDirs (cache);
278 for (i = 0; i < cache->dirs_count; i++)
280 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
281 if (FcConfigAcceptFilename (config, dir))
282 FcStrSetAddFilename (dirSet, dir);
289 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
295 dirlist = FcStrListCreate (dirSet);
299 while ((dir = FcStrListNext (dirlist)))
301 if (FcDebug () & FC_DBG_FONTSET)
302 printf ("adding fonts from%s\n", dir);
303 cache = FcDirCacheRead (dir, FcFalse, config);
306 FcConfigAddCache (config, cache, set, dirSet);
307 FcDirCacheUnload (cache);
309 FcStrListDone (dirlist);
314 * Scan the current list of directories in the configuration
315 * and build the set of available fonts.
319 FcConfigBuildFonts (FcConfig *config)
325 config = FcConfigGetCurrent ();
330 fonts = FcFontSetCreate ();
334 FcConfigSetFonts (config, fonts, FcSetSystem);
336 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
338 if (FcDebug () & FC_DBG_FONTSET)
339 FcFontSetPrint (fonts);
344 FcConfigSetCurrent (FcConfig *config)
347 if (!FcConfigBuildFonts (config))
351 FcConfigDestroy (_fcConfig);
357 FcConfigGetCurrent (void)
366 FcConfigAddConfigDir (FcConfig *config,
369 return FcStrSetAddFilename (config->configDirs, d);
373 FcConfigGetConfigDirs (FcConfig *config)
377 config = FcConfigGetCurrent ();
381 return FcStrListCreate (config->configDirs);
385 FcConfigAddFontDir (FcConfig *config,
388 return FcStrSetAddFilename (config->fontDirs, d);
392 FcConfigAddDir (FcConfig *config,
395 return (FcConfigAddConfigDir (config, d) &&
396 FcConfigAddFontDir (config, d));
400 FcConfigGetFontDirs (FcConfig *config)
404 config = FcConfigGetCurrent ();
408 return FcStrListCreate (config->fontDirs);
412 FcConfigAddCacheDir (FcConfig *config,
415 return FcStrSetAddFilename (config->cacheDirs, d);
419 FcConfigGetCacheDirs (FcConfig *config)
423 config = FcConfigGetCurrent ();
427 return FcStrListCreate (config->cacheDirs);
431 FcConfigAddConfigFile (FcConfig *config,
435 FcChar8 *file = FcConfigFilename (f);
440 ret = FcStrSetAdd (config->configFiles, file);
446 FcConfigGetConfigFiles (FcConfig *config)
450 config = FcConfigGetCurrent ();
454 return FcStrListCreate (config->configFiles);
458 FcConfigGetCache (FcConfig *config)
464 FcConfigGetFonts (FcConfig *config,
469 config = FcConfigGetCurrent ();
473 return config->fonts[set];
477 FcConfigSetFonts (FcConfig *config,
481 if (config->fonts[set])
482 FcFontSetDestroy (config->fonts[set]);
483 config->fonts[set] = fonts;
487 FcConfigGetBlanks (FcConfig *config)
491 config = FcConfigGetCurrent ();
495 return config->blanks;
499 FcConfigAddBlank (FcConfig *config,
502 FcBlanks *b, *freeme = 0;
507 freeme = b = FcBlanksCreate ();
511 if (!FcBlanksAdd (b, blank))
514 FcBlanksDestroy (freeme);
522 FcConfigGetRescanInverval (FcConfig *config)
526 config = FcConfigGetCurrent ();
530 return config->rescanInterval;
534 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
538 config = FcConfigGetCurrent ();
542 config->rescanInterval = rescanInterval;
547 FcConfigAddEdit (FcConfig *config,
552 FcSubst *subst, **prev;
558 prev = &config->substPattern;
561 prev = &config->substFont;
564 prev = &config->substScan;
569 subst = (FcSubst *) malloc (sizeof (FcSubst));
572 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
573 for (; *prev; prev = &(*prev)->next);
579 for (t = test; t; t = t->next)
581 if (t->kind == FcMatchDefault)
585 if (config->maxObjects < num)
586 config->maxObjects = num;
587 if (FcDebug () & FC_DBG_EDIT)
589 printf ("Add Subst ");
590 FcSubstPrint (subst);
595 typedef struct _FcSubState {
601 FcConfigPromote (FcValue v, FcValue u)
603 if (v.type == FcTypeInteger)
605 v.type = FcTypeDouble;
606 v.u.d = (double) v.u.i;
608 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
610 v.u.m = &FcIdentityMatrix;
611 v.type = FcTypeMatrix;
613 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
615 v.u.l = FcLangSetPromote (v.u.s);
616 v.type = FcTypeLangSet;
622 FcConfigCompareValue (const FcValue *left_o,
624 const FcValue *right_o)
626 FcValue left = FcValueCanonicalize(left_o);
627 FcValue right = FcValueCanonicalize(right_o);
628 FcBool ret = FcFalse;
630 left = FcConfigPromote (left, right);
631 right = FcConfigPromote (right, left);
632 if (left.type == right.type)
636 break; /* FcConfigPromote prevents this from happening */
642 ret = left.u.d == right.u.d;
645 case FcOpNotContains:
646 ret = left.u.d != right.u.d;
649 ret = left.u.d < right.u.d;
652 ret = left.u.d <= right.u.d;
655 ret = left.u.d > right.u.d;
658 ret = left.u.d >= right.u.d;
669 ret = left.u.b == right.u.b;
672 case FcOpNotContains:
673 ret = left.u.b != right.u.b;
683 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
686 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
689 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
691 case FcOpNotContains:
692 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
703 ret = FcMatrixEqual (left.u.m, right.u.m);
706 case FcOpNotContains:
707 ret = !FcMatrixEqual (left.u.m, right.u.m);
717 /* left contains right if right is a subset of left */
718 ret = FcCharSetIsSubset (right.u.c, left.u.c);
720 case FcOpNotContains:
721 /* left contains right if right is a subset of left */
722 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
725 ret = FcCharSetEqual (left.u.c, right.u.c);
728 ret = !FcCharSetEqual (left.u.c, right.u.c);
738 ret = FcLangSetContains (left.u.l, right.u.l);
740 case FcOpNotContains:
741 ret = !FcLangSetContains (left.u.l, right.u.l);
744 ret = FcLangSetEqual (left.u.l, right.u.l);
747 ret = !FcLangSetEqual (left.u.l, right.u.l);
769 ret = left.u.f == right.u.f;
772 case FcOpNotContains:
773 ret = left.u.f != right.u.f;
783 if (op == FcOpNotEqual || op == FcOpNotContains)
790 #define _FcDoubleFloor(d) ((int) (d))
791 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
792 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
793 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
794 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
795 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
798 FcConfigEvaluate (FcPattern *p, FcExpr *e)
807 v.type = FcTypeInteger;
811 v.type = FcTypeDouble;
815 v.type = FcTypeString;
816 v.u.s = FcStrStaticName(e->u.sval);
819 v.type = FcTypeMatrix;
824 v.type = FcTypeCharSet;
833 r = FcPatternObjectGet (p, e->u.object, 0, &v);
834 if (r != FcResultMatch)
839 if (FcNameConstant (e->u.constant, &v.u.i))
840 v.type = FcTypeInteger;
845 vl = FcConfigEvaluate (p, e->u.tree.left);
846 if (vl.type == FcTypeBool)
849 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
851 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
864 case FcOpNotContains:
866 vl = FcConfigEvaluate (p, e->u.tree.left);
867 vr = FcConfigEvaluate (p, e->u.tree.right);
869 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
879 vl = FcConfigEvaluate (p, e->u.tree.left);
880 vr = FcConfigEvaluate (p, e->u.tree.right);
881 vl = FcConfigPromote (vl, vr);
882 vr = FcConfigPromote (vr, vl);
883 if (vl.type == vr.type)
889 v.type = FcTypeDouble;
890 v.u.d = vl.u.d + vr.u.d;
893 v.type = FcTypeDouble;
894 v.u.d = vl.u.d - vr.u.d;
897 v.type = FcTypeDouble;
898 v.u.d = vl.u.d * vr.u.d;
901 v.type = FcTypeDouble;
902 v.u.d = vl.u.d / vr.u.d;
908 if (v.type == FcTypeDouble &&
909 v.u.d == (double) (int) v.u.d)
911 v.type = FcTypeInteger;
919 v.u.b = vl.u.b || vr.u.b;
923 v.u.b = vl.u.b && vr.u.b;
933 v.type = FcTypeString;
934 str = FcStrPlus (vl.u.s, vr.u.s);
935 v.u.s = FcStrStaticName (str);
949 v.type = FcTypeMatrix;
950 m = malloc (sizeof (FcMatrix));
953 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
954 FcMatrixMultiply (m, vl.u.m, vr.u.m);
978 vl = FcConfigEvaluate (p, e->u.tree.left);
991 vl = FcConfigEvaluate (p, e->u.tree.left);
997 v.type = FcTypeInteger;
998 v.u.i = FcDoubleFloor (vl.u.d);
1001 v.type = FcTypeVoid;
1004 FcValueDestroy (vl);
1007 vl = FcConfigEvaluate (p, e->u.tree.left);
1013 v.type = FcTypeInteger;
1014 v.u.i = FcDoubleCeil (vl.u.d);
1017 v.type = FcTypeVoid;
1020 FcValueDestroy (vl);
1023 vl = FcConfigEvaluate (p, e->u.tree.left);
1029 v.type = FcTypeInteger;
1030 v.u.i = FcDoubleRound (vl.u.d);
1033 v.type = FcTypeVoid;
1036 FcValueDestroy (vl);
1039 vl = FcConfigEvaluate (p, e->u.tree.left);
1045 v.type = FcTypeInteger;
1046 v.u.i = FcDoubleTrunc (vl.u.d);
1049 v.type = FcTypeVoid;
1052 FcValueDestroy (vl);
1055 v.type = FcTypeVoid;
1061 static FcValueList *
1062 FcConfigMatchValueList (FcPattern *p,
1064 FcValueList *values)
1066 FcValueList *ret = 0;
1067 FcExpr *e = t->expr;
1073 /* Compute the value of the match expression */
1074 if (e->op == FcOpComma)
1076 value = FcConfigEvaluate (p, e->u.tree.left);
1077 e = e->u.tree.right;
1081 value = FcConfigEvaluate (p, e);
1085 for (v = values; v; v = FcValueListNext(v))
1087 /* Compare the pattern value to the match expression value */
1088 if (FcConfigCompareValue (&v->value, t->op, &value))
1095 if (t->qual == FcQualAll)
1102 FcValueDestroy (value);
1107 static FcValueList *
1108 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1114 l = (FcValueList *) malloc (sizeof (FcValueList));
1117 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1118 if (e->op == FcOpComma)
1120 l->value = FcConfigEvaluate (p, e->u.tree.left);
1121 l->next = FcConfigValues (p, e->u.tree.right, binding);
1125 l->value = FcConfigEvaluate (p, e);
1128 l->binding = binding;
1129 if (l->value.type == FcTypeVoid)
1131 FcValueList *next = FcValueListNext(l);
1133 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1142 FcConfigAdd (FcValueListPtr *head,
1143 FcValueList *position,
1147 FcValueListPtr *prev, last, v;
1148 FcValueBinding sameBinding;
1151 sameBinding = position->binding;
1153 sameBinding = FcValueBindingWeak;
1154 for (v = new; v != NULL; v = FcValueListNext(v))
1155 if (v->binding == FcValueBindingSame)
1156 v->binding = sameBinding;
1160 prev = &position->next;
1162 for (prev = head; *prev != NULL;
1163 prev = &(*prev)->next)
1170 for (prev = head; *prev != NULL;
1171 prev = &(*prev)->next)
1173 if (*prev == position)
1180 if (FcDebug () & FC_DBG_EDIT)
1183 printf ("position not on list\n");
1187 if (FcDebug () & FC_DBG_EDIT)
1189 printf ("%s list before ", append ? "Append" : "Prepend");
1190 FcValueListPrint (*head);
1197 while (last->next != NULL)
1204 if (FcDebug () & FC_DBG_EDIT)
1206 printf ("%s list after ", append ? "Append" : "Prepend");
1207 FcValueListPrint (*head);
1215 FcConfigDel (FcValueListPtr *head,
1216 FcValueList *position)
1218 FcValueListPtr *prev;
1220 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1222 if (*prev == position)
1224 *prev = position->next;
1225 position->next = NULL;
1226 FcValueListDestroy (position);
1233 FcConfigPatternAdd (FcPattern *p,
1240 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1244 FcConfigAdd (&e->values, 0, append, list);
1249 * Delete all values associated with a field
1252 FcConfigPatternDel (FcPattern *p,
1255 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1258 while (e->values != NULL)
1259 FcConfigDel (&e->values, e->values);
1263 FcConfigPatternCanon (FcPattern *p,
1266 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1269 if (e->values == NULL)
1270 FcPatternObjectDel (p, object);
1274 FcConfigSubstituteWithPat (FcConfig *config,
1289 config = FcConfigGetCurrent ();
1295 case FcMatchPattern:
1296 s = config->substPattern;
1299 s = config->substFont;
1302 s = config->substScan;
1308 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1309 if (!st && config->maxObjects)
1311 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1313 if (FcDebug () & FC_DBG_EDIT)
1315 printf ("FcConfigSubstitute ");
1318 for (; s; s = s->next)
1321 * Check the tests to see if
1322 * they all match the pattern
1324 for (t = s->test, i = 0; t; t = t->next, i++)
1326 if (FcDebug () & FC_DBG_EDIT)
1328 printf ("FcConfigSubstitute test ");
1332 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1337 st[i].elt = FcPatternObjectFindElt (m, t->object);
1341 * If there's no such field in the font,
1342 * then FcQualAll matches while FcQualAny does not
1346 if (t->qual == FcQualAll)
1355 * Check to see if there is a match, mark the location
1356 * to apply match-relative edits
1358 st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
1361 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1363 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1368 if (FcDebug () & FC_DBG_EDIT)
1369 printf ("No match\n");
1372 if (FcDebug () & FC_DBG_EDIT)
1374 printf ("Substitute ");
1377 for (e = s->edit; e; e = e->next)
1380 * Evaluate the list of expressions
1382 l = FcConfigValues (p, e->expr, e->binding);
1384 * Locate any test associated with this field, skipping
1385 * tests associated with the pattern when substituting in
1388 for (t = s->test, i = 0; t; t = t->next, i++)
1390 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1391 t->object == e->object)
1394 * KLUDGE - the pattern may have been reallocated or
1395 * things may have been inserted or deleted above
1396 * this element by other edits. Go back and find
1399 if (e != s->edit && st[i].elt)
1400 st[i].elt = FcPatternObjectFindElt (p, t->object);
1409 * If there was a test, then replace the matched
1410 * value with the new list of values
1414 FcValueList *thisValue = st[i].value;
1415 FcValueList *nextValue = thisValue;
1418 * Append the new list of values after the current value
1420 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1422 * Delete the marked value
1425 FcConfigDel (&st[i].elt->values, thisValue);
1427 * Adjust any pointers into the value list to ensure
1428 * future edits occur at the same place
1430 for (t = s->test, i = 0; t; t = t->next, i++)
1432 if (st[i].value == thisValue)
1433 st[i].value = nextValue;
1437 /* fall through ... */
1438 case FcOpAssignReplace:
1440 * Delete all of the values and insert
1443 FcConfigPatternDel (p, e->object);
1444 FcConfigPatternAdd (p, e->object, l, FcTrue);
1446 * Adjust any pointers into the value list as they no
1447 * longer point to anything valid
1451 FcPatternElt *thisElt = st[i].elt;
1452 for (t = s->test, i = 0; t; t = t->next, i++)
1454 if (st[i].elt == thisElt)
1462 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1465 /* fall through ... */
1466 case FcOpPrependFirst:
1467 FcConfigPatternAdd (p, e->object, l, FcFalse);
1472 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1475 /* fall through ... */
1476 case FcOpAppendLast:
1477 FcConfigPatternAdd (p, e->object, l, FcTrue);
1480 FcValueListDestroy (l);
1485 * Now go through the pattern and eliminate
1486 * any properties without data
1488 for (e = s->edit; e; e = e->next)
1489 FcConfigPatternCanon (p, e->object);
1491 if (FcDebug () & FC_DBG_EDIT)
1493 printf ("FcConfigSubstitute edit");
1497 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1499 if (FcDebug () & FC_DBG_EDIT)
1501 printf ("FcConfigSubstitute done");
1508 FcConfigSubstitute (FcConfig *config,
1512 return FcConfigSubstituteWithPat (config, p, 0, kind);
1515 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1517 static FcChar8 fontconfig_path[1000] = "";
1520 DllMain (HINSTANCE hinstDLL,
1526 switch (fdwReason) {
1527 case DLL_PROCESS_ATTACH:
1528 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1529 sizeof (fontconfig_path)))
1532 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1533 * assume it's a Unix-style installation tree, and use
1534 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1535 * folder where the DLL is as FONTCONFIG_PATH.
1537 p = strrchr (fontconfig_path, '\\');
1541 p = strrchr (fontconfig_path, '\\');
1542 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1543 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1545 strcat (fontconfig_path, "\\etc\\fonts");
1548 fontconfig_path[0] = '\0';
1556 #undef FONTCONFIG_PATH
1557 #define FONTCONFIG_PATH fontconfig_path
1559 #else /* !(_WIN32 && PIC) */
1561 #endif /* !(_WIN32 && PIC) */
1563 #ifndef FONTCONFIG_FILE
1564 #define FONTCONFIG_FILE "fonts.conf"
1568 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1573 dir = (FcChar8 *) "";
1574 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1578 strcpy ((char *) path, (const char *) dir);
1579 /* make sure there's a single separator */
1581 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1582 path[strlen((char *) path)-1] != '\\')) &&
1585 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1586 strcat ((char *) path, "\\");
1588 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1589 strcat ((char *) path, "/");
1591 strcat ((char *) path, (char *) file);
1593 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1594 if (access ((char *) path, R_OK) == 0)
1602 FcConfigGetPath (void)
1605 FcChar8 *env, *e, *colon;
1610 npath = 2; /* default dir + null */
1611 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1617 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1620 path = calloc (npath, sizeof (FcChar8 *));
1630 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1632 colon = e + strlen ((char *) e);
1633 path[i] = malloc (colon - e + 1);
1636 strncpy ((char *) path[i], (const char *) e, colon - e);
1637 path[i][colon - e] = '\0';
1646 dir = (FcChar8 *) FONTCONFIG_PATH;
1647 path[i] = malloc (strlen ((char *) dir) + 1);
1650 strcpy ((char *) path[i], (const char *) dir);
1654 for (i = 0; path[i]; i++)
1662 FcConfigFreePath (FcChar8 **path)
1666 for (p = path; *p; p++)
1671 static FcBool _FcConfigHomeEnabled = FcTrue;
1676 if (_FcConfigHomeEnabled)
1678 char *home = getenv ("HOME");
1682 home = getenv ("USERPROFILE");
1685 return (FcChar8 *) home;
1691 FcConfigEnableHome (FcBool enable)
1693 FcBool prev = _FcConfigHomeEnabled;
1694 _FcConfigHomeEnabled = enable;
1699 FcConfigFilename (const FcChar8 *url)
1701 FcChar8 *file, *dir, **path, **p;
1705 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1707 url = (FcChar8 *) FONTCONFIG_FILE;
1712 if (isalpha (*url) &&
1714 (url[2] == '/' || url[2] == '\\'))
1720 dir = FcConfigHome ();
1722 file = FcConfigFileExists (dir, url + 1);
1731 file = FcConfigFileExists (0, url);
1734 path = FcConfigGetPath ();
1737 for (p = path; *p; p++)
1739 file = FcConfigFileExists (*p, url);
1743 FcConfigFreePath (path);
1750 * Manage the application-specific fonts
1754 FcConfigAppFontAddFile (FcConfig *config,
1755 const FcChar8 *file)
1764 config = FcConfigGetCurrent ();
1769 subdirs = FcStrSetCreate ();
1773 set = FcConfigGetFonts (config, FcSetApplication);
1776 set = FcFontSetCreate ();
1779 FcStrSetDestroy (subdirs);
1782 FcConfigSetFonts (config, set, FcSetApplication);
1785 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
1787 FcStrSetDestroy (subdirs);
1790 if ((sublist = FcStrListCreate (subdirs)))
1792 while ((subdir = FcStrListNext (sublist)))
1794 FcConfigAppFontAddDir (config, subdir);
1796 FcStrListDone (sublist);
1798 FcStrSetDestroy (subdirs);
1803 FcConfigAppFontAddDir (FcConfig *config,
1811 config = FcConfigGetCurrent ();
1816 dirs = FcStrSetCreate ();
1820 set = FcConfigGetFonts (config, FcSetApplication);
1823 set = FcFontSetCreate ();
1826 FcStrSetDestroy (dirs);
1829 FcConfigSetFonts (config, set, FcSetApplication);
1832 FcStrSetAddFilename (dirs, dir);
1834 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
1836 FcStrSetDestroy (dirs);
1839 FcStrSetDestroy (dirs);
1844 FcConfigAppFontClear (FcConfig *config)
1848 config = FcConfigGetCurrent ();
1853 FcConfigSetFonts (config, 0, FcSetApplication);
1857 * Manage filename-based font source selectors
1861 FcConfigGlobAdd (FcConfig *config,
1862 const FcChar8 *glob,
1865 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1867 return FcStrSetAdd (set, glob);
1871 FcConfigGlobMatch (const FcChar8 *glob,
1872 const FcChar8 *string)
1876 while ((c = *glob++))
1880 /* short circuit common case */
1883 /* short circuit another common case */
1884 if (strchr ((char *) glob, '*') == 0)
1885 string += strlen ((char *) string) - strlen ((char *) glob);
1888 if (FcConfigGlobMatch (glob, string))
1894 if (*string++ == '\0')
1903 return *string == '\0';
1907 FcConfigGlobsMatch (const FcStrSet *globs,
1908 const FcChar8 *string)
1912 for (i = 0; i < globs->num; i++)
1913 if (FcConfigGlobMatch (globs->strs[i], string))
1919 FcConfigAcceptFilename (FcConfig *config,
1920 const FcChar8 *filename)
1922 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1924 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1930 * Manage font-pattern based font source selectors
1934 FcConfigPatternsAdd (FcConfig *config,
1938 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1940 return FcFontSetAdd (set, pattern);
1944 FcConfigPatternsMatch (const FcFontSet *patterns,
1945 const FcPattern *font)
1949 for (i = 0; i < patterns->nfont; i++)
1950 if (FcListPatternMatchAny (patterns->fonts[i], font))
1956 FcConfigAcceptFont (FcConfig *config,
1957 const FcPattern *font)
1959 if (FcConfigPatternsMatch (config->acceptPatterns, font))
1961 if (FcConfigPatternsMatch (config->rejectPatterns, font))
1966 #include "fcaliastail.h"