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 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config
= malloc (sizeof (FcConfig
));
50 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
52 config
->configDirs
= FcStrSetCreate ();
53 if (!config
->configDirs
)
56 config
->configFiles
= FcStrSetCreate ();
57 if (!config
->configFiles
)
60 config
->fontDirs
= FcStrSetCreate ();
61 if (!config
->fontDirs
)
64 config
->acceptGlobs
= FcStrSetCreate ();
65 if (!config
->acceptGlobs
)
68 config
->rejectGlobs
= FcStrSetCreate ();
69 if (!config
->rejectGlobs
)
72 config
->acceptPatterns
= FcFontSetCreate ();
73 if (!config
->acceptPatterns
)
76 config
->rejectPatterns
= FcFontSetCreate ();
77 if (!config
->rejectPatterns
)
82 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
86 if (config
->cache
== 0)
88 /* If no home, use the temp folder. */
90 int templen
= GetTempPath (1, dummy
);
91 FcChar8
*temp
= malloc (templen
+ 1);
97 GetTempPath (templen
+ 1, temp
);
98 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
100 if (!FcConfigSetCache (config
, cache_dir
))
102 FcStrFree (cache_dir
);
105 FcStrFree (cache_dir
);
110 config
->cacheDirs
= FcStrSetCreate ();
111 if (!config
->cacheDirs
)
116 config
->substPattern
= 0;
117 config
->substFont
= 0;
118 config
->maxObjects
= 0;
119 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
120 config
->fonts
[set
] = 0;
122 config
->rescanTime
= time(0);
123 config
->rescanInterval
= 30;
128 FcStrFree (config
->cache
);
130 FcFontSetDestroy (config
->rejectPatterns
);
132 FcFontSetDestroy (config
->acceptPatterns
);
134 FcStrSetDestroy (config
->rejectGlobs
);
136 FcStrSetDestroy (config
->acceptGlobs
);
138 FcStrSetDestroy (config
->fontDirs
);
140 FcStrSetDestroy (config
->configFiles
);
142 FcStrSetDestroy (config
->configDirs
);
145 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
151 FcConfigNewestFile (FcStrSet
*files
)
153 FcStrList
*list
= FcStrListCreate (files
);
154 FcFileTime newest
= { 0, FcFalse
};
160 while ((file
= FcStrListNext (list
)))
161 if (stat ((char *) file
, &statb
) == 0)
162 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
165 newest
.time
= statb
.st_mtime
;
167 FcStrListDone (list
);
173 FcConfigModifiedTime (FcConfig
*config
)
177 FcFileTime v
= { 0, FcFalse
};
178 config
= FcConfigGetCurrent ();
182 return FcConfigNewestFile (config
->configFiles
);
186 FcConfigUptoDate (FcConfig
*config
)
188 FcFileTime config_time
, font_time
;
189 time_t now
= time(0);
192 config
= FcConfigGetCurrent ();
196 config_time
= FcConfigNewestFile (config
->configFiles
);
197 font_time
= FcConfigNewestFile (config
->fontDirs
);
198 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
199 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
203 config
->rescanTime
= now
;
208 FcSubstDestroy (FcSubst
*s
)
216 FcTestDestroy (s
->test
);
218 FcEditDestroy (s
->edit
);
220 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
226 FcConfigDestroy (FcConfig
*config
)
230 if (config
== _fcConfig
)
233 FcStrSetDestroy (config
->configDirs
);
234 FcStrSetDestroy (config
->fontDirs
);
235 FcStrSetDestroy (config
->cacheDirs
);
236 FcStrSetDestroy (config
->configFiles
);
237 FcStrSetDestroy (config
->acceptGlobs
);
238 FcStrSetDestroy (config
->rejectGlobs
);
239 FcFontSetDestroy (config
->acceptPatterns
);
240 FcFontSetDestroy (config
->rejectPatterns
);
243 FcBlanksDestroy (config
->blanks
);
246 FcStrFree (config
->cache
);
248 FcSubstDestroy (config
->substPattern
);
249 FcSubstDestroy (config
->substFont
);
250 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
251 if (config
->fonts
[set
])
252 FcFontSetDestroy (config
->fonts
[set
]);
255 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
259 * Scan the current list of directories in the configuration
260 * and build the set of available fonts. Update the
261 * per-user cache file to reflect the new configuration
265 FcConfigBuildFonts (FcConfig
*config
)
267 FcFontSet
*fonts
, *cached_fonts
;
268 FcGlobalCache
*cache
;
273 fonts
= FcFontSetCreate ();
277 cache
= FcGlobalCacheCreate ();
281 oldDirs
= FcStrSetCreate ();
286 FcGlobalCacheLoad (cache
, oldDirs
, config
->cache
, config
);
288 cached_fonts
= FcCacheRead(config
, cache
);
291 list
= FcConfigGetFontDirs (config
);
295 while ((dir
= FcStrListNext (list
)))
297 if (FcDebug () & FC_DBG_FONTSET
)
298 printf ("build scan dir %s\n", dir
);
299 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
300 config
->blanks
, dir
, FcFalse
, config
);
303 FcStrListDone (list
);
309 for (i
= 0; i
< oldDirs
->num
; i
++)
311 if (FcDebug () & FC_DBG_FONTSET
)
312 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
313 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
314 config
->blanks
, oldDirs
->strs
[i
],
318 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
321 FcPatternGetString (cached_fonts
->fonts
[i
], FC_FILE
, 0, &cfn
);
323 if (FcConfigAcceptFont (config
, cached_fonts
->fonts
[i
]) &&
324 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
325 FcFontSetAdd (fonts
, cached_fonts
->fonts
[i
]);
327 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
329 cached_fonts
->nfont
= 0;
330 FcFontSetDestroy (cached_fonts
);
333 if (FcDebug () & FC_DBG_FONTSET
)
334 FcFontSetPrint (fonts
);
337 FcGlobalCacheSave (cache
, config
->cache
, config
);
338 FcGlobalCacheDestroy (cache
);
339 FcStrSetDestroy (oldDirs
);
341 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
345 FcStrSetDestroy (oldDirs
);
347 FcGlobalCacheDestroy (cache
);
349 FcFontSetDestroy (fonts
);
355 FcConfigSetCurrent (FcConfig
*config
)
358 if (!FcConfigBuildFonts (config
))
362 FcConfigDestroy (_fcConfig
);
368 FcConfigGetCurrent (void)
377 FcConfigAddConfigDir (FcConfig
*config
,
380 return FcStrSetAddFilename (config
->configDirs
, d
);
384 FcConfigGetConfigDirs (FcConfig
*config
)
388 config
= FcConfigGetCurrent ();
392 return FcStrListCreate (config
->configDirs
);
396 FcConfigInodeMatchFontDir (FcConfig
*config
, const FcChar8
*d
)
403 /* first we do string matches rather than file accesses */
404 /* FcStrSetMember doesn't tell us the index so that we can return
405 * the config-owned copy. */
406 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
408 if (!FcStrCmp (config
->fontDirs
->strs
[n
], d
))
409 return config
->fontDirs
->strs
[n
];
412 /* If this is a bottleneck, we can cache the fontDir inodes. */
413 if (stat ((char *)d
, &s
) == -1)
415 di
= s
.st_ino
; dd
= s
.st_dev
;
417 for (n
= 0; n
< config
->fontDirs
->num
; n
++)
419 if (stat ((char *)config
->fontDirs
->strs
[n
], &s
) == -1)
421 if (di
== s
.st_ino
&& dd
== s
.st_dev
)
422 return config
->fontDirs
->strs
[n
];
428 FcConfigAddFontDir (FcConfig
*config
,
431 /* Avoid adding d if it's an alias of something else, too. */
432 if (FcConfigInodeMatchFontDir(config
, d
))
434 return FcStrSetAddFilename (config
->fontDirs
, d
);
438 FcConfigAddFontDirSubdirs (FcConfig
*config
,
444 FcBool added
= FcFalse
;
446 if (!(dir
= opendir ((char *) d
)))
448 if (!(subdir
= (FcChar8
*) malloc (strlen ((char *) d
) + FC_MAX_FILE_LEN
+ 2)))
450 fprintf (stderr
, "out of memory");
453 while ((e
= readdir (dir
)))
455 if (strcmp (e
->d_name
, ".") && strcmp (e
->d_name
, "..") &&
456 strlen (e
->d_name
) < FC_MAX_FILE_LEN
)
458 strcpy ((char *)subdir
, (char *)d
);
459 strcat ((char *)subdir
, "/");
460 strcat ((char *)subdir
, e
->d_name
);
461 if (FcFileIsDir (subdir
))
463 if (FcConfigInodeMatchFontDir(config
, subdir
))
464 continue; /* already added */
465 FcStrSetAddFilename (config
->fontDirs
, subdir
);
466 FcConfigAddFontDirSubdirs (config
, subdir
);
477 FcConfigNormalizeFontDir (FcConfig
*config
,
482 FcBool added
= FcFalse
;
484 d0
= FcConfigInodeMatchFontDir(config
, d
);
488 /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
489 for (n
= 0, n0
= config
->fontDirs
->num
; n
< n0
; n
++)
491 if (FcConfigAddFontDirSubdirs (config
, config
->fontDirs
->strs
[n
]))
495 /* ... and try again. */
497 return FcConfigInodeMatchFontDir(config
, d
);
503 FcConfigAddDir (FcConfig
*config
,
506 return (FcConfigAddConfigDir (config
, d
) &&
507 FcConfigAddFontDir (config
, d
));
511 FcConfigGetFontDirs (FcConfig
*config
)
515 config
= FcConfigGetCurrent ();
519 return FcStrListCreate (config
->fontDirs
);
523 FcConfigAddCacheDir (FcConfig
*config
,
526 return FcStrSetAddFilename (config
->cacheDirs
, d
);
530 FcConfigGetCacheDirs (FcConfig
*config
)
534 config
= FcConfigGetCurrent ();
538 return FcStrListCreate (config
->cacheDirs
);
542 FcConfigAddConfigFile (FcConfig
*config
,
546 FcChar8
*file
= FcConfigFilename (f
);
551 ret
= FcStrSetAdd (config
->configFiles
, file
);
557 FcConfigGetConfigFiles (FcConfig
*config
)
561 config
= FcConfigGetCurrent ();
565 return FcStrListCreate (config
->configFiles
);
569 FcConfigSetCache (FcConfig
*config
,
572 FcChar8
*new = FcStrCopyFilename (c
);
577 FcStrFree (config
->cache
);
583 FcConfigGetCache (FcConfig
*config
)
587 config
= FcConfigGetCurrent ();
591 return config
->cache
;
595 FcConfigGetFonts (FcConfig
*config
,
600 config
= FcConfigGetCurrent ();
604 return config
->fonts
[set
];
608 FcConfigSetFonts (FcConfig
*config
,
612 if (config
->fonts
[set
])
613 FcFontSetDestroy (config
->fonts
[set
]);
614 config
->fonts
[set
] = fonts
;
620 FcConfigGetBlanks (FcConfig
*config
)
624 config
= FcConfigGetCurrent ();
628 return config
->blanks
;
632 FcConfigAddBlank (FcConfig
*config
,
635 FcBlanks
*b
, *freeme
= 0;
640 freeme
= b
= FcBlanksCreate ();
644 if (!FcBlanksAdd (b
, blank
))
647 FcBlanksDestroy (freeme
);
655 FcConfigGetRescanInverval (FcConfig
*config
)
659 config
= FcConfigGetCurrent ();
663 return config
->rescanInterval
;
667 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
671 config
= FcConfigGetCurrent ();
675 config
->rescanInterval
= rescanInterval
;
680 FcConfigAddEdit (FcConfig
*config
,
685 FcSubst
*subst
, **prev
;
689 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
692 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
693 if (kind
== FcMatchPattern
)
694 prev
= &config
->substPattern
;
696 prev
= &config
->substFont
;
697 for (; *prev
; prev
= &(*prev
)->next
);
703 for (t
= test
; t
; t
= t
->next
)
705 if (t
->kind
== FcMatchDefault
)
709 if (config
->maxObjects
< num
)
710 config
->maxObjects
= num
;
711 if (FcDebug () & FC_DBG_EDIT
)
713 printf ("Add Subst ");
714 FcSubstPrint (subst
);
719 typedef struct _FcSubState
{
725 FcConfigPromote (FcValue v
, FcValue u
)
727 if (v
.type
== FcTypeInteger
)
729 v
.type
= FcTypeDouble
;
730 v
.u
.d
= (double) v
.u
.i
;
732 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
734 v
.u
.m
= &FcIdentityMatrix
;
735 v
.type
= FcTypeMatrix
;
737 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
739 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
740 v
.type
= FcTypeLangSet
;
746 FcConfigCompareValue (const FcValue
*left_o
,
748 const FcValue
*right_o
)
750 FcValue left
= FcValueCanonicalize(left_o
);
751 FcValue right
= FcValueCanonicalize(right_o
);
752 FcBool ret
= FcFalse
;
754 left
= FcConfigPromote (left
, right
);
755 right
= FcConfigPromote (right
, left
);
756 if (left
.type
== right
.type
)
760 break; /* FcConfigPromote prevents this from happening */
766 ret
= left
.u
.d
== right
.u
.d
;
769 case FcOpNotContains
:
770 ret
= left
.u
.d
!= right
.u
.d
;
773 ret
= left
.u
.d
< right
.u
.d
;
776 ret
= left
.u
.d
<= right
.u
.d
;
779 ret
= left
.u
.d
> right
.u
.d
;
782 ret
= left
.u
.d
>= right
.u
.d
;
793 ret
= left
.u
.b
== right
.u
.b
;
796 case FcOpNotContains
:
797 ret
= left
.u
.b
!= right
.u
.b
;
807 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
810 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
813 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
815 case FcOpNotContains
:
816 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
827 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
830 case FcOpNotContains
:
831 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
841 /* left contains right if right is a subset of left */
842 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
844 case FcOpNotContains
:
845 /* left contains right if right is a subset of left */
846 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
849 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
852 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
862 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
864 case FcOpNotContains
:
865 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
868 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
871 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
893 ret
= left
.u
.f
== right
.u
.f
;
896 case FcOpNotContains
:
897 ret
= left
.u
.f
!= right
.u
.f
;
907 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
914 #define _FcDoubleFloor(d) ((int) (d))
915 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
916 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
917 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
918 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
919 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
922 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
931 v
.type
= FcTypeInteger
;
935 v
.type
= FcTypeDouble
;
939 v
.type
= FcTypeString
;
940 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
943 v
.type
= FcTypeMatrix
;
948 v
.type
= FcTypeCharSet
;
957 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
958 if (r
!= FcResultMatch
)
963 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
964 v
.type
= FcTypeInteger
;
969 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
970 if (vl
.type
== FcTypeBool
)
973 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
975 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
988 case FcOpNotContains
:
990 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
991 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
993 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
1003 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1004 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
1005 vl
= FcConfigPromote (vl
, vr
);
1006 vr
= FcConfigPromote (vr
, vl
);
1007 if (vl
.type
== vr
.type
)
1013 v
.type
= FcTypeDouble
;
1014 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
1017 v
.type
= FcTypeDouble
;
1018 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
1021 v
.type
= FcTypeDouble
;
1022 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
1025 v
.type
= FcTypeDouble
;
1026 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
1029 v
.type
= FcTypeVoid
;
1032 if (v
.type
== FcTypeDouble
&&
1033 v
.u
.d
== (double) (int) v
.u
.d
)
1035 v
.type
= FcTypeInteger
;
1036 v
.u
.i
= (int) v
.u
.d
;
1042 v
.type
= FcTypeBool
;
1043 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
1046 v
.type
= FcTypeBool
;
1047 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
1050 v
.type
= FcTypeVoid
;
1057 v
.type
= FcTypeString
;
1058 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
1059 v
.u
.s
= FcStrStaticName (str
);
1063 v
.type
= FcTypeVoid
;
1066 v
.type
= FcTypeVoid
;
1073 v
.type
= FcTypeMatrix
;
1074 m
= malloc (sizeof (FcMatrix
));
1077 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
1078 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1083 v
.type
= FcTypeVoid
;
1087 v
.type
= FcTypeVoid
;
1092 v
.type
= FcTypeVoid
;
1097 v
.type
= FcTypeVoid
;
1098 FcValueDestroy (vl
);
1099 FcValueDestroy (vr
);
1102 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1105 v
.type
= FcTypeBool
;
1109 v
.type
= FcTypeVoid
;
1112 FcValueDestroy (vl
);
1115 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1121 v
.type
= FcTypeInteger
;
1122 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1125 v
.type
= FcTypeVoid
;
1128 FcValueDestroy (vl
);
1131 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1137 v
.type
= FcTypeInteger
;
1138 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1141 v
.type
= FcTypeVoid
;
1144 FcValueDestroy (vl
);
1147 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1153 v
.type
= FcTypeInteger
;
1154 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1157 v
.type
= FcTypeVoid
;
1160 FcValueDestroy (vl
);
1163 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1169 v
.type
= FcTypeInteger
;
1170 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1173 v
.type
= FcTypeVoid
;
1176 FcValueDestroy (vl
);
1179 v
.type
= FcTypeVoid
;
1185 static FcValueList
*
1186 FcConfigMatchValueList (FcPattern
*p
,
1188 FcValueList
*values
)
1190 FcValueList
*ret
= 0;
1191 FcExpr
*e
= t
->expr
;
1197 /* Compute the value of the match expression */
1198 if (e
->op
== FcOpComma
)
1200 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1201 e
= e
->u
.tree
.right
;
1205 value
= FcConfigEvaluate (p
, e
);
1209 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1211 /* Compare the pattern value to the match expression value */
1212 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1219 if (t
->qual
== FcQualAll
)
1226 FcValueDestroy (value
);
1231 static FcValueList
*
1232 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1238 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1241 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1242 if (e
->op
== FcOpComma
)
1244 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1245 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1249 l
->value
= FcConfigEvaluate (p
, e
);
1250 l
->next
= FcValueListPtrCreateDynamic(0);
1252 l
->binding
= binding
;
1253 if (l
->value
.type
== FcTypeVoid
)
1255 FcValueList
*next
= FcValueListPtrU(l
->next
);
1257 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1266 FcConfigAdd (FcValueListPtr
*head
,
1267 FcValueList
*position
,
1271 FcValueListPtr
*prev
, last
, v
;
1272 FcValueBinding sameBinding
;
1275 sameBinding
= position
->binding
;
1277 sameBinding
= FcValueBindingWeak
;
1278 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1279 v
= FcValueListPtrU(v
)->next
)
1280 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1281 FcValueListPtrU(v
)->binding
= sameBinding
;
1285 prev
= &position
->next
;
1287 for (prev
= head
; FcValueListPtrU(*prev
);
1288 prev
= &(FcValueListPtrU(*prev
)->next
))
1295 for (prev
= head
; FcValueListPtrU(*prev
);
1296 prev
= &(FcValueListPtrU(*prev
)->next
))
1298 if (FcValueListPtrU(*prev
) == position
)
1305 if (FcDebug () & FC_DBG_EDIT
)
1307 if (!FcValueListPtrU(*prev
))
1308 printf ("position not on list\n");
1312 if (FcDebug () & FC_DBG_EDIT
)
1314 printf ("%s list before ", append
? "Append" : "Prepend");
1315 FcValueListPrint (*head
);
1321 last
= FcValueListPtrCreateDynamic(new);
1322 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1323 last
= FcValueListPtrU(last
)->next
;
1325 FcValueListPtrU(last
)->next
= *prev
;
1326 *prev
= FcValueListPtrCreateDynamic(new);
1329 if (FcDebug () & FC_DBG_EDIT
)
1331 printf ("%s list after ", append
? "Append" : "Prepend");
1332 FcValueListPrint (*head
);
1340 FcConfigDel (FcValueListPtr
*head
,
1341 FcValueList
*position
)
1343 FcValueListPtr
*prev
;
1345 for (prev
= head
; FcValueListPtrU(*prev
);
1346 prev
= &(FcValueListPtrU(*prev
)->next
))
1348 if (FcValueListPtrU(*prev
) == position
)
1350 *prev
= position
->next
;
1351 position
->next
= FcValueListPtrCreateDynamic(0);
1352 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1359 FcConfigPatternAdd (FcPattern
*p
,
1366 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1370 FcConfigAdd (&e
->values
, 0, append
, list
);
1375 * Delete all values associated with a field
1378 FcConfigPatternDel (FcPattern
*p
,
1381 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1384 while (FcValueListPtrU(e
->values
))
1385 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1389 FcConfigPatternCanon (FcPattern
*p
,
1392 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1395 if (!FcValueListPtrU(e
->values
))
1396 FcPatternDel (p
, object
);
1400 FcConfigSubstituteWithPat (FcConfig
*config
,
1415 config
= FcConfigGetCurrent ();
1420 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1421 if (!st
&& config
->maxObjects
)
1423 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1425 if (FcDebug () & FC_DBG_EDIT
)
1427 printf ("FcConfigSubstitute ");
1430 if (kind
== FcMatchPattern
)
1431 s
= config
->substPattern
;
1433 s
= config
->substFont
;
1434 for (; s
; s
= s
->next
)
1437 * Check the tests to see if
1438 * they all match the pattern
1440 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1442 if (FcDebug () & FC_DBG_EDIT
)
1444 printf ("FcConfigSubstitute test ");
1448 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1453 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1457 * If there's no such field in the font,
1458 * then FcQualAll matches while FcQualAny does not
1462 if (t
->qual
== FcQualAll
)
1471 * Check to see if there is a match, mark the location
1472 * to apply match-relative edits
1474 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1477 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1479 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1484 if (FcDebug () & FC_DBG_EDIT
)
1485 printf ("No match\n");
1488 if (FcDebug () & FC_DBG_EDIT
)
1490 printf ("Substitute ");
1493 for (e
= s
->edit
; e
; e
= e
->next
)
1496 * Evaluate the list of expressions
1498 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1500 * Locate any test associated with this field, skipping
1501 * tests associated with the pattern when substituting in
1504 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1506 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1507 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1508 (FcChar8
*) e
->field
))
1511 * KLUDGE - the pattern may have been reallocated or
1512 * things may have been inserted or deleted above
1513 * this element by other edits. Go back and find
1516 if (e
!= s
->edit
&& st
[i
].elt
)
1517 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1526 * If there was a test, then replace the matched
1527 * value with the new list of values
1531 FcValueList
*thisValue
= st
[i
].value
;
1532 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1535 * Append the new list of values after the current value
1537 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1539 * Delete the marked value
1542 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1544 * Adjust any pointers into the value list to ensure
1545 * future edits occur at the same place
1547 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1549 if (st
[i
].value
== thisValue
)
1550 st
[i
].value
= nextValue
;
1554 /* fall through ... */
1555 case FcOpAssignReplace
:
1557 * Delete all of the values and insert
1560 FcConfigPatternDel (p
, e
->field
);
1561 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1563 * Adjust any pointers into the value list as they no
1564 * longer point to anything valid
1568 FcPatternElt
*thisElt
= st
[i
].elt
;
1569 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1571 if (st
[i
].elt
== thisElt
)
1579 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1582 /* fall through ... */
1583 case FcOpPrependFirst
:
1584 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1589 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1592 /* fall through ... */
1593 case FcOpAppendLast
:
1594 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1597 FcValueListDestroy (FcValueListPtrCreateDynamic(l
));
1602 * Now go through the pattern and eliminate
1603 * any properties without data
1605 for (e
= s
->edit
; e
; e
= e
->next
)
1606 FcConfigPatternCanon (p
, e
->field
);
1608 if (FcDebug () & FC_DBG_EDIT
)
1610 printf ("FcConfigSubstitute edit");
1614 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1616 if (FcDebug () & FC_DBG_EDIT
)
1618 printf ("FcConfigSubstitute done");
1625 FcConfigSubstitute (FcConfig
*config
,
1629 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1632 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1634 static FcChar8 fontconfig_path
[1000] = "";
1637 DllMain (HINSTANCE hinstDLL
,
1643 switch (fdwReason
) {
1644 case DLL_PROCESS_ATTACH
:
1645 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1646 sizeof (fontconfig_path
)))
1649 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1650 * assume it's a Unix-style installation tree, and use
1651 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1652 * folder where the DLL is as FONTCONFIG_PATH.
1654 p
= strrchr (fontconfig_path
, '\\');
1658 p
= strrchr (fontconfig_path
, '\\');
1659 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1660 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1662 strcat (fontconfig_path
, "\\etc\\fonts");
1665 fontconfig_path
[0] = '\0';
1673 #undef FONTCONFIG_PATH
1674 #define FONTCONFIG_PATH fontconfig_path
1676 #else /* !(_WIN32 && PIC) */
1678 #endif /* !(_WIN32 && PIC) */
1680 #ifndef FONTCONFIG_FILE
1681 #define FONTCONFIG_FILE "fonts.conf"
1685 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1690 dir
= (FcChar8
*) "";
1691 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1695 strcpy ((char *) path
, (const char *) dir
);
1696 /* make sure there's a single separator */
1698 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1699 path
[strlen((char *) path
)-1] != '\\')) &&
1702 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1703 strcat ((char *) path
, "\\");
1705 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1706 strcat ((char *) path
, "/");
1708 strcat ((char *) path
, (char *) file
);
1710 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1711 if (access ((char *) path
, R_OK
) == 0)
1719 FcConfigGetPath (void)
1722 FcChar8
*env
, *e
, *colon
;
1727 npath
= 2; /* default dir + null */
1728 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1734 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1737 path
= calloc (npath
, sizeof (FcChar8
*));
1747 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1749 colon
= e
+ strlen ((char *) e
);
1750 path
[i
] = malloc (colon
- e
+ 1);
1753 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1754 path
[i
][colon
- e
] = '\0';
1763 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1764 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1767 strcpy ((char *) path
[i
], (const char *) dir
);
1771 for (i
= 0; path
[i
]; i
++)
1779 FcConfigFreePath (FcChar8
**path
)
1783 for (p
= path
; *p
; p
++)
1788 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1793 if (_FcConfigHomeEnabled
)
1795 char *home
= getenv ("HOME");
1799 home
= getenv ("USERPROFILE");
1802 return (FcChar8
*) home
;
1808 FcConfigEnableHome (FcBool enable
)
1810 FcBool prev
= _FcConfigHomeEnabled
;
1811 _FcConfigHomeEnabled
= enable
;
1816 FcConfigFilename (const FcChar8
*url
)
1818 FcChar8
*file
, *dir
, **path
, **p
;
1822 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1824 url
= (FcChar8
*) FONTCONFIG_FILE
;
1829 if (isalpha (*url
) &&
1831 (url
[2] == '/' || url
[2] == '\\'))
1837 dir
= FcConfigHome ();
1839 file
= FcConfigFileExists (dir
, url
+ 1);
1848 file
= FcConfigFileExists (0, url
);
1851 path
= FcConfigGetPath ();
1854 for (p
= path
; *p
; p
++)
1856 file
= FcConfigFileExists (*p
, url
);
1860 FcConfigFreePath (path
);
1867 * Manage the application-specific fonts
1871 FcConfigAppFontAddFile (FcConfig
*config
,
1872 const FcChar8
*file
)
1881 config
= FcConfigGetCurrent ();
1886 subdirs
= FcStrSetCreate ();
1890 set
= FcConfigGetFonts (config
, FcSetApplication
);
1893 set
= FcFontSetCreate ();
1896 FcStrSetDestroy (subdirs
);
1899 FcConfigSetFonts (config
, set
, FcSetApplication
);
1902 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1904 FcStrSetDestroy (subdirs
);
1907 if ((sublist
= FcStrListCreate (subdirs
)))
1909 while ((subdir
= FcStrListNext (sublist
)))
1911 FcConfigAppFontAddDir (config
, subdir
);
1913 FcStrListDone (sublist
);
1915 FcStrSetDestroy (subdirs
);
1920 FcConfigAppFontAddDir (FcConfig
*config
,
1930 config
= FcConfigGetCurrent ();
1934 subdirs
= FcStrSetCreate ();
1938 set
= FcConfigGetFonts (config
, FcSetApplication
);
1941 set
= FcFontSetCreate ();
1944 FcStrSetDestroy (subdirs
);
1947 FcConfigSetFonts (config
, set
, FcSetApplication
);
1950 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1952 FcStrSetDestroy (subdirs
);
1955 if ((sublist
= FcStrListCreate (subdirs
)))
1957 while ((subdir
= FcStrListNext (sublist
)))
1959 FcConfigAppFontAddDir (config
, subdir
);
1961 FcStrListDone (sublist
);
1963 FcStrSetDestroy (subdirs
);
1968 FcConfigAppFontClear (FcConfig
*config
)
1972 config
= FcConfigGetCurrent ();
1977 FcConfigSetFonts (config
, 0, FcSetApplication
);
1981 * Manage filename-based font source selectors
1985 FcConfigGlobAdd (FcConfig
*config
,
1986 const FcChar8
*glob
,
1989 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1991 return FcStrSetAdd (set
, glob
);
1995 FcConfigGlobMatch (const FcChar8
*glob
,
1996 const FcChar8
*string
)
2000 while ((c
= *glob
++))
2004 /* short circuit common case */
2007 /* short circuit another common case */
2008 if (strchr ((char *) glob
, '*') == 0)
2009 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
2012 if (FcConfigGlobMatch (glob
, string
))
2018 if (*string
++ == '\0')
2027 return *string
== '\0';
2031 FcConfigGlobsMatch (const FcStrSet
*globs
,
2032 const FcChar8
*string
)
2036 for (i
= 0; i
< globs
->num
; i
++)
2037 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
2043 FcConfigAcceptFilename (FcConfig
*config
,
2044 const FcChar8
*filename
)
2046 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
2048 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
2054 * Manage font-pattern based font source selectors
2058 FcConfigPatternsAdd (FcConfig
*config
,
2062 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
2064 return FcFontSetAdd (set
, pattern
);
2068 FcConfigPatternsMatch (const FcFontSet
*patterns
,
2069 const FcPattern
*font
)
2073 for (i
= 0; i
< patterns
->nfont
; i
++)
2074 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
2080 FcConfigAcceptFont (FcConfig
*config
,
2081 const FcPattern
*font
)
2083 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
2085 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))