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
)
250 FcFontSet
*fonts
, *cached_fonts
;
251 FcGlobalCache
*cache
;
255 fonts
= FcFontSetCreate ();
259 cache
= FcGlobalCacheCreate ();
264 FcGlobalCacheLoad (cache
, config
->cache
);
266 cached_fonts
= FcCacheRead(config
, cache
);
269 list
= FcConfigGetFontDirs (config
);
273 while ((dir
= FcStrListNext (list
)))
275 if (FcDebug () & FC_DBG_FONTSET
)
276 printf ("scan dir %s\n", dir
);
277 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
278 config
->blanks
, dir
, FcFalse
, config
);
281 FcStrListDone (list
);
287 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
289 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]))
290 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
292 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
294 cached_fonts
->nfont
= 0;
295 FcFontSetDestroy (cached_fonts
);
298 if (FcDebug () & FC_DBG_FONTSET
)
299 FcFontSetPrint (fonts
);
302 FcGlobalCacheSave (cache
, config
->cache
);
303 FcGlobalCacheDestroy (cache
);
305 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
309 FcFontSetDestroy (fonts
);
315 FcConfigSetCurrent (FcConfig
*config
)
318 if (!FcConfigBuildFonts (config
))
322 FcConfigDestroy (_fcConfig
);
328 FcConfigGetCurrent (void)
337 FcConfigAddConfigDir (FcConfig
*config
,
340 return FcStrSetAddFilename (config
->configDirs
, d
);
344 FcConfigGetConfigDirs (FcConfig
*config
)
348 config
= FcConfigGetCurrent ();
352 return FcStrListCreate (config
->configDirs
);
356 FcConfigAddFontDir (FcConfig
*config
,
359 return FcStrSetAddFilename (config
->fontDirs
, d
);
363 FcConfigAddDir (FcConfig
*config
,
366 return (FcConfigAddConfigDir (config
, d
) &&
367 FcConfigAddFontDir (config
, d
));
371 FcConfigGetFontDirs (FcConfig
*config
)
375 config
= FcConfigGetCurrent ();
379 return FcStrListCreate (config
->fontDirs
);
383 FcConfigAddConfigFile (FcConfig
*config
,
387 FcChar8
*file
= FcConfigFilename (f
);
392 ret
= FcStrSetAdd (config
->configFiles
, file
);
398 FcConfigGetConfigFiles (FcConfig
*config
)
402 config
= FcConfigGetCurrent ();
406 return FcStrListCreate (config
->configFiles
);
410 FcConfigSetCache (FcConfig
*config
,
413 FcChar8
*new = FcStrCopyFilename (c
);
418 FcStrFree (config
->cache
);
424 FcConfigGetCache (FcConfig
*config
)
428 config
= FcConfigGetCurrent ();
432 return config
->cache
;
436 FcConfigGetFonts (FcConfig
*config
,
441 config
= FcConfigGetCurrent ();
445 return config
->fonts
[set
];
449 FcConfigSetFonts (FcConfig
*config
,
453 if (config
->fonts
[set
])
454 FcFontSetDestroy (config
->fonts
[set
]);
455 config
->fonts
[set
] = fonts
;
461 FcConfigGetBlanks (FcConfig
*config
)
465 config
= FcConfigGetCurrent ();
469 return config
->blanks
;
473 FcConfigAddBlank (FcConfig
*config
,
481 b
= FcBlanksCreate ();
485 if (!FcBlanksAdd (b
, blank
))
492 FcConfigGetRescanInverval (FcConfig
*config
)
496 config
= FcConfigGetCurrent ();
500 return config
->rescanInterval
;
504 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
508 config
= FcConfigGetCurrent ();
512 config
->rescanInterval
= rescanInterval
;
517 FcConfigAddEdit (FcConfig
*config
,
522 FcSubst
*subst
, **prev
;
526 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
529 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
530 if (kind
== FcMatchPattern
)
531 prev
= &config
->substPattern
;
533 prev
= &config
->substFont
;
534 for (; *prev
; prev
= &(*prev
)->next
);
540 for (t
= test
; t
; t
= t
->next
)
542 if (t
->kind
== FcMatchDefault
)
546 if (config
->maxObjects
< num
)
547 config
->maxObjects
= num
;
548 if (FcDebug () & FC_DBG_EDIT
)
550 printf ("Add Subst ");
551 FcSubstPrint (subst
);
556 typedef struct _FcSubState
{
562 FcConfigPromote (FcValue v
, FcValue u
)
564 if (v
.type
== FcTypeInteger
)
566 v
.type
= FcTypeDouble
;
567 v
.u
.d
= (double) v
.u
.i
;
569 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
571 v
.u
.m
= &FcIdentityMatrix
;
572 v
.type
= FcTypeMatrix
;
574 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
576 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
577 v
.type
= FcTypeLangSet
;
583 FcConfigCompareValue (const FcValue
*left_o
,
585 const FcValue
*right_o
)
587 FcValue left
= FcValueCanonicalize(left_o
);
588 FcValue right
= FcValueCanonicalize(right_o
);
589 FcBool ret
= FcFalse
;
591 left
= FcConfigPromote (left
, right
);
592 right
= FcConfigPromote (right
, left
);
593 if (left
.type
== right
.type
)
597 break; /* FcConfigPromote prevents this from happening */
603 ret
= left
.u
.d
== right
.u
.d
;
606 case FcOpNotContains
:
607 ret
= left
.u
.d
!= right
.u
.d
;
610 ret
= left
.u
.d
< right
.u
.d
;
613 ret
= left
.u
.d
<= right
.u
.d
;
616 ret
= left
.u
.d
> right
.u
.d
;
619 ret
= left
.u
.d
>= right
.u
.d
;
630 ret
= left
.u
.b
== right
.u
.b
;
633 case FcOpNotContains
:
634 ret
= left
.u
.b
!= right
.u
.b
;
644 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
647 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
650 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
652 case FcOpNotContains
:
653 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
664 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
667 case FcOpNotContains
:
668 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
678 /* left contains right if right is a subset of left */
679 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
681 case FcOpNotContains
:
682 /* left contains right if right is a subset of left */
683 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
686 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
689 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
699 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
701 case FcOpNotContains
:
702 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
705 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
708 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
730 ret
= left
.u
.f
== right
.u
.f
;
733 case FcOpNotContains
:
734 ret
= left
.u
.f
!= right
.u
.f
;
744 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
751 #define _FcDoubleFloor(d) ((int) (d))
752 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
753 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
754 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
755 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
756 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
759 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
767 v
.type
= FcTypeInteger
;
771 v
.type
= FcTypeDouble
;
775 v
.type
= FcTypeString
;
776 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
780 v
.type
= FcTypeMatrix
;
785 v
.type
= FcTypeCharSet
;
794 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
795 if (r
!= FcResultMatch
)
799 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
800 v
.type
= FcTypeInteger
;
805 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
806 if (vl
.type
== FcTypeBool
)
809 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
811 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
824 case FcOpNotContains
:
826 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
827 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
829 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
839 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
840 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
841 vl
= FcConfigPromote (vl
, vr
);
842 vr
= FcConfigPromote (vr
, vl
);
843 if (vl
.type
== vr
.type
)
849 v
.type
= FcTypeDouble
;
850 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
853 v
.type
= FcTypeDouble
;
854 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
857 v
.type
= FcTypeDouble
;
858 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
861 v
.type
= FcTypeDouble
;
862 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
868 if (v
.type
== FcTypeDouble
&&
869 v
.u
.d
== (double) (int) v
.u
.d
)
871 v
.type
= FcTypeInteger
;
879 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
883 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
893 v
.type
= FcTypeString
;
894 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
907 v
.type
= FcTypeMatrix
;
908 m
= malloc (sizeof (FcMatrix
));
911 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
912 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
936 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
949 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
955 v
.type
= FcTypeInteger
;
956 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
965 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
971 v
.type
= FcTypeInteger
;
972 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
981 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
987 v
.type
= FcTypeInteger
;
988 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
997 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1003 v
.type
= FcTypeInteger
;
1004 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1007 v
.type
= FcTypeVoid
;
1010 FcValueDestroy (vl
);
1013 v
.type
= FcTypeVoid
;
1019 static FcValueList
*
1020 FcConfigMatchValueList (FcPattern
*p
,
1022 FcValueList
*values
)
1024 FcValueList
*ret
= 0;
1025 FcExpr
*e
= t
->expr
;
1031 /* Compute the value of the match expression */
1032 if (e
->op
== FcOpComma
)
1034 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1035 e
= e
->u
.tree
.right
;
1039 value
= FcConfigEvaluate (p
, e
);
1043 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1045 /* Compare the pattern value to the match expression value */
1046 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1053 if (t
->qual
== FcQualAll
)
1060 FcValueDestroy (value
);
1065 static FcValueList
*
1066 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1073 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1076 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1077 if (e
->op
== FcOpComma
)
1079 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1080 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1084 l
->value
= FcConfigEvaluate (p
, e
);
1085 l
->next
= FcValueListPtrCreateDynamic(0);
1087 l
->binding
= binding
;
1088 lp
= FcValueListPtrCreateDynamic(l
);
1089 while (FcValueListPtrU(lp
) && FcValueListPtrU(lp
)->value
.type
== FcTypeVoid
)
1091 FcValueListPtr next
= FcValueListPtrU(lp
)->next
;
1093 if (lp
.bank
== FC_BANK_DYNAMIC
)
1095 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1104 FcConfigAdd (FcValueListPtr
*head
,
1105 FcValueList
*position
,
1109 FcValueListPtr
*prev
, last
, v
;
1110 FcValueBinding sameBinding
;
1113 sameBinding
= position
->binding
;
1115 sameBinding
= FcValueBindingWeak
;
1116 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1117 v
= FcValueListPtrU(v
)->next
)
1118 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1119 FcValueListPtrU(v
)->binding
= sameBinding
;
1123 prev
= &position
->next
;
1125 for (prev
= head
; FcValueListPtrU(*prev
);
1126 prev
= &(FcValueListPtrU(*prev
)->next
))
1133 for (prev
= head
; FcValueListPtrU(*prev
);
1134 prev
= &(FcValueListPtrU(*prev
)->next
))
1136 if (FcValueListPtrU(*prev
) == position
)
1143 if (FcDebug () & FC_DBG_EDIT
)
1145 if (!FcValueListPtrU(*prev
))
1146 printf ("position not on list\n");
1150 if (FcDebug () & FC_DBG_EDIT
)
1152 printf ("%s list before ", append
? "Append" : "Prepend");
1153 FcValueListPrint (*head
);
1159 last
= FcValueListPtrCreateDynamic(new);
1160 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1161 last
= FcValueListPtrU(last
)->next
;
1163 FcValueListPtrU(last
)->next
= *prev
;
1164 *prev
= FcValueListPtrCreateDynamic(new);
1167 if (FcDebug () & FC_DBG_EDIT
)
1169 printf ("%s list after ", append
? "Append" : "Prepend");
1170 FcValueListPrint (*head
);
1178 FcConfigDel (FcValueListPtr
*head
,
1179 FcValueList
*position
)
1181 FcValueListPtr
*prev
;
1183 for (prev
= head
; FcValueListPtrU(*prev
);
1184 prev
= &(FcValueListPtrU(*prev
)->next
))
1186 if (FcValueListPtrU(*prev
) == position
)
1188 *prev
= position
->next
;
1189 position
->next
= FcValueListPtrCreateDynamic(0);
1190 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1197 FcConfigPatternAdd (FcPattern
*p
,
1204 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1208 FcConfigAdd (&e
->values
, 0, append
, list
);
1213 * Delete all values associated with a field
1216 FcConfigPatternDel (FcPattern
*p
,
1219 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1222 while (FcValueListPtrU(e
->values
))
1223 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1227 FcConfigPatternCanon (FcPattern
*p
,
1230 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1233 if (!FcValueListPtrU(e
->values
))
1234 FcPatternDel (p
, object
);
1238 FcConfigSubstituteWithPat (FcConfig
*config
,
1253 config
= FcConfigGetCurrent ();
1258 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1259 if (!st
&& config
->maxObjects
)
1261 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1263 if (FcDebug () & FC_DBG_EDIT
)
1265 printf ("FcConfigSubstitute ");
1268 if (kind
== FcMatchPattern
)
1269 s
= config
->substPattern
;
1271 s
= config
->substFont
;
1272 for (; s
; s
= s
->next
)
1275 * Check the tests to see if
1276 * they all match the pattern
1278 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1280 if (FcDebug () & FC_DBG_EDIT
)
1282 printf ("FcConfigSubstitute test ");
1286 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1291 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1295 * If there's no such field in the font,
1296 * then FcQualAll matches while FcQualAny does not
1300 if (t
->qual
== FcQualAll
)
1309 * Check to see if there is a match, mark the location
1310 * to apply match-relative edits
1312 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1315 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1317 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1322 if (FcDebug () & FC_DBG_EDIT
)
1323 printf ("No match\n");
1326 if (FcDebug () & FC_DBG_EDIT
)
1328 printf ("Substitute ");
1331 for (e
= s
->edit
; e
; e
= e
->next
)
1334 * Evaluate the list of expressions
1336 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1338 * Locate any test associated with this field, skipping
1339 * tests associated with the pattern when substituting in
1342 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1344 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1345 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1346 (FcChar8
*) e
->field
))
1349 * KLUDGE - the pattern may have been reallocated or
1350 * things may have been inserted or deleted above
1351 * this element by other edits. Go back and find
1354 if (e
!= s
->edit
&& st
[i
].elt
)
1355 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1364 * If there was a test, then replace the matched
1365 * value with the new list of values
1369 FcValueList
*thisValue
= st
[i
].value
;
1370 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1373 * Append the new list of values after the current value
1375 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1377 * Delete the marked value
1379 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1381 * Adjust any pointers into the value list to ensure
1382 * future edits occur at the same place
1384 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1386 if (st
[i
].value
== thisValue
)
1387 st
[i
].value
= nextValue
;
1391 /* fall through ... */
1392 case FcOpAssignReplace
:
1394 * Delete all of the values and insert
1397 FcConfigPatternDel (p
, e
->field
);
1398 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1400 * Adjust any pointers into the value list as they no
1401 * longer point to anything valid
1405 FcPatternElt
*thisElt
= st
[i
].elt
;
1406 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1408 if (st
[i
].elt
== thisElt
)
1416 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1419 /* fall through ... */
1420 case FcOpPrependFirst
:
1421 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1426 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1429 /* fall through ... */
1430 case FcOpAppendLast
:
1431 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1438 * Now go through the pattern and eliminate
1439 * any properties without data
1441 for (e
= s
->edit
; e
; e
= e
->next
)
1442 FcConfigPatternCanon (p
, e
->field
);
1444 if (FcDebug () & FC_DBG_EDIT
)
1446 printf ("FcConfigSubstitute edit");
1450 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1452 if (FcDebug () & FC_DBG_EDIT
)
1454 printf ("FcConfigSubstitute done");
1461 FcConfigSubstitute (FcConfig
*config
,
1465 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1468 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1470 static FcChar8 fontconfig_path
[1000] = "";
1473 DllMain (HINSTANCE hinstDLL
,
1479 switch (fdwReason
) {
1480 case DLL_PROCESS_ATTACH
:
1481 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1482 sizeof (fontconfig_path
)))
1485 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1486 * assume it's a Unix-style installation tree, and use
1487 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1488 * folder where the DLL is as FONTCONFIG_PATH.
1490 p
= strrchr (fontconfig_path
, '\\');
1494 p
= strrchr (fontconfig_path
, '\\');
1495 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1496 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1498 strcat (fontconfig_path
, "\\etc\\fonts");
1501 fontconfig_path
[0] = '\0';
1509 #undef FONTCONFIG_PATH
1510 #define FONTCONFIG_PATH fontconfig_path
1512 #else /* !(_WIN32 && PIC) */
1514 #endif /* !(_WIN32 && PIC) */
1516 #ifndef FONTCONFIG_FILE
1517 #define FONTCONFIG_FILE "fonts.conf"
1521 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1526 dir
= (FcChar8
*) "";
1527 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1531 strcpy ((char *) path
, (const char *) dir
);
1532 /* make sure there's a single separator */
1534 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1535 path
[strlen((char *) path
)-1] != '\\')) &&
1538 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1539 strcat ((char *) path
, "\\");
1541 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1542 strcat ((char *) path
, "/");
1544 strcat ((char *) path
, (char *) file
);
1546 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1547 if (access ((char *) path
, R_OK
) == 0)
1555 FcConfigGetPath (void)
1558 FcChar8
*env
, *e
, *colon
;
1563 npath
= 2; /* default dir + null */
1564 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1570 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1573 path
= calloc (npath
, sizeof (FcChar8
*));
1583 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1585 colon
= e
+ strlen ((char *) e
);
1586 path
[i
] = malloc (colon
- e
+ 1);
1589 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1590 path
[i
][colon
- e
] = '\0';
1599 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1600 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1603 strcpy ((char *) path
[i
], (const char *) dir
);
1607 for (i
= 0; path
[i
]; i
++)
1615 FcConfigFreePath (FcChar8
**path
)
1619 for (p
= path
; *p
; p
++)
1624 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1629 if (_FcConfigHomeEnabled
)
1631 char *home
= getenv ("HOME");
1635 home
= getenv ("USERPROFILE");
1644 FcConfigEnableHome (FcBool enable
)
1646 FcBool prev
= _FcConfigHomeEnabled
;
1647 _FcConfigHomeEnabled
= enable
;
1652 FcConfigFilename (const FcChar8
*url
)
1654 FcChar8
*file
, *dir
, **path
, **p
;
1658 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1660 url
= (FcChar8
*) FONTCONFIG_FILE
;
1665 if (isalpha (*url
) &&
1667 (url
[2] == '/' || url
[2] == '\\'))
1673 dir
= FcConfigHome ();
1675 file
= FcConfigFileExists (dir
, url
+ 1);
1684 file
= FcConfigFileExists (0, url
);
1687 path
= FcConfigGetPath ();
1690 for (p
= path
; *p
; p
++)
1692 file
= FcConfigFileExists (*p
, url
);
1696 FcConfigFreePath (path
);
1703 * Manage the application-specific fonts
1707 FcConfigAppFontAddFile (FcConfig
*config
,
1708 const FcChar8
*file
)
1717 config
= FcConfigGetCurrent ();
1722 subdirs
= FcStrSetCreate ();
1726 set
= FcConfigGetFonts (config
, FcSetApplication
);
1729 set
= FcFontSetCreate ();
1732 FcStrSetDestroy (subdirs
);
1735 FcConfigSetFonts (config
, set
, FcSetApplication
);
1738 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1740 FcStrSetDestroy (subdirs
);
1743 if ((sublist
= FcStrListCreate (subdirs
)))
1745 while ((subdir
= FcStrListNext (sublist
)))
1747 FcConfigAppFontAddDir (config
, subdir
);
1749 FcStrListDone (sublist
);
1755 FcConfigAppFontAddDir (FcConfig
*config
,
1765 config
= FcConfigGetCurrent ();
1769 subdirs
= FcStrSetCreate ();
1773 set
= FcConfigGetFonts (config
, FcSetApplication
);
1776 set
= FcFontSetCreate ();
1779 FcStrSetDestroy (subdirs
);
1782 FcConfigSetFonts (config
, set
, FcSetApplication
);
1785 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1787 FcStrSetDestroy (subdirs
);
1790 if ((sublist
= FcStrListCreate (subdirs
)))
1792 while ((subdir
= FcStrListNext (sublist
)))
1794 FcConfigAppFontAddDir (config
, subdir
);
1796 FcStrListDone (sublist
);
1802 FcConfigAppFontClear (FcConfig
*config
)
1806 config
= FcConfigGetCurrent ();
1811 FcConfigSetFonts (config
, 0, FcSetApplication
);
1815 * Manage filename-based font source selectors
1819 FcConfigGlobAdd (FcConfig
*config
,
1820 const FcChar8
*glob
,
1823 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1825 return FcStrSetAdd (set
, glob
);
1829 FcConfigGlobMatch (const FcChar8
*glob
,
1830 const FcChar8
*string
)
1834 while ((c
= *glob
++))
1838 /* short circuit common case */
1841 /* short circuit another common case */
1842 if (strchr ((char *) glob
, '*') == 0)
1843 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1846 if (FcConfigGlobMatch (glob
, string
))
1852 if (*string
++ == '\0')
1861 return *string
== '\0';
1865 FcConfigGlobsMatch (const FcStrSet
*globs
,
1866 const FcChar8
*string
)
1870 for (i
= 0; i
< globs
->num
; i
++)
1871 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1877 FcConfigAcceptFilename (FcConfig
*config
,
1878 const FcChar8
*filename
)
1880 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1882 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1888 * Manage font-pattern based font source selectors
1892 FcConfigPatternsAdd (FcConfig
*config
,
1896 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1898 return FcFontSetAdd (set
, pattern
);
1902 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1903 const FcPattern
*font
)
1907 for (i
= 0; i
< patterns
->nfont
; i
++)
1908 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1914 FcConfigAcceptFont (FcConfig
*config
,
1915 const FcPattern
*font
)
1917 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1919 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))