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
++)
311 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
312 FcConfigAcceptFilename (config
, (FcChar8
*)FcPatternFindFullFname(cached_fonts
->fonts
[i
])))
313 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
315 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
317 cached_fonts
->nfont
= 0;
318 FcFontSetDestroy (cached_fonts
);
321 if (FcDebug () & FC_DBG_FONTSET
)
322 FcFontSetPrint (fonts
);
325 FcGlobalCacheSave (cache
, config
->cache
);
326 FcGlobalCacheDestroy (cache
);
327 FcStrSetDestroy (oldDirs
);
329 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
333 FcStrSetDestroy (oldDirs
);
335 FcFontSetDestroy (fonts
);
341 FcConfigSetCurrent (FcConfig
*config
)
344 if (!FcConfigBuildFonts (config
))
348 FcConfigDestroy (_fcConfig
);
354 FcConfigGetCurrent (void)
363 FcConfigAddConfigDir (FcConfig
*config
,
366 return FcStrSetAddFilename (config
->configDirs
, d
);
370 FcConfigGetConfigDirs (FcConfig
*config
)
374 config
= FcConfigGetCurrent ();
378 return FcStrListCreate (config
->configDirs
);
382 FcConfigAddFontDir (FcConfig
*config
,
385 return FcStrSetAddFilename (config
->fontDirs
, d
);
389 FcConfigAddDir (FcConfig
*config
,
392 return (FcConfigAddConfigDir (config
, d
) &&
393 FcConfigAddFontDir (config
, d
));
397 FcConfigGetFontDirs (FcConfig
*config
)
401 config
= FcConfigGetCurrent ();
405 return FcStrListCreate (config
->fontDirs
);
409 FcConfigAddConfigFile (FcConfig
*config
,
413 FcChar8
*file
= FcConfigFilename (f
);
418 ret
= FcStrSetAdd (config
->configFiles
, file
);
424 FcConfigGetConfigFiles (FcConfig
*config
)
428 config
= FcConfigGetCurrent ();
432 return FcStrListCreate (config
->configFiles
);
436 FcConfigSetCache (FcConfig
*config
,
439 FcChar8
*new = FcStrCopyFilename (c
);
444 FcStrFree (config
->cache
);
450 FcConfigGetCache (FcConfig
*config
)
454 config
= FcConfigGetCurrent ();
458 return config
->cache
;
462 FcConfigGetFonts (FcConfig
*config
,
467 config
= FcConfigGetCurrent ();
471 return config
->fonts
[set
];
475 FcConfigSetFonts (FcConfig
*config
,
479 if (config
->fonts
[set
])
480 FcFontSetDestroy (config
->fonts
[set
]);
481 config
->fonts
[set
] = fonts
;
487 FcConfigGetBlanks (FcConfig
*config
)
491 config
= FcConfigGetCurrent ();
495 return config
->blanks
;
499 FcConfigAddBlank (FcConfig
*config
,
507 b
= FcBlanksCreate ();
511 if (!FcBlanksAdd (b
, blank
))
518 FcConfigGetRescanInverval (FcConfig
*config
)
522 config
= FcConfigGetCurrent ();
526 return config
->rescanInterval
;
530 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
534 config
= FcConfigGetCurrent ();
538 config
->rescanInterval
= rescanInterval
;
543 FcConfigAddEdit (FcConfig
*config
,
548 FcSubst
*subst
, **prev
;
552 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
555 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
556 if (kind
== FcMatchPattern
)
557 prev
= &config
->substPattern
;
559 prev
= &config
->substFont
;
560 for (; *prev
; prev
= &(*prev
)->next
);
566 for (t
= test
; t
; t
= t
->next
)
568 if (t
->kind
== FcMatchDefault
)
572 if (config
->maxObjects
< num
)
573 config
->maxObjects
= num
;
574 if (FcDebug () & FC_DBG_EDIT
)
576 printf ("Add Subst ");
577 FcSubstPrint (subst
);
582 typedef struct _FcSubState
{
588 FcConfigPromote (FcValue v
, FcValue u
)
590 if (v
.type
== FcTypeInteger
)
592 v
.type
= FcTypeDouble
;
593 v
.u
.d
= (double) v
.u
.i
;
595 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
597 v
.u
.m
= &FcIdentityMatrix
;
598 v
.type
= FcTypeMatrix
;
600 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
602 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
603 v
.type
= FcTypeLangSet
;
609 FcConfigCompareValue (const FcValue
*left_o
,
611 const FcValue
*right_o
)
613 FcValue left
= FcValueCanonicalize(left_o
);
614 FcValue right
= FcValueCanonicalize(right_o
);
615 FcBool ret
= FcFalse
;
617 left
= FcConfigPromote (left
, right
);
618 right
= FcConfigPromote (right
, left
);
619 if (left
.type
== right
.type
)
623 break; /* FcConfigPromote prevents this from happening */
629 ret
= left
.u
.d
== right
.u
.d
;
632 case FcOpNotContains
:
633 ret
= left
.u
.d
!= right
.u
.d
;
636 ret
= left
.u
.d
< right
.u
.d
;
639 ret
= left
.u
.d
<= right
.u
.d
;
642 ret
= left
.u
.d
> right
.u
.d
;
645 ret
= left
.u
.d
>= right
.u
.d
;
656 ret
= left
.u
.b
== right
.u
.b
;
659 case FcOpNotContains
:
660 ret
= left
.u
.b
!= right
.u
.b
;
670 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
673 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
676 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
678 case FcOpNotContains
:
679 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
690 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
693 case FcOpNotContains
:
694 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
704 /* left contains right if right is a subset of left */
705 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
707 case FcOpNotContains
:
708 /* left contains right if right is a subset of left */
709 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
712 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
715 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
725 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
727 case FcOpNotContains
:
728 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
731 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
734 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
756 ret
= left
.u
.f
== right
.u
.f
;
759 case FcOpNotContains
:
760 ret
= left
.u
.f
!= right
.u
.f
;
770 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
777 #define _FcDoubleFloor(d) ((int) (d))
778 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
779 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
780 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
781 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
782 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
785 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
793 v
.type
= FcTypeInteger
;
797 v
.type
= FcTypeDouble
;
801 v
.type
= FcTypeString
;
802 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
806 v
.type
= FcTypeMatrix
;
811 v
.type
= FcTypeCharSet
;
820 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
821 if (r
!= FcResultMatch
)
825 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
826 v
.type
= FcTypeInteger
;
831 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
832 if (vl
.type
== FcTypeBool
)
835 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
837 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
850 case FcOpNotContains
:
852 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
853 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
855 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
865 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
866 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
867 vl
= FcConfigPromote (vl
, vr
);
868 vr
= FcConfigPromote (vr
, vl
);
869 if (vl
.type
== vr
.type
)
875 v
.type
= FcTypeDouble
;
876 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
879 v
.type
= FcTypeDouble
;
880 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
883 v
.type
= FcTypeDouble
;
884 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
887 v
.type
= FcTypeDouble
;
888 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
894 if (v
.type
== FcTypeDouble
&&
895 v
.u
.d
== (double) (int) v
.u
.d
)
897 v
.type
= FcTypeInteger
;
905 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
909 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
919 v
.type
= FcTypeString
;
920 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
933 v
.type
= FcTypeMatrix
;
934 m
= malloc (sizeof (FcMatrix
));
937 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
938 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
962 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
975 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
981 v
.type
= FcTypeInteger
;
982 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
991 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
997 v
.type
= FcTypeInteger
;
998 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1001 v
.type
= FcTypeVoid
;
1004 FcValueDestroy (vl
);
1007 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1013 v
.type
= FcTypeInteger
;
1014 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1017 v
.type
= FcTypeVoid
;
1020 FcValueDestroy (vl
);
1023 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1029 v
.type
= FcTypeInteger
;
1030 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1033 v
.type
= FcTypeVoid
;
1036 FcValueDestroy (vl
);
1039 v
.type
= FcTypeVoid
;
1045 static FcValueList
*
1046 FcConfigMatchValueList (FcPattern
*p
,
1048 FcValueList
*values
)
1050 FcValueList
*ret
= 0;
1051 FcExpr
*e
= t
->expr
;
1057 /* Compute the value of the match expression */
1058 if (e
->op
== FcOpComma
)
1060 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1061 e
= e
->u
.tree
.right
;
1065 value
= FcConfigEvaluate (p
, e
);
1069 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1071 /* Compare the pattern value to the match expression value */
1072 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1079 if (t
->qual
== FcQualAll
)
1086 FcValueDestroy (value
);
1091 static FcValueList
*
1092 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1098 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1101 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1102 if (e
->op
== FcOpComma
)
1104 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1105 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1109 l
->value
= FcConfigEvaluate (p
, e
);
1110 l
->next
= FcValueListPtrCreateDynamic(0);
1112 l
->binding
= binding
;
1113 if (l
->value
.type
== FcTypeVoid
)
1115 FcValueList
*next
= FcValueListPtrU(l
->next
);
1117 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1126 FcConfigAdd (FcValueListPtr
*head
,
1127 FcValueList
*position
,
1131 FcValueListPtr
*prev
, last
, v
;
1132 FcValueBinding sameBinding
;
1135 sameBinding
= position
->binding
;
1137 sameBinding
= FcValueBindingWeak
;
1138 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1139 v
= FcValueListPtrU(v
)->next
)
1140 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1141 FcValueListPtrU(v
)->binding
= sameBinding
;
1145 prev
= &position
->next
;
1147 for (prev
= head
; FcValueListPtrU(*prev
);
1148 prev
= &(FcValueListPtrU(*prev
)->next
))
1155 for (prev
= head
; FcValueListPtrU(*prev
);
1156 prev
= &(FcValueListPtrU(*prev
)->next
))
1158 if (FcValueListPtrU(*prev
) == position
)
1165 if (FcDebug () & FC_DBG_EDIT
)
1167 if (!FcValueListPtrU(*prev
))
1168 printf ("position not on list\n");
1172 if (FcDebug () & FC_DBG_EDIT
)
1174 printf ("%s list before ", append
? "Append" : "Prepend");
1175 FcValueListPrint (*head
);
1181 last
= FcValueListPtrCreateDynamic(new);
1182 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1183 last
= FcValueListPtrU(last
)->next
;
1185 FcValueListPtrU(last
)->next
= *prev
;
1186 *prev
= FcValueListPtrCreateDynamic(new);
1189 if (FcDebug () & FC_DBG_EDIT
)
1191 printf ("%s list after ", append
? "Append" : "Prepend");
1192 FcValueListPrint (*head
);
1200 FcConfigDel (FcValueListPtr
*head
,
1201 FcValueList
*position
)
1203 FcValueListPtr
*prev
;
1205 for (prev
= head
; FcValueListPtrU(*prev
);
1206 prev
= &(FcValueListPtrU(*prev
)->next
))
1208 if (FcValueListPtrU(*prev
) == position
)
1210 *prev
= position
->next
;
1211 position
->next
= FcValueListPtrCreateDynamic(0);
1212 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1219 FcConfigPatternAdd (FcPattern
*p
,
1226 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1230 FcConfigAdd (&e
->values
, 0, append
, list
);
1235 * Delete all values associated with a field
1238 FcConfigPatternDel (FcPattern
*p
,
1241 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1244 while (FcValueListPtrU(e
->values
))
1245 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1249 FcConfigPatternCanon (FcPattern
*p
,
1252 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1255 if (!FcValueListPtrU(e
->values
))
1256 FcPatternDel (p
, object
);
1260 FcConfigSubstituteWithPat (FcConfig
*config
,
1275 config
= FcConfigGetCurrent ();
1280 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1281 if (!st
&& config
->maxObjects
)
1283 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1285 if (FcDebug () & FC_DBG_EDIT
)
1287 printf ("FcConfigSubstitute ");
1290 if (kind
== FcMatchPattern
)
1291 s
= config
->substPattern
;
1293 s
= config
->substFont
;
1294 for (; s
; s
= s
->next
)
1297 * Check the tests to see if
1298 * they all match the pattern
1300 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1302 if (FcDebug () & FC_DBG_EDIT
)
1304 printf ("FcConfigSubstitute test ");
1308 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1313 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1317 * If there's no such field in the font,
1318 * then FcQualAll matches while FcQualAny does not
1322 if (t
->qual
== FcQualAll
)
1331 * Check to see if there is a match, mark the location
1332 * to apply match-relative edits
1334 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1337 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1339 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1344 if (FcDebug () & FC_DBG_EDIT
)
1345 printf ("No match\n");
1348 if (FcDebug () & FC_DBG_EDIT
)
1350 printf ("Substitute ");
1353 for (e
= s
->edit
; e
; e
= e
->next
)
1356 * Evaluate the list of expressions
1358 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1360 * Locate any test associated with this field, skipping
1361 * tests associated with the pattern when substituting in
1364 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1366 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1367 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1368 (FcChar8
*) e
->field
))
1371 * KLUDGE - the pattern may have been reallocated or
1372 * things may have been inserted or deleted above
1373 * this element by other edits. Go back and find
1376 if (e
!= s
->edit
&& st
[i
].elt
)
1377 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1386 * If there was a test, then replace the matched
1387 * value with the new list of values
1391 FcValueList
*thisValue
= st
[i
].value
;
1392 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1395 * Append the new list of values after the current value
1397 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1399 * Delete the marked value
1401 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1403 * Adjust any pointers into the value list to ensure
1404 * future edits occur at the same place
1406 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1408 if (st
[i
].value
== thisValue
)
1409 st
[i
].value
= nextValue
;
1413 /* fall through ... */
1414 case FcOpAssignReplace
:
1416 * Delete all of the values and insert
1419 FcConfigPatternDel (p
, e
->field
);
1420 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1422 * Adjust any pointers into the value list as they no
1423 * longer point to anything valid
1427 FcPatternElt
*thisElt
= st
[i
].elt
;
1428 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1430 if (st
[i
].elt
== thisElt
)
1438 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1441 /* fall through ... */
1442 case FcOpPrependFirst
:
1443 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1448 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1451 /* fall through ... */
1452 case FcOpAppendLast
:
1453 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1460 * Now go through the pattern and eliminate
1461 * any properties without data
1463 for (e
= s
->edit
; e
; e
= e
->next
)
1464 FcConfigPatternCanon (p
, e
->field
);
1466 if (FcDebug () & FC_DBG_EDIT
)
1468 printf ("FcConfigSubstitute edit");
1472 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1474 if (FcDebug () & FC_DBG_EDIT
)
1476 printf ("FcConfigSubstitute done");
1483 FcConfigSubstitute (FcConfig
*config
,
1487 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1490 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1492 static FcChar8 fontconfig_path
[1000] = "";
1495 DllMain (HINSTANCE hinstDLL
,
1501 switch (fdwReason
) {
1502 case DLL_PROCESS_ATTACH
:
1503 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1504 sizeof (fontconfig_path
)))
1507 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1508 * assume it's a Unix-style installation tree, and use
1509 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1510 * folder where the DLL is as FONTCONFIG_PATH.
1512 p
= strrchr (fontconfig_path
, '\\');
1516 p
= strrchr (fontconfig_path
, '\\');
1517 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1518 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1520 strcat (fontconfig_path
, "\\etc\\fonts");
1523 fontconfig_path
[0] = '\0';
1531 #undef FONTCONFIG_PATH
1532 #define FONTCONFIG_PATH fontconfig_path
1534 #else /* !(_WIN32 && PIC) */
1536 #endif /* !(_WIN32 && PIC) */
1538 #ifndef FONTCONFIG_FILE
1539 #define FONTCONFIG_FILE "fonts.conf"
1543 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1548 dir
= (FcChar8
*) "";
1549 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1553 strcpy ((char *) path
, (const char *) dir
);
1554 /* make sure there's a single separator */
1556 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1557 path
[strlen((char *) path
)-1] != '\\')) &&
1560 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1561 strcat ((char *) path
, "\\");
1563 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1564 strcat ((char *) path
, "/");
1566 strcat ((char *) path
, (char *) file
);
1568 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1569 if (access ((char *) path
, R_OK
) == 0)
1577 FcConfigGetPath (void)
1580 FcChar8
*env
, *e
, *colon
;
1585 npath
= 2; /* default dir + null */
1586 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1592 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1595 path
= calloc (npath
, sizeof (FcChar8
*));
1605 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1607 colon
= e
+ strlen ((char *) e
);
1608 path
[i
] = malloc (colon
- e
+ 1);
1611 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1612 path
[i
][colon
- e
] = '\0';
1621 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1622 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1625 strcpy ((char *) path
[i
], (const char *) dir
);
1629 for (i
= 0; path
[i
]; i
++)
1637 FcConfigFreePath (FcChar8
**path
)
1641 for (p
= path
; *p
; p
++)
1646 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1651 if (_FcConfigHomeEnabled
)
1653 char *home
= getenv ("HOME");
1657 home
= getenv ("USERPROFILE");
1660 return (FcChar8
*) home
;
1666 FcConfigEnableHome (FcBool enable
)
1668 FcBool prev
= _FcConfigHomeEnabled
;
1669 _FcConfigHomeEnabled
= enable
;
1674 FcConfigFilename (const FcChar8
*url
)
1676 FcChar8
*file
, *dir
, **path
, **p
;
1680 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1682 url
= (FcChar8
*) FONTCONFIG_FILE
;
1687 if (isalpha (*url
) &&
1689 (url
[2] == '/' || url
[2] == '\\'))
1695 dir
= FcConfigHome ();
1697 file
= FcConfigFileExists (dir
, url
+ 1);
1706 file
= FcConfigFileExists (0, url
);
1709 path
= FcConfigGetPath ();
1712 for (p
= path
; *p
; p
++)
1714 file
= FcConfigFileExists (*p
, url
);
1718 FcConfigFreePath (path
);
1725 * Manage the application-specific fonts
1729 FcConfigAppFontAddFile (FcConfig
*config
,
1730 const FcChar8
*file
)
1739 config
= FcConfigGetCurrent ();
1744 subdirs
= FcStrSetCreate ();
1748 set
= FcConfigGetFonts (config
, FcSetApplication
);
1751 set
= FcFontSetCreate ();
1754 FcStrSetDestroy (subdirs
);
1757 FcConfigSetFonts (config
, set
, FcSetApplication
);
1760 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1762 FcStrSetDestroy (subdirs
);
1765 if ((sublist
= FcStrListCreate (subdirs
)))
1767 while ((subdir
= FcStrListNext (sublist
)))
1769 FcConfigAppFontAddDir (config
, subdir
);
1771 FcStrListDone (sublist
);
1777 FcConfigAppFontAddDir (FcConfig
*config
,
1787 config
= FcConfigGetCurrent ();
1791 subdirs
= FcStrSetCreate ();
1795 set
= FcConfigGetFonts (config
, FcSetApplication
);
1798 set
= FcFontSetCreate ();
1801 FcStrSetDestroy (subdirs
);
1804 FcConfigSetFonts (config
, set
, FcSetApplication
);
1807 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1809 FcStrSetDestroy (subdirs
);
1812 if ((sublist
= FcStrListCreate (subdirs
)))
1814 while ((subdir
= FcStrListNext (sublist
)))
1816 FcConfigAppFontAddDir (config
, subdir
);
1818 FcStrListDone (sublist
);
1824 FcConfigAppFontClear (FcConfig
*config
)
1828 config
= FcConfigGetCurrent ();
1833 FcConfigSetFonts (config
, 0, FcSetApplication
);
1837 * Manage filename-based font source selectors
1841 FcConfigGlobAdd (FcConfig
*config
,
1842 const FcChar8
*glob
,
1845 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1847 return FcStrSetAdd (set
, glob
);
1851 FcConfigGlobMatch (const FcChar8
*glob
,
1852 const FcChar8
*string
)
1856 while ((c
= *glob
++))
1860 /* short circuit common case */
1863 /* short circuit another common case */
1864 if (strchr ((char *) glob
, '*') == 0)
1865 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1868 if (FcConfigGlobMatch (glob
, string
))
1874 if (*string
++ == '\0')
1883 return *string
== '\0';
1887 FcConfigGlobsMatch (const FcStrSet
*globs
,
1888 const FcChar8
*string
)
1892 for (i
= 0; i
< globs
->num
; i
++)
1893 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1899 FcConfigAcceptFilename (FcConfig
*config
,
1900 const FcChar8
*filename
)
1902 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1904 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1910 * Manage font-pattern based font source selectors
1914 FcConfigPatternsAdd (FcConfig
*config
,
1918 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1920 return FcFontSetAdd (set
, pattern
);
1924 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1925 const FcPattern
*font
)
1929 for (i
= 0; i
< patterns
->nfont
; i
++)
1930 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1936 FcConfigAcceptFont (FcConfig
*config
,
1937 const FcPattern
*font
)
1939 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1941 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))