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
)
251 FcGlobalCache
*cache
;
255 fonts
= FcFontSetCreate ();
259 cache
= FcGlobalCacheCreate ();
264 FcGlobalCacheLoad (cache
, config
->cache
);
266 list
= FcConfigGetFontDirs (config
);
270 while ((dir
= FcStrListNext (list
)))
272 if (FcDebug () & FC_DBG_FONTSET
)
273 printf ("scan dir %s\n", dir
);
274 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
275 config
->blanks
, dir
, FcFalse
, config
);
278 FcStrListDone (list
);
280 if (FcDebug () & FC_DBG_FONTSET
)
281 FcFontSetPrint (fonts
);
284 FcGlobalCacheSave (cache
, config
->cache
);
285 FcGlobalCacheDestroy (cache
);
287 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
291 FcFontSetDestroy (fonts
);
297 FcConfigSetCurrent (FcConfig
*config
)
300 if (!FcConfigBuildFonts (config
))
304 FcConfigDestroy (_fcConfig
);
310 FcConfigGetCurrent (void)
319 FcConfigAddConfigDir (FcConfig
*config
,
322 return FcStrSetAddFilename (config
->configDirs
, d
);
326 FcConfigGetConfigDirs (FcConfig
*config
)
330 config
= FcConfigGetCurrent ();
334 return FcStrListCreate (config
->configDirs
);
338 FcConfigAddFontDir (FcConfig
*config
,
341 return FcStrSetAddFilename (config
->fontDirs
, d
);
345 FcConfigAddDir (FcConfig
*config
,
348 return (FcConfigAddConfigDir (config
, d
) &&
349 FcConfigAddFontDir (config
, d
));
353 FcConfigGetFontDirs (FcConfig
*config
)
357 config
= FcConfigGetCurrent ();
361 return FcStrListCreate (config
->fontDirs
);
365 FcConfigAddConfigFile (FcConfig
*config
,
369 FcChar8
*file
= FcConfigFilename (f
);
374 ret
= FcStrSetAdd (config
->configFiles
, file
);
380 FcConfigGetConfigFiles (FcConfig
*config
)
384 config
= FcConfigGetCurrent ();
388 return FcStrListCreate (config
->configFiles
);
392 FcConfigSetCache (FcConfig
*config
,
395 FcChar8
*new = FcStrCopyFilename (c
);
400 FcStrFree (config
->cache
);
406 FcConfigGetCache (FcConfig
*config
)
410 config
= FcConfigGetCurrent ();
414 return config
->cache
;
418 FcConfigGetFonts (FcConfig
*config
,
423 config
= FcConfigGetCurrent ();
427 return config
->fonts
[set
];
431 FcConfigSetFonts (FcConfig
*config
,
435 if (config
->fonts
[set
])
436 FcFontSetDestroy (config
->fonts
[set
]);
437 config
->fonts
[set
] = fonts
;
443 FcConfigGetBlanks (FcConfig
*config
)
447 config
= FcConfigGetCurrent ();
451 return config
->blanks
;
455 FcConfigAddBlank (FcConfig
*config
,
463 b
= FcBlanksCreate ();
467 if (!FcBlanksAdd (b
, blank
))
474 FcConfigGetRescanInverval (FcConfig
*config
)
478 config
= FcConfigGetCurrent ();
482 return config
->rescanInterval
;
486 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
490 config
= FcConfigGetCurrent ();
494 config
->rescanInterval
= rescanInterval
;
499 FcConfigAddEdit (FcConfig
*config
,
504 FcSubst
*subst
, **prev
;
508 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
511 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
512 if (kind
== FcMatchPattern
)
513 prev
= &config
->substPattern
;
515 prev
= &config
->substFont
;
516 for (; *prev
; prev
= &(*prev
)->next
);
522 for (t
= test
; t
; t
= t
->next
)
524 if (t
->kind
== FcMatchDefault
)
528 if (config
->maxObjects
< num
)
529 config
->maxObjects
= num
;
530 if (FcDebug () & FC_DBG_EDIT
)
532 printf ("Add Subst ");
533 FcSubstPrint (subst
);
538 typedef struct _FcSubState
{
544 FcConfigPromote (FcValue v
, FcValue u
)
546 if (v
.type
== FcTypeInteger
)
548 v
.type
= FcTypeDouble
;
549 v
.u
.d
= (double) v
.u
.i
;
551 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
553 v
.u
.mi
= FcIdentityMatrix
;
554 v
.type
= FcTypeMatrix
;
556 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
558 v
.u
.li
= FcLangSetPtrCreateDynamic(FcLangSetPromote
559 (FcObjectPtrU(v
.u
.si
)));
560 v
.type
= FcTypeLangSet
;
566 FcConfigCompareValue (const FcValue left_o
,
568 const FcValue right_o
)
570 FcValue left
= left_o
;
571 FcValue right
= right_o
;
572 FcBool ret
= FcFalse
;
574 left
= FcConfigPromote (left
, right
);
575 right
= FcConfigPromote (right
, left
);
576 if (left
.type
== right
.type
)
580 break; /* FcConfigPromote prevents this from happening */
586 ret
= left
.u
.d
== right
.u
.d
;
589 case FcOpNotContains
:
590 ret
= left
.u
.d
!= right
.u
.d
;
593 ret
= left
.u
.d
< right
.u
.d
;
596 ret
= left
.u
.d
<= right
.u
.d
;
599 ret
= left
.u
.d
> right
.u
.d
;
602 ret
= left
.u
.d
>= right
.u
.d
;
613 ret
= left
.u
.b
== right
.u
.b
;
616 case FcOpNotContains
:
617 ret
= left
.u
.b
!= right
.u
.b
;
627 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
628 FcObjectPtrU(right
.u
.si
)) == 0;
631 ret
= FcStrStrIgnoreCase (FcObjectPtrU(left
.u
.si
),
632 FcObjectPtrU(right
.u
.si
)) != 0;
635 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
636 FcObjectPtrU(right
.u
.si
)) != 0;
638 case FcOpNotContains
:
639 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
640 FcObjectPtrU(right
.u
.si
)) == 0;
651 ret
= FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
654 case FcOpNotContains
:
655 ret
= !FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
665 /* left contains right if right is a subset of left */
666 ret
= FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
668 case FcOpNotContains
:
669 /* left contains right if right is a subset of left */
670 ret
= !FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
673 ret
= FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
676 ret
= !FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
686 ret
= FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
688 case FcOpNotContains
:
689 ret
= !FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
692 ret
= FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
695 ret
= !FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
717 ret
= left
.u
.f
== right
.u
.f
;
720 case FcOpNotContains
:
721 ret
= left
.u
.f
!= right
.u
.f
;
731 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
738 #define _FcDoubleFloor(d) ((int) (d))
739 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
740 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
741 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
742 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
743 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
746 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
754 v
.type
= FcTypeInteger
;
758 v
.type
= FcTypeDouble
;
762 v
.type
= FcTypeString
;
763 v
.u
.si
= FcObjectStaticName(e
->u
.sval
);
767 v
.type
= FcTypeMatrix
;
768 v
.u
.mi
= FcMatrixPtrCreateDynamic(e
->u
.mval
);
772 v
.type
= FcTypeCharSet
;
773 v
.u
.ci
= FcCharSetPtrCreateDynamic(e
->u
.cval
);
781 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
782 if (r
!= FcResultMatch
)
786 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
787 v
.type
= FcTypeInteger
;
792 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
793 if (vl
.type
== FcTypeBool
)
796 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
798 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
811 case FcOpNotContains
:
813 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
814 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
816 v
.u
.b
= FcConfigCompareValue (vl
, e
->op
, vr
);
826 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
827 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
828 vl
= FcConfigPromote (vl
, vr
);
829 vr
= FcConfigPromote (vr
, vl
);
830 if (vl
.type
== vr
.type
)
836 v
.type
= FcTypeDouble
;
837 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
840 v
.type
= FcTypeDouble
;
841 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
844 v
.type
= FcTypeDouble
;
845 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
848 v
.type
= FcTypeDouble
;
849 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
855 if (v
.type
== FcTypeDouble
&&
856 v
.u
.d
== (double) (int) v
.u
.d
)
858 v
.type
= FcTypeInteger
;
866 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
870 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
880 v
.type
= FcTypeString
;
881 v
.u
.si
= FcObjectStaticName
882 (FcStrPlus (FcObjectPtrU(vl
.u
.si
),
883 FcObjectPtrU(vr
.u
.si
)));
885 if (!FcObjectPtrU(v
.u
.si
))
896 v
.type
= FcTypeMatrix
;
897 m
= malloc (sizeof (FcMatrix
));
900 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
901 FcMatrixMultiply (m
, FcMatrixPtrU(vl
.u
.mi
),
902 FcMatrixPtrU(vr
.u
.mi
));
903 v
.u
.mi
= FcMatrixPtrCreateDynamic(m
);
926 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
939 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
945 v
.type
= FcTypeInteger
;
946 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
955 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
961 v
.type
= FcTypeInteger
;
962 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
971 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
977 v
.type
= FcTypeInteger
;
978 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
987 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
993 v
.type
= FcTypeInteger
;
994 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1000 FcValueDestroy (vl
);
1003 v
.type
= FcTypeVoid
;
1009 static FcValueList
*
1010 FcConfigMatchValueList (FcPattern
*p
,
1012 FcValueList
*values
)
1014 FcValueList
*ret
= 0;
1015 FcExpr
*e
= t
->expr
;
1021 /* Compute the value of the match expression */
1022 if (e
->op
== FcOpComma
)
1024 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1025 e
= e
->u
.tree
.right
;
1029 value
= FcConfigEvaluate (p
, e
);
1033 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1035 /* Compare the pattern value to the match expression value */
1036 if (FcConfigCompareValue (v
->value
, t
->op
, value
))
1043 if (t
->qual
== FcQualAll
)
1050 FcValueDestroy (value
);
1055 static FcValueList
*
1056 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1063 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1066 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1067 if (e
->op
== FcOpComma
)
1069 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1070 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1074 l
->value
= FcConfigEvaluate (p
, e
);
1075 l
->next
= FcValueListPtrCreateDynamic(0);
1077 l
->binding
= binding
;
1078 lp
= FcValueListPtrCreateDynamic(l
);
1079 while (FcValueListPtrU(lp
) && FcValueListPtrU(lp
)->value
.type
== FcTypeVoid
)
1081 FcValueListPtr next
= FcValueListPtrU(lp
)->next
;
1083 if (lp
.storage
== FcStorageDynamic
)
1085 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1094 FcConfigAdd (FcValueListPtr
*head
,
1095 FcValueList
*position
,
1099 FcValueListPtr
*prev
, last
, v
;
1100 FcValueBinding sameBinding
;
1103 sameBinding
= position
->binding
;
1105 sameBinding
= FcValueBindingWeak
;
1106 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1107 v
= FcValueListPtrU(v
)->next
)
1108 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1109 FcValueListPtrU(v
)->binding
= sameBinding
;
1113 prev
= &position
->next
;
1115 for (prev
= head
; FcValueListPtrU(*prev
);
1116 prev
= &(FcValueListPtrU(*prev
)->next
))
1123 for (prev
= head
; FcValueListPtrU(*prev
);
1124 prev
= &(FcValueListPtrU(*prev
)->next
))
1126 if (FcValueListPtrU(*prev
) == position
)
1133 if (FcDebug () & FC_DBG_EDIT
)
1135 if (!FcValueListPtrU(*prev
))
1136 printf ("position not on list\n");
1140 if (FcDebug () & FC_DBG_EDIT
)
1142 printf ("%s list before ", append
? "Append" : "Prepend");
1143 FcValueListPrint (*head
);
1149 last
= FcValueListPtrCreateDynamic(new);
1150 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1151 last
= FcValueListPtrU(last
)->next
;
1153 FcValueListPtrU(last
)->next
= *prev
;
1154 *prev
= FcValueListPtrCreateDynamic(new);
1157 if (FcDebug () & FC_DBG_EDIT
)
1159 printf ("%s list after ", append
? "Append" : "Prepend");
1160 FcValueListPrint (*head
);
1168 FcConfigDel (FcValueListPtr
*head
,
1169 FcValueList
*position
)
1171 FcValueListPtr
*prev
;
1173 for (prev
= head
; FcValueListPtrU(*prev
);
1174 prev
= &(FcValueListPtrU(*prev
)->next
))
1176 if (FcValueListPtrU(*prev
) == position
)
1178 *prev
= position
->next
;
1179 position
->next
= FcValueListPtrCreateDynamic(0);
1180 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1187 FcConfigPatternAdd (FcPattern
*p
,
1194 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1198 FcConfigAdd (&e
->values
, 0, append
, list
);
1203 * Delete all values associated with a field
1206 FcConfigPatternDel (FcPattern
*p
,
1209 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1212 while (FcValueListPtrU(e
->values
))
1213 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1217 FcConfigPatternCanon (FcPattern
*p
,
1220 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1223 if (!FcValueListPtrU(e
->values
))
1224 FcPatternDel (p
, object
);
1228 FcConfigSubstituteWithPat (FcConfig
*config
,
1243 config
= FcConfigGetCurrent ();
1248 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1249 if (!st
&& config
->maxObjects
)
1251 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1253 if (FcDebug () & FC_DBG_EDIT
)
1255 printf ("FcConfigSubstitute ");
1258 if (kind
== FcMatchPattern
)
1259 s
= config
->substPattern
;
1261 s
= config
->substFont
;
1262 for (; s
; s
= s
->next
)
1265 * Check the tests to see if
1266 * they all match the pattern
1268 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1270 if (FcDebug () & FC_DBG_EDIT
)
1272 printf ("FcConfigSubstitute test ");
1276 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1281 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1285 * If there's no such field in the font,
1286 * then FcQualAll matches while FcQualAny does not
1290 if (t
->qual
== FcQualAll
)
1299 * Check to see if there is a match, mark the location
1300 * to apply match-relative edits
1302 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1305 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1307 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1312 if (FcDebug () & FC_DBG_EDIT
)
1313 printf ("No match\n");
1316 if (FcDebug () & FC_DBG_EDIT
)
1318 printf ("Substitute ");
1321 for (e
= s
->edit
; e
; e
= e
->next
)
1324 * Evaluate the list of expressions
1326 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1328 * Locate any test associated with this field, skipping
1329 * tests associated with the pattern when substituting in
1332 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1334 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1335 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1336 (FcChar8
*) e
->field
))
1339 * KLUDGE - the pattern may have been reallocated or
1340 * things may have been inserted or deleted above
1341 * this element by other edits. Go back and find
1344 if (e
!= s
->edit
&& st
[i
].elt
)
1345 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1354 * If there was a test, then replace the matched
1355 * value with the new list of values
1359 FcValueList
*thisValue
= st
[i
].value
;
1360 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1363 * Append the new list of values after the current value
1365 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1367 * Delete the marked value
1369 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1371 * Adjust any pointers into the value list to ensure
1372 * future edits occur at the same place
1374 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1376 if (st
[i
].value
== thisValue
)
1377 st
[i
].value
= nextValue
;
1381 /* fall through ... */
1382 case FcOpAssignReplace
:
1384 * Delete all of the values and insert
1387 FcConfigPatternDel (p
, e
->field
);
1388 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1390 * Adjust any pointers into the value list as they no
1391 * longer point to anything valid
1395 FcPatternElt
*thisElt
= st
[i
].elt
;
1396 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1398 if (st
[i
].elt
== thisElt
)
1406 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1409 /* fall through ... */
1410 case FcOpPrependFirst
:
1411 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1416 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1419 /* fall through ... */
1420 case FcOpAppendLast
:
1421 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1428 * Now go through the pattern and eliminate
1429 * any properties without data
1431 for (e
= s
->edit
; e
; e
= e
->next
)
1432 FcConfigPatternCanon (p
, e
->field
);
1434 if (FcDebug () & FC_DBG_EDIT
)
1436 printf ("FcConfigSubstitute edit");
1440 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1442 if (FcDebug () & FC_DBG_EDIT
)
1444 printf ("FcConfigSubstitute done");
1451 FcConfigSubstitute (FcConfig
*config
,
1455 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1458 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1460 static FcChar8 fontconfig_path
[1000] = "";
1463 DllMain (HINSTANCE hinstDLL
,
1469 switch (fdwReason
) {
1470 case DLL_PROCESS_ATTACH
:
1471 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1472 sizeof (fontconfig_path
)))
1475 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1476 * assume it's a Unix-style installation tree, and use
1477 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1478 * folder where the DLL is as FONTCONFIG_PATH.
1480 p
= strrchr (fontconfig_path
, '\\');
1484 p
= strrchr (fontconfig_path
, '\\');
1485 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1486 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1488 strcat (fontconfig_path
, "\\etc\\fonts");
1491 fontconfig_path
[0] = '\0';
1499 #undef FONTCONFIG_PATH
1500 #define FONTCONFIG_PATH fontconfig_path
1502 #else /* !(_WIN32 && PIC) */
1504 #endif /* !(_WIN32 && PIC) */
1506 #ifndef FONTCONFIG_FILE
1507 #define FONTCONFIG_FILE "fonts.conf"
1511 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1516 dir
= (FcChar8
*) "";
1517 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1521 strcpy ((char *) path
, (const char *) dir
);
1522 /* make sure there's a single separator */
1524 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1525 path
[strlen((char *) path
)-1] != '\\')) &&
1528 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1529 strcat ((char *) path
, "\\");
1531 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1532 strcat ((char *) path
, "/");
1534 strcat ((char *) path
, (char *) file
);
1536 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1537 if (access ((char *) path
, R_OK
) == 0)
1545 FcConfigGetPath (void)
1548 FcChar8
*env
, *e
, *colon
;
1553 npath
= 2; /* default dir + null */
1554 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1560 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1563 path
= calloc (npath
, sizeof (FcChar8
*));
1573 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1575 colon
= e
+ strlen ((char *) e
);
1576 path
[i
] = malloc (colon
- e
+ 1);
1579 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1580 path
[i
][colon
- e
] = '\0';
1589 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1590 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1593 strcpy ((char *) path
[i
], (const char *) dir
);
1597 for (i
= 0; path
[i
]; i
++)
1605 FcConfigFreePath (FcChar8
**path
)
1609 for (p
= path
; *p
; p
++)
1614 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1619 if (_FcConfigHomeEnabled
)
1621 char *home
= getenv ("HOME");
1625 home
= getenv ("USERPROFILE");
1634 FcConfigEnableHome (FcBool enable
)
1636 FcBool prev
= _FcConfigHomeEnabled
;
1637 _FcConfigHomeEnabled
= enable
;
1642 FcConfigFilename (const FcChar8
*url
)
1644 FcChar8
*file
, *dir
, **path
, **p
;
1648 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1650 url
= (FcChar8
*) FONTCONFIG_FILE
;
1655 if (isalpha (*url
) &&
1657 (url
[2] == '/' || url
[2] == '\\'))
1663 dir
= FcConfigHome ();
1665 file
= FcConfigFileExists (dir
, url
+ 1);
1674 file
= FcConfigFileExists (0, url
);
1677 path
= FcConfigGetPath ();
1680 for (p
= path
; *p
; p
++)
1682 file
= FcConfigFileExists (*p
, url
);
1686 FcConfigFreePath (path
);
1693 * Manage the application-specific fonts
1697 FcConfigAppFontAddFile (FcConfig
*config
,
1698 const FcChar8
*file
)
1707 config
= FcConfigGetCurrent ();
1712 subdirs
= FcStrSetCreate ();
1716 set
= FcConfigGetFonts (config
, FcSetApplication
);
1719 set
= FcFontSetCreate ();
1722 FcStrSetDestroy (subdirs
);
1725 FcConfigSetFonts (config
, set
, FcSetApplication
);
1728 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1730 FcStrSetDestroy (subdirs
);
1733 if ((sublist
= FcStrListCreate (subdirs
)))
1735 while ((subdir
= FcStrListNext (sublist
)))
1737 FcConfigAppFontAddDir (config
, subdir
);
1739 FcStrListDone (sublist
);
1745 FcConfigAppFontAddDir (FcConfig
*config
,
1755 config
= FcConfigGetCurrent ();
1759 subdirs
= FcStrSetCreate ();
1763 set
= FcConfigGetFonts (config
, FcSetApplication
);
1766 set
= FcFontSetCreate ();
1769 FcStrSetDestroy (subdirs
);
1772 FcConfigSetFonts (config
, set
, FcSetApplication
);
1775 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1777 FcStrSetDestroy (subdirs
);
1780 if ((sublist
= FcStrListCreate (subdirs
)))
1782 while ((subdir
= FcStrListNext (sublist
)))
1784 FcConfigAppFontAddDir (config
, subdir
);
1786 FcStrListDone (sublist
);
1792 FcConfigAppFontClear (FcConfig
*config
)
1796 config
= FcConfigGetCurrent ();
1801 FcConfigSetFonts (config
, 0, FcSetApplication
);
1805 * Manage filename-based font source selectors
1809 FcConfigGlobAdd (FcConfig
*config
,
1810 const FcChar8
*glob
,
1813 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1815 return FcStrSetAdd (set
, glob
);
1819 FcConfigGlobMatch (const FcChar8
*glob
,
1820 const FcChar8
*string
)
1824 while ((c
= *glob
++))
1828 /* short circuit common case */
1831 /* short circuit another common case */
1832 if (strchr ((char *) glob
, '*') == 0)
1833 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1836 if (FcConfigGlobMatch (glob
, string
))
1842 if (*string
++ == '\0')
1851 return *string
== '\0';
1855 FcConfigGlobsMatch (const FcStrSet
*globs
,
1856 const FcChar8
*string
)
1860 for (i
= 0; i
< globs
->num
; i
++)
1861 if (FcConfigGlobMatch (FcStrSetGet(globs
, i
), string
))
1867 FcConfigAcceptFilename (FcConfig
*config
,
1868 const FcChar8
*filename
)
1870 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1872 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1878 * Manage font-pattern based font source selectors
1882 FcConfigPatternsAdd (FcConfig
*config
,
1886 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1888 return FcFontSetAdd (set
, pattern
);
1892 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1893 const FcPattern
*font
)
1897 for (i
= 0; i
< patterns
->nfont
; i
++)
1898 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1904 FcConfigAcceptFont (FcConfig
*config
,
1905 const FcPattern
*font
)
1907 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1909 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))