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
));
143 FcConfigNewestFile (FcStrSet
*files
)
145 FcStrList
*list
= FcStrListCreate (files
);
146 FcFileTime newest
= { 0, FcFalse
};
152 while ((file
= FcStrListNext (list
)))
153 if (stat ((char *) file
, &statb
) == 0)
154 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
157 newest
.time
= statb
.st_mtime
;
159 FcStrListDone (list
);
165 FcConfigModifiedTime (FcConfig
*config
)
169 FcFileTime v
= { 0, FcFalse
};
170 config
= FcConfigGetCurrent ();
174 return FcConfigNewestFile (config
->configFiles
);
178 FcConfigUptoDate (FcConfig
*config
)
180 FcFileTime config_time
, font_time
;
181 time_t now
= time(0);
184 config
= FcConfigGetCurrent ();
188 config_time
= FcConfigNewestFile (config
->configFiles
);
189 font_time
= FcConfigNewestFile (config
->fontDirs
);
190 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
191 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
195 config
->rescanTime
= now
;
200 FcSubstDestroy (FcSubst
*s
)
208 FcTestDestroy (s
->test
);
210 FcEditDestroy (s
->edit
);
212 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
218 FcConfigDestroy (FcConfig
*config
)
222 if (config
== _fcConfig
)
225 FcStrSetDestroy (config
->configDirs
);
226 FcStrSetDestroy (config
->fontDirs
);
227 FcStrSetDestroy (config
->configFiles
);
228 FcStrSetDestroy (config
->acceptGlobs
);
229 FcStrSetDestroy (config
->rejectGlobs
);
230 FcFontSetDestroy (config
->acceptPatterns
);
231 FcFontSetDestroy (config
->rejectPatterns
);
234 FcBlanksDestroy (config
->blanks
);
237 FcStrFree (config
->cache
);
239 FcSubstDestroy (config
->substPattern
);
240 FcSubstDestroy (config
->substFont
);
241 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
242 if (config
->fonts
[set
])
243 FcFontSetDestroy (config
->fonts
[set
]);
246 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
250 * Scan the current list of directories in the configuration
251 * and build the set of available fonts. Update the
252 * per-user cache file to reflect the new configuration
256 FcConfigBuildFonts (FcConfig
*config
)
258 FcFontSet
*fonts
, *cached_fonts
;
259 FcGlobalCache
*cache
;
264 fonts
= FcFontSetCreate ();
268 cache
= FcGlobalCacheCreate ();
272 oldDirs
= FcStrSetCreate ();
277 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
, config
);
279 cached_fonts
= FcCacheRead(config
, cache
);
282 list
= FcConfigGetFontDirs (config
);
286 while ((dir
= FcStrListNext (list
)))
288 if (FcDebug () & FC_DBG_FONTSET
)
289 printf ("build scan dir %s\n", dir
);
290 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
291 config
->blanks
, dir
, FcFalse
, config
);
294 FcStrListDone (list
);
300 for (i
= 0; i
< oldDirs
->num
; i
++)
302 if (FcDebug () & FC_DBG_FONTSET
)
303 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
304 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
305 config
->blanks
, oldDirs
->strs
[i
],
309 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
311 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]))
312 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
314 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
316 cached_fonts
->nfont
= 0;
317 FcFontSetDestroy (cached_fonts
);
320 if (FcDebug () & FC_DBG_FONTSET
)
321 FcFontSetPrint (fonts
);
324 FcGlobalCacheSave (cache
, config
->cache
);
325 FcGlobalCacheDestroy (cache
);
326 FcStrSetDestroy (oldDirs
);
328 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
332 FcStrSetDestroy (oldDirs
);
334 FcFontSetDestroy (fonts
);
340 FcConfigSetCurrent (FcConfig
*config
)
343 if (!FcConfigBuildFonts (config
))
347 FcConfigDestroy (_fcConfig
);
353 FcConfigGetCurrent (void)
362 FcConfigAddConfigDir (FcConfig
*config
,
365 return FcStrSetAddFilename (config
->configDirs
, d
);
369 FcConfigGetConfigDirs (FcConfig
*config
)
373 config
= FcConfigGetCurrent ();
377 return FcStrListCreate (config
->configDirs
);
381 FcConfigAddFontDir (FcConfig
*config
,
384 return FcStrSetAddFilename (config
->fontDirs
, d
);
388 FcConfigAddDir (FcConfig
*config
,
391 return (FcConfigAddConfigDir (config
, d
) &&
392 FcConfigAddFontDir (config
, d
));
396 FcConfigGetFontDirs (FcConfig
*config
)
400 config
= FcConfigGetCurrent ();
404 return FcStrListCreate (config
->fontDirs
);
408 FcConfigAddConfigFile (FcConfig
*config
,
412 FcChar8
*file
= FcConfigFilename (f
);
417 ret
= FcStrSetAdd (config
->configFiles
, file
);
423 FcConfigGetConfigFiles (FcConfig
*config
)
427 config
= FcConfigGetCurrent ();
431 return FcStrListCreate (config
->configFiles
);
435 FcConfigSetCache (FcConfig
*config
,
438 FcChar8
*new = FcStrCopyFilename (c
);
443 FcStrFree (config
->cache
);
449 FcConfigGetCache (FcConfig
*config
)
453 config
= FcConfigGetCurrent ();
457 return config
->cache
;
461 FcConfigGetFonts (FcConfig
*config
,
466 config
= FcConfigGetCurrent ();
470 return config
->fonts
[set
];
474 FcConfigSetFonts (FcConfig
*config
,
478 if (config
->fonts
[set
])
479 FcFontSetDestroy (config
->fonts
[set
]);
480 config
->fonts
[set
] = fonts
;
486 FcConfigGetBlanks (FcConfig
*config
)
490 config
= FcConfigGetCurrent ();
494 return config
->blanks
;
498 FcConfigAddBlank (FcConfig
*config
,
506 b
= FcBlanksCreate ();
510 if (!FcBlanksAdd (b
, blank
))
517 FcConfigGetRescanInverval (FcConfig
*config
)
521 config
= FcConfigGetCurrent ();
525 return config
->rescanInterval
;
529 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
533 config
= FcConfigGetCurrent ();
537 config
->rescanInterval
= rescanInterval
;
542 FcConfigAddEdit (FcConfig
*config
,
547 FcSubst
*subst
, **prev
;
551 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
554 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
555 if (kind
== FcMatchPattern
)
556 prev
= &config
->substPattern
;
558 prev
= &config
->substFont
;
559 for (; *prev
; prev
= &(*prev
)->next
);
565 for (t
= test
; t
; t
= t
->next
)
567 if (t
->kind
== FcMatchDefault
)
571 if (config
->maxObjects
< num
)
572 config
->maxObjects
= num
;
573 if (FcDebug () & FC_DBG_EDIT
)
575 printf ("Add Subst ");
576 FcSubstPrint (subst
);
581 typedef struct _FcSubState
{
587 FcConfigPromote (FcValue v
, FcValue u
)
589 if (v
.type
== FcTypeInteger
)
591 v
.type
= FcTypeDouble
;
592 v
.u
.d
= (double) v
.u
.i
;
594 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
596 v
.u
.m
= &FcIdentityMatrix
;
597 v
.type
= FcTypeMatrix
;
599 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
601 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
602 v
.type
= FcTypeLangSet
;
608 FcConfigCompareValue (const FcValue
*left_o
,
610 const FcValue
*right_o
)
612 FcValue left
= FcValueCanonicalize(left_o
);
613 FcValue right
= FcValueCanonicalize(right_o
);
614 FcBool ret
= FcFalse
;
616 left
= FcConfigPromote (left
, right
);
617 right
= FcConfigPromote (right
, left
);
618 if (left
.type
== right
.type
)
622 break; /* FcConfigPromote prevents this from happening */
628 ret
= left
.u
.d
== right
.u
.d
;
631 case FcOpNotContains
:
632 ret
= left
.u
.d
!= right
.u
.d
;
635 ret
= left
.u
.d
< right
.u
.d
;
638 ret
= left
.u
.d
<= right
.u
.d
;
641 ret
= left
.u
.d
> right
.u
.d
;
644 ret
= left
.u
.d
>= right
.u
.d
;
655 ret
= left
.u
.b
== right
.u
.b
;
658 case FcOpNotContains
:
659 ret
= left
.u
.b
!= right
.u
.b
;
669 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
672 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
675 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
677 case FcOpNotContains
:
678 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
689 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
692 case FcOpNotContains
:
693 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
703 /* left contains right if right is a subset of left */
704 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
706 case FcOpNotContains
:
707 /* left contains right if right is a subset of left */
708 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
711 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
714 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
724 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
726 case FcOpNotContains
:
727 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
730 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
733 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
755 ret
= left
.u
.f
== right
.u
.f
;
758 case FcOpNotContains
:
759 ret
= left
.u
.f
!= right
.u
.f
;
769 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
776 #define _FcDoubleFloor(d) ((int) (d))
777 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
778 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
779 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
780 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
781 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
784 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
792 v
.type
= FcTypeInteger
;
796 v
.type
= FcTypeDouble
;
800 v
.type
= FcTypeString
;
801 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
805 v
.type
= FcTypeMatrix
;
810 v
.type
= FcTypeCharSet
;
819 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
820 if (r
!= FcResultMatch
)
824 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
825 v
.type
= FcTypeInteger
;
830 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
831 if (vl
.type
== FcTypeBool
)
834 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
836 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
849 case FcOpNotContains
:
851 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
852 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
854 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
864 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
865 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
866 vl
= FcConfigPromote (vl
, vr
);
867 vr
= FcConfigPromote (vr
, vl
);
868 if (vl
.type
== vr
.type
)
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
;
882 v
.type
= FcTypeDouble
;
883 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
886 v
.type
= FcTypeDouble
;
887 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
893 if (v
.type
== FcTypeDouble
&&
894 v
.u
.d
== (double) (int) v
.u
.d
)
896 v
.type
= FcTypeInteger
;
904 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
908 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
918 v
.type
= FcTypeString
;
919 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
932 v
.type
= FcTypeMatrix
;
933 m
= malloc (sizeof (FcMatrix
));
936 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
937 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
961 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
974 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
980 v
.type
= FcTypeInteger
;
981 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
990 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
996 v
.type
= FcTypeInteger
;
997 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1000 v
.type
= FcTypeVoid
;
1003 FcValueDestroy (vl
);
1006 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1012 v
.type
= FcTypeInteger
;
1013 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1016 v
.type
= FcTypeVoid
;
1019 FcValueDestroy (vl
);
1022 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1028 v
.type
= FcTypeInteger
;
1029 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1032 v
.type
= FcTypeVoid
;
1035 FcValueDestroy (vl
);
1038 v
.type
= FcTypeVoid
;
1044 static FcValueList
*
1045 FcConfigMatchValueList (FcPattern
*p
,
1047 FcValueList
*values
)
1049 FcValueList
*ret
= 0;
1050 FcExpr
*e
= t
->expr
;
1056 /* Compute the value of the match expression */
1057 if (e
->op
== FcOpComma
)
1059 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1060 e
= e
->u
.tree
.right
;
1064 value
= FcConfigEvaluate (p
, e
);
1068 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1070 /* Compare the pattern value to the match expression value */
1071 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1078 if (t
->qual
== FcQualAll
)
1085 FcValueDestroy (value
);
1090 static FcValueList
*
1091 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1097 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1100 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1101 if (e
->op
== FcOpComma
)
1103 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1104 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1108 l
->value
= FcConfigEvaluate (p
, e
);
1109 l
->next
= FcValueListPtrCreateDynamic(0);
1111 l
->binding
= binding
;
1112 if (l
->value
.type
== FcTypeVoid
)
1114 FcValueList
*next
= FcValueListPtrU(l
->next
);
1116 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1125 FcConfigAdd (FcValueListPtr
*head
,
1126 FcValueList
*position
,
1130 FcValueListPtr
*prev
, last
, v
;
1131 FcValueBinding sameBinding
;
1134 sameBinding
= position
->binding
;
1136 sameBinding
= FcValueBindingWeak
;
1137 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1138 v
= FcValueListPtrU(v
)->next
)
1139 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1140 FcValueListPtrU(v
)->binding
= sameBinding
;
1144 prev
= &position
->next
;
1146 for (prev
= head
; FcValueListPtrU(*prev
);
1147 prev
= &(FcValueListPtrU(*prev
)->next
))
1154 for (prev
= head
; FcValueListPtrU(*prev
);
1155 prev
= &(FcValueListPtrU(*prev
)->next
))
1157 if (FcValueListPtrU(*prev
) == position
)
1164 if (FcDebug () & FC_DBG_EDIT
)
1166 if (!FcValueListPtrU(*prev
))
1167 printf ("position not on list\n");
1171 if (FcDebug () & FC_DBG_EDIT
)
1173 printf ("%s list before ", append
? "Append" : "Prepend");
1174 FcValueListPrint (*head
);
1180 last
= FcValueListPtrCreateDynamic(new);
1181 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1182 last
= FcValueListPtrU(last
)->next
;
1184 FcValueListPtrU(last
)->next
= *prev
;
1185 *prev
= FcValueListPtrCreateDynamic(new);
1188 if (FcDebug () & FC_DBG_EDIT
)
1190 printf ("%s list after ", append
? "Append" : "Prepend");
1191 FcValueListPrint (*head
);
1199 FcConfigDel (FcValueListPtr
*head
,
1200 FcValueList
*position
)
1202 FcValueListPtr
*prev
;
1204 for (prev
= head
; FcValueListPtrU(*prev
);
1205 prev
= &(FcValueListPtrU(*prev
)->next
))
1207 if (FcValueListPtrU(*prev
) == position
)
1209 *prev
= position
->next
;
1210 position
->next
= FcValueListPtrCreateDynamic(0);
1211 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1218 FcConfigPatternAdd (FcPattern
*p
,
1225 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1229 FcConfigAdd (&e
->values
, 0, append
, list
);
1234 * Delete all values associated with a field
1237 FcConfigPatternDel (FcPattern
*p
,
1240 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1243 while (FcValueListPtrU(e
->values
))
1244 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1248 FcConfigPatternCanon (FcPattern
*p
,
1251 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1254 if (!FcValueListPtrU(e
->values
))
1255 FcPatternDel (p
, object
);
1259 FcConfigSubstituteWithPat (FcConfig
*config
,
1274 config
= FcConfigGetCurrent ();
1279 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1280 if (!st
&& config
->maxObjects
)
1282 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1284 if (FcDebug () & FC_DBG_EDIT
)
1286 printf ("FcConfigSubstitute ");
1289 if (kind
== FcMatchPattern
)
1290 s
= config
->substPattern
;
1292 s
= config
->substFont
;
1293 for (; s
; s
= s
->next
)
1296 * Check the tests to see if
1297 * they all match the pattern
1299 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1301 if (FcDebug () & FC_DBG_EDIT
)
1303 printf ("FcConfigSubstitute test ");
1307 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1312 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1316 * If there's no such field in the font,
1317 * then FcQualAll matches while FcQualAny does not
1321 if (t
->qual
== FcQualAll
)
1330 * Check to see if there is a match, mark the location
1331 * to apply match-relative edits
1333 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1336 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1338 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1343 if (FcDebug () & FC_DBG_EDIT
)
1344 printf ("No match\n");
1347 if (FcDebug () & FC_DBG_EDIT
)
1349 printf ("Substitute ");
1352 for (e
= s
->edit
; e
; e
= e
->next
)
1355 * Evaluate the list of expressions
1357 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1359 * Locate any test associated with this field, skipping
1360 * tests associated with the pattern when substituting in
1363 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1365 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1366 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1367 (FcChar8
*) e
->field
))
1370 * KLUDGE - the pattern may have been reallocated or
1371 * things may have been inserted or deleted above
1372 * this element by other edits. Go back and find
1375 if (e
!= s
->edit
&& st
[i
].elt
)
1376 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1385 * If there was a test, then replace the matched
1386 * value with the new list of values
1390 FcValueList
*thisValue
= st
[i
].value
;
1391 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1394 * Append the new list of values after the current value
1396 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1398 * Delete the marked value
1400 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1402 * Adjust any pointers into the value list to ensure
1403 * future edits occur at the same place
1405 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1407 if (st
[i
].value
== thisValue
)
1408 st
[i
].value
= nextValue
;
1412 /* fall through ... */
1413 case FcOpAssignReplace
:
1415 * Delete all of the values and insert
1418 FcConfigPatternDel (p
, e
->field
);
1419 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1421 * Adjust any pointers into the value list as they no
1422 * longer point to anything valid
1426 FcPatternElt
*thisElt
= st
[i
].elt
;
1427 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1429 if (st
[i
].elt
== thisElt
)
1437 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1440 /* fall through ... */
1441 case FcOpPrependFirst
:
1442 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1447 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1450 /* fall through ... */
1451 case FcOpAppendLast
:
1452 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1459 * Now go through the pattern and eliminate
1460 * any properties without data
1462 for (e
= s
->edit
; e
; e
= e
->next
)
1463 FcConfigPatternCanon (p
, e
->field
);
1465 if (FcDebug () & FC_DBG_EDIT
)
1467 printf ("FcConfigSubstitute edit");
1471 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1473 if (FcDebug () & FC_DBG_EDIT
)
1475 printf ("FcConfigSubstitute done");
1482 FcConfigSubstitute (FcConfig
*config
,
1486 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1489 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1491 static FcChar8 fontconfig_path
[1000] = "";
1494 DllMain (HINSTANCE hinstDLL
,
1500 switch (fdwReason
) {
1501 case DLL_PROCESS_ATTACH
:
1502 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1503 sizeof (fontconfig_path
)))
1506 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1507 * assume it's a Unix-style installation tree, and use
1508 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1509 * folder where the DLL is as FONTCONFIG_PATH.
1511 p
= strrchr (fontconfig_path
, '\\');
1515 p
= strrchr (fontconfig_path
, '\\');
1516 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1517 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1519 strcat (fontconfig_path
, "\\etc\\fonts");
1522 fontconfig_path
[0] = '\0';
1530 #undef FONTCONFIG_PATH
1531 #define FONTCONFIG_PATH fontconfig_path
1533 #else /* !(_WIN32 && PIC) */
1535 #endif /* !(_WIN32 && PIC) */
1537 #ifndef FONTCONFIG_FILE
1538 #define FONTCONFIG_FILE "fonts.conf"
1542 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1547 dir
= (FcChar8
*) "";
1548 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1552 strcpy ((char *) path
, (const char *) dir
);
1553 /* make sure there's a single separator */
1555 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1556 path
[strlen((char *) path
)-1] != '\\')) &&
1559 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1560 strcat ((char *) path
, "\\");
1562 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1563 strcat ((char *) path
, "/");
1565 strcat ((char *) path
, (char *) file
);
1567 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1568 if (access ((char *) path
, R_OK
) == 0)
1576 FcConfigGetPath (void)
1579 FcChar8
*env
, *e
, *colon
;
1584 npath
= 2; /* default dir + null */
1585 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1591 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1594 path
= calloc (npath
, sizeof (FcChar8
*));
1604 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1606 colon
= e
+ strlen ((char *) e
);
1607 path
[i
] = malloc (colon
- e
+ 1);
1610 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1611 path
[i
][colon
- e
] = '\0';
1620 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1621 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1624 strcpy ((char *) path
[i
], (const char *) dir
);
1628 for (i
= 0; path
[i
]; i
++)
1636 FcConfigFreePath (FcChar8
**path
)
1640 for (p
= path
; *p
; p
++)
1645 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1650 if (_FcConfigHomeEnabled
)
1652 char *home
= getenv ("HOME");
1656 home
= getenv ("USERPROFILE");
1659 return (FcChar8
*) home
;
1665 FcConfigEnableHome (FcBool enable
)
1667 FcBool prev
= _FcConfigHomeEnabled
;
1668 _FcConfigHomeEnabled
= enable
;
1673 FcConfigFilename (const FcChar8
*url
)
1675 FcChar8
*file
, *dir
, **path
, **p
;
1679 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1681 url
= (FcChar8
*) FONTCONFIG_FILE
;
1686 if (isalpha (*url
) &&
1688 (url
[2] == '/' || url
[2] == '\\'))
1694 dir
= FcConfigHome ();
1696 file
= FcConfigFileExists (dir
, url
+ 1);
1705 file
= FcConfigFileExists (0, url
);
1708 path
= FcConfigGetPath ();
1711 for (p
= path
; *p
; p
++)
1713 file
= FcConfigFileExists (*p
, url
);
1717 FcConfigFreePath (path
);
1724 * Manage the application-specific fonts
1728 FcConfigAppFontAddFile (FcConfig
*config
,
1729 const FcChar8
*file
)
1738 config
= FcConfigGetCurrent ();
1743 subdirs
= FcStrSetCreate ();
1747 set
= FcConfigGetFonts (config
, FcSetApplication
);
1750 set
= FcFontSetCreate ();
1753 FcStrSetDestroy (subdirs
);
1756 FcConfigSetFonts (config
, set
, FcSetApplication
);
1759 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1761 FcStrSetDestroy (subdirs
);
1764 if ((sublist
= FcStrListCreate (subdirs
)))
1766 while ((subdir
= FcStrListNext (sublist
)))
1768 FcConfigAppFontAddDir (config
, subdir
);
1770 FcStrListDone (sublist
);
1776 FcConfigAppFontAddDir (FcConfig
*config
,
1786 config
= FcConfigGetCurrent ();
1790 subdirs
= FcStrSetCreate ();
1794 set
= FcConfigGetFonts (config
, FcSetApplication
);
1797 set
= FcFontSetCreate ();
1800 FcStrSetDestroy (subdirs
);
1803 FcConfigSetFonts (config
, set
, FcSetApplication
);
1806 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1808 FcStrSetDestroy (subdirs
);
1811 if ((sublist
= FcStrListCreate (subdirs
)))
1813 while ((subdir
= FcStrListNext (sublist
)))
1815 FcConfigAppFontAddDir (config
, subdir
);
1817 FcStrListDone (sublist
);
1823 FcConfigAppFontClear (FcConfig
*config
)
1827 config
= FcConfigGetCurrent ();
1832 FcConfigSetFonts (config
, 0, FcSetApplication
);
1836 * Manage filename-based font source selectors
1840 FcConfigGlobAdd (FcConfig
*config
,
1841 const FcChar8
*glob
,
1844 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1846 return FcStrSetAdd (set
, glob
);
1850 FcConfigGlobMatch (const FcChar8
*glob
,
1851 const FcChar8
*string
)
1855 while ((c
= *glob
++))
1859 /* short circuit common case */
1862 /* short circuit another common case */
1863 if (strchr ((char *) glob
, '*') == 0)
1864 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1867 if (FcConfigGlobMatch (glob
, string
))
1873 if (*string
++ == '\0')
1882 return *string
== '\0';
1886 FcConfigGlobsMatch (const FcStrSet
*globs
,
1887 const FcChar8
*string
)
1891 for (i
= 0; i
< globs
->num
; i
++)
1892 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1898 FcConfigAcceptFilename (FcConfig
*config
,
1899 const FcChar8
*filename
)
1901 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1903 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1909 * Manage font-pattern based font source selectors
1913 FcConfigPatternsAdd (FcConfig
*config
,
1917 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1919 return FcFontSetAdd (set
, pattern
);
1923 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1924 const FcPattern
*font
)
1928 for (i
= 0; i
< patterns
->nfont
; i
++)
1929 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1935 FcConfigAcceptFont (FcConfig
*config
,
1936 const FcPattern
*font
)
1938 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1940 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))