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.
27 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config
= malloc (sizeof (FcConfig
));
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
)
80 config
->cacheDirs
= FcStrSetCreate ();
81 if (!config
->cacheDirs
)
86 config
->substPattern
= 0;
87 config
->substFont
= 0;
88 config
->substScan
= 0;
89 config
->maxObjects
= 0;
90 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
91 config
->fonts
[set
] = 0;
93 config
->caches
= NULL
;
95 config
->rescanTime
= time(0);
96 config
->rescanInterval
= 30;
101 FcFontSetDestroy (config
->rejectPatterns
);
103 FcFontSetDestroy (config
->acceptPatterns
);
105 FcStrSetDestroy (config
->rejectGlobs
);
107 FcStrSetDestroy (config
->acceptGlobs
);
109 FcStrSetDestroy (config
->fontDirs
);
111 FcStrSetDestroy (config
->configFiles
);
113 FcStrSetDestroy (config
->configDirs
);
116 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
122 FcConfigNewestFile (FcStrSet
*files
)
124 FcStrList
*list
= FcStrListCreate (files
);
125 FcFileTime newest
= { 0, FcFalse
};
131 while ((file
= FcStrListNext (list
)))
132 if (stat ((char *) file
, &statb
) == 0)
133 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
136 newest
.time
= statb
.st_mtime
;
138 FcStrListDone (list
);
144 FcConfigModifiedTime (FcConfig
*config
)
148 FcFileTime v
= { 0, FcFalse
};
149 config
= FcConfigGetCurrent ();
153 return FcConfigNewestFile (config
->configFiles
);
157 FcConfigUptoDate (FcConfig
*config
)
159 FcFileTime config_time
, font_time
;
160 time_t now
= time(0);
163 config
= FcConfigGetCurrent ();
167 config_time
= FcConfigNewestFile (config
->configFiles
);
168 font_time
= FcConfigNewestFile (config
->fontDirs
);
169 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
170 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
174 config
->rescanTime
= now
;
179 FcSubstDestroy (FcSubst
*s
)
187 FcTestDestroy (s
->test
);
189 FcEditDestroy (s
->edit
);
191 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
197 FcConfigDestroy (FcConfig
*config
)
200 FcCacheList
*cl
, *cl_next
;
202 if (config
== _fcConfig
)
205 FcStrSetDestroy (config
->configDirs
);
206 FcStrSetDestroy (config
->fontDirs
);
207 FcStrSetDestroy (config
->cacheDirs
);
208 FcStrSetDestroy (config
->configFiles
);
209 FcStrSetDestroy (config
->acceptGlobs
);
210 FcStrSetDestroy (config
->rejectGlobs
);
211 FcFontSetDestroy (config
->acceptPatterns
);
212 FcFontSetDestroy (config
->rejectPatterns
);
215 FcBlanksDestroy (config
->blanks
);
217 FcSubstDestroy (config
->substPattern
);
218 FcSubstDestroy (config
->substFont
);
219 FcSubstDestroy (config
->substScan
);
220 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
221 if (config
->fonts
[set
])
222 FcFontSetDestroy (config
->fonts
[set
]);
224 for (cl
= config
->caches
; cl
; cl
= cl_next
)
227 FcDirCacheUnload (cl
->cache
);
232 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
236 * Add cache to configuration, adding fonts and directories
240 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
)
242 FcCacheList
*cl
= malloc (sizeof (FcCacheList
));
253 cl
->next
= config
->caches
;
259 fs
= FcCacheSet (cache
);
262 for (i
= 0; i
< fs
->nfont
; i
++)
264 FcPattern
*font
= FcFontSetFont (fs
, i
);
268 * Check to see if font is banned by filename
270 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
271 0, &font_file
) == FcResultMatch
&&
272 !FcConfigAcceptFilename (config
, font_file
))
278 * Check to see if font is banned by pattern
280 if (!FcConfigAcceptFont (config
, font
))
283 FcFontSetAdd (config
->fonts
[FcSetSystem
], font
);
290 dirs
= FcCacheDirs (cache
);
293 for (i
= 0; i
< cache
->dirs_count
; i
++)
295 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
296 if (FcConfigAcceptFilename (config
, dir
))
297 FcConfigAddFontDir (config
, dir
);
304 * Scan the current list of directories in the configuration
305 * and build the set of available fonts.
309 FcConfigBuildFonts (FcConfig
*config
)
318 config
= FcConfigGetCurrent ();
323 fonts
= FcFontSetCreate ();
327 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
329 dirlist
= FcStrListCreate (config
->fontDirs
);
333 while ((dir
= FcStrListNext (dirlist
)))
335 if (FcDebug () & FC_DBG_FONTSET
)
336 printf ("adding fonts from%s\n", dir
);
337 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
340 FcConfigAddCache (config
, cache
);
343 FcStrListDone (dirlist
);
345 if (FcDebug () & FC_DBG_FONTSET
)
346 FcFontSetPrint (fonts
);
354 FcConfigSetCurrent (FcConfig
*config
)
357 if (!FcConfigBuildFonts (config
))
361 FcConfigDestroy (_fcConfig
);
367 FcConfigGetCurrent (void)
376 FcConfigAddConfigDir (FcConfig
*config
,
379 return FcStrSetAddFilename (config
->configDirs
, d
);
383 FcConfigGetConfigDirs (FcConfig
*config
)
387 config
= FcConfigGetCurrent ();
391 return FcStrListCreate (config
->configDirs
);
395 FcConfigAddFontDir (FcConfig
*config
,
398 return FcStrSetAddFilename (config
->fontDirs
, d
);
402 FcConfigAddDir (FcConfig
*config
,
405 return (FcConfigAddConfigDir (config
, d
) &&
406 FcConfigAddFontDir (config
, d
));
410 FcConfigGetFontDirs (FcConfig
*config
)
414 config
= FcConfigGetCurrent ();
418 return FcStrListCreate (config
->fontDirs
);
422 FcConfigAddCacheDir (FcConfig
*config
,
425 return FcStrSetAddFilename (config
->cacheDirs
, d
);
429 FcConfigGetCacheDirs (FcConfig
*config
)
433 config
= FcConfigGetCurrent ();
437 return FcStrListCreate (config
->cacheDirs
);
441 FcConfigAddConfigFile (FcConfig
*config
,
445 FcChar8
*file
= FcConfigFilename (f
);
450 ret
= FcStrSetAdd (config
->configFiles
, file
);
456 FcConfigGetConfigFiles (FcConfig
*config
)
460 config
= FcConfigGetCurrent ();
464 return FcStrListCreate (config
->configFiles
);
468 FcConfigGetCache (FcConfig
*config
)
474 FcConfigGetFonts (FcConfig
*config
,
479 config
= FcConfigGetCurrent ();
483 return config
->fonts
[set
];
487 FcConfigSetFonts (FcConfig
*config
,
491 if (config
->fonts
[set
])
492 FcFontSetDestroy (config
->fonts
[set
]);
493 config
->fonts
[set
] = fonts
;
497 FcConfigGetBlanks (FcConfig
*config
)
501 config
= FcConfigGetCurrent ();
505 return config
->blanks
;
509 FcConfigAddBlank (FcConfig
*config
,
512 FcBlanks
*b
, *freeme
= 0;
517 freeme
= b
= FcBlanksCreate ();
521 if (!FcBlanksAdd (b
, blank
))
524 FcBlanksDestroy (freeme
);
532 FcConfigGetRescanInverval (FcConfig
*config
)
536 config
= FcConfigGetCurrent ();
540 return config
->rescanInterval
;
544 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
548 config
= FcConfigGetCurrent ();
552 config
->rescanInterval
= rescanInterval
;
557 FcConfigAddEdit (FcConfig
*config
,
562 FcSubst
*subst
, **prev
;
568 prev
= &config
->substPattern
;
571 prev
= &config
->substFont
;
574 prev
= &config
->substScan
;
579 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
582 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
583 for (; *prev
; prev
= &(*prev
)->next
);
589 for (t
= test
; t
; t
= t
->next
)
591 if (t
->kind
== FcMatchDefault
)
595 if (config
->maxObjects
< num
)
596 config
->maxObjects
= num
;
597 if (FcDebug () & FC_DBG_EDIT
)
599 printf ("Add Subst ");
600 FcSubstPrint (subst
);
605 typedef struct _FcSubState
{
611 FcConfigPromote (FcValue v
, FcValue u
)
613 if (v
.type
== FcTypeInteger
)
615 v
.type
= FcTypeDouble
;
616 v
.u
.d
= (double) v
.u
.i
;
618 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
620 v
.u
.m
= &FcIdentityMatrix
;
621 v
.type
= FcTypeMatrix
;
623 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
625 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
626 v
.type
= FcTypeLangSet
;
632 FcConfigCompareValue (const FcValue
*left_o
,
634 const FcValue
*right_o
)
636 FcValue left
= FcValueCanonicalize(left_o
);
637 FcValue right
= FcValueCanonicalize(right_o
);
638 FcBool ret
= FcFalse
;
640 left
= FcConfigPromote (left
, right
);
641 right
= FcConfigPromote (right
, left
);
642 if (left
.type
== right
.type
)
646 break; /* FcConfigPromote prevents this from happening */
652 ret
= left
.u
.d
== right
.u
.d
;
655 case FcOpNotContains
:
656 ret
= left
.u
.d
!= right
.u
.d
;
659 ret
= left
.u
.d
< right
.u
.d
;
662 ret
= left
.u
.d
<= right
.u
.d
;
665 ret
= left
.u
.d
> right
.u
.d
;
668 ret
= left
.u
.d
>= right
.u
.d
;
679 ret
= left
.u
.b
== right
.u
.b
;
682 case FcOpNotContains
:
683 ret
= left
.u
.b
!= right
.u
.b
;
693 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
696 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
699 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
701 case FcOpNotContains
:
702 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
713 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
716 case FcOpNotContains
:
717 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
727 /* left contains right if right is a subset of left */
728 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
730 case FcOpNotContains
:
731 /* left contains right if right is a subset of left */
732 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
735 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
738 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
748 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
750 case FcOpNotContains
:
751 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
754 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
757 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
779 ret
= left
.u
.f
== right
.u
.f
;
782 case FcOpNotContains
:
783 ret
= left
.u
.f
!= right
.u
.f
;
793 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
800 #define _FcDoubleFloor(d) ((int) (d))
801 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
802 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
803 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
804 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
805 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
808 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
817 v
.type
= FcTypeInteger
;
821 v
.type
= FcTypeDouble
;
825 v
.type
= FcTypeString
;
826 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
829 v
.type
= FcTypeMatrix
;
834 v
.type
= FcTypeCharSet
;
843 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
844 if (r
!= FcResultMatch
)
849 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
850 v
.type
= FcTypeInteger
;
855 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
856 if (vl
.type
== FcTypeBool
)
859 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
861 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
874 case FcOpNotContains
:
876 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
877 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
879 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
889 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
890 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
891 vl
= FcConfigPromote (vl
, vr
);
892 vr
= FcConfigPromote (vr
, vl
);
893 if (vl
.type
== vr
.type
)
899 v
.type
= FcTypeDouble
;
900 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
903 v
.type
= FcTypeDouble
;
904 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
907 v
.type
= FcTypeDouble
;
908 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
911 v
.type
= FcTypeDouble
;
912 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
918 if (v
.type
== FcTypeDouble
&&
919 v
.u
.d
== (double) (int) v
.u
.d
)
921 v
.type
= FcTypeInteger
;
929 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
933 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
943 v
.type
= FcTypeString
;
944 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
945 v
.u
.s
= FcStrStaticName (str
);
959 v
.type
= FcTypeMatrix
;
960 m
= malloc (sizeof (FcMatrix
));
963 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
964 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
988 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1001 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1007 v
.type
= FcTypeInteger
;
1008 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1011 v
.type
= FcTypeVoid
;
1014 FcValueDestroy (vl
);
1017 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1023 v
.type
= FcTypeInteger
;
1024 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1027 v
.type
= FcTypeVoid
;
1030 FcValueDestroy (vl
);
1033 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1039 v
.type
= FcTypeInteger
;
1040 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1043 v
.type
= FcTypeVoid
;
1046 FcValueDestroy (vl
);
1049 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1055 v
.type
= FcTypeInteger
;
1056 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1059 v
.type
= FcTypeVoid
;
1062 FcValueDestroy (vl
);
1065 v
.type
= FcTypeVoid
;
1071 static FcValueList
*
1072 FcConfigMatchValueList (FcPattern
*p
,
1074 FcValueList
*values
)
1076 FcValueList
*ret
= 0;
1077 FcExpr
*e
= t
->expr
;
1083 /* Compute the value of the match expression */
1084 if (e
->op
== FcOpComma
)
1086 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1087 e
= e
->u
.tree
.right
;
1091 value
= FcConfigEvaluate (p
, e
);
1095 for (v
= values
; v
; v
= FcValueListNext(v
))
1097 /* Compare the pattern value to the match expression value */
1098 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1105 if (t
->qual
== FcQualAll
)
1112 FcValueDestroy (value
);
1117 static FcValueList
*
1118 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1124 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1127 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1128 if (e
->op
== FcOpComma
)
1130 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1131 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1135 l
->value
= FcConfigEvaluate (p
, e
);
1138 l
->binding
= binding
;
1139 if (l
->value
.type
== FcTypeVoid
)
1141 FcValueList
*next
= FcValueListNext(l
);
1143 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1152 FcConfigAdd (FcValueListPtr
*head
,
1153 FcValueList
*position
,
1157 FcValueListPtr
*prev
, last
, v
;
1158 FcValueBinding sameBinding
;
1161 sameBinding
= position
->binding
;
1163 sameBinding
= FcValueBindingWeak
;
1164 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1165 if (v
->binding
== FcValueBindingSame
)
1166 v
->binding
= sameBinding
;
1170 prev
= &position
->next
;
1172 for (prev
= head
; *prev
!= NULL
;
1173 prev
= &(*prev
)->next
)
1180 for (prev
= head
; *prev
!= NULL
;
1181 prev
= &(*prev
)->next
)
1183 if (*prev
== position
)
1190 if (FcDebug () & FC_DBG_EDIT
)
1193 printf ("position not on list\n");
1197 if (FcDebug () & FC_DBG_EDIT
)
1199 printf ("%s list before ", append
? "Append" : "Prepend");
1200 FcValueListPrint (*head
);
1207 while (last
->next
!= NULL
)
1214 if (FcDebug () & FC_DBG_EDIT
)
1216 printf ("%s list after ", append
? "Append" : "Prepend");
1217 FcValueListPrint (*head
);
1225 FcConfigDel (FcValueListPtr
*head
,
1226 FcValueList
*position
)
1228 FcValueListPtr
*prev
;
1230 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1232 if (*prev
== position
)
1234 *prev
= position
->next
;
1235 position
->next
= NULL
;
1236 FcValueListDestroy (position
);
1243 FcConfigPatternAdd (FcPattern
*p
,
1250 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1254 FcConfigAdd (&e
->values
, 0, append
, list
);
1259 * Delete all values associated with a field
1262 FcConfigPatternDel (FcPattern
*p
,
1265 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1268 while (e
->values
!= NULL
)
1269 FcConfigDel (&e
->values
, e
->values
);
1273 FcConfigPatternCanon (FcPattern
*p
,
1276 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1279 if (e
->values
== NULL
)
1280 FcPatternObjectDel (p
, object
);
1284 FcConfigSubstituteWithPat (FcConfig
*config
,
1299 config
= FcConfigGetCurrent ();
1305 case FcMatchPattern
:
1306 s
= config
->substPattern
;
1309 s
= config
->substFont
;
1312 s
= config
->substScan
;
1318 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1319 if (!st
&& config
->maxObjects
)
1321 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1323 if (FcDebug () & FC_DBG_EDIT
)
1325 printf ("FcConfigSubstitute ");
1328 for (; s
; s
= s
->next
)
1331 * Check the tests to see if
1332 * they all match the pattern
1334 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1336 if (FcDebug () & FC_DBG_EDIT
)
1338 printf ("FcConfigSubstitute test ");
1342 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1347 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1351 * If there's no such field in the font,
1352 * then FcQualAll matches while FcQualAny does not
1356 if (t
->qual
== FcQualAll
)
1365 * Check to see if there is a match, mark the location
1366 * to apply match-relative edits
1368 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1371 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1373 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1378 if (FcDebug () & FC_DBG_EDIT
)
1379 printf ("No match\n");
1382 if (FcDebug () & FC_DBG_EDIT
)
1384 printf ("Substitute ");
1387 for (e
= s
->edit
; e
; e
= e
->next
)
1390 * Evaluate the list of expressions
1392 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1394 * Locate any test associated with this field, skipping
1395 * tests associated with the pattern when substituting in
1398 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1400 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1401 t
->object
== e
->object
)
1404 * KLUDGE - the pattern may have been reallocated or
1405 * things may have been inserted or deleted above
1406 * this element by other edits. Go back and find
1409 if (e
!= s
->edit
&& st
[i
].elt
)
1410 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1419 * If there was a test, then replace the matched
1420 * value with the new list of values
1424 FcValueList
*thisValue
= st
[i
].value
;
1425 FcValueList
*nextValue
= thisValue
;
1428 * Append the new list of values after the current value
1430 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1432 * Delete the marked value
1435 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1437 * Adjust any pointers into the value list to ensure
1438 * future edits occur at the same place
1440 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1442 if (st
[i
].value
== thisValue
)
1443 st
[i
].value
= nextValue
;
1447 /* fall through ... */
1448 case FcOpAssignReplace
:
1450 * Delete all of the values and insert
1453 FcConfigPatternDel (p
, e
->object
);
1454 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1456 * Adjust any pointers into the value list as they no
1457 * longer point to anything valid
1461 FcPatternElt
*thisElt
= st
[i
].elt
;
1462 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1464 if (st
[i
].elt
== thisElt
)
1472 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1475 /* fall through ... */
1476 case FcOpPrependFirst
:
1477 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1482 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1485 /* fall through ... */
1486 case FcOpAppendLast
:
1487 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1490 FcValueListDestroy (l
);
1495 * Now go through the pattern and eliminate
1496 * any properties without data
1498 for (e
= s
->edit
; e
; e
= e
->next
)
1499 FcConfigPatternCanon (p
, e
->object
);
1501 if (FcDebug () & FC_DBG_EDIT
)
1503 printf ("FcConfigSubstitute edit");
1507 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1509 if (FcDebug () & FC_DBG_EDIT
)
1511 printf ("FcConfigSubstitute done");
1518 FcConfigSubstitute (FcConfig
*config
,
1522 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1525 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1527 static FcChar8 fontconfig_path
[1000] = "";
1530 DllMain (HINSTANCE hinstDLL
,
1536 switch (fdwReason
) {
1537 case DLL_PROCESS_ATTACH
:
1538 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1539 sizeof (fontconfig_path
)))
1542 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1543 * assume it's a Unix-style installation tree, and use
1544 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1545 * folder where the DLL is as FONTCONFIG_PATH.
1547 p
= strrchr (fontconfig_path
, '\\');
1551 p
= strrchr (fontconfig_path
, '\\');
1552 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1553 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1555 strcat (fontconfig_path
, "\\etc\\fonts");
1558 fontconfig_path
[0] = '\0';
1566 #undef FONTCONFIG_PATH
1567 #define FONTCONFIG_PATH fontconfig_path
1569 #else /* !(_WIN32 && PIC) */
1571 #endif /* !(_WIN32 && PIC) */
1573 #ifndef FONTCONFIG_FILE
1574 #define FONTCONFIG_FILE "fonts.conf"
1578 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1583 dir
= (FcChar8
*) "";
1584 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1588 strcpy ((char *) path
, (const char *) dir
);
1589 /* make sure there's a single separator */
1591 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1592 path
[strlen((char *) path
)-1] != '\\')) &&
1595 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1596 strcat ((char *) path
, "\\");
1598 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1599 strcat ((char *) path
, "/");
1601 strcat ((char *) path
, (char *) file
);
1603 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1604 if (access ((char *) path
, R_OK
) == 0)
1612 FcConfigGetPath (void)
1615 FcChar8
*env
, *e
, *colon
;
1620 npath
= 2; /* default dir + null */
1621 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1627 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1630 path
= calloc (npath
, sizeof (FcChar8
*));
1640 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1642 colon
= e
+ strlen ((char *) e
);
1643 path
[i
] = malloc (colon
- e
+ 1);
1646 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1647 path
[i
][colon
- e
] = '\0';
1656 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1657 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1660 strcpy ((char *) path
[i
], (const char *) dir
);
1664 for (i
= 0; path
[i
]; i
++)
1672 FcConfigFreePath (FcChar8
**path
)
1676 for (p
= path
; *p
; p
++)
1681 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1686 if (_FcConfigHomeEnabled
)
1688 char *home
= getenv ("HOME");
1692 home
= getenv ("USERPROFILE");
1695 return (FcChar8
*) home
;
1701 FcConfigEnableHome (FcBool enable
)
1703 FcBool prev
= _FcConfigHomeEnabled
;
1704 _FcConfigHomeEnabled
= enable
;
1709 FcConfigFilename (const FcChar8
*url
)
1711 FcChar8
*file
, *dir
, **path
, **p
;
1715 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1717 url
= (FcChar8
*) FONTCONFIG_FILE
;
1722 if (isalpha (*url
) &&
1724 (url
[2] == '/' || url
[2] == '\\'))
1730 dir
= FcConfigHome ();
1732 file
= FcConfigFileExists (dir
, url
+ 1);
1741 file
= FcConfigFileExists (0, url
);
1744 path
= FcConfigGetPath ();
1747 for (p
= path
; *p
; p
++)
1749 file
= FcConfigFileExists (*p
, url
);
1753 FcConfigFreePath (path
);
1760 * Manage the application-specific fonts
1764 FcConfigAppFontAddFile (FcConfig
*config
,
1765 const FcChar8
*file
)
1774 config
= FcConfigGetCurrent ();
1779 subdirs
= FcStrSetCreate ();
1783 set
= FcConfigGetFonts (config
, FcSetApplication
);
1786 set
= FcFontSetCreate ();
1789 FcStrSetDestroy (subdirs
);
1792 FcConfigSetFonts (config
, set
, FcSetApplication
);
1795 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1797 FcStrSetDestroy (subdirs
);
1800 if ((sublist
= FcStrListCreate (subdirs
)))
1802 while ((subdir
= FcStrListNext (sublist
)))
1804 FcConfigAppFontAddDir (config
, subdir
);
1806 FcStrListDone (sublist
);
1808 FcStrSetDestroy (subdirs
);
1813 FcConfigAppFontAddDir (FcConfig
*config
,
1823 config
= FcConfigGetCurrent ();
1827 subdirs
= FcStrSetCreate ();
1831 set
= FcConfigGetFonts (config
, FcSetApplication
);
1834 set
= FcFontSetCreate ();
1837 FcStrSetDestroy (subdirs
);
1840 FcConfigSetFonts (config
, set
, FcSetApplication
);
1843 if (!FcDirScanConfig (set
, subdirs
, config
->blanks
, dir
, FcFalse
, config
))
1845 FcStrSetDestroy (subdirs
);
1848 if ((sublist
= FcStrListCreate (subdirs
)))
1850 while ((subdir
= FcStrListNext (sublist
)))
1852 FcConfigAppFontAddDir (config
, subdir
);
1854 FcStrListDone (sublist
);
1856 FcStrSetDestroy (subdirs
);
1861 FcConfigAppFontClear (FcConfig
*config
)
1865 config
= FcConfigGetCurrent ();
1870 FcConfigSetFonts (config
, 0, FcSetApplication
);
1874 * Manage filename-based font source selectors
1878 FcConfigGlobAdd (FcConfig
*config
,
1879 const FcChar8
*glob
,
1882 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1884 return FcStrSetAdd (set
, glob
);
1888 FcConfigGlobMatch (const FcChar8
*glob
,
1889 const FcChar8
*string
)
1893 while ((c
= *glob
++))
1897 /* short circuit common case */
1900 /* short circuit another common case */
1901 if (strchr ((char *) glob
, '*') == 0)
1902 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1905 if (FcConfigGlobMatch (glob
, string
))
1911 if (*string
++ == '\0')
1920 return *string
== '\0';
1924 FcConfigGlobsMatch (const FcStrSet
*globs
,
1925 const FcChar8
*string
)
1929 for (i
= 0; i
< globs
->num
; i
++)
1930 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1936 FcConfigAcceptFilename (FcConfig
*config
,
1937 const FcChar8
*filename
)
1939 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1941 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1947 * Manage font-pattern based font source selectors
1951 FcConfigPatternsAdd (FcConfig
*config
,
1955 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1957 return FcFontSetAdd (set
, pattern
);
1961 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1962 const FcPattern
*font
)
1966 for (i
= 0; i
< patterns
->nfont
; i
++)
1967 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1973 FcConfigAcceptFont (FcConfig
*config
,
1974 const FcPattern
*font
)
1976 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1978 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))