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
)
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
);
110 config
->cacheDirs
= FcStrSetCreate ();
111 if (!config
->cacheDirs
)
116 config
->substPattern
= 0;
117 config
->substFont
= 0;
118 config
->maxObjects
= 0;
119 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
120 config
->fonts
[set
] = 0;
122 config
->rescanTime
= time(0);
123 config
->rescanInterval
= 30;
128 FcStrFree (config
->cache
);
130 FcFontSetDestroy (config
->rejectPatterns
);
132 FcFontSetDestroy (config
->acceptPatterns
);
134 FcStrSetDestroy (config
->rejectGlobs
);
136 FcStrSetDestroy (config
->acceptGlobs
);
138 FcStrSetDestroy (config
->fontDirs
);
140 FcStrSetDestroy (config
->configFiles
);
142 FcStrSetDestroy (config
->configDirs
);
145 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
151 FcConfigNewestFile (FcStrSet
*files
)
153 FcStrList
*list
= FcStrListCreate (files
);
154 FcFileTime newest
= { 0, FcFalse
};
160 while ((file
= FcStrListNext (list
)))
161 if (stat ((char *) file
, &statb
) == 0)
162 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
165 newest
.time
= statb
.st_mtime
;
167 FcStrListDone (list
);
173 FcConfigModifiedTime (FcConfig
*config
)
177 FcFileTime v
= { 0, FcFalse
};
178 config
= FcConfigGetCurrent ();
182 return FcConfigNewestFile (config
->configFiles
);
186 FcConfigUptoDate (FcConfig
*config
)
188 FcFileTime config_time
, font_time
;
189 time_t now
= time(0);
192 config
= FcConfigGetCurrent ();
196 config_time
= FcConfigNewestFile (config
->configFiles
);
197 font_time
= FcConfigNewestFile (config
->fontDirs
);
198 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
199 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
203 config
->rescanTime
= now
;
208 FcSubstDestroy (FcSubst
*s
)
216 FcTestDestroy (s
->test
);
218 FcEditDestroy (s
->edit
);
220 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
226 FcConfigDestroy (FcConfig
*config
)
230 if (config
== _fcConfig
)
233 FcStrSetDestroy (config
->configDirs
);
234 FcStrSetDestroy (config
->fontDirs
);
235 FcStrSetDestroy (config
->cacheDirs
);
236 FcStrSetDestroy (config
->configFiles
);
237 FcStrSetDestroy (config
->acceptGlobs
);
238 FcStrSetDestroy (config
->rejectGlobs
);
239 FcFontSetDestroy (config
->acceptPatterns
);
240 FcFontSetDestroy (config
->rejectPatterns
);
243 FcBlanksDestroy (config
->blanks
);
246 FcStrFree (config
->cache
);
248 FcSubstDestroy (config
->substPattern
);
249 FcSubstDestroy (config
->substFont
);
250 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
251 if (config
->fonts
[set
])
252 FcFontSetDestroy (config
->fonts
[set
]);
255 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
259 * Scan the current list of directories in the configuration
260 * and build the set of available fonts. Update the
261 * per-user cache file to reflect the new configuration
265 FcConfigBuildFonts (FcConfig
*config
)
267 FcFontSet
*fonts
, *cached_fonts
;
272 fonts
= FcFontSetCreate ();
276 oldDirs
= FcStrSetCreate ();
280 cached_fonts
= FcCacheRead(config
);
283 list
= FcConfigGetFontDirs (config
);
287 while ((dir
= FcStrListNext (list
)))
289 if (FcDebug () & FC_DBG_FONTSET
)
290 printf ("build scan dir %s\n", dir
);
291 FcDirScanConfig (fonts
, config
->fontDirs
,
292 config
->blanks
, dir
, FcFalse
, config
);
295 FcStrListDone (list
);
301 for (i
= 0; i
< oldDirs
->num
; i
++)
303 if (FcDebug () & FC_DBG_FONTSET
)
304 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
305 FcDirScanConfig (fonts
, config
->fontDirs
,
306 config
->blanks
, oldDirs
->strs
[i
],
310 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
313 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
315 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
316 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
317 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
319 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
321 cached_fonts
->nfont
= 0;
322 FcFontSetDestroy (cached_fonts
);
325 if (FcDebug () & FC_DBG_FONTSET
)
326 FcFontSetPrint (fonts
);
328 FcStrSetDestroy (oldDirs
);
330 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
334 FcStrSetDestroy (oldDirs
);
336 FcFontSetDestroy (fonts
);
342 FcConfigSetCurrent (FcConfig
*config
)
345 if (!FcConfigBuildFonts (config
))
349 FcConfigDestroy (_fcConfig
);
355 FcConfigGetCurrent (void)
364 FcConfigAddConfigDir (FcConfig
*config
,
367 return FcStrSetAddFilename (config
->configDirs
, d
);
371 FcConfigGetConfigDirs (FcConfig
*config
)
375 config
= FcConfigGetCurrent ();
379 return FcStrListCreate (config
->configDirs
);
383 FcConfigAddFontDir (FcConfig
*config
,
386 return FcStrSetAddFilename (config
->fontDirs
, d
);
390 FcConfigAddDir (FcConfig
*config
,
393 return (FcConfigAddConfigDir (config
, d
) &&
394 FcConfigAddFontDir (config
, d
));
398 FcConfigGetFontDirs (FcConfig
*config
)
402 config
= FcConfigGetCurrent ();
406 return FcStrListCreate (config
->fontDirs
);
410 FcConfigAddCacheDir (FcConfig
*config
,
413 return FcStrSetAddFilename (config
->cacheDirs
, d
);
417 FcConfigGetCacheDirs (FcConfig
*config
)
421 config
= FcConfigGetCurrent ();
425 return FcStrListCreate (config
->cacheDirs
);
429 FcConfigAddConfigFile (FcConfig
*config
,
433 FcChar8
*file
= FcConfigFilename (f
);
438 ret
= FcStrSetAdd (config
->configFiles
, file
);
444 FcConfigGetConfigFiles (FcConfig
*config
)
448 config
= FcConfigGetCurrent ();
452 return FcStrListCreate (config
->configFiles
);
456 FcConfigSetCache (FcConfig
*config
,
459 FcChar8
*new = FcStrCopyFilename (c
);
464 FcStrFree (config
->cache
);
470 FcConfigGetCache (FcConfig
*config
)
474 config
= FcConfigGetCurrent ();
478 return config
->cache
;
482 FcConfigGetFonts (FcConfig
*config
,
487 config
= FcConfigGetCurrent ();
491 return config
->fonts
[set
];
495 FcConfigSetFonts (FcConfig
*config
,
499 if (config
->fonts
[set
])
500 FcFontSetDestroy (config
->fonts
[set
]);
501 config
->fonts
[set
] = fonts
;
507 FcConfigGetBlanks (FcConfig
*config
)
511 config
= FcConfigGetCurrent ();
515 return config
->blanks
;
519 FcConfigAddBlank (FcConfig
*config
,
522 FcBlanks
*b
, *freeme
= 0;
527 freeme
= b
= FcBlanksCreate ();
531 if (!FcBlanksAdd (b
, blank
))
534 FcBlanksDestroy (freeme
);
542 FcConfigGetRescanInverval (FcConfig
*config
)
546 config
= FcConfigGetCurrent ();
550 return config
->rescanInterval
;
554 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
558 config
= FcConfigGetCurrent ();
562 config
->rescanInterval
= rescanInterval
;
567 FcConfigAddEdit (FcConfig
*config
,
572 FcSubst
*subst
, **prev
;
576 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
579 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
580 if (kind
== FcMatchPattern
)
581 prev
= &config
->substPattern
;
583 prev
= &config
->substFont
;
584 for (; *prev
; prev
= &(*prev
)->next
);
590 for (t
= test
; t
; t
= t
->next
)
592 if (t
->kind
== FcMatchDefault
)
596 if (config
->maxObjects
< num
)
597 config
->maxObjects
= num
;
598 if (FcDebug () & FC_DBG_EDIT
)
600 printf ("Add Subst ");
601 FcSubstPrint (subst
);
606 typedef struct _FcSubState
{
612 FcConfigPromote (FcValue v
, FcValue u
)
614 if (v
.type
== FcTypeInteger
)
616 v
.type
= FcTypeDouble
;
617 v
.u
.d
= (double) v
.u
.i
;
619 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
621 v
.u
.m
= &FcIdentityMatrix
;
622 v
.type
= FcTypeMatrix
;
624 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
626 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
627 v
.type
= FcTypeLangSet
;
633 FcConfigCompareValue (const FcValue
*left_o
,
635 const FcValue
*right_o
)
637 FcValue left
= FcValueCanonicalize(left_o
);
638 FcValue right
= FcValueCanonicalize(right_o
);
639 FcBool ret
= FcFalse
;
641 left
= FcConfigPromote (left
, right
);
642 right
= FcConfigPromote (right
, left
);
643 if (left
.type
== right
.type
)
647 break; /* FcConfigPromote prevents this from happening */
653 ret
= left
.u
.d
== right
.u
.d
;
656 case FcOpNotContains
:
657 ret
= left
.u
.d
!= right
.u
.d
;
660 ret
= left
.u
.d
< right
.u
.d
;
663 ret
= left
.u
.d
<= right
.u
.d
;
666 ret
= left
.u
.d
> right
.u
.d
;
669 ret
= left
.u
.d
>= right
.u
.d
;
680 ret
= left
.u
.b
== right
.u
.b
;
683 case FcOpNotContains
:
684 ret
= left
.u
.b
!= right
.u
.b
;
694 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
697 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
700 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
702 case FcOpNotContains
:
703 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
714 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
717 case FcOpNotContains
:
718 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
728 /* left contains right if right is a subset of left */
729 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
731 case FcOpNotContains
:
732 /* left contains right if right is a subset of left */
733 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
736 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
739 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
749 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
751 case FcOpNotContains
:
752 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
755 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
758 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
780 ret
= left
.u
.f
== right
.u
.f
;
783 case FcOpNotContains
:
784 ret
= left
.u
.f
!= right
.u
.f
;
794 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
801 #define _FcDoubleFloor(d) ((int) (d))
802 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
803 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
804 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
805 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
806 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
809 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
818 v
.type
= FcTypeInteger
;
822 v
.type
= FcTypeDouble
;
826 v
.type
= FcTypeString
;
827 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
830 v
.type
= FcTypeMatrix
;
835 v
.type
= FcTypeCharSet
;
844 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
845 if (r
!= FcResultMatch
)
850 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
851 v
.type
= FcTypeInteger
;
856 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
857 if (vl
.type
== FcTypeBool
)
860 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
862 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
875 case FcOpNotContains
:
877 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
878 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
880 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
890 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
891 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
892 vl
= FcConfigPromote (vl
, vr
);
893 vr
= FcConfigPromote (vr
, vl
);
894 if (vl
.type
== vr
.type
)
900 v
.type
= FcTypeDouble
;
901 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
904 v
.type
= FcTypeDouble
;
905 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
908 v
.type
= FcTypeDouble
;
909 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
912 v
.type
= FcTypeDouble
;
913 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
919 if (v
.type
== FcTypeDouble
&&
920 v
.u
.d
== (double) (int) v
.u
.d
)
922 v
.type
= FcTypeInteger
;
930 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
934 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
944 v
.type
= FcTypeString
;
945 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
946 v
.u
.s
= FcStrStaticName (str
);
960 v
.type
= FcTypeMatrix
;
961 m
= malloc (sizeof (FcMatrix
));
964 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
965 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
989 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1002 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1008 v
.type
= FcTypeInteger
;
1009 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1012 v
.type
= FcTypeVoid
;
1015 FcValueDestroy (vl
);
1018 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1024 v
.type
= FcTypeInteger
;
1025 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1028 v
.type
= FcTypeVoid
;
1031 FcValueDestroy (vl
);
1034 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1040 v
.type
= FcTypeInteger
;
1041 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1044 v
.type
= FcTypeVoid
;
1047 FcValueDestroy (vl
);
1050 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1056 v
.type
= FcTypeInteger
;
1057 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1060 v
.type
= FcTypeVoid
;
1063 FcValueDestroy (vl
);
1066 v
.type
= FcTypeVoid
;
1072 static FcValueList
*
1073 FcConfigMatchValueList (FcPattern
*p
,
1075 FcValueList
*values
)
1077 FcValueList
*ret
= 0;
1078 FcExpr
*e
= t
->expr
;
1084 /* Compute the value of the match expression */
1085 if (e
->op
== FcOpComma
)
1087 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1088 e
= e
->u
.tree
.right
;
1092 value
= FcConfigEvaluate (p
, e
);
1096 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1098 /* Compare the pattern value to the match expression value */
1099 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1106 if (t
->qual
== FcQualAll
)
1113 FcValueDestroy (value
);
1118 static FcValueList
*
1119 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1125 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1128 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1129 if (e
->op
== FcOpComma
)
1131 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1132 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1136 l
->value
= FcConfigEvaluate (p
, e
);
1137 l
->next
= FcValueListPtrCreateDynamic(0);
1139 l
->binding
= binding
;
1140 if (l
->value
.type
== FcTypeVoid
)
1142 FcValueList
*next
= FcValueListPtrU(l
->next
);
1144 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1153 FcConfigAdd (FcValueListPtr
*head
,
1154 FcValueList
*position
,
1158 FcValueListPtr
*prev
, last
, v
;
1159 FcValueBinding sameBinding
;
1162 sameBinding
= position
->binding
;
1164 sameBinding
= FcValueBindingWeak
;
1165 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1166 v
= FcValueListPtrU(v
)->next
)
1167 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1168 FcValueListPtrU(v
)->binding
= sameBinding
;
1172 prev
= &position
->next
;
1174 for (prev
= head
; FcValueListPtrU(*prev
);
1175 prev
= &(FcValueListPtrU(*prev
)->next
))
1182 for (prev
= head
; FcValueListPtrU(*prev
);
1183 prev
= &(FcValueListPtrU(*prev
)->next
))
1185 if (FcValueListPtrU(*prev
) == position
)
1192 if (FcDebug () & FC_DBG_EDIT
)
1194 if (!FcValueListPtrU(*prev
))
1195 printf ("position not on list\n");
1199 if (FcDebug () & FC_DBG_EDIT
)
1201 printf ("%s list before ", append
? "Append" : "Prepend");
1202 FcValueListPrint (*head
);
1208 last
= FcValueListPtrCreateDynamic(new);
1209 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1210 last
= FcValueListPtrU(last
)->next
;
1212 FcValueListPtrU(last
)->next
= *prev
;
1213 *prev
= FcValueListPtrCreateDynamic(new);
1216 if (FcDebug () & FC_DBG_EDIT
)
1218 printf ("%s list after ", append
? "Append" : "Prepend");
1219 FcValueListPrint (*head
);
1227 FcConfigDel (FcValueListPtr
*head
,
1228 FcValueList
*position
)
1230 FcValueListPtr
*prev
;
1232 for (prev
= head
; FcValueListPtrU(*prev
);
1233 prev
= &(FcValueListPtrU(*prev
)->next
))
1235 if (FcValueListPtrU(*prev
) == position
)
1237 *prev
= position
->next
;
1238 position
->next
= FcValueListPtrCreateDynamic(0);
1239 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1246 FcConfigPatternAdd (FcPattern
*p
,
1253 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1257 FcConfigAdd (&e
->values
, 0, append
, list
);
1262 * Delete all values associated with a field
1265 FcConfigPatternDel (FcPattern
*p
,
1268 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1271 while (FcValueListPtrU(e
->values
))
1272 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1276 FcConfigPatternCanon (FcPattern
*p
,
1279 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1282 if (!FcValueListPtrU(e
->values
))
1283 FcPatternDel (p
, object
);
1287 FcConfigSubstituteWithPat (FcConfig
*config
,
1302 config
= FcConfigGetCurrent ();
1307 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1308 if (!st
&& config
->maxObjects
)
1310 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1312 if (FcDebug () & FC_DBG_EDIT
)
1314 printf ("FcConfigSubstitute ");
1317 if (kind
== FcMatchPattern
)
1318 s
= config
->substPattern
;
1320 s
= config
->substFont
;
1321 for (; s
; s
= s
->next
)
1324 * Check the tests to see if
1325 * they all match the pattern
1327 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1329 if (FcDebug () & FC_DBG_EDIT
)
1331 printf ("FcConfigSubstitute test ");
1335 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1340 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1344 * If there's no such field in the font,
1345 * then FcQualAll matches while FcQualAny does not
1349 if (t
->qual
== FcQualAll
)
1358 * Check to see if there is a match, mark the location
1359 * to apply match-relative edits
1361 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1364 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1366 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1371 if (FcDebug () & FC_DBG_EDIT
)
1372 printf ("No match\n");
1375 if (FcDebug () & FC_DBG_EDIT
)
1377 printf ("Substitute ");
1380 for (e
= s
->edit
; e
; e
= e
->next
)
1383 * Evaluate the list of expressions
1385 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1387 * Locate any test associated with this field, skipping
1388 * tests associated with the pattern when substituting in
1391 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1393 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1394 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1395 (FcChar8
*) e
->field
))
1398 * KLUDGE - the pattern may have been reallocated or
1399 * things may have been inserted or deleted above
1400 * this element by other edits. Go back and find
1403 if (e
!= s
->edit
&& st
[i
].elt
)
1404 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1413 * If there was a test, then replace the matched
1414 * value with the new list of values
1418 FcValueList
*thisValue
= st
[i
].value
;
1419 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1422 * Append the new list of values after the current value
1424 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1426 * Delete the marked value
1429 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1431 * Adjust any pointers into the value list to ensure
1432 * future edits occur at the same place
1434 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1436 if (st
[i
].value
== thisValue
)
1437 st
[i
].value
= nextValue
;
1441 /* fall through ... */
1442 case FcOpAssignReplace
:
1444 * Delete all of the values and insert
1447 FcConfigPatternDel (p
, e
->field
);
1448 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1450 * Adjust any pointers into the value list as they no
1451 * longer point to anything valid
1455 FcPatternElt
*thisElt
= st
[i
].elt
;
1456 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1458 if (st
[i
].elt
== thisElt
)
1466 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1469 /* fall through ... */
1470 case FcOpPrependFirst
:
1471 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1476 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1479 /* fall through ... */
1480 case FcOpAppendLast
:
1481 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1484 FcValueListDestroy (FcValueListPtrCreateDynamic(l
));
1489 * Now go through the pattern and eliminate
1490 * any properties without data
1492 for (e
= s
->edit
; e
; e
= e
->next
)
1493 FcConfigPatternCanon (p
, e
->field
);
1495 if (FcDebug () & FC_DBG_EDIT
)
1497 printf ("FcConfigSubstitute edit");
1501 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1503 if (FcDebug () & FC_DBG_EDIT
)
1505 printf ("FcConfigSubstitute done");
1512 FcConfigSubstitute (FcConfig
*config
,
1516 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1519 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1521 static FcChar8 fontconfig_path
[1000] = "";
1524 DllMain (HINSTANCE hinstDLL
,
1530 switch (fdwReason
) {
1531 case DLL_PROCESS_ATTACH
:
1532 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1533 sizeof (fontconfig_path
)))
1536 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1537 * assume it's a Unix-style installation tree, and use
1538 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1539 * folder where the DLL is as FONTCONFIG_PATH.
1541 p
= strrchr (fontconfig_path
, '\\');
1545 p
= strrchr (fontconfig_path
, '\\');
1546 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1547 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1549 strcat (fontconfig_path
, "\\etc\\fonts");
1552 fontconfig_path
[0] = '\0';
1560 #undef FONTCONFIG_PATH
1561 #define FONTCONFIG_PATH fontconfig_path
1563 #else /* !(_WIN32 && PIC) */
1565 #endif /* !(_WIN32 && PIC) */
1567 #ifndef FONTCONFIG_FILE
1568 #define FONTCONFIG_FILE "fonts.conf"
1572 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1577 dir
= (FcChar8
*) "";
1578 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1582 strcpy ((char *) path
, (const char *) dir
);
1583 /* make sure there's a single separator */
1585 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1586 path
[strlen((char *) path
)-1] != '\\')) &&
1589 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1590 strcat ((char *) path
, "\\");
1592 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1593 strcat ((char *) path
, "/");
1595 strcat ((char *) path
, (char *) file
);
1597 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1598 if (access ((char *) path
, R_OK
) == 0)
1606 FcConfigGetPath (void)
1609 FcChar8
*env
, *e
, *colon
;
1614 npath
= 2; /* default dir + null */
1615 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1621 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1624 path
= calloc (npath
, sizeof (FcChar8
*));
1634 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1636 colon
= e
+ strlen ((char *) e
);
1637 path
[i
] = malloc (colon
- e
+ 1);
1640 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1641 path
[i
][colon
- e
] = '\0';
1650 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1651 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1654 strcpy ((char *) path
[i
], (const char *) dir
);
1658 for (i
= 0; path
[i
]; i
++)
1666 FcConfigFreePath (FcChar8
**path
)
1670 for (p
= path
; *p
; p
++)
1675 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1680 if (_FcConfigHomeEnabled
)
1682 char *home
= getenv ("HOME");
1686 home
= getenv ("USERPROFILE");
1689 return (FcChar8
*) home
;
1695 FcConfigEnableHome (FcBool enable
)
1697 FcBool prev
= _FcConfigHomeEnabled
;
1698 _FcConfigHomeEnabled
= enable
;
1703 FcConfigFilename (const FcChar8
*url
)
1705 FcChar8
*file
, *dir
, **path
, **p
;
1709 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1711 url
= (FcChar8
*) FONTCONFIG_FILE
;
1716 if (isalpha (*url
) &&
1718 (url
[2] == '/' || url
[2] == '\\'))
1724 dir
= FcConfigHome ();
1726 file
= FcConfigFileExists (dir
, url
+ 1);
1735 file
= FcConfigFileExists (0, url
);
1738 path
= FcConfigGetPath ();
1741 for (p
= path
; *p
; p
++)
1743 file
= FcConfigFileExists (*p
, url
);
1747 FcConfigFreePath (path
);
1754 * Manage the application-specific fonts
1758 FcConfigAppFontAddFile (FcConfig
*config
,
1759 const FcChar8
*file
)
1768 config
= FcConfigGetCurrent ();
1773 subdirs
= FcStrSetCreate ();
1777 set
= FcConfigGetFonts (config
, FcSetApplication
);
1780 set
= FcFontSetCreate ();
1783 FcStrSetDestroy (subdirs
);
1786 FcConfigSetFonts (config
, set
, FcSetApplication
);
1789 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, FcFalse
, config
))
1791 FcStrSetDestroy (subdirs
);
1794 if ((sublist
= FcStrListCreate (subdirs
)))
1796 while ((subdir
= FcStrListNext (sublist
)))
1798 FcConfigAppFontAddDir (config
, subdir
);
1800 FcStrListDone (sublist
);
1802 FcStrSetDestroy (subdirs
);
1807 FcConfigAppFontAddDir (FcConfig
*config
,
1817 config
= FcConfigGetCurrent ();
1821 subdirs
= FcStrSetCreate ();
1825 set
= FcConfigGetFonts (config
, FcSetApplication
);
1828 set
= FcFontSetCreate ();
1831 FcStrSetDestroy (subdirs
);
1834 FcConfigSetFonts (config
, set
, FcSetApplication
);
1837 if (!FcDirScanConfig (set
, subdirs
, config
->blanks
, dir
, FcFalse
, config
))
1839 FcStrSetDestroy (subdirs
);
1842 if ((sublist
= FcStrListCreate (subdirs
)))
1844 while ((subdir
= FcStrListNext (sublist
)))
1846 FcConfigAppFontAddDir (config
, subdir
);
1848 FcStrListDone (sublist
);
1850 FcStrSetDestroy (subdirs
);
1855 FcConfigAppFontClear (FcConfig
*config
)
1859 config
= FcConfigGetCurrent ();
1864 FcConfigSetFonts (config
, 0, FcSetApplication
);
1868 * Manage filename-based font source selectors
1872 FcConfigGlobAdd (FcConfig
*config
,
1873 const FcChar8
*glob
,
1876 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1878 return FcStrSetAdd (set
, glob
);
1882 FcConfigGlobMatch (const FcChar8
*glob
,
1883 const FcChar8
*string
)
1887 while ((c
= *glob
++))
1891 /* short circuit common case */
1894 /* short circuit another common case */
1895 if (strchr ((char *) glob
, '*') == 0)
1896 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1899 if (FcConfigGlobMatch (glob
, string
))
1905 if (*string
++ == '\0')
1914 return *string
== '\0';
1918 FcConfigGlobsMatch (const FcStrSet
*globs
,
1919 const FcChar8
*string
)
1923 for (i
= 0; i
< globs
->num
; i
++)
1924 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1930 FcConfigAcceptFilename (FcConfig
*config
,
1931 const FcChar8
*filename
)
1933 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1935 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1941 * Manage font-pattern based font source selectors
1945 FcConfigPatternsAdd (FcConfig
*config
,
1949 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1951 return FcFontSetAdd (set
, pattern
);
1955 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1956 const FcPattern
*font
)
1960 for (i
= 0; i
< patterns
->nfont
; i
++)
1961 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1967 FcConfigAcceptFont (FcConfig
*config
,
1968 const FcPattern
*font
)
1970 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1972 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))