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.
25 /* Objects MT-safe for readonly access. */
29 #include <sys/types.h>
31 #if defined (_WIN32) && !defined (R_OK)
35 static FcConfig *_fcConfig; /* MT-safe */
42 config = fc_atomic_ptr_get (&_fcConfig);
45 config = FcInitLoadConfigAndFonts ();
47 if (!fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config)) {
48 FcConfigDestroy (config);
58 return FcConfigEnsure () ? FcTrue : FcFalse;
64 FcConfig *cfg = fc_atomic_ptr_get (&_fcConfig);
65 if (cfg && fc_atomic_ptr_cmpexch (&_fcConfig, cfg, NULL))
66 FcConfigDestroy (cfg);
76 config = malloc (sizeof (FcConfig));
80 config->configDirs = FcStrSetCreate ();
81 if (!config->configDirs)
84 config->configFiles = FcStrSetCreate ();
85 if (!config->configFiles)
88 config->fontDirs = FcStrSetCreate ();
89 if (!config->fontDirs)
92 config->acceptGlobs = FcStrSetCreate ();
93 if (!config->acceptGlobs)
96 config->rejectGlobs = FcStrSetCreate ();
97 if (!config->rejectGlobs)
100 config->acceptPatterns = FcFontSetCreate ();
101 if (!config->acceptPatterns)
104 config->rejectPatterns = FcFontSetCreate ();
105 if (!config->rejectPatterns)
108 config->cacheDirs = FcStrSetCreate ();
109 if (!config->cacheDirs)
114 config->substPattern = 0;
115 config->substFont = 0;
116 config->substScan = 0;
117 config->maxObjects = 0;
118 for (set = FcSetSystem; set <= FcSetApplication; set++)
119 config->fonts[set] = 0;
121 config->rescanTime = time(0);
122 config->rescanInterval = 30;
124 config->expr_pool = NULL;
126 config->sysRoot = NULL;
128 FcRefInit (&config->ref, 1);
133 FcFontSetDestroy (config->rejectPatterns);
135 FcFontSetDestroy (config->acceptPatterns);
137 FcStrSetDestroy (config->rejectGlobs);
139 FcStrSetDestroy (config->acceptGlobs);
141 FcStrSetDestroy (config->fontDirs);
143 FcStrSetDestroy (config->configFiles);
145 FcStrSetDestroy (config->configDirs);
153 FcConfigNewestFile (FcStrSet *files)
155 FcStrList *list = FcStrListCreate (files);
156 FcFileTime newest = { 0, FcFalse };
162 while ((file = FcStrListNext (list)))
163 if (FcStat (file, &statb) == 0)
164 if (!newest.set || statb.st_mtime - newest.time > 0)
167 newest.time = statb.st_mtime;
169 FcStrListDone (list);
175 FcConfigUptoDate (FcConfig *config)
177 FcFileTime config_time, config_dir_time, font_time;
178 time_t now = time(0);
181 config = FcConfigGetCurrent ();
185 config_time = FcConfigNewestFile (config->configFiles);
186 config_dir_time = FcConfigNewestFile (config->configDirs);
187 font_time = FcConfigNewestFile (config->fontDirs);
188 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
189 (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
190 (font_time.set && (font_time.time - config->rescanTime) > 0))
192 /* We need to check for potential clock problems here (OLPC ticket #6046) */
193 if ((config_time.set && (config_time.time - now) > 0) ||
194 (config_dir_time.set && (config_dir_time.time - now) > 0) ||
195 (font_time.set && (font_time.time - now) > 0))
198 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected.\n");
199 config->rescanTime = now;
205 config->rescanTime = now;
210 FcSubstDestroy (FcSubst *s)
218 FcRuleDestroy (s->rule);
225 FcConfigAllocExpr (FcConfig *config)
227 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
229 FcExprPage *new_page;
231 new_page = malloc (sizeof (FcExprPage));
235 new_page->next_page = config->expr_pool;
236 new_page->next = new_page->exprs;
237 config->expr_pool = new_page;
240 return config->expr_pool->next++;
244 FcConfigReference (FcConfig *config)
248 config = FcConfigGetCurrent ();
253 FcRefInc (&config->ref);
259 FcConfigDestroy (FcConfig *config)
264 if (FcRefDec (&config->ref) != 1)
267 (void) fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL);
269 FcStrSetDestroy (config->configDirs);
270 FcStrSetDestroy (config->fontDirs);
271 FcStrSetDestroy (config->cacheDirs);
272 FcStrSetDestroy (config->configFiles);
273 FcStrSetDestroy (config->acceptGlobs);
274 FcStrSetDestroy (config->rejectGlobs);
275 FcFontSetDestroy (config->acceptPatterns);
276 FcFontSetDestroy (config->rejectPatterns);
279 FcBlanksDestroy (config->blanks);
281 FcSubstDestroy (config->substPattern);
282 FcSubstDestroy (config->substFont);
283 FcSubstDestroy (config->substScan);
284 for (set = FcSetSystem; set <= FcSetApplication; set++)
285 if (config->fonts[set])
286 FcFontSetDestroy (config->fonts[set]);
288 page = config->expr_pool;
291 FcExprPage *next = page->next_page;
296 FcStrFree (config->sysRoot);
302 * Add cache to configuration, adding fonts and directories
306 FcConfigAddCache (FcConfig *config, FcCache *cache,
307 FcSetName set, FcStrSet *dirSet)
316 fs = FcCacheSet (cache);
321 for (i = 0; i < fs->nfont; i++)
323 FcPattern *font = FcFontSetFont (fs, i);
327 * Check to see if font is banned by filename
329 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
330 0, &font_file) == FcResultMatch &&
331 !FcConfigAcceptFilename (config, font_file))
337 * Check to see if font is banned by pattern
339 if (!FcConfigAcceptFont (config, font))
342 if (FcFontSetAdd (config->fonts[set], font))
345 FcDirCacheReference (cache, nref);
351 dirs = FcCacheDirs (cache);
354 for (i = 0; i < cache->dirs_count; i++)
356 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
357 if (FcConfigAcceptFilename (config, dir))
358 FcStrSetAddFilename (dirSet, dir);
365 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
371 dirlist = FcStrListCreate (dirSet);
375 while ((dir = FcStrListNext (dirlist)))
377 if (FcDebug () & FC_DBG_FONTSET)
378 printf ("adding fonts from%s\n", dir);
379 cache = FcDirCacheRead (dir, FcFalse, config);
382 FcConfigAddCache (config, cache, set, dirSet);
383 FcDirCacheUnload (cache);
385 FcStrListDone (dirlist);
390 * Scan the current list of directories in the configuration
391 * and build the set of available fonts.
395 FcConfigBuildFonts (FcConfig *config)
401 config = FcConfigGetCurrent ();
406 fonts = FcFontSetCreate ();
410 FcConfigSetFonts (config, fonts, FcSetSystem);
412 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
414 if (FcDebug () & FC_DBG_FONTSET)
415 FcFontSetPrint (fonts);
420 FcConfigSetCurrent (FcConfig *config)
425 cfg = fc_atomic_ptr_get (&_fcConfig);
430 if (config && !config->fonts[FcSetSystem])
431 if (!FcConfigBuildFonts (config))
434 if (!fc_atomic_ptr_cmpexch (&_fcConfig, cfg, config))
438 FcConfigDestroy (cfg);
444 FcConfigGetCurrent (void)
446 return FcConfigEnsure ();
450 FcConfigAddConfigDir (FcConfig *config,
453 return FcStrSetAddFilename (config->configDirs, d);
457 FcConfigGetConfigDirs (FcConfig *config)
461 config = FcConfigGetCurrent ();
465 return FcStrListCreate (config->configDirs);
469 FcConfigAddFontDir (FcConfig *config,
472 return FcStrSetAddFilename (config->fontDirs, d);
476 FcConfigAddDir (FcConfig *config,
479 return (FcConfigAddConfigDir (config, d) &&
480 FcConfigAddFontDir (config, d));
484 FcConfigGetFontDirs (FcConfig *config)
488 config = FcConfigGetCurrent ();
492 return FcStrListCreate (config->fontDirs);
496 FcConfigAddCacheDir (FcConfig *config,
499 return FcStrSetAddFilename (config->cacheDirs, d);
503 FcConfigGetCacheDirs (const FcConfig *config)
507 config = FcConfigGetCurrent ();
511 return FcStrListCreate (config->cacheDirs);
515 FcConfigAddConfigFile (FcConfig *config,
519 FcChar8 *file = FcConfigFilename (f);
524 ret = FcStrSetAdd (config->configFiles, file);
530 FcConfigGetConfigFiles (FcConfig *config)
534 config = FcConfigGetCurrent ();
538 return FcStrListCreate (config->configFiles);
542 FcConfigGetCache (FcConfig *config FC_UNUSED)
548 FcConfigGetFonts (FcConfig *config,
553 config = FcConfigGetCurrent ();
557 return config->fonts[set];
561 FcConfigSetFonts (FcConfig *config,
565 if (config->fonts[set])
566 FcFontSetDestroy (config->fonts[set]);
567 config->fonts[set] = fonts;
571 FcConfigGetBlanks (FcConfig *config)
575 config = FcConfigGetCurrent ();
579 return config->blanks;
583 FcConfigAddBlank (FcConfig *config,
586 FcBlanks *b, *freeme = 0;
591 freeme = b = FcBlanksCreate ();
595 if (!FcBlanksAdd (b, blank))
598 FcBlanksDestroy (freeme);
606 FcConfigGetRescanInterval (FcConfig *config)
610 config = FcConfigGetCurrent ();
614 return config->rescanInterval;
618 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
622 config = FcConfigGetCurrent ();
626 config->rescanInterval = rescanInterval;
631 * A couple of typos escaped into the library
634 FcConfigGetRescanInverval (FcConfig *config)
636 return FcConfigGetRescanInterval (config);
640 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
642 return FcConfigSetRescanInterval (config, rescanInterval);
646 FcConfigAddRule (FcConfig *config,
650 FcSubst *subst, **prev;
658 prev = &config->substPattern;
661 prev = &config->substFont;
664 prev = &config->substScan;
669 subst = (FcSubst *) malloc (sizeof (FcSubst));
672 for (; *prev; prev = &(*prev)->next);
676 for (r = rule; r; r = r->next)
682 r->u.test->kind == FcMatchDefault)
683 r->u.test->kind = kind;
685 if (n < r->u.test->object)
686 n = r->u.test->object;
689 if (n < r->u.edit->object)
690 n = r->u.edit->object;
696 n = FC_OBJ_ID (n) - FC_MAX_BASE_OBJECT;
697 if (config->maxObjects < n)
698 config->maxObjects = n;
699 if (FcDebug () & FC_DBG_EDIT)
701 printf ("Add Subst ");
702 FcSubstPrint (subst);
708 FcConfigPromote (FcValue v, FcValue u, FcValuePromotionBuffer *buf)
710 if (v.type == FcTypeInteger)
712 v.type = FcTypeDouble;
713 v.u.d = (double) v.u.i;
715 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
717 v.u.m = &FcIdentityMatrix;
718 v.type = FcTypeMatrix;
720 else if (buf && v.type == FcTypeString && u.type == FcTypeLangSet)
722 v.u.l = FcLangSetPromote (v.u.s, buf);
723 v.type = FcTypeLangSet;
729 FcConfigCompareValue (const FcValue *left_o,
731 const FcValue *right_o)
733 FcValue left = FcValueCanonicalize(left_o);
734 FcValue right = FcValueCanonicalize(right_o);
735 FcBool ret = FcFalse;
736 FcOp op = FC_OP_GET_OP (op_);
737 int flags = FC_OP_GET_FLAGS (op_);
738 FcValuePromotionBuffer buf1, buf2;
740 left = FcConfigPromote (left, right, &buf1);
741 right = FcConfigPromote (right, left, &buf2);
742 if (left.type == right.type)
746 break; /* No way to guess how to compare for this object */
748 break; /* FcConfigPromote prevents this from happening */
754 ret = left.u.d == right.u.d;
757 case FcOpNotContains:
758 ret = left.u.d != right.u.d;
761 ret = left.u.d < right.u.d;
764 ret = left.u.d <= right.u.d;
767 ret = left.u.d > right.u.d;
770 ret = left.u.d >= right.u.d;
781 ret = left.u.b == right.u.b;
784 case FcOpNotContains:
785 ret = left.u.b != right.u.b;
795 if (flags & FcOpFlagIgnoreBlanks)
796 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) == 0;
798 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
801 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
804 if (flags & FcOpFlagIgnoreBlanks)
805 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) != 0;
807 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
809 case FcOpNotContains:
810 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
821 ret = FcMatrixEqual (left.u.m, right.u.m);
824 case FcOpNotContains:
825 ret = !FcMatrixEqual (left.u.m, right.u.m);
835 /* left contains right if right is a subset of left */
836 ret = FcCharSetIsSubset (right.u.c, left.u.c);
838 case FcOpNotContains:
839 /* left contains right if right is a subset of left */
840 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
843 ret = FcCharSetEqual (left.u.c, right.u.c);
846 ret = !FcCharSetEqual (left.u.c, right.u.c);
856 ret = FcLangSetContains (left.u.l, right.u.l);
858 case FcOpNotContains:
859 ret = !FcLangSetContains (left.u.l, right.u.l);
862 ret = FcLangSetEqual (left.u.l, right.u.l);
865 ret = !FcLangSetEqual (left.u.l, right.u.l);
887 ret = left.u.f == right.u.f;
890 case FcOpNotContains:
891 ret = left.u.f != right.u.f;
901 if (op == FcOpNotEqual || op == FcOpNotContains)
908 #define _FcDoubleFloor(d) ((int) (d))
909 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
910 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
911 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
912 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
913 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
916 FcConfigEvaluate (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e)
921 FcOp op = FC_OP_GET_OP (e->op);
925 v.type = FcTypeInteger;
929 v.type = FcTypeDouble;
933 v.type = FcTypeString;
940 FcValue xx, xy, yx, yy;
941 v.type = FcTypeMatrix;
942 xx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xx), v, NULL);
943 xy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xy), v, NULL);
944 yx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yx), v, NULL);
945 yy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yy), v, NULL);
946 if (xx.type == FcTypeDouble && xy.type == FcTypeDouble &&
947 yx.type == FcTypeDouble && yy.type == FcTypeDouble)
961 v.type = FcTypeCharSet;
966 v.type = FcTypeLangSet;
975 if (kind == FcMatchFont && e->u.name.kind == FcMatchPattern)
977 if (FcResultMatch != FcPatternObjectGet (p_pat, e->u.name.object, 0, &v))
980 else if (kind == FcMatchPattern && e->u.name.kind == FcMatchFont)
983 "Fontconfig warning: <name> tag has target=\"font\" in a <match target=\"pattern\">.\n");
988 if (FcResultMatch != FcPatternObjectGet (p, e->u.name.object, 0, &v))
994 if (FcNameConstant (e->u.constant, &v.u.i))
995 v.type = FcTypeInteger;
1000 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1001 if (vl.type == FcTypeBool)
1004 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.left);
1006 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.right);
1009 v.type = FcTypeVoid;
1010 FcValueDestroy (vl);
1019 case FcOpNotContains:
1021 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1022 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1023 v.type = FcTypeBool;
1024 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
1025 FcValueDestroy (vl);
1026 FcValueDestroy (vr);
1034 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1035 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1036 vl = FcConfigPromote (vl, vr, NULL);
1037 vr = FcConfigPromote (vr, vl, NULL);
1038 if (vl.type == vr.type)
1040 switch ((int) vl.type) {
1044 v.type = FcTypeDouble;
1045 v.u.d = vl.u.d + vr.u.d;
1048 v.type = FcTypeDouble;
1049 v.u.d = vl.u.d - vr.u.d;
1052 v.type = FcTypeDouble;
1053 v.u.d = vl.u.d * vr.u.d;
1056 v.type = FcTypeDouble;
1057 v.u.d = vl.u.d / vr.u.d;
1060 v.type = FcTypeVoid;
1063 if (v.type == FcTypeDouble &&
1064 v.u.d == (double) (int) v.u.d)
1066 v.type = FcTypeInteger;
1067 v.u.i = (int) v.u.d;
1073 v.type = FcTypeBool;
1074 v.u.b = vl.u.b || vr.u.b;
1077 v.type = FcTypeBool;
1078 v.u.b = vl.u.b && vr.u.b;
1081 v.type = FcTypeVoid;
1088 v.type = FcTypeString;
1089 str = FcStrPlus (vl.u.s, vr.u.s);
1090 v.u.s = FcStrdup (str);
1094 v.type = FcTypeVoid;
1097 v.type = FcTypeVoid;
1104 v.type = FcTypeMatrix;
1105 m = malloc (sizeof (FcMatrix));
1108 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1113 v.type = FcTypeVoid;
1117 v.type = FcTypeVoid;
1124 v.type = FcTypeCharSet;
1125 v.u.c = FcCharSetUnion (vl.u.c, vr.u.c);
1127 v.type = FcTypeVoid;
1130 v.type = FcTypeCharSet;
1131 v.u.c = FcCharSetSubtract (vl.u.c, vr.u.c);
1133 v.type = FcTypeVoid;
1136 v.type = FcTypeVoid;
1143 v.type = FcTypeLangSet;
1144 v.u.l = FcLangSetUnion (vl.u.l, vr.u.l);
1146 v.type = FcTypeVoid;
1149 v.type = FcTypeLangSet;
1150 v.u.l = FcLangSetSubtract (vl.u.l, vr.u.l);
1152 v.type = FcTypeVoid;
1155 v.type = FcTypeVoid;
1160 v.type = FcTypeVoid;
1165 v.type = FcTypeVoid;
1166 FcValueDestroy (vl);
1167 FcValueDestroy (vr);
1170 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1171 switch ((int) vl.type) {
1173 v.type = FcTypeBool;
1177 v.type = FcTypeVoid;
1180 FcValueDestroy (vl);
1183 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1184 switch ((int) vl.type) {
1189 v.type = FcTypeInteger;
1190 v.u.i = FcDoubleFloor (vl.u.d);
1193 v.type = FcTypeVoid;
1196 FcValueDestroy (vl);
1199 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1200 switch ((int) vl.type) {
1205 v.type = FcTypeInteger;
1206 v.u.i = FcDoubleCeil (vl.u.d);
1209 v.type = FcTypeVoid;
1212 FcValueDestroy (vl);
1215 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1216 switch ((int) vl.type) {
1221 v.type = FcTypeInteger;
1222 v.u.i = FcDoubleRound (vl.u.d);
1225 v.type = FcTypeVoid;
1228 FcValueDestroy (vl);
1231 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1232 switch ((int) vl.type) {
1237 v.type = FcTypeInteger;
1238 v.u.i = FcDoubleTrunc (vl.u.d);
1241 v.type = FcTypeVoid;
1244 FcValueDestroy (vl);
1247 v.type = FcTypeVoid;
1253 static FcValueList *
1254 FcConfigMatchValueList (FcPattern *p,
1258 FcValueList *values)
1260 FcValueList *ret = 0;
1261 FcExpr *e = t->expr;
1267 /* Compute the value of the match expression */
1268 if (FC_OP_GET_OP (e->op) == FcOpComma)
1270 value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1271 e = e->u.tree.right;
1275 value = FcConfigEvaluate (p, p_pat, kind, e);
1279 for (v = values; v; v = FcValueListNext(v))
1281 /* Compare the pattern value to the match expression value */
1282 if (FcConfigCompareValue (&v->value, t->op, &value))
1289 if (t->qual == FcQualAll)
1296 FcValueDestroy (value);
1301 static FcValueList *
1302 FcConfigValues (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e, FcValueBinding binding)
1308 l = (FcValueList *) malloc (sizeof (FcValueList));
1311 if (FC_OP_GET_OP (e->op) == FcOpComma)
1313 l->value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1314 l->next = FcConfigValues (p, p_pat, kind, e->u.tree.right, binding);
1318 l->value = FcConfigEvaluate (p, p_pat, kind, e);
1321 l->binding = binding;
1322 if (l->value.type == FcTypeVoid)
1324 FcValueList *next = FcValueListNext(l);
1334 FcConfigAdd (FcValueListPtr *head,
1335 FcValueList *position,
1340 FcValueListPtr *prev, l, last, v;
1341 FcValueBinding sameBinding;
1344 * Make sure the stored type is valid for built-in objects
1346 for (l = new; l != NULL; l = FcValueListNext (l))
1348 if (!FcObjectValidType (object, l->value.type))
1351 "Fontconfig warning: FcPattern object %s does not accept value", FcObjectName (object));
1352 FcValuePrintFile (stderr, l->value);
1353 fprintf (stderr, "\n");
1355 if (FcDebug () & FC_DBG_EDIT)
1357 printf ("Not adding\n");
1365 sameBinding = position->binding;
1367 sameBinding = FcValueBindingWeak;
1368 for (v = new; v != NULL; v = FcValueListNext(v))
1369 if (v->binding == FcValueBindingSame)
1370 v->binding = sameBinding;
1374 prev = &position->next;
1376 for (prev = head; *prev != NULL;
1377 prev = &(*prev)->next)
1384 for (prev = head; *prev != NULL;
1385 prev = &(*prev)->next)
1387 if (*prev == position)
1394 if (FcDebug () & FC_DBG_EDIT)
1397 printf ("position not on list\n");
1401 if (FcDebug () & FC_DBG_EDIT)
1403 printf ("%s list before ", append ? "Append" : "Prepend");
1404 FcValueListPrintWithPosition (*head, *prev);
1411 while (last->next != NULL)
1418 if (FcDebug () & FC_DBG_EDIT)
1420 printf ("%s list after ", append ? "Append" : "Prepend");
1421 FcValueListPrint (*head);
1429 FcConfigDel (FcValueListPtr *head,
1430 FcValueList *position)
1432 FcValueListPtr *prev;
1434 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1436 if (*prev == position)
1438 *prev = position->next;
1439 position->next = NULL;
1440 FcValueListDestroy (position);
1447 FcConfigPatternAdd (FcPattern *p,
1454 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1458 FcConfigAdd (&e->values, 0, append, list, object);
1463 * Delete all values associated with a field
1466 FcConfigPatternDel (FcPattern *p,
1469 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1472 while (e->values != NULL)
1473 FcConfigDel (&e->values, e->values);
1477 FcConfigPatternCanon (FcPattern *p,
1480 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1483 if (e->values == NULL)
1484 FcPatternObjectDel (p, object);
1488 FcConfigSubstituteWithPat (FcConfig *config,
1496 FcValueList *l, **value = NULL, *vl;
1499 FcObject object = FC_INVALID_OBJECT;
1500 FcPatternElt **elt = NULL, *e;
1502 FcBool retval = FcTrue;
1503 FcTest **tst = NULL;
1507 config = FcConfigGetCurrent ();
1513 case FcMatchPattern:
1514 s = config->substPattern;
1515 strs = FcGetDefaultLangs ();
1518 FcStrList *l = FcStrListCreate (strs);
1522 FcStrSetDestroy (strs);
1523 while (l && (lang = FcStrListNext (l)))
1525 v.type = FcTypeString;
1527 FcPatternObjectAddWithBinding (p, FC_LANG_OBJECT, v, FcValueBindingWeak, FcTrue);
1531 if (FcPatternObjectGet (p, FC_PRGNAME_OBJECT, 0, &v) == FcResultNoMatch)
1533 FcChar8 *prgname = FcGetPrgname ();
1535 FcPatternObjectAddString (p, FC_PRGNAME_OBJECT, prgname);
1539 s = config->substFont;
1542 s = config->substScan;
1548 nobjs = FC_MAX_BASE_OBJECT + config->maxObjects + 2;
1549 value = (FcValueList **) malloc (SIZEOF_VOID_P * nobjs);
1555 elt = (FcPatternElt **) malloc (SIZEOF_VOID_P * nobjs);
1561 tst = (FcTest **) malloc (SIZEOF_VOID_P * nobjs);
1568 if (FcDebug () & FC_DBG_EDIT)
1570 printf ("FcConfigSubstitute ");
1573 for (; s; s = s->next)
1576 for (i = 0; i < nobjs; i++)
1582 for (; r; r = r->next)
1586 /* shouldn't be reached */
1589 object = FC_OBJ_ID (r->u.test->object);
1591 * Check the tests to see if
1592 * they all match the pattern
1594 if (FcDebug () & FC_DBG_EDIT)
1596 printf ("FcConfigSubstitute test ");
1597 FcTestPrint (r->u.test);
1599 if (kind == FcMatchFont && r->u.test->kind == FcMatchPattern)
1604 e = FcPatternObjectFindElt (m, r->u.test->object);
1607 /* different 'kind' won't be the target of edit */
1608 if (!elt[object] && kind == r->u.test->kind)
1611 tst[object] = r->u.test;
1614 * If there's no such field in the font,
1615 * then FcQualAll matches while FcQualAny does not
1619 if (r->u.test->qual == FcQualAll)
1621 value[object] = NULL;
1626 if (FcDebug () & FC_DBG_EDIT)
1627 printf ("No match\n");
1632 * Check to see if there is a match, mark the location
1633 * to apply match-relative edits
1635 vl = FcConfigMatchValueList (m, p_pat, kind, r->u.test, e->values);
1636 /* different 'kind' won't be the target of edit */
1637 if (!value[object] && kind == r->u.test->kind)
1640 (r->u.test->qual == FcQualFirst && vl != e->values) ||
1641 (r->u.test->qual == FcQualNotFirst && vl == e->values))
1643 if (FcDebug () & FC_DBG_EDIT)
1644 printf ("No match\n");
1649 object = FC_OBJ_ID (r->u.edit->object);
1650 if (FcDebug () & FC_DBG_EDIT)
1652 printf ("Substitute ");
1653 FcEditPrint (r->u.edit);
1657 * Evaluate the list of expressions
1659 l = FcConfigValues (p, p_pat,kind, r->u.edit->expr, r->u.edit->binding);
1660 if (tst[object] && (tst[object]->kind == FcMatchFont || kind == FcMatchPattern))
1661 elt[object] = FcPatternObjectFindElt (p, tst[object]->object);
1663 switch (FC_OP_GET_OP (r->u.edit->op)) {
1666 * If there was a test, then replace the matched
1667 * value with the new list of values
1671 FcValueList *thisValue = value[object];
1672 FcValueList *nextValue = l;
1675 * Append the new list of values after the current value
1677 FcConfigAdd (&elt[object]->values, thisValue, FcTrue, l, r->u.edit->object);
1679 * Delete the marked value
1682 FcConfigDel (&elt[object]->values, thisValue);
1684 * Adjust a pointer into the value list to ensure
1685 * future edits occur at the same place
1687 value[object] = nextValue;
1690 /* fall through ... */
1691 case FcOpAssignReplace:
1693 * Delete all of the values and insert
1696 FcConfigPatternDel (p, r->u.edit->object);
1697 FcConfigPatternAdd (p, r->u.edit->object, l, FcTrue);
1699 * Adjust a pointer into the value list as they no
1700 * longer point to anything valid
1702 value[object] = NULL;
1707 FcConfigAdd (&elt[object]->values, value[object], FcFalse, l, r->u.edit->object);
1710 /* fall through ... */
1711 case FcOpPrependFirst:
1712 FcConfigPatternAdd (p, r->u.edit->object, l, FcFalse);
1717 FcConfigAdd (&elt[object]->values, value[object], FcTrue, l, r->u.edit->object);
1720 /* fall through ... */
1721 case FcOpAppendLast:
1722 FcConfigPatternAdd (p, r->u.edit->object, l, FcTrue);
1727 FcConfigDel (&elt[object]->values, value[object]);
1730 /* fall through ... */
1732 FcConfigPatternDel (p, r->u.edit->object);
1735 FcValueListDestroy (l);
1739 * Now go through the pattern and eliminate
1740 * any properties without data
1742 FcConfigPatternCanon (p, r->u.edit->object);
1744 if (FcDebug () & FC_DBG_EDIT)
1746 printf ("FcConfigSubstitute edit");
1754 if (FcDebug () & FC_DBG_EDIT)
1756 printf ("FcConfigSubstitute done");
1771 FcConfigSubstitute (FcConfig *config,
1775 return FcConfigSubstituteWithPat (config, p, 0, kind);
1778 #if defined (_WIN32)
1780 static FcChar8 fontconfig_path[1000] = ""; /* MT-dontcare */
1782 # if (defined (PIC) || defined (DLL_EXPORT))
1785 DllMain (HINSTANCE hinstDLL,
1787 LPVOID lpvReserved);
1790 DllMain (HINSTANCE hinstDLL,
1796 switch (fdwReason) {
1797 case DLL_PROCESS_ATTACH:
1798 if (!GetModuleFileName ((HMODULE) hinstDLL, (LPCH) fontconfig_path,
1799 sizeof (fontconfig_path)))
1802 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1803 * assume it's a Unix-style installation tree, and use
1804 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1805 * folder where the DLL is as FONTCONFIG_PATH.
1807 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1811 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1812 if (p && (FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "bin") == 0 ||
1813 FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "lib") == 0))
1815 strcat ((char *) fontconfig_path, "\\etc\\fonts");
1818 fontconfig_path[0] = '\0';
1828 #undef FONTCONFIG_PATH
1829 #define FONTCONFIG_PATH fontconfig_path
1831 #endif /* !_WIN32 */
1833 #ifndef FONTCONFIG_FILE
1834 #define FONTCONFIG_FILE "fonts.conf"
1838 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1844 dir = (FcChar8 *) "";
1846 osize = strlen ((char *) dir) + 1 + strlen ((char *) file) + 1;
1848 * workaround valgrind warning because glibc takes advantage of how it knows memory is
1849 * allocated to implement strlen by reading in groups of 4
1851 size = (osize + 3) & ~3;
1853 path = malloc (size);
1857 strcpy ((char *) path, (const char *) dir);
1858 /* make sure there's a single separator */
1860 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1861 path[strlen((char *) path)-1] != '\\')) &&
1864 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1865 strcat ((char *) path, "\\");
1867 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1868 strcat ((char *) path, "/");
1872 strcat ((char *) path, (char *) file);
1874 if (access ((char *) path, R_OK) == 0)
1883 FcConfigGetPath (void)
1886 FcChar8 *env, *e, *colon;
1891 npath = 2; /* default dir + null */
1892 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1898 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1901 path = calloc (npath, sizeof (FcChar8 *));
1911 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1913 colon = e + strlen ((char *) e);
1914 path[i] = malloc (colon - e + 1);
1917 strncpy ((char *) path[i], (const char *) e, colon - e);
1918 path[i][colon - e] = '\0';
1928 if (fontconfig_path[0] == '\0')
1931 if(!GetModuleFileName(NULL, (LPCH) fontconfig_path, sizeof(fontconfig_path)))
1933 p = strrchr ((const char *) fontconfig_path, '\\');
1935 strcat ((char *) fontconfig_path, "\\fonts");
1938 dir = (FcChar8 *) FONTCONFIG_PATH;
1939 path[i] = malloc (strlen ((char *) dir) + 1);
1942 strcpy ((char *) path[i], (const char *) dir);
1946 for (i = 0; path[i]; i++)
1954 FcConfigFreePath (FcChar8 **path)
1958 for (p = path; *p; p++)
1963 static FcBool _FcConfigHomeEnabled = FcTrue; /* MT-goodenough */
1968 if (_FcConfigHomeEnabled)
1970 char *home = getenv ("HOME");
1974 home = getenv ("USERPROFILE");
1977 return (FcChar8 *) home;
1983 FcConfigXdgCacheHome (void)
1985 const char *env = getenv ("XDG_CACHE_HOME");
1986 FcChar8 *ret = NULL;
1989 ret = FcStrCopy ((const FcChar8 *)env);
1992 const FcChar8 *home = FcConfigHome ();
1993 size_t len = home ? strlen ((const char *)home) : 0;
1995 ret = malloc (len + 7 + 1);
1998 memcpy (ret, home, len);
1999 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".cache", 7);
2008 FcConfigXdgConfigHome (void)
2010 const char *env = getenv ("XDG_CONFIG_HOME");
2011 FcChar8 *ret = NULL;
2014 ret = FcStrCopy ((const FcChar8 *)env);
2017 const FcChar8 *home = FcConfigHome ();
2018 size_t len = home ? strlen ((const char *)home) : 0;
2020 ret = malloc (len + 8 + 1);
2023 memcpy (ret, home, len);
2024 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".config", 8);
2033 FcConfigXdgDataHome (void)
2035 const char *env = getenv ("XDG_DATA_HOME");
2036 FcChar8 *ret = NULL;
2039 ret = FcStrCopy ((const FcChar8 *)env);
2042 const FcChar8 *home = FcConfigHome ();
2043 size_t len = home ? strlen ((const char *)home) : 0;
2045 ret = malloc (len + 13 + 1);
2048 memcpy (ret, home, len);
2049 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".local" FC_DIR_SEPARATOR_S "share", 13);
2058 FcConfigEnableHome (FcBool enable)
2060 FcBool prev = _FcConfigHomeEnabled;
2061 _FcConfigHomeEnabled = enable;
2066 FcConfigFilename (const FcChar8 *url)
2068 FcChar8 *file, *dir, **path, **p;
2072 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
2074 url = (FcChar8 *) FONTCONFIG_FILE;
2079 if (isalpha (*url) &&
2081 (url[2] == '/' || url[2] == '\\'))
2087 dir = FcConfigHome ();
2089 file = FcConfigFileExists (dir, url + 1);
2098 file = FcConfigFileExists (0, url);
2101 path = FcConfigGetPath ();
2104 for (p = path; *p; p++)
2106 file = FcConfigFileExists (*p, url);
2110 FcConfigFreePath (path);
2118 * Manage the application-specific fonts
2122 FcConfigAppFontAddFile (FcConfig *config,
2123 const FcChar8 *file)
2132 config = FcConfigGetCurrent ();
2137 subdirs = FcStrSetCreate ();
2141 set = FcConfigGetFonts (config, FcSetApplication);
2144 set = FcFontSetCreate ();
2147 FcStrSetDestroy (subdirs);
2150 FcConfigSetFonts (config, set, FcSetApplication);
2153 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
2155 FcStrSetDestroy (subdirs);
2158 if ((sublist = FcStrListCreate (subdirs)))
2160 while ((subdir = FcStrListNext (sublist)))
2162 FcConfigAppFontAddDir (config, subdir);
2164 FcStrListDone (sublist);
2166 FcStrSetDestroy (subdirs);
2171 FcConfigAppFontAddDir (FcConfig *config,
2179 config = FcConfigGetCurrent ();
2184 dirs = FcStrSetCreate ();
2188 set = FcConfigGetFonts (config, FcSetApplication);
2191 set = FcFontSetCreate ();
2194 FcStrSetDestroy (dirs);
2197 FcConfigSetFonts (config, set, FcSetApplication);
2200 FcStrSetAddFilename (dirs, dir);
2202 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
2204 FcStrSetDestroy (dirs);
2207 FcStrSetDestroy (dirs);
2212 FcConfigAppFontClear (FcConfig *config)
2216 config = FcConfigGetCurrent ();
2221 FcConfigSetFonts (config, 0, FcSetApplication);
2225 * Manage filename-based font source selectors
2229 FcConfigGlobAdd (FcConfig *config,
2230 const FcChar8 *glob,
2233 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
2235 return FcStrSetAdd (set, glob);
2239 FcConfigGlobsMatch (const FcStrSet *globs,
2240 const FcChar8 *string)
2244 for (i = 0; i < globs->num; i++)
2245 if (FcStrGlobMatch (globs->strs[i], string))
2251 FcConfigAcceptFilename (FcConfig *config,
2252 const FcChar8 *filename)
2254 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2256 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2262 * Manage font-pattern based font source selectors
2266 FcConfigPatternsAdd (FcConfig *config,
2270 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2272 return FcFontSetAdd (set, pattern);
2276 FcConfigPatternsMatch (const FcFontSet *patterns,
2277 const FcPattern *font)
2281 for (i = 0; i < patterns->nfont; i++)
2282 if (FcListPatternMatchAny (patterns->fonts[i], font))
2288 FcConfigAcceptFont (FcConfig *config,
2289 const FcPattern *font)
2291 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2293 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2299 FcConfigGetSysRoot (const FcConfig *config)
2303 config = FcConfigGetCurrent ();
2308 return config->sysRoot;
2312 FcConfigSetSysRoot (FcConfig *config,
2313 const FcChar8 *sysroot)
2316 FcBool init = FcFalse;
2320 /* We can't use FcConfigGetCurrent() here to ensure
2321 * the sysroot is set prior to initialize FcConfig,
2322 * to avoid loading caches from non-sysroot dirs.
2323 * So postpone the initialization later.
2325 config = fc_atomic_ptr_get (&_fcConfig);
2328 config = FcConfigCreate ();
2335 s = FcStrCopyFilename (sysroot);
2339 if (config->sysRoot)
2340 FcStrFree (config->sysRoot);
2342 config->sysRoot = s;
2345 config = FcInitLoadOwnConfigAndFonts (config);
2346 FcConfigSetCurrent (config);
2351 #include "fcaliastail.h"