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
);
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 FcConfigAddDir (FcConfig
*config
,
395 return (FcConfigAddConfigDir (config
, d
) &&
396 FcConfigAddFontDir (config
, d
));
400 FcConfigGetFontDirs (FcConfig
*config
)
404 config
= FcConfigGetCurrent ();
408 return FcStrListCreate (config
->fontDirs
);
412 FcConfigAddConfigFile (FcConfig
*config
,
416 FcChar8
*file
= FcConfigFilename (f
);
421 ret
= FcStrSetAdd (config
->configFiles
, file
);
427 FcConfigGetConfigFiles (FcConfig
*config
)
431 config
= FcConfigGetCurrent ();
435 return FcStrListCreate (config
->configFiles
);
439 FcConfigSetCache (FcConfig
*config
,
442 FcChar8
*new = FcStrCopyFilename (c
);
447 FcStrFree (config
->cache
);
453 FcConfigGetCache (FcConfig
*config
)
457 config
= FcConfigGetCurrent ();
461 return config
->cache
;
465 FcConfigGetFonts (FcConfig
*config
,
470 config
= FcConfigGetCurrent ();
474 return config
->fonts
[set
];
478 FcConfigSetFonts (FcConfig
*config
,
482 if (config
->fonts
[set
])
483 FcFontSetDestroy (config
->fonts
[set
]);
484 config
->fonts
[set
] = fonts
;
490 FcConfigGetBlanks (FcConfig
*config
)
494 config
= FcConfigGetCurrent ();
498 return config
->blanks
;
502 FcConfigAddBlank (FcConfig
*config
,
510 b
= FcBlanksCreate ();
514 if (!FcBlanksAdd (b
, blank
))
521 FcConfigGetRescanInverval (FcConfig
*config
)
525 config
= FcConfigGetCurrent ();
529 return config
->rescanInterval
;
533 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
537 config
= FcConfigGetCurrent ();
541 config
->rescanInterval
= rescanInterval
;
546 FcConfigAddEdit (FcConfig
*config
,
551 FcSubst
*subst
, **prev
;
555 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
558 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
559 if (kind
== FcMatchPattern
)
560 prev
= &config
->substPattern
;
562 prev
= &config
->substFont
;
563 for (; *prev
; prev
= &(*prev
)->next
);
569 for (t
= test
; t
; t
= t
->next
)
571 if (t
->kind
== FcMatchDefault
)
575 if (config
->maxObjects
< num
)
576 config
->maxObjects
= num
;
577 if (FcDebug () & FC_DBG_EDIT
)
579 printf ("Add Subst ");
580 FcSubstPrint (subst
);
585 typedef struct _FcSubState
{
591 FcConfigPromote (FcValue v
, FcValue u
)
593 if (v
.type
== FcTypeInteger
)
595 v
.type
= FcTypeDouble
;
596 v
.u
.d
= (double) v
.u
.i
;
598 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
600 v
.u
.m
= &FcIdentityMatrix
;
601 v
.type
= FcTypeMatrix
;
603 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
605 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
606 v
.type
= FcTypeLangSet
;
612 FcConfigCompareValue (const FcValue
*left_o
,
614 const FcValue
*right_o
)
616 FcValue left
= FcValueCanonicalize(left_o
);
617 FcValue right
= FcValueCanonicalize(right_o
);
618 FcBool ret
= FcFalse
;
620 left
= FcConfigPromote (left
, right
);
621 right
= FcConfigPromote (right
, left
);
622 if (left
.type
== right
.type
)
626 break; /* FcConfigPromote prevents this from happening */
632 ret
= left
.u
.d
== right
.u
.d
;
635 case FcOpNotContains
:
636 ret
= left
.u
.d
!= right
.u
.d
;
639 ret
= left
.u
.d
< right
.u
.d
;
642 ret
= left
.u
.d
<= right
.u
.d
;
645 ret
= left
.u
.d
> right
.u
.d
;
648 ret
= left
.u
.d
>= right
.u
.d
;
659 ret
= left
.u
.b
== right
.u
.b
;
662 case FcOpNotContains
:
663 ret
= left
.u
.b
!= right
.u
.b
;
673 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
676 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
679 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
681 case FcOpNotContains
:
682 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
693 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
696 case FcOpNotContains
:
697 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
707 /* left contains right if right is a subset of left */
708 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
710 case FcOpNotContains
:
711 /* left contains right if right is a subset of left */
712 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
715 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
718 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
728 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
730 case FcOpNotContains
:
731 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
734 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
737 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
759 ret
= left
.u
.f
== right
.u
.f
;
762 case FcOpNotContains
:
763 ret
= left
.u
.f
!= right
.u
.f
;
773 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
780 #define _FcDoubleFloor(d) ((int) (d))
781 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
782 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
783 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
784 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
785 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
788 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
796 v
.type
= FcTypeInteger
;
800 v
.type
= FcTypeDouble
;
804 v
.type
= FcTypeString
;
805 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
809 v
.type
= FcTypeMatrix
;
814 v
.type
= FcTypeCharSet
;
823 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
824 if (r
!= FcResultMatch
)
829 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
830 v
.type
= FcTypeInteger
;
835 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
836 if (vl
.type
== FcTypeBool
)
839 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
841 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
854 case FcOpNotContains
:
856 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
857 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
859 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
869 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
870 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
871 vl
= FcConfigPromote (vl
, vr
);
872 vr
= FcConfigPromote (vr
, vl
);
873 if (vl
.type
== vr
.type
)
879 v
.type
= FcTypeDouble
;
880 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
883 v
.type
= FcTypeDouble
;
884 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
887 v
.type
= FcTypeDouble
;
888 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
891 v
.type
= FcTypeDouble
;
892 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
898 if (v
.type
== FcTypeDouble
&&
899 v
.u
.d
== (double) (int) v
.u
.d
)
901 v
.type
= FcTypeInteger
;
909 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
913 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
923 v
.type
= FcTypeString
;
924 v
.u
.s
= FcStrStaticName (FcStrPlus (vl
.u
.s
, vr
.u
.s
));
937 v
.type
= FcTypeMatrix
;
938 m
= malloc (sizeof (FcMatrix
));
941 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
942 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
966 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
979 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
985 v
.type
= FcTypeInteger
;
986 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
995 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1001 v
.type
= FcTypeInteger
;
1002 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1005 v
.type
= FcTypeVoid
;
1008 FcValueDestroy (vl
);
1011 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1017 v
.type
= FcTypeInteger
;
1018 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1021 v
.type
= FcTypeVoid
;
1024 FcValueDestroy (vl
);
1027 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1033 v
.type
= FcTypeInteger
;
1034 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1037 v
.type
= FcTypeVoid
;
1040 FcValueDestroy (vl
);
1043 v
.type
= FcTypeVoid
;
1049 static FcValueList
*
1050 FcConfigMatchValueList (FcPattern
*p
,
1052 FcValueList
*values
)
1054 FcValueList
*ret
= 0;
1055 FcExpr
*e
= t
->expr
;
1061 /* Compute the value of the match expression */
1062 if (e
->op
== FcOpComma
)
1064 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1065 e
= e
->u
.tree
.right
;
1069 value
= FcConfigEvaluate (p
, e
);
1073 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1075 /* Compare the pattern value to the match expression value */
1076 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1083 if (t
->qual
== FcQualAll
)
1090 FcValueDestroy (value
);
1095 static FcValueList
*
1096 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1102 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1105 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1106 if (e
->op
== FcOpComma
)
1108 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1109 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1113 l
->value
= FcConfigEvaluate (p
, e
);
1114 l
->next
= FcValueListPtrCreateDynamic(0);
1116 l
->binding
= binding
;
1117 if (l
->value
.type
== FcTypeVoid
)
1119 FcValueList
*next
= FcValueListPtrU(l
->next
);
1121 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1130 FcConfigAdd (FcValueListPtr
*head
,
1131 FcValueList
*position
,
1135 FcValueListPtr
*prev
, last
, v
;
1136 FcValueBinding sameBinding
;
1139 sameBinding
= position
->binding
;
1141 sameBinding
= FcValueBindingWeak
;
1142 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1143 v
= FcValueListPtrU(v
)->next
)
1144 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1145 FcValueListPtrU(v
)->binding
= sameBinding
;
1149 prev
= &position
->next
;
1151 for (prev
= head
; FcValueListPtrU(*prev
);
1152 prev
= &(FcValueListPtrU(*prev
)->next
))
1159 for (prev
= head
; FcValueListPtrU(*prev
);
1160 prev
= &(FcValueListPtrU(*prev
)->next
))
1162 if (FcValueListPtrU(*prev
) == position
)
1169 if (FcDebug () & FC_DBG_EDIT
)
1171 if (!FcValueListPtrU(*prev
))
1172 printf ("position not on list\n");
1176 if (FcDebug () & FC_DBG_EDIT
)
1178 printf ("%s list before ", append
? "Append" : "Prepend");
1179 FcValueListPrint (*head
);
1185 last
= FcValueListPtrCreateDynamic(new);
1186 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1187 last
= FcValueListPtrU(last
)->next
;
1189 FcValueListPtrU(last
)->next
= *prev
;
1190 *prev
= FcValueListPtrCreateDynamic(new);
1193 if (FcDebug () & FC_DBG_EDIT
)
1195 printf ("%s list after ", append
? "Append" : "Prepend");
1196 FcValueListPrint (*head
);
1204 FcConfigDel (FcValueListPtr
*head
,
1205 FcValueList
*position
)
1207 FcValueListPtr
*prev
;
1209 for (prev
= head
; FcValueListPtrU(*prev
);
1210 prev
= &(FcValueListPtrU(*prev
)->next
))
1212 if (FcValueListPtrU(*prev
) == position
)
1214 *prev
= position
->next
;
1215 position
->next
= FcValueListPtrCreateDynamic(0);
1216 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1223 FcConfigPatternAdd (FcPattern
*p
,
1230 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1234 FcConfigAdd (&e
->values
, 0, append
, list
);
1239 * Delete all values associated with a field
1242 FcConfigPatternDel (FcPattern
*p
,
1245 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1248 while (FcValueListPtrU(e
->values
))
1249 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1253 FcConfigPatternCanon (FcPattern
*p
,
1256 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1259 if (!FcValueListPtrU(e
->values
))
1260 FcPatternDel (p
, object
);
1264 FcConfigSubstituteWithPat (FcConfig
*config
,
1279 config
= FcConfigGetCurrent ();
1284 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1285 if (!st
&& config
->maxObjects
)
1287 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1289 if (FcDebug () & FC_DBG_EDIT
)
1291 printf ("FcConfigSubstitute ");
1294 if (kind
== FcMatchPattern
)
1295 s
= config
->substPattern
;
1297 s
= config
->substFont
;
1298 for (; s
; s
= s
->next
)
1301 * Check the tests to see if
1302 * they all match the pattern
1304 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1306 if (FcDebug () & FC_DBG_EDIT
)
1308 printf ("FcConfigSubstitute test ");
1312 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1317 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1321 * If there's no such field in the font,
1322 * then FcQualAll matches while FcQualAny does not
1326 if (t
->qual
== FcQualAll
)
1335 * Check to see if there is a match, mark the location
1336 * to apply match-relative edits
1338 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1341 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1343 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1348 if (FcDebug () & FC_DBG_EDIT
)
1349 printf ("No match\n");
1352 if (FcDebug () & FC_DBG_EDIT
)
1354 printf ("Substitute ");
1357 for (e
= s
->edit
; e
; e
= e
->next
)
1360 * Evaluate the list of expressions
1362 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1364 * Locate any test associated with this field, skipping
1365 * tests associated with the pattern when substituting in
1368 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1370 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1371 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1372 (FcChar8
*) e
->field
))
1375 * KLUDGE - the pattern may have been reallocated or
1376 * things may have been inserted or deleted above
1377 * this element by other edits. Go back and find
1380 if (e
!= s
->edit
&& st
[i
].elt
)
1381 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1390 * If there was a test, then replace the matched
1391 * value with the new list of values
1395 FcValueList
*thisValue
= st
[i
].value
;
1396 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1399 * Append the new list of values after the current value
1401 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1403 * Delete the marked value
1405 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1407 * Adjust any pointers into the value list to ensure
1408 * future edits occur at the same place
1410 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1412 if (st
[i
].value
== thisValue
)
1413 st
[i
].value
= nextValue
;
1417 /* fall through ... */
1418 case FcOpAssignReplace
:
1420 * Delete all of the values and insert
1423 FcConfigPatternDel (p
, e
->field
);
1424 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1426 * Adjust any pointers into the value list as they no
1427 * longer point to anything valid
1431 FcPatternElt
*thisElt
= st
[i
].elt
;
1432 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1434 if (st
[i
].elt
== thisElt
)
1442 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1445 /* fall through ... */
1446 case FcOpPrependFirst
:
1447 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1452 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1455 /* fall through ... */
1456 case FcOpAppendLast
:
1457 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1464 * Now go through the pattern and eliminate
1465 * any properties without data
1467 for (e
= s
->edit
; e
; e
= e
->next
)
1468 FcConfigPatternCanon (p
, e
->field
);
1470 if (FcDebug () & FC_DBG_EDIT
)
1472 printf ("FcConfigSubstitute edit");
1476 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1478 if (FcDebug () & FC_DBG_EDIT
)
1480 printf ("FcConfigSubstitute done");
1487 FcConfigSubstitute (FcConfig
*config
,
1491 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1494 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1496 static FcChar8 fontconfig_path
[1000] = "";
1499 DllMain (HINSTANCE hinstDLL
,
1505 switch (fdwReason
) {
1506 case DLL_PROCESS_ATTACH
:
1507 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1508 sizeof (fontconfig_path
)))
1511 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1512 * assume it's a Unix-style installation tree, and use
1513 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1514 * folder where the DLL is as FONTCONFIG_PATH.
1516 p
= strrchr (fontconfig_path
, '\\');
1520 p
= strrchr (fontconfig_path
, '\\');
1521 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1522 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1524 strcat (fontconfig_path
, "\\etc\\fonts");
1527 fontconfig_path
[0] = '\0';
1535 #undef FONTCONFIG_PATH
1536 #define FONTCONFIG_PATH fontconfig_path
1538 #else /* !(_WIN32 && PIC) */
1540 #endif /* !(_WIN32 && PIC) */
1542 #ifndef FONTCONFIG_FILE
1543 #define FONTCONFIG_FILE "fonts.conf"
1547 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1552 dir
= (FcChar8
*) "";
1553 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1557 strcpy ((char *) path
, (const char *) dir
);
1558 /* make sure there's a single separator */
1560 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1561 path
[strlen((char *) path
)-1] != '\\')) &&
1564 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1565 strcat ((char *) path
, "\\");
1567 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1568 strcat ((char *) path
, "/");
1570 strcat ((char *) path
, (char *) file
);
1572 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1573 if (access ((char *) path
, R_OK
) == 0)
1581 FcConfigGetPath (void)
1584 FcChar8
*env
, *e
, *colon
;
1589 npath
= 2; /* default dir + null */
1590 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1596 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1599 path
= calloc (npath
, sizeof (FcChar8
*));
1609 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1611 colon
= e
+ strlen ((char *) e
);
1612 path
[i
] = malloc (colon
- e
+ 1);
1615 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1616 path
[i
][colon
- e
] = '\0';
1625 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1626 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1629 strcpy ((char *) path
[i
], (const char *) dir
);
1633 for (i
= 0; path
[i
]; i
++)
1641 FcConfigFreePath (FcChar8
**path
)
1645 for (p
= path
; *p
; p
++)
1650 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1655 if (_FcConfigHomeEnabled
)
1657 char *home
= getenv ("HOME");
1661 home
= getenv ("USERPROFILE");
1664 return (FcChar8
*) home
;
1670 FcConfigEnableHome (FcBool enable
)
1672 FcBool prev
= _FcConfigHomeEnabled
;
1673 _FcConfigHomeEnabled
= enable
;
1678 FcConfigFilename (const FcChar8
*url
)
1680 FcChar8
*file
, *dir
, **path
, **p
;
1684 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1686 url
= (FcChar8
*) FONTCONFIG_FILE
;
1691 if (isalpha (*url
) &&
1693 (url
[2] == '/' || url
[2] == '\\'))
1699 dir
= FcConfigHome ();
1701 file
= FcConfigFileExists (dir
, url
+ 1);
1710 file
= FcConfigFileExists (0, url
);
1713 path
= FcConfigGetPath ();
1716 for (p
= path
; *p
; p
++)
1718 file
= FcConfigFileExists (*p
, url
);
1722 FcConfigFreePath (path
);
1729 * Manage the application-specific fonts
1733 FcConfigAppFontAddFile (FcConfig
*config
,
1734 const FcChar8
*file
)
1743 config
= FcConfigGetCurrent ();
1748 subdirs
= FcStrSetCreate ();
1752 set
= FcConfigGetFonts (config
, FcSetApplication
);
1755 set
= FcFontSetCreate ();
1758 FcStrSetDestroy (subdirs
);
1761 FcConfigSetFonts (config
, set
, FcSetApplication
);
1764 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1766 FcStrSetDestroy (subdirs
);
1769 if ((sublist
= FcStrListCreate (subdirs
)))
1771 while ((subdir
= FcStrListNext (sublist
)))
1773 FcConfigAppFontAddDir (config
, subdir
);
1775 FcStrListDone (sublist
);
1781 FcConfigAppFontAddDir (FcConfig
*config
,
1791 config
= FcConfigGetCurrent ();
1795 subdirs
= FcStrSetCreate ();
1799 set
= FcConfigGetFonts (config
, FcSetApplication
);
1802 set
= FcFontSetCreate ();
1805 FcStrSetDestroy (subdirs
);
1808 FcConfigSetFonts (config
, set
, FcSetApplication
);
1811 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1813 FcStrSetDestroy (subdirs
);
1816 if ((sublist
= FcStrListCreate (subdirs
)))
1818 while ((subdir
= FcStrListNext (sublist
)))
1820 FcConfigAppFontAddDir (config
, subdir
);
1822 FcStrListDone (sublist
);
1828 FcConfigAppFontClear (FcConfig
*config
)
1832 config
= FcConfigGetCurrent ();
1837 FcConfigSetFonts (config
, 0, FcSetApplication
);
1841 * Manage filename-based font source selectors
1845 FcConfigGlobAdd (FcConfig
*config
,
1846 const FcChar8
*glob
,
1849 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1851 return FcStrSetAdd (set
, glob
);
1855 FcConfigGlobMatch (const FcChar8
*glob
,
1856 const FcChar8
*string
)
1860 while ((c
= *glob
++))
1864 /* short circuit common case */
1867 /* short circuit another common case */
1868 if (strchr ((char *) glob
, '*') == 0)
1869 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1872 if (FcConfigGlobMatch (glob
, string
))
1878 if (*string
++ == '\0')
1887 return *string
== '\0';
1891 FcConfigGlobsMatch (const FcStrSet
*globs
,
1892 const FcChar8
*string
)
1896 for (i
= 0; i
< globs
->num
; i
++)
1897 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1903 FcConfigAcceptFilename (FcConfig
*config
,
1904 const FcChar8
*filename
)
1906 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1908 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1914 * Manage font-pattern based font source selectors
1918 FcConfigPatternsAdd (FcConfig
*config
,
1922 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1924 return FcFontSetAdd (set
, pattern
);
1928 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1929 const FcPattern
*font
)
1933 for (i
= 0; i
< patterns
->nfont
; i
++)
1934 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1940 FcConfigAcceptFont (FcConfig
*config
,
1941 const FcPattern
*font
)
1943 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1945 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))