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 * 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
)
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;
99 FcFontSetDestroy (config
->rejectPatterns
);
101 FcFontSetDestroy (config
->acceptPatterns
);
103 FcStrSetDestroy (config
->rejectGlobs
);
105 FcStrSetDestroy (config
->acceptGlobs
);
107 FcStrSetDestroy (config
->fontDirs
);
109 FcStrSetDestroy (config
->configFiles
);
111 FcStrSetDestroy (config
->configDirs
);
114 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
120 FcConfigNewestFile (FcStrSet
*files
)
122 FcStrList
*list
= FcStrListCreate (files
);
123 FcFileTime newest
= { 0, FcFalse
};
129 while ((file
= FcStrListNext (list
)))
130 if (stat ((char *) file
, &statb
) == 0)
131 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
134 newest
.time
= statb
.st_mtime
;
136 FcStrListDone (list
);
142 FcConfigUptoDate (FcConfig
*config
)
144 FcFileTime config_time
, config_dir_time
, font_time
;
145 time_t now
= time(0);
148 config
= FcConfigGetCurrent ();
152 config_time
= FcConfigNewestFile (config
->configFiles
);
153 config_dir_time
= FcConfigNewestFile (config
->configDirs
);
154 font_time
= FcConfigNewestFile (config
->fontDirs
);
155 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
156 (config_dir_time
.set
&& (config_dir_time
.time
- config
->rescanTime
) > 0) ||
157 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
159 /* We need to check for potential clock problems here (OLPC ticket #6046) */
160 if ((config_time
.set
&& (config_time
.time
- now
) > 0) ||
161 (config_dir_time
.set
&& (config_dir_time
.time
- now
) > 0) ||
162 (font_time
.set
&& (font_time
.time
- now
) > 0))
165 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
166 config
->rescanTime
= now
;
172 config
->rescanTime
= now
;
177 FcSubstDestroy (FcSubst
*s
)
185 FcTestDestroy (s
->test
);
187 FcEditDestroy (s
->edit
);
189 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
195 FcConfigDestroy (FcConfig
*config
)
199 if (config
== _fcConfig
)
202 FcStrSetDestroy (config
->configDirs
);
203 FcStrSetDestroy (config
->fontDirs
);
204 FcStrSetDestroy (config
->cacheDirs
);
205 FcStrSetDestroy (config
->configFiles
);
206 FcStrSetDestroy (config
->acceptGlobs
);
207 FcStrSetDestroy (config
->rejectGlobs
);
208 FcFontSetDestroy (config
->acceptPatterns
);
209 FcFontSetDestroy (config
->rejectPatterns
);
212 FcBlanksDestroy (config
->blanks
);
214 FcSubstDestroy (config
->substPattern
);
215 FcSubstDestroy (config
->substFont
);
216 FcSubstDestroy (config
->substScan
);
217 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
218 if (config
->fonts
[set
])
219 FcFontSetDestroy (config
->fonts
[set
]);
222 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
226 * Add cache to configuration, adding fonts and directories
230 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
,
231 FcSetName set
, FcStrSet
*dirSet
)
240 fs
= FcCacheSet (cache
);
245 for (i
= 0; i
< fs
->nfont
; i
++)
247 FcPattern
*font
= FcFontSetFont (fs
, i
);
251 * Check to see if font is banned by filename
253 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
254 0, &font_file
) == FcResultMatch
&&
255 !FcConfigAcceptFilename (config
, font_file
))
261 * Check to see if font is banned by pattern
263 if (!FcConfigAcceptFont (config
, font
))
267 FcFontSetAdd (config
->fonts
[set
], font
);
269 FcDirCacheReference (cache
, nref
);
275 dirs
= FcCacheDirs (cache
);
278 for (i
= 0; i
< cache
->dirs_count
; i
++)
280 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
281 if (FcConfigAcceptFilename (config
, dir
))
282 FcStrSetAddFilename (dirSet
, dir
);
289 FcConfigAddDirList (FcConfig
*config
, FcSetName set
, FcStrSet
*dirSet
)
295 dirlist
= FcStrListCreate (dirSet
);
299 while ((dir
= FcStrListNext (dirlist
)))
301 if (FcDebug () & FC_DBG_FONTSET
)
302 printf ("adding fonts from%s\n", dir
);
303 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
306 FcConfigAddCache (config
, cache
, set
, dirSet
);
307 FcDirCacheUnload (cache
);
309 FcStrListDone (dirlist
);
314 * Scan the current list of directories in the configuration
315 * and build the set of available fonts.
319 FcConfigBuildFonts (FcConfig
*config
)
325 config
= FcConfigGetCurrent ();
330 fonts
= FcFontSetCreate ();
334 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
336 if (!FcConfigAddDirList (config
, FcSetSystem
, config
->fontDirs
))
338 if (FcDebug () & FC_DBG_FONTSET
)
339 FcFontSetPrint (fonts
);
344 FcConfigSetCurrent (FcConfig
*config
)
346 if (config
== _fcConfig
)
350 if (!FcConfigBuildFonts (config
))
354 FcConfigDestroy (_fcConfig
);
360 FcConfigGetCurrent (void)
369 FcConfigAddConfigDir (FcConfig
*config
,
372 return FcStrSetAddFilename (config
->configDirs
, d
);
376 FcConfigGetConfigDirs (FcConfig
*config
)
380 config
= FcConfigGetCurrent ();
384 return FcStrListCreate (config
->configDirs
);
388 FcConfigAddFontDir (FcConfig
*config
,
391 return FcStrSetAddFilename (config
->fontDirs
, d
);
395 FcConfigAddDir (FcConfig
*config
,
398 return (FcConfigAddConfigDir (config
, d
) &&
399 FcConfigAddFontDir (config
, d
));
403 FcConfigGetFontDirs (FcConfig
*config
)
407 config
= FcConfigGetCurrent ();
411 return FcStrListCreate (config
->fontDirs
);
415 FcConfigAddCacheDir (FcConfig
*config
,
418 return FcStrSetAddFilename (config
->cacheDirs
, d
);
422 FcConfigGetCacheDirs (FcConfig
*config
)
426 config
= FcConfigGetCurrent ();
430 return FcStrListCreate (config
->cacheDirs
);
434 FcConfigAddConfigFile (FcConfig
*config
,
438 FcChar8
*file
= FcConfigFilename (f
);
443 ret
= FcStrSetAdd (config
->configFiles
, file
);
449 FcConfigGetConfigFiles (FcConfig
*config
)
453 config
= FcConfigGetCurrent ();
457 return FcStrListCreate (config
->configFiles
);
461 FcConfigGetCache (FcConfig
*config
)
467 FcConfigGetFonts (FcConfig
*config
,
472 config
= FcConfigGetCurrent ();
476 return config
->fonts
[set
];
480 FcConfigSetFonts (FcConfig
*config
,
484 if (config
->fonts
[set
])
485 FcFontSetDestroy (config
->fonts
[set
]);
486 config
->fonts
[set
] = fonts
;
490 FcConfigGetBlanks (FcConfig
*config
)
494 config
= FcConfigGetCurrent ();
498 return config
->blanks
;
502 FcConfigAddBlank (FcConfig
*config
,
505 FcBlanks
*b
, *freeme
= 0;
510 freeme
= b
= FcBlanksCreate ();
514 if (!FcBlanksAdd (b
, blank
))
517 FcBlanksDestroy (freeme
);
525 FcConfigGetRescanInterval (FcConfig
*config
)
529 config
= FcConfigGetCurrent ();
533 return config
->rescanInterval
;
537 FcConfigSetRescanInterval (FcConfig
*config
, int rescanInterval
)
541 config
= FcConfigGetCurrent ();
545 config
->rescanInterval
= rescanInterval
;
550 * A couple of typos escaped into the library
553 FcConfigGetRescanInverval (FcConfig
*config
)
555 return FcConfigGetRescanInterval (config
);
559 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
561 return FcConfigSetRescanInterval (config
, rescanInterval
);
566 FcConfigAddEdit (FcConfig
*config
,
571 FcSubst
*subst
, **prev
;
577 prev
= &config
->substPattern
;
580 prev
= &config
->substFont
;
583 prev
= &config
->substScan
;
588 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
591 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
592 for (; *prev
; prev
= &(*prev
)->next
);
598 for (t
= test
; t
; t
= t
->next
)
600 if (t
->kind
== FcMatchDefault
)
604 if (config
->maxObjects
< num
)
605 config
->maxObjects
= num
;
606 if (FcDebug () & FC_DBG_EDIT
)
608 printf ("Add Subst ");
609 FcSubstPrint (subst
);
614 typedef struct _FcSubState
{
620 FcConfigPromote (FcValue v
, FcValue u
)
622 if (v
.type
== FcTypeInteger
)
624 v
.type
= FcTypeDouble
;
625 v
.u
.d
= (double) v
.u
.i
;
627 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
629 v
.u
.m
= &FcIdentityMatrix
;
630 v
.type
= FcTypeMatrix
;
632 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
634 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
635 v
.type
= FcTypeLangSet
;
641 FcConfigCompareValue (const FcValue
*left_o
,
643 const FcValue
*right_o
)
645 FcValue left
= FcValueCanonicalize(left_o
);
646 FcValue right
= FcValueCanonicalize(right_o
);
647 FcBool ret
= FcFalse
;
649 left
= FcConfigPromote (left
, right
);
650 right
= FcConfigPromote (right
, left
);
651 if (left
.type
== right
.type
)
655 break; /* FcConfigPromote prevents this from happening */
661 ret
= left
.u
.d
== right
.u
.d
;
664 case FcOpNotContains
:
665 ret
= left
.u
.d
!= right
.u
.d
;
668 ret
= left
.u
.d
< right
.u
.d
;
671 ret
= left
.u
.d
<= right
.u
.d
;
674 ret
= left
.u
.d
> right
.u
.d
;
677 ret
= left
.u
.d
>= right
.u
.d
;
688 ret
= left
.u
.b
== right
.u
.b
;
691 case FcOpNotContains
:
692 ret
= left
.u
.b
!= right
.u
.b
;
702 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
705 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
708 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
710 case FcOpNotContains
:
711 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
722 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
725 case FcOpNotContains
:
726 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
736 /* left contains right if right is a subset of left */
737 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
739 case FcOpNotContains
:
740 /* left contains right if right is a subset of left */
741 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
744 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
747 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
757 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
759 case FcOpNotContains
:
760 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
763 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
766 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
788 ret
= left
.u
.f
== right
.u
.f
;
791 case FcOpNotContains
:
792 ret
= left
.u
.f
!= right
.u
.f
;
802 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
809 #define _FcDoubleFloor(d) ((int) (d))
810 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
811 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
812 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
813 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
814 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
817 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
826 v
.type
= FcTypeInteger
;
830 v
.type
= FcTypeDouble
;
834 v
.type
= FcTypeString
;
835 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
838 v
.type
= FcTypeMatrix
;
843 v
.type
= FcTypeCharSet
;
852 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
853 if (r
!= FcResultMatch
)
858 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
859 v
.type
= FcTypeInteger
;
864 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
865 if (vl
.type
== FcTypeBool
)
868 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
870 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
883 case FcOpNotContains
:
885 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
886 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
888 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
898 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
899 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
900 vl
= FcConfigPromote (vl
, vr
);
901 vr
= FcConfigPromote (vr
, vl
);
902 if (vl
.type
== vr
.type
)
908 v
.type
= FcTypeDouble
;
909 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
912 v
.type
= FcTypeDouble
;
913 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
916 v
.type
= FcTypeDouble
;
917 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
920 v
.type
= FcTypeDouble
;
921 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
927 if (v
.type
== FcTypeDouble
&&
928 v
.u
.d
== (double) (int) v
.u
.d
)
930 v
.type
= FcTypeInteger
;
938 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
942 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
952 v
.type
= FcTypeString
;
953 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
954 v
.u
.s
= FcStrStaticName (str
);
968 v
.type
= FcTypeMatrix
;
969 m
= malloc (sizeof (FcMatrix
));
972 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
973 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
997 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1000 v
.type
= FcTypeBool
;
1004 v
.type
= FcTypeVoid
;
1007 FcValueDestroy (vl
);
1010 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1016 v
.type
= FcTypeInteger
;
1017 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1020 v
.type
= FcTypeVoid
;
1023 FcValueDestroy (vl
);
1026 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1032 v
.type
= FcTypeInteger
;
1033 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1036 v
.type
= FcTypeVoid
;
1039 FcValueDestroy (vl
);
1042 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1048 v
.type
= FcTypeInteger
;
1049 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1052 v
.type
= FcTypeVoid
;
1055 FcValueDestroy (vl
);
1058 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1064 v
.type
= FcTypeInteger
;
1065 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1068 v
.type
= FcTypeVoid
;
1071 FcValueDestroy (vl
);
1074 v
.type
= FcTypeVoid
;
1080 static FcValueList
*
1081 FcConfigMatchValueList (FcPattern
*p
,
1083 FcValueList
*values
)
1085 FcValueList
*ret
= 0;
1086 FcExpr
*e
= t
->expr
;
1092 /* Compute the value of the match expression */
1093 if (e
->op
== FcOpComma
)
1095 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1096 e
= e
->u
.tree
.right
;
1100 value
= FcConfigEvaluate (p
, e
);
1104 for (v
= values
; v
; v
= FcValueListNext(v
))
1106 /* Compare the pattern value to the match expression value */
1107 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1114 if (t
->qual
== FcQualAll
)
1121 FcValueDestroy (value
);
1126 static FcValueList
*
1127 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1133 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1136 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1137 if (e
->op
== FcOpComma
)
1139 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1140 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1144 l
->value
= FcConfigEvaluate (p
, e
);
1147 l
->binding
= binding
;
1148 if (l
->value
.type
== FcTypeVoid
)
1150 FcValueList
*next
= FcValueListNext(l
);
1152 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1161 FcConfigAdd (FcValueListPtr
*head
,
1162 FcValueList
*position
,
1166 FcValueListPtr
*prev
, last
, v
;
1167 FcValueBinding sameBinding
;
1170 sameBinding
= position
->binding
;
1172 sameBinding
= FcValueBindingWeak
;
1173 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1174 if (v
->binding
== FcValueBindingSame
)
1175 v
->binding
= sameBinding
;
1179 prev
= &position
->next
;
1181 for (prev
= head
; *prev
!= NULL
;
1182 prev
= &(*prev
)->next
)
1189 for (prev
= head
; *prev
!= NULL
;
1190 prev
= &(*prev
)->next
)
1192 if (*prev
== position
)
1199 if (FcDebug () & FC_DBG_EDIT
)
1202 printf ("position not on list\n");
1206 if (FcDebug () & FC_DBG_EDIT
)
1208 printf ("%s list before ", append
? "Append" : "Prepend");
1209 FcValueListPrint (*head
);
1216 while (last
->next
!= NULL
)
1223 if (FcDebug () & FC_DBG_EDIT
)
1225 printf ("%s list after ", append
? "Append" : "Prepend");
1226 FcValueListPrint (*head
);
1234 FcConfigDel (FcValueListPtr
*head
,
1235 FcValueList
*position
)
1237 FcValueListPtr
*prev
;
1239 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1241 if (*prev
== position
)
1243 *prev
= position
->next
;
1244 position
->next
= NULL
;
1245 FcValueListDestroy (position
);
1252 FcConfigPatternAdd (FcPattern
*p
,
1259 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1263 FcConfigAdd (&e
->values
, 0, append
, list
);
1268 * Delete all values associated with a field
1271 FcConfigPatternDel (FcPattern
*p
,
1274 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1277 while (e
->values
!= NULL
)
1278 FcConfigDel (&e
->values
, e
->values
);
1282 FcConfigPatternCanon (FcPattern
*p
,
1285 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1288 if (e
->values
== NULL
)
1289 FcPatternObjectDel (p
, object
);
1293 FcConfigSubstituteWithPat (FcConfig
*config
,
1308 config
= FcConfigGetCurrent ();
1314 case FcMatchPattern
:
1315 s
= config
->substPattern
;
1318 s
= config
->substFont
;
1321 s
= config
->substScan
;
1327 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1328 if (!st
&& config
->maxObjects
)
1330 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1332 if (FcDebug () & FC_DBG_EDIT
)
1334 printf ("FcConfigSubstitute ");
1337 for (; s
; s
= s
->next
)
1340 * Check the tests to see if
1341 * they all match the pattern
1343 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1345 if (FcDebug () & FC_DBG_EDIT
)
1347 printf ("FcConfigSubstitute test ");
1351 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1356 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1360 * If there's no such field in the font,
1361 * then FcQualAll matches while FcQualAny does not
1365 if (t
->qual
== FcQualAll
)
1374 * Check to see if there is a match, mark the location
1375 * to apply match-relative edits
1377 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1380 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1382 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1387 if (FcDebug () & FC_DBG_EDIT
)
1388 printf ("No match\n");
1391 if (FcDebug () & FC_DBG_EDIT
)
1393 printf ("Substitute ");
1396 for (e
= s
->edit
; e
; e
= e
->next
)
1399 * Evaluate the list of expressions
1401 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1403 * Locate any test associated with this field, skipping
1404 * tests associated with the pattern when substituting in
1407 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1409 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1410 t
->object
== e
->object
)
1413 * KLUDGE - the pattern may have been reallocated or
1414 * things may have been inserted or deleted above
1415 * this element by other edits. Go back and find
1418 if (e
!= s
->edit
&& st
[i
].elt
)
1419 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1428 * If there was a test, then replace the matched
1429 * value with the new list of values
1433 FcValueList
*thisValue
= st
[i
].value
;
1434 FcValueList
*nextValue
= thisValue
;
1437 * Append the new list of values after the current value
1439 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1441 * Delete the marked value
1444 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1446 * Adjust any pointers into the value list to ensure
1447 * future edits occur at the same place
1449 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1451 if (st
[i
].value
== thisValue
)
1452 st
[i
].value
= nextValue
;
1456 /* fall through ... */
1457 case FcOpAssignReplace
:
1459 * Delete all of the values and insert
1462 FcConfigPatternDel (p
, e
->object
);
1463 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1465 * Adjust any pointers into the value list as they no
1466 * longer point to anything valid
1470 FcPatternElt
*thisElt
= st
[i
].elt
;
1471 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1473 if (st
[i
].elt
== thisElt
)
1481 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1484 /* fall through ... */
1485 case FcOpPrependFirst
:
1486 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1491 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1494 /* fall through ... */
1495 case FcOpAppendLast
:
1496 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1499 FcValueListDestroy (l
);
1504 * Now go through the pattern and eliminate
1505 * any properties without data
1507 for (e
= s
->edit
; e
; e
= e
->next
)
1508 FcConfigPatternCanon (p
, e
->object
);
1510 if (FcDebug () & FC_DBG_EDIT
)
1512 printf ("FcConfigSubstitute edit");
1516 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1518 if (FcDebug () & FC_DBG_EDIT
)
1520 printf ("FcConfigSubstitute done");
1527 FcConfigSubstitute (FcConfig
*config
,
1531 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1534 #if defined (_WIN32)
1536 # define WIN32_LEAN_AND_MEAN
1537 # define WIN32_EXTRA_LEAN
1538 # include <windows.h>
1540 static FcChar8 fontconfig_path
[1000] = "";
1542 # if (defined (PIC) || defined (DLL_EXPORT))
1545 DllMain (HINSTANCE hinstDLL
,
1551 switch (fdwReason
) {
1552 case DLL_PROCESS_ATTACH
:
1553 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1554 sizeof (fontconfig_path
)))
1557 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1558 * assume it's a Unix-style installation tree, and use
1559 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1560 * folder where the DLL is as FONTCONFIG_PATH.
1562 p
= strrchr (fontconfig_path
, '\\');
1566 p
= strrchr (fontconfig_path
, '\\');
1567 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1568 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1570 strcat (fontconfig_path
, "\\etc\\fonts");
1573 fontconfig_path
[0] = '\0';
1583 #undef FONTCONFIG_PATH
1584 #define FONTCONFIG_PATH fontconfig_path
1586 #endif /* !_WIN32 */
1588 #ifndef FONTCONFIG_FILE
1589 #define FONTCONFIG_FILE "fonts.conf"
1593 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1598 dir
= (FcChar8
*) "";
1599 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1603 strcpy ((char *) path
, (const char *) dir
);
1604 /* make sure there's a single separator */
1606 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1607 path
[strlen((char *) path
)-1] != '\\')) &&
1610 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1611 strcat ((char *) path
, "\\");
1613 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1614 strcat ((char *) path
, "/");
1616 strcat ((char *) path
, (char *) file
);
1618 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1619 if (access ((char *) path
, R_OK
) == 0)
1627 FcConfigGetPath (void)
1630 FcChar8
*env
, *e
, *colon
;
1635 npath
= 2; /* default dir + null */
1636 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1642 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1645 path
= calloc (npath
, sizeof (FcChar8
*));
1655 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1657 colon
= e
+ strlen ((char *) e
);
1658 path
[i
] = malloc (colon
- e
+ 1);
1661 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1662 path
[i
][colon
- e
] = '\0';
1672 if (fontconfig_path
[0] == '\0')
1675 if(!GetModuleFileName(NULL
, fontconfig_path
, sizeof(fontconfig_path
)))
1677 p
= strrchr (fontconfig_path
, '\\');
1679 strcat (fontconfig_path
, "\\fonts");
1682 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1683 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1686 strcpy ((char *) path
[i
], (const char *) dir
);
1690 for (i
= 0; path
[i
]; i
++)
1698 FcConfigFreePath (FcChar8
**path
)
1702 for (p
= path
; *p
; p
++)
1707 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1712 if (_FcConfigHomeEnabled
)
1714 char *home
= getenv ("HOME");
1718 home
= getenv ("USERPROFILE");
1721 return (FcChar8
*) home
;
1727 FcConfigEnableHome (FcBool enable
)
1729 FcBool prev
= _FcConfigHomeEnabled
;
1730 _FcConfigHomeEnabled
= enable
;
1735 FcConfigFilename (const FcChar8
*url
)
1737 FcChar8
*file
, *dir
, **path
, **p
;
1741 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1743 url
= (FcChar8
*) FONTCONFIG_FILE
;
1748 if (isalpha (*url
) &&
1750 (url
[2] == '/' || url
[2] == '\\'))
1756 dir
= FcConfigHome ();
1758 file
= FcConfigFileExists (dir
, url
+ 1);
1767 file
= FcConfigFileExists (0, url
);
1770 path
= FcConfigGetPath ();
1773 for (p
= path
; *p
; p
++)
1775 file
= FcConfigFileExists (*p
, url
);
1779 FcConfigFreePath (path
);
1786 * Manage the application-specific fonts
1790 FcConfigAppFontAddFile (FcConfig
*config
,
1791 const FcChar8
*file
)
1800 config
= FcConfigGetCurrent ();
1805 subdirs
= FcStrSetCreate ();
1809 set
= FcConfigGetFonts (config
, FcSetApplication
);
1812 set
= FcFontSetCreate ();
1815 FcStrSetDestroy (subdirs
);
1818 FcConfigSetFonts (config
, set
, FcSetApplication
);
1821 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1823 FcStrSetDestroy (subdirs
);
1826 if ((sublist
= FcStrListCreate (subdirs
)))
1828 while ((subdir
= FcStrListNext (sublist
)))
1830 FcConfigAppFontAddDir (config
, subdir
);
1832 FcStrListDone (sublist
);
1834 FcStrSetDestroy (subdirs
);
1839 FcConfigAppFontAddDir (FcConfig
*config
,
1847 config
= FcConfigGetCurrent ();
1852 dirs
= FcStrSetCreate ();
1856 set
= FcConfigGetFonts (config
, FcSetApplication
);
1859 set
= FcFontSetCreate ();
1862 FcStrSetDestroy (dirs
);
1865 FcConfigSetFonts (config
, set
, FcSetApplication
);
1868 FcStrSetAddFilename (dirs
, dir
);
1870 if (!FcConfigAddDirList (config
, FcSetApplication
, dirs
))
1872 FcStrSetDestroy (dirs
);
1875 FcStrSetDestroy (dirs
);
1880 FcConfigAppFontClear (FcConfig
*config
)
1884 config
= FcConfigGetCurrent ();
1889 FcConfigSetFonts (config
, 0, FcSetApplication
);
1893 * Manage filename-based font source selectors
1897 FcConfigGlobAdd (FcConfig
*config
,
1898 const FcChar8
*glob
,
1901 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1903 return FcStrSetAdd (set
, glob
);
1907 FcConfigGlobMatch (const FcChar8
*glob
,
1908 const FcChar8
*string
)
1912 while ((c
= *glob
++))
1916 /* short circuit common case */
1919 /* short circuit another common case */
1920 if (strchr ((char *) glob
, '*') == 0)
1921 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1924 if (FcConfigGlobMatch (glob
, string
))
1930 if (*string
++ == '\0')
1939 return *string
== '\0';
1943 FcConfigGlobsMatch (const FcStrSet
*globs
,
1944 const FcChar8
*string
)
1948 for (i
= 0; i
< globs
->num
; i
++)
1949 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1955 FcConfigAcceptFilename (FcConfig
*config
,
1956 const FcChar8
*filename
)
1958 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1960 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1966 * Manage font-pattern based font source selectors
1970 FcConfigPatternsAdd (FcConfig
*config
,
1974 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1976 return FcFontSetAdd (set
, pattern
);
1980 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1981 const FcPattern
*font
)
1985 for (i
= 0; i
< patterns
->nfont
; i
++)
1986 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1992 FcConfigAcceptFont (FcConfig
*config
,
1993 const FcPattern
*font
)
1995 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1997 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))
2002 #include "fcaliastail.h"