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 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
33 #if defined (_WIN32) && !defined (R_OK)
45 config
= malloc (sizeof (FcConfig
));
48 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
50 config
->configDirs
= FcStrSetCreate ();
51 if (!config
->configDirs
)
54 config
->configFiles
= FcStrSetCreate ();
55 if (!config
->configFiles
)
58 config
->fontDirs
= FcStrSetCreate ();
59 if (!config
->fontDirs
)
62 config
->acceptGlobs
= FcStrSetCreate ();
63 if (!config
->acceptGlobs
)
66 config
->rejectGlobs
= FcStrSetCreate ();
67 if (!config
->rejectGlobs
)
70 config
->acceptPatterns
= FcFontSetCreate ();
71 if (!config
->acceptPatterns
)
74 config
->rejectPatterns
= FcFontSetCreate ();
75 if (!config
->rejectPatterns
)
80 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
84 if (config
->cache
== 0)
86 /* If no home, use the temp folder. */
88 int templen
= GetTempPath (1, dummy
);
89 FcChar8
*temp
= malloc (templen
+ 1);
95 GetTempPath (templen
+ 1, temp
);
96 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
98 if (!FcConfigSetCache (config
, cache_dir
))
100 FcStrFree (cache_dir
);
103 FcStrFree (cache_dir
);
110 config
->substPattern
= 0;
111 config
->substFont
= 0;
112 config
->maxObjects
= 0;
113 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
114 config
->fonts
[set
] = 0;
116 config
->rescanTime
= time(0);
117 config
->rescanInterval
= 30;
122 FcFontSetDestroy (config
->rejectPatterns
);
124 FcFontSetDestroy (config
->acceptPatterns
);
126 FcStrSetDestroy (config
->rejectGlobs
);
128 FcStrSetDestroy (config
->acceptGlobs
);
130 FcStrSetDestroy (config
->fontDirs
);
132 FcStrSetDestroy (config
->configFiles
);
134 FcStrSetDestroy (config
->configDirs
);
137 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
142 typedef struct _FcFileTime
{
148 FcConfigNewestFile (FcStrSet
*files
)
150 FcStrList
*list
= FcStrListCreate (files
);
151 FcFileTime newest
= { 0, FcFalse
};
157 while ((file
= FcStrListNext (list
)))
158 if (stat ((char *) file
, &statb
) == 0)
159 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
162 newest
.time
= statb
.st_mtime
;
164 FcStrListDone (list
);
170 FcConfigUptoDate (FcConfig
*config
)
172 FcFileTime config_time
, font_time
;
173 time_t now
= time(0);
176 config
= FcConfigGetCurrent ();
180 config_time
= FcConfigNewestFile (config
->configFiles
);
181 font_time
= FcConfigNewestFile (config
->fontDirs
);
182 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
183 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
187 config
->rescanTime
= now
;
192 FcSubstDestroy (FcSubst
*s
)
200 FcTestDestroy (s
->test
);
202 FcEditDestroy (s
->edit
);
204 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
210 FcConfigDestroy (FcConfig
*config
)
214 if (config
== _fcConfig
)
217 FcStrSetDestroy (config
->configDirs
);
218 FcStrSetDestroy (config
->fontDirs
);
219 FcStrSetDestroy (config
->configFiles
);
220 FcStrSetDestroy (config
->acceptGlobs
);
221 FcStrSetDestroy (config
->rejectGlobs
);
222 FcFontSetDestroy (config
->acceptPatterns
);
223 FcFontSetDestroy (config
->rejectPatterns
);
226 FcBlanksDestroy (config
->blanks
);
229 FcStrFree (config
->cache
);
231 FcSubstDestroy (config
->substPattern
);
232 FcSubstDestroy (config
->substFont
);
233 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
234 if (config
->fonts
[set
])
235 FcFontSetDestroy (config
->fonts
[set
]);
238 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
242 * Scan the current list of directories in the configuration
243 * and build the set of available fonts. Update the
244 * per-user cache file to reflect the new configuration
248 FcConfigBuildFonts (FcConfig
*config
)
251 FcGlobalCache
*cache
;
255 fonts
= FcFontSetCreate ();
259 cache
= FcGlobalCacheCreate ();
264 FcGlobalCacheLoad (cache
, config
->cache
);
266 list
= FcConfigGetFontDirs (config
);
270 while ((dir
= FcStrListNext (list
)))
272 if (FcDebug () & FC_DBG_FONTSET
)
273 printf ("scan dir %s\n", dir
);
274 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
275 config
->blanks
, dir
, FcFalse
, config
);
278 FcStrListDone (list
);
280 if (FcDebug () & FC_DBG_FONTSET
)
281 FcFontSetPrint (fonts
);
284 FcGlobalCacheSave (cache
, config
->cache
);
285 FcGlobalCacheDestroy (cache
);
287 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
291 FcFontSetDestroy (fonts
);
297 FcConfigSetCurrent (FcConfig
*config
)
300 if (!FcConfigBuildFonts (config
))
304 FcConfigDestroy (_fcConfig
);
310 FcConfigGetCurrent (void)
319 FcConfigAddConfigDir (FcConfig
*config
,
322 return FcStrSetAddFilename (config
->configDirs
, d
);
326 FcConfigGetConfigDirs (FcConfig
*config
)
330 config
= FcConfigGetCurrent ();
334 return FcStrListCreate (config
->configDirs
);
338 FcConfigAddFontDir (FcConfig
*config
,
341 return FcStrSetAddFilename (config
->fontDirs
, d
);
345 FcConfigAddDir (FcConfig
*config
,
348 return (FcConfigAddConfigDir (config
, d
) &&
349 FcConfigAddFontDir (config
, d
));
353 FcConfigGetFontDirs (FcConfig
*config
)
357 config
= FcConfigGetCurrent ();
361 return FcStrListCreate (config
->fontDirs
);
365 FcConfigAddConfigFile (FcConfig
*config
,
369 FcChar8
*file
= FcConfigFilename (f
);
374 ret
= FcStrSetAdd (config
->configFiles
, file
);
380 FcConfigGetConfigFiles (FcConfig
*config
)
384 config
= FcConfigGetCurrent ();
388 return FcStrListCreate (config
->configFiles
);
392 FcConfigSetCache (FcConfig
*config
,
395 FcChar8
*new = FcStrCopyFilename (c
);
400 FcStrFree (config
->cache
);
406 FcConfigGetCache (FcConfig
*config
)
410 config
= FcConfigGetCurrent ();
414 return config
->cache
;
418 FcConfigGetFonts (FcConfig
*config
,
423 config
= FcConfigGetCurrent ();
427 return config
->fonts
[set
];
431 FcConfigSetFonts (FcConfig
*config
,
435 if (config
->fonts
[set
])
436 FcFontSetDestroy (config
->fonts
[set
]);
437 config
->fonts
[set
] = fonts
;
443 FcConfigGetBlanks (FcConfig
*config
)
447 config
= FcConfigGetCurrent ();
451 return config
->blanks
;
455 FcConfigAddBlank (FcConfig
*config
,
463 b
= FcBlanksCreate ();
467 if (!FcBlanksAdd (b
, blank
))
474 FcConfigGetRescanInverval (FcConfig
*config
)
478 config
= FcConfigGetCurrent ();
482 return config
->rescanInterval
;
486 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
490 config
= FcConfigGetCurrent ();
494 config
->rescanInterval
= rescanInterval
;
499 FcConfigAddEdit (FcConfig
*config
,
504 FcSubst
*subst
, **prev
;
508 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
511 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
512 if (kind
== FcMatchPattern
)
513 prev
= &config
->substPattern
;
515 prev
= &config
->substFont
;
516 for (; *prev
; prev
= &(*prev
)->next
);
522 for (t
= test
; t
; t
= t
->next
)
524 if (t
->kind
== FcMatchDefault
)
528 if (config
->maxObjects
< num
)
529 config
->maxObjects
= num
;
530 if (FcDebug () & FC_DBG_EDIT
)
532 printf ("Add Subst ");
533 FcSubstPrint (subst
);
538 typedef struct _FcSubState
{
544 FcConfigPromote (FcValue v
, FcValue u
)
546 if (v
.type
== FcTypeInteger
)
548 v
.type
= FcTypeDouble
;
549 v
.u
.d
= (double) v
.u
.i
;
551 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
553 v
.u
.m
= &FcIdentityMatrix
;
554 v
.type
= FcTypeMatrix
;
556 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
558 v
.u
.l
= FcLangSetPromote (v
.u
.s
);
559 v
.type
= FcTypeLangSet
;
565 FcConfigCompareValue (const FcValue left_o
,
567 const FcValue right_o
)
569 FcValue left
= left_o
;
570 FcValue right
= right_o
;
571 FcBool ret
= FcFalse
;
573 left
= FcConfigPromote (left
, right
);
574 right
= FcConfigPromote (right
, left
);
575 if (left
.type
== right
.type
)
579 break; /* FcConfigPromote prevents this from happening */
585 ret
= left
.u
.d
== right
.u
.d
;
588 case FcOpNotContains
:
589 ret
= left
.u
.d
!= right
.u
.d
;
592 ret
= left
.u
.d
< right
.u
.d
;
595 ret
= left
.u
.d
<= right
.u
.d
;
598 ret
= left
.u
.d
> right
.u
.d
;
601 ret
= left
.u
.d
>= right
.u
.d
;
612 ret
= left
.u
.b
== right
.u
.b
;
615 case FcOpNotContains
:
616 ret
= left
.u
.b
!= right
.u
.b
;
626 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
629 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
632 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
634 case FcOpNotContains
:
635 ret
= FcStrStrIgnoreCase (left
.u
.s
, right
.u
.s
) == 0;
646 ret
= FcMatrixEqual (left
.u
.m
, right
.u
.m
);
649 case FcOpNotContains
:
650 ret
= !FcMatrixEqual (left
.u
.m
, right
.u
.m
);
660 /* left contains right if right is a subset of left */
661 ret
= FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
663 case FcOpNotContains
:
664 /* left contains right if right is a subset of left */
665 ret
= !FcCharSetIsSubset (right
.u
.c
, left
.u
.c
);
668 ret
= FcCharSetEqual (left
.u
.c
, right
.u
.c
);
671 ret
= !FcCharSetEqual (left
.u
.c
, right
.u
.c
);
681 ret
= FcLangSetContains (left
.u
.l
, right
.u
.l
);
683 case FcOpNotContains
:
684 ret
= !FcLangSetContains (left
.u
.l
, right
.u
.l
);
687 ret
= FcLangSetEqual (left
.u
.l
, right
.u
.l
);
690 ret
= !FcLangSetEqual (left
.u
.l
, right
.u
.l
);
712 ret
= left
.u
.f
== right
.u
.f
;
715 case FcOpNotContains
:
716 ret
= left
.u
.f
!= right
.u
.f
;
726 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
733 #define _FcDoubleFloor(d) ((int) (d))
734 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
735 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
736 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
737 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
738 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
741 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
749 v
.type
= FcTypeInteger
;
753 v
.type
= FcTypeDouble
;
757 v
.type
= FcTypeString
;
762 v
.type
= FcTypeMatrix
;
767 v
.type
= FcTypeCharSet
;
776 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
777 if (r
!= FcResultMatch
)
781 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
782 v
.type
= FcTypeInteger
;
787 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
788 if (vl
.type
== FcTypeBool
)
791 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
793 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
806 case FcOpNotContains
:
808 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
809 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
811 v
.u
.b
= FcConfigCompareValue (vl
, e
->op
, vr
);
821 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
822 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
823 vl
= FcConfigPromote (vl
, vr
);
824 vr
= FcConfigPromote (vr
, vl
);
825 if (vl
.type
== vr
.type
)
831 v
.type
= FcTypeDouble
;
832 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
835 v
.type
= FcTypeDouble
;
836 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
839 v
.type
= FcTypeDouble
;
840 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
843 v
.type
= FcTypeDouble
;
844 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
850 if (v
.type
== FcTypeDouble
&&
851 v
.u
.d
== (double) (int) v
.u
.d
)
853 v
.type
= FcTypeInteger
;
861 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
865 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
875 v
.type
= FcTypeString
;
876 v
.u
.s
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
888 v
.type
= FcTypeMatrix
;
889 m
= malloc (sizeof (FcMatrix
));
892 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
893 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
917 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
930 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
936 v
.type
= FcTypeInteger
;
937 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
946 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
952 v
.type
= FcTypeInteger
;
953 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
962 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
968 v
.type
= FcTypeInteger
;
969 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
978 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
984 v
.type
= FcTypeInteger
;
985 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1000 static FcValueList
*
1001 FcConfigMatchValueList (FcPattern
*p
,
1003 FcValueList
*values
)
1005 FcValueList
*ret
= 0;
1006 FcExpr
*e
= t
->expr
;
1012 /* Compute the value of the match expression */
1013 if (e
->op
== FcOpComma
)
1015 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1016 e
= e
->u
.tree
.right
;
1020 value
= FcConfigEvaluate (p
, e
);
1024 for (v
= values
; v
; v
= v
->next
)
1026 /* Compare the pattern value to the match expression value */
1027 if (FcConfigCompareValue (v
->value
, t
->op
, value
))
1034 if (t
->qual
== FcQualAll
)
1041 FcValueDestroy (value
);
1046 static FcValueList
*
1047 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1053 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1056 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1057 if (e
->op
== FcOpComma
)
1059 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1060 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
, binding
);
1064 l
->value
= FcConfigEvaluate (p
, e
);
1067 l
->binding
= binding
;
1068 while (l
&& l
->value
.type
== FcTypeVoid
)
1070 FcValueList
*next
= l
->next
;
1072 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1080 FcConfigAdd (FcValueList
**head
,
1081 FcValueList
*position
,
1085 FcValueList
**prev
, *last
, *v
;
1086 FcValueBinding sameBinding
;
1089 sameBinding
= position
->binding
;
1091 sameBinding
= FcValueBindingWeak
;
1092 for (v
= new; v
; v
= v
->next
)
1093 if (v
->binding
== FcValueBindingSame
)
1094 v
->binding
= sameBinding
;
1098 prev
= &position
->next
;
1100 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
1107 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
1109 if (*prev
== position
)
1116 if (FcDebug () & FC_DBG_EDIT
)
1119 printf ("position not on list\n");
1123 if (FcDebug () & FC_DBG_EDIT
)
1125 printf ("%s list before ", append
? "Append" : "Prepend");
1126 FcValueListPrint (*head
);
1140 if (FcDebug () & FC_DBG_EDIT
)
1142 printf ("%s list after ", append
? "Append" : "Prepend");
1143 FcValueListPrint (*head
);
1151 FcConfigDel (FcValueList
**head
,
1152 FcValueList
*position
)
1156 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
1158 if (*prev
== position
)
1160 *prev
= position
->next
;
1162 FcValueListDestroy (position
);
1169 FcConfigPatternAdd (FcPattern
*p
,
1176 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1180 FcConfigAdd (&e
->values
, 0, append
, list
);
1185 * Delete all values associated with a field
1188 FcConfigPatternDel (FcPattern
*p
,
1191 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1195 FcConfigDel (&e
->values
, e
->values
);
1199 FcConfigPatternCanon (FcPattern
*p
,
1202 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1206 FcPatternDel (p
, object
);
1210 FcConfigSubstituteWithPat (FcConfig
*config
,
1225 config
= FcConfigGetCurrent ();
1230 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1231 if (!st
&& config
->maxObjects
)
1233 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1235 if (FcDebug () & FC_DBG_EDIT
)
1237 printf ("FcConfigSubstitute ");
1240 if (kind
== FcMatchPattern
)
1241 s
= config
->substPattern
;
1243 s
= config
->substFont
;
1244 for (; s
; s
= s
->next
)
1247 * Check the tests to see if
1248 * they all match the pattern
1250 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1252 if (FcDebug () & FC_DBG_EDIT
)
1254 printf ("FcConfigSubstitute test ");
1258 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1263 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1267 * If there's no such field in the font,
1268 * then FcQualAll matches while FcQualAny does not
1272 if (t
->qual
== FcQualAll
)
1281 * Check to see if there is a match, mark the location
1282 * to apply match-relative edits
1284 st
[i
].value
= FcConfigMatchValueList (m
, t
, st
[i
].elt
->values
);
1287 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= st
[i
].elt
->values
)
1289 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== st
[i
].elt
->values
)
1294 if (FcDebug () & FC_DBG_EDIT
)
1295 printf ("No match\n");
1298 if (FcDebug () & FC_DBG_EDIT
)
1300 printf ("Substitute ");
1303 for (e
= s
->edit
; e
; e
= e
->next
)
1306 * Evaluate the list of expressions
1308 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1310 * Locate any test associated with this field, skipping
1311 * tests associated with the pattern when substituting in
1314 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1316 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1317 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1318 (FcChar8
*) e
->field
))
1321 * KLUDGE - the pattern may have been reallocated or
1322 * things may have been inserted or deleted above
1323 * this element by other edits. Go back and find
1326 if (e
!= s
->edit
&& st
[i
].elt
)
1327 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1336 * If there was a test, then replace the matched
1337 * value with the new list of values
1341 FcValueList
*thisValue
= st
[i
].value
;
1342 FcValueList
*nextValue
= thisValue
? thisValue
->next
: 0;
1345 * Append the new list of values after the current value
1347 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1349 * Delete the marked value
1351 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1353 * Adjust any pointers into the value list to ensure
1354 * future edits occur at the same place
1356 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1358 if (st
[i
].value
== thisValue
)
1359 st
[i
].value
= nextValue
;
1363 /* fall through ... */
1364 case FcOpAssignReplace
:
1366 * Delete all of the values and insert
1369 FcConfigPatternDel (p
, e
->field
);
1370 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1372 * Adjust any pointers into the value list as they no
1373 * longer point to anything valid
1377 FcPatternElt
*thisElt
= st
[i
].elt
;
1378 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1380 if (st
[i
].elt
== thisElt
)
1388 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1391 /* fall through ... */
1392 case FcOpPrependFirst
:
1393 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1398 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1401 /* fall through ... */
1402 case FcOpAppendLast
:
1403 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1410 * Now go through the pattern and eliminate
1411 * any properties without data
1413 for (e
= s
->edit
; e
; e
= e
->next
)
1414 FcConfigPatternCanon (p
, e
->field
);
1416 if (FcDebug () & FC_DBG_EDIT
)
1418 printf ("FcConfigSubstitute edit");
1422 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1424 if (FcDebug () & FC_DBG_EDIT
)
1426 printf ("FcConfigSubstitute done");
1433 FcConfigSubstitute (FcConfig
*config
,
1437 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1440 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1442 static FcChar8 fontconfig_path
[1000] = "";
1445 DllMain (HINSTANCE hinstDLL
,
1451 switch (fdwReason
) {
1452 case DLL_PROCESS_ATTACH
:
1453 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1454 sizeof (fontconfig_path
)))
1457 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1458 * assume it's a Unix-style installation tree, and use
1459 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1460 * folder where the DLL is as FONTCONFIG_PATH.
1462 p
= strrchr (fontconfig_path
, '\\');
1466 p
= strrchr (fontconfig_path
, '\\');
1467 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1468 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1470 strcat (fontconfig_path
, "\\etc\\fonts");
1473 fontconfig_path
[0] = '\0';
1481 #undef FONTCONFIG_PATH
1482 #define FONTCONFIG_PATH fontconfig_path
1484 #else /* !(_WIN32 && PIC) */
1486 #endif /* !(_WIN32 && PIC) */
1488 #ifndef FONTCONFIG_FILE
1489 #define FONTCONFIG_FILE "fonts.conf"
1493 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1498 dir
= (FcChar8
*) "";
1499 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1503 strcpy ((char *) path
, (const char *) dir
);
1504 /* make sure there's a single separator */
1506 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1507 path
[strlen((char *) path
)-1] != '\\')) &&
1510 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1511 strcat ((char *) path
, "\\");
1513 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1514 strcat ((char *) path
, "/");
1516 strcat ((char *) path
, (char *) file
);
1518 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1519 if (access ((char *) path
, R_OK
) == 0)
1527 FcConfigGetPath (void)
1530 FcChar8
*env
, *e
, *colon
;
1535 npath
= 2; /* default dir + null */
1536 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1542 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1545 path
= calloc (npath
, sizeof (FcChar8
*));
1555 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1557 colon
= e
+ strlen ((char *) e
);
1558 path
[i
] = malloc (colon
- e
+ 1);
1561 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1562 path
[i
][colon
- e
] = '\0';
1571 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1572 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1575 strcpy ((char *) path
[i
], (const char *) dir
);
1579 for (i
= 0; path
[i
]; i
++)
1587 FcConfigFreePath (FcChar8
**path
)
1591 for (p
= path
; *p
; p
++)
1596 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1601 if (_FcConfigHomeEnabled
)
1603 char *home
= getenv ("HOME");
1607 home
= getenv ("USERPROFILE");
1616 FcConfigEnableHome (FcBool enable
)
1618 FcBool prev
= _FcConfigHomeEnabled
;
1619 _FcConfigHomeEnabled
= enable
;
1624 FcConfigFilename (const FcChar8
*url
)
1626 FcChar8
*file
, *dir
, **path
, **p
;
1630 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1632 url
= (FcChar8
*) FONTCONFIG_FILE
;
1637 if (isalpha (*url
) &&
1639 (url
[2] == '/' || url
[2] == '\\'))
1645 dir
= FcConfigHome ();
1647 file
= FcConfigFileExists (dir
, url
+ 1);
1656 file
= FcConfigFileExists (0, url
);
1659 path
= FcConfigGetPath ();
1662 for (p
= path
; *p
; p
++)
1664 file
= FcConfigFileExists (*p
, url
);
1668 FcConfigFreePath (path
);
1675 * Manage the application-specific fonts
1679 FcConfigAppFontAddFile (FcConfig
*config
,
1680 const FcChar8
*file
)
1689 config
= FcConfigGetCurrent ();
1694 subdirs
= FcStrSetCreate ();
1698 set
= FcConfigGetFonts (config
, FcSetApplication
);
1701 set
= FcFontSetCreate ();
1704 FcStrSetDestroy (subdirs
);
1707 FcConfigSetFonts (config
, set
, FcSetApplication
);
1710 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1712 FcStrSetDestroy (subdirs
);
1715 if ((sublist
= FcStrListCreate (subdirs
)))
1717 while ((subdir
= FcStrListNext (sublist
)))
1719 FcConfigAppFontAddDir (config
, subdir
);
1721 FcStrListDone (sublist
);
1727 FcConfigAppFontAddDir (FcConfig
*config
,
1737 config
= FcConfigGetCurrent ();
1741 subdirs
= FcStrSetCreate ();
1745 set
= FcConfigGetFonts (config
, FcSetApplication
);
1748 set
= FcFontSetCreate ();
1751 FcStrSetDestroy (subdirs
);
1754 FcConfigSetFonts (config
, set
, FcSetApplication
);
1757 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1759 FcStrSetDestroy (subdirs
);
1762 if ((sublist
= FcStrListCreate (subdirs
)))
1764 while ((subdir
= FcStrListNext (sublist
)))
1766 FcConfigAppFontAddDir (config
, subdir
);
1768 FcStrListDone (sublist
);
1774 FcConfigAppFontClear (FcConfig
*config
)
1778 config
= FcConfigGetCurrent ();
1783 FcConfigSetFonts (config
, 0, FcSetApplication
);
1787 * Manage filename-based font source selectors
1791 FcConfigGlobAdd (FcConfig
*config
,
1792 const FcChar8
*glob
,
1795 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1797 return FcStrSetAdd (set
, glob
);
1801 FcConfigGlobMatch (const FcChar8
*glob
,
1802 const FcChar8
*string
)
1806 while ((c
= *glob
++))
1810 /* short circuit common case */
1813 /* short circuit another common case */
1814 if (strchr ((char *) glob
, '*') == 0)
1815 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1818 if (FcConfigGlobMatch (glob
, string
))
1824 if (*string
++ == '\0')
1833 return *string
== '\0';
1837 FcConfigGlobsMatch (const FcStrSet
*globs
,
1838 const FcChar8
*string
)
1842 for (i
= 0; i
< globs
->num
; i
++)
1843 if (FcConfigGlobMatch (globs
->strs
[i
], string
))
1849 FcConfigAcceptFilename (FcConfig
*config
,
1850 const FcChar8
*filename
)
1852 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1854 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1860 * Manage font-pattern based font source selectors
1864 FcConfigPatternsAdd (FcConfig
*config
,
1868 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1870 return FcFontSetAdd (set
, pattern
);
1874 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1875 const FcPattern
*font
)
1879 for (i
= 0; i
< patterns
->nfont
; i
++)
1880 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1886 FcConfigAcceptFont (FcConfig
*config
,
1887 const FcPattern
*font
)
1889 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1891 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))