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
));
142 typedef struct _FcFileTime
{
148 FcConfigNewestFile (FcStrSet
*files
)
150 FcStrList
*list
= FcStrListCreate (files
);
151 FcFileTime newest
= { 0, FcFalse
};
157 while ((file
= FcStrListNext (list
)))
158 if (stat ((char *) file
, &statb
) == 0)
159 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
162 newest
.time
= statb
.st_mtime
;
164 FcStrListDone (list
);
170 FcConfigUptoDate (FcConfig
*config
)
172 FcFileTime config_time
, font_time
;
173 time_t now
= time(0);
176 config
= FcConfigGetCurrent ();
180 config_time
= FcConfigNewestFile (config
->configFiles
);
181 font_time
= FcConfigNewestFile (config
->fontDirs
);
182 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
183 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
187 config
->rescanTime
= now
;
192 FcSubstDestroy (FcSubst
*s
)
200 FcTestDestroy (s
->test
);
202 FcEditDestroy (s
->edit
);
204 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
210 FcConfigDestroy (FcConfig
*config
)
214 if (config
== _fcConfig
)
217 FcStrSetDestroy (config
->configDirs
);
218 FcStrSetDestroy (config
->fontDirs
);
219 FcStrSetDestroy (config
->configFiles
);
220 FcStrSetDestroy (config
->acceptGlobs
);
221 FcStrSetDestroy (config
->rejectGlobs
);
222 FcFontSetDestroy (config
->acceptPatterns
);
223 FcFontSetDestroy (config
->rejectPatterns
);
226 FcBlanksDestroy (config
->blanks
);
229 FcStrFree (config
->cache
);
231 FcSubstDestroy (config
->substPattern
);
232 FcSubstDestroy (config
->substFont
);
233 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
234 if (config
->fonts
[set
])
235 FcFontSetDestroy (config
->fonts
[set
]);
238 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
242 * Scan the current list of directories in the configuration
243 * and build the set of available fonts. Update the
244 * per-user cache file to reflect the new configuration
248 FcConfigBuildFonts (FcConfig
*config
)
251 FcGlobalCache
*cache
;
257 FcGlobalCacheLoad (cache
, config
->cache
);
259 if (config
->fonts
[FcSetSystem
])
262 fonts
= FcFontSetCreate ();
267 cache
= FcGlobalCacheCreate ();
272 list
= FcConfigGetFontDirs (config
);
276 while ((dir
= FcStrListNext (list
)))
278 if (FcDebug () & FC_DBG_FONTSET
)
279 printf ("scan dir %s\n", dir
);
280 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
281 config
->blanks
, dir
, FcFalse
, config
);
284 FcStrListDone (list
);
286 if (FcDebug () & FC_DBG_FONTSET
)
287 FcFontSetPrint (fonts
);
291 FcGlobalCacheSave (cache
, config
->cache
);
292 FcGlobalCacheDestroy (cache
);
295 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
299 FcFontSetDestroy (fonts
);
305 FcConfigSetCurrent (FcConfig
*config
)
308 if (!FcConfigBuildFonts (config
))
312 FcConfigDestroy (_fcConfig
);
318 FcConfigGetCurrent (void)
327 FcConfigAddConfigDir (FcConfig
*config
,
330 return FcStrSetAddFilename (config
->configDirs
, d
);
334 FcConfigGetConfigDirs (FcConfig
*config
)
338 config
= FcConfigGetCurrent ();
342 return FcStrListCreate (config
->configDirs
);
346 FcConfigAddFontDir (FcConfig
*config
,
349 return FcStrSetAddFilename (config
->fontDirs
, d
);
353 FcConfigAddDir (FcConfig
*config
,
356 return (FcConfigAddConfigDir (config
, d
) &&
357 FcConfigAddFontDir (config
, d
));
361 FcConfigGetFontDirs (FcConfig
*config
)
365 config
= FcConfigGetCurrent ();
369 return FcStrListCreate (config
->fontDirs
);
373 FcConfigAddConfigFile (FcConfig
*config
,
377 FcChar8
*file
= FcConfigFilename (f
);
382 ret
= FcStrSetAdd (config
->configFiles
, file
);
388 FcConfigGetConfigFiles (FcConfig
*config
)
392 config
= FcConfigGetCurrent ();
396 return FcStrListCreate (config
->configFiles
);
400 FcConfigSetCache (FcConfig
*config
,
403 FcChar8
*new = FcStrCopyFilename (c
);
408 FcStrFree (config
->cache
);
414 FcConfigGetCache (FcConfig
*config
)
418 config
= FcConfigGetCurrent ();
422 return config
->cache
;
426 FcConfigGetFonts (FcConfig
*config
,
431 config
= FcConfigGetCurrent ();
435 return config
->fonts
[set
];
439 FcConfigSetFonts (FcConfig
*config
,
443 if (config
->fonts
[set
])
444 FcFontSetDestroy (config
->fonts
[set
]);
445 config
->fonts
[set
] = fonts
;
451 FcConfigGetBlanks (FcConfig
*config
)
455 config
= FcConfigGetCurrent ();
459 return config
->blanks
;
463 FcConfigAddBlank (FcConfig
*config
,
471 b
= FcBlanksCreate ();
475 if (!FcBlanksAdd (b
, blank
))
482 FcConfigGetRescanInverval (FcConfig
*config
)
486 config
= FcConfigGetCurrent ();
490 return config
->rescanInterval
;
494 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
498 config
= FcConfigGetCurrent ();
502 config
->rescanInterval
= rescanInterval
;
507 FcConfigAddEdit (FcConfig
*config
,
512 FcSubst
*subst
, **prev
;
516 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
519 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
520 if (kind
== FcMatchPattern
)
521 prev
= &config
->substPattern
;
523 prev
= &config
->substFont
;
524 for (; *prev
; prev
= &(*prev
)->next
);
530 for (t
= test
; t
; t
= t
->next
)
532 if (t
->kind
== FcMatchDefault
)
536 if (config
->maxObjects
< num
)
537 config
->maxObjects
= num
;
538 if (FcDebug () & FC_DBG_EDIT
)
540 printf ("Add Subst ");
541 FcSubstPrint (subst
);
546 typedef struct _FcSubState
{
552 FcConfigPromote (FcValue v
, FcValue u
)
554 if (v
.type
== FcTypeInteger
)
556 v
.type
= FcTypeDouble
;
557 v
.u
.d
= (double) v
.u
.i
;
559 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
561 v
.u
.mi
= FcIdentityMatrix
;
562 v
.type
= FcTypeMatrix
;
564 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
566 v
.u
.li
= FcLangSetPtrCreateDynamic(FcLangSetPromote
567 (FcObjectPtrU(v
.u
.si
)));
568 v
.type
= FcTypeLangSet
;
574 FcConfigCompareValue (const FcValue left_o
,
576 const FcValue right_o
)
578 FcValue left
= left_o
;
579 FcValue right
= right_o
;
580 FcBool ret
= FcFalse
;
582 left
= FcConfigPromote (left
, right
);
583 right
= FcConfigPromote (right
, left
);
584 if (left
.type
== right
.type
)
588 break; /* FcConfigPromote prevents this from happening */
594 ret
= left
.u
.d
== right
.u
.d
;
597 case FcOpNotContains
:
598 ret
= left
.u
.d
!= right
.u
.d
;
601 ret
= left
.u
.d
< right
.u
.d
;
604 ret
= left
.u
.d
<= right
.u
.d
;
607 ret
= left
.u
.d
> right
.u
.d
;
610 ret
= left
.u
.d
>= right
.u
.d
;
621 ret
= left
.u
.b
== right
.u
.b
;
624 case FcOpNotContains
:
625 ret
= left
.u
.b
!= right
.u
.b
;
635 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
636 FcObjectPtrU(right
.u
.si
)) == 0;
639 ret
= FcStrStrIgnoreCase (FcObjectPtrU(left
.u
.si
),
640 FcObjectPtrU(right
.u
.si
)) != 0;
643 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
644 FcObjectPtrU(right
.u
.si
)) != 0;
646 case FcOpNotContains
:
647 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
648 FcObjectPtrU(right
.u
.si
)) == 0;
659 ret
= FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
662 case FcOpNotContains
:
663 ret
= !FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
673 /* left contains right if right is a subset of left */
674 ret
= FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
676 case FcOpNotContains
:
677 /* left contains right if right is a subset of left */
678 ret
= !FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
681 ret
= FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
684 ret
= !FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
694 ret
= FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
696 case FcOpNotContains
:
697 ret
= !FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
700 ret
= FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
703 ret
= !FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
725 ret
= left
.u
.f
== right
.u
.f
;
728 case FcOpNotContains
:
729 ret
= left
.u
.f
!= right
.u
.f
;
739 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
746 #define _FcDoubleFloor(d) ((int) (d))
747 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
748 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
749 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
750 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
751 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
754 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
762 v
.type
= FcTypeInteger
;
766 v
.type
= FcTypeDouble
;
770 v
.type
= FcTypeString
;
771 v
.u
.si
= FcObjectStaticName(e
->u
.sval
);
775 v
.type
= FcTypeMatrix
;
776 v
.u
.mi
= FcMatrixPtrCreateDynamic(e
->u
.mval
);
780 v
.type
= FcTypeCharSet
;
781 v
.u
.ci
= FcCharSetPtrCreateDynamic(e
->u
.cval
);
789 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
790 if (r
!= FcResultMatch
)
794 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
795 v
.type
= FcTypeInteger
;
800 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
801 if (vl
.type
== FcTypeBool
)
804 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
806 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
819 case FcOpNotContains
:
821 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
822 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
824 v
.u
.b
= FcConfigCompareValue (vl
, e
->op
, vr
);
834 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
835 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
836 vl
= FcConfigPromote (vl
, vr
);
837 vr
= FcConfigPromote (vr
, vl
);
838 if (vl
.type
== vr
.type
)
844 v
.type
= FcTypeDouble
;
845 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
848 v
.type
= FcTypeDouble
;
849 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
852 v
.type
= FcTypeDouble
;
853 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
856 v
.type
= FcTypeDouble
;
857 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
863 if (v
.type
== FcTypeDouble
&&
864 v
.u
.d
== (double) (int) v
.u
.d
)
866 v
.type
= FcTypeInteger
;
874 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
878 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
888 v
.type
= FcTypeString
;
889 v
.u
.si
= FcObjectStaticName
890 (FcStrPlus (FcObjectPtrU(vl
.u
.si
),
891 FcObjectPtrU(vr
.u
.si
)));
893 if (!FcObjectPtrU(v
.u
.si
))
904 v
.type
= FcTypeMatrix
;
905 m
= malloc (sizeof (FcMatrix
));
908 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
909 FcMatrixMultiply (m
, FcMatrixPtrU(vl
.u
.mi
),
910 FcMatrixPtrU(vr
.u
.mi
));
911 v
.u
.mi
= FcMatrixPtrCreateDynamic(m
);
934 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
947 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
953 v
.type
= FcTypeInteger
;
954 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
963 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
969 v
.type
= FcTypeInteger
;
970 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
979 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
985 v
.type
= FcTypeInteger
;
986 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
995 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1001 v
.type
= FcTypeInteger
;
1002 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1005 v
.type
= FcTypeVoid
;
1008 FcValueDestroy (vl
);
1011 v
.type
= FcTypeVoid
;
1017 static FcValueList
*
1018 FcConfigMatchValueList (FcPattern
*p
,
1020 FcValueList
*values
)
1022 FcValueList
*ret
= 0;
1023 FcExpr
*e
= t
->expr
;
1029 /* Compute the value of the match expression */
1030 if (e
->op
== FcOpComma
)
1032 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1033 e
= e
->u
.tree
.right
;
1037 value
= FcConfigEvaluate (p
, e
);
1041 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1043 /* Compare the pattern value to the match expression value */
1044 if (FcConfigCompareValue (v
->value
, t
->op
, value
))
1051 if (t
->qual
== FcQualAll
)
1058 FcValueDestroy (value
);
1063 static FcValueList
*
1064 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1071 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1074 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1075 if (e
->op
== FcOpComma
)
1077 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1078 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1082 l
->value
= FcConfigEvaluate (p
, e
);
1083 l
->next
= FcValueListPtrCreateDynamic(0);
1085 l
->binding
= binding
;
1086 lp
= FcValueListPtrCreateDynamic(l
);
1087 while (FcValueListPtrU(lp
) && FcValueListPtrU(lp
)->value
.type
== FcTypeVoid
)
1089 FcValueListPtr next
= FcValueListPtrU(lp
)->next
;
1091 if (lp
.storage
== FcStorageDynamic
)
1093 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1102 FcConfigAdd (FcValueListPtr
*head
,
1103 FcValueList
*position
,
1107 FcValueListPtr
*prev
, last
, v
;
1108 FcValueBinding sameBinding
;
1111 sameBinding
= position
->binding
;
1113 sameBinding
= FcValueBindingWeak
;
1114 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1115 v
= FcValueListPtrU(v
)->next
)
1116 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1117 FcValueListPtrU(v
)->binding
= sameBinding
;
1121 prev
= &position
->next
;
1123 for (prev
= head
; FcValueListPtrU(*prev
);
1124 prev
= &(FcValueListPtrU(*prev
)->next
))
1131 for (prev
= head
; FcValueListPtrU(*prev
);
1132 prev
= &(FcValueListPtrU(*prev
)->next
))
1134 if (FcValueListPtrU(*prev
) == position
)
1141 if (FcDebug () & FC_DBG_EDIT
)
1143 if (!FcValueListPtrU(*prev
))
1144 printf ("position not on list\n");
1148 if (FcDebug () & FC_DBG_EDIT
)
1150 printf ("%s list before ", append
? "Append" : "Prepend");
1151 FcValueListPrint (*head
);
1157 last
= FcValueListPtrCreateDynamic(new);
1158 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1159 last
= FcValueListPtrU(last
)->next
;
1161 FcValueListPtrU(last
)->next
= *prev
;
1162 *prev
= FcValueListPtrCreateDynamic(new);
1165 if (FcDebug () & FC_DBG_EDIT
)
1167 printf ("%s list after ", append
? "Append" : "Prepend");
1168 FcValueListPrint (*head
);
1176 FcConfigDel (FcValueListPtr
*head
,
1177 FcValueList
*position
)
1179 FcValueListPtr
*prev
;
1181 for (prev
= head
; FcValueListPtrU(*prev
);
1182 prev
= &(FcValueListPtrU(*prev
)->next
))
1184 if (FcValueListPtrU(*prev
) == position
)
1186 *prev
= position
->next
;
1187 position
->next
= FcValueListPtrCreateDynamic(0);
1188 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1195 FcConfigPatternAdd (FcPattern
*p
,
1202 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1206 FcConfigAdd (&e
->values
, 0, append
, list
);
1211 * Delete all values associated with a field
1214 FcConfigPatternDel (FcPattern
*p
,
1217 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1220 while (FcValueListPtrU(e
->values
))
1221 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1225 FcConfigPatternCanon (FcPattern
*p
,
1228 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1231 if (!FcValueListPtrU(e
->values
))
1232 FcPatternDel (p
, object
);
1236 FcConfigSubstituteWithPat (FcConfig
*config
,
1251 config
= FcConfigGetCurrent ();
1256 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1257 if (!st
&& config
->maxObjects
)
1259 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1261 if (FcDebug () & FC_DBG_EDIT
)
1263 printf ("FcConfigSubstitute ");
1266 if (kind
== FcMatchPattern
)
1267 s
= config
->substPattern
;
1269 s
= config
->substFont
;
1270 for (; s
; s
= s
->next
)
1273 * Check the tests to see if
1274 * they all match the pattern
1276 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1278 if (FcDebug () & FC_DBG_EDIT
)
1280 printf ("FcConfigSubstitute test ");
1284 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1289 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1293 * If there's no such field in the font,
1294 * then FcQualAll matches while FcQualAny does not
1298 if (t
->qual
== FcQualAll
)
1307 * Check to see if there is a match, mark the location
1308 * to apply match-relative edits
1310 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1313 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1315 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1320 if (FcDebug () & FC_DBG_EDIT
)
1321 printf ("No match\n");
1324 if (FcDebug () & FC_DBG_EDIT
)
1326 printf ("Substitute ");
1329 for (e
= s
->edit
; e
; e
= e
->next
)
1332 * Evaluate the list of expressions
1334 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1336 * Locate any test associated with this field, skipping
1337 * tests associated with the pattern when substituting in
1340 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1342 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1343 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1344 (FcChar8
*) e
->field
))
1347 * KLUDGE - the pattern may have been reallocated or
1348 * things may have been inserted or deleted above
1349 * this element by other edits. Go back and find
1352 if (e
!= s
->edit
&& st
[i
].elt
)
1353 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1362 * If there was a test, then replace the matched
1363 * value with the new list of values
1367 FcValueList
*thisValue
= st
[i
].value
;
1368 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1371 * Append the new list of values after the current value
1373 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1375 * Delete the marked value
1377 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1379 * Adjust any pointers into the value list to ensure
1380 * future edits occur at the same place
1382 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1384 if (st
[i
].value
== thisValue
)
1385 st
[i
].value
= nextValue
;
1389 /* fall through ... */
1390 case FcOpAssignReplace
:
1392 * Delete all of the values and insert
1395 FcConfigPatternDel (p
, e
->field
);
1396 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1398 * Adjust any pointers into the value list as they no
1399 * longer point to anything valid
1403 FcPatternElt
*thisElt
= st
[i
].elt
;
1404 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1406 if (st
[i
].elt
== thisElt
)
1414 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1417 /* fall through ... */
1418 case FcOpPrependFirst
:
1419 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1424 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1427 /* fall through ... */
1428 case FcOpAppendLast
:
1429 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1436 * Now go through the pattern and eliminate
1437 * any properties without data
1439 for (e
= s
->edit
; e
; e
= e
->next
)
1440 FcConfigPatternCanon (p
, e
->field
);
1442 if (FcDebug () & FC_DBG_EDIT
)
1444 printf ("FcConfigSubstitute edit");
1448 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1450 if (FcDebug () & FC_DBG_EDIT
)
1452 printf ("FcConfigSubstitute done");
1459 FcConfigSubstitute (FcConfig
*config
,
1463 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1466 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1468 static FcChar8 fontconfig_path
[1000] = "";
1471 DllMain (HINSTANCE hinstDLL
,
1477 switch (fdwReason
) {
1478 case DLL_PROCESS_ATTACH
:
1479 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1480 sizeof (fontconfig_path
)))
1483 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1484 * assume it's a Unix-style installation tree, and use
1485 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1486 * folder where the DLL is as FONTCONFIG_PATH.
1488 p
= strrchr (fontconfig_path
, '\\');
1492 p
= strrchr (fontconfig_path
, '\\');
1493 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1494 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1496 strcat (fontconfig_path
, "\\etc\\fonts");
1499 fontconfig_path
[0] = '\0';
1507 #undef FONTCONFIG_PATH
1508 #define FONTCONFIG_PATH fontconfig_path
1510 #else /* !(_WIN32 && PIC) */
1512 #endif /* !(_WIN32 && PIC) */
1514 #ifndef FONTCONFIG_FILE
1515 #define FONTCONFIG_FILE "fonts.conf"
1519 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1524 dir
= (FcChar8
*) "";
1525 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1529 strcpy ((char *) path
, (const char *) dir
);
1530 /* make sure there's a single separator */
1532 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1533 path
[strlen((char *) path
)-1] != '\\')) &&
1536 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1537 strcat ((char *) path
, "\\");
1539 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1540 strcat ((char *) path
, "/");
1542 strcat ((char *) path
, (char *) file
);
1544 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1545 if (access ((char *) path
, R_OK
) == 0)
1553 FcConfigGetPath (void)
1556 FcChar8
*env
, *e
, *colon
;
1561 npath
= 2; /* default dir + null */
1562 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1568 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1571 path
= calloc (npath
, sizeof (FcChar8
*));
1581 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1583 colon
= e
+ strlen ((char *) e
);
1584 path
[i
] = malloc (colon
- e
+ 1);
1587 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1588 path
[i
][colon
- e
] = '\0';
1597 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1598 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1601 strcpy ((char *) path
[i
], (const char *) dir
);
1605 for (i
= 0; path
[i
]; i
++)
1613 FcConfigFreePath (FcChar8
**path
)
1617 for (p
= path
; *p
; p
++)
1622 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1627 if (_FcConfigHomeEnabled
)
1629 char *home
= getenv ("HOME");
1633 home
= getenv ("USERPROFILE");
1642 FcConfigEnableHome (FcBool enable
)
1644 FcBool prev
= _FcConfigHomeEnabled
;
1645 _FcConfigHomeEnabled
= enable
;
1650 FcConfigFilename (const FcChar8
*url
)
1652 FcChar8
*file
, *dir
, **path
, **p
;
1656 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1658 url
= (FcChar8
*) FONTCONFIG_FILE
;
1663 if (isalpha (*url
) &&
1665 (url
[2] == '/' || url
[2] == '\\'))
1671 dir
= FcConfigHome ();
1673 file
= FcConfigFileExists (dir
, url
+ 1);
1682 file
= FcConfigFileExists (0, url
);
1685 path
= FcConfigGetPath ();
1688 for (p
= path
; *p
; p
++)
1690 file
= FcConfigFileExists (*p
, url
);
1694 FcConfigFreePath (path
);
1701 * Manage the application-specific fonts
1705 FcConfigAppFontAddFile (FcConfig
*config
,
1706 const FcChar8
*file
)
1715 config
= FcConfigGetCurrent ();
1720 subdirs
= FcStrSetCreate ();
1724 set
= FcConfigGetFonts (config
, FcSetApplication
);
1727 set
= FcFontSetCreate ();
1730 FcStrSetDestroy (subdirs
);
1733 FcConfigSetFonts (config
, set
, FcSetApplication
);
1736 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1738 FcStrSetDestroy (subdirs
);
1741 if ((sublist
= FcStrListCreate (subdirs
)))
1743 while ((subdir
= FcStrListNext (sublist
)))
1745 FcConfigAppFontAddDir (config
, subdir
);
1747 FcStrListDone (sublist
);
1753 FcConfigAppFontAddDir (FcConfig
*config
,
1763 config
= FcConfigGetCurrent ();
1767 subdirs
= FcStrSetCreate ();
1771 set
= FcConfigGetFonts (config
, FcSetApplication
);
1774 set
= FcFontSetCreate ();
1777 FcStrSetDestroy (subdirs
);
1780 FcConfigSetFonts (config
, set
, FcSetApplication
);
1783 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1785 FcStrSetDestroy (subdirs
);
1788 if ((sublist
= FcStrListCreate (subdirs
)))
1790 while ((subdir
= FcStrListNext (sublist
)))
1792 FcConfigAppFontAddDir (config
, subdir
);
1794 FcStrListDone (sublist
);
1800 FcConfigAppFontClear (FcConfig
*config
)
1804 config
= FcConfigGetCurrent ();
1809 FcConfigSetFonts (config
, 0, FcSetApplication
);
1813 * Manage filename-based font source selectors
1817 FcConfigGlobAdd (FcConfig
*config
,
1818 const FcChar8
*glob
,
1821 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1823 return FcStrSetAdd (set
, glob
);
1827 FcConfigGlobMatch (const FcChar8
*glob
,
1828 const FcChar8
*string
)
1832 while ((c
= *glob
++))
1836 /* short circuit common case */
1839 /* short circuit another common case */
1840 if (strchr ((char *) glob
, '*') == 0)
1841 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1844 if (FcConfigGlobMatch (glob
, string
))
1850 if (*string
++ == '\0')
1859 return *string
== '\0';
1863 FcConfigGlobsMatch (const FcStrSet
*globs
,
1864 const FcChar8
*string
)
1868 for (i
= 0; i
< globs
->num
; i
++)
1869 if (FcConfigGlobMatch (FcStrSetGet(globs
, i
), string
))
1875 FcConfigAcceptFilename (FcConfig
*config
,
1876 const FcChar8
*filename
)
1878 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1880 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1886 * Manage font-pattern based font source selectors
1890 FcConfigPatternsAdd (FcConfig
*config
,
1894 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1896 return FcFontSetAdd (set
, pattern
);
1900 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1901 const FcPattern
*font
)
1905 for (i
= 0; i
< patterns
->nfont
; i
++)
1906 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1912 FcConfigAcceptFont (FcConfig
*config
,
1913 const FcPattern
*font
)
1915 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1917 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))