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 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
33 #if defined (_WIN32) && !defined (R_OK)
45 config
= malloc (sizeof (FcConfig
));
48 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
50 config
->configDirs
= FcStrSetCreate ();
51 if (!config
->configDirs
)
54 config
->configFiles
= FcStrSetCreate ();
55 if (!config
->configFiles
)
58 config
->fontDirs
= FcStrSetCreate ();
59 if (!config
->fontDirs
)
62 config
->acceptGlobs
= FcStrSetCreate ();
63 if (!config
->acceptGlobs
)
66 config
->rejectGlobs
= FcStrSetCreate ();
67 if (!config
->rejectGlobs
)
70 config
->acceptPatterns
= FcFontSetCreate ();
71 if (!config
->acceptPatterns
)
74 config
->rejectPatterns
= FcFontSetCreate ();
75 if (!config
->rejectPatterns
)
80 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
84 if (config
->cache
== 0)
86 /* If no home, use the temp folder. */
88 int templen
= GetTempPath (1, dummy
);
89 FcChar8
*temp
= malloc (templen
+ 1);
95 GetTempPath (templen
+ 1, temp
);
96 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
98 if (!FcConfigSetCache (config
, cache_dir
))
100 FcStrFree (cache_dir
);
103 FcStrFree (cache_dir
);
110 config
->substPattern
= 0;
111 config
->substFont
= 0;
112 config
->maxObjects
= 0;
113 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
114 config
->fonts
[set
] = 0;
116 config
->rescanTime
= time(0);
117 config
->rescanInterval
= 30;
122 FcFontSetDestroy (config
->rejectPatterns
);
124 FcFontSetDestroy (config
->acceptPatterns
);
126 FcStrSetDestroy (config
->rejectGlobs
);
128 FcStrSetDestroy (config
->acceptGlobs
);
130 FcStrSetDestroy (config
->fontDirs
);
132 FcStrSetDestroy (config
->configFiles
);
134 FcStrSetDestroy (config
->configDirs
);
137 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
143 FcConfigNewestFile (FcStrSet
*files
)
145 FcStrList
*list
= FcStrListCreate (files
);
146 FcFileTime newest
= { 0, FcFalse
};
152 while ((file
= FcStrListNext (list
)))
153 if (stat ((char *) file
, &statb
) == 0)
154 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
157 newest
.time
= statb
.st_mtime
;
159 FcStrListDone (list
);
165 FcConfigModifiedTime (FcConfig
*config
)
169 FcFileTime v
= { 0, FcFalse
};
170 config
= FcConfigGetCurrent ();
174 return FcConfigNewestFile (config
->configFiles
);
178 FcConfigUptoDate (FcConfig
*config
)
180 FcFileTime config_time
, font_time
;
181 time_t now
= time(0);
184 config
= FcConfigGetCurrent ();
188 config_time
= FcConfigNewestFile (config
->configFiles
);
189 font_time
= FcConfigNewestFile (config
->fontDirs
);
190 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
191 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
195 config
->rescanTime
= now
;
200 FcSubstDestroy (FcSubst
*s
)
208 FcTestDestroy (s
->test
);
210 FcEditDestroy (s
->edit
);
212 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
218 FcConfigDestroy (FcConfig
*config
)
222 if (config
== _fcConfig
)
225 FcStrSetDestroy (config
->configDirs
);
226 FcStrSetDestroy (config
->fontDirs
);
227 FcStrSetDestroy (config
->configFiles
);
228 FcStrSetDestroy (config
->acceptGlobs
);
229 FcStrSetDestroy (config
->rejectGlobs
);
230 FcFontSetDestroy (config
->acceptPatterns
);
231 FcFontSetDestroy (config
->rejectPatterns
);
234 FcBlanksDestroy (config
->blanks
);
237 FcStrFree (config
->cache
);
239 FcSubstDestroy (config
->substPattern
);
240 FcSubstDestroy (config
->substFont
);
241 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
242 if (config
->fonts
[set
])
243 FcFontSetDestroy (config
->fonts
[set
]);
246 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
250 * Scan the current list of directories in the configuration
251 * and build the set of available fonts. Update the
252 * per-user cache file to reflect the new configuration
256 FcConfigBuildFonts (FcConfig
*config
)
258 FcFontSet
*fonts
, *cached_fonts
;
259 FcGlobalCache
*cache
;
264 fonts
= FcFontSetCreate ();
268 cache
= FcGlobalCacheCreate ();
272 oldDirs
= FcStrSetCreate ();
277 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
, config
);
279 cached_fonts
= FcCacheRead(config
, cache
);
282 list
= FcConfigGetFontDirs (config
);
286 while ((dir
= FcStrListNext (list
)))
288 if (FcDebug () & FC_DBG_FONTSET
)
289 printf ("build scan dir %s\n", dir
);
290 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
291 config
->blanks
, dir
, FcFalse
, config
);
294 FcStrListDone (list
);
300 for (i
= 0; i
< oldDirs
->num
; i
++)
302 if (FcDebug () & FC_DBG_FONTSET
)
303 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
304 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
305 config
->blanks
, oldDirs
->strs
[i
],
309 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
312 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
314 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
315 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
316 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
318 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
320 cached_fonts
->nfont
= 0;
321 FcFontSetDestroy (cached_fonts
);
324 if (FcDebug () & FC_DBG_FONTSET
)
325 FcFontSetPrint (fonts
);
328 FcGlobalCacheSave (cache
, config
->cache
, config
);
329 FcGlobalCacheDestroy (cache
);
330 FcStrSetDestroy (oldDirs
);
332 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
336 FcStrSetDestroy (oldDirs
);
338 FcFontSetDestroy (fonts
);
344 FcConfigSetCurrent (FcConfig
*config
)
347 if (!FcConfigBuildFonts (config
))
351 FcConfigDestroy (_fcConfig
);
357 FcConfigGetCurrent (void)
366 FcConfigAddConfigDir (FcConfig
*config
,
369 return FcStrSetAddFilename (config
->configDirs
, d
);
373 FcConfigGetConfigDirs (FcConfig
*config
)
377 config
= FcConfigGetCurrent ();
381 return FcStrListCreate (config
->configDirs
);
385 FcConfigAddFontDir (FcConfig
*config
,
388 return FcStrSetAddFilename (config
->fontDirs
, d
);
392 FcConfigNormalizeFontDir (FcConfig
*config
,
395 /* If this is a bottleneck, we can cache the fontDir inodes. */
401 if (stat ((char *)d
, &s
) == -1)
403 di
= s
.st_ino
; dd
= s
.st_dev
;
405 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
407 if (stat ((char *)config
->fontDirs
->strs
[n
], &s
) == -1)
409 if (di
== s
.st_ino
&& dd
== s
.st_dev
)
410 return config
->fontDirs
->strs
[n
];
416 FcConfigAddDir (FcConfig
*config
,
419 return (FcConfigAddConfigDir (config
, d
) &&
420 FcConfigAddFontDir (config
, d
));
424 FcConfigGetFontDirs (FcConfig
*config
)
428 config
= FcConfigGetCurrent ();
432 return FcStrListCreate (config
->fontDirs
);
436 FcConfigAddConfigFile (FcConfig
*config
,
440 FcChar8
*file
= FcConfigFilename (f
);
445 ret
= FcStrSetAdd (config
->configFiles
, file
);
451 FcConfigGetConfigFiles (FcConfig
*config
)
455 config
= FcConfigGetCurrent ();
459 return FcStrListCreate (config
->configFiles
);
463 FcConfigSetCache (FcConfig
*config
,
466 FcChar8
*new = FcStrCopyFilename (c
);
471 FcStrFree (config
->cache
);
477 FcConfigGetCache (FcConfig
*config
)
481 config
= FcConfigGetCurrent ();
485 return config
->cache
;
489 FcConfigGetFonts (FcConfig
*config
,
494 config
= FcConfigGetCurrent ();
498 return config
->fonts
[set
];
502 FcConfigSetFonts (FcConfig
*config
,
506 if (config
->fonts
[set
])
507 FcFontSetDestroy (config
->fonts
[set
]);
508 config
->fonts
[set
] = fonts
;
514 FcConfigGetBlanks (FcConfig
*config
)
518 config
= FcConfigGetCurrent ();
522 return config
->blanks
;
526 FcConfigAddBlank (FcConfig
*config
,
534 b
= FcBlanksCreate ();
538 if (!FcBlanksAdd (b
, blank
))
545 FcConfigGetRescanInverval (FcConfig
*config
)
549 config
= FcConfigGetCurrent ();
553 return config
->rescanInterval
;
557 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
561 config
= FcConfigGetCurrent ();
565 config
->rescanInterval
= rescanInterval
;
570 FcConfigAddEdit (FcConfig
*config
,
575 FcSubst
*subst
, **prev
;
579 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
582 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
583 if (kind
== FcMatchPattern
)
584 prev
= &config
->substPattern
;
586 prev
= &config
->substFont
;
587 for (; *prev
; prev
= &(*prev
)->next
);
593 for (t
= test
; t
; t
= t
->next
)
595 if (t
->kind
== FcMatchDefault
)
599 if (config
->maxObjects
< num
)
600 config
->maxObjects
= num
;
601 if (FcDebug () & FC_DBG_EDIT
)
603 printf ("Add Subst ");
604 FcSubstPrint (subst
);
609 typedef struct _FcSubState
{
615 FcConfigPromote (FcValue v
, FcValue u
)
617 if (v
.type
== FcTypeInteger
)
619 v
.type
= FcTypeDouble
;
620 v
.u
.d
= (double) v
.u
.i
;
622 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
624 v
.u
.m
= &FcIdentityMatrix
;
625 v
.type
= FcTypeMatrix
;
627 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
629 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
630 v
.type
= FcTypeLangSet
;
636 FcConfigCompareValue (const FcValue
*left_o
,
638 const FcValue
*right_o
)
640 FcValue left
= FcValueCanonicalize(left_o
);
641 FcValue right
= FcValueCanonicalize(right_o
);
642 FcBool ret
= FcFalse
;
644 left
= FcConfigPromote (left
, right
);
645 right
= FcConfigPromote (right
, left
);
646 if (left
.type
== right
.type
)
650 break; /* FcConfigPromote prevents this from happening */
656 ret
= left
.u
.d
== right
.u
.d
;
659 case FcOpNotContains
:
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
;
672 ret
= left
.u
.d
>= right
.u
.d
;
683 ret
= left
.u
.b
== right
.u
.b
;
686 case FcOpNotContains
:
687 ret
= left
.u
.b
!= right
.u
.b
;
697 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
700 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
703 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
705 case FcOpNotContains
:
706 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
717 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
720 case FcOpNotContains
:
721 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
731 /* left contains right if right is a subset of left */
732 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
734 case FcOpNotContains
:
735 /* left contains right if right is a subset of left */
736 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
739 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
742 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
752 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
754 case FcOpNotContains
:
755 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
758 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
761 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
783 ret
= left
.u
.f
== right
.u
.f
;
786 case FcOpNotContains
:
787 ret
= left
.u
.f
!= right
.u
.f
;
797 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
804 #define _FcDoubleFloor(d) ((int) (d))
805 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
806 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
807 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
808 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
809 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
812 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
820 v
.type
= FcTypeInteger
;
824 v
.type
= FcTypeDouble
;
828 v
.type
= FcTypeString
;
829 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
832 v
.type
= FcTypeMatrix
;
837 v
.type
= FcTypeCharSet
;
846 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
847 if (r
!= FcResultMatch
)
852 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
853 v
.type
= FcTypeInteger
;
858 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
859 if (vl
.type
== FcTypeBool
)
862 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
864 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
877 case FcOpNotContains
:
879 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
880 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
882 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
892 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
893 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
894 vl
= FcConfigPromote (vl
, vr
);
895 vr
= FcConfigPromote (vr
, vl
);
896 if (vl
.type
== vr
.type
)
902 v
.type
= FcTypeDouble
;
903 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
906 v
.type
= FcTypeDouble
;
907 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
910 v
.type
= FcTypeDouble
;
911 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
914 v
.type
= FcTypeDouble
;
915 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
921 if (v
.type
== FcTypeDouble
&&
922 v
.u
.d
== (double) (int) v
.u
.d
)
924 v
.type
= FcTypeInteger
;
932 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
936 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
946 v
.type
= FcTypeString
;
947 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
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
1428 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1430 * Adjust any pointers into the value list to ensure
1431 * future edits occur at the same place
1433 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1435 if (st
[i
].value
== thisValue
)
1436 st
[i
].value
= nextValue
;
1440 /* fall through ... */
1441 case FcOpAssignReplace
:
1443 * Delete all of the values and insert
1446 FcConfigPatternDel (p
, e
->field
);
1447 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1449 * Adjust any pointers into the value list as they no
1450 * longer point to anything valid
1454 FcPatternElt
*thisElt
= st
[i
].elt
;
1455 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1457 if (st
[i
].elt
== thisElt
)
1465 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1468 /* fall through ... */
1469 case FcOpPrependFirst
:
1470 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1475 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1478 /* fall through ... */
1479 case FcOpAppendLast
:
1480 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1487 * Now go through the pattern and eliminate
1488 * any properties without data
1490 for (e
= s
->edit
; e
; e
= e
->next
)
1491 FcConfigPatternCanon (p
, e
->field
);
1493 if (FcDebug () & FC_DBG_EDIT
)
1495 printf ("FcConfigSubstitute edit");
1499 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1501 if (FcDebug () & FC_DBG_EDIT
)
1503 printf ("FcConfigSubstitute done");
1510 FcConfigSubstitute (FcConfig
*config
,
1514 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1517 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1519 static FcChar8 fontconfig_path
[1000] = "";
1522 DllMain (HINSTANCE hinstDLL
,
1528 switch (fdwReason
) {
1529 case DLL_PROCESS_ATTACH
:
1530 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1531 sizeof (fontconfig_path
)))
1534 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1535 * assume it's a Unix-style installation tree, and use
1536 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1537 * folder where the DLL is as FONTCONFIG_PATH.
1539 p
= strrchr (fontconfig_path
, '\\');
1543 p
= strrchr (fontconfig_path
, '\\');
1544 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1545 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1547 strcat (fontconfig_path
, "\\etc\\fonts");
1550 fontconfig_path
[0] = '\0';
1558 #undef FONTCONFIG_PATH
1559 #define FONTCONFIG_PATH fontconfig_path
1561 #else /* !(_WIN32 && PIC) */
1563 #endif /* !(_WIN32 && PIC) */
1565 #ifndef FONTCONFIG_FILE
1566 #define FONTCONFIG_FILE "fonts.conf"
1570 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1575 dir
= (FcChar8
*) "";
1576 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1580 strcpy ((char *) path
, (const char *) dir
);
1581 /* make sure there's a single separator */
1583 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1584 path
[strlen((char *) path
)-1] != '\\')) &&
1587 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1588 strcat ((char *) path
, "\\");
1590 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1591 strcat ((char *) path
, "/");
1593 strcat ((char *) path
, (char *) file
);
1595 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1596 if (access ((char *) path
, R_OK
) == 0)
1604 FcConfigGetPath (void)
1607 FcChar8
*env
, *e
, *colon
;
1612 npath
= 2; /* default dir + null */
1613 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1619 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1622 path
= calloc (npath
, sizeof (FcChar8
*));
1632 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1634 colon
= e
+ strlen ((char *) e
);
1635 path
[i
] = malloc (colon
- e
+ 1);
1638 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1639 path
[i
][colon
- e
] = '\0';
1648 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1649 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1652 strcpy ((char *) path
[i
], (const char *) dir
);
1656 for (i
= 0; path
[i
]; i
++)
1664 FcConfigFreePath (FcChar8
**path
)
1668 for (p
= path
; *p
; p
++)
1673 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1678 if (_FcConfigHomeEnabled
)
1680 char *home
= getenv ("HOME");
1684 home
= getenv ("USERPROFILE");
1687 return (FcChar8
*) home
;
1693 FcConfigEnableHome (FcBool enable
)
1695 FcBool prev
= _FcConfigHomeEnabled
;
1696 _FcConfigHomeEnabled
= enable
;
1701 FcConfigFilename (const FcChar8
*url
)
1703 FcChar8
*file
, *dir
, **path
, **p
;
1707 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1709 url
= (FcChar8
*) FONTCONFIG_FILE
;
1714 if (isalpha (*url
) &&
1716 (url
[2] == '/' || url
[2] == '\\'))
1722 dir
= FcConfigHome ();
1724 file
= FcConfigFileExists (dir
, url
+ 1);
1733 file
= FcConfigFileExists (0, url
);
1736 path
= FcConfigGetPath ();
1739 for (p
= path
; *p
; p
++)
1741 file
= FcConfigFileExists (*p
, url
);
1745 FcConfigFreePath (path
);
1752 * Manage the application-specific fonts
1756 FcConfigAppFontAddFile (FcConfig
*config
,
1757 const FcChar8
*file
)
1766 config
= FcConfigGetCurrent ();
1771 subdirs
= FcStrSetCreate ();
1775 set
= FcConfigGetFonts (config
, FcSetApplication
);
1778 set
= FcFontSetCreate ();
1781 FcStrSetDestroy (subdirs
);
1784 FcConfigSetFonts (config
, set
, FcSetApplication
);
1787 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1789 FcStrSetDestroy (subdirs
);
1792 if ((sublist
= FcStrListCreate (subdirs
)))
1794 while ((subdir
= FcStrListNext (sublist
)))
1796 FcConfigAppFontAddDir (config
, subdir
);
1798 FcStrListDone (sublist
);
1804 FcConfigAppFontAddDir (FcConfig
*config
,
1814 config
= FcConfigGetCurrent ();
1818 subdirs
= FcStrSetCreate ();
1822 set
= FcConfigGetFonts (config
, FcSetApplication
);
1825 set
= FcFontSetCreate ();
1828 FcStrSetDestroy (subdirs
);
1831 FcConfigSetFonts (config
, set
, FcSetApplication
);
1834 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1836 FcStrSetDestroy (subdirs
);
1839 if ((sublist
= FcStrListCreate (subdirs
)))
1841 while ((subdir
= FcStrListNext (sublist
)))
1843 FcConfigAppFontAddDir (config
, subdir
);
1845 FcStrListDone (sublist
);
1851 FcConfigAppFontClear (FcConfig
*config
)
1855 config
= FcConfigGetCurrent ();
1860 FcConfigSetFonts (config
, 0, FcSetApplication
);
1864 * Manage filename-based font source selectors
1868 FcConfigGlobAdd (FcConfig
*config
,
1869 const FcChar8
*glob
,
1872 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1874 return FcStrSetAdd (set
, glob
);
1878 FcConfigGlobMatch (const FcChar8
*glob
,
1879 const FcChar8
*string
)
1883 while ((c
= *glob
++))
1887 /* short circuit common case */
1890 /* short circuit another common case */
1891 if (strchr ((char *) glob
, '*') == 0)
1892 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1895 if (FcConfigGlobMatch (glob
, string
))
1901 if (*string
++ == '\0')
1910 return *string
== '\0';
1914 FcConfigGlobsMatch (const FcStrSet
*globs
,
1915 const FcChar8
*string
)
1919 for (i
= 0; i
< globs
->num
; i
++)
1920 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1926 FcConfigAcceptFilename (FcConfig
*config
,
1927 const FcChar8
*filename
)
1929 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1931 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1937 * Manage font-pattern based font source selectors
1941 FcConfigPatternsAdd (FcConfig
*config
,
1945 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1947 return FcFontSetAdd (set
, pattern
);
1951 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1952 const FcPattern
*font
)
1956 for (i
= 0; i
< patterns
->nfont
; i
++)
1957 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1963 FcConfigAcceptFont (FcConfig
*config
,
1964 const FcPattern
*font
)
1966 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1968 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))