2 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
29 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
35 #if defined (_WIN32) && !defined (R_OK)
47 config
= malloc (sizeof (FcConfig
));
50 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
52 config
->configDirs
= FcStrSetCreate ();
53 if (!config
->configDirs
)
56 config
->configFiles
= FcStrSetCreate ();
57 if (!config
->configFiles
)
60 config
->fontDirs
= FcStrSetCreate ();
61 if (!config
->fontDirs
)
64 config
->acceptGlobs
= FcStrSetCreate ();
65 if (!config
->acceptGlobs
)
68 config
->rejectGlobs
= FcStrSetCreate ();
69 if (!config
->rejectGlobs
)
72 config
->acceptPatterns
= FcFontSetCreate ();
73 if (!config
->acceptPatterns
)
76 config
->rejectPatterns
= FcFontSetCreate ();
77 if (!config
->rejectPatterns
)
80 config
->cacheDirs
= FcStrSetCreate ();
81 if (!config
->cacheDirs
)
86 config
->substPattern
= 0;
87 config
->substFont
= 0;
88 config
->maxObjects
= 0;
89 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
90 config
->fonts
[set
] = 0;
92 config
->caches
= NULL
;
94 config
->rescanTime
= time(0);
95 config
->rescanInterval
= 30;
100 FcFontSetDestroy (config
->rejectPatterns
);
102 FcFontSetDestroy (config
->acceptPatterns
);
104 FcStrSetDestroy (config
->rejectGlobs
);
106 FcStrSetDestroy (config
->acceptGlobs
);
108 FcStrSetDestroy (config
->fontDirs
);
110 FcStrSetDestroy (config
->configFiles
);
112 FcStrSetDestroy (config
->configDirs
);
115 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
121 FcConfigNewestFile (FcStrSet
*files
)
123 FcStrList
*list
= FcStrListCreate (files
);
124 FcFileTime newest
= { 0, FcFalse
};
130 while ((file
= FcStrListNext (list
)))
131 if (stat ((char *) file
, &statb
) == 0)
132 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
135 newest
.time
= statb
.st_mtime
;
137 FcStrListDone (list
);
143 FcConfigModifiedTime (FcConfig
*config
)
147 FcFileTime v
= { 0, FcFalse
};
148 config
= FcConfigGetCurrent ();
152 return FcConfigNewestFile (config
->configFiles
);
156 FcConfigUptoDate (FcConfig
*config
)
158 FcFileTime config_time
, font_time
;
159 time_t now
= time(0);
162 config
= FcConfigGetCurrent ();
166 config_time
= FcConfigNewestFile (config
->configFiles
);
167 font_time
= FcConfigNewestFile (config
->fontDirs
);
168 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
169 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
173 config
->rescanTime
= now
;
178 FcSubstDestroy (FcSubst
*s
)
186 FcTestDestroy (s
->test
);
188 FcEditDestroy (s
->edit
);
190 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
196 FcConfigDestroy (FcConfig
*config
)
199 FcCacheList
*cl
, *cl_next
;
201 if (config
== _fcConfig
)
204 FcStrSetDestroy (config
->configDirs
);
205 FcStrSetDestroy (config
->fontDirs
);
206 FcStrSetDestroy (config
->cacheDirs
);
207 FcStrSetDestroy (config
->configFiles
);
208 FcStrSetDestroy (config
->acceptGlobs
);
209 FcStrSetDestroy (config
->rejectGlobs
);
210 FcFontSetDestroy (config
->acceptPatterns
);
211 FcFontSetDestroy (config
->rejectPatterns
);
214 FcBlanksDestroy (config
->blanks
);
216 FcSubstDestroy (config
->substPattern
);
217 FcSubstDestroy (config
->substFont
);
218 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
219 if (config
->fonts
[set
])
220 FcFontSetDestroy (config
->fonts
[set
]);
222 for (cl
= config
->caches
; cl
; cl
= cl_next
)
225 FcDirCacheUnmap (cl
->cache
);
230 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
234 * Scan the current list of directories in the configuration
235 * and build the set of available fonts. Update the
236 * per-user cache file to reflect the new configuration
240 FcConfigBuildFonts (FcConfig
*config
)
242 FcFontSet
*fonts
, *cached_fonts
;
247 fonts
= FcFontSetCreate ();
251 oldDirs
= FcStrSetCreate ();
255 cached_fonts
= FcCacheRead(config
);
258 list
= FcConfigGetFontDirs (config
);
262 while ((dir
= FcStrListNext (list
)))
264 if (FcDebug () & FC_DBG_FONTSET
)
265 printf ("build scan dir %s\n", dir
);
266 FcDirScanConfig (fonts
, config
->fontDirs
,
267 config
->blanks
, dir
, FcFalse
, config
);
270 FcStrListDone (list
);
276 for (i
= 0; i
< oldDirs
->num
; i
++)
278 if (FcDebug () & FC_DBG_FONTSET
)
279 printf ("scan dir %s\n", oldDirs
->strs
[i
]);
280 FcDirScanConfig (fonts
, config
->fontDirs
,
281 config
->blanks
, oldDirs
->strs
[i
],
285 for (i
= 0; i
< cached_fonts
->nfont
; i
++)
288 FcPattern
*font
= cached_fonts
->fonts
[i
];
289 FcPatternObjectGetString (font
, FC_FILE_OBJECT
, 0, &cfn
);
291 if (FcConfigAcceptFont (config
, font
) &&
292 (cfn
&& FcConfigAcceptFilename (config
, cfn
)))
293 FcFontSetAdd (fonts
, font
);
295 cached_fonts
->fonts
[i
] = 0; /* prevent free in FcFontSetDestroy */
297 cached_fonts
->nfont
= 0;
298 FcFontSetDestroy (cached_fonts
);
301 if (FcDebug () & FC_DBG_FONTSET
)
302 FcFontSetPrint (fonts
);
304 FcStrSetDestroy (oldDirs
);
306 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
310 FcStrSetDestroy (oldDirs
);
312 FcFontSetDestroy (fonts
);
318 FcConfigSetCurrent (FcConfig
*config
)
321 if (!FcConfigBuildFonts (config
))
325 FcConfigDestroy (_fcConfig
);
331 FcConfigGetCurrent (void)
340 FcConfigAddConfigDir (FcConfig
*config
,
343 return FcStrSetAddFilename (config
->configDirs
, d
);
347 FcConfigGetConfigDirs (FcConfig
*config
)
351 config
= FcConfigGetCurrent ();
355 return FcStrListCreate (config
->configDirs
);
359 FcConfigAddFontDir (FcConfig
*config
,
362 return FcStrSetAddFilename (config
->fontDirs
, d
);
366 FcConfigAddDir (FcConfig
*config
,
369 return (FcConfigAddConfigDir (config
, d
) &&
370 FcConfigAddFontDir (config
, d
));
374 FcConfigGetFontDirs (FcConfig
*config
)
378 config
= FcConfigGetCurrent ();
382 return FcStrListCreate (config
->fontDirs
);
386 FcConfigAddCacheDir (FcConfig
*config
,
389 return FcStrSetAddFilename (config
->cacheDirs
, d
);
393 FcConfigGetCacheDirs (FcConfig
*config
)
397 config
= FcConfigGetCurrent ();
401 return FcStrListCreate (config
->cacheDirs
);
405 FcConfigAddConfigFile (FcConfig
*config
,
409 FcChar8
*file
= FcConfigFilename (f
);
414 ret
= FcStrSetAdd (config
->configFiles
, file
);
420 FcConfigGetConfigFiles (FcConfig
*config
)
424 config
= FcConfigGetCurrent ();
428 return FcStrListCreate (config
->configFiles
);
432 FcConfigGetCache (FcConfig
*config
)
438 FcConfigGetFonts (FcConfig
*config
,
443 config
= FcConfigGetCurrent ();
447 return config
->fonts
[set
];
451 FcConfigSetFonts (FcConfig
*config
,
455 if (config
->fonts
[set
])
456 FcFontSetDestroy (config
->fonts
[set
]);
457 config
->fonts
[set
] = fonts
;
461 FcConfigAddCache (FcConfig
*config
, FcCache
*cache
)
463 FcCacheList
*cl
= malloc (sizeof (FcCacheList
));
468 cl
->next
= config
->caches
;
474 FcConfigGetBlanks (FcConfig
*config
)
478 config
= FcConfigGetCurrent ();
482 return config
->blanks
;
486 FcConfigAddBlank (FcConfig
*config
,
489 FcBlanks
*b
, *freeme
= 0;
494 freeme
= b
= FcBlanksCreate ();
498 if (!FcBlanksAdd (b
, blank
))
501 FcBlanksDestroy (freeme
);
509 FcConfigGetRescanInverval (FcConfig
*config
)
513 config
= FcConfigGetCurrent ();
517 return config
->rescanInterval
;
521 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
525 config
= FcConfigGetCurrent ();
529 config
->rescanInterval
= rescanInterval
;
534 FcConfigAddEdit (FcConfig
*config
,
539 FcSubst
*subst
, **prev
;
543 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
546 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
547 if (kind
== FcMatchPattern
)
548 prev
= &config
->substPattern
;
550 prev
= &config
->substFont
;
551 for (; *prev
; prev
= &(*prev
)->next
);
557 for (t
= test
; t
; t
= t
->next
)
559 if (t
->kind
== FcMatchDefault
)
563 if (config
->maxObjects
< num
)
564 config
->maxObjects
= num
;
565 if (FcDebug () & FC_DBG_EDIT
)
567 printf ("Add Subst ");
568 FcSubstPrint (subst
);
573 typedef struct _FcSubState
{
579 FcConfigPromote (FcValue v
, FcValue u
)
581 if (v
.type
== FcTypeInteger
)
583 v
.type
= FcTypeDouble
;
584 v
.u
.d
= (double) v
.u
.i
;
586 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
588 v
.u
.m
= &FcIdentityMatrix
;
589 v
.type
= FcTypeMatrix
;
591 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
593 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
594 v
.type
= FcTypeLangSet
;
600 FcConfigCompareValue (const FcValue
*left_o
,
602 const FcValue
*right_o
)
604 FcValue left
= FcValueCanonicalize(left_o
);
605 FcValue right
= FcValueCanonicalize(right_o
);
606 FcBool ret
= FcFalse
;
608 left
= FcConfigPromote (left
, right
);
609 right
= FcConfigPromote (right
, left
);
610 if (left
.type
== right
.type
)
614 break; /* FcConfigPromote prevents this from happening */
620 ret
= left
.u
.d
== right
.u
.d
;
623 case FcOpNotContains
:
624 ret
= left
.u
.d
!= right
.u
.d
;
627 ret
= left
.u
.d
< right
.u
.d
;
630 ret
= left
.u
.d
<= right
.u
.d
;
633 ret
= left
.u
.d
> right
.u
.d
;
636 ret
= left
.u
.d
>= right
.u
.d
;
647 ret
= left
.u
.b
== right
.u
.b
;
650 case FcOpNotContains
:
651 ret
= left
.u
.b
!= right
.u
.b
;
661 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
664 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
667 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
669 case FcOpNotContains
:
670 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
681 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
684 case FcOpNotContains
:
685 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
695 /* left contains right if right is a subset of left */
696 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
698 case FcOpNotContains
:
699 /* left contains right if right is a subset of left */
700 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
703 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
706 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
716 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
718 case FcOpNotContains
:
719 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
722 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
725 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
747 ret
= left
.u
.f
== right
.u
.f
;
750 case FcOpNotContains
:
751 ret
= left
.u
.f
!= right
.u
.f
;
761 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
768 #define _FcDoubleFloor(d) ((int) (d))
769 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
770 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
771 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
772 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
773 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
776 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
785 v
.type
= FcTypeInteger
;
789 v
.type
= FcTypeDouble
;
793 v
.type
= FcTypeString
;
794 v
.u
.s
= FcStrStaticName(e
->u
.sval
);
797 v
.type
= FcTypeMatrix
;
802 v
.type
= FcTypeCharSet
;
811 r
= FcPatternObjectGet (p
, e
->u
.object
, 0, &v
);
812 if (r
!= FcResultMatch
)
817 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
818 v
.type
= FcTypeInteger
;
823 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
824 if (vl
.type
== FcTypeBool
)
827 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
829 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
842 case FcOpNotContains
:
844 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
845 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
847 v
.u
.b
= FcConfigCompareValue (&vl
, e
->op
, &vr
);
857 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
858 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
859 vl
= FcConfigPromote (vl
, vr
);
860 vr
= FcConfigPromote (vr
, vl
);
861 if (vl
.type
== vr
.type
)
867 v
.type
= FcTypeDouble
;
868 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
871 v
.type
= FcTypeDouble
;
872 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
875 v
.type
= FcTypeDouble
;
876 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
879 v
.type
= FcTypeDouble
;
880 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
886 if (v
.type
== FcTypeDouble
&&
887 v
.u
.d
== (double) (int) v
.u
.d
)
889 v
.type
= FcTypeInteger
;
897 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
901 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
911 v
.type
= FcTypeString
;
912 str
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
913 v
.u
.s
= FcStrStaticName (str
);
927 v
.type
= FcTypeMatrix
;
928 m
= malloc (sizeof (FcMatrix
));
931 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
932 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
956 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
969 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
975 v
.type
= FcTypeInteger
;
976 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
985 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
991 v
.type
= FcTypeInteger
;
992 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
1001 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1007 v
.type
= FcTypeInteger
;
1008 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
1011 v
.type
= FcTypeVoid
;
1014 FcValueDestroy (vl
);
1017 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1023 v
.type
= FcTypeInteger
;
1024 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1027 v
.type
= FcTypeVoid
;
1030 FcValueDestroy (vl
);
1033 v
.type
= FcTypeVoid
;
1039 static FcValueList
*
1040 FcConfigMatchValueList (FcPattern
*p
,
1042 FcValueList
*values
)
1044 FcValueList
*ret
= 0;
1045 FcExpr
*e
= t
->expr
;
1051 /* Compute the value of the match expression */
1052 if (e
->op
== FcOpComma
)
1054 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1055 e
= e
->u
.tree
.right
;
1059 value
= FcConfigEvaluate (p
, e
);
1063 for (v
= values
; v
; v
= FcValueListNext(v
))
1065 /* Compare the pattern value to the match expression value */
1066 if (FcConfigCompareValue (&v
->value
, t
->op
, &value
))
1073 if (t
->qual
== FcQualAll
)
1080 FcValueDestroy (value
);
1085 static FcValueList
*
1086 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1092 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1095 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1096 if (e
->op
== FcOpComma
)
1098 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1099 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1103 l
->value
= FcConfigEvaluate (p
, e
);
1106 l
->binding
= binding
;
1107 if (l
->value
.type
== FcTypeVoid
)
1109 FcValueList
*next
= FcValueListNext(l
);
1111 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1120 FcConfigAdd (FcValueListPtr
*head
,
1121 FcValueList
*position
,
1125 FcValueListPtr
*prev
, last
, v
;
1126 FcValueBinding sameBinding
;
1129 sameBinding
= position
->binding
;
1131 sameBinding
= FcValueBindingWeak
;
1132 for (v
= new; v
!= NULL
; v
= FcValueListNext(v
))
1133 if (v
->binding
== FcValueBindingSame
)
1134 v
->binding
= sameBinding
;
1138 prev
= &position
->next
;
1140 for (prev
= head
; *prev
!= NULL
;
1141 prev
= &(*prev
)->next
)
1148 for (prev
= head
; *prev
!= NULL
;
1149 prev
= &(*prev
)->next
)
1151 if (*prev
== position
)
1158 if (FcDebug () & FC_DBG_EDIT
)
1161 printf ("position not on list\n");
1165 if (FcDebug () & FC_DBG_EDIT
)
1167 printf ("%s list before ", append
? "Append" : "Prepend");
1168 FcValueListPrint (*head
);
1175 while (last
->next
!= NULL
)
1182 if (FcDebug () & FC_DBG_EDIT
)
1184 printf ("%s list after ", append
? "Append" : "Prepend");
1185 FcValueListPrint (*head
);
1193 FcConfigDel (FcValueListPtr
*head
,
1194 FcValueList
*position
)
1196 FcValueListPtr
*prev
;
1198 for (prev
= head
; *prev
!= NULL
; prev
= &(*prev
)->next
)
1200 if (*prev
== position
)
1202 *prev
= position
->next
;
1203 position
->next
= NULL
;
1204 FcValueListDestroy (position
);
1211 FcConfigPatternAdd (FcPattern
*p
,
1218 FcPatternElt
*e
= FcPatternObjectInsertElt (p
, object
);
1222 FcConfigAdd (&e
->values
, 0, append
, list
);
1227 * Delete all values associated with a field
1230 FcConfigPatternDel (FcPattern
*p
,
1233 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1236 while (e
->values
!= NULL
)
1237 FcConfigDel (&e
->values
, e
->values
);
1241 FcConfigPatternCanon (FcPattern
*p
,
1244 FcPatternElt
*e
= FcPatternObjectFindElt (p
, object
);
1247 if (e
->values
== NULL
)
1248 FcPatternObjectDel (p
, object
);
1252 FcConfigSubstituteWithPat (FcConfig
*config
,
1267 config
= FcConfigGetCurrent ();
1272 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1273 if (!st
&& config
->maxObjects
)
1275 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1277 if (FcDebug () & FC_DBG_EDIT
)
1279 printf ("FcConfigSubstitute ");
1282 if (kind
== FcMatchPattern
)
1283 s
= config
->substPattern
;
1285 s
= config
->substFont
;
1286 for (; s
; s
= s
->next
)
1289 * Check the tests to see if
1290 * they all match the pattern
1292 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1294 if (FcDebug () & FC_DBG_EDIT
)
1296 printf ("FcConfigSubstitute test ");
1300 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1305 st
[i
].elt
= FcPatternObjectFindElt (m
, t
->object
);
1309 * If there's no such field in the font,
1310 * then FcQualAll matches while FcQualAny does not
1314 if (t
->qual
== FcQualAll
)
1323 * Check to see if there is a match, mark the location
1324 * to apply match-relative edits
1326 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1329 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1331 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1336 if (FcDebug () & FC_DBG_EDIT
)
1337 printf ("No match\n");
1340 if (FcDebug () & FC_DBG_EDIT
)
1342 printf ("Substitute ");
1345 for (e
= s
->edit
; e
; e
= e
->next
)
1348 * Evaluate the list of expressions
1350 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1352 * Locate any test associated with this field, skipping
1353 * tests associated with the pattern when substituting in
1356 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1358 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1359 t
->object
== e
->object
)
1362 * KLUDGE - the pattern may have been reallocated or
1363 * things may have been inserted or deleted above
1364 * this element by other edits. Go back and find
1367 if (e
!= s
->edit
&& st
[i
].elt
)
1368 st
[i
].elt
= FcPatternObjectFindElt (p
, t
->object
);
1377 * If there was a test, then replace the matched
1378 * value with the new list of values
1382 FcValueList
*thisValue
= st
[i
].value
;
1383 FcValueList
*nextValue
= thisValue
;
1386 * Append the new list of values after the current value
1388 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1390 * Delete the marked value
1393 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1395 * Adjust any pointers into the value list to ensure
1396 * future edits occur at the same place
1398 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1400 if (st
[i
].value
== thisValue
)
1401 st
[i
].value
= nextValue
;
1405 /* fall through ... */
1406 case FcOpAssignReplace
:
1408 * Delete all of the values and insert
1411 FcConfigPatternDel (p
, e
->object
);
1412 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1414 * Adjust any pointers into the value list as they no
1415 * longer point to anything valid
1419 FcPatternElt
*thisElt
= st
[i
].elt
;
1420 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1422 if (st
[i
].elt
== thisElt
)
1430 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1433 /* fall through ... */
1434 case FcOpPrependFirst
:
1435 FcConfigPatternAdd (p
, e
->object
, l
, FcFalse
);
1440 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1443 /* fall through ... */
1444 case FcOpAppendLast
:
1445 FcConfigPatternAdd (p
, e
->object
, l
, FcTrue
);
1448 FcValueListDestroy (l
);
1453 * Now go through the pattern and eliminate
1454 * any properties without data
1456 for (e
= s
->edit
; e
; e
= e
->next
)
1457 FcConfigPatternCanon (p
, e
->object
);
1459 if (FcDebug () & FC_DBG_EDIT
)
1461 printf ("FcConfigSubstitute edit");
1465 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1467 if (FcDebug () & FC_DBG_EDIT
)
1469 printf ("FcConfigSubstitute done");
1476 FcConfigSubstitute (FcConfig
*config
,
1480 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1483 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1485 static FcChar8 fontconfig_path
[1000] = "";
1488 DllMain (HINSTANCE hinstDLL
,
1494 switch (fdwReason
) {
1495 case DLL_PROCESS_ATTACH
:
1496 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1497 sizeof (fontconfig_path
)))
1500 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1501 * assume it's a Unix-style installation tree, and use
1502 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1503 * folder where the DLL is as FONTCONFIG_PATH.
1505 p
= strrchr (fontconfig_path
, '\\');
1509 p
= strrchr (fontconfig_path
, '\\');
1510 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1511 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1513 strcat (fontconfig_path
, "\\etc\\fonts");
1516 fontconfig_path
[0] = '\0';
1524 #undef FONTCONFIG_PATH
1525 #define FONTCONFIG_PATH fontconfig_path
1527 #else /* !(_WIN32 && PIC) */
1529 #endif /* !(_WIN32 && PIC) */
1531 #ifndef FONTCONFIG_FILE
1532 #define FONTCONFIG_FILE "fonts.conf"
1536 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1541 dir
= (FcChar8
*) "";
1542 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1546 strcpy ((char *) path
, (const char *) dir
);
1547 /* make sure there's a single separator */
1549 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1550 path
[strlen((char *) path
)-1] != '\\')) &&
1553 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1554 strcat ((char *) path
, "\\");
1556 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1557 strcat ((char *) path
, "/");
1559 strcat ((char *) path
, (char *) file
);
1561 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1562 if (access ((char *) path
, R_OK
) == 0)
1570 FcConfigGetPath (void)
1573 FcChar8
*env
, *e
, *colon
;
1578 npath
= 2; /* default dir + null */
1579 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1585 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1588 path
= calloc (npath
, sizeof (FcChar8
*));
1598 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1600 colon
= e
+ strlen ((char *) e
);
1601 path
[i
] = malloc (colon
- e
+ 1);
1604 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1605 path
[i
][colon
- e
] = '\0';
1614 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1615 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1618 strcpy ((char *) path
[i
], (const char *) dir
);
1622 for (i
= 0; path
[i
]; i
++)
1630 FcConfigFreePath (FcChar8
**path
)
1634 for (p
= path
; *p
; p
++)
1639 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1644 if (_FcConfigHomeEnabled
)
1646 char *home
= getenv ("HOME");
1650 home
= getenv ("USERPROFILE");
1653 return (FcChar8
*) home
;
1659 FcConfigEnableHome (FcBool enable
)
1661 FcBool prev
= _FcConfigHomeEnabled
;
1662 _FcConfigHomeEnabled
= enable
;
1667 FcConfigFilename (const FcChar8
*url
)
1669 FcChar8
*file
, *dir
, **path
, **p
;
1673 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1675 url
= (FcChar8
*) FONTCONFIG_FILE
;
1680 if (isalpha (*url
) &&
1682 (url
[2] == '/' || url
[2] == '\\'))
1688 dir
= FcConfigHome ();
1690 file
= FcConfigFileExists (dir
, url
+ 1);
1699 file
= FcConfigFileExists (0, url
);
1702 path
= FcConfigGetPath ();
1705 for (p
= path
; *p
; p
++)
1707 file
= FcConfigFileExists (*p
, url
);
1711 FcConfigFreePath (path
);
1718 * Manage the application-specific fonts
1722 FcConfigAppFontAddFile (FcConfig
*config
,
1723 const FcChar8
*file
)
1732 config
= FcConfigGetCurrent ();
1737 subdirs
= FcStrSetCreate ();
1741 set
= FcConfigGetFonts (config
, FcSetApplication
);
1744 set
= FcFontSetCreate ();
1747 FcStrSetDestroy (subdirs
);
1750 FcConfigSetFonts (config
, set
, FcSetApplication
);
1753 if (!FcFileScanConfig (set
, subdirs
, config
->blanks
, file
, FcFalse
, config
))
1755 FcStrSetDestroy (subdirs
);
1758 if ((sublist
= FcStrListCreate (subdirs
)))
1760 while ((subdir
= FcStrListNext (sublist
)))
1762 FcConfigAppFontAddDir (config
, subdir
);
1764 FcStrListDone (sublist
);
1766 FcStrSetDestroy (subdirs
);
1771 FcConfigAppFontAddDir (FcConfig
*config
,
1781 config
= FcConfigGetCurrent ();
1785 subdirs
= FcStrSetCreate ();
1789 set
= FcConfigGetFonts (config
, FcSetApplication
);
1792 set
= FcFontSetCreate ();
1795 FcStrSetDestroy (subdirs
);
1798 FcConfigSetFonts (config
, set
, FcSetApplication
);
1801 if (!FcDirScanConfig (set
, subdirs
, config
->blanks
, dir
, FcFalse
, config
))
1803 FcStrSetDestroy (subdirs
);
1806 if ((sublist
= FcStrListCreate (subdirs
)))
1808 while ((subdir
= FcStrListNext (sublist
)))
1810 FcConfigAppFontAddDir (config
, subdir
);
1812 FcStrListDone (sublist
);
1814 FcStrSetDestroy (subdirs
);
1819 FcConfigAppFontClear (FcConfig
*config
)
1823 config
= FcConfigGetCurrent ();
1828 FcConfigSetFonts (config
, 0, FcSetApplication
);
1832 * Manage filename-based font source selectors
1836 FcConfigGlobAdd (FcConfig
*config
,
1837 const FcChar8
*glob
,
1840 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1842 return FcStrSetAdd (set
, glob
);
1846 FcConfigGlobMatch (const FcChar8
*glob
,
1847 const FcChar8
*string
)
1851 while ((c
= *glob
++))
1855 /* short circuit common case */
1858 /* short circuit another common case */
1859 if (strchr ((char *) glob
, '*') == 0)
1860 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1863 if (FcConfigGlobMatch (glob
, string
))
1869 if (*string
++ == '\0')
1878 return *string
== '\0';
1882 FcConfigGlobsMatch (const FcStrSet
*globs
,
1883 const FcChar8
*string
)
1887 for (i
= 0; i
< globs
->num
; i
++)
1888 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1894 FcConfigAcceptFilename (FcConfig
*config
,
1895 const FcChar8
*filename
)
1897 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1899 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1905 * Manage font-pattern based font source selectors
1909 FcConfigPatternsAdd (FcConfig
*config
,
1913 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1915 return FcFontSetAdd (set
, pattern
);
1919 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1920 const FcPattern
*font
)
1924 for (i
= 0; i
< patterns
->nfont
; i
++)
1925 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1931 FcConfigAcceptFont (FcConfig
*config
,
1932 const FcPattern
*font
)
1934 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1936 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))