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));
51 config->configDirs = FcStrSetCreate ();
52 if (!config->configDirs)
55 config->configFiles = FcStrSetCreate ();
56 if (!config->configFiles)
59 config->fontDirs = FcStrSetCreate ();
60 if (!config->fontDirs)
63 config->acceptGlobs = FcStrSetCreate ();
64 if (!config->acceptGlobs)
67 config->rejectGlobs = FcStrSetCreate ();
68 if (!config->rejectGlobs)
71 config->acceptPatterns = FcFontSetCreate ();
72 if (!config->acceptPatterns)
75 config->rejectPatterns = FcFontSetCreate ();
76 if (!config->rejectPatterns)
79 config->cacheDirs = FcStrSetCreate ();
80 if (!config->cacheDirs)
85 config->substPattern = 0;
86 config->substFont = 0;
87 config->substScan = 0;
88 config->maxObjects = 0;
89 for (set = FcSetSystem; set <= FcSetApplication; set++)
90 config->fonts[set] = 0;
92 config->rescanTime = time(0);
93 config->rescanInterval = 30;
95 config->expr_pool = NULL;
102 FcFontSetDestroy (config->rejectPatterns);
104 FcFontSetDestroy (config->acceptPatterns);
106 FcStrSetDestroy (config->rejectGlobs);
108 FcStrSetDestroy (config->acceptGlobs);
110 FcStrSetDestroy (config->fontDirs);
112 FcStrSetDestroy (config->configFiles);
114 FcStrSetDestroy (config->configDirs);
122 FcConfigNewestFile (FcStrSet *files)
124 FcStrList *list = FcStrListCreate (files);
125 FcFileTime newest = { 0, FcFalse };
131 while ((file = FcStrListNext (list)))
132 if (FcStat (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);
196 FcConfigAllocExpr (FcConfig *config)
198 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
200 FcExprPage *new_page;
202 new_page = malloc (sizeof (FcExprPage));
206 new_page->next_page = config->expr_pool;
207 new_page->next = new_page->exprs;
208 config->expr_pool = new_page;
211 return config->expr_pool->next++;
215 FcConfigReference (FcConfig *config)
219 config = FcConfigGetCurrent ();
230 FcConfigDestroy (FcConfig *config)
235 if (--config->ref > 0)
238 if (config == _fcConfig)
241 FcStrSetDestroy (config->configDirs);
242 FcStrSetDestroy (config->fontDirs);
243 FcStrSetDestroy (config->cacheDirs);
244 FcStrSetDestroy (config->configFiles);
245 FcStrSetDestroy (config->acceptGlobs);
246 FcStrSetDestroy (config->rejectGlobs);
247 FcFontSetDestroy (config->acceptPatterns);
248 FcFontSetDestroy (config->rejectPatterns);
251 FcBlanksDestroy (config->blanks);
253 FcSubstDestroy (config->substPattern);
254 FcSubstDestroy (config->substFont);
255 FcSubstDestroy (config->substScan);
256 for (set = FcSetSystem; set <= FcSetApplication; set++)
257 if (config->fonts[set])
258 FcFontSetDestroy (config->fonts[set]);
260 page = config->expr_pool;
263 FcExprPage *next = page->next_page;
272 * Add cache to configuration, adding fonts and directories
276 FcConfigAddCache (FcConfig *config, FcCache *cache,
277 FcSetName set, FcStrSet *dirSet)
286 fs = FcCacheSet (cache);
291 for (i = 0; i < fs->nfont; i++)
293 FcPattern *font = FcFontSetFont (fs, i);
297 * Check to see if font is banned by filename
299 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
300 0, &font_file) == FcResultMatch &&
301 !FcConfigAcceptFilename (config, font_file))
307 * Check to see if font is banned by pattern
309 if (!FcConfigAcceptFont (config, font))
313 FcFontSetAdd (config->fonts[set], font);
315 FcDirCacheReference (cache, nref);
321 dirs = FcCacheDirs (cache);
324 for (i = 0; i < cache->dirs_count; i++)
326 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
327 if (FcConfigAcceptFilename (config, dir))
328 FcStrSetAddFilename (dirSet, dir);
335 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
341 dirlist = FcStrListCreate (dirSet);
345 while ((dir = FcStrListNext (dirlist)))
347 if (FcDebug () & FC_DBG_FONTSET)
348 printf ("adding fonts from%s\n", dir);
349 cache = FcDirCacheRead (dir, FcFalse, config);
352 FcConfigAddCache (config, cache, set, dirSet);
353 FcDirCacheUnload (cache);
355 FcStrListDone (dirlist);
360 * Scan the current list of directories in the configuration
361 * and build the set of available fonts.
365 FcConfigBuildFonts (FcConfig *config)
371 config = FcConfigGetCurrent ();
376 fonts = FcFontSetCreate ();
380 FcConfigSetFonts (config, fonts, FcSetSystem);
382 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
384 if (FcDebug () & FC_DBG_FONTSET)
385 FcFontSetPrint (fonts);
390 FcConfigSetCurrent (FcConfig *config)
392 if (config == _fcConfig)
395 if (!config->fonts[FcSetSystem])
396 if (!FcConfigBuildFonts (config))
400 FcConfigDestroy (_fcConfig);
406 FcConfigGetCurrent (void)
415 FcConfigAddConfigDir (FcConfig *config,
418 return FcStrSetAddFilename (config->configDirs, d);
422 FcConfigGetConfigDirs (FcConfig *config)
426 config = FcConfigGetCurrent ();
430 return FcStrListCreate (config->configDirs);
434 FcConfigAddFontDir (FcConfig *config,
437 return FcStrSetAddFilename (config->fontDirs, d);
441 FcConfigAddDir (FcConfig *config,
444 return (FcConfigAddConfigDir (config, d) &&
445 FcConfigAddFontDir (config, d));
449 FcConfigGetFontDirs (FcConfig *config)
453 config = FcConfigGetCurrent ();
457 return FcStrListCreate (config->fontDirs);
461 FcConfigAddCacheDir (FcConfig *config,
464 return FcStrSetAddFilename (config->cacheDirs, d);
468 FcConfigGetCacheDirs (const FcConfig *config)
472 config = FcConfigGetCurrent ();
476 return FcStrListCreate (config->cacheDirs);
480 FcConfigAddConfigFile (FcConfig *config,
484 FcChar8 *file = FcConfigFilename (f);
489 ret = FcStrSetAdd (config->configFiles, file);
495 FcConfigGetConfigFiles (FcConfig *config)
499 config = FcConfigGetCurrent ();
503 return FcStrListCreate (config->configFiles);
507 FcConfigGetCache (FcConfig *config FC_UNUSED)
513 FcConfigGetFonts (FcConfig *config,
518 config = FcConfigGetCurrent ();
522 return config->fonts[set];
526 FcConfigSetFonts (FcConfig *config,
530 if (config->fonts[set])
531 FcFontSetDestroy (config->fonts[set]);
532 config->fonts[set] = fonts;
536 FcConfigGetBlanks (FcConfig *config)
540 config = FcConfigGetCurrent ();
544 return config->blanks;
548 FcConfigAddBlank (FcConfig *config,
551 FcBlanks *b, *freeme = 0;
556 freeme = b = FcBlanksCreate ();
560 if (!FcBlanksAdd (b, blank))
563 FcBlanksDestroy (freeme);
571 FcConfigGetRescanInterval (FcConfig *config)
575 config = FcConfigGetCurrent ();
579 return config->rescanInterval;
583 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
587 config = FcConfigGetCurrent ();
591 config->rescanInterval = rescanInterval;
596 * A couple of typos escaped into the library
599 FcConfigGetRescanInverval (FcConfig *config)
601 return FcConfigGetRescanInterval (config);
605 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
607 return FcConfigSetRescanInterval (config, rescanInterval);
612 FcConfigAddEdit (FcConfig *config,
617 FcSubst *subst, **prev;
623 prev = &config->substPattern;
626 prev = &config->substFont;
629 prev = &config->substScan;
634 subst = (FcSubst *) malloc (sizeof (FcSubst));
637 for (; *prev; prev = &(*prev)->next);
643 for (t = test; t; t = t->next)
645 if (t->kind == FcMatchDefault)
649 if (config->maxObjects < num)
650 config->maxObjects = num;
651 if (FcDebug () & FC_DBG_EDIT)
653 printf ("Add Subst ");
654 FcSubstPrint (subst);
659 typedef struct _FcSubState {
665 FcConfigPromote (FcValue v, FcValue u)
667 if (v.type == FcTypeInteger)
669 v.type = FcTypeDouble;
670 v.u.d = (double) v.u.i;
672 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
674 v.u.m = &FcIdentityMatrix;
675 v.type = FcTypeMatrix;
677 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
679 v.u.l = FcLangSetPromote (v.u.s);
680 v.type = FcTypeLangSet;
686 FcConfigCompareValue (const FcValue *left_o,
688 const FcValue *right_o)
690 FcValue left = FcValueCanonicalize(left_o);
691 FcValue right = FcValueCanonicalize(right_o);
692 FcBool ret = FcFalse;
693 FcOp op = FC_OP_GET_OP (op_);
694 int flags = FC_OP_GET_FLAGS (op_);
696 left = FcConfigPromote (left, right);
697 right = FcConfigPromote (right, left);
698 if (left.type == right.type)
702 break; /* FcConfigPromote prevents this from happening */
708 ret = left.u.d == right.u.d;
711 case FcOpNotContains:
712 ret = left.u.d != right.u.d;
715 ret = left.u.d < right.u.d;
718 ret = left.u.d <= right.u.d;
721 ret = left.u.d > right.u.d;
724 ret = left.u.d >= right.u.d;
735 ret = left.u.b == right.u.b;
738 case FcOpNotContains:
739 ret = left.u.b != right.u.b;
749 if (flags & FcOpFlagIgnoreBlanks)
750 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) == 0;
752 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
755 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
758 if (flags & FcOpFlagIgnoreBlanks)
759 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) != 0;
761 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
763 case FcOpNotContains:
764 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
775 ret = FcMatrixEqual (left.u.m, right.u.m);
778 case FcOpNotContains:
779 ret = !FcMatrixEqual (left.u.m, right.u.m);
789 /* left contains right if right is a subset of left */
790 ret = FcCharSetIsSubset (right.u.c, left.u.c);
792 case FcOpNotContains:
793 /* left contains right if right is a subset of left */
794 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
797 ret = FcCharSetEqual (left.u.c, right.u.c);
800 ret = !FcCharSetEqual (left.u.c, right.u.c);
810 ret = FcLangSetContains (left.u.l, right.u.l);
812 case FcOpNotContains:
813 ret = !FcLangSetContains (left.u.l, right.u.l);
816 ret = FcLangSetEqual (left.u.l, right.u.l);
819 ret = !FcLangSetEqual (left.u.l, right.u.l);
841 ret = left.u.f == right.u.f;
844 case FcOpNotContains:
845 ret = left.u.f != right.u.f;
855 if (op == FcOpNotEqual || op == FcOpNotContains)
862 #define _FcDoubleFloor(d) ((int) (d))
863 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
864 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
865 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
866 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
867 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
870 FcConfigEvaluate (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e)
876 FcOp op = FC_OP_GET_OP (e->op);
880 v.type = FcTypeInteger;
884 v.type = FcTypeDouble;
888 v.type = FcTypeString;
895 FcValue xx, xy, yx, yy;
896 v.type = FcTypeMatrix;
897 xx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xx), v);
898 xy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xy), v);
899 yx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yx), v);
900 yy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yy), v);
901 if (xx.type == FcTypeDouble && xy.type == FcTypeDouble &&
902 yx.type == FcTypeDouble && yy.type == FcTypeDouble)
916 v.type = FcTypeCharSet;
921 v.type = FcTypeLangSet;
930 if (kind == FcMatchFont && e->u.name.kind == FcMatchPattern)
931 r = FcPatternObjectGet (p_pat, e->u.name.object, 0, &v);
933 r = FcPatternObjectGet (p, e->u.name.object, 0, &v);
934 if (r != FcResultMatch)
939 if (FcNameConstant (e->u.constant, &v.u.i))
940 v.type = FcTypeInteger;
945 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
946 if (vl.type == FcTypeBool)
949 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.left);
951 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.right);
964 case FcOpNotContains:
966 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
967 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
969 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
979 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
980 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
981 vl = FcConfigPromote (vl, vr);
982 vr = FcConfigPromote (vr, vl);
983 if (vl.type == vr.type)
985 switch ((int) vl.type) {
989 v.type = FcTypeDouble;
990 v.u.d = vl.u.d + vr.u.d;
993 v.type = FcTypeDouble;
994 v.u.d = vl.u.d - vr.u.d;
997 v.type = FcTypeDouble;
998 v.u.d = vl.u.d * vr.u.d;
1001 v.type = FcTypeDouble;
1002 v.u.d = vl.u.d / vr.u.d;
1005 v.type = FcTypeVoid;
1008 if (v.type == FcTypeDouble &&
1009 v.u.d == (double) (int) v.u.d)
1011 v.type = FcTypeInteger;
1012 v.u.i = (int) v.u.d;
1018 v.type = FcTypeBool;
1019 v.u.b = vl.u.b || vr.u.b;
1022 v.type = FcTypeBool;
1023 v.u.b = vl.u.b && vr.u.b;
1026 v.type = FcTypeVoid;
1033 v.type = FcTypeString;
1034 str = FcStrPlus (vl.u.s, vr.u.s);
1035 v.u.s = FcSharedStr (str);
1039 v.type = FcTypeVoid;
1042 v.type = FcTypeVoid;
1049 v.type = FcTypeMatrix;
1050 m = malloc (sizeof (FcMatrix));
1053 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1058 v.type = FcTypeVoid;
1062 v.type = FcTypeVoid;
1069 v.type = FcTypeCharSet;
1070 v.u.c = FcCharSetUnion (vl.u.c, vr.u.c);
1072 v.type = FcTypeVoid;
1075 v.type = FcTypeCharSet;
1076 v.u.c = FcCharSetSubtract (vl.u.c, vr.u.c);
1078 v.type = FcTypeVoid;
1081 v.type = FcTypeVoid;
1088 v.type = FcTypeLangSet;
1089 v.u.l = FcLangSetUnion (vl.u.l, vr.u.l);
1091 v.type = FcTypeVoid;
1094 v.type = FcTypeLangSet;
1095 v.u.l = FcLangSetSubtract (vl.u.l, vr.u.l);
1097 v.type = FcTypeVoid;
1100 v.type = FcTypeVoid;
1105 v.type = FcTypeVoid;
1110 v.type = FcTypeVoid;
1111 FcValueDestroy (vl);
1112 FcValueDestroy (vr);
1115 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1116 switch ((int) vl.type) {
1118 v.type = FcTypeBool;
1122 v.type = FcTypeVoid;
1125 FcValueDestroy (vl);
1128 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1129 switch ((int) vl.type) {
1134 v.type = FcTypeInteger;
1135 v.u.i = FcDoubleFloor (vl.u.d);
1138 v.type = FcTypeVoid;
1141 FcValueDestroy (vl);
1144 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1145 switch ((int) vl.type) {
1150 v.type = FcTypeInteger;
1151 v.u.i = FcDoubleCeil (vl.u.d);
1154 v.type = FcTypeVoid;
1157 FcValueDestroy (vl);
1160 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1161 switch ((int) vl.type) {
1166 v.type = FcTypeInteger;
1167 v.u.i = FcDoubleRound (vl.u.d);
1170 v.type = FcTypeVoid;
1173 FcValueDestroy (vl);
1176 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1177 switch ((int) vl.type) {
1182 v.type = FcTypeInteger;
1183 v.u.i = FcDoubleTrunc (vl.u.d);
1186 v.type = FcTypeVoid;
1189 FcValueDestroy (vl);
1192 v.type = FcTypeVoid;
1198 static FcValueList *
1199 FcConfigMatchValueList (FcPattern *p,
1203 FcValueList *values)
1205 FcValueList *ret = 0;
1206 FcExpr *e = t->expr;
1212 /* Compute the value of the match expression */
1213 if (FC_OP_GET_OP (e->op) == FcOpComma)
1215 value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1216 e = e->u.tree.right;
1220 value = FcConfigEvaluate (p, p_pat, kind, e);
1224 for (v = values; v; v = FcValueListNext(v))
1226 /* Compare the pattern value to the match expression value */
1227 if (FcConfigCompareValue (&v->value, t->op, &value))
1234 if (t->qual == FcQualAll)
1241 FcValueDestroy (value);
1246 static FcValueList *
1247 FcConfigValues (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e, FcValueBinding binding)
1253 l = (FcValueList *) malloc (sizeof (FcValueList));
1256 if (FC_OP_GET_OP (e->op) == FcOpComma)
1258 l->value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1259 l->next = FcConfigValues (p, p_pat, kind, e->u.tree.right, binding);
1263 l->value = FcConfigEvaluate (p, p_pat, kind, e);
1266 l->binding = binding;
1267 if (l->value.type == FcTypeVoid)
1269 FcValueList *next = FcValueListNext(l);
1279 FcConfigAdd (FcValueListPtr *head,
1280 FcValueList *position,
1284 FcValueListPtr *prev, last, v;
1285 FcValueBinding sameBinding;
1288 sameBinding = position->binding;
1290 sameBinding = FcValueBindingWeak;
1291 for (v = new; v != NULL; v = FcValueListNext(v))
1292 if (v->binding == FcValueBindingSame)
1293 v->binding = sameBinding;
1297 prev = &position->next;
1299 for (prev = head; *prev != NULL;
1300 prev = &(*prev)->next)
1307 for (prev = head; *prev != NULL;
1308 prev = &(*prev)->next)
1310 if (*prev == position)
1317 if (FcDebug () & FC_DBG_EDIT)
1320 printf ("position not on list\n");
1324 if (FcDebug () & FC_DBG_EDIT)
1326 printf ("%s list before ", append ? "Append" : "Prepend");
1327 FcValueListPrintWithPosition (*head, *prev);
1334 while (last->next != NULL)
1341 if (FcDebug () & FC_DBG_EDIT)
1343 printf ("%s list after ", append ? "Append" : "Prepend");
1344 FcValueListPrint (*head);
1352 FcConfigDel (FcValueListPtr *head,
1353 FcValueList *position)
1355 FcValueListPtr *prev;
1357 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1359 if (*prev == position)
1361 *prev = position->next;
1362 position->next = NULL;
1363 FcValueListDestroy (position);
1370 FcConfigPatternAdd (FcPattern *p,
1377 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1381 FcConfigAdd (&e->values, 0, append, list);
1386 * Delete all values associated with a field
1389 FcConfigPatternDel (FcPattern *p,
1392 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1395 while (e->values != NULL)
1396 FcConfigDel (&e->values, e->values);
1400 FcConfigPatternCanon (FcPattern *p,
1403 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1406 if (e->values == NULL)
1407 FcPatternObjectDel (p, object);
1411 FcConfigSubstituteWithPat (FcConfig *config,
1427 config = FcConfigGetCurrent ();
1433 case FcMatchPattern:
1434 s = config->substPattern;
1435 strs = FcGetDefaultLangs ();
1438 FcStrList *l = FcStrListCreate (strs);
1442 FcStrSetDestroy (strs);
1443 while (l && (lang = FcStrListNext (l)))
1445 v.type = FcTypeString;
1447 FcPatternObjectAddWithBinding (p, FC_LANG_OBJECT, v, FcValueBindingWeak, FcTrue);
1453 s = config->substFont;
1456 s = config->substScan;
1462 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1463 if (!st && config->maxObjects)
1466 if (FcDebug () & FC_DBG_EDIT)
1468 printf ("FcConfigSubstitute ");
1471 for (; s; s = s->next)
1474 * Check the tests to see if
1475 * they all match the pattern
1477 for (t = s->test, i = 0; t; t = t->next, i++)
1479 if (FcDebug () & FC_DBG_EDIT)
1481 printf ("FcConfigSubstitute test ");
1485 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1490 st[i].elt = FcPatternObjectFindElt (m, t->object);
1494 * If there's no such field in the font,
1495 * then FcQualAll matches while FcQualAny does not
1499 if (t->qual == FcQualAll)
1508 * Check to see if there is a match, mark the location
1509 * to apply match-relative edits
1511 st[i].value = FcConfigMatchValueList (m, p_pat, kind, t, st[i].elt->values);
1514 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1516 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1521 if (FcDebug () & FC_DBG_EDIT)
1522 printf ("No match\n");
1525 if (FcDebug () & FC_DBG_EDIT)
1527 printf ("Substitute ");
1530 for (e = s->edit; e; e = e->next)
1533 * Evaluate the list of expressions
1535 l = FcConfigValues (p, p_pat,kind, e->expr, e->binding);
1537 * Locate any test associated with this field, skipping
1538 * tests associated with the pattern when substituting in
1541 for (t = s->test, i = 0; t; t = t->next, i++)
1543 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1544 t->object == e->object)
1547 * KLUDGE - the pattern may have been reallocated or
1548 * things may have been inserted or deleted above
1549 * this element by other edits. Go back and find
1552 if (e != s->edit && st[i].elt)
1553 st[i].elt = FcPatternObjectFindElt (p, t->object);
1559 switch (FC_OP_GET_OP (e->op)) {
1562 * If there was a test, then replace the matched
1563 * value with the new list of values
1567 FcValueList *thisValue = st[i].value;
1568 FcValueList *nextValue = thisValue;
1571 * Append the new list of values after the current value
1573 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1575 * Delete the marked value
1578 FcConfigDel (&st[i].elt->values, thisValue);
1580 * Adjust any pointers into the value list to ensure
1581 * future edits occur at the same place
1583 for (t = s->test, i = 0; t; t = t->next, i++)
1585 if (st[i].value == thisValue)
1586 st[i].value = nextValue;
1590 /* fall through ... */
1591 case FcOpAssignReplace:
1593 * Delete all of the values and insert
1596 FcConfigPatternDel (p, e->object);
1597 FcConfigPatternAdd (p, e->object, l, FcTrue);
1599 * Adjust any pointers into the value list as they no
1600 * longer point to anything valid
1604 FcPatternElt *thisElt = st[i].elt;
1605 for (t = s->test, i = 0; t; t = t->next, i++)
1607 if (st[i].elt == thisElt)
1615 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1618 /* fall through ... */
1619 case FcOpPrependFirst:
1620 FcConfigPatternAdd (p, e->object, l, FcFalse);
1625 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1628 /* fall through ... */
1629 case FcOpAppendLast:
1630 FcConfigPatternAdd (p, e->object, l, FcTrue);
1633 FcValueListDestroy (l);
1638 * Now go through the pattern and eliminate
1639 * any properties without data
1641 for (e = s->edit; e; e = e->next)
1642 FcConfigPatternCanon (p, e->object);
1644 if (FcDebug () & FC_DBG_EDIT)
1646 printf ("FcConfigSubstitute edit");
1651 if (FcDebug () & FC_DBG_EDIT)
1653 printf ("FcConfigSubstitute done");
1660 FcConfigSubstitute (FcConfig *config,
1664 return FcConfigSubstituteWithPat (config, p, 0, kind);
1667 #if defined (_WIN32)
1669 # define WIN32_LEAN_AND_MEAN
1670 # define WIN32_EXTRA_LEAN
1671 # include <windows.h>
1673 static FcChar8 fontconfig_path[1000] = "";
1675 # if (defined (PIC) || defined (DLL_EXPORT))
1678 DllMain (HINSTANCE hinstDLL,
1684 switch (fdwReason) {
1685 case DLL_PROCESS_ATTACH:
1686 if (!GetModuleFileName ((HMODULE) hinstDLL, (LPCH) fontconfig_path,
1687 sizeof (fontconfig_path)))
1690 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1691 * assume it's a Unix-style installation tree, and use
1692 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1693 * folder where the DLL is as FONTCONFIG_PATH.
1695 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1699 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1700 if (p && (FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "bin") == 0 ||
1701 FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "lib") == 0))
1703 strcat ((char *) fontconfig_path, "\\etc\\fonts");
1706 fontconfig_path[0] = '\0';
1716 #undef FONTCONFIG_PATH
1717 #define FONTCONFIG_PATH fontconfig_path
1719 #endif /* !_WIN32 */
1721 #ifndef FONTCONFIG_FILE
1722 #define FONTCONFIG_FILE "fonts.conf"
1726 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1732 dir = (FcChar8 *) "";
1734 osize = strlen ((char *) dir) + 1 + strlen ((char *) file) + 1;
1736 * workaround valgrind warning because glibc takes advantage of how it knows memory is
1737 * allocated to implement strlen by reading in groups of 4
1739 size = (osize + 3) & ~3;
1741 path = malloc (size);
1745 strcpy ((char *) path, (const char *) dir);
1746 /* make sure there's a single separator */
1748 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1749 path[strlen((char *) path)-1] != '\\')) &&
1752 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1753 strcat ((char *) path, "\\");
1755 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1756 strcat ((char *) path, "/");
1760 strcat ((char *) path, (char *) file);
1762 if (access ((char *) path, R_OK) == 0)
1771 FcConfigGetPath (void)
1774 FcChar8 *env, *e, *colon;
1779 npath = 2; /* default dir + null */
1780 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1786 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1789 path = calloc (npath, sizeof (FcChar8 *));
1799 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1801 colon = e + strlen ((char *) e);
1802 path[i] = malloc (colon - e + 1);
1805 strncpy ((char *) path[i], (const char *) e, colon - e);
1806 path[i][colon - e] = '\0';
1816 if (fontconfig_path[0] == '\0')
1819 if(!GetModuleFileName(NULL, (LPCH) fontconfig_path, sizeof(fontconfig_path)))
1821 p = strrchr ((const char *) fontconfig_path, '\\');
1823 strcat ((char *) fontconfig_path, "\\fonts");
1826 dir = (FcChar8 *) FONTCONFIG_PATH;
1827 path[i] = malloc (strlen ((char *) dir) + 1);
1830 strcpy ((char *) path[i], (const char *) dir);
1834 for (i = 0; path[i]; i++)
1842 FcConfigFreePath (FcChar8 **path)
1846 for (p = path; *p; p++)
1851 static FcBool _FcConfigHomeEnabled = FcTrue;
1856 if (_FcConfigHomeEnabled)
1858 char *home = getenv ("HOME");
1862 home = getenv ("USERPROFILE");
1865 return (FcChar8 *) home;
1871 FcConfigXdgCacheHome (void)
1873 const char *env = getenv ("XDG_CACHE_HOME");
1874 FcChar8 *ret = NULL;
1877 ret = FcStrCopy ((const FcChar8 *)env);
1880 const FcChar8 *home = FcConfigHome ();
1881 size_t len = home ? strlen ((const char *)home) : 0;
1883 ret = malloc (len + 7 + 1);
1886 memcpy (ret, home, len);
1887 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".cache", 7);
1896 FcConfigXdgConfigHome (void)
1898 const char *env = getenv ("XDG_CONFIG_HOME");
1899 FcChar8 *ret = NULL;
1902 ret = FcStrCopy ((const FcChar8 *)env);
1905 const FcChar8 *home = FcConfigHome ();
1906 size_t len = home ? strlen ((const char *)home) : 0;
1908 ret = malloc (len + 8 + 1);
1911 memcpy (ret, home, len);
1912 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".config", 8);
1921 FcConfigXdgDataHome (void)
1923 const char *env = getenv ("XDG_DATA_HOME");
1924 FcChar8 *ret = NULL;
1927 ret = FcStrCopy ((const FcChar8 *)env);
1930 const FcChar8 *home = FcConfigHome ();
1931 size_t len = home ? strlen ((const char *)home) : 0;
1933 ret = malloc (len + 13 + 1);
1936 memcpy (ret, home, len);
1937 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".local" FC_DIR_SEPARATOR_S "share", 13);
1946 FcConfigEnableHome (FcBool enable)
1948 FcBool prev = _FcConfigHomeEnabled;
1949 _FcConfigHomeEnabled = enable;
1954 FcConfigFilename (const FcChar8 *url)
1956 FcChar8 *file, *dir, **path, **p;
1960 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1962 url = (FcChar8 *) FONTCONFIG_FILE;
1967 if (isalpha (*url) &&
1969 (url[2] == '/' || url[2] == '\\'))
1975 dir = FcConfigHome ();
1977 file = FcConfigFileExists (dir, url + 1);
1986 file = FcConfigFileExists (0, url);
1989 path = FcConfigGetPath ();
1992 for (p = path; *p; p++)
1994 file = FcConfigFileExists (*p, url);
1998 FcConfigFreePath (path);
2006 * Manage the application-specific fonts
2010 FcConfigAppFontAddFile (FcConfig *config,
2011 const FcChar8 *file)
2020 config = FcConfigGetCurrent ();
2025 subdirs = FcStrSetCreate ();
2029 set = FcConfigGetFonts (config, FcSetApplication);
2032 set = FcFontSetCreate ();
2035 FcStrSetDestroy (subdirs);
2038 FcConfigSetFonts (config, set, FcSetApplication);
2041 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
2043 FcStrSetDestroy (subdirs);
2046 if ((sublist = FcStrListCreate (subdirs)))
2048 while ((subdir = FcStrListNext (sublist)))
2050 FcConfigAppFontAddDir (config, subdir);
2052 FcStrListDone (sublist);
2054 FcStrSetDestroy (subdirs);
2059 FcConfigAppFontAddDir (FcConfig *config,
2067 config = FcConfigGetCurrent ();
2072 dirs = FcStrSetCreate ();
2076 set = FcConfigGetFonts (config, FcSetApplication);
2079 set = FcFontSetCreate ();
2082 FcStrSetDestroy (dirs);
2085 FcConfigSetFonts (config, set, FcSetApplication);
2088 FcStrSetAddFilename (dirs, dir);
2090 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
2092 FcStrSetDestroy (dirs);
2095 FcStrSetDestroy (dirs);
2100 FcConfigAppFontClear (FcConfig *config)
2104 config = FcConfigGetCurrent ();
2109 FcConfigSetFonts (config, 0, FcSetApplication);
2113 * Manage filename-based font source selectors
2117 FcConfigGlobAdd (FcConfig *config,
2118 const FcChar8 *glob,
2121 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
2123 return FcStrSetAdd (set, glob);
2127 FcConfigGlobMatch (const FcChar8 *glob,
2128 const FcChar8 *string)
2132 while ((c = *glob++))
2136 /* short circuit common case */
2139 /* short circuit another common case */
2140 if (strchr ((char *) glob, '*') == 0)
2144 l1 = strlen ((char *) string);
2145 l2 = strlen ((char *) glob);
2148 string += (l1 - l2);
2152 if (FcConfigGlobMatch (glob, string))
2158 if (*string++ == '\0')
2167 return *string == '\0';
2171 FcConfigGlobsMatch (const FcStrSet *globs,
2172 const FcChar8 *string)
2176 for (i = 0; i < globs->num; i++)
2177 if (FcConfigGlobMatch (globs->strs[i], string))
2183 FcConfigAcceptFilename (FcConfig *config,
2184 const FcChar8 *filename)
2186 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2188 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2194 * Manage font-pattern based font source selectors
2198 FcConfigPatternsAdd (FcConfig *config,
2202 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2204 return FcFontSetAdd (set, pattern);
2208 FcConfigPatternsMatch (const FcFontSet *patterns,
2209 const FcPattern *font)
2213 for (i = 0; i < patterns->nfont; i++)
2214 if (FcListPatternMatchAny (patterns->fonts[i], font))
2220 FcConfigAcceptFont (FcConfig *config,
2221 const FcPattern *font)
2223 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2225 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2230 #include "fcaliastail.h"