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
++)
312 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
314 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
315 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
316 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
318 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
320 cached_fonts
->nfont
= 0;
321 FcFontSetDestroy (cached_fonts
);
324 if (FcDebug () & FC_DBG_FONTSET
)
325 FcFontSetPrint (fonts
);
328 FcGlobalCacheSave (cache
, config
->cache
, config
);
329 FcGlobalCacheDestroy (cache
);
330 FcStrSetDestroy (oldDirs
);
332 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
336 FcStrSetDestroy (oldDirs
);
338 FcFontSetDestroy (fonts
);
344 FcConfigSetCurrent (FcConfig
*config
)
347 if (!FcConfigBuildFonts (config
))
351 FcConfigDestroy (_fcConfig
);
357 FcConfigGetCurrent (void)
366 FcConfigAddConfigDir (FcConfig
*config
,
369 return FcStrSetAddFilename (config
->configDirs
, d
);
373 FcConfigGetConfigDirs (FcConfig
*config
)
377 config
= FcConfigGetCurrent ();
381 return FcStrListCreate (config
->configDirs
);
385 FcConfigAddFontDir (FcConfig
*config
,
388 return FcStrSetAddFilename (config
->fontDirs
, d
);
392 FcConfigNormalizeFontDir (FcConfig
*config
,
395 /* If this is a bottleneck, we can cache the fontDir inodes. */
401 if (stat ((char *)d
, &s
) == -1)
403 di
= s
.st_ino
; dd
= s
.st_dev
;
405 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
407 if (stat ((char *)config
->fontDirs
->strs
[n
], &s
) == -1)
409 if (di
== s
.st_ino
&& dd
== s
.st_dev
)
410 return config
->fontDirs
->strs
[n
];
416 FcConfigAddDir (FcConfig
*config
,
419 return (FcConfigAddConfigDir (config
, d
) &&
420 FcConfigAddFontDir (config
, d
));
424 FcConfigGetFontDirs (FcConfig
*config
)
428 config
= FcConfigGetCurrent ();
432 return FcStrListCreate (config
->fontDirs
);
436 FcConfigAddConfigFile (FcConfig
*config
,
440 FcChar8
*file
= FcConfigFilename (f
);
445 ret
= FcStrSetAdd (config
->configFiles
, file
);
451 FcConfigGetConfigFiles (FcConfig
*config
)
455 config
= FcConfigGetCurrent ();
459 return FcStrListCreate (config
->configFiles
);
463 FcConfigSetCache (FcConfig
*config
,
466 FcChar8
*new = FcStrCopyFilename (c
);
471 FcStrFree (config
->cache
);
477 FcConfigGetCache (FcConfig
*config
)
481 config
= FcConfigGetCurrent ();
485 return config
->cache
;
489 FcConfigGetFonts (FcConfig
*config
,
494 config
= FcConfigGetCurrent ();
498 return config
->fonts
[set
];
502 FcConfigSetFonts (FcConfig
*config
,
506 if (config
->fonts
[set
])
507 FcFontSetDestroy (config
->fonts
[set
]);
508 config
->fonts
[set
] = fonts
;
514 FcConfigGetBlanks (FcConfig
*config
)
518 config
= FcConfigGetCurrent ();
522 return config
->blanks
;
526 FcConfigAddBlank (FcConfig
*config
,
534 b
= FcBlanksCreate ();
538 if (!FcBlanksAdd (b
, blank
))
545 FcConfigGetRescanInverval (FcConfig
*config
)
549 config
= FcConfigGetCurrent ();
553 return config
->rescanInterval
;
557 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
561 config
= FcConfigGetCurrent ();
565 config
->rescanInterval
= rescanInterval
;
570 FcConfigAddEdit (FcConfig
*config
,
575 FcSubst
*subst
, **prev
;
579 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
582 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
583 if (kind
== FcMatchPattern
)
584 prev
= &config
->substPattern
;
586 prev
= &config
->substFont
;
587 for (; *prev
; prev
= &(*prev
)->next
);
593 for (t
= test
; t
; t
= t
->next
)
595 if (t
->kind
== FcMatchDefault
)
599 if (config
->maxObjects
< num
)
600 config
->maxObjects
= num
;
601 if (FcDebug () & FC_DBG_EDIT
)
603 printf ("Add Subst ");
604 FcSubstPrint (subst
);
609 typedef struct _FcSubState
{
615 FcConfigPromote (FcValue v
, FcValue u
)
617 if (v
.type
== FcTypeInteger
)
619 v
.type
= FcTypeDouble
;
620 v
.u
.d
= (double) v
.u
.i
;
622 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
624 v
.u
.m
= &FcIdentityMatrix
;
625 v
.type
= FcTypeMatrix
;
627 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
629 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
630 v
.type
= FcTypeLangSet
;
636 FcConfigCompareValue (const FcValue
*left_o
,
638 const FcValue
*right_o
)
640 FcValue left
= FcValueCanonicalize(left_o
);
641 FcValue right
= FcValueCanonicalize(right_o
);
642 FcBool ret
= FcFalse
;
644 left
= FcConfigPromote (left
, right
);
645 right
= FcConfigPromote (right
, left
);
646 if (left
.type
== right
.type
)
650 break; /* FcConfigPromote prevents this from happening */
656 ret
= left
.u
.d
== right
.u
.d
;
659 case FcOpNotContains
:
660 ret
= left
.u
.d
!= right
.u
.d
;
663 ret
= left
.u
.d
< right
.u
.d
;
666 ret
= left
.u
.d
<= right
.u
.d
;
669 ret
= left
.u
.d
> right
.u
.d
;
672 ret
= left
.u
.d
>= right
.u
.d
;
683 ret
= left
.u
.b
== right
.u
.b
;
686 case FcOpNotContains
:
687 ret
= left
.u
.b
!= right
.u
.b
;
697 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
700 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
703 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
705 case FcOpNotContains
:
706 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
717 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
720 case FcOpNotContains
:
721 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
731 /* left contains right if right is a subset of left */
732 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
734 case FcOpNotContains
:
735 /* left contains right if right is a subset of left */
736 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
739 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
742 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
752 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
754 case FcOpNotContains
:
755 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
758 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
761 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
783 ret
= left
.u
.f
== right
.u
.f
;
786 case FcOpNotContains
:
787 ret
= left
.u
.f
!= right
.u
.f
;
797 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
804 #define _FcDoubleFloor(d) ((int) (d))
805 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
806 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
807 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
808 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
809 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
812 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
820 v
.type
= FcTypeInteger
;
824 v
.type
= FcTypeDouble
;
828 v
.type
= FcTypeString
;
829 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
833 v
.type
= FcTypeMatrix
;
838 v
.type
= FcTypeCharSet
;
847 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
848 if (r
!= FcResultMatch
)
853 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
854 v
.type
= FcTypeInteger
;
859 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
860 if (vl
.type
== FcTypeBool
)
863 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
865 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
878 case FcOpNotContains
:
880 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
881 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
883 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
893 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
894 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
895 vl
= FcConfigPromote (vl
, vr
);
896 vr
= FcConfigPromote (vr
, vl
);
897 if (vl
.type
== vr
.type
)
903 v
.type
= FcTypeDouble
;
904 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
907 v
.type
= FcTypeDouble
;
908 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
911 v
.type
= FcTypeDouble
;
912 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
915 v
.type
= FcTypeDouble
;
916 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
922 if (v
.type
== FcTypeDouble
&&
923 v
.u
.d
== (double) (int) v
.u
.d
)
925 v
.type
= FcTypeInteger
;
933 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
937 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
947 v
.type
= FcTypeString
;
948 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
961 v
.type
= FcTypeMatrix
;
962 m
= malloc (sizeof (FcMatrix
));
965 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
966 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
990 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1000 FcValueDestroy (vl
);
1003 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1009 v
.type
= FcTypeInteger
;
1010 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1013 v
.type
= FcTypeVoid
;
1016 FcValueDestroy (vl
);
1019 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1025 v
.type
= FcTypeInteger
;
1026 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1029 v
.type
= FcTypeVoid
;
1032 FcValueDestroy (vl
);
1035 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1041 v
.type
= FcTypeInteger
;
1042 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1045 v
.type
= FcTypeVoid
;
1048 FcValueDestroy (vl
);
1051 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1057 v
.type
= FcTypeInteger
;
1058 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1061 v
.type
= FcTypeVoid
;
1064 FcValueDestroy (vl
);
1067 v
.type
= FcTypeVoid
;
1073 static FcValueList
*
1074 FcConfigMatchValueList (FcPattern
*p
,
1076 FcValueList
*values
)
1078 FcValueList
*ret
= 0;
1079 FcExpr
*e
= t
->expr
;
1085 /* Compute the value of the match expression */
1086 if (e
->op
== FcOpComma
)
1088 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1089 e
= e
->u
.tree
.right
;
1093 value
= FcConfigEvaluate (p
, e
);
1097 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1099 /* Compare the pattern value to the match expression value */
1100 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1107 if (t
->qual
== FcQualAll
)
1114 FcValueDestroy (value
);
1119 static FcValueList
*
1120 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1126 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1129 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1130 if (e
->op
== FcOpComma
)
1132 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1133 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1137 l
->value
= FcConfigEvaluate (p
, e
);
1138 l
->next
= FcValueListPtrCreateDynamic(0);
1140 l
->binding
= binding
;
1141 if (l
->value
.type
== FcTypeVoid
)
1143 FcValueList
*next
= FcValueListPtrU(l
->next
);
1145 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1154 FcConfigAdd (FcValueListPtr
*head
,
1155 FcValueList
*position
,
1159 FcValueListPtr
*prev
, last
, v
;
1160 FcValueBinding sameBinding
;
1163 sameBinding
= position
->binding
;
1165 sameBinding
= FcValueBindingWeak
;
1166 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1167 v
= FcValueListPtrU(v
)->next
)
1168 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1169 FcValueListPtrU(v
)->binding
= sameBinding
;
1173 prev
= &position
->next
;
1175 for (prev
= head
; FcValueListPtrU(*prev
);
1176 prev
= &(FcValueListPtrU(*prev
)->next
))
1183 for (prev
= head
; FcValueListPtrU(*prev
);
1184 prev
= &(FcValueListPtrU(*prev
)->next
))
1186 if (FcValueListPtrU(*prev
) == position
)
1193 if (FcDebug () & FC_DBG_EDIT
)
1195 if (!FcValueListPtrU(*prev
))
1196 printf ("position not on list\n");
1200 if (FcDebug () & FC_DBG_EDIT
)
1202 printf ("%s list before ", append
? "Append" : "Prepend");
1203 FcValueListPrint (*head
);
1209 last
= FcValueListPtrCreateDynamic(new);
1210 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1211 last
= FcValueListPtrU(last
)->next
;
1213 FcValueListPtrU(last
)->next
= *prev
;
1214 *prev
= FcValueListPtrCreateDynamic(new);
1217 if (FcDebug () & FC_DBG_EDIT
)
1219 printf ("%s list after ", append
? "Append" : "Prepend");
1220 FcValueListPrint (*head
);
1228 FcConfigDel (FcValueListPtr
*head
,
1229 FcValueList
*position
)
1231 FcValueListPtr
*prev
;
1233 for (prev
= head
; FcValueListPtrU(*prev
);
1234 prev
= &(FcValueListPtrU(*prev
)->next
))
1236 if (FcValueListPtrU(*prev
) == position
)
1238 *prev
= position
->next
;
1239 position
->next
= FcValueListPtrCreateDynamic(0);
1240 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1247 FcConfigPatternAdd (FcPattern
*p
,
1254 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1258 FcConfigAdd (&e
->values
, 0, append
, list
);
1263 * Delete all values associated with a field
1266 FcConfigPatternDel (FcPattern
*p
,
1269 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1272 while (FcValueListPtrU(e
->values
))
1273 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1277 FcConfigPatternCanon (FcPattern
*p
,
1280 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1283 if (!FcValueListPtrU(e
->values
))
1284 FcPatternDel (p
, object
);
1288 FcConfigSubstituteWithPat (FcConfig
*config
,
1303 config
= FcConfigGetCurrent ();
1308 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1309 if (!st
&& config
->maxObjects
)
1311 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1313 if (FcDebug () & FC_DBG_EDIT
)
1315 printf ("FcConfigSubstitute ");
1318 if (kind
== FcMatchPattern
)
1319 s
= config
->substPattern
;
1321 s
= config
->substFont
;
1322 for (; s
; s
= s
->next
)
1325 * Check the tests to see if
1326 * they all match the pattern
1328 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1330 if (FcDebug () & FC_DBG_EDIT
)
1332 printf ("FcConfigSubstitute test ");
1336 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1341 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1345 * If there's no such field in the font,
1346 * then FcQualAll matches while FcQualAny does not
1350 if (t
->qual
== FcQualAll
)
1359 * Check to see if there is a match, mark the location
1360 * to apply match-relative edits
1362 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1365 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1367 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1372 if (FcDebug () & FC_DBG_EDIT
)
1373 printf ("No match\n");
1376 if (FcDebug () & FC_DBG_EDIT
)
1378 printf ("Substitute ");
1381 for (e
= s
->edit
; e
; e
= e
->next
)
1384 * Evaluate the list of expressions
1386 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1388 * Locate any test associated with this field, skipping
1389 * tests associated with the pattern when substituting in
1392 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1394 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1395 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1396 (FcChar8
*) e
->field
))
1399 * KLUDGE - the pattern may have been reallocated or
1400 * things may have been inserted or deleted above
1401 * this element by other edits. Go back and find
1404 if (e
!= s
->edit
&& st
[i
].elt
)
1405 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1414 * If there was a test, then replace the matched
1415 * value with the new list of values
1419 FcValueList
*thisValue
= st
[i
].value
;
1420 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1423 * Append the new list of values after the current value
1425 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1427 * Delete the marked value
1429 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1431 * Adjust any pointers into the value list to ensure
1432 * future edits occur at the same place
1434 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1436 if (st
[i
].value
== thisValue
)
1437 st
[i
].value
= nextValue
;
1441 /* fall through ... */
1442 case FcOpAssignReplace
:
1444 * Delete all of the values and insert
1447 FcConfigPatternDel (p
, e
->field
);
1448 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1450 * Adjust any pointers into the value list as they no
1451 * longer point to anything valid
1455 FcPatternElt
*thisElt
= st
[i
].elt
;
1456 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1458 if (st
[i
].elt
== thisElt
)
1466 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1469 /* fall through ... */
1470 case FcOpPrependFirst
:
1471 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1476 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1479 /* fall through ... */
1480 case FcOpAppendLast
:
1481 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1488 * Now go through the pattern and eliminate
1489 * any properties without data
1491 for (e
= s
->edit
; e
; e
= e
->next
)
1492 FcConfigPatternCanon (p
, e
->field
);
1494 if (FcDebug () & FC_DBG_EDIT
)
1496 printf ("FcConfigSubstitute edit");
1500 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1502 if (FcDebug () & FC_DBG_EDIT
)
1504 printf ("FcConfigSubstitute done");
1511 FcConfigSubstitute (FcConfig
*config
,
1515 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1518 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1520 static FcChar8 fontconfig_path
[1000] = "";
1523 DllMain (HINSTANCE hinstDLL
,
1529 switch (fdwReason
) {
1530 case DLL_PROCESS_ATTACH
:
1531 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1532 sizeof (fontconfig_path
)))
1535 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1536 * assume it's a Unix-style installation tree, and use
1537 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1538 * folder where the DLL is as FONTCONFIG_PATH.
1540 p
= strrchr (fontconfig_path
, '\\');
1544 p
= strrchr (fontconfig_path
, '\\');
1545 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1546 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1548 strcat (fontconfig_path
, "\\etc\\fonts");
1551 fontconfig_path
[0] = '\0';
1559 #undef FONTCONFIG_PATH
1560 #define FONTCONFIG_PATH fontconfig_path
1562 #else /* !(_WIN32 && PIC) */
1564 #endif /* !(_WIN32 && PIC) */
1566 #ifndef FONTCONFIG_FILE
1567 #define FONTCONFIG_FILE "fonts.conf"
1571 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1576 dir
= (FcChar8
*) "";
1577 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1581 strcpy ((char *) path
, (const char *) dir
);
1582 /* make sure there's a single separator */
1584 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1585 path
[strlen((char *) path
)-1] != '\\')) &&
1588 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1589 strcat ((char *) path
, "\\");
1591 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1592 strcat ((char *) path
, "/");
1594 strcat ((char *) path
, (char *) file
);
1596 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1597 if (access ((char *) path
, R_OK
) == 0)
1605 FcConfigGetPath (void)
1608 FcChar8
*env
, *e
, *colon
;
1613 npath
= 2; /* default dir + null */
1614 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1620 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1623 path
= calloc (npath
, sizeof (FcChar8
*));
1633 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1635 colon
= e
+ strlen ((char *) e
);
1636 path
[i
] = malloc (colon
- e
+ 1);
1639 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1640 path
[i
][colon
- e
] = '\0';
1649 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1650 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1653 strcpy ((char *) path
[i
], (const char *) dir
);
1657 for (i
= 0; path
[i
]; i
++)
1665 FcConfigFreePath (FcChar8
**path
)
1669 for (p
= path
; *p
; p
++)
1674 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1679 if (_FcConfigHomeEnabled
)
1681 char *home
= getenv ("HOME");
1685 home
= getenv ("USERPROFILE");
1688 return (FcChar8
*) home
;
1694 FcConfigEnableHome (FcBool enable
)
1696 FcBool prev
= _FcConfigHomeEnabled
;
1697 _FcConfigHomeEnabled
= enable
;
1702 FcConfigFilename (const FcChar8
*url
)
1704 FcChar8
*file
, *dir
, **path
, **p
;
1708 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1710 url
= (FcChar8
*) FONTCONFIG_FILE
;
1715 if (isalpha (*url
) &&
1717 (url
[2] == '/' || url
[2] == '\\'))
1723 dir
= FcConfigHome ();
1725 file
= FcConfigFileExists (dir
, url
+ 1);
1734 file
= FcConfigFileExists (0, url
);
1737 path
= FcConfigGetPath ();
1740 for (p
= path
; *p
; p
++)
1742 file
= FcConfigFileExists (*p
, url
);
1746 FcConfigFreePath (path
);
1753 * Manage the application-specific fonts
1757 FcConfigAppFontAddFile (FcConfig
*config
,
1758 const FcChar8
*file
)
1767 config
= FcConfigGetCurrent ();
1772 subdirs
= FcStrSetCreate ();
1776 set
= FcConfigGetFonts (config
, FcSetApplication
);
1779 set
= FcFontSetCreate ();
1782 FcStrSetDestroy (subdirs
);
1785 FcConfigSetFonts (config
, set
, FcSetApplication
);
1788 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1790 FcStrSetDestroy (subdirs
);
1793 if ((sublist
= FcStrListCreate (subdirs
)))
1795 while ((subdir
= FcStrListNext (sublist
)))
1797 FcConfigAppFontAddDir (config
, subdir
);
1799 FcStrListDone (sublist
);
1805 FcConfigAppFontAddDir (FcConfig
*config
,
1815 config
= FcConfigGetCurrent ();
1819 subdirs
= FcStrSetCreate ();
1823 set
= FcConfigGetFonts (config
, FcSetApplication
);
1826 set
= FcFontSetCreate ();
1829 FcStrSetDestroy (subdirs
);
1832 FcConfigSetFonts (config
, set
, FcSetApplication
);
1835 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1837 FcStrSetDestroy (subdirs
);
1840 if ((sublist
= FcStrListCreate (subdirs
)))
1842 while ((subdir
= FcStrListNext (sublist
)))
1844 FcConfigAppFontAddDir (config
, subdir
);
1846 FcStrListDone (sublist
);
1852 FcConfigAppFontClear (FcConfig
*config
)
1856 config
= FcConfigGetCurrent ();
1861 FcConfigSetFonts (config
, 0, FcSetApplication
);
1865 * Manage filename-based font source selectors
1869 FcConfigGlobAdd (FcConfig
*config
,
1870 const FcChar8
*glob
,
1873 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1875 return FcStrSetAdd (set
, glob
);
1879 FcConfigGlobMatch (const FcChar8
*glob
,
1880 const FcChar8
*string
)
1884 while ((c
= *glob
++))
1888 /* short circuit common case */
1891 /* short circuit another common case */
1892 if (strchr ((char *) glob
, '*') == 0)
1893 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1896 if (FcConfigGlobMatch (glob
, string
))
1902 if (*string
++ == '\0')
1911 return *string
== '\0';
1915 FcConfigGlobsMatch (const FcStrSet
*globs
,
1916 const FcChar8
*string
)
1920 for (i
= 0; i
< globs
->num
; i
++)
1921 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1927 FcConfigAcceptFilename (FcConfig
*config
,
1928 const FcChar8
*filename
)
1930 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1932 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1938 * Manage font-pattern based font source selectors
1942 FcConfigPatternsAdd (FcConfig
*config
,
1946 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1948 return FcFontSetAdd (set
, pattern
);
1952 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1953 const FcPattern
*font
)
1957 for (i
= 0; i
< patterns
->nfont
; i
++)
1958 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1964 FcConfigAcceptFont (FcConfig
*config
,
1965 const FcPattern
*font
)
1967 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1969 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))