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 * 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;
101 FcFontSetDestroy (config->rejectPatterns);
103 FcFontSetDestroy (config->acceptPatterns);
105 FcStrSetDestroy (config->rejectGlobs);
107 FcStrSetDestroy (config->acceptGlobs);
109 FcStrSetDestroy (config->fontDirs);
111 FcStrSetDestroy (config->configFiles);
113 FcStrSetDestroy (config->configDirs);
116 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
122 FcConfigNewestFile (FcStrSet *files)
124 FcStrList *list = FcStrListCreate (files);
125 FcFileTime newest = { 0, FcFalse };
131 while ((file = FcStrListNext (list)))
132 if (FcStat ((char *) file, &statb) == 0)
133 if (!newest.set || statb.st_mtime - newest.time > 0)
136 newest.time = statb.st_mtime;
138 FcStrListDone (list);
144 FcConfigUptoDate (FcConfig *config)
146 FcFileTime config_time, config_dir_time, font_time;
147 time_t now = time(0);
150 config = FcConfigGetCurrent ();
154 config_time = FcConfigNewestFile (config->configFiles);
155 config_dir_time = FcConfigNewestFile (config->configDirs);
156 font_time = FcConfigNewestFile (config->fontDirs);
157 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
158 (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
159 (font_time.set && (font_time.time - config->rescanTime) > 0))
161 /* We need to check for potential clock problems here (OLPC ticket #6046) */
162 if ((config_time.set && (config_time.time - now) > 0) ||
163 (config_dir_time.set && (config_dir_time.time - now) > 0) ||
164 (font_time.set && (font_time.time - now) > 0))
167 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
168 config->rescanTime = now;
174 config->rescanTime = now;
179 FcSubstDestroy (FcSubst *s)
187 FcTestDestroy (s->test);
189 FcEditDestroy (s->edit);
191 FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
197 FcConfigReference (FcConfig *config)
201 config = FcConfigGetCurrent ();
212 FcConfigDestroy (FcConfig *config)
216 if (--config->ref > 0)
219 if (config == _fcConfig)
222 FcStrSetDestroy (config->configDirs);
223 FcStrSetDestroy (config->fontDirs);
224 FcStrSetDestroy (config->cacheDirs);
225 FcStrSetDestroy (config->configFiles);
226 FcStrSetDestroy (config->acceptGlobs);
227 FcStrSetDestroy (config->rejectGlobs);
228 FcFontSetDestroy (config->acceptPatterns);
229 FcFontSetDestroy (config->rejectPatterns);
232 FcBlanksDestroy (config->blanks);
234 FcSubstDestroy (config->substPattern);
235 FcSubstDestroy (config->substFont);
236 FcSubstDestroy (config->substScan);
237 for (set = FcSetSystem; set <= FcSetApplication; set++)
238 if (config->fonts[set])
239 FcFontSetDestroy (config->fonts[set]);
242 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
246 * Add cache to configuration, adding fonts and directories
250 FcConfigAddCache (FcConfig *config, FcCache *cache,
251 FcSetName set, FcStrSet *dirSet)
260 fs = FcCacheSet (cache);
265 for (i = 0; i < fs->nfont; i++)
267 FcPattern *font = FcFontSetFont (fs, i);
271 * Check to see if font is banned by filename
273 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
274 0, &font_file) == FcResultMatch &&
275 !FcConfigAcceptFilename (config, font_file))
281 * Check to see if font is banned by pattern
283 if (!FcConfigAcceptFont (config, font))
287 FcFontSetAdd (config->fonts[set], font);
289 FcDirCacheReference (cache, nref);
295 dirs = FcCacheDirs (cache);
298 for (i = 0; i < cache->dirs_count; i++)
300 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
301 if (FcConfigAcceptFilename (config, dir))
302 FcStrSetAddFilename (dirSet, dir);
309 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
315 dirlist = FcStrListCreate (dirSet);
319 while ((dir = FcStrListNext (dirlist)))
321 if (FcDebug () & FC_DBG_FONTSET)
322 printf ("adding fonts from%s\n", dir);
323 cache = FcDirCacheRead (dir, FcFalse, config);
326 FcConfigAddCache (config, cache, set, dirSet);
327 FcDirCacheUnload (cache);
329 FcStrListDone (dirlist);
334 * Scan the current list of directories in the configuration
335 * and build the set of available fonts.
339 FcConfigBuildFonts (FcConfig *config)
345 config = FcConfigGetCurrent ();
350 fonts = FcFontSetCreate ();
354 FcConfigSetFonts (config, fonts, FcSetSystem);
356 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
358 if (FcDebug () & FC_DBG_FONTSET)
359 FcFontSetPrint (fonts);
364 FcConfigSetCurrent (FcConfig *config)
366 if (config == _fcConfig)
370 if (!FcConfigBuildFonts (config))
374 FcConfigDestroy (_fcConfig);
380 FcConfigGetCurrent (void)
389 FcConfigAddConfigDir (FcConfig *config,
392 return FcStrSetAddFilename (config->configDirs, d);
396 FcConfigGetConfigDirs (FcConfig *config)
400 config = FcConfigGetCurrent ();
404 return FcStrListCreate (config->configDirs);
408 FcConfigAddFontDir (FcConfig *config,
411 return FcStrSetAddFilename (config->fontDirs, d);
415 FcConfigAddDir (FcConfig *config,
418 return (FcConfigAddConfigDir (config, d) &&
419 FcConfigAddFontDir (config, d));
423 FcConfigGetFontDirs (FcConfig *config)
427 config = FcConfigGetCurrent ();
431 return FcStrListCreate (config->fontDirs);
435 FcConfigAddCacheDir (FcConfig *config,
438 return FcStrSetAddFilename (config->cacheDirs, d);
442 FcConfigGetCacheDirs (FcConfig *config)
446 config = FcConfigGetCurrent ();
450 return FcStrListCreate (config->cacheDirs);
454 FcConfigAddConfigFile (FcConfig *config,
458 FcChar8 *file = FcConfigFilename (f);
463 ret = FcStrSetAdd (config->configFiles, file);
469 FcConfigGetConfigFiles (FcConfig *config)
473 config = FcConfigGetCurrent ();
477 return FcStrListCreate (config->configFiles);
481 FcConfigGetCache (FcConfig *config)
487 FcConfigGetFonts (FcConfig *config,
492 config = FcConfigGetCurrent ();
496 return config->fonts[set];
500 FcConfigSetFonts (FcConfig *config,
504 if (config->fonts[set])
505 FcFontSetDestroy (config->fonts[set]);
506 config->fonts[set] = fonts;
510 FcConfigGetBlanks (FcConfig *config)
514 config = FcConfigGetCurrent ();
518 return config->blanks;
522 FcConfigAddBlank (FcConfig *config,
525 FcBlanks *b, *freeme = 0;
530 freeme = b = FcBlanksCreate ();
534 if (!FcBlanksAdd (b, blank))
537 FcBlanksDestroy (freeme);
545 FcConfigGetRescanInterval (FcConfig *config)
549 config = FcConfigGetCurrent ();
553 return config->rescanInterval;
557 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
561 config = FcConfigGetCurrent ();
565 config->rescanInterval = rescanInterval;
570 * A couple of typos escaped into the library
573 FcConfigGetRescanInverval (FcConfig *config)
575 return FcConfigGetRescanInterval (config);
579 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
581 return FcConfigSetRescanInterval (config, rescanInterval);
586 FcConfigAddEdit (FcConfig *config,
591 FcSubst *subst, **prev;
597 prev = &config->substPattern;
600 prev = &config->substFont;
603 prev = &config->substScan;
608 subst = (FcSubst *) malloc (sizeof (FcSubst));
611 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
612 for (; *prev; prev = &(*prev)->next);
618 for (t = test; t; t = t->next)
620 if (t->kind == FcMatchDefault)
624 if (config->maxObjects < num)
625 config->maxObjects = num;
626 if (FcDebug () & FC_DBG_EDIT)
628 printf ("Add Subst ");
629 FcSubstPrint (subst);
634 typedef struct _FcSubState {
640 FcConfigPromote (FcValue v, FcValue u)
642 if (v.type == FcTypeInteger)
644 v.type = FcTypeDouble;
645 v.u.d = (double) v.u.i;
647 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
649 v.u.m = &FcIdentityMatrix;
650 v.type = FcTypeMatrix;
652 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
654 v.u.l = FcLangSetPromote (v.u.s);
655 v.type = FcTypeLangSet;
661 FcConfigCompareValue (const FcValue *left_o,
663 const FcValue *right_o)
665 FcValue left = FcValueCanonicalize(left_o);
666 FcValue right = FcValueCanonicalize(right_o);
667 FcBool ret = FcFalse;
669 left = FcConfigPromote (left, right);
670 right = FcConfigPromote (right, left);
671 if (left.type == right.type)
675 break; /* FcConfigPromote prevents this from happening */
681 ret = left.u.d == right.u.d;
684 case FcOpNotContains:
685 ret = left.u.d != right.u.d;
688 ret = left.u.d < right.u.d;
691 ret = left.u.d <= right.u.d;
694 ret = left.u.d > right.u.d;
697 ret = left.u.d >= right.u.d;
708 ret = left.u.b == right.u.b;
711 case FcOpNotContains:
712 ret = left.u.b != right.u.b;
722 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
725 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
728 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
730 case FcOpNotContains:
731 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
742 ret = FcMatrixEqual (left.u.m, right.u.m);
745 case FcOpNotContains:
746 ret = !FcMatrixEqual (left.u.m, right.u.m);
756 /* left contains right if right is a subset of left */
757 ret = FcCharSetIsSubset (right.u.c, left.u.c);
759 case FcOpNotContains:
760 /* left contains right if right is a subset of left */
761 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
764 ret = FcCharSetEqual (left.u.c, right.u.c);
767 ret = !FcCharSetEqual (left.u.c, right.u.c);
777 ret = FcLangSetContains (left.u.l, right.u.l);
779 case FcOpNotContains:
780 ret = !FcLangSetContains (left.u.l, right.u.l);
783 ret = FcLangSetEqual (left.u.l, right.u.l);
786 ret = !FcLangSetEqual (left.u.l, right.u.l);
808 ret = left.u.f == right.u.f;
811 case FcOpNotContains:
812 ret = left.u.f != right.u.f;
822 if (op == FcOpNotEqual || op == FcOpNotContains)
829 #define _FcDoubleFloor(d) ((int) (d))
830 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
831 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
832 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
833 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
834 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
837 FcConfigEvaluate (FcPattern *p, FcExpr *e)
846 v.type = FcTypeInteger;
850 v.type = FcTypeDouble;
854 v.type = FcTypeString;
855 v.u.s = FcStrStaticName(e->u.sval);
858 v.type = FcTypeMatrix;
863 v.type = FcTypeCharSet;
872 r = FcPatternObjectGet (p, e->u.object, 0, &v);
873 if (r != FcResultMatch)
878 if (FcNameConstant (e->u.constant, &v.u.i))
879 v.type = FcTypeInteger;
884 vl = FcConfigEvaluate (p, e->u.tree.left);
885 if (vl.type == FcTypeBool)
888 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
890 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
903 case FcOpNotContains:
905 vl = FcConfigEvaluate (p, e->u.tree.left);
906 vr = FcConfigEvaluate (p, e->u.tree.right);
908 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
918 vl = FcConfigEvaluate (p, e->u.tree.left);
919 vr = FcConfigEvaluate (p, e->u.tree.right);
920 vl = FcConfigPromote (vl, vr);
921 vr = FcConfigPromote (vr, vl);
922 if (vl.type == vr.type)
928 v.type = FcTypeDouble;
929 v.u.d = vl.u.d + vr.u.d;
932 v.type = FcTypeDouble;
933 v.u.d = vl.u.d - vr.u.d;
936 v.type = FcTypeDouble;
937 v.u.d = vl.u.d * vr.u.d;
940 v.type = FcTypeDouble;
941 v.u.d = vl.u.d / vr.u.d;
947 if (v.type == FcTypeDouble &&
948 v.u.d == (double) (int) v.u.d)
950 v.type = FcTypeInteger;
958 v.u.b = vl.u.b || vr.u.b;
962 v.u.b = vl.u.b && vr.u.b;
972 v.type = FcTypeString;
973 str = FcStrPlus (vl.u.s, vr.u.s);
974 v.u.s = FcStrStaticName (str);
988 v.type = FcTypeMatrix;
989 m = malloc (sizeof (FcMatrix));
992 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
993 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1002 v.type = FcTypeVoid;
1007 v.type = FcTypeVoid;
1012 v.type = FcTypeVoid;
1013 FcValueDestroy (vl);
1014 FcValueDestroy (vr);
1017 vl = FcConfigEvaluate (p, e->u.tree.left);
1020 v.type = FcTypeBool;
1024 v.type = FcTypeVoid;
1027 FcValueDestroy (vl);
1030 vl = FcConfigEvaluate (p, e->u.tree.left);
1036 v.type = FcTypeInteger;
1037 v.u.i = FcDoubleFloor (vl.u.d);
1040 v.type = FcTypeVoid;
1043 FcValueDestroy (vl);
1046 vl = FcConfigEvaluate (p, e->u.tree.left);
1052 v.type = FcTypeInteger;
1053 v.u.i = FcDoubleCeil (vl.u.d);
1056 v.type = FcTypeVoid;
1059 FcValueDestroy (vl);
1062 vl = FcConfigEvaluate (p, e->u.tree.left);
1068 v.type = FcTypeInteger;
1069 v.u.i = FcDoubleRound (vl.u.d);
1072 v.type = FcTypeVoid;
1075 FcValueDestroy (vl);
1078 vl = FcConfigEvaluate (p, e->u.tree.left);
1084 v.type = FcTypeInteger;
1085 v.u.i = FcDoubleTrunc (vl.u.d);
1088 v.type = FcTypeVoid;
1091 FcValueDestroy (vl);
1094 v.type = FcTypeVoid;
1100 static FcValueList *
1101 FcConfigMatchValueList (FcPattern *p,
1103 FcValueList *values)
1105 FcValueList *ret = 0;
1106 FcExpr *e = t->expr;
1112 /* Compute the value of the match expression */
1113 if (e->op == FcOpComma)
1115 value = FcConfigEvaluate (p, e->u.tree.left);
1116 e = e->u.tree.right;
1120 value = FcConfigEvaluate (p, e);
1124 for (v = values; v; v = FcValueListNext(v))
1126 /* Compare the pattern value to the match expression value */
1127 if (FcConfigCompareValue (&v->value, t->op, &value))
1134 if (t->qual == FcQualAll)
1141 FcValueDestroy (value);
1146 static FcValueList *
1147 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1153 l = (FcValueList *) malloc (sizeof (FcValueList));
1156 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1157 if (e->op == FcOpComma)
1159 l->value = FcConfigEvaluate (p, e->u.tree.left);
1160 l->next = FcConfigValues (p, e->u.tree.right, binding);
1164 l->value = FcConfigEvaluate (p, e);
1167 l->binding = binding;
1168 if (l->value.type == FcTypeVoid)
1170 FcValueList *next = FcValueListNext(l);
1172 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1181 FcConfigAdd (FcValueListPtr *head,
1182 FcValueList *position,
1186 FcValueListPtr *prev, last, v;
1187 FcValueBinding sameBinding;
1190 sameBinding = position->binding;
1192 sameBinding = FcValueBindingWeak;
1193 for (v = new; v != NULL; v = FcValueListNext(v))
1194 if (v->binding == FcValueBindingSame)
1195 v->binding = sameBinding;
1199 prev = &position->next;
1201 for (prev = head; *prev != NULL;
1202 prev = &(*prev)->next)
1209 for (prev = head; *prev != NULL;
1210 prev = &(*prev)->next)
1212 if (*prev == position)
1219 if (FcDebug () & FC_DBG_EDIT)
1222 printf ("position not on list\n");
1226 if (FcDebug () & FC_DBG_EDIT)
1228 printf ("%s list before ", append ? "Append" : "Prepend");
1229 FcValueListPrint (*head);
1236 while (last->next != NULL)
1243 if (FcDebug () & FC_DBG_EDIT)
1245 printf ("%s list after ", append ? "Append" : "Prepend");
1246 FcValueListPrint (*head);
1254 FcConfigDel (FcValueListPtr *head,
1255 FcValueList *position)
1257 FcValueListPtr *prev;
1259 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1261 if (*prev == position)
1263 *prev = position->next;
1264 position->next = NULL;
1265 FcValueListDestroy (position);
1272 FcConfigPatternAdd (FcPattern *p,
1279 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1283 FcConfigAdd (&e->values, 0, append, list);
1288 * Delete all values associated with a field
1291 FcConfigPatternDel (FcPattern *p,
1294 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1297 while (e->values != NULL)
1298 FcConfigDel (&e->values, e->values);
1302 FcConfigPatternCanon (FcPattern *p,
1305 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1308 if (e->values == NULL)
1309 FcPatternObjectDel (p, object);
1313 FcConfigSubstituteWithPat (FcConfig *config,
1328 config = FcConfigGetCurrent ();
1334 case FcMatchPattern:
1335 s = config->substPattern;
1338 s = config->substFont;
1341 s = config->substScan;
1347 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1348 if (!st && config->maxObjects)
1350 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1352 if (FcDebug () & FC_DBG_EDIT)
1354 printf ("FcConfigSubstitute ");
1357 for (; s; s = s->next)
1360 * Check the tests to see if
1361 * they all match the pattern
1363 for (t = s->test, i = 0; t; t = t->next, i++)
1365 if (FcDebug () & FC_DBG_EDIT)
1367 printf ("FcConfigSubstitute test ");
1371 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1376 st[i].elt = FcPatternObjectFindElt (m, t->object);
1380 * If there's no such field in the font,
1381 * then FcQualAll matches while FcQualAny does not
1385 if (t->qual == FcQualAll)
1394 * Check to see if there is a match, mark the location
1395 * to apply match-relative edits
1397 st[i].value = FcConfigMatchValueList (m, t, st[i].elt->values);
1400 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1402 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1407 if (FcDebug () & FC_DBG_EDIT)
1408 printf ("No match\n");
1411 if (FcDebug () & FC_DBG_EDIT)
1413 printf ("Substitute ");
1416 for (e = s->edit; e; e = e->next)
1419 * Evaluate the list of expressions
1421 l = FcConfigValues (p, e->expr, e->binding);
1423 * Locate any test associated with this field, skipping
1424 * tests associated with the pattern when substituting in
1427 for (t = s->test, i = 0; t; t = t->next, i++)
1429 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1430 t->object == e->object)
1433 * KLUDGE - the pattern may have been reallocated or
1434 * things may have been inserted or deleted above
1435 * this element by other edits. Go back and find
1438 if (e != s->edit && st[i].elt)
1439 st[i].elt = FcPatternObjectFindElt (p, t->object);
1448 * If there was a test, then replace the matched
1449 * value with the new list of values
1453 FcValueList *thisValue = st[i].value;
1454 FcValueList *nextValue = thisValue;
1457 * Append the new list of values after the current value
1459 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1461 * Delete the marked value
1464 FcConfigDel (&st[i].elt->values, thisValue);
1466 * Adjust any pointers into the value list to ensure
1467 * future edits occur at the same place
1469 for (t = s->test, i = 0; t; t = t->next, i++)
1471 if (st[i].value == thisValue)
1472 st[i].value = nextValue;
1476 /* fall through ... */
1477 case FcOpAssignReplace:
1479 * Delete all of the values and insert
1482 FcConfigPatternDel (p, e->object);
1483 FcConfigPatternAdd (p, e->object, l, FcTrue);
1485 * Adjust any pointers into the value list as they no
1486 * longer point to anything valid
1490 FcPatternElt *thisElt = st[i].elt;
1491 for (t = s->test, i = 0; t; t = t->next, i++)
1493 if (st[i].elt == thisElt)
1501 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1504 /* fall through ... */
1505 case FcOpPrependFirst:
1506 FcConfigPatternAdd (p, e->object, l, FcFalse);
1511 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1514 /* fall through ... */
1515 case FcOpAppendLast:
1516 FcConfigPatternAdd (p, e->object, l, FcTrue);
1519 FcValueListDestroy (l);
1524 * Now go through the pattern and eliminate
1525 * any properties without data
1527 for (e = s->edit; e; e = e->next)
1528 FcConfigPatternCanon (p, e->object);
1530 if (FcDebug () & FC_DBG_EDIT)
1532 printf ("FcConfigSubstitute edit");
1536 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1538 if (FcDebug () & FC_DBG_EDIT)
1540 printf ("FcConfigSubstitute done");
1547 FcConfigSubstitute (FcConfig *config,
1551 return FcConfigSubstituteWithPat (config, p, 0, kind);
1554 #if defined (_WIN32)
1556 # define WIN32_LEAN_AND_MEAN
1557 # define WIN32_EXTRA_LEAN
1558 # include <windows.h>
1560 static FcChar8 fontconfig_path[1000] = "";
1562 # if (defined (PIC) || defined (DLL_EXPORT))
1565 DllMain (HINSTANCE hinstDLL,
1571 switch (fdwReason) {
1572 case DLL_PROCESS_ATTACH:
1573 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1574 sizeof (fontconfig_path)))
1577 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1578 * assume it's a Unix-style installation tree, and use
1579 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1580 * folder where the DLL is as FONTCONFIG_PATH.
1582 p = strrchr (fontconfig_path, '\\');
1586 p = strrchr (fontconfig_path, '\\');
1587 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1588 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1590 strcat (fontconfig_path, "\\etc\\fonts");
1593 fontconfig_path[0] = '\0';
1603 #undef FONTCONFIG_PATH
1604 #define FONTCONFIG_PATH fontconfig_path
1606 #endif /* !_WIN32 */
1608 #ifndef FONTCONFIG_FILE
1609 #define FONTCONFIG_FILE "fonts.conf"
1613 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1618 dir = (FcChar8 *) "";
1619 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1623 strcpy ((char *) path, (const char *) dir);
1624 /* make sure there's a single separator */
1626 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1627 path[strlen((char *) path)-1] != '\\')) &&
1630 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1631 strcat ((char *) path, "\\");
1633 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1634 strcat ((char *) path, "/");
1636 strcat ((char *) path, (char *) file);
1638 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1639 if (access ((char *) path, R_OK) == 0)
1647 FcConfigGetPath (void)
1650 FcChar8 *env, *e, *colon;
1655 npath = 2; /* default dir + null */
1656 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1662 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1665 path = calloc (npath, sizeof (FcChar8 *));
1675 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1677 colon = e + strlen ((char *) e);
1678 path[i] = malloc (colon - e + 1);
1681 strncpy ((char *) path[i], (const char *) e, colon - e);
1682 path[i][colon - e] = '\0';
1692 if (fontconfig_path[0] == '\0')
1695 if(!GetModuleFileName(NULL, fontconfig_path, sizeof(fontconfig_path)))
1697 p = strrchr (fontconfig_path, '\\');
1699 strcat (fontconfig_path, "\\fonts");
1702 dir = (FcChar8 *) FONTCONFIG_PATH;
1703 path[i] = malloc (strlen ((char *) dir) + 1);
1706 strcpy ((char *) path[i], (const char *) dir);
1710 for (i = 0; path[i]; i++)
1718 FcConfigFreePath (FcChar8 **path)
1722 for (p = path; *p; p++)
1727 static FcBool _FcConfigHomeEnabled = FcTrue;
1732 if (_FcConfigHomeEnabled)
1734 char *home = getenv ("HOME");
1738 home = getenv ("USERPROFILE");
1741 return (FcChar8 *) home;
1747 FcConfigEnableHome (FcBool enable)
1749 FcBool prev = _FcConfigHomeEnabled;
1750 _FcConfigHomeEnabled = enable;
1755 FcConfigFilename (const FcChar8 *url)
1757 FcChar8 *file, *dir, **path, **p;
1761 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1763 url = (FcChar8 *) FONTCONFIG_FILE;
1768 if (isalpha (*url) &&
1770 (url[2] == '/' || url[2] == '\\'))
1776 dir = FcConfigHome ();
1778 file = FcConfigFileExists (dir, url + 1);
1787 file = FcConfigFileExists (0, url);
1790 path = FcConfigGetPath ();
1793 for (p = path; *p; p++)
1795 file = FcConfigFileExists (*p, url);
1799 FcConfigFreePath (path);
1806 * Manage the application-specific fonts
1810 FcConfigAppFontAddFile (FcConfig *config,
1811 const FcChar8 *file)
1820 config = FcConfigGetCurrent ();
1825 subdirs = FcStrSetCreate ();
1829 set = FcConfigGetFonts (config, FcSetApplication);
1832 set = FcFontSetCreate ();
1835 FcStrSetDestroy (subdirs);
1838 FcConfigSetFonts (config, set, FcSetApplication);
1841 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
1843 FcStrSetDestroy (subdirs);
1846 if ((sublist = FcStrListCreate (subdirs)))
1848 while ((subdir = FcStrListNext (sublist)))
1850 FcConfigAppFontAddDir (config, subdir);
1852 FcStrListDone (sublist);
1854 FcStrSetDestroy (subdirs);
1859 FcConfigAppFontAddDir (FcConfig *config,
1867 config = FcConfigGetCurrent ();
1872 dirs = FcStrSetCreate ();
1876 set = FcConfigGetFonts (config, FcSetApplication);
1879 set = FcFontSetCreate ();
1882 FcStrSetDestroy (dirs);
1885 FcConfigSetFonts (config, set, FcSetApplication);
1888 FcStrSetAddFilename (dirs, dir);
1890 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
1892 FcStrSetDestroy (dirs);
1895 FcStrSetDestroy (dirs);
1900 FcConfigAppFontClear (FcConfig *config)
1904 config = FcConfigGetCurrent ();
1909 FcConfigSetFonts (config, 0, FcSetApplication);
1913 * Manage filename-based font source selectors
1917 FcConfigGlobAdd (FcConfig *config,
1918 const FcChar8 *glob,
1921 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1923 return FcStrSetAdd (set, glob);
1927 FcConfigGlobMatch (const FcChar8 *glob,
1928 const FcChar8 *string)
1932 while ((c = *glob++))
1936 /* short circuit common case */
1939 /* short circuit another common case */
1940 if (strchr ((char *) glob, '*') == 0)
1941 string += strlen ((char *) string) - strlen ((char *) glob);
1944 if (FcConfigGlobMatch (glob, string))
1950 if (*string++ == '\0')
1959 return *string == '\0';
1963 FcConfigGlobsMatch (const FcStrSet *globs,
1964 const FcChar8 *string)
1968 for (i = 0; i < globs->num; i++)
1969 if (FcConfigGlobMatch (globs->strs[i], string))
1975 FcConfigAcceptFilename (FcConfig *config,
1976 const FcChar8 *filename)
1978 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1980 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1986 * Manage font-pattern based font source selectors
1990 FcConfigPatternsAdd (FcConfig *config,
1994 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1996 return FcFontSetAdd (set, pattern);
2000 FcConfigPatternsMatch (const FcFontSet *patterns,
2001 const FcPattern *font)
2005 for (i = 0; i < patterns->nfont; i++)
2006 if (FcListPatternMatchAny (patterns->fonts[i], font))
2012 FcConfigAcceptFont (FcConfig *config,
2013 const FcPattern *font)
2015 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2017 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2022 #include "fcaliastail.h"