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 FcTestDestroy (s->test);
220 FcEditDestroy (s->edit);
227 FcConfigAllocExpr (FcConfig *config)
229 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
231 FcExprPage *new_page;
233 new_page = malloc (sizeof (FcExprPage));
237 new_page->next_page = config->expr_pool;
238 new_page->next = new_page->exprs;
239 config->expr_pool = new_page;
242 return config->expr_pool->next++;
246 FcConfigReference (FcConfig *config)
250 config = FcConfigGetCurrent ();
255 FcRefInc (&config->ref);
261 FcConfigDestroy (FcConfig *config)
266 if (FcRefDec (&config->ref) != 1)
269 (void) fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL);
271 FcStrSetDestroy (config->configDirs);
272 FcStrSetDestroy (config->fontDirs);
273 FcStrSetDestroy (config->cacheDirs);
274 FcStrSetDestroy (config->configFiles);
275 FcStrSetDestroy (config->acceptGlobs);
276 FcStrSetDestroy (config->rejectGlobs);
277 FcFontSetDestroy (config->acceptPatterns);
278 FcFontSetDestroy (config->rejectPatterns);
281 FcBlanksDestroy (config->blanks);
283 FcSubstDestroy (config->substPattern);
284 FcSubstDestroy (config->substFont);
285 FcSubstDestroy (config->substScan);
286 for (set = FcSetSystem; set <= FcSetApplication; set++)
287 if (config->fonts[set])
288 FcFontSetDestroy (config->fonts[set]);
290 page = config->expr_pool;
293 FcExprPage *next = page->next_page;
298 FcStrFree (config->sysRoot);
304 * Add cache to configuration, adding fonts and directories
308 FcConfigAddCache (FcConfig *config, FcCache *cache,
309 FcSetName set, FcStrSet *dirSet)
318 fs = FcCacheSet (cache);
323 for (i = 0; i < fs->nfont; i++)
325 FcPattern *font = FcFontSetFont (fs, i);
329 * Check to see if font is banned by filename
331 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
332 0, &font_file) == FcResultMatch &&
333 !FcConfigAcceptFilename (config, font_file))
339 * Check to see if font is banned by pattern
341 if (!FcConfigAcceptFont (config, font))
344 if (FcFontSetAdd (config->fonts[set], font))
347 FcDirCacheReference (cache, nref);
353 dirs = FcCacheDirs (cache);
356 for (i = 0; i < cache->dirs_count; i++)
358 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
359 if (FcConfigAcceptFilename (config, dir))
360 FcStrSetAddFilename (dirSet, dir);
367 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
373 dirlist = FcStrListCreate (dirSet);
377 while ((dir = FcStrListNext (dirlist)))
379 if (FcDebug () & FC_DBG_FONTSET)
380 printf ("adding fonts from%s\n", dir);
381 cache = FcDirCacheRead (dir, FcFalse, config);
384 FcConfigAddCache (config, cache, set, dirSet);
385 FcDirCacheUnload (cache);
387 FcStrListDone (dirlist);
392 * Scan the current list of directories in the configuration
393 * and build the set of available fonts.
397 FcConfigBuildFonts (FcConfig *config)
403 config = FcConfigGetCurrent ();
408 fonts = FcFontSetCreate ();
412 FcConfigSetFonts (config, fonts, FcSetSystem);
414 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
416 if (FcDebug () & FC_DBG_FONTSET)
417 FcFontSetPrint (fonts);
422 FcConfigSetCurrent (FcConfig *config)
427 cfg = fc_atomic_ptr_get (&_fcConfig);
432 if (config && !config->fonts[FcSetSystem])
433 if (!FcConfigBuildFonts (config))
436 if (!fc_atomic_ptr_cmpexch (&_fcConfig, cfg, config))
440 FcConfigDestroy (cfg);
446 FcConfigGetCurrent (void)
448 return FcConfigEnsure ();
452 FcConfigAddConfigDir (FcConfig *config,
455 return FcStrSetAddFilename (config->configDirs, d);
459 FcConfigGetConfigDirs (FcConfig *config)
463 config = FcConfigGetCurrent ();
467 return FcStrListCreate (config->configDirs);
471 FcConfigAddFontDir (FcConfig *config,
474 return FcStrSetAddFilename (config->fontDirs, d);
478 FcConfigAddDir (FcConfig *config,
481 return (FcConfigAddConfigDir (config, d) &&
482 FcConfigAddFontDir (config, d));
486 FcConfigGetFontDirs (FcConfig *config)
490 config = FcConfigGetCurrent ();
494 return FcStrListCreate (config->fontDirs);
498 FcConfigAddCacheDir (FcConfig *config,
501 return FcStrSetAddFilename (config->cacheDirs, d);
505 FcConfigGetCacheDirs (const FcConfig *config)
509 config = FcConfigGetCurrent ();
513 return FcStrListCreate (config->cacheDirs);
517 FcConfigAddConfigFile (FcConfig *config,
521 FcChar8 *file = FcConfigFilename (f);
526 ret = FcStrSetAdd (config->configFiles, file);
532 FcConfigGetConfigFiles (FcConfig *config)
536 config = FcConfigGetCurrent ();
540 return FcStrListCreate (config->configFiles);
544 FcConfigGetCache (FcConfig *config FC_UNUSED)
550 FcConfigGetFonts (FcConfig *config,
555 config = FcConfigGetCurrent ();
559 return config->fonts[set];
563 FcConfigSetFonts (FcConfig *config,
567 if (config->fonts[set])
568 FcFontSetDestroy (config->fonts[set]);
569 config->fonts[set] = fonts;
573 FcConfigGetBlanks (FcConfig *config)
577 config = FcConfigGetCurrent ();
581 return config->blanks;
585 FcConfigAddBlank (FcConfig *config,
588 FcBlanks *b, *freeme = 0;
593 freeme = b = FcBlanksCreate ();
597 if (!FcBlanksAdd (b, blank))
600 FcBlanksDestroy (freeme);
608 FcConfigGetRescanInterval (FcConfig *config)
612 config = FcConfigGetCurrent ();
616 return config->rescanInterval;
620 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
624 config = FcConfigGetCurrent ();
628 config->rescanInterval = rescanInterval;
633 * A couple of typos escaped into the library
636 FcConfigGetRescanInverval (FcConfig *config)
638 return FcConfigGetRescanInterval (config);
642 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
644 return FcConfigSetRescanInterval (config, rescanInterval);
649 FcConfigAddEdit (FcConfig *config,
654 FcSubst *subst, **prev;
660 prev = &config->substPattern;
663 prev = &config->substFont;
666 prev = &config->substScan;
671 subst = (FcSubst *) malloc (sizeof (FcSubst));
674 for (; *prev; prev = &(*prev)->next);
680 for (t = test; t; t = t->next)
682 if (t->kind == FcMatchDefault)
686 if (config->maxObjects < num)
687 config->maxObjects = num;
688 if (FcDebug () & FC_DBG_EDIT)
690 printf ("Add Subst ");
691 FcSubstPrint (subst);
696 typedef struct _FcSubState {
702 FcConfigPromote (FcValue v, FcValue u, FcValuePromotionBuffer *buf)
704 if (v.type == FcTypeInteger)
706 v.type = FcTypeDouble;
707 v.u.d = (double) v.u.i;
709 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
711 v.u.m = &FcIdentityMatrix;
712 v.type = FcTypeMatrix;
714 else if (buf && v.type == FcTypeString && u.type == FcTypeLangSet)
716 v.u.l = FcLangSetPromote (v.u.s, buf);
717 v.type = FcTypeLangSet;
723 FcConfigCompareValue (const FcValue *left_o,
725 const FcValue *right_o)
727 FcValue left = FcValueCanonicalize(left_o);
728 FcValue right = FcValueCanonicalize(right_o);
729 FcBool ret = FcFalse;
730 FcOp op = FC_OP_GET_OP (op_);
731 int flags = FC_OP_GET_FLAGS (op_);
732 FcValuePromotionBuffer buf1, buf2;
734 left = FcConfigPromote (left, right, &buf1);
735 right = FcConfigPromote (right, left, &buf2);
736 if (left.type == right.type)
740 break; /* FcConfigPromote prevents this from happening */
746 ret = left.u.d == right.u.d;
749 case FcOpNotContains:
750 ret = left.u.d != right.u.d;
753 ret = left.u.d < right.u.d;
756 ret = left.u.d <= right.u.d;
759 ret = left.u.d > right.u.d;
762 ret = left.u.d >= right.u.d;
773 ret = left.u.b == right.u.b;
776 case FcOpNotContains:
777 ret = left.u.b != right.u.b;
787 if (flags & FcOpFlagIgnoreBlanks)
788 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) == 0;
790 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
793 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
796 if (flags & FcOpFlagIgnoreBlanks)
797 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) != 0;
799 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
801 case FcOpNotContains:
802 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
813 ret = FcMatrixEqual (left.u.m, right.u.m);
816 case FcOpNotContains:
817 ret = !FcMatrixEqual (left.u.m, right.u.m);
827 /* left contains right if right is a subset of left */
828 ret = FcCharSetIsSubset (right.u.c, left.u.c);
830 case FcOpNotContains:
831 /* left contains right if right is a subset of left */
832 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
835 ret = FcCharSetEqual (left.u.c, right.u.c);
838 ret = !FcCharSetEqual (left.u.c, right.u.c);
848 ret = FcLangSetContains (left.u.l, right.u.l);
850 case FcOpNotContains:
851 ret = !FcLangSetContains (left.u.l, right.u.l);
854 ret = FcLangSetEqual (left.u.l, right.u.l);
857 ret = !FcLangSetEqual (left.u.l, right.u.l);
879 ret = left.u.f == right.u.f;
882 case FcOpNotContains:
883 ret = left.u.f != right.u.f;
893 if (op == FcOpNotEqual || op == FcOpNotContains)
900 #define _FcDoubleFloor(d) ((int) (d))
901 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
902 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
903 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
904 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
905 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
908 FcConfigEvaluate (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e)
913 FcOp op = FC_OP_GET_OP (e->op);
917 v.type = FcTypeInteger;
921 v.type = FcTypeDouble;
925 v.type = FcTypeString;
932 FcValue xx, xy, yx, yy;
933 v.type = FcTypeMatrix;
934 xx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xx), v, NULL);
935 xy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xy), v, NULL);
936 yx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yx), v, NULL);
937 yy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yy), v, NULL);
938 if (xx.type == FcTypeDouble && xy.type == FcTypeDouble &&
939 yx.type == FcTypeDouble && yy.type == FcTypeDouble)
953 v.type = FcTypeCharSet;
958 v.type = FcTypeLangSet;
967 if (kind == FcMatchFont && e->u.name.kind == FcMatchPattern)
969 if (FcResultMatch != FcPatternObjectGet (p_pat, e->u.name.object, 0, &v))
972 else if (kind == FcMatchPattern && e->u.name.kind == FcMatchFont)
975 "Fontconfig warning: <name> tag has target=\"font\" in a <match target=\"pattern\">.\n");
980 if (FcResultMatch != FcPatternObjectGet (p, e->u.name.object, 0, &v))
986 if (FcNameConstant (e->u.constant, &v.u.i))
987 v.type = FcTypeInteger;
992 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
993 if (vl.type == FcTypeBool)
996 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.left);
998 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.right);
1001 v.type = FcTypeVoid;
1002 FcValueDestroy (vl);
1011 case FcOpNotContains:
1013 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1014 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1015 v.type = FcTypeBool;
1016 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
1017 FcValueDestroy (vl);
1018 FcValueDestroy (vr);
1026 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1027 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1028 vl = FcConfigPromote (vl, vr, NULL);
1029 vr = FcConfigPromote (vr, vl, NULL);
1030 if (vl.type == vr.type)
1032 switch ((int) vl.type) {
1036 v.type = FcTypeDouble;
1037 v.u.d = vl.u.d + vr.u.d;
1040 v.type = FcTypeDouble;
1041 v.u.d = vl.u.d - vr.u.d;
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 = FcTypeVoid;
1055 if (v.type == FcTypeDouble &&
1056 v.u.d == (double) (int) v.u.d)
1058 v.type = FcTypeInteger;
1059 v.u.i = (int) v.u.d;
1065 v.type = FcTypeBool;
1066 v.u.b = vl.u.b || vr.u.b;
1069 v.type = FcTypeBool;
1070 v.u.b = vl.u.b && vr.u.b;
1073 v.type = FcTypeVoid;
1080 v.type = FcTypeString;
1081 str = FcStrPlus (vl.u.s, vr.u.s);
1082 v.u.s = FcStrdup (str);
1086 v.type = FcTypeVoid;
1089 v.type = FcTypeVoid;
1096 v.type = FcTypeMatrix;
1097 m = malloc (sizeof (FcMatrix));
1100 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1105 v.type = FcTypeVoid;
1109 v.type = FcTypeVoid;
1116 v.type = FcTypeCharSet;
1117 v.u.c = FcCharSetUnion (vl.u.c, vr.u.c);
1119 v.type = FcTypeVoid;
1122 v.type = FcTypeCharSet;
1123 v.u.c = FcCharSetSubtract (vl.u.c, vr.u.c);
1125 v.type = FcTypeVoid;
1128 v.type = FcTypeVoid;
1135 v.type = FcTypeLangSet;
1136 v.u.l = FcLangSetUnion (vl.u.l, vr.u.l);
1138 v.type = FcTypeVoid;
1141 v.type = FcTypeLangSet;
1142 v.u.l = FcLangSetSubtract (vl.u.l, vr.u.l);
1144 v.type = FcTypeVoid;
1147 v.type = FcTypeVoid;
1152 v.type = FcTypeVoid;
1157 v.type = FcTypeVoid;
1158 FcValueDestroy (vl);
1159 FcValueDestroy (vr);
1162 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1163 switch ((int) vl.type) {
1165 v.type = FcTypeBool;
1169 v.type = FcTypeVoid;
1172 FcValueDestroy (vl);
1175 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1176 switch ((int) vl.type) {
1181 v.type = FcTypeInteger;
1182 v.u.i = FcDoubleFloor (vl.u.d);
1185 v.type = FcTypeVoid;
1188 FcValueDestroy (vl);
1191 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1192 switch ((int) vl.type) {
1197 v.type = FcTypeInteger;
1198 v.u.i = FcDoubleCeil (vl.u.d);
1201 v.type = FcTypeVoid;
1204 FcValueDestroy (vl);
1207 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1208 switch ((int) vl.type) {
1213 v.type = FcTypeInteger;
1214 v.u.i = FcDoubleRound (vl.u.d);
1217 v.type = FcTypeVoid;
1220 FcValueDestroy (vl);
1223 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1224 switch ((int) vl.type) {
1229 v.type = FcTypeInteger;
1230 v.u.i = FcDoubleTrunc (vl.u.d);
1233 v.type = FcTypeVoid;
1236 FcValueDestroy (vl);
1239 v.type = FcTypeVoid;
1245 static FcValueList *
1246 FcConfigMatchValueList (FcPattern *p,
1250 FcValueList *values)
1252 FcValueList *ret = 0;
1253 FcExpr *e = t->expr;
1259 /* Compute the value of the match expression */
1260 if (FC_OP_GET_OP (e->op) == FcOpComma)
1262 value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1263 e = e->u.tree.right;
1267 value = FcConfigEvaluate (p, p_pat, kind, e);
1271 for (v = values; v; v = FcValueListNext(v))
1273 /* Compare the pattern value to the match expression value */
1274 if (FcConfigCompareValue (&v->value, t->op, &value))
1281 if (t->qual == FcQualAll)
1288 FcValueDestroy (value);
1293 static FcValueList *
1294 FcConfigValues (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e, FcValueBinding binding)
1300 l = (FcValueList *) malloc (sizeof (FcValueList));
1303 if (FC_OP_GET_OP (e->op) == FcOpComma)
1305 l->value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1306 l->next = FcConfigValues (p, p_pat, kind, e->u.tree.right, binding);
1310 l->value = FcConfigEvaluate (p, p_pat, kind, e);
1313 l->binding = binding;
1314 if (l->value.type == FcTypeVoid)
1316 FcValueList *next = FcValueListNext(l);
1326 FcConfigAdd (FcValueListPtr *head,
1327 FcValueList *position,
1332 FcValueListPtr *prev, l, last, v;
1333 FcValueBinding sameBinding;
1336 * Make sure the stored type is valid for built-in objects
1338 for (l = new; l != NULL; l = FcValueListNext (l))
1340 if (!FcObjectValidType (object, l->value.type))
1343 "Fontconfig warning: FcPattern object %s does not accept value", FcObjectName (object));
1344 FcValuePrintFile (stderr, l->value);
1345 fprintf (stderr, "\n");
1347 if (FcDebug () & FC_DBG_EDIT)
1349 printf ("Not adding\n");
1357 sameBinding = position->binding;
1359 sameBinding = FcValueBindingWeak;
1360 for (v = new; v != NULL; v = FcValueListNext(v))
1361 if (v->binding == FcValueBindingSame)
1362 v->binding = sameBinding;
1366 prev = &position->next;
1368 for (prev = head; *prev != NULL;
1369 prev = &(*prev)->next)
1376 for (prev = head; *prev != NULL;
1377 prev = &(*prev)->next)
1379 if (*prev == position)
1386 if (FcDebug () & FC_DBG_EDIT)
1389 printf ("position not on list\n");
1393 if (FcDebug () & FC_DBG_EDIT)
1395 printf ("%s list before ", append ? "Append" : "Prepend");
1396 FcValueListPrintWithPosition (*head, *prev);
1403 while (last->next != NULL)
1410 if (FcDebug () & FC_DBG_EDIT)
1412 printf ("%s list after ", append ? "Append" : "Prepend");
1413 FcValueListPrint (*head);
1421 FcConfigDel (FcValueListPtr *head,
1422 FcValueList *position)
1424 FcValueListPtr *prev;
1426 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1428 if (*prev == position)
1430 *prev = position->next;
1431 position->next = NULL;
1432 FcValueListDestroy (position);
1439 FcConfigPatternAdd (FcPattern *p,
1446 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1450 FcConfigAdd (&e->values, 0, append, list, object);
1455 * Delete all values associated with a field
1458 FcConfigPatternDel (FcPattern *p,
1461 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1464 while (e->values != NULL)
1465 FcConfigDel (&e->values, e->values);
1469 FcConfigPatternCanon (FcPattern *p,
1472 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1475 if (e->values == NULL)
1476 FcPatternObjectDel (p, object);
1480 FcConfigSubstituteWithPat (FcConfig *config,
1497 config = FcConfigGetCurrent ();
1503 case FcMatchPattern:
1504 s = config->substPattern;
1505 strs = FcGetDefaultLangs ();
1508 FcStrList *l = FcStrListCreate (strs);
1512 FcStrSetDestroy (strs);
1513 while (l && (lang = FcStrListNext (l)))
1515 v.type = FcTypeString;
1517 FcPatternObjectAddWithBinding (p, FC_LANG_OBJECT, v, FcValueBindingWeak, FcTrue);
1521 if (FcPatternObjectGet (p, FC_PRGNAME_OBJECT, 0, &v) == FcResultNoMatch)
1523 FcChar8 *prgname = FcGetPrgname ();
1525 FcPatternObjectAddString (p, FC_PRGNAME_OBJECT, prgname);
1529 s = config->substFont;
1532 s = config->substScan;
1538 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1539 if (!st && config->maxObjects)
1542 if (FcDebug () & FC_DBG_EDIT)
1544 printf ("FcConfigSubstitute ");
1547 for (; s; s = s->next)
1550 * Check the tests to see if
1551 * they all match the pattern
1553 for (t = s->test, i = 0; t; t = t->next, i++)
1555 if (FcDebug () & FC_DBG_EDIT)
1557 printf ("FcConfigSubstitute test ");
1561 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1566 st[i].elt = FcPatternObjectFindElt (m, t->object);
1570 * If there's no such field in the font,
1571 * then FcQualAll matches while FcQualAny does not
1575 if (t->qual == FcQualAll)
1584 * Check to see if there is a match, mark the location
1585 * to apply match-relative edits
1587 st[i].value = FcConfigMatchValueList (m, p_pat, kind, t, st[i].elt->values);
1590 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1592 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1597 if (FcDebug () & FC_DBG_EDIT)
1598 printf ("No match\n");
1601 if (FcDebug () & FC_DBG_EDIT)
1603 printf ("Substitute ");
1606 for (e = s->edit; e; e = e->next)
1609 * Evaluate the list of expressions
1611 l = FcConfigValues (p, p_pat,kind, e->expr, e->binding);
1613 * Locate any test associated with this field, skipping
1614 * tests associated with the pattern when substituting in
1617 for (t = s->test, i = 0; t; t = t->next, i++)
1619 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1620 t->object == e->object)
1623 * KLUDGE - the pattern may have been reallocated or
1624 * things may have been inserted or deleted above
1625 * this element by other edits. Go back and find
1628 if (e != s->edit && st[i].elt)
1629 st[i].elt = FcPatternObjectFindElt (p, t->object);
1635 switch (FC_OP_GET_OP (e->op)) {
1638 * If there was a test, then replace the matched
1639 * value with the new list of values
1643 FcValueList *thisValue = st[i].value;
1644 FcValueList *nextValue = thisValue;
1647 * Append the new list of values after the current value
1649 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l, e->object);
1651 * Delete the marked value
1654 FcConfigDel (&st[i].elt->values, thisValue);
1656 * Adjust any pointers into the value list to ensure
1657 * future edits occur at the same place
1659 for (t = s->test, i = 0; t; t = t->next, i++)
1661 if (st[i].value == thisValue)
1662 st[i].value = nextValue;
1666 /* fall through ... */
1667 case FcOpAssignReplace:
1669 * Delete all of the values and insert
1672 FcConfigPatternDel (p, e->object);
1673 FcConfigPatternAdd (p, e->object, l, FcTrue);
1675 * Adjust any pointers into the value list as they no
1676 * longer point to anything valid
1680 FcPatternElt *thisElt = st[i].elt;
1681 for (t = s->test, i = 0; t; t = t->next, i++)
1683 if (st[i].elt == thisElt)
1691 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l, e->object);
1694 /* fall through ... */
1695 case FcOpPrependFirst:
1696 FcConfigPatternAdd (p, e->object, l, FcFalse);
1701 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l, e->object);
1704 /* fall through ... */
1705 case FcOpAppendLast:
1706 FcConfigPatternAdd (p, e->object, l, FcTrue);
1711 FcConfigDel (&st[i].elt->values, st[i].value);
1714 /* fall through ... */
1716 FcConfigPatternDel (p, e->object);
1719 FcValueListDestroy (l);
1724 * Now go through the pattern and eliminate
1725 * any properties without data
1727 for (e = s->edit; e; e = e->next)
1728 FcConfigPatternCanon (p, e->object);
1730 if (FcDebug () & FC_DBG_EDIT)
1732 printf ("FcConfigSubstitute edit");
1737 if (FcDebug () & FC_DBG_EDIT)
1739 printf ("FcConfigSubstitute done");
1746 FcConfigSubstitute (FcConfig *config,
1750 return FcConfigSubstituteWithPat (config, p, 0, kind);
1753 #if defined (_WIN32)
1755 static FcChar8 fontconfig_path[1000] = ""; /* MT-dontcare */
1757 # if (defined (PIC) || defined (DLL_EXPORT))
1760 DllMain (HINSTANCE hinstDLL,
1762 LPVOID lpvReserved);
1765 DllMain (HINSTANCE hinstDLL,
1771 switch (fdwReason) {
1772 case DLL_PROCESS_ATTACH:
1773 if (!GetModuleFileName ((HMODULE) hinstDLL, (LPCH) fontconfig_path,
1774 sizeof (fontconfig_path)))
1777 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1778 * assume it's a Unix-style installation tree, and use
1779 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1780 * folder where the DLL is as FONTCONFIG_PATH.
1782 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1786 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1787 if (p && (FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "bin") == 0 ||
1788 FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "lib") == 0))
1790 strcat ((char *) fontconfig_path, "\\etc\\fonts");
1793 fontconfig_path[0] = '\0';
1803 #undef FONTCONFIG_PATH
1804 #define FONTCONFIG_PATH fontconfig_path
1806 #endif /* !_WIN32 */
1808 #ifndef FONTCONFIG_FILE
1809 #define FONTCONFIG_FILE "fonts.conf"
1813 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1819 dir = (FcChar8 *) "";
1821 osize = strlen ((char *) dir) + 1 + strlen ((char *) file) + 1;
1823 * workaround valgrind warning because glibc takes advantage of how it knows memory is
1824 * allocated to implement strlen by reading in groups of 4
1826 size = (osize + 3) & ~3;
1828 path = malloc (size);
1832 strcpy ((char *) path, (const char *) dir);
1833 /* make sure there's a single separator */
1835 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1836 path[strlen((char *) path)-1] != '\\')) &&
1839 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1840 strcat ((char *) path, "\\");
1842 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1843 strcat ((char *) path, "/");
1847 strcat ((char *) path, (char *) file);
1849 if (access ((char *) path, R_OK) == 0)
1858 FcConfigGetPath (void)
1861 FcChar8 *env, *e, *colon;
1866 npath = 2; /* default dir + null */
1867 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1873 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1876 path = calloc (npath, sizeof (FcChar8 *));
1886 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1888 colon = e + strlen ((char *) e);
1889 path[i] = malloc (colon - e + 1);
1892 strncpy ((char *) path[i], (const char *) e, colon - e);
1893 path[i][colon - e] = '\0';
1903 if (fontconfig_path[0] == '\0')
1906 if(!GetModuleFileName(NULL, (LPCH) fontconfig_path, sizeof(fontconfig_path)))
1908 p = strrchr ((const char *) fontconfig_path, '\\');
1910 strcat ((char *) fontconfig_path, "\\fonts");
1913 dir = (FcChar8 *) FONTCONFIG_PATH;
1914 path[i] = malloc (strlen ((char *) dir) + 1);
1917 strcpy ((char *) path[i], (const char *) dir);
1921 for (i = 0; path[i]; i++)
1929 FcConfigFreePath (FcChar8 **path)
1933 for (p = path; *p; p++)
1938 static FcBool _FcConfigHomeEnabled = FcTrue; /* MT-goodenough */
1943 if (_FcConfigHomeEnabled)
1945 char *home = getenv ("HOME");
1949 home = getenv ("USERPROFILE");
1952 return (FcChar8 *) home;
1958 FcConfigXdgCacheHome (void)
1960 const char *env = getenv ("XDG_CACHE_HOME");
1961 FcChar8 *ret = NULL;
1964 ret = FcStrCopy ((const FcChar8 *)env);
1967 const FcChar8 *home = FcConfigHome ();
1968 size_t len = home ? strlen ((const char *)home) : 0;
1970 ret = malloc (len + 7 + 1);
1973 memcpy (ret, home, len);
1974 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".cache", 7);
1983 FcConfigXdgConfigHome (void)
1985 const char *env = getenv ("XDG_CONFIG_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 + 8 + 1);
1998 memcpy (ret, home, len);
1999 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".config", 8);
2008 FcConfigXdgDataHome (void)
2010 const char *env = getenv ("XDG_DATA_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 + 13 + 1);
2023 memcpy (ret, home, len);
2024 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".local" FC_DIR_SEPARATOR_S "share", 13);
2033 FcConfigEnableHome (FcBool enable)
2035 FcBool prev = _FcConfigHomeEnabled;
2036 _FcConfigHomeEnabled = enable;
2041 FcConfigFilename (const FcChar8 *url)
2043 FcChar8 *file, *dir, **path, **p;
2047 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
2049 url = (FcChar8 *) FONTCONFIG_FILE;
2054 if (isalpha (*url) &&
2056 (url[2] == '/' || url[2] == '\\'))
2062 dir = FcConfigHome ();
2064 file = FcConfigFileExists (dir, url + 1);
2073 file = FcConfigFileExists (0, url);
2076 path = FcConfigGetPath ();
2079 for (p = path; *p; p++)
2081 file = FcConfigFileExists (*p, url);
2085 FcConfigFreePath (path);
2093 * Manage the application-specific fonts
2097 FcConfigAppFontAddFile (FcConfig *config,
2098 const FcChar8 *file)
2107 config = FcConfigGetCurrent ();
2112 subdirs = FcStrSetCreate ();
2116 set = FcConfigGetFonts (config, FcSetApplication);
2119 set = FcFontSetCreate ();
2122 FcStrSetDestroy (subdirs);
2125 FcConfigSetFonts (config, set, FcSetApplication);
2128 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
2130 FcStrSetDestroy (subdirs);
2133 if ((sublist = FcStrListCreate (subdirs)))
2135 while ((subdir = FcStrListNext (sublist)))
2137 FcConfigAppFontAddDir (config, subdir);
2139 FcStrListDone (sublist);
2141 FcStrSetDestroy (subdirs);
2146 FcConfigAppFontAddDir (FcConfig *config,
2154 config = FcConfigGetCurrent ();
2159 dirs = FcStrSetCreate ();
2163 set = FcConfigGetFonts (config, FcSetApplication);
2166 set = FcFontSetCreate ();
2169 FcStrSetDestroy (dirs);
2172 FcConfigSetFonts (config, set, FcSetApplication);
2175 FcStrSetAddFilename (dirs, dir);
2177 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
2179 FcStrSetDestroy (dirs);
2182 FcStrSetDestroy (dirs);
2187 FcConfigAppFontClear (FcConfig *config)
2191 config = FcConfigGetCurrent ();
2196 FcConfigSetFonts (config, 0, FcSetApplication);
2200 * Manage filename-based font source selectors
2204 FcConfigGlobAdd (FcConfig *config,
2205 const FcChar8 *glob,
2208 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
2210 return FcStrSetAdd (set, glob);
2214 FcConfigGlobMatch (const FcChar8 *glob,
2215 const FcChar8 *string)
2219 while ((c = *glob++))
2223 /* short circuit common case */
2226 /* short circuit another common case */
2227 if (strchr ((char *) glob, '*') == 0)
2231 l1 = strlen ((char *) string);
2232 l2 = strlen ((char *) glob);
2235 string += (l1 - l2);
2239 if (FcConfigGlobMatch (glob, string))
2245 if (*string++ == '\0')
2254 return *string == '\0';
2258 FcConfigGlobsMatch (const FcStrSet *globs,
2259 const FcChar8 *string)
2263 for (i = 0; i < globs->num; i++)
2264 if (FcConfigGlobMatch (globs->strs[i], string))
2270 FcConfigAcceptFilename (FcConfig *config,
2271 const FcChar8 *filename)
2273 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2275 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2281 * Manage font-pattern based font source selectors
2285 FcConfigPatternsAdd (FcConfig *config,
2289 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2291 return FcFontSetAdd (set, pattern);
2295 FcConfigPatternsMatch (const FcFontSet *patterns,
2296 const FcPattern *font)
2300 for (i = 0; i < patterns->nfont; i++)
2301 if (FcListPatternMatchAny (patterns->fonts[i], font))
2307 FcConfigAcceptFont (FcConfig *config,
2308 const FcPattern *font)
2310 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2312 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2318 FcConfigGetSysRoot (const FcConfig *config)
2322 config = FcConfigGetCurrent ();
2327 return config->sysRoot;
2331 FcConfigSetSysRoot (FcConfig *config,
2332 const FcChar8 *sysroot)
2335 FcBool init = FcFalse;
2339 /* We can't use FcConfigGetCurrent() here to ensure
2340 * the sysroot is set prior to initialize FcConfig,
2341 * to avoid loading caches from non-sysroot dirs.
2342 * So postpone the initialization later.
2344 config = fc_atomic_ptr_get (&_fcConfig);
2347 config = FcConfigCreate ();
2354 s = FcStrCopyFilename (sysroot);
2358 if (config->sysRoot)
2359 FcStrFree (config->sysRoot);
2361 config->sysRoot = s;
2364 config = FcInitLoadOwnConfigAndFonts (config);
2365 FcConfigSetCurrent (config);
2370 #include "fcaliastail.h"