2 * fontconfig/src/fccfg.c
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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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
)
80 config
->cacheDirs
= FcStrSetCreate ();
81 if (!config
->cacheDirs
)
86 config
->substPattern
= 0;
87 config
->substFont
= 0;
88 config
->substScan
= 0;
89 config
->maxObjects
= 0;
90 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
91 config
->fonts
[set
] = 0;
93 config
->rescanTime
= time(0);
94 config
->rescanInterval
= 30;
101 FcFontSetDestroy (config
->rejectPatterns
);
103 FcFontSetDestroy (config
->acceptPatterns
);
105 FcStrSetDestroy (config
->rejectGlobs
);
107 FcStrSetDestroy (config
->acceptGlobs
);
109 FcStrSetDestroy (config
->fontDirs
);
111 FcStrSetDestroy (config
->configFiles
);
113 FcStrSetDestroy (config
->configDirs
);
116 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
122 FcConfigNewestFile (FcStrSet
*files
)
124 FcStrList
*list
= FcStrListCreate (files
);
125 FcFileTime newest
= { 0, FcFalse
};
131 while ((file
= FcStrListNext (list
)))
132 if (FcStat ((char *) file
, &statb
) == 0)
133 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
136 newest
.time
= statb
.st_mtime
;
138 FcStrListDone (list
);
144 FcConfigUptoDate (FcConfig
*config
)
146 FcFileTime config_time
, config_dir_time
, font_time
;
147 time_t now
= time(0);
150 config
= FcConfigGetCurrent ();
154 config_time
= FcConfigNewestFile (config
->configFiles
);
155 config_dir_time
= FcConfigNewestFile (config
->configDirs
);
156 font_time
= FcConfigNewestFile (config
->fontDirs
);
157 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
158 (config_dir_time
.set
&& (config_dir_time
.time
- config
->rescanTime
) > 0) ||
159 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
161 /* We need to check for potential clock problems here (OLPC ticket #6046) */
162 if ((config_time
.set
&& (config_time
.time
- now
) > 0) ||
163 (config_dir_time
.set
&& (config_dir_time
.time
- now
) > 0) ||
164 (font_time
.set
&& (font_time
.time
- now
) > 0))
167 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
168 config
->rescanTime
= now
;
174 config
->rescanTime
= now
;
179 FcSubstDestroy (FcSubst
*s
)
187 FcTestDestroy (s
->test
);
189 FcEditDestroy (s
->edit
);
191 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
197 FcConfigReference (FcConfig
*config
)
201 config
= FcConfigGetCurrent ();
212 FcConfigDestroy (FcConfig
*config
)
216 if (--config
->ref
> 0)
219 if (config
== _fcConfig
)
222 FcStrSetDestroy (config
->configDirs
);
223 FcStrSetDestroy (config
->fontDirs
);
224 FcStrSetDestroy (config
->cacheDirs
);
225 FcStrSetDestroy (config
->configFiles
);
226 FcStrSetDestroy (config
->acceptGlobs
);
227 FcStrSetDestroy (config
->rejectGlobs
);
228 FcFontSetDestroy (config
->acceptPatterns
);
229 FcFontSetDestroy (config
->rejectPatterns
);
232 FcBlanksDestroy (config
->blanks
);
234 FcSubstDestroy (config
->substPattern
);
235 FcSubstDestroy (config
->substFont
);
236 FcSubstDestroy (config
->substScan
);
237 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
238 if (config
->fonts
[set
])
239 FcFontSetDestroy (config
->fonts
[set
]);
242 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
246 * Add cache to configuration, adding fonts and directories
250 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
,
251 FcSetName set
, FcStrSet
*dirSet
)
260 fs
= FcCacheSet (cache
);
265 for (i
= 0; i
< fs
->nfont
; i
++)
267 FcPattern
*font
= FcFontSetFont (fs
, i
);
271 * Check to see if font is banned by filename
273 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
274 0, &font_file
) == FcResultMatch
&&
275 !FcConfigAcceptFilename (config
, font_file
))
281 * Check to see if font is banned by pattern
283 if (!FcConfigAcceptFont (config
, font
))
287 FcFontSetAdd (config
->fonts
[set
], font
);
289 FcDirCacheReference (cache
, nref
);
295 dirs
= FcCacheDirs (cache
);
298 for (i
= 0; i
< cache
->dirs_count
; i
++)
300 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
301 if (FcConfigAcceptFilename (config
, dir
))
302 FcStrSetAddFilename (dirSet
, dir
);
309 FcConfigAddDirList (FcConfig
*config
, FcSetName set
, FcStrSet
*dirSet
)
315 dirlist
= FcStrListCreate (dirSet
);
319 while ((dir
= FcStrListNext (dirlist
)))
321 if (FcDebug () & FC_DBG_FONTSET
)
322 printf ("adding fonts from%s\n", dir
);
323 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
326 FcConfigAddCache (config
, cache
, set
, dirSet
);
327 FcDirCacheUnload (cache
);
329 FcStrListDone (dirlist
);
334 * Scan the current list of directories in the configuration
335 * and build the set of available fonts.
339 FcConfigBuildFonts (FcConfig
*config
)
345 config
= FcConfigGetCurrent ();
350 fonts
= FcFontSetCreate ();
354 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
356 if (!FcConfigAddDirList (config
, FcSetSystem
, config
->fontDirs
))
358 if (FcDebug () & FC_DBG_FONTSET
)
359 FcFontSetPrint (fonts
);
364 FcConfigSetCurrent (FcConfig
*config
)
366 if (config
== _fcConfig
)
370 if (!FcConfigBuildFonts (config
))
374 FcConfigDestroy (_fcConfig
);
380 FcConfigGetCurrent (void)
389 FcConfigAddConfigDir (FcConfig
*config
,
392 return FcStrSetAddFilename (config
->configDirs
, d
);
396 FcConfigGetConfigDirs (FcConfig
*config
)
400 config
= FcConfigGetCurrent ();
404 return FcStrListCreate (config
->configDirs
);
408 FcConfigAddFontDir (FcConfig
*config
,
411 return FcStrSetAddFilename (config
->fontDirs
, d
);
415 FcConfigAddDir (FcConfig
*config
,
418 return (FcConfigAddConfigDir (config
, d
) &&
419 FcConfigAddFontDir (config
, d
));
423 FcConfigGetFontDirs (FcConfig
*config
)
427 config
= FcConfigGetCurrent ();
431 return FcStrListCreate (config
->fontDirs
);
435 FcConfigAddCacheDir (FcConfig
*config
,
438 return FcStrSetAddFilename (config
->cacheDirs
, d
);
442 FcConfigGetCacheDirs (FcConfig
*config
)
446 config
= FcConfigGetCurrent ();
450 return FcStrListCreate (config
->cacheDirs
);
454 FcConfigAddConfigFile (FcConfig
*config
,
458 FcChar8
*file
= FcConfigFilename (f
);
463 ret
= FcStrSetAdd (config
->configFiles
, file
);
469 FcConfigGetConfigFiles (FcConfig
*config
)
473 config
= FcConfigGetCurrent ();
477 return FcStrListCreate (config
->configFiles
);
481 FcConfigGetCache (FcConfig
*config
)
487 FcConfigGetFonts (FcConfig
*config
,
492 config
= FcConfigGetCurrent ();
496 return config
->fonts
[set
];
500 FcConfigSetFonts (FcConfig
*config
,
504 if (config
->fonts
[set
])
505 FcFontSetDestroy (config
->fonts
[set
]);
506 config
->fonts
[set
] = fonts
;
510 FcConfigGetBlanks (FcConfig
*config
)
514 config
= FcConfigGetCurrent ();
518 return config
->blanks
;
522 FcConfigAddBlank (FcConfig
*config
,
525 FcBlanks
*b
, *freeme
= 0;
530 freeme
= b
= FcBlanksCreate ();
534 if (!FcBlanksAdd (b
, blank
))
537 FcBlanksDestroy (freeme
);
545 FcConfigGetRescanInterval (FcConfig
*config
)
549 config
= FcConfigGetCurrent ();
553 return config
->rescanInterval
;
557 FcConfigSetRescanInterval (FcConfig
*config
, int rescanInterval
)
561 config
= FcConfigGetCurrent ();
565 config
->rescanInterval
= rescanInterval
;
570 * A couple of typos escaped into the library
573 FcConfigGetRescanInverval (FcConfig
*config
)
575 return FcConfigGetRescanInterval (config
);
579 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
581 return FcConfigSetRescanInterval (config
, rescanInterval
);
586 FcConfigAddEdit (FcConfig
*config
,
591 FcSubst
*subst
, **prev
;
597 prev
= &config
->substPattern
;
600 prev
= &config
->substFont
;
603 prev
= &config
->substScan
;
608 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
611 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
612 for (; *prev
; prev
= &(*prev
)->next
);
618 for (t
= test
; t
; t
= t
->next
)
620 if (t
->kind
== FcMatchDefault
)
624 if (config
->maxObjects
< num
)
625 config
->maxObjects
= num
;
626 if (FcDebug () & FC_DBG_EDIT
)
628 printf ("Add Subst ");
629 FcSubstPrint (subst
);
634 typedef struct _FcSubState
{
640 FcConfigPromote (FcValue v
, FcValue u
)
642 if (v
.type
== FcTypeInteger
)
644 v
.type
= FcTypeDouble
;
645 v
.u
.d
= (double) v
.u
.i
;
647 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
649 v
.u
.m
= &FcIdentityMatrix
;
650 v
.type
= FcTypeMatrix
;
652 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
654 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
655 v
.type
= FcTypeLangSet
;
661 FcConfigCompareValue (const FcValue
*left_o
,
663 const FcValue
*right_o
)
665 FcValue left
= FcValueCanonicalize(left_o
);
666 FcValue right
= FcValueCanonicalize(right_o
);
667 FcBool ret
= FcFalse
;
669 left
= FcConfigPromote (left
, right
);
670 right
= FcConfigPromote (right
, left
);
671 if (left
.type
== right
.type
)
675 break; /* FcConfigPromote prevents this from happening */
681 ret
= left
.u
.d
== right
.u
.d
;
684 case FcOpNotContains
:
685 ret
= left
.u
.d
!= right
.u
.d
;
688 ret
= left
.u
.d
< right
.u
.d
;
691 ret
= left
.u
.d
<= right
.u
.d
;
694 ret
= left
.u
.d
> right
.u
.d
;
697 ret
= left
.u
.d
>= right
.u
.d
;
708 ret
= left
.u
.b
== right
.u
.b
;
711 case FcOpNotContains
:
712 ret
= left
.u
.b
!= right
.u
.b
;
722 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
725 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
728 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
730 case FcOpNotContains
:
731 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
742 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
745 case FcOpNotContains
:
746 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
756 /* left contains right if right is a subset of left */
757 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
759 case FcOpNotContains
:
760 /* left contains right if right is a subset of left */
761 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
764 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
767 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
777 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
779 case FcOpNotContains
:
780 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
783 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
786 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
808 ret
= left
.u
.f
== right
.u
.f
;
811 case FcOpNotContains
:
812 ret
= left
.u
.f
!= right
.u
.f
;
822 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
829 #define _FcDoubleFloor(d) ((int) (d))
830 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
831 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
832 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
833 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
834 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
837 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
846 v
.type
= FcTypeInteger
;
850 v
.type
= FcTypeDouble
;
854 v
.type
= FcTypeString
;
859 v
.type
= FcTypeMatrix
;
864 v
.type
= FcTypeCharSet
;
873 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
874 if (r
!= FcResultMatch
)
879 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
880 v
.type
= FcTypeInteger
;
885 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
886 if (vl
.type
== FcTypeBool
)
889 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
891 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
904 case FcOpNotContains
:
906 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
907 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
909 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
919 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
920 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
921 vl
= FcConfigPromote (vl
, vr
);
922 vr
= FcConfigPromote (vr
, vl
);
923 if (vl
.type
== vr
.type
)
929 v
.type
= FcTypeDouble
;
930 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
933 v
.type
= FcTypeDouble
;
934 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
937 v
.type
= FcTypeDouble
;
938 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
941 v
.type
= FcTypeDouble
;
942 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
948 if (v
.type
== FcTypeDouble
&&
949 v
.u
.d
== (double) (int) v
.u
.d
)
951 v
.type
= FcTypeInteger
;
959 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
963 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
973 v
.type
= FcTypeString
;
974 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
975 v
.u
.s
= FcStrStaticName (str
);
989 v
.type
= FcTypeMatrix
;
990 m
= malloc (sizeof (FcMatrix
));
993 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
994 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1003 v
.type
= FcTypeVoid
;
1008 v
.type
= FcTypeVoid
;
1013 v
.type
= FcTypeVoid
;
1014 FcValueDestroy (vl
);
1015 FcValueDestroy (vr
);
1018 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1021 v
.type
= FcTypeBool
;
1025 v
.type
= FcTypeVoid
;
1028 FcValueDestroy (vl
);
1031 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1037 v
.type
= FcTypeInteger
;
1038 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1041 v
.type
= FcTypeVoid
;
1044 FcValueDestroy (vl
);
1047 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1053 v
.type
= FcTypeInteger
;
1054 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1057 v
.type
= FcTypeVoid
;
1060 FcValueDestroy (vl
);
1063 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1069 v
.type
= FcTypeInteger
;
1070 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1073 v
.type
= FcTypeVoid
;
1076 FcValueDestroy (vl
);
1079 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1085 v
.type
= FcTypeInteger
;
1086 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1089 v
.type
= FcTypeVoid
;
1092 FcValueDestroy (vl
);
1095 v
.type
= FcTypeVoid
;
1101 static FcValueList
*
1102 FcConfigMatchValueList (FcPattern
*p
,
1104 FcValueList
*values
)
1106 FcValueList
*ret
= 0;
1107 FcExpr
*e
= t
->expr
;
1113 /* Compute the value of the match expression */
1114 if (e
->op
== FcOpComma
)
1116 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1117 e
= e
->u
.tree
.right
;
1121 value
= FcConfigEvaluate (p
, e
);
1125 for (v
= values
; v
; v
= FcValueListNext(v
))
1127 /* Compare the pattern value to the match expression value */
1128 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1135 if (t
->qual
== FcQualAll
)
1142 FcValueDestroy (value
);
1147 static FcValueList
*
1148 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1154 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1157 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1158 if (e
->op
== FcOpComma
)
1160 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1161 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1165 l
->value
= FcConfigEvaluate (p
, e
);
1168 l
->binding
= binding
;
1169 if (l
->value
.type
== FcTypeVoid
)
1171 FcValueList
*next
= FcValueListNext(l
);
1173 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1182 FcConfigAdd (FcValueListPtr
*head
,
1183 FcValueList
*position
,
1187 FcValueListPtr
*prev
, last
, v
;
1188 FcValueBinding sameBinding
;
1191 sameBinding
= position
->binding
;
1193 sameBinding
= FcValueBindingWeak
;
1194 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1195 if (v
->binding
== FcValueBindingSame
)
1196 v
->binding
= sameBinding
;
1200 prev
= &position
->next
;
1202 for (prev
= head
; *prev
!= NULL
;
1203 prev
= &(*prev
)->next
)
1210 for (prev
= head
; *prev
!= NULL
;
1211 prev
= &(*prev
)->next
)
1213 if (*prev
== position
)
1220 if (FcDebug () & FC_DBG_EDIT
)
1223 printf ("position not on list\n");
1227 if (FcDebug () & FC_DBG_EDIT
)
1229 printf ("%s list before ", append
? "Append" : "Prepend");
1230 FcValueListPrint (*head
);
1237 while (last
->next
!= NULL
)
1244 if (FcDebug () & FC_DBG_EDIT
)
1246 printf ("%s list after ", append
? "Append" : "Prepend");
1247 FcValueListPrint (*head
);
1255 FcConfigDel (FcValueListPtr
*head
,
1256 FcValueList
*position
)
1258 FcValueListPtr
*prev
;
1260 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1262 if (*prev
== position
)
1264 *prev
= position
->next
;
1265 position
->next
= NULL
;
1266 FcValueListDestroy (position
);
1273 FcConfigPatternAdd (FcPattern
*p
,
1280 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1284 FcConfigAdd (&e
->values
, 0, append
, list
);
1289 * Delete all values associated with a field
1292 FcConfigPatternDel (FcPattern
*p
,
1295 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1298 while (e
->values
!= NULL
)
1299 FcConfigDel (&e
->values
, e
->values
);
1303 FcConfigPatternCanon (FcPattern
*p
,
1306 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1309 if (e
->values
== NULL
)
1310 FcPatternObjectDel (p
, object
);
1314 FcConfigSubstituteWithPat (FcConfig
*config
,
1329 config
= FcConfigGetCurrent ();
1335 case FcMatchPattern
:
1336 s
= config
->substPattern
;
1339 s
= config
->substFont
;
1342 s
= config
->substScan
;
1348 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1349 if (!st
&& config
->maxObjects
)
1351 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1353 if (FcDebug () & FC_DBG_EDIT
)
1355 printf ("FcConfigSubstitute ");
1358 for (; s
; s
= s
->next
)
1361 * Check the tests to see if
1362 * they all match the pattern
1364 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1366 if (FcDebug () & FC_DBG_EDIT
)
1368 printf ("FcConfigSubstitute test ");
1372 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1377 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1381 * If there's no such field in the font,
1382 * then FcQualAll matches while FcQualAny does not
1386 if (t
->qual
== FcQualAll
)
1395 * Check to see if there is a match, mark the location
1396 * to apply match-relative edits
1398 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1401 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1403 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1408 if (FcDebug () & FC_DBG_EDIT
)
1409 printf ("No match\n");
1412 if (FcDebug () & FC_DBG_EDIT
)
1414 printf ("Substitute ");
1417 for (e
= s
->edit
; e
; e
= e
->next
)
1420 * Evaluate the list of expressions
1422 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1424 * Locate any test associated with this field, skipping
1425 * tests associated with the pattern when substituting in
1428 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1430 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1431 t
->object
== e
->object
)
1434 * KLUDGE - the pattern may have been reallocated or
1435 * things may have been inserted or deleted above
1436 * this element by other edits. Go back and find
1439 if (e
!= s
->edit
&& st
[i
].elt
)
1440 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1449 * If there was a test, then replace the matched
1450 * value with the new list of values
1454 FcValueList
*thisValue
= st
[i
].value
;
1455 FcValueList
*nextValue
= thisValue
;
1458 * Append the new list of values after the current value
1460 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1462 * Delete the marked value
1465 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1467 * Adjust any pointers into the value list to ensure
1468 * future edits occur at the same place
1470 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1472 if (st
[i
].value
== thisValue
)
1473 st
[i
].value
= nextValue
;
1477 /* fall through ... */
1478 case FcOpAssignReplace
:
1480 * Delete all of the values and insert
1483 FcConfigPatternDel (p
, e
->object
);
1484 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1486 * Adjust any pointers into the value list as they no
1487 * longer point to anything valid
1491 FcPatternElt
*thisElt
= st
[i
].elt
;
1492 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1494 if (st
[i
].elt
== thisElt
)
1502 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1505 /* fall through ... */
1506 case FcOpPrependFirst
:
1507 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1512 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1515 /* fall through ... */
1516 case FcOpAppendLast
:
1517 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1520 FcValueListDestroy (l
);
1525 * Now go through the pattern and eliminate
1526 * any properties without data
1528 for (e
= s
->edit
; e
; e
= e
->next
)
1529 FcConfigPatternCanon (p
, e
->object
);
1531 if (FcDebug () & FC_DBG_EDIT
)
1533 printf ("FcConfigSubstitute edit");
1537 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1539 if (FcDebug () & FC_DBG_EDIT
)
1541 printf ("FcConfigSubstitute done");
1548 FcConfigSubstitute (FcConfig
*config
,
1552 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1555 #if defined (_WIN32)
1557 # define WIN32_LEAN_AND_MEAN
1558 # define WIN32_EXTRA_LEAN
1559 # include <windows.h>
1561 static FcChar8 fontconfig_path
[1000] = "";
1563 # if (defined (PIC) || defined (DLL_EXPORT))
1566 DllMain (HINSTANCE hinstDLL
,
1572 switch (fdwReason
) {
1573 case DLL_PROCESS_ATTACH
:
1574 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1575 sizeof (fontconfig_path
)))
1578 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1579 * assume it's a Unix-style installation tree, and use
1580 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1581 * folder where the DLL is as FONTCONFIG_PATH.
1583 p
= strrchr (fontconfig_path
, '\\');
1587 p
= strrchr (fontconfig_path
, '\\');
1588 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1589 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1591 strcat (fontconfig_path
, "\\etc\\fonts");
1594 fontconfig_path
[0] = '\0';
1604 #undef FONTCONFIG_PATH
1605 #define FONTCONFIG_PATH fontconfig_path
1607 #endif /* !_WIN32 */
1609 #ifndef FONTCONFIG_FILE
1610 #define FONTCONFIG_FILE "fonts.conf"
1614 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1619 dir
= (FcChar8
*) "";
1620 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1624 strcpy ((char *) path
, (const char *) dir
);
1625 /* make sure there's a single separator */
1627 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1628 path
[strlen((char *) path
)-1] != '\\')) &&
1631 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1632 strcat ((char *) path
, "\\");
1634 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1635 strcat ((char *) path
, "/");
1637 strcat ((char *) path
, (char *) file
);
1639 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1640 if (access ((char *) path
, R_OK
) == 0)
1648 FcConfigGetPath (void)
1651 FcChar8
*env
, *e
, *colon
;
1656 npath
= 2; /* default dir + null */
1657 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1663 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1666 path
= calloc (npath
, sizeof (FcChar8
*));
1676 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1678 colon
= e
+ strlen ((char *) e
);
1679 path
[i
] = malloc (colon
- e
+ 1);
1682 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1683 path
[i
][colon
- e
] = '\0';
1693 if (fontconfig_path
[0] == '\0')
1696 if(!GetModuleFileName(NULL
, fontconfig_path
, sizeof(fontconfig_path
)))
1698 p
= strrchr (fontconfig_path
, '\\');
1700 strcat (fontconfig_path
, "\\fonts");
1703 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1704 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1707 strcpy ((char *) path
[i
], (const char *) dir
);
1711 for (i
= 0; path
[i
]; i
++)
1719 FcConfigFreePath (FcChar8
**path
)
1723 for (p
= path
; *p
; p
++)
1728 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1733 if (_FcConfigHomeEnabled
)
1735 char *home
= getenv ("HOME");
1739 home
= getenv ("USERPROFILE");
1742 return (FcChar8
*) home
;
1748 FcConfigEnableHome (FcBool enable
)
1750 FcBool prev
= _FcConfigHomeEnabled
;
1751 _FcConfigHomeEnabled
= enable
;
1756 FcConfigFilename (const FcChar8
*url
)
1758 FcChar8
*file
, *dir
, **path
, **p
;
1762 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1764 url
= (FcChar8
*) FONTCONFIG_FILE
;
1769 if (isalpha (*url
) &&
1771 (url
[2] == '/' || url
[2] == '\\'))
1777 dir
= FcConfigHome ();
1779 file
= FcConfigFileExists (dir
, url
+ 1);
1788 file
= FcConfigFileExists (0, url
);
1791 path
= FcConfigGetPath ();
1794 for (p
= path
; *p
; p
++)
1796 file
= FcConfigFileExists (*p
, url
);
1800 FcConfigFreePath (path
);
1807 * Manage the application-specific fonts
1811 FcConfigAppFontAddFile (FcConfig
*config
,
1812 const FcChar8
*file
)
1821 config
= FcConfigGetCurrent ();
1826 subdirs
= FcStrSetCreate ();
1830 set
= FcConfigGetFonts (config
, FcSetApplication
);
1833 set
= FcFontSetCreate ();
1836 FcStrSetDestroy (subdirs
);
1839 FcConfigSetFonts (config
, set
, FcSetApplication
);
1842 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1844 FcStrSetDestroy (subdirs
);
1847 if ((sublist
= FcStrListCreate (subdirs
)))
1849 while ((subdir
= FcStrListNext (sublist
)))
1851 FcConfigAppFontAddDir (config
, subdir
);
1853 FcStrListDone (sublist
);
1855 FcStrSetDestroy (subdirs
);
1860 FcConfigAppFontAddDir (FcConfig
*config
,
1868 config
= FcConfigGetCurrent ();
1873 dirs
= FcStrSetCreate ();
1877 set
= FcConfigGetFonts (config
, FcSetApplication
);
1880 set
= FcFontSetCreate ();
1883 FcStrSetDestroy (dirs
);
1886 FcConfigSetFonts (config
, set
, FcSetApplication
);
1889 FcStrSetAddFilename (dirs
, dir
);
1891 if (!FcConfigAddDirList (config
, FcSetApplication
, dirs
))
1893 FcStrSetDestroy (dirs
);
1896 FcStrSetDestroy (dirs
);
1901 FcConfigAppFontClear (FcConfig
*config
)
1905 config
= FcConfigGetCurrent ();
1910 FcConfigSetFonts (config
, 0, FcSetApplication
);
1914 * Manage filename-based font source selectors
1918 FcConfigGlobAdd (FcConfig
*config
,
1919 const FcChar8
*glob
,
1922 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1924 return FcStrSetAdd (set
, glob
);
1928 FcConfigGlobMatch (const FcChar8
*glob
,
1929 const FcChar8
*string
)
1933 while ((c
= *glob
++))
1937 /* short circuit common case */
1940 /* short circuit another common case */
1941 if (strchr ((char *) glob
, '*') == 0)
1942 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1945 if (FcConfigGlobMatch (glob
, string
))
1951 if (*string
++ == '\0')
1960 return *string
== '\0';
1964 FcConfigGlobsMatch (const FcStrSet
*globs
,
1965 const FcChar8
*string
)
1969 for (i
= 0; i
< globs
->num
; i
++)
1970 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1976 FcConfigAcceptFilename (FcConfig
*config
,
1977 const FcChar8
*filename
)
1979 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1981 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1987 * Manage font-pattern based font source selectors
1991 FcConfigPatternsAdd (FcConfig
*config
,
1995 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1997 return FcFontSetAdd (set
, pattern
);
2001 FcConfigPatternsMatch (const FcFontSet
*patterns
,
2002 const FcPattern
*font
)
2006 for (i
= 0; i
< patterns
->nfont
; i
++)
2007 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
2013 FcConfigAcceptFont (FcConfig
*config
,
2014 const FcPattern
*font
)
2016 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
2018 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))
2023 #include "fcaliastail.h"