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
);
313 cfd
= (FcChar8
*)FcCacheFindBankDir (cached_fonts
->fonts
[i
]->bank
);
315 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
316 (cfn
&& FcConfigAcceptFilename (config
, cfn
)) &&
317 (cfd
&& FcConfigAcceptFilename (config
, cfd
)))
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
);
331 FcGlobalCacheDestroy (cache
);
332 FcStrSetDestroy (oldDirs
);
334 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
338 FcStrSetDestroy (oldDirs
);
340 FcFontSetDestroy (fonts
);
346 FcConfigSetCurrent (FcConfig
*config
)
349 if (!FcConfigBuildFonts (config
))
353 FcConfigDestroy (_fcConfig
);
359 FcConfigGetCurrent (void)
368 FcConfigAddConfigDir (FcConfig
*config
,
371 return FcStrSetAddFilename (config
->configDirs
, d
);
375 FcConfigGetConfigDirs (FcConfig
*config
)
379 config
= FcConfigGetCurrent ();
383 return FcStrListCreate (config
->configDirs
);
387 FcConfigAddFontDir (FcConfig
*config
,
390 return FcStrSetAddFilename (config
->fontDirs
, d
);
394 FcConfigAddDir (FcConfig
*config
,
397 return (FcConfigAddConfigDir (config
, d
) &&
398 FcConfigAddFontDir (config
, d
));
402 FcConfigGetFontDirs (FcConfig
*config
)
406 config
= FcConfigGetCurrent ();
410 return FcStrListCreate (config
->fontDirs
);
414 FcConfigAddConfigFile (FcConfig
*config
,
418 FcChar8
*file
= FcConfigFilename (f
);
423 ret
= FcStrSetAdd (config
->configFiles
, file
);
429 FcConfigGetConfigFiles (FcConfig
*config
)
433 config
= FcConfigGetCurrent ();
437 return FcStrListCreate (config
->configFiles
);
441 FcConfigSetCache (FcConfig
*config
,
444 FcChar8
*new = FcStrCopyFilename (c
);
449 FcStrFree (config
->cache
);
455 FcConfigGetCache (FcConfig
*config
)
459 config
= FcConfigGetCurrent ();
463 return config
->cache
;
467 FcConfigGetFonts (FcConfig
*config
,
472 config
= FcConfigGetCurrent ();
476 return config
->fonts
[set
];
480 FcConfigSetFonts (FcConfig
*config
,
484 if (config
->fonts
[set
])
485 FcFontSetDestroy (config
->fonts
[set
]);
486 config
->fonts
[set
] = fonts
;
492 FcConfigGetBlanks (FcConfig
*config
)
496 config
= FcConfigGetCurrent ();
500 return config
->blanks
;
504 FcConfigAddBlank (FcConfig
*config
,
512 b
= FcBlanksCreate ();
516 if (!FcBlanksAdd (b
, blank
))
523 FcConfigGetRescanInverval (FcConfig
*config
)
527 config
= FcConfigGetCurrent ();
531 return config
->rescanInterval
;
535 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
539 config
= FcConfigGetCurrent ();
543 config
->rescanInterval
= rescanInterval
;
548 FcConfigAddEdit (FcConfig
*config
,
553 FcSubst
*subst
, **prev
;
557 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
560 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
561 if (kind
== FcMatchPattern
)
562 prev
= &config
->substPattern
;
564 prev
= &config
->substFont
;
565 for (; *prev
; prev
= &(*prev
)->next
);
571 for (t
= test
; t
; t
= t
->next
)
573 if (t
->kind
== FcMatchDefault
)
577 if (config
->maxObjects
< num
)
578 config
->maxObjects
= num
;
579 if (FcDebug () & FC_DBG_EDIT
)
581 printf ("Add Subst ");
582 FcSubstPrint (subst
);
587 typedef struct _FcSubState
{
593 FcConfigPromote (FcValue v
, FcValue u
)
595 if (v
.type
== FcTypeInteger
)
597 v
.type
= FcTypeDouble
;
598 v
.u
.d
= (double) v
.u
.i
;
600 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
602 v
.u
.m
= &FcIdentityMatrix
;
603 v
.type
= FcTypeMatrix
;
605 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
607 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
608 v
.type
= FcTypeLangSet
;
614 FcConfigCompareValue (const FcValue
*left_o
,
616 const FcValue
*right_o
)
618 FcValue left
= FcValueCanonicalize(left_o
);
619 FcValue right
= FcValueCanonicalize(right_o
);
620 FcBool ret
= FcFalse
;
622 left
= FcConfigPromote (left
, right
);
623 right
= FcConfigPromote (right
, left
);
624 if (left
.type
== right
.type
)
628 break; /* FcConfigPromote prevents this from happening */
634 ret
= left
.u
.d
== right
.u
.d
;
637 case FcOpNotContains
:
638 ret
= left
.u
.d
!= right
.u
.d
;
641 ret
= left
.u
.d
< right
.u
.d
;
644 ret
= left
.u
.d
<= right
.u
.d
;
647 ret
= left
.u
.d
> right
.u
.d
;
650 ret
= left
.u
.d
>= right
.u
.d
;
661 ret
= left
.u
.b
== right
.u
.b
;
664 case FcOpNotContains
:
665 ret
= left
.u
.b
!= right
.u
.b
;
675 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
678 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
681 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
683 case FcOpNotContains
:
684 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
695 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
698 case FcOpNotContains
:
699 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
709 /* left contains right if right is a subset of left */
710 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
712 case FcOpNotContains
:
713 /* left contains right if right is a subset of left */
714 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
717 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
720 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
730 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
732 case FcOpNotContains
:
733 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
736 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
739 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
761 ret
= left
.u
.f
== right
.u
.f
;
764 case FcOpNotContains
:
765 ret
= left
.u
.f
!= right
.u
.f
;
775 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
782 #define _FcDoubleFloor(d) ((int) (d))
783 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
784 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
785 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
786 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
787 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
790 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
798 v
.type
= FcTypeInteger
;
802 v
.type
= FcTypeDouble
;
806 v
.type
= FcTypeString
;
807 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
811 v
.type
= FcTypeMatrix
;
816 v
.type
= FcTypeCharSet
;
825 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
826 if (r
!= FcResultMatch
)
830 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
831 v
.type
= FcTypeInteger
;
836 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
837 if (vl
.type
== FcTypeBool
)
840 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
842 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
855 case FcOpNotContains
:
857 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
858 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
860 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
870 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
871 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
872 vl
= FcConfigPromote (vl
, vr
);
873 vr
= FcConfigPromote (vr
, vl
);
874 if (vl
.type
== vr
.type
)
880 v
.type
= FcTypeDouble
;
881 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
884 v
.type
= FcTypeDouble
;
885 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
888 v
.type
= FcTypeDouble
;
889 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
892 v
.type
= FcTypeDouble
;
893 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
899 if (v
.type
== FcTypeDouble
&&
900 v
.u
.d
== (double) (int) v
.u
.d
)
902 v
.type
= FcTypeInteger
;
910 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
914 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
924 v
.type
= FcTypeString
;
925 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
938 v
.type
= FcTypeMatrix
;
939 m
= malloc (sizeof (FcMatrix
));
942 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
943 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
967 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
980 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
986 v
.type
= FcTypeInteger
;
987 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
996 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1002 v
.type
= FcTypeInteger
;
1003 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1006 v
.type
= FcTypeVoid
;
1009 FcValueDestroy (vl
);
1012 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1018 v
.type
= FcTypeInteger
;
1019 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1022 v
.type
= FcTypeVoid
;
1025 FcValueDestroy (vl
);
1028 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1034 v
.type
= FcTypeInteger
;
1035 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1038 v
.type
= FcTypeVoid
;
1041 FcValueDestroy (vl
);
1044 v
.type
= FcTypeVoid
;
1050 static FcValueList
*
1051 FcConfigMatchValueList (FcPattern
*p
,
1053 FcValueList
*values
)
1055 FcValueList
*ret
= 0;
1056 FcExpr
*e
= t
->expr
;
1062 /* Compute the value of the match expression */
1063 if (e
->op
== FcOpComma
)
1065 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1066 e
= e
->u
.tree
.right
;
1070 value
= FcConfigEvaluate (p
, e
);
1074 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1076 /* Compare the pattern value to the match expression value */
1077 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1084 if (t
->qual
== FcQualAll
)
1091 FcValueDestroy (value
);
1096 static FcValueList
*
1097 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1103 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1106 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1107 if (e
->op
== FcOpComma
)
1109 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1110 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1114 l
->value
= FcConfigEvaluate (p
, e
);
1115 l
->next
= FcValueListPtrCreateDynamic(0);
1117 l
->binding
= binding
;
1118 if (l
->value
.type
== FcTypeVoid
)
1120 FcValueList
*next
= FcValueListPtrU(l
->next
);
1122 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1131 FcConfigAdd (FcValueListPtr
*head
,
1132 FcValueList
*position
,
1136 FcValueListPtr
*prev
, last
, v
;
1137 FcValueBinding sameBinding
;
1140 sameBinding
= position
->binding
;
1142 sameBinding
= FcValueBindingWeak
;
1143 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1144 v
= FcValueListPtrU(v
)->next
)
1145 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1146 FcValueListPtrU(v
)->binding
= sameBinding
;
1150 prev
= &position
->next
;
1152 for (prev
= head
; FcValueListPtrU(*prev
);
1153 prev
= &(FcValueListPtrU(*prev
)->next
))
1160 for (prev
= head
; FcValueListPtrU(*prev
);
1161 prev
= &(FcValueListPtrU(*prev
)->next
))
1163 if (FcValueListPtrU(*prev
) == position
)
1170 if (FcDebug () & FC_DBG_EDIT
)
1172 if (!FcValueListPtrU(*prev
))
1173 printf ("position not on list\n");
1177 if (FcDebug () & FC_DBG_EDIT
)
1179 printf ("%s list before ", append
? "Append" : "Prepend");
1180 FcValueListPrint (*head
);
1186 last
= FcValueListPtrCreateDynamic(new);
1187 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1188 last
= FcValueListPtrU(last
)->next
;
1190 FcValueListPtrU(last
)->next
= *prev
;
1191 *prev
= FcValueListPtrCreateDynamic(new);
1194 if (FcDebug () & FC_DBG_EDIT
)
1196 printf ("%s list after ", append
? "Append" : "Prepend");
1197 FcValueListPrint (*head
);
1205 FcConfigDel (FcValueListPtr
*head
,
1206 FcValueList
*position
)
1208 FcValueListPtr
*prev
;
1210 for (prev
= head
; FcValueListPtrU(*prev
);
1211 prev
= &(FcValueListPtrU(*prev
)->next
))
1213 if (FcValueListPtrU(*prev
) == position
)
1215 *prev
= position
->next
;
1216 position
->next
= FcValueListPtrCreateDynamic(0);
1217 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1224 FcConfigPatternAdd (FcPattern
*p
,
1231 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1235 FcConfigAdd (&e
->values
, 0, append
, list
);
1240 * Delete all values associated with a field
1243 FcConfigPatternDel (FcPattern
*p
,
1246 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1249 while (FcValueListPtrU(e
->values
))
1250 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1254 FcConfigPatternCanon (FcPattern
*p
,
1257 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1260 if (!FcValueListPtrU(e
->values
))
1261 FcPatternDel (p
, object
);
1265 FcConfigSubstituteWithPat (FcConfig
*config
,
1280 config
= FcConfigGetCurrent ();
1285 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1286 if (!st
&& config
->maxObjects
)
1288 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1290 if (FcDebug () & FC_DBG_EDIT
)
1292 printf ("FcConfigSubstitute ");
1295 if (kind
== FcMatchPattern
)
1296 s
= config
->substPattern
;
1298 s
= config
->substFont
;
1299 for (; s
; s
= s
->next
)
1302 * Check the tests to see if
1303 * they all match the pattern
1305 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1307 if (FcDebug () & FC_DBG_EDIT
)
1309 printf ("FcConfigSubstitute test ");
1313 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1318 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1322 * If there's no such field in the font,
1323 * then FcQualAll matches while FcQualAny does not
1327 if (t
->qual
== FcQualAll
)
1336 * Check to see if there is a match, mark the location
1337 * to apply match-relative edits
1339 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1342 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1344 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1349 if (FcDebug () & FC_DBG_EDIT
)
1350 printf ("No match\n");
1353 if (FcDebug () & FC_DBG_EDIT
)
1355 printf ("Substitute ");
1358 for (e
= s
->edit
; e
; e
= e
->next
)
1361 * Evaluate the list of expressions
1363 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1365 * Locate any test associated with this field, skipping
1366 * tests associated with the pattern when substituting in
1369 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1371 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1372 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1373 (FcChar8
*) e
->field
))
1376 * KLUDGE - the pattern may have been reallocated or
1377 * things may have been inserted or deleted above
1378 * this element by other edits. Go back and find
1381 if (e
!= s
->edit
&& st
[i
].elt
)
1382 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1391 * If there was a test, then replace the matched
1392 * value with the new list of values
1396 FcValueList
*thisValue
= st
[i
].value
;
1397 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1400 * Append the new list of values after the current value
1402 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1404 * Delete the marked value
1406 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1408 * Adjust any pointers into the value list to ensure
1409 * future edits occur at the same place
1411 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1413 if (st
[i
].value
== thisValue
)
1414 st
[i
].value
= nextValue
;
1418 /* fall through ... */
1419 case FcOpAssignReplace
:
1421 * Delete all of the values and insert
1424 FcConfigPatternDel (p
, e
->field
);
1425 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1427 * Adjust any pointers into the value list as they no
1428 * longer point to anything valid
1432 FcPatternElt
*thisElt
= st
[i
].elt
;
1433 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1435 if (st
[i
].elt
== thisElt
)
1443 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1446 /* fall through ... */
1447 case FcOpPrependFirst
:
1448 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1453 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1456 /* fall through ... */
1457 case FcOpAppendLast
:
1458 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1465 * Now go through the pattern and eliminate
1466 * any properties without data
1468 for (e
= s
->edit
; e
; e
= e
->next
)
1469 FcConfigPatternCanon (p
, e
->field
);
1471 if (FcDebug () & FC_DBG_EDIT
)
1473 printf ("FcConfigSubstitute edit");
1477 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1479 if (FcDebug () & FC_DBG_EDIT
)
1481 printf ("FcConfigSubstitute done");
1488 FcConfigSubstitute (FcConfig
*config
,
1492 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1495 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1497 static FcChar8 fontconfig_path
[1000] = "";
1500 DllMain (HINSTANCE hinstDLL
,
1506 switch (fdwReason
) {
1507 case DLL_PROCESS_ATTACH
:
1508 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1509 sizeof (fontconfig_path
)))
1512 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1513 * assume it's a Unix-style installation tree, and use
1514 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1515 * folder where the DLL is as FONTCONFIG_PATH.
1517 p
= strrchr (fontconfig_path
, '\\');
1521 p
= strrchr (fontconfig_path
, '\\');
1522 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1523 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1525 strcat (fontconfig_path
, "\\etc\\fonts");
1528 fontconfig_path
[0] = '\0';
1536 #undef FONTCONFIG_PATH
1537 #define FONTCONFIG_PATH fontconfig_path
1539 #else /* !(_WIN32 && PIC) */
1541 #endif /* !(_WIN32 && PIC) */
1543 #ifndef FONTCONFIG_FILE
1544 #define FONTCONFIG_FILE "fonts.conf"
1548 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1553 dir
= (FcChar8
*) "";
1554 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1558 strcpy ((char *) path
, (const char *) dir
);
1559 /* make sure there's a single separator */
1561 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1562 path
[strlen((char *) path
)-1] != '\\')) &&
1565 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1566 strcat ((char *) path
, "\\");
1568 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1569 strcat ((char *) path
, "/");
1571 strcat ((char *) path
, (char *) file
);
1573 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1574 if (access ((char *) path
, R_OK
) == 0)
1582 FcConfigGetPath (void)
1585 FcChar8
*env
, *e
, *colon
;
1590 npath
= 2; /* default dir + null */
1591 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1597 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1600 path
= calloc (npath
, sizeof (FcChar8
*));
1610 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1612 colon
= e
+ strlen ((char *) e
);
1613 path
[i
] = malloc (colon
- e
+ 1);
1616 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1617 path
[i
][colon
- e
] = '\0';
1626 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1627 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1630 strcpy ((char *) path
[i
], (const char *) dir
);
1634 for (i
= 0; path
[i
]; i
++)
1642 FcConfigFreePath (FcChar8
**path
)
1646 for (p
= path
; *p
; p
++)
1651 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1656 if (_FcConfigHomeEnabled
)
1658 char *home
= getenv ("HOME");
1662 home
= getenv ("USERPROFILE");
1665 return (FcChar8
*) home
;
1671 FcConfigEnableHome (FcBool enable
)
1673 FcBool prev
= _FcConfigHomeEnabled
;
1674 _FcConfigHomeEnabled
= enable
;
1679 FcConfigFilename (const FcChar8
*url
)
1681 FcChar8
*file
, *dir
, **path
, **p
;
1685 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1687 url
= (FcChar8
*) FONTCONFIG_FILE
;
1692 if (isalpha (*url
) &&
1694 (url
[2] == '/' || url
[2] == '\\'))
1700 dir
= FcConfigHome ();
1702 file
= FcConfigFileExists (dir
, url
+ 1);
1711 file
= FcConfigFileExists (0, url
);
1714 path
= FcConfigGetPath ();
1717 for (p
= path
; *p
; p
++)
1719 file
= FcConfigFileExists (*p
, url
);
1723 FcConfigFreePath (path
);
1730 * Manage the application-specific fonts
1734 FcConfigAppFontAddFile (FcConfig
*config
,
1735 const FcChar8
*file
)
1744 config
= FcConfigGetCurrent ();
1749 subdirs
= FcStrSetCreate ();
1753 set
= FcConfigGetFonts (config
, FcSetApplication
);
1756 set
= FcFontSetCreate ();
1759 FcStrSetDestroy (subdirs
);
1762 FcConfigSetFonts (config
, set
, FcSetApplication
);
1765 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1767 FcStrSetDestroy (subdirs
);
1770 if ((sublist
= FcStrListCreate (subdirs
)))
1772 while ((subdir
= FcStrListNext (sublist
)))
1774 FcConfigAppFontAddDir (config
, subdir
);
1776 FcStrListDone (sublist
);
1782 FcConfigAppFontAddDir (FcConfig
*config
,
1792 config
= FcConfigGetCurrent ();
1796 subdirs
= FcStrSetCreate ();
1800 set
= FcConfigGetFonts (config
, FcSetApplication
);
1803 set
= FcFontSetCreate ();
1806 FcStrSetDestroy (subdirs
);
1809 FcConfigSetFonts (config
, set
, FcSetApplication
);
1812 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1814 FcStrSetDestroy (subdirs
);
1817 if ((sublist
= FcStrListCreate (subdirs
)))
1819 while ((subdir
= FcStrListNext (sublist
)))
1821 FcConfigAppFontAddDir (config
, subdir
);
1823 FcStrListDone (sublist
);
1829 FcConfigAppFontClear (FcConfig
*config
)
1833 config
= FcConfigGetCurrent ();
1838 FcConfigSetFonts (config
, 0, FcSetApplication
);
1842 * Manage filename-based font source selectors
1846 FcConfigGlobAdd (FcConfig
*config
,
1847 const FcChar8
*glob
,
1850 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1852 return FcStrSetAdd (set
, glob
);
1856 FcConfigGlobMatch (const FcChar8
*glob
,
1857 const FcChar8
*string
)
1861 while ((c
= *glob
++))
1865 /* short circuit common case */
1868 /* short circuit another common case */
1869 if (strchr ((char *) glob
, '*') == 0)
1870 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1873 if (FcConfigGlobMatch (glob
, string
))
1879 if (*string
++ == '\0')
1888 return *string
== '\0';
1892 FcConfigGlobsMatch (const FcStrSet
*globs
,
1893 const FcChar8
*string
)
1897 for (i
= 0; i
< globs
->num
; i
++)
1898 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1904 FcConfigAcceptFilename (FcConfig
*config
,
1905 const FcChar8
*filename
)
1907 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1909 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1915 * Manage font-pattern based font source selectors
1919 FcConfigPatternsAdd (FcConfig
*config
,
1923 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1925 return FcFontSetAdd (set
, pattern
);
1929 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1930 const FcPattern
*font
)
1934 for (i
= 0; i
< patterns
->nfont
; i
++)
1935 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1941 FcConfigAcceptFont (FcConfig
*config
,
1942 const FcPattern
*font
)
1944 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1946 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))