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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 (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;
901 v.type = FcTypeLangSet;
910 r = FcPatternObjectGet (p, e->u.object, 0, &v);
911 if (r != FcResultMatch)
916 if (FcNameConstant (e->u.constant, &v.u.i))
917 v.type = FcTypeInteger;
922 vl = FcConfigEvaluate (p, e->u.tree.left);
923 if (vl.type == FcTypeBool)
926 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
928 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
941 case FcOpNotContains:
943 vl = FcConfigEvaluate (p, e->u.tree.left);
944 vr = FcConfigEvaluate (p, e->u.tree.right);
946 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
956 vl = FcConfigEvaluate (p, e->u.tree.left);
957 vr = FcConfigEvaluate (p, e->u.tree.right);
958 vl = FcConfigPromote (vl, vr);
959 vr = FcConfigPromote (vr, vl);
960 if (vl.type == vr.type)
966 v.type = FcTypeDouble;
967 v.u.d = vl.u.d + vr.u.d;
970 v.type = FcTypeDouble;
971 v.u.d = vl.u.d - vr.u.d;
974 v.type = FcTypeDouble;
975 v.u.d = vl.u.d * vr.u.d;
978 v.type = FcTypeDouble;
979 v.u.d = vl.u.d / vr.u.d;
985 if (v.type == FcTypeDouble &&
986 v.u.d == (double) (int) v.u.d)
988 v.type = FcTypeInteger;
996 v.u.b = vl.u.b || vr.u.b;
1000 v.u.b = vl.u.b && vr.u.b;
1003 v.type = FcTypeVoid;
1010 v.type = FcTypeString;
1011 str = FcStrPlus (vl.u.s, vr.u.s);
1012 v.u.s = FcStrStaticName (str);
1016 v.type = FcTypeVoid;
1019 v.type = FcTypeVoid;
1026 v.type = FcTypeMatrix;
1027 m = malloc (sizeof (FcMatrix));
1030 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
1031 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1036 v.type = FcTypeVoid;
1040 v.type = FcTypeVoid;
1047 v.type = FcTypeCharSet;
1048 v.u.c = FcCharSetUnion (vl.u.c, vr.u.c);
1050 v.type = FcTypeVoid;
1053 v.type = FcTypeCharSet;
1054 v.u.c = FcCharSetSubtract (vl.u.c, vr.u.c);
1056 v.type = FcTypeVoid;
1059 v.type = FcTypeVoid;
1066 v.type = FcTypeLangSet;
1067 v.u.l = FcLangSetUnion (vl.u.l, vr.u.l);
1069 v.type = FcTypeVoid;
1072 v.type = FcTypeLangSet;
1073 v.u.l = FcLangSetSubtract (vl.u.l, vr.u.l);
1075 v.type = FcTypeVoid;
1078 v.type = FcTypeVoid;
1083 v.type = FcTypeVoid;
1088 v.type = FcTypeVoid;
1089 FcValueDestroy (vl);
1090 FcValueDestroy (vr);
1093 vl = FcConfigEvaluate (p, e->u.tree.left);
1096 v.type = FcTypeBool;
1100 v.type = FcTypeVoid;
1103 FcValueDestroy (vl);
1106 vl = FcConfigEvaluate (p, e->u.tree.left);
1112 v.type = FcTypeInteger;
1113 v.u.i = FcDoubleFloor (vl.u.d);
1116 v.type = FcTypeVoid;
1119 FcValueDestroy (vl);
1122 vl = FcConfigEvaluate (p, e->u.tree.left);
1128 v.type = FcTypeInteger;
1129 v.u.i = FcDoubleCeil (vl.u.d);
1132 v.type = FcTypeVoid;
1135 FcValueDestroy (vl);
1138 vl = FcConfigEvaluate (p, e->u.tree.left);
1144 v.type = FcTypeInteger;
1145 v.u.i = FcDoubleRound (vl.u.d);
1148 v.type = FcTypeVoid;
1151 FcValueDestroy (vl);
1154 vl = FcConfigEvaluate (p, e->u.tree.left);
1160 v.type = FcTypeInteger;
1161 v.u.i = FcDoubleTrunc (vl.u.d);
1164 v.type = FcTypeVoid;
1167 FcValueDestroy (vl);
1170 v.type = FcTypeVoid;
1176 static FcValueList *
1177 FcConfigMatchValueList (FcPattern *p,
1179 FcValueList *values)
1181 FcValueList *ret = 0;
1182 FcExpr *e = t->expr;
1188 /* Compute the value of the match expression */
1189 if (e->op == FcOpComma)
1191 value = FcConfigEvaluate (p, e->u.tree.left);
1192 e = e->u.tree.right;
1196 value = FcConfigEvaluate (p, e);
1200 for (v = values; v; v = FcValueListNext(v))
1202 /* Compare the pattern value to the match expression value */
1203 if (FcConfigCompareValue (&v->value, t->op, &value))
1210 if (t->qual == FcQualAll)
1217 FcValueDestroy (value);
1222 static FcValueList *
1223 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1229 l = (FcValueList *) malloc (sizeof (FcValueList));
1232 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1233 if (e->op == FcOpComma)
1235 l->value = FcConfigEvaluate (p, e->u.tree.left);
1236 l->next = FcConfigValues (p, e->u.tree.right, binding);
1240 l->value = FcConfigEvaluate (p, e);
1243 l->binding = binding;
1244 if (l->value.type == FcTypeVoid)
1246 FcValueList *next = FcValueListNext(l);
1248 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1257 FcConfigAdd (FcValueListPtr *head,
1258 FcValueList *position,
1262 FcValueListPtr *prev, last, v;
1263 FcValueBinding sameBinding;
1266 sameBinding = position->binding;
1268 sameBinding = FcValueBindingWeak;
1269 for (v = new; v != NULL; v = FcValueListNext(v))
1270 if (v->binding == FcValueBindingSame)
1271 v->binding = sameBinding;
1275 prev = &position->next;
1277 for (prev = head; *prev != NULL;
1278 prev = &(*prev)->next)
1285 for (prev = head; *prev != NULL;
1286 prev = &(*prev)->next)
1288 if (*prev == position)
1295 if (FcDebug () & FC_DBG_EDIT)
1298 printf ("position not on list\n");
1302 if (FcDebug () & FC_DBG_EDIT)
1304 printf ("%s list before ", append ? "Append" : "Prepend");
1305 FcValueListPrint (*head);
1312 while (last->next != NULL)
1319 if (FcDebug () & FC_DBG_EDIT)
1321 printf ("%s list after ", append ? "Append" : "Prepend");
1322 FcValueListPrint (*head);
1330 FcConfigDel (FcValueListPtr *head,
1331 FcValueList *position)
1333 FcValueListPtr *prev;
1335 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1337 if (*prev == position)
1339 *prev = position->next;
1340 position->next = NULL;
1341 FcValueListDestroy (position);
1348 FcConfigPatternAdd (FcPattern *p,
1355 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1359 FcConfigAdd (&e->values, 0, append, list);
1364 * Delete all values associated with a field
1367 FcConfigPatternDel (FcPattern *p,
1370 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1373 while (e->values != NULL)
1374 FcConfigDel (&e->values, e->values);
1378 FcConfigPatternCanon (FcPattern *p,
1381 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1384 if (e->values == NULL)
1385 FcPatternObjectDel (p, object);
1389 FcConfigSubstituteWithPat (FcConfig *config,
1404 config = FcConfigGetCurrent ();
1410 case FcMatchPattern:
1411 s = config->substPattern;
1414 s = config->substFont;
1417 s = config->substScan;
1423 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1424 if (!st && config->maxObjects)
1426 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1428 if (FcDebug () & FC_DBG_EDIT)
1430 printf ("FcConfigSubstitute ");
1433 for (; s; s = s->next)
1436 * Check the tests to see if
1437 * they all match the pattern
1439 for (t = s->test, i = 0; t; t = t->next, i++)
1441 if (FcDebug () & FC_DBG_EDIT)
1443 printf ("FcConfigSubstitute test ");
1447 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1452 st[i].elt = FcPatternObjectFindElt (m, t->object);
1456 * If there's no such field in the font,
1457 * then FcQualAll matches while FcQualAny does not
1461 if (t->qual == FcQualAll)
1470 * Check to see if there is a match, mark the location
1471 * to apply match-relative edits
1473 st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
1476 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1478 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1483 if (FcDebug () & FC_DBG_EDIT)
1484 printf ("No match\n");
1487 if (FcDebug () & FC_DBG_EDIT)
1489 printf ("Substitute ");
1492 for (e = s->edit; e; e = e->next)
1495 * Evaluate the list of expressions
1497 l = FcConfigValues (p, e->expr, e->binding);
1499 * Locate any test associated with this field, skipping
1500 * tests associated with the pattern when substituting in
1503 for (t = s->test, i = 0; t; t = t->next, i++)
1505 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1506 t->object == e->object)
1509 * KLUDGE - the pattern may have been reallocated or
1510 * things may have been inserted or deleted above
1511 * this element by other edits. Go back and find
1514 if (e != s->edit && st[i].elt)
1515 st[i].elt = FcPatternObjectFindElt (p, t->object);
1524 * If there was a test, then replace the matched
1525 * value with the new list of values
1529 FcValueList *thisValue = st[i].value;
1530 FcValueList *nextValue = thisValue;
1533 * Append the new list of values after the current value
1535 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1537 * Delete the marked value
1540 FcConfigDel (&st[i].elt->values, thisValue);
1542 * Adjust any pointers into the value list to ensure
1543 * future edits occur at the same place
1545 for (t = s->test, i = 0; t; t = t->next, i++)
1547 if (st[i].value == thisValue)
1548 st[i].value = nextValue;
1552 /* fall through ... */
1553 case FcOpAssignReplace:
1555 * Delete all of the values and insert
1558 FcConfigPatternDel (p, e->object);
1559 FcConfigPatternAdd (p, e->object, l, FcTrue);
1561 * Adjust any pointers into the value list as they no
1562 * longer point to anything valid
1566 FcPatternElt *thisElt = st[i].elt;
1567 for (t = s->test, i = 0; t; t = t->next, i++)
1569 if (st[i].elt == thisElt)
1577 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1580 /* fall through ... */
1581 case FcOpPrependFirst:
1582 FcConfigPatternAdd (p, e->object, l, FcFalse);
1587 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1590 /* fall through ... */
1591 case FcOpAppendLast:
1592 FcConfigPatternAdd (p, e->object, l, FcTrue);
1595 FcValueListDestroy (l);
1600 * Now go through the pattern and eliminate
1601 * any properties without data
1603 for (e = s->edit; e; e = e->next)
1604 FcConfigPatternCanon (p, e->object);
1606 if (FcDebug () & FC_DBG_EDIT)
1608 printf ("FcConfigSubstitute edit");
1612 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1614 if (FcDebug () & FC_DBG_EDIT)
1616 printf ("FcConfigSubstitute done");
1623 FcConfigSubstitute (FcConfig *config,
1627 return FcConfigSubstituteWithPat (config, p, 0, kind);
1630 #if defined (_WIN32)
1632 # define WIN32_LEAN_AND_MEAN
1633 # define WIN32_EXTRA_LEAN
1634 # include <windows.h>
1636 static FcChar8 fontconfig_path[1000] = "";
1638 # if (defined (PIC) || defined (DLL_EXPORT))
1641 DllMain (HINSTANCE hinstDLL,
1647 switch (fdwReason) {
1648 case DLL_PROCESS_ATTACH:
1649 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1650 sizeof (fontconfig_path)))
1653 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1654 * assume it's a Unix-style installation tree, and use
1655 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1656 * folder where the DLL is as FONTCONFIG_PATH.
1658 p = strrchr (fontconfig_path, '\\');
1662 p = strrchr (fontconfig_path, '\\');
1663 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1664 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1666 strcat (fontconfig_path, "\\etc\\fonts");
1669 fontconfig_path[0] = '\0';
1679 #undef FONTCONFIG_PATH
1680 #define FONTCONFIG_PATH fontconfig_path
1682 #endif /* !_WIN32 */
1684 #ifndef FONTCONFIG_FILE
1685 #define FONTCONFIG_FILE "fonts.conf"
1689 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1694 dir = (FcChar8 *) "";
1695 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1699 strcpy ((char *) path, (const char *) dir);
1700 /* make sure there's a single separator */
1702 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1703 path[strlen((char *) path)-1] != '\\')) &&
1706 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1707 strcat ((char *) path, "\\");
1709 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1710 strcat ((char *) path, "/");
1712 strcat ((char *) path, (char *) file);
1714 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1715 if (access ((char *) path, R_OK) == 0)
1723 FcConfigGetPath (void)
1726 FcChar8 *env, *e, *colon;
1731 npath = 2; /* default dir + null */
1732 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1738 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1741 path = calloc (npath, sizeof (FcChar8 *));
1751 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1753 colon = e + strlen ((char *) e);
1754 path[i] = malloc (colon - e + 1);
1757 strncpy ((char *) path[i], (const char *) e, colon - e);
1758 path[i][colon - e] = '\0';
1768 if (fontconfig_path[0] == '\0')
1771 if(!GetModuleFileName(NULL, fontconfig_path, sizeof(fontconfig_path)))
1773 p = strrchr (fontconfig_path, '\\');
1775 strcat (fontconfig_path, "\\fonts");
1778 dir = (FcChar8 *) FONTCONFIG_PATH;
1779 path[i] = malloc (strlen ((char *) dir) + 1);
1782 strcpy ((char *) path[i], (const char *) dir);
1786 for (i = 0; path[i]; i++)
1794 FcConfigFreePath (FcChar8 **path)
1798 for (p = path; *p; p++)
1803 static FcBool _FcConfigHomeEnabled = FcTrue;
1808 if (_FcConfigHomeEnabled)
1810 char *home = getenv ("HOME");
1814 home = getenv ("USERPROFILE");
1817 return (FcChar8 *) home;
1823 FcConfigEnableHome (FcBool enable)
1825 FcBool prev = _FcConfigHomeEnabled;
1826 _FcConfigHomeEnabled = enable;
1831 FcConfigFilename (const FcChar8 *url)
1833 FcChar8 *file, *dir, **path, **p;
1837 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1839 url = (FcChar8 *) FONTCONFIG_FILE;
1844 if (isalpha (*url) &&
1846 (url[2] == '/' || url[2] == '\\'))
1852 dir = FcConfigHome ();
1854 file = FcConfigFileExists (dir, url + 1);
1863 file = FcConfigFileExists (0, url);
1866 path = FcConfigGetPath ();
1869 for (p = path; *p; p++)
1871 file = FcConfigFileExists (*p, url);
1875 FcConfigFreePath (path);
1882 * Manage the application-specific fonts
1886 FcConfigAppFontAddFile (FcConfig *config,
1887 const FcChar8 *file)
1896 config = FcConfigGetCurrent ();
1901 subdirs = FcStrSetCreate ();
1905 set = FcConfigGetFonts (config, FcSetApplication);
1908 set = FcFontSetCreate ();
1911 FcStrSetDestroy (subdirs);
1914 FcConfigSetFonts (config, set, FcSetApplication);
1917 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
1919 FcStrSetDestroy (subdirs);
1922 if ((sublist = FcStrListCreate (subdirs)))
1924 while ((subdir = FcStrListNext (sublist)))
1926 FcConfigAppFontAddDir (config, subdir);
1928 FcStrListDone (sublist);
1930 FcStrSetDestroy (subdirs);
1935 FcConfigAppFontAddDir (FcConfig *config,
1943 config = FcConfigGetCurrent ();
1948 dirs = FcStrSetCreate ();
1952 set = FcConfigGetFonts (config, FcSetApplication);
1955 set = FcFontSetCreate ();
1958 FcStrSetDestroy (dirs);
1961 FcConfigSetFonts (config, set, FcSetApplication);
1964 FcStrSetAddFilename (dirs, dir);
1966 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
1968 FcStrSetDestroy (dirs);
1971 FcStrSetDestroy (dirs);
1976 FcConfigAppFontClear (FcConfig *config)
1980 config = FcConfigGetCurrent ();
1985 FcConfigSetFonts (config, 0, FcSetApplication);
1989 * Manage filename-based font source selectors
1993 FcConfigGlobAdd (FcConfig *config,
1994 const FcChar8 *glob,
1997 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1999 return FcStrSetAdd (set, glob);
2003 FcConfigGlobMatch (const FcChar8 *glob,
2004 const FcChar8 *string)
2008 while ((c = *glob++))
2012 /* short circuit common case */
2015 /* short circuit another common case */
2016 if (strchr ((char *) glob, '*') == 0)
2017 string += strlen ((char *) string) - strlen ((char *) glob);
2020 if (FcConfigGlobMatch (glob, string))
2026 if (*string++ == '\0')
2035 return *string == '\0';
2039 FcConfigGlobsMatch (const FcStrSet *globs,
2040 const FcChar8 *string)
2044 for (i = 0; i < globs->num; i++)
2045 if (FcConfigGlobMatch (globs->strs[i], string))
2051 FcConfigAcceptFilename (FcConfig *config,
2052 const FcChar8 *filename)
2054 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2056 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2062 * Manage font-pattern based font source selectors
2066 FcConfigPatternsAdd (FcConfig *config,
2070 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2072 return FcFontSetAdd (set, pattern);
2076 FcConfigPatternsMatch (const FcFontSet *patterns,
2077 const FcPattern *font)
2081 for (i = 0; i < patterns->nfont; i++)
2082 if (FcListPatternMatchAny (patterns->fonts[i], font))
2088 FcConfigAcceptFont (FcConfig *config,
2089 const FcPattern *font)
2091 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2093 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2098 #include "fcaliastail.h"