2 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
26 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config
= malloc (sizeof (FcConfig
));
50 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
52 config
->configDirs
= FcStrSetCreate ();
53 if (!config
->configDirs
)
56 config
->configFiles
= FcStrSetCreate ();
57 if (!config
->configFiles
)
60 config
->fontDirs
= FcStrSetCreate ();
61 if (!config
->fontDirs
)
64 config
->acceptGlobs
= FcStrSetCreate ();
65 if (!config
->acceptGlobs
)
68 config
->rejectGlobs
= FcStrSetCreate ();
69 if (!config
->rejectGlobs
)
72 config
->acceptPatterns
= FcFontSetCreate ();
73 if (!config
->acceptPatterns
)
76 config
->rejectPatterns
= FcFontSetCreate ();
77 if (!config
->rejectPatterns
)
82 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
86 if (config
->cache
== 0)
88 /* If no home, use the temp folder. */
90 int templen
= GetTempPath (1, dummy
);
91 FcChar8
*temp
= malloc (templen
+ 1);
97 GetTempPath (templen
+ 1, temp
);
98 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
100 if (!FcConfigSetCache (config
, cache_dir
))
102 FcStrFree (cache_dir
);
105 FcStrFree (cache_dir
);
112 config
->substPattern
= 0;
113 config
->substFont
= 0;
114 config
->maxObjects
= 0;
115 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
116 config
->fonts
[set
] = 0;
118 config
->rescanTime
= time(0);
119 config
->rescanInterval
= 30;
124 FcFontSetDestroy (config
->rejectPatterns
);
126 FcFontSetDestroy (config
->acceptPatterns
);
128 FcStrSetDestroy (config
->rejectGlobs
);
130 FcStrSetDestroy (config
->acceptGlobs
);
132 FcStrSetDestroy (config
->fontDirs
);
134 FcStrSetDestroy (config
->configFiles
);
136 FcStrSetDestroy (config
->configDirs
);
139 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
145 FcConfigNewestFile (FcStrSet
*files
)
147 FcStrList
*list
= FcStrListCreate (files
);
148 FcFileTime newest
= { 0, FcFalse
};
154 while ((file
= FcStrListNext (list
)))
155 if (stat ((char *) file
, &statb
) == 0)
156 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
159 newest
.time
= statb
.st_mtime
;
161 FcStrListDone (list
);
167 FcConfigModifiedTime (FcConfig
*config
)
171 FcFileTime v
= { 0, FcFalse
};
172 config
= FcConfigGetCurrent ();
176 return FcConfigNewestFile (config
->configFiles
);
180 FcConfigUptoDate (FcConfig
*config
)
182 FcFileTime config_time
, font_time
;
183 time_t now
= time(0);
186 config
= FcConfigGetCurrent ();
190 config_time
= FcConfigNewestFile (config
->configFiles
);
191 font_time
= FcConfigNewestFile (config
->fontDirs
);
192 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
193 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
197 config
->rescanTime
= now
;
202 FcSubstDestroy (FcSubst
*s
)
210 FcTestDestroy (s
->test
);
212 FcEditDestroy (s
->edit
);
214 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
220 FcConfigDestroy (FcConfig
*config
)
224 if (config
== _fcConfig
)
227 FcStrSetDestroy (config
->configDirs
);
228 FcStrSetDestroy (config
->fontDirs
);
229 FcStrSetDestroy (config
->configFiles
);
230 FcStrSetDestroy (config
->acceptGlobs
);
231 FcStrSetDestroy (config
->rejectGlobs
);
232 FcFontSetDestroy (config
->acceptPatterns
);
233 FcFontSetDestroy (config
->rejectPatterns
);
236 FcBlanksDestroy (config
->blanks
);
239 FcStrFree (config
->cache
);
241 FcSubstDestroy (config
->substPattern
);
242 FcSubstDestroy (config
->substFont
);
243 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
244 if (config
->fonts
[set
])
245 FcFontSetDestroy (config
->fonts
[set
]);
248 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
252 * Scan the current list of directories in the configuration
253 * and build the set of available fonts. Update the
254 * per-user cache file to reflect the new configuration
258 FcConfigBuildFonts (FcConfig
*config
)
260 FcFontSet
*fonts
, *cached_fonts
;
261 FcGlobalCache
*cache
;
266 fonts
= FcFontSetCreate ();
270 cache
= FcGlobalCacheCreate ();
274 oldDirs
= FcStrSetCreate ();
279 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
, config
);
281 cached_fonts
= FcCacheRead(config
, cache
);
284 list
= FcConfigGetFontDirs (config
);
288 while ((dir
= FcStrListNext (list
)))
290 if (FcDebug () & FC_DBG_FONTSET
)
291 printf ("build scan dir %s\n", dir
);
292 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
293 config
->blanks
, dir
, FcFalse
, config
);
296 FcStrListDone (list
);
302 for (i
= 0; i
< oldDirs
->num
; i
++)
304 if (FcDebug () & FC_DBG_FONTSET
)
305 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
306 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
307 config
->blanks
, oldDirs
->strs
[i
],
311 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
314 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
316 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
317 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
318 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
320 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
322 cached_fonts
->nfont
= 0;
323 FcFontSetDestroy (cached_fonts
);
326 if (FcDebug () & FC_DBG_FONTSET
)
327 FcFontSetPrint (fonts
);
330 FcGlobalCacheSave (cache
, config
->cache
, config
);
331 FcGlobalCacheDestroy (cache
);
332 FcStrSetDestroy (oldDirs
);
334 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
338 FcGlobalCacheDestroy (cache
);
339 FcStrSetDestroy (oldDirs
);
341 FcFontSetDestroy (fonts
);
347 FcConfigSetCurrent (FcConfig
*config
)
350 if (!FcConfigBuildFonts (config
))
354 FcConfigDestroy (_fcConfig
);
360 FcConfigGetCurrent (void)
369 FcConfigAddConfigDir (FcConfig
*config
,
372 return FcStrSetAddFilename (config
->configDirs
, d
);
376 FcConfigGetConfigDirs (FcConfig
*config
)
380 config
= FcConfigGetCurrent ();
384 return FcStrListCreate (config
->configDirs
);
388 FcConfigInodeMatchFontDir (FcConfig
*config
, const FcChar8
*d
)
395 /* first we do string matches rather than file accesses */
396 /* FcStrSetMember doesn't tell us the index so that we can return
397 * the config-owned copy. */
398 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
400 if (!FcStrCmp (config
->fontDirs
->strs
[n
], d
))
401 return config
->fontDirs
->strs
[n
];
404 /* If this is a bottleneck, we can cache the fontDir inodes. */
405 if (stat ((char *)d
, &s
) == -1)
407 di
= s
.st_ino
; dd
= s
.st_dev
;
409 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
411 if (stat ((char *)config
->fontDirs
->strs
[n
], &s
) == -1)
413 if (di
== s
.st_ino
&& dd
== s
.st_dev
)
414 return config
->fontDirs
->strs
[n
];
420 FcConfigAddFontDir (FcConfig
*config
,
423 /* Avoid adding d if it's an alias of something else, too. */
424 if (FcConfigInodeMatchFontDir(config
, d
))
426 return FcStrSetAddFilename (config
->fontDirs
, d
);
430 FcConfigAddFontDirSubdirs (FcConfig
*config
,
436 FcBool added
= FcFalse
;
438 if (!(dir
= opendir ((char *) d
)))
440 if (!(subdir
= (FcChar8
*) malloc (strlen ((char *) d
) + FC_MAX_FILE_LEN
+ 2)))
442 fprintf (stderr
, "out of memory");
445 while ((e
= readdir (dir
)))
447 if (strcmp (e
->d_name
, ".") && strcmp (e
->d_name
, "..") &&
448 strlen (e
->d_name
) < FC_MAX_FILE_LEN
)
450 strcpy ((char *)subdir
, (char *)d
);
451 strcat ((char *)subdir
, "/");
452 strcat ((char *)subdir
, e
->d_name
);
453 if (FcFileIsDir (subdir
))
455 if (FcConfigInodeMatchFontDir(config
, subdir
))
456 continue; /* already added */
457 FcStrSetAddFilename (config
->fontDirs
, subdir
);
458 FcConfigAddFontDirSubdirs (config
, subdir
);
469 FcConfigNormalizeFontDir (FcConfig
*config
,
474 FcBool added
= FcFalse
;
476 d0
= FcConfigInodeMatchFontDir(config
, d
);
480 /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
481 for (n
= 0, n0
= config
->fontDirs
->num
; n
< n0
; n
++)
483 if (FcConfigAddFontDirSubdirs (config
, config
->fontDirs
->strs
[n
]))
487 /* ... and try again. */
489 return FcConfigInodeMatchFontDir(config
, d
);
495 FcConfigAddDir (FcConfig
*config
,
498 return (FcConfigAddConfigDir (config
, d
) &&
499 FcConfigAddFontDir (config
, d
));
503 FcConfigGetFontDirs (FcConfig
*config
)
507 config
= FcConfigGetCurrent ();
511 return FcStrListCreate (config
->fontDirs
);
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 FcConfigSetCache (FcConfig
*config
,
545 FcChar8
*new = FcStrCopyFilename (c
);
550 FcStrFree (config
->cache
);
556 FcConfigGetCache (FcConfig
*config
)
560 config
= FcConfigGetCurrent ();
564 return config
->cache
;
568 FcConfigGetFonts (FcConfig
*config
,
573 config
= FcConfigGetCurrent ();
577 return config
->fonts
[set
];
581 FcConfigSetFonts (FcConfig
*config
,
585 if (config
->fonts
[set
])
586 FcFontSetDestroy (config
->fonts
[set
]);
587 config
->fonts
[set
] = fonts
;
593 FcConfigGetBlanks (FcConfig
*config
)
597 config
= FcConfigGetCurrent ();
601 return config
->blanks
;
605 FcConfigAddBlank (FcConfig
*config
,
613 b
= FcBlanksCreate ();
617 if (!FcBlanksAdd (b
, blank
))
624 FcConfigGetRescanInverval (FcConfig
*config
)
628 config
= FcConfigGetCurrent ();
632 return config
->rescanInterval
;
636 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
640 config
= FcConfigGetCurrent ();
644 config
->rescanInterval
= rescanInterval
;
649 FcConfigAddEdit (FcConfig
*config
,
654 FcSubst
*subst
, **prev
;
658 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
661 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
662 if (kind
== FcMatchPattern
)
663 prev
= &config
->substPattern
;
665 prev
= &config
->substFont
;
666 for (; *prev
; prev
= &(*prev
)->next
);
672 for (t
= test
; t
; t
= t
->next
)
674 if (t
->kind
== FcMatchDefault
)
678 if (config
->maxObjects
< num
)
679 config
->maxObjects
= num
;
680 if (FcDebug () & FC_DBG_EDIT
)
682 printf ("Add Subst ");
683 FcSubstPrint (subst
);
688 typedef struct _FcSubState
{
694 FcConfigPromote (FcValue v
, FcValue u
)
696 if (v
.type
== FcTypeInteger
)
698 v
.type
= FcTypeDouble
;
699 v
.u
.d
= (double) v
.u
.i
;
701 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
703 v
.u
.m
= &FcIdentityMatrix
;
704 v
.type
= FcTypeMatrix
;
706 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
708 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
709 v
.type
= FcTypeLangSet
;
715 FcConfigCompareValue (const FcValue
*left_o
,
717 const FcValue
*right_o
)
719 FcValue left
= FcValueCanonicalize(left_o
);
720 FcValue right
= FcValueCanonicalize(right_o
);
721 FcBool ret
= FcFalse
;
723 left
= FcConfigPromote (left
, right
);
724 right
= FcConfigPromote (right
, left
);
725 if (left
.type
== right
.type
)
729 break; /* FcConfigPromote prevents this from happening */
735 ret
= left
.u
.d
== right
.u
.d
;
738 case FcOpNotContains
:
739 ret
= left
.u
.d
!= right
.u
.d
;
742 ret
= left
.u
.d
< right
.u
.d
;
745 ret
= left
.u
.d
<= right
.u
.d
;
748 ret
= left
.u
.d
> right
.u
.d
;
751 ret
= left
.u
.d
>= right
.u
.d
;
762 ret
= left
.u
.b
== right
.u
.b
;
765 case FcOpNotContains
:
766 ret
= left
.u
.b
!= right
.u
.b
;
776 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
779 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
782 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
784 case FcOpNotContains
:
785 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
796 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
799 case FcOpNotContains
:
800 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
810 /* left contains right if right is a subset of left */
811 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
813 case FcOpNotContains
:
814 /* left contains right if right is a subset of left */
815 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
818 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
821 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
831 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
833 case FcOpNotContains
:
834 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
837 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
840 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
862 ret
= left
.u
.f
== right
.u
.f
;
865 case FcOpNotContains
:
866 ret
= left
.u
.f
!= right
.u
.f
;
876 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
883 #define _FcDoubleFloor(d) ((int) (d))
884 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
885 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
886 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
887 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
888 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
891 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
900 v
.type
= FcTypeInteger
;
904 v
.type
= FcTypeDouble
;
908 v
.type
= FcTypeString
;
909 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
912 v
.type
= FcTypeMatrix
;
917 v
.type
= FcTypeCharSet
;
926 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
927 if (r
!= FcResultMatch
)
932 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
933 v
.type
= FcTypeInteger
;
938 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
939 if (vl
.type
== FcTypeBool
)
942 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
944 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
957 case FcOpNotContains
:
959 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
960 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
962 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
972 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
973 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
974 vl
= FcConfigPromote (vl
, vr
);
975 vr
= FcConfigPromote (vr
, vl
);
976 if (vl
.type
== vr
.type
)
982 v
.type
= FcTypeDouble
;
983 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
986 v
.type
= FcTypeDouble
;
987 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
990 v
.type
= FcTypeDouble
;
991 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
994 v
.type
= FcTypeDouble
;
995 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
1001 if (v
.type
== FcTypeDouble
&&
1002 v
.u
.d
== (double) (int) v
.u
.d
)
1004 v
.type
= FcTypeInteger
;
1005 v
.u
.i
= (int) v
.u
.d
;
1011 v
.type
= FcTypeBool
;
1012 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
1015 v
.type
= FcTypeBool
;
1016 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
1019 v
.type
= FcTypeVoid
;
1026 v
.type
= FcTypeString
;
1027 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
1028 v
.u
.s
= FcStrStaticName (str
);
1032 v
.type
= FcTypeVoid
;
1035 v
.type
= FcTypeVoid
;
1042 v
.type
= FcTypeMatrix
;
1043 m
= malloc (sizeof (FcMatrix
));
1046 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
1047 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1052 v
.type
= FcTypeVoid
;
1056 v
.type
= FcTypeVoid
;
1061 v
.type
= FcTypeVoid
;
1066 v
.type
= FcTypeVoid
;
1067 FcValueDestroy (vl
);
1068 FcValueDestroy (vr
);
1071 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1074 v
.type
= FcTypeBool
;
1078 v
.type
= FcTypeVoid
;
1081 FcValueDestroy (vl
);
1084 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1090 v
.type
= FcTypeInteger
;
1091 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1094 v
.type
= FcTypeVoid
;
1097 FcValueDestroy (vl
);
1100 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1106 v
.type
= FcTypeInteger
;
1107 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1110 v
.type
= FcTypeVoid
;
1113 FcValueDestroy (vl
);
1116 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1122 v
.type
= FcTypeInteger
;
1123 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1126 v
.type
= FcTypeVoid
;
1129 FcValueDestroy (vl
);
1132 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1138 v
.type
= FcTypeInteger
;
1139 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1142 v
.type
= FcTypeVoid
;
1145 FcValueDestroy (vl
);
1148 v
.type
= FcTypeVoid
;
1154 static FcValueList
*
1155 FcConfigMatchValueList (FcPattern
*p
,
1157 FcValueList
*values
)
1159 FcValueList
*ret
= 0;
1160 FcExpr
*e
= t
->expr
;
1166 /* Compute the value of the match expression */
1167 if (e
->op
== FcOpComma
)
1169 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1170 e
= e
->u
.tree
.right
;
1174 value
= FcConfigEvaluate (p
, e
);
1178 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1180 /* Compare the pattern value to the match expression value */
1181 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1188 if (t
->qual
== FcQualAll
)
1195 FcValueDestroy (value
);
1200 static FcValueList
*
1201 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1207 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1210 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1211 if (e
->op
== FcOpComma
)
1213 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1214 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1218 l
->value
= FcConfigEvaluate (p
, e
);
1219 l
->next
= FcValueListPtrCreateDynamic(0);
1221 l
->binding
= binding
;
1222 if (l
->value
.type
== FcTypeVoid
)
1224 FcValueList
*next
= FcValueListPtrU(l
->next
);
1226 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1235 FcConfigAdd (FcValueListPtr
*head
,
1236 FcValueList
*position
,
1240 FcValueListPtr
*prev
, last
, v
;
1241 FcValueBinding sameBinding
;
1244 sameBinding
= position
->binding
;
1246 sameBinding
= FcValueBindingWeak
;
1247 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1248 v
= FcValueListPtrU(v
)->next
)
1249 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1250 FcValueListPtrU(v
)->binding
= sameBinding
;
1254 prev
= &position
->next
;
1256 for (prev
= head
; FcValueListPtrU(*prev
);
1257 prev
= &(FcValueListPtrU(*prev
)->next
))
1264 for (prev
= head
; FcValueListPtrU(*prev
);
1265 prev
= &(FcValueListPtrU(*prev
)->next
))
1267 if (FcValueListPtrU(*prev
) == position
)
1274 if (FcDebug () & FC_DBG_EDIT
)
1276 if (!FcValueListPtrU(*prev
))
1277 printf ("position not on list\n");
1281 if (FcDebug () & FC_DBG_EDIT
)
1283 printf ("%s list before ", append
? "Append" : "Prepend");
1284 FcValueListPrint (*head
);
1290 last
= FcValueListPtrCreateDynamic(new);
1291 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1292 last
= FcValueListPtrU(last
)->next
;
1294 FcValueListPtrU(last
)->next
= *prev
;
1295 *prev
= FcValueListPtrCreateDynamic(new);
1298 if (FcDebug () & FC_DBG_EDIT
)
1300 printf ("%s list after ", append
? "Append" : "Prepend");
1301 FcValueListPrint (*head
);
1309 FcConfigDel (FcValueListPtr
*head
,
1310 FcValueList
*position
)
1312 FcValueListPtr
*prev
;
1314 for (prev
= head
; FcValueListPtrU(*prev
);
1315 prev
= &(FcValueListPtrU(*prev
)->next
))
1317 if (FcValueListPtrU(*prev
) == position
)
1319 *prev
= position
->next
;
1320 position
->next
= FcValueListPtrCreateDynamic(0);
1321 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1328 FcConfigPatternAdd (FcPattern
*p
,
1335 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1339 FcConfigAdd (&e
->values
, 0, append
, list
);
1344 * Delete all values associated with a field
1347 FcConfigPatternDel (FcPattern
*p
,
1350 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1353 while (FcValueListPtrU(e
->values
))
1354 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1358 FcConfigPatternCanon (FcPattern
*p
,
1361 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1364 if (!FcValueListPtrU(e
->values
))
1365 FcPatternDel (p
, object
);
1369 FcConfigSubstituteWithPat (FcConfig
*config
,
1384 config
= FcConfigGetCurrent ();
1389 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1390 if (!st
&& config
->maxObjects
)
1392 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1394 if (FcDebug () & FC_DBG_EDIT
)
1396 printf ("FcConfigSubstitute ");
1399 if (kind
== FcMatchPattern
)
1400 s
= config
->substPattern
;
1402 s
= config
->substFont
;
1403 for (; s
; s
= s
->next
)
1406 * Check the tests to see if
1407 * they all match the pattern
1409 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1411 if (FcDebug () & FC_DBG_EDIT
)
1413 printf ("FcConfigSubstitute test ");
1417 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1422 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1426 * If there's no such field in the font,
1427 * then FcQualAll matches while FcQualAny does not
1431 if (t
->qual
== FcQualAll
)
1440 * Check to see if there is a match, mark the location
1441 * to apply match-relative edits
1443 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1446 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1448 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1453 if (FcDebug () & FC_DBG_EDIT
)
1454 printf ("No match\n");
1457 if (FcDebug () & FC_DBG_EDIT
)
1459 printf ("Substitute ");
1462 for (e
= s
->edit
; e
; e
= e
->next
)
1465 * Evaluate the list of expressions
1467 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1469 * Locate any test associated with this field, skipping
1470 * tests associated with the pattern when substituting in
1473 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1475 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1476 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1477 (FcChar8
*) e
->field
))
1480 * KLUDGE - the pattern may have been reallocated or
1481 * things may have been inserted or deleted above
1482 * this element by other edits. Go back and find
1485 if (e
!= s
->edit
&& st
[i
].elt
)
1486 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1495 * If there was a test, then replace the matched
1496 * value with the new list of values
1500 FcValueList
*thisValue
= st
[i
].value
;
1501 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1504 * Append the new list of values after the current value
1506 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1508 * Delete the marked value
1510 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1512 * Adjust any pointers into the value list to ensure
1513 * future edits occur at the same place
1515 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1517 if (st
[i
].value
== thisValue
)
1518 st
[i
].value
= nextValue
;
1522 /* fall through ... */
1523 case FcOpAssignReplace
:
1525 * Delete all of the values and insert
1528 FcConfigPatternDel (p
, e
->field
);
1529 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1531 * Adjust any pointers into the value list as they no
1532 * longer point to anything valid
1536 FcPatternElt
*thisElt
= st
[i
].elt
;
1537 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1539 if (st
[i
].elt
== thisElt
)
1547 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1550 /* fall through ... */
1551 case FcOpPrependFirst
:
1552 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1557 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1560 /* fall through ... */
1561 case FcOpAppendLast
:
1562 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1569 * Now go through the pattern and eliminate
1570 * any properties without data
1572 for (e
= s
->edit
; e
; e
= e
->next
)
1573 FcConfigPatternCanon (p
, e
->field
);
1575 if (FcDebug () & FC_DBG_EDIT
)
1577 printf ("FcConfigSubstitute edit");
1581 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1583 if (FcDebug () & FC_DBG_EDIT
)
1585 printf ("FcConfigSubstitute done");
1592 FcConfigSubstitute (FcConfig
*config
,
1596 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1599 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1601 static FcChar8 fontconfig_path
[1000] = "";
1604 DllMain (HINSTANCE hinstDLL
,
1610 switch (fdwReason
) {
1611 case DLL_PROCESS_ATTACH
:
1612 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1613 sizeof (fontconfig_path
)))
1616 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1617 * assume it's a Unix-style installation tree, and use
1618 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1619 * folder where the DLL is as FONTCONFIG_PATH.
1621 p
= strrchr (fontconfig_path
, '\\');
1625 p
= strrchr (fontconfig_path
, '\\');
1626 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1627 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1629 strcat (fontconfig_path
, "\\etc\\fonts");
1632 fontconfig_path
[0] = '\0';
1640 #undef FONTCONFIG_PATH
1641 #define FONTCONFIG_PATH fontconfig_path
1643 #else /* !(_WIN32 && PIC) */
1645 #endif /* !(_WIN32 && PIC) */
1647 #ifndef FONTCONFIG_FILE
1648 #define FONTCONFIG_FILE "fonts.conf"
1652 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1657 dir
= (FcChar8
*) "";
1658 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1662 strcpy ((char *) path
, (const char *) dir
);
1663 /* make sure there's a single separator */
1665 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1666 path
[strlen((char *) path
)-1] != '\\')) &&
1669 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1670 strcat ((char *) path
, "\\");
1672 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1673 strcat ((char *) path
, "/");
1675 strcat ((char *) path
, (char *) file
);
1677 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1678 if (access ((char *) path
, R_OK
) == 0)
1686 FcConfigGetPath (void)
1689 FcChar8
*env
, *e
, *colon
;
1694 npath
= 2; /* default dir + null */
1695 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1701 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1704 path
= calloc (npath
, sizeof (FcChar8
*));
1714 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1716 colon
= e
+ strlen ((char *) e
);
1717 path
[i
] = malloc (colon
- e
+ 1);
1720 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1721 path
[i
][colon
- e
] = '\0';
1730 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1731 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1734 strcpy ((char *) path
[i
], (const char *) dir
);
1738 for (i
= 0; path
[i
]; i
++)
1746 FcConfigFreePath (FcChar8
**path
)
1750 for (p
= path
; *p
; p
++)
1755 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1760 if (_FcConfigHomeEnabled
)
1762 char *home
= getenv ("HOME");
1766 home
= getenv ("USERPROFILE");
1769 return (FcChar8
*) home
;
1775 FcConfigEnableHome (FcBool enable
)
1777 FcBool prev
= _FcConfigHomeEnabled
;
1778 _FcConfigHomeEnabled
= enable
;
1783 FcConfigFilename (const FcChar8
*url
)
1785 FcChar8
*file
, *dir
, **path
, **p
;
1789 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1791 url
= (FcChar8
*) FONTCONFIG_FILE
;
1796 if (isalpha (*url
) &&
1798 (url
[2] == '/' || url
[2] == '\\'))
1804 dir
= FcConfigHome ();
1806 file
= FcConfigFileExists (dir
, url
+ 1);
1815 file
= FcConfigFileExists (0, url
);
1818 path
= FcConfigGetPath ();
1821 for (p
= path
; *p
; p
++)
1823 file
= FcConfigFileExists (*p
, url
);
1827 FcConfigFreePath (path
);
1834 * Manage the application-specific fonts
1838 FcConfigAppFontAddFile (FcConfig
*config
,
1839 const FcChar8
*file
)
1848 config
= FcConfigGetCurrent ();
1853 subdirs
= FcStrSetCreate ();
1857 set
= FcConfigGetFonts (config
, FcSetApplication
);
1860 set
= FcFontSetCreate ();
1863 FcStrSetDestroy (subdirs
);
1866 FcConfigSetFonts (config
, set
, FcSetApplication
);
1869 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1871 FcStrSetDestroy (subdirs
);
1874 if ((sublist
= FcStrListCreate (subdirs
)))
1876 while ((subdir
= FcStrListNext (sublist
)))
1878 FcConfigAppFontAddDir (config
, subdir
);
1880 FcStrListDone (sublist
);
1882 FcStrSetDestroy (subdirs
);
1887 FcConfigAppFontAddDir (FcConfig
*config
,
1897 config
= FcConfigGetCurrent ();
1901 subdirs
= FcStrSetCreate ();
1905 set
= FcConfigGetFonts (config
, FcSetApplication
);
1908 set
= FcFontSetCreate ();
1911 FcStrSetDestroy (subdirs
);
1914 FcConfigSetFonts (config
, set
, FcSetApplication
);
1917 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1919 FcStrSetDestroy (subdirs
);
1922 if ((sublist
= FcStrListCreate (subdirs
)))
1924 while ((subdir
= FcStrListNext (sublist
)))
1926 FcConfigAppFontAddDir (config
, subdir
);
1928 FcStrListDone (sublist
);
1930 FcStrSetDestroy (subdirs
);
1935 FcConfigAppFontClear (FcConfig
*config
)
1939 config
= FcConfigGetCurrent ();
1944 FcConfigSetFonts (config
, 0, FcSetApplication
);
1948 * Manage filename-based font source selectors
1952 FcConfigGlobAdd (FcConfig
*config
,
1953 const FcChar8
*glob
,
1956 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1958 return FcStrSetAdd (set
, glob
);
1962 FcConfigGlobMatch (const FcChar8
*glob
,
1963 const FcChar8
*string
)
1967 while ((c
= *glob
++))
1971 /* short circuit common case */
1974 /* short circuit another common case */
1975 if (strchr ((char *) glob
, '*') == 0)
1976 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1979 if (FcConfigGlobMatch (glob
, string
))
1985 if (*string
++ == '\0')
1994 return *string
== '\0';
1998 FcConfigGlobsMatch (const FcStrSet
*globs
,
1999 const FcChar8
*string
)
2003 for (i
= 0; i
< globs
->num
; i
++)
2004 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
2010 FcConfigAcceptFilename (FcConfig
*config
,
2011 const FcChar8
*filename
)
2013 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
2015 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
2021 * Manage font-pattern based font source selectors
2025 FcConfigPatternsAdd (FcConfig
*config
,
2029 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
2031 return FcFontSetAdd (set
, pattern
);
2035 FcConfigPatternsMatch (const FcFontSet
*patterns
,
2036 const FcPattern
*font
)
2040 for (i
= 0; i
< patterns
->nfont
; i
++)
2041 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
2047 FcConfigAcceptFont (FcConfig
*config
,
2048 const FcPattern
*font
)
2050 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
2052 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))