2 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
27 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
33 #if defined (_WIN32) && !defined (R_OK)
45 config
= malloc (sizeof (FcConfig
));
48 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
50 config
->configDirs
= FcStrSetCreate ();
51 if (!config
->configDirs
)
54 config
->configFiles
= FcStrSetCreate ();
55 if (!config
->configFiles
)
58 config
->fontDirs
= FcStrSetCreate ();
59 if (!config
->fontDirs
)
62 config
->acceptGlobs
= FcStrSetCreate ();
63 if (!config
->acceptGlobs
)
66 config
->rejectGlobs
= FcStrSetCreate ();
67 if (!config
->rejectGlobs
)
70 config
->acceptPatterns
= FcFontSetCreate ();
71 if (!config
->acceptPatterns
)
74 config
->rejectPatterns
= FcFontSetCreate ();
75 if (!config
->rejectPatterns
)
80 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
84 if (config
->cache
== 0)
86 /* If no home, use the temp folder. */
88 int templen
= GetTempPath (1, dummy
);
89 FcChar8
*temp
= malloc (templen
+ 1);
95 GetTempPath (templen
+ 1, temp
);
96 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
98 if (!FcConfigSetCache (config
, cache_dir
))
100 FcStrFree (cache_dir
);
103 FcStrFree (cache_dir
);
110 config
->substPattern
= 0;
111 config
->substFont
= 0;
112 config
->maxObjects
= 0;
113 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
114 config
->fonts
[set
] = 0;
116 config
->rescanTime
= time(0);
117 config
->rescanInterval
= 30;
122 FcFontSetDestroy (config
->rejectPatterns
);
124 FcFontSetDestroy (config
->acceptPatterns
);
126 FcStrSetDestroy (config
->rejectGlobs
);
128 FcStrSetDestroy (config
->acceptGlobs
);
130 FcStrSetDestroy (config
->fontDirs
);
132 FcStrSetDestroy (config
->configFiles
);
134 FcStrSetDestroy (config
->configDirs
);
137 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
142 typedef struct _FcFileTime
{
148 FcConfigNewestFile (FcStrSet
*files
)
150 FcStrList
*list
= FcStrListCreate (files
);
151 FcFileTime newest
= { 0, FcFalse
};
157 while ((file
= FcStrListNext (list
)))
158 if (stat ((char *) file
, &statb
) == 0)
159 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
162 newest
.time
= statb
.st_mtime
;
164 FcStrListDone (list
);
170 FcConfigUptoDate (FcConfig
*config
)
172 FcFileTime config_time
, font_time
;
173 time_t now
= time(0);
176 config
= FcConfigGetCurrent ();
180 config_time
= FcConfigNewestFile (config
->configFiles
);
181 font_time
= FcConfigNewestFile (config
->fontDirs
);
182 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
183 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
187 config
->rescanTime
= now
;
192 FcSubstDestroy (FcSubst
*s
)
200 FcTestDestroy (s
->test
);
202 FcEditDestroy (s
->edit
);
204 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
210 FcConfigDestroy (FcConfig
*config
)
214 if (config
== _fcConfig
)
217 FcStrSetDestroy (config
->configDirs
);
218 FcStrSetDestroy (config
->fontDirs
);
219 FcStrSetDestroy (config
->configFiles
);
220 FcStrSetDestroy (config
->acceptGlobs
);
221 FcStrSetDestroy (config
->rejectGlobs
);
222 FcFontSetDestroy (config
->acceptPatterns
);
223 FcFontSetDestroy (config
->rejectPatterns
);
226 FcBlanksDestroy (config
->blanks
);
229 FcStrFree (config
->cache
);
231 FcSubstDestroy (config
->substPattern
);
232 FcSubstDestroy (config
->substFont
);
233 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
234 if (config
->fonts
[set
])
235 FcFontSetDestroy (config
->fonts
[set
]);
238 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
242 * Scan the current list of directories in the configuration
243 * and build the set of available fonts. Update the
244 * per-user cache file to reflect the new configuration
248 FcConfigBuildFonts (FcConfig
*config
)
250 FcFontSet
*fonts
, *cached_fonts
;
251 FcGlobalCache
*cache
;
256 fonts
= FcFontSetCreate ();
260 cache
= FcGlobalCacheCreate ();
264 oldDirs
= FcStrSetCreate ();
269 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
);
271 cached_fonts
= FcCacheRead(config
, cache
);
274 list
= FcConfigGetFontDirs (config
);
278 while ((dir
= FcStrListNext (list
)))
280 if (FcDebug () & FC_DBG_FONTSET
)
281 printf ("build scan dir %s\n", dir
);
282 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
283 config
->blanks
, dir
, FcFalse
, config
);
286 FcStrListDone (list
);
292 for (i
= 0; i
< oldDirs
->num
; i
++)
294 if (FcDebug () & FC_DBG_FONTSET
)
295 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
296 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
297 config
->blanks
, oldDirs
->strs
[i
],
301 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
303 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]))
304 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
306 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
308 cached_fonts
->nfont
= 0;
309 FcFontSetDestroy (cached_fonts
);
312 if (FcDebug () & FC_DBG_FONTSET
)
313 FcFontSetPrint (fonts
);
316 FcGlobalCacheSave (cache
, config
->cache
);
317 FcGlobalCacheDestroy (cache
);
318 FcStrSetDestroy (oldDirs
);
320 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
324 FcStrSetDestroy (oldDirs
);
326 FcFontSetDestroy (fonts
);
332 FcConfigSetCurrent (FcConfig
*config
)
335 if (!FcConfigBuildFonts (config
))
339 FcConfigDestroy (_fcConfig
);
345 FcConfigGetCurrent (void)
354 FcConfigAddConfigDir (FcConfig
*config
,
357 return FcStrSetAddFilename (config
->configDirs
, d
);
361 FcConfigGetConfigDirs (FcConfig
*config
)
365 config
= FcConfigGetCurrent ();
369 return FcStrListCreate (config
->configDirs
);
373 FcConfigAddFontDir (FcConfig
*config
,
376 return FcStrSetAddFilename (config
->fontDirs
, d
);
380 FcConfigAddDir (FcConfig
*config
,
383 return (FcConfigAddConfigDir (config
, d
) &&
384 FcConfigAddFontDir (config
, d
));
388 FcConfigGetFontDirs (FcConfig
*config
)
392 config
= FcConfigGetCurrent ();
396 return FcStrListCreate (config
->fontDirs
);
400 FcConfigAddConfigFile (FcConfig
*config
,
404 FcChar8
*file
= FcConfigFilename (f
);
409 ret
= FcStrSetAdd (config
->configFiles
, file
);
415 FcConfigGetConfigFiles (FcConfig
*config
)
419 config
= FcConfigGetCurrent ();
423 return FcStrListCreate (config
->configFiles
);
427 FcConfigSetCache (FcConfig
*config
,
430 FcChar8
*new = FcStrCopyFilename (c
);
435 FcStrFree (config
->cache
);
441 FcConfigGetCache (FcConfig
*config
)
445 config
= FcConfigGetCurrent ();
449 return config
->cache
;
453 FcConfigGetFonts (FcConfig
*config
,
458 config
= FcConfigGetCurrent ();
462 return config
->fonts
[set
];
466 FcConfigSetFonts (FcConfig
*config
,
470 if (config
->fonts
[set
])
471 FcFontSetDestroy (config
->fonts
[set
]);
472 config
->fonts
[set
] = fonts
;
478 FcConfigGetBlanks (FcConfig
*config
)
482 config
= FcConfigGetCurrent ();
486 return config
->blanks
;
490 FcConfigAddBlank (FcConfig
*config
,
498 b
= FcBlanksCreate ();
502 if (!FcBlanksAdd (b
, blank
))
509 FcConfigGetRescanInverval (FcConfig
*config
)
513 config
= FcConfigGetCurrent ();
517 return config
->rescanInterval
;
521 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
525 config
= FcConfigGetCurrent ();
529 config
->rescanInterval
= rescanInterval
;
534 FcConfigAddEdit (FcConfig
*config
,
539 FcSubst
*subst
, **prev
;
543 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
546 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
547 if (kind
== FcMatchPattern
)
548 prev
= &config
->substPattern
;
550 prev
= &config
->substFont
;
551 for (; *prev
; prev
= &(*prev
)->next
);
557 for (t
= test
; t
; t
= t
->next
)
559 if (t
->kind
== FcMatchDefault
)
563 if (config
->maxObjects
< num
)
564 config
->maxObjects
= num
;
565 if (FcDebug () & FC_DBG_EDIT
)
567 printf ("Add Subst ");
568 FcSubstPrint (subst
);
573 typedef struct _FcSubState
{
579 FcConfigPromote (FcValue v
, FcValue u
)
581 if (v
.type
== FcTypeInteger
)
583 v
.type
= FcTypeDouble
;
584 v
.u
.d
= (double) v
.u
.i
;
586 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
588 v
.u
.m
= &FcIdentityMatrix
;
589 v
.type
= FcTypeMatrix
;
591 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
593 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
594 v
.type
= FcTypeLangSet
;
600 FcConfigCompareValue (const FcValue
*left_o
,
602 const FcValue
*right_o
)
604 FcValue left
= FcValueCanonicalize(left_o
);
605 FcValue right
= FcValueCanonicalize(right_o
);
606 FcBool ret
= FcFalse
;
608 left
= FcConfigPromote (left
, right
);
609 right
= FcConfigPromote (right
, left
);
610 if (left
.type
== right
.type
)
614 break; /* FcConfigPromote prevents this from happening */
620 ret
= left
.u
.d
== right
.u
.d
;
623 case FcOpNotContains
:
624 ret
= left
.u
.d
!= right
.u
.d
;
627 ret
= left
.u
.d
< right
.u
.d
;
630 ret
= left
.u
.d
<= right
.u
.d
;
633 ret
= left
.u
.d
> right
.u
.d
;
636 ret
= left
.u
.d
>= right
.u
.d
;
647 ret
= left
.u
.b
== right
.u
.b
;
650 case FcOpNotContains
:
651 ret
= left
.u
.b
!= right
.u
.b
;
661 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
664 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
667 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
669 case FcOpNotContains
:
670 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
681 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
684 case FcOpNotContains
:
685 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
695 /* left contains right if right is a subset of left */
696 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
698 case FcOpNotContains
:
699 /* left contains right if right is a subset of left */
700 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
703 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
706 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
716 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
718 case FcOpNotContains
:
719 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
722 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
725 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
747 ret
= left
.u
.f
== right
.u
.f
;
750 case FcOpNotContains
:
751 ret
= left
.u
.f
!= right
.u
.f
;
761 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
768 #define _FcDoubleFloor(d) ((int) (d))
769 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
770 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
771 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
772 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
773 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
776 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
784 v
.type
= FcTypeInteger
;
788 v
.type
= FcTypeDouble
;
792 v
.type
= FcTypeString
;
793 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
797 v
.type
= FcTypeMatrix
;
802 v
.type
= FcTypeCharSet
;
811 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
812 if (r
!= FcResultMatch
)
816 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
817 v
.type
= FcTypeInteger
;
822 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
823 if (vl
.type
== FcTypeBool
)
826 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
828 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
841 case FcOpNotContains
:
843 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
844 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
846 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
856 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
857 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
858 vl
= FcConfigPromote (vl
, vr
);
859 vr
= FcConfigPromote (vr
, vl
);
860 if (vl
.type
== vr
.type
)
866 v
.type
= FcTypeDouble
;
867 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
870 v
.type
= FcTypeDouble
;
871 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
874 v
.type
= FcTypeDouble
;
875 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
878 v
.type
= FcTypeDouble
;
879 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
885 if (v
.type
== FcTypeDouble
&&
886 v
.u
.d
== (double) (int) v
.u
.d
)
888 v
.type
= FcTypeInteger
;
896 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
900 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
910 v
.type
= FcTypeString
;
911 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
924 v
.type
= FcTypeMatrix
;
925 m
= malloc (sizeof (FcMatrix
));
928 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
929 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
953 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
966 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
972 v
.type
= FcTypeInteger
;
973 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
982 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
988 v
.type
= FcTypeInteger
;
989 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
998 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1004 v
.type
= FcTypeInteger
;
1005 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1008 v
.type
= FcTypeVoid
;
1011 FcValueDestroy (vl
);
1014 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1020 v
.type
= FcTypeInteger
;
1021 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1024 v
.type
= FcTypeVoid
;
1027 FcValueDestroy (vl
);
1030 v
.type
= FcTypeVoid
;
1036 static FcValueList
*
1037 FcConfigMatchValueList (FcPattern
*p
,
1039 FcValueList
*values
)
1041 FcValueList
*ret
= 0;
1042 FcExpr
*e
= t
->expr
;
1048 /* Compute the value of the match expression */
1049 if (e
->op
== FcOpComma
)
1051 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1052 e
= e
->u
.tree
.right
;
1056 value
= FcConfigEvaluate (p
, e
);
1060 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1062 /* Compare the pattern value to the match expression value */
1063 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1070 if (t
->qual
== FcQualAll
)
1077 FcValueDestroy (value
);
1082 static FcValueList
*
1083 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1090 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1093 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1094 if (e
->op
== FcOpComma
)
1096 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1097 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1101 l
->value
= FcConfigEvaluate (p
, e
);
1102 l
->next
= FcValueListPtrCreateDynamic(0);
1104 l
->binding
= binding
;
1105 lp
= FcValueListPtrCreateDynamic(l
);
1106 while (FcValueListPtrU(lp
) && FcValueListPtrU(lp
)->value
.type
== FcTypeVoid
)
1108 FcValueListPtr next
= FcValueListPtrU(lp
)->next
;
1110 if (lp
.bank
== FC_BANK_DYNAMIC
)
1112 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1121 FcConfigAdd (FcValueListPtr
*head
,
1122 FcValueList
*position
,
1126 FcValueListPtr
*prev
, last
, v
;
1127 FcValueBinding sameBinding
;
1130 sameBinding
= position
->binding
;
1132 sameBinding
= FcValueBindingWeak
;
1133 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1134 v
= FcValueListPtrU(v
)->next
)
1135 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1136 FcValueListPtrU(v
)->binding
= sameBinding
;
1140 prev
= &position
->next
;
1142 for (prev
= head
; FcValueListPtrU(*prev
);
1143 prev
= &(FcValueListPtrU(*prev
)->next
))
1150 for (prev
= head
; FcValueListPtrU(*prev
);
1151 prev
= &(FcValueListPtrU(*prev
)->next
))
1153 if (FcValueListPtrU(*prev
) == position
)
1160 if (FcDebug () & FC_DBG_EDIT
)
1162 if (!FcValueListPtrU(*prev
))
1163 printf ("position not on list\n");
1167 if (FcDebug () & FC_DBG_EDIT
)
1169 printf ("%s list before ", append
? "Append" : "Prepend");
1170 FcValueListPrint (*head
);
1176 last
= FcValueListPtrCreateDynamic(new);
1177 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1178 last
= FcValueListPtrU(last
)->next
;
1180 FcValueListPtrU(last
)->next
= *prev
;
1181 *prev
= FcValueListPtrCreateDynamic(new);
1184 if (FcDebug () & FC_DBG_EDIT
)
1186 printf ("%s list after ", append
? "Append" : "Prepend");
1187 FcValueListPrint (*head
);
1195 FcConfigDel (FcValueListPtr
*head
,
1196 FcValueList
*position
)
1198 FcValueListPtr
*prev
;
1200 for (prev
= head
; FcValueListPtrU(*prev
);
1201 prev
= &(FcValueListPtrU(*prev
)->next
))
1203 if (FcValueListPtrU(*prev
) == position
)
1205 *prev
= position
->next
;
1206 position
->next
= FcValueListPtrCreateDynamic(0);
1207 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1214 FcConfigPatternAdd (FcPattern
*p
,
1221 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1225 FcConfigAdd (&e
->values
, 0, append
, list
);
1230 * Delete all values associated with a field
1233 FcConfigPatternDel (FcPattern
*p
,
1236 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1239 while (FcValueListPtrU(e
->values
))
1240 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1244 FcConfigPatternCanon (FcPattern
*p
,
1247 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1250 if (!FcValueListPtrU(e
->values
))
1251 FcPatternDel (p
, object
);
1255 FcConfigSubstituteWithPat (FcConfig
*config
,
1270 config
= FcConfigGetCurrent ();
1275 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1276 if (!st
&& config
->maxObjects
)
1278 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1280 if (FcDebug () & FC_DBG_EDIT
)
1282 printf ("FcConfigSubstitute ");
1285 if (kind
== FcMatchPattern
)
1286 s
= config
->substPattern
;
1288 s
= config
->substFont
;
1289 for (; s
; s
= s
->next
)
1292 * Check the tests to see if
1293 * they all match the pattern
1295 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1297 if (FcDebug () & FC_DBG_EDIT
)
1299 printf ("FcConfigSubstitute test ");
1303 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1308 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1312 * If there's no such field in the font,
1313 * then FcQualAll matches while FcQualAny does not
1317 if (t
->qual
== FcQualAll
)
1326 * Check to see if there is a match, mark the location
1327 * to apply match-relative edits
1329 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1332 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1334 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1339 if (FcDebug () & FC_DBG_EDIT
)
1340 printf ("No match\n");
1343 if (FcDebug () & FC_DBG_EDIT
)
1345 printf ("Substitute ");
1348 for (e
= s
->edit
; e
; e
= e
->next
)
1351 * Evaluate the list of expressions
1353 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1355 * Locate any test associated with this field, skipping
1356 * tests associated with the pattern when substituting in
1359 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1361 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1362 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1363 (FcChar8
*) e
->field
))
1366 * KLUDGE - the pattern may have been reallocated or
1367 * things may have been inserted or deleted above
1368 * this element by other edits. Go back and find
1371 if (e
!= s
->edit
&& st
[i
].elt
)
1372 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1381 * If there was a test, then replace the matched
1382 * value with the new list of values
1386 FcValueList
*thisValue
= st
[i
].value
;
1387 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1390 * Append the new list of values after the current value
1392 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1394 * Delete the marked value
1396 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1398 * Adjust any pointers into the value list to ensure
1399 * future edits occur at the same place
1401 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1403 if (st
[i
].value
== thisValue
)
1404 st
[i
].value
= nextValue
;
1408 /* fall through ... */
1409 case FcOpAssignReplace
:
1411 * Delete all of the values and insert
1414 FcConfigPatternDel (p
, e
->field
);
1415 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1417 * Adjust any pointers into the value list as they no
1418 * longer point to anything valid
1422 FcPatternElt
*thisElt
= st
[i
].elt
;
1423 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1425 if (st
[i
].elt
== thisElt
)
1433 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1436 /* fall through ... */
1437 case FcOpPrependFirst
:
1438 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1443 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1446 /* fall through ... */
1447 case FcOpAppendLast
:
1448 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1455 * Now go through the pattern and eliminate
1456 * any properties without data
1458 for (e
= s
->edit
; e
; e
= e
->next
)
1459 FcConfigPatternCanon (p
, e
->field
);
1461 if (FcDebug () & FC_DBG_EDIT
)
1463 printf ("FcConfigSubstitute edit");
1467 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1469 if (FcDebug () & FC_DBG_EDIT
)
1471 printf ("FcConfigSubstitute done");
1478 FcConfigSubstitute (FcConfig
*config
,
1482 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1485 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1487 static FcChar8 fontconfig_path
[1000] = "";
1490 DllMain (HINSTANCE hinstDLL
,
1496 switch (fdwReason
) {
1497 case DLL_PROCESS_ATTACH
:
1498 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1499 sizeof (fontconfig_path
)))
1502 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1503 * assume it's a Unix-style installation tree, and use
1504 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1505 * folder where the DLL is as FONTCONFIG_PATH.
1507 p
= strrchr (fontconfig_path
, '\\');
1511 p
= strrchr (fontconfig_path
, '\\');
1512 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1513 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1515 strcat (fontconfig_path
, "\\etc\\fonts");
1518 fontconfig_path
[0] = '\0';
1526 #undef FONTCONFIG_PATH
1527 #define FONTCONFIG_PATH fontconfig_path
1529 #else /* !(_WIN32 && PIC) */
1531 #endif /* !(_WIN32 && PIC) */
1533 #ifndef FONTCONFIG_FILE
1534 #define FONTCONFIG_FILE "fonts.conf"
1538 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1543 dir
= (FcChar8
*) "";
1544 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1548 strcpy ((char *) path
, (const char *) dir
);
1549 /* make sure there's a single separator */
1551 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1552 path
[strlen((char *) path
)-1] != '\\')) &&
1555 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1556 strcat ((char *) path
, "\\");
1558 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1559 strcat ((char *) path
, "/");
1561 strcat ((char *) path
, (char *) file
);
1563 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1564 if (access ((char *) path
, R_OK
) == 0)
1572 FcConfigGetPath (void)
1575 FcChar8
*env
, *e
, *colon
;
1580 npath
= 2; /* default dir + null */
1581 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1587 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1590 path
= calloc (npath
, sizeof (FcChar8
*));
1600 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1602 colon
= e
+ strlen ((char *) e
);
1603 path
[i
] = malloc (colon
- e
+ 1);
1606 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1607 path
[i
][colon
- e
] = '\0';
1616 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1617 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1620 strcpy ((char *) path
[i
], (const char *) dir
);
1624 for (i
= 0; path
[i
]; i
++)
1632 FcConfigFreePath (FcChar8
**path
)
1636 for (p
= path
; *p
; p
++)
1641 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1646 if (_FcConfigHomeEnabled
)
1648 char *home
= getenv ("HOME");
1652 home
= getenv ("USERPROFILE");
1655 return (FcChar8
*) home
;
1661 FcConfigEnableHome (FcBool enable
)
1663 FcBool prev
= _FcConfigHomeEnabled
;
1664 _FcConfigHomeEnabled
= enable
;
1669 FcConfigFilename (const FcChar8
*url
)
1671 FcChar8
*file
, *dir
, **path
, **p
;
1675 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1677 url
= (FcChar8
*) FONTCONFIG_FILE
;
1682 if (isalpha (*url
) &&
1684 (url
[2] == '/' || url
[2] == '\\'))
1690 dir
= FcConfigHome ();
1692 file
= FcConfigFileExists (dir
, url
+ 1);
1701 file
= FcConfigFileExists (0, url
);
1704 path
= FcConfigGetPath ();
1707 for (p
= path
; *p
; p
++)
1709 file
= FcConfigFileExists (*p
, url
);
1713 FcConfigFreePath (path
);
1720 * Manage the application-specific fonts
1724 FcConfigAppFontAddFile (FcConfig
*config
,
1725 const FcChar8
*file
)
1734 config
= FcConfigGetCurrent ();
1739 subdirs
= FcStrSetCreate ();
1743 set
= FcConfigGetFonts (config
, FcSetApplication
);
1746 set
= FcFontSetCreate ();
1749 FcStrSetDestroy (subdirs
);
1752 FcConfigSetFonts (config
, set
, FcSetApplication
);
1755 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1757 FcStrSetDestroy (subdirs
);
1760 if ((sublist
= FcStrListCreate (subdirs
)))
1762 while ((subdir
= FcStrListNext (sublist
)))
1764 FcConfigAppFontAddDir (config
, subdir
);
1766 FcStrListDone (sublist
);
1772 FcConfigAppFontAddDir (FcConfig
*config
,
1782 config
= FcConfigGetCurrent ();
1786 subdirs
= FcStrSetCreate ();
1790 set
= FcConfigGetFonts (config
, FcSetApplication
);
1793 set
= FcFontSetCreate ();
1796 FcStrSetDestroy (subdirs
);
1799 FcConfigSetFonts (config
, set
, FcSetApplication
);
1802 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1804 FcStrSetDestroy (subdirs
);
1807 if ((sublist
= FcStrListCreate (subdirs
)))
1809 while ((subdir
= FcStrListNext (sublist
)))
1811 FcConfigAppFontAddDir (config
, subdir
);
1813 FcStrListDone (sublist
);
1819 FcConfigAppFontClear (FcConfig
*config
)
1823 config
= FcConfigGetCurrent ();
1828 FcConfigSetFonts (config
, 0, FcSetApplication
);
1832 * Manage filename-based font source selectors
1836 FcConfigGlobAdd (FcConfig
*config
,
1837 const FcChar8
*glob
,
1840 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1842 return FcStrSetAdd (set
, glob
);
1846 FcConfigGlobMatch (const FcChar8
*glob
,
1847 const FcChar8
*string
)
1851 while ((c
= *glob
++))
1855 /* short circuit common case */
1858 /* short circuit another common case */
1859 if (strchr ((char *) glob
, '*') == 0)
1860 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1863 if (FcConfigGlobMatch (glob
, string
))
1869 if (*string
++ == '\0')
1878 return *string
== '\0';
1882 FcConfigGlobsMatch (const FcStrSet
*globs
,
1883 const FcChar8
*string
)
1887 for (i
= 0; i
< globs
->num
; i
++)
1888 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1894 FcConfigAcceptFilename (FcConfig
*config
,
1895 const FcChar8
*filename
)
1897 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1899 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1905 * Manage font-pattern based font source selectors
1909 FcConfigPatternsAdd (FcConfig
*config
,
1913 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1915 return FcFontSetAdd (set
, pattern
);
1919 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1920 const FcPattern
*font
)
1924 for (i
= 0; i
< patterns
->nfont
; i
++)
1925 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1931 FcConfigAcceptFont (FcConfig
*config
,
1932 const FcPattern
*font
)
1934 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1936 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))