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 (PIC) || defined (DLL_EXPORT))
37 #if defined (_WIN32) && !defined (R_OK)
41 static FcConfig *_fcConfig; /* MT-safe */
48 config = fc_atomic_ptr_get (&_fcConfig);
51 config = FcInitLoadConfigAndFonts ();
53 if (!fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config)) {
54 FcConfigDestroy (config);
64 return FcConfigEnsure () ? FcTrue : FcFalse;
70 FcConfig *cfg = fc_atomic_ptr_get (&_fcConfig);
71 if (cfg && fc_atomic_ptr_cmpexch (&_fcConfig, cfg, NULL))
72 FcConfigDestroy (cfg);
82 config = malloc (sizeof (FcConfig));
86 config->configDirs = FcStrSetCreate ();
87 if (!config->configDirs)
90 config->configFiles = FcStrSetCreate ();
91 if (!config->configFiles)
94 config->fontDirs = FcStrSetCreate ();
95 if (!config->fontDirs)
98 config->acceptGlobs = FcStrSetCreate ();
99 if (!config->acceptGlobs)
102 config->rejectGlobs = FcStrSetCreate ();
103 if (!config->rejectGlobs)
106 config->acceptPatterns = FcFontSetCreate ();
107 if (!config->acceptPatterns)
110 config->rejectPatterns = FcFontSetCreate ();
111 if (!config->rejectPatterns)
114 config->cacheDirs = FcStrSetCreate ();
115 if (!config->cacheDirs)
120 config->substPattern = 0;
121 config->substFont = 0;
122 config->substScan = 0;
123 config->maxObjects = 0;
124 for (set = FcSetSystem; set <= FcSetApplication; set++)
125 config->fonts[set] = 0;
127 config->rescanTime = time(0);
128 config->rescanInterval = 30;
130 config->expr_pool = NULL;
132 FcRefInit (&config->ref, 1);
137 FcFontSetDestroy (config->rejectPatterns);
139 FcFontSetDestroy (config->acceptPatterns);
141 FcStrSetDestroy (config->rejectGlobs);
143 FcStrSetDestroy (config->acceptGlobs);
145 FcStrSetDestroy (config->fontDirs);
147 FcStrSetDestroy (config->configFiles);
149 FcStrSetDestroy (config->configDirs);
157 FcConfigNewestFile (FcStrSet *files)
159 FcStrList *list = FcStrListCreate (files);
160 FcFileTime newest = { 0, FcFalse };
166 while ((file = FcStrListNext (list)))
167 if (FcStat (file, &statb) == 0)
168 if (!newest.set || statb.st_mtime - newest.time > 0)
171 newest.time = statb.st_mtime;
173 FcStrListDone (list);
179 FcConfigUptoDate (FcConfig *config)
181 FcFileTime config_time, config_dir_time, font_time;
182 time_t now = time(0);
185 config = FcConfigGetCurrent ();
189 config_time = FcConfigNewestFile (config->configFiles);
190 config_dir_time = FcConfigNewestFile (config->configDirs);
191 font_time = FcConfigNewestFile (config->fontDirs);
192 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
193 (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
194 (font_time.set && (font_time.time - config->rescanTime) > 0))
196 /* We need to check for potential clock problems here (OLPC ticket #6046) */
197 if ((config_time.set && (config_time.time - now) > 0) ||
198 (config_dir_time.set && (config_dir_time.time - now) > 0) ||
199 (font_time.set && (font_time.time - now) > 0))
202 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected.\n");
203 config->rescanTime = now;
209 config->rescanTime = now;
214 FcSubstDestroy (FcSubst *s)
222 FcTestDestroy (s->test);
224 FcEditDestroy (s->edit);
231 FcConfigAllocExpr (FcConfig *config)
233 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end)
235 FcExprPage *new_page;
237 new_page = malloc (sizeof (FcExprPage));
241 new_page->next_page = config->expr_pool;
242 new_page->next = new_page->exprs;
243 config->expr_pool = new_page;
246 return config->expr_pool->next++;
250 FcConfigReference (FcConfig *config)
254 config = FcConfigGetCurrent ();
259 FcRefInc (&config->ref);
265 FcConfigDestroy (FcConfig *config)
270 if (FcRefDec (&config->ref) != 1)
273 fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL);
275 FcStrSetDestroy (config->configDirs);
276 FcStrSetDestroy (config->fontDirs);
277 FcStrSetDestroy (config->cacheDirs);
278 FcStrSetDestroy (config->configFiles);
279 FcStrSetDestroy (config->acceptGlobs);
280 FcStrSetDestroy (config->rejectGlobs);
281 FcFontSetDestroy (config->acceptPatterns);
282 FcFontSetDestroy (config->rejectPatterns);
285 FcBlanksDestroy (config->blanks);
287 FcSubstDestroy (config->substPattern);
288 FcSubstDestroy (config->substFont);
289 FcSubstDestroy (config->substScan);
290 for (set = FcSetSystem; set <= FcSetApplication; set++)
291 if (config->fonts[set])
292 FcFontSetDestroy (config->fonts[set]);
294 page = config->expr_pool;
297 FcExprPage *next = page->next_page;
306 * Add cache to configuration, adding fonts and directories
310 FcConfigAddCache (FcConfig *config, FcCache *cache,
311 FcSetName set, FcStrSet *dirSet)
320 fs = FcCacheSet (cache);
325 for (i = 0; i < fs->nfont; i++)
327 FcPattern *font = FcFontSetFont (fs, i);
331 * Check to see if font is banned by filename
333 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
334 0, &font_file) == FcResultMatch &&
335 !FcConfigAcceptFilename (config, font_file))
341 * Check to see if font is banned by pattern
343 if (!FcConfigAcceptFont (config, font))
347 FcFontSetAdd (config->fonts[set], font);
349 FcDirCacheReference (cache, nref);
355 dirs = FcCacheDirs (cache);
358 for (i = 0; i < cache->dirs_count; i++)
360 FcChar8 *dir = FcOffsetToPtr (dirs, dirs[i], FcChar8);
361 if (FcConfigAcceptFilename (config, dir))
362 FcStrSetAddFilename (dirSet, dir);
369 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
375 dirlist = FcStrListCreate (dirSet);
379 while ((dir = FcStrListNext (dirlist)))
381 if (FcDebug () & FC_DBG_FONTSET)
382 printf ("adding fonts from%s\n", dir);
383 cache = FcDirCacheRead (dir, FcFalse, config);
386 FcConfigAddCache (config, cache, set, dirSet);
387 FcDirCacheUnload (cache);
389 FcStrListDone (dirlist);
394 * Scan the current list of directories in the configuration
395 * and build the set of available fonts.
399 FcConfigBuildFonts (FcConfig *config)
405 config = FcConfigGetCurrent ();
410 fonts = FcFontSetCreate ();
414 FcConfigSetFonts (config, fonts, FcSetSystem);
416 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
418 if (FcDebug () & FC_DBG_FONTSET)
419 FcFontSetPrint (fonts);
424 FcConfigSetCurrent (FcConfig *config)
429 cfg = fc_atomic_ptr_get (&_fcConfig);
434 if (!config->fonts[FcSetSystem])
435 if (!FcConfigBuildFonts (config))
438 if (!fc_atomic_ptr_cmpexch (&_fcConfig, cfg, config))
442 FcConfigDestroy (cfg);
448 FcConfigGetCurrent (void)
450 return FcConfigEnsure ();
454 FcConfigAddConfigDir (FcConfig *config,
457 return FcStrSetAddFilename (config->configDirs, d);
461 FcConfigGetConfigDirs (FcConfig *config)
465 config = FcConfigGetCurrent ();
469 return FcStrListCreate (config->configDirs);
473 FcConfigAddFontDir (FcConfig *config,
476 return FcStrSetAddFilename (config->fontDirs, d);
480 FcConfigAddDir (FcConfig *config,
483 return (FcConfigAddConfigDir (config, d) &&
484 FcConfigAddFontDir (config, d));
488 FcConfigGetFontDirs (FcConfig *config)
492 config = FcConfigGetCurrent ();
496 return FcStrListCreate (config->fontDirs);
500 FcConfigAddCacheDir (FcConfig *config,
503 return FcStrSetAddFilename (config->cacheDirs, d);
507 FcConfigGetCacheDirs (const FcConfig *config)
511 config = FcConfigGetCurrent ();
515 return FcStrListCreate (config->cacheDirs);
519 FcConfigAddConfigFile (FcConfig *config,
523 FcChar8 *file = FcConfigFilename (f);
528 ret = FcStrSetAdd (config->configFiles, file);
534 FcConfigGetConfigFiles (FcConfig *config)
538 config = FcConfigGetCurrent ();
542 return FcStrListCreate (config->configFiles);
546 FcConfigGetCache (FcConfig *config FC_UNUSED)
552 FcConfigGetFonts (FcConfig *config,
557 config = FcConfigGetCurrent ();
561 return config->fonts[set];
565 FcConfigSetFonts (FcConfig *config,
569 if (config->fonts[set])
570 FcFontSetDestroy (config->fonts[set]);
571 config->fonts[set] = fonts;
575 FcConfigGetBlanks (FcConfig *config)
579 config = FcConfigGetCurrent ();
583 return config->blanks;
587 FcConfigAddBlank (FcConfig *config,
590 FcBlanks *b, *freeme = 0;
595 freeme = b = FcBlanksCreate ();
599 if (!FcBlanksAdd (b, blank))
602 FcBlanksDestroy (freeme);
610 FcConfigGetRescanInterval (FcConfig *config)
614 config = FcConfigGetCurrent ();
618 return config->rescanInterval;
622 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
626 config = FcConfigGetCurrent ();
630 config->rescanInterval = rescanInterval;
635 * A couple of typos escaped into the library
638 FcConfigGetRescanInverval (FcConfig *config)
640 return FcConfigGetRescanInterval (config);
644 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
646 return FcConfigSetRescanInterval (config, rescanInterval);
651 FcConfigAddEdit (FcConfig *config,
656 FcSubst *subst, **prev;
662 prev = &config->substPattern;
665 prev = &config->substFont;
668 prev = &config->substScan;
673 subst = (FcSubst *) malloc (sizeof (FcSubst));
676 for (; *prev; prev = &(*prev)->next);
682 for (t = test; t; t = t->next)
684 if (t->kind == FcMatchDefault)
688 if (config->maxObjects < num)
689 config->maxObjects = num;
690 if (FcDebug () & FC_DBG_EDIT)
692 printf ("Add Subst ");
693 FcSubstPrint (subst);
698 typedef struct _FcSubState {
704 FcConfigPromote (FcValue v, FcValue u, FcValuePromotionBuffer *buf)
706 if (v.type == FcTypeInteger)
708 v.type = FcTypeDouble;
709 v.u.d = (double) v.u.i;
711 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
713 v.u.m = &FcIdentityMatrix;
714 v.type = FcTypeMatrix;
716 else if (buf && v.type == FcTypeString && u.type == FcTypeLangSet)
718 v.u.l = FcLangSetPromote (v.u.s, buf);
719 v.type = FcTypeLangSet;
725 FcConfigCompareValue (const FcValue *left_o,
727 const FcValue *right_o)
729 FcValue left = FcValueCanonicalize(left_o);
730 FcValue right = FcValueCanonicalize(right_o);
731 FcBool ret = FcFalse;
732 FcOp op = FC_OP_GET_OP (op_);
733 int flags = FC_OP_GET_FLAGS (op_);
734 FcValuePromotionBuffer buf1, buf2;
736 left = FcConfigPromote (left, right, &buf1);
737 right = FcConfigPromote (right, left, &buf2);
738 if (left.type == right.type)
742 break; /* FcConfigPromote prevents this from happening */
748 ret = left.u.d == right.u.d;
751 case FcOpNotContains:
752 ret = left.u.d != right.u.d;
755 ret = left.u.d < right.u.d;
758 ret = left.u.d <= right.u.d;
761 ret = left.u.d > right.u.d;
764 ret = left.u.d >= right.u.d;
775 ret = left.u.b == right.u.b;
778 case FcOpNotContains:
779 ret = left.u.b != right.u.b;
789 if (flags & FcOpFlagIgnoreBlanks)
790 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) == 0;
792 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
795 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
798 if (flags & FcOpFlagIgnoreBlanks)
799 ret = FcStrCmpIgnoreBlanksAndCase (left.u.s, right.u.s) != 0;
801 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
803 case FcOpNotContains:
804 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) == 0;
815 ret = FcMatrixEqual (left.u.m, right.u.m);
818 case FcOpNotContains:
819 ret = !FcMatrixEqual (left.u.m, right.u.m);
829 /* left contains right if right is a subset of left */
830 ret = FcCharSetIsSubset (right.u.c, left.u.c);
832 case FcOpNotContains:
833 /* left contains right if right is a subset of left */
834 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
837 ret = FcCharSetEqual (left.u.c, right.u.c);
840 ret = !FcCharSetEqual (left.u.c, right.u.c);
850 ret = FcLangSetContains (left.u.l, right.u.l);
852 case FcOpNotContains:
853 ret = !FcLangSetContains (left.u.l, right.u.l);
856 ret = FcLangSetEqual (left.u.l, right.u.l);
859 ret = !FcLangSetEqual (left.u.l, right.u.l);
881 ret = left.u.f == right.u.f;
884 case FcOpNotContains:
885 ret = left.u.f != right.u.f;
895 if (op == FcOpNotEqual || op == FcOpNotContains)
902 #define _FcDoubleFloor(d) ((int) (d))
903 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
904 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
905 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
906 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
907 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
910 FcConfigEvaluate (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e)
915 FcOp op = FC_OP_GET_OP (e->op);
919 v.type = FcTypeInteger;
923 v.type = FcTypeDouble;
927 v.type = FcTypeString;
934 FcValue xx, xy, yx, yy;
935 v.type = FcTypeMatrix;
936 xx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xx), v, NULL);
937 xy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xy), v, NULL);
938 yx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yx), v, NULL);
939 yy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yy), v, NULL);
940 if (xx.type == FcTypeDouble && xy.type == FcTypeDouble &&
941 yx.type == FcTypeDouble && yy.type == FcTypeDouble)
955 v.type = FcTypeCharSet;
960 v.type = FcTypeLangSet;
969 if (kind == FcMatchFont && e->u.name.kind == FcMatchPattern)
971 if (FcResultMatch != FcPatternObjectGet (p_pat, e->u.name.object, 0, &v))
974 else if (kind == FcMatchPattern && e->u.name.kind == FcMatchFont)
977 "Fontconfig warning: <name> tag has target=\"font\" in a <match target=\"pattern\">.\n");
982 if (FcResultMatch != FcPatternObjectGet (p, e->u.name.object, 0, &v))
988 if (FcNameConstant (e->u.constant, &v.u.i))
989 v.type = FcTypeInteger;
994 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
995 if (vl.type == FcTypeBool)
998 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.left);
1000 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.right);
1003 v.type = FcTypeVoid;
1004 FcValueDestroy (vl);
1013 case FcOpNotContains:
1015 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1016 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1017 v.type = FcTypeBool;
1018 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
1019 FcValueDestroy (vl);
1020 FcValueDestroy (vr);
1028 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1029 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1030 vl = FcConfigPromote (vl, vr, NULL);
1031 vr = FcConfigPromote (vr, vl, NULL);
1032 if (vl.type == vr.type)
1034 switch ((int) vl.type) {
1038 v.type = FcTypeDouble;
1039 v.u.d = vl.u.d + vr.u.d;
1042 v.type = FcTypeDouble;
1043 v.u.d = vl.u.d - vr.u.d;
1046 v.type = FcTypeDouble;
1047 v.u.d = vl.u.d * vr.u.d;
1050 v.type = FcTypeDouble;
1051 v.u.d = vl.u.d / vr.u.d;
1054 v.type = FcTypeVoid;
1057 if (v.type == FcTypeDouble &&
1058 v.u.d == (double) (int) v.u.d)
1060 v.type = FcTypeInteger;
1061 v.u.i = (int) v.u.d;
1067 v.type = FcTypeBool;
1068 v.u.b = vl.u.b || vr.u.b;
1071 v.type = FcTypeBool;
1072 v.u.b = vl.u.b && vr.u.b;
1075 v.type = FcTypeVoid;
1082 v.type = FcTypeString;
1083 str = FcStrPlus (vl.u.s, vr.u.s);
1084 v.u.s = FcStrdup (str);
1088 v.type = FcTypeVoid;
1091 v.type = FcTypeVoid;
1098 v.type = FcTypeMatrix;
1099 m = malloc (sizeof (FcMatrix));
1102 FcMatrixMultiply (m, vl.u.m, vr.u.m);
1107 v.type = FcTypeVoid;
1111 v.type = FcTypeVoid;
1118 v.type = FcTypeCharSet;
1119 v.u.c = FcCharSetUnion (vl.u.c, vr.u.c);
1121 v.type = FcTypeVoid;
1124 v.type = FcTypeCharSet;
1125 v.u.c = FcCharSetSubtract (vl.u.c, vr.u.c);
1127 v.type = FcTypeVoid;
1130 v.type = FcTypeVoid;
1137 v.type = FcTypeLangSet;
1138 v.u.l = FcLangSetUnion (vl.u.l, vr.u.l);
1140 v.type = FcTypeVoid;
1143 v.type = FcTypeLangSet;
1144 v.u.l = FcLangSetSubtract (vl.u.l, vr.u.l);
1146 v.type = FcTypeVoid;
1149 v.type = FcTypeVoid;
1154 v.type = FcTypeVoid;
1159 v.type = FcTypeVoid;
1160 FcValueDestroy (vl);
1161 FcValueDestroy (vr);
1164 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1165 switch ((int) vl.type) {
1167 v.type = FcTypeBool;
1171 v.type = FcTypeVoid;
1174 FcValueDestroy (vl);
1177 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1178 switch ((int) vl.type) {
1183 v.type = FcTypeInteger;
1184 v.u.i = FcDoubleFloor (vl.u.d);
1187 v.type = FcTypeVoid;
1190 FcValueDestroy (vl);
1193 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1194 switch ((int) vl.type) {
1199 v.type = FcTypeInteger;
1200 v.u.i = FcDoubleCeil (vl.u.d);
1203 v.type = FcTypeVoid;
1206 FcValueDestroy (vl);
1209 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1210 switch ((int) vl.type) {
1215 v.type = FcTypeInteger;
1216 v.u.i = FcDoubleRound (vl.u.d);
1219 v.type = FcTypeVoid;
1222 FcValueDestroy (vl);
1225 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1226 switch ((int) vl.type) {
1231 v.type = FcTypeInteger;
1232 v.u.i = FcDoubleTrunc (vl.u.d);
1235 v.type = FcTypeVoid;
1238 FcValueDestroy (vl);
1241 v.type = FcTypeVoid;
1247 static FcValueList *
1248 FcConfigMatchValueList (FcPattern *p,
1252 FcValueList *values)
1254 FcValueList *ret = 0;
1255 FcExpr *e = t->expr;
1261 /* Compute the value of the match expression */
1262 if (FC_OP_GET_OP (e->op) == FcOpComma)
1264 value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1265 e = e->u.tree.right;
1269 value = FcConfigEvaluate (p, p_pat, kind, e);
1273 for (v = values; v; v = FcValueListNext(v))
1275 /* Compare the pattern value to the match expression value */
1276 if (FcConfigCompareValue (&v->value, t->op, &value))
1283 if (t->qual == FcQualAll)
1290 FcValueDestroy (value);
1295 static FcValueList *
1296 FcConfigValues (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e, FcValueBinding binding)
1302 l = (FcValueList *) malloc (sizeof (FcValueList));
1305 if (FC_OP_GET_OP (e->op) == FcOpComma)
1307 l->value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1308 l->next = FcConfigValues (p, p_pat, kind, e->u.tree.right, binding);
1312 l->value = FcConfigEvaluate (p, p_pat, kind, e);
1315 l->binding = binding;
1316 if (l->value.type == FcTypeVoid)
1318 FcValueList *next = FcValueListNext(l);
1328 FcConfigAdd (FcValueListPtr *head,
1329 FcValueList *position,
1334 FcValueListPtr *prev, l, last, v;
1335 FcValueBinding sameBinding;
1338 * Make sure the stored type is valid for built-in objects
1340 for (l = new; l != NULL; l = FcValueListNext (l))
1342 if (!FcObjectValidType (object, l->value.type))
1345 "Fontconfig warning: FcPattern object %s does not accept value", FcObjectName (object));
1346 FcValuePrintFile (stderr, l->value);
1347 fprintf (stderr, "\n");
1349 if (FcDebug () & FC_DBG_EDIT)
1351 printf ("Not adding\n");
1359 sameBinding = position->binding;
1361 sameBinding = FcValueBindingWeak;
1362 for (v = new; v != NULL; v = FcValueListNext(v))
1363 if (v->binding == FcValueBindingSame)
1364 v->binding = sameBinding;
1368 prev = &position->next;
1370 for (prev = head; *prev != NULL;
1371 prev = &(*prev)->next)
1378 for (prev = head; *prev != NULL;
1379 prev = &(*prev)->next)
1381 if (*prev == position)
1388 if (FcDebug () & FC_DBG_EDIT)
1391 printf ("position not on list\n");
1395 if (FcDebug () & FC_DBG_EDIT)
1397 printf ("%s list before ", append ? "Append" : "Prepend");
1398 FcValueListPrintWithPosition (*head, *prev);
1405 while (last->next != NULL)
1412 if (FcDebug () & FC_DBG_EDIT)
1414 printf ("%s list after ", append ? "Append" : "Prepend");
1415 FcValueListPrint (*head);
1423 FcConfigDel (FcValueListPtr *head,
1424 FcValueList *position)
1426 FcValueListPtr *prev;
1428 for (prev = head; *prev != NULL; prev = &(*prev)->next)
1430 if (*prev == position)
1432 *prev = position->next;
1433 position->next = NULL;
1434 FcValueListDestroy (position);
1441 FcConfigPatternAdd (FcPattern *p,
1448 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1452 FcConfigAdd (&e->values, 0, append, list, object);
1457 * Delete all values associated with a field
1460 FcConfigPatternDel (FcPattern *p,
1463 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1466 while (e->values != NULL)
1467 FcConfigDel (&e->values, e->values);
1471 FcConfigPatternCanon (FcPattern *p,
1474 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1477 if (e->values == NULL)
1478 FcPatternObjectDel (p, object);
1482 FcConfigSubstituteWithPat (FcConfig *config,
1498 config = FcConfigGetCurrent ();
1504 case FcMatchPattern:
1505 s = config->substPattern;
1506 strs = FcGetDefaultLangs ();
1509 FcStrList *l = FcStrListCreate (strs);
1513 FcStrSetDestroy (strs);
1514 while (l && (lang = FcStrListNext (l)))
1516 v.type = FcTypeString;
1518 FcPatternObjectAddWithBinding (p, FC_LANG_OBJECT, v, FcValueBindingWeak, FcTrue);
1524 s = config->substFont;
1527 s = config->substScan;
1533 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1534 if (!st && config->maxObjects)
1537 if (FcDebug () & FC_DBG_EDIT)
1539 printf ("FcConfigSubstitute ");
1542 for (; s; s = s->next)
1545 * Check the tests to see if
1546 * they all match the pattern
1548 for (t = s->test, i = 0; t; t = t->next, i++)
1550 if (FcDebug () & FC_DBG_EDIT)
1552 printf ("FcConfigSubstitute test ");
1556 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1561 st[i].elt = FcPatternObjectFindElt (m, t->object);
1565 * If there's no such field in the font,
1566 * then FcQualAll matches while FcQualAny does not
1570 if (t->qual == FcQualAll)
1579 * Check to see if there is a match, mark the location
1580 * to apply match-relative edits
1582 st[i].value = FcConfigMatchValueList (m, p_pat, kind, t, st[i].elt->values);
1585 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1587 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1592 if (FcDebug () & FC_DBG_EDIT)
1593 printf ("No match\n");
1596 if (FcDebug () & FC_DBG_EDIT)
1598 printf ("Substitute ");
1601 for (e = s->edit; e; e = e->next)
1604 * Evaluate the list of expressions
1606 l = FcConfigValues (p, p_pat,kind, e->expr, e->binding);
1608 * Locate any test associated with this field, skipping
1609 * tests associated with the pattern when substituting in
1612 for (t = s->test, i = 0; t; t = t->next, i++)
1614 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1615 t->object == e->object)
1618 * KLUDGE - the pattern may have been reallocated or
1619 * things may have been inserted or deleted above
1620 * this element by other edits. Go back and find
1623 if (e != s->edit && st[i].elt)
1624 st[i].elt = FcPatternObjectFindElt (p, t->object);
1630 switch (FC_OP_GET_OP (e->op)) {
1633 * If there was a test, then replace the matched
1634 * value with the new list of values
1638 FcValueList *thisValue = st[i].value;
1639 FcValueList *nextValue = thisValue;
1642 * Append the new list of values after the current value
1644 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l, e->object);
1646 * Delete the marked value
1649 FcConfigDel (&st[i].elt->values, thisValue);
1651 * Adjust any pointers into the value list to ensure
1652 * future edits occur at the same place
1654 for (t = s->test, i = 0; t; t = t->next, i++)
1656 if (st[i].value == thisValue)
1657 st[i].value = nextValue;
1661 /* fall through ... */
1662 case FcOpAssignReplace:
1664 * Delete all of the values and insert
1667 FcConfigPatternDel (p, e->object);
1668 FcConfigPatternAdd (p, e->object, l, FcTrue);
1670 * Adjust any pointers into the value list as they no
1671 * longer point to anything valid
1675 FcPatternElt *thisElt = st[i].elt;
1676 for (t = s->test, i = 0; t; t = t->next, i++)
1678 if (st[i].elt == thisElt)
1686 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l, e->object);
1689 /* fall through ... */
1690 case FcOpPrependFirst:
1691 FcConfigPatternAdd (p, e->object, l, FcFalse);
1696 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l, e->object);
1699 /* fall through ... */
1700 case FcOpAppendLast:
1701 FcConfigPatternAdd (p, e->object, l, FcTrue);
1704 FcValueListDestroy (l);
1709 * Now go through the pattern and eliminate
1710 * any properties without data
1712 for (e = s->edit; e; e = e->next)
1713 FcConfigPatternCanon (p, e->object);
1715 if (FcDebug () & FC_DBG_EDIT)
1717 printf ("FcConfigSubstitute edit");
1722 if (FcDebug () & FC_DBG_EDIT)
1724 printf ("FcConfigSubstitute done");
1731 FcConfigSubstitute (FcConfig *config,
1735 return FcConfigSubstituteWithPat (config, p, 0, kind);
1738 #if defined (_WIN32)
1740 # define WIN32_LEAN_AND_MEAN
1741 # define WIN32_EXTRA_LEAN
1742 # include <windows.h>
1744 static FcChar8 fontconfig_path[1000] = ""; /* MT-dontcare */
1746 # if (defined (PIC) || defined (DLL_EXPORT))
1749 DllMain (HINSTANCE hinstDLL,
1755 switch (fdwReason) {
1756 case DLL_PROCESS_ATTACH:
1757 if (!GetModuleFileName ((HMODULE) hinstDLL, (LPCH) fontconfig_path,
1758 sizeof (fontconfig_path)))
1761 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1762 * assume it's a Unix-style installation tree, and use
1763 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1764 * folder where the DLL is as FONTCONFIG_PATH.
1766 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1770 p = (FcChar8 *) strrchr ((const char *) fontconfig_path, '\\');
1771 if (p && (FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "bin") == 0 ||
1772 FcStrCmpIgnoreCase (p + 1, (const FcChar8 *) "lib") == 0))
1774 strcat ((char *) fontconfig_path, "\\etc\\fonts");
1777 fontconfig_path[0] = '\0';
1787 #undef FONTCONFIG_PATH
1788 #define FONTCONFIG_PATH fontconfig_path
1790 #endif /* !_WIN32 */
1792 #ifndef FONTCONFIG_FILE
1793 #define FONTCONFIG_FILE "fonts.conf"
1797 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1803 dir = (FcChar8 *) "";
1805 osize = strlen ((char *) dir) + 1 + strlen ((char *) file) + 1;
1807 * workaround valgrind warning because glibc takes advantage of how it knows memory is
1808 * allocated to implement strlen by reading in groups of 4
1810 size = (osize + 3) & ~3;
1812 path = malloc (size);
1816 strcpy ((char *) path, (const char *) dir);
1817 /* make sure there's a single separator */
1819 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1820 path[strlen((char *) path)-1] != '\\')) &&
1823 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1824 strcat ((char *) path, "\\");
1826 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1827 strcat ((char *) path, "/");
1831 strcat ((char *) path, (char *) file);
1833 if (access ((char *) path, R_OK) == 0)
1842 FcConfigGetPath (void)
1845 FcChar8 *env, *e, *colon;
1850 npath = 2; /* default dir + null */
1851 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1857 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1860 path = calloc (npath, sizeof (FcChar8 *));
1870 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1872 colon = e + strlen ((char *) e);
1873 path[i] = malloc (colon - e + 1);
1876 strncpy ((char *) path[i], (const char *) e, colon - e);
1877 path[i][colon - e] = '\0';
1887 if (fontconfig_path[0] == '\0')
1890 if(!GetModuleFileName(NULL, (LPCH) fontconfig_path, sizeof(fontconfig_path)))
1892 p = strrchr ((const char *) fontconfig_path, '\\');
1894 strcat ((char *) fontconfig_path, "\\fonts");
1897 dir = (FcChar8 *) FONTCONFIG_PATH;
1898 path[i] = malloc (strlen ((char *) dir) + 1);
1901 strcpy ((char *) path[i], (const char *) dir);
1905 for (i = 0; path[i]; i++)
1913 FcConfigFreePath (FcChar8 **path)
1917 for (p = path; *p; p++)
1922 static FcBool _FcConfigHomeEnabled = FcTrue; /* MT-goodenough */
1927 if (_FcConfigHomeEnabled)
1929 char *home = getenv ("HOME");
1933 home = getenv ("USERPROFILE");
1936 return (FcChar8 *) home;
1942 FcConfigXdgCacheHome (void)
1944 const char *env = getenv ("XDG_CACHE_HOME");
1945 FcChar8 *ret = NULL;
1948 ret = FcStrCopy ((const FcChar8 *)env);
1951 const FcChar8 *home = FcConfigHome ();
1952 size_t len = home ? strlen ((const char *)home) : 0;
1954 ret = malloc (len + 7 + 1);
1957 memcpy (ret, home, len);
1958 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".cache", 7);
1967 FcConfigXdgConfigHome (void)
1969 const char *env = getenv ("XDG_CONFIG_HOME");
1970 FcChar8 *ret = NULL;
1973 ret = FcStrCopy ((const FcChar8 *)env);
1976 const FcChar8 *home = FcConfigHome ();
1977 size_t len = home ? strlen ((const char *)home) : 0;
1979 ret = malloc (len + 8 + 1);
1982 memcpy (ret, home, len);
1983 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".config", 8);
1992 FcConfigXdgDataHome (void)
1994 const char *env = getenv ("XDG_DATA_HOME");
1995 FcChar8 *ret = NULL;
1998 ret = FcStrCopy ((const FcChar8 *)env);
2001 const FcChar8 *home = FcConfigHome ();
2002 size_t len = home ? strlen ((const char *)home) : 0;
2004 ret = malloc (len + 13 + 1);
2007 memcpy (ret, home, len);
2008 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".local" FC_DIR_SEPARATOR_S "share", 13);
2017 FcConfigEnableHome (FcBool enable)
2019 FcBool prev = _FcConfigHomeEnabled;
2020 _FcConfigHomeEnabled = enable;
2025 FcConfigFilename (const FcChar8 *url)
2027 FcChar8 *file, *dir, **path, **p;
2031 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
2033 url = (FcChar8 *) FONTCONFIG_FILE;
2038 if (isalpha (*url) &&
2040 (url[2] == '/' || url[2] == '\\'))
2046 dir = FcConfigHome ();
2048 file = FcConfigFileExists (dir, url + 1);
2057 file = FcConfigFileExists (0, url);
2060 path = FcConfigGetPath ();
2063 for (p = path; *p; p++)
2065 file = FcConfigFileExists (*p, url);
2069 FcConfigFreePath (path);
2077 * Manage the application-specific fonts
2081 FcConfigAppFontAddFile (FcConfig *config,
2082 const FcChar8 *file)
2091 config = FcConfigGetCurrent ();
2096 subdirs = FcStrSetCreate ();
2100 set = FcConfigGetFonts (config, FcSetApplication);
2103 set = FcFontSetCreate ();
2106 FcStrSetDestroy (subdirs);
2109 FcConfigSetFonts (config, set, FcSetApplication);
2112 if (!FcFileScanConfig (set, subdirs, config->blanks, file, config))
2114 FcStrSetDestroy (subdirs);
2117 if ((sublist = FcStrListCreate (subdirs)))
2119 while ((subdir = FcStrListNext (sublist)))
2121 FcConfigAppFontAddDir (config, subdir);
2123 FcStrListDone (sublist);
2125 FcStrSetDestroy (subdirs);
2130 FcConfigAppFontAddDir (FcConfig *config,
2138 config = FcConfigGetCurrent ();
2143 dirs = FcStrSetCreate ();
2147 set = FcConfigGetFonts (config, FcSetApplication);
2150 set = FcFontSetCreate ();
2153 FcStrSetDestroy (dirs);
2156 FcConfigSetFonts (config, set, FcSetApplication);
2159 FcStrSetAddFilename (dirs, dir);
2161 if (!FcConfigAddDirList (config, FcSetApplication, dirs))
2163 FcStrSetDestroy (dirs);
2166 FcStrSetDestroy (dirs);
2171 FcConfigAppFontClear (FcConfig *config)
2175 config = FcConfigGetCurrent ();
2180 FcConfigSetFonts (config, 0, FcSetApplication);
2184 * Manage filename-based font source selectors
2188 FcConfigGlobAdd (FcConfig *config,
2189 const FcChar8 *glob,
2192 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
2194 return FcStrSetAdd (set, glob);
2198 FcConfigGlobMatch (const FcChar8 *glob,
2199 const FcChar8 *string)
2203 while ((c = *glob++))
2207 /* short circuit common case */
2210 /* short circuit another common case */
2211 if (strchr ((char *) glob, '*') == 0)
2215 l1 = strlen ((char *) string);
2216 l2 = strlen ((char *) glob);
2219 string += (l1 - l2);
2223 if (FcConfigGlobMatch (glob, string))
2229 if (*string++ == '\0')
2238 return *string == '\0';
2242 FcConfigGlobsMatch (const FcStrSet *globs,
2243 const FcChar8 *string)
2247 for (i = 0; i < globs->num; i++)
2248 if (FcConfigGlobMatch (globs->strs[i], string))
2254 FcConfigAcceptFilename (FcConfig *config,
2255 const FcChar8 *filename)
2257 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2259 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2265 * Manage font-pattern based font source selectors
2269 FcConfigPatternsAdd (FcConfig *config,
2273 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2275 return FcFontSetAdd (set, pattern);
2279 FcConfigPatternsMatch (const FcFontSet *patterns,
2280 const FcPattern *font)
2284 for (i = 0; i < patterns->nfont; i++)
2285 if (FcListPatternMatchAny (patterns->fonts[i], font))
2291 FcConfigAcceptFont (FcConfig *config,
2292 const FcPattern *font)
2294 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2296 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2301 #include "fcaliastail.h"