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 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config
= malloc (sizeof (FcConfig
));
50 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
52 config
->configDirs
= FcStrSetCreate ();
53 if (!config
->configDirs
)
56 config
->configFiles
= FcStrSetCreate ();
57 if (!config
->configFiles
)
60 config
->fontDirs
= FcStrSetCreate ();
61 if (!config
->fontDirs
)
64 config
->acceptGlobs
= FcStrSetCreate ();
65 if (!config
->acceptGlobs
)
68 config
->rejectGlobs
= FcStrSetCreate ();
69 if (!config
->rejectGlobs
)
72 config
->acceptPatterns
= FcFontSetCreate ();
73 if (!config
->acceptPatterns
)
76 config
->rejectPatterns
= FcFontSetCreate ();
77 if (!config
->rejectPatterns
)
82 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
86 if (config
->cache
== 0)
88 /* If no home, use the temp folder. */
90 int templen
= GetTempPath (1, dummy
);
91 FcChar8
*temp
= malloc (templen
+ 1);
97 GetTempPath (templen
+ 1, temp
);
98 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
100 if (!FcConfigSetCache (config
, cache_dir
))
102 FcStrFree (cache_dir
);
105 FcStrFree (cache_dir
);
110 config
->cacheDirs
= FcStrSetCreate ();
111 if (!config
->cacheDirs
)
116 config
->substPattern
= 0;
117 config
->substFont
= 0;
118 config
->maxObjects
= 0;
119 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
120 config
->fonts
[set
] = 0;
122 config
->rescanTime
= time(0);
123 config
->rescanInterval
= 30;
128 FcStrFree (config
->cache
);
130 FcFontSetDestroy (config
->rejectPatterns
);
132 FcFontSetDestroy (config
->acceptPatterns
);
134 FcStrSetDestroy (config
->rejectGlobs
);
136 FcStrSetDestroy (config
->acceptGlobs
);
138 FcStrSetDestroy (config
->fontDirs
);
140 FcStrSetDestroy (config
->configFiles
);
142 FcStrSetDestroy (config
->configDirs
);
145 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
151 FcConfigNewestFile (FcStrSet
*files
)
153 FcStrList
*list
= FcStrListCreate (files
);
154 FcFileTime newest
= { 0, FcFalse
};
160 while ((file
= FcStrListNext (list
)))
161 if (stat ((char *) file
, &statb
) == 0)
162 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
165 newest
.time
= statb
.st_mtime
;
167 FcStrListDone (list
);
173 FcConfigModifiedTime (FcConfig
*config
)
177 FcFileTime v
= { 0, FcFalse
};
178 config
= FcConfigGetCurrent ();
182 return FcConfigNewestFile (config
->configFiles
);
186 FcConfigUptoDate (FcConfig
*config
)
188 FcFileTime config_time
, font_time
;
189 time_t now
= time(0);
192 config
= FcConfigGetCurrent ();
196 config_time
= FcConfigNewestFile (config
->configFiles
);
197 font_time
= FcConfigNewestFile (config
->fontDirs
);
198 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
199 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
203 config
->rescanTime
= now
;
208 FcSubstDestroy (FcSubst
*s
)
216 FcTestDestroy (s
->test
);
218 FcEditDestroy (s
->edit
);
220 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
226 FcConfigDestroy (FcConfig
*config
)
230 if (config
== _fcConfig
)
233 FcStrSetDestroy (config
->configDirs
);
234 FcStrSetDestroy (config
->fontDirs
);
235 FcStrSetDestroy (config
->cacheDirs
);
236 FcStrSetDestroy (config
->configFiles
);
237 FcStrSetDestroy (config
->acceptGlobs
);
238 FcStrSetDestroy (config
->rejectGlobs
);
239 FcFontSetDestroy (config
->acceptPatterns
);
240 FcFontSetDestroy (config
->rejectPatterns
);
243 FcBlanksDestroy (config
->blanks
);
246 FcStrFree (config
->cache
);
248 FcSubstDestroy (config
->substPattern
);
249 FcSubstDestroy (config
->substFont
);
250 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
251 if (config
->fonts
[set
])
252 FcFontSetDestroy (config
->fonts
[set
]);
255 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
259 * Scan the current list of directories in the configuration
260 * and build the set of available fonts. Update the
261 * per-user cache file to reflect the new configuration
265 FcConfigBuildFonts (FcConfig
*config
)
267 FcFontSet
*fonts
, *cached_fonts
;
272 fonts
= FcFontSetCreate ();
276 oldDirs
= FcStrSetCreate ();
280 cached_fonts
= FcCacheRead(config
);
283 list
= FcConfigGetFontDirs (config
);
287 while ((dir
= FcStrListNext (list
)))
289 if (FcDebug () & FC_DBG_FONTSET
)
290 printf ("build scan dir %s\n", dir
);
291 FcDirScanConfig (fonts
, config
->fontDirs
,
292 config
->blanks
, dir
, FcFalse
, config
);
295 FcStrListDone (list
);
301 for (i
= 0; i
< oldDirs
->num
; i
++)
303 if (FcDebug () & FC_DBG_FONTSET
)
304 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
305 FcDirScanConfig (fonts
, config
->fontDirs
,
306 config
->blanks
, oldDirs
->strs
[i
],
310 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
313 FcPattern
*font
= cached_fonts
->fonts
[i
];
314 FcPatternObjectGetString (font
, FC_FILE_OBJECT
, 0, &cfn
);
316 if (FcConfigAcceptFont (config
, font
) &&
317 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
318 FcFontSetAdd (fonts
, font
);
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
);
329 FcStrSetDestroy (oldDirs
);
331 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
335 FcStrSetDestroy (oldDirs
);
337 FcFontSetDestroy (fonts
);
343 FcConfigSetCurrent (FcConfig
*config
)
346 if (!FcConfigBuildFonts (config
))
350 FcConfigDestroy (_fcConfig
);
356 FcConfigGetCurrent (void)
365 FcConfigAddConfigDir (FcConfig
*config
,
368 return FcStrSetAddFilename (config
->configDirs
, d
);
372 FcConfigGetConfigDirs (FcConfig
*config
)
376 config
= FcConfigGetCurrent ();
380 return FcStrListCreate (config
->configDirs
);
384 FcConfigAddFontDir (FcConfig
*config
,
387 return FcStrSetAddFilename (config
->fontDirs
, d
);
391 FcConfigAddDir (FcConfig
*config
,
394 return (FcConfigAddConfigDir (config
, d
) &&
395 FcConfigAddFontDir (config
, d
));
399 FcConfigGetFontDirs (FcConfig
*config
)
403 config
= FcConfigGetCurrent ();
407 return FcStrListCreate (config
->fontDirs
);
411 FcConfigAddCacheDir (FcConfig
*config
,
414 return FcStrSetAddFilename (config
->cacheDirs
, d
);
418 FcConfigGetCacheDirs (FcConfig
*config
)
422 config
= FcConfigGetCurrent ();
426 return FcStrListCreate (config
->cacheDirs
);
430 FcConfigAddConfigFile (FcConfig
*config
,
434 FcChar8
*file
= FcConfigFilename (f
);
439 ret
= FcStrSetAdd (config
->configFiles
, file
);
445 FcConfigGetConfigFiles (FcConfig
*config
)
449 config
= FcConfigGetCurrent ();
453 return FcStrListCreate (config
->configFiles
);
457 FcConfigSetCache (FcConfig
*config
,
460 FcChar8
*new = FcStrCopyFilename (c
);
465 FcStrFree (config
->cache
);
471 FcConfigGetCache (FcConfig
*config
)
475 config
= FcConfigGetCurrent ();
479 return config
->cache
;
483 FcConfigGetFonts (FcConfig
*config
,
488 config
= FcConfigGetCurrent ();
492 return config
->fonts
[set
];
496 FcConfigSetFonts (FcConfig
*config
,
500 if (config
->fonts
[set
])
501 FcFontSetDestroy (config
->fonts
[set
]);
502 config
->fonts
[set
] = fonts
;
508 FcConfigGetBlanks (FcConfig
*config
)
512 config
= FcConfigGetCurrent ();
516 return config
->blanks
;
520 FcConfigAddBlank (FcConfig
*config
,
523 FcBlanks
*b
, *freeme
= 0;
528 freeme
= b
= FcBlanksCreate ();
532 if (!FcBlanksAdd (b
, blank
))
535 FcBlanksDestroy (freeme
);
543 FcConfigGetRescanInverval (FcConfig
*config
)
547 config
= FcConfigGetCurrent ();
551 return config
->rescanInterval
;
555 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
559 config
= FcConfigGetCurrent ();
563 config
->rescanInterval
= rescanInterval
;
568 FcConfigAddEdit (FcConfig
*config
,
573 FcSubst
*subst
, **prev
;
577 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
580 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
581 if (kind
== FcMatchPattern
)
582 prev
= &config
->substPattern
;
584 prev
= &config
->substFont
;
585 for (; *prev
; prev
= &(*prev
)->next
);
591 for (t
= test
; t
; t
= t
->next
)
593 if (t
->kind
== FcMatchDefault
)
597 if (config
->maxObjects
< num
)
598 config
->maxObjects
= num
;
599 if (FcDebug () & FC_DBG_EDIT
)
601 printf ("Add Subst ");
602 FcSubstPrint (subst
);
607 typedef struct _FcSubState
{
613 FcConfigPromote (FcValue v
, FcValue u
)
615 if (v
.type
== FcTypeInteger
)
617 v
.type
= FcTypeDouble
;
618 v
.u
.d
= (double) v
.u
.i
;
620 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
622 v
.u
.m
= &FcIdentityMatrix
;
623 v
.type
= FcTypeMatrix
;
625 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
627 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
628 v
.type
= FcTypeLangSet
;
634 FcConfigCompareValue (const FcValue
*left_o
,
636 const FcValue
*right_o
)
638 FcValue left
= FcValueCanonicalize(left_o
);
639 FcValue right
= FcValueCanonicalize(right_o
);
640 FcBool ret
= FcFalse
;
642 left
= FcConfigPromote (left
, right
);
643 right
= FcConfigPromote (right
, left
);
644 if (left
.type
== right
.type
)
648 break; /* FcConfigPromote prevents this from happening */
654 ret
= left
.u
.d
== right
.u
.d
;
657 case FcOpNotContains
:
658 ret
= left
.u
.d
!= right
.u
.d
;
661 ret
= left
.u
.d
< right
.u
.d
;
664 ret
= left
.u
.d
<= right
.u
.d
;
667 ret
= left
.u
.d
> right
.u
.d
;
670 ret
= left
.u
.d
>= right
.u
.d
;
681 ret
= left
.u
.b
== right
.u
.b
;
684 case FcOpNotContains
:
685 ret
= left
.u
.b
!= right
.u
.b
;
695 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
698 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
701 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
703 case FcOpNotContains
:
704 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
715 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
718 case FcOpNotContains
:
719 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
729 /* left contains right if right is a subset of left */
730 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
732 case FcOpNotContains
:
733 /* left contains right if right is a subset of left */
734 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
737 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
740 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
750 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
752 case FcOpNotContains
:
753 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
756 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
759 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
781 ret
= left
.u
.f
== right
.u
.f
;
784 case FcOpNotContains
:
785 ret
= left
.u
.f
!= right
.u
.f
;
795 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
802 #define _FcDoubleFloor(d) ((int) (d))
803 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
804 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
805 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
806 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
807 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
810 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
819 v
.type
= FcTypeInteger
;
823 v
.type
= FcTypeDouble
;
827 v
.type
= FcTypeString
;
828 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
831 v
.type
= FcTypeMatrix
;
836 v
.type
= FcTypeCharSet
;
845 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
846 if (r
!= FcResultMatch
)
851 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
852 v
.type
= FcTypeInteger
;
857 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
858 if (vl
.type
== FcTypeBool
)
861 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
863 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
876 case FcOpNotContains
:
878 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
879 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
881 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
891 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
892 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
893 vl
= FcConfigPromote (vl
, vr
);
894 vr
= FcConfigPromote (vr
, vl
);
895 if (vl
.type
== vr
.type
)
901 v
.type
= FcTypeDouble
;
902 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
905 v
.type
= FcTypeDouble
;
906 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
909 v
.type
= FcTypeDouble
;
910 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
913 v
.type
= FcTypeDouble
;
914 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
920 if (v
.type
== FcTypeDouble
&&
921 v
.u
.d
== (double) (int) v
.u
.d
)
923 v
.type
= FcTypeInteger
;
931 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
935 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
945 v
.type
= FcTypeString
;
946 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
947 v
.u
.s
= FcStrStaticName (str
);
961 v
.type
= FcTypeMatrix
;
962 m
= malloc (sizeof (FcMatrix
));
965 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
966 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
990 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1000 FcValueDestroy (vl
);
1003 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1009 v
.type
= FcTypeInteger
;
1010 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1013 v
.type
= FcTypeVoid
;
1016 FcValueDestroy (vl
);
1019 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1025 v
.type
= FcTypeInteger
;
1026 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1029 v
.type
= FcTypeVoid
;
1032 FcValueDestroy (vl
);
1035 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1041 v
.type
= FcTypeInteger
;
1042 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1045 v
.type
= FcTypeVoid
;
1048 FcValueDestroy (vl
);
1051 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1057 v
.type
= FcTypeInteger
;
1058 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1061 v
.type
= FcTypeVoid
;
1064 FcValueDestroy (vl
);
1067 v
.type
= FcTypeVoid
;
1073 static FcValueList
*
1074 FcConfigMatchValueList (FcPattern
*p
,
1076 FcValueList
*values
)
1078 FcValueList
*ret
= 0;
1079 FcExpr
*e
= t
->expr
;
1085 /* Compute the value of the match expression */
1086 if (e
->op
== FcOpComma
)
1088 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1089 e
= e
->u
.tree
.right
;
1093 value
= FcConfigEvaluate (p
, e
);
1097 for (v
= values
; v
; v
= FcValueListNext(v
))
1099 /* Compare the pattern value to the match expression value */
1100 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1107 if (t
->qual
== FcQualAll
)
1114 FcValueDestroy (value
);
1119 static FcValueList
*
1120 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1126 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1129 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1130 if (e
->op
== FcOpComma
)
1132 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1133 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1137 l
->value
= FcConfigEvaluate (p
, e
);
1140 l
->binding
= binding
;
1141 if (l
->value
.type
== FcTypeVoid
)
1143 FcValueList
*next
= FcValueListNext(l
);
1145 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1154 FcConfigAdd (FcValueListPtr
*head
,
1155 FcValueList
*position
,
1159 FcValueListPtr
*prev
, last
, v
;
1160 FcValueBinding sameBinding
;
1163 sameBinding
= position
->binding
;
1165 sameBinding
= FcValueBindingWeak
;
1166 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1167 if (v
->binding
== FcValueBindingSame
)
1168 v
->binding
= sameBinding
;
1172 prev
= &position
->next
;
1174 for (prev
= head
; *prev
!= NULL
;
1175 prev
= &(*prev
)->next
)
1182 for (prev
= head
; *prev
!= NULL
;
1183 prev
= &(*prev
)->next
)
1185 if (*prev
== position
)
1192 if (FcDebug () & FC_DBG_EDIT
)
1195 printf ("position not on list\n");
1199 if (FcDebug () & FC_DBG_EDIT
)
1201 printf ("%s list before ", append
? "Append" : "Prepend");
1202 FcValueListPrint (*head
);
1209 while (last
->next
!= NULL
)
1216 if (FcDebug () & FC_DBG_EDIT
)
1218 printf ("%s list after ", append
? "Append" : "Prepend");
1219 FcValueListPrint (*head
);
1227 FcConfigDel (FcValueListPtr
*head
,
1228 FcValueList
*position
)
1230 FcValueListPtr
*prev
;
1232 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1234 if (*prev
== position
)
1236 *prev
= position
->next
;
1237 position
->next
= NULL
;
1238 FcValueListDestroy (position
);
1245 FcConfigPatternAdd (FcPattern
*p
,
1252 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1256 FcConfigAdd (&e
->values
, 0, append
, list
);
1261 * Delete all values associated with a field
1264 FcConfigPatternDel (FcPattern
*p
,
1267 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1270 while (e
->values
!= NULL
)
1271 FcConfigDel (&e
->values
, e
->values
);
1275 FcConfigPatternCanon (FcPattern
*p
,
1278 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1281 if (e
->values
== NULL
)
1282 FcPatternObjectDel (p
, object
);
1286 FcConfigSubstituteWithPat (FcConfig
*config
,
1301 config
= FcConfigGetCurrent ();
1306 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1307 if (!st
&& config
->maxObjects
)
1309 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1311 if (FcDebug () & FC_DBG_EDIT
)
1313 printf ("FcConfigSubstitute ");
1316 if (kind
== FcMatchPattern
)
1317 s
= config
->substPattern
;
1319 s
= config
->substFont
;
1320 for (; s
; s
= s
->next
)
1323 * Check the tests to see if
1324 * they all match the pattern
1326 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1328 if (FcDebug () & FC_DBG_EDIT
)
1330 printf ("FcConfigSubstitute test ");
1334 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1339 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1343 * If there's no such field in the font,
1344 * then FcQualAll matches while FcQualAny does not
1348 if (t
->qual
== FcQualAll
)
1357 * Check to see if there is a match, mark the location
1358 * to apply match-relative edits
1360 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1363 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1365 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1370 if (FcDebug () & FC_DBG_EDIT
)
1371 printf ("No match\n");
1374 if (FcDebug () & FC_DBG_EDIT
)
1376 printf ("Substitute ");
1379 for (e
= s
->edit
; e
; e
= e
->next
)
1382 * Evaluate the list of expressions
1384 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1386 * Locate any test associated with this field, skipping
1387 * tests associated with the pattern when substituting in
1390 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1392 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1393 t
->object
== e
->object
)
1396 * KLUDGE - the pattern may have been reallocated or
1397 * things may have been inserted or deleted above
1398 * this element by other edits. Go back and find
1401 if (e
!= s
->edit
&& st
[i
].elt
)
1402 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1411 * If there was a test, then replace the matched
1412 * value with the new list of values
1416 FcValueList
*thisValue
= st
[i
].value
;
1417 FcValueList
*nextValue
= thisValue
;
1420 * Append the new list of values after the current value
1422 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1424 * Delete the marked value
1427 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1429 * Adjust any pointers into the value list to ensure
1430 * future edits occur at the same place
1432 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1434 if (st
[i
].value
== thisValue
)
1435 st
[i
].value
= nextValue
;
1439 /* fall through ... */
1440 case FcOpAssignReplace
:
1442 * Delete all of the values and insert
1445 FcConfigPatternDel (p
, e
->object
);
1446 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1448 * Adjust any pointers into the value list as they no
1449 * longer point to anything valid
1453 FcPatternElt
*thisElt
= st
[i
].elt
;
1454 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1456 if (st
[i
].elt
== thisElt
)
1464 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1467 /* fall through ... */
1468 case FcOpPrependFirst
:
1469 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1474 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1477 /* fall through ... */
1478 case FcOpAppendLast
:
1479 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1482 FcValueListDestroy (l
);
1487 * Now go through the pattern and eliminate
1488 * any properties without data
1490 for (e
= s
->edit
; e
; e
= e
->next
)
1491 FcConfigPatternCanon (p
, e
->object
);
1493 if (FcDebug () & FC_DBG_EDIT
)
1495 printf ("FcConfigSubstitute edit");
1499 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1501 if (FcDebug () & FC_DBG_EDIT
)
1503 printf ("FcConfigSubstitute done");
1510 FcConfigSubstitute (FcConfig
*config
,
1514 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1517 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1519 static FcChar8 fontconfig_path
[1000] = "";
1522 DllMain (HINSTANCE hinstDLL
,
1528 switch (fdwReason
) {
1529 case DLL_PROCESS_ATTACH
:
1530 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1531 sizeof (fontconfig_path
)))
1534 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1535 * assume it's a Unix-style installation tree, and use
1536 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1537 * folder where the DLL is as FONTCONFIG_PATH.
1539 p
= strrchr (fontconfig_path
, '\\');
1543 p
= strrchr (fontconfig_path
, '\\');
1544 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1545 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1547 strcat (fontconfig_path
, "\\etc\\fonts");
1550 fontconfig_path
[0] = '\0';
1558 #undef FONTCONFIG_PATH
1559 #define FONTCONFIG_PATH fontconfig_path
1561 #else /* !(_WIN32 && PIC) */
1563 #endif /* !(_WIN32 && PIC) */
1565 #ifndef FONTCONFIG_FILE
1566 #define FONTCONFIG_FILE "fonts.conf"
1570 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1575 dir
= (FcChar8
*) "";
1576 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1580 strcpy ((char *) path
, (const char *) dir
);
1581 /* make sure there's a single separator */
1583 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1584 path
[strlen((char *) path
)-1] != '\\')) &&
1587 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1588 strcat ((char *) path
, "\\");
1590 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1591 strcat ((char *) path
, "/");
1593 strcat ((char *) path
, (char *) file
);
1595 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1596 if (access ((char *) path
, R_OK
) == 0)
1604 FcConfigGetPath (void)
1607 FcChar8
*env
, *e
, *colon
;
1612 npath
= 2; /* default dir + null */
1613 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1619 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1622 path
= calloc (npath
, sizeof (FcChar8
*));
1632 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1634 colon
= e
+ strlen ((char *) e
);
1635 path
[i
] = malloc (colon
- e
+ 1);
1638 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1639 path
[i
][colon
- e
] = '\0';
1648 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1649 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1652 strcpy ((char *) path
[i
], (const char *) dir
);
1656 for (i
= 0; path
[i
]; i
++)
1664 FcConfigFreePath (FcChar8
**path
)
1668 for (p
= path
; *p
; p
++)
1673 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1678 if (_FcConfigHomeEnabled
)
1680 char *home
= getenv ("HOME");
1684 home
= getenv ("USERPROFILE");
1687 return (FcChar8
*) home
;
1693 FcConfigEnableHome (FcBool enable
)
1695 FcBool prev
= _FcConfigHomeEnabled
;
1696 _FcConfigHomeEnabled
= enable
;
1701 FcConfigFilename (const FcChar8
*url
)
1703 FcChar8
*file
, *dir
, **path
, **p
;
1707 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1709 url
= (FcChar8
*) FONTCONFIG_FILE
;
1714 if (isalpha (*url
) &&
1716 (url
[2] == '/' || url
[2] == '\\'))
1722 dir
= FcConfigHome ();
1724 file
= FcConfigFileExists (dir
, url
+ 1);
1733 file
= FcConfigFileExists (0, url
);
1736 path
= FcConfigGetPath ();
1739 for (p
= path
; *p
; p
++)
1741 file
= FcConfigFileExists (*p
, url
);
1745 FcConfigFreePath (path
);
1752 * Manage the application-specific fonts
1756 FcConfigAppFontAddFile (FcConfig
*config
,
1757 const FcChar8
*file
)
1766 config
= FcConfigGetCurrent ();
1771 subdirs
= FcStrSetCreate ();
1775 set
= FcConfigGetFonts (config
, FcSetApplication
);
1778 set
= FcFontSetCreate ();
1781 FcStrSetDestroy (subdirs
);
1784 FcConfigSetFonts (config
, set
, FcSetApplication
);
1787 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, FcFalse
, config
))
1789 FcStrSetDestroy (subdirs
);
1792 if ((sublist
= FcStrListCreate (subdirs
)))
1794 while ((subdir
= FcStrListNext (sublist
)))
1796 FcConfigAppFontAddDir (config
, subdir
);
1798 FcStrListDone (sublist
);
1800 FcStrSetDestroy (subdirs
);
1805 FcConfigAppFontAddDir (FcConfig
*config
,
1815 config
= FcConfigGetCurrent ();
1819 subdirs
= FcStrSetCreate ();
1823 set
= FcConfigGetFonts (config
, FcSetApplication
);
1826 set
= FcFontSetCreate ();
1829 FcStrSetDestroy (subdirs
);
1832 FcConfigSetFonts (config
, set
, FcSetApplication
);
1835 if (!FcDirScanConfig (set
, subdirs
, config
->blanks
, dir
, FcFalse
, config
))
1837 FcStrSetDestroy (subdirs
);
1840 if ((sublist
= FcStrListCreate (subdirs
)))
1842 while ((subdir
= FcStrListNext (sublist
)))
1844 FcConfigAppFontAddDir (config
, subdir
);
1846 FcStrListDone (sublist
);
1848 FcStrSetDestroy (subdirs
);
1853 FcConfigAppFontClear (FcConfig
*config
)
1857 config
= FcConfigGetCurrent ();
1862 FcConfigSetFonts (config
, 0, FcSetApplication
);
1866 * Manage filename-based font source selectors
1870 FcConfigGlobAdd (FcConfig
*config
,
1871 const FcChar8
*glob
,
1874 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1876 return FcStrSetAdd (set
, glob
);
1880 FcConfigGlobMatch (const FcChar8
*glob
,
1881 const FcChar8
*string
)
1885 while ((c
= *glob
++))
1889 /* short circuit common case */
1892 /* short circuit another common case */
1893 if (strchr ((char *) glob
, '*') == 0)
1894 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1897 if (FcConfigGlobMatch (glob
, string
))
1903 if (*string
++ == '\0')
1912 return *string
== '\0';
1916 FcConfigGlobsMatch (const FcStrSet
*globs
,
1917 const FcChar8
*string
)
1921 for (i
= 0; i
< globs
->num
; i
++)
1922 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1928 FcConfigAcceptFilename (FcConfig
*config
,
1929 const FcChar8
*filename
)
1931 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1933 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1939 * Manage font-pattern based font source selectors
1943 FcConfigPatternsAdd (FcConfig
*config
,
1947 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1949 return FcFontSetAdd (set
, pattern
);
1953 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1954 const FcPattern
*font
)
1958 for (i
= 0; i
< patterns
->nfont
; i
++)
1959 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1965 FcConfigAcceptFont (FcConfig
*config
,
1966 const FcPattern
*font
)
1968 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1970 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))