2 * fontconfig/src/fccfg.c
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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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;
96 config->expr_pool = NULL;
103 FcFontSetDestroy (config->rejectPatterns);
105 FcFontSetDestroy (config->acceptPatterns);
107 FcStrSetDestroy (config->rejectGlobs);
109 FcStrSetDestroy (config->acceptGlobs);
111 FcStrSetDestroy (config->fontDirs);
113 FcStrSetDestroy (config->configFiles);
115 FcStrSetDestroy (config->configDirs);
118 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
124 FcConfigNewestFile (FcStrSet *files)
126 FcStrList *list = FcStrListCreate (files);
127 FcFileTime newest = { 0, FcFalse };
133 while ((file = FcStrListNext (list)))
134 if (FcStat ((char *) file, &statb) == 0)
135 if (!newest.set || statb.st_mtime - newest.time > 0)
138 newest.time = statb.st_mtime;
140 FcStrListDone (list);
146 FcConfigUptoDate (FcConfig *config)
148 FcFileTime config_time, config_dir_time, font_time;
149 time_t now = time(0);
152 config = FcConfigGetCurrent ();
156 config_time = FcConfigNewestFile (config->configFiles);
157 config_dir_time = FcConfigNewestFile (config->configDirs);
158 font_time = FcConfigNewestFile (config->fontDirs);
159 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
160 (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
161 (font_time.set && (font_time.time - config->rescanTime) > 0))
163 /* We need to check for potential clock problems here (OLPC ticket #6046) */
164 if ((config_time.set && (config_time.time - now) > 0) ||
165 (config_dir_time.set && (config_dir_time.time - now) > 0) ||
166 (font_time.set && (font_time.time - now) > 0))
169 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
170 config->rescanTime = now;
176 config->rescanTime = now;
181 FcSubstDestroy (FcSubst *s)
189 FcTestDestroy (s->test);
191 FcEditDestroy (s->edit);
193 FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
199 FcConfigAllocExpr (FcConfig *config)
201 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
203 FcExprPage *new_page;
205 new_page = malloc (sizeof (FcExprPage));
208 FcMemAlloc (FC_MEM_EXPR, sizeof (FcExprPage));
210 new_page->next_page = config->expr_pool;
211 new_page->next = new_page->exprs;
212 config->expr_pool = new_page;
215 return config->expr_pool->next++;
219 FcConfigReference (FcConfig *config)
223 config = FcConfigGetCurrent ();
234 FcConfigDestroy (FcConfig *config)
239 if (--config->ref > 0)
242 if (config == _fcConfig)
245 FcStrSetDestroy (config->configDirs);
246 FcStrSetDestroy (config->fontDirs);
247 FcStrSetDestroy (config->cacheDirs);
248 FcStrSetDestroy (config->configFiles);
249 FcStrSetDestroy (config->acceptGlobs);
250 FcStrSetDestroy (config->rejectGlobs);
251 FcFontSetDestroy (config->acceptPatterns);
252 FcFontSetDestroy (config->rejectPatterns);
255 FcBlanksDestroy (config->blanks);
257 FcSubstDestroy (config->substPattern);
258 FcSubstDestroy (config->substFont);
259 FcSubstDestroy (config->substScan);
260 for (set = FcSetSystem; set <= FcSetApplication; set++)
261 if (config->fonts[set])
262 FcFontSetDestroy (config->fonts[set]);
264 page = config->expr_pool;
267 FcExprPage *next = page->next_page;
268 FcMemFree (FC_MEM_EXPR, sizeof (FcExprPage));
274 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
278 * Add cache to configuration, adding fonts and directories
282 FcConfigAddCache (FcConfig *config, FcCache *cache,
283 FcSetName set, FcStrSet *dirSet)
292 fs = FcCacheSet (cache);
297 for (i = 0; i < fs->nfont; i++)
299 FcPattern *font = FcFontSetFont (fs, i);
303 * Check to see if font is banned by filename
305 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
306 0, &font_file) == FcResultMatch &&
307 !FcConfigAcceptFilename (config, font_file))
313 * Check to see if font is banned by pattern
315 if (!FcConfigAcceptFont (config, font))
319 FcFontSetAdd (config->fonts[set], font);
321 FcDirCacheReference (cache, nref);
327 dirs = FcCacheDirs (cache);
330 for (i = 0; i < cache->dirs_count; i++)
332 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
333 if (FcConfigAcceptFilename (config, dir))
334 FcStrSetAddFilename (dirSet, dir);
341 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
347 dirlist = FcStrListCreate (dirSet);
351 while ((dir = FcStrListNext (dirlist)))
353 if (FcDebug () & FC_DBG_FONTSET)
354 printf ("adding fonts from%s\n", dir);
355 cache = FcDirCacheRead (dir, FcFalse, config);
358 FcConfigAddCache (config, cache, set, dirSet);
359 FcDirCacheUnload (cache);
361 FcStrListDone (dirlist);
366 * Scan the current list of directories in the configuration
367 * and build the set of available fonts.
371 FcConfigBuildFonts (FcConfig *config)
377 config = FcConfigGetCurrent ();
382 fonts = FcFontSetCreate ();
386 FcConfigSetFonts (config, fonts, FcSetSystem);
388 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
390 if (FcDebug () & FC_DBG_FONTSET)
391 FcFontSetPrint (fonts);
396 FcConfigSetCurrent (FcConfig *config)
398 if (config == _fcConfig)
402 if (!FcConfigBuildFonts (config))
406 FcConfigDestroy (_fcConfig);
412 FcConfigGetCurrent (void)
421 FcConfigAddConfigDir (FcConfig *config,
424 return FcStrSetAddFilename (config->configDirs, d);
428 FcConfigGetConfigDirs (FcConfig *config)
432 config = FcConfigGetCurrent ();
436 return FcStrListCreate (config->configDirs);
440 FcConfigAddFontDir (FcConfig *config,
443 return FcStrSetAddFilename (config->fontDirs, d);
447 FcConfigAddDir (FcConfig *config,
450 return (FcConfigAddConfigDir (config, d) &&
451 FcConfigAddFontDir (config, d));
455 FcConfigGetFontDirs (FcConfig *config)
459 config = FcConfigGetCurrent ();
463 return FcStrListCreate (config->fontDirs);
467 FcConfigAddCacheDir (FcConfig *config,
470 return FcStrSetAddFilename (config->cacheDirs, d);
474 FcConfigGetCacheDirs (FcConfig *config)
478 config = FcConfigGetCurrent ();
482 return FcStrListCreate (config->cacheDirs);
486 FcConfigAddConfigFile (FcConfig *config,
490 FcChar8 *file = FcConfigFilename (f);
495 ret = FcStrSetAdd (config->configFiles, file);
501 FcConfigGetConfigFiles (FcConfig *config)
505 config = FcConfigGetCurrent ();
509 return FcStrListCreate (config->configFiles);
513 FcConfigGetCache (FcConfig *config)
519 FcConfigGetFonts (FcConfig *config,
524 config = FcConfigGetCurrent ();
528 return config->fonts[set];
532 FcConfigSetFonts (FcConfig *config,
536 if (config->fonts[set])
537 FcFontSetDestroy (config->fonts[set]);
538 config->fonts[set] = fonts;
542 FcConfigGetBlanks (FcConfig *config)
546 config = FcConfigGetCurrent ();
550 return config->blanks;
554 FcConfigAddBlank (FcConfig *config,
557 FcBlanks *b, *freeme = 0;
562 freeme = b = FcBlanksCreate ();
566 if (!FcBlanksAdd (b, blank))
569 FcBlanksDestroy (freeme);
577 FcConfigGetRescanInterval (FcConfig *config)
581 config = FcConfigGetCurrent ();
585 return config->rescanInterval;
589 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
593 config = FcConfigGetCurrent ();
597 config->rescanInterval = rescanInterval;
602 * A couple of typos escaped into the library
605 FcConfigGetRescanInverval (FcConfig *config)
607 return FcConfigGetRescanInterval (config);
611 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
613 return FcConfigSetRescanInterval (config, rescanInterval);
618 FcConfigAddEdit (FcConfig *config,
623 FcSubst *subst, **prev;
629 prev = &config->substPattern;
632 prev = &config->substFont;
635 prev = &config->substScan;
640 subst = (FcSubst *) malloc (sizeof (FcSubst));
643 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
644 for (; *prev; prev = &(*prev)->next);
650 for (t = test; t; t = t->next)
652 if (t->kind == FcMatchDefault)
656 if (config->maxObjects < num)
657 config->maxObjects = num;
658 if (FcDebug () & FC_DBG_EDIT)
660 printf ("Add Subst ");
661 FcSubstPrint (subst);
666 typedef struct _FcSubState {
672 FcConfigPromote (FcValue v, FcValue u)
674 if (v.type == FcTypeInteger)
676 v.type = FcTypeDouble;
677 v.u.d = (double) v.u.i;
679 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
681 v.u.m = &FcIdentityMatrix;
682 v.type = FcTypeMatrix;
684 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
686 v.u.l = FcLangSetPromote (v.u.s);
687 v.type = FcTypeLangSet;
693 FcConfigCompareValue (const FcValue *left_o,
695 const FcValue *right_o)
697 FcValue left = FcValueCanonicalize(left_o);
698 FcValue right = FcValueCanonicalize(right_o);
699 FcBool ret = FcFalse;
701 left = FcConfigPromote (left, right);
702 right = FcConfigPromote (right, left);
703 if (left.type == right.type)
707 break; /* FcConfigPromote prevents this from happening */
713 ret = left.u.d == right.u.d;
716 case FcOpNotContains:
717 ret = left.u.d != right.u.d;
720 ret = left.u.d < right.u.d;
723 ret = left.u.d <= right.u.d;
726 ret = left.u.d > right.u.d;
729 ret = left.u.d >= right.u.d;
740 ret = left.u.b == right.u.b;
743 case FcOpNotContains:
744 ret = left.u.b != right.u.b;
754 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
757 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
760 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
762 case FcOpNotContains:
763 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
774 ret = FcMatrixEqual (left.u.m, right.u.m);
777 case FcOpNotContains:
778 ret = !FcMatrixEqual (left.u.m, right.u.m);
788 /* left contains right if right is a subset of left */
789 ret = FcCharSetIsSubset (right.u.c, left.u.c);
791 case FcOpNotContains:
792 /* left contains right if right is a subset of left */
793 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
796 ret = FcCharSetEqual (left.u.c, right.u.c);
799 ret = !FcCharSetEqual (left.u.c, right.u.c);
809 ret = FcLangSetContains (left.u.l, right.u.l);
811 case FcOpNotContains:
812 ret = !FcLangSetContains (left.u.l, right.u.l);
815 ret = FcLangSetEqual (left.u.l, right.u.l);
818 ret = !FcLangSetEqual (left.u.l, right.u.l);
840 ret = left.u.f == right.u.f;
843 case FcOpNotContains:
844 ret = left.u.f != right.u.f;
854 if (op == FcOpNotEqual || op == FcOpNotContains)
861 #define _FcDoubleFloor(d) ((int) (d))
862 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
863 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
864 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
865 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
866 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
869 FcConfigEvaluate (FcPattern *p, FcExpr *e)
878 v.type = FcTypeInteger;
882 v.type = FcTypeDouble;
886 v.type = FcTypeString;
891 v.type = FcTypeMatrix;
896 v.type = FcTypeCharSet;
905 r = FcPatternObjectGet (p, e->u.object, 0, &v);
906 if (r != FcResultMatch)
911 if (FcNameConstant (e->u.constant, &v.u.i))
912 v.type = FcTypeInteger;
917 vl = FcConfigEvaluate (p, e->u.tree.left);
918 if (vl.type == FcTypeBool)
921 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
923 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
936 case FcOpNotContains:
938 vl = FcConfigEvaluate (p, e->u.tree.left);
939 vr = FcConfigEvaluate (p, e->u.tree.right);
941 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
951 vl = FcConfigEvaluate (p, e->u.tree.left);
952 vr = FcConfigEvaluate (p, e->u.tree.right);
953 vl = FcConfigPromote (vl, vr);
954 vr = FcConfigPromote (vr, vl);
955 if (vl.type == vr.type)
961 v.type = FcTypeDouble;
962 v.u.d = vl.u.d + vr.u.d;
965 v.type = FcTypeDouble;
966 v.u.d = vl.u.d - vr.u.d;
969 v.type = FcTypeDouble;
970 v.u.d = vl.u.d * vr.u.d;
973 v.type = FcTypeDouble;
974 v.u.d = vl.u.d / vr.u.d;
980 if (v.type == FcTypeDouble &&
981 v.u.d == (double) (int) v.u.d)
983 v.type = FcTypeInteger;
991 v.u.b = vl.u.b || vr.u.b;
995 v.u.b = vl.u.b && vr.u.b;
1005 v.type = FcTypeString;
1006 str = FcStrPlus (vl.u.s, vr.u.s);
1007 v.u.s = FcStrStaticName (str);
1011 v.type = FcTypeVoid;
1014 v.type = FcTypeVoid;
1021 v.type = FcTypeMatrix;
1022 m = malloc (sizeof (FcMatrix));
1025 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
1026 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1031 v.type = FcTypeVoid;
1035 v.type = FcTypeVoid;
1040 v.type = FcTypeVoid;
1045 v.type = FcTypeVoid;
1046 FcValueDestroy (vl);
1047 FcValueDestroy (vr);
1050 vl = FcConfigEvaluate (p, e->u.tree.left);
1053 v.type = FcTypeBool;
1057 v.type = FcTypeVoid;
1060 FcValueDestroy (vl);
1063 vl = FcConfigEvaluate (p, e->u.tree.left);
1069 v.type = FcTypeInteger;
1070 v.u.i = FcDoubleFloor (vl.u.d);
1073 v.type = FcTypeVoid;
1076 FcValueDestroy (vl);
1079 vl = FcConfigEvaluate (p, e->u.tree.left);
1085 v.type = FcTypeInteger;
1086 v.u.i = FcDoubleCeil (vl.u.d);
1089 v.type = FcTypeVoid;
1092 FcValueDestroy (vl);
1095 vl = FcConfigEvaluate (p, e->u.tree.left);
1101 v.type = FcTypeInteger;
1102 v.u.i = FcDoubleRound (vl.u.d);
1105 v.type = FcTypeVoid;
1108 FcValueDestroy (vl);
1111 vl = FcConfigEvaluate (p, e->u.tree.left);
1117 v.type = FcTypeInteger;
1118 v.u.i = FcDoubleTrunc (vl.u.d);
1121 v.type = FcTypeVoid;
1124 FcValueDestroy (vl);
1127 v.type = FcTypeVoid;
1133 static FcValueList *
1134 FcConfigMatchValueList (FcPattern *p,
1136 FcValueList *values)
1138 FcValueList *ret = 0;
1139 FcExpr *e = t->expr;
1145 /* Compute the value of the match expression */
1146 if (e->op == FcOpComma)
1148 value = FcConfigEvaluate (p, e->u.tree.left);
1149 e = e->u.tree.right;
1153 value = FcConfigEvaluate (p, e);
1157 for (v = values; v; v = FcValueListNext(v))
1159 /* Compare the pattern value to the match expression value */
1160 if (FcConfigCompareValue (&v->value, t->op, &value))
1167 if (t->qual == FcQualAll)
1174 FcValueDestroy (value);
1179 static FcValueList *
1180 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1186 l = (FcValueList *) malloc (sizeof (FcValueList));
1189 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1190 if (e->op == FcOpComma)
1192 l->value = FcConfigEvaluate (p, e->u.tree.left);
1193 l->next = FcConfigValues (p, e->u.tree.right, binding);
1197 l->value = FcConfigEvaluate (p, e);
1200 l->binding = binding;
1201 if (l->value.type == FcTypeVoid)
1203 FcValueList *next = FcValueListNext(l);
1205 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1214 FcConfigAdd (FcValueListPtr *head,
1215 FcValueList *position,
1219 FcValueListPtr *prev, last, v;
1220 FcValueBinding sameBinding;
1223 sameBinding = position->binding;
1225 sameBinding = FcValueBindingWeak;
1226 for (v = new; v != NULL; v = FcValueListNext(v))
1227 if (v->binding == FcValueBindingSame)
1228 v->binding = sameBinding;
1232 prev = &position->next;
1234 for (prev = head; *prev != NULL;
1235 prev = &(*prev)->next)
1242 for (prev = head; *prev != NULL;
1243 prev = &(*prev)->next)
1245 if (*prev == position)
1252 if (FcDebug () & FC_DBG_EDIT)
1255 printf ("position not on list\n");
1259 if (FcDebug () & FC_DBG_EDIT)
1261 printf ("%s list before ", append ? "Append" : "Prepend");
1262 FcValueListPrint (*head);
1269 while (last->next != NULL)
1276 if (FcDebug () & FC_DBG_EDIT)
1278 printf ("%s list after ", append ? "Append" : "Prepend");
1279 FcValueListPrint (*head);
1287 FcConfigDel (FcValueListPtr *head,
1288 FcValueList *position)
1290 FcValueListPtr *prev;
1292 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1294 if (*prev == position)
1296 *prev = position->next;
1297 position->next = NULL;
1298 FcValueListDestroy (position);
1305 FcConfigPatternAdd (FcPattern *p,
1312 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1316 FcConfigAdd (&e->values, 0, append, list);
1321 * Delete all values associated with a field
1324 FcConfigPatternDel (FcPattern *p,
1327 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1330 while (e->values != NULL)
1331 FcConfigDel (&e->values, e->values);
1335 FcConfigPatternCanon (FcPattern *p,
1338 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1341 if (e->values == NULL)
1342 FcPatternObjectDel (p, object);
1346 FcConfigSubstituteWithPat (FcConfig *config,
1361 config = FcConfigGetCurrent ();
1367 case FcMatchPattern:
1368 s = config->substPattern;
1371 s = config->substFont;
1374 s = config->substScan;
1380 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1381 if (!st && config->maxObjects)
1383 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1385 if (FcDebug () & FC_DBG_EDIT)
1387 printf ("FcConfigSubstitute ");
1390 for (; s; s = s->next)
1393 * Check the tests to see if
1394 * they all match the pattern
1396 for (t = s->test, i = 0; t; t = t->next, i++)
1398 if (FcDebug () & FC_DBG_EDIT)
1400 printf ("FcConfigSubstitute test ");
1404 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1409 st[i].elt = FcPatternObjectFindElt (m, t->object);
1413 * If there's no such field in the font,
1414 * then FcQualAll matches while FcQualAny does not
1418 if (t->qual == FcQualAll)
1427 * Check to see if there is a match, mark the location
1428 * to apply match-relative edits
1430 st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
1433 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1435 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1440 if (FcDebug () & FC_DBG_EDIT)
1441 printf ("No match\n");
1444 if (FcDebug () & FC_DBG_EDIT)
1446 printf ("Substitute ");
1449 for (e = s->edit; e; e = e->next)
1452 * Evaluate the list of expressions
1454 l = FcConfigValues (p, e->expr, e->binding);
1456 * Locate any test associated with this field, skipping
1457 * tests associated with the pattern when substituting in
1460 for (t = s->test, i = 0; t; t = t->next, i++)
1462 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1463 t->object == e->object)
1466 * KLUDGE - the pattern may have been reallocated or
1467 * things may have been inserted or deleted above
1468 * this element by other edits. Go back and find
1471 if (e != s->edit && st[i].elt)
1472 st[i].elt = FcPatternObjectFindElt (p, t->object);
1481 * If there was a test, then replace the matched
1482 * value with the new list of values
1486 FcValueList *thisValue = st[i].value;
1487 FcValueList *nextValue = thisValue;
1490 * Append the new list of values after the current value
1492 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1494 * Delete the marked value
1497 FcConfigDel (&st[i].elt->values, thisValue);
1499 * Adjust any pointers into the value list to ensure
1500 * future edits occur at the same place
1502 for (t = s->test, i = 0; t; t = t->next, i++)
1504 if (st[i].value == thisValue)
1505 st[i].value = nextValue;
1509 /* fall through ... */
1510 case FcOpAssignReplace:
1512 * Delete all of the values and insert
1515 FcConfigPatternDel (p, e->object);
1516 FcConfigPatternAdd (p, e->object, l, FcTrue);
1518 * Adjust any pointers into the value list as they no
1519 * longer point to anything valid
1523 FcPatternElt *thisElt = st[i].elt;
1524 for (t = s->test, i = 0; t; t = t->next, i++)
1526 if (st[i].elt == thisElt)
1534 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1537 /* fall through ... */
1538 case FcOpPrependFirst:
1539 FcConfigPatternAdd (p, e->object, l, FcFalse);
1544 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1547 /* fall through ... */
1548 case FcOpAppendLast:
1549 FcConfigPatternAdd (p, e->object, l, FcTrue);
1552 FcValueListDestroy (l);
1557 * Now go through the pattern and eliminate
1558 * any properties without data
1560 for (e = s->edit; e; e = e->next)
1561 FcConfigPatternCanon (p, e->object);
1563 if (FcDebug () & FC_DBG_EDIT)
1565 printf ("FcConfigSubstitute edit");
1569 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1571 if (FcDebug () & FC_DBG_EDIT)
1573 printf ("FcConfigSubstitute done");
1580 FcConfigSubstitute (FcConfig *config,
1584 return FcConfigSubstituteWithPat (config, p, 0, kind);
1587 #if defined (_WIN32)
1589 # define WIN32_LEAN_AND_MEAN
1590 # define WIN32_EXTRA_LEAN
1591 # include <windows.h>
1593 static FcChar8 fontconfig_path[1000] = "";
1595 # if (defined (PIC) || defined (DLL_EXPORT))
1598 DllMain (HINSTANCE hinstDLL,
1604 switch (fdwReason) {
1605 case DLL_PROCESS_ATTACH:
1606 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1607 sizeof (fontconfig_path)))
1610 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1611 * assume it's a Unix-style installation tree, and use
1612 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1613 * folder where the DLL is as FONTCONFIG_PATH.
1615 p = strrchr (fontconfig_path, '\\');
1619 p = strrchr (fontconfig_path, '\\');
1620 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1621 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1623 strcat (fontconfig_path, "\\etc\\fonts");
1626 fontconfig_path[0] = '\0';
1636 #undef FONTCONFIG_PATH
1637 #define FONTCONFIG_PATH fontconfig_path
1639 #endif /* !_WIN32 */
1641 #ifndef FONTCONFIG_FILE
1642 #define FONTCONFIG_FILE "fonts.conf"
1646 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1651 dir = (FcChar8 *) "";
1652 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1656 strcpy ((char *) path, (const char *) dir);
1657 /* make sure there's a single separator */
1659 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1660 path[strlen((char *) path)-1] != '\\')) &&
1663 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1664 strcat ((char *) path, "\\");
1666 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1667 strcat ((char *) path, "/");
1669 strcat ((char *) path, (char *) file);
1671 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1672 if (access ((char *) path, R_OK) == 0)
1680 FcConfigGetPath (void)
1683 FcChar8 *env, *e, *colon;
1688 npath = 2; /* default dir + null */
1689 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1695 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1698 path = calloc (npath, sizeof (FcChar8 *));
1708 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1710 colon = e + strlen ((char *) e);
1711 path[i] = malloc (colon - e + 1);
1714 strncpy ((char *) path[i], (const char *) e, colon - e);
1715 path[i][colon - e] = '\0';
1725 if (fontconfig_path[0] == '\0')
1728 if(!GetModuleFileName(NULL, fontconfig_path, sizeof(fontconfig_path)))
1730 p = strrchr (fontconfig_path, '\\');
1732 strcat (fontconfig_path, "\\fonts");
1735 dir = (FcChar8 *) FONTCONFIG_PATH;
1736 path[i] = malloc (strlen ((char *) dir) + 1);
1739 strcpy ((char *) path[i], (const char *) dir);
1743 for (i = 0; path[i]; i++)
1751 FcConfigFreePath (FcChar8 **path)
1755 for (p = path; *p; p++)
1760 static FcBool _FcConfigHomeEnabled = FcTrue;
1765 if (_FcConfigHomeEnabled)
1767 char *home = getenv ("HOME");
1771 home = getenv ("USERPROFILE");
1774 return (FcChar8 *) home;
1780 FcConfigEnableHome (FcBool enable)
1782 FcBool prev = _FcConfigHomeEnabled;
1783 _FcConfigHomeEnabled = enable;
1788 FcConfigFilename (const FcChar8 *url)
1790 FcChar8 *file, *dir, **path, **p;
1794 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1796 url = (FcChar8 *) FONTCONFIG_FILE;
1801 if (isalpha (*url) &&
1803 (url[2] == '/' || url[2] == '\\'))
1809 dir = FcConfigHome ();
1811 file = FcConfigFileExists (dir, url + 1);
1820 file = FcConfigFileExists (0, url);
1823 path = FcConfigGetPath ();
1826 for (p = path; *p; p++)
1828 file = FcConfigFileExists (*p, url);
1832 FcConfigFreePath (path);
1839 * Manage the application-specific fonts
1843 FcConfigAppFontAddFile (FcConfig *config,
1844 const FcChar8 *file)
1853 config = FcConfigGetCurrent ();
1858 subdirs = FcStrSetCreate ();
1862 set = FcConfigGetFonts (config, FcSetApplication);
1865 set = FcFontSetCreate ();
1868 FcStrSetDestroy (subdirs);
1871 FcConfigSetFonts (config, set, FcSetApplication);
1874 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
1876 FcStrSetDestroy (subdirs);
1879 if ((sublist = FcStrListCreate (subdirs)))
1881 while ((subdir = FcStrListNext (sublist)))
1883 FcConfigAppFontAddDir (config, subdir);
1885 FcStrListDone (sublist);
1887 FcStrSetDestroy (subdirs);
1892 FcConfigAppFontAddDir (FcConfig *config,
1900 config = FcConfigGetCurrent ();
1905 dirs = FcStrSetCreate ();
1909 set = FcConfigGetFonts (config, FcSetApplication);
1912 set = FcFontSetCreate ();
1915 FcStrSetDestroy (dirs);
1918 FcConfigSetFonts (config, set, FcSetApplication);
1921 FcStrSetAddFilename (dirs, dir);
1923 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
1925 FcStrSetDestroy (dirs);
1928 FcStrSetDestroy (dirs);
1933 FcConfigAppFontClear (FcConfig *config)
1937 config = FcConfigGetCurrent ();
1942 FcConfigSetFonts (config, 0, FcSetApplication);
1946 * Manage filename-based font source selectors
1950 FcConfigGlobAdd (FcConfig *config,
1951 const FcChar8 *glob,
1954 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1956 return FcStrSetAdd (set, glob);
1960 FcConfigGlobMatch (const FcChar8 *glob,
1961 const FcChar8 *string)
1965 while ((c = *glob++))
1969 /* short circuit common case */
1972 /* short circuit another common case */
1973 if (strchr ((char *) glob, '*') == 0)
1974 string += strlen ((char *) string) - strlen ((char *) glob);
1977 if (FcConfigGlobMatch (glob, string))
1983 if (*string++ == '\0')
1992 return *string == '\0';
1996 FcConfigGlobsMatch (const FcStrSet *globs,
1997 const FcChar8 *string)
2001 for (i = 0; i < globs->num; i++)
2002 if (FcConfigGlobMatch (globs->strs[i], string))
2008 FcConfigAcceptFilename (FcConfig *config,
2009 const FcChar8 *filename)
2011 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2013 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2019 * Manage font-pattern based font source selectors
2023 FcConfigPatternsAdd (FcConfig *config,
2027 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2029 return FcFontSetAdd (set, pattern);
2033 FcConfigPatternsMatch (const FcFontSet *patterns,
2034 const FcPattern *font)
2038 for (i = 0; i < patterns->nfont; i++)
2039 if (FcListPatternMatchAny (patterns->fonts[i], font))
2045 FcConfigAcceptFont (FcConfig *config,
2046 const FcPattern *font)
2048 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2050 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2055 #include "fcaliastail.h"