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
)
84 config
->rootDir
= FcStrCopy ((const FcChar8
*) "");
88 config
->substPattern
= 0;
89 config
->substFont
= 0;
90 config
->substScan
= 0;
91 config
->maxObjects
= 0;
92 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
93 config
->fonts
[set
] = 0;
95 config
->rescanTime
= time(0);
96 config
->rescanInterval
= 30;
98 config
->expr_pool
= NULL
;
105 FcFontSetDestroy (config
->rejectPatterns
);
107 FcFontSetDestroy (config
->acceptPatterns
);
109 FcStrSetDestroy (config
->rejectGlobs
);
111 FcStrSetDestroy (config
->acceptGlobs
);
113 FcStrSetDestroy (config
->fontDirs
);
115 FcStrSetDestroy (config
->configFiles
);
117 FcStrSetDestroy (config
->configDirs
);
120 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
126 FcConfigNewestFile (FcConfig
*config
, FcStrSet
*files
)
128 FcStrList
*list
= FcStrListCreate (files
);
129 FcFileTime newest
= { 0, FcFalse
};
135 while ((file
= FcStrListNext (list
)))
136 if (FcStat (config
, file
, &statb
) == 0)
137 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
140 newest
.time
= statb
.st_mtime
;
142 FcStrListDone (list
);
148 FcConfigUptoDate (FcConfig
*config
)
150 FcFileTime config_time
, config_dir_time
, font_time
;
151 time_t now
= time(0);
154 config
= FcConfigGetCurrent ();
158 config_time
= FcConfigNewestFile (config
, config
->configFiles
);
159 config_dir_time
= FcConfigNewestFile (config
, config
->configDirs
);
160 font_time
= FcConfigNewestFile (config
, config
->fontDirs
);
161 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
162 (config_dir_time
.set
&& (config_dir_time
.time
- config
->rescanTime
) > 0) ||
163 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
165 /* We need to check for potential clock problems here (OLPC ticket #6046) */
166 if ((config_time
.set
&& (config_time
.time
- now
) > 0) ||
167 (config_dir_time
.set
&& (config_dir_time
.time
- now
) > 0) ||
168 (font_time
.set
&& (font_time
.time
- now
) > 0))
171 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected\n");
172 config
->rescanTime
= now
;
178 config
->rescanTime
= now
;
183 FcSubstDestroy (FcSubst
*s
)
191 FcTestDestroy (s
->test
);
193 FcEditDestroy (s
->edit
);
195 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
201 FcConfigAllocExpr (FcConfig
*config
)
203 if (!config
->expr_pool
|| config
->expr_pool
->next
== config
->expr_pool
->end
)
205 FcExprPage
*new_page
;
207 new_page
= malloc (sizeof (FcExprPage
));
210 FcMemAlloc (FC_MEM_EXPR
, sizeof (FcExprPage
));
212 new_page
->next_page
= config
->expr_pool
;
213 new_page
->next
= new_page
->exprs
;
214 config
->expr_pool
= new_page
;
217 return config
->expr_pool
->next
++;
221 FcConfigReference (FcConfig
*config
)
225 config
= FcConfigGetCurrent ();
236 FcConfigDestroy (FcConfig
*config
)
241 if (--config
->ref
> 0)
244 if (config
== _fcConfig
)
247 FcStrSetDestroy (config
->configDirs
);
248 FcStrSetDestroy (config
->fontDirs
);
249 FcStrSetDestroy (config
->cacheDirs
);
250 FcStrSetDestroy (config
->configFiles
);
251 FcStrSetDestroy (config
->acceptGlobs
);
252 FcStrSetDestroy (config
->rejectGlobs
);
253 FcFontSetDestroy (config
->acceptPatterns
);
254 FcFontSetDestroy (config
->rejectPatterns
);
257 FcBlanksDestroy (config
->blanks
);
259 FcSubstDestroy (config
->substPattern
);
260 FcSubstDestroy (config
->substFont
);
261 FcSubstDestroy (config
->substScan
);
262 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
263 if (config
->fonts
[set
])
264 FcFontSetDestroy (config
->fonts
[set
]);
266 page
= config
->expr_pool
;
269 FcExprPage
*next
= page
->next_page
;
270 FcMemFree (FC_MEM_EXPR
, sizeof (FcExprPage
));
276 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
280 * Add cache to configuration, adding fonts and directories
284 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
,
285 FcSetName set
, FcStrSet
*dirSet
)
294 fs
= FcCacheSet (cache
);
299 for (i
= 0; i
< fs
->nfont
; i
++)
301 FcPattern
*font
= FcFontSetFont (fs
, i
);
305 * Check to see if font is banned by filename
307 if (FcPatternObjectGetString (font
, FC_FILE_OBJECT
,
308 0, &font_file
) == FcResultMatch
&&
309 !FcConfigAcceptFilename (config
, font_file
))
315 * Check to see if font is banned by pattern
317 if (!FcConfigAcceptFont (config
, font
))
321 FcFontSetAdd (config
->fonts
[set
], font
);
323 FcDirCacheReference (cache
, nref
);
329 dirs
= FcCacheDirs (cache
);
332 for (i
= 0; i
< cache
->dirs_count
; i
++)
334 FcChar8
*dir
= FcOffsetToPtr (dirs
, dirs
[i
], FcChar8
);
335 if (FcConfigAcceptFilename (config
, dir
))
336 FcStrSetAddFilename (dirSet
, dir
);
343 FcConfigAddDirList (FcConfig
*config
, FcSetName set
, FcStrSet
*dirSet
)
349 dirlist
= FcStrListCreate (dirSet
);
353 while ((dir
= FcStrListNext (dirlist
)))
355 if (FcDebug () & FC_DBG_FONTSET
)
356 printf ("adding fonts from%s\n", dir
);
357 cache
= FcDirCacheRead (dir
, FcFalse
, config
);
360 FcConfigAddCache (config
, cache
, set
, dirSet
);
361 FcDirCacheUnload (cache
);
363 FcStrListDone (dirlist
);
368 * Scan the current list of directories in the configuration
369 * and build the set of available fonts.
373 FcConfigBuildFonts (FcConfig
*config
)
379 config
= FcConfigGetCurrent ();
384 fonts
= FcFontSetCreate ();
388 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
390 if (!FcConfigAddDirList (config
, FcSetSystem
, config
->fontDirs
))
392 if (FcDebug () & FC_DBG_FONTSET
)
393 FcFontSetPrint (fonts
);
398 FcConfigSetCurrent (FcConfig
*config
)
400 if (config
== _fcConfig
)
404 if (!FcConfigBuildFonts (config
))
408 FcConfigDestroy (_fcConfig
);
414 FcConfigGetCurrent (void)
423 FcConfigAddConfigDir (FcConfig
*config
,
426 return FcStrSetAddFilename (config
->configDirs
, d
);
430 FcConfigGetConfigDirs (FcConfig
*config
)
434 config
= FcConfigGetCurrent ();
438 return FcStrListCreate (config
->configDirs
);
442 FcConfigAddFontDir (FcConfig
*config
,
445 return FcStrSetAddFilename (config
->fontDirs
, d
);
449 FcConfigAddDir (FcConfig
*config
,
452 return (FcConfigAddConfigDir (config
, d
) &&
453 FcConfigAddFontDir (config
, d
));
457 FcConfigGetFontDirs (FcConfig
*config
)
461 config
= FcConfigGetCurrent ();
465 return FcStrListCreate (config
->fontDirs
);
469 FcConfigAddCacheDir (FcConfig
*config
,
472 return FcStrSetAddFilename (config
->cacheDirs
, d
);
476 FcConfigGetCacheDirs (FcConfig
*config
)
480 config
= FcConfigGetCurrent ();
484 return FcStrListCreate (config
->cacheDirs
);
488 FcConfigAddConfigFile (FcConfig
*config
,
492 FcChar8
*file
= FcConfigFilename2 (config
, f
);
497 ret
= FcStrSetAdd (config
->configFiles
, file
);
503 FcConfigGetConfigFiles (FcConfig
*config
)
507 config
= FcConfigGetCurrent ();
511 return FcStrListCreate (config
->configFiles
);
515 FcConfigGetCache (FcConfig
*config
)
521 FcConfigGetFonts (FcConfig
*config
,
526 config
= FcConfigGetCurrent ();
530 return config
->fonts
[set
];
534 FcConfigSetFonts (FcConfig
*config
,
538 if (config
->fonts
[set
])
539 FcFontSetDestroy (config
->fonts
[set
]);
540 config
->fonts
[set
] = fonts
;
544 FcConfigGetBlanks (FcConfig
*config
)
548 config
= FcConfigGetCurrent ();
552 return config
->blanks
;
556 FcConfigAddBlank (FcConfig
*config
,
559 FcBlanks
*b
, *freeme
= 0;
564 freeme
= b
= FcBlanksCreate ();
568 if (!FcBlanksAdd (b
, blank
))
571 FcBlanksDestroy (freeme
);
579 FcConfigGetRescanInterval (FcConfig
*config
)
583 config
= FcConfigGetCurrent ();
587 return config
->rescanInterval
;
591 FcConfigSetRescanInterval (FcConfig
*config
, int rescanInterval
)
595 config
= FcConfigGetCurrent ();
599 config
->rescanInterval
= rescanInterval
;
604 * A couple of typos escaped into the library
607 FcConfigGetRescanInverval (FcConfig
*config
)
609 return FcConfigGetRescanInterval (config
);
613 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
615 return FcConfigSetRescanInterval (config
, rescanInterval
);
620 FcConfigAddEdit (FcConfig
*config
,
625 FcSubst
*subst
, **prev
;
631 prev
= &config
->substPattern
;
634 prev
= &config
->substFont
;
637 prev
= &config
->substScan
;
642 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
645 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
646 for (; *prev
; prev
= &(*prev
)->next
);
652 for (t
= test
; t
; t
= t
->next
)
654 if (t
->kind
== FcMatchDefault
)
658 if (config
->maxObjects
< num
)
659 config
->maxObjects
= num
;
660 if (FcDebug () & FC_DBG_EDIT
)
662 printf ("Add Subst ");
663 FcSubstPrint (subst
);
668 typedef struct _FcSubState
{
674 FcConfigPromote (FcValue v
, FcValue u
)
676 if (v
.type
== FcTypeInteger
)
678 v
.type
= FcTypeDouble
;
679 v
.u
.d
= (double) v
.u
.i
;
681 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
683 v
.u
.m
= &FcIdentityMatrix
;
684 v
.type
= FcTypeMatrix
;
686 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
688 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
689 v
.type
= FcTypeLangSet
;
695 FcConfigCompareValue (const FcValue
*left_o
,
697 const FcValue
*right_o
)
699 FcValue left
= FcValueCanonicalize(left_o
);
700 FcValue right
= FcValueCanonicalize(right_o
);
701 FcBool ret
= FcFalse
;
703 left
= FcConfigPromote (left
, right
);
704 right
= FcConfigPromote (right
, left
);
705 if (left
.type
== right
.type
)
709 break; /* FcConfigPromote prevents this from happening */
715 ret
= left
.u
.d
== right
.u
.d
;
718 case FcOpNotContains
:
719 ret
= left
.u
.d
!= right
.u
.d
;
722 ret
= left
.u
.d
< right
.u
.d
;
725 ret
= left
.u
.d
<= right
.u
.d
;
728 ret
= left
.u
.d
> right
.u
.d
;
731 ret
= left
.u
.d
>= right
.u
.d
;
742 ret
= left
.u
.b
== right
.u
.b
;
745 case FcOpNotContains
:
746 ret
= left
.u
.b
!= right
.u
.b
;
756 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
759 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
762 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
764 case FcOpNotContains
:
765 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
776 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
779 case FcOpNotContains
:
780 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
790 /* left contains right if right is a subset of left */
791 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
793 case FcOpNotContains
:
794 /* left contains right if right is a subset of left */
795 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
798 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
801 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
811 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
813 case FcOpNotContains
:
814 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
817 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
820 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
842 ret
= left
.u
.f
== right
.u
.f
;
845 case FcOpNotContains
:
846 ret
= left
.u
.f
!= right
.u
.f
;
856 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
863 #define _FcDoubleFloor(d) ((int) (d))
864 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
865 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
866 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
867 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
868 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
871 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
880 v
.type
= FcTypeInteger
;
884 v
.type
= FcTypeDouble
;
888 v
.type
= FcTypeString
;
893 v
.type
= FcTypeMatrix
;
898 v
.type
= FcTypeCharSet
;
903 v
.type
= FcTypeLangSet
;
912 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
913 if (r
!= FcResultMatch
)
918 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
919 v
.type
= FcTypeInteger
;
924 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
925 if (vl
.type
== FcTypeBool
)
928 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
930 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
943 case FcOpNotContains
:
945 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
946 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
948 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
958 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
959 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
960 vl
= FcConfigPromote (vl
, vr
);
961 vr
= FcConfigPromote (vr
, vl
);
962 if (vl
.type
== vr
.type
)
968 v
.type
= FcTypeDouble
;
969 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
972 v
.type
= FcTypeDouble
;
973 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
976 v
.type
= FcTypeDouble
;
977 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
980 v
.type
= FcTypeDouble
;
981 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
987 if (v
.type
== FcTypeDouble
&&
988 v
.u
.d
== (double) (int) v
.u
.d
)
990 v
.type
= FcTypeInteger
;
998 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
1001 v
.type
= FcTypeBool
;
1002 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
1005 v
.type
= FcTypeVoid
;
1012 v
.type
= FcTypeString
;
1013 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
1014 v
.u
.s
= FcStrStaticName (str
);
1018 v
.type
= FcTypeVoid
;
1021 v
.type
= FcTypeVoid
;
1028 v
.type
= FcTypeMatrix
;
1029 m
= malloc (sizeof (FcMatrix
));
1032 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
1033 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
1038 v
.type
= FcTypeVoid
;
1042 v
.type
= FcTypeVoid
;
1049 v
.type
= FcTypeCharSet
;
1050 v
.u
.c
= FcCharSetUnion (vl
.u
.c
, vr
.u
.c
);
1052 v
.type
= FcTypeVoid
;
1055 v
.type
= FcTypeCharSet
;
1056 v
.u
.c
= FcCharSetSubtract (vl
.u
.c
, vr
.u
.c
);
1058 v
.type
= FcTypeVoid
;
1061 v
.type
= FcTypeVoid
;
1068 v
.type
= FcTypeLangSet
;
1069 v
.u
.l
= FcLangSetUnion (vl
.u
.l
, vr
.u
.l
);
1071 v
.type
= FcTypeVoid
;
1074 v
.type
= FcTypeLangSet
;
1075 v
.u
.l
= FcLangSetSubtract (vl
.u
.l
, vr
.u
.l
);
1077 v
.type
= FcTypeVoid
;
1080 v
.type
= FcTypeVoid
;
1085 v
.type
= FcTypeVoid
;
1090 v
.type
= FcTypeVoid
;
1091 FcValueDestroy (vl
);
1092 FcValueDestroy (vr
);
1095 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1098 v
.type
= FcTypeBool
;
1102 v
.type
= FcTypeVoid
;
1105 FcValueDestroy (vl
);
1108 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1114 v
.type
= FcTypeInteger
;
1115 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
1118 v
.type
= FcTypeVoid
;
1121 FcValueDestroy (vl
);
1124 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1130 v
.type
= FcTypeInteger
;
1131 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1134 v
.type
= FcTypeVoid
;
1137 FcValueDestroy (vl
);
1140 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1146 v
.type
= FcTypeInteger
;
1147 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1150 v
.type
= FcTypeVoid
;
1153 FcValueDestroy (vl
);
1156 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1162 v
.type
= FcTypeInteger
;
1163 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1166 v
.type
= FcTypeVoid
;
1169 FcValueDestroy (vl
);
1172 v
.type
= FcTypeVoid
;
1178 static FcValueList
*
1179 FcConfigMatchValueList (FcPattern
*p
,
1181 FcValueList
*values
)
1183 FcValueList
*ret
= 0;
1184 FcExpr
*e
= t
->expr
;
1190 /* Compute the value of the match expression */
1191 if (e
->op
== FcOpComma
)
1193 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1194 e
= e
->u
.tree
.right
;
1198 value
= FcConfigEvaluate (p
, e
);
1202 for (v
= values
; v
; v
= FcValueListNext(v
))
1204 /* Compare the pattern value to the match expression value */
1205 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1212 if (t
->qual
== FcQualAll
)
1219 FcValueDestroy (value
);
1224 static FcValueList
*
1225 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1231 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1234 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1235 if (e
->op
== FcOpComma
)
1237 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1238 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1242 l
->value
= FcConfigEvaluate (p
, e
);
1245 l
->binding
= binding
;
1246 if (l
->value
.type
== FcTypeVoid
)
1248 FcValueList
*next
= FcValueListNext(l
);
1250 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1259 FcConfigAdd (FcValueListPtr
*head
,
1260 FcValueList
*position
,
1264 FcValueListPtr
*prev
, last
, v
;
1265 FcValueBinding sameBinding
;
1268 sameBinding
= position
->binding
;
1270 sameBinding
= FcValueBindingWeak
;
1271 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1272 if (v
->binding
== FcValueBindingSame
)
1273 v
->binding
= sameBinding
;
1277 prev
= &position
->next
;
1279 for (prev
= head
; *prev
!= NULL
;
1280 prev
= &(*prev
)->next
)
1287 for (prev
= head
; *prev
!= NULL
;
1288 prev
= &(*prev
)->next
)
1290 if (*prev
== position
)
1297 if (FcDebug () & FC_DBG_EDIT
)
1300 printf ("position not on list\n");
1304 if (FcDebug () & FC_DBG_EDIT
)
1306 printf ("%s list before ", append
? "Append" : "Prepend");
1307 FcValueListPrint (*head
);
1314 while (last
->next
!= NULL
)
1321 if (FcDebug () & FC_DBG_EDIT
)
1323 printf ("%s list after ", append
? "Append" : "Prepend");
1324 FcValueListPrint (*head
);
1332 FcConfigDel (FcValueListPtr
*head
,
1333 FcValueList
*position
)
1335 FcValueListPtr
*prev
;
1337 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1339 if (*prev
== position
)
1341 *prev
= position
->next
;
1342 position
->next
= NULL
;
1343 FcValueListDestroy (position
);
1350 FcConfigPatternAdd (FcPattern
*p
,
1357 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1361 FcConfigAdd (&e
->values
, 0, append
, list
);
1366 * Delete all values associated with a field
1369 FcConfigPatternDel (FcPattern
*p
,
1372 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1375 while (e
->values
!= NULL
)
1376 FcConfigDel (&e
->values
, e
->values
);
1380 FcConfigPatternCanon (FcPattern
*p
,
1383 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1386 if (e
->values
== NULL
)
1387 FcPatternObjectDel (p
, object
);
1391 FcConfigSubstituteWithPat (FcConfig
*config
,
1406 config
= FcConfigGetCurrent ();
1412 case FcMatchPattern
:
1413 s
= config
->substPattern
;
1416 s
= config
->substFont
;
1419 s
= config
->substScan
;
1425 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1426 if (!st
&& config
->maxObjects
)
1428 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1430 if (FcDebug () & FC_DBG_EDIT
)
1432 printf ("FcConfigSubstitute ");
1435 for (; s
; s
= s
->next
)
1438 * Check the tests to see if
1439 * they all match the pattern
1441 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1443 if (FcDebug () & FC_DBG_EDIT
)
1445 printf ("FcConfigSubstitute test ");
1449 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1454 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1458 * If there's no such field in the font,
1459 * then FcQualAll matches while FcQualAny does not
1463 if (t
->qual
== FcQualAll
)
1472 * Check to see if there is a match, mark the location
1473 * to apply match-relative edits
1475 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1478 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1480 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1485 if (FcDebug () & FC_DBG_EDIT
)
1486 printf ("No match\n");
1489 if (FcDebug () & FC_DBG_EDIT
)
1491 printf ("Substitute ");
1494 for (e
= s
->edit
; e
; e
= e
->next
)
1497 * Evaluate the list of expressions
1499 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1501 * Locate any test associated with this field, skipping
1502 * tests associated with the pattern when substituting in
1505 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1507 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1508 t
->object
== e
->object
)
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
= FcPatternObjectFindElt (p
, t
->object
);
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
;
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
->object
);
1561 FcConfigPatternAdd (p
, e
->object
, 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
->object
, l
, FcFalse
);
1589 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1592 /* fall through ... */
1593 case FcOpAppendLast
:
1594 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1597 FcValueListDestroy (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
->object
);
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)
1634 # define WIN32_LEAN_AND_MEAN
1635 # define WIN32_EXTRA_LEAN
1636 # include <windows.h>
1638 static FcChar8 fontconfig_path
[1000] = "";
1640 # if (defined (PIC) || defined (DLL_EXPORT))
1643 DllMain (HINSTANCE hinstDLL
,
1649 switch (fdwReason
) {
1650 case DLL_PROCESS_ATTACH
:
1651 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1652 sizeof (fontconfig_path
)))
1655 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1656 * assume it's a Unix-style installation tree, and use
1657 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1658 * folder where the DLL is as FONTCONFIG_PATH.
1660 p
= strrchr (fontconfig_path
, '\\');
1664 p
= strrchr (fontconfig_path
, '\\');
1665 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1666 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1668 strcat (fontconfig_path
, "\\etc\\fonts");
1671 fontconfig_path
[0] = '\0';
1681 #undef FONTCONFIG_PATH
1682 #define FONTCONFIG_PATH fontconfig_path
1684 #endif /* !_WIN32 */
1686 #ifndef FONTCONFIG_FILE
1687 #define FONTCONFIG_FILE "fonts.conf"
1691 FcConfigFileExists (FcConfig
*config
, const FcChar8
*dir
, const FcChar8
*file
)
1696 dir
= (const FcChar8
*) "";
1698 path
= FcStrPathPlus (FcConfigGetRoot (config
), dir
, file
, NULL
);
1699 if (access ((char *) path
, R_OK
) == 0)
1702 return FcStrPathPlus (dir
, file
, NULL
);
1710 FcConfigGetPath (void)
1713 FcChar8
*env
, *e
, *colon
;
1718 npath
= 2; /* default dir + null */
1719 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1725 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1728 path
= calloc (npath
, sizeof (FcChar8
*));
1738 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1740 colon
= e
+ strlen ((char *) e
);
1741 path
[i
] = malloc (colon
- e
+ 1);
1744 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1745 path
[i
][colon
- e
] = '\0';
1755 if (fontconfig_path
[0] == '\0')
1758 if(!GetModuleFileName(NULL
, fontconfig_path
, sizeof(fontconfig_path
)))
1760 p
= strrchr (fontconfig_path
, '\\');
1762 strcat (fontconfig_path
, "\\fonts");
1765 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1766 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1769 strcpy ((char *) path
[i
], (const char *) dir
);
1773 for (i
= 0; path
[i
]; i
++)
1781 FcConfigFreePath (FcChar8
**path
)
1785 for (p
= path
; *p
; p
++)
1790 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1795 if (_FcConfigHomeEnabled
)
1797 char *home
= getenv ("HOME");
1801 home
= getenv ("USERPROFILE");
1804 return (FcChar8
*) home
;
1810 FcConfigEnableHome (FcBool enable
)
1812 FcBool prev
= _FcConfigHomeEnabled
;
1813 _FcConfigHomeEnabled
= enable
;
1818 FcConfigFilename (const FcChar8
*url
)
1820 return FcConfigFilename2 (FcConfigGetCurrent (), url
);
1824 FcConfigFilename2 (FcConfig
*config
, const FcChar8
*url
)
1826 FcChar8
*file
, *dir
, **path
, **p
;
1830 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1832 url
= (FcChar8
*) FONTCONFIG_FILE
;
1837 if (isalpha (*url
) &&
1839 (url
[2] == '/' || url
[2] == '\\'))
1845 dir
= FcConfigHome ();
1847 file
= FcConfigFileExists (config
, dir
, url
+ 1);
1856 file
= FcConfigFileExists (config
, 0, url
);
1859 path
= FcConfigGetPath ();
1862 for (p
= path
; *p
; p
++)
1864 file
= FcConfigFileExists (config
, *p
, url
);
1868 FcConfigFreePath (path
);
1875 * Manage the application-specific fonts
1879 FcConfigAppFontAddFile (FcConfig
*config
,
1880 const FcChar8
*file
)
1889 config
= FcConfigGetCurrent ();
1894 subdirs
= FcStrSetCreate ();
1898 set
= FcConfigGetFonts (config
, FcSetApplication
);
1901 set
= FcFontSetCreate ();
1904 FcStrSetDestroy (subdirs
);
1907 FcConfigSetFonts (config
, set
, FcSetApplication
);
1910 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, config
))
1912 FcStrSetDestroy (subdirs
);
1915 if ((sublist
= FcStrListCreate (subdirs
)))
1917 while ((subdir
= FcStrListNext (sublist
)))
1919 FcConfigAppFontAddDir (config
, subdir
);
1921 FcStrListDone (sublist
);
1923 FcStrSetDestroy (subdirs
);
1928 FcConfigAppFontAddDir (FcConfig
*config
,
1936 config
= FcConfigGetCurrent ();
1941 dirs
= FcStrSetCreate ();
1945 set
= FcConfigGetFonts (config
, FcSetApplication
);
1948 set
= FcFontSetCreate ();
1951 FcStrSetDestroy (dirs
);
1954 FcConfigSetFonts (config
, set
, FcSetApplication
);
1957 FcStrSetAddFilename (dirs
, dir
);
1959 if (!FcConfigAddDirList (config
, FcSetApplication
, dirs
))
1961 FcStrSetDestroy (dirs
);
1964 FcStrSetDestroy (dirs
);
1969 FcConfigAppFontClear (FcConfig
*config
)
1973 config
= FcConfigGetCurrent ();
1978 FcConfigSetFonts (config
, 0, FcSetApplication
);
1982 * Manage filename-based font source selectors
1986 FcConfigGlobAdd (FcConfig
*config
,
1987 const FcChar8
*glob
,
1990 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1992 return FcStrSetAdd (set
, glob
);
1996 FcConfigGlobMatch (const FcChar8
*glob
,
1997 const FcChar8
*string
)
2001 while ((c
= *glob
++))
2005 /* short circuit common case */
2008 /* short circuit another common case */
2009 if (strchr ((char *) glob
, '*') == 0)
2010 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
2013 if (FcConfigGlobMatch (glob
, string
))
2019 if (*string
++ == '\0')
2028 return *string
== '\0';
2032 FcConfigGlobsMatch (const FcStrSet
*globs
,
2033 const FcChar8
*string
)
2037 for (i
= 0; i
< globs
->num
; i
++)
2038 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
2044 FcConfigAcceptFilename (FcConfig
*config
,
2045 const FcChar8
*filename
)
2047 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
2049 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
2055 * Manage font-pattern based font source selectors
2059 FcConfigPatternsAdd (FcConfig
*config
,
2063 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
2065 return FcFontSetAdd (set
, pattern
);
2069 FcConfigPatternsMatch (const FcFontSet
*patterns
,
2070 const FcPattern
*font
)
2074 for (i
= 0; i
< patterns
->nfont
; i
++)
2075 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
2081 FcConfigAcceptFont (FcConfig
*config
,
2082 const FcPattern
*font
)
2084 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
2086 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))
2092 FcConfigSetRoot (FcConfig
*config
,
2093 const FcChar8
*path
)
2095 FcStrFree (config
->rootDir
);
2096 config
->rootDir
= FcStrCopy (path
);
2100 FcConfigGetRoot (FcConfig
*config
)
2102 return config
->rootDir
;
2106 FcConfigGetRootPlus (FcConfig
*config
,
2107 const FcChar8
*path
)
2110 * Since we have older funcs which do not take a config, it
2111 * might be NULL, so handle that as well rather than crash.
2113 if (!config
|| !config
->rootDir
)
2115 return FcStrPathPlus (config
->rootDir
, path
, NULL
);
2119 #include "fcaliastail.h"