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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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;
96 config
->expr_pool
= NULL
;
103 FcFontSetDestroy (config
->rejectPatterns
);
105 FcFontSetDestroy (config
->acceptPatterns
);
107 FcStrSetDestroy (config
->rejectGlobs
);
109 FcStrSetDestroy (config
->acceptGlobs
);
111 FcStrSetDestroy (config
->fontDirs
);
113 FcStrSetDestroy (config
->configFiles
);
115 FcStrSetDestroy (config
->configDirs
);
118 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
124 FcConfigNewestFile (FcStrSet
*files
)
126 FcStrList
*list
= FcStrListCreate (files
);
127 FcFileTime newest
= { 0, FcFalse
};
133 while ((file
= FcStrListNext (list
)))
134 if (FcStat ((char *) file
, &statb
) == 0)
135 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
138 newest
.time
= statb
.st_mtime
;
140 FcStrListDone (list
);
146 FcConfigUptoDate (FcConfig
*config
)
148 FcFileTime config_time
, config_dir_time
, font_time
;
149 time_t now
= time(0);
152 config
= FcConfigGetCurrent ();
156 config_time
= FcConfigNewestFile (config
->configFiles
);
157 config_dir_time
= FcConfigNewestFile (config
->configDirs
);
158 font_time
= FcConfigNewestFile (config
->fontDirs
);
159 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
160 (config_dir_time
.set
&& (config_dir_time
.time
- config
->rescanTime
) > 0) ||
161 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
163 /* We need to check for potential clock problems here (OLPC ticket #6046) */
164 if ((config_time
.set
&& (config_time
.time
- now
) > 0) ||
165 (config_dir_time
.set
&& (config_dir_time
.time
- now
) > 0) ||
166 (font_time
.set
&& (font_time
.time
- now
) > 0))
169 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
170 config
->rescanTime
= now
;
176 config
->rescanTime
= now
;
181 FcSubstDestroy (FcSubst
*s
)
189 FcTestDestroy (s
->test
);
191 FcEditDestroy (s
->edit
);
193 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
199 FcConfigAllocExpr (FcConfig
*config
)
201 if (!config
->expr_pool
|| config
->expr_pool
->next
== config
->expr_pool
->end
)
203 FcExprPage
*new_page
;
205 new_page
= malloc (sizeof (FcExprPage
));
208 FcMemAlloc (FC_MEM_EXPR
, sizeof (FcExprPage
));
210 new_page
->next_page
= config
->expr_pool
;
211 new_page
->next
= new_page
->exprs
;
212 config
->expr_pool
= new_page
;
215 return config
->expr_pool
->next
++;
219 FcConfigReference (FcConfig
*config
)
223 config
= FcConfigGetCurrent ();
234 FcConfigDestroy (FcConfig
*config
)
239 if (--config
->ref
> 0)
242 if (config
== _fcConfig
)
245 FcStrSetDestroy (config
->configDirs
);
246 FcStrSetDestroy (config
->fontDirs
);
247 FcStrSetDestroy (config
->cacheDirs
);
248 FcStrSetDestroy (config
->configFiles
);
249 FcStrSetDestroy (config
->acceptGlobs
);
250 FcStrSetDestroy (config
->rejectGlobs
);
251 FcFontSetDestroy (config
->acceptPatterns
);
252 FcFontSetDestroy (config
->rejectPatterns
);
255 FcBlanksDestroy (config
->blanks
);
257 FcSubstDestroy (config
->substPattern
);
258 FcSubstDestroy (config
->substFont
);
259 FcSubstDestroy (config
->substScan
);
260 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
261 if (config
->fonts
[set
])
262 FcFontSetDestroy (config
->fonts
[set
]);
264 page
= config
->expr_pool
;
267 FcExprPage
*next
= page
->next_page
;
268 FcMemFree (FC_MEM_EXPR
, sizeof (FcExprPage
));
274 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
278 * Add cache to configuration, adding fonts and directories
282 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
,
283 FcSetName set
, FcStrSet
*dirSet
)
292 fs
= FcCacheSet (cache
);
297 for (i
= 0; i
< fs
->nfont
; i
++)
299 FcPattern
*font
= FcFontSetFont (fs
, i
);
303 * Check to see if font is banned by filename
305 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
306 0, &font_file
) == FcResultMatch
&&
307 !FcConfigAcceptFilename (config
, font_file
))
313 * Check to see if font is banned by pattern
315 if (!FcConfigAcceptFont (config
, font
))
319 FcFontSetAdd (config
->fonts
[set
], font
);
321 FcDirCacheReference (cache
, nref
);
327 dirs
= FcCacheDirs (cache
);
330 for (i
= 0; i
< cache
->dirs_count
; i
++)
332 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
333 if (FcConfigAcceptFilename (config
, dir
))
334 FcStrSetAddFilename (dirSet
, dir
);
341 FcConfigAddDirList (FcConfig
*config
, FcSetName set
, FcStrSet
*dirSet
)
347 dirlist
= FcStrListCreate (dirSet
);
351 while ((dir
= FcStrListNext (dirlist
)))
353 if (FcDebug () & FC_DBG_FONTSET
)
354 printf ("adding fonts from%s\n", dir
);
355 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
358 FcConfigAddCache (config
, cache
, set
, dirSet
);
359 FcDirCacheUnload (cache
);
361 FcStrListDone (dirlist
);
366 * Scan the current list of directories in the configuration
367 * and build the set of available fonts.
371 FcConfigBuildFonts (FcConfig
*config
)
377 config
= FcConfigGetCurrent ();
382 fonts
= FcFontSetCreate ();
386 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
388 if (!FcConfigAddDirList (config
, FcSetSystem
, config
->fontDirs
))
390 if (FcDebug () & FC_DBG_FONTSET
)
391 FcFontSetPrint (fonts
);
396 FcConfigSetCurrent (FcConfig
*config
)
398 if (config
== _fcConfig
)
402 if (!FcConfigBuildFonts (config
))
406 FcConfigDestroy (_fcConfig
);
412 FcConfigGetCurrent (void)
421 FcConfigAddConfigDir (FcConfig
*config
,
424 return FcStrSetAddFilename (config
->configDirs
, d
);
428 FcConfigGetConfigDirs (FcConfig
*config
)
432 config
= FcConfigGetCurrent ();
436 return FcStrListCreate (config
->configDirs
);
440 FcConfigAddFontDir (FcConfig
*config
,
443 return FcStrSetAddFilename (config
->fontDirs
, d
);
447 FcConfigAddDir (FcConfig
*config
,
450 return (FcConfigAddConfigDir (config
, d
) &&
451 FcConfigAddFontDir (config
, d
));
455 FcConfigGetFontDirs (FcConfig
*config
)
459 config
= FcConfigGetCurrent ();
463 return FcStrListCreate (config
->fontDirs
);
467 FcConfigAddCacheDir (FcConfig
*config
,
470 return FcStrSetAddFilename (config
->cacheDirs
, d
);
474 FcConfigGetCacheDirs (FcConfig
*config
)
478 config
= FcConfigGetCurrent ();
482 return FcStrListCreate (config
->cacheDirs
);
486 FcConfigAddConfigFile (FcConfig
*config
,
490 FcChar8
*file
= FcConfigFilename (f
);
495 ret
= FcStrSetAdd (config
->configFiles
, file
);
501 FcConfigGetConfigFiles (FcConfig
*config
)
505 config
= FcConfigGetCurrent ();
509 return FcStrListCreate (config
->configFiles
);
513 FcConfigGetCache (FcConfig
*config
)
519 FcConfigGetFonts (FcConfig
*config
,
524 config
= FcConfigGetCurrent ();
528 return config
->fonts
[set
];
532 FcConfigSetFonts (FcConfig
*config
,
536 if (config
->fonts
[set
])
537 FcFontSetDestroy (config
->fonts
[set
]);
538 config
->fonts
[set
] = fonts
;
542 FcConfigGetBlanks (FcConfig
*config
)
546 config
= FcConfigGetCurrent ();
550 return config
->blanks
;
554 FcConfigAddBlank (FcConfig
*config
,
557 FcBlanks
*b
, *freeme
= 0;
562 freeme
= b
= FcBlanksCreate ();
566 if (!FcBlanksAdd (b
, blank
))
569 FcBlanksDestroy (freeme
);
577 FcConfigGetRescanInterval (FcConfig
*config
)
581 config
= FcConfigGetCurrent ();
585 return config
->rescanInterval
;
589 FcConfigSetRescanInterval (FcConfig
*config
, int rescanInterval
)
593 config
= FcConfigGetCurrent ();
597 config
->rescanInterval
= rescanInterval
;
602 * A couple of typos escaped into the library
605 FcConfigGetRescanInverval (FcConfig
*config
)
607 return FcConfigGetRescanInterval (config
);
611 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
613 return FcConfigSetRescanInterval (config
, rescanInterval
);
618 FcConfigAddEdit (FcConfig
*config
,
623 FcSubst
*subst
, **prev
;
629 prev
= &config
->substPattern
;
632 prev
= &config
->substFont
;
635 prev
= &config
->substScan
;
640 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
643 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
644 for (; *prev
; prev
= &(*prev
)->next
);
650 for (t
= test
; t
; t
= t
->next
)
652 if (t
->kind
== FcMatchDefault
)
656 if (config
->maxObjects
< num
)
657 config
->maxObjects
= num
;
658 if (FcDebug () & FC_DBG_EDIT
)
660 printf ("Add Subst ");
661 FcSubstPrint (subst
);
666 typedef struct _FcSubState
{
672 FcConfigPromote (FcValue v
, FcValue u
)
674 if (v
.type
== FcTypeInteger
)
676 v
.type
= FcTypeDouble
;
677 v
.u
.d
= (double) v
.u
.i
;
679 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
681 v
.u
.m
= &FcIdentityMatrix
;
682 v
.type
= FcTypeMatrix
;
684 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
686 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
687 v
.type
= FcTypeLangSet
;
693 FcConfigCompareValue (const FcValue
*left_o
,
695 const FcValue
*right_o
)
697 FcValue left
= FcValueCanonicalize(left_o
);
698 FcValue right
= FcValueCanonicalize(right_o
);
699 FcBool ret
= FcFalse
;
701 left
= FcConfigPromote (left
, right
);
702 right
= FcConfigPromote (right
, left
);
703 if (left
.type
== right
.type
)
707 break; /* FcConfigPromote prevents this from happening */
713 ret
= left
.u
.d
== right
.u
.d
;
716 case FcOpNotContains
:
717 ret
= left
.u
.d
!= right
.u
.d
;
720 ret
= left
.u
.d
< right
.u
.d
;
723 ret
= left
.u
.d
<= right
.u
.d
;
726 ret
= left
.u
.d
> right
.u
.d
;
729 ret
= left
.u
.d
>= right
.u
.d
;
740 ret
= left
.u
.b
== right
.u
.b
;
743 case FcOpNotContains
:
744 ret
= left
.u
.b
!= right
.u
.b
;
754 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
757 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
760 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
762 case FcOpNotContains
:
763 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
774 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
777 case FcOpNotContains
:
778 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
788 /* left contains right if right is a subset of left */
789 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
791 case FcOpNotContains
:
792 /* left contains right if right is a subset of left */
793 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
796 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
799 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
809 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
811 case FcOpNotContains
:
812 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
815 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
818 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
840 ret
= left
.u
.f
== right
.u
.f
;
843 case FcOpNotContains
:
844 ret
= left
.u
.f
!= right
.u
.f
;
854 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
861 #define _FcDoubleFloor(d) ((int) (d))
862 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
863 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
864 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
865 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
866 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
869 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
878 v
.type
= FcTypeInteger
;
882 v
.type
= FcTypeDouble
;
886 v
.type
= FcTypeString
;
891 v
.type
= FcTypeMatrix
;
896 v
.type
= FcTypeCharSet
;
905 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
906 if (r
!= FcResultMatch
)
911 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
912 v
.type
= FcTypeInteger
;
917 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
918 if (vl
.type
== FcTypeBool
)
921 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
923 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
936 case FcOpNotContains
:
938 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
939 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
941 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
951 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
952 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
953 vl
= FcConfigPromote (vl
, vr
);
954 vr
= FcConfigPromote (vr
, vl
);
955 if (vl
.type
== vr
.type
)
961 v
.type
= FcTypeDouble
;
962 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
965 v
.type
= FcTypeDouble
;
966 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
969 v
.type
= FcTypeDouble
;
970 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
973 v
.type
= FcTypeDouble
;
974 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
980 if (v
.type
== FcTypeDouble
&&
981 v
.u
.d
== (double) (int) v
.u
.d
)
983 v
.type
= FcTypeInteger
;
991 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
995 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
1005 v
.type
= FcTypeString
;
1006 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
1007 v
.u
.s
= FcStrStaticName (str
);
1011 v
.type
= FcTypeVoid
;
1014 v
.type
= FcTypeVoid
;
1021 v
.type
= FcTypeMatrix
;
1022 m
= malloc (sizeof (FcMatrix
));
1025 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
1026 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1031 v
.type
= FcTypeVoid
;
1035 v
.type
= FcTypeVoid
;
1042 v
.type
= FcTypeCharSet
;
1043 v
.u
.c
= FcCharSetUnion (vl
.u
.c
, vr
.u
.c
);
1045 v
.type
= FcTypeVoid
;
1048 v
.type
= FcTypeCharSet
;
1049 v
.u
.c
= FcCharSetSubtract (vl
.u
.c
, vr
.u
.c
);
1051 v
.type
= FcTypeVoid
;
1054 v
.type
= FcTypeVoid
;
1059 v
.type
= FcTypeVoid
;
1064 v
.type
= FcTypeVoid
;
1065 FcValueDestroy (vl
);
1066 FcValueDestroy (vr
);
1069 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1072 v
.type
= FcTypeBool
;
1076 v
.type
= FcTypeVoid
;
1079 FcValueDestroy (vl
);
1082 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1088 v
.type
= FcTypeInteger
;
1089 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1092 v
.type
= FcTypeVoid
;
1095 FcValueDestroy (vl
);
1098 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1104 v
.type
= FcTypeInteger
;
1105 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1108 v
.type
= FcTypeVoid
;
1111 FcValueDestroy (vl
);
1114 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1120 v
.type
= FcTypeInteger
;
1121 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1124 v
.type
= FcTypeVoid
;
1127 FcValueDestroy (vl
);
1130 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1136 v
.type
= FcTypeInteger
;
1137 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1140 v
.type
= FcTypeVoid
;
1143 FcValueDestroy (vl
);
1146 v
.type
= FcTypeVoid
;
1152 static FcValueList
*
1153 FcConfigMatchValueList (FcPattern
*p
,
1155 FcValueList
*values
)
1157 FcValueList
*ret
= 0;
1158 FcExpr
*e
= t
->expr
;
1164 /* Compute the value of the match expression */
1165 if (e
->op
== FcOpComma
)
1167 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1168 e
= e
->u
.tree
.right
;
1172 value
= FcConfigEvaluate (p
, e
);
1176 for (v
= values
; v
; v
= FcValueListNext(v
))
1178 /* Compare the pattern value to the match expression value */
1179 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1186 if (t
->qual
== FcQualAll
)
1193 FcValueDestroy (value
);
1198 static FcValueList
*
1199 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1205 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1208 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1209 if (e
->op
== FcOpComma
)
1211 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1212 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1216 l
->value
= FcConfigEvaluate (p
, e
);
1219 l
->binding
= binding
;
1220 if (l
->value
.type
== FcTypeVoid
)
1222 FcValueList
*next
= FcValueListNext(l
);
1224 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1233 FcConfigAdd (FcValueListPtr
*head
,
1234 FcValueList
*position
,
1238 FcValueListPtr
*prev
, last
, v
;
1239 FcValueBinding sameBinding
;
1242 sameBinding
= position
->binding
;
1244 sameBinding
= FcValueBindingWeak
;
1245 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1246 if (v
->binding
== FcValueBindingSame
)
1247 v
->binding
= sameBinding
;
1251 prev
= &position
->next
;
1253 for (prev
= head
; *prev
!= NULL
;
1254 prev
= &(*prev
)->next
)
1261 for (prev
= head
; *prev
!= NULL
;
1262 prev
= &(*prev
)->next
)
1264 if (*prev
== position
)
1271 if (FcDebug () & FC_DBG_EDIT
)
1274 printf ("position not on list\n");
1278 if (FcDebug () & FC_DBG_EDIT
)
1280 printf ("%s list before ", append
? "Append" : "Prepend");
1281 FcValueListPrint (*head
);
1288 while (last
->next
!= NULL
)
1295 if (FcDebug () & FC_DBG_EDIT
)
1297 printf ("%s list after ", append
? "Append" : "Prepend");
1298 FcValueListPrint (*head
);
1306 FcConfigDel (FcValueListPtr
*head
,
1307 FcValueList
*position
)
1309 FcValueListPtr
*prev
;
1311 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1313 if (*prev
== position
)
1315 *prev
= position
->next
;
1316 position
->next
= NULL
;
1317 FcValueListDestroy (position
);
1324 FcConfigPatternAdd (FcPattern
*p
,
1331 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1335 FcConfigAdd (&e
->values
, 0, append
, list
);
1340 * Delete all values associated with a field
1343 FcConfigPatternDel (FcPattern
*p
,
1346 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1349 while (e
->values
!= NULL
)
1350 FcConfigDel (&e
->values
, e
->values
);
1354 FcConfigPatternCanon (FcPattern
*p
,
1357 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1360 if (e
->values
== NULL
)
1361 FcPatternObjectDel (p
, object
);
1365 FcConfigSubstituteWithPat (FcConfig
*config
,
1380 config
= FcConfigGetCurrent ();
1386 case FcMatchPattern
:
1387 s
= config
->substPattern
;
1390 s
= config
->substFont
;
1393 s
= config
->substScan
;
1399 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1400 if (!st
&& config
->maxObjects
)
1402 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1404 if (FcDebug () & FC_DBG_EDIT
)
1406 printf ("FcConfigSubstitute ");
1409 for (; s
; s
= s
->next
)
1412 * Check the tests to see if
1413 * they all match the pattern
1415 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1417 if (FcDebug () & FC_DBG_EDIT
)
1419 printf ("FcConfigSubstitute test ");
1423 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1428 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1432 * If there's no such field in the font,
1433 * then FcQualAll matches while FcQualAny does not
1437 if (t
->qual
== FcQualAll
)
1446 * Check to see if there is a match, mark the location
1447 * to apply match-relative edits
1449 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1452 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1454 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1459 if (FcDebug () & FC_DBG_EDIT
)
1460 printf ("No match\n");
1463 if (FcDebug () & FC_DBG_EDIT
)
1465 printf ("Substitute ");
1468 for (e
= s
->edit
; e
; e
= e
->next
)
1471 * Evaluate the list of expressions
1473 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1475 * Locate any test associated with this field, skipping
1476 * tests associated with the pattern when substituting in
1479 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1481 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1482 t
->object
== e
->object
)
1485 * KLUDGE - the pattern may have been reallocated or
1486 * things may have been inserted or deleted above
1487 * this element by other edits. Go back and find
1490 if (e
!= s
->edit
&& st
[i
].elt
)
1491 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1500 * If there was a test, then replace the matched
1501 * value with the new list of values
1505 FcValueList
*thisValue
= st
[i
].value
;
1506 FcValueList
*nextValue
= thisValue
;
1509 * Append the new list of values after the current value
1511 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1513 * Delete the marked value
1516 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1518 * Adjust any pointers into the value list to ensure
1519 * future edits occur at the same place
1521 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1523 if (st
[i
].value
== thisValue
)
1524 st
[i
].value
= nextValue
;
1528 /* fall through ... */
1529 case FcOpAssignReplace
:
1531 * Delete all of the values and insert
1534 FcConfigPatternDel (p
, e
->object
);
1535 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1537 * Adjust any pointers into the value list as they no
1538 * longer point to anything valid
1542 FcPatternElt
*thisElt
= st
[i
].elt
;
1543 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1545 if (st
[i
].elt
== thisElt
)
1553 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1556 /* fall through ... */
1557 case FcOpPrependFirst
:
1558 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1563 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1566 /* fall through ... */
1567 case FcOpAppendLast
:
1568 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1571 FcValueListDestroy (l
);
1576 * Now go through the pattern and eliminate
1577 * any properties without data
1579 for (e
= s
->edit
; e
; e
= e
->next
)
1580 FcConfigPatternCanon (p
, e
->object
);
1582 if (FcDebug () & FC_DBG_EDIT
)
1584 printf ("FcConfigSubstitute edit");
1588 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1590 if (FcDebug () & FC_DBG_EDIT
)
1592 printf ("FcConfigSubstitute done");
1599 FcConfigSubstitute (FcConfig
*config
,
1603 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1606 #if defined (_WIN32)
1608 # define WIN32_LEAN_AND_MEAN
1609 # define WIN32_EXTRA_LEAN
1610 # include <windows.h>
1612 static FcChar8 fontconfig_path
[1000] = "";
1614 # if (defined (PIC) || defined (DLL_EXPORT))
1617 DllMain (HINSTANCE hinstDLL
,
1623 switch (fdwReason
) {
1624 case DLL_PROCESS_ATTACH
:
1625 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1626 sizeof (fontconfig_path
)))
1629 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1630 * assume it's a Unix-style installation tree, and use
1631 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1632 * folder where the DLL is as FONTCONFIG_PATH.
1634 p
= strrchr (fontconfig_path
, '\\');
1638 p
= strrchr (fontconfig_path
, '\\');
1639 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1640 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1642 strcat (fontconfig_path
, "\\etc\\fonts");
1645 fontconfig_path
[0] = '\0';
1655 #undef FONTCONFIG_PATH
1656 #define FONTCONFIG_PATH fontconfig_path
1658 #endif /* !_WIN32 */
1660 #ifndef FONTCONFIG_FILE
1661 #define FONTCONFIG_FILE "fonts.conf"
1665 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1670 dir
= (FcChar8
*) "";
1671 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1675 strcpy ((char *) path
, (const char *) dir
);
1676 /* make sure there's a single separator */
1678 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1679 path
[strlen((char *) path
)-1] != '\\')) &&
1682 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1683 strcat ((char *) path
, "\\");
1685 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1686 strcat ((char *) path
, "/");
1688 strcat ((char *) path
, (char *) file
);
1690 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1691 if (access ((char *) path
, R_OK
) == 0)
1699 FcConfigGetPath (void)
1702 FcChar8
*env
, *e
, *colon
;
1707 npath
= 2; /* default dir + null */
1708 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1714 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1717 path
= calloc (npath
, sizeof (FcChar8
*));
1727 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1729 colon
= e
+ strlen ((char *) e
);
1730 path
[i
] = malloc (colon
- e
+ 1);
1733 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1734 path
[i
][colon
- e
] = '\0';
1744 if (fontconfig_path
[0] == '\0')
1747 if(!GetModuleFileName(NULL
, fontconfig_path
, sizeof(fontconfig_path
)))
1749 p
= strrchr (fontconfig_path
, '\\');
1751 strcat (fontconfig_path
, "\\fonts");
1754 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1755 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1758 strcpy ((char *) path
[i
], (const char *) dir
);
1762 for (i
= 0; path
[i
]; i
++)
1770 FcConfigFreePath (FcChar8
**path
)
1774 for (p
= path
; *p
; p
++)
1779 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1784 if (_FcConfigHomeEnabled
)
1786 char *home
= getenv ("HOME");
1790 home
= getenv ("USERPROFILE");
1793 return (FcChar8
*) home
;
1799 FcConfigEnableHome (FcBool enable
)
1801 FcBool prev
= _FcConfigHomeEnabled
;
1802 _FcConfigHomeEnabled
= enable
;
1807 FcConfigFilename (const FcChar8
*url
)
1809 FcChar8
*file
, *dir
, **path
, **p
;
1813 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1815 url
= (FcChar8
*) FONTCONFIG_FILE
;
1820 if (isalpha (*url
) &&
1822 (url
[2] == '/' || url
[2] == '\\'))
1828 dir
= FcConfigHome ();
1830 file
= FcConfigFileExists (dir
, url
+ 1);
1839 file
= FcConfigFileExists (0, url
);
1842 path
= FcConfigGetPath ();
1845 for (p
= path
; *p
; p
++)
1847 file
= FcConfigFileExists (*p
, url
);
1851 FcConfigFreePath (path
);
1858 * Manage the application-specific fonts
1862 FcConfigAppFontAddFile (FcConfig
*config
,
1863 const FcChar8
*file
)
1872 config
= FcConfigGetCurrent ();
1877 subdirs
= FcStrSetCreate ();
1881 set
= FcConfigGetFonts (config
, FcSetApplication
);
1884 set
= FcFontSetCreate ();
1887 FcStrSetDestroy (subdirs
);
1890 FcConfigSetFonts (config
, set
, FcSetApplication
);
1893 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1895 FcStrSetDestroy (subdirs
);
1898 if ((sublist
= FcStrListCreate (subdirs
)))
1900 while ((subdir
= FcStrListNext (sublist
)))
1902 FcConfigAppFontAddDir (config
, subdir
);
1904 FcStrListDone (sublist
);
1906 FcStrSetDestroy (subdirs
);
1911 FcConfigAppFontAddDir (FcConfig
*config
,
1919 config
= FcConfigGetCurrent ();
1924 dirs
= FcStrSetCreate ();
1928 set
= FcConfigGetFonts (config
, FcSetApplication
);
1931 set
= FcFontSetCreate ();
1934 FcStrSetDestroy (dirs
);
1937 FcConfigSetFonts (config
, set
, FcSetApplication
);
1940 FcStrSetAddFilename (dirs
, dir
);
1942 if (!FcConfigAddDirList (config
, FcSetApplication
, dirs
))
1944 FcStrSetDestroy (dirs
);
1947 FcStrSetDestroy (dirs
);
1952 FcConfigAppFontClear (FcConfig
*config
)
1956 config
= FcConfigGetCurrent ();
1961 FcConfigSetFonts (config
, 0, FcSetApplication
);
1965 * Manage filename-based font source selectors
1969 FcConfigGlobAdd (FcConfig
*config
,
1970 const FcChar8
*glob
,
1973 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1975 return FcStrSetAdd (set
, glob
);
1979 FcConfigGlobMatch (const FcChar8
*glob
,
1980 const FcChar8
*string
)
1984 while ((c
= *glob
++))
1988 /* short circuit common case */
1991 /* short circuit another common case */
1992 if (strchr ((char *) glob
, '*') == 0)
1993 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1996 if (FcConfigGlobMatch (glob
, string
))
2002 if (*string
++ == '\0')
2011 return *string
== '\0';
2015 FcConfigGlobsMatch (const FcStrSet
*globs
,
2016 const FcChar8
*string
)
2020 for (i
= 0; i
< globs
->num
; i
++)
2021 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
2027 FcConfigAcceptFilename (FcConfig
*config
,
2028 const FcChar8
*filename
)
2030 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
2032 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
2038 * Manage font-pattern based font source selectors
2042 FcConfigPatternsAdd (FcConfig
*config
,
2046 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
2048 return FcFontSetAdd (set
, pattern
);
2052 FcConfigPatternsMatch (const FcFontSet
*patterns
,
2053 const FcPattern
*font
)
2057 for (i
= 0; i
< patterns
->nfont
; i
++)
2058 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
2064 FcConfigAcceptFont (FcConfig
*config
,
2065 const FcPattern
*font
)
2067 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
2069 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))
2074 #include "fcaliastail.h"