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
)
80 config
->cacheDirs
= FcStrSetCreate ();
81 if (!config
->cacheDirs
)
86 config
->substPattern
= 0;
87 config
->substFont
= 0;
88 config
->substScan
= 0;
89 config
->maxObjects
= 0;
90 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
91 config
->fonts
[set
] = 0;
93 config
->rescanTime
= time(0);
94 config
->rescanInterval
= 30;
99 FcFontSetDestroy (config
->rejectPatterns
);
101 FcFontSetDestroy (config
->acceptPatterns
);
103 FcStrSetDestroy (config
->rejectGlobs
);
105 FcStrSetDestroy (config
->acceptGlobs
);
107 FcStrSetDestroy (config
->fontDirs
);
109 FcStrSetDestroy (config
->configFiles
);
111 FcStrSetDestroy (config
->configDirs
);
114 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
120 FcConfigNewestFile (FcStrSet
*files
)
122 FcStrList
*list
= FcStrListCreate (files
);
123 FcFileTime newest
= { 0, FcFalse
};
129 while ((file
= FcStrListNext (list
)))
130 if (stat ((char *) file
, &statb
) == 0)
131 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
134 newest
.time
= statb
.st_mtime
;
136 FcStrListDone (list
);
142 FcConfigUptoDate (FcConfig
*config
)
144 FcFileTime config_time
, config_dir_time
, font_time
;
145 time_t now
= time(0);
148 config
= FcConfigGetCurrent ();
152 config_time
= FcConfigNewestFile (config
->configFiles
);
153 config_dir_time
= FcConfigNewestFile (config
->configDirs
);
154 font_time
= FcConfigNewestFile (config
->fontDirs
);
155 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
156 (config_dir_time
.set
&& (config_dir_time
.time
- config
->rescanTime
) > 0) ||
157 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
161 config
->rescanTime
= now
;
166 FcSubstDestroy (FcSubst
*s
)
174 FcTestDestroy (s
->test
);
176 FcEditDestroy (s
->edit
);
178 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
184 FcConfigDestroy (FcConfig
*config
)
188 if (config
== _fcConfig
)
191 FcStrSetDestroy (config
->configDirs
);
192 FcStrSetDestroy (config
->fontDirs
);
193 FcStrSetDestroy (config
->cacheDirs
);
194 FcStrSetDestroy (config
->configFiles
);
195 FcStrSetDestroy (config
->acceptGlobs
);
196 FcStrSetDestroy (config
->rejectGlobs
);
197 FcFontSetDestroy (config
->acceptPatterns
);
198 FcFontSetDestroy (config
->rejectPatterns
);
201 FcBlanksDestroy (config
->blanks
);
203 FcSubstDestroy (config
->substPattern
);
204 FcSubstDestroy (config
->substFont
);
205 FcSubstDestroy (config
->substScan
);
206 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
207 if (config
->fonts
[set
])
208 FcFontSetDestroy (config
->fonts
[set
]);
211 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
215 * Add cache to configuration, adding fonts and directories
219 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
,
220 FcSetName set
, FcStrSet
*dirSet
)
229 fs
= FcCacheSet (cache
);
234 for (i
= 0; i
< fs
->nfont
; i
++)
236 FcPattern
*font
= FcFontSetFont (fs
, i
);
240 * Check to see if font is banned by filename
242 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
243 0, &font_file
) == FcResultMatch
&&
244 !FcConfigAcceptFilename (config
, font_file
))
250 * Check to see if font is banned by pattern
252 if (!FcConfigAcceptFont (config
, font
))
256 FcFontSetAdd (config
->fonts
[set
], font
);
258 FcDirCacheReference (cache
, nref
);
264 dirs
= FcCacheDirs (cache
);
267 for (i
= 0; i
< cache
->dirs_count
; i
++)
269 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
270 if (FcConfigAcceptFilename (config
, dir
))
271 FcStrSetAddFilename (dirSet
, dir
);
278 FcConfigAddDirList (FcConfig
*config
, FcSetName set
, FcStrSet
*dirSet
)
284 dirlist
= FcStrListCreate (dirSet
);
288 while ((dir
= FcStrListNext (dirlist
)))
290 if (FcDebug () & FC_DBG_FONTSET
)
291 printf ("adding fonts from%s\n", dir
);
292 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
295 FcConfigAddCache (config
, cache
, set
, dirSet
);
296 FcDirCacheUnload (cache
);
298 FcStrListDone (dirlist
);
303 * Scan the current list of directories in the configuration
304 * and build the set of available fonts.
308 FcConfigBuildFonts (FcConfig
*config
)
314 config
= FcConfigGetCurrent ();
319 fonts
= FcFontSetCreate ();
323 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
325 if (!FcConfigAddDirList (config
, FcSetSystem
, config
->fontDirs
))
327 if (FcDebug () & FC_DBG_FONTSET
)
328 FcFontSetPrint (fonts
);
333 FcConfigSetCurrent (FcConfig
*config
)
335 if (config
== _fcConfig
)
339 if (!FcConfigBuildFonts (config
))
343 FcConfigDestroy (_fcConfig
);
349 FcConfigGetCurrent (void)
358 FcConfigAddConfigDir (FcConfig
*config
,
361 return FcStrSetAddFilename (config
->configDirs
, d
);
365 FcConfigGetConfigDirs (FcConfig
*config
)
369 config
= FcConfigGetCurrent ();
373 return FcStrListCreate (config
->configDirs
);
377 FcConfigAddFontDir (FcConfig
*config
,
380 return FcStrSetAddFilename (config
->fontDirs
, d
);
384 FcConfigAddDir (FcConfig
*config
,
387 return (FcConfigAddConfigDir (config
, d
) &&
388 FcConfigAddFontDir (config
, d
));
392 FcConfigGetFontDirs (FcConfig
*config
)
396 config
= FcConfigGetCurrent ();
400 return FcStrListCreate (config
->fontDirs
);
404 FcConfigAddCacheDir (FcConfig
*config
,
407 return FcStrSetAddFilename (config
->cacheDirs
, d
);
411 FcConfigGetCacheDirs (FcConfig
*config
)
415 config
= FcConfigGetCurrent ();
419 return FcStrListCreate (config
->cacheDirs
);
423 FcConfigAddConfigFile (FcConfig
*config
,
427 FcChar8
*file
= FcConfigFilename (f
);
432 ret
= FcStrSetAdd (config
->configFiles
, file
);
438 FcConfigGetConfigFiles (FcConfig
*config
)
442 config
= FcConfigGetCurrent ();
446 return FcStrListCreate (config
->configFiles
);
450 FcConfigGetCache (FcConfig
*config
)
456 FcConfigGetFonts (FcConfig
*config
,
461 config
= FcConfigGetCurrent ();
465 return config
->fonts
[set
];
469 FcConfigSetFonts (FcConfig
*config
,
473 if (config
->fonts
[set
])
474 FcFontSetDestroy (config
->fonts
[set
]);
475 config
->fonts
[set
] = fonts
;
479 FcConfigGetBlanks (FcConfig
*config
)
483 config
= FcConfigGetCurrent ();
487 return config
->blanks
;
491 FcConfigAddBlank (FcConfig
*config
,
494 FcBlanks
*b
, *freeme
= 0;
499 freeme
= b
= FcBlanksCreate ();
503 if (!FcBlanksAdd (b
, blank
))
506 FcBlanksDestroy (freeme
);
514 FcConfigGetRescanInterval (FcConfig
*config
)
518 config
= FcConfigGetCurrent ();
522 return config
->rescanInterval
;
526 FcConfigSetRescanInterval (FcConfig
*config
, int rescanInterval
)
530 config
= FcConfigGetCurrent ();
534 config
->rescanInterval
= rescanInterval
;
539 FcConfigAddEdit (FcConfig
*config
,
544 FcSubst
*subst
, **prev
;
550 prev
= &config
->substPattern
;
553 prev
= &config
->substFont
;
556 prev
= &config
->substScan
;
561 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
564 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
565 for (; *prev
; prev
= &(*prev
)->next
);
571 for (t
= test
; t
; t
= t
->next
)
573 if (t
->kind
== FcMatchDefault
)
577 if (config
->maxObjects
< num
)
578 config
->maxObjects
= num
;
579 if (FcDebug () & FC_DBG_EDIT
)
581 printf ("Add Subst ");
582 FcSubstPrint (subst
);
587 typedef struct _FcSubState
{
593 FcConfigPromote (FcValue v
, FcValue u
)
595 if (v
.type
== FcTypeInteger
)
597 v
.type
= FcTypeDouble
;
598 v
.u
.d
= (double) v
.u
.i
;
600 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
602 v
.u
.m
= &FcIdentityMatrix
;
603 v
.type
= FcTypeMatrix
;
605 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
607 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
608 v
.type
= FcTypeLangSet
;
614 FcConfigCompareValue (const FcValue
*left_o
,
616 const FcValue
*right_o
)
618 FcValue left
= FcValueCanonicalize(left_o
);
619 FcValue right
= FcValueCanonicalize(right_o
);
620 FcBool ret
= FcFalse
;
622 left
= FcConfigPromote (left
, right
);
623 right
= FcConfigPromote (right
, left
);
624 if (left
.type
== right
.type
)
628 break; /* FcConfigPromote prevents this from happening */
634 ret
= left
.u
.d
== right
.u
.d
;
637 case FcOpNotContains
:
638 ret
= left
.u
.d
!= right
.u
.d
;
641 ret
= left
.u
.d
< right
.u
.d
;
644 ret
= left
.u
.d
<= right
.u
.d
;
647 ret
= left
.u
.d
> right
.u
.d
;
650 ret
= left
.u
.d
>= right
.u
.d
;
661 ret
= left
.u
.b
== right
.u
.b
;
664 case FcOpNotContains
:
665 ret
= left
.u
.b
!= right
.u
.b
;
675 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
678 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
681 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
683 case FcOpNotContains
:
684 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
695 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
698 case FcOpNotContains
:
699 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
709 /* left contains right if right is a subset of left */
710 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
712 case FcOpNotContains
:
713 /* left contains right if right is a subset of left */
714 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
717 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
720 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
730 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
732 case FcOpNotContains
:
733 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
736 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
739 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
761 ret
= left
.u
.f
== right
.u
.f
;
764 case FcOpNotContains
:
765 ret
= left
.u
.f
!= right
.u
.f
;
775 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
782 #define _FcDoubleFloor(d) ((int) (d))
783 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
784 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
785 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
786 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
787 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
790 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
799 v
.type
= FcTypeInteger
;
803 v
.type
= FcTypeDouble
;
807 v
.type
= FcTypeString
;
808 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
811 v
.type
= FcTypeMatrix
;
816 v
.type
= FcTypeCharSet
;
825 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
826 if (r
!= FcResultMatch
)
831 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
832 v
.type
= FcTypeInteger
;
837 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
838 if (vl
.type
== FcTypeBool
)
841 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
843 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
856 case FcOpNotContains
:
858 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
859 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
861 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
871 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
872 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
873 vl
= FcConfigPromote (vl
, vr
);
874 vr
= FcConfigPromote (vr
, vl
);
875 if (vl
.type
== vr
.type
)
881 v
.type
= FcTypeDouble
;
882 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
885 v
.type
= FcTypeDouble
;
886 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
889 v
.type
= FcTypeDouble
;
890 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
893 v
.type
= FcTypeDouble
;
894 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
900 if (v
.type
== FcTypeDouble
&&
901 v
.u
.d
== (double) (int) v
.u
.d
)
903 v
.type
= FcTypeInteger
;
911 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
915 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
925 v
.type
= FcTypeString
;
926 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
927 v
.u
.s
= FcStrStaticName (str
);
941 v
.type
= FcTypeMatrix
;
942 m
= malloc (sizeof (FcMatrix
));
945 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
946 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
970 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
983 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
989 v
.type
= FcTypeInteger
;
990 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
999 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1005 v
.type
= FcTypeInteger
;
1006 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1009 v
.type
= FcTypeVoid
;
1012 FcValueDestroy (vl
);
1015 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1021 v
.type
= FcTypeInteger
;
1022 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1025 v
.type
= FcTypeVoid
;
1028 FcValueDestroy (vl
);
1031 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1037 v
.type
= FcTypeInteger
;
1038 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1041 v
.type
= FcTypeVoid
;
1044 FcValueDestroy (vl
);
1047 v
.type
= FcTypeVoid
;
1053 static FcValueList
*
1054 FcConfigMatchValueList (FcPattern
*p
,
1056 FcValueList
*values
)
1058 FcValueList
*ret
= 0;
1059 FcExpr
*e
= t
->expr
;
1065 /* Compute the value of the match expression */
1066 if (e
->op
== FcOpComma
)
1068 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1069 e
= e
->u
.tree
.right
;
1073 value
= FcConfigEvaluate (p
, e
);
1077 for (v
= values
; v
; v
= FcValueListNext(v
))
1079 /* Compare the pattern value to the match expression value */
1080 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1087 if (t
->qual
== FcQualAll
)
1094 FcValueDestroy (value
);
1099 static FcValueList
*
1100 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1106 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1109 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1110 if (e
->op
== FcOpComma
)
1112 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1113 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1117 l
->value
= FcConfigEvaluate (p
, e
);
1120 l
->binding
= binding
;
1121 if (l
->value
.type
== FcTypeVoid
)
1123 FcValueList
*next
= FcValueListNext(l
);
1125 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1134 FcConfigAdd (FcValueListPtr
*head
,
1135 FcValueList
*position
,
1139 FcValueListPtr
*prev
, last
, v
;
1140 FcValueBinding sameBinding
;
1143 sameBinding
= position
->binding
;
1145 sameBinding
= FcValueBindingWeak
;
1146 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1147 if (v
->binding
== FcValueBindingSame
)
1148 v
->binding
= sameBinding
;
1152 prev
= &position
->next
;
1154 for (prev
= head
; *prev
!= NULL
;
1155 prev
= &(*prev
)->next
)
1162 for (prev
= head
; *prev
!= NULL
;
1163 prev
= &(*prev
)->next
)
1165 if (*prev
== position
)
1172 if (FcDebug () & FC_DBG_EDIT
)
1175 printf ("position not on list\n");
1179 if (FcDebug () & FC_DBG_EDIT
)
1181 printf ("%s list before ", append
? "Append" : "Prepend");
1182 FcValueListPrint (*head
);
1189 while (last
->next
!= NULL
)
1196 if (FcDebug () & FC_DBG_EDIT
)
1198 printf ("%s list after ", append
? "Append" : "Prepend");
1199 FcValueListPrint (*head
);
1207 FcConfigDel (FcValueListPtr
*head
,
1208 FcValueList
*position
)
1210 FcValueListPtr
*prev
;
1212 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1214 if (*prev
== position
)
1216 *prev
= position
->next
;
1217 position
->next
= NULL
;
1218 FcValueListDestroy (position
);
1225 FcConfigPatternAdd (FcPattern
*p
,
1232 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1236 FcConfigAdd (&e
->values
, 0, append
, list
);
1241 * Delete all values associated with a field
1244 FcConfigPatternDel (FcPattern
*p
,
1247 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1250 while (e
->values
!= NULL
)
1251 FcConfigDel (&e
->values
, e
->values
);
1255 FcConfigPatternCanon (FcPattern
*p
,
1258 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1261 if (e
->values
== NULL
)
1262 FcPatternObjectDel (p
, object
);
1266 FcConfigSubstituteWithPat (FcConfig
*config
,
1281 config
= FcConfigGetCurrent ();
1287 case FcMatchPattern
:
1288 s
= config
->substPattern
;
1291 s
= config
->substFont
;
1294 s
= config
->substScan
;
1300 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1301 if (!st
&& config
->maxObjects
)
1303 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1305 if (FcDebug () & FC_DBG_EDIT
)
1307 printf ("FcConfigSubstitute ");
1310 for (; s
; s
= s
->next
)
1313 * Check the tests to see if
1314 * they all match the pattern
1316 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1318 if (FcDebug () & FC_DBG_EDIT
)
1320 printf ("FcConfigSubstitute test ");
1324 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1329 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1333 * If there's no such field in the font,
1334 * then FcQualAll matches while FcQualAny does not
1338 if (t
->qual
== FcQualAll
)
1347 * Check to see if there is a match, mark the location
1348 * to apply match-relative edits
1350 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1353 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1355 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1360 if (FcDebug () & FC_DBG_EDIT
)
1361 printf ("No match\n");
1364 if (FcDebug () & FC_DBG_EDIT
)
1366 printf ("Substitute ");
1369 for (e
= s
->edit
; e
; e
= e
->next
)
1372 * Evaluate the list of expressions
1374 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1376 * Locate any test associated with this field, skipping
1377 * tests associated with the pattern when substituting in
1380 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1382 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1383 t
->object
== e
->object
)
1386 * KLUDGE - the pattern may have been reallocated or
1387 * things may have been inserted or deleted above
1388 * this element by other edits. Go back and find
1391 if (e
!= s
->edit
&& st
[i
].elt
)
1392 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1401 * If there was a test, then replace the matched
1402 * value with the new list of values
1406 FcValueList
*thisValue
= st
[i
].value
;
1407 FcValueList
*nextValue
= thisValue
;
1410 * Append the new list of values after the current value
1412 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1414 * Delete the marked value
1417 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1419 * Adjust any pointers into the value list to ensure
1420 * future edits occur at the same place
1422 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1424 if (st
[i
].value
== thisValue
)
1425 st
[i
].value
= nextValue
;
1429 /* fall through ... */
1430 case FcOpAssignReplace
:
1432 * Delete all of the values and insert
1435 FcConfigPatternDel (p
, e
->object
);
1436 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1438 * Adjust any pointers into the value list as they no
1439 * longer point to anything valid
1443 FcPatternElt
*thisElt
= st
[i
].elt
;
1444 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1446 if (st
[i
].elt
== thisElt
)
1454 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1457 /* fall through ... */
1458 case FcOpPrependFirst
:
1459 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1464 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1467 /* fall through ... */
1468 case FcOpAppendLast
:
1469 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1472 FcValueListDestroy (l
);
1477 * Now go through the pattern and eliminate
1478 * any properties without data
1480 for (e
= s
->edit
; e
; e
= e
->next
)
1481 FcConfigPatternCanon (p
, e
->object
);
1483 if (FcDebug () & FC_DBG_EDIT
)
1485 printf ("FcConfigSubstitute edit");
1489 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1491 if (FcDebug () & FC_DBG_EDIT
)
1493 printf ("FcConfigSubstitute done");
1500 FcConfigSubstitute (FcConfig
*config
,
1504 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1507 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1509 static FcChar8 fontconfig_path
[1000] = "";
1512 DllMain (HINSTANCE hinstDLL
,
1518 switch (fdwReason
) {
1519 case DLL_PROCESS_ATTACH
:
1520 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1521 sizeof (fontconfig_path
)))
1524 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1525 * assume it's a Unix-style installation tree, and use
1526 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1527 * folder where the DLL is as FONTCONFIG_PATH.
1529 p
= strrchr (fontconfig_path
, '\\');
1533 p
= strrchr (fontconfig_path
, '\\');
1534 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1535 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1537 strcat (fontconfig_path
, "\\etc\\fonts");
1540 fontconfig_path
[0] = '\0';
1548 #undef FONTCONFIG_PATH
1549 #define FONTCONFIG_PATH fontconfig_path
1551 #else /* !(_WIN32 && PIC) */
1553 #endif /* !(_WIN32 && PIC) */
1555 #ifndef FONTCONFIG_FILE
1556 #define FONTCONFIG_FILE "fonts.conf"
1560 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1565 dir
= (FcChar8
*) "";
1566 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1570 strcpy ((char *) path
, (const char *) dir
);
1571 /* make sure there's a single separator */
1573 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1574 path
[strlen((char *) path
)-1] != '\\')) &&
1577 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1578 strcat ((char *) path
, "\\");
1580 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1581 strcat ((char *) path
, "/");
1583 strcat ((char *) path
, (char *) file
);
1585 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1586 if (access ((char *) path
, R_OK
) == 0)
1594 FcConfigGetPath (void)
1597 FcChar8
*env
, *e
, *colon
;
1602 npath
= 2; /* default dir + null */
1603 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1609 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1612 path
= calloc (npath
, sizeof (FcChar8
*));
1622 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1624 colon
= e
+ strlen ((char *) e
);
1625 path
[i
] = malloc (colon
- e
+ 1);
1628 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1629 path
[i
][colon
- e
] = '\0';
1638 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1639 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1642 strcpy ((char *) path
[i
], (const char *) dir
);
1646 for (i
= 0; path
[i
]; i
++)
1654 FcConfigFreePath (FcChar8
**path
)
1658 for (p
= path
; *p
; p
++)
1663 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1668 if (_FcConfigHomeEnabled
)
1670 char *home
= getenv ("HOME");
1674 home
= getenv ("USERPROFILE");
1677 return (FcChar8
*) home
;
1683 FcConfigEnableHome (FcBool enable
)
1685 FcBool prev
= _FcConfigHomeEnabled
;
1686 _FcConfigHomeEnabled
= enable
;
1691 FcConfigFilename (const FcChar8
*url
)
1693 FcChar8
*file
, *dir
, **path
, **p
;
1697 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1699 url
= (FcChar8
*) FONTCONFIG_FILE
;
1704 if (isalpha (*url
) &&
1706 (url
[2] == '/' || url
[2] == '\\'))
1712 dir
= FcConfigHome ();
1714 file
= FcConfigFileExists (dir
, url
+ 1);
1723 file
= FcConfigFileExists (0, url
);
1726 path
= FcConfigGetPath ();
1729 for (p
= path
; *p
; p
++)
1731 file
= FcConfigFileExists (*p
, url
);
1735 FcConfigFreePath (path
);
1742 * Manage the application-specific fonts
1746 FcConfigAppFontAddFile (FcConfig
*config
,
1747 const FcChar8
*file
)
1756 config
= FcConfigGetCurrent ();
1761 subdirs
= FcStrSetCreate ();
1765 set
= FcConfigGetFonts (config
, FcSetApplication
);
1768 set
= FcFontSetCreate ();
1771 FcStrSetDestroy (subdirs
);
1774 FcConfigSetFonts (config
, set
, FcSetApplication
);
1777 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1779 FcStrSetDestroy (subdirs
);
1782 if ((sublist
= FcStrListCreate (subdirs
)))
1784 while ((subdir
= FcStrListNext (sublist
)))
1786 FcConfigAppFontAddDir (config
, subdir
);
1788 FcStrListDone (sublist
);
1790 FcStrSetDestroy (subdirs
);
1795 FcConfigAppFontAddDir (FcConfig
*config
,
1803 config
= FcConfigGetCurrent ();
1808 dirs
= FcStrSetCreate ();
1812 set
= FcConfigGetFonts (config
, FcSetApplication
);
1815 set
= FcFontSetCreate ();
1818 FcStrSetDestroy (dirs
);
1821 FcConfigSetFonts (config
, set
, FcSetApplication
);
1824 FcStrSetAddFilename (dirs
, dir
);
1826 if (!FcConfigAddDirList (config
, FcSetApplication
, dirs
))
1828 FcStrSetDestroy (dirs
);
1831 FcStrSetDestroy (dirs
);
1836 FcConfigAppFontClear (FcConfig
*config
)
1840 config
= FcConfigGetCurrent ();
1845 FcConfigSetFonts (config
, 0, FcSetApplication
);
1849 * Manage filename-based font source selectors
1853 FcConfigGlobAdd (FcConfig
*config
,
1854 const FcChar8
*glob
,
1857 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1859 return FcStrSetAdd (set
, glob
);
1863 FcConfigGlobMatch (const FcChar8
*glob
,
1864 const FcChar8
*string
)
1868 while ((c
= *glob
++))
1872 /* short circuit common case */
1875 /* short circuit another common case */
1876 if (strchr ((char *) glob
, '*') == 0)
1877 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1880 if (FcConfigGlobMatch (glob
, string
))
1886 if (*string
++ == '\0')
1895 return *string
== '\0';
1899 FcConfigGlobsMatch (const FcStrSet
*globs
,
1900 const FcChar8
*string
)
1904 for (i
= 0; i
< globs
->num
; i
++)
1905 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1911 FcConfigAcceptFilename (FcConfig
*config
,
1912 const FcChar8
*filename
)
1914 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1916 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1922 * Manage font-pattern based font source selectors
1926 FcConfigPatternsAdd (FcConfig
*config
,
1930 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1932 return FcFontSetAdd (set
, pattern
);
1936 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1937 const FcPattern
*font
)
1941 for (i
= 0; i
< patterns
->nfont
; i
++)
1942 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1948 FcConfigAcceptFont (FcConfig
*config
,
1949 const FcPattern
*font
)
1951 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1953 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))
1958 #include "fcaliastail.h"