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
;
268 FcGlobalCache
*cache
;
273 fonts
= FcFontSetCreate ();
277 cache
= FcGlobalCacheCreate ();
281 oldDirs
= FcStrSetCreate ();
286 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
, config
);
288 cached_fonts
= FcCacheRead(config
, cache
);
291 list
= FcConfigGetFontDirs (config
);
295 while ((dir
= FcStrListNext (list
)))
297 if (FcDebug () & FC_DBG_FONTSET
)
298 printf ("build scan dir %s\n", dir
);
299 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
300 config
->blanks
, dir
, FcFalse
, config
);
303 FcStrListDone (list
);
309 for (i
= 0; i
< oldDirs
->num
; i
++)
311 if (FcDebug () & FC_DBG_FONTSET
)
312 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
313 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
314 config
->blanks
, oldDirs
->strs
[i
],
318 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
321 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
323 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
324 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
325 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
327 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
329 cached_fonts
->nfont
= 0;
330 FcFontSetDestroy (cached_fonts
);
333 if (FcDebug () & FC_DBG_FONTSET
)
334 FcFontSetPrint (fonts
);
337 FcGlobalCacheSave (cache
, config
->cache
, config
);
338 FcGlobalCacheDestroy (cache
);
339 FcStrSetDestroy (oldDirs
);
341 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
345 FcStrSetDestroy (oldDirs
);
347 FcGlobalCacheDestroy (cache
);
349 FcFontSetDestroy (fonts
);
355 FcConfigSetCurrent (FcConfig
*config
)
358 if (!FcConfigBuildFonts (config
))
362 FcConfigDestroy (_fcConfig
);
368 FcConfigGetCurrent (void)
377 FcConfigAddConfigDir (FcConfig
*config
,
380 return FcStrSetAddFilename (config
->configDirs
, d
);
384 FcConfigGetConfigDirs (FcConfig
*config
)
388 config
= FcConfigGetCurrent ();
392 return FcStrListCreate (config
->configDirs
);
396 FcConfigAddFontDir (FcConfig
*config
,
399 return FcStrSetAddFilename (config
->fontDirs
, d
);
403 FcConfigAddDir (FcConfig
*config
,
406 return (FcConfigAddConfigDir (config
, d
) &&
407 FcConfigAddFontDir (config
, d
));
411 FcConfigGetFontDirs (FcConfig
*config
)
415 config
= FcConfigGetCurrent ();
419 return FcStrListCreate (config
->fontDirs
);
423 FcConfigAddCacheDir (FcConfig
*config
,
426 return FcStrSetAddFilename (config
->cacheDirs
, d
);
430 FcConfigGetCacheDirs (FcConfig
*config
)
434 config
= FcConfigGetCurrent ();
438 return FcStrListCreate (config
->cacheDirs
);
442 FcConfigAddConfigFile (FcConfig
*config
,
446 FcChar8
*file
= FcConfigFilename (f
);
451 ret
= FcStrSetAdd (config
->configFiles
, file
);
457 FcConfigGetConfigFiles (FcConfig
*config
)
461 config
= FcConfigGetCurrent ();
465 return FcStrListCreate (config
->configFiles
);
469 FcConfigSetCache (FcConfig
*config
,
472 FcChar8
*new = FcStrCopyFilename (c
);
477 FcStrFree (config
->cache
);
483 FcConfigGetCache (FcConfig
*config
)
487 config
= FcConfigGetCurrent ();
491 return config
->cache
;
495 FcConfigGetFonts (FcConfig
*config
,
500 config
= FcConfigGetCurrent ();
504 return config
->fonts
[set
];
508 FcConfigSetFonts (FcConfig
*config
,
512 if (config
->fonts
[set
])
513 FcFontSetDestroy (config
->fonts
[set
]);
514 config
->fonts
[set
] = fonts
;
520 FcConfigGetBlanks (FcConfig
*config
)
524 config
= FcConfigGetCurrent ();
528 return config
->blanks
;
532 FcConfigAddBlank (FcConfig
*config
,
535 FcBlanks
*b
, *freeme
= 0;
540 freeme
= b
= FcBlanksCreate ();
544 if (!FcBlanksAdd (b
, blank
))
547 FcBlanksDestroy (freeme
);
555 FcConfigGetRescanInverval (FcConfig
*config
)
559 config
= FcConfigGetCurrent ();
563 return config
->rescanInterval
;
567 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
571 config
= FcConfigGetCurrent ();
575 config
->rescanInterval
= rescanInterval
;
580 FcConfigAddEdit (FcConfig
*config
,
585 FcSubst
*subst
, **prev
;
589 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
592 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
593 if (kind
== FcMatchPattern
)
594 prev
= &config
->substPattern
;
596 prev
= &config
->substFont
;
597 for (; *prev
; prev
= &(*prev
)->next
);
603 for (t
= test
; t
; t
= t
->next
)
605 if (t
->kind
== FcMatchDefault
)
609 if (config
->maxObjects
< num
)
610 config
->maxObjects
= num
;
611 if (FcDebug () & FC_DBG_EDIT
)
613 printf ("Add Subst ");
614 FcSubstPrint (subst
);
619 typedef struct _FcSubState
{
625 FcConfigPromote (FcValue v
, FcValue u
)
627 if (v
.type
== FcTypeInteger
)
629 v
.type
= FcTypeDouble
;
630 v
.u
.d
= (double) v
.u
.i
;
632 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
634 v
.u
.m
= &FcIdentityMatrix
;
635 v
.type
= FcTypeMatrix
;
637 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
639 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
640 v
.type
= FcTypeLangSet
;
646 FcConfigCompareValue (const FcValue
*left_o
,
648 const FcValue
*right_o
)
650 FcValue left
= FcValueCanonicalize(left_o
);
651 FcValue right
= FcValueCanonicalize(right_o
);
652 FcBool ret
= FcFalse
;
654 left
= FcConfigPromote (left
, right
);
655 right
= FcConfigPromote (right
, left
);
656 if (left
.type
== right
.type
)
660 break; /* FcConfigPromote prevents this from happening */
666 ret
= left
.u
.d
== right
.u
.d
;
669 case FcOpNotContains
:
670 ret
= left
.u
.d
!= right
.u
.d
;
673 ret
= left
.u
.d
< right
.u
.d
;
676 ret
= left
.u
.d
<= right
.u
.d
;
679 ret
= left
.u
.d
> right
.u
.d
;
682 ret
= left
.u
.d
>= right
.u
.d
;
693 ret
= left
.u
.b
== right
.u
.b
;
696 case FcOpNotContains
:
697 ret
= left
.u
.b
!= right
.u
.b
;
707 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
710 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
713 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
715 case FcOpNotContains
:
716 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
727 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
730 case FcOpNotContains
:
731 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
741 /* left contains right if right is a subset of left */
742 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
744 case FcOpNotContains
:
745 /* left contains right if right is a subset of left */
746 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
749 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
752 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
762 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
764 case FcOpNotContains
:
765 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
768 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
771 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
793 ret
= left
.u
.f
== right
.u
.f
;
796 case FcOpNotContains
:
797 ret
= left
.u
.f
!= right
.u
.f
;
807 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
814 #define _FcDoubleFloor(d) ((int) (d))
815 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
816 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
817 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
818 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
819 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
822 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
831 v
.type
= FcTypeInteger
;
835 v
.type
= FcTypeDouble
;
839 v
.type
= FcTypeString
;
840 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
843 v
.type
= FcTypeMatrix
;
848 v
.type
= FcTypeCharSet
;
857 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
858 if (r
!= FcResultMatch
)
863 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
864 v
.type
= FcTypeInteger
;
869 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
870 if (vl
.type
== FcTypeBool
)
873 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
875 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
888 case FcOpNotContains
:
890 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
891 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
893 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
903 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
904 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
905 vl
= FcConfigPromote (vl
, vr
);
906 vr
= FcConfigPromote (vr
, vl
);
907 if (vl
.type
== vr
.type
)
913 v
.type
= FcTypeDouble
;
914 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
917 v
.type
= FcTypeDouble
;
918 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
921 v
.type
= FcTypeDouble
;
922 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
925 v
.type
= FcTypeDouble
;
926 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
932 if (v
.type
== FcTypeDouble
&&
933 v
.u
.d
== (double) (int) v
.u
.d
)
935 v
.type
= FcTypeInteger
;
943 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
947 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
957 v
.type
= FcTypeString
;
958 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
959 v
.u
.s
= FcStrStaticName (str
);
973 v
.type
= FcTypeMatrix
;
974 m
= malloc (sizeof (FcMatrix
));
977 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
978 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1002 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1005 v
.type
= FcTypeBool
;
1009 v
.type
= FcTypeVoid
;
1012 FcValueDestroy (vl
);
1015 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1021 v
.type
= FcTypeInteger
;
1022 v
.u
.i
= FcDoubleFloor (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
= FcDoubleCeil (vl
.u
.d
);
1041 v
.type
= FcTypeVoid
;
1044 FcValueDestroy (vl
);
1047 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1053 v
.type
= FcTypeInteger
;
1054 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1057 v
.type
= FcTypeVoid
;
1060 FcValueDestroy (vl
);
1063 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1069 v
.type
= FcTypeInteger
;
1070 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1073 v
.type
= FcTypeVoid
;
1076 FcValueDestroy (vl
);
1079 v
.type
= FcTypeVoid
;
1085 static FcValueList
*
1086 FcConfigMatchValueList (FcPattern
*p
,
1088 FcValueList
*values
)
1090 FcValueList
*ret
= 0;
1091 FcExpr
*e
= t
->expr
;
1097 /* Compute the value of the match expression */
1098 if (e
->op
== FcOpComma
)
1100 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1101 e
= e
->u
.tree
.right
;
1105 value
= FcConfigEvaluate (p
, e
);
1109 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1111 /* Compare the pattern value to the match expression value */
1112 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1119 if (t
->qual
== FcQualAll
)
1126 FcValueDestroy (value
);
1131 static FcValueList
*
1132 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1138 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1141 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1142 if (e
->op
== FcOpComma
)
1144 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1145 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1149 l
->value
= FcConfigEvaluate (p
, e
);
1150 l
->next
= FcValueListPtrCreateDynamic(0);
1152 l
->binding
= binding
;
1153 if (l
->value
.type
== FcTypeVoid
)
1155 FcValueList
*next
= FcValueListPtrU(l
->next
);
1157 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1166 FcConfigAdd (FcValueListPtr
*head
,
1167 FcValueList
*position
,
1171 FcValueListPtr
*prev
, last
, v
;
1172 FcValueBinding sameBinding
;
1175 sameBinding
= position
->binding
;
1177 sameBinding
= FcValueBindingWeak
;
1178 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1179 v
= FcValueListPtrU(v
)->next
)
1180 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1181 FcValueListPtrU(v
)->binding
= sameBinding
;
1185 prev
= &position
->next
;
1187 for (prev
= head
; FcValueListPtrU(*prev
);
1188 prev
= &(FcValueListPtrU(*prev
)->next
))
1195 for (prev
= head
; FcValueListPtrU(*prev
);
1196 prev
= &(FcValueListPtrU(*prev
)->next
))
1198 if (FcValueListPtrU(*prev
) == position
)
1205 if (FcDebug () & FC_DBG_EDIT
)
1207 if (!FcValueListPtrU(*prev
))
1208 printf ("position not on list\n");
1212 if (FcDebug () & FC_DBG_EDIT
)
1214 printf ("%s list before ", append
? "Append" : "Prepend");
1215 FcValueListPrint (*head
);
1221 last
= FcValueListPtrCreateDynamic(new);
1222 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1223 last
= FcValueListPtrU(last
)->next
;
1225 FcValueListPtrU(last
)->next
= *prev
;
1226 *prev
= FcValueListPtrCreateDynamic(new);
1229 if (FcDebug () & FC_DBG_EDIT
)
1231 printf ("%s list after ", append
? "Append" : "Prepend");
1232 FcValueListPrint (*head
);
1240 FcConfigDel (FcValueListPtr
*head
,
1241 FcValueList
*position
)
1243 FcValueListPtr
*prev
;
1245 for (prev
= head
; FcValueListPtrU(*prev
);
1246 prev
= &(FcValueListPtrU(*prev
)->next
))
1248 if (FcValueListPtrU(*prev
) == position
)
1250 *prev
= position
->next
;
1251 position
->next
= FcValueListPtrCreateDynamic(0);
1252 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1259 FcConfigPatternAdd (FcPattern
*p
,
1266 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1270 FcConfigAdd (&e
->values
, 0, append
, list
);
1275 * Delete all values associated with a field
1278 FcConfigPatternDel (FcPattern
*p
,
1281 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1284 while (FcValueListPtrU(e
->values
))
1285 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1289 FcConfigPatternCanon (FcPattern
*p
,
1292 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1295 if (!FcValueListPtrU(e
->values
))
1296 FcPatternDel (p
, object
);
1300 FcConfigSubstituteWithPat (FcConfig
*config
,
1315 config
= FcConfigGetCurrent ();
1320 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1321 if (!st
&& config
->maxObjects
)
1323 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1325 if (FcDebug () & FC_DBG_EDIT
)
1327 printf ("FcConfigSubstitute ");
1330 if (kind
== FcMatchPattern
)
1331 s
= config
->substPattern
;
1333 s
= config
->substFont
;
1334 for (; s
; s
= s
->next
)
1337 * Check the tests to see if
1338 * they all match the pattern
1340 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1342 if (FcDebug () & FC_DBG_EDIT
)
1344 printf ("FcConfigSubstitute test ");
1348 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1353 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1357 * If there's no such field in the font,
1358 * then FcQualAll matches while FcQualAny does not
1362 if (t
->qual
== FcQualAll
)
1371 * Check to see if there is a match, mark the location
1372 * to apply match-relative edits
1374 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1377 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1379 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1384 if (FcDebug () & FC_DBG_EDIT
)
1385 printf ("No match\n");
1388 if (FcDebug () & FC_DBG_EDIT
)
1390 printf ("Substitute ");
1393 for (e
= s
->edit
; e
; e
= e
->next
)
1396 * Evaluate the list of expressions
1398 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1400 * Locate any test associated with this field, skipping
1401 * tests associated with the pattern when substituting in
1404 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1406 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1407 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1408 (FcChar8
*) e
->field
))
1411 * KLUDGE - the pattern may have been reallocated or
1412 * things may have been inserted or deleted above
1413 * this element by other edits. Go back and find
1416 if (e
!= s
->edit
&& st
[i
].elt
)
1417 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1426 * If there was a test, then replace the matched
1427 * value with the new list of values
1431 FcValueList
*thisValue
= st
[i
].value
;
1432 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1435 * Append the new list of values after the current value
1437 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1439 * Delete the marked value
1442 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1444 * Adjust any pointers into the value list to ensure
1445 * future edits occur at the same place
1447 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1449 if (st
[i
].value
== thisValue
)
1450 st
[i
].value
= nextValue
;
1454 /* fall through ... */
1455 case FcOpAssignReplace
:
1457 * Delete all of the values and insert
1460 FcConfigPatternDel (p
, e
->field
);
1461 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1463 * Adjust any pointers into the value list as they no
1464 * longer point to anything valid
1468 FcPatternElt
*thisElt
= st
[i
].elt
;
1469 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1471 if (st
[i
].elt
== thisElt
)
1479 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1482 /* fall through ... */
1483 case FcOpPrependFirst
:
1484 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1489 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1492 /* fall through ... */
1493 case FcOpAppendLast
:
1494 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1497 FcValueListDestroy (FcValueListPtrCreateDynamic(l
));
1502 * Now go through the pattern and eliminate
1503 * any properties without data
1505 for (e
= s
->edit
; e
; e
= e
->next
)
1506 FcConfigPatternCanon (p
, e
->field
);
1508 if (FcDebug () & FC_DBG_EDIT
)
1510 printf ("FcConfigSubstitute edit");
1514 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1516 if (FcDebug () & FC_DBG_EDIT
)
1518 printf ("FcConfigSubstitute done");
1525 FcConfigSubstitute (FcConfig
*config
,
1529 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1532 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1534 static FcChar8 fontconfig_path
[1000] = "";
1537 DllMain (HINSTANCE hinstDLL
,
1543 switch (fdwReason
) {
1544 case DLL_PROCESS_ATTACH
:
1545 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1546 sizeof (fontconfig_path
)))
1549 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1550 * assume it's a Unix-style installation tree, and use
1551 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1552 * folder where the DLL is as FONTCONFIG_PATH.
1554 p
= strrchr (fontconfig_path
, '\\');
1558 p
= strrchr (fontconfig_path
, '\\');
1559 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1560 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1562 strcat (fontconfig_path
, "\\etc\\fonts");
1565 fontconfig_path
[0] = '\0';
1573 #undef FONTCONFIG_PATH
1574 #define FONTCONFIG_PATH fontconfig_path
1576 #else /* !(_WIN32 && PIC) */
1578 #endif /* !(_WIN32 && PIC) */
1580 #ifndef FONTCONFIG_FILE
1581 #define FONTCONFIG_FILE "fonts.conf"
1585 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1590 dir
= (FcChar8
*) "";
1591 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1595 strcpy ((char *) path
, (const char *) dir
);
1596 /* make sure there's a single separator */
1598 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1599 path
[strlen((char *) path
)-1] != '\\')) &&
1602 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1603 strcat ((char *) path
, "\\");
1605 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1606 strcat ((char *) path
, "/");
1608 strcat ((char *) path
, (char *) file
);
1610 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1611 if (access ((char *) path
, R_OK
) == 0)
1619 FcConfigGetPath (void)
1622 FcChar8
*env
, *e
, *colon
;
1627 npath
= 2; /* default dir + null */
1628 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1634 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1637 path
= calloc (npath
, sizeof (FcChar8
*));
1647 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1649 colon
= e
+ strlen ((char *) e
);
1650 path
[i
] = malloc (colon
- e
+ 1);
1653 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1654 path
[i
][colon
- e
] = '\0';
1663 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1664 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1667 strcpy ((char *) path
[i
], (const char *) dir
);
1671 for (i
= 0; path
[i
]; i
++)
1679 FcConfigFreePath (FcChar8
**path
)
1683 for (p
= path
; *p
; p
++)
1688 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1693 if (_FcConfigHomeEnabled
)
1695 char *home
= getenv ("HOME");
1699 home
= getenv ("USERPROFILE");
1702 return (FcChar8
*) home
;
1708 FcConfigEnableHome (FcBool enable
)
1710 FcBool prev
= _FcConfigHomeEnabled
;
1711 _FcConfigHomeEnabled
= enable
;
1716 FcConfigFilename (const FcChar8
*url
)
1718 FcChar8
*file
, *dir
, **path
, **p
;
1722 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1724 url
= (FcChar8
*) FONTCONFIG_FILE
;
1729 if (isalpha (*url
) &&
1731 (url
[2] == '/' || url
[2] == '\\'))
1737 dir
= FcConfigHome ();
1739 file
= FcConfigFileExists (dir
, url
+ 1);
1748 file
= FcConfigFileExists (0, url
);
1751 path
= FcConfigGetPath ();
1754 for (p
= path
; *p
; p
++)
1756 file
= FcConfigFileExists (*p
, url
);
1760 FcConfigFreePath (path
);
1767 * Manage the application-specific fonts
1771 FcConfigAppFontAddFile (FcConfig
*config
,
1772 const FcChar8
*file
)
1781 config
= FcConfigGetCurrent ();
1786 subdirs
= FcStrSetCreate ();
1790 set
= FcConfigGetFonts (config
, FcSetApplication
);
1793 set
= FcFontSetCreate ();
1796 FcStrSetDestroy (subdirs
);
1799 FcConfigSetFonts (config
, set
, FcSetApplication
);
1802 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1804 FcStrSetDestroy (subdirs
);
1807 if ((sublist
= FcStrListCreate (subdirs
)))
1809 while ((subdir
= FcStrListNext (sublist
)))
1811 FcConfigAppFontAddDir (config
, subdir
);
1813 FcStrListDone (sublist
);
1815 FcStrSetDestroy (subdirs
);
1820 FcConfigAppFontAddDir (FcConfig
*config
,
1830 config
= FcConfigGetCurrent ();
1834 subdirs
= FcStrSetCreate ();
1838 set
= FcConfigGetFonts (config
, FcSetApplication
);
1841 set
= FcFontSetCreate ();
1844 FcStrSetDestroy (subdirs
);
1847 FcConfigSetFonts (config
, set
, FcSetApplication
);
1850 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1852 FcStrSetDestroy (subdirs
);
1855 if ((sublist
= FcStrListCreate (subdirs
)))
1857 while ((subdir
= FcStrListNext (sublist
)))
1859 FcConfigAppFontAddDir (config
, subdir
);
1861 FcStrListDone (sublist
);
1863 FcStrSetDestroy (subdirs
);
1868 FcConfigAppFontClear (FcConfig
*config
)
1872 config
= FcConfigGetCurrent ();
1877 FcConfigSetFonts (config
, 0, FcSetApplication
);
1881 * Manage filename-based font source selectors
1885 FcConfigGlobAdd (FcConfig
*config
,
1886 const FcChar8
*glob
,
1889 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1891 return FcStrSetAdd (set
, glob
);
1895 FcConfigGlobMatch (const FcChar8
*glob
,
1896 const FcChar8
*string
)
1900 while ((c
= *glob
++))
1904 /* short circuit common case */
1907 /* short circuit another common case */
1908 if (strchr ((char *) glob
, '*') == 0)
1909 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1912 if (FcConfigGlobMatch (glob
, string
))
1918 if (*string
++ == '\0')
1927 return *string
== '\0';
1931 FcConfigGlobsMatch (const FcStrSet
*globs
,
1932 const FcChar8
*string
)
1936 for (i
= 0; i
< globs
->num
; i
++)
1937 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1943 FcConfigAcceptFilename (FcConfig
*config
,
1944 const FcChar8
*filename
)
1946 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1948 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1954 * Manage font-pattern based font source selectors
1958 FcConfigPatternsAdd (FcConfig
*config
,
1962 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1964 return FcFontSetAdd (set
, pattern
);
1968 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1969 const FcPattern
*font
)
1973 for (i
= 0; i
< patterns
->nfont
; i
++)
1974 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1980 FcConfigAcceptFont (FcConfig
*config
,
1981 const FcPattern
*font
)
1983 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1985 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))