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 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
33 #if defined (_WIN32) && !defined (R_OK)
45 config = malloc (sizeof (FcConfig));
48 FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig));
50 config->configDirs = FcStrSetCreate ();
51 if (!config->configDirs)
54 config->configFiles = FcStrSetCreate ();
55 if (!config->configFiles)
58 config->fontDirs = FcStrSetCreate ();
59 if (!config->fontDirs)
62 config->acceptGlobs = FcStrSetCreate ();
63 if (!config->acceptGlobs)
66 config->rejectGlobs = FcStrSetCreate ();
67 if (!config->rejectGlobs)
70 config->acceptPatterns = FcFontSetCreate ();
71 if (!config->acceptPatterns)
74 config->rejectPatterns = FcFontSetCreate ();
75 if (!config->rejectPatterns)
80 if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
84 if (config->cache == 0)
86 /* If no home, use the temp folder. */
88 int templen = GetTempPath (1, dummy);
89 FcChar8 *temp = malloc (templen + 1);
95 GetTempPath (templen + 1, temp);
96 cache_dir = FcStrPlus (temp, FC_USER_CACHE_FILE);
98 if (!FcConfigSetCache (config, cache_dir))
100 FcStrFree (cache_dir);
103 FcStrFree (cache_dir);
110 config->substPattern = 0;
111 config->substFont = 0;
112 config->maxObjects = 0;
113 for (set = FcSetSystem; set <= FcSetApplication; set++)
114 config->fonts[set] = 0;
116 config->rescanTime = time(0);
117 config->rescanInterval = 30;
122 FcFontSetDestroy (config->rejectPatterns);
124 FcFontSetDestroy (config->acceptPatterns);
126 FcStrSetDestroy (config->rejectGlobs);
128 FcStrSetDestroy (config->acceptGlobs);
130 FcStrSetDestroy (config->fontDirs);
132 FcStrSetDestroy (config->configFiles);
134 FcStrSetDestroy (config->configDirs);
137 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
143 FcConfigNewestFile (FcStrSet *files)
145 FcStrList *list = FcStrListCreate (files);
146 FcFileTime newest = { 0, FcFalse };
152 while ((file = FcStrListNext (list)))
153 if (stat ((char *) file, &statb) == 0)
154 if (!newest.set || statb.st_mtime - newest.time > 0)
157 newest.time = statb.st_mtime;
159 FcStrListDone (list);
165 FcConfigModifiedTime (FcConfig *config)
169 FcFileTime v = { 0, FcFalse };
170 config = FcConfigGetCurrent ();
174 return FcConfigNewestFile (config->configFiles);
178 FcConfigUptoDate (FcConfig *config)
180 FcFileTime config_time, font_time;
181 time_t now = time(0);
184 config = FcConfigGetCurrent ();
188 config_time = FcConfigNewestFile (config->configFiles);
189 font_time = FcConfigNewestFile (config->fontDirs);
190 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
191 (font_time.set && (font_time.time - config->rescanTime) > 0))
195 config->rescanTime = now;
200 FcSubstDestroy (FcSubst *s)
208 FcTestDestroy (s->test);
210 FcEditDestroy (s->edit);
212 FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
218 FcConfigDestroy (FcConfig *config)
222 if (config == _fcConfig)
225 FcStrSetDestroy (config->configDirs);
226 FcStrSetDestroy (config->fontDirs);
227 FcStrSetDestroy (config->configFiles);
228 FcStrSetDestroy (config->acceptGlobs);
229 FcStrSetDestroy (config->rejectGlobs);
230 FcFontSetDestroy (config->acceptPatterns);
231 FcFontSetDestroy (config->rejectPatterns);
234 FcBlanksDestroy (config->blanks);
237 FcStrFree (config->cache);
239 FcSubstDestroy (config->substPattern);
240 FcSubstDestroy (config->substFont);
241 for (set = FcSetSystem; set <= FcSetApplication; set++)
242 if (config->fonts[set])
243 FcFontSetDestroy (config->fonts[set]);
246 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
250 * Scan the current list of directories in the configuration
251 * and build the set of available fonts. Update the
252 * per-user cache file to reflect the new configuration
256 FcConfigBuildFonts (FcConfig *config)
258 FcFontSet *fonts, *cached_fonts;
259 FcGlobalCache *cache;
264 fonts = FcFontSetCreate ();
268 cache = FcGlobalCacheCreate ();
272 oldDirs = FcStrSetCreate ();
277 FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
279 cached_fonts = FcCacheRead(config, cache);
282 list = FcConfigGetFontDirs (config);
286 while ((dir = FcStrListNext (list)))
288 if (FcDebug () & FC_DBG_FONTSET)
289 printf ("build scan dir %s\n", dir);
290 FcDirScanConfig (fonts, config->fontDirs, cache,
291 config->blanks, dir, FcFalse, config);
294 FcStrListDone (list);
300 for (i = 0; i < oldDirs->num; i++)
302 if (FcDebug () & FC_DBG_FONTSET)
303 printf ("scan dir %s\n", oldDirs->strs[i]);
304 FcDirScanConfig (fonts, config->fontDirs, cache,
305 config->blanks, oldDirs->strs[i],
309 for (i = 0; i < cached_fonts->nfont; i++)
311 const char * cfn = (FcChar8 *)FcPatternFindFullFname
312 (cached_fonts->fonts[i]);
314 if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
315 (cfn && FcConfigAcceptFilename (config, cfn)))
316 FcFontSetAdd (fonts, cached_fonts->fonts[i]);
318 cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
320 cached_fonts->nfont = 0;
321 FcFontSetDestroy (cached_fonts);
324 if (FcDebug () & FC_DBG_FONTSET)
325 FcFontSetPrint (fonts);
328 FcGlobalCacheSave (cache, config->cache);
329 FcGlobalCacheDestroy (cache);
330 FcStrSetDestroy (oldDirs);
332 FcConfigSetFonts (config, fonts, FcSetSystem);
336 FcStrSetDestroy (oldDirs);
338 FcFontSetDestroy (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 FcConfigAddConfigFile (FcConfig *config,
416 FcChar8 *file = FcConfigFilename (f);
421 ret = FcStrSetAdd (config->configFiles, file);
427 FcConfigGetConfigFiles (FcConfig *config)
431 config = FcConfigGetCurrent ();
435 return FcStrListCreate (config->configFiles);
439 FcConfigSetCache (FcConfig *config,
442 FcChar8 *new = FcStrCopyFilename (c);
447 FcStrFree (config->cache);
453 FcConfigGetCache (FcConfig *config)
457 config = FcConfigGetCurrent ();
461 return config->cache;
465 FcConfigGetFonts (FcConfig *config,
470 config = FcConfigGetCurrent ();
474 return config->fonts[set];
478 FcConfigSetFonts (FcConfig *config,
482 if (config->fonts[set])
483 FcFontSetDestroy (config->fonts[set]);
484 config->fonts[set] = fonts;
490 FcConfigGetBlanks (FcConfig *config)
494 config = FcConfigGetCurrent ();
498 return config->blanks;
502 FcConfigAddBlank (FcConfig *config,
510 b = FcBlanksCreate ();
514 if (!FcBlanksAdd (b, blank))
521 FcConfigGetRescanInverval (FcConfig *config)
525 config = FcConfigGetCurrent ();
529 return config->rescanInterval;
533 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
537 config = FcConfigGetCurrent ();
541 config->rescanInterval = rescanInterval;
546 FcConfigAddEdit (FcConfig *config,
551 FcSubst *subst, **prev;
555 subst = (FcSubst *) malloc (sizeof (FcSubst));
558 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
559 if (kind == FcMatchPattern)
560 prev = &config->substPattern;
562 prev = &config->substFont;
563 for (; *prev; prev = &(*prev)->next);
569 for (t = test; t; t = t->next)
571 if (t->kind == FcMatchDefault)
575 if (config->maxObjects < num)
576 config->maxObjects = num;
577 if (FcDebug () & FC_DBG_EDIT)
579 printf ("Add Subst ");
580 FcSubstPrint (subst);
585 typedef struct _FcSubState {
591 FcConfigPromote (FcValue v, FcValue u)
593 if (v.type == FcTypeInteger)
595 v.type = FcTypeDouble;
596 v.u.d = (double) v.u.i;
598 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
600 v.u.m = &FcIdentityMatrix;
601 v.type = FcTypeMatrix;
603 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
605 v.u.l = FcLangSetPromote (v.u.s);
606 v.type = FcTypeLangSet;
612 FcConfigCompareValue (const FcValue *left_o,
614 const FcValue *right_o)
616 FcValue left = FcValueCanonicalize(left_o);
617 FcValue right = FcValueCanonicalize(right_o);
618 FcBool ret = FcFalse;
620 left = FcConfigPromote (left, right);
621 right = FcConfigPromote (right, left);
622 if (left.type == right.type)
626 break; /* FcConfigPromote prevents this from happening */
632 ret = left.u.d == right.u.d;
635 case FcOpNotContains:
636 ret = left.u.d != right.u.d;
639 ret = left.u.d < right.u.d;
642 ret = left.u.d <= right.u.d;
645 ret = left.u.d > right.u.d;
648 ret = left.u.d >= right.u.d;
659 ret = left.u.b == right.u.b;
662 case FcOpNotContains:
663 ret = left.u.b != right.u.b;
673 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
676 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
679 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
681 case FcOpNotContains:
682 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
693 ret = FcMatrixEqual (left.u.m, right.u.m);
696 case FcOpNotContains:
697 ret = !FcMatrixEqual (left.u.m, right.u.m);
707 /* left contains right if right is a subset of left */
708 ret = FcCharSetIsSubset (right.u.c, left.u.c);
710 case FcOpNotContains:
711 /* left contains right if right is a subset of left */
712 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
715 ret = FcCharSetEqual (left.u.c, right.u.c);
718 ret = !FcCharSetEqual (left.u.c, right.u.c);
728 ret = FcLangSetContains (left.u.l, right.u.l);
730 case FcOpNotContains:
731 ret = !FcLangSetContains (left.u.l, right.u.l);
734 ret = FcLangSetEqual (left.u.l, right.u.l);
737 ret = !FcLangSetEqual (left.u.l, right.u.l);
759 ret = left.u.f == right.u.f;
762 case FcOpNotContains:
763 ret = left.u.f != right.u.f;
773 if (op == FcOpNotEqual || op == FcOpNotContains)
780 #define _FcDoubleFloor(d) ((int) (d))
781 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
782 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
783 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
784 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
785 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
788 FcConfigEvaluate (FcPattern *p, FcExpr *e)
796 v.type = FcTypeInteger;
800 v.type = FcTypeDouble;
804 v.type = FcTypeString;
805 v.u.s = FcStrStaticName(e->u.sval);
809 v.type = FcTypeMatrix;
814 v.type = FcTypeCharSet;
823 r = FcPatternGet (p, e->u.field, 0, &v);
824 if (r != FcResultMatch)
828 if (FcNameConstant (e->u.constant, &v.u.i))
829 v.type = FcTypeInteger;
834 vl = FcConfigEvaluate (p, e->u.tree.left);
835 if (vl.type == FcTypeBool)
838 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
840 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
853 case FcOpNotContains:
855 vl = FcConfigEvaluate (p, e->u.tree.left);
856 vr = FcConfigEvaluate (p, e->u.tree.right);
858 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
868 vl = FcConfigEvaluate (p, e->u.tree.left);
869 vr = FcConfigEvaluate (p, e->u.tree.right);
870 vl = FcConfigPromote (vl, vr);
871 vr = FcConfigPromote (vr, vl);
872 if (vl.type == vr.type)
878 v.type = FcTypeDouble;
879 v.u.d = vl.u.d + vr.u.d;
882 v.type = FcTypeDouble;
883 v.u.d = vl.u.d - vr.u.d;
886 v.type = FcTypeDouble;
887 v.u.d = vl.u.d * vr.u.d;
890 v.type = FcTypeDouble;
891 v.u.d = vl.u.d / vr.u.d;
897 if (v.type == FcTypeDouble &&
898 v.u.d == (double) (int) v.u.d)
900 v.type = FcTypeInteger;
908 v.u.b = vl.u.b || vr.u.b;
912 v.u.b = vl.u.b && vr.u.b;
922 v.type = FcTypeString;
923 v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
936 v.type = FcTypeMatrix;
937 m = malloc (sizeof (FcMatrix));
940 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
941 FcMatrixMultiply (m, vl.u.m, vr.u.m);
965 vl = FcConfigEvaluate (p, e->u.tree.left);
978 vl = FcConfigEvaluate (p, e->u.tree.left);
984 v.type = FcTypeInteger;
985 v.u.i = FcDoubleFloor (vl.u.d);
994 vl = FcConfigEvaluate (p, e->u.tree.left);
1000 v.type = FcTypeInteger;
1001 v.u.i = FcDoubleCeil (vl.u.d);
1004 v.type = FcTypeVoid;
1007 FcValueDestroy (vl);
1010 vl = FcConfigEvaluate (p, e->u.tree.left);
1016 v.type = FcTypeInteger;
1017 v.u.i = FcDoubleRound (vl.u.d);
1020 v.type = FcTypeVoid;
1023 FcValueDestroy (vl);
1026 vl = FcConfigEvaluate (p, e->u.tree.left);
1032 v.type = FcTypeInteger;
1033 v.u.i = FcDoubleTrunc (vl.u.d);
1036 v.type = FcTypeVoid;
1039 FcValueDestroy (vl);
1042 v.type = FcTypeVoid;
1048 static FcValueList *
1049 FcConfigMatchValueList (FcPattern *p,
1051 FcValueList *values)
1053 FcValueList *ret = 0;
1054 FcExpr *e = t->expr;
1060 /* Compute the value of the match expression */
1061 if (e->op == FcOpComma)
1063 value = FcConfigEvaluate (p, e->u.tree.left);
1064 e = e->u.tree.right;
1068 value = FcConfigEvaluate (p, e);
1072 for (v = values; v; v = FcValueListPtrU(v->next))
1074 /* Compare the pattern value to the match expression value */
1075 if (FcConfigCompareValue (&v->value, t->op, &value))
1082 if (t->qual == FcQualAll)
1089 FcValueDestroy (value);
1094 static FcValueList *
1095 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1101 l = (FcValueList *) malloc (sizeof (FcValueList));
1104 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1105 if (e->op == FcOpComma)
1107 l->value = FcConfigEvaluate (p, e->u.tree.left);
1108 l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
1112 l->value = FcConfigEvaluate (p, e);
1113 l->next = FcValueListPtrCreateDynamic(0);
1115 l->binding = binding;
1116 if (l->value.type == FcTypeVoid)
1118 FcValueList *next = FcValueListPtrU(l->next);
1120 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1129 FcConfigAdd (FcValueListPtr *head,
1130 FcValueList *position,
1134 FcValueListPtr *prev, last, v;
1135 FcValueBinding sameBinding;
1138 sameBinding = position->binding;
1140 sameBinding = FcValueBindingWeak;
1141 for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v);
1142 v = FcValueListPtrU(v)->next)
1143 if (FcValueListPtrU(v)->binding == FcValueBindingSame)
1144 FcValueListPtrU(v)->binding = sameBinding;
1148 prev = &position->next;
1150 for (prev = head; FcValueListPtrU(*prev);
1151 prev = &(FcValueListPtrU(*prev)->next))
1158 for (prev = head; FcValueListPtrU(*prev);
1159 prev = &(FcValueListPtrU(*prev)->next))
1161 if (FcValueListPtrU(*prev) == position)
1168 if (FcDebug () & FC_DBG_EDIT)
1170 if (!FcValueListPtrU(*prev))
1171 printf ("position not on list\n");
1175 if (FcDebug () & FC_DBG_EDIT)
1177 printf ("%s list before ", append ? "Append" : "Prepend");
1178 FcValueListPrint (*head);
1184 last = FcValueListPtrCreateDynamic(new);
1185 while (FcValueListPtrU(FcValueListPtrU(last)->next))
1186 last = FcValueListPtrU(last)->next;
1188 FcValueListPtrU(last)->next = *prev;
1189 *prev = FcValueListPtrCreateDynamic(new);
1192 if (FcDebug () & FC_DBG_EDIT)
1194 printf ("%s list after ", append ? "Append" : "Prepend");
1195 FcValueListPrint (*head);
1203 FcConfigDel (FcValueListPtr *head,
1204 FcValueList *position)
1206 FcValueListPtr *prev;
1208 for (prev = head; FcValueListPtrU(*prev);
1209 prev = &(FcValueListPtrU(*prev)->next))
1211 if (FcValueListPtrU(*prev) == position)
1213 *prev = position->next;
1214 position->next = FcValueListPtrCreateDynamic(0);
1215 FcValueListDestroy (FcValueListPtrCreateDynamic(position));
1222 FcConfigPatternAdd (FcPattern *p,
1229 FcPatternElt *e = FcPatternInsertElt (p, object);
1233 FcConfigAdd (&e->values, 0, append, list);
1238 * Delete all values associated with a field
1241 FcConfigPatternDel (FcPattern *p,
1244 FcPatternElt *e = FcPatternFindElt (p, object);
1247 while (FcValueListPtrU(e->values))
1248 FcConfigDel (&e->values, FcValueListPtrU(e->values));
1252 FcConfigPatternCanon (FcPattern *p,
1255 FcPatternElt *e = FcPatternFindElt (p, object);
1258 if (!FcValueListPtrU(e->values))
1259 FcPatternDel (p, object);
1263 FcConfigSubstituteWithPat (FcConfig *config,
1278 config = FcConfigGetCurrent ();
1283 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1284 if (!st && config->maxObjects)
1286 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1288 if (FcDebug () & FC_DBG_EDIT)
1290 printf ("FcConfigSubstitute ");
1293 if (kind == FcMatchPattern)
1294 s = config->substPattern;
1296 s = config->substFont;
1297 for (; s; s = s->next)
1300 * Check the tests to see if
1301 * they all match the pattern
1303 for (t = s->test, i = 0; t; t = t->next, i++)
1305 if (FcDebug () & FC_DBG_EDIT)
1307 printf ("FcConfigSubstitute test ");
1311 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1316 st[i].elt = FcPatternFindElt (m, t->field);
1320 * If there's no such field in the font,
1321 * then FcQualAll matches while FcQualAny does not
1325 if (t->qual == FcQualAll)
1334 * Check to see if there is a match, mark the location
1335 * to apply match-relative edits
1337 st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
1340 if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
1342 if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
1347 if (FcDebug () & FC_DBG_EDIT)
1348 printf ("No match\n");
1351 if (FcDebug () & FC_DBG_EDIT)
1353 printf ("Substitute ");
1356 for (e = s->edit; e; e = e->next)
1359 * Evaluate the list of expressions
1361 l = FcConfigValues (p, e->expr, e->binding);
1363 * Locate any test associated with this field, skipping
1364 * tests associated with the pattern when substituting in
1367 for (t = s->test, i = 0; t; t = t->next, i++)
1369 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1370 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1371 (FcChar8 *) e->field))
1374 * KLUDGE - the pattern may have been reallocated or
1375 * things may have been inserted or deleted above
1376 * this element by other edits. Go back and find
1379 if (e != s->edit && st[i].elt)
1380 st[i].elt = FcPatternFindElt (p, t->field);
1389 * If there was a test, then replace the matched
1390 * value with the new list of values
1394 FcValueList *thisValue = st[i].value;
1395 FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
1398 * Append the new list of values after the current value
1400 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1402 * Delete the marked value
1404 FcConfigDel (&st[i].elt->values, thisValue);
1406 * Adjust any pointers into the value list to ensure
1407 * future edits occur at the same place
1409 for (t = s->test, i = 0; t; t = t->next, i++)
1411 if (st[i].value == thisValue)
1412 st[i].value = nextValue;
1416 /* fall through ... */
1417 case FcOpAssignReplace:
1419 * Delete all of the values and insert
1422 FcConfigPatternDel (p, e->field);
1423 FcConfigPatternAdd (p, e->field, l, FcTrue);
1425 * Adjust any pointers into the value list as they no
1426 * longer point to anything valid
1430 FcPatternElt *thisElt = st[i].elt;
1431 for (t = s->test, i = 0; t; t = t->next, i++)
1433 if (st[i].elt == thisElt)
1441 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1444 /* fall through ... */
1445 case FcOpPrependFirst:
1446 FcConfigPatternAdd (p, e->field, l, FcFalse);
1451 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1454 /* fall through ... */
1455 case FcOpAppendLast:
1456 FcConfigPatternAdd (p, e->field, l, FcTrue);
1463 * Now go through the pattern and eliminate
1464 * any properties without data
1466 for (e = s->edit; e; e = e->next)
1467 FcConfigPatternCanon (p, e->field);
1469 if (FcDebug () & FC_DBG_EDIT)
1471 printf ("FcConfigSubstitute edit");
1475 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1477 if (FcDebug () & FC_DBG_EDIT)
1479 printf ("FcConfigSubstitute done");
1486 FcConfigSubstitute (FcConfig *config,
1490 return FcConfigSubstituteWithPat (config, p, 0, kind);
1493 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1495 static FcChar8 fontconfig_path[1000] = "";
1498 DllMain (HINSTANCE hinstDLL,
1504 switch (fdwReason) {
1505 case DLL_PROCESS_ATTACH:
1506 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1507 sizeof (fontconfig_path)))
1510 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1511 * assume it's a Unix-style installation tree, and use
1512 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1513 * folder where the DLL is as FONTCONFIG_PATH.
1515 p = strrchr (fontconfig_path, '\\');
1519 p = strrchr (fontconfig_path, '\\');
1520 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1521 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1523 strcat (fontconfig_path, "\\etc\\fonts");
1526 fontconfig_path[0] = '\0';
1534 #undef FONTCONFIG_PATH
1535 #define FONTCONFIG_PATH fontconfig_path
1537 #else /* !(_WIN32 && PIC) */
1539 #endif /* !(_WIN32 && PIC) */
1541 #ifndef FONTCONFIG_FILE
1542 #define FONTCONFIG_FILE "fonts.conf"
1546 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1551 dir = (FcChar8 *) "";
1552 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1556 strcpy ((char *) path, (const char *) dir);
1557 /* make sure there's a single separator */
1559 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1560 path[strlen((char *) path)-1] != '\\')) &&
1563 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1564 strcat ((char *) path, "\\");
1566 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1567 strcat ((char *) path, "/");
1569 strcat ((char *) path, (char *) file);
1571 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1572 if (access ((char *) path, R_OK) == 0)
1580 FcConfigGetPath (void)
1583 FcChar8 *env, *e, *colon;
1588 npath = 2; /* default dir + null */
1589 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1595 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1598 path = calloc (npath, sizeof (FcChar8 *));
1608 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1610 colon = e + strlen ((char *) e);
1611 path[i] = malloc (colon - e + 1);
1614 strncpy ((char *) path[i], (const char *) e, colon - e);
1615 path[i][colon - e] = '\0';
1624 dir = (FcChar8 *) FONTCONFIG_PATH;
1625 path[i] = malloc (strlen ((char *) dir) + 1);
1628 strcpy ((char *) path[i], (const char *) dir);
1632 for (i = 0; path[i]; i++)
1640 FcConfigFreePath (FcChar8 **path)
1644 for (p = path; *p; p++)
1649 static FcBool _FcConfigHomeEnabled = FcTrue;
1654 if (_FcConfigHomeEnabled)
1656 char *home = getenv ("HOME");
1660 home = getenv ("USERPROFILE");
1663 return (FcChar8 *) home;
1669 FcConfigEnableHome (FcBool enable)
1671 FcBool prev = _FcConfigHomeEnabled;
1672 _FcConfigHomeEnabled = enable;
1677 FcConfigFilename (const FcChar8 *url)
1679 FcChar8 *file, *dir, **path, **p;
1683 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1685 url = (FcChar8 *) FONTCONFIG_FILE;
1690 if (isalpha (*url) &&
1692 (url[2] == '/' || url[2] == '\\'))
1698 dir = FcConfigHome ();
1700 file = FcConfigFileExists (dir, url + 1);
1709 file = FcConfigFileExists (0, url);
1712 path = FcConfigGetPath ();
1715 for (p = path; *p; p++)
1717 file = FcConfigFileExists (*p, url);
1721 FcConfigFreePath (path);
1728 * Manage the application-specific fonts
1732 FcConfigAppFontAddFile (FcConfig *config,
1733 const FcChar8 *file)
1742 config = FcConfigGetCurrent ();
1747 subdirs = FcStrSetCreate ();
1751 set = FcConfigGetFonts (config, FcSetApplication);
1754 set = FcFontSetCreate ();
1757 FcStrSetDestroy (subdirs);
1760 FcConfigSetFonts (config, set, FcSetApplication);
1763 if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
1765 FcStrSetDestroy (subdirs);
1768 if ((sublist = FcStrListCreate (subdirs)))
1770 while ((subdir = FcStrListNext (sublist)))
1772 FcConfigAppFontAddDir (config, subdir);
1774 FcStrListDone (sublist);
1780 FcConfigAppFontAddDir (FcConfig *config,
1790 config = FcConfigGetCurrent ();
1794 subdirs = FcStrSetCreate ();
1798 set = FcConfigGetFonts (config, FcSetApplication);
1801 set = FcFontSetCreate ();
1804 FcStrSetDestroy (subdirs);
1807 FcConfigSetFonts (config, set, FcSetApplication);
1810 if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
1812 FcStrSetDestroy (subdirs);
1815 if ((sublist = FcStrListCreate (subdirs)))
1817 while ((subdir = FcStrListNext (sublist)))
1819 FcConfigAppFontAddDir (config, subdir);
1821 FcStrListDone (sublist);
1827 FcConfigAppFontClear (FcConfig *config)
1831 config = FcConfigGetCurrent ();
1836 FcConfigSetFonts (config, 0, FcSetApplication);
1840 * Manage filename-based font source selectors
1844 FcConfigGlobAdd (FcConfig *config,
1845 const FcChar8 *glob,
1848 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1850 return FcStrSetAdd (set, glob);
1854 FcConfigGlobMatch (const FcChar8 *glob,
1855 const FcChar8 *string)
1859 while ((c = *glob++))
1863 /* short circuit common case */
1866 /* short circuit another common case */
1867 if (strchr ((char *) glob, '*') == 0)
1868 string += strlen ((char *) string) - strlen ((char *) glob);
1871 if (FcConfigGlobMatch (glob, string))
1877 if (*string++ == '\0')
1886 return *string == '\0';
1890 FcConfigGlobsMatch (const FcStrSet *globs,
1891 const FcChar8 *string)
1895 for (i = 0; i < globs->num; i++)
1896 if (FcConfigGlobMatch (globs->strs[i], string))
1902 FcConfigAcceptFilename (FcConfig *config,
1903 const FcChar8 *filename)
1905 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1907 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1913 * Manage font-pattern based font source selectors
1917 FcConfigPatternsAdd (FcConfig *config,
1921 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1923 return FcFontSetAdd (set, pattern);
1927 FcConfigPatternsMatch (const FcFontSet *patterns,
1928 const FcPattern *font)
1932 for (i = 0; i < patterns->nfont; i++)
1933 if (FcListPatternMatchAny (patterns->fonts[i], font))
1939 FcConfigAcceptFont (FcConfig *config,
1940 const FcPattern *font)
1942 if (FcConfigPatternsMatch (config->acceptPatterns, font))
1944 if (FcConfigPatternsMatch (config->rejectPatterns, font))